[gtranslator: 1/10] Implement font_desc_to_css in tree




commit d2abbe67ed19369da0957c9a8a26128a1ee6268f
Author: Maximiliano Sandoval R <msandova gnome org>
Date:   Mon Mar 14 13:45:15 2022 +0100

    Implement font_desc_to_css in tree
    
    To be able to remove libdazzle as a dep. This is useful for porting the
    app to gtk4.

 src/gtr-utils.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gtr-utils.h |   2 +
 src/gtr-view.c  |   5 +-
 3 files changed, 216 insertions(+), 3 deletions(-)
---
diff --git a/src/gtr-utils.c b/src/gtr-utils.c
index 994cd0a6..13cef40e 100644
--- a/src/gtr-utils.c
+++ b/src/gtr-utils.c
@@ -588,3 +588,215 @@ gtr_utils_get_filename (const gchar * filename)
 
   return new_str;
 }
+
+/* Copied from gtkfontbutton.c */
+static void
+add_css_variations (GString    *s,
+                    const char *variations)
+{
+  const char *p;
+  const char *sep = "";
+
+  if (variations == NULL || variations[0] == '\0')
+    {
+      g_string_append (s, "normal");
+      return;
+    }
+
+  p = variations;
+  while (p && *p)
+    {
+      const char *start;
+      const char *end, *end2;
+      double value;
+      char name[5];
+
+      while (g_ascii_isspace (*p)) p++;
+
+      start = p;
+      end = strchr (p, ',');
+      if (end && (end - p < 6))
+        goto skip;
+
+      name[0] = p[0];
+      name[1] = p[1];
+      name[2] = p[2];
+      name[3] = p[3];
+      name[4] = '\0';
+
+      p += 4;
+      while (g_ascii_isspace (*p)) p++;
+      if (*p == '=') p++;
+
+      if (p - start < 5)
+        goto skip;
+
+      value = g_ascii_strtod (p, (char **) &end2);
+
+      while (end2 && g_ascii_isspace (*end2)) end2++;
+
+      if (end2 && (*end2 != ',' && *end2 != '\0'))
+        goto skip;
+
+      g_string_append_printf (s, "%s\"%s\" %g", sep, name, value);
+      sep = ", ";
+
+skip:
+      p = end ? end + 1 : NULL;
+    }
+}
+
+/* Copied from gtkfontbutton.c */
+/* The only difference is the definition of `s`. */
+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;
+        default:
+          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;
+        case PANGO_VARIANT_ALL_SMALL_CAPS:
+          g_string_append (s, "font-variant: all-small-caps; ");
+          break;
+        case PANGO_VARIANT_PETITE_CAPS:
+          g_string_append (s, "font-variant: petite-caps; ");
+          break;
+        case PANGO_VARIANT_ALL_PETITE_CAPS:
+          g_string_append (s, "font-variant: all-petite-caps; ");
+          break;
+        case PANGO_VARIANT_UNICASE:
+          g_string_append (s, "font-variant: unicase; ");
+          break;
+        case PANGO_VARIANT_TITLE_CAPS:
+          g_string_append (s, "font-variant: titling-caps; ");
+          break;
+        default:
+          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;
+        default:
+          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:
+          break;
+        case PANGO_STRETCH_ULTRA_EXPANDED:
+          g_string_append (s, "font-stretch: ultra-expanded; ");
+          break;
+        default:
+          break;
+        }
+    }
+  if (set & PANGO_FONT_MASK_SIZE)
+    {
+      g_string_append_printf (s, "font-size: %dpt; ", pango_font_description_get_size (desc) / PANGO_SCALE);
+    }
+
+  if (set & PANGO_FONT_MASK_VARIATIONS)
+    {
+      const char *variations;
+
+      g_string_append (s, "font-variation-settings: ");
+      variations = pango_font_description_get_variations (desc);
+      add_css_variations (s, variations);
+      g_string_append (s, "; ");
+    }
+
+  g_string_append (s, "}");
+
+  return g_string_free (s, FALSE);
+}
diff --git a/src/gtr-utils.h b/src/gtr-utils.h
index bfa26636..0a411b92 100644
--- a/src/gtr-utils.h
+++ b/src/gtr-utils.h
@@ -78,4 +78,6 @@ void gtr_utils_menu_position_under_tree_view (GtkMenu * menu,
      gchar *gtr_utils_get_win32_plugindir (void);
 
      gchar * gtr_utils_get_filename (const gchar * filename);
+
+     gchar * pango_font_description_to_css (PangoFontDescription *desc);
 #endif
diff --git a/src/gtr-view.c b/src/gtr-view.c
index 3ba7421d..06a24eef 100644
--- a/src/gtr-view.c
+++ b/src/gtr-view.c
@@ -40,7 +40,6 @@
 #include <gtk/gtk.h>
 
 #include <gtksourceview/gtksource.h>
-#include <dazzle.h>
 
 #include <gspell/gspell.h>
 
@@ -923,8 +922,8 @@ gtr_view_set_font (GtrView *view, char *font)
 
   gtk_font_chooser_set_font (GTK_FONT_CHOOSER (button), font);
   font_desc = gtk_font_chooser_get_font_desc (GTK_FONT_CHOOSER (button));
-  str = dzl_pango_font_description_to_css (font_desc);
-  css = g_strdup_printf ("textview { %s }", str ?: "");
+  str = pango_font_description_to_css (font_desc);
+  css = g_strdup_printf ("textview  %s", str ?: "");
 
   gtk_css_provider_load_from_data (priv->provider, css, -1, NULL);
 


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