Reputation: 367
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:
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.
Upvotes: 1
Views: 1062
Reputation: 11
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:
internal static GlyphRun TryCreate(
GlyphTypeface glyphTypeface,
int bidiLevel,
bool isSideways,
double renderingEmSize,
IList<ushort> glyphIndices,
Point baselineOrigin,
IList<double> advanceWidths,
IList<Point> glyphOffsets,
IList<char> characters,
string deviceFontName,
IList<ushort> clusterMap,
IList<bool> caretStops,
XmlLanguage language,
TextFormattingMode textLayout
)
but the problem that you need to get advanceWidths
with TextFormattingMode = Ideal
. For this you need access via reflection to internal methods provided by GlyphTypeface
class.
GlyphTypeface.AdvanceWidths
property that returns dictionary with these widths internally calls to
internal double GetAdvanceWidth(ushort glyph, TextFormattingMode textFormattingMode, bool isSideways)
when 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.
Upvotes: 1