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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
use std::collections::HashMap;
use glam::{Mat3, Mat4, Vec2, Vec3, Vec3A};
use ttf_parser::GlyphId;
use crate::{
error::MeshTextError,
util::{
glam_3d_vecs_from_raw_2d, glam_vecs_from_raw, mesh_to_flat_2d, mesh_to_indexed_flat_2d,
raster_to_mesh, raster_to_mesh_indexed, text_mesh_from_data, text_mesh_from_data_2d,
text_mesh_from_data_indexed, text_mesh_from_data_indexed_2d, GlyphOutlineBuilder,
},
BoundingBox, CacheType, FontFace, Glyph, IndexedMeshText, MeshText, QualitySettings,
TextSection, TriangleMesh,
};
type Mesh = (Vec<Vec3A>, BoundingBox);
type Mesh2D = (Vec<Vec2>, BoundingBox);
type IndexedMesh = (Vec<u32>, Vec<Vec3A>, BoundingBox);
type IndexedMesh2D = (Vec<u32>, Vec<Vec2>, BoundingBox);
/// A [MeshGenerator] handles rasterizing individual glyphs.
///
/// Each [MeshGenerator] will handle exactly one font. This means
/// if you need support for multiple fonts, you will need to create
/// multiple instances (one per font) of this generator.
pub struct MeshGenerator<T>
where
T: FontFace,
{
/// Cached non-indexed glyphs are stored in this [HashMap].
///
/// The key is the character itself, however because each
/// character can have a 2D and a 3D variant, in the 3D
/// variant each character is prefixed with an `_`.
#[allow(unused)]
pub(super) cache: HashMap<String, Mesh>,
/// The current [FontFace].
pub(super) font: T,
/// Cached indexed glyphs are stored in this [HashMap].
///
/// The key is the character itself, however because each
/// character can have a 2D and a 3D variant, in the 3D
/// variant each character is prefixed with an `_`.
#[allow(unused)]
pub(super) indexed_cache: HashMap<String, IndexedMesh>,
/// Quality settings for generating the text meshes.
pub(super) quality: QualitySettings,
/// Controls wether the generator will automatically
/// cache glyphs.
#[allow(unused)]
pub(super) use_cache: bool,
}
#[cfg(not(feature = "owned"))]
mod borrowed_mesh_generator {
use std::collections::HashMap;
use ttf_parser::GlyphId;
use crate::{FontFace, MeshGenerator, QualitySettings};
impl FontFace for ttf_parser::Face<'_> {
/// Computes glyph's horizontal advance.
///
/// This method is affected by variation axes.
///
/// Returns:
///
/// The horizontal advance of the glyph.
fn glyph_hor_advance(&self, glyph_id: GlyphId) -> Option<u16> {
ttf_parser::Face::glyph_hor_advance(self, glyph_id)
}
/// Resolves a Glyph ID for a code point.
///
/// All sub-table formats except Mixed Coverage (8) are supported.
///
/// If you need a more low-level control, prefer `Face::tables().cmap`.
///
/// Returns:
///
/// The [GlyphId] or `None` when the glyph is not found.
fn glyph_index(&self, code_point: char) -> Option<GlyphId> {
ttf_parser::Face::glyph_index(self, code_point)
}
/// Computes the face's height.
///
/// This method is affected by variation axes.
///
/// Returns:
///
/// The line height.
fn height(&self) -> i16 {
ttf_parser::Face::height(self)
}
/// Outlines a glyph and returns its tight bounding box.
///
/// **Warning**: since `ttf-parser` is a pull parser,
/// `OutlineBuilder` will emit segments even when outline is partially malformed.
/// You must check `outline_glyph()` result before using
/// `OutlineBuilder`'s output.
///
/// `gvar`, `glyf`, `CFF` and `CFF2` tables are supported.
/// And they will be accesses in this specific order.
///
/// This method is affected by variation axes.
///
/// Returns `None` when glyph has no outline or on error.
///
/// # Example
///
/// ```
/// use std::fmt::Write;
/// use ttf_parser;
///
/// struct Builder(String);
///
/// impl ttf_parser::OutlineBuilder for Builder {
/// fn move_to(&mut self, x: f32, y: f32) {
/// write!(&mut self.0, "M {} {} ", x, y).unwrap();
/// }
///
/// fn line_to(&mut self, x: f32, y: f32) {
/// write!(&mut self.0, "L {} {} ", x, y).unwrap();
/// }
///
/// fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
/// write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap();
/// }
///
/// fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
/// write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap();
/// }
///
/// fn close(&mut self) {
/// write!(&mut self.0, "Z ").unwrap();
/// }
/// }
///
/// let data = std::fs::read("assets/font/FiraMono-Regular.ttf").unwrap();
/// let face = ttf_parser::Face::parse(&data, 0).unwrap();
/// let mut builder = Builder(String::new());
/// let bbox = face.outline_glyph(ttf_parser::GlyphId(36), &mut builder).unwrap();
/// assert_eq!(builder.0, "M 161 176 L 106 0 L 20 0 L 245 689 L 355 689 L 579 0 L 489 0 \
/// L 434 176 L 161 176 Z M 411 248 L 298 615 L 184 248 L 411 248 Z ");
/// assert_eq!(bbox, ttf_parser::Rect { x_min: 20, y_min: 0, x_max: 579, y_max: 689 });
/// ```
fn outline_glyph(
&self,
glyph_id: GlyphId,
builder: &mut dyn ttf_parser::OutlineBuilder,
) -> Option<ttf_parser::Rect> {
ttf_parser::Face::outline_glyph(self, glyph_id, builder)
}
}
impl MeshGenerator<ttf_parser::Face<'_>> {
/// Creates a new [MeshGenerator].
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
pub fn new(font: &'static [u8]) -> Self {
let face =
ttf_parser::Face::parse(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality: QualitySettings::default(),
use_cache: true,
}
}
/// Creates a new [MeshGenerator] with custom quality settings.
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
/// * `quality`: The [QualitySettings] that should be used.
pub fn new_with_quality(font: &'static [u8], quality: QualitySettings) -> Self {
let face =
ttf_parser::Face::parse(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality,
use_cache: true,
}
}
/// Creates a new [MeshGenerator] with custom quality settings and no caching.
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
/// * `quality`: The [QualitySettings] that should be used.
pub fn new_without_cache(font: &'static [u8], quality: QualitySettings) -> Self {
let face =
ttf_parser::Face::parse(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality,
use_cache: false,
}
}
}
}
#[cfg(feature = "owned")]
mod owned_mesh_generator {
use crate::{FontFace, MeshGenerator, QualitySettings};
use std::collections::HashMap;
use owned_ttf_parser::{AsFaceRef, OwnedFace};
impl FontFace for OwnedFace {
/// Computes glyph's horizontal advance.
///
/// This method is affected by variation axes.
///
/// Returns:
///
/// The horizontal advance of the glyph.
fn glyph_hor_advance(&self, glyph_id: owned_ttf_parser::GlyphId) -> Option<u16> {
self.as_face_ref().glyph_hor_advance(glyph_id)
}
/// Resolves a Glyph ID for a code point.
///
/// All sub-table formats except Mixed Coverage (8) are supported.
///
/// If you need a more low-level control, prefer `Face::tables().cmap`.
///
/// Returns:
///
/// The [GlyphId] or `None` when the glyph is not found.
fn glyph_index(&self, code_point: char) -> Option<owned_ttf_parser::GlyphId> {
self.as_face_ref().glyph_index(code_point)
}
/// Computes the face's height.
///
/// This method is affected by variation axes.
///
/// Returns:
///
/// The line height.
fn height(&self) -> i16 {
self.as_face_ref().height()
}
/// Outlines a glyph and returns its tight bounding box.
///
/// **Warning**: since `ttf-parser` is a pull parser,
/// `OutlineBuilder` will emit segments even when outline is partially malformed.
/// You must check `outline_glyph()` result before using
/// `OutlineBuilder`'s output.
///
/// `gvar`, `glyf`, `CFF` and `CFF2` tables are supported.
/// And they will be accesses in this specific order.
///
/// This method is affected by variation axes.
///
/// Returns `None` when glyph has no outline or on error.
///
/// # Example
///
/// ```
/// use std::fmt::Write;
/// use ttf_parser;
///
/// struct Builder(String);
///
/// impl ttf_parser::OutlineBuilder for Builder {
/// fn move_to(&mut self, x: f32, y: f32) {
/// write!(&mut self.0, "M {} {} ", x, y).unwrap();
/// }
///
/// fn line_to(&mut self, x: f32, y: f32) {
/// write!(&mut self.0, "L {} {} ", x, y).unwrap();
/// }
///
/// fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
/// write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap();
/// }
///
/// fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
/// write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap();
/// }
///
/// fn close(&mut self) {
/// write!(&mut self.0, "Z ").unwrap();
/// }
/// }
///
/// let data = std::fs::read("assets/font/FiraMono-Regular.ttf").unwrap();
/// let face = ttf_parser::Face::parse(&data, 0).unwrap();
/// let mut builder = Builder(String::new());
/// let bbox = face.outline_glyph(ttf_parser::GlyphId(36), &mut builder).unwrap();
/// assert_eq!(builder.0, "M 161 176 L 106 0 L 20 0 L 245 689 L 355 689 L 579 0 L 489 0 \
/// L 434 176 L 161 176 Z M 411 248 L 298 615 L 184 248 L 411 248 Z ");
/// assert_eq!(bbox, ttf_parser::Rect { x_min: 20, y_min: 0, x_max: 579, y_max: 689 });
/// ```
fn outline_glyph(
&self,
glyph_id: owned_ttf_parser::GlyphId,
builder: &mut dyn owned_ttf_parser::OutlineBuilder,
) -> Option<owned_ttf_parser::Rect> {
self.as_face_ref().outline_glyph(glyph_id, builder)
}
}
impl MeshGenerator<OwnedFace> {
/// Creates a new [MeshGenerator].
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
pub fn new(font: Vec<u8>) -> Self {
let face = OwnedFace::from_vec(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality: QualitySettings::default(),
use_cache: true,
}
}
/// Creates a new [MeshGenerator] with custom quality settings.
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
/// * `quality`: The [QualitySettings] that should be used.
pub fn new_with_quality(font: Vec<u8>, quality: QualitySettings) -> Self {
let face = OwnedFace::from_vec(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality,
use_cache: true,
}
}
/// Creates a new [MeshGenerator] with custom quality settings and no caching.
///
/// Arguments:
///
/// * `font`: The font that will be used for rasterizing.
/// * `quality`: The [QualitySettings] that should be used.
pub fn new_without_cache(font: Vec<u8>, quality: QualitySettings) -> Self {
let face = OwnedFace::from_vec(font, 0).expect("Failed to generate font from data.");
Self {
cache: HashMap::new(),
font: face,
indexed_cache: HashMap::new(),
quality,
use_cache: false,
}
}
}
}
impl<T> MeshGenerator<T>
where
T: FontFace,
{
/// Removes all stored glyphs from the internal cache.
///
/// Normally it should not be necessary to do this manually unless your program
/// cached so many glyphs, that memory consumption becomes an issue.
///
/// This function does nothing if the current [MeshGenerator] does not have a cache.
///
/// # Example
///
/// ```rust
/// use meshtext::MeshGenerator;
///
/// let font_data = include_bytes!("../assets/font/FiraMono-Regular.ttf");
/// let mut generator = MeshGenerator::new(font_data);
///
/// generator.clear_cache();
/// ```
pub fn clear_cache(&mut self) {
if self.use_cache {
self.cache.clear();
}
}
/// Gets a reference to the currently loaded [FontFace].
///
/// For example this allows reading out the [FontFace::height],
/// or retrieving the [FontFace::glyph_index] of a certain [char].
pub fn font(&self) -> &T {
&self.font
}
/// Allows inserting a custom mesh into the internal cache that will be used for rendering
/// the given `glyph`.
///
/// Please note that this will not work if [MeshGenerator::new_without_cache] was used to
/// construct this [MeshGenerator].
///
/// Arguments:
///
/// * `glyph`: The glyph that will be pre-cached. If the given glyph is already present in the
/// cache, it will be overwritten.
/// * `flat`: Wether the flat or three-dimensional variant of the characters should be preloaded.
/// When set to `true` two coordinates per vertex must be specified in the `mesh`, otherwise three.
/// * `mesh`: The mesh that should be used for rendering the given `glyph`.
///
/// Note: For optimal results, make sure that all vertices of the `mesh` have coordinates in the range `0..1`.
/// This ensures that the font size will be consistent with that of the generated glyphs.
///
/// Returns:
///
/// A [Result] indicating if the operation was successful.
///
/// # Example
///
/// ```rust
/// use meshtext::{MeshGenerator, MeshText};
///
/// let font_data = include_bytes!("../assets/font/FiraMono-Regular.ttf");
/// let mut generator = MeshGenerator::new(font_data);
///
/// let triangle: Vec<f32> = vec![
/// 0.50, 0f32,
/// 0.25, 0.57,
/// 0f32, 0f32];
/// let triangle_mesh = MeshText::new(triangle).unwrap();
///
/// // Substitute the uppercase letter 'A' with a triangle for non-indexed flat meshes.
/// generator.precache_custom_glyph('A', true, &triangle_mesh).unwrap();
/// ```
pub fn precache_custom_glyph<M>(
&mut self,
glyph: char,
flat: bool,
mesh: &M,
) -> Result<(), Box<dyn MeshTextError>>
where
M: TriangleMesh,
{
if let Some(indices) = mesh.indices() {
if flat {
self.indexed_cache.insert(
glyph.to_string(),
(
indices,
glam_3d_vecs_from_raw_2d(mesh.vertices()),
mesh.bbox(),
),
);
} else {
self.indexed_cache.insert(
format!("_{}", glyph),
(indices, glam_vecs_from_raw(mesh.vertices()), mesh.bbox()),
);
}
} else if flat {
self.cache.insert(
glyph.to_string(),
(glam_3d_vecs_from_raw_2d(mesh.vertices()), mesh.bbox()),
);
} else {
self.cache.insert(
format!("_{}", glyph),
(glam_vecs_from_raw(mesh.vertices()), mesh.bbox()),
);
}
Ok(())
}
/// Fills the internal cache of a [MeshGenerator] with the given characters.
///
/// Arguments:
///
/// * `glyphs`: The glyphs that will be pre-cached. Each character should appear exactly once.
/// * `flat`: Wether the flat or three-dimensional variant of the characters should be preloaded.
/// If both variants should be pre-cached this function must be called twice with this parameter set
/// to `true` and `false`.
/// * `cache`: An optional value that controls which cache will be filled. [None] means both caches will be filled.
///
/// Returns:
///
/// A [Result] indicating if the operation was successful.
///
/// # Example
///
/// ```rust
/// use meshtext::MeshGenerator;
///
/// let font_data = include_bytes!("../assets/font/FiraMono-Regular.ttf");
/// let mut generator = MeshGenerator::new(font_data);
///
/// let common = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".to_string();
///
/// // Pre-cache both flat and three-dimensional glyphs both for indexed and non-indexed meshes.
/// generator.precache_glyphs(&common, false, None);
/// generator.precache_glyphs(&common, true, None);
/// ```
pub fn precache_glyphs(
&mut self,
glyphs: &str,
flat: bool,
cache: Option<CacheType>,
) -> Result<(), Box<dyn MeshTextError>> {
if let Some(cache_type) = cache {
match cache_type {
CacheType::Normal => {
for c in glyphs.chars() {
self.generate_glyph(c, flat, None)?;
}
}
CacheType::Indexed => {
for c in glyphs.chars() {
self.generate_glyph_indexed(c, flat, None)?;
}
}
}
} else {
// If no type is set explicitly, both variants will be pre-cached.
for c in glyphs.chars() {
self.generate_glyph(c, flat, None)?;
}
for c in glyphs.chars() {
self.generate_glyph_indexed(c, flat, None)?;
}
}
Ok(())
}
/// Generates the [MeshText] of a single character with a custom transformation.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The 4x4 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_glyph(
&mut self,
glyph: char,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
let mut mesh = self.load_from_cache(glyph, flat)?;
if let Some(value) = transform {
let transform = Mat4::from_cols_array(value);
for v in mesh.0.iter_mut() {
*v = transform.transform_point3a(*v);
}
mesh.1.transform(&transform);
}
Ok(text_mesh_from_data(mesh))
}
/// Generates the two-dimensional [MeshText] of a single character with a custom transformation.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `transform`: The 3x3 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired two-dimensional [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_glyph_2d(
&mut self,
glyph: char,
transform: Option<&[f32; 9]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
let mesh = self.load_from_cache(glyph, true)?;
let mut mesh = mesh_to_flat_2d(mesh);
if let Some(value) = transform {
let transform = Mat3::from_cols_array(value);
for v in mesh.0.iter_mut() {
*v = transform.transform_point2(*v);
}
mesh.1.transform_2d(&transform);
}
Ok(text_mesh_from_data_2d(mesh))
}
/// Generates the [IndexedMeshText] of a single character with a custom transformation.
///
/// This function generates a mesh with indices and vertices.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The 4x4 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [IndexedMeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_glyph_indexed(
&mut self,
glyph: char,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
let mut mesh = self.load_from_cache_indexed(glyph, flat)?;
if let Some(value) = transform {
let transform = Mat4::from_cols_array(value);
for v in mesh.1.iter_mut() {
*v = transform.transform_point3a(*v);
}
mesh.2.transform(&transform);
}
Ok(text_mesh_from_data_indexed(mesh))
}
/// Generates the two-dimensional [IndexedMeshText] of a single character
/// with a custom transformation.
///
/// This function generates a mesh with indices and vertices.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `transform`: The 3x3 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [IndexedMeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_glyph_indexed_2d(
&mut self,
glyph: char,
transform: Option<&[f32; 9]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
let mesh = self.load_from_cache_indexed(glyph, true)?;
let mut mesh = mesh_to_indexed_flat_2d(mesh);
if let Some(value) = transform {
let transform = Mat3::from_cols_array(value);
for v in mesh.1.iter_mut() {
*v = transform.transform_point2(*v);
}
mesh.2.transform_2d(&transform);
}
Ok(text_mesh_from_data_indexed_2d(mesh))
}
/// Generates the [Mesh] of a single character with a custom transformation given
/// as a [Mat4].
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The 4x4 homogenous transformation matrix.
///
/// Returns:
///
/// The desired [Mesh] or an [MeshTextError] if anything went wrong in the
/// process.
pub(crate) fn generate_glyph_with_glam_transform(
&mut self,
glyph: char,
flat: bool,
transform: &Mat4,
) -> Result<Mesh, Box<dyn MeshTextError>> {
let mut mesh = self.load_from_cache(glyph, flat)?;
for v in mesh.0.iter_mut() {
*v = transform.transform_point3a(*v);
}
mesh.1.transform(transform);
Ok(mesh)
}
/// Generates the [Mesh2D] of a single character with a custom transformation given
/// as a [Mat3].
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `transform`: The 3x3 homogenous transformation matrix.
///
/// Returns:
///
/// The desired [Mesh2D] or an [MeshTextError] if anything went wrong in the
/// process.
pub(crate) fn generate_glyph_with_glam_transform_2d(
&mut self,
glyph: char,
transform: &Mat3,
) -> Result<Mesh2D, Box<dyn MeshTextError>> {
let mesh = self.load_from_cache(glyph, true)?;
let mut mesh = mesh_to_flat_2d(mesh);
for v in mesh.0.iter_mut() {
*v = transform.transform_point2(*v);
}
mesh.1.transform_2d(transform);
Ok(mesh)
}
/// Generates the [IndexedMesh] of a single character with a custom transformation given
/// as a [Mat4].
///
/// This function handles indexed meshes.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The 4x4 homogenous transformation matrix.
///
/// Returns:
///
/// The desired [IndexedMesh] or an [MeshTextError] if anything went wrong in the
/// process.
pub(crate) fn generate_glyph_with_glam_transform_indexed(
&mut self,
glyph: char,
flat: bool,
transform: &Mat4,
) -> Result<IndexedMesh, Box<dyn MeshTextError>> {
let mut mesh = self.load_from_cache_indexed(glyph, flat)?;
for v in mesh.1.iter_mut() {
*v = transform.transform_point3a(*v);
}
mesh.2.transform(transform);
Ok(mesh)
}
/// Generates the [IndexedMesh2D] of a single character with a custom transformation given
/// as a [Mat3].
///
/// This function handles indexed meshes.
///
/// Arguments:
///
/// * `glyph`: The character that should be converted to a mesh.
/// * `transform`: The 3x3 homogenous transformation matrix.
///
/// Returns:
///
/// The desired [IndexedMesh2D] or an [MeshTextError] if anything went wrong in the
/// process.
pub(crate) fn generate_glyph_with_glam_transform_indexed_2d(
&mut self,
glyph: char,
transform: &Mat3,
) -> Result<IndexedMesh2D, Box<dyn MeshTextError>> {
let mesh = self.load_from_cache_indexed(glyph, true)?;
let mut mesh = mesh_to_indexed_flat_2d(mesh);
for v in mesh.1.iter_mut() {
*v = transform.transform_point2(*v);
}
mesh.2.transform_2d(transform);
Ok(mesh)
}
/// Generates the [MeshText] of a given text section.
///
/// Arguments:
///
/// * `text`: The text that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The optional 4x4 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_text_section(
&mut self,
text: &str,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
let base_transform = match transform {
Some(value) => Mat4::from_cols_array(value),
None => Mat4::IDENTITY,
};
let mut mesh = (Vec::new(), BoundingBox::empty());
let mut overall_advance = 0f32;
let mut chars_iter = text.chars();
// The first char will be handled differently if present.
if let Some(first_glyph) = chars_iter.next() {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(first_glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat4::from_translation(Vec3::new(overall_advance, 0f32, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform(first_glyph, flat, &transform)?;
// Add vertices and replace bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh = (mesh.0, glyph_mesh.1);
overall_advance += x_advance;
}
for glyph in chars_iter {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat4::from_translation(Vec3::new(overall_advance, 0f32, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform(glyph, flat, &transform)?;
// Add vertices and adjust bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh = (mesh.0, mesh.1.combine(&glyph_mesh.1));
overall_advance += x_advance;
}
Ok(text_mesh_from_data(mesh))
}
/// Generates two-dimensional [MeshText] for a given text section.
///
/// Arguments:
///
/// * `text`: The text that should be converted to a mesh.
/// * `transform`: The optional 3x3 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_text_section_2d(
&mut self,
text: &str,
transform: Option<&[f32; 9]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
let base_transform = match transform {
Some(value) => Mat3::from_cols_array(value),
None => Mat3::IDENTITY,
};
let mut mesh = (Vec::new(), BoundingBox::empty());
let mut overall_advance = 0f32;
let mut chars_iter = text.chars();
// The first char will be handled differently if present.
if let Some(first_glyph) = chars_iter.next() {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(first_glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat3::from_translation(Vec2::new(overall_advance, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform_2d(first_glyph, &transform)?;
// Add vertices and replace bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh = (mesh.0, glyph_mesh.1);
overall_advance += x_advance;
}
for glyph in chars_iter {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat3::from_translation(Vec2::new(overall_advance, 0f32));
let mut glyph_mesh = self.generate_glyph_with_glam_transform_2d(glyph, &transform)?;
// Add vertices and adjust bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh = (mesh.0, mesh.1.combine(&glyph_mesh.1));
overall_advance += x_advance;
}
Ok(text_mesh_from_data_2d(mesh))
}
/// Generates the [MeshText] of a given text section.
///
/// This function handles indexed meshes.
///
/// Arguments:
///
/// * `text`: The text that should be converted to a mesh.
/// * `flat`: Set this to `true` for 2D meshes, or to `false` in order
/// to generate a mesh with a depth of `1.0` units.
/// * `transform`: The optional 4x4 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_text_section_indexed(
&mut self,
text: &str,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
let base_transform = match transform {
Some(value) => Mat4::from_cols_array(value),
None => Mat4::IDENTITY,
};
let mut mesh = (Vec::new(), Vec::new(), BoundingBox::empty());
let mut overall_advance = 0f32;
let mut index_offset = 0;
let mut chars_iter = text.chars();
// The first char will be handled differently if present.
if let Some(first_glyph) = chars_iter.next() {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(first_glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat4::from_translation(Vec3::new(overall_advance, 0f32, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform_indexed(first_glyph, flat, &transform)?;
// Update index offset (note that glyph meshes can be empty).
if let Some(max) = glyph_mesh.0.iter().max() {
index_offset = *max + 1;
}
// Add vertices and replace bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh.1.append(&mut glyph_mesh.1);
mesh = (mesh.0, mesh.1, glyph_mesh.2);
overall_advance += x_advance;
}
for glyph in chars_iter {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat4::from_translation(Vec3::new(overall_advance, 0f32, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform_indexed(glyph, flat, &transform)?;
// Offset indices.
for i in glyph_mesh.0.iter_mut() {
*i += index_offset;
}
// Update index offset (note that glyph meshes can be empty).
if let Some(max) = glyph_mesh.0.iter().max() {
index_offset = *max + 1;
}
// Add vertices and indices and adjust bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh.1.append(&mut glyph_mesh.1);
mesh = (mesh.0, mesh.1, mesh.2.combine(&glyph_mesh.2));
overall_advance += x_advance;
}
Ok(text_mesh_from_data_indexed(mesh))
}
/// Generates two-dimensional [MeshText] for a given text section.
///
/// This function handles indexed meshes.
///
/// Arguments:
///
/// * `text`: The text that should be converted to a mesh.
/// * `transform`: The optional 3x3 homogenous transformation matrix in column
/// major order that will be applied to this text.
///
/// Returns:
///
/// The desired [MeshText] or an [MeshTextError] if anything went wrong in the
/// process.
fn generate_text_section_indexed_2d(
&mut self,
text: &str,
transform: Option<&[f32; 9]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
let base_transform = match transform {
Some(value) => Mat3::from_cols_array(value),
None => Mat3::IDENTITY,
};
let mut mesh = (Vec::new(), Vec::new(), BoundingBox::empty());
let mut overall_advance = 0f32;
let mut index_offset = 0;
let mut chars_iter = text.chars();
// The first char will be handled differently if present.
if let Some(first_glyph) = chars_iter.next() {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(first_glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat3::from_translation(Vec2::new(overall_advance, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform_indexed_2d(first_glyph, &transform)?;
// Update index offset (note that glyph meshes can be empty).
if let Some(max) = glyph_mesh.0.iter().max() {
index_offset = *max + 1;
}
// Add vertices and replace bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh.1.append(&mut glyph_mesh.1);
mesh = (mesh.0, mesh.1, glyph_mesh.2);
overall_advance += x_advance;
}
for glyph in chars_iter {
let x_advance = self
.font
.glyph_hor_advance(self.glyph_id_of_char(glyph))
.unwrap_or(0) as f32
/ self.font.height() as f32;
let transform =
base_transform * Mat3::from_translation(Vec2::new(overall_advance, 0f32));
let mut glyph_mesh =
self.generate_glyph_with_glam_transform_indexed_2d(glyph, &transform)?;
// Offset indices.
for i in glyph_mesh.0.iter_mut() {
*i += index_offset;
}
// Update index offset (note that glyph meshes can be empty).
if let Some(max) = glyph_mesh.0.iter().max() {
index_offset = *max + 1;
}
// Add vertices and indices and adjust bbox.
mesh.0.append(&mut glyph_mesh.0);
mesh.1.append(&mut glyph_mesh.1);
mesh = (mesh.0, mesh.1, mesh.2.combine(&glyph_mesh.2));
overall_advance += x_advance;
}
Ok(text_mesh_from_data_indexed_2d(mesh))
}
/// Loads the given glyph from the cache or adds it.
///
/// Arguments:
///
/// * `glyph`: The character that should be loaded.
/// * `flat`: Wether the character should be laid out in a 2D mesh.
///
/// Returns:
///
/// A [Result] containing the [Mesh] if successful, otherwise an [MeshTextError].
fn load_from_cache(&mut self, glyph: char, flat: bool) -> Result<Mesh, Box<dyn MeshTextError>> {
if flat {
match self.cache.get(&glyph.to_string()) {
Some(glyph_mesh) => Ok(glyph_mesh.to_owned()),
None => self.insert_into_cache(glyph, flat),
}
} else {
match self.cache.get(&format!("_{}", glyph)) {
Some(glyph_mesh) => Ok(glyph_mesh.to_owned()),
None => self.insert_into_cache(glyph, flat),
}
}
}
/// Loads the given glyph from the cache or adds it.
///
/// This function deals with indexed meshes.
///
/// Arguments:
///
/// * `glyph`: The character that should be loaded.
/// * `flat`: Wether the character should be laid out in a 2D mesh.
///
/// Returns:
///
/// A [Result] containing the [IndexedMesh] if successful, otherwise an [MeshTextError].
fn load_from_cache_indexed(
&mut self,
glyph: char,
flat: bool,
) -> Result<IndexedMesh, Box<dyn MeshTextError>> {
if flat {
match self.indexed_cache.get(&glyph.to_string()) {
Some(glyph_mesh) => Ok(glyph_mesh.to_owned()),
None => self.insert_into_cache_indexed(glyph, flat),
}
} else {
match self.indexed_cache.get(&format!("_{}", glyph)) {
Some(glyph_mesh) => Ok(glyph_mesh.to_owned()),
None => self.insert_into_cache_indexed(glyph, flat),
}
}
}
/// Generates a new [Mesh] from the loaded font and the given `glyph`
/// and inserts it into the internal `cache`.
///
/// Arguments:
///
/// * `glyph`: The character that should be loaded.
/// * `flat`: Wether the character should be laid out in a 2D mesh.
///
/// Returns:
///
/// A [Result] containing the [Mesh] if successful, otherwise an [MeshTextError].
fn insert_into_cache(
&mut self,
glyph: char,
flat: bool,
) -> Result<Mesh, Box<dyn MeshTextError>> {
let font_height = self.font.height() as f32;
let mut builder = GlyphOutlineBuilder::new(font_height, self.quality);
let glyph_index = self.glyph_id_of_char(glyph);
let mut depth = (0.5f32, -0.5f32);
let (rect, mesh) = match self.font.outline_glyph(glyph_index, &mut builder) {
Some(bbox) => {
let mesh = raster_to_mesh(&builder.get_glyph_outline(), flat)?;
(bbox, mesh)
}
None => {
// The glyph has no outline so it is most likely a space or any other
// character that can not be displayed.
// An empty mesh is cached for simplicity nevertheless.
depth = (0f32, 0f32);
(
ttf_parser::Rect {
x_min: 0,
y_min: 0,
x_max: 0,
y_max: 0,
},
Vec::new(),
)
}
};
// Add mesh to cache.
let bbox;
if flat {
bbox = BoundingBox {
max: Vec3A::new(
rect.x_max as f32 / font_height,
rect.y_max as f32 / font_height,
0f32,
),
min: Vec3A::new(
rect.x_min as f32 / font_height,
rect.y_min as f32 / font_height,
0f32,
),
};
self.cache.insert(glyph.to_string(), (mesh.clone(), bbox));
} else {
bbox = BoundingBox {
max: Vec3A::new(
rect.x_max as f32 / font_height,
rect.y_max as f32 / font_height,
depth.0,
),
min: Vec3A::new(
rect.x_min as f32 / font_height,
rect.y_min as f32 / font_height,
depth.1,
),
};
self.cache
.insert(format!("_{}", glyph), (mesh.clone(), bbox));
}
Ok((mesh, bbox))
}
/// Generates a new [IndexedMesh] from the loaded font and the given `glyph`
/// and inserts it into the internal `cache`.
///
/// Arguments:
///
/// * `glyph`: The character that should be loaded.
/// * `flat`: Wether the character should be laid out in a 2D mesh.
///
/// Returns:
///
/// A [Result] containing the [IndexedMesh] if successful, otherwise an [MeshTextError].
fn insert_into_cache_indexed(
&mut self,
glyph: char,
flat: bool,
) -> Result<IndexedMesh, Box<dyn MeshTextError>> {
let font_height = self.font.height() as f32;
let mut builder = GlyphOutlineBuilder::new(font_height, self.quality);
let glyph_index = self.glyph_id_of_char(glyph);
let mut depth = (0.5f32, -0.5f32);
let (rect, vertices, indices) = match self.font.outline_glyph(glyph_index, &mut builder) {
Some(bbox) => {
let mesh = raster_to_mesh_indexed(&builder.get_glyph_outline(), flat)?;
(bbox, mesh.0, mesh.1)
}
None => {
// The glyph has no outline so it is most likely a space or any other
// character that can not be displayed.
// An empty mesh is cached for simplicity nevertheless.
depth = (0f32, 0f32);
(
ttf_parser::Rect {
x_min: 0,
y_min: 0,
x_max: 0,
y_max: 0,
},
Vec::new(),
Vec::new(),
)
}
};
// Add mesh to cache.
let bbox;
if flat {
bbox = BoundingBox {
max: Vec3A::new(
rect.x_max as f32 / font_height,
rect.y_max as f32 / font_height,
0f32,
),
min: Vec3A::new(
rect.x_min as f32 / font_height,
rect.y_min as f32 / font_height,
0f32,
),
};
self.indexed_cache
.insert(glyph.to_string(), (indices.clone(), vertices.clone(), bbox));
} else {
bbox = BoundingBox {
max: Vec3A::new(
rect.x_max as f32 / font_height,
rect.y_max as f32 / font_height,
depth.0,
),
min: Vec3A::new(
rect.x_min as f32 / font_height,
rect.y_min as f32 / font_height,
depth.1,
),
};
self.indexed_cache.insert(
format!("_{}", glyph),
(indices.clone(), vertices.clone(), bbox),
);
}
Ok((indices, vertices, bbox))
}
/// Finds the [GlyphId] of a certain [char].
///
/// Arguments:
///
/// * `glyph`: The character of which the id is determined.
///
/// Returns:
///
/// The corresponding [GlyphId].
fn glyph_id_of_char(&self, glyph: char) -> GlyphId {
self.font
.glyph_index(glyph)
.unwrap_or(ttf_parser::GlyphId(0))
}
}
impl<T> TextSection<MeshText> for MeshGenerator<T>
where
T: FontFace,
{
fn generate_section(
&mut self,
text: &str,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
self.generate_text_section(text, flat, transform)
}
fn generate_section_2d(
&mut self,
text: &str,
transform: Option<&[f32; 9]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
self.generate_text_section_2d(text, transform)
}
}
impl<T> TextSection<IndexedMeshText> for MeshGenerator<T>
where
T: FontFace,
{
fn generate_section(
&mut self,
text: &str,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
self.generate_text_section_indexed(text, flat, transform)
}
fn generate_section_2d(
&mut self,
text: &str,
transform: Option<&[f32; 9]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
self.generate_text_section_indexed_2d(text, transform)
}
}
impl<T> Glyph<MeshText> for MeshGenerator<T>
where
T: FontFace,
{
fn generate_glyph(
&mut self,
glyph: char,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
self.generate_glyph(glyph, flat, transform)
}
fn generate_glyph_2d(
&mut self,
glyph: char,
transform: Option<&[f32; 9]>,
) -> Result<MeshText, Box<dyn MeshTextError>> {
self.generate_glyph_2d(glyph, transform)
}
}
impl<T> Glyph<IndexedMeshText> for MeshGenerator<T>
where
T: FontFace,
{
fn generate_glyph(
&mut self,
glyph: char,
flat: bool,
transform: Option<&[f32; 16]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
self.generate_glyph_indexed(glyph, flat, transform)
}
fn generate_glyph_2d(
&mut self,
glyph: char,
transform: Option<&[f32; 9]>,
) -> Result<IndexedMeshText, Box<dyn MeshTextError>> {
self.generate_glyph_indexed_2d(glyph, transform)
}
}