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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use super::{
OutlineCurveBuilder,
RawGlyph,
ScaledGlyph,
OutlinedGlyph,
Scale,
};
use glium::{
Display,
texture::{
Texture2d,
UncompressedFloatFormat,
MipmapsOption,
RawImage2d,
ClientFormat,
},
Rect,
};
use ttf_parser::{
Face,
GlyphId,
};
use std::{
collections::HashMap,
borrow::Cow,
ops::Range,
};
// ᶠᵉᵉᵈ ᵐᵉ /ᐠ-ⱉ-ᐟ\ノ
/// Хранилище глифов.
/// A glyph cache.
pub struct GlyphCache{
glyphs:HashMap<char,RawGlyph<Texture2d>>,
undefined_glyph:RawGlyph<Texture2d>,
// Немасштабированная ширина пробела
whitespace_advance:f32,
bounding_size:[f32;2],
}
impl GlyphCache{
/// Создаёт новое хранилище глифов для данной области номеров символов.
///
/// Игнорирует неопределённые символы.
///
/// range = `None` - использует все символы шрифта.
///
/// Creates a new glyph cache with the given range of characters ids.
///
/// Ignores undefined characters.
///
/// range = `None` - takes all characters of the font.
pub fn new(font:&Face,range:Option<Range<u16>>,scale:Scale,display:&Display)->GlyphCache{
let global_box=font.global_bounding_box();
let bounding_size=[
global_box.width() as f32*scale.horizontal,
global_box.height() as f32*scale.vertical,
];
// Неопределённый символ
let not_defined_id=GlyphId(0);
let undefined_glyph=build_glyph(not_defined_id,scale,&font,display).expect("No 'Not Defined' glyph");
// Ширина пробела
let whitespace_advance_id=GlyphId(3);
let whitespace_advance=font.glyph_hor_advance(whitespace_advance_id).expect("No 'Whitespace' glyph") as f32*scale.horizontal;
let range=if let Some(range)=range{
range
}
else{
1u16..font.number_of_glyphs()
};
let mut glyphs=HashMap::with_capacity(range.len());
for g in range{
let id=GlyphId(g);
if let Some(glyph)=build_glyph(id,scale,&font,display){
let character=unsafe{std::char::from_u32_unchecked(g as u32)};
glyphs.insert(character,glyph);
}
}
Self{
glyphs,
undefined_glyph,
whitespace_advance,
bounding_size,
}
}
/// Создаёт новое хранилище глифов для данного алфавита.
///
/// Игнорирует неопределённые символы.
///
/// Creates a new glyph cache for the given alphabet.
///
/// Ignors undefined characters.
pub fn new_alphabet(font:&Face,alphabet:&str,scale:Scale,display:&Display)->GlyphCache{
let global_box=font.global_bounding_box();
let bounding_size=[
global_box.width() as f32*scale.horizontal,
global_box.height() as f32*scale.vertical,
];
let mut glyphs=HashMap::with_capacity(alphabet.len());
// Неопределённый символ
let not_defined_id=GlyphId(0);
let undefined_glyph=build_glyph(not_defined_id,scale,&font,display).expect("No 'Not Defined' glyph");
// Ширина пробела
let whitespace_advance_id=GlyphId(3);
let whitespace_advance=font.glyph_hor_advance(whitespace_advance_id).expect("No 'Whitespace' glyph") as f32*scale.horizontal;
for character in alphabet.chars(){
if let Some(id)=font.glyph_index(character){
if let Some(glyph)=build_glyph(id,scale,&font,display){
glyphs.insert(character,glyph);
}
}
}
Self{
glyphs,
undefined_glyph,
whitespace_advance,
bounding_size
}
}
/// Создаёт и добавляет новый глиф для данного символа.
///
/// Игнорирует неопределённые символы.
/// Заменяет старый глиф для этого символа, если такой есть.
///
/// Creates and inserts a new glyph for the given character.
///
/// Ignors undefined characters.
/// Replaces the old glyph for this character if there is one.
pub fn insert_char(&mut self,character:char,font:&Face,scale:Scale,display:&Display){
if let Some(id)=font.glyph_index(character){
if let Some(glyph)=build_glyph(id,scale,font,display){
self.glyphs.insert(character,glyph);
}
}
}
/// Создаёт и вставляет новые глифы для данный символов.
///
/// Creates and inserts new glyphs for the given characters.
///
/// ```
/// for character in alphabet.chars(){
/// self.insert_char(character,font,scale,display);
/// }
/// ```
pub fn insert_str(&mut self,font:&Face,alphabet:&str,scale:Scale,display:&Display){
for character in alphabet.chars(){
self.insert_char(character,font,scale,display);
}
}
pub fn bounding_size(&self)->[f32; 2]{
self.bounding_size
}
}
impl RawGlyphCache for GlyphCache{
#[inline(always)]
fn whitespace_advance_width(&self,horizontal_scale:f32)->f32{
self.whitespace_advance*horizontal_scale
}
#[inline(always)]
fn raw_glyph(&self,character:char)->Option<&RawGlyph<Texture2d>>{
self.glyphs.get(&character)
}
#[inline(always)]
fn raw_undefined_glyph(&self)->&RawGlyph<Texture2d>{
&self.undefined_glyph
}
fn scale_for_height(&self, height:f32)->Scale{
let height0 = self.bounding_size[1];
let k = height/height0;
Scale::new(k, k)
}
}
fn build_glyph(id:GlyphId,scale:Scale,face:&Face,display:&Display)->Option<RawGlyph<Texture2d>>{
let mut outline_builder=OutlineCurveBuilder::default();
if let Some(bounds)=face.outline_glyph(id,&mut outline_builder){
// Сдвиг символа относительно глобальной рамки (global bounding box)
let offset=[
(bounds.x_min as f32*scale.horizontal),
(bounds.y_min as f32*scale.vertical),
];
// Размер символа, изображения
let size=[
(bounds.width() as f32*scale.horizontal).ceil(),
(bounds.height() as f32*scale.vertical).ceil(),
];
let glyph=OutlinedGlyph::raw(outline_builder.outline,offset,size,scale);
let width=size[0] as usize;
let height=size[1] as u32;
let len=width*height as usize;
let mut image=Vec::with_capacity(len);
glyph.draw(|_,a|{
let gray=255f32*a;
let byte=gray.round() as u8;
image.push(byte);
});
let texture=Texture2d::empty_with_format(
display,
UncompressedFloatFormat::U8,
MipmapsOption::NoMipmap,
width as u32,
height
).unwrap();
let rect=Rect{
left:0,
bottom:0,
width:width as u32,
height:height,
};
let raw_image=RawImage2d{
data:Cow::Borrowed(&image),
width:width as u32,
height:height,
format:ClientFormat::U8,
};
texture.write(rect,raw_image);
let advance_width=face.glyph_hor_advance(id).unwrap() as f32*scale.horizontal;
let glyph=RawGlyph::<Texture2d>::raw(
texture,
size,
offset,
advance_width,
);
Some(glyph)
}
else{
None
}
}
/// Типаж для определения хранилищ глифов.
///
/// A trait for defining glyph cache.
pub trait RawGlyphCache{
/// Возращает немасштабированный глиф.
///
/// Returns an unscaled glyph.
fn raw_glyph(&self,character:char)->Option<&RawGlyph<Texture2d>>;
/// Возращает немасштабированный глиф неопределённого символа.
///
/// Returns an unscaled undefined character glyph.
fn raw_undefined_glyph(&self)->&RawGlyph<Texture2d>;
/// Возращает немасштабированный глиф для данного или неопределённого символа.
///
/// Returns an unscaled glyph of the given character or of the undefined one.
fn raw_glyph_or_undefined(&self,character:char)->&RawGlyph<Texture2d>{
if let Some(glyph)=self.raw_glyph(character){
glyph
}
else{
self.raw_undefined_glyph()
}
}
/// Возращает масштабированный глиф.
///
/// Returns a scaled glyph.
fn scaled_glyph(&self,character:char,scale:Scale)->Option<ScaledGlyph<Texture2d>>{
if let Some(glyph)=self.raw_glyph(character){
Some(glyph.scale(scale))
}
else{
None
}
}
/// Возращает масштабированный глиф неопределённого символа.
///
/// Returns the scaled glyph of the undefined character.
fn scaled_undefined_glyph(&self,scale:Scale)->ScaledGlyph<Texture2d>{
self.raw_undefined_glyph().scale(scale)
}
/// Возращает масштабированный глиф для данного или неопределённого символа.
///
/// Returns the scaled glyph of the given character or of the undefined one.
fn scaled_glyph_or_undefined(&self,character:char,scale:Scale)->ScaledGlyph<Texture2d>{
if let Some(glyph)=self.raw_glyph(character){
glyph.scale(scale)
}
else{
self.raw_undefined_glyph().scale(scale)
}
}
fn text_width(&self,text:&str,scale:Scale)->f32{
let mut width=0f32;
for character in text.chars(){
width+=if let Some(glyph)=self.scaled_glyph(character,scale){
let advance_width=glyph.advance_width();
advance_width
}
else{
if character==' '{
self.whitespace_advance_width(scale.horizontal)
}
else{
self.scaled_undefined_glyph(scale).advance_width()
}
}
}
width
}
fn text_size(&self,text:&str,scale:Scale)->[f32;2]{
let mut size=[0f32;2];
for character in text.chars(){
if let Some(glyph)=self.scaled_glyph(character,scale){
let glyph_size=glyph.size();
if glyph_size[1] as f32>size[1]{
size[1]=glyph_size[1] as f32;
}
size[0]+=glyph.advance_width();
}
else{
if character==' '{
size[0]+=self.whitespace_advance_width(scale.horizontal);
}
else{
let glyph=self.scaled_undefined_glyph(scale);
let glyph_size=glyph.size();
if glyph_size[1] as f32>size[1]{
size[1]=glyph_size[1] as f32;
}
size[0]+=glyph.advance_width();
}
}
}
size
}
/// Возращает масштабированную ширину пробела.
///
/// Returns whitespace's scaled width.
fn whitespace_advance_width(&self,horizontal_scale:f32)->f32;
fn scale_for_height(&self,height:f32)->Scale;
}