Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Oct 13, 2023
1 parent 8a5aeeb commit 26bd85e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions R/tmapGridComp.R
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,46 @@ tmapGridCompHeight_text = function(comp, o) {
textS = if (comp$text == "") 0 else comp$size #* o$scale
textP = comp$padding[c(3,1)] * textS * o$lin
textH = textS * o$lin
comp$Hin = sum(textP[1], textH, textP[2])

nlines = number_text_lines(comp$text)

comp$Hin = sum(textP[1], textH * nlines, textP[2])
comp
}

# borrowed from treemap (wraps text to 1-5 lines)
wrapText = function(txt, nlines) {
if (nlines == 1) {
txt
} else {
# create some wrappings, with slightly different widths:
results <- lapply(1:5, FUN=function(pos, nlines, txt) {
strwrap(txt, width = pos+(nchar(txt)/nlines))}, nlines, txt)
lengths = sapply(results, length)

# find the best match
diff = nlines - lengths
diff[diff < 0] = 1000
id = which.min(diff)[1]

paste(results[[id]], collapse = "\n")
}
}


tmapGridCompWidth_text = function(comp, o) {
textS = if (comp$text == "") 0 else comp$size #* o$scale
textP = comp$padding[c(2,4)] * textS * o$lin
textW = textS * graphics::strwidth(comp$text, units = "inch", family = comp$fontfamily, font = fontface2nr(comp$fontface))
comp$Win = sum(textP[1], textW, textP[2])

w = sum(textP[1], textW, textP[2])
if (!is.na(comp$width)) {
approxNumL = min(20, round(w / (comp$width * textS * o$lin)))
comp$text = wrapText(comp$text, approxNumL)
comp$Win = comp$width * textS * o$lin
} else {
comp$Win = w
}
comp
}

Expand Down

0 comments on commit 26bd85e

Please sign in to comment.