有没有办法让 PhantomJS(或任何无头浏览器)使用备用字体缓存?

Dra*_*kes 7 linux command-line fonts

有没有办法让 PhantomJS(或任何无头浏览器)使用替代字体缓存/usr/share/fonts/?在 PhantomJS 中使用更多字体(例如 CJK 字体)的一种方法是将它们安装到此目录中。但是,这是一个共享服务器,无法完成。

我似乎无法为此找到CLI 参数。如果这是一个愚蠢的问题,请原谅我。

这是一个 RedHat 版本,yum 和 rpm 被禁用。

PhantomJS 的屏幕截图- 字体未加载:

字体渲染失败

预期结果http://v1.jontangerine.com/silo/typography/web-fonts/):

实际字体渲染


已解决: @grochmal 向我展示了字体可以安装在主文件夹中。我跑fc-cache -vf了,系统字体和~/.fonts/TTF字体都被缓存了。例如,运行fc-list "impact"找到 Impact 字体(仅供个人使用):

> fc-list impact
Impact:style=Regular,Normal,oby?ejné,Standard,????????,Normaali,Normál,Normale,Standaard,Normalny,???????,Normálne,Navadno,Arrunta
Run Code Online (Sandbox Code Playgroud)

我用@grochmal 巧妙建议的堆栈跟踪证实了这一点:

strace ./phantomjs ../examples/rasterize.js http://example.com img.jpg 2>&1 | grep font
Run Code Online (Sandbox Code Playgroud)

并发现 PhantomJS 确实在我的用户字体目录中查找

open("/home/user1/.fonts/TTF/verdana.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/AndaleMo.TTF", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/arial.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/cour.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/georgia.ttf", O_RDONLY) = 11
open("/home/user1/.fonts/TTF/impact.ttf", O_RDONLY) = 11
...
Run Code Online (Sandbox Code Playgroud)

gro*_*mal 4

PhantomJS 尊重fontconfig目录,甚至旧的font.dir/ font.scalepostscript 字体配置。例如我有旧的 Type 1 字体:

$ find ~/.fonts/Type1/
/home/grochmal/.fonts/Type1/
/home/grochmal/.fonts/Type1/augie___.pfb
/home/grochmal/.fonts/Type1/fonts.scale
/home/grochmal/.fonts/Type1/fonts.dir
Run Code Online (Sandbox Code Playgroud)

(这是用 ol' X11 创建的mkfontdir

并且,为了更好的示例,我将把fotnconfig字体复制到我的主目录中:

$ mkdir -p ~/.local/share/fonts/TTF
$ cp /usr/share/fonts/TTF/HomemadeApple.ttf ~/.local/share/fonts/TTF
$ fc-cache  # just in case
Run Code Online (Sandbox Code Playgroud)

现在让我们看看 PhantomJS 如何使用它们(使用 PhantomJS github 中的经典示例):

$ wget https://raw.githubusercontent.com/ariya/phantomjs/master/examples/rasterize.js
Run Code Online (Sandbox Code Playgroud)

strace打印所有系统调用(包括文件系统访问):

$ strace phantomjs rasterize.js 2>&1 | grep font | grep grochmal |grep -v cache
stat("/home/grochmal/.config/fontconfig/conf.d", 0x7ffff95fbbc0) = -1 ENOENT (No such file or directory)
stat("/home/grochmal/.config/fontconfig/conf.d", 0x7ffff95fbbc0) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.config/fontconfig/conf.d", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.config/fontconfig/conf.d", R_OK) = -1 ENOENT (No such file or directory)
stat("/home/grochmal/.config/fontconfig/fonts.conf", 0x7ffff95fbbc0) = -1 ENOENT (No such file or directory)
stat("/home/grochmal/.config/fontconfig/fonts.conf", 0x7ffff95fbbc0) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.config/fontconfig/fonts.conf", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.config/fontconfig/fonts.conf", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.fonts.conf.d", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.fonts.conf.d", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.fonts.conf", R_OK) = -1 ENOENT (No such file or directory)
access("/home/grochmal/.fonts.conf", R_OK) = -1 ENOENT (No such file or directory)
stat("/home/grochmal/.local/share/fonts", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.local/share/fonts", O_RDONLY|O_CLOEXEC) = 4
stat("/home/grochmal/.local/share/fonts", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.local/share/fonts", O_RDONLY|O_CLOEXEC) = 4
open("/home/grochmal/.local/share/fonts", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 5
stat("/home/grochmal/.local/share/fonts/HomemadeApple.ttf", {st_mode=S_IFREG|0644, st_size=110080, ...}) = 0
open("/home/grochmal/.local/share/fonts/HomemadeApple.ttf", O_RDONLY) = 6
stat("/home/grochmal/.local/share/fonts/TTF", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/home/grochmal/.fonts", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.fonts", O_RDONLY|O_CLOEXEC) = 4
stat("/home/grochmal/.local/share/fonts/TTF", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.local/share/fonts/TTF", O_RDONLY|O_CLOEXEC) = 4
stat("/home/grochmal/.local/share/fonts/TTF", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.local/share/fonts/TTF", O_RDONLY|O_CLOEXEC) = 4
open("/home/grochmal/.local/share/fonts/TTF", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4
stat("/home/grochmal/.local/share/fonts/TTF/HomemadeApple.ttf", {st_mode=S_IFREG|0644, st_size=110080, ...}) = 0
open("/home/grochmal/.local/share/fonts/TTF/HomemadeApple.ttf", O_RDONLY) = 5
stat("/home/grochmal/.fonts/Type1", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/home/grochmal/.fonts/Type1", O_RDONLY|O_CLOEXEC) = 4
Run Code Online (Sandbox Code Playgroud)

PhantomJS 进入字体目录并加载它们!

我没有CJK 字体~/.config/fontconfig/fonts.conf可能需要的(因为这些字体可能需要一些实际配置),但您可以从中复制文件(特别是某些字体,以获取示例配置)。/etc/fonts/conf.d/*nonlatin

然而,您可能可以通过简单地将大多数字体放入~/.local/share/fonts/TTF然后运行来摆脱它们fc-cache

免责声明:旧的 RedHat(肯定是 5,不确定 6)可能不使用fontconfig,这就是我在示例中包含 PFB 字体的原因。在这种情况下,您需要使用ttmkfdirmkfontdir来生成font.scalefont.dir文件。

参考: