Skip to content

Commit

Permalink
Use BaseDirs for locating font directories
Browse files Browse the repository at this point in the history
Identifying the correct platform-specific font directories can be done
mostly correctly most of the time just with a hardcoded list of places
to look (particularly on Apple systems). However, on Windows and
XDG-following (i.e. Linux and friends) systems the user and system font
directories can end up in other places, and accommodating these edge
cases takes some effort.

This effort has already been put in with the BaseDirs package, which is
a small zero-dependency package whose entire purpose is to find the
correct directories for different types of content on various platforms.
So, we can remove the current font-folder-finding code entirely and just
call BaseDirs.fonts() for a reduction in code here and an improvement in
compatibility.
  • Loading branch information
tecosaur committed Oct 4, 2024
1 parent 6e525c9 commit d01e904
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ uuid = "663a7486-cb36-511b-a19d-713bb74d65c9"
version = "0.10.3"

[deps]
BaseDirs = "18cc8868-cbac-4acf-b575-c8ff214dc66f"
ColorVectorSpace = "c3611d14-8923-5661-9e6a-0046d554d3a4"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
FreeType = "b38be410-82b0-50bf-ab77-7b57e271db43"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"

[compat]
BaseDirs = "1"
ColorVectorSpace = "0.8, 0.9, 0.10"
Colors = "0.11, 0.12"
FreeType = "4"
Expand Down
16 changes: 13 additions & 3 deletions src/FreeTypeAbstraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Base.Iterators: Repeated, repeated
import Base: /, *, ==, @lock
import Base.Broadcast: BroadcastStyle, Style, broadcasted
using GeometryBasics: StaticVector
using BaseDirs

include("types.jl")
include("findfonts.jl")
Expand All @@ -25,15 +26,24 @@ fontpaths() = valid_fontpaths
function __init__()
ft_init()
atexit(ft_done)
append!(valid_fontpaths, BaseDirs.fonts(existent=true))
@static if Sys.isunix() && !Sys.isapple()
i = firstindex(valid_fontpaths)
while i <= length(valid_fontpaths)
path = valid_fontpaths[i]
if isdir(path)
append!(valid_fontpaths, filter(isdir, readdir(path, join=true)))
end
i += 1
end
end
# This method of finding fonts might not work for exotic platforms,
# so we supply a way to help it with an environment variable.
paths = filter(isdir, _font_paths())
if haskey(ENV, "FREETYPE_ABSTRACTION_FONT_PATH")
path = ENV["FREETYPE_ABSTRACTION_FONT_PATH"]
isdir(path) || error("Path in environment variable FREETYPE_ABSTRACTION_FONT_PATH is not a valid directory!")
push!(paths, path)
push!(valid_fontpaths, path)
end
append!(valid_fontpaths, paths)
end

if Base.VERSION >= v"1.4.2"
Expand Down
44 changes: 0 additions & 44 deletions src/findfonts.jl
Original file line number Diff line number Diff line change
@@ -1,47 +1,3 @@
if Sys.isapple()
function _font_paths()
return [
"/Library/Fonts", # Additional fonts that can be used by all users. This is generally where fonts go if they are to be used by other applications.
joinpath(homedir(), "Library/Fonts"), # Fonts specific to each user.
"/Network/Library/Fonts", # Fonts shared for users on a network
"/System/Library/Fonts", # System specific fonts
"/System/Library/Fonts/Supplemental", # new location since Catalina
]
end
elseif Sys.iswindows()
function _font_paths()
return [
joinpath(get(ENV, "SYSTEMROOT", "C:\\Windows"), "Fonts"),
joinpath(homedir(), "AppData", "Local", "Microsoft", "Windows", "Fonts"),
]
end
else
function add_recursive(result, path)
for p in readdir(path)
pabs = joinpath(path, p)
if isdir(pabs)
push!(result, pabs)
add_recursive(result, pabs)
end
end
end
function _font_paths()
result = String[]
for p in (
"/usr/share/fonts",
joinpath(homedir(), ".fonts"),
joinpath(homedir(), ".local/share/fonts"),
"/usr/local/share/fonts"
)
if isdir(p)
push!(result, p)
add_recursive(result, p)
end
end
return result
end
end

family_name(x::FTFont) = lowercase(x.family_name)
style_name(x::FTFont) = lowercase(x.style_name)

Expand Down

0 comments on commit d01e904

Please sign in to comment.