Closed kbzsl closed 3 years ago
Cambria isn't a Google Font, so thematic doesn't know how to automatically install/register it https://rstudio.github.io/thematic/articles/fonts.html#google-fonts
Instead, you'll have to provide the font files directly to either sysfonts::font_add()
or systemfonts::register_font()
https://rstudio.github.io/thematic/articles/fonts.html#other-fonts
Hi @cpsievert,
The recommended is not working, because family name already exists (according to the systemfonts):
library(tidyverse)
library(systemfonts)
system_fonts() %>%
filter(str_detect(name, "Consolas"))
#> # A tibble: 4 x 9
#> path index name family style weight width italic monospace
#>
#> 1 "C:\\WINDOWS\\F~ 0 Consolas Conso~ Regular normal norm~ FALSE TRUE
#> 2 "C:\\WINDOWS\\F~ 0 Consolas-~ Conso~ Bold bold norm~ FALSE TRUE
#> 3 "C:\\WINDOWS\\F~ 0 Consolas-~ Conso~ Bold I~ bold norm~ TRUE TRUE
#> 4 "C:\\WINDOWS\\F~ 0 Consolas-~ Conso~ Italic normal norm~ TRUE TRUE
register_font(name = "Consolas",
"C:\\WINDOWS\\Fonts\\consola.ttf")
#> Error: A system font with that family name already exists
On the other hand, (as I wrote) when I keep the blisb
definition in the YAML
header, and I remove just the one line of the thematic
, this Rmd is knitted perfectly to the html. What is the reason why this code is working with the bslib
, but not working when the thematic
is added?
Thank you.
The warning is about Cambria, not Consolas (that font appears to be already installed and registered with R on your system).
What is the reason why this code is working with the bslib , but not working when the thematic is added?
HTML/CSS (i.e., bslib) fonts are rendered by the web browser, not the R graphics device.
Hi @cpsievert,
Sorry, you are right. I way trying many different fonts (installed by default on windows), and regardless the font the result was the same. The systemfonts recognize, Cambria, too.
library(tidyverse)
library(systemfonts)
system_fonts() %>%
filter(str_detect(name, "Cambria"))
#> # A tibble: 5 x 9
#> path index name family style weight width italic monospace
#>
#> 1 "C:\\WINDOWS\\~ 0 Cambria Cambria Regul~ normal norm~ FALSE FALSE
#> 2 "C:\\WINDOWS\\~ 1 CambriaMa~ Cambria~ Regul~ normal norm~ FALSE FALSE
#> 3 "C:\\WINDOWS\\~ 0 Cambria-B~ Cambria Bold bold norm~ FALSE FALSE
#> 4 "C:\\WINDOWS\\~ 0 Cambria-B~ Cambria Bold ~ bold norm~ TRUE FALSE
#> 5 "C:\\WINDOWS\\~ 0 Cambria-I~ Cambria Italic normal norm~ TRUE FALSE
register_font(name = "Cambria",
"C:\\WINDOWS\\Fonts\\cambria.ttc")
#> Error: A system font with that family name already exists
Sorry for my wording, I was referring to the plots. It’s a bit strange when having only the bslib definition the plots are rendered correctly (ragg_png), but when the thematic definition is added the plots are not rendered anymore correctly (nor Cambria, nor Consolas...).
Thank you.
when the thematic definition is added the plots are not rendered anymore correctly
Have you tried using these fonts without thematic? If neither of these examples work, the problem resides in ragg/showtext, not thematic.
library(ragg)
agg_png("ragg.png")
plot(1, family = "Cambria")
dev.off()
file.show("ragg.png")
library(showtext)
png("showtext.png")
showtext_begin()
plot(1, family = "Cambria")
dev.off()
file.show("showtext.png")
Hi @cpsievert,
Yes, as I wrote, when I comment out the single line containing thematic definition, the Rmd file is rendered correctly, both the text body (which is rendered in fact by the browser) and all the plots are containing the expected fonts. Is it possible to share the rendered htmls?
Running the code snippets that you provided, the showtext version is generating a lot of warning for the font (sans is used instead). The ragg version is running without any error, but is not rendered correctly (axis text is missing).
Thank you.
Hi @cpsievert
I am trying to rephrase my issue and add extra sreenshots to explain my findings/concerns. It might happen that I misunderstood something in the documentation or other materials, but still I cannot find an explanation, nor solution.
This time, I am using the Lucida fonts, which is an easily distinguishable font family (and which is installed by default on most of window machines).
There are two Rmd files, the only difference is that the thematic::thematic_rmd()
line is commented out in the version without thematic
.
The first version (_custom_font_test_withbslib.Rmd) includes only bslib
definitions. The rendered results are ok: the fonts in the text and code parts are ok (this part is rendered by the browser); and all the plots (the title and subtitle part) are rendered using the right fonts.
After adding the thematic
definition (_custom_font_test_with_bslibthematic.Rmd) I run into problems.
I expected that the thematic will propagate the font settings (base_font: "Lucida Handwriting"
) in case of the first plot, where the font family is not explicitly set - but this not happened.
The next two plots, where the font families were explicitly set, are not rendered correctly and there are font not found warnings: _Warning in grid.Call(CstringMetric, as.graphicsAnnot(x$label)): font family - but this was working fine previously, and now the fonts which are know to the systemfonts
are not "recognized" anymore. For this reason I am notifying: @thomasp85
Thank you.
custom_font_test_with_bslib.Rmd
---
title: "custom font test using local fonts"
subtitle: "with bslib"
output:
html_document:
theme:
version: 4
base_font: "Lucida Handwriting"
code_font: "Lucida Console"
---
```{r setup, message=FALSE}
# thematic::thematic_rmd()
knitr::opts_chunk$set(dev = "ragg_png",
fig.width = 5, fig.height = 1, dpi = 300)
library(tidyverse)
library(systemfonts)
library(ragg)
font_test <- function(font_family = NULL){
ggplot(NULL) +
ggtitle("The quick brown fox jumps over the lazy dog",
subtitle = "01234567890987654321012345678909876543210") +
theme_classic(base_family = font_family)
}
```
ggplot rendered using the **default** font
```{r}
font_test()
```
ggplot rendered using the **Lucida Handwriting** font
```{r}
system_fonts() |>
filter(str_detect(family, "Lucida Handwriting")) |>
glimpse()
font_test("Lucida Handwriting")
```
ggplot rendered using the **Lucida Console** font
```{r}
system_fonts() |>
filter(str_detect(family, "Lucida Console")) |>
glimpse()
font_test("Lucida Console")
```
custom_font_test_with_bslib_thematic.Rmd
---
title: "custom font test using local fonts"
subtitle: "with bslib and thematic"
output:
html_document:
theme:
version: 4
base_font: "Lucida Handwriting"
code_font: "Lucida Console"
---
```{r setup, message=FALSE}
thematic::thematic_rmd()
knitr::opts_chunk$set(dev = "ragg_png",
fig.width = 5, fig.height = 1, dpi = 300)
library(tidyverse)
library(systemfonts)
library(ragg)
font_test <- function(font_family = NULL){
ggplot(NULL) +
ggtitle("The quick brown fox jumps over the lazy dog",
subtitle = "01234567890987654321012345678909876543210") +
theme_classic(base_family = font_family)
}
```
ggplot rendered using the **default** font
```{r}
font_test()
```
ggplot rendered using the **Lucida Handwriting** font
```{r}
system_fonts() |>
filter(str_detect(family, "Lucida Handwriting")) |>
glimpse()
font_test("Lucida Handwriting")
```
ggplot rendered using the **Lucida Console** font
```{r}
system_fonts() |>
filter(str_detect(family, "Lucida Console")) |>
glimpse()
font_test("Lucida Console")
```
I was trying to define custom fonts with bslib + thematic. It looks that the thematic is not working with ragg/systemfonts when knitting the html document (on a windows machine). I am receiving the following warnings, even though the
knitr::opts_chunk$set(dev = "ragg_png")
was setThe pixel peeping (extracting the plot from the html documents) suggests also that not the ragg graphic device was used when knitting this html document. On the other hand, when removing the thematic definition (
thematic::thematic_rmd(font = "auto")
), the document was knitted correctly.Rmd file:
Session Info