From 1c461041c1c5fe950b44c8e6981efa34e99fb385 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 26 Nov 2023 18:53:59 -0500 Subject: [PATCH] Cache fonts in attributed string converter --- .../AttributedStringConverter.swift | 84 ++++++++++++------- .../AttributedStringConverterTests.swift | 4 +- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/Sources/HTMLStreamer/AttributedStringConverter.swift b/Sources/HTMLStreamer/AttributedStringConverter.swift index 14edbb3..9da5463 100644 --- a/Sources/HTMLStreamer/AttributedStringConverter.swift +++ b/Sources/HTMLStreamer/AttributedStringConverter.swift @@ -11,26 +11,40 @@ import UIKit import AppKit #endif +#if os(iOS) +private typealias PlatformFont = UIFont +#elseif os(macOS) +private typealias PlatformFont = NSFont +#endif + public struct AttributedStringConverter { private let configuration: AttributedStringConverterConfiguration - private var tokenizer: Tokenizer - private let str = NSMutableAttributedString() + private var fontCache: [FontTrait: PlatformFont] = [:] + + private var tokenizer: Tokenizer! + private var str: NSMutableAttributedString! private var actionStack: InlineArray3 = [] private var styleStack: InlineArray3