Compare commits

..

No commits in common. "68e43131b7fbf6cabfba540ed15798df378d9997" and "e01664565fa9eaa9e6aeb6a7438f5d2c7d666879" have entirely different histories.

2 changed files with 4 additions and 27 deletions

View File

@ -19,8 +19,7 @@ private typealias PlatformFont = NSFont
public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> { public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
private let configuration: AttributedStringConverterConfiguration private let configuration: AttributedStringConverterConfiguration
// There are only 8 combinations of FontTraits private var fontCache: [FontTrait: PlatformFont] = [:]
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!
@ -341,7 +340,7 @@ public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
} }
private func getFont(traits: FontTrait) -> PlatformFont? { private func getFont(traits: FontTrait) -> PlatformFont? {
if let cached = fontCache[traits.rawValue] { if let cached = fontCache[traits] {
return cached return cached
} }
@ -356,20 +355,10 @@ 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) fontCache[traits] = font
let scaled = configuration.fontMetrics.scaledFont(for: font)
fontCache[traits.rawValue] = scaled
return scaled
#else
fontCache[traits.rawValue] = font
return font return font
#endif
} }
} }
@ -377,7 +366,6 @@ public struct AttributedStringConverterConfiguration {
#if os(iOS) || os(visionOS) #if os(iOS) || os(visionOS)
public var font: UIFont public var font: UIFont
public var monospaceFont: UIFont public var monospaceFont: UIFont
public var fontMetrics: UIFontMetrics
public var color: UIColor public var color: UIColor
#elseif os(macOS) #elseif os(macOS)
public var font: NSFont public var font: NSFont
@ -387,10 +375,9 @@ public struct AttributedStringConverterConfiguration {
public var paragraphStyle: NSParagraphStyle public var paragraphStyle: NSParagraphStyle
#if os(iOS) || os(visionOS) #if os(iOS) || os(visionOS)
public init(font: UIFont, monospaceFont: UIFont, fontMetrics: UIFontMetrics, color: UIColor, paragraphStyle: NSParagraphStyle) { public init(font: UIFont, monospaceFont: UIFont, color: UIColor, paragraphStyle: NSParagraphStyle) {
self.font = font self.font = font
self.monospaceFont = monospaceFont self.monospaceFont = monospaceFont
self.fontMetrics = fontMetrics
self.color = color self.color = color
self.paragraphStyle = paragraphStyle self.paragraphStyle = paragraphStyle
} }

View File

@ -44,22 +44,12 @@ final class AttributedStringConverterTests: XCTestCase {
} }
private func convert<Callbacks: HTMLConversionCallbacks>(_ html: String, callbacks _: Callbacks.Type = Callbacks.self) -> NSAttributedString { private func convert<Callbacks: HTMLConversionCallbacks>(_ html: String, callbacks _: Callbacks.Type = Callbacks.self) -> NSAttributedString {
#if os(iOS) || os(visionOS)
let config = AttributedStringConverterConfiguration(
font: font,
monospaceFont: monospaceFont,
fontMetrics: .default,
color: color,
paragraphStyle: .default
)
#else
let config = AttributedStringConverterConfiguration( let config = AttributedStringConverterConfiguration(
font: font, font: font,
monospaceFont: monospaceFont, monospaceFont: monospaceFont,
color: color, color: color,
paragraphStyle: .default paragraphStyle: .default
) )
#endif
let converter = AttributedStringConverter<Callbacks>(configuration: config) let converter = AttributedStringConverter<Callbacks>(configuration: config)
return converter.convert(html: html) return converter.convert(html: html)
} }