Followers 0

# BMFont and how to interpret the .fnt file

## 4 posts in this topic

I figured this is the AngelCode forum, so it probably covers the tools as well as AngelScript. I have written a loader and renderer for the fonts produced by BMFont. All is working well in that the fonts are displaying on the screen. What I cannot work out is how to use the xoffset and yoffset fields from the .fnt file. If I add or subtract the yoffset from the y coordinate used to display the character, the characters are not displayed in line (they seem to be at random heights). What is the meaning of the offset values and how do I use them? I am currently looking at 'guppy' with the g, p and y characters sitting high so their descenders are in line with the bottom of the u. Is there some sample code or explanation somewhere that I could look at? There is also a bug in BMFont in how the xadvance field is written to the .fnt file in regards to the antialias level. The xadvance field is incorrectly multiplied by the antialias level. If I have the antialias set to 2, then I have to divide the xadvance value by 2 to get the text spaced correctly. Likewise for 3 and 4 (have to divide by 3 and 4 to get the correct value). Otherwise I'm getting some nice results from BMFont. Thanks for releasing it.
0

##### Share on other sites
Yes, this is the right place for your question.

I've never gotten around to writing any tutorial or sample for using the font file. I should probably do that as it would make the tool more valuable.

Here's a short description of each attribute in the .fnt file.

lineHeight: is how much to move the cursor when going to the next line.

base: this is the offset from the top of line, to where the base of each character is.

scaleW and scaleH: This is the size of the texture.

pages: gives how many textures that are used for the font.

id: is the character number in the ASCII table.

x, y, width, and height: give the position and size of the character image in the texture.

xoffset and yoffset: hold the offset with which to offset the cursor position when drawing the character image. Note, these shouldn't actually change the cursor position.

xadvance: is how much the cursor position should be moved after each character.

page: gives the texture where the character image is found.

Here's some pseudo code for rendering a character:

  // Compute the source rect  Rect src;  src.left   = x;  src.top    = y;  src.right  = x + width;  src.bottom = y + height;  // Compute the destination rect  Rect dst;  dst.left   = cursor.x + xoffset;  dst.top    = cursor.y + yoffset;  dst.right  = dst.left + width;  dst.bottom = dst.top + height;  // Draw the image from the right texture  DrawRect(page, src, dst);  // Update the position  cursos.x += xadvance;

I hope that explains it.

I'll look into the bug. Thanks for letting me know.

Regards,
Andreas

 fixed the pseudo code as Sly pointed out [/edit]

[Edited by - WitchLord on November 25, 2004 8:00:15 AM]
1

##### Share on other sites
Thanks. I'll give that a try when I get home tonight, or here at work if I manage to get FreePascal to compile my Delphi game project.
0

##### Share on other sites
For the easily confused, Andreas' example should have read

  // Compute the destination rect  Rect dst;  dst.left   = cursor.x + xoffset;  dst.top    = cursor.y + yoffset;  dst.right  = dst.left + width;  dst.bottom = dst.top + height;

Thanks for that, Andreas. It all works sweet now. :)

[Edited by - Sly on November 25, 2004 3:52:29 AM]
0

##### Share on other sites
I uploaded a new version of BMFont, fixing the bug you noticed. Thanks for letting me know.
0

## Create an account

Register a new account