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
use encoding::Encoding;
use fontmetrics::FontMetrics;
use std::fmt;
use std::sync::Arc;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct FontRef {
n: usize,
encoding: Encoding,
metrics: Arc<FontMetrics>,
}
pub fn create_font_ref(n: usize,
encoding: Encoding,
metrics: Arc<FontMetrics>)
-> FontRef {
FontRef {
n: n,
encoding: encoding,
metrics: metrics,
}
}
impl FontRef {
pub fn get_encoding(&self) -> Encoding {
self.encoding.clone()
}
pub fn get_width(&self, size: f32, text: &str) -> f32 {
size * self.get_width_raw(text) as f32 / 1000.0
}
pub fn get_width_raw(&self, text: &str) -> u32 {
let mut result = 0;
for char in self.encoding.encode_string(text) {
result += self.metrics.get_width(char).unwrap_or(100) as u32;
}
result
}
}
impl fmt::Display for FontRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "/F{}", self.n)
}
}