diff --git a/src/font/rasterizer/freetype.rs b/src/font/rasterizer/freetype.rs index 78aead044..74d004ad7 100644 --- a/src/font/rasterizer/freetype.rs +++ b/src/font/rasterizer/freetype.rs @@ -8,15 +8,13 @@ use std::slice; pub struct FreeTypeRasterizer { face: RefCell, - /// nominal monospace cell height - cell_height: f64, - /// nominal monospace cell width - cell_width: f64, has_color: bool, } impl FontRasterizer for FreeTypeRasterizer { - fn rasterize_glyph(&self, glyph_pos: u32) -> Fallible { + fn rasterize_glyph(&self, glyph_pos: u32, size: f64, dpi: u32) -> Fallible { + self.face.borrow_mut().set_font_size(size, dpi)?; + let render_mode = //ftwrap::FT_Render_Mode::FT_RENDER_MODE_NORMAL; // ftwrap::FT_Render_Mode::FT_RENDER_MODE_LCD; ftwrap::FT_Render_Mode::FT_RENDER_MODE_LIGHT; @@ -271,46 +269,7 @@ impl FreeTypeRasterizer { } pub fn with_face_size_and_dpi(mut face: ftwrap::Face, size: f64, dpi: u32) -> Fallible { - log::debug!("set_char_size {} dpi={}", size, dpi); - // Scaling before truncating to integer minimizes the chances of hitting - // the fallback code for set_pixel_sizes below. - let size = (size * 64.0) as ftwrap::FT_F26Dot6; - - let (cell_width, cell_height) = match face.set_char_size(size, size, dpi, dpi) { - Ok(_) => { - // Compute metrics for the nominal monospace cell - face.cell_metrics() - } - Err(err) => { - let sizes = unsafe { - let rec = &(*face.face); - slice::from_raw_parts(rec.available_sizes, rec.num_fixed_sizes as usize) - }; - if sizes.is_empty() { - return Err(err); - } - // Find the best matching size. - // We just take the biggest. - let mut best = 0; - let mut best_size = 0; - let mut cell_width = 0; - let mut cell_height = 0; - - for (idx, info) in sizes.iter().enumerate() { - let size = best_size.max(info.height); - if size > best_size { - best = idx; - best_size = size; - cell_width = info.width; - cell_height = info.height; - } - } - face.select_size(best)?; - (f64::from(cell_width), f64::from(cell_height)) - } - }; - - log::debug!("metrics: width={} height={}", cell_width, cell_height); + face.set_font_size(size, dpi)?; let has_color = unsafe { (((*face.face).face_flags as u32) & (ftwrap::FT_FACE_FLAG_COLOR as u32)) != 0 @@ -318,8 +277,6 @@ impl FreeTypeRasterizer { Ok(Self { face: RefCell::new(face), - cell_height, - cell_width, has_color, }) } diff --git a/src/font/rasterizer/mod.rs b/src/font/rasterizer/mod.rs index c88349659..540a17ae0 100644 --- a/src/font/rasterizer/mod.rs +++ b/src/font/rasterizer/mod.rs @@ -6,5 +6,5 @@ pub mod freetype; /// Rasterizes the specified glyph index in the associated font /// and returns the generated bitmap pub trait FontRasterizer { - fn rasterize_glyph(&self, glyph_pos: u32) -> Fallible; + fn rasterize_glyph(&self, glyph_pos: u32, size: f64, dpi: u32) -> Fallible; } diff --git a/src/font/shaper/harfbuzz.rs b/src/font/shaper/harfbuzz.rs index 83184de7a..86a738010 100644 --- a/src/font/shaper/harfbuzz.rs +++ b/src/font/shaper/harfbuzz.rs @@ -20,6 +20,8 @@ impl HarfbuzzShaper { &self, font_idx: crate::font::system::FallbackIdx, s: &str, + font_size: f64, + dpi: u32, ) -> Fallible> { let features = vec![ // kerning @@ -39,9 +41,9 @@ impl HarfbuzzShaper { { match self.fonts.get(font_idx) { Some(pair) => { - pair.borrow_mut() - .font - .shape(&mut buf, Some(features.as_slice())); + let mut pair = pair.borrow_mut(); + pair.face.set_font_size(font_size, dpi)?; + pair.font.shape(&mut buf, Some(features.as_slice())); } None => { let chars: Vec = s.chars().map(|c| c as u32).collect(); @@ -109,14 +111,14 @@ impl HarfbuzzShaper { //debug!("range: {:?}-{:?} needs fallback", start, pos); let substr = &s[start_pos..pos]; - let mut shape = match self.do_shape(font_idx + 1, substr) { + let mut shape = match self.do_shape(font_idx + 1, substr, font_size, dpi) { Ok(shape) => Ok(shape), Err(e) => { error!("{:?} for {:?}", e, substr); if font_idx == 0 && s == "?" { bail!("unable to find any usable glyphs for `?` in font_idx 0"); } - self.do_shape(0, "?") + self.do_shape(0, "?", font_size, dpi) } }?; @@ -134,7 +136,7 @@ impl HarfbuzzShaper { //debug!("glyph from `{}`", text); cluster.push(GlyphInfo::new(text, font_idx, info, &positions[i])); } else { - cluster.append(&mut self.do_shape(0, "?")?); + cluster.append(&mut self.do_shape(0, "?", font_size, dpi)?); } } } @@ -151,14 +153,14 @@ impl HarfbuzzShaper { substr, ); } - let mut shape = match self.do_shape(font_idx + 1, substr) { + let mut shape = match self.do_shape(font_idx + 1, substr, font_size, dpi) { Ok(shape) => Ok(shape), Err(e) => { error!("{:?} for {:?}", e, substr); if font_idx == 0 && s == "?" { bail!("unable to find any usable glyphs for `?` in font_idx 0"); } - self.do_shape(0, "?") + self.do_shape(0, "?", font_size, dpi) } }?; // Fixup the cluster member to match our current offset @@ -175,7 +177,7 @@ impl HarfbuzzShaper { } impl FontShaper for HarfbuzzShaper { - fn shape(&self, text: &str) -> Fallible> { - self.do_shape(0, text) + fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible> { + self.do_shape(0, text, size, dpi) } } diff --git a/src/font/shaper/mod.rs b/src/font/shaper/mod.rs index 3f74e4bac..ca5b9077c 100644 --- a/src/font/shaper/mod.rs +++ b/src/font/shaper/mod.rs @@ -5,5 +5,5 @@ pub mod harfbuzz; pub trait FontShaper { /// Shape text and return a vector of GlyphInfo - fn shape(&self, text: &str) -> Fallible>; + fn shape(&self, text: &str, size: f64, dpi: u32) -> Fallible>; }