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