Skip to content

Commit

Permalink
Expose vertical offset as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Koranir committed Jan 17, 2024
1 parent 10c4871 commit 0bc262a
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<u8>) {
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
Expand All @@ -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<u8>) {
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
Expand All @@ -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<u8>` - 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<u8>) {
pub fn rasterize_indexed_offset(&self, index: u16, px: f32, offset: (f32, f32)) -> (Metrics, Vec<u8>) {
if px <= 0.0 {
return (Metrics::default(), Vec::new());
}
Expand All @@ -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<u8>` - 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<u8>) {
pub fn rasterize_indexed_subpixel_offset(
&self,
index: u16,
px: f32,
offset: (f32, f32),
) -> (Metrics, Vec<u8>) {
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())
Expand Down

0 comments on commit 0bc262a

Please sign in to comment.