[gtk+] css: always get default font size in pixels



commit df08fc91bdc1d2e4c866122304fabe4dd298a7de
Author: Matt Watson <mattdangerw gmail com>
Date:   Mon Apr 11 18:18:31 2016 -0700

    css: always get default font size in pixels
    
    Fixes a couple bugs...
    
     - Pixel font sizes in css would render as point sizes.
    
     - For em font sizes, where the parent size was set and not default, we would
       incorrectly convert a pixel value from points to pixels.
    
    We'll always grab the default font size in pixels so we don't keep confusing
    things.
    
    Worth noting that gtk css font-size will still behave differently than the
    web. Pango interprets font-size differently.

 gtk/gtkcssdimensionvalue.c        |   24 ++++++++++----------
 gtk/gtkcssenumvalue.c             |   41 +++++++++++++++++++++++-------------
 gtk/gtkcssenumvalueprivate.h      |    3 +-
 gtk/gtkcssshorthandpropertyimpl.c |    5 +++-
 4 files changed, 44 insertions(+), 29 deletions(-)
---
diff --git a/gtk/gtkcssdimensionvalue.c b/gtk/gtkcssdimensionvalue.c
index 92c2d99..0577df6 100644
--- a/gtk/gtkcssdimensionvalue.c
+++ b/gtk/gtkcssdimensionvalue.c
@@ -37,17 +37,17 @@ gtk_css_value_dimension_free (GtkCssValue *value)
 }
 
 static double
-get_base_font_size (guint                    property_id,
-                    GtkStyleProviderPrivate *provider,
-                    GtkCssStyle             *style,
-                    GtkCssStyle             *parent_style)
+get_base_font_size_px (guint                    property_id,
+                       GtkStyleProviderPrivate *provider,
+                       GtkCssStyle             *style,
+                       GtkCssStyle             *parent_style)
 {
   if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
     {
       if (parent_style)
         return _gtk_css_number_value_get (gtk_css_style_get_value (parent_style, 
GTK_CSS_PROPERTY_FONT_SIZE), 100);
       else
-        return _gtk_css_font_size_get_default (provider);
+        return gtk_css_font_size_get_default_px (provider, style);
     }
 
   return _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_FONT_SIZE), 100);
@@ -109,7 +109,7 @@ gtk_css_value_dimension_compute (GtkCssValue             *number,
       /* percentages for font sizes are computed, other percentages aren't */
       if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
         return gtk_css_dimension_value_new (number->value / 100.0 * 
-                                            get_base_font_size (property_id, provider, style, parent_style),
+                                            get_base_font_size_px (property_id, provider, style, 
parent_style),
                                             GTK_CSS_PX);
     case GTK_CSS_NUMBER:
     case GTK_CSS_PX:
@@ -132,17 +132,17 @@ gtk_css_value_dimension_compute (GtkCssValue             *number,
       return gtk_css_dimension_value_new (number->value * get_dpi (style) * 0.039370078740157477,
                                           GTK_CSS_PX);
     case GTK_CSS_EM:
-      return gtk_css_dimension_value_new (number->value * get_dpi (style) / 72.0 *
-                                          get_base_font_size (property_id, provider, style, parent_style),
+      return gtk_css_dimension_value_new (number->value *
+                                          get_base_font_size_px (property_id, provider, style, parent_style),
                                           GTK_CSS_PX);
     case GTK_CSS_EX:
       /* for now we pretend ex is half of em */
-      return gtk_css_dimension_value_new (number->value * 0.5 * get_dpi (style) / 72.0 *
-                                          get_base_font_size (property_id, provider, style, parent_style),
+      return gtk_css_dimension_value_new (number->value * 0.5 *
+                                          get_base_font_size_px (property_id, provider, style, parent_style),
                                           GTK_CSS_PX);
     case GTK_CSS_REM:
-      return gtk_css_dimension_value_new (number->value * get_dpi (style) / 72.0 *
-                                          _gtk_css_font_size_get_default (provider),
+      return gtk_css_dimension_value_new (number->value *
+                                          gtk_css_font_size_get_default_px (provider, style),
                                           GTK_CSS_PX);
     case GTK_CSS_RAD:
       return gtk_css_dimension_value_new (number->value * 360.0 / (2 * G_PI),
diff --git a/gtk/gtkcssenumvalue.c b/gtk/gtkcssenumvalue.c
index f8828ec..7cb0adc 100644
--- a/gtk/gtkcssenumvalue.c
+++ b/gtk/gtkcssenumvalue.c
@@ -127,13 +127,20 @@ _gtk_css_border_style_value_get (const GtkCssValue *value)
 
 /* GtkCssFontSize */
 
+static double
+get_dpi (GtkCssStyle *style)
+{
+  return _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_DPI), 96);
+}
+
 /* XXX: Kinda bad to have that machinery here, nobody expects vital font
  * size code to appear in gtkcssvalueenum.c.
  */
-#define DEFAULT_FONT_SIZE 10
+#define DEFAULT_FONT_SIZE_PT 10
 
 double
-_gtk_css_font_size_get_default (GtkStyleProviderPrivate *provider)
+gtk_css_font_size_get_default_px (GtkStyleProviderPrivate *provider,
+                                  GtkCssStyle             *style)
 {
   GtkSettings *settings;
   PangoFontDescription *description;
@@ -142,18 +149,22 @@ _gtk_css_font_size_get_default (GtkStyleProviderPrivate *provider)
 
   settings = _gtk_style_provider_private_get_settings (provider);
   if (settings == NULL)
-    return DEFAULT_FONT_SIZE;
+    return DEFAULT_FONT_SIZE_PT * get_dpi (style) / 72.0;
   
   g_object_get (settings, "gtk-font-name", &font_name, NULL);
   description = pango_font_description_from_string (font_name);
   g_free (font_name);
   if (description == NULL)
-    return DEFAULT_FONT_SIZE;
+    return DEFAULT_FONT_SIZE_PT * get_dpi (style) / 72.0;
 
   if (pango_font_description_get_set_fields (description) & PANGO_FONT_MASK_SIZE)
-    font_size = (double) pango_font_description_get_size (description) / PANGO_SCALE;
+    {
+      font_size = (double) pango_font_description_get_size (description) / PANGO_SCALE;
+      if (!pango_font_description_get_size_is_absolute (description))
+        font_size = font_size * get_dpi (style) / 72.0;
+    }
   else
-    font_size = DEFAULT_FONT_SIZE;
+    font_size = DEFAULT_FONT_SIZE_PT * get_dpi (style) / 72.0;
 
   pango_font_description_free (description);
   return font_size;
@@ -171,34 +182,34 @@ gtk_css_value_font_size_compute (GtkCssValue             *value,
   switch (value->value)
     {
     case GTK_CSS_FONT_SIZE_XX_SMALL:
-      font_size = _gtk_css_font_size_get_default (provider) * 3. / 5;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 3. / 5;
       break;
     case GTK_CSS_FONT_SIZE_X_SMALL:
-      font_size = _gtk_css_font_size_get_default (provider) * 3. / 4;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 3. / 4;
       break;
     case GTK_CSS_FONT_SIZE_SMALL:
-      font_size = _gtk_css_font_size_get_default (provider) * 8. / 9;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 8. / 9;
       break;
     default:
       g_assert_not_reached ();
       /* fall thru */
     case GTK_CSS_FONT_SIZE_MEDIUM:
-      font_size = _gtk_css_font_size_get_default (provider);
+      font_size = gtk_css_font_size_get_default_px (provider, style);
       break;
     case GTK_CSS_FONT_SIZE_LARGE:
-      font_size = _gtk_css_font_size_get_default (provider) * 6. / 5;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 6. / 5;
       break;
     case GTK_CSS_FONT_SIZE_X_LARGE:
-      font_size = _gtk_css_font_size_get_default (provider) * 3. / 2;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 3. / 2;
       break;
     case GTK_CSS_FONT_SIZE_XX_LARGE:
-      font_size = _gtk_css_font_size_get_default (provider) * 2;
+      font_size = gtk_css_font_size_get_default_px (provider, style) * 2;
       break;
     case GTK_CSS_FONT_SIZE_SMALLER:
       if (parent_style)
         font_size = _gtk_css_number_value_get (gtk_css_style_get_value (parent_style, 
GTK_CSS_PROPERTY_FONT_SIZE), 100);
       else
-        font_size = _gtk_css_font_size_get_default (provider);
+        font_size = gtk_css_font_size_get_default_px (provider, style);
       /* XXX: This is what WebKit does... */
       font_size /= 1.2;
       break;
@@ -206,7 +217,7 @@ gtk_css_value_font_size_compute (GtkCssValue             *value,
       if (parent_style)
         font_size = _gtk_css_number_value_get (gtk_css_style_get_value (parent_style, 
GTK_CSS_PROPERTY_FONT_SIZE), 100);
       else
-        font_size = _gtk_css_font_size_get_default (provider);
+        font_size = gtk_css_font_size_get_default_px (provider, style);
       /* XXX: This is what WebKit does... */
       font_size *= 1.2;
       break;
diff --git a/gtk/gtkcssenumvalueprivate.h b/gtk/gtkcssenumvalueprivate.h
index 884a243..f8b37c2 100644
--- a/gtk/gtkcssenumvalueprivate.h
+++ b/gtk/gtkcssenumvalueprivate.h
@@ -34,7 +34,8 @@ GtkBorderStyle  _gtk_css_border_style_value_get       (const GtkCssValue *value)
 GtkCssValue *   _gtk_css_font_size_value_new          (GtkCssFontSize     size);
 GtkCssValue *   _gtk_css_font_size_value_try_parse    (GtkCssParser      *parser);
 GtkCssFontSize  _gtk_css_font_size_value_get          (const GtkCssValue *value);
-double          _gtk_css_font_size_get_default        (GtkStyleProviderPrivate *provider);
+double          gtk_css_font_size_get_default_px      (GtkStyleProviderPrivate *provider,
+                                                       GtkCssStyle             *style);
 
 GtkCssValue *   _gtk_css_font_style_value_new         (PangoStyle         style);
 GtkCssValue *   _gtk_css_font_style_value_try_parse   (GtkCssParser      *parser);
diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c
index 27ff25b..fc4cf42 100644
--- a/gtk/gtkcssshorthandpropertyimpl.c
+++ b/gtk/gtkcssshorthandpropertyimpl.c
@@ -1074,6 +1074,7 @@ pack_font_description (GtkCssShorthandProperty *shorthand,
 {
   PangoFontDescription *description;
   GtkCssValue *v;
+  double dpi;
 
   description = pango_font_description_new ();
 
@@ -1084,9 +1085,11 @@ pack_font_description (GtkCssShorthandProperty *shorthand,
       pango_font_description_set_family (description, _gtk_css_string_value_get 
(_gtk_css_array_value_get_nth (v, 0)));
     }
 
+  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup 
("-gtk-dpi"))), query_data);
+  dpi = _gtk_css_number_value_get (v, 96);
   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup 
("font-size"))), query_data);
   if (v)
-    pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE));
+    pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE * 
72 / dpi));
 
   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup 
("font-style"))), query_data);
   if (v)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]