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
use super::UnitType;
use glib::{translate::*, GString};
use std::{fmt, mem};

glib_wrapper! {
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct Units(Boxed<ffi::ClutterUnits>);

    match fn {
        copy => |ptr| ffi::clutter_units_copy(mut_override(ptr)),
        free => |ptr| ffi::clutter_units_free(ptr),
        get_type => || ffi::clutter_units_get_type(),
    }
}

impl Units {
    /// Retrieves the unit type of the value stored inside `self`
    ///
    /// # Returns
    ///
    /// a unit type
    pub fn get_unit_type(&self) -> UnitType {
        unsafe { from_glib(ffi::clutter_units_get_unit_type(self.to_glib_none().0)) }
    }

    /// Retrieves the value stored inside `self`
    ///
    /// # Returns
    ///
    /// the value stored inside a `Units`
    pub fn get_unit_value(&self) -> f32 {
        unsafe { ffi::clutter_units_get_unit_value(self.to_glib_none().0) }
    }

    /// Converts a value in `Units` to pixels
    ///
    /// # Returns
    ///
    /// the value in pixels
    pub fn to_pixels(&mut self) -> f32 {
        unsafe { ffi::clutter_units_to_pixels(self.to_glib_none_mut().0) }
    }

    /// Converts `self` into a string
    ///
    /// See `Units::from_string` for the units syntax and for
    /// examples of output
    ///
    /// Fractional values are truncated to the second decimal
    /// position for em, mm and cm, and to the first decimal position for
    /// typographic points. Pixels are integers.
    ///
    /// # Returns
    ///
    /// a newly allocated string containing the encoded
    ///  `Units` value. Use `g_free` to free the string
    fn to_string(&self) -> GString {
        unsafe { from_glib_full(ffi::clutter_units_to_string(self.to_glib_none().0)) }
    }

    /// Stores a value in centimeters inside `units`
    /// ## `units`
    /// a `Units`
    /// ## `cm`
    /// centimeters
    pub fn from_cm(cm: f32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_cm(units.to_glib_none_mut().0, cm);
            units
        }
    }

    /// Stores a value in em inside `units`, using the default font
    /// name as returned by `Backend::get_font_name`
    /// ## `units`
    /// a `Units`
    /// ## `em`
    /// em
    pub fn from_em(em: f32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_em(units.to_glib_none_mut().0, em);
            units
        }
    }

    /// Stores a value in em inside `units` using `font_name`
    /// ## `units`
    /// a `Units`
    /// ## `font_name`
    /// the font name and size
    /// ## `em`
    /// em
    pub fn from_em_for_font(font_name: Option<&str>, em: f32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_em_for_font(
                units.to_glib_none_mut().0,
                font_name.to_glib_none().0,
                em,
            );
            units
        }
    }

    /// Stores a value in millimiters inside `units`
    /// ## `units`
    /// a `Units`
    /// ## `mm`
    /// millimeters
    pub fn from_mm(mm: f32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_mm(units.to_glib_none_mut().0, mm);
            units
        }
    }

    /// Stores a value in pixels inside `units`
    /// ## `units`
    /// a `Units`
    /// ## `px`
    /// pixels
    pub fn from_pixels(px: i32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_pixels(units.to_glib_none_mut().0, px);
            units
        }
    }

    /// Stores a value in typographic points inside `units`
    /// ## `units`
    /// a `Units`
    /// ## `pt`
    /// typographic points
    pub fn from_pt(pt: f32) -> Units {
        unsafe {
            let mut units = Units::uninitialized();
            ffi::clutter_units_from_pt(units.to_glib_none_mut().0, pt);
            units
        }
    }

    /// Parses a value and updates `units` with it
    ///
    /// A `Units` expressed in string should match:
    ///
    ///
    /// ```text
    ///   units: wsp* unit-value wsp* unit-name? wsp*
    ///   unit-value: number
    ///   unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
    ///   number: digit+
    ///           | digit* sep digit+
    ///   sep: '.' | ','
    ///   digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
    ///   wsp: (#0x20 | #0x9 | #0xA | #0xB | #0xC | #0xD)+
    /// ```
    ///
    /// For instance, these are valid strings:
    ///
    ///
    /// ```text
    ///   10 px
    ///   5.1 em
    ///   24 pt
    ///   12.6 mm
    ///   .3 cm
    /// ```
    ///
    /// While these are not:
    ///
    ///
    /// ```text
    ///   42 cats
    ///   omg!1!ponies
    /// ```
    ///
    /// If no unit is specified, pixels are assumed.
    /// ## `units`
    /// a `Units`
    /// ## `str`
    /// the string to convert
    ///
    /// # Returns
    ///
    /// `true` if the string was successfully parsed,
    ///  and `false` otherwise
    pub fn from_string(str: &str) -> Option<Units> {
        unsafe {
            let mut units = Units::uninitialized();
            let ret = from_glib(ffi::clutter_units_from_string(
                units.to_glib_none_mut().0,
                str.to_glib_none().0,
            ));
            if ret {
                Some(units)
            } else {
                None
            }
        }
    }
}

#[doc(hidden)]
impl Uninitialized for Units {
    #[inline]
    unsafe fn uninitialized() -> Self {
        mem::zeroed()
    }
}

impl fmt::Display for Units {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_string())
    }
}