Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 9 additions & 6 deletions Sources/GraphiteText/FontFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
for folder in folders { Self.scan(folder, depth: 0, into: &found, seen: &seen) }
cached = found
return found
}
Expand All @@ -64,7 +67,7 @@ public final class FontFinder: Sendable {

private static let extensions: Set<String> = ["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<String>) {
guard depth < 4, let directory = opendir(folder) else { return }
defer { closedir(directory) }
var names: [(String, Bool)] = []
Expand All @@ -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
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/GraphiteText/FreeTypeFace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
14 changes: 12 additions & 2 deletions Tests/GraphiteFilesTests/TemporaryFolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
48 changes: 48 additions & 0 deletions Tests/GraphiteTextTests/FontFinderTests.swift
Original file line number Diff line number Diff line change
@@ -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
Loading