summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/math
diff options
context:
space:
mode:
authorMax <max@mkor.je>2025-07-09 13:08:49 +0000
committerGitHub <noreply@github.com>2025-07-09 13:08:49 +0000
commitf9b01f595dba96044c725a97ecb4972bec7d57ed (patch)
tree55ec44d50cf50ab5099a8ee9e0eacee02b0efd08 /crates/typst-library/src/math
parenteed34070513e6060f86f86b56ba7071507fe811b (diff)
Move math styling to codex and add `math.scr` (#6309)
Diffstat (limited to 'crates/typst-library/src/math')
-rw-r--r--crates/typst-library/src/math/equation.rs7
-rw-r--r--crates/typst-library/src/math/mod.rs1
-rw-r--r--crates/typst-library/src/math/style.rs68
3 files changed, 42 insertions, 34 deletions
diff --git a/crates/typst-library/src/math/equation.rs b/crates/typst-library/src/math/equation.rs
index 0c9ba11d..a2ae5447 100644
--- a/crates/typst-library/src/math/equation.rs
+++ b/crates/typst-library/src/math/equation.rs
@@ -1,5 +1,6 @@
use std::num::NonZeroUsize;
+use codex::styling::MathVariant;
use typst_utils::NonZeroExt;
use unicode_math_class::MathClass;
@@ -12,7 +13,7 @@ use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{
AlignElem, Alignment, BlockElem, OuterHAlignment, SpecificAlignment, VAlignment,
};
-use crate::math::{MathSize, MathVariant};
+use crate::math::MathSize;
use crate::model::{Numbering, Outlinable, ParLine, Refable, Supplement};
use crate::text::{FontFamily, FontList, FontWeight, LocalName, TextElem};
@@ -111,7 +112,7 @@ pub struct EquationElem {
/// The style variant to select.
#[internal]
#[ghost]
- pub variant: MathVariant,
+ pub variant: Option<MathVariant>,
/// Affects the height of exponents.
#[internal]
@@ -128,7 +129,7 @@ pub struct EquationElem {
/// Whether to use italic glyphs.
#[internal]
#[ghost]
- pub italic: Smart<bool>,
+ pub italic: Option<bool>,
/// A forced class to use for all fragment.
#[internal]
diff --git a/crates/typst-library/src/math/mod.rs b/crates/typst-library/src/math/mod.rs
index 2e6d42b1..3d39e2fd 100644
--- a/crates/typst-library/src/math/mod.rs
+++ b/crates/typst-library/src/math/mod.rs
@@ -80,6 +80,7 @@ pub fn module() -> Module {
math.define_func::<italic>();
math.define_func::<serif>();
math.define_func::<sans>();
+ math.define_func::<scr>();
math.define_func::<cal>();
math.define_func::<frak>();
math.define_func::<mono>();
diff --git a/crates/typst-library/src/math/style.rs b/crates/typst-library/src/math/style.rs
index 53242e6e..6a85fd12 100644
--- a/crates/typst-library/src/math/style.rs
+++ b/crates/typst-library/src/math/style.rs
@@ -1,4 +1,6 @@
-use crate::foundations::{func, Cast, Content, Smart};
+use codex::styling::MathVariant;
+
+use crate::foundations::{func, Cast, Content};
use crate::math::EquationElem;
/// Bold font style in math.
@@ -24,7 +26,7 @@ pub fn upright(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::italic, Smart::Custom(false))
+ body.set(EquationElem::italic, Some(false))
}
/// Italic font style in math.
@@ -35,7 +37,7 @@ pub fn italic(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::italic, Smart::Custom(true))
+ body.set(EquationElem::italic, Some(true))
}
/// Serif (roman) font style in math.
@@ -46,7 +48,7 @@ pub fn serif(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Serif)
+ body.set(EquationElem::variant, Some(MathVariant::Plain))
}
/// Sans-serif font style in math.
@@ -59,23 +61,39 @@ pub fn sans(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Sans)
+ body.set(EquationElem::variant, Some(MathVariant::SansSerif))
}
-/// Calligraphic font style in math.
+/// Calligraphic (chancery) font style in math.
///
/// ```example
/// Let $cal(P)$ be the set of ...
/// ```
///
-/// This corresponds both to LaTeX's `\mathcal` and `\mathscr` as both of these
-/// styles share the same Unicode codepoints. Switching between the styles is
-/// thus only possible if supported by the font via
-/// [font features]($text.features).
+/// This is the default calligraphic/script style for most math fonts. See
+/// [`scr`]($math.scr) for more on how to get the other style (roundhand).
+#[func(title = "Calligraphic", keywords = ["mathcal", "chancery"])]
+pub fn cal(
+ /// The content to style.
+ body: Content,
+) -> Content {
+ body.set(EquationElem::variant, Some(MathVariant::Chancery))
+}
+
+/// Script (roundhand) font style in math.
+///
+/// ```example
+/// $ scr(S) $
+/// ```
+///
+/// There are two ways that fonts can support differentiating `cal` and `scr`.
+/// The first is using Unicode variation sequences. This works out of the box
+/// in Typst, however only a few math fonts currently support this.
///
-/// For the default math font, the roundhand style is available through the
-/// `ss01` feature. Therefore, you could define your own version of `\mathscr`
-/// like this:
+/// The other way is using [font features]($text.features). For example, the
+/// roundhand style might be available in a font through the `ss01` feature.
+/// To use it in Typst, you could then define your own version of `scr` like
+/// this:
///
/// ```example
/// #let scr(it) = text(
@@ -88,12 +106,12 @@ pub fn sans(
///
/// (The box is not conceptually necessary, but unfortunately currently needed
/// due to limitations in Typst's text style handling in math.)
-#[func(title = "Calligraphic", keywords = ["mathcal", "mathscr"])]
-pub fn cal(
+#[func(title = "Script Style", keywords = ["mathscr", "roundhand"])]
+pub fn scr(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Cal)
+ body.set(EquationElem::variant, Some(MathVariant::Roundhand))
}
/// Fraktur font style in math.
@@ -106,7 +124,7 @@ pub fn frak(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Frak)
+ body.set(EquationElem::variant, Some(MathVariant::Fraktur))
}
/// Monospace font style in math.
@@ -119,7 +137,7 @@ pub fn mono(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Mono)
+ body.set(EquationElem::variant, Some(MathVariant::Monospace))
}
/// Blackboard bold (double-struck) font style in math.
@@ -137,7 +155,7 @@ pub fn bb(
/// The content to style.
body: Content,
) -> Content {
- body.set(EquationElem::variant, MathVariant::Bb)
+ body.set(EquationElem::variant, Some(MathVariant::DoubleStruck))
}
/// Forced display style in math.
@@ -240,15 +258,3 @@ pub enum MathSize {
/// Math on its own line.
Display,
}
-
-/// A mathematical style variant, as defined by Unicode.
-#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Cast, Hash)]
-pub enum MathVariant {
- #[default]
- Serif,
- Sans,
- Cal,
- Frak,
- Mono,
- Bb,
-}