diff --git a/data/common/romfs/04b_19.fnt b/data/common/romfs/04b_19.fnt deleted file mode 100644 index c4296636..00000000 Binary files a/data/common/romfs/04b_19.fnt and /dev/null differ diff --git a/data/common/romfs/04b_19_0.png b/data/common/romfs/04b_19_0.png deleted file mode 100644 index fe52d738..00000000 Binary files a/data/common/romfs/04b_19_0.png and /dev/null differ diff --git a/data/common/romfs/future.bmfc b/data/common/romfs/future.bmfc new file mode 100644 index 00000000..1bfd3fff --- /dev/null +++ b/data/common/romfs/future.bmfc @@ -0,0 +1,59 @@ +# AngelCode Bitmap Font Generator configuration file +fileVersion=1 + +# font settings +fontName=This Is The Future +fontFile= +charSet=0 +fontSize=-20 +aa=1 +scaleH=100 +useSmoothing=1 +isBold=0 +isItalic=0 +useUnicode=1 +disableBoxChars=1 +outputInvalidCharGlyph=0 +dontIncludeKerningPairs=0 +useHinting=1 +renderFromOutline=1 +useClearType=1 +autoFitNumPages=0 +autoFitFontSizeMin=0 +autoFitFontSizeMax=0 + +# character alignment +paddingDown=0 +paddingUp=0 +paddingRight=0 +paddingLeft=0 +spacingHoriz=1 +spacingVert=1 +useFixedHeight=0 +forceZero=0 +widthPaddingFactor=0.00 + +# output file +outWidth=256 +outHeight=256 +outBitDepth=32 +fontDescFormat=2 +fourChnlPacked=0 +textureFormat=png +textureCompression=0 +alphaChnl=1 +redChnl=0 +greenChnl=0 +blueChnl=0 +invA=0 +invR=0 +invG=0 +invB=0 + +# outline +outlineThickness=2 + +# selected chars +chars=0-0,29,32-126 + +# imported icon images diff --git a/data/common/romfs/future.fnt b/data/common/romfs/future.fnt new file mode 100644 index 00000000..f0041247 Binary files /dev/null and b/data/common/romfs/future.fnt differ diff --git a/data/common/romfs/future.ttf b/data/common/romfs/future.ttf new file mode 100644 index 00000000..328ced6c Binary files /dev/null and b/data/common/romfs/future.ttf differ diff --git a/data/common/romfs/future_0.png b/data/common/romfs/future_0.png new file mode 100644 index 00000000..60344c74 Binary files /dev/null and b/data/common/romfs/future_0.png differ diff --git a/source/skeleton/sfml/BMFont.cpp b/source/skeleton/sfml/BMFont.cpp index 2594316d..9758e4c1 100644 --- a/source/skeleton/sfml/BMFont.cpp +++ b/source/skeleton/sfml/BMFont.cpp @@ -46,12 +46,12 @@ namespace c2d { //////////////////////////////////////////////////////////// bool BMFont::loadFromFile(const std::string &fntPath) { - char *fontData = nullptr; - size_t fontSize = c2d_renderer->getIo()->read(fntPath, &fontData); - if (!fontData || fontSize < 4) { + char *data = nullptr; + size_t len = c2d_renderer->getIo()->read(fntPath, &data); + if (!data || len < 4) { printf("BMFont::loadFromFile(%s): could not read font file...\n", fntPath.c_str()); - if (fontData) { - free(fontData); + if (data) { + free(data); } return false; } @@ -61,15 +61,15 @@ namespace c2d { size_t texSize = c2d_renderer->getIo()->read(texPath, &texData); if (!texData || texSize < 4) { printf("BMFont::loadFromFile(%s): could not read texture file...\n", texPath.c_str()); - free(fontData); + free(data); if (texData) { free(texData); } return false; } - bool ret = loadFromMemory(fontData, fontSize, texData, texSize); - free(fontData); + bool ret = loadFromMemory(data, len, texData, texSize); + free(data); free(texData); return ret; } @@ -99,7 +99,7 @@ namespace c2d { uint32_t blockSize = stream.getU32(); switch (blockID) { case BMFONT_BLOCK_TYPE_INFO: { - m_bmfont.info.fontSize = stream.getS16(); + m_bmfont.info.fontSize = (int16_t) abs(stream.getS16()); m_bmfont.info.bitField = stream.getU8(); m_bmfont.info.charSet = stream.getU8(); m_bmfont.info.stretchH = stream.getU16(); @@ -159,12 +159,11 @@ namespace c2d { // create glyph Glyph glyph = { // advance - (float) bmfChar.xadvance + (float) m_bmfont.info.outline / 2, + (float) bmfChar.xadvance, // bounds { - (float) (bmfChar.xoffset), - (float) (bmfChar.yoffset - m_bmfont.common.lineHeight) + - ((float) m_bmfont.info.outline / 2), + (float) (bmfChar.xoffset + m_bmfont.info.outline), + (float) (bmfChar.yoffset - m_bmfont.common.base + m_bmfont.info.outline), bmfChar.width, bmfChar.height }, @@ -179,7 +178,7 @@ namespace c2d { } case BMFONT_BLOCK_TYPE_KERNINGS: { int32_t kerningPairsCount = (int32_t) blockSize / 10; - for (int32_t index = 0; index < kerningPairsCount; ++index) { + for (int32_t index = 0; index < kerningPairsCount; index++) { m_bmfont.kerningPairs.push_back({stream.getU32(), stream.getU32(), stream.getS16()}); } break; @@ -195,8 +194,9 @@ namespace c2d { return false; } - printf("BMFont::loadFromMemory: loaded %lu chars (font size: %i, outline size: %i)\n", - m_bmfont.glyphs.size(), m_bmfont.info.fontSize, m_bmfont.info.outline); + printf("BMFont::loadFromMemory: loaded %lu chars (size: %i, outline: %i, base: %i, line height: %i)\n", + m_bmfont.glyphs.size(), m_bmfont.info.fontSize, m_bmfont.info.outline, + m_bmfont.common.base, m_bmfont.common.lineHeight); return true; } @@ -209,7 +209,9 @@ namespace c2d { bool bold, float outlineThickness) const { if (m_bmfont.glyphs.count((int) codePoint)) { if ((int16_t) characterSize == m_bmfont.info.fontSize) { - return m_bmfont.glyphs.at((int) codePoint); + s_glyph = m_bmfont.glyphs.at((int) codePoint); + //printf("getGlyph(%c): advance: %f\n", codePoint, s_glyph.advance); + return s_glyph; } else { s_glyph = m_bmfont.glyphs.at((int) codePoint); float scaling = (float) characterSize / (float) (m_bmfont.info.fontSize + m_bmfont.info.outline); @@ -227,14 +229,18 @@ namespace c2d { //////////////////////////////////////////////////////////// float BMFont::getKerning(uint32_t first, uint32_t second, unsigned int characterSize, bool bold) const { - if (!m_bmfont.kerningPairs.empty()) { - float scaling = (float) characterSize / (float) m_bmfont.info.fontSize; - for (const auto &kerningPair: m_bmfont.kerningPairs) { - if (kerningPair.first == first && kerningPair.second == second) { - return (float) kerningPair.amount * scaling; - } + if (first == 0 || second == 0) return 0; + if (m_bmfont.kerningPairs.empty()) return 0; + + float scaling = (float) characterSize / (float) m_bmfont.info.fontSize; + for (const auto &kerningPair: m_bmfont.kerningPairs) { + if (kerningPair.first == first && kerningPair.second == second) { + float kerning = (float) kerningPair.amount * scaling; + //printf("BMFont::getKerning(%c %c): %f\n", first, second, kerning); + return kerning; } } + return 0; } diff --git a/source/skeleton/sfml/Font.cpp b/source/skeleton/sfml/Font.cpp index 7adcf596..3e672fa5 100644 --- a/source/skeleton/sfml/Font.cpp +++ b/source/skeleton/sfml/Font.cpp @@ -205,6 +205,7 @@ namespace c2d { } else { // Not found: we have to load it Glyph glyph = loadGlyph(codePoint, characterSize, bold, outlineThickness); + //printf("Font::getGlyph(%c): advance: %f\n", codePoint, glyph.advance); return glyphs.emplace(key, glyph).first->second; } } diff --git a/test/main.cpp b/test/main.cpp index 76af3cd5..d83b40c7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -43,22 +43,24 @@ int main(int argc, char *argv[]) { #ifndef __NO_FREETYPE__ // create a text - auto text = new Text("libcross2d @ Cpasjuste"); + auto font = new Font(); + font->loadFromFile(renderer->getIo()->getRomFsPath() + "future.ttf"); + auto text = new Text("libcross2d @ Cpasjuste", 20, font); text->setOutlineThickness(2); - text->setPosition(rect->getSize().x - 16 * scaling, rect->getSize().y - 16 * scaling); - text->setOrigin(Origin::BottomRight); - text->setScale(scaling, scaling); - rect->add(text); -#else - // create a bitmap text (https://www.angelcode.com/products/bmfont/) - auto font = new BMFont(); - font->loadFromFile(renderer->getIo()->getRomFsPath() + "04b_19.fnt"); - auto text = new Text("libcross2d @ Cpasjuste", 24, font); - text->setPosition(renderer->getSize().x - 8 * scaling, renderer->getSize().y - 8 * scaling); + text->setPosition(renderer->getSize().x - 8, renderer->getSize().y - 8); text->setOrigin(Origin::BottomRight); renderer->add(text); #endif + // create a bitmap text (https://www.angelcode.com/products/bmfont/) + auto bmFont = new BMFont(); + bmFont->loadFromFile(renderer->getIo()->getRomFsPath() + "future.fnt"); + auto bmText = new Text("libcross2d @ Cpasjuste", 20, bmFont); + bmText->setFillColor(Color::Red); + bmText->setPosition(renderer->getSize().x - 8, renderer->getSize().y - 40); + bmText->setOrigin(Origin::BottomRight); + renderer->add(bmText); + // add all this crap to the renderer renderer->add(rect); @@ -93,6 +95,11 @@ int main(int argc, char *argv[]) { renderer->flip(); } +#ifndef __NO_FREETYPE__ + delete (font); +#endif + delete (bmFont); + // will delete child's (textures, shapes, text..) delete (renderer);