diff --git a/DESCRIPTION b/DESCRIPTION index 32a3833..a61f79f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: gridpattern Type: Package Title: 'grid' Pattern Grobs -Version: 1.1.0 +Version: 1.1.1 Authors@R: c(person("Mike", "FC", role = "aut", comment = "Code/docs adapted from ggpattern"), person("Trevor L", "Davis", role=c("aut", "cre"), email="trevor.l.davis@gmail.com", comment = c(ORCID = "0000-0001-6341-4639")), diff --git a/NEWS.md b/NEWS.md index 1523dd1..da45eac 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +gridpattern v1.1.1 +================== + +* "text" pattern example now skipped if ran within certain graphics devices like `pdf()` + that can't handle the playing cards Unicode glyphs (#68). + gridpattern v1.1.0 ================== diff --git a/R/pattern-both-text.R b/R/pattern-both-text.R index 4953705..3d19eb6 100644 --- a/R/pattern-both-text.R +++ b/R/pattern-both-text.R @@ -11,17 +11,19 @@ #' @param fontface The font face. See [grid::gpar()] for more details. #' @return A grid grob object invisibly. If `draw` is `TRUE` then also draws to the graphic device as a side effect. #' @examples -#' if (require("grid") && capabilities("png")) { -#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) -#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) +#' if (require("grid") && +#' capabilities("png") && +#' gridpattern:::device_supports_unicode()) { +#' x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) +#' y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) #' -#' playing_card_symbols <- c("\u2660", "\u2665", "\u2666", "\u2663") -#' grid.newpage() -#' grid.pattern_text(x_hex, y_hex, -#' shape = playing_card_symbols, -#' colour = c("black", "red", "red", "black"), -#' size = 18, spacing = 0.1, angle = 0) -#' } +#' playing_card_symbols <- c("\u2660", "\u2665", "\u2666", "\u2663") +#' grid.newpage() +#' grid.pattern_text(x_hex, y_hex, +#' shape = playing_card_symbols, +#' colour = c("black", "red", "red", "black"), +#' size = 18, spacing = 0.1, angle = 0) +#' } #' @export grid.pattern_text <- function(x = c(0, 0, 1, 1), y = c(1, 0, 0, 1), id = 1L, ..., colour = gp$col %||% "grey20", diff --git a/R/utils-misc.R b/R/utils-misc.R index bd0cf0f..8ae78a2 100644 --- a/R/utils-misc.R +++ b/R/utils-misc.R @@ -5,3 +5,29 @@ assert_suggested <- function(package, pattern) { i = glue('Install with the command `install.packages("{package}")`'))) } } + +# base R's Cairo/Quartz devices as well as {ragg} / {svglite} / {vdiffr} devices +# should support Unicode without complaint +# Notably `pdf()` is a device that does not... +# Any other devices to add? +device_supports_unicode <- function() { + device <- names(grDevices::dev.cur()) + if (device %in% c("agg_jpeg", "agg_ppm", "agg_png", "agg_tiff", # {ragg} + "devSVG", # {svglite} / {vdiffr} + "quartz", "quartz_off_screen", # Quartz + "cairo_pdf", "cairo_ps", "svg", "X11cairo") # Cairo + ) { + TRUE + } else if (device %in% c("bmp", "jpeg", "png", "tiff")) { + # on unix non-"cairo" type have different device names from "cairo" type + # but on Windows can't distinguish between `type = "windows"` or `type = "cairo"` + # Windows device doesn't support new patterns feature + if (getRversion() >= "4.2.0") { + "LinearGradient" %in% grDevices::dev.capabilities()$patterns + } else { + .Platform$OS.type == "unix" + } + } else { + FALSE + } +} diff --git a/cran-comments.md b/cran-comments.md index 0563eb6..3227b79 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,9 +1,10 @@ -* Example that triggered a "Examples with CPU time > 2.5 times elapsed time" NOTE - is now wrapped in `\donttest{}`. +* "text" pattern example now skipped if ran within certain graphics devices like `pdf()` + that can't handle the playing cards Unicode glyphs (#68). ## Test environments * local (linux, R 4.3.1) +* local (linux, R devel) * win-builder (windows, R devel) * github actions (linux, R oldrel) * github actions (linux, R release) diff --git a/man/grid.pattern_text.Rd b/man/grid.pattern_text.Rd index dccfea5..d2de5d7 100644 --- a/man/grid.pattern_text.Rd +++ b/man/grid.pattern_text.Rd @@ -120,15 +120,17 @@ A grid grob object invisibly. If \code{draw} is \code{TRUE} then also draws to \code{grid.pattern_text()} draws a text character pattern onto the graphic device. } \examples{ - if (require("grid") && capabilities("png")) { - x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) - y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) - - playing_card_symbols <- c("\u2660", "\u2665", "\u2666", "\u2663") - grid.newpage() - grid.pattern_text(x_hex, y_hex, - shape = playing_card_symbols, - colour = c("black", "red", "red", "black"), - size = 18, spacing = 0.1, angle = 0) - } +if (require("grid") && + capabilities("png") && + gridpattern:::device_supports_unicode()) { + x_hex <- 0.5 + 0.5 * cos(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) + y_hex <- 0.5 + 0.5 * sin(seq(2 * pi / 4, by = 2 * pi / 6, length.out = 6)) + + playing_card_symbols <- c("\u2660", "\u2665", "\u2666", "\u2663") + grid.newpage() + grid.pattern_text(x_hex, y_hex, + shape = playing_card_symbols, + colour = c("black", "red", "red", "black"), + size = 18, spacing = 0.1, angle = 0) +} }