Add default_font preference to Litehtml plugin
authorAndrej Kacian <ticho@claws-mail.org>
Sun, 17 Feb 2019 19:48:17 +0000 (20:48 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Sat, 4 May 2019 14:52:45 +0000 (16:52 +0200)
src/plugins/litehtml_viewer/container_linux.cpp
src/plugins/litehtml_viewer/container_linux.h
src/plugins/litehtml_viewer/lh_prefs.c
src/plugins/litehtml_viewer/lh_prefs.h
src/plugins/litehtml_viewer/lh_widget.cpp
src/plugins/litehtml_viewer/lh_widget.h

index 0993f8d1f3da9715fa0755c569ecec8524b4b6c6..a2b190859eed45253b19eefb9d11e28d98443f48 100644 (file)
@@ -217,11 +217,6 @@ int container_linux::pt_to_px( int pt )
        return (int) ((double) pt * dpi / 72.0);
 }
 
-int container_linux::get_default_font_size() const
-{
-       return 16;
-}
-
 void container_linux::draw_list_marker( litehtml::uint_ptr hdc, const litehtml::list_marker& marker )
 {
        if(!marker.image.empty())
@@ -871,11 +866,6 @@ gint container_linux::clear_images(gint desired_size)
        return num;
 }
 
-const litehtml::tchar_t* container_linux::get_default_font_name() const
-{
-       return "Times New Roman";
-}
-
 std::shared_ptr<litehtml::element>     container_linux::create_element(const litehtml::tchar_t *tag_name,
                                                                                                                                          const litehtml::string_map &attributes,
                                                                                                                                          const std::shared_ptr<litehtml::document> &doc)
index c6cda8e43f594778289e7e741b365bfb7e12a1bb..43a917c6e7cb70bdc6a7a19102af4be66e276a02 100644 (file)
@@ -62,8 +62,6 @@ public:
        virtual int                                             text_width(const litehtml::tchar_t* text, litehtml::uint_ptr hFont) override;
        virtual void                                            draw_text(litehtml::uint_ptr hdc, const litehtml::tchar_t* text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position& pos) override;
        virtual int                                             pt_to_px(int pt) override;
-       virtual int                                             get_default_font_size() const override;
-       virtual const litehtml::tchar_t*        get_default_font_name() const override;
        virtual void                                            load_image(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, bool redraw_on_ready) override;
        virtual void                                            get_image_size(const litehtml::tchar_t* src, const litehtml::tchar_t* baseurl, litehtml::size& sz) override;
        virtual void                                            draw_background(litehtml::uint_ptr hdc, const litehtml::background_paint& bg) override;
index 7dcc7aa738aa0dc50510837182084876a6fb62a1..94acc4ef45b5bef1a117a9c5142a1b858ed4a3d1 100644 (file)
@@ -44,6 +44,7 @@ struct _LHPrefsPage {
        PrefsPage page;
        GtkWidget *enable_remote_content;
        GtkWidget *image_cache_size;
+       GtkWidget *default_font;
 };
 typedef struct _LHPrefsPage LHPrefsPage;
 
@@ -52,6 +53,8 @@ static PrefParam param[] = {
                NULL, NULL, NULL },
        { "image_cache_size", "20", &lh_prefs.image_cache_size, P_INT,
                NULL, NULL, NULL },
+       { "default_font", "Sans 16", &lh_prefs.default_font, P_STRING,
+               NULL, NULL, NULL },
        { NULL, NULL, NULL, 0, NULL, NULL, NULL }
 };
 
@@ -99,6 +102,7 @@ static void create_lh_prefs_page(PrefsPage *page, GtkWindow *window,
        GtkWidget *label;
        GtkWidget *enable_remote_content;
        GtkWidget *image_cache_size;
+       GtkWidget *default_font;
        GtkObject *adj;
 
        vbox = gtk_vbox_new(FALSE, 3);
@@ -136,10 +140,22 @@ static void create_lh_prefs_page(PrefsPage *page, GtkWindow *window,
                        lh_prefs.image_cache_size);
        gtk_box_pack_start(GTK_BOX(hbox), image_cache_size, FALSE, FALSE, 0);
 
+       /* Font */
+       hbox = gtk_hbox_new(FALSE, 8);
+       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+       label = gtk_label_new(_("Default font"));
+       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+       default_font = gtk_font_button_new_with_font(lh_prefs.default_font);
+       g_object_set(G_OBJECT(default_font), "use-font", TRUE, NULL);
+       gtk_box_pack_start(GTK_BOX(hbox), default_font, FALSE, FALSE, 0);
+
        gtk_widget_show_all(hbox);
 
        prefs_page->enable_remote_content = enable_remote_content;
        prefs_page->image_cache_size = image_cache_size;
+       prefs_page->default_font = default_font;
        prefs_page->page.widget = vbox;
 }
 
@@ -153,9 +169,14 @@ static void save_lh_prefs_page(PrefsPage *page)
 
        lh_prefs.enable_remote_content = gtk_toggle_button_get_active(
                        GTK_TOGGLE_BUTTON(prefs_page->enable_remote_content));
+
        lh_prefs.image_cache_size = gtk_spin_button_get_value_as_int(
                        GTK_SPIN_BUTTON(prefs_page->image_cache_size));
 
+       g_free(lh_prefs.default_font);
+       lh_prefs.default_font = g_strdup(gtk_font_button_get_font_name(
+                       GTK_FONT_BUTTON(prefs_page->default_font)));
+
        save_prefs();
 }
 
index d226fe6c2dff5087ca7e4098431d9d544a954863..cd73f8152c32d78207f62633404bbd70281564c4 100644 (file)
@@ -30,6 +30,7 @@ struct _LHPrefs
 {
        gboolean enable_remote_content;
        gint image_cache_size;
+       gchar *default_font;
 };
 
 LHPrefs *lh_prefs_get(void);
index bf047cf0774e27c88a8e4e3ab3e24bc0e141a71d..17ae04e74269db427875f4e12f8051d8f8f3db69 100644 (file)
@@ -104,6 +104,9 @@ lh_widget::lh_widget()
        m_rendered_width = 0;
        m_context.load_master_stylesheet(master_css);
 
+       m_font_name = NULL;
+       m_font_size = 0;
+
        gtk_widget_set_events(m_drawing_area,
                                GDK_BUTTON_RELEASE_MASK
                              | GDK_BUTTON_PRESS_MASK
@@ -218,6 +221,8 @@ void lh_widget::open_html(const gchar *contents)
 
        debug_print("LH: cleared %d images from image cache\n", num);
 
+       update_font();
+
        lh_widget_statusbar_push("Loading HTML part ...");
        m_html = litehtml::document::createFromString(contents, this, &m_context);
        m_rendered_width = 0;
@@ -421,6 +426,25 @@ void lh_widget::popup_context_menu(const litehtml::tchar_t *url,
                        event->button, event->time);
 }
 
+void lh_widget::update_font()
+{
+       PangoFontDescription *pd =
+               pango_font_description_from_string(lh_prefs_get()->default_font);
+       gboolean absolute = pango_font_description_get_size_is_absolute(pd);
+
+       g_free(m_font_name);
+       m_font_name = g_strdup(pango_font_description_get_family(pd));
+       m_font_size = pango_font_description_get_size(pd);
+
+       pango_font_description_free(pd);
+
+       if (!absolute)
+               m_font_size /= PANGO_SCALE;
+
+       debug_print("Font set to '%s', size %d\n", m_font_name, m_font_size);
+}
+
+////////////////////////////////////////////////
 static gboolean expose_event_cb(GtkWidget *widget, GdkEvent *event,
                gpointer user_data)
 {
index 5e2a8720b9441ca793c84b595b99327a21c425e4..a7921978e164df4eb53e7141cbaa1d0f1945f843 100644 (file)
@@ -12,12 +12,15 @@ class lh_widget : public container_linux
 
                GtkWidget *get_widget() const;
 
+               /* Methods that litehtml calls */
                void set_caption(const litehtml::tchar_t* caption);
                void set_base_url(const litehtml::tchar_t* base_url);
                void on_anchor_click(const litehtml::tchar_t* url, const litehtml::element::ptr& el);
                void set_cursor(const litehtml::tchar_t* cursor);
                void import_css(litehtml::tstring& text, const litehtml::tstring& url, litehtml::tstring& baseurl);
                void get_client_rect(litehtml::position& client) const;
+               inline const litehtml::tchar_t *get_default_font_name() const { return m_font_name; };
+               inline int get_default_font_size() const { return m_font_size; };
                GdkPixbuf *get_image(const litehtml::tchar_t* url, bool redraw_on_ready);
 
                void draw(cairo_t *cr);
@@ -25,6 +28,7 @@ class lh_widget : public container_linux
                void open_html(const gchar *contents);
                void clear();
                void update_cursor();
+               void update_font();
                void print();
 
                const litehtml::tchar_t *get_href_at(const gint x, const gint y) const;
@@ -44,4 +48,7 @@ class lh_widget : public container_linux
                litehtml::context m_context;
                gint m_height;
                litehtml::tstring m_cursor;
+
+               litehtml::tchar_t *m_font_name;
+               int m_font_size;
 };