diff options
| author | Sébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com> | 2023-11-06 21:37:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-06 21:37:50 +0100 |
| commit | c0f6d2004afebfa9412ba0c2d598ef8287197c42 (patch) | |
| tree | 4bb034ca671e7d1982a306f5aecfc4f78a01841d /crates/typst-library/src/shared | |
| parent | 8fd546760c7c425398f0114997c8085a481d8d2a (diff) | |
Content rework 2 - Electric Boogaloo (#2504)
Diffstat (limited to 'crates/typst-library/src/shared')
| -rw-r--r-- | crates/typst-library/src/shared/behave.rs | 12 | ||||
| -rw-r--r-- | crates/typst-library/src/shared/ext.rs | 6 |
2 files changed, 10 insertions, 8 deletions
diff --git a/crates/typst-library/src/shared/behave.rs b/crates/typst-library/src/shared/behave.rs index f97e3fbc..e152fbb5 100644 --- a/crates/typst-library/src/shared/behave.rs +++ b/crates/typst-library/src/shared/behave.rs @@ -1,16 +1,18 @@ //! Element interaction. +use std::borrow::Cow; + use typst::model::{Behave, Behaviour, Content, StyleChain, StyleVec, StyleVecBuilder}; /// A wrapper around a [`StyleVecBuilder`] that allows elements to interact. #[derive(Debug)] pub struct BehavedBuilder<'a> { /// The internal builder. - builder: StyleVecBuilder<'a, Content>, + builder: StyleVecBuilder<'a, Cow<'a, Content>>, /// Staged weak and ignorant elements that we can't yet commit to the /// builder. The option is `Some(_)` for weak elements and `None` for /// ignorant elements. - staged: Vec<(Content, Behaviour, StyleChain<'a>)>, + staged: Vec<(Cow<'a, Content>, Behaviour, StyleChain<'a>)>, /// What the last non-ignorant item was. last: Behaviour, } @@ -41,7 +43,7 @@ impl<'a> BehavedBuilder<'a> { } /// Push an item into the sequence. - pub fn push(&mut self, elem: Content, styles: StyleChain<'a>) { + pub fn push(&mut self, elem: Cow<'a, Content>, styles: StyleChain<'a>) { let interaction = elem .with::<dyn Behave>() .map_or(Behaviour::Supportive, Behave::behaviour); @@ -81,12 +83,12 @@ impl<'a> BehavedBuilder<'a> { } /// Iterate over the contained elements. - pub fn elems(&self) -> impl DoubleEndedIterator<Item = &Content> { + pub fn elems(&self) -> impl DoubleEndedIterator<Item = &Cow<'a, Content>> { self.builder.elems().chain(self.staged.iter().map(|(item, ..)| item)) } /// Return the finish style vec and the common prefix chain. - pub fn finish(mut self) -> (StyleVec<Content>, StyleChain<'a>) { + pub fn finish(mut self) -> (StyleVec<Cow<'a, Content>>, StyleChain<'a>) { self.flush(false); self.builder.finish() } diff --git a/crates/typst-library/src/shared/ext.rs b/crates/typst-library/src/shared/ext.rs index e6431423..60614820 100644 --- a/crates/typst-library/src/shared/ext.rs +++ b/crates/typst-library/src/shared/ext.rs @@ -47,13 +47,13 @@ impl ContentExt for Content { } fn linked(self, dest: Destination) -> Self { - self.styled(MetaElem::set_data(vec![Meta::Link(dest)])) + self.styled(MetaElem::set_data(smallvec![Meta::Link(dest)])) } fn backlinked(self, loc: Location) -> Self { let mut backlink = Content::empty(); backlink.set_location(loc); - self.styled(MetaElem::set_data(vec![Meta::Elem(backlink)])) + self.styled(MetaElem::set_data(smallvec![Meta::Elem(backlink)])) } fn aligned(self, align: Align) -> Self { @@ -85,7 +85,7 @@ impl StylesExt for Styles { fn set_family(&mut self, preferred: FontFamily, existing: StyleChain) { self.set(TextElem::set_font(FontList( std::iter::once(preferred) - .chain(TextElem::font_in(existing)) + .chain(TextElem::font_in(existing).into_iter().cloned()) .collect(), ))); } |
