Skip to main content

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::{GlyphItem, GlyphString, Item, ffi};
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    /// offset is an offset from the
25    ///   start of @text)
26    ///
27    /// # Returns
28    ///
29    ///
30    /// ## `logical_widths`
31    /// num_chars`) to be filled in with the resulting
32    ///   character widths.
33    #[doc(alias = "pango_glyph_item_get_logical_widths")]
34    #[doc(alias = "get_logical_widths")]
35    pub fn logical_widths(&self, text: &str) -> Vec<i32> {
36        let count = text.chars().count();
37        unsafe {
38            let mut logical_widths = Vec::with_capacity(count);
39            ffi::pango_glyph_item_get_logical_widths(
40                mut_override(self.to_glib_none().0),
41                text.to_glib_none().0,
42                logical_widths.as_mut_ptr(),
43            );
44            logical_widths.set_len(count);
45            logical_widths
46        }
47    }
48}