Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* Saving a high-resolution plot now uses the correct text layout after drawing
a low-resolution preview (@NaumanRafay, #6929).

* Updates for compatibility with S7 > 0.2.2.

* Faceting by a named vector no longer partially matches the `names` attribute
Expand Down
11 changes: 6 additions & 5 deletions R/margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,13 @@ descent_cache <- new.env(parent = emptyenv())
# font descents at once.
font_descent <- function(family = "", face = "plain", size = 12, cex = 1) {
cur_dev <- names(grDevices::dev.cur())
if (cur_dev == "null device") {
cache <- FALSE # don't cache if no device open
} else {
cache <- TRUE
cache <- cur_dev != "null device"
if (cache) {
# Raster devices can measure the same font differently at different DPIs.
dpi <- grDevices::dev.size("px") / grDevices::dev.size("in")
cur_dev <- paste(cur_dev, dpi[1], dpi[2], sep = ":")
}
key <- paste0(cur_dev, ':', family, ':', face, ":", size, ":", cex)
key <- paste(cur_dev, family, face, size, cex, sep = ":")
# we only look up the first result; this function is not vectorized
key <- key[1]

Expand Down
16 changes: 14 additions & 2 deletions tests/testthat/test-guide-.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ test_that("plotting does not induce state changes in guides", {
geom_point() +
guides

snapshot <- serialize(as.list(p@guides), NULL)
# Preserve S7 class references while copying the mutable guide state.
# Serializing these references also copies installation-specific environments.
class_refs <- list()
snapshot <- serialize(as.list(p@guides), NULL, refhook = function(x) {
if (!inherits(x, "S7_class_ref")) {
return(NULL)
}
class_refs[[length(class_refs) + 1L]] <<- x
as.character(length(class_refs))
})

grob <- ggplotGrob(p)

expect_identical(as.list(p@guides), unserialize(snapshot))
expect_identical(
as.list(p@guides),
unserialize(snapshot, refhook = function(x) class_refs[[as.integer(x)]])
)
})

test_that("adding guides doesn't change plot state", {
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ test_that("titleGrob() and margins() work correctly", {
expect_equal(width_cm(g10), width_cm(g1) + 2)
})

test_that("a low-resolution preview does not change a high-resolution export", {
skip_if_not_installed("ragg")

plot <- ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(title = "Title gjpqy")

export <- function(preview) {
local_mocked_bindings(descent_cache = new.env(parent = emptyenv()))
if (preview) {
ggsave(
withr::local_tempfile(fileext = ".png"),
plot,
device = ragg::agg_png,
width = 4,
height = 3,
dpi = 72
)
}

file <- withr::local_tempfile(fileext = ".png")
ggsave(file, plot, device = ragg::agg_png, width = 4, height = 3, dpi = 300)
readBin(file, "raw", n = file.size(file))
}

expect_true(identical(export(preview = TRUE), export(preview = FALSE)))
})

test_that("margins() warn against wrong input lengths", {
expect_snapshot(margin(c(1, 2), 3, 4, c(5, 6, 7)))
})
Expand Down
Loading