Skip to content

Commit 1ebf6ec

Browse files
committed
Styles and better preloading.
1 parent 0d6a913 commit 1ebf6ec

12 files changed

+501
-139
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
targets: ["LaTeXSwiftUI"]),
1616
],
1717
dependencies: [
18-
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.3.0"),
18+
.package(url: "https://github.com/colinc86/MathJaxSwift", from: "3.4.0"),
1919
.package(url: "https://github.com/exyte/SVGView", from: "1.0.4"),
2020
.package(url: "https://github.com/Kitura/swift-html-entities", from: "4.0.1")
2121
],

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ A SwiftUI view that renders LaTeX equations.
2424
- [Unencode HTML](#🔗-unencode-html)
2525
- [Rendering Style](#🕶️-rendering-style)
2626
- [Rendering Animation](#🪩-animated)
27+
- [Styles](#🪮-styles)
2728
- [Caching](#🗄️-caching)
2829
- [Preloading](#🏃‍♀️-preloading)
2930

@@ -47,7 +48,7 @@ It won't
4748
Add the dependency to your package manifest file.
4849

4950
```swift
50-
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.2.3")
51+
.package(url: "https://github.com/colinc86/LaTeXSwiftUI", from: "1.3.0")
5152
```
5253

5354
## ⌨️ Usage
@@ -249,6 +250,44 @@ LaTeX(input)
249250

250251
> In the above example, the input text will be displayed until the SVGs have been rendered at which point the rendered views will animate in to view.
251252
253+
### 🪮 Styles
254+
255+
You can use the provided view styles or create your own.
256+
257+
```swift
258+
// The default view style.
259+
LaTeX(input)
260+
.latexStyle(.automatic)
261+
262+
// A "standard" style with HTML elements unencoded and block equations numbered.
263+
LaTeX(input)
264+
.latexStyle(.standard)
265+
```
266+
267+
To create your own style, conform to the `LaTeXStyle` protocol. Its `makeBody(content:)` method takes a `LaTeX` view and returns a stylized version of the view.
268+
269+
The following would create a style for the first title used at the [top](#latexswiftui) of this README.
270+
271+
```swift
272+
@available(iOS 16.1, *)
273+
public struct TitleLaTeXStyle: LaTeXStyle {
274+
275+
public func makeBody(content: LaTeX) -> some View {
276+
content
277+
.fontDesign(.serif)
278+
.font(.largeTitle)
279+
.foregroundStyle(
280+
LinearGradient(
281+
colors: [.red, .orange, .yellow, .green, .blue, .indigo, .purple],
282+
startPoint: .leading,
283+
endPoint: .trailing
284+
)
285+
)
286+
}
287+
288+
}
289+
```
290+
252291
### 🗄️ Caching
253292

254293
`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.

Sources/LaTeXSwiftUI/LaTeX.swift

Lines changed: 21 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public struct LaTeX: View {
5050
case blockViews
5151
}
5252

53+
/// The view's equation number mode.
5354
public enum EquationNumberMode {
5455

5556
/// The view should not number named block equations.
@@ -85,6 +86,7 @@ public struct LaTeX: View {
8586
case onlyEquations
8687
}
8788

89+
/// The view's rendering style.
8890
public enum RenderingStyle {
8991

9092
/// The view remains empty until its finished rendering.
@@ -159,6 +161,9 @@ public struct LaTeX: View {
159161
/// The view's renderer.
160162
@StateObject private var renderer: Renderer
161163

164+
/// The view's preload task, if any.
165+
@State private var preloadTask: Task<(), Never>?
166+
162167
// MARK: Initializers
163168

164169
/// Initializes a view with a LaTeX input string.
@@ -195,6 +200,7 @@ public struct LaTeX: View {
195200
}
196201
.animation(renderingAnimation, value: renderer.rendered)
197202
.environmentObject(renderer)
203+
.onDisappear(perform: preloadTask?.cancel)
198204
}
199205

200206
}
@@ -205,10 +211,19 @@ extension LaTeX {
205211

206212
/// Preloads the view's SVG and image data.
207213
public func preload() {
208-
Task {
209-
await renderAsync()
210-
}
214+
preloadTask?.cancel()
215+
preloadTask = Task { await renderAsync() }
216+
Task { await preloadTask?.value }
217+
}
218+
219+
/// Configures the `LaTeX` view with the given style.
220+
///
221+
/// - Parameter style: The `LaTeX` view style to use.
222+
/// - Returns: A stylized view.
223+
public func latexStyle<S>(_ style: S) -> some View where S: LaTeXStyle {
224+
style.makeBody(content: self)
211225
}
226+
212227
}
213228

214229
// MARK: Private methods
@@ -271,6 +286,9 @@ extension LaTeX {
271286
}
272287
}
273288

289+
/// The view to display while its content is rendering.
290+
///
291+
/// - Returns: The view's body.
274292
@MainActor @ViewBuilder private func loadingView() -> some View {
275293
switch renderingStyle {
276294
case .empty:
@@ -285,135 +303,3 @@ extension LaTeX {
285303
}
286304

287305
}
288-
289-
@available(iOS 16.1, *)
290-
struct LaTeX_Previews: PreviewProvider {
291-
static var previews: some View {
292-
VStack {
293-
LaTeX("Hello, $\\LaTeX$!")
294-
.font(.largeTitle)
295-
.foregroundStyle(
296-
LinearGradient(
297-
colors: [.red, .orange, .yellow, .green, .blue, .indigo, .purple],
298-
startPoint: .leading,
299-
endPoint: .trailing
300-
)
301-
)
302-
303-
LaTeX("Hello, $\\LaTeX$!")
304-
.font(.title)
305-
.foregroundColor(.red)
306-
307-
LaTeX("Hello, $\\LaTeX$!")
308-
.font(.title2)
309-
.foregroundColor(.orange)
310-
311-
LaTeX("Hello, $\\LaTeX$!")
312-
.font(.title3)
313-
.foregroundColor(.yellow)
314-
315-
LaTeX("Hello, $\\LaTeX$!")
316-
.font(.body)
317-
.foregroundColor(.green)
318-
319-
LaTeX("Hello, $\\LaTeX$!")
320-
.font(.caption)
321-
.foregroundColor(.indigo)
322-
323-
LaTeX("Hello, $\\LaTeX$!")
324-
.font(.caption2)
325-
.foregroundColor(.purple)
326-
}
327-
.fontDesign(.serif)
328-
.previewLayout(.sizeThatFits)
329-
.previewDisplayName("Hello, LaTeX!")
330-
331-
VStack {
332-
LaTeX("Hello, $\\color{blue}\\LaTeX$")
333-
.imageRenderingMode(.original)
334-
335-
LaTeX("Hello, $\\LaTeX$")
336-
.imageRenderingMode(.template)
337-
}
338-
.previewDisplayName("Image Rendering Mode")
339-
340-
VStack {
341-
LaTeX("$\\asdf$")
342-
.errorMode(.error)
343-
344-
LaTeX("$\\asdf$")
345-
.errorMode(.original)
346-
347-
LaTeX("$\\asdf$")
348-
.errorMode(.rendered)
349-
}
350-
.previewDisplayName("Error Mode")
351-
352-
VStack {
353-
LaTeX("$x&lt;0$")
354-
.errorMode(.error)
355-
356-
LaTeX("$x&lt;0$")
357-
.unencoded()
358-
.errorMode(.error)
359-
}
360-
.previewDisplayName("Unencoded")
361-
362-
VStack {
363-
LaTeX("$a^2 + b^2 = c^2$")
364-
.parsingMode(.onlyEquations)
365-
366-
LaTeX("a^2 + b^2 = c^2")
367-
.parsingMode(.all)
368-
}
369-
.previewDisplayName("Parsing Mode")
370-
371-
VStack {
372-
LaTeX("Equation 1: $$x = 3$$")
373-
.blockMode(.blockViews)
374-
375-
LaTeX("Equation 1: $$x = 3$$")
376-
.blockMode(.blockText)
377-
378-
LaTeX("Equation 1: $$x = 3$$")
379-
.blockMode(.alwaysInline)
380-
}
381-
.previewDisplayName("Block Mode")
382-
383-
VStack {
384-
LaTeX("$$E = mc^2$$")
385-
.equationNumberMode(.right)
386-
.equationNumberOffset(10)
387-
.padding([.bottom])
388-
389-
LaTeX("\\begin{equation} E = mc^2 \\end{equation} \\begin{equation} E = mc^2 \\end{equation}")
390-
.equationNumberMode(.right)
391-
.equationNumberOffset(10)
392-
.equationNumberStart(2)
393-
}
394-
.fontDesign(.serif)
395-
.previewLayout(.sizeThatFits)
396-
.previewDisplayName("Equation Numbers")
397-
.formatEquationNumber { n in
398-
return "~[\(n)]~"
399-
}
400-
401-
VStack {
402-
LaTeX("Hello, $\\LaTeX$!")
403-
.renderingStyle(.wait)
404-
405-
LaTeX("Hello, $\\LaTeX$!")
406-
.renderingStyle(.empty)
407-
408-
LaTeX("Hello, $\\LaTeX$!")
409-
.renderingStyle(.original)
410-
.renderingAnimation(.default)
411-
412-
LaTeX("Hello, $\\LaTeX$!")
413-
.renderingStyle(.progress)
414-
.renderingAnimation(.easeIn)
415-
}
416-
.previewDisplayName("Rendering Style and Animated")
417-
}
418-
419-
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// LaTeX_Previews+Color.swift
3+
// LaTeXSwiftUI
4+
//
5+
// Copyright (c) 2023 Colin Campbell
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to
9+
// deal in the Software without restriction, including without limitation the
10+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11+
// sell copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in
15+
// all copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23+
// IN THE SOFTWARE.
24+
//
25+
26+
import SwiftUI
27+
28+
struct LaTeX_Previews_Color: PreviewProvider {
29+
30+
static var previews: some View {
31+
VStack {
32+
LaTeX("Hello, $\\LaTeX$!")
33+
.font(.largeTitle)
34+
.foregroundStyle(
35+
LinearGradient(
36+
colors: [.red, .orange, .yellow, .green, .blue, .indigo, .purple],
37+
startPoint: .leading,
38+
endPoint: .trailing
39+
)
40+
)
41+
42+
LaTeX("Hello, $\\LaTeX$!")
43+
.font(.title)
44+
.foregroundColor(.red)
45+
46+
LaTeX("Hello, $\\LaTeX$!")
47+
.font(.title2)
48+
.foregroundColor(.orange)
49+
50+
LaTeX("Hello, $\\LaTeX$!")
51+
.font(.title3)
52+
.foregroundColor(.yellow)
53+
54+
LaTeX("Hello, $\\LaTeX$!")
55+
.font(.body)
56+
.foregroundColor(.green)
57+
58+
LaTeX("Hello, $\\LaTeX$!")
59+
.font(.caption)
60+
.foregroundColor(.indigo)
61+
62+
LaTeX("Hello, $\\LaTeX$!")
63+
.font(.caption2)
64+
.foregroundColor(.purple)
65+
}
66+
.previewLayout(.sizeThatFits)
67+
.previewDisplayName("Hello, LaTeX!")
68+
}
69+
70+
}

0 commit comments

Comments
 (0)