From cc79dc6e2bbf78acd146b618df246a7936a0e4ae Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Wed, 18 Jul 2018 18:26:37 +0200
Subject: Built Pango markups from CSS theme items.

---
 src/glibext/linesegment.c | 100 +----------------------
 src/gtkext/Makefile.am    |   1 +
 src/gtkext/rendering.c    | 204 ++++++++++++++++++++++++++++++++++++++++++++++
 src/gtkext/rendering.h    |  65 +++++++++++++++
 4 files changed, 272 insertions(+), 98 deletions(-)
 create mode 100644 src/gtkext/rendering.c
 create mode 100644 src/gtkext/rendering.h

diff --git a/src/glibext/linesegment.c b/src/glibext/linesegment.c
index 373d744..02c6691 100644
--- a/src/glibext/linesegment.c
+++ b/src/glibext/linesegment.c
@@ -36,6 +36,7 @@
 #include "../common/extstr.h"
 #include "../common/fnv1a.h"
 #include "../core/paths.h"
+#include "../gtkext/rendering.h"
 
 
 
@@ -88,24 +89,6 @@ static const char *_segment_names[RTT_COUNT] = {
 
 
 /* Propriétés de rendu */
-
-typedef struct _rendering_color_t
-{
-    GdkRGBA color;                          /* Couleur de rendu            */
-    bool has_color;                         /* Définition en place ?       */
-
-} rendering_color_t;
-
-typedef struct _rendering_pattern_t
-{
-    rendering_color_t foreground;           /* Couleur d'impression        */
-    rendering_color_t inverted;             /* Couleur inversée pour sél.  */
-
-    cairo_font_slant_t slant;               /* Style d'impression          */
-    cairo_font_weight_t weight;             /* Poids de la police          */
-
-} rendering_pattern_t;
-
 typedef struct _segment_rendering
 {
     rendering_color_t selection_bg;         /* Fond d'impression           */
@@ -192,8 +175,6 @@ bool selection_list_has_segment_content(const segcnt_list *, const line_segment
 
 bool load_segment_rendering_parameters(void)
 {
-    GtkStyleContext *context;               /* Contexte pour les styles    */
-    GtkWidgetPath *path;                    /* Chemin d'accès aux thèmes   */
     gchar *filename;                        /* Accès à une image 1x1       */
     cairo_font_slant_t s;                   /* Boucle de parcours #1       */
     cairo_font_weight_t w;                  /* Boucle de parcours #2       */
@@ -202,15 +183,6 @@ bool load_segment_rendering_parameters(void)
     cairo_text_extents_t extents;           /* Couverture des caractères   */
     RenderingTagType i;                     /* Boucle de parcours          */
 
-    /* Création d'un contexte d'accès */
-
-    path = gtk_widget_path_new();
-    gtk_widget_path_append_type(path, G_TYPE_OBJECT);
-
-    context = gtk_style_context_new();
-    gtk_style_context_set_path(context, path);
-    gtk_style_context_set_screen(context, gdk_screen_get_default());
-
     /* Contextes pour les mesures initiales */
 
     filename = find_pixmap_file("nil.png");
@@ -245,76 +217,8 @@ bool load_segment_rendering_parameters(void)
 
     /* Chargement des définitions utiles */
 
-    void define_rendering_pattern(GtkStyleContext *ctx, const char *name, rendering_pattern_t *pattern)
-    {
-        GdkRGBA *tmp_color;                 /* Description d'une couleur   */
-        PangoFontDescription *font_desc;    /* Description d'une police    */
-
-        gtk_style_context_save(ctx);
-
-        gtk_style_context_add_class(context, name);
-
-        gtk_style_context_get(ctx, GTK_STATE_NORMAL, GTK_STYLE_PROPERTY_COLOR, &tmp_color, NULL);
-
-        pattern->foreground.has_color = true;
-        pattern->foreground.color = *tmp_color;
-
-        pattern->inverted.has_color = true;
-        pattern->inverted.color.red = 1.0 - tmp_color->red;
-        pattern->inverted.color.green = 1.0 - tmp_color->green;
-        pattern->inverted.color.blue = 1.0 - tmp_color->blue;
-        pattern->inverted.color.alpha = tmp_color->alpha;
-
-        gdk_rgba_free(tmp_color);
-
-        gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_FONT, &font_desc, NULL);
-
-        switch (pango_font_description_get_style(font_desc))
-        {
-            case PANGO_STYLE_NORMAL:
-                pattern->slant = CAIRO_FONT_SLANT_NORMAL;
-                break;
-            case PANGO_STYLE_ITALIC:
-                pattern->slant = CAIRO_FONT_SLANT_ITALIC;
-                break;
-            case PANGO_STYLE_OBLIQUE:
-                pattern->slant = CAIRO_FONT_SLANT_OBLIQUE;
-                break;
-        }
-
-        switch (pango_font_description_get_weight(font_desc))
-        {
-            case PANGO_WEIGHT_THIN:
-            case PANGO_WEIGHT_ULTRALIGHT:	
-            case PANGO_WEIGHT_LIGHT:
-            case PANGO_WEIGHT_SEMILIGHT:
-            case PANGO_WEIGHT_BOOK:
-            case PANGO_WEIGHT_NORMAL:
-            case PANGO_WEIGHT_MEDIUM:
-                pattern->weight = CAIRO_FONT_WEIGHT_NORMAL;
-                break;
-            case PANGO_WEIGHT_SEMIBOLD:
-            case PANGO_WEIGHT_BOLD:
-            case PANGO_WEIGHT_ULTRABOLD:
-            case PANGO_WEIGHT_HEAVY:
-            case PANGO_WEIGHT_ULTRAHEAVY:
-                pattern->weight = CAIRO_FONT_WEIGHT_BOLD;
-                break;
-        }
-
-        pango_font_description_free(font_desc);
-
-        gtk_style_context_restore(context);
-
-    }
-
     for (i = 0; i < RTT_COUNT; i++)
-        define_rendering_pattern(context, _segment_names[i], &_seg_params.patterns[i]);
-
-    /* Nettoyages finaux... */
-
-    gtk_widget_path_free(path);
-    g_object_unref(context);
+        load_rendering_pattern(_segment_names[i], &_seg_params.patterns[i]);
 
     return true;
 
diff --git a/src/gtkext/Makefile.am b/src/gtkext/Makefile.am
index 45719d2..42905fb 100644
--- a/src/gtkext/Makefile.am
+++ b/src/gtkext/Makefile.am
@@ -14,6 +14,7 @@ libgtkext_la_SOURCES =						\
 	gtkdockstation.h gtkdockstation.c		\
 	gtkgraphdisplay.h gtkgraphdisplay.c		\
 	gtkstatusstack.h gtkstatusstack.c		\
+	rendering.h rendering.c					\
 	support.h support.c						\
 	tmgt.h tmgt.c
 
diff --git a/src/gtkext/rendering.c b/src/gtkext/rendering.c
new file mode 100644
index 0000000..bb86791
--- /dev/null
+++ b/src/gtkext/rendering.c
@@ -0,0 +1,204 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * rendering.h - prototypes pour la transformation de paramètres du thème GTK courant
+ *
+ * Copyright (C) 2018 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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.
+ *
+ *  Chrysalide 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, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "rendering.h"
+
+
+#include <malloc.h>
+#include <stdio.h>
+#include <string.h>
+
+
+#include "../common/extstr.h"
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : name    = désignation d'un élément dans une feuille de style.*
+*                pattern = paramètres restitués en interne. |OUT]             *
+*                                                                             *
+*  Description : Récupère les informations de rendus d'un élément de thème.   *
+*                                                                             *
+*  Retour      : -                                                            *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+void load_rendering_pattern(const char *name, rendering_pattern_t *pattern)
+{
+    GtkStyleContext *context;               /* Contexte pour les styles    */
+    GtkWidgetPath *path;                    /* Chemin d'accès aux thèmes   */
+    GdkRGBA *tmp_color;                     /* Description d'une couleur   */
+    PangoFontDescription *font_desc;        /* Description d'une police    */
+
+    /* Création d'un contexte d'accès */
+
+    path = gtk_widget_path_new();
+    gtk_widget_path_append_type(path, G_TYPE_OBJECT);
+
+    context = gtk_style_context_new();
+    gtk_style_context_set_path(context, path);
+    gtk_style_context_set_screen(context, gdk_screen_get_default());
+
+    gtk_style_context_add_class(context, name);
+
+    gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_COLOR, &tmp_color, NULL);
+
+    pattern->foreground.has_color = true;
+    pattern->foreground.color = *tmp_color;
+
+    pattern->inverted.has_color = true;
+    pattern->inverted.color.red = 1.0 - tmp_color->red;
+    pattern->inverted.color.green = 1.0 - tmp_color->green;
+    pattern->inverted.color.blue = 1.0 - tmp_color->blue;
+    pattern->inverted.color.alpha = tmp_color->alpha;
+
+    gdk_rgba_free(tmp_color);
+
+    gtk_style_context_get(context, GTK_STATE_FLAG_NORMAL, GTK_STYLE_PROPERTY_FONT, &font_desc, NULL);
+
+    switch (pango_font_description_get_style(font_desc))
+    {
+        case PANGO_STYLE_NORMAL:
+            pattern->slant = CAIRO_FONT_SLANT_NORMAL;
+            break;
+        case PANGO_STYLE_ITALIC:
+            pattern->slant = CAIRO_FONT_SLANT_ITALIC;
+            break;
+        case PANGO_STYLE_OBLIQUE:
+            pattern->slant = CAIRO_FONT_SLANT_OBLIQUE;
+            break;
+    }
+
+    switch (pango_font_description_get_weight(font_desc))
+    {
+        case PANGO_WEIGHT_THIN:
+        case PANGO_WEIGHT_ULTRALIGHT:	
+        case PANGO_WEIGHT_LIGHT:
+        case PANGO_WEIGHT_SEMILIGHT:
+        case PANGO_WEIGHT_BOOK:
+        case PANGO_WEIGHT_NORMAL:
+        case PANGO_WEIGHT_MEDIUM:
+            pattern->weight = CAIRO_FONT_WEIGHT_NORMAL;
+            break;
+        case PANGO_WEIGHT_SEMIBOLD:
+        case PANGO_WEIGHT_BOLD:
+        case PANGO_WEIGHT_ULTRABOLD:
+        case PANGO_WEIGHT_HEAVY:
+        case PANGO_WEIGHT_ULTRAHEAVY:
+            pattern->weight = CAIRO_FONT_WEIGHT_BOLD;
+            break;
+    }
+
+    pango_font_description_free(font_desc);
+
+    gtk_widget_path_free(path);
+    g_object_unref(context);
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : text    = texte à encadrer par des balises Pango.            *
+*                pattern = paramètres restitués en interne.                   *
+*                                                                             *
+*  Description : Enjolive du texte selon les paramètres d'un élément de thème.*
+*                                                                             *
+*  Retour      : Chaîne de caractère à libérer après usage.                   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+char *build_pango_markup_for(const char *text, const rendering_pattern_t *pattern)
+{
+    char *result;                           /* Construction à retourner    */
+    char color[10];                         /* Définition hexa de couleur  */
+
+    result = strdup(text);
+
+    if (pattern->foreground.has_color)
+    {
+        snprintf(color, sizeof(color), "#%02hhx%02hhx%02hhx%02hhx",
+                (unsigned char)(255 * pattern->foreground.color.red),
+                (unsigned char)(255 * pattern->foreground.color.green),
+                (unsigned char)(255 * pattern->foreground.color.blue),
+                (unsigned char)(255 * pattern->foreground.color.alpha));
+
+        result = strprep(result, "\">");
+        result = strprep(result, color);
+        result = strprep(result, "<span color=\"");
+
+        result = stradd(result, "</span>");
+
+    }
+
+    if (pattern->slant == CAIRO_FONT_SLANT_ITALIC || pattern->slant == CAIRO_FONT_SLANT_OBLIQUE)
+    {
+        result = strprep(result, "<i>");
+        result = stradd(result, "</i>");
+    }
+
+    if (pattern->weight == CAIRO_FONT_WEIGHT_BOLD)
+    {
+        result = strprep(result, "<b>");
+        result = stradd(result, "</b>");
+    }
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : base    = base de texte à compléter.                         *
+*                text    = texte à encadrer par des balises Pango.            *
+*                pattern = paramètres restitués en interne.                   *
+*                                                                             *
+*  Description : Ajoute du texte enjolivé selon un élément de thème.          *
+*                                                                             *
+*  Retour      : Chaîne de caractère à libérer après usage.                   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+char *append_pango_markup_with(char *base, const char *text, const rendering_pattern_t *pattern)
+{
+    char *result;                           /* Construction à retourner    */
+    char *tmp;                              /* Stockage temporaire         */
+
+    tmp = build_pango_markup_for(text, pattern);
+
+    result = stradd(base, tmp);
+
+    free(tmp);
+
+    return result;
+
+}
diff --git a/src/gtkext/rendering.h b/src/gtkext/rendering.h
new file mode 100644
index 0000000..d80c096
--- /dev/null
+++ b/src/gtkext/rendering.h
@@ -0,0 +1,65 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * rendering.h - prototypes pour la transformation de paramètres du thème GTK courant
+ *
+ * Copyright (C) 2018 Cyrille Bagard
+ *
+ *  This file is part of Chrysalide.
+ *
+ *  Chrysalide 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.
+ *
+ *  Chrysalide 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, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _GTKEXT_RENDERING_H
+#define _GTKEXT_RENDERING_H
+
+
+#include <stdbool.h>
+#include <gtk/gtk.h>
+
+
+
+/* Restitution d'une couleur */
+typedef struct _rendering_color_t
+{
+    GdkRGBA color;                          /* Couleur de rendu            */
+    bool has_color;                         /* Définition en place ?       */
+
+} rendering_color_t;
+
+/* Restitution d'un élément de thème */
+typedef struct _rendering_pattern_t
+{
+    rendering_color_t foreground;           /* Couleur d'impression        */
+    rendering_color_t inverted;             /* Couleur inversée pour sél.  */
+
+    cairo_font_slant_t slant;               /* Style d'impression          */
+    cairo_font_weight_t weight;             /* Poids de la police          */
+
+} rendering_pattern_t;
+
+
+/* Récupère les informations de rendus d'un élément de thème. */
+void load_rendering_pattern(const char *, rendering_pattern_t *);
+
+/* Enjolive du texte selon les paramètres d'un élément de thème. */
+char *build_pango_markup_for(const char *, const rendering_pattern_t *);
+
+/* Ajoute du texte enjolivé selon un élément de thème. */
+char *append_pango_markup_with(char *, const char *, const rendering_pattern_t *);
+
+
+
+#endif  /* _GTKEXT_RENDERING_H */
-- 
cgit v0.11.2-87-g4458