Skip to content

Memoise descent #2993

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

Merged
merged 18 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Depends:
R (>= 3.1)
Imports:
digest,
grDevices,
grid,
gtable (>= 0.1.1),
lazyeval,
Expand All @@ -39,23 +40,23 @@ Suggests:
ggplot2movies,
hexbin,
Hmisc,
knitr,
lattice,
mapproj,
maps,
maptools,
multcomp,
munsell,
nlme,
testthat (>= 0.11.0),
vdiffr,
profvis,
quantreg,
knitr,
rgeos,
rpart,
rmarkdown,
rpart,
sf (>= 0.3-4),
svglite (>= 1.2.0.9001),
profvis
testthat (>= 0.11.0),
vdiffr
Enhances: sp
License: GPL-2 | file LICENSE
URL: http://ggplot2.tidyverse.org, https://github.com/tidyverse/ggplot2
Expand Down
24 changes: 22 additions & 2 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(),
# has the common letters with descenders. This guarantees that the grob always has
# the same height regardless of whether the text actually contains letters with
# descenders or not. The same happens automatically with ascenders already.
temp <- editGrob(text_grob, label = "gjpqyQ")
descent <- descentDetails(temp)
descent <- font_descent(gp$fontfamily, gp$fontface, gp$fontsize, gp$cex)

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

list(hjust = hnew, vjust = vnew)
}
descent_cache <- new.env(parent = emptyenv())
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
cur_dev <- names(grDevices::dev.cur())
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)

descent <- descent_cache[[key]]

if (is.null(descent)) {
descent <- descentDetails(textGrob(
label = "gjpqyQ",
gp = gpar(
fontsize = size,
cex = cex,
fontfamily = family,
fontface = face
)
))
descent_cache[[key]] <- descent
}
descent
}
6 changes: 6 additions & 0 deletions vignettes/profiling.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ To keep track of changes focused on improving the performance of gtable they
are summarised below:

### v`r packageVersion('ggplot2')`

- **Caching of calls to `grid::descentDetails()`** The absolute biggest offender
was the construction of titles. In recent versions this has included calls to
`grid::descentDetails()` to ensure that they are aligned across plots, but
this is quite heavy. These calls are now cached so they only have to be
calculated once per font setting.