[gedit/tchaik/69-text-size: 1/2] view: Implement text view font scaling



commit 84ff867d6c3d4671ea25171e9c4d48a3d9718ca0
Author: Martin Blanchard <tchaik gmx com>
Date:   Tue Aug 13 22:42:37 2019 +0200

    view: Implement text view font scaling
    
    Let the text view font size be scaled up and down by +/- 3 levels
    using a new GeditView API.

 gedit/gedit-pango.c | 17 ++++++++++++
 gedit/gedit-pango.h |  5 +++-
 gedit/gedit-view.c  | 80 +++++++++++++++++++++++++++++++++++++++++++++++++----
 gedit/gedit-view.h  |  4 +++
 4 files changed, 100 insertions(+), 6 deletions(-)
---
diff --git a/gedit/gedit-pango.c b/gedit/gedit-pango.c
index 10c548868..a87993761 100644
--- a/gedit/gedit-pango.c
+++ b/gedit/gedit-pango.c
@@ -204,3 +204,20 @@ gedit_pango_font_description_to_css (const PangoFontDescription *font_desc)
 #undef ADD_KEYVAL
 #undef ADD_KEYVAL_PRINTF
 }
+
+PangoFontDescription *
+gedit_pango_scale_font_description (const PangoFontDescription *font_desc,
+                                    gdouble                     font_scale)
+{
+       PangoFontDescription *font_desc_copy;
+       guint font_size;
+
+       g_return_val_if_fail (font_desc, NULL);
+
+       font_desc_copy = pango_font_description_copy (font_desc);
+       font_size = pango_font_description_get_size (font_desc);
+
+       pango_font_description_set_size (font_desc_copy, font_size * font_scale);
+
+       return font_desc_copy;
+}
diff --git a/gedit/gedit-pango.h b/gedit/gedit-pango.h
index 022edc571..44c2c3f13 100644
--- a/gedit/gedit-pango.h
+++ b/gedit/gedit-pango.h
@@ -25,7 +25,10 @@
 
 G_BEGIN_DECLS
 
-gchar *gedit_pango_font_description_to_css (const PangoFontDescription *font_desc);
+gchar                  *gedit_pango_font_description_to_css    (const PangoFontDescription *font_desc);
+
+PangoFontDescription   *gedit_pango_scale_font_description     (const PangoFontDescription *font_desc,
+                                                                gdouble                     font_scale);
 
 G_END_DECLS
 
diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c
index 9ff201164..be2973f85 100644
--- a/gedit/gedit-view.c
+++ b/gedit/gedit-view.c
@@ -42,6 +42,17 @@ enum
        TARGET_XDNDDIRECTSAVE
 };
 
+enum {
+       FONT_SCALE_XX_SMALL,
+       FONT_SCALE_X_SMALL,
+       FONT_SCALE_SMALL,
+       FONT_SCALE_NORMAL,
+       FONT_SCALE_LARGE,
+       FONT_SCALE_X_LARGE,
+       FONT_SCALE_XX_LARGE,
+       LAST_FONT_SCALE
+};
+
 struct _GeditViewPrivate
 {
        GSettings            *editor_settings;
@@ -51,6 +62,7 @@ struct _GeditViewPrivate
 
        GtkCssProvider       *css_provider;
        PangoFontDescription *font_desc;
+       guint                 font_scale;
 };
 
 G_DEFINE_TYPE_WITH_PRIVATE (GeditView, gedit_view, GTK_SOURCE_TYPE_VIEW)
@@ -62,6 +74,15 @@ enum
 };
 
 static guint view_signals[LAST_SIGNAL] = { 0 };
+static gdouble font_scales[LAST_FONT_SCALE] = {
+       PANGO_SCALE_XX_SMALL,
+       PANGO_SCALE_X_SMALL,
+       PANGO_SCALE_SMALL,
+       PANGO_SCALE_MEDIUM,
+       PANGO_SCALE_LARGE,
+       PANGO_SCALE_X_LARGE,
+       PANGO_SCALE_XX_LARGE,
+};
 
 static void
 file_read_only_notify_handler (GtkSourceFile *file,
@@ -202,6 +223,8 @@ gedit_view_constructed (GObject *object)
        view = GEDIT_VIEW (object);
        priv = view->priv;
 
+       view->priv->font_scale = FONT_SCALE_NORMAL;
+
        use_default_font = g_settings_get_boolean (view->priv->editor_settings,
                                                   GEDIT_SETTINGS_USE_DEFAULT_FONT);
 
@@ -967,18 +990,26 @@ gedit_view_scroll_to_cursor (GeditView *view)
 static void
 update_css_provider (GeditView *view)
 {
-       gchar *str;
-       gchar *css;
+       const PangoFontDescription *font_desc = view->priv->font_desc;
+       PangoFontDescription *copy = NULL;
+       gchar *desc, *css;
 
        g_assert (GEDIT_IS_VIEW (view));
        g_assert (view->priv->font_desc != NULL);
 
-       str = gedit_pango_font_description_to_css (view->priv->font_desc);
-       css = g_strdup_printf ("textview { %s }", str ? str : "");
+       if (view->priv->font_scale != FONT_SCALE_NORMAL)
+       {
+               font_desc = copy = gedit_pango_scale_font_description (font_desc,
+                                                                      font_scales[view->priv->font_scale]);
+       }
+
+       desc = gedit_pango_font_description_to_css (font_desc);
+       css = g_strdup_printf ("textview { %s }", desc ? desc : "");
        gtk_css_provider_load_from_data (view->priv->css_provider, css, -1, NULL);
 
+       g_clear_pointer (&copy, pango_font_description_free);
        g_free (css);
-       g_free (str);
+       g_free (desc);
 }
 
 /**
@@ -1024,4 +1055,43 @@ gedit_view_set_font (GeditView   *view,
        update_css_provider (view);
 }
 
+void
+gedit_view_increase_font_size (GeditView *view)
+{
+       g_return_if_fail (GEDIT_IS_VIEW (view));
+
+       if (view->priv->font_scale < LAST_FONT_SCALE - 1)
+       {
+               view->priv->font_scale++;
+
+               update_css_provider (view);
+       }
+}
+
+void
+gedit_view_decrease_font_size (GeditView *view)
+{
+       g_return_if_fail (GEDIT_IS_VIEW (view));
+
+       if (view->priv->font_scale > 0)
+       {
+               view->priv->font_scale--;
+
+               update_css_provider (view);
+       }
+}
+
+void
+gedit_view_reset_font_size (GeditView *view)
+{
+       g_return_if_fail (GEDIT_IS_VIEW (view));
+
+       if (view->priv->font_scale != FONT_SCALE_NORMAL)
+       {
+               view->priv->font_scale = FONT_SCALE_NORMAL;
+
+               update_css_provider (view);
+       }
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gedit/gedit-view.h b/gedit/gedit-view.h
index fc10225b5..8cfef5576 100644
--- a/gedit/gedit-view.h
+++ b/gedit/gedit-view.h
@@ -75,6 +75,10 @@ void                  gedit_view_set_font            (GeditView       *view,
                                                 gboolean         default_font,
                                                 const gchar     *font_name);
 
+void            gedit_view_increase_font_size  (GeditView       *view);
+void            gedit_view_decrease_font_size  (GeditView       *view);
+void            gedit_view_reset_font_size     (GeditView       *view);
+
 G_END_DECLS
 
 #endif /* GEDIT_VIEW_H */


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