Is there a way to make PhantomJS (or any headless browser) use an alternate font cache besides /usr/share/fonts/
? One way to use more fonts (e.g. CJK fonts) with PhantomJS is to install them to this directory. However, this is a shared server and cannot be done.
I cannot seem to find a CLI parameter for this. Please forgive me if this is a silly question.
This is a RedHat build, and yum and rpm are disabled.
Screenshot with PhantomJS – fonts not loading:
Desired result (http://v1.jontangerine.com/silo/typography/web-fonts/):
SOLVED: @grochmal showed me that fonts can be installed in the home folder. I ran fc-cache -vf
and the system fonts and the ~/.fonts/TTF
fonts get cached. For example, running fc-list "impact"
finds the Impact font (for personal use only):
> fc-list impact
Impact:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
I confirmed this with the stack trace cleverly suggested by @grochmal:
strace ./phantomjs ../examples/rasterize.js http://example.com img.jpg 2>&1 | grep font
and found out that PhantomJS is indeed looking in my user fonts directory
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
...
Best Answer
PhantomJS respects
fontconfig
directories and even the oldfont.dir
/font.scale
postscript font configuration. For example I have and old Type 1 font:(That was created with the ol' X11
mkfontdir
)And, for a better example, I'll copy a
fotnconfig
font into my home directory:Now let's see how PhantomJS uses them (using a classic example from the PhantomJS github):
strace
prints all system calls (including filesystem access):And PhantomJS went to the font directories and loaded them!
I do not have a
~/.config/fontconfig/fonts.conf
which may be needed for CJK fonts (because those may need some actual configuration), but you can copy a file from/etc/fonts/conf.d/*
(notably somenonlatin
font, to get a sample configuration).Yet, you can probably get away with most fonts by simply dropping them into
~/.local/share/fonts/TTF
and then runningfc-cache
.Disclaimer: An old RedHat (5 for sure, not sure about 6) may not be using
fontconfig
, that's why I included the PFB font in the example. In that case you need to usettmkfdir
andmkfontdir
to generate thefont.scale
andfont.dir
files.References: