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
// Take a look at the license at the top of the repository in the LICENSE file.

use std::{marker::PhantomData, mem};

use glib::{prelude::*, translate::*, GStr, GString};

use crate::GlyphItem;

/// A [`GlyphItemIter`][crate::GlyphItemIter] is an iterator over the clusters in a
/// [`GlyphItem`][crate::GlyphItem].
///
/// The *forward direction* of the iterator is the logical direction of text.
/// That is, with increasing @start_index and @start_char values. If @glyph_item
/// is right-to-left (that is, if `glyph_item->item->analysis.level` is odd),
/// then @start_glyph decreases as the iterator moves forward.  Moreover,
/// in right-to-left cases, @start_glyph is greater than @end_glyph.
///
/// An iterator should be initialized using either
/// pango_glyph_item_iter_init_start() or
/// pango_glyph_item_iter_init_end(), for forward and backward iteration
/// respectively, and walked over using any desired mixture of
/// pango_glyph_item_iter_next_cluster() and
/// pango_glyph_item_iter_prev_cluster().
///
/// A common idiom for doing a forward iteration over the clusters is:
///
/// ```text
/// PangoGlyphItemIter cluster_iter;
/// gboolean have_cluster;
///
/// for (have_cluster = pango_glyph_item_iter_init_start (&cluster_iter,
///                                                       glyph_item, text);
///      have_cluster;
///      have_cluster = pango_glyph_item_iter_next_cluster (&cluster_iter))
/// {
///   ...
/// }
/// ```
///
/// Note that @text is the start of the text for layout, which is then
/// indexed by `glyph_item->item->offset` to get to the text of @glyph_item.
/// The @start_index and @end_index values can directly index into @text. The
/// @start_glyph, @end_glyph, @start_char, and @end_char values however are
/// zero-based for the @glyph_item.  For each cluster, the item pointed at by
/// the start variables is included in the cluster while the one pointed at by
/// end variables is not.
///
/// None of the members of a [`GlyphItemIter`][crate::GlyphItemIter] should be modified manually.
#[derive(Clone, Debug)]
pub struct GlyphItemIter<'item> {
    inner: ffi::PangoGlyphItemIter,
    text: GString,
    item: PhantomData<&'item GlyphItem>,
}

impl<'item> StaticType for GlyphItemIter<'item> {
    #[inline]
    fn static_type() -> glib::Type {
        unsafe { from_glib(ffi::pango_glyph_item_iter_get_type()) }
    }
}

impl<'item> GlyphItemIter<'item> {
    #[doc(alias = "pango_glyph_item_iter_init_start")]
    pub fn new_start(glyph_item: &'item GlyphItem, text: &str) -> Result<Self, glib::BoolError> {
        unsafe {
            let mut iter = mem::MaybeUninit::zeroed();
            let text = GString::from(text);
            let res: bool = from_glib(ffi::pango_glyph_item_iter_init_start(
                iter.as_mut_ptr(),
                mut_override(glyph_item.to_glib_none().0),
                text.as_ptr(),
            ));

            if res {
                Ok(Self {
                    inner: iter.assume_init(),
                    text,
                    item: PhantomData,
                })
            } else {
                Err(glib::bool_error!("Failed to create glyph item iter"))
            }
        }
    }

    #[doc(alias = "pango_glyph_item_iter_init_end")]
    pub fn new_end(glyph_item: &'item GlyphItem, text: &str) -> Result<Self, glib::BoolError> {
        unsafe {
            let mut iter = mem::MaybeUninit::zeroed();
            let text = GString::from(text);
            let res: bool = from_glib(ffi::pango_glyph_item_iter_init_end(
                iter.as_mut_ptr(),
                mut_override(glyph_item.to_glib_none().0),
                text.as_ptr(),
            ));

            if res {
                Ok(Self {
                    inner: iter.assume_init(),
                    text,
                    item: PhantomData,
                })
            } else {
                Err(glib::bool_error!("Failed to create glyph item iter"))
            }
        }
    }

    #[doc(alias = "pango_glyph_item_iter_next_cluster")]
    pub fn next_cluster(&mut self) -> bool {
        unsafe {
            from_glib(ffi::pango_glyph_item_iter_next_cluster(
                self.to_glib_none_mut().0,
            ))
        }
    }

    #[doc(alias = "pango_glyph_item_iter_prev_cluster")]
    pub fn prev_cluster(&mut self) -> bool {
        unsafe {
            from_glib(ffi::pango_glyph_item_iter_prev_cluster(
                self.to_glib_none_mut().0,
            ))
        }
    }

    #[inline]
    pub fn glyph_item(&self) -> &'item GlyphItem {
        unsafe { &*(&self.inner.glyph_item as *const _ as *const GlyphItem) }
    }
    #[inline]
    pub fn text(&self) -> &GStr {
        self.text.as_gstr()
    }
    #[inline]
    pub fn start_glyph(&self) -> i32 {
        self.inner.start_glyph
    }
    #[inline]
    pub fn start_index(&self) -> i32 {
        self.inner.start_index
    }
    #[inline]
    pub fn start_char(&self) -> i32 {
        self.inner.start_char
    }
    #[inline]
    pub fn end_glyph(&self) -> i32 {
        self.inner.end_glyph
    }
    #[inline]
    pub fn end_index(&self) -> i32 {
        self.inner.end_index
    }
    #[inline]
    pub fn end_char(&self) -> i32 {
        self.inner.end_char
    }
}

impl<'item> IntoIterator for GlyphItemIter<'item> {
    type Item = (i32, i32, i32, i32, i32, i32);
    type IntoIter = GlyphItemIntoIter<'item>;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        GlyphItemIntoIter(Some(self))
    }
}

#[derive(Clone, Debug)]
#[repr(transparent)]
pub struct GlyphItemIntoIter<'item>(Option<GlyphItemIter<'item>>);

impl<'item> Iterator for GlyphItemIntoIter<'item> {
    type Item = (i32, i32, i32, i32, i32, i32);
    fn next(&mut self) -> Option<Self::Item> {
        if let Some(iter) = &mut self.0 {
            let values = (
                iter.start_glyph(),
                iter.start_index(),
                iter.start_char(),
                iter.end_glyph(),
                iter.end_index(),
                iter.end_char(),
            );
            if !iter.next_cluster() {
                self.0 = None;
            }
            Some(values)
        } else {
            None
        }
    }
}

impl<'item> std::iter::FusedIterator for GlyphItemIntoIter<'item> {}

#[doc(hidden)]
impl<'a, 'item> ToGlibPtr<'a, *const ffi::PangoGlyphItemIter> for GlyphItemIter<'item>
where
    'item: 'a,
{
    type Storage = PhantomData<&'a Self>;
    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::PangoGlyphItemIter, Self> {
        Stash(&self.inner, PhantomData)
    }
}

#[doc(hidden)]
impl<'a, 'item> ToGlibPtrMut<'a, *mut ffi::PangoGlyphItemIter> for GlyphItemIter<'item>
where
    'item: 'a,
{
    type Storage = PhantomData<&'a mut Self>;
    #[inline]
    fn to_glib_none_mut(&'a mut self) -> StashMut<'a, *mut ffi::PangoGlyphItemIter, Self> {
        StashMut(&mut self.inner, PhantomData)
    }
}