8

一个字体问题的解决

 3 years ago
source link: https://zhuanlan.zhihu.com/p/265412088
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

一个字体问题的解决

首席水煮牛肉品鉴师

前几天有人问我问题,就是下面代码编译出来的PDF文件,会出现一些问题:

\documentclass{article}
\usepackage{unicode-math}
\setmathfont{Fira Math}
\begin{document}
$\{a\}$
\end{document}

编译出来一看,好像确实是有问题。具体表现是这样的:

v2-eeda5b766ca4dc69d8e50048cd152172_720w.jpg

以及这样的:

原问题的提问者,用的是LuaTeX,但是我用XeTeX也能够产出让Acrobat Reader报错的文档。

所以这一步能确定不是引擎搞鬼,甚至跟数学公式的处理也没关系。

所以追查的地方,应该是字体了。把文档中使用的字体解压出来看一看(见工具:PDF处理的一些小技巧),FiraMath的曲线实体格式是CFF。

所以……我就拿我去年写的CFF.py工具解析了一下,这才发现,受到影响的左括号的曲线之中存在一个操作符是别的字符曲线中不存在的:cntrmask。

在CFF相关的文档中,对于cntrmask的描述大概是:

The hint operators cntrmask and/or hintmask must not occur if the charstring has no stem hints.
The Type 2 Charstring Format

所以,需要检查的是字体(md5=37dbbbe38d8141ddfe8cf54736e8b5a1)中的cntrmask是不是如文档描述的那样。这时候可以用fonttools把字体转换为xml:

ttx -o FiraMath-Regular-New.xml FiraMath-Regular.otf

使用grep查找cntrmask字符串,发现有70个匹配,而且都是不带参数的。所以直接快速的一个解决方案是:

sed -i s/cntrmask//g FiraMath-Regular-New.xml
ttx FiraMath-Regular-New.xml

这样生成的FiraMath-Regular-New.otf文件,就不会出现前文出现的问题了。

原始的字体是通过fontforge生成的,所以不排除其他用fontforge生成的字体也会出现类似的问题。如果出现,可照此文方法尝试解决。如果解决不了,可以找我。

本文首发于公众号“学术与TeX”,欢迎搜索关注本公众号。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK