Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-scaled-font.c
Line
Count
Source
1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/*
3
 * Copyright © 2005 Keith Packard
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is Keith Packard
31
 *
32
 * Contributor(s):
33
 *      Keith Packard <keithp@keithp.com>
34
 *  Carl D. Worth <cworth@cworth.org>
35
 *      Graydon Hoare <graydon@redhat.com>
36
 *      Owen Taylor <otaylor@redhat.com>
37
 *      Behdad Esfahbod <behdad@behdad.org>
38
 *      Chris Wilson <chris@chris-wilson.co.uk>
39
 */
40
41
#include "cairoint.h"
42
#include "cairo-array-private.h"
43
#include "cairo-error-private.h"
44
#include "cairo-image-surface-private.h"
45
#include "cairo-list-inline.h"
46
#include "cairo-pattern-private.h"
47
#include "cairo-scaled-font-private.h"
48
#include "cairo-surface-backend-private.h"
49
50
/**
51
 * SECTION:cairo-scaled-font
52
 * @Title: cairo_scaled_font_t
53
 * @Short_Description: Font face at particular size and options
54
 * @See_Also: #cairo_font_face_t, #cairo_matrix_t, #cairo_font_options_t
55
 *
56
 * #cairo_scaled_font_t represents a realization of a font face at a particular
57
 * size and transformation and a certain set of font options.
58
 **/
59
60
static uintptr_t
61
_cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font);
62
63
/* Global Glyph Cache
64
 *
65
 * We maintain a global pool of glyphs split between all active fonts. This
66
 * allows a heavily used individual font to cache more glyphs than we could
67
 * manage if we used per-font glyph caches, but at the same time maintains
68
 * fairness across all fonts and provides a cap on the maximum number of
69
 * global glyphs.
70
 *
71
 * The glyphs are allocated in pages, which are capped in the global pool.
72
 * Using pages means we can reduce the frequency at which we have to probe the
73
 * global pool and ameliorates the memory allocation pressure.
74
 */
75
76
/* XXX: This number is arbitrary---we've never done any measurement of this. */
77
1
#define MAX_GLYPH_PAGES_CACHED 512
78
static cairo_cache_t cairo_scaled_glyph_page_cache;
79
80
10
#define CAIRO_SCALED_GLYPH_PAGE_SIZE 32
81
struct _cairo_scaled_glyph_page {
82
    cairo_cache_entry_t cache_entry;
83
    cairo_scaled_font_t *scaled_font;
84
    cairo_list_t link;
85
86
    unsigned int num_glyphs;
87
    cairo_scaled_glyph_t glyphs[CAIRO_SCALED_GLYPH_PAGE_SIZE];
88
};
89
90
/*
91
 *  Notes:
92
 *
93
 *  To store rasterizations of glyphs, we use an image surface and the
94
 *  device offset to represent the glyph origin.
95
 *
96
 *  A device_transform converts from device space (a conceptual space) to
97
 *  surface space.  For simple cases of translation only, it's called a
98
 *  device_offset and is public API (cairo_surface_[gs]et_device_offset()).
99
 *  A possibly better name for those functions could have been
100
 *  cairo_surface_[gs]et_origin().  So, that's what they do: they set where
101
 *  the device-space origin (0,0) is in the surface.  If the origin is inside
102
 *  the surface, device_offset values are positive.  It may look like this:
103
 *
104
 *  Device space:
105
 *        (-x,-y) <-- negative numbers
106
 *           +----------------+
107
 *           |      .         |
108
 *           |      .         |
109
 *           |......(0,0) <---|-- device-space origin
110
 *           |                |
111
 *           |                |
112
 *           +----------------+
113
 *                    (width-x,height-y)
114
 *
115
 *  Surface space:
116
 *         (0,0) <-- surface-space origin
117
 *           +---------------+
118
 *           |      .        |
119
 *           |      .        |
120
 *           |......(x,y) <--|-- device_offset
121
 *           |               |
122
 *           |               |
123
 *           +---------------+
124
 *                     (width,height)
125
 *
126
 *  In other words: device_offset is the coordinates of the device-space
127
 *  origin relative to the top-left of the surface.
128
 *
129
 *  We use device offsets in a couple of places:
130
 *
131
 *    - Public API: To let toolkits like Gtk+ give user a surface that
132
 *      only represents part of the final destination (say, the expose
133
 *      area), but has the same device space as the destination.  In these
134
 *      cases device_offset is typically negative.  Example:
135
 *
136
 *           application window
137
 *           +---------------+
138
 *           |      .        |
139
 *           | (x,y).        |
140
 *           |......+---+    |
141
 *           |      |   | <--|-- expose area
142
 *           |      +---+    |
143
 *           +---------------+
144
 *
145
 *      In this case, the user of cairo API can set the device_space on
146
 *      the expose area to (-x,-y) to move the device space origin to that
147
 *      of the application window, such that drawing in the expose area
148
 *      surface and painting it in the application window has the same
149
 *      effect as drawing in the application window directly.  Gtk+ has
150
 *      been using this feature.
151
 *
152
 *    - Glyph surfaces: In most font rendering systems, glyph surfaces
153
 *      have an origin at (0,0) and a bounding box that is typically
154
 *      represented as (x_bearing,y_bearing,width,height).  Depending on
155
 *      which way y progresses in the system, y_bearing may typically be
156
 *      negative (for systems similar to cairo, with origin at top left),
157
 *      or be positive (in systems like PDF with origin at bottom left).
158
 *      No matter which is the case, it is important to note that
159
 *      (x_bearing,y_bearing) is the coordinates of top-left of the glyph
160
 *      relative to the glyph origin.  That is, for example:
161
 *
162
 *      Scaled-glyph space:
163
 *
164
 *        (x_bearing,y_bearing) <-- negative numbers
165
 *           +----------------+
166
 *           |      .         |
167
 *           |      .         |
168
 *           |......(0,0) <---|-- glyph origin
169
 *           |                |
170
 *           |                |
171
 *           +----------------+
172
 *                    (width+x_bearing,height+y_bearing)
173
 *
174
 *      Note the similarity of the origin to the device space.  That is
175
 *      exactly how we use the device_offset to represent scaled glyphs:
176
 *      to use the device-space origin as the glyph origin.
177
 *
178
 *  Now compare the scaled-glyph space to device-space and surface-space
179
 *  and convince yourself that:
180
 *
181
 *  (x_bearing,y_bearing) = (-x,-y) = - device_offset
182
 *
183
 *  That's right.  If you are not convinced yet, contrast the definition
184
 *  of the two:
185
 *
186
 *  "(x_bearing,y_bearing) is the coordinates of top-left of the
187
 *   glyph relative to the glyph origin."
188
 *
189
 *  "In other words: device_offset is the coordinates of the
190
 *   device-space origin relative to the top-left of the surface."
191
 *
192
 *  and note that glyph origin = device-space origin.
193
 */
194
195
static void
196
_cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font);
197
198
static void
199
_cairo_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
200
        cairo_scaled_glyph_t *scaled_glyph)
201
0
{
202
0
    while (! cairo_list_is_empty (&scaled_glyph->dev_privates)) {
203
0
  cairo_scaled_glyph_private_t *private =
204
0
      cairo_list_first_entry (&scaled_glyph->dev_privates,
205
0
            cairo_scaled_glyph_private_t,
206
0
            link);
207
0
  private->destroy (private, scaled_glyph, scaled_font);
208
0
    }
209
210
0
    _cairo_image_scaled_glyph_fini (scaled_font, scaled_glyph);
211
212
0
    if (scaled_glyph->surface != NULL)
213
0
  cairo_surface_destroy (&scaled_glyph->surface->base);
214
215
0
    if (scaled_glyph->path != NULL)
216
0
  _cairo_path_fixed_destroy (scaled_glyph->path);
217
218
0
    if (scaled_glyph->recording_surface != NULL) {
219
0
  cairo_status_t status;
220
221
  /* If the recording surface contains other fonts, destroying
222
   * it while holding _cairo_scaled_glyph_page_cache_mutex will
223
   * result in deadlock when the recording surface font is
224
   * destroyed. Instead, move the recording surface to a list of
225
   * surfaces to free and free it in
226
   * _cairo_scaled_font_thaw_cache() after
227
   * _cairo_scaled_glyph_page_cache_mutex is unlocked. */
228
0
  status = _cairo_array_append (&scaled_font->recording_surfaces_to_free, &scaled_glyph->recording_surface);
229
0
  assert (status == CAIRO_STATUS_SUCCESS);
230
0
    }
231
232
0
    if (scaled_glyph->color_surface != NULL)
233
0
  cairo_surface_destroy (&scaled_glyph->color_surface->base);
234
0
}
235
236
1
#define ZOMBIE 0
237
static const cairo_scaled_font_t _cairo_scaled_font_nil = {
238
    { ZOMBIE },     /* hash_entry */
239
    CAIRO_STATUS_NO_MEMORY, /* status */
240
    CAIRO_REFERENCE_COUNT_INVALID,  /* ref_count */
241
    { 0, 0, 0, NULL },    /* user_data */
242
    NULL,     /* original_font_face */
243
    NULL,     /* font_face */
244
    { 1., 0., 0., 1., 0, 0},  /* font_matrix */
245
    { 1., 0., 0., 1., 0, 0},  /* ctm */
246
    { CAIRO_ANTIALIAS_DEFAULT,  /* options */
247
      CAIRO_SUBPIXEL_ORDER_DEFAULT,
248
      CAIRO_HINT_STYLE_DEFAULT,
249
      CAIRO_HINT_METRICS_DEFAULT} ,
250
    FALSE,      /* placeholder */
251
    FALSE,      /* holdover */
252
    TRUE,     /* finished */
253
    { 1., 0., 0., 1., 0, 0},  /* scale */
254
    { 1., 0., 0., 1., 0, 0},  /* scale_inverse */
255
    1.,       /* max_scale */
256
    { 0., 0., 0., 0., 0. }, /* extents */
257
    { 0., 0., 0., 0., 0. }, /* fs_extents */
258
    CAIRO_MUTEX_NIL_INITIALIZER,/* mutex */
259
    NULL,     /* glyphs */
260
    { NULL, NULL },   /* pages */
261
    FALSE,      /* cache_frozen */
262
    FALSE,      /* global_cache_frozen */
263
    { 0, 0, sizeof(cairo_surface_t*), NULL }, /* recording_surfaces_to_free */
264
    { NULL, NULL },   /* privates */
265
    NULL      /* backend */
266
};
267
268
/**
269
 * _cairo_scaled_font_set_error:
270
 * @scaled_font: a scaled_font
271
 * @status: a status value indicating an error
272
 *
273
 * Atomically sets scaled_font->status to @status and calls _cairo_error;
274
 * Does nothing if status is %CAIRO_STATUS_SUCCESS.
275
 *
276
 * All assignments of an error status to scaled_font->status should happen
277
 * through _cairo_scaled_font_set_error(). Note that due to the nature of
278
 * the atomic operation, it is not safe to call this function on the nil
279
 * objects.
280
 *
281
 * The purpose of this function is to allow the user to set a
282
 * breakpoint in _cairo_error() to generate a stack trace for when the
283
 * user causes cairo to detect an error.
284
 *
285
 * Return value: the error status.
286
 **/
287
cairo_status_t
288
_cairo_scaled_font_set_error (cairo_scaled_font_t *scaled_font,
289
            cairo_status_t status)
290
0
{
291
0
    if (status == CAIRO_STATUS_SUCCESS)
292
0
  return status;
293
294
    /* Don't overwrite an existing error. This preserves the first
295
     * error, which is the most significant. */
296
0
    _cairo_status_set_error (&scaled_font->status, status);
297
298
0
    return _cairo_error (status);
299
0
}
300
301
/**
302
 * cairo_scaled_font_get_type:
303
 * @scaled_font: a #cairo_scaled_font_t
304
 *
305
 * This function returns the type of the backend used to create
306
 * a scaled font. See #cairo_font_type_t for available types.
307
 * However, this function never returns %CAIRO_FONT_TYPE_TOY.
308
 *
309
 * Return value: The type of @scaled_font.
310
 *
311
 * Since: 1.2
312
 **/
313
cairo_font_type_t
314
cairo_scaled_font_get_type (cairo_scaled_font_t *scaled_font)
315
0
{
316
0
    if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
317
0
  return CAIRO_FONT_TYPE_TOY;
318
319
0
    return scaled_font->backend->type;
320
0
}
321
322
/**
323
 * cairo_scaled_font_status:
324
 * @scaled_font: a #cairo_scaled_font_t
325
 *
326
 * Checks whether an error has previously occurred for this
327
 * scaled_font.
328
 *
329
 * Return value: %CAIRO_STATUS_SUCCESS or another error such as
330
 *   %CAIRO_STATUS_NO_MEMORY.
331
 *
332
 * Since: 1.0
333
 **/
334
cairo_status_t
335
cairo_scaled_font_status (cairo_scaled_font_t *scaled_font)
336
44
{
337
44
    return scaled_font->status;
338
44
}
339
340
/* Here we keep a unique mapping from
341
 * font_face/matrix/ctm/font_options => #cairo_scaled_font_t.
342
 *
343
 * Here are the things that we want to map:
344
 *
345
 *  a) All otherwise referenced #cairo_scaled_font_t's
346
 *  b) Some number of not otherwise referenced #cairo_scaled_font_t's
347
 *
348
 * The implementation uses a hash table which covers (a)
349
 * completely. Then, for (b) we have an array of otherwise
350
 * unreferenced fonts (holdovers) which are expired in
351
 * least-recently-used order.
352
 *
353
 * The cairo_scaled_font_create() code gets to treat this like a regular
354
 * hash table. All of the magic for the little holdover cache is in
355
 * cairo_scaled_font_reference() and cairo_scaled_font_destroy().
356
 */
357
358
/* This defines the size of the holdover array ... that is, the number
359
 * of scaled fonts we keep around even when not otherwise referenced
360
 */
361
1
#define CAIRO_SCALED_FONT_MAX_HOLDOVERS 256
362
363
typedef struct _cairo_scaled_font_map {
364
    cairo_scaled_font_t *mru_scaled_font;
365
    cairo_hash_table_t *hash_table;
366
    cairo_scaled_font_t *holdovers[CAIRO_SCALED_FONT_MAX_HOLDOVERS];
367
    int num_holdovers;
368
} cairo_scaled_font_map_t;
369
370
static cairo_scaled_font_map_t *cairo_scaled_font_map;
371
372
static int
373
_cairo_scaled_font_keys_equal (const void *abstract_key_a, const void *abstract_key_b);
374
375
static cairo_scaled_font_map_t *
376
_cairo_scaled_font_map_lock (void)
377
45
{
378
45
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
379
380
45
    if (cairo_scaled_font_map == NULL) {
381
1
  cairo_scaled_font_map = _cairo_calloc (sizeof (cairo_scaled_font_map_t));
382
1
  if (unlikely (cairo_scaled_font_map == NULL))
383
0
      goto CLEANUP_MUTEX_LOCK;
384
385
1
  cairo_scaled_font_map->mru_scaled_font = NULL;
386
1
  cairo_scaled_font_map->hash_table =
387
1
      _cairo_hash_table_create (_cairo_scaled_font_keys_equal);
388
389
1
  if (unlikely (cairo_scaled_font_map->hash_table == NULL))
390
0
      goto CLEANUP_SCALED_FONT_MAP;
391
392
1
  cairo_scaled_font_map->num_holdovers = 0;
393
1
    }
394
395
45
    return cairo_scaled_font_map;
396
397
0
 CLEANUP_SCALED_FONT_MAP:
398
0
    free (cairo_scaled_font_map);
399
0
    cairo_scaled_font_map = NULL;
400
0
 CLEANUP_MUTEX_LOCK:
401
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
402
0
    _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
403
0
    return NULL;
404
0
}
405
406
static void
407
_cairo_scaled_font_map_unlock (void)
408
45
{
409
45
   CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
410
45
}
411
412
void
413
_cairo_scaled_font_map_destroy (void)
414
0
{
415
0
    cairo_scaled_font_map_t *font_map;
416
0
    cairo_scaled_font_t *scaled_font;
417
418
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
419
420
0
    font_map = cairo_scaled_font_map;
421
0
    if (unlikely (font_map == NULL)) {
422
0
        goto CLEANUP_MUTEX_LOCK;
423
0
    }
424
425
0
    scaled_font = font_map->mru_scaled_font;
426
0
    if (scaled_font != NULL) {
427
0
  CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
428
0
  cairo_scaled_font_destroy (scaled_font);
429
0
  CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
430
0
    }
431
432
    /* remove scaled_fonts starting from the end so that font_map->holdovers
433
     * is always in a consistent state when we release the mutex. */
434
0
    while (font_map->num_holdovers) {
435
0
  scaled_font = font_map->holdovers[font_map->num_holdovers-1];
436
0
  assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
437
0
  _cairo_hash_table_remove (font_map->hash_table,
438
0
          &scaled_font->hash_entry);
439
440
0
  font_map->num_holdovers--;
441
442
  /* This releases the font_map lock to avoid the possibility of a
443
   * recursive deadlock when the scaled font destroy closure gets
444
   * called
445
   */
446
0
  _cairo_scaled_font_fini (scaled_font);
447
448
0
  free (scaled_font);
449
0
    }
450
451
0
    _cairo_hash_table_destroy (font_map->hash_table);
452
453
0
    free (cairo_scaled_font_map);
454
0
    cairo_scaled_font_map = NULL;
455
456
0
 CLEANUP_MUTEX_LOCK:
457
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
458
0
}
459
460
static void
461
_cairo_scaled_glyph_page_destroy (cairo_scaled_font_t *scaled_font,
462
          cairo_scaled_glyph_page_t *page)
463
0
{
464
0
    unsigned int n;
465
466
0
    assert (!scaled_font->cache_frozen);
467
0
    assert (!scaled_font->global_cache_frozen);
468
469
0
    for (n = 0; n < page->num_glyphs; n++) {
470
0
  _cairo_hash_table_remove (scaled_font->glyphs,
471
0
          &page->glyphs[n].hash_entry);
472
0
  _cairo_scaled_glyph_fini (scaled_font, &page->glyphs[n]);
473
0
    }
474
475
0
    cairo_list_del (&page->link);
476
0
    free (page);
477
0
}
478
479
static void
480
_cairo_scaled_glyph_page_pluck (void *closure)
481
0
{
482
0
    cairo_scaled_glyph_page_t *page = closure;
483
0
    cairo_scaled_font_t *scaled_font;
484
485
0
    assert (! cairo_list_is_empty (&page->link));
486
487
0
    scaled_font = page->scaled_font;
488
489
    /* The font is locked in _cairo_scaled_glyph_page_can_remove () */
490
0
    _cairo_scaled_glyph_page_destroy (scaled_font, page);
491
0
    CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
492
0
}
493
494
/* If a scaled font wants to unlock the font map while still being
495
 * created (needed for user-fonts), we need to take extra care not
496
 * ending up with multiple identical scaled fonts being created.
497
 *
498
 * What we do is, we create a fake identical scaled font, and mark
499
 * it as placeholder, lock its mutex, and insert that in the fontmap
500
 * hash table.  This makes other code trying to create an identical
501
 * scaled font to just wait and retry.
502
 *
503
 * The reason we have to create a fake scaled font instead of just using
504
 * scaled_font is for lifecycle management: we need to (or rather,
505
 * other code needs to) reference the scaled_font in the hash table.
506
 * We can't do that on the input scaled_font as it may be freed by
507
 * font backend upon error.
508
 */
509
510
cairo_status_t
511
_cairo_scaled_font_register_placeholder_and_unlock_font_map (cairo_scaled_font_t *scaled_font)
512
0
{
513
0
    cairo_status_t status;
514
0
    cairo_scaled_font_t *placeholder_scaled_font;
515
516
0
    assert (CAIRO_MUTEX_IS_LOCKED (_cairo_scaled_font_map_mutex));
517
518
0
    status = scaled_font->status;
519
0
    if (unlikely (status))
520
0
  return status;
521
522
0
    placeholder_scaled_font = _cairo_calloc (sizeof (cairo_scaled_font_t));
523
0
    if (unlikely (placeholder_scaled_font == NULL))
524
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
525
526
    /* full initialization is wasteful, but who cares... */
527
0
    status = _cairo_scaled_font_init (placeholder_scaled_font,
528
0
              scaled_font->font_face,
529
0
              &scaled_font->font_matrix,
530
0
              &scaled_font->ctm,
531
0
              &scaled_font->options,
532
0
              NULL);
533
0
    if (unlikely (status))
534
0
  goto FREE_PLACEHOLDER;
535
536
0
    placeholder_scaled_font->placeholder = TRUE;
537
538
0
    placeholder_scaled_font->hash_entry.hash
539
0
  = _cairo_scaled_font_compute_hash (placeholder_scaled_font);
540
0
    status = _cairo_hash_table_insert (cairo_scaled_font_map->hash_table,
541
0
               &placeholder_scaled_font->hash_entry);
542
0
    if (unlikely (status))
543
0
  goto FINI_PLACEHOLDER;
544
545
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
546
0
    CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
547
548
0
    return CAIRO_STATUS_SUCCESS;
549
550
0
  FINI_PLACEHOLDER:
551
0
    _cairo_scaled_font_fini_internal (placeholder_scaled_font);
552
0
  FREE_PLACEHOLDER:
553
0
    free (placeholder_scaled_font);
554
555
0
    return _cairo_scaled_font_set_error (scaled_font, status);
556
0
}
557
558
void
559
_cairo_scaled_font_unregister_placeholder_and_lock_font_map (cairo_scaled_font_t *scaled_font)
560
0
{
561
0
    cairo_scaled_font_t *placeholder_scaled_font;
562
563
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
564
565
    /* temporary hash value to match the placeholder */
566
0
    scaled_font->hash_entry.hash
567
0
  = _cairo_scaled_font_compute_hash (scaled_font);
568
0
    placeholder_scaled_font =
569
0
  _cairo_hash_table_lookup (cairo_scaled_font_map->hash_table,
570
0
          &scaled_font->hash_entry);
571
0
    assert (placeholder_scaled_font != NULL);
572
0
    assert (placeholder_scaled_font->placeholder);
573
0
    assert (CAIRO_MUTEX_IS_LOCKED (placeholder_scaled_font->mutex));
574
575
0
    _cairo_hash_table_remove (cairo_scaled_font_map->hash_table,
576
0
            &placeholder_scaled_font->hash_entry);
577
578
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
579
580
0
    CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
581
0
    cairo_scaled_font_destroy (placeholder_scaled_font);
582
583
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
584
0
}
585
586
static void
587
_cairo_scaled_font_placeholder_wait_for_creation_to_finish (cairo_scaled_font_t *placeholder_scaled_font)
588
0
{
589
    /* reference the place holder so it doesn't go away */
590
0
    cairo_scaled_font_reference (placeholder_scaled_font);
591
592
    /* now unlock the fontmap mutex so creation has a chance to finish */
593
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
594
595
    /* wait on placeholder mutex until we are awaken */
596
0
    CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
597
598
    /* ok, creation done.  just clean up and back out */
599
0
    CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
600
0
    cairo_scaled_font_destroy (placeholder_scaled_font);
601
602
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
603
0
}
604
605
/* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/)
606
 *
607
 * Not necessarily better than a lot of other hashes, but should be OK, and
608
 * well tested with binary data.
609
 */
610
611
384
#define FNV_64_PRIME ((uint64_t)0x00000100000001B3)
612
4
#define FNV1_64_INIT ((uint64_t)0xcbf29ce484222325)
613
614
static uint64_t
615
_hash_matrix_fnv (const cairo_matrix_t  *matrix,
616
      uint64_t     hval)
617
8
{
618
8
    const uint8_t *buffer = (const uint8_t *) matrix;
619
8
    int len = sizeof (cairo_matrix_t);
620
384
    do {
621
384
  hval *= FNV_64_PRIME;
622
384
  hval ^= *buffer++;
623
384
    } while (--len);
624
625
8
    return hval;
626
8
}
627
628
static uint64_t
629
_hash_mix_bits (uint64_t hash)
630
8
{
631
8
    hash += hash << 12;
632
8
    hash ^= hash >> 7;
633
8
    hash += hash << 3;
634
8
    hash ^= hash >> 17;
635
8
    hash += hash << 5;
636
8
    return hash;
637
8
}
638
639
static uintptr_t
640
_cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font)
641
4
{
642
4
    uint64_t hash = FNV1_64_INIT;
643
644
    /* We do a bytewise hash on the font matrices */
645
4
    hash = _hash_matrix_fnv (&scaled_font->font_matrix, hash);
646
4
    hash = _hash_matrix_fnv (&scaled_font->ctm, hash);
647
4
    hash = _hash_mix_bits (hash);
648
649
4
    hash ^= (uintptr_t) scaled_font->original_font_face;
650
4
    hash ^= cairo_font_options_hash (&scaled_font->options);
651
652
    /* final mixing of bits */
653
4
    hash = _hash_mix_bits (hash);
654
4
    assert (hash != ZOMBIE);
655
656
4
    return hash;
657
4
}
658
659
static void
660
_cairo_scaled_font_init_key (cairo_scaled_font_t        *scaled_font,
661
           cairo_font_face_t          *font_face,
662
           const cairo_matrix_t       *font_matrix,
663
           const cairo_matrix_t       *ctm,
664
           const cairo_font_options_t *options)
665
2
{
666
2
    scaled_font->status = CAIRO_STATUS_SUCCESS;
667
2
    scaled_font->placeholder = FALSE;
668
2
    scaled_font->font_face = font_face;
669
2
    scaled_font->original_font_face = font_face;
670
2
    scaled_font->font_matrix = *font_matrix;
671
2
    scaled_font->ctm = *ctm;
672
    /* ignore translation values in the ctm */
673
2
    scaled_font->ctm.x0 = 0.;
674
2
    scaled_font->ctm.y0 = 0.;
675
2
    _cairo_font_options_init_copy (&scaled_font->options, options);
676
677
2
    scaled_font->hash_entry.hash =
678
2
  _cairo_scaled_font_compute_hash (scaled_font);
679
2
}
680
681
static void
682
_cairo_scaled_font_fini_key (cairo_scaled_font_t *scaled_font)
683
2
{
684
2
    _cairo_font_options_fini (&scaled_font->options);
685
2
}
686
687
static cairo_bool_t
688
_cairo_scaled_font_keys_equal (const void *abstract_key_a,
689
             const void *abstract_key_b)
690
0
{
691
0
    const cairo_scaled_font_t *key_a = abstract_key_a;
692
0
    const cairo_scaled_font_t *key_b = abstract_key_b;
693
694
0
    return key_a->original_font_face == key_b->original_font_face &&
695
0
           key_a->font_face == key_b->font_face &&
696
0
      memcmp ((unsigned char *)(&key_a->font_matrix.xx),
697
0
        (unsigned char *)(&key_b->font_matrix.xx),
698
0
        sizeof(cairo_matrix_t)) == 0 &&
699
0
      memcmp ((unsigned char *)(&key_a->ctm.xx),
700
0
        (unsigned char *)(&key_b->ctm.xx),
701
0
        sizeof(cairo_matrix_t)) == 0 &&
702
0
      cairo_font_options_equal (&key_a->options, &key_b->options);
703
0
}
704
705
static cairo_bool_t
706
_cairo_scaled_font_matches (const cairo_scaled_font_t *scaled_font,
707
                      const cairo_font_face_t *font_face,
708
          const cairo_matrix_t *font_matrix,
709
          const cairo_matrix_t *ctm,
710
          const cairo_font_options_t *options)
711
21
{
712
21
    return scaled_font->original_font_face == font_face &&
713
20
      memcmp ((unsigned char *)(&scaled_font->font_matrix.xx),
714
20
        (unsigned char *)(&font_matrix->xx),
715
20
        sizeof(cairo_matrix_t)) == 0 &&
716
20
      memcmp ((unsigned char *)(&scaled_font->ctm.xx),
717
20
        (unsigned char *)(&ctm->xx),
718
20
        sizeof(cairo_matrix_t)) == 0 &&
719
20
      cairo_font_options_equal (&scaled_font->options, options);
720
21
}
721
722
/*
723
 * Basic #cairo_scaled_font_t object management
724
 */
725
726
cairo_status_t
727
_cairo_scaled_font_init (cairo_scaled_font_t               *scaled_font,
728
       cairo_font_face_t       *font_face,
729
       const cairo_matrix_t              *font_matrix,
730
       const cairo_matrix_t              *ctm,
731
       const cairo_font_options_t    *options,
732
       const cairo_scaled_font_backend_t *backend)
733
2
{
734
2
    cairo_status_t status;
735
736
2
    status = cairo_font_options_status ((cairo_font_options_t *) options);
737
2
    if (unlikely (status))
738
0
  return status;
739
740
2
    scaled_font->status = CAIRO_STATUS_SUCCESS;
741
2
    scaled_font->placeholder = FALSE;
742
2
    scaled_font->font_face = font_face;
743
2
    scaled_font->original_font_face = font_face;
744
2
    scaled_font->font_matrix = *font_matrix;
745
2
    scaled_font->ctm = *ctm;
746
    /* ignore translation values in the ctm */
747
2
    scaled_font->ctm.x0 = 0.;
748
2
    scaled_font->ctm.y0 = 0.;
749
2
    _cairo_font_options_init_copy (&scaled_font->options, options);
750
751
2
    cairo_matrix_multiply (&scaled_font->scale,
752
2
         &scaled_font->font_matrix,
753
2
         &scaled_font->ctm);
754
755
2
    scaled_font->max_scale = MAX (fabs (scaled_font->scale.xx) + fabs (scaled_font->scale.xy),
756
2
          fabs (scaled_font->scale.yx) + fabs (scaled_font->scale.yy));
757
2
    scaled_font->scale_inverse = scaled_font->scale;
758
2
    status = cairo_matrix_invert (&scaled_font->scale_inverse);
759
2
    if (unlikely (status)) {
760
  /* If the font scale matrix is rank 0, just using an all-zero inverse matrix
761
   * makes everything work correctly.  This make font size 0 work without
762
   * producing an error.
763
   *
764
   * FIXME:  If the scale is rank 1, we still go into error mode.  But then
765
   * again, that's what we do everywhere in cairo.
766
   *
767
   * Also, the check for == 0. below may be too harsh...
768
   */
769
0
        if (_cairo_matrix_is_scale_0 (&scaled_font->scale)) {
770
0
      cairo_matrix_init (&scaled_font->scale_inverse,
771
0
             0, 0, 0, 0,
772
0
             -scaled_font->scale.x0,
773
0
             -scaled_font->scale.y0);
774
0
  } else
775
0
      return status;
776
0
    }
777
778
2
    scaled_font->glyphs = _cairo_hash_table_create (NULL);
779
2
    if (unlikely (scaled_font->glyphs == NULL))
780
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
781
782
2
    cairo_list_init (&scaled_font->glyph_pages);
783
2
    scaled_font->cache_frozen = FALSE;
784
2
    scaled_font->global_cache_frozen = FALSE;
785
2
    _cairo_array_init (&scaled_font->recording_surfaces_to_free, sizeof (cairo_surface_t *));
786
787
2
    scaled_font->holdover = FALSE;
788
2
    scaled_font->finished = FALSE;
789
790
2
    CAIRO_REFERENCE_COUNT_INIT (&scaled_font->ref_count, 1);
791
792
2
    _cairo_user_data_array_init (&scaled_font->user_data);
793
794
2
    cairo_font_face_reference (font_face);
795
2
    scaled_font->original_font_face = NULL;
796
797
2
    CAIRO_RECURSIVE_MUTEX_INIT (scaled_font->mutex);
798
799
2
    cairo_list_init (&scaled_font->dev_privates);
800
801
2
    scaled_font->backend = backend;
802
2
    cairo_list_init (&scaled_font->link);
803
804
2
    return CAIRO_STATUS_SUCCESS;
805
2
}
806
807
static void _cairo_scaled_font_free_recording_surfaces (cairo_scaled_font_t *scaled_font)
808
2
{
809
2
    int num_recording_surfaces;
810
2
    cairo_surface_t *surface;
811
812
2
    num_recording_surfaces = _cairo_array_num_elements (&scaled_font->recording_surfaces_to_free);
813
2
    if (num_recording_surfaces > 0) {
814
0
  for (int i = 0; i < num_recording_surfaces; i++) {
815
0
      _cairo_array_copy_element (&scaled_font->recording_surfaces_to_free, i, &surface);
816
0
      cairo_surface_finish (surface);
817
0
      cairo_surface_destroy (surface);
818
0
  }
819
0
  _cairo_array_truncate (&scaled_font->recording_surfaces_to_free, 0);
820
0
    }
821
2
}
822
823
void
824
_cairo_scaled_font_freeze_cache (cairo_scaled_font_t *scaled_font)
825
2
{
826
    /* ensure we do not modify an error object */
827
2
    assert (scaled_font->status == CAIRO_STATUS_SUCCESS);
828
829
2
    CAIRO_MUTEX_LOCK (scaled_font->mutex);
830
2
    scaled_font->cache_frozen = TRUE;
831
2
}
832
833
void
834
_cairo_scaled_font_thaw_cache (cairo_scaled_font_t *scaled_font)
835
2
{
836
2
    assert (scaled_font->cache_frozen);
837
838
2
    if (scaled_font->global_cache_frozen) {
839
1
  CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
840
1
  _cairo_cache_thaw (&cairo_scaled_glyph_page_cache);
841
1
  CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
842
1
  scaled_font->global_cache_frozen = FALSE;
843
1
    }
844
845
2
    _cairo_scaled_font_free_recording_surfaces (scaled_font);
846
847
2
    scaled_font->cache_frozen = FALSE;
848
2
    CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
849
2
}
850
851
void
852
_cairo_scaled_font_reset_cache (cairo_scaled_font_t *scaled_font)
853
0
{
854
0
    cairo_scaled_glyph_page_t *page;
855
856
0
    CAIRO_MUTEX_LOCK (scaled_font->mutex);
857
0
    assert (! scaled_font->cache_frozen);
858
0
    assert (! scaled_font->global_cache_frozen);
859
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
860
861
0
    cairo_list_foreach_entry (page,
862
0
            cairo_scaled_glyph_page_t,
863
0
            &scaled_font->glyph_pages,
864
0
            link) {
865
0
  cairo_scaled_glyph_page_cache.size -= page->cache_entry.size;
866
0
  _cairo_hash_table_remove (cairo_scaled_glyph_page_cache.hash_table,
867
0
          (cairo_hash_entry_t *) &page->cache_entry);
868
0
    }
869
870
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
871
872
    /* Destroy scaled_font's pages while holding its lock only, and not the
873
     * global page cache lock. The destructor can cause us to recurse and
874
     * end up back here for a different scaled_font. */
875
876
0
    while (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
877
0
  page = cairo_list_first_entry (&scaled_font->glyph_pages,
878
0
               cairo_scaled_glyph_page_t,
879
0
               link);
880
0
  _cairo_scaled_glyph_page_destroy (scaled_font, page);
881
0
    }
882
883
0
    CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
884
0
}
885
886
cairo_status_t
887
_cairo_scaled_font_set_metrics (cairo_scaled_font_t     *scaled_font,
888
        cairo_font_extents_t      *fs_metrics)
889
2
{
890
2
    cairo_status_t status;
891
2
    double  font_scale_x, font_scale_y;
892
893
2
    scaled_font->fs_extents = *fs_metrics;
894
895
2
    status = _cairo_matrix_compute_basis_scale_factors (&scaled_font->font_matrix,
896
2
              &font_scale_x, &font_scale_y,
897
2
              1);
898
2
    if (unlikely (status))
899
0
  return status;
900
901
    /*
902
     * The font responded in unscaled units, scale by the font
903
     * matrix scale factors to get to user space
904
     */
905
906
2
    scaled_font->extents.ascent = fs_metrics->ascent * font_scale_y;
907
2
    scaled_font->extents.descent = fs_metrics->descent * font_scale_y;
908
2
    scaled_font->extents.height = fs_metrics->height * font_scale_y;
909
2
    scaled_font->extents.max_x_advance = fs_metrics->max_x_advance * font_scale_x;
910
2
    scaled_font->extents.max_y_advance = fs_metrics->max_y_advance * font_scale_y;
911
912
2
    return CAIRO_STATUS_SUCCESS;
913
2
}
914
915
static void
916
_cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font)
917
0
{
918
0
    assert (! scaled_font->cache_frozen);
919
0
    assert (! scaled_font->global_cache_frozen);
920
0
    scaled_font->finished = TRUE;
921
922
0
    _cairo_scaled_font_reset_cache (scaled_font);
923
0
    _cairo_hash_table_destroy (scaled_font->glyphs);
924
0
    _cairo_font_options_fini (&scaled_font->options);
925
926
0
    cairo_font_face_destroy (scaled_font->font_face);
927
0
    cairo_font_face_destroy (scaled_font->original_font_face);
928
929
0
    _cairo_scaled_font_free_recording_surfaces (scaled_font);
930
0
    _cairo_array_fini (&scaled_font->recording_surfaces_to_free);
931
932
0
    CAIRO_MUTEX_FINI (scaled_font->mutex);
933
934
0
    while (! cairo_list_is_empty (&scaled_font->dev_privates)) {
935
0
  cairo_scaled_font_private_t *private =
936
0
      cairo_list_first_entry (&scaled_font->dev_privates,
937
0
            cairo_scaled_font_private_t,
938
0
            link);
939
0
  private->destroy (private, scaled_font);
940
0
    }
941
942
0
    if (scaled_font->backend != NULL && scaled_font->backend->fini != NULL)
943
0
  scaled_font->backend->fini (scaled_font);
944
945
0
    _cairo_user_data_array_fini (&scaled_font->user_data);
946
0
}
947
948
void
949
_cairo_scaled_font_fini (cairo_scaled_font_t *scaled_font)
950
0
{
951
    /* Release the lock to avoid the possibility of a recursive
952
     * deadlock when the scaled font destroy closure gets called. */
953
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
954
0
    _cairo_scaled_font_fini_internal (scaled_font);
955
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
956
0
}
957
958
void
959
_cairo_scaled_font_attach_private (cairo_scaled_font_t *scaled_font,
960
           cairo_scaled_font_private_t *private,
961
           const void *key,
962
           void (*destroy) (cairo_scaled_font_private_t *,
963
                cairo_scaled_font_t *))
964
0
{
965
0
    private->key = key;
966
0
    private->destroy = destroy;
967
0
    cairo_list_add (&private->link, &scaled_font->dev_privates);
968
0
}
969
970
cairo_scaled_font_private_t *
971
_cairo_scaled_font_find_private (cairo_scaled_font_t *scaled_font,
972
         const void *key)
973
0
{
974
0
    cairo_scaled_font_private_t *priv;
975
976
0
    cairo_list_foreach_entry (priv, cairo_scaled_font_private_t,
977
0
            &scaled_font->dev_privates, link)
978
0
    {
979
0
  if (priv->key == key) {
980
0
      if (priv->link.prev != &scaled_font->dev_privates)
981
0
    cairo_list_move (&priv->link, &scaled_font->dev_privates);
982
0
      return priv;
983
0
  }
984
0
    }
985
986
0
    return NULL;
987
0
}
988
989
void
990
_cairo_scaled_glyph_attach_private (cairo_scaled_glyph_t *scaled_glyph,
991
           cairo_scaled_glyph_private_t *private,
992
           const void *key,
993
           void (*destroy) (cairo_scaled_glyph_private_t *,
994
                cairo_scaled_glyph_t *,
995
                cairo_scaled_font_t *))
996
11
{
997
11
    private->key = key;
998
11
    private->destroy = destroy;
999
11
    cairo_list_add (&private->link, &scaled_glyph->dev_privates);
1000
11
}
1001
1002
cairo_scaled_glyph_private_t *
1003
_cairo_scaled_glyph_find_private (cairo_scaled_glyph_t *scaled_glyph,
1004
         const void *key)
1005
33
{
1006
33
    cairo_scaled_glyph_private_t *priv;
1007
1008
33
    cairo_list_foreach_entry (priv, cairo_scaled_glyph_private_t,
1009
33
            &scaled_glyph->dev_privates, link)
1010
33
    {
1011
33
  if (priv->key == key) {
1012
33
      if (priv->link.prev != &scaled_glyph->dev_privates)
1013
0
    cairo_list_move (&priv->link, &scaled_glyph->dev_privates);
1014
33
      return priv;
1015
33
  }
1016
33
    }
1017
1018
0
    return NULL;
1019
33
}
1020
1021
/**
1022
 * cairo_scaled_font_create:
1023
 * @font_face: a #cairo_font_face_t
1024
 * @font_matrix: font space to user space transformation matrix for the
1025
 *       font. In the simplest case of a N point font, this matrix is
1026
 *       just a scale by N, but it can also be used to shear the font
1027
 *       or stretch it unequally along the two axes. See
1028
 *       cairo_set_font_matrix().
1029
 * @ctm: user to device transformation matrix with which the font will
1030
 *       be used.
1031
 * @options: options to use when getting metrics for the font and
1032
 *           rendering with it.
1033
 *
1034
 * Creates a #cairo_scaled_font_t object from a font face and matrices that
1035
 * describe the size of the font and the environment in which it will
1036
 * be used.
1037
 *
1038
 * Return value: a newly created #cairo_scaled_font_t. Destroy with
1039
 *  cairo_scaled_font_destroy()
1040
 *
1041
 * Since: 1.0
1042
 **/
1043
cairo_scaled_font_t *
1044
cairo_scaled_font_create (cairo_font_face_t          *font_face,
1045
        const cairo_matrix_t       *font_matrix,
1046
        const cairo_matrix_t       *ctm,
1047
        const cairo_font_options_t *options)
1048
22
{
1049
22
    cairo_status_t status;
1050
22
    cairo_scaled_font_map_t *font_map;
1051
22
    cairo_font_face_t *original_font_face = font_face;
1052
22
    cairo_scaled_font_t key, *old = NULL, *scaled_font = NULL, *dead = NULL;
1053
22
    double det;
1054
1055
22
    status = font_face->status;
1056
22
    if (unlikely (status))
1057
0
  return _cairo_scaled_font_create_in_error (status);
1058
1059
22
    det = _cairo_matrix_compute_determinant (font_matrix);
1060
22
    if (! ISFINITE (det))
1061
0
  return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
1062
1063
22
    det = _cairo_matrix_compute_determinant (ctm);
1064
22
    if (! ISFINITE (det))
1065
0
  return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
1066
1067
22
    status = cairo_font_options_status ((cairo_font_options_t *) options);
1068
22
    if (unlikely (status))
1069
0
  return _cairo_scaled_font_create_in_error (status);
1070
1071
    /* Note that degenerate ctm or font_matrix *are* allowed.
1072
     * We want to support a font size of 0. */
1073
1074
22
    font_map = _cairo_scaled_font_map_lock ();
1075
22
    if (unlikely (font_map == NULL))
1076
0
  return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1077
1078
22
    scaled_font = font_map->mru_scaled_font;
1079
22
    if (scaled_font != NULL &&
1080
21
  _cairo_scaled_font_matches (scaled_font,
1081
21
                              font_face, font_matrix, ctm, options))
1082
20
    {
1083
20
  assert (scaled_font->hash_entry.hash != ZOMBIE);
1084
20
  assert (! scaled_font->placeholder);
1085
1086
20
  if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
1087
      /* We increment the reference count manually here, (rather
1088
       * than calling into cairo_scaled_font_reference), since we
1089
       * must modify the reference count while our lock is still
1090
       * held. */
1091
20
      _cairo_reference_count_inc (&scaled_font->ref_count);
1092
20
      _cairo_scaled_font_map_unlock ();
1093
20
      return scaled_font;
1094
20
  }
1095
1096
  /* the font has been put into an error status - abandon the cache */
1097
0
  _cairo_hash_table_remove (font_map->hash_table,
1098
0
          &scaled_font->hash_entry);
1099
0
  scaled_font->hash_entry.hash = ZOMBIE;
1100
0
  dead = scaled_font;
1101
0
  font_map->mru_scaled_font = NULL;
1102
0
    }
1103
1104
2
    _cairo_scaled_font_init_key (&key, font_face, font_matrix, ctm, options);
1105
1106
2
    while ((scaled_font = _cairo_hash_table_lookup (font_map->hash_table,
1107
2
                &key.hash_entry)))
1108
0
    {
1109
0
  if (! scaled_font->placeholder)
1110
0
      break;
1111
1112
  /* If the scaled font is being created (happens for user-font),
1113
   * just wait until it's done, then retry */
1114
0
  _cairo_scaled_font_placeholder_wait_for_creation_to_finish (scaled_font);
1115
0
    }
1116
2
    _cairo_scaled_font_fini_key (&key);
1117
1118
2
    if (scaled_font != NULL) {
1119
  /* If the original reference count is 0, then this font must have
1120
   * been found in font_map->holdovers, (which means this caching is
1121
   * actually working). So now we remove it from the holdovers
1122
   * array, unless we caught the font in the middle of destruction.
1123
   */
1124
0
  if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
1125
0
      if (scaled_font->holdover) {
1126
0
    int i;
1127
1128
0
    for (i = 0; i < font_map->num_holdovers; i++) {
1129
0
        if (font_map->holdovers[i] == scaled_font) {
1130
0
      font_map->num_holdovers--;
1131
0
      memmove (&font_map->holdovers[i],
1132
0
         &font_map->holdovers[i+1],
1133
0
         (font_map->num_holdovers - i) * sizeof (cairo_scaled_font_t*));
1134
0
      break;
1135
0
        }
1136
0
    }
1137
1138
0
    scaled_font->holdover = FALSE;
1139
0
      }
1140
1141
      /* reset any error status */
1142
0
      scaled_font->status = CAIRO_STATUS_SUCCESS;
1143
0
  }
1144
1145
0
  if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
1146
      /* We increment the reference count manually here, (rather
1147
       * than calling into cairo_scaled_font_reference), since we
1148
       * must modify the reference count while our lock is still
1149
       * held. */
1150
1151
0
      old = font_map->mru_scaled_font;
1152
0
      font_map->mru_scaled_font = scaled_font;
1153
      /* increment reference count for the mru cache */
1154
0
      _cairo_reference_count_inc (&scaled_font->ref_count);
1155
      /* and increment for the returned reference */
1156
0
      _cairo_reference_count_inc (&scaled_font->ref_count);
1157
0
      _cairo_scaled_font_map_unlock ();
1158
1159
0
      cairo_scaled_font_destroy (old);
1160
0
      if (font_face != original_font_face)
1161
0
    cairo_font_face_destroy (font_face);
1162
1163
0
      return scaled_font;
1164
0
  }
1165
1166
  /* the font has been put into an error status - abandon the cache */
1167
0
  _cairo_hash_table_remove (font_map->hash_table,
1168
0
          &scaled_font->hash_entry);
1169
0
  scaled_font->hash_entry.hash = ZOMBIE;
1170
0
    }
1171
1172
1173
    /* Otherwise create it and insert it into the hash table. */
1174
2
    if (font_face->backend->get_implementation != NULL) {
1175
2
  font_face = font_face->backend->get_implementation (font_face,
1176
2
                  font_matrix,
1177
2
                  ctm,
1178
2
                  options);
1179
2
  if (unlikely (font_face->status)) {
1180
0
      _cairo_scaled_font_map_unlock ();
1181
0
      return _cairo_scaled_font_create_in_error (font_face->status);
1182
0
  }
1183
2
    }
1184
1185
2
    status = font_face->backend->scaled_font_create (font_face, font_matrix,
1186
2
                 ctm, options, &scaled_font);
1187
2
    if (unlikely (status)) {
1188
0
  _cairo_scaled_font_map_unlock ();
1189
0
  if (font_face != original_font_face)
1190
0
      cairo_font_face_destroy (font_face);
1191
1192
0
  if (dead != NULL)
1193
0
      cairo_scaled_font_destroy (dead);
1194
1195
0
  return _cairo_scaled_font_create_in_error (status);
1196
0
    }
1197
    /* Or did we encounter an error whilst constructing the scaled font? */
1198
2
    if (unlikely (scaled_font->status)) {
1199
0
  _cairo_scaled_font_map_unlock ();
1200
0
  if (font_face != original_font_face)
1201
0
      cairo_font_face_destroy (font_face);
1202
1203
0
  if (dead != NULL)
1204
0
      cairo_scaled_font_destroy (dead);
1205
1206
0
  return scaled_font;
1207
0
    }
1208
1209
    /* Our caching above is defeated if the backend switches fonts on us -
1210
     * e.g. old incarnations of toy-font-face and lazily resolved
1211
     * ft-font-faces
1212
     */
1213
2
    assert (scaled_font->font_face == font_face);
1214
2
    assert (! scaled_font->cache_frozen);
1215
2
    assert (! scaled_font->global_cache_frozen);
1216
1217
2
    scaled_font->original_font_face =
1218
2
  cairo_font_face_reference (original_font_face);
1219
1220
2
    scaled_font->hash_entry.hash = _cairo_scaled_font_compute_hash(scaled_font);
1221
1222
2
    status = _cairo_hash_table_insert (font_map->hash_table,
1223
2
               &scaled_font->hash_entry);
1224
2
    if (likely (status == CAIRO_STATUS_SUCCESS)) {
1225
2
  old = font_map->mru_scaled_font;
1226
2
  font_map->mru_scaled_font = scaled_font;
1227
2
  _cairo_reference_count_inc (&scaled_font->ref_count);
1228
2
    }
1229
1230
2
    _cairo_scaled_font_map_unlock ();
1231
1232
2
    cairo_scaled_font_destroy (old);
1233
2
    if (font_face != original_font_face)
1234
0
  cairo_font_face_destroy (font_face);
1235
1236
2
    if (dead != NULL)
1237
0
  cairo_scaled_font_destroy (dead);
1238
1239
2
    if (unlikely (status)) {
1240
  /* We can't call _cairo_scaled_font_destroy here since it expects
1241
   * that the font has already been successfully inserted into the
1242
   * hash table. */
1243
0
  _cairo_scaled_font_fini_internal (scaled_font);
1244
0
  free (scaled_font);
1245
0
  return _cairo_scaled_font_create_in_error (status);
1246
0
    }
1247
1248
2
    return scaled_font;
1249
2
}
1250
1251
static cairo_scaled_font_t *_cairo_scaled_font_nil_objects[CAIRO_STATUS_LAST_STATUS + 1];
1252
1253
/* XXX This should disappear in favour of a common pool of error objects. */
1254
cairo_scaled_font_t *
1255
_cairo_scaled_font_create_in_error (cairo_status_t status)
1256
0
{
1257
0
    cairo_scaled_font_t *scaled_font;
1258
1259
0
    assert (status != CAIRO_STATUS_SUCCESS);
1260
1261
0
    if (status == CAIRO_STATUS_NO_MEMORY)
1262
0
  return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
1263
1264
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
1265
0
    scaled_font = _cairo_scaled_font_nil_objects[status];
1266
0
    if (unlikely (scaled_font == NULL)) {
1267
0
  scaled_font = _cairo_calloc (sizeof (cairo_scaled_font_t));
1268
0
  if (unlikely (scaled_font == NULL)) {
1269
0
      CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1270
0
      _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
1271
0
      return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
1272
0
  }
1273
1274
0
  *scaled_font = _cairo_scaled_font_nil;
1275
0
  scaled_font->status = status;
1276
0
  _cairo_scaled_font_nil_objects[status] = scaled_font;
1277
0
    }
1278
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1279
1280
0
    return scaled_font;
1281
0
}
1282
1283
void
1284
_cairo_scaled_font_reset_static_data (void)
1285
0
{
1286
0
    int status;
1287
1288
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
1289
0
    for (status = CAIRO_STATUS_SUCCESS;
1290
0
   status <= CAIRO_STATUS_LAST_STATUS;
1291
0
   status++)
1292
0
    {
1293
0
  free (_cairo_scaled_font_nil_objects[status]);
1294
0
  _cairo_scaled_font_nil_objects[status] = NULL;
1295
0
    }
1296
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1297
1298
0
    CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
1299
0
    if (cairo_scaled_glyph_page_cache.hash_table != NULL) {
1300
0
  _cairo_cache_fini (&cairo_scaled_glyph_page_cache);
1301
0
  cairo_scaled_glyph_page_cache.hash_table = NULL;
1302
0
    }
1303
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
1304
0
}
1305
1306
/**
1307
 * cairo_scaled_font_reference:
1308
 * @scaled_font: a #cairo_scaled_font_t, (may be %NULL in which case
1309
 * this function does nothing)
1310
 *
1311
 * Increases the reference count on @scaled_font by one. This prevents
1312
 * @scaled_font from being destroyed until a matching call to
1313
 * cairo_scaled_font_destroy() is made.
1314
 *
1315
 * Use cairo_scaled_font_get_reference_count() to get the number of
1316
 * references to a #cairo_scaled_font_t.
1317
 *
1318
 * Returns: the referenced #cairo_scaled_font_t
1319
 *
1320
 * Since: 1.0
1321
 **/
1322
cairo_scaled_font_t *
1323
cairo_scaled_font_reference (cairo_scaled_font_t *scaled_font)
1324
26.6k
{
1325
26.6k
    if (scaled_font == NULL ||
1326
0
      CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1327
26.6k
  return scaled_font;
1328
1329
26.6k
    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
1330
1331
0
    _cairo_reference_count_inc (&scaled_font->ref_count);
1332
1333
0
    return scaled_font;
1334
0
}
1335
1336
/**
1337
 * cairo_scaled_font_destroy:
1338
 * @scaled_font: a #cairo_scaled_font_t
1339
 *
1340
 * Decreases the reference count on @font by one. If the result
1341
 * is zero, then @font and all associated resources are freed.
1342
 * See cairo_scaled_font_reference().
1343
 *
1344
 * Since: 1.0
1345
 **/
1346
void
1347
cairo_scaled_font_destroy (cairo_scaled_font_t *scaled_font)
1348
3.71M
{
1349
3.71M
    cairo_scaled_font_t *lru = NULL;
1350
3.71M
    cairo_scaled_font_map_t *font_map;
1351
1352
3.71M
    assert (CAIRO_MUTEX_IS_UNLOCKED (_cairo_scaled_font_map_mutex));
1353
1354
3.71M
    if (scaled_font == NULL ||
1355
23
      CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1356
3.71M
  return;
1357
1358
3.71M
    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
1359
1360
23
    font_map = _cairo_scaled_font_map_lock ();
1361
23
    assert (font_map != NULL);
1362
1363
23
    if (! _cairo_reference_count_dec_and_test (&scaled_font->ref_count))
1364
22
  goto unlock;
1365
1366
23
    assert (! scaled_font->cache_frozen);
1367
1
    assert (! scaled_font->global_cache_frozen);
1368
1369
    /* Another thread may have resurrected the font whilst we waited */
1370
1
    if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
1371
1
  if (! scaled_font->placeholder &&
1372
1
      scaled_font->hash_entry.hash != ZOMBIE)
1373
1
  {
1374
      /* Another thread may have already inserted us into the holdovers */
1375
1
      if (scaled_font->holdover)
1376
0
    goto unlock;
1377
1378
      /* Rather than immediately destroying this object, we put it into
1379
       * the font_map->holdovers array in case it will get used again
1380
       * soon (and is why we must hold the lock over the atomic op on
1381
       * the reference count). To make room for it, we do actually
1382
       * destroy the least-recently-used holdover.
1383
       */
1384
1385
1
      if (font_map->num_holdovers == CAIRO_SCALED_FONT_MAX_HOLDOVERS) {
1386
0
    lru = font_map->holdovers[0];
1387
0
    assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&lru->ref_count));
1388
1389
0
    _cairo_hash_table_remove (font_map->hash_table,
1390
0
            &lru->hash_entry);
1391
1392
0
    font_map->num_holdovers--;
1393
0
    memmove (&font_map->holdovers[0],
1394
0
       &font_map->holdovers[1],
1395
0
       font_map->num_holdovers * sizeof (cairo_scaled_font_t*));
1396
0
      }
1397
1398
1
      font_map->holdovers[font_map->num_holdovers++] = scaled_font;
1399
1
      scaled_font->holdover = TRUE;
1400
1
  } else
1401
0
      lru = scaled_font;
1402
1
    }
1403
1404
23
  unlock:
1405
23
    _cairo_scaled_font_map_unlock ();
1406
1407
    /* If we pulled an item from the holdovers array, (while the font
1408
     * map lock was held, of course), then there is no way that anyone
1409
     * else could have acquired a reference to it. So we can now
1410
     * safely call fini on it without any lock held. This is desirable
1411
     * as we never want to call into any backend function with a lock
1412
     * held. */
1413
23
    if (lru != NULL) {
1414
0
  _cairo_scaled_font_fini_internal (lru);
1415
0
  free (lru);
1416
0
    }
1417
23
}
1418
1419
/**
1420
 * cairo_scaled_font_get_reference_count:
1421
 * @scaled_font: a #cairo_scaled_font_t
1422
 *
1423
 * Returns the current reference count of @scaled_font.
1424
 *
1425
 * Return value: the current reference count of @scaled_font.  If the
1426
 * object is a nil object, 0 will be returned.
1427
 *
1428
 * Since: 1.4
1429
 **/
1430
unsigned int
1431
cairo_scaled_font_get_reference_count (cairo_scaled_font_t *scaled_font)
1432
0
{
1433
0
    if (scaled_font == NULL ||
1434
0
      CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1435
0
  return 0;
1436
1437
0
    return CAIRO_REFERENCE_COUNT_GET_VALUE (&scaled_font->ref_count);
1438
0
}
1439
1440
/**
1441
 * cairo_scaled_font_get_user_data:
1442
 * @scaled_font: a #cairo_scaled_font_t
1443
 * @key: the address of the #cairo_user_data_key_t the user data was
1444
 * attached to
1445
 *
1446
 * Return user data previously attached to @scaled_font using the
1447
 * specified key.  If no user data has been attached with the given
1448
 * key this function returns %NULL.
1449
 *
1450
 * Return value: the user data previously attached or %NULL.
1451
 *
1452
 * Since: 1.4
1453
 **/
1454
void *
1455
cairo_scaled_font_get_user_data (cairo_scaled_font_t       *scaled_font,
1456
         const cairo_user_data_key_t *key)
1457
0
{
1458
0
    return _cairo_user_data_array_get_data (&scaled_font->user_data,
1459
0
              key);
1460
0
}
1461
1462
/**
1463
 * cairo_scaled_font_set_user_data:
1464
 * @scaled_font: a #cairo_scaled_font_t
1465
 * @key: the address of a #cairo_user_data_key_t to attach the user data to
1466
 * @user_data: the user data to attach to the #cairo_scaled_font_t
1467
 * @destroy: a #cairo_destroy_func_t which will be called when the
1468
 * #cairo_t is destroyed or when new user data is attached using the
1469
 * same key.
1470
 *
1471
 * Attach user data to @scaled_font.  To remove user data from a surface,
1472
 * call this function with the key that was used to set it and %NULL
1473
 * for @data.
1474
 *
1475
 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1476
 * slot could not be allocated for the user data.
1477
 *
1478
 * Since: 1.4
1479
 **/
1480
cairo_status_t
1481
cairo_scaled_font_set_user_data (cairo_scaled_font_t       *scaled_font,
1482
         const cairo_user_data_key_t *key,
1483
         void          *user_data,
1484
         cairo_destroy_func_t       destroy)
1485
0
{
1486
0
    if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1487
0
  return scaled_font->status;
1488
1489
0
    return _cairo_user_data_array_set_data (&scaled_font->user_data,
1490
0
              key, user_data, destroy);
1491
0
}
1492
1493
/* Public font API follows. */
1494
1495
/**
1496
 * cairo_scaled_font_extents:
1497
 * @scaled_font: a #cairo_scaled_font_t
1498
 * @extents: a #cairo_font_extents_t which to store the retrieved extents.
1499
 *
1500
 * Gets the metrics for a #cairo_scaled_font_t.
1501
 *
1502
 * Since: 1.0
1503
 **/
1504
void
1505
cairo_scaled_font_extents (cairo_scaled_font_t  *scaled_font,
1506
         cairo_font_extents_t *extents)
1507
0
{
1508
0
    if (scaled_font->status) {
1509
0
  extents->ascent  = 0.0;
1510
0
  extents->descent = 0.0;
1511
0
  extents->height  = 0.0;
1512
0
  extents->max_x_advance = 0.0;
1513
0
  extents->max_y_advance = 0.0;
1514
0
  return;
1515
0
    }
1516
1517
0
    *extents = scaled_font->extents;
1518
0
}
1519
1520
/**
1521
 * cairo_scaled_font_text_extents:
1522
 * @scaled_font: a #cairo_scaled_font_t
1523
 * @utf8: a NUL-terminated string of text, encoded in UTF-8
1524
 * @extents: a #cairo_text_extents_t which to store the retrieved extents.
1525
 *
1526
 * Gets the extents for a string of text. The extents describe a
1527
 * user-space rectangle that encloses the "inked" portion of the text
1528
 * drawn at the origin (0,0) (as it would be drawn by cairo_show_text()
1529
 * if the cairo graphics state were set to the same font_face,
1530
 * font_matrix, ctm, and font_options as @scaled_font).  Additionally,
1531
 * the x_advance and y_advance values indicate the amount by which the
1532
 * current point would be advanced by cairo_show_text().
1533
 *
1534
 * Note that whitespace characters do not directly contribute to the
1535
 * size of the rectangle (extents.width and extents.height). They do
1536
 * contribute indirectly by changing the position of non-whitespace
1537
 * characters. In particular, trailing whitespace characters are
1538
 * likely to not affect the size of the rectangle, though they will
1539
 * affect the x_advance and y_advance values.
1540
 *
1541
 * Since: 1.2
1542
 **/
1543
void
1544
cairo_scaled_font_text_extents (cairo_scaled_font_t   *scaled_font,
1545
        const char            *utf8,
1546
        cairo_text_extents_t  *extents)
1547
0
{
1548
0
    cairo_status_t status;
1549
0
    cairo_glyph_t *glyphs = NULL;
1550
0
    int num_glyphs;
1551
1552
0
    if (scaled_font->status)
1553
0
  goto ZERO_EXTENTS;
1554
1555
0
    if (utf8 == NULL)
1556
0
  goto ZERO_EXTENTS;
1557
1558
0
    status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
1559
0
                 utf8, -1,
1560
0
                 &glyphs, &num_glyphs,
1561
0
                 NULL, NULL,
1562
0
                 NULL);
1563
0
    if (unlikely (status)) {
1564
0
  status = _cairo_scaled_font_set_error (scaled_font, status);
1565
0
  goto ZERO_EXTENTS;
1566
0
    }
1567
1568
0
    cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, extents);
1569
0
    free (glyphs);
1570
1571
0
    return;
1572
1573
0
ZERO_EXTENTS:
1574
0
    extents->x_bearing = 0.0;
1575
0
    extents->y_bearing = 0.0;
1576
0
    extents->width  = 0.0;
1577
0
    extents->height = 0.0;
1578
0
    extents->x_advance = 0.0;
1579
0
    extents->y_advance = 0.0;
1580
0
}
1581
1582
/**
1583
 * cairo_scaled_font_glyph_extents:
1584
 * @scaled_font: a #cairo_scaled_font_t
1585
 * @glyphs: an array of glyph IDs with X and Y offsets.
1586
 * @num_glyphs: the number of glyphs in the @glyphs array
1587
 * @extents: a #cairo_text_extents_t which to store the retrieved extents.
1588
 *
1589
 * Gets the extents for an array of glyphs. The extents describe a
1590
 * user-space rectangle that encloses the "inked" portion of the
1591
 * glyphs, (as they would be drawn by cairo_show_glyphs() if the cairo
1592
 * graphics state were set to the same font_face, font_matrix, ctm,
1593
 * and font_options as @scaled_font).  Additionally, the x_advance and
1594
 * y_advance values indicate the amount by which the current point
1595
 * would be advanced by cairo_show_glyphs().
1596
 *
1597
 * Note that whitespace glyphs do not contribute to the size of the
1598
 * rectangle (extents.width and extents.height).
1599
 *
1600
 * Since: 1.0
1601
 **/
1602
void
1603
cairo_scaled_font_glyph_extents (cairo_scaled_font_t   *scaled_font,
1604
         const cairo_glyph_t   *glyphs,
1605
         int                    num_glyphs,
1606
         cairo_text_extents_t  *extents)
1607
0
{
1608
0
    cairo_status_t status;
1609
0
    int i;
1610
0
    double min_x = 0.0, min_y = 0.0, max_x = 0.0, max_y = 0.0;
1611
0
    cairo_bool_t visible = FALSE;
1612
0
    cairo_scaled_glyph_t *scaled_glyph = NULL;
1613
1614
0
    extents->x_bearing = 0.0;
1615
0
    extents->y_bearing = 0.0;
1616
0
    extents->width  = 0.0;
1617
0
    extents->height = 0.0;
1618
0
    extents->x_advance = 0.0;
1619
0
    extents->y_advance = 0.0;
1620
1621
0
    if (unlikely (scaled_font->status))
1622
0
  goto ZERO_EXTENTS;
1623
1624
0
    if (num_glyphs == 0)
1625
0
  goto ZERO_EXTENTS;
1626
1627
0
    if (unlikely (num_glyphs < 0)) {
1628
0
  _cairo_error_throw (CAIRO_STATUS_NEGATIVE_COUNT);
1629
  /* XXX Can't propagate error */
1630
0
  goto ZERO_EXTENTS;
1631
0
    }
1632
1633
0
    if (unlikely (glyphs == NULL)) {
1634
0
  _cairo_error_throw (CAIRO_STATUS_NULL_POINTER);
1635
  /* XXX Can't propagate error */
1636
0
  goto ZERO_EXTENTS;
1637
0
    }
1638
1639
0
    _cairo_scaled_font_freeze_cache (scaled_font);
1640
1641
0
    for (i = 0; i < num_glyphs; i++) {
1642
0
  double      left, top, right, bottom;
1643
1644
0
  status = _cairo_scaled_glyph_lookup (scaled_font,
1645
0
               glyphs[i].index,
1646
0
               CAIRO_SCALED_GLYPH_INFO_METRICS,
1647
0
               NULL, /* foreground color */
1648
0
               &scaled_glyph);
1649
0
  if (unlikely (status)) {
1650
0
      status = _cairo_scaled_font_set_error (scaled_font, status);
1651
0
      goto UNLOCK;
1652
0
  }
1653
1654
  /* "Ink" extents should skip "invisible" glyphs */
1655
0
  if (scaled_glyph->metrics.width == 0 || scaled_glyph->metrics.height == 0)
1656
0
      continue;
1657
1658
0
  left = scaled_glyph->metrics.x_bearing + glyphs[i].x;
1659
0
  right = left + scaled_glyph->metrics.width;
1660
0
  top = scaled_glyph->metrics.y_bearing + glyphs[i].y;
1661
0
  bottom = top + scaled_glyph->metrics.height;
1662
1663
0
  if (!visible) {
1664
0
      visible = TRUE;
1665
0
      min_x = left;
1666
0
      max_x = right;
1667
0
      min_y = top;
1668
0
      max_y = bottom;
1669
0
  } else {
1670
0
      if (left < min_x) min_x = left;
1671
0
      if (right > max_x) max_x = right;
1672
0
      if (top < min_y) min_y = top;
1673
0
      if (bottom > max_y) max_y = bottom;
1674
0
  }
1675
0
    }
1676
1677
0
    if (visible) {
1678
0
  extents->x_bearing = min_x - glyphs[0].x;
1679
0
  extents->y_bearing = min_y - glyphs[0].y;
1680
0
  extents->width = max_x - min_x;
1681
0
  extents->height = max_y - min_y;
1682
0
    } else {
1683
0
  extents->x_bearing = 0.0;
1684
0
  extents->y_bearing = 0.0;
1685
0
  extents->width = 0.0;
1686
0
  extents->height = 0.0;
1687
0
    }
1688
1689
0
    if (num_glyphs) {
1690
0
        double x0, y0, x1, y1;
1691
1692
0
  x0 = glyphs[0].x;
1693
0
  y0 = glyphs[0].y;
1694
1695
  /* scaled_glyph contains the glyph for num_glyphs - 1 already. */
1696
0
  x1 = glyphs[num_glyphs - 1].x + scaled_glyph->metrics.x_advance;
1697
0
  y1 = glyphs[num_glyphs - 1].y + scaled_glyph->metrics.y_advance;
1698
1699
0
  extents->x_advance = x1 - x0;
1700
0
  extents->y_advance = y1 - y0;
1701
0
    } else {
1702
0
  extents->x_advance = 0.0;
1703
0
  extents->y_advance = 0.0;
1704
0
    }
1705
1706
0
 UNLOCK:
1707
0
    _cairo_scaled_font_thaw_cache (scaled_font);
1708
0
    return;
1709
1710
0
ZERO_EXTENTS:
1711
0
    extents->x_bearing = 0.0;
1712
0
    extents->y_bearing = 0.0;
1713
0
    extents->width  = 0.0;
1714
0
    extents->height = 0.0;
1715
0
    extents->x_advance = 0.0;
1716
0
    extents->y_advance = 0.0;
1717
0
}
1718
1719
0
#define GLYPH_LUT_SIZE 64
1720
static cairo_status_t
1721
cairo_scaled_font_text_to_glyphs_internal_cached (cairo_scaled_font_t    *scaled_font,
1722
                double        x,
1723
                double        y,
1724
                const char       *utf8,
1725
                cairo_glyph_t    *glyphs,
1726
                cairo_text_cluster_t  **clusters,
1727
                int         num_chars)
1728
0
{
1729
0
    struct glyph_lut_elt {
1730
0
  unsigned long index;
1731
0
  double x_advance;
1732
0
  double y_advance;
1733
0
    } glyph_lut[GLYPH_LUT_SIZE];
1734
0
    uint32_t glyph_lut_unicode[GLYPH_LUT_SIZE];
1735
0
    cairo_status_t status;
1736
0
    const char *p;
1737
0
    int i;
1738
1739
0
    for (i = 0; i < GLYPH_LUT_SIZE; i++)
1740
0
  glyph_lut_unicode[i] = ~0U;
1741
1742
0
    p = utf8;
1743
0
    for (i = 0; i < num_chars; i++) {
1744
0
  int idx, num_bytes;
1745
0
  uint32_t unicode;
1746
0
  cairo_scaled_glyph_t *scaled_glyph;
1747
0
  struct glyph_lut_elt *glyph_slot;
1748
1749
0
  num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
1750
0
  p += num_bytes;
1751
1752
0
  glyphs[i].x = x;
1753
0
  glyphs[i].y = y;
1754
1755
0
  idx = unicode % ARRAY_LENGTH (glyph_lut);
1756
0
  glyph_slot = &glyph_lut[idx];
1757
0
  if (glyph_lut_unicode[idx] == unicode) {
1758
0
      glyphs[i].index = glyph_slot->index;
1759
0
      x += glyph_slot->x_advance;
1760
0
      y += glyph_slot->y_advance;
1761
0
  } else {
1762
0
      unsigned long g;
1763
1764
0
      g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
1765
0
      status = _cairo_scaled_glyph_lookup (scaled_font,
1766
0
             g,
1767
0
             CAIRO_SCALED_GLYPH_INFO_METRICS,
1768
0
             NULL, /* foreground color */
1769
0
             &scaled_glyph);
1770
0
      if (unlikely (status))
1771
0
    return status;
1772
1773
0
      x += scaled_glyph->metrics.x_advance;
1774
0
      y += scaled_glyph->metrics.y_advance;
1775
1776
0
      glyph_lut_unicode[idx] = unicode;
1777
0
      glyph_slot->index = g;
1778
0
      glyph_slot->x_advance = scaled_glyph->metrics.x_advance;
1779
0
      glyph_slot->y_advance = scaled_glyph->metrics.y_advance;
1780
1781
0
      glyphs[i].index = g;
1782
0
  }
1783
1784
0
  if (clusters) {
1785
0
      (*clusters)[i].num_bytes  = num_bytes;
1786
0
      (*clusters)[i].num_glyphs = 1;
1787
0
  }
1788
0
    }
1789
1790
0
    return CAIRO_STATUS_SUCCESS;
1791
0
}
1792
1793
static cairo_status_t
1794
cairo_scaled_font_text_to_glyphs_internal_uncached (cairo_scaled_font_t  *scaled_font,
1795
              double      x,
1796
              double      y,
1797
              const char     *utf8,
1798
              cairo_glyph_t    *glyphs,
1799
              cairo_text_cluster_t  **clusters,
1800
              int       num_chars)
1801
0
{
1802
0
    const char *p;
1803
0
    int i;
1804
1805
0
    p = utf8;
1806
0
    for (i = 0; i < num_chars; i++) {
1807
0
  unsigned long g;
1808
0
  int num_bytes;
1809
0
  uint32_t unicode;
1810
0
  cairo_scaled_glyph_t *scaled_glyph;
1811
0
  cairo_status_t status;
1812
1813
0
  num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
1814
0
  p += num_bytes;
1815
1816
0
  glyphs[i].x = x;
1817
0
  glyphs[i].y = y;
1818
1819
0
  g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
1820
1821
  /*
1822
   * No advance needed for a single character string. So, let's speed up
1823
   * one-character strings by skipping glyph lookup.
1824
   */
1825
0
  if (num_chars > 1) {
1826
0
      status = _cairo_scaled_glyph_lookup (scaled_font,
1827
0
               g,
1828
0
               CAIRO_SCALED_GLYPH_INFO_METRICS,
1829
0
               NULL, /* foreground color */
1830
0
               &scaled_glyph);
1831
0
      if (unlikely (status))
1832
0
    return status;
1833
1834
0
      x += scaled_glyph->metrics.x_advance;
1835
0
      y += scaled_glyph->metrics.y_advance;
1836
0
  }
1837
1838
0
  glyphs[i].index = g;
1839
1840
0
  if (clusters) {
1841
0
      (*clusters)[i].num_bytes  = num_bytes;
1842
0
      (*clusters)[i].num_glyphs = 1;
1843
0
  }
1844
0
    }
1845
1846
0
    return CAIRO_STATUS_SUCCESS;
1847
0
}
1848
1849
/**
1850
 * cairo_scaled_font_text_to_glyphs:
1851
 * @scaled_font: a #cairo_scaled_font_t
1852
 * @x: X position to place first glyph
1853
 * @y: Y position to place first glyph
1854
 * @utf8: a string of text encoded in UTF-8
1855
 * @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
1856
 * @glyphs: pointer to array of glyphs to fill
1857
 * @num_glyphs: pointer to number of glyphs
1858
 * @clusters: pointer to array of cluster mapping information to fill, or %NULL
1859
 * @num_clusters: pointer to number of clusters, or %NULL
1860
 * @cluster_flags: pointer to location to store cluster flags corresponding to the
1861
 *                 output @clusters, or %NULL
1862
 *
1863
 * Converts UTF-8 text to an array of glyphs, optionally with cluster
1864
 * mapping, that can be used to render later using @scaled_font.
1865
 *
1866
 * If @glyphs initially points to a non-%NULL value, that array is used
1867
 * as a glyph buffer, and @num_glyphs should point to the number of glyph
1868
 * entries available there.  If the provided glyph array is too short for
1869
 * the conversion, a new glyph array is allocated using cairo_glyph_allocate()
1870
 * and placed in @glyphs.  Upon return, @num_glyphs always contains the
1871
 * number of generated glyphs.  If the value @glyphs points to has changed
1872
 * after the call, the user is responsible for freeing the allocated glyph
1873
 * array using cairo_glyph_free().  This may happen even if the provided
1874
 * array was large enough.
1875
 *
1876
 * If @clusters is not %NULL, @num_clusters and @cluster_flags should not be %NULL,
1877
 * and cluster mapping will be computed.
1878
 * The semantics of how cluster array allocation works is similar to the glyph
1879
 * array.  That is,
1880
 * if @clusters initially points to a non-%NULL value, that array is used
1881
 * as a cluster buffer, and @num_clusters should point to the number of cluster
1882
 * entries available there.  If the provided cluster array is too short for
1883
 * the conversion, a new cluster array is allocated using cairo_text_cluster_allocate()
1884
 * and placed in @clusters.  Upon return, @num_clusters always contains the
1885
 * number of generated clusters.  If the value @clusters points at has changed
1886
 * after the call, the user is responsible for freeing the allocated cluster
1887
 * array using cairo_text_cluster_free().  This may happen even if the provided
1888
 * array was large enough.
1889
 *
1890
 * In the simplest case, @glyphs and @clusters can point to %NULL initially
1891
 * and a suitable array will be allocated.  In code:
1892
 * <informalexample><programlisting>
1893
 * cairo_status_t status;
1894
 *
1895
 * cairo_glyph_t *glyphs = NULL;
1896
 * int num_glyphs;
1897
 * cairo_text_cluster_t *clusters = NULL;
1898
 * int num_clusters;
1899
 * cairo_text_cluster_flags_t cluster_flags;
1900
 *
1901
 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1902
 *                                            x, y,
1903
 *                                            utf8, utf8_len,
1904
 *                                            &amp;glyphs, &amp;num_glyphs,
1905
 *                                            &amp;clusters, &amp;num_clusters, &amp;cluster_flags);
1906
 *
1907
 * if (status == CAIRO_STATUS_SUCCESS) {
1908
 *     cairo_show_text_glyphs (cr,
1909
 *                             utf8, utf8_len,
1910
 *                             glyphs, num_glyphs,
1911
 *                             clusters, num_clusters, cluster_flags);
1912
 *
1913
 *     cairo_glyph_free (glyphs);
1914
 *     cairo_text_cluster_free (clusters);
1915
 * }
1916
 * </programlisting></informalexample>
1917
 *
1918
 * If no cluster mapping is needed:
1919
 * <informalexample><programlisting>
1920
 * cairo_status_t status;
1921
 *
1922
 * cairo_glyph_t *glyphs = NULL;
1923
 * int num_glyphs;
1924
 *
1925
 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1926
 *                                            x, y,
1927
 *                                            utf8, utf8_len,
1928
 *                                            &amp;glyphs, &amp;num_glyphs,
1929
 *                                            NULL, NULL,
1930
 *                                            NULL);
1931
 *
1932
 * if (status == CAIRO_STATUS_SUCCESS) {
1933
 *     cairo_show_glyphs (cr, glyphs, num_glyphs);
1934
 *     cairo_glyph_free (glyphs);
1935
 * }
1936
 * </programlisting></informalexample>
1937
 *
1938
 * If stack-based glyph and cluster arrays are to be used for small
1939
 * arrays:
1940
 * <informalexample><programlisting>
1941
 * cairo_status_t status;
1942
 *
1943
 * cairo_glyph_t stack_glyphs[40];
1944
 * cairo_glyph_t *glyphs = stack_glyphs;
1945
 * int num_glyphs = sizeof (stack_glyphs) / sizeof (stack_glyphs[0]);
1946
 * cairo_text_cluster_t stack_clusters[40];
1947
 * cairo_text_cluster_t *clusters = stack_clusters;
1948
 * int num_clusters = sizeof (stack_clusters) / sizeof (stack_clusters[0]);
1949
 * cairo_text_cluster_flags_t cluster_flags;
1950
 *
1951
 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1952
 *                                            x, y,
1953
 *                                            utf8, utf8_len,
1954
 *                                            &amp;glyphs, &amp;num_glyphs,
1955
 *                                            &amp;clusters, &amp;num_clusters, &amp;cluster_flags);
1956
 *
1957
 * if (status == CAIRO_STATUS_SUCCESS) {
1958
 *     cairo_show_text_glyphs (cr,
1959
 *                             utf8, utf8_len,
1960
 *                             glyphs, num_glyphs,
1961
 *                             clusters, num_clusters, cluster_flags);
1962
 *
1963
 *     if (glyphs != stack_glyphs)
1964
 *         cairo_glyph_free (glyphs);
1965
 *     if (clusters != stack_clusters)
1966
 *         cairo_text_cluster_free (clusters);
1967
 * }
1968
 * </programlisting></informalexample>
1969
 *
1970
 * For details of how @clusters, @num_clusters, and @cluster_flags map input
1971
 * UTF-8 text to the output glyphs see cairo_show_text_glyphs().
1972
 *
1973
 * The output values can be readily passed to cairo_show_text_glyphs()
1974
 * cairo_show_glyphs(), or related functions, assuming that the exact
1975
 * same @scaled_font is used for the operation.
1976
 *
1977
 * Return value: %CAIRO_STATUS_SUCCESS upon success, or an error status
1978
 * if the input values are wrong or if conversion failed.  If the input
1979
 * values are correct but the conversion failed, the error status is also
1980
 * set on @scaled_font.
1981
 *
1982
 * Since: 1.8
1983
 **/
1984
0
#define CACHING_THRESHOLD 16
1985
cairo_status_t
1986
cairo_scaled_font_text_to_glyphs (cairo_scaled_font_t   *scaled_font,
1987
          double     x,
1988
          double     y,
1989
          const char          *utf8,
1990
          int            utf8_len,
1991
          cairo_glyph_t        **glyphs,
1992
          int           *num_glyphs,
1993
          cairo_text_cluster_t **clusters,
1994
          int           *num_clusters,
1995
          cairo_text_cluster_flags_t *cluster_flags)
1996
0
{
1997
0
    int num_chars = 0;
1998
0
    cairo_int_status_t status;
1999
0
    cairo_glyph_t *orig_glyphs;
2000
0
    cairo_text_cluster_t *orig_clusters;
2001
2002
0
    status = scaled_font->status;
2003
0
    if (unlikely (status))
2004
0
  return status;
2005
2006
    /* A slew of sanity checks */
2007
2008
    /* glyphs and num_glyphs can't be NULL */
2009
0
    if (glyphs     == NULL ||
2010
0
  num_glyphs == NULL) {
2011
0
  status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2012
0
  goto BAIL;
2013
0
    }
2014
2015
    /* Special case for NULL and -1 */
2016
0
    if (utf8 == NULL && utf8_len == -1)
2017
0
  utf8_len = 0;
2018
2019
    /* No NULLs for non-NULLs! */
2020
0
    if ((utf8_len && utf8          == NULL) ||
2021
0
  (clusters && num_clusters  == NULL) ||
2022
0
  (clusters && cluster_flags == NULL)) {
2023
0
  status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2024
0
  goto BAIL;
2025
0
    }
2026
2027
    /* A -1 for utf8_len means NUL-terminated */
2028
0
    if (utf8_len == -1)
2029
0
  utf8_len = strlen (utf8);
2030
2031
    /* A NULL *glyphs means no prealloced glyphs array */
2032
0
    if (glyphs && *glyphs == NULL)
2033
0
  *num_glyphs = 0;
2034
2035
    /* A NULL *clusters means no prealloced clusters array */
2036
0
    if (clusters && *clusters == NULL)
2037
0
  *num_clusters = 0;
2038
2039
0
    if (!clusters && num_clusters) {
2040
0
  num_clusters = NULL;
2041
0
    }
2042
2043
0
    if (cluster_flags) {
2044
0
  *cluster_flags = FALSE;
2045
0
    }
2046
2047
0
    if (!clusters && cluster_flags) {
2048
0
  cluster_flags = NULL;
2049
0
    }
2050
2051
    /* Apart from that, no negatives */
2052
0
    if (utf8_len < 0 ||
2053
0
  *num_glyphs < 0 ||
2054
0
  (num_clusters && *num_clusters < 0)) {
2055
0
  status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
2056
0
  goto BAIL;
2057
0
    }
2058
2059
0
    if (utf8_len == 0) {
2060
0
  status = CAIRO_STATUS_SUCCESS;
2061
0
  goto BAIL;
2062
0
    }
2063
2064
    /* validate input so backend does not have to */
2065
0
    status = _cairo_utf8_to_ucs4 (utf8, utf8_len, NULL, &num_chars);
2066
0
    if (unlikely (status))
2067
0
  goto BAIL;
2068
2069
0
    _cairo_scaled_font_freeze_cache (scaled_font);
2070
2071
0
    orig_glyphs = *glyphs;
2072
0
    orig_clusters = clusters ? *clusters : NULL;
2073
2074
0
    if (scaled_font->backend->text_to_glyphs) {
2075
0
  status = scaled_font->backend->text_to_glyphs (scaled_font, x, y,
2076
0
                   utf8, utf8_len,
2077
0
                   glyphs, num_glyphs,
2078
0
                   clusters, num_clusters,
2079
0
                   cluster_flags);
2080
0
        if (status != CAIRO_INT_STATUS_UNSUPPORTED) {
2081
0
      if (status == CAIRO_INT_STATUS_SUCCESS) {
2082
          /* The checks here are crude; we only should do them in
2083
     * user-font backend, but they don't hurt here.  This stuff
2084
     * can be hard to get right. */
2085
2086
0
          if (*num_glyphs < 0) {
2087
0
        status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
2088
0
        goto DONE;
2089
0
    }
2090
0
    if (*num_glyphs != 0 && *glyphs == NULL) {
2091
0
        status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2092
0
        goto DONE;
2093
0
    }
2094
2095
0
    if (clusters) {
2096
0
        if (*num_clusters < 0) {
2097
0
      status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
2098
0
      goto DONE;
2099
0
        }
2100
0
        if (*num_clusters != 0 && *clusters == NULL) {
2101
0
      status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2102
0
      goto DONE;
2103
0
        }
2104
2105
        /* Don't trust the backend, validate clusters! */
2106
0
        status =
2107
0
      _cairo_validate_text_clusters (utf8, utf8_len,
2108
0
                   *glyphs, *num_glyphs,
2109
0
                   *clusters, *num_clusters,
2110
0
                   *cluster_flags);
2111
0
    }
2112
0
      }
2113
2114
0
            goto DONE;
2115
0
  }
2116
0
    }
2117
2118
0
    if (*num_glyphs < num_chars) {
2119
0
  *glyphs = cairo_glyph_allocate (num_chars);
2120
0
  if (unlikely (*glyphs == NULL)) {
2121
0
      status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2122
0
      goto DONE;
2123
0
  }
2124
0
    }
2125
0
    *num_glyphs = num_chars;
2126
2127
0
    if (clusters) {
2128
0
  if (*num_clusters < num_chars) {
2129
0
      *clusters = cairo_text_cluster_allocate (num_chars);
2130
0
      if (unlikely (*clusters == NULL)) {
2131
0
    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2132
0
    goto DONE;
2133
0
      }
2134
0
  }
2135
0
  *num_clusters = num_chars;
2136
0
    }
2137
2138
0
    if (num_chars > CACHING_THRESHOLD)
2139
0
  status = cairo_scaled_font_text_to_glyphs_internal_cached (scaled_font,
2140
0
                     x, y,
2141
0
                     utf8,
2142
0
                     *glyphs,
2143
0
                     clusters,
2144
0
                     num_chars);
2145
0
    else
2146
0
  status = cairo_scaled_font_text_to_glyphs_internal_uncached (scaled_font,
2147
0
                   x, y,
2148
0
                   utf8,
2149
0
                   *glyphs,
2150
0
                   clusters,
2151
0
                   num_chars);
2152
2153
0
 DONE: /* error that should be logged on scaled_font happened */
2154
0
    _cairo_scaled_font_thaw_cache (scaled_font);
2155
2156
0
    if (unlikely (status)) {
2157
0
  *num_glyphs = 0;
2158
0
  if (*glyphs != orig_glyphs) {
2159
0
      cairo_glyph_free (*glyphs);
2160
0
      *glyphs = orig_glyphs;
2161
0
  }
2162
2163
0
  if (clusters) {
2164
0
      *num_clusters = 0;
2165
0
      if (*clusters != orig_clusters) {
2166
0
    cairo_text_cluster_free (*clusters);
2167
0
    *clusters = orig_clusters;
2168
0
      }
2169
0
  }
2170
0
    }
2171
2172
0
    return _cairo_scaled_font_set_error (scaled_font, status);
2173
2174
0
 BAIL: /* error with input arguments */
2175
2176
0
    if (num_glyphs)
2177
0
  *num_glyphs = 0;
2178
2179
0
    if (num_clusters)
2180
0
  *num_clusters = 0;
2181
2182
0
    return status;
2183
0
}
2184
2185
static inline cairo_bool_t
2186
_range_contains_glyph (const cairo_box_t *extents,
2187
           cairo_fixed_t left,
2188
           cairo_fixed_t top,
2189
           cairo_fixed_t right,
2190
           cairo_fixed_t bottom)
2191
28
{
2192
28
    if (left == right || top == bottom)
2193
2
  return FALSE;
2194
2195
26
    return right > extents->p1.x &&
2196
24
     left < extents->p2.x &&
2197
0
     bottom > extents->p1.y &&
2198
0
     top < extents->p2.y;
2199
28
}
2200
2201
static cairo_status_t
2202
_cairo_scaled_font_single_glyph_device_extents (cairo_scaled_font_t  *scaled_font,
2203
            const cairo_glyph_t  *glyph,
2204
            cairo_rectangle_int_t   *extents)
2205
0
{
2206
0
    cairo_scaled_glyph_t *scaled_glyph;
2207
0
    cairo_status_t status;
2208
2209
0
    _cairo_scaled_font_freeze_cache (scaled_font);
2210
0
    status = _cairo_scaled_glyph_lookup (scaled_font,
2211
0
           glyph->index,
2212
0
           CAIRO_SCALED_GLYPH_INFO_METRICS,
2213
0
           NULL, /* foreground color */
2214
0
           &scaled_glyph);
2215
0
    if (likely (status == CAIRO_STATUS_SUCCESS)) {
2216
0
  cairo_bool_t round_xy = _cairo_font_options_get_round_glyph_positions (&scaled_font->options) == CAIRO_ROUND_GLYPH_POS_ON;
2217
0
  cairo_box_t box;
2218
0
  cairo_fixed_t v;
2219
2220
0
  if (round_xy)
2221
0
      v = _cairo_fixed_from_int (_cairo_lround (glyph->x));
2222
0
  else
2223
0
      v = _cairo_fixed_from_double (glyph->x);
2224
0
  box.p1.x = v + scaled_glyph->bbox.p1.x;
2225
0
  box.p2.x = v + scaled_glyph->bbox.p2.x;
2226
2227
0
  if (round_xy)
2228
0
      v = _cairo_fixed_from_int (_cairo_lround (glyph->y));
2229
0
  else
2230
0
      v = _cairo_fixed_from_double (glyph->y);
2231
0
  box.p1.y = v + scaled_glyph->bbox.p1.y;
2232
0
  box.p2.y = v + scaled_glyph->bbox.p2.y;
2233
2234
0
  _cairo_box_round_to_rectangle (&box, extents);
2235
0
    }
2236
0
    _cairo_scaled_font_thaw_cache (scaled_font);
2237
0
    return status;
2238
0
}
2239
2240
/*
2241
 * Compute a device-space bounding box for the glyphs.
2242
 */
2243
cairo_status_t
2244
_cairo_scaled_font_glyph_device_extents (cairo_scaled_font_t   *scaled_font,
2245
           const cairo_glyph_t   *glyphs,
2246
           int                      num_glyphs,
2247
           cairo_rectangle_int_t   *extents,
2248
           cairo_bool_t *overlap_out)
2249
2
{
2250
2
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
2251
2
    cairo_box_t box = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN }};
2252
2
    cairo_scaled_glyph_t *glyph_cache[64];
2253
2
    cairo_bool_t overlap = overlap_out ? FALSE : TRUE;
2254
2
    cairo_round_glyph_positions_t round_glyph_positions = _cairo_font_options_get_round_glyph_positions (&scaled_font->options);
2255
2
    int i;
2256
2257
2
    if (unlikely (scaled_font->status))
2258
0
  return scaled_font->status;
2259
2260
2
    if (num_glyphs == 1) {
2261
0
  if (overlap_out)
2262
0
      *overlap_out = FALSE;
2263
0
  return _cairo_scaled_font_single_glyph_device_extents (scaled_font,
2264
0
                     glyphs,
2265
0
                     extents);
2266
0
    }
2267
2268
2
    _cairo_scaled_font_freeze_cache (scaled_font);
2269
2270
2
    memset (glyph_cache, 0, sizeof (glyph_cache));
2271
2272
30
    for (i = 0; i < num_glyphs; i++) {
2273
28
  cairo_scaled_glyph_t  *scaled_glyph;
2274
28
  cairo_fixed_t x, y, x1, y1, x2, y2;
2275
28
  int cache_index = glyphs[i].index % ARRAY_LENGTH (glyph_cache);
2276
2277
28
  scaled_glyph = glyph_cache[cache_index];
2278
28
  if (scaled_glyph == NULL ||
2279
6
      _cairo_scaled_glyph_index (scaled_glyph) != glyphs[i].index)
2280
22
  {
2281
22
      status = _cairo_scaled_glyph_lookup (scaled_font,
2282
22
             glyphs[i].index,
2283
22
             CAIRO_SCALED_GLYPH_INFO_METRICS,
2284
22
             NULL, /* foreground color */
2285
22
             &scaled_glyph);
2286
22
      if (unlikely (status))
2287
0
    break;
2288
2289
22
      glyph_cache[cache_index] = scaled_glyph;
2290
22
  }
2291
2292
28
  if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
2293
0
      x = _cairo_fixed_from_int (_cairo_lround (glyphs[i].x));
2294
28
  else
2295
28
      x = _cairo_fixed_from_double (glyphs[i].x);
2296
28
  x1 = x + scaled_glyph->bbox.p1.x;
2297
28
  x2 = x + scaled_glyph->bbox.p2.x;
2298
2299
28
  if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
2300
0
      y = _cairo_fixed_from_int (_cairo_lround (glyphs[i].y));
2301
28
  else
2302
28
      y = _cairo_fixed_from_double (glyphs[i].y);
2303
28
  y1 = y + scaled_glyph->bbox.p1.y;
2304
28
  y2 = y + scaled_glyph->bbox.p2.y;
2305
2306
28
  if (overlap == FALSE)
2307
28
      overlap = _range_contains_glyph (&box, x1, y1, x2, y2);
2308
2309
28
  if (x1 < box.p1.x) box.p1.x = x1;
2310
28
  if (x2 > box.p2.x) box.p2.x = x2;
2311
28
  if (y1 < box.p1.y) box.p1.y = y1;
2312
28
  if (y2 > box.p2.y) box.p2.y = y2;
2313
28
    }
2314
2315
2
    _cairo_scaled_font_thaw_cache (scaled_font);
2316
2
    if (unlikely (status))
2317
0
  return _cairo_scaled_font_set_error (scaled_font, status);
2318
2319
2
    if (box.p1.x < box.p2.x) {
2320
2
  _cairo_box_round_to_rectangle (&box, extents);
2321
2
    } else {
2322
0
  extents->x = extents->y = 0;
2323
0
  extents->width = extents->height = 0;
2324
0
    }
2325
2326
2
    if (overlap_out != NULL)
2327
2
  *overlap_out = overlap;
2328
2329
2
    return CAIRO_STATUS_SUCCESS;
2330
2
}
2331
2332
cairo_bool_t
2333
_cairo_scaled_font_glyph_approximate_extents (cairo_scaled_font_t  *scaled_font,
2334
                const cairo_glyph_t  *glyphs,
2335
                int                      num_glyphs,
2336
                cairo_rectangle_int_t   *extents)
2337
0
{
2338
0
    double x0, x1, y0, y1, pad;
2339
0
    int i;
2340
2341
    /* If any of the factors are suspect (i.e. the font is broken), bail */
2342
0
    if (scaled_font->fs_extents.max_x_advance == 0 ||
2343
0
  scaled_font->fs_extents.height == 0 ||
2344
0
  scaled_font->max_scale == 0)
2345
0
    {
2346
0
  return FALSE;
2347
0
    }
2348
2349
0
    assert (num_glyphs);
2350
2351
0
    x0 = x1 = glyphs[0].x;
2352
0
    y0 = y1 = glyphs[0].y;
2353
0
    for (i = 1; i < num_glyphs; i++) {
2354
0
  double g;
2355
2356
0
  g = glyphs[i].x;
2357
0
  if (g < x0) x0 = g;
2358
0
  if (g > x1) x1 = g;
2359
2360
0
  g = glyphs[i].y;
2361
0
  if (g < y0) y0 = g;
2362
0
  if (g > y1) y1 = g;
2363
0
    }
2364
2365
0
    pad = MAX(scaled_font->fs_extents.max_x_advance,
2366
0
        scaled_font->fs_extents.height);
2367
0
    pad *= scaled_font->max_scale;
2368
2369
0
    extents->x = floor (x0 - pad);
2370
0
    extents->width = ceil (x1 + pad) - extents->x;
2371
0
    extents->y = floor (y0 - pad);
2372
0
    extents->height = ceil (y1 + pad) - extents->y;
2373
0
    return TRUE;
2374
0
}
2375
2376
/* Add a single-device-unit rectangle to a path. */
2377
static cairo_status_t
2378
_add_unit_rectangle_to_path (cairo_path_fixed_t *path,
2379
           cairo_fixed_t x,
2380
           cairo_fixed_t y)
2381
0
{
2382
0
    cairo_status_t status;
2383
2384
0
    status = _cairo_path_fixed_move_to (path, x, y);
2385
0
    if (unlikely (status))
2386
0
  return status;
2387
2388
0
    status = _cairo_path_fixed_rel_line_to (path,
2389
0
              _cairo_fixed_from_int (1),
2390
0
              _cairo_fixed_from_int (0));
2391
0
    if (unlikely (status))
2392
0
  return status;
2393
2394
0
    status = _cairo_path_fixed_rel_line_to (path,
2395
0
              _cairo_fixed_from_int (0),
2396
0
              _cairo_fixed_from_int (1));
2397
0
    if (unlikely (status))
2398
0
  return status;
2399
2400
0
    status = _cairo_path_fixed_rel_line_to (path,
2401
0
              _cairo_fixed_from_int (-1),
2402
0
              _cairo_fixed_from_int (0));
2403
0
    if (unlikely (status))
2404
0
  return status;
2405
2406
0
    return _cairo_path_fixed_close_path (path);
2407
0
}
2408
2409
/**
2410
 * _trace_mask_to_path:
2411
 * @bitmap: An alpha mask (either %CAIRO_FORMAT_A1 or %CAIRO_FORMAT_A8)
2412
 * @path: An initialized path to hold the result
2413
 *
2414
 * Given a mask surface, (an alpha image), fill out the provided path
2415
 * so that when filled it would result in something that approximates
2416
 * the mask.
2417
 *
2418
 * Note: The current tracing code here is extremely primitive. It
2419
 * operates only on an A1 surface, (converting an A8 surface to A1 if
2420
 * necessary), and performs the tracing by drawing a little square
2421
 * around each pixel that is on in the mask. We do not pretend that
2422
 * this is a high-quality result. But we are leaving it up to someone
2423
 * who cares enough about getting a better result to implement
2424
 * something more sophisticated.
2425
 **/
2426
static cairo_status_t
2427
_trace_mask_to_path (cairo_image_surface_t *mask,
2428
         cairo_path_fixed_t *path,
2429
         double tx, double ty)
2430
0
{
2431
0
    const uint8_t *row;
2432
0
    int rows, cols, bytes_per_row;
2433
0
    int x, y, bit;
2434
0
    double xoff, yoff;
2435
0
    cairo_fixed_t x0, y0;
2436
0
    cairo_fixed_t px, py;
2437
0
    cairo_status_t status;
2438
2439
0
    mask = _cairo_image_surface_coerce_to_format (mask, CAIRO_FORMAT_A1);
2440
0
    status = mask->base.status;
2441
0
    if (unlikely (status))
2442
0
  return status;
2443
2444
0
    cairo_surface_get_device_offset (&mask->base, &xoff, &yoff);
2445
0
    x0 = _cairo_fixed_from_double (tx - xoff);
2446
0
    y0 = _cairo_fixed_from_double (ty - yoff);
2447
2448
0
    bytes_per_row = (mask->width + 7) / 8;
2449
0
    row = mask->data;
2450
0
    for (y = 0, rows = mask->height; rows--; row += mask->stride, y++) {
2451
0
  const uint8_t *byte_ptr = row;
2452
0
  x = 0;
2453
0
  py = _cairo_fixed_from_int (y);
2454
0
  for (cols = bytes_per_row; cols--; ) {
2455
0
      uint8_t byte = *byte_ptr++;
2456
0
      if (byte == 0) {
2457
0
    x += 8;
2458
0
    continue;
2459
0
      }
2460
2461
0
      byte = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (byte);
2462
0
      for (bit = 1 << 7; bit && x < mask->width; bit >>= 1, x++) {
2463
0
    if (byte & bit) {
2464
0
        px = _cairo_fixed_from_int (x);
2465
0
        status = _add_unit_rectangle_to_path (path,
2466
0
                px + x0,
2467
0
                py + y0);
2468
0
        if (unlikely (status))
2469
0
      goto BAIL;
2470
0
    }
2471
0
      }
2472
0
  }
2473
0
    }
2474
2475
0
BAIL:
2476
0
    cairo_surface_destroy (&mask->base);
2477
2478
0
    return status;
2479
0
}
2480
2481
cairo_status_t
2482
_cairo_scaled_font_glyph_path (cairo_scaled_font_t *scaled_font,
2483
             const cairo_glyph_t *glyphs,
2484
             int        num_glyphs,
2485
             cairo_path_fixed_t  *path)
2486
0
{
2487
0
    cairo_int_status_t status;
2488
0
    int i;
2489
2490
0
    status = scaled_font->status;
2491
0
    if (unlikely (status))
2492
0
  return status;
2493
2494
0
    _cairo_scaled_font_freeze_cache (scaled_font);
2495
0
    for (i = 0; i < num_glyphs; i++) {
2496
0
  cairo_scaled_glyph_t *scaled_glyph;
2497
2498
0
  status = _cairo_scaled_glyph_lookup (scaled_font,
2499
0
               glyphs[i].index,
2500
0
               CAIRO_SCALED_GLYPH_INFO_PATH,
2501
0
               NULL, /* foreground color */
2502
0
               &scaled_glyph);
2503
0
  if (status == CAIRO_INT_STATUS_SUCCESS) {
2504
0
      status = _cairo_path_fixed_append (path,
2505
0
                 scaled_glyph->path,
2506
0
                 _cairo_fixed_from_double (glyphs[i].x),
2507
0
                 _cairo_fixed_from_double (glyphs[i].y));
2508
2509
0
  } else if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
2510
      /* If the font is incapable of providing a path, then we'll
2511
       * have to trace our own from a surface.
2512
       */
2513
0
      status = _cairo_scaled_glyph_lookup (scaled_font,
2514
0
             glyphs[i].index,
2515
0
             CAIRO_SCALED_GLYPH_INFO_SURFACE,
2516
0
             NULL, /* foreground color */
2517
0
             &scaled_glyph);
2518
0
      if (unlikely (status))
2519
0
    goto BAIL;
2520
2521
0
      status = _trace_mask_to_path (scaled_glyph->surface, path,
2522
0
            glyphs[i].x, glyphs[i].y);
2523
0
  }
2524
2525
0
  if (unlikely (status))
2526
0
      goto BAIL;
2527
0
    }
2528
0
  BAIL:
2529
0
    _cairo_scaled_font_thaw_cache (scaled_font);
2530
2531
0
    return _cairo_scaled_font_set_error (scaled_font, status);
2532
0
}
2533
2534
/**
2535
 * _cairo_scaled_glyph_set_metrics:
2536
 * @scaled_glyph: a #cairo_scaled_glyph_t
2537
 * @scaled_font: a #cairo_scaled_font_t
2538
 * @fs_metrics: a #cairo_text_extents_t in font space
2539
 *
2540
 * _cairo_scaled_glyph_set_metrics() stores user space metrics
2541
 * for the specified glyph given font space metrics. It is
2542
 * called by the font backend when initializing a glyph with
2543
 * %CAIRO_SCALED_GLYPH_INFO_METRICS.
2544
 **/
2545
void
2546
_cairo_scaled_glyph_set_metrics (cairo_scaled_glyph_t *scaled_glyph,
2547
         cairo_scaled_font_t *scaled_font,
2548
         cairo_text_extents_t *fs_metrics)
2549
11
{
2550
11
    cairo_bool_t first = TRUE;
2551
11
    double hm, wm;
2552
11
    double min_user_x = 0.0, max_user_x = 0.0, min_user_y = 0.0, max_user_y = 0.0;
2553
11
    double min_device_x = 0.0, max_device_x = 0.0, min_device_y = 0.0, max_device_y = 0.0;
2554
11
    double device_x_advance, device_y_advance;
2555
2556
11
    scaled_glyph->fs_metrics = *fs_metrics;
2557
2558
33
    for (hm = 0.0; hm <= 1.0; hm += 1.0)
2559
66
  for (wm = 0.0; wm <= 1.0; wm += 1.0) {
2560
44
      double x, y;
2561
2562
      /* Transform this corner to user space */
2563
44
      x = fs_metrics->x_bearing + fs_metrics->width * wm;
2564
44
      y = fs_metrics->y_bearing + fs_metrics->height * hm;
2565
44
      cairo_matrix_transform_point (&scaled_font->font_matrix,
2566
44
            &x, &y);
2567
44
      if (first) {
2568
11
    min_user_x = max_user_x = x;
2569
11
    min_user_y = max_user_y = y;
2570
33
      } else {
2571
33
    if (x < min_user_x) min_user_x = x;
2572
33
    if (x > max_user_x) max_user_x = x;
2573
33
    if (y < min_user_y) min_user_y = y;
2574
33
    if (y > max_user_y) max_user_y = y;
2575
33
      }
2576
2577
      /* Transform this corner to device space from glyph origin */
2578
44
      x = fs_metrics->x_bearing + fs_metrics->width * wm;
2579
44
      y = fs_metrics->y_bearing + fs_metrics->height * hm;
2580
44
      cairo_matrix_transform_distance (&scaled_font->scale,
2581
44
               &x, &y);
2582
2583
44
      if (first) {
2584
11
    min_device_x = max_device_x = x;
2585
11
    min_device_y = max_device_y = y;
2586
33
      } else {
2587
33
    if (x < min_device_x) min_device_x = x;
2588
33
    if (x > max_device_x) max_device_x = x;
2589
33
    if (y < min_device_y) min_device_y = y;
2590
33
    if (y > max_device_y) max_device_y = y;
2591
33
      }
2592
44
      first = FALSE;
2593
44
  }
2594
11
    scaled_glyph->metrics.x_bearing = min_user_x;
2595
11
    scaled_glyph->metrics.y_bearing = min_user_y;
2596
11
    scaled_glyph->metrics.width = max_user_x - min_user_x;
2597
11
    scaled_glyph->metrics.height = max_user_y - min_user_y;
2598
2599
11
    scaled_glyph->metrics.x_advance = fs_metrics->x_advance;
2600
11
    scaled_glyph->metrics.y_advance = fs_metrics->y_advance;
2601
11
    cairo_matrix_transform_distance (&scaled_font->font_matrix,
2602
11
             &scaled_glyph->metrics.x_advance,
2603
11
             &scaled_glyph->metrics.y_advance);
2604
2605
11
    device_x_advance = fs_metrics->x_advance;
2606
11
    device_y_advance = fs_metrics->y_advance;
2607
11
    cairo_matrix_transform_distance (&scaled_font->scale,
2608
11
             &device_x_advance,
2609
11
             &device_y_advance);
2610
2611
11
    scaled_glyph->bbox.p1.x = _cairo_fixed_from_double (min_device_x);
2612
11
    scaled_glyph->bbox.p1.y = _cairo_fixed_from_double (min_device_y);
2613
11
    scaled_glyph->bbox.p2.x = _cairo_fixed_from_double (max_device_x);
2614
11
    scaled_glyph->bbox.p2.y = _cairo_fixed_from_double (max_device_y);
2615
2616
11
    scaled_glyph->x_advance = _cairo_lround (device_x_advance);
2617
11
    scaled_glyph->y_advance = _cairo_lround (device_y_advance);
2618
2619
11
    scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_METRICS;
2620
11
}
2621
2622
void
2623
_cairo_scaled_glyph_set_surface (cairo_scaled_glyph_t *scaled_glyph,
2624
         cairo_scaled_font_t *scaled_font,
2625
         cairo_image_surface_t *surface)
2626
0
{
2627
0
    if (scaled_glyph->surface != NULL)
2628
0
  cairo_surface_destroy (&scaled_glyph->surface->base);
2629
2630
    /* sanity check the backend glyph contents */
2631
0
    _cairo_debug_check_image_surface_is_defined (&surface->base);
2632
0
    scaled_glyph->surface = surface;
2633
2634
0
    if (surface != NULL)
2635
0
  scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_SURFACE;
2636
0
    else
2637
0
  scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_SURFACE;
2638
0
}
2639
2640
void
2641
_cairo_scaled_glyph_set_path (cairo_scaled_glyph_t *scaled_glyph,
2642
            cairo_scaled_font_t *scaled_font,
2643
            cairo_path_fixed_t *path)
2644
0
{
2645
0
    if (scaled_glyph->path != NULL)
2646
0
  _cairo_path_fixed_destroy (scaled_glyph->path);
2647
2648
0
    scaled_glyph->path = path;
2649
2650
0
    if (path != NULL)
2651
0
  scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_PATH;
2652
0
    else
2653
0
  scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_PATH;
2654
0
}
2655
2656
/**
2657
 * _cairo_scaled_glyph_set_recording_surface:
2658
 * @scaled_glyph: a #cairo_scaled_glyph_t
2659
 * @scaled_font: a #cairo_scaled_font_t
2660
 * @recording_surface: The recording surface
2661
 * @foreground_color: The foreground color that was used to record the
2662
 * glyph, or NULL if foreground color not required.
2663
 *
2664
 * Sets the surface that was used to record the glyph.
2665
 **/
2666
void
2667
_cairo_scaled_glyph_set_recording_surface (cairo_scaled_glyph_t *scaled_glyph,
2668
             cairo_scaled_font_t  *scaled_font,
2669
             cairo_surface_t      *recording_surface,
2670
             const cairo_color_t * foreground_color)
2671
0
{
2672
0
    if (scaled_glyph->recording_surface != NULL) {
2673
0
  cairo_surface_finish (scaled_glyph->recording_surface);
2674
0
  cairo_surface_destroy (scaled_glyph->recording_surface);
2675
0
    }
2676
2677
0
    scaled_glyph->recording_surface = recording_surface;
2678
0
    scaled_glyph->recording_uses_foreground_color = foreground_color != NULL;
2679
0
    if (foreground_color)
2680
0
  scaled_glyph->foreground_color = *foreground_color;
2681
2682
0
    if (recording_surface != NULL)
2683
0
  scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
2684
0
    else
2685
0
  scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
2686
0
}
2687
2688
/**
2689
 * _cairo_scaled_glyph_set_color_surface:
2690
 * @scaled_glyph: a #cairo_scaled_glyph_t
2691
 * @scaled_font: a #cairo_scaled_font_t
2692
 * @surface: The image surface
2693
 * @foreground_marker_color: The foreground color that was used to
2694
 * substitute the foreground_marker, or NULL if foreground_marker not
2695
 * used when rendering the surface color.
2696
 *
2697
 * Sets the color surface of the glyph.
2698
 **/
2699
void
2700
_cairo_scaled_glyph_set_color_surface (cairo_scaled_glyph_t  *scaled_glyph,
2701
                                 cairo_scaled_font_t   *scaled_font,
2702
                                 cairo_image_surface_t *surface,
2703
               const cairo_color_t   *foreground_marker_color)
2704
0
{
2705
0
    if (scaled_glyph->color_surface != NULL)
2706
0
  cairo_surface_destroy (&scaled_glyph->color_surface->base);
2707
2708
    /* sanity check the backend glyph contents */
2709
0
    _cairo_debug_check_image_surface_is_defined (&surface->base);
2710
0
    scaled_glyph->color_surface = surface;
2711
0
    scaled_glyph->recording_uses_foreground_marker = foreground_marker_color != NULL;
2712
0
    if (foreground_marker_color)
2713
0
  scaled_glyph->foreground_color = *foreground_marker_color;
2714
2715
0
    if (surface != NULL)
2716
0
  scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
2717
0
    else
2718
0
  scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
2719
0
}
2720
2721
/* _cairo_hash_table_random_entry () predicate. To avoid race conditions,
2722
 * the font is locked when tested. The font is unlocked in
2723
 * _cairo_scaled_glyph_page_pluck. */
2724
static cairo_bool_t
2725
_cairo_scaled_glyph_page_can_remove (const void *closure)
2726
0
{
2727
0
    const cairo_scaled_glyph_page_t *page = closure;
2728
0
    cairo_scaled_font_t *scaled_font;
2729
2730
0
    scaled_font = page->scaled_font;
2731
2732
0
    if (!CAIRO_MUTEX_TRY_LOCK (scaled_font->mutex))
2733
0
       return FALSE;
2734
2735
0
    if (scaled_font->cache_frozen != 0) {
2736
0
       CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
2737
0
       return FALSE;
2738
0
    }
2739
2740
0
    return TRUE;
2741
0
}
2742
2743
static cairo_status_t
2744
_cairo_scaled_font_allocate_glyph (cairo_scaled_font_t *scaled_font,
2745
           cairo_scaled_glyph_t **scaled_glyph)
2746
11
{
2747
11
    cairo_scaled_glyph_page_t *page;
2748
11
    cairo_status_t status;
2749
2750
11
    assert (scaled_font->cache_frozen);
2751
2752
    /* only the first page in the list may contain available slots */
2753
11
    if (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
2754
10
        page = cairo_list_last_entry (&scaled_font->glyph_pages,
2755
10
                                      cairo_scaled_glyph_page_t,
2756
10
                                      link);
2757
10
        if (page->num_glyphs < CAIRO_SCALED_GLYPH_PAGE_SIZE) {
2758
10
            *scaled_glyph = &page->glyphs[page->num_glyphs++];
2759
10
            return CAIRO_STATUS_SUCCESS;
2760
10
        }
2761
10
    }
2762
2763
1
    page = _cairo_calloc (sizeof (cairo_scaled_glyph_page_t));
2764
1
    if (unlikely (page == NULL))
2765
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2766
2767
1
    page->cache_entry.hash = (uintptr_t) scaled_font;
2768
1
    page->scaled_font = scaled_font;
2769
1
    page->cache_entry.size = 1; /* XXX occupancy weighting? */
2770
1
    page->num_glyphs = 0;
2771
2772
1
    CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
2773
1
    if (scaled_font->global_cache_frozen == FALSE) {
2774
1
  if (unlikely (cairo_scaled_glyph_page_cache.hash_table == NULL)) {
2775
1
      status = _cairo_cache_init (&cairo_scaled_glyph_page_cache,
2776
1
          NULL,
2777
1
          _cairo_scaled_glyph_page_can_remove,
2778
1
          _cairo_scaled_glyph_page_pluck,
2779
1
          MAX_GLYPH_PAGES_CACHED);
2780
1
      if (unlikely (status)) {
2781
0
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2782
0
    free (page);
2783
0
    return status;
2784
0
      }
2785
1
  }
2786
2787
1
  _cairo_cache_freeze (&cairo_scaled_glyph_page_cache);
2788
1
  scaled_font->global_cache_frozen = TRUE;
2789
1
    }
2790
2791
1
    status = _cairo_cache_insert (&cairo_scaled_glyph_page_cache,
2792
1
          &page->cache_entry);
2793
1
    CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2794
1
    if (unlikely (status)) {
2795
0
  free (page);
2796
0
  return status;
2797
0
    }
2798
2799
1
    cairo_list_add_tail (&page->link, &scaled_font->glyph_pages);
2800
2801
1
    *scaled_glyph = &page->glyphs[page->num_glyphs++];
2802
1
    return CAIRO_STATUS_SUCCESS;
2803
1
}
2804
2805
static void
2806
_cairo_scaled_font_free_last_glyph (cairo_scaled_font_t *scaled_font,
2807
                 cairo_scaled_glyph_t *scaled_glyph)
2808
0
{
2809
0
    cairo_scaled_glyph_page_t *page;
2810
2811
0
    assert (scaled_font->cache_frozen);
2812
0
    assert (! cairo_list_is_empty (&scaled_font->glyph_pages));
2813
0
    page = cairo_list_last_entry (&scaled_font->glyph_pages,
2814
0
                                  cairo_scaled_glyph_page_t,
2815
0
                                  link);
2816
0
    assert (scaled_glyph == &page->glyphs[page->num_glyphs-1]);
2817
2818
0
    _cairo_scaled_glyph_fini (scaled_font, scaled_glyph);
2819
2820
0
    if (--page->num_glyphs == 0) {
2821
0
  _cairo_scaled_font_thaw_cache (scaled_font);
2822
0
  CAIRO_MUTEX_LOCK (scaled_font->mutex);
2823
2824
0
  CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
2825
  /* Temporarily disconnect callback to avoid recursive locking */
2826
0
  cairo_scaled_glyph_page_cache.entry_destroy = NULL;
2827
0
  _cairo_cache_remove (&cairo_scaled_glyph_page_cache,
2828
0
                 &page->cache_entry);
2829
0
  _cairo_scaled_glyph_page_destroy (scaled_font, page);
2830
0
  cairo_scaled_glyph_page_cache.entry_destroy = _cairo_scaled_glyph_page_pluck;
2831
0
  CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2832
2833
0
  CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
2834
0
  _cairo_scaled_font_freeze_cache (scaled_font);
2835
0
    }
2836
0
}
2837
2838
/**
2839
 * _cairo_scaled_glyph_lookup:
2840
 * @scaled_font: a #cairo_scaled_font_t
2841
 * @index: the glyph to create
2842
 * @info: a #cairo_scaled_glyph_info_t marking which portions of
2843
 * the glyph should be filled in.
2844
 * @foreground_color - foreground color to use when rendering color
2845
 * fonts. Use NULL if not requesting
2846
 * CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE or
2847
 * CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE, or foreground color is
2848
 * unknown.
2849
 * @scaled_glyph_ret: a #cairo_scaled_glyph_t where the glyph
2850
 * is returned.
2851
 *
2852
 * If the desired info is not available, (for example, when trying to
2853
 * get INFO_PATH with a bitmapped font), this function will return
2854
 * %CAIRO_INT_STATUS_UNSUPPORTED.
2855
 *
2856
 * Note: This function must be called with the scaled font frozen, and it must
2857
 * remain frozen for as long as the @scaled_glyph_ret is alive. (If the scaled
2858
 * font was not frozen, then there is no guarantee that the glyph would not be
2859
 * evicted before you tried to access it.) See
2860
 * _cairo_scaled_font_freeze_cache() and _cairo_scaled_font_thaw_cache().
2861
 *
2862
 * Returns: a glyph with the requested portions filled in. Glyph
2863
 * lookup is cached and glyph will be automatically freed along
2864
 * with the scaled_font so no explicit free is required.
2865
 * @info can be one or more of:
2866
 *  %CAIRO_SCALED_GLYPH_INFO_METRICS - glyph metrics and bounding box
2867
 *  %CAIRO_SCALED_GLYPH_INFO_SURFACE - surface holding glyph image
2868
 *  %CAIRO_SCALED_GLYPH_INFO_PATH - path holding glyph outline in device space
2869
 *  %CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE - surface holding recording of glyph
2870
 *  %CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE - surface holding color glyph image
2871
 **/
2872
cairo_int_status_t
2873
_cairo_scaled_glyph_lookup (cairo_scaled_font_t *scaled_font,
2874
          unsigned long index,
2875
          cairo_scaled_glyph_info_t info,
2876
          const cairo_color_t   *foreground_color,
2877
          cairo_scaled_glyph_t **scaled_glyph_ret)
2878
22
{
2879
22
    cairo_int_status_t     status = CAIRO_INT_STATUS_SUCCESS;
2880
22
    cairo_scaled_glyph_t  *scaled_glyph;
2881
22
    cairo_scaled_glyph_info_t  need_info;
2882
22
    cairo_hash_entry_t           key;
2883
2884
22
    *scaled_glyph_ret = NULL;
2885
2886
22
    if (unlikely (scaled_font->status))
2887
0
  return scaled_font->status;
2888
2889
22
    assert (CAIRO_MUTEX_IS_LOCKED(scaled_font->mutex));
2890
22
    assert (scaled_font->cache_frozen);
2891
2892
22
    if (CAIRO_INJECT_FAULT ())
2893
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2894
2895
22
    if (foreground_color == NULL)
2896
22
  foreground_color = CAIRO_COLOR_BLACK;
2897
2898
    /*
2899
     * Check cache for glyph
2900
     */
2901
22
    key.hash = index;
2902
22
    scaled_glyph = _cairo_hash_table_lookup (scaled_font->glyphs, &key);
2903
22
    if (scaled_glyph == NULL) {
2904
11
  status = _cairo_scaled_font_allocate_glyph (scaled_font, &scaled_glyph);
2905
11
  if (unlikely (status))
2906
0
      goto err;
2907
2908
11
  memset (scaled_glyph, 0, sizeof (cairo_scaled_glyph_t));
2909
11
  _cairo_scaled_glyph_set_index (scaled_glyph, index);
2910
11
  cairo_list_init (&scaled_glyph->dev_privates);
2911
2912
  /* ask backend to initialize metrics and shape fields */
2913
11
  status =
2914
11
      scaled_font->backend->scaled_glyph_init (scaled_font,
2915
11
                 scaled_glyph,
2916
11
                 info | CAIRO_SCALED_GLYPH_INFO_METRICS,
2917
11
                 foreground_color);
2918
11
  if (unlikely (status)) {
2919
0
      _cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
2920
0
      goto err;
2921
0
  }
2922
2923
11
  status = _cairo_hash_table_insert (scaled_font->glyphs,
2924
11
             &scaled_glyph->hash_entry);
2925
11
  if (unlikely (status)) {
2926
0
      _cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
2927
0
      goto err;
2928
0
  }
2929
11
    }
2930
2931
    /*
2932
     * Check and see if the glyph, as provided,
2933
     * already has the requested data and amend it if not
2934
     */
2935
22
    need_info = info & ~scaled_glyph->has_info;
2936
2937
    /* If this is not a color glyph, don't try loading the color surface again. */
2938
22
    if ((need_info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) &&
2939
0
  scaled_glyph->color_glyph_set && !scaled_glyph->color_glyph)
2940
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
2941
2942
    /* If requesting a color surface or recording for a glyph that has
2943
     * used the foreground color to render the recording, and the
2944
     * foreground color has changed, request a new  recording. */
2945
22
    if ((info & (CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE | CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) &&
2946
0
  scaled_glyph->recording_uses_foreground_color &&
2947
0
  !_cairo_color_equal (foreground_color, &scaled_glyph->foreground_color))
2948
0
    {
2949
0
  need_info |= CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
2950
0
    }
2951
2952
    /* If requesting a color surface for a glyph that has
2953
     * used the foreground color to render the color_surface, and the
2954
     * foreground color has changed, request a new image. */
2955
22
    if (info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE &&
2956
0
  (scaled_glyph->recording_uses_foreground_marker || scaled_glyph->recording_uses_foreground_color) &&
2957
0
  !_cairo_color_equal (foreground_color, &scaled_glyph->foreground_color))
2958
0
    {
2959
0
      need_info |= CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE;
2960
0
    }
2961
2962
22
    if (need_info) {
2963
0
  status = scaled_font->backend->scaled_glyph_init (scaled_font,
2964
0
                scaled_glyph,
2965
0
                need_info,
2966
0
                foreground_color);
2967
0
  if (unlikely (status))
2968
0
      goto err;
2969
2970
  /* Don't trust the scaled_glyph_init() return value, the font
2971
   * backend may not even know about some of the info.  For example,
2972
   * no backend other than the user-fonts knows about recording-surface
2973
   * glyph info. */
2974
0
  if (info & ~scaled_glyph->has_info)
2975
0
      return CAIRO_INT_STATUS_UNSUPPORTED;
2976
0
    }
2977
2978
22
    *scaled_glyph_ret = scaled_glyph;
2979
22
    return CAIRO_STATUS_SUCCESS;
2980
2981
0
err:
2982
    /* It's not an error for the backend to not support the info we want. */
2983
0
    if (status != CAIRO_INT_STATUS_UNSUPPORTED)
2984
0
  status = _cairo_scaled_font_set_error (scaled_font, status);
2985
0
    return status;
2986
22
}
2987
2988
double
2989
_cairo_scaled_font_get_max_scale (cairo_scaled_font_t *scaled_font)
2990
44
{
2991
44
    return scaled_font->max_scale;
2992
44
}
2993
2994
/**
2995
 * cairo_scaled_font_get_font_face:
2996
 * @scaled_font: a #cairo_scaled_font_t
2997
 *
2998
 * Gets the font face that this scaled font uses.  This might be the
2999
 * font face passed to cairo_scaled_font_create(), but this does not
3000
 * hold true for all possible cases.
3001
 *
3002
 * Return value: The #cairo_font_face_t with which @scaled_font was
3003
 * created.  This object is owned by cairo. To keep a reference to it,
3004
 * you must call cairo_scaled_font_reference().
3005
 *
3006
 * Since: 1.2
3007
 **/
3008
cairo_font_face_t *
3009
cairo_scaled_font_get_font_face (cairo_scaled_font_t *scaled_font)
3010
0
{
3011
0
    if (scaled_font->status)
3012
0
  return (cairo_font_face_t*) &_cairo_font_face_nil;
3013
3014
0
    if (scaled_font->original_font_face != NULL)
3015
0
  return scaled_font->original_font_face;
3016
3017
0
    return scaled_font->font_face;
3018
0
}
3019
3020
/**
3021
 * cairo_scaled_font_get_font_matrix:
3022
 * @scaled_font: a #cairo_scaled_font_t
3023
 * @font_matrix: return value for the matrix
3024
 *
3025
 * Stores the font matrix with which @scaled_font was created into
3026
 * @matrix.
3027
 *
3028
 * Since: 1.2
3029
 **/
3030
void
3031
cairo_scaled_font_get_font_matrix (cairo_scaled_font_t  *scaled_font,
3032
           cairo_matrix_t *font_matrix)
3033
0
{
3034
0
    if (scaled_font->status) {
3035
0
  cairo_matrix_init_identity (font_matrix);
3036
0
  return;
3037
0
    }
3038
3039
0
    *font_matrix = scaled_font->font_matrix;
3040
0
}
3041
3042
/**
3043
 * cairo_scaled_font_get_ctm:
3044
 * @scaled_font: a #cairo_scaled_font_t
3045
 * @ctm: return value for the CTM
3046
 *
3047
 * Stores the CTM with which @scaled_font was created into @ctm.
3048
 * Note that the translation offsets (x0, y0) of the CTM are ignored
3049
 * by cairo_scaled_font_create().  So, the matrix this
3050
 * function returns always has 0,0 as x0,y0.
3051
 *
3052
 * Since: 1.2
3053
 **/
3054
void
3055
cairo_scaled_font_get_ctm (cairo_scaled_font_t  *scaled_font,
3056
         cairo_matrix_t *ctm)
3057
0
{
3058
0
    if (scaled_font->status) {
3059
0
  cairo_matrix_init_identity (ctm);
3060
0
  return;
3061
0
    }
3062
3063
0
    *ctm = scaled_font->ctm;
3064
0
}
3065
3066
/**
3067
 * cairo_scaled_font_get_scale_matrix:
3068
 * @scaled_font: a #cairo_scaled_font_t
3069
 * @scale_matrix: return value for the matrix
3070
 *
3071
 * Stores the scale matrix of @scaled_font into @matrix.
3072
 * The scale matrix is product of the font matrix and the ctm
3073
 * associated with the scaled font, and hence is the matrix mapping from
3074
 * font space to device space.
3075
 *
3076
 * Since: 1.8
3077
 **/
3078
void
3079
cairo_scaled_font_get_scale_matrix (cairo_scaled_font_t *scaled_font,
3080
            cairo_matrix_t  *scale_matrix)
3081
0
{
3082
0
    if (scaled_font->status) {
3083
0
  cairo_matrix_init_identity (scale_matrix);
3084
0
  return;
3085
0
    }
3086
3087
0
    *scale_matrix = scaled_font->scale;
3088
0
}
3089
3090
/**
3091
 * cairo_scaled_font_get_font_options:
3092
 * @scaled_font: a #cairo_scaled_font_t
3093
 * @options: return value for the font options
3094
 *
3095
 * Stores the font options with which @scaled_font was created into
3096
 * @options.
3097
 *
3098
 * Since: 1.2
3099
 **/
3100
void
3101
cairo_scaled_font_get_font_options (cairo_scaled_font_t   *scaled_font,
3102
            cairo_font_options_t  *options)
3103
0
{
3104
0
    if (cairo_font_options_status (options))
3105
0
  return;
3106
3107
0
    if (scaled_font->status) {
3108
0
  _cairo_font_options_init_default (options);
3109
0
  return;
3110
0
    }
3111
3112
0
    _cairo_font_options_fini (options);
3113
0
    _cairo_font_options_init_copy (options, &scaled_font->options);
3114
0
}
3115
3116
cairo_bool_t
3117
_cairo_scaled_font_has_color_glyphs (cairo_scaled_font_t *scaled_font)
3118
24
{
3119
24
    if (scaled_font->backend != NULL && scaled_font->backend->has_color_glyphs != NULL)
3120
24
        return scaled_font->backend->has_color_glyphs (scaled_font);
3121
0
    else
3122
0
       return FALSE;
3123
24
}