wpf WPF字体质量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13138299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF font quality
提问by Andrew Stephens
I'm developing a WPF app but I've noticed that at certain font sizes the text doesn't render as nicely as the samples you see in Control Panel -> Fonts. I'm using large Segoe UI fonts (FontSize="36"), and the effect is more noticeable on the upright lines, e.g. a letter "U" might be slightly thicker on one side than the other.
).
我正在开发 WPF 应用程序,但我注意到在某些字体大小下,文本的呈现效果不如您在“控制面板”->“字体”中看到的示例那么好。我使用的是大 Segoe UI 字体 ( FontSize="36"),在直立的线条上效果更明显,例如字母“U”的一侧可能比另一侧稍厚。)。
The font quality improves at certain font sizes, e.g. FontSize="48"(which I believe is the equivalent of 36pt), but using a limited number of font sizes isn't always practical.
某些字体大小的字体质量FontSize="48"有所提高,例如(我认为这相当于 36pt),但使用有限数量的字体大小并不总是实用的。
I can improve the font quality by applying the following properties to the TextBlock:-
我可以通过将以下属性应用于 TextBlock 来提高字体质量:-
TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType"
Given the improvement in quality I'm curious to know why WPF doesn't do this for all text, or is it down to performance? I was thinking of creating a global style to apply this to all controls, or will this cause a problem?
鉴于质量的提高,我很想知道为什么 WPF 不对所有文本都执行此操作,还是取决于性能?我正在考虑创建一个全局样式以将其应用于所有控件,否则会导致问题吗?
(I tried uploading a screenshot but SO must store images at a low quality, and you couldn't really make out the font problem).
(我尝试上传屏幕截图,但因此必须以低质量存储图像,并且您无法真正弄清楚字体问题)。
回答by Abe Heidebrecht
Here is the blog postthat the WPF Text team wrote about this feature.
这是WPF 文本团队撰写的有关此功能的博客文章。
Note for the TextFormattingMode:
注意事项TextFormattingMode:
IdealIdeal text metrics are the metrics which have been used to format text since the introduction of WPF. These metrics result in glyphs' shapes maintaining high fidelity with their outlines from the font file. The glyphs' final placement is not taken into account when creating glyph bitmaps or positioning the glyphs relative to each other.
DisplayIn this new formatting mode, WPF uses GDI compatible text metrics. This ensures that every glyph has a width of multiple whole pixels and is positioned on whole pixels. The use of GDI compatible text metrics also means that glyph sizes and line breaking is similar to GDI based frameworks. That said, glyph sizes are not the only input into the line breaking algorithm used by WPF. Even though we use the same metrics as GDI, our line breaking will not be exactly the same.
理想的理想文本指标是自 WPF 引入以来用于格式化文本的指标。这些指标导致字形的形状与字体文件中的轮廓保持高保真度。创建字形位图或相对于彼此定位字形时,不考虑字形的最终位置。
显示在这种新的格式化模式中,WPF 使用 GDI 兼容的文本度量。这确保了每个字形都具有多个完整像素的宽度并位于整个像素上。使用 GDI 兼容的文本度量也意味着字形大小和换行符类似于基于 GDI 的框架。也就是说,字形大小并不是 WPF 使用的换行算法的唯一输入。即使我们使用与 GDI 相同的指标,我们的换行也不会完全相同。
Since these properties are newin .NET 4.0, they kept the original WPF algorithm as default, which is Ideal mode.
由于这些属性是.NET 4.0中的新属性,因此它们保留了原始 WPF 算法作为默认值,即理想模式。
For the TextRenderingMode
为了 TextRenderingMode
AutoThis mode will use ClearType unless system settings have been set to specifically disable ClearType on the machine.
AliasedNo antialiasing will be used to draw text.
GrayscaleGrayscale antialiasing will be used to draw text.
ClearTypeClearType antialising will be used to draw text.
自动此模式将使用 ClearType,除非已将系统设置设置为在机器上专门禁用 ClearType。
Aliased不使用抗锯齿来绘制文本。
灰度灰度抗锯齿将用于绘制文本。
ClearTypeClearType 抗锯齿将用于绘制文本。
Since Autois default, you will generally get ClearTyperendering.
由于Auto是默认设置,您通常会得到ClearType渲染。
Now, because these are attached properties, and they inherit, you can just set them at the root Window. No need to create a bunch of Styles.
现在,因为这些是附加属性,并且它们是继承的,所以您可以将它们设置在根目录中Window。无需创建一堆Styles。
回答by Ming Slogar
I have noticed small performance issues when dealing with large amounts of data (upwards of 10,000 items) when ClearType is turned on. Changing TextFormattingMode to Display has no visible performance impact.
我注意到在打开 ClearType 时处理大量数据(超过 10,000 个项目)时出现小的性能问题。将 TextFormattingMode 更改为 Display 没有明显的性能影响。
This said, in all of my WPF apps I use global styles to improve text rendering, unless the performance impact is large enough to make the UI feel sticky.
这就是说,在我所有的 WPF 应用程序中,我都使用全局样式来改进文本呈现,除非性能影响大到足以让 UI 感觉很粘。

