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

use crate::GlyphItem;
use glib::{translate::*, GStr, GString};
use std::{marker::PhantomData, mem};

#[derive(Clone, Debug)]
pub struct GlyphItemIter<'item> {
    inner: ffi::PangoGlyphItemIter,
    text: GString,
    item: PhantomData<&'item GlyphItem>,
}

impl<'item> glib::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 = &'a Self;
    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::PangoGlyphItemIter, Self> {
        Stash(&self.inner, self)
    }
}

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