I'm using the DrawingContext DrawGlyphRun(GlyphRun) function to draw text in the Canvas using the solution from https://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/.
I'm using this over the FormattedText because it's faster and it's also used for calculating text width.
This works well except for 2 problems:
- Text is blurred (see below the image). Text at the top is displayed using GlyphRun. The bottom text is displayed using FormattedText which has better quality.
- Cannot display Japanese or Chinese characters.
Problem with characters seems to be that GlyphTypeface.CharacterToGlyphMap cannot find the jp or cn character, so I'm not sure how exactly to deal with these characters.
I just found your question after some research that I made.
GlyphRun created using public constructors creates object with
TextFormattingMode = Ideal
All WPF contols for their rendring use methods/constructors that accepts TextFormattingMode as parameter.
You can call
GlyphRun.TryCreate()
static method via reflection:but the problem that you need to get
advanceWidths
withTextFormattingMode = Ideal
. For this you need access via reflection to internal methods provided byGlyphTypeface
class.GlyphTypeface.AdvanceWidths
property that returns dictionary with these widths internally calls towhen you access dictionary by index with
textFormattingMode = TextFormattingMode.Ideal
You can download .Net source code and check it yourself.
As for your second question I think that you use chars instead of unicode code points to get glyph index.