1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{translate::*, Slice};

use crate::{GlyphInfo, GlyphString};

impl GlyphString {
    #[inline]
    pub fn num_glyphs(&self) -> i32 {
        unsafe { (*self.as_ptr()).num_glyphs }
    }

    #[inline]
    pub fn glyph_info(&self) -> &[GlyphInfo] {
        unsafe {
            let ptr = (*self.as_ptr()).glyphs;
            Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
        }
    }

    #[inline]
    pub fn glyph_info_mut(&mut self) -> &mut [GlyphInfo] {
        unsafe {
            let ptr = (*self.as_ptr()).glyphs;
            Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
        }
    }

    #[inline]
    pub fn log_clusters(&self) -> &[i32] {
        unsafe {
            let ptr = (*self.as_ptr()).log_clusters as *const i32;
            Slice::from_glib_borrow_num(ptr, self.num_glyphs() as usize)
        }
    }

    #[inline]
    pub fn log_clusters_mut(&mut self) -> &mut [i32] {
        unsafe {
            let ptr = (*self.as_ptr()).log_clusters;
            Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
        }
    }

    /// Given a [`GlyphString`][crate::GlyphString] and corresponding text, determine the width
    /// corresponding to each character.
    ///
    /// When multiple characters compose a single cluster, the width of the
    /// entire cluster is divided equally among the characters.
    ///
    /// See also [`GlyphItem::logical_widths()`][crate::GlyphItem::logical_widths()].
    /// ## `text`
    /// the text corresponding to the glyphs
    /// ## `length`
    /// the length of @text, in bytes
    /// ## `embedding_level`
    /// the embedding level of the string
    /// ## `logical_widths`
    /// an array whose length is the number of
    ///   characters in text (equal to `g_utf8_strlen (text, length)` unless
    ///   text has `NUL` bytes) to be filled in with the resulting character widths.
    #[doc(alias = "pango_glyph_string_get_logical_widths")]
    #[doc(alias = "get_logical_widths")]
    pub fn logical_widths(&self, text: &str, rtl: bool) -> Vec<i32> {
        let count = text.chars().count();
        unsafe {
            let mut logical_widths = Vec::with_capacity(count);
            ffi::pango_glyph_string_get_logical_widths(
                mut_override(self.to_glib_none().0),
                text.as_ptr() as *const _,
                text.len().try_into().unwrap(),
                rtl as i32,
                logical_widths.as_mut_ptr(),
            );
            logical_widths.set_len(count);
            logical_widths
        }
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn glyph_string_logical_widths() {
        const TXT: &str = "abcdefghijklmnopqrstuv";
        let mut s = super::GlyphString::new();
        s.set_size(TXT.len() as i32);
        for i in 0..TXT.len() {
            s.glyph_info_mut()[i].set_glyph(TXT.as_bytes()[i] as u32);
            s.glyph_info_mut()[i].geometry_mut().set_width(12);
            s.log_clusters_mut()[i] = i as i32;
        }
        let widths = s.logical_widths(TXT, false);
        println!("{widths:?}");
    }
}