diff --git a/R/decorate_grob.R b/R/decorate_grob.R index b0ef4ff5a0..a86eb0e6ef 100644 --- a/R/decorate_grob.R +++ b/R/decorate_grob.R @@ -319,10 +319,17 @@ split_text_grob <- function(text, horizontal_npc_width_no_gp <- grid::convertWidth( grid::grobWidth( grid::textGrob( - paste0(text, collapse = "\n"))), "npc", valueOnly = TRUE) + paste0(text, collapse = "\n") + ) + ), "npc", + valueOnly = TRUE + ) horizontal_npc_width_with_gp <- grid::convertWidth(grid::grobWidth( grid::textGrob( - paste0(text, collapse = "\n"), gp = gp)), "npc", valueOnly = TRUE) + paste0(text, collapse = "\n"), + gp = gp + ) + ), "npc", valueOnly = TRUE) # Adapting width to the input gpar (it is normalized so does not matter what is text) width <- width * horizontal_npc_width_no_gp / horizontal_npc_width_with_gp diff --git a/tests/testthat/test-decorate_grob.R b/tests/testthat/test-decorate_grob.R index 8c221af35b..429e90e740 100644 --- a/tests/testthat/test-decorate_grob.R +++ b/tests/testthat/test-decorate_grob.R @@ -152,12 +152,13 @@ testthat::test_that("Wrapping works consistently", { collapse = " " ) subtitles <- c("something\nwith\\n", "", "and such") - out <- split_text_grob(c(titles, subtitles), x = 0, y = 1, - just = c("left", "top"), - width = grid::unit(11.63, "inches") - grid::unit(1.5, "cm"), - vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1), - gp = grid::gpar() - ) + out <- split_text_grob(c(titles, subtitles), + x = 0, y = 1, + just = c("left", "top"), + width = grid::unit(11.63, "inches") - grid::unit(1.5, "cm"), + vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1), + gp = grid::gpar() + ) expect_equal( nchar(strsplit(out$label, "\n")[[1]]),