Skip to content

Commit

Permalink
Fixed crash on very small fill areas.
Browse files Browse the repository at this point in the history
  • Loading branch information
dansmith01 committed Jun 24, 2024
1 parent 2d612b8 commit 73dc497
Show file tree
Hide file tree
Showing 256 changed files with 567 additions and 456 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: fillpattern
Type: Package
Title: Patterned Fills for 'ggplot2' and 'grid' Graphics
Version: 1.0.1
Date: 2024-03-09
Version: 1.0.2
Date: 2024-06-24
Authors@R: c(
person("Daniel P.", "Smith", , "dansmith01@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-2479-2044")),
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import(grid)
importFrom(ggplot2, pattern_alpha)
importFrom(methods, formalArgs)
importFrom(methods, formalArgs, is)
importFrom(utils, head, tail)

export(fill_pattern)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# fillpattern 1.0.2

* Fixed crash on very small fill areas.
* New parameter `min_size` to use solid fills for small areas.


# fillpattern 1.0.1

* Allow installation on R 4.1 or later (for conda-forge compatibility).
Expand Down
58 changes: 35 additions & 23 deletions R/fill_pattern.r
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@
#' before rendering. Should accept two parameters: `env`, an
#' environment that the function should modify, and `row`, the row of
#' transformed data that ggbuild has constructed for this grob
#' (including aes mappings). The return value is not used.
#' Default: `NULL`
#' (including aes mappings). The function should return a gTree or an
#' error to force returning from the parent function immediately, or
#' `NULL` to continue processing with the updated `env`. Default: `NULL`
#'
#' @param min_size Minimum size of the pattern to draw. Applies to both width
#' and height. Useful for avoiding CPU and memory overhead on tiny
#' graphical elements. Assumed to be millimeters unless set otherwise
#' with [unit()]. Default: `2`
#'
#'
#' @details `fillPatternGrob()` expects a single value for each parameter.
Expand Down Expand Up @@ -109,7 +115,8 @@

fill_pattern <- function (
patterns = "brick", fg = "black", bg = "transparent",
angle = 0, width = 5, height = NA, lwd = 1, lty = "solid", fun = NULL ) {
angle = 0, width = 5, height = NA, lwd = 1, lty = "solid", fun = NULL,
min_size = 2 ) {

n <- length(patterns)
if (n == 0) return (list())
Expand All @@ -132,15 +139,16 @@ fill_pattern <- function (
grid::pattern(
group = FALSE,
grob = fillPatternGrob(
pattern = get_i(patterns, i),
fg = get_i(fg, i),
bg = get_i(bg, i),
angle = get_i(angle, i),
width = get_i(width, i),
height = get_i(height, i),
lwd = get_i(lwd, i),
lty = get_i(lty, i),
fun = get_i(fun, i) ))
pattern = get_i(patterns, i),
fg = get_i(fg, i),
bg = get_i(bg, i),
angle = get_i(angle, i),
width = get_i(width, i),
height = get_i(height, i),
lwd = get_i(lwd, i),
lty = get_i(lty, i),
fun = get_i(fun, i),
min_size = get_i(min_size, i) ))
})


Expand All @@ -160,23 +168,27 @@ fill_pattern <- function (
#' @export
fillPatternGrob <- function (
pattern = "brick", fg = "black", bg = "transparent",
angle = 0, width = 5, height = NA, lwd = 1, lty = "solid", fun = NULL ) {
angle = 0, width = 5, height = NA, lwd = 1, lty = "solid", fun = NULL,
min_size = 2 ) {


for (i in setdiff(formalArgs(fillPatternGrob), 'fun'))
if (length(val <- get(i, inherits = FALSE)) != 1)
stop("`", i, "` should be one value, not ", length(val), ".")

if (!is.unit(min_size)) min_size <- unit(min_size, 'mm')

grid::gTree(
pattern = pattern,
fg = fg,
bg = bg,
angle = angle,
width = width,
height = height,
lwd = lwd,
lty = lty,
fun = fun,
cl = "fill_pattern" )
pattern = pattern,
fg = fg,
bg = bg,
angle = angle,
width = width,
height = height,
lwd = lwd,
lty = lty,
fun = fun,
min_size = min_size,
cl = "fill_pattern" )
}

Loading

0 comments on commit 73dc497

Please sign in to comment.