2017-01-04 124 views
1

我使用DrawingContext DrawGlyphRun(GlyphRun)函數在畫布中使用https://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/的解決方案繪製文本。WPF DrawingContext DrawGlyphRun模糊文本

我在FormattedText上使用它,因爲它速度更快,它也用於計算文本寬度。

這工作得很好,除了兩個問題:

  1. 文字模糊(見下面的圖片)。頂部的文本使用GlyphRun顯示。底部文本使用FormattedText顯示,質量更好。

Blur Image

  • 無法顯示日文或中國字。
  • 問題與字符似乎是GlyphTypeface.CharacterToGlyphMap找不到jp或cn字符,所以我不知道如何處理這些字符。

    回答

    1

    我剛發現你的問題後,我做了一些研究。

    GlyphRun使用公共構造函數來創建創建了TextFormattingMode = Ideal

    所有WPF對象contols其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 
         ) 
    

    ,但你需要得到advanceWidthsTextFormattingMode = Ideal問題。爲此,您需要通過反射訪問GlyphTypeface課程提供的內部方法。

    GlyphTypeface.AdvanceWidths屬性,返回字典,這些寬度,當你訪問字典的指數與textFormattingMode = TextFormattingMode.Ideal

    您可以下載的.Net源代碼,並檢查自己內部調用到

    internal double GetAdvanceWidth(ushort glyph, TextFormattingMode textFormattingMode, bool isSideways) 
    

    至於你的第二個問題,我認爲你使用字符而不是unicode代碼點來獲得標誌符號索引。