Unofficial.SixLabors.Fonts.MonoGame 1.1.0

Suggested Alternatives

FontStashSharp

Additional Details

SixLabors updated policy - now if you have revenues over $1M you have to pay license fees

dotnet add package Unofficial.SixLabors.Fonts.MonoGame --version 1.1.0                
NuGet\Install-Package Unofficial.SixLabors.Fonts.MonoGame -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Unofficial.SixLabors.Fonts.MonoGame" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Unofficial.SixLabors.Fonts.MonoGame --version 1.1.0                
#r "nuget: Unofficial.SixLabors.Fonts.MonoGame, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Unofficial.SixLabors.Fonts.MonoGame as a Cake Addin
#addin nuget:?package=Unofficial.SixLabors.Fonts.MonoGame&version=1.1.0

// Install Unofficial.SixLabors.Fonts.MonoGame as a Cake Tool
#tool nuget:?package=Unofficial.SixLabors.Fonts.MonoGame&version=1.1.0                

SixLabors.Fonts.MonoGame

Unofficial port of SixLabors.Fonts to MonoGame for .NET 6 and above.

Example

Please note

This has only been tested on Windows. It should work on other systems, but if it doesn't, please open an issue. This library is also new and unoptimised. Pull requests are welcome!

Usage

Basic usage

The premise is simple: we have a FontEngine, which we use to store our fonts. We then use the FontEngine to get a loaded font, and we draw with it.

// Load up our very epic fonts!!!
FontEngine engine = new();
engine.LoadFont("path/to/some-cool-font.ttf");

// The font name from the truetype file is what we need to use here.
MonoGameFont font = engine.GetFont("some-cool-font", 24f);

// We've got the font! Now we can draw.
// We have an extension method on SpriteBatch just for that.
// It's in SixLabors.Fonts.MonoGame.Utils
batch.DrawText(font, "Hello, world!", new Vector2(10, 10), Color.White);

Default font name

Presumably, you have one font and you don't want to keep typing its name over and over again. Or, maybe your game has a mod system, and mods can load in their own font and use it as the default for the entire game. In cases as such, you can modify the default font name.

// We are using the code from the above example.

// We're setting the DefaultFontName to the font we'll be using
engine.DefaultFontName = "some-cool-font";

// Now we can forget about the name at all!
MonoGameFont defaultFont = engine.GetDefaultFont(24f);

Fallback fonts

For missing glyphs, SixLabors.Fonts provides fallback fonts. This library implements them as well. You can add a fallback font by adding it to the FallbackFontFamilies list in FontEngine.

// We are using the code from the above examples.

fontEngine.FallbackFontFamilies.Add(font);

If addSystemFonts in the FontEngine constructor is set to true (which is the default), the fonts Arial, Courier and Helvetica will be added to the fallback fonts list.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 1,322 7/4/2022 1.1.0 is deprecated because it is no longer maintained.
1.0.0 366 7/4/2022

Added color parameter to SpriteBatch.DrawText