pango/
glyph_string.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use 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    /// Given a [`GlyphString`][crate::GlyphString] and corresponding text, determine the width
46    /// corresponding to each character.
47    ///
48    /// When multiple characters compose a single cluster, the width of the
49    /// entire cluster is divided equally among the characters.
50    ///
51    /// See also [`GlyphItem::logical_widths()`][crate::GlyphItem::logical_widths()].
52    /// ## `text`
53    /// the text corresponding to the glyphs
54    /// ## `length`
55    /// the length of @text, in bytes
56    /// ## `embedding_level`
57    /// the embedding level of the string
58    /// ## `logical_widths`
59    /// an array whose length is the number of
60    ///   characters in text (equal to `g_utf8_strlen (text, length)` unless
61    ///   text has `NUL` bytes) to be filled in with the resulting character widths.
62    #[doc(alias = "pango_glyph_string_get_logical_widths")]
63    #[doc(alias = "get_logical_widths")]
64    pub fn logical_widths(&self, text: &str, rtl: bool) -> Vec<i32> {
65        let count = text.chars().count();
66        unsafe {
67            let mut logical_widths = Vec::with_capacity(count);
68            ffi::pango_glyph_string_get_logical_widths(
69                mut_override(self.to_glib_none().0),
70                text.as_ptr() as *const _,
71                text.len().try_into().unwrap(),
72                rtl as i32,
73                logical_widths.as_mut_ptr(),
74            );
75            logical_widths.set_len(count);
76            logical_widths
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    #[test]
84    fn glyph_string_logical_widths() {
85        const TXT: &str = "abcdefghijklmnopqrstuv";
86        let mut s = super::GlyphString::new();
87        s.set_size(TXT.len() as i32);
88        for i in 0..TXT.len() {
89            s.glyph_info_mut()[i].set_glyph(TXT.as_bytes()[i] as u32);
90            s.glyph_info_mut()[i].geometry_mut().set_width(12);
91            s.log_clusters_mut()[i] = i as i32;
92        }
93        let widths = s.logical_widths(TXT, false);
94        println!("{widths:?}");
95    }
96}