From 8605073edb7a1e830696632a6de5ad694f1af98d Mon Sep 17 00:00:00 2001 From: Damien Lespiau Date: Tue, 6 Oct 2009 17:47:34 +0100 Subject: [PATCH] [units] Add support for centimeters The only tricky part of the patch is to remember that 1cm is 10mm. Reviewed-by: Emmanuele Bassi --- clutter/clutter-units.c | 48 +++++++++++++++++++++- clutter/clutter-units.h | 7 +++- doc/reference/clutter/clutter-sections.txt | 1 + tests/conform/test-clutter-units.c | 17 ++++++-- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/clutter/clutter-units.c b/clutter/clutter-units.c index 069bed0bc..595f0a50f 100644 --- a/clutter/clutter-units.c +++ b/clutter/clutter-units.c @@ -90,6 +90,12 @@ units_mm_to_pixels (gfloat mm) return mm * dpi / 25.4; } +static gfloat +units_cm_to_pixels (gfloat cm) +{ + return units_mm_to_pixels (cm * 10); +} + static gfloat units_pt_to_pixels (gfloat pt) { @@ -152,6 +158,27 @@ clutter_units_from_mm (ClutterUnits *units, units->pixels_set = TRUE; } +/** + * 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 @@ -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 * * 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. * * 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"; diff --git a/clutter/clutter-units.h b/clutter/clutter-units.h index 4a407787f..e67af95ae 100644 --- a/clutter/clutter-units.h +++ b/clutter/clutter-units.h @@ -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 ()) diff --git a/doc/reference/clutter/clutter-sections.txt b/doc/reference/clutter/clutter-sections.txt index 30fd665cf..21b971b7a 100644 --- a/doc/reference/clutter/clutter-sections.txt +++ b/doc/reference/clutter/clutter-sections.txt @@ -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 diff --git a/tests/conform/test-clutter-units.c b/tests/conform/test-clutter-units.c index 3506eaf10..e6fbe44a3 100644 --- a/tests/conform/test-clutter-units.c +++ b/tests/conform/test-clutter-units.c @@ -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);