diff --git a/DESCRIPTION b/DESCRIPTION index 350b1b9..fcf3f2d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: lt Type: Package Title: Lightweight Tables via JSON Specs and JavaScript -Version: 0.1.6 +Version: 0.1.7 Authors@R: person('Yihui', 'Xie', email = 'xie@yihui.name', role = c('aut', 'cre', 'cph'), comment = c(ORCID = '0000-0003-0645-5666', URL = 'https://yihui.org')) Description: A lightweight grammar of tables. Build a table by declaring a JSON diff --git a/R/render.R b/R/render.R index 83370b5..2cda69d 100644 --- a/R/render.R +++ b/R/render.R @@ -320,7 +320,14 @@ lt_measure = function(html, pad, width = NULL, browser = NULL) { dom = with_temp_html(html, function(f) xfun::browser_dom(f, browser = browser)) m = regmatches(dom, regexec('data-ltw="([0-9]+)" data-lth="([0-9]+)"', dom))[[1]] if (length(m) != 3L) stop('Failed to measure the table dimensions.') - as.integer(m[-1L]) + d = as.integer(m[-1L]) + # scrollWidth is the floor of the table's fractional natural width. Pinning + # the body to that floored width leaves it a sub-pixel too narrow, so a cell + # wraps and the table grows taller than the measured height, spilling the + # PDF onto a second page (platform-dependent: seen on macOS, not Linux). Add + # 1px so the body is never narrower than the content and never re-wraps. + d[1L] = d[1L] + 1L + d } #' Export an lt table to a file diff --git a/tests/test-ci/test-js.R b/tests/test-ci/test-js.R index f98fa70..f6a0a9a 100644 --- a/tests/test-ci/test-js.R +++ b/tests/test-ci/test-js.R @@ -281,6 +281,22 @@ if (has_browser()) assert("lt_export() crops a large table to one PDF page", { (pdf_pages(pdf) %==% 1L) }) +# A wide table must crop to one page too. scrollWidth is the floor of the +# table's fractional natural width; pinning the body to that floored width +# used to leave it a sub-pixel too narrow, wrapping a cell and growing the +# table past the measured height, so the PDF spilled onto a second page +# (seen on macOS, not Linux). lt_measure() now rounds the width up by 1px. +if (has_browser()) assert("lt_export() crops a wide table to one PDF page", { + # Many columns with long labels give the table a fractional natural width. + x = lt(as.data.frame(matrix( + 1:32, 4, 8, dimnames = list(NULL, paste0("a_long_column_label_", 1:8)) + ))) + pdf = tempfile(fileext = ".pdf") + on.exit(unlink(pdf), add = TRUE) + lt_export(x, pdf) + (pdf_pages(pdf) %==% 1L) +}) + if (has_browser() && xfun::loadable("magick")) assert("lt_export() crops PNG tightly to the table size", { x = lt(head(mtcars))