[almanah: 1/2] main-window: Correctly convert Pango font description to CSS
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [almanah: 1/2] main-window: Correctly convert Pango font description to CSS
- Date: Wed, 9 Sep 2020 10:25:51 +0000 (UTC)
commit 0b944a92ebc5f5c69156a0f46b8560a35384233c
Author: Philip Withnall <withnall endlessm com>
Date: Wed Sep 9 11:20:33 2020 +0100
main-window: Correctly convert Pango font description to CSS
A Pango font description string is not actually CSS, as it doesn’t use
the same units. Correctly convert it using code from GTK.
Solution suggested by Rinat Ibragimov.
Signed-off-by: Philip Withnall <withnall endlessm com>
Fixes: #4
src/main-window.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 121 insertions(+), 5 deletions(-)
---
diff --git a/src/main-window.c b/src/main-window.c
index 62b0f2a..04ef71e 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -1404,11 +1404,125 @@ mw_setup_headerbar (AlmanahMainWindow *main_window, AlmanahApplication *applicat
gtk_header_bar_pack_end (GTK_HEADER_BAR (main_window->priv->header_bar), button);
}
+/* Taken from pango_font_description_to_css() in GTK, licensed under GPLv2+
+ * and modified to add @selector. */
+static gchar *
+font_description_to_css (PangoFontDescription *desc, const gchar *selector)
+{
+ GString *s;
+ PangoFontMask set;
+
+ s = g_string_new (selector);
+ g_string_append (s, " { ");
+
+ set = pango_font_description_get_set_fields (desc);
+ if (set & PANGO_FONT_MASK_FAMILY) {
+ g_string_append (s, "font-family: ");
+ g_string_append (s, pango_font_description_get_family (desc));
+ g_string_append (s, "; ");
+ }
+ if (set & PANGO_FONT_MASK_STYLE) {
+ switch (pango_font_description_get_style (desc)) {
+ case PANGO_STYLE_NORMAL:
+ g_string_append (s, "font-style: normal; ");
+ break;
+ case PANGO_STYLE_OBLIQUE:
+ g_string_append (s, "font-style: oblique; ");
+ break;
+ case PANGO_STYLE_ITALIC:
+ g_string_append (s, "font-style: italic; ");
+ break;
+ }
+ }
+ if (set & PANGO_FONT_MASK_VARIANT) {
+ switch (pango_font_description_get_variant (desc)) {
+ case PANGO_VARIANT_NORMAL:
+ g_string_append (s, "font-variant: normal; ");
+ break;
+ case PANGO_VARIANT_SMALL_CAPS:
+ g_string_append (s, "font-variant: small-caps; ");
+ break;
+ }
+ }
+ if (set & PANGO_FONT_MASK_WEIGHT) {
+ switch (pango_font_description_get_weight (desc)) {
+ case PANGO_WEIGHT_THIN:
+ g_string_append (s, "font-weight: 100; ");
+ break;
+ case PANGO_WEIGHT_ULTRALIGHT:
+ g_string_append (s, "font-weight: 200; ");
+ break;
+ case PANGO_WEIGHT_LIGHT:
+ case PANGO_WEIGHT_SEMILIGHT:
+ g_string_append (s, "font-weight: 300; ");
+ break;
+ case PANGO_WEIGHT_BOOK:
+ case PANGO_WEIGHT_NORMAL:
+ g_string_append (s, "font-weight: 400; ");
+ break;
+ case PANGO_WEIGHT_MEDIUM:
+ g_string_append (s, "font-weight: 500; ");
+ break;
+ case PANGO_WEIGHT_SEMIBOLD:
+ g_string_append (s, "font-weight: 600; ");
+ break;
+ case PANGO_WEIGHT_BOLD:
+ g_string_append (s, "font-weight: 700; ");
+ break;
+ case PANGO_WEIGHT_ULTRABOLD:
+ g_string_append (s, "font-weight: 800; ");
+ break;
+ case PANGO_WEIGHT_HEAVY:
+ case PANGO_WEIGHT_ULTRAHEAVY:
+ g_string_append (s, "font-weight: 900; ");
+ break;
+ }
+ }
+ if (set & PANGO_FONT_MASK_STRETCH) {
+ switch (pango_font_description_get_stretch (desc)) {
+ case PANGO_STRETCH_ULTRA_CONDENSED:
+ g_string_append (s, "font-stretch: ultra-condensed; ");
+ break;
+ case PANGO_STRETCH_EXTRA_CONDENSED:
+ g_string_append (s, "font-stretch: extra-condensed; ");
+ break;
+ case PANGO_STRETCH_CONDENSED:
+ g_string_append (s, "font-stretch: condensed; ");
+ break;
+ case PANGO_STRETCH_SEMI_CONDENSED:
+ g_string_append (s, "font-stretch: semi-condensed; ");
+ break;
+ case PANGO_STRETCH_NORMAL:
+ g_string_append (s, "font-stretch: normal; ");
+ break;
+ case PANGO_STRETCH_SEMI_EXPANDED:
+ g_string_append (s, "font-stretch: semi-expanded; ");
+ break;
+ case PANGO_STRETCH_EXPANDED:
+ g_string_append (s, "font-stretch: expanded; ");
+ break;
+ case PANGO_STRETCH_EXTRA_EXPANDED:
+ g_string_append (s, "font-stretch: extra-expanded; ");
+ break;
+ case PANGO_STRETCH_ULTRA_EXPANDED:
+ g_string_append (s, "font-stretch: ultra-expanded; ");
+ break;
+ }
+ }
+ if (set & PANGO_FONT_MASK_SIZE) {
+ g_string_append_printf (s, "font-size: %dpt", pango_font_description_get_size (desc) /
PANGO_SCALE);
+ }
+
+ g_string_append (s, "}");
+
+ return g_string_free (s, FALSE);
+}
static void
mw_setup_size_text_view (AlmanahMainWindow *self)
{
- gchar *font_name = NULL;
+ gchar *font_desc_string = NULL;
+ PangoFontDescription *font_desc = NULL;
gchar *css_font = NULL;
int fixed_width;
@@ -1421,8 +1535,9 @@ mw_setup_size_text_view (AlmanahMainWindow *self)
self->priv->desktop_interface_settings = g_settings_new
(ALMANAH_MAIN_WINDOW_DESKTOP_INTERFACE_SETTINGS_SCHEMA);
g_signal_connect (self->priv->desktop_interface_settings, "changed", G_CALLBACK
(mw_desktop_interface_settings_changed), self);
}
- font_name = g_settings_get_string (self->priv->desktop_interface_settings,
ALMANAH_MAIN_WINDOW_DOCUMENT_FONT_KEY_NAME);
- css_font = g_strdup_printf (".almanah-mw-entry-view { font: %s; }", font_name);
+ font_desc_string = g_settings_get_string (self->priv->desktop_interface_settings,
ALMANAH_MAIN_WINDOW_DOCUMENT_FONT_KEY_NAME);
+ font_desc = pango_font_description_from_string (font_desc_string);
+ css_font = font_description_to_css (font_desc, ".almanah-mw-entry-view");
if (self->priv->css_provider == NULL) {
GtkStyleContext *style_context;
@@ -1433,12 +1548,13 @@ mw_setup_size_text_view (AlmanahMainWindow *self)
gtk_css_provider_load_from_data (self->priv->css_provider, css_font, strlen(css_font), NULL);
/* Setting up entry GtkTextView size based on font size plus a margin */
- fixed_width = mw_get_font_width (GTK_WIDGET (self->priv->entry_view), font_name) +
ALMANAH_MAIN_WINDOW_FIXED_MARGIN_FONT;
+ fixed_width = mw_get_font_width (GTK_WIDGET (self->priv->entry_view), font_desc_string) +
ALMANAH_MAIN_WINDOW_FIXED_MARGIN_FONT;
/* The ScrolledWindow (parent container for the text view) must be at
least the new width plus the text view margin */
gtk_widget_set_size_request(GTK_WIDGET (self->priv->entry_view), fixed_width, -1);
- g_free (font_name);
+ g_free (font_desc_string);
+ pango_font_description_free (font_desc);
g_free (css_font);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]