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-list-inline.h"
48
#include "cairo-path-private.h"
49
#include "cairo-pattern-private.h"
50
#include "cairo-pixman-private.h"
51
#include "cairo-recording-surface-private.h"
52

            
53
#include <float.h>
54

            
55
#include "cairo-fontconfig-private.h"
56

            
57
#include <ft2build.h>
58
#include FT_FREETYPE_H
59
#include FT_OUTLINE_H
60
#include FT_IMAGE_H
61
#include FT_BITMAP_H
62
#include FT_TRUETYPE_TABLES_H
63
#include FT_FONT_FORMATS_H
64
#include FT_MULTIPLE_MASTERS_H
65
#include FT_SYNTHESIS_H
66
#include FT_LCD_FILTER_H
67

            
68
#if HAVE_FT_SVG_DOCUMENT
69
#include FT_OTSVG_H
70
#endif
71

            
72
#if HAVE_UNISTD_H
73
#include <unistd.h>
74
#elif !defined(access)
75
#define access(p, m) 0
76
#endif
77

            
78
/*  FreeType version older than 2.11 does not have the FT_RENDER_MODE_SDF enum value in FT_Render_Mode */
79
#if FREETYPE_MAJOR > 2 || (FREETYPE_MAJOR == 2 && FREETYPE_MINOR >= 11)
80
#define HAVE_FT_RENDER_MODE_SDF 1
81
#endif
82

            
83
#define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
84
#define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0))
85
#define DOUBLE_FROM_16_16(t) ((double)(t) / 65536.0)
86

            
87
/* SCALE() mimics code from commit 399b00a99b2bbc1c56a05974c936aa69a08021f5
88
 * concerning a potential division by 0, but instead of doing a * (1/b), it
89
 * does a/b, thus improving the accuracy. With a * (1/b), for a bitmap font
90
 * of size 13, the computed -y_bearing was 0x1.6000000000001p+3 instead of
91
 * 0x1.6p+3 (= 11). This triggered a bug in GNU Emacs (when built against
92
 * cairo), which rounded the value to an integer with ceil().
93
 * Details:
94
 *   https://gitlab.freedesktop.org/cairo/cairo/-/issues/503
95
 *   https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44284
96
 *
97
 * Note that rounding errors are not necessarily expected by applications
98
 * in simple cases like the GNU Emacs one (an identity transformation,
99
 * which should normally leave the inputs unchanged). However, with the
100
 * current cairo code, due to the scaling, there is no guarantee that
101
 * rounding errors will always be avoided at the end. For instance,
102
 * (a/b)*b may be different from a, but this is still better than doing
103
 * (a*(1/b))*b.
104
 *
105
 * According to the commit mentioned above, avoiding a division by zero
106
 * was an attempt to fix
107
 *   https://bugzilla.gnome.org/show_bug.cgi?id=311299
108
 * but this did not actually solve the problem. So it might be possible
109
 * to change SCALE() to just do (a) / (b).
110
 */
111
#define SCALE(a,b) ((b) == 0 ? 0.0 : (a) / (b))
112

            
113
/* This is the max number of FT_face objects we keep open at once
114
 */
115
#define MAX_OPEN_FACES 10
116

            
117
/**
118
 * SECTION:cairo-ft
119
 * @Title: FreeType Fonts
120
 * @Short_Description: Font support for FreeType
121
 * @See_Also: #cairo_font_face_t
122
 *
123
 * The FreeType font backend is primarily used to render text on GNU/Linux
124
 * systems, but can be used on other platforms too.
125
 **/
126

            
127
/**
128
 * CAIRO_HAS_FT_FONT:
129
 *
130
 * Defined if the FreeType font backend is available.
131
 * This macro can be used to conditionally compile backend-specific code.
132
 *
133
 * Since: 1.0
134
 **/
135

            
136
/**
137
 * CAIRO_HAS_FC_FONT:
138
 *
139
 * Defined if the Fontconfig-specific functions of the FreeType font backend
140
 * are available.
141
 * This macro can be used to conditionally compile backend-specific code.
142
 *
143
 * Since: 1.10
144
 **/
145

            
146
/*
147
 * The simple 2x2 matrix is converted into separate scale and shape
148
 * factors so that hinting works right
149
 */
150

            
151
typedef struct _cairo_ft_font_transform {
152
    double  x_scale, y_scale;
153
    double  shape[2][2];
154
} cairo_ft_font_transform_t;
155

            
156
/*
157
 * We create an object that corresponds to a single font on the disk;
158
 * (identified by a filename/id pair) these are shared between all
159
 * fonts using that file.  For cairo_ft_font_face_create_for_ft_face(), we
160
 * just create a one-off version with a permanent face value.
161
 */
162

            
163
typedef struct _cairo_ft_font_face cairo_ft_font_face_t;
164

            
165
struct _cairo_ft_unscaled_font {
166
    cairo_unscaled_font_t base;
167

            
168
    cairo_bool_t from_face; /* was the FT_Face provided by user? */
169
    FT_Face face;	    /* provided or cached face */
170

            
171
    /* only set if from_face is false */
172
    char *filename;
173
    int id;
174

            
175
    /* We temporarily scale the unscaled font as needed */
176
    cairo_bool_t have_scale;
177
    cairo_matrix_t current_scale;
178
    double x_scale;		/* Extracted X scale factor */
179
    double y_scale;             /* Extracted Y scale factor */
180
    cairo_bool_t have_shape;	/* true if the current scale has a non-scale component*/
181
    cairo_matrix_t current_shape;
182
    FT_Matrix Current_Shape;
183

            
184
    unsigned int have_color_set  : 1;
185
    unsigned int have_color      : 1;  /* true if the font contains color glyphs */
186
    FT_Fixed *variations;              /* variation settings that FT_Face came */
187
    unsigned int num_palettes;
188

            
189
    cairo_mutex_t mutex;
190
    int lock_count;
191

            
192
    cairo_ft_font_face_t *faces;	/* Linked list of faces for this font */
193
};
194

            
195
static int
196
_cairo_ft_unscaled_font_keys_equal (const void *key_a,
197
				    const void *key_b);
198

            
199
static void
200
_cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled);
201

            
202
typedef struct _cairo_ft_options {
203
    cairo_font_options_t base;
204
    unsigned int load_flags; /* flags for FT_Load_Glyph */
205
    unsigned int synth_flags;
206
} cairo_ft_options_t;
207

            
208
static void
209
329
_cairo_ft_options_init_copy (cairo_ft_options_t       *options,
210
                             const cairo_ft_options_t *other)
211
{
212
329
    _cairo_font_options_init_copy (&options->base, &other->base);
213
329
    options->load_flags = other->load_flags;
214
329
    options->synth_flags = other->synth_flags;
215
329
}
216

            
217
static void
218
1860
_cairo_ft_options_fini (cairo_ft_options_t *options)
219
{
220
1860
    _cairo_font_options_fini (&options->base);
221
1860
}
222

            
223
struct _cairo_ft_font_face {
224
    cairo_font_face_t base;
225

            
226
    cairo_ft_unscaled_font_t *unscaled;
227
    cairo_ft_options_t ft_options;
228
    cairo_ft_font_face_t *next;
229

            
230
#if CAIRO_HAS_FC_FONT
231
    FcPattern *pattern; /* if pattern is set, the above fields will be NULL */
232
    cairo_font_face_t *resolved_font_face;
233
    FcConfig *resolved_config;
234
#endif
235
};
236

            
237
static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend;
238

            
239
#if CAIRO_HAS_FC_FONT
240
static cairo_status_t
241
_cairo_ft_font_options_substitute (const cairo_font_options_t *options,
242
				   FcPattern                  *pattern);
243

            
244
static cairo_font_face_t *
245
_cairo_ft_resolve_pattern (FcPattern		      *pattern,
246
			   const cairo_matrix_t       *font_matrix,
247
			   const cairo_matrix_t       *ctm,
248
			   const cairo_font_options_t *options);
249

            
250
#endif
251

            
252
cairo_status_t
253
_cairo_ft_to_cairo_error (FT_Error error)
254
{
255
  /* Currently we don't get many (any?) useful statuses here.
256
   * Populate as needed. */
257
  switch (error)
258
  {
259
      case FT_Err_Ok:
260
	  return CAIRO_STATUS_SUCCESS;
261
      case FT_Err_Out_Of_Memory:
262
	  return CAIRO_STATUS_NO_MEMORY;
263
      default:
264
	  return CAIRO_STATUS_FREETYPE_ERROR;
265
  }
266
}
267

            
268
/*
269
 * We maintain a hash table to map file/id => #cairo_ft_unscaled_font_t.
270
 * The hash table itself isn't limited in size. However, we limit the
271
 * number of FT_Face objects we keep around; when we've exceeded that
272
 * limit and need to create a new FT_Face, we dump the FT_Face from a
273
 * random #cairo_ft_unscaled_font_t which has an unlocked FT_Face, (if
274
 * there are any).
275
 */
276

            
277
typedef struct _cairo_ft_unscaled_font_map {
278
    cairo_hash_table_t *hash_table;
279
    FT_Library ft_library;
280
    int num_open_faces;
281
} cairo_ft_unscaled_font_map_t;
282

            
283
static cairo_ft_unscaled_font_map_t *cairo_ft_unscaled_font_map = NULL;
284

            
285

            
286
static FT_Face
287
_cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled);
288

            
289
static void
290
_cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled);
291

            
292
static cairo_bool_t
293
_cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font);
294

            
295

            
296
static void
297
6
_font_map_release_face_lock_held (cairo_ft_unscaled_font_map_t *font_map,
298
				  cairo_ft_unscaled_font_t *unscaled)
299
{
300
6
    if (unscaled->face) {
301
6
	FT_Done_Face (unscaled->face);
302
6
	unscaled->face = NULL;
303
6
	unscaled->have_scale = FALSE;
304

            
305
6
	font_map->num_open_faces--;
306
    }
307
6
}
308

            
309
static cairo_status_t
310
263
_cairo_ft_unscaled_font_map_create (void)
311
{
312
    cairo_ft_unscaled_font_map_t *font_map;
313

            
314
    /* This function is only intended to be called from
315
     * _cairo_ft_unscaled_font_map_lock. So we'll crash if we can
316
     * detect some other call path. */
317
263
    assert (cairo_ft_unscaled_font_map == NULL);
318

            
319
263
    font_map = _cairo_calloc (sizeof (cairo_ft_unscaled_font_map_t));
320
263
    if (unlikely (font_map == NULL))
321
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
322

            
323
263
    font_map->hash_table =
324
263
	_cairo_hash_table_create (_cairo_ft_unscaled_font_keys_equal);
325

            
326
263
    if (unlikely (font_map->hash_table == NULL))
327
	goto FAIL;
328

            
329
263
    if (unlikely (FT_Init_FreeType (&font_map->ft_library)))
330
	goto FAIL;
331

            
332
263
    font_map->num_open_faces = 0;
333

            
334
263
    cairo_ft_unscaled_font_map = font_map;
335
263
    return CAIRO_STATUS_SUCCESS;
336

            
337
FAIL:
338
    if (font_map->hash_table)
339
	_cairo_hash_table_destroy (font_map->hash_table);
340
    free (font_map);
341

            
342
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
343
}
344

            
345

            
346
static void
347
_cairo_ft_unscaled_font_map_pluck_entry (void *entry, void *closure)
348
{
349
    cairo_ft_unscaled_font_t *unscaled = entry;
350
    cairo_ft_unscaled_font_map_t *font_map = closure;
351

            
352
    _cairo_hash_table_remove (font_map->hash_table,
353
			      &unscaled->base.hash_entry);
354

            
355
    if (! unscaled->from_face)
356
	_font_map_release_face_lock_held (font_map, unscaled);
357

            
358
    _cairo_ft_unscaled_font_fini (unscaled);
359
    free (unscaled);
360
}
361

            
362
static void
363
608
_cairo_ft_unscaled_font_map_destroy (void)
364
{
365
    cairo_ft_unscaled_font_map_t *font_map;
366

            
367
608
    CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
368
608
    font_map = cairo_ft_unscaled_font_map;
369
608
    cairo_ft_unscaled_font_map = NULL;
370
608
    CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
371

            
372
608
    if (font_map != NULL) {
373
	_cairo_hash_table_foreach (font_map->hash_table,
374
				   _cairo_ft_unscaled_font_map_pluck_entry,
375
				   font_map);
376
	assert (font_map->num_open_faces == 0);
377

            
378
	FT_Done_FreeType (font_map->ft_library);
379

            
380
	_cairo_hash_table_destroy (font_map->hash_table);
381

            
382
	free (font_map);
383
    }
384
608
}
385

            
386
static cairo_ft_unscaled_font_map_t *
387
111316
_cairo_ft_unscaled_font_map_lock (void)
388
{
389
    CAIRO_MUTEX_INITIALIZE ();
390

            
391
111316
    CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
392

            
393
111316
    if (unlikely (cairo_ft_unscaled_font_map == NULL)) {
394
263
	if (unlikely (_cairo_ft_unscaled_font_map_create ())) {
395
	    CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
396
	    return NULL;
397
	}
398
    }
399

            
400
111316
    return cairo_ft_unscaled_font_map;
401
}
402

            
403
static void
404
111316
_cairo_ft_unscaled_font_map_unlock (void)
405
{
406
111316
    CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
407
111316
}
408

            
409
static void
410
111340
_cairo_ft_unscaled_font_init_key (cairo_ft_unscaled_font_t *key,
411
				  cairo_bool_t              from_face,
412
				  char			   *filename,
413
				  int			    id,
414
				  FT_Face		    face)
415
{
416
    uintptr_t hash;
417

            
418
111340
    key->from_face = from_face;
419
111340
    key->filename = filename;
420
111340
    key->id = id;
421
111340
    key->face = face;
422

            
423
111340
    hash = _cairo_hash_string (filename);
424
    /* the constants are just arbitrary primes */
425
111340
    hash += ((uintptr_t) id) * 1607;
426
111340
    hash += ((uintptr_t) face) * 2137;
427

            
428
111340
    key->base.hash_entry.hash = hash;
429
111340
}
430

            
431
/**
432
 * _cairo_ft_unscaled_font_init:
433
 *
434
 * Initialize a #cairo_ft_unscaled_font_t.
435
 *
436
 * There are two basic flavors of #cairo_ft_unscaled_font_t, one
437
 * created from an FT_Face and the other created from a filename/id
438
 * pair. These two flavors are identified as from_face and !from_face.
439
 *
440
 * To initialize a from_face font, pass filename==%NULL, id=0 and the
441
 * desired face.
442
 *
443
 * To initialize a !from_face font, pass the filename/id as desired
444
 * and face==%NULL.
445
 *
446
 * Note that the code handles these two flavors in very distinct
447
 * ways. For example there is a hash_table mapping
448
 * filename/id->#cairo_unscaled_font_t in the !from_face case, but no
449
 * parallel in the from_face case, (where the calling code would have
450
 * to do its own mapping to ensure similar sharing).
451
 **/
452
static cairo_status_t
453
320
_cairo_ft_unscaled_font_init (cairo_ft_unscaled_font_t *unscaled,
454
			      cairo_bool_t              from_face,
455
			      const char	       *filename,
456
			      int			id,
457
			      FT_Face			face)
458
{
459
320
    _cairo_unscaled_font_init (&unscaled->base,
460
			       &cairo_ft_unscaled_font_backend);
461

            
462
320
    unscaled->variations = NULL;
463

            
464
320
    if (from_face) {
465
	FT_MM_Var *ft_mm_var;
466
30
	unscaled->from_face = TRUE;
467
30
	_cairo_ft_unscaled_font_init_key (unscaled, TRUE, NULL, id, face);
468

            
469

            
470
30
        unscaled->have_color = FT_HAS_COLOR (face) != 0;
471
30
        unscaled->have_color_set = TRUE;
472
30
	if (FT_Get_MM_Var (face, &ft_mm_var) == 0) {
473
3
	    unscaled->variations = _cairo_calloc_ab (ft_mm_var->num_axis, sizeof (FT_Fixed));
474
3
	    if (unscaled->variations)
475
3
		FT_Get_Var_Design_Coordinates (face, ft_mm_var->num_axis, unscaled->variations);
476
3
	    FT_Done_MM_Var (face->glyph->library, ft_mm_var);
477
	}
478
    } else {
479
	char *filename_copy;
480

            
481
290
	unscaled->from_face = FALSE;
482
290
	unscaled->face = NULL;
483

            
484
290
	filename_copy = strdup (filename);
485
290
	if (unlikely (filename_copy == NULL))
486
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
487

            
488
290
	_cairo_ft_unscaled_font_init_key (unscaled, FALSE, filename_copy, id, NULL);
489

            
490
290
	unscaled->have_color_set = FALSE;
491
    }
492

            
493
320
    unscaled->have_scale = FALSE;
494
320
    CAIRO_MUTEX_INIT (unscaled->mutex);
495
320
    unscaled->lock_count = 0;
496

            
497
320
    unscaled->faces = NULL;
498

            
499
320
    return CAIRO_STATUS_SUCCESS;
500
}
501

            
502
/**
503
 * _cairo_ft_unscaled_font_fini:
504
 *
505
 * Free all data associated with a #cairo_ft_unscaled_font_t.
506
 *
507
 * CAUTION: The unscaled->face field must be %NULL before calling this
508
 * function. This is because the #cairo_ft_unscaled_font_t_map keeps a
509
 * count of these faces (font_map->num_open_faces) so it maintains the
510
 * unscaled->face field while it has its lock held. See
511
 * _font_map_release_face_lock_held().
512
 **/
513
static void
514
6
_cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled)
515
{
516
6
    assert (unscaled->face == NULL);
517

            
518
6
    free (unscaled->filename);
519
6
    unscaled->filename = NULL;
520

            
521
6
    free (unscaled->variations);
522

            
523
6
    CAIRO_MUTEX_FINI (unscaled->mutex);
524
6
}
525

            
526
static int
527
110700
_cairo_ft_unscaled_font_keys_equal (const void *key_a,
528
				    const void *key_b)
529
{
530
110700
    const cairo_ft_unscaled_font_t *unscaled_a = key_a;
531
110700
    const cairo_ft_unscaled_font_t *unscaled_b = key_b;
532

            
533
110700
    if (unscaled_a->id == unscaled_b->id &&
534
110700
	unscaled_a->from_face == unscaled_b->from_face)
535
     {
536
110700
        if (unscaled_a->from_face)
537
110700
	    return unscaled_a->face == unscaled_b->face;
538

            
539
	if (unscaled_a->filename == NULL && unscaled_b->filename == NULL)
540
	    return TRUE;
541
	else if (unscaled_a->filename == NULL || unscaled_b->filename == NULL)
542
	    return FALSE;
543
	else
544
	    return (strcmp (unscaled_a->filename, unscaled_b->filename) == 0);
545
    }
546

            
547
    return FALSE;
548
}
549

            
550
/* Finds or creates a #cairo_ft_unscaled_font_t for the filename/id from
551
 * pattern.  Returns a new reference to the unscaled font.
552
 */
553
static cairo_status_t
554
111020
_cairo_ft_unscaled_font_create_internal (cairo_bool_t from_face,
555
					 char *filename,
556
					 int id,
557
					 FT_Face font_face,
558
					 cairo_ft_unscaled_font_t **out)
559
{
560
    cairo_ft_unscaled_font_t key, *unscaled;
561
    cairo_ft_unscaled_font_map_t *font_map;
562
    cairo_status_t status;
563

            
564
111020
    font_map = _cairo_ft_unscaled_font_map_lock ();
565
111020
    if (unlikely (font_map == NULL))
566
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
567

            
568
111020
    _cairo_ft_unscaled_font_init_key (&key, from_face, filename, id, font_face);
569

            
570
    /* Return existing unscaled font if it exists in the hash table. */
571
111020
    unscaled = _cairo_hash_table_lookup (font_map->hash_table,
572
					 &key.base.hash_entry);
573
111020
    if (unscaled != NULL) {
574
110700
	_cairo_unscaled_font_reference (&unscaled->base);
575
110700
	goto DONE;
576
    }
577

            
578
    /* Otherwise create it and insert into hash table. */
579
320
    unscaled = _cairo_calloc (sizeof (cairo_ft_unscaled_font_t));
580
320
    if (unlikely (unscaled == NULL)) {
581
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
582
	goto UNWIND_FONT_MAP_LOCK;
583
    }
584

            
585
320
    status = _cairo_ft_unscaled_font_init (unscaled, from_face, filename, id, font_face);
586
320
    if (unlikely (status))
587
	goto UNWIND_UNSCALED_MALLOC;
588

            
589
320
    assert (unscaled->base.hash_entry.hash == key.base.hash_entry.hash);
590
320
    status = _cairo_hash_table_insert (font_map->hash_table,
591
				       &unscaled->base.hash_entry);
592
320
    if (unlikely (status))
593
	goto UNWIND_UNSCALED_FONT_INIT;
594

            
595
320
DONE:
596
111020
    _cairo_ft_unscaled_font_map_unlock ();
597
111020
    *out = unscaled;
598
111020
    return CAIRO_STATUS_SUCCESS;
599

            
600
UNWIND_UNSCALED_FONT_INIT:
601
    _cairo_ft_unscaled_font_fini (unscaled);
602
UNWIND_UNSCALED_MALLOC:
603
    free (unscaled);
604
UNWIND_FONT_MAP_LOCK:
605
    _cairo_ft_unscaled_font_map_unlock ();
606
    return status;
607
}
608

            
609

            
610
#if CAIRO_HAS_FC_FONT
611
static cairo_status_t
612
560
_cairo_ft_unscaled_font_create_for_pattern (FcPattern *pattern,
613
					    cairo_ft_unscaled_font_t **out)
614
{
615
560
    FT_Face font_face = NULL;
616
560
    char *filename = NULL;
617
560
    int id = 0;
618
    FcResult ret;
619

            
620
560
    ret = FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &font_face);
621
560
    if (ret == FcResultMatch)
622
	goto DONE;
623
560
    if (ret == FcResultOutOfMemory)
624
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
625

            
626
560
    ret = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **) &filename);
627
560
    if (ret == FcResultOutOfMemory)
628
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
629
560
    if (ret == FcResultMatch) {
630
290
	if (access (filename, R_OK) == 0) {
631
	    /* If FC_INDEX is not set, we just use 0 */
632
290
	    ret = FcPatternGetInteger (pattern, FC_INDEX, 0, &id);
633
290
	    if (ret == FcResultOutOfMemory)
634
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
635

            
636
290
	    goto DONE;
637
	} else
638
	    return _cairo_error (CAIRO_STATUS_FILE_NOT_FOUND);
639
    }
640

            
641
    /* The pattern contains neither a face nor a filename, resolve it later. */
642
270
    *out = NULL;
643
270
    return CAIRO_STATUS_SUCCESS;
644

            
645
290
DONE:
646
290
    return _cairo_ft_unscaled_font_create_internal (font_face != NULL,
647
						    filename, id, font_face,
648
						    out);
649
}
650
#endif
651

            
652
static cairo_status_t
653
110730
_cairo_ft_unscaled_font_create_from_face (FT_Face face,
654
					  cairo_ft_unscaled_font_t **out)
655
{
656
110730
    return _cairo_ft_unscaled_font_create_internal (TRUE, NULL, face->face_index, face, out);
657
}
658

            
659
static cairo_bool_t
660
6
_cairo_ft_unscaled_font_destroy (void *abstract_font)
661
{
662
6
    cairo_ft_unscaled_font_t *unscaled  = abstract_font;
663
    cairo_ft_unscaled_font_map_t *font_map;
664

            
665
6
    font_map = _cairo_ft_unscaled_font_map_lock ();
666
    /* All created objects must have been mapped in the font map. */
667
6
    assert (font_map != NULL);
668

            
669
6
    if (! _cairo_reference_count_dec_and_test (&unscaled->base.ref_count)) {
670
	/* somebody recreated the font whilst we waited for the lock */
671
	_cairo_ft_unscaled_font_map_unlock ();
672
	return FALSE;
673
    }
674

            
675
6
    _cairo_hash_table_remove (font_map->hash_table,
676
			      &unscaled->base.hash_entry);
677

            
678
6
    if (unscaled->from_face) {
679
	/* See comments in _ft_font_face_destroy about the "zombie" state
680
	 * for a _ft_font_face.
681
	 */
682
	if (unscaled->faces && unscaled->faces->unscaled == NULL) {
683
	    assert (unscaled->faces->next == NULL);
684
	    cairo_font_face_destroy (&unscaled->faces->base);
685
	}
686
    } else {
687
6
	_font_map_release_face_lock_held (font_map, unscaled);
688
    }
689
6
    unscaled->face = NULL;
690

            
691
6
    _cairo_ft_unscaled_font_map_unlock ();
692

            
693
6
    _cairo_ft_unscaled_font_fini (unscaled);
694
6
    return TRUE;
695
}
696

            
697
static cairo_bool_t
698
_has_unlocked_face (const void *entry)
699
{
700
    const cairo_ft_unscaled_font_t *unscaled = entry;
701

            
702
    return (!unscaled->from_face && unscaled->lock_count == 0 && unscaled->face);
703
}
704

            
705
/* Ensures that an unscaled font has a face object. If we exceed
706
 * MAX_OPEN_FACES, try to close some.
707
 *
708
 * This differs from _cairo_ft_scaled_font_lock_face in that it doesn't
709
 * set the scale on the face, but just returns it at the last scale.
710
 */
711
static cairo_warn FT_Face
712
340092
_cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
713
{
714
    cairo_ft_unscaled_font_map_t *font_map;
715
340092
    FT_Face face = NULL;
716
    FT_Error error;
717

            
718
340092
    CAIRO_MUTEX_LOCK (unscaled->mutex);
719
340092
    unscaled->lock_count++;
720

            
721
340092
    if (unscaled->face)
722
339802
	return unscaled->face;
723

            
724
    /* If this unscaled font was created from an FT_Face then we just
725
     * returned it above. */
726
290
    assert (!unscaled->from_face);
727

            
728
290
    font_map = _cairo_ft_unscaled_font_map_lock ();
729
    {
730
290
	assert (font_map != NULL);
731

            
732
290
	while (font_map->num_open_faces >= MAX_OPEN_FACES)
733
	{
734
	    cairo_ft_unscaled_font_t *entry;
735

            
736
	    entry = _cairo_hash_table_random_entry (font_map->hash_table,
737
						    _has_unlocked_face);
738
	    if (entry == NULL)
739
		break;
740

            
741
	    _font_map_release_face_lock_held (font_map, entry);
742
	}
743
    }
744
290
    _cairo_ft_unscaled_font_map_unlock ();
745

            
746
290
    error = FT_New_Face (font_map->ft_library,
747
290
			 unscaled->filename,
748
290
			 unscaled->id,
749
			 &face);
750
290
    if (error)
751
    {
752
	unscaled->lock_count--;
753
	CAIRO_MUTEX_UNLOCK (unscaled->mutex);
754
	_cairo_error_throw (_cairo_ft_to_cairo_error (error));
755
	return NULL;
756
    }
757

            
758
290
    unscaled->face = face;
759

            
760
290
    unscaled->have_color = FT_HAS_COLOR (face) != 0;
761
290
    unscaled->have_color_set = TRUE;
762

            
763
290
    font_map->num_open_faces++;
764

            
765
290
    return face;
766
}
767

            
768

            
769
/* Unlock unscaled font locked with _cairo_ft_unscaled_font_lock_face
770
 */
771
static void
772
340091
_cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled)
773
{
774
340091
    assert (unscaled->lock_count > 0);
775

            
776
340091
    unscaled->lock_count--;
777

            
778
340091
    CAIRO_MUTEX_UNLOCK (unscaled->mutex);
779
340091
}
780

            
781

            
782
static cairo_status_t
783
4594
_compute_transform (cairo_ft_font_transform_t *sf,
784
		    cairo_matrix_t      *scale,
785
		    cairo_ft_unscaled_font_t *unscaled)
786
{
787
    cairo_status_t status;
788
    double x_scale, y_scale;
789
4594
    cairo_matrix_t normalized = *scale;
790

            
791
    /* The font matrix has x and y "scale" components which we extract and
792
     * use as character scale values. These influence the way freetype
793
     * chooses hints, as well as selecting different bitmaps in
794
     * hand-rendered fonts. We also copy the normalized matrix to
795
     * freetype's transformation.
796
     */
797

            
798
4594
    status = _cairo_matrix_compute_basis_scale_factors (scale,
799
						  &x_scale, &y_scale,
800
						  1);
801
4594
    if (unlikely (status))
802
	return status;
803

            
804
    /* FreeType docs say this about x_scale and y_scale:
805
     * "A character width or height smaller than 1pt is set to 1pt;"
806
     * So, we cap them from below at 1.0 and let the FT transform
807
     * take care of sub-1.0 scaling. */
808
4594
    if (x_scale < 1.0)
809
117
      x_scale = 1.0;
810
4594
    if (y_scale < 1.0)
811
117
      y_scale = 1.0;
812

            
813
4594
    if (unscaled && (unscaled->face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) {
814
207
	double min_distance = DBL_MAX;
815
207
	cairo_bool_t magnify = TRUE;
816
	int i;
817
207
	double best_x_size = 0;
818
207
	double best_y_size = 0;
819

            
820
414
	for (i = 0; i < unscaled->face->num_fixed_sizes; i++) {
821
207
	    double x_size = unscaled->face->available_sizes[i].x_ppem / 64.;
822
207
	    double y_size = unscaled->face->available_sizes[i].y_ppem / 64.;
823
207
	    double distance = y_size - y_scale;
824

            
825
	    /*
826
	     * distance is positive if current strike is larger than desired
827
	     * size, and negative if smaller.
828
	     *
829
	     * We like to prefer down-scaling to upscaling.
830
	     */
831

            
832
207
	    if ((magnify && distance >= 0) || fabs (distance) <= min_distance) {
833
207
		magnify = distance < 0;
834
207
		min_distance = fabs (distance);
835
207
		best_x_size = x_size;
836
207
		best_y_size = y_size;
837
	    }
838
	}
839

            
840
207
	x_scale = best_x_size;
841
207
	y_scale = best_y_size;
842
    }
843

            
844
4594
    sf->x_scale = x_scale;
845
4594
    sf->y_scale = y_scale;
846

            
847
4594
    cairo_matrix_scale (&normalized, 1.0 / x_scale, 1.0 / y_scale);
848

            
849
4594
    _cairo_matrix_get_affine (&normalized,
850
			      &sf->shape[0][0], &sf->shape[0][1],
851
			      &sf->shape[1][0], &sf->shape[1][1],
852
			      NULL, NULL);
853

            
854
4594
    return CAIRO_STATUS_SUCCESS;
855
}
856

            
857
/* Temporarily scales an unscaled font to the give scale. We catch
858
 * scaling to the same size, since changing a FT_Face is expensive.
859
 */
860
static cairo_status_t
861
11097
_cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
862
				   cairo_matrix_t	      *scale)
863
{
864
    cairo_status_t status;
865
    cairo_ft_font_transform_t sf;
866
    FT_Matrix mat;
867
    FT_Error error;
868

            
869
11097
    assert (unscaled->face != NULL);
870

            
871
11097
    if (unscaled->have_scale &&
872
10783
	scale->xx == unscaled->current_scale.xx &&
873
6772
	scale->yx == unscaled->current_scale.yx &&
874
6772
	scale->xy == unscaled->current_scale.xy &&
875
6772
	scale->yy == unscaled->current_scale.yy)
876
6772
	return CAIRO_STATUS_SUCCESS;
877

            
878
4325
    unscaled->have_scale = TRUE;
879
4325
    unscaled->current_scale = *scale;
880

            
881
4325
    status = _compute_transform (&sf, scale, unscaled);
882
4325
    if (unlikely (status))
883
	return status;
884

            
885
4325
    unscaled->x_scale = sf.x_scale;
886
4325
    unscaled->y_scale = sf.y_scale;
887

            
888
4325
    mat.xx = DOUBLE_TO_16_16(sf.shape[0][0]);
889
4325
    mat.yx = - DOUBLE_TO_16_16(sf.shape[0][1]);
890
4325
    mat.xy = - DOUBLE_TO_16_16(sf.shape[1][0]);
891
4325
    mat.yy = DOUBLE_TO_16_16(sf.shape[1][1]);
892

            
893
11898
    unscaled->have_shape = (mat.xx != 0x10000 ||
894
3248
			    mat.yx != 0x00000 ||
895
10785
			    mat.xy != 0x00000 ||
896
3212
			    mat.yy != 0x10000);
897

            
898
4325
    unscaled->Current_Shape = mat;
899
4325
    cairo_matrix_init (&unscaled->current_shape,
900
		       sf.shape[0][0], sf.shape[0][1],
901
		       sf.shape[1][0], sf.shape[1][1],
902
		       0.0, 0.0);
903

            
904
4325
    FT_Set_Transform(unscaled->face, &mat, NULL);
905

            
906
4325
    error = FT_Set_Char_Size (unscaled->face,
907
4325
			      sf.x_scale * 64.0 + .5,
908
4325
			      sf.y_scale * 64.0 + .5,
909
			      0, 0);
910
4325
    if (error)
911
      return _cairo_error (_cairo_ft_to_cairo_error (error));
912

            
913
4325
    return CAIRO_STATUS_SUCCESS;
914
}
915

            
916
/* we sometimes need to convert the glyph bitmap in a FT_GlyphSlot
917
 * into a different format. For example, we want to convert a
918
 * FT_PIXEL_MODE_LCD or FT_PIXEL_MODE_LCD_V bitmap into a 32-bit
919
 * ARGB or ABGR bitmap.
920
 *
921
 * this function prepares a target descriptor for this operation.
922
 *
923
 * input :: target bitmap descriptor. The function will set its
924
 *          'width', 'rows' and 'pitch' fields, and only these
925
 *
926
 * slot  :: the glyph slot containing the source bitmap. this
927
 *          function assumes that slot->format == FT_GLYPH_FORMAT_BITMAP
928
 *
929
 * mode  :: the requested final rendering mode. supported values are
930
 *          MONO, NORMAL (i.e. gray), LCD and LCD_V
931
 *
932
 * the function returns the size in bytes of the corresponding buffer,
933
 * it's up to the caller to allocate the corresponding memory block
934
 * before calling _fill_xrender_bitmap
935
 *
936
 * it also returns -1 in case of error (e.g. incompatible arguments,
937
 * like trying to convert a gray bitmap into a monochrome one)
938
 */
939
static int
940
2679
_compute_xrender_bitmap_size(FT_Bitmap      *target,
941
			     FT_GlyphSlot    slot,
942
			     FT_Render_Mode  mode)
943
{
944
    FT_Bitmap *ftbit;
945
    int width, height, pitch;
946

            
947
2679
    if (slot->format != FT_GLYPH_FORMAT_BITMAP)
948
	return -1;
949

            
950
    /* compute the size of the final bitmap */
951
2679
    ftbit = &slot->bitmap;
952

            
953
2679
    width = ftbit->width;
954
2679
    height = ftbit->rows;
955
2679
    pitch = (width + 3) & ~3;
956

            
957
2679
    switch (ftbit->pixel_mode) {
958
42
    case FT_PIXEL_MODE_MONO:
959
42
	if (mode == FT_RENDER_MODE_MONO) {
960
42
	    pitch = (((width + 31) & ~31) >> 3);
961
42
	    break;
962
	}
963
	/* fall-through */
964

            
965
    case FT_PIXEL_MODE_GRAY:
966
2532
	if (mode == FT_RENDER_MODE_LCD ||
967
	    mode == FT_RENDER_MODE_LCD_V)
968
	{
969
	    /* each pixel is replicated into a 32-bit ARGB value */
970
	    pitch = width * 4;
971
	}
972
2532
	break;
973

            
974
63
    case FT_PIXEL_MODE_LCD:
975
63
	if (mode != FT_RENDER_MODE_LCD)
976
	    return -1;
977

            
978
	/* horz pixel triplets are packed into 32-bit ARGB values */
979
63
	width /= 3;
980
63
	pitch = width * 4;
981
63
	break;
982

            
983
42
    case FT_PIXEL_MODE_LCD_V:
984
42
	if (mode != FT_RENDER_MODE_LCD_V)
985
	    return -1;
986

            
987
	/* vert pixel triplets are packed into 32-bit ARGB values */
988
42
	height /= 3;
989
42
	pitch = width * 4;
990
42
	break;
991

            
992
    case FT_PIXEL_MODE_BGRA:
993
	/* each pixel is replicated into a 32-bit ARGB value */
994
	pitch = width * 4;
995
	break;
996

            
997
    default:  /* unsupported source format */
998
	return -1;
999
    }
2679
    target->width = width;
2679
    target->rows = height;
2679
    target->pitch = pitch;
2679
    target->buffer = NULL;
2679
    return pitch * height;
}
/* this functions converts the glyph bitmap found in a FT_GlyphSlot
 * into a different format (see _compute_xrender_bitmap_size)
 *
 * you should call this function after _compute_xrender_bitmap_size
 *
 * target :: target bitmap descriptor. Note that its 'buffer' pointer
 *           must point to memory allocated by the caller
 *
 * slot   :: the glyph slot containing the source bitmap
 *
 * mode   :: the requested final rendering mode
 *
 * bgr    :: boolean, set if BGR or VBGR pixel ordering is needed
 */
static void
2679
_fill_xrender_bitmap(FT_Bitmap      *target,
		     FT_GlyphSlot    slot,
		     FT_Render_Mode  mode,
		     int             bgr)
{
2679
    FT_Bitmap *ftbit = &slot->bitmap;
2679
    unsigned char *srcLine = ftbit->buffer;
2679
    unsigned char *dstLine = target->buffer;
2679
    int src_pitch = ftbit->pitch;
2679
    int width = target->width;
2679
    int height = target->rows;
2679
    int pitch = target->pitch;
    int subpixel;
    int h;
2679
    subpixel = (mode == FT_RENDER_MODE_LCD ||
		mode == FT_RENDER_MODE_LCD_V);
2679
    if (src_pitch < 0)
	srcLine -= src_pitch * (ftbit->rows - 1);
2679
    target->pixel_mode = ftbit->pixel_mode;
2679
    switch (ftbit->pixel_mode) {
42
    case FT_PIXEL_MODE_MONO:
42
	if (subpixel) {
	    /* convert mono to ARGB32 values */
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
		int x;
		for (x = 0; x < width; x++) {
		    if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
			((unsigned int *) dstLine)[x] = 0xffffffffU;
		}
	    }
	    target->pixel_mode = FT_PIXEL_MODE_LCD;
42
	} else if (mode == FT_RENDER_MODE_NORMAL) {
	    /* convert mono to 8-bit gray */
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
		int x;
		for (x = 0; x < width; x++) {
		    if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
			dstLine[x] = 0xff;
		}
	    }
	    target->pixel_mode = FT_PIXEL_MODE_GRAY;
	} else {
	    /* copy mono to mono */
42
	    int  bytes = (width + 7) >> 3;
381
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
339
		memcpy (dstLine, srcLine, bytes);
	}
42
	break;
2532
    case FT_PIXEL_MODE_GRAY:
2532
	if (subpixel) {
	    /* convert gray to ARGB32 values */
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
		int x;
		unsigned int *dst = (unsigned int *) dstLine;
		for (x = 0; x < width; x++) {
		    unsigned int pix = srcLine[x];
		    pix |= (pix << 8);
		    pix |= (pix << 16);
		    dst[x] = pix;
		}
	    }
	    target->pixel_mode = FT_PIXEL_MODE_LCD;
        } else {
            /* copy gray into gray */
71418
            for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
68886
                memcpy (dstLine, srcLine, width);
        }
2532
        break;
63
    case FT_PIXEL_MODE_LCD:
63
	if (!bgr) {
	    /* convert horizontal RGB into ARGB32 */
420
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
		int x;
378
		unsigned char *src = srcLine;
378
		unsigned int *dst = (unsigned int *) dstLine;
3102
		for (x = 0; x < width; x++, src += 3) {
		    unsigned int  pix;
2724
		    pix = ((unsigned int)src[0] << 16) |
2724
			  ((unsigned int)src[1] <<  8) |
2724
			  ((unsigned int)src[2]      ) |
2724
			  ((unsigned int)src[1] << 24) ;
2724
		    dst[x] = pix;
		}
	    }
	} else {
	    /* convert horizontal BGR into ARGB32 */
210
	    for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
		int x;
189
		unsigned char *src = srcLine;
189
		unsigned int *dst = (unsigned int *) dstLine;
1551
		for (x = 0; x < width; x++, src += 3) {
		    unsigned int  pix;
1362
		    pix = ((unsigned int)src[2] << 16) |
1362
			  ((unsigned int)src[1] <<  8) |
1362
			  ((unsigned int)src[0]      ) |
1362
			  ((unsigned int)src[1] << 24) ;
1362
		    dst[x] = pix;
		}
	    }
	}
63
	break;
42
    case FT_PIXEL_MODE_LCD_V:
	/* convert vertical RGB into ARGB32 */
42
	if (!bgr) {
228
	    for (h = height; h > 0; h--, srcLine += 3 * src_pitch, dstLine += pitch) {
		int x;
207
		unsigned char* src = srcLine;
207
		unsigned int*  dst = (unsigned int *) dstLine;
1371
		for (x = 0; x < width; x++, src += 1) {
		    unsigned int pix;
1164
		    pix = ((unsigned int)src[0]           << 16) |
1164
			  ((unsigned int)src[src_pitch]   <<  8) |
1164
			  ((unsigned int)src[src_pitch*2]      ) |
1164
			  ((unsigned int)src[src_pitch]   << 24) ;
1164
		    dst[x] = pix;
		}
	    }
	} else {
228
	    for (h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch) {
		int x;
207
		unsigned char *src = srcLine;
207
		unsigned int *dst = (unsigned int *) dstLine;
1371
		for (x = 0; x < width; x++, src += 1) {
		    unsigned int  pix;
1164
		    pix = ((unsigned int)src[src_pitch * 2] << 16) |
1164
			  ((unsigned int)src[src_pitch]     <<  8) |
1164
			  ((unsigned int)src[0]                  ) |
1164
			  ((unsigned int)src[src_pitch]     << 24) ;
1164
		    dst[x] = pix;
		}
	    }
	}
42
	break;
    case FT_PIXEL_MODE_BGRA:
	for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
	    memcpy (dstLine, srcLine, (size_t)width * 4);
	break;
    default:
	assert (0);
    }
2679
}
/* Fills in val->image with an image surface created from @bitmap
 */
static cairo_status_t
2892
_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)
{
    unsigned int width, height;
    unsigned char *data;
2892
    int format = CAIRO_FORMAT_A8;
    int stride;
    cairo_image_surface_t *image;
2892
    cairo_bool_t component_alpha = FALSE;
2892
    width = bitmap->width;
2892
    height = bitmap->rows;
2892
    if (width == 0 || height == 0) {
9
	*surface = (cairo_image_surface_t *)
9
	    cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
9
	return (*surface)->base.status;
    }
2883
    switch (bitmap->pixel_mode) {
240
    case FT_PIXEL_MODE_MONO:
240
	stride = (((width + 31) & ~31) >> 3);
240
	if (own_buffer) {
42
	    data = bitmap->buffer;
42
	    assert (stride == bitmap->pitch);
	} else {
198
	    data = _cairo_malloc_ab (height, stride);
198
	    if (!data)
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
198
	    if (stride == bitmap->pitch) {
		memcpy (data, bitmap->buffer, (size_t)stride * height);
	    } else {
		int i;
		unsigned char *source, *dest;
198
		source = bitmap->buffer;
198
		dest = data;
2772
		for (i = height; i; i--) {
2574
		    memcpy (dest, source, bitmap->pitch);
2574
		    memset (dest + bitmap->pitch, '\0', stride - bitmap->pitch);
2574
		    source += bitmap->pitch;
2574
		    dest += stride;
		}
	    }
	}
#ifndef WORDS_BIGENDIAN
	{
240
	    uint8_t *d = data;
240
	    int count = stride * height;
11892
	    while (count--) {
11652
		*d = CAIRO_BITSWAP8 (*d);
11652
		d++;
	    }
	}
#endif
240
	format = CAIRO_FORMAT_A1;
240
	break;
2640
    case FT_PIXEL_MODE_LCD:
    case FT_PIXEL_MODE_LCD_V:
    case FT_PIXEL_MODE_GRAY:
2640
	if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL ||
105
	    bitmap->pixel_mode == FT_PIXEL_MODE_GRAY)
	{
2535
	    stride = bitmap->pitch;
	    /* We don't support stride not multiple of 4. */
2535
	    if (stride & 3)
	    {
		assert (!own_buffer);
		goto convert;
	    }
2535
	    if (own_buffer) {
2532
		data = bitmap->buffer;
	    } else {
3
		data = _cairo_malloc_ab (height, stride);
3
		if (!data)
		    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3
		memcpy (data, bitmap->buffer, (size_t)stride * height);
	    }
2535
	    format = CAIRO_FORMAT_A8;
	} else {
105
	    data = bitmap->buffer;
105
	    stride = bitmap->pitch;
105
	    format = CAIRO_FORMAT_ARGB32;
105
	    component_alpha = TRUE;
	}
2640
	break;
3
    case FT_PIXEL_MODE_BGRA:
3
	stride = width * 4;
3
	if (own_buffer) {
	    data = bitmap->buffer;
	} else {
3
	    data = _cairo_malloc_ab (height, stride);
3
	    if (!data)
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3
	    memcpy (data, bitmap->buffer, (size_t)stride * height);
	}
3
	if (!_cairo_is_little_endian ())
	{
	    /* Byteswap. */
	    unsigned int i, count = height * width;
	    uint32_t *p = (uint32_t *) data;
	    for (i = 0; i < count; i++)
		p[i] = bswap_32 (p[i]);
	}
3
	format = CAIRO_FORMAT_ARGB32;
3
	break;
    case FT_PIXEL_MODE_GRAY2:
    case FT_PIXEL_MODE_GRAY4:
    convert:
	if (!own_buffer && library)
	{
	    /* This is pretty much the only case that we can get in here. */
	    /* Convert to 8bit grayscale. */
	    FT_Bitmap  tmp;
	    FT_Int     align;
	    FT_Error   error;
	    format = CAIRO_FORMAT_A8;
	    align = cairo_format_stride_for_width (format, bitmap->width);
	    FT_Bitmap_New( &tmp );
	    error = FT_Bitmap_Convert( library, bitmap, &tmp, align );
	    if (error)
		return _cairo_error (_cairo_ft_to_cairo_error (error));
	    FT_Bitmap_Done( library, bitmap );
	    *bitmap = tmp;
	    stride = bitmap->pitch;
	    data = _cairo_malloc_ab (height, stride);
	    if (!data)
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    if (bitmap->num_grays != 256)
	    {
	      unsigned int x, y;
	      unsigned int mul = 255 / (bitmap->num_grays - 1);
	      FT_Byte *p = bitmap->buffer;
	      for (y = 0; y < height; y++) {
	        for (x = 0; x < width; x++)
		  p[x] *= mul;
		p += bitmap->pitch;
	      }
	    }
	    memcpy (data, bitmap->buffer, (size_t)stride * height);
	    break;
	}
	/* fall through */
	/* These could be triggered by very rare types of TrueType fonts */
    default:
	if (own_buffer)
	    free (bitmap->buffer);
	return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
    }
    /* XXX */
2883
    *surface = image = (cairo_image_surface_t *)
2883
	cairo_image_surface_create_for_data (data,
					     format,
					     width, height, stride);
2883
    if (image->base.status) {
	free (data);
	return (*surface)->base.status;
    }
2883
    if (component_alpha)
105
	pixman_image_set_component_alpha (image->pixman_image, TRUE);
2883
    _cairo_image_surface_assume_ownership_of_data (image);
    _cairo_debug_check_image_surface_is_defined (&image->base);
2883
    return CAIRO_STATUS_SUCCESS;
}
/* Converts an outline FT_GlyphSlot into an image
 *
 * This could go through _render_glyph_bitmap as well, letting
 * FreeType convert the outline to a bitmap, but doing it ourselves
 * has two minor advantages: first, we save a copy of the bitmap
 * buffer: we can directly use the buffer that FreeType renders
 * into.
 *
 * Second, it may help when we add support for subpixel
 * rendering: the Xft code does it this way. (Keith thinks that
 * it may also be possible to get the subpixel rendering with
 * FT_Render_Glyph: something worth looking into in more detail
 * when we add subpixel support. If so, we may want to eliminate
 * this version of the code path entirely.
 */
static cairo_status_t
2751
_render_glyph_outline (FT_Face                    face,
		       cairo_font_options_t	 *font_options,
		       cairo_image_surface_t	**surface)
{
2751
    cairo_subpixel_order_t rgba = CAIRO_SUBPIXEL_ORDER_DEFAULT;
2751
    int lcd_filter = FT_LCD_FILTER_DEFAULT;
2751
    FT_GlyphSlot glyphslot = face->glyph;
2751
    FT_Outline *outline = &glyphslot->outline;
    FT_Bitmap bitmap;
    FT_BBox cbox;
    unsigned int width, height;
    cairo_status_t status;
    FT_Error error;
2751
    FT_Library library = glyphslot->library;
2751
    FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
2751
    switch (font_options->antialias) {
42
    case CAIRO_ANTIALIAS_NONE:
42
	render_mode = FT_RENDER_MODE_MONO;
42
	break;
105
    case CAIRO_ANTIALIAS_SUBPIXEL:
    case CAIRO_ANTIALIAS_BEST:
105
	switch (font_options->subpixel_order) {
63
	    case CAIRO_SUBPIXEL_ORDER_DEFAULT:
	    case CAIRO_SUBPIXEL_ORDER_RGB:
	    case CAIRO_SUBPIXEL_ORDER_BGR:
63
		render_mode = FT_RENDER_MODE_LCD;
63
		break;
42
	    case CAIRO_SUBPIXEL_ORDER_VRGB:
	    case CAIRO_SUBPIXEL_ORDER_VBGR:
42
		render_mode = FT_RENDER_MODE_LCD_V;
42
		break;
	}
105
	switch (font_options->lcd_filter) {
	case CAIRO_LCD_FILTER_NONE:
	    lcd_filter = FT_LCD_FILTER_NONE;
	    break;
	case CAIRO_LCD_FILTER_INTRA_PIXEL:
	    lcd_filter = FT_LCD_FILTER_LEGACY;
	    break;
	case CAIRO_LCD_FILTER_FIR3:
	    lcd_filter = FT_LCD_FILTER_LIGHT;
	    break;
105
	case CAIRO_LCD_FILTER_DEFAULT:
	case CAIRO_LCD_FILTER_FIR5:
105
	    lcd_filter = FT_LCD_FILTER_DEFAULT;
105
	    break;
	}
105
	break;
2604
    case CAIRO_ANTIALIAS_DEFAULT:
    case CAIRO_ANTIALIAS_GRAY:
    case CAIRO_ANTIALIAS_GOOD:
    case CAIRO_ANTIALIAS_FAST:
2604
	render_mode = FT_RENDER_MODE_NORMAL;
    }
2751
    FT_Outline_Get_CBox (outline, &cbox);
2751
    cbox.xMin &= -64;
2751
    cbox.yMin &= -64;
2751
    cbox.xMax = (cbox.xMax + 63) & -64;
2751
    cbox.yMax = (cbox.yMax + 63) & -64;
2751
    width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
2751
    height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
2751
    if (width * height == 0) {
	cairo_format_t format;
	/* Looks like fb handles zero-sized images just fine */
72
	switch (render_mode) {
	case FT_RENDER_MODE_MONO:
	    format = CAIRO_FORMAT_A1;
	    break;
	case FT_RENDER_MODE_LCD:
	case FT_RENDER_MODE_LCD_V:
	    format= CAIRO_FORMAT_ARGB32;
	    break;
72
	case FT_RENDER_MODE_LIGHT:
	case FT_RENDER_MODE_NORMAL:
	case FT_RENDER_MODE_MAX:
#if HAVE_FT_RENDER_MODE_SDF
	case FT_RENDER_MODE_SDF:
#endif
	default:
72
	    format = CAIRO_FORMAT_A8;
72
	    break;
	}
72
	(*surface) = (cairo_image_surface_t *)
72
	    cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
72
	pixman_image_set_component_alpha ((*surface)->pixman_image, TRUE);
72
	if ((*surface)->base.status)
	    return (*surface)->base.status;
    } else {
	int bitmap_size;
2679
	switch (render_mode) {
63
	case FT_RENDER_MODE_LCD:
63
	    if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
21
		rgba = CAIRO_SUBPIXEL_ORDER_BGR;
	    else
42
		rgba = CAIRO_SUBPIXEL_ORDER_RGB;
63
	    break;
42
	case FT_RENDER_MODE_LCD_V:
42
	    if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
21
		rgba = CAIRO_SUBPIXEL_ORDER_VBGR;
	    else
21
		rgba = CAIRO_SUBPIXEL_ORDER_VRGB;
42
	    break;
2574
	case FT_RENDER_MODE_MONO:
	case FT_RENDER_MODE_LIGHT:
	case FT_RENDER_MODE_NORMAL:
	case FT_RENDER_MODE_MAX:
#if HAVE_FT_RENDER_MODE_SDF
	case FT_RENDER_MODE_SDF:
#endif
	default:
2574
	    break;
	}
2679
	FT_Library_SetLcdFilter (library, lcd_filter);
2679
	error = FT_Render_Glyph (face->glyph, render_mode);
2679
	FT_Library_SetLcdFilter (library, FT_LCD_FILTER_NONE);
2679
	if (error)
	    return _cairo_error (_cairo_ft_to_cairo_error (error));
2679
	bitmap_size = _compute_xrender_bitmap_size (&bitmap,
						    face->glyph,
						    render_mode);
2679
	if (bitmap_size < 0)
	    return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
2679
	bitmap.buffer = _cairo_calloc (bitmap_size);
2679
	if (bitmap.buffer == NULL)
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2679
	_fill_xrender_bitmap (&bitmap, face->glyph, render_mode,
			      (rgba == CAIRO_SUBPIXEL_ORDER_BGR || rgba == CAIRO_SUBPIXEL_ORDER_VBGR));
	/* Note:
	 * _get_bitmap_surface will free bitmap.buffer if there is an error
	 */
2679
	status = _get_bitmap_surface (&bitmap, NULL, TRUE, font_options, surface);
2679
	if (unlikely (status))
	    return status;
	/* Note: the font's coordinate system is upside down from ours, so the
	 * Y coordinate of the control box needs to be negated.  Moreover, device
	 * offsets are position of glyph origin relative to top left while xMin
	 * and yMax are offsets of top left relative to origin.  Another negation.
	 */
2679
	cairo_surface_set_device_offset (&(*surface)->base,
2679
					 (double)-glyphslot->bitmap_left,
2679
					 (double)+glyphslot->bitmap_top);
    }
2751
    return CAIRO_STATUS_SUCCESS;
}
/* Converts a bitmap (or other) FT_GlyphSlot into an image */
static cairo_status_t
213
_render_glyph_bitmap (FT_Face		      face,
		      cairo_font_options_t   *font_options,
		      cairo_image_surface_t **surface)
{
213
    FT_GlyphSlot glyphslot = face->glyph;
    cairo_status_t status;
    FT_Error error;
    /* According to the FreeType docs, glyphslot->format could be
     * something other than FT_GLYPH_FORMAT_OUTLINE or
     * FT_GLYPH_FORMAT_BITMAP. Calling FT_Render_Glyph gives FreeType
     * the opportunity to convert such to
     * bitmap. FT_GLYPH_FORMAT_COMPOSITE will not be encountered since
     * we avoid the FT_LOAD_NO_RECURSE flag.
     */
213
    error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL);
    /* XXX ignoring all other errors for now.  They are not fatal, typically
     * just a glyph-not-found. */
213
    if (error == FT_Err_Out_Of_Memory)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
213
    status = _get_bitmap_surface (&glyphslot->bitmap,
				  glyphslot->library,
				  FALSE, font_options,
				  surface);
213
    if (unlikely (status))
	return status;
    /*
     * Note: the font's coordinate system is upside down from ours, so the
     * Y coordinate of the control box needs to be negated.  Moreover, device
     * offsets are position of glyph origin relative to top left while
     * bitmap_left and bitmap_top are offsets of top left relative to origin.
     * Another negation.
     */
213
    cairo_surface_set_device_offset (&(*surface)->base,
213
				     -glyphslot->bitmap_left,
213
				     +glyphslot->bitmap_top);
213
    return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
105
_transform_glyph_bitmap (cairo_matrix_t         * shape,
			 cairo_image_surface_t ** surface)
{
    cairo_matrix_t original_to_transformed;
    cairo_matrix_t transformed_to_original;
    cairo_image_surface_t *old_image;
    cairo_surface_t *image;
    double x[4], y[4];
    double origin_x, origin_y;
    int orig_width, orig_height;
    int i;
    int x_min, y_min, x_max, y_max;
    int width, height;
    cairo_status_t status;
    cairo_surface_pattern_t pattern;
    /* We want to compute a transform that takes the origin
     * (device_x_offset, device_y_offset) to 0,0, then applies
     * the "shape" portion of the font transform
     */
105
    original_to_transformed = *shape;
105
    cairo_surface_get_device_offset (&(*surface)->base, &origin_x, &origin_y);
105
    orig_width = (*surface)->width;
105
    orig_height = (*surface)->height;
105
    cairo_matrix_translate (&original_to_transformed,
			    -origin_x, -origin_y);
    /* Find the bounding box of the original bitmap under that
     * transform
     */
105
    x[0] = 0;          y[0] = 0;
105
    x[1] = orig_width; y[1] = 0;
105
    x[2] = orig_width; y[2] = orig_height;
105
    x[3] = 0;          y[3] = orig_height;
525
    for (i = 0; i < 4; i++)
420
      cairo_matrix_transform_point (&original_to_transformed,
				    &x[i], &y[i]);
105
    x_min = floor (x[0]);   y_min = floor (y[0]);
105
    x_max =  ceil (x[0]);   y_max =  ceil (y[0]);
420
    for (i = 1; i < 4; i++) {
315
	if (x[i] < x_min)
99
	    x_min = floor (x[i]);
216
	else if (x[i] > x_max)
6
	    x_max = ceil (x[i]);
315
	if (y[i] < y_min)
99
	    y_min = floor (y[i]);
216
	else if (y[i] > y_max)
6
	    y_max = ceil (y[i]);
    }
    /* Adjust the transform so that the bounding box starts at 0,0 ...
     * this gives our final transform from original bitmap to transformed
     * bitmap.
     */
105
    original_to_transformed.x0 -= x_min;
105
    original_to_transformed.y0 -= y_min;
    /* Create the transformed bitmap */
105
    width  = x_max - x_min;
105
    height = y_max - y_min;
105
    transformed_to_original = original_to_transformed;
105
    status = cairo_matrix_invert (&transformed_to_original);
105
    if (unlikely (status))
	return status;
108
    if ((*surface)->format == CAIRO_FORMAT_ARGB32 &&
3
        !pixman_image_get_component_alpha ((*surface)->pixman_image))
3
      image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    else
102
      image = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
105
    if (unlikely (image->status))
	return image->status;
    /* Draw the original bitmap transformed into the new bitmap
     */
105
    _cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
105
    cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
105
    status = _cairo_surface_paint (image,
				   CAIRO_OPERATOR_SOURCE,
				   &pattern.base,
				   NULL);
105
    _cairo_pattern_fini (&pattern.base);
105
    if (unlikely (status)) {
	cairo_surface_destroy (image);
	return status;
    }
    /* Now update the cache entry for the new bitmap, recomputing
     * the origin based on the final transform.
     */
105
    cairo_matrix_transform_point (&original_to_transformed,
				  &origin_x, &origin_y);
105
    old_image = (*surface);
105
    (*surface) = (cairo_image_surface_t *)image;
    /* Note: we converted subpixel-rendered RGBA images to grayscale,
     * so, no need to copy component alpha to new image. */
105
    cairo_surface_destroy (&old_image->base);
105
    cairo_surface_set_device_offset (&(*surface)->base,
105
				     _cairo_lround (origin_x),
105
				     _cairo_lround (origin_y));
105
    return CAIRO_STATUS_SUCCESS;
}
static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend = {
    _cairo_ft_unscaled_font_destroy,
#if 0
    _cairo_ft_unscaled_font_create_glyph
#endif
};
/* #cairo_ft_scaled_font_t */
typedef struct _cairo_ft_scaled_font {
    cairo_scaled_font_t base;
    cairo_ft_unscaled_font_t *unscaled;
    cairo_ft_options_t ft_options;
} cairo_ft_scaled_font_t;
static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend;
#if CAIRO_HAS_FC_FONT
/* The load flags passed to FT_Load_Glyph control aspects like hinting and
 * antialiasing. Here we compute them from the fields of a FcPattern.
 */
static void
2117
_get_pattern_ft_options (FcPattern *pattern, cairo_ft_options_t *ret)
{
    FcBool antialias, vertical_layout, hinting, autohint, bitmap, embolden;
    cairo_ft_options_t ft_options;
    int rgba;
    int hintstyle;
    char *variations;
2117
    _cairo_font_options_init_default (&ft_options.base);
2117
    ft_options.load_flags = FT_LOAD_DEFAULT;
2117
    ft_options.synth_flags = 0;
    /* Check whether to force use of embedded bitmaps */
2117
    if (FcPatternGetBool (pattern,
			  FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
1830
	bitmap = FcFalse;
    /* disable antialiasing if requested */
2117
    if (FcPatternGetBool (pattern,
			  FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
1827
	antialias = FcTrue;
2117
    if (antialias) {
	cairo_subpixel_order_t subpixel_order;
	int lcd_filter;
	/* disable hinting if requested */
2108
	if (FcPatternGetBool (pattern,
			      FC_HINTING, 0, &hinting) != FcResultMatch)
1827
	    hinting = FcTrue;
2108
	if (FcPatternGetInteger (pattern,
				 FC_RGBA, 0, &rgba) != FcResultMatch)
1928
	    rgba = FC_RGBA_UNKNOWN;
2108
	switch (rgba) {
6
	case FC_RGBA_RGB:
6
	    subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
6
	    break;
3
	case FC_RGBA_BGR:
3
	    subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
3
	    break;
3
	case FC_RGBA_VRGB:
3
	    subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
3
	    break;
3
	case FC_RGBA_VBGR:
3
	    subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
3
	    break;
2093
	case FC_RGBA_UNKNOWN:
	case FC_RGBA_NONE:
	default:
2093
	    subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
2093
	    break;
	}
2108
	if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT) {
15
	    ft_options.base.subpixel_order = subpixel_order;
15
	    ft_options.base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
	}
2108
	if (FcPatternGetInteger (pattern,
				 FC_LCD_FILTER, 0, &lcd_filter) == FcResultMatch)
	{
281
	    switch (lcd_filter) {
	    case FC_LCD_NONE:
		ft_options.base.lcd_filter = CAIRO_LCD_FILTER_NONE;
		break;
281
	    case FC_LCD_DEFAULT:
281
		ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR5;
281
		break;
	    case FC_LCD_LIGHT:
		ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR3;
		break;
	    case FC_LCD_LEGACY:
		ft_options.base.lcd_filter = CAIRO_LCD_FILTER_INTRA_PIXEL;
		break;
	    }
	}
2108
	if (FcPatternGetInteger (pattern,
				 FC_HINT_STYLE, 0, &hintstyle) != FcResultMatch)
1827
	    hintstyle = FC_HINT_FULL;
2108
	if (!hinting)
186
	    hintstyle = FC_HINT_NONE;
2108
	switch (hintstyle) {
186
	case FC_HINT_NONE:
186
	    ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
186
	    break;
95
	case FC_HINT_SLIGHT:
95
	    ft_options.base.hint_style = CAIRO_HINT_STYLE_SLIGHT;
95
	    break;
	case FC_HINT_MEDIUM:
	default:
	    ft_options.base.hint_style = CAIRO_HINT_STYLE_MEDIUM;
	    break;
1827
	case FC_HINT_FULL:
1827
	    ft_options.base.hint_style = CAIRO_HINT_STYLE_FULL;
1827
	    break;
	}
	/* Force embedded bitmaps off if no hinting requested */
2108
	if (ft_options.base.hint_style == CAIRO_HINT_STYLE_NONE)
186
	  bitmap = FcFalse;
2108
	if (!bitmap)
2013
	    ft_options.load_flags |= FT_LOAD_NO_BITMAP;
    } else {
9
	ft_options.base.antialias = CAIRO_ANTIALIAS_NONE;
    }
    /* force autohinting if requested */
2117
    if (FcPatternGetBool (pattern,
			  FC_AUTOHINT, 0, &autohint) != FcResultMatch)
1830
	autohint = FcFalse;
2117
    if (autohint)
	ft_options.load_flags |= FT_LOAD_FORCE_AUTOHINT;
2117
    if (FcPatternGetBool (pattern,
			  FC_VERTICAL_LAYOUT, 0, &vertical_layout) != FcResultMatch)
1830
	vertical_layout = FcFalse;
2117
    if (vertical_layout)
6
	ft_options.load_flags |= FT_LOAD_VERTICAL_LAYOUT;
2117
    if (FcPatternGetBool (pattern,
			  FC_EMBOLDEN, 0, &embolden) != FcResultMatch)
2117
	embolden = FcFalse;
2117
    if (embolden)
	ft_options.synth_flags |= CAIRO_FT_SYNTHESIZE_BOLD;
2117
    if (FcPatternGetString (pattern, FC_FONT_VARIATIONS, 0, (FcChar8 **) &variations) == FcResultMatch) {
3
      ft_options.base.variations = strdup (variations);
    }
2117
    *ret = ft_options;
2117
}
#endif
static void
962
_cairo_ft_options_merge (cairo_ft_options_t *options,
			 cairo_ft_options_t *other)
{
962
    int load_flags = other->load_flags;
962
    int load_target = FT_LOAD_TARGET_NORMAL;
    /* clear load target mode */
962
    load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(other->load_flags)));
962
    if (load_flags & FT_LOAD_NO_HINTING)
	other->base.hint_style = CAIRO_HINT_STYLE_NONE;
962
    if (other->base.antialias == CAIRO_ANTIALIAS_NONE ||
917
	options->base.antialias == CAIRO_ANTIALIAS_NONE) {
48
	options->base.antialias = CAIRO_ANTIALIAS_NONE;
48
	options->base.subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
    }
962
    if (other->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL &&
15
	options->base.antialias == CAIRO_ANTIALIAS_DEFAULT) {
	options->base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
	options->base.subpixel_order = other->base.subpixel_order;
    }
962
    if (options->base.hint_style == CAIRO_HINT_STYLE_DEFAULT)
485
	options->base.hint_style = other->base.hint_style;
962
    if (other->base.hint_style == CAIRO_HINT_STYLE_NONE)
402
	options->base.hint_style = CAIRO_HINT_STYLE_NONE;
962
    if (options->base.lcd_filter == CAIRO_LCD_FILTER_DEFAULT)
962
	options->base.lcd_filter = other->base.lcd_filter;
962
    if (other->base.lcd_filter == CAIRO_LCD_FILTER_NONE)
	options->base.lcd_filter = CAIRO_LCD_FILTER_NONE;
962
    if (options->base.antialias == CAIRO_ANTIALIAS_NONE) {
48
	if (options->base.hint_style == CAIRO_HINT_STYLE_NONE)
24
	    load_flags |= FT_LOAD_NO_HINTING;
	else
24
	    load_target = FT_LOAD_TARGET_MONO;
48
	load_flags |= FT_LOAD_MONOCHROME;
    } else {
914
	switch (options->base.hint_style) {
441
	case CAIRO_HINT_STYLE_NONE:
441
	    load_flags |= FT_LOAD_NO_HINTING;
441
	    break;
464
	case CAIRO_HINT_STYLE_SLIGHT:
464
	    load_target = FT_LOAD_TARGET_LIGHT;
464
	    break;
	case CAIRO_HINT_STYLE_MEDIUM:
	    break;
9
	case CAIRO_HINT_STYLE_FULL:
	case CAIRO_HINT_STYLE_DEFAULT:
9
	    if (options->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
		switch (options->base.subpixel_order) {
		case CAIRO_SUBPIXEL_ORDER_DEFAULT:
		case CAIRO_SUBPIXEL_ORDER_RGB:
		case CAIRO_SUBPIXEL_ORDER_BGR:
		    load_target = FT_LOAD_TARGET_LCD;
		    break;
		case CAIRO_SUBPIXEL_ORDER_VRGB:
		case CAIRO_SUBPIXEL_ORDER_VBGR:
		    load_target = FT_LOAD_TARGET_LCD_V;
		break;
		}
	    }
9
	    break;
	}
    }
962
    if (other->base.variations) {
3
      if (options->base.variations) {
        char *p;
        /* 'merge' variations by concatenating - later entries win */
3
        p = malloc (strlen (other->base.variations) + strlen (options->base.variations) + 2);
3
        p[0] = 0;
3
        strcat (p, other->base.variations);
3
        strcat (p, ",");
3
        strcat (p, options->base.variations);
3
        free (options->base.variations);
3
        options->base.variations = p;
      }
      else {
        options->base.variations = strdup (other->base.variations);
      }
    }
962
    options->load_flags = load_flags | load_target;
962
    options->synth_flags = other->synth_flags;
962
}
static cairo_status_t
962
_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)
{
962
    cairo_ft_font_face_t *font_face = abstract_font_face;
    cairo_ft_scaled_font_t *scaled_font;
    FT_Face face;
    FT_Size_Metrics *metrics;
    cairo_font_extents_t fs_metrics;
    cairo_status_t status;
    cairo_ft_unscaled_font_t *unscaled;
962
    assert (font_face->unscaled);
962
    face = _cairo_ft_unscaled_font_lock_face (font_face->unscaled);
962
    if (unlikely (face == NULL)) /* backend error */
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
962
    scaled_font = _cairo_calloc (sizeof (cairo_ft_scaled_font_t));
962
    if (unlikely (scaled_font == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto FAIL;
    }
962
    scaled_font->unscaled = unscaled = font_face->unscaled;
962
    _cairo_unscaled_font_reference (&unscaled->base);
962
    _cairo_font_options_init_copy (&scaled_font->ft_options.base, options);
962
    _cairo_ft_options_merge (&scaled_font->ft_options, &font_face->ft_options);
962
    status = _cairo_scaled_font_init (&scaled_font->base,
			              &font_face->base,
				      font_matrix, ctm, options,
				      &_cairo_ft_scaled_font_backend);
962
    if (unlikely (status))
9
	goto CLEANUP_SCALED_FONT;
953
    status = _cairo_ft_unscaled_font_set_scale (unscaled,
				                &scaled_font->base.scale);
953
    if (unlikely (status)) {
	/* This can only fail if we encounter an error with the underlying
	 * font, so propagate the error back to the font-face. */
	_cairo_ft_unscaled_font_unlock_face (unscaled);
	_cairo_unscaled_font_destroy (&unscaled->base);
	free (scaled_font);
	return status;
    }
953
    metrics = &face->size->metrics;
    /*
     * Get to unscaled metrics so that the upper level can get back to
     * user space
     *
     * Also use this path for bitmap-only fonts.  The other branch uses
     * face members that are only relevant for scalable fonts.  This is
     * detected by simply checking for units_per_EM==0.
     */
953
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
144
	face->units_per_EM == 0) {
824
	fs_metrics.ascent =        SCALE (DOUBLE_FROM_26_6 (metrics->ascender), unscaled->y_scale);
824
	fs_metrics.descent =       SCALE (DOUBLE_FROM_26_6 (- metrics->descender), unscaled->y_scale);
824
	fs_metrics.height =        SCALE (DOUBLE_FROM_26_6 (metrics->height), unscaled->y_scale);
1648
	if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
812
	    fs_metrics.max_x_advance = SCALE (DOUBLE_FROM_26_6 (metrics->max_advance), unscaled->x_scale);
812
	    fs_metrics.max_y_advance = 0;
	} else {
12
	    fs_metrics.max_x_advance = 0;
12
	    fs_metrics.max_y_advance = SCALE (DOUBLE_FROM_26_6 (metrics->max_advance), unscaled->y_scale);
	}
    } else {
129
	double scale = face->units_per_EM;
129
	fs_metrics.ascent =        face->ascender / scale;
129
	fs_metrics.descent =       - face->descender / scale;
129
	fs_metrics.height =        face->height / scale;
129
	if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
129
	    fs_metrics.max_x_advance = face->max_advance_width / scale;
129
	    fs_metrics.max_y_advance = 0;
	} else {
	    fs_metrics.max_x_advance = 0;
	    fs_metrics.max_y_advance = face->max_advance_height / scale;
	}
    }
953
    status = _cairo_scaled_font_set_metrics (&scaled_font->base, &fs_metrics);
953
    if (unlikely (status))
	goto CLEANUP_SCALED_FONT;
953
    _cairo_ft_unscaled_font_unlock_face (unscaled);
953
    *font_out = &scaled_font->base;
953
    return CAIRO_STATUS_SUCCESS;
9
  CLEANUP_SCALED_FONT:
9
    _cairo_unscaled_font_destroy (&unscaled->base);
9
    free (scaled_font);
9
  FAIL:
9
    _cairo_ft_unscaled_font_unlock_face (font_face->unscaled);
9
    *font_out = _cairo_scaled_font_create_in_error (status);
9
    return CAIRO_STATUS_SUCCESS; /* non-backend error */
}
cairo_bool_t
1002
_cairo_scaled_font_is_ft (cairo_scaled_font_t *scaled_font)
{
1002
    return scaled_font->backend == &_cairo_ft_scaled_font_backend;
}
static void
_cairo_ft_scaled_font_fini (void *abstract_font)
{
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    if (scaled_font == NULL)
        return;
    _cairo_font_options_fini (&scaled_font->ft_options.base);
    _cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
}
static int
480
_move_to (FT_Vector *to, void *closure)
{
480
    cairo_path_fixed_t *path = closure;
    cairo_fixed_t x, y;
480
    x = _cairo_fixed_from_26_6 (to->x);
480
    y = _cairo_fixed_from_26_6 (to->y);
480
    if (_cairo_path_fixed_close_path (path) != CAIRO_STATUS_SUCCESS)
	return 1;
480
    if (_cairo_path_fixed_move_to (path, x, y) != CAIRO_STATUS_SUCCESS)
	return 1;
480
    return 0;
}
static int
2061
_line_to (FT_Vector *to, void *closure)
{
2061
    cairo_path_fixed_t *path = closure;
    cairo_fixed_t x, y;
2061
    x = _cairo_fixed_from_26_6 (to->x);
2061
    y = _cairo_fixed_from_26_6 (to->y);
2061
    if (_cairo_path_fixed_line_to (path, x, y) != CAIRO_STATUS_SUCCESS)
	return 1;
2061
    return 0;
}
static int
3432
_conic_to (FT_Vector *control, FT_Vector *to, void *closure)
{
3432
    cairo_path_fixed_t *path = closure;
    cairo_fixed_t x0, y0;
    cairo_fixed_t x1, y1;
    cairo_fixed_t x2, y2;
    cairo_fixed_t x3, y3;
    cairo_point_t conic;
3432
    if (! _cairo_path_fixed_get_current_point (path, &x0, &y0))
	return 1;
3432
    conic.x = _cairo_fixed_from_26_6 (control->x);
3432
    conic.y = _cairo_fixed_from_26_6 (control->y);
3432
    x3 = _cairo_fixed_from_26_6 (to->x);
3432
    y3 = _cairo_fixed_from_26_6 (to->y);
3432
    x1 = x0 + 2.0/3.0 * (conic.x - x0);
3432
    y1 = y0 + 2.0/3.0 * (conic.y - y0);
3432
    x2 = x3 + 2.0/3.0 * (conic.x - x3);
3432
    y2 = y3 + 2.0/3.0 * (conic.y - y3);
3432
    if (_cairo_path_fixed_curve_to (path,
				    x1, y1,
				    x2, y2,
				    x3, y3) != CAIRO_STATUS_SUCCESS)
	return 1;
3432
    return 0;
}
static int
_cubic_to (FT_Vector *control1, FT_Vector *control2,
	   FT_Vector *to, void *closure)
{
    cairo_path_fixed_t *path = closure;
    cairo_fixed_t x0, y0;
    cairo_fixed_t x1, y1;
    cairo_fixed_t x2, y2;
    x0 = _cairo_fixed_from_26_6 (control1->x);
    y0 = _cairo_fixed_from_26_6 (control1->y);
    x1 = _cairo_fixed_from_26_6 (control2->x);
    y1 = _cairo_fixed_from_26_6 (control2->y);
    x2 = _cairo_fixed_from_26_6 (to->x);
    y2 = _cairo_fixed_from_26_6 (to->y);
    if (_cairo_path_fixed_curve_to (path,
				    x0, y0,
				    x1, y1,
				    x2, y2) != CAIRO_STATUS_SUCCESS)
	return 1;
    return 0;
}
cairo_status_t
330
_cairo_ft_face_decompose_glyph_outline (FT_Face		     face,
					cairo_path_fixed_t **pathp)
{
    static const FT_Outline_Funcs outline_funcs = {
	(FT_Outline_MoveToFunc)_move_to,
	(FT_Outline_LineToFunc)_line_to,
	(FT_Outline_ConicToFunc)_conic_to,
	(FT_Outline_CubicToFunc)_cubic_to,
	0, /* shift */
	0, /* delta */
    };
    static const FT_Matrix invert_y = {
	DOUBLE_TO_16_16 (1.0), 0,
	0, DOUBLE_TO_16_16 (-1.0),
    };
    FT_GlyphSlot glyph;
    cairo_path_fixed_t *path;
    cairo_status_t status;
330
    path = _cairo_path_fixed_create ();
330
    if (!path)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
330
    glyph = face->glyph;
    /* Font glyphs have an inverted Y axis compared to cairo. */
330
    FT_Outline_Transform (&glyph->outline, &invert_y);
330
    if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
	_cairo_path_fixed_destroy (path);
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
330
    status = _cairo_path_fixed_close_path (path);
330
    if (unlikely (status)) {
	_cairo_path_fixed_destroy (path);
	return status;
    }
330
    *pathp = path;
330
    return CAIRO_STATUS_SUCCESS;
}
/*
 * Translate glyph to match its metrics.
 */
static void
72
_cairo_ft_scaled_glyph_vertical_layout_bearing_fix (void        *abstract_font,
						    FT_GlyphSlot glyph)
{
72
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    FT_Vector vector;
72
    vector.x = glyph->metrics.vertBearingX - glyph->metrics.horiBearingX;
72
    vector.y = -glyph->metrics.vertBearingY - glyph->metrics.horiBearingY;
72
    if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
72
	FT_Vector_Transform (&vector, &scaled_font->unscaled->Current_Shape);
72
	FT_Outline_Translate(&glyph->outline, vector.x, vector.y);
    } else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
	glyph->bitmap_left += vector.x / 64;
	glyph->bitmap_top  += vector.y / 64;
    }
72
}
static void
10144
cairo_ft_apply_variations (FT_Face                 face,
			   cairo_ft_scaled_font_t *scaled_font)
{
    FT_MM_Var *ft_mm_var;
    FT_Error ret;
10144
    unsigned int instance_id = scaled_font->unscaled->id >> 16;
10144
    ret = FT_Get_MM_Var (face, &ft_mm_var);
10144
    if (ret == 0) {
        FT_Fixed *current_coords;
        FT_Fixed *coords;
        unsigned int i;
        const char *p;
1930
        coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
	/* FIXME check coords. */
1930
	if (scaled_font->unscaled->variations)
	{
	    memcpy (coords, scaled_font->unscaled->variations, ft_mm_var->num_axis * sizeof (*coords));
	}
1930
	else if (instance_id && instance_id <= ft_mm_var->num_namedstyles)
1902
	{
1902
	    FT_Var_Named_Style *instance = &ft_mm_var->namedstyle[instance_id - 1];
1902
	    memcpy (coords, instance->coords, ft_mm_var->num_axis * sizeof (*coords));
	}
	else
56
	    for (i = 0; i < ft_mm_var->num_axis; i++)
28
		coords[i] = ft_mm_var->axis[i].def;
1930
        p = scaled_font->ft_options.base.variations;
1980
        while (p && *p) {
            const char *start;
            const char *end, *end2;
            FT_ULong tag;
            double value;
50
            while (_cairo_isspace (*p)) p++;
50
            start = p;
50
            end = strchr (p, ',');
50
            if (end && (end - p < 6))
                goto skip;
50
            tag = FT_MAKE_TAG(p[0], p[1], p[2], p[3]);
50
            p += 4;
50
            while (_cairo_isspace (*p)) p++;
50
            if (*p == '=') p++;
50
            if (p - start < 5)
                goto skip;
50
            value = _cairo_strtod (p, (char **) &end2);
50
            while (end2 && _cairo_isspace (*end2)) end2++;
50
            if (end2 && (*end2 != ',' && *end2 != '\0'))
                goto skip;
51
            for (i = 0; i < ft_mm_var->num_axis; i++) {
50
                if (ft_mm_var->axis[i].tag == tag) {
49
                    coords[i] = (FT_Fixed)(value*65536);
49
                    break;
                }
            }
1
skip:
50
            p = end ? end + 1 : NULL;
        }
1930
        current_coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
1930
        ret = FT_Get_Var_Design_Coordinates (face, ft_mm_var->num_axis, current_coords);
1930
        if (ret == 0) {
3856
            for (i = 0; i < ft_mm_var->num_axis; i++) {
1930
              if (coords[i] != current_coords[i])
4
                break;
            }
1930
            if (i == ft_mm_var->num_axis)
1926
              goto done;
        }
4
        FT_Set_Var_Design_Coordinates (face, ft_mm_var->num_axis, coords);
1930
done:
1930
        free (coords);
1930
        free (current_coords);
1930
        FT_Done_MM_Var (face->glyph->library, ft_mm_var);
    }
10144
}
typedef enum {
    CAIRO_FT_GLYPH_TYPE_BITMAP,
    CAIRO_FT_GLYPH_TYPE_OUTLINE,
    CAIRO_FT_GLYPH_TYPE_SVG,
    CAIRO_FT_GLYPH_TYPE_COLR_V0,
    CAIRO_FT_GLYPH_TYPE_COLR_V1,
} cairo_ft_glyph_format_t;
typedef struct {
    cairo_scaled_glyph_private_t base;
    cairo_ft_glyph_format_t format;
} cairo_ft_glyph_private_t;
static const int ft_glyph_private_key;
static cairo_int_status_t
10119
_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)
{
    FT_Error error;
    cairo_status_t status;
    cairo_ft_glyph_private_t *glyph_priv;
10119
    glyph_priv = (cairo_ft_glyph_private_t *) _cairo_scaled_glyph_find_private (scaled_glyph,
                                                                                &ft_glyph_private_key);
10119
    assert (glyph_priv != NULL);
10119
    if (use_em_size) {
	cairo_matrix_t em_size;
1710
	cairo_matrix_init_scale (&em_size, face->units_per_EM, face->units_per_EM);
1710
	status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled, &em_size);
    } else {
8409
	status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
						    &scaled_font->base.scale);
    }
10119
    if (unlikely (status))
	return status;
10119
    cairo_ft_apply_variations (face, scaled_font);
#if defined(HAVE_FT_LOAD_NO_SVG)
10119
    if (load_flags & FT_LOAD_COLOR && glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
        load_flags |= FT_LOAD_NO_SVG;
#endif
10119
    error = FT_Load_Glyph (face,
10119
			   _cairo_scaled_glyph_index(scaled_glyph),
			   load_flags);
    /* XXX ignoring all other errors for now.  They are not fatal, typically
     * just a glyph-not-found. */
10119
    if (error == FT_Err_Out_Of_Memory)
	return  _cairo_error (CAIRO_STATUS_NO_MEMORY);
    /*
     * synthesize glyphs if requested
     */
10119
    if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
	FT_GlyphSlot_Embolden (face->glyph);
10119
    if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
	FT_GlyphSlot_Oblique (face->glyph);
10119
    if (vertical_layout)
72
	_cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, face->glyph);
10119
    if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
        FT_Pos xshift, yshift;
9183
        xshift = _cairo_scaled_glyph_xphase (scaled_glyph) << 4;
9183
        yshift = _cairo_scaled_glyph_yphase (scaled_glyph) << 4;
9183
        FT_Outline_Translate (&face->glyph->outline, xshift, -yshift);
    }
10119
    return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_ft_glyph_fini (cairo_scaled_glyph_private_t *glyph_private,
		      cairo_scaled_glyph_t *glyph,
		      cairo_scaled_font_t  *font)
{
    cairo_list_del (&glyph_private->link);
    free (glyph_private);
}
static void
273
_cairo_ft_scaled_glyph_set_palette (cairo_ft_scaled_font_t  *scaled_font,
				    FT_Face                  face,
				    unsigned int            *num_entries_ret,
				    FT_Color               **entries_ret)
{
273
    unsigned int num_entries = 0;
273
    FT_Color *entries = NULL;
    FT_Palette_Data palette_data;
273
    if (FT_Palette_Data_Get (face, &palette_data) == 0 && palette_data.num_palettes > 0) {
267
	FT_UShort palette_index = CAIRO_COLOR_PALETTE_DEFAULT;
267
	if (scaled_font->base.options.palette_index < palette_data.num_palettes)
267
	    palette_index = scaled_font->base.options.palette_index;
267
	if (FT_Palette_Select (face, palette_index, &entries) == 0) {
267
	    num_entries = palette_data.num_palette_entries;
            /* Overlay custom colors */
285
            for (unsigned int i = 0; i < scaled_font->base.options.custom_palette_size; i++) {
18
                cairo_palette_color_t *entry = &scaled_font->base.options.custom_palette[i];
18
                if (entry->index < num_entries) {
18
                    entries[entry->index].red = 255 * entry->red;
18
                    entries[entry->index].green = 255 * entry->green;
18
                    entries[entry->index].blue = 255 * entry->blue;
18
                    entries[entry->index].alpha = 255 * entry->alpha;
                }
            }
        }
    }
273
    if (num_entries_ret)
270
	*num_entries_ret = num_entries;
273
    if (entries_ret)
270
	*entries_ret = entries;
273
}
/* returns TRUE if foreground color used */
static cairo_bool_t
3
_cairo_ft_scaled_glyph_set_foreground_color (cairo_ft_scaled_font_t *scaled_font,
					     cairo_scaled_glyph_t   *scaled_glyph,
					     FT_Face                 face,
					     const cairo_color_t    *foreground_color)
{
3
    cairo_bool_t uses_foreground_color = FALSE;
    FT_LayerIterator  iterator;
    FT_UInt layer_glyph_index;
    FT_UInt layer_color_index;
    FT_Color color;
    /* Check if there is a layer that uses the foreground color */
3
    iterator.p  = NULL;
6
    while (FT_Get_Color_Glyph_Layer(face,
3
				    _cairo_scaled_glyph_index (scaled_glyph),
				    &layer_glyph_index,
				    &layer_color_index,
				    &iterator)) {
	if (layer_color_index == 0xFFFF) {
	    uses_foreground_color = TRUE;
	    break;
	}
    }
3
    if (uses_foreground_color) {
	color.red = (FT_Byte)(foreground_color->red * 0xFF);
	color.green = (FT_Byte)(foreground_color->green * 0xFF);
	color.blue = (FT_Byte)(foreground_color->blue * 0xFF);
	color.alpha = (FT_Byte)(foreground_color->alpha * 0xFF);
	FT_Palette_Set_Foreground_Color (face, color);
    }
3
    return uses_foreground_color;
}
static cairo_int_status_t
2964
_cairo_ft_scaled_glyph_init_surface (cairo_ft_scaled_font_t     *scaled_font,
				     cairo_scaled_glyph_t	*scaled_glyph,
				     cairo_ft_glyph_private_t   *glyph_priv,
				     cairo_scaled_glyph_info_t	 info,
				     FT_Face face,
				     const cairo_color_t        *foreground_color,
				     cairo_bool_t vertical_layout,
				     int load_flags)
{
2964
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_GlyphSlot glyph;
    cairo_status_t status;
    cairo_image_surface_t	*surface;
2964
    cairo_bool_t uses_foreground_color = FALSE;
    /* Only one info type at a time handled in this function */
2964
    assert (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE || info == CAIRO_SCALED_GLYPH_INFO_SURFACE);
2964
    if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) {
3
	if (!unscaled->have_color) {
	    scaled_glyph->color_glyph = FALSE;
	    scaled_glyph->color_glyph_set = TRUE;
	    return CAIRO_INT_STATUS_UNSUPPORTED;
	}
3
	uses_foreground_color = _cairo_ft_scaled_glyph_set_foreground_color (scaled_font,
									     scaled_glyph,
									     face,
									     foreground_color);
3
	_cairo_ft_scaled_glyph_set_palette (scaled_font, face, NULL, NULL);
3
        load_flags &= ~FT_LOAD_MONOCHROME;
	/* clear load target mode */
3
	load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(load_flags)));
3
	load_flags |= FT_LOAD_TARGET_NORMAL;
3
	load_flags |= FT_LOAD_COLOR;
    } else { /* info == CAIRO_SCALED_GLYPH_INFO_SURFACE */
2961
        load_flags &= ~FT_LOAD_COLOR;
    }
2964
    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
						scaled_glyph,
						face,
						load_flags,
						FALSE,
						vertical_layout);
2964
    if (unlikely (status))
	return status;
2964
    glyph = face->glyph;
2964
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_OUTLINE) {
2751
	status = _render_glyph_outline (face, &scaled_font->ft_options.base,
					    &surface);
    } else {
213
	status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
					   &surface);
213
	if (likely (status == CAIRO_STATUS_SUCCESS) && unscaled->have_shape) {
105
	    status = _transform_glyph_bitmap (&unscaled->current_shape,
					      &surface);
105
	    if (unlikely (status))
		cairo_surface_destroy (&surface->base);
	}
    }
2964
    if (unlikely (status))
	return status;
2964
    if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) {
	/* We tried loading a color glyph and can now check if we got
	 * a color glyph and set scaled_glyph->color_glyph
	 * accordingly */
6
	if (pixman_image_get_format (surface->pixman_image) == PIXMAN_a8r8g8b8 &&
3
	    !pixman_image_get_component_alpha (surface->pixman_image))
	{
3
	    _cairo_scaled_glyph_set_color_surface (scaled_glyph,
						   &scaled_font->base,
						   surface,
						   uses_foreground_color ? foreground_color : NULL);
3
	    scaled_glyph->color_glyph = TRUE;
	} else {
	    /* We didn't ask for a non-color surface, but store it
	     * anyway so we don't have to load it again. */
	    _cairo_scaled_glyph_set_surface (scaled_glyph,
					     &scaled_font->base,
					     surface);
	    scaled_glyph->color_glyph = FALSE;
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
	}
3
	scaled_glyph->color_glyph_set = TRUE;
    } else { /* info == CAIRO_SCALED_GLYPH_INFO_SURFACE */
2961
	_cairo_scaled_glyph_set_surface (scaled_glyph,
					 &scaled_font->base,
					 surface);
    }
2964
    return status;
}
static cairo_int_status_t
_cairo_ft_scaled_glyph_init_record_colr_v0_glyph (cairo_ft_scaled_font_t *scaled_font,
						  cairo_scaled_glyph_t   *scaled_glyph,
						  FT_Face                 face,
						  cairo_bool_t            vertical_layout,
						  int                     load_flags)
{
    cairo_surface_t *recording_surface;
    cairo_t *cr;
    cairo_status_t status;
    FT_Color *palette;
    unsigned int num_palette_entries;
    FT_LayerIterator iterator;
    FT_UInt layer_glyph_index;
    FT_UInt layer_color_index;
    cairo_path_fixed_t *path_fixed;
    cairo_path_t *path;
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
    load_flags &= ~FT_LOAD_MONOCHROME;
    /* clear load target mode */
    load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(load_flags)));
    load_flags |= FT_LOAD_TARGET_NORMAL;
    load_flags |= FT_LOAD_COLOR;
    recording_surface =
	cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
    cr = cairo_create (recording_surface);
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
        cairo_matrix_t scale;
	scale = scaled_font->base.scale;
	scale.x0 = scale.y0 = 0.;
	cairo_set_matrix (cr, &scale);
    }
    iterator.p  = NULL;
    while (FT_Get_Color_Glyph_Layer(face,
				    _cairo_scaled_glyph_index (scaled_glyph),
				    &layer_glyph_index,
				    &layer_color_index,
				    &iterator))
    {
	cairo_pattern_t *pattern;
	if (layer_color_index == 0xFFFF) {
	    pattern = _cairo_pattern_create_foreground_marker ();
	} else {
	    double r = 0, g = 0, b = 0, a = 1;
	    if (layer_color_index <  num_palette_entries) {
		FT_Color *color = &palette[layer_color_index];
		r = color->red / 255.0;
		g = color->green/ 255.0;
		b = color->blue / 255.0;
		a = color->alpha / 255.0;
	    }
	    pattern = cairo_pattern_create_rgba (r, g, b, a);
	}
	cairo_set_source (cr, pattern);
	cairo_pattern_destroy (pattern);
	if (FT_Load_Glyph (face, layer_glyph_index, load_flags) != 0) {
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
	    goto cleanup;
	}
	status = _cairo_ft_face_decompose_glyph_outline (face, &path_fixed);
	if (unlikely (status))
	    return status;
	path = _cairo_path_create (path_fixed, cr);
	_cairo_path_fixed_destroy (path_fixed);
	cairo_append_path(cr, path);
	cairo_path_destroy (path);
	cairo_fill (cr);
    }
  cleanup:
    cairo_destroy (cr);
    if (status) {
	cairo_surface_destroy (recording_surface);
	return status;
    }
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
					       &scaled_font->base,
					       recording_surface,
					       NULL);
    return status;
}
static cairo_int_status_t
_cairo_ft_scaled_glyph_init_record_colr_v1_glyph (cairo_ft_scaled_font_t *scaled_font,
						  cairo_scaled_glyph_t   *scaled_glyph,
						  FT_Face                 face,
						  const cairo_color_t    *foreground_color,
						  cairo_text_extents_t   *extents)
{
#if HAVE_FT_COLR_V1
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_surface_t *recording_surface;
    cairo_t *cr;
    FT_Color *palette;
    unsigned int num_palette_entries;
    cairo_bool_t foreground_source_used = FALSE;
    recording_surface =
	cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
    cairo_surface_set_device_scale (recording_surface, 1, -1);
    cr = cairo_create (recording_surface);
    cairo_set_font_size (cr, 1.0);
    cairo_set_font_options (cr, &scaled_font->base.options);
    extents->x_bearing = DOUBLE_FROM_26_6(face->bbox.xMin);
    extents->y_bearing = DOUBLE_FROM_26_6(face->bbox.yMin);
    extents->width = DOUBLE_FROM_26_6(face->bbox.xMax) - extents->x_bearing;
    extents->height = DOUBLE_FROM_26_6(face->bbox.yMax) - extents->y_bearing;
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
	cairo_pattern_t *foreground_pattern = _cairo_pattern_create_solid (foreground_color);
	status = _cairo_render_colr_v1_glyph (face,
					      _cairo_scaled_glyph_index (scaled_glyph),
                                              palette,
                                              num_palette_entries,
					      cr,
					      foreground_pattern,
					      &foreground_source_used);
	cairo_pattern_destroy (foreground_pattern);
	if (status == CAIRO_STATUS_SUCCESS)
	    status = cairo_status (cr);
    }
    cairo_destroy (cr);
    if (status) {
	cairo_surface_destroy (recording_surface);
	scaled_glyph->color_glyph = FALSE;
	scaled_glyph->color_glyph_set = TRUE;
	return status;
    }
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
					       &scaled_font->base,
					       recording_surface,
					       foreground_source_used ? foreground_color : NULL);
    scaled_glyph->color_glyph = TRUE;
    scaled_glyph->color_glyph_set = TRUE;
    /* get metrics */
    /* Copied from cairo-user-font.c */
    cairo_matrix_t extent_scale;
    double extent_x_scale = 1.0;
    double extent_y_scale = 1.0;
    double snap_x_scale;
    double snap_y_scale;
    double fixed_scale, x_scale, y_scale;
    extent_scale = scaled_font->base.scale_inverse;
    snap_x_scale = 1.0;
    snap_y_scale = 1.0;
    status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
							&x_scale, &y_scale,
							1);
    if (status == CAIRO_STATUS_SUCCESS) {
	if (x_scale == 0)
	    x_scale = 1;
	if (y_scale == 0)
	    y_scale = 1;
	snap_x_scale = x_scale;
	snap_y_scale = y_scale;
	/* since glyphs are pretty much 1.0x1.0, we can reduce error by
	 * scaling to a larger square.  say, 1024.x1024. */
	fixed_scale = 1024;
	x_scale /= fixed_scale;
	y_scale /= fixed_scale;
	cairo_matrix_scale (&extent_scale, 1.0 / x_scale, 1.0 / y_scale);
	extent_x_scale = x_scale;
	extent_y_scale = y_scale;
    }
    {
	/* compute width / height */
	cairo_box_t bbox;
	double x1, y1, x2, y2;
	double x_scale, y_scale;
	/* Compute extents.x/y/width/height from recording_surface,
	 * in font space.
	 */
	status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
						    &bbox,
						    &extent_scale);
	if (unlikely (status))
	    return status;
	_cairo_box_to_doubles (&bbox, &x1, &y1, &x2, &y2);
	x_scale = extent_x_scale;
	y_scale = extent_y_scale;
	extents->x_bearing = x1 * x_scale;
	extents->y_bearing = y1 * y_scale;
	extents->width     = (x2 - x1) * x_scale;
	extents->height    = (y2 - y1) * y_scale;
    }
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF) {
	extents->x_advance = _cairo_lround (extents->x_advance / snap_x_scale) * snap_x_scale;
	extents->y_advance = _cairo_lround (extents->y_advance / snap_y_scale) * snap_y_scale;
    }
    return status;
#else
    return CAIRO_INT_STATUS_UNSUPPORTED;
#endif
}
static cairo_int_status_t
270
_cairo_ft_scaled_glyph_init_record_svg_glyph (cairo_ft_scaled_font_t *scaled_font,
					      cairo_scaled_glyph_t   *scaled_glyph,
					      FT_Face                 face,
					      const cairo_color_t    *foreground_color,
					      cairo_text_extents_t   *extents)
{
#if HAVE_FT_SVG_DOCUMENT
270
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_surface_t *recording_surface;
    cairo_t *cr;
270
    FT_SVG_Document svg_doc = face->glyph->other;
    char *svg_document;
    FT_Color *palette;
    unsigned int num_palette_entries;
270
    cairo_bool_t foreground_source_used = FALSE;
    /* Create NULL terminated SVG document */
270
    svg_document = _cairo_strndup ((const char*)svg_doc->svg_document, svg_doc->svg_document_length);
    recording_surface =
270
	cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
270
    cr = cairo_create (recording_surface);
270
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
        cairo_matrix_t scale;
270
	scale = scaled_font->base.scale;
270
	scale.x0 = scale.y0 = 0.;
270
	cairo_set_matrix (cr, &scale);
    }
270
    cairo_set_font_size (cr, 1.0);
270
    cairo_set_font_options (cr, &scaled_font->base.options);
270
    extents->x_bearing = DOUBLE_FROM_26_6(face->bbox.xMin);
270
    extents->y_bearing = DOUBLE_FROM_26_6(face->bbox.yMin);
270
    extents->width = DOUBLE_FROM_26_6(face->bbox.xMax) - extents->x_bearing;
270
    extents->height = DOUBLE_FROM_26_6(face->bbox.yMax) - extents->y_bearing;
270
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
270
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
270
	cairo_pattern_t *foreground_pattern = _cairo_pattern_create_solid (foreground_color);
270
	status = _cairo_render_svg_glyph (svg_document,
270
					  svg_doc->start_glyph_id,
270
					  svg_doc->end_glyph_id,
270
					  _cairo_scaled_glyph_index(scaled_glyph),
270
					  svg_doc->units_per_EM,
					  palette,
					  num_palette_entries,
					  cr,
					  foreground_pattern,
					  &foreground_source_used);
270
	cairo_pattern_destroy (foreground_pattern);
270
	if (status == CAIRO_STATUS_SUCCESS)
270
	    status = cairo_status (cr);
    }
270
    cairo_destroy (cr);
270
    free (svg_document);
270
    if (status) {
	cairo_surface_destroy (recording_surface);
	scaled_glyph->color_glyph = FALSE;
	scaled_glyph->color_glyph_set = TRUE;
	return status;
    }
270
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
					       &scaled_font->base,
					       recording_surface,
270
					       foreground_source_used ? foreground_color : NULL);
270
    scaled_glyph->color_glyph = TRUE;
270
    scaled_glyph->color_glyph_set = TRUE;
    /* get metrics */
    /* Copied from cairo-user-font.c */
    cairo_matrix_t extent_scale;
    double extent_x_scale;
    double extent_y_scale;
    double snap_x_scale;
    double snap_y_scale;
    double fixed_scale, x_scale, y_scale;
270
    extent_scale = scaled_font->base.scale_inverse;
270
    snap_x_scale = 1.0;
270
    snap_y_scale = 1.0;
270
    status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
							&x_scale, &y_scale,
							1);
270
    if (status == CAIRO_STATUS_SUCCESS) {
270
	if (x_scale == 0)
	    x_scale = 1;
270
	if (y_scale == 0)
	    y_scale = 1;
270
	snap_x_scale = x_scale;
270
	snap_y_scale = y_scale;
	/* since glyphs are pretty much 1.0x1.0, we can reduce error by
	 * scaling to a larger square.  say, 1024.x1024. */
270
	fixed_scale = 1024;
270
	x_scale /= fixed_scale;
270
	y_scale /= fixed_scale;
270
	cairo_matrix_scale (&extent_scale, 1.0 / x_scale, 1.0 / y_scale);
270
	extent_x_scale = x_scale;
270
	extent_y_scale = y_scale;
    }
    {
	/* compute width / height */
	cairo_box_t bbox;
	double x1, y1, x2, y2;
	double x_scale, y_scale;
	/* Compute extents.x/y/width/height from recording_surface,
	 * in font space.
	 */
270
	status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
						    &bbox,
						    &extent_scale);
270
	if (unlikely (status))
	    return status;
270
	_cairo_box_to_doubles (&bbox, &x1, &y1, &x2, &y2);
270
	x_scale = extent_x_scale;
270
	y_scale = extent_y_scale;
270
	extents->x_bearing = x1 * x_scale;
270
	extents->y_bearing = y1 * y_scale;
270
	extents->width     = (x2 - x1) * x_scale;
270
	extents->height    = (y2 - y1) * y_scale;
    }
270
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF) {
270
	extents->x_advance = _cairo_lround (extents->x_advance / snap_x_scale) * snap_x_scale;
270
	extents->y_advance = _cairo_lround (extents->y_advance / snap_y_scale) * snap_y_scale;
    }
270
    return status;
#else
    return CAIRO_INT_STATUS_UNSUPPORTED;
#endif
}
static cairo_int_status_t
258
_cairo_ft_scaled_glyph_init_surface_for_recording_surface (cairo_ft_scaled_font_t *scaled_font,
							   cairo_scaled_glyph_t   *scaled_glyph,
							   const cairo_color_t    *foreground_color)
{
    cairo_surface_t *surface;
    int width, height;
258
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_bool_t foreground_used;
258
    width = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.x) -
258
	_cairo_fixed_integer_floor (scaled_glyph->bbox.p1.x);
258
    height = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.y) -
258
	_cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y);
258
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
516
    cairo_surface_set_device_offset (surface,
258
				     - _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.x),
258
				     - _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y));
258
    status = _cairo_recording_surface_replay_with_foreground_color (scaled_glyph->recording_surface,
								    surface,
								    foreground_color,
								    &foreground_used);
258
    if (unlikely (status)) {
	cairo_surface_destroy(surface);
	return status;
    }
258
    _cairo_scaled_glyph_set_color_surface (scaled_glyph,
					   &scaled_font->base,
					   (cairo_image_surface_t *)surface,
258
					   foreground_used ? foreground_color : NULL);
258
    surface = NULL;
258
    if (surface)
	cairo_surface_destroy (surface);
258
    return status;
}
static void
4962
_cairo_ft_scaled_glyph_get_metrics (cairo_ft_scaled_font_t     *scaled_font,
				    FT_Face face,
				    cairo_bool_t vertical_layout,
				    int load_flags,
				    cairo_text_extents_t *fs_metrics)
{
    FT_Glyph_Metrics *metrics;
4962
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
4962
    cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
4962
    FT_GlyphSlot glyph = face->glyph;
    /*
     * Compute font-space metrics
     */
4962
    metrics = &glyph->metrics;
    /*
     * Note: Y coordinates of the horizontal bearing need to be negated.
     *
     * Scale metrics back to glyph space from the scaled glyph space returned
     * by FreeType
     *
     * If we want hinted metrics but aren't asking for hinted glyphs from
     * FreeType, then we need to do the metric hinting ourselves.
     */
4962
    if (hint_metrics && (load_flags & FT_LOAD_NO_HINTING))
1818
    {
	FT_Pos x1, x2;
	FT_Pos y1, y2;
	FT_Pos advance;
1818
	if (!vertical_layout) {
1782
	    x1 = (metrics->horiBearingX) & -64;
1782
	    x2 = (metrics->horiBearingX + metrics->width + 63) & -64;
1782
	    y1 = (-metrics->horiBearingY) & -64;
1782
	    y2 = (-metrics->horiBearingY + metrics->height + 63) & -64;
1782
	    advance = ((metrics->horiAdvance + 32) & -64);
1782
	    fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (x1), unscaled->x_scale);
1782
	    fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (y1), unscaled->y_scale);
1782
	    fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (x2 - x1), unscaled->x_scale);
1782
	    fs_metrics->height  = SCALE (DOUBLE_FROM_26_6 (y2 - y1), unscaled->y_scale);
1782
	    fs_metrics->x_advance = SCALE (DOUBLE_FROM_26_6 (advance), unscaled->x_scale);
1782
	    fs_metrics->y_advance = 0;
	} else {
36
	    x1 = (metrics->vertBearingX) & -64;
36
	    x2 = (metrics->vertBearingX + metrics->width + 63) & -64;
36
	    y1 = (metrics->vertBearingY) & -64;
36
	    y2 = (metrics->vertBearingY + metrics->height + 63) & -64;
36
	    advance = ((metrics->vertAdvance + 32) & -64);
36
	    fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (x1), unscaled->x_scale);
36
	    fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (y1), unscaled->y_scale);
36
	    fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (x2 - x1), unscaled->x_scale);
36
	    fs_metrics->height  = SCALE (DOUBLE_FROM_26_6 (y2 - y1), unscaled->y_scale);
36
	    fs_metrics->x_advance = 0;
36
	    fs_metrics->y_advance = SCALE (DOUBLE_FROM_26_6 (advance), unscaled->y_scale);
	}
    } else {
3144
	fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (metrics->width), unscaled->x_scale);
3144
	fs_metrics->height = SCALE (DOUBLE_FROM_26_6 (metrics->height), unscaled->y_scale);
3144
	if (!vertical_layout) {
3144
	    fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->horiBearingX), unscaled->x_scale);
3144
	    fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (-metrics->horiBearingY), unscaled->y_scale);
3144
	    if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
1533
		fs_metrics->x_advance = SCALE (DOUBLE_FROM_26_6 (metrics->horiAdvance), unscaled->x_scale);
	    else
1611
		fs_metrics->x_advance = SCALE (DOUBLE_FROM_16_16 (glyph->linearHoriAdvance), unscaled->x_scale);
3144
	    fs_metrics->y_advance = 0;
	} else {
	    fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->vertBearingX), unscaled->x_scale);
	    fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->vertBearingY), unscaled->y_scale);
	    fs_metrics->x_advance = 0;
	    if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
		fs_metrics->y_advance = SCALE (DOUBLE_FROM_26_6 (metrics->vertAdvance), unscaled->y_scale);
	    else
		fs_metrics->y_advance = SCALE (DOUBLE_FROM_16_16 (glyph->linearVertAdvance), unscaled->y_scale);
	}
    }
4962
}
static cairo_bool_t
_cairo_ft_scaled_glyph_is_colr_v0 (cairo_ft_scaled_font_t *scaled_font,
				   cairo_scaled_glyph_t   *scaled_glyph,
				   FT_Face                 face)
{
    FT_LayerIterator  iterator;
    FT_UInt layer_glyph_index;
    FT_UInt layer_color_index;
    iterator.p  = NULL;
    if (FT_Get_Color_Glyph_Layer(face,
                                 _cairo_scaled_glyph_index (scaled_glyph),
                                 &layer_glyph_index,
                                 &layer_color_index,
				 &iterator) == 1)
    {
	return TRUE;
    }
    return FALSE;
}
static cairo_bool_t
258
_cairo_ft_scaled_glyph_is_colr_v1 (cairo_ft_scaled_font_t *scaled_font,
				   cairo_scaled_glyph_t   *scaled_glyph,
				   FT_Face                 face)
{
#if HAVE_FT_COLR_V1
258
    FT_OpaquePaint paint = { NULL, 0 };
258
    if (FT_Get_Color_Glyph_Paint (face,
258
				  _cairo_scaled_glyph_index (scaled_glyph),
				  FT_COLOR_INCLUDE_ROOT_TRANSFORM,
				  &paint) == 1)
    {
	return TRUE;
    }
#endif
258
    return FALSE;
}
static cairo_int_status_t
4962
_cairo_ft_scaled_glyph_init_metrics (cairo_ft_scaled_font_t  *scaled_font,
				     cairo_scaled_glyph_t    *scaled_glyph,
				     FT_Face                  face,
				     cairo_bool_t             vertical_layout,
				     int                      load_flags,
				     const cairo_color_t     *foreground_color)
{
4962
    cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
    cairo_text_extents_t fs_metrics;
    cairo_ft_glyph_private_t *glyph_priv;
4962
    cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
    /* _cairo_ft_scaled_glyph_init_metrics() is called once the first
     * time a cairo_scaled_glyph_t is created. We first allocate the
     * cairo_ft_glyph_private_t struct and determine the glyph type.
     */
4962
    glyph_priv = _cairo_calloc (sizeof (*glyph_priv));
4962
    if (unlikely (glyph_priv == NULL))
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4962
    _cairo_scaled_glyph_attach_private (scaled_glyph, &glyph_priv->base,
					&ft_glyph_private_key,
					_cairo_ft_glyph_fini);
    /* We need to load color to determine if this is a color format. */
4962
    int color_flag = 0;
4962
    if (scaled_font->unscaled->have_color && scaled_font->base.options.color_mode != CAIRO_COLOR_MODE_NO_COLOR)
261
	color_flag = FT_LOAD_COLOR;
    /* Ensure use_em_size = FALSE as the format (bitmap or outline)
     * may change with the size. */
4962
    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
						scaled_glyph,
						face,
						load_flags | color_flag,
						FALSE,
						vertical_layout);
4962
    if (unlikely (status))
	return status;
4962
    cairo_bool_t is_svg_format = FALSE;
#if HAVE_FT_SVG_DOCUMENT
4962
    if (face->glyph->format == FT_GLYPH_FORMAT_SVG)
258
	is_svg_format = TRUE;
#endif
4962
    if (is_svg_format) {
258
        glyph_priv->format = CAIRO_FT_GLYPH_TYPE_SVG;
#if defined(HAVE_FT_COLR_V1) && defined(HAVE_FT_LOAD_NO_SVG)
        /* Prefer COLRv1 table over SVG table due to performance reasons for now */
258
        if (_cairo_ft_scaled_glyph_is_colr_v1 (scaled_font, scaled_glyph, face)) {
            glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V1;
        }
#endif
4704
    } else if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
4491
	glyph_priv->format = CAIRO_FT_GLYPH_TYPE_OUTLINE;
4491
	if (color_flag) {
	    if (_cairo_ft_scaled_glyph_is_colr_v1 (scaled_font, scaled_glyph, face))
		glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V1;
	    else if (_cairo_ft_scaled_glyph_is_colr_v0 (scaled_font, scaled_glyph, face))
		glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V0;
	}
    } else {
	/* For anything else we let FreeType render a bitmap. */
213
	 glyph_priv->format =  CAIRO_FT_GLYPH_TYPE_BITMAP;
    }
    /* If hinting is off, load the glyph with font size set the the em size. */
4962
    if (!hint_metrics) {
1710
	status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
						    scaled_glyph,
						    face,
						    load_flags | color_flag,
						    TRUE,
						    vertical_layout);
1710
	if (unlikely (status))
	    return status;
    }
4962
    _cairo_ft_scaled_glyph_get_metrics (scaled_font,
					face,
					vertical_layout,
					load_flags,
					&fs_metrics);
    /* SVG and COLRv1 glyphs require the bounding box to be obtained
     * from the ink extents of the rendering. We need to render glyph
     * to a recording surface to obtain these extents. But we also
     * need the advance from _cairo_ft_scaled_glyph_get_metrics()
     * before calling this function.
     */
4962
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG) {
258
	status = (cairo_int_status_t)_cairo_ft_scaled_glyph_init_record_svg_glyph (scaled_font,
										   scaled_glyph,
										   face,
										   foreground_color,
										   &fs_metrics);
258
	if (unlikely (status))
	    return status;
    }
4962
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1) {
	/* Restore font size if previously loaded at em_size. */
	if (!hint_metrics) {
	    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
							scaled_glyph,
							face,
							load_flags | color_flag,
							FALSE,
							vertical_layout);
	    if (unlikely (status))
		return status;
	}
	status = (cairo_int_status_t)_cairo_ft_scaled_glyph_init_record_colr_v1_glyph (scaled_font,
										       scaled_glyph,
										       face,
										       foreground_color,
										       &fs_metrics);
	if (unlikely (status))
	    return status;
    }
4962
    _cairo_scaled_glyph_set_metrics (scaled_glyph,
				     &scaled_font->base,
				     &fs_metrics);
4962
    return status;
}
static cairo_int_status_t
7725
_cairo_ft_scaled_glyph_init (void			*abstract_font,
			     cairo_scaled_glyph_t	*scaled_glyph,
			     cairo_scaled_glyph_info_t	 info,
			     const cairo_color_t        *foreground_color)
{
7725
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
7725
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
7725
    int load_flags = scaled_font->ft_options.load_flags;
7725
    cairo_bool_t vertical_layout = FALSE;
7725
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_ft_glyph_private_t *glyph_priv;
7725
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
7725
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    /* Ignore global advance unconditionally */
7725
    load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
7725
    if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0 &&
471
	(info & (CAIRO_SCALED_GLYPH_INFO_SURFACE |
                 CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) == 0) {
471
	load_flags |= FT_LOAD_NO_BITMAP;
    }
    /*
     * Don't pass FT_LOAD_VERTICAL_LAYOUT to FT_Load_Glyph here as
     * suggested by freetype people.
     */
7725
    if (load_flags & FT_LOAD_VERTICAL_LAYOUT) {
54
	load_flags &= ~FT_LOAD_VERTICAL_LAYOUT;
54
	vertical_layout = TRUE;
    }
    /* Metrics will always be requested when a scaled glyph is created */
7725
    if (info & CAIRO_SCALED_GLYPH_INFO_METRICS) {
4962
	status = _cairo_ft_scaled_glyph_init_metrics (scaled_font,
						      scaled_glyph,
						      face,
						      vertical_layout,
						      load_flags,
						      foreground_color);
4962
	if (unlikely (status))
	    goto FAIL;
    }
7725
    glyph_priv = (cairo_ft_glyph_private_t *) _cairo_scaled_glyph_find_private (scaled_glyph,
										&ft_glyph_private_key);
7725
    assert (glyph_priv != NULL);
7725
    if (info & CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE) {
12
	status = CAIRO_INT_STATUS_UNSUPPORTED;
12
	if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG ||
	    glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V0 ||
	    glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
	{
12
	    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
							scaled_glyph,
							face,
							load_flags | FT_LOAD_COLOR,
							FALSE,
							vertical_layout);
12
	    if (unlikely (status))
		goto FAIL;
12
	    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG) {
12
		status = _cairo_ft_scaled_glyph_init_record_svg_glyph (scaled_font,
								       scaled_glyph,
								       face,
								       foreground_color,
								       &scaled_glyph->fs_metrics);
	    } else if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1) {
		status = _cairo_ft_scaled_glyph_init_record_colr_v1_glyph (scaled_font,
									   scaled_glyph,
									   face,
									   foreground_color,
									   &scaled_glyph->fs_metrics);
	    } else if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V0) {
		status = _cairo_ft_scaled_glyph_init_record_colr_v0_glyph (scaled_font,
									   scaled_glyph,
									   face,
									   vertical_layout,
									   load_flags);
	    }
	}
12
	if (status)
	    goto FAIL;
    }
7725
    if ((info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) && scaled_font->base.options.color_mode != CAIRO_COLOR_MODE_NO_COLOR) {
261
	if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG ||
3
	    glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
	{
258
	    status = _cairo_ft_scaled_glyph_init_surface_for_recording_surface (scaled_font,
										scaled_glyph,
										foreground_color);
	} else {
3
	    status = _cairo_ft_scaled_glyph_init_surface (scaled_font,
							  scaled_glyph,
							  glyph_priv,
							  CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE,
							  face,
							  foreground_color,
							  vertical_layout,
							  load_flags);
	}
261
	if (unlikely (status))
	    goto FAIL;
    }
7725
    if (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) {
2961
	status = _cairo_ft_scaled_glyph_init_surface (scaled_font,
						      scaled_glyph,
						      glyph_priv,
						      CAIRO_SCALED_GLYPH_INFO_SURFACE,
						      face,
						      NULL, /* foreground color */
						      vertical_layout,
						      load_flags);
2961
	if (unlikely (status))
	    goto FAIL;
    }
7725
    if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
471
	cairo_path_fixed_t *path = NULL; /* hide compiler warning */
	/* Load non-color glyph */
471
	status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
						    scaled_glyph,
						    face,
						    load_flags,
						    FALSE,
						    vertical_layout);
471
	if (unlikely (status))
141
	    goto FAIL;
471
	if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
330
	    status = _cairo_ft_face_decompose_glyph_outline (face, &path);
	else
141
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
471
	if (unlikely (status))
141
	    goto FAIL;
330
	_cairo_scaled_glyph_set_path (scaled_glyph,
				      &scaled_font->base,
				      path);
    }
7254
 FAIL:
7725
    _cairo_ft_unscaled_font_unlock_face (unscaled);
7725
    return status;
}
static unsigned long
331380
_cairo_ft_ucs4_to_index (void	    *abstract_font,
			 uint32_t    ucs4)
{
331380
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
331380
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    FT_UInt index;
331380
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
331380
    if (!face)
	return 0;
#if CAIRO_HAS_FC_FONT
331380
    index = FcFreeTypeCharIndex (face, ucs4);
#else
    index = FT_Get_Char_Index (face, ucs4);
#endif
331380
    _cairo_ft_unscaled_font_unlock_face (unscaled);
331380
    return index;
}
static cairo_int_status_t
_cairo_ft_load_truetype_table (void	       *abstract_font,
                              unsigned long     tag,
                              long              offset,
                              unsigned char    *buffer,
                              unsigned long    *length)
{
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
    /* We don't support the FreeType feature of loading a table
     * without specifying the size since this may overflow our
     * buffer. */
    assert (length != NULL);
    if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
        return CAIRO_INT_STATUS_UNSUPPORTED;
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    if (FT_IS_SFNT (face)) {
	if (buffer == NULL)
	    *length = 0;
	if (FT_Load_Sfnt_Table (face, tag, offset, buffer, length) == 0)
	    status = CAIRO_STATUS_SUCCESS;
    }
    _cairo_ft_unscaled_font_unlock_face (unscaled);
    return status;
}
static cairo_int_status_t
_cairo_ft_index_to_ucs4(void	        *abstract_font,
			unsigned long    index,
			uint32_t	*ucs4)
{
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    FT_ULong  charcode;
    FT_UInt   gindex;
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    *ucs4 = (uint32_t) -1;
    charcode = FT_Get_First_Char(face, &gindex);
    while (gindex != 0) {
	if (gindex == index) {
	    *ucs4 = charcode;
	    break;
	}
	charcode = FT_Get_Next_Char (face, charcode, &gindex);
    }
    _cairo_ft_unscaled_font_unlock_face (unscaled);
    return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_ft_is_synthetic (void	        *abstract_font,
			cairo_bool_t    *is_synthetic)
{
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    FT_Error error;
    if (scaled_font->ft_options.synth_flags != 0) {
	*is_synthetic = TRUE;
	return status;
    }
    *is_synthetic = FALSE;
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    if (face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
	FT_MM_Var *mm_var = NULL;
	FT_Fixed *coords = NULL;
	int num_axis;
	int i;
	/* If this is an MM or variable font we can't assume the current outlines
	 * are the same as the font tables */
	*is_synthetic = TRUE;
	error = FT_Get_MM_Var (face, &mm_var);
	if (error) {
	    status = _cairo_error (_cairo_ft_to_cairo_error (error));
	    goto cleanup;
	}
	num_axis = mm_var->num_axis;
	coords = _cairo_malloc_ab (num_axis, sizeof(FT_Fixed));
	if (!coords) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto cleanup;
	}
	/* Check if the current design coordinates are the default
	 * coordinates. In this case the current outlines match the
	 * font tables.
	 */
	FT_Get_Var_Blend_Coordinates (face, num_axis, coords);
	*is_synthetic = FALSE;
	for (i = 0; i < num_axis; i++) {
	    if (coords[i]) {
		*is_synthetic = TRUE;
		break;
	    }
	}
      cleanup:
	free (coords);
	FT_Done_MM_Var (face->glyph->library, mm_var);
    }
    _cairo_ft_unscaled_font_unlock_face (unscaled);
    return status;
}
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)
{
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    char buffer[256]; /* PLRM specifies max name length of 127 */
    FT_Error error;
    int i;
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    error = FT_Get_Glyph_Name (face, glyph_index, buffer, sizeof buffer);
    _cairo_ft_unscaled_font_unlock_face (unscaled);
    if (error != FT_Err_Ok) {
	/* propagate fatal errors from FreeType */
	if (error == FT_Err_Out_Of_Memory)
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	return CAIRO_INT_STATUS_UNSUPPORTED;
    }
    /* FT first numbers the glyphs in the order they are read from the
     * Type 1 font. Then if .notdef is not the first glyph, the first
     * glyph is swapped with .notdef to ensure that .notdef is at
     * glyph index 0.
     *
     * As all but two glyphs in glyph_names already have the same
     * index as the FT glyph index, we first check if
     * glyph_names[glyph_index] is the name we are looking for. If not
     * we fall back to searching the entire array.
     */
    if ((long)glyph_index < num_glyph_names &&
	strcmp (glyph_names[glyph_index], buffer) == 0)
    {
	*glyph_array_index = glyph_index;
	return CAIRO_STATUS_SUCCESS;
    }
    for (i = 0; i < num_glyph_names; i++) {
	if (strcmp (glyph_names[i], buffer) == 0) {
	    *glyph_array_index = i;
	    return CAIRO_STATUS_SUCCESS;
	}
    }
    return CAIRO_INT_STATUS_UNSUPPORTED;
}
static cairo_bool_t
_ft_is_type1 (FT_Face face)
{
    const char *font_format = FT_Get_Font_Format (face);
    if (font_format &&
	(strcmp (font_format, "Type 1") == 0 ||
	 strcmp (font_format, "CFF") == 0))
    {
	return TRUE;
    }
    return FALSE;
}
static cairo_int_status_t
_cairo_ft_load_type1_data (void	            *abstract_font,
			   long              offset,
			   unsigned char    *buffer,
			   unsigned long    *length)
{
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
    FT_Face face;
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
    unsigned long available_length;
    unsigned long ret;
    assert (length != NULL);
    if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
        return CAIRO_INT_STATUS_UNSUPPORTED;
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
    if (!face)
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    if (FT_IS_SFNT (face)) {
	status = CAIRO_INT_STATUS_UNSUPPORTED;
	goto unlock;
    }
    if (! _ft_is_type1 (face)) {
        status = CAIRO_INT_STATUS_UNSUPPORTED;
	goto unlock;
    }
    available_length = MAX (face->stream->size - offset, 0);
    if (!buffer) {
	*length = available_length;
    } else {
	if (*length > available_length) {
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
	} else if (face->stream->read != NULL) {
	    /* Note that read() may be implemented as a macro, thanks POSIX!, so we
	     * need to wrap the following usage in parentheses in order to
	     * disambiguate it for the pre-processor - using the verbose function
	     * pointer dereference for clarity.
	     */
	    ret = (* face->stream->read) (face->stream,
					  offset,
					  buffer,
					  *length);
	    if (ret != *length)
		status = _cairo_error (CAIRO_STATUS_READ_ERROR);
	} else {
	    memcpy (buffer, face->stream->base + offset, *length);
	}
    }
  unlock:
    _cairo_ft_unscaled_font_unlock_face (unscaled);
    return status;
}
static cairo_bool_t
140448
_cairo_ft_has_color_glyphs (void *scaled)
{
140448
    cairo_ft_unscaled_font_t *unscaled = ((cairo_ft_scaled_font_t *)scaled)->unscaled;
140448
    if (!unscaled->have_color_set) {
	FT_Face face;
	face = _cairo_ft_unscaled_font_lock_face (unscaled);
	if (unlikely (face == NULL))
	    return FALSE;
	_cairo_ft_unscaled_font_unlock_face (unscaled);
    }
140448
    return unscaled->have_color;
}
static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend = {
    CAIRO_FONT_TYPE_FT,
    _cairo_ft_scaled_font_fini,
    _cairo_ft_scaled_glyph_init,
    NULL,			/* text_to_glyphs */
    _cairo_ft_ucs4_to_index,
    _cairo_ft_load_truetype_table,
    _cairo_ft_index_to_ucs4,
    _cairo_ft_is_synthetic,
    _cairo_index_to_glyph_name,
    _cairo_ft_load_type1_data,
    _cairo_ft_has_color_glyphs
};
/* #cairo_ft_font_face_t */
#if CAIRO_HAS_FC_FONT
static cairo_font_face_t *
_cairo_ft_font_face_create_for_pattern (FcPattern *pattern);
static cairo_status_t
1826
_cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
				    cairo_font_face_t **font_face_out)
{
1826
    cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
    FcPattern *pattern;
    int fcslant;
    int fcweight;
1826
    pattern = FcPatternCreate ();
1826
    if (!pattern) {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	return font_face->status;
    }
1826
    if (!FcPatternAddString (pattern,
1826
		             FC_FAMILY, (unsigned char *) toy_face->family))
    {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	goto FREE_PATTERN;
    }
1826
    switch (toy_face->slant)
    {
6
    case CAIRO_FONT_SLANT_ITALIC:
6
        fcslant = FC_SLANT_ITALIC;
6
        break;
2
    case CAIRO_FONT_SLANT_OBLIQUE:
2
	fcslant = FC_SLANT_OBLIQUE;
2
        break;
1818
    case CAIRO_FONT_SLANT_NORMAL:
    default:
1818
        fcslant = FC_SLANT_ROMAN;
1818
        break;
    }
1826
    if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	goto FREE_PATTERN;
    }
1826
    switch (toy_face->weight)
    {
14
    case CAIRO_FONT_WEIGHT_BOLD:
14
        fcweight = FC_WEIGHT_BOLD;
14
        break;
1812
    case CAIRO_FONT_WEIGHT_NORMAL:
    default:
1812
        fcweight = FC_WEIGHT_MEDIUM;
1812
        break;
    }
1826
    if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	goto FREE_PATTERN;
    }
1826
    font_face = _cairo_ft_font_face_create_for_pattern (pattern);
1826
 FREE_PATTERN:
1826
    FcPatternDestroy (pattern);
1826
    *font_face_out = font_face;
1826
    return font_face->status;
}
#endif
static cairo_bool_t
1570
_cairo_ft_font_face_destroy (void *abstract_face)
{
1570
    cairo_ft_font_face_t *font_face = abstract_face;
    /* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
     * we have a special "zombie" state for the face when the unscaled font
     * is still alive but there are no other references to a font face with
     * the same FT_Face.
     *
     * We go from:
     *
     *   font_face ------> unscaled
     *        <-....weak....../
     *
     * To:
     *
     *    font_face <------- unscaled
     */
1570
    if (font_face->unscaled &&
6
	font_face->unscaled->from_face &&
	font_face->next == NULL &&
	font_face->unscaled->faces == font_face &&
	CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->unscaled->base.ref_count) > 1)
    {
	_cairo_unscaled_font_destroy (&font_face->unscaled->base);
	font_face->unscaled = NULL;
	return FALSE;
    }
1570
    if (font_face->unscaled) {
6
	cairo_ft_font_face_t *tmp_face = NULL;
6
	cairo_ft_font_face_t *last_face = NULL;
	/* Remove face from linked list */
6
	for (tmp_face = font_face->unscaled->faces;
12
	     tmp_face;
6
	     tmp_face = tmp_face->next)
	{
6
	    if (tmp_face == font_face) {
6
		if (last_face)
		    last_face->next = tmp_face->next;
		else
6
		    font_face->unscaled->faces = tmp_face->next;
	    }
6
	    last_face = tmp_face;
	}
6
	_cairo_unscaled_font_destroy (&font_face->unscaled->base);
6
	font_face->unscaled = NULL;
    }
1570
    _cairo_ft_options_fini (&font_face->ft_options);
#if CAIRO_HAS_FC_FONT
1570
    if (font_face->pattern) {
1564
	FcPatternDestroy (font_face->pattern);
1564
	cairo_font_face_destroy (font_face->resolved_font_face);
    }
#endif
1570
    return TRUE;
}
static cairo_font_face_t *
962
_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)
{
    /* The handling of font options is different depending on how the
     * font face was created. When the user creates a font face with
     * cairo_ft_font_face_create_for_ft_face(), then the load flags
     * passed in augment the load flags for the options.  But for
     * cairo_ft_font_face_create_for_pattern(), the load flags are
     * derived from a pattern where the user has called
     * cairo_ft_font_options_substitute(), so *just* use those load
     * flags and ignore the options.
     */
#if CAIRO_HAS_FC_FONT
962
    cairo_ft_font_face_t      *font_face = abstract_face;
    /* If we have an unresolved pattern, resolve it and create
     * unscaled font.  Otherwise, use the ones stored in font_face.
     */
962
    if (font_face->pattern) {
	cairo_font_face_t *resolved;
	/* Cache the resolved font whilst the FcConfig remains consistent. */
683
	resolved = font_face->resolved_font_face;
683
	if (resolved != NULL) {
414
	    if (! FcInitBringUptoDate ()) {
		_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
		return (cairo_font_face_t *) &_cairo_font_face_nil;
	    }
414
	    if (font_face->resolved_config == FcConfigGetCurrent ())
414
		return cairo_font_face_reference (resolved);
	    cairo_font_face_destroy (resolved);
	    font_face->resolved_font_face = NULL;
	}
269
	resolved = _cairo_ft_resolve_pattern (font_face->pattern,
					      font_matrix,
					      ctm,
					      options);
269
	if (unlikely (resolved->status))
	    return resolved;
269
	font_face->resolved_font_face = cairo_font_face_reference (resolved);
269
	font_face->resolved_config = FcConfigGetCurrent ();
269
	return resolved;
    }
#endif
279
    return abstract_face;
}
const cairo_font_face_backend_t _cairo_ft_font_face_backend = {
    CAIRO_FONT_TYPE_FT,
#if CAIRO_HAS_FC_FONT
    _cairo_ft_font_face_create_for_toy,
#else
    NULL,
#endif
    _cairo_ft_font_face_destroy,
    _cairo_ft_font_face_scaled_font_create,
    _cairo_ft_font_face_get_implementation
};
#if CAIRO_HAS_FC_FONT
static cairo_font_face_t *
1827
_cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
{
    cairo_ft_font_face_t *font_face;
1827
    font_face = _cairo_calloc (sizeof (cairo_ft_font_face_t));
1827
    if (unlikely (font_face == NULL)) {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	return (cairo_font_face_t *) &_cairo_font_face_nil;
    }
1827
    font_face->unscaled = NULL;
1827
    _get_pattern_ft_options (pattern, &font_face->ft_options);
1827
    font_face->next = NULL;
1827
    font_face->pattern = FcPatternDuplicate (pattern);
1827
    if (unlikely (font_face->pattern == NULL)) {
	free (font_face);
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	return (cairo_font_face_t *) &_cairo_font_face_nil;
    }
1827
    font_face->resolved_font_face = NULL;
1827
    font_face->resolved_config = NULL;
1827
    _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
1827
    return &font_face->base;
}
#endif
static cairo_font_face_t *
111020
_cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
			    cairo_ft_options_t	     *ft_options)
{
    cairo_ft_font_face_t *font_face, **prev_font_face;
    /* Looked for an existing matching font face */
111020
    for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
343463
	 font_face;
232443
	 prev_font_face = &font_face->next, font_face = font_face->next)
    {
343134
	if (font_face->ft_options.load_flags == ft_options->load_flags &&
221382
	    font_face->ft_options.synth_flags == ft_options->synth_flags &&
110691
	    cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
	{
110691
	    if (font_face->base.status) {
		/* The font_face has been left in an error state, abandon it. */
		*prev_font_face = font_face->next;
		break;
	    }
110691
	    if (font_face->unscaled == NULL) {
		/* Resurrect this "zombie" font_face (from
		 * _cairo_ft_font_face_destroy), switching its unscaled_font
		 * from owner to ownee. */
		font_face->unscaled = unscaled;
		_cairo_unscaled_font_reference (&unscaled->base);
		return &font_face->base;
	    } else
110691
		return cairo_font_face_reference (&font_face->base);
	}
    }
    /* No match found, create a new one */
329
    font_face = _cairo_calloc (sizeof (cairo_ft_font_face_t));
329
    if (unlikely (!font_face)) {
	_cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
	return (cairo_font_face_t *)&_cairo_font_face_nil;
    }
329
    font_face->unscaled = unscaled;
329
    _cairo_unscaled_font_reference (&unscaled->base);
329
    _cairo_ft_options_init_copy (&font_face->ft_options, ft_options);
329
    if (unscaled->faces && unscaled->faces->unscaled == NULL) {
	/* This "zombie" font_face (from _cairo_ft_font_face_destroy)
	 * is no longer needed. */
	assert (unscaled->from_face && unscaled->faces->next == NULL);
	cairo_font_face_destroy (&unscaled->faces->base);
	unscaled->faces = NULL;
    }
329
    font_face->next = unscaled->faces;
329
    unscaled->faces = font_face;
#if CAIRO_HAS_FC_FONT
329
    font_face->pattern = NULL;
#endif
329
    _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
329
    return &font_face->base;
}
/* implement the platform-specific interface */
#if CAIRO_HAS_FC_FONT
static cairo_status_t
287
_cairo_ft_font_options_substitute (const cairo_font_options_t *options,
				   FcPattern                  *pattern)
{
    FcValue v;
287
    if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
    {
189
	if (FcPatternGet (pattern, FC_ANTIALIAS, 0, &v) == FcResultNoMatch)
	{
	    if (! FcPatternAddBool (pattern,
			            FC_ANTIALIAS,
				    options->antialias != CAIRO_ANTIALIAS_NONE))
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    if (options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
		FcPatternDel (pattern, FC_RGBA);
		if (! FcPatternAddInteger (pattern, FC_RGBA, FC_RGBA_NONE))
		    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    }
	}
    }
287
    if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
    {
189
	if (FcPatternGet (pattern, FC_RGBA, 0, &v) == FcResultNoMatch)
	{
	    int rgba;
189
	    if (options->antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
15
		switch (options->subpixel_order) {
6
		case CAIRO_SUBPIXEL_ORDER_DEFAULT:
		case CAIRO_SUBPIXEL_ORDER_RGB:
		default:
6
		    rgba = FC_RGBA_RGB;
6
		    break;
3
		case CAIRO_SUBPIXEL_ORDER_BGR:
3
		    rgba = FC_RGBA_BGR;
3
		    break;
3
		case CAIRO_SUBPIXEL_ORDER_VRGB:
3
		    rgba = FC_RGBA_VRGB;
3
		    break;
3
		case CAIRO_SUBPIXEL_ORDER_VBGR:
3
		    rgba = FC_RGBA_VBGR;
3
		    break;
		}
	    } else {
174
		rgba = FC_RGBA_NONE;
	    }
189
	    if (! FcPatternAddInteger (pattern, FC_RGBA, rgba))
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	}
    }
287
    if (options->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
    {
	if (FcPatternGet (pattern, FC_LCD_FILTER, 0, &v) == FcResultNoMatch)
	{
	    int lcd_filter;
	    switch (options->lcd_filter) {
	    case CAIRO_LCD_FILTER_NONE:
		lcd_filter = FT_LCD_FILTER_NONE;
		break;
	    case CAIRO_LCD_FILTER_INTRA_PIXEL:
		lcd_filter = FT_LCD_FILTER_LEGACY;
		break;
	    case CAIRO_LCD_FILTER_FIR3:
		lcd_filter = FT_LCD_FILTER_LIGHT;
		break;
	    default:
	    case CAIRO_LCD_FILTER_DEFAULT:
	    case CAIRO_LCD_FILTER_FIR5:
		lcd_filter = FT_LCD_FILTER_DEFAULT;
		break;
	    }
	    if (! FcPatternAddInteger (pattern, FC_LCD_FILTER, lcd_filter))
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	}
    }
287
    if (options->hint_style != CAIRO_HINT_STYLE_DEFAULT)
    {
192
	if (FcPatternGet (pattern, FC_HINTING, 0, &v) == FcResultNoMatch)
	{
192
	    if (! FcPatternAddBool (pattern,
			            FC_HINTING,
192
				    options->hint_style != CAIRO_HINT_STYLE_NONE))
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	}
192
	if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
	{
	    int hint_style;
	    switch (options->hint_style) {
	    case CAIRO_HINT_STYLE_NONE:
		hint_style = FC_HINT_NONE;
		break;
	    case CAIRO_HINT_STYLE_SLIGHT:
		hint_style = FC_HINT_SLIGHT;
		break;
	    case CAIRO_HINT_STYLE_MEDIUM:
		hint_style = FC_HINT_MEDIUM;
		break;
	    case CAIRO_HINT_STYLE_FULL:
	    case CAIRO_HINT_STYLE_DEFAULT:
	    default:
		hint_style = FC_HINT_FULL;
		break;
	    }
	    if (! FcPatternAddInteger (pattern, FC_HINT_STYLE, hint_style))
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
	}
    }
287
    return CAIRO_STATUS_SUCCESS;
}
/**
 * cairo_ft_font_options_substitute:
 * @options: a #cairo_font_options_t object
 * @pattern: an existing #FcPattern
 *
 * Add options to a #FcPattern based on a #cairo_font_options_t font
 * options object. Options that are already in the pattern, are not overridden,
 * so you should call this function after calling FcConfigSubstitute() (the
 * user's settings should override options based on the surface type), but
 * before calling FcDefaultSubstitute().
 *
 * Since: 1.0
 **/
void
18
cairo_ft_font_options_substitute (const cairo_font_options_t *options,
				  FcPattern                  *pattern)
{
18
    if (cairo_font_options_status ((cairo_font_options_t *) options))
	return;
18
    _cairo_ft_font_options_substitute (options, pattern);
}
static cairo_font_face_t *
269
_cairo_ft_resolve_pattern (FcPattern		      *pattern,
			   const cairo_matrix_t       *font_matrix,
			   const cairo_matrix_t       *ctm,
			   const cairo_font_options_t *font_options)
{
    cairo_status_t status;
    cairo_matrix_t scale;
    FcPattern *resolved;
    cairo_ft_font_transform_t sf;
    FcResult result;
    cairo_ft_unscaled_font_t *unscaled;
    cairo_ft_options_t ft_options;
    cairo_font_face_t *font_face;
269
    scale = *ctm;
269
    scale.x0 = scale.y0 = 0;
269
    cairo_matrix_multiply (&scale,
                           font_matrix,
                           &scale);
269
    status = _compute_transform (&sf, &scale, NULL);
269
    if (unlikely (status))
	return (cairo_font_face_t *)&_cairo_font_face_nil;
269
    pattern = FcPatternDuplicate (pattern);
269
    if (pattern == NULL)
	return (cairo_font_face_t *)&_cairo_font_face_nil;
269
    if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
	font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
	goto FREE_PATTERN;
    }
269
    if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
	font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
	goto FREE_PATTERN;
    }
269
    status = _cairo_ft_font_options_substitute (font_options, pattern);
269
    if (status) {
	font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
	goto FREE_PATTERN;
    }
269
    FcDefaultSubstitute (pattern);
269
    status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
269
    if (unlikely (status)) {
	font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
	goto FREE_PATTERN;
    }
269
    if (unscaled == NULL) {
269
	resolved = FcFontMatch (NULL, pattern, &result);
269
	if (!resolved) {
	    /* We failed to find any font. Substitute twin so that the user can
	     * see something (and hopefully recognise that the font is missing)
	     * and not just receive a NO_MEMORY error during rendering.
	     */
	    font_face = _cairo_font_face_twin_create_fallback ();
	    goto FREE_PATTERN;
	}
269
	status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
269
	if (unlikely (status || unscaled == NULL)) {
	    font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
	    goto FREE_RESOLVED;
	}
    } else
	resolved = pattern;
269
    _get_pattern_ft_options (resolved, &ft_options);
269
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
269
     _cairo_ft_options_fini (&ft_options);
269
    _cairo_unscaled_font_destroy (&unscaled->base);
269
FREE_RESOLVED:
269
    if (resolved != pattern)
269
	FcPatternDestroy (resolved);
FREE_PATTERN:
269
    FcPatternDestroy (pattern);
269
    return font_face;
}
/**
 * cairo_ft_font_face_create_for_pattern:
 * @pattern: A fontconfig pattern.  Cairo makes a copy of the pattern
 * if it needs to.  You are free to modify or free @pattern after this call.
 *
 * Creates a new font face for the FreeType font backend based on a
 * fontconfig pattern. This font can then be used with
 * cairo_set_font_face() or cairo_scaled_font_create(). The
 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
 * also for the FreeType backend and can be used with functions such
 * as cairo_ft_scaled_font_lock_face().
 *
 * Font rendering options are represented both here and when you
 * call cairo_scaled_font_create(). Font options that have a representation
 * in a #FcPattern must be passed in here; to modify #FcPattern
 * appropriately to reflect the options in a #cairo_font_options_t, call
 * cairo_ft_font_options_substitute().
 *
 * The pattern's FC_FT_FACE element is inspected first and if that is set,
 * that will be the FreeType font face associated with the returned cairo
 * font face.  Otherwise the FC_FILE element is checked.  If it's set,
 * that and the value of the FC_INDEX element (defaults to zero) of @pattern
 * are used to load a font face from file.
 *
 * If both steps from the previous paragraph fails, @pattern will be passed
 * to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
 * and the resulting font pattern is used.
 *
 * If the FC_FT_FACE element of @pattern is set, the user is responsible
 * for making sure that the referenced FT_Face remains valid for the life
 * time of the returned #cairo_font_face_t.  See
 * cairo_ft_font_face_create_for_ft_face() for an example of how to couple
 * the life time of the FT_Face to that of the cairo font-face.
 *
 * Return value: a newly created #cairo_font_face_t. Free with
 *  cairo_font_face_destroy() when you are done using it.
 *
 * Since: 1.0
 **/
cairo_font_face_t *
22
cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
{
    cairo_ft_unscaled_font_t *unscaled;
    cairo_font_face_t *font_face;
    cairo_ft_options_t ft_options;
    cairo_status_t status;
22
    status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
22
    if (unlikely (status)) {
      if (status == CAIRO_STATUS_FILE_NOT_FOUND)
	return (cairo_font_face_t *) &_cairo_font_face_nil_file_not_found;
      else
	return (cairo_font_face_t *) &_cairo_font_face_nil;
    }
22
    if (unlikely (unscaled == NULL)) {
	/* Store the pattern.  We will resolve it and create unscaled
	 * font when creating scaled fonts */
1
	return _cairo_ft_font_face_create_for_pattern (pattern);
    }
21
    _get_pattern_ft_options (pattern, &ft_options);
21
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
21
    _cairo_ft_options_fini (&ft_options);
21
    _cairo_unscaled_font_destroy (&unscaled->base);
21
    return font_face;
}
#endif
/**
 * cairo_ft_font_face_create_for_ft_face:
 * @face: A FreeType face object, already opened. This must
 *   be kept around until the face's ref_count drops to
 *   zero and it is freed. Since the face may be referenced
 *   internally to Cairo, the best way to determine when it
 *   is safe to free the face is to pass a
 *   #cairo_destroy_func_t to cairo_font_face_set_user_data()
 * @load_flags: flags to pass to FT_Load_Glyph when loading
 *   glyphs from the font. These flags are OR'ed together with
 *   the flags derived from the #cairo_font_options_t passed
 *   to cairo_scaled_font_create(), so only a few values such
 *   as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
 *   are useful. You should not pass any of the flags affecting
 *   the load target, such as %FT_LOAD_TARGET_LIGHT.
 *
 * Creates a new font face for the FreeType font backend from a
 * pre-opened FreeType face. This font can then be used with
 * cairo_set_font_face() or cairo_scaled_font_create(). The
 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
 * also for the FreeType backend and can be used with functions such
 * as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
 * to the FT_Face alive in a font-cache and the exact lifetime of the reference
 * depends highly upon the exact usage pattern and is subject to external
 * factors. You must not call FT_Done_Face() before the last reference to the
 * #cairo_font_face_t has been dropped.
 *
 * As an example, below is how one might correctly couple the lifetime of
 * the FreeType face object to the #cairo_font_face_t.
 *
 * <informalexample><programlisting>
 * static const cairo_user_data_key_t key;
 *
 * font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
 * status = cairo_font_face_set_user_data (font_face, &key,
 *                                ft_face, (cairo_destroy_func_t) FT_Done_Face);
 * if (status) {
 *    cairo_font_face_destroy (font_face);
 *    FT_Done_Face (ft_face);
 *    return ERROR;
 * }
 * </programlisting></informalexample>
 *
 * Return value: a newly created #cairo_font_face_t. Free with
 *  cairo_font_face_destroy() when you are done using it.
 *
 * Since: 1.0
 **/
cairo_font_face_t *
110730
cairo_ft_font_face_create_for_ft_face (FT_Face         face,
				       int             load_flags)
{
    cairo_ft_unscaled_font_t *unscaled;
    cairo_font_face_t *font_face;
    cairo_ft_options_t ft_options;
    cairo_status_t status;
110730
    status = _cairo_ft_unscaled_font_create_from_face (face, &unscaled);
110730
    if (unlikely (status))
	return (cairo_font_face_t *)&_cairo_font_face_nil;
110730
    ft_options.load_flags = load_flags;
110730
    ft_options.synth_flags = 0;
110730
    _cairo_font_options_init_default (&ft_options.base);
110730
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
110730
    _cairo_unscaled_font_destroy (&unscaled->base);
110730
    return font_face;
}
/**
 * cairo_ft_font_face_set_synthesize:
 * @font_face: The #cairo_ft_font_face_t object to modify
 * @synth_flags: the set of synthesis options to enable
 *
 * FreeType provides the ability to synthesize different glyphs from a base
 * font, which is useful if you lack those glyphs from a true bold or oblique
 * font. See also #cairo_ft_synthesize_t.
 *
 * Since: 1.12
 **/
void
cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
				   unsigned int synth_flags)
{
    cairo_ft_font_face_t *ft;
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
	return;
    ft = (cairo_ft_font_face_t *) font_face;
    ft->ft_options.synth_flags |= synth_flags;
}
/**
 * cairo_ft_font_face_unset_synthesize:
 * @font_face: The #cairo_ft_font_face_t object to modify
 * @synth_flags: the set of synthesis options to disable
 *
 * See cairo_ft_font_face_set_synthesize().
 *
 * Since: 1.12
 **/
void
cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
				     unsigned int synth_flags)
{
    cairo_ft_font_face_t *ft;
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
	return;
    ft = (cairo_ft_font_face_t *) font_face;
    ft->ft_options.synth_flags &= ~synth_flags;
}
/**
 * cairo_ft_font_face_get_synthesize:
 * @font_face: The #cairo_ft_font_face_t object to query
 *
 * See #cairo_ft_synthesize_t.
 *
 * Returns: the current set of synthesis options.
 *
 * Since: 1.12
 **/
unsigned int
cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
{
    cairo_ft_font_face_t *ft;
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
	return 0;
    ft = (cairo_ft_font_face_t *) font_face;
    return ft->ft_options.synth_flags;
}
/**
 * cairo_ft_scaled_font_lock_face:
 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
 *   object can be created by calling cairo_scaled_font_create() on a
 *   FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
 *   cairo_ft_font_face_create_for_ft_face()).
 *
 * cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
 * backend font and scales it appropriately for the font and applies OpenType
 * font variations if applicable. You must
 * release the face with cairo_ft_scaled_font_unlock_face()
 * when you are done using it.  Since the #FT_Face object can be
 * shared between multiple #cairo_scaled_font_t objects, you must not
 * lock any other font objects until you unlock this one. A count is
 * kept of the number of times cairo_ft_scaled_font_lock_face() is
 * called. cairo_ft_scaled_font_unlock_face() must be called the same number
 * of times.
 *
 * You must be careful when using this function in a library or in a
 * threaded application, because freetype's design makes it unsafe to
 * call freetype functions simultaneously from multiple threads, (even
 * if using distinct FT_Face objects). Because of this, application
 * code that acquires an FT_Face object with this call must add its
 * own locking to protect any use of that object, (and which also must
 * protect any other calls into cairo as almost any cairo function
 * might result in a call into the freetype library).
 *
 * Return value: The #FT_Face object for @font, scaled appropriately,
 * or %NULL if @scaled_font is in an error state (see
 * cairo_scaled_font_status()) or there is insufficient memory.
 *
 * Since: 1.0
 **/
FT_Face
25
cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
{
25
    cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
    FT_Face face;
    cairo_status_t status;
25
    if (! _cairo_scaled_font_is_ft (abstract_font)) {
	_cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
	return NULL;
    }
25
    if (scaled_font->base.status)
	return NULL;
25
    face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled);
25
    if (unlikely (face == NULL)) {
	status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY);
	return NULL;
    }
25
    status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
				                &scaled_font->base.scale);
25
    if (unlikely (status)) {
	_cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
	status = _cairo_scaled_font_set_error (&scaled_font->base, status);
	return NULL;
    }
25
    cairo_ft_apply_variations (face, scaled_font);
    /* Note: We deliberately release the unscaled font's mutex here,
     * so that we are not holding a lock across two separate calls to
     * cairo function, (which would give the application some
     * opportunity for creating deadlock. This is obviously unsafe,
     * but as documented, the user must add manual locking when using
     * this function. */
25
     CAIRO_MUTEX_UNLOCK (scaled_font->unscaled->mutex);
25
    return face;
}
/**
 * cairo_ft_scaled_font_unlock_face:
 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
 *   object can be created by calling cairo_scaled_font_create() on a
 *   FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
 *   cairo_ft_font_face_create_for_ft_face()).
 *
 * Releases a face obtained with cairo_ft_scaled_font_lock_face().
 *
 * Since: 1.0
 **/
void
24
cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
{
24
    cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
24
    if (! _cairo_scaled_font_is_ft (abstract_font)) {
	_cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
	return;
    }
24
    if (scaled_font->base.status)
	return;
    /* Note: We released the unscaled font's mutex at the end of
     * cairo_ft_scaled_font_lock_face, so we have to acquire it again
     * as _cairo_ft_unscaled_font_unlock_face expects it to be held
     * when we call into it. */
24
    CAIRO_MUTEX_LOCK (scaled_font->unscaled->mutex);
24
    _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
}
static cairo_bool_t
953
_cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font)
{
    cairo_ft_scaled_font_t *ft_scaled_font;
953
    if (!_cairo_scaled_font_is_ft (scaled_font))
	return FALSE;
953
    ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
953
    if (ft_scaled_font->ft_options.load_flags & FT_LOAD_VERTICAL_LAYOUT)
12
	return TRUE;
941
    return FALSE;
}
unsigned int
_cairo_ft_scaled_font_get_load_flags (cairo_scaled_font_t *scaled_font)
{
    cairo_ft_scaled_font_t *ft_scaled_font;
    if (! _cairo_scaled_font_is_ft (scaled_font))
	return 0;
    ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
    return ft_scaled_font->ft_options.load_flags;
}
void
608
_cairo_ft_font_reset_static_data (void)
{
608
    _cairo_ft_unscaled_font_map_destroy ();
608
}