Skip to content

Commit

Permalink
fix: Skip "text" pattern example in pdf()
Browse files Browse the repository at this point in the history
* "text" pattern example now skipped if ran within certain graphics devices like `pdf()`
  that can't handle the playing cards Unicode glyphs (#68).

closes #68
  • Loading branch information
trevorld committed Oct 25, 2023
1 parent ad61790 commit 90ec9cb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
==================

Expand Down
22 changes: 12 additions & 10 deletions R/pattern-both-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions R/utils-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 13 additions & 11 deletions man/grid.pattern_text.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90ec9cb

Please sign in to comment.