extrafont
extrafont copied to clipboard
Will extrafont work with OpenType (*.otf) fonts like Myriad Pro?
Lots of the new fonts in Windows 7 (and I think Mac) are OpenType, which I think is a superset of TrueType. Perhaps 50% of the fonts on my Win7 machine are TrueType, and that's just from installing standard Microsoft applications.
Hmm. This may require recompiling ttf2pt1 to use the FreeType 2 library, instead of relying on ttf2pt1's built-in ttf-to-afm capabilities. Hopefully ghostscript can also embed OTF fonts directly, as it can with TTF fonts.
I've implemented this in an experimental branch. If you could test it out, that would be great.
Here's how to install it (you'll need a compiler installed, via the Windows RTools):
library(devtools)
install_github("Rttf2pt1", "wch", "freetype2")
install_github("extrafont", "wch", "freetype")
# Once it's installed, import the fonts as before:
library(extrafont)
font_import()
Hi Winston,
I ran into some issues using OTF fonts. Several fonts are imported and registered (e.g. Trade Gothic LT Std
):
> tail(fonts(), 10)
[1] "Trade Gothic LT Std" "Trajan Pro" "Trebuchet MS" "Tw Cen MT"
[5] "Verdana" "Webdings" "Wingdings" "Wingdings 2"
[9] "Wingdings 3" "Zapfino"
> loadfonts()
...
Registering font with R using pdfFonts(): Trade Gothic LT Std
Registering font with R using pdfFonts(): Trajan Pro
Registering font with R using pdfFonts(): Trebuchet MS
...
Some issue arise when using the font (e.g. Trade Gothic LT Std
):
pdf("test.pdf")
grid.text("some test text", .5, .5, gp=gpar(fontfamily="Trade Gothic LT Std"))
dev.off()
Warning messages:
1: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
unknown AFM entity encountered
2: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
unknown AFM entity encountered
3: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
unknown AFM entity encountered
4: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
unknown AFM entity encountered
5: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
font width unknown for character 0x20
6: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
font width unknown for character 0x20
The character 0x20
appears to be a blank space as it is not shown in the pdf output for the OTF fonts I have installed. In the pic below I printed the same text using several TTF / TTC (No. 1-10) and OTF fonts (No.>10). All OTF fonts lack the blank space.
@markheckmann I just rebased and push the freetype branches for Rttf2pt1 and extrafont. Could you try to re-install them, and re-import the fonts?
This might be related to #11, but I think the previous freetype branch didn't get that fix.
@wch I just reinstalled the freetype branches for both packages and reimported the fonts. When installing Rttf2pt1
I get quite a lot of warnings:
ttf2pt1.c: In function 'unicode_init_user':
ttf2pt1.c:647: warning: format '%d' expects type 'int *', but argument 3 has type 'int'
ttf2pt1.c:647: warning: format '%d' expects type 'int *', but argument 4 has type 'int'
...
pt1.c:307: warning: format '%x' expects type 'unsigned int', but argument 3 has type 'struct GENTRY *'
pt1.c:307: warning: format '%x' expects type 'unsigned int', but argument 4 has type 'struct GENTRY *'
...
The output still lacks the spaces. But the warnings have changed: Only the last two warnings remain.
pdf("test.pdf")
grid.text("some test text", .5, .5, gp=gpar(fontfamily="Trade Gothic LT Std"))
dev.off()
Warning messages:
1: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :+1:
font width unknown for character 0x20
2: In grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :
font width unknown for character 0x20
The lacking spaces are definitively an issue -- even for TTF-fonts that seem to somehow contain OTF standards: I tried to use the regular Constantia font which ships with Windows since 2007, CONSTAN.TTF. Same problem as described for OTF fonts above.
At least I could find a workaround: I opened CONSTAN.TTF with fontforge and let it create a new font (const.ttf) from it, unsetting the OTF-tickbutton in the "Options" submenu before saving. Using const.ttf instead, the spaces show up as they should.
I am trying to import an .otf font into R 3.0.3 on Windows 7. I have previously had no problem installing and using (with cairo_pdf but not pdf) truetype fonts on this setup. The freetype2 branch of Rttf2pt1 described above seems to install ok, but with lots of warnings; the freetype branch of extrafont installs fine. When I try to import the fonts it delivers warnings. But the font is not working - a test use of the font draws a plot (not shown) using the R default sans font.
The culprit looks likely to be in Rttf2pt1. Is there anything I can try?
> font_import(pattern="Gustan", prompt=FALSE)
Scanning ttf files in C:\Windows\Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\Gustan-Black.otf => C:/Users/Peter Ellis/Documents/R/win-library/3.0/extrafontdb/metrics/Gustan-Black
C:\Windows\Fonts\Gustan-BlackItalic.otf => C:/Users/Peter Ellis/Documents/R/win-library/3.0/extrafontdb/metrics/Gustan-BlackItalic
...
Found FontName for 16 fonts.
Scanning afm files in C:/Users/Peter Ellis/Documents/R/win-library/3.0/extrafontdb/metrics
Writing font table in C:/Users/Peter Ellis/Documents/R/win-library/3.0/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to C:/Users/Peter Ellis/Documents/R/win-library/3.0/extrafontdb/fontmap/Fontmap...
There were 16 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In readLines(fd, 30) : seek on a gzfile connection returned an internal error
2: In readLines(fd, 30) : seek on a gzfile connection returned an internal error
...
> loadfonts("win")
Registering font with R using windowsFonts(): Gustan
> loadfonts("pdf")
...
> phrase <- "The quick brown fox jumped over the lazy dog"
> plot(1:10, 1:10, main=phrase, family="Gustan")
> sessionInfo()
R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] extrafont_0.15.99 devtools_1.4.1
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.1 extrafontdb_1.0 httr_0.3
[5] memoise_0.1 parallel_3.0.3 RCurl_1.95-4.1 Rttf2pt1_1.1.99
[9] stringr_0.6.2 tools_3.0.3 whisker_0.3-2
An unrelated problem is distinguishing between the various families and sub-families, but I can fix that by editing the fonttable and it seems nothing to do with actual import of the OTF font.
Those branches were based on an older versions of extrafont and Rttf2pt1. I just pushed some changes to those branches. Can you try reinstalling them from github and see if it works now?
Thanks - super quick response! but, no I deleted extrafontdb and reinstalled it from CRAN and the other two from github as per the above, and it still has the same result.
Some extra info - I had previously successfully converted these OTF fonts to TTF and used them that way and they looked fine, but we're working with a designer who needs us to use the exact OTF fonts so they are the same as those from other inputs to the project.
The compile warnings are normal for Rttf2pt1. Have you tried the following?
- Create PDF and embed the fonts into it
- Create a PNG
Yes, tried creating PDFs and PNGs and didn't work. I've now switched to using {Cairo} rather than {extrafont} for this use, which seems to do the job fine. Thanks for your help, and the useful package, anyway.
So, just to clarify what ellisp meant for future Googlers like myself, from my reverse engineering of his comment:
You can correct the spaces problem, even for PDF output, by installing the Cairo
library, and using the device=cairo_pdf
argument of the ggsave
call.
This seems to work for me; I haven't checked yet however if Cairo requires loadfonts
, but at the moment I'm happy... I seem to be producing OTF enabled plots!
On Mac (10.10) with R 3.2.0 and
install_github("Rttf2pt1", "wch", "freetype2")
install_github("extrafont", "wch", "freetype")
library(extrafont)
font_import()
all I get for all .otf files is
...
/Library/Fonts/MyriadPro-Regular.otf : No FontName. Skipping.
...
Same here. No FontName. Skipping.
for every font file, both .otf and .ttf
Ditto on macOS Sierra (10.12) and R 3.3.3. Was there any solution to this?
For what it's worth, I updated the the freetype2 branch of Rttf2pt1 and the freetype branch of extrafont. It imports Myriad Pro for me (I'm running macOS 10.12.3):
library(devtools)
install_github("wch/Rttf2pt1@freetype2")
install_github("wch/extrafont@freetype")
library(extrafont)
font_import(pattern = "Myriad.*", prompt=FALSE)
# Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
# Extracting .afm files from .ttf files...
# /Users/winston/Library/Fonts/MyriadPro-Regular.otf => /Users/winston/R/3.3/extrafontdb/metrics/MyriadPro-Regular
# Found FontName for 1 fonts.
# Scanning afm files in /Users/winston/R/3.3/extrafontdb/metrics
# Writing font table in /Users/winston/R/3.3/extrafontdb/fontmap/fonttable.csv
# Writing Fontmap to /Users/winston/R/3.3/extrafontdb/fontmap/Fontmap...
# Warning messages:
# 1: In readLines(fd, 30) :
# seek on a gzfile connection returned an internal error
# 2: In grepl("^FamilyName", text) :
# input string 4 is invalid in this locale
# 3: In grepl("^FontName", text) : input string 4 is invalid in this locale
# 4: In grepl("^FullName", text) : input string 4 is invalid in this locale
# 5: In grepl("^Weight", text) : input string 4 is invalid in this locale
fonttable()
# package afmfile fontfile FullName FamilyName
# 1 NA MyriadPro-Regular.afm.gz /Users/winston/Library/Fonts/MyriadPro-Regular.otf Myriad Pro Myriad Pro
# FontName Bold Italic Symbol afmsymfile
# 1 MyriadPro-Regular FALSE FALSE FALSE NA
session_info()
# Session info -----------------------------------------------------------------------------------------------
# setting value
# version R version 3.3.2 (2016-10-31)
# system x86_64, darwin13.4.0
# ui RStudio (1.1.104)
# language (EN)
# collate en_US.UTF-8
# tz America/Chicago
# date 2017-03-14
#
# Packages ---------------------------------------------------------------------------------------------------
# package * version date source
# devtools * 1.12.0 2016-06-24 CRAN (R 3.3.0)
# digest 0.6.12 2017-01-27 cran (@0.6.12)
# extrafont * 0.16.0.99 2017-03-15 Github (wch/extrafont@bdc21a0)
# extrafontdb 1.0 2012-06-11 CRAN (R 3.3.0)
# memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
# rstudioapi 0.6 2016-06-27 CRAN (R 3.3.0)
# Rttf2pt1 1.3.0.99 2017-03-15 Github (wch/Rttf2pt1@d15ee89)
# withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
I haven't done any more work on it, and it's possible that more fixes are necessary. Notably, the warnings probably shouldn't be there. I don't have much time to devote to this anytime soon, though. So if someone feels like taking up this project, please feel free to clean it up and submit a PR.