wch / extrafont

Tools for using fonts in R graphics
312 stars 49 forks source link

extrafont fonts not recognized #26

Open jxchong opened 11 years ago

jxchong commented 11 years ago

I followed the instructions and installed the Robot font family (downloaded from Google Fonts), but I can't seem to get pdf() to recognize the font.

When I type subset(fonttable(), FamilyName == "Roboto"), it seems to show that the font is loaded, but when I try to generate the pdf, I get an error:

pdf("test.pdf", width=4, height=7, family="Roboto") Error in pdf("Glycosylation Burden.pdf", width = 4, height = 7, family = "Roboto") : unknown family 'Roboto'

subset(fonttable(), FamilyName == "Roboto") package afmfile fontfile FullName FamilyName FontName Bold Italic Symbol afmsymfile 158 NA Roboto-Black.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Black.ttf Roboto Black Roboto Roboto-Black FALSE FALSE FALSE NA 159 NA Roboto-BlackItalic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-BlackItalic.ttf Roboto Black Italic Roboto Roboto-BlackItalic FALSE TRUE FALSE NA 160 NA Roboto-Bold.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Bold.ttf Roboto Bold Roboto Roboto-Bold TRUE FALSE FALSE NA 161 NA Roboto-BoldItalic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-BoldItalic.ttf Roboto Bold Italic Roboto Roboto-BoldItalic TRUE TRUE FALSE NA 162 NA Roboto-Italic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Italic.ttf Roboto Italic Roboto Roboto-Italic FALSE TRUE FALSE NA 163 NA Roboto-Light.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Light.ttf Roboto Light Roboto Roboto-Light FALSE FALSE FALSE NA 164 NA Roboto-LightItalic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-LightItalic.ttf Roboto Light Italic Roboto Roboto-LightItalic FALSE TRUE FALSE NA 165 NA Roboto-Medium.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Medium.ttf Roboto Medium Roboto Roboto-Medium FALSE FALSE FALSE NA 166 NA Roboto-MediumItalic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-MediumItalic.ttf Roboto Medium Italic Roboto Roboto-MediumItalic FALSE TRUE FALSE NA 167 NA Roboto-Regular.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Regular.ttf Roboto Regular Roboto Roboto-Regular FALSE FALSE FALSE NA 168 NA Roboto-Thin.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Thin.ttf Roboto Thin Roboto Roboto-Thin FALSE FALSE FALSE NA 169 NA Roboto-ThinItalic.afm.gz ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-ThinItalic.ttf Roboto Thin Italic Roboto Roboto-ThinItalic FALSE TRUE FALSE NA

This might be related. After I quit and restarted R library(extrafont) Error : .onAttach failed in attachNamespace() for 'extrafont', details: call: (function (...) error: invalid arguments in 'pdfFonts' (must be font names) Error: package or namespace load failed for ‘extrafont’

wch commented 11 years ago

Two things to check:

jxchong commented 11 years ago

Yes I ran loadfonts before, actually tried both: loadfonts() loadfonts(device="postscript")

I think the namespace conflict might have been related to the fact that I tried installing the github extrafont install first (wanted to try an OTF font). So, I manually deleted the package folder for extrafont and extrafontdb from the R folder and then reinstalled extrafont.

After starting from a clean slate, I tried to reimport Roboto and a second Google Font download (Open Sans).

font_import(paths="~/Dropbox/Public/Exported Fonts", recursive=TRUE, prompt=FALSE)
Scanning ttf files in ~/Dropbox/Public/Exported Fonts ... Extracting .afm files from .ttf files...

~/Dropbox/Public/Exported Fonts/Open_Sans/OpenSans-Bold.ttf => ~/Library/R/3.0/library/extrafontdb/metrics/OpenSans-Bold ~/Dropbox/Public/Exported Fonts/Roboto/Roboto-Black.ttf => ~/Library/R/3.0/library/extrafontdb/metrics/Roboto-Black

[…] Found FontName for 22 fonts. Scanning afm files in /Users/jxchong/Library/R/3.0/library/extrafontdb/metrics Writing font table in /Users/jxchong/Library/R/3.0/library/extrafontdb/fontmap/fonttable.csv Writing Fontmap to /Users/jxchong/Library/R/3.0/library/extrafontdb/fontmap/Fontmap... There were 50 or more warnings (use warnings() to see the first 50)

warnings() 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

loadfonts() Open Sans already registered with pdfFonts(). Open Sans Extrabold already registered with pdfFonts(). Open Sans Light already registered with pdfFonts(). Open Sans Semibold already registered with pdfFonts(). Warning message: In loadfonts() : More than one version of regular/bold/italic found for Roboto. Skipping setup for this font.

When I run the second command:

str(pdfFonts(), max.level=1) $ Open Sans :List of 3 ..- attr(, "class")= chr "Type1Font" $ Open Sans Extrabold :List of 3 ..- attr(, "class")= chr "Type1Font" $ Open Sans Light :List of 3 ..- attr(, "class")= chr "Type1Font" $ Open Sans Semibold :List of 3 ..- attr(, "class")= chr "Type1Font"

(but no Roboto)

Now pdf("test.pdf", width=4, height=7, family="Open Sans") does work (though Roboto doesn't work)

On Thursday, April 11, 2013 at 1:35 PM, Winston Chang wrote:

Two things to check: Did you run loadfonts()? After running loadfonts(), does the font show up when you run str(pdfFonts(), max.level=1)?

— Reply to this email directly or view it on GitHub (https://github.com/wch/extrafont/issues/26#issuecomment-16259619).

wch commented 11 years ago

I just rebased and pushed the freetype branch. If you want to try that, install that and the freetype2 branch of Rttf2pt1.

As for Roboto, the issue is this: extrafont doesn't know how to distinguish between Roboto Black, Roboto Light, and plain Roboto. The FamilyName entry in all of these is just "Roboto". R wants each font family to have a regular, bold, italic, and bold-italic version, but Roboto doesn't fit this scheme. Here are all the different versions:

Roboto Regular
Roboto Bold
Roboto Italic
Roboto Bold Italic

Roboto Black
Roboto Black Italic

Roboto Light
Roboto Light Italic

Roboto Medium
Roboto Medium Italic

Roboto Thin
Roboto Thin Italic

Probably the best solution is to edit the font table so that the FamilyName entry for the Black version is "Roboto Black" instead of "Roboto". Or you could delete those entries. The font table file should be in the directory returned by system.file("fontmap", "fonttable.csv", package="extrafontdb").

Unfortunately, I don't see a straightforward way of automating font imports so it knows how to handle cases like this.

jxchong commented 11 years ago

Thanks, just to clarify, it's the Robot Black, Light, Medium, and Thin ones that are the problems? So if I edit the font table to make those have their own distinct family names, that should fix the conflict?

wch commented 11 years ago

Yes, I think that should do the trick.

Rufflewind commented 9 years ago

I just ran into the same problem. If it can't be fixed automatically, it would at least nice to get a warning instead of silently ignoring the font so the user can fix the problem by hand.

There are quite a few fonts can't be loaded automatically due to this same problem (e.g. Linux Libertine, Merriweather, Signika) so I don't think this is a particularly rare occurrence.

JanaJarecki commented 5 years ago

Hi! I just ran into the same problem, using another font. Is there a way to fix this automatically? Kind regards, Jana

JanaJarecki commented 4 years ago

Would a fix be replacing FamilyName with FullName if there are duplicated font names?