diff --git a/Cargo.toml b/Cargo.toml index c760b3452..043175861 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,10 @@ path = "term" [dependencies.termwiz] path = "termwiz" +[target.'cfg(windows)'.dependencies] +font-loader = "0.8" +rusttype = "0.7" + [target.'cfg(any(target_os = "android", all(unix, not(target_os = "macos"))))'.dependencies] freetype = "~0.4" servo-fontconfig = "~0.4" diff --git a/src/font/mod.rs b/src/font/mod.rs index 3e86a115f..4a97e1b86 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -25,9 +25,9 @@ pub mod coretext; use self::coretext::FontSystemImpl; #[cfg(windows)] -pub mod windows; +pub mod rtype; #[cfg(windows)] -use self::windows::FontSystemImpl; +use self::rtype::FontSystemImpl; use super::config::{Config, TextStyle}; use term::CellAttributes; diff --git a/src/font/rtype.rs b/src/font/rtype.rs new file mode 100644 index 000000000..34d4a2f98 --- /dev/null +++ b/src/font/rtype.rs @@ -0,0 +1,87 @@ +//! Systems using rust native rasterizer + +use super::hbwrap as harfbuzz; +use config::{Config, TextStyle}; +use failure::{self, Error}; +use font::{ + shape_with_harfbuzz, FallbackIdx, Font, FontMetrics, FontSystem, GlyphInfo, NamedFont, + RasterizedGlyph, +}; +use font_loader::system_fonts; +use rusttype::{Font as RTFont, FontCollection, Scale}; + +struct NamedFontImpl<'a> { + collection: FontCollection<'a>, + font: RTFont<'a>, + scale: Scale, +} + +pub type FontSystemImpl = RustTypeFonts; +pub struct RustTypeFonts {} +impl RustTypeFonts { + pub fn new() -> Self { + Self {} + } +} + +impl FontSystem for RustTypeFonts { + fn load_font(&self, config: &Config, style: &TextStyle) -> Result, Error> { + let font_props = system_fonts::FontPropertyBuilder::new() + .family(&style.fontconfig_pattern) + .monospace() + .build(); + let (data, idx) = system_fonts::get(&font_props) + .ok_or_else(|| format_err!("no font matching {:?}", style))?; + let collection = FontCollection::from_bytes(data)?; + let font = collection.font_at(idx as usize)?; + eprintln!("made a font for {:?}", style); + let scale = Scale::uniform(config.font_size as f32 * 96.0 / 72.0); + Ok(Box::new(NamedFontImpl { + collection, + font, + scale, + })) + } +} + +impl<'a> NamedFont for NamedFontImpl<'a> { + fn get_fallback(&mut self, idx: FallbackIdx) -> Result<&Font, Error> { + ensure!(idx == 0, "no fallback fonts available"); + Ok(self) + } + fn shape(&mut self, s: &str) -> Result, Error> { + shape_with_harfbuzz(self, 0, s) + } +} + +impl<'a> Font for NamedFontImpl<'a> { + fn harfbuzz_shape( + &self, + buf: &mut harfbuzz::Buffer, + features: Option<&[harfbuzz::hb_feature_t]>, + ) { + unimplemented!(); + } + + fn has_color(&self) -> bool { + false + } + + fn metrics(&self) -> FontMetrics { + let vmetrics = self.font.v_metrics(self.scale); + let hmetrics = self + .font + .glyph(rusttype::Codepoint(33)) + .scaled(self.scale) + .h_metrics(); + FontMetrics { + cell_height: f64::from(vmetrics.ascent - vmetrics.descent + vmetrics.line_gap), + cell_width: hmetrics.advance_width.into(), + descender: vmetrics.descent as i16, + } + } + + fn rasterize_glyph(&self, glyph_pos: u32) -> Result { + unimplemented!(); + } +} diff --git a/src/font/windows.rs b/src/font/windows.rs deleted file mode 100644 index 8a18f19b2..000000000 --- a/src/font/windows.rs +++ /dev/null @@ -1,20 +0,0 @@ -use config::{Config, TextStyle}; -use failure::{self, Error}; -use font::{ - shape_with_harfbuzz, FallbackIdx, Font, FontMetrics, FontSystem, GlyphInfo, NamedFont, - RasterizedGlyph, -}; - -pub type FontSystemImpl = WindowsFonts; -pub struct WindowsFonts {} -impl WindowsFonts { - pub fn new() -> Self { - Self {} - } -} - -impl FontSystem for WindowsFonts { - fn load_font(&self, config: &Config, style: &TextStyle) -> Result, Error> { - unimplemented!(); - } -} diff --git a/src/main.rs b/src/main.rs index cb7c9c664..6394eef55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,12 @@ extern crate term; extern crate termwiz; extern crate toml; extern crate unicode_width; + +#[cfg(windows)] +extern crate font_loader; +#[cfg(windows)] +extern crate rusttype; + #[macro_use] pub mod log;