Repository Public
1// ORIX Docs2// File: App/Sources/Sheets/EquationPalette.swift3// Description: The equation editor's symbol and structure palette. Every candidate snippet is typeset once with the math renderer;4// only the ones it accepts are offered.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import DocMath1011/// One palette key: a LaTeX snippet in which `‸` marks the places the writer fills in.12nonisolated struct MathSnippet: Identifiable, Hashable, Sendable {13 static let marker = "\u{2038}"14 let latex: String15 var id: String { latex }1617 /// The snippet as inserted, without the markers.18 var source: String { latex.replacingOccurrences(of: MathSnippet.marker, with: "") }19 /// What is typeset on the key: the places to fill are shown as small squares.20 var sample: String { latex.replacingOccurrences(of: MathSnippet.marker, with: #"\square "#) }21 /// What is checked against the renderer: the snippet with a letter in each place to fill.22 var probe: String { latex.replacingOccurrences(of: MathSnippet.marker, with: "x") }23}2425nonisolated struct MathPaletteCategory: Identifiable, Sendable {26 let id: String27 let title: String28 let symbol: String29 /// Width of a key in points (structures need more room than single symbols).30 let keyWidth: Double31 var snippets: [MathSnippet]32}3334nonisolated enum EquationPalette {35 private static func items(_ text: String) -> [MathSnippet] {36 text.split(whereSeparator: \.isNewline).map { MathSnippet(latex: $0.trimmingCharacters(in: .whitespaces)) }.filter { !$0.latex.isEmpty }37 }3839 /// Every snippet considered, before the renderer has had its say.40 static let candidates: [MathPaletteCategory] = [41 MathPaletteCategory(id: "greek", title: String(localized: "Greek"), symbol: "character", keyWidth: 34, snippets: items(#"""42 \alpha43 \beta44 \gamma45 \delta46 \epsilon47 \varepsilon48 \zeta49 \eta50 \theta51 \vartheta52 \iota53 \kappa54 \varkappa55 \lambda56 \mu57 \nu58 \xi59 \pi60 \varpi61 \rho62 \varrho63 \sigma64 \varsigma65 \tau66 \upsilon67 \phi68 \varphi69 \chi70 \psi71 \omega72 \Gamma73 \Delta74 \Theta75 \Lambda76 \Xi77 \Pi78 \Sigma79 \Upsilon80 \Phi81 \Psi82 \Omega83 \digamma84 """#)),85 MathPaletteCategory(id: "operators", title: String(localized: "Operators"), symbol: "plus.forwardslash.minus", keyWidth: 34, snippets: items(#"""86 +87 -88 \pm89 \mp90 \times91 \div92 \cdot93 \ast94 \star95 \circ96 \bullet97 \oplus98 \ominus99 \otimes100 \oslash101 \odot102 \dagger103 \ddagger104 \wr105 \amalg106 \partial107 \nabla108 \infty109 \hbar110 \ell111 \Re112 \Im113 \aleph114 \wp115 \prime116 \angle117 \triangle118 \surd119 \top120 \bot121 \ldots122 \cdots123 \vdots124 \ddots125 \degree126 \%127 \sin128 \cos129 \tan130 \cot131 \sec132 \csc133 \arcsin134 \arccos135 \arctan136 \sinh137 \cosh138 \tanh139 \log140 \ln141 \lg142 \exp143 \det144 \dim145 \ker146 \gcd147 \deg148 \hom149 \arg150 \Pr151 \operatorname{‸}152 \bmod153 \pmod{‸}154 """#)),155 MathPaletteCategory(id: "relations", title: String(localized: "Relations"), symbol: "equal", keyWidth: 34, snippets: items(#"""156 =157 \neq158 <159 >160 \leq161 \geq162 \ll163 \gg164 \approx165 \sim166 \simeq167 \cong168 \equiv169 \propto170 \asymp171 \doteq172 \prec173 \succ174 \preceq175 \succeq176 \lesssim177 \gtrsim178 \leqslant179 \geqslant180 \nless181 \ngtr182 \nleq183 \ngeq184 \triangleq185 \coloneqq186 \mid187 \nmid188 \parallel189 \nparallel190 \perp191 \models192 \vdash193 \dashv194 \in195 \notin196 \ni197 \subset198 \supset199 \subseteq200 \supseteq201 \nsubseteq202 \subsetneq203 \sqsubseteq204 \cup205 \cap206 \setminus207 \emptyset208 \varnothing209 \forall210 \exists211 \nexists212 \neg213 \wedge214 \vee215 \therefore216 \because217 """#)),218 MathPaletteCategory(id: "arrows", title: String(localized: "Arrows"), symbol: "arrow.right", keyWidth: 40, snippets: items(#"""219 \leftarrow220 \rightarrow221 \leftrightarrow222 \Leftarrow223 \Rightarrow224 \Leftrightarrow225 \longleftarrow226 \longrightarrow227 \longleftrightarrow228 \Longleftarrow229 \Longrightarrow230 \Longleftrightarrow231 \uparrow232 \downarrow233 \updownarrow234 \Uparrow235 \Downarrow236 \Updownarrow237 \mapsto238 \longmapsto239 \to240 \gets241 \iff242 \implies243 \impliedby244 \nearrow245 \searrow246 \swarrow247 \nwarrow248 \hookleftarrow249 \hookrightarrow250 \leftharpoonup251 \rightharpoonup252 \leftharpoondown253 \rightharpoondown254 \rightleftharpoons255 \leadsto256 \nrightarrow257 \nleftarrow258 \xrightarrow{‸}259 \xleftarrow{‸}260 """#)),261 MathPaletteCategory(id: "fractions", title: String(localized: "Fractions and roots"), symbol: "x.squareroot", keyWidth: 52, snippets: items(#"""262 \frac{‸}{‸}263 \dfrac{‸}{‸}264 \tfrac{‸}{‸}265 \cfrac{‸}{‸}266 {‸ \over ‸}267 \binom{‸}{‸}268 {‸ \choose ‸}269 \sqrt{‸}270 \sqrt[‸]{‸}271 ‸^{‸}272 ‸_{‸}273 ‸_{‸}^{‸}274 {}^{‸}‸275 e^{‸}276 10^{‸}277 \frac{d ‸}{d ‸}278 \frac{\partial ‸}{\partial ‸}279 \frac{\partial^{2} ‸}{\partial ‸^{2}}280 \stackrel{‸}{‸}281 \overset{‸}{‸}282 \underset{‸}{‸}283 \substack{‸ \\ ‸}284 """#)),285 MathPaletteCategory(id: "sums", title: String(localized: "Sums, integrals and limits"), symbol: "sum", keyWidth: 60, snippets: items(#"""286 \sum287 \sum_{‸}^{‸}288 \sum_{i=1}^{n}289 \prod290 \prod_{‸}^{‸}291 \coprod292 \int293 \int_{‸}^{‸}294 \int_{-\infty}^{\infty}295 \iint296 \iiint297 \oint298 \bigcup299 \bigcap300 \bigcup_{‸}^{‸}301 \bigcap_{‸}^{‸}302 \bigoplus303 \bigotimes304 \bigodot305 \bigsqcup306 \biguplus307 \bigvee308 \bigwedge309 \lim_{‸ \to ‸}310 \lim_{n \to \infty}311 \limsup_{‸}312 \liminf_{‸}313 \max_{‸}314 \min_{‸}315 \sup_{‸}316 \inf_{‸}317 \sum\limits_{‸}^{‸}318 \int\nolimits_{‸}^{‸}319 \,d‸320 """#)),321 MathPaletteCategory(id: "matrices", title: String(localized: "Matrices"), symbol: "tablecells", keyWidth: 84, snippets: items(#"""322 \begin{matrix} ‸ & ‸ \\ ‸ & ‸ \end{matrix}323 \begin{pmatrix} ‸ & ‸ \\ ‸ & ‸ \end{pmatrix}324 \begin{bmatrix} ‸ & ‸ \\ ‸ & ‸ \end{bmatrix}325 \begin{Bmatrix} ‸ & ‸ \\ ‸ & ‸ \end{Bmatrix}326 \begin{vmatrix} ‸ & ‸ \\ ‸ & ‸ \end{vmatrix}327 \begin{Vmatrix} ‸ & ‸ \\ ‸ & ‸ \end{Vmatrix}328 \begin{pmatrix} ‸ \\ ‸ \end{pmatrix}329 \begin{pmatrix} ‸ & ‸ & ‸ \\ ‸ & ‸ & ‸ \\ ‸ & ‸ & ‸ \end{pmatrix}330 \begin{bmatrix} ‸ & ‸ & ‸ \\ ‸ & ‸ & ‸ \\ ‸ & ‸ & ‸ \end{bmatrix}331 \begin{smallmatrix} ‸ & ‸ \\ ‸ & ‸ \end{smallmatrix}332 \begin{cases} ‸ & ‸ \\ ‸ & ‸ \end{cases}333 \begin{aligned} ‸ &= ‸ \\ ‸ &= ‸ \end{aligned}334 \begin{gathered} ‸ \\ ‸ \end{gathered}335 \begin{split} ‸ &= ‸ \\ &= ‸ \end{split}336 \begin{array}{cc} ‸ & ‸ \\ ‸ & ‸ \end{array}337 \begin{eqnarray} ‸ &=& ‸ \\ ‸ &=& ‸ \end{eqnarray}338 """#)),339 MathPaletteCategory(id: "accents", title: String(localized: "Accents and fonts"), symbol: "textformat.alt", keyWidth: 46, snippets: items(#"""340 \hat{‸}341 \widehat{‸}342 \bar{‸}343 \overline{‸}344 \underline{‸}345 \tilde{‸}346 \widetilde{‸}347 \vec{‸}348 \dot{‸}349 \ddot{‸}350 \dddot{‸}351 \acute{‸}352 \grave{‸}353 \check{‸}354 \breve{‸}355 \mathring{‸}356 \overrightarrow{‸}357 \overleftarrow{‸}358 \overleftrightarrow{‸}359 \overbrace{‸}360 \underbrace{‸}361 \cancel{‸}362 \boxed{‸}363 \mathbb{‸}364 \mathcal{‸}365 \mathfrak{‸}366 \mathscr{‸}367 \mathbf{‸}368 \boldsymbol{‸}369 \mathrm{‸}370 \mathit{‸}371 \mathsf{‸}372 \mathtt{‸}373 \text{‸}374 \textbf{‸}375 \textit{‸}376 \mathbb{R}377 \mathbb{N}378 \mathbb{Z}379 \mathbb{Q}380 \mathbb{C}381 \mathbb{E}382 \mathbb{P}383 """#)),384 MathPaletteCategory(id: "delimiters", title: String(localized: "Delimiters"), symbol: "parentheses", keyWidth: 52, snippets: items(#"""385 (‸)386 [‸]387 \{‸\}388 |‸|389 \|‸\|390 \langle ‸ \rangle391 \lfloor ‸ \rfloor392 \lceil ‸ \rceil393 \left( ‸ \right)394 \left[ ‸ \right]395 \left\{ ‸ \right\}396 \left| ‸ \right|397 \left\| ‸ \right\|398 \left\langle ‸ \right\rangle399 \left\lfloor ‸ \right\rfloor400 \left\lceil ‸ \right\rceil401 \left. ‸ \right|_{‸}402 \left( ‸ \middle| ‸ \right)403 \bigl( ‸ \bigr)404 \Bigl( ‸ \Bigr)405 \biggl( ‸ \biggr)406 \lvert ‸ \rvert407 \lVert ‸ \rVert408 \ulcorner ‸ \urcorner409 \llbracket ‸ \rrbracket410 """#)),411 ]412413 /// True when the renderer both parses the snippet and typesets it without error.414 static func isSupported(_ snippet: MathSnippet) -> Bool {415 guard MathRenderer.validate(snippet.probe) == nil else { return false }416 if MathRenderer.layout(latex: snippet.probe, fontSize: 14, display: true).error != nil { return false }417 // The key face must typeset too; it only differs by the placeholder squares.418 return snippet.sample == snippet.latex || MathRenderer.validate(snippet.sample) == nil419 }420421 /// The palette that is shown: the candidates the renderer typesets. Computed once.422 static let supported: [MathPaletteCategory] = candidates.compactMap { category in423 var c = category424 c.snippets = category.snippets.filter(isSupported)425 return c.snippets.isEmpty ? nil : c426 }427428 /// Candidates the renderer rejects (listed for the record; never shown).429 static var rejected: [MathSnippet] { candidates.flatMap { $0.snippets.filter { !isSupported($0) } } }430}431