From 40a689688971f1587960e3bb70bcb2d9021c1e3c Mon Sep 17 00:00:00 2001 From: Peter Tang Date: Fri, 15 Sep 2023 17:06:06 +0800 Subject: [PATCH] corrected some variable names within BundleManager functions, added onDemandBundle registration instead of loading all Resources at startup. --- Sources/SwiftMath/MathBundle/MathFont.swift | 27 +++++++++++++++------ Tests/SwiftMathTests/MathFontTests.swift | 7 ++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Sources/SwiftMath/MathBundle/MathFont.swift b/Sources/SwiftMath/MathBundle/MathFont.swift index 5be5d50..4c8a1b6 100644 --- a/Sources/SwiftMath/MathBundle/MathFont.swift +++ b/Sources/SwiftMath/MathBundle/MathFont.swift @@ -126,16 +126,28 @@ private class BundleManager { initializedOnceAlready.toggle() } + private func onDemandRegistration(mathFont: MathFont) { + guard cgFonts[mathFont] == nil else { return } + do { + try BundleManager.manager.registerCGFont(mathFont: mathFont) + try BundleManager.manager.registerMathTable(mathFont: mathFont) + + } catch { + fatalError("MTMathFonts:\(#function) ondemand loading failed, mathFont \(mathFont.rawValue), reason \(error)") + } + } fileprivate func obtainCGFont(font: MathFont) -> CGFont { - if !initializedOnceAlready { registerAllBundleResources() } - guard let cfFont = cgFonts[font] else { - fatalError("\(#function) unable to locate CTFont \(font.fontName)") + // if !initializedOnceAlready { registerAllBundleResources() } + onDemandRegistration(mathFont: font) + guard let cgFont = cgFonts[font] else { + fatalError("\(#function) unable to locate CGFont \(font.fontName)") } - return cfFont + return cgFont } fileprivate func obtainCTFont(font: MathFont, withSize size: CGFloat) -> CTFont { - if !initializedOnceAlready { registerAllBundleResources() } + // if !initializedOnceAlready { registerAllBundleResources() } + onDemandRegistration(mathFont: font) let fontPair = CTFontPair(font: font, size: size) guard let ctFont = ctFonts[fontPair] else { if let cgFont = cgFonts[font] { @@ -143,12 +155,13 @@ private class BundleManager { ctFonts[fontPair] = ctFont return ctFont } - fatalError("\(#function) unable to locate CTFont \(font.fontName)") + fatalError("\(#function) unable to locate CGFont \(font.fontName), nor create CTFont") } return ctFont } fileprivate func obtainMathTable(font: MathFont) -> NSDictionary { - if !initializedOnceAlready { registerAllBundleResources() } + // if !initializedOnceAlready { registerAllBundleResources() } + onDemandRegistration(mathFont: font) guard let mathTable = mathTables[font] else { fatalError("\(#function) unable to locate mathTable: \(font.rawValue).plist") } diff --git a/Tests/SwiftMathTests/MathFontTests.swift b/Tests/SwiftMathTests/MathFontTests.swift index 09ac0db..ed2f441 100644 --- a/Tests/SwiftMathTests/MathFontTests.swift +++ b/Tests/SwiftMathTests/MathFontTests.swift @@ -37,6 +37,13 @@ final class MathFontTests: XCTestCase { } #endif } + func testOnDemandMathFontScript() throws { + let size = Int.random(in: 20 ... 40) + let mathFont = MathFont.allCases.randomElement()! + XCTAssertNotNil(mathFont.cgFont()) + XCTAssertNotNil(mathFont.ctFont(withSize: CGFloat(size))) + XCTAssertEqual(mathFont.ctFont(withSize: CGFloat(size)).fontSize, CGFloat(size), "ctFont fontSize test") + } var fontNames: [String] { MathFont.allCases.map { $0.fontName } }