summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-08-28 01:50:45 +0200
committerGitHub <noreply@github.com>2024-08-27 23:50:45 +0000
commit4e4c5175e531d99ec6f39d9a77d59d02f2109308 (patch)
treec7dcbc281263c04855aef6a6dc966ada33663743
parentb5ef9244eb2b546b47530d40860e9e04256aafb2 (diff)
Merge the two realization functions (#4852)
-rw-r--r--crates/typst/src/layout/flow.rs15
-rw-r--r--crates/typst/src/realize/mod.rs35
2 files changed, 19 insertions, 31 deletions
diff --git a/crates/typst/src/layout/flow.rs b/crates/typst/src/layout/flow.rs
index f3b105de..f99f8bea 100644
--- a/crates/typst/src/layout/flow.rs
+++ b/crates/typst/src/layout/flow.rs
@@ -22,8 +22,10 @@ use crate::layout::{
OuterVAlignment, Page, PageElem, PagebreakElem, Paper, Parity, PlaceElem, Point,
Ratio, Region, Regions, Rel, Sides, Size, Spacing, VAlignment, VElem,
};
-use crate::model::{Document, FootnoteElem, FootnoteEntry, Numbering, ParElem};
-use crate::realize::{first_span, realize_root, realizer_container, Arenas, Pair};
+use crate::model::{
+ Document, DocumentInfo, FootnoteElem, FootnoteEntry, Numbering, ParElem,
+};
+use crate::realize::{first_span, realize, Arenas, Pair};
use crate::syntax::Span;
use crate::text::TextElem;
use crate::utils::{NonZeroExt, Numeric};
@@ -110,8 +112,9 @@ fn layout_document_impl(
let styles = StyleChain::new(&styles);
let arenas = Arenas::default();
- let (mut children, info) =
- realize_root(&mut engine, &mut locator, &arenas, content, styles)?;
+ let mut info = DocumentInfo::default();
+ let mut children =
+ realize(&mut engine, &mut locator, &arenas, Some(&mut info), content, styles)?;
let pages = layout_pages(&mut engine, &mut children, locator, styles)?;
@@ -726,10 +729,8 @@ fn layout_fragment_impl(
engine.route.check_layout_depth().at(content.span())?;
- // If we are in a `PageElem`, this might already be a realized flow.
let arenas = Arenas::default();
- let children =
- realizer_container(&mut engine, &mut locator, &arenas, content, styles)?;
+ let children = realize(&mut engine, &mut locator, &arenas, None, content, styles)?;
FlowLayouter::new(
&mut engine,
diff --git a/crates/typst/src/realize/mod.rs b/crates/typst/src/realize/mod.rs
index a154fce1..29dd1e40 100644
--- a/crates/typst/src/realize/mod.rs
+++ b/crates/typst/src/realize/mod.rs
@@ -38,31 +38,17 @@ use crate::utils::SliceExt;
/// A pair of content and a style chain that applies to it.
pub type Pair<'a> = (&'a Content, StyleChain<'a>);
-/// Realize at the root level.
+/// Realize content into a flat list of well-known, styled items.
#[typst_macros::time(name = "realize")]
-pub fn realize_root<'a>(
- engine: &mut Engine<'a>,
- locator: &mut SplitLocator<'a>,
- arenas: &'a Arenas<'a>,
- content: &'a Content,
- styles: StyleChain<'a>,
-) -> SourceResult<(Vec<Pair<'a>>, DocumentInfo)> {
- let mut builder = Builder::new(engine, locator, arenas, true);
- builder.accept(content, styles)?;
- builder.interrupt_par()?;
- Ok((builder.sink.finish(), builder.doc_info.unwrap()))
-}
-
-/// Realize at the container level.
-#[typst_macros::time(name = "realize")]
-pub fn realizer_container<'a>(
+pub fn realize<'a>(
engine: &mut Engine<'a>,
locator: &mut SplitLocator<'a>,
arenas: &'a Arenas<'a>,
+ doc_info: Option<&mut DocumentInfo>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<Vec<Pair<'a>>> {
- let mut builder = Builder::new(engine, locator, arenas, false);
+ let mut builder = Builder::new(engine, locator, arenas, doc_info);
builder.accept(content, styles)?;
builder.interrupt_par()?;
Ok(builder.sink.finish())
@@ -77,11 +63,11 @@ struct Builder<'a, 'v> {
/// Scratch arenas for building.
arenas: &'a Arenas<'a>,
- /// The output elements of well-known types collected by the builder.
- sink: BehavedBuilder<'a>,
/// Document metadata we have collected from `set document` rules. If this
/// is `None`, we are in a container.
- doc_info: Option<DocumentInfo>,
+ doc_info: Option<&'v mut DocumentInfo>,
+ /// The output elements of well-known types collected by the builder.
+ sink: BehavedBuilder<'a>,
/// A builder for a paragraph that might be under construction.
par: ParBuilder<'a>,
@@ -104,18 +90,19 @@ impl<'a, 'v> Builder<'a, 'v> {
engine: &'v mut Engine<'a>,
locator: &'v mut SplitLocator<'a>,
arenas: &'a Arenas<'a>,
- root: bool,
+ doc_info: Option<&'v mut DocumentInfo>,
) -> Self {
+ let outside = doc_info.is_some();
Self {
engine,
locator,
arenas,
+ doc_info,
sink: BehavedBuilder::default(),
- doc_info: root.then(DocumentInfo::default),
par: ParBuilder::default(),
list: ListBuilder::default(),
cites: CiteGroupBuilder::default(),
- outside: root,
+ outside,
last_was_par: false,
}
}