|Project:||The Open Toolkit library|
I have just committed a WIP implementation of a new text renderer to the text branch. Improvements:
- Simpler to use.
- More modular. It is now possible to have different GL1/GL2/GL3-level output or text layout providers. It is also possible to specify your own implementations (for example if you wish to integrate this into your engine).
- More accurate rendering (better grid fitting).
- Improved speed and reduced GC pressure through caching.
- Lower texture memory consumption.
The new implementation is based on GdiPMeasureCharacterRanges, which provides better control over text layout, when compared to MeasureString (the old implementation). Text is now cached on two levels, rasterized glyphs (loaded in texture memory) and laid out text. The public API looks like this:
// No TextureFonts, TextHandles or anything. TextPrinter text = new TextPrinter(); [...] text.Begin(); text.Print("Hello, World!", font); text.Print(fps.ToString(), font, TextPrinterOptions.NoCache); text.End(); [...] TextExtents extents = text.Measure("ABC", font); RectangleF bbox = extents.BoundingBox; RectangleF char_pos = extents; // Returns the position of "B" - useful for e.g. drawing a caret or selecting text.
The implementation in SVN is still under heavy development. Caveats:
- (Fixed) Single texture sheet limitation remains. This is proving more difficult than expected.
- (Fixed) Sometimes binds the wrong texture for glyphs (funky results!)
- Trying to draw / measure uncached strings with newlines will cause GC pressure. This is the unfortunate result of a bug in Mono's GdiPMeasureCharacterRanges implementation. Fortunately, this is unlikely to cause problems in real life (you don't usually create dynamic text with newlines in a tight loop).
Anyone still interested despite these problems, you can test the code from the text branch. Expect screenshots shortly.
Edit: Clarified GC-pressure caveat.