diff --git a/R/process-tags.R b/R/process-tags.R index ccac519..0c7adc7 100644 --- a/R/process-tags.R +++ b/R/process-tags.R @@ -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, diff --git a/tests/testthat/test-textbox-grob.R b/tests/testthat/test-textbox-grob.R index beaf613..072415d 100644 --- a/tests/testthat/test-textbox-grob.R +++ b/tests/testthat/test-textbox-grob.R @@ -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 Is Text That Should\n\nNot Break "), + c("This\u00A0Is\u00A0Text", + "That\u00A0Should", + "Not\u00A0Break")) + ) + + it("preserves non-breaking space at start and end", + expect_contains( + get_labels("  Beginning Should Not Trim Nor End  "), + c("\u00A0Beginning", + "Should", "Not", "Trim", "Nor", + "End\u00A0")) + ) +})