Home

Awesome

C# AngelCode bitmap font parsing

Build status CodeQL NuGet Donate

The font parser library was used by this OpenGL application
that renders text

While writing bitmap font processing code for an OpenGL project, I settled on using AngelCode's BMFont utility to generate both the textures and the font definition.

This library is a generic parser for the BMFont format - it doesn't include any rendering functionality or exotic references and should be usable in any version of .NET from 2.0 upwards. BMFont can generate fonts in three formats - binary, text and XML. This library supports all three formats, although it can only read version 3 binary fonts.

Note: This library only provides parsing functionality for loading font meta data. It is up to you to provide functionality used to load textures and render characters.

Getting the library

The easiest way of obtaining the library is via NuGet.

Install-Package Cyotek.Drawing.BitmapFont

If you don't use NuGet, pre-compiled binaries can be obtained from the GitHub Releases page.

Of course, you can always grab the source and build it yourself!

Overview of the library

There are four main classes used to describe a font:

There is also a support class, Padding, as I didn't want to reference System.Windows.Forms in order to use its own and using a Rectangle instead would be confusing.

What format should I use

All 3 formats contain the same information so ultimately there is no real different in the 3. However, the binary format is is far more efficient in both storage space and load speed than the other formats.

The following benchmarks were generated via BenchmarkDotNet, using a font with 424 characters. Loading binary fonts is the clear winner, both in terms of speed and allocations, followed by text and finally XML.

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18363.1082 (1909/November2018Update/19H2)
AMD FX(tm)-6300, 1 CPU, 6 logical and 3 physical cores
.NET Core SDK=3.1.402
  [Host]     : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT
  DefaultJob : .NET Core 3.1.8 (CoreCLR 4.700.20.41105, CoreFX 4.700.20.41903), X64 RyuJIT
MethodMeanErrorStdDevGen 0Gen 1Gen 2Allocated
LoadBinary135.8 us1.41 us1.32 us10.0098--41.33 KB
LoadAutoBinary227.1 us3.60 us4.29 us10.98630.2441-45.55 KB
LoadText2,734.2 us6.06 us5.37 us164.06253.9063-674.75 KB
LoadAutoText2,778.5 us14.96 us13.26 us164.0625--678.98 KB
LoadXml3,216.2 us18.86 us14.73 us144.531370.3125-780.34 KB
LoadAutoXml3,298.5 us26.18 us24.49 us152.343874.2188-784.53 KB

Loading a font

To load a font, call BitmapFontLoader.LoadFontFromFile. This will attempt to auto detect the file type and load a font. Alternatively, if you already know the file type in advance, then call the variations BitmapFontLoader.LoadFontFromBinaryFile, BitmapFontLoader.LoadFontFromTextFile or BitmapFontLoader.LoadFontFromXmlFile.

Each of these functions returns a new BitmapFont object on success.

Alternatively, you can create a BitmapFont instance yourself and call one one of the following methods:

Note: Load methods on the BitmapFont and BitmapFontLoader classes that take a string filename will fully qualify the FileName property of loaded Page instances. Page instances for all other load methods will have a relative filename and it is up to the calling application to fully qualify the path as appropriate when loading textures.

Using a font

The BitmapFont class returns all the information specified in the font file, such as the attributes used to create the font. Most of these not directly used and are there only for if your application needs to know how the font was generated (for example if the textures are packed or not). The main things you would be interested in are:

The Character class describes a single character. Your rendering functionality will probably need to use all of the properties it contains:

Example rendering using GDI

The sample project shows a very way of rending using GDI; however this is just for demonstration purposes and you should probably come up with something more efficient in a real application!

Example rendering using the Text Maker sample

    private void DrawCharacter(Graphics g, Character character, int x, int y)
    {
      g.DrawImage(_textures[character.TexturePage],
                  new RectangleF(x, y, character.Width, character.Height),
                  new Rectangle(character.X, character.Y, character.Width, character.Height),
                  GraphicsUnit.Pixel);
    }

    private void DrawPreview()
    {
      if (_font != null)
      {
        string text;
        Size size;

        text = previewTextBox.Text;
        size = _font.MeasureFont(text);

        if (size.Height != 0 && size.Width != 0)
        {
          Bitmap image;
          int x;
          int y;
          char previousCharacter;

          image = new Bitmap(size.Width, size.Height);
          x = 0;
          y = 0;
          previousCharacter = ' ';

          using (Graphics g = Graphics.FromImage(image))
          {
            foreach (char character in text)
            {
              switch (character)
              {
                case '\n':
                  x = 0;
                  y += _font.LineHeight;
                  break;

                case '\r':
                  break;

                default:
                  Character data;

                  data = _font[character];

                  if (!data.IsEmpty)
                  {
                    int kerning;
                    kerning = _font.GetKerning(previousCharacter, character);

                    this.DrawCharacter(g, data, x + data.XOffset + kerning, y + data.YOffset);

                    x += data.XAdvance + kerning;
                  }
                  break;
              }

              previousCharacter = character;
            }
          }

          previewImageBox.Image = image;
          previewImageBox.ActualSize();
        }
      }
    }

The Bitmap Font Viewer application

This sample application loads and previews bitmap fonts

Included in this repository is a sample WinForms application for viewing BMFont font definitions.

Note: All of the fonts I have created and tested were unpacked. The font viewer does not support packed textures, and while it will still load the font, it will not draw glyphs properly as it isn't able to do any of the magic with channels that the packed texture requires. In addition, as .NET doesn't support the TGA format by default, neither does this sample project.

Requirements

.NET Framework 2.0 or later.

Pre-built binaries are available via a signed NuGet package containing the following targets.

Is there a target not on this list you'd like to see? Raise an issue, or even better, a pull request.

Acknowledgements

See CONTRIBUTORS.md for details of contributions to this library.

License

This source is licensed under the MIT license. See LICENSE.txt for the full text.