From de37a056ed3e90d8ba93c4b3a315a8046ef53484 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 21 Feb 2021 11:43:25 +0100 Subject: =?UTF-8?q?Split=20pushed=20text=20at=20newlines=20=E2=9C=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/exec/context.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src/exec/context.rs') diff --git a/src/exec/context.rs b/src/exec/context.rs index 5ff55c00..62f50880 100644 --- a/src/exec/context.rs +++ b/src/exec/context.rs @@ -9,6 +9,7 @@ use crate::geom::{ChildAlign, Dir, Gen, LayoutDirs, Length, Linear, Sides, Size} use crate::layout::{ Expansion, Node, NodePad, NodePages, NodePar, NodeSpacing, NodeStack, NodeText, Tree, }; +use crate::parse::is_newline; /// The context for execution. #[derive(Debug)] @@ -217,7 +218,7 @@ impl<'a> ExecContext<'a> { } } - /// Push a normal space. + /// Push a normal word space. pub fn push_space(&mut self) { let em = self.state.font.font_size(); self.push(NodeSpacing { @@ -226,10 +227,20 @@ impl<'a> ExecContext<'a> { }); } - /// Push a text node. - pub fn push_text(&mut self, text: impl Into) { - let node = self.make_text_node(text.into()); - self.push(node); + /// Push text into the context. + /// + /// The text is split into lines at newlines. + pub fn push_text(&mut self, text: &str) { + let mut newline = false; + for line in text.split_terminator(is_newline) { + if newline { + self.apply_linebreak(); + } + + let node = self.make_text_node(line.into()); + self.push(node); + newline = true; + } } /// Construct a text node from the given string based on the active text -- cgit v1.2.3