1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use super::font::Font;
use crate::graphics::drawable::Drawable;
use crate::graphics::canvas::Canvas;
const EMPTY_STR: &str = "";
const PX_STR: &str = "px";
pub struct TextStyle {
pub italic: bool,
pub bold: bool,
pub underlined: bool,
}
impl Default for TextStyle {
fn default() -> TextStyle {
TextStyle {
italic: false,
bold: false,
underlined: false
}
}
}
pub struct Text<'a> {
pub coords: (usize, usize),
pub font: &'a Font,
pub text: &'a str,
pub style: TextStyle,
pub character_size: (usize, &'a str)
}
impl<'a> Text<'a> {
pub fn new(font: &'a Font) -> Text<'a> {
Text {
coords: (0,0),
font,
text: EMPTY_STR,
style: TextStyle::default(),
character_size: (26, PX_STR)
}
}
pub fn new_with_text_and_coords(font: &'a Font, text: &'a str, coords: (usize, usize)) -> Text<'a> {
Text {
coords,
font,
text,
style: TextStyle::default(),
character_size: (26, PX_STR)
}
}
pub fn new_with_options(font: &'a Font, text: &'a str, coords: (usize, usize), style: TextStyle, character_size: (usize, &'a str)) -> Text<'a> {
Text {
coords,
font,
text,
style,
character_size
}
}
pub fn set_text(&mut self, text: &'a str) {
self.text = text;
}
pub fn set_style(&mut self, style: TextStyle) {
self.style = style;
}
}
impl<'a> Drawable for Text<'a> {
fn draw_on_canvas(&self, canvas: &mut Canvas) {
let mut font = String::new();
if self.style.italic {
font.push_str("italic ");
}
if self.style.bold {
font.push_str("bold ");
}
if self.style.underlined {
unimplemented!("avoid underlined text for now");
}
font.push_str(&self.character_size.0.to_string());
font.push_str(self.character_size.1);
font.push_str(" '");
font.push_str(&self.font.name);
font.push_str("'");
canvas.context.set_font(&font);
canvas.fill_text(self.coords, self.text, None);
}
}