Add documentation

This commit is contained in:
Guille Gonzalez
2026-01-11 06:53:55 +01:00
parent 339f20a34b
commit 8d32feb1bd
4 changed files with 63 additions and 0 deletions

View File

@@ -1,22 +1,30 @@
import SwiftUI import SwiftUI
extension Math { extension Math {
/// Identifies a math font and size used for typesetting.
public struct Font: Hashable, Sendable { public struct Font: Hashable, Sendable {
/// Known math font names bundled with the package.
public struct Name: Hashable, Sendable, RawRepresentable, ExpressibleByStringLiteral { public struct Name: Hashable, Sendable, RawRepresentable, ExpressibleByStringLiteral {
/// The raw font name used in the bundle.
public let rawValue: String public let rawValue: String
/// Creates a name from a raw font string.
public init(rawValue: String) { public init(rawValue: String) {
self.rawValue = rawValue self.rawValue = rawValue
} }
/// Creates a name from a string literal.
public init(stringLiteral value: StringLiteralType) { public init(stringLiteral value: StringLiteralType) {
self.rawValue = value self.rawValue = value
} }
} }
/// The bundled font name.
public let name: Name public let name: Name
/// The font size in points.
public let size: CGFloat public let size: CGFloat
/// Creates a font configuration.
public init(name: Name, size: CGFloat) { public init(name: Name, size: CGFloat) {
self.name = name self.name = name
self.size = size self.size = size
@@ -25,21 +33,34 @@ extension Math {
} }
extension Math.Font.Name { extension Math.Font.Name {
/// Latin Modern Math.
public static let latinModern: Self = "latinmodern-math" public static let latinModern: Self = "latinmodern-math"
/// KpMath Light.
public static let kpMathLight: Self = "KpMath-Light" public static let kpMathLight: Self = "KpMath-Light"
/// KpMath Sans.
public static let kpMathSans: Self = "KpMath-Sans" public static let kpMathSans: Self = "KpMath-Sans"
/// XITS Math.
public static let xits: Self = "xits-math" public static let xits: Self = "xits-math"
/// TeX Gyre Termes Math.
public static let termes: Self = "texgyretermes-math" public static let termes: Self = "texgyretermes-math"
/// Asana Math.
public static let asana: Self = "Asana-Math" public static let asana: Self = "Asana-Math"
/// Euler Math.
public static let euler: Self = "Euler-Math" public static let euler: Self = "Euler-Math"
/// Fira Math.
public static let fira: Self = "FiraMath-Regular" public static let fira: Self = "FiraMath-Regular"
/// Noto Sans Math.
public static let notoSans: Self = "NotoSansMath-Regular" public static let notoSans: Self = "NotoSansMath-Regular"
/// Libertinus Math.
public static let libertinus: Self = "LibertinusMath-Regular" public static let libertinus: Self = "LibertinusMath-Regular"
/// Garamond Math.
public static let garamond: Self = "Garamond-Math" public static let garamond: Self = "Garamond-Math"
/// Lete Sans Math.
public static let leteSans: Self = "LeteSansMath" public static let leteSans: Self = "LeteSansMath"
} }
extension View { extension View {
/// Sets the math font used by ``Math`` views in this hierarchy.
public func mathFont(_ font: Math.Font) -> some View { public func mathFont(_ font: Math.Font) -> some View {
environment(\.mathFont, font) environment(\.mathFont, font)
} }

View File

@@ -1,5 +1,36 @@
import SwiftUI import SwiftUI
/// Renders a LaTeX math expression using SwiftUI.
///
/// You can create a view with a LaTeX string; this is a good default for short expressions:
///
/// ```swift
/// Math("x^2 + y^2 = z^2")
/// ```
///
/// If you need inline vs display behavior, set the typesetting style:
///
/// ```swift
/// Math("\\int_0^1 x^2\\,dx = \\frac{1}{3}")
/// .mathTypesettingStyle(.text)
///
/// Math("\\frac{1}{2}+\\sqrt{2}")
/// .mathTypesettingStyle(.display)
/// ```
///
/// To customize the appearance, choose a bundled math font and size:
///
/// ```swift
/// Math("\\sum_{i=1}^{n}x_i")
/// .mathFont(Math.Font(name: .latinModern, size: 24))
/// ```
///
/// For multicolor expressions, enable multicolor mode and use hex colors:
///
/// ```swift
/// Math("\\color{#cc0000}{a}+\\color{#00aa00}{b}+\\color{#0000cc}{c}")
/// .mathRenderingMode(.multicolor)
/// ```
public struct Math: View { public struct Math: View {
@Environment(\.mathFont) private var font @Environment(\.mathFont) private var font
@Environment(\.mathTypesettingStyle) private var typesettingStyle @Environment(\.mathTypesettingStyle) private var typesettingStyle
@@ -7,6 +38,8 @@ public struct Math: View {
private let latex: String private let latex: String
/// Creates a math view that renders the given LaTeX string.
/// - Parameter latex: A LaTeX math expression.
public init(_ latex: String) { public init(_ latex: String) {
self.latex = latex self.latex = latex
} }

View File

@@ -1,10 +1,14 @@
import SwiftUI import SwiftUI
extension Math { extension Math {
/// Controls how colors are applied when rendering math.
public enum RenderingMode: Sendable { public enum RenderingMode: Sendable {
/// Draws all glyphs using the view's foreground style.
case monochrome case monochrome
/// Honors LaTeX color commands and uses the base color for uncolored glyphs.
case multicolor(base: SwiftUI.Color) case multicolor(base: SwiftUI.Color)
/// Multicolor rendering using the view's primary color as the base.
static var multicolor: Self { static var multicolor: Self {
.multicolor(base: .primary) .multicolor(base: .primary)
} }
@@ -12,6 +16,7 @@ extension Math {
} }
extension View { extension View {
/// Sets the rendering mode for ``Math`` views in this hierarchy.
public func mathRenderingMode(_ mathRenderingMode: Math.RenderingMode) -> some View { public func mathRenderingMode(_ mathRenderingMode: Math.RenderingMode) -> some View {
environment(\.mathRenderingMode, mathRenderingMode) environment(\.mathRenderingMode, mathRenderingMode)
} }

View File

@@ -1,13 +1,17 @@
import SwiftUI import SwiftUI
extension Math { extension Math {
/// Controls how math is typeset (display vs inline text).
public enum TypesettingStyle: Sendable { public enum TypesettingStyle: Sendable {
/// Display style for standalone equations.
case display case display
/// Text style for inline math.
case text case text
} }
} }
extension View { extension View {
/// Sets the typesetting style for ``Math`` views in this hierarchy.
public func mathTypesettingStyle(_ typesettingStyle: Math.TypesettingStyle) -> some View { public func mathTypesettingStyle(_ typesettingStyle: Math.TypesettingStyle) -> some View {
environment(\.mathTypesettingStyle, typesettingStyle) environment(\.mathTypesettingStyle, typesettingStyle)
} }