pub struct Pcf<'a> {
pub glyph_byte_length: usize,
pub glyph_width: usize,
pub glyph_height: usize,
pub glyphs: &'a [u8],
pub unicode_table: Option<&'a [u8]>,
}
Expand description
A PCF font
Use parse to create one, get_glyph_pixels to get a glyph by index, and iter_unicode_entries to get a glyph index by unicode character. PCF stores ASCII glyphs at the corresponding index, so you can retrieve ASCII characters without calling iter_unicode_entries by giving the ASCII number to get_glyph_pixels directly.
Fields§
§glyph_byte_length: usize
§glyph_width: usize
§glyph_height: usize
§glyphs: &'a [u8]
§unicode_table: Option<&'a [u8]>
Implementations§
Source§impl<'a> Pcf<'a>
impl<'a> Pcf<'a>
Sourcepub fn parse(data: &[u8]) -> Result<Pcf<'_>, ParseError>
pub fn parse(data: &[u8]) -> Result<Pcf<'_>, ParseError>
Parse a PCF font from some bytes
See ParseError for possible errors.
PCF fonts may optionally have a unicode table indicating which glyphs correspond to which unicode characters. This function does not validate the structure of the unicode table. Errors in the encoding of the unicode table, if they can be detected, are reported by the iterator returned from the iter_unicode_entries function.
Sourcepub fn get_glyph_bits(&self, glyph_index: usize) -> Option<&[u8]>
pub fn get_glyph_bits(&self, glyph_index: usize) -> Option<&[u8]>
Get the bits corresponding to the glyph’s bitmap
PCF stores the bitmap as packed bits. Each byte in the slice contains eight pixels, with the last byte of each row containing padding bits so that the next row starts on a byte boundary.
You might be looking for get_glyph_pixels instead, which unpacks the bits for you.
Sourcepub fn get_glyph_pixels<'b>(
&'b self,
glyph_index: usize,
) -> Option<impl Iterator<Item = bool> + 'b>
pub fn get_glyph_pixels<'b>( &'b self, glyph_index: usize, ) -> Option<impl Iterator<Item = bool> + 'b>
Get the pixels that correspond to the given glyph’s bitmap
PCF stores bitmaps in the packed bits of each byte. This iterator unpacks the bits, returning a boolean for each pixel in the bitmap indicating whether that pixel is lit or not.
PCF stores ASCII glyphs in the corresponding index, so you can retrieve ASCII glyphs directly
by calling get_glyph_pixels(b'a' as usize)
. Unicode characters must be looked up with
(iter_unicode_entries)Pcf::iter_unicode_entries.
Sourcepub fn iter_unicode_entries<'b>(
&'b self,
) -> Option<impl Iterator<Item = (usize, Result<&'b str, Utf8Error>)> + 'b>
pub fn iter_unicode_entries<'b>( &'b self, ) -> Option<impl Iterator<Item = (usize, Result<&'b str, Utf8Error>)> + 'b>
Iterate over the entries in the unicode table
PCF fonts may optionally include a unicode table indicating which unicode character each glyph corresponds to. If such a table exists, this function returns an iterator that will yield each unicode entry along with the index of the glyph that represents to it.
In the case of a malformed unicode table, the behavior of this iterator is unpredictable because errors can not always be unambiguously detected. The iterator may return a unicode entry with a string containing multiple graphemes that ought to each have their own glyph, it may return None, or it may return a core::str::Utf8Error.
If you are using the standard library, this iterator can be used to construct a HashMap lookup table of unicode character to glyph index pairs.
let glyph_lookup_table = pcf.iter_unicode_entries().unwrap()
.filter_map(|(index, result)| result.ok().map(|character| (character, index)))
.collect::<std::collections::HashMap<&str, usize>>();