20
20
* Lesser General Public License for more details.
22
22
* You should have received a copy of the GNU Lesser General Public
23
* License along with this library; if not, write to the
24
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25
* Boston, MA 02111-1307, USA.
23
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
144
151
clutter_units_from_mm (ClutterUnits *units,
154
ClutterBackend *backend;
147
156
g_return_if_fail (units != NULL);
158
backend = clutter_get_default_backend ();
149
160
units->unit_type = CLUTTER_UNIT_MM;
150
161
units->value = mm;
151
162
units->pixels = units_mm_to_pixels (mm);
152
163
units->pixels_set = TRUE;
164
units->serial = _clutter_backend_get_units_serial (backend);
168
* clutter_units_from_cm:
169
* @units: a #ClutterUnits
172
* Stores a value in centimeters inside @units
177
clutter_units_from_cm (ClutterUnits *units,
180
ClutterBackend *backend;
182
g_return_if_fail (units != NULL);
184
backend = clutter_get_default_backend ();
186
units->unit_type = CLUTTER_UNIT_CM;
188
units->pixels = units_cm_to_pixels (cm);
189
units->pixels_set = TRUE;
190
units->serial = _clutter_backend_get_units_serial (backend);
187
230
clutter_units_from_em (ClutterUnits *units,
233
ClutterBackend *backend;
190
235
g_return_if_fail (units != NULL);
237
backend = clutter_get_default_backend ();
192
239
units->unit_type = CLUTTER_UNIT_EM;
193
240
units->value = em;
194
241
units->pixels = units_em_to_pixels (NULL, em);
195
242
units->pixels_set = TRUE;
243
units->serial = _clutter_backend_get_units_serial (backend);
327
385
clutter_units_to_pixels (ClutterUnits *units)
387
ClutterBackend *backend;
329
389
g_return_val_if_fail (units != NULL, 0.0);
391
/* if the backend settings changed we evict the cached value */
392
backend = clutter_get_default_backend ();
393
if (units->serial != _clutter_backend_get_units_serial (backend))
394
units->pixels_set = FALSE;
396
if (units->pixels_set)
397
return units->pixels;
331
399
switch (units->unit_type)
333
401
case CLUTTER_UNIT_MM:
334
402
units->pixels = units_mm_to_pixels (units->value);
405
case CLUTTER_UNIT_CM:
406
units->pixels = units_cm_to_pixels (units->value);
337
409
case CLUTTER_UNIT_POINT:
338
410
units->pixels = units_pt_to_pixels (units->value);
362
435
* A #ClutterUnits expressed in string should match:
366
* unit_value: <number>+
367
* unit_name: px|pt|mm|em
368
* units: <unit_value> <unit_name>
438
* units: wsp* unit-value wsp* unit-name? wsp*
440
* unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
442
* | digit* sep digit+
444
* digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
445
* wsp: (#0x20 | #0x9 | #0xA | #0xB | #0xC | #0xD)+
371
448
* For instance, these are valid strings:
395
473
clutter_units_from_string (ClutterUnits *units,
396
474
const gchar *str)
476
ClutterBackend *backend;
398
477
ClutterUnitType unit_type;
401
480
g_return_val_if_fail (units != NULL, FALSE);
402
481
g_return_val_if_fail (str != NULL, FALSE);
404
/* Ensure that the first character is a digit */
483
/* strip leading space */
405
484
while (g_ascii_isspace (*str))
408
487
if (*str == '\0')
411
if (!g_ascii_isdigit (*str))
414
490
/* integer part */
415
491
value = (gfloat) strtoul (str, (char **) &str, 10);
417
493
if (*str == '.' || *str == ',')
421
while (g_ascii_isdigit (*++str))
495
gfloat divisor = 0.1;
497
/* 5.cm is not a valid number */
498
if (!g_ascii_isdigit (*++str))
501
while (g_ascii_isdigit (*str))
423
frac += (*str - '0') * frac;
503
value += (*str - '0') * divisor;
427
value += (1.0f / (gfloat) frac);
509
while (g_ascii_isspace (*str))
430
512
/* assume pixels by default, if no unit is specified */
432
514
unit_type = CLUTTER_UNIT_PIXEL;
515
else if (strncmp (str, "em", 2) == 0)
517
unit_type = CLUTTER_UNIT_EM;
520
else if (strncmp (str, "mm", 2) == 0)
522
unit_type = CLUTTER_UNIT_MM;
525
else if (strncmp (str, "cm", 2) == 0)
527
unit_type = CLUTTER_UNIT_CM;
530
else if (strncmp (str, "pt", 2) == 0)
532
unit_type = CLUTTER_UNIT_POINT;
535
else if (strncmp (str, "px", 2) == 0)
537
unit_type = CLUTTER_UNIT_PIXEL;
435
while (g_ascii_isspace (*str))
438
if (strncmp (str, "em", 2) == 0)
439
unit_type = CLUTTER_UNIT_EM;
440
else if (strncmp (str, "mm", 2) == 0)
441
unit_type = CLUTTER_UNIT_MM;
442
else if (strncmp (str, "pt", 2) == 0)
443
unit_type = CLUTTER_UNIT_POINT;
444
else if (strncmp (str, "px", 2) == 0)
445
unit_type = CLUTTER_UNIT_PIXEL;
543
/* ensure the unit is only followed by white space */
544
while (g_ascii_isspace (*str))
549
backend = clutter_get_default_backend ();
450
551
units->unit_type = unit_type;
451
552
units->value = value;
452
553
units->pixels_set = FALSE;
554
units->serial = _clutter_backend_get_units_serial (backend);
651
* ClutterInterval integration
655
clutter_units_progress (const GValue *a,
660
ClutterUnits *a_units = (ClutterUnits *) clutter_value_get_units (a);
661
ClutterUnits *b_units = (ClutterUnits *) clutter_value_get_units (b);
663
gfloat a_px, b_px, value;
665
a_px = clutter_units_to_pixels (a_units);
666
b_px = clutter_units_to_pixels (b_units);
667
value = progress * (b_px - a_px) + a_px;
669
clutter_units_from_pixels (&res, value);
670
clutter_value_set_units (retval, &res);
541
676
* GValue and GParamSpec integration