diff --git a/R/utilities.R b/R/utilities.R index fd50818e..0e33c166 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -472,17 +472,13 @@ split_strings <- function(strng, width, units, multiplier = 1.03) { wrds <- strsplit(split, " ", fixed = TRUE)[[1]] - lngths <- c() - # trycatch({ - lngths <- (suppressWarnings(strwidth(wrds, units = un)) + - suppressWarnings(strwidth(" ", units = un))) * multiplier - - # }, err = { - # - # lngths <- (suppressWarnings(strwidth(wrds, units = un)) + - # suppressWarnings(strwidth(" ", units = un))) * multiplier - # - # }) + # Old code + # lngths <- (suppressWarnings(strwidth(wrds, units = un)) + + # suppressWarnings(strwidth(" ", units = un))) * multiplier + + if (length(wrds) > 0) { + lngths <- (strwdth(wrds, un) + strwdth(" ", un)) * multiplier + } # Loop through words and add up lines for (i in seq_along(wrds)) { @@ -550,6 +546,36 @@ split_strings <- function(strng, width, units, multiplier = 1.03) { return(ret) } + + +strwdth <- Vectorize(function(wrd, un) { + + + tryCatch({ + + if (is.na(wrd)) + nwrd <- " " + else + nwrd <- wrd + + ret <- suppressWarnings(strwidth(nwrd, units = un)) + + }, error = function(cond) { + + if (is.na(wrd)) { + nwrd <- " " + } else { + + nwrd <- rep("a", nchar(wrd)) + } + + ret <- suppressWarnings(strwidth(nwrd, units = un)) + + }) + + return(ret) +}, USE.NAMES = FALSE, SIMPLIFY = TRUE) + #' @description Calling function is responsible for opening the #' device context and assigning the font. This function will use #' strwidth to determine number of wraps of a string within a particular diff --git a/tests/testthat/docx/test50.docx b/tests/testthat/docx/test50.docx deleted file mode 100644 index fc87b8c6..00000000 Binary files a/tests/testthat/docx/test50.docx and /dev/null differ diff --git a/tests/testthat/docx/test51.docx b/tests/testthat/docx/test51.docx deleted file mode 100644 index 9925db5f..00000000 Binary files a/tests/testthat/docx/test51.docx and /dev/null differ diff --git a/tests/testthat/docx/test53.docx b/tests/testthat/docx/test53.docx deleted file mode 100644 index 97695db3..00000000 Binary files a/tests/testthat/docx/test53.docx and /dev/null differ diff --git a/tests/testthat/docx/test55.docx b/tests/testthat/docx/test55.docx deleted file mode 100644 index c4055b06..00000000 Binary files a/tests/testthat/docx/test55.docx and /dev/null differ diff --git a/tests/testthat/test-docx.R b/tests/testthat/test-docx.R index cab816f5..6adf96d5 100644 --- a/tests/testthat/test-docx.R +++ b/tests/testthat/test-docx.R @@ -24,7 +24,7 @@ dev <- FALSE # Basic Tests 1 - 10 ------------------------------------------------------ -test_that("docx0: Basic text works as expected.", { +test_that("docx0: Basic docx works as expected.", { fp <- file.path(base_path, "docx/test0.docx") diff --git a/tests/testthat/test-utilities.R b/tests/testthat/test-utilities.R index db1b3218..e9e65aaf 100644 --- a/tests/testthat/test-utilities.R +++ b/tests/testthat/test-utilities.R @@ -656,3 +656,53 @@ test_that("utils30: has_glue() works as expected.", { expect_equal(res3, TRUE) }) + +test_that("utils31: strwdth() works as expected.", { + + strngs <- c("hello", "goodbye", paste0("here is something", supsc('6'))) + + + + pdf(NULL) + par(family = 'mono', ps = 10) + + res <- strwidth(strngs, un = "inches") + + dev.off() + + res + + + + pdf(NULL) + par(family = 'mono', ps = 10) + + res1 <- strwdth(strngs, un = "inches") + + dev.off() + + res1 + + expect_equal(all(res == res1), TRUE) + + + pdf(NULL) + par(family = 'sans', ps = 10) + + res2 <- strwdth(strngs, un = "inches") + + dev.off() + + res2 + + # print(res1) + # print(res2) + + expect_equal(all(res1 != res2) , TRUE) + + + +}) + + +