Skip to content

fix: with_pattern() handles a plain list of patterns as fill - #144

Merged
trevorld merged 1 commit into
mainfrom
fix/plain-pattern-list-fill
Nov 23, 2026
Merged

trevorld merged 1 commit into
mainfrom
fix/plain-pattern-list-fill

Conversation

@trevorld

Copy link
Copy Markdown
Owner

gpar(fill = list(<patterns>)) gives the fill grid's (unexported) "GridPatternList" class, but code that sets grob$gp$fill <- list(<patterns>) directly ends up with a plain list. Grid draws both the same (it re-validates the gp at draw time), but {gridpattern} checked the class directly:

  • has_grouped_pattern_fill() didn't see a plain list of grouped gradients as grouped, so with_pattern() filled each shape separately (each gradient relative to its own shape).
  • shape_gps() handled a plain list of two or more patterns correctly only by accident, and didn't unwrap a one-element plain list.

This came up via {ggpattern}'s with_pattern() tracing (trevorld/ggpattern#174), which restores list fills (gradients) directly into grob$gp$fill.

Changes

  • New internal is_pattern_list(): a non-empty list whose elements are all <GridPattern> objects (classed or plain). A bare is.list() would be too broad because gpar() also keeps list col / lty values.
  • has_grouped_pattern_fill() and shape_gps() use it. The two branches of shape_gps() merge into one, since rep_len() already drops the "GridPatternList" class.
  • Tests for plain-list fills (grouped and group = FALSE, including a one-element list) and for one-element list col / lty being left as they are.
  • Dev version bump to 1.6.0-18. No NEWS entry, because with_pattern() is unreleased.

Repro

library(grid); library(gridpattern)
g <- linearGradient(c("red", "white"))
draw <- function(r) { f <- tempfile(fileext = ".png"); png(f, 200, 100); grid.newpage(); grid.draw(r); dev.off(); png::readPNG(f) }
r1 <- rectGrob(x = c(.25, .75), width = .4, gp = gpar(fill = list(g, g)))
r2 <- rectGrob(x = c(.25, .75), width = .4, gp = gpar(fill = c("#000001", "#000002"))); r2$gp$fill <- list(g, g)
identical(draw(r1), draw(r2))  # TRUE
identical(draw(with_pattern(r1, pattern = "stripe")), draw(with_pattern(r2, pattern = "stripe")))  # was FALSE, now TRUE

🤖 Generated with Claude Code

* A list of grid patterns assigned directly to `grob$gp$fill`
  (i.e. without the "GridPatternList" class that `gpar()` adds)
  is now treated the same as one set via `gpar()`,
  matching how grid itself draws it.
  In particular a plain list of grouped gradients is now seen as relative to all the shapes
  (so they are no longer filled separately, each gradient relative to its own shape)
  and a one-element plain list gives each shape the bare pattern.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@trevorld
trevorld merged commit cb1922f into main Nov 23, 2026
4 checks passed
@trevorld
trevorld deleted the fix/plain-pattern-list-fill branch November 23, 2026 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant