[tepl] pango: add tepl_pango_font_description_to_css()



commit 4be997287b0c97f9de183a91ca27049ff22cc48b
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Oct 29 20:52:46 2020 +0100

    pango: add tepl_pango_font_description_to_css()
    
    Copied from gedit-pango.[ch], which was copied from gtkfontbutton.c.
    
    It will be used in gedit.

 docs/reference/tepl-docs.xml     |   1 +
 docs/reference/tepl-sections.txt |   5 +
 po/POTFILES.in                   |   1 +
 tepl/meson.build                 |   2 +
 tepl/tepl-pango.c                | 223 +++++++++++++++++++++++++++++++++++++++
 tepl/tepl-pango.h                |  22 ++++
 tepl/tepl.h                      |   1 +
 7 files changed, 255 insertions(+)
---
diff --git a/docs/reference/tepl-docs.xml b/docs/reference/tepl-docs.xml
index 1c3f42d..a7dca6a 100644
--- a/docs/reference/tepl-docs.xml
+++ b/docs/reference/tepl-docs.xml
@@ -67,6 +67,7 @@
       <xi:include href="xml/info-bar.xml"/>
       <xi:include href="xml/iter.xml"/>
       <xi:include href="xml/panel.xml"/>
+      <xi:include href="xml/pango.xml"/>
       <xi:include href="xml/space-drawer-prefs.xml"/>
       <xi:include href="xml/statusbar.xml"/>
       <xi:include href="xml/style-scheme-chooser-widget.xml"/>
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 9400709..7a1f04d 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -328,6 +328,11 @@ TeplPanelPrivate
 tepl_panel_get_type
 </SECTION>
 
+<SECTION>
+<FILE>pango</FILE>
+tepl_pango_font_description_to_css
+</SECTION>
+
 <SECTION>
 <FILE>space-drawer-prefs</FILE>
 TeplSpaceDrawerPrefs
diff --git a/po/POTFILES.in b/po/POTFILES.in
index e3e549f..c89b4e0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -21,6 +21,7 @@ tepl/tepl-metadata-manager.c
 tepl/tepl-metadata-parser.c
 tepl/tepl-notebook.c
 tepl/tepl-panel.c
+tepl/tepl-pango.c
 tepl/tepl-signal-group.c
 tepl/tepl-space-drawer-prefs.c
 tepl/tepl-statusbar.c
diff --git a/tepl/meson.build b/tepl/meson.build
index 0075478..3bd5248 100644
--- a/tepl/meson.build
+++ b/tepl/meson.build
@@ -21,6 +21,7 @@ tepl_public_headers = [
   'tepl-metadata-manager.h',
   'tepl-notebook.h',
   'tepl-panel.h',
+  'tepl-pango.h',
   'tepl-space-drawer-prefs.h',
   'tepl-statusbar.h',
   'tepl-style-scheme-chooser-widget.h',
@@ -54,6 +55,7 @@ tepl_public_c_files = [
   'tepl-metadata-manager.c',
   'tepl-notebook.c',
   'tepl-panel.c',
+  'tepl-pango.c',
   'tepl-space-drawer-prefs.c',
   'tepl-statusbar.c',
   'tepl-style-scheme-chooser-widget.c',
diff --git a/tepl/tepl-pango.c b/tepl/tepl-pango.c
new file mode 100644
index 0000000..54e3ca8
--- /dev/null
+++ b/tepl/tepl-pango.c
@@ -0,0 +1,223 @@
+/* SPDX-FileCopyrightText: 2016 - Matthias Clasen <mclasen redhat com>
+ * SPDX-FileCopyrightText: 2020 - Sébastien Wilmet <swilmet gnome org>
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ */
+
+/**
+ * SECTION:pango
+ * @Title: TeplPango
+ * @Short_description: Pango utility functions
+ *
+ * Utility functions related to the Pango library.
+ */
+
+/* The code is taken from pango_font_description_to_css(), gtkfontbutton.c, GTK. */
+
+#include "tepl-pango.h"
+
+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;
+    }
+}
+
+/**
+ * tepl_pango_font_description_to_css:
+ * @desc: a #PangoFontDescription.
+ *
+ * This function will generate CSS suitable for the GTK CSS engine based on the
+ * properties of the #PangoFontDescription.
+ *
+ * The returned string contains only the CSS declarations, it is not a complete
+ * CSS rule set. So the selector and curly braces are not present. Each
+ * declaration, including the last one, ends with a semicolon.
+ *
+ * Returns: (transfer full): A newly allocated string containing the CSS
+ *   describing the font description. Free with g_free() when no longer needed.
+ * Since: 5.2
+ */
+gchar *
+tepl_pango_font_description_to_css (const 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;
+        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, "; ");
+    }
+
+  return g_string_free (s, FALSE);
+}
diff --git a/tepl/tepl-pango.h b/tepl/tepl-pango.h
new file mode 100644
index 0000000..3de65b8
--- /dev/null
+++ b/tepl/tepl-pango.h
@@ -0,0 +1,22 @@
+/* SPDX-FileCopyrightText: 2020 - Sébastien Wilmet <swilmet gnome org>
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ */
+
+#ifndef TEPL_PANGO_H
+#define TEPL_PANGO_H
+
+#if !defined (TEPL_H_INSIDE) && !defined (TEPL_COMPILATION)
+#error "Only <tepl/tepl.h> can be included directly."
+#endif
+
+#include <tepl/tepl-macros.h>
+#include <pango/pango.h>
+
+G_BEGIN_DECLS
+
+_TEPL_EXTERN
+gchar *                tepl_pango_font_description_to_css      (const PangoFontDescription *desc);
+
+G_END_DECLS
+
+#endif /* TEPL_PANGO_H */
diff --git a/tepl/tepl.h b/tepl/tepl.h
index 6c3e3fd..f2751b0 100644
--- a/tepl/tepl.h
+++ b/tepl/tepl.h
@@ -32,6 +32,7 @@
 #include <tepl/tepl-metadata-manager.h>
 #include <tepl/tepl-notebook.h>
 #include <tepl/tepl-panel.h>
+#include <tepl/tepl-pango.h>
 #include <tepl/tepl-space-drawer-prefs.h>
 #include <tepl/tepl-statusbar.h>
 #include <tepl/tepl-style-scheme-chooser-widget.h>


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