Fix AttributedStringConverter producing non-Dynamic Type fonts

This commit is contained in:
Shadowfacts 2024-07-21 19:38:18 -07:00
parent e01664565f
commit 444a47327c
2 changed files with 19 additions and 1 deletions

View File

@ -357,8 +357,14 @@ public class AttributedStringConverter<Callbacks: HTMLConversionCallbacks> {
descriptor = italic descriptor = italic
} }
let font = PlatformFont(descriptor: descriptor, size: 0) let font = PlatformFont(descriptor: descriptor, size: 0)
#if os(iOS) || os(visionOS)
let scaled = configuration.fontMetrics.scaledFont(for: font)
fontCache[traits] = scaled
return scaled
#else
fontCache[traits] = font fontCache[traits] = font
return font return font
#endif
} }
} }
@ -366,6 +372,7 @@ 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
@ -375,9 +382,10 @@ 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, color: UIColor, paragraphStyle: NSParagraphStyle) { public init(font: UIFont, monospaceFont: UIFont, fontMetrics: UIFontMetrics, 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,12 +44,22 @@ 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)
} }