After filing
https://github.com/JuliaGraphics/FreeTypeAbstraction.jl/issues/67
I have been looking for the best way to locate and load a font.
(initially for DynamicGrids.jl, which is otherwise super fast, so it mattered, but the question should be of wider interest).
fontconfig
is about 2000 times faster to locate the right font:
using FreeTypeAbstraction
using Fontconfig
font_file(pattern::Fontconfig.Pattern) = Fontconfig.format(Fontconfig.match(pattern), "%{file}")
font_file(str::String) = font_file(Fontconfig.Pattern(str))
# str is either a direct path, or a fontconfig spec such as "cantarell:bold"
function load_font(str::String)
path = isfile(str) ? str : font_file(str)
return FreeTypeAbstraction.FTFont(path)
end
julia> load_font("cantarell:bold")
FTFont (family = Cantarell, style = Bold)
julia> using BenchmarkTools
julia> @btime (load_font("cantarell") |> finalize) # finalize to avoid "too many open files" error
1.246 ms (15 allocations: 2.62 KiB)
While
julia> using FreeTypeAbstraction
julia> using BenchmarkTools
julia> @btime FreeTypeAbstraction.findfont("cantarell")
2.321 s (250478 allocations: 24.99 MiB)
FTFont (family = Cantarell, style = Regular)
Currently Fontconfig.jl and FreeTypeAbstraction.jl are independent, which seems good.
Adding to FreeTypeAbstraction.findfont
a cache similar to what Makie does might make sense.
But a one time full scan overhead will remain,
unless a font properties cache is added.
But that looks like reinventing fontconfig
, and Fontconfig.jl looks good now.
Did I overlook a package that gather both functionalities ?
(Found nothing, either here on discourse, on stackoverflow, or on juliapackages)
Or maybe is there an unregistered package in preparation ?
If not, would it be interesting ?