Skip to content

Commit a470b69

Browse files
committed
Added dx parameter for sub-pixel rasterization
Added dx parameter which allows for controlling the horizontal offset within the pixel. This allows drawing glyphs at the exact floating point x position provided by accumulating the horizontal advance_width metric when drawing text.
1 parent 0eb26ca commit a470b69

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/font.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl Font {
480480
offset_y += 1.0;
481481
}
482482
let metrics = Metrics {
483-
xmin: as_i32(floor(bounds.xmin)),
483+
xmin: as_i32(floor(bounds.xmin + offset)),
484484
ymin: as_i32(floor(bounds.ymin)),
485485
width: as_i32(ceil(bounds.width + offset_x)) as usize,
486486
height: as_i32(ceil(bounds.height + offset_y)) as usize,
@@ -505,7 +505,7 @@ impl Font {
505505
/// the top left corner of the glyph.
506506
#[inline]
507507
pub fn rasterize_config(&self, config: GlyphRasterConfig) -> (Metrics, Vec<u8>) {
508-
self.rasterize_indexed(config.glyph_index, config.px)
508+
self.rasterize_indexed(config.glyph_index, config.px, 0.0)
509509
}
510510

511511
/// Retrieves the layout metrics and rasterized bitmap for the given character. If the
@@ -516,15 +516,16 @@ impl Font {
516516
/// * `character` - The character to rasterize.
517517
/// * `px` - The size to render the character at. Cannot be negative. The units of the scale
518518
/// are pixels per Em unit.
519+
/// * `dx` - The sub-pixel x offset in which to render the glyph.
519520
/// # Returns
520521
///
521522
/// * `Metrics` - Sizing and positioning metadata for the rasterized glyph.
522523
/// * `Vec<u8>` - Coverage vector for the glyph. Coverage is a linear scale where 0 represents
523524
/// 0% coverage of that pixel by the glyph and 255 represents 100% coverage. The vec starts at
524525
/// the top left corner of the glyph.
525526
#[inline]
526-
pub fn rasterize(&self, character: char, px: f32) -> (Metrics, Vec<u8>) {
527-
self.rasterize_indexed(self.lookup_glyph_index(character), px)
527+
pub fn rasterize(&self, character: char, px: f32, dx: f32) -> (Metrics, Vec<u8>) {
528+
self.rasterize_indexed(self.lookup_glyph_index(character), px, dx)
528529
}
529530

530531
/// Retrieves the layout rasterized bitmap for the given raster config. If the raster config's
@@ -576,19 +577,21 @@ impl Font {
576577
/// * `index` - The glyph index in the font to rasterize.
577578
/// * `px` - The size to render the character at. Cannot be negative. The units of the scale
578579
/// are pixels per Em unit.
580+
/// * `dx` - The sub-pixel x offset in which to render the glyph.
581+
/// # Returns
579582
/// # Returns
580583
///
581584
/// * `Metrics` - Sizing and positioning metadata for the rasterized glyph.
582585
/// * `Vec<u8>` - Coverage vector for the glyph. Coverage is a linear scale where 0 represents
583586
/// 0% coverage of that pixel by the glyph and 255 represents 100% coverage. The vec starts at
584587
/// the top left corner of the glyph.
585-
pub fn rasterize_indexed(&self, index: u16, px: f32) -> (Metrics, Vec<u8>) {
588+
pub fn rasterize_indexed(&self, index: u16, px: f32, dx: f32) -> (Metrics, Vec<u8>) {
586589
if px <= 0.0 {
587590
return (Metrics::default(), Vec::new());
588591
}
589592
let glyph = &self.glyphs[index as usize];
590593
let scale = self.scale_factor(px);
591-
let (metrics, offset_x, offset_y) = self.metrics_raw(scale, glyph, 0.0);
594+
let (metrics, offset_x, offset_y) = self.metrics_raw(scale, glyph, dx);
592595
let mut canvas = Raster::new(metrics.width, metrics.height);
593596
canvas.draw(&glyph, scale, scale, offset_x, offset_y);
594597
(metrics, canvas.get_bitmap())

0 commit comments

Comments
 (0)