1use glib::{Slice, translate::*};
4
5use crate::{GlyphInfo, GlyphString, ffi};
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")]
68 #[doc(alias = "get_logical_widths")]
69 pub fn logical_widths(&self, text: &str, rtl: bool) -> Vec<i32> {
70 let count = text.chars().count();
71 unsafe {
72 let mut logical_widths = vec![0i32; count];
76 ffi::pango_glyph_string_get_logical_widths(
77 mut_override(self.to_glib_none().0),
78 text.as_ptr() as *const _,
79 text.len().try_into().unwrap(),
80 rtl as i32,
81 logical_widths.as_mut_ptr(),
82 );
83 logical_widths
84 }
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 #[test]
91 fn glyph_string_logical_widths() {
92 const TXT: &str = "abcdefghijklmnopqrstuv";
93 let mut s = super::GlyphString::new();
94 s.set_size(TXT.len() as i32);
95 for i in 0..TXT.len() {
96 s.glyph_info_mut()[i].set_glyph(TXT.as_bytes()[i] as u32);
97 s.glyph_info_mut()[i].geometry_mut().set_width(12);
98 s.log_clusters_mut()[i] = i as i32;
99 }
100 let widths = s.logical_widths(TXT, false);
101 println!("{widths:?}");
102 }
103}