From ef5f1aa607003905defe941bd7dd69a1c9527081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Net=C3=ADk?= Date: Thu, 21 Apr 2022 23:44:30 +0200 Subject: [PATCH 1/5] use font instead of fontface --- R/text-details.R | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/R/text-details.R b/R/text-details.R index 85778bb..2465012 100644 --- a/R/text-details.R +++ b/R/text-details.R @@ -2,23 +2,23 @@ #' #' Calculate text details for a given text label #' @param label Character vector containing the label. Can handle only one label at a time. -#' @param gp Grid graphical parameters defining the font (`fontfamily`, `fontface`, and -#' `fontface` should be defined). +#' @param gp Grid graphical parameters defining the font (`fontfamily`, `font`, and +#' `fontsize` should be defined). #' @examples -#' text_details("Hello world!", grid::gpar(fontfamily = "", fontface = "plain", fontsize = 12)) -#' text_details("Hello world!", grid::gpar(fontfamily = "", fontface = "plain", fontsize = 24)) +#' text_details("Hello world!", grid::gpar(fontfamily = "", font = "plain", fontsize = 12)) +#' text_details("Hello world!", grid::gpar(fontfamily = "", font = "plain", fontsize = 24)) #' text_details( #' "Hello world\nwith newline", -#' grid::gpar(fontfamily = "", fontface = "plain", fontsize = 12) +#' grid::gpar(fontfamily = "", font = "plain", fontsize = 12) #' ) #' @noRd text_details <- function(label, gp = gpar()) { fontfamily <- gp$fontfamily %||% grid::get.gpar("fontfamily")$fontfamily - fontface <- gp$fontface %||% grid::get.gpar("fontface")$fontface + font <- gp$font %||% grid::get.gpar("font")$font fontsize <- gp$fontsize %||% grid::get.gpar("fontsize")$fontsize devname <- names(grDevices::dev.cur()) - fontkey <- paste0(devname, fontfamily, fontface, fontsize) + fontkey <- paste0(devname, fontfamily, font, fontsize) if (devname == "null device") { cache <- FALSE # don't cache if no device open } else { @@ -30,16 +30,16 @@ text_details <- function(label, gp = gpar()) { } # ascent and width depend on label and font - l1 <- text_info(label, fontkey, fontfamily, fontface, fontsize, cache) + l1 <- text_info(label, fontkey, fontfamily, font, fontsize, cache) # descent and space width depend only on font - l2 <- font_info(fontkey, fontfamily, fontface, fontsize, cache) + l2 <- font_info(fontkey, fontfamily, font, fontsize, cache) # concatenate, result is a list with four members, width_pt, ascent_pt, descent_pt, space_pt c(l1, l2) } font_info_cache <- new.env(parent = emptyenv()) -font_info <- function(fontkey, fontfamily, fontface, fontsize, cache) { +font_info <- function(fontkey, fontfamily, font, fontsize, cache) { info <- font_info_cache[[fontkey]] if (is.null(info)) { @@ -48,7 +48,7 @@ font_info <- function(fontkey, fontfamily, fontface, fontsize, cache) { gp = gpar( fontsize = fontsize, fontfamily = fontfamily, - fontface = fontface, + font = font, cex = 1 ) )), "pt", valueOnly = TRUE) @@ -58,7 +58,7 @@ font_info <- function(fontkey, fontfamily, fontface, fontsize, cache) { gp = gpar( fontsize = fontsize, fontfamily = fontfamily, - fontface = fontface, + font = font, cex = 1 ) )), "pt", valueOnly = TRUE) @@ -73,7 +73,7 @@ font_info <- function(fontkey, fontfamily, fontface, fontsize, cache) { } text_info_cache <- new.env(parent = emptyenv()) -text_info <- function(label, fontkey, fontfamily, fontface, fontsize, cache) { +text_info <- function(label, fontkey, fontfamily, font, fontsize, cache) { key <- paste0(label, fontkey) info <- text_info_cache[[key]] @@ -83,7 +83,7 @@ text_info <- function(label, fontkey, fontfamily, fontface, fontsize, cache) { gp = gpar( fontsize = fontsize, fontfamily = fontfamily, - fontface = fontface, + font = font, cex = 1 ) )), "pt", valueOnly = TRUE) @@ -93,7 +93,7 @@ text_info <- function(label, fontkey, fontfamily, fontface, fontsize, cache) { gp = gpar( fontsize = fontsize, fontfamily = fontfamily, - fontface = fontface, + font = font, cex = 1 ) )), "pt", valueOnly = TRUE) From bad26077522e59444df54049e3d181976b546ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Net=C3=ADk?= Date: Thu, 21 Apr 2022 23:49:55 +0200 Subject: [PATCH 2/5] remove fontface, not font (if it should exist) when updating gpar --- R/drawing-context.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/drawing-context.R b/R/drawing-context.R index 4bbcb58..15bffa8 100644 --- a/R/drawing-context.R +++ b/R/drawing-context.R @@ -46,8 +46,8 @@ set_style <- function(drawing_context, style = NULL) { update_gpar <- function(gp, gp_new) { names_new <- names(gp_new) names_old <- names(gp) - gp[c(intersect(names_old, names_new), "font")] <- NULL - gp_new["font"] <- NULL + gp[c(intersect(names_old, names_new), "fontface")] <- NULL + gp_new["fontface"] <- NULL do.call(gpar, c(gp, gp_new)) } From b410f9747ce56e8b783f0d2e26535108f51630fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Net=C3=ADk?= Date: Thu, 21 Apr 2022 23:51:09 +0200 Subject: [PATCH 3/5] retrieve font, not fontface in set_context_fontface, change fontface char to font int --- R/drawing-context.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/drawing-context.R b/R/drawing-context.R index 15bffa8..2900d47 100644 --- a/R/drawing-context.R +++ b/R/drawing-context.R @@ -70,13 +70,13 @@ set_context_gp <- function(drawing_context, gp = NULL) { # update the fontface of a drawing context set_context_fontface <- function(drawing_context, fontface = "plain", overwrite = FALSE) { - fontface_old <- drawing_context$gp$fontface + fontface_old <- drawing_context$gp$font # combine bold and italic if needed if (!isTRUE(overwrite)) { - if (isTRUE(fontface == "italic") && isTRUE(fontface_old == "bold")) { + if (isTRUE(fontface == "italic") && isTRUE(fontface_old == 2)) { # see ?grid::gpar for fontface codes fontface <- "bold.italic" - } else if (isTRUE(fontface == "bold") && isTRUE(fontface_old == "italic")) { + } else if (isTRUE(fontface == "bold") && isTRUE(fontface_old == 3)) { fontface <- "bold.italic" } } From 82a0b13dca59454756ff99cc1548c9307f2a93c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Net=C3=ADk?= Date: Fri, 22 Apr 2022 20:44:45 +0200 Subject: [PATCH 4/5] accept proposed changes from review --- R/drawing-context.R | 6 +++--- R/text-details.R | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/drawing-context.R b/R/drawing-context.R index 2900d47..02389a2 100644 --- a/R/drawing-context.R +++ b/R/drawing-context.R @@ -70,13 +70,13 @@ set_context_gp <- function(drawing_context, gp = NULL) { # update the fontface of a drawing context set_context_fontface <- function(drawing_context, fontface = "plain", overwrite = FALSE) { - fontface_old <- drawing_context$gp$font + font_old <- drawing_context$gp$font # combine bold and italic if needed if (!isTRUE(overwrite)) { - if (isTRUE(fontface == "italic") && isTRUE(fontface_old == 2)) { # see ?grid::gpar for fontface codes + if (isTRUE(fontface == "italic") && isTRUE(font_old == 2)) { # see ?grid::gpar for fontface codes fontface <- "bold.italic" - } else if (isTRUE(fontface == "bold") && isTRUE(fontface_old == 3)) { + } else if (isTRUE(fontface == "bold") && isTRUE(font_old == 3)) { fontface <- "bold.italic" } } diff --git a/R/text-details.R b/R/text-details.R index 2465012..50cd6f6 100644 --- a/R/text-details.R +++ b/R/text-details.R @@ -2,14 +2,14 @@ #' #' Calculate text details for a given text label #' @param label Character vector containing the label. Can handle only one label at a time. -#' @param gp Grid graphical parameters defining the font (`fontfamily`, `font`, and +#' @param gp Grid graphical parameters defining the font (`fontfamily`, `fontface`, and #' `fontsize` should be defined). #' @examples -#' text_details("Hello world!", grid::gpar(fontfamily = "", font = "plain", fontsize = 12)) -#' text_details("Hello world!", grid::gpar(fontfamily = "", font = "plain", fontsize = 24)) +#' text_details("Hello world!", grid::gpar(fontfamily = "", fontface = "plain", fontsize = 12)) +#' text_details("Hello world!", grid::gpar(fontfamily = "", fontface = "plain", fontsize = 24)) #' text_details( #' "Hello world\nwith newline", -#' grid::gpar(fontfamily = "", font = "plain", fontsize = 12) +#' grid::gpar(fontfamily = "", fontface = "plain", fontsize = 12) #' ) #' @noRd text_details <- function(label, gp = gpar()) { From 13f28b7b6a89e2f566e80ec17f861c84b53a040f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Net=C3=ADk?= Date: Fri, 22 Apr 2022 20:52:37 +0200 Subject: [PATCH 5/5] add NEWS bullet --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 0e1ebf1..b199af4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # gridtext 0.1.4.9000 - Transition to curl package and drop RCurl dependency +- Fix fontface not being processed and words spaced properly in R 4.2.0 # gridtext 0.1.4