in class/System.Windows/System.Windows/:
[moon.git] / cairo / src / cairo-font-face.c
blob30c8d9fb3de47dd6566b6648a288470e9e921ff0
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
33 * California.
35 * Contributor(s):
36 * Carl D. Worth <cworth@cworth.org>
37 * Graydon Hoare <graydon@redhat.com>
38 * Owen Taylor <otaylor@redhat.com>
41 #define _BSD_SOURCE /* for strdup() */
42 #include "cairoint.h"
44 static const cairo_font_face_backend_t _cairo_toy_font_face_backend;
46 /* #cairo_font_face_t */
48 const cairo_toy_font_face_t _cairo_font_face_nil = {
50 { 0 }, /* hash_entry */
51 CAIRO_STATUS_NO_MEMORY, /* status */
52 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
53 { 0, 0, 0, NULL }, /* user_data */
54 &_cairo_toy_font_face_backend
56 CAIRO_FONT_FAMILY_DEFAULT, /* family */
57 TRUE, /* owns_family */
58 CAIRO_FONT_SLANT_DEFAULT, /* slant */
59 CAIRO_FONT_WEIGHT_DEFAULT /* weight */
62 static const cairo_toy_font_face_t _cairo_font_face_null_pointer = {
64 { 0 }, /* hash_entry */
65 CAIRO_STATUS_NULL_POINTER, /* status */
66 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
67 { 0, 0, 0, NULL }, /* user_data */
68 &_cairo_toy_font_face_backend
70 CAIRO_FONT_FAMILY_DEFAULT, /* family */
71 TRUE, /* owns_family */
72 CAIRO_FONT_SLANT_DEFAULT, /* slant */
73 CAIRO_FONT_WEIGHT_DEFAULT /* weight */
76 static const cairo_toy_font_face_t _cairo_font_face_invalid_string = {
78 { 0 }, /* hash_entry */
79 CAIRO_STATUS_INVALID_STRING, /* status */
80 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
81 { 0, 0, 0, NULL }, /* user_data */
82 &_cairo_toy_font_face_backend
84 CAIRO_FONT_FAMILY_DEFAULT, /* family */
85 TRUE, /* owns_family */
86 CAIRO_FONT_SLANT_DEFAULT, /* slant */
87 CAIRO_FONT_WEIGHT_DEFAULT /* weight */
90 static const cairo_toy_font_face_t _cairo_font_face_invalid_slant = {
92 { 0 }, /* hash_entry */
93 CAIRO_STATUS_INVALID_SLANT, /* status */
94 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
95 { 0, 0, 0, NULL }, /* user_data */
96 &_cairo_toy_font_face_backend
98 CAIRO_FONT_FAMILY_DEFAULT, /* family */
99 TRUE, /* owns_family */
100 CAIRO_FONT_SLANT_DEFAULT, /* slant */
101 CAIRO_FONT_WEIGHT_DEFAULT /* weight */
104 static const cairo_toy_font_face_t _cairo_font_face_invalid_weight = {
106 { 0 }, /* hash_entry */
107 CAIRO_STATUS_INVALID_WEIGHT, /* status */
108 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */
109 { 0, 0, 0, NULL }, /* user_data */
110 &_cairo_toy_font_face_backend
112 CAIRO_FONT_FAMILY_DEFAULT, /* family */
113 TRUE, /* owns_family */
114 CAIRO_FONT_SLANT_DEFAULT, /* slant */
115 CAIRO_FONT_WEIGHT_DEFAULT /* weight */
118 cairo_status_t
119 _cairo_font_face_set_error (cairo_font_face_t *font_face,
120 cairo_status_t status)
122 if (status == CAIRO_STATUS_SUCCESS)
123 return status;
125 /* Don't overwrite an existing error. This preserves the first
126 * error, which is the most significant. */
127 _cairo_status_set_error (&font_face->status, status);
129 return _cairo_error (status);
132 void
133 _cairo_font_face_init (cairo_font_face_t *font_face,
134 const cairo_font_face_backend_t *backend)
136 CAIRO_MUTEX_INITIALIZE ();
138 font_face->status = CAIRO_STATUS_SUCCESS;
139 CAIRO_REFERENCE_COUNT_INIT (&font_face->ref_count, 1);
140 font_face->backend = backend;
142 _cairo_user_data_array_init (&font_face->user_data);
146 * cairo_font_face_reference:
147 * @font_face: a #cairo_font_face_t, (may be %NULL in which case this
148 * function does nothing).
150 * Increases the reference count on @font_face by one. This prevents
151 * @font_face from being destroyed until a matching call to
152 * cairo_font_face_destroy() is made.
154 * The number of references to a #cairo_font_face_t can be get using
155 * cairo_font_face_get_reference_count().
157 * Return value: the referenced #cairo_font_face_t.
159 cairo_font_face_t *
160 cairo_font_face_reference (cairo_font_face_t *font_face)
162 if (font_face == NULL ||
163 CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
164 return font_face;
166 /* We would normally assert that we have a reference here but we
167 * can't get away with that due to the zombie case as documented
168 * in _cairo_ft_font_face_destroy. */
170 _cairo_reference_count_inc (&font_face->ref_count);
172 return font_face;
174 slim_hidden_def (cairo_font_face_reference);
177 * cairo_font_face_destroy:
178 * @font_face: a #cairo_font_face_t
180 * Decreases the reference count on @font_face by one. If the result
181 * is zero, then @font_face and all associated resources are freed.
182 * See cairo_font_face_reference().
184 void
185 cairo_font_face_destroy (cairo_font_face_t *font_face)
187 if (font_face == NULL ||
188 CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
189 return;
191 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&font_face->ref_count));
193 if (! _cairo_reference_count_dec_and_test (&font_face->ref_count))
194 return;
196 if (font_face->backend->destroy)
197 font_face->backend->destroy (font_face);
199 /* We allow resurrection to deal with some memory management for the
200 * FreeType backend where cairo_ft_font_face_t and cairo_ft_unscaled_font_t
201 * need to effectively mutually reference each other
203 if (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&font_face->ref_count))
204 return;
206 _cairo_user_data_array_fini (&font_face->user_data);
208 free (font_face);
210 slim_hidden_def (cairo_font_face_destroy);
213 * cairo_font_face_get_type:
214 * @font_face: a font face
216 * This function returns the type of the backend used to create
217 * a font face. See #cairo_font_type_t for available types.
219 * Return value: The type of @font_face.
221 * Since: 1.2
223 cairo_font_type_t
224 cairo_font_face_get_type (cairo_font_face_t *font_face)
226 return font_face->backend->type;
230 * cairo_font_face_get_reference_count:
231 * @font_face: a #cairo_font_face_t
233 * Returns the current reference count of @font_face.
235 * Return value: the current reference count of @font_face. If the
236 * object is a nil object, 0 will be returned.
238 * Since: 1.4
240 unsigned int
241 cairo_font_face_get_reference_count (cairo_font_face_t *font_face)
243 if (font_face == NULL ||
244 CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
245 return 0;
247 return CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->ref_count);
251 * cairo_font_face_status:
252 * @font_face: a #cairo_font_face_t
254 * Checks whether an error has previously occurred for this
255 * font face
257 * Return value: %CAIRO_STATUS_SUCCESS or another error such as
258 * %CAIRO_STATUS_NO_MEMORY.
260 cairo_status_t
261 cairo_font_face_status (cairo_font_face_t *font_face)
263 return font_face->status;
267 * cairo_font_face_get_user_data:
268 * @font_face: a #cairo_font_face_t
269 * @key: the address of the #cairo_user_data_key_t the user data was
270 * attached to
272 * Return user data previously attached to @font_face using the specified
273 * key. If no user data has been attached with the given key this
274 * function returns %NULL.
276 * Return value: the user data previously attached or %NULL.
278 void *
279 cairo_font_face_get_user_data (cairo_font_face_t *font_face,
280 const cairo_user_data_key_t *key)
282 return _cairo_user_data_array_get_data (&font_face->user_data,
283 key);
285 slim_hidden_def (cairo_font_face_get_user_data);
288 * cairo_font_face_set_user_data:
289 * @font_face: a #cairo_font_face_t
290 * @key: the address of a #cairo_user_data_key_t to attach the user data to
291 * @user_data: the user data to attach to the font face
292 * @destroy: a #cairo_destroy_func_t which will be called when the
293 * font face is destroyed or when new user data is attached using the
294 * same key.
296 * Attach user data to @font_face. To remove user data from a font face,
297 * call this function with the key that was used to set it and %NULL
298 * for @data.
300 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY if a
301 * slot could not be allocated for the user data.
303 cairo_status_t
304 cairo_font_face_set_user_data (cairo_font_face_t *font_face,
305 const cairo_user_data_key_t *key,
306 void *user_data,
307 cairo_destroy_func_t destroy)
309 if (CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->ref_count))
310 return font_face->status;
312 return _cairo_user_data_array_set_data (&font_face->user_data,
313 key, user_data, destroy);
315 slim_hidden_def (cairo_font_face_set_user_data);
317 static const cairo_font_face_backend_t _cairo_toy_font_face_backend;
319 static int
320 _cairo_toy_font_face_keys_equal (const void *key_a,
321 const void *key_b);
323 /* We maintain a hash table from family/weight/slant =>
324 * #cairo_font_face_t for #cairo_toy_font_t. The primary purpose of
325 * this mapping is to provide unique #cairo_font_face_t values so that
326 * our cache and mapping from #cairo_font_face_t => #cairo_scaled_font_t
327 * works. Once the corresponding #cairo_font_face_t objects fall out of
328 * downstream caches, we don't need them in this hash table anymore.
330 * Modifications to this hash table are protected by
331 * _cairo_font_face_mutex.
333 static cairo_hash_table_t *cairo_toy_font_face_hash_table = NULL;
335 static cairo_hash_table_t *
336 _cairo_toy_font_face_hash_table_lock (void)
338 CAIRO_MUTEX_LOCK (_cairo_font_face_mutex);
340 if (cairo_toy_font_face_hash_table == NULL)
342 cairo_toy_font_face_hash_table =
343 _cairo_hash_table_create (_cairo_toy_font_face_keys_equal);
345 if (cairo_toy_font_face_hash_table == NULL) {
346 CAIRO_MUTEX_UNLOCK (_cairo_font_face_mutex);
347 return NULL;
351 return cairo_toy_font_face_hash_table;
354 static void
355 _cairo_toy_font_face_hash_table_unlock (void)
357 CAIRO_MUTEX_UNLOCK (_cairo_font_face_mutex);
361 * _cairo_toy_font_face_init_key:
363 * Initialize those portions of #cairo_toy_font_face_t needed to use
364 * it as a hash table key, including the hash code buried away in
365 * font_face->base.hash_entry. No memory allocation is performed here
366 * so that no fini call is needed. We do this to make it easier to use
367 * an automatic #cairo_toy_font_face_t variable as a key.
369 static void
370 _cairo_toy_font_face_init_key (cairo_toy_font_face_t *key,
371 const char *family,
372 cairo_font_slant_t slant,
373 cairo_font_weight_t weight)
375 unsigned long hash;
377 key->family = family;
378 key->owns_family = FALSE;
380 key->slant = slant;
381 key->weight = weight;
383 /* 1607 and 1451 are just a couple of arbitrary primes. */
384 hash = _cairo_hash_string (family);
385 hash += ((unsigned long) slant) * 1607;
386 hash += ((unsigned long) weight) * 1451;
388 assert (hash != 0);
389 key->base.hash_entry.hash = hash;
392 static cairo_status_t
393 _cairo_toy_font_face_init (cairo_toy_font_face_t *font_face,
394 const char *family,
395 cairo_font_slant_t slant,
396 cairo_font_weight_t weight)
398 char *family_copy;
400 family_copy = strdup (family);
401 if (family_copy == NULL)
402 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
404 _cairo_toy_font_face_init_key (font_face, family_copy,
405 slant, weight);
406 font_face->owns_family = TRUE;
408 _cairo_font_face_init (&font_face->base, &_cairo_toy_font_face_backend);
410 return CAIRO_STATUS_SUCCESS;
413 static void
414 _cairo_toy_font_face_fini (cairo_toy_font_face_t *font_face)
416 /* We assert here that we own font_face->family before casting
417 * away the const qualifer. */
418 assert (font_face->owns_family);
419 free ((char*) font_face->family);
422 static int
423 _cairo_toy_font_face_keys_equal (const void *key_a,
424 const void *key_b)
426 const cairo_toy_font_face_t *face_a = key_a;
427 const cairo_toy_font_face_t *face_b = key_b;
429 return (strcmp (face_a->family, face_b->family) == 0 &&
430 face_a->slant == face_b->slant &&
431 face_a->weight == face_b->weight);
435 * cairo_toy_font_face_create:
436 * @family: a font family name, encoded in UTF-8
437 * @slant: the slant for the font
438 * @weight: the weight for the font
440 * Creates a font face from a triplet of family, slant, and weight.
441 * These font faces are used in implementation of the the #cairo_t "toy"
442 * font API.
444 * If @family is the zero-length string "", the platform-specific default
445 * family is assumed. The default family then can be queried using
446 * cairo_toy_font_face_get_family().
448 * The cairo_select_font_face() function uses this to create font faces.
449 * See that function for limitations of toy font faces.
451 * Return value: a newly created #cairo_font_face_t. Free with
452 * cairo_font_face_destroy() when you are done using it.
454 * Since: 1.8
456 cairo_font_face_t *
457 cairo_toy_font_face_create (const char *family,
458 cairo_font_slant_t slant,
459 cairo_font_weight_t weight)
461 cairo_status_t status;
462 cairo_toy_font_face_t key, *font_face;
463 cairo_hash_table_t *hash_table;
465 if (family == NULL)
466 return (cairo_font_face_t*) &_cairo_font_face_null_pointer;
468 /* Make sure we've got valid UTF-8 for the family */
469 status = _cairo_utf8_to_ucs4 (family, -1, NULL, NULL);
470 if (status == CAIRO_STATUS_INVALID_STRING)
471 return (cairo_font_face_t*) &_cairo_font_face_invalid_string;
472 else if (status)
473 return (cairo_font_face_t*) &_cairo_font_face_nil;
475 switch (slant) {
476 case CAIRO_FONT_SLANT_NORMAL:
477 case CAIRO_FONT_SLANT_ITALIC:
478 case CAIRO_FONT_SLANT_OBLIQUE:
479 break;
480 default:
481 return (cairo_font_face_t*) &_cairo_font_face_invalid_slant;
484 switch (weight) {
485 case CAIRO_FONT_WEIGHT_NORMAL:
486 case CAIRO_FONT_WEIGHT_BOLD:
487 break;
488 default:
489 return (cairo_font_face_t*) &_cairo_font_face_invalid_weight;
492 if (*family == '\0')
493 family = CAIRO_FONT_FAMILY_DEFAULT;
495 hash_table = _cairo_toy_font_face_hash_table_lock ();
496 if (hash_table == NULL)
497 goto UNWIND;
499 _cairo_toy_font_face_init_key (&key, family, slant, weight);
501 /* Return existing font_face if it exists in the hash table. */
502 if (_cairo_hash_table_lookup (hash_table,
503 &key.base.hash_entry,
504 (cairo_hash_entry_t **) &font_face))
506 if (! font_face->base.status) {
507 /* We increment the reference count here manually to avoid
508 double-locking. */
509 _cairo_reference_count_inc (&font_face->base.ref_count);
510 _cairo_toy_font_face_hash_table_unlock ();
511 return &font_face->base;
514 /* remove the bad font from the hash table */
515 _cairo_hash_table_remove (hash_table, &key.base.hash_entry);
516 font_face->base.hash_entry.hash = 0;
519 /* Otherwise create it and insert into hash table. */
520 font_face = malloc (sizeof (cairo_toy_font_face_t));
521 if (font_face == NULL) {
522 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
523 goto UNWIND_HASH_TABLE_LOCK;
526 status = _cairo_toy_font_face_init (font_face, family, slant, weight);
527 if (status)
528 goto UNWIND_FONT_FACE_MALLOC;
530 status = _cairo_hash_table_insert (hash_table, &font_face->base.hash_entry);
531 if (status)
532 goto UNWIND_FONT_FACE_INIT;
534 _cairo_toy_font_face_hash_table_unlock ();
536 return &font_face->base;
538 UNWIND_FONT_FACE_INIT:
539 _cairo_toy_font_face_fini (font_face);
540 UNWIND_FONT_FACE_MALLOC:
541 free (font_face);
542 UNWIND_HASH_TABLE_LOCK:
543 _cairo_toy_font_face_hash_table_unlock ();
544 UNWIND:
545 return (cairo_font_face_t*) &_cairo_font_face_nil;
547 slim_hidden_def (cairo_toy_font_face_create);
549 static void
550 _cairo_toy_font_face_destroy (void *abstract_face)
552 cairo_toy_font_face_t *font_face = abstract_face;
553 cairo_hash_table_t *hash_table;
555 if (font_face == NULL ||
556 CAIRO_REFERENCE_COUNT_IS_INVALID (&font_face->base.ref_count))
557 return;
559 hash_table = _cairo_toy_font_face_hash_table_lock ();
560 /* All created objects must have been mapped in the hash table. */
561 assert (hash_table != NULL);
563 if (font_face->base.hash_entry.hash != 0)
564 _cairo_hash_table_remove (hash_table, &font_face->base.hash_entry);
566 _cairo_toy_font_face_hash_table_unlock ();
568 _cairo_toy_font_face_fini (font_face);
571 static cairo_status_t
572 _cairo_toy_font_face_scaled_font_get_implementation (void *abstract_font_face,
573 cairo_font_face_t **font_face_out)
575 cairo_toy_font_face_t *font_face = abstract_font_face;
576 cairo_status_t status;
578 if (font_face->base.status)
579 return font_face->base.status;
581 if (CAIRO_SCALED_FONT_BACKEND_DEFAULT != &_cairo_user_scaled_font_backend)
583 const cairo_scaled_font_backend_t * backend = CAIRO_SCALED_FONT_BACKEND_DEFAULT;
585 if (backend->get_implementation == NULL) {
586 *font_face_out = &font_face->base;
587 return CAIRO_STATUS_SUCCESS;
590 status = backend->get_implementation (font_face,
591 font_face_out);
593 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
594 return _cairo_font_face_set_error (&font_face->base, status);
597 status = _cairo_user_scaled_font_backend.get_implementation (font_face,
598 font_face_out);
600 return _cairo_font_face_set_error (&font_face->base, status);
603 static cairo_status_t
604 _cairo_toy_font_face_scaled_font_create (void *abstract_font_face,
605 const cairo_matrix_t *font_matrix,
606 const cairo_matrix_t *ctm,
607 const cairo_font_options_t *options,
608 cairo_scaled_font_t **scaled_font)
610 cairo_toy_font_face_t *font_face = abstract_font_face;
611 cairo_status_t status;
613 if (font_face->base.status)
614 return font_face->base.status;
616 status = cairo_font_options_status ((cairo_font_options_t *) options);
617 if (status)
618 return status;
620 if (CAIRO_SCALED_FONT_BACKEND_DEFAULT != &_cairo_user_scaled_font_backend)
622 const cairo_scaled_font_backend_t * backend = CAIRO_SCALED_FONT_BACKEND_DEFAULT;
624 *scaled_font = NULL;
625 status = backend->create_toy (font_face,
626 font_matrix,
627 ctm,
628 options,
629 scaled_font);
631 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
632 return _cairo_font_face_set_error (&font_face->base, status);
634 if (*scaled_font)
635 cairo_scaled_font_destroy (*scaled_font);
638 status = _cairo_user_scaled_font_backend.create_toy (font_face,
639 font_matrix,
640 ctm,
641 options,
642 scaled_font);
644 return _cairo_font_face_set_error (&font_face->base, status);
647 static cairo_bool_t
648 _cairo_font_face_is_toy (cairo_font_face_t *font_face)
650 return font_face->backend == &_cairo_toy_font_face_backend;
654 * cairo_toy_font_face_get_family:
655 * @font_face: A toy font face
657 * Gets the familly name of a toy font.
659 * Return value: The family name. This string is owned by the font face
660 * and remains valid as long as the font face is alive (referenced).
662 * Since: 1.8
664 const char *
665 cairo_toy_font_face_get_family (cairo_font_face_t *font_face)
667 cairo_toy_font_face_t *toy_font_face = (cairo_toy_font_face_t *) font_face;
668 if (! _cairo_font_face_is_toy (font_face)) {
669 if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
670 return CAIRO_FONT_FAMILY_DEFAULT;
672 assert (toy_font_face->owns_family);
673 return toy_font_face->family;
677 * cairo_toy_font_face_get_slant:
678 * @font_face: A toy font face
680 * Gets the slant a toy font.
682 * Return value: The slant value
684 * Since: 1.8
686 cairo_font_slant_t
687 cairo_toy_font_face_get_slant (cairo_font_face_t *font_face)
689 cairo_toy_font_face_t *toy_font_face = (cairo_toy_font_face_t *) font_face;
690 if (! _cairo_font_face_is_toy (font_face)) {
691 if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
692 return CAIRO_FONT_SLANT_DEFAULT;
694 return toy_font_face->slant;
696 slim_hidden_def (cairo_toy_font_face_get_slant);
699 * cairo_toy_font_face_get_weight:
700 * @font_face: A toy font face
702 * Gets the weight a toy font.
704 * Return value: The weight value
706 * Since: 1.8
708 cairo_font_weight_t
709 cairo_toy_font_face_get_weight (cairo_font_face_t *font_face)
711 cairo_toy_font_face_t *toy_font_face = (cairo_toy_font_face_t *) font_face;
712 if (! _cairo_font_face_is_toy (font_face)) {
713 if (_cairo_font_face_set_error (font_face, CAIRO_STATUS_FONT_TYPE_MISMATCH))
714 return CAIRO_FONT_WEIGHT_DEFAULT;
716 return toy_font_face->weight;
718 slim_hidden_def (cairo_toy_font_face_get_weight);
720 static const cairo_font_face_backend_t _cairo_toy_font_face_backend = {
721 CAIRO_FONT_TYPE_TOY,
722 _cairo_toy_font_face_destroy,
723 _cairo_toy_font_face_scaled_font_get_implementation,
724 _cairo_toy_font_face_scaled_font_create
727 void
728 _cairo_unscaled_font_init (cairo_unscaled_font_t *unscaled_font,
729 const cairo_unscaled_font_backend_t *backend)
731 CAIRO_REFERENCE_COUNT_INIT (&unscaled_font->ref_count, 1);
732 unscaled_font->backend = backend;
735 cairo_unscaled_font_t *
736 _cairo_unscaled_font_reference (cairo_unscaled_font_t *unscaled_font)
738 if (unscaled_font == NULL)
739 return NULL;
741 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&unscaled_font->ref_count));
743 _cairo_reference_count_inc (&unscaled_font->ref_count);
745 return unscaled_font;
748 void
749 _cairo_unscaled_font_destroy (cairo_unscaled_font_t *unscaled_font)
751 if (unscaled_font == NULL)
752 return;
754 assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&unscaled_font->ref_count));
756 if (! _cairo_reference_count_dec_and_test (&unscaled_font->ref_count))
757 return;
759 unscaled_font->backend->destroy (unscaled_font);
761 free (unscaled_font);
764 void
765 _cairo_font_face_reset_static_data (void)
767 _cairo_scaled_font_map_destroy ();
769 /* We manually acquire the lock rather than calling
770 * cairo_toy_font_face_hash_table_lock simply to avoid
771 * creating the table only to destroy it again. */
772 CAIRO_MUTEX_LOCK (_cairo_font_face_mutex);
773 _cairo_hash_table_destroy (cairo_toy_font_face_hash_table);
774 cairo_toy_font_face_hash_table = NULL;
775 CAIRO_MUTEX_UNLOCK (_cairo_font_face_mutex);