[clutter/clutter-1.16] units: Handle negative values in clutter_units_from_string()



commit 44f283bb72a3c1236629f7fe36ed5dc75bbb07e7
Author: Bastian Winkler <buz netbuz org>
Date:   Wed May 22 15:10:28 2013 +0200

    units: Handle negative values in clutter_units_from_string()
    
    In order to allow values like "-2cm" in ClutterScript,
    clutter_units_from_string() needs to handle negative values as well.

 clutter/clutter-units.c |   13 +++++++++++++
 tests/conform/units.c   |    4 ++++
 2 files changed, 17 insertions(+), 0 deletions(-)
---
diff --git a/clutter/clutter-units.c b/clutter/clutter-units.c
index f07f0f6..8a43cfe 100644
--- a/clutter/clutter-units.c
+++ b/clutter/clutter-units.c
@@ -480,11 +480,21 @@ clutter_units_from_string (ClutterUnits *units,
   ClutterBackend *backend;
   ClutterUnitType unit_type;
   gfloat value;
+  gboolean negative = FALSE;
 
   g_return_val_if_fail (units != NULL, FALSE);
   g_return_val_if_fail (str != NULL, FALSE);
 
   /* strip leading space */
+  while (g_ascii_isspace (*str) || *str == '+')
+    str++;
+
+  if (*str == '-')
+    {
+      negative = TRUE;
+      str++;
+    }
+
   while (g_ascii_isspace (*str))
     str++;
 
@@ -550,6 +560,9 @@ clutter_units_from_string (ClutterUnits *units,
   if (*str != '\0')
     return FALSE;
 
+  if (negative)
+    value *= -1;
+
   backend = clutter_get_default_backend ();
 
   units->unit_type = unit_type;
diff --git a/tests/conform/units.c b/tests/conform/units.c
index 3822e05..0dcc355 100644
--- a/tests/conform/units.c
+++ b/tests/conform/units.c
@@ -108,6 +108,10 @@ units_string (TestConformSimpleFixture *fixture,
   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_POINT);
   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 0.5f);
 
+  g_assert (clutter_units_from_string (&units, "-3 px") == TRUE);
+  g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
+  g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, -3.0);
+
   g_assert (clutter_units_from_string (&units, "1 omg!!pony") == FALSE);
 
   clutter_units_from_pt (&units, 24.0);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]