除此之外,还有没有办法让 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
我通过 @grochmal 巧妙建议的堆栈跟踪证实了这一点:
strace ./phantomjs ../examples/rasterize.js http://example.com img.jpg 2>&1 | grep font
发现 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
...
答案1
PhantomJS 尊重fontconfig
目录,甚至旧的font.dir
/ font.scale
postscript 字体配置。例如我有旧的 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
(这是用 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
现在让我们看看 PhantomJS 如何使用它们(使用 PhantomJS github 中的经典示例):
$ wget https://raw.githubusercontent.com/ariya/phantomjs/master/examples/rasterize.js
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
PhantomJS 进入字体目录并加载它们!
我没有~/.config/fontconfig/fonts.conf
哪个可能CJK 字体需要(因为那些可能需要一些实际配置),但您可以从中复制文件/etc/fonts/conf.d/*
(特别是某些nonlatin
字体,以获取示例配置)。
~/.local/share/fonts/TTF
然而,您可能可以通过简单地将大多数字体放入然后运行来摆脱它们fc-cache
。
免责声明:旧的 RedHat(肯定是 5,不确定 6)可能不使用fontconfig
,这就是我在示例中包含 PFB 字体的原因。在这种情况下,您需要使用ttmkfdir
和mkfontdir
来生成font.scale
和font.dir
文件。
参考: