[gedit] view: remove deprecated usage of gtk_widget_override_font
- From: Sébastien Lafargue <slafargue src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit] view: remove deprecated usage of gtk_widget_override_font
- Date: Wed, 28 Nov 2018 22:08:40 +0000 (UTC)
commit 100b91ee49feeaf848ff1486e952d27394fcbf74
Author: Sebastien Lafargue <slafargue gnome org>
Date: Wed Nov 28 12:16:09 2018 +0100
view: remove deprecated usage of gtk_widget_override_font
We now use a css provider to change the font description.
gedit/Makefile.am | 2 +
gedit/gedit-pango.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++
gedit/gedit-pango.h | 32 ++++++++
gedit/gedit-view.c | 52 ++++++++++---
4 files changed, 280 insertions(+), 12 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index fa2f92dd6..65ae371c8 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -120,6 +120,7 @@ gedit_NOINST_H_FILES = \
gedit/gedit-open-document-selector.h \
gedit/gedit-open-document-selector-helper.h \
gedit/gedit-open-document-selector-store.h \
+ gedit/gedit-pango.h \
gedit/gedit-plugins-engine.h \
gedit/gedit-preferences-dialog.h \
gedit/gedit-print-job.h \
@@ -192,6 +193,7 @@ gedit_libgedit_c_files = \
gedit/gedit-open-document-selector.c \
gedit/gedit-open-document-selector-helper.c \
gedit/gedit-open-document-selector-store.c \
+ gedit/gedit-pango.c \
gedit/gedit-plugins-engine.c \
gedit/gedit-preferences-dialog.c \
gedit/gedit-print-job.c \
diff --git a/gedit/gedit-pango.c b/gedit/gedit-pango.c
new file mode 100644
index 000000000..10c548868
--- /dev/null
+++ b/gedit/gedit-pango.c
@@ -0,0 +1,206 @@
+/* gedit-pango.c
+ *
+ * This file is a copy of libdazzle dzl-pango.c
+ *
+ * Copyright (C) 2014-2017 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "gedit-pango"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <gdk/gdk.h>
+#include <glib/gstdio.h>
+#include <math.h>
+
+#include "gedit-pango.h"
+
+#define FONT_FAMILY "font-family"
+#define FONT_VARIANT "font-variant"
+#define FONT_STRETCH "font-stretch"
+#define FONT_WEIGHT "font-weight"
+#define FONT_SIZE "font-size"
+
+/**
+ * gedit_pango_font_description_to_css:
+ *
+ * This function will generate CSS suitable for Gtk's CSS engine
+ * based on the properties of the #PangoFontDescription.
+ *
+ * Returns: (transfer full): A newly allocated string containing the
+ * CSS describing the font description.
+ */
+gchar *
+gedit_pango_font_description_to_css (const PangoFontDescription *font_desc)
+{
+ PangoFontMask mask;
+ GString *str;
+
+#define ADD_KEYVAL(key,fmt) \
+ g_string_append(str,key":"fmt";")
+#define ADD_KEYVAL_PRINTF(key,fmt,...) \
+ g_string_append_printf(str,key":"fmt";", __VA_ARGS__)
+
+ g_return_val_if_fail (font_desc, NULL);
+
+ str = g_string_new (NULL);
+
+ mask = pango_font_description_get_set_fields (font_desc);
+
+ if ((mask & PANGO_FONT_MASK_FAMILY) != 0)
+ {
+ const gchar *family;
+
+ family = pango_font_description_get_family (font_desc);
+ ADD_KEYVAL_PRINTF (FONT_FAMILY, "\"%s\"", family);
+ }
+
+ if ((mask & PANGO_FONT_MASK_STYLE) != 0)
+ {
+ PangoVariant variant;
+
+ variant = pango_font_description_get_variant (font_desc);
+
+ switch (variant)
+ {
+ case PANGO_VARIANT_NORMAL:
+ ADD_KEYVAL (FONT_VARIANT, "normal");
+ break;
+
+ case PANGO_VARIANT_SMALL_CAPS:
+ ADD_KEYVAL (FONT_VARIANT, "small-caps");
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ if ((mask & PANGO_FONT_MASK_WEIGHT))
+ {
+ gint weight;
+
+ weight = pango_font_description_get_weight (font_desc);
+
+ /*
+ * WORKAROUND:
+ *
+ * font-weight with numbers does not appear to be working as expected
+ * right now. So for the common (bold/normal), let's just use the string
+ * and let gtk warn for the other values, which shouldn't really be
+ * used for this.
+ */
+
+ switch (weight)
+ {
+ case PANGO_WEIGHT_SEMILIGHT:
+ /*
+ * 350 is not actually a valid css font-weight, so we will just round
+ * up to 400.
+ */
+ case PANGO_WEIGHT_NORMAL:
+ ADD_KEYVAL (FONT_WEIGHT, "normal");
+ break;
+
+ case PANGO_WEIGHT_BOLD:
+ ADD_KEYVAL (FONT_WEIGHT, "bold");
+ break;
+
+ case PANGO_WEIGHT_THIN:
+ case PANGO_WEIGHT_ULTRALIGHT:
+ case PANGO_WEIGHT_LIGHT:
+ case PANGO_WEIGHT_BOOK:
+ case PANGO_WEIGHT_MEDIUM:
+ case PANGO_WEIGHT_SEMIBOLD:
+ case PANGO_WEIGHT_ULTRABOLD:
+ case PANGO_WEIGHT_HEAVY:
+ case PANGO_WEIGHT_ULTRAHEAVY:
+ default:
+ /* round to nearest hundred */
+ weight = round (weight / 100.0) * 100;
+ ADD_KEYVAL_PRINTF ("font-weight", "%d", weight);
+ break;
+ }
+ }
+
+#ifndef GDK_WINDOWING_QUARTZ
+ /*
+ * We seem to get "Condensed" for fonts on the Quartz backend,
+ * which is rather annoying as it results in us always hitting
+ * fallback (stretch) paths. So let's cheat and just disable
+ * stretch support for now on Quartz.
+ */
+ if ((mask & PANGO_FONT_MASK_STRETCH))
+ {
+ switch (pango_font_description_get_stretch (font_desc))
+ {
+ case PANGO_STRETCH_ULTRA_CONDENSED:
+ ADD_KEYVAL (FONT_STRETCH, "untra-condensed");
+ break;
+
+ case PANGO_STRETCH_EXTRA_CONDENSED:
+ ADD_KEYVAL (FONT_STRETCH, "extra-condensed");
+ break;
+
+ case PANGO_STRETCH_CONDENSED:
+ ADD_KEYVAL (FONT_STRETCH, "condensed");
+ break;
+
+ case PANGO_STRETCH_SEMI_CONDENSED:
+ ADD_KEYVAL (FONT_STRETCH, "semi-condensed");
+ break;
+
+ case PANGO_STRETCH_NORMAL:
+ ADD_KEYVAL (FONT_STRETCH, "normal");
+ break;
+
+ case PANGO_STRETCH_SEMI_EXPANDED:
+ ADD_KEYVAL (FONT_STRETCH, "semi-expanded");
+ break;
+
+ case PANGO_STRETCH_EXPANDED:
+ ADD_KEYVAL (FONT_STRETCH, "expanded");
+ break;
+
+ case PANGO_STRETCH_EXTRA_EXPANDED:
+ ADD_KEYVAL (FONT_STRETCH, "extra-expanded");
+ break;
+
+ case PANGO_STRETCH_ULTRA_EXPANDED:
+ ADD_KEYVAL (FONT_STRETCH, "untra-expanded");
+ break;
+
+ default:
+ break;
+ }
+ }
+#endif
+
+ if ((mask & PANGO_FONT_MASK_SIZE))
+ {
+ gint font_size;
+
+ font_size = pango_font_description_get_size (font_desc) / PANGO_SCALE;
+ ADD_KEYVAL_PRINTF ("font-size", "%dpt", font_size);
+ }
+
+ return g_string_free (str, FALSE);
+
+#undef ADD_KEYVAL
+#undef ADD_KEYVAL_PRINTF
+}
diff --git a/gedit/gedit-pango.h b/gedit/gedit-pango.h
new file mode 100644
index 000000000..022edc571
--- /dev/null
+++ b/gedit/gedit-pango.h
@@ -0,0 +1,32 @@
+/* gedit-pango.h
+ *
+ * This file is a copy of libdazzle dzl-pango.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GEDIT_PANGO_H
+#define GEDIT_PANGO_H
+
+#include <pango/pango.h>
+
+G_BEGIN_DECLS
+
+gchar *gedit_pango_font_description_to_css (const PangoFontDescription *font_desc);
+
+G_END_DECLS
+
+#endif /* GEDIT_PANGO_H */
diff --git a/gedit/gedit-view.c b/gedit/gedit-view.c
index 441d29306..9ff201164 100644
--- a/gedit/gedit-view.c
+++ b/gedit/gedit-view.c
@@ -28,6 +28,7 @@
#include "gedit-view-activatable.h"
#include "gedit-plugins-engine.h"
#include "gedit-debug.h"
+#include "gedit-pango.h"
#include "gedit-utils.h"
#include "gedit-settings.h"
#include "gedit-app.h"
@@ -43,10 +44,13 @@ enum
struct _GeditViewPrivate
{
- GSettings *editor_settings;
- GtkTextBuffer *current_buffer;
- PeasExtensionSet *extensions;
- gchar *direct_save_uri;
+ GSettings *editor_settings;
+ GtkTextBuffer *current_buffer;
+ PeasExtensionSet *extensions;
+ gchar *direct_save_uri;
+
+ GtkCssProvider *css_provider;
+ PangoFontDescription *font_desc;
};
G_DEFINE_TYPE_WITH_PRIVATE (GeditView, gedit_view, GTK_SOURCE_TYPE_VIEW)
@@ -154,8 +158,14 @@ gedit_view_init (GeditView *view)
G_CALLBACK (on_notify_buffer_cb),
NULL);
+
+ view->priv->css_provider = gtk_css_provider_new ();
context = gtk_widget_get_style_context (GTK_WIDGET (view));
gtk_style_context_add_class (context, "gedit-view");
+
+ gtk_style_context_add_provider (context,
+ GTK_STYLE_PROVIDER (view->priv->css_provider),
+ GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
}
static void
@@ -176,6 +186,9 @@ gedit_view_dispose (GObject *object)
*/
g_signal_handlers_disconnect_by_func (view, on_notify_buffer_cb, NULL);
+ g_clear_object (&view->priv->css_provider);
+ g_clear_pointer (&view->priv->font_desc, pango_font_description_free);
+
G_OBJECT_CLASS (gedit_view_parent_class)->dispose (object);
}
@@ -951,6 +964,23 @@ gedit_view_scroll_to_cursor (GeditView *view)
0.0);
}
+static void
+update_css_provider (GeditView *view)
+{
+ gchar *str;
+ gchar *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 : "");
+ gtk_css_provider_load_from_data (view->priv->css_provider, css, -1, NULL);
+
+ g_free (css);
+ g_free (str);
+}
+
/**
* gedit_view_set_font:
* @view: a #GeditView
@@ -965,12 +995,12 @@ gedit_view_set_font (GeditView *view,
gboolean default_font,
const gchar *font_name)
{
- PangoFontDescription *font_desc;
-
gedit_debug (DEBUG_VIEW);
g_return_if_fail (GEDIT_IS_VIEW (view));
+ g_clear_pointer (&view->priv->font_desc, pango_font_description_free);
+
if (default_font)
{
GeditSettings *settings;
@@ -979,21 +1009,19 @@ gedit_view_set_font (GeditView *view,
settings = _gedit_app_get_settings (GEDIT_APP (g_application_get_default ()));
font = gedit_settings_get_system_font (settings);
- font_desc = pango_font_description_from_string (font);
+ view->priv->font_desc = pango_font_description_from_string (font);
g_free (font);
}
else
{
g_return_if_fail (font_name != NULL);
- font_desc = pango_font_description_from_string (font_name);
+ view->priv->font_desc = pango_font_description_from_string (font_name);
}
- g_return_if_fail (font_desc != NULL);
-
- gtk_widget_override_font (GTK_WIDGET (view), font_desc);
+ g_return_if_fail (view->priv->font_desc != NULL);
- pango_font_description_free (font_desc);
+ update_css_provider (view);
}
/* ex:set ts=8 noet: */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]