Skip to content

Commit 3ce7edf

Browse files
committed
Stop sorting font files, cache info in precompile
1 parent fad9055 commit 3ce7edf

File tree

2 files changed

+9
-39
lines changed

2 files changed

+9
-39
lines changed

src/findfonts.jl

+4-39
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ function findfont_nocache(searchstring::String, fontfolders::Vector{<:AbstractSt
176176
# \W splits at all groups of non-word characters (like space, -, ., etc)
177177
searchparts = unique(split(lowercase(searchstring), r"\W+", keepempty=false))
178178
max_score1 = sum(length, searchparts) + sum(1:length(searchparts))
179-
best_1i, best_file, best_score = 0, nothing, (0, 0, 0, typemin(Int), 0)
180-
for (i, fontfile) in enumerate(fontfiles_guess_sorted(searchparts, fontfolders))
179+
best_file, best_score = nothing, (0, 0, 0, typemin(Int), 0)
180+
for folder in fontfolders, fontfile in readdir(folder, join=true)
181181
# we can compare all five tuple elements of the score at once
182182
# in order of importance:
183183
# 1. number of family and style match characters (with priority factor)
@@ -186,44 +186,9 @@ function findfont_nocache(searchstring::String, fontfolders::Vector{<:AbstractSt
186186
# 4. the negative length of the font name, the shorter the better
187187
# 5. the font file extension priority
188188
score = score_font(searchparts, fontfile)
189-
if first(score) > 0 && score >= best_score
190-
best_1i = i
191-
if score > best_score
192-
best_file, best_score = fontfile, score
193-
end
194-
elseif first(best_score) == max_score1 && first(score) < first(best_score) && i > 2 * best_1i
195-
return best_file
189+
if first(score) > 0 && score > best_score
190+
best_file, best_score = fontfile, score
196191
end
197192
end
198193
best_file
199194
end
200-
201-
const FONTFILE_NAMES = Dict{String, String}()
202-
203-
"""
204-
fontfiles_guess_sorted(searchparts::Vector{<:AbstractString}; additional_fonts::String="")
205-
206-
Collect all font files under `fontpaths` (also including `additional_fonts` if
207-
non-empty), and then rank them by how closely we think the font information
208-
matches `searchparts`, guessing based on the font file name.
209-
"""
210-
function fontfiles_guess_sorted(searchparts::Vector{<:AbstractString}, fontfolders::Vector{<:AbstractString})
211-
function score_names(searchparts, fontfile)
212-
filename = get!(() -> splitext(basename(fontfile)) |> first |> lowercase,
213-
FONTFILE_NAMES, fontfile) # Caching produces a ~7x speedup
214-
# We sum the length of the search parts found in `filename` with a reverse part
215-
# index (`i`) to weight earlier matching parts higher. This is essentially
216-
# an approximation of the more sophisticated (and expensive) scoring performed
217-
# in `score_font`.
218-
sum((i + length(part) for (i, part) in enumerate(Iterators.reverse(searchparts))
219-
if occursin(part, filename)),
220-
init=0), -length(filename)
221-
end
222-
searchparts = copy(searchparts)
223-
push!(searchparts, String(map(first, searchparts)))
224-
allfonts = String[]
225-
for folder in fontfolders
226-
append!(allfonts, readdir(folder, join=true))
227-
end
228-
sort(allfonts, by=Base.Fix1(score_names, searchparts), rev=true)
229-
end

src/precompile.jl

+5
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ function _precompile_()
33
@assert precompile(Tuple{typeof(findfont),String}) # time: 0.12886831
44
@assert precompile(Tuple{typeof(try_load),String}) # time: 0.033520337
55
@assert precompile(Tuple{typeof(renderface),FTFont,Char,Int64}) # time: 0.019107351
6+
# Populate the font info cache
7+
__init__()
8+
for folder in fontpaths()
9+
map(font_info, readdir(folder, join=true))
10+
end
611
end

0 commit comments

Comments
 (0)