Skip to content

Commit

Permalink
Fix #4 as well as do precalc for makeContext/makeContent if the width…
Browse files Browse the repository at this point in the history
…s are absolute/NA
  • Loading branch information
thomasp85 committed Apr 23, 2024
1 parent 0fad5f1 commit dbf176b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ S3method(format,marquee_style)
S3method(format,marquee_style_set)
S3method(heightDetails,textboxgrob)
S3method(makeContent,marquee)
S3method(makeContent,marquee_precalculated)
S3method(makeContent,svg_grob)
S3method(makeContext,marquee)
S3method(makeContext,marquee_precalculated)
S3method(print,marquee_box)
S3method(print,marquee_em)
S3method(print,marquee_relative)
Expand Down
34 changes: 33 additions & 1 deletion R/grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,18 @@ marquee_grob <- function(text, style, x = 0, y = 1, width = NULL, default.units
cli::cli_abort("{.arg vjust} must either be numeric or a character vector")
}

gTree(
grob <- gTree(
text = parsed, bullets = bullets, images = images, x = rep_along(text, x),
y = rep_along(text, y), width = rep_along(text, width),
hjust = rep_along(text, hjust), vjust = rep_along(text, vjust),
angle = rep_along(text, angle), vp = vp, name = name, cl = "marquee"
)
# Check if we can do all calculations upfront
if (all(is.na(grob$width) | unitType(absolute.size(grob$width)) != "null")) {
grob <- makeContent.marquee(makeContext.marquee(grob))
class(grob) <- c("marquee_precalculated", class(grob))
}
grob
}

#' @export
Expand Down Expand Up @@ -324,6 +330,26 @@ makeContext.marquee <- function(x) {
space_before = 0,
space_after = 0
)

# If any widths are not defined we grab the text widths from the shaping and start again
if (anyNA(widths)) {
added_width <- rowSums(x$text[block_starts, c("padding_left", "padding_right", "margin_left", "margin_right")])
## We go back from the most indented to the least and compound the widths
for (i in rev(seq_len(max(block_indent)))) {
blocks <- which(block_indent == i)
widths[blocks] <- vapply(blocks, function(j) {
k <- which(block_starts > block_starts[j] & block_starts <= x$text$ends[block_starts[j]])
max(shape$metrics$width[j], widths[j], widths[k] + added_width[k], na.rm = TRUE)
}, numeric(1))
}
if (anyNA(widths)) {
cli::cli_abort("Could not resolve width of text")
}
x$width <- unit(widths[block_indent == 1], "bigpts")
## Restart evaluation
return(makeContext.marquee(x))
}

# Inherit color and id from parsed text
shape$shape$col <- x$text$color[shape$shape$string_id]
shape$shape$id <- x$text$id[shape$shape$string_id]
Expand Down Expand Up @@ -618,6 +644,9 @@ makeContext.marquee <- function(x) {
x
}

#' @export
makeContext.marquee_precalculated <- function(x) x

#' @export
heightDetails.textboxgrob <- function(x) {
x$full_height
Expand Down Expand Up @@ -784,3 +813,6 @@ makeContent.marquee <- function(x) {

setChildren(x, inject(gList(!!!grobs)))
}

#' @export
makeContent.marquee_precalculated <- function(x) x
37 changes: 34 additions & 3 deletions man/marquee_parse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dbf176b

Please sign in to comment.