corrected some variable names within BundleManager functions, added onDemandBundle registration instead of loading all Resources at startup.

This commit is contained in:
Peter Tang
2023-09-15 17:06:06 +08:00
parent 2cdab9cc0e
commit 40a6896889
2 changed files with 28 additions and 8 deletions

View File

@@ -126,16 +126,28 @@ private class BundleManager {
initializedOnceAlready.toggle() initializedOnceAlready.toggle()
} }
fileprivate func obtainCGFont(font: MathFont) -> CGFont { private func onDemandRegistration(mathFont: MathFont) {
if !initializedOnceAlready { registerAllBundleResources() } guard cgFonts[mathFont] == nil else { return }
guard let cfFont = cgFonts[font] else { do {
fatalError("\(#function) unable to locate CTFont \(font.fontName)") try BundleManager.manager.registerCGFont(mathFont: mathFont)
try BundleManager.manager.registerMathTable(mathFont: mathFont)
} catch {
fatalError("MTMathFonts:\(#function) ondemand loading failed, mathFont \(mathFont.rawValue), reason \(error)")
} }
return cfFont }
fileprivate func obtainCGFont(font: MathFont) -> CGFont {
// if !initializedOnceAlready { registerAllBundleResources() }
onDemandRegistration(mathFont: font)
guard let cgFont = cgFonts[font] else {
fatalError("\(#function) unable to locate CGFont \(font.fontName)")
}
return cgFont
} }
fileprivate func obtainCTFont(font: MathFont, withSize size: CGFloat) -> CTFont { 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) let fontPair = CTFontPair(font: font, size: size)
guard let ctFont = ctFonts[fontPair] else { guard let ctFont = ctFonts[fontPair] else {
if let cgFont = cgFonts[font] { if let cgFont = cgFonts[font] {
@@ -143,12 +155,13 @@ private class BundleManager {
ctFonts[fontPair] = ctFont ctFonts[fontPair] = ctFont
return ctFont return ctFont
} }
fatalError("\(#function) unable to locate CTFont \(font.fontName)") fatalError("\(#function) unable to locate CGFont \(font.fontName), nor create CTFont")
} }
return ctFont return ctFont
} }
fileprivate func obtainMathTable(font: MathFont) -> NSDictionary { fileprivate func obtainMathTable(font: MathFont) -> NSDictionary {
if !initializedOnceAlready { registerAllBundleResources() } // if !initializedOnceAlready { registerAllBundleResources() }
onDemandRegistration(mathFont: font)
guard let mathTable = mathTables[font] else { guard let mathTable = mathTables[font] else {
fatalError("\(#function) unable to locate mathTable: \(font.rawValue).plist") fatalError("\(#function) unable to locate mathTable: \(font.rawValue).plist")
} }

View File

@@ -37,6 +37,13 @@ final class MathFontTests: XCTestCase {
} }
#endif #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] { var fontNames: [String] {
MathFont.allCases.map { $0.fontName } MathFont.allCases.map { $0.fontName }
} }