From 0bc262ab9aeb05a0950118d584369c80c7182239 Mon Sep 17 00:00:00 2001 From: Daniel <101683475+Koranir@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:33:21 +1100 Subject: [PATCH] Expose vertical offset as well --- src/font.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/font.rs b/src/font.rs index 5be0af4e..fd0428b2 100644 --- a/src/font.rs +++ b/src/font.rs @@ -458,15 +458,15 @@ impl Font { pub fn metrics_indexed(&self, index: u16, px: f32) -> Metrics { let glyph = &self.glyphs[index as usize]; let scale = self.scale_factor(px); - let (metrics, _, _) = self.metrics_raw(scale, glyph, 0.0); + let (metrics, _, _) = self.metrics_raw(scale, glyph, (0.0, 0.0)); metrics } /// Internal function to generate the metrics, offset_x, and offset_y of the glyph. - fn metrics_raw(&self, scale: f32, glyph: &Glyph, offset: f32) -> (Metrics, f32, f32) { + fn metrics_raw(&self, scale: f32, glyph: &Glyph, offset: (f32, f32)) -> (Metrics, f32, f32) { let bounds = glyph.bounds.scale(scale); - let mut offset_x = fract(bounds.xmin + offset); - let mut offset_y = fract(1.0 - fract(bounds.height) - fract(bounds.ymin)); + let mut offset_x = fract(bounds.xmin + offset.0); + let mut offset_y = fract(1.0 - fract(bounds.height) - fract(bounds.ymin + offset.1)); if is_negative(offset_x) { offset_x += 1.0; } @@ -577,7 +577,7 @@ impl Font { /// 0% coverage of that pixel by the glyph and 255 represents 100% coverage. The vec starts at /// the top left corner of the glyph. pub fn rasterize_indexed(&self, index: u16, px: f32) -> (Metrics, Vec) { - self.rasterize_indexed_offset(index, px, 0.0) + self.rasterize_indexed_offset(index, px, (0.0, 0.0)) } /// Retrieves the layout metrics and rasterized bitmap at the given index. You normally want to @@ -597,7 +597,7 @@ impl Font { /// represents 0% coverage of that subpixel by the glyph and 255 represents 100% coverage. The /// vec starts at the top left corner of the glyph. pub fn rasterize_indexed_subpixel(&self, index: u16, px: f32) -> (Metrics, Vec) { - self.rasterize_indexed_subpixel_offset(index, px, 0.0) + self.rasterize_indexed_subpixel_offset(index, px, (0.0, 0.0)) } /// Retrieves the layout metrics and rasterized bitmap at the given index. You normally want to @@ -607,14 +607,15 @@ impl Font { /// * `index` - The glyph index in the font to rasterize. /// * `px` - The size to render the character at. Cannot be negative. The units of the scale /// are pixels per Em unit. - /// * `offset` - Additional horizontal offset to apply while rendering for more control over hinting. + /// * `offset` - Additional horizontal and vertical (in that order) offset to apply while + /// rendering for more control over hinting. /// # Returns /// /// * `Metrics` - Sizing and positioning metadata for the rasterized glyph. /// * `Vec` - Coverage vector for the glyph. Coverage is a linear scale where 0 represents /// 0% coverage of that pixel by the glyph and 255 represents 100% coverage. The vec starts at /// the top left corner of the glyph. - pub fn rasterize_indexed_offset(&self, index: u16, px: f32, offset: f32) -> (Metrics, Vec) { + pub fn rasterize_indexed_offset(&self, index: u16, px: f32, offset: (f32, f32)) -> (Metrics, Vec) { if px <= 0.0 { return (Metrics::default(), Vec::new()); } @@ -636,20 +637,26 @@ impl Font { /// * `index` - The glyph index in the font to rasterize. /// * `px` - The size to render the character at. Cannot be negative. The units of the scale /// are pixels per Em unit. - /// * `offset` - Additional horizontal offset to apply while rendering for more control over hinting. + /// * `offset` - Additional horizontal and vertical (in that order) offset to apply while + /// rendering for more control over hinting. /// # Returns /// /// * `Metrics` - Sizing and positioning metadata for the rasterized glyph. /// * `Vec` - Swizzled RGB coverage vector for the glyph. Coverage is a linear scale where 0 /// represents 0% coverage of that subpixel by the glyph and 255 represents 100% coverage. The /// vec starts at the top left corner of the glyph. - pub fn rasterize_indexed_subpixel_offset(&self, index: u16, px: f32, offset: f32) -> (Metrics, Vec) { + pub fn rasterize_indexed_subpixel_offset( + &self, + index: u16, + px: f32, + offset: (f32, f32), + ) -> (Metrics, Vec) { if px <= 0.0 { return (Metrics::default(), Vec::new()); } let glyph = &self.glyphs[index as usize]; let scale = self.scale_factor(px); - let (metrics, offset_x, offset_y) = self.metrics_raw(scale, glyph, offset * 3.0); + let (metrics, offset_x, offset_y) = self.metrics_raw(scale, glyph, (offset.0 * 3.0, offset.1)); let mut canvas = Raster::new(metrics.width * 3, metrics.height); canvas.draw(&glyph, scale * 3.0, scale, offset_x, offset_y); (metrics, canvas.get_bitmap())