mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
More sensible LayoutRoot
This commit is contained in:
parent
ea2a1525f0
commit
c2749f7615
@ -49,20 +49,25 @@ use crate::text::{
|
||||
#[capability]
|
||||
pub trait LayoutRoot {
|
||||
/// Layout into one frame per page.
|
||||
fn layout_root(&self, world: Tracked<dyn World>) -> SourceResult<Vec<Frame>>;
|
||||
fn layout_root(
|
||||
&self,
|
||||
world: Tracked<dyn World>,
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<Vec<Frame>>;
|
||||
}
|
||||
|
||||
impl LayoutRoot for Content {
|
||||
#[comemo::memoize]
|
||||
fn layout_root(&self, world: Tracked<dyn World>) -> SourceResult<Vec<Frame>> {
|
||||
let styles = StyleChain::with_root(&world.config().styles);
|
||||
fn layout_root(
|
||||
&self,
|
||||
world: Tracked<dyn World>,
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<Vec<Frame>> {
|
||||
let scratch = Scratch::default();
|
||||
|
||||
let mut builder = Builder::new(world, &scratch, true);
|
||||
builder.accept(self, styles)?;
|
||||
|
||||
let (doc, shared) = builder.into_doc(styles)?;
|
||||
doc.layout(world, shared)
|
||||
doc.layout_root(world, shared)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -154,7 +154,7 @@ pub fn styles() -> StyleMap {
|
||||
/// Construct the standard lang item mapping.
|
||||
pub fn items() -> LangItems {
|
||||
LangItems {
|
||||
root: |world, content| content.layout_root(world),
|
||||
root: |content, world, styles| content.layout_root(world, styles),
|
||||
em: |styles| styles.get(text::TextNode::SIZE),
|
||||
dir: |styles| styles.get(text::TextNode::DIR),
|
||||
space: || text::SpaceNode.pack(),
|
||||
|
@ -1,13 +1,16 @@
|
||||
use crate::layout::PageNode;
|
||||
use crate::layout::{LayoutRoot, PageNode};
|
||||
use crate::prelude::*;
|
||||
|
||||
/// A sequence of page runs.
|
||||
#[derive(Hash)]
|
||||
pub struct DocNode(pub StyleVec<PageNode>);
|
||||
|
||||
impl DocNode {
|
||||
#[node(LayoutRoot)]
|
||||
impl DocNode {}
|
||||
|
||||
impl LayoutRoot for DocNode {
|
||||
/// Layout the document into a sequence of frames, one per page.
|
||||
pub fn layout(
|
||||
fn layout_root(
|
||||
&self,
|
||||
world: Tracked<dyn World>,
|
||||
styles: StyleChain,
|
||||
|
@ -49,7 +49,7 @@ use comemo::{Prehashed, Track};
|
||||
use crate::diag::{FileResult, SourceResult};
|
||||
use crate::font::{Font, FontBook};
|
||||
use crate::frame::Frame;
|
||||
use crate::model::{LangItems, Route, Scope, StyleMap};
|
||||
use crate::model::{LangItems, Route, Scope, StyleChain, StyleMap};
|
||||
use crate::syntax::{Source, SourceId};
|
||||
use crate::util::Buffer;
|
||||
|
||||
@ -62,10 +62,12 @@ pub fn typeset(
|
||||
world: &(dyn World + 'static),
|
||||
main: SourceId,
|
||||
) -> SourceResult<Vec<Frame>> {
|
||||
crate::model::set_lang_items(world.config().items);
|
||||
let config = world.config();
|
||||
crate::model::set_lang_items(config.items);
|
||||
let route = Route::default();
|
||||
let module = model::eval(world.track(), route.track(), main)?;
|
||||
item!(root)(world.track(), &module.content)
|
||||
let styles = StyleChain::with_root(&config.styles);
|
||||
item!(root)(&module.content, world.track(), styles)
|
||||
}
|
||||
|
||||
/// The environment in which typesetting occurs.
|
||||
|
@ -42,8 +42,11 @@ macro_rules! item {
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct LangItems {
|
||||
/// The root layout function.
|
||||
pub root:
|
||||
fn(world: Tracked<dyn World>, document: &Content) -> SourceResult<Vec<Frame>>,
|
||||
pub root: fn(
|
||||
content: &Content,
|
||||
world: Tracked<dyn World>,
|
||||
styles: StyleChain,
|
||||
) -> SourceResult<Vec<Frame>>,
|
||||
/// Access the em size.
|
||||
pub em: fn(StyleChain) -> Abs,
|
||||
/// Access the text direction.
|
||||
|
Loading…
x
Reference in New Issue
Block a user