WPF 字体:为什么缺少一些字符?

发布于 2024-07-19 15:49:56 字数 661 浏览 7 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒解孤独 2024-07-26 15:49:56

来自 MSDN

字体后备

字体回退是指自动
替换除
客户选择的字体
应用。 主要有两个
调用字体回退的原因:

  • 客户端应用程序指定的字体不存在
    在系统上。
  • 客户端应用程序指定的字体不
    包含所需的字形
    渲染文本。

在WPF中,字体回退机制
使用默认后备字体系列,
“全局用户界面”,作为
替代字体。 该字体已定义
作为复合字体,其文件名

“GlobalUserInterface.CompositeFont”。
有关复合材料的更多信息
字体,请参阅复合字体部分
在这个主题中。

WPF 字体回退机制
替换以前的 Win32 字体
替代技术。

我的猜测是该字体不支持 Unicode - 该字体本身是在 1996 年创建的,并且由于它旨在模拟 Scrabble 作品,因此我不确定字体作者是否考虑过本地化。

编辑
根据字体文档,该字体支持字母,任何数字都应该呈现空白图块。 空间不渲染图块。

From MSDN:

Font Fallback

Font fallback refers to the automatic
substitution of a font other than the
font that is selected by the client
application. There are two primary
reasons why font fallback is invoked:

  • The font that is specified by the client application does not exist
    on the system.
  • The font that is specified by the client application does not
    contain the glyphs that are required
    to render text.

In WPF, the font fallback mechanism
uses the default fallback font family,
"Global User Interface", as the
substitute font. This font is defined
as a composite font, whose file name
is
"GlobalUserInterface.CompositeFont".
For more information about composite
fonts, see the Composite Fonts section
in this topic.

The WPF font fallback mechanism
replaces previous Win32 font
substitution technologies.

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.

人│生佛魔见 2024-07-26 15:49:56

简短的回答是,Scramble 不会在 WPF 或其他任何东西中使用空白拼字游戏块来表示全角空格。 您需要另一种字体或其他字体来编辑 em/en 空格字形(例如 Font Creator)。

我得到 另一种字体(x-grid),我知道它有一个特殊的空格字形,并在下面的代码中使用它们。

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" FontFamily="Scramble" TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" Text="Example Text M M M"/>
    <TextBlock Grid.Row="1" FontFamily="x-grid"  TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" Text="Example Text M M M"/>
</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.

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="500">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" FontFamily="Scramble" TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" Text="Example Text M M M"/>
    <TextBlock Grid.Row="1" FontFamily="x-grid"  TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" Text="Example Text M M M"/>
</Grid>

The resulting window looked like this.

alt text

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.

一百个冬季 2024-07-26 15:49:56

不要设置 FontFaily="Fonts/#Scrambe",只需使用不带“Fonts/#”的字体名称:

<TextBox Text="Testing testing testing" FontFamily="Scramble" />

这对我有用。 我下载并安装了您提到的字体,无论是在 VS2008 的设计视图中还是在运行应用程序时,它都完美地显示了该字体。

Instead of setting FontFaily="Fonts/#Scrambe", just use the font name without "Fonts/#":

<TextBox Text="Testing testing testing" FontFamily="Scramble" />

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.

铜锣湾横着走 2024-07-26 15:49:56

一般的版式,特别是字体是一个相当复杂的话题,我并不真正感兴趣。 这就是我从 Windows Presentation Foundation 中的版式中得出的结果和相关的 MSDN 文档:

  • 文本通过文本渲染管道渲染(请参阅上面的链接以获取图表)。
  • WPF 促进 OpenType 作为 TrueType 字体格式的扩展。
    • 版式 对象公开了 OpenType 字体的许多高级功能。
  • 一个重要的低级文本呈现概念是 Glyphs 元素,请参阅 MSDN 介绍

如果您查看提到的文本渲染管道图,您会注意到,虽然字形是构建块,但它们在最终显示在特定介质(例如屏幕)上之前可能会以各种方式进行操作(过滤/转换/...)或打印机; 一个例子是在 LCD 屏幕上应用 ClearType。 然而,与通常的流水线概念一样,这些转换通常更多或更少可选。

现在,根据您的应用程序要求,这可能已经产生了解决方案。 如果您确实不需要 TextBox,您也可以像这样使用 Glyphs 本身:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>
    <TextBox Text="TextBox: 1234567890" FontFamily="Scramble" FontSize="12" />
    <Glyphs UnicodeString="Glyphs: 1234567890" FontUri="C:\WINDOWS\Fonts\Scramble.TTF" 
            FontRenderingEmSize="12" Fill="Black" OriginX="5" OriginY="32"/>
  </Grid>
</Page>

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:

  • Text is rendered by means of a text rendering pipeline (see link above for a diagram).
  • WPF facilitates OpenType as an extension of the TrueType font format.
    • The Typography object exposes many of the advanced features of OpenType fonts.
  • An important low level text rendering concept is the 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:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <Grid>
    <TextBox Text="TextBox: 1234567890" FontFamily="Scramble" FontSize="12" />
    <Glyphs UnicodeString="Glyphs: 1234567890" FontUri="C:\WINDOWS\Fonts\Scramble.TTF" 
            FontRenderingEmSize="12" Fill="Black" OriginX="5" OriginY="32"/>
  </Grid>
</Page>

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 a TextBox. 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...

南七夏 2024-07-26 15:49:56

我不喜欢 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文