FontStash.NET 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package FontStash.NET --version 1.0.0                
NuGet\Install-Package FontStash.NET -Version 1.0.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="FontStash.NET" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FontStash.NET --version 1.0.0                
#r "nuget: FontStash.NET, 1.0.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 FontStash.NET as a Cake Addin
#addin nuget:?package=FontStash.NET&version=1.0.0

// Install FontStash.NET as a Cake Tool
#tool nuget:?package=FontStash.NET&version=1.0.0                

FontStash.NET

A port of memononen/fontstash to C# for SilkyNvg.

Usage / Examples

Creating a new context

Information about the desired font atlas and the callbacks used for rendering are stored in the FonsParams struct.

FonsParams prams;
// the atlas's initial size
prams.Width = 512;
prams.Height = 512;
// where the (0|0) on the atlas will be
prams.flags = (byte)FonsFlags.ZeroTopLeft;
// Callbacks for creating, resisizing, updating, drawing and deleting the atlas
prams.RenderCreate = Create;
prams.RenderResize = Resize;
prams.RenderUpdate = Update;
prams.RenderDraw = Draw;
prams.RenderDelete = Delete;

To finally create the Fontstash context and instance, do the following:

var fons = new Fontstash(prams);

Now, one can use the Fontstash instance the same as memononen/fontstash, apart from not having to parse a context with every call, as the context information is stored per api-instance.

// Create a new font
// Set last parameter to 0 always, incase using StbTrueType font indices.
int fornNormal = fons.CreateFont("testFont", "./fonts/verdana.ttf", 0);

// Rendering method here
// Colours are stored as 4 bytes (rgba) next to each other in a uint.
private uint GetColour(byte r, byte g, byte b, byte a) => return (uint)((r) | (g << 8) | (b << 16) | (a << 24));
uint fontColourRed = GetColour(255, 0, 0, 255);

// Render "I am the walrus!"
fons.SetFont(fontNormal);
fons.SetSize(72.0f);
fons.SetColour(fontColourRed);
// FontStash.DrawText(float, float string) returns the end X-Position of the rendered string on the window.
float endX = fons.DrawText(20, 100, "I am the walrus!");

// render the font atlas in it's current state
fons.DrawDebug(800, 200);
OpenGL utillities

When using OpenGL FontStash.NET, the same as fontstash, provids utillity classes to aid rendering. They are FontStash.NET.GL.Legacy for legacy OpenGL and FontStash.NET.GL for modern OpenGL respectively. These use Silk.NET for rendering, so a compatible OpenGL object must be parsed.

GLFons glFons = new GLFons(gl);
Fontstash fs = glFons.Create(512, 512, (int)FonsFlags.ZeroTopleft);

The example seen above has the same effect as the "manual" example.

Examples

Two example projects using OpenGL can be found in 'samples' directory on github.

Credits

Obviously mnemononen/fontstash StbTrueTypeSharp for the StbTrueType implementation Silk.NET for the OpenGL implementation in the helper classes.

License

FontStash.NET uses the MIT-License fontstash uses the ZLib-License

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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.  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 (4)

Showing the top 4 NuGet packages that depend on FontStash.NET:

Package Downloads
SilkyNvg.Core

A port of memononen/nanovg to .NET5.0 primarilly centered around the Silk.NET framework.

SilkyNvg.Text

A port of memononen/nanovg to .NET5.0 primarilly centered around the Silk.NET framework.

FontStash.NET.GL.Legacy

The legacy OpenGL utillity for FontStash.NET

Waddle

A simple data-driven game engine built on .NET.

GitHub repositories

This package is not used by any popular GitHub repositories.