Skip to content

Commit

Permalink
add ft_path field to FTFont
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 23, 2022
1 parent caa0f02 commit 792df58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
8 changes: 3 additions & 5 deletions src/findfonts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ end

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

const FONT_CACHE = Dict{String, Tuple{String,FTFont}}()
const FONT_CACHE = Dict{String, FTFont}()

function findfont(
searchstring::String;
Expand All @@ -138,7 +138,6 @@ function findfont(
searchparts = unique(split(lowercase(searchstring), r"\W+", keepempty=false))

best_score_so_far = (0, 0, false, typemin(Int))
best_font_path = ""
best_font = nothing

for folder in font_folders
Expand All @@ -162,14 +161,13 @@ function findfont(
isnothing(best_font) || finalize(best_font)

# new candidate
best_font_path = fpath
best_font = face
best_score_so_far = score
else
finalize(face)
end
end
end
best_font === nothing || (FONT_CACHE[searchstring] = (best_font_path, best_font))
return (best_font_path, best_font)
best_font === nothing || (FONT_CACHE[searchstring] = best_font)
return best_font
end
7 changes: 4 additions & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ 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
Expand All @@ -138,7 +139,7 @@ end
use_cache(face::FTFont) = getfield(face, :use_cache)
get_cache(face::FTFont) = getfield(face, :extent_cache)

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
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Test
@test FA.ft_init()
end

face = FA.findfont("hack")[2]
face = FA.findfont("hack")

@testset "basics" begin
@test :size in propertynames(face)
Expand Down Expand Up @@ -261,12 +261,12 @@ end
append!(FA.valid_fontpaths, valid_fontpaths)
for font in fonts
@testset "finding $font" begin
@test findfont(font)[2] !== nothing
@test findfont(font) !== nothing
end
end
@testset "find in additional dir" begin
@test findfont("Hack")[2] === nothing
@test findfont("Hack", additional_fonts = @__DIR__)[2] !== nothing
@test findfont("Hack") === nothing
@test findfont("Hack", additional_fonts = @__DIR__) !== nothing
end
end

Expand Down

0 comments on commit 792df58

Please sign in to comment.