test_that("stamp_logo_png preserves image dimensions and returns the path invisibly", { p <- tempfile(fileext = ".png") on.exit(unlink(p), add = TRUE) grDevices::png(p, width = 600, height = 240); plot(1:5); grDevices::dev.off() in_dim <- dim(png::readPNG(p))[1:2] expect_invisible(out <- stamp_logo_png(p)) expect_identical(out, p) expect_identical(dim(png::readPNG(p))[1:2], in_dim) # overwritten at same size }) test_that("stamp_logo_png runs for every type/variant/position combination", { p <- tempfile(fileext = ".png") on.exit(unlink(p), add = TRUE) grDevices::png(p, width = 500, height = 200); plot(1:5); grDevices::dev.off() for (ty in c("wordmark", "mark")) { for (va in c("light", "dark")) { for (pos in c("bottom-right", "bottom-left", "top-right", "top-left")) { expect_no_error(stamp_logo_png(p, type = ty, variant = va, position = pos)) } } } }) test_that("stamp_logo_png validates its inputs", { expect_error(stamp_logo_png(tempfile(fileext = ".png"))) # file does not exist # 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) } }) # --- add_logo_ga() layout (#28) ---------------------------------------------- test_that("add_logo_ga reserves a logo band instead of using negative margins", { logo <- make_logo_grob() composed <- add_logo_ga(list(caption_plot(), caption_plot()), logo, 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("add_logo_ga does not clip captions at several canvas sizes", { skip_on_cran() logo <- make_logo_grob() for (nrow in c(1, 2)) { for (w in c(8, 11, 16, 24)) { composed <- suppressWarnings( add_logo_ga(list(caption_plot(), caption_plot()), logo, nrow = nrow) ) ink <- bottom_ink(composed, w, w * 0.64) expect_equal(ink, 0, info = paste("nrow", nrow, "width", w)) } } })