pango/
layout.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, LayoutLine, LayoutRun};
6
7// rustdoc-stripper-ignore-next
8/// The result of [`LayoutLine::x_to_index`].
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10pub struct HitPosition {
11    index: i32,
12    trailing: i32,
13    is_inside: bool,
14}
15
16impl HitPosition {
17    // rustdoc-stripper-ignore-next
18    /// The UTF-8 byte offset of the grapheme closest to the position.
19    ///
20    /// This position is relative to the start of the [`Layout`]'s text.
21    ///
22    /// [`Layout`]: crate::Layout
23    pub fn index(self) -> i32 {
24        self.index
25    }
26
27    // rustdoc-stripper-ignore-next
28    /// The codepoint within the grapheme of the position.
29    ///
30    /// This will always be either `0`, or the number of `char`s (*not bytes!*)
31    /// in the grapheme. This represents whether the user clicked near the start
32    /// of the grapheme or near the end; this is important for things like
33    /// resolving cursor positions.
34    pub fn trailing(self) -> i32 {
35        self.trailing
36    }
37
38    // rustdoc-stripper-ignore-next
39    /// Whether or not the position was within the bounds of the line.
40    ///
41    /// If this is `false`, then `index` and `trailing` will always resolve
42    /// to either the very first or the very last position in the line; this
43    /// behaviour is dependent on the line's resolved writing direction.
44    pub fn is_inside(self) -> bool {
45        self.is_inside
46    }
47}
48
49impl LayoutLine {
50    // rustdoc-stripper-ignore-next
51    /// The byte index of the start of this line into the text used to create
52    /// the source [`Layout`].
53    ///
54    /// [`Layout`]: crate::Layout
55    #[cfg(not(feature = "v1_50"))]
56    #[cfg_attr(docsrs, doc(cfg(not(feature = "v1_50"))))]
57    pub fn start_index(&self) -> i32 {
58        unsafe { (*self.as_ptr()).start_index }
59    }
60
61    // rustdoc-stripper-ignore-next
62    /// The length of this line's text, in bytes.
63    #[cfg(not(feature = "v1_50"))]
64    #[cfg_attr(docsrs, doc(cfg(not(feature = "v1_50"))))]
65    pub fn length(&self) -> i32 {
66        unsafe { (*self.as_ptr()).length }
67    }
68
69    #[doc(alias = "pango_layout_line_runs")]
70    pub fn runs(&self) -> Vec<LayoutRun> {
71        unsafe { FromGlibPtrContainer::from_glib_none((*self.as_ptr()).runs) }
72    }
73    /// Converts from x offset to the byte index of the corresponding character
74    /// within the text of the layout.
75    ///
76    /// If @x_pos is outside the line, @index_ and @trailing will point to the very
77    /// first or very last position in the line. This determination is based on the
78    /// resolved direction of the paragraph; for example, if the resolved direction
79    /// is right-to-left, then an X position to the right of the line (after it)
80    /// results in 0 being stored in @index_ and @trailing. An X position to the
81    /// left of the line results in @index_ pointing to the (logical) last grapheme
82    /// in the line and @trailing being set to the number of characters in that
83    /// grapheme. The reverse is true for a left-to-right line.
84    /// ## `x_pos`
85    /// the X offset (in Pango units) from the left edge of the line.
86    ///
87    /// # Returns
88    ///
89    /// [`false`] if @x_pos was outside the line, [`true`] if inside
90    ///
91    /// ## `index_`
92    /// location to store calculated byte index for the grapheme
93    ///   in which the user clicked
94    ///
95    /// ## `trailing`
96    /// location to store an integer indicating where in the
97    ///   grapheme the user clicked. It will either be zero, or the number of
98    ///   characters in the grapheme. 0 represents the leading edge of the grapheme.
99    #[doc(alias = "pango_layout_line_x_to_index")]
100    pub fn x_to_index(&self, x_pos: i32) -> HitPosition {
101        let mut index = 0;
102        let mut trailing = 0;
103
104        let is_inside = unsafe {
105            from_glib(ffi::pango_layout_line_x_to_index(
106                self.to_glib_none().0,
107                x_pos,
108                &mut index,
109                &mut trailing,
110            ))
111        };
112
113        HitPosition {
114            index,
115            trailing,
116            is_inside,
117        }
118    }
119
120    /// Gets a list of visual ranges corresponding to a given logical range.
121    ///
122    /// This list is not necessarily minimal - there may be consecutive
123    /// ranges which are adjacent. The ranges will be sorted from left to
124    /// right. The ranges are with respect to the left edge of the entire
125    /// layout, not with respect to the line.
126    /// ## `start_index`
127    /// Start byte index of the logical range. If this value
128    ///   is less than the start index for the line, then the first range
129    ///   will extend all the way to the leading edge of the layout. Otherwise,
130    ///   it will start at the leading edge of the first character.
131    /// ## `end_index`
132    /// Ending byte index of the logical range. If this value is
133    ///   greater than the end index for the line, then the last range will
134    ///   extend all the way to the trailing edge of the layout. Otherwise,
135    ///   it will end at the trailing edge of the last character.
136    ///
137    /// # Returns
138    ///
139    ///
140    /// ## `ranges`
141    /// location to
142    ///   store a pointer to an array of ranges. The array will be of length
143    ///   `2*n_ranges`, with each range starting at `(*ranges)[2*n]` and of
144    ///   width `(*ranges)[2*n + 1] - (*ranges)[2*n]`. This array must be freed
145    ///   with g_free(). The coordinates are relative to the layout and are in
146    ///   Pango units.
147    #[doc(alias = "pango_layout_line_get_x_ranges")]
148    #[doc(alias = "get_x_ranges")]
149    pub fn x_ranges(&self, start_index: i32, end_index: i32) -> Vec<i32> {
150        unsafe {
151            let mut ranges = std::ptr::null_mut();
152            let mut n_ranges = std::mem::MaybeUninit::uninit();
153            ffi::pango_layout_line_get_x_ranges(
154                self.to_glib_none().0,
155                start_index,
156                end_index,
157                &mut ranges,
158                n_ranges.as_mut_ptr(),
159            );
160            FromGlibContainer::from_glib_full_num(ranges, 2 * n_ranges.assume_init() as usize)
161        }
162    }
163}