[yelp/wip/amigadave/webkit2-port: 22/23] Convert font size from points to pixels



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

    Convert font size from points to pixels
    
    WebKit2 expects a font size of pixels, not points.
    
    Code copied from Devhelp, written by Carlos Garcia Campos.

 libyelp/yelp-view.c |   77 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 66 insertions(+), 11 deletions(-)
---
diff --git a/libyelp/yelp-view.c b/libyelp/yelp-view.c
index df68ccd..23701a5 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,
@@ -222,6 +224,7 @@ struct _YelpViewPrivate {
     gdouble        hadjust;
     gulong         vadjuster;
     gulong         hadjuster;
+    gulong         fonts_changed;
 
     gchar         *popup_link_uri;
     gchar         *popup_link_text;
@@ -378,6 +381,22 @@ yelp_view_init (YelpView *view)
 }
 
 static void
+yelp_view_constructed (GObject *object)
+{
+    YelpView *view = YELP_VIEW (object);
+    YelpViewPrivate *priv = GET_PRIV (view);
+    YelpSettings *settings = yelp_settings_get_default ();
+
+    G_OBJECT_CLASS (yelp_view_parent_class)->constructed (object);
+
+    priv->fonts_changed = g_signal_connect (settings,
+                                            "fonts-changed",
+                                            G_CALLBACK (settings_set_fonts),
+                                            view);
+    settings_set_fonts (settings, view);
+}
+
+static void
 yelp_view_dispose (GObject *object)
 {
     YelpViewPrivate *priv = GET_PRIV (object);
@@ -394,6 +413,12 @@ yelp_view_dispose (GObject *object)
         priv->hadjuster = 0;
     }
 
+    if (priv->fonts_changed > 0) {
+        g_signal_handler_disconnect (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (object)),
+                                     priv->fonts_changed);
+        priv->fonts_changed = 0;
+    }
+
     if (priv->print_action) {
         g_object_unref (priv->print_action);
         priv->print_action = NULL;
@@ -473,11 +498,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);
@@ -488,6 +508,7 @@ yelp_view_class_init (YelpViewClass *klass)
 
     klass->external_uri = view_external_uri;
 
+    object_class->constructed = yelp_view_constructed;
     object_class->dispose = yelp_view_dispose;
     object_class->finalize = yelp_view_finalize;
     object_class->get_property = yelp_view_get_property;
@@ -2121,21 +2142,55 @@ 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,
                                         YELP_SETTINGS_FONT_VARIABLE);
-    g_object_set (websettings,
+    g_object_set (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view)),
                   "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);
 
@@ -2143,9 +2198,9 @@ settings_set_fonts (YelpSettings *settings)
                                             YELP_SETTINGS_FONT_FIXED);
     size = yelp_settings_get_font_size (settings,
                                         YELP_SETTINGS_FONT_FIXED);
-    g_object_set (websettings,
+    g_object_set (webkit_web_view_get_settings (WEBKIT_WEB_VIEW (view)),
                   "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]