wch / extrafont

Tools for using fonts in R graphics
315 stars 48 forks source link

Warnings & all characters end up on top of each other #34

Open SverreStausland opened 11 years ago

SverreStausland commented 11 years ago

My code:

#Create local directory for my packages
dir.create(path = "M:/PortableSoftware/R-3.0.2/mylibrary")

#Set local library for my packages
my.library <- "M:/PortableSoftware/R-3.0.2/mylibrary"

#Set repository
my.repo <- getOption(x = "repos")
my.repo["CRAN"] <- "http://cran.uib.no/"

#Install extrafont package
install.packages(pkgs = "extrafont", lib = my.library, repos = my.repo, dependencies = TRUE, type = "win.binary", verbose = TRUE)

#Load extrafont package
library(package = "extrafont", lib.loc = my.library, verbose = TRUE)

#Import all .ttf fonts in a given directory
ttf_import(paths = "M:/MyStuff/Fonts/timesnewroman6.80/")

#Verify that the fonts were imported
fonts()
fonttable()

#Register the imported fonts for R's pdf (or postscript) device
loadfonts(device = "pdf", quiet = FALSE)

#Testing if it worked
pdf("font_plot.pdf", family="Times New Roman", width=4, height=4)
plot(mtcars$mpg, mtcars$wt,
    main = "Fuel Efficiency of 32 Cars",
    xlab = "Weight (x1000 lb)",
    ylab = "Miles per Gallon")
dev.off()

Console output:

`
R version 3.0.2 (2013-09-25) -- "Frisbee Sailing"
Copyright (C) 2013 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> #Create local directory for my packages
> dir.create(path = "M:/PortableSoftware/R-3.0.2/mylibrary")
Warning message:
In dir.create(path = "M:/PortableSoftware/R-3.0.2/mylibrary") :
  'M:\PortableSoftware\R-3.0.2\mylibrary' already exists
> 
> #Set local library for my packages
> my.library <- "M:/PortableSoftware/R-3.0.2/mylibrary"
> 
> #Set repository
> my.repo <- getOption(x = "repos")
> my.repo["CRAN"] <- "http://cran.uib.no/"
> 
> #Install extrafont package
> install.packages(pkgs = "extrafont", lib = my.library, repos = my.repo, dependencies = TRUE, type = "win.binary", verbose = TRUE)
trying URL 'http://cran.uib.no/bin/windows/contrib/3.0/extrafont_0.15.zip'
Content type 'application/zip' length 34007 bytes (33 Kb)
opened URL
downloaded 33 Kb

package ‘extrafont’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\sverrej\AppData\Local\Temp\RtmpYrkbZs\downloaded_packages
> 
> #Load extrafont package
> library(package = "extrafont", lib.loc = my.library, verbose = TRUE)
Registering fonts with R
> 
> #Import all .ttf fonts in a given directory
> ttf_import(paths = "M:/MyStuff/Fonts/timesnewroman6.80/")
Scanning ttf files in M:/MyStuff/Fonts/timesnewroman6.80/ ...
Extracting .afm files from .ttf files...
M:\MyStuff\Fonts\timesnewroman6.80\times.ttf : TimesNewRomanPSMT already registered in fonts database. Skipping.
M:\MyStuff\Fonts\timesnewroman6.80\timesbd.ttf : TimesNewRomanPS-BoldMT already registered in fonts database. Skipping.
M:\MyStuff\Fonts\timesnewroman6.80\timesbi.ttf : TimesNewRomanPS-BoldItalicMT already registered in fonts database. Skipping.
M:\MyStuff\Fonts\timesnewroman6.80\timesi.ttf : TimesNewRomanPS-ItalicMT already registered in fonts database. Skipping.
Found FontName for 0 fonts.
Scanning afm files in M:/PortableSoftware/R-3.0.2/mylibrary/extrafontdb/metrics
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
3: In readLines(fd, 30) :
  seek on a gzfile connection returned an internal error
4: In readLines(fd, 30) :
  seek on a gzfile connection returned an internal error
> 
> #Verify that the fonts were imported
> fonts()
[1] "Times New Roman"
> fonttable()
  package        afmfile                                           fontfile
1      NA timesbi.afm.gz M:\\MyStuff\\Fonts\\timesnewroman6.80\\timesbi.ttf
2      NA timesbd.afm.gz M:\\MyStuff\\Fonts\\timesnewroman6.80\\timesbd.ttf
3      NA  timesi.afm.gz  M:\\MyStuff\\Fonts\\timesnewroman6.80\\timesi.ttf
4      NA   times.afm.gz   M:\\MyStuff\\Fonts\\timesnewroman6.80\\times.ttf
                     FullName      FamilyName                     FontName  Bold Italic
1 Times New Roman Bold Italic Times New Roman TimesNewRomanPS-BoldItalicMT  TRUE   TRUE
2        Times New Roman Bold Times New Roman       TimesNewRomanPS-BoldMT  TRUE  FALSE
3      Times New Roman Italic Times New Roman     TimesNewRomanPS-ItalicMT FALSE   TRUE
4             Times New Roman Times New Roman            TimesNewRomanPSMT FALSE  FALSE
  Symbol afmsymfile
1  FALSE         NA
2  FALSE         NA
3  FALSE         NA
4  FALSE         NA
> 
> #Register the imported fonts for R's pdf (or postscript) device
> loadfonts(device = "pdf", quiet = FALSE)
Times New Roman already registered with pdfFonts().
> 
> #Testing if it worked
> pdf("font_plot.pdf", family="Times New Roman", width=4, height=4)
Warning messages:
1: In pdf("font_plot.pdf", family = "Times New Roman", width = 4, height = 4) :
  unknown AFM entity encountered
2: In pdf("font_plot.pdf", family = "Times New Roman", width = 4, height = 4) :
  unknown AFM entity encountered
3: In pdf("font_plot.pdf", family = "Times New Roman", width = 4, height = 4) :
  unknown AFM entity encountered
4: In pdf("font_plot.pdf", family = "Times New Roman", width = 4, height = 4) :
  unknown AFM entity encountered
> plot(mtcars$mpg, mtcars$wt,
+ main = "Fuel Efficiency of 32 Cars",
+ xlab = "Weight (x1000 lb)",
+ ylab = "Miles per Gallon")
There were 50 or more warnings (use warnings() to see the first 50)
> dev.off()
null device 
          1 
> warnings()
Warning messages:
1: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x6d
2: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x31
3: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
4: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x31
5: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
6: In axis(side = side, at = at, labels = labels, ...) :
  font metrics unknown for character 0x4d
7: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x31
8: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
9: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x31
10: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
11: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
12: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
13: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
14: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
15: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
16: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
17: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
18: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
19: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x33
20: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
21: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x33
22: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x30
23: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x6d
24: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
25: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x32
26: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x33
27: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x33
28: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x34
29: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x34
30: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
31: In axis(side = side, at = at, labels = labels, ...) :
  font width unknown for character 0x35
32: In title(...) : font width unknown for character 0x46
33: In title(...) : font width unknown for character 0x75
34: In title(...) : font width unknown for character 0x65
35: In title(...) : font width unknown for character 0x6c
36: In title(...) : font width unknown for character 0x20
37: In title(...) : font width unknown for character 0x45
38: In title(...) : font width unknown for character 0x66
39: In title(...) : font width unknown for character 0x66
40: In title(...) : font width unknown for character 0x69
41: In title(...) : font width unknown for character 0x63
42: In title(...) : font width unknown for character 0x69
43: In title(...) : font width unknown for character 0x65
44: In title(...) : font width unknown for character 0x6e
45: In title(...) : font width unknown for character 0x63
46: In title(...) : font width unknown for character 0x79
47: In title(...) : font width unknown for character 0x20
48: In title(...) : font width unknown for character 0x6f
49: In title(...) : font width unknown for character 0x66
50: In title(...) : font width unknown for character 0x20
> 

The output:

extrafont

FMKerckhof commented 6 years ago

I have the same issue using "Arial" with R3.4.4, extrafont 0.17 and extrafontdb 1.0 on a Win10 machine.

My devtools::session_info() is as follows:

Session info ------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.4 (2018-03-15)
 system   x86_64, mingw32             
 ui       RStudio (1.1.423)           
 language (EN)                        
 collate  Dutch_Belgium.1252          
 tz       Europe/Paris                
 date     2018-05-21                  

Packages ----------------------------------------------------------------------------------------------------------
 package     * version date       source        
 assertthat    0.2.0   2017-04-11 CRAN (R 3.4.1)
 base        * 3.4.4   2018-03-15 local         
 bindr         0.1.1   2018-03-13 CRAN (R 3.4.4)
 bindrcpp      0.2.2   2018-03-29 CRAN (R 3.4.4)
 cellranger    1.1.0   2016-07-27 CRAN (R 3.4.1)
 compiler      3.4.4   2018-03-15 local         
 datasets    * 3.4.4   2018-03-15 local         
 devtools      1.13.5  2018-02-18 CRAN (R 3.4.3)
 digest        0.6.15  2018-01-28 CRAN (R 3.4.3)
 dplyr       * 0.7.5   2018-05-19 CRAN (R 3.4.4)
 extrafont   * 0.17    2014-12-08 CRAN (R 3.4.0)
 extrafontdb   1.0     2012-06-11 CRAN (R 3.4.0)
 glue          1.2.0   2017-10-29 CRAN (R 3.4.2)
 graphics    * 3.4.4   2018-03-15 local         
 grDevices   * 3.4.4   2018-03-15 local         
 grofit      * 1.1.1-1 2014-10-06 CRAN (R 3.4.0)
 magrittr      1.5     2014-11-22 CRAN (R 3.4.1)
 memoise       1.1.0   2017-04-21 CRAN (R 3.4.1)
 methods     * 3.4.4   2018-03-15 local         
 minpack.lm  * 1.2-1   2016-11-20 CRAN (R 3.4.3)
 pillar        1.2.2   2018-04-26 CRAN (R 3.4.4)
 pkgconfig     2.0.1   2017-03-21 CRAN (R 3.4.1)
 purrr         0.2.4   2017-10-18 CRAN (R 3.4.4)
 R6            2.2.2   2017-06-17 CRAN (R 3.4.1)
 Rcpp          0.12.17 2018-05-18 CRAN (R 3.4.4)
 readxl      * 1.1.0   2018-04-20 CRAN (R 3.4.4)
 rlang         0.2.0   2018-02-20 CRAN (R 3.4.3)
 Rttf2pt1      1.3.6   2018-02-22 CRAN (R 3.4.3)
 stats       * 3.4.4   2018-03-15 local         
 tibble        1.4.2   2018-01-22 CRAN (R 3.4.3)
 tidyr       * 0.8.1   2018-05-18 CRAN (R 3.4.4)
 tidyselect    0.2.4   2018-02-26 CRAN (R 3.4.4)
 tools         3.4.4   2018-03-15 local         
 utils       * 3.4.4   2018-03-15 local         
 withr         2.1.2   2018-03-15 CRAN (R 3.4.4)
 yaml          2.1.19  2018-05-01 CRAN (R 3.4.4)

The code called to make extrafont work was:

extrafont::font_import()
extrafont::loadfonts(device="pdf")

When calling fonts() and fonttable() I can verify that I have "Arial" in the list. When calling a pdf device with family="Arial" I am getting warnings like above:

(...)
1: In strwidth(legend, units = "user", cex = cex, font = text.font) :
  font width unknown for character 0x74
2: In strwidth(legend, units = "user", cex = cex, font = text.font) :
  font width unknown for character 0x65
3: In strwidth(legend, units = "user", cex = cex, font = text.font) :
  font width unknown for character 0x73
4: In strwidth(legend, units = "user", cex = cex, font = text.font) :
  font width unknown for character 0x74
(...)
MichelineCampbell commented 3 years ago

I was having the same issue, but using

ggsave(device = cairo_pdf... fixed it (previously I was calling cairo_pdf(), which did printed to PDF but had overlapping titles like users above).

DonKMA commented 3 years ago

Thanks MichelineCampbell! That worked for me!

Cloudptj commented 1 year ago

I was having the same issue, but using

ggsave(device = cairo_pdf... fixed it (previously I was calling cairo_pdf(), which did printed to PDF but had overlapping titles like users above).

I have beening trying to solve this overlapping problem all day. Thanks a lot!