A modernised version of FontStash.NET, that conforms to standard .NET code style practices, provides API cleanups, small optimisation and refactors.
Examples can be found in the 'samples' directory.
Information about the desired font atlas and the callbacks used for rendering
are stored in the FontParams
struct.
var fontParams = new FontParams()
{
// The initial dimensions of the font atlas
Width = 512,
Height = 512,
// Where point (0, 0) is located on the atlas
Flags = FontFlags.ZeroTopLeft,
// Callbacks for creating, resizing, updating, drawing and deleting the atlas
RenderCreate = OnCreate,
RenderUpdate = OnUpdate,
RenderDraw = OnDraw,
RenderDelete = OnDelete
};
Examples for implementations of these methods can be found in either the
src/FontStash.NET.GL.Legacy
(for people not used to OpenGL) or
src/FontStash.NET.GL
(for how to render in modern OpenGL) projects.
To finally create the FontStash context and instance, do the following:
var fontManager = new FontManager(fontParams);
Now, the FontManager can be used with a similar API to that of memononen/fontstash, however, a context does not need to be passed with each call, as the context information is stored within each FontManager instance.
// Create a new font
// The last parameter should usually be set to zero when using StbTrueType font indices.
int testFontId = fontManager.CreateFont("testFont", "./fonts/verdana.ttf", 0);
// Render "I am the walrus!"
fontManager.SetFont(testFontId);
fontManager.SetSize(72.0f);
fontManager.SetColour(255, 0, 0, 255);
// FontStash.DrawText(float, float string) returns the end X-Position of the rendered string on the window.
float endX = fontManager.DrawText(20, 100, "I am the walrus!");
// Render the font atlas in it's current state
fontManager.DrawDebug(800, 200);
When using OpenGL FontStash.NET, the same as fontstash, provids utillity classes
to aid rendering. They are located in either src/FontStash.NET.GL.Legacy
for legacy OpenGL
and src/FontStash.NET.GL
for modern OpenGL respectively. These use Silk.NET
for rendering, so a compatible OpenGL object must be parsed.
// Pass in the GL context, usually created with Silk.Net's 'window.CreateOpenGL()' method
var glFont = new GLFont(gl);
FontManager fontManager = glFont.Create(512, 512, FontFlags.ZeroTopLeft);
The example seen above has the same effect as the "manual" example.
Two example projects using OpenGL can be found in 'samples' directory.
Without debug atlas displayed:
With debug atlas displayed:
- FontStash.NET this fork's base implementation.
- mnemononen/fontstash the original project that FontStash.NET is a port of.
- StbTrueTypeSharp for the StbTrueType implementation.
- Silk.NET for the OpenGL implementation in the helper classes.
FontStash.NET uses the MIT-License
fontstash uses the ZLib-License