diff --git a/NAMESPACE b/NAMESPACE index 03d2141f..16ccbd6d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,10 +10,12 @@ S3method(.raster_magick,Layer) S3method(.raster_magick,QuadLayout) S3method(.raster_magick,StackLayout) S3method(.raster_magick,default) +S3method(.raster_magick,ggalignRasterMagick) S3method(.raster_magick,ggalign_plot) S3method(.raster_magick,ggplot) S3method(.raster_magick,grob) S3method(.raster_magick,list) +S3method(.raster_magick,zeroGrob) S3method(align_melt_facet,"NULL") S3method(align_melt_facet,FacetGrid) S3method(align_melt_facet,FacetNull) @@ -223,7 +225,7 @@ S3method(link_to_location,list) S3method(link_to_location,waiver) S3method(makeContent,ggalignLinkGrob) S3method(makeContent,ggalignMarkGtable) -S3method(makeContext,ggalign_raster_magick) +S3method(makeContent,ggalignRasterMagick) S3method(make_wrap,alignpatches) S3method(make_wrap,ggplot) S3method(make_wrap,patch_ggplot) @@ -541,6 +543,7 @@ importFrom(grid,convertWidth) importFrom(grid,editGrob) importFrom(grid,gpar) importFrom(grid,grid.draw) +importFrom(grid,grob) importFrom(grid,grobHeight) importFrom(grid,grobName) importFrom(grid,grobWidth) @@ -548,7 +551,6 @@ importFrom(grid,heightDetails) importFrom(grid,is.grob) importFrom(grid,is.unit) importFrom(grid,makeContent) -importFrom(grid,makeContext) importFrom(grid,unit) importFrom(grid,unit.c) importFrom(grid,unitType) diff --git a/R/raster-magick.R b/R/raster-magick.R index 5536126c..98201f09 100644 --- a/R/raster-magick.R +++ b/R/raster-magick.R @@ -94,19 +94,32 @@ raster_magick <- function(x, magick = NULL, ..., x } +#' @importFrom grid grob #' @export .raster_magick.grob <- function(x, magick = NULL, ..., res = NULL, interpolate = FALSE) { - if (inherits(x, "zeroGrob")) { - return(x) - } - x$ggalign_raster_magick <- list( - magick = magick, res = res, - interpolate = interpolate + grob( + grob = x, + magick = magick, + res = res, + interpolate = interpolate, + cl = "ggalignRasterMagick" ) - add_class(x, "ggalign_raster_magick") } +#' @export +.raster_magick.ggalignRasterMagick <- function(x, magick = NULL, ..., + res = NULL, + interpolate = FALSE) { + x["magick"] <- list(magick) + x["res"] <- list(res) + x["interpolate"] <- list(interpolate) + x +} + +#' @export +.raster_magick.zeroGrob <- function(x, magick = NULL, ...) x + #' @export .raster_magick.default <- function(x, magick = NULL, ...) x @@ -119,20 +132,19 @@ raster_magick <- function(x, magick = NULL, ..., # postDraw: # - postDrawDetails: by default, do noting # - popgrobvp -#' @importFrom grid makeContext unit convertHeight convertWidth viewport +#' @importFrom grid makeContent unit convertHeight convertWidth viewport #' @export -makeContext.ggalign_raster_magick <- function(x) { +makeContent.ggalignRasterMagick <- function(x) { # Grab viewport information width <- convertWidth(unit(1, "npc"), "pt", valueOnly = TRUE) height <- convertHeight(unit(1, "npc"), "pt", valueOnly = TRUE) # Grab grob metadata - params <- .subset2(x, "ggalign_raster_magick") plot_res <- convertWidth(unit(1, "inch"), "pt", valueOnly = TRUE) - res <- .subset2(params, "res") %||% plot_res + res <- .subset2(x, "res") %||% plot_res - magick <- .subset2(params, "magick") - interpolate <- .subset2(params, "interpolate") + magick <- .subset2(x, "magick") + interpolate <- .subset2(x, "interpolate") # Track current device old_dev <- grDevices::dev.cur() @@ -152,9 +164,7 @@ makeContext.ggalign_raster_magick <- function(x) { grid::pushViewport(viewport()) # Clean up the grob for rendering - x <- remove_class(x, "ggalign_raster_magick") - x$ggalign_raster_magick <- NULL - grid::grid.draw(x) # should respect the viewport of `x` + grid::grid.draw(.subset2(x, "grob")) # should respect the viewport of `x` grid::popViewport() grDevices::dev.off() on.exit(magick::image_destroy(image), add = TRUE)