From 655ef2b6de24f060679cba06255ef6814582ce40 Mon Sep 17 00:00:00 2001 From: Claus Wilke Date: Mon, 19 Aug 2019 20:53:49 -0500 Subject: [PATCH 1/5] make font_descent() not vectorized. closes #3492. --- R/margins.R | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/R/margins.R b/R/margins.R index b85f37a5fe..c321ea9d98 100644 --- a/R/margins.R +++ b/R/margins.R @@ -333,29 +333,36 @@ rotate_just <- function(angle, hjust, vjust) { list(hjust = hnew, vjust = vnew) } descent_cache <- new.env(parent = emptyenv()) +# Important: This function is not vectorized. Do not use to look up multiple +# font descents at once. font_descent <- function(family = "", face = "plain", size = 12, cex = 1) { cur_dev <- names(grDevices::dev.cur()) + if (cur_dev == "null device") { + cache <- FALSE # don't cache if no device open + } else { + cache <- TRUE + } key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex) - descents <- lapply(key, function(k) { - descent <- descent_cache[[k]] - - if (is.null(descent)) { - descent <- convertHeight(grobDescent(textGrob( - label = "gjpqyQ", - gp = gpar( - fontsize = size, - cex = cex, - fontfamily = family, - fontface = face - ) - )), 'inches') - descent_cache[[k]] <- descent + # we only look up the first result; this function is not vectorized + key <- key[1] + + descent <- descent_cache[[key]] + + if (is.null(descent)) { + descent <- convertHeight(grobDescent(textGrob( + label = "gjpqyQ", + gp = gpar( + fontsize = size, + cex = cex, + fontfamily = family, + fontface = face + ) + )), 'inches') + + if (cache) { + descent_cache[[key]] <- descent } - descent - }) - if (length(descents) == 1) { - descents[[1]] - } else { - do.call(unit.c, descents) } + + descent } From d817fb9cb5507cbd3f3d2fcf54fead1b23719eeb Mon Sep 17 00:00:00 2001 From: Claus Wilke Date: Tue, 20 Aug 2019 01:34:09 -0500 Subject: [PATCH 2/5] work around vectorized angles --- R/margins.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/margins.R b/R/margins.R index c321ea9d98..e3382a1601 100644 --- a/R/margins.R +++ b/R/margins.R @@ -71,8 +71,8 @@ title_spec <- function(label, x, y, hjust, vjust, angle, gp = gpar(), # 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 # the grob smaller when it's flipped over. - text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle / 180 * pi)) * descent - text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle / 180 * pi)) * descent + text_height <- unit(1, "grobheight", text_grob) + abs(cos(angle[1] / 180 * pi)) * descent + text_width <- unit(1, "grobwidth", text_grob) + abs(sin(angle[1] / 180 * pi)) * descent if (isTRUE(debug)) { children <- gList( From 1bce62e99046bb7d58562ab7f959b6b7158a33ff Mon Sep 17 00:00:00 2001 From: Claus Wilke Date: Sun, 25 Aug 2019 16:13:18 -0500 Subject: [PATCH 3/5] Add warning when element_text() is used with vectorized input. --- R/theme-elements.r | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/theme-elements.r b/R/theme-elements.r index f2cdd5c0fa..316c35a1f4 100644 --- a/R/theme-elements.r +++ b/R/theme-elements.r @@ -112,6 +112,20 @@ element_text <- function(family = NULL, face = NULL, colour = NULL, color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE) { if (!is.null(color)) colour <- color + + n <- max( + length(family), length(face), length(colour), length(size), + length(hjust), length(vjust), length(angle), length(lineheight) + ) + if (n > 1) { + warning( + "Vectorized input to `element_text()` is not officially supported.\n", + "Results may be unexpected or may change in future versions of ggplot2.", + call. = FALSE + ) + } + + structure( list(family = family, face = face, colour = colour, size = size, hjust = hjust, vjust = vjust, angle = angle, lineheight = lineheight, From e97c64bf49338a00907f6d9cf617e225dfd2b411 Mon Sep 17 00:00:00 2001 From: Claus Wilke Date: Mon, 26 Aug 2019 10:28:04 -0500 Subject: [PATCH 4/5] add news item --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 384a50b645..b26e66e87c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # ggplot2 (development version) +* `element_text()` now issues a warning when vectorized arguments are provided, as in + `colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported. + * stacking text when calculating the labels and the y axis with `stat_summary()` now works (@ikosmidis, #2709) From 64540961ece1e3e73a3d9bb68cb96f3667d8ed74 Mon Sep 17 00:00:00 2001 From: Claus Wilke Date: Mon, 26 Aug 2019 10:29:03 -0500 Subject: [PATCH 5/5] add username and issue number to news item. --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index b26e66e87c..e927165029 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,8 @@ # ggplot2 (development version) * `element_text()` now issues a warning when vectorized arguments are provided, as in - `colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported. + `colour = c("red", "green", "blue")`. Such use is discouraged and not officially supported + (@clauswilke, #3492). * stacking text when calculating the labels and the y axis with `stat_summary()` now works (@ikosmidis, #2709)