Skip to content

Commit

Permalink
Merge pull request #25 from netique/master
Browse files Browse the repository at this point in the history
Solve #24 – use `font` gpar instead of `fontface` to work with R 4.2.0
  • Loading branch information
clauswilke authored Apr 22, 2022
2 parents 6d6c03e + 13f28b7 commit 6192174
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 5 additions & 5 deletions R/drawing-context.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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
font_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(font_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(font_old == 3)) {
fontface <- "bold.italic"
}
}
Expand Down
22 changes: 11 additions & 11 deletions R/text-details.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' 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).
#' `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))
Expand All @@ -14,11 +14,11 @@
#' @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 {
Expand All @@ -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)) {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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]]

Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6192174

Please sign in to comment.