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
247
248
249
250
// 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

#[cfg(any(feature = "v1_50", feature = "dox"))]
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_50")))]
use crate::Direction;
use crate::Rectangle;
use glib::translate::*;
use std::mem;
use std::ptr;

glib::wrapper! {
    /// A [`LayoutLine`][crate::LayoutLine] 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.
    #[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
    ///
    /// ## `logical_rect`
    /// rectangle used to store the logical
    ///   extents of the glyph string
    #[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, as the maximum of the heights
    /// of fonts used in this line.
    ///
    /// Note that the actual baseline-to-baseline distance between lines
    /// of text is influenced by other factors, such as
    /// [`Layout::set_spacing()`][crate::Layout::set_spacing()] and
    /// [`Layout::set_line_spacing()`][crate::Layout::set_line_spacing()].
    ///
    /// # 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
        }
    }

    /// Returns the length of the line, in bytes.
    ///
    /// # Returns
    ///
    /// the length of the line
    #[cfg(any(feature = "v1_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_50")))]
    #[doc(alias = "pango_layout_line_get_length")]
    #[doc(alias = "get_length")]
    pub fn length(&self) -> i32 {
        unsafe { ffi::pango_layout_line_get_length(self.to_glib_none().0) }
    }

    /// 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
    ///
    /// ## `logical_rect`
    /// rectangle used to store the logical
    ///   extents of the glyph string
    #[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)
        }
    }

    /// Returns the resolved direction of the line.
    ///
    /// # Returns
    ///
    /// the resolved direction of the line
    #[cfg(any(feature = "v1_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_50")))]
    #[doc(alias = "pango_layout_line_get_resolved_direction")]
    #[doc(alias = "get_resolved_direction")]
    pub fn resolved_direction(&self) -> Direction {
        unsafe {
            from_glib(ffi::pango_layout_line_get_resolved_direction(
                self.to_glib_none().0,
            ))
        }
    }

    /// Returns the start index of the line, as byte index
    /// into the text of the layout.
    ///
    /// # Returns
    ///
    /// the start index of the line
    #[cfg(any(feature = "v1_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_50")))]
    #[doc(alias = "pango_layout_line_get_start_index")]
    #[doc(alias = "get_start_index")]
    pub fn start_index(&self) -> i32 {
        unsafe { ffi::pango_layout_line_get_start_index(self.to_glib_none().0) }
    }

    /// 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
    ///   `2*n_ranges`, with each range starting at `(*ranges)[2*n]` and of
    ///   width `(*ranges)[2*n + 1] - (*ranges)[2*n]`. 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 units)
    #[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
        }
    }

    /// Returns whether this is the first line of the paragraph.
    ///
    /// # Returns
    ///
    /// [`true`] if this is the first line
    #[cfg(any(feature = "v1_50", feature = "dox"))]
    #[cfg_attr(feature = "dox", doc(cfg(feature = "v1_50")))]
    #[doc(alias = "pango_layout_line_is_paragraph_start")]
    pub fn is_paragraph_start(&self) -> bool {
        unsafe {
            from_glib(ffi::pango_layout_line_is_paragraph_start(
                self.to_glib_none().0,
            ))
        }
    }
}