From 024468cdddaa716b8c549af729dfd17c812a1c47 Mon Sep 17 00:00:00 2001 From: Peter Tang Date: Mon, 18 Sep 2023 21:54:45 +0800 Subject: [PATCH] threadsafe protect CTFontCreateWithGraphicsFont --- Sources/SwiftMath/MathBundle/MathFont.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftMath/MathBundle/MathFont.swift b/Sources/SwiftMath/MathBundle/MathFont.swift index 0b509cd..4bfd2b4 100644 --- a/Sources/SwiftMath/MathBundle/MathFont.swift +++ b/Sources/SwiftMath/MathBundle/MathFont.swift @@ -156,11 +156,15 @@ private class BundleManager { } //Note: ctfont creation and caching is now threadsafe. guard threadSafeQueue.sync(execute: { ctFonts[fontSizePair] }) == nil else { return ctFonts[fontSizePair]! } - let newCTFont = CTFontCreateWithGraphicsFont(cgFont, size, nil, nil) - threadSafeQueue.sync(flags: .barrier) { - ctFonts[fontSizePair] = newCTFont - } - return newCTFont + return threadSafeQueue.sync(flags: .barrier, execute: { + if let ctfont = ctFonts[fontSizePair] { + return ctfont + } else { + let result = CTFontCreateWithGraphicsFont(cgFont, size, nil, nil) + ctFonts[fontSizePair] = result + return result + } + }) } fileprivate func obtainRawMathTable(font: MathFont) -> NSDictionary { onDemandRegistration(mathFont: font)