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
3 *
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2005 Red Hat, Inc
6 *
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.
14 *
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
20 *
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/
25 *
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.
29 *
30 * The Original Code is the cairo graphics library.
31 *
32 * The Initial Developer of the Original Code is Red Hat, Inc.
33 *
34 * Contributor(s):
35 * Graydon Hoare <graydon@redhat.com>
36 * Owen Taylor <otaylor@redhat.com>
37 * Keith Packard <keithp@keithp.com>
38 * Carl Worth <cworth@cworth.org>
39 */
40
41 #define _DEFAULT_SOURCE /* for strdup() */
42 #include "cairoint.h"
43
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"
48 #include "cairo-pixman-private.h"
49
50 #include <float.h>
51
52 #include "cairo-fontconfig-private.h"
53
54 #include <ft2build.h>
55 #include FT_FREETYPE_H
56 #include FT_OUTLINE_H
57 #include FT_IMAGE_H
58 #include FT_BITMAP_H
59 #include FT_TRUETYPE_TABLES_H
60 #include FT_XFREE86_H
61 #include FT_MULTIPLE_MASTERS_H
62 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
63 #include FT_SYNTHESIS_H
64 #endif
65
66 #if HAVE_FT_LIBRARY_SETLCDFILTER
67 #include FT_LCD_FILTER_H
68 #endif
69
70 #if HAVE_UNISTD_H
71 #include <unistd.h>
72 #else
73 #define access(p, m) 0
74 #endif
75 #include <dlfcn.h>
76
77 /* Fontconfig version older than 2.6 didn't have these options */
78 #ifndef FC_LCD_FILTER
79 #define FC_LCD_FILTER "lcdfilter"
80 #endif
81 /* Some Ubuntu versions defined FC_LCD_FILTER without defining the following */
82 #ifndef FC_LCD_NONE
83 #define FC_LCD_NONE 0
84 #define FC_LCD_DEFAULT 1
85 #define FC_LCD_LIGHT 2
86 #define FC_LCD_LEGACY 3
87 #endif
88
89 /* FreeType version older than 2.3.5(?) didn't have these options */
90 #ifndef FT_LCD_FILTER_NONE
91 #define FT_LCD_FILTER_NONE 0
92 #define FT_LCD_FILTER_DEFAULT 1
93 #define FT_LCD_FILTER_LIGHT 2
94 #define FT_LCD_FILTER_LEGACY 16
95 #endif
96
97 #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
98 #define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0))
99 #define DOUBLE_FROM_16_16(t) ((double)(t) / 65536.0)
100
101 /* This is the max number of FT_face objects we keep open at once
102 */
103 #define MAX_OPEN_FACES 10
104
105 extern void mozilla_AddRefSharedFTFace(void* aContext);
106 extern void mozilla_ReleaseSharedFTFace(void* aContext, void* aOwner);
107 /* Returns true if the face's state has been modified by another owner. */
108 extern int mozilla_LockSharedFTFace(void* aContext, void* aOwner);
109 extern void mozilla_UnlockSharedFTFace(void* aContext);
110 extern FT_Error mozilla_LoadFTGlyph(FT_Face aFace, uint32_t aGlyphIndex, int32_t aFlags);
111 extern void mozilla_LockFTLibrary(FT_Library aLibrary);
112 extern void mozilla_UnlockFTLibrary(FT_Library aLibrary);
113
114 #define CAIRO_FT_LOCK(unscaled) \
115 ((unscaled)->face_context \
116 ? (void)mozilla_LockSharedFTFace((unscaled)->face_context, NULL) \
117 : (void)CAIRO_MUTEX_LOCK((unscaled)->mutex))
118 #define CAIRO_FT_UNLOCK(unscaled) \
119 ((unscaled)->face_context \
120 ? mozilla_UnlockSharedFTFace((unscaled)->face_context) \
121 : (void)CAIRO_MUTEX_UNLOCK((unscaled)->mutex))
122
123 /**
124 * Function types for FreeType symbols we'll look up at runtime, rather than
125 * relying on build-time checks for availability.
126 */
127 typedef FT_Error (*GetVarFunc) (FT_Face, FT_MM_Var**);
128 typedef FT_Error (*DoneVarFunc) (FT_Library, FT_MM_Var*);
129 typedef FT_Error (*GetVarDesignCoordsFunc) (FT_Face, FT_UInt, FT_Fixed*);
130 typedef FT_Error (*SetVarDesignCoordsFunc) (FT_Face, FT_UInt, FT_Fixed*);
131 typedef FT_Error (*GetVarBlendCoordsFunc) (FT_Face, FT_UInt, FT_Fixed*);
132
133 /**
134 * SECTION:cairo-ft
135 * @Title: FreeType Fonts
136 * @Short_Description: Font support for FreeType
137 * @See_Also: #cairo_font_face_t
138 *
139 * The FreeType font backend is primarily used to render text on GNU/Linux
140 * systems, but can be used on other platforms too.
141 **/
142
143 /**
144 * CAIRO_HAS_FT_FONT:
145 *
146 * Defined if the FreeType font backend is available.
147 * This macro can be used to conditionally compile backend-specific code.
148 *
149 * Since: 1.0
150 **/
151
152 /**
153 * CAIRO_HAS_FC_FONT:
154 *
155 * Defined if the Fontconfig-specific functions of the FreeType font backend
156 * are available.
157 * This macro can be used to conditionally compile backend-specific code.
158 *
159 * Since: 1.10
160 **/
161
162 /*
163 * The simple 2x2 matrix is converted into separate scale and shape
164 * factors so that hinting works right
165 */
166
167 typedef struct _cairo_ft_font_transform {
168 double x_scale, y_scale;
169 double shape[2][2];
170 } cairo_ft_font_transform_t;
171
172 /*
173 * We create an object that corresponds to a single font on the disk;
174 * (identified by a filename/id pair) these are shared between all
175 * fonts using that file. For cairo_ft_font_face_create_for_ft_face(), we
176 * just create a one-off version with a permanent face value.
177 */
178
179 typedef struct _cairo_ft_font_face cairo_ft_font_face_t;
180
181 struct _cairo_ft_unscaled_font {
182 cairo_unscaled_font_t base;
183
184 cairo_bool_t from_face; /* was the FT_Face provided by user? */
185 FT_Face face; /* provided or cached face */
186 void *face_context;
187
188 /* only set if from_face is false */
189 char *filename;
190 int id;
191
192 /* We temporarily scale the unscaled font as needed */
193 cairo_bool_t have_scale;
194 cairo_matrix_t current_scale;
195 double x_scale; /* Extracted X scale factor */
196 double y_scale; /* Extracted Y scale factor */
197 cairo_bool_t have_shape; /* true if the current scale has a non-scale component*/
198 cairo_matrix_t current_shape;
199 FT_Matrix Current_Shape;
200
201 unsigned int have_color_set : 1;
202 unsigned int have_color : 1; /* true if the font contains color glyphs */
203 FT_Fixed *variations; /* variation settings that FT_Face came */
204
205 cairo_mutex_t mutex;
206 int lock_count;
207
208 cairo_ft_font_face_t *faces; /* Linked list of faces for this font */
209 };
210
211 static int
212 _cairo_ft_unscaled_font_keys_equal (const void *key_a,
213 const void *key_b);
214
215 static void
216 _cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled);
217
218 typedef struct _cairo_ft_options {
219 cairo_font_options_t base;
220 unsigned int load_flags; /* flags for FT_Load_Glyph */
221 unsigned int synth_flags;
222 } cairo_ft_options_t;
223
224 static void
_cairo_ft_options_init_copy(cairo_ft_options_t * options,const cairo_ft_options_t * other)225 _cairo_ft_options_init_copy (cairo_ft_options_t *options,
226 const cairo_ft_options_t *other)
227 {
228 _cairo_font_options_init_copy (&options->base, &other->base);
229 options->load_flags = other->load_flags;
230 options->synth_flags = other->synth_flags;
231 }
232
233 static void
_cairo_ft_options_fini(cairo_ft_options_t * options)234 _cairo_ft_options_fini (cairo_ft_options_t *options)
235 {
236 _cairo_font_options_fini (&options->base);
237 }
238
239 struct _cairo_ft_font_face {
240 cairo_font_face_t base;
241
242 cairo_ft_unscaled_font_t *unscaled;
243 cairo_ft_options_t ft_options;
244 cairo_ft_font_face_t *next;
245
246 #if CAIRO_HAS_FC_FONT
247 FcPattern *pattern; /* if pattern is set, the above fields will be NULL */
248 cairo_font_face_t *resolved_font_face;
249 FcConfig *resolved_config;
250 #endif
251 };
252
253 static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend;
254
255 #if CAIRO_HAS_FC_FONT
256 static cairo_status_t
257 _cairo_ft_font_options_substitute (const cairo_font_options_t *options,
258 FcPattern *pattern);
259
260 static cairo_font_face_t *
261 _cairo_ft_resolve_pattern (FcPattern *pattern,
262 const cairo_matrix_t *font_matrix,
263 const cairo_matrix_t *ctm,
264 const cairo_font_options_t *options);
265
266 #endif
267
268 static cairo_status_t
_ft_to_cairo_error(FT_Error error)269 _ft_to_cairo_error (FT_Error error)
270 {
271 /* Currently we don't get many (any?) useful statuses here.
272 * Populate as needed. */
273 switch (error)
274 {
275 case FT_Err_Out_Of_Memory:
276 return CAIRO_STATUS_NO_MEMORY;
277 default:
278 return CAIRO_STATUS_FREETYPE_ERROR;
279 }
280 }
281
282 /*
283 * We maintain a hash table to map file/id => #cairo_ft_unscaled_font_t.
284 * The hash table itself isn't limited in size. However, we limit the
285 * number of FT_Face objects we keep around; when we've exceeded that
286 * limit and need to create a new FT_Face, we dump the FT_Face from a
287 * random #cairo_ft_unscaled_font_t which has an unlocked FT_Face, (if
288 * there are any).
289 */
290
291 typedef struct _cairo_ft_unscaled_font_map {
292 cairo_hash_table_t *hash_table;
293 FT_Library ft_library;
294 int num_open_faces;
295 } cairo_ft_unscaled_font_map_t;
296
297 static cairo_ft_unscaled_font_map_t *cairo_ft_unscaled_font_map = NULL;
298
299
300 static FT_Face
301 _cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled);
302
303 static void
304 _cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled);
305
306 static cairo_bool_t
307 _cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font);
308
309
310 static void
_font_map_release_face_lock_held(cairo_ft_unscaled_font_map_t * font_map,cairo_ft_unscaled_font_t * unscaled)311 _font_map_release_face_lock_held (cairo_ft_unscaled_font_map_t *font_map,
312 cairo_ft_unscaled_font_t *unscaled)
313 {
314 if (unscaled->face) {
315 FT_Done_Face (unscaled->face);
316 unscaled->face = NULL;
317 unscaled->have_scale = FALSE;
318
319 font_map->num_open_faces--;
320 }
321 }
322
323 static cairo_status_t
_cairo_ft_unscaled_font_map_create(void)324 _cairo_ft_unscaled_font_map_create (void)
325 {
326 cairo_ft_unscaled_font_map_t *font_map;
327
328 /* This function is only intended to be called from
329 * _cairo_ft_unscaled_font_map_lock. So we'll crash if we can
330 * detect some other call path. */
331 assert (cairo_ft_unscaled_font_map == NULL);
332
333 font_map = _cairo_malloc (sizeof (cairo_ft_unscaled_font_map_t));
334 if (unlikely (font_map == NULL))
335 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
336
337 font_map->hash_table =
338 _cairo_hash_table_create (_cairo_ft_unscaled_font_keys_equal);
339
340 if (unlikely (font_map->hash_table == NULL))
341 goto FAIL;
342
343 if (unlikely (FT_Init_FreeType (&font_map->ft_library)))
344 goto FAIL;
345
346 font_map->num_open_faces = 0;
347
348 cairo_ft_unscaled_font_map = font_map;
349 return CAIRO_STATUS_SUCCESS;
350
351 FAIL:
352 if (font_map->hash_table)
353 _cairo_hash_table_destroy (font_map->hash_table);
354 free (font_map);
355
356 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
357 }
358
359
360 static void
_cairo_ft_unscaled_font_map_pluck_entry(void * entry,void * closure)361 _cairo_ft_unscaled_font_map_pluck_entry (void *entry, void *closure)
362 {
363 cairo_ft_unscaled_font_t *unscaled = entry;
364 cairo_ft_unscaled_font_map_t *font_map = closure;
365
366 _cairo_hash_table_remove (font_map->hash_table,
367 &unscaled->base.hash_entry);
368
369 if (unscaled->from_face)
370 mozilla_ReleaseSharedFTFace (unscaled->face_context, unscaled);
371 else
372 _font_map_release_face_lock_held (font_map, unscaled);
373
374 _cairo_ft_unscaled_font_fini (unscaled);
375 free (unscaled);
376 }
377
378 static void
_cairo_ft_unscaled_font_map_destroy(void)379 _cairo_ft_unscaled_font_map_destroy (void)
380 {
381 cairo_ft_unscaled_font_map_t *font_map;
382
383 CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
384 font_map = cairo_ft_unscaled_font_map;
385 cairo_ft_unscaled_font_map = NULL;
386 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
387
388 if (font_map != NULL) {
389 _cairo_hash_table_foreach (font_map->hash_table,
390 _cairo_ft_unscaled_font_map_pluck_entry,
391 font_map);
392 assert (font_map->num_open_faces == 0);
393
394 FT_Done_FreeType (font_map->ft_library);
395
396 _cairo_hash_table_destroy (font_map->hash_table);
397
398 free (font_map);
399 }
400 }
401
402 static cairo_ft_unscaled_font_map_t *
_cairo_ft_unscaled_font_map_lock(void)403 _cairo_ft_unscaled_font_map_lock (void)
404 {
405 CAIRO_MUTEX_INITIALIZE ();
406
407 CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
408
409 if (unlikely (cairo_ft_unscaled_font_map == NULL)) {
410 if (unlikely (_cairo_ft_unscaled_font_map_create ())) {
411 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
412 return NULL;
413 }
414 }
415
416 return cairo_ft_unscaled_font_map;
417 }
418
419 static void
_cairo_ft_unscaled_font_map_unlock(void)420 _cairo_ft_unscaled_font_map_unlock (void)
421 {
422 CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
423 }
424
425 static void
_cairo_ft_unscaled_font_init_key(cairo_ft_unscaled_font_t * key,cairo_bool_t from_face,char * filename,int id,FT_Face face,void * face_context)426 _cairo_ft_unscaled_font_init_key (cairo_ft_unscaled_font_t *key,
427 cairo_bool_t from_face,
428 char *filename,
429 int id,
430 FT_Face face,
431 void *face_context)
432 {
433 unsigned long hash;
434
435 key->from_face = from_face;
436 key->filename = filename;
437 key->id = id;
438 key->face = face;
439 key->face_context = face_context;
440
441 hash = _cairo_hash_string (filename);
442 /* the constants are just arbitrary primes */
443 hash += ((unsigned long) id) * 1607;
444 hash += ((unsigned long) face) * 2137;
445
446 key->base.hash_entry.hash = hash;
447 }
448
449 /**
450 * _cairo_ft_unscaled_font_init:
451 *
452 * Initialize a #cairo_ft_unscaled_font_t.
453 *
454 * There are two basic flavors of #cairo_ft_unscaled_font_t, one
455 * created from an FT_Face and the other created from a filename/id
456 * pair. These two flavors are identified as from_face and !from_face.
457 *
458 * To initialize a from_face font, pass filename==%NULL, id=0 and the
459 * desired face.
460 *
461 * To initialize a !from_face font, pass the filename/id as desired
462 * and face==%NULL.
463 *
464 * Note that the code handles these two flavors in very distinct
465 * ways. For example there is a hash_table mapping
466 * filename/id->#cairo_unscaled_font_t in the !from_face case, but no
467 * parallel in the from_face case, (where the calling code would have
468 * to do its own mapping to ensure similar sharing).
469 **/
470 static cairo_status_t
_cairo_ft_unscaled_font_init(cairo_ft_unscaled_font_t * unscaled,cairo_bool_t from_face,const char * filename,int id,FT_Face face,void * face_context)471 _cairo_ft_unscaled_font_init (cairo_ft_unscaled_font_t *unscaled,
472 cairo_bool_t from_face,
473 const char *filename,
474 int id,
475 FT_Face face,
476 void *face_context)
477 {
478 _cairo_unscaled_font_init (&unscaled->base,
479 &cairo_ft_unscaled_font_backend);
480
481 unscaled->variations = NULL;
482
483 if (from_face) {
484 unscaled->from_face = TRUE;
485 _cairo_ft_unscaled_font_init_key (unscaled, TRUE, NULL, id, face, face_context);
486
487
488 unscaled->have_color = FT_HAS_COLOR (face) != 0;
489 unscaled->have_color_set = TRUE;
490
491 static GetVarFunc getVar;
492 static DoneVarFunc doneVar;
493 static GetVarDesignCoordsFunc getVarDesignCoords;
494
495 static int firstTime = 1;
496 if (firstTime) {
497 getVar = (GetVarFunc) dlsym (RTLD_DEFAULT, "FT_Get_MM_Var");
498 doneVar = (DoneVarFunc) dlsym (RTLD_DEFAULT, "FT_Done_MM_Var");
499 getVarDesignCoords = (GetVarDesignCoordsFunc) dlsym (RTLD_DEFAULT, "FT_Get_Var_Design_Coordinates");
500 firstTime = 0;
501 }
502
503 if (getVar && getVarDesignCoords) {
504 FT_MM_Var *ft_mm_var;
505 if (0 == (*getVar) (face, &ft_mm_var))
506 {
507 unscaled->variations = calloc (ft_mm_var->num_axis, sizeof (FT_Fixed));
508 if (unscaled->variations)
509 (*getVarDesignCoords) (face, ft_mm_var->num_axis, unscaled->variations);
510 if (doneVar)
511 (*doneVar) (face->glyph->library, ft_mm_var);
512 else
513 free (ft_mm_var);
514 }
515 }
516 } else {
517 char *filename_copy;
518
519 unscaled->from_face = FALSE;
520 unscaled->face = NULL;
521 unscaled->face_context = NULL;
522
523 filename_copy = strdup (filename);
524 if (unlikely (filename_copy == NULL))
525 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
526
527 _cairo_ft_unscaled_font_init_key (unscaled, FALSE, filename_copy, id, NULL, NULL);
528
529 unscaled->have_color_set = FALSE;
530 }
531
532 unscaled->have_scale = FALSE;
533 CAIRO_MUTEX_INIT (unscaled->mutex);
534 unscaled->lock_count = 0;
535
536 unscaled->faces = NULL;
537
538 return CAIRO_STATUS_SUCCESS;
539 }
540
541 /**
542 * _cairo_ft_unscaled_font_fini:
543 *
544 * Free all data associated with a #cairo_ft_unscaled_font_t.
545 *
546 * CAUTION: The unscaled->face field must be %NULL before calling this
547 * function. This is because the #cairo_ft_unscaled_font_t_map keeps a
548 * count of these faces (font_map->num_open_faces) so it maintains the
549 * unscaled->face field while it has its lock held. See
550 * _font_map_release_face_lock_held().
551 **/
552 static void
_cairo_ft_unscaled_font_fini(cairo_ft_unscaled_font_t * unscaled)553 _cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled)
554 {
555 assert (unscaled->face == NULL);
556
557 free (unscaled->filename);
558 unscaled->filename = NULL;
559
560 free (unscaled->variations);
561
562 CAIRO_MUTEX_FINI (unscaled->mutex);
563 }
564
565 static int
_cairo_ft_unscaled_font_keys_equal(const void * key_a,const void * key_b)566 _cairo_ft_unscaled_font_keys_equal (const void *key_a,
567 const void *key_b)
568 {
569 const cairo_ft_unscaled_font_t *unscaled_a = key_a;
570 const cairo_ft_unscaled_font_t *unscaled_b = key_b;
571
572 if (unscaled_a->id == unscaled_b->id &&
573 unscaled_a->from_face == unscaled_b->from_face)
574 {
575 if (unscaled_a->from_face)
576 return unscaled_a->face == unscaled_b->face &&
577 unscaled_a->face_context == unscaled_b->face_context;
578
579 if (unscaled_a->filename == NULL && unscaled_b->filename == NULL)
580 return TRUE;
581 else if (unscaled_a->filename == NULL || unscaled_b->filename == NULL)
582 return FALSE;
583 else
584 return (strcmp (unscaled_a->filename, unscaled_b->filename) == 0);
585 }
586
587 return FALSE;
588 }
589
590 /* Finds or creates a #cairo_ft_unscaled_font_t for the filename/id from
591 * pattern. Returns a new reference to the unscaled font.
592 */
593 static cairo_status_t
_cairo_ft_unscaled_font_create_internal(cairo_bool_t from_face,char * filename,int id,FT_Face font_face,void * face_context,cairo_ft_unscaled_font_t ** out)594 _cairo_ft_unscaled_font_create_internal (cairo_bool_t from_face,
595 char *filename,
596 int id,
597 FT_Face font_face,
598 void *face_context,
599 cairo_ft_unscaled_font_t **out)
600 {
601 cairo_ft_unscaled_font_t key, *unscaled;
602 cairo_ft_unscaled_font_map_t *font_map;
603 cairo_status_t status;
604
605 font_map = _cairo_ft_unscaled_font_map_lock ();
606 if (unlikely (font_map == NULL))
607 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
608
609 _cairo_ft_unscaled_font_init_key (&key, from_face, filename, id, font_face, face_context);
610
611 /* Return existing unscaled font if it exists in the hash table. */
612 unscaled = _cairo_hash_table_lookup (font_map->hash_table,
613 &key.base.hash_entry);
614 if (unscaled != NULL) {
615 _cairo_unscaled_font_reference (&unscaled->base);
616 goto DONE;
617 }
618
619 /* Otherwise create it and insert into hash table. */
620 unscaled = _cairo_malloc (sizeof (cairo_ft_unscaled_font_t));
621 if (unlikely (unscaled == NULL)) {
622 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
623 goto UNWIND_FONT_MAP_LOCK;
624 }
625
626 status = _cairo_ft_unscaled_font_init (unscaled, from_face, filename, id, font_face, face_context);
627 if (unlikely (status))
628 goto UNWIND_UNSCALED_MALLOC;
629
630 assert (unscaled->base.hash_entry.hash == key.base.hash_entry.hash);
631 status = _cairo_hash_table_insert (font_map->hash_table,
632 &unscaled->base.hash_entry);
633 if (unlikely (status))
634 goto UNWIND_UNSCALED_FONT_INIT;
635
636 mozilla_AddRefSharedFTFace (face_context);
637
638 DONE:
639 _cairo_ft_unscaled_font_map_unlock ();
640 *out = unscaled;
641 return CAIRO_STATUS_SUCCESS;
642
643 UNWIND_UNSCALED_FONT_INIT:
644 _cairo_ft_unscaled_font_fini (unscaled);
645 UNWIND_UNSCALED_MALLOC:
646 free (unscaled);
647 UNWIND_FONT_MAP_LOCK:
648 _cairo_ft_unscaled_font_map_unlock ();
649 return status;
650 }
651
652
653 #if CAIRO_HAS_FC_FONT
654 static cairo_status_t
_cairo_ft_unscaled_font_create_for_pattern(FcPattern * pattern,cairo_ft_unscaled_font_t ** out)655 _cairo_ft_unscaled_font_create_for_pattern (FcPattern *pattern,
656 cairo_ft_unscaled_font_t **out)
657 {
658 FT_Face font_face = NULL;
659 char *filename = NULL;
660 int id = 0;
661 FcResult ret;
662
663 ret = FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &font_face);
664 if (ret == FcResultMatch)
665 goto DONE;
666 if (ret == FcResultOutOfMemory)
667 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
668
669 ret = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **) &filename);
670 if (ret == FcResultOutOfMemory)
671 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
672 if (ret == FcResultMatch) {
673 if (access (filename, R_OK) == 0) {
674 /* If FC_INDEX is not set, we just use 0 */
675 ret = FcPatternGetInteger (pattern, FC_INDEX, 0, &id);
676 if (ret == FcResultOutOfMemory)
677 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
678
679 goto DONE;
680 } else
681 return _cairo_error (CAIRO_STATUS_FILE_NOT_FOUND);
682 }
683
684 /* The pattern contains neither a face nor a filename, resolve it later. */
685 *out = NULL;
686 return CAIRO_STATUS_SUCCESS;
687
688 DONE:
689 return _cairo_ft_unscaled_font_create_internal (font_face != NULL,
690 filename, id, font_face, NULL,
691 out);
692 }
693 #endif
694
695 static cairo_status_t
_cairo_ft_unscaled_font_create_from_face(FT_Face face,void * face_context,cairo_ft_unscaled_font_t ** out)696 _cairo_ft_unscaled_font_create_from_face (FT_Face face,
697 void *face_context,
698 cairo_ft_unscaled_font_t **out)
699 {
700 return _cairo_ft_unscaled_font_create_internal (TRUE, NULL, face->face_index, face, face_context, out);
701 }
702
703 static cairo_bool_t
_cairo_ft_unscaled_font_destroy(void * abstract_font)704 _cairo_ft_unscaled_font_destroy (void *abstract_font)
705 {
706 cairo_ft_unscaled_font_t *unscaled = abstract_font;
707 cairo_ft_unscaled_font_map_t *font_map;
708
709 font_map = _cairo_ft_unscaled_font_map_lock ();
710 /* All created objects must have been mapped in the font map. */
711 assert (font_map != NULL);
712
713 if (! _cairo_reference_count_dec_and_test (&unscaled->base.ref_count)) {
714 /* somebody recreated the font whilst we waited for the lock */
715 _cairo_ft_unscaled_font_map_unlock ();
716 return FALSE;
717 }
718
719 _cairo_hash_table_remove (font_map->hash_table,
720 &unscaled->base.hash_entry);
721
722 if (unscaled->from_face) {
723 /* See comments in _ft_font_face_destroy about the "zombie" state
724 * for a _ft_font_face.
725 */
726 if (unscaled->faces && unscaled->faces->unscaled == NULL) {
727 assert (unscaled->faces->next == NULL);
728 CAIRO_FT_LOCK (unscaled);
729 cairo_font_face_destroy (&unscaled->faces->base);
730 CAIRO_FT_UNLOCK (unscaled);
731 }
732 mozilla_ReleaseSharedFTFace (unscaled->face_context, unscaled);
733 } else {
734 _font_map_release_face_lock_held (font_map, unscaled);
735 }
736 unscaled->face = NULL;
737 unscaled->face_context = NULL;
738
739 _cairo_ft_unscaled_font_map_unlock ();
740
741 _cairo_ft_unscaled_font_fini (unscaled);
742 return TRUE;
743 }
744
745 static cairo_bool_t
_has_unlocked_face(const void * entry)746 _has_unlocked_face (const void *entry)
747 {
748 const cairo_ft_unscaled_font_t *unscaled = entry;
749
750 return (!unscaled->from_face && unscaled->lock_count == 0 && unscaled->face);
751 }
752
753 /* Ensures that an unscaled font has a face object. If we exceed
754 * MAX_OPEN_FACES, try to close some.
755 *
756 * This differs from _cairo_ft_scaled_font_lock_face in that it doesn't
757 * set the scale on the face, but just returns it at the last scale.
758 */
759 static cairo_warn FT_Face
_cairo_ft_unscaled_font_lock_face(cairo_ft_unscaled_font_t * unscaled)760 _cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
761 {
762 cairo_ft_unscaled_font_map_t *font_map;
763 FT_Face face = NULL;
764 FT_Error error;
765
766 if (unscaled->face_context) {
767 if (!mozilla_LockSharedFTFace (unscaled->face_context, unscaled)) {
768 unscaled->have_scale = FALSE;
769 }
770 } else {
771 CAIRO_FT_LOCK (unscaled);
772 }
773 unscaled->lock_count++;
774
775 if (unscaled->face)
776 return unscaled->face;
777
778 /* If this unscaled font was created from an FT_Face then we just
779 * returned it above. */
780 assert (!unscaled->from_face);
781
782 font_map = _cairo_ft_unscaled_font_map_lock ();
783 {
784 assert (font_map != NULL);
785
786 while (font_map->num_open_faces >= MAX_OPEN_FACES)
787 {
788 cairo_ft_unscaled_font_t *entry;
789
790 entry = _cairo_hash_table_random_entry (font_map->hash_table,
791 _has_unlocked_face);
792 if (entry == NULL)
793 break;
794
795 _font_map_release_face_lock_held (font_map, entry);
796 }
797 }
798 _cairo_ft_unscaled_font_map_unlock ();
799
800 error = FT_New_Face (font_map->ft_library,
801 unscaled->filename,
802 unscaled->id,
803 &face);
804 if (error)
805 {
806 unscaled->lock_count--;
807 CAIRO_FT_UNLOCK (unscaled);
808 _cairo_error_throw (_ft_to_cairo_error (error));
809 return NULL;
810 }
811
812 unscaled->face = face;
813
814 unscaled->have_color = FT_HAS_COLOR (face) != 0;
815 unscaled->have_color_set = TRUE;
816
817 font_map->num_open_faces++;
818
819 return face;
820 }
821
822
823 /* Unlock unscaled font locked with _cairo_ft_unscaled_font_lock_face
824 */
825 static void
_cairo_ft_unscaled_font_unlock_face(cairo_ft_unscaled_font_t * unscaled)826 _cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled)
827 {
828 assert (unscaled->lock_count > 0);
829
830 unscaled->lock_count--;
831
832 CAIRO_FT_UNLOCK (unscaled);
833 }
834
835
836 static cairo_status_t
_compute_transform(cairo_ft_font_transform_t * sf,cairo_matrix_t * scale,cairo_ft_unscaled_font_t * unscaled)837 _compute_transform (cairo_ft_font_transform_t *sf,
838 cairo_matrix_t *scale,
839 cairo_ft_unscaled_font_t *unscaled)
840 {
841 cairo_status_t status;
842 double x_scale, y_scale;
843 cairo_matrix_t normalized = *scale;
844
845 /* The font matrix has x and y "scale" components which we extract and
846 * use as character scale values. These influence the way freetype
847 * chooses hints, as well as selecting different bitmaps in
848 * hand-rendered fonts. We also copy the normalized matrix to
849 * freetype's transformation.
850 */
851
852 status = _cairo_matrix_compute_basis_scale_factors (scale,
853 &x_scale, &y_scale,
854 1);
855 if (unlikely (status))
856 return status;
857
858 /* FreeType docs say this about x_scale and y_scale:
859 * "A character width or height smaller than 1pt is set to 1pt;"
860 * So, we cap them from below at 1.0 and let the FT transform
861 * take care of sub-1.0 scaling. */
862 if (x_scale < 1.0)
863 x_scale = 1.0;
864 if (y_scale < 1.0)
865 y_scale = 1.0;
866
867 if (unscaled && (unscaled->face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) {
868 double min_distance = DBL_MAX;
869 cairo_bool_t magnify = TRUE;
870 int i;
871 double best_x_size = 0;
872 double best_y_size = 0;
873
874 for (i = 0; i < unscaled->face->num_fixed_sizes; i++) {
875 double x_size = unscaled->face->available_sizes[i].x_ppem / 64.;
876 double y_size = unscaled->face->available_sizes[i].y_ppem / 64.;
877 double distance = y_size - y_scale;
878
879 /*
880 * distance is positive if current strike is larger than desired
881 * size, and negative if smaller.
882 *
883 * We like to prefer down-scaling to upscaling.
884 */
885
886 if ((magnify && distance >= 0) || fabs (distance) <= min_distance) {
887 magnify = distance < 0;
888 min_distance = fabs (distance);
889 best_x_size = x_size;
890 best_y_size = y_size;
891 }
892 }
893
894 x_scale = best_x_size;
895 y_scale = best_y_size;
896 }
897
898 sf->x_scale = x_scale;
899 sf->y_scale = y_scale;
900
901 cairo_matrix_scale (&normalized, 1.0 / x_scale, 1.0 / y_scale);
902
903 _cairo_matrix_get_affine (&normalized,
904 &sf->shape[0][0], &sf->shape[0][1],
905 &sf->shape[1][0], &sf->shape[1][1],
906 NULL, NULL);
907
908 return CAIRO_STATUS_SUCCESS;
909 }
910
911 /* Temporarily scales an unscaled font to the give scale. We catch
912 * scaling to the same size, since changing a FT_Face is expensive.
913 */
914 static cairo_status_t
_cairo_ft_unscaled_font_set_scale(cairo_ft_unscaled_font_t * unscaled,cairo_matrix_t * scale)915 _cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
916 cairo_matrix_t *scale)
917 {
918 cairo_status_t status;
919 cairo_ft_font_transform_t sf;
920 FT_Matrix mat;
921 FT_Error error;
922
923 assert (unscaled->face != NULL);
924
925 if (unscaled->have_scale &&
926 scale->xx == unscaled->current_scale.xx &&
927 scale->yx == unscaled->current_scale.yx &&
928 scale->xy == unscaled->current_scale.xy &&
929 scale->yy == unscaled->current_scale.yy)
930 return CAIRO_STATUS_SUCCESS;
931
932 unscaled->have_scale = TRUE;
933 unscaled->current_scale = *scale;
934
935 status = _compute_transform (&sf, scale, unscaled);
936 if (unlikely (status))
937 return status;
938
939 unscaled->x_scale = sf.x_scale;
940 unscaled->y_scale = sf.y_scale;
941
942 mat.xx = DOUBLE_TO_16_16(sf.shape[0][0]);
943 mat.yx = - DOUBLE_TO_16_16(sf.shape[0][1]);
944 mat.xy = - DOUBLE_TO_16_16(sf.shape[1][0]);
945 mat.yy = DOUBLE_TO_16_16(sf.shape[1][1]);
946
947 unscaled->have_shape = (mat.xx != 0x10000 ||
948 mat.yx != 0x00000 ||
949 mat.xy != 0x00000 ||
950 mat.yy != 0x10000);
951
952 unscaled->Current_Shape = mat;
953 cairo_matrix_init (&unscaled->current_shape,
954 sf.shape[0][0], sf.shape[0][1],
955 sf.shape[1][0], sf.shape[1][1],
956 0.0, 0.0);
957
958 FT_Set_Transform(unscaled->face, &mat, NULL);
959
960 error = FT_Set_Char_Size (unscaled->face,
961 sf.x_scale * 64.0 + .5,
962 sf.y_scale * 64.0 + .5,
963 0, 0);
964 if (error)
965 return _cairo_error (_ft_to_cairo_error (error));
966
967 return CAIRO_STATUS_SUCCESS;
968 }
969
970 /* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
971 * into a different format. For example, we want to convert a
972 * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
973 * ARGB or ABGR bitmap.
974 *
975 * this function prepares a target descriptor for this operation.
976 *
977 * input :: target bitmap descriptor. The function will set its
978 * 'width', 'rows' and 'pitch' fields, and only these
979 *
980 * slot :: the glyph slot containing the source bitmap. this
981 * function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
982 *
983 * mode :: the requested final rendering mode. supported values are
984 * MONO, NORMAL (i.e. gray), LCD and LCD_V
985 *
986 * the function returns the size in bytes of the corresponding buffer,
987 * it's up to the caller to allocate the corresponding memory block
988 * before calling _fill_xrender_bitmap
989 *
990 * it also returns -1 in case of error (e.g. incompatible arguments,
991 * like trying to convert a gray bitmap into a monochrome one)
992 */
993 static int
_compute_xrender_bitmap_size(FT_Bitmap * target,FT_GlyphSlot slot,FT_Render_Mode mode)994 _compute_xrender_bitmap_size(FT_Bitmap *target,
995 FT_GlyphSlot slot,
996 FT_Render_Mode mode)
997 {
998 FT_Bitmap *ftbit;
999 int width, height, pitch;
1000
1001 if (slot->format != FT_GLYPH_FORMAT_BITMAP)
1002 return -1;
1003
1004 /* compute the size of the final bitmap */
1005 ftbit = &slot->bitmap;
1006
1007 width = ftbit->width;
1008 height = ftbit->rows;
1009 pitch = (width + 3) & ~3;
1010
1011 switch (ftbit->pixel_mode) {
1012 case FT_PIXEL_MODE_MONO:
1013 if (mode == FT_RENDER_MODE_MONO) {
1014 pitch = (((width + 31) & ~31) >> 3);
1015 break;
1016 }
1017 /* fall-through */
1018
1019 case FT_PIXEL_MODE_GRAY:
1020 if (mode == FT_RENDER_MODE_LCD ||
1021 mode == FT_RENDER_MODE_LCD_V)
1022 {
1023 /* each pixel is replicated into a 32-bit ARGB value */
1024 pitch = width * 4;
1025 }
1026 break;
1027
1028 case FT_PIXEL_MODE_LCD:
1029 if (mode != FT_RENDER_MODE_LCD)
1030 return -1;
1031
1032 /* horz pixel triplets are packed into 32-bit ARGB values */
1033 width /= 3;
1034 pitch = width * 4;
1035 break;
1036
1037 case FT_PIXEL_MODE_LCD_V:
1038 if (mode != FT_RENDER_MODE_LCD_V)
1039 return -1;
1040
1041 /* vert pixel triplets are packed into 32-bit ARGB values */
1042 height /= 3;
1043 pitch = width * 4;
1044 break;
1045
1046 #ifdef FT_LOAD_COLOR
1047 case FT_PIXEL_MODE_BGRA:
1048 /* each pixel is replicated into a 32-bit ARGB value */
1049 pitch = width * 4;
1050 break;
1051 #endif
1052
1053 default: /* unsupported source format */
1054 return -1;
1055 }
1056
1057 target->width = width;
1058 target->rows = height;
1059 target->pitch = pitch;
1060 target->buffer = NULL;
1061
1062 return pitch * height;
1063 }
1064
1065 /* this functions converts the glyph bitmap found in a FT_GlyphSlot
1066 * into a different format (see _compute_xrender_bitmap_size)
1067 *
1068 * you should call this function after _compute_xrender_bitmap_size
1069 *
1070 * target :: target bitmap descriptor. Note that its 'buffer' pointer
1071 * must point to memory allocated by the caller
1072 *
1073 * slot :: the glyph slot containing the source bitmap
1074 *
1075 * mode :: the requested final rendering mode
1076 *
1077 * bgr :: boolean, set if BGR or VBGR pixel ordering is needed
1078 */
1079 static void
_fill_xrender_bitmap(FT_Bitmap * target,FT_GlyphSlot slot,FT_Render_Mode mode,int bgr)1080 _fill_xrender_bitmap(FT_Bitmap *target,
1081 FT_GlyphSlot slot,
1082 FT_Render_Mode mode,
1083 int bgr)
1084 {
1085 FT_Bitmap *ftbit = &slot->bitmap;
1086 unsigned char *srcLine = ftbit->buffer;
1087 unsigned char *dstLine = target->buffer;
1088 int src_pitch = ftbit->pitch;
1089 int width = target->width;
1090 int height = target->rows;
1091 int pitch = target->pitch;
1092 int subpixel;
1093 int h;
1094
1095 subpixel = (mode == FT_RENDER_MODE_LCD ||
1096 mode == FT_RENDER_MODE_LCD_V);
1097
1098 if (src_pitch < 0)
1099 srcLine -= src_pitch * (ftbit->rows - 1);
1100
1101 target->pixel_mode = ftbit->pixel_mode;
1102
1103 switch (ftbit->pixel_mode) {
1104 case FT_PIXEL_MODE_MONO:
1105 if (subpixel) {
1106 /* convert mono to ARGB32 values */
1107
1108 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1109 int x;
1110
1111 for (x = 0; x < width; x++) {
1112 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
1113 ((unsigned int *) dstLine)[x] = 0xffffffffU;
1114 }
1115 }
1116 target->pixel_mode = FT_PIXEL_MODE_LCD;
1117
1118 } else if (mode == FT_RENDER_MODE_NORMAL) {
1119 /* convert mono to 8-bit gray */
1120
1121 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1122 int x;
1123
1124 for (x = 0; x < width; x++) {
1125 if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
1126 dstLine[x] = 0xff;
1127 }
1128 }
1129 target->pixel_mode = FT_PIXEL_MODE_GRAY;
1130
1131 } else {
1132 /* copy mono to mono */
1133
1134 int bytes = (width + 7) >> 3;
1135
1136 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1137 memcpy (dstLine, srcLine, bytes);
1138 }
1139 break;
1140
1141 case FT_PIXEL_MODE_GRAY:
1142 if (subpixel) {
1143 /* convert gray to ARGB32 values */
1144
1145 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1146 int x;
1147 unsigned int *dst = (unsigned int *) dstLine;
1148
1149 for (x = 0; x < width; x++) {
1150 unsigned int pix = srcLine[x];
1151
1152 pix |= (pix << 8);
1153 pix |= (pix << 16);
1154
1155 dst[x] = pix;
1156 }
1157 }
1158 target->pixel_mode = FT_PIXEL_MODE_LCD;
1159 } else {
1160 /* copy gray into gray */
1161
1162 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1163 memcpy (dstLine, srcLine, width);
1164 }
1165 break;
1166
1167 case FT_PIXEL_MODE_LCD:
1168 if (!bgr) {
1169 /* convert horizontal RGB into ARGB32 */
1170
1171 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1172 int x;
1173 unsigned char *src = srcLine;
1174 unsigned int *dst = (unsigned int *) dstLine;
1175
1176 for (x = 0; x < width; x++, src += 3) {
1177 unsigned int pix;
1178
1179 pix = ((unsigned int)src[0] << 16) |
1180 ((unsigned int)src[1] << 8) |
1181 ((unsigned int)src[2] ) |
1182 ((unsigned int)src[1] << 24) ;
1183
1184 dst[x] = pix;
1185 }
1186 }
1187 } else {
1188 /* convert horizontal BGR into ARGB32 */
1189
1190 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1191
1192 int x;
1193 unsigned char *src = srcLine;
1194 unsigned int *dst = (unsigned int *) dstLine;
1195
1196 for (x = 0; x < width; x++, src += 3) {
1197 unsigned int pix;
1198
1199 pix = ((unsigned int)src[2] << 16) |
1200 ((unsigned int)src[1] << 8) |
1201 ((unsigned int)src[0] ) |
1202 ((unsigned int)src[1] << 24) ;
1203
1204 dst[x] = pix;
1205 }
1206 }
1207 }
1208 break;
1209
1210 case FT_PIXEL_MODE_LCD_V:
1211 /* convert vertical RGB into ARGB32 */
1212 if (!bgr) {
1213
1214 for (h = height; h > 0; h--, srcLine += 3 * src_pitch, dstLine += pitch) {
1215 int x;
1216 unsigned char* src = srcLine;
1217 unsigned int* dst = (unsigned int *) dstLine;
1218
1219 for (x = 0; x < width; x++, src += 1) {
1220 unsigned int pix;
1221 pix = ((unsigned int)src[0] << 16) |
1222 ((unsigned int)src[src_pitch] << 8) |
1223 ((unsigned int)src[src_pitch*2] ) |
1224 ((unsigned int)src[src_pitch] << 24) ;
1225 dst[x] = pix;
1226 }
1227 }
1228 } else {
1229
1230 for (h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch) {
1231 int x;
1232 unsigned char *src = srcLine;
1233 unsigned int *dst = (unsigned int *) dstLine;
1234
1235 for (x = 0; x < width; x++, src += 1) {
1236 unsigned int pix;
1237
1238 pix = ((unsigned int)src[src_pitch * 2] << 16) |
1239 ((unsigned int)src[src_pitch] << 8) |
1240 ((unsigned int)src[0] ) |
1241 ((unsigned int)src[src_pitch] << 24) ;
1242
1243 dst[x] = pix;
1244 }
1245 }
1246 }
1247 break;
1248
1249 #ifdef FT_LOAD_COLOR
1250 case FT_PIXEL_MODE_BGRA:
1251 for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1252 memcpy (dstLine, srcLine, width * 4);
1253 break;
1254 #endif
1255
1256 default:
1257 assert (0);
1258 }
1259 }
1260
1261
1262 /* Fills in val->image with an image surface created from @bitmap
1263 */
1264 static cairo_status_t
_get_bitmap_surface(FT_Bitmap * bitmap,FT_Library library,cairo_bool_t own_buffer,cairo_font_options_t * font_options,cairo_image_surface_t ** surface)1265 _get_bitmap_surface (FT_Bitmap *bitmap,
1266 FT_Library library,
1267 cairo_bool_t own_buffer,
1268 cairo_font_options_t *font_options,
1269 cairo_image_surface_t **surface)
1270 {
1271 unsigned int width, height;
1272 unsigned char *data;
1273 int format = CAIRO_FORMAT_A8;
1274 int stride;
1275 cairo_image_surface_t *image;
1276 cairo_bool_t component_alpha = FALSE;
1277
1278 width = bitmap->width;
1279 height = bitmap->rows;
1280
1281 if (width == 0 || height == 0) {
1282 *surface = (cairo_image_surface_t *)
1283 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1284 return (*surface)->base.status;
1285 }
1286
1287 switch (bitmap->pixel_mode) {
1288 case FT_PIXEL_MODE_MONO:
1289 stride = (((width + 31) & ~31) >> 3);
1290 if (own_buffer) {
1291 data = bitmap->buffer;
1292 assert (stride == bitmap->pitch);
1293 } else {
1294 data = _cairo_malloc_ab (height, stride);
1295 if (!data)
1296 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1297
1298 if (stride == bitmap->pitch) {
1299 memcpy (data, bitmap->buffer, stride * height);
1300 } else {
1301 int i;
1302 unsigned char *source, *dest;
1303
1304 source = bitmap->buffer;
1305 dest = data;
1306 for (i = height; i; i--) {
1307 memcpy (dest, source, bitmap->pitch);
1308 memset (dest + bitmap->pitch, '\0', stride - bitmap->pitch);
1309
1310 source += bitmap->pitch;
1311 dest += stride;
1312 }
1313 }
1314 }
1315
1316 #ifndef WORDS_BIGENDIAN
1317 {
1318 uint8_t *d = data;
1319 int count = stride * height;
1320
1321 while (count--) {
1322 *d = CAIRO_BITSWAP8 (*d);
1323 d++;
1324 }
1325 }
1326 #endif
1327 format = CAIRO_FORMAT_A1;
1328 break;
1329
1330 case FT_PIXEL_MODE_LCD:
1331 case FT_PIXEL_MODE_LCD_V:
1332 case FT_PIXEL_MODE_GRAY:
1333 if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL ||
1334 bitmap->pixel_mode == FT_PIXEL_MODE_GRAY)
1335 {
1336 stride = bitmap->pitch;
1337
1338 /* We don't support stride not multiple of 4. */
1339 if (stride & 3)
1340 {
1341 assert (!own_buffer);
1342 goto convert;
1343 }
1344
1345 if (own_buffer) {
1346 data = bitmap->buffer;
1347 } else {
1348 data = _cairo_malloc_ab (height, stride);
1349 if (!data)
1350 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1351
1352 memcpy (data, bitmap->buffer, stride * height);
1353 }
1354
1355 format = CAIRO_FORMAT_A8;
1356 } else {
1357 data = bitmap->buffer;
1358 stride = bitmap->pitch;
1359 format = CAIRO_FORMAT_ARGB32;
1360 component_alpha = TRUE;
1361 }
1362 break;
1363 #ifdef FT_LOAD_COLOR
1364 case FT_PIXEL_MODE_BGRA:
1365 stride = width * 4;
1366 if (own_buffer) {
1367 data = bitmap->buffer;
1368 } else {
1369 data = _cairo_malloc_ab (height, stride);
1370 if (!data)
1371 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1372
1373 memcpy (data, bitmap->buffer, stride * height);
1374 }
1375
1376 if (!_cairo_is_little_endian ())
1377 {
1378 /* Byteswap. */
1379 unsigned int i, count = height * width;
1380 uint32_t *p = (uint32_t *) data;
1381 for (i = 0; i < count; i++)
1382 p[i] = be32_to_cpu (p[i]);
1383 }
1384 format = CAIRO_FORMAT_ARGB32;
1385 break;
1386 #endif
1387 case FT_PIXEL_MODE_GRAY2:
1388 case FT_PIXEL_MODE_GRAY4:
1389 convert:
1390 if (!own_buffer && library)
1391 {
1392 /* This is pretty much the only case that we can get in here. */
1393 /* Convert to 8bit grayscale. */
1394
1395 FT_Bitmap tmp;
1396 FT_Int align;
1397 FT_Error error;
1398
1399 format = CAIRO_FORMAT_A8;
1400
1401 align = cairo_format_stride_for_width (format, bitmap->width);
1402
1403 FT_Bitmap_New( &tmp );
1404
1405 error = FT_Bitmap_Convert( library, bitmap, &tmp, align );
1406 if (error)
1407 return _cairo_error (_ft_to_cairo_error (error));
1408
1409 FT_Bitmap_Done( library, bitmap );
1410 *bitmap = tmp;
1411
1412 stride = bitmap->pitch;
1413 data = _cairo_malloc_ab (height, stride);
1414 if (!data)
1415 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1416
1417 if (bitmap->num_grays != 256)
1418 {
1419 unsigned int x, y;
1420 unsigned int mul = 255 / (bitmap->num_grays - 1);
1421 FT_Byte *p = bitmap->buffer;
1422 for (y = 0; y < height; y++) {
1423 for (x = 0; x < width; x++)
1424 p[x] *= mul;
1425 p += bitmap->pitch;
1426 }
1427 }
1428
1429 memcpy (data, bitmap->buffer, stride * height);
1430 break;
1431 }
1432 /* fall through */
1433 /* These could be triggered by very rare types of TrueType fonts */
1434 default:
1435 if (own_buffer)
1436 free (bitmap->buffer);
1437 return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
1438 }
1439
1440 /* XXX */
1441 *surface = image = (cairo_image_surface_t *)
1442 cairo_image_surface_create_for_data (data,
1443 format,
1444 width, height, stride);
1445 if (image->base.status) {
1446 free (data);
1447 return (*surface)->base.status;
1448 }
1449
1450 if (component_alpha)
1451 pixman_image_set_component_alpha (image->pixman_image, TRUE);
1452
1453 _cairo_image_surface_assume_ownership_of_data (image);
1454
1455 _cairo_debug_check_image_surface_is_defined (&image->base);
1456
1457 return CAIRO_STATUS_SUCCESS;
1458 }
1459
1460 /* Converts an outline FT_GlyphSlot into an image
1461 *
1462 * This could go through _render_glyph_bitmap as well, letting
1463 * FreeType convert the outline to a bitmap, but doing it ourselves
1464 * has two minor advantages: first, we save a copy of the bitmap
1465 * buffer: we can directly use the buffer that FreeType renders
1466 * into.
1467 *
1468 * Second, it may help when we add support for subpixel
1469 * rendering: the Xft code does it this way. (Keith thinks that
1470 * it may also be possible to get the subpixel rendering with
1471 * FT_Render_Glyph: something worth looking into in more detail
1472 * when we add subpixel support. If so, we may want to eliminate
1473 * this version of the code path entirely.
1474 */
1475 static cairo_status_t
_render_glyph_outline(FT_Face face,cairo_font_options_t * font_options,cairo_image_surface_t ** surface)1476 _render_glyph_outline (FT_Face face,
1477 cairo_font_options_t *font_options,
1478 cairo_image_surface_t **surface)
1479 {
1480 int rgba = FC_RGBA_UNKNOWN;
1481 int lcd_filter = FT_LCD_FILTER_LEGACY;
1482 FT_GlyphSlot glyphslot = face->glyph;
1483 FT_Outline *outline = &glyphslot->outline;
1484 FT_Bitmap bitmap;
1485 FT_BBox cbox;
1486 unsigned int width, height;
1487 cairo_status_t status;
1488 FT_Error error;
1489 FT_Library library = glyphslot->library;
1490 FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
1491
1492 switch (font_options->antialias) {
1493 case CAIRO_ANTIALIAS_NONE:
1494 render_mode = FT_RENDER_MODE_MONO;
1495 break;
1496
1497 case CAIRO_ANTIALIAS_SUBPIXEL:
1498 case CAIRO_ANTIALIAS_BEST:
1499 switch (font_options->subpixel_order) {
1500 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1501 case CAIRO_SUBPIXEL_ORDER_RGB:
1502 case CAIRO_SUBPIXEL_ORDER_BGR:
1503 render_mode = FT_RENDER_MODE_LCD;
1504 break;
1505
1506 case CAIRO_SUBPIXEL_ORDER_VRGB:
1507 case CAIRO_SUBPIXEL_ORDER_VBGR:
1508 render_mode = FT_RENDER_MODE_LCD_V;
1509 break;
1510 }
1511
1512 switch (font_options->lcd_filter) {
1513 case CAIRO_LCD_FILTER_NONE:
1514 lcd_filter = FT_LCD_FILTER_NONE;
1515 break;
1516 case CAIRO_LCD_FILTER_DEFAULT:
1517 case CAIRO_LCD_FILTER_INTRA_PIXEL:
1518 lcd_filter = FT_LCD_FILTER_LEGACY;
1519 break;
1520 case CAIRO_LCD_FILTER_FIR3:
1521 lcd_filter = FT_LCD_FILTER_LIGHT;
1522 break;
1523 case CAIRO_LCD_FILTER_FIR5:
1524 lcd_filter = FT_LCD_FILTER_DEFAULT;
1525 break;
1526 }
1527
1528 break;
1529
1530 case CAIRO_ANTIALIAS_DEFAULT:
1531 case CAIRO_ANTIALIAS_GRAY:
1532 case CAIRO_ANTIALIAS_GOOD:
1533 case CAIRO_ANTIALIAS_FAST:
1534 render_mode = FT_RENDER_MODE_NORMAL;
1535 }
1536
1537 FT_Outline_Get_CBox (outline, &cbox);
1538
1539 cbox.xMin &= -64;
1540 cbox.yMin &= -64;
1541 cbox.xMax = (cbox.xMax + 63) & -64;
1542 cbox.yMax = (cbox.yMax + 63) & -64;
1543
1544 width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
1545 height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
1546
1547 if (width * height == 0) {
1548 cairo_format_t format;
1549 /* Looks like fb handles zero-sized images just fine */
1550 switch (render_mode) {
1551 case FT_RENDER_MODE_MONO:
1552 format = CAIRO_FORMAT_A1;
1553 break;
1554 case FT_RENDER_MODE_LCD:
1555 case FT_RENDER_MODE_LCD_V:
1556 format= CAIRO_FORMAT_ARGB32;
1557 break;
1558 case FT_RENDER_MODE_LIGHT:
1559 case FT_RENDER_MODE_NORMAL:
1560 case FT_RENDER_MODE_MAX:
1561 default:
1562 format = CAIRO_FORMAT_A8;
1563 break;
1564 }
1565
1566 (*surface) = (cairo_image_surface_t *)
1567 cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1568 pixman_image_set_component_alpha ((*surface)->pixman_image, TRUE);
1569 if ((*surface)->base.status)
1570 return (*surface)->base.status;
1571 } else {
1572
1573 int bitmap_size;
1574
1575 switch (render_mode) {
1576 case FT_RENDER_MODE_LCD:
1577 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
1578 rgba = FC_RGBA_BGR;
1579 else
1580 rgba = FC_RGBA_RGB;
1581 break;
1582
1583 case FT_RENDER_MODE_LCD_V:
1584 if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
1585 rgba = FC_RGBA_VBGR;
1586 else
1587 rgba = FC_RGBA_VRGB;
1588 break;
1589
1590 case FT_RENDER_MODE_MONO:
1591 case FT_RENDER_MODE_LIGHT:
1592 case FT_RENDER_MODE_NORMAL:
1593 case FT_RENDER_MODE_MAX:
1594 default:
1595 break;
1596 }
1597
1598 #if HAVE_FT_LIBRARY_SETLCDFILTER
1599 FT_Library_SetLcdFilter (library, lcd_filter);
1600 #endif
1601
1602 error = FT_Render_Glyph (face->glyph, render_mode);
1603
1604 #if HAVE_FT_LIBRARY_SETLCDFILTER
1605 FT_Library_SetLcdFilter (library, FT_LCD_FILTER_NONE);
1606 #endif
1607
1608 if (error)
1609 return _cairo_error (_ft_to_cairo_error (error));
1610
1611 bitmap_size = _compute_xrender_bitmap_size (&bitmap,
1612 face->glyph,
1613 render_mode);
1614 if (bitmap_size < 0)
1615 return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
1616
1617 bitmap.buffer = calloc (1, bitmap_size);
1618 if (bitmap.buffer == NULL)
1619 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1620
1621 _fill_xrender_bitmap (&bitmap, face->glyph, render_mode,
1622 (rgba == FC_RGBA_BGR || rgba == FC_RGBA_VBGR));
1623
1624 /* Note:
1625 * _get_bitmap_surface will free bitmap.buffer if there is an error
1626 */
1627 status = _get_bitmap_surface (&bitmap, NULL, TRUE, font_options, surface);
1628 if (unlikely (status))
1629 return status;
1630
1631 /* Note: the font's coordinate system is upside down from ours, so the
1632 * Y coordinate of the control box needs to be negated. Moreover, device
1633 * offsets are position of glyph origin relative to top left while xMin
1634 * and yMax are offsets of top left relative to origin. Another negation.
1635 */
1636 cairo_surface_set_device_offset (&(*surface)->base,
1637 (double)-glyphslot->bitmap_left,
1638 (double)+glyphslot->bitmap_top);
1639 }
1640
1641 return CAIRO_STATUS_SUCCESS;
1642 }
1643
1644 /* Converts a bitmap (or other) FT_GlyphSlot into an image */
1645 static cairo_status_t
_render_glyph_bitmap(FT_Face face,cairo_font_options_t * font_options,cairo_image_surface_t ** surface)1646 _render_glyph_bitmap (FT_Face face,
1647 cairo_font_options_t *font_options,
1648 cairo_image_surface_t **surface)
1649 {
1650 FT_GlyphSlot glyphslot = face->glyph;
1651 cairo_status_t status;
1652 FT_Error error;
1653
1654 /* According to the FreeType docs, glyphslot->format could be
1655 * something other than FT_GLYPH_FORMAT_OUTLINE or
1656 * FT_GLYPH_FORMAT_BITMAP. Calling FT_Render_Glyph gives FreeType
1657 * the opportunity to convert such to
1658 * bitmap. FT_GLYPH_FORMAT_COMPOSITE will not be encountered since
1659 * we avoid the FT_LOAD_NO_RECURSE flag.
1660 */
1661 error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL);
1662 /* XXX ignoring all other errors for now. They are not fatal, typically
1663 * just a glyph-not-found. */
1664 if (error == FT_Err_Out_Of_Memory)
1665 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1666
1667 status = _get_bitmap_surface (&glyphslot->bitmap,
1668 glyphslot->library,
1669 FALSE, font_options,
1670 surface);
1671 if (unlikely (status))
1672 return status;
1673
1674 /*
1675 * Note: the font's coordinate system is upside down from ours, so the
1676 * Y coordinate of the control box needs to be negated. Moreover, device
1677 * offsets are position of glyph origin relative to top left while
1678 * bitmap_left and bitmap_top are offsets of top left relative to origin.
1679 * Another negation.
1680 */
1681 cairo_surface_set_device_offset (&(*surface)->base,
1682 -glyphslot->bitmap_left,
1683 +glyphslot->bitmap_top);
1684
1685 return CAIRO_STATUS_SUCCESS;
1686 }
1687
1688 static cairo_status_t
_transform_glyph_bitmap(cairo_matrix_t * shape,cairo_image_surface_t ** surface)1689 _transform_glyph_bitmap (cairo_matrix_t * shape,
1690 cairo_image_surface_t ** surface)
1691 {
1692 cairo_matrix_t original_to_transformed;
1693 cairo_matrix_t transformed_to_original;
1694 cairo_image_surface_t *old_image;
1695 cairo_surface_t *image;
1696 double x[4], y[4];
1697 double origin_x, origin_y;
1698 int orig_width, orig_height;
1699 int i;
1700 int x_min, y_min, x_max, y_max;
1701 int width, height;
1702 cairo_status_t status;
1703 cairo_surface_pattern_t pattern;
1704
1705 /* We want to compute a transform that takes the origin
1706 * (device_x_offset, device_y_offset) to 0,0, then applies
1707 * the "shape" portion of the font transform
1708 */
1709 original_to_transformed = *shape;
1710
1711 cairo_surface_get_device_offset (&(*surface)->base, &origin_x, &origin_y);
1712 orig_width = (*surface)->width;
1713 orig_height = (*surface)->height;
1714
1715 cairo_matrix_translate (&original_to_transformed,
1716 -origin_x, -origin_y);
1717
1718 /* Find the bounding box of the original bitmap under that
1719 * transform
1720 */
1721 x[0] = 0; y[0] = 0;
1722 x[1] = orig_width; y[1] = 0;
1723 x[2] = orig_width; y[2] = orig_height;
1724 x[3] = 0; y[3] = orig_height;
1725
1726 for (i = 0; i < 4; i++)
1727 cairo_matrix_transform_point (&original_to_transformed,
1728 &x[i], &y[i]);
1729
1730 x_min = floor (x[0]); y_min = floor (y[0]);
1731 x_max = ceil (x[0]); y_max = ceil (y[0]);
1732
1733 for (i = 1; i < 4; i++) {
1734 if (x[i] < x_min)
1735 x_min = floor (x[i]);
1736 else if (x[i] > x_max)
1737 x_max = ceil (x[i]);
1738 if (y[i] < y_min)
1739 y_min = floor (y[i]);
1740 else if (y[i] > y_max)
1741 y_max = ceil (y[i]);
1742 }
1743
1744 /* Adjust the transform so that the bounding box starts at 0,0 ...
1745 * this gives our final transform from original bitmap to transformed
1746 * bitmap.
1747 */
1748 original_to_transformed.x0 -= x_min;
1749 original_to_transformed.y0 -= y_min;
1750
1751 /* Create the transformed bitmap */
1752 width = x_max - x_min;
1753 height = y_max - y_min;
1754
1755 transformed_to_original = original_to_transformed;
1756 status = cairo_matrix_invert (&transformed_to_original);
1757 if (unlikely (status))
1758 return status;
1759
1760 if ((*surface)->format == CAIRO_FORMAT_ARGB32 &&
1761 !pixman_image_get_component_alpha ((*surface)->pixman_image))
1762 image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
1763 else
1764 image = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
1765 if (unlikely (image->status))
1766 return image->status;
1767
1768 /* Draw the original bitmap transformed into the new bitmap
1769 */
1770 _cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
1771 cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
1772
1773 status = _cairo_surface_paint (image,
1774 CAIRO_OPERATOR_SOURCE,
1775 &pattern.base,
1776 NULL);
1777
1778 _cairo_pattern_fini (&pattern.base);
1779
1780 if (unlikely (status)) {
1781 cairo_surface_destroy (image);
1782 return status;
1783 }
1784
1785 /* Now update the cache entry for the new bitmap, recomputing
1786 * the origin based on the final transform.
1787 */
1788 cairo_matrix_transform_point (&original_to_transformed,
1789 &origin_x, &origin_y);
1790
1791 old_image = (*surface);
1792 (*surface) = (cairo_image_surface_t *)image;
1793
1794 /* Note: we converted subpixel-rendered RGBA images to grayscale,
1795 * so, no need to copy component alpha to new image. */
1796
1797 cairo_surface_destroy (&old_image->base);
1798
1799 cairo_surface_set_device_offset (&(*surface)->base,
1800 _cairo_lround (origin_x),
1801 _cairo_lround (origin_y));
1802 return CAIRO_STATUS_SUCCESS;
1803 }
1804
1805 static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend = {
1806 _cairo_ft_unscaled_font_destroy,
1807 #if 0
1808 _cairo_ft_unscaled_font_create_glyph
1809 #endif
1810 };
1811
1812 /* #cairo_ft_scaled_font_t */
1813
1814 typedef struct _cairo_ft_scaled_font {
1815 cairo_scaled_font_t base;
1816 cairo_ft_unscaled_font_t *unscaled;
1817 cairo_ft_options_t ft_options;
1818 } cairo_ft_scaled_font_t;
1819
1820 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend;
1821
1822 #if CAIRO_HAS_FC_FONT
1823 /* The load flags passed to FT_Load_Glyph control aspects like hinting and
1824 * antialiasing. Here we compute them from the fields of a FcPattern.
1825 */
1826 static void
_get_pattern_ft_options(FcPattern * pattern,cairo_ft_options_t * ret)1827 _get_pattern_ft_options (FcPattern *pattern, cairo_ft_options_t *ret)
1828 {
1829 FcBool antialias, vertical_layout, hinting, autohint, bitmap, embolden;
1830 cairo_ft_options_t ft_options;
1831 int rgba;
1832 #ifdef FC_HINT_STYLE
1833 int hintstyle;
1834 #endif
1835 char *variations;
1836
1837 _cairo_font_options_init_default (&ft_options.base);
1838 ft_options.load_flags = FT_LOAD_DEFAULT;
1839 ft_options.synth_flags = 0;
1840
1841 #ifndef FC_EMBEDDED_BITMAP
1842 #define FC_EMBEDDED_BITMAP "embeddedbitmap"
1843 #endif
1844
1845 /* Check whether to force use of embedded bitmaps */
1846 if (FcPatternGetBool (pattern,
1847 FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
1848 bitmap = FcFalse;
1849
1850 /* disable antialiasing if requested */
1851 if (FcPatternGetBool (pattern,
1852 FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
1853 antialias = FcTrue;
1854
1855 if (antialias) {
1856 cairo_subpixel_order_t subpixel_order;
1857 int lcd_filter;
1858
1859 /* disable hinting if requested */
1860 if (FcPatternGetBool (pattern,
1861 FC_HINTING, 0, &hinting) != FcResultMatch)
1862 hinting = FcTrue;
1863
1864 if (FcPatternGetInteger (pattern,
1865 FC_RGBA, 0, &rgba) != FcResultMatch)
1866 rgba = FC_RGBA_UNKNOWN;
1867
1868 switch (rgba) {
1869 case FC_RGBA_RGB:
1870 subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
1871 break;
1872 case FC_RGBA_BGR:
1873 subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
1874 break;
1875 case FC_RGBA_VRGB:
1876 subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
1877 break;
1878 case FC_RGBA_VBGR:
1879 subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
1880 break;
1881 case FC_RGBA_UNKNOWN:
1882 case FC_RGBA_NONE:
1883 default:
1884 subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1885 break;
1886 }
1887
1888 if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT) {
1889 ft_options.base.subpixel_order = subpixel_order;
1890 ft_options.base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1891 }
1892
1893 if (FcPatternGetInteger (pattern,
1894 FC_LCD_FILTER, 0, &lcd_filter) == FcResultMatch)
1895 {
1896 switch (lcd_filter) {
1897 case FC_LCD_NONE:
1898 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1899 break;
1900 case FC_LCD_DEFAULT:
1901 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR5;
1902 break;
1903 case FC_LCD_LIGHT:
1904 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR3;
1905 break;
1906 case FC_LCD_LEGACY:
1907 ft_options.base.lcd_filter = CAIRO_LCD_FILTER_INTRA_PIXEL;
1908 break;
1909 }
1910 }
1911
1912 #ifdef FC_HINT_STYLE
1913 if (FcPatternGetInteger (pattern,
1914 FC_HINT_STYLE, 0, &hintstyle) != FcResultMatch)
1915 hintstyle = FC_HINT_FULL;
1916
1917 if (!hinting)
1918 hintstyle = FC_HINT_NONE;
1919
1920 switch (hintstyle) {
1921 case FC_HINT_NONE:
1922 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1923 break;
1924 case FC_HINT_SLIGHT:
1925 ft_options.base.hint_style = CAIRO_HINT_STYLE_SLIGHT;
1926 break;
1927 case FC_HINT_MEDIUM:
1928 default:
1929 ft_options.base.hint_style = CAIRO_HINT_STYLE_MEDIUM;
1930 break;
1931 case FC_HINT_FULL:
1932 ft_options.base.hint_style = CAIRO_HINT_STYLE_FULL;
1933 break;
1934 }
1935 #else /* !FC_HINT_STYLE */
1936 if (!hinting) {
1937 ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1938 }
1939 #endif /* FC_HINT_STYLE */
1940
1941 /* Force embedded bitmaps off if no hinting requested */
1942 if (ft_options.base.hint_style == CAIRO_HINT_STYLE_NONE)
1943 bitmap = FcFalse;
1944
1945 if (!bitmap)
1946 ft_options.load_flags |= FT_LOAD_NO_BITMAP;
1947
1948 } else {
1949 ft_options.base.antialias = CAIRO_ANTIALIAS_NONE;
1950 }
1951
1952 /* force autohinting if requested */
1953 if (FcPatternGetBool (pattern,
1954 FC_AUTOHINT, 0, &autohint) != FcResultMatch)
1955 autohint = FcFalse;
1956
1957 if (autohint)
1958 ft_options.load_flags |= FT_LOAD_FORCE_AUTOHINT;
1959
1960 if (FcPatternGetBool (pattern,
1961 FC_VERTICAL_LAYOUT, 0, &vertical_layout) != FcResultMatch)
1962 vertical_layout = FcFalse;
1963
1964 if (vertical_layout)
1965 ft_options.load_flags |= FT_LOAD_VERTICAL_LAYOUT;
1966
1967 #ifndef FC_EMBOLDEN
1968 #define FC_EMBOLDEN "embolden"
1969 #endif
1970 if (FcPatternGetBool (pattern,
1971 FC_EMBOLDEN, 0, &embolden) != FcResultMatch)
1972 embolden = FcFalse;
1973
1974 if (embolden)
1975 ft_options.synth_flags |= CAIRO_FT_SYNTHESIZE_BOLD;
1976
1977 #ifndef FC_FONT_VARIATIONS
1978 #define FC_FONT_VARIATIONS "fontvariations"
1979 #endif
1980 if (FcPatternGetString (pattern, FC_FONT_VARIATIONS, 0, (FcChar8 **) &variations) == FcResultMatch) {
1981 ft_options.base.variations = strdup (variations);
1982 }
1983
1984 *ret = ft_options;
1985 }
1986 #endif
1987
1988 static void
_cairo_ft_options_merge(cairo_ft_options_t * options,cairo_ft_options_t * other)1989 _cairo_ft_options_merge (cairo_ft_options_t *options,
1990 cairo_ft_options_t *other)
1991 {
1992 int load_flags = other->load_flags;
1993 int load_target = FT_LOAD_TARGET_NORMAL;
1994
1995 /* clear load target mode */
1996 load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(other->load_flags)));
1997
1998 if (load_flags & FT_LOAD_NO_HINTING)
1999 other->base.hint_style = CAIRO_HINT_STYLE_NONE;
2000
2001 if (other->base.antialias == CAIRO_ANTIALIAS_NONE ||
2002 options->base.antialias == CAIRO_ANTIALIAS_NONE) {
2003 options->base.antialias = CAIRO_ANTIALIAS_NONE;
2004 options->base.subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
2005 }
2006
2007 if (other->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL &&
2008 (options->base.antialias == CAIRO_ANTIALIAS_DEFAULT ||
2009 options->base.antialias == CAIRO_ANTIALIAS_GRAY)) {
2010 options->base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
2011 options->base.subpixel_order = other->base.subpixel_order;
2012 }
2013
2014 if (options->base.hint_style == CAIRO_HINT_STYLE_DEFAULT)
2015 options->base.hint_style = other->base.hint_style;
2016
2017 if (other->base.hint_style == CAIRO_HINT_STYLE_NONE)
2018 options->base.hint_style = CAIRO_HINT_STYLE_NONE;
2019
2020 if (options->base.lcd_filter == CAIRO_LCD_FILTER_DEFAULT)
2021 options->base.lcd_filter = other->base.lcd_filter;
2022
2023 if (other->base.lcd_filter == CAIRO_LCD_FILTER_NONE)
2024 options->base.lcd_filter = CAIRO_LCD_FILTER_NONE;
2025
2026 if (options->base.antialias == CAIRO_ANTIALIAS_NONE) {
2027 if (options->base.hint_style == CAIRO_HINT_STYLE_NONE)
2028 load_flags |= FT_LOAD_NO_HINTING;
2029 else
2030 load_target = FT_LOAD_TARGET_MONO;
2031 load_flags |= FT_LOAD_MONOCHROME;
2032 } else {
2033 switch (options->base.hint_style) {
2034 case CAIRO_HINT_STYLE_NONE:
2035 load_flags |= FT_LOAD_NO_HINTING;
2036 break;
2037 case CAIRO_HINT_STYLE_SLIGHT:
2038 load_target = FT_LOAD_TARGET_LIGHT;
2039 break;
2040 case CAIRO_HINT_STYLE_MEDIUM:
2041 break;
2042 case CAIRO_HINT_STYLE_FULL:
2043 case CAIRO_HINT_STYLE_DEFAULT:
2044 if (options->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
2045 switch (options->base.subpixel_order) {
2046 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
2047 case CAIRO_SUBPIXEL_ORDER_RGB:
2048 case CAIRO_SUBPIXEL_ORDER_BGR:
2049 load_target = FT_LOAD_TARGET_LCD;
2050 break;
2051 case CAIRO_SUBPIXEL_ORDER_VRGB:
2052 case CAIRO_SUBPIXEL_ORDER_VBGR:
2053 load_target = FT_LOAD_TARGET_LCD_V;
2054 break;
2055 }
2056 }
2057 break;
2058 }
2059 }
2060
2061 if (other->base.variations) {
2062 if (options->base.variations) {
2063 char *p;
2064
2065 /* 'merge' variations by concatenating - later entries win */
2066 p = malloc (strlen (other->base.variations) + strlen (options->base.variations) + 2);
2067 p[0] = 0;
2068 strcat (p, other->base.variations);
2069 strcat (p, ",");
2070 strcat (p, options->base.variations);
2071 free (options->base.variations);
2072 options->base.variations = p;
2073 }
2074 else {
2075 options->base.variations = strdup (other->base.variations);
2076 }
2077 }
2078
2079 options->load_flags = load_flags | load_target;
2080 options->synth_flags = other->synth_flags;
2081 }
2082
2083 static cairo_status_t
_cairo_ft_font_face_scaled_font_create(void * abstract_font_face,const cairo_matrix_t * font_matrix,const cairo_matrix_t * ctm,const cairo_font_options_t * options,cairo_scaled_font_t ** font_out)2084 _cairo_ft_font_face_scaled_font_create (void *abstract_font_face,
2085 const cairo_matrix_t *font_matrix,
2086 const cairo_matrix_t *ctm,
2087 const cairo_font_options_t *options,
2088 cairo_scaled_font_t **font_out)
2089 {
2090 cairo_ft_font_face_t *font_face = abstract_font_face;
2091 cairo_ft_scaled_font_t *scaled_font;
2092 FT_Face face;
2093 FT_Size_Metrics *metrics;
2094 cairo_font_extents_t fs_metrics;
2095 cairo_status_t status;
2096 cairo_ft_unscaled_font_t *unscaled;
2097
2098 assert (font_face->unscaled);
2099
2100 face = _cairo_ft_unscaled_font_lock_face (font_face->unscaled);
2101 if (unlikely (face == NULL)) /* backend error */
2102 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2103
2104 scaled_font = _cairo_malloc (sizeof (cairo_ft_scaled_font_t));
2105 if (unlikely (scaled_font == NULL)) {
2106 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2107 goto FAIL;
2108 }
2109
2110 scaled_font->unscaled = unscaled = font_face->unscaled;
2111 _cairo_unscaled_font_reference (&unscaled->base);
2112
2113 _cairo_font_options_init_copy (&scaled_font->ft_options.base, options);
2114 _cairo_ft_options_merge (&scaled_font->ft_options, &font_face->ft_options);
2115
2116 status = _cairo_scaled_font_init (&scaled_font->base,
2117 &font_face->base,
2118 font_matrix, ctm, options,
2119 &_cairo_ft_scaled_font_backend);
2120 if (unlikely (status))
2121 goto CLEANUP_SCALED_FONT;
2122
2123 status = _cairo_ft_unscaled_font_set_scale (unscaled,
2124 &scaled_font->base.scale);
2125 if (unlikely (status)) {
2126 /* This can only fail if we encounter an error with the underlying
2127 * font, so propagate the error back to the font-face. */
2128 _cairo_ft_unscaled_font_unlock_face (unscaled);
2129 _cairo_unscaled_font_destroy (&unscaled->base);
2130 free (scaled_font);
2131 return status;
2132 }
2133
2134
2135 metrics = &face->size->metrics;
2136
2137 /*
2138 * Get to unscaled metrics so that the upper level can get back to
2139 * user space
2140 *
2141 * Also use this path for bitmap-only fonts. The other branch uses
2142 * face members that are only relevant for scalable fonts. This is
2143 * detected by simply checking for units_per_EM==0.
2144 */
2145 if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
2146 face->units_per_EM == 0) {
2147 double x_factor, y_factor;
2148
2149 if (unscaled->x_scale == 0)
2150 x_factor = 0;
2151 else
2152 x_factor = 1 / unscaled->x_scale;
2153
2154 if (unscaled->y_scale == 0)
2155 y_factor = 0;
2156 else
2157 y_factor = 1 / unscaled->y_scale;
2158
2159 fs_metrics.ascent = DOUBLE_FROM_26_6(metrics->ascender) * y_factor;
2160 fs_metrics.descent = DOUBLE_FROM_26_6(- metrics->descender) * y_factor;
2161 fs_metrics.height = DOUBLE_FROM_26_6(metrics->height) * y_factor;
2162 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
2163 fs_metrics.max_x_advance = DOUBLE_FROM_26_6(metrics->max_advance) * x_factor;
2164 fs_metrics.max_y_advance = 0;
2165 } else {
2166 fs_metrics.max_x_advance = 0;
2167 fs_metrics.max_y_advance = DOUBLE_FROM_26_6(metrics->max_advance) * y_factor;
2168 }
2169 } else {
2170 double scale = face->units_per_EM;
2171
2172 fs_metrics.ascent = face->ascender / scale;
2173 fs_metrics.descent = - face->descender / scale;
2174 fs_metrics.height = face->height / scale;
2175 if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
2176 fs_metrics.max_x_advance = face->max_advance_width / scale;
2177 fs_metrics.max_y_advance = 0;
2178 } else {
2179 fs_metrics.max_x_advance = 0;
2180 fs_metrics.max_y_advance = face->max_advance_height / scale;
2181 }
2182 }
2183
2184 status = _cairo_scaled_font_set_metrics (&scaled_font->base, &fs_metrics);
2185 if (unlikely (status))
2186 goto CLEANUP_SCALED_FONT;
2187
2188 _cairo_ft_unscaled_font_unlock_face (unscaled);
2189
2190 *font_out = &scaled_font->base;
2191 return CAIRO_STATUS_SUCCESS;
2192
2193 CLEANUP_SCALED_FONT:
2194 _cairo_unscaled_font_destroy (&unscaled->base);
2195 free (scaled_font);
2196 FAIL:
2197 _cairo_ft_unscaled_font_unlock_face (font_face->unscaled);
2198 *font_out = _cairo_scaled_font_create_in_error (status);
2199 return CAIRO_STATUS_SUCCESS; /* non-backend error */
2200 }
2201
2202 cairo_bool_t
_cairo_scaled_font_is_ft(cairo_scaled_font_t * scaled_font)2203 _cairo_scaled_font_is_ft (cairo_scaled_font_t *scaled_font)
2204 {
2205 return scaled_font->backend == &_cairo_ft_scaled_font_backend;
2206 }
2207
2208 static void
_cairo_ft_scaled_font_fini(void * abstract_font)2209 _cairo_ft_scaled_font_fini (void *abstract_font)
2210 {
2211 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2212
2213 if (scaled_font == NULL)
2214 return;
2215
2216 _cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
2217 }
2218
2219 static int
_move_to(FT_Vector * to,void * closure)2220 _move_to (FT_Vector *to, void *closure)
2221 {
2222 cairo_path_fixed_t *path = closure;
2223 cairo_fixed_t x, y;
2224
2225 x = _cairo_fixed_from_26_6 (to->x);
2226 y = _cairo_fixed_from_26_6 (to->y);
2227
2228 if (_cairo_path_fixed_close_path (path) != CAIRO_STATUS_SUCCESS)
2229 return 1;
2230 if (_cairo_path_fixed_move_to (path, x, y) != CAIRO_STATUS_SUCCESS)
2231 return 1;
2232
2233 return 0;
2234 }
2235
2236 static int
_line_to(FT_Vector * to,void * closure)2237 _line_to (FT_Vector *to, void *closure)
2238 {
2239 cairo_path_fixed_t *path = closure;
2240 cairo_fixed_t x, y;
2241
2242 x = _cairo_fixed_from_26_6 (to->x);
2243 y = _cairo_fixed_from_26_6 (to->y);
2244
2245 if (_cairo_path_fixed_line_to (path, x, y) != CAIRO_STATUS_SUCCESS)
2246 return 1;
2247
2248 return 0;
2249 }
2250
2251 static int
_conic_to(FT_Vector * control,FT_Vector * to,void * closure)2252 _conic_to (FT_Vector *control, FT_Vector *to, void *closure)
2253 {
2254 cairo_path_fixed_t *path = closure;
2255
2256 cairo_fixed_t x0, y0;
2257 cairo_fixed_t x1, y1;
2258 cairo_fixed_t x2, y2;
2259 cairo_fixed_t x3, y3;
2260 cairo_point_t conic;
2261
2262 if (! _cairo_path_fixed_get_current_point (path, &x0, &y0))
2263 return 1;
2264
2265 conic.x = _cairo_fixed_from_26_6 (control->x);
2266 conic.y = _cairo_fixed_from_26_6 (control->y);
2267
2268 x3 = _cairo_fixed_from_26_6 (to->x);
2269 y3 = _cairo_fixed_from_26_6 (to->y);
2270
2271 x1 = x0 + 2.0/3.0 * (conic.x - x0);
2272 y1 = y0 + 2.0/3.0 * (conic.y - y0);
2273
2274 x2 = x3 + 2.0/3.0 * (conic.x - x3);
2275 y2 = y3 + 2.0/3.0 * (conic.y - y3);
2276
2277 if (_cairo_path_fixed_curve_to (path,
2278 x1, y1,
2279 x2, y2,
2280 x3, y3) != CAIRO_STATUS_SUCCESS)
2281 return 1;
2282
2283 return 0;
2284 }
2285
2286 static int
_cubic_to(FT_Vector * control1,FT_Vector * control2,FT_Vector * to,void * closure)2287 _cubic_to (FT_Vector *control1, FT_Vector *control2,
2288 FT_Vector *to, void *closure)
2289 {
2290 cairo_path_fixed_t *path = closure;
2291 cairo_fixed_t x0, y0;
2292 cairo_fixed_t x1, y1;
2293 cairo_fixed_t x2, y2;
2294
2295 x0 = _cairo_fixed_from_26_6 (control1->x);
2296 y0 = _cairo_fixed_from_26_6 (control1->y);
2297
2298 x1 = _cairo_fixed_from_26_6 (control2->x);
2299 y1 = _cairo_fixed_from_26_6 (control2->y);
2300
2301 x2 = _cairo_fixed_from_26_6 (to->x);
2302 y2 = _cairo_fixed_from_26_6 (to->y);
2303
2304 if (_cairo_path_fixed_curve_to (path,
2305 x0, y0,
2306 x1, y1,
2307 x2, y2) != CAIRO_STATUS_SUCCESS)
2308 return 1;
2309
2310 return 0;
2311 }
2312
2313 static cairo_status_t
_decompose_glyph_outline(FT_Face face,cairo_font_options_t * options,cairo_path_fixed_t ** pathp)2314 _decompose_glyph_outline (FT_Face face,
2315 cairo_font_options_t *options,
2316 cairo_path_fixed_t **pathp)
2317 {
2318 static const FT_Outline_Funcs outline_funcs = {
2319 (FT_Outline_MoveToFunc)_move_to,
2320 (FT_Outline_LineToFunc)_line_to,
2321 (FT_Outline_ConicToFunc)_conic_to,
2322 (FT_Outline_CubicToFunc)_cubic_to,
2323 0, /* shift */
2324 0, /* delta */
2325 };
2326 static const FT_Matrix invert_y = {
2327 DOUBLE_TO_16_16 (1.0), 0,
2328 0, DOUBLE_TO_16_16 (-1.0),
2329 };
2330
2331 FT_GlyphSlot glyph;
2332 cairo_path_fixed_t *path;
2333 cairo_status_t status;
2334
2335 path = _cairo_path_fixed_create ();
2336 if (!path)
2337 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2338
2339 glyph = face->glyph;
2340
2341 /* Font glyphs have an inverted Y axis compared to cairo. */
2342 FT_Outline_Transform (&glyph->outline, &invert_y);
2343 if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
2344 _cairo_path_fixed_destroy (path);
2345 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2346 }
2347
2348 status = _cairo_path_fixed_close_path (path);
2349 if (unlikely (status)) {
2350 _cairo_path_fixed_destroy (path);
2351 return status;
2352 }
2353
2354 *pathp = path;
2355
2356 return CAIRO_STATUS_SUCCESS;
2357 }
2358
2359 /*
2360 * Translate glyph to match its metrics.
2361 */
2362 static void
_cairo_ft_scaled_glyph_vertical_layout_bearing_fix(void * abstract_font,FT_GlyphSlot glyph)2363 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (void *abstract_font,
2364 FT_GlyphSlot glyph)
2365 {
2366 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2367 FT_Vector vector;
2368
2369 vector.x = glyph->metrics.vertBearingX - glyph->metrics.horiBearingX;
2370 vector.y = -glyph->metrics.vertBearingY - glyph->metrics.horiBearingY;
2371
2372 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2373 FT_Vector_Transform (&vector, &scaled_font->unscaled->Current_Shape);
2374 FT_Outline_Translate(&glyph->outline, vector.x, vector.y);
2375 } else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
2376 glyph->bitmap_left += vector.x / 64;
2377 glyph->bitmap_top += vector.y / 64;
2378 }
2379 }
2380
2381 static void
cairo_ft_apply_variations(FT_Face face,cairo_ft_scaled_font_t * scaled_font)2382 cairo_ft_apply_variations (FT_Face face,
2383 cairo_ft_scaled_font_t *scaled_font)
2384 {
2385 FT_MM_Var *ft_mm_var;
2386 FT_Error ret;
2387 unsigned int instance_id = scaled_font->unscaled->id >> 16;
2388
2389 static GetVarFunc getVar;
2390 static DoneVarFunc doneVar;
2391 static GetVarDesignCoordsFunc getVarDesignCoords;
2392 static SetVarDesignCoordsFunc setVarDesignCoords;
2393
2394 static int firstTime = 1;
2395 if (firstTime) {
2396 getVar = (GetVarFunc) dlsym (RTLD_DEFAULT, "FT_Get_MM_Var");
2397 doneVar = (DoneVarFunc) dlsym (RTLD_DEFAULT, "FT_Done_MM_Var");
2398 getVarDesignCoords = (GetVarDesignCoordsFunc) dlsym (RTLD_DEFAULT, "FT_Get_Var_Design_Coordinates");
2399 setVarDesignCoords = (SetVarDesignCoordsFunc) dlsym (RTLD_DEFAULT, "FT_Set_Var_Design_Coordinates");
2400 firstTime = 0;
2401 }
2402
2403 if (!getVar || !setVarDesignCoords)
2404 return;
2405
2406 ret = (*getVar) (face, &ft_mm_var);
2407 if (ret == 0) {
2408 FT_Fixed *current_coords;
2409 FT_Fixed *coords;
2410 unsigned int i;
2411 const char *p;
2412
2413 coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
2414 /* FIXME check coords. */
2415
2416 if (scaled_font->unscaled->variations)
2417 {
2418 memcpy (coords, scaled_font->unscaled->variations, ft_mm_var->num_axis * sizeof (*coords));
2419 }
2420 else if (instance_id && instance_id <= ft_mm_var->num_namedstyles)
2421 {
2422 FT_Var_Named_Style *instance = &ft_mm_var->namedstyle[instance_id - 1];
2423 memcpy (coords, instance->coords, ft_mm_var->num_axis * sizeof (*coords));
2424 }
2425 else
2426 for (i = 0; i < ft_mm_var->num_axis; i++)
2427 coords[i] = ft_mm_var->axis[i].def;
2428
2429 p = scaled_font->ft_options.base.variations;
2430 while (p && *p) {
2431 const char *start;
2432 const char *end, *end2;
2433 FT_ULong tag;
2434 double value;
2435
2436 while (_cairo_isspace (*p)) p++;
2437
2438 start = p;
2439 end = strchr (p, ',');
2440 if (end && (end - p < 6))
2441 goto skip;
2442
2443 tag = FT_MAKE_TAG(p[0], p[1], p[2], p[3]);
2444
2445 p += 4;
2446 while (_cairo_isspace (*p)) p++;
2447 if (*p == '=') p++;
2448
2449 if (p - start < 5)
2450 goto skip;
2451
2452 value = _cairo_strtod (p, (char **) &end2);
2453
2454 while (end2 && _cairo_isspace (*end2)) end2++;
2455
2456 if (end2 && (*end2 != ',' && *end2 != '\0'))
2457 goto skip;
2458
2459 for (i = 0; i < ft_mm_var->num_axis; i++) {
2460 if (ft_mm_var->axis[i].tag == tag) {
2461 coords[i] = (FT_Fixed)(value*65536);
2462 break;
2463 }
2464 }
2465
2466 skip:
2467 p = end ? end + 1 : NULL;
2468 }
2469
2470 current_coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
2471
2472 if (getVarDesignCoords) {
2473 ret = (*getVarDesignCoords) (face, ft_mm_var->num_axis, current_coords);
2474 if (ret == 0) {
2475 for (i = 0; i < ft_mm_var->num_axis; i++) {
2476 if (coords[i] != current_coords[i])
2477 break;
2478 }
2479 if (i == ft_mm_var->num_axis)
2480 goto done;
2481 }
2482 }
2483
2484 (*setVarDesignCoords) (face, ft_mm_var->num_axis, coords);
2485 done:
2486 free (coords);
2487 free (current_coords);
2488
2489 if (doneVar)
2490 (*doneVar) (face->glyph->library, ft_mm_var);
2491 else
2492 free (ft_mm_var);
2493 }
2494 }
2495
2496 static cairo_int_status_t
_cairo_ft_scaled_glyph_load_glyph(cairo_ft_scaled_font_t * scaled_font,cairo_scaled_glyph_t * scaled_glyph,FT_Face face,int load_flags,cairo_bool_t use_em_size,cairo_bool_t vertical_layout)2497 _cairo_ft_scaled_glyph_load_glyph (cairo_ft_scaled_font_t *scaled_font,
2498 cairo_scaled_glyph_t *scaled_glyph,
2499 FT_Face face,
2500 int load_flags,
2501 cairo_bool_t use_em_size,
2502 cairo_bool_t vertical_layout)
2503 {
2504 FT_Error error;
2505 cairo_status_t status;
2506
2507 if (use_em_size) {
2508 cairo_matrix_t em_size;
2509 cairo_matrix_init_scale (&em_size, face->units_per_EM, face->units_per_EM);
2510 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled, &em_size);
2511 } else {
2512 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
2513 &scaled_font->base.scale);
2514 }
2515 if (unlikely (status))
2516 return status;
2517
2518 cairo_ft_apply_variations (face, scaled_font);
2519
2520 error = FT_Load_Glyph (face,
2521 _cairo_scaled_glyph_index(scaled_glyph),
2522 load_flags);
2523 /* XXX ignoring all other errors for now. They are not fatal, typically
2524 * just a glyph-not-found. */
2525 if (error == FT_Err_Out_Of_Memory)
2526 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2527
2528 /*
2529 * synthesize glyphs if requested
2530 */
2531 #if HAVE_FT_GLYPHSLOT_EMBOLDEN
2532 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2533 FT_GlyphSlot_Embolden (face->glyph);
2534 #endif
2535
2536 #if HAVE_FT_GLYPHSLOT_OBLIQUE
2537 if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2538 FT_GlyphSlot_Oblique (face->glyph);
2539 #endif
2540
2541 if (vertical_layout)
2542 _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, face->glyph);
2543
2544 if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2545 FT_Pos xshift, yshift;
2546
2547 xshift = _cairo_scaled_glyph_xphase (scaled_glyph) << 4;
2548 yshift = _cairo_scaled_glyph_yphase (scaled_glyph) << 4;
2549
2550 FT_Outline_Translate (&face->glyph->outline, xshift, -yshift);
2551 }
2552
2553 return CAIRO_STATUS_SUCCESS;
2554 }
2555
2556 static cairo_int_status_t
_cairo_ft_scaled_glyph_init(void * abstract_font,cairo_scaled_glyph_t * scaled_glyph,cairo_scaled_glyph_info_t info)2557 _cairo_ft_scaled_glyph_init (void *abstract_font,
2558 cairo_scaled_glyph_t *scaled_glyph,
2559 cairo_scaled_glyph_info_t info)
2560 {
2561 cairo_text_extents_t fs_metrics;
2562 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2563 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2564 FT_GlyphSlot glyph;
2565 FT_Face face;
2566 int load_flags = scaled_font->ft_options.load_flags;
2567 FT_Glyph_Metrics *metrics;
2568 double x_factor, y_factor;
2569 cairo_bool_t vertical_layout = FALSE;
2570 cairo_status_t status = CAIRO_STATUS_SUCCESS;
2571 cairo_bool_t scaled_glyph_loaded = FALSE;
2572
2573 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2574 if (!face)
2575 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2576
2577 /* Ignore global advance unconditionally */
2578 load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
2579
2580 if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0 &&
2581 (info & (CAIRO_SCALED_GLYPH_INFO_SURFACE |
2582 CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) == 0) {
2583 load_flags |= FT_LOAD_NO_BITMAP;
2584 }
2585
2586 /*
2587 * Don't pass FT_LOAD_VERTICAL_LAYOUT to FT_Load_Glyph here as
2588 * suggested by freetype people.
2589 */
2590 if (load_flags & FT_LOAD_VERTICAL_LAYOUT) {
2591 load_flags &= ~FT_LOAD_VERTICAL_LAYOUT;
2592 vertical_layout = TRUE;
2593 }
2594
2595 #ifdef FT_LOAD_COLOR
2596 load_flags |= FT_LOAD_COLOR;
2597 #endif
2598
2599
2600 if (info & CAIRO_SCALED_GLYPH_INFO_METRICS) {
2601
2602 cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
2603
2604 status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
2605 scaled_glyph,
2606 face,
2607 load_flags,
2608 !hint_metrics,
2609 vertical_layout);
2610 if (unlikely (status))
2611 goto FAIL;
2612
2613 glyph = face->glyph;
2614 scaled_glyph_loaded = hint_metrics;
2615
2616 /*
2617 * Compute font-space metrics
2618 */
2619 metrics = &glyph->metrics;
2620
2621 if (unscaled->x_scale == 0)
2622 x_factor = 0;
2623 else
2624 x_factor = 1 / unscaled->x_scale;
2625
2626 if (unscaled->y_scale == 0)
2627 y_factor = 0;
2628 else
2629 y_factor = 1 / unscaled->y_scale;
2630
2631 /*
2632 * Note: Y coordinates of the horizontal bearing need to be negated.
2633 *
2634 * Scale metrics back to glyph space from the scaled glyph space returned
2635 * by FreeType
2636 *
2637 * If we want hinted metrics but aren't asking for hinted glyphs from
2638 * FreeType, then we need to do the metric hinting ourselves.
2639 */
2640
2641 if (hint_metrics && (load_flags & FT_LOAD_NO_HINTING))
2642 {
2643 FT_Pos x1, x2;
2644 FT_Pos y1, y2;
2645 FT_Pos advance;
2646
2647 if (!vertical_layout) {
2648 x1 = (metrics->horiBearingX) & -64;
2649 x2 = (metrics->horiBearingX + metrics->width + 63) & -64;
2650 y1 = (-metrics->horiBearingY) & -64;
2651 y2 = (-metrics->horiBearingY + metrics->height + 63) & -64;
2652
2653 advance = ((metrics->horiAdvance + 32) & -64);
2654
2655 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2656 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2657
2658 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2659 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2660
2661 fs_metrics.x_advance = DOUBLE_FROM_26_6 (advance) * x_factor;
2662 fs_metrics.y_advance = 0;
2663 } else {
2664 x1 = (metrics->vertBearingX) & -64;
2665 x2 = (metrics->vertBearingX + metrics->width + 63) & -64;
2666 y1 = (metrics->vertBearingY) & -64;
2667 y2 = (metrics->vertBearingY + metrics->height + 63) & -64;
2668
2669 advance = ((metrics->vertAdvance + 32) & -64);
2670
2671 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (x1) * x_factor;
2672 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (y1) * y_factor;
2673
2674 fs_metrics.width = DOUBLE_FROM_26_6 (x2 - x1) * x_factor;
2675 fs_metrics.height = DOUBLE_FROM_26_6 (y2 - y1) * y_factor;
2676
2677 fs_metrics.x_advance = 0;
2678 fs_metrics.y_advance = DOUBLE_FROM_26_6 (advance) * y_factor;
2679 }
2680 } else {
2681 fs_metrics.width = DOUBLE_FROM_26_6 (metrics->width) * x_factor;
2682 fs_metrics.height = DOUBLE_FROM_26_6 (metrics->height) * y_factor;
2683
2684 if (!vertical_layout) {
2685 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->horiBearingX) * x_factor;
2686 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (-metrics->horiBearingY) * y_factor;
2687
2688 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2689 fs_metrics.x_advance = DOUBLE_FROM_26_6 (metrics->horiAdvance) * x_factor;
2690 else
2691 fs_metrics.x_advance = DOUBLE_FROM_16_16 (glyph->linearHoriAdvance) * x_factor;
2692 fs_metrics.y_advance = 0 * y_factor;
2693 } else {
2694 fs_metrics.x_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingX) * x_factor;
2695 fs_metrics.y_bearing = DOUBLE_FROM_26_6 (metrics->vertBearingY) * y_factor;
2696
2697 fs_metrics.x_advance = 0 * x_factor;
2698 if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
2699 fs_metrics.y_advance = DOUBLE_FROM_26_6 (metrics->vertAdvance) * y_factor;
2700 else
2701 fs_metrics.y_advance = DOUBLE_FROM_16_16 (glyph->linearVertAdvance) * y_factor;
2702 }
2703 }
2704
2705 _cairo_scaled_glyph_set_metrics (scaled_glyph,
2706 &scaled_font->base,
2707 &fs_metrics);
2708 }
2709
2710 LOAD:
2711 if (info & (CAIRO_SCALED_GLYPH_INFO_SURFACE | CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) {
2712 cairo_image_surface_t *surface;
2713
2714 if (!scaled_glyph_loaded) {
2715 status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
2716 scaled_glyph,
2717 face,
2718 load_flags,
2719 FALSE,
2720 vertical_layout);
2721 if (unlikely (status))
2722 goto FAIL;
2723
2724 glyph = face->glyph;
2725 scaled_glyph_loaded = TRUE;
2726 }
2727
2728 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2729 status = _render_glyph_outline (face, &scaled_font->ft_options.base,
2730 &surface);
2731 } else {
2732 status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
2733 &surface);
2734 if (likely (status == CAIRO_STATUS_SUCCESS) &&
2735 unscaled->have_shape)
2736 {
2737 status = _transform_glyph_bitmap (&unscaled->current_shape,
2738 &surface);
2739 if (unlikely (status))
2740 cairo_surface_destroy (&surface->base);
2741 }
2742 }
2743 if (unlikely (status))
2744 goto FAIL;
2745
2746 if (pixman_image_get_format (surface->pixman_image) == PIXMAN_a8r8g8b8 &&
2747 !pixman_image_get_component_alpha (surface->pixman_image)) {
2748 _cairo_scaled_glyph_set_color_surface (scaled_glyph,
2749 &scaled_font->base,
2750 surface);
2751 } else {
2752 _cairo_scaled_glyph_set_surface (scaled_glyph,
2753 &scaled_font->base,
2754 surface);
2755 }
2756 }
2757
2758 if (((info & (CAIRO_SCALED_GLYPH_INFO_SURFACE | CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) != 0) &&
2759 ((scaled_glyph->has_info & CAIRO_SCALED_GLYPH_INFO_SURFACE) == 0)) {
2760 /*
2761 * A kludge -- load again, without color.
2762 * No need to load the metrics again, though
2763 */
2764 scaled_glyph_loaded = FALSE;
2765 info &= ~CAIRO_SCALED_GLYPH_INFO_METRICS;
2766 #ifdef FT_LOAD_COLOR
2767 load_flags &= ~FT_LOAD_COLOR;
2768 #endif
2769 goto LOAD;
2770 }
2771
2772 if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
2773 cairo_path_fixed_t *path = NULL; /* hide compiler warning */
2774
2775 /*
2776 * A kludge -- the above code will trash the outline,
2777 * so reload it. This will probably never occur though
2778 */
2779 if ((info & (CAIRO_SCALED_GLYPH_INFO_SURFACE | CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) != 0) {
2780 scaled_glyph_loaded = FALSE;
2781 load_flags |= FT_LOAD_NO_BITMAP;
2782 }
2783
2784 if (!scaled_glyph_loaded) {
2785 status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
2786 scaled_glyph,
2787 face,
2788 load_flags,
2789 FALSE,
2790 vertical_layout);
2791 if (unlikely (status))
2792 goto FAIL;
2793
2794 glyph = face->glyph;
2795 }
2796
2797 if (glyph->format == FT_GLYPH_FORMAT_OUTLINE)
2798 status = _decompose_glyph_outline (face, &scaled_font->ft_options.base,
2799 &path);
2800 else
2801 status = CAIRO_INT_STATUS_UNSUPPORTED;
2802
2803 if (unlikely (status))
2804 goto FAIL;
2805
2806 _cairo_scaled_glyph_set_path (scaled_glyph,
2807 &scaled_font->base,
2808 path);
2809 }
2810 FAIL:
2811 _cairo_ft_unscaled_font_unlock_face (unscaled);
2812
2813 return status;
2814 }
2815
2816 static unsigned long
_cairo_ft_ucs4_to_index(void * abstract_font,uint32_t ucs4)2817 _cairo_ft_ucs4_to_index (void *abstract_font,
2818 uint32_t ucs4)
2819 {
2820 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2821 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2822 FT_Face face;
2823 FT_UInt index;
2824
2825 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2826 if (!face)
2827 return 0;
2828
2829 #if CAIRO_HAS_FC_FONT
2830 index = FcFreeTypeCharIndex (face, ucs4);
2831 #else
2832 index = FT_Get_Char_Index (face, ucs4);
2833 #endif
2834
2835 _cairo_ft_unscaled_font_unlock_face (unscaled);
2836 return index;
2837 }
2838
2839 static cairo_int_status_t
_cairo_ft_load_truetype_table(void * abstract_font,unsigned long tag,long offset,unsigned char * buffer,unsigned long * length)2840 _cairo_ft_load_truetype_table (void *abstract_font,
2841 unsigned long tag,
2842 long offset,
2843 unsigned char *buffer,
2844 unsigned long *length)
2845 {
2846 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2847 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2848 FT_Face face;
2849 cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
2850
2851 /* We don't support the FreeType feature of loading a table
2852 * without specifying the size since this may overflow our
2853 * buffer. */
2854 assert (length != NULL);
2855
2856 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
2857 return CAIRO_INT_STATUS_UNSUPPORTED;
2858
2859 #if HAVE_FT_LOAD_SFNT_TABLE
2860 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2861 if (!face)
2862 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2863
2864 if (FT_IS_SFNT (face)) {
2865 if (buffer == NULL)
2866 *length = 0;
2867
2868 if (FT_Load_Sfnt_Table (face, tag, offset, buffer, length) == 0)
2869 status = CAIRO_STATUS_SUCCESS;
2870 }
2871
2872 _cairo_ft_unscaled_font_unlock_face (unscaled);
2873 #endif
2874
2875 return status;
2876 }
2877
2878 static cairo_int_status_t
_cairo_ft_index_to_ucs4(void * abstract_font,unsigned long index,uint32_t * ucs4)2879 _cairo_ft_index_to_ucs4(void *abstract_font,
2880 unsigned long index,
2881 uint32_t *ucs4)
2882 {
2883 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2884 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2885 FT_Face face;
2886 FT_ULong charcode;
2887 FT_UInt gindex;
2888
2889 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2890 if (!face)
2891 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2892
2893 *ucs4 = (uint32_t) -1;
2894 charcode = FT_Get_First_Char(face, &gindex);
2895 while (gindex != 0) {
2896 if (gindex == index) {
2897 *ucs4 = charcode;
2898 break;
2899 }
2900 charcode = FT_Get_Next_Char (face, charcode, &gindex);
2901 }
2902
2903 _cairo_ft_unscaled_font_unlock_face (unscaled);
2904
2905 return CAIRO_STATUS_SUCCESS;
2906 }
2907
2908 static cairo_int_status_t
_cairo_ft_is_synthetic(void * abstract_font,cairo_bool_t * is_synthetic)2909 _cairo_ft_is_synthetic (void *abstract_font,
2910 cairo_bool_t *is_synthetic)
2911 {
2912 cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
2913 cairo_ft_scaled_font_t *scaled_font = abstract_font;
2914 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2915 FT_Face face;
2916 FT_Error error;
2917
2918 static GetVarFunc getVar;
2919 static DoneVarFunc doneVar;
2920 static GetVarBlendCoordsFunc getVarBlendCoords;
2921
2922 static int firstTime = 1;
2923 if (firstTime) {
2924 getVar = (GetVarFunc) dlsym (RTLD_DEFAULT, "FT_Get_MM_Var");
2925 doneVar = (DoneVarFunc) dlsym (RTLD_DEFAULT, "FT_Done_MM_Var");
2926 getVarBlendCoords = (GetVarBlendCoordsFunc) dlsym (RTLD_DEFAULT, "FT_Get_Var_Blend_Coordinates");
2927 firstTime = 0;
2928 }
2929
2930 if (scaled_font->ft_options.synth_flags != 0) {
2931 *is_synthetic = TRUE;
2932 return status;
2933 }
2934
2935 *is_synthetic = FALSE;
2936 face = _cairo_ft_unscaled_font_lock_face (unscaled);
2937 if (!face)
2938 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2939
2940 if (face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
2941 FT_MM_Var *mm_var = NULL;
2942 FT_Fixed *coords = NULL;
2943 int num_axis;
2944
2945 /* If this is an MM or variable font we can't assume the current outlines
2946 * are the same as the font tables */
2947 *is_synthetic = TRUE;
2948
2949 error = getVar ? (*getVar) (face, &mm_var) : -1;
2950 if (error) {
2951 status = _cairo_error (_ft_to_cairo_error (error));
2952 goto cleanup;
2953 }
2954
2955 num_axis = mm_var->num_axis;
2956 coords = _cairo_malloc_ab (num_axis, sizeof(FT_Fixed));
2957 if (!coords) {
2958 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2959 goto cleanup;
2960 }
2961
2962 /* If FT_Get_Var_Blend_Coordinates() is available, we can check if the
2963 * current design coordinates are the default coordinates. In this case
2964 * the current outlines match the font tables.
2965 */
2966 if (getVarBlendCoords) {
2967 int i;
2968
2969 (*getVarBlendCoords) (face, num_axis, coords);
2970 *is_synthetic = FALSE;
2971 for (i = 0; i < num_axis; i++) {
2972 if (coords[i]) {
2973 *is_synthetic = TRUE;
2974 break;
2975 }
2976 }
2977 }
2978
2979 cleanup:
2980 free (coords);
2981 if (doneVar)
2982 (*doneVar) (face->glyph->library, mm_var);
2983 else
2984 free (mm_var);
2985 }
2986
2987 _cairo_ft_unscaled_font_unlock_face (unscaled);
2988
2989 return status;
2990 }
2991
2992 static cairo_int_status_t
_cairo_index_to_glyph_name(void * abstract_font,char ** glyph_names,int num_glyph_names,unsigned long glyph_index,unsigned long * glyph_array_index)2993 _cairo_index_to_glyph_name (void *abstract_font,
2994 char **glyph_names,
2995 int num_glyph_names,
2996 unsigned long glyph_index,
2997 unsigned long *glyph_array_index)
2998 {
2999 cairo_ft_scaled_font_t *scaled_font = abstract_font;
3000 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3001 FT_Face face;
3002 char buffer[256]; /* PLRM specifies max name length of 127 */
3003 FT_Error error;
3004 int i;
3005
3006 face = _cairo_ft_unscaled_font_lock_face (unscaled);
3007 if (!face)
3008 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3009
3010 error = FT_Get_Glyph_Name (face, glyph_index, buffer, sizeof buffer);
3011
3012 _cairo_ft_unscaled_font_unlock_face (unscaled);
3013
3014 if (error != FT_Err_Ok) {
3015 /* propagate fatal errors from FreeType */
3016 if (error == FT_Err_Out_Of_Memory)
3017 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3018
3019 return CAIRO_INT_STATUS_UNSUPPORTED;
3020 }
3021
3022 /* FT first numbers the glyphs in the order they are read from the
3023 * Type 1 font. Then if .notdef is not the first glyph, the first
3024 * glyph is swapped with .notdef to ensure that .notdef is at
3025 * glyph index 0.
3026 *
3027 * As all but two glyphs in glyph_names already have the same
3028 * index as the FT glyph index, we first check if
3029 * glyph_names[glyph_index] is the name we are looking for. If not
3030 * we fall back to searching the entire array.
3031 */
3032
3033 if ((long)glyph_index < num_glyph_names &&
3034 strcmp (glyph_names[glyph_index], buffer) == 0)
3035 {
3036 *glyph_array_index = glyph_index;
3037
3038 return CAIRO_STATUS_SUCCESS;
3039 }
3040
3041 for (i = 0; i < num_glyph_names; i++) {
3042 if (strcmp (glyph_names[i], buffer) == 0) {
3043 *glyph_array_index = i;
3044
3045 return CAIRO_STATUS_SUCCESS;
3046 }
3047 }
3048
3049 return CAIRO_INT_STATUS_UNSUPPORTED;
3050 }
3051
3052 static cairo_bool_t
_ft_is_type1(FT_Face face)3053 _ft_is_type1 (FT_Face face)
3054 {
3055 #if HAVE_FT_GET_X11_FONT_FORMAT
3056 const char *font_format = FT_Get_X11_Font_Format (face);
3057 if (font_format &&
3058 (strcmp (font_format, "Type 1") == 0 ||
3059 strcmp (font_format, "CFF") == 0))
3060 {
3061 return TRUE;
3062 }
3063 #endif
3064
3065 return FALSE;
3066 }
3067
3068 static cairo_int_status_t
_cairo_ft_load_type1_data(void * abstract_font,long offset,unsigned char * buffer,unsigned long * length)3069 _cairo_ft_load_type1_data (void *abstract_font,
3070 long offset,
3071 unsigned char *buffer,
3072 unsigned long *length)
3073 {
3074 cairo_ft_scaled_font_t *scaled_font = abstract_font;
3075 cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3076 FT_Face face;
3077 cairo_status_t status = CAIRO_STATUS_SUCCESS;
3078 unsigned long available_length;
3079 unsigned long ret;
3080
3081 assert (length != NULL);
3082
3083 if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
3084 return CAIRO_INT_STATUS_UNSUPPORTED;
3085
3086 face = _cairo_ft_unscaled_font_lock_face (unscaled);
3087 if (!face)
3088 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3089
3090 #if HAVE_FT_LOAD_SFNT_TABLE
3091 if (FT_IS_SFNT (face)) {
3092 status = CAIRO_INT_STATUS_UNSUPPORTED;
3093 goto unlock;
3094 }
3095 #endif
3096
3097 if (! _ft_is_type1 (face)) {
3098 status = CAIRO_INT_STATUS_UNSUPPORTED;
3099 goto unlock;
3100 }
3101
3102 available_length = MAX (face->stream->size - offset, 0);
3103 if (!buffer) {
3104 *length = available_length;
3105 } else {
3106 if (*length > available_length) {
3107 status = CAIRO_INT_STATUS_UNSUPPORTED;
3108 } else if (face->stream->read != NULL) {
3109 /* Note that read() may be implemented as a macro, thanks POSIX!, so we
3110 * need to wrap the following usage in parentheses in order to
3111 * disambiguate it for the pre-processor - using the verbose function
3112 * pointer dereference for clarity.
3113 */
3114 ret = (* face->stream->read) (face->stream,
3115 offset,
3116 buffer,
3117 *length);
3118 if (ret != *length)
3119 status = _cairo_error (CAIRO_STATUS_READ_ERROR);
3120 } else {
3121 memcpy (buffer, face->stream->base + offset, *length);
3122 }
3123 }
3124
3125 unlock:
3126 _cairo_ft_unscaled_font_unlock_face (unscaled);
3127
3128 return status;
3129 }
3130
3131 static cairo_bool_t
_cairo_ft_has_color_glyphs(void * scaled)3132 _cairo_ft_has_color_glyphs (void *scaled)
3133 {
3134 cairo_ft_unscaled_font_t *unscaled = ((cairo_ft_scaled_font_t *)scaled)->unscaled;
3135
3136 if (!unscaled->have_color_set) {
3137 FT_Face face;
3138 face = _cairo_ft_unscaled_font_lock_face (unscaled);
3139 if (unlikely (face == NULL))
3140 return FALSE;
3141 _cairo_ft_unscaled_font_unlock_face (unscaled);
3142 }
3143
3144 return unscaled->have_color;
3145 }
3146
3147 static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend = {
3148 CAIRO_FONT_TYPE_FT,
3149 _cairo_ft_scaled_font_fini,
3150 _cairo_ft_scaled_glyph_init,
3151 NULL, /* text_to_glyphs */
3152 _cairo_ft_ucs4_to_index,
3153 _cairo_ft_load_truetype_table,
3154 _cairo_ft_index_to_ucs4,
3155 _cairo_ft_is_synthetic,
3156 _cairo_index_to_glyph_name,
3157 _cairo_ft_load_type1_data,
3158 _cairo_ft_has_color_glyphs
3159 };
3160
3161 /* #cairo_ft_font_face_t */
3162
3163 #if CAIRO_HAS_FC_FONT
3164 static cairo_font_face_t *
3165 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern);
3166
3167 static cairo_status_t
_cairo_ft_font_face_create_for_toy(cairo_toy_font_face_t * toy_face,cairo_font_face_t ** font_face_out)3168 _cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
3169 cairo_font_face_t **font_face_out)
3170 {
3171 cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
3172 FcPattern *pattern;
3173 int fcslant;
3174 int fcweight;
3175
3176 pattern = FcPatternCreate ();
3177 if (!pattern) {
3178 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3179 return font_face->status;
3180 }
3181
3182 if (!FcPatternAddString (pattern,
3183 FC_FAMILY, (unsigned char *) toy_face->family))
3184 {
3185 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3186 goto FREE_PATTERN;
3187 }
3188
3189 switch (toy_face->slant)
3190 {
3191 case CAIRO_FONT_SLANT_ITALIC:
3192 fcslant = FC_SLANT_ITALIC;
3193 break;
3194 case CAIRO_FONT_SLANT_OBLIQUE:
3195 fcslant = FC_SLANT_OBLIQUE;
3196 break;
3197 case CAIRO_FONT_SLANT_NORMAL:
3198 default:
3199 fcslant = FC_SLANT_ROMAN;
3200 break;
3201 }
3202
3203 if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
3204 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3205 goto FREE_PATTERN;
3206 }
3207
3208 switch (toy_face->weight)
3209 {
3210 case CAIRO_FONT_WEIGHT_BOLD:
3211 fcweight = FC_WEIGHT_BOLD;
3212 break;
3213 case CAIRO_FONT_WEIGHT_NORMAL:
3214 default:
3215 fcweight = FC_WEIGHT_MEDIUM;
3216 break;
3217 }
3218
3219 if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
3220 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3221 goto FREE_PATTERN;
3222 }
3223
3224 font_face = _cairo_ft_font_face_create_for_pattern (pattern);
3225
3226 FREE_PATTERN:
3227 FcPatternDestroy (pattern);
3228
3229 *font_face_out = font_face;
3230 return font_face->status;
3231 }
3232 #endif
3233
3234 static cairo_bool_t
_cairo_ft_font_face_destroy(void * abstract_face)3235 _cairo_ft_font_face_destroy (void *abstract_face)
3236 {
3237 cairo_ft_font_face_t *font_face = abstract_face;
3238
3239 /* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
3240 * we have a special "zombie" state for the face when the unscaled font
3241 * is still alive but there are no other references to a font face with
3242 * the same FT_Face.
3243 *
3244 * We go from:
3245 *
3246 * font_face ------> unscaled
3247 * <-....weak....../
3248 *
3249 * To:
3250 *
3251 * font_face <------- unscaled
3252 */
3253
3254 if (font_face->unscaled) {
3255 CAIRO_FT_LOCK (font_face->unscaled);
3256
3257 if (font_face->unscaled->from_face &&
3258 font_face->next == NULL &&
3259 font_face->unscaled->faces == font_face &&
3260 CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->unscaled->base.ref_count) > 1)
3261 {
3262 CAIRO_FT_UNLOCK (font_face->unscaled);
3263 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
3264 font_face->unscaled = NULL;
3265
3266 return FALSE;
3267 }
3268
3269 cairo_ft_font_face_t *tmp_face = NULL;
3270 cairo_ft_font_face_t *last_face = NULL;
3271
3272 /* Remove face from linked list */
3273 for (tmp_face = font_face->unscaled->faces;
3274 tmp_face;
3275 tmp_face = tmp_face->next)
3276 {
3277 if (tmp_face == font_face) {
3278 if (last_face)
3279 last_face->next = tmp_face->next;
3280 else
3281 font_face->unscaled->faces = tmp_face->next;
3282 }
3283
3284 last_face = tmp_face;
3285 }
3286
3287 CAIRO_FT_UNLOCK (font_face->unscaled);
3288 _cairo_unscaled_font_destroy (&font_face->unscaled->base);
3289 font_face->unscaled = NULL;
3290 }
3291
3292 _cairo_ft_options_fini (&font_face->ft_options);
3293
3294 #if CAIRO_HAS_FC_FONT
3295 if (font_face->pattern) {
3296 FcPatternDestroy (font_face->pattern);
3297 cairo_font_face_destroy (font_face->resolved_font_face);
3298 }
3299 #endif
3300
3301 return TRUE;
3302 }
3303
3304 static cairo_font_face_t *
_cairo_ft_font_face_get_implementation(void * abstract_face,const cairo_matrix_t * font_matrix,const cairo_matrix_t * ctm,const cairo_font_options_t * options)3305 _cairo_ft_font_face_get_implementation (void *abstract_face,
3306 const cairo_matrix_t *font_matrix,
3307 const cairo_matrix_t *ctm,
3308 const cairo_font_options_t *options)
3309 {
3310 cairo_ft_font_face_t *font_face = abstract_face;
3311
3312 /* The handling of font options is different depending on how the
3313 * font face was created. When the user creates a font face with
3314 * cairo_ft_font_face_create_for_ft_face(), then the load flags
3315 * passed in augment the load flags for the options. But for
3316 * cairo_ft_font_face_create_for_pattern(), the load flags are
3317 * derived from a pattern where the user has called
3318 * cairo_ft_font_options_substitute(), so *just* use those load
3319 * flags and ignore the options.
3320 */
3321
3322 #if CAIRO_HAS_FC_FONT
3323 /* If we have an unresolved pattern, resolve it and create
3324 * unscaled font. Otherwise, use the ones stored in font_face.
3325 */
3326 if (font_face->pattern) {
3327 cairo_font_face_t *resolved;
3328
3329 /* Cache the resolved font whilst the FcConfig remains consistent. */
3330 resolved = font_face->resolved_font_face;
3331 if (resolved != NULL) {
3332 if (! FcInitBringUptoDate ()) {
3333 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3334 return (cairo_font_face_t *) &_cairo_font_face_nil;
3335 }
3336
3337 if (font_face->resolved_config == FcConfigGetCurrent ())
3338 return cairo_font_face_reference (resolved);
3339
3340 cairo_font_face_destroy (resolved);
3341 font_face->resolved_font_face = NULL;
3342 }
3343
3344 resolved = _cairo_ft_resolve_pattern (font_face->pattern,
3345 font_matrix,
3346 ctm,
3347 options);
3348 if (unlikely (resolved->status))
3349 return resolved;
3350
3351 font_face->resolved_font_face = cairo_font_face_reference (resolved);
3352 font_face->resolved_config = FcConfigGetCurrent ();
3353
3354 return resolved;
3355 }
3356 #endif
3357
3358 return abstract_face;
3359 }
3360
3361 static void
_cairo_ft_font_face_lock(void * abstract_face)3362 _cairo_ft_font_face_lock (void *abstract_face)
3363 {
3364 cairo_ft_font_face_t *font_face = abstract_face;
3365 if (font_face->unscaled) {
3366 CAIRO_FT_LOCK (font_face->unscaled);
3367 }
3368 }
3369
3370 static void
_cairo_ft_font_face_unlock(void * abstract_face)3371 _cairo_ft_font_face_unlock (void *abstract_face)
3372 {
3373 cairo_ft_font_face_t *font_face = abstract_face;
3374 if (font_face->unscaled) {
3375 CAIRO_FT_UNLOCK (font_face->unscaled);
3376 }
3377 }
3378
3379 const cairo_font_face_backend_t _cairo_ft_font_face_backend = {
3380 CAIRO_FONT_TYPE_FT,
3381 #if CAIRO_HAS_FC_FONT
3382 _cairo_ft_font_face_create_for_toy,
3383 #else
3384 NULL,
3385 #endif
3386 _cairo_ft_font_face_destroy,
3387 _cairo_ft_font_face_scaled_font_create,
3388 _cairo_ft_font_face_get_implementation,
3389 /*
3390 _cairo_ft_font_face_lock,
3391 _cairo_ft_font_face_unlock
3392 */
3393 };
3394
3395 #if CAIRO_HAS_FC_FONT
3396 static cairo_font_face_t *
_cairo_ft_font_face_create_for_pattern(FcPattern * pattern)3397 _cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
3398 {
3399 cairo_ft_font_face_t *font_face;
3400
3401 font_face = _cairo_malloc (sizeof (cairo_ft_font_face_t));
3402 if (unlikely (font_face == NULL)) {
3403 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3404 return (cairo_font_face_t *) &_cairo_font_face_nil;
3405 }
3406
3407 font_face->unscaled = NULL;
3408
3409 _get_pattern_ft_options (pattern, &font_face->ft_options);
3410
3411 font_face->next = NULL;
3412
3413 font_face->pattern = FcPatternDuplicate (pattern);
3414 if (unlikely (font_face->pattern == NULL)) {
3415 free (font_face);
3416 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3417 return (cairo_font_face_t *) &_cairo_font_face_nil;
3418 }
3419
3420 font_face->resolved_font_face = NULL;
3421 font_face->resolved_config = NULL;
3422
3423 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
3424
3425 return &font_face->base;
3426 }
3427 #endif
3428
3429 static cairo_font_face_t *
_cairo_ft_font_face_create(cairo_ft_unscaled_font_t * unscaled,cairo_ft_options_t * ft_options)3430 _cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
3431 cairo_ft_options_t *ft_options)
3432 {
3433 cairo_ft_font_face_t *font_face, **prev_font_face;
3434
3435 CAIRO_FT_LOCK (unscaled);
3436
3437 /* Looked for an existing matching font face */
3438 for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
3439 font_face;
3440 prev_font_face = &font_face->next, font_face = font_face->next)
3441 {
3442 if (font_face->ft_options.load_flags == ft_options->load_flags &&
3443 font_face->ft_options.synth_flags == ft_options->synth_flags &&
3444 cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
3445 {
3446 if (font_face->base.status) {
3447 /* The font_face has been left in an error state, abandon it. */
3448 *prev_font_face = font_face->next;
3449 break;
3450 }
3451
3452 if (font_face->unscaled == NULL) {
3453 /* Resurrect this "zombie" font_face (from
3454 * _cairo_ft_font_face_destroy), switching its unscaled_font
3455 * from owner to ownee. */
3456 font_face->unscaled = unscaled;
3457 _cairo_unscaled_font_reference (&unscaled->base);
3458 } else {
3459 cairo_font_face_reference (&font_face->base);
3460 }
3461
3462 CAIRO_FT_UNLOCK (unscaled);
3463 return &font_face->base;
3464 }
3465 }
3466
3467 /* No match found, create a new one */
3468 font_face = _cairo_malloc (sizeof (cairo_ft_font_face_t));
3469 if (unlikely (!font_face)) {
3470 CAIRO_FT_UNLOCK (unscaled);
3471 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3472 return (cairo_font_face_t *)&_cairo_font_face_nil;
3473 }
3474
3475 font_face->unscaled = unscaled;
3476 _cairo_unscaled_font_reference (&unscaled->base);
3477
3478 _cairo_ft_options_init_copy (&font_face->ft_options, ft_options);
3479
3480 if (unscaled->faces && unscaled->faces->unscaled == NULL) {
3481 /* This "zombie" font_face (from _cairo_ft_font_face_destroy)
3482 * is no longer needed. */
3483 assert (unscaled->from_face && unscaled->faces->next == NULL);
3484 cairo_font_face_destroy (&unscaled->faces->base);
3485 unscaled->faces = NULL;
3486 }
3487
3488 font_face->next = unscaled->faces;
3489 unscaled->faces = font_face;
3490
3491 #if CAIRO_HAS_FC_FONT
3492 font_face->pattern = NULL;
3493 #endif
3494
3495 _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
3496
3497 CAIRO_FT_UNLOCK (unscaled);
3498 return &font_face->base;
3499 }
3500
3501 /* implement the platform-specific interface */
3502
3503 #if CAIRO_HAS_FC_FONT
3504 static cairo_status_t
_cairo_ft_font_options_substitute(const cairo_font_options_t * options,FcPattern * pattern)3505 _cairo_ft_font_options_substitute (const cairo_font_options_t *options,
3506 FcPattern *pattern)
3507 {
3508 FcValue v;
3509
3510 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
3511 {
3512 if (FcPatternGet (pattern, FC_ANTIALIAS, 0, &v) == FcResultNoMatch)
3513 {
3514 if (! FcPatternAddBool (pattern,
3515 FC_ANTIALIAS,
3516 options->antialias != CAIRO_ANTIALIAS_NONE))
3517 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3518
3519 if (options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
3520 FcPatternDel (pattern, FC_RGBA);
3521 if (! FcPatternAddInteger (pattern, FC_RGBA, FC_RGBA_NONE))
3522 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3523 }
3524 }
3525 }
3526
3527 if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
3528 {
3529 if (FcPatternGet (pattern, FC_RGBA, 0, &v) == FcResultNoMatch)
3530 {
3531 int rgba;
3532
3533 if (options->antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
3534 switch (options->subpixel_order) {
3535 case CAIRO_SUBPIXEL_ORDER_DEFAULT:
3536 case CAIRO_SUBPIXEL_ORDER_RGB:
3537 default:
3538 rgba = FC_RGBA_RGB;
3539 break;
3540 case CAIRO_SUBPIXEL_ORDER_BGR:
3541 rgba = FC_RGBA_BGR;
3542 break;
3543 case CAIRO_SUBPIXEL_ORDER_VRGB:
3544 rgba = FC_RGBA_VRGB;
3545 break;
3546 case CAIRO_SUBPIXEL_ORDER_VBGR:
3547 rgba = FC_RGBA_VBGR;
3548 break;
3549 }
3550 } else {
3551 rgba = FC_RGBA_NONE;
3552 }
3553
3554 if (! FcPatternAddInteger (pattern, FC_RGBA, rgba))
3555 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3556 }
3557 }
3558
3559 if (options->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
3560 {
3561 if (FcPatternGet (pattern, FC_LCD_FILTER, 0, &v) == FcResultNoMatch)
3562 {
3563 int lcd_filter;
3564
3565 switch (options->lcd_filter) {
3566 case CAIRO_LCD_FILTER_NONE:
3567 lcd_filter = FT_LCD_FILTER_NONE;
3568 break;
3569 case CAIRO_LCD_FILTER_DEFAULT:
3570 case CAIRO_LCD_FILTER_INTRA_PIXEL:
3571 lcd_filter = FT_LCD_FILTER_LEGACY;
3572 break;
3573 case CAIRO_LCD_FILTER_FIR3:
3574 lcd_filter = FT_LCD_FILTER_LIGHT;
3575 break;
3576 default:
3577 case CAIRO_LCD_FILTER_FIR5:
3578 lcd_filter = FT_LCD_FILTER_DEFAULT;
3579 break;
3580 }
3581
3582 if (! FcPatternAddInteger (pattern, FC_LCD_FILTER, lcd_filter))
3583 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3584 }
3585 }
3586
3587 if (options->hint_style != CAIRO_HINT_STYLE_DEFAULT)
3588 {
3589 if (FcPatternGet (pattern, FC_HINTING, 0, &v) == FcResultNoMatch)
3590 {
3591 if (! FcPatternAddBool (pattern,
3592 FC_HINTING,
3593 options->hint_style != CAIRO_HINT_STYLE_NONE))
3594 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3595 }
3596
3597 #ifdef FC_HINT_STYLE
3598 if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
3599 {
3600 int hint_style;
3601
3602 switch (options->hint_style) {
3603 case CAIRO_HINT_STYLE_NONE:
3604 hint_style = FC_HINT_NONE;
3605 break;
3606 case CAIRO_HINT_STYLE_SLIGHT:
3607 hint_style = FC_HINT_SLIGHT;
3608 break;
3609 case CAIRO_HINT_STYLE_MEDIUM:
3610 hint_style = FC_HINT_MEDIUM;
3611 break;
3612 case CAIRO_HINT_STYLE_FULL:
3613 case CAIRO_HINT_STYLE_DEFAULT:
3614 default:
3615 hint_style = FC_HINT_FULL;
3616 break;
3617 }
3618
3619 if (! FcPatternAddInteger (pattern, FC_HINT_STYLE, hint_style))
3620 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3621 }
3622 #endif
3623 }
3624
3625 return CAIRO_STATUS_SUCCESS;
3626 }
3627
3628 /**
3629 * cairo_ft_font_options_substitute:
3630 * @options: a #cairo_font_options_t object
3631 * @pattern: an existing #FcPattern
3632 *
3633 * Add options to a #FcPattern based on a #cairo_font_options_t font
3634 * options object. Options that are already in the pattern, are not overridden,
3635 * so you should call this function after calling FcConfigSubstitute() (the
3636 * user's settings should override options based on the surface type), but
3637 * before calling FcDefaultSubstitute().
3638 *
3639 * Since: 1.0
3640 **/
3641 void
cairo_ft_font_options_substitute(const cairo_font_options_t * options,FcPattern * pattern)3642 cairo_ft_font_options_substitute (const cairo_font_options_t *options,
3643 FcPattern *pattern)
3644 {
3645 if (cairo_font_options_status ((cairo_font_options_t *) options))
3646 return;
3647
3648 _cairo_ft_font_options_substitute (options, pattern);
3649 }
3650
3651 static cairo_font_face_t *
_cairo_ft_resolve_pattern(FcPattern * pattern,const cairo_matrix_t * font_matrix,const cairo_matrix_t * ctm,const cairo_font_options_t * font_options)3652 _cairo_ft_resolve_pattern (FcPattern *pattern,
3653 const cairo_matrix_t *font_matrix,
3654 const cairo_matrix_t *ctm,
3655 const cairo_font_options_t *font_options)
3656 {
3657 cairo_status_t status;
3658
3659 cairo_matrix_t scale;
3660 FcPattern *resolved;
3661 cairo_ft_font_transform_t sf;
3662 FcResult result;
3663 cairo_ft_unscaled_font_t *unscaled;
3664 cairo_ft_options_t ft_options;
3665 cairo_font_face_t *font_face;
3666
3667 scale = *ctm;
3668 scale.x0 = scale.y0 = 0;
3669 cairo_matrix_multiply (&scale,
3670 font_matrix,
3671 &scale);
3672
3673 status = _compute_transform (&sf, &scale, NULL);
3674 if (unlikely (status))
3675 return (cairo_font_face_t *)&_cairo_font_face_nil;
3676
3677 pattern = FcPatternDuplicate (pattern);
3678 if (pattern == NULL)
3679 return (cairo_font_face_t *)&_cairo_font_face_nil;
3680
3681 if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
3682 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3683 goto FREE_PATTERN;
3684 }
3685
3686 if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
3687 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3688 goto FREE_PATTERN;
3689 }
3690
3691 status = _cairo_ft_font_options_substitute (font_options, pattern);
3692 if (status) {
3693 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3694 goto FREE_PATTERN;
3695 }
3696
3697 FcDefaultSubstitute (pattern);
3698
3699 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3700 if (unlikely (status)) {
3701 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3702 goto FREE_PATTERN;
3703 }
3704
3705 if (unscaled == NULL) {
3706 resolved = FcFontMatch (NULL, pattern, &result);
3707 if (!resolved) {
3708 /* We failed to find any font. Substitute twin so that the user can
3709 * see something (and hopefully recognise that the font is missing)
3710 * and not just receive a NO_MEMORY error during rendering.
3711 */
3712 font_face = _cairo_font_face_twin_create_fallback ();
3713 goto FREE_PATTERN;
3714 }
3715
3716 status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
3717 if (unlikely (status || unscaled == NULL)) {
3718 font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
3719 goto FREE_RESOLVED;
3720 }
3721 } else
3722 resolved = pattern;
3723
3724 _get_pattern_ft_options (resolved, &ft_options);
3725 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3726 _cairo_ft_options_fini (&ft_options);
3727 _cairo_unscaled_font_destroy (&unscaled->base);
3728
3729 FREE_RESOLVED:
3730 if (resolved != pattern)
3731 FcPatternDestroy (resolved);
3732
3733 FREE_PATTERN:
3734 FcPatternDestroy (pattern);
3735
3736 return font_face;
3737 }
3738
3739 /**
3740 * cairo_ft_font_face_create_for_pattern:
3741 * @pattern: A fontconfig pattern. Cairo makes a copy of the pattern
3742 * if it needs to. You are free to modify or free @pattern after this call.
3743 *
3744 * Creates a new font face for the FreeType font backend based on a
3745 * fontconfig pattern. This font can then be used with
3746 * cairo_set_font_face() or cairo_scaled_font_create(). The
3747 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3748 * also for the FreeType backend and can be used with functions such
3749 * as cairo_ft_scaled_font_lock_face().
3750 *
3751 * Font rendering options are represented both here and when you
3752 * call cairo_scaled_font_create(). Font options that have a representation
3753 * in a #FcPattern must be passed in here; to modify #FcPattern
3754 * appropriately to reflect the options in a #cairo_font_options_t, call
3755 * cairo_ft_font_options_substitute().
3756 *
3757 * The pattern's FC_FT_FACE element is inspected first and if that is set,
3758 * that will be the FreeType font face associated with the returned cairo
3759 * font face. Otherwise the FC_FILE element is checked. If it's set,
3760 * that and the value of the FC_INDEX element (defaults to zero) of @pattern
3761 * are used to load a font face from file.
3762 *
3763 * If both steps from the previous paragraph fails, @pattern will be passed
3764 * to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
3765 * and the resulting font pattern is used.
3766 *
3767 * If the FC_FT_FACE element of @pattern is set, the user is responsible
3768 * for making sure that the referenced FT_Face remains valid for the life
3769 * time of the returned #cairo_font_face_t. See
3770 * cairo_ft_font_face_create_for_ft_face() for an example of how to couple
3771 * the life time of the FT_Face to that of the cairo font-face.
3772 *
3773 * Return value: a newly created #cairo_font_face_t. Free with
3774 * cairo_font_face_destroy() when you are done using it.
3775 *
3776 * Since: 1.0
3777 **/
3778 cairo_font_face_t *
cairo_ft_font_face_create_for_pattern(FcPattern * pattern)3779 cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
3780 {
3781 cairo_ft_unscaled_font_t *unscaled;
3782 cairo_font_face_t *font_face;
3783 cairo_ft_options_t ft_options;
3784 cairo_status_t status;
3785
3786 status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
3787 if (unlikely (status)) {
3788 if (status == CAIRO_STATUS_FILE_NOT_FOUND)
3789 return (cairo_font_face_t *) &_cairo_font_face_nil_file_not_found;
3790 else
3791 return (cairo_font_face_t *) &_cairo_font_face_nil;
3792 }
3793 if (unlikely (unscaled == NULL)) {
3794 /* Store the pattern. We will resolve it and create unscaled
3795 * font when creating scaled fonts */
3796 return _cairo_ft_font_face_create_for_pattern (pattern);
3797 }
3798
3799 _get_pattern_ft_options (pattern, &ft_options);
3800 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3801 _cairo_ft_options_fini (&ft_options);
3802 _cairo_unscaled_font_destroy (&unscaled->base);
3803
3804 return font_face;
3805 }
3806 #endif
3807
3808 /**
3809 * cairo_ft_font_face_create_for_ft_face:
3810 * @face: A FreeType face object, already opened. This must
3811 * be kept around until the face's ref_count drops to
3812 * zero and it is freed. Since the face may be referenced
3813 * internally to Cairo, the best way to determine when it
3814 * is safe to free the face is to pass a
3815 * #cairo_destroy_func_t to cairo_font_face_set_user_data()
3816 * @load_flags: flags to pass to FT_Load_Glyph when loading
3817 * glyphs from the font. These flags are OR'ed together with
3818 * the flags derived from the #cairo_font_options_t passed
3819 * to cairo_scaled_font_create(), so only a few values such
3820 * as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
3821 * are useful. You should not pass any of the flags affecting
3822 * the load target, such as %FT_LOAD_TARGET_LIGHT.
3823 *
3824 * Creates a new font face for the FreeType font backend from a
3825 * pre-opened FreeType face. This font can then be used with
3826 * cairo_set_font_face() or cairo_scaled_font_create(). The
3827 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
3828 * also for the FreeType backend and can be used with functions such
3829 * as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
3830 * to the FT_Face alive in a font-cache and the exact lifetime of the reference
3831 * depends highly upon the exact usage pattern and is subject to external
3832 * factors. You must not call FT_Done_Face() before the last reference to the
3833 * #cairo_font_face_t has been dropped.
3834 *
3835 * As an example, below is how one might correctly couple the lifetime of
3836 * the FreeType face object to the #cairo_font_face_t.
3837 *
3838 * <informalexample><programlisting>
3839 * static const cairo_user_data_key_t key;
3840 *
3841 * font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
3842 * status = cairo_font_face_set_user_data (font_face, &key,
3843 * ft_face, (cairo_destroy_func_t) FT_Done_Face);
3844 * if (status) {
3845 * cairo_font_face_destroy (font_face);
3846 * FT_Done_Face (ft_face);
3847 * return ERROR;
3848 * }
3849 * </programlisting></informalexample>
3850 *
3851 * Return value: a newly created #cairo_font_face_t. Free with
3852 * cairo_font_face_destroy() when you are done using it.
3853 *
3854 * Since: 1.0
3855 **/
3856 cairo_font_face_t *
cairo_ft_font_face_create_for_ft_face(FT_Face face,int load_flags,unsigned int synth_flags,void * face_context)3857 cairo_ft_font_face_create_for_ft_face (FT_Face face,
3858 int load_flags,
3859 unsigned int synth_flags,
3860 void *face_context)
3861 {
3862 cairo_ft_unscaled_font_t *unscaled;
3863 cairo_font_face_t *font_face;
3864 cairo_ft_options_t ft_options;
3865 cairo_status_t status;
3866
3867 status = _cairo_ft_unscaled_font_create_from_face (face, face_context,
3868 &unscaled);
3869 if (unlikely (status))
3870 return (cairo_font_face_t *)&_cairo_font_face_nil;
3871
3872 ft_options.load_flags = load_flags;
3873 ft_options.synth_flags = synth_flags;
3874 _cairo_font_options_init_default (&ft_options.base);
3875
3876 font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
3877 _cairo_unscaled_font_destroy (&unscaled->base);
3878
3879 return font_face;
3880 }
3881
3882 /**
3883 * cairo_ft_font_face_set_synthesize:
3884 * @font_face: The #cairo_ft_font_face_t object to modify
3885 * @synth_flags: the set of synthesis options to enable
3886 *
3887 * FreeType provides the ability to synthesize different glyphs from a base
3888 * font, which is useful if you lack those glyphs from a true bold or oblique
3889 * font. See also #cairo_ft_synthesize_t.
3890 *
3891 * Since: 1.12
3892 **/
3893 void
cairo_ft_font_face_set_synthesize(cairo_font_face_t * font_face,unsigned int synth_flags)3894 cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
3895 unsigned int synth_flags)
3896 {
3897 cairo_ft_font_face_t *ft;
3898
3899 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3900 return;
3901
3902 ft = (cairo_ft_font_face_t *) font_face;
3903 ft->ft_options.synth_flags |= synth_flags;
3904 }
3905
3906 /**
3907 * cairo_ft_font_face_unset_synthesize:
3908 * @font_face: The #cairo_ft_font_face_t object to modify
3909 * @synth_flags: the set of synthesis options to disable
3910 *
3911 * See cairo_ft_font_face_set_synthesize().
3912 *
3913 * Since: 1.12
3914 **/
3915 void
cairo_ft_font_face_unset_synthesize(cairo_font_face_t * font_face,unsigned int synth_flags)3916 cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
3917 unsigned int synth_flags)
3918 {
3919 cairo_ft_font_face_t *ft;
3920
3921 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3922 return;
3923
3924 ft = (cairo_ft_font_face_t *) font_face;
3925 ft->ft_options.synth_flags &= ~synth_flags;
3926 }
3927
3928 /**
3929 * cairo_ft_font_face_get_synthesize:
3930 * @font_face: The #cairo_ft_font_face_t object to query
3931 *
3932 * See #cairo_ft_synthesize_t.
3933 *
3934 * Returns: the current set of synthesis options.
3935 *
3936 * Since: 1.12
3937 **/
3938 unsigned int
cairo_ft_font_face_get_synthesize(cairo_font_face_t * font_face)3939 cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
3940 {
3941 cairo_ft_font_face_t *ft;
3942
3943 if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
3944 return 0;
3945
3946 ft = (cairo_ft_font_face_t *) font_face;
3947 return ft->ft_options.synth_flags;
3948 }
3949
3950 /**
3951 * cairo_ft_scaled_font_lock_face:
3952 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
3953 * object can be created by calling cairo_scaled_font_create() on a
3954 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
3955 * cairo_ft_font_face_create_for_ft_face()).
3956 *
3957 * cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
3958 * backend font and scales it appropriately for the font and applies OpenType
3959 * font variations if applicable. You must
3960 * release the face with cairo_ft_scaled_font_unlock_face()
3961 * when you are done using it. Since the #FT_Face object can be
3962 * shared between multiple #cairo_scaled_font_t objects, you must not
3963 * lock any other font objects until you unlock this one. A count is
3964 * kept of the number of times cairo_ft_scaled_font_lock_face() is
3965 * called. cairo_ft_scaled_font_unlock_face() must be called the same number
3966 * of times.
3967 *
3968 * You must be careful when using this function in a library or in a
3969 * threaded application, because freetype's design makes it unsafe to
3970 * call freetype functions simultaneously from multiple threads, (even
3971 * if using distinct FT_Face objects). Because of this, application
3972 * code that acquires an FT_Face object with this call must add its
3973 * own locking to protect any use of that object, (and which also must
3974 * protect any other calls into cairo as almost any cairo function
3975 * might result in a call into the freetype library).
3976 *
3977 * Return value: The #FT_Face object for @font, scaled appropriately,
3978 * or %NULL if @scaled_font is in an error state (see
3979 * cairo_scaled_font_status()) or there is insufficient memory.
3980 *
3981 * Since: 1.0
3982 **/
3983 FT_Face
cairo_ft_scaled_font_lock_face(cairo_scaled_font_t * abstract_font)3984 cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
3985 {
3986 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
3987 FT_Face face;
3988 cairo_status_t status;
3989
3990 if (! _cairo_scaled_font_is_ft (abstract_font)) {
3991 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
3992 return NULL;
3993 }
3994
3995 if (scaled_font->base.status)
3996 return NULL;
3997
3998 face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled);
3999 if (unlikely (face == NULL)) {
4000 status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY);
4001 return NULL;
4002 }
4003
4004 status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
4005 &scaled_font->base.scale);
4006 if (unlikely (status)) {
4007 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
4008 status = _cairo_scaled_font_set_error (&scaled_font->base, status);
4009 return NULL;
4010 }
4011
4012 cairo_ft_apply_variations (face, scaled_font);
4013
4014 /* Note: We deliberately release the unscaled font's mutex here,
4015 * so that we are not holding a lock across two separate calls to
4016 * cairo function, (which would give the application some
4017 * opportunity for creating deadlock. This is obviously unsafe,
4018 * but as documented, the user must add manual locking when using
4019 * this function. */
4020 CAIRO_FT_UNLOCK (scaled_font->unscaled);
4021
4022 return face;
4023 }
4024
4025 /**
4026 * cairo_ft_scaled_font_unlock_face:
4027 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
4028 * object can be created by calling cairo_scaled_font_create() on a
4029 * FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
4030 * cairo_ft_font_face_create_for_ft_face()).
4031 *
4032 * Releases a face obtained with cairo_ft_scaled_font_lock_face().
4033 *
4034 * Since: 1.0
4035 **/
4036 void
cairo_ft_scaled_font_unlock_face(cairo_scaled_font_t * abstract_font)4037 cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
4038 {
4039 cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
4040
4041 if (! _cairo_scaled_font_is_ft (abstract_font)) {
4042 _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
4043 return;
4044 }
4045
4046 if (scaled_font->base.status)
4047 return;
4048
4049 /* Note: We released the unscaled font's mutex at the end of
4050 * cairo_ft_scaled_font_lock_face, so we have to acquire it again
4051 * as _cairo_ft_unscaled_font_unlock_face expects it to be held
4052 * when we call into it. */
4053 CAIRO_FT_LOCK (scaled_font->unscaled);
4054
4055 _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
4056 }
4057
4058 static cairo_bool_t
_cairo_ft_scaled_font_is_vertical(cairo_scaled_font_t * scaled_font)4059 _cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font)
4060 {
4061 cairo_ft_scaled_font_t *ft_scaled_font;
4062
4063 if (!_cairo_scaled_font_is_ft (scaled_font))
4064 return FALSE;
4065
4066 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
4067 if (ft_scaled_font->ft_options.load_flags & FT_LOAD_VERTICAL_LAYOUT)
4068 return TRUE;
4069 return FALSE;
4070 }
4071
4072 unsigned int
_cairo_ft_scaled_font_get_load_flags(cairo_scaled_font_t * scaled_font)4073 _cairo_ft_scaled_font_get_load_flags (cairo_scaled_font_t *scaled_font)
4074 {
4075 cairo_ft_scaled_font_t *ft_scaled_font;
4076
4077 if (! _cairo_scaled_font_is_ft (scaled_font))
4078 return 0;
4079
4080 ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
4081 return ft_scaled_font->ft_options.load_flags;
4082 }
4083
4084 void
_cairo_ft_font_reset_static_data(void)4085 _cairo_ft_font_reset_static_data (void)
4086 {
4087 _cairo_ft_unscaled_font_map_destroy ();
4088 }
4089