Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cache mechanism #76

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "FreeTypeAbstraction"
uuid = "663a7486-cb36-511b-a19d-713bb74d65c9"
version = "0.10.0"
version = "0.11.0"

[deps]
ColorVectorSpace = "c3611d14-8923-5661-9e6a-0046d554d3a4"
Expand Down
2 changes: 2 additions & 0 deletions src/FreeTypeAbstraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function __init__()
push!(paths, path)
end
append!(valid_fontpaths, paths)
# fonts aren't cacheable by precompilation, so we need to empty it on load!
empty!(FONT_CACHE)
end

if Base.VERSION >= v"1.4.2"
Expand Down
10 changes: 5 additions & 5 deletions src/findfonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ end

fontname(ft::FTFont) = "$(family_name(ft)) $(style_name(ft))"

const FONT_CACHE = Dict{String, FTFont}()

function findfont(
searchstring::String;
italic::Bool=false, # this is unused in the new implementation
bold::Bool=false, # and this as well
additional_fonts::String=""
)
(font = get(FONT_CACHE, searchstring, nothing)) !== nothing && return font
font_folders = copy(fontpaths())

isempty(additional_fonts) || pushfirst!(font_folders, additional_fonts)
Expand All @@ -144,7 +147,6 @@ function findfont(
face === nothing && continue

score = match_font(face, searchparts)

# we can compare all four tuple elements of the score at once
# in order of importance:

Expand All @@ -156,9 +158,7 @@ function findfont(
family_match_score = score[1]
if family_match_score > 0 && score > best_score_so_far
# finalize previous best font to close the font file
if !isnothing(best_font)
finalize(best_font)
end
isnothing(best_font) || finalize(best_font)

# new candidate
best_font = face
Expand All @@ -168,6 +168,6 @@ function findfont(
end
end
end

best_font === nothing || (FONT_CACHE[searchstring] = best_font)
return best_font
end
8 changes: 5 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,23 @@ function boundingbox(extent::FontExtent{T}) where T
end

mutable struct FTFont
ft_path::String
ft_ptr::FreeType.FT_Face
use_cache::Bool
extent_cache::Dict{UInt64, FontExtent{Float32}}
function FTFont(ft_ptr::FreeType.FT_Face, use_cache::Bool=true)
function FTFont(ft_path::String, ft_ptr::FreeType.FT_Face, use_cache::Bool=true)
extent_cache = Dict{UInt64, FontExtent{Float32}}()
face = new(ft_ptr, use_cache, extent_cache)
face = new(ft_path, ft_ptr, use_cache, extent_cache)
finalizer(safe_free, face)
return face
end
end

use_cache(face::FTFont) = getfield(face, :use_cache)
get_cache(face::FTFont) = getfield(face, :extent_cache)
get_path(face::FTFont) = getfield(face, :ft_path)

FTFont(path::String) = FTFont(newface(path))
FTFont(path::String) = FTFont(path, newface(path))

# C interop
Base.cconvert(::Type{FreeType.FT_Face}, font::FTFont) = font
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ face = FA.findfont("hack")
@test :size in propertynames(face)
@test repr(face) == "FTFont (family = Hack, style = Regular)"
@test Broadcast.broadcastable(face)[] == face
@test FA.get_path(face) == joinpath(@__DIR__, "hack_regular.ttf")

@test FA.ascender(face) isa Real
@test FA.descender(face) isa Real
Expand Down