threadsafe MathFont, MTFontV2, MTFontMathTableV2 with concurrent testScripts.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// MTFontV2.swift
|
||||
//
|
||||
//
|
||||
//
|
||||
// Created by Peter Tang on 15/9/2023.
|
||||
//
|
||||
@@ -17,17 +17,18 @@ extension MathFont {
|
||||
public final class MTFontV2: MTFont {
|
||||
let font: MathFont
|
||||
let size: CGFloat
|
||||
private lazy var _cgFont: CGFont = {
|
||||
font.cgFont()
|
||||
}()
|
||||
private lazy var _ctFont: CTFont = {
|
||||
font.ctFont(withSize: size)
|
||||
}()
|
||||
private lazy var _mathTab = MTFontMathTableV2(mathFont: font, size: size)
|
||||
private let _cgFont: CGFont
|
||||
private let _ctFont: CTFont
|
||||
private let unitsPerEm: UInt
|
||||
private var _mathTab: MTFontMathTableV2?
|
||||
init(font: MathFont = .latinModernFont, size: CGFloat) {
|
||||
self.font = font
|
||||
self.size = size
|
||||
|
||||
// MathFont cgfont and ctfont are fast & threadsafe, keep a local copy is cheaper than
|
||||
// handling via NSLock
|
||||
self._cgFont = font.cgFont()
|
||||
self._ctFont = font.ctFont(withSize: size)
|
||||
self.unitsPerEm = self._ctFont.unitsPerEm
|
||||
super.init()
|
||||
|
||||
super.defaultCGFont = nil
|
||||
@@ -43,9 +44,19 @@ public final class MTFontV2: MTFont {
|
||||
set { fatalError("\(#function): change to \(font.fontName) not allowed.") }
|
||||
get { _ctFont }
|
||||
}
|
||||
private let mtfontV2LockOnMathTable = NSLock()
|
||||
override var mathTable: MTFontMathTable? {
|
||||
set { fatalError("\(#function): change to \(font.rawValue) not allowed.") }
|
||||
get { _mathTab }
|
||||
get {
|
||||
guard _mathTab == nil else { return _mathTab }
|
||||
//Note: lazy _mathTab initialization is now threadsafe.
|
||||
mtfontV2LockOnMathTable.lock()
|
||||
defer { mtfontV2LockOnMathTable.unlock() }
|
||||
if _mathTab == nil {
|
||||
_mathTab = MTFontMathTableV2(mathFont: font, size: size, unitsPerEm: unitsPerEm)
|
||||
}
|
||||
return _mathTab
|
||||
}
|
||||
}
|
||||
override var rawMathTable: NSDictionary? {
|
||||
set { fatalError("\(#function): change to \(font.rawValue) not allowed.") }
|
||||
|
||||
Reference in New Issue
Block a user