Skip to content
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 8 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test-ci/test-js.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading