[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:
parent
96859959bd
commit
8605073edb
4 changed files with 67 additions and 6 deletions
|
@ -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
|
||||
*
|
||||
* <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";
|
||||
|
|
|
@ -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 ())
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue