Skip to main content

pango/
glyph_string.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use 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    /// 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    ///
59    /// # Returns
60    ///
61    ///
62    /// ## `logical_widths`
63    /// an array whose length is
64    ///   the number of characters in text (equal to `g_utf8_strlen (text, length)`
65    ///   unless text has `NUL` bytes) to be filled in with the resulting character
66    ///   widths.
67    #[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            // Pango only initializes the entries covered by the shaped
73            // glyphs; zero-initialize the rest instead of exposing
74            // uninitialized memory.
75            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}