2014-04-25 38 views
3

我正在做一些自定義的控制和我有一些文本通過雕文被抽運行WPF字形運行正確渲染度的符號

我的問題是℃,沒有被正確我的字形渲染(無論字體家庭),看起來像這樣:

Problem symbol

所以我對字形運行代碼(簡體)是:

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

這種問題通常是由於因爲一個特定的字體系列沒有特定的字符......你能確認你使用過的字體*是否有這個字符?如果不是,那麼您應該可以爲該字符使用不同的字體系列。 – Sheridan

+0

它的確如我使用Segoe UI一樣。如果我在Segoe UI中使用文本塊,則會顯示符號。它只有當我使用字形運行中的字體時,符號不能正確顯示。 –

回答

4

嘗試更換

var glyphIndex = (ushort)(text[n] - 29); 

var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]]; 
+0

完美,非常感謝。 –

+0

+1好抓!原始代碼很可能想要消除地圖查找,並提出了「減29」公式,但該方法在當前字體系列中失敗。 – Sabuncu

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" /> 

enter image description here

+0

我正在使用字形運行,因爲我通過DrawingContext(而不是XAML)渲染,它只接受GlyphRun。 –