WPF 字体:为什么缺少一些字符?
我正在编写一个 WPF 应用程序,而我使用的字体仅在 WPF 中运行时出现问题 - 在其他任何东西(记事本、写字板等)中使用它都可以正常工作。 WPF 的问题是它有时会回退到另一种字体。 我所说的“有时”是指只有字符 [a-zA-Z] 看起来可以正确渲染 - 其他所有字符似乎都渲染为默认的 TextBox 字体。
有谁知道 WPF 对其支持的字体是否有某种限制? 这似乎是 WPF 中的错误 - 该字体在其他地方都可以正常工作。
我尝试使用的字体是“Scramble”TTF 字体(http://knownfonts. smackbomb.com/fonts/scrabble.php)。
数字和空格应被视为空白拼字游戏/打乱图块,但数字本身出现在我正在使用的文本框中。
我正在使用的代码:
<TextBox Text="Testing testing testing" FontFamily="Fonts/#Scramble" />
还有其他人经历过类似的事情吗?
任何建议都会摇滚!
谢谢!
I'm writing a WPF app and the font that I am using only has problems running in WPF - it works fine using it in anything else (notepad, wordpad, etc.). The problem with WPF is that it falls back to another font sometimes. By "sometimes" I mean that only characters [a-zA-Z] appear to render correctly - everything else appears to be rendered as the default TextBox font.
Does anyone know if WPF has some sort of limitation for the fonts that it supports? It almost seems to be bug in WPF - the font works fine everywhere else.
The font that I'm trying to use is the "Scramble" TTF font (http://famousfonts.smackbomb.com/fonts/scrabble.php).
Numbers and spaces should be seen as a blank Scrabble/Scramble tile, but instead the number itself appears in the textbox I'm using.
The code I'm using:
<TextBox Text="Testing testing testing" FontFamily="Fonts/#Scramble" />
Has anyone else experienced something similar?
Any suggestions would rock!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 MSDN:
我的猜测是该字体不支持 Unicode - 该字体本身是在 1996 年创建的,并且由于它旨在模拟 Scrabble 作品,因此我不确定字体作者是否考虑过本地化。
编辑
根据字体文档,该字体支持字母,任何数字都应该呈现空白图块。 空间不渲染图块。
From MSDN:
My guess would be that the font doesn't support Unicode - the font itself was created in 1996, and since it's intended to emulate Scrabble pieces, I'm not sure that the font author even considered localization.
EDIT
According to the font documentation, the font supports the letters, and any number is supposed to render a blank tile. Spaces don't render a tile.
简短的回答是,Scramble 不会在 WPF 或其他任何东西中使用空白拼字游戏块来表示全角空格。 您需要另一种字体或其他字体来编辑 em/en 空格字形(例如 Font Creator)。
我得到 另一种字体(x-grid),我知道它有一个特殊的空格字形,并在下面的代码中使用它们。
生成的窗口如下所示。
恐怕与字体后备无关。 抱歉,但我希望这会有所帮助。
**更新:**抱歉,刚刚注意到您也提到了数字。 Paint.Net 中的快速测试显示数字也不会出现在图块中。
The short answer is that Scramble doesn't use a blank scrabble piece for an em space in WPF or anything else. You'll need another font or something to edit the em/en space glyphs (something like Font Creator).
I got another font (x-grid) which i KNOW has a special glyph for spaces and used them as in the code below.
The resulting window looked like this.
Nothing to do with font-fallback I'm afraid. Sorry, but I hope that helps.
**UPDATE:**Sorry, just noticed you mentioned numbers too. A quick test in Paint.Net shows that numbers don't appear in tiles either.
不要设置 FontFaily="Fonts/#Scrambe",只需使用不带“Fonts/#”的字体名称:
这对我有用。 我下载并安装了您提到的字体,无论是在 VS2008 的设计视图中还是在运行应用程序时,它都完美地显示了该字体。
Instead of setting FontFaily="Fonts/#Scrambe", just use the font name without "Fonts/#":
This worked for me. I downloaded and installed the font you referred to, and it shows the font perfectly, both in design view in VS2008, and in runtime when I run the app.
一般的版式,特别是字体是一个相当复杂的话题,我并不真正感兴趣。 这就是我从 Windows Presentation Foundation 中的版式中得出的结果和相关的 MSDN 文档:
版式
对象公开了 OpenType 字体的许多高级功能。Glyphs
元素,请参阅 MSDN 介绍。如果您查看提到的文本渲染管道图,您会注意到,虽然字形是构建块,但它们在最终显示在特定介质(例如屏幕)上之前可能会以各种方式进行操作(过滤/转换/...)或打印机; 一个例子是在 LCD 屏幕上应用 ClearType。 然而,与通常的流水线概念一样,这些转换通常更多或更少可选。
现在,根据您的应用程序要求,这可能已经产生了解决方案。 如果您确实不需要 TextBox,您也可以像这样使用 Glyphs 本身:
Glyphs 元素根据需要呈现数字的空图块,而 TextBox 元素则不会。 请注意,由于 Glyphs 是低级元素,因此存在一些限制,特别是 FontUri/Fill/FontRenderingEmSize 都是必需的,即没有像相关 TextBox 属性那样的默认值。
考虑到所有这些,回到你原来的问题:
我不认为当前的问题是 WPF 限制(甚至是错误),而是与 WPF UI 布局上下文中应用的文本渲染要求和默认值有关的影响。 例如,复合(即非低级)
TextBox
控件将文本格式和版式设置应用于其内容(字形),促进嵌入字体中的各种字符映射表(可能有 很多...); 像 Scramble 这样的特殊/简单字体可能无法在此处提供足够或正确的信息,因此 WPF 渲染引擎可能被迫应用字体回退,如 银河牛仔已经概述了。如果情况确实如此,则可能会以某种方式覆盖 WPF 默认渲染算法(请参阅类
TextFormatter
,WPF 文本引擎),但可能需要深入挖掘框架才能弄清楚在文本框
。 在 Scramble 字体中“调试”最终丢失或不正确的字符映射可能要容易得多。 但这将是一次完全不同的努力......Typography in general and fonts in particular are a fairly complex topic I'm not really into. That said here's what I've figured from Typography in Windows Presentation Foundation and related MSDN documentation:
Typography
object exposes many of the advanced features of OpenType fonts.Glyphs
element, see MSDN introduction.If you look at the mentioned text rendering pipeline diagram you'll notice that while glyphs are the building blocks they may be acted upon in various ways (filtered/transformed/...) before finally getting displayed on a particular medium, e.g. a screen or a printer; one example would be applying ClearType for LCD screens. However, as usually with pipelining concepts, those transformations are more ore less optional in general.
Now, depending on your application requirements, this might already yield a solution. If you don't really need a TextBox you might as well just use Glyphs in itself like so:
The Glyphs element is rendering empty tiles for numbers as desired while the TextBox element does not. Please note that due to Glyphs being a low level element, several restrictions apply, especially FontUri/Fill/FontRenderingEmSize are all required, i.e. there are no defaults like for the related TextBox properties.
With all this in mind back to your original question:
I wouldn't think of the problem at hand as a WPF limitation (or a bug even), rather of an effect regarding the text rendering requirements and defaults applied in the context of WPF UI layout. E.g. the composite (i.e. non low level)
TextBox
control is applying text formatting and typography settings to its content (glyphs), facilitating the various character mapping tables embedded in fonts (there are potentially lots of them...); special/simplistic fonts like Scramble might just not provide enough or correct information here, consequently the WPF rendering engine might be forced to apply font fallback as outlined by GalacticCowboy already.If that's indeed the case, it might be possible to override the WPF default rendering algorithm somehow (see class
TextFormatter
, the WPF text engine), but likely one would need to dig pretty deep into the framework to figure out what's going on within aTextBox
. It's probably much easier to 'debug' the eventually missing or incorrect character mappings within the Scramble font. This would be an entirely different endeavor though...我不喜欢 WPF,但我知道 Microsoft 通常以不同的方式处理拉丁字符和非拉丁字符。 我发现您可以定义复合字体,并将特定的 unicode 范围映射到字体。 您可以尝试强制使用整个 unicode 范围的字体。 链接文本
I'm not into WPF, but i know that Microsoft handles in general latin an non-latin characters differently. I have seen that you can define a composite font, and map a specific unicode range to a font. You can try to force your font for the whole unicode range. link text