Skip to content

Commit d1aab50

Browse files
committed
put in more finalizers to avoid "too many files open"
1 parent e91568b commit d1aab50

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/findfonts.jl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,22 @@ function findfont(
153153
# remove all candidates that have lesser scores than the first one
154154
for i in 2:length(candidates)
155155
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)
158161
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
161163
break
162164
end
163165
end
164166

165167
# return early if only one font remains
166168
length(candidates) == 1 && return candidates[1][1]
167169

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
169172

170173
# prefer regular fonts among the remaining options
171174
regular_styles = ("regular", "normal", "medium", "standard", "roman")
@@ -180,14 +183,31 @@ function findfont(
180183
by = c -> length(fontname(c[1])))
181184

182185
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+
183194
return shortest
184195
end
185196

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
188199

189200
sort!(candidates,
190201
by = c -> length(fontname(c[1])))
191202

192203
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
193213
end

0 commit comments

Comments
 (0)