summaryrefslogtreecommitdiff
path: root/src/glibext/gbuffersegment.c
blob: eb7bd3102a76175213299dfff0bfc478cdfbb9bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513

/* Chrysalide - Outil d'analyse de fichiers binaires
 * gbuffersegment.c - concentration d'un fragment de caractères aux propriétés communes
 *
 * Copyright (C) 2010-2014 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "gbuffersegment.h"


#include <stdbool.h>
#include <stdlib.h>
#include <string.h>


#include "../common/fnv1a.h"
#include "../gtkext/gtkblockview.h"
#include "../gtkext/support.h"



/* Utilisation du champ pixel des couleurs cachées */
#define COLOR_NOT_SET   0
#define COLOR_SET       1

/* Propriétés de rendu */
typedef struct _rendering_pattern_t
{
    GdkColor foreground;                    /* Couleur d'impression        */

    cairo_font_slant_t slant;               /* Style d'impression          */
    cairo_font_weight_t weight;             /* Poids de la police          */

} rendering_pattern_t;

/* Compléments à Cairo */

#define CAIRO_FONT_SLANT_COUNT  3
#define CAIRO_FONT_WEIGHT_COUNT 2

#define CAIRO_FONTS_COUNT (CAIRO_FONT_SLANT_COUNT * CAIRO_FONT_WEIGHT_COUNT)
#define CAIRO_FONT_INDEX(s, w) ((s) * CAIRO_FONT_SLANT_COUNT + (w))

/* Fragment de caractères aux propriétés communes (instance) */
struct _GBufferSegment
{
    GObject parent;                         /* A laisser en premier        */

    char *text;                             /* Texte brut conservé         */
    fnv64_t hash;                           /* Empreinte pour comparaisons */

    rendering_pattern_t *pattern;           /* Propriétés du rendu         */

    SegRenderingStyle style;                /* Apparence du segment        */

    GdkColor cache_bg;                      /* Fond d'impression           */
    GdkColor alt_fg;                        /* Couleur d'impression bis    */
    GdkColor *used_fg;                      /* Couleur d'impression utile  */

    cairo_text_extents_t extents;           /* Dimensions du texte         */

};

/* Fragment de caractères aux propriétés communes (classe) */
struct _GBufferSegmentClass
{
    GObjectClass parent;                    /* A laisser en premier        */

    cairo_t *font_ctxts[CAIRO_FONTS_COUNT]; /* Contextes de police         */
    rendering_pattern_t patterns[RTT_COUNT];/* Modèles d'impression        */

};


/* Procède à l'initialisation d'une classe de fragment de texte. */
static void g_buffer_segment_class_init(GBufferSegmentClass *);

/* Procède à l'initialisation d'un fragment de texte. */
static void g_buffer_segment_init(GBufferSegment *);

/* Définit les dernières propriétés de rendu restantes. */
static void g_buffer_segment_prepare(GBufferSegment *, const char *);



/* Détermine le type du fragment de caractères aux propriétés communes. */
G_DEFINE_TYPE(GBufferSegment, g_buffer_segment, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe de composant GTK à initialiser.               *
*                                                                             *
*  Description : Procède à l'initialisation d'une classe de fragment de texte.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_buffer_segment_class_init(GBufferSegmentClass *class)
{
    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       */
    cairo_t **cr;                           /* Contexte à créer            */
    cairo_surface_t *surface;               /* Surface pour dessin Cairo   */

    /* Contextes pour les mesures initiales */

    filename = find_pixmap_file("nil.png");
    if (filename == NULL) abort();

    for (s = CAIRO_FONT_SLANT_NORMAL; s < CAIRO_FONT_SLANT_COUNT; s++)
        for (w = CAIRO_FONT_WEIGHT_NORMAL; w < CAIRO_FONT_WEIGHT_COUNT; w++)
        {
            cr = &class->font_ctxts[CAIRO_FONT_INDEX(s, w)];

            surface = cairo_image_surface_create_from_png(filename);
            *cr = cairo_create(surface);
            cairo_surface_destroy(surface);

            cairo_select_font_face(*cr, "mono", s, w);
            cairo_set_font_size(*cr, 13);

        }

    g_free(filename);

    /* Propriétés d'impression */

#define GET_RESET_PATTERN(tp)                           \
    ({                                                  \
        rendering_pattern_t *__p;                       \
        __p = &class->patterns[RTT_ ## tp];             \
        memset(__p, 0, sizeof(rendering_pattern_t));    \
        __p;                                            \
    })

#define DEFINE_SIMPLE_PATTERN(rtt)                      \
    do                                                  \
    {                                                   \
        rendering_pattern_t *__ptn;                     \
        __ptn = GET_RESET_PATTERN(rtt);                 \
        __ptn->slant = CAIRO_FONT_SLANT_NORMAL;         \
        __ptn->weight = CAIRO_FONT_WEIGHT_NORMAL;       \
        __ptn->foreground.pixel = COLOR_NOT_SET;        \
    }                                                   \
    while (0)

#define DEFINE_PATTERN(rtt, s, w, r, g, b)              \
    do                                                  \
    {                                                   \
        rendering_pattern_t *__ptn;                     \
        __ptn = GET_RESET_PATTERN(rtt);                 \
        __ptn->slant = CAIRO_FONT_SLANT_ ## s;          \
        __ptn->weight = CAIRO_FONT_WEIGHT_ ## w;        \
        __ptn->foreground.red = r;                      \
        __ptn->foreground.green = g;                    \
        __ptn->foreground.blue = b;                     \
        __ptn->foreground.pixel = COLOR_SET;            \
    }                                                   \
    while (0)

    DEFINE_PATTERN(RAW,         NORMAL, NORMAL, 0, 0, 0);
    DEFINE_PATTERN(COMMENT,     NORMAL, NORMAL, 14335, 45311, 23551);
    DEFINE_PATTERN(INDICATION,  ITALIC, NORMAL, 33410, 33410, 33410);
    DEFINE_PATTERN(RAW_CODE,    NORMAL, NORMAL, 48895, 48895, 48895);
    DEFINE_PATTERN(INSTRUCTION, NORMAL, NORMAL, 0, 0, 0);
    DEFINE_PATTERN(IMMEDIATE,   NORMAL, NORMAL, 41215, 8447, 61695);
    DEFINE_PATTERN(REGISTER,    NORMAL, NORMAL, 16895, 16895, 53759);
    DEFINE_PATTERN(PUNCT,       NORMAL, BOLD,   0, 0, 0);
    DEFINE_PATTERN(HOOK,        NORMAL, BOLD,   0, 0, 0);
    DEFINE_PATTERN(SIGNS,       NORMAL, BOLD,   0, 0, 0);
    DEFINE_SIMPLE_PATTERN(LTGT);
    DEFINE_PATTERN(SECTION,     NORMAL, NORMAL, 51200, 2560, 2560);
    DEFINE_SIMPLE_PATTERN(SEGMENT);
    DEFINE_PATTERN(STRING,      NORMAL, NORMAL, 52224, 32256, 0);
    DEFINE_PATTERN(VAR_NAME,    NORMAL, NORMAL, 0, 0, 0);
    DEFINE_PATTERN(KEY_WORD,    NORMAL, NORMAL, 0, 0, 0);
    DEFINE_PATTERN(ERROR,       NORMAL, BOLD,   65535, 0, 0);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = composant GTK à initialiser.                       *
*                                                                             *
*  Description : Procède à l'initialisation d'un fragment de texte.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_buffer_segment_init(GBufferSegment *segment)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : type   = propriétés de la zone de texte.                     *
*                text   = chaîne de caractères à traiter.                     *
*                length = quantité de ces caractères.                         *
*                                                                             *
*  Description : Crée un nouveau fragment de texte avec des propriétés.       *
*                                                                             *
*  Retour      : Composant GTK créé.                                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GBufferSegment *g_buffer_segment_new(RenderingTagType type, const char *text, size_t length)
{
    GBufferSegment *result;                 /* Composant à retourner       */
    GBufferSegmentClass *class;             /* Stockage de styles préparés */ 

    result = g_object_new(G_TYPE_BUFFER_SEGMENT, NULL);

    result->text = strdup(text);
    result->hash = fnv_64a_hash(text);

    class = G_BUFFER_SEGMENT_GET_CLASS(result);
    result->pattern = &class->patterns[type];

    g_buffer_segment_prepare(result, text);

    g_buffer_segment_set_style(result, SRS_CLASSIC);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = instance de segment à affiner.                     *
*                text    = chaîne de caractères à traiter.                    *
*                                                                             *
*  Description : Définit les dernières propriétés de rendu restantes.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_buffer_segment_prepare(GBufferSegment *segment, const char *text)
{
    GBufferSegmentClass *class;             /* Classe associée à l'instance*/
    cairo_font_slant_t slant;               /* Style d'impression          */
    cairo_font_weight_t weight;             /* Poids de la police          */
    cairo_t *cr;                            /* Contexte de rendu           */

    /* Taille */

    class = G_BUFFER_SEGMENT_GET_CLASS(segment);

    slant = segment->pattern->slant;
    weight = segment->pattern->weight;

    cr = class->font_ctxts[CAIRO_FONT_INDEX(slant, weight)];

    cairo_text_extents(cr, text, &segment->extents);

    /* Couleurs */

    segment->alt_fg.red = 65535 - segment->pattern->foreground.red;
    segment->alt_fg.green = 65535 - segment->pattern->foreground.green;
    segment->alt_fg.blue = 65535 - segment->pattern->foreground.blue;
    segment->alt_fg.pixel = segment->pattern->foreground.pixel;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à consulter.                     *
*                ref     = segment de référence servant à la comparaison.     *
*                                                                             *
*  Description : Indique si les textes de deux segments sont identiques.      *
*                                                                             *
*  Retour      : Bilan de la comparaison.                                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_buffer_segment_compare(const GBufferSegment *segment, const GBufferSegment *ref)
{
    bool result;                            /* Bilan à retourner           */

    result = cmp_fnv_64a(segment->hash, ref->hash);

    result &= (strcmp(segment->text, ref->text) == 0);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à consulter.                     *
*                                                                             *
*  Description : Fournit le texte brut conservé dans le segment.              *
*                                                                             *
*  Retour      : Texte conservé en interne.                                   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *g_buffer_segment_get_text(const GBufferSegment *segment)
{
    return segment->text;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à consulter.                     *
*                                                                             *
*  Description : Fournit la quantité de pixels requise pour l'impression.     *
*                                                                             *
*  Retour      : Largeur requise par la colonne, en pixel.                    *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

gint g_buffer_segment_get_width(const GBufferSegment *segment)
{
    return segment->extents.x_advance;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à consulter.                     *
*                x       = position horizontale au niveau du segment.         *
*                                                                             *
*  Description : Fournit la position idéale pour un marqueur.                 *
*                                                                             *
*  Retour      : Position dans le segment donné.                              *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

gint g_buffer_segment_get_caret_position(const GBufferSegment *segment, gint x)
{
    gint result;                            /* Position à retourner        */
    gint width;                             /* Largeur du segment          */
    gint char_width;                        /* Largeur de police fixe      */

    width = g_buffer_segment_get_width(segment);

    if (x <= 0)
        result = 0;

    else if (x >= width)
        result = width;

    else
    {
        char_width = width / strlen(segment->text);

        result = (x / char_width) * char_width;
        if ((x % char_width) > (char_width / 2))
            result += char_width;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à manipuler.                     *
*                style   = style de rendu pour le segment.                    *
*                                                                             *
*  Description : Module l'apparence finale du composant.                      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_buffer_segment_set_style(GBufferSegment *segment, SegRenderingStyle style)
{
    segment->style = style;

    switch (style)
    {
        default:
        case SRS_CLASSIC:
            segment->used_fg = &segment->pattern->foreground;
            break;

        case SRS_HIGHLIGHT_SAME:

            segment->cache_bg.red = 32768;
            segment->cache_bg.green = 32768;
            segment->cache_bg.blue = 32768;

            segment->used_fg = &segment->alt_fg;

            break;

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : segment = fragment de texte à manipuler.                     *
*                cairo   = contexte graphique à utiliser pour les pinceaux.   *
*                x       = abscisse du point d'impression (à maj). [OUT]      *
*                y       = ordonnée du point d'impression.                    *
*                                                                             *
*  Description : Imprime le fragment de texte représenté.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_buffer_segment_draw(GBufferSegment *segment, cairo_t *cairo, gint *x, gint y)
{

    cairo_font_extents_t extents;



    /* Fond du texte */
    if (segment->style != SRS_CLASSIC || 1)
    {
        cairo_set_source_rgb(cairo,
                             segment->cache_bg.red / 65535.0,
                             segment->cache_bg.green / 65535.0,
                             segment->cache_bg.blue / 65535.0);

        cairo_rectangle(cairo, *x, y, segment->extents.x_advance, 17);

        cairo_set_operator(cairo, CAIRO_OPERATOR_DIFFERENCE);
        cairo_fill(cairo);
        cairo_set_operator(cairo, CAIRO_OPERATOR_OVER);

    }

    /* Couleur d'impression */

    if (segment->used_fg->pixel == COLOR_SET)
        cairo_set_source_rgb(cairo,
                             segment->used_fg->red / 65535.0,
                             segment->used_fg->green / 65535.0,
                             segment->used_fg->blue / 65535.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 (extents.descent != 3)
    printf("FONT : %g, %g\n", extents.ascent, extents.descent);

    cairo_show_text(cairo, segment->text);

    //printf(">> %s >> %f %f\n", segment->text, segment->extents.width, segment->extents.x_advance);

    *x += segment->extents.x_advance;

}