@@ -47,15 +47,15 @@ function feelingoccupancy(imgs, border=0, bgvalue=imgs[1][1])
47
47
oc * 0.93
48
48
end
49
49
50
- function wordsoccupancy! (wc)
50
+ function wordsoccupancy! (wc, cache = ( - ones ( length (wc)), Vector (undef, length (wc))) )
51
51
words = wc. words
52
52
fonts = getfonts (wc)
53
53
angles = getangles (wc) ./ 180 .* π
54
54
border = 1
55
55
sizemax = size (wc. mask) .* √ (getparameter (wc, :volume ) / prod (size (wc. mask))) .* 0.9
56
56
check = getparameter (wc, :maxfontsize0 ) == :auto
57
57
check && setparameter! (wc, minimum (sizemax), :maxfontsize )
58
- imgs = Vector (undef, length (words))
58
+ szcache, imgs = cache
59
59
for i in 1 : 3
60
60
fontsizes = getfontsizes (wc)
61
61
success = true
@@ -64,6 +64,9 @@ function wordsoccupancy!(wc)
64
64
for j in ichunk: nchunk: length (words) # it is a more balance split strategy since the words is sorted by size
65
65
success || break
66
66
c, sz, ft, θ = words[j], fontsizes[j], fonts[j], angles[j]
67
+ if szcache[j] == sz # only check the fontsize, assume the font won't change
68
+ continue
69
+ end
67
70
img = Render. rendertext (string (c), sz, backgroundcolor= (0 , 0 , 0 , 0 ), font= ft, border= border)
68
71
a, b = size (img)
69
72
imsz = max (a* abs (cos (θ)), b* abs (sin (θ))), max (a* abs (sin (θ)), b* abs (cos (θ)))
@@ -76,6 +79,7 @@ function wordsoccupancy!(wc)
76
79
end
77
80
end
78
81
imgs[j] = img
82
+ szcache[j] = sz
79
83
end
80
84
success || break
81
85
end
@@ -145,6 +149,7 @@ function findscale!(wc::WC; initialscale=0, density=0.3, maxiter=5, tolerance=0.
145
149
sc0 = 0.
146
150
tg0 = 0.
147
151
oneway_count = 1
152
+ occ_cache = (- ones (length (wc)), Vector (undef, length (wc)))
148
153
while true
149
154
step = step + 1
150
155
if step > maxiter
@@ -153,7 +158,7 @@ function findscale!(wc::WC; initialscale=0, density=0.3, maxiter=5, tolerance=0.
153
158
end
154
159
# cal tg1
155
160
setparameter! (wc, sc1, :scale )
156
- tg1 = wordsoccupancy! (wc)
161
+ tg1 = wordsoccupancy! (wc, occ_cache )
157
162
dens = tg1 / area
158
163
@debug " ⋯scale=$(getparameter (wc, :scale )) , density=$dens \t " * (dens > density ? " ↑" : " ↓" )
159
164
if tg1 > target
0 commit comments