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

Whitespace font kerning problem with fresh device #37

Open
jmw86069 opened this issue Nov 26, 2024 · 0 comments
Open

Whitespace font kerning problem with fresh device #37

jmw86069 opened this issue Nov 26, 2024 · 0 comments

Comments

@jmw86069
Copy link

Love the package, love your work. Big fan. ahem

I noticed "sometimes" I had problems with text labels, compressing or expanding whitespace between words.
I also saw some issues here and on ggtext.

I may have found a reproducible test case!

My question: How can I track it down?

TL;DR

My spark: I noticed that calling gridtext::richtext_grob() would open a new graphics device if not already open.

I think the symptoms below are caused when the first graphics device is opened.
If that device opens "as expected by gridtext", font/kerning calculations are fine.
If that device opens "unexpectedly" then font/kerning is incorrect.
It seems to be cached.

It doesn't appear to happen with grid::textGrob().

Scenario 1.

Using gridtext within RStudio, or R console when the graphics device is already open.

grob1 <- gridtext::richtext_grob(
   x=0.5, y=0.8,
   name="test_gridtext1",
   text="Set Name Goes Here<br>A",
   gp=grid::gpar(
      fontfamily="Arial",
      fontsize=16))
grid::grid.newpage()
grid::grid.draw(grob1)
image

Falalalala lala la la. It looks great.

Scenario 2.

Using gridtext on R console when the graphics device is not open.
(Same R code.)
image
(Admittedly, the wider spacing isn't super obvious, I promise it gets obvious.)

Scenario 3.

Using gridtext in an RMarkdown, with dev=c("png", "cairo_pdf")
(Same R code, but inside an RMarkdown.)
image
^^ Much more obvious.
This is the cool one, because it shows the whitespace isn't just incorrect, it's also inconsistent! Aha.

Also for RMarkdown:
Using dev=c("png", "cairo_pdf") causes both the PNG and PDF files to have the exact same weird spacing shown above.
In fact, using any combination of multiple output dev devices causes the same weird spacing to appear in all of them.
However, using any one dev such as dev="png" or dev="cairo_pdf" the font spacing seems fine... weirdly one exception is dev="jpeg" - that's always incorrect spacing. (I think rmarkdown:render() secretly adds "png" only for this case.)

Also when I try grid::textGrob() it always calculates proper spacing.
To be fair, it isn't doing the same work as richtext_grob(). But it isn't likely a grid issue.

Questions

Do you or others see the same behavior?
(Is it just me?)

Why and when does gridtext open a graphical device?
Maybe some call to par()?

Does gridtext cache the font metrics somewhere?
I think yes.

  • If I try Scenario 2 first, with no device open upfront, it creates the wrong spacing. If I close dev.off() then run dev.new() and repeat, in the same R session, it keeps using the same wrong spacing consistently.
  • If I try Scenario 1, open dev.new() upfront, then create the grob, the spacing is normal. If I close and open a new device, spacing is consistently normal in that R session.
  • The above are moot for rendering RMarkdown, since it creates its own session, or something.

Can it be fixed?

Session Info
### Session info 
 setting  value
 version  R version 4.3.3 (2024-02-29)
 os       macOS Sonoma 14.6.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2024-11-26
 pandoc   2.19.2 @ /Users/wardjm/mambaforge/bin/pandoc

### Packages 
 package     * version  date (UTC) lib source
 cli           3.6.3    2024-06-21 [1] CRAN (R 4.3.3)
 clipr         0.8.0    2022-02-22 [1] CRAN (R 4.3.0)
 commonmark    1.9.2    2024-10-04 [1] CRAN (R 4.3.3)
 glue          1.8.0    2024-09-30 [1] CRAN (R 4.3.3)
 gridtext      0.1.5    2022-09-16 [1] CRAN (R 4.3.0)
 lifecycle     1.0.4    2023-11-07 [1] CRAN (R 4.3.1)
 magrittr      2.0.3    2022-03-30 [1] CRAN (R 4.3.0)
 markdown      1.13     2024-06-04 [1] CRAN (R 4.3.3)
 marquee       0.1.0    2024-05-28 [1] CRAN (R 4.3.3)
 Rcpp          1.0.13-1 2024-11-02 [1] CRAN (R 4.3.3)
 rlang         1.1.4    2024-06-04 [1] CRAN (R 4.3.3)
 sessioninfo   1.2.2    2021-12-06 [1] CRAN (R 4.3.0)
 stringi       1.8.4    2024-05-06 [1] CRAN (R 4.3.1)
 stringr       1.5.1    2023-11-14 [1] CRAN (R 4.3.1)
 vctrs         0.6.5    2023-12-01 [1] CRAN (R 4.3.1)
 xfun          0.49     2024-10-31 [1] CRAN (R 4.3.3)
 xml2          1.3.6    2023-12-04 [1] CRAN (R 4.3.1)

 [1] /Users/wardjm/Library/R/arm64/4.3/library
 [2] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
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

1 participant