Skip to content

Commit

Permalink
use makeContent instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunuuuu committed Dec 31, 2024
1 parent 79049d7 commit 57b9ae7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
6 changes: 4 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -541,14 +543,14 @@ 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)
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)
Expand Down
42 changes: 26 additions & 16 deletions R/raster-magick.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 57b9ae7

Please sign in to comment.