diff options
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/actions.rs | 13 | ||||
| -rw-r--r-- | src/layout/mod.rs | 4 | ||||
| -rw-r--r-- | src/layout/text.rs | 55 | ||||
| -rw-r--r-- | src/layout/tree.rs | 7 |
4 files changed, 36 insertions, 43 deletions
diff --git a/src/layout/actions.rs b/src/layout/actions.rs index b0d2c21d..4ab9fdb1 100644 --- a/src/layout/actions.rs +++ b/src/layout/actions.rs @@ -1,6 +1,7 @@ //! Drawing and cofiguration actions composing layouts. use std::fmt::{self, Display, Formatter}; +use toddle::query::FontIndex; use super::*; use LayoutAction::*; @@ -11,7 +12,7 @@ pub enum LayoutAction { /// Move to an absolute position. MoveAbsolute(Size2D), /// Set the font by index and font size. - SetFont(usize, Size), + SetFont(FontIndex, Size), /// Write text starting at the current position. WriteText(String), /// Visualize a box for debugging purposes. @@ -22,7 +23,7 @@ impl Serialize for LayoutAction { fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> { match self { MoveAbsolute(s) => write!(f, "m {:.4} {:.4}", s.x.to_pt(), s.y.to_pt()), - SetFont(i, s) => write!(f, "f {} {}", i, s.to_pt()), + SetFont(i, s) => write!(f, "f {} {} {}", i.id, i.variant, s.to_pt()), WriteText(s) => write!(f, "w {}", s), DebugBox(s) => write!(f, "b {} {}", s.x.to_pt(), s.y.to_pt()), } @@ -34,7 +35,7 @@ impl Display for LayoutAction { use LayoutAction::*; match self { MoveAbsolute(s) => write!(f, "move {} {}", s.x, s.y), - SetFont(i, s) => write!(f, "font {} {}", i, s), + SetFont(i, s) => write!(f, "font {} {} {}", i.id, i.variant, s), WriteText(s) => write!(f, "write \"{}\"", s), DebugBox(s) => write!(f, "box {}", s), } @@ -58,9 +59,9 @@ debug_display!(LayoutAction); pub struct LayoutActions { pub origin: Size2D, actions: Vec<LayoutAction>, - active_font: (usize, Size), + active_font: (FontIndex, Size), next_pos: Option<Size2D>, - next_font: Option<(usize, Size)>, + next_font: Option<(FontIndex, Size)>, } impl LayoutActions { @@ -69,7 +70,7 @@ impl LayoutActions { LayoutActions { actions: vec![], origin: Size2D::ZERO, - active_font: (std::usize::MAX, Size::ZERO), + active_font: (FontIndex::MAX, Size::ZERO), next_pos: None, next_font: None, } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 53c3e91e..be1ed43c 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -2,7 +2,7 @@ use std::io::{self, Write}; use smallvec::SmallVec; -use toddle::query::SharedFontLoader; +use toddle::query::{SharedFontLoader, FontIndex}; use crate::size::{Size, Size2D, SizeBox}; use crate::style::LayoutStyle; @@ -58,7 +58,7 @@ pub struct Layout { impl Layout { /// Returns a vector with all used font indices. - pub fn find_used_fonts(&self) -> Vec<usize> { + pub fn find_used_fonts(&self) -> Vec<FontIndex> { let mut fonts = Vec::new(); for action in &self.actions { if let LayoutAction::SetFont(index, _) = action { diff --git a/src/layout/text.rs b/src/layout/text.rs index e9721429..96704f60 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -1,4 +1,4 @@ -use toddle::query::{SharedFontLoader, FontQuery, FontClass}; +use toddle::query::{SharedFontLoader, FontQuery, FontIndex}; use toddle::tables::{CharMap, Header, HorizontalMetrics}; use crate::size::{Size, Size2D}; @@ -30,9 +30,8 @@ struct TextLayouter<'a, 'p> { text: &'a str, actions: LayoutActions, buffer: String, - active_font: usize, + active_font: FontIndex, width: Size, - classes: Vec<FontClass>, } impl<'a, 'p> TextLayouter<'a, 'p> { @@ -43,9 +42,8 @@ impl<'a, 'p> TextLayouter<'a, 'p> { text, actions: LayoutActions::new(), buffer: String::new(), - active_font: std::usize::MAX, + active_font: FontIndex::MAX, width: Size::ZERO, - classes: ctx.style.classes.clone(), } } @@ -95,39 +93,34 @@ impl<'a, 'p> TextLayouter<'a, 'p> { /// Select the best font for a character and return its index along with /// the width of the char in the font. - fn select_font(&mut self, c: char) -> LayoutResult<(usize, Size)> { + fn select_font(&mut self, c: char) -> LayoutResult<(FontIndex, Size)> { let mut loader = self.ctx.loader.borrow_mut(); - for class in &self.ctx.style.fallback { - self.classes.push(class.clone()); + let query = FontQuery { + fallback: &self.ctx.style.fallback, + variant: self.ctx.style.variant, + c, + }; - let query = FontQuery { - chars: &[c], - classes: &self.classes, - }; + if let Some((font, index)) = loader.get(query) { + let font_unit_ratio = 1.0 / (font.read_table::<Header>()?.units_per_em as f32); + let font_unit_to_size = |x| Size::pt(font_unit_ratio * x); - if let Some((font, index)) = loader.get(query) { - let font_unit_ratio = 1.0 / (font.read_table::<Header>()?.units_per_em as f32); - let font_unit_to_size = |x| Size::pt(font_unit_ratio * x); + let glyph = font + .read_table::<CharMap>()? + .get(c) + .expect("select_font: font should have char"); - let glyph = font - .read_table::<CharMap>()? - .get(c) - .expect("select_font: font should have char"); + let glyph_width = font + .read_table::<HorizontalMetrics>()? + .get(glyph) + .expect("select_font: font should have glyph") + .advance_width as f32; - let glyph_width = font - .read_table::<HorizontalMetrics>()? - .get(glyph) - .expect("select_font: font should have glyph") - .advance_width as f32; + let char_width = font_unit_to_size(glyph_width) + * self.ctx.style.font_size().to_pt(); - let char_width = font_unit_to_size(glyph_width) - * self.ctx.style.font_size().to_pt(); - - return Ok((index, char_width)); - } - - self.classes.pop(); + return Ok((index, char_width)); } error!("no suitable font for character `{}`", c); diff --git a/src/layout/tree.rs b/src/layout/tree.rs index db59ca8d..e77fd528 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -1,5 +1,4 @@ use smallvec::smallvec; -use toddle::query::FontClass; use crate::func::Command; use crate::syntax::{SyntaxTree, Node, FuncCall}; @@ -45,9 +44,9 @@ impl<'a, 'p> TreeLayouter<'a, 'p> { Node::Space => self.layout_space(), Node::Newline => self.layout_paragraph()?, - Node::ToggleItalics => self.style.text.toggle_class(FontClass::Italic), - Node::ToggleBold => self.style.text.toggle_class(FontClass::Bold), - Node::ToggleMonospace => self.style.text.toggle_class(FontClass::Monospace), + Node::ToggleItalics => {}, + Node::ToggleBold => {}, + Node::ToggleMonospace => {}, Node::Func(func) => self.layout_func(func)?, } |
