如果在系统里面存在诡异的字体,同时自己的 WPF 中有一个控件尝试使用这个字体放在界面中,那么将会在界面布局过程炸了,整个控件或者整个界面布局都无法继续

本文本来是由吕水大大发布的,但是他没空写,于是我就成为了写博客的工具人

有一个用户报告了软件在他的电脑上打不开列出本机字体列表,于是吕水大大就去远程他的设备,在用户的设备上找到了一个诡异的字体,加载这个字体的时候,将会在 MS.Internal.Text.TextInterface.Font.CreateFontFace 抛出异常。而且有趣的是 Win10 的 UWP 版的设置里面是找不到这个字体的,原因是 UWP 版本也会在读取此字体的时候炸了

复现的步骤如下,先从 https://github.com/walterlv/Walterlv.Demo.WpfBadFontCrash 下载代码,在代码仓库里面可以找到 不给糖就捣蛋的万圣节.TTF 这个字体,值得一说的是,诡异的字体有很多,这个字体只是一个例子。双击安装一下这个字体到你的系统上

然后新建一个 WPF 或 UWP 程序,在界面里面添加一个 TextBlock 然后在 TextBlock 里面采用此字体,如下面代码

    <TextBlock Text="ABCDEFGH" FontFamily="不给糖就捣蛋的万圣节" />

参与运行此 WPF 程序,将会看到如下提示

System.IO.FileFormatException: Invalid file format.
at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)
at MS.Internal.Text.TextInterface.Font.CreateFontFace()
at MS.Internal.Text.TextInterface.Font.AddFontFaceToCache()
at MS.Internal.Text.TextInterface.Font.GetFontFace()
at System.Windows.Media.GlyphTypeface..ctor(Font font)
at MS.Internal.FontFace.PhysicalFontFamily.GetGlyphTypeface(FontStyle style, FontWeight weight, FontStretch stretch)
at MS.Internal.FontFace.PhysicalFontFamily.MS.Internal.FontFace.IFontFamily.GetTypefaceMetrics(FontStyle style, FontWeight weight, FontStretch stretch)
at System.Windows.Media.Typeface.ConstructCachedTypeface()
at System.Windows.Media.Typeface.get_CachedTypeface()
at System.Windows.Media.Typeface.CheckFastPathNominalGlyphs(CharacterBufferRange charBufferRange, Double emSize, Single pixelsPerDip, Double scalingFactor, Double widthMax, Boolean keepAWord, Boolean numberSubstitution, CultureInfo cultureInfo, TextFormattingMode textFormattingMode, Boolean isSideways, Boolean breakOnTabs, Int32& stringLengthFit)
at MS.Internal.TextFormatting.SimpleRun.CreateSimpleTextRun(CharacterBufferRange charBufferRange, TextRun textRun, TextFormatterImp formatter, Int32 widthLeft, Boolean emergencyWrap, Boolean breakOnTabs, Double pixelsPerDip)
at MS.Internal.TextFormatting.SimpleRun.Create(FormatSettings settings, CharacterBufferRange charString, TextRun textRun, Int32 cp, Int32 cpFirst, Int32 runLength, Int32 widthLeft, Int32 idealRunOffsetUnRounded, Double pixelsPerDip)
at MS.Internal.TextFormatting.SimpleTextLine.Create(FormatSettings settings, Int32 cpFirst, Int32 paragraphWidth, Double pixelsPerDip)
at MS.Internal.TextFormatting.TextFormatterImp.FormatLineInternal(TextSource textSource, Int32 firstCharIndex, Int32 lineLength, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
at MS.Internal.TextFormatting.TextFormatterImp.FormatLine(TextSource textSource, Int32 firstCharIndex, Double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache)
at System.Windows.Controls.TextBlock.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Decorator.MeasureOverride(Size constraint)
at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Window.MeasureOverrideHelper(Size constraint)
at System.Windows.Window.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

如果上面的逻辑是放在 MainWindow 里面,那么意味着刚尝试创建窗口就凉凉了,可以看到窗口都创建失败。如果自己没有写 Dispatcher.UnhandledException 那么应用程序将会退出

代码请看 https://github.com/walterlv/Walterlv.Demo.WpfBadFontCrash

这个坑我报告给了 WPF 官方,请看 WPF can not handle special damaged font · Issue #4283 · dotnet/wpf

对于 UWP 来说也一样,尝试在界面中放和上面 WPF 相同的代码,可以看到设计器给出了提示

运行 UWP 应用,将可以看到进入了下面代码

#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif

这里面的 e 的内容没有啥有用的信息,可以看到的代码如下

-		Exception	{"指示输入文件 (例如字体文件) 中的错误。\r\n\r\n指示输入文件 (例如字体文件) 中的错误。\r\n"}	System.Exception {System.Runtime.InteropServices.COMException}

也就说 UWP 在调用到更底层的时候炸掉了,其实也看不到堆栈。同时在 UWP 如果是在第一个界面中添加以上代码,那么即使在 App.xaml.cs 使用下面代码尝试接住,应用也是继续退出

        public App()
{
InitializeComponent();
Suspending += OnSuspending;
UnhandledException += App_UnhandledException;
} private void App_UnhandledException(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)
{
e.Handled = true;
}

代码放在 githubgitee 欢迎小伙伴访问

WPF 加载诡异的字体无法布局的更多相关文章

  1. WPF加载Winform窗体时 报错:子控件不能为顶级窗体

    一.wpf项目中引用WindowsFormsIntegration和System.Windows.Forms 二.Form1.Designer.cs 的 partial class Form1 设置为 ...

  2. WPF加载HTML、WPF与JavaScript交互

    目录 一.WebBrowser加载远程网页 二.WebBrowser加载本地网页,注:不可以加载本地样式CSS和脚本JS文件 三.WebBrowser隐藏网页的JavaScript错误 四.网页屏蔽鼠 ...

  3. ASP.net 加载不了字体Failed to load resource: the server responded with a status of 404 (Not Found)

    在bootstrap下加载不了字体内容.出现下列错误. 1.打开IIS找到部署的网站,点击MIME类型,把.woff和.woff2两个类型分别添加到新类型中,重启网站即可.  

  4. WPF 加载等待动画

    原文:WPF 加载等待动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_29844879/article/details/80216587 ...

  5. WPF加载等待动画

    原文:WPF加载等待动画 原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner 界面遮罩 <UserC ...

  6. iOS加载动态自定义字体

    iOS加载动态自定义字体  NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDom ...

  7. uiwebview 加载html时字体变小 加载前或加载后改变字体大小

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } p.p2 { margin: 0.0px 0. ...

  8. WPF加载程序集中字符串资源

    WPF资源 WPF资源使用其实的也是resources格式嵌入资源,默认的资源名称为"应用程序名.g.resources",不过WPF资源使用的pack URI来访问资源. 添加图 ...

  9. WPF 加载 WINFORM控件 异常: 调度程序进程已挂起,但消息仍在处理中

    在加载TradeAtServer的统计中的 单个合约盈亏情况 异常:,调度程序进程已挂起,但消息仍在处理中 发现可能是属性设置引发的问题 比如DateTimePikcer.Value+= set, g ...

  10. wpf 加载项目图片的几种写法

    new System.Windows.Media.Imaging.BitmapImage( new Uri("pack://application:,,,/TeacherAssistant. ...

随机推荐

  1. 三维模型3DTile格式轻量化压缩模型变形浅析

    三维模型3DTile格式轻量化压缩模型变形浅析 在对三维模型进行轻量化压缩处理的过程中,常常会出现模型变形的现象.这种变形现象多数源于模型压缩过程中信息丢失或误差累积等因素.以下将对此现象进行详细分析 ...

  2. openApi generator总是生成类名为 defaultApi

    生成器可以开启 useTags 设置,开启之后会根据 api 文档中的 tags 生成前缀类名,因此,要不生成 defaultApi 需要以下操作: 1.openApi 文档中每个 url 必须要有 ...

  3. Vue3.0里为什么要用 Proxy API 替代 defineProperty API ?

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 一.Object.defineProperty 定义:Object.defineProperty() 方法会直接在一个对象上定义一个新属性 ...

  4. modelsim的工程文件结构

    modelsim的工程文件结构 1.工程结构 modelsim中的工程包括一个库(这个库可以是空的,也可以包含器件延时信息的真实库),一个工程(以mpf为后缀的文件是工程的快捷打开方式)和若干源文件. ...

  5. 测试开发之系统篇-安装KVM虚拟机

    虚拟机(Virtual Machine)和容器(Container)是两种流行的虚拟化技术. 虚拟机模拟机器的硬件,包括了完整的操作系统和应用,它一旦被开启,预分配给它的资源将全部被占用.容器是运行在 ...

  6. #点分治#洛谷 4149 [IOI2011]Race

    题目 给一棵树,每条边有权.求一条简单路径,权值和等于 \(k\),且边的数量最小. 分析 点分治,记录一定权值的最小边数量, 每遍历一棵子树后统计答案 代码 #include <cstdio& ...

  7. #回滚莫队#AT1219 歴史の研究

    洛谷题目 AT1219 分析 不满足区间减性质的运算,如最值,就不能用普通莫队求, 考虑回滚莫队,它的核心思想就是若区间在块内直接暴力, 否则将右端点从小到大排序,右端点按普通莫队求,那么左端点由于只 ...

  8. OpenHarmony 官网文档有哪些上新?上篇:应用开发文档上新

    随着 OpenAtom OpenHarmony(以下简称"OpenHarmony")系统能力持续升级,已具备支撑复杂带屏标准设备和应用开发的基础能力.相较于旧版本,OpenHarm ...

  9. Seaborn调色盘设置

    调色盘设置 设置调色 color_palette()设置调色盘,返回一个调色盘的颜色列表,默认6种颜色:deep, muted, bright, pastel, dark, colorblind. s ...

  10. HarmonyOS音频开发指导:使用OpenSL ES开发音频播放功能

      OpenSL ES全称为Open Sound Library for Embedded Systems,是一个嵌入式.跨平台.免费的音频处理库.为嵌入式移动多媒体设备上的应用开发者提供标准化.高性 ...