Skip to content

Commit

Permalink
Merge pull request #15 from colinc86/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
colinc86 authored Jun 11, 2023
2 parents 383ce8f + 0a18295 commit be34fcb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 46 deletions.
9 changes: 0 additions & 9 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -26,7 +25,6 @@ let package = Package(
dependencies: [
"MathJaxSwift",
"SVGView",
"Nuke",
.product(name: "HTMLEntities", package: "swift-html-entities")
]),
.testTarget(
Expand Down
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions Sources/LaTeXSwiftUI/LaTeX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NSString, NSData> {
Renderer.shared.dataCache
}

#if os(macOS)
/// The package's shared image cache.
public static var imageCache: ImageCache {
public static var imageCache: NSCache<NSString, NSImage> {
Renderer.shared.imageCache
}
#else
/// The package's shared image cache.
public static var imageCache: NSCache<NSString, UIImage> {
Renderer.shared.imageCache
}
#endif


// MARK: Public properties

Expand Down
7 changes: 0 additions & 7 deletions Sources/LaTeXSwiftUI/Models/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,14 @@ 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) })
}

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
Expand Down
24 changes: 6 additions & 18 deletions Sources/LaTeXSwiftUI/Models/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import CryptoKit
import Foundation
import MathJaxSwift
import Nuke
import SwiftUI
import SVGView

Expand Down Expand Up @@ -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
Expand All @@ -97,13 +95,13 @@ internal class Renderer {
private let mathjax: MathJax?

/// The renderer's data cache.
internal let dataCache: DataCache?
internal let dataCache: NSCache<NSString, NSData> = 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<NSString, _Image> = NSCache()

/// Semaphore for thread-safe access to `imageCache`.
internal let imageCacheSemaphore = DispatchSemaphore(value: 1)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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.
Expand All @@ -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()
}

Expand All @@ -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.
Expand All @@ -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()
}

Expand Down

0 comments on commit be34fcb

Please sign in to comment.