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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
extern crate rusttype as rt;
use std::io;
use std::path::{Path};
use std::collections::hash_map::{ HashMap, Entry };
use graphics::character::{ CharacterCache, Character };
use graphics::types::{FontSize, Scalar};
use { gfx, Texture, TextureSettings };
use gfx::CombinedError;
#[derive(Debug)]
pub enum Error {
Texture(CombinedError),
IoError(io::Error),
NoFont,
}
impl From<CombinedError> for Error {
fn from(tex_err: CombinedError) -> Self {
Error::Texture(tex_err)
}
}
impl From<io::Error> for Error {
fn from(io_error: io::Error) -> Self {
Error::IoError(io_error)
}
}
pub struct GlyphCache<R, F> where R: gfx::Resources {
pub font: rt::Font<'static>,
factory: F,
data: HashMap<(FontSize, char), ([Scalar; 2], [Scalar; 2], Texture<R>)>
}
impl<R, F> GlyphCache<R, F> where R: gfx::Resources {
pub fn new<P>(font_path: P, factory: F) -> Result<Self, Error>
where P: AsRef<Path> {
use std::io::Read;
use std::fs::File;
let mut file = try!(File::open(font_path));
let mut file_buffer = Vec::new();
try!(file.read_to_end(&mut file_buffer));
let collection = rt::FontCollection::from_bytes(file_buffer);
let font = match collection.into_font() {
Some(font) => font,
None => return Err(Error::NoFont),
};
Ok(GlyphCache {
font: font,
factory: factory,
data: HashMap::new(),
})
}
}
impl<R, F> CharacterCache for GlyphCache<R, F> where
R: gfx::Resources,
F: gfx::Factory<R>,
{
type Texture = Texture<R>;
fn character<'a>(
&'a mut self,
size: FontSize,
ch: char
) -> Character<'a, Self::Texture> {
let size = ((size as f32) * 1.333).round() as u32 ;
match self.data.entry((size, ch)) {
Entry::Occupied(v) => {
let &mut (offset, size, ref texture) = v.into_mut();
Character {
offset: offset,
size: size,
texture: texture
}
}
Entry::Vacant(v) => {
let glyph = self.font.glyph(ch).unwrap();
let scale = rt::Scale::uniform(size as f32);
let mut glyph = glyph.scaled(scale);
if glyph.id() == rt::GlyphId(0) && glyph.shape().is_none() {
glyph = self.font.glyph('\u{FFFD}').unwrap().scaled(scale);
}
let h_metrics = glyph.h_metrics();
let bounding_box = glyph.exact_bounding_box().unwrap_or(rt::Rect{min: rt::Point{x: 0.0, y: 0.0}, max: rt::Point{x: 0.0, y: 0.0} });
let glyph = glyph.positioned(rt::point(0.0, 0.0));
let pixel_bounding_box = glyph.pixel_bounding_box().unwrap_or(rt::Rect{min: rt::Point{x: 0, y: 0}, max: rt::Point{x: 0, y: 0} });
let pixel_bb_width = pixel_bounding_box.width();
let pixel_bb_height = pixel_bounding_box.height();
let mut image_buffer = Vec::<u8>::new();
image_buffer.resize((pixel_bb_width * pixel_bb_height) as usize, 0);
glyph.draw(|x, y, v| {
let pos = (x + y * (pixel_bb_width as u32)) as usize;
image_buffer[pos] = (255.0 * v) as u8;
});
let &mut (offset, size, ref texture) = v.insert((
[
bounding_box.min.x as Scalar,
-pixel_bounding_box.min.y as Scalar,
],
[
h_metrics.advance_width as Scalar,
0 as Scalar,
],
{
if pixel_bb_width == 0 || pixel_bb_height == 0 {
Texture::empty(&mut self.factory)
.unwrap()
} else {
Texture::from_memory_alpha(
&mut self.factory,
&image_buffer,
pixel_bb_width as u32,
pixel_bb_height as u32,
&TextureSettings::new()
).unwrap()
}
},
));
Character {
offset: offset,
size: size,
texture: texture
}
}
}
}
}