Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with registering Roboto Condensed on macOS #18

Open
AntoniniP opened this issue May 6, 2017 · 17 comments
Open

Problems with registering Roboto Condensed on macOS #18

AntoniniP opened this issue May 6, 2017 · 17 comments

Comments

@AntoniniP
Copy link

I suggest integrating the following piece of code in the import_roboto_condensed() function, after running extrafont::font_import():

d <- read.csv(extrafont:::fonttable_file(), stringsAsFactors = FALSE)
d[grepl("Light", d$FontName), ]$FamilyName <- font_rc_light  # "Roboto Condensed Light"
write.csv(d, extrafont:::fonttable_file(), row.names = FALSE)

extrafont::loadfonts()

What the code does is to change the family of Roboto Condensed Light and Roboto Condensed Light Italic stored in the fonttable CSV file from Roboto Condensed to Roboto Condensed Light. This operation is needed, because otherwise extrafont::loadfonts() would detect duplicate fonts, and it would be impossible to export a plot (e.g., through ggsave()).

This solves problems similar to #2.

@hrbrmstr
Copy link
Owner

hrbrmstr commented May 6, 2017 via email

@AntoniniP
Copy link
Author

I see, and I understand that the behaviour is weird. I'll go through the passages I did and my understanding of the issue, in the hope this is of help. In the case I did anything wrong in the installation of the package, which in turn may generate the error, just let me know.

Not working

If I run the following lines to install and test the package, the plot is rendered correctly in a Quartz window, but it won't export to a PDF (or any other format). Notice in particular the output to extrafont::loadfonts().

> install.packages("extrafontdb")  # reset fonttable
# ...
> devtools::install_github("hrbrmstr/hrbrthemes", force = TRUE)
# ...
> hrbrthemes::import_roboto_condensed()
You will likely need to install these fonts on your system as well.

You can find them in [/Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed]
> extrafont::loadfonts()
More than one version of regular/bold/italic found for Roboto Condensed. Skipping setup for this font.
> library(hrbrthemes)
> library(ggplot2)
> ggplot(mtcars, aes(mpg, wt)) +
+   geom_point(aes(color=factor(carb))) +
+   labs(x="Fuel effiiency (mpg)", y="Weight (tons)",
+        title="Seminal ggplot2 scatterplot example",
+        subtitle="A plot that is only useful for demonstration purposes",
+        caption="Brought to you by the letter 'g'") + 
+   scale_color_ipsum() +
+   theme_ipsum_rc()
> ggsave("test.pdf")
Saving 7 x 7 in image
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Roboto Condensed' not found in PostScript font database
# ...
32: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font family 'Roboto Condensed Light' not found in PostScript font database
# ...
>

The current fonttable is as follows:

> extrafont:::fonttable()
  package                            afmfile
1      NA        RobotoCondensed-Bold.afm.gz
2      NA  RobotoCondensed-BoldItalic.afm.gz
3      NA      RobotoCondensed-Italic.afm.gz
4      NA       RobotoCondensed-Light.afm.gz
5      NA RobotoCondensed-LightItalic.afm.gz
6      NA     RobotoCondensed-Regular.afm.gz
                                                                                                                          fontfile
1        /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Bold.ttf
2  /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-BoldItalic.ttf
3      /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Italic.ttf
4       /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Light.ttf
5 /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-LightItalic.ttf
6     /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Regular.ttf
                       FullName       FamilyName                    FontName  Bold Italic Symbol afmsymfile
1         Roboto Condensed Bold Roboto Condensed        RobotoCondensed-Bold  TRUE  FALSE  FALSE         NA
2  Roboto Condensed Bold Italic Roboto Condensed  RobotoCondensed-BoldItalic  TRUE   TRUE  FALSE         NA
3       Roboto Condensed Italic Roboto Condensed      RobotoCondensed-Italic FALSE   TRUE  FALSE         NA
4        Roboto Condensed Light Roboto Condensed       RobotoCondensed-Light FALSE  FALSE  FALSE         NA
5 Roboto Condensed Light Italic Roboto Condensed RobotoCondensed-LightItalic FALSE   TRUE  FALSE         NA
6      Roboto Condensed Regular Roboto Condensed     RobotoCondensed-Regular FALSE  FALSE  FALSE         NA
> 

I understand that the problem stands in the fact that there is a conflict between Roboto Condensed Regular and Roboto Condensed Light, because they both are non-bold, non-italic fonts in the same family.

                       FullName       FamilyName                    FontName  Bold Italic
4        Roboto Condensed Light Roboto Condensed       RobotoCondensed-Light FALSE  FALSE
6      Roboto Condensed Regular Roboto Condensed     RobotoCondensed-Regular FALSE  FALSE

The same conflict exists between Roboto Condensed Italic and Roboto Condensed Light Italic, as they are non-bold, italic fonts in the same family.

Working

Instead, when I run the following, I am able to export correctly the plot.

> install.packages("extrafontdb")  # reset fonttable
# ...
> devtools::install_github("hrbrmstr/hrbrthemes", force = TRUE)
# ...
> hrbrthemes::import_roboto_condensed()
You will likely need to install these fonts on your system as well.

You can find them in [/Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed]
> library(hrbrthemes)
> # MY SUGGESTED CODE #####################################
> d <- read.csv(extrafont:::fonttable_file(), stringsAsFactors = FALSE)
> d[grepl("Light", d$FontName),]$FamilyName <- font_rc_light
> write.csv(d,extrafont:::fonttable_file(), row.names = FALSE)
> #########################################################
> extrafont::loadfonts()
Registering font with R using pdfFonts(): Roboto Condensed
Registering font with R using pdfFonts(): Roboto Condensed Light
> library(ggplot2)
> ggplot(mtcars, aes(mpg, wt)) +
+   geom_point(aes(color=factor(carb))) +
+   labs(x="Fuel effiiency (mpg)", y="Weight (tons)",
+        title="Seminal ggplot2 scatterplot example",
+        subtitle="A plot that is only useful for demonstration purposes",
+        caption="Brought to you by the letter 'g'") + 
+   scale_color_ipsum() +
+   theme_ipsum_rc()
> ggsave("test.pdf")
Saving 7 x 7 in image
> 

In the code above, extrafont::loadfonts() executes correctly, thus allowing the proper registration of Roboto Condensed and Roboto Condensed Light, which are now in two different font families and no longer conflicting:

> extrafont:::fonttable()
  package                            afmfile
1      NA        RobotoCondensed-Bold.afm.gz
2      NA  RobotoCondensed-BoldItalic.afm.gz
3      NA      RobotoCondensed-Italic.afm.gz
4      NA       RobotoCondensed-Light.afm.gz
5      NA RobotoCondensed-LightItalic.afm.gz
6      NA     RobotoCondensed-Regular.afm.gz
                                                                                                                          fontfile
1        /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Bold.ttf
2  /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-BoldItalic.ttf
3      /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Italic.ttf
4       /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Light.ttf
5 /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-LightItalic.ttf
6     /Library/Frameworks/R.framework/Versions/3.4/Resources/library/hrbrthemes/fonts/roboto-condensed/RobotoCondensed-Regular.ttf
                       FullName             FamilyName                    FontName  Bold Italic Symbol afmsymfile
1         Roboto Condensed Bold       Roboto Condensed        RobotoCondensed-Bold  TRUE  FALSE  FALSE         NA
2  Roboto Condensed Bold Italic       Roboto Condensed  RobotoCondensed-BoldItalic  TRUE   TRUE  FALSE         NA
3       Roboto Condensed Italic       Roboto Condensed      RobotoCondensed-Italic FALSE   TRUE  FALSE         NA
4        Roboto Condensed Light Roboto Condensed Light       RobotoCondensed-Light FALSE  FALSE  FALSE         NA
5 Roboto Condensed Light Italic Roboto Condensed Light RobotoCondensed-LightItalic FALSE   TRUE  FALSE         NA
6      Roboto Condensed Regular       Roboto Condensed     RobotoCondensed-Regular FALSE  FALSE  FALSE         NA
> 

Bottomline

Again, I understand that the behaviour of my installation is weird, also because I couldn't find almost any reference about similar situations on the web. I hope that this was of help for you; should you need any further information to understand and troubleshoot the problem, I'll be happy to help you.

In case I did anything wrong that prevents the correct installation of the package, just let me know.

@RoyalTS
Copy link

RoyalTS commented Jun 21, 2017

Can confirm this behavior and that the fix is working on Ubuntu 14.04.5

@cboettig
Copy link

Hmm... I cannot seem to get either Roboto Condensed (theme_ipsum_rc) or Arial Narrow (theme_ipsum) fonts working for PDF output; either on MacOS (High Sierra) or Debian stretch.

I appear to get the above behavior as @AntoniniP describes when first installing the font. After relabling as he recommends, I can run

extrafont::loadfonts()

and no longer get the message:

More than one version of regular/bold/italic found for Roboto Condensed. Skipping setup for this font.

but instead see that it sets up the pdf fonts:

Registering font with R using pdfFonts(): Roboto Condensed
Registering font with R using pdfFonts(): Roboto Condensed Light

Unfortunately, I still cannot export the image to PDF, though I have no trouble with png format. This gets me to a place where instead of the error on creating a PDF (also the same as @AntoniniP gets):

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
In addition: There were 50 or more warnings (use warnings() to see the first 50)

that things run without throwing any error; but the linux box results in a serifed font that is clearly not Roboto, while the Mac results in pdf figures that simply lack any text elements.

Color me confused.

Debian linux result I'm seeing should at least be pretty reproducible e.g. fire up a rocker/verse docker image running RStudio, apt-get install fonts-roboto and run the above example code...

@cboettig
Copy link

Looks like my problems with getting the wrong font but not getting an error might be related to the font not getting embedded into the PDF; so some pdf displays (Mac Preview) show it with no fonts, others (RStudio's built-in pdf preview) pull in some other font. Playing around with dev="CairoPDF" seems to help.

I forget the current status of default on Cairo graphics compiling with R, and rmarkdown/rstudio/knitr's preferred defaults for how it chooses pdf (vaguely recall it all being Cairo / vector pdf by default...). Will explore more.

@petetalbert
Copy link

Any headway on this problem? My Win 10 machine does all of the above listed problems. If @AntoniniP workaround it will print to PDF, but like @cboettig I get some other font pulled in.

@hrbrmstr
Copy link
Owner

hrbrmstr commented Jan 9, 2019

I'll see if I can get some time on my Windows VM to debug it better. (I realize I've said the following before) I literally use this every single day on Ubuntu and macOS and generate a ton of both PNGs and PDFs for our various internal departments and the fonts never fail to work.

@petetalbert
Copy link

Hi all,

Just an update that I've found for Win 10 users. When I went and looked in C:\Windows\Fonts it would show only one listing of Roboto. But when I went in with cmd, I noticed more versions (probably because I was accidentally installing it multiple times.) I deleted manually in cmd (running as administrator; DEL [options] [C:\Windows\Fonts\] files_to_delete).

Once I was sure all my versions of Roboto were deleted, I went into ~\R\R-3.5.2\library\hrbrthemes\fonts\roboto-condensed and manually installed only one version of Roboto Condensed. Then extrafonts doesn't get confused with what it sees as duplicates.

Threw + theme_ipsum_rc() on my ggplots and I'm set.

@OFish
Copy link

OFish commented Sep 16, 2019

Can also confirm this behaviour on OS 10.14.5 despite thinking I'd installed everything correctly. Used the workaround above which solved the issue. So thank you for that.

@amandeepjutla
Copy link

@AntoniniP's fix works for me as well on macOS 10.13.6.

@epgui
Copy link

epgui commented Nov 18, 2019

I am running into the same issue, I cannot get knitr to generate pdf output with hrbrthemes from this .Rmd file.

---
title: "blablabla..."
subtitle: "blablabla..."
author: "Author"
date: "`r format(Sys.time(), '%d %B %Y')`"
tags: [nothing, nothingness]
output: 
  pdf_document: 
    fig_caption: yes
    highlight: tango
    keep_tex: yes
    latex_engine: xelatex
    number_sections: yes
    toc: yes
    toc_depth: 6
mainfont: Minion Pro
sansfont: Helvetica Neue Light
monofont: Menlo
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  fig.width = 12,
  fig.height = 8,
  fig.path = 'Figs/',
  echo = FALSE,
  warning = FALSE,
  message = FALSE,
  dev = 'pdf'
)
```

\newpage

## Packages

```{r libraries, echo=TRUE}
library("ggplot2")

# Tidyverse stuff
library("dplyr")
library("tidyr")
library("purrr")

# Used to generate this annotated PDF report
library("knitr")

# https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf
library("kableExtra")

library("pander")

# Having to do this is a bit dumb, and also doesn't work
library("extrafont")
extrafont::font_import() # You may have to restart RStudio here
extrafont::loadfonts()
```

## SessionInfo()

This may be useful for troubleshooting and is included for the sake of reproducibility.

```{r environment, echo=TRUE, results="markup", include=TRUE, warning=TRUE, message=TRUE}

pander(
  sessionInfo(),
  locale = TRUE,
  compact = TRUE
)

```

\newpage

blablabla...

```{r publicationsPubmedAPI, echo=TRUE, results="markup", include=TRUE, warning=TRUE, message=TRUE}

getPubmedData <- function(yearList, searchTerms) {
  library("httr")
  library("xml2")
  endpoint <- "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?"
  dbString <- "db=pubmed"
  
  createSearchQueryTermsString <- function(terms, concatenator="+OR+") {
    concatTerms <- paste(terms, collapse = concatenator)
    groupedConcatTerms <- paste("(", concatTerms, ")", sep="")
    termString <- paste("term", groupedConcatTerms, sep="=")
    termString
  }
  
  getCount <- function(year, termString) {
    termYearString <- paste(termString, "+AND+", year, "[pdat]", sep="")
    query <- paste(endpoint, paste(dbString, termYearString, sep="&"), sep="")
    response <- query %>% read_xml() %>% xml_children()
    count <- response[1] %>% xml_text() %>% as.integer()
    count
  }
  
  df <- data.frame("year" = yearList)
  df$total <- df$year %>% map(function(year) {
    termString <- createSearchQueryTermsString(searchTerms)
    count <- getCount(year, termString)
    count
  }) %>% as.integer()
  
  for (term in searchTerms) {
    df[[term]] <- df$year %>% map(function(year) {
      termString <- paste("term=", term, sep="")
      count <- getCount(year, termString)
      count
    }) %>% as.integer()
  }
  
  df
}
```

\newpage

blablabla...

```{r publicationsPubmedPlot, echo=TRUE, results="markup", include=TRUE, warning=TRUE, message=TRUE}

plotPublicationsPerYear <- function(data) {
  library("reshape2")
  library("ggplot2")
  library("viridis")
  library("hrbrthemes")

  hrbrthemes::import_plex_sans() # Also doesn't seem to work because the following crashes
  
  data$total <- NULL
  data <- data %>% melt(id.vars = "year")
  
  data %>% ggplot(
    aes(
      x = year,
      y = value,
      fill = variable
    )
  ) + 
  labs(
    x="Year",
    y="Nombre d'articles publiés chaque année",
    fill="Requête",
    title="Intérêt grandissant du champ de recherche"
  ) +
  geom_area(
    alpha = 0.6 ,
    size = 0.2,
    colour="white"
  ) +
  scale_x_continuous(
    limits = c(1967, 2018),
    expand = c(0, 0),
    breaks = seq(1965, 2020, 5)
  ) +
  scale_y_continuous(
    limits = c(0, 4500),
    expand = c(0, 0),
    breaks = seq(0, 4500, 500)
  ) +
  scale_fill_viridis(
    discrete = TRUE
  ) +
  theme_ipsum_ps(
    grid="XY",
    axis="XY"
  )
}
```


\newpage
blablabla...

```{r publicationsPubmed, echo=TRUE, results="markup", include=TRUE, warning=TRUE, message=TRUE}

yearList <- seq(1960, 2019)

searchTerms = c(
  "ectosomes",
  "microparticles",
  "microvesicles",
  "exosomes"
)

data <- getPubmedData(yearList, searchTerms)
plotPublicationsPerYear(data) #This crashes

```

@Sumidu
Copy link

Sumidu commented Mar 13, 2020

adding + theme(plot.caption = element_text(family = "Roboto Condensed")) fixed the problem for me. :)

@genmeblog
Copy link

genmeblog commented Apr 16, 2020

The same issue under WLS1 with Ubuntu 18.04.4 LTS while rendering into PNG file. Workaround by OP works.

@petrbouchal
Copy link

petrbouchal commented May 3, 2020

FWIW, I just had this happen to me on a Mac - and yes, the Roboto Condensed Light font was registered as part of the Roboto Condensed family, creating an entry that looked duplicated to the device/grid/wherever the font selection happens.

The solution was to manually edit the fonttable CSV as per #18 (comment).

To see where this happened, I then cleared the extrafont db database and imported all Roboto fonts from my system library instead, with Roboto Condensed freshly downloaded from Google Fonts. Now, the Roboto Condensed Light font and its sibling showed up in their own family called "Roboto Condensed Light". Grid throws some errors about unknown font width, but after embedding the fonts using extrafont::embed_fonts() the PDF looks fine.

When I retried this with hrbrthemes - clearing extrafont DB again and calling hrbrthemes::import_roboto_condensed(), the issue reappeared.

So the solution might simply be replacing the font files in inst by newer ones from Google?

EDIT: the PDF created using the current font files from Google is not in fact fine - non-Latin characters (with diacritics) end up on top of each other. This does not happen using the font files currently in the pacakage. So the solution might be edit the fonttable CSV in hrbrthemes::import_roboto_condensed().

@hamedkhe
Copy link

Hello,
I have Mac and using the " theme_ft_rc()" for ggplot I got this error: "Error in In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : no font could be found for family "Roboto Condensed".
I followed above instructions but not worked for me. I directly installed Roboto Condensed family from the Google font website, https://fonts.google.com/specimen/Roboto+Condensed. After installing to my fontbook my issue resolved and i can use theme_ft_rc() without any issue.
I hope these steps work.

@josh-friedlander-kando
Copy link

I also got this error while running the example in a Jupyter notebook, even after installing the Roboto Condensed family. Works in the console, and OP's hack also works.

@martinlukk
Copy link

Hello, I have Mac and using the " theme_ft_rc()" for ggplot I got this error: "Error in In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : no font could be found for family "Roboto Condensed". I followed above instructions but not worked for me. I directly installed Roboto Condensed family from the Google font website, https://fonts.google.com/specimen/Roboto+Condensed. After installing to my fontbook my issue resolved and i can use theme_ft_rc() without any issue. I hope these steps work.

@AntoniniP's solution followed by @hamedkhe's (I.e., installing the fonts directly from Google) works in 2023 on macOS 13.2.1 too.

It might work to combine these solutions as well, i.e., installing fonts from https://fonts.google.com/specimen/Roboto+Condensed (rather than the package files) right after import_roboto_condensed(), but I haven't tried this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests