diff --git a/Cargo.toml b/Cargo.toml index cf61b8ce0..ee344428b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ directories = "~1.0" font-loader = "0.8" rusttype = "0.7" clipboard = "0.5" +unicode-normalization = "~0.1" [target.'cfg(unix)'.dependencies] harfbuzz-sys = "~0.2" diff --git a/src/config.rs b/src/config.rs index 8a61068b7..2195d864e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -179,6 +179,15 @@ impl TextStyle { .collect(), } } + + fn add_fallback(&mut self) { + #[cfg(target_os = "macos")] + self.font.push(FontAttributes { + family: "Apple Symbols".into(), + bold: None, + italic: None, + }); + } } /// Defines a rule that can be used to select a `TextStyle` given @@ -293,6 +302,8 @@ impl Config { }); } + cfg.font.add_fallback(); + cfg } } diff --git a/src/font/rtype.rs b/src/font/rtype.rs index 1b52a3ecd..e9019aeb2 100644 --- a/src/font/rtype.rs +++ b/src/font/rtype.rs @@ -7,10 +7,16 @@ use failure::Error; use font::{FallbackIdx, Font, FontMetrics, FontSystem, GlyphInfo, NamedFont, RasterizedGlyph}; use font_loader::system_fonts; use rusttype::{ - point, Codepoint, Font as RTFont, FontCollection, PositionedGlyph, Rect, Scale, VMetrics, + point, Codepoint, Font as RTFont, FontCollection, PositionedGlyph, Rect, Scale, ScaledGlyph, + VMetrics, }; +use unicode_normalization::UnicodeNormalization; struct NamedFontImpl<'a> { + fonts: Vec>, +} + +struct FontImpl<'a> { _collection: FontCollection<'a>, font: RTFont<'a>, scale: Scale, @@ -28,47 +34,50 @@ impl RustTypeFonts { impl FontSystem for RustTypeFonts { fn load_font(&self, config: &Config, style: &TextStyle) -> Result, Error> { - let font = &style.font[0]; - let mut font_props = system_fonts::FontPropertyBuilder::new() - .family(&font.family) - .monospace(); - font_props = if *font.bold.as_ref().unwrap_or(&false) { - font_props.bold() - } else { - font_props - }; - font_props = if *font.italic.as_ref().unwrap_or(&false) { - font_props.italic() - } else { - font_props - }; - let font_props = font_props.build(); + let mut fonts = Vec::new(); + for font_attr in &style.font { + let mut font_props = system_fonts::FontPropertyBuilder::new() + .family(&font_attr.family) + .monospace(); + font_props = if *font_attr.bold.as_ref().unwrap_or(&false) { + font_props.bold() + } else { + font_props + }; + font_props = if *font_attr.italic.as_ref().unwrap_or(&false) { + font_props.italic() + } else { + font_props + }; + let font_props = font_props.build(); - let (data, idx) = system_fonts::get(&font_props) - .ok_or_else(|| format_err!("no font matching {:?} {:?}", font_props, font))?; - eprintln!("want idx {} in bytes of len {}", idx, data.len()); - let collection = FontCollection::from_bytes(data)?; - // Most likely problem is that we matched an OpenType font and rusttype can't - // load it today. - let font = collection.font_at(idx as usize).map_err(|e| { - format_err!( - "{:?}: {} (Note that rusttype only supports TrueType font files!)", + let (data, idx) = system_fonts::get(&font_props) + .ok_or_else(|| format_err!("no font matching {:?} {:?}", font_props, font_attr))?; + eprintln!("want idx {} in bytes of len {}", idx, data.len()); + let collection = FontCollection::from_bytes(data)?; + // Most likely problem is that we matched an OpenType font and rusttype can't + // load it today. + let font = collection.font_at(idx as usize).map_err(|e| { + format_err!( + "{:?}: {} (Note that rusttype only supports TrueType font files!)", + font_attr, + e + ) + })?; + eprintln!("made a font for {:?}", font_attr); + let scale = Scale::uniform(config.font_size as f32 * config.dpi as f32 / 72.0); + let vmetrics = font.v_metrics(scale); + eprintln!("vmetrics {:?}", vmetrics); + let cell_height = f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap); + fonts.push(FontImpl { + _collection: collection, + cell_height, font, - e - ) - })?; - eprintln!("made a font for {:?}", style); - let scale = Scale::uniform(config.font_size as f32 * config.dpi as f32 / 72.0); - let vmetrics = font.v_metrics(scale); - eprintln!("vmetrics {:?}", vmetrics); - let cell_height = f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap); - Ok(Box::new(NamedFontImpl { - _collection: collection, - cell_height, - font, - scale, - vmetrics, - })) + scale, + vmetrics, + }); + } + Ok(Box::new(NamedFontImpl { fonts })) } } @@ -82,25 +91,51 @@ fn bounds(g: &PositionedGlyph) -> Rect { } } +impl<'a> NamedFontImpl<'a> { + fn glyph(&mut self, c: char) -> Result<(ScaledGlyph, usize), Error> { + let codepoint = Codepoint(c as u32); + for (idx, font) in self.fonts.iter().enumerate() { + let g = font.font.glyph(codepoint); + if g.id().0 == 0 { + // notdef; continue looking in the fallbacks + continue; + } + return Ok((g.scaled(font.scale), idx)); + } + if c != '?' { + return match self.glyph('?') { + Ok((g, idx)) => Ok((g, idx)), + Err(_) => bail!("unable to resolve glyph for char={} or the fallback ?", c), + }; + } + bail!("unable to resolve glyph for ?"); + } +} + impl<'a> NamedFont for NamedFontImpl<'a> { fn get_fallback(&mut self, idx: FallbackIdx) -> Result<&Font, Error> { - ensure!(idx == 0, "no fallback fonts available (idx={})", idx); - Ok(self) + self.fonts + .get(idx) + .map(|f| { + let f: &Font = f; + f + }) + .ok_or_else(|| format_err!("no fallback fonts available (idx={})", idx)) } fn shape(&mut self, s: &str) -> Result, Error> { let mut shaped = Vec::new(); - for (cluster, c) in s.chars().enumerate() { - let glyph = self.font.glyph(Codepoint(c as u32)).scaled(self.scale); + for (cluster, c) in s.nfc().enumerate() { + let (glyph, font_idx) = self.glyph(c)?; let hmetrics = glyph.h_metrics(); let glyph = glyph.positioned(point(0.0, 0.0)); shaped.push(GlyphInfo { #[cfg(debug_assertions)] - text: s[cluster..cluster].to_string(), + text: c.to_string(), cluster: cluster as u32, num_cells: 1, - font_idx: 0, + font_idx, glyph_pos: glyph.id().0, x_advance: hmetrics.advance_width.into(), x_offset: (-hmetrics.left_side_bearing).into(), @@ -113,7 +148,7 @@ impl<'a> NamedFont for NamedFontImpl<'a> { } } -impl<'a> Font for NamedFontImpl<'a> { +impl<'a> Font for FontImpl<'a> { #[cfg(unix)] fn harfbuzz_shape( &self, diff --git a/src/main.rs b/src/main.rs index 04aad7800..00157b33e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ extern crate clipboard; extern crate term; extern crate termwiz; extern crate toml; +extern crate unicode_normalization; extern crate unicode_width; #[cfg(any(windows, feature = "force-rusttype"))]