// // FontToolHelper.swift // // // Created by Thibaut Schmitt on 13/12/2021. // import Foundation import CLIToolCore class FontToolHelper { static func getFontsData(fromInputFolder inputFolder: String) -> (fontsNames: [String], fontsFileNames: [String]) { // Get a enumerator for all files let fileManager = FileManager() guard fileManager.fileExists(atPath: inputFolder) else { FontTool.exit(withError: FontToolError.fcScan(path, task.terminationStatus, task.output)) } 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 } // Get font name (font name and font file name can be different) let fontsNames = fontsFileNames.compactMap { getFontName(atPath: "\(inputFolder)/\($0)") } return (fontsNames: fontsNames, fontsFileNames: fontsFileNames) } 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 } }