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
// 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::Rectangle;
use glib::translate::*;
use std::mem;
use std::ptr;

glib::wrapper! {
    /// The [`LayoutLine`][crate::LayoutLine] structure represents one of the lines resulting
    /// from laying out a paragraph via [`Layout`][crate::Layout]. [`LayoutLine`][crate::LayoutLine]
    /// structures are obtained by calling [`Layout::line()`][crate::Layout::line()] and
    /// are only valid until the text, attributes, or settings of the
    /// parent [`Layout`][crate::Layout] are modified.
    ///
    /// Routines for rendering PangoLayout objects are provided in
    /// code specific to each rendering system.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct LayoutLine(Shared<ffi::PangoLayoutLine>);

    match fn {
        ref => |ptr| ffi::pango_layout_line_ref(ptr),
        unref => |ptr| ffi::pango_layout_line_unref(ptr),
        type_ => || ffi::pango_layout_line_get_type(),
    }
}

impl LayoutLine {
    /// Computes the logical and ink extents of a layout line. See
    /// [`FontExt::glyph_extents()`][crate::prelude::FontExt::glyph_extents()] for details about the interpretation
    /// of the rectangles.
    ///
    /// # Returns
    ///
    ///
    /// ## `ink_rect`
    /// rectangle used to store the extents of
    ///  the glyph string as drawn, or [`None`]
    ///
    /// ## `logical_rect`
    /// rectangle used to store the logical
    ///  extents of the glyph string, or [`None`]
    #[doc(alias = "pango_layout_line_get_extents")]
    #[doc(alias = "get_extents")]
    pub fn extents(&self) -> (Rectangle, Rectangle) {
        unsafe {
            let mut ink_rect = Rectangle::uninitialized();
            let mut logical_rect = Rectangle::uninitialized();
            ffi::pango_layout_line_get_extents(
                self.to_glib_none().0,
                ink_rect.to_glib_none_mut().0,
                logical_rect.to_glib_none_mut().0,
            );
            (ink_rect, logical_rect)
        }
    }

    /// Computes the height of the line, ie the distance between
    /// this and the previous lines baseline.
    ///
    /// # Returns
    ///
    ///
    /// ## `height`
    /// return location for the line height
    #[cfg(any(feature = "v1_44", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_44")))]
    #[doc(alias = "pango_layout_line_get_height")]
    #[doc(alias = "get_height")]
    pub fn height(&self) -> i32 {
        unsafe {
            let mut height = mem::MaybeUninit::uninit();
            ffi::pango_layout_line_get_height(self.to_glib_none().0, height.as_mut_ptr());
            let height = height.assume_init();
            height
        }
    }

    /// Computes the logical and ink extents of `self` in device units.
    /// This function just calls [`extents()`][Self::extents()] followed by
    /// two [`extents_to_pixels()`][crate::extents_to_pixels()] calls, rounding `ink_rect` and `logical_rect`
    /// such that the rounded rectangles fully contain the unrounded one (that is,
    /// passes them as first argument to [`extents_to_pixels()`][crate::extents_to_pixels()]).
    ///
    /// # Returns
    ///
    ///
    /// ## `ink_rect`
    /// rectangle used to store the extents of
    ///  the glyph string as drawn, or [`None`]
    ///
    /// ## `logical_rect`
    /// rectangle used to store the logical
    ///  extents of the glyph string, or [`None`]
    #[doc(alias = "pango_layout_line_get_pixel_extents")]
    #[doc(alias = "get_pixel_extents")]
    pub fn pixel_extents(&self) -> (Rectangle, Rectangle) {
        unsafe {
            let mut ink_rect = Rectangle::uninitialized();
            let mut logical_rect = Rectangle::uninitialized();
            ffi::pango_layout_line_get_pixel_extents(
                self.to_glib_none().0,
                ink_rect.to_glib_none_mut().0,
                logical_rect.to_glib_none_mut().0,
            );
            (ink_rect, logical_rect)
        }
    }

    /// Gets a list of visual ranges corresponding to a given logical range.
    /// This list is not necessarily minimal - there may be consecutive
    /// ranges which are adjacent. The ranges will be sorted from left to
    /// right. The ranges are with respect to the left edge of the entire
    /// layout, not with respect to the line.
    /// ## `start_index`
    /// Start byte index of the logical range. If this value
    ///  is less than the start index for the line, then
    ///  the first range will extend all the way to the leading
    ///  edge of the layout. Otherwise it will start at the
    ///  leading edge of the first character.
    /// ## `end_index`
    /// Ending byte index of the logical range. If this value
    ///  is greater than the end index for the line, then
    ///  the last range will extend all the way to the trailing
    ///  edge of the layout. Otherwise, it will end at the
    ///  trailing edge of the last character.
    ///
    /// # Returns
    ///
    ///
    /// ## `ranges`
    ///
    ///  location to store a pointer to an array of ranges.
    ///  The array will be of length `<literal>`2*n_ranges`</literal>`,
    ///  with each range starting at `<literal>`(*ranges)[2*n]`</literal>`
    ///  and of width `<literal>`(*ranges)[2*n + 1] - (*ranges)[2*n]`</literal>`.
    ///  This array must be freed with `g_free()`. The coordinates are relative
    ///  to the layout and are in Pango units.
    #[doc(alias = "pango_layout_line_get_x_ranges")]
    #[doc(alias = "get_x_ranges")]
    pub fn x_ranges(&self, start_index: i32, end_index: i32) -> Vec<i32> {
        unsafe {
            let mut ranges = ptr::null_mut();
            let mut n_ranges = mem::MaybeUninit::uninit();
            ffi::pango_layout_line_get_x_ranges(
                self.to_glib_none().0,
                start_index,
                end_index,
                &mut ranges,
                n_ranges.as_mut_ptr(),
            );
            FromGlibContainer::from_glib_full_num(ranges, n_ranges.assume_init() as usize)
        }
    }

    /// Converts an index within a line to a X position.
    /// ## `index_`
    /// byte offset of a grapheme within the layout
    /// ## `trailing`
    /// an integer indicating the edge of the grapheme to retrieve
    ///  the position of. If > 0, the trailing edge of the grapheme,
    ///  if 0, the leading of the grapheme.
    ///
    /// # Returns
    ///
    ///
    /// ## `x_pos`
    /// location to store the x_offset (in Pango unit)
    #[doc(alias = "pango_layout_line_index_to_x")]
    pub fn index_to_x(&self, index_: i32, trailing: bool) -> i32 {
        unsafe {
            let mut x_pos = mem::MaybeUninit::uninit();
            ffi::pango_layout_line_index_to_x(
                self.to_glib_none().0,
                index_,
                trailing.into_glib(),
                x_pos.as_mut_ptr(),
            );
            let x_pos = x_pos.assume_init();
            x_pos
        }
    }
}