fix(logo): reserve a logo band, keep patchwork panels, and preserve logo aspect (#20)
The logo gets its own band (logo_height inches) instead of negative margins, so captions are no longer clipped. Patchwork compositions go through patchworkGrob() so every panel survives. font_scale defaults to 1. make_logo_grob() sizes by height so the logo keeps its shape on any canvas. Closes #20 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d6e3ba2fb4
commit
0561df4221
6 files changed
+171
-72
No files matched your search
@@ -28,3 +28,86 @@ test_that("stamp_logo_png validates its inputs", {
|
||||
# invalid type is rejected by match.arg() before the file is touched
|
||||
expect_error(stamp_logo_png(tempfile(fileext = ".png"), type = "banner"))
|
||||
})
|
||||
|
||||
# --- civilytics_logo() layout (#20) ------------------------------------------
|
||||
|
||||
caption_plot <- function() {
|
||||
ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
|
||||
ggplot2::geom_point() +
|
||||
ggplot2::labs(title = "Title",
|
||||
caption = "Source: a reasonably long attribution line, 2021-22\nSecond line") +
|
||||
theme_civilytics()
|
||||
}
|
||||
|
||||
# The composed grob is a gtable, wrapped in a grobTree when the plot has a
|
||||
# background fill.
|
||||
composed_table <- function(composed) {
|
||||
if (inherits(composed, "gtable")) composed else composed$children[[2]]
|
||||
}
|
||||
|
||||
bottom_ink <- function(grob, width, height, rows = 2) {
|
||||
path <- tempfile(fileext = ".png")
|
||||
grDevices::png(path, width = width, height = height, units = "in", res = 100)
|
||||
grid::grid.newpage()
|
||||
grid::grid.draw(grob)
|
||||
grDevices::dev.off()
|
||||
img <- png::readPNG(path)
|
||||
n <- dim(img)[1]
|
||||
edge <- img[(n - rows + 1):n, , 1:3, drop = FALSE]
|
||||
sum(edge < 0.5)
|
||||
}
|
||||
|
||||
test_that("font_scale defaults to 1 everywhere", {
|
||||
expect_identical(formals(add_logo)$font_scale, 1)
|
||||
expect_identical(formals(add_logo_ga)$font_scale, 1)
|
||||
expect_identical(formals(civilytics_logo)$font_scale, 1)
|
||||
})
|
||||
|
||||
test_that("the logo band is reserved space that sums to the canvas", {
|
||||
p <- caption_plot()
|
||||
for (pos in c("bottom-right", "top-left")) {
|
||||
composed <- suppressWarnings(civilytics_logo(p, position = pos, logo_height = 0.6))
|
||||
heights <- composed_table(composed)$heights
|
||||
expect_equal(length(heights), 2)
|
||||
expect_true(any(grepl("null", as.character(heights))))
|
||||
expect_true(any(grepl("0.6", as.character(heights))))
|
||||
}
|
||||
})
|
||||
|
||||
test_that("a caption is not clipped at the bottom edge at several canvas sizes", {
|
||||
skip_on_cran()
|
||||
p <- caption_plot()
|
||||
for (w in c(8, 11, 16, 24)) {
|
||||
h <- w * 0.64
|
||||
ink <- bottom_ink(suppressWarnings(civilytics_logo(p, position = "top-right")), w, h)
|
||||
expect_equal(ink, 0, info = paste("width", w))
|
||||
}
|
||||
})
|
||||
|
||||
test_that("patchwork compositions keep every panel", {
|
||||
skip_if_not_installed("patchwork")
|
||||
p1 <- ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) + ggplot2::geom_point()
|
||||
p2 <- ggplot2::ggplot(mtcars, ggplot2::aes(hp, wt)) + ggplot2::geom_point()
|
||||
pw <- p1 + p2 + patchwork::plot_annotation(title = "Both panels")
|
||||
composed <- suppressWarnings(civilytics_logo(pw))
|
||||
panel_grob <- composed_table(composed)$grobs[[1]]
|
||||
n_panels <- sum(grepl("^panel", panel_grob$layout$name))
|
||||
expect_gte(n_panels, 2)
|
||||
})
|
||||
|
||||
test_that("the logo keeps its aspect ratio at any canvas width (#20)", {
|
||||
skip_if_not_installed("ragg")
|
||||
ink_aspect <- function(width, type) {
|
||||
path <- tempfile(fileext = ".png")
|
||||
ragg::agg_png(path, width = width, height = 0.5, units = "in", res = 200,
|
||||
background = "transparent")
|
||||
grid::grid.draw(ggplot2::ggplotGrob(make_logo_grob(type = type)))
|
||||
grDevices::dev.off()
|
||||
ink <- which(png::readPNG(path)[, , 4] > 0.05, arr.ind = TRUE)
|
||||
diff(range(ink[, 2])) / diff(range(ink[, 1]))
|
||||
}
|
||||
for (type in c("wordmark", "mark")) {
|
||||
aspects <- vapply(c(6, 11, 24), ink_aspect, numeric(1), type = type)
|
||||
expect_lt(diff(range(aspects)) / mean(aspects), 0.03, label = type)
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user