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>
Sun, 17 Feb 2019 19:48:17 +0000 (20:48 +0100)
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 0993f8d..a2b1908 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 c6cda8e..43a917c 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 7dcc7aa..94acc4e 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 d226fe6..cd73f81 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 bf047cf..17ae04e 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 5e2a872..a792197 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;
 };