SpriteFontPlus

repository·master·Indexed 41 days ago

https://github.com/rds1983/spritefontplus

A library for MonoGame and FNA that extends SpriteFont functionality, enabling the runtime creation of fonts from TTF files via TtfFontBaker.Bake and AngelCode BMFont XML files via BMFontLoader.Load.

Tokens
694
Snippets
4
Records
4
Agent score
50%

What's inside SpriteFontPlus

  1. Install SpriteFontPlus via NuGet or Source

    master

    You can integrate SpriteFontPlus into your project using one of two methods depending on your framework:

    1. NuGet (MonoGame only)

    Use the NuGet package manager to install SpriteFontPlus.

    2. Source Code (MonoGame and FNA)

    To use the source directly, follow these steps:

    1. Clone the repository.
    2. Run git submodule update --init --recursive in the root folder.
    3. Add src/SpriteFontPlus.MonoGame.csproj (for MonoGame) or src/SpriteFontPlus.FNA.csproj (for FNA) to your solution.

    Note: If using FNA, ensure your folder structure matches the project expectations.

    git submodule update --init --recursive
  2. Build SpriteFontPlus from source

    master

    To build the project locally:

    1. Clone the repository.
    2. Execute git submodule update --init --recursive to initialize submodules.
    3. Open one of the solutions located in the build folder.
    git submodule update --init --recursive
  3. Create a SpriteFont from a TTF file

    master

    You can generate a SpriteFont at runtime from a TrueType Font (.ttf) file using the TtfFontBaker.Bake method. This method requires the font bytes, a font size, texture dimensions, and an array of CharacterRange values to include.

    Once baked, use CreateSpriteFont(GraphicsDevice) on the result to produce the final SpriteFont object.

    	25,
    	1024,
    	1024,
    	new[]
    	{
    		CharacterRange.BasicLatin,
    		CharacterRange.Latin1Supplement,
    		CharacterRange.LatinExtendedA,
    		CharacterRange.Cyrillic
    	}
    );
    
    SpriteFont font = fontBakeResult.CreateSpriteFont(GraphicsDevice);
  4. Load a SpriteFont from AngelCode BMFont

    master

    To load a font created with AngelCode BMFont, use the BMFontLoader.Load method. This requires the font data (as a string) and a function that maps file names to streams (to allow the loader to find associated texture files).

    Note: Currently, only XML formats with a single texture are supported.

    using (var stream = TitleContainer.OpenStream("Fonts/test.fnt"))
    {
    	using (var reader = new StreamReader(stream))
    	{
    		fontData = reader.ReadToEnd();
    	}
    }
    
    SpriteFont font = BMFontLoader.Load(fontData, name => TitleContainer.OpenStream("Fonts/" + name), GraphicsDevice);