@@ -153,19 +153,22 @@ function findfont(
153
153
# remove all candidates that have lesser scores than the first one
154
154
for i in 2 : length (candidates)
155
155
if candidates[i][2 ][1 ] < highscore[1 ] || candidates[i][2 ][2 ] < highscore[2 ]
156
- foreach (candidates[i: end ]) do c
157
- finalize (c[1 ])
156
+ # remove fonts from back to front and finalize them to close
157
+ # their files
158
+ for j in length (candidates): - 1 : i
159
+ to_remove = pop! (candidates)[1 ]
160
+ finalize (to_remove)
158
161
end
159
- # remove finalized candidates
160
- candidates = candidates[1 : i- 1 ]
162
+ # there will be no other i's we need to check after this
161
163
break
162
164
end
163
165
end
164
166
165
167
# return early if only one font remains
166
168
length (candidates) == 1 && return candidates[1 ][1 ]
167
169
168
- # now candidates all have the same family and style score
170
+ # there is still more than one candidate
171
+ # all candidates have the same family and style score
169
172
170
173
# prefer regular fonts among the remaining options
171
174
regular_styles = (" regular" , " normal" , " medium" , " standard" , " roman" )
@@ -180,14 +183,31 @@ function findfont(
180
183
by = c -> length (fontname (c[1 ])))
181
184
182
185
shortest = regular_matches[1 ][1 ]
186
+
187
+ # close all non-used font files
188
+ for c in candidates
189
+ if c[1 ] != = shortest
190
+ finalize (c)
191
+ end
192
+ end
193
+
183
194
return shortest
184
195
end
185
196
186
- # if we have multiple fonts with similar scores and none of them could
187
- # be chosen because they were "regular", just choose the shortest font name
197
+ # we didn't find any font with a "regular" name
198
+ # as a last heuristic, we just choose the font with the shortest name
188
199
189
200
sort! (candidates,
190
201
by = c -> length (fontname (c[1 ])))
191
202
192
203
shortest = candidates[1 ][1 ]
204
+
205
+ # close all non-used font files
206
+ for c in candidates
207
+ if c[1 ] != = shortest
208
+ finalize (c)
209
+ end
210
+ end
211
+
212
+ shortest
193
213
end
0 commit comments