summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-01-22 13:30:20 +0100
committerLaurenz <laurmaedje@gmail.com>2023-01-22 13:30:20 +0100
commit13cc16b3ccaa94cdf5dca2bf3195067d1d12f9b1 (patch)
tree9b13a3fc34b76370d9cdb691162cb9a8b94188ee
parentcfca1158045be4a3bfeaac5b12ac2aac182fa77c (diff)
Class-based math spacing
-rw-r--r--library/src/math/mod.rs5
-rw-r--r--library/src/math/spacing.rs36
2 files changed, 41 insertions, 0 deletions
diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs
index 73ce91ce..e3fc13f6 100644
--- a/library/src/math/mod.rs
+++ b/library/src/math/mod.rs
@@ -9,6 +9,7 @@ mod group;
mod matrix;
mod root;
mod script;
+mod spacing;
mod stretch;
mod style;
@@ -59,6 +60,10 @@ pub fn define(scope: &mut Scope) {
scope.def_func::<FrakNode>("frak");
scope.def_func::<MonoNode>("mono");
scope.def_func::<BbNode>("bb");
+ scope.define("thin", HNode::strong(THIN).pack());
+ scope.define("med", HNode::strong(MEDIUM).pack());
+ scope.define("thick", HNode::strong(THICK).pack());
+ scope.define("quad", HNode::strong(QUAD).pack());
}
/// # Math
diff --git a/library/src/math/spacing.rs b/library/src/math/spacing.rs
new file mode 100644
index 00000000..0f613309
--- /dev/null
+++ b/library/src/math/spacing.rs
@@ -0,0 +1,36 @@
+use super::*;
+
+pub(super) const ZERO: Em = Em::zero();
+pub(super) const THIN: Em = Em::new(1.0 / 6.0);
+pub(super) const MEDIUM: Em = Em::new(2.0 / 9.0);
+pub(super) const THICK: Em = Em::new(5.0 / 18.0);
+pub(super) const QUAD: Em = Em::new(1.0);
+
+/// Determine the spacing between two fragments in a given style.
+pub(super) fn spacing(left: MathClass, right: MathClass, style: MathStyle) -> Em {
+ use MathClass::*;
+ let script = style.size <= MathSize::Script;
+ match (left, right) {
+ // No spacing before punctuation; thin spacing after punctuation, unless
+ // in script size.
+ (_, Punctuation) => ZERO,
+ (Punctuation, _) if !script => THIN,
+
+ // No spacing after opening delimiters and before closing delimiters.
+ (Opening, _) | (_, Closing) => ZERO,
+
+ // Thick spacing around relations, unless followed by a another relation
+ // or in script size.
+ (Relation, Relation) => ZERO,
+ (Relation, _) | (_, Relation) if !script => THICK,
+
+ // Medium spacing around binary operators, unless in script size.
+ (Vary | Binary, _) | (_, Vary | Binary) if !script => MEDIUM,
+
+ // Thin spacing around large operators, unless next to a delimiter.
+ (Large, Opening | Fence) | (Closing | Fence, Large) => ZERO,
+ (Large, _) | (_, Large) => THIN,
+
+ _ => ZERO,
+ }
+}