#242 – Drawing Text Using DrawGlyphRun
March 11, 2011 1 Comment
If you want an even lower-level mechanism for drawing text than provided by DrawingContext.DrawText, you can use DrawingContext.DrawGlyphRun.
DrawGlyphRun lets you draw text based on glyphs, which are individual characters within a font that typically represent a single character in a language.
Below is an example of rendering the word “Wow” using DrawGlyphRun, in a custom DrawingVisual.
class MyDrawingVisual : DrawingVisual { public MyDrawingVisual() { using (DrawingContext dc = RenderOpen()) { GlyphRun gr = new GlyphRun( new GlyphTypeface(new Uri(@"C:\Windows\Fonts\BKANT.TTF")), 0, // Bi-directional nesting level false, // isSideways 96, // pt size new ushort[] { 58, 82, 90 }, // glyphIndices new Point(100.0, 100.0), // baselineOrigin new double[] { 80.0, 45.0, 0.0 }, // advanceWidths null, // glyphOffsets null, // characters null, // deviceFontName null, // clusterMap null, // caretStops null); // xmlLanguage // Draw the text at a location dc.DrawGlyphRun(Brushes.Black, gr); } } }
The glyphIndices parameter specifies a list of indexes into the font for the glyphs that are to be rendered.