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 { impl Property {
/// Create a new property from a key-value pair. /// 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 where
E: NativeElement,
T: Debug + Clone + Hash + Send + Sync + 'static, 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. /// Whether this property is the given one.
@ -245,6 +251,11 @@ impl Property {
pub fn is_of(&self, elem: Element) -> bool { pub fn is_of(&self, elem: Element) -> bool {
self.elem == elem self.elem == elem
} }
/// Turn this property into prehashed style.
pub fn wrap(self) -> Prehashed<Style> {
Prehashed::new(Style::Property(self))
}
} }
impl Debug for Property { 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. /// Use this to prevent page breaks between e.g. a heading and its body.
#[internal] #[internal]
#[default(false)] #[default(false)]
#[ghost]
pub sticky: bool, pub sticky: bool,
} }

View File

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

View File

@ -1,4 +1,3 @@
use comemo::Prehashed;
use unicode_math_class::MathClass; use unicode_math_class::MathClass;
use crate::diag::SourceResult; use crate::diag::SourceResult;
@ -37,7 +36,7 @@ impl LayoutMath for Packed<ClassElem> {
#[typst_macros::time(name = "math.class", span = self.span())] #[typst_macros::time(name = "math.class", span = self.span())]
fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> { fn layout_math(&self, ctx: &mut MathContext, styles: StyleChain) -> SourceResult<()> {
let class = *self.class(); 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))?; let mut fragment = ctx.layout_fragment(self.body(), styles.chain(&style))?;
fragment.set_class(class); fragment.set_class(class);
fragment.set_limits(Limits::for_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>, boxed: &Packed<BoxElem>,
styles: StyleChain, styles: StyleChain,
) -> SourceResult<Frame> { ) -> SourceResult<Frame> {
let local = Prehashed::new(TextElem::set_size(TextSize( let local =
scaled_font_size(self, styles).into(), TextElem::set_size(TextSize(scaled_font_size(self, styles).into())).wrap();
)));
boxed.layout(self.engine, styles.chain(&local), self.regions) boxed.layout(self.engine, styles.chain(&local), self.regions)
} }
@ -184,9 +183,8 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
content: &Content, content: &Content,
styles: StyleChain, styles: StyleChain,
) -> SourceResult<Frame> { ) -> SourceResult<Frame> {
let local = Prehashed::new(TextElem::set_size(TextSize( let local =
scaled_font_size(self, styles).into(), TextElem::set_size(TextSize(scaled_font_size(self, styles).into())).wrap();
)));
Ok(content Ok(content
.layout(self.engine, styles.chain(&local), self.regions)? .layout(self.engine, styles.chain(&local), self.regions)?
.into_frame()) .into_frame())
@ -248,7 +246,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
TextElem::set_size(TextSize(scaled_font_size(self, styles).into())), TextElem::set_size(TextSize(scaled_font_size(self, styles).into())),
EquationElem::set_italic(Smart::Custom(false)), EquationElem::set_italic(Smart::Custom(false)),
] ]
.map(Prehashed::new); .map(|p| p.wrap());
// Anything else is handled by Typst's standard text layout. // Anything else is handled by Typst's standard text layout.
let styles = styles.chain(&local); let styles = styles.chain(&local);

View File

@ -100,28 +100,34 @@ pub struct EquationElem {
/// The size of the glyphs. /// The size of the glyphs.
#[internal] #[internal]
#[default(MathSize::Text)] #[default(MathSize::Text)]
#[ghost]
pub size: MathSize, pub size: MathSize,
/// The style variant to select. /// The style variant to select.
#[internal] #[internal]
#[ghost]
pub variant: MathVariant, pub variant: MathVariant,
/// Affects the height of exponents. /// Affects the height of exponents.
#[internal] #[internal]
#[default(false)] #[default(false)]
#[ghost]
pub cramped: bool, pub cramped: bool,
/// Whether to use bold glyphs. /// Whether to use bold glyphs.
#[internal] #[internal]
#[default(false)] #[default(false)]
#[ghost]
pub bold: bool, pub bold: bool,
/// Whether to use italic glyphs. /// Whether to use italic glyphs.
#[internal] #[internal]
#[ghost]
pub italic: Smart<bool>, pub italic: Smart<bool>,
/// A forced class to use for all fragment. /// A forced class to use for all fragment.
#[internal] #[internal]
#[ghost]
pub class: Option<MathClass>, pub class: Option<MathClass>,
} }

View File

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

View File

@ -254,34 +254,36 @@ pub fn scaled_font_size(ctx: &MathContext, styles: StyleChain) -> Abs {
/// Styles something as cramped. /// Styles something as cramped.
pub fn style_cramped() -> Prehashed<Style> { 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. /// The style for subscripts in the current style.
pub fn style_for_subscript(styles: StyleChain) -> [Prehashed<Style>; 2] { 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. /// The style for superscripts in the current style.
pub fn style_for_superscript(styles: StyleChain) -> Prehashed<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::Display | MathSize::Text => MathSize::Script,
MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript, MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript,
})) })
.wrap()
} }
/// The style for numerators in the current style. /// The style for numerators in the current style.
pub fn style_for_numerator(styles: StyleChain) -> Prehashed<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::Display => MathSize::Text,
MathSize::Text => MathSize::Script, MathSize::Text => MathSize::Script,
MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript, MathSize::Script | MathSize::ScriptScript => MathSize::ScriptScript,
})) })
.wrap()
} }
/// The style for denominators in the current style. /// The style for denominators in the current style.
pub fn style_for_denominator(styles: StyleChain) -> [Prehashed<Style>; 2] { 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. /// 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>() { if let Some(page) = child.to_packed::<PageElem>() {
let extend_to = iter.peek().and_then(|&next| { let extend_to = iter.peek().and_then(|&next| {
next.to_styled() *next
.to_styled()
.map_or(next, |(elem, _)| elem) .map_or(next, |(elem, _)| elem)
.to_packed::<PageElem>()? .to_packed::<PageElem>()?
.clear_to(styles) .clear_to()
}); });
let run = page.layout(engine, styles, &mut page_counter, extend_to)?; let run = page.layout(engine, styles, &mut page_counter, extend_to)?;
pages.extend(run); pages.extend(run);
@ -103,10 +104,10 @@ impl LayoutRoot for Packed<DocumentElem> {
Ok(Document { Ok(Document {
pages, pages,
title: self.title(styles).map(|content| content.plain_text()), title: DocumentElem::title_in(styles).map(|content| content.plain_text()),
author: self.author(styles).0, author: DocumentElem::author_in(styles).0,
keywords: self.keywords(styles).0, keywords: DocumentElem::keywords_in(styles).0,
date: self.date(styles), date: DocumentElem::date_in(styles),
introspector: Introspector::default(), introspector: Introspector::default(),
}) })
} }

View File

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

View File

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