Unifont

Struct Unifont 

Source
pub struct Unifont { /* private fields */ }
Expand description

A data structure for caching Unifont character bitmaps. Decompresses the compressed font data in the executable on demand, and caches it in blocks (“pages”) of 256 code points each.

Implementations§

Source§

impl Unifont

Source

pub fn load_bitmap(&mut self, codepoint: u32) -> Bitmap<'_>

Loads the Unifont bitmap corresponding to the given Unicode codepoint (if necessary), and returns it.

Will return the bitmap for U+FFFD REPLACEMENT CHAR (�) if Unifont does not include a glyph for this bitmap.

PANICS if you pass a codepoint larger than MAX_UNICODE_CODEPOINT.

Examples found in repository?
examples/banner.rs (line 8)
6fn banner_print(unifont: &mut Unifont, ink: char, wat: &str) {
7    for c in wat.chars() {
8	let bitmap = unifont.load_bitmap(c as u32);
9	let pitch = if bitmap.is_wide() { 2 } else { 1 };
10	for x in 0..bitmap.get_dimensions().0 {
11	    for _ in 0 .. 2 {
12		for y in (0..16).rev() {
13		    for _ in 0 .. 2 {
14			let bi = (x/8) + y*pitch;
15			let shift = x%8;
16			let b = bitmap.get_bytes()[bi];
17			if (128 >> shift) & b == 0 {
18			    print!(" ");
19			}
20			else {
21			    print!("{}", ink);
22			}
23		    }
24		}
25		println!("");
26	    }
27	}
28    }
29}
Source

pub fn get_bitmap(&self, codepoint: u32) -> Option<Bitmap<'_>>

Gets the Unifont bitmap corresponding to the given Unicode codepoint, if and only if it is already loaded.

Will return the bitmap for U+FFFD REPLACEMENT CHAR (�) if Unifont does not include a glyph for this bitmap, iff the respective page of the font is already loaded.

PANICS if you pass a codepoint larger than MAX_UNICODE_CODEPOINT.

Source

pub fn load_page(&mut self, page: u32)

Loads a given page, if it’s not loaded already. (Since loading is usually done transparently, this isn’t usually needed.)

Source

pub fn open() -> Unifont

Creates a new instance of this class, with no glyphs cached yet.

The font data is embedded in your executable, and does not need to be provided any other way.

Examples found in repository?
examples/banner.rs (line 56)
31fn main() {
32    let args: Vec<String> = std::env::args().collect();
33    if args.len() < 2 {
34	eprintln!("At least one argument must be specified.\n\
35		   \n\
36		   Usage: banner [--blocks] [--] [text to output...]\n\
37		   \n\
38		   If no text is given as arguments, will print text from \
39		   standard input.\n\
40		   \n\
41		   If you want to just do a banner from standard input, \
42		   without using --blocks,\ndo: \"banner --\"\n\
43		   \n\
44		   --blocks: Use a U+2588 FULL BLOCK as \"ink\" instead of \
45		   #. May break some\nterminals.\n\
46		   \n\
47		   Please note that this example makes no attempt to account \
48		   for combining\ncharacters or invisibles!");
49	std::process::exit(1);
50    }
51    let mut args = &args[1..];
52    let ink = if args.get(0).map(String::as_str) == Some("--blocks") {
53	args = &args[1..];
54	'\u{2588}'
55    } else { '#' };
56    let mut unifont = Unifont::open();
57    if args.get(0).map(String::as_str) == Some("--") {
58	args = &args[1..];
59    }
60    if args.len() == 0 {
61	// read lines and print those as banner
62	let stdin = stdin();
63	let mut lines = stdin.lock().lines();
64	let mut first = true;
65	while let Some(line) = lines.next() {
66	    let line = line.unwrap();
67	    if !first {
68		banner_print(&mut unifont, ink, " ");
69	    } else { first = true }
70	    banner_print(&mut unifont, ink, &line);
71	}
72    }
73    else {
74	// print args as banner, separated by space
75	let mut first = true;
76	for arg in args {
77	    if !first {
78		banner_print(&mut unifont, ink, " ");
79	    } else { first = true }
80	    banner_print(&mut unifont, ink, arg);
81	}
82    }
83}

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.