Minor optimization for caching platform fonts
This commit is contained in:
parent
444a47327c
commit
68e43131b7
|
@ -19,7 +19,8 @@ private typealias PlatformFont = NSFont
|
||||||
|
|
||||||
public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
||||||
private let configuration: AttributedStringConverterConfiguration
|
private let configuration: AttributedStringConverterConfiguration
|
||||||
private var fontCache: [FontTrait: PlatformFont] = [:]
|
// There are only 8 combinations of FontTraits
|
||||||
|
private var fontCache: [PlatformFont?] = Array(repeating: nil, count: 8)
|
||||||
|
|
||||||
private var tokenizer: Tokenizer<String.UnicodeScalarView.Iterator>!
|
private var tokenizer: Tokenizer<String.UnicodeScalarView.Iterator>!
|
||||||
private var str: NSMutableAttributedString!
|
private var str: NSMutableAttributedString!
|
||||||
|
@ -340,7 +341,7 @@ public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getFont(traits: FontTrait) -> PlatformFont? {
|
private func getFont(traits: FontTrait) -> PlatformFont? {
|
||||||
if let cached = fontCache[traits] {
|
if let cached = fontCache[traits.rawValue] {
|
||||||
return cached
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,14 +356,18 @@ public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
|
||||||
} else if traits.contains(.italic),
|
} else if traits.contains(.italic),
|
||||||
let italic = descriptor.withSymbolicTraits(.traitItalic) {
|
let italic = descriptor.withSymbolicTraits(.traitItalic) {
|
||||||
descriptor = italic
|
descriptor = italic
|
||||||
|
} else {
|
||||||
|
// N.B.: this does not go through the UIFontMetrics, the configuration fonts are expected to be setup for Dynamic Type already
|
||||||
|
fontCache[traits.rawValue] = baseFont
|
||||||
|
return baseFont
|
||||||
}
|
}
|
||||||
let font = PlatformFont(descriptor: descriptor, size: 0)
|
let font = PlatformFont(descriptor: descriptor, size: 0)
|
||||||
#if os(iOS) || os(visionOS)
|
#if os(iOS) || os(visionOS)
|
||||||
let scaled = configuration.fontMetrics.scaledFont(for: font)
|
let scaled = configuration.fontMetrics.scaledFont(for: font)
|
||||||
fontCache[traits] = scaled
|
fontCache[traits.rawValue] = scaled
|
||||||
return scaled
|
return scaled
|
||||||
#else
|
#else
|
||||||
fontCache[traits] = font
|
fontCache[traits.rawValue] = font
|
||||||
return font
|
return font
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue