Skip to content

Commit f5a88a7

Browse files
authored
Memoise descentDetails (#2993)
1 parent 868fdb7 commit f5a88a7

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

DESCRIPTION

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Depends:
2020
R (>= 3.1)
2121
Imports:
2222
digest,
23+
grDevices,
2324
grid,
2425
gtable (>= 0.1.1),
2526
lazyeval,
@@ -39,23 +40,23 @@ Suggests:
3940
ggplot2movies,
4041
hexbin,
4142
Hmisc,
43+
knitr,
4244
lattice,
4345
mapproj,
4446
maps,
4547
maptools,
4648
multcomp,
4749
munsell,
4850
nlme,
49-
testthat (>= 0.11.0),
50-
vdiffr,
51+
profvis,
5152
quantreg,
52-
knitr,
5353
rgeos,
54-
rpart,
5554
rmarkdown,
55+
rpart,
5656
sf (>= 0.3-4),
5757
svglite (>= 1.2.0.9001),
58-
profvis
58+
testthat (>= 0.11.0),
59+
vdiffr
5960
Enhances: sp
6061
License: GPL-2 | file LICENSE
6162
URL: http://ggplot2.tidyverse.org, https://github.com/tidyverse/ggplot2

R/margins.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(),
6363
# has the common letters with descenders. This guarantees that the grob always has
6464
# the same height regardless of whether the text actually contains letters with
6565
# descenders or not. The same happens automatically with ascenders already.
66-
temp <- editGrob(text_grob, label = "gjpqyQ")
67-
descent <- descentDetails(temp)
66+
descent <- font_descent(gp$fontfamily, gp$fontface, gp$fontsize, gp$cex)
6867

6968
# Use trigonometry to calculate grobheight and width for rotated grobs. This is only
7069
# exactly correct when vjust = 1. We need to take the absolute value so we don't make
@@ -329,3 +328,24 @@ rotate_just <- function(angle, hjust, vjust) {
329328

330329
list(hjust = hnew, vjust = vnew)
331330
}
331+
descent_cache <- new.env(parent = emptyenv())
332+
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
333+
cur_dev <- names(grDevices::dev.cur())
334+
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)
335+
336+
descent <- descent_cache[[key]]
337+
338+
if (is.null(descent)) {
339+
descent <- descentDetails(textGrob(
340+
label = "gjpqyQ",
341+
gp = gpar(
342+
fontsize = size,
343+
cex = cex,
344+
fontfamily = family,
345+
fontface = face
346+
)
347+
))
348+
descent_cache[[key]] <- descent
349+
}
350+
descent
351+
}

vignettes/profiling.Rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ To keep track of changes focused on improving the performance of gtable they
5555
are summarised below:
5656

5757
### v`r packageVersion('ggplot2')`
58+
59+
- **Caching of calls to `grid::descentDetails()`** The absolute biggest offender
60+
was the construction of titles. In recent versions this has included calls to
61+
`grid::descentDetails()` to ensure that they are aligned across plots, but
62+
this is quite heavy. These calls are now cached so they only have to be
63+
calculated once per font setting.

0 commit comments

Comments
 (0)