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

Svglite device and custom fonts inside knitr #2272

Closed
3 tasks done
Darxor opened this issue Jul 25, 2023 · 1 comment · Fixed by #2273
Closed
3 tasks done

Svglite device and custom fonts inside knitr #2272

Darxor opened this issue Jul 25, 2023 · 1 comment · Fixed by #2273
Assignees

Comments

@Darxor
Copy link
Contributor

Darxor commented Jul 25, 2023

Using custom fonts with svglite device (with {systemfonts}, rather than loading them via {extrafont}) leads to issues:

  • Charts throw warnings like:
Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : font family 'Montserrat' not found in PostScript font database
  • Some text manipulations, like those done by ggtext::geom_richtext produce unexpected results.

Example

In my example I used Montserrat font (can be installed as system font from google fonts).

An .rmd like one bellow will produce warnings as one above and text in the chart will be rendered incorrectly.

---
title: "svglite reprex"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = TRUE,
  dev = "svglite",
  fig.height = 4L,
  fig.width = 8L
)
```

## Example 
Systemfonts successfully finds the font.
```{r font_exists}
systemfonts::match_font("Montserrat")
```

```{r chart}
library(ggplot2)
chart_data <- data.frame(
  x = -2L:2L,
  y = -2L:2L,
  label = "example text here"
)
ggplot(chart_data, aes(x, y)) +
  ggtext::geom_richtext(
    aes(label = label),
    size = 3L,
    family = "Montserrat"
  )
```

Produced chart:
chart-1
Note: this chart will be absolutely fine when rendered with svglite directly.

Proposed solution

The issue is caused by using a pdf device at the time of calling evaluate() in eng_r(), if chunk device was set to svglite.
A change to chunk_device() to support svglite device seems to fix this and produces a report with no warnings and no visual issues. I will provide a PR for this.

chart-2

Issue is reproducible to me under macOS and Ubuntu

Session info
r$> xfun::session_info('knitr')
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.4.1, RStudio 0


Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

time zone: Europe/Moscow
tzcode source: internal

Package version:
  evaluate_0.21   graphics_4.3.1  grDevices_4.3.1 highr_0.10      knitr_1.43      methods_4.3.1   stats_4.3.1     tools_4.3.1     utils_4.3.1     xfun_0.39       yaml_2.3.7 
---

By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • I have provided the necessary information about my issue.
    • If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants