From c4fef0930d7ac3dac2e6f37b20683bf0cf2a6fbb Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 26 Nov 2026 11:33:20 -0500 Subject: [PATCH 1/5] Gave FreeType faces their font file's face count --- Sources/GraphiteText/FreeTypeFace.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/GraphiteText/FreeTypeFace.swift b/Sources/GraphiteText/FreeTypeFace.swift index 5e885ff..f732332 100644 --- a/Sources/GraphiteText/FreeTypeFace.swift +++ b/Sources/GraphiteText/FreeTypeFace.swift @@ -36,6 +36,9 @@ public final class FreeTypeFace: FontFace, Sendable { /// The styles the font itself is: its bold and italic bits. public let styles: TextStyle + /// How many faces the font file has: more than 1 for a collection (.ttc). + public let faceCount: Int + /// A font in memory: `index` picks the face of a collection (a .ttc has /// several). public init(data bytes: some ContiguousBytes, index: Int = 0) throws { @@ -49,6 +52,7 @@ public final class FreeTypeFace: FontFace, Sendable { } tables = try? SfntTables(font: font, index: index) (familyName, styleName, styles) = Self.names(of: face, tables: tables) + faceCount = max(1, face.faceCount) self.face = Mutex(face) } @@ -64,6 +68,7 @@ public final class FreeTypeFace: FontFace, Sendable { tables = (try? Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)) .flatMap { try? SfntTables(font: $0, index: index) } (familyName, styleName, styles) = Self.names(of: face, tables: tables) + faceCount = max(1, face.faceCount) self.face = Mutex(face) } From 78a88428cf86fee1a3763e75ae7e91fd6f940a0c Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 26 Nov 2026 11:33:21 -0500 Subject: [PATCH 2/5] Listed a collection's faces by their count, and each font file once --- Sources/GraphiteText/FontFinder.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Sources/GraphiteText/FontFinder.swift b/Sources/GraphiteText/FontFinder.swift index e240ef5..41bf96f 100644 --- a/Sources/GraphiteText/FontFinder.swift +++ b/Sources/GraphiteText/FontFinder.swift @@ -42,7 +42,10 @@ public final class FontFinder: Sendable { entries.withLock { cached in if let cached { return cached } var found: [Entry] = [] - for folder in folders { Self.scan(folder, depth: 0, into: &found) } + // A folder may be inside another (Supplemental is in Fonts), so + // a file is read once, whichever folder reaches it first. + var seen = Set() + for folder in folders { Self.scan(folder, depth: 0, into: &found, seen: &seen) } cached = found return found } @@ -64,7 +67,7 @@ public final class FontFinder: Sendable { private static let extensions: Set = ["ttf", "otf", "ttc", "otc", "pfb", "dfont"] - private static func scan(_ folder: String, depth: Int, into found: inout [Entry]) { + private static func scan(_ folder: String, depth: Int, into found: inout [Entry], seen: inout Set) { guard depth < 4, let directory = opendir(folder) else { return } defer { closedir(directory) } var names: [(String, Bool)] = [] @@ -76,15 +79,15 @@ public final class FontFinder: Sendable { for (name, isDirectory) in names.sorted(by: { $0.0 < $1.0 }) { let path = folder + "/" + name if isDirectory { - scan(path, depth: depth + 1, into: &found) + scan(path, depth: depth + 1, into: &found, seen: &seen) } else if let dot = name.lastIndex(of: "."), extensions.contains(name[name.index(after: dot)...].lowercased()) { + guard seen.insert(path).inserted else { continue } guard let first = try? FreeTypeFace(path: path) else { continue } found.append(Entry(path: path, index: 0, family: first.familyName, style: first.styleName)) // A collection's other faces. - var index = 1 - while index < 32, let next = try? FreeTypeFace(path: path, index: index) { + for index in 1 ..< first.faceCount { + guard let next = try? FreeTypeFace(path: path, index: index) else { continue } found.append(Entry(path: path, index: index, family: next.familyName, style: next.styleName)) - index += 1 } } } From ca4dfc88c5527e078d0b6f6510c15caa1d5ed34f Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 26 Nov 2026 11:33:21 -0500 Subject: [PATCH 3/5] Tested the font finder's faces and that none is listed twice --- Tests/GraphiteTextTests/FontFinderTests.swift | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Tests/GraphiteTextTests/FontFinderTests.swift diff --git a/Tests/GraphiteTextTests/FontFinderTests.swift b/Tests/GraphiteTextTests/FontFinderTests.swift new file mode 100644 index 0000000..cbd77c3 --- /dev/null +++ b/Tests/GraphiteTextTests/FontFinderTests.swift @@ -0,0 +1,48 @@ +#if FreeType +import Testing +import GraphiteText + +/// Needs fonts on the machine: the system's, which nothing here checks in. +@Suite struct FontFinderTests { + + static let finder = FontFinder() + static let hasFont = !finder.fonts.isEmpty + + @Test(.enabled(if: Self.hasFont)) func noFaceIsListedTwice() { + // Folders inside others (Supplemental is in Fonts) don't list a file again. + let keys = Self.finder.fonts.map { "\($0.path)#\($0.index)" } + #expect(Set(keys).count == keys.count) + } + + @Test(.enabled(if: Self.hasFont)) func everyFaceOfAFileIsListedOnce() throws { + var indices: [String: [Int]] = [:] + for entry in Self.finder.fonts { indices[entry.path, default: []].append(entry.index) } + // A sample of the files: each opens its first face to be checked. + for (path, listed) in indices.sorted(by: { $0.key < $1.key }).prefix(150) { + let count = try FreeTypeFace(path: path).faceCount + #expect(count >= 1) + #expect(listed.first == 0, "\(path)") + #expect(Set(listed).count == listed.count, "\(path)") + #expect(listed.allSatisfy { (0 ..< count).contains($0) }, "\(path)") + } + } + + @Test(.enabled(if: Self.hasFont)) func collectionsAreListedWholeWhereTheirFacesOpen() throws { + var indices: [String: [Int]] = [:] + for entry in Self.finder.fonts { indices[entry.path, default: []].append(entry.index) } + for (path, listed) in indices where listed.count > 1 { + let face = try FreeTypeFace(path: path) + // Past the old cap of 32, and not stopped by one face that wouldn't open. + let opening = (0 ..< face.faceCount).filter { (try? FreeTypeFace(path: path, index: $0)) != nil } + #expect(listed == opening, "\(path)") + } + } + + @Test(.enabled(if: Self.hasFont)) func anOrdinaryFontHasOneFace() throws { + let entry = try #require(Self.finder.fonts.first { $0.path.lowercased().hasSuffix(".ttf") }) + let face = try FreeTypeFace(path: entry.path) + #expect(face.faceCount == 1) + #expect(Self.finder.fonts.filter { $0.path == entry.path }.count == 1) + } +} +#endif From 2ade70bcda8ee750b9e4dc96c6da336fb3fed354 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 26 Nov 2026 11:40:20 -0500 Subject: [PATCH 4/5] Depended on a tagged swift-freetype, 0.1.1 --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index f6a7187..7259d0d 100644 --- a/Package.swift +++ b/Package.swift @@ -73,7 +73,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-system.git", from: "1.9.0"), - .package(url: "https://github.com/PureSwift/swift-freetype.git", branch: "master"), + .package(url: "https://github.com/PureSwift/swift-freetype.git", from: "0.1.1"), ], targets: [ .target( From 43d3133c168d1c2999844751129e79c793444184 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Thu, 26 Nov 2026 11:48:56 -0500 Subject: [PATCH 5/5] Kept each Files test's folder out of /tmp's, where alias search finds other tests' files --- Tests/GraphiteFilesTests/TemporaryFolder.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Tests/GraphiteFilesTests/TemporaryFolder.swift b/Tests/GraphiteFilesTests/TemporaryFolder.swift index 941bcc8..b9ab7df 100644 --- a/Tests/GraphiteFilesTests/TemporaryFolder.swift +++ b/Tests/GraphiteFilesTests/TemporaryFolder.swift @@ -7,21 +7,31 @@ import Darwin #endif /// A folder of its own for a test, deleted with what's in it at the end. +/// +/// The folder is inside another of its own, not directly in /tmp: finding an +/// item that moved looks in the second folder up that is still there, and +/// there, other tests running at once make files that can be given a deleted +/// file's inode. final class TemporaryFolder { let path: String + /// The folder the test's is in, which goes with it. + private let root: String + init() { var template = Array("/tmp/graphite-XXXXXX".utf8CString) let made = template.withUnsafeMutableBufferPointer { mkdtemp($0.baseAddress!) } // The real path: /tmp is a link on macOS. let resolved = realpath(made!, nil)! - path = String(cString: resolved) + root = String(cString: resolved) free(resolved) + path = root + "/folder" + mkdir(path, 0o755) } deinit { - remove(path) + remove(root) } private func remove(_ path: String) {