bdf2/
font.rs

1use std::collections::HashMap;
2
3use crate::{BoundingBox, Direction, Glyph, Property};
4
5/// Size of a font.
6#[derive(PartialEq, Eq, Clone, Copy, Debug)]
7pub struct Size {
8    /// Point size of the font.
9    pub pt: u16,
10
11    /// X-axis DPI.
12    pub x: u16,
13
14    /// Y-axis DPI.
15    pub y: u16,
16}
17
18/// A BDF font.
19#[derive(Clone, Debug)]
20pub struct Font {
21    format: String,
22
23    name: Option<String>,
24    version: Option<String>,
25
26    size: Option<Size>,
27    bounds: Option<BoundingBox>,
28
29    direction: Direction,
30
31    scalable_width: Option<(u32, u32)>,
32    device_width: Option<(u32, u32)>,
33
34    alternate_scalable_width: Option<(u32, u32)>,
35    alternate_device_width: Option<(u32, u32)>,
36
37    vector: Option<(u32, u32)>,
38
39    properties: HashMap<String, Property>,
40    glyphs: HashMap<char, Glyph>,
41}
42
43impl Default for Font {
44    #[inline]
45    fn default() -> Self {
46        Font {
47            format: "2.2".to_owned(),
48
49            name: None,
50            version: None,
51
52            size: None,
53            bounds: None,
54
55            direction: Default::default(),
56
57            scalable_width: None,
58            device_width: None,
59
60            alternate_scalable_width: None,
61            alternate_device_width: None,
62
63            vector: None,
64
65            properties: HashMap::new(),
66            glyphs: HashMap::new(),
67        }
68    }
69}
70
71impl Font {
72    /// Create a new font with the given name and content-version.
73    #[inline]
74    pub fn new<T: Into<String>>(name: T, version: Option<T>) -> Self {
75        Font {
76            name: Some(name.into()),
77            version: version.map(|v| v.into()),
78
79            ..Default::default()
80        }
81    }
82
83    /// Validates the definition.
84    pub fn validate(&self) -> bool {
85        if self.name.is_none() {
86            return false;
87        }
88
89        if self.size.is_none() {
90            return false;
91        }
92
93        if self.bounds.is_none() {
94            return false;
95        }
96
97        true
98    }
99
100    /// Gets BDF format version.
101    #[inline]
102    pub fn format(&self) -> &str {
103        &self.format
104    }
105
106    /// Sets the BDF format version.
107    #[inline]
108    pub fn set_format<T: Into<String>>(&mut self, format: T) {
109        self.format = format.into();
110    }
111
112    /// Gets the name.
113    #[inline]
114    pub fn name(&self) -> &str {
115        self.name.as_ref().unwrap().as_ref()
116    }
117
118    /// Sets the name.
119    #[inline]
120    pub fn set_name<T: Into<String>>(&mut self, name: T) {
121        self.name = Some(name.into());
122    }
123
124    /// Gets the content-version.
125    #[inline]
126    pub fn version(&self) -> Option<&str> {
127        self.version.as_ref().map(|v| v.as_ref())
128    }
129
130    /// Sets the content-version.
131    #[inline]
132    pub fn set_version<T: Into<String>>(&mut self, version: Option<T>) {
133        self.version = version.map(|v| v.into());
134    }
135
136    /// Gets the size.
137    #[inline]
138    pub fn size(&self) -> &Size {
139        self.size.as_ref().unwrap()
140    }
141
142    /// Sets the size.
143    #[inline]
144    pub fn set_size(&mut self, size: Size) {
145        self.size = Some(size);
146    }
147
148    /// Gets the default bounding box.
149    #[inline]
150    pub fn bounds(&self) -> &BoundingBox {
151        self.bounds.as_ref().unwrap()
152    }
153
154    /// Sets the default bounding box.
155    #[inline]
156    pub fn set_bounds(&mut self, bounds: BoundingBox) {
157        self.bounds = Some(bounds);
158    }
159
160    /// Gets the default direction.
161    #[inline]
162    pub fn direction(&self) -> Direction {
163        self.direction
164    }
165
166    /// Sets the default direction.
167    #[inline]
168    pub fn set_direction(&mut self, direction: Direction) {
169        self.direction = direction;
170    }
171
172    /// Gets the default scalable width.
173    #[inline]
174    pub fn scalable_width(&self) -> Option<&(u32, u32)> {
175        self.scalable_width.as_ref()
176    }
177
178    /// Sets the default scalable width.
179    #[inline]
180    pub fn set_scalable_width(&mut self, value: Option<(u32, u32)>) {
181        self.scalable_width = value;
182    }
183
184    /// Gets the default device width.
185    #[inline]
186    pub fn device_width(&self) -> Option<&(u32, u32)> {
187        self.device_width.as_ref()
188    }
189
190    /// Sets the default device width.
191    #[inline]
192    pub fn set_device_width(&mut self, value: Option<(u32, u32)>) {
193        self.device_width = value;
194    }
195
196    /// Gets the default alternate scalable width.
197    #[inline]
198    pub fn alternate_scalable_width(&self) -> Option<&(u32, u32)> {
199        self.alternate_scalable_width.as_ref()
200    }
201
202    /// Sets the default alternate scalable width.
203    #[inline]
204    pub fn set_alternate_scalable_width(&mut self, value: Option<(u32, u32)>) {
205        self.alternate_scalable_width = value;
206    }
207
208    /// Gets the default alternate device width.
209    #[inline]
210    pub fn alternate_device_width(&self) -> Option<&(u32, u32)> {
211        self.alternate_device_width.as_ref()
212    }
213
214    /// Sets the default alternate device width.
215    #[inline]
216    pub fn set_alternate_device_width(&mut self, value: Option<(u32, u32)>) {
217        self.alternate_device_width = value;
218    }
219
220    /// Gets the default offset vector.
221    #[inline]
222    pub fn vector(&self) -> Option<&(u32, u32)> {
223        self.vector.as_ref()
224    }
225
226    /// Sets the default offset vector.
227    #[inline]
228    pub fn set_vector(&mut self, value: Option<(u32, u32)>) {
229        self.vector = value;
230    }
231
232    /// Gets the properties.
233    #[inline]
234    pub fn properties(&self) -> &HashMap<String, Property> {
235        &self.properties
236    }
237
238    /// Gets a mutable reference to the properties.
239    #[inline]
240    pub fn properties_mut(&mut self) -> &mut HashMap<String, Property> {
241        &mut self.properties
242    }
243
244    /// Gets the glyphs.
245    #[inline]
246    pub fn glyphs(&self) -> &HashMap<char, Glyph> {
247        &self.glyphs
248    }
249
250    /// Gets a mutable reference to the glyphs.
251    #[inline]
252    pub fn glyphs_mut(&mut self) -> &mut HashMap<char, Glyph> {
253        &mut self.glyphs
254    }
255}