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 © 2000 Keith Packard
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
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 Red Hat, Inc.
35 * Graydon Hoare <graydon@redhat.com>
36 * Owen Taylor <otaylor@redhat.com>
37 * Keith Packard <keithp@keithp.com>
38 * Carl Worth <cworth@cworth.org>
41 #define _BSD_SOURCE /* for strdup() */
44 #include "cairo-error-private.h"
45 #include "cairo-image-surface-private.h"
46 #include "cairo-ft-private.h"
47 #include "cairo-pattern-private.h"
51 #include "cairo-fontconfig-private.h"
54 #include FT_FREETYPE_H
57 #include FT_TRUETYPE_TABLES_H
59 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
60 #include FT_SYNTHESIS_H
63 #if HAVE_FT_LIBRARY_SETLCDFILTER
64 #include FT_LCD_FILTER_H
70 #define access(p, m) 0
73 /* Fontconfig version older than 2.6 didn't have these options */
75 #define FC_LCD_FILTER "lcdfilter"
77 /* Some Ubuntu versions defined FC_LCD_FILTER without defining the following */
80 #define FC_LCD_DEFAULT 1
81 #define FC_LCD_LIGHT 2
82 #define FC_LCD_LEGACY 3
85 /* FreeType version older than 2.3.5(?) didn't have these options */
86 #ifndef FT_LCD_FILTER_NONE
87 #define FT_LCD_FILTER_NONE 0
88 #define FT_LCD_FILTER_DEFAULT 1
89 #define FT_LCD_FILTER_LIGHT 2
90 #define FT_LCD_FILTER_LEGACY 16
93 #define DOUBLE_TO_26_6(d) ((FT_F26Dot6)((d) * 64.0))
94 #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
95 #define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0))
96 #define DOUBLE_FROM_16_16(t) ((double)(t) / 65536.0)
98 /* This is the max number of FT_face objects we keep open at once
100 #define MAX_OPEN_FACES 10
104 * @Title: FreeType Fonts
105 * @Short_Description: Font support for FreeType
106 * @See_Also: #cairo_font_face_t
108 * The FreeType font backend is primarily used to render text on GNU/Linux
109 * systems, but can be used on other platforms too.
115 * Defined if the FreeType font backend is available.
116 * This macro can be used to conditionally compile backend-specific code.
124 * Defined if the Fontconfig-specific functions of the FreeType font backend
126 * This macro can be used to conditionally compile backend-specific code.
132 * The simple 2x2 matrix is converted into separate scale and shape
133 * factors so that hinting works right
136 typedef struct _cairo_ft_font_transform {
137 double x_scale, y_scale;
139 } cairo_ft_font_transform_t;
142 * We create an object that corresponds to a single font on the disk;
143 * (identified by a filename/id pair) these are shared between all
144 * fonts using that file. For cairo_ft_font_face_create_for_ft_face(), we
145 * just create a one-off version with a permanent face value.
148 typedef struct _cairo_ft_font_face cairo_ft_font_face_t;
150 struct _cairo_ft_unscaled_font {
151 cairo_unscaled_font_t base;
153 cairo_bool_t from_face; /* was the FT_Face provided by user? */
154 FT_Face face; /* provided or cached face */
156 /* only set if from_face is false */
160 /* We temporarily scale the unscaled font as needed */
161 cairo_bool_t have_scale;
162 cairo_matrix_t current_scale;
163 double x_scale; /* Extracted X scale factor */
164 double y_scale; /* Extracted Y scale factor */
165 cairo_bool_t have_shape; /* true if the current scale has a non-scale component*/
166 cairo_matrix_t current_shape;
167 FT_Matrix Current_Shape;
172 cairo_ft_font_face_t *faces; /* Linked list of faces for this font */
176 _cairo_ft_unscaled_font_keys_equal (const void *key_a,
180 _cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled);
182 typedef struct _cairo_ft_options {
183 cairo_font_options_t base;
184 unsigned int load_flags; /* flags for FT_Load_Glyph */
185 unsigned int synth_flags;
186 } cairo_ft_options_t;
188 struct _cairo_ft_font_face {
189 cairo_font_face_t base;
191 cairo_ft_unscaled_font_t *unscaled;
192 cairo_ft_options_t ft_options;
193 cairo_ft_font_face_t *next;
195 #if CAIRO_HAS_FC_FONT
196 FcPattern *pattern; /* if pattern is set, the above fields will be NULL */
197 cairo_font_face_t *resolved_font_face;
198 FcConfig *resolved_config;
202 static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend;
204 #if CAIRO_HAS_FC_FONT
205 static cairo_status_t
206 _cairo_ft_font_options_substitute (const cairo_font_options_t *options,
209 static cairo_font_face_t *
210 _cairo_ft_resolve_pattern (FcPattern *pattern,
211 const cairo_matrix_t *font_matrix,
212 const cairo_matrix_t *ctm,
213 const cairo_font_options_t *options);
218 * We maintain a hash table to map file/id => #cairo_ft_unscaled_font_t.
219 * The hash table itself isn't limited in size. However, we limit the
220 * number of FT_Face objects we keep around; when we've exceeded that
221 * limit and need to create a new FT_Face, we dump the FT_Face from a
222 * random #cairo_ft_unscaled_font_t which has an unlocked FT_Face, (if
226 typedef struct _cairo_ft_unscaled_font_map {
227 cairo_hash_table_t *hash_table;
228 FT_Library ft_library;
230 } cairo_ft_unscaled_font_map_t;
232 static cairo_ft_unscaled_font_map_t *cairo_ft_unscaled_font_map = NULL;
236 _cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled);
239 _cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled);
242 _cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font);
246 _font_map_release_face_lock_held (cairo_ft_unscaled_font_map_t *font_map,
247 cairo_ft_unscaled_font_t *unscaled)
249 if (unscaled->face) {
250 FT_Done_Face (unscaled->face);
251 unscaled->face = NULL;
252 unscaled->have_scale = FALSE;
254 font_map->num_open_faces--;
258 static cairo_status_t
259 _cairo_ft_unscaled_font_map_create (void)
261 cairo_ft_unscaled_font_map_t *font_map;
263 /* This function is only intended to be called from
264 * _cairo_ft_unscaled_font_map_lock. So we'll crash if we can
265 * detect some other call path. */
266 assert (cairo_ft_unscaled_font_map == NULL);
268 font_map = malloc (sizeof (cairo_ft_unscaled_font_map_t));
269 if (unlikely (font_map == NULL))
270 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
272 font_map->hash_table =
273 _cairo_hash_table_create (_cairo_ft_unscaled_font_keys_equal);
275 if (unlikely (font_map->hash_table == NULL))
278 if (unlikely (FT_Init_FreeType (&font_map->ft_library)))
281 font_map->num_open_faces = 0;
283 cairo_ft_unscaled_font_map = font_map;
284 return CAIRO_STATUS_SUCCESS;
287 if (font_map->hash_table)
288 _cairo_hash_table_destroy (font_map->hash_table);
291 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
296 _cairo_ft_unscaled_font_map_pluck_entry (void *entry, void *closure)
298 cairo_ft_unscaled_font_t *unscaled = entry;
299 cairo_ft_unscaled_font_map_t *font_map = closure;
301 _cairo_hash_table_remove (font_map->hash_table,
302 &unscaled->base.hash_entry);
304 if (! unscaled->from_face)
305 _font_map_release_face_lock_held (font_map, unscaled);
307 _cairo_ft_unscaled_font_fini (unscaled);
312 _cairo_ft_unscaled_font_map_destroy (void)
314 cairo_ft_unscaled_font_map_t *font_map;
316 CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
317 font_map = cairo_ft_unscaled_font_map;
318 cairo_ft_unscaled_font_map = NULL;
319 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
321 if (font_map != NULL) {
322 _cairo_hash_table_foreach (font_map->hash_table,
323 _cairo_ft_unscaled_font_map_pluck_entry,
325 assert (font_map->num_open_faces == 0);
327 FT_Done_FreeType (font_map->ft_library);
329 _cairo_hash_table_destroy (font_map->hash_table);
335 static cairo_ft_unscaled_font_map_t *
336 _cairo_ft_unscaled_font_map_lock (void)
338 CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
340 if (unlikely (cairo_ft_unscaled_font_map == NULL)) {
341 if (unlikely (_cairo_ft_unscaled_font_map_create ())) {
342 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
347 return cairo_ft_unscaled_font_map;
351 _cairo_ft_unscaled_font_map_unlock (void)
353 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
357 _cairo_ft_unscaled_font_init_key (cairo_ft_unscaled_font_t *key,
358 cairo_bool_t from_face,
365 key->from_face = from_face;
366 key->filename = filename;
370 hash = _cairo_hash_string (filename);
371 /* the constants are just arbitrary primes */
372 hash += ((unsigned long) id) * 1607;
373 hash += ((unsigned long) face) * 2137;
375 key->base.hash_entry.hash = hash;
379 * _cairo_ft_unscaled_font_init:
381 * Initialize a #cairo_ft_unscaled_font_t.
383 * There are two basic flavors of #cairo_ft_unscaled_font_t, one
384 * created from an FT_Face and the other created from a filename/id
385 * pair. These two flavors are identified as from_face and !from_face.
387 * To initialize a from_face font, pass filename==%NULL, id=0 and the
390 * To initialize a !from_face font, pass the filename/id as desired
393 * Note that the code handles these two flavors in very distinct
394 * ways. For example there is a hash_table mapping
395 * filename/id->#cairo_unscaled_font_t in the !from_face case, but no
396 * parallel in the from_face case, (where the calling code would have
397 * to do its own mapping to ensure similar sharing).
399 static cairo_status_t
400 _cairo_ft_unscaled_font_init (cairo_ft_unscaled_font_t *unscaled,
401 cairo_bool_t from_face,
402 const char *filename,
406 _cairo_unscaled_font_init (&unscaled->base,
407 &cairo_ft_unscaled_font_backend);
410 unscaled->from_face = TRUE;
411 _cairo_ft_unscaled_font_init_key (unscaled, TRUE, NULL, 0, face);
415 unscaled->from_face = FALSE;
416 unscaled->face = NULL;
418 filename_copy = strdup (filename);
419 if (unlikely (filename_copy == NULL))
420 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
422 _cairo_ft_unscaled_font_init_key (unscaled, FALSE, filename_copy, id, NULL);
425 unscaled->have_scale = FALSE;
426 CAIRO_MUTEX_INIT (unscaled->mutex);
427 unscaled->lock_count = 0;
429 unscaled->faces = NULL;
431 return CAIRO_STATUS_SUCCESS;
435 * _cairo_ft_unscaled_font_fini:
437 * Free all data associated with a #cairo_ft_unscaled_font_t.
439 * CAUTION: The unscaled->face field must be %NULL before calling this
440 * function. This is because the #cairo_ft_unscaled_font_t_map keeps a
441 * count of these faces (font_map->num_open_faces) so it maintains the
442 * unscaled->face field while it has its lock held. See
443 * _font_map_release_face_lock_held().
446 _cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled)
448 assert (unscaled->face == NULL);
450 free (unscaled->filename);
451 unscaled->filename = NULL;
453 CAIRO_MUTEX_FINI (unscaled->mutex);
457 _cairo_ft_unscaled_font_keys_equal (const void *key_a,
460 const cairo_ft_unscaled_font_t *unscaled_a = key_a;
461 const cairo_ft_unscaled_font_t *unscaled_b = key_b;
463 if (unscaled_a->id == unscaled_b->id &&
464 unscaled_a->from_face == unscaled_b->from_face)
466 if (unscaled_a->from_face)
467 return unscaled_a->face == unscaled_b->face;
469 if (unscaled_a->filename == NULL && unscaled_b->filename == NULL)
471 else if (unscaled_a->filename == NULL || unscaled_b->filename == NULL)
474 return (strcmp (unscaled_a->filename, unscaled_b->filename) == 0);
480 /* Finds or creates a #cairo_ft_unscaled_font_t for the filename/id from
481 * pattern. Returns a new reference to the unscaled font.
483 static cairo_status_t
484 _cairo_ft_unscaled_font_create_internal (cairo_bool_t from_face,
488 cairo_ft_unscaled_font_t **out)
490 cairo_ft_unscaled_font_t key, *unscaled;
491 cairo_ft_unscaled_font_map_t *font_map;
492 cairo_status_t status;
494 font_map = _cairo_ft_unscaled_font_map_lock ();
495 if (unlikely (font_map == NULL))
496 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
498 _cairo_ft_unscaled_font_init_key (&key, from_face, filename, id, font_face);
500 /* Return existing unscaled font if it exists in the hash table. */
501 unscaled = _cairo_hash_table_lookup (font_map->hash_table,
502 &key.base.hash_entry);
503 if (unscaled != NULL) {
504 _cairo_unscaled_font_reference (&unscaled->base);
508 /* Otherwise create it and insert into hash table. */
509 unscaled = malloc (sizeof (cairo_ft_unscaled_font_t));
510 if (unlikely (unscaled == NULL)) {
511 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
512 goto UNWIND_FONT_MAP_LOCK;
515 status = _cairo_ft_unscaled_font_init (unscaled, from_face, filename, id, font_face);
516 if (unlikely (status))
517 goto UNWIND_UNSCALED_MALLOC;
519 assert (unscaled->base.hash_entry.hash == key.base.hash_entry.hash);
520 status = _cairo_hash_table_insert (font_map->hash_table,
521 &unscaled->base.hash_entry);
522 if (unlikely (status))
523 goto UNWIND_UNSCALED_FONT_INIT;
526 _cairo_ft_unscaled_font_map_unlock ();
528 return CAIRO_STATUS_SUCCESS;
530 UNWIND_UNSCALED_FONT_INIT:
531 _cairo_ft_unscaled_font_fini (unscaled);
532 UNWIND_UNSCALED_MALLOC:
534 UNWIND_FONT_MAP_LOCK:
535 _cairo_ft_unscaled_font_map_unlock ();
540 #if CAIRO_HAS_FC_FONT
541 static cairo_status_t
542 _cairo_ft_unscaled_font_create_for_pattern (FcPattern *pattern,
543 cairo_ft_unscaled_font_t **out)
545 FT_Face font_face = NULL;
546 char *filename = NULL;
550 ret = FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &font_face);
551 if (ret == FcResultMatch)
553 if (ret == FcResultOutOfMemory)
554 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
556 ret = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **) &filename);
557 if (ret == FcResultOutOfMemory)
558 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
559 if (ret == FcResultMatch) {
560 if (access (filename, R_OK) == 0) {
561 /* If FC_INDEX is not set, we just use 0 */
562 ret = FcPatternGetInteger (pattern, FC_INDEX, 0, &id);
563 if (ret == FcResultOutOfMemory)
564 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
568 return _cairo_error (CAIRO_STATUS_FILE_NOT_FOUND);
571 /* The pattern contains neither a face nor a filename, resolve it later. */
573 return CAIRO_STATUS_SUCCESS;
576 return _cairo_ft_unscaled_font_create_internal (font_face != NULL,
577 filename, id, font_face,
582 static cairo_status_t
583 _cairo_ft_unscaled_font_create_from_face (FT_Face face,
584 cairo_ft_unscaled_font_t **out)
586 return _cairo_ft_unscaled_font_create_internal (TRUE, NULL, 0, face, out);
590 _cairo_ft_unscaled_font_destroy (void *abstract_font)
592 cairo_ft_unscaled_font_t *unscaled = abstract_font;
593 cairo_ft_unscaled_font_map_t *font_map;
595 if (unscaled == NULL)
598 font_map = _cairo_ft_unscaled_font_map_lock ();
599 /* All created objects must have been mapped in the font map. */
600 assert (font_map != NULL);
602 if (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&unscaled->base.ref_count)) {
603 /* somebody recreated the font whilst we waited for the lock */
604 _cairo_ft_unscaled_font_map_unlock ();
608 _cairo_hash_table_remove (font_map->hash_table,
609 &unscaled->base.hash_entry);
611 if (unscaled->from_face) {
612 /* See comments in _ft_font_face_destroy about the "zombie" state
613 * for a _ft_font_face.
615 if (unscaled->faces && unscaled->faces->unscaled == NULL) {
616 assert (unscaled->faces->next == NULL);
617 cairo_font_face_destroy (&unscaled->faces->base);
620 _font_map_release_face_lock_held (font_map, unscaled);
622 unscaled->face = NULL;
624 _cairo_ft_unscaled_font_map_unlock ();
626 _cairo_ft_unscaled_font_fini (unscaled);
630 _has_unlocked_face (const void *entry)
632 const cairo_ft_unscaled_font_t *unscaled = entry;
634 return (!unscaled->from_face && unscaled->lock_count == 0 && unscaled->face);
637 /* Ensures that an unscaled font has a face object. If we exceed
638 * MAX_OPEN_FACES, try to close some.
640 * This differs from _cairo_ft_scaled_font_lock_face in that it doesn't
641 * set the scale on the face, but just returns it at the last scale.
643 static cairo_warn FT_Face
644 _cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
646 cairo_ft_unscaled_font_map_t *font_map;
649 CAIRO_MUTEX_LOCK (unscaled->mutex);
650 unscaled->lock_count++;
653 return unscaled->face;
655 /* If this unscaled font was created from an FT_Face then we just
656 * returned it above. */
657 assert (!unscaled->from_face);
659 font_map = _cairo_ft_unscaled_font_map_lock ();
661 assert (font_map != NULL);
663 while (font_map->num_open_faces >= MAX_OPEN_FACES)
665 cairo_ft_unscaled_font_t *entry;
667 entry = _cairo_hash_table_random_entry (font_map->hash_table,
672 _font_map_release_face_lock_held (font_map, entry);
675 _cairo_ft_unscaled_font_map_unlock ();
677 if (FT_New_Face (font_map->ft_library,
682 unscaled->lock_count--;
683 CAIRO_MUTEX_UNLOCK (unscaled->mutex);
684 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
688 unscaled->face = face;
690 font_map->num_open_faces++;
696 /* Unlock unscaled font locked with _cairo_ft_unscaled_font_lock_face
699 _cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled)
701 assert (unscaled->lock_count > 0);
703 unscaled->lock_count--;
705 CAIRO_MUTEX_UNLOCK (unscaled->mutex);
709 static cairo_status_t
710 _compute_transform (cairo_ft_font_transform_t *sf,
711 cairo_matrix_t *scale)
713 cairo_status_t status;
714 double x_scale, y_scale;
715 cairo_matrix_t normalized = *scale;
717 /* The font matrix has x and y "scale" components which we extract and
718 * use as character scale values. These influence the way freetype
719 * chooses hints, as well as selecting different bitmaps in
720 * hand-rendered fonts. We also copy the normalized matrix to
721 * freetype's transformation.
724 status = _cairo_matrix_compute_basis_scale_factors (scale,
727 if (unlikely (status))
730 /* FreeType docs say this about x_scale and y_scale:
731 * "A character width or height smaller than 1pt is set to 1pt;"
732 * So, we cap them from below at 1.0 and let the FT transform
733 * take care of sub-1.0 scaling. */
739 sf->x_scale = x_scale;
740 sf->y_scale = y_scale;
742 cairo_matrix_scale (&normalized, 1.0 / x_scale, 1.0 / y_scale);
744 _cairo_matrix_get_affine (&normalized,
745 &sf->shape[0][0], &sf->shape[0][1],
746 &sf->shape[1][0], &sf->shape[1][1],
749 return CAIRO_STATUS_SUCCESS;
752 /* Temporarily scales an unscaled font to the give scale. We catch
753 * scaling to the same size, since changing a FT_Face is expensive.
755 static cairo_status_t
756 _cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
757 cairo_matrix_t *scale)
759 cairo_status_t status;
760 cairo_ft_font_transform_t sf;
764 assert (unscaled->face != NULL);
766 if (unscaled->have_scale &&
767 scale->xx == unscaled->current_scale.xx &&
768 scale->yx == unscaled->current_scale.yx &&
769 scale->xy == unscaled->current_scale.xy &&
770 scale->yy == unscaled->current_scale.yy)
771 return CAIRO_STATUS_SUCCESS;
773 unscaled->have_scale = TRUE;
774 unscaled->current_scale = *scale;
776 status = _compute_transform (&sf, scale);
777 if (unlikely (status))
780 unscaled->x_scale = sf.x_scale;
781 unscaled->y_scale = sf.y_scale;
783 mat.xx = DOUBLE_TO_16_16(sf.shape[0][0]);
784 mat.yx = - DOUBLE_TO_16_16(sf.shape[0][1]);
785 mat.xy = - DOUBLE_TO_16_16(sf.shape[1][0]);
786 mat.yy = DOUBLE_TO_16_16(sf.shape[1][1]);
788 unscaled->have_shape = (mat.xx != 0x10000 ||
793 unscaled->Current_Shape = mat;
794 cairo_matrix_init (&unscaled->current_shape,
795 sf.shape[0][0], sf.shape[0][1],
796 sf.shape[1][0], sf.shape[1][1],
799 FT_Set_Transform(unscaled->face, &mat, NULL);
801 if ((unscaled->face->face_flags & FT_FACE_FLAG_SCALABLE) != 0) {
802 error = FT_Set_Char_Size (unscaled->face,
803 sf.x_scale * 64.0 + .5,
804 sf.y_scale * 64.0 + .5,
807 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
809 double min_distance = DBL_MAX;
813 for (i = 0; i < unscaled->face->num_fixed_sizes; i++) {
814 #if HAVE_FT_BITMAP_SIZE_Y_PPEM
815 double size = unscaled->face->available_sizes[i].y_ppem / 64.;
817 double size = unscaled->face->available_sizes[i].height;
819 double distance = fabs (size - sf.y_scale);
821 if (distance <= min_distance) {
822 min_distance = distance;
826 #if HAVE_FT_BITMAP_SIZE_Y_PPEM
827 error = FT_Set_Char_Size (unscaled->face,
828 unscaled->face->available_sizes[best_i].x_ppem,
829 unscaled->face->available_sizes[best_i].y_ppem,
833 error = FT_Set_Pixel_Sizes (unscaled->face,
834 unscaled->face->available_sizes[best_i].width,
835 unscaled->face->available_sizes[best_i].height);
837 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
840 return CAIRO_STATUS_SUCCESS;
843 /* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
844 * into a different format. For example, we want to convert a
845 * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
846 * ARGB or ABGR bitmap.
848 * this function prepares a target descriptor for this operation.
850 * input :: target bitmap descriptor. The function will set its
851 * 'width', 'rows' and 'pitch' fields, and only these
853 * slot :: the glyph slot containing the source bitmap. this
854 * function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
856 * mode :: the requested final rendering mode. supported values are
857 * MONO, NORMAL (i.e. gray), LCD and LCD_V
859 * the function returns the size in bytes of the corresponding buffer,
860 * it's up to the caller to allocate the corresponding memory block
861 * before calling _fill_xrender_bitmap
863 * it also returns -1 in case of error (e.g. incompatible arguments,
864 * like trying to convert a gray bitmap into a monochrome one)
867 _compute_xrender_bitmap_size(FT_Bitmap *target,
872 int width, height, pitch;
874 if (slot->format != FT_GLYPH_FORMAT_BITMAP)
877 /* compute the size of the final bitmap */
878 ftbit = &slot->bitmap;
880 width = ftbit->width;
881 height = ftbit->rows;
882 pitch = (width + 3) & ~3;
884 switch (ftbit->pixel_mode) {
885 case FT_PIXEL_MODE_MONO:
886 if (mode == FT_RENDER_MODE_MONO) {
887 pitch = (((width + 31) & ~31) >> 3);
892 case FT_PIXEL_MODE_GRAY:
893 if (mode == FT_RENDER_MODE_LCD ||
894 mode == FT_RENDER_MODE_LCD_V)
896 /* each pixel is replicated into a 32-bit ARGB value */
901 case FT_PIXEL_MODE_LCD:
902 if (mode != FT_RENDER_MODE_LCD)
905 /* horz pixel triplets are packed into 32-bit ARGB values */
910 case FT_PIXEL_MODE_LCD_V:
911 if (mode != FT_RENDER_MODE_LCD_V)
914 /* vert pixel triplets are packed into 32-bit ARGB values */
919 default: /* unsupported source format */
923 target->width = width;
924 target->rows = height;
925 target->pitch = pitch;
926 target->buffer = NULL;
928 return pitch * height;
931 /* this functions converts the glyph bitmap found in a FT_GlyphSlot
932 * into a different format (see _compute_xrender_bitmap_size)
934 * you should call this function after _compute_xrender_bitmap_size
936 * target :: target bitmap descriptor. Note that its 'buffer' pointer
937 * must point to memory allocated by the caller
939 * slot :: the glyph slot containing the source bitmap
941 * mode :: the requested final rendering mode
943 * bgr :: boolean, set if BGR or VBGR pixel ordering is needed
946 _fill_xrender_bitmap(FT_Bitmap *target,
951 FT_Bitmap *ftbit = &slot->bitmap;
952 unsigned char *srcLine = ftbit->buffer;
953 unsigned char *dstLine = target->buffer;
954 int src_pitch = ftbit->pitch;
955 int width = target->width;
956 int height = target->rows;
957 int pitch = target->pitch;
961 subpixel = (mode == FT_RENDER_MODE_LCD ||
962 mode == FT_RENDER_MODE_LCD_V);
965 srcLine -= src_pitch * (ftbit->rows - 1);
967 target->pixel_mode = ftbit->pixel_mode;
969 switch (ftbit->pixel_mode) {
970 case FT_PIXEL_MODE_MONO:
972 /* convert mono to ARGB32 values */
974 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
977 for (x = 0; x < width; x++) {
978 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
979 ((unsigned int *) dstLine)[x] = 0xffffffffU;
982 target->pixel_mode = FT_PIXEL_MODE_LCD;
984 } else if (mode == FT_RENDER_MODE_NORMAL) {
985 /* convert mono to 8-bit gray */
987 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
990 for (x = 0; x < width; x++) {
991 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
995 target->pixel_mode = FT_PIXEL_MODE_GRAY;
998 /* copy mono to mono */
1000 int bytes = (width + 7) >> 3;
1002 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1003 memcpy (dstLine, srcLine, bytes);
1007 case FT_PIXEL_MODE_GRAY:
1009 /* convert gray to ARGB32 values */
1011 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1013 unsigned int *dst = (unsigned int *) dstLine;
1015 for (x = 0; x < width; x++) {
1016 unsigned int pix = srcLine[x];
1024 target->pixel_mode = FT_PIXEL_MODE_LCD;
1026 /* copy gray into gray */
1028 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1029 memcpy (dstLine, srcLine, width);
1033 case FT_PIXEL_MODE_LCD:
1035 /* convert horizontal RGB into ARGB32 */
1037 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1039 unsigned char *src = srcLine;
1040 unsigned int *dst = (unsigned int *) dstLine;
1042 for (x = 0; x < width; x++, src += 3) {
1045 pix = ((unsigned int)src[0] << 16) |
1046 ((unsigned int)src[1] << 8) |
1047 ((unsigned int)src[2] ) |
1048 ((unsigned int)src[1] << 24) ;
1054 /* convert horizontal BGR into ARGB32 */
1056 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1059 unsigned char *src = srcLine;
1060 unsigned int *dst = (unsigned int *) dstLine;
1062 for (x = 0; x < width; x++, src += 3) {
1065 pix = ((unsigned int)src[2] << 16) |
1066 ((unsigned int)src[1] << 8) |
1067 ((unsigned int)src[0] ) |
1068 ((unsigned int)src[1] << 24) ;
1076 default: /* FT_PIXEL_MODE_LCD_V */
1077 /* convert vertical RGB into ARGB32 */
1080 for (h = height; h > 0; h--, srcLine += 3 * src_pitch, dstLine += pitch) {
1082 unsigned char* src = srcLine;
1083 unsigned int* dst = (unsigned int *) dstLine;
1085 for (x = 0; x < width; x++, src += 1) {
1087 pix = ((unsigned int)src[0] << 16) |
1088 ((unsigned int)src[src_pitch] << 8) |
1089 ((unsigned int)src[src_pitch*2] ) |
1090 ((unsigned int)src[src_pitch] << 24) ;
1096 for (h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch) {
1098 unsigned char *src = srcLine;
1099 unsigned int *dst = (unsigned int *) dstLine;
1101 for (x = 0; x < width; x++, src += 1) {
1104 pix = ((unsigned int)src[src_pitch * 2] << 16) |
1105 ((unsigned int)src[src_pitch] << 8) |
1106 ((unsigned int)src[0] ) |
1107 ((unsigned int)src[src_pitch] << 24) ;
1117 /* Fills in val->image with an image surface created from @bitmap
1119 static cairo_status_t
1120 _get_bitmap_surface (FT_Bitmap *bitmap,
1121 cairo_bool_t own_buffer,
1122 cairo_font_options_t *font_options,
1123 cairo_image_surface_t **surface)
1125 int width, height, stride;
1126 unsigned char *data;
1127 int format = CAIRO_FORMAT_A8;
1128 cairo_image_surface_t *image;
1130 width = bitmap->width;
1131 height = bitmap->rows;
1133 if (width == 0 || height == 0) {
1134 *surface = (cairo_image_surface_t *)
1135 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1136 return (*surface)->base.status;
1139 switch (bitmap->pixel_mode) {
1140 case FT_PIXEL_MODE_MONO:
1141 stride = (((width + 31) & ~31) >> 3);
1143 data = bitmap->buffer;
1144 assert (stride == bitmap->pitch);
1146 data = _cairo_malloc_ab (height, stride);
1148 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1150 if (stride == bitmap->pitch) {
1151 memcpy (data, bitmap->buffer, stride * height);
1154 unsigned char *source, *dest;
1156 source = bitmap->buffer;
1158 for (i = height; i; i--) {
1159 memcpy (dest, source, bitmap->pitch);
1160 memset (dest + bitmap->pitch, '\0', stride - bitmap->pitch);
1162 source += bitmap->pitch;
1168 #ifndef WORDS_BIGENDIAN
1171 int count = stride * height;
1174 *d = CAIRO_BITSWAP8 (*d);
1179 format = CAIRO_FORMAT_A1;
1182 case FT_PIXEL_MODE_LCD:
1183 case FT_PIXEL_MODE_LCD_V:
1184 case FT_PIXEL_MODE_GRAY:
1185 if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
1186 stride = bitmap->pitch;
1188 data = bitmap->buffer;
1190 data = _cairo_malloc_ab (height, stride);
1192 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1194 memcpy (data, bitmap->buffer, stride * height);
1197 format = CAIRO_FORMAT_A8;
1199 /* if we get there, the data from the source bitmap
1200 * really comes from _fill_xrender_bitmap, and is
1201 * made of 32-bit ARGB or ABGR values */
1202 assert (own_buffer != 0);
1203 assert (bitmap->pixel_mode != FT_PIXEL_MODE_GRAY);
1205 data = bitmap->buffer;
1206 stride = bitmap->pitch;
1207 format = CAIRO_FORMAT_ARGB32;
1210 case FT_PIXEL_MODE_GRAY2:
1211 case FT_PIXEL_MODE_GRAY4:
1212 /* These could be triggered by very rare types of TrueType fonts */
1215 free (bitmap->buffer);
1216 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1220 *surface = image = (cairo_image_surface_t *)
1221 cairo_image_surface_create_for_data (data,
1223 width, height, stride);
1224 if (image->base.status) {
1226 return (*surface)->base.status;
1229 if (format == CAIRO_FORMAT_ARGB32)
1230 pixman_image_set_component_alpha (image->pixman_image, TRUE);
1232 _cairo_image_surface_assume_ownership_of_data (image);
1234 _cairo_debug_check_image_surface_is_defined (&image->base);
1236 return CAIRO_STATUS_SUCCESS;
1239 /* Converts an outline FT_GlyphSlot into an image
1241 * This could go through _render_glyph_bitmap as well, letting
1242 * FreeType convert the outline to a bitmap, but doing it ourselves
1243 * has two minor advantages: first, we save a copy of the bitmap
1244 * buffer: we can directly use the buffer that FreeType renders
1247 * Second, it may help when we add support for subpixel
1248 * rendering: the Xft code does it this way. (Keith thinks that
1249 * it may also be possible to get the subpixel rendering with
1250 * FT_Render_Glyph: something worth looking into in more detail
1251 * when we add subpixel support. If so, we may want to eliminate
1252 * this version of the code path entirely.
1254 static cairo_status_t
1255 _render_glyph_outline (FT_Face face,
1256 cairo_font_options_t *font_options,
1257 cairo_image_surface_t **surface)
1259 int rgba = FC_RGBA_UNKNOWN;
1260 int lcd_filter = FT_LCD_FILTER_LEGACY;
1261 FT_GlyphSlot glyphslot = face->glyph;
1262 FT_Outline *outline = &glyphslot->outline;
1265 unsigned int width, height;
1266 cairo_status_t status;
1268 FT_Library library = glyphslot->library;
1269 FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
1271 switch (font_options->antialias) {
1272 case CAIRO_ANTIALIAS_NONE:
1273 render_mode = FT_RENDER_MODE_MONO;
1276 case CAIRO_ANTIALIAS_SUBPIXEL:
1277 case CAIRO_ANTIALIAS_BEST:
1278 switch (font_options->subpixel_order) {
1279 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1280 case CAIRO_SUBPIXEL_ORDER_RGB:
1281 case CAIRO_SUBPIXEL_ORDER_BGR:
1282 render_mode = FT_RENDER_MODE_LCD;
1285 case CAIRO_SUBPIXEL_ORDER_VRGB:
1286 case CAIRO_SUBPIXEL_ORDER_VBGR:
1287 render_mode = FT_RENDER_MODE_LCD_V;
1291 switch (font_options->lcd_filter) {
1292 case CAIRO_LCD_FILTER_NONE:
1293 lcd_filter = FT_LCD_FILTER_NONE;
1295 case CAIRO_LCD_FILTER_DEFAULT:
1296 case CAIRO_LCD_FILTER_INTRA_PIXEL:
1297 lcd_filter = FT_LCD_FILTER_LEGACY;
1299 case CAIRO_LCD_FILTER_FIR3:
1300 lcd_filter = FT_LCD_FILTER_LIGHT;
1302 case CAIRO_LCD_FILTER_FIR5:
1303 lcd_filter = FT_LCD_FILTER_DEFAULT;
1309 case CAIRO_ANTIALIAS_DEFAULT:
1310 case CAIRO_ANTIALIAS_GRAY:
1311 case CAIRO_ANTIALIAS_GOOD:
1312 case CAIRO_ANTIALIAS_FAST:
1313 render_mode = FT_RENDER_MODE_NORMAL;
1316 FT_Outline_Get_CBox (outline, &cbox);
1320 cbox.xMax = (cbox.xMax + 63) & -64;
1321 cbox.yMax = (cbox.yMax + 63) & -64;
1323 width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
1324 height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
1326 if (width * height == 0) {
1327 cairo_format_t format;
1328 /* Looks like fb handles zero-sized images just fine */
1329 switch (render_mode) {
1330 case FT_RENDER_MODE_MONO:
1331 format = CAIRO_FORMAT_A1;
1333 case FT_RENDER_MODE_LCD:
1334 case FT_RENDER_MODE_LCD_V:
1335 format= CAIRO_FORMAT_ARGB32;
1337 case FT_RENDER_MODE_LIGHT:
1338 case FT_RENDER_MODE_NORMAL:
1339 case FT_RENDER_MODE_MAX:
1341 format = CAIRO_FORMAT_A8;
1345 (*surface) = (cairo_image_surface_t *)
1346 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1347 if ((*surface)->base.status)
1348 return (*surface)->base.status;
1353 switch (render_mode) {
1354 case FT_RENDER_MODE_LCD:
1355 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
1361 case FT_RENDER_MODE_LCD_V:
1362 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
1363 rgba = FC_RGBA_VBGR;
1365 rgba = FC_RGBA_VRGB;
1368 case FT_RENDER_MODE_MONO:
1369 case FT_RENDER_MODE_LIGHT:
1370 case FT_RENDER_MODE_NORMAL:
1371 case FT_RENDER_MODE_MAX:
1376 #if HAVE_FT_LIBRARY_SETLCDFILTER
1377 FT_Library_SetLcdFilter (library, lcd_filter);
1380 fterror = FT_Render_Glyph (face->glyph, render_mode);
1382 #if HAVE_FT_LIBRARY_SETLCDFILTER
1383 FT_Library_SetLcdFilter (library, FT_LCD_FILTER_NONE);
1387 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1389 bitmap_size = _compute_xrender_bitmap_size (&bitmap,
1392 if (bitmap_size < 0)
1393 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1395 bitmap.buffer = calloc (1, bitmap_size);
1396 if (bitmap.buffer == NULL)
1397 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1399 _fill_xrender_bitmap (&bitmap, face->glyph, render_mode,
1400 (rgba == FC_RGBA_BGR || rgba == FC_RGBA_VBGR));
1403 * _get_bitmap_surface will free bitmap.buffer if there is an error
1405 status = _get_bitmap_surface (&bitmap, TRUE, font_options, surface);
1406 if (unlikely (status))
1409 /* Note: the font's coordinate system is upside down from ours, so the
1410 * Y coordinate of the control box needs to be negated. Moreover, device
1411 * offsets are position of glyph origin relative to top left while xMin
1412 * and yMax are offsets of top left relative to origin. Another negation.
1414 cairo_surface_set_device_offset (&(*surface)->base,
1415 (double)-glyphslot->bitmap_left,
1416 (double)+glyphslot->bitmap_top);
1419 return CAIRO_STATUS_SUCCESS;
1422 /* Converts a bitmap (or other) FT_GlyphSlot into an image */
1423 static cairo_status_t
1424 _render_glyph_bitmap (FT_Face face,
1425 cairo_font_options_t *font_options,
1426 cairo_image_surface_t **surface)
1428 FT_GlyphSlot glyphslot = face->glyph;
1429 cairo_status_t status;
1432 /* According to the FreeType docs, glyphslot->format could be
1433 * something other than FT_GLYPH_FORMAT_OUTLINE or
1434 * FT_GLYPH_FORMAT_BITMAP. Calling FT_Render_Glyph gives FreeType
1435 * the opportunity to convert such to
1436 * bitmap. FT_GLYPH_FORMAT_COMPOSITE will not be encountered since
1437 * we avoid the FT_LOAD_NO_RECURSE flag.
1439 error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL);
1440 /* XXX ignoring all other errors for now. They are not fatal, typically
1441 * just a glyph-not-found. */
1442 if (error == FT_Err_Out_Of_Memory)
1443 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1445 status = _get_bitmap_surface (&glyphslot->bitmap,
1446 FALSE, font_options,
1448 if (unlikely (status))
1452 * Note: the font's coordinate system is upside down from ours, so the
1453 * Y coordinate of the control box needs to be negated. Moreover, device
1454 * offsets are position of glyph origin relative to top left while
1455 * bitmap_left and bitmap_top are offsets of top left relative to origin.
1458 cairo_surface_set_device_offset (&(*surface)->base,
1459 -glyphslot->bitmap_left,
1460 +glyphslot->bitmap_top);
1462 return CAIRO_STATUS_SUCCESS;
1465 static cairo_status_t
1466 _transform_glyph_bitmap (cairo_matrix_t * shape,
1467 cairo_image_surface_t ** surface)
1469 cairo_matrix_t original_to_transformed;
1470 cairo_matrix_t transformed_to_original;
1471 cairo_image_surface_t *old_image;
1472 cairo_surface_t *image;
1474 double origin_x, origin_y;
1475 int orig_width, orig_height;
1477 int x_min, y_min, x_max, y_max;
1479 cairo_status_t status;
1480 cairo_surface_pattern_t pattern;
1482 /* We want to compute a transform that takes the origin
1483 * (device_x_offset, device_y_offset) to 0,0, then applies
1484 * the "shape" portion of the font transform
1486 original_to_transformed = *shape;
1488 cairo_surface_get_device_offset (&(*surface)->base, &origin_x, &origin_y);
1489 orig_width = (*surface)->width;
1490 orig_height = (*surface)->height;
1492 cairo_matrix_translate (&original_to_transformed,
1493 -origin_x, -origin_y);
1495 /* Find the bounding box of the original bitmap under that
1499 x[1] = orig_width; y[1] = 0;
1500 x[2] = orig_width; y[2] = orig_height;
1501 x[3] = 0; y[3] = orig_height;
1503 for (i = 0; i < 4; i++)
1504 cairo_matrix_transform_point (&original_to_transformed,
1507 x_min = floor (x[0]); y_min = floor (y[0]);
1508 x_max = ceil (x[0]); y_max = ceil (y[0]);
1510 for (i = 1; i < 4; i++) {
1512 x_min = floor (x[i]);
1513 else if (x[i] > x_max)
1514 x_max = ceil (x[i]);
1516 y_min = floor (y[i]);
1517 else if (y[i] > y_max)
1518 y_max = ceil (y[i]);
1521 /* Adjust the transform so that the bounding box starts at 0,0 ...
1522 * this gives our final transform from original bitmap to transformed
1525 original_to_transformed.x0 -= x_min;
1526 original_to_transformed.y0 -= y_min;
1528 /* Create the transformed bitmap */
1529 width = x_max - x_min;
1530 height = y_max - y_min;
1532 transformed_to_original = original_to_transformed;
1533 status = cairo_matrix_invert (&transformed_to_original);
1534 if (unlikely (status))
1537 image = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
1538 if (unlikely (image->status))
1539 return image->status;
1541 /* Draw the original bitmap transformed into the new bitmap
1543 _cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
1544 cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
1546 status = _cairo_surface_paint (image,
1547 CAIRO_OPERATOR_SOURCE,
1551 _cairo_pattern_fini (&pattern.base);
1553 if (unlikely (status)) {
1554 cairo_surface_destroy (image);
1558 /* Now update the cache entry for the new bitmap, recomputing
1559 * the origin based on the final transform.
1561 cairo_matrix_transform_point (&original_to_transformed,
1562 &origin_x, &origin_y);
1564 old_image = (*surface);
1565 (*surface) = (cairo_image_surface_t *)image;
1566 cairo_surface_destroy (&old_image->base);
1568 cairo_surface_set_device_offset (&(*surface)->base,
1569 _cairo_lround (origin_x),
1570 _cairo_lround (origin_y));
1571 return CAIRO_STATUS_SUCCESS;
1574 static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend = {
1575 _cairo_ft_unscaled_font_destroy,
1577 _cairo_ft_unscaled_font_create_glyph
1581 /* #cairo_ft_scaled_font_t */
1583 typedef struct _cairo_ft_scaled_font {
1584 cairo_scaled_font_t base;
1585 cairo_ft_unscaled_font_t *unscaled;
1586 cairo_ft_options_t ft_options;
1587 } cairo_ft_scaled_font_t;
1589 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend;
1591 #if CAIRO_HAS_FC_FONT
1592 /* The load flags passed to FT_Load_Glyph control aspects like hinting and
1593 * antialiasing. Here we compute them from the fields of a FcPattern.
1596 _get_pattern_ft_options (FcPattern *pattern, cairo_ft_options_t *ret)
1598 FcBool antialias, vertical_layout, hinting, autohint, bitmap, embolden;
1599 cairo_ft_options_t ft_options;
1601 #ifdef FC_HINT_STYLE
1605 _cairo_font_options_init_default (&ft_options.base);
1606 ft_options.load_flags = FT_LOAD_DEFAULT;
1607 ft_options.synth_flags = 0;
1609 #ifndef FC_EMBEDDED_BITMAP
1610 #define FC_EMBEDDED_BITMAP "embeddedbitmap"
1613 /* Check whether to force use of embedded bitmaps */
1614 if (FcPatternGetBool (pattern,
1615 FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
1618 /* disable antialiasing if requested */
1619 if (FcPatternGetBool (pattern,
1620 FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
1624 cairo_subpixel_order_t subpixel_order;
1627 /* disable hinting if requested */
1628 if (FcPatternGetBool (pattern,
1629 FC_HINTING, 0, &hinting) != FcResultMatch)
1632 if (FcPatternGetInteger (pattern,
1633 FC_RGBA, 0, &rgba) != FcResultMatch)
1634 rgba = FC_RGBA_UNKNOWN;
1638 subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
1641 subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
1644 subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
1647 subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
1649 case FC_RGBA_UNKNOWN:
1652 subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1656 if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT) {
1657 ft_options.base.subpixel_order = subpixel_order;
1658 ft_options.base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1661 if (FcPatternGetInteger (pattern,
1662 FC_LCD_FILTER, 0, &lcd_filter) == FcResultMatch)
1664 switch (lcd_filter) {
1666 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1668 case FC_LCD_DEFAULT:
1669 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR5;
1672 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR3;
1675 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_INTRA_PIXEL;
1680 #ifdef FC_HINT_STYLE
1681 if (FcPatternGetInteger (pattern,
1682 FC_HINT_STYLE, 0, &hintstyle) != FcResultMatch)
1683 hintstyle = FC_HINT_FULL;
1686 hintstyle = FC_HINT_NONE;
1688 switch (hintstyle) {
1690 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1692 case FC_HINT_SLIGHT:
1693 ft_options.base.hint_style = CAIRO_HINT_STYLE_SLIGHT;
1695 case FC_HINT_MEDIUM:
1697 ft_options.base.hint_style = CAIRO_HINT_STYLE_MEDIUM;
1700 ft_options.base.hint_style = CAIRO_HINT_STYLE_FULL;
1703 #else /* !FC_HINT_STYLE */
1705 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1707 #endif /* FC_HINT_STYLE */
1709 /* Force embedded bitmaps off if no hinting requested */
1710 if (ft_options.base.hint_style == CAIRO_HINT_STYLE_NONE)
1714 ft_options.load_flags |= FT_LOAD_NO_BITMAP;
1717 ft_options.base.antialias = CAIRO_ANTIALIAS_NONE;
1720 /* force autohinting if requested */
1721 if (FcPatternGetBool (pattern,
1722 FC_AUTOHINT, 0, &autohint) != FcResultMatch)
1726 ft_options.load_flags |= FT_LOAD_FORCE_AUTOHINT;
1728 if (FcPatternGetBool (pattern,
1729 FC_VERTICAL_LAYOUT, 0, &vertical_layout) != FcResultMatch)
1730 vertical_layout = FcFalse;
1732 if (vertical_layout)
1733 ft_options.load_flags |= FT_LOAD_VERTICAL_LAYOUT;
1736 #define FC_EMBOLDEN "embolden"
1738 if (FcPatternGetBool (pattern,
1739 FC_EMBOLDEN, 0, &embolden) != FcResultMatch)
1743 ft_options.synth_flags |= CAIRO_FT_SYNTHESIZE_BOLD;
1750 _cairo_ft_options_merge (cairo_ft_options_t *options,
1751 cairo_ft_options_t *other)
1753 int load_flags = other->load_flags;
1754 int load_target = FT_LOAD_TARGET_NORMAL;
1756 /* clear load target mode */
1757 load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(other->load_flags)));
1759 if (load_flags & FT_LOAD_NO_HINTING)
1760 other->base.hint_style = CAIRO_HINT_STYLE_NONE;
1762 if (other->base.antialias == CAIRO_ANTIALIAS_NONE ||
1763 options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1764 options->base.antialias = CAIRO_ANTIALIAS_NONE;
1765 options->base.subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1768 if (other->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL &&
1769 (options->base.antialias == CAIRO_ANTIALIAS_DEFAULT ||
1770 options->base.antialias == CAIRO_ANTIALIAS_GRAY)) {
1771 options->base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1772 options->base.subpixel_order = other->base.subpixel_order;
1775 if (options->base.hint_style == CAIRO_HINT_STYLE_DEFAULT)
1776 options->base.hint_style = other->base.hint_style;
1778 if (other->base.hint_style == CAIRO_HINT_STYLE_NONE)
1779 options->base.hint_style = CAIRO_HINT_STYLE_NONE;
1781 if (options->base.lcd_filter == CAIRO_LCD_FILTER_DEFAULT)
1782 options->base.lcd_filter = other->base.lcd_filter;
1784 if (other->base.lcd_filter == CAIRO_LCD_FILTER_NONE)
1785 options->base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1787 if (options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1788 if (options->base.hint_style == CAIRO_HINT_STYLE_NONE)
1789 load_flags |= FT_LOAD_NO_HINTING;
1791 load_target = FT_LOAD_TARGET_MONO;
1792 load_flags |= FT_LOAD_MONOCHROME;
1794 switch (options->base.hint_style) {
1795 case CAIRO_HINT_STYLE_NONE:
1796 load_flags |= FT_LOAD_NO_HINTING;
1798 case CAIRO_HINT_STYLE_SLIGHT:
1799 load_target = FT_LOAD_TARGET_LIGHT;
1801 case CAIRO_HINT_STYLE_MEDIUM:
1803 case CAIRO_HINT_STYLE_FULL:
1804 case CAIRO_HINT_STYLE_DEFAULT:
1805 if (options->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
1806 switch (options->base.subpixel_order) {
1807 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1808 case CAIRO_SUBPIXEL_ORDER_RGB:
1809 case CAIRO_SUBPIXEL_ORDER_BGR:
1810 load_target = FT_LOAD_TARGET_LCD;
1812 case CAIRO_SUBPIXEL_ORDER_VRGB:
1813 case CAIRO_SUBPIXEL_ORDER_VBGR:
1814 load_target = FT_LOAD_TARGET_LCD_V;
1822 options->load_flags = load_flags | load_target;
1823 options->synth_flags = other->synth_flags;
1826 static cairo_status_t
1827 _cairo_ft_font_face_scaled_font_create (void *abstract_font_face,
1828 const cairo_matrix_t *font_matrix,
1829 const cairo_matrix_t *ctm,
1830 const cairo_font_options_t *options,
1831 cairo_scaled_font_t **font_out)
1833 cairo_ft_font_face_t *font_face = abstract_font_face;
1834 cairo_ft_scaled_font_t *scaled_font;
1836 FT_Size_Metrics *metrics;
1837 cairo_font_extents_t fs_metrics;
1838 cairo_status_t status;
1839 cairo_ft_unscaled_font_t *unscaled;
1841 assert (font_face->unscaled);
1843 face = _cairo_ft_unscaled_font_lock_face (font_face->unscaled);
1844 if (unlikely (face == NULL)) /* backend error */
1845 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1847 scaled_font = malloc (sizeof (cairo_ft_scaled_font_t));
1848 if (unlikely (scaled_font == NULL)) {
1849 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1853 scaled_font->unscaled = unscaled = font_face->unscaled;
1854 _cairo_unscaled_font_reference (&unscaled->base);
1856 _cairo_font_options_init_copy (&scaled_font->ft_options.base, options);
1857 _cairo_ft_options_merge (&scaled_font->ft_options, &font_face->ft_options);
1859 status = _cairo_scaled_font_init (&scaled_font->base,
1861 font_matrix, ctm, options,
1862 &_cairo_ft_scaled_font_backend);
1863 if (unlikely (status))
1864 goto CLEANUP_SCALED_FONT;
1866 status = _cairo_ft_unscaled_font_set_scale (unscaled,
1867 &scaled_font->base.scale);
1868 if (unlikely (status)) {
1869 /* This can only fail if we encounter an error with the underlying
1870 * font, so propagate the error back to the font-face. */
1871 _cairo_ft_unscaled_font_unlock_face (unscaled);
1872 _cairo_unscaled_font_destroy (&unscaled->base);
1878 metrics = &face->size->metrics;
1881 * Get to unscaled metrics so that the upper level can get back to
1884 * Also use this path for bitmap-only fonts. The other branch uses
1885 * face members that are only relevant for scalable fonts. This is
1886 * detected by simply checking for units_per_EM==0.
1888 if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
1889 face->units_per_EM == 0) {
1890 double x_factor, y_factor;
1892 if (unscaled->x_scale == 0)
1895 x_factor = 1 / unscaled->x_scale;
1897 if (unscaled->y_scale == 0)
1900 y_factor = 1 / unscaled->y_scale;
1902 fs_metrics.ascent = DOUBLE_FROM_26_6(metrics->ascender) * y_factor;
1903 fs_metrics.descent = DOUBLE_FROM_26_6(- metrics->descender) * y_factor;
1904 fs_metrics.height = DOUBLE_FROM_26_6(metrics->height) * y_factor;
1905 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
1906 fs_metrics.max_x_advance = DOUBLE_FROM_26_6(metrics->max_advance) * x_factor;
1907 fs_metrics.max_y_advance = 0;
1909 fs_metrics.max_x_advance = 0;
1910 fs_metrics.max_y_advance = DOUBLE_FROM_26_6(metrics->max_advance) * y_factor;
1913 double scale = face->units_per_EM;
1915 fs_metrics.ascent = face->ascender / scale;
1916 fs_metrics.descent = - face->descender / scale;
1917 fs_metrics.height = face->height / scale;
1918 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
1919 fs_metrics.max_x_advance = face->max_advance_width / scale;
1920 fs_metrics.max_y_advance = 0;
1922 fs_metrics.max_x_advance = 0;
1923 fs_metrics.max_y_advance = face->max_advance_height / scale;
1927 status = _cairo_scaled_font_set_metrics (&scaled_font->base, &fs_metrics);
1928 if (unlikely (status))
1929 goto CLEANUP_SCALED_FONT;
1931 _cairo_ft_unscaled_font_unlock_face (unscaled);
1933 *font_out = &scaled_font->base;
1934 return CAIRO_STATUS_SUCCESS;
1936 CLEANUP_SCALED_FONT:
1937 _cairo_unscaled_font_destroy (&unscaled->base);
1940 _cairo_ft_unscaled_font_unlock_face (font_face->unscaled);
1941 *font_out = _cairo_scaled_font_create_in_error (status);
1942 return CAIRO_STATUS_SUCCESS; /* non-backend error */
1946 _cairo_scaled_font_is_ft (cairo_scaled_font_t *scaled_font)
1948 return scaled_font->backend == &_cairo_ft_scaled_font_backend;
1952 _cairo_ft_scaled_font_fini (void *abstract_font)
1954 cairo_ft_scaled_font_t *scaled_font = abstract_font;
1956 if (scaled_font == NULL)
1959 _cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
1963 _move_to (FT_Vector *to, void *closure)
1965 cairo_path_fixed_t *path = closure;
1968 x = _cairo_fixed_from_26_6 (to->x);
1969 y = _cairo_fixed_from_26_6 (to->y);
1971 if (_cairo_path_fixed_close_path (path) != CAIRO_STATUS_SUCCESS)
1973 if (_cairo_path_fixed_move_to (path, x, y) != CAIRO_STATUS_SUCCESS)
1980 _line_to (FT_Vector *to, void *closure)
1982 cairo_path_fixed_t *path = closure;
1985 x = _cairo_fixed_from_26_6 (to->x);
1986 y = _cairo_fixed_from_26_6 (to->y);
1988 if (_cairo_path_fixed_line_to (path, x, y) != CAIRO_STATUS_SUCCESS)
1995 _conic_to (FT_Vector *control, FT_Vector *to, void *closure)
1997 cairo_path_fixed_t *path = closure;
1999 cairo_fixed_t x0, y0;
2000 cairo_fixed_t x1, y1;
2001 cairo_fixed_t x2, y2;
2002 cairo_fixed_t x3, y3;
2003 cairo_point_t conic;
2005 if (! _cairo_path_fixed_get_current_point (path, &x0, &y0))
2008 conic.x = _cairo_fixed_from_26_6 (control->x);
2009 conic.y = _cairo_fixed_from_26_6 (control->y);
2011 x3 = _cairo_fixed_from_26_6 (to->x);
2012 y3 = _cairo_fixed_from_26_6 (to->y);
2014 x1 = x0 + 2.0/3.0 * (conic.x - x0);
2015 y1 = y0 + 2.0/3.0 * (conic.y - y0);
2017 x2 = x3 + 2.0/3.0 * (conic.x - x3);
2018 y2 = y3 + 2.0/3.0 * (conic.y - y3);
2020 if (_cairo_path_fixed_curve_to (path,
2023 x3, y3) != CAIRO_STATUS_SUCCESS)
2030 _cubic_to (FT_Vector *control1, FT_Vector *control2,
2031 FT_Vector *to, void *closure)
2033 cairo_path_fixed_t *path = closure;
2034 cairo_fixed_t x0, y0;
2035 cairo_fixed_t x1, y1;
2036 cairo_fixed_t x2, y2;
2038 x0 = _cairo_fixed_from_26_6 (control1->x);
2039 y0 = _cairo_fixed_from_26_6 (control1->y);
2041 x1 = _cairo_fixed_from_26_6 (control2->x);
2042 y1 = _cairo_fixed_from_26_6 (control2->y);
2044 x2 = _cairo_fixed_from_26_6 (to->x);
2045 y2 = _cairo_fixed_from_26_6 (to->y);
2047 if (_cairo_path_fixed_curve_to (path,
2050 x2, y2) != CAIRO_STATUS_SUCCESS)
2056 static cairo_status_t
2057 _decompose_glyph_outline (FT_Face face,
2058 cairo_font_options_t *options,
2059 cairo_path_fixed_t **pathp)
2061 static const FT_Outline_Funcs outline_funcs = {
2062 (FT_Outline_MoveToFunc)_move_to,
2063 (FT_Outline_LineToFunc)_line_to,
2064 (FT_Outline_ConicToFunc)_conic_to,
2065 (FT_Outline_CubicToFunc)_cubic_to,
2069 static const FT_Matrix invert_y = {
2070 DOUBLE_TO_16_16 (1.0), 0,
2071 0, DOUBLE_TO_16_16 (-1.0),
2075 cairo_path_fixed_t *path;
2076 cairo_status_t status;
2078 path = _cairo_path_fixed_create ();
2080 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2082 glyph = face->glyph;
2084 /* Font glyphs have an inverted Y axis compared to cairo. */
2085 FT_Outline_Transform (&glyph->outline, &invert_y);
2086 if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
2087 _cairo_path_fixed_destroy (path);
2088 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2091 status = _cairo_path_fixed_close_path (path);
2092 if (unlikely (status)) {
2093 _cairo_path_fixed_destroy (path);
2099 return CAIRO_STATUS_SUCCESS;
2103 * Translate glyph to match its metrics.
2106 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (void *abstract_font,
2109 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2112 vector.x = glyph->metrics.vertBearingX - glyph->metrics.horiBearingX;
2113 vector.y = -glyph->metrics.vertBearingY - glyph->metrics.horiBearingY;
2115 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2116 FT_Vector_Transform (&vector, &scaled_font->unscaled->Current_Shape);
2117 FT_Outline_Translate(&glyph->outline, vector.x, vector.y);
2118 } else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
2119 glyph->bitmap_left += vector.x / 64;
2120 glyph->bitmap_top += vector.y / 64;
2124 static cairo_int_status_t
2125 _cairo_ft_scaled_glyph_init (void *abstract_font,
2126 cairo_scaled_glyph_t *scaled_glyph,
2127 cairo_scaled_glyph_info_t info)
2129 cairo_text_extents_t fs_metrics;
2130 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2131 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2135 int load_flags = scaled_font->ft_options.load_flags;
2136 FT_Glyph_Metrics *metrics;
2137 double x_factor, y_factor;
2138 cairo_bool_t vertical_layout = FALSE;
2139 cairo_status_t status;
2141 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2143 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2145 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
2146 &scaled_font->base.scale);
2147 if (unlikely (status))
2150 /* Ignore global advance unconditionally */
2151 load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
2153 if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0 &&
2154 (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) == 0)
2155 load_flags |= FT_LOAD_NO_BITMAP;
2158 * Don't pass FT_LOAD_VERTICAL_LAYOUT to FT_Load_Glyph here as
2159 * suggested by freetype people.
2161 if (load_flags & FT_LOAD_VERTICAL_LAYOUT) {
2162 load_flags &= ~FT_LOAD_VERTICAL_LAYOUT;
2163 vertical_layout = TRUE;
2166 error = FT_Load_Glyph (face,
2167 _cairo_scaled_glyph_index(scaled_glyph),
2169 /* XXX ignoring all other errors for now. They are not fatal, typically
2170 * just a glyph-not-found. */
2171 if (error == FT_Err_Out_Of_Memory) {
2172 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2176 glyph = face->glyph;
2179 * synthesize glyphs if requested
2181 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
2182 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2183 FT_GlyphSlot_Embolden (glyph);
2186 #if HAVE_FT_GLYPHSLOT_OBLIQUE
2187 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2188 FT_GlyphSlot_Oblique (glyph);
2191 if (vertical_layout)
2192 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, glyph);
2194 if (info & CAIRO_SCALED_GLYPH_INFO_METRICS) {
2196 cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
2198 * Compute font-space metrics
2200 metrics = &glyph->metrics;
2202 if (unscaled->x_scale == 0)
2205 x_factor = 1 / unscaled->x_scale;
2207 if (unscaled->y_scale == 0)
2210 y_factor = 1 / unscaled->y_scale;
2213 * Note: Y coordinates of the horizontal bearing need to be negated.
2215 * Scale metrics back to glyph space from the scaled glyph space returned
2218 * If we want hinted metrics but aren't asking for hinted glyphs from
2219 * FreeType, then we need to do the metric hinting ourselves.
2222 if (hint_metrics && (load_flags & FT_LOAD_NO_HINTING))
2228 if (!vertical_layout) {
2229 x1 = (metrics->horiBearingX) & -64;
2230 x2 = (metrics->horiBearingX + metrics->width + 63) & -64;
2231 y1 = (-metrics->horiBearingY) & -64;
2232 y2 = (-metrics->horiBearingY + metrics->height + 63) & -64;
2234 advance = ((metrics->horiAdvance + 32) & -64);
2236 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2237 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2239 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2240 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2242 fs_metrics.x_advance = DOUBLE_FROM_26_6 (advance) * x_factor;
2243 fs_metrics.y_advance = 0;
2245 x1 = (metrics->vertBearingX) & -64;
2246 x2 = (metrics->vertBearingX + metrics->width + 63) & -64;
2247 y1 = (metrics->vertBearingY) & -64;
2248 y2 = (metrics->vertBearingY + metrics->height + 63) & -64;
2250 advance = ((metrics->vertAdvance + 32) & -64);
2252 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2253 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2255 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2256 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2258 fs_metrics.x_advance = 0;
2259 fs_metrics.y_advance = DOUBLE_FROM_26_6 (advance) * y_factor;
2262 fs_metrics.width = DOUBLE_FROM_26_6 (metrics->width) * x_factor;
2263 fs_metrics.height = DOUBLE_FROM_26_6 (metrics->height) * y_factor;
2265 if (!vertical_layout) {
2266 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->horiBearingX) * x_factor;
2267 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (-metrics->horiBearingY) * y_factor;
2269 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2270 fs_metrics.x_advance = DOUBLE_FROM_26_6 (metrics->horiAdvance) * x_factor;
2272 fs_metrics.x_advance = DOUBLE_FROM_16_16 (glyph->linearHoriAdvance) * x_factor;
2273 fs_metrics.y_advance = 0 * y_factor;
2275 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingX) * x_factor;
2276 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingY) * y_factor;
2278 fs_metrics.x_advance = 0 * x_factor;
2279 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2280 fs_metrics.y_advance = DOUBLE_FROM_26_6 (metrics->vertAdvance) * y_factor;
2282 fs_metrics.y_advance = DOUBLE_FROM_16_16 (glyph->linearVertAdvance) * y_factor;
2286 _cairo_scaled_glyph_set_metrics (scaled_glyph,
2291 if ((info & CAIRO_SCALED_GLYPH_INFO_SURFACE) != 0) {
2292 cairo_image_surface_t *surface;
2294 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2295 status = _render_glyph_outline (face, &scaled_font->ft_options.base,
2298 status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
2300 if (likely (status == CAIRO_STATUS_SUCCESS) &&
2301 unscaled->have_shape)
2303 status = _transform_glyph_bitmap (&unscaled->current_shape,
2305 if (unlikely (status))
2306 cairo_surface_destroy (&surface->base);
2309 if (unlikely (status))
2312 _cairo_scaled_glyph_set_surface (scaled_glyph,
2317 if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
2318 cairo_path_fixed_t *path = NULL; /* hide compiler warning */
2321 * A kludge -- the above code will trash the outline,
2322 * so reload it. This will probably never occur though
2324 if ((info & CAIRO_SCALED_GLYPH_INFO_SURFACE) != 0) {
2325 error = FT_Load_Glyph (face,
2326 _cairo_scaled_glyph_index(scaled_glyph),
2327 load_flags | FT_LOAD_NO_BITMAP);
2328 /* XXX ignoring all other errors for now. They are not fatal, typically
2329 * just a glyph-not-found. */
2330 if (error == FT_Err_Out_Of_Memory) {
2331 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2334 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
2335 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2336 FT_GlyphSlot_Embolden (glyph);
2338 #if HAVE_FT_GLYPHSLOT_OBLIQUE
2339 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2340 FT_GlyphSlot_Oblique (glyph);
2342 if (vertical_layout)
2343 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, glyph);
2346 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
2347 status = _decompose_glyph_outline (face, &scaled_font->ft_options.base,
2350 status = CAIRO_INT_STATUS_UNSUPPORTED;
2352 if (unlikely (status))
2355 _cairo_scaled_glyph_set_path (scaled_glyph,
2360 _cairo_ft_unscaled_font_unlock_face (unscaled);
2365 static unsigned long
2366 _cairo_ft_ucs4_to_index (void *abstract_font,
2369 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2370 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2374 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2378 #if CAIRO_HAS_FC_FONT
2379 index = FcFreeTypeCharIndex (face, ucs4);
2381 index = FT_Get_Char_Index (face, ucs4);
2384 _cairo_ft_unscaled_font_unlock_face (unscaled);
2388 static cairo_int_status_t
2389 _cairo_ft_load_truetype_table (void *abstract_font,
2392 unsigned char *buffer,
2393 unsigned long *length)
2395 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2396 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2398 cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
2400 /* We don't support the FreeType feature of loading a table
2401 * without specifying the size since this may overflow our
2403 assert (length != NULL);
2405 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
2406 return CAIRO_INT_STATUS_UNSUPPORTED;
2408 #if HAVE_FT_LOAD_SFNT_TABLE
2409 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2411 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2413 if (FT_IS_SFNT (face)) {
2417 if (FT_Load_Sfnt_Table (face, tag, offset, buffer, length) == 0)
2418 status = CAIRO_STATUS_SUCCESS;
2421 _cairo_ft_unscaled_font_unlock_face (unscaled);
2427 static cairo_int_status_t
2428 _cairo_ft_index_to_ucs4(void *abstract_font,
2429 unsigned long index,
2432 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2433 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2438 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2440 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2442 *ucs4 = (uint32_t) -1;
2443 charcode = FT_Get_First_Char(face, &gindex);
2444 while (gindex != 0) {
2445 if (gindex == index) {
2449 charcode = FT_Get_Next_Char (face, charcode, &gindex);
2452 _cairo_ft_unscaled_font_unlock_face (unscaled);
2454 return CAIRO_STATUS_SUCCESS;
2458 _cairo_ft_is_synthetic (void *abstract_font)
2460 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2461 return scaled_font->ft_options.synth_flags != 0;
2464 static cairo_int_status_t
2465 _cairo_index_to_glyph_name (void *abstract_font,
2467 int num_glyph_names,
2468 unsigned long glyph_index,
2469 unsigned long *glyph_array_index)
2471 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2472 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2474 char buffer[256]; /* PLRM spcifies max name length of 127 */
2478 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2480 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2482 error = FT_Get_Glyph_Name (face, glyph_index, buffer, sizeof buffer);
2484 _cairo_ft_unscaled_font_unlock_face (unscaled);
2486 if (error != FT_Err_Ok) {
2487 /* propagate fatal errors from FreeType */
2488 if (error == FT_Err_Out_Of_Memory)
2489 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2491 return CAIRO_INT_STATUS_UNSUPPORTED;
2494 /* FT first numbers the glyphs in the order they are read from the
2495 * Type 1 font. Then if .notdef is not the first glyph, the first
2496 * glyph is swapped with .notdef to ensure that .notdef is at
2499 * As all but two glyphs in glyph_names already have the same
2500 * index as the FT glyph index, we first check if
2501 * glyph_names[glyph_index] is the name we are looking for. If not
2502 * we fall back to searching the entire array.
2505 if ((long)glyph_index < num_glyph_names &&
2506 strcmp (glyph_names[glyph_index], buffer) == 0)
2508 *glyph_array_index = glyph_index;
2510 return CAIRO_STATUS_SUCCESS;
2513 for (i = 0; i < num_glyph_names; i++) {
2514 if (strcmp (glyph_names[i], buffer) == 0) {
2515 *glyph_array_index = i;
2517 return CAIRO_STATUS_SUCCESS;
2521 return CAIRO_INT_STATUS_UNSUPPORTED;
2525 _ft_is_type1 (FT_Face face)
2527 #if HAVE_FT_GET_X11_FONT_FORMAT
2528 const char *font_format = FT_Get_X11_Font_Format (face);
2530 (strcmp (font_format, "Type 1") == 0 ||
2531 strcmp (font_format, "CFF") == 0))
2540 static cairo_int_status_t
2541 _cairo_ft_load_type1_data (void *abstract_font,
2543 unsigned char *buffer,
2544 unsigned long *length)
2546 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2547 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2549 cairo_status_t status = CAIRO_STATUS_SUCCESS;
2550 unsigned long available_length;
2553 assert (length != NULL);
2555 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
2556 return CAIRO_INT_STATUS_UNSUPPORTED;
2558 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2560 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2562 #if HAVE_FT_LOAD_SFNT_TABLE
2563 if (FT_IS_SFNT (face)) {
2564 status = CAIRO_INT_STATUS_UNSUPPORTED;
2569 if (! _ft_is_type1 (face)) {
2570 status = CAIRO_INT_STATUS_UNSUPPORTED;
2574 available_length = MAX (face->stream->size - offset, 0);
2576 *length = available_length;
2578 if (*length > available_length) {
2579 status = CAIRO_INT_STATUS_UNSUPPORTED;
2580 } else if (face->stream->read != NULL) {
2581 /* Note that read() may be implemented as a macro, thanks POSIX!, so we
2582 * need to wrap the following usage in parentheses in order to
2583 * disambiguate it for the pre-processor - using the verbose function
2584 * pointer dereference for clarity.
2586 ret = (* face->stream->read) (face->stream,
2591 status = _cairo_error (CAIRO_STATUS_READ_ERROR);
2593 memcpy (buffer, face->stream->base + offset, *length);
2598 _cairo_ft_unscaled_font_unlock_face (unscaled);
2603 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend = {
2605 _cairo_ft_scaled_font_fini,
2606 _cairo_ft_scaled_glyph_init,
2607 NULL, /* text_to_glyphs */
2608 _cairo_ft_ucs4_to_index,
2609 _cairo_ft_load_truetype_table,
2610 _cairo_ft_index_to_ucs4,
2611 _cairo_ft_is_synthetic,
2612 _cairo_index_to_glyph_name,
2613 _cairo_ft_load_type1_data
2616 /* #cairo_ft_font_face_t */
2618 #if CAIRO_HAS_FC_FONT
2619 static cairo_font_face_t *
2620 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern);
2622 static cairo_status_t
2623 _cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
2624 cairo_font_face_t **font_face_out)
2626 cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
2631 pattern = FcPatternCreate ();
2633 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2634 return font_face->status;
2637 if (!FcPatternAddString (pattern,
2638 FC_FAMILY, (unsigned char *) toy_face->family))
2640 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2644 switch (toy_face->slant)
2646 case CAIRO_FONT_SLANT_ITALIC:
2647 fcslant = FC_SLANT_ITALIC;
2649 case CAIRO_FONT_SLANT_OBLIQUE:
2650 fcslant = FC_SLANT_OBLIQUE;
2652 case CAIRO_FONT_SLANT_NORMAL:
2654 fcslant = FC_SLANT_ROMAN;
2658 if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
2659 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2663 switch (toy_face->weight)
2665 case CAIRO_FONT_WEIGHT_BOLD:
2666 fcweight = FC_WEIGHT_BOLD;
2668 case CAIRO_FONT_WEIGHT_NORMAL:
2670 fcweight = FC_WEIGHT_MEDIUM;
2674 if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
2675 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2679 font_face = _cairo_ft_font_face_create_for_pattern (pattern);
2682 FcPatternDestroy (pattern);
2684 *font_face_out = font_face;
2685 return font_face->status;
2690 _cairo_ft_font_face_destroy (void *abstract_face)
2692 cairo_ft_font_face_t *font_face = abstract_face;
2694 /* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
2695 * we have a special "zombie" state for the face when the unscaled font
2696 * is still alive but there are no other references to a font face with
2701 * font_face ------> unscaled
2706 * font_face <------- unscaled
2709 if (font_face->unscaled &&
2710 font_face->unscaled->from_face &&
2711 font_face->next == NULL &&
2712 font_face->unscaled->faces == font_face &&
2713 CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->unscaled->base.ref_count) > 1)
2715 cairo_font_face_reference (&font_face->base);
2717 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
2718 font_face->unscaled = NULL;
2723 if (font_face->unscaled) {
2724 cairo_ft_font_face_t *tmp_face = NULL;
2725 cairo_ft_font_face_t *last_face = NULL;
2727 /* Remove face from linked list */
2728 for (tmp_face = font_face->unscaled->faces;
2730 tmp_face = tmp_face->next)
2732 if (tmp_face == font_face) {
2734 last_face->next = tmp_face->next;
2736 font_face->unscaled->faces = tmp_face->next;
2739 last_face = tmp_face;
2742 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
2743 font_face->unscaled = NULL;
2746 #if CAIRO_HAS_FC_FONT
2747 if (font_face->pattern) {
2748 FcPatternDestroy (font_face->pattern);
2749 cairo_font_face_destroy (font_face->resolved_font_face);
2754 static cairo_font_face_t *
2755 _cairo_ft_font_face_get_implementation (void *abstract_face,
2756 const cairo_matrix_t *font_matrix,
2757 const cairo_matrix_t *ctm,
2758 const cairo_font_options_t *options)
2760 cairo_ft_font_face_t *font_face = abstract_face;
2762 /* The handling of font options is different depending on how the
2763 * font face was created. When the user creates a font face with
2764 * cairo_ft_font_face_create_for_ft_face(), then the load flags
2765 * passed in augment the load flags for the options. But for
2766 * cairo_ft_font_face_create_for_pattern(), the load flags are
2767 * derived from a pattern where the user has called
2768 * cairo_ft_font_options_substitute(), so *just* use those load
2769 * flags and ignore the options.
2772 #if CAIRO_HAS_FC_FONT
2773 /* If we have an unresolved pattern, resolve it and create
2774 * unscaled font. Otherwise, use the ones stored in font_face.
2776 if (font_face->pattern) {
2777 cairo_font_face_t *resolved;
2779 /* Cache the resolved font whilst the FcConfig remains consistent. */
2780 resolved = font_face->resolved_font_face;
2781 if (resolved != NULL) {
2782 if (! FcInitBringUptoDate ()) {
2783 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2784 return (cairo_font_face_t *) &_cairo_font_face_nil;
2787 if (font_face->resolved_config == FcConfigGetCurrent ())
2788 return cairo_font_face_reference (resolved);
2790 cairo_font_face_destroy (resolved);
2791 font_face->resolved_font_face = NULL;
2794 resolved = _cairo_ft_resolve_pattern (font_face->pattern,
2798 if (unlikely (resolved->status))
2801 font_face->resolved_font_face = cairo_font_face_reference (resolved);
2802 font_face->resolved_config = FcConfigGetCurrent ();
2808 return abstract_face;
2811 const cairo_font_face_backend_t _cairo_ft_font_face_backend = {
2813 #if CAIRO_HAS_FC_FONT
2814 _cairo_ft_font_face_create_for_toy,
2818 _cairo_ft_font_face_destroy,
2819 _cairo_ft_font_face_scaled_font_create,
2820 _cairo_ft_font_face_get_implementation
2823 #if CAIRO_HAS_FC_FONT
2824 static cairo_font_face_t *
2825 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
2827 cairo_ft_font_face_t *font_face;
2829 font_face = malloc (sizeof (cairo_ft_font_face_t));
2830 if (unlikely (font_face == NULL)) {
2831 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2832 return (cairo_font_face_t *) &_cairo_font_face_nil;
2835 font_face->unscaled = NULL;
2836 font_face->next = NULL;
2838 font_face->pattern = FcPatternDuplicate (pattern);
2839 if (unlikely (font_face->pattern == NULL)) {
2841 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2842 return (cairo_font_face_t *) &_cairo_font_face_nil;
2845 font_face->resolved_font_face = NULL;
2846 font_face->resolved_config = NULL;
2848 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
2850 return &font_face->base;
2854 static cairo_font_face_t *
2855 _cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
2856 cairo_ft_options_t *ft_options)
2858 cairo_ft_font_face_t *font_face, **prev_font_face;
2860 /* Looked for an existing matching font face */
2861 for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
2863 prev_font_face = &font_face->next, font_face = font_face->next)
2865 if (font_face->ft_options.load_flags == ft_options->load_flags &&
2866 font_face->ft_options.synth_flags == ft_options->synth_flags &&
2867 cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
2869 if (font_face->base.status) {
2870 /* The font_face has been left in an error state, abandon it. */
2871 *prev_font_face = font_face->next;
2875 if (font_face->unscaled == NULL) {
2876 /* Resurrect this "zombie" font_face (from
2877 * _cairo_ft_font_face_destroy), switching its unscaled_font
2878 * from owner to ownee. */
2879 font_face->unscaled = unscaled;
2880 _cairo_unscaled_font_reference (&unscaled->base);
2881 return &font_face->base;
2883 return cairo_font_face_reference (&font_face->base);
2887 /* No match found, create a new one */
2888 font_face = malloc (sizeof (cairo_ft_font_face_t));
2889 if (unlikely (!font_face)) {
2890 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2891 return (cairo_font_face_t *)&_cairo_font_face_nil;
2894 font_face->unscaled = unscaled;
2895 _cairo_unscaled_font_reference (&unscaled->base);
2897 font_face->ft_options = *ft_options;
2899 if (unscaled->faces && unscaled->faces->unscaled == NULL) {
2900 /* This "zombie" font_face (from _cairo_ft_font_face_destroy)
2901 * is no longer needed. */
2902 assert (unscaled->from_face && unscaled->faces->next == NULL);
2903 cairo_font_face_destroy (&unscaled->faces->base);
2904 unscaled->faces = NULL;
2907 font_face->next = unscaled->faces;
2908 unscaled->faces = font_face;
2910 #if CAIRO_HAS_FC_FONT
2911 font_face->pattern = NULL;
2914 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
2916 return &font_face->base;
2919 /* implement the platform-specific interface */
2921 #if CAIRO_HAS_FC_FONT
2922 static cairo_status_t
2923 _cairo_ft_font_options_substitute (const cairo_font_options_t *options,
2928 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
2930 if (FcPatternGet (pattern, FC_ANTIALIAS, 0, &v) == FcResultNoMatch)
2932 if (! FcPatternAddBool (pattern,
2934 options->antialias != CAIRO_ANTIALIAS_NONE))
2935 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2937 if (options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
2938 FcPatternDel (pattern, FC_RGBA);
2939 if (! FcPatternAddInteger (pattern, FC_RGBA, FC_RGBA_NONE))
2940 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2945 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
2947 if (FcPatternGet (pattern, FC_RGBA, 0, &v) == FcResultNoMatch)
2951 if (options->antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
2952 switch (options->subpixel_order) {
2953 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
2954 case CAIRO_SUBPIXEL_ORDER_RGB:
2958 case CAIRO_SUBPIXEL_ORDER_BGR:
2961 case CAIRO_SUBPIXEL_ORDER_VRGB:
2962 rgba = FC_RGBA_VRGB;
2964 case CAIRO_SUBPIXEL_ORDER_VBGR:
2965 rgba = FC_RGBA_VBGR;
2969 rgba = FC_RGBA_NONE;
2972 if (! FcPatternAddInteger (pattern, FC_RGBA, rgba))
2973 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2977 if (options->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
2979 if (FcPatternGet (pattern, FC_LCD_FILTER, 0, &v) == FcResultNoMatch)
2983 switch (options->lcd_filter) {
2984 case CAIRO_LCD_FILTER_NONE:
2985 lcd_filter = FT_LCD_FILTER_NONE;
2987 case CAIRO_LCD_FILTER_DEFAULT:
2988 case CAIRO_LCD_FILTER_INTRA_PIXEL:
2989 lcd_filter = FT_LCD_FILTER_LEGACY;
2991 case CAIRO_LCD_FILTER_FIR3:
2992 lcd_filter = FT_LCD_FILTER_LIGHT;
2995 case CAIRO_LCD_FILTER_FIR5:
2996 lcd_filter = FT_LCD_FILTER_DEFAULT;
3000 if (! FcPatternAddInteger (pattern, FC_LCD_FILTER, lcd_filter))
3001 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3005 if (options->hint_style != CAIRO_HINT_STYLE_DEFAULT)
3007 if (FcPatternGet (pattern, FC_HINTING, 0, &v) == FcResultNoMatch)
3009 if (! FcPatternAddBool (pattern,
3011 options->hint_style != CAIRO_HINT_STYLE_NONE))
3012 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3015 #ifdef FC_HINT_STYLE
3016 if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
3020 switch (options->hint_style) {
3021 case CAIRO_HINT_STYLE_NONE:
3022 hint_style = FC_HINT_NONE;
3024 case CAIRO_HINT_STYLE_SLIGHT:
3025 hint_style = FC_HINT_SLIGHT;
3027 case CAIRO_HINT_STYLE_MEDIUM:
3028 hint_style = FC_HINT_MEDIUM;
3030 case CAIRO_HINT_STYLE_FULL:
3031 case CAIRO_HINT_STYLE_DEFAULT:
3033 hint_style = FC_HINT_FULL;
3037 if (! FcPatternAddInteger (pattern, FC_HINT_STYLE, hint_style))
3038 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3043 return CAIRO_STATUS_SUCCESS;
3047 * cairo_ft_font_options_substitute:
3048 * @options: a #cairo_font_options_t object
3049 * @pattern: an existing #FcPattern
3051 * Add options to a #FcPattern based on a #cairo_font_options_t font
3052 * options object. Options that are already in the pattern, are not overridden,
3053 * so you should call this function after calling FcConfigSubstitute() (the
3054 * user's settings should override options based on the surface type), but
3055 * before calling FcDefaultSubstitute().
3060 cairo_ft_font_options_substitute (const cairo_font_options_t *options,
3063 if (cairo_font_options_status ((cairo_font_options_t *) options))
3066 _cairo_ft_font_options_substitute (options, pattern);
3069 static cairo_font_face_t *
3070 _cairo_ft_resolve_pattern (FcPattern *pattern,
3071 const cairo_matrix_t *font_matrix,
3072 const cairo_matrix_t *ctm,
3073 const cairo_font_options_t *font_options)
3075 cairo_status_t status;
3077 cairo_matrix_t scale;
3078 FcPattern *resolved;
3079 cairo_ft_font_transform_t sf;
3081 cairo_ft_unscaled_font_t *unscaled;
3082 cairo_ft_options_t ft_options;
3083 cairo_font_face_t *font_face;
3086 scale.x0 = scale.y0 = 0;
3087 cairo_matrix_multiply (&scale,
3091 status = _compute_transform (&sf, &scale);
3092 if (unlikely (status))
3093 return (cairo_font_face_t *)&_cairo_font_face_nil;
3095 pattern = FcPatternDuplicate (pattern);
3096 if (pattern == NULL)
3097 return (cairo_font_face_t *)&_cairo_font_face_nil;
3099 if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
3100 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3104 if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
3105 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3109 status = _cairo_ft_font_options_substitute (font_options, pattern);
3111 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3115 FcDefaultSubstitute (pattern);
3117 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3118 if (unlikely (status)) {
3119 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3123 if (unscaled == NULL) {
3124 resolved = FcFontMatch (NULL, pattern, &result);
3126 /* We failed to find any font. Substitute twin so that the user can
3127 * see something (and hopefully recognise that the font is missing)
3128 * and not just receive a NO_MEMORY error during rendering.
3130 font_face = _cairo_font_face_twin_create_fallback ();
3134 status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
3135 if (unlikely (status || unscaled == NULL)) {
3136 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3142 _get_pattern_ft_options (resolved, &ft_options);
3143 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3144 _cairo_unscaled_font_destroy (&unscaled->base);
3147 if (resolved != pattern)
3148 FcPatternDestroy (resolved);
3151 FcPatternDestroy (pattern);
3157 * cairo_ft_font_face_create_for_pattern:
3158 * @pattern: A fontconfig pattern. Cairo makes a copy of the pattern
3159 * if it needs to. You are free to modify or free @pattern after this call.
3161 * Creates a new font face for the FreeType font backend based on a
3162 * fontconfig pattern. This font can then be used with
3163 * cairo_set_font_face() or cairo_scaled_font_create(). The
3164 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3165 * also for the FreeType backend and can be used with functions such
3166 * as cairo_ft_scaled_font_lock_face().
3168 * Font rendering options are represented both here and when you
3169 * call cairo_scaled_font_create(). Font options that have a representation
3170 * in a #FcPattern must be passed in here; to modify #FcPattern
3171 * appropriately to reflect the options in a #cairo_font_options_t, call
3172 * cairo_ft_font_options_substitute().
3174 * The pattern's FC_FT_FACE element is inspected first and if that is set,
3175 * that will be the FreeType font face associated with the returned cairo
3176 * font face. Otherwise the FC_FILE element is checked. If it's set,
3177 * that and the value of the FC_INDEX element (defaults to zero) of @pattern
3178 * are used to load a font face from file.
3180 * If both steps from the previous paragraph fails, @pattern will be passed
3181 * to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
3182 * and the resulting font pattern is used.
3184 * If the FC_FT_FACE element of @pattern is set, the user is responsible
3185 * for making sure that the referenced FT_Face remains valid for the life
3186 * time of the returned #cairo_font_face_t. See
3187 * cairo_ft_font_face_create_for_ft_face() for an example of how to couple
3188 * the life time of the FT_Face to that of the cairo font-face.
3190 * Return value: a newly created #cairo_font_face_t. Free with
3191 * cairo_font_face_destroy() when you are done using it.
3196 cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
3198 cairo_ft_unscaled_font_t *unscaled;
3199 cairo_font_face_t *font_face;
3200 cairo_ft_options_t ft_options;
3201 cairo_status_t status;
3203 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3204 if (unlikely (status))
3205 return (cairo_font_face_t *) &_cairo_font_face_nil;
3206 if (unlikely (unscaled == NULL)) {
3207 /* Store the pattern. We will resolve it and create unscaled
3208 * font when creating scaled fonts */
3209 return _cairo_ft_font_face_create_for_pattern (pattern);
3212 _get_pattern_ft_options (pattern, &ft_options);
3213 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3214 _cairo_unscaled_font_destroy (&unscaled->base);
3221 * cairo_ft_font_face_create_for_ft_face:
3222 * @face: A FreeType face object, already opened. This must
3223 * be kept around until the face's ref_count drops to
3224 * zero and it is freed. Since the face may be referenced
3225 * internally to Cairo, the best way to determine when it
3226 * is safe to free the face is to pass a
3227 * #cairo_destroy_func_t to cairo_font_face_set_user_data()
3228 * @load_flags: flags to pass to FT_Load_Glyph when loading
3229 * glyphs from the font. These flags are OR'ed together with
3230 * the flags derived from the #cairo_font_options_t passed
3231 * to cairo_scaled_font_create(), so only a few values such
3232 * as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
3233 * are useful. You should not pass any of the flags affecting
3234 * the load target, such as %FT_LOAD_TARGET_LIGHT.
3236 * Creates a new font face for the FreeType font backend from a
3237 * pre-opened FreeType face. This font can then be used with
3238 * cairo_set_font_face() or cairo_scaled_font_create(). The
3239 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3240 * also for the FreeType backend and can be used with functions such
3241 * as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
3242 * to the FT_Face alive in a font-cache and the exact lifetime of the reference
3243 * depends highly upon the exact usage pattern and is subject to external
3244 * factors. You must not call FT_Done_Face() before the last reference to the
3245 * #cairo_font_face_t has been dropped.
3247 * As an example, below is how one might correctly couple the lifetime of
3248 * the FreeType face object to the #cairo_font_face_t.
3250 * <informalexample><programlisting>
3251 * static const cairo_user_data_key_t key;
3253 * font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
3254 * status = cairo_font_face_set_user_data (font_face, &key,
3255 * ft_face, (cairo_destroy_func_t) FT_Done_Face);
3257 * cairo_font_face_destroy (font_face);
3258 * FT_Done_Face (ft_face);
3261 * </programlisting></informalexample>
3263 * Return value: a newly created #cairo_font_face_t. Free with
3264 * cairo_font_face_destroy() when you are done using it.
3269 cairo_ft_font_face_create_for_ft_face (FT_Face face,
3272 cairo_ft_unscaled_font_t *unscaled;
3273 cairo_font_face_t *font_face;
3274 cairo_ft_options_t ft_options;
3275 cairo_status_t status;
3277 status = _cairo_ft_unscaled_font_create_from_face (face, &unscaled);
3278 if (unlikely (status))
3279 return (cairo_font_face_t *)&_cairo_font_face_nil;
3281 ft_options.load_flags = load_flags;
3282 ft_options.synth_flags = 0;
3283 _cairo_font_options_init_default (&ft_options.base);
3285 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3286 _cairo_unscaled_font_destroy (&unscaled->base);
3292 * cairo_ft_font_face_set_synthesize:
3293 * @font_face: The #cairo_ft_font_face_t object to modify
3294 * @synth_flags: the set of synthesis options to enable
3296 * FreeType provides the ability to synthesize different glyphs from a base
3297 * font, which is useful if you lack those glyphs from a true bold or oblique
3298 * font. See also #cairo_ft_synthesize_t.
3303 cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
3304 unsigned int synth_flags)
3306 cairo_ft_font_face_t *ft;
3308 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3311 ft = (cairo_ft_font_face_t *) font_face;
3312 ft->ft_options.synth_flags |= synth_flags;
3316 * cairo_ft_font_face_unset_synthesize:
3317 * @font_face: The #cairo_ft_font_face_t object to modify
3318 * @synth_flags: the set of synthesis options to disable
3320 * See cairo_ft_font_face_set_synthesize().
3325 cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
3326 unsigned int synth_flags)
3328 cairo_ft_font_face_t *ft;
3330 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3333 ft = (cairo_ft_font_face_t *) font_face;
3334 ft->ft_options.synth_flags &= ~synth_flags;
3338 * cairo_ft_font_face_get_synthesize:
3339 * @font_face: The #cairo_ft_font_face_t object to query
3341 * See #cairo_ft_synthesize_t.
3343 * Returns: the current set of synthesis options.
3348 cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
3350 cairo_ft_font_face_t *ft;
3352 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3355 ft = (cairo_ft_font_face_t *) font_face;
3356 return ft->ft_options.synth_flags;
3360 * cairo_ft_scaled_font_lock_face:
3361 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
3362 * object can be created by calling cairo_scaled_font_create() on a
3363 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
3364 * cairo_ft_font_face_create_for_ft_face()).
3366 * cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
3367 * backend font and scales it appropriately for the font. You must
3368 * release the face with cairo_ft_scaled_font_unlock_face()
3369 * when you are done using it. Since the #FT_Face object can be
3370 * shared between multiple #cairo_scaled_font_t objects, you must not
3371 * lock any other font objects until you unlock this one. A count is
3372 * kept of the number of times cairo_ft_scaled_font_lock_face() is
3373 * called. cairo_ft_scaled_font_unlock_face() must be called the same number
3376 * You must be careful when using this function in a library or in a
3377 * threaded application, because freetype's design makes it unsafe to
3378 * call freetype functions simultaneously from multiple threads, (even
3379 * if using distinct FT_Face objects). Because of this, application
3380 * code that acquires an FT_Face object with this call must add its
3381 * own locking to protect any use of that object, (and which also must
3382 * protect any other calls into cairo as almost any cairo function
3383 * might result in a call into the freetype library).
3385 * Return value: The #FT_Face object for @font, scaled appropriately,
3386 * or %NULL if @scaled_font is in an error state (see
3387 * cairo_scaled_font_status()) or there is insufficient memory.
3392 cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
3394 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
3396 cairo_status_t status;
3398 if (! _cairo_scaled_font_is_ft (abstract_font)) {
3399 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
3403 if (scaled_font->base.status)
3406 face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled);
3407 if (unlikely (face == NULL)) {
3408 status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY);
3412 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
3413 &scaled_font->base.scale);
3414 if (unlikely (status)) {
3415 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
3416 status = _cairo_scaled_font_set_error (&scaled_font->base, status);
3420 /* Note: We deliberately release the unscaled font's mutex here,
3421 * so that we are not holding a lock across two separate calls to
3422 * cairo function, (which would give the application some
3423 * opportunity for creating deadlock. This is obviously unsafe,
3424 * but as documented, the user must add manual locking when using
3426 CAIRO_MUTEX_UNLOCK (scaled_font->unscaled->mutex);
3432 * cairo_ft_scaled_font_unlock_face:
3433 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
3434 * object can be created by calling cairo_scaled_font_create() on a
3435 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
3436 * cairo_ft_font_face_create_for_ft_face()).
3438 * Releases a face obtained with cairo_ft_scaled_font_lock_face().
3443 cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
3445 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
3447 if (! _cairo_scaled_font_is_ft (abstract_font)) {
3448 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
3452 if (scaled_font->base.status)
3455 /* Note: We released the unscaled font's mutex at the end of
3456 * cairo_ft_scaled_font_lock_face, so we have to acquire it again
3457 * as _cairo_ft_unscaled_font_unlock_face expects it to be held
3458 * when we call into it. */
3459 CAIRO_MUTEX_LOCK (scaled_font->unscaled->mutex);
3461 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
3465 _cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font)
3467 cairo_ft_scaled_font_t *ft_scaled_font;
3469 if (!_cairo_scaled_font_is_ft (scaled_font))
3472 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
3473 if (ft_scaled_font->ft_options.load_flags & FT_LOAD_VERTICAL_LAYOUT)
3479 _cairo_ft_scaled_font_get_load_flags (cairo_scaled_font_t *scaled_font)
3481 cairo_ft_scaled_font_t *ft_scaled_font;
3483 if (! _cairo_scaled_font_is_ft (scaled_font))
3486 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
3487 return ft_scaled_font->ft_options.load_flags;
3491 _cairo_ft_font_reset_static_data (void)
3493 _cairo_ft_unscaled_font_map_destroy ();