From 13762515153f9e254e5c17fb747ad76121f8710c Mon Sep 17 00:00:00 2001 From: Dan Vrátil Date: Thu, 12 Apr 2012 12:48:00 +0200 Subject: Bug #673108 - Font settings and monospace fonts don't work --- data/webview.css | 4 -- mail/e-mail-display.c | 56 ++++++++++++++++ mail/em-format-html.c | 2 +- widgets/misc/e-web-view.c | 159 ++++++++++++++++++++++++++++++++++++++++++---- widgets/misc/e-web-view.h | 10 ++- 5 files changed, 212 insertions(+), 19 deletions(-) diff --git a/data/webview.css b/data/webview.css index 9ff822013e..fb14fc704d 100644 --- a/data/webview.css +++ b/data/webview.css @@ -23,10 +23,6 @@ th { color: #7f7f7f; } -.pre { - font-family: monospace; -} - span.navigable, div.navigable, p.navigable { cursor: pointer; text-decoration: underline; diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c index a1d95bdaaa..d1f3137c80 100644 --- a/mail/e-mail-display.c +++ b/mail/e-mail-display.c @@ -66,6 +66,8 @@ struct _EMailDisplayPrivate { GtkActionGroup *images_actions; gint force_image_load: 1; + + GSettings *settings; }; enum { @@ -314,6 +316,11 @@ mail_display_dispose (GObject *object) priv->formatter = NULL; } + if (priv->settings) { + g_object_unref (priv->settings); + priv->settings = NULL; + } + /* Chain up to parent's dispose() method. */ G_OBJECT_CLASS (parent_class)->dispose (object); } @@ -1179,10 +1186,43 @@ mail_display_frame_created (WebKitWebView *web_view, G_CALLBACK (puri_bind_dom), NULL); } +static void +mail_display_set_fonts (EWebView *web_view, + PangoFontDescription **monospace, + PangoFontDescription **variable) +{ + EMailDisplay *display = E_MAIL_DISPLAY (web_view); + gboolean use_custom_font; + gchar *monospace_font, *variable_font; + + use_custom_font = g_settings_get_boolean (display->priv->settings, "use-custom-font"); + if (!use_custom_font) { + *monospace = NULL; + *variable = NULL; + return; + } + + monospace_font = g_settings_get_string ( + display->priv->settings, + "monospace-font"); + variable_font = g_settings_get_string ( + display->priv->settings, + "variable-width-font"); + + *monospace = monospace_font ? pango_font_description_from_string (monospace_font) : NULL; + *variable = variable_font ? pango_font_description_from_string (variable_font) : NULL; + + if (monospace_font) + g_free (monospace_font); + if (variable_font) + g_free (variable_font); +} + static void e_mail_display_class_init (EMailDisplayClass *class) { GObjectClass *object_class; + EWebViewClass *web_view_class; GtkWidgetClass *widget_class; parent_class = g_type_class_peek_parent (class); @@ -1193,6 +1233,9 @@ e_mail_display_class_init (EMailDisplayClass *class) object_class->get_property = mail_display_get_property; object_class->dispose = mail_display_dispose; + web_view_class = E_WEB_VIEW_CLASS (class); + web_view_class->set_fonts = mail_display_set_fonts; + widget_class = GTK_WIDGET_CLASS (class); widget_class->realize = mail_display_realize; widget_class->style_set = mail_display_style_set; @@ -1280,6 +1323,19 @@ e_mail_display_init (EMailDisplay *display) g_signal_connect (display, "frame-created", G_CALLBACK (mail_display_frame_created), NULL); + display->priv->settings = g_settings_new ("org.gnome.evolution.mail"); + g_signal_connect_swapped ( + display->priv->settings , "changed::monospace-font", + G_CALLBACK (e_web_view_update_fonts), display); + g_signal_connect_swapped ( + display->priv->settings , "changed::variable-width-font", + G_CALLBACK (e_web_view_update_fonts), display); + g_signal_connect_swapped ( + display->priv->settings , "changed::use-custom-font", + G_CALLBACK (e_web_view_update_fonts), display); + + e_web_view_update_fonts (E_WEB_VIEW (display)); + main_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (display)); g_signal_connect (main_frame, "notify::load-status", G_CALLBACK (setup_DOM_bindings), NULL); diff --git a/mail/em-format-html.c b/mail/em-format-html.c index 0dd190eba9..020ca9e721 100644 --- a/mail/em-format-html.c +++ b/mail/em-format-html.c @@ -760,7 +760,7 @@ efh_write_text_plain (EMFormat *emf, content = g_strdup_printf ( "
" - "
\n", + "
\n", e_color_to_value (&efh->priv->colors[ EM_FORMAT_HTML_COLOR_FRAME]), e_color_to_value (&efh->priv->colors[ diff --git a/widgets/misc/e-web-view.c b/widgets/misc/e-web-view.c index 582187558e..ca021b71f7 100644 --- a/widgets/misc/e-web-view.c +++ b/widgets/misc/e-web-view.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -69,6 +70,9 @@ struct _EWebViewPrivate { guint disable_save_to_disk : 1; guint caret_mode : 1; + + GSettings *font_settings; + GSettings *aliasing_settings; }; enum { @@ -658,7 +662,6 @@ web_view_set_property (GObject *object, g_value_get_string (value)); return; } - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -740,6 +743,7 @@ web_view_get_property (GObject *object, value, e_web_view_get_selected_uri ( E_WEB_VIEW (object))); return; + } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -792,6 +796,16 @@ web_view_dispose (GObject *object) priv->highlights = NULL; } + if (priv->aliasing_settings != NULL) { + g_object_unref (priv->aliasing_settings); + priv->aliasing_settings = NULL; + } + + if (priv->font_settings != NULL) { + g_object_unref (priv->font_settings); + priv->font_settings = NULL; + } + /* Chain up to parent's dispose() method. */ G_OBJECT_CLASS (parent_class)->dispose (object); } @@ -1692,10 +1706,22 @@ e_web_view_init (EWebView *web_view) ui_manager, "connect-proxy", G_CALLBACK (web_view_connect_proxy_cb), web_view); - web_settings = e_web_view_get_default_settings (GTK_WIDGET (web_view)); + web_settings = e_web_view_get_default_settings (); e_web_view_set_settings (web_view, web_settings); g_object_unref (web_settings); + web_view->priv->font_settings = g_settings_new ("org.gnome.desktop.interface"); + g_signal_connect_swapped (web_view->priv->font_settings, "changed::font-name", + G_CALLBACK (e_web_view_update_fonts), web_view); + g_signal_connect_swapped (web_view->priv->font_settings, "changed::monospace-font-name", + G_CALLBACK (e_web_view_update_fonts), web_view); + + web_view->priv->aliasing_settings = g_settings_new ("org.gnome.settings-daemon.plugins.xsettings"); + g_signal_connect_swapped (web_view->priv->aliasing_settings, "changed::antialiasing", + G_CALLBACK (e_web_view_update_fonts), web_view); + + e_web_view_update_fonts (web_view); + action_group = gtk_action_group_new ("uri"); gtk_action_group_set_translation_domain (action_group, domain); gtk_ui_manager_insert_action_group (ui_manager, action_group, 0); @@ -2792,23 +2818,13 @@ e_web_view_set_settings (EWebView *web_view, } WebKitWebSettings * -e_web_view_get_default_settings (GtkWidget *parent_widget) +e_web_view_get_default_settings (void) { - GtkStyleContext *context; - const PangoFontDescription *font; WebKitWebSettings *settings; - g_return_val_if_fail (GTK_IS_WIDGET (parent_widget), NULL); - settings = webkit_web_settings_new (); - /* Use same font-size as rest of Evolution */ - context = gtk_widget_get_style_context (parent_widget); - font = gtk_style_context_get_font (context, GTK_STATE_FLAG_NORMAL); - g_object_set (G_OBJECT (settings), - "default-font-size", (pango_font_description_get_size (font) / PANGO_SCALE), - "default-monospace-font-size", (pango_font_description_get_size (font) / PANGO_SCALE), "enable-frame-flattening", TRUE, "enable-java-applet", FALSE, "enable-html5-database", FALSE, @@ -2820,3 +2836,120 @@ e_web_view_get_default_settings (GtkWidget *parent_widget) return settings; } + +void +e_web_view_update_fonts(EWebView *web_view) +{ + GString *stylesheet; + gchar *aa, *base64; + WebKitWebSettings *settings; + PangoFontDescription *min_size, *ms, *vw; + const gchar *styles[] = { "normal", "oblique", "italic" }; + + ms = NULL; + vw = NULL; + + if (E_WEB_VIEW_GET_CLASS (web_view)->set_fonts) + E_WEB_VIEW_GET_CLASS (web_view)->set_fonts (web_view, &ms, &vw); + + if (ms == NULL) { + gchar *font; + + font = g_settings_get_string ( + web_view->priv->font_settings, + "monospace-font-name"); + + ms = pango_font_description_from_string ( + font ? font : "monospace 10"); + + g_free (font); + } + + if (vw == NULL) { + gchar *font; + + font = g_settings_get_string ( + web_view->priv->font_settings, + "font-name"); + + vw = pango_font_description_from_string ( + font ? font : "serif 10"); + + g_free (font); + } + + if (pango_font_description_get_size (ms) < pango_font_description_get_size (vw)) { + min_size = ms; + } else { + min_size = vw; + } + + stylesheet = g_string_new (""); + g_string_append_printf (stylesheet, + "body {\n" + " font-family: %s;\n" + " font-size: %dpt;\n" + " font-weight: %d;\n" + " font-style: %s;\n", + pango_font_description_get_family (vw), + pango_font_description_get_size (vw) / PANGO_SCALE, + pango_font_description_get_weight (vw), + styles[pango_font_description_get_style (vw)]); + + aa = g_settings_get_string (web_view->priv->aliasing_settings, "antialiasing"); + if (aa) { + const gchar *smoothing = NULL; + + if (g_strcmp0 (aa, "none") == 0) { + smoothing = "none"; + } else if (g_strcmp0 (aa, "grayscale") == 0) { + smoothing = "antialiased"; + } else if (g_strcmp0 (aa, "rgba") == 0) { + smoothing = "subpixel-antialiased"; + } + + if (smoothing) { + g_string_append_printf (stylesheet, + " -webkit-font-smoothing: %s;\n", + smoothing); + } + + g_free (aa); + } + + g_string_append (stylesheet, "}\n"); + + g_string_append_printf (stylesheet, + "pre,code,.pre {\n" + " font-family: %s;\n" + " font-size: %dpt;\n" + " font-weight: %d;\n" + " font-style: %s;\n" + "}", + pango_font_description_get_family (ms), + pango_font_description_get_size (ms) / PANGO_SCALE, + pango_font_description_get_weight (ms), + styles[pango_font_description_get_style (ms)]); + + base64 = g_base64_encode ((guchar *) stylesheet->str, stylesheet->len); + g_string_free (stylesheet, TRUE); + + stylesheet = g_string_new ("data:text/css;charset=utf-8;base64,"); + g_string_append (stylesheet, base64); + g_free (base64); + + settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (web_view)); + g_object_set (G_OBJECT (settings), + "default-font-size", pango_font_description_get_size (vw) / PANGO_SCALE, + "default-font-family", pango_font_description_get_family (vw), + "monospace-font-family", pango_font_description_get_family (ms), + "default-monospace-font-size", (pango_font_description_get_size (ms) / PANGO_SCALE), + "minimum-font-size", (pango_font_description_get_size (min_size) / PANGO_SCALE), + "user-stylesheet-uri", stylesheet->str, + NULL); + + g_string_free (stylesheet, TRUE); + + pango_font_description_free (ms); + pango_font_description_free (vw); +} diff --git a/widgets/misc/e-web-view.h b/widgets/misc/e-web-view.h index d71cf04822..02eb9ecf66 100644 --- a/widgets/misc/e-web-view.h +++ b/widgets/misc/e-web-view.h @@ -54,6 +54,7 @@ G_BEGIN_DECLS typedef struct _EWebView EWebView; typedef struct _EWebViewClass EWebViewClass; typedef struct _EWebViewPrivate EWebViewPrivate; +struct PangoFontDescription; struct _EWebView { WebKitWebView parent; @@ -90,6 +91,10 @@ struct _EWebViewClass { void (*frame_load_uri) (EWebView *web_view, const gchar *frame_name, const gchar *uri); + void (*set_fonts) (EWebView *web_view, + PangoFontDescription **monospace, + PangoFontDescription **variable_width); + /* Signals */ gboolean (*popup_event) (EWebView *web_view, GdkEventButton *event, @@ -219,8 +224,11 @@ gchar * e_web_view_get_selection_html (EWebView *web_view); void e_web_view_set_settings (EWebView *web_view, WebKitWebSettings *settings); + +void e_web_view_update_fonts (EWebView *web_view); + WebKitWebSettings * - e_web_view_get_default_settings (); + e_web_view_get_default_settings (void); G_END_DECLS -- cgit