[units] Add support for centimeters
authorDamien Lespiau <damien.lespiau@intel.com>
Tue, 6 Oct 2009 16:47:34 +0000 (17:47 +0100)
committerDamien Lespiau <damien.lespiau@intel.com>
Wed, 7 Oct 2009 15:06:26 +0000 (16:06 +0100)
The only tricky part of the patch is to remember that 1cm is 10mm.

Reviewed-by: Emmanuele Bassi <ebassi@linux.intel.com>
clutter/clutter-units.c
clutter/clutter-units.h
doc/reference/clutter/clutter-sections.txt
tests/conform/test-clutter-units.c

index 069bed0..595f0a5 100644 (file)
@@ -91,6 +91,12 @@ units_mm_to_pixels (gfloat mm)
 }
 
 static gfloat
+units_cm_to_pixels (gfloat cm)
+{
+  return units_mm_to_pixels (cm * 10);
+}
+
+static gfloat
 units_pt_to_pixels (gfloat pt)
 {
   ClutterBackend *backend;
@@ -153,6 +159,27 @@ clutter_units_from_mm (ClutterUnits *units,
 }
 
 /**
+ * clutter_units_from_cm:
+ * @units: a #ClutterUnits
+ * @cm: centimeters
+ *
+ * Stores a value in centimeters inside @units
+ *
+ * Since: 1.2
+ */
+void
+clutter_units_from_cm (ClutterUnits *units,
+                       gfloat        cm)
+{
+  g_return_if_fail (units != NULL);
+
+  units->unit_type  = CLUTTER_UNIT_CM;
+  units->value      = cm;
+  units->pixels     = units_cm_to_pixels (cm);
+  units->pixels_set = TRUE;
+}
+
+/**
  * clutter_units_from_pt:
  * @units: a #ClutterUnits
  * @pt: typographic points
@@ -334,6 +361,10 @@ clutter_units_to_pixels (ClutterUnits *units)
       units->pixels = units_mm_to_pixels (units->value);
       break;
 
+    case CLUTTER_UNIT_CM:
+      units->pixels = units_cm_to_pixels (units->value);
+      break;
+
     case CLUTTER_UNIT_POINT:
       units->pixels = units_pt_to_pixels (units->value);
       break;
@@ -364,7 +395,7 @@ clutter_units_to_pixels (ClutterUnits *units)
  * |[
  *   units: wsp* unit-value wsp* unit-name? wsp*
  *   unit-value: number
- *   unit-name: 'px' | 'pt' | 'mm' | 'em'
+ *   unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
  *   number: digit+
  *           | digit* sep digit+
  *   sep: '.' | ','
@@ -448,6 +479,11 @@ clutter_units_from_string (ClutterUnits *units,
       unit_type = CLUTTER_UNIT_MM;
       str += 2;
     }
+  else if (strncmp (str, "cm", 2) == 0)
+    {
+      unit_type = CLUTTER_UNIT_CM;
+      str += 2;
+    }
   else if (strncmp (str, "pt", 2) == 0)
     {
       unit_type = CLUTTER_UNIT_POINT;
@@ -482,6 +518,9 @@ clutter_unit_type_name (ClutterUnitType unit_type)
     case CLUTTER_UNIT_MM:
       return "mm";
 
+    case CLUTTER_UNIT_CM:
+      return "cm";
+
     case CLUTTER_UNIT_POINT:
       return "pt";
 
@@ -507,7 +546,7 @@ clutter_unit_type_name (ClutterUnitType unit_type)
  * examples of output
  *
  * <note>Fractional values are truncated to the second decimal
- * position for em and mm, and to the first decimal position for
+ * position for em, mm and cm, and to the first decimal position for
  * typographic points. Pixels are integers.</note>
  *
  * Return value: a newly allocated string containing the encoded
@@ -537,6 +576,11 @@ clutter_units_to_string (const ClutterUnits *units)
       fmt = "%.2f";
       break;
 
+    case CLUTTER_UNIT_CM:
+      unit_name = "cm";
+      fmt = "%.2f";
+      break;
+
     case CLUTTER_UNIT_POINT:
       unit_name = "pt";
       fmt = "%.1f";
index 4a40778..e67af95 100644 (file)
@@ -43,6 +43,7 @@ G_BEGIN_DECLS
  * @CLUTTER_UNIT_EM: Unit expressed in em
  * @CLUTTER_UNIT_MM: Unit expressed in millimeters
  * @CLUTTER_UNIT_POINT: Unit expressed in points
+ * @CLUTTER_UNIT_CM: Unit expressed in centimeters
  *
  * The type of unit in which a value is expressed
  *
@@ -54,7 +55,8 @@ typedef enum {
   CLUTTER_UNIT_PIXEL,
   CLUTTER_UNIT_EM,
   CLUTTER_UNIT_MM,
-  CLUTTER_UNIT_POINT
+  CLUTTER_UNIT_POINT,
+  CLUTTER_UNIT_CM
 } ClutterUnitType;
 
 /**
@@ -99,6 +101,8 @@ void            clutter_units_from_em_for_font (ClutterUnits       *units,
                                                 gfloat              em);
 void            clutter_units_from_mm          (ClutterUnits       *units,
                                                 gfloat              mm);
+void            clutter_units_from_cm          (ClutterUnits       *units,
+                                                gfloat              cm);
 void            clutter_units_from_pt          (ClutterUnits       *units,
                                                 gfloat              pt);
 
@@ -113,6 +117,7 @@ gchar *         clutter_units_to_string        (const ClutterUnits *units);
 #define clutter_units_em                clutter_units_from_em
 #define clutter_units_em_for_font       clutter_units_from_em_for_font
 #define clutter_units_mm                clutter_units_from_mm
+#define clutter_units_cm                clutter_units_from_cm
 #define clutter_units_pt                clutter_units_from_pt
 
 #define CLUTTER_TYPE_UNITS                 (clutter_units_get_type ())
index 30fd665..21b971b 100644 (file)
@@ -30,6 +30,7 @@ clutter_media_get_type
 ClutterUnitType
 ClutterUnits
 clutter_units_from_mm
+clutter_units_from_cm
 clutter_units_from_pt
 clutter_units_from_em
 clutter_units_from_em_for_font
index 3506eaf..e6fbe44 100644 (file)
@@ -7,7 +7,7 @@ void
 test_units_constructors (TestConformSimpleFixture *fixture,
                          gconstpointer data)
 {
-  ClutterUnits units;
+  ClutterUnits units, units_cm;
 
   clutter_units_from_pixels (&units, 100);
   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL);
@@ -18,6 +18,17 @@ test_units_constructors (TestConformSimpleFixture *fixture,
   g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_EM);
   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.0);
   g_assert_cmpfloat (clutter_units_to_pixels (&units), !=, 5.0);
+
+  clutter_units_from_cm (&units_cm, 5.0);
+  g_assert (clutter_units_get_unit_type (&units_cm) == CLUTTER_UNIT_CM);
+  g_assert_cmpfloat (clutter_units_get_unit_value (&units_cm), ==, 5.0);
+  g_assert_cmpfloat (clutter_units_to_pixels (&units_cm), !=, 5.0);
+
+  clutter_units_from_mm (&units, 50.0);
+  g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM);
+  g_assert_cmpfloat (clutter_units_to_pixels (&units),
+                     ==,
+                     clutter_units_to_pixels (&units_cm));
 }
 
 void
@@ -51,8 +62,8 @@ test_units_string (TestConformSimpleFixture *fixture,
 
   g_assert (clutter_units_from_string (&units, "  32   em   garbage") == FALSE);
 
-  g_assert (clutter_units_from_string (&units, "5.1mm") == TRUE);
-  g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM);
+  g_assert (clutter_units_from_string (&units, "5.1cm") == TRUE);
+  g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_CM);
   g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.1f);
 
   g_assert (clutter_units_from_string (&units, "5,mm") == FALSE);