Better space coalescing logic 🌧

This creates a smaller state machine helper type for softness coalescing, which does not own the resulting nodes. While this creates a bit more duplication in stack and par builder, it makes it a lot easier to integrate additional logic into the paragraph builder.

Furthermore:
- Line breaks are now "hard", that is, not coalesced with each other.
- Text nodes with equal style are now merged allowing for example `f{}i` to form a ligature.
This commit is contained in:
Laurenz 2021-03-27 21:52:39 +01:00
parent 76fc4cca62
commit 57ca9628c1
23 changed files with 149 additions and 152 deletions

View File

@ -65,87 +65,66 @@ impl<'a> ExecContext<'a> {
mem::replace(&mut self.stack, stack).build() mem::replace(&mut self.stack, stack).build()
} }
/// Push any node into the active paragraph.
pub fn push(&mut self, node: impl Into<AnyNode>) {
let align = self.state.aligns.cross;
self.stack.par.push(ParChild::Any(node.into(), align));
}
/// Push a word space into the active paragraph.
pub fn push_word_space(&mut self) {
let em = self.state.font.resolve_size();
let amount = self.state.par.word_spacing.resolve(em);
self.stack.par.push_soft(ParChild::Spacing(amount));
}
/// Push text into the active paragraph. /// Push text into the active paragraph.
/// ///
/// The text is split into lines at newlines. /// The text is split into lines at newlines.
pub fn push_text(&mut self, text: &str) { pub fn push_text(&mut self, text: &str) {
let mut scanner = Scanner::new(text); let mut scanner = Scanner::new(text);
let mut line = String::new(); let mut text = String::new();
let push = |this: &mut Self, text| {
let props = this.state.font.resolve_props();
let node = TextNode { text, props };
let align = this.state.aligns.cross;
this.stack.par.folder.push(ParChild::Text(node, align))
};
while let Some(c) = scanner.eat_merging_crlf() { while let Some(c) = scanner.eat_merging_crlf() {
if is_newline(c) { if is_newline(c) {
push(self, mem::take(&mut line)); self.stack.par.push_text(mem::take(&mut text), &self.state);
self.push_linebreak(); self.linebreak();
} else { } else {
line.push(c); text.push(c);
} }
} }
push(self, line); self.stack.par.push_text(text, &self.state);
}
/// Push a word space.
pub fn push_word_space(&mut self) {
let em = self.state.font.resolve_size();
let amount = self.state.par.word_spacing.resolve(em);
self.push_spacing(GenAxis::Cross, amount, 1);
}
/// Apply a forced line break.
pub fn push_linebreak(&mut self) {
let em = self.state.font.resolve_size();
let amount = self.state.par.leading.resolve(em);
self.push_spacing(GenAxis::Main, amount, 2);
}
/// Apply a forced paragraph break.
pub fn push_parbreak(&mut self) {
let em = self.state.font.resolve_size();
let amount = self.state.par.spacing.resolve(em);
self.push_spacing(GenAxis::Main, amount, 1);
} }
/// Push spacing into paragraph or stack depending on `axis`. /// Push spacing into paragraph or stack depending on `axis`.
/// pub fn push_spacing(&mut self, axis: GenAxis, amount: Length) {
/// The `softness` configures how the spacing interacts with surrounding
/// spacing.
pub fn push_spacing(&mut self, axis: GenAxis, amount: Length, softness: u8) {
match axis { match axis {
GenAxis::Main => { GenAxis::Main => {
let spacing = StackChild::Spacing(amount); self.stack.parbreak(&self.state);
self.stack.finish_par(&self.state); self.stack.push_hard(StackChild::Spacing(amount));
self.stack.folder.push_soft(spacing, softness);
} }
GenAxis::Cross => { GenAxis::Cross => {
let spacing = ParChild::Spacing(amount); self.stack.par.push_hard(ParChild::Spacing(amount));
self.stack.par.folder.push_soft(spacing, softness);
} }
} }
} }
/// Push any node into the active paragraph. /// Apply a forced line break.
pub fn push_into_par(&mut self, node: impl Into<AnyNode>) { pub fn linebreak(&mut self) {
let align = self.state.aligns.cross; self.stack.par.push_hard(ParChild::Linebreak);
self.stack.par.folder.push(ParChild::Any(node.into(), align));
} }
/// Push any node directly into the stack of paragraphs. /// Apply a forced paragraph break.
/// pub fn parbreak(&mut self) {
/// This finishes the active paragraph and starts a new one. let em = self.state.font.resolve_size();
pub fn push_into_stack(&mut self, node: impl Into<AnyNode>) { let amount = self.state.par.spacing.resolve(em);
let aligns = self.state.aligns; self.stack.parbreak(&self.state);
self.stack.finish_par(&self.state); self.stack.push_soft(StackChild::Spacing(amount));
self.stack.folder.push(StackChild::Any(node.into(), aligns));
} }
/// Finish the active page. /// Apply a forced page break.
pub fn finish_page(&mut self, keep: bool, hard: bool, source: Span) { pub fn pagebreak(&mut self, keep: bool, hard: bool, source: Span) {
if let Some(builder) = &mut self.page { if let Some(builder) = &mut self.page {
let page = mem::replace(builder, PageBuilder::new(&self.state, hard)); let page = mem::replace(builder, PageBuilder::new(&self.state, hard));
let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state)); let stack = mem::replace(&mut self.stack, StackBuilder::new(&self.state));
@ -158,7 +137,7 @@ impl<'a> ExecContext<'a> {
/// Finish execution and return the created layout tree. /// Finish execution and return the created layout tree.
pub fn finish(mut self) -> Pass<Tree> { pub fn finish(mut self) -> Pass<Tree> {
assert!(self.page.is_some()); assert!(self.page.is_some());
self.finish_page(true, false, Span::default()); self.pagebreak(true, false, Span::default());
Pass::new(self.tree, self.diags) Pass::new(self.tree, self.diags)
} }
} }
@ -189,7 +168,8 @@ impl PageBuilder {
struct StackBuilder { struct StackBuilder {
dirs: Gen<Dir>, dirs: Gen<Dir>,
folder: SoftFolder<StackChild>, children: Vec<StackChild>,
last: Last<StackChild>,
par: ParBuilder, par: ParBuilder,
} }
@ -197,20 +177,36 @@ impl StackBuilder {
fn new(state: &State) -> Self { fn new(state: &State) -> Self {
Self { Self {
dirs: Gen::new(Dir::TTB, state.lang.dir), dirs: Gen::new(Dir::TTB, state.lang.dir),
folder: SoftFolder::new(), children: vec![],
last: Last::None,
par: ParBuilder::new(state), par: ParBuilder::new(state),
} }
} }
fn finish_par(&mut self, state: &State) { fn push_soft(&mut self, child: StackChild) {
self.last.soft(child);
}
fn push_hard(&mut self, child: StackChild) {
self.last.hard();
self.children.push(child);
}
fn parbreak(&mut self, state: &State) {
let par = mem::replace(&mut self.par, ParBuilder::new(state)); let par = mem::replace(&mut self.par, ParBuilder::new(state));
self.folder.extend(par.build()); if let Some(par) = par.build() {
self.children.extend(self.last.any());
self.children.push(par);
}
} }
fn build(self) -> StackNode { fn build(self) -> StackNode {
let Self { dirs, mut folder, par } = self; let Self { dirs, mut children, par, mut last } = self;
folder.extend(par.build()); if let Some(par) = par.build() {
StackNode { dirs, children: folder.finish() } children.extend(last.any());
children.push(par);
}
StackNode { dirs, children }
} }
} }
@ -218,7 +214,8 @@ struct ParBuilder {
aligns: Gen<Align>, aligns: Gen<Align>,
dir: Dir, dir: Dir,
line_spacing: Length, line_spacing: Length,
folder: SoftFolder<ParChild>, children: Vec<ParChild>,
last: Last<ParChild>,
} }
impl ParBuilder { impl ParBuilder {
@ -228,13 +225,43 @@ impl ParBuilder {
aligns: state.aligns, aligns: state.aligns,
dir: state.lang.dir, dir: state.lang.dir,
line_spacing: state.par.leading.resolve(em), line_spacing: state.par.leading.resolve(em),
folder: SoftFolder::new(), children: vec![],
last: Last::None,
} }
} }
fn push(&mut self, child: ParChild) {
self.children.extend(self.last.any());
self.children.push(child);
}
fn push_text(&mut self, text: String, state: &State) {
self.children.extend(self.last.any());
let align = state.aligns.cross;
let props = state.font.resolve_props();
if let Some(ParChild::Text(prev, prev_align)) = self.children.last_mut() {
if *prev_align == align && prev.props == props {
prev.text.push_str(&text);
return;
}
}
self.children.push(ParChild::Text(TextNode { text, props }, align));
}
fn push_soft(&mut self, child: ParChild) {
self.last.soft(child);
}
fn push_hard(&mut self, child: ParChild) {
self.last.hard();
self.children.push(child);
}
fn build(self) -> Option<StackChild> { fn build(self) -> Option<StackChild> {
let Self { aligns, dir, line_spacing, folder } = self; let Self { aligns, dir, line_spacing, children, .. } = self;
let children = folder.finish();
(!children.is_empty()).then(|| { (!children.is_empty()).then(|| {
let node = ParNode { dir, line_spacing, children }; let node = ParNode { dir, line_spacing, children };
StackChild::Any(node.into(), aligns) StackChild::Any(node.into(), aligns)
@ -242,54 +269,28 @@ impl ParBuilder {
} }
} }
/// This is used to remove leading and trailing word/line/paragraph spacing /// Finite state machine for spacing coalescing.
/// as well as collapse sequences of spacings into just one.
struct SoftFolder<N> {
nodes: Vec<N>,
last: Last<N>,
}
enum Last<N> { enum Last<N> {
None, None,
Hard, Any,
Soft(N, u8), Soft(N),
} }
impl<N> SoftFolder<N> { impl<N> Last<N> {
fn new() -> Self { fn any(&mut self) -> Option<N> {
Self { nodes: vec![], last: Last::Hard } match mem::replace(self, Self::Any) {
} Self::Soft(soft) => Some(soft),
_ => None,
fn push(&mut self, node: N) {
let last = mem::replace(&mut self.last, Last::None);
if let Last::Soft(soft, _) = last {
self.nodes.push(soft);
}
self.nodes.push(node);
}
fn push_soft(&mut self, node: N, softness: u8) {
if softness == 0 {
self.last = Last::Hard;
self.nodes.push(node);
} else {
match self.last {
Last::Hard => {}
Last::Soft(_, other) if softness >= other => {}
_ => self.last = Last::Soft(node, softness),
}
} }
} }
fn finish(self) -> Vec<N> { fn soft(&mut self, soft: N) {
self.nodes if let Self::Any = self {
} *self = Self::Soft(soft);
}
impl<N> Extend<N> for SoftFolder<N> {
fn extend<T: IntoIterator<Item = N>>(&mut self, iter: T) {
for elem in iter {
self.push(elem);
} }
} }
fn hard(&mut self) {
*self = Self::None;
}
} }

View File

@ -23,6 +23,8 @@ pub enum ParChild {
Text(TextNode, Align), Text(TextNode, Align),
/// Any child node and how to align it in its line. /// Any child node and how to align it in its line.
Any(AnyNode, Align), Any(AnyNode, Align),
/// A forced linebreak.
Linebreak,
} }
/// A consecutive, styled run of text. /// A consecutive, styled run of text.
@ -55,6 +57,7 @@ impl Layout for ParNode {
layouter.push_frame(frame, align); layouter.push_frame(frame, align);
} }
} }
ParChild::Linebreak => layouter.finish_line(),
} }
} }
layouter.finish() layouter.finish()

View File

@ -52,7 +52,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
if let Some(vertical) = vertical { if let Some(vertical) = vertical {
ctx.state.aligns.main = vertical.to_align(Dir::TTB); ctx.state.aligns.main = vertical.to_align(Dir::TTB);
if ctx.state.aligns.main != snapshot.aligns.main { if ctx.state.aligns.main != snapshot.aligns.main {
ctx.push_linebreak(); ctx.parbreak();
} }
} }

View File

@ -23,7 +23,7 @@ pub fn image(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let loaded = ctx.env.resources.load(&path.v, ImageResource::parse); let loaded = ctx.env.resources.load(&path.v, ImageResource::parse);
if let Some((res, img)) = loaded { if let Some((res, img)) = loaded {
let dimensions = img.buf.dimensions(); let dimensions = img.buf.dimensions();
ctx.push_into_par(ImageNode { res, dimensions, width, height }); ctx.push(ImageNode { res, dimensions, width, height });
} else { } else {
ctx.diag(error!(path.span, "failed to load image")); ctx.diag(error!(path.span, "failed to load image"));
} }

View File

@ -32,7 +32,7 @@ pub fn lang(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
} }
} }
ctx.push_parbreak(); ctx.parbreak();
}) })
} }

View File

@ -14,7 +14,7 @@ use crate::syntax::{HeadingNode, RawNode};
/// A template that inserts a line break. /// A template that inserts a line break.
pub fn linebreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value { pub fn linebreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value {
Value::template(Node::LINEBREAK, move |ctx| { Value::template(Node::LINEBREAK, move |ctx| {
ctx.push_linebreak(); ctx.linebreak();
}) })
} }
@ -24,7 +24,7 @@ pub fn linebreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value {
/// A template that inserts a paragraph break. /// A template that inserts a paragraph break.
pub fn parbreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value { pub fn parbreak(_: &mut EvalContext, _: &mut FuncArgs) -> Value {
Value::template(Node::PARBREAK, move |ctx| { Value::template(Node::PARBREAK, move |ctx| {
ctx.push_parbreak(); ctx.parbreak();
}) })
} }
@ -118,7 +118,7 @@ pub fn heading(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
body.exec(ctx); body.exec(ctx);
ctx.state = snapshot; ctx.state = snapshot;
ctx.push_parbreak(); ctx.parbreak();
}) })
} }
@ -155,7 +155,7 @@ pub fn raw(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
Value::template(Node::RAW, move |ctx| { Value::template(Node::RAW, move |ctx| {
if block { if block {
ctx.push_parbreak(); ctx.parbreak();
} }
let snapshot = ctx.state.clone(); let snapshot = ctx.state.clone();
@ -164,7 +164,7 @@ pub fn raw(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
ctx.state = snapshot; ctx.state = snapshot;
if block { if block {
ctx.push_parbreak(); ctx.parbreak();
} }
}) })
} }

View File

@ -32,6 +32,6 @@ pub fn pad(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
Value::template("pad", move |ctx| { Value::template("pad", move |ctx| {
let child = ctx.exec_group(&body).into(); let child = ctx.exec_group(&body).into();
ctx.push_into_par(PadNode { padding, child }); ctx.push(PadNode { padding, child });
}) })
} }

View File

@ -83,13 +83,13 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
std::mem::swap(&mut page.size.width, &mut page.size.height); std::mem::swap(&mut page.size.width, &mut page.size.height);
} }
ctx.finish_page(false, true, span); ctx.pagebreak(false, true, span);
if let Some(body) = &body { if let Some(body) = &body {
// TODO: Restrict body to a single page? // TODO: Restrict body to a single page?
body.exec(ctx); body.exec(ctx);
ctx.state = snapshot; ctx.state = snapshot;
ctx.finish_page(true, false, span); ctx.pagebreak(true, false, span);
} }
}) })
} }
@ -101,6 +101,6 @@ pub fn page(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
pub fn pagebreak(_: &mut EvalContext, args: &mut FuncArgs) -> Value { pub fn pagebreak(_: &mut EvalContext, args: &mut FuncArgs) -> Value {
let span = args.span; let span = args.span;
Value::template("pagebreak", move |ctx| { Value::template("pagebreak", move |ctx| {
ctx.finish_page(true, true, span); ctx.pagebreak(true, true, span);
}) })
} }

View File

@ -27,6 +27,6 @@ pub fn par(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
ctx.state.par.word_spacing = word_spacing; ctx.state.par.word_spacing = word_spacing;
} }
ctx.push_parbreak(); ctx.parbreak();
}) })
} }

View File

@ -63,13 +63,13 @@ fn rect_impl(
let node = FixedNode { width, height, aspect, child }; let node = FixedNode { width, height, aspect, child };
if let Some(color) = fill { if let Some(color) = fill {
ctx.push_into_par(BackgroundNode { ctx.push(BackgroundNode {
shape: BackgroundShape::Rect, shape: BackgroundShape::Rect,
fill: Fill::Color(color), fill: Fill::Color(color),
child: node.into(), child: node.into(),
}); });
} else { } else {
ctx.push_into_par(node); ctx.push(node);
} }
}) })
} }
@ -146,13 +146,13 @@ fn ellipse_impl(
}; };
if let Some(color) = fill { if let Some(color) = fill {
ctx.push_into_par(BackgroundNode { ctx.push(BackgroundNode {
shape: BackgroundShape::Ellipse, shape: BackgroundShape::Ellipse,
fill: Fill::Color(color), fill: Fill::Color(color),
child: node.into(), child: node.into(),
}); });
} else { } else {
ctx.push_into_par(node); ctx.push(node);
} }
}) })
} }

View File

@ -32,7 +32,7 @@ fn spacing_impl(
Value::template(name, move |ctx| { Value::template(name, move |ctx| {
if let Some(linear) = spacing { if let Some(linear) = spacing {
let amount = linear.resolve(ctx.state.font.resolve_size()); let amount = linear.resolve(ctx.state.font.resolve_size());
ctx.push_spacing(axis, amount, 0); ctx.push_spacing(axis, amount);
} }
}) })
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -14,8 +14,8 @@
// Dictionary is not traversed in insertion order. // Dictionary is not traversed in insertion order.
// Should output `age: 1, name: Typst,`. // Should output `age: 1, name: Typst,`.
#for k, v in (name: "Typst", age: 2) [ #for k, v in (Name: "Typst", Age: 2) [
{k}: {v}, \ {k}: {v}.
] ]
// String. // String.

View File

@ -7,7 +7,7 @@
// Top-level paragraph fills page, boxed paragraph only when width is fixed. // Top-level paragraph fills page, boxed paragraph only when width is fixed.
L #right[R] \ L #right[R] \
#rect(width: 50pt)[L #right[R]] \ #rect(width: 50pt)[L #right[R]] \
#rect[L #right[R]] \ #rect[L #right[R]]
// Pad inherits expansion behaviour. // Pad inherits expansion behaviour.
#pad[PL #right[PR]] \ #pad[PL #right[PR]] \

View File

@ -60,7 +60,7 @@
#args[a] \ #args[a] \
#args(a) \ #args(a) \
#args(a, [b]) \ #args(a, [b]) \
#args(a)[b] \ #args(a)[b]
// Template can be argument or body depending on whitespace. // Template can be argument or body depending on whitespace.
#if "template" == type[b] [Sure ] #if "template" == type[b] [Sure ]

View File

@ -53,7 +53,7 @@ Emoji: 🐪, 🌋, 🏞
#font(sans-serif: "PT Sans") #font(sans-serif: "PT Sans")
#font(sans-serif)[Sans-serif.] \ #font(sans-serif)[Sans-serif.] \
#font(monospace)[Monospace.] \ #font(monospace)[Monospace.] \
#font(monospace, monospace: ("Nope", "Latin Modern Math"))[Math.] \ #font(monospace, monospace: ("Nope", "Latin Modern Math"))[Math.]
--- ---
// Ref: false // Ref: false

View File

@ -10,17 +10,11 @@ Line \ Break
// Directly before word does not work. // Directly before word does not work.
No \Break No \Break
--- \ Before
// Leading line break.
\ Leading
// Trailing before paragraph break. Multiple \ \ \
Trailing 1 \
Trailing 2 Times
// Trailing before end of document.
Trailing 3 \
--- ---
#let linebreak() = [ #let linebreak() = [
@ -28,4 +22,4 @@ Trailing 3 \
#circle(radius: 2pt, fill: #000) \ #circle(radius: 2pt, fill: #000) \
] ]
A \ B \ C \ A \ B \ C

View File

@ -1,5 +1,4 @@
// Test the non breaking space. // Test the non breaking space.
--- ---
// Parsed correctly, but not actually doing anything at the moment. The non-breaking~space does work.
The non-breaking~space does not work.

View File

@ -9,7 +9,7 @@
{name} \ {name} \
{ke-bab} \ {ke-bab} \
{α} \ {α}
// Error: 2-3 unknown variable // Error: 2-3 unknown variable
{_} {_}
@ -18,7 +18,7 @@
// Literal values. // Literal values.
{none} (empty) \ {none} (empty) \
{true} \ {true} \
{false} \ {false}
--- ---
// Numerical values. // Numerical values.
@ -33,16 +33,16 @@
{2.5rad} \ {2.5rad} \
{45deg} \ {45deg} \
// Not in monospace via repr. // Not in monospace via repr.
#repr(45deg) \ #repr(45deg)
--- ---
// Colors. // Colors.
{#f7a20500} \ {#f7a20500}
--- ---
// Strings and escaping. // Strings and escaping.
{"hi"} \ {"hi"} \
{"a\n[]\"\u{1F680}string"} \ {"a\n[]\"\u{1F680}string"}
--- ---
// Templates. // Templates.
@ -54,4 +54,4 @@
{rect} \ {rect} \
{f} \ {f} \
{() => none} \ {() => none}

View File

@ -7,7 +7,7 @@
A#let;B \ A#let;B \
A#let x = 1;B #test(x, 1) \ A#let x = 1;B #test(x, 1) \
A #let x = 2;B #test(x, 2) \ A #let x = 2;B #test(x, 2) \
A#let x = 3; B #test(x, 3) \ A#let x = 3; B #test(x, 3)
--- ---
// Spacing around if-else. // Spacing around if-else.
@ -17,7 +17,7 @@ A#if true [B] C \
A #if true{"B"}C \ A #if true{"B"}C \
A #if true{"B"} C \ A #if true{"B"} C \
A#if false [] #else [B]C \ A#if false [] #else [B]C \
A#if true [B] #else [] C \ A#if true [B] #else [] C
--- ---
// Spacing around while loop. // Spacing around while loop.
@ -25,11 +25,11 @@ A#if true [B] #else [] C \
#let c = true; A#while c [{c = false}B]C \ #let c = true; A#while c [{c = false}B]C \
#let c = true; A#while c [{c = false}B] C \ #let c = true; A#while c [{c = false}B] C \
#let c = true; A #while c { c = false; "B" }C \ #let c = true; A #while c { c = false; "B" }C \
#let c = true; A #while c { c = false; "B" } C \ #let c = true; A #while c { c = false; "B" } C
--- ---
// Spacing around for loop. // Spacing around for loop.
A#for _ in (none,) [B]C \ A#for _ in (none,) [B]C \
A#for _ in (none,) [B] C \ A#for _ in (none,) [B] C \
A #for _ in (none,) {"B"}C \ A #for _ in (none,) {"B"}C