Skip to content

Commit

Permalink
Fix for #312
Browse files Browse the repository at this point in the history
  • Loading branch information
dbosak01 committed Oct 29, 2023
1 parent 8016e01 commit 0344771
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
48 changes: 37 additions & 11 deletions R/utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
Binary file removed tests/testthat/docx/test50.docx
Binary file not shown.
Binary file removed tests/testthat/docx/test51.docx
Binary file not shown.
Binary file removed tests/testthat/docx/test53.docx
Binary file not shown.
Binary file removed tests/testthat/docx/test55.docx
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testthat/test-docx.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test-utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)



})



0 comments on commit 0344771

Please sign in to comment.