typst_library/math/equation.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
use std::num::NonZeroUsize;
use typst_utils::NonZeroExt;
use unicode_math_class::MathClass;
use crate::diag::SourceResult;
use crate::engine::Engine;
use crate::foundations::{
elem, Content, NativeElement, Packed, Show, ShowSet, Smart, StyleChain, Styles,
Synthesize,
};
use crate::introspection::{Count, Counter, CounterUpdate, Locatable};
use crate::layout::{
AlignElem, Alignment, BlockElem, InlineElem, OuterHAlignment, SpecificAlignment,
VAlignment,
};
use crate::math::{MathSize, MathVariant};
use crate::model::{Numbering, Outlinable, ParLine, Refable, Supplement};
use crate::text::{FontFamily, FontList, FontWeight, LocalName, TextElem};
/// A mathematical equation.
///
/// Can be displayed inline with text or as a separate block. An equation
/// becomes block-level through the presence of at least one space after the
/// opening dollar sign and one space before the closing dollar sign.
///
/// # Example
/// ```example
/// #set text(font: "New Computer Modern")
///
/// Let $a$, $b$, and $c$ be the side
/// lengths of right-angled triangle.
/// Then, we know that:
/// $ a^2 + b^2 = c^2 $
///
/// Prove by induction:
/// $ sum_(k=1)^n k = (n(n+1)) / 2 $
/// ```
///
/// By default, block-level equations will not break across pages. This can be
/// changed through `{show math.equation: set block(breakable: true)}`.
///
/// # Syntax
/// This function also has dedicated syntax: Write mathematical markup within
/// dollar signs to create an equation. Starting and ending the equation with at
/// least one space lifts it into a separate block that is centered
/// horizontally. For more details about math syntax, see the
/// [main math page]($category/math).
#[elem(Locatable, Synthesize, Show, ShowSet, Count, LocalName, Refable, Outlinable)]
pub struct EquationElem {
/// Whether the equation is displayed as a separate block.
#[default(false)]
pub block: bool,
/// How to [number]($numbering) block-level equations.
///
/// ```example
/// #set math.equation(numbering: "(1)")
///
/// We define:
/// $ phi.alt := (1 + sqrt(5)) / 2 $ <ratio>
///
/// With @ratio, we get:
/// $ F_n = floor(1 / sqrt(5) phi.alt^n) $
/// ```
#[borrowed]
pub numbering: Option<Numbering>,
/// The alignment of the equation numbering.
///
/// By default, the alignment is `{end + horizon}`. For the horizontal
/// component, you can use `{right}`, `{left}`, or `{start}` and `{end}`
/// of the text direction; for the vertical component, you can use
/// `{top}`, `{horizon}`, or `{bottom}`.
///
/// ```example
/// #set math.equation(numbering: "(1)", number-align: bottom)
///
/// We can calculate:
/// $ E &= sqrt(m_0^2 + p^2) \
/// &approx 125 "GeV" $
/// ```
#[default(SpecificAlignment::Both(OuterHAlignment::End, VAlignment::Horizon))]
pub number_align: SpecificAlignment<OuterHAlignment, VAlignment>,
/// A supplement for the equation.
///
/// For references to equations, this is added before the referenced number.
///
/// If a function is specified, it is passed the referenced equation and
/// should return content.
///
/// ```example
/// #set math.equation(numbering: "(1)", supplement: [Eq.])
///
/// We define:
/// $ phi.alt := (1 + sqrt(5)) / 2 $ <ratio>
///
/// With @ratio, we get:
/// $ F_n = floor(1 / sqrt(5) phi.alt^n) $
/// ```
pub supplement: Smart<Option<Supplement>>,
/// The contents of the equation.
#[required]
pub body: Content,
/// The size of the glyphs.
#[internal]
#[default(MathSize::Text)]
#[ghost]
pub size: MathSize,
/// The style variant to select.
#[internal]
#[ghost]
pub variant: MathVariant,
/// Affects the height of exponents.
#[internal]
#[default(false)]
#[ghost]
pub cramped: bool,
/// Whether to use bold glyphs.
#[internal]
#[default(false)]
#[ghost]
pub bold: bool,
/// Whether to use italic glyphs.
#[internal]
#[ghost]
pub italic: Smart<bool>,
/// A forced class to use for all fragment.
#[internal]
#[ghost]
pub class: Option<MathClass>,
/// Values of `scriptPercentScaleDown` and `scriptScriptPercentScaleDown`
/// respectively in the current font's MathConstants table.
#[internal]
#[default((70, 50))]
#[ghost]
pub script_scale: (i16, i16),
}
impl Synthesize for Packed<EquationElem> {
fn synthesize(
&mut self,
engine: &mut Engine,
styles: StyleChain,
) -> SourceResult<()> {
let supplement = match self.as_ref().supplement(styles) {
Smart::Auto => TextElem::packed(Self::local_name_in(styles)),
Smart::Custom(None) => Content::empty(),
Smart::Custom(Some(supplement)) => {
supplement.resolve(engine, styles, [self.clone().pack()])?
}
};
self.push_supplement(Smart::Custom(Some(Supplement::Content(supplement))));
Ok(())
}
}
impl Show for Packed<EquationElem> {
fn show(&self, engine: &mut Engine, styles: StyleChain) -> SourceResult<Content> {
if self.block(styles) {
Ok(BlockElem::multi_layouter(
self.clone(),
engine.routines.layout_equation_block,
)
.pack()
.spanned(self.span()))
} else {
Ok(InlineElem::layouter(self.clone(), engine.routines.layout_equation_inline)
.pack()
.spanned(self.span()))
}
}
}
impl ShowSet for Packed<EquationElem> {
fn show_set(&self, styles: StyleChain) -> Styles {
let mut out = Styles::new();
if self.block(styles) {
out.set(AlignElem::set_alignment(Alignment::CENTER));
out.set(BlockElem::set_breakable(false));
out.set(ParLine::set_numbering(None));
out.set(EquationElem::set_size(MathSize::Display));
} else {
out.set(EquationElem::set_size(MathSize::Text));
}
out.set(TextElem::set_weight(FontWeight::from_number(450)));
out.set(TextElem::set_font(FontList(vec![FontFamily::new(
"New Computer Modern Math",
)])));
out
}
}
impl Count for Packed<EquationElem> {
fn update(&self) -> Option<CounterUpdate> {
(self.block(StyleChain::default()) && self.numbering().is_some())
.then(|| CounterUpdate::Step(NonZeroUsize::ONE))
}
}
impl LocalName for Packed<EquationElem> {
const KEY: &'static str = "equation";
}
impl Refable for Packed<EquationElem> {
fn supplement(&self) -> Content {
// After synthesis, this should always be custom content.
match (**self).supplement(StyleChain::default()) {
Smart::Custom(Some(Supplement::Content(content))) => content,
_ => Content::empty(),
}
}
fn counter(&self) -> Counter {
Counter::of(EquationElem::elem())
}
fn numbering(&self) -> Option<&Numbering> {
(**self).numbering(StyleChain::default()).as_ref()
}
}
impl Outlinable for Packed<EquationElem> {
fn outlined(&self) -> bool {
self.block(StyleChain::default()) && self.numbering().is_some()
}
fn prefix(&self, numbers: Content) -> Content {
let supplement = self.supplement();
if !supplement.is_empty() {
supplement + TextElem::packed('\u{a0}') + numbers
} else {
numbers
}
}
fn body(&self) -> Content {
Content::empty()
}
}