我使用DrawingContext DrawGlyphRun(GlyphRun)函数在画布中使用https://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/的解决方案绘制文本。
我在FormattedText上使用它,因为它更快,也用于计算文本宽度。
除了两个问题外,这个方法效果很好:

字符的问题似乎是GlyphTypeface.CharacterToGlyphMap找不到jp或cn字符,所以我不知道如何准确地处理这些字符。
发布于 2017-03-31 07:38:42
我在做了些调查之后才发现你的问题。
使用公共构造函数创建的GlyphRun使用TextFormattingMode = Ideal创建对象
所有的WPF控件对于其rendring都使用接受TextFormattingMode作为参数的方法/构造函数。
您可以通过反射调用GlyphRun.TryCreate()静态方法:
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
)但是,您需要用advanceWidths获得TextFormattingMode = Ideal的问题。为此,您需要通过反射访问GlyphTypeface类提供的内部方法。
返回具有以下宽度的字典的GlyphTypeface.AdvanceWidths属性
internal double GetAdvanceWidth(ushort glyph, TextFormattingMode textFormattingMode, bool isSideways)使用textFormattingMode = TextFormattingMode.Ideal按索引访问字典时
您可以下载.Net源代码并亲自检查它。
至于您的第二个问题,我认为您使用字符而不是unicode代码点来获取字形索引。
https://stackoverflow.com/questions/41466938
复制相似问题