[yelp/wip/amigadave/webkit2-port] HACK! Convert font size from points to pixels



commit a4a5fa0e2310ac749f04ec25f946001d111163ca
Author: David King <amigadave amigadave com>
Date:   Sun Feb 15 16:15:23 2015 +0000

    HACK! Convert font size from points to pixels
    
    Code copied from Devhelp, written by Carlos Garcia Campos.

 libyelp/yelp-view.c |   58 +++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 49 insertions(+), 9 deletions(-)
---
diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c
index df68ccd..ed2fc46 100644
--- a/libyelp/yelp-view.c
+++ b/libyelp/yelp-view.c
@@ -30,6 +30,7 @@
 #ifdef GDK_WINDOWING_X11
 #include <gdk/gdkx.h>
 #endif
+#include <math.h>
 #include <webkit2/webkit2.h>
 
 #include "yelp-debug.h"
@@ -107,7 +108,8 @@ static void        view_load_page                    (YelpView           *view);
 static void        view_show_error_page              (YelpView           *view,
                                                       GError             *error);
 
-static void        settings_set_fonts                (YelpSettings       *settings);
+static void        settings_set_fonts                (YelpSettings       *settings,
+                                                      gpointer            user_data);
 static void        settings_show_text_cursor         (YelpSettings       *settings);
 
 static void        uri_resolved                      (YelpUri            *uri,
@@ -317,6 +319,7 @@ yelp_view_init (YelpView *view)
     };
 
     YelpViewPrivate *priv = GET_PRIV (view);
+    YelpSettings *settings = yelp_settings_get_default ();
 
     priv->cancellable = NULL;
 
@@ -375,6 +378,14 @@ yelp_view_init (YelpView *view)
                       "activate",
                       G_CALLBACK (view_navigation_action),
                       view);
+
+    g_signal_connect (settings,
+                      "fonts-changed",
+                      G_CALLBACK (settings_set_fonts),
+                      view);
+    settings_set_fonts (settings, view);
+    /* FIXME: Hack! */
+    g_object_set (view, "settings", websettings, NULL);
 }
 
 static void
@@ -473,11 +484,6 @@ yelp_view_class_init (YelpViewClass *klass)
                     NULL);
 
     g_signal_connect (settings,
-                      "fonts-changed",
-                      G_CALLBACK (settings_set_fonts),
-                      NULL);
-    settings_set_fonts (settings);
-    g_signal_connect (settings,
                       "notify::show-text-cursor",
                       G_CALLBACK (settings_show_text_cursor),
                       NULL);
@@ -2121,13 +2127,47 @@ view_show_error_page (YelpView *view,
         g_free (content_end);
 }
 
+static gdouble
+get_screen_dpi (GdkScreen *screen)
+{
+    gdouble dpi;
+    gdouble dp, di;
+
+    dpi = gdk_screen_get_resolution (screen);
+    if (dpi != -1)
+        return dpi;
+
+    dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
+    di = hypot (gdk_screen_get_width_mm (screen), gdk_screen_get_height_mm (screen)) / 25.4;
+
+    return dp / di;
+}
+
+static guint
+convert_font_size_to_pixels (GtkWidget *widget,
+                             gdouble    font_size)
+{
+    GdkScreen *screen;
+    gdouble    dpi;
+
+    /* WebKit2 uses font sizes in pixels */
+    screen = gtk_widget_has_screen (widget) ?
+             gtk_widget_get_screen (widget) : gdk_screen_get_default ();
+    dpi = screen ? get_screen_dpi (screen) : 96;
+
+    return font_size / 72.0 * dpi;
+}
 
 static void
-settings_set_fonts (YelpSettings *settings)
+settings_set_fonts (YelpSettings *settings,
+                    gpointer      user_data)
 {
+    YelpView *view;
     gchar *family;
     gint size;
 
+    view = (YelpView *) user_data;
+
     family = yelp_settings_get_font_family (settings,
                                             YELP_SETTINGS_FONT_VARIABLE);
     size = yelp_settings_get_font_size (settings,
@@ -2135,7 +2175,7 @@ settings_set_fonts (YelpSettings *settings)
     g_object_set (websettings,
                   "default-font-family", family,
                   "sans-serif-font-family", family,
-                  "default-font-size", size,
+                  "default-font-size", convert_font_size_to_pixels (GTK_WIDGET (view), size),
                   NULL);
     g_free (family);
 
@@ -2145,7 +2185,7 @@ settings_set_fonts (YelpSettings *settings)
                                         YELP_SETTINGS_FONT_FIXED);
     g_object_set (websettings,
                   "monospace-font-family", family,
-                  "default-monospace-font-size", size,
+                  "default-monospace-font-size", convert_font_size_to_pixels (GTK_WIDGET (view), size),
                   NULL);
     g_free (family);
 }


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