pango/
glyph_item.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::translate::*;
4
5use crate::{ffi, GlyphItem, GlyphString, Item};
6
7impl GlyphItem {
8    pub fn item(&self) -> Item {
9        unsafe { from_glib_none((*self.as_ptr()).item) }
10    }
11
12    pub fn glyph_string(&self) -> GlyphString {
13        unsafe { from_glib_none((*self.as_ptr()).glyphs) }
14    }
15
16    /// Given a [`GlyphItem`][crate::GlyphItem] and the corresponding text, determine the
17    /// width corresponding to each character.
18    ///
19    /// When multiple characters compose a single cluster, the width of the
20    /// entire cluster is divided equally among the characters.
21    ///
22    /// See also [`GlyphString::logical_widths()`][crate::GlyphString::logical_widths()].
23    /// ## `text`
24    /// text that @self corresponds to
25    ///   (glyph_item->item->offset is an offset from the
26    ///   start of @text)
27    /// ## `logical_widths`
28    /// an array whose length is the number of
29    ///   characters in glyph_item (equal to glyph_item->item->num_chars)
30    ///   to be filled in with the resulting character widths.
31    #[doc(alias = "pango_glyph_item_get_logical_widths")]
32    #[doc(alias = "get_logical_widths")]
33    pub fn logical_widths(&self, text: &str) -> Vec<i32> {
34        let count = text.chars().count();
35        unsafe {
36            let mut logical_widths = Vec::with_capacity(count);
37            ffi::pango_glyph_item_get_logical_widths(
38                mut_override(self.to_glib_none().0),
39                text.to_glib_none().0,
40                logical_widths.as_mut_ptr(),
41            );
42            logical_widths.set_len(count);
43            logical_widths
44        }
45    }
46}