FontStashSharp

I would to introduce FontStashSharp, which is well-known(mostly within MG discord) text rendering library.
Its main features:

  • Loading ttf fonts at the run-time
  • Glyphs are rendered on-demand on the texture atlas. Hence the library doesnt require to explicitly specify required character ranges during the font creation.
6 Likes

FontStashSharp got simple rich text support: Rich Text · FontStashSharp/FontStashSharp Wiki · GitHub

1 Like

That’s awesome, thanks for your hard work!

1 Like

Thank you! this is really good, I’ve been testing it in the past few days and it works pretty well even under heavy load, I will replace all my font logic with this project now, this is what I was looking for quite many years!.

1 Like

rds1983,
Do you know how to get the size of a text that gives the baseline of the text?
For example if I use “Hello” measure string will give me an Y value, but if I try to print “jello” because j goes below the baseline I get a different Y value than Hello, though I want to print both at the same Y value.

I do not want to make different logic for letters that go below the baseline of the text, any idea how can I achieve a correct baseline with FontStash?

SpriteFontBase.LineHeight?

1 Like

Thanks!, looks like that one did the trick. Thought the MeasureString always returns the exact font dimension, I think it may be more useful if there is a flag to return the height based on baseline. That was the reason for my fonts to not align properly everywhere and I was trying to understand why it failed with some words until I notice the baseline. The same thing happened with the default SpriteFont.

Hello everyone,

This thread is a bit older, but my question fits in well. I am not sure if I have implemented FontStashSharp correctly. I can create a FontSystem instance:

private FontSystem _fontSystem;
_fontSystem = new();
_fontSystem.AddFont(File.ReadAllBytes("Content/fonts/OpenSans-Regular.ttf"));

This works well. But if I want to display a text via a method,

SpriteFontBase font = _fontSystem.GetFont(24);
_game.spriteBatch.DrawString(font, _text, position, _color);

I get the following error message underlying on “font”:

cannot convert from ‘FontStashSharp.SpriteFontBase’ to ‘Microsoft.Xna.Framework.Graphics.SpriteFont’

I have followed the instructions (link), but the FNA folder is missing in my folder structure.

Does anyone have any advice?

Thank you very much

You need to put a using at the top of the C# file:

using FontStashSharp;

I removed that in mine and then got your error above.

Thanks for your reply,

I reinstalled everything and now it works :slight_smile: