aboutsummaryrefslogtreecommitdiff
path: root/Sources/NorgUI/Math/NorgMathRenderer.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/NorgUI/Math/NorgMathRenderer.swift')
-rw-r--r--Sources/NorgUI/Math/NorgMathRenderer.swift46
1 files changed, 46 insertions, 0 deletions
diff --git a/Sources/NorgUI/Math/NorgMathRenderer.swift b/Sources/NorgUI/Math/NorgMathRenderer.swift
new file mode 100644
index 0000000..4fcf60f
--- /dev/null
+++ b/Sources/NorgUI/Math/NorgMathRenderer.swift
@@ -0,0 +1,46 @@
+import SwiftUI
+
+#if canImport(UIKit)
+ import UIKit
+ public typealias PlatformImage = UIImage
+#elseif canImport(AppKit)
+ import AppKit
+ public typealias PlatformImage = NSImage
+#endif
+
+public enum NorgMathMode: Sendable {
+ case inline
+ case block
+}
+
+public struct NorgMathImage {
+ public let image: PlatformImage
+ public let size: CGSize
+ public let descent: CGFloat
+
+ public init(image: PlatformImage, size: CGSize, descent: CGFloat) {
+ self.image = image
+ self.size = size
+ self.descent = descent
+ }
+
+ public var swiftUIImage: Image {
+ #if canImport(UIKit)
+ Image(uiImage: image)
+ #elseif canImport(AppKit)
+ Image(nsImage: image)
+ #endif
+ }
+}
+
+/// Renders LaTeX-style math into an image for display.
+public protocol NorgMathRenderer: Sendable {
+ /// Render `latex` at the given point size and colour.
+ ///
+ /// - Returns: the rendered image, or `nil` if the input could not be
+ /// rendered (the caller then keeps its text fallback).
+ @MainActor
+ func render(
+ latex: String, mode: NorgMathMode, fontSize: CGFloat, color: Color?
+ ) async -> NorgMathImage?
+}