Skip to content

Commit

Permalink
corrected some variable names within BundleManager functions, added o…
Browse files Browse the repository at this point in the history
…nDemandBundle registration instead of loading all Resources at startup.
  • Loading branch information
petersktang committed Sep 15, 2023
1 parent 2cdab9c commit 40a6896
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Sources/SwiftMath/MathBundle/MathFont.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,42 @@ 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] {
let ctFont = CTFontCreateWithGraphicsFont(cgFont, size, nil, nil)
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")
}
Expand Down
7 changes: 7 additions & 0 deletions Tests/SwiftMathTests/MathFontTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down

0 comments on commit 40a6896

Please sign in to comment.