diff --git a/Package.resolved b/Package.resolved index 0d5e40c..7003d03 100644 --- a/Package.resolved +++ b/Package.resolved @@ -9,15 +9,6 @@ "version" : "3.3.0" } }, - { - "identity" : "nuke", - "kind" : "remoteSourceControl", - "location" : "https://github.com/kean/Nuke", - "state" : { - "revision" : "f4d9b95788679d0654c032961f73e7e9c16ca6b4", - "version" : "12.1.0" - } - }, { "identity" : "svgview", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index a2a92c4..9e27ff0 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,6 @@ let package = Package( dependencies: [ .package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.3.0"), .package(url: "https://github.com/exyte/SVGView", from: "1.0.4"), - .package(url: "https://github.com/kean/Nuke", from: "12.1.0"), .package(url: "https://github.com/Kitura/swift-html-entities", from: "4.0.1") ], targets: [ @@ -26,7 +25,6 @@ let package = Package( dependencies: [ "MathJaxSwift", "SVGView", - "Nuke", .product(name: "HTMLEntities", package: "swift-html-entities") ]), .testTarget( diff --git a/README.md b/README.md index 7910c4e..e2efa3b 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ It won't Add the dependency to your package manifest file. ```swift -.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.0") +.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.1") ``` ## ⌨️ Usage @@ -253,18 +253,14 @@ LaTeX(input) `LaTeXSwiftUI` caches its SVG responses from MathJax and the images rendered as a result of the view's environment. If you want to control the cache, then you can access the static `dataCache` and `imageCache` properties. -The caches are managed automatically, but if, for example, you wanted to clear the cache manually you may do so. - ```swift // Clear the SVG data cache. -LaTeX.dataCache?.removeAll() +LaTeX.dataCache.removeAllObjects() // Clear the rendered image cache. -LaTeX.imageCache.removeAll() +LaTeX.imageCache.removeAllObjects() ``` -`LaTeXSwiftUI` uses the [caching](https://github.com/kean/Nuke/tree/master/Sources/Nuke/Caching) components of the [Nuke](https://github.com/kean/Nuke) package. - ### 🏃‍♀️ Preloading SVGs and images are rendered and cached on demand, but there may be situations where you want to preload the data so that there is minimal lag when the view appears. diff --git a/Sources/LaTeXSwiftUI/LaTeX.swift b/Sources/LaTeXSwiftUI/LaTeX.swift index 794af8c..1866f22 100644 --- a/Sources/LaTeXSwiftUI/LaTeX.swift +++ b/Sources/LaTeXSwiftUI/LaTeX.swift @@ -25,7 +25,6 @@ import HTMLEntities import MathJaxSwift -import Nuke import SwiftUI /// A view that can parse and render TeX and LaTeX equations that contain @@ -104,14 +103,22 @@ public struct LaTeX: View { // MARK: Static properties /// The package's shared data cache. - public static var dataCache: DataCache? { + public static var dataCache: NSCache { Renderer.shared.dataCache } +#if os(macOS) /// The package's shared image cache. - public static var imageCache: ImageCache { + public static var imageCache: NSCache { Renderer.shared.imageCache } +#else + /// The package's shared image cache. + public static var imageCache: NSCache { + Renderer.shared.imageCache + } +#endif + // MARK: Public properties diff --git a/Sources/LaTeXSwiftUI/Models/Parser.swift b/Sources/LaTeXSwiftUI/Models/Parser.swift index 8f3b1f0..42f6e67 100644 --- a/Sources/LaTeXSwiftUI/Models/Parser.swift +++ b/Sources/LaTeXSwiftUI/Models/Parser.swift @@ -115,9 +115,7 @@ extension Parser { /// - Parameter input: The input string. /// - Returns: An array of LaTeX components. static func parse(_ input: String) -> [Component] { - print("parsing input \(input)") // Get the first match of each each equation type -// let matches = allEquations.map({ ($0, input.firstMatch(of: $0.regex)) }) let matchArrays = allEquations.map { equationComponent in let regexMatches = input.matches(of: equationComponent.regex) return regexMatches.map({ (equationComponent, $0) }) @@ -125,11 +123,6 @@ extension Parser { let matches = matchArrays.reduce([], +) - for match in matches { - let substring = input[match.1.range] - print("match: \(substring)") - } - // Filter the matches let filteredMatches = matches.filter { match in // We only want matches with ranges diff --git a/Sources/LaTeXSwiftUI/Models/Renderer.swift b/Sources/LaTeXSwiftUI/Models/Renderer.swift index 22600e7..fdd33ab 100644 --- a/Sources/LaTeXSwiftUI/Models/Renderer.swift +++ b/Sources/LaTeXSwiftUI/Models/Renderer.swift @@ -26,7 +26,6 @@ import CryptoKit import Foundation import MathJaxSwift -import Nuke import SwiftUI import SVGView @@ -83,7 +82,6 @@ internal class Renderer { let svg: SVG let xHeight: CGFloat internal var fallbackKey: String { String(data: svg.data, encoding: .utf8) ?? "" } - internal var nukeCacheKey: Nuke.ImageCacheKey { Nuke.ImageCacheKey(key: key()) } } // MARK: Static properties @@ -97,13 +95,13 @@ internal class Renderer { private let mathjax: MathJax? /// The renderer's data cache. - internal let dataCache: DataCache? + internal let dataCache: NSCache = NSCache() /// Semaphore for thread-safe access to `dataCache`. internal let dataCacheSemaphore = DispatchSemaphore(value: 1) /// The renderer's image cache. - internal let imageCache: ImageCache + internal let imageCache: NSCache = NSCache() /// Semaphore for thread-safe access to `imageCache`. internal let imageCacheSemaphore = DispatchSemaphore(value: 1) @@ -112,16 +110,6 @@ internal class Renderer { /// Initializes a renderer with a MathJax instance. init() { - do { - dataCache = try DataCache(name: "mathJaxRenderDataCache") - } - catch { - NSLog("Error creating DataCache instance: \(error)") - dataCache = nil - } - - imageCache = ImageCache() - do { mathjax = try MathJax(preferredOutputFormat: .svg) } @@ -275,7 +263,7 @@ extension Renderer { private func dataCacheValue(for key: SVGCacheKey) -> Data? { dataCacheSemaphore.wait() defer { dataCacheSemaphore.signal() } - return dataCache?[key.key()] + return dataCache.object(forKey: key.key() as NSString) as Data? } /// Safely sets the cache value. @@ -285,7 +273,7 @@ extension Renderer { /// - key: The value's key. private func setDataCacheValue(_ value: Data, for key: SVGCacheKey) { dataCacheSemaphore.wait() - dataCache?[key.key()] = value + dataCache.setObject(value as NSData, forKey: key.key() as NSString) dataCacheSemaphore.signal() } @@ -296,7 +284,7 @@ extension Renderer { private func imageCacheValue(for key: ImageCacheKey) -> _Image? { imageCacheSemaphore.wait() defer { imageCacheSemaphore.signal() } - return imageCache[key.nukeCacheKey]?.image + return imageCache.object(forKey: key.key() as NSString) } /// Safely sets the cache value. @@ -306,7 +294,7 @@ extension Renderer { /// - key: The value's key. private func setImageCacheValue(_ value: _Image, for key: ImageCacheKey) { imageCacheSemaphore.wait() - imageCache[key.nukeCacheKey] = ImageContainer(image: value) + imageCache.setObject(value, forKey: key.key() as NSString) imageCacheSemaphore.signal() }