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,
712 cairo_ft_unscaled_font_t *unscaled)
714 cairo_status_t status;
715 double x_scale, y_scale;
716 cairo_matrix_t normalized = *scale;
718 /* The font matrix has x and y "scale" components which we extract and
719 * use as character scale values. These influence the way freetype
720 * chooses hints, as well as selecting different bitmaps in
721 * hand-rendered fonts. We also copy the normalized matrix to
722 * freetype's transformation.
725 status = _cairo_matrix_compute_basis_scale_factors (scale,
728 if (unlikely (status))
731 /* FreeType docs say this about x_scale and y_scale:
732 * "A character width or height smaller than 1pt is set to 1pt;"
733 * So, we cap them from below at 1.0 and let the FT transform
734 * take care of sub-1.0 scaling. */
740 if (unscaled && (unscaled->face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) {
741 double min_distance = DBL_MAX;
744 double best_x_size = 0;
745 double best_y_size = 0;
747 for (i = 0; i < unscaled->face->num_fixed_sizes; i++) {
748 double x_size = unscaled->face->available_sizes[i].y_ppem / 64.;
749 double y_size = unscaled->face->available_sizes[i].y_ppem / 64.;
750 double distance = fabs (y_size - y_scale);
752 if (distance <= min_distance) {
753 min_distance = distance;
755 best_x_size = x_size;
756 best_y_size = y_size;
760 x_scale = best_x_size;
761 y_scale = best_y_size;
764 sf->x_scale = x_scale;
765 sf->y_scale = y_scale;
767 cairo_matrix_scale (&normalized, 1.0 / x_scale, 1.0 / y_scale);
769 _cairo_matrix_get_affine (&normalized,
770 &sf->shape[0][0], &sf->shape[0][1],
771 &sf->shape[1][0], &sf->shape[1][1],
774 return CAIRO_STATUS_SUCCESS;
777 /* Temporarily scales an unscaled font to the give scale. We catch
778 * scaling to the same size, since changing a FT_Face is expensive.
780 static cairo_status_t
781 _cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
782 cairo_matrix_t *scale)
784 cairo_status_t status;
785 cairo_ft_font_transform_t sf;
789 assert (unscaled->face != NULL);
791 if (unscaled->have_scale &&
792 scale->xx == unscaled->current_scale.xx &&
793 scale->yx == unscaled->current_scale.yx &&
794 scale->xy == unscaled->current_scale.xy &&
795 scale->yy == unscaled->current_scale.yy)
796 return CAIRO_STATUS_SUCCESS;
798 unscaled->have_scale = TRUE;
799 unscaled->current_scale = *scale;
801 status = _compute_transform (&sf, scale, unscaled);
802 if (unlikely (status))
805 unscaled->x_scale = sf.x_scale;
806 unscaled->y_scale = sf.y_scale;
808 mat.xx = DOUBLE_TO_16_16(sf.shape[0][0]);
809 mat.yx = - DOUBLE_TO_16_16(sf.shape[0][1]);
810 mat.xy = - DOUBLE_TO_16_16(sf.shape[1][0]);
811 mat.yy = DOUBLE_TO_16_16(sf.shape[1][1]);
813 unscaled->have_shape = (mat.xx != 0x10000 ||
818 unscaled->Current_Shape = mat;
819 cairo_matrix_init (&unscaled->current_shape,
820 sf.shape[0][0], sf.shape[0][1],
821 sf.shape[1][0], sf.shape[1][1],
824 FT_Set_Transform(unscaled->face, &mat, NULL);
826 error = FT_Set_Char_Size (unscaled->face,
827 sf.x_scale * 64.0 + .5,
828 sf.y_scale * 64.0 + .5,
831 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
833 return CAIRO_STATUS_SUCCESS;
836 /* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
837 * into a different format. For example, we want to convert a
838 * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
839 * ARGB or ABGR bitmap.
841 * this function prepares a target descriptor for this operation.
843 * input :: target bitmap descriptor. The function will set its
844 * 'width', 'rows' and 'pitch' fields, and only these
846 * slot :: the glyph slot containing the source bitmap. this
847 * function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
849 * mode :: the requested final rendering mode. supported values are
850 * MONO, NORMAL (i.e. gray), LCD and LCD_V
852 * the function returns the size in bytes of the corresponding buffer,
853 * it's up to the caller to allocate the corresponding memory block
854 * before calling _fill_xrender_bitmap
856 * it also returns -1 in case of error (e.g. incompatible arguments,
857 * like trying to convert a gray bitmap into a monochrome one)
860 _compute_xrender_bitmap_size(FT_Bitmap *target,
865 int width, height, pitch;
867 if (slot->format != FT_GLYPH_FORMAT_BITMAP)
870 /* compute the size of the final bitmap */
871 ftbit = &slot->bitmap;
873 width = ftbit->width;
874 height = ftbit->rows;
875 pitch = (width + 3) & ~3;
877 switch (ftbit->pixel_mode) {
878 case FT_PIXEL_MODE_MONO:
879 if (mode == FT_RENDER_MODE_MONO) {
880 pitch = (((width + 31) & ~31) >> 3);
885 case FT_PIXEL_MODE_GRAY:
886 if (mode == FT_RENDER_MODE_LCD ||
887 mode == FT_RENDER_MODE_LCD_V)
889 /* each pixel is replicated into a 32-bit ARGB value */
894 case FT_PIXEL_MODE_LCD:
895 if (mode != FT_RENDER_MODE_LCD)
898 /* horz pixel triplets are packed into 32-bit ARGB values */
903 case FT_PIXEL_MODE_LCD_V:
904 if (mode != FT_RENDER_MODE_LCD_V)
907 /* vert pixel triplets are packed into 32-bit ARGB values */
912 default: /* unsupported source format */
916 target->width = width;
917 target->rows = height;
918 target->pitch = pitch;
919 target->buffer = NULL;
921 return pitch * height;
924 /* this functions converts the glyph bitmap found in a FT_GlyphSlot
925 * into a different format (see _compute_xrender_bitmap_size)
927 * you should call this function after _compute_xrender_bitmap_size
929 * target :: target bitmap descriptor. Note that its 'buffer' pointer
930 * must point to memory allocated by the caller
932 * slot :: the glyph slot containing the source bitmap
934 * mode :: the requested final rendering mode
936 * bgr :: boolean, set if BGR or VBGR pixel ordering is needed
939 _fill_xrender_bitmap(FT_Bitmap *target,
944 FT_Bitmap *ftbit = &slot->bitmap;
945 unsigned char *srcLine = ftbit->buffer;
946 unsigned char *dstLine = target->buffer;
947 int src_pitch = ftbit->pitch;
948 int width = target->width;
949 int height = target->rows;
950 int pitch = target->pitch;
954 subpixel = (mode == FT_RENDER_MODE_LCD ||
955 mode == FT_RENDER_MODE_LCD_V);
958 srcLine -= src_pitch * (ftbit->rows - 1);
960 target->pixel_mode = ftbit->pixel_mode;
962 switch (ftbit->pixel_mode) {
963 case FT_PIXEL_MODE_MONO:
965 /* convert mono to ARGB32 values */
967 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
970 for (x = 0; x < width; x++) {
971 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
972 ((unsigned int *) dstLine)[x] = 0xffffffffU;
975 target->pixel_mode = FT_PIXEL_MODE_LCD;
977 } else if (mode == FT_RENDER_MODE_NORMAL) {
978 /* convert mono to 8-bit gray */
980 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
983 for (x = 0; x < width; x++) {
984 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
988 target->pixel_mode = FT_PIXEL_MODE_GRAY;
991 /* copy mono to mono */
993 int bytes = (width + 7) >> 3;
995 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
996 memcpy (dstLine, srcLine, bytes);
1000 case FT_PIXEL_MODE_GRAY:
1002 /* convert gray to ARGB32 values */
1004 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1006 unsigned int *dst = (unsigned int *) dstLine;
1008 for (x = 0; x < width; x++) {
1009 unsigned int pix = srcLine[x];
1017 target->pixel_mode = FT_PIXEL_MODE_LCD;
1019 /* copy gray into gray */
1021 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1022 memcpy (dstLine, srcLine, width);
1026 case FT_PIXEL_MODE_LCD:
1028 /* convert horizontal RGB into ARGB32 */
1030 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1032 unsigned char *src = srcLine;
1033 unsigned int *dst = (unsigned int *) dstLine;
1035 for (x = 0; x < width; x++, src += 3) {
1038 pix = ((unsigned int)src[0] << 16) |
1039 ((unsigned int)src[1] << 8) |
1040 ((unsigned int)src[2] ) |
1041 ((unsigned int)src[1] << 24) ;
1047 /* convert horizontal BGR into ARGB32 */
1049 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1052 unsigned char *src = srcLine;
1053 unsigned int *dst = (unsigned int *) dstLine;
1055 for (x = 0; x < width; x++, src += 3) {
1058 pix = ((unsigned int)src[2] << 16) |
1059 ((unsigned int)src[1] << 8) |
1060 ((unsigned int)src[0] ) |
1061 ((unsigned int)src[1] << 24) ;
1069 default: /* FT_PIXEL_MODE_LCD_V */
1070 /* convert vertical RGB into ARGB32 */
1073 for (h = height; h > 0; h--, srcLine += 3 * src_pitch, dstLine += pitch) {
1075 unsigned char* src = srcLine;
1076 unsigned int* dst = (unsigned int *) dstLine;
1078 for (x = 0; x < width; x++, src += 1) {
1080 pix = ((unsigned int)src[0] << 16) |
1081 ((unsigned int)src[src_pitch] << 8) |
1082 ((unsigned int)src[src_pitch*2] ) |
1083 ((unsigned int)src[src_pitch] << 24) ;
1089 for (h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch) {
1091 unsigned char *src = srcLine;
1092 unsigned int *dst = (unsigned int *) dstLine;
1094 for (x = 0; x < width; x++, src += 1) {
1097 pix = ((unsigned int)src[src_pitch * 2] << 16) |
1098 ((unsigned int)src[src_pitch] << 8) |
1099 ((unsigned int)src[0] ) |
1100 ((unsigned int)src[src_pitch] << 24) ;
1110 /* Fills in val->image with an image surface created from @bitmap
1112 static cairo_status_t
1113 _get_bitmap_surface (FT_Bitmap *bitmap,
1114 cairo_bool_t own_buffer,
1115 cairo_font_options_t *font_options,
1116 cairo_image_surface_t **surface)
1118 int width, height, stride;
1119 unsigned char *data;
1120 int format = CAIRO_FORMAT_A8;
1121 cairo_image_surface_t *image;
1123 width = bitmap->width;
1124 height = bitmap->rows;
1126 if (width == 0 || height == 0) {
1127 *surface = (cairo_image_surface_t *)
1128 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1129 return (*surface)->base.status;
1132 switch (bitmap->pixel_mode) {
1133 case FT_PIXEL_MODE_MONO:
1134 stride = (((width + 31) & ~31) >> 3);
1136 data = bitmap->buffer;
1137 assert (stride == bitmap->pitch);
1139 data = _cairo_malloc_ab (height, stride);
1141 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1143 if (stride == bitmap->pitch) {
1144 memcpy (data, bitmap->buffer, stride * height);
1147 unsigned char *source, *dest;
1149 source = bitmap->buffer;
1151 for (i = height; i; i--) {
1152 memcpy (dest, source, bitmap->pitch);
1153 memset (dest + bitmap->pitch, '\0', stride - bitmap->pitch);
1155 source += bitmap->pitch;
1161 #ifndef WORDS_BIGENDIAN
1164 int count = stride * height;
1167 *d = CAIRO_BITSWAP8 (*d);
1172 format = CAIRO_FORMAT_A1;
1175 case FT_PIXEL_MODE_LCD:
1176 case FT_PIXEL_MODE_LCD_V:
1177 case FT_PIXEL_MODE_GRAY:
1178 if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
1179 stride = bitmap->pitch;
1181 data = bitmap->buffer;
1183 data = _cairo_malloc_ab (height, stride);
1185 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1187 memcpy (data, bitmap->buffer, stride * height);
1190 format = CAIRO_FORMAT_A8;
1192 /* if we get there, the data from the source bitmap
1193 * really comes from _fill_xrender_bitmap, and is
1194 * made of 32-bit ARGB or ABGR values */
1195 assert (own_buffer != 0);
1196 assert (bitmap->pixel_mode != FT_PIXEL_MODE_GRAY);
1198 data = bitmap->buffer;
1199 stride = bitmap->pitch;
1200 format = CAIRO_FORMAT_ARGB32;
1203 case FT_PIXEL_MODE_GRAY2:
1204 case FT_PIXEL_MODE_GRAY4:
1205 /* These could be triggered by very rare types of TrueType fonts */
1208 free (bitmap->buffer);
1209 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1213 *surface = image = (cairo_image_surface_t *)
1214 cairo_image_surface_create_for_data (data,
1216 width, height, stride);
1217 if (image->base.status) {
1219 return (*surface)->base.status;
1222 if (format == CAIRO_FORMAT_ARGB32)
1223 pixman_image_set_component_alpha (image->pixman_image, TRUE);
1225 _cairo_image_surface_assume_ownership_of_data (image);
1227 _cairo_debug_check_image_surface_is_defined (&image->base);
1229 return CAIRO_STATUS_SUCCESS;
1232 /* Converts an outline FT_GlyphSlot into an image
1234 * This could go through _render_glyph_bitmap as well, letting
1235 * FreeType convert the outline to a bitmap, but doing it ourselves
1236 * has two minor advantages: first, we save a copy of the bitmap
1237 * buffer: we can directly use the buffer that FreeType renders
1240 * Second, it may help when we add support for subpixel
1241 * rendering: the Xft code does it this way. (Keith thinks that
1242 * it may also be possible to get the subpixel rendering with
1243 * FT_Render_Glyph: something worth looking into in more detail
1244 * when we add subpixel support. If so, we may want to eliminate
1245 * this version of the code path entirely.
1247 static cairo_status_t
1248 _render_glyph_outline (FT_Face face,
1249 cairo_font_options_t *font_options,
1250 cairo_image_surface_t **surface)
1252 int rgba = FC_RGBA_UNKNOWN;
1253 int lcd_filter = FT_LCD_FILTER_LEGACY;
1254 FT_GlyphSlot glyphslot = face->glyph;
1255 FT_Outline *outline = &glyphslot->outline;
1258 unsigned int width, height;
1259 cairo_status_t status;
1261 FT_Library library = glyphslot->library;
1262 FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
1264 switch (font_options->antialias) {
1265 case CAIRO_ANTIALIAS_NONE:
1266 render_mode = FT_RENDER_MODE_MONO;
1269 case CAIRO_ANTIALIAS_SUBPIXEL:
1270 case CAIRO_ANTIALIAS_BEST:
1271 switch (font_options->subpixel_order) {
1272 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1273 case CAIRO_SUBPIXEL_ORDER_RGB:
1274 case CAIRO_SUBPIXEL_ORDER_BGR:
1275 render_mode = FT_RENDER_MODE_LCD;
1278 case CAIRO_SUBPIXEL_ORDER_VRGB:
1279 case CAIRO_SUBPIXEL_ORDER_VBGR:
1280 render_mode = FT_RENDER_MODE_LCD_V;
1284 switch (font_options->lcd_filter) {
1285 case CAIRO_LCD_FILTER_NONE:
1286 lcd_filter = FT_LCD_FILTER_NONE;
1288 case CAIRO_LCD_FILTER_DEFAULT:
1289 case CAIRO_LCD_FILTER_INTRA_PIXEL:
1290 lcd_filter = FT_LCD_FILTER_LEGACY;
1292 case CAIRO_LCD_FILTER_FIR3:
1293 lcd_filter = FT_LCD_FILTER_LIGHT;
1295 case CAIRO_LCD_FILTER_FIR5:
1296 lcd_filter = FT_LCD_FILTER_DEFAULT;
1302 case CAIRO_ANTIALIAS_DEFAULT:
1303 case CAIRO_ANTIALIAS_GRAY:
1304 case CAIRO_ANTIALIAS_GOOD:
1305 case CAIRO_ANTIALIAS_FAST:
1306 render_mode = FT_RENDER_MODE_NORMAL;
1309 FT_Outline_Get_CBox (outline, &cbox);
1313 cbox.xMax = (cbox.xMax + 63) & -64;
1314 cbox.yMax = (cbox.yMax + 63) & -64;
1316 width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
1317 height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
1319 if (width * height == 0) {
1320 cairo_format_t format;
1321 /* Looks like fb handles zero-sized images just fine */
1322 switch (render_mode) {
1323 case FT_RENDER_MODE_MONO:
1324 format = CAIRO_FORMAT_A1;
1326 case FT_RENDER_MODE_LCD:
1327 case FT_RENDER_MODE_LCD_V:
1328 format= CAIRO_FORMAT_ARGB32;
1330 case FT_RENDER_MODE_LIGHT:
1331 case FT_RENDER_MODE_NORMAL:
1332 case FT_RENDER_MODE_MAX:
1334 format = CAIRO_FORMAT_A8;
1338 (*surface) = (cairo_image_surface_t *)
1339 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1340 if ((*surface)->base.status)
1341 return (*surface)->base.status;
1346 switch (render_mode) {
1347 case FT_RENDER_MODE_LCD:
1348 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
1354 case FT_RENDER_MODE_LCD_V:
1355 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
1356 rgba = FC_RGBA_VBGR;
1358 rgba = FC_RGBA_VRGB;
1361 case FT_RENDER_MODE_MONO:
1362 case FT_RENDER_MODE_LIGHT:
1363 case FT_RENDER_MODE_NORMAL:
1364 case FT_RENDER_MODE_MAX:
1369 #if HAVE_FT_LIBRARY_SETLCDFILTER
1370 FT_Library_SetLcdFilter (library, lcd_filter);
1373 fterror = FT_Render_Glyph (face->glyph, render_mode);
1375 #if HAVE_FT_LIBRARY_SETLCDFILTER
1376 FT_Library_SetLcdFilter (library, FT_LCD_FILTER_NONE);
1380 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1382 bitmap_size = _compute_xrender_bitmap_size (&bitmap,
1385 if (bitmap_size < 0)
1386 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1388 bitmap.buffer = calloc (1, bitmap_size);
1389 if (bitmap.buffer == NULL)
1390 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1392 _fill_xrender_bitmap (&bitmap, face->glyph, render_mode,
1393 (rgba == FC_RGBA_BGR || rgba == FC_RGBA_VBGR));
1396 * _get_bitmap_surface will free bitmap.buffer if there is an error
1398 status = _get_bitmap_surface (&bitmap, TRUE, font_options, surface);
1399 if (unlikely (status))
1402 /* Note: the font's coordinate system is upside down from ours, so the
1403 * Y coordinate of the control box needs to be negated. Moreover, device
1404 * offsets are position of glyph origin relative to top left while xMin
1405 * and yMax are offsets of top left relative to origin. Another negation.
1407 cairo_surface_set_device_offset (&(*surface)->base,
1408 (double)-glyphslot->bitmap_left,
1409 (double)+glyphslot->bitmap_top);
1412 return CAIRO_STATUS_SUCCESS;
1415 /* Converts a bitmap (or other) FT_GlyphSlot into an image */
1416 static cairo_status_t
1417 _render_glyph_bitmap (FT_Face face,
1418 cairo_font_options_t *font_options,
1419 cairo_image_surface_t **surface)
1421 FT_GlyphSlot glyphslot = face->glyph;
1422 cairo_status_t status;
1425 /* According to the FreeType docs, glyphslot->format could be
1426 * something other than FT_GLYPH_FORMAT_OUTLINE or
1427 * FT_GLYPH_FORMAT_BITMAP. Calling FT_Render_Glyph gives FreeType
1428 * the opportunity to convert such to
1429 * bitmap. FT_GLYPH_FORMAT_COMPOSITE will not be encountered since
1430 * we avoid the FT_LOAD_NO_RECURSE flag.
1432 error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL);
1433 /* XXX ignoring all other errors for now. They are not fatal, typically
1434 * just a glyph-not-found. */
1435 if (error == FT_Err_Out_Of_Memory)
1436 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1438 status = _get_bitmap_surface (&glyphslot->bitmap,
1439 FALSE, font_options,
1441 if (unlikely (status))
1445 * Note: the font's coordinate system is upside down from ours, so the
1446 * Y coordinate of the control box needs to be negated. Moreover, device
1447 * offsets are position of glyph origin relative to top left while
1448 * bitmap_left and bitmap_top are offsets of top left relative to origin.
1451 cairo_surface_set_device_offset (&(*surface)->base,
1452 -glyphslot->bitmap_left,
1453 +glyphslot->bitmap_top);
1455 return CAIRO_STATUS_SUCCESS;
1458 static cairo_status_t
1459 _transform_glyph_bitmap (cairo_matrix_t * shape,
1460 cairo_image_surface_t ** surface)
1462 cairo_matrix_t original_to_transformed;
1463 cairo_matrix_t transformed_to_original;
1464 cairo_image_surface_t *old_image;
1465 cairo_surface_t *image;
1467 double origin_x, origin_y;
1468 int orig_width, orig_height;
1470 int x_min, y_min, x_max, y_max;
1472 cairo_status_t status;
1473 cairo_surface_pattern_t pattern;
1475 /* We want to compute a transform that takes the origin
1476 * (device_x_offset, device_y_offset) to 0,0, then applies
1477 * the "shape" portion of the font transform
1479 original_to_transformed = *shape;
1481 cairo_surface_get_device_offset (&(*surface)->base, &origin_x, &origin_y);
1482 orig_width = (*surface)->width;
1483 orig_height = (*surface)->height;
1485 cairo_matrix_translate (&original_to_transformed,
1486 -origin_x, -origin_y);
1488 /* Find the bounding box of the original bitmap under that
1492 x[1] = orig_width; y[1] = 0;
1493 x[2] = orig_width; y[2] = orig_height;
1494 x[3] = 0; y[3] = orig_height;
1496 for (i = 0; i < 4; i++)
1497 cairo_matrix_transform_point (&original_to_transformed,
1500 x_min = floor (x[0]); y_min = floor (y[0]);
1501 x_max = ceil (x[0]); y_max = ceil (y[0]);
1503 for (i = 1; i < 4; i++) {
1505 x_min = floor (x[i]);
1506 else if (x[i] > x_max)
1507 x_max = ceil (x[i]);
1509 y_min = floor (y[i]);
1510 else if (y[i] > y_max)
1511 y_max = ceil (y[i]);
1514 /* Adjust the transform so that the bounding box starts at 0,0 ...
1515 * this gives our final transform from original bitmap to transformed
1518 original_to_transformed.x0 -= x_min;
1519 original_to_transformed.y0 -= y_min;
1521 /* Create the transformed bitmap */
1522 width = x_max - x_min;
1523 height = y_max - y_min;
1525 transformed_to_original = original_to_transformed;
1526 status = cairo_matrix_invert (&transformed_to_original);
1527 if (unlikely (status))
1530 image = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
1531 if (unlikely (image->status))
1532 return image->status;
1534 /* Draw the original bitmap transformed into the new bitmap
1536 _cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
1537 cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
1539 status = _cairo_surface_paint (image,
1540 CAIRO_OPERATOR_SOURCE,
1544 _cairo_pattern_fini (&pattern.base);
1546 if (unlikely (status)) {
1547 cairo_surface_destroy (image);
1551 /* Now update the cache entry for the new bitmap, recomputing
1552 * the origin based on the final transform.
1554 cairo_matrix_transform_point (&original_to_transformed,
1555 &origin_x, &origin_y);
1557 old_image = (*surface);
1558 (*surface) = (cairo_image_surface_t *)image;
1559 cairo_surface_destroy (&old_image->base);
1561 cairo_surface_set_device_offset (&(*surface)->base,
1562 _cairo_lround (origin_x),
1563 _cairo_lround (origin_y));
1564 return CAIRO_STATUS_SUCCESS;
1567 static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend = {
1568 _cairo_ft_unscaled_font_destroy,
1570 _cairo_ft_unscaled_font_create_glyph
1574 /* #cairo_ft_scaled_font_t */
1576 typedef struct _cairo_ft_scaled_font {
1577 cairo_scaled_font_t base;
1578 cairo_ft_unscaled_font_t *unscaled;
1579 cairo_ft_options_t ft_options;
1580 } cairo_ft_scaled_font_t;
1582 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend;
1584 #if CAIRO_HAS_FC_FONT
1585 /* The load flags passed to FT_Load_Glyph control aspects like hinting and
1586 * antialiasing. Here we compute them from the fields of a FcPattern.
1589 _get_pattern_ft_options (FcPattern *pattern, cairo_ft_options_t *ret)
1591 FcBool antialias, vertical_layout, hinting, autohint, bitmap, embolden;
1592 cairo_ft_options_t ft_options;
1594 #ifdef FC_HINT_STYLE
1598 _cairo_font_options_init_default (&ft_options.base);
1599 ft_options.load_flags = FT_LOAD_DEFAULT;
1600 ft_options.synth_flags = 0;
1602 #ifndef FC_EMBEDDED_BITMAP
1603 #define FC_EMBEDDED_BITMAP "embeddedbitmap"
1606 /* Check whether to force use of embedded bitmaps */
1607 if (FcPatternGetBool (pattern,
1608 FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
1611 /* disable antialiasing if requested */
1612 if (FcPatternGetBool (pattern,
1613 FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
1617 cairo_subpixel_order_t subpixel_order;
1620 /* disable hinting if requested */
1621 if (FcPatternGetBool (pattern,
1622 FC_HINTING, 0, &hinting) != FcResultMatch)
1625 if (FcPatternGetInteger (pattern,
1626 FC_RGBA, 0, &rgba) != FcResultMatch)
1627 rgba = FC_RGBA_UNKNOWN;
1631 subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
1634 subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
1637 subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
1640 subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
1642 case FC_RGBA_UNKNOWN:
1645 subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1649 if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT) {
1650 ft_options.base.subpixel_order = subpixel_order;
1651 ft_options.base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1654 if (FcPatternGetInteger (pattern,
1655 FC_LCD_FILTER, 0, &lcd_filter) == FcResultMatch)
1657 switch (lcd_filter) {
1659 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1661 case FC_LCD_DEFAULT:
1662 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR5;
1665 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR3;
1668 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_INTRA_PIXEL;
1673 #ifdef FC_HINT_STYLE
1674 if (FcPatternGetInteger (pattern,
1675 FC_HINT_STYLE, 0, &hintstyle) != FcResultMatch)
1676 hintstyle = FC_HINT_FULL;
1679 hintstyle = FC_HINT_NONE;
1681 switch (hintstyle) {
1683 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1685 case FC_HINT_SLIGHT:
1686 ft_options.base.hint_style = CAIRO_HINT_STYLE_SLIGHT;
1688 case FC_HINT_MEDIUM:
1690 ft_options.base.hint_style = CAIRO_HINT_STYLE_MEDIUM;
1693 ft_options.base.hint_style = CAIRO_HINT_STYLE_FULL;
1696 #else /* !FC_HINT_STYLE */
1698 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1700 #endif /* FC_HINT_STYLE */
1702 /* Force embedded bitmaps off if no hinting requested */
1703 if (ft_options.base.hint_style == CAIRO_HINT_STYLE_NONE)
1707 ft_options.load_flags |= FT_LOAD_NO_BITMAP;
1710 ft_options.base.antialias = CAIRO_ANTIALIAS_NONE;
1713 /* force autohinting if requested */
1714 if (FcPatternGetBool (pattern,
1715 FC_AUTOHINT, 0, &autohint) != FcResultMatch)
1719 ft_options.load_flags |= FT_LOAD_FORCE_AUTOHINT;
1721 if (FcPatternGetBool (pattern,
1722 FC_VERTICAL_LAYOUT, 0, &vertical_layout) != FcResultMatch)
1723 vertical_layout = FcFalse;
1725 if (vertical_layout)
1726 ft_options.load_flags |= FT_LOAD_VERTICAL_LAYOUT;
1729 #define FC_EMBOLDEN "embolden"
1731 if (FcPatternGetBool (pattern,
1732 FC_EMBOLDEN, 0, &embolden) != FcResultMatch)
1736 ft_options.synth_flags |= CAIRO_FT_SYNTHESIZE_BOLD;
1743 _cairo_ft_options_merge (cairo_ft_options_t *options,
1744 cairo_ft_options_t *other)
1746 int load_flags = other->load_flags;
1747 int load_target = FT_LOAD_TARGET_NORMAL;
1749 /* clear load target mode */
1750 load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(other->load_flags)));
1752 if (load_flags & FT_LOAD_NO_HINTING)
1753 other->base.hint_style = CAIRO_HINT_STYLE_NONE;
1755 if (other->base.antialias == CAIRO_ANTIALIAS_NONE ||
1756 options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1757 options->base.antialias = CAIRO_ANTIALIAS_NONE;
1758 options->base.subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1761 if (other->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL &&
1762 (options->base.antialias == CAIRO_ANTIALIAS_DEFAULT ||
1763 options->base.antialias == CAIRO_ANTIALIAS_GRAY)) {
1764 options->base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1765 options->base.subpixel_order = other->base.subpixel_order;
1768 if (options->base.hint_style == CAIRO_HINT_STYLE_DEFAULT)
1769 options->base.hint_style = other->base.hint_style;
1771 if (other->base.hint_style == CAIRO_HINT_STYLE_NONE)
1772 options->base.hint_style = CAIRO_HINT_STYLE_NONE;
1774 if (options->base.lcd_filter == CAIRO_LCD_FILTER_DEFAULT)
1775 options->base.lcd_filter = other->base.lcd_filter;
1777 if (other->base.lcd_filter == CAIRO_LCD_FILTER_NONE)
1778 options->base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1780 if (options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1781 if (options->base.hint_style == CAIRO_HINT_STYLE_NONE)
1782 load_flags |= FT_LOAD_NO_HINTING;
1784 load_target = FT_LOAD_TARGET_MONO;
1785 load_flags |= FT_LOAD_MONOCHROME;
1787 switch (options->base.hint_style) {
1788 case CAIRO_HINT_STYLE_NONE:
1789 load_flags |= FT_LOAD_NO_HINTING;
1791 case CAIRO_HINT_STYLE_SLIGHT:
1792 load_target = FT_LOAD_TARGET_LIGHT;
1794 case CAIRO_HINT_STYLE_MEDIUM:
1796 case CAIRO_HINT_STYLE_FULL:
1797 case CAIRO_HINT_STYLE_DEFAULT:
1798 if (options->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
1799 switch (options->base.subpixel_order) {
1800 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1801 case CAIRO_SUBPIXEL_ORDER_RGB:
1802 case CAIRO_SUBPIXEL_ORDER_BGR:
1803 load_target = FT_LOAD_TARGET_LCD;
1805 case CAIRO_SUBPIXEL_ORDER_VRGB:
1806 case CAIRO_SUBPIXEL_ORDER_VBGR:
1807 load_target = FT_LOAD_TARGET_LCD_V;
1815 options->load_flags = load_flags | load_target;
1816 options->synth_flags = other->synth_flags;
1819 static cairo_status_t
1820 _cairo_ft_font_face_scaled_font_create (void *abstract_font_face,
1821 const cairo_matrix_t *font_matrix,
1822 const cairo_matrix_t *ctm,
1823 const cairo_font_options_t *options,
1824 cairo_scaled_font_t **font_out)
1826 cairo_ft_font_face_t *font_face = abstract_font_face;
1827 cairo_ft_scaled_font_t *scaled_font;
1829 FT_Size_Metrics *metrics;
1830 cairo_font_extents_t fs_metrics;
1831 cairo_status_t status;
1832 cairo_ft_unscaled_font_t *unscaled;
1834 assert (font_face->unscaled);
1836 face = _cairo_ft_unscaled_font_lock_face (font_face->unscaled);
1837 if (unlikely (face == NULL)) /* backend error */
1838 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1840 scaled_font = malloc (sizeof (cairo_ft_scaled_font_t));
1841 if (unlikely (scaled_font == NULL)) {
1842 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1846 scaled_font->unscaled = unscaled = font_face->unscaled;
1847 _cairo_unscaled_font_reference (&unscaled->base);
1849 _cairo_font_options_init_copy (&scaled_font->ft_options.base, options);
1850 _cairo_ft_options_merge (&scaled_font->ft_options, &font_face->ft_options);
1852 status = _cairo_scaled_font_init (&scaled_font->base,
1854 font_matrix, ctm, options,
1855 &_cairo_ft_scaled_font_backend);
1856 if (unlikely (status))
1857 goto CLEANUP_SCALED_FONT;
1859 status = _cairo_ft_unscaled_font_set_scale (unscaled,
1860 &scaled_font->base.scale);
1861 if (unlikely (status)) {
1862 /* This can only fail if we encounter an error with the underlying
1863 * font, so propagate the error back to the font-face. */
1864 _cairo_ft_unscaled_font_unlock_face (unscaled);
1865 _cairo_unscaled_font_destroy (&unscaled->base);
1871 metrics = &face->size->metrics;
1874 * Get to unscaled metrics so that the upper level can get back to
1877 * Also use this path for bitmap-only fonts. The other branch uses
1878 * face members that are only relevant for scalable fonts. This is
1879 * detected by simply checking for units_per_EM==0.
1881 if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
1882 face->units_per_EM == 0) {
1883 double x_factor, y_factor;
1885 if (unscaled->x_scale == 0)
1888 x_factor = 1 / unscaled->x_scale;
1890 if (unscaled->y_scale == 0)
1893 y_factor = 1 / unscaled->y_scale;
1895 fs_metrics.ascent = DOUBLE_FROM_26_6(metrics->ascender) * y_factor;
1896 fs_metrics.descent = DOUBLE_FROM_26_6(- metrics->descender) * y_factor;
1897 fs_metrics.height = DOUBLE_FROM_26_6(metrics->height) * y_factor;
1898 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
1899 fs_metrics.max_x_advance = DOUBLE_FROM_26_6(metrics->max_advance) * x_factor;
1900 fs_metrics.max_y_advance = 0;
1902 fs_metrics.max_x_advance = 0;
1903 fs_metrics.max_y_advance = DOUBLE_FROM_26_6(metrics->max_advance) * y_factor;
1906 double scale = face->units_per_EM;
1908 fs_metrics.ascent = face->ascender / scale;
1909 fs_metrics.descent = - face->descender / scale;
1910 fs_metrics.height = face->height / scale;
1911 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
1912 fs_metrics.max_x_advance = face->max_advance_width / scale;
1913 fs_metrics.max_y_advance = 0;
1915 fs_metrics.max_x_advance = 0;
1916 fs_metrics.max_y_advance = face->max_advance_height / scale;
1920 status = _cairo_scaled_font_set_metrics (&scaled_font->base, &fs_metrics);
1921 if (unlikely (status))
1922 goto CLEANUP_SCALED_FONT;
1924 _cairo_ft_unscaled_font_unlock_face (unscaled);
1926 *font_out = &scaled_font->base;
1927 return CAIRO_STATUS_SUCCESS;
1929 CLEANUP_SCALED_FONT:
1930 _cairo_unscaled_font_destroy (&unscaled->base);
1933 _cairo_ft_unscaled_font_unlock_face (font_face->unscaled);
1934 *font_out = _cairo_scaled_font_create_in_error (status);
1935 return CAIRO_STATUS_SUCCESS; /* non-backend error */
1939 _cairo_scaled_font_is_ft (cairo_scaled_font_t *scaled_font)
1941 return scaled_font->backend == &_cairo_ft_scaled_font_backend;
1945 _cairo_ft_scaled_font_fini (void *abstract_font)
1947 cairo_ft_scaled_font_t *scaled_font = abstract_font;
1949 if (scaled_font == NULL)
1952 _cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
1956 _move_to (FT_Vector *to, void *closure)
1958 cairo_path_fixed_t *path = closure;
1961 x = _cairo_fixed_from_26_6 (to->x);
1962 y = _cairo_fixed_from_26_6 (to->y);
1964 if (_cairo_path_fixed_close_path (path) != CAIRO_STATUS_SUCCESS)
1966 if (_cairo_path_fixed_move_to (path, x, y) != CAIRO_STATUS_SUCCESS)
1973 _line_to (FT_Vector *to, void *closure)
1975 cairo_path_fixed_t *path = closure;
1978 x = _cairo_fixed_from_26_6 (to->x);
1979 y = _cairo_fixed_from_26_6 (to->y);
1981 if (_cairo_path_fixed_line_to (path, x, y) != CAIRO_STATUS_SUCCESS)
1988 _conic_to (FT_Vector *control, FT_Vector *to, void *closure)
1990 cairo_path_fixed_t *path = closure;
1992 cairo_fixed_t x0, y0;
1993 cairo_fixed_t x1, y1;
1994 cairo_fixed_t x2, y2;
1995 cairo_fixed_t x3, y3;
1996 cairo_point_t conic;
1998 if (! _cairo_path_fixed_get_current_point (path, &x0, &y0))
2001 conic.x = _cairo_fixed_from_26_6 (control->x);
2002 conic.y = _cairo_fixed_from_26_6 (control->y);
2004 x3 = _cairo_fixed_from_26_6 (to->x);
2005 y3 = _cairo_fixed_from_26_6 (to->y);
2007 x1 = x0 + 2.0/3.0 * (conic.x - x0);
2008 y1 = y0 + 2.0/3.0 * (conic.y - y0);
2010 x2 = x3 + 2.0/3.0 * (conic.x - x3);
2011 y2 = y3 + 2.0/3.0 * (conic.y - y3);
2013 if (_cairo_path_fixed_curve_to (path,
2016 x3, y3) != CAIRO_STATUS_SUCCESS)
2023 _cubic_to (FT_Vector *control1, FT_Vector *control2,
2024 FT_Vector *to, void *closure)
2026 cairo_path_fixed_t *path = closure;
2027 cairo_fixed_t x0, y0;
2028 cairo_fixed_t x1, y1;
2029 cairo_fixed_t x2, y2;
2031 x0 = _cairo_fixed_from_26_6 (control1->x);
2032 y0 = _cairo_fixed_from_26_6 (control1->y);
2034 x1 = _cairo_fixed_from_26_6 (control2->x);
2035 y1 = _cairo_fixed_from_26_6 (control2->y);
2037 x2 = _cairo_fixed_from_26_6 (to->x);
2038 y2 = _cairo_fixed_from_26_6 (to->y);
2040 if (_cairo_path_fixed_curve_to (path,
2043 x2, y2) != CAIRO_STATUS_SUCCESS)
2049 static cairo_status_t
2050 _decompose_glyph_outline (FT_Face face,
2051 cairo_font_options_t *options,
2052 cairo_path_fixed_t **pathp)
2054 static const FT_Outline_Funcs outline_funcs = {
2055 (FT_Outline_MoveToFunc)_move_to,
2056 (FT_Outline_LineToFunc)_line_to,
2057 (FT_Outline_ConicToFunc)_conic_to,
2058 (FT_Outline_CubicToFunc)_cubic_to,
2062 static const FT_Matrix invert_y = {
2063 DOUBLE_TO_16_16 (1.0), 0,
2064 0, DOUBLE_TO_16_16 (-1.0),
2068 cairo_path_fixed_t *path;
2069 cairo_status_t status;
2071 path = _cairo_path_fixed_create ();
2073 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2075 glyph = face->glyph;
2077 /* Font glyphs have an inverted Y axis compared to cairo. */
2078 FT_Outline_Transform (&glyph->outline, &invert_y);
2079 if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
2080 _cairo_path_fixed_destroy (path);
2081 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2084 status = _cairo_path_fixed_close_path (path);
2085 if (unlikely (status)) {
2086 _cairo_path_fixed_destroy (path);
2092 return CAIRO_STATUS_SUCCESS;
2096 * Translate glyph to match its metrics.
2099 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (void *abstract_font,
2102 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2105 vector.x = glyph->metrics.vertBearingX - glyph->metrics.horiBearingX;
2106 vector.y = -glyph->metrics.vertBearingY - glyph->metrics.horiBearingY;
2108 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2109 FT_Vector_Transform (&vector, &scaled_font->unscaled->Current_Shape);
2110 FT_Outline_Translate(&glyph->outline, vector.x, vector.y);
2111 } else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
2112 glyph->bitmap_left += vector.x / 64;
2113 glyph->bitmap_top += vector.y / 64;
2117 static cairo_int_status_t
2118 _cairo_ft_scaled_glyph_init (void *abstract_font,
2119 cairo_scaled_glyph_t *scaled_glyph,
2120 cairo_scaled_glyph_info_t info)
2122 cairo_text_extents_t fs_metrics;
2123 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2124 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2128 int load_flags = scaled_font->ft_options.load_flags;
2129 FT_Glyph_Metrics *metrics;
2130 double x_factor, y_factor;
2131 cairo_bool_t vertical_layout = FALSE;
2132 cairo_status_t status;
2134 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2136 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2138 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
2139 &scaled_font->base.scale);
2140 if (unlikely (status))
2143 /* Ignore global advance unconditionally */
2144 load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
2146 if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0 &&
2147 (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) == 0)
2148 load_flags |= FT_LOAD_NO_BITMAP;
2151 * Don't pass FT_LOAD_VERTICAL_LAYOUT to FT_Load_Glyph here as
2152 * suggested by freetype people.
2154 if (load_flags & FT_LOAD_VERTICAL_LAYOUT) {
2155 load_flags &= ~FT_LOAD_VERTICAL_LAYOUT;
2156 vertical_layout = TRUE;
2159 error = FT_Load_Glyph (face,
2160 _cairo_scaled_glyph_index(scaled_glyph),
2162 /* XXX ignoring all other errors for now. They are not fatal, typically
2163 * just a glyph-not-found. */
2164 if (error == FT_Err_Out_Of_Memory) {
2165 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2169 glyph = face->glyph;
2172 * synthesize glyphs if requested
2174 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
2175 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2176 FT_GlyphSlot_Embolden (glyph);
2179 #if HAVE_FT_GLYPHSLOT_OBLIQUE
2180 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2181 FT_GlyphSlot_Oblique (glyph);
2184 if (vertical_layout)
2185 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, glyph);
2187 if (info & CAIRO_SCALED_GLYPH_INFO_METRICS) {
2189 cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
2191 * Compute font-space metrics
2193 metrics = &glyph->metrics;
2195 if (unscaled->x_scale == 0)
2198 x_factor = 1 / unscaled->x_scale;
2200 if (unscaled->y_scale == 0)
2203 y_factor = 1 / unscaled->y_scale;
2206 * Note: Y coordinates of the horizontal bearing need to be negated.
2208 * Scale metrics back to glyph space from the scaled glyph space returned
2211 * If we want hinted metrics but aren't asking for hinted glyphs from
2212 * FreeType, then we need to do the metric hinting ourselves.
2215 if (hint_metrics && (load_flags & FT_LOAD_NO_HINTING))
2221 if (!vertical_layout) {
2222 x1 = (metrics->horiBearingX) & -64;
2223 x2 = (metrics->horiBearingX + metrics->width + 63) & -64;
2224 y1 = (-metrics->horiBearingY) & -64;
2225 y2 = (-metrics->horiBearingY + metrics->height + 63) & -64;
2227 advance = ((metrics->horiAdvance + 32) & -64);
2229 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2230 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2232 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2233 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2235 fs_metrics.x_advance = DOUBLE_FROM_26_6 (advance) * x_factor;
2236 fs_metrics.y_advance = 0;
2238 x1 = (metrics->vertBearingX) & -64;
2239 x2 = (metrics->vertBearingX + metrics->width + 63) & -64;
2240 y1 = (metrics->vertBearingY) & -64;
2241 y2 = (metrics->vertBearingY + metrics->height + 63) & -64;
2243 advance = ((metrics->vertAdvance + 32) & -64);
2245 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2246 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2248 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2249 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2251 fs_metrics.x_advance = 0;
2252 fs_metrics.y_advance = DOUBLE_FROM_26_6 (advance) * y_factor;
2255 fs_metrics.width = DOUBLE_FROM_26_6 (metrics->width) * x_factor;
2256 fs_metrics.height = DOUBLE_FROM_26_6 (metrics->height) * y_factor;
2258 if (!vertical_layout) {
2259 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->horiBearingX) * x_factor;
2260 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (-metrics->horiBearingY) * y_factor;
2262 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2263 fs_metrics.x_advance = DOUBLE_FROM_26_6 (metrics->horiAdvance) * x_factor;
2265 fs_metrics.x_advance = DOUBLE_FROM_16_16 (glyph->linearHoriAdvance) * x_factor;
2266 fs_metrics.y_advance = 0 * y_factor;
2268 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingX) * x_factor;
2269 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingY) * y_factor;
2271 fs_metrics.x_advance = 0 * x_factor;
2272 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2273 fs_metrics.y_advance = DOUBLE_FROM_26_6 (metrics->vertAdvance) * y_factor;
2275 fs_metrics.y_advance = DOUBLE_FROM_16_16 (glyph->linearVertAdvance) * y_factor;
2279 _cairo_scaled_glyph_set_metrics (scaled_glyph,
2284 if ((info & CAIRO_SCALED_GLYPH_INFO_SURFACE) != 0) {
2285 cairo_image_surface_t *surface;
2287 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2288 status = _render_glyph_outline (face, &scaled_font->ft_options.base,
2291 status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
2293 if (likely (status == CAIRO_STATUS_SUCCESS) &&
2294 unscaled->have_shape)
2296 status = _transform_glyph_bitmap (&unscaled->current_shape,
2298 if (unlikely (status))
2299 cairo_surface_destroy (&surface->base);
2302 if (unlikely (status))
2305 _cairo_scaled_glyph_set_surface (scaled_glyph,
2310 if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
2311 cairo_path_fixed_t *path = NULL; /* hide compiler warning */
2314 * A kludge -- the above code will trash the outline,
2315 * so reload it. This will probably never occur though
2317 if ((info & CAIRO_SCALED_GLYPH_INFO_SURFACE) != 0) {
2318 error = FT_Load_Glyph (face,
2319 _cairo_scaled_glyph_index(scaled_glyph),
2320 load_flags | FT_LOAD_NO_BITMAP);
2321 /* XXX ignoring all other errors for now. They are not fatal, typically
2322 * just a glyph-not-found. */
2323 if (error == FT_Err_Out_Of_Memory) {
2324 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2327 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
2328 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2329 FT_GlyphSlot_Embolden (glyph);
2331 #if HAVE_FT_GLYPHSLOT_OBLIQUE
2332 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2333 FT_GlyphSlot_Oblique (glyph);
2335 if (vertical_layout)
2336 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, glyph);
2339 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
2340 status = _decompose_glyph_outline (face, &scaled_font->ft_options.base,
2343 status = CAIRO_INT_STATUS_UNSUPPORTED;
2345 if (unlikely (status))
2348 _cairo_scaled_glyph_set_path (scaled_glyph,
2353 _cairo_ft_unscaled_font_unlock_face (unscaled);
2358 static unsigned long
2359 _cairo_ft_ucs4_to_index (void *abstract_font,
2362 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2363 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2367 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2371 #if CAIRO_HAS_FC_FONT
2372 index = FcFreeTypeCharIndex (face, ucs4);
2374 index = FT_Get_Char_Index (face, ucs4);
2377 _cairo_ft_unscaled_font_unlock_face (unscaled);
2381 static cairo_int_status_t
2382 _cairo_ft_load_truetype_table (void *abstract_font,
2385 unsigned char *buffer,
2386 unsigned long *length)
2388 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2389 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2391 cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
2393 /* We don't support the FreeType feature of loading a table
2394 * without specifying the size since this may overflow our
2396 assert (length != NULL);
2398 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
2399 return CAIRO_INT_STATUS_UNSUPPORTED;
2401 #if HAVE_FT_LOAD_SFNT_TABLE
2402 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2404 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2406 if (FT_IS_SFNT (face)) {
2410 if (FT_Load_Sfnt_Table (face, tag, offset, buffer, length) == 0)
2411 status = CAIRO_STATUS_SUCCESS;
2414 _cairo_ft_unscaled_font_unlock_face (unscaled);
2420 static cairo_int_status_t
2421 _cairo_ft_index_to_ucs4(void *abstract_font,
2422 unsigned long index,
2425 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2426 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2431 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2433 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2435 *ucs4 = (uint32_t) -1;
2436 charcode = FT_Get_First_Char(face, &gindex);
2437 while (gindex != 0) {
2438 if (gindex == index) {
2442 charcode = FT_Get_Next_Char (face, charcode, &gindex);
2445 _cairo_ft_unscaled_font_unlock_face (unscaled);
2447 return CAIRO_STATUS_SUCCESS;
2451 _cairo_ft_is_synthetic (void *abstract_font)
2453 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2454 return scaled_font->ft_options.synth_flags != 0;
2457 static cairo_int_status_t
2458 _cairo_index_to_glyph_name (void *abstract_font,
2460 int num_glyph_names,
2461 unsigned long glyph_index,
2462 unsigned long *glyph_array_index)
2464 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2465 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2467 char buffer[256]; /* PLRM spcifies max name length of 127 */
2471 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2473 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2475 error = FT_Get_Glyph_Name (face, glyph_index, buffer, sizeof buffer);
2477 _cairo_ft_unscaled_font_unlock_face (unscaled);
2479 if (error != FT_Err_Ok) {
2480 /* propagate fatal errors from FreeType */
2481 if (error == FT_Err_Out_Of_Memory)
2482 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2484 return CAIRO_INT_STATUS_UNSUPPORTED;
2487 /* FT first numbers the glyphs in the order they are read from the
2488 * Type 1 font. Then if .notdef is not the first glyph, the first
2489 * glyph is swapped with .notdef to ensure that .notdef is at
2492 * As all but two glyphs in glyph_names already have the same
2493 * index as the FT glyph index, we first check if
2494 * glyph_names[glyph_index] is the name we are looking for. If not
2495 * we fall back to searching the entire array.
2498 if ((long)glyph_index < num_glyph_names &&
2499 strcmp (glyph_names[glyph_index], buffer) == 0)
2501 *glyph_array_index = glyph_index;
2503 return CAIRO_STATUS_SUCCESS;
2506 for (i = 0; i < num_glyph_names; i++) {
2507 if (strcmp (glyph_names[i], buffer) == 0) {
2508 *glyph_array_index = i;
2510 return CAIRO_STATUS_SUCCESS;
2514 return CAIRO_INT_STATUS_UNSUPPORTED;
2518 _ft_is_type1 (FT_Face face)
2520 #if HAVE_FT_GET_X11_FONT_FORMAT
2521 const char *font_format = FT_Get_X11_Font_Format (face);
2523 (strcmp (font_format, "Type 1") == 0 ||
2524 strcmp (font_format, "CFF") == 0))
2533 static cairo_int_status_t
2534 _cairo_ft_load_type1_data (void *abstract_font,
2536 unsigned char *buffer,
2537 unsigned long *length)
2539 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2540 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2542 cairo_status_t status = CAIRO_STATUS_SUCCESS;
2543 unsigned long available_length;
2546 assert (length != NULL);
2548 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
2549 return CAIRO_INT_STATUS_UNSUPPORTED;
2551 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2553 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2555 #if HAVE_FT_LOAD_SFNT_TABLE
2556 if (FT_IS_SFNT (face)) {
2557 status = CAIRO_INT_STATUS_UNSUPPORTED;
2562 if (! _ft_is_type1 (face)) {
2563 status = CAIRO_INT_STATUS_UNSUPPORTED;
2567 available_length = MAX (face->stream->size - offset, 0);
2569 *length = available_length;
2571 if (*length > available_length) {
2572 status = CAIRO_INT_STATUS_UNSUPPORTED;
2573 } else if (face->stream->read != NULL) {
2574 /* Note that read() may be implemented as a macro, thanks POSIX!, so we
2575 * need to wrap the following usage in parentheses in order to
2576 * disambiguate it for the pre-processor - using the verbose function
2577 * pointer dereference for clarity.
2579 ret = (* face->stream->read) (face->stream,
2584 status = _cairo_error (CAIRO_STATUS_READ_ERROR);
2586 memcpy (buffer, face->stream->base + offset, *length);
2591 _cairo_ft_unscaled_font_unlock_face (unscaled);
2596 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend = {
2598 _cairo_ft_scaled_font_fini,
2599 _cairo_ft_scaled_glyph_init,
2600 NULL, /* text_to_glyphs */
2601 _cairo_ft_ucs4_to_index,
2602 _cairo_ft_load_truetype_table,
2603 _cairo_ft_index_to_ucs4,
2604 _cairo_ft_is_synthetic,
2605 _cairo_index_to_glyph_name,
2606 _cairo_ft_load_type1_data
2609 /* #cairo_ft_font_face_t */
2611 #if CAIRO_HAS_FC_FONT
2612 static cairo_font_face_t *
2613 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern);
2615 static cairo_status_t
2616 _cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
2617 cairo_font_face_t **font_face_out)
2619 cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
2624 pattern = FcPatternCreate ();
2626 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2627 return font_face->status;
2630 if (!FcPatternAddString (pattern,
2631 FC_FAMILY, (unsigned char *) toy_face->family))
2633 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2637 switch (toy_face->slant)
2639 case CAIRO_FONT_SLANT_ITALIC:
2640 fcslant = FC_SLANT_ITALIC;
2642 case CAIRO_FONT_SLANT_OBLIQUE:
2643 fcslant = FC_SLANT_OBLIQUE;
2645 case CAIRO_FONT_SLANT_NORMAL:
2647 fcslant = FC_SLANT_ROMAN;
2651 if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
2652 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2656 switch (toy_face->weight)
2658 case CAIRO_FONT_WEIGHT_BOLD:
2659 fcweight = FC_WEIGHT_BOLD;
2661 case CAIRO_FONT_WEIGHT_NORMAL:
2663 fcweight = FC_WEIGHT_MEDIUM;
2667 if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
2668 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2672 font_face = _cairo_ft_font_face_create_for_pattern (pattern);
2675 FcPatternDestroy (pattern);
2677 *font_face_out = font_face;
2678 return font_face->status;
2683 _cairo_ft_font_face_destroy (void *abstract_face)
2685 cairo_ft_font_face_t *font_face = abstract_face;
2687 /* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
2688 * we have a special "zombie" state for the face when the unscaled font
2689 * is still alive but there are no other references to a font face with
2694 * font_face ------> unscaled
2699 * font_face <------- unscaled
2702 if (font_face->unscaled &&
2703 font_face->unscaled->from_face &&
2704 font_face->next == NULL &&
2705 font_face->unscaled->faces == font_face &&
2706 CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->unscaled->base.ref_count) > 1)
2708 cairo_font_face_reference (&font_face->base);
2710 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
2711 font_face->unscaled = NULL;
2716 if (font_face->unscaled) {
2717 cairo_ft_font_face_t *tmp_face = NULL;
2718 cairo_ft_font_face_t *last_face = NULL;
2720 /* Remove face from linked list */
2721 for (tmp_face = font_face->unscaled->faces;
2723 tmp_face = tmp_face->next)
2725 if (tmp_face == font_face) {
2727 last_face->next = tmp_face->next;
2729 font_face->unscaled->faces = tmp_face->next;
2732 last_face = tmp_face;
2735 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
2736 font_face->unscaled = NULL;
2739 #if CAIRO_HAS_FC_FONT
2740 if (font_face->pattern) {
2741 FcPatternDestroy (font_face->pattern);
2742 cairo_font_face_destroy (font_face->resolved_font_face);
2747 static cairo_font_face_t *
2748 _cairo_ft_font_face_get_implementation (void *abstract_face,
2749 const cairo_matrix_t *font_matrix,
2750 const cairo_matrix_t *ctm,
2751 const cairo_font_options_t *options)
2753 cairo_ft_font_face_t *font_face = abstract_face;
2755 /* The handling of font options is different depending on how the
2756 * font face was created. When the user creates a font face with
2757 * cairo_ft_font_face_create_for_ft_face(), then the load flags
2758 * passed in augment the load flags for the options. But for
2759 * cairo_ft_font_face_create_for_pattern(), the load flags are
2760 * derived from a pattern where the user has called
2761 * cairo_ft_font_options_substitute(), so *just* use those load
2762 * flags and ignore the options.
2765 #if CAIRO_HAS_FC_FONT
2766 /* If we have an unresolved pattern, resolve it and create
2767 * unscaled font. Otherwise, use the ones stored in font_face.
2769 if (font_face->pattern) {
2770 cairo_font_face_t *resolved;
2772 /* Cache the resolved font whilst the FcConfig remains consistent. */
2773 resolved = font_face->resolved_font_face;
2774 if (resolved != NULL) {
2775 if (! FcInitBringUptoDate ()) {
2776 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2777 return (cairo_font_face_t *) &_cairo_font_face_nil;
2780 if (font_face->resolved_config == FcConfigGetCurrent ())
2781 return cairo_font_face_reference (resolved);
2783 cairo_font_face_destroy (resolved);
2784 font_face->resolved_font_face = NULL;
2787 resolved = _cairo_ft_resolve_pattern (font_face->pattern,
2791 if (unlikely (resolved->status))
2794 font_face->resolved_font_face = cairo_font_face_reference (resolved);
2795 font_face->resolved_config = FcConfigGetCurrent ();
2801 return abstract_face;
2804 const cairo_font_face_backend_t _cairo_ft_font_face_backend = {
2806 #if CAIRO_HAS_FC_FONT
2807 _cairo_ft_font_face_create_for_toy,
2811 _cairo_ft_font_face_destroy,
2812 _cairo_ft_font_face_scaled_font_create,
2813 _cairo_ft_font_face_get_implementation
2816 #if CAIRO_HAS_FC_FONT
2817 static cairo_font_face_t *
2818 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
2820 cairo_ft_font_face_t *font_face;
2822 font_face = malloc (sizeof (cairo_ft_font_face_t));
2823 if (unlikely (font_face == NULL)) {
2824 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2825 return (cairo_font_face_t *) &_cairo_font_face_nil;
2828 font_face->unscaled = NULL;
2829 font_face->next = NULL;
2831 font_face->pattern = FcPatternDuplicate (pattern);
2832 if (unlikely (font_face->pattern == NULL)) {
2834 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2835 return (cairo_font_face_t *) &_cairo_font_face_nil;
2838 font_face->resolved_font_face = NULL;
2839 font_face->resolved_config = NULL;
2841 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
2843 return &font_face->base;
2847 static cairo_font_face_t *
2848 _cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
2849 cairo_ft_options_t *ft_options)
2851 cairo_ft_font_face_t *font_face, **prev_font_face;
2853 /* Looked for an existing matching font face */
2854 for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
2856 prev_font_face = &font_face->next, font_face = font_face->next)
2858 if (font_face->ft_options.load_flags == ft_options->load_flags &&
2859 font_face->ft_options.synth_flags == ft_options->synth_flags &&
2860 cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
2862 if (font_face->base.status) {
2863 /* The font_face has been left in an error state, abandon it. */
2864 *prev_font_face = font_face->next;
2868 if (font_face->unscaled == NULL) {
2869 /* Resurrect this "zombie" font_face (from
2870 * _cairo_ft_font_face_destroy), switching its unscaled_font
2871 * from owner to ownee. */
2872 font_face->unscaled = unscaled;
2873 _cairo_unscaled_font_reference (&unscaled->base);
2874 return &font_face->base;
2876 return cairo_font_face_reference (&font_face->base);
2880 /* No match found, create a new one */
2881 font_face = malloc (sizeof (cairo_ft_font_face_t));
2882 if (unlikely (!font_face)) {
2883 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
2884 return (cairo_font_face_t *)&_cairo_font_face_nil;
2887 font_face->unscaled = unscaled;
2888 _cairo_unscaled_font_reference (&unscaled->base);
2890 font_face->ft_options = *ft_options;
2892 if (unscaled->faces && unscaled->faces->unscaled == NULL) {
2893 /* This "zombie" font_face (from _cairo_ft_font_face_destroy)
2894 * is no longer needed. */
2895 assert (unscaled->from_face && unscaled->faces->next == NULL);
2896 cairo_font_face_destroy (&unscaled->faces->base);
2897 unscaled->faces = NULL;
2900 font_face->next = unscaled->faces;
2901 unscaled->faces = font_face;
2903 #if CAIRO_HAS_FC_FONT
2904 font_face->pattern = NULL;
2907 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
2909 return &font_face->base;
2912 /* implement the platform-specific interface */
2914 #if CAIRO_HAS_FC_FONT
2915 static cairo_status_t
2916 _cairo_ft_font_options_substitute (const cairo_font_options_t *options,
2921 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
2923 if (FcPatternGet (pattern, FC_ANTIALIAS, 0, &v) == FcResultNoMatch)
2925 if (! FcPatternAddBool (pattern,
2927 options->antialias != CAIRO_ANTIALIAS_NONE))
2928 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2930 if (options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
2931 FcPatternDel (pattern, FC_RGBA);
2932 if (! FcPatternAddInteger (pattern, FC_RGBA, FC_RGBA_NONE))
2933 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2938 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
2940 if (FcPatternGet (pattern, FC_RGBA, 0, &v) == FcResultNoMatch)
2944 if (options->antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
2945 switch (options->subpixel_order) {
2946 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
2947 case CAIRO_SUBPIXEL_ORDER_RGB:
2951 case CAIRO_SUBPIXEL_ORDER_BGR:
2954 case CAIRO_SUBPIXEL_ORDER_VRGB:
2955 rgba = FC_RGBA_VRGB;
2957 case CAIRO_SUBPIXEL_ORDER_VBGR:
2958 rgba = FC_RGBA_VBGR;
2962 rgba = FC_RGBA_NONE;
2965 if (! FcPatternAddInteger (pattern, FC_RGBA, rgba))
2966 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2970 if (options->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
2972 if (FcPatternGet (pattern, FC_LCD_FILTER, 0, &v) == FcResultNoMatch)
2976 switch (options->lcd_filter) {
2977 case CAIRO_LCD_FILTER_NONE:
2978 lcd_filter = FT_LCD_FILTER_NONE;
2980 case CAIRO_LCD_FILTER_DEFAULT:
2981 case CAIRO_LCD_FILTER_INTRA_PIXEL:
2982 lcd_filter = FT_LCD_FILTER_LEGACY;
2984 case CAIRO_LCD_FILTER_FIR3:
2985 lcd_filter = FT_LCD_FILTER_LIGHT;
2988 case CAIRO_LCD_FILTER_FIR5:
2989 lcd_filter = FT_LCD_FILTER_DEFAULT;
2993 if (! FcPatternAddInteger (pattern, FC_LCD_FILTER, lcd_filter))
2994 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2998 if (options->hint_style != CAIRO_HINT_STYLE_DEFAULT)
3000 if (FcPatternGet (pattern, FC_HINTING, 0, &v) == FcResultNoMatch)
3002 if (! FcPatternAddBool (pattern,
3004 options->hint_style != CAIRO_HINT_STYLE_NONE))
3005 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3008 #ifdef FC_HINT_STYLE
3009 if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
3013 switch (options->hint_style) {
3014 case CAIRO_HINT_STYLE_NONE:
3015 hint_style = FC_HINT_NONE;
3017 case CAIRO_HINT_STYLE_SLIGHT:
3018 hint_style = FC_HINT_SLIGHT;
3020 case CAIRO_HINT_STYLE_MEDIUM:
3021 hint_style = FC_HINT_MEDIUM;
3023 case CAIRO_HINT_STYLE_FULL:
3024 case CAIRO_HINT_STYLE_DEFAULT:
3026 hint_style = FC_HINT_FULL;
3030 if (! FcPatternAddInteger (pattern, FC_HINT_STYLE, hint_style))
3031 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3036 return CAIRO_STATUS_SUCCESS;
3040 * cairo_ft_font_options_substitute:
3041 * @options: a #cairo_font_options_t object
3042 * @pattern: an existing #FcPattern
3044 * Add options to a #FcPattern based on a #cairo_font_options_t font
3045 * options object. Options that are already in the pattern, are not overridden,
3046 * so you should call this function after calling FcConfigSubstitute() (the
3047 * user's settings should override options based on the surface type), but
3048 * before calling FcDefaultSubstitute().
3053 cairo_ft_font_options_substitute (const cairo_font_options_t *options,
3056 if (cairo_font_options_status ((cairo_font_options_t *) options))
3059 _cairo_ft_font_options_substitute (options, pattern);
3062 static cairo_font_face_t *
3063 _cairo_ft_resolve_pattern (FcPattern *pattern,
3064 const cairo_matrix_t *font_matrix,
3065 const cairo_matrix_t *ctm,
3066 const cairo_font_options_t *font_options)
3068 cairo_status_t status;
3070 cairo_matrix_t scale;
3071 FcPattern *resolved;
3072 cairo_ft_font_transform_t sf;
3074 cairo_ft_unscaled_font_t *unscaled;
3075 cairo_ft_options_t ft_options;
3076 cairo_font_face_t *font_face;
3079 scale.x0 = scale.y0 = 0;
3080 cairo_matrix_multiply (&scale,
3084 status = _compute_transform (&sf, &scale, NULL);
3085 if (unlikely (status))
3086 return (cairo_font_face_t *)&_cairo_font_face_nil;
3088 pattern = FcPatternDuplicate (pattern);
3089 if (pattern == NULL)
3090 return (cairo_font_face_t *)&_cairo_font_face_nil;
3092 if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
3093 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3097 if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
3098 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3102 status = _cairo_ft_font_options_substitute (font_options, pattern);
3104 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3108 FcDefaultSubstitute (pattern);
3110 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3111 if (unlikely (status)) {
3112 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3116 if (unscaled == NULL) {
3117 resolved = FcFontMatch (NULL, pattern, &result);
3119 /* We failed to find any font. Substitute twin so that the user can
3120 * see something (and hopefully recognise that the font is missing)
3121 * and not just receive a NO_MEMORY error during rendering.
3123 font_face = _cairo_font_face_twin_create_fallback ();
3127 status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
3128 if (unlikely (status || unscaled == NULL)) {
3129 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3135 _get_pattern_ft_options (resolved, &ft_options);
3136 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3137 _cairo_unscaled_font_destroy (&unscaled->base);
3140 if (resolved != pattern)
3141 FcPatternDestroy (resolved);
3144 FcPatternDestroy (pattern);
3150 * cairo_ft_font_face_create_for_pattern:
3151 * @pattern: A fontconfig pattern. Cairo makes a copy of the pattern
3152 * if it needs to. You are free to modify or free @pattern after this call.
3154 * Creates a new font face for the FreeType font backend based on a
3155 * fontconfig pattern. This font can then be used with
3156 * cairo_set_font_face() or cairo_scaled_font_create(). The
3157 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3158 * also for the FreeType backend and can be used with functions such
3159 * as cairo_ft_scaled_font_lock_face().
3161 * Font rendering options are represented both here and when you
3162 * call cairo_scaled_font_create(). Font options that have a representation
3163 * in a #FcPattern must be passed in here; to modify #FcPattern
3164 * appropriately to reflect the options in a #cairo_font_options_t, call
3165 * cairo_ft_font_options_substitute().
3167 * The pattern's FC_FT_FACE element is inspected first and if that is set,
3168 * that will be the FreeType font face associated with the returned cairo
3169 * font face. Otherwise the FC_FILE element is checked. If it's set,
3170 * that and the value of the FC_INDEX element (defaults to zero) of @pattern
3171 * are used to load a font face from file.
3173 * If both steps from the previous paragraph fails, @pattern will be passed
3174 * to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
3175 * and the resulting font pattern is used.
3177 * If the FC_FT_FACE element of @pattern is set, the user is responsible
3178 * for making sure that the referenced FT_Face remains valid for the life
3179 * time of the returned #cairo_font_face_t. See
3180 * cairo_ft_font_face_create_for_ft_face() for an example of how to couple
3181 * the life time of the FT_Face to that of the cairo font-face.
3183 * Return value: a newly created #cairo_font_face_t. Free with
3184 * cairo_font_face_destroy() when you are done using it.
3189 cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
3191 cairo_ft_unscaled_font_t *unscaled;
3192 cairo_font_face_t *font_face;
3193 cairo_ft_options_t ft_options;
3194 cairo_status_t status;
3196 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3197 if (unlikely (status))
3198 return (cairo_font_face_t *) &_cairo_font_face_nil;
3199 if (unlikely (unscaled == NULL)) {
3200 /* Store the pattern. We will resolve it and create unscaled
3201 * font when creating scaled fonts */
3202 return _cairo_ft_font_face_create_for_pattern (pattern);
3205 _get_pattern_ft_options (pattern, &ft_options);
3206 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3207 _cairo_unscaled_font_destroy (&unscaled->base);
3214 * cairo_ft_font_face_create_for_ft_face:
3215 * @face: A FreeType face object, already opened. This must
3216 * be kept around until the face's ref_count drops to
3217 * zero and it is freed. Since the face may be referenced
3218 * internally to Cairo, the best way to determine when it
3219 * is safe to free the face is to pass a
3220 * #cairo_destroy_func_t to cairo_font_face_set_user_data()
3221 * @load_flags: flags to pass to FT_Load_Glyph when loading
3222 * glyphs from the font. These flags are OR'ed together with
3223 * the flags derived from the #cairo_font_options_t passed
3224 * to cairo_scaled_font_create(), so only a few values such
3225 * as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
3226 * are useful. You should not pass any of the flags affecting
3227 * the load target, such as %FT_LOAD_TARGET_LIGHT.
3229 * Creates a new font face for the FreeType font backend from a
3230 * pre-opened FreeType face. This font can then be used with
3231 * cairo_set_font_face() or cairo_scaled_font_create(). The
3232 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3233 * also for the FreeType backend and can be used with functions such
3234 * as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
3235 * to the FT_Face alive in a font-cache and the exact lifetime of the reference
3236 * depends highly upon the exact usage pattern and is subject to external
3237 * factors. You must not call FT_Done_Face() before the last reference to the
3238 * #cairo_font_face_t has been dropped.
3240 * As an example, below is how one might correctly couple the lifetime of
3241 * the FreeType face object to the #cairo_font_face_t.
3243 * <informalexample><programlisting>
3244 * static const cairo_user_data_key_t key;
3246 * font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
3247 * status = cairo_font_face_set_user_data (font_face, &key,
3248 * ft_face, (cairo_destroy_func_t) FT_Done_Face);
3250 * cairo_font_face_destroy (font_face);
3251 * FT_Done_Face (ft_face);
3254 * </programlisting></informalexample>
3256 * Return value: a newly created #cairo_font_face_t. Free with
3257 * cairo_font_face_destroy() when you are done using it.
3262 cairo_ft_font_face_create_for_ft_face (FT_Face face,
3265 cairo_ft_unscaled_font_t *unscaled;
3266 cairo_font_face_t *font_face;
3267 cairo_ft_options_t ft_options;
3268 cairo_status_t status;
3270 status = _cairo_ft_unscaled_font_create_from_face (face, &unscaled);
3271 if (unlikely (status))
3272 return (cairo_font_face_t *)&_cairo_font_face_nil;
3274 ft_options.load_flags = load_flags;
3275 ft_options.synth_flags = 0;
3276 _cairo_font_options_init_default (&ft_options.base);
3278 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3279 _cairo_unscaled_font_destroy (&unscaled->base);
3285 * cairo_ft_font_face_set_synthesize:
3286 * @font_face: The #cairo_ft_font_face_t object to modify
3287 * @synth_flags: the set of synthesis options to enable
3289 * FreeType provides the ability to synthesize different glyphs from a base
3290 * font, which is useful if you lack those glyphs from a true bold or oblique
3291 * font. See also #cairo_ft_synthesize_t.
3296 cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
3297 unsigned int synth_flags)
3299 cairo_ft_font_face_t *ft;
3301 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3304 ft = (cairo_ft_font_face_t *) font_face;
3305 ft->ft_options.synth_flags |= synth_flags;
3309 * cairo_ft_font_face_unset_synthesize:
3310 * @font_face: The #cairo_ft_font_face_t object to modify
3311 * @synth_flags: the set of synthesis options to disable
3313 * See cairo_ft_font_face_set_synthesize().
3318 cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
3319 unsigned int synth_flags)
3321 cairo_ft_font_face_t *ft;
3323 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3326 ft = (cairo_ft_font_face_t *) font_face;
3327 ft->ft_options.synth_flags &= ~synth_flags;
3331 * cairo_ft_font_face_get_synthesize:
3332 * @font_face: The #cairo_ft_font_face_t object to query
3334 * See #cairo_ft_synthesize_t.
3336 * Returns: the current set of synthesis options.
3341 cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
3343 cairo_ft_font_face_t *ft;
3345 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3348 ft = (cairo_ft_font_face_t *) font_face;
3349 return ft->ft_options.synth_flags;
3353 * cairo_ft_scaled_font_lock_face:
3354 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
3355 * object can be created by calling cairo_scaled_font_create() on a
3356 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
3357 * cairo_ft_font_face_create_for_ft_face()).
3359 * cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
3360 * backend font and scales it appropriately for the font. You must
3361 * release the face with cairo_ft_scaled_font_unlock_face()
3362 * when you are done using it. Since the #FT_Face object can be
3363 * shared between multiple #cairo_scaled_font_t objects, you must not
3364 * lock any other font objects until you unlock this one. A count is
3365 * kept of the number of times cairo_ft_scaled_font_lock_face() is
3366 * called. cairo_ft_scaled_font_unlock_face() must be called the same number
3369 * You must be careful when using this function in a library or in a
3370 * threaded application, because freetype's design makes it unsafe to
3371 * call freetype functions simultaneously from multiple threads, (even
3372 * if using distinct FT_Face objects). Because of this, application
3373 * code that acquires an FT_Face object with this call must add its
3374 * own locking to protect any use of that object, (and which also must
3375 * protect any other calls into cairo as almost any cairo function
3376 * might result in a call into the freetype library).
3378 * Return value: The #FT_Face object for @font, scaled appropriately,
3379 * or %NULL if @scaled_font is in an error state (see
3380 * cairo_scaled_font_status()) or there is insufficient memory.
3385 cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
3387 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
3389 cairo_status_t status;
3391 if (! _cairo_scaled_font_is_ft (abstract_font)) {
3392 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
3396 if (scaled_font->base.status)
3399 face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled);
3400 if (unlikely (face == NULL)) {
3401 status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY);
3405 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
3406 &scaled_font->base.scale);
3407 if (unlikely (status)) {
3408 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
3409 status = _cairo_scaled_font_set_error (&scaled_font->base, status);
3413 /* Note: We deliberately release the unscaled font's mutex here,
3414 * so that we are not holding a lock across two separate calls to
3415 * cairo function, (which would give the application some
3416 * opportunity for creating deadlock. This is obviously unsafe,
3417 * but as documented, the user must add manual locking when using
3419 CAIRO_MUTEX_UNLOCK (scaled_font->unscaled->mutex);
3425 * cairo_ft_scaled_font_unlock_face:
3426 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
3427 * object can be created by calling cairo_scaled_font_create() on a
3428 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
3429 * cairo_ft_font_face_create_for_ft_face()).
3431 * Releases a face obtained with cairo_ft_scaled_font_lock_face().
3436 cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
3438 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
3440 if (! _cairo_scaled_font_is_ft (abstract_font)) {
3441 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
3445 if (scaled_font->base.status)
3448 /* Note: We released the unscaled font's mutex at the end of
3449 * cairo_ft_scaled_font_lock_face, so we have to acquire it again
3450 * as _cairo_ft_unscaled_font_unlock_face expects it to be held
3451 * when we call into it. */
3452 CAIRO_MUTEX_LOCK (scaled_font->unscaled->mutex);
3454 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
3458 _cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font)
3460 cairo_ft_scaled_font_t *ft_scaled_font;
3462 if (!_cairo_scaled_font_is_ft (scaled_font))
3465 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
3466 if (ft_scaled_font->ft_options.load_flags & FT_LOAD_VERTICAL_LAYOUT)
3472 _cairo_ft_scaled_font_get_load_flags (cairo_scaled_font_t *scaled_font)
3474 cairo_ft_scaled_font_t *ft_scaled_font;
3476 if (! _cairo_scaled_font_is_ft (scaled_font))
3479 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
3480 return ft_scaled_font->ft_options.load_flags;
3484 _cairo_ft_font_reset_static_data (void)
3486 _cairo_ft_unscaled_font_map_destroy ();