1use glib::{translate::*, Slice};
4
5use crate::{ffi, GlyphInfo, GlyphString};
6
7impl GlyphString {
8 #[inline]
9 pub fn num_glyphs(&self) -> i32 {
10 unsafe { (*self.as_ptr()).num_glyphs }
11 }
12
13 #[inline]
14 pub fn glyph_info(&self) -> &[GlyphInfo] {
15 unsafe {
16 let ptr = (*self.as_ptr()).glyphs;
17 Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
18 }
19 }
20
21 #[inline]
22 pub fn glyph_info_mut(&mut self) -> &mut [GlyphInfo] {
23 unsafe {
24 let ptr = (*self.as_ptr()).glyphs;
25 Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
26 }
27 }
28
29 #[inline]
30 pub fn log_clusters(&self) -> &[i32] {
31 unsafe {
32 let ptr = (*self.as_ptr()).log_clusters as *const i32;
33 Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
34 }
35 }
36
37 #[inline]
38 pub fn log_clusters_mut(&mut self) -> &mut [i32] {
39 unsafe {
40 let ptr = (*self.as_ptr()).log_clusters;
41 Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
42 }
43 }
44
45 #[doc(alias = "pango_glyph_string_get_logical_widths")]
46 #[doc(alias = "get_logical_widths")]
47 pub fn logical_widths(&self, text: &str, rtl: bool) -> Vec<i32> {
48 let count = text.chars().count();
49 unsafe {
50 let mut logical_widths = Vec::with_capacity(count);
51 ffi::pango_glyph_string_get_logical_widths(
52 mut_override(self.to_glib_none().0),
53 text.as_ptr() as *const _,
54 text.len().try_into().unwrap(),
55 rtl as i32,
56 logical_widths.as_mut_ptr(),
57 );
58 logical_widths.set_len(count);
59 logical_widths
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 #[test]
67 fn glyph_string_logical_widths() {
68 const TXT: &str = "abcdefghijklmnopqrstuv";
69 let mut s = super::GlyphString::new();
70 s.set_size(TXT.len() as i32);
71 for i in 0..TXT.len() {
72 s.glyph_info_mut()[i].set_glyph(TXT.as_bytes()[i] as u32);
73 s.glyph_info_mut()[i].geometry_mut().set_width(12);
74 s.log_clusters_mut()[i] = i as i32;
75 }
76 let widths = s.logical_widths(TXT, false);
77 println!("{widths:?}");
78 }
79}