From 4e5f85aa4ac0d6b51323bb2a6e1fbd3f4f46babb Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 10 Mar 2021 17:42:47 +0100 Subject: =?UTF-8?q?Pad=20function=20=F0=9F=94=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/exec/context.rs | 116 ++++++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 59 deletions(-) (limited to 'src/exec') diff --git a/src/exec/context.rs b/src/exec/context.rs index b37b15cb..d491a251 100644 --- a/src/exec/context.rs +++ b/src/exec/context.rs @@ -80,6 +80,45 @@ impl<'a> ExecContext<'a> { self.inner.push(node); } + /// Push a normal word space. + pub fn push_space(&mut self) { + let em = self.state.font.font_size(); + self.push(NodeSpacing { + amount: self.state.par.word_spacing.resolve(em), + softness: Softness::Soft, + }); + } + + /// 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; + } + } + + /// Execute the body of a function and return the result as a stack node. + pub fn exec_body(&mut self, body: &ValueTemplate, expand: Spec) -> Node { + let dirs = self.state.dirs; + let align = self.state.align; + + self.start_group(ContentGroup); + self.start_par_group(); + body.exec(self); + self.end_par_group(); + let children = self.end_group::().1; + + NodeStack { dirs, align, expand, children }.into() + } + /// Start a page group based on the active page state. /// /// The `softness` is a hint on whether empty pages should be kept in the @@ -130,22 +169,6 @@ impl<'a> ExecContext<'a> { group.softness } - /// Start a content group. - /// - /// This also starts an inner paragraph. - pub fn start_content_group(&mut self) { - self.start_group(ContentGroup); - self.start_par_group(); - } - - /// End a content group and return the resulting nodes. - /// - /// This also ends an inner paragraph. - pub fn end_content_group(&mut self) -> Vec { - self.end_par_group(); - self.end_group::().1 - } - /// Start a paragraph group based on the active text state. pub fn start_par_group(&mut self) { let em = self.state.font.font_size(); @@ -218,29 +241,28 @@ impl<'a> ExecContext<'a> { } } - /// Push a normal word space. - pub fn push_space(&mut self) { + /// Set the font to monospace. + pub fn apply_monospace(&mut self) { + let families = self.state.font.families_mut(); + families.list.insert(0, "monospace".to_string()); + families.flatten(); + } + + /// Apply a forced line break. + pub fn apply_linebreak(&mut self) { + self.end_par_group(); + self.start_par_group(); + } + + /// Apply a forced paragraph break. + pub fn apply_parbreak(&mut self) { + self.end_par_group(); let em = self.state.font.font_size(); self.push(NodeSpacing { - amount: self.state.par.word_spacing.resolve(em), + amount: self.state.par.par_spacing.resolve(em), softness: Softness::Soft, }); - } - - /// 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; - } + self.start_par_group(); } /// Construct a text node from the given string based on the active text @@ -269,30 +291,6 @@ impl<'a> ExecContext<'a> { variant, } } - - /// Set the font to monospace. - pub fn apply_monospace(&mut self) { - let families = self.state.font.families_mut(); - families.list.insert(0, "monospace".to_string()); - families.flatten(); - } - - /// Apply a forced line break. - pub fn apply_linebreak(&mut self) { - self.end_par_group(); - self.start_par_group(); - } - - /// Apply a forced paragraph break. - pub fn apply_parbreak(&mut self) { - self.end_par_group(); - let em = self.state.font.font_size(); - self.push(NodeSpacing { - amount: self.state.par.par_spacing.resolve(em), - softness: Softness::Soft, - }); - self.start_par_group(); - } } /// Defines how an item interacts with surrounding items. -- cgit v1.2.3