Skip to content

Commit

Permalink
BMFont: few fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cpasjuste committed Dec 24, 2022
1 parent d7a3f48 commit da6f116
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 34 deletions.
Binary file removed data/common/romfs/04b_19.fnt
Binary file not shown.
Binary file removed data/common/romfs/04b_19_0.png
Binary file not shown.
59 changes: 59 additions & 0 deletions data/common/romfs/future.bmfc
Original file line number Diff line number Diff line change
@@ -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
Binary file added data/common/romfs/future.fnt
Binary file not shown.
Binary file added data/common/romfs/future.ttf
Binary file not shown.
Binary file added data/common/romfs/future_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 29 additions & 23 deletions source/skeleton/sfml/BMFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
},
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions source/skeleton/sfml/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
29 changes: 18 additions & 11 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit da6f116

Please sign in to comment.