pango/
glyph_string.rs
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")]
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::with_capacity(count);
73 ffi::pango_glyph_string_get_logical_widths(
74 mut_override(self.to_glib_none().0),
75 text.as_ptr() as *const _,
76 text.len().try_into().unwrap(),
77 rtl as i32,
78 logical_widths.as_mut_ptr(),
79 );
80 logical_widths.set_len(count);
81 logical_widths
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 #[test]
89 fn glyph_string_logical_widths() {
90 const TXT: &str = "abcdefghijklmnopqrstuv";
91 let mut s = super::GlyphString::new();
92 s.set_size(TXT.len() as i32);
93 for i in 0..TXT.len() {
94 s.glyph_info_mut()[i].set_glyph(TXT.as_bytes()[i] as u32);
95 s.glyph_info_mut()[i].geometry_mut().set_width(12);
96 s.log_clusters_mut()[i] = i as i32;
97 }
98 let widths = s.logical_widths(TXT, false);
99 println!("{widths:?}");
100 }
101}