From ec5384c97f24c3e6d8284926fd3e415f47fe2b04 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 25 Jul 2021 01:16:38 +0200 Subject: State-based monospace handling --- src/exec/context.rs | 19 +++++++++---------- src/exec/mod.rs | 17 ++++------------- src/exec/state.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 53 insertions(+), 31 deletions(-) (limited to 'src/exec') diff --git a/src/exec/context.rs b/src/exec/context.rs index 925fd7de..13e53b09 100644 --- a/src/exec/context.rs +++ b/src/exec/context.rs @@ -1,7 +1,7 @@ use std::mem; use std::rc::Rc; -use super::{Exec, ExecWithMap, FontFamily, State}; +use super::{Exec, ExecWithMap, State}; use crate::diag::{Diag, DiagSet, Pass}; use crate::eco::EcoString; use crate::eval::{ExprMap, Template}; @@ -44,15 +44,6 @@ impl ExecContext { self.diags.insert(diag); } - /// Set the font to monospace. - pub fn set_monospace(&mut self) { - self.state - .font_mut() - .families_mut() - .list - .insert(0, FontFamily::Monospace); - } - /// Execute a template and return the result as a stack node. pub fn exec_template_stack(&mut self, template: &Template) -> StackNode { self.exec_stack(|ctx| template.exec(ctx)) @@ -83,6 +74,14 @@ impl ExecContext { self.stack.par.push(self.make_text_node(text)); } + /// Push text, but in monospace. + pub fn push_monospace_text(&mut self, text: impl Into) { + let prev = Rc::clone(&self.state.font); + self.state.font_mut().monospace = true; + self.push_text(text); + self.state.font = prev; + } + /// Push a word space into the active paragraph. pub fn push_word_space(&mut self) { self.stack.par.push_soft(self.make_text_node(" ")); diff --git a/src/exec/mod.rs b/src/exec/mod.rs index d61e0793..76857b44 100644 --- a/src/exec/mod.rs +++ b/src/exec/mod.rs @@ -7,7 +7,6 @@ pub use context::*; pub use state::*; use std::fmt::Write; -use std::rc::Rc; use crate::diag::Pass; use crate::eco::EcoString; @@ -75,10 +74,7 @@ impl Exec for RawNode { ctx.parbreak(); } - let snapshot = Rc::clone(&ctx.state.font); - ctx.set_monospace(); - ctx.push_text(&self.text); - ctx.state.font = snapshot; + ctx.push_monospace_text(&self.text); if self.block { ctx.parbreak(); @@ -143,14 +139,9 @@ impl Exec for Value { Value::Str(v) => ctx.push_text(v), Value::Template(v) => v.exec(ctx), Value::Error => {} - other => { - // For values which can't be shown "naturally", we print - // the representation in monospace. - let prev = Rc::clone(&ctx.state.font.families); - ctx.set_monospace(); - ctx.push_text(pretty(other)); - ctx.state.font_mut().families = prev; - } + // For values which can't be shown "naturally", we print the + // representation in monospace. + other => ctx.push_monospace_text(pretty(other)), } } } diff --git a/src/exec/state.rs b/src/exec/state.rs index f99a8901..624394ba 100644 --- a/src/exec/state.rs +++ b/src/exec/state.rs @@ -111,6 +111,14 @@ pub struct FontState { pub families: Rc, /// The selected font variant. pub variant: FontVariant, + /// Whether the strong toggle is active or inactive. This determines + /// whether the next `*` adds or removes font weight. + pub strong: bool, + /// Whether the emphasis toggle is active or inactive. This determines + /// whether the next `_` makes italic or non-italic. + pub emph: bool, + /// Whether the monospace toggle is active or inactive. + pub monospace: bool, /// The font size. pub size: Length, /// The top end of the text bounding box. @@ -119,12 +127,6 @@ pub struct FontState { pub bottom_edge: VerticalFontMetric, /// Glyph color. pub fill: Paint, - /// Whether the strong toggle is active or inactive. This determines - /// whether the next `*` adds or removes font weight. - pub strong: bool, - /// Whether the emphasis toggle is active or inactive. This determines - /// whether the next `_` makes italic or non-italic. - pub emph: bool, /// The specifications for a strikethrough line, if any. pub strikethrough: Option>, /// The specifications for a underline, if any. @@ -138,6 +140,35 @@ impl FontState { pub fn families_mut(&mut self) -> &mut FamilyList { Rc::make_mut(&mut self.families) } + + /// The canonical family iterator. + pub fn families(&self) -> impl Iterator + Clone { + let head = if self.monospace { + self.families.monospace.as_slice() + } else { + &[] + }; + head.iter().map(String::as_str).chain(self.families.iter()) + } + + /// The canonical variant with `strong` and `emph` factored in. + pub fn variant(&self) -> FontVariant { + let mut variant = self.variant; + + if self.strong { + variant.weight = variant.weight.thicken(300); + } + + if self.emph { + variant.style = match variant.style { + FontStyle::Normal => FontStyle::Italic, + FontStyle::Italic => FontStyle::Normal, + FontStyle::Oblique => FontStyle::Normal, + } + } + + variant + } } impl Default for FontState { @@ -149,12 +180,13 @@ impl Default for FontState { weight: FontWeight::REGULAR, stretch: FontStretch::NORMAL, }, + strong: false, + emph: false, + monospace: false, size: Length::pt(11.0), top_edge: VerticalFontMetric::CapHeight, bottom_edge: VerticalFontMetric::Baseline, fill: Paint::Color(Color::Rgba(RgbaColor::BLACK)), - strong: false, - emph: false, strikethrough: None, underline: None, overline: None, -- cgit v1.2.3