Skip to content
Open
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
9 changes: 8 additions & 1 deletion R/process-tags.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
process_text <- function(node, drawing_context) {
tokens <- stringr::str_split(stringr::str_squish(node), "[[:space:]]+")[[1]]
# regex: [:space:] set-minus \p{Line_Break=Glue}
re_whitespace_non_glue <- "[[:space:]-[\\p{Line_Break=Glue}]]+"
re_whitespace_non_glue_trim <- paste0("^", re_whitespace_non_glue,
"|",
re_whitespace_non_glue, "$")
node_squish_internal <- stringr::str_replace_all(node, re_whitespace_non_glue, " ")
node_squish_trim <- stringr::str_replace_all(node_squish_internal, re_whitespace_non_glue_trim, "")
tokens <- stringr::str_split(node_squish_trim, re_whitespace_non_glue)[[1]]

# make interior boxes
boxes <- lapply(tokens,
Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/test-textbox-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,39 @@ test_that("visual tests", {
expect_doppelganger("Rotation around fixed point", draw_rotated_fixedpoint())

})

describe("text breaking behavior", {
get_labels <- function(x) {
g <- textbox_grob(x)
labels <- sapply(grid.force(g)$children, function(x) x$label)
return(labels)
}

it("does not break no-space text",
expect_contains(
get_labels(" ThisIsALongWordThatShouldNotBreak "),
c("ThisIsALongWordThatShouldNotBreak"))
)

it("does break spaced text",
expect_contains(
get_labels( "This Is Text That Should Break" ),
c("This", "Is", "Text", "That", "Should", "Break"))
)

it("does not break at non-breaking space but still at regular space",
expect_contains(
get_labels(" This&nbsp;Is&nbsp;Text That&nbsp;Should\n\nNot&nbsp;Break "),
c("This\u00A0Is\u00A0Text",
"That\u00A0Should",
"Not\u00A0Break"))
)

it("preserves non-breaking space at start and end",
expect_contains(
get_labels(" &nbsp;Beginning Should Not Trim Nor End&nbsp; "),
c("\u00A0Beginning",
"Should", "Not", "Trim", "Nor",
"End\u00A0"))
)
})