diff --git a/Gir.toml b/Gir.toml index 196b2aa..880d496 100644 --- a/Gir.toml +++ b/Gir.toml @@ -45,7 +45,6 @@ generate = [ "Pango.FontsetSimple", "Pango.Glyph", "Pango.GlyphItem", - "Pango.GlyphItemIter", "Pango.GlyphString", "Pango.GlyphUnit", "Pango.Gravity", @@ -209,6 +208,11 @@ status = "generate" name = "language" const = true +[[object]] +name = "Pango.GlyphItemIter" +#need manual iterator implementation +status = "ignored" + [[object]] name = "Pango.Layout" status = "generate" diff --git a/src/auto/glyph_item_iter.rs b/src/auto/glyph_item_iter.rs deleted file mode 100644 index c251bb4..0000000 --- a/src/auto/glyph_item_iter.rs +++ /dev/null @@ -1,56 +0,0 @@ -// 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 - -use glib::translate::*; -use pango_sys; -use GlyphItem; - -glib_wrapper! { - #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct GlyphItemIter(Boxed); - - match fn { - copy => |ptr| pango_sys::pango_glyph_item_iter_copy(mut_override(ptr)), - free => |ptr| pango_sys::pango_glyph_item_iter_free(ptr), - get_type => || pango_sys::pango_glyph_item_iter_get_type(), - } -} - -impl GlyphItemIter { - pub fn init_end(&mut self, glyph_item: &mut GlyphItem, text: &str) -> bool { - unsafe { - from_glib(pango_sys::pango_glyph_item_iter_init_end( - self.to_glib_none_mut().0, - glyph_item.to_glib_none_mut().0, - text.to_glib_none().0, - )) - } - } - - pub fn init_start(&mut self, glyph_item: &mut GlyphItem, text: &str) -> bool { - unsafe { - from_glib(pango_sys::pango_glyph_item_iter_init_start( - self.to_glib_none_mut().0, - glyph_item.to_glib_none_mut().0, - text.to_glib_none().0, - )) - } - } - - pub fn next_cluster(&mut self) -> bool { - unsafe { - from_glib(pango_sys::pango_glyph_item_iter_next_cluster( - self.to_glib_none_mut().0, - )) - } - } - - pub fn prev_cluster(&mut self) -> bool { - unsafe { - from_glib(pango_sys::pango_glyph_item_iter_prev_cluster( - self.to_glib_none_mut().0, - )) - } - } -} diff --git a/src/auto/mod.rs b/src/auto/mod.rs index d797690..442ae82 100644 --- a/src/auto/mod.rs +++ b/src/auto/mod.rs @@ -65,9 +65,6 @@ pub use self::font_metrics::FontMetrics; mod glyph_item; pub use self::glyph_item::GlyphItem; -mod glyph_item_iter; -pub use self::glyph_item_iter::GlyphItemIter; - mod glyph_string; pub use self::glyph_string::GlyphString; diff --git a/src/glyph_item_iter.rs b/src/glyph_item_iter.rs new file mode 100644 index 0000000..c348f20 --- /dev/null +++ b/src/glyph_item_iter.rs @@ -0,0 +1,207 @@ +// Copyright 2019, The Gtk-rs Project Developers. +// See the COPYRIGHT file at the top-level directory of this distribution. +// Licensed under the MIT license, see the LICENSE file or + +use glib::translate::*; +use pango_sys; +use std::marker::PhantomData; +use GlyphItem; + +//Note: This type not exported +glib_wrapper! { + #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct GlyphItemIter(Boxed); + + match fn { + copy => |ptr| pango_sys::pango_glyph_item_iter_copy(mut_override(ptr)), + free => |ptr| pango_sys::pango_glyph_item_iter_free(ptr), + init => |_ptr| (), + clear => |_ptr| (), + get_type => || pango_sys::pango_glyph_item_iter_get_type(), + } +} + +impl GlyphItemIter { + pub(crate) unsafe fn init_end(glyph_item: &GlyphItem, text: &str) -> Option { + let mut iter = GlyphItemIter::uninitialized(); + let ret = from_glib(pango_sys::pango_glyph_item_iter_init_end( + iter.to_glib_none_mut().0, + mut_override(glyph_item.to_glib_none().0), + text.to_glib_none().0, + )); + if ret { + Some(iter) + } else { + None + } + } + + pub(crate) unsafe fn init_start(glyph_item: &GlyphItem, text: &str) -> Option { + let mut iter = GlyphItemIter::uninitialized(); + let ret = from_glib(pango_sys::pango_glyph_item_iter_init_start( + iter.to_glib_none_mut().0, + mut_override(glyph_item.to_glib_none().0), + text.to_glib_none().0, + )); + if ret { + Some(iter) + } else { + None + } + } + + pub fn next_cluster(&mut self) -> bool { + unsafe { + from_glib(pango_sys::pango_glyph_item_iter_next_cluster( + self.to_glib_none_mut().0, + )) + } + } + + pub fn prev_cluster(&mut self) -> bool { + unsafe { + from_glib(pango_sys::pango_glyph_item_iter_prev_cluster( + self.to_glib_none_mut().0, + )) + } + } + + pub fn to_data(&self) -> GlyphItemIteratorData { + GlyphItemIteratorData { + start_glyph: self.0.start_glyph as usize, + end_glyph: self.0.end_glyph as usize, + start_index: self.0.start_index as usize, + end_index: self.0.end_index as usize, + start_char: self.0.start_char as usize, + end_char: self.0.end_char as usize, + } + } +} + +pub struct GlyphItemIteratorData { + pub start_glyph: usize, + pub start_index: usize, + pub start_char: usize, + + pub end_glyph: usize, + pub end_index: usize, + pub end_char: usize, +} + +enum NormalIterator {} +enum ReverseIterator {} + +struct GlyphItemIterator<'a, T> { + item: &'a GlyphItem, + text: &'a str, + iter: Option, + _mode: PhantomData, +} + +impl GlyphItem { + pub fn iter<'a>( + &'a self, + text: &'a str, + ) -> impl DoubleEndedIterator + 'a { + GlyphItemIterator:: { + item: self, + text, + iter: None, + _mode: PhantomData, + } + } + + pub fn reverse_iter<'a>( + &'a self, + text: &'a str, + ) -> impl DoubleEndedIterator + 'a { + GlyphItemIterator:: { + item: self, + text, + iter: None, + _mode: PhantomData, + } + } +} + +impl<'a> Iterator for GlyphItemIterator<'a, NormalIterator> { + type Item = GlyphItemIteratorData; + + fn next(&mut self) -> Option { + if let Some(ref mut iter) = self.iter { + if iter.next_cluster() { + Some(iter.to_data()) + } else { + None + } + } else { + let iter = unsafe { GlyphItemIter::init_start(self.item, self.text) }; + if let Some(iter) = iter { + let data = iter.to_data(); + self.iter = Some(iter); + Some(data) + } else { + None + } + } + } +} + +impl<'a> Iterator for GlyphItemIterator<'a, ReverseIterator> { + type Item = GlyphItemIteratorData; + + fn next(&mut self) -> Option { + if let Some(ref mut iter) = self.iter { + if iter.prev_cluster() { + Some(iter.to_data()) + } else { + None + } + } else { + let iter = unsafe { GlyphItemIter::init_end(self.item, self.text) }; + if let Some(iter) = iter { + let data = iter.to_data(); + self.iter = Some(iter); + Some(data) + } else { + None + } + } + } +} + +impl<'a> DoubleEndedIterator for GlyphItemIterator<'a, NormalIterator> { + fn next_back(&mut self) -> Option { + if let Some(ref mut iter) = self.iter { + if iter.prev_cluster() { + Some(iter.to_data()) + } else { + None + } + } else if let Some(iter) = unsafe { GlyphItemIter::init_end(self.item, self.text) } { + let data = iter.to_data(); + self.iter = Some(iter); + Some(data) + } else { + None + } + } +} + +impl<'a> DoubleEndedIterator for GlyphItemIterator<'a, ReverseIterator> { + fn next_back(&mut self) -> Option { + if let Some(ref mut iter) = self.iter { + if iter.next_cluster() { + Some(iter.to_data()) + } else { + None + } + } else if let Some(iter) = unsafe { GlyphItemIter::init_start(self.item, self.text) } { + let data = iter.to_data(); + self.iter = Some(iter); + Some(data) + } else { + None + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 4bf8121..40c2477 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,7 @@ pub mod attr_list; pub mod attribute; pub mod font_description; mod functions; +mod glyph_item_iter; pub mod gravity; pub mod item; pub mod language;