Support user-defined font-family for TextSelection
RyanMullins opened this issue · 2 comments
It would be nice if Megaplot offered a couple of different font families for TextSelections, similar to CSS font-family
, but maybe restricted to a known set since they need to be converted into a texture that gets sampled?
This is a reasonable request. Doing it at all is easy. Doing it well would be hard.
Glyphs in Megaplot are implemented by sampling into a Signed Distance Field (SDF) texture which is made and managed by the GlyphMapper
class. One of the construction parameters of the GlyphMapper
is fontFamily
, which currently defaults to 'monospace'
. It would be relatively easy to expose fontFamily
as an optional construction parameter on the Scene
class and pass it through.
However, this introduces the problem of kerning. Currently, issues of kerning are headed off by only supporting a monospace font. Allowing the user to specify an arbitrary typeface introduces the problem of differential character-pair spacing. User-specified fonts might also make glyphs that are wider or narrower than currently expected, or extend beyond the top or bottom of the planned bounding box.
Lastly, there's the issue of loading the font file(s). Rendering to the SDF texture should be delayed until the specified font family has finished loading. Loading may be delayed relative to other page assets. The font family may not load at all (for example if the URL is broken). What to do in these cases is not immediately clear.
Some of these problems are likely to be encountered anyway, even without custom fonts. For example, some languages' characters are especially wide or tall. Eventually the data Megaplot is asked to render will probably include Unicode characters which may render incorrectly or not at all, even using a fixed monospace font.
Yup, totally get the can of worms this could potentially open.
What about a phased approach -- baking in a limited set of fixed width fonts (even just 2), then expanding to user-defined fixed-width fonts, then finally to any user-defined font?
At the moment, all I want is something that looks nicer than Chrome's custom monospace
font family. Noto Sans Mono would be great and its license seems to be compatible with Megaplot's use case.
The other two steps in the progression could come (much) later... as in v2 or v3 "later".