| 1 | @testable import BaseKit |
| 2 | import CoreText |
| 3 | import SwiftUI |
| 4 | import XCTest |
| 5 | |
| 6 | /// P8: Timeless Sans registers before first use and both cuts resolve; |
| 7 | /// without it, DM Sans, and with neither the system face, never a crash. |
| 8 | final class TypeTests: XCTestCase { |
| 9 | override func tearDown() { BaseFonts.register() } |
| 10 | |
| 11 | private func axis(_ name: String, _ tag: Int) -> Double? { |
| 12 | let font = CTFontCreateWithName(name as CFString, 16, nil) |
| 13 | let variation = CTFontCopyVariation(font) as? [NSNumber: NSNumber] |
| 14 | return variation?[NSNumber(value: tag)]?.doubleValue |
| 15 | } |
| 16 | |
| 17 | func testTimelessRegistersAndBothCutsResolve() throws { |
| 18 | let bundled = try XCTUnwrap(BaseFonts.bundled) |
| 19 | guard FileManager.default.fileExists(atPath: bundled.appendingPathComponent("TimelessSansVF.woff2").path) else { |
| 20 | throw XCTSkip("no Timeless Sans in this build (make fonts fetches it): the fallback tests cover it") |
| 21 | } |
| 22 | XCTAssertEqual(BaseFonts.register(), .timeless) |
| 23 | XCTAssertEqual(BaseFonts.register(), .timeless, "registering twice is harmless") |
| 24 | let styl = 0x5354_594C // 'STYL' |
| 25 | for weight in BaseFont.Weight.allCases { |
| 26 | for grotesk in [true, false] { |
| 27 | let name = try XCTUnwrap(BaseFonts.postScriptName(grotesk: grotesk, weight: weight, face: .timeless)) |
| 28 | let font = CTFontCreateWithName(name as CFString, 16, nil) |
| 29 | XCTAssertEqual(CTFontCopyPostScriptName(font) as String, name, "\(name) does not resolve") |
| 30 | XCTAssertEqual(CTFontCopyFamilyName(font) as String, "Timeless Sans") |
| 31 | // Grotesk is STYL 0, Sans STYL 100 (app.css's font-variation-settings). |
| 32 | if let value = axis(name, styl) { XCTAssertEqual(value, grotesk ? 0 : 100, name) } |
| 33 | // Fable's N4: each has its italic, by name. |
| 34 | let italic = try XCTUnwrap(BaseFonts.postScriptName(grotesk: grotesk, weight: weight, face: .timeless, italic: true)) |
| 35 | XCTAssertEqual(CTFontCopyPostScriptName(CTFontCreateWithName(italic as CFString, 16, nil)) as String, italic, "\(italic) does not resolve") |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func testDMSansWhenTimelessIsAbsent() throws { |
| 41 | let bundled = try XCTUnwrap(BaseFonts.bundled) |
| 42 | let dir = FileManager.default.temporaryDirectory.appendingPathComponent("base-fonts-\(UUID().uuidString)") |
| 43 | try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) |
| 44 | defer { try? FileManager.default.removeItem(at: dir) } |
| 45 | try FileManager.default.copyItem(at: bundled.appendingPathComponent("DMSans-VF.woff2"), to: dir.appendingPathComponent("DMSans-VF.woff2")) |
| 46 | XCTAssertEqual(BaseFonts.register(from: dir), .dmSans) |
| 47 | for weight in BaseFont.Weight.allCases { |
| 48 | let name = try XCTUnwrap(BaseFonts.postScriptName(grotesk: true, weight: weight, face: .dmSans)) |
| 49 | XCTAssertEqual(CTFontCopyPostScriptName(CTFontCreateWithName(name as CFString, 16, nil)) as String, name) |
| 50 | } |
| 51 | _ = BaseFont.heading(20) |
| 52 | } |
| 53 | |
| 54 | func testNoFaceAtAllIsTheSystemsAndNoCrash() throws { |
| 55 | let empty = FileManager.default.temporaryDirectory.appendingPathComponent("base-fonts-empty-\(UUID().uuidString)") |
| 56 | XCTAssertEqual(BaseFonts.register(from: empty), .system) |
| 57 | XCTAssertEqual(BaseFonts.register(from: nil), .system) |
| 58 | _ = BaseFont.body(15) |
| 59 | _ = BaseFont.heading(20, .medium) |
| 60 | } |
| 61 | } |
| 62 | |