我在做了一些研究后才发现你的问题。
使用公共构造函数创建的 GlyphRun 使用 TextFormattingMode = Ideal 创建对象
所有用于渲染的 WPF 控件都使用接受 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
)
但是你需要用TextFormattingMode = Ideal 来获得advanceWidths 的问题。为此,您需要通过反射访问GlyphTypeface 类提供的内部方法。
GlyphTypeface.AdvanceWidths 属性返回具有这些宽度的字典,内部调用
internal double GetAdvanceWidth(ushort glyph, TextFormattingMode textFormattingMode, bool isSideways)
当您使用textFormattingMode = TextFormattingMode.Ideal按索引访问字典时
您可以下载.Net源代码并自行查看。
至于您的第二个问题,我认为您使用 chars 而不是 unicode 代码点来获取字形索引。