我正在做一些自定义控件,我有一些文字通过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;
}
我认为它存在某种本地化问题。任何帮助,将不胜感激。
答案 0 :(得分:4)
尝试替换
var glyphIndex = (ushort)(text[n] - 29);
与
var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
答案 1 :(得分:0)
如何使用Glyphs class
呢?学位符号将在使用时显示:
<Glyphs FontUri="C:\WINDOWS\Fonts\SegoeUI.TTF" FontRenderingEmSize="80"
StyleSimulations="BoldSimulation" UnicodeString="It's 30°C"
Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center" />