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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::Analysis;
use crate::Font;
use crate::Rectangle;
use glib::object::IsA;
use glib::translate::*;
use std::mem;

glib::wrapper! {
    /// The [`GlyphString`][crate::GlyphString] structure is used to store strings
    /// of glyphs with geometry and visual attribute information.
    /// The storage for the glyph information is owned
    /// by the structure which simplifies memory management.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct GlyphString(Boxed<ffi::PangoGlyphString>);

    match fn {
        copy => |ptr| ffi::pango_glyph_string_copy(mut_override(ptr)),
        free => |ptr| ffi::pango_glyph_string_free(ptr),
        type_ => || ffi::pango_glyph_string_get_type(),
    }
}

impl GlyphString {
    /// Create a new [`GlyphString`][crate::GlyphString].
    ///
    /// # Returns
    ///
    /// the newly allocated [`GlyphString`][crate::GlyphString], which
    ///  should be freed with `pango_glyph_string_free()`.
    #[doc(alias = "pango_glyph_string_new")]
    pub fn new() -> GlyphString {
        unsafe { from_glib_full(ffi::pango_glyph_string_new()) }
    }

    /// Compute the logical and ink extents of a glyph string. See the documentation
    /// for [`FontExt::glyph_extents()`][crate::prelude::FontExt::glyph_extents()] for details about the interpretation
    /// of the rectangles.
    ///
    /// Examples of logical (red) and ink (green) rects:
    ///
    /// ![](rects1.png) ![](rects2.png)
    /// ## `font`
    /// a [`Font`][crate::Font]
    ///
    /// # Returns
    ///
    ///
    /// ## `ink_rect`
    /// rectangle used to store the extents of the glyph string
    ///  as drawn or [`None`] to indicate that the result is not needed.
    ///
    /// ## `logical_rect`
    /// rectangle used to store the logical extents of the
    ///  glyph string or [`None`] to indicate that the result is not needed.
    #[doc(alias = "pango_glyph_string_extents")]
    pub fn extents<P: IsA<Font>>(&mut self, font: &P) -> (Rectangle, Rectangle) {
        unsafe {
            let mut ink_rect = Rectangle::uninitialized();
            let mut logical_rect = Rectangle::uninitialized();
            ffi::pango_glyph_string_extents(
                self.to_glib_none_mut().0,
                font.as_ref().to_glib_none().0,
                ink_rect.to_glib_none_mut().0,
                logical_rect.to_glib_none_mut().0,
            );
            (ink_rect, logical_rect)
        }
    }

    /// Computes the extents of a sub-portion of a glyph string. The extents are
    /// relative to the start of the glyph string range (the origin of their
    /// coordinate system is at the start of the range, not at the start of the entire
    /// glyph string).
    /// ## `start`
    /// start index
    /// ## `end`
    /// end index (the range is the set of bytes with
    ///      indices such that start <= index < end)
    /// ## `font`
    /// a [`Font`][crate::Font]
    ///
    /// # Returns
    ///
    ///
    /// ## `ink_rect`
    /// rectangle used to
    ///  store the extents of the glyph string range as drawn or
    ///  [`None`] to indicate that the result is not needed.
    ///
    /// ## `logical_rect`
    /// rectangle used to
    ///  store the logical extents of the glyph string range or
    ///  [`None`] to indicate that the result is not needed.
    #[doc(alias = "pango_glyph_string_extents_range")]
    pub fn extents_range<P: IsA<Font>>(
        &mut self,
        start: i32,
        end: i32,
        font: &P,
    ) -> (Rectangle, Rectangle) {
        unsafe {
            let mut ink_rect = Rectangle::uninitialized();
            let mut logical_rect = Rectangle::uninitialized();
            ffi::pango_glyph_string_extents_range(
                self.to_glib_none_mut().0,
                start,
                end,
                font.as_ref().to_glib_none().0,
                ink_rect.to_glib_none_mut().0,
                logical_rect.to_glib_none_mut().0,
            );
            (ink_rect, logical_rect)
        }
    }

    //#[doc(alias = "pango_glyph_string_get_logical_widths")]
    //#[doc(alias = "get_logical_widths")]
    //pub fn logical_widths(&mut self, text: &str, embedding_level: i32, logical_widths: &[i32]) {
    //    unsafe { TODO: call ffi:pango_glyph_string_get_logical_widths() }
    //}

    /// Computes the logical width of the glyph string as can also be computed
    /// using [`extents()`][Self::extents()]. However, since this only computes the
    /// width, it's much faster. This is in fact only a convenience function that
    /// computes the sum of geometry.width for each glyph in the `self`.
    ///
    /// # Returns
    ///
    /// the logical width of the glyph string.
    #[doc(alias = "pango_glyph_string_get_width")]
    #[doc(alias = "get_width")]
    pub fn width(&mut self) -> i32 {
        unsafe { ffi::pango_glyph_string_get_width(self.to_glib_none_mut().0) }
    }

    /// Converts from character position to x position. (X position
    /// is measured from the left edge of the run). Character positions
    /// are computed by dividing up each cluster into equal portions.
    /// ## `text`
    /// the text for the run
    /// ## `length`
    /// the number of bytes (not characters) in `text`.
    /// ## `analysis`
    /// the analysis information return from [`itemize()`][crate::itemize()]
    /// ## `index_`
    /// the byte index within `text`
    /// ## `trailing`
    /// whether we should compute the result for the beginning ([`false`])
    ///  or end ([`true`]) of the character.
    ///
    /// # Returns
    ///
    ///
    /// ## `x_pos`
    /// location to store result
    #[doc(alias = "pango_glyph_string_index_to_x")]
    pub fn index_to_x(
        &mut self,
        text: &str,
        analysis: &mut Analysis,
        index_: i32,
        trailing: bool,
    ) -> i32 {
        let length = text.len() as i32;
        unsafe {
            let mut x_pos = mem::MaybeUninit::uninit();
            ffi::pango_glyph_string_index_to_x(
                self.to_glib_none_mut().0,
                text.to_glib_none().0,
                length,
                analysis.to_glib_none_mut().0,
                index_,
                trailing.into_glib(),
                x_pos.as_mut_ptr(),
            );
            let x_pos = x_pos.assume_init();
            x_pos
        }
    }

    /// Resize a glyph string to the given length.
    /// ## `new_len`
    /// the new length of the string.
    #[doc(alias = "pango_glyph_string_set_size")]
    pub fn set_size(&mut self, new_len: i32) {
        unsafe {
            ffi::pango_glyph_string_set_size(self.to_glib_none_mut().0, new_len);
        }
    }

    /// Convert from x offset to character position. Character positions
    /// are computed by dividing up each cluster into equal portions.
    /// In scripts where positioning within a cluster is not allowed
    /// (such as Thai), the returned value may not be a valid cursor
    /// position; the caller must combine the result with the logical
    /// attributes for the text to compute the valid cursor position.
    /// ## `text`
    /// the text for the run
    /// ## `length`
    /// the number of bytes (not characters) in text.
    /// ## `analysis`
    /// the analysis information return from [`itemize()`][crate::itemize()]
    /// ## `x_pos`
    /// the x offset (in Pango units)
    ///
    /// # Returns
    ///
    ///
    /// ## `index_`
    /// location to store calculated byte index within `text`
    ///
    /// ## `trailing`
    /// location to store a boolean indicating
    ///  whether the user clicked on the leading or trailing
    ///  edge of the character.
    #[doc(alias = "pango_glyph_string_x_to_index")]
    pub fn x_to_index(&mut self, text: &str, analysis: &mut Analysis, x_pos: i32) -> (i32, i32) {
        let length = text.len() as i32;
        unsafe {
            let mut index_ = mem::MaybeUninit::uninit();
            let mut trailing = mem::MaybeUninit::uninit();
            ffi::pango_glyph_string_x_to_index(
                self.to_glib_none_mut().0,
                text.to_glib_none().0,
                length,
                analysis.to_glib_none_mut().0,
                x_pos,
                index_.as_mut_ptr(),
                trailing.as_mut_ptr(),
            );
            let index_ = index_.assume_init();
            let trailing = trailing.assume_init();
            (index_, trailing)
        }
    }
}

impl Default for GlyphString {
    fn default() -> Self {
        Self::new()
    }
}