From ee883f30d47992e5ad87d0d70d2f97f79442e8ad Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Wed, 21 Jan 2009 17:35:47 +0000 Subject: [PATCH] Add units-from-em conversion An em is a unit of measurement in typography, equal to the point size of the current font. It should be possible to convert a value expressed in em to ClutterUnits by using the current font and the current DPI as stored by the default backend. --- clutter/clutter-units.c | 50 ++++++++++++++++++++++ clutter/clutter-units.h | 11 +++++ doc/reference/clutter/clutter-sections.txt | 4 ++ 3 files changed, 65 insertions(+) diff --git a/clutter/clutter-units.c b/clutter/clutter-units.c index cdcb8390b..88b2e7fe6 100644 --- a/clutter/clutter-units.c +++ b/clutter/clutter-units.c @@ -153,6 +153,56 @@ clutter_units_pt (gdouble pt) return pt * dpi / 72.0; } +/** + * clutter_units_em: + * @em: em to convert + * + * Converts a value in em to #ClutterUnits at the + * current DPI. + * + * Return value: the value in units + * + * Since: 1.0 + */ +ClutterUnit +clutter_units_em (gdouble em) +{ + ClutterBackend *backend; + const gchar *font_name; + gdouble dpi; + ClutterUnit retval = 0; + + backend = clutter_get_default_backend (); + + dpi = clutter_backend_get_resolution (backend); + font_name = clutter_backend_get_font_name (backend); + if (G_LIKELY ((font_name && *font_name != '\0'))) + { + PangoFontDescription *font_desc; + gdouble font_size = 0; + + font_desc = pango_font_description_from_string (font_name); + if (G_LIKELY (font_desc != NULL)) + { + gint pango_size; + gboolean is_absolute; + + pango_size = pango_font_description_get_size (font_desc); + is_absolute = + pango_font_description_get_size_is_absolute (font_desc); + if (!is_absolute) + font_size = ((gdouble) font_size) / PANGO_SCALE; + + pango_font_description_free (font_desc); + } + + /* 10 points at 96 DPI is 12 pixels */ + retval = 1.2 * font_size * dpi / 96.0; + } + + return retval; +} + static GTypeInfo _info = { 0, NULL, diff --git a/clutter/clutter-units.h b/clutter/clutter-units.h index 8eb24d102..efc622e1b 100644 --- a/clutter/clutter-units.h +++ b/clutter/clutter-units.h @@ -131,8 +131,19 @@ typedef float ClutterUnit; */ #define CLUTTER_UNITS_FROM_POINTS(x) (clutter_units_pt (x)) +/** + * CLUTTER_UNITS_FROM_EM: + * @x: a value in em + * + * Converts a value in em into #ClutterUnits + * + * Since: 1.0 + */ +#define CLUTTER_UNITS_FROM_EM(x) (clutter_units_em (x)) + ClutterUnit clutter_units_mm (gdouble mm); ClutterUnit clutter_units_pt (gdouble pt); +ClutterUnit clutter_units_em (gdouble em); #define CLUTTER_TYPE_UNIT (clutter_unit_get_type ()) #define CLUTTER_TYPE_PARAM_UNIT (clutter_param_unit_get_type ()) diff --git a/doc/reference/clutter/clutter-sections.txt b/doc/reference/clutter/clutter-sections.txt index abef50c16..748691144 100644 --- a/doc/reference/clutter/clutter-sections.txt +++ b/doc/reference/clutter/clutter-sections.txt @@ -33,6 +33,8 @@ CLUTTER_UNITS_FROM_FLOAT CLUTTER_UNITS_TO_FLOAT CLUTTER_UNITS_FROM_INT CLUTTER_UNITS_TO_INT + + CLUTTER_UNITS_FROM_DEVICE CLUTTER_UNITS_TO_DEVICE CLUTTER_UNITS_FROM_FIXED @@ -41,8 +43,10 @@ CLUTTER_UNITS_FROM_PANGO_UNIT CLUTTER_UNITS_TO_PANGO_UNIT CLUTTER_UNITS_FROM_MM CLUTTER_UNITS_FROM_POINTS +CLUTTER_UNITS_FROM_EM clutter_units_mm clutter_units_pt +clutter_units_em CLUTTER_MAXUNIT