From b09d6ae31c78463ad741644194c17ddd47c2bd56 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 26 Jan 2024 10:50:33 +0100 Subject: [PATCH] Refactor math styling to bring it closer to normal styling (#3262) --- crates/typst/src/foundations/styles.rs | 50 ++++- crates/typst/src/math/accent.rs | 30 +-- crates/typst/src/math/align.rs | 4 +- crates/typst/src/math/attach.rs | 119 +++++----- crates/typst/src/math/cancel.rs | 14 +- crates/typst/src/math/class.rs | 18 +- crates/typst/src/math/ctx.rs | 244 +++++++++------------ crates/typst/src/math/equation.rs | 48 +++- crates/typst/src/math/frac.rs | 72 +++--- crates/typst/src/math/fragment.rs | 178 ++++++++------- crates/typst/src/math/lr.rs | 35 +-- crates/typst/src/math/matrix.rs | 72 +++--- crates/typst/src/math/mod.rs | 61 +++--- crates/typst/src/math/op.rs | 14 +- crates/typst/src/math/root.rs | 40 ++-- crates/typst/src/math/row.rs | 38 ++-- crates/typst/src/math/spacing.rs | 5 +- crates/typst/src/math/stretch.rs | 18 +- crates/typst/src/math/style.rs | 289 ++++++------------------- crates/typst/src/math/underover.rs | 96 ++++---- tests/ref/math/class.png | Bin 7599 -> 7545 bytes tests/ref/math/equation-show.png | Bin 0 -> 2428 bytes tests/typ/math/equation-show.typ | 7 + 23 files changed, 698 insertions(+), 754 deletions(-) create mode 100644 tests/ref/math/equation-show.png create mode 100644 tests/typ/math/equation-show.typ diff --git a/crates/typst/src/foundations/styles.rs b/crates/typst/src/foundations/styles.rs index da8447c3a..f44a320f3 100644 --- a/crates/typst/src/foundations/styles.rs +++ b/crates/typst/src/foundations/styles.rs @@ -445,17 +445,16 @@ impl<'a> StyleChain<'a> { Self { head: &root.0, tail: None } } - /// Make the given style list the first link of this chain. + /// Make the given chainable the first link of this chain. /// /// The resulting style chain contains styles from `local` as well as /// `self`. The ones from `local` take precedence over the ones from /// `self`. For folded properties `local` contributes the inner value. - pub fn chain<'b>(&'b self, local: &'b Styles) -> StyleChain<'b> { - if local.is_empty() { - *self - } else { - StyleChain { head: &local.0, tail: Some(self) } - } + pub fn chain<'b, C>(&'b self, local: &'b C) -> StyleChain<'b> + where + C: Chainable, + { + Chainable::chain(local, self) } /// Cast the first value for the given property in the chain, @@ -630,6 +629,43 @@ impl PartialEq for StyleChain<'_> { } } +/// Things that can be attached to a style chain. +pub trait Chainable { + /// Attach `self` as the first link of the chain. + fn chain<'a>(&'a self, outer: &'a StyleChain<'_>) -> StyleChain<'a>; +} + +impl Chainable for Prehashed