[gnome-logs] Fix CSS parsing error



commit bcf4307e2e22328c7f9d87df653a78c06f7f6a5a
Author: Jonathan Kang <jonathan121537 gmail com>
Date:   Mon Oct 10 16:43:51 2016 +0800

    Fix CSS parsing error
    
    https://bugzilla.gnome.org/show_bug.cgi?id=768985

 src/gl-application.c |   10 +++-
 src/gl-util.c        |  124 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gl-util.h        |    1 +
 3 files changed, 133 insertions(+), 2 deletions(-)
---
diff --git a/src/gl-application.c b/src/gl-application.c
index b4984aa..5919661 100644
--- a/src/gl-application.c
+++ b/src/gl-application.c
@@ -159,12 +159,16 @@ on_monospace_font_name_changed (GSettings *settings,
     {
         GtkCssProvider *provider;
         gchar *css_fragment;
+        gchar *css_desc;
+        PangoFontDescription *font_desc;
 
         g_free (priv->monospace_font);
         priv->monospace_font = font_name;
 
-        css_fragment = g_strconcat (".event-monospace { font: ", font_name,
-                                    "; }", NULL);
+        font_desc = pango_font_description_from_string (font_name);
+        css_desc = pango_font_description_to_css (font_desc);
+        css_fragment = g_strconcat (".event-monospace ", css_desc, NULL);
+
         provider = gtk_css_provider_new ();
         g_signal_connect (provider, "parsing-error",
                           G_CALLBACK (gl_util_on_css_provider_parsing_error),
@@ -175,8 +179,10 @@ on_monospace_font_name_changed (GSettings *settings,
                                                    GTK_STYLE_PROVIDER (provider),
                                                    GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
 
+        g_free (css_desc);
         g_free (css_fragment);
         g_object_unref (provider);
+        pango_font_description_free (font_desc);
     }
     else
     {
diff --git a/src/gl-util.c b/src/gl-util.c
index e10ff58..eabacf6 100644
--- a/src/gl-util.c
+++ b/src/gl-util.c
@@ -460,3 +460,127 @@ gl_util_can_read_user_journal (void)
         return FALSE;
     }
 }
+
+/**
+ * This function is orignally written in gtk/gtkfontbutton.c of gtk+ project.
+ */
+gchar *
+pango_font_description_to_css (PangoFontDescription *desc)
+{
+  GString *s;
+  PangoFontMask set;
+
+  s = g_string_new ("{ ");
+
+  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);
+}
diff --git a/src/gl-util.h b/src/gl-util.h
index 1b1c6ac..2cdcee8 100644
--- a/src/gl-util.h
+++ b/src/gl-util.h
@@ -65,6 +65,7 @@ gchar * gl_util_boot_time_to_display (guint64 timestamp_first,
 GlJournalStorage gl_util_journal_storage_type (void);
 gboolean gl_util_can_read_system_journal (GlJournalStorage storage_type);
 gboolean gl_util_can_read_user_journal (void);
+gchar *pango_font_description_to_css (PangoFontDescription *desc);
 
 G_END_DECLS
 


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