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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{GlyphInfo, GlyphString};
use glib::{translate::*, Slice};

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

    #[inline]
    pub fn glyph_info(&self) -> &[GlyphInfo] {
        unsafe {
            let ptr = (*self.to_glib_none().0).glyphs as *const GlyphInfo;
            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.to_glib_none().0).glyphs as *mut GlyphInfo;
            Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
        }
    }

    #[inline]
    pub fn log_clusters(&self) -> &[i32] {
        unsafe {
            let ptr = (*self.to_glib_none().0).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.to_glib_none().0).log_clusters;
            Slice::from_glib_borrow_num_mut(ptr, self.num_glyphs() as usize)
        }
    }

    #[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:?}");
    }
}