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

use glib::translate::*;

use crate::{GlyphItem, GlyphString, Item};

impl GlyphItem {
    pub fn item(&self) -> Item {
        unsafe { from_glib_none((*self.as_ptr()).item) }
    }

    pub fn glyph_string(&self) -> GlyphString {
        unsafe { from_glib_none((*self.as_ptr()).glyphs) }
    }

    /// Given a [`GlyphItem`][crate::GlyphItem] and the 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 [`GlyphString::logical_widths()`][crate::GlyphString::logical_widths()].
    /// ## `text`
    /// text that @self corresponds to
    ///   (glyph_item->item->offset is an offset from the
    ///   start of @text)
    /// ## `logical_widths`
    /// an array whose length is the number of
    ///   characters in glyph_item (equal to glyph_item->item->num_chars)
    ///   to be filled in with the resulting character widths.
    #[doc(alias = "pango_glyph_item_get_logical_widths")]
    #[doc(alias = "get_logical_widths")]
    pub fn logical_widths(&self, text: &str) -> Vec<i32> {
        let count = text.chars().count();
        unsafe {
            let mut logical_widths = Vec::with_capacity(count);
            ffi::pango_glyph_item_get_logical_widths(
                mut_override(self.to_glib_none().0),
                text.to_glib_none().0,
                logical_widths.as_mut_ptr(),
            );
            logical_widths.set_len(count);
            logical_widths
        }
    }
}