[units] Add support for centimeters

The only tricky part of the patch is to remember that 1cm is 10mm.

Reviewed-by: Emmanuele Bassi <ebassi@linux.intel.com>
This commit is contained in:
Damien Lespiau 2009-10-06 17:47:34 +01:00
parent 96859959bd
commit 8605073edb
4 changed files with 67 additions and 6 deletions

View File

@ -90,6 +90,12 @@ units_mm_to_pixels (gfloat mm)
return mm * dpi / 25.4; return mm * dpi / 25.4;
} }
static gfloat
units_cm_to_pixels (gfloat cm)
{
return units_mm_to_pixels (cm * 10);
}
static gfloat static gfloat
units_pt_to_pixels (gfloat pt) units_pt_to_pixels (gfloat pt)
{ {
@ -152,6 +158,27 @@ clutter_units_from_mm (ClutterUnits *units,
units->pixels_set = TRUE; 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: * clutter_units_from_pt:
* @units: a #ClutterUnits * @units: a #ClutterUnits
@ -334,6 +361,10 @@ clutter_units_to_pixels (ClutterUnits *units)
units->pixels = units_mm_to_pixels (units->value); units->pixels = units_mm_to_pixels (units->value);
break; break;
case CLUTTER_UNIT_CM:
units->pixels = units_cm_to_pixels (units->value);
break;
case CLUTTER_UNIT_POINT: case CLUTTER_UNIT_POINT:
units->pixels = units_pt_to_pixels (units->value); units->pixels = units_pt_to_pixels (units->value);
break; break;
@ -364,7 +395,7 @@ clutter_units_to_pixels (ClutterUnits *units)
* |[ * |[
* units: wsp* unit-value wsp* unit-name? wsp* * units: wsp* unit-value wsp* unit-name? wsp*
* unit-value: number * unit-value: number
* unit-name: 'px' | 'pt' | 'mm' | 'em' * unit-name: 'px' | 'pt' | 'mm' | 'em' | 'cm'
* number: digit+ * number: digit+
* | digit* sep digit+ * | digit* sep digit+
* sep: '.' | ',' * sep: '.' | ','
@ -448,6 +479,11 @@ clutter_units_from_string (ClutterUnits *units,
unit_type = CLUTTER_UNIT_MM; unit_type = CLUTTER_UNIT_MM;
str += 2; str += 2;
} }
else if (strncmp (str, "cm", 2) == 0)
{
unit_type = CLUTTER_UNIT_CM;
str += 2;
}
else if (strncmp (str, "pt", 2) == 0) else if (strncmp (str, "pt", 2) == 0)
{ {
unit_type = CLUTTER_UNIT_POINT; unit_type = CLUTTER_UNIT_POINT;
@ -482,6 +518,9 @@ clutter_unit_type_name (ClutterUnitType unit_type)
case CLUTTER_UNIT_MM: case CLUTTER_UNIT_MM:
return "mm"; return "mm";
case CLUTTER_UNIT_CM:
return "cm";
case CLUTTER_UNIT_POINT: case CLUTTER_UNIT_POINT:
return "pt"; return "pt";
@ -507,7 +546,7 @@ clutter_unit_type_name (ClutterUnitType unit_type)
* examples of output * examples of output
* *
* <note>Fractional values are truncated to the second decimal * <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> * typographic points. Pixels are integers.</note>
* *
* Return value: a newly allocated string containing the encoded * Return value: a newly allocated string containing the encoded
@ -537,6 +576,11 @@ clutter_units_to_string (const ClutterUnits *units)
fmt = "%.2f"; fmt = "%.2f";
break; break;
case CLUTTER_UNIT_CM:
unit_name = "cm";
fmt = "%.2f";
break;
case CLUTTER_UNIT_POINT: case CLUTTER_UNIT_POINT:
unit_name = "pt"; unit_name = "pt";
fmt = "%.1f"; fmt = "%.1f";

View File

@ -43,6 +43,7 @@ G_BEGIN_DECLS
* @CLUTTER_UNIT_EM: Unit expressed in em * @CLUTTER_UNIT_EM: Unit expressed in em
* @CLUTTER_UNIT_MM: Unit expressed in millimeters * @CLUTTER_UNIT_MM: Unit expressed in millimeters
* @CLUTTER_UNIT_POINT: Unit expressed in points * @CLUTTER_UNIT_POINT: Unit expressed in points
* @CLUTTER_UNIT_CM: Unit expressed in centimeters
* *
* The type of unit in which a value is expressed * The type of unit in which a value is expressed
* *
@ -54,7 +55,8 @@ typedef enum {
CLUTTER_UNIT_PIXEL, CLUTTER_UNIT_PIXEL,
CLUTTER_UNIT_EM, CLUTTER_UNIT_EM,
CLUTTER_UNIT_MM, CLUTTER_UNIT_MM,
CLUTTER_UNIT_POINT CLUTTER_UNIT_POINT,
CLUTTER_UNIT_CM
} ClutterUnitType; } ClutterUnitType;
/** /**
@ -99,6 +101,8 @@ void clutter_units_from_em_for_font (ClutterUnits *units,
gfloat em); gfloat em);
void clutter_units_from_mm (ClutterUnits *units, void clutter_units_from_mm (ClutterUnits *units,
gfloat mm); gfloat mm);
void clutter_units_from_cm (ClutterUnits *units,
gfloat cm);
void clutter_units_from_pt (ClutterUnits *units, void clutter_units_from_pt (ClutterUnits *units,
gfloat pt); 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 clutter_units_from_em
#define clutter_units_em_for_font clutter_units_from_em_for_font #define clutter_units_em_for_font clutter_units_from_em_for_font
#define clutter_units_mm clutter_units_from_mm #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_units_pt clutter_units_from_pt
#define CLUTTER_TYPE_UNITS (clutter_units_get_type ()) #define CLUTTER_TYPE_UNITS (clutter_units_get_type ())

View File

@ -30,6 +30,7 @@ clutter_media_get_type
ClutterUnitType ClutterUnitType
ClutterUnits ClutterUnits
clutter_units_from_mm clutter_units_from_mm
clutter_units_from_cm
clutter_units_from_pt clutter_units_from_pt
clutter_units_from_em clutter_units_from_em
clutter_units_from_em_for_font clutter_units_from_em_for_font

View File

@ -7,7 +7,7 @@ void
test_units_constructors (TestConformSimpleFixture *fixture, test_units_constructors (TestConformSimpleFixture *fixture,
gconstpointer data) gconstpointer data)
{ {
ClutterUnits units; ClutterUnits units, units_cm;
clutter_units_from_pixels (&units, 100); clutter_units_from_pixels (&units, 100);
g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_PIXEL); 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 (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_get_unit_value (&units), ==, 5.0);
g_assert_cmpfloat (clutter_units_to_pixels (&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 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, " 32 em garbage") == FALSE);
g_assert (clutter_units_from_string (&units, "5.1mm") == TRUE); g_assert (clutter_units_from_string (&units, "5.1cm") == TRUE);
g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_MM); g_assert (clutter_units_get_unit_type (&units) == CLUTTER_UNIT_CM);
g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.1f); g_assert_cmpfloat (clutter_units_get_unit_value (&units), ==, 5.1f);
g_assert (clutter_units_from_string (&units, "5,mm") == FALSE); g_assert (clutter_units_from_string (&units, "5,mm") == FALSE);