summaryrefslogtreecommitdiff
path: root/pango/pango-attr.c
blob: 5794e7c73a972e7abc414b20adcbde152a109667 (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* Pango
 * pango-attr.c: Attributed text
 *
 * Copyright (C) 2000-2002 Red Hat Software
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <string.h>

#include "pango-attr-private.h"
#include "pango-impl-utils.h"


/* {{{ Generic attribute code */

G_LOCK_DEFINE_STATIC (attr_type);
static GArray *attr_type;

#define MIN_CUSTOM 1000

typedef struct _PangoAttrClass PangoAttrClass;

struct _PangoAttrClass {
  guint type;
  const char *name;
  PangoAttrDataCopyFunc copy;
  GDestroyNotify destroy;
  GEqualFunc equal;
  PangoAttrDataSerializeFunc serialize;
};

static const char *
get_attr_type_nick (PangoAttrType type)
{
  GEnumClass *enum_class;
  GEnumValue *enum_value;

  enum_class = g_type_class_ref (pango_attr_type_get_type ());
  enum_value = g_enum_get_value (enum_class, type);
  g_type_class_unref (enum_class);

  return enum_value->value_nick;
}

static gboolean
is_valid_attr_type (guint type)
{
  switch (type)
    {
    case PANGO_ATTR_INVALID:
      return FALSE;
    case PANGO_ATTR_LANGUAGE:
    case PANGO_ATTR_FAMILY:
    case PANGO_ATTR_STYLE:
    case PANGO_ATTR_WEIGHT:
    case PANGO_ATTR_VARIANT:
    case PANGO_ATTR_STRETCH:
    case PANGO_ATTR_SIZE:
    case PANGO_ATTR_FONT_DESC:
    case PANGO_ATTR_FOREGROUND:
    case PANGO_ATTR_BACKGROUND:
    case PANGO_ATTR_UNDERLINE:
    case PANGO_ATTR_STRIKETHROUGH:
    case PANGO_ATTR_RISE:
    case PANGO_ATTR_SCALE:
    case PANGO_ATTR_FALLBACK:
    case PANGO_ATTR_LETTER_SPACING:
    case PANGO_ATTR_UNDERLINE_COLOR:
    case PANGO_ATTR_STRIKETHROUGH_COLOR:
    case PANGO_ATTR_ABSOLUTE_SIZE:
    case PANGO_ATTR_GRAVITY:
    case PANGO_ATTR_GRAVITY_HINT:
    case PANGO_ATTR_FONT_FEATURES:
    case PANGO_ATTR_FOREGROUND_ALPHA:
    case PANGO_ATTR_BACKGROUND_ALPHA:
    case PANGO_ATTR_ALLOW_BREAKS:
    case PANGO_ATTR_SHOW:
    case PANGO_ATTR_INSERT_HYPHENS:
    case PANGO_ATTR_OVERLINE:
    case PANGO_ATTR_OVERLINE_COLOR:
    case PANGO_ATTR_LINE_HEIGHT:
    case PANGO_ATTR_ABSOLUTE_LINE_HEIGHT:
    case PANGO_ATTR_TEXT_TRANSFORM:
    case PANGO_ATTR_WORD:
    case PANGO_ATTR_SENTENCE:
    case PANGO_ATTR_BASELINE_SHIFT:
    case PANGO_ATTR_FONT_SCALE:
      return TRUE;
    default:
      if (!attr_type)
        return FALSE;
      else
        {
          gboolean result = FALSE;

          G_LOCK (attr_type);

          for (int i = 0; i < attr_type->len; i++)
            {
              PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
              if (class->type == type)
                {
                  result = TRUE;
                  break;
                }
            }

          G_UNLOCK (attr_type);

          return result;
        }
    }
}

/**
 * pango_attr_type_register:
 * @name: (nullable): an identifier for the type
 * @value_type: the `PangoAttrValueType` for the attribute
 * @affects: `PangoAttrAffects` flags for the attribute
 * @merge: `PangoAttrMerge` flags for the attribute
 * @copy: (nullable): function to copy the data of an attribute
 *   of this type
 * @destroy: (nullable): function to free the data of an attribute
 *   of this type
 * @equal: (nullable): function to compare the data of two attributes
 *   of this type
 * @serialize: (nullable): function to serialize the data
 *   of an attribute of this type
 *
 * Register a new attribute type.
 *
 * The attribute type name can be accessed later
 * by using [func@Pango.AttrType.get_name].
 *
 * The callbacks are only needed if @type is `PANGO_ATTR_VALUE_POINTER`,
 * Pango knows how to handle other value types and will only use the
 * callbacks for generic pointer values.
 *
 * If @name and @serialize are provided, they will be used
 * to serialize attributes of this type.
 *
 * To create attributes with the new type, use [func@Pango.attr_custom_new].
 *
 * Return value: the attribute type ID
 */
guint
pango_attr_type_register (const char                 *name,
                          PangoAttrValueType          value_type,
                          PangoAttrAffects            affects,
                          PangoAttrMerge              merge,
                          PangoAttrDataCopyFunc       copy,
                          GDestroyNotify              destroy,
                          GEqualFunc                  equal,
                          PangoAttrDataSerializeFunc  serialize)
{
  static guint current_id = MIN_CUSTOM; /* MT-safe */
  PangoAttrClass class;

  G_LOCK (attr_type);

  class.type = value_type | (affects << 8) | (merge << 12) | (current_id << 16);
  current_id++;

  class.copy = copy;
  class.destroy = destroy;
  class.equal = equal;
  class.serialize = serialize;

  if (name)
    class.name = g_intern_string (name);

  if (attr_type == NULL)
    attr_type = g_array_new (FALSE, FALSE, sizeof (PangoAttrClass));

  g_array_append_val (attr_type, class);

  G_UNLOCK (attr_type);

  return class.type;
}

/**
 * pango_attr_type_get_name:
 * @type: an attribute type ID to fetch the name for
 *
 * Fetches the attribute type name.
 *
 * The attribute type name is the string passed in
 * when registering the type using
 * [func@Pango.AttrType.register].
 *
 * The returned value is an interned string (see
 * g_intern_string() for what that means) that should
 * not be modified or freed.
 *
 * Return value: (nullable): the type ID name (which
 *   may be %NULL), or %NULL if @type is a built-in Pango
 *   attribute type or invalid.
 *
 * Since: 1.22
 */
const char *
pango_attr_type_get_name (guint type)
{
  const char *result = NULL;

  if ((type >> 16) < MIN_CUSTOM)
    return get_attr_type_nick (type);

  G_LOCK (attr_type);

  for (int i = 0; i < attr_type->len; i++)
    {
      PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
      if (class->type == type)
        {
          result = class->name;
          break;
        }
    }

  G_UNLOCK (attr_type);

  return result;
}

/**
 * pango_attribute_copy:
 * @attr: a `PangoAttribute`
 *
 * Make a copy of an attribute.
 *
 * Return value: (transfer full): the newly allocated
 *   `PangoAttribute`, which should be freed with
 *   [method@Pango.Attribute.destroy].
 */
PangoAttribute *
pango_attribute_copy (const PangoAttribute *attr)
{
  PangoAttribute *result;

  g_return_val_if_fail (attr != NULL, NULL);

  result = g_slice_dup (PangoAttribute, attr);

  switch (PANGO_ATTR_VALUE_TYPE (attr))
    {
    case PANGO_ATTR_VALUE_STRING:
      result->str_value = g_strdup (attr->str_value);
      break;

    case PANGO_ATTR_VALUE_FONT_DESC:
      result->font_value = pango_font_description_copy (attr->font_value);
      break;

    case PANGO_ATTR_VALUE_POINTER:
      {
        PangoAttrDataCopyFunc copy = NULL;

        G_LOCK (attr_type);

        g_assert (attr_type != NULL);

        for (int i = 0; i < attr_type->len; i++)
          {
            PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
            if (class->type == attr->type)
              {
                copy = class->copy;
                break;
              }
          }

        G_UNLOCK (attr_type);

        if (copy)
          result->pointer_value = copy (attr->pointer_value);
      }
      break;

    case PANGO_ATTR_VALUE_INT:
    case PANGO_ATTR_VALUE_BOOLEAN:
    case PANGO_ATTR_VALUE_FLOAT:
    case PANGO_ATTR_VALUE_COLOR:
    case PANGO_ATTR_VALUE_LANGUAGE:
    default: ;
    }

  return result;
}

/**
 * pango_attribute_destroy:
 * @attr: a `PangoAttribute`.
 *
 * Destroy a `PangoAttribute` and free all associated memory.
 */
void
pango_attribute_destroy (PangoAttribute *attr)
{
  g_return_if_fail (attr != NULL);

  switch (PANGO_ATTR_VALUE_TYPE (attr))
    {
    case PANGO_ATTR_VALUE_STRING:
      g_free (attr->str_value);
      break;

    case PANGO_ATTR_VALUE_FONT_DESC:
      pango_font_description_free (attr->font_value);
      break;

    case PANGO_ATTR_VALUE_POINTER:
      {
        GDestroyNotify destroy = NULL;

        G_LOCK (attr_type);

        g_assert (attr_type != NULL);

        for (int i = 0; i < attr_type->len; i++)
          {
            PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
            if (class->type == attr->type)
              {
                destroy = class->destroy;
                break;
              }
          }

        G_UNLOCK (attr_type);

        if (destroy)
          destroy (attr->pointer_value);
      }
      break;

    case PANGO_ATTR_VALUE_INT:
    case PANGO_ATTR_VALUE_BOOLEAN:
    case PANGO_ATTR_VALUE_FLOAT:
    case PANGO_ATTR_VALUE_COLOR:
    case PANGO_ATTR_VALUE_LANGUAGE:
    default: ;
    }

  g_slice_free (PangoAttribute, attr);
}

G_DEFINE_BOXED_TYPE (PangoAttribute, pango_attribute,
                     pango_attribute_copy,
                     pango_attribute_destroy);

/**
 * pango_attribute_equal:
 * @attr1: a `PangoAttribute`
 * @attr2: another `PangoAttribute`
 *
 * Compare two attributes for equality.
 *
 * This compares only the actual value of the two
 * attributes and not the ranges that the attributes
 * apply to.
 *
 * Return value: %TRUE if the two attributes have the same value
 */
gboolean
pango_attribute_equal (const PangoAttribute *attr1,
                       const PangoAttribute *attr2)
{
  g_return_val_if_fail (attr1 != NULL, FALSE);
  g_return_val_if_fail (attr2 != NULL, FALSE);

  if (attr1->type != attr2->type)
    return FALSE;

  switch (PANGO_ATTR_VALUE_TYPE (attr1))
    {
    case PANGO_ATTR_VALUE_STRING:
      return strcmp (attr1->str_value, attr2->str_value) == 0;

    case PANGO_ATTR_VALUE_INT:
      return attr1->int_value == attr2->int_value;

    case PANGO_ATTR_VALUE_BOOLEAN:
      return attr1->boolean_value == attr2->boolean_value;

    case PANGO_ATTR_VALUE_FLOAT:
      return attr1->double_value == attr2->double_value;

    case PANGO_ATTR_VALUE_COLOR:
      return memcmp (&attr1->color_value, &attr2->color_value, sizeof (PangoColor)) == 0;

    case PANGO_ATTR_VALUE_LANGUAGE:
      return attr1->lang_value == attr2->lang_value;

    case PANGO_ATTR_VALUE_FONT_DESC:
      return pango_font_description_equal (attr1->font_value, attr2->font_value);

    case PANGO_ATTR_VALUE_POINTER:
      {
        GEqualFunc equal = NULL;

        G_LOCK (attr_type);

        g_assert (attr_type != NULL);

        for (int i = 0; i < attr_type->len; i++)
          {
            PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
            if (class->type == attr1->type)
              {
                equal = class->equal;
                break;
              }
          }

        G_UNLOCK (attr_type);

        g_assert (equal != NULL);

        if (equal)
          return equal (attr1->pointer_value, attr2->pointer_value);

        return attr1->pointer_value == attr2->pointer_value;
      }

    default:
      g_assert_not_reached ();
    }
}

/**
 * pango_attribute_new:
 * @type: the attribute type
 *
 * Creates a new attribute for the given type.
 *
 * The type must be one of the `PangoAttrType` values, or
 * have been registered with [func@Pango.register_attr_type].
 *
 * Pango will initialize @start_index and @end_index to an
 * all-inclusive range of `[0,G_MAXUINT]`.  The caller is
 * responsible for filling the proper value field with the
 * desired value.
 *
 * Return value: (transfer full): the newly allocated
 *   `PangoAttribute`, which should be freed with
 *   [method@Pango.Attribute.destroy]
 */
PangoAttribute *
pango_attribute_new (guint type)
{
  PangoAttribute *attr;

  g_return_val_if_fail (is_valid_attr_type (type), NULL);

  attr = g_slice_new0 (PangoAttribute);
  attr->type = type;
  attr->start_index = PANGO_ATTR_INDEX_FROM_TEXT_BEGINNING;
  attr->end_index = PANGO_ATTR_INDEX_TO_TEXT_END;

  return attr;
}

/* }}} */
/* {{{ Private API */

char *
pango_attr_value_serialize (PangoAttribute *attr)
{
  PangoAttrDataSerializeFunc serialize = NULL;

  G_LOCK (attr_type);

  g_assert (attr_type != NULL);

  for (int i = 0; i < attr_type->len; i++)
    {
      PangoAttrClass *class = &g_array_index (attr_type, PangoAttrClass, i);
      if (class->type == attr->type)
        {
          serialize = class->serialize;
          break;
        }
    }

  G_UNLOCK (attr_type);

  if (serialize)
    return serialize (attr->pointer_value);

  return g_strdup_printf ("%p", attr->pointer_value);
}

/* }}} */
/* {{{ Binding Helpers */

gboolean
pango_attribute_get_string (PangoAttribute   *attribute,
                            const char      **value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_STRING)
    return FALSE;

  *value = attribute->str_value;
  return TRUE;
}

gboolean
pango_attribute_get_language (PangoAttribute  *attribute,
                              PangoLanguage  **value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_LANGUAGE)
    return FALSE;

  *value = attribute->lang_value;
  return TRUE;
}

gboolean
pango_attribute_get_int (PangoAttribute *attribute,
                         int            *value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_INT)
    return FALSE;

  *value = attribute->int_value;
  return TRUE;
}

gboolean
pango_attribute_get_boolean (PangoAttribute *attribute,
                             int            *value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_BOOLEAN)
    return FALSE;

  *value = attribute->boolean_value;
  return TRUE;
}

gboolean
pango_attribute_get_float (PangoAttribute *attribute,
                           double         *value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_FLOAT)
    return FALSE;

  *value = attribute->double_value;
  return TRUE;
}

gboolean
pango_attribute_get_color (PangoAttribute *attribute,
                           PangoColor     *value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_COLOR)
    return FALSE;

  *value = attribute->color_value;
  return TRUE;
}

gboolean
pango_attribute_get_font_desc (PangoAttribute        *attribute,
                               PangoFontDescription **value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_FONT_DESC)
    return FALSE;

  *value = attribute->font_value;
  return TRUE;
}

gboolean
pango_attribute_get_pointer (PangoAttribute *attribute,
                             gpointer       *value)
{
  if (PANGO_ATTR_VALUE_TYPE (attribute) != PANGO_ATTR_VALUE_POINTER)
    return FALSE;

  *value = attribute->pointer_value;
  return TRUE;
}

/* }}} */

/* vim:set foldmethod=marker expandtab: */