Refactor #[elem] macro (#3303)

This commit is contained in:
Laurenz 2024-01-30 14:49:51 +01:00 committed by GitHub
parent f14288cacf
commit a1e8560ca6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 547 additions and 647 deletions

File diff suppressed because it is too large Load Diff

View File

@ -229,11 +229,17 @@ pub struct Property {
impl Property {
/// Create a new property from a key-value pair.
pub fn new<T>(elem: Element, id: u8, value: T) -> Self
pub fn new<E, T>(id: u8, value: T) -> Self
where
E: NativeElement,
T: Debug + Clone + Hash + Send + Sync + 'static,
{
Self { elem, id, value: Block::new(value), span: None }
Self {
elem: E::elem(),
id,
value: Block::new(value),
span: None,
}
}
/// Whether this property is the given one.
@ -245,6 +251,11 @@ impl Property {
pub fn is_of(&self, elem: Element) -> bool {
self.elem == elem
}
/// Turn this property into prehashed style.
pub fn wrap(self) -> Prehashed<Style> {
Prehashed::new(Style::Property(self))
}
}
impl Debug for Property {

View File

@ -341,6 +341,7 @@ pub struct BlockElem {
/// Use this to prevent page breaks between e.g. a heading and its body.
#[internal]
#[default(false)]
#[ghost]
pub sticky: bool,
}

View File

@ -330,6 +330,8 @@ pub struct PageElem {
/// Whether the page should be aligned to an even or odd page.
#[internal]
#[synthesized]
#[default(None)]
pub clear_to: Option<Parity>,
}

View File

@ -1,4 +1,3 @@
use comemo::Prehashed;
use unicode_math_class::MathClass;
use crate::diag::SourceResult;
@ -37,7 +36,7 @@ impl LayoutMath for Packed<ClassElem> {
#[typst_macros::time(name = "math.class", span = self.span())]
fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> {
let class = *self.class();
let style = Prehashed::new(EquationElem::set_class(Some(class)));
let style = EquationElem::set_class(Some(class)).wrap();
let mut fragment = ctx.layout_fragment(self.body(), styles.chain(&style))?;
fragment.set_class(class);
fragment.set_limits(Limits::for_class(class));

View File

@ -173,9 +173,8 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
boxed: &Packed<BoxElem>,
styles: StyleChain,
) -> SourceResult<Frame> {
let local = Prehashed::new(TextElem::set_size(TextSize(
scaled_font_size(self, styles).into(),
)));
let local =
TextElem::set_size(TextSize(scaled_font_size(self, styles).into())).wrap();
boxed.layout(self.engine, styles.chain(&local), self.regions)
}
@ -184,9 +183,8 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
content: &Content,
styles: StyleChain,
) -> SourceResult<Frame> {
let local = Prehashed::new(TextElem::set_size(TextSize(
scaled_font_size(self, styles).into(),
)));
let local =
TextElem::set_size(TextSize(scaled_font_size(self, styles).into())).wrap();
Ok(content
.layout(self.engine, styles.chain(&local), self.regions)?
.into_frame())
@ -248,7 +246,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
TextElem::set_size(TextSize(scaled_font_size(self, styles).into())),
EquationElem::set_italic(Smart::Custom(false)),
]
.map(Prehashed::new);
.map(|p| p.wrap());
// Anything else is handled by Typst's standard text layout.
let styles = styles.chain(&local);

View File

@ -100,28 +100,34 @@ pub struct EquationElem {
/// 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>,
}

View File

@ -1,5 +1,3 @@
use comemo::Prehashed;
use crate::diag::SourceResult;
use crate::foundations::{elem, func, Content, NativeElement, Packed, StyleChain};
use crate::layout::{Abs, Frame, FrameItem, Point, Size};
@ -82,7 +80,7 @@ fn layout(
.frame;
// Layout the index.
let sscript = Prehashed::new(EquationElem::set_size(MathSize::ScriptScript));
let sscript = EquationElem::set_size(MathSize::ScriptScript).wrap();
let index = index
.map(|elem| ctx.layout_frame(elem, styles.chain(&sscript)))
.transpose()?;

View File

@ -254,34 +254,36 @@ pub fn scaled_font_size(ctx: &MathContext, styles: StyleChain) -> Abs {
/// Styles something as cramped.
pub fn style_cramped() -> Prehashed<Style> {
Prehashed::new(EquationElem::set_cramped(true))
EquationElem::set_cramped(true).wrap()
}
/// The style for subscripts in the current style.
pub fn style_for_subscript(styles: StyleChain) -> [Prehashed<Style>; 2] {
[style_for_superscript(styles), Prehashed::new(EquationElem::set_cramped(true))]
[style_for_superscript(styles), EquationElem::set_cramped(true).wrap()]
}
/// The style for superscripts in the current style.
pub fn style_for_superscript(styles: StyleChain) -> Prehashed<Style> {
Prehashed::new(EquationElem::set_size(match EquationElem::size_in(styles) {
EquationElem::set_size(match EquationElem::size_in(styles) {
MathSize::Display | MathSize::Text => MathSize::Script,
MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript,
}))
})
.wrap()
}
/// The style for numerators in the current style.
pub fn style_for_numerator(styles: StyleChain) -> Prehashed<Style> {
Prehashed::new(EquationElem::set_size(match EquationElem::size_in(styles) {
EquationElem::set_size(match EquationElem::size_in(styles) {
MathSize::Display => MathSize::Text,
MathSize::Text => MathSize::Script,
MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript,
}))
})
.wrap()
}
/// The style for denominators in the current style.
pub fn style_for_denominator(styles: StyleChain) -> [Prehashed<Style>; 2] {
[style_for_numerator(styles), Prehashed::new(EquationElem::set_cramped(true))]
[style_for_numerator(styles), EquationElem::set_cramped(true).wrap()]
}
/// Select the correct styled math letter.

View File

@ -89,10 +89,11 @@ impl LayoutRoot for Packed<DocumentElem> {
if let Some(page) = child.to_packed::<PageElem>() {
let extend_to = iter.peek().and_then(|&next| {
next.to_styled()
*next
.to_styled()
.map_or(next, |(elem, _)| elem)
.to_packed::<PageElem>()?
.clear_to(styles)
.clear_to()
});
let run = page.layout(engine, styles, &mut page_counter, extend_to)?;
pages.extend(run);
@ -103,10 +104,10 @@ impl LayoutRoot for Packed<DocumentElem> {
Ok(Document {
pages,
title: self.title(styles).map(|content| content.plain_text()),
author: self.author(styles).0,
keywords: self.keywords(styles).0,
date: self.date(styles),
title: DocumentElem::title_in(styles).map(|content| content.plain_text()),
author: DocumentElem::author_in(styles).0,
keywords: DocumentElem::keywords_in(styles).0,
date: DocumentElem::date_in(styles),
introspector: Introspector::default(),
})
}

View File

@ -199,6 +199,7 @@ pub struct EnumElem {
/// The numbers of parent items.
#[internal]
#[fold]
#[ghost]
parents: SmallVec<[usize; 4]>,
}
@ -228,7 +229,7 @@ impl LayoutMultiple for Packed<EnumElem> {
let mut cells = vec![];
let mut number = self.start(styles);
let mut parents = self.parents(styles);
let mut parents = EnumElem::parents_in(styles);
parents.reverse();
let full = self.full(styles);

View File

@ -124,6 +124,7 @@ pub struct ListElem {
/// The nesting depth.
#[internal]
#[fold]
#[ghost]
depth: Depth,
}
@ -150,7 +151,7 @@ impl LayoutMultiple for Packed<ListElem> {
.unwrap_or_else(|| *BlockElem::below_in(styles).amount())
};
let Depth(depth) = self.depth(styles);
let Depth(depth) = ListElem::depth_in(styles);
let marker = self
.marker(styles)
.resolve(engine, depth)?