【问题标题】:WPF DrawingContext DrawGlyphRun blur textWPF DrawingContext DrawGlyphRun 模糊文本
【发布时间】:2017-05-18 22:16:32
【问题描述】:

我正在使用来自 https://smellegantcode.wordpress.com/2008/07/03/glyphrun-and-so-forth/ 的解决方案使用 DrawingContext DrawGlyphRun(GlyphRun) 函数在画布中绘制文本。

我在 FormattedText 上使用它,因为它更快,并且还用于计算文本宽度。

这很好用,除了两个问题:

  1. 文字模糊(见下图)。顶部的文本使用 GlyphRun 显示。底部文本使用质量更好的 FormattedText 显示。

  1. 无法显示日文或中文字符。

字符的问题似乎是 GlyphTypeface.CharacterToGlyphMap 找不到 jp 或 cn 字符,所以我不确定如何处理这些字符。

【问题讨论】:

    标签: wpf drawingcontext


    【解决方案1】:

    我在做了一些研究后才发现你的问题。

    使用公共构造函数创建的 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 代码点来获取字形索引。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2014-05-21
      • 2011-05-09
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      相关资源
      最近更新 更多