51 lines
1.6 KiB
Swift
51 lines
1.6 KiB
Swift
//
|
|
// FontToolHelper.swift
|
|
//
|
|
//
|
|
// Created by Thibaut Schmitt on 13/12/2021.
|
|
//
|
|
|
|
import Foundation
|
|
import CLIToolCore
|
|
|
|
class FontToolHelper {
|
|
static func getFontsFilenames(fromInputFolder inputFolder: String) -> [String] {
|
|
// Get a enumerator for all files
|
|
let fileManager = FileManager()
|
|
|
|
guard fileManager.fileExists(atPath: inputFolder) else {
|
|
FontTool.exit(withError: FontToolError.inputFolderNotFound(inputFolder))
|
|
}
|
|
|
|
let enumerator: FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: inputFolder)!
|
|
|
|
// Filters font files
|
|
let fontsFileNames: [String] = (enumerator.allObjects as! [String])
|
|
.filter {
|
|
if $0.hasSuffix(".ttf") || $0.hasSuffix(".otf") {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
return fontsFileNames
|
|
}
|
|
|
|
static func getFontsNames(fontsFileNames: [String]) -> [String] {
|
|
// Get font name (font name and font file name can be different)
|
|
fontsFileNames.compactMap { getFontName(atPath: $0) }
|
|
}
|
|
|
|
private static func getFontName(atPath path: String) -> String {
|
|
//print("fc-scan --format %{postscriptname} \(path)")
|
|
// Get real font name
|
|
let task = Shell.shell("fc-scan", "--format", "%{postscriptname}", path)
|
|
|
|
guard let fontName = task.output, task.terminationStatus == 0 else {
|
|
FontTool.exit(withError: FontToolError.fcScan(path, task.terminationStatus, task.output))
|
|
}
|
|
|
|
return fontName
|
|
}
|
|
}
|