Write spaces and linebreaks into text runs ✒

This commit is contained in:
Laurenz 2021-03-28 12:44:05 +02:00
parent 318eb9021e
commit 8c27dc1010
4 changed files with 47 additions and 48 deletions

View File

@ -8,7 +8,6 @@ use crate::geom::{Align, Dir, Gen, GenAxis, Length, Linear, Sides, Size};
use crate::layout::{
AnyNode, PadNode, PageRun, ParChild, ParNode, StackChild, StackNode, TextNode, Tree,
};
use crate::parse::{is_newline, Scanner};
use crate::syntax::Span;
/// The context for execution.
@ -73,28 +72,14 @@ impl<'a> ExecContext<'a> {
/// 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));
self.stack.par.push_soft(self.make_text_node(" "));
}
/// Push text into the active paragraph.
///
/// The text is split into lines at newlines.
pub fn push_text(&mut self, text: &str) {
let mut scanner = Scanner::new(text);
let mut text = String::new();
while let Some(c) = scanner.eat_merging_crlf() {
if is_newline(c) {
self.stack.par.push_text(mem::take(&mut text), &self.state);
self.linebreak();
} else {
text.push(c);
}
}
self.stack.par.push_text(text, &self.state);
pub fn push_text(&mut self, text: impl Into<String>) {
self.stack.par.push(self.make_text_node(text));
}
/// Push spacing into paragraph or stack depending on `axis`.
@ -112,7 +97,7 @@ impl<'a> ExecContext<'a> {
/// Apply a forced line break.
pub fn linebreak(&mut self) {
self.stack.par.push_hard(ParChild::Linebreak);
self.stack.par.push_hard(self.make_text_node("\n"));
}
/// Apply a forced paragraph break.
@ -140,6 +125,12 @@ impl<'a> ExecContext<'a> {
self.pagebreak(true, false, Span::default());
Pass::new(self.tree, self.diags)
}
fn make_text_node(&self, text: impl Into<String>) -> ParChild {
let align = self.state.aligns.cross;
let props = self.state.font.resolve_props();
ParChild::Text(TextNode { text: text.into(), props }, align)
}
}
struct PageBuilder {
@ -231,24 +222,10 @@ impl ParBuilder {
}
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;
}
if let Some(soft) = self.last.any() {
self.push_inner(soft);
}
self.children.push(ParChild::Text(TextNode { text, props }, align));
self.push_inner(child);
}
fn push_soft(&mut self, child: ParChild) {
@ -257,6 +234,19 @@ impl ParBuilder {
fn push_hard(&mut self, child: ParChild) {
self.last.hard();
self.push_inner(child);
}
fn push_inner(&mut self, child: ParChild) {
if let ParChild::Text(curr, curr_align) = &child {
if let Some(ParChild::Text(prev, prev_align)) = self.children.last_mut() {
if prev_align == curr_align && prev.props == curr.props {
prev.text.push_str(&curr.text);
return;
}
}
}
self.children.push(child);
}

View File

@ -64,7 +64,7 @@ impl ExecWithMap for Tree {
impl ExecWithMap for Node {
fn exec_with_map(&self, ctx: &mut ExecContext, map: &NodeMap) {
match self {
Node::Text(text) => ctx.push_text(text),
Node::Text(text) => ctx.push_text(text.clone()),
Node::Space => ctx.push_word_space(),
_ => map[&(self as *const _)].exec(ctx),
}
@ -75,9 +75,9 @@ impl Exec for Value {
fn exec(&self, ctx: &mut ExecContext) {
match self {
Value::None => {}
Value::Int(v) => ctx.push_text(&pretty(v)),
Value::Float(v) => ctx.push_text(&pretty(v)),
Value::Str(v) => ctx.push_text(v),
Value::Int(v) => ctx.push_text(pretty(v)),
Value::Float(v) => ctx.push_text(pretty(v)),
Value::Str(v) => ctx.push_text(v.clone()),
Value::Template(v) => v.exec(ctx),
Value::Error => {}
other => {
@ -85,7 +85,7 @@ impl Exec for Value {
// the representation in monospace.
let prev = Rc::clone(&ctx.state.font.families);
ctx.set_monospace();
ctx.push_text(&pretty(other));
ctx.push_text(pretty(other));
ctx.state.font.families = prev;
}
}
@ -104,7 +104,7 @@ impl Exec for TemplateNode {
fn exec(&self, ctx: &mut ExecContext) {
match self {
Self::Tree { tree, map } => tree.exec_with_map(ctx, &map),
Self::Str(v) => ctx.push_text(v),
Self::Str(v) => ctx.push_text(v.clone()),
Self::Func(v) => v.exec(ctx),
}
}

View File

@ -15,7 +15,7 @@ pub struct ParNode {
}
/// A child of a paragraph node.
#[derive(Debug, Clone, PartialEq)]
#[derive(Clone, PartialEq)]
pub enum ParChild {
/// Spacing between other nodes.
Spacing(Length),
@ -23,8 +23,18 @@ pub enum ParChild {
Text(TextNode, Align),
/// Any child node and how to align it in its line.
Any(AnyNode, Align),
/// A forced linebreak.
Linebreak,
}
impl Debug for ParChild {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Spacing(amount) => write!(f, "Spacing({:?})", amount),
Self::Text(node, align) => write!(f, "Text({:?}, {:?})", node.text, align),
Self::Any(any, align) => {
f.debug_tuple("Any").field(any).field(align).finish()
}
}
}
}
/// A consecutive, styled run of text.
@ -38,7 +48,7 @@ pub struct TextNode {
impl Debug for TextNode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Text({})", self.text)
write!(f, "Text({:?})", self.text)
}
}
@ -57,7 +67,6 @@ impl Layout for ParNode {
layouter.push_frame(frame, align);
}
}
ParChild::Linebreak => layouter.finish_line(),
}
}
layouter.finish()

View File

@ -160,7 +160,7 @@ pub fn raw(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
let snapshot = ctx.state.clone();
ctx.set_monospace();
ctx.push_text(&text);
ctx.push_text(text.clone());
ctx.state = snapshot;
if block {