Skip to main content

Text

Struct Text 

Source
pub struct Text<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Text<'a>

Source

pub fn width(&self) -> u32

Return width of the text

Examples found in repository?
examples/character_map.rs (line 35)
15fn main() {
16    let (title, font_res) = match env::args().nth(1) {
17        Some(arg) => (arg.clone(), Font::from_path(&arg)),
18        None => ("Default Font".to_string(), Font::find(None, None, None)),
19    };
20
21    match font_res {
22        Ok(font) => {
23            let lines = [
24                font.render("ABCDEFGHIJK", 64.0),
25                font.render("LMNOPQRSTUV", 64.0),
26                font.render("WXYZabcdefg", 64.0),
27                font.render("hijklmnopqr", 64.0),
28                font.render("stuvwxyz.?!", 64.0),
29                font.render("0123456789 ", 64.0),
30            ];
31
32            let mut width = 0;
33            let mut height = 0;
34            for line in lines.iter() {
35                width = max(width, line.width());
36                height += line.height();
37            }
38
39            let redraw = move |window: &mut Window| {
40                window.set(Color::rgb(255, 255, 255));
41                let mut y = 0;
42                for line in lines.iter() {
43                    line.draw(window, 0, y, Color::rgb(0, 0, 0));
44                    y += line.height() as i32;
45                }
46                window.sync();
47            };
48
49            let mut window = Window::new_flags(
50                -1,
51                -1,
52                max(320, width),
53                max(32, height),
54                &format!("{} - Character Map", title),
55                &[WindowFlag::Resizable],
56            )
57            .unwrap();
58
59            redraw(&mut window);
60
61            loop {
62                for event in window.events() {
63                    match event.to_option() {
64                        EventOption::Resize(_) => redraw(&mut window),
65                        EventOption::Quit(_) => return,
66                        _ => (),
67                    }
68                }
69            }
70        }
71        Err(err) => {
72            let mut window =
73                Window::new(-1, -1, 320, 32, &format!("{} - Character Map", err)).unwrap();
74            window.set(Color::rgb(0, 0, 0));
75            window.sync();
76            loop {
77                for event in window.events() {
78                    match event.to_option() {
79                        EventOption::Quit(_) => return,
80                        _ => (),
81                    }
82                }
83            }
84        }
85    }
86}
Source

pub fn height(&self) -> u32

Return height of the text

Examples found in repository?
examples/character_map.rs (line 36)
15fn main() {
16    let (title, font_res) = match env::args().nth(1) {
17        Some(arg) => (arg.clone(), Font::from_path(&arg)),
18        None => ("Default Font".to_string(), Font::find(None, None, None)),
19    };
20
21    match font_res {
22        Ok(font) => {
23            let lines = [
24                font.render("ABCDEFGHIJK", 64.0),
25                font.render("LMNOPQRSTUV", 64.0),
26                font.render("WXYZabcdefg", 64.0),
27                font.render("hijklmnopqr", 64.0),
28                font.render("stuvwxyz.?!", 64.0),
29                font.render("0123456789 ", 64.0),
30            ];
31
32            let mut width = 0;
33            let mut height = 0;
34            for line in lines.iter() {
35                width = max(width, line.width());
36                height += line.height();
37            }
38
39            let redraw = move |window: &mut Window| {
40                window.set(Color::rgb(255, 255, 255));
41                let mut y = 0;
42                for line in lines.iter() {
43                    line.draw(window, 0, y, Color::rgb(0, 0, 0));
44                    y += line.height() as i32;
45                }
46                window.sync();
47            };
48
49            let mut window = Window::new_flags(
50                -1,
51                -1,
52                max(320, width),
53                max(32, height),
54                &format!("{} - Character Map", title),
55                &[WindowFlag::Resizable],
56            )
57            .unwrap();
58
59            redraw(&mut window);
60
61            loop {
62                for event in window.events() {
63                    match event.to_option() {
64                        EventOption::Resize(_) => redraw(&mut window),
65                        EventOption::Quit(_) => return,
66                        _ => (),
67                    }
68                }
69            }
70        }
71        Err(err) => {
72            let mut window =
73                Window::new(-1, -1, 320, 32, &format!("{} - Character Map", err)).unwrap();
74            window.set(Color::rgb(0, 0, 0));
75            window.sync();
76            loop {
77                for event in window.events() {
78                    match event.to_option() {
79                        EventOption::Quit(_) => return,
80                        _ => (),
81                    }
82                }
83            }
84        }
85    }
86}
Source

pub fn draw_clipped<R: Renderer + ?Sized>( &self, renderer: &mut R, x: i32, y: i32, bounds_x: i32, bounds_width: u32, color: Color, )

Draw the text onto a window and clipp the text to the given bounds

Source

pub fn draw<R: Renderer + ?Sized>( &self, renderer: &mut R, x: i32, y: i32, color: Color, )

Draw the text onto a window

Examples found in repository?
examples/character_map.rs (line 43)
15fn main() {
16    let (title, font_res) = match env::args().nth(1) {
17        Some(arg) => (arg.clone(), Font::from_path(&arg)),
18        None => ("Default Font".to_string(), Font::find(None, None, None)),
19    };
20
21    match font_res {
22        Ok(font) => {
23            let lines = [
24                font.render("ABCDEFGHIJK", 64.0),
25                font.render("LMNOPQRSTUV", 64.0),
26                font.render("WXYZabcdefg", 64.0),
27                font.render("hijklmnopqr", 64.0),
28                font.render("stuvwxyz.?!", 64.0),
29                font.render("0123456789 ", 64.0),
30            ];
31
32            let mut width = 0;
33            let mut height = 0;
34            for line in lines.iter() {
35                width = max(width, line.width());
36                height += line.height();
37            }
38
39            let redraw = move |window: &mut Window| {
40                window.set(Color::rgb(255, 255, 255));
41                let mut y = 0;
42                for line in lines.iter() {
43                    line.draw(window, 0, y, Color::rgb(0, 0, 0));
44                    y += line.height() as i32;
45                }
46                window.sync();
47            };
48
49            let mut window = Window::new_flags(
50                -1,
51                -1,
52                max(320, width),
53                max(32, height),
54                &format!("{} - Character Map", title),
55                &[WindowFlag::Resizable],
56            )
57            .unwrap();
58
59            redraw(&mut window);
60
61            loop {
62                for event in window.events() {
63                    match event.to_option() {
64                        EventOption::Resize(_) => redraw(&mut window),
65                        EventOption::Quit(_) => return,
66                        _ => (),
67                    }
68                }
69            }
70        }
71        Err(err) => {
72            let mut window =
73                Window::new(-1, -1, 320, 32, &format!("{} - Character Map", err)).unwrap();
74            window.set(Color::rgb(0, 0, 0));
75            window.sync();
76            loop {
77                for event in window.events() {
78                    match event.to_option() {
79                        EventOption::Quit(_) => return,
80                        _ => (),
81                    }
82                }
83            }
84        }
85    }
86}

Auto Trait Implementations§

§

impl<'a> Freeze for Text<'a>

§

impl<'a> RefUnwindSafe for Text<'a>

§

impl<'a> Send for Text<'a>

§

impl<'a> Sync for Text<'a>

§

impl<'a> Unpin for Text<'a>

§

impl<'a> UnsafeUnpin for Text<'a>

§

impl<'a> UnwindSafe for Text<'a>

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.