struct URLManager {
static private var fontsDirectories: [URL] {
guard
let directoryInSystem = FileManager.default.urls(for: .libraryDirectory, in: .systemDomainMask).first?.appendingPathComponent("Fonts"),
let directoryInGlobal = FileManager.default.urls(for: .libraryDirectory, in: .localDomainMask).first?.appendingPathComponent("Fonts"),
let directoryInUser = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first?.appendingPathComponent("Fonts")
else {
return []
}
return [directoryInSystem, directoryInGlobal, directoryInUser]
}
static private func loadFonts(in directory: URL) -> [URL] {
guard let enumerator = FileManager.default.enumerator(
at: directory,
includingPropertiesForKeys: [.isRegularFileKey],
options: [.skipsHiddenFiles, .skipsPackageDescendants]) else {
return []
}
var fontURLs = [URL]()
let extensions = ["otf", "ttf", "ttc"]
for case let fileURL as URL in enumerator {
if extensions.contains(fileURL.pathExtension) {
fontURLs.append(fileURL)
}
fontURLs.append(fileURL)
}
return fontURLs
}
static func loadAllFontsInLocal() -> [URL] {
var urls: [URL] = []
fontsDirectories.forEach { directory in
urls += loadFonts(in: directory)
}
urls.sort { $0.path() < $1.path() }
return urls
}
}