我正在做一些自定义控件,我有一些文本是通过 Glyph Runs 绘制的
我的问题是 °C 没有被我的字形正确渲染(无论字体系列如何),看起来像这样:
所以我的字形运行代码(简化)是:
var typeface = new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch);
...
GlyphRun glyphRun = CreateGlyphRun(typeface, "°C", 10, new Point(0,0));
dc.DrawGlyphRun(brush, glyphRun);
在哪里
internal static GlyphRun CreateGlyphRun(Typeface typeface, string text, double size, Point origin)
{
if (text.Length == 0)
return null;
GlyphTypeface glyphTypeface;
typeface.TryGetGlyphTypeface(out glyphTypeface)
var glyphIndexes = new ushort[text.Length];
var advanceWidths = new double[text.Length];
for (int n = 0; n < text.Length; n++)
{
var glyphIndex = (ushort)(text[n] - 29);
glyphIndexes[n] = glyphIndex;
advanceWidths[n] = glyphTypeface.AdvanceWidths[glyphIndex] * size;
}
var glyphRun = new GlyphRun(glyphTypeface, 0, false, size, glyphIndexes, origin, advanceWidths, null, null,
null,
null, null, null);
return glyphRun;
}
我假设它是某种本地化问题。任何帮助,将不胜感激。
最佳答案
尝试替换
var glyphIndex = (ushort)(text[n] - 29);
与
var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
关于c# - WPF Glyph Run 错误地渲染度数符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23296997/