Skip to main content

GlyphBitmap

Struct GlyphBitmap 

Source
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

Source

pub const fn empty() -> Self

An empty bitmap: no pixels, no ink, at the origin.

Source

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.

Source

pub const fn width(&self) -> u32

Width in pixels.

Source

pub const fn height(&self) -> u32

Height in pixels.

Source

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.

Source

pub const fn top(&self) -> i32

Rows from the baseline up to the bitmap’s top edge.

Source

pub fn coverage(&self) -> &[u8] ⓘ

The coverage bytes, row-major from the top-left.

Source

pub fn into_coverage(self) -> Vec<u8> ⓘ

Consume the bitmap and take its coverage bytes.

Source

pub fn is_empty(&self) -> bool

True when there are no pixels at all — a blank glyph, a zero-size raster, or a space.

Source

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.

Source

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);
Source

pub fn row(&self, y: u32) -> &[u8] ⓘ

One row of coverage, or an empty slice when the row is off the bitmap.

Source

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);
Source

pub const fn translated(self, dx: i32, dy: i32) -> Self

Translate the bitmap without touching its pixels.

Trait Implementations§

Source§

impl Clone for GlyphBitmap

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GlyphBitmap

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for GlyphBitmap

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for GlyphBitmap

Source§

impl PartialEq for GlyphBitmap

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for GlyphBitmap

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.