Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Launcher: make each Manrope size and weight once

v0lumehi9d167bb17841
diff --git a/frontend/Sources/NSPortBuilder/Typography.swift b/frontend/Sources/NSPortBuilder/Typography.swift
index cc478564..51369276 100644
--- a/frontend/Sources/NSPortBuilder/Typography.swift
+++ b/frontend/Sources/NSPortBuilder/Typography.swift
@@ -30,15 +30,28 @@ enum Typography {
return CTFontCreateWithFontDescriptor(variation, size, nil)
}
- static func font(_ face: CTFontDescriptor?, size: CGFloat, weight: Font.Weight, fallback: Font) -> Font {
- ctFont(face, size: size, weight: weight).map { Font($0) } ?? fallback
- }
-
/// Manrope as a CTFont, to measure how wide a label is set (nil when the face cannot be read).
- static func manropeFont(size: CGFloat, weight: Font.Weight) -> CTFont? { ctFont(manrope, size: size, weight: weight) }
+ static func manropeFont(size: CGFloat, weight: Font.Weight) -> CTFont? { manropeFace(size: size, weight: weight).ctFont }
+
+ static func text(_ size: CGFloat, _ weight: Font.Weight) -> Font { manropeFace(size: size, weight: weight).font }
+
+ /// Every label asks for its font each time a page is drawn, and a page has hundreds of labels. Each size and
+ /// weight is made once: one CTFont and one Font, so Core Text and SwiftUI see the same font again and reuse
+ /// what they measured.
+ private struct Face { let ctFont: CTFont?; let font: Font }
+ private struct FaceKey: Hashable { let size: CGFloat; let weight: Font.Weight }
+ private static let facesLock = NSLock()
+ private static var faces: [FaceKey: Face] = [:]
- static func text(_ size: CGFloat, _ weight: Font.Weight) -> Font {
- font(manrope, size: size, weight: weight, fallback: .system(size: size, weight: weight))
+ private static func manropeFace(size: CGFloat, weight: Font.Weight) -> Face {
+ let key = FaceKey(size: size, weight: weight)
+ facesLock.lock()
+ defer { facesLock.unlock() }
+ if let face = faces[key] { return face }
+ let ct = ctFont(manrope, size: size, weight: weight)
+ let face = Face(ctFont: ct, font: ct.map { Font($0) } ?? .system(size: size, weight: weight))
+ faces[key] = face
+ return face
}
}
diff --git a/frontend/Tests/NSPortBuilderTests/TypographyTests.swift b/frontend/Tests/NSPortBuilderTests/TypographyTests.swift
new file mode 100644
index 00000000..70aad486
--- /dev/null
+++ b/frontend/Tests/NSPortBuilderTests/TypographyTests.swift
@@ -0,0 +1,27 @@
+import CoreText
+import XCTest
+@testable import NSPortBuilder
+
+/// Each size and weight of Manrope is made once and then reused, so a page with hundreds of labels does not
+/// create hundreds of fonts each time it is drawn.
+final class TypographyTests: XCTestCase {
+ func testTheSameSizeAndWeightGiveTheSameFont() throws {
+ let first = try XCTUnwrap(Typography.manropeFont(size: 13, weight: .semibold))
+ let second = try XCTUnwrap(Typography.manropeFont(size: 13, weight: .semibold))
+ XCTAssertTrue(first === second)
+ XCTAssertEqual(Typography.text(13, .semibold), Typography.text(13, .semibold))
+ }
+
+ func testSizesAndWeightsStayApart() throws {
+ let regular = try XCTUnwrap(Typography.manropeFont(size: 13, weight: .regular))
+ let semibold = try XCTUnwrap(Typography.manropeFont(size: 13, weight: .semibold))
+ let larger = try XCTUnwrap(Typography.manropeFont(size: 15, weight: .semibold))
+ XCTAssertFalse(regular === semibold)
+ XCTAssertEqual(CTFontGetSize(semibold), 13)
+ XCTAssertEqual(CTFontGetSize(larger), 15)
+ let wght = NSNumber(value: 0x7767_6874)
+ let axes = { (font: CTFont) in (CTFontCopyVariation(font) as? [NSNumber: Double])?[wght] }
+ XCTAssertEqual(axes(regular), 400)
+ XCTAssertEqual(axes(semibold), 600)
+ }
+}