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 #[doc(alias = "pango_layout_line_x_to_index")]
74 pub fn x_to_index(&self, x_pos: i32) -> HitPosition {
75 let mut index = 0;
76 let mut trailing = 0;
77
78 let is_inside = unsafe {
79 from_glib(ffi::pango_layout_line_x_to_index(
80 self.to_glib_none().0,
81 x_pos,
82 &mut index,
83 &mut trailing,
84 ))
85 };
86
87 HitPosition {
88 index,
89 trailing,
90 is_inside,
91 }
92 }
93
94 #[doc(alias = "pango_layout_line_get_x_ranges")]
95 #[doc(alias = "get_x_ranges")]
96 pub fn x_ranges(&self, start_index: i32, end_index: i32) -> Vec<i32> {
97 unsafe {
98 let mut ranges = std::ptr::null_mut();
99 let mut n_ranges = std::mem::MaybeUninit::uninit();
100 ffi::pango_layout_line_get_x_ranges(
101 self.to_glib_none().0,
102 start_index,
103 end_index,
104 &mut ranges,
105 n_ranges.as_mut_ptr(),
106 );
107 FromGlibContainer::from_glib_full_num(ranges, 2 * n_ranges.assume_init() as usize)
108 }
109 }
110}