From e5898439665d6708ab063aefc5745c9bad9dcb95 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 1 Apr 2019 10:01:42 +0200 Subject: =?UTF-8?q?Implement=20font=20fallback=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine/mod.rs | 338 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 256 insertions(+), 82 deletions(-) (limited to 'src/engine') diff --git a/src/engine/mod.rs b/src/engine/mod.rs index c588e5c3..d9d16965 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -1,8 +1,11 @@ //! Core typesetting engine. +use std::cell::{RefCell, Ref}; +use std::collections::HashMap; +use std::mem::swap; use crate::syntax::{SyntaxTree, Node}; use crate::doc::{Document, Page, Text, TextCommand}; -use crate::font::{Font, FontFamily, FontFilter, FontError}; +use crate::font::{Font, FontFamily, FontInfo, FontError}; use crate::Context; mod size; @@ -11,16 +14,21 @@ pub use size::Size; /// The core typesetting engine, transforming an abstract syntax tree into a document. pub struct Engine<'t> { - // Immutable + // Input tree: &'t SyntaxTree<'t>, ctx: &'t Context<'t>, - // Mutable - fonts: Vec, - active_font: usize, + // Internal + font_loader: FontLoader<'t>, + + // Output text_commands: Vec, - current_line: String, - current_width: Size, + + // Intermediates + active_font: usize, + current_text: String, + current_line_width: Size, + current_max_vertical_move: Size, } impl<'t> Engine<'t> { @@ -29,121 +37,287 @@ impl<'t> Engine<'t> { Engine { tree, ctx: context, - fonts: vec![], - active_font: 0, + font_loader: FontLoader::new(context), text_commands: vec![], - current_line: String::new(), - current_width: Size::zero(), + active_font: std::usize::MAX, + current_text: String::new(), + current_line_width: Size::zero(), + current_max_vertical_move: Size::zero(), } } /// Generate the abstract document. pub(crate) fn typeset(mut self) -> TypeResult { - // Load font defined by style - let mut font = None; - let filter = FontFilter::new(&self.ctx.style.font_families); - for provider in &self.ctx.font_providers { - let available = provider.available(); - for info in available { - if filter.matches(info) { - if let Some(mut source) = provider.get(info) { - let mut program = Vec::new(); - source.read_to_end(&mut program)?; - font = Some(Font::new(program)?); - break; - } - } + // Start by moving to a suitable position. + self.move_start(); + + // Iterate through the documents nodes. + for node in &self.tree.nodes { + match node { + Node::Word(word) => self.write_word(word)?, + Node::Space => self.write_space()?, + Node::Newline => (), + Node::ToggleItalics | Node::ToggleBold | Node::ToggleMath => unimplemented!(), + Node::Func(_) => unimplemented!(), } } - let font = match font { - Some(font) => font, - None => return Err(TypesetError::MissingFont), - }; + // Flush the text buffer. + self.write_buffered_text(); + + let fonts = self.font_loader.into_fonts(); - self.fonts.push(font); - self.active_font = 0; + println!("fonts: {:?}", fonts.len()); + + // Create a document with one page from the contents. + Ok(Document { + pages: vec![Page { + width: self.ctx.style.width, + height: self.ctx.style.height, + text: vec![Text { + commands: self.text_commands, + }], + }], + fonts, + }) + } + /// Move to the starting position defined by the style. + fn move_start(&mut self) { // Move cursor to top-left position self.text_commands.push(TextCommand::Move( self.ctx.style.margin_left, self.ctx.style.height - self.ctx.style.margin_top )); + } - // Set the current font - self.text_commands.push(TextCommand::SetFont(0, self.ctx.style.font_size)); + /// Move to a new line. + fn move_newline(&mut self) { + let vertical_move = - if self.current_max_vertical_move == Size::zero() { + // If max vertical move is still zero, the line is empty and we take the + // font size from the previous line. + self.ctx.style.font_size + * self.ctx.style.line_spacing + * self.font_loader.get_at(self.active_font).metrics.ascender + } else { + self.current_max_vertical_move + }; - // Iterate through the documents nodes. - for node in &self.tree.nodes { - match node { - Node::Word(word) => self.write_word(word), + self.text_commands.push(TextCommand::Move(Size::zero(), vertical_move)); + self.current_max_vertical_move = Size::zero(); + self.current_line_width = Size::zero(); + } - Node::Space => self.write_space(), - Node::Newline => (), + /// Set the current font. + fn set_font(&mut self, index: usize) { + self.text_commands.push(TextCommand::SetFont(index, self.ctx.style.font_size)); + self.active_font = index; + } - Node::ToggleItalics | Node::ToggleBold | Node::ToggleMath => unimplemented!(), - Node::Func(_) => unimplemented!(), + /// Write a word. + fn write_word(&mut self, word: &str) -> TypeResult<()> { + let width = self.width(word)?; + + // If this would overflow, we move to a new line and finally write the previous one. + if self.would_overflow(width) { + self.write_buffered_text(); + self.move_newline(); + } + + for c in word.chars() { + let (index, _) = self.get_font_for(c)?; + if index != self.active_font { + self.write_buffered_text(); + self.set_font(index); } + self.current_text.push(c); + let char_width = self.char_width(c).unwrap(); + self.current_line_width += char_width; } - // Create a page from the contents. - let page = Page { - width: self.ctx.style.width, - height: self.ctx.style.height, - text: vec![Text { - commands: self.text_commands, - }], - }; + Ok(()) + } - Ok(Document { - pages: vec![page], - fonts: self.fonts, - }) + /// Write the space character: `' '`. + fn write_space(&mut self) -> TypeResult<()> { + let space_width = self.char_width(' ')?; + + if !self.would_overflow(space_width) && self.current_line_width > Size::zero() { + self.write_word(" ")?; + } + + Ok(()) } - fn write_word(&mut self, word: &str) { - let font = &self.fonts[self.active_font]; + /// Write a text command with the buffered text. + fn write_buffered_text(&mut self) { + if !self.current_text.is_empty() { + let mut current_text = String::new(); + swap(&mut self.current_text, &mut current_text); + self.text_commands.push(TextCommand::Text(current_text)); + } + } - let width = self.width(word); - if self.would_overflow(width) { - let vertical_move = - self.ctx.style.font_size - * self.ctx.style.line_spacing - * font.metrics.ascender; - self.text_commands.push(TextCommand::Move(Size::zero(), vertical_move)); + /// Whether the current line plus the extra `width` would overflow the line. + fn would_overflow(&self, width: Size) -> bool { + let max_width = self.ctx.style.width + - self.ctx.style.margin_left - self.ctx.style.margin_right; + self.current_line_width + width > max_width + } - self.current_line.clear(); - self.current_width = Size::zero(); + /// The width of a word when printed out. + fn width(&self, word: &str) -> TypeResult { + let mut width = Size::zero(); + for c in word.chars() { + width += self.char_width(c)?; } + Ok(width) + } - self.text_commands.push(TextCommand::Text(word.to_owned())); - self.current_line.push_str(word); - self.current_width += width; + /// The width of a char when printed out. + fn char_width(&self, character: char) -> TypeResult { + let font = self.get_font_for(character)?.1; + Ok(font.widths[font.map(character) as usize] * self.ctx.style.font_size) } - fn write_space(&mut self) { - let space_width = self.width(" "); + /// Load a font that has the character we need. + fn get_font_for(&self, character: char) -> TypeResult<(usize, Ref)> { + let res = self.font_loader.get(FontQuery { + families: &self.ctx.style.font_families, + italic: false, + bold: false, + character, + }).ok_or_else(|| TypesetError::MissingFont)?; + Ok(res) + } +} + +/// Serves matching fonts given a query. +struct FontLoader<'t> { + /// The context containing the used font providers. + context: &'t Context<'t>, + /// All available fonts indexed by provider. + availables: Vec<&'t [FontInfo]>, + /// Allows to lookup fonts by their infos. + indices: RefCell>, + /// Allows to retrieve cached results for queries. + matches: RefCell, usize>>, + /// All loaded fonts. + loaded: RefCell>, + /// Indexed by outside and indices maps to internal indices. + external: RefCell>, +} - if !self.would_overflow(space_width) && !self.current_line.is_empty() { - self.text_commands.push(TextCommand::Text(" ".to_owned())); - self.current_line.push_str(" "); - self.current_width += space_width; +impl<'t> FontLoader<'t> { + /// Create a new font loader. + pub fn new(context: &'t Context<'t>) -> FontLoader { + let availables = context.font_providers.iter() + .map(|prov| prov.available()).collect(); + + FontLoader { + context, + availables, + indices: RefCell::new(HashMap::new()), + matches: RefCell::new(HashMap::new()), + loaded: RefCell::new(vec![]), + external: RefCell::new(vec![]), } } - fn width(&self, word: &str) -> Size { - let font = &self.fonts[self.active_font]; - word.chars() - .map(|c| font.widths[font.map(c) as usize] * self.ctx.style.font_size) - .sum() + /// Return the list of fonts. + pub fn into_fonts(self) -> Vec { + // FIXME: Don't clone here. + let fonts = self.loaded.into_inner(); + self.external.into_inner().into_iter().map(|index| fonts[index].clone()).collect() } - fn would_overflow(&self, width: Size) -> bool { - let max_width = self.ctx.style.width - - self.ctx.style.margin_left - - self.ctx.style.margin_right; + /// Return the best matching font and it's index (if there is any) given the query. + pub fn get(&self, query: FontQuery<'t>) -> Option<(usize, Ref)> { + if let Some(index) = self.matches.borrow().get(&query) { + let external = self.external.borrow().iter().position(|i| i == index).unwrap(); + return Some((external, self.get_at_internal(*index))); + } - self.current_width + width > max_width + // Go through all available fonts and try to find one. + for family in query.families { + for (p, available) in self.availables.iter().enumerate() { + for info in available.iter() { + if Self::matches(query, &family, info) { + if let Some((index, font)) = self.try_load(info, p) { + if font.mapping.contains_key(&query.character) { + self.matches.borrow_mut().insert(query, index); + + let pos = self.external.borrow().iter().position(|&i| i == index); + let external = pos.unwrap_or_else(|| { + let external = self.external.borrow().len(); + self.external.borrow_mut().push(index); + external + }); + + return Some((external, font)); + } + } + } + } + } + } + + None + } + + /// Return a loaded font at an index. Panics if the index is out of bounds. + pub fn get_at(&self, index: usize) -> Ref { + let internal = self.external.borrow()[index]; + self.get_at_internal(internal) } + + /// Try to load the font with the given info from the provider. + fn try_load(&self, info: &FontInfo, provider: usize) -> Option<(usize, Ref)> { + if let Some(index) = self.indices.borrow().get(info) { + return Some((*index, self.get_at_internal(*index))); + } + + if let Some(mut source) = self.context.font_providers[provider].get(info) { + let mut program = Vec::new(); + source.read_to_end(&mut program).ok()?; + + let font = Font::new(program).ok()?; + + let index = self.loaded.borrow().len(); + println!("loading at interal index: {}", index); + self.loaded.borrow_mut().push(font); + self.indices.borrow_mut().insert(info.clone(), index); + + Some((index, self.get_at_internal(index))) + } else { + None + } + } + + /// Return a loaded font at an internal index. Panics if the index is out of bounds. + fn get_at_internal(&self, index: usize) -> Ref { + Ref::map(self.loaded.borrow(), |loaded| &loaded[index]) + } + + /// Check whether the query and the current family match the info. + fn matches(query: FontQuery, family: &FontFamily, info: &FontInfo) -> bool { + info.families.contains(family) + && info.italic == query.italic && info.bold == query.bold + } +} + +/// A query for a font with specific properties. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +struct FontQuery<'a> { + /// A fallback list of font families to accept. The first family in this list, that also + /// satisfies the other conditions, shall be returned. + families: &'a [FontFamily], + /// Whether the font shall be in italics. + italic: bool, + /// Whether the font shall be in boldface. + bold: bool, + /// Which character we need. + character: char, } /// Default styles for typesetting. -- cgit v1.2.3