@@ -121,12 +121,15 @@ end
121
121
122
122
fontname (ft:: FTFont ) = " $(family_name (ft)) $(style_name (ft)) "
123
123
124
+ const FONT_CACHE = Dict {String, Tuple{String,FTFont}} ()
125
+
124
126
function findfont (
125
127
searchstring:: String ;
126
128
italic:: Bool = false , # this is unused in the new implementation
127
129
bold:: Bool = false , # and this as well
128
130
additional_fonts:: String = " "
129
131
)
132
+ (path_ft = get (FONT_CACHE, searchstring, nothing )) != = nothing && return path_ft
130
133
font_folders = copy (fontpaths ())
131
134
132
135
isempty (additional_fonts) || pushfirst! (font_folders, additional_fonts)
@@ -135,6 +138,7 @@ function findfont(
135
138
searchparts = unique (split (lowercase (searchstring), r" \W +" , keepempty= false ))
136
139
137
140
best_score_so_far = (0 , 0 , false , typemin (Int))
141
+ best_font_path = " "
138
142
best_font = nothing
139
143
140
144
for folder in font_folders
@@ -144,7 +148,6 @@ function findfont(
144
148
face === nothing && continue
145
149
146
150
score = match_font (face, searchparts)
147
-
148
151
# we can compare all four tuple elements of the score at once
149
152
# in order of importance:
150
153
@@ -156,18 +159,17 @@ function findfont(
156
159
family_match_score = score[1 ]
157
160
if family_match_score > 0 && score > best_score_so_far
158
161
# finalize previous best font to close the font file
159
- if ! isnothing (best_font)
160
- finalize (best_font)
161
- end
162
+ isnothing (best_font) || finalize (best_font)
162
163
163
164
# new candidate
165
+ best_font_path = fpath
164
166
best_font = face
165
167
best_score_so_far = score
166
168
else
167
169
finalize (face)
168
170
end
169
171
end
170
172
end
171
-
172
- return best_font
173
+ best_font === nothing || (FONT_CACHE[searchstring] = (best_font_path, best_font))
174
+ return (best_font_path, best_font)
173
175
end
0 commit comments