Skip to content

Commit

Permalink
fix: Do not split on Unicode characters with Line_Break=Glue property
Browse files Browse the repository at this point in the history
Specifically, this allows for using non-breaking space ("\u00A0", " ")
in text which is preserved when using SVG output.

Connects to:
- <wilkelab#35>,
- <wilkelab/ggtext#95>.
  • Loading branch information
zmughal committed Dec 2, 2024
1 parent 391684c commit 15b3187
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
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, width = unit(50, "pt"))
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"))
)
})

0 comments on commit 15b3187

Please sign in to comment.