1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
3 * Copyright © 2005 Keith Packard
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.
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
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/
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.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Keith Packard
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>
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"
49 #define TOLERANCE 0.00001
51 #if _XOPEN_SOURCE >= 600 || defined (_ISOC99_SOURCE)
52 #define ISFINITE(x) isfinite (x)
54 #define ISFINITE(x) ((x) * (x) >= 0.) /* check for NaNs */
58 * SECTION:cairo-scaled-font
59 * @Title: cairo_scaled_font_t
60 * @Short_Description: Font face at particular size and options
61 * @See_Also: #cairo_font_face_t, #cairo_matrix_t, #cairo_font_options_t
63 * #cairo_scaled_font_t represents a realization of a font face at a particular
64 * size and transformation and a certain set of font options.
68 _cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font);
72 * We maintain a global pool of glyphs split between all active fonts. This
73 * allows a heavily used individual font to cache more glyphs than we could
74 * manage if we used per-font glyph caches, but at the same time maintains
75 * fairness across all fonts and provides a cap on the maximum number of
78 * The glyphs are allocated in pages, which are capped in the global pool.
79 * Using pages means we can reduce the frequency at which we have to probe the
80 * global pool and ameliorates the memory allocation pressure.
83 /* XXX: This number is arbitrary---we've never done any measurement of this. */
84 #define MAX_GLYPH_PAGES_CACHED 512
85 static cairo_cache_t cairo_scaled_glyph_page_cache;
87 #define CAIRO_SCALED_GLYPH_PAGE_SIZE 32
88 struct _cairo_scaled_glyph_page {
89 cairo_cache_entry_t cache_entry;
93 unsigned int num_glyphs;
94 cairo_scaled_glyph_t glyphs[CAIRO_SCALED_GLYPH_PAGE_SIZE];
100 * To store rasterizations of glyphs, we use an image surface and the
101 * device offset to represent the glyph origin.
103 * A device_transform converts from device space (a conceptual space) to
104 * surface space. For simple cases of translation only, it's called a
105 * device_offset and is public API (cairo_surface_[gs]et_device_offset()).
106 * A possibly better name for those functions could have been
107 * cairo_surface_[gs]et_origin(). So, that's what they do: they set where
108 * the device-space origin (0,0) is in the surface. If the origin is inside
109 * the surface, device_offset values are positive. It may look like this:
112 * (-x,-y) <-- negative numbers
116 * |......(0,0) <---|-- device-space origin
123 * (0,0) <-- surface-space origin
127 * |......(x,y) <--|-- device_offset
133 * In other words: device_offset is the coordinates of the device-space
134 * origin relative to the top-left of the surface.
136 * We use device offsets in a couple of places:
138 * - Public API: To let toolkits like Gtk+ give user a surface that
139 * only represents part of the final destination (say, the expose
140 * area), but has the same device space as the destination. In these
141 * cases device_offset is typically negative. Example:
148 * | | | <--|-- expose area
152 * In this case, the user of cairo API can set the device_space on
153 * the expose area to (-x,-y) to move the device space origin to that
154 * of the application window, such that drawing in the expose area
155 * surface and painting it in the application window has the same
156 * effect as drawing in the application window directly. Gtk+ has
157 * been using this feature.
159 * - Glyph surfaces: In most font rendering systems, glyph surfaces
160 * have an origin at (0,0) and a bounding box that is typically
161 * represented as (x_bearing,y_bearing,width,height). Depending on
162 * which way y progresses in the system, y_bearing may typically be
163 * negative (for systems similar to cairo, with origin at top left),
164 * or be positive (in systems like PDF with origin at bottom left).
165 * No matter which is the case, it is important to note that
166 * (x_bearing,y_bearing) is the coordinates of top-left of the glyph
167 * relative to the glyph origin. That is, for example:
169 * Scaled-glyph space:
171 * (x_bearing,y_bearing) <-- negative numbers
175 * |......(0,0) <---|-- glyph origin
179 * (width+x_bearing,height+y_bearing)
181 * Note the similarity of the origin to the device space. That is
182 * exactly how we use the device_offset to represent scaled glyphs:
183 * to use the device-space origin as the glyph origin.
185 * Now compare the scaled-glyph space to device-space and surface-space
186 * and convince yourself that:
188 * (x_bearing,y_bearing) = (-x,-y) = - device_offset
190 * That's right. If you are not convinced yet, contrast the definition
193 * "(x_bearing,y_bearing) is the coordinates of top-left of the
194 * glyph relative to the glyph origin."
196 * "In other words: device_offset is the coordinates of the
197 * device-space origin relative to the top-left of the surface."
199 * and note that glyph origin = device-space origin.
203 _cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font);
206 _cairo_scaled_glyph_fini (cairo_scaled_font_t *scaled_font,
207 cairo_scaled_glyph_t *scaled_glyph)
209 while (! cairo_list_is_empty (&scaled_glyph->dev_privates)) {
210 cairo_scaled_glyph_private_t *private =
211 cairo_list_first_entry (&scaled_glyph->dev_privates,
212 cairo_scaled_glyph_private_t,
214 private->destroy (private, scaled_glyph, scaled_font);
217 _cairo_image_scaled_glyph_fini (scaled_font, scaled_glyph);
219 if (scaled_glyph->surface != NULL)
220 cairo_surface_destroy (&scaled_glyph->surface->base);
222 if (scaled_glyph->path != NULL)
223 _cairo_path_fixed_destroy (scaled_glyph->path);
225 if (scaled_glyph->recording_surface != NULL) {
226 cairo_surface_finish (scaled_glyph->recording_surface);
227 cairo_surface_destroy (scaled_glyph->recording_surface);
232 static const cairo_scaled_font_t _cairo_scaled_font_nil = {
233 { ZOMBIE }, /* hash_entry */
234 CAIRO_STATUS_NO_MEMORY, /* status */
235 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
236 { 0, 0, 0, NULL }, /* user_data */
237 NULL, /* original_font_face */
238 NULL, /* font_face */
239 { 1., 0., 0., 1., 0, 0}, /* font_matrix */
240 { 1., 0., 0., 1., 0, 0}, /* ctm */
241 { CAIRO_ANTIALIAS_DEFAULT, /* options */
242 CAIRO_SUBPIXEL_ORDER_DEFAULT,
243 CAIRO_HINT_STYLE_DEFAULT,
244 CAIRO_HINT_METRICS_DEFAULT} ,
245 FALSE, /* placeholder */
246 FALSE, /* holdover */
248 { 1., 0., 0., 1., 0, 0}, /* scale */
249 { 1., 0., 0., 1., 0, 0}, /* scale_inverse */
251 { 0., 0., 0., 0., 0. }, /* extents */
252 { 0., 0., 0., 0., 0. }, /* fs_extents */
253 CAIRO_MUTEX_NIL_INITIALIZER,/* mutex */
255 { NULL, NULL }, /* pages */
256 FALSE, /* cache_frozen */
257 FALSE, /* global_cache_frozen */
258 { NULL, NULL }, /* privates */
263 * _cairo_scaled_font_set_error:
264 * @scaled_font: a scaled_font
265 * @status: a status value indicating an error
267 * Atomically sets scaled_font->status to @status and calls _cairo_error;
268 * Does nothing if status is %CAIRO_STATUS_SUCCESS.
270 * All assignments of an error status to scaled_font->status should happen
271 * through _cairo_scaled_font_set_error(). Note that due to the nature of
272 * the atomic operation, it is not safe to call this function on the nil
275 * The purpose of this function is to allow the user to set a
276 * breakpoint in _cairo_error() to generate a stack trace for when the
277 * user causes cairo to detect an error.
279 * Return value: the error status.
282 _cairo_scaled_font_set_error (cairo_scaled_font_t *scaled_font,
283 cairo_status_t status)
285 if (status == CAIRO_STATUS_SUCCESS)
288 /* Don't overwrite an existing error. This preserves the first
289 * error, which is the most significant. */
290 _cairo_status_set_error (&scaled_font->status, status);
292 return _cairo_error (status);
296 * cairo_scaled_font_get_type:
297 * @scaled_font: a #cairo_scaled_font_t
299 * This function returns the type of the backend used to create
300 * a scaled font. See #cairo_font_type_t for available types.
301 * However, this function never returns %CAIRO_FONT_TYPE_TOY.
303 * Return value: The type of @scaled_font.
308 cairo_scaled_font_get_type (cairo_scaled_font_t *scaled_font)
310 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
311 return CAIRO_FONT_TYPE_TOY;
313 return scaled_font->backend->type;
317 * cairo_scaled_font_status:
318 * @scaled_font: a #cairo_scaled_font_t
320 * Checks whether an error has previously occurred for this
323 * Return value: %CAIRO_STATUS_SUCCESS or another error such as
324 * %CAIRO_STATUS_NO_MEMORY.
329 cairo_scaled_font_status (cairo_scaled_font_t *scaled_font)
331 return scaled_font->status;
333 slim_hidden_def (cairo_scaled_font_status);
335 /* Here we keep a unique mapping from
336 * font_face/matrix/ctm/font_options => #cairo_scaled_font_t.
338 * Here are the things that we want to map:
340 * a) All otherwise referenced #cairo_scaled_font_t's
341 * b) Some number of not otherwise referenced #cairo_scaled_font_t's
343 * The implementation uses a hash table which covers (a)
344 * completely. Then, for (b) we have an array of otherwise
345 * unreferenced fonts (holdovers) which are expired in
346 * least-recently-used order.
348 * The cairo_scaled_font_create() code gets to treat this like a regular
349 * hash table. All of the magic for the little holdover cache is in
350 * cairo_scaled_font_reference() and cairo_scaled_font_destroy().
353 /* This defines the size of the holdover array ... that is, the number
354 * of scaled fonts we keep around even when not otherwise referenced
356 #define CAIRO_SCALED_FONT_MAX_HOLDOVERS 256
358 typedef struct _cairo_scaled_font_map {
359 cairo_scaled_font_t *mru_scaled_font;
360 cairo_hash_table_t *hash_table;
361 cairo_scaled_font_t *holdovers[CAIRO_SCALED_FONT_MAX_HOLDOVERS];
363 } cairo_scaled_font_map_t;
365 static cairo_scaled_font_map_t *cairo_scaled_font_map;
368 _cairo_scaled_font_keys_equal (const void *abstract_key_a, const void *abstract_key_b);
370 static cairo_scaled_font_map_t *
371 _cairo_scaled_font_map_lock (void)
373 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
375 if (cairo_scaled_font_map == NULL) {
376 cairo_scaled_font_map = malloc (sizeof (cairo_scaled_font_map_t));
377 if (unlikely (cairo_scaled_font_map == NULL))
378 goto CLEANUP_MUTEX_LOCK;
380 cairo_scaled_font_map->mru_scaled_font = NULL;
381 cairo_scaled_font_map->hash_table =
382 _cairo_hash_table_create (_cairo_scaled_font_keys_equal);
384 if (unlikely (cairo_scaled_font_map->hash_table == NULL))
385 goto CLEANUP_SCALED_FONT_MAP;
387 cairo_scaled_font_map->num_holdovers = 0;
390 return cairo_scaled_font_map;
392 CLEANUP_SCALED_FONT_MAP:
393 free (cairo_scaled_font_map);
394 cairo_scaled_font_map = NULL;
396 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
397 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
402 _cairo_scaled_font_map_unlock (void)
404 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
408 _cairo_scaled_font_map_destroy (void)
410 cairo_scaled_font_map_t *font_map;
411 cairo_scaled_font_t *scaled_font;
413 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
415 font_map = cairo_scaled_font_map;
416 if (unlikely (font_map == NULL)) {
417 goto CLEANUP_MUTEX_LOCK;
420 scaled_font = font_map->mru_scaled_font;
421 if (scaled_font != NULL) {
422 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
423 cairo_scaled_font_destroy (scaled_font);
424 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
427 /* remove scaled_fonts starting from the end so that font_map->holdovers
428 * is always in a consistent state when we release the mutex. */
429 while (font_map->num_holdovers) {
430 scaled_font = font_map->holdovers[font_map->num_holdovers-1];
431 assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
432 _cairo_hash_table_remove (font_map->hash_table,
433 &scaled_font->hash_entry);
435 font_map->num_holdovers--;
437 /* This releases the font_map lock to avoid the possibility of a
438 * recursive deadlock when the scaled font destroy closure gets
441 _cairo_scaled_font_fini (scaled_font);
446 _cairo_hash_table_destroy (font_map->hash_table);
448 free (cairo_scaled_font_map);
449 cairo_scaled_font_map = NULL;
452 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
455 _cairo_scaled_glyph_page_destroy (void *closure)
457 cairo_scaled_glyph_page_t *page = closure;
458 cairo_scaled_font_t *scaled_font;
461 assert (! cairo_list_is_empty (&page->link));
463 scaled_font = (cairo_scaled_font_t *) page->cache_entry.hash;
464 for (n = 0; n < page->num_glyphs; n++) {
465 _cairo_hash_table_remove (scaled_font->glyphs,
466 &page->glyphs[n].hash_entry);
467 _cairo_scaled_glyph_fini (scaled_font, &page->glyphs[n]);
470 cairo_list_del (&page->link);
475 /* If a scaled font wants to unlock the font map while still being
476 * created (needed for user-fonts), we need to take extra care not
477 * ending up with multiple identical scaled fonts being created.
479 * What we do is, we create a fake identical scaled font, and mark
480 * it as placeholder, lock its mutex, and insert that in the fontmap
481 * hash table. This makes other code trying to create an identical
482 * scaled font to just wait and retry.
484 * The reason we have to create a fake scaled font instead of just using
485 * scaled_font is for lifecycle management: we need to (or rather,
486 * other code needs to) reference the scaled_font in the hash table.
487 * We can't do that on the input scaled_font as it may be freed by
488 * font backend upon error.
492 _cairo_scaled_font_register_placeholder_and_unlock_font_map (cairo_scaled_font_t *scaled_font)
494 cairo_status_t status;
495 cairo_scaled_font_t *placeholder_scaled_font;
497 assert (CAIRO_MUTEX_IS_LOCKED (_cairo_scaled_font_map_mutex));
499 status = scaled_font->status;
500 if (unlikely (status))
503 placeholder_scaled_font = malloc (sizeof (cairo_scaled_font_t));
504 if (unlikely (placeholder_scaled_font == NULL))
505 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
507 /* full initialization is wasteful, but who cares... */
508 status = _cairo_scaled_font_init (placeholder_scaled_font,
509 scaled_font->font_face,
510 &scaled_font->font_matrix,
512 &scaled_font->options,
514 if (unlikely (status))
515 goto FREE_PLACEHOLDER;
517 placeholder_scaled_font->placeholder = TRUE;
519 placeholder_scaled_font->hash_entry.hash
520 = _cairo_scaled_font_compute_hash (placeholder_scaled_font);
521 status = _cairo_hash_table_insert (cairo_scaled_font_map->hash_table,
522 &placeholder_scaled_font->hash_entry);
523 if (unlikely (status))
524 goto FINI_PLACEHOLDER;
526 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
527 CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
529 return CAIRO_STATUS_SUCCESS;
532 _cairo_scaled_font_fini_internal (placeholder_scaled_font);
534 free (placeholder_scaled_font);
536 return _cairo_scaled_font_set_error (scaled_font, status);
540 _cairo_scaled_font_unregister_placeholder_and_lock_font_map (cairo_scaled_font_t *scaled_font)
542 cairo_scaled_font_t *placeholder_scaled_font;
544 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
546 /* temporary hash value to match the placeholder */
547 scaled_font->hash_entry.hash
548 = _cairo_scaled_font_compute_hash (scaled_font);
549 placeholder_scaled_font =
550 _cairo_hash_table_lookup (cairo_scaled_font_map->hash_table,
551 &scaled_font->hash_entry);
552 assert (placeholder_scaled_font != NULL);
553 assert (placeholder_scaled_font->placeholder);
554 assert (CAIRO_MUTEX_IS_LOCKED (placeholder_scaled_font->mutex));
556 _cairo_hash_table_remove (cairo_scaled_font_map->hash_table,
557 &placeholder_scaled_font->hash_entry);
559 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
561 CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
562 cairo_scaled_font_destroy (placeholder_scaled_font);
564 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
568 _cairo_scaled_font_placeholder_wait_for_creation_to_finish (cairo_scaled_font_t *placeholder_scaled_font)
570 /* reference the place holder so it doesn't go away */
571 cairo_scaled_font_reference (placeholder_scaled_font);
573 /* now unlock the fontmap mutex so creation has a chance to finish */
574 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
576 /* wait on placeholder mutex until we are awaken */
577 CAIRO_MUTEX_LOCK (placeholder_scaled_font->mutex);
579 /* ok, creation done. just clean up and back out */
580 CAIRO_MUTEX_UNLOCK (placeholder_scaled_font->mutex);
581 cairo_scaled_font_destroy (placeholder_scaled_font);
583 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
586 /* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/)
588 * Not necessarily better than a lot of other hashes, but should be OK, and
589 * well tested with binary data.
592 #define FNV_32_PRIME ((uint32_t)0x01000193)
593 #define FNV1_32_INIT ((uint32_t)0x811c9dc5)
596 _hash_matrix_fnv (const cairo_matrix_t *matrix,
599 const uint8_t *buffer = (const uint8_t *) matrix;
600 int len = sizeof (cairo_matrix_t);
602 hval *= FNV_32_PRIME;
610 _hash_mix_bits (uint32_t hash)
621 _cairo_scaled_font_compute_hash (cairo_scaled_font_t *scaled_font)
623 uint32_t hash = FNV1_32_INIT;
625 /* We do a bytewise hash on the font matrices */
626 hash = _hash_matrix_fnv (&scaled_font->font_matrix, hash);
627 hash = _hash_matrix_fnv (&scaled_font->ctm, hash);
628 hash = _hash_mix_bits (hash);
630 hash ^= (unsigned long) scaled_font->original_font_face;
631 hash ^= cairo_font_options_hash (&scaled_font->options);
633 /* final mixing of bits */
634 hash = _hash_mix_bits (hash);
635 assert (hash != ZOMBIE);
641 _cairo_scaled_font_init_key (cairo_scaled_font_t *scaled_font,
642 cairo_font_face_t *font_face,
643 const cairo_matrix_t *font_matrix,
644 const cairo_matrix_t *ctm,
645 const cairo_font_options_t *options)
647 scaled_font->status = CAIRO_STATUS_SUCCESS;
648 scaled_font->placeholder = FALSE;
649 scaled_font->font_face = font_face;
650 scaled_font->original_font_face = font_face;
651 scaled_font->font_matrix = *font_matrix;
652 scaled_font->ctm = *ctm;
653 /* ignore translation values in the ctm */
654 scaled_font->ctm.x0 = 0.;
655 scaled_font->ctm.y0 = 0.;
656 _cairo_font_options_init_copy (&scaled_font->options, options);
658 scaled_font->hash_entry.hash =
659 _cairo_scaled_font_compute_hash (scaled_font);
663 _cairo_scaled_font_keys_equal (const void *abstract_key_a,
664 const void *abstract_key_b)
666 const cairo_scaled_font_t *key_a = abstract_key_a;
667 const cairo_scaled_font_t *key_b = abstract_key_b;
669 return key_a->original_font_face == key_b->original_font_face &&
670 memcmp ((unsigned char *)(&key_a->font_matrix.xx),
671 (unsigned char *)(&key_b->font_matrix.xx),
672 sizeof(cairo_matrix_t)) == 0 &&
673 memcmp ((unsigned char *)(&key_a->ctm.xx),
674 (unsigned char *)(&key_b->ctm.xx),
675 sizeof(cairo_matrix_t)) == 0 &&
676 cairo_font_options_equal (&key_a->options, &key_b->options);
680 _cairo_scaled_font_matches (const cairo_scaled_font_t *scaled_font,
681 const cairo_font_face_t *font_face,
682 const cairo_matrix_t *font_matrix,
683 const cairo_matrix_t *ctm,
684 const cairo_font_options_t *options)
686 return scaled_font->original_font_face == font_face &&
687 memcmp ((unsigned char *)(&scaled_font->font_matrix.xx),
688 (unsigned char *)(&font_matrix->xx),
689 sizeof(cairo_matrix_t)) == 0 &&
690 memcmp ((unsigned char *)(&scaled_font->ctm.xx),
691 (unsigned char *)(&ctm->xx),
692 sizeof(cairo_matrix_t)) == 0 &&
693 cairo_font_options_equal (&scaled_font->options, options);
697 * Basic #cairo_scaled_font_t object management
701 _cairo_scaled_font_init (cairo_scaled_font_t *scaled_font,
702 cairo_font_face_t *font_face,
703 const cairo_matrix_t *font_matrix,
704 const cairo_matrix_t *ctm,
705 const cairo_font_options_t *options,
706 const cairo_scaled_font_backend_t *backend)
708 cairo_status_t status;
710 status = cairo_font_options_status ((cairo_font_options_t *) options);
711 if (unlikely (status))
714 scaled_font->status = CAIRO_STATUS_SUCCESS;
715 scaled_font->placeholder = FALSE;
716 scaled_font->font_face = font_face;
717 scaled_font->original_font_face = font_face;
718 scaled_font->font_matrix = *font_matrix;
719 scaled_font->ctm = *ctm;
720 /* ignore translation values in the ctm */
721 scaled_font->ctm.x0 = 0.;
722 scaled_font->ctm.y0 = 0.;
723 _cairo_font_options_init_copy (&scaled_font->options, options);
725 cairo_matrix_multiply (&scaled_font->scale,
726 &scaled_font->font_matrix,
729 scaled_font->max_scale = MAX (fabs (scaled_font->scale.xx) + fabs (scaled_font->scale.xy),
730 fabs (scaled_font->scale.yx) + fabs (scaled_font->scale.yy));
731 scaled_font->scale_inverse = scaled_font->scale;
732 status = cairo_matrix_invert (&scaled_font->scale_inverse);
733 if (unlikely (status)) {
734 /* If the font scale matrix is rank 0, just using an all-zero inverse matrix
735 * makes everything work correctly. This make font size 0 work without
736 * producing an error.
738 * FIXME: If the scale is rank 1, we still go into error mode. But then
739 * again, that's what we do everywhere in cairo.
741 * Also, the check for == 0. below may be too harsh...
743 if (_cairo_matrix_is_scale_0 (&scaled_font->scale)) {
744 cairo_matrix_init (&scaled_font->scale_inverse,
746 -scaled_font->scale.x0,
747 -scaled_font->scale.y0);
752 scaled_font->glyphs = _cairo_hash_table_create (NULL);
753 if (unlikely (scaled_font->glyphs == NULL))
754 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
756 cairo_list_init (&scaled_font->glyph_pages);
757 scaled_font->cache_frozen = FALSE;
758 scaled_font->global_cache_frozen = FALSE;
760 scaled_font->holdover = FALSE;
761 scaled_font->finished = FALSE;
763 CAIRO_REFERENCE_COUNT_INIT (&scaled_font->ref_count, 1);
765 _cairo_user_data_array_init (&scaled_font->user_data);
767 cairo_font_face_reference (font_face);
768 scaled_font->original_font_face = NULL;
770 CAIRO_MUTEX_INIT (scaled_font->mutex);
772 cairo_list_init (&scaled_font->dev_privates);
774 scaled_font->backend = backend;
775 cairo_list_init (&scaled_font->link);
777 return CAIRO_STATUS_SUCCESS;
781 _cairo_scaled_font_freeze_cache (cairo_scaled_font_t *scaled_font)
783 /* ensure we do not modify an error object */
784 assert (scaled_font->status == CAIRO_STATUS_SUCCESS);
786 CAIRO_MUTEX_LOCK (scaled_font->mutex);
787 scaled_font->cache_frozen = TRUE;
791 _cairo_scaled_font_thaw_cache (cairo_scaled_font_t *scaled_font)
793 scaled_font->cache_frozen = FALSE;
795 if (scaled_font->global_cache_frozen) {
796 CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
797 _cairo_cache_thaw (&cairo_scaled_glyph_page_cache);
798 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
800 scaled_font->global_cache_frozen = FALSE;
803 CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
807 _cairo_scaled_font_reset_cache (cairo_scaled_font_t *scaled_font)
809 assert (! scaled_font->cache_frozen);
811 CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
812 while (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
813 _cairo_cache_remove (&cairo_scaled_glyph_page_cache,
814 &cairo_list_first_entry (&scaled_font->glyph_pages,
815 cairo_scaled_glyph_page_t,
818 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
822 _cairo_scaled_font_set_metrics (cairo_scaled_font_t *scaled_font,
823 cairo_font_extents_t *fs_metrics)
825 cairo_status_t status;
826 double font_scale_x, font_scale_y;
828 scaled_font->fs_extents = *fs_metrics;
830 status = _cairo_matrix_compute_basis_scale_factors (&scaled_font->font_matrix,
831 &font_scale_x, &font_scale_y,
833 if (unlikely (status))
837 * The font responded in unscaled units, scale by the font
838 * matrix scale factors to get to user space
841 scaled_font->extents.ascent = fs_metrics->ascent * font_scale_y;
842 scaled_font->extents.descent = fs_metrics->descent * font_scale_y;
843 scaled_font->extents.height = fs_metrics->height * font_scale_y;
844 scaled_font->extents.max_x_advance = fs_metrics->max_x_advance * font_scale_x;
845 scaled_font->extents.max_y_advance = fs_metrics->max_y_advance * font_scale_y;
847 return CAIRO_STATUS_SUCCESS;
851 _cairo_scaled_font_fini_internal (cairo_scaled_font_t *scaled_font)
853 scaled_font->finished = TRUE;
855 _cairo_scaled_font_reset_cache (scaled_font);
856 _cairo_hash_table_destroy (scaled_font->glyphs);
858 cairo_font_face_destroy (scaled_font->font_face);
859 cairo_font_face_destroy (scaled_font->original_font_face);
861 CAIRO_MUTEX_FINI (scaled_font->mutex);
863 while (! cairo_list_is_empty (&scaled_font->dev_privates)) {
864 cairo_scaled_font_private_t *private =
865 cairo_list_first_entry (&scaled_font->dev_privates,
866 cairo_scaled_font_private_t,
868 private->destroy (private, scaled_font);
871 if (scaled_font->backend != NULL && scaled_font->backend->fini != NULL)
872 scaled_font->backend->fini (scaled_font);
874 _cairo_user_data_array_fini (&scaled_font->user_data);
878 _cairo_scaled_font_fini (cairo_scaled_font_t *scaled_font)
880 /* Release the lock to avoid the possibility of a recursive
881 * deadlock when the scaled font destroy closure gets called. */
882 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_map_mutex);
883 _cairo_scaled_font_fini_internal (scaled_font);
884 CAIRO_MUTEX_LOCK (_cairo_scaled_font_map_mutex);
888 _cairo_scaled_font_attach_private (cairo_scaled_font_t *scaled_font,
889 cairo_scaled_font_private_t *private,
891 void (*destroy) (cairo_scaled_font_private_t *,
892 cairo_scaled_font_t *))
895 private->destroy = destroy;
896 cairo_list_add (&private->link, &scaled_font->dev_privates);
899 cairo_scaled_font_private_t *
900 _cairo_scaled_font_find_private (cairo_scaled_font_t *scaled_font,
903 cairo_scaled_font_private_t *priv;
905 cairo_list_foreach_entry (priv, cairo_scaled_font_private_t,
906 &scaled_font->dev_privates, link)
908 if (priv->key == key) {
909 if (priv->link.prev != &scaled_font->dev_privates)
910 cairo_list_move (&priv->link, &scaled_font->dev_privates);
919 _cairo_scaled_glyph_attach_private (cairo_scaled_glyph_t *scaled_glyph,
920 cairo_scaled_glyph_private_t *private,
922 void (*destroy) (cairo_scaled_glyph_private_t *,
923 cairo_scaled_glyph_t *,
924 cairo_scaled_font_t *))
927 private->destroy = destroy;
928 cairo_list_add (&private->link, &scaled_glyph->dev_privates);
931 cairo_scaled_glyph_private_t *
932 _cairo_scaled_glyph_find_private (cairo_scaled_glyph_t *scaled_glyph,
935 cairo_scaled_glyph_private_t *priv;
937 cairo_list_foreach_entry (priv, cairo_scaled_glyph_private_t,
938 &scaled_glyph->dev_privates, link)
940 if (priv->key == key) {
941 if (priv->link.prev != &scaled_glyph->dev_privates)
942 cairo_list_move (&priv->link, &scaled_glyph->dev_privates);
951 * cairo_scaled_font_create:
952 * @font_face: a #cairo_font_face_t
953 * @font_matrix: font space to user space transformation matrix for the
954 * font. In the simplest case of a N point font, this matrix is
955 * just a scale by N, but it can also be used to shear the font
956 * or stretch it unequally along the two axes. See
957 * cairo_set_font_matrix().
958 * @ctm: user to device transformation matrix with which the font will
960 * @options: options to use when getting metrics for the font and
963 * Creates a #cairo_scaled_font_t object from a font face and matrices that
964 * describe the size of the font and the environment in which it will
967 * Return value: a newly created #cairo_scaled_font_t. Destroy with
968 * cairo_scaled_font_destroy()
972 cairo_scaled_font_t *
973 cairo_scaled_font_create (cairo_font_face_t *font_face,
974 const cairo_matrix_t *font_matrix,
975 const cairo_matrix_t *ctm,
976 const cairo_font_options_t *options)
978 cairo_status_t status;
979 cairo_scaled_font_map_t *font_map;
980 cairo_font_face_t *original_font_face = font_face;
981 cairo_scaled_font_t key, *old = NULL, *scaled_font = NULL, *dead = NULL;
984 status = font_face->status;
985 if (unlikely (status))
986 return _cairo_scaled_font_create_in_error (status);
988 det = _cairo_matrix_compute_determinant (font_matrix);
989 if (! ISFINITE (det))
990 return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
992 det = _cairo_matrix_compute_determinant (ctm);
993 if (! ISFINITE (det))
994 return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_MATRIX));
996 status = cairo_font_options_status ((cairo_font_options_t *) options);
997 if (unlikely (status))
998 return _cairo_scaled_font_create_in_error (status);
1000 /* Note that degenerate ctm or font_matrix *are* allowed.
1001 * We want to support a font size of 0. */
1003 font_map = _cairo_scaled_font_map_lock ();
1004 if (unlikely (font_map == NULL))
1005 return _cairo_scaled_font_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1007 scaled_font = font_map->mru_scaled_font;
1008 if (scaled_font != NULL &&
1009 _cairo_scaled_font_matches (scaled_font,
1010 font_face, font_matrix, ctm, options))
1012 assert (scaled_font->hash_entry.hash != ZOMBIE);
1013 assert (! scaled_font->placeholder);
1015 if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
1016 /* We increment the reference count manually here, (rather
1017 * than calling into cairo_scaled_font_reference), since we
1018 * must modify the reference count while our lock is still
1020 _cairo_reference_count_inc (&scaled_font->ref_count);
1021 _cairo_scaled_font_map_unlock ();
1025 /* the font has been put into an error status - abandon the cache */
1026 _cairo_hash_table_remove (font_map->hash_table,
1027 &scaled_font->hash_entry);
1028 scaled_font->hash_entry.hash = ZOMBIE;
1030 font_map->mru_scaled_font = NULL;
1033 _cairo_scaled_font_init_key (&key, font_face, font_matrix, ctm, options);
1035 while ((scaled_font = _cairo_hash_table_lookup (font_map->hash_table,
1038 if (! scaled_font->placeholder)
1041 /* If the scaled font is being created (happens for user-font),
1042 * just wait until it's done, then retry */
1043 _cairo_scaled_font_placeholder_wait_for_creation_to_finish (scaled_font);
1046 if (scaled_font != NULL) {
1047 /* If the original reference count is 0, then this font must have
1048 * been found in font_map->holdovers, (which means this caching is
1049 * actually working). So now we remove it from the holdovers
1050 * array, unless we caught the font in the middle of destruction.
1052 if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
1053 if (scaled_font->holdover) {
1056 for (i = 0; i < font_map->num_holdovers; i++) {
1057 if (font_map->holdovers[i] == scaled_font) {
1058 font_map->num_holdovers--;
1059 memmove (&font_map->holdovers[i],
1060 &font_map->holdovers[i+1],
1061 (font_map->num_holdovers - i) * sizeof (cairo_scaled_font_t*));
1066 scaled_font->holdover = FALSE;
1069 /* reset any error status */
1070 scaled_font->status = CAIRO_STATUS_SUCCESS;
1073 if (likely (scaled_font->status == CAIRO_STATUS_SUCCESS)) {
1074 /* We increment the reference count manually here, (rather
1075 * than calling into cairo_scaled_font_reference), since we
1076 * must modify the reference count while our lock is still
1079 old = font_map->mru_scaled_font;
1080 font_map->mru_scaled_font = scaled_font;
1081 /* increment reference count for the mru cache */
1082 _cairo_reference_count_inc (&scaled_font->ref_count);
1083 /* and increment for the returned reference */
1084 _cairo_reference_count_inc (&scaled_font->ref_count);
1085 _cairo_scaled_font_map_unlock ();
1087 cairo_scaled_font_destroy (old);
1088 if (font_face != original_font_face)
1089 cairo_font_face_destroy (font_face);
1094 /* the font has been put into an error status - abandon the cache */
1095 _cairo_hash_table_remove (font_map->hash_table,
1096 &scaled_font->hash_entry);
1097 scaled_font->hash_entry.hash = ZOMBIE;
1101 /* Otherwise create it and insert it into the hash table. */
1102 if (font_face->backend->get_implementation != NULL) {
1103 font_face = font_face->backend->get_implementation (font_face,
1107 if (unlikely (font_face->status)) {
1108 _cairo_scaled_font_map_unlock ();
1109 return _cairo_scaled_font_create_in_error (font_face->status);
1113 status = font_face->backend->scaled_font_create (font_face, font_matrix,
1114 ctm, options, &scaled_font);
1115 /* Did we leave the backend in an error state? */
1116 if (unlikely (status)) {
1117 _cairo_scaled_font_map_unlock ();
1118 if (font_face != original_font_face)
1119 cairo_font_face_destroy (font_face);
1122 cairo_scaled_font_destroy (dead);
1124 status = _cairo_font_face_set_error (font_face, status);
1125 return _cairo_scaled_font_create_in_error (status);
1127 /* Or did we encounter an error whilst constructing the scaled font? */
1128 if (unlikely (scaled_font->status)) {
1129 _cairo_scaled_font_map_unlock ();
1130 if (font_face != original_font_face)
1131 cairo_font_face_destroy (font_face);
1134 cairo_scaled_font_destroy (dead);
1139 /* Our caching above is defeated if the backend switches fonts on us -
1140 * e.g. old incarnations of toy-font-face and lazily resolved
1143 assert (scaled_font->font_face == font_face);
1145 scaled_font->original_font_face =
1146 cairo_font_face_reference (original_font_face);
1148 scaled_font->hash_entry.hash = _cairo_scaled_font_compute_hash(scaled_font);
1150 status = _cairo_hash_table_insert (font_map->hash_table,
1151 &scaled_font->hash_entry);
1152 if (likely (status == CAIRO_STATUS_SUCCESS)) {
1153 old = font_map->mru_scaled_font;
1154 font_map->mru_scaled_font = scaled_font;
1155 _cairo_reference_count_inc (&scaled_font->ref_count);
1158 _cairo_scaled_font_map_unlock ();
1160 cairo_scaled_font_destroy (old);
1161 if (font_face != original_font_face)
1162 cairo_font_face_destroy (font_face);
1165 cairo_scaled_font_destroy (dead);
1167 if (unlikely (status)) {
1168 /* We can't call _cairo_scaled_font_destroy here since it expects
1169 * that the font has already been successfully inserted into the
1171 _cairo_scaled_font_fini_internal (scaled_font);
1173 return _cairo_scaled_font_create_in_error (status);
1178 slim_hidden_def (cairo_scaled_font_create);
1180 static cairo_scaled_font_t *_cairo_scaled_font_nil_objects[CAIRO_STATUS_LAST_STATUS + 1];
1182 /* XXX This should disappear in favour of a common pool of error objects. */
1183 cairo_scaled_font_t *
1184 _cairo_scaled_font_create_in_error (cairo_status_t status)
1186 cairo_scaled_font_t *scaled_font;
1188 assert (status != CAIRO_STATUS_SUCCESS);
1190 if (status == CAIRO_STATUS_NO_MEMORY)
1191 return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
1193 CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
1194 scaled_font = _cairo_scaled_font_nil_objects[status];
1195 if (unlikely (scaled_font == NULL)) {
1196 scaled_font = malloc (sizeof (cairo_scaled_font_t));
1197 if (unlikely (scaled_font == NULL)) {
1198 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1199 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
1200 return (cairo_scaled_font_t *) &_cairo_scaled_font_nil;
1203 *scaled_font = _cairo_scaled_font_nil;
1204 scaled_font->status = status;
1205 _cairo_scaled_font_nil_objects[status] = scaled_font;
1207 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1213 _cairo_scaled_font_reset_static_data (void)
1217 CAIRO_MUTEX_LOCK (_cairo_scaled_font_error_mutex);
1218 for (status = CAIRO_STATUS_SUCCESS;
1219 status <= CAIRO_STATUS_LAST_STATUS;
1222 free (_cairo_scaled_font_nil_objects[status]);
1223 _cairo_scaled_font_nil_objects[status] = NULL;
1225 CAIRO_MUTEX_UNLOCK (_cairo_scaled_font_error_mutex);
1227 CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
1228 if (cairo_scaled_glyph_page_cache.hash_table != NULL) {
1229 _cairo_cache_fini (&cairo_scaled_glyph_page_cache);
1230 cairo_scaled_glyph_page_cache.hash_table = NULL;
1232 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
1236 * cairo_scaled_font_reference:
1237 * @scaled_font: a #cairo_scaled_font_t, (may be %NULL in which case
1238 * this function does nothing)
1240 * Increases the reference count on @scaled_font by one. This prevents
1241 * @scaled_font from being destroyed until a matching call to
1242 * cairo_scaled_font_destroy() is made.
1244 * The number of references to a #cairo_scaled_font_t can be get using
1245 * cairo_scaled_font_get_reference_count().
1247 * Returns: the referenced #cairo_scaled_font_t
1251 cairo_scaled_font_t *
1252 cairo_scaled_font_reference (cairo_scaled_font_t *scaled_font)
1254 if (scaled_font == NULL ||
1255 CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1258 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
1260 _cairo_reference_count_inc (&scaled_font->ref_count);
1264 slim_hidden_def (cairo_scaled_font_reference);
1267 * cairo_scaled_font_destroy:
1268 * @scaled_font: a #cairo_scaled_font_t
1270 * Decreases the reference count on @font by one. If the result
1271 * is zero, then @font and all associated resources are freed.
1272 * See cairo_scaled_font_reference().
1277 cairo_scaled_font_destroy (cairo_scaled_font_t *scaled_font)
1279 cairo_scaled_font_t *lru = NULL;
1280 cairo_scaled_font_map_t *font_map;
1282 assert (CAIRO_MUTEX_IS_UNLOCKED (_cairo_scaled_font_map_mutex));
1284 if (scaled_font == NULL ||
1285 CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1288 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count));
1290 if (! _cairo_reference_count_dec_and_test (&scaled_font->ref_count))
1293 font_map = _cairo_scaled_font_map_lock ();
1294 assert (font_map != NULL);
1296 /* Another thread may have resurrected the font whilst we waited */
1297 if (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&scaled_font->ref_count)) {
1298 if (! scaled_font->placeholder &&
1299 scaled_font->hash_entry.hash != ZOMBIE)
1301 /* Another thread may have already inserted us into the holdovers */
1302 if (scaled_font->holdover)
1305 /* Rather than immediately destroying this object, we put it into
1306 * the font_map->holdovers array in case it will get used again
1307 * soon (and is why we must hold the lock over the atomic op on
1308 * the reference count). To make room for it, we do actually
1309 * destroy the least-recently-used holdover.
1312 if (font_map->num_holdovers == CAIRO_SCALED_FONT_MAX_HOLDOVERS) {
1313 lru = font_map->holdovers[0];
1314 assert (! CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&lru->ref_count));
1316 _cairo_hash_table_remove (font_map->hash_table,
1319 font_map->num_holdovers--;
1320 memmove (&font_map->holdovers[0],
1321 &font_map->holdovers[1],
1322 font_map->num_holdovers * sizeof (cairo_scaled_font_t*));
1325 font_map->holdovers[font_map->num_holdovers++] = scaled_font;
1326 scaled_font->holdover = TRUE;
1332 _cairo_scaled_font_map_unlock ();
1334 /* If we pulled an item from the holdovers array, (while the font
1335 * map lock was held, of course), then there is no way that anyone
1336 * else could have acquired a reference to it. So we can now
1337 * safely call fini on it without any lock held. This is desirable
1338 * as we never want to call into any backend function with a lock
1341 _cairo_scaled_font_fini_internal (lru);
1345 slim_hidden_def (cairo_scaled_font_destroy);
1348 * cairo_scaled_font_get_reference_count:
1349 * @scaled_font: a #cairo_scaled_font_t
1351 * Returns the current reference count of @scaled_font.
1353 * Return value: the current reference count of @scaled_font. If the
1354 * object is a nil object, 0 will be returned.
1359 cairo_scaled_font_get_reference_count (cairo_scaled_font_t *scaled_font)
1361 if (scaled_font == NULL ||
1362 CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1365 return CAIRO_REFERENCE_COUNT_GET_VALUE (&scaled_font->ref_count);
1369 * cairo_scaled_font_get_user_data:
1370 * @scaled_font: a #cairo_scaled_font_t
1371 * @key: the address of the #cairo_user_data_key_t the user data was
1374 * Return user data previously attached to @scaled_font using the
1375 * specified key. If no user data has been attached with the given
1376 * key this function returns %NULL.
1378 * Return value: the user data previously attached or %NULL.
1383 cairo_scaled_font_get_user_data (cairo_scaled_font_t *scaled_font,
1384 const cairo_user_data_key_t *key)
1386 return _cairo_user_data_array_get_data (&scaled_font->user_data,
1389 slim_hidden_def (cairo_scaled_font_get_user_data);
1392 * cairo_scaled_font_set_user_data:
1393 * @scaled_font: a #cairo_scaled_font_t
1394 * @key: the address of a #cairo_user_data_key_t to attach the user data to
1395 * @user_data: the user data to attach to the #cairo_scaled_font_t
1396 * @destroy: a #cairo_destroy_func_t which will be called when the
1397 * #cairo_t is destroyed or when new user data is attached using the
1400 * Attach user data to @scaled_font. To remove user data from a surface,
1401 * call this function with the key that was used to set it and %NULL
1404 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
1405 * slot could not be allocated for the user data.
1410 cairo_scaled_font_set_user_data (cairo_scaled_font_t *scaled_font,
1411 const cairo_user_data_key_t *key,
1413 cairo_destroy_func_t destroy)
1415 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&scaled_font->ref_count))
1416 return scaled_font->status;
1418 return _cairo_user_data_array_set_data (&scaled_font->user_data,
1419 key, user_data, destroy);
1421 slim_hidden_def (cairo_scaled_font_set_user_data);
1423 /* Public font API follows. */
1426 * cairo_scaled_font_extents:
1427 * @scaled_font: a #cairo_scaled_font_t
1428 * @extents: a #cairo_font_extents_t which to store the retrieved extents.
1430 * Gets the metrics for a #cairo_scaled_font_t.
1435 cairo_scaled_font_extents (cairo_scaled_font_t *scaled_font,
1436 cairo_font_extents_t *extents)
1438 if (scaled_font->status) {
1439 extents->ascent = 0.0;
1440 extents->descent = 0.0;
1441 extents->height = 0.0;
1442 extents->max_x_advance = 0.0;
1443 extents->max_y_advance = 0.0;
1447 *extents = scaled_font->extents;
1449 slim_hidden_def (cairo_scaled_font_extents);
1452 * cairo_scaled_font_text_extents:
1453 * @scaled_font: a #cairo_scaled_font_t
1454 * @utf8: a NUL-terminated string of text, encoded in UTF-8
1455 * @extents: a #cairo_text_extents_t which to store the retrieved extents.
1457 * Gets the extents for a string of text. The extents describe a
1458 * user-space rectangle that encloses the "inked" portion of the text
1459 * drawn at the origin (0,0) (as it would be drawn by cairo_show_text()
1460 * if the cairo graphics state were set to the same font_face,
1461 * font_matrix, ctm, and font_options as @scaled_font). Additionally,
1462 * the x_advance and y_advance values indicate the amount by which the
1463 * current point would be advanced by cairo_show_text().
1465 * Note that whitespace characters do not directly contribute to the
1466 * size of the rectangle (extents.width and extents.height). They do
1467 * contribute indirectly by changing the position of non-whitespace
1468 * characters. In particular, trailing whitespace characters are
1469 * likely to not affect the size of the rectangle, though they will
1470 * affect the x_advance and y_advance values.
1475 cairo_scaled_font_text_extents (cairo_scaled_font_t *scaled_font,
1477 cairo_text_extents_t *extents)
1479 cairo_status_t status;
1480 cairo_glyph_t *glyphs = NULL;
1483 if (scaled_font->status)
1489 status = cairo_scaled_font_text_to_glyphs (scaled_font, 0., 0.,
1491 &glyphs, &num_glyphs,
1494 if (unlikely (status)) {
1495 status = _cairo_scaled_font_set_error (scaled_font, status);
1499 cairo_scaled_font_glyph_extents (scaled_font, glyphs, num_glyphs, extents);
1505 extents->x_bearing = 0.0;
1506 extents->y_bearing = 0.0;
1507 extents->width = 0.0;
1508 extents->height = 0.0;
1509 extents->x_advance = 0.0;
1510 extents->y_advance = 0.0;
1514 * cairo_scaled_font_glyph_extents:
1515 * @scaled_font: a #cairo_scaled_font_t
1516 * @glyphs: an array of glyph IDs with X and Y offsets.
1517 * @num_glyphs: the number of glyphs in the @glyphs array
1518 * @extents: a #cairo_text_extents_t which to store the retrieved extents.
1520 * Gets the extents for an array of glyphs. The extents describe a
1521 * user-space rectangle that encloses the "inked" portion of the
1522 * glyphs, (as they would be drawn by cairo_show_glyphs() if the cairo
1523 * graphics state were set to the same font_face, font_matrix, ctm,
1524 * and font_options as @scaled_font). Additionally, the x_advance and
1525 * y_advance values indicate the amount by which the current point
1526 * would be advanced by cairo_show_glyphs().
1528 * Note that whitespace glyphs do not contribute to the size of the
1529 * rectangle (extents.width and extents.height).
1534 cairo_scaled_font_glyph_extents (cairo_scaled_font_t *scaled_font,
1535 const cairo_glyph_t *glyphs,
1537 cairo_text_extents_t *extents)
1539 cairo_status_t status;
1541 double min_x = 0.0, min_y = 0.0, max_x = 0.0, max_y = 0.0;
1542 cairo_bool_t visible = FALSE;
1543 cairo_scaled_glyph_t *scaled_glyph = NULL;
1545 extents->x_bearing = 0.0;
1546 extents->y_bearing = 0.0;
1547 extents->width = 0.0;
1548 extents->height = 0.0;
1549 extents->x_advance = 0.0;
1550 extents->y_advance = 0.0;
1552 if (unlikely (scaled_font->status))
1555 if (num_glyphs == 0)
1558 if (unlikely (num_glyphs < 0)) {
1559 _cairo_error_throw (CAIRO_STATUS_NEGATIVE_COUNT);
1560 /* XXX Can't propagate error */
1564 if (unlikely (glyphs == NULL)) {
1565 _cairo_error_throw (CAIRO_STATUS_NULL_POINTER);
1566 /* XXX Can't propagate error */
1570 _cairo_scaled_font_freeze_cache (scaled_font);
1572 for (i = 0; i < num_glyphs; i++) {
1573 double left, top, right, bottom;
1575 status = _cairo_scaled_glyph_lookup (scaled_font,
1577 CAIRO_SCALED_GLYPH_INFO_METRICS,
1579 if (unlikely (status)) {
1580 status = _cairo_scaled_font_set_error (scaled_font, status);
1584 /* "Ink" extents should skip "invisible" glyphs */
1585 if (scaled_glyph->metrics.width == 0 || scaled_glyph->metrics.height == 0)
1588 left = scaled_glyph->metrics.x_bearing + glyphs[i].x;
1589 right = left + scaled_glyph->metrics.width;
1590 top = scaled_glyph->metrics.y_bearing + glyphs[i].y;
1591 bottom = top + scaled_glyph->metrics.height;
1600 if (left < min_x) min_x = left;
1601 if (right > max_x) max_x = right;
1602 if (top < min_y) min_y = top;
1603 if (bottom > max_y) max_y = bottom;
1608 extents->x_bearing = min_x - glyphs[0].x;
1609 extents->y_bearing = min_y - glyphs[0].y;
1610 extents->width = max_x - min_x;
1611 extents->height = max_y - min_y;
1613 extents->x_bearing = 0.0;
1614 extents->y_bearing = 0.0;
1615 extents->width = 0.0;
1616 extents->height = 0.0;
1620 double x0, y0, x1, y1;
1625 /* scaled_glyph contains the glyph for num_glyphs - 1 already. */
1626 x1 = glyphs[num_glyphs - 1].x + scaled_glyph->metrics.x_advance;
1627 y1 = glyphs[num_glyphs - 1].y + scaled_glyph->metrics.y_advance;
1629 extents->x_advance = x1 - x0;
1630 extents->y_advance = y1 - y0;
1632 extents->x_advance = 0.0;
1633 extents->y_advance = 0.0;
1637 _cairo_scaled_font_thaw_cache (scaled_font);
1641 extents->x_bearing = 0.0;
1642 extents->y_bearing = 0.0;
1643 extents->width = 0.0;
1644 extents->height = 0.0;
1645 extents->x_advance = 0.0;
1646 extents->y_advance = 0.0;
1648 slim_hidden_def (cairo_scaled_font_glyph_extents);
1650 #define GLYPH_LUT_SIZE 64
1651 static cairo_status_t
1652 cairo_scaled_font_text_to_glyphs_internal_cached (cairo_scaled_font_t *scaled_font,
1656 cairo_glyph_t *glyphs,
1657 cairo_text_cluster_t **clusters,
1660 struct glyph_lut_elt {
1661 unsigned long index;
1664 } glyph_lut[GLYPH_LUT_SIZE];
1665 uint32_t glyph_lut_unicode[GLYPH_LUT_SIZE];
1666 cairo_status_t status;
1670 for (i = 0; i < GLYPH_LUT_SIZE; i++)
1671 glyph_lut_unicode[i] = ~0U;
1674 for (i = 0; i < num_chars; i++) {
1677 cairo_scaled_glyph_t *scaled_glyph;
1678 struct glyph_lut_elt *glyph_slot;
1680 num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
1686 idx = unicode % ARRAY_LENGTH (glyph_lut);
1687 glyph_slot = &glyph_lut[idx];
1688 if (glyph_lut_unicode[idx] == unicode) {
1689 glyphs[i].index = glyph_slot->index;
1690 x += glyph_slot->x_advance;
1691 y += glyph_slot->y_advance;
1695 g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
1696 status = _cairo_scaled_glyph_lookup (scaled_font,
1698 CAIRO_SCALED_GLYPH_INFO_METRICS,
1700 if (unlikely (status))
1703 x += scaled_glyph->metrics.x_advance;
1704 y += scaled_glyph->metrics.y_advance;
1706 glyph_lut_unicode[idx] = unicode;
1707 glyph_slot->index = g;
1708 glyph_slot->x_advance = scaled_glyph->metrics.x_advance;
1709 glyph_slot->y_advance = scaled_glyph->metrics.y_advance;
1711 glyphs[i].index = g;
1715 (*clusters)[i].num_bytes = num_bytes;
1716 (*clusters)[i].num_glyphs = 1;
1720 return CAIRO_STATUS_SUCCESS;
1723 static cairo_status_t
1724 cairo_scaled_font_text_to_glyphs_internal_uncached (cairo_scaled_font_t *scaled_font,
1728 cairo_glyph_t *glyphs,
1729 cairo_text_cluster_t **clusters,
1736 for (i = 0; i < num_chars; i++) {
1740 cairo_scaled_glyph_t *scaled_glyph;
1741 cairo_status_t status;
1743 num_bytes = _cairo_utf8_get_char_validated (p, &unicode);
1749 g = scaled_font->backend->ucs4_to_index (scaled_font, unicode);
1752 * No advance needed for a single character string. So, let's speed up
1753 * one-character strings by skipping glyph lookup.
1755 if (num_chars > 1) {
1756 status = _cairo_scaled_glyph_lookup (scaled_font,
1758 CAIRO_SCALED_GLYPH_INFO_METRICS,
1760 if (unlikely (status))
1763 x += scaled_glyph->metrics.x_advance;
1764 y += scaled_glyph->metrics.y_advance;
1767 glyphs[i].index = g;
1770 (*clusters)[i].num_bytes = num_bytes;
1771 (*clusters)[i].num_glyphs = 1;
1775 return CAIRO_STATUS_SUCCESS;
1779 * cairo_scaled_font_text_to_glyphs:
1780 * @x: X position to place first glyph
1781 * @y: Y position to place first glyph
1782 * @scaled_font: a #cairo_scaled_font_t
1783 * @utf8: a string of text encoded in UTF-8
1784 * @utf8_len: length of @utf8 in bytes, or -1 if it is NUL-terminated
1785 * @glyphs: pointer to array of glyphs to fill
1786 * @num_glyphs: pointer to number of glyphs
1787 * @clusters: pointer to array of cluster mapping information to fill, or %NULL
1788 * @num_clusters: pointer to number of clusters, or %NULL
1789 * @cluster_flags: pointer to location to store cluster flags corresponding to the
1790 * output @clusters, or %NULL
1792 * Converts UTF-8 text to an array of glyphs, optionally with cluster
1793 * mapping, that can be used to render later using @scaled_font.
1795 * If @glyphs initially points to a non-%NULL value, that array is used
1796 * as a glyph buffer, and @num_glyphs should point to the number of glyph
1797 * entries available there. If the provided glyph array is too short for
1798 * the conversion, a new glyph array is allocated using cairo_glyph_allocate()
1799 * and placed in @glyphs. Upon return, @num_glyphs always contains the
1800 * number of generated glyphs. If the value @glyphs points to has changed
1801 * after the call, the user is responsible for freeing the allocated glyph
1802 * array using cairo_glyph_free(). This may happen even if the provided
1803 * array was large enough.
1805 * If @clusters is not %NULL, @num_clusters and @cluster_flags should not be %NULL,
1806 * and cluster mapping will be computed.
1807 * The semantics of how cluster array allocation works is similar to the glyph
1809 * if @clusters initially points to a non-%NULL value, that array is used
1810 * as a cluster buffer, and @num_clusters should point to the number of cluster
1811 * entries available there. If the provided cluster array is too short for
1812 * the conversion, a new cluster array is allocated using cairo_text_cluster_allocate()
1813 * and placed in @clusters. Upon return, @num_clusters always contains the
1814 * number of generated clusters. If the value @clusters points at has changed
1815 * after the call, the user is responsible for freeing the allocated cluster
1816 * array using cairo_text_cluster_free(). This may happen even if the provided
1817 * array was large enough.
1819 * In the simplest case, @glyphs and @clusters can point to %NULL initially
1820 * and a suitable array will be allocated. In code:
1821 * <informalexample><programlisting>
1822 * cairo_status_t status;
1824 * cairo_glyph_t *glyphs = NULL;
1826 * cairo_text_cluster_t *clusters = NULL;
1828 * cairo_text_cluster_flags_t cluster_flags;
1830 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1833 * &glyphs, &num_glyphs,
1834 * &clusters, &num_clusters, &cluster_flags);
1836 * if (status == CAIRO_STATUS_SUCCESS) {
1837 * cairo_show_text_glyphs (cr,
1839 * glyphs, num_glyphs,
1840 * clusters, num_clusters, cluster_flags);
1842 * cairo_glyph_free (glyphs);
1843 * cairo_text_cluster_free (clusters);
1845 * </programlisting></informalexample>
1847 * If no cluster mapping is needed:
1848 * <informalexample><programlisting>
1849 * cairo_status_t status;
1851 * cairo_glyph_t *glyphs = NULL;
1854 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1857 * &glyphs, &num_glyphs,
1861 * if (status == CAIRO_STATUS_SUCCESS) {
1862 * cairo_show_glyphs (cr, glyphs, num_glyphs);
1863 * cairo_glyph_free (glyphs);
1865 * </programlisting></informalexample>
1867 * If stack-based glyph and cluster arrays are to be used for small
1869 * <informalexample><programlisting>
1870 * cairo_status_t status;
1872 * cairo_glyph_t stack_glyphs[40];
1873 * cairo_glyph_t *glyphs = stack_glyphs;
1874 * int num_glyphs = sizeof (stack_glyphs) / sizeof (stack_glyphs[0]);
1875 * cairo_text_cluster_t stack_clusters[40];
1876 * cairo_text_cluster_t *clusters = stack_clusters;
1877 * int num_clusters = sizeof (stack_clusters) / sizeof (stack_clusters[0]);
1878 * cairo_text_cluster_flags_t cluster_flags;
1880 * status = cairo_scaled_font_text_to_glyphs (scaled_font,
1883 * &glyphs, &num_glyphs,
1884 * &clusters, &num_clusters, &cluster_flags);
1886 * if (status == CAIRO_STATUS_SUCCESS) {
1887 * cairo_show_text_glyphs (cr,
1889 * glyphs, num_glyphs,
1890 * clusters, num_clusters, cluster_flags);
1892 * if (glyphs != stack_glyphs)
1893 * cairo_glyph_free (glyphs);
1894 * if (clusters != stack_clusters)
1895 * cairo_text_cluster_free (clusters);
1897 * </programlisting></informalexample>
1899 * For details of how @clusters, @num_clusters, and @cluster_flags map input
1900 * UTF-8 text to the output glyphs see cairo_show_text_glyphs().
1902 * The output values can be readily passed to cairo_show_text_glyphs()
1903 * cairo_show_glyphs(), or related functions, assuming that the exact
1904 * same @scaled_font is used for the operation.
1906 * Return value: %CAIRO_STATUS_SUCCESS upon success, or an error status
1907 * if the input values are wrong or if conversion failed. If the input
1908 * values are correct but the conversion failed, the error status is also
1909 * set on @scaled_font.
1913 #define CACHING_THRESHOLD 16
1915 cairo_scaled_font_text_to_glyphs (cairo_scaled_font_t *scaled_font,
1920 cairo_glyph_t **glyphs,
1922 cairo_text_cluster_t **clusters,
1924 cairo_text_cluster_flags_t *cluster_flags)
1927 cairo_int_status_t status;
1928 cairo_glyph_t *orig_glyphs;
1929 cairo_text_cluster_t *orig_clusters;
1931 status = scaled_font->status;
1932 if (unlikely (status))
1935 /* A slew of sanity checks */
1937 /* glyphs and num_glyphs can't be NULL */
1938 if (glyphs == NULL ||
1939 num_glyphs == NULL) {
1940 status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
1944 /* Special case for NULL and -1 */
1945 if (utf8 == NULL && utf8_len == -1)
1948 /* No NULLs for non-NULLs! */
1949 if ((utf8_len && utf8 == NULL) ||
1950 (clusters && num_clusters == NULL) ||
1951 (clusters && cluster_flags == NULL)) {
1952 status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
1956 /* A -1 for utf8_len means NUL-terminated */
1958 utf8_len = strlen (utf8);
1960 /* A NULL *glyphs means no prealloced glyphs array */
1961 if (glyphs && *glyphs == NULL)
1964 /* A NULL *clusters means no prealloced clusters array */
1965 if (clusters && *clusters == NULL)
1968 if (!clusters && num_clusters) {
1969 num_clusters = NULL;
1972 if (cluster_flags) {
1973 *cluster_flags = FALSE;
1976 if (!clusters && cluster_flags) {
1977 cluster_flags = NULL;
1980 /* Apart from that, no negatives */
1983 (num_clusters && *num_clusters < 0)) {
1984 status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
1988 if (utf8_len == 0) {
1989 status = CAIRO_STATUS_SUCCESS;
1993 /* validate input so backend does not have to */
1994 status = _cairo_utf8_to_ucs4 (utf8, utf8_len, NULL, &num_chars);
1995 if (unlikely (status))
1998 _cairo_scaled_font_freeze_cache (scaled_font);
2000 orig_glyphs = *glyphs;
2001 orig_clusters = clusters ? *clusters : NULL;
2003 if (scaled_font->backend->text_to_glyphs) {
2004 status = scaled_font->backend->text_to_glyphs (scaled_font, x, y,
2007 clusters, num_clusters,
2009 if (status != CAIRO_INT_STATUS_UNSUPPORTED) {
2010 if (status == CAIRO_INT_STATUS_SUCCESS) {
2011 /* The checks here are crude; we only should do them in
2012 * user-font backend, but they don't hurt here. This stuff
2013 * can be hard to get right. */
2015 if (*num_glyphs < 0) {
2016 status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
2019 if (num_glyphs && *glyphs == NULL) {
2020 status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2025 if (*num_clusters < 0) {
2026 status = _cairo_error (CAIRO_STATUS_NEGATIVE_COUNT);
2029 if (num_clusters && *clusters == NULL) {
2030 status = _cairo_error (CAIRO_STATUS_NULL_POINTER);
2034 /* Don't trust the backend, validate clusters! */
2036 _cairo_validate_text_clusters (utf8, utf8_len,
2037 *glyphs, *num_glyphs,
2038 *clusters, *num_clusters,
2047 if (*num_glyphs < num_chars) {
2048 *glyphs = cairo_glyph_allocate (num_chars);
2049 if (unlikely (*glyphs == NULL)) {
2050 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2054 *num_glyphs = num_chars;
2057 if (*num_clusters < num_chars) {
2058 *clusters = cairo_text_cluster_allocate (num_chars);
2059 if (unlikely (*clusters == NULL)) {
2060 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2064 *num_clusters = num_chars;
2067 if (num_chars > CACHING_THRESHOLD)
2068 status = cairo_scaled_font_text_to_glyphs_internal_cached (scaled_font,
2075 status = cairo_scaled_font_text_to_glyphs_internal_uncached (scaled_font,
2082 DONE: /* error that should be logged on scaled_font happened */
2083 _cairo_scaled_font_thaw_cache (scaled_font);
2085 if (unlikely (status)) {
2087 if (*glyphs != orig_glyphs) {
2088 cairo_glyph_free (*glyphs);
2089 *glyphs = orig_glyphs;
2094 if (*clusters != orig_clusters) {
2095 cairo_text_cluster_free (*clusters);
2096 *clusters = orig_clusters;
2101 return _cairo_scaled_font_set_error (scaled_font, status);
2103 BAIL: /* error with input arguments */
2113 slim_hidden_def (cairo_scaled_font_text_to_glyphs);
2115 /* XXX: Shoot me - cairo has two basic ways of rendring text in API.
2116 * The first way is cairo_show_text() and the second way is
2117 * cairo_show_glyphs().
2119 * For the first method, each glyph location is advanced by x_advance
2120 * and y_advance computed from glyph metrics. For the second method,
2121 * app is responsible for supplying the glyph location. This gives us
2122 * opportunity to check whether the computed x_advance and _y_advance
2123 * is equala/larger than supplied glyphs difference for each adjacent
2124 * glyphs. The rational is that if the spacing between glyphs is at
2125 * least larger than computed x_advance/y_advance, there should be no
2126 * overlapping in text portion of the text images.
2128 static inline cairo_bool_t
2129 _glyph_is_next_to_glyph (cairo_glyph_t *prev,
2130 cairo_glyph_t *current,
2134 return current->x - prev->x >= x_advance - TOLERANCE &&
2135 current->y - prev->y >= y_advance - TOLERANCE;
2138 static inline cairo_bool_t
2139 _range_contains_glyph (const cairo_box_t *extents,
2142 cairo_fixed_t right,
2143 cairo_fixed_t bottom)
2145 if (left == right || top == bottom)
2148 return right > extents->p1.x &&
2149 left < extents->p2.x &&
2150 bottom > extents->p1.y &&
2151 top < extents->p2.y;
2154 static cairo_status_t
2155 _cairo_scaled_font_single_glyph_device_extents (cairo_scaled_font_t *scaled_font,
2156 const cairo_glyph_t *glyph,
2157 cairo_rectangle_int_t *extents)
2159 cairo_round_glyph_positions_t round_xy;
2160 cairo_scaled_glyph_t *scaled_glyph;
2161 cairo_status_t status;
2165 status = _cairo_scaled_glyph_lookup (scaled_font,
2167 CAIRO_SCALED_GLYPH_INFO_METRICS,
2169 if (unlikely (status))
2172 round_xy = _cairo_font_options_get_round_glyph_positions (&scaled_font->options);
2173 if (round_xy == CAIRO_ROUND_GLYPH_POS_ON)
2174 v = _cairo_fixed_from_int (_cairo_lround (glyph->x));
2176 v = _cairo_fixed_from_double (glyph->x);
2177 box.p1.x = v + scaled_glyph->bbox.p1.x;
2178 box.p2.x = v + scaled_glyph->bbox.p2.x;
2180 if (round_xy == CAIRO_ROUND_GLYPH_POS_ON)
2181 v = _cairo_fixed_from_int (_cairo_lround (glyph->y));
2183 v = _cairo_fixed_from_double (glyph->y);
2184 box.p1.y = v + scaled_glyph->bbox.p1.y;
2185 box.p2.y = v + scaled_glyph->bbox.p2.y;
2187 _cairo_box_round_to_rectangle (&box, extents);
2188 return CAIRO_STATUS_SUCCESS;
2192 * Compute a device-space bounding box for the glyphs.
2195 _cairo_scaled_font_glyph_device_extents (cairo_scaled_font_t *scaled_font,
2196 const cairo_glyph_t *glyphs,
2198 cairo_rectangle_int_t *extents,
2199 cairo_bool_t *overlap_out)
2201 cairo_status_t status = CAIRO_STATUS_SUCCESS;
2202 cairo_box_t box = { { INT_MAX, INT_MAX }, { INT_MIN, INT_MIN }};
2203 cairo_scaled_glyph_t *glyph_cache[64];
2204 cairo_bool_t overlap = overlap_out ? FALSE : TRUE;
2205 cairo_scaled_glyph_t *prev_scaled_glyph = NULL;
2206 cairo_round_glyph_positions_t round_glyph_positions = _cairo_font_options_get_round_glyph_positions (&scaled_font->options);
2208 cairo_bool_t is_next = FALSE;
2210 if (unlikely (scaled_font->status))
2211 return scaled_font->status;
2213 if (num_glyphs == 1) {
2215 *overlap_out = FALSE;
2216 return _cairo_scaled_font_single_glyph_device_extents (scaled_font,
2221 _cairo_scaled_font_freeze_cache (scaled_font);
2223 memset (glyph_cache, 0, sizeof (glyph_cache));
2225 for (i = 0; i < num_glyphs; i++) {
2226 cairo_scaled_glyph_t *scaled_glyph;
2227 cairo_fixed_t x, y, x1, y1, x2, y2;
2228 int cache_index = glyphs[i].index % ARRAY_LENGTH (glyph_cache);
2230 scaled_glyph = glyph_cache[cache_index];
2231 if (scaled_glyph == NULL ||
2232 _cairo_scaled_glyph_index (scaled_glyph) != glyphs[i].index)
2234 status = _cairo_scaled_glyph_lookup (scaled_font,
2236 CAIRO_SCALED_GLYPH_INFO_METRICS,
2238 if (unlikely (status))
2241 glyph_cache[cache_index] = scaled_glyph;
2244 if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
2245 x = _cairo_fixed_from_int (_cairo_lround (glyphs[i].x));
2247 x = _cairo_fixed_from_double (glyphs[i].x);
2248 x1 = x + scaled_glyph->bbox.p1.x;
2249 x2 = x + scaled_glyph->bbox.p2.x;
2251 if (round_glyph_positions == CAIRO_ROUND_GLYPH_POS_ON)
2252 y = _cairo_fixed_from_int (_cairo_lround (glyphs[i].y));
2254 y = _cairo_fixed_from_double (glyphs[i].y);
2255 y1 = y + scaled_glyph->bbox.p1.y;
2256 y2 = y + scaled_glyph->bbox.p2.y;
2258 if (prev_scaled_glyph != NULL && overlap == FALSE) {
2259 is_next = _glyph_is_next_to_glyph ((cairo_glyph_t *)&glyphs[i-1],
2260 (cairo_glyph_t *)&glyphs[i],
2261 prev_scaled_glyph->metrics.x_advance,
2262 prev_scaled_glyph->metrics.y_advance);
2264 if (is_next == FALSE)
2265 overlap = _range_contains_glyph (&box, x1, y1, x2, y2);
2268 if (x1 < box.p1.x) box.p1.x = x1;
2269 if (x2 > box.p2.x) box.p2.x = x2;
2270 if (y1 < box.p1.y) box.p1.y = y1;
2271 if (y2 > box.p2.y) box.p2.y = y2;
2273 prev_scaled_glyph = scaled_glyph;
2276 _cairo_scaled_font_thaw_cache (scaled_font);
2277 if (unlikely (status))
2278 return _cairo_scaled_font_set_error (scaled_font, status);
2280 if (box.p1.x < box.p2.x) {
2281 _cairo_box_round_to_rectangle (&box, extents);
2283 extents->x = extents->y = 0;
2284 extents->width = extents->height = 0;
2287 if (overlap_out != NULL)
2288 *overlap_out = overlap;
2290 return CAIRO_STATUS_SUCCESS;
2294 _cairo_scaled_font_glyph_approximate_extents (cairo_scaled_font_t *scaled_font,
2295 const cairo_glyph_t *glyphs,
2297 cairo_rectangle_int_t *extents)
2299 double x0, x1, y0, y1, pad;
2302 /* If any of the factors are suspect (i.e. the font is broken), bail */
2303 if (scaled_font->fs_extents.max_x_advance == 0 ||
2304 scaled_font->fs_extents.height == 0 ||
2305 scaled_font->max_scale == 0)
2310 assert (num_glyphs);
2312 x0 = x1 = glyphs[0].x;
2313 y0 = y1 = glyphs[0].y;
2314 for (i = 1; i < num_glyphs; i++) {
2326 pad = MAX(scaled_font->fs_extents.max_x_advance,
2327 scaled_font->fs_extents.height);
2328 pad *= scaled_font->max_scale;
2330 extents->x = floor (x0 - pad);
2331 extents->width = ceil (x1 + pad) - extents->x;
2332 extents->y = floor (y0 - pad);
2333 extents->height = ceil (y1 + pad) - extents->y;
2340 _cairo_scaled_font_show_glyphs (cairo_scaled_font_t *scaled_font,
2341 cairo_operator_t op,
2342 const cairo_pattern_t *pattern,
2343 cairo_surface_t *surface,
2349 unsigned int height,
2350 cairo_glyph_t *glyphs,
2352 cairo_region_t *clip_region)
2354 cairo_int_status_t status;
2355 cairo_surface_t *mask = NULL;
2356 cairo_format_t mask_format = CAIRO_FORMAT_A1; /* shut gcc up */
2357 cairo_surface_pattern_t mask_pattern;
2360 /* These operators aren't interpreted the same way by the backends;
2361 * they are implemented in terms of other operators in cairo-gstate.c
2363 assert (op != CAIRO_OPERATOR_SOURCE && op != CAIRO_OPERATOR_CLEAR);
2365 if (scaled_font->status)
2366 return scaled_font->status;
2369 return CAIRO_STATUS_SUCCESS;
2371 if (scaled_font->backend->show_glyphs != NULL) {
2372 int remaining_glyphs = num_glyphs;
2373 status = scaled_font->backend->show_glyphs (scaled_font,
2382 glyphs += num_glyphs - remaining_glyphs;
2383 num_glyphs = remaining_glyphs;
2384 if (remaining_glyphs == 0)
2385 status = CAIRO_INT_STATUS_SUCCESS;
2386 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
2387 return _cairo_scaled_font_set_error (scaled_font, status);
2390 /* Font display routine either does not exist or failed. */
2392 _cairo_scaled_font_freeze_cache (scaled_font);
2394 for (i = 0; i < num_glyphs; i++) {
2396 cairo_image_surface_t *glyph_surface;
2397 cairo_scaled_glyph_t *scaled_glyph;
2399 status = _cairo_scaled_glyph_lookup (scaled_font,
2401 CAIRO_SCALED_GLYPH_INFO_SURFACE,
2404 if (unlikely (status))
2407 glyph_surface = scaled_glyph->surface;
2409 /* To start, create the mask using the format from the first
2410 * glyph. Later we'll deal with different formats. */
2412 mask_format = glyph_surface->format;
2413 mask = cairo_image_surface_create (mask_format, width, height);
2414 status = mask->status;
2415 if (unlikely (status))
2419 /* If we have glyphs of different formats, we "upgrade" the mask
2420 * to the wider of the formats. */
2421 if (glyph_surface->format != mask_format &&
2422 _cairo_format_bits_per_pixel (mask_format) <
2423 _cairo_format_bits_per_pixel (glyph_surface->format) )
2425 cairo_surface_t *new_mask;
2427 switch (glyph_surface->format) {
2428 case CAIRO_FORMAT_ARGB32:
2429 case CAIRO_FORMAT_A8:
2430 case CAIRO_FORMAT_A1:
2431 mask_format = glyph_surface->format;
2433 case CAIRO_FORMAT_RGB16_565:
2434 case CAIRO_FORMAT_RGB24:
2435 case CAIRO_FORMAT_RGB30:
2436 case CAIRO_FORMAT_INVALID:
2439 mask_format = CAIRO_FORMAT_ARGB32;
2443 new_mask = cairo_image_surface_create (mask_format, width, height);
2444 status = new_mask->status;
2445 if (unlikely (status)) {
2446 cairo_surface_destroy (new_mask);
2450 _cairo_pattern_init_for_surface (&mask_pattern, mask);
2451 /* Note that we only upgrade masks, i.e. A1 -> A8 -> ARGB32, so there is
2452 * never any component alpha here.
2454 status = _cairo_surface_composite (CAIRO_OPERATOR_ADD,
2455 &_cairo_pattern_white.base,
2464 _cairo_pattern_fini (&mask_pattern.base);
2466 if (unlikely (status)) {
2467 cairo_surface_destroy (new_mask);
2471 cairo_surface_destroy (mask);
2475 if (glyph_surface->width && glyph_surface->height) {
2476 cairo_surface_pattern_t glyph_pattern;
2478 /* round glyph locations to the nearest pixel */
2479 /* XXX: FRAGILE: We're ignoring device_transform scaling here. A bug? */
2480 x = _cairo_lround (glyphs[i].x -
2481 glyph_surface->base.device_transform.x0);
2482 y = _cairo_lround (glyphs[i].y -
2483 glyph_surface->base.device_transform.y0);
2485 _cairo_pattern_init_for_surface (&glyph_pattern,
2486 &glyph_surface->base);
2487 if (mask_format == CAIRO_FORMAT_ARGB32)
2488 glyph_pattern.base.has_component_alpha = TRUE;
2490 status = _cairo_surface_composite (CAIRO_OPERATOR_ADD,
2491 &_cairo_pattern_white.base,
2492 &glyph_pattern.base,
2496 x - dest_x, y - dest_y,
2497 glyph_surface->width,
2498 glyph_surface->height,
2501 _cairo_pattern_fini (&glyph_pattern.base);
2503 if (unlikely (status))
2508 _cairo_pattern_init_for_surface (&mask_pattern, mask);
2509 if (mask_format == CAIRO_FORMAT_ARGB32)
2510 mask_pattern.base.has_component_alpha = TRUE;
2512 status = _cairo_surface_composite (op, pattern, &mask_pattern.base,
2520 _cairo_pattern_fini (&mask_pattern.base);
2523 _cairo_scaled_font_thaw_cache (scaled_font);
2526 cairo_surface_destroy (mask);
2527 return _cairo_scaled_font_set_error (scaled_font, status);
2531 /* Add a single-device-unit rectangle to a path. */
2532 static cairo_status_t
2533 _add_unit_rectangle_to_path (cairo_path_fixed_t *path,
2537 cairo_status_t status;
2539 status = _cairo_path_fixed_move_to (path, x, y);
2540 if (unlikely (status))
2543 status = _cairo_path_fixed_rel_line_to (path,
2544 _cairo_fixed_from_int (1),
2545 _cairo_fixed_from_int (0));
2546 if (unlikely (status))
2549 status = _cairo_path_fixed_rel_line_to (path,
2550 _cairo_fixed_from_int (0),
2551 _cairo_fixed_from_int (1));
2552 if (unlikely (status))
2555 status = _cairo_path_fixed_rel_line_to (path,
2556 _cairo_fixed_from_int (-1),
2557 _cairo_fixed_from_int (0));
2558 if (unlikely (status))
2561 return _cairo_path_fixed_close_path (path);
2565 * _trace_mask_to_path:
2566 * @bitmap: An alpha mask (either %CAIRO_FORMAT_A1 or %CAIRO_FORMAT_A8)
2567 * @path: An initialized path to hold the result
2569 * Given a mask surface, (an alpha image), fill out the provided path
2570 * so that when filled it would result in something that approximates
2573 * Note: The current tracing code here is extremely primitive. It
2574 * operates only on an A1 surface, (converting an A8 surface to A1 if
2575 * necessary), and performs the tracing by drawing a little square
2576 * around each pixel that is on in the mask. We do not pretend that
2577 * this is a high-quality result. But we are leaving it up to someone
2578 * who cares enough about getting a better result to implement
2579 * something more sophisticated.
2581 static cairo_status_t
2582 _trace_mask_to_path (cairo_image_surface_t *mask,
2583 cairo_path_fixed_t *path,
2584 double tx, double ty)
2587 int rows, cols, bytes_per_row;
2590 cairo_fixed_t x0, y0;
2591 cairo_fixed_t px, py;
2592 cairo_status_t status;
2594 mask = _cairo_image_surface_coerce_to_format (mask, CAIRO_FORMAT_A1);
2595 status = mask->base.status;
2596 if (unlikely (status))
2599 cairo_surface_get_device_offset (&mask->base, &xoff, &yoff);
2600 x0 = _cairo_fixed_from_double (tx - xoff);
2601 y0 = _cairo_fixed_from_double (ty - yoff);
2603 bytes_per_row = (mask->width + 7) / 8;
2605 for (y = 0, rows = mask->height; rows--; row += mask->stride, y++) {
2606 const uint8_t *byte_ptr = row;
2608 py = _cairo_fixed_from_int (y);
2609 for (cols = bytes_per_row; cols--; ) {
2610 uint8_t byte = *byte_ptr++;
2616 byte = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (byte);
2617 for (bit = 1 << 7; bit && x < mask->width; bit >>= 1, x++) {
2619 px = _cairo_fixed_from_int (x);
2620 status = _add_unit_rectangle_to_path (path,
2623 if (unlikely (status))
2631 cairo_surface_destroy (&mask->base);
2637 _cairo_scaled_font_glyph_path (cairo_scaled_font_t *scaled_font,
2638 const cairo_glyph_t *glyphs,
2640 cairo_path_fixed_t *path)
2642 cairo_int_status_t status;
2645 status = scaled_font->status;
2646 if (unlikely (status))
2649 _cairo_scaled_font_freeze_cache (scaled_font);
2650 for (i = 0; i < num_glyphs; i++) {
2651 cairo_scaled_glyph_t *scaled_glyph;
2653 status = _cairo_scaled_glyph_lookup (scaled_font,
2655 CAIRO_SCALED_GLYPH_INFO_PATH,
2657 if (status == CAIRO_INT_STATUS_SUCCESS) {
2658 status = _cairo_path_fixed_append (path,
2660 _cairo_fixed_from_double (glyphs[i].x),
2661 _cairo_fixed_from_double (glyphs[i].y));
2663 } else if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
2664 /* If the font is incapable of providing a path, then we'll
2665 * have to trace our own from a surface.
2667 status = _cairo_scaled_glyph_lookup (scaled_font,
2669 CAIRO_SCALED_GLYPH_INFO_SURFACE,
2671 if (unlikely (status))
2674 status = _trace_mask_to_path (scaled_glyph->surface, path,
2675 glyphs[i].x, glyphs[i].y);
2678 if (unlikely (status))
2682 _cairo_scaled_font_thaw_cache (scaled_font);
2684 return _cairo_scaled_font_set_error (scaled_font, status);
2688 * _cairo_scaled_glyph_set_metrics:
2689 * @scaled_glyph: a #cairo_scaled_glyph_t
2690 * @scaled_font: a #cairo_scaled_font_t
2691 * @fs_metrics: a #cairo_text_extents_t in font space
2693 * _cairo_scaled_glyph_set_metrics() stores user space metrics
2694 * for the specified glyph given font space metrics. It is
2695 * called by the font backend when initializing a glyph with
2696 * %CAIRO_SCALED_GLYPH_INFO_METRICS.
2699 _cairo_scaled_glyph_set_metrics (cairo_scaled_glyph_t *scaled_glyph,
2700 cairo_scaled_font_t *scaled_font,
2701 cairo_text_extents_t *fs_metrics)
2703 cairo_bool_t first = TRUE;
2705 double min_user_x = 0.0, max_user_x = 0.0, min_user_y = 0.0, max_user_y = 0.0;
2706 double min_device_x = 0.0, max_device_x = 0.0, min_device_y = 0.0, max_device_y = 0.0;
2707 double device_x_advance, device_y_advance;
2709 scaled_glyph->fs_metrics = *fs_metrics;
2711 for (hm = 0.0; hm <= 1.0; hm += 1.0)
2712 for (wm = 0.0; wm <= 1.0; wm += 1.0) {
2715 /* Transform this corner to user space */
2716 x = fs_metrics->x_bearing + fs_metrics->width * wm;
2717 y = fs_metrics->y_bearing + fs_metrics->height * hm;
2718 cairo_matrix_transform_point (&scaled_font->font_matrix,
2721 min_user_x = max_user_x = x;
2722 min_user_y = max_user_y = y;
2724 if (x < min_user_x) min_user_x = x;
2725 if (x > max_user_x) max_user_x = x;
2726 if (y < min_user_y) min_user_y = y;
2727 if (y > max_user_y) max_user_y = y;
2730 /* Transform this corner to device space from glyph origin */
2731 x = fs_metrics->x_bearing + fs_metrics->width * wm;
2732 y = fs_metrics->y_bearing + fs_metrics->height * hm;
2733 cairo_matrix_transform_distance (&scaled_font->scale,
2737 min_device_x = max_device_x = x;
2738 min_device_y = max_device_y = y;
2740 if (x < min_device_x) min_device_x = x;
2741 if (x > max_device_x) max_device_x = x;
2742 if (y < min_device_y) min_device_y = y;
2743 if (y > max_device_y) max_device_y = y;
2747 scaled_glyph->metrics.x_bearing = min_user_x;
2748 scaled_glyph->metrics.y_bearing = min_user_y;
2749 scaled_glyph->metrics.width = max_user_x - min_user_x;
2750 scaled_glyph->metrics.height = max_user_y - min_user_y;
2752 scaled_glyph->metrics.x_advance = fs_metrics->x_advance;
2753 scaled_glyph->metrics.y_advance = fs_metrics->y_advance;
2754 cairo_matrix_transform_distance (&scaled_font->font_matrix,
2755 &scaled_glyph->metrics.x_advance,
2756 &scaled_glyph->metrics.y_advance);
2758 device_x_advance = fs_metrics->x_advance;
2759 device_y_advance = fs_metrics->y_advance;
2760 cairo_matrix_transform_distance (&scaled_font->scale,
2764 scaled_glyph->bbox.p1.x = _cairo_fixed_from_double (min_device_x);
2765 scaled_glyph->bbox.p1.y = _cairo_fixed_from_double (min_device_y);
2766 scaled_glyph->bbox.p2.x = _cairo_fixed_from_double (max_device_x);
2767 scaled_glyph->bbox.p2.y = _cairo_fixed_from_double (max_device_y);
2769 scaled_glyph->x_advance = _cairo_lround (device_x_advance);
2770 scaled_glyph->y_advance = _cairo_lround (device_y_advance);
2772 scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_METRICS;
2776 _cairo_scaled_glyph_set_surface (cairo_scaled_glyph_t *scaled_glyph,
2777 cairo_scaled_font_t *scaled_font,
2778 cairo_image_surface_t *surface)
2780 if (scaled_glyph->surface != NULL)
2781 cairo_surface_destroy (&scaled_glyph->surface->base);
2783 /* sanity check the backend glyph contents */
2784 _cairo_debug_check_image_surface_is_defined (&surface->base);
2785 scaled_glyph->surface = surface;
2787 if (surface != NULL)
2788 scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_SURFACE;
2790 scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_SURFACE;
2794 _cairo_scaled_glyph_set_path (cairo_scaled_glyph_t *scaled_glyph,
2795 cairo_scaled_font_t *scaled_font,
2796 cairo_path_fixed_t *path)
2798 if (scaled_glyph->path != NULL)
2799 _cairo_path_fixed_destroy (scaled_glyph->path);
2801 scaled_glyph->path = path;
2804 scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_PATH;
2806 scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_PATH;
2810 _cairo_scaled_glyph_set_recording_surface (cairo_scaled_glyph_t *scaled_glyph,
2811 cairo_scaled_font_t *scaled_font,
2812 cairo_surface_t *recording_surface)
2814 if (scaled_glyph->recording_surface != NULL) {
2815 cairo_surface_finish (scaled_glyph->recording_surface);
2816 cairo_surface_destroy (scaled_glyph->recording_surface);
2819 scaled_glyph->recording_surface = recording_surface;
2821 if (recording_surface != NULL)
2822 scaled_glyph->has_info |= CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
2824 scaled_glyph->has_info &= ~CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE;
2828 _cairo_scaled_glyph_page_can_remove (const void *closure)
2830 const cairo_scaled_glyph_page_t *page = closure;
2831 const cairo_scaled_font_t *scaled_font;
2833 scaled_font = (cairo_scaled_font_t *) page->cache_entry.hash;
2834 return scaled_font->cache_frozen == 0;
2837 static cairo_status_t
2838 _cairo_scaled_font_allocate_glyph (cairo_scaled_font_t *scaled_font,
2839 cairo_scaled_glyph_t **scaled_glyph)
2841 cairo_scaled_glyph_page_t *page;
2842 cairo_status_t status;
2844 /* only the first page in the list may contain available slots */
2845 if (! cairo_list_is_empty (&scaled_font->glyph_pages)) {
2846 page = cairo_list_last_entry (&scaled_font->glyph_pages,
2847 cairo_scaled_glyph_page_t,
2849 if (page->num_glyphs < CAIRO_SCALED_GLYPH_PAGE_SIZE) {
2850 *scaled_glyph = &page->glyphs[page->num_glyphs++];
2851 return CAIRO_STATUS_SUCCESS;
2855 page = malloc (sizeof (cairo_scaled_glyph_page_t));
2856 if (unlikely (page == NULL))
2857 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2859 page->cache_entry.hash = (unsigned long) scaled_font;
2860 page->cache_entry.size = 1; /* XXX occupancy weighting? */
2861 page->num_glyphs = 0;
2863 CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
2864 if (scaled_font->global_cache_frozen == FALSE) {
2865 if (unlikely (cairo_scaled_glyph_page_cache.hash_table == NULL)) {
2866 status = _cairo_cache_init (&cairo_scaled_glyph_page_cache,
2868 _cairo_scaled_glyph_page_can_remove,
2869 _cairo_scaled_glyph_page_destroy,
2870 MAX_GLYPH_PAGES_CACHED);
2871 if (unlikely (status)) {
2872 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2878 _cairo_cache_freeze (&cairo_scaled_glyph_page_cache);
2879 scaled_font->global_cache_frozen = TRUE;
2882 status = _cairo_cache_insert (&cairo_scaled_glyph_page_cache,
2883 &page->cache_entry);
2884 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2885 if (unlikely (status)) {
2890 cairo_list_add_tail (&page->link, &scaled_font->glyph_pages);
2892 *scaled_glyph = &page->glyphs[page->num_glyphs++];
2893 return CAIRO_STATUS_SUCCESS;
2897 _cairo_scaled_font_free_last_glyph (cairo_scaled_font_t *scaled_font,
2898 cairo_scaled_glyph_t *scaled_glyph)
2900 cairo_scaled_glyph_page_t *page;
2902 assert (! cairo_list_is_empty (&scaled_font->glyph_pages));
2903 page = cairo_list_last_entry (&scaled_font->glyph_pages,
2904 cairo_scaled_glyph_page_t,
2906 assert (scaled_glyph == &page->glyphs[page->num_glyphs-1]);
2908 _cairo_scaled_glyph_fini (scaled_font, scaled_glyph);
2910 if (--page->num_glyphs == 0) {
2911 CAIRO_MUTEX_LOCK (_cairo_scaled_glyph_page_cache_mutex);
2912 _cairo_cache_remove (&cairo_scaled_glyph_page_cache,
2913 &page->cache_entry);
2914 CAIRO_MUTEX_UNLOCK (_cairo_scaled_glyph_page_cache_mutex);
2919 * _cairo_scaled_glyph_lookup:
2920 * @scaled_font: a #cairo_scaled_font_t
2921 * @index: the glyph to create
2922 * @info: a #cairo_scaled_glyph_info_t marking which portions of
2923 * the glyph should be filled in.
2924 * @scaled_glyph_ret: a #cairo_scaled_glyph_t where the glyph
2927 * If the desired info is not available, (for example, when trying to
2928 * get INFO_PATH with a bitmapped font), this function will return
2929 * %CAIRO_INT_STATUS_UNSUPPORTED.
2931 * Note: This function must be called with the scaled font frozen, and it must
2932 * remain frozen for as long as the @scaled_glyph_ret is alive. (If the scaled
2933 * font was not frozen, then there is no guarantee that the glyph would not be
2934 * evicted before you tried to access it.) See
2935 * _cairo_scaled_font_freeze_cache() and _cairo_scaled_font_thaw_cache().
2937 * Returns: a glyph with the requested portions filled in. Glyph
2938 * lookup is cached and glyph will be automatically freed along
2939 * with the scaled_font so no explicit free is required.
2940 * @info can be one or more of:
2941 * %CAIRO_SCALED_GLYPH_INFO_METRICS - glyph metrics and bounding box
2942 * %CAIRO_SCALED_GLYPH_INFO_SURFACE - surface holding glyph image
2943 * %CAIRO_SCALED_GLYPH_INFO_PATH - path holding glyph outline in device space
2946 _cairo_scaled_glyph_lookup (cairo_scaled_font_t *scaled_font,
2947 unsigned long index,
2948 cairo_scaled_glyph_info_t info,
2949 cairo_scaled_glyph_t **scaled_glyph_ret)
2951 cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
2952 cairo_scaled_glyph_t *scaled_glyph;
2953 cairo_scaled_glyph_info_t need_info;
2955 *scaled_glyph_ret = NULL;
2957 if (unlikely (scaled_font->status))
2958 return scaled_font->status;
2960 assert (CAIRO_MUTEX_IS_LOCKED(scaled_font->mutex));
2962 if (CAIRO_INJECT_FAULT ())
2963 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2966 * Check cache for glyph
2968 scaled_glyph = _cairo_hash_table_lookup (scaled_font->glyphs,
2969 (cairo_hash_entry_t *) &index);
2970 if (scaled_glyph == NULL) {
2971 status = _cairo_scaled_font_allocate_glyph (scaled_font, &scaled_glyph);
2972 if (unlikely (status))
2975 memset (scaled_glyph, 0, sizeof (cairo_scaled_glyph_t));
2976 _cairo_scaled_glyph_set_index (scaled_glyph, index);
2977 cairo_list_init (&scaled_glyph->dev_privates);
2979 /* ask backend to initialize metrics and shape fields */
2981 scaled_font->backend->scaled_glyph_init (scaled_font,
2983 info | CAIRO_SCALED_GLYPH_INFO_METRICS);
2984 if (unlikely (status)) {
2985 _cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
2989 status = _cairo_hash_table_insert (scaled_font->glyphs,
2990 &scaled_glyph->hash_entry);
2991 if (unlikely (status)) {
2992 _cairo_scaled_font_free_last_glyph (scaled_font, scaled_glyph);
2998 * Check and see if the glyph, as provided,
2999 * already has the requested data and amend it if not
3001 need_info = info & ~scaled_glyph->has_info;
3003 status = scaled_font->backend->scaled_glyph_init (scaled_font,
3006 if (unlikely (status))
3009 /* Don't trust the scaled_glyph_init() return value, the font
3010 * backend may not even know about some of the info. For example,
3011 * no backend other than the user-fonts knows about recording-surface
3013 if (info & ~scaled_glyph->has_info)
3014 return CAIRO_INT_STATUS_UNSUPPORTED;
3017 *scaled_glyph_ret = scaled_glyph;
3018 return CAIRO_STATUS_SUCCESS;
3021 /* It's not an error for the backend to not support the info we want. */
3022 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3023 status = _cairo_scaled_font_set_error (scaled_font, status);
3028 _cairo_scaled_font_get_max_scale (cairo_scaled_font_t *scaled_font)
3030 return scaled_font->max_scale;
3035 * cairo_scaled_font_get_font_face:
3036 * @scaled_font: a #cairo_scaled_font_t
3038 * Gets the font face that this scaled font uses. This might be the
3039 * font face passed to cairo_scaled_font_create(), but this does not
3040 * hold true for all possible cases.
3042 * Return value: The #cairo_font_face_t with which @scaled_font was
3048 cairo_scaled_font_get_font_face (cairo_scaled_font_t *scaled_font)
3050 if (scaled_font->status)
3051 return (cairo_font_face_t*) &_cairo_font_face_nil;
3053 if (scaled_font->original_font_face != NULL)
3054 return scaled_font->original_font_face;
3056 return scaled_font->font_face;
3058 slim_hidden_def (cairo_scaled_font_get_font_face);
3061 * cairo_scaled_font_get_font_matrix:
3062 * @scaled_font: a #cairo_scaled_font_t
3063 * @font_matrix: return value for the matrix
3065 * Stores the font matrix with which @scaled_font was created into
3071 cairo_scaled_font_get_font_matrix (cairo_scaled_font_t *scaled_font,
3072 cairo_matrix_t *font_matrix)
3074 if (scaled_font->status) {
3075 cairo_matrix_init_identity (font_matrix);
3079 *font_matrix = scaled_font->font_matrix;
3081 slim_hidden_def (cairo_scaled_font_get_font_matrix);
3084 * cairo_scaled_font_get_ctm:
3085 * @scaled_font: a #cairo_scaled_font_t
3086 * @ctm: return value for the CTM
3088 * Stores the CTM with which @scaled_font was created into @ctm.
3089 * Note that the translation offsets (x0, y0) of the CTM are ignored
3090 * by cairo_scaled_font_create(). So, the matrix this
3091 * function returns always has 0,0 as x0,y0.
3096 cairo_scaled_font_get_ctm (cairo_scaled_font_t *scaled_font,
3097 cairo_matrix_t *ctm)
3099 if (scaled_font->status) {
3100 cairo_matrix_init_identity (ctm);
3104 *ctm = scaled_font->ctm;
3106 slim_hidden_def (cairo_scaled_font_get_ctm);
3109 * cairo_scaled_font_get_scale_matrix:
3110 * @scaled_font: a #cairo_scaled_font_t
3111 * @scale_matrix: return value for the matrix
3113 * Stores the scale matrix of @scaled_font into @matrix.
3114 * The scale matrix is product of the font matrix and the ctm
3115 * associated with the scaled font, and hence is the matrix mapping from
3116 * font space to device space.
3121 cairo_scaled_font_get_scale_matrix (cairo_scaled_font_t *scaled_font,
3122 cairo_matrix_t *scale_matrix)
3124 if (scaled_font->status) {
3125 cairo_matrix_init_identity (scale_matrix);
3129 *scale_matrix = scaled_font->scale;
3133 * cairo_scaled_font_get_font_options:
3134 * @scaled_font: a #cairo_scaled_font_t
3135 * @options: return value for the font options
3137 * Stores the font options with which @scaled_font was created into
3143 cairo_scaled_font_get_font_options (cairo_scaled_font_t *scaled_font,
3144 cairo_font_options_t *options)
3146 if (cairo_font_options_status (options))
3149 if (scaled_font->status) {
3150 _cairo_font_options_init_default (options);
3154 _cairo_font_options_init_copy (options, &scaled_font->options);
3156 slim_hidden_def (cairo_scaled_font_get_font_options);