pub struct GlyphBitmap { /* private fields */ }Expand description
A glyph’s coverage: 8 bits per pixel, with the placement a caller needs to put it back into a text run.
use font_shape::{rasterize_outline, GlyphBitmap};
let mut outline = font_model::Outline::new();
let mut p = font_model::Path::starting_at(0.0, 0.0);
p.line_to(1000.0, 0.0);
p.line_to(1000.0, 1000.0);
p.line_to(0.0, 1000.0);
p.close();
outline.push_contour(p);
// A 1000-upem glyph drawn 10 px tall.
let bmp = rasterize_outline(&outline, 1000, 10.0, font_shape::Hinting::None).unwrap();
assert_eq!(bmp.height(), 10);
assert_eq!(bmp.width(), 10);
assert_eq!(bmp.left(), 0);
assert_eq!(bmp.top(), 10, "the ink starts at the baseline");
assert_eq!(bmp.coverage().len(), 100);
// It is a solid 10x10 block, so the ink is the full area.
assert!((bmp.ink() - 100.0).abs() < 1e-6, "{}", bmp.ink());Implementations§
Source§impl GlyphBitmap
impl GlyphBitmap
Sourcepub const fn new(
width: u32,
height: u32,
left: i32,
top: i32,
coverage: Vec<u8>,
) -> Self
pub const fn new( width: u32, height: u32, left: i32, top: i32, coverage: Vec<u8>, ) -> Self
Build from raw parts. The mask must be exactly width × height long.
Sourcepub const fn left(&self) -> i32
pub const fn left(&self) -> i32
Columns from the pen position to the bitmap’s left edge. May be negative: a glyph can overhang to the left of its origin.
Sourcepub fn into_coverage(self) -> Vec<u8> ⓘ
pub fn into_coverage(self) -> Vec<u8> ⓘ
Consume the bitmap and take its coverage bytes.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
True when there are no pixels at all — a blank glyph, a zero-size raster, or a space.
Sourcepub fn ink(&self) -> f64
pub fn ink(&self) -> f64
The total ink, in pixels: coverage / 255 summed.
For a shape this is its area, which is the property
fill_path is built to hold.
Sourcepub fn coverage_at(&self, x: u32, y: u32) -> u8
pub fn coverage_at(&self, x: u32, y: u32) -> u8
The coverage at (x, y), or 0 outside the bitmap.
use font_shape::GlyphBitmap;
let b = GlyphBitmap::new(2, 1, 0, 0, vec![10, 200]);
assert_eq!(b.coverage_at(0, 0), 10);
assert_eq!(b.coverage_at(1, 0), 200);
assert_eq!(b.coverage_at(2, 0), 0, "out of bounds reads as empty");
assert_eq!(b.coverage_at(0, 9), 0);Sourcepub fn row(&self, y: u32) -> &[u8] ⓘ
pub fn row(&self, y: u32) -> &[u8] ⓘ
One row of coverage, or an empty slice when the row is off the bitmap.
Sourcepub fn ink_bounds(&self) -> Option<(u32, u32, u32, u32)>
pub fn ink_bounds(&self) -> Option<(u32, u32, u32, u32)>
The tight ink bounds in bitmap pixels, or None when nothing is drawn.
use font_shape::GlyphBitmap;
// A 4x3 block whose middle 2x2 is ink.
let b = GlyphBitmap::new(4, 3, 0, 0, vec![
0, 0, 0, 0,
0, 255, 255, 0,
0, 255, 255, 0,
]);
assert_eq!(b.ink_bounds(), Some((1, 1, 2, 2)));
assert_eq!(GlyphBitmap::empty().ink_bounds(), None);Sourcepub const fn translated(self, dx: i32, dy: i32) -> Self
pub const fn translated(self, dx: i32, dy: i32) -> Self
Translate the bitmap without touching its pixels.