From 6fdb693f2cbcd6c0e527a2e682c6f13a6f7e5e7a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 9 Dec 2019 13:17:40 -0800 Subject: [PATCH] fonts: cut raster and shaper over to new stuff --- src/font/mod.rs | 33 +++++++++++++++++++++++++++++++++ src/font/rasterizer/freetype.rs | 6 +++--- src/font/shaper/harfbuzz.rs | 4 ++-- src/frontend/gui/glyphcache.rs | 25 ++++++++----------------- src/frontend/gui/termwindow.rs | 6 ++---- 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/font/mod.rs b/src/font/mod.rs index afff43701..da9d5ecd7 100644 --- a/src/font/mod.rs +++ b/src/font/mod.rs @@ -44,6 +44,31 @@ use term::CellAttributes; pub struct LoadedFont { rasterizers: Vec>, shaper: Box, + metrics: FontMetrics, + font_size: f64, + dpi: u32, +} + +impl LoadedFont { + pub fn metrics(&self) -> FontMetrics { + self.metrics + } + + pub fn shape(&self, text: &str) -> Fallible> { + self.shaper.shape(text, self.font_size, self.dpi) + } + + pub fn rasterize_glyph( + &self, + glyph_pos: u32, + fallback: FallbackIdx, + ) -> Fallible { + let rasterizer = self + .rasterizers + .get(fallback) + .ok_or_else(|| format_err!("no such fallback index: {}", fallback))?; + rasterizer.rasterize_glyph(glyph_pos, self.font_size, self.dpi) + } } type FontPtr = Rc>>; @@ -192,9 +217,17 @@ impl FontConfiguration { } let shaper = FontShaperSelection::get_default().new_shaper(&handles)?; + let config = configuration(); + let font_size = config.font_size * *self.font_scale.borrow(); + let dpi = config.dpi as u32; + let metrics = shaper.metrics(font_size, dpi)?; + let loaded = Rc::new(LoadedFont { rasterizers, shaper, + metrics, + font_size, + dpi, }); fonts.insert(style.clone(), Rc::clone(&loaded)); diff --git a/src/font/rasterizer/freetype.rs b/src/font/rasterizer/freetype.rs index 3f74aeae9..f1ead283f 100644 --- a/src/font/rasterizer/freetype.rs +++ b/src/font/rasterizer/freetype.rs @@ -8,9 +8,9 @@ use std::mem; use std::slice; pub struct FreeTypeRasterizer { - face: RefCell, - lib: ftwrap::Library, has_color: bool, + face: RefCell, + _lib: ftwrap::Library, } impl FontRasterizer for FreeTypeRasterizer { @@ -272,7 +272,7 @@ impl FreeTypeRasterizer { (((*face.face).face_flags as u32) & (ftwrap::FT_FACE_FLAG_COLOR as u32)) != 0 }; Ok(Self { - lib, + _lib: lib, face: RefCell::new(face), has_color, }) diff --git a/src/font/shaper/harfbuzz.rs b/src/font/shaper/harfbuzz.rs index 123598e53..813091cab 100644 --- a/src/font/shaper/harfbuzz.rs +++ b/src/font/shaper/harfbuzz.rs @@ -14,7 +14,7 @@ struct FontPair { pub struct HarfbuzzShaper { fonts: Vec>, - lib: ftwrap::Library, + _lib: ftwrap::Library, } impl HarfbuzzShaper { @@ -29,7 +29,7 @@ impl HarfbuzzShaper { font.set_load_flags(load_flags); fonts.push(RefCell::new(FontPair { face, font })); } - Ok(Self { fonts, lib }) + Ok(Self { fonts, _lib: lib }) } fn do_shape( diff --git a/src/frontend/gui/glyphcache.rs b/src/frontend/gui/glyphcache.rs index 33d71a049..4c76cb921 100644 --- a/src/frontend/gui/glyphcache.rs +++ b/src/frontend/gui/glyphcache.rs @@ -103,22 +103,13 @@ impl GlyphCache { fn load_glyph(&mut self, info: &GlyphInfo, style: &TextStyle) -> Fallible>> { let metrics; let glyph; - let has_color; - let (cell_width, cell_height) = { - let font = self.fonts.cached_font(style)?; - let mut font = font.borrow_mut(); - metrics = font - .get_fallback(0) - .map_err(|e| e.context(format!("glyph {:?}", info)))? - .metrics(); - let active_font = font - .get_fallback(info.font_idx) - .map_err(|e| e.context(format!("glyph {:?}", info)))?; - has_color = active_font.has_color(); - glyph = active_font.rasterize_glyph(info.glyph_pos)?; - (metrics.cell_width, metrics.cell_height) - }; + { + let font = self.fonts.resolve_font(style)?; + metrics = font.metrics(); + glyph = font.rasterize_glyph(info.glyph_pos, info.font_idx)?; + } + let (cell_width, cell_height) = (metrics.cell_width, metrics.cell_height); let scale = if (info.x_advance / f64::from(info.num_cells)).floor() > cell_width { f64::from(info.num_cells) * (cell_width / info.x_advance) @@ -130,7 +121,7 @@ impl GlyphCache { let glyph = if glyph.width == 0 || glyph.height == 0 { // a whitespace glyph CachedGlyph { - has_color, + has_color: glyph.has_color, texture: None, x_offset: info.x_offset * scale, y_offset: info.y_offset * scale, @@ -160,7 +151,7 @@ impl GlyphCache { let tex = self.atlas.allocate(&raw_im)?; CachedGlyph { - has_color, + has_color: glyph.has_color, texture: Some(tex), x_offset, y_offset, diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 93e6281d4..bc66ddb2d 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -1250,8 +1250,7 @@ impl TermWindow { // Shape the printable text from this cluster let glyph_info = { - let font = self.fonts.cached_font(style)?; - let mut font = font.borrow_mut(); + let font = self.fonts.resolve_font(style)?; font.shape(&cluster.text)? }; @@ -1478,8 +1477,7 @@ impl TermWindow { // Shape the printable text from this cluster let glyph_info = { - let font = self.fonts.cached_font(style)?; - let mut font = font.borrow_mut(); + let font = self.fonts.resolve_font(style)?; font.shape(&cluster.text)? };