Merge the two realization functions (#4852)

This commit is contained in:
Laurenz 2024-08-28 01:50:45 +02:00 committed by GitHub
parent b5ef9244eb
commit 4e4c5175e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 31 deletions

View File

@ -22,8 +22,10 @@ use crate::layout::{
OuterVAlignment, Page, PageElem, PagebreakElem, Paper, Parity, PlaceElem, Point, OuterVAlignment, Page, PageElem, PagebreakElem, Paper, Parity, PlaceElem, Point,
Ratio, Region, Regions, Rel, Sides, Size, Spacing, VAlignment, VElem, Ratio, Region, Regions, Rel, Sides, Size, Spacing, VAlignment, VElem,
}; };
use crate::model::{Document, FootnoteElem, FootnoteEntry, Numbering, ParElem}; use crate::model::{
use crate::realize::{first_span, realize_root, realizer_container, Arenas, Pair}; Document, DocumentInfo, FootnoteElem, FootnoteEntry, Numbering, ParElem,
};
use crate::realize::{first_span, realize, Arenas, Pair};
use crate::syntax::Span; use crate::syntax::Span;
use crate::text::TextElem; use crate::text::TextElem;
use crate::utils::{NonZeroExt, Numeric}; use crate::utils::{NonZeroExt, Numeric};
@ -110,8 +112,9 @@ fn layout_document_impl(
let styles = StyleChain::new(&styles); let styles = StyleChain::new(&styles);
let arenas = Arenas::default(); let arenas = Arenas::default();
let (mut children, info) = let mut info = DocumentInfo::default();
realize_root(&mut engine, &mut locator, &arenas, content, styles)?; let mut children =
realize(&mut engine, &mut locator, &arenas, Some(&mut info), content, styles)?;
let pages = layout_pages(&mut engine, &mut children, locator, 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())?; 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 arenas = Arenas::default();
let children = let children = realize(&mut engine, &mut locator, &arenas, None, content, styles)?;
realizer_container(&mut engine, &mut locator, &arenas, content, styles)?;
FlowLayouter::new( FlowLayouter::new(
&mut engine, &mut engine,

View File

@ -38,31 +38,17 @@ use crate::utils::SliceExt;
/// A pair of content and a style chain that applies to it. /// A pair of content and a style chain that applies to it.
pub type Pair<'a> = (&'a Content, StyleChain<'a>); 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")] #[typst_macros::time(name = "realize")]
pub fn realize_root<'a>( pub fn realize<'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>(
engine: &mut Engine<'a>, engine: &mut Engine<'a>,
locator: &mut SplitLocator<'a>, locator: &mut SplitLocator<'a>,
arenas: &'a Arenas<'a>, arenas: &'a Arenas<'a>,
doc_info: Option<&mut DocumentInfo>,
content: &'a Content, content: &'a Content,
styles: StyleChain<'a>, styles: StyleChain<'a>,
) -> SourceResult<Vec<Pair<'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.accept(content, styles)?;
builder.interrupt_par()?; builder.interrupt_par()?;
Ok(builder.sink.finish()) Ok(builder.sink.finish())
@ -77,11 +63,11 @@ struct Builder<'a, 'v> {
/// Scratch arenas for building. /// Scratch arenas for building.
arenas: &'a Arenas<'a>, 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 /// Document metadata we have collected from `set document` rules. If this
/// is `None`, we are in a container. /// 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. /// A builder for a paragraph that might be under construction.
par: ParBuilder<'a>, par: ParBuilder<'a>,
@ -104,18 +90,19 @@ impl<'a, 'v> Builder<'a, 'v> {
engine: &'v mut Engine<'a>, engine: &'v mut Engine<'a>,
locator: &'v mut SplitLocator<'a>, locator: &'v mut SplitLocator<'a>,
arenas: &'a Arenas<'a>, arenas: &'a Arenas<'a>,
root: bool, doc_info: Option<&'v mut DocumentInfo>,
) -> Self { ) -> Self {
let outside = doc_info.is_some();
Self { Self {
engine, engine,
locator, locator,
arenas, arenas,
doc_info,
sink: BehavedBuilder::default(), sink: BehavedBuilder::default(),
doc_info: root.then(DocumentInfo::default),
par: ParBuilder::default(), par: ParBuilder::default(),
list: ListBuilder::default(), list: ListBuilder::default(),
cites: CiteGroupBuilder::default(), cites: CiteGroupBuilder::default(),
outside: root, outside,
last_was_par: false, last_was_par: false,
} }
} }