Add units-from-em conversion
authorEmmanuele Bassi <ebassi@linux.intel.com>
Wed, 21 Jan 2009 17:35:47 +0000 (17:35 +0000)
committerEmmanuele Bassi <ebassi@linux.intel.com>
Wed, 21 Jan 2009 17:35:47 +0000 (17:35 +0000)
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
clutter/clutter-units.h
doc/reference/clutter/clutter-sections.txt

index cdcb839..88b2e7f 100644 (file)
@@ -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 #ClutterUnit<!-- -->s 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,
index 8eb24d1..efc622e 100644 (file)
@@ -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 #ClutterUnit<!-- -->s
+ *
+ * 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 ())
index abef50c..7486911 100644 (file)
@@ -33,6 +33,8 @@ CLUTTER_UNITS_FROM_FLOAT
 CLUTTER_UNITS_TO_FLOAT
 CLUTTER_UNITS_FROM_INT
 CLUTTER_UNITS_TO_INT
+
+<SUBSECTION>
 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
 
 <SUBSECTION>
 CLUTTER_MAXUNIT