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
use crate::common::{Direction, HarfbuzzObject, Language, Owned, Script, Tag};
use crate::font::Position;
use crate::hb;
use fmt::Formatter;
use std::io::Read;
use std::os;
use std::os::raw::c_uint;
use std::ptr::NonNull;
use std::{fmt, io};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct SegmentProperties {
pub direction: Direction,
pub script: Script,
pub language: Language,
}
impl SegmentProperties {
pub fn from_raw(raw: hb::hb_segment_properties_t) -> Self {
let direction = Direction::from_raw(raw.direction);
let script = Script(raw.script);
let language = Language(raw.language);
SegmentProperties {
direction,
script,
language,
}
}
pub fn into_raw(self) -> hb::hb_segment_properties_t {
hb::hb_segment_properties_t {
direction: self.direction.to_raw(),
script: self.script.0,
language: self.language.0,
reserved1: std::ptr::null_mut(),
reserved2: std::ptr::null_mut(),
}
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct GlyphPosition {
pub x_advance: Position,
pub y_advance: Position,
pub x_offset: Position,
pub y_offset: Position,
var: hb::hb_var_int_t,
}
impl std::fmt::Debug for GlyphPosition {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GlyphPosition")
.field("x_advance", &self.x_advance)
.field("y_advance", &self.y_advance)
.field("x_offset", &self.x_offset)
.field("y_offset", &self.y_offset)
.finish()
}
}
impl GlyphPosition {
pub const fn new(
x_advance: Position,
y_advance: Position,
x_offset: Position,
y_offset: Position,
) -> Self {
GlyphPosition {
x_advance,
y_advance,
x_offset,
y_offset,
var: hb::hb_var_int_t { u32: 0 },
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GlyphFlags(pub hb::hb_glyph_flags_t);
#[allow(clippy::trivially_copy_pass_by_ref)]
impl GlyphFlags {
pub fn unsafe_to_break(&self) -> bool {
self.0 & hb::HB_GLYPH_FLAG_UNSAFE_TO_BREAK == hb::HB_GLYPH_FLAG_UNSAFE_TO_BREAK
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct GlyphInfo {
pub codepoint: u32,
mask: hb::hb_mask_t,
pub cluster: u32,
var1: hb::hb_var_int_t,
var2: hb::hb_var_int_t,
}
impl std::fmt::Debug for GlyphInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GlyphInfo")
.field("codepoint", &self.codepoint)
.field("cluster", &self.cluster)
.field("flags", &self.glyph_flags())
.finish()
}
}
impl GlyphInfo {
pub fn glyph_flags(&self) -> GlyphFlags {
GlyphFlags(unsafe { hb::hb_glyph_info_get_glyph_flags(self.as_raw()) })
}
fn as_raw(&self) -> *const hb::hb_glyph_info_t {
(self as *const GlyphInfo) as *const _
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ClusterLevel {
MonotoneGraphemes,
MonotoneCharacters,
Characters,
}
impl ClusterLevel {
pub fn from_raw(raw: hb::hb_buffer_cluster_level_t) -> Self {
match raw {
hb::HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES => ClusterLevel::MonotoneGraphemes,
hb::HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS => ClusterLevel::MonotoneCharacters,
hb::HB_BUFFER_CLUSTER_LEVEL_CHARACTERS => ClusterLevel::Characters,
_ => panic!("received unrecognized HB_BUFFER_CLUSTER_LEVEL"),
}
}
pub fn into_raw(self) -> hb::hb_buffer_cluster_level_t {
match self {
ClusterLevel::MonotoneGraphemes => hb::HB_BUFFER_CLUSTER_LEVEL_MONOTONE_GRAPHEMES,
ClusterLevel::MonotoneCharacters => hb::HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS,
ClusterLevel::Characters => hb::HB_BUFFER_CLUSTER_LEVEL_CHARACTERS,
}
}
}
impl Default for ClusterLevel {
fn default() -> Self {
ClusterLevel::MonotoneGraphemes
}
}
#[derive(Debug)]
pub(crate) struct GenericBuffer {
raw: NonNull<hb::hb_buffer_t>,
}
impl GenericBuffer {
pub(crate) fn new() -> Owned<GenericBuffer> {
let buffer = unsafe { hb::hb_buffer_create() };
unsafe { Owned::from_raw(buffer) }
}
#[allow(unused)]
pub(crate) fn empty() -> Owned<GenericBuffer> {
let buffer = unsafe { hb::hb_buffer_get_empty() };
unsafe { Owned::from_raw(buffer) }
}
pub(crate) fn len(&self) -> usize {
unsafe { hb::hb_buffer_get_length(self.as_raw()) as usize }
}
pub(crate) fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn add(&mut self, codepoint: u32, cluster: u32) {
unsafe {
hb::hb_buffer_add(self.as_raw(), codepoint, cluster);
}
}
pub(crate) fn add_str_item(&mut self, string: &str, item_start: usize, item_len: usize) {
assert!(item_start + item_len <= string.len());
let utf8_ptr = string.as_ptr() as *const _;
unsafe {
hb::hb_buffer_add_utf8(
self.as_raw(),
utf8_ptr,
string.len() as os::raw::c_int,
item_start as os::raw::c_uint,
item_len as os::raw::c_int,
);
}
}
pub(crate) fn append(&mut self, source: &GenericBuffer, start: c_uint, end: c_uint) {
unsafe {
hb::hb_buffer_append(self.as_raw(), source.as_raw(), start, end);
}
}
pub(crate) fn set_direction(&mut self, direction: Direction) {
unsafe { hb::hb_buffer_set_direction(self.as_raw(), direction.to_raw()) };
}
pub(crate) fn get_direction(&self) -> Direction {
Direction::from_raw(unsafe { hb::hb_buffer_get_direction(self.as_raw()) })
}
pub(crate) fn set_language(&mut self, lang: Language) {
unsafe { hb::hb_buffer_set_language(self.as_raw(), lang.0) }
}
pub(crate) fn get_language(&self) -> Option<Language> {
let raw_lang = unsafe { hb::hb_buffer_get_language(self.as_raw()) };
if raw_lang.is_null() {
None
} else {
Some(Language(raw_lang))
}
}
pub(crate) fn set_script(&mut self, script: hb::hb_script_t) {
unsafe { hb::hb_buffer_set_script(self.as_raw(), script) }
}
pub(crate) fn get_script(&self) -> hb::hb_script_t {
unsafe { hb::hb_buffer_get_script(self.as_raw()) }
}
pub(crate) fn guess_segment_properties(&mut self) {
unsafe { hb::hb_buffer_guess_segment_properties(self.as_raw()) };
}
pub(crate) fn get_segment_properties(&self) -> SegmentProperties {
unsafe {
let mut segment_props: hb::hb_segment_properties_t = std::mem::zeroed();
hb::hb_buffer_get_segment_properties(self.as_raw(), &mut segment_props as *mut _);
SegmentProperties::from_raw(segment_props)
}
}
pub(crate) fn set_cluster_level(&mut self, cluster_level: ClusterLevel) {
unsafe { hb::hb_buffer_set_cluster_level(self.as_raw(), cluster_level.into_raw()) }
}
pub(crate) fn get_cluster_level(&self) -> ClusterLevel {
ClusterLevel::from_raw(unsafe { hb::hb_buffer_get_cluster_level(self.as_raw()) })
}
pub(crate) fn pre_allocate(&mut self, size: usize) {
let size = size.min(std::os::raw::c_uint::max_value() as usize);
unsafe { hb::hb_buffer_pre_allocate(self.as_raw(), size as _) };
}
pub(crate) fn clear_contents(&mut self) {
unsafe { hb::hb_buffer_clear_contents(self.as_raw()) };
}
pub(crate) fn get_glyph_positions(&self) -> &[GlyphPosition] {
unsafe {
let mut length: u32 = 0;
let glyph_pos =
hb::hb_buffer_get_glyph_positions(self.as_raw(), &mut length as *mut u32);
std::slice::from_raw_parts(glyph_pos as *const _, length as usize)
}
}
pub(crate) fn get_glyph_infos(&self) -> &[GlyphInfo] {
unsafe {
let mut length: u32 = 0;
let glyph_infos = hb::hb_buffer_get_glyph_infos(self.as_raw(), &mut length as *mut u32);
std::slice::from_raw_parts(glyph_infos as *const _, length as usize)
}
}
pub(crate) fn reverse(&mut self) {
unsafe { hb::hb_buffer_reverse(self.as_raw()) };
}
pub(crate) fn reverse_range(&mut self, start: usize, end: usize) {
assert!(start <= self.len(), "{}", end <= self.len());
unsafe { hb::hb_buffer_reverse_range(self.as_raw(), start as u32, end as u32) }
}
pub(crate) fn set_content_type(&self, content_type: hb::hb_buffer_content_type_t) {
unsafe { hb::hb_buffer_set_content_type(self.as_raw(), content_type) }
}
pub(crate) fn content_type(&self) -> hb::hb_buffer_content_type_t {
unsafe { hb::hb_buffer_get_content_type(self.as_raw()) }
}
}
unsafe impl HarfbuzzObject for GenericBuffer {
type Raw = hb::hb_buffer_t;
unsafe fn from_raw(raw: *const Self::Raw) -> Self {
GenericBuffer {
raw: NonNull::new(raw as *mut _).unwrap(),
}
}
fn as_raw(&self) -> *mut Self::Raw {
self.raw.as_ptr()
}
unsafe fn reference(&self) {
hb::hb_buffer_reference(self.as_raw());
}
unsafe fn dereference(&self) {
hb::hb_buffer_destroy(self.as_raw());
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum SerializeFormat {
Text,
Json,
}
impl From<SerializeFormat> for hb::hb_buffer_serialize_format_t {
fn from(fmt: SerializeFormat) -> Self {
match fmt {
SerializeFormat::Text => hb::HB_BUFFER_SERIALIZE_FORMAT_TEXT,
SerializeFormat::Json => hb::HB_BUFFER_SERIALIZE_FORMAT_JSON,
}
}
}
bitflags! {
#[derive(Default)]
pub struct SerializeFlags: u32 {
const NO_CLUSTERS = hb::HB_BUFFER_SERIALIZE_FLAG_NO_CLUSTERS;
const NO_POSITIONS = hb::HB_BUFFER_SERIALIZE_FLAG_NO_POSITIONS;
const NO_GLYPH_NAMES = hb::HB_BUFFER_SERIALIZE_FLAG_NO_GLYPH_NAMES;
const GLYPH_EXTENTS = hb::HB_BUFFER_SERIALIZE_FLAG_GLYPH_EXTENTS;
const GLYPH_FLAGS = hb::HB_BUFFER_SERIALIZE_FLAG_GLYPH_FLAGS;
const NO_ADVANCES = hb::HB_BUFFER_SERIALIZE_FLAG_NO_ADVANCES;
}
}
#[derive(Debug)]
pub struct BufferSerializer<'a> {
font: Option<&'a crate::Font<'a>>,
buffer: &'a Owned<GenericBuffer>,
start: usize,
end: usize,
format: SerializeFormat,
flags: SerializeFlags,
bytes: io::Cursor<Vec<u8>>,
}
impl<'a> Read for BufferSerializer<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.bytes.read(buf) {
Ok(0) => {
if self.start > self.end.saturating_sub(1) {
return Ok(0);
}
let mut bytes_written = 0;
let num_serialized_items = unsafe {
hb::hb_buffer_serialize_glyphs(
self.buffer.as_raw(),
self.start as u32,
self.end as u32,
self.bytes.get_mut().as_mut_ptr() as *mut _,
self.bytes.get_ref().capacity() as u32,
&mut bytes_written,
self.font
.map(|f| f.as_raw())
.unwrap_or(std::ptr::null_mut()),
self.format.into(),
self.flags.bits(),
)
};
self.start += num_serialized_items as usize;
self.bytes.set_position(0);
unsafe { self.bytes.get_mut().set_len(bytes_written as usize) };
self.read(buf)
}
Ok(size) => Ok(size),
Err(err) => Err(err),
}
}
}
#[derive(Debug)]
pub enum TypedBuffer {
Unicode(UnicodeBuffer),
Glyphs(GlyphBuffer),
}
impl TypedBuffer {
pub unsafe fn take_from_raw(raw: *mut hb::hb_buffer_t) -> Option<TypedBuffer> {
let generic_buf: Owned<GenericBuffer> = Owned::from_raw(raw);
let content_type = generic_buf.content_type();
match content_type {
hb::HB_BUFFER_CONTENT_TYPE_UNICODE => {
Some(TypedBuffer::Unicode(UnicodeBuffer(generic_buf)))
}
hb::HB_BUFFER_CONTENT_TYPE_GLYPHS => {
Some(TypedBuffer::Glyphs(GlyphBuffer(generic_buf)))
}
_ => None,
}
}
}
pub struct UnicodeBuffer(pub(crate) Owned<GenericBuffer>);
impl UnicodeBuffer {
pub(crate) fn from_generic(generic: Owned<GenericBuffer>) -> Self {
generic.set_content_type(hb::HB_BUFFER_CONTENT_TYPE_UNICODE);
UnicodeBuffer(generic)
}
pub fn new() -> UnicodeBuffer {
UnicodeBuffer::from_generic(GenericBuffer::new())
}
pub fn into_raw(self) -> *mut hb::hb_buffer_t {
Owned::into_raw(self.0)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn add(mut self, codepoint: u32, cluster: u32) -> UnicodeBuffer {
self.0.add(codepoint, cluster);
self
}
pub fn add_str(mut self, str_slice: &str) -> UnicodeBuffer {
self.0.add_str_item(str_slice, 0, str_slice.len());
self
}
pub fn add_str_item(mut self, context: &str, item: &str) -> UnicodeBuffer {
const PANIC_MSG: &str = "`item` must be a substring of `context`";
let offset =
usize::checked_sub(item.as_ptr() as _, context.as_ptr() as _).expect(PANIC_MSG);
assert!(offset + item.len() <= context.len(), "{}", PANIC_MSG);
self.0.add_str_item(context, offset, item.len());
self
}
pub fn append(mut self, other: &UnicodeBuffer) -> UnicodeBuffer {
self.0.append(&other.0, 0, c_uint::max_value());
self
}
pub fn append_range(
mut self,
other: &UnicodeBuffer,
range: impl std::ops::RangeBounds<usize>,
) -> UnicodeBuffer {
let (start, end) = crate::start_end_range(range);
self.0.append(&other.0, start, end);
self
}
pub fn codepoints(&self) -> Codepoints<'_> {
Codepoints {
slice_iter: self.0.get_glyph_infos().iter(),
}
}
pub fn string_lossy(&self) -> String {
self.codepoints()
.map(|cp| std::char::from_u32(cp).unwrap_or('\u{FFFD}'))
.collect()
}
pub fn set_direction(mut self, direction: Direction) -> UnicodeBuffer {
self.0.set_direction(direction);
self
}
pub fn get_direction(&self) -> Direction {
self.0.get_direction()
}
pub fn set_script(mut self, script: Tag) -> UnicodeBuffer {
self.0
.set_script(unsafe { hb::hb_script_from_iso15924_tag(script.0) });
self
}
pub fn get_script(&self) -> Tag {
Tag(unsafe { hb::hb_script_to_iso15924_tag(self.0.get_script()) })
}
pub fn set_language(mut self, lang: Language) -> UnicodeBuffer {
self.0.set_language(lang);
self
}
pub fn get_language(&self) -> Option<Language> {
self.0.get_language()
}
pub fn guess_segment_properties(mut self) -> UnicodeBuffer {
self.0.guess_segment_properties();
self
}
pub fn get_segment_properties(&self) -> SegmentProperties {
self.0.get_segment_properties()
}
pub fn set_cluster_level(mut self, cluster_level: ClusterLevel) -> UnicodeBuffer {
self.0.set_cluster_level(cluster_level);
self
}
pub fn get_cluster_level(&self) -> ClusterLevel {
self.0.get_cluster_level()
}
pub fn pre_allocate(&mut self, size: usize) {
self.0.pre_allocate(size)
}
pub fn clear_contents(mut self) -> UnicodeBuffer {
self.0.clear_contents();
self
}
}
impl std::fmt::Debug for UnicodeBuffer {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.debug_struct("UnicodeBuffer")
.field("content", &self.string_lossy())
.field("direction", &self.get_direction())
.field("language", &self.get_language())
.field("script", &self.get_script())
.field("cluster_level", &self.get_cluster_level())
.finish()
}
}
impl std::default::Default for UnicodeBuffer {
fn default() -> UnicodeBuffer {
UnicodeBuffer::new()
}
}
#[derive(Debug, Clone)]
pub struct Codepoints<'a> {
slice_iter: std::slice::Iter<'a, GlyphInfo>,
}
impl<'a> Iterator for Codepoints<'a> {
type Item = u32;
fn next(&mut self) -> Option<u32> {
self.slice_iter.next().map(|info| info.codepoint)
}
}
pub struct GlyphBuffer(pub(crate) Owned<GenericBuffer>);
impl GlyphBuffer {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn into_raw(self) -> *mut hb::hb_buffer_t {
Owned::into_raw(self.0)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn get_glyph_positions(&self) -> &[GlyphPosition] {
self.0.get_glyph_positions()
}
pub fn get_glyph_infos(&self) -> &[GlyphInfo] {
self.0.get_glyph_infos()
}
pub fn reverse(&mut self) {
self.0.reverse()
}
pub fn reverse_range(&mut self, start: usize, end: usize) {
self.0.reverse_range(start, end)
}
pub fn clear(mut self) -> UnicodeBuffer {
self.0.clear_contents();
UnicodeBuffer::from_generic(self.0)
}
pub fn serializer<'a>(
&'a self,
font: Option<&'a crate::Font<'a>>,
format: SerializeFormat,
flags: SerializeFlags,
) -> BufferSerializer<'a> {
BufferSerializer {
font,
buffer: &self.0,
start: 0,
end: self.len(),
format,
flags,
bytes: io::Cursor::new(Vec::with_capacity(128)),
}
}
}
impl fmt::Debug for GlyphBuffer {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("GlyphBuffer")
.field("glyph_positions", &self.get_glyph_positions())
.field("glyph_infos", &self.get_glyph_infos())
.finish()
}
}
impl fmt::Display for GlyphBuffer {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut serializer =
self.serializer(None, SerializeFormat::Text, SerializeFlags::default());
let mut string = String::new();
serializer.read_to_string(&mut string).unwrap();
write!(fmt, "{}", string)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::assert_memory_layout_equal;
use crate::{shape, Face, Font};
#[test]
fn test_memory_layouts() {
assert_memory_layout_equal::<hb::hb_glyph_position_t, GlyphPosition>();
assert_memory_layout_equal::<hb::hb_glyph_info_t, GlyphInfo>();
}
#[test]
fn test_str_item_heap() {
let string = String::from("Test String for test");
UnicodeBuffer::new().add_str_item(&string, &string[5..10]);
}
#[test]
#[should_panic(expected = "must be a substring of")]
fn test_str_item_different_allocations() {
UnicodeBuffer::new().add_str_item("Test", "String");
}
#[test]
#[should_panic(expected = "must be a substring of")]
fn test_str_item_not_substring() {
let string = "Test String";
UnicodeBuffer::new().add_str_item(&string[0..5], &string[4..6]);
}
#[test]
#[should_panic(expected = "must be a substring of")]
fn test_str_item_not_substring2() {
let string = "Test String";
UnicodeBuffer::new().add_str_item(&string[4..], &string[0..5]);
}
#[test]
fn test_glyph_buffer_serialization_single_char() {
let path = "testfiles/SourceSansVariable-Roman.ttf";
let face = Face::from_file(path, 0).unwrap();
let font = Font::new(face);
let buffer = UnicodeBuffer::new().add_str("A");
let glyph_buffer = shape(&font, buffer, &[]);
let mut serializer = glyph_buffer.serializer(
Some(&font),
SerializeFormat::Text,
SerializeFlags::NO_ADVANCES
| SerializeFlags::NO_CLUSTERS
| SerializeFlags::NO_POSITIONS
| SerializeFlags::NO_GLYPH_NAMES,
);
let mut string = String::new();
serializer.read_to_string(&mut string).unwrap();
let string = string.replace(|c: char| !c.is_ascii_digit(), "");
assert_eq!(
string.parse::<u32>().unwrap(),
glyph_buffer.get_glyph_infos()[0].codepoint
);
}
#[test]
fn test_glyph_buffer_serialization_text() {
let path = "testfiles/SourceSansVariable-Roman.ttf";
let face = Face::from_file(path, 0).unwrap();
let font = Font::new(face);
let buffer = UnicodeBuffer::new().add_str("Hello 🌍");
let glyph_buffer = shape(&font, buffer, &[]);
let mut serializer = glyph_buffer.serializer(
Some(&font),
SerializeFormat::Text,
SerializeFlags::NO_ADVANCES
| SerializeFlags::NO_CLUSTERS
| SerializeFlags::NO_POSITIONS
| SerializeFlags::NO_GLYPH_NAMES,
);
let mut string = String::new();
serializer.read_to_string(&mut string).unwrap();
for (serialized_glyph, glyph_info) in string
.split_terminator::<&[char]>(&['|', '[', ']'])
.filter(|c| !c.is_empty())
.zip(glyph_buffer.get_glyph_infos())
{
assert_eq!(
serialized_glyph.parse::<u32>().unwrap(),
glyph_info.codepoint
);
}
}
}