love
love copied to clipboard
Alternative scalable font type?
Original report by jarodwright (Bitbucket: jarodwright, ).
Because fonts are rasterized, when applying a transform to a font it doesn't scale well like most other draw functions.
For performance reasons rasterizing should be kept, however I propose that an alternative dynamic font which renders characters each frame would be desirable for scaling text.
An argument against this would be that you shouldn't ever be drawing text to the world and should instead be drawing it to the UI at a screen position based on the world.
Original comment by Sasha Szpakowski (Bitbucket: slime73, GitHub: slime73).
LÖVE can load BMFont files, and there are tools to produce BMFont glyph atlases which use signed distance fields for the glyphs. You can then write a shader which renders those. https://github.com/libgdx/libgdx/wiki/Distance-field-fonts
It might be worth looking into making a shader included by default in LÖVE for that, although I'm not sure how useful that would be since typically you'd want to tailor things a bit to how your game is going to use the distance field font. Some documentation on the wiki about this would be good, in any case.
Definite +1 here, I did search for SDF so missed this issue. Adding SDF to the comments so that it will be found for anyone else searching for that.
The Unity engine does a good job at implementing SDF text rendering with its TextMesh Pro component. It might be a good reference implementation. It would be good if the render function allowed the font size to be specified each time love.graphics.printSdf("Hello", 32, 0, 0)
or alternatively if there was a font size state love.graphics.setFontSize(32)
.
All that you need is a shader that works with distance field font glyphs and a love.graphics.setShader
call before printing text, everything else is already supported by love (for example love.graphics.print
accepts scale parameters already, and love.graphics.newFont
accepts BMFonts which can have SDF glyphs).
Upgrading FreeType to 2.11.0 or later would allow Löve to use its SDF render mode to output SDF glyphs from any font. I already have a working proof of concept (adding "sdf" HintingMode paired with a shader)
(excuse the experimental subpixel font test)
I'll probably clean it up and open a PR soon.
For now I've figured out you can use a simple shader that will use the antialiasing as a small section of a distance field, although it only works for upscaling:
uniform float scl; // the scale factor you use to draw the text
vec4 effect(vec4 color, Image texture, vec2 tc, vec2 sc) {
vec4 c = gammaToLinear(color);
float d = clamp(Texel(texture, tc).a * scl - 0.5 * scl + 0.5, 0.0, 1.0);
return linearToGamma(vec4(c.rgb, c.a * d));
}
@Labrium it would be cool if you did open a PR for this :D I am still wanting this
#1966 :)