animate/legacy/units.rs
1use super::UnitType;
2use glib::{translate::*, GString};
3use std::{fmt, mem};
4
5glib_wrapper! {
6 #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7 pub struct Units(Boxed<ffi::ClutterUnits>);
8
9 match fn {
10 copy => |ptr| ffi::clutter_units_copy(mut_override(ptr)),
11 free => |ptr| ffi::clutter_units_free(ptr),
12 get_type => || ffi::clutter_units_get_type(),
13 }
14}
15
16impl Units {
17 /// Retrieves the unit type of the value stored inside `self`
18 ///
19 /// # Returns
20 ///
21 /// a unit type
22 pub fn get_unit_type(&self) -> UnitType {
23 unsafe { from_glib(ffi::clutter_units_get_unit_type(self.to_glib_none().0)) }
24 }
25
26 /// Retrieves the value stored inside `self`
27 ///
28 /// # Returns
29 ///
30 /// the value stored inside a `Units`
31 pub fn get_unit_value(&self) -> f32 {
32 unsafe { ffi::clutter_units_get_unit_value(self.to_glib_none().0) }
33 }
34
35 /// Converts a value in `Units` to pixels
36 ///
37 /// # Returns
38 ///
39 /// the value in pixels
40 pub fn to_pixels(&mut self) -> f32 {
41 unsafe { ffi::clutter_units_to_pixels(self.to_glib_none_mut().0) }
42 }
43
44 /// Converts `self` into a string
45 ///
46 /// See `Units::from_string` for the units syntax and for
47 /// examples of output
48 ///
49 /// Fractional values are truncated to the second decimal
50 /// position for em, mm and cm, and to the first decimal position for
51 /// typographic points. Pixels are integers.
52 ///
53 /// # Returns
54 ///
55 /// a newly allocated string containing the encoded
56 /// `Units` value. Use `g_free` to free the string
57 fn to_string(&self) -> GString {
58 unsafe { from_glib_full(ffi::clutter_units_to_string(self.to_glib_none().0)) }
59 }
60
61 /// Stores a value in centimeters inside `units`
62 /// ## `units`
63 /// a `Units`
64 /// ## `cm`
65 /// centimeters
66 pub fn from_cm(cm: f32) -> Units {
67 unsafe {
68 let mut units = Units::uninitialized();
69 ffi::clutter_units_from_cm(units.to_glib_none_mut().0, cm);
70 units
71 }
72 }
73
74 /// Stores a value in em inside `units`, using the default font
75 /// name as returned by `Backend::get_font_name`
76 /// ## `units`
77 /// a `Units`
78 /// ## `em`
79 /// em
80 pub fn from_em(em: f32) -> Units {
81 unsafe {
82 let mut units = Units::uninitialized();
83 ffi::clutter_units_from_em(units.to_glib_none_mut().0, em);
84 units
85 }
86 }
87
88 /// Stores a value in em inside `units` using `font_name`
89 /// ## `units`
90 /// a `Units`
91 /// ## `font_name`
92 /// the font name and size
93 /// ## `em`
94 /// em
95 pub fn from_em_for_font(font_name: Option<&str>, em: f32) -> Units {
96 unsafe {
97 let mut units = Units::uninitialized();
98 ffi::clutter_units_from_em_for_font(
99 units.to_glib_none_mut().0,
100 font_name.to_glib_none().0,
101 em,
102 );
103 units
104 }
105 }
106
107 /// Stores a value in millimiters inside `units`
108 /// ## `units`
109 /// a `Units`
110 /// ## `mm`
111 /// millimeters
112 pub fn from_mm(mm: f32) -> Units {
113 unsafe {
114 let mut units = Units::uninitialized();
115 ffi::clutter_units_from_mm(units.to_glib_none_mut().0, mm);
116 units
117 }
118 }
119
120 /// Stores a value in pixels inside `units`
121 /// ## `units`
122 /// a `Units`
123 /// ## `px`
124 /// pixels
125 pub fn from_pixels(px: i32) -> Units {
126 unsafe {
127 let mut units = Units::uninitialized();
128 ffi::clutter_units_from_pixels(units.to_glib_none_mut().0, px);
129 units
130 }
131 }
132
133 /// Stores a value in typographic points inside `units`
134 /// ## `units`
135 /// a `Units`
136 /// ## `pt`
137 /// typographic points
138 pub fn from_pt(pt: f32) -> Units {
139 unsafe {
140 let mut units = Units::uninitialized();
141 ffi::clutter_units_from_pt(units.to_glib_none_mut().0, pt);
142 units
143 }
144 }
145
146 /// Parses a value and updates `units` with it
147 ///
148 /// A `Units` expressed in string should match:
149 ///
150 ///
151 /// ```text
152 /// units: wsp* unit-value wsp* unit-name? wsp*
153 /// unit-value: number
154 /// unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
155 /// number: digit+
156 /// | digit* sep digit+
157 /// sep: '.' | ','
158 /// digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
159 /// wsp: (#0x20 | #0x9 | #0xA | #0xB | #0xC | #0xD)+
160 /// ```
161 ///
162 /// For instance, these are valid strings:
163 ///
164 ///
165 /// ```text
166 /// 10 px
167 /// 5.1 em
168 /// 24 pt
169 /// 12.6 mm
170 /// .3 cm
171 /// ```
172 ///
173 /// While these are not:
174 ///
175 ///
176 /// ```text
177 /// 42 cats
178 /// omg!1!ponies
179 /// ```
180 ///
181 /// If no unit is specified, pixels are assumed.
182 /// ## `units`
183 /// a `Units`
184 /// ## `str`
185 /// the string to convert
186 ///
187 /// # Returns
188 ///
189 /// `true` if the string was successfully parsed,
190 /// and `false` otherwise
191 pub fn from_string(str: &str) -> Option<Units> {
192 unsafe {
193 let mut units = Units::uninitialized();
194 let ret = from_glib(ffi::clutter_units_from_string(
195 units.to_glib_none_mut().0,
196 str.to_glib_none().0,
197 ));
198 if ret {
199 Some(units)
200 } else {
201 None
202 }
203 }
204 }
205}
206
207#[doc(hidden)]
208impl Uninitialized for Units {
209 #[inline]
210 unsafe fn uninitialized() -> Self {
211 mem::zeroed()
212 }
213}
214
215impl fmt::Display for Units {
216 #[inline]
217 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218 write!(f, "{}", self.to_string())
219 }
220}