Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 23, 2022
1 parent 9911f05 commit 257ff2b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ FileIO = "1.6"
FixedPointNumbers = "0.6, 0.7, 0.8"
Formatting = "0.4"
FreeType = "3.0, 4.0"
FreeTypeAbstraction = "0.10"
FreeTypeAbstraction = "0.11"
GeometryBasics = "0.4.2"
GridLayoutBase = "0.9"
ImageIO = "0.2, 0.3, 0.4, 0.5, 0.6"
Expand Down
27 changes: 14 additions & 13 deletions src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ to_align(x::Vec2f) = x

function find_and_load_font(str::String)
fontpath = assetpath("fonts")
font_path, font = FreeTypeAbstraction.findfont(str; additional_fonts=fontpath)
font = FreeTypeAbstraction.findfont(str; additional_fonts=fontpath)
if font === nothing
@warn("Could not find font $str, using TeX Gyre Heros Makie")
if "tex gyre heros makie" == lowercase(str)
Expand All @@ -924,16 +924,15 @@ function find_and_load_font(str::String)
end
return find_and_load_font("TeX Gyre Heros Makie")
end
return font_path, font
return font
end

const FONT_PATHS = Dict{String, String}(
"regular" => find_and_load_font("TeX Gyre Heros Makie")[1],
"bold" => find_and_load_font("TeX Gyre Heros Makie Bold")[1],
"italic" => find_and_load_font("TeX Gyre Heros Makie Italic")[1],
"bold_italic" => find_and_load_font("TeX Gyre Heros Makie Bold Italic")[1],
"regular" => FreeTypeAbstraction.get_path(find_and_load_font("TeX Gyre Heros Makie")),
"bold" => FreeTypeAbstraction.get_path(find_and_load_font("TeX Gyre Heros Makie Bold")),
"italic" => FreeTypeAbstraction.get_path(find_and_load_font("TeX Gyre Heros Makie Italic")),
"bold_italic" => FreeTypeAbstraction.get_path(find_and_load_font("TeX Gyre Heros Makie Bold Italic")),
)
const FONT_CACHE = Dict{String, NativeFont}()

function load_font_from_path(str::String)
font = FreeTypeAbstraction.try_load(str)
Expand All @@ -944,14 +943,15 @@ end
function load_font(str::String)
str == "default" && return load_font("TeX Gyre Heros Makie")

if (font_path = get(FONT_PATHS, str, nothing)) !== nothing
return load_font_from_path(font_path)
font = if (ft_path = get(FONT_PATHS, str, nothing)) !== nothing
load_font_from_path(ft_path)
elseif isfile(str) # check if the string points to a font file and load that
return load_font_from_path(str)
load_font_from_path(str)
else
find_and_load_font(str) # costly (scans directories)
end

# costly (scans directories)
FONT_PATHS[str], font = find_and_load_font(str)
FONT_PATHS[str] = FreeTypeAbstraction.get_path(font)
font
end

Expand All @@ -962,9 +962,10 @@ a string naming a font, e.g. helvetica
"""
to_font(x::AbstractString) =
let str = string(x)
get!(FONT_CACHE, str) do
if (font = get(FreeTypeAbstraction.FONT_CACHE, str, nothing)) === nothing
return load_font(str)
end
font
end
to_font(x::Vector{String}) = to_font.(x)
to_font(x::NativeFont) = x
Expand Down

0 comments on commit 257ff2b

Please sign in to comment.