diff --git a/crates/typst/src/foundations/content.rs b/crates/typst/src/foundations/content.rs index 0d764f115..8c093764b 100644 --- a/crates/typst/src/foundations/content.rs +++ b/crates/typst/src/foundations/content.rs @@ -14,8 +14,9 @@ use smallvec::smallvec; use crate::diag::{SourceResult, StrResult}; use crate::engine::Engine; use crate::foundations::{ - elem, func, scope, ty, Dict, Element, Fields, IntoValue, Label, NativeElement, - Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, Value, + elem, func, scope, ty, Behave, Behaviour, Dict, Element, Fields, IntoValue, Label, + NativeElement, Recipe, RecipeIndex, Repr, Selector, Str, Style, StyleChain, Styles, + Value, }; use crate::introspection::{Location, Meta, MetaElem}; use crate::layout::{AlignElem, Alignment, Axes, Length, MoveElem, PadElem, Rel, Sides}; @@ -167,6 +168,12 @@ impl Content { self.make_mut().lifecycle.insert(0); } + /// How this element interacts with other elements in a stream. + pub fn behaviour(&self) -> Behaviour { + self.with::() + .map_or(Behaviour::Supportive, Behave::behaviour) + } + /// Get a field by ID. /// /// This is the preferred way to access fields. However, you can only use it @@ -335,7 +342,7 @@ impl Content { } /// Also auto expands sequence of sequences into flat sequence - pub fn sequence_recursive_for_each(&self, f: &mut impl FnMut(&Self)) { + pub fn sequence_recursive_for_each<'a>(&'a self, f: &mut impl FnMut(&'a Self)) { if let Some(children) = self.to_sequence() { children.for_each(|c| c.sequence_recursive_for_each(f)); } else { diff --git a/crates/typst/src/foundations/element.rs b/crates/typst/src/foundations/element.rs index 412e30892..b59a16cb3 100644 --- a/crates/typst/src/foundations/element.rs +++ b/crates/typst/src/foundations/element.rs @@ -1,5 +1,4 @@ use std::any::TypeId; -use std::borrow::Cow; use std::cmp::Ordering; use std::fmt::{self, Debug}; use std::hash::Hash; @@ -309,11 +308,7 @@ pub trait Behave { /// Whether this weak element is larger than a previous one and thus picked /// as the maximum when the levels are the same. #[allow(unused_variables)] - fn larger( - &self, - prev: &(Cow, Behaviour, StyleChain), - styles: StyleChain, - ) -> bool { + fn larger(&self, prev: &(&Content, StyleChain), styles: StyleChain) -> bool { false } } @@ -336,3 +331,10 @@ pub enum Behaviour { /// An element that does not have a visual representation. Invisible, } + +impl Behaviour { + /// Whether this of `Weak(_)` variant. + pub fn is_weak(self) -> bool { + matches!(self, Self::Weak(_)) + } +} diff --git a/crates/typst/src/foundations/styles.rs b/crates/typst/src/foundations/styles.rs index c6ee3c5e2..9656fafbf 100644 --- a/crates/typst/src/foundations/styles.rs +++ b/crates/typst/src/foundations/styles.rs @@ -1,8 +1,7 @@ use std::any::{Any, TypeId}; -use std::borrow::Cow; use std::fmt::{self, Debug, Formatter}; use std::hash::{Hash, Hasher}; -use std::{iter, mem, ptr}; +use std::{mem, ptr}; use comemo::Prehashed; use ecow::{eco_vec, EcoString, EcoVec}; @@ -570,13 +569,13 @@ impl<'a> StyleChain<'a> { } /// Iterate over the links of the chain. - fn links(self) -> Links<'a> { + pub fn links(self) -> Links<'a> { Links(Some(self)) } /// Build owned styles from the suffix (all links beyond the `len`) of the /// chain. - fn suffix(self, len: usize) -> Styles { + pub fn suffix(self, len: usize) -> Styles { let mut suffix = Styles::new(); let take = self.links().count().saturating_sub(len); for link in self.links().take(take) { @@ -586,7 +585,7 @@ impl<'a> StyleChain<'a> { } /// Remove the last link from the chain. - fn pop(&mut self) { + pub fn pop(&mut self) { *self = self.tail.copied().unwrap_or_default(); } } @@ -672,7 +671,7 @@ impl<'a> Iterator for Entries<'a> { } /// An iterator over the links of a style chain. -struct Links<'a>(Option>); +pub struct Links<'a>(Option>); impl<'a> Iterator for Links<'a> { type Item = &'a [Prehashed