From 40886e00da4c593227cecf3ab574f9c5d230b089 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 22 Oct 2016 15:46:58 +0200
Subject: Reduced the memory usage by cutting down the size of GBufferSegment.

---
 ChangeLog                    |   6 ++
 src/glibext/gbuffersegment.c | 138 ++++++++++++++++---------------------------
 2 files changed, 58 insertions(+), 86 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 44b667f..78a5a8a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 16-10-22  Cyrille Bagard <nocbos@gmail.com>
 
+	* src/glibext/gbuffersegment.c:
+	Reduce the memory usage by cutting down the size of GBufferSegment from
+	152 bytes to 64 bytes.
+
+16-10-22  Cyrille Bagard <nocbos@gmail.com>
+
 	* src/gtkext/gtkbinarystrip.c:
 	* src/gui/editor.c:
 	* src/gui/panels/symbols.c:
diff --git a/src/glibext/gbuffersegment.c b/src/glibext/gbuffersegment.c
index 3cbff1e..43c710c 100644
--- a/src/glibext/gbuffersegment.c
+++ b/src/glibext/gbuffersegment.c
@@ -52,6 +52,7 @@ typedef struct _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          */
@@ -109,10 +110,6 @@ struct _GBufferSegment
 
     rendering_pattern_t *pattern;           /* Propriétés du rendu         */
 
-    rendering_color_t cache_bg;             /* Fond d'impression           */
-    rendering_color_t alt_fg;               /* Couleur d'impression bis    */
-    const rendering_color_t *used_fg;       /* Couleur d'impression utile  */
-
     gint x_advance;                         /* Dimensions du texte         */
 
 };
@@ -122,6 +119,8 @@ struct _GBufferSegmentClass
 {
     GObjectClass parent;                    /* A laisser en premier        */
 
+    rendering_color_t selection_bg;         /* Fond d'impression           */
+
     cairo_t *font_ctxts[CAIRO_FONTS_COUNT]; /* Contextes de police         */
     double x_advances[CAIRO_FONTS_COUNT];   /* Largeurs par caractère      */
     rendering_pattern_t patterns[RTT_COUNT];/* Modèles d'impression        */
@@ -236,6 +235,14 @@ static void g_buffer_segment_class_init(GBufferSegmentClass *class)
 
     g_free(filename);
 
+    /* Fond d'impression */
+
+    class->selection_bg.has_color = true;
+    class->selection_bg.color.red = 0.5;
+    class->selection_bg.color.green = 0.5;
+    class->selection_bg.color.blue = 0.5;
+    class->selection_bg.color.alpha = 1.0;
+
     /* Chargement des définitions utiles */
 
     void define_rendering_pattern(GtkStyleContext *ctx, const char *name, rendering_pattern_t *pattern)
@@ -248,8 +255,16 @@ static void g_buffer_segment_class_init(GBufferSegmentClass *class)
         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);
@@ -435,13 +450,6 @@ static void g_buffer_segment_prepare(GBufferSegment *segment, size_t length)
 
     segment->x_advance = class->x_advances[CAIRO_FONT_INDEX(slant, weight)] * length;
 
-    /* Couleurs */
-
-    segment->alt_fg.color.red = 1.0 - segment->pattern->foreground.color.red;
-    segment->alt_fg.color.green = 1.0 - segment->pattern->foreground.color.green;
-    segment->alt_fg.color.blue = 1.0 - segment->pattern->foreground.color.blue;
-    segment->alt_fg.has_color = segment->pattern->foreground.has_color;
-
 }
 
 
@@ -770,7 +778,7 @@ bool g_buffer_segment_move_caret(const GBufferSegment *segment, gint *x, bool ct
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : segment = fragment de texte à manipuler.                     *
-*                cairo   = contexte graphique à utiliser pour les pinceaux.   *
+*                cr      = contexte graphique à utiliser pour les pinceaux.   *
 *                x       = abscisse du point d'impression (à maj). [OUT]      *
 *                y       = ordonnée du point d'impression.                    *
 *                list    = liste de contenus à mettre en évidence.            *
@@ -783,101 +791,59 @@ bool g_buffer_segment_move_caret(const GBufferSegment *segment, gint *x, bool ct
 *                                                                             *
 ******************************************************************************/
 
-void g_buffer_segment_draw(GBufferSegment *segment, cairo_t *cairo, gint *x, gint y, const segcnt_list *list)
+void g_buffer_segment_draw(GBufferSegment *segment, cairo_t *cr, gint *x, gint y, const segcnt_list *list)
 {
     bool selected;                          /* Marquer une sélection ?     */
-
-    cairo_font_extents_t extents;
-
-
-    /*
-    switch (style)
-    {
-        default:
-        case SRS_CLASSIC:
-            segment->used_fg = &segment->pattern->foreground;
-            break;
-
-        case SRS_HIGHLIGHT_SAME:
-
-            segment->cache_bg.color.red = 0.5;
-            segment->cache_bg.color.green = 0.5;
-            segment->cache_bg.color.blue = 0.5;
-
-            segment->used_fg = &segment->alt_fg;
-
-            break;
-
-    }
-    */
-
-
-    segment->used_fg = &segment->pattern->foreground;
-
+    GBufferSegmentClass *class;             /* Accès aux infos globales    */
+    const rendering_color_t *used_fg;       /* Couleur d'impression utile  */
+    cairo_operator_t old;                   /* Sauvegarde avant changement */
 
     selected = selection_list_has_segment_content(list, segment);
 
-
-    if (selected)
-    {
-        segment->cache_bg.color.red = 0.5;
-        segment->cache_bg.color.green = 0.5;
-        segment->cache_bg.color.blue = 0.5;
-
-        segment->used_fg = &segment->alt_fg;
-
-    }
-
-
     /* Fond du texte */
     if (selected)
     {
-        cairo_set_source_rgb(cairo,
-                             segment->cache_bg.color.red,
-                             segment->cache_bg.color.green,
-                             segment->cache_bg.color.blue);
+        class = G_BUFFER_SEGMENT_GET_CLASS(segment);
+
+        cairo_set_source_rgba(cr,
+                              class->selection_bg.color.red,
+                              class->selection_bg.color.green,
+                              class->selection_bg.color.blue,
+                              class->selection_bg.color.alpha);
 
-        cairo_rectangle(cairo, *x, y, segment->x_advance, 17);
+        cairo_rectangle(cr, *x, y, segment->x_advance, 17);
 
-        cairo_set_operator(cairo, CAIRO_OPERATOR_DIFFERENCE);
-        cairo_fill(cairo);
-        cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);
+        old = cairo_get_operator(cr);
+        cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
+        cairo_fill(cr);
+        cairo_set_operator(cr, old);
 
     }
 
     /* Couleur d'impression */
 
-    if (segment->used_fg->has_color)
-        cairo_set_source_rgb(cairo,
-                             segment->used_fg->color.red,
-                             segment->used_fg->color.green,
-                             segment->used_fg->color.blue);
+    if (selected)
+        used_fg = &segment->pattern->inverted;
+    else
+        used_fg = &segment->pattern->foreground;
+
+    if (used_fg->has_color)
+        cairo_set_source_rgba(cr,
+                              used_fg->color.red,
+                              used_fg->color.green,
+                              used_fg->color.blue,
+                              used_fg->color.alpha);
     else
-        cairo_set_source_rgb(cairo, 0, 0, 0);
+        cairo_set_source_rgb(cr, 0, 0, 0);
 
     /* Impression du texte */
 
-    cairo_select_font_face(cairo, "mono", segment->pattern->slant, segment->pattern->weight);
-    cairo_set_font_size(cairo, 13);
-
-
-
-
-    cairo_move_to(cairo, *x, y + 17 - 3);
-
-
-
-
-    cairo_font_extents(cairo, &extents);
-
-#if 1
-    if (extents.descent != 3)
-    printf("FONT : %g, %g\n", extents.ascent, extents.descent);
-#endif
+    cairo_select_font_face(cr, "mono", segment->pattern->slant, segment->pattern->weight);
+    cairo_set_font_size(cr, 13);
 
-    cairo_show_text(cairo, segment->text);
+    cairo_move_to(cr, *x, y + 17 - 3);  /* 3 = font extents.descent */
 
-    //printf(">> %s >> %f %f\n", segment->text, segment->extents.width, segment->extents.x_advance);
+    cairo_show_text(cr, segment->text);
 
     *x += segment->x_advance;
 
-- 
cgit v0.11.2-87-g4458