Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/cairo/src/cairo-toy-font-face.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2002 University of Southern California
5
 * Copyright © 2005,2008 Red Hat Inc.
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it either under the terms of the GNU Lesser General Public
9
 * License version 2.1 as published by the Free Software Foundation
10
 * (the "LGPL") or, at your option, under the terms of the Mozilla
11
 * Public License Version 1.1 (the "MPL"). If you do not alter this
12
 * notice, a recipient may use your version of this file under either
13
 * the MPL or the LGPL.
14
 *
15
 * You should have received a copy of the LGPL along with this library
16
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18
 * You should have received a copy of the MPL along with this library
19
 * in the file COPYING-MPL-1.1
20
 *
21
 * The contents of this file are subject to the Mozilla Public License
22
 * Version 1.1 (the "License"); you may not use this file except in
23
 * compliance with the License. You may obtain a copy of the License at
24
 * http://www.mozilla.org/MPL/
25
 *
26
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28
 * the specific language governing rights and limitations.
29
 *
30
 * The Original Code is the cairo graphics library.
31
 *
32
 * The Initial Developer of the Original Code is University of Southern
33
 * California.
34
 *
35
 * Contributor(s):
36
 *  Carl D. Worth <cworth@cworth.org>
37
 *      Graydon Hoare <graydon@redhat.com>
38
 *      Owen Taylor <otaylor@redhat.com>
39
 *      Behdad Esfahbod <behdad@behdad.org>
40
 */
41
42
#define _DEFAULT_SOURCE /* for strdup() */
43
#include "cairoint.h"
44
#include "cairo-error-private.h"
45
46
47
static const cairo_font_face_t _cairo_font_face_null_pointer = {
48
    { 0 },        /* hash_entry */
49
    CAIRO_STATUS_NULL_POINTER,    /* status */
50
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */
51
    { 0, 0, 0, NULL },      /* user_data */
52
    NULL
53
};
54
55
static const cairo_font_face_t _cairo_font_face_invalid_string = {
56
    { 0 },        /* hash_entry */
57
    CAIRO_STATUS_INVALID_STRING,  /* status */
58
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */
59
    { 0, 0, 0, NULL },      /* user_data */
60
    NULL
61
};
62
63
static const cairo_font_face_t _cairo_font_face_invalid_slant = {
64
    { 0 },        /* hash_entry */
65
    CAIRO_STATUS_INVALID_SLANT,   /* status */
66
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */
67
    { 0, 0, 0, NULL },      /* user_data */
68
    NULL
69
};
70
71
static const cairo_font_face_t _cairo_font_face_invalid_weight = {
72
    { 0 },        /* hash_entry */
73
    CAIRO_STATUS_INVALID_WEIGHT,  /* status */
74
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */
75
    { 0, 0, 0, NULL },      /* user_data */
76
    NULL
77
};
78
79
80
static const cairo_font_face_backend_t _cairo_toy_font_face_backend;
81
82
static int
83
_cairo_toy_font_face_keys_equal (const void *key_a,
84
         const void *key_b);
85
86
/* We maintain a hash table from family/weight/slant =>
87
 * #cairo_font_face_t for #cairo_toy_font_t. The primary purpose of
88
 * this mapping is to provide unique #cairo_font_face_t values so that
89
 * our cache and mapping from #cairo_font_face_t => #cairo_scaled_font_t
90
 * works. Once the corresponding #cairo_font_face_t objects fall out of
91
 * downstream caches, we don't need them in this hash table anymore.
92
 *
93
 * Modifications to this hash table are protected by
94
 * _cairo_toy_font_face_mutex.
95
 */
96
static cairo_hash_table_t *cairo_toy_font_face_hash_table = NULL;
97
98
static cairo_hash_table_t *
99
_cairo_toy_font_face_hash_table_lock (void)
100
0
{
101
0
    CAIRO_MUTEX_LOCK (_cairo_toy_font_face_mutex);
102
103
0
    if (cairo_toy_font_face_hash_table == NULL)
104
0
    {
105
0
  cairo_toy_font_face_hash_table =
106
0
      _cairo_hash_table_create (_cairo_toy_font_face_keys_equal);
107
108
0
  if (cairo_toy_font_face_hash_table == NULL) {
109
0
      CAIRO_MUTEX_UNLOCK (_cairo_toy_font_face_mutex);
110
0
      return NULL;
111
0
  }
112
0
    }
113
114
0
    return cairo_toy_font_face_hash_table;
115
0
}
116
117
static void
118
_cairo_toy_font_face_hash_table_unlock (void)
119
0
{
120
0
    CAIRO_MUTEX_UNLOCK (_cairo_toy_font_face_mutex);
121
0
}
122
123
/**
124
 * _cairo_toy_font_face_init_key:
125
 *
126
 * Initialize those portions of #cairo_toy_font_face_t needed to use
127
 * it as a hash table key, including the hash code buried away in
128
 * font_face->base.hash_entry. No memory allocation is performed here
129
 * so that no fini call is needed. We do this to make it easier to use
130
 * an automatic #cairo_toy_font_face_t variable as a key.
131
 **/
132
static void
133
_cairo_toy_font_face_init_key (cairo_toy_font_face_t *key,
134
             const char      *family,
135
             cairo_font_slant_t     slant,
136
             cairo_font_weight_t    weight)
137
0
{
138
0
    uintptr_t hash;
139
140
0
    key->family = family;
141
0
    key->owns_family = FALSE;
142
143
0
    key->slant = slant;
144
0
    key->weight = weight;
145
146
    /* 1607 and 1451 are just a couple of arbitrary primes. */
147
0
    hash = _cairo_hash_string (family);
148
0
    hash += ((uintptr_t) slant) * 1607;
149
0
    hash += ((uintptr_t) weight) * 1451;
150
151
0
    key->base.hash_entry.hash = hash;
152
0
}
153
154
static cairo_status_t
155
_cairo_toy_font_face_create_impl_face (cairo_toy_font_face_t *font_face,
156
               cairo_font_face_t **impl_font_face)
157
0
{
158
0
    const cairo_font_face_backend_t * backend = CAIRO_FONT_FACE_BACKEND_DEFAULT;
159
0
    cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
160
161
0
    if (unlikely (font_face->base.status))
162
0
  return font_face->base.status;
163
164
0
    if (backend->create_for_toy != NULL &&
165
0
  0 != strncmp (font_face->family, CAIRO_USER_FONT_FAMILY_DEFAULT,
166
0
          strlen (CAIRO_USER_FONT_FAMILY_DEFAULT)))
167
0
    {
168
0
  status = backend->create_for_toy (font_face, impl_font_face);
169
0
    }
170
171
0
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
172
0
  backend = &_cairo_user_font_face_backend;
173
0
  status = backend->create_for_toy (font_face, impl_font_face);
174
0
    }
175
176
0
    return status;
177
0
}
178
179
static cairo_status_t
180
_cairo_toy_font_face_init (cairo_toy_font_face_t *font_face,
181
         const char          *family,
182
         cairo_font_slant_t   slant,
183
         cairo_font_weight_t    weight)
184
0
{
185
0
    char *family_copy;
186
0
    cairo_status_t status;
187
188
0
    family_copy = strdup (family);
189
0
    if (unlikely (family_copy == NULL))
190
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
191
192
0
    _cairo_toy_font_face_init_key (font_face, family_copy, slant, weight);
193
0
    font_face->owns_family = TRUE;
194
195
0
    _cairo_font_face_init (&font_face->base, &_cairo_toy_font_face_backend);
196
197
0
    status = _cairo_toy_font_face_create_impl_face (font_face,
198
0
                &font_face->impl_face);
199
0
    if (unlikely (status)) {
200
0
  free (family_copy);
201
0
  return status;
202
0
    }
203
204
0
    return CAIRO_STATUS_SUCCESS;
205
0
}
206
207
static void
208
_cairo_toy_font_face_fini (cairo_toy_font_face_t *font_face)
209
0
{
210
    /* We assert here that we own font_face->family before casting
211
     * away the const qualifier. */
212
0
    assert (font_face->owns_family);
213
0
    free ((char*) font_face->family);
214
215
0
    if (font_face->impl_face)
216
0
  cairo_font_face_destroy (font_face->impl_face);
217
0
}
218
219
static int
220
_cairo_toy_font_face_keys_equal (const void *key_a,
221
         const void *key_b)
222
0
{
223
0
    const cairo_toy_font_face_t *face_a = key_a;
224
0
    const cairo_toy_font_face_t *face_b = key_b;
225
226
0
    return (strcmp (face_a->family, face_b->family) == 0 &&
227
0
      face_a->slant == face_b->slant &&
228
0
      face_a->weight == face_b->weight);
229
0
}
230
231
/**
232
 * cairo_toy_font_face_create:
233
 * @family: a font family name, encoded in UTF-8
234
 * @slant: the slant for the font
235
 * @weight: the weight for the font
236
 *
237
 * Creates a font face from a triplet of family, slant, and weight.
238
 * These font faces are used in implementation of the the #cairo_t "toy"
239
 * font API.
240
 *
241
 * If @family is the zero-length string "", the platform-specific default
242
 * family is assumed.  The default family then can be queried using
243
 * cairo_toy_font_face_get_family().
244
 *
245
 * The cairo_select_font_face() function uses this to create font faces.
246
 * See that function for limitations and other details of toy font faces.
247
 *
248
 * Return value: a newly created #cairo_font_face_t. Free with
249
 *  cairo_font_face_destroy() when you are done using it.
250
 *
251
 * Since: 1.8
252
 **/
253
cairo_font_face_t *
254
cairo_toy_font_face_create (const char          *family,
255
          cairo_font_slant_t   slant,
256
          cairo_font_weight_t  weight)
257
0
{
258
0
    cairo_status_t status;
259
0
    cairo_toy_font_face_t key, *font_face;
260
0
    cairo_hash_table_t *hash_table;
261
262
0
    if (family == NULL)
263
0
  return (cairo_font_face_t*) &_cairo_font_face_null_pointer;
264
265
    /* Make sure we've got valid UTF-8 for the family */
266
0
    status = _cairo_utf8_to_ucs4 (family, -1, NULL, NULL);
267
0
    if (unlikely (status)) {
268
0
  if (status == CAIRO_STATUS_INVALID_STRING)
269
0
      return (cairo_font_face_t*) &_cairo_font_face_invalid_string;
270
271
0
  return (cairo_font_face_t*) &_cairo_font_face_nil;
272
0
    }
273
274
0
    switch (slant) {
275
0
  case CAIRO_FONT_SLANT_NORMAL:
276
0
  case CAIRO_FONT_SLANT_ITALIC:
277
0
  case CAIRO_FONT_SLANT_OBLIQUE:
278
0
      break;
279
0
  default:
280
0
      return (cairo_font_face_t*) &_cairo_font_face_invalid_slant;
281
0
    }
282
283
0
    switch (weight) {
284
0
  case CAIRO_FONT_WEIGHT_NORMAL:
285
0
  case CAIRO_FONT_WEIGHT_BOLD:
286
0
      break;
287
0
  default:
288
0
      return (cairo_font_face_t*) &_cairo_font_face_invalid_weight;
289
0
    }
290
291
0
    if (*family == '\0')
292
0
  family = CAIRO_FONT_FAMILY_DEFAULT;
293
294
0
    hash_table = _cairo_toy_font_face_hash_table_lock ();
295
0
    if (unlikely (hash_table == NULL))
296
0
  goto UNWIND;
297
298
0
    _cairo_toy_font_face_init_key (&key, family, slant, weight);
299
300
    /* Return existing font_face if it exists in the hash table. */
301
0
    font_face = _cairo_hash_table_lookup (hash_table,
302
0
            &key.base.hash_entry);
303
0
    if (font_face != NULL) {
304
0
  if (font_face->base.status == CAIRO_STATUS_SUCCESS) {
305
0
      cairo_font_face_reference (&font_face->base);
306
0
      _cairo_toy_font_face_hash_table_unlock ();
307
0
      return &font_face->base;
308
0
  }
309
310
  /* remove the bad font from the hash table */
311
0
  _cairo_hash_table_remove (hash_table, &font_face->base.hash_entry);
312
0
    }
313
314
    /* Otherwise create it and insert into hash table. */
315
0
    font_face = _cairo_malloc (sizeof (cairo_toy_font_face_t));
316
0
    if (unlikely (font_face == NULL)) {
317
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
318
0
  goto UNWIND_HASH_TABLE_LOCK;
319
0
    }
320
321
0
    status = _cairo_toy_font_face_init (font_face, family, slant, weight);
322
0
    if (unlikely (status))
323
0
  goto UNWIND_FONT_FACE_MALLOC;
324
325
0
    assert (font_face->base.hash_entry.hash == key.base.hash_entry.hash);
326
0
    status = _cairo_hash_table_insert (hash_table, &font_face->base.hash_entry);
327
0
    if (unlikely (status))
328
0
  goto UNWIND_FONT_FACE_INIT;
329
330
0
    _cairo_toy_font_face_hash_table_unlock ();
331
332
0
    return &font_face->base;
333
334
0
 UNWIND_FONT_FACE_INIT:
335
0
    _cairo_toy_font_face_fini (font_face);
336
0
 UNWIND_FONT_FACE_MALLOC:
337
0
    free (font_face);
338
0
 UNWIND_HASH_TABLE_LOCK:
339
0
    _cairo_toy_font_face_hash_table_unlock ();
340
0
 UNWIND:
341
0
    return (cairo_font_face_t*) &_cairo_font_face_nil;
342
0
}
343
slim_hidden_def (cairo_toy_font_face_create);
344
345
static cairo_bool_t
346
_cairo_toy_font_face_destroy (void *abstract_face)
347
0
{
348
0
    cairo_toy_font_face_t *font_face = abstract_face;
349
0
    cairo_hash_table_t *hash_table;
350
351
0
    hash_table = _cairo_toy_font_face_hash_table_lock ();
352
    /* All created objects must have been mapped in the hash table. */
353
0
    assert (hash_table != NULL);
354
355
0
    if (! _cairo_reference_count_dec_and_test (&font_face->base.ref_count)) {
356
  /* somebody recreated the font whilst we waited for the lock */
357
0
  _cairo_toy_font_face_hash_table_unlock ();
358
0
  return FALSE;
359
0
    }
360
361
    /* Font faces in SUCCESS status are guaranteed to be in the
362
     * hashtable. Font faces in an error status are removed from the
363
     * hashtable if they are found during a lookup, thus they should
364
     * only be removed if they are in the hashtable. */
365
0
    if (likely (font_face->base.status == CAIRO_STATUS_SUCCESS) ||
366
0
  _cairo_hash_table_lookup (hash_table, &font_face->base.hash_entry) == font_face)
367
0
  _cairo_hash_table_remove (hash_table, &font_face->base.hash_entry);
368
369
0
    _cairo_toy_font_face_hash_table_unlock ();
370
371
0
    _cairo_toy_font_face_fini (font_face);
372
0
    return TRUE;
373
0
}
374
375
static cairo_status_t
376
_cairo_toy_font_face_scaled_font_create (void                *abstract_font_face,
377
           const cairo_matrix_t       *font_matrix,
378
           const cairo_matrix_t       *ctm,
379
           const cairo_font_options_t *options,
380
           cairo_scaled_font_t     **scaled_font)
381
0
{
382
0
    cairo_toy_font_face_t *font_face = (cairo_toy_font_face_t *) abstract_font_face;
383
384
0
    ASSERT_NOT_REACHED;
385
386
0
    return _cairo_font_face_set_error (&font_face->base, CAIRO_STATUS_FONT_TYPE_MISMATCH);
387
0
}
388
389
static cairo_font_face_t *
390
_cairo_toy_font_face_get_implementation (void                *abstract_font_face,
391
           const cairo_matrix_t       *font_matrix,
392
           const cairo_matrix_t       *ctm,
393
           const cairo_font_options_t *options)
394
0
{
395
0
    cairo_toy_font_face_t *font_face = abstract_font_face;
396
397
0
    if (font_face->impl_face) {
398
0
  cairo_font_face_t *impl = font_face->impl_face;
399
400
0
  if (impl->backend->get_implementation != NULL) {
401
0
      return impl->backend->get_implementation (impl,
402
0
                  font_matrix,
403
0
                  ctm,
404
0
                  options);
405
0
  }
406
407
0
  return cairo_font_face_reference (impl);
408
0
    }
409
410
0
    return abstract_font_face;
411
0
}
412
413
static cairo_bool_t
414
_cairo_font_face_is_toy (cairo_font_face_t *font_face)
415
0
{
416
0
    return font_face->backend == &_cairo_toy_font_face_backend;
417
0
}
418
419
/**
420
 * cairo_toy_font_face_get_family:
421
 * @font_face: A toy font face
422
 *
423
 * Gets the family name of a toy font.
424
 *
425
 * Return value: The family name.  This string is owned by the font face
426
 * and remains valid as long as the font face is alive (referenced).
427
 *
428
 * Since: 1.8
429
 **/
430
const char *
431
cairo_toy_font_face_get_family (cairo_font_face_t *font_face)
432
0
{
433
0
    cairo_toy_font_face_t *toy_font_face;
434
435
0
    if (font_face->status)
436
0
  return CAIRO_FONT_FAMILY_DEFAULT;
437
438
0
    toy_font_face = (cairo_toy_font_face_t *) font_face;
439
0
    if (! _cairo_font_face_is_toy (font_face)) {
440
0
  if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
441
0
      return CAIRO_FONT_FAMILY_DEFAULT;
442
0
    }
443
0
    assert (toy_font_face->owns_family);
444
0
    return toy_font_face->family;
445
0
}
446
447
/**
448
 * cairo_toy_font_face_get_slant:
449
 * @font_face: A toy font face
450
 *
451
 * Gets the slant a toy font.
452
 *
453
 * Return value: The slant value
454
 *
455
 * Since: 1.8
456
 **/
457
cairo_font_slant_t
458
cairo_toy_font_face_get_slant (cairo_font_face_t *font_face)
459
0
{
460
0
    cairo_toy_font_face_t *toy_font_face;
461
462
0
    if (font_face->status)
463
0
  return CAIRO_FONT_SLANT_DEFAULT;
464
465
0
    toy_font_face = (cairo_toy_font_face_t *) font_face;
466
0
    if (! _cairo_font_face_is_toy (font_face)) {
467
0
  if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
468
0
      return CAIRO_FONT_SLANT_DEFAULT;
469
0
    }
470
0
    return toy_font_face->slant;
471
0
}
472
slim_hidden_def (cairo_toy_font_face_get_slant);
473
474
/**
475
 * cairo_toy_font_face_get_weight:
476
 * @font_face: A toy font face
477
 *
478
 * Gets the weight a toy font.
479
 *
480
 * Return value: The weight value
481
 *
482
 * Since: 1.8
483
 **/
484
cairo_font_weight_t
485
cairo_toy_font_face_get_weight (cairo_font_face_t *font_face)
486
0
{
487
0
    cairo_toy_font_face_t *toy_font_face;
488
489
0
    if (font_face->status)
490
0
  return CAIRO_FONT_WEIGHT_DEFAULT;
491
492
0
    toy_font_face = (cairo_toy_font_face_t *) font_face;
493
0
    if (! _cairo_font_face_is_toy (font_face)) {
494
0
  if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
495
0
      return CAIRO_FONT_WEIGHT_DEFAULT;
496
0
    }
497
0
    return toy_font_face->weight;
498
0
}
499
slim_hidden_def (cairo_toy_font_face_get_weight);
500
501
static const cairo_font_face_backend_t _cairo_toy_font_face_backend = {
502
    CAIRO_FONT_TYPE_TOY,
503
    NULL,         /* create_for_toy */
504
    _cairo_toy_font_face_destroy,
505
    _cairo_toy_font_face_scaled_font_create,
506
    _cairo_toy_font_face_get_implementation
507
};
508
509
void
510
_cairo_toy_font_face_reset_static_data (void)
511
0
{
512
0
    cairo_hash_table_t *hash_table;
513
514
    /* We manually acquire the lock rather than calling
515
     * cairo_toy_font_face_hash_table_lock simply to avoid
516
     * creating the table only to destroy it again. */
517
0
    CAIRO_MUTEX_LOCK (_cairo_toy_font_face_mutex);
518
0
    hash_table = cairo_toy_font_face_hash_table;
519
0
    cairo_toy_font_face_hash_table = NULL;
520
0
    CAIRO_MUTEX_UNLOCK (_cairo_toy_font_face_mutex);
521
522
0
    if (hash_table != NULL)
523
0
  _cairo_hash_table_destroy (hash_table);
524
0
}