Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-ft-font.c
Line
Count
Source
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
0
#define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0)
84
249k
#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
161k
#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
5
#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
_cairo_ft_options_init_copy (cairo_ft_options_t       *options,
210
                             const cairo_ft_options_t *other)
211
6
{
212
6
    _cairo_font_options_init_copy (&options->base, &other->base);
213
6
    options->load_flags = other->load_flags;
214
6
    options->synth_flags = other->synth_flags;
215
6
}
216
217
static void
218
_cairo_ft_options_fini (cairo_ft_options_t *options)
219
207
{
220
207
    _cairo_font_options_fini (&options->base);
221
207
}
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
10
{
255
  /* Currently we don't get many (any?) useful statuses here.
256
   * Populate as needed. */
257
10
  switch (error)
258
10
  {
259
0
      case FT_Err_Ok:
260
0
    return CAIRO_STATUS_SUCCESS;
261
0
      case FT_Err_Out_Of_Memory:
262
0
    return CAIRO_STATUS_NO_MEMORY;
263
10
      default:
264
10
    return CAIRO_STATUS_FREETYPE_ERROR;
265
10
  }
266
10
}
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
_font_map_release_face_lock_held (cairo_ft_unscaled_font_map_t *font_map,
298
          cairo_ft_unscaled_font_t *unscaled)
299
0
{
300
0
    if (unscaled->face) {
301
0
  FT_Done_Face (unscaled->face);
302
0
  unscaled->face = NULL;
303
0
  unscaled->have_scale = FALSE;
304
305
0
  font_map->num_open_faces--;
306
0
    }
307
0
}
308
309
static cairo_status_t
310
_cairo_ft_unscaled_font_map_create (void)
311
2
{
312
2
    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
2
    assert (cairo_ft_unscaled_font_map == NULL);
318
319
2
    font_map = _cairo_calloc (sizeof (cairo_ft_unscaled_font_map_t));
320
2
    if (unlikely (font_map == NULL))
321
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
322
323
2
    font_map->hash_table =
324
2
  _cairo_hash_table_create (_cairo_ft_unscaled_font_keys_equal);
325
326
2
    if (unlikely (font_map->hash_table == NULL))
327
0
  goto FAIL;
328
329
2
    if (unlikely (FT_Init_FreeType (&font_map->ft_library)))
330
0
  goto FAIL;
331
332
2
    font_map->num_open_faces = 0;
333
334
2
    cairo_ft_unscaled_font_map = font_map;
335
2
    return CAIRO_STATUS_SUCCESS;
336
337
0
FAIL:
338
0
    if (font_map->hash_table)
339
0
  _cairo_hash_table_destroy (font_map->hash_table);
340
0
    free (font_map);
341
342
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
343
2
}
344
345
346
static void
347
_cairo_ft_unscaled_font_map_pluck_entry (void *entry, void *closure)
348
0
{
349
0
    cairo_ft_unscaled_font_t *unscaled = entry;
350
0
    cairo_ft_unscaled_font_map_t *font_map = closure;
351
352
0
    _cairo_hash_table_remove (font_map->hash_table,
353
0
            &unscaled->base.hash_entry);
354
355
0
    if (! unscaled->from_face)
356
0
  _font_map_release_face_lock_held (font_map, unscaled);
357
358
0
    _cairo_ft_unscaled_font_fini (unscaled);
359
0
    free (unscaled);
360
0
}
361
362
static void
363
_cairo_ft_unscaled_font_map_destroy (void)
364
0
{
365
0
    cairo_ft_unscaled_font_map_t *font_map;
366
367
0
    CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
368
0
    font_map = cairo_ft_unscaled_font_map;
369
0
    cairo_ft_unscaled_font_map = NULL;
370
0
    CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
371
372
0
    if (font_map != NULL) {
373
0
  _cairo_hash_table_foreach (font_map->hash_table,
374
0
           _cairo_ft_unscaled_font_map_pluck_entry,
375
0
           font_map);
376
0
  assert (font_map->num_open_faces == 0);
377
378
0
  FT_Done_FreeType (font_map->ft_library);
379
380
0
  _cairo_hash_table_destroy (font_map->hash_table);
381
382
0
  free (font_map);
383
0
    }
384
0
}
385
386
static cairo_ft_unscaled_font_map_t *
387
_cairo_ft_unscaled_font_map_lock (void)
388
212
{
389
212
    CAIRO_MUTEX_INITIALIZE ();
390
391
212
    CAIRO_MUTEX_LOCK (_cairo_ft_unscaled_font_map_mutex);
392
393
212
    if (unlikely (cairo_ft_unscaled_font_map == NULL)) {
394
2
  if (unlikely (_cairo_ft_unscaled_font_map_create ())) {
395
0
      CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
396
0
      return NULL;
397
0
  }
398
2
    }
399
400
212
    return cairo_ft_unscaled_font_map;
401
212
}
402
403
static void
404
_cairo_ft_unscaled_font_map_unlock (void)
405
212
{
406
212
    CAIRO_MUTEX_UNLOCK (_cairo_ft_unscaled_font_map_mutex);
407
212
}
408
409
static void
410
_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
212
{
416
212
    uintptr_t hash;
417
418
212
    key->from_face = from_face;
419
212
    key->filename = filename;
420
212
    key->id = id;
421
212
    key->face = face;
422
423
212
    hash = _cairo_hash_string (filename);
424
    /* the constants are just arbitrary primes */
425
212
    hash += ((uintptr_t) id) * 1607;
426
212
    hash += ((uintptr_t) face) * 2137;
427
428
212
    key->base.hash_entry.hash = hash;
429
212
}
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
_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
5
{
459
5
    _cairo_unscaled_font_init (&unscaled->base,
460
5
             &cairo_ft_unscaled_font_backend);
461
462
5
    unscaled->variations = NULL;
463
464
5
    if (from_face) {
465
0
  FT_MM_Var *ft_mm_var;
466
0
  unscaled->from_face = TRUE;
467
0
  _cairo_ft_unscaled_font_init_key (unscaled, TRUE, NULL, id, face);
468
469
470
0
        unscaled->have_color = FT_HAS_COLOR (face) != 0;
471
0
        unscaled->have_color_set = TRUE;
472
0
  if (FT_Get_MM_Var (face, &ft_mm_var) == 0) {
473
0
      unscaled->variations = _cairo_calloc_ab (ft_mm_var->num_axis, sizeof (FT_Fixed));
474
0
      if (unscaled->variations)
475
0
    FT_Get_Var_Design_Coordinates (face, ft_mm_var->num_axis, unscaled->variations);
476
0
      FT_Done_MM_Var (face->glyph->library, ft_mm_var);
477
0
  }
478
5
    } else {
479
5
  char *filename_copy;
480
481
5
  unscaled->from_face = FALSE;
482
5
  unscaled->face = NULL;
483
484
5
  filename_copy = strdup (filename);
485
5
  if (unlikely (filename_copy == NULL))
486
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
487
488
5
  _cairo_ft_unscaled_font_init_key (unscaled, FALSE, filename_copy, id, NULL);
489
490
5
  unscaled->have_color_set = FALSE;
491
5
    }
492
493
5
    unscaled->have_scale = FALSE;
494
5
    CAIRO_MUTEX_INIT (unscaled->mutex);
495
5
    unscaled->lock_count = 0;
496
497
5
    unscaled->faces = NULL;
498
499
5
    return CAIRO_STATUS_SUCCESS;
500
5
}
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
_cairo_ft_unscaled_font_fini (cairo_ft_unscaled_font_t *unscaled)
515
0
{
516
0
    assert (unscaled->face == NULL);
517
518
0
    free (unscaled->filename);
519
0
    unscaled->filename = NULL;
520
521
0
    free (unscaled->variations);
522
523
0
    CAIRO_MUTEX_FINI (unscaled->mutex);
524
0
}
525
526
static int
527
_cairo_ft_unscaled_font_keys_equal (const void *key_a,
528
            const void *key_b)
529
202
{
530
202
    const cairo_ft_unscaled_font_t *unscaled_a = key_a;
531
202
    const cairo_ft_unscaled_font_t *unscaled_b = key_b;
532
533
202
    if (unscaled_a->id == unscaled_b->id &&
534
202
  unscaled_a->from_face == unscaled_b->from_face)
535
202
     {
536
202
        if (unscaled_a->from_face)
537
0
      return unscaled_a->face == unscaled_b->face;
538
539
202
  if (unscaled_a->filename == NULL && unscaled_b->filename == NULL)
540
0
      return TRUE;
541
202
  else if (unscaled_a->filename == NULL || unscaled_b->filename == NULL)
542
0
      return FALSE;
543
202
  else
544
202
      return (strcmp (unscaled_a->filename, unscaled_b->filename) == 0);
545
202
    }
546
547
0
    return FALSE;
548
202
}
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
_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
207
{
560
207
    cairo_ft_unscaled_font_t key, *unscaled;
561
207
    cairo_ft_unscaled_font_map_t *font_map;
562
207
    cairo_status_t status;
563
564
207
    font_map = _cairo_ft_unscaled_font_map_lock ();
565
207
    if (unlikely (font_map == NULL))
566
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
567
568
207
    _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
207
    unscaled = _cairo_hash_table_lookup (font_map->hash_table,
572
207
           &key.base.hash_entry);
573
207
    if (unscaled != NULL) {
574
202
  _cairo_unscaled_font_reference (&unscaled->base);
575
202
  goto DONE;
576
202
    }
577
578
    /* Otherwise create it and insert into hash table. */
579
5
    unscaled = _cairo_calloc (sizeof (cairo_ft_unscaled_font_t));
580
5
    if (unlikely (unscaled == NULL)) {
581
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
582
0
  goto UNWIND_FONT_MAP_LOCK;
583
0
    }
584
585
5
    status = _cairo_ft_unscaled_font_init (unscaled, from_face, filename, id, font_face);
586
5
    if (unlikely (status))
587
0
  goto UNWIND_UNSCALED_MALLOC;
588
589
5
    assert (unscaled->base.hash_entry.hash == key.base.hash_entry.hash);
590
5
    status = _cairo_hash_table_insert (font_map->hash_table,
591
5
               &unscaled->base.hash_entry);
592
5
    if (unlikely (status))
593
0
  goto UNWIND_UNSCALED_FONT_INIT;
594
595
207
DONE:
596
207
    _cairo_ft_unscaled_font_map_unlock ();
597
207
    *out = unscaled;
598
207
    return CAIRO_STATUS_SUCCESS;
599
600
0
UNWIND_UNSCALED_FONT_INIT:
601
0
    _cairo_ft_unscaled_font_fini (unscaled);
602
0
UNWIND_UNSCALED_MALLOC:
603
0
    free (unscaled);
604
0
UNWIND_FONT_MAP_LOCK:
605
0
    _cairo_ft_unscaled_font_map_unlock ();
606
0
    return status;
607
0
}
608
609
610
#if CAIRO_HAS_FC_FONT
611
static cairo_status_t
612
_cairo_ft_unscaled_font_create_for_pattern (FcPattern *pattern,
613
              cairo_ft_unscaled_font_t **out)
614
207
{
615
207
    FT_Face font_face = NULL;
616
207
    char *filename = NULL;
617
207
    int id = 0;
618
207
    FcResult ret;
619
620
207
    ret = FcPatternGetFTFace (pattern, FC_FT_FACE, 0, &font_face);
621
207
    if (ret == FcResultMatch)
622
0
  goto DONE;
623
207
    if (ret == FcResultOutOfMemory)
624
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
625
626
207
    ret = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **) &filename);
627
207
    if (ret == FcResultOutOfMemory)
628
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
629
207
    if (ret == FcResultMatch) {
630
207
  if (access (filename, R_OK) == 0) {
631
      /* If FC_INDEX is not set, we just use 0 */
632
207
      ret = FcPatternGetInteger (pattern, FC_INDEX, 0, &id);
633
207
      if (ret == FcResultOutOfMemory)
634
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
635
636
207
      goto DONE;
637
207
  } else
638
0
      return _cairo_error (CAIRO_STATUS_FILE_NOT_FOUND);
639
207
    }
640
641
    /* The pattern contains neither a face nor a filename, resolve it later. */
642
0
    *out = NULL;
643
0
    return CAIRO_STATUS_SUCCESS;
644
645
207
DONE:
646
207
    return _cairo_ft_unscaled_font_create_internal (font_face != NULL,
647
207
                filename, id, font_face,
648
207
                out);
649
207
}
650
#endif
651
652
static cairo_status_t
653
_cairo_ft_unscaled_font_create_from_face (FT_Face face,
654
            cairo_ft_unscaled_font_t **out)
655
0
{
656
0
    return _cairo_ft_unscaled_font_create_internal (TRUE, NULL, face->face_index, face, out);
657
0
}
658
659
static cairo_bool_t
660
_cairo_ft_unscaled_font_destroy (void *abstract_font)
661
0
{
662
0
    cairo_ft_unscaled_font_t *unscaled  = abstract_font;
663
0
    cairo_ft_unscaled_font_map_t *font_map;
664
665
0
    font_map = _cairo_ft_unscaled_font_map_lock ();
666
    /* All created objects must have been mapped in the font map. */
667
0
    assert (font_map != NULL);
668
669
0
    if (! _cairo_reference_count_dec_and_test (&unscaled->base.ref_count)) {
670
  /* somebody recreated the font whilst we waited for the lock */
671
0
  _cairo_ft_unscaled_font_map_unlock ();
672
0
  return FALSE;
673
0
    }
674
675
0
    _cairo_hash_table_remove (font_map->hash_table,
676
0
            &unscaled->base.hash_entry);
677
678
0
    if (unscaled->from_face) {
679
  /* See comments in _ft_font_face_destroy about the "zombie" state
680
   * for a _ft_font_face.
681
   */
682
0
  if (unscaled->faces && unscaled->faces->unscaled == NULL) {
683
0
      assert (unscaled->faces->next == NULL);
684
0
      cairo_font_face_destroy (&unscaled->faces->base);
685
0
  }
686
0
    } else {
687
0
  _font_map_release_face_lock_held (font_map, unscaled);
688
0
    }
689
0
    unscaled->face = NULL;
690
691
0
    _cairo_ft_unscaled_font_map_unlock ();
692
693
0
    _cairo_ft_unscaled_font_fini (unscaled);
694
0
    return TRUE;
695
0
}
696
697
static cairo_bool_t
698
_has_unlocked_face (const void *entry)
699
0
{
700
0
    const cairo_ft_unscaled_font_t *unscaled = entry;
701
702
0
    return (!unscaled->from_face && unscaled->lock_count == 0 && unscaled->face);
703
0
}
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
_cairo_ft_unscaled_font_lock_face (cairo_ft_unscaled_font_t *unscaled)
713
38.9k
{
714
38.9k
    cairo_ft_unscaled_font_map_t *font_map;
715
38.9k
    FT_Face face = NULL;
716
38.9k
    FT_Error error;
717
718
38.9k
    CAIRO_MUTEX_LOCK (unscaled->mutex);
719
38.9k
    unscaled->lock_count++;
720
721
38.9k
    if (unscaled->face)
722
38.9k
  return unscaled->face;
723
724
    /* If this unscaled font was created from an FT_Face then we just
725
     * returned it above. */
726
38.9k
    assert (!unscaled->from_face);
727
728
5
    font_map = _cairo_ft_unscaled_font_map_lock ();
729
5
    {
730
5
  assert (font_map != NULL);
731
732
5
  while (font_map->num_open_faces >= MAX_OPEN_FACES)
733
0
  {
734
0
      cairo_ft_unscaled_font_t *entry;
735
736
0
      entry = _cairo_hash_table_random_entry (font_map->hash_table,
737
0
                _has_unlocked_face);
738
0
      if (entry == NULL)
739
0
    break;
740
741
0
      _font_map_release_face_lock_held (font_map, entry);
742
0
  }
743
5
    }
744
5
    _cairo_ft_unscaled_font_map_unlock ();
745
746
5
    error = FT_New_Face (font_map->ft_library,
747
5
       unscaled->filename,
748
5
       unscaled->id,
749
5
       &face);
750
5
    if (error)
751
0
    {
752
0
  unscaled->lock_count--;
753
0
  CAIRO_MUTEX_UNLOCK (unscaled->mutex);
754
0
  _cairo_error_throw (_cairo_ft_to_cairo_error (error));
755
0
  return NULL;
756
0
    }
757
758
5
    unscaled->face = face;
759
760
5
    unscaled->have_color = FT_HAS_COLOR (face) != 0;
761
5
    unscaled->have_color_set = TRUE;
762
763
5
    font_map->num_open_faces++;
764
765
5
    return face;
766
5
}
767
768
769
/* Unlock unscaled font locked with _cairo_ft_unscaled_font_lock_face
770
 */
771
static void
772
_cairo_ft_unscaled_font_unlock_face (cairo_ft_unscaled_font_t *unscaled)
773
38.9k
{
774
38.9k
    assert (unscaled->lock_count > 0);
775
776
38.9k
    unscaled->lock_count--;
777
778
38.9k
    CAIRO_MUTEX_UNLOCK (unscaled->mutex);
779
38.9k
}
780
781
782
static cairo_status_t
783
_compute_transform (cairo_ft_font_transform_t *sf,
784
        cairo_matrix_t      *scale,
785
        cairo_ft_unscaled_font_t *unscaled)
786
62.3k
{
787
62.3k
    cairo_status_t status;
788
62.3k
    double x_scale, y_scale;
789
62.3k
    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
62.3k
    status = _cairo_matrix_compute_basis_scale_factors (scale,
799
62.3k
              &x_scale, &y_scale,
800
62.3k
              1);
801
62.3k
    if (unlikely (status))
802
0
  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
62.3k
    if (x_scale < 1.0)
809
150
      x_scale = 1.0;
810
62.3k
    if (y_scale < 1.0)
811
154
      y_scale = 1.0;
812
813
62.3k
    if (unscaled && (unscaled->face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) {
814
0
  double min_distance = DBL_MAX;
815
0
  cairo_bool_t magnify = TRUE;
816
0
  int i;
817
0
  double best_x_size = 0;
818
0
  double best_y_size = 0;
819
820
0
  for (i = 0; i < unscaled->face->num_fixed_sizes; i++) {
821
0
      double x_size = unscaled->face->available_sizes[i].x_ppem / 64.;
822
0
      double y_size = unscaled->face->available_sizes[i].y_ppem / 64.;
823
0
      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
0
      if ((magnify && distance >= 0) || fabs (distance) <= min_distance) {
833
0
    magnify = distance < 0;
834
0
    min_distance = fabs (distance);
835
0
    best_x_size = x_size;
836
0
    best_y_size = y_size;
837
0
      }
838
0
  }
839
840
0
  x_scale = best_x_size;
841
0
  y_scale = best_y_size;
842
0
    }
843
844
62.3k
    sf->x_scale = x_scale;
845
62.3k
    sf->y_scale = y_scale;
846
847
62.3k
    cairo_matrix_scale (&normalized, 1.0 / x_scale, 1.0 / y_scale);
848
849
62.3k
    _cairo_matrix_get_affine (&normalized,
850
62.3k
            &sf->shape[0][0], &sf->shape[0][1],
851
62.3k
            &sf->shape[1][0], &sf->shape[1][1],
852
62.3k
            NULL, NULL);
853
854
62.3k
    return CAIRO_STATUS_SUCCESS;
855
62.3k
}
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
_cairo_ft_unscaled_font_set_scale (cairo_ft_unscaled_font_t *unscaled,
862
           cairo_matrix_t       *scale)
863
88.9k
{
864
88.9k
    cairo_status_t status;
865
88.9k
    cairo_ft_font_transform_t sf;
866
88.9k
    FT_Matrix mat;
867
88.9k
    FT_Error error;
868
869
88.9k
    assert (unscaled->face != NULL);
870
871
88.9k
    if (unscaled->have_scale &&
872
88.9k
  scale->xx == unscaled->current_scale.xx &&
873
26.6k
  scale->yx == unscaled->current_scale.yx &&
874
26.6k
  scale->xy == unscaled->current_scale.xy &&
875
26.6k
  scale->yy == unscaled->current_scale.yy)
876
26.6k
  return CAIRO_STATUS_SUCCESS;
877
878
62.3k
    unscaled->have_scale = TRUE;
879
62.3k
    unscaled->current_scale = *scale;
880
881
62.3k
    status = _compute_transform (&sf, scale, unscaled);
882
62.3k
    if (unlikely (status))
883
0
  return status;
884
885
62.3k
    unscaled->x_scale = sf.x_scale;
886
62.3k
    unscaled->y_scale = sf.y_scale;
887
888
62.3k
    mat.xx = DOUBLE_TO_16_16(sf.shape[0][0]);
889
62.3k
    mat.yx = - DOUBLE_TO_16_16(sf.shape[0][1]);
890
62.3k
    mat.xy = - DOUBLE_TO_16_16(sf.shape[1][0]);
891
62.3k
    mat.yy = DOUBLE_TO_16_16(sf.shape[1][1]);
892
893
62.3k
    unscaled->have_shape = (mat.xx != 0x10000 ||
894
50.1k
          mat.yx != 0x00000 ||
895
50.1k
          mat.xy != 0x00000 ||
896
50.1k
          mat.yy != 0x10000);
897
898
62.3k
    unscaled->Current_Shape = mat;
899
62.3k
    cairo_matrix_init (&unscaled->current_shape,
900
62.3k
           sf.shape[0][0], sf.shape[0][1],
901
62.3k
           sf.shape[1][0], sf.shape[1][1],
902
62.3k
           0.0, 0.0);
903
904
62.3k
    FT_Set_Transform(unscaled->face, &mat, NULL);
905
906
62.3k
    error = FT_Set_Char_Size (unscaled->face,
907
62.3k
            sf.x_scale * 64.0 + .5,
908
62.3k
            sf.y_scale * 64.0 + .5,
909
62.3k
            0, 0);
910
62.3k
    if (error)
911
0
      return _cairo_error (_cairo_ft_to_cairo_error (error));
912
913
62.3k
    return CAIRO_STATUS_SUCCESS;
914
62.3k
}
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
_compute_xrender_bitmap_size(FT_Bitmap      *target,
941
           FT_GlyphSlot    slot,
942
           FT_Render_Mode  mode)
943
25.1k
{
944
25.1k
    FT_Bitmap *ftbit;
945
25.1k
    int width, height, pitch;
946
947
25.1k
    if (slot->format != FT_GLYPH_FORMAT_BITMAP)
948
0
  return -1;
949
950
    /* compute the size of the final bitmap */
951
25.1k
    ftbit = &slot->bitmap;
952
953
25.1k
    width = ftbit->width;
954
25.1k
    height = ftbit->rows;
955
25.1k
    pitch = (width + 3) & ~3;
956
957
25.1k
    switch (ftbit->pixel_mode) {
958
0
    case FT_PIXEL_MODE_MONO:
959
0
  if (mode == FT_RENDER_MODE_MONO) {
960
0
      pitch = (((width + 31) & ~31) >> 3);
961
0
      break;
962
0
  }
963
  /* fall-through */
964
965
25.1k
    case FT_PIXEL_MODE_GRAY:
966
25.1k
  if (mode == FT_RENDER_MODE_LCD ||
967
25.1k
      mode == FT_RENDER_MODE_LCD_V)
968
0
  {
969
      /* each pixel is replicated into a 32-bit ARGB value */
970
0
      pitch = width * 4;
971
0
  }
972
25.1k
  break;
973
974
0
    case FT_PIXEL_MODE_LCD:
975
0
  if (mode != FT_RENDER_MODE_LCD)
976
0
      return -1;
977
978
  /* horz pixel triplets are packed into 32-bit ARGB values */
979
0
  width /= 3;
980
0
  pitch = width * 4;
981
0
  break;
982
983
0
    case FT_PIXEL_MODE_LCD_V:
984
0
  if (mode != FT_RENDER_MODE_LCD_V)
985
0
      return -1;
986
987
  /* vert pixel triplets are packed into 32-bit ARGB values */
988
0
  height /= 3;
989
0
  pitch = width * 4;
990
0
  break;
991
992
0
    case FT_PIXEL_MODE_BGRA:
993
  /* each pixel is replicated into a 32-bit ARGB value */
994
0
  pitch = width * 4;
995
0
  break;
996
997
0
    default:  /* unsupported source format */
998
0
  return -1;
999
25.1k
    }
1000
1001
25.1k
    target->width = width;
1002
25.1k
    target->rows = height;
1003
25.1k
    target->pitch = pitch;
1004
25.1k
    target->buffer = NULL;
1005
1006
25.1k
    return pitch * height;
1007
25.1k
}
1008
1009
/* this functions converts the glyph bitmap found in a FT_GlyphSlot
1010
 * into a different format (see _compute_xrender_bitmap_size)
1011
 *
1012
 * you should call this function after _compute_xrender_bitmap_size
1013
 *
1014
 * target :: target bitmap descriptor. Note that its 'buffer' pointer
1015
 *           must point to memory allocated by the caller
1016
 *
1017
 * slot   :: the glyph slot containing the source bitmap
1018
 *
1019
 * mode   :: the requested final rendering mode
1020
 *
1021
 * bgr    :: boolean, set if BGR or VBGR pixel ordering is needed
1022
 */
1023
static void
1024
_fill_xrender_bitmap(FT_Bitmap      *target,
1025
         FT_GlyphSlot    slot,
1026
         FT_Render_Mode  mode,
1027
         int             bgr)
1028
25.1k
{
1029
25.1k
    FT_Bitmap *ftbit = &slot->bitmap;
1030
25.1k
    unsigned char *srcLine = ftbit->buffer;
1031
25.1k
    unsigned char *dstLine = target->buffer;
1032
25.1k
    int src_pitch = ftbit->pitch;
1033
25.1k
    int width = target->width;
1034
25.1k
    int height = target->rows;
1035
25.1k
    int pitch = target->pitch;
1036
25.1k
    int subpixel;
1037
25.1k
    int h;
1038
1039
25.1k
    subpixel = (mode == FT_RENDER_MODE_LCD ||
1040
25.1k
    mode == FT_RENDER_MODE_LCD_V);
1041
1042
25.1k
    if (src_pitch < 0)
1043
0
  srcLine -= src_pitch * (ftbit->rows - 1);
1044
1045
25.1k
    target->pixel_mode = ftbit->pixel_mode;
1046
1047
25.1k
    switch (ftbit->pixel_mode) {
1048
0
    case FT_PIXEL_MODE_MONO:
1049
0
  if (subpixel) {
1050
      /* convert mono to ARGB32 values */
1051
1052
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1053
0
    int x;
1054
1055
0
    for (x = 0; x < width; x++) {
1056
0
        if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
1057
0
      ((unsigned int *) dstLine)[x] = 0xffffffffU;
1058
0
    }
1059
0
      }
1060
0
      target->pixel_mode = FT_PIXEL_MODE_LCD;
1061
1062
0
  } else if (mode == FT_RENDER_MODE_NORMAL) {
1063
      /* convert mono to 8-bit gray */
1064
1065
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1066
0
    int x;
1067
1068
0
    for (x = 0; x < width; x++) {
1069
0
        if (srcLine[(x >> 3)] & (0x80 >> (x & 7)))
1070
0
      dstLine[x] = 0xff;
1071
0
    }
1072
0
      }
1073
0
      target->pixel_mode = FT_PIXEL_MODE_GRAY;
1074
1075
0
  } else {
1076
      /* copy mono to mono */
1077
1078
0
      int  bytes = (width + 7) >> 3;
1079
1080
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1081
0
    memcpy (dstLine, srcLine, bytes);
1082
0
  }
1083
0
  break;
1084
1085
25.1k
    case FT_PIXEL_MODE_GRAY:
1086
25.1k
  if (subpixel) {
1087
      /* convert gray to ARGB32 values */
1088
1089
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1090
0
    int x;
1091
0
    unsigned int *dst = (unsigned int *) dstLine;
1092
1093
0
    for (x = 0; x < width; x++) {
1094
0
        unsigned int pix = srcLine[x];
1095
1096
0
        pix |= (pix << 8);
1097
0
        pix |= (pix << 16);
1098
1099
0
        dst[x] = pix;
1100
0
    }
1101
0
      }
1102
0
      target->pixel_mode = FT_PIXEL_MODE_LCD;
1103
25.1k
        } else {
1104
            /* copy gray into gray */
1105
1106
3.02M
            for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1107
3.00M
                memcpy (dstLine, srcLine, width);
1108
25.1k
        }
1109
25.1k
        break;
1110
1111
0
    case FT_PIXEL_MODE_LCD:
1112
0
  if (!bgr) {
1113
      /* convert horizontal RGB into ARGB32 */
1114
1115
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1116
0
    int x;
1117
0
    unsigned char *src = srcLine;
1118
0
    unsigned int *dst = (unsigned int *) dstLine;
1119
1120
0
    for (x = 0; x < width; x++, src += 3) {
1121
0
        unsigned int  pix;
1122
1123
0
        pix = ((unsigned int)src[0] << 16) |
1124
0
        ((unsigned int)src[1] <<  8) |
1125
0
        ((unsigned int)src[2]      ) |
1126
0
        ((unsigned int)src[1] << 24) ;
1127
1128
0
        dst[x] = pix;
1129
0
    }
1130
0
      }
1131
0
  } else {
1132
      /* convert horizontal BGR into ARGB32 */
1133
1134
0
      for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch) {
1135
1136
0
    int x;
1137
0
    unsigned char *src = srcLine;
1138
0
    unsigned int *dst = (unsigned int *) dstLine;
1139
1140
0
    for (x = 0; x < width; x++, src += 3) {
1141
0
        unsigned int  pix;
1142
1143
0
        pix = ((unsigned int)src[2] << 16) |
1144
0
        ((unsigned int)src[1] <<  8) |
1145
0
        ((unsigned int)src[0]      ) |
1146
0
        ((unsigned int)src[1] << 24) ;
1147
1148
0
        dst[x] = pix;
1149
0
    }
1150
0
      }
1151
0
  }
1152
0
  break;
1153
1154
0
    case FT_PIXEL_MODE_LCD_V:
1155
  /* convert vertical RGB into ARGB32 */
1156
0
  if (!bgr) {
1157
1158
0
      for (h = height; h > 0; h--, srcLine += 3 * src_pitch, dstLine += pitch) {
1159
0
    int x;
1160
0
    unsigned char* src = srcLine;
1161
0
    unsigned int*  dst = (unsigned int *) dstLine;
1162
1163
0
    for (x = 0; x < width; x++, src += 1) {
1164
0
        unsigned int pix;
1165
0
        pix = ((unsigned int)src[0]           << 16) |
1166
0
        ((unsigned int)src[src_pitch]   <<  8) |
1167
0
        ((unsigned int)src[src_pitch*2]      ) |
1168
0
        ((unsigned int)src[src_pitch]   << 24) ;
1169
0
        dst[x] = pix;
1170
0
    }
1171
0
      }
1172
0
  } else {
1173
1174
0
      for (h = height; h > 0; h--, srcLine += 3*src_pitch, dstLine += pitch) {
1175
0
    int x;
1176
0
    unsigned char *src = srcLine;
1177
0
    unsigned int *dst = (unsigned int *) dstLine;
1178
1179
0
    for (x = 0; x < width; x++, src += 1) {
1180
0
        unsigned int  pix;
1181
1182
0
        pix = ((unsigned int)src[src_pitch * 2] << 16) |
1183
0
        ((unsigned int)src[src_pitch]     <<  8) |
1184
0
        ((unsigned int)src[0]                  ) |
1185
0
        ((unsigned int)src[src_pitch]     << 24) ;
1186
1187
0
        dst[x] = pix;
1188
0
    }
1189
0
      }
1190
0
  }
1191
0
  break;
1192
1193
0
    case FT_PIXEL_MODE_BGRA:
1194
0
  for (h = height; h > 0; h--, srcLine += src_pitch, dstLine += pitch)
1195
0
      memcpy (dstLine, srcLine, (size_t)width * 4);
1196
0
  break;
1197
1198
0
    default:
1199
0
  assert (0);
1200
25.1k
    }
1201
25.1k
}
1202
1203
1204
/* Fills in val->image with an image surface created from @bitmap
1205
 */
1206
static cairo_status_t
1207
_get_bitmap_surface (FT_Bitmap         *bitmap,
1208
         FT_Library         library,
1209
         cairo_bool_t       own_buffer,
1210
         cairo_font_options_t    *font_options,
1211
         cairo_image_surface_t  **surface)
1212
25.1k
{
1213
25.1k
    unsigned int width, height;
1214
25.1k
    unsigned char *data;
1215
25.1k
    int format = CAIRO_FORMAT_A8;
1216
25.1k
    int stride;
1217
25.1k
    cairo_image_surface_t *image;
1218
25.1k
    cairo_bool_t component_alpha = FALSE;
1219
1220
25.1k
    width = bitmap->width;
1221
25.1k
    height = bitmap->rows;
1222
1223
25.1k
    if (width == 0 || height == 0) {
1224
0
  *surface = (cairo_image_surface_t *)
1225
0
      cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1226
0
  return (*surface)->base.status;
1227
0
    }
1228
1229
25.1k
    switch (bitmap->pixel_mode) {
1230
0
    case FT_PIXEL_MODE_MONO:
1231
0
  stride = (((width + 31) & ~31) >> 3);
1232
0
  if (own_buffer) {
1233
0
      data = bitmap->buffer;
1234
0
      assert (stride == bitmap->pitch);
1235
0
  } else {
1236
0
      data = _cairo_malloc_ab (height, stride);
1237
0
      if (!data)
1238
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1239
1240
0
      if (stride == bitmap->pitch) {
1241
0
    memcpy (data, bitmap->buffer, (size_t)stride * height);
1242
0
      } else {
1243
0
    int i;
1244
0
    unsigned char *source, *dest;
1245
1246
0
    source = bitmap->buffer;
1247
0
    dest = data;
1248
0
    for (i = height; i; i--) {
1249
0
        memcpy (dest, source, bitmap->pitch);
1250
0
        memset (dest + bitmap->pitch, '\0', stride - bitmap->pitch);
1251
1252
0
        source += bitmap->pitch;
1253
0
        dest += stride;
1254
0
    }
1255
0
      }
1256
0
  }
1257
1258
0
#ifndef WORDS_BIGENDIAN
1259
0
  {
1260
0
      uint8_t *d = data;
1261
0
      int count = stride * height;
1262
1263
0
      while (count--) {
1264
0
    *d = CAIRO_BITSWAP8 (*d);
1265
0
    d++;
1266
0
      }
1267
0
  }
1268
0
#endif
1269
0
  format = CAIRO_FORMAT_A1;
1270
0
  break;
1271
1272
0
    case FT_PIXEL_MODE_LCD:
1273
0
    case FT_PIXEL_MODE_LCD_V:
1274
25.1k
    case FT_PIXEL_MODE_GRAY:
1275
25.1k
  if (font_options->antialias != CAIRO_ANTIALIAS_SUBPIXEL ||
1276
0
      bitmap->pixel_mode == FT_PIXEL_MODE_GRAY)
1277
25.1k
  {
1278
25.1k
      stride = bitmap->pitch;
1279
1280
      /* We don't support stride not multiple of 4. */
1281
25.1k
      if (stride & 3)
1282
0
      {
1283
0
    assert (!own_buffer);
1284
0
    goto convert;
1285
0
      }
1286
1287
25.1k
      if (own_buffer) {
1288
25.1k
    data = bitmap->buffer;
1289
25.1k
      } else {
1290
0
    data = _cairo_malloc_ab (height, stride);
1291
0
    if (!data)
1292
0
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1293
1294
0
    memcpy (data, bitmap->buffer, (size_t)stride * height);
1295
0
      }
1296
1297
25.1k
      format = CAIRO_FORMAT_A8;
1298
25.1k
  } else {
1299
0
      data = bitmap->buffer;
1300
0
      stride = bitmap->pitch;
1301
0
      format = CAIRO_FORMAT_ARGB32;
1302
0
      component_alpha = TRUE;
1303
0
  }
1304
25.1k
  break;
1305
25.1k
    case FT_PIXEL_MODE_BGRA:
1306
0
  stride = width * 4;
1307
0
  if (own_buffer) {
1308
0
      data = bitmap->buffer;
1309
0
  } else {
1310
0
      data = _cairo_malloc_ab (height, stride);
1311
0
      if (!data)
1312
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1313
1314
0
      memcpy (data, bitmap->buffer, (size_t)stride * height);
1315
0
  }
1316
1317
0
  if (!_cairo_is_little_endian ())
1318
0
  {
1319
      /* Byteswap. */
1320
0
      unsigned int i, count = height * width;
1321
0
      uint32_t *p = (uint32_t *) data;
1322
0
      for (i = 0; i < count; i++)
1323
0
    p[i] = bswap_32 (p[i]);
1324
0
  }
1325
0
  format = CAIRO_FORMAT_ARGB32;
1326
0
  break;
1327
0
    case FT_PIXEL_MODE_GRAY2:
1328
0
    case FT_PIXEL_MODE_GRAY4:
1329
0
    convert:
1330
0
  if (!own_buffer && library)
1331
0
  {
1332
      /* This is pretty much the only case that we can get in here. */
1333
      /* Convert to 8bit grayscale. */
1334
1335
0
      FT_Bitmap  tmp;
1336
0
      FT_Int     align;
1337
0
      FT_Error   error;
1338
1339
0
      format = CAIRO_FORMAT_A8;
1340
1341
0
      align = cairo_format_stride_for_width (format, bitmap->width);
1342
1343
0
      FT_Bitmap_New( &tmp );
1344
1345
0
      error = FT_Bitmap_Convert( library, bitmap, &tmp, align );
1346
0
      if (error)
1347
0
    return _cairo_error (_cairo_ft_to_cairo_error (error));
1348
1349
0
      FT_Bitmap_Done( library, bitmap );
1350
0
      *bitmap = tmp;
1351
1352
0
      stride = bitmap->pitch;
1353
0
      data = _cairo_malloc_ab (height, stride);
1354
0
      if (!data)
1355
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1356
1357
0
      if (bitmap->num_grays != 256)
1358
0
      {
1359
0
        unsigned int x, y;
1360
0
        unsigned int mul = 255 / (bitmap->num_grays - 1);
1361
0
        FT_Byte *p = bitmap->buffer;
1362
0
        for (y = 0; y < height; y++) {
1363
0
          for (x = 0; x < width; x++)
1364
0
      p[x] *= mul;
1365
0
    p += bitmap->pitch;
1366
0
        }
1367
0
      }
1368
1369
0
      memcpy (data, bitmap->buffer, (size_t)stride * height);
1370
0
      break;
1371
0
  }
1372
  /* fall through */
1373
  /* These could be triggered by very rare types of TrueType fonts */
1374
0
    default:
1375
0
  if (own_buffer)
1376
0
      free (bitmap->buffer);
1377
0
  return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
1378
25.1k
    }
1379
1380
    /* XXX */
1381
25.1k
    *surface = image = (cairo_image_surface_t *)
1382
25.1k
  cairo_image_surface_create_for_data (data,
1383
25.1k
               format,
1384
25.1k
               width, height, stride);
1385
25.1k
    if (image->base.status) {
1386
0
  free (data);
1387
0
  return (*surface)->base.status;
1388
0
    }
1389
1390
25.1k
    if (component_alpha)
1391
0
  pixman_image_set_component_alpha (image->pixman_image, TRUE);
1392
1393
25.1k
    _cairo_image_surface_assume_ownership_of_data (image);
1394
1395
25.1k
    _cairo_debug_check_image_surface_is_defined (&image->base);
1396
1397
25.1k
    return CAIRO_STATUS_SUCCESS;
1398
25.1k
}
1399
1400
/* Converts an outline FT_GlyphSlot into an image
1401
 *
1402
 * This could go through _render_glyph_bitmap as well, letting
1403
 * FreeType convert the outline to a bitmap, but doing it ourselves
1404
 * has two minor advantages: first, we save a copy of the bitmap
1405
 * buffer: we can directly use the buffer that FreeType renders
1406
 * into.
1407
 *
1408
 * Second, it may help when we add support for subpixel
1409
 * rendering: the Xft code does it this way. (Keith thinks that
1410
 * it may also be possible to get the subpixel rendering with
1411
 * FT_Render_Glyph: something worth looking into in more detail
1412
 * when we add subpixel support. If so, we may want to eliminate
1413
 * this version of the code path entirely.
1414
 */
1415
static cairo_status_t
1416
_render_glyph_outline (FT_Face                    face,
1417
           cairo_font_options_t  *font_options,
1418
           cairo_image_surface_t  **surface)
1419
25.6k
{
1420
25.6k
    cairo_subpixel_order_t rgba = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1421
25.6k
    int lcd_filter = FT_LCD_FILTER_DEFAULT;
1422
25.6k
    FT_GlyphSlot glyphslot = face->glyph;
1423
25.6k
    FT_Outline *outline = &glyphslot->outline;
1424
25.6k
    FT_Bitmap bitmap;
1425
25.6k
    FT_BBox cbox;
1426
25.6k
    unsigned int width, height;
1427
25.6k
    cairo_status_t status;
1428
25.6k
    FT_Error error;
1429
25.6k
    FT_Library library = glyphslot->library;
1430
25.6k
    FT_Render_Mode render_mode = FT_RENDER_MODE_NORMAL;
1431
1432
25.6k
    switch (font_options->antialias) {
1433
0
    case CAIRO_ANTIALIAS_NONE:
1434
0
  render_mode = FT_RENDER_MODE_MONO;
1435
0
  break;
1436
1437
0
    case CAIRO_ANTIALIAS_SUBPIXEL:
1438
0
    case CAIRO_ANTIALIAS_BEST:
1439
0
  switch (font_options->subpixel_order) {
1440
0
      case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1441
0
      case CAIRO_SUBPIXEL_ORDER_RGB:
1442
0
      case CAIRO_SUBPIXEL_ORDER_BGR:
1443
0
    render_mode = FT_RENDER_MODE_LCD;
1444
0
    break;
1445
1446
0
      case CAIRO_SUBPIXEL_ORDER_VRGB:
1447
0
      case CAIRO_SUBPIXEL_ORDER_VBGR:
1448
0
    render_mode = FT_RENDER_MODE_LCD_V;
1449
0
    break;
1450
0
  }
1451
1452
0
  switch (font_options->lcd_filter) {
1453
0
  case CAIRO_LCD_FILTER_NONE:
1454
0
      lcd_filter = FT_LCD_FILTER_NONE;
1455
0
      break;
1456
0
  case CAIRO_LCD_FILTER_INTRA_PIXEL:
1457
0
      lcd_filter = FT_LCD_FILTER_LEGACY;
1458
0
      break;
1459
0
  case CAIRO_LCD_FILTER_FIR3:
1460
0
      lcd_filter = FT_LCD_FILTER_LIGHT;
1461
0
      break;
1462
0
  case CAIRO_LCD_FILTER_DEFAULT:
1463
0
  case CAIRO_LCD_FILTER_FIR5:
1464
0
      lcd_filter = FT_LCD_FILTER_DEFAULT;
1465
0
      break;
1466
0
  }
1467
1468
0
  break;
1469
1470
0
    case CAIRO_ANTIALIAS_DEFAULT:
1471
25.6k
    case CAIRO_ANTIALIAS_GRAY:
1472
25.6k
    case CAIRO_ANTIALIAS_GOOD:
1473
25.6k
    case CAIRO_ANTIALIAS_FAST:
1474
25.6k
  render_mode = FT_RENDER_MODE_NORMAL;
1475
25.6k
    }
1476
1477
25.6k
    FT_Outline_Get_CBox (outline, &cbox);
1478
1479
25.6k
    cbox.xMin &= -64;
1480
25.6k
    cbox.yMin &= -64;
1481
25.6k
    cbox.xMax = (cbox.xMax + 63) & -64;
1482
25.6k
    cbox.yMax = (cbox.yMax + 63) & -64;
1483
1484
25.6k
    width = (unsigned int) ((cbox.xMax - cbox.xMin) >> 6);
1485
25.6k
    height = (unsigned int) ((cbox.yMax - cbox.yMin) >> 6);
1486
1487
25.6k
    if (width * height == 0) {
1488
465
  cairo_format_t format;
1489
  /* Looks like fb handles zero-sized images just fine */
1490
465
  switch (render_mode) {
1491
0
  case FT_RENDER_MODE_MONO:
1492
0
      format = CAIRO_FORMAT_A1;
1493
0
      break;
1494
0
  case FT_RENDER_MODE_LCD:
1495
0
  case FT_RENDER_MODE_LCD_V:
1496
0
      format= CAIRO_FORMAT_ARGB32;
1497
0
      break;
1498
0
  case FT_RENDER_MODE_LIGHT:
1499
465
  case FT_RENDER_MODE_NORMAL:
1500
465
  case FT_RENDER_MODE_MAX:
1501
465
#if HAVE_FT_RENDER_MODE_SDF
1502
465
  case FT_RENDER_MODE_SDF:
1503
465
#endif
1504
465
  default:
1505
465
      format = CAIRO_FORMAT_A8;
1506
465
      break;
1507
465
  }
1508
1509
465
  (*surface) = (cairo_image_surface_t *)
1510
465
      cairo_image_surface_create_for_data (NULL, format, 0, 0, 0);
1511
465
  pixman_image_set_component_alpha ((*surface)->pixman_image, TRUE);
1512
465
  if ((*surface)->base.status)
1513
0
      return (*surface)->base.status;
1514
25.1k
    } else {
1515
1516
25.1k
  int bitmap_size;
1517
1518
25.1k
  switch (render_mode) {
1519
0
  case FT_RENDER_MODE_LCD:
1520
0
      if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_BGR)
1521
0
    rgba = CAIRO_SUBPIXEL_ORDER_BGR;
1522
0
      else
1523
0
    rgba = CAIRO_SUBPIXEL_ORDER_RGB;
1524
0
      break;
1525
1526
0
  case FT_RENDER_MODE_LCD_V:
1527
0
      if (font_options->subpixel_order == CAIRO_SUBPIXEL_ORDER_VBGR)
1528
0
    rgba = CAIRO_SUBPIXEL_ORDER_VBGR;
1529
0
      else
1530
0
    rgba = CAIRO_SUBPIXEL_ORDER_VRGB;
1531
0
      break;
1532
1533
0
  case FT_RENDER_MODE_MONO:
1534
0
  case FT_RENDER_MODE_LIGHT:
1535
25.1k
  case FT_RENDER_MODE_NORMAL:
1536
25.1k
  case FT_RENDER_MODE_MAX:
1537
25.1k
#if HAVE_FT_RENDER_MODE_SDF
1538
25.1k
  case FT_RENDER_MODE_SDF:
1539
25.1k
#endif
1540
25.1k
  default:
1541
25.1k
      break;
1542
25.1k
  }
1543
1544
25.1k
  FT_Library_SetLcdFilter (library, lcd_filter);
1545
25.1k
  error = FT_Render_Glyph (face->glyph, render_mode);
1546
1547
25.1k
  FT_Library_SetLcdFilter (library, FT_LCD_FILTER_NONE);
1548
25.1k
  if (error)
1549
10
      return _cairo_error (_cairo_ft_to_cairo_error (error));
1550
1551
25.1k
  bitmap_size = _compute_xrender_bitmap_size (&bitmap,
1552
25.1k
                face->glyph,
1553
25.1k
                render_mode);
1554
25.1k
  if (bitmap_size < 0)
1555
0
      return _cairo_error (CAIRO_STATUS_INVALID_FORMAT);
1556
1557
25.1k
  bitmap.buffer = _cairo_calloc (bitmap_size);
1558
25.1k
  if (bitmap.buffer == NULL)
1559
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1560
1561
25.1k
  _fill_xrender_bitmap (&bitmap, face->glyph, render_mode,
1562
25.1k
            (rgba == CAIRO_SUBPIXEL_ORDER_BGR || rgba == CAIRO_SUBPIXEL_ORDER_VBGR));
1563
1564
  /* Note:
1565
   * _get_bitmap_surface will free bitmap.buffer if there is an error
1566
   */
1567
25.1k
  status = _get_bitmap_surface (&bitmap, NULL, TRUE, font_options, surface);
1568
25.1k
  if (unlikely (status))
1569
0
      return status;
1570
1571
  /* Note: the font's coordinate system is upside down from ours, so the
1572
   * Y coordinate of the control box needs to be negated.  Moreover, device
1573
   * offsets are position of glyph origin relative to top left while xMin
1574
   * and yMax are offsets of top left relative to origin.  Another negation.
1575
   */
1576
25.1k
  cairo_surface_set_device_offset (&(*surface)->base,
1577
25.1k
           (double)-glyphslot->bitmap_left,
1578
25.1k
           (double)+glyphslot->bitmap_top);
1579
25.1k
    }
1580
1581
25.5k
    return CAIRO_STATUS_SUCCESS;
1582
25.6k
}
1583
1584
/* Converts a bitmap (or other) FT_GlyphSlot into an image */
1585
static cairo_status_t
1586
_render_glyph_bitmap (FT_Face         face,
1587
          cairo_font_options_t   *font_options,
1588
          cairo_image_surface_t **surface)
1589
0
{
1590
0
    FT_GlyphSlot glyphslot = face->glyph;
1591
0
    cairo_status_t status;
1592
0
    FT_Error error;
1593
1594
    /* According to the FreeType docs, glyphslot->format could be
1595
     * something other than FT_GLYPH_FORMAT_OUTLINE or
1596
     * FT_GLYPH_FORMAT_BITMAP. Calling FT_Render_Glyph gives FreeType
1597
     * the opportunity to convert such to
1598
     * bitmap. FT_GLYPH_FORMAT_COMPOSITE will not be encountered since
1599
     * we avoid the FT_LOAD_NO_RECURSE flag.
1600
     */
1601
0
    error = FT_Render_Glyph (glyphslot, FT_RENDER_MODE_NORMAL);
1602
    /* XXX ignoring all other errors for now.  They are not fatal, typically
1603
     * just a glyph-not-found. */
1604
0
    if (error == FT_Err_Out_Of_Memory)
1605
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1606
1607
0
    status = _get_bitmap_surface (&glyphslot->bitmap,
1608
0
          glyphslot->library,
1609
0
          FALSE, font_options,
1610
0
          surface);
1611
0
    if (unlikely (status))
1612
0
  return status;
1613
1614
    /*
1615
     * Note: the font's coordinate system is upside down from ours, so the
1616
     * Y coordinate of the control box needs to be negated.  Moreover, device
1617
     * offsets are position of glyph origin relative to top left while
1618
     * bitmap_left and bitmap_top are offsets of top left relative to origin.
1619
     * Another negation.
1620
     */
1621
0
    cairo_surface_set_device_offset (&(*surface)->base,
1622
0
             -glyphslot->bitmap_left,
1623
0
             +glyphslot->bitmap_top);
1624
1625
0
    return CAIRO_STATUS_SUCCESS;
1626
0
}
1627
1628
static cairo_status_t
1629
_transform_glyph_bitmap (cairo_matrix_t         * shape,
1630
       cairo_image_surface_t ** surface)
1631
0
{
1632
0
    cairo_matrix_t original_to_transformed;
1633
0
    cairo_matrix_t transformed_to_original;
1634
0
    cairo_image_surface_t *old_image;
1635
0
    cairo_surface_t *image;
1636
0
    double x[4], y[4];
1637
0
    double origin_x, origin_y;
1638
0
    int orig_width, orig_height;
1639
0
    int i;
1640
0
    int x_min, y_min, x_max, y_max;
1641
0
    int width, height;
1642
0
    cairo_status_t status;
1643
0
    cairo_surface_pattern_t pattern;
1644
1645
    /* We want to compute a transform that takes the origin
1646
     * (device_x_offset, device_y_offset) to 0,0, then applies
1647
     * the "shape" portion of the font transform
1648
     */
1649
0
    original_to_transformed = *shape;
1650
1651
0
    cairo_surface_get_device_offset (&(*surface)->base, &origin_x, &origin_y);
1652
0
    orig_width = (*surface)->width;
1653
0
    orig_height = (*surface)->height;
1654
1655
0
    cairo_matrix_translate (&original_to_transformed,
1656
0
          -origin_x, -origin_y);
1657
1658
    /* Find the bounding box of the original bitmap under that
1659
     * transform
1660
     */
1661
0
    x[0] = 0;          y[0] = 0;
1662
0
    x[1] = orig_width; y[1] = 0;
1663
0
    x[2] = orig_width; y[2] = orig_height;
1664
0
    x[3] = 0;          y[3] = orig_height;
1665
1666
0
    for (i = 0; i < 4; i++)
1667
0
      cairo_matrix_transform_point (&original_to_transformed,
1668
0
            &x[i], &y[i]);
1669
1670
0
    x_min = floor (x[0]);   y_min = floor (y[0]);
1671
0
    x_max =  ceil (x[0]);   y_max =  ceil (y[0]);
1672
1673
0
    for (i = 1; i < 4; i++) {
1674
0
  if (x[i] < x_min)
1675
0
      x_min = floor (x[i]);
1676
0
  else if (x[i] > x_max)
1677
0
      x_max = ceil (x[i]);
1678
0
  if (y[i] < y_min)
1679
0
      y_min = floor (y[i]);
1680
0
  else if (y[i] > y_max)
1681
0
      y_max = ceil (y[i]);
1682
0
    }
1683
1684
    /* Adjust the transform so that the bounding box starts at 0,0 ...
1685
     * this gives our final transform from original bitmap to transformed
1686
     * bitmap.
1687
     */
1688
0
    original_to_transformed.x0 -= x_min;
1689
0
    original_to_transformed.y0 -= y_min;
1690
1691
    /* Create the transformed bitmap */
1692
0
    width  = x_max - x_min;
1693
0
    height = y_max - y_min;
1694
1695
0
    transformed_to_original = original_to_transformed;
1696
0
    status = cairo_matrix_invert (&transformed_to_original);
1697
0
    if (unlikely (status))
1698
0
  return status;
1699
1700
0
    if ((*surface)->format == CAIRO_FORMAT_ARGB32 &&
1701
0
        !pixman_image_get_component_alpha ((*surface)->pixman_image))
1702
0
      image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
1703
0
    else
1704
0
      image = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
1705
0
    if (unlikely (image->status))
1706
0
  return image->status;
1707
1708
    /* Draw the original bitmap transformed into the new bitmap
1709
     */
1710
0
    _cairo_pattern_init_for_surface (&pattern, &(*surface)->base);
1711
0
    cairo_pattern_set_matrix (&pattern.base, &transformed_to_original);
1712
1713
0
    status = _cairo_surface_paint (image,
1714
0
           CAIRO_OPERATOR_SOURCE,
1715
0
           &pattern.base,
1716
0
           NULL);
1717
1718
0
    _cairo_pattern_fini (&pattern.base);
1719
1720
0
    if (unlikely (status)) {
1721
0
  cairo_surface_destroy (image);
1722
0
  return status;
1723
0
    }
1724
1725
    /* Now update the cache entry for the new bitmap, recomputing
1726
     * the origin based on the final transform.
1727
     */
1728
0
    cairo_matrix_transform_point (&original_to_transformed,
1729
0
          &origin_x, &origin_y);
1730
1731
0
    old_image = (*surface);
1732
0
    (*surface) = (cairo_image_surface_t *)image;
1733
1734
    /* Note: we converted subpixel-rendered RGBA images to grayscale,
1735
     * so, no need to copy component alpha to new image. */
1736
1737
0
    cairo_surface_destroy (&old_image->base);
1738
1739
0
    cairo_surface_set_device_offset (&(*surface)->base,
1740
0
             _cairo_lround (origin_x),
1741
0
             _cairo_lround (origin_y));
1742
0
    return CAIRO_STATUS_SUCCESS;
1743
0
}
1744
1745
static const cairo_unscaled_font_backend_t cairo_ft_unscaled_font_backend = {
1746
    _cairo_ft_unscaled_font_destroy,
1747
#if 0
1748
    _cairo_ft_unscaled_font_create_glyph
1749
#endif
1750
};
1751
1752
/* #cairo_ft_scaled_font_t */
1753
1754
typedef struct _cairo_ft_scaled_font {
1755
    cairo_scaled_font_t base;
1756
    cairo_ft_unscaled_font_t *unscaled;
1757
    cairo_ft_options_t ft_options;
1758
} cairo_ft_scaled_font_t;
1759
1760
static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend;
1761
1762
#if CAIRO_HAS_FC_FONT
1763
/* The load flags passed to FT_Load_Glyph control aspects like hinting and
1764
 * antialiasing. Here we compute them from the fields of a FcPattern.
1765
 */
1766
static void
1767
_get_pattern_ft_options (FcPattern *pattern, cairo_ft_options_t *ret)
1768
207
{
1769
207
    FcBool antialias, vertical_layout, hinting, autohint, bitmap, embolden;
1770
207
    cairo_ft_options_t ft_options;
1771
207
    int rgba;
1772
207
    int hintstyle;
1773
207
    char *variations;
1774
1775
207
    _cairo_font_options_init_default (&ft_options.base);
1776
207
    ft_options.load_flags = FT_LOAD_DEFAULT;
1777
207
    ft_options.synth_flags = 0;
1778
1779
    /* Check whether to force use of embedded bitmaps */
1780
207
    if (FcPatternGetBool (pattern,
1781
207
        FC_EMBEDDED_BITMAP, 0, &bitmap) != FcResultMatch)
1782
0
  bitmap = FcFalse;
1783
1784
    /* disable antialiasing if requested */
1785
207
    if (FcPatternGetBool (pattern,
1786
207
        FC_ANTIALIAS, 0, &antialias) != FcResultMatch)
1787
0
  antialias = FcTrue;
1788
1789
207
    if (antialias) {
1790
207
  cairo_subpixel_order_t subpixel_order;
1791
207
  int lcd_filter;
1792
1793
  /* disable hinting if requested */
1794
207
  if (FcPatternGetBool (pattern,
1795
207
            FC_HINTING, 0, &hinting) != FcResultMatch)
1796
0
      hinting = FcTrue;
1797
1798
207
  if (FcPatternGetInteger (pattern,
1799
207
         FC_RGBA, 0, &rgba) != FcResultMatch)
1800
0
      rgba = FC_RGBA_UNKNOWN;
1801
1802
207
  switch (rgba) {
1803
0
  case FC_RGBA_RGB:
1804
0
      subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
1805
0
      break;
1806
0
  case FC_RGBA_BGR:
1807
0
      subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
1808
0
      break;
1809
0
  case FC_RGBA_VRGB:
1810
0
      subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
1811
0
      break;
1812
0
  case FC_RGBA_VBGR:
1813
0
      subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
1814
0
      break;
1815
0
  case FC_RGBA_UNKNOWN:
1816
207
  case FC_RGBA_NONE:
1817
207
  default:
1818
207
      subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1819
207
      break;
1820
207
  }
1821
1822
207
  if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT) {
1823
0
      ft_options.base.subpixel_order = subpixel_order;
1824
0
      ft_options.base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1825
0
  }
1826
1827
207
  if (FcPatternGetInteger (pattern,
1828
207
         FC_LCD_FILTER, 0, &lcd_filter) == FcResultMatch)
1829
0
  {
1830
0
      switch (lcd_filter) {
1831
0
      case FC_LCD_NONE:
1832
0
    ft_options.base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1833
0
    break;
1834
0
      case FC_LCD_DEFAULT:
1835
0
    ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR5;
1836
0
    break;
1837
0
      case FC_LCD_LIGHT:
1838
0
    ft_options.base.lcd_filter = CAIRO_LCD_FILTER_FIR3;
1839
0
    break;
1840
0
      case FC_LCD_LEGACY:
1841
0
    ft_options.base.lcd_filter = CAIRO_LCD_FILTER_INTRA_PIXEL;
1842
0
    break;
1843
0
      }
1844
0
  }
1845
1846
207
  if (FcPatternGetInteger (pattern,
1847
207
         FC_HINT_STYLE, 0, &hintstyle) != FcResultMatch)
1848
0
      hintstyle = FC_HINT_FULL;
1849
1850
207
  if (!hinting)
1851
0
      hintstyle = FC_HINT_NONE;
1852
1853
207
  switch (hintstyle) {
1854
0
  case FC_HINT_NONE:
1855
0
      ft_options.base.hint_style = CAIRO_HINT_STYLE_NONE;
1856
0
      break;
1857
0
  case FC_HINT_SLIGHT:
1858
0
      ft_options.base.hint_style = CAIRO_HINT_STYLE_SLIGHT;
1859
0
      break;
1860
0
  case FC_HINT_MEDIUM:
1861
0
  default:
1862
0
      ft_options.base.hint_style = CAIRO_HINT_STYLE_MEDIUM;
1863
0
      break;
1864
207
  case FC_HINT_FULL:
1865
207
      ft_options.base.hint_style = CAIRO_HINT_STYLE_FULL;
1866
207
      break;
1867
207
  }
1868
1869
  /* Force embedded bitmaps off if no hinting requested */
1870
207
  if (ft_options.base.hint_style == CAIRO_HINT_STYLE_NONE)
1871
0
    bitmap = FcFalse;
1872
1873
207
  if (!bitmap)
1874
0
      ft_options.load_flags |= FT_LOAD_NO_BITMAP;
1875
1876
207
    } else {
1877
0
  ft_options.base.antialias = CAIRO_ANTIALIAS_NONE;
1878
0
    }
1879
1880
    /* force autohinting if requested */
1881
207
    if (FcPatternGetBool (pattern,
1882
207
        FC_AUTOHINT, 0, &autohint) != FcResultMatch)
1883
0
  autohint = FcFalse;
1884
1885
207
    if (autohint)
1886
0
  ft_options.load_flags |= FT_LOAD_FORCE_AUTOHINT;
1887
1888
207
    if (FcPatternGetBool (pattern,
1889
207
        FC_VERTICAL_LAYOUT, 0, &vertical_layout) != FcResultMatch)
1890
0
  vertical_layout = FcFalse;
1891
1892
207
    if (vertical_layout)
1893
0
  ft_options.load_flags |= FT_LOAD_VERTICAL_LAYOUT;
1894
1895
207
    if (FcPatternGetBool (pattern,
1896
207
        FC_EMBOLDEN, 0, &embolden) != FcResultMatch)
1897
0
  embolden = FcFalse;
1898
1899
207
    if (embolden)
1900
0
  ft_options.synth_flags |= CAIRO_FT_SYNTHESIZE_BOLD;
1901
1902
207
    if (FcPatternGetString (pattern, FC_FONT_VARIATIONS, 0, (FcChar8 **) &variations) == FcResultMatch) {
1903
3
      ft_options.base.variations = strdup (variations);
1904
3
    }
1905
1906
207
    *ret = ft_options;
1907
207
}
1908
#endif
1909
1910
static void
1911
_cairo_ft_options_merge (cairo_ft_options_t *options,
1912
       cairo_ft_options_t *other)
1913
91
{
1914
91
    int load_flags = other->load_flags;
1915
91
    int load_target = FT_LOAD_TARGET_NORMAL;
1916
1917
    /* clear load target mode */
1918
91
    load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(other->load_flags)));
1919
1920
91
    if (load_flags & FT_LOAD_NO_HINTING)
1921
0
  other->base.hint_style = CAIRO_HINT_STYLE_NONE;
1922
1923
91
    if (other->base.antialias == CAIRO_ANTIALIAS_NONE ||
1924
91
  options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1925
0
  options->base.antialias = CAIRO_ANTIALIAS_NONE;
1926
0
  options->base.subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
1927
0
    }
1928
1929
91
    if (other->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL &&
1930
0
  options->base.antialias == CAIRO_ANTIALIAS_DEFAULT) {
1931
0
  options->base.antialias = CAIRO_ANTIALIAS_SUBPIXEL;
1932
0
  options->base.subpixel_order = other->base.subpixel_order;
1933
0
    }
1934
1935
91
    if (options->base.hint_style == CAIRO_HINT_STYLE_DEFAULT)
1936
21
  options->base.hint_style = other->base.hint_style;
1937
1938
91
    if (other->base.hint_style == CAIRO_HINT_STYLE_NONE)
1939
0
  options->base.hint_style = CAIRO_HINT_STYLE_NONE;
1940
1941
91
    if (options->base.lcd_filter == CAIRO_LCD_FILTER_DEFAULT)
1942
91
  options->base.lcd_filter = other->base.lcd_filter;
1943
1944
91
    if (other->base.lcd_filter == CAIRO_LCD_FILTER_NONE)
1945
0
  options->base.lcd_filter = CAIRO_LCD_FILTER_NONE;
1946
1947
91
    if (options->base.antialias == CAIRO_ANTIALIAS_NONE) {
1948
0
  if (options->base.hint_style == CAIRO_HINT_STYLE_NONE)
1949
0
      load_flags |= FT_LOAD_NO_HINTING;
1950
0
  else
1951
0
      load_target = FT_LOAD_TARGET_MONO;
1952
0
  load_flags |= FT_LOAD_MONOCHROME;
1953
91
    } else {
1954
91
  switch (options->base.hint_style) {
1955
0
  case CAIRO_HINT_STYLE_NONE:
1956
0
      load_flags |= FT_LOAD_NO_HINTING;
1957
0
      break;
1958
70
  case CAIRO_HINT_STYLE_SLIGHT:
1959
70
      load_target = FT_LOAD_TARGET_LIGHT;
1960
70
      break;
1961
0
  case CAIRO_HINT_STYLE_MEDIUM:
1962
0
      break;
1963
21
  case CAIRO_HINT_STYLE_FULL:
1964
21
  case CAIRO_HINT_STYLE_DEFAULT:
1965
21
      if (options->base.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
1966
0
    switch (options->base.subpixel_order) {
1967
0
    case CAIRO_SUBPIXEL_ORDER_DEFAULT:
1968
0
    case CAIRO_SUBPIXEL_ORDER_RGB:
1969
0
    case CAIRO_SUBPIXEL_ORDER_BGR:
1970
0
        load_target = FT_LOAD_TARGET_LCD;
1971
0
        break;
1972
0
    case CAIRO_SUBPIXEL_ORDER_VRGB:
1973
0
    case CAIRO_SUBPIXEL_ORDER_VBGR:
1974
0
        load_target = FT_LOAD_TARGET_LCD_V;
1975
0
    break;
1976
0
    }
1977
0
      }
1978
21
      break;
1979
91
  }
1980
91
    }
1981
1982
91
    if (other->base.variations) {
1983
2
      if (options->base.variations) {
1984
0
        char *p;
1985
1986
        /* 'merge' variations by concatenating - later entries win */
1987
0
        p = malloc (strlen (other->base.variations) + strlen (options->base.variations) + 2);
1988
0
        p[0] = 0;
1989
0
        strcat (p, other->base.variations);
1990
0
        strcat (p, ",");
1991
0
        strcat (p, options->base.variations);
1992
0
        free (options->base.variations);
1993
0
        options->base.variations = p;
1994
0
      }
1995
2
      else {
1996
2
        options->base.variations = strdup (other->base.variations);
1997
2
      }
1998
2
    }
1999
2000
91
    options->load_flags = load_flags | load_target;
2001
91
    options->synth_flags = other->synth_flags;
2002
91
}
2003
2004
static cairo_status_t
2005
_cairo_ft_font_face_scaled_font_create (void        *abstract_font_face,
2006
          const cairo_matrix_t   *font_matrix,
2007
          const cairo_matrix_t   *ctm,
2008
          const cairo_font_options_t *options,
2009
          cairo_scaled_font_t       **font_out)
2010
91
{
2011
91
    cairo_ft_font_face_t *font_face = abstract_font_face;
2012
91
    cairo_ft_scaled_font_t *scaled_font;
2013
91
    FT_Face face;
2014
91
    FT_Size_Metrics *metrics;
2015
91
    cairo_font_extents_t fs_metrics;
2016
91
    cairo_status_t status;
2017
91
    cairo_ft_unscaled_font_t *unscaled;
2018
2019
91
    assert (font_face->unscaled);
2020
2021
91
    face = _cairo_ft_unscaled_font_lock_face (font_face->unscaled);
2022
91
    if (unlikely (face == NULL)) /* backend error */
2023
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2024
2025
91
    scaled_font = _cairo_calloc (sizeof (cairo_ft_scaled_font_t));
2026
91
    if (unlikely (scaled_font == NULL)) {
2027
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2028
0
  goto FAIL;
2029
0
    }
2030
2031
91
    scaled_font->unscaled = unscaled = font_face->unscaled;
2032
91
    _cairo_unscaled_font_reference (&unscaled->base);
2033
2034
91
    _cairo_font_options_init_copy (&scaled_font->ft_options.base, options);
2035
91
    _cairo_ft_options_merge (&scaled_font->ft_options, &font_face->ft_options);
2036
2037
91
    status = _cairo_scaled_font_init (&scaled_font->base,
2038
91
                    &font_face->base,
2039
91
              font_matrix, ctm, options,
2040
91
              &_cairo_ft_scaled_font_backend);
2041
91
    if (unlikely (status))
2042
0
  goto CLEANUP_SCALED_FONT;
2043
2044
91
    status = _cairo_ft_unscaled_font_set_scale (unscaled,
2045
91
                        &scaled_font->base.scale);
2046
91
    if (unlikely (status)) {
2047
  /* This can only fail if we encounter an error with the underlying
2048
   * font, so propagate the error back to the font-face. */
2049
0
  _cairo_ft_unscaled_font_unlock_face (unscaled);
2050
0
  _cairo_unscaled_font_destroy (&unscaled->base);
2051
0
  free (scaled_font);
2052
0
  return status;
2053
0
    }
2054
2055
2056
91
    metrics = &face->size->metrics;
2057
2058
    /*
2059
     * Get to unscaled metrics so that the upper level can get back to
2060
     * user space
2061
     *
2062
     * Also use this path for bitmap-only fonts.  The other branch uses
2063
     * face members that are only relevant for scalable fonts.  This is
2064
     * detected by simply checking for units_per_EM==0.
2065
     */
2066
91
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF ||
2067
70
  face->units_per_EM == 0) {
2068
21
  fs_metrics.ascent =        SCALE (DOUBLE_FROM_26_6 (metrics->ascender), unscaled->y_scale);
2069
21
  fs_metrics.descent =       SCALE (DOUBLE_FROM_26_6 (- metrics->descender), unscaled->y_scale);
2070
21
  fs_metrics.height =        SCALE (DOUBLE_FROM_26_6 (metrics->height), unscaled->y_scale);
2071
21
  if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
2072
21
      fs_metrics.max_x_advance = SCALE (DOUBLE_FROM_26_6 (metrics->max_advance), unscaled->x_scale);
2073
21
      fs_metrics.max_y_advance = 0;
2074
21
  } else {
2075
0
      fs_metrics.max_x_advance = 0;
2076
0
      fs_metrics.max_y_advance = SCALE (DOUBLE_FROM_26_6 (metrics->max_advance), unscaled->y_scale);
2077
0
  }
2078
70
    } else {
2079
70
  double scale = face->units_per_EM;
2080
2081
70
  fs_metrics.ascent =        face->ascender / scale;
2082
70
  fs_metrics.descent =       - face->descender / scale;
2083
70
  fs_metrics.height =        face->height / scale;
2084
70
  if (!_cairo_ft_scaled_font_is_vertical (&scaled_font->base)) {
2085
70
      fs_metrics.max_x_advance = face->max_advance_width / scale;
2086
70
      fs_metrics.max_y_advance = 0;
2087
70
  } else {
2088
0
      fs_metrics.max_x_advance = 0;
2089
0
      fs_metrics.max_y_advance = face->max_advance_height / scale;
2090
0
  }
2091
70
    }
2092
2093
91
    status = _cairo_scaled_font_set_metrics (&scaled_font->base, &fs_metrics);
2094
91
    if (unlikely (status))
2095
0
  goto CLEANUP_SCALED_FONT;
2096
2097
91
    _cairo_ft_unscaled_font_unlock_face (unscaled);
2098
2099
91
    *font_out = &scaled_font->base;
2100
91
    return CAIRO_STATUS_SUCCESS;
2101
2102
0
  CLEANUP_SCALED_FONT:
2103
0
    _cairo_unscaled_font_destroy (&unscaled->base);
2104
0
    free (scaled_font);
2105
0
  FAIL:
2106
0
    _cairo_ft_unscaled_font_unlock_face (font_face->unscaled);
2107
0
    *font_out = _cairo_scaled_font_create_in_error (status);
2108
0
    return CAIRO_STATUS_SUCCESS; /* non-backend error */
2109
0
}
2110
2111
cairo_bool_t
2112
_cairo_scaled_font_is_ft (cairo_scaled_font_t *scaled_font)
2113
91
{
2114
91
    return scaled_font->backend == &_cairo_ft_scaled_font_backend;
2115
91
}
2116
2117
static void
2118
_cairo_ft_scaled_font_fini (void *abstract_font)
2119
1
{
2120
1
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
2121
2122
1
    if (scaled_font == NULL)
2123
0
        return;
2124
2125
1
    _cairo_font_options_fini (&scaled_font->ft_options.base);
2126
1
    _cairo_unscaled_font_destroy (&scaled_font->unscaled->base);
2127
1
}
2128
2129
static int
2130
_move_to (FT_Vector *to, void *closure)
2131
0
{
2132
0
    cairo_path_fixed_t *path = closure;
2133
0
    cairo_fixed_t x, y;
2134
2135
0
    x = _cairo_fixed_from_26_6 (to->x);
2136
0
    y = _cairo_fixed_from_26_6 (to->y);
2137
2138
0
    if (_cairo_path_fixed_close_path (path) != CAIRO_STATUS_SUCCESS)
2139
0
  return 1;
2140
0
    if (_cairo_path_fixed_move_to (path, x, y) != CAIRO_STATUS_SUCCESS)
2141
0
  return 1;
2142
2143
0
    return 0;
2144
0
}
2145
2146
static int
2147
_line_to (FT_Vector *to, void *closure)
2148
0
{
2149
0
    cairo_path_fixed_t *path = closure;
2150
0
    cairo_fixed_t x, y;
2151
2152
0
    x = _cairo_fixed_from_26_6 (to->x);
2153
0
    y = _cairo_fixed_from_26_6 (to->y);
2154
2155
0
    if (_cairo_path_fixed_line_to (path, x, y) != CAIRO_STATUS_SUCCESS)
2156
0
  return 1;
2157
2158
0
    return 0;
2159
0
}
2160
2161
static int
2162
_conic_to (FT_Vector *control, FT_Vector *to, void *closure)
2163
0
{
2164
0
    cairo_path_fixed_t *path = closure;
2165
2166
0
    cairo_fixed_t x0, y0;
2167
0
    cairo_fixed_t x1, y1;
2168
0
    cairo_fixed_t x2, y2;
2169
0
    cairo_fixed_t x3, y3;
2170
0
    cairo_point_t conic;
2171
2172
0
    if (! _cairo_path_fixed_get_current_point (path, &x0, &y0))
2173
0
  return 1;
2174
2175
0
    conic.x = _cairo_fixed_from_26_6 (control->x);
2176
0
    conic.y = _cairo_fixed_from_26_6 (control->y);
2177
2178
0
    x3 = _cairo_fixed_from_26_6 (to->x);
2179
0
    y3 = _cairo_fixed_from_26_6 (to->y);
2180
2181
0
    x1 = x0 + 2.0/3.0 * (conic.x - x0);
2182
0
    y1 = y0 + 2.0/3.0 * (conic.y - y0);
2183
2184
0
    x2 = x3 + 2.0/3.0 * (conic.x - x3);
2185
0
    y2 = y3 + 2.0/3.0 * (conic.y - y3);
2186
2187
0
    if (_cairo_path_fixed_curve_to (path,
2188
0
            x1, y1,
2189
0
            x2, y2,
2190
0
            x3, y3) != CAIRO_STATUS_SUCCESS)
2191
0
  return 1;
2192
2193
0
    return 0;
2194
0
}
2195
2196
static int
2197
_cubic_to (FT_Vector *control1, FT_Vector *control2,
2198
     FT_Vector *to, void *closure)
2199
0
{
2200
0
    cairo_path_fixed_t *path = closure;
2201
0
    cairo_fixed_t x0, y0;
2202
0
    cairo_fixed_t x1, y1;
2203
0
    cairo_fixed_t x2, y2;
2204
2205
0
    x0 = _cairo_fixed_from_26_6 (control1->x);
2206
0
    y0 = _cairo_fixed_from_26_6 (control1->y);
2207
2208
0
    x1 = _cairo_fixed_from_26_6 (control2->x);
2209
0
    y1 = _cairo_fixed_from_26_6 (control2->y);
2210
2211
0
    x2 = _cairo_fixed_from_26_6 (to->x);
2212
0
    y2 = _cairo_fixed_from_26_6 (to->y);
2213
2214
0
    if (_cairo_path_fixed_curve_to (path,
2215
0
            x0, y0,
2216
0
            x1, y1,
2217
0
            x2, y2) != CAIRO_STATUS_SUCCESS)
2218
0
  return 1;
2219
2220
0
    return 0;
2221
0
}
2222
2223
cairo_status_t
2224
_cairo_ft_face_decompose_glyph_outline (FT_Face        face,
2225
          cairo_path_fixed_t **pathp)
2226
0
{
2227
0
    static const FT_Outline_Funcs outline_funcs = {
2228
0
  (FT_Outline_MoveToFunc)_move_to,
2229
0
  (FT_Outline_LineToFunc)_line_to,
2230
0
  (FT_Outline_ConicToFunc)_conic_to,
2231
0
  (FT_Outline_CubicToFunc)_cubic_to,
2232
0
  0, /* shift */
2233
0
  0, /* delta */
2234
0
    };
2235
0
    static const FT_Matrix invert_y = {
2236
0
  DOUBLE_TO_16_16 (1.0), 0,
2237
0
  0, DOUBLE_TO_16_16 (-1.0),
2238
0
    };
2239
2240
0
    FT_GlyphSlot glyph;
2241
0
    cairo_path_fixed_t *path;
2242
0
    cairo_status_t status;
2243
2244
0
    path = _cairo_path_fixed_create ();
2245
0
    if (!path)
2246
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2247
2248
0
    glyph = face->glyph;
2249
2250
    /* Font glyphs have an inverted Y axis compared to cairo. */
2251
0
    FT_Outline_Transform (&glyph->outline, &invert_y);
2252
0
    if (FT_Outline_Decompose (&glyph->outline, &outline_funcs, path)) {
2253
0
  _cairo_path_fixed_destroy (path);
2254
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2255
0
    }
2256
2257
0
    status = _cairo_path_fixed_close_path (path);
2258
0
    if (unlikely (status)) {
2259
0
  _cairo_path_fixed_destroy (path);
2260
0
  return status;
2261
0
    }
2262
2263
0
    *pathp = path;
2264
2265
0
    return CAIRO_STATUS_SUCCESS;
2266
0
}
2267
2268
/*
2269
 * Translate glyph to match its metrics.
2270
 */
2271
static void
2272
_cairo_ft_scaled_glyph_vertical_layout_bearing_fix (void        *abstract_font,
2273
                FT_GlyphSlot glyph)
2274
0
{
2275
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
2276
0
    FT_Vector vector;
2277
2278
0
    vector.x = glyph->metrics.vertBearingX - glyph->metrics.horiBearingX;
2279
0
    vector.y = -glyph->metrics.vertBearingY - glyph->metrics.horiBearingY;
2280
2281
0
    if (glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2282
0
  FT_Vector_Transform (&vector, &scaled_font->unscaled->Current_Shape);
2283
0
  FT_Outline_Translate(&glyph->outline, vector.x, vector.y);
2284
0
    } else if (glyph->format == FT_GLYPH_FORMAT_BITMAP) {
2285
0
  glyph->bitmap_left += vector.x / 64;
2286
0
  glyph->bitmap_top  += vector.y / 64;
2287
0
    }
2288
0
}
2289
2290
static void
2291
cairo_ft_apply_variations (FT_Face                 face,
2292
         cairo_ft_scaled_font_t *scaled_font)
2293
88.9k
{
2294
88.9k
    FT_MM_Var *ft_mm_var;
2295
88.9k
    FT_Error ret;
2296
88.9k
    unsigned int instance_id = scaled_font->unscaled->id >> 16;
2297
2298
88.9k
    ret = FT_Get_MM_Var (face, &ft_mm_var);
2299
88.9k
    if (ret == 0) {
2300
0
        FT_Fixed *current_coords;
2301
0
        FT_Fixed *coords;
2302
0
        unsigned int i;
2303
0
        const char *p;
2304
2305
0
        coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
2306
  /* FIXME check coords. */
2307
2308
0
  if (scaled_font->unscaled->variations)
2309
0
  {
2310
0
      memcpy (coords, scaled_font->unscaled->variations, ft_mm_var->num_axis * sizeof (*coords));
2311
0
  }
2312
0
  else if (instance_id && instance_id <= ft_mm_var->num_namedstyles)
2313
0
  {
2314
0
      FT_Var_Named_Style *instance = &ft_mm_var->namedstyle[instance_id - 1];
2315
0
      memcpy (coords, instance->coords, ft_mm_var->num_axis * sizeof (*coords));
2316
0
  }
2317
0
  else
2318
0
      for (i = 0; i < ft_mm_var->num_axis; i++)
2319
0
    coords[i] = ft_mm_var->axis[i].def;
2320
2321
0
        p = scaled_font->ft_options.base.variations;
2322
0
        while (p && *p) {
2323
0
            const char *start;
2324
0
            const char *end, *end2;
2325
0
            FT_ULong tag;
2326
0
            double value;
2327
2328
0
            while (_cairo_isspace (*p)) p++;
2329
2330
0
            start = p;
2331
0
            end = strchr (p, ',');
2332
0
            if (end && (end - p < 6))
2333
0
                goto skip;
2334
2335
0
            tag = FT_MAKE_TAG(p[0], p[1], p[2], p[3]);
2336
2337
0
            p += 4;
2338
0
            while (_cairo_isspace (*p)) p++;
2339
0
            if (*p == '=') p++;
2340
2341
0
            if (p - start < 5)
2342
0
                goto skip;
2343
2344
0
            value = _cairo_strtod (p, (char **) &end2);
2345
2346
0
            while (end2 && _cairo_isspace (*end2)) end2++;
2347
2348
0
            if (end2 && (*end2 != ',' && *end2 != '\0'))
2349
0
                goto skip;
2350
2351
0
            for (i = 0; i < ft_mm_var->num_axis; i++) {
2352
0
                if (ft_mm_var->axis[i].tag == tag) {
2353
0
                    coords[i] = (FT_Fixed)(value*65536);
2354
0
                    break;
2355
0
                }
2356
0
            }
2357
2358
0
skip:
2359
0
            p = end ? end + 1 : NULL;
2360
0
        }
2361
2362
0
        current_coords = malloc (sizeof (FT_Fixed) * ft_mm_var->num_axis);
2363
0
        ret = FT_Get_Var_Design_Coordinates (face, ft_mm_var->num_axis, current_coords);
2364
0
        if (ret == 0) {
2365
0
            for (i = 0; i < ft_mm_var->num_axis; i++) {
2366
0
              if (coords[i] != current_coords[i])
2367
0
                break;
2368
0
            }
2369
0
            if (i == ft_mm_var->num_axis)
2370
0
              goto done;
2371
0
        }
2372
2373
0
        FT_Set_Var_Design_Coordinates (face, ft_mm_var->num_axis, coords);
2374
0
done:
2375
0
        free (coords);
2376
0
        free (current_coords);
2377
0
        FT_Done_MM_Var (face->glyph->library, ft_mm_var);
2378
0
    }
2379
88.9k
}
2380
2381
typedef enum {
2382
    CAIRO_FT_GLYPH_TYPE_BITMAP,
2383
    CAIRO_FT_GLYPH_TYPE_OUTLINE,
2384
    CAIRO_FT_GLYPH_TYPE_SVG,
2385
    CAIRO_FT_GLYPH_TYPE_COLR_V0,
2386
    CAIRO_FT_GLYPH_TYPE_COLR_V1,
2387
} cairo_ft_glyph_format_t;
2388
2389
typedef struct {
2390
    cairo_scaled_glyph_private_t base;
2391
2392
    cairo_ft_glyph_format_t format;
2393
} cairo_ft_glyph_private_t;
2394
2395
static const int ft_glyph_private_key;
2396
2397
static cairo_int_status_t
2398
_cairo_ft_scaled_glyph_load_glyph (cairo_ft_scaled_font_t *scaled_font,
2399
           cairo_scaled_glyph_t   *scaled_glyph,
2400
           FT_Face                 face,
2401
           int                     load_flags,
2402
           cairo_bool_t            use_em_size,
2403
           cairo_bool_t            vertical_layout)
2404
88.9k
{
2405
88.9k
    FT_Error error;
2406
88.9k
    cairo_status_t status;
2407
88.9k
    cairo_ft_glyph_private_t *glyph_priv;
2408
2409
88.9k
    glyph_priv = (cairo_ft_glyph_private_t *) _cairo_scaled_glyph_find_private (scaled_glyph,
2410
88.9k
                                                                                &ft_glyph_private_key);
2411
88.9k
    assert (glyph_priv != NULL);
2412
2413
88.9k
    if (use_em_size) {
2414
31.0k
  cairo_matrix_t em_size;
2415
31.0k
  cairo_matrix_init_scale (&em_size, face->units_per_EM, face->units_per_EM);
2416
31.0k
  status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled, &em_size);
2417
57.8k
    } else {
2418
57.8k
  status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
2419
57.8k
                &scaled_font->base.scale);
2420
57.8k
    }
2421
88.9k
    if (unlikely (status))
2422
0
  return status;
2423
2424
88.9k
    cairo_ft_apply_variations (face, scaled_font);
2425
2426
88.9k
#if defined(HAVE_FT_LOAD_NO_SVG)
2427
88.9k
    if (load_flags & FT_LOAD_COLOR && glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
2428
0
        load_flags |= FT_LOAD_NO_SVG;
2429
88.9k
#endif
2430
2431
88.9k
    error = FT_Load_Glyph (face,
2432
88.9k
         _cairo_scaled_glyph_index(scaled_glyph),
2433
88.9k
         load_flags);
2434
    /* XXX ignoring all other errors for now.  They are not fatal, typically
2435
     * just a glyph-not-found. */
2436
88.9k
    if (error == FT_Err_Out_Of_Memory)
2437
0
  return  _cairo_error (CAIRO_STATUS_NO_MEMORY);
2438
2439
    /*
2440
     * synthesize glyphs if requested
2441
     */
2442
88.9k
    if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_BOLD)
2443
0
  FT_GlyphSlot_Embolden (face->glyph);
2444
2445
88.9k
    if (scaled_font->ft_options.synth_flags & CAIRO_FT_SYNTHESIZE_OBLIQUE)
2446
0
  FT_GlyphSlot_Oblique (face->glyph);
2447
2448
88.9k
    if (vertical_layout)
2449
0
  _cairo_ft_scaled_glyph_vertical_layout_bearing_fix (scaled_font, face->glyph);
2450
2451
88.9k
    if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
2452
88.9k
        FT_Pos xshift, yshift;
2453
2454
88.9k
        xshift = _cairo_scaled_glyph_xphase (scaled_glyph) << 4;
2455
88.9k
        yshift = _cairo_scaled_glyph_yphase (scaled_glyph) << 4;
2456
2457
88.9k
        FT_Outline_Translate (&face->glyph->outline, xshift, -yshift);
2458
88.9k
    }
2459
2460
88.9k
    return CAIRO_STATUS_SUCCESS;
2461
88.9k
}
2462
2463
static void
2464
_cairo_ft_glyph_fini (cairo_scaled_glyph_private_t *glyph_private,
2465
          cairo_scaled_glyph_t *glyph,
2466
          cairo_scaled_font_t  *font)
2467
16.6k
{
2468
16.6k
    cairo_list_del (&glyph_private->link);
2469
16.6k
    free (glyph_private);
2470
16.6k
}
2471
2472
2473
static void
2474
_cairo_ft_scaled_glyph_set_palette (cairo_ft_scaled_font_t  *scaled_font,
2475
            FT_Face                  face,
2476
            unsigned int            *num_entries_ret,
2477
            FT_Color               **entries_ret)
2478
0
{
2479
0
    unsigned int num_entries = 0;
2480
0
    FT_Color *entries = NULL;
2481
2482
0
    FT_Palette_Data palette_data;
2483
2484
0
    if (FT_Palette_Data_Get (face, &palette_data) == 0 && palette_data.num_palettes > 0) {
2485
0
  FT_UShort palette_index = CAIRO_COLOR_PALETTE_DEFAULT;
2486
0
  if (scaled_font->base.options.palette_index < palette_data.num_palettes)
2487
0
      palette_index = scaled_font->base.options.palette_index;
2488
2489
0
  if (FT_Palette_Select (face, palette_index, &entries) == 0) {
2490
0
      num_entries = palette_data.num_palette_entries;
2491
2492
            /* Overlay custom colors */
2493
0
            for (unsigned int i = 0; i < scaled_font->base.options.custom_palette_size; i++) {
2494
0
                cairo_palette_color_t *entry = &scaled_font->base.options.custom_palette[i];
2495
0
                if (entry->index < num_entries) {
2496
0
                    entries[entry->index].red = 255 * entry->red;
2497
0
                    entries[entry->index].green = 255 * entry->green;
2498
0
                    entries[entry->index].blue = 255 * entry->blue;
2499
0
                    entries[entry->index].alpha = 255 * entry->alpha;
2500
0
                }
2501
0
            }
2502
0
        }
2503
0
    }
2504
2505
0
    if (num_entries_ret)
2506
0
  *num_entries_ret = num_entries;
2507
2508
0
    if (entries_ret)
2509
0
  *entries_ret = entries;
2510
0
}
2511
2512
/* returns TRUE if foreground color used */
2513
static cairo_bool_t
2514
_cairo_ft_scaled_glyph_set_foreground_color (cairo_ft_scaled_font_t *scaled_font,
2515
               cairo_scaled_glyph_t   *scaled_glyph,
2516
               FT_Face                 face,
2517
               const cairo_color_t    *foreground_color)
2518
0
{
2519
0
    cairo_bool_t uses_foreground_color = FALSE;
2520
0
    FT_LayerIterator  iterator;
2521
0
    FT_UInt layer_glyph_index;
2522
0
    FT_UInt layer_color_index;
2523
0
    FT_Color color;
2524
2525
    /* Check if there is a layer that uses the foreground color */
2526
0
    iterator.p  = NULL;
2527
0
    while (FT_Get_Color_Glyph_Layer(face,
2528
0
            _cairo_scaled_glyph_index (scaled_glyph),
2529
0
            &layer_glyph_index,
2530
0
            &layer_color_index,
2531
0
            &iterator)) {
2532
0
  if (layer_color_index == 0xFFFF) {
2533
0
      uses_foreground_color = TRUE;
2534
0
      break;
2535
0
  }
2536
0
    }
2537
2538
0
    if (uses_foreground_color) {
2539
0
  color.red = (FT_Byte)(foreground_color->red * 0xFF);
2540
0
  color.green = (FT_Byte)(foreground_color->green * 0xFF);
2541
0
  color.blue = (FT_Byte)(foreground_color->blue * 0xFF);
2542
0
  color.alpha = (FT_Byte)(foreground_color->alpha * 0xFF);
2543
0
  FT_Palette_Set_Foreground_Color (face, color);
2544
0
    }
2545
2546
0
    return uses_foreground_color;
2547
0
}
2548
2549
static cairo_int_status_t
2550
_cairo_ft_scaled_glyph_init_surface (cairo_ft_scaled_font_t     *scaled_font,
2551
             cairo_scaled_glyph_t *scaled_glyph,
2552
             cairo_ft_glyph_private_t   *glyph_priv,
2553
             cairo_scaled_glyph_info_t   info,
2554
             FT_Face face,
2555
             const cairo_color_t        *foreground_color,
2556
             cairo_bool_t vertical_layout,
2557
             int load_flags)
2558
25.6k
{
2559
25.6k
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
2560
25.6k
    FT_GlyphSlot glyph;
2561
25.6k
    cairo_status_t status;
2562
25.6k
    cairo_image_surface_t *surface;
2563
25.6k
    cairo_bool_t uses_foreground_color = FALSE;
2564
2565
    /* Only one info type at a time handled in this function */
2566
25.6k
    assert (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE || info == CAIRO_SCALED_GLYPH_INFO_SURFACE);
2567
2568
25.6k
    if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) {
2569
0
  if (!unscaled->have_color) {
2570
0
      scaled_glyph->color_glyph = FALSE;
2571
0
      scaled_glyph->color_glyph_set = TRUE;
2572
0
      return CAIRO_INT_STATUS_UNSUPPORTED;
2573
0
  }
2574
2575
0
  uses_foreground_color = _cairo_ft_scaled_glyph_set_foreground_color (scaled_font,
2576
0
                       scaled_glyph,
2577
0
                       face,
2578
0
                       foreground_color);
2579
0
  _cairo_ft_scaled_glyph_set_palette (scaled_font, face, NULL, NULL);
2580
2581
0
        load_flags &= ~FT_LOAD_MONOCHROME;
2582
  /* clear load target mode */
2583
0
  load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(load_flags)));
2584
0
  load_flags |= FT_LOAD_TARGET_NORMAL;
2585
0
  load_flags |= FT_LOAD_COLOR;
2586
25.6k
    } else { /* info == CAIRO_SCALED_GLYPH_INFO_SURFACE */
2587
25.6k
        load_flags &= ~FT_LOAD_COLOR;
2588
25.6k
    }
2589
2590
25.6k
    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
2591
25.6k
            scaled_glyph,
2592
25.6k
            face,
2593
25.6k
            load_flags,
2594
25.6k
            FALSE,
2595
25.6k
            vertical_layout);
2596
25.6k
    if (unlikely (status))
2597
0
  return status;
2598
2599
25.6k
    glyph = face->glyph;
2600
2601
25.6k
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_OUTLINE) {
2602
2603
25.6k
  status = _render_glyph_outline (face, &scaled_font->ft_options.base,
2604
25.6k
              &surface);
2605
25.6k
    } else {
2606
0
  status = _render_glyph_bitmap (face, &scaled_font->ft_options.base,
2607
0
             &surface);
2608
0
  if (likely (status == CAIRO_STATUS_SUCCESS) && unscaled->have_shape) {
2609
0
      status = _transform_glyph_bitmap (&unscaled->current_shape,
2610
0
                &surface);
2611
0
      if (unlikely (status))
2612
0
    cairo_surface_destroy (&surface->base);
2613
0
  }
2614
0
    }
2615
2616
25.6k
    if (unlikely (status))
2617
10
  return status;
2618
2619
25.5k
    if (info == CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) {
2620
  /* We tried loading a color glyph and can now check if we got
2621
   * a color glyph and set scaled_glyph->color_glyph
2622
   * accordingly */
2623
0
  if (pixman_image_get_format (surface->pixman_image) == PIXMAN_a8r8g8b8 &&
2624
0
      !pixman_image_get_component_alpha (surface->pixman_image))
2625
0
  {
2626
0
      _cairo_scaled_glyph_set_color_surface (scaled_glyph,
2627
0
               &scaled_font->base,
2628
0
               surface,
2629
0
               uses_foreground_color ? foreground_color : NULL);
2630
2631
0
      scaled_glyph->color_glyph = TRUE;
2632
0
  } else {
2633
      /* We didn't ask for a non-color surface, but store it
2634
       * anyway so we don't have to load it again. */
2635
0
      _cairo_scaled_glyph_set_surface (scaled_glyph,
2636
0
               &scaled_font->base,
2637
0
               surface);
2638
0
      scaled_glyph->color_glyph = FALSE;
2639
0
      status = CAIRO_INT_STATUS_UNSUPPORTED;
2640
0
  }
2641
0
  scaled_glyph->color_glyph_set = TRUE;
2642
25.5k
    } else { /* info == CAIRO_SCALED_GLYPH_INFO_SURFACE */
2643
25.5k
  _cairo_scaled_glyph_set_surface (scaled_glyph,
2644
25.5k
           &scaled_font->base,
2645
25.5k
           surface);
2646
25.5k
    }
2647
2648
25.5k
    return status;
2649
25.6k
}
2650
2651
static cairo_int_status_t
2652
_cairo_ft_scaled_glyph_init_record_colr_v0_glyph (cairo_ft_scaled_font_t *scaled_font,
2653
              cairo_scaled_glyph_t   *scaled_glyph,
2654
              FT_Face                 face,
2655
              cairo_bool_t            vertical_layout,
2656
              int                     load_flags)
2657
0
{
2658
0
    cairo_surface_t *recording_surface;
2659
0
    cairo_t *cr;
2660
0
    cairo_status_t status;
2661
0
    FT_Color *palette;
2662
0
    unsigned int num_palette_entries;
2663
0
    FT_LayerIterator iterator;
2664
0
    FT_UInt layer_glyph_index;
2665
0
    FT_UInt layer_color_index;
2666
0
    cairo_path_fixed_t *path_fixed;
2667
0
    cairo_path_t *path;
2668
2669
0
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
2670
2671
0
    load_flags &= ~FT_LOAD_MONOCHROME;
2672
    /* clear load target mode */
2673
0
    load_flags &= ~(FT_LOAD_TARGET_(FT_LOAD_TARGET_MODE(load_flags)));
2674
0
    load_flags |= FT_LOAD_TARGET_NORMAL;
2675
0
    load_flags |= FT_LOAD_COLOR;
2676
2677
0
    recording_surface =
2678
0
  cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
2679
2680
0
    cr = cairo_create (recording_surface);
2681
2682
0
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
2683
0
        cairo_matrix_t scale;
2684
0
  scale = scaled_font->base.scale;
2685
0
  scale.x0 = scale.y0 = 0.;
2686
0
  cairo_set_matrix (cr, &scale);
2687
0
    }
2688
2689
0
    iterator.p  = NULL;
2690
0
    while (FT_Get_Color_Glyph_Layer(face,
2691
0
            _cairo_scaled_glyph_index (scaled_glyph),
2692
0
            &layer_glyph_index,
2693
0
            &layer_color_index,
2694
0
            &iterator))
2695
0
    {
2696
0
  cairo_pattern_t *pattern;
2697
0
  if (layer_color_index == 0xFFFF) {
2698
0
      pattern = _cairo_pattern_create_foreground_marker ();
2699
0
  } else {
2700
0
      double r = 0, g = 0, b = 0, a = 1;
2701
0
      if (layer_color_index <  num_palette_entries) {
2702
0
    FT_Color *color = &palette[layer_color_index];
2703
0
    r = color->red / 255.0;
2704
0
    g = color->green/ 255.0;
2705
0
    b = color->blue / 255.0;
2706
0
    a = color->alpha / 255.0;
2707
0
      }
2708
0
      pattern = cairo_pattern_create_rgba (r, g, b, a);
2709
0
  }
2710
0
  cairo_set_source (cr, pattern);
2711
0
  cairo_pattern_destroy (pattern);
2712
2713
0
  if (FT_Load_Glyph (face, layer_glyph_index, load_flags) != 0) {
2714
0
      status = CAIRO_INT_STATUS_UNSUPPORTED;
2715
0
      goto cleanup;
2716
0
  }
2717
2718
0
  status = _cairo_ft_face_decompose_glyph_outline (face, &path_fixed);
2719
0
  if (unlikely (status))
2720
0
      return status;
2721
2722
0
  path = _cairo_path_create (path_fixed, cr);
2723
0
  _cairo_path_fixed_destroy (path_fixed);
2724
0
  cairo_append_path(cr, path);
2725
0
  cairo_path_destroy (path);
2726
0
  cairo_fill (cr);
2727
0
    }
2728
2729
0
  cleanup:
2730
0
    cairo_destroy (cr);
2731
2732
0
    if (status) {
2733
0
  cairo_surface_destroy (recording_surface);
2734
0
  return status;
2735
0
    }
2736
2737
0
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
2738
0
                 &scaled_font->base,
2739
0
                 recording_surface,
2740
0
                 NULL);
2741
0
    return status;
2742
0
}
2743
2744
static cairo_int_status_t
2745
_cairo_ft_scaled_glyph_init_record_colr_v1_glyph (cairo_ft_scaled_font_t *scaled_font,
2746
              cairo_scaled_glyph_t   *scaled_glyph,
2747
              FT_Face                 face,
2748
              const cairo_color_t    *foreground_color,
2749
              cairo_text_extents_t   *extents)
2750
0
{
2751
0
#if HAVE_FT_COLR_V1
2752
0
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
2753
0
    cairo_surface_t *recording_surface;
2754
0
    cairo_t *cr;
2755
0
    FT_Color *palette;
2756
0
    unsigned int num_palette_entries;
2757
0
    cairo_bool_t foreground_source_used = FALSE;
2758
2759
0
    recording_surface =
2760
0
  cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
2761
2762
0
    cairo_surface_set_device_scale (recording_surface, 1, -1);
2763
2764
0
    cr = cairo_create (recording_surface);
2765
2766
0
    cairo_set_font_size (cr, 1.0);
2767
0
    cairo_set_font_options (cr, &scaled_font->base.options);
2768
2769
0
    extents->x_bearing = DOUBLE_FROM_26_6(face->bbox.xMin);
2770
0
    extents->y_bearing = DOUBLE_FROM_26_6(face->bbox.yMin);
2771
0
    extents->width = DOUBLE_FROM_26_6(face->bbox.xMax) - extents->x_bearing;
2772
0
    extents->height = DOUBLE_FROM_26_6(face->bbox.yMax) - extents->y_bearing;
2773
2774
0
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
2775
2776
0
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
2777
0
  cairo_pattern_t *foreground_pattern = _cairo_pattern_create_solid (foreground_color);
2778
0
  status = _cairo_render_colr_v1_glyph (face,
2779
0
                _cairo_scaled_glyph_index (scaled_glyph),
2780
0
                                              palette,
2781
0
                                              num_palette_entries,
2782
0
                cr,
2783
0
                foreground_pattern,
2784
0
                &foreground_source_used);
2785
0
  cairo_pattern_destroy (foreground_pattern);
2786
0
  if (status == CAIRO_STATUS_SUCCESS)
2787
0
      status = cairo_status (cr);
2788
0
    }
2789
2790
0
    cairo_destroy (cr);
2791
2792
0
    if (status) {
2793
0
  cairo_surface_destroy (recording_surface);
2794
0
  scaled_glyph->color_glyph = FALSE;
2795
0
  scaled_glyph->color_glyph_set = TRUE;
2796
0
  return status;
2797
0
    }
2798
2799
0
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
2800
0
                 &scaled_font->base,
2801
0
                 recording_surface,
2802
0
                 foreground_source_used ? foreground_color : NULL);
2803
2804
0
    scaled_glyph->color_glyph = TRUE;
2805
0
    scaled_glyph->color_glyph_set = TRUE;
2806
2807
    /* get metrics */
2808
2809
    /* Copied from cairo-user-font.c */
2810
0
    cairo_matrix_t extent_scale;
2811
0
    double extent_x_scale = 1.0;
2812
0
    double extent_y_scale = 1.0;
2813
0
    double snap_x_scale;
2814
0
    double snap_y_scale;
2815
0
    double fixed_scale, x_scale, y_scale;
2816
2817
0
    extent_scale = scaled_font->base.scale_inverse;
2818
0
    snap_x_scale = 1.0;
2819
0
    snap_y_scale = 1.0;
2820
0
    status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
2821
0
              &x_scale, &y_scale,
2822
0
              1);
2823
0
    if (status == CAIRO_STATUS_SUCCESS) {
2824
0
  if (x_scale == 0)
2825
0
      x_scale = 1;
2826
0
  if (y_scale == 0)
2827
0
      y_scale = 1;
2828
2829
0
  snap_x_scale = x_scale;
2830
0
  snap_y_scale = y_scale;
2831
2832
  /* since glyphs are pretty much 1.0x1.0, we can reduce error by
2833
   * scaling to a larger square.  say, 1024.x1024. */
2834
0
  fixed_scale = 1024;
2835
0
  x_scale /= fixed_scale;
2836
0
  y_scale /= fixed_scale;
2837
2838
0
  cairo_matrix_scale (&extent_scale, 1.0 / x_scale, 1.0 / y_scale);
2839
2840
0
  extent_x_scale = x_scale;
2841
0
  extent_y_scale = y_scale;
2842
0
    }
2843
2844
0
    {
2845
  /* compute width / height */
2846
0
  cairo_box_t bbox;
2847
0
  double x1, y1, x2, y2;
2848
0
  double x_scale, y_scale;
2849
2850
  /* Compute extents.x/y/width/height from recording_surface,
2851
   * in font space.
2852
   */
2853
0
  status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
2854
0
                &bbox,
2855
0
                &extent_scale);
2856
0
  if (unlikely (status))
2857
0
      return status;
2858
2859
0
  _cairo_box_to_doubles (&bbox, &x1, &y1, &x2, &y2);
2860
2861
0
  x_scale = extent_x_scale;
2862
0
  y_scale = extent_y_scale;
2863
0
  extents->x_bearing = x1 * x_scale;
2864
0
  extents->y_bearing = y1 * y_scale;
2865
0
  extents->width     = (x2 - x1) * x_scale;
2866
0
  extents->height    = (y2 - y1) * y_scale;
2867
0
    }
2868
2869
0
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF) {
2870
0
  extents->x_advance = _cairo_lround (extents->x_advance / snap_x_scale) * snap_x_scale;
2871
0
  extents->y_advance = _cairo_lround (extents->y_advance / snap_y_scale) * snap_y_scale;
2872
0
    }
2873
2874
0
    return status;
2875
#else
2876
    return CAIRO_INT_STATUS_UNSUPPORTED;
2877
#endif
2878
0
}
2879
2880
static cairo_int_status_t
2881
_cairo_ft_scaled_glyph_init_record_svg_glyph (cairo_ft_scaled_font_t *scaled_font,
2882
                cairo_scaled_glyph_t   *scaled_glyph,
2883
                FT_Face                 face,
2884
                const cairo_color_t    *foreground_color,
2885
                cairo_text_extents_t   *extents)
2886
0
{
2887
0
#if HAVE_FT_SVG_DOCUMENT
2888
0
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
2889
0
    cairo_surface_t *recording_surface;
2890
0
    cairo_t *cr;
2891
0
    FT_SVG_Document svg_doc = face->glyph->other;
2892
0
    char *svg_document;
2893
0
    FT_Color *palette;
2894
0
    unsigned int num_palette_entries;
2895
0
    cairo_bool_t foreground_source_used = FALSE;
2896
2897
    /* Create NULL terminated SVG document */
2898
0
    svg_document = _cairo_strndup ((const char*)svg_doc->svg_document, svg_doc->svg_document_length);
2899
2900
0
    recording_surface =
2901
0
  cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
2902
2903
0
    cr = cairo_create (recording_surface);
2904
2905
0
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
2906
0
        cairo_matrix_t scale;
2907
0
  scale = scaled_font->base.scale;
2908
0
  scale.x0 = scale.y0 = 0.;
2909
0
  cairo_set_matrix (cr, &scale);
2910
0
    }
2911
2912
0
    cairo_set_font_size (cr, 1.0);
2913
0
    cairo_set_font_options (cr, &scaled_font->base.options);
2914
2915
0
    extents->x_bearing = DOUBLE_FROM_26_6(face->bbox.xMin);
2916
0
    extents->y_bearing = DOUBLE_FROM_26_6(face->bbox.yMin);
2917
0
    extents->width = DOUBLE_FROM_26_6(face->bbox.xMax) - extents->x_bearing;
2918
0
    extents->height = DOUBLE_FROM_26_6(face->bbox.yMax) - extents->y_bearing;
2919
2920
0
    _cairo_ft_scaled_glyph_set_palette (scaled_font, face, &num_palette_entries, &palette);
2921
2922
0
    if (!_cairo_matrix_is_scale_0 (&scaled_font->base.scale)) {
2923
0
  cairo_pattern_t *foreground_pattern = _cairo_pattern_create_solid (foreground_color);
2924
0
  status = _cairo_render_svg_glyph (svg_document,
2925
0
            svg_doc->start_glyph_id,
2926
0
            svg_doc->end_glyph_id,
2927
0
            _cairo_scaled_glyph_index(scaled_glyph),
2928
0
            svg_doc->units_per_EM,
2929
0
            palette,
2930
0
            num_palette_entries,
2931
0
            cr,
2932
0
            foreground_pattern,
2933
0
            &foreground_source_used);
2934
0
  cairo_pattern_destroy (foreground_pattern);
2935
0
  if (status == CAIRO_STATUS_SUCCESS)
2936
0
      status = cairo_status (cr);
2937
0
    }
2938
2939
0
    cairo_destroy (cr);
2940
0
    free (svg_document);
2941
2942
0
    if (status) {
2943
0
  cairo_surface_destroy (recording_surface);
2944
0
  scaled_glyph->color_glyph = FALSE;
2945
0
  scaled_glyph->color_glyph_set = TRUE;
2946
0
  return status;
2947
0
    }
2948
2949
0
    _cairo_scaled_glyph_set_recording_surface (scaled_glyph,
2950
0
                 &scaled_font->base,
2951
0
                 recording_surface,
2952
0
                 foreground_source_used ? foreground_color : NULL);
2953
2954
0
    scaled_glyph->color_glyph = TRUE;
2955
0
    scaled_glyph->color_glyph_set = TRUE;
2956
2957
    /* get metrics */
2958
2959
    /* Copied from cairo-user-font.c */
2960
0
    cairo_matrix_t extent_scale;
2961
0
    double extent_x_scale;
2962
0
    double extent_y_scale;
2963
0
    double snap_x_scale;
2964
0
    double snap_y_scale;
2965
0
    double fixed_scale, x_scale, y_scale;
2966
2967
0
    extent_scale = scaled_font->base.scale_inverse;
2968
0
    snap_x_scale = 1.0;
2969
0
    snap_y_scale = 1.0;
2970
0
    status = _cairo_matrix_compute_basis_scale_factors (&extent_scale,
2971
0
              &x_scale, &y_scale,
2972
0
              1);
2973
0
    if (status == CAIRO_STATUS_SUCCESS) {
2974
0
  if (x_scale == 0)
2975
0
      x_scale = 1;
2976
0
  if (y_scale == 0)
2977
0
      y_scale = 1;
2978
2979
0
  snap_x_scale = x_scale;
2980
0
  snap_y_scale = y_scale;
2981
2982
  /* since glyphs are pretty much 1.0x1.0, we can reduce error by
2983
   * scaling to a larger square.  say, 1024.x1024. */
2984
0
  fixed_scale = 1024;
2985
0
  x_scale /= fixed_scale;
2986
0
  y_scale /= fixed_scale;
2987
2988
0
  cairo_matrix_scale (&extent_scale, 1.0 / x_scale, 1.0 / y_scale);
2989
2990
0
  extent_x_scale = x_scale;
2991
0
  extent_y_scale = y_scale;
2992
0
    }
2993
2994
0
    {
2995
  /* compute width / height */
2996
0
  cairo_box_t bbox;
2997
0
  double x1, y1, x2, y2;
2998
0
  double x_scale, y_scale;
2999
3000
  /* Compute extents.x/y/width/height from recording_surface,
3001
   * in font space.
3002
   */
3003
0
  status = _cairo_recording_surface_get_bbox ((cairo_recording_surface_t *) recording_surface,
3004
0
                &bbox,
3005
0
                &extent_scale);
3006
0
  if (unlikely (status))
3007
0
      return status;
3008
3009
0
  _cairo_box_to_doubles (&bbox, &x1, &y1, &x2, &y2);
3010
3011
0
  x_scale = extent_x_scale;
3012
0
  y_scale = extent_y_scale;
3013
0
  extents->x_bearing = x1 * x_scale;
3014
0
  extents->y_bearing = y1 * y_scale;
3015
0
  extents->width     = (x2 - x1) * x_scale;
3016
0
  extents->height    = (y2 - y1) * y_scale;
3017
0
    }
3018
3019
0
    if (scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF) {
3020
0
  extents->x_advance = _cairo_lround (extents->x_advance / snap_x_scale) * snap_x_scale;
3021
0
  extents->y_advance = _cairo_lround (extents->y_advance / snap_y_scale) * snap_y_scale;
3022
0
    }
3023
3024
0
    return status;
3025
#else
3026
    return CAIRO_INT_STATUS_UNSUPPORTED;
3027
#endif
3028
0
}
3029
3030
static cairo_int_status_t
3031
_cairo_ft_scaled_glyph_init_surface_for_recording_surface (cairo_ft_scaled_font_t *scaled_font,
3032
                 cairo_scaled_glyph_t   *scaled_glyph,
3033
                 const cairo_color_t    *foreground_color)
3034
0
{
3035
0
    cairo_surface_t *surface;
3036
0
    int width, height;
3037
0
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
3038
0
    cairo_bool_t foreground_used;
3039
3040
0
    width = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.x) -
3041
0
  _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.x);
3042
0
    height = _cairo_fixed_integer_ceil (scaled_glyph->bbox.p2.y) -
3043
0
  _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y);
3044
3045
0
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
3046
3047
0
    cairo_surface_set_device_offset (surface,
3048
0
             - _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.x),
3049
0
             - _cairo_fixed_integer_floor (scaled_glyph->bbox.p1.y));
3050
3051
0
    status = _cairo_recording_surface_replay_with_foreground_color (scaled_glyph->recording_surface,
3052
0
                    surface,
3053
0
                    foreground_color,
3054
0
                    &foreground_used);
3055
0
    if (unlikely (status)) {
3056
0
  cairo_surface_destroy(surface);
3057
0
  return status;
3058
0
    }
3059
3060
0
    _cairo_scaled_glyph_set_color_surface (scaled_glyph,
3061
0
             &scaled_font->base,
3062
0
             (cairo_image_surface_t *)surface,
3063
0
             foreground_used ? foreground_color : NULL);
3064
0
    surface = NULL;
3065
3066
0
    if (surface)
3067
0
  cairo_surface_destroy (surface);
3068
3069
0
    return status;
3070
0
}
3071
3072
static void
3073
_cairo_ft_scaled_glyph_get_metrics (cairo_ft_scaled_font_t     *scaled_font,
3074
            FT_Face face,
3075
            cairo_bool_t vertical_layout,
3076
            int load_flags,
3077
            cairo_text_extents_t *fs_metrics)
3078
32.2k
{
3079
32.2k
    FT_Glyph_Metrics *metrics;
3080
32.2k
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3081
32.2k
    cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
3082
32.2k
    FT_GlyphSlot glyph = face->glyph;
3083
3084
    /*
3085
     * Compute font-space metrics
3086
     */
3087
32.2k
    metrics = &glyph->metrics;
3088
3089
    /*
3090
     * Note: Y coordinates of the horizontal bearing need to be negated.
3091
     *
3092
     * Scale metrics back to glyph space from the scaled glyph space returned
3093
     * by FreeType
3094
     *
3095
     * If we want hinted metrics but aren't asking for hinted glyphs from
3096
     * FreeType, then we need to do the metric hinting ourselves.
3097
     */
3098
3099
32.2k
    if (hint_metrics && (load_flags & FT_LOAD_NO_HINTING))
3100
0
    {
3101
0
  FT_Pos x1, x2;
3102
0
  FT_Pos y1, y2;
3103
0
  FT_Pos advance;
3104
3105
0
  if (!vertical_layout) {
3106
0
      x1 = (metrics->horiBearingX) & -64;
3107
0
      x2 = (metrics->horiBearingX + metrics->width + 63) & -64;
3108
0
      y1 = (-metrics->horiBearingY) & -64;
3109
0
      y2 = (-metrics->horiBearingY + metrics->height + 63) & -64;
3110
3111
0
      advance = ((metrics->horiAdvance + 32) & -64);
3112
3113
0
      fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (x1), unscaled->x_scale);
3114
0
      fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (y1), unscaled->y_scale);
3115
3116
0
      fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (x2 - x1), unscaled->x_scale);
3117
0
      fs_metrics->height  = SCALE (DOUBLE_FROM_26_6 (y2 - y1), unscaled->y_scale);
3118
3119
0
      fs_metrics->x_advance = SCALE (DOUBLE_FROM_26_6 (advance), unscaled->x_scale);
3120
0
      fs_metrics->y_advance = 0;
3121
0
  } else {
3122
0
      x1 = (metrics->vertBearingX) & -64;
3123
0
      x2 = (metrics->vertBearingX + metrics->width + 63) & -64;
3124
0
      y1 = (metrics->vertBearingY) & -64;
3125
0
      y2 = (metrics->vertBearingY + metrics->height + 63) & -64;
3126
3127
0
      advance = ((metrics->vertAdvance + 32) & -64);
3128
3129
0
      fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (x1), unscaled->x_scale);
3130
0
      fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (y1), unscaled->y_scale);
3131
3132
0
      fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (x2 - x1), unscaled->x_scale);
3133
0
      fs_metrics->height  = SCALE (DOUBLE_FROM_26_6 (y2 - y1), unscaled->y_scale);
3134
3135
0
      fs_metrics->x_advance = 0;
3136
0
      fs_metrics->y_advance = SCALE (DOUBLE_FROM_26_6 (advance), unscaled->y_scale);
3137
0
  }
3138
32.2k
    } else {
3139
32.2k
  fs_metrics->width  = SCALE (DOUBLE_FROM_26_6 (metrics->width), unscaled->x_scale);
3140
32.2k
  fs_metrics->height = SCALE (DOUBLE_FROM_26_6 (metrics->height), unscaled->y_scale);
3141
3142
32.2k
  if (!vertical_layout) {
3143
32.2k
      fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->horiBearingX), unscaled->x_scale);
3144
32.2k
      fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (-metrics->horiBearingY), unscaled->y_scale);
3145
3146
32.2k
      if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
3147
1.11k
    fs_metrics->x_advance = SCALE (DOUBLE_FROM_26_6 (metrics->horiAdvance), unscaled->x_scale);
3148
31.0k
      else
3149
31.0k
    fs_metrics->x_advance = SCALE (DOUBLE_FROM_16_16 (glyph->linearHoriAdvance), unscaled->x_scale);
3150
32.2k
      fs_metrics->y_advance = 0;
3151
32.2k
  } else {
3152
0
      fs_metrics->x_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->vertBearingX), unscaled->x_scale);
3153
0
      fs_metrics->y_bearing = SCALE (DOUBLE_FROM_26_6 (metrics->vertBearingY), unscaled->y_scale);
3154
3155
0
      fs_metrics->x_advance = 0;
3156
0
      if (hint_metrics || glyph->format != FT_GLYPH_FORMAT_OUTLINE)
3157
0
    fs_metrics->y_advance = SCALE (DOUBLE_FROM_26_6 (metrics->vertAdvance), unscaled->y_scale);
3158
0
      else
3159
0
    fs_metrics->y_advance = SCALE (DOUBLE_FROM_16_16 (glyph->linearVertAdvance), unscaled->y_scale);
3160
0
  }
3161
32.2k
    }
3162
32.2k
}
3163
3164
static cairo_bool_t
3165
_cairo_ft_scaled_glyph_is_colr_v0 (cairo_ft_scaled_font_t *scaled_font,
3166
           cairo_scaled_glyph_t   *scaled_glyph,
3167
           FT_Face                 face)
3168
0
{
3169
0
    FT_LayerIterator  iterator;
3170
0
    FT_UInt layer_glyph_index;
3171
0
    FT_UInt layer_color_index;
3172
3173
0
    iterator.p  = NULL;
3174
0
    if (FT_Get_Color_Glyph_Layer(face,
3175
0
                                 _cairo_scaled_glyph_index (scaled_glyph),
3176
0
                                 &layer_glyph_index,
3177
0
                                 &layer_color_index,
3178
0
         &iterator) == 1)
3179
0
    {
3180
0
  return TRUE;
3181
0
    }
3182
3183
0
    return FALSE;
3184
0
}
3185
3186
static cairo_bool_t
3187
_cairo_ft_scaled_glyph_is_colr_v1 (cairo_ft_scaled_font_t *scaled_font,
3188
           cairo_scaled_glyph_t   *scaled_glyph,
3189
           FT_Face                 face)
3190
0
{
3191
0
#if HAVE_FT_COLR_V1
3192
0
    FT_OpaquePaint paint = { NULL, 0 };
3193
3194
0
    if (FT_Get_Color_Glyph_Paint (face,
3195
0
          _cairo_scaled_glyph_index (scaled_glyph),
3196
0
          FT_COLOR_INCLUDE_ROOT_TRANSFORM,
3197
0
          &paint) == 1)
3198
0
    {
3199
0
  return TRUE;
3200
0
    }
3201
0
#endif
3202
0
    return FALSE;
3203
0
}
3204
3205
static cairo_int_status_t
3206
_cairo_ft_scaled_glyph_init_metrics (cairo_ft_scaled_font_t  *scaled_font,
3207
             cairo_scaled_glyph_t    *scaled_glyph,
3208
             FT_Face                  face,
3209
             cairo_bool_t             vertical_layout,
3210
             int                      load_flags,
3211
             const cairo_color_t     *foreground_color)
3212
32.2k
{
3213
32.2k
    cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
3214
32.2k
    cairo_text_extents_t fs_metrics;
3215
32.2k
    cairo_ft_glyph_private_t *glyph_priv;
3216
3217
32.2k
    cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF;
3218
3219
    /* _cairo_ft_scaled_glyph_init_metrics() is called once the first
3220
     * time a cairo_scaled_glyph_t is created. We first allocate the
3221
     * cairo_ft_glyph_private_t struct and determine the glyph type.
3222
     */
3223
3224
32.2k
    glyph_priv = _cairo_calloc (sizeof (*glyph_priv));
3225
32.2k
    if (unlikely (glyph_priv == NULL))
3226
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3227
3228
32.2k
    _cairo_scaled_glyph_attach_private (scaled_glyph, &glyph_priv->base,
3229
32.2k
          &ft_glyph_private_key,
3230
32.2k
          _cairo_ft_glyph_fini);
3231
3232
    /* We need to load color to determine if this is a color format. */
3233
32.2k
    int color_flag = 0;
3234
3235
32.2k
    if (scaled_font->unscaled->have_color && scaled_font->base.options.color_mode != CAIRO_COLOR_MODE_NO_COLOR)
3236
0
  color_flag = FT_LOAD_COLOR;
3237
3238
    /* Ensure use_em_size = FALSE as the format (bitmap or outline)
3239
     * may change with the size. */
3240
32.2k
    status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
3241
32.2k
            scaled_glyph,
3242
32.2k
            face,
3243
32.2k
            load_flags | color_flag,
3244
32.2k
            FALSE,
3245
32.2k
            vertical_layout);
3246
32.2k
    if (unlikely (status))
3247
0
  return status;
3248
3249
32.2k
    cairo_bool_t is_svg_format = FALSE;
3250
32.2k
#if HAVE_FT_SVG_DOCUMENT
3251
32.2k
    if (face->glyph->format == FT_GLYPH_FORMAT_SVG)
3252
0
  is_svg_format = TRUE;
3253
32.2k
#endif
3254
3255
32.2k
    if (is_svg_format) {
3256
0
        glyph_priv->format = CAIRO_FT_GLYPH_TYPE_SVG;
3257
3258
0
#if defined(HAVE_FT_COLR_V1) && defined(HAVE_FT_LOAD_NO_SVG)
3259
        /* Prefer COLRv1 table over SVG table due to performance reasons for now */
3260
0
        if (_cairo_ft_scaled_glyph_is_colr_v1 (scaled_font, scaled_glyph, face)) {
3261
0
            glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V1;
3262
0
        }
3263
0
#endif
3264
3265
32.2k
    } else if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE) {
3266
32.2k
  glyph_priv->format = CAIRO_FT_GLYPH_TYPE_OUTLINE;
3267
32.2k
  if (color_flag) {
3268
0
      if (_cairo_ft_scaled_glyph_is_colr_v1 (scaled_font, scaled_glyph, face))
3269
0
    glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V1;
3270
0
      else if (_cairo_ft_scaled_glyph_is_colr_v0 (scaled_font, scaled_glyph, face))
3271
0
    glyph_priv->format = CAIRO_FT_GLYPH_TYPE_COLR_V0;
3272
0
  }
3273
32.2k
    } else {
3274
  /* For anything else we let FreeType render a bitmap. */
3275
0
   glyph_priv->format =  CAIRO_FT_GLYPH_TYPE_BITMAP;
3276
0
    }
3277
3278
    /* If hinting is off, load the glyph with font size set the the em size. */
3279
32.2k
    if (!hint_metrics) {
3280
31.0k
  status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
3281
31.0k
                scaled_glyph,
3282
31.0k
                face,
3283
31.0k
                load_flags | color_flag,
3284
31.0k
                TRUE,
3285
31.0k
                vertical_layout);
3286
31.0k
  if (unlikely (status))
3287
0
      return status;
3288
31.0k
    }
3289
3290
32.2k
    _cairo_ft_scaled_glyph_get_metrics (scaled_font,
3291
32.2k
          face,
3292
32.2k
          vertical_layout,
3293
32.2k
          load_flags,
3294
32.2k
          &fs_metrics);
3295
3296
3297
    /* SVG and COLRv1 glyphs require the bounding box to be obtained
3298
     * from the ink extents of the rendering. We need to render glyph
3299
     * to a recording surface to obtain these extents. But we also
3300
     * need the advance from _cairo_ft_scaled_glyph_get_metrics()
3301
     * before calling this function.
3302
     */
3303
3304
32.2k
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG) {
3305
0
  status = (cairo_int_status_t)_cairo_ft_scaled_glyph_init_record_svg_glyph (scaled_font,
3306
0
                       scaled_glyph,
3307
0
                       face,
3308
0
                       foreground_color,
3309
0
                       &fs_metrics);
3310
0
  if (unlikely (status))
3311
0
      return status;
3312
0
    }
3313
3314
32.2k
    if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1) {
3315
  /* Restore font size if previously loaded at em_size. */
3316
0
  if (!hint_metrics) {
3317
0
      status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
3318
0
              scaled_glyph,
3319
0
              face,
3320
0
              load_flags | color_flag,
3321
0
              FALSE,
3322
0
              vertical_layout);
3323
0
      if (unlikely (status))
3324
0
    return status;
3325
0
  }
3326
3327
0
  status = (cairo_int_status_t)_cairo_ft_scaled_glyph_init_record_colr_v1_glyph (scaled_font,
3328
0
                           scaled_glyph,
3329
0
                           face,
3330
0
                           foreground_color,
3331
0
                           &fs_metrics);
3332
0
  if (unlikely (status))
3333
0
      return status;
3334
0
    }
3335
3336
32.2k
    _cairo_scaled_glyph_set_metrics (scaled_glyph,
3337
32.2k
             &scaled_font->base,
3338
32.2k
             &fs_metrics);
3339
3340
32.2k
    return status;
3341
32.2k
}
3342
3343
static cairo_int_status_t
3344
_cairo_ft_scaled_glyph_init (void     *abstract_font,
3345
           cairo_scaled_glyph_t *scaled_glyph,
3346
           cairo_scaled_glyph_info_t   info,
3347
           const cairo_color_t        *foreground_color)
3348
38.8k
{
3349
38.8k
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3350
38.8k
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3351
38.8k
    FT_Face face;
3352
38.8k
    int load_flags = scaled_font->ft_options.load_flags;
3353
38.8k
    cairo_bool_t vertical_layout = FALSE;
3354
38.8k
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
3355
38.8k
    cairo_ft_glyph_private_t *glyph_priv;
3356
3357
38.8k
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3358
38.8k
    if (!face)
3359
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3360
3361
    /* Ignore global advance unconditionally */
3362
38.8k
    load_flags |= FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH;
3363
3364
38.8k
    if ((info & CAIRO_SCALED_GLYPH_INFO_PATH) != 0 &&
3365
0
  (info & (CAIRO_SCALED_GLYPH_INFO_SURFACE |
3366
0
                 CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE)) == 0) {
3367
0
  load_flags |= FT_LOAD_NO_BITMAP;
3368
0
    }
3369
3370
    /*
3371
     * Don't pass FT_LOAD_VERTICAL_LAYOUT to FT_Load_Glyph here as
3372
     * suggested by freetype people.
3373
     */
3374
38.8k
    if (load_flags & FT_LOAD_VERTICAL_LAYOUT) {
3375
0
  load_flags &= ~FT_LOAD_VERTICAL_LAYOUT;
3376
0
  vertical_layout = TRUE;
3377
0
    }
3378
3379
    /* Metrics will always be requested when a scaled glyph is created */
3380
38.8k
    if (info & CAIRO_SCALED_GLYPH_INFO_METRICS) {
3381
32.2k
  status = _cairo_ft_scaled_glyph_init_metrics (scaled_font,
3382
32.2k
                  scaled_glyph,
3383
32.2k
                  face,
3384
32.2k
                  vertical_layout,
3385
32.2k
                  load_flags,
3386
32.2k
                  foreground_color);
3387
32.2k
  if (unlikely (status))
3388
0
      goto FAIL;
3389
32.2k
    }
3390
3391
38.8k
    glyph_priv = (cairo_ft_glyph_private_t *) _cairo_scaled_glyph_find_private (scaled_glyph,
3392
38.8k
                    &ft_glyph_private_key);
3393
38.8k
    assert (glyph_priv != NULL);
3394
3395
38.8k
    if (info & CAIRO_SCALED_GLYPH_INFO_RECORDING_SURFACE) {
3396
0
  status = CAIRO_INT_STATUS_UNSUPPORTED;
3397
0
  if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG ||
3398
0
      glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V0 ||
3399
0
      glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
3400
0
  {
3401
0
      status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
3402
0
              scaled_glyph,
3403
0
              face,
3404
0
              load_flags | FT_LOAD_COLOR,
3405
0
              FALSE,
3406
0
              vertical_layout);
3407
0
      if (unlikely (status))
3408
0
    goto FAIL;
3409
3410
0
      if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG) {
3411
0
    status = _cairo_ft_scaled_glyph_init_record_svg_glyph (scaled_font,
3412
0
                       scaled_glyph,
3413
0
                       face,
3414
0
                       foreground_color,
3415
0
                       &scaled_glyph->fs_metrics);
3416
0
      } else if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1) {
3417
0
    status = _cairo_ft_scaled_glyph_init_record_colr_v1_glyph (scaled_font,
3418
0
                     scaled_glyph,
3419
0
                     face,
3420
0
                     foreground_color,
3421
0
                     &scaled_glyph->fs_metrics);
3422
0
      } else if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V0) {
3423
0
    status = _cairo_ft_scaled_glyph_init_record_colr_v0_glyph (scaled_font,
3424
0
                     scaled_glyph,
3425
0
                     face,
3426
0
                     vertical_layout,
3427
0
                     load_flags);
3428
0
      }
3429
0
  }
3430
0
  if (status)
3431
0
      goto FAIL;
3432
0
    }
3433
3434
38.8k
    if ((info & CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE) && scaled_font->base.options.color_mode != CAIRO_COLOR_MODE_NO_COLOR) {
3435
0
  if (glyph_priv->format == CAIRO_FT_GLYPH_TYPE_SVG ||
3436
0
      glyph_priv->format == CAIRO_FT_GLYPH_TYPE_COLR_V1)
3437
0
  {
3438
0
      status = _cairo_ft_scaled_glyph_init_surface_for_recording_surface (scaled_font,
3439
0
                    scaled_glyph,
3440
0
                    foreground_color);
3441
0
  } else {
3442
0
      status = _cairo_ft_scaled_glyph_init_surface (scaled_font,
3443
0
                scaled_glyph,
3444
0
                glyph_priv,
3445
0
                CAIRO_SCALED_GLYPH_INFO_COLOR_SURFACE,
3446
0
                face,
3447
0
                foreground_color,
3448
0
                vertical_layout,
3449
0
                load_flags);
3450
0
  }
3451
0
  if (unlikely (status))
3452
0
      goto FAIL;
3453
0
    }
3454
3455
38.8k
    if (info & CAIRO_SCALED_GLYPH_INFO_SURFACE) {
3456
25.6k
  status = _cairo_ft_scaled_glyph_init_surface (scaled_font,
3457
25.6k
                  scaled_glyph,
3458
25.6k
                  glyph_priv,
3459
25.6k
                  CAIRO_SCALED_GLYPH_INFO_SURFACE,
3460
25.6k
                  face,
3461
25.6k
                  NULL, /* foreground color */
3462
25.6k
                  vertical_layout,
3463
25.6k
                  load_flags);
3464
25.6k
  if (unlikely (status))
3465
10
      goto FAIL;
3466
25.6k
    }
3467
3468
38.8k
    if (info & CAIRO_SCALED_GLYPH_INFO_PATH) {
3469
0
  cairo_path_fixed_t *path = NULL; /* hide compiler warning */
3470
3471
  /* Load non-color glyph */
3472
0
  status = _cairo_ft_scaled_glyph_load_glyph (scaled_font,
3473
0
                scaled_glyph,
3474
0
                face,
3475
0
                load_flags,
3476
0
                FALSE,
3477
0
                vertical_layout);
3478
0
  if (unlikely (status))
3479
0
      goto FAIL;
3480
3481
0
  if (face->glyph->format == FT_GLYPH_FORMAT_OUTLINE)
3482
0
      status = _cairo_ft_face_decompose_glyph_outline (face, &path);
3483
0
  else
3484
0
      status = CAIRO_INT_STATUS_UNSUPPORTED;
3485
3486
0
  if (unlikely (status))
3487
0
      goto FAIL;
3488
3489
0
  _cairo_scaled_glyph_set_path (scaled_glyph,
3490
0
              &scaled_font->base,
3491
0
              path);
3492
0
    }
3493
38.8k
 FAIL:
3494
38.8k
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3495
3496
38.8k
    return status;
3497
38.8k
}
3498
3499
static unsigned long
3500
_cairo_ft_ucs4_to_index (void     *abstract_font,
3501
       uint32_t    ucs4)
3502
0
{
3503
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3504
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3505
0
    FT_Face face;
3506
0
    FT_UInt index;
3507
3508
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3509
0
    if (!face)
3510
0
  return 0;
3511
3512
0
#if CAIRO_HAS_FC_FONT
3513
0
    index = FcFreeTypeCharIndex (face, ucs4);
3514
#else
3515
    index = FT_Get_Char_Index (face, ucs4);
3516
#endif
3517
3518
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3519
0
    return index;
3520
0
}
3521
3522
static cairo_int_status_t
3523
_cairo_ft_load_truetype_table (void        *abstract_font,
3524
                              unsigned long     tag,
3525
                              long              offset,
3526
                              unsigned char    *buffer,
3527
                              unsigned long    *length)
3528
0
{
3529
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3530
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3531
0
    FT_Face face;
3532
0
    cairo_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
3533
3534
    /* We don't support the FreeType feature of loading a table
3535
     * without specifying the size since this may overflow our
3536
     * buffer. */
3537
0
    assert (length != NULL);
3538
3539
0
    if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
3540
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
3541
3542
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3543
0
    if (!face)
3544
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3545
3546
0
    if (FT_IS_SFNT (face)) {
3547
0
  if (buffer == NULL)
3548
0
      *length = 0;
3549
3550
0
  if (FT_Load_Sfnt_Table (face, tag, offset, buffer, length) == 0)
3551
0
      status = CAIRO_STATUS_SUCCESS;
3552
0
    }
3553
3554
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3555
3556
0
    return status;
3557
0
}
3558
3559
static cairo_int_status_t
3560
_cairo_ft_index_to_ucs4(void          *abstract_font,
3561
      unsigned long    index,
3562
      uint32_t  *ucs4)
3563
0
{
3564
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3565
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3566
0
    FT_Face face;
3567
0
    FT_ULong  charcode;
3568
0
    FT_UInt   gindex;
3569
3570
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3571
0
    if (!face)
3572
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3573
3574
0
    *ucs4 = (uint32_t) -1;
3575
0
    charcode = FT_Get_First_Char(face, &gindex);
3576
0
    while (gindex != 0) {
3577
0
  if (gindex == index) {
3578
0
      *ucs4 = charcode;
3579
0
      break;
3580
0
  }
3581
0
  charcode = FT_Get_Next_Char (face, charcode, &gindex);
3582
0
    }
3583
3584
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3585
3586
0
    return CAIRO_STATUS_SUCCESS;
3587
0
}
3588
3589
static cairo_int_status_t
3590
_cairo_ft_is_synthetic (void          *abstract_font,
3591
      cairo_bool_t    *is_synthetic)
3592
0
{
3593
0
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
3594
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3595
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3596
0
    FT_Face face;
3597
0
    FT_Error error;
3598
3599
0
    if (scaled_font->ft_options.synth_flags != 0) {
3600
0
  *is_synthetic = TRUE;
3601
0
  return status;
3602
0
    }
3603
3604
0
    *is_synthetic = FALSE;
3605
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3606
0
    if (!face)
3607
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3608
3609
0
    if (face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
3610
0
  FT_MM_Var *mm_var = NULL;
3611
0
  FT_Fixed *coords = NULL;
3612
0
  int num_axis;
3613
0
  int i;
3614
3615
  /* If this is an MM or variable font we can't assume the current outlines
3616
   * are the same as the font tables */
3617
0
  *is_synthetic = TRUE;
3618
3619
0
  error = FT_Get_MM_Var (face, &mm_var);
3620
0
  if (error) {
3621
0
      status = _cairo_error (_cairo_ft_to_cairo_error (error));
3622
0
      goto cleanup;
3623
0
  }
3624
3625
0
  num_axis = mm_var->num_axis;
3626
0
  coords = _cairo_malloc_ab (num_axis, sizeof(FT_Fixed));
3627
0
  if (!coords) {
3628
0
      status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3629
0
      goto cleanup;
3630
0
  }
3631
3632
  /* Check if the current design coordinates are the default
3633
   * coordinates. In this case the current outlines match the
3634
   * font tables.
3635
   */
3636
0
  FT_Get_Var_Blend_Coordinates (face, num_axis, coords);
3637
0
  *is_synthetic = FALSE;
3638
0
  for (i = 0; i < num_axis; i++) {
3639
0
      if (coords[i]) {
3640
0
    *is_synthetic = TRUE;
3641
0
    break;
3642
0
      }
3643
0
  }
3644
3645
0
      cleanup:
3646
0
  free (coords);
3647
0
  FT_Done_MM_Var (face->glyph->library, mm_var);
3648
0
    }
3649
3650
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3651
3652
0
    return status;
3653
0
}
3654
3655
static cairo_int_status_t
3656
_cairo_index_to_glyph_name (void           *abstract_font,
3657
          char                **glyph_names,
3658
          int                   num_glyph_names,
3659
          unsigned long         glyph_index,
3660
          unsigned long        *glyph_array_index)
3661
0
{
3662
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3663
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3664
0
    FT_Face face;
3665
0
    char buffer[256]; /* PLRM specifies max name length of 127 */
3666
0
    FT_Error error;
3667
0
    int i;
3668
3669
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3670
0
    if (!face)
3671
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3672
3673
0
    error = FT_Get_Glyph_Name (face, glyph_index, buffer, sizeof buffer);
3674
3675
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3676
3677
0
    if (error != FT_Err_Ok) {
3678
  /* propagate fatal errors from FreeType */
3679
0
  if (error == FT_Err_Out_Of_Memory)
3680
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3681
3682
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
3683
0
    }
3684
3685
    /* FT first numbers the glyphs in the order they are read from the
3686
     * Type 1 font. Then if .notdef is not the first glyph, the first
3687
     * glyph is swapped with .notdef to ensure that .notdef is at
3688
     * glyph index 0.
3689
     *
3690
     * As all but two glyphs in glyph_names already have the same
3691
     * index as the FT glyph index, we first check if
3692
     * glyph_names[glyph_index] is the name we are looking for. If not
3693
     * we fall back to searching the entire array.
3694
     */
3695
3696
0
    if ((long)glyph_index < num_glyph_names &&
3697
0
  strcmp (glyph_names[glyph_index], buffer) == 0)
3698
0
    {
3699
0
  *glyph_array_index = glyph_index;
3700
3701
0
  return CAIRO_STATUS_SUCCESS;
3702
0
    }
3703
3704
0
    for (i = 0; i < num_glyph_names; i++) {
3705
0
  if (strcmp (glyph_names[i], buffer) == 0) {
3706
0
      *glyph_array_index = i;
3707
3708
0
      return CAIRO_STATUS_SUCCESS;
3709
0
  }
3710
0
    }
3711
3712
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
3713
0
}
3714
3715
static cairo_bool_t
3716
_ft_is_type1 (FT_Face face)
3717
0
{
3718
0
    const char *font_format = FT_Get_Font_Format (face);
3719
0
    if (font_format &&
3720
0
  (strcmp (font_format, "Type 1") == 0 ||
3721
0
   strcmp (font_format, "CFF") == 0))
3722
0
    {
3723
0
  return TRUE;
3724
0
    }
3725
3726
0
    return FALSE;
3727
0
}
3728
3729
static cairo_int_status_t
3730
_cairo_ft_load_type1_data (void             *abstract_font,
3731
         long              offset,
3732
         unsigned char    *buffer,
3733
         unsigned long    *length)
3734
0
{
3735
0
    cairo_ft_scaled_font_t *scaled_font = abstract_font;
3736
0
    cairo_ft_unscaled_font_t *unscaled = scaled_font->unscaled;
3737
0
    FT_Face face;
3738
0
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
3739
0
    unsigned long available_length;
3740
0
    unsigned long ret;
3741
3742
0
    assert (length != NULL);
3743
3744
0
    if (_cairo_ft_scaled_font_is_vertical (&scaled_font->base))
3745
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
3746
3747
0
    face = _cairo_ft_unscaled_font_lock_face (unscaled);
3748
0
    if (!face)
3749
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3750
3751
0
    if (FT_IS_SFNT (face)) {
3752
0
  status = CAIRO_INT_STATUS_UNSUPPORTED;
3753
0
  goto unlock;
3754
0
    }
3755
3756
0
    if (! _ft_is_type1 (face)) {
3757
0
        status = CAIRO_INT_STATUS_UNSUPPORTED;
3758
0
  goto unlock;
3759
0
    }
3760
3761
0
    available_length = MAX (face->stream->size - offset, 0);
3762
0
    if (!buffer) {
3763
0
  *length = available_length;
3764
0
    } else {
3765
0
  if (*length > available_length) {
3766
0
      status = CAIRO_INT_STATUS_UNSUPPORTED;
3767
0
  } else if (face->stream->read != NULL) {
3768
      /* Note that read() may be implemented as a macro, thanks POSIX!, so we
3769
       * need to wrap the following usage in parentheses in order to
3770
       * disambiguate it for the pre-processor - using the verbose function
3771
       * pointer dereference for clarity.
3772
       */
3773
0
      ret = (* face->stream->read) (face->stream,
3774
0
            offset,
3775
0
            buffer,
3776
0
            *length);
3777
0
      if (ret != *length)
3778
0
    status = _cairo_error (CAIRO_STATUS_READ_ERROR);
3779
0
  } else {
3780
0
      memcpy (buffer, face->stream->base + offset, *length);
3781
0
  }
3782
0
    }
3783
3784
0
  unlock:
3785
0
    _cairo_ft_unscaled_font_unlock_face (unscaled);
3786
3787
0
    return status;
3788
0
}
3789
3790
static cairo_bool_t
3791
_cairo_ft_has_color_glyphs (void *scaled)
3792
219k
{
3793
219k
    cairo_ft_unscaled_font_t *unscaled = ((cairo_ft_scaled_font_t *)scaled)->unscaled;
3794
3795
219k
    if (!unscaled->have_color_set) {
3796
0
  FT_Face face;
3797
0
  face = _cairo_ft_unscaled_font_lock_face (unscaled);
3798
0
  if (unlikely (face == NULL))
3799
0
      return FALSE;
3800
0
  _cairo_ft_unscaled_font_unlock_face (unscaled);
3801
0
    }
3802
3803
219k
    return unscaled->have_color;
3804
219k
}
3805
3806
static const cairo_scaled_font_backend_t _cairo_ft_scaled_font_backend = {
3807
    CAIRO_FONT_TYPE_FT,
3808
    _cairo_ft_scaled_font_fini,
3809
    _cairo_ft_scaled_glyph_init,
3810
    NULL,     /* text_to_glyphs */
3811
    _cairo_ft_ucs4_to_index,
3812
    _cairo_ft_load_truetype_table,
3813
    _cairo_ft_index_to_ucs4,
3814
    _cairo_ft_is_synthetic,
3815
    _cairo_index_to_glyph_name,
3816
    _cairo_ft_load_type1_data,
3817
    _cairo_ft_has_color_glyphs
3818
};
3819
3820
/* #cairo_ft_font_face_t */
3821
3822
#if CAIRO_HAS_FC_FONT
3823
static cairo_font_face_t *
3824
_cairo_ft_font_face_create_for_pattern (FcPattern *pattern);
3825
3826
static cairo_status_t
3827
_cairo_ft_font_face_create_for_toy (cairo_toy_font_face_t *toy_face,
3828
            cairo_font_face_t **font_face_out)
3829
0
{
3830
0
    cairo_font_face_t *font_face = (cairo_font_face_t *) &_cairo_font_face_nil;
3831
0
    FcPattern *pattern;
3832
0
    int fcslant;
3833
0
    int fcweight;
3834
3835
0
    pattern = FcPatternCreate ();
3836
0
    if (!pattern) {
3837
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3838
0
  return font_face->status;
3839
0
    }
3840
3841
0
    if (!FcPatternAddString (pattern,
3842
0
                 FC_FAMILY, (unsigned char *) toy_face->family))
3843
0
    {
3844
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3845
0
  goto FREE_PATTERN;
3846
0
    }
3847
3848
0
    switch (toy_face->slant)
3849
0
    {
3850
0
    case CAIRO_FONT_SLANT_ITALIC:
3851
0
        fcslant = FC_SLANT_ITALIC;
3852
0
        break;
3853
0
    case CAIRO_FONT_SLANT_OBLIQUE:
3854
0
  fcslant = FC_SLANT_OBLIQUE;
3855
0
        break;
3856
0
    case CAIRO_FONT_SLANT_NORMAL:
3857
0
    default:
3858
0
        fcslant = FC_SLANT_ROMAN;
3859
0
        break;
3860
0
    }
3861
3862
0
    if (!FcPatternAddInteger (pattern, FC_SLANT, fcslant)) {
3863
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3864
0
  goto FREE_PATTERN;
3865
0
    }
3866
3867
0
    switch (toy_face->weight)
3868
0
    {
3869
0
    case CAIRO_FONT_WEIGHT_BOLD:
3870
0
        fcweight = FC_WEIGHT_BOLD;
3871
0
        break;
3872
0
    case CAIRO_FONT_WEIGHT_NORMAL:
3873
0
    default:
3874
0
        fcweight = FC_WEIGHT_MEDIUM;
3875
0
        break;
3876
0
    }
3877
3878
0
    if (!FcPatternAddInteger (pattern, FC_WEIGHT, fcweight)) {
3879
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3880
0
  goto FREE_PATTERN;
3881
0
    }
3882
3883
0
    font_face = _cairo_ft_font_face_create_for_pattern (pattern);
3884
3885
0
 FREE_PATTERN:
3886
0
    FcPatternDestroy (pattern);
3887
3888
0
    *font_face_out = font_face;
3889
0
    return font_face->status;
3890
0
}
3891
#endif
3892
3893
static cairo_bool_t
3894
_cairo_ft_font_face_destroy (void *abstract_face)
3895
0
{
3896
0
    cairo_ft_font_face_t *font_face = abstract_face;
3897
3898
    /* When destroying a face created by cairo_ft_font_face_create_for_ft_face,
3899
     * we have a special "zombie" state for the face when the unscaled font
3900
     * is still alive but there are no other references to a font face with
3901
     * the same FT_Face.
3902
     *
3903
     * We go from:
3904
     *
3905
     *   font_face ------> unscaled
3906
     *        <-....weak....../
3907
     *
3908
     * To:
3909
     *
3910
     *    font_face <------- unscaled
3911
     */
3912
3913
0
    if (font_face->unscaled &&
3914
0
  font_face->unscaled->from_face &&
3915
0
  font_face->next == NULL &&
3916
0
  font_face->unscaled->faces == font_face &&
3917
0
  CAIRO_REFERENCE_COUNT_GET_VALUE (&font_face->unscaled->base.ref_count) > 1)
3918
0
    {
3919
0
  _cairo_unscaled_font_destroy (&font_face->unscaled->base);
3920
0
  font_face->unscaled = NULL;
3921
3922
0
  return FALSE;
3923
0
    }
3924
3925
0
    if (font_face->unscaled) {
3926
0
  cairo_ft_font_face_t *tmp_face = NULL;
3927
0
  cairo_ft_font_face_t *last_face = NULL;
3928
3929
  /* Remove face from linked list */
3930
0
  for (tmp_face = font_face->unscaled->faces;
3931
0
       tmp_face;
3932
0
       tmp_face = tmp_face->next)
3933
0
  {
3934
0
      if (tmp_face == font_face) {
3935
0
    if (last_face)
3936
0
        last_face->next = tmp_face->next;
3937
0
    else
3938
0
        font_face->unscaled->faces = tmp_face->next;
3939
0
      }
3940
3941
0
      last_face = tmp_face;
3942
0
  }
3943
3944
0
  _cairo_unscaled_font_destroy (&font_face->unscaled->base);
3945
0
  font_face->unscaled = NULL;
3946
0
    }
3947
3948
0
    _cairo_ft_options_fini (&font_face->ft_options);
3949
3950
0
#if CAIRO_HAS_FC_FONT
3951
0
    if (font_face->pattern) {
3952
0
  FcPatternDestroy (font_face->pattern);
3953
0
  cairo_font_face_destroy (font_face->resolved_font_face);
3954
0
    }
3955
0
#endif
3956
3957
0
    return TRUE;
3958
0
}
3959
3960
static cairo_font_face_t *
3961
_cairo_ft_font_face_get_implementation (void                     *abstract_face,
3962
          const cairo_matrix_t       *font_matrix,
3963
          const cairo_matrix_t       *ctm,
3964
          const cairo_font_options_t *options)
3965
91
{
3966
    /* The handling of font options is different depending on how the
3967
     * font face was created. When the user creates a font face with
3968
     * cairo_ft_font_face_create_for_ft_face(), then the load flags
3969
     * passed in augment the load flags for the options.  But for
3970
     * cairo_ft_font_face_create_for_pattern(), the load flags are
3971
     * derived from a pattern where the user has called
3972
     * cairo_ft_font_options_substitute(), so *just* use those load
3973
     * flags and ignore the options.
3974
     */
3975
3976
91
#if CAIRO_HAS_FC_FONT
3977
91
    cairo_ft_font_face_t      *font_face = abstract_face;
3978
3979
    /* If we have an unresolved pattern, resolve it and create
3980
     * unscaled font.  Otherwise, use the ones stored in font_face.
3981
     */
3982
91
    if (font_face->pattern) {
3983
0
  cairo_font_face_t *resolved;
3984
3985
  /* Cache the resolved font whilst the FcConfig remains consistent. */
3986
0
  resolved = font_face->resolved_font_face;
3987
0
  if (resolved != NULL) {
3988
0
      if (! FcInitBringUptoDate ()) {
3989
0
    _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
3990
0
    return (cairo_font_face_t *) &_cairo_font_face_nil;
3991
0
      }
3992
3993
0
      if (font_face->resolved_config == FcConfigGetCurrent ())
3994
0
    return cairo_font_face_reference (resolved);
3995
3996
0
      cairo_font_face_destroy (resolved);
3997
0
      font_face->resolved_font_face = NULL;
3998
0
  }
3999
4000
0
  resolved = _cairo_ft_resolve_pattern (font_face->pattern,
4001
0
                font_matrix,
4002
0
                ctm,
4003
0
                options);
4004
0
  if (unlikely (resolved->status))
4005
0
      return resolved;
4006
4007
0
  font_face->resolved_font_face = cairo_font_face_reference (resolved);
4008
0
  font_face->resolved_config = FcConfigGetCurrent ();
4009
4010
0
  return resolved;
4011
0
    }
4012
91
#endif
4013
4014
91
    return abstract_face;
4015
91
}
4016
4017
const cairo_font_face_backend_t _cairo_ft_font_face_backend = {
4018
    CAIRO_FONT_TYPE_FT,
4019
#if CAIRO_HAS_FC_FONT
4020
    _cairo_ft_font_face_create_for_toy,
4021
#else
4022
    NULL,
4023
#endif
4024
    _cairo_ft_font_face_destroy,
4025
    _cairo_ft_font_face_scaled_font_create,
4026
    _cairo_ft_font_face_get_implementation
4027
};
4028
4029
#if CAIRO_HAS_FC_FONT
4030
static cairo_font_face_t *
4031
_cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
4032
0
{
4033
0
    cairo_ft_font_face_t *font_face;
4034
4035
0
    font_face = _cairo_calloc (sizeof (cairo_ft_font_face_t));
4036
0
    if (unlikely (font_face == NULL)) {
4037
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
4038
0
  return (cairo_font_face_t *) &_cairo_font_face_nil;
4039
0
    }
4040
4041
0
    font_face->unscaled = NULL;
4042
4043
0
    _get_pattern_ft_options (pattern, &font_face->ft_options);
4044
4045
0
    font_face->next = NULL;
4046
4047
0
    font_face->pattern = FcPatternDuplicate (pattern);
4048
0
    if (unlikely (font_face->pattern == NULL)) {
4049
0
  free (font_face);
4050
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
4051
0
  return (cairo_font_face_t *) &_cairo_font_face_nil;
4052
0
    }
4053
4054
0
    font_face->resolved_font_face = NULL;
4055
0
    font_face->resolved_config = NULL;
4056
4057
0
    _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
4058
4059
0
    return &font_face->base;
4060
0
}
4061
#endif
4062
4063
static cairo_font_face_t *
4064
_cairo_ft_font_face_create (cairo_ft_unscaled_font_t *unscaled,
4065
          cairo_ft_options_t       *ft_options)
4066
207
{
4067
207
    cairo_ft_font_face_t *font_face, **prev_font_face;
4068
4069
    /* Looked for an existing matching font face */
4070
207
    for (font_face = unscaled->faces, prev_font_face = &unscaled->faces;
4071
208
   font_face;
4072
207
   prev_font_face = &font_face->next, font_face = font_face->next)
4073
202
    {
4074
202
  if (font_face->ft_options.load_flags == ft_options->load_flags &&
4075
202
      font_face->ft_options.synth_flags == ft_options->synth_flags &&
4076
202
      cairo_font_options_equal (&font_face->ft_options.base, &ft_options->base))
4077
201
  {
4078
201
      if (font_face->base.status) {
4079
    /* The font_face has been left in an error state, abandon it. */
4080
0
    *prev_font_face = font_face->next;
4081
0
    break;
4082
0
      }
4083
4084
201
      if (font_face->unscaled == NULL) {
4085
    /* Resurrect this "zombie" font_face (from
4086
     * _cairo_ft_font_face_destroy), switching its unscaled_font
4087
     * from owner to ownee. */
4088
0
    font_face->unscaled = unscaled;
4089
0
    _cairo_unscaled_font_reference (&unscaled->base);
4090
0
    return &font_face->base;
4091
0
      } else
4092
201
    return cairo_font_face_reference (&font_face->base);
4093
201
  }
4094
202
    }
4095
4096
    /* No match found, create a new one */
4097
6
    font_face = _cairo_calloc (sizeof (cairo_ft_font_face_t));
4098
6
    if (unlikely (!font_face)) {
4099
0
  _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
4100
0
  return (cairo_font_face_t *)&_cairo_font_face_nil;
4101
0
    }
4102
4103
6
    font_face->unscaled = unscaled;
4104
6
    _cairo_unscaled_font_reference (&unscaled->base);
4105
4106
6
    _cairo_ft_options_init_copy (&font_face->ft_options, ft_options);
4107
4108
6
    if (unscaled->faces && unscaled->faces->unscaled == NULL) {
4109
  /* This "zombie" font_face (from _cairo_ft_font_face_destroy)
4110
   * is no longer needed. */
4111
0
  assert (unscaled->from_face && unscaled->faces->next == NULL);
4112
0
  cairo_font_face_destroy (&unscaled->faces->base);
4113
0
  unscaled->faces = NULL;
4114
0
    }
4115
4116
6
    font_face->next = unscaled->faces;
4117
6
    unscaled->faces = font_face;
4118
4119
6
#if CAIRO_HAS_FC_FONT
4120
6
    font_face->pattern = NULL;
4121
6
#endif
4122
4123
6
    _cairo_font_face_init (&font_face->base, &_cairo_ft_font_face_backend);
4124
4125
6
    return &font_face->base;
4126
6
}
4127
4128
/* implement the platform-specific interface */
4129
4130
#if CAIRO_HAS_FC_FONT
4131
static cairo_status_t
4132
_cairo_ft_font_options_substitute (const cairo_font_options_t *options,
4133
           FcPattern                  *pattern)
4134
66
{
4135
66
    FcValue v;
4136
4137
66
    if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
4138
66
    {
4139
66
  if (FcPatternGet (pattern, FC_ANTIALIAS, 0, &v) == FcResultNoMatch)
4140
66
  {
4141
66
      if (! FcPatternAddBool (pattern,
4142
66
                  FC_ANTIALIAS,
4143
66
            options->antialias != CAIRO_ANTIALIAS_NONE))
4144
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4145
4146
66
      if (options->antialias != CAIRO_ANTIALIAS_SUBPIXEL) {
4147
66
    FcPatternDel (pattern, FC_RGBA);
4148
66
    if (! FcPatternAddInteger (pattern, FC_RGBA, FC_RGBA_NONE))
4149
0
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4150
66
      }
4151
66
  }
4152
66
    }
4153
4154
66
    if (options->antialias != CAIRO_ANTIALIAS_DEFAULT)
4155
66
    {
4156
66
  if (FcPatternGet (pattern, FC_RGBA, 0, &v) == FcResultNoMatch)
4157
0
  {
4158
0
      int rgba;
4159
4160
0
      if (options->antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
4161
0
    switch (options->subpixel_order) {
4162
0
    case CAIRO_SUBPIXEL_ORDER_DEFAULT:
4163
0
    case CAIRO_SUBPIXEL_ORDER_RGB:
4164
0
    default:
4165
0
        rgba = FC_RGBA_RGB;
4166
0
        break;
4167
0
    case CAIRO_SUBPIXEL_ORDER_BGR:
4168
0
        rgba = FC_RGBA_BGR;
4169
0
        break;
4170
0
    case CAIRO_SUBPIXEL_ORDER_VRGB:
4171
0
        rgba = FC_RGBA_VRGB;
4172
0
        break;
4173
0
    case CAIRO_SUBPIXEL_ORDER_VBGR:
4174
0
        rgba = FC_RGBA_VBGR;
4175
0
        break;
4176
0
    }
4177
0
      } else {
4178
0
    rgba = FC_RGBA_NONE;
4179
0
      }
4180
4181
0
      if (! FcPatternAddInteger (pattern, FC_RGBA, rgba))
4182
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4183
0
  }
4184
66
    }
4185
4186
66
    if (options->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
4187
0
    {
4188
0
  if (FcPatternGet (pattern, FC_LCD_FILTER, 0, &v) == FcResultNoMatch)
4189
0
  {
4190
0
      int lcd_filter;
4191
4192
0
      switch (options->lcd_filter) {
4193
0
      case CAIRO_LCD_FILTER_NONE:
4194
0
    lcd_filter = FT_LCD_FILTER_NONE;
4195
0
    break;
4196
0
      case CAIRO_LCD_FILTER_INTRA_PIXEL:
4197
0
    lcd_filter = FT_LCD_FILTER_LEGACY;
4198
0
    break;
4199
0
      case CAIRO_LCD_FILTER_FIR3:
4200
0
    lcd_filter = FT_LCD_FILTER_LIGHT;
4201
0
    break;
4202
0
      default:
4203
0
      case CAIRO_LCD_FILTER_DEFAULT:
4204
0
      case CAIRO_LCD_FILTER_FIR5:
4205
0
    lcd_filter = FT_LCD_FILTER_DEFAULT;
4206
0
    break;
4207
0
      }
4208
4209
0
      if (! FcPatternAddInteger (pattern, FC_LCD_FILTER, lcd_filter))
4210
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4211
0
  }
4212
0
    }
4213
4214
66
    if (options->hint_style != CAIRO_HINT_STYLE_DEFAULT)
4215
0
    {
4216
0
  if (FcPatternGet (pattern, FC_HINTING, 0, &v) == FcResultNoMatch)
4217
0
  {
4218
0
      if (! FcPatternAddBool (pattern,
4219
0
                  FC_HINTING,
4220
0
            options->hint_style != CAIRO_HINT_STYLE_NONE))
4221
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4222
0
  }
4223
4224
0
  if (FcPatternGet (pattern, FC_HINT_STYLE, 0, &v) == FcResultNoMatch)
4225
0
  {
4226
0
      int hint_style;
4227
4228
0
      switch (options->hint_style) {
4229
0
      case CAIRO_HINT_STYLE_NONE:
4230
0
    hint_style = FC_HINT_NONE;
4231
0
    break;
4232
0
      case CAIRO_HINT_STYLE_SLIGHT:
4233
0
    hint_style = FC_HINT_SLIGHT;
4234
0
    break;
4235
0
      case CAIRO_HINT_STYLE_MEDIUM:
4236
0
    hint_style = FC_HINT_MEDIUM;
4237
0
    break;
4238
0
      case CAIRO_HINT_STYLE_FULL:
4239
0
      case CAIRO_HINT_STYLE_DEFAULT:
4240
0
      default:
4241
0
    hint_style = FC_HINT_FULL;
4242
0
    break;
4243
0
      }
4244
4245
0
      if (! FcPatternAddInteger (pattern, FC_HINT_STYLE, hint_style))
4246
0
    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
4247
0
  }
4248
0
    }
4249
4250
66
    return CAIRO_STATUS_SUCCESS;
4251
66
}
4252
4253
/**
4254
 * cairo_ft_font_options_substitute:
4255
 * @options: a #cairo_font_options_t object
4256
 * @pattern: an existing #FcPattern
4257
 *
4258
 * Add options to a #FcPattern based on a #cairo_font_options_t font
4259
 * options object. Options that are already in the pattern, are not overridden,
4260
 * so you should call this function after calling FcConfigSubstitute() (the
4261
 * user's settings should override options based on the surface type), but
4262
 * before calling FcDefaultSubstitute().
4263
 *
4264
 * Since: 1.0
4265
 **/
4266
void
4267
cairo_ft_font_options_substitute (const cairo_font_options_t *options,
4268
          FcPattern                  *pattern)
4269
66
{
4270
66
    if (cairo_font_options_status ((cairo_font_options_t *) options))
4271
0
  return;
4272
4273
66
    _cairo_ft_font_options_substitute (options, pattern);
4274
66
}
4275
4276
static cairo_font_face_t *
4277
_cairo_ft_resolve_pattern (FcPattern          *pattern,
4278
         const cairo_matrix_t       *font_matrix,
4279
         const cairo_matrix_t       *ctm,
4280
         const cairo_font_options_t *font_options)
4281
0
{
4282
0
    cairo_status_t status;
4283
4284
0
    cairo_matrix_t scale;
4285
0
    FcPattern *resolved;
4286
0
    cairo_ft_font_transform_t sf;
4287
0
    FcResult result;
4288
0
    cairo_ft_unscaled_font_t *unscaled;
4289
0
    cairo_ft_options_t ft_options;
4290
0
    cairo_font_face_t *font_face;
4291
4292
0
    scale = *ctm;
4293
0
    scale.x0 = scale.y0 = 0;
4294
0
    cairo_matrix_multiply (&scale,
4295
0
                           font_matrix,
4296
0
                           &scale);
4297
4298
0
    status = _compute_transform (&sf, &scale, NULL);
4299
0
    if (unlikely (status))
4300
0
  return (cairo_font_face_t *)&_cairo_font_face_nil;
4301
4302
0
    pattern = FcPatternDuplicate (pattern);
4303
0
    if (pattern == NULL)
4304
0
  return (cairo_font_face_t *)&_cairo_font_face_nil;
4305
4306
0
    if (! FcPatternAddDouble (pattern, FC_PIXEL_SIZE, sf.y_scale)) {
4307
0
  font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
4308
0
  goto FREE_PATTERN;
4309
0
    }
4310
4311
0
    if (! FcConfigSubstitute (NULL, pattern, FcMatchPattern)) {
4312
0
  font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
4313
0
  goto FREE_PATTERN;
4314
0
    }
4315
4316
0
    status = _cairo_ft_font_options_substitute (font_options, pattern);
4317
0
    if (status) {
4318
0
  font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
4319
0
  goto FREE_PATTERN;
4320
0
    }
4321
4322
0
    FcDefaultSubstitute (pattern);
4323
4324
0
    status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
4325
0
    if (unlikely (status)) {
4326
0
  font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
4327
0
  goto FREE_PATTERN;
4328
0
    }
4329
4330
0
    if (unscaled == NULL) {
4331
0
  resolved = FcFontMatch (NULL, pattern, &result);
4332
0
  if (!resolved) {
4333
      /* We failed to find any font. Substitute twin so that the user can
4334
       * see something (and hopefully recognise that the font is missing)
4335
       * and not just receive a NO_MEMORY error during rendering.
4336
       */
4337
0
      font_face = _cairo_font_face_twin_create_fallback ();
4338
0
      goto FREE_PATTERN;
4339
0
  }
4340
4341
0
  status = _cairo_ft_unscaled_font_create_for_pattern (resolved, &unscaled);
4342
0
  if (unlikely (status || unscaled == NULL)) {
4343
0
      font_face = (cairo_font_face_t *)&_cairo_font_face_nil;
4344
0
      goto FREE_RESOLVED;
4345
0
  }
4346
0
    } else
4347
0
  resolved = pattern;
4348
4349
0
    _get_pattern_ft_options (resolved, &ft_options);
4350
0
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
4351
0
     _cairo_ft_options_fini (&ft_options);
4352
0
    _cairo_unscaled_font_destroy (&unscaled->base);
4353
4354
0
FREE_RESOLVED:
4355
0
    if (resolved != pattern)
4356
0
  FcPatternDestroy (resolved);
4357
4358
0
FREE_PATTERN:
4359
0
    FcPatternDestroy (pattern);
4360
4361
0
    return font_face;
4362
0
}
4363
4364
/**
4365
 * cairo_ft_font_face_create_for_pattern:
4366
 * @pattern: A fontconfig pattern.  Cairo makes a copy of the pattern
4367
 * if it needs to.  You are free to modify or free @pattern after this call.
4368
 *
4369
 * Creates a new font face for the FreeType font backend based on a
4370
 * fontconfig pattern. This font can then be used with
4371
 * cairo_set_font_face() or cairo_scaled_font_create(). The
4372
 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
4373
 * also for the FreeType backend and can be used with functions such
4374
 * as cairo_ft_scaled_font_lock_face().
4375
 *
4376
 * Font rendering options are represented both here and when you
4377
 * call cairo_scaled_font_create(). Font options that have a representation
4378
 * in a #FcPattern must be passed in here; to modify #FcPattern
4379
 * appropriately to reflect the options in a #cairo_font_options_t, call
4380
 * cairo_ft_font_options_substitute().
4381
 *
4382
 * The pattern's FC_FT_FACE element is inspected first and if that is set,
4383
 * that will be the FreeType font face associated with the returned cairo
4384
 * font face.  Otherwise the FC_FILE element is checked.  If it's set,
4385
 * that and the value of the FC_INDEX element (defaults to zero) of @pattern
4386
 * are used to load a font face from file.
4387
 *
4388
 * If both steps from the previous paragraph fails, @pattern will be passed
4389
 * to FcConfigSubstitute, FcDefaultSubstitute, and finally FcFontMatch,
4390
 * and the resulting font pattern is used.
4391
 *
4392
 * If the FC_FT_FACE element of @pattern is set, the user is responsible
4393
 * for making sure that the referenced FT_Face remains valid for the life
4394
 * time of the returned #cairo_font_face_t.  See
4395
 * cairo_ft_font_face_create_for_ft_face() for an example of how to couple
4396
 * the life time of the FT_Face to that of the cairo font-face.
4397
 *
4398
 * Return value: a newly created #cairo_font_face_t. Free with
4399
 *  cairo_font_face_destroy() when you are done using it.
4400
 *
4401
 * Since: 1.0
4402
 **/
4403
cairo_font_face_t *
4404
cairo_ft_font_face_create_for_pattern (FcPattern *pattern)
4405
207
{
4406
207
    cairo_ft_unscaled_font_t *unscaled;
4407
207
    cairo_font_face_t *font_face;
4408
207
    cairo_ft_options_t ft_options;
4409
207
    cairo_status_t status;
4410
4411
207
    status = _cairo_ft_unscaled_font_create_for_pattern (pattern, &unscaled);
4412
207
    if (unlikely (status)) {
4413
0
      if (status == CAIRO_STATUS_FILE_NOT_FOUND)
4414
0
  return (cairo_font_face_t *) &_cairo_font_face_nil_file_not_found;
4415
0
      else
4416
0
  return (cairo_font_face_t *) &_cairo_font_face_nil;
4417
0
    }
4418
207
    if (unlikely (unscaled == NULL)) {
4419
  /* Store the pattern.  We will resolve it and create unscaled
4420
   * font when creating scaled fonts */
4421
0
  return _cairo_ft_font_face_create_for_pattern (pattern);
4422
0
    }
4423
4424
207
    _get_pattern_ft_options (pattern, &ft_options);
4425
207
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
4426
207
    _cairo_ft_options_fini (&ft_options);
4427
207
    _cairo_unscaled_font_destroy (&unscaled->base);
4428
4429
207
    return font_face;
4430
207
}
4431
#endif
4432
4433
/**
4434
 * cairo_ft_font_face_create_for_ft_face:
4435
 * @face: A FreeType face object, already opened. This must
4436
 *   be kept around until the face's ref_count drops to
4437
 *   zero and it is freed. Since the face may be referenced
4438
 *   internally to Cairo, the best way to determine when it
4439
 *   is safe to free the face is to pass a
4440
 *   #cairo_destroy_func_t to cairo_font_face_set_user_data()
4441
 * @load_flags: flags to pass to FT_Load_Glyph when loading
4442
 *   glyphs from the font. These flags are OR'ed together with
4443
 *   the flags derived from the #cairo_font_options_t passed
4444
 *   to cairo_scaled_font_create(), so only a few values such
4445
 *   as %FT_LOAD_VERTICAL_LAYOUT, and %FT_LOAD_FORCE_AUTOHINT
4446
 *   are useful. You should not pass any of the flags affecting
4447
 *   the load target, such as %FT_LOAD_TARGET_LIGHT.
4448
 *
4449
 * Creates a new font face for the FreeType font backend from a
4450
 * pre-opened FreeType face. This font can then be used with
4451
 * cairo_set_font_face() or cairo_scaled_font_create(). The
4452
 * #cairo_scaled_font_t returned from cairo_scaled_font_create() is
4453
 * also for the FreeType backend and can be used with functions such
4454
 * as cairo_ft_scaled_font_lock_face(). Note that Cairo may keep a reference
4455
 * to the FT_Face alive in a font-cache and the exact lifetime of the reference
4456
 * depends highly upon the exact usage pattern and is subject to external
4457
 * factors. You must not call FT_Done_Face() before the last reference to the
4458
 * #cairo_font_face_t has been dropped.
4459
 *
4460
 * As an example, below is how one might correctly couple the lifetime of
4461
 * the FreeType face object to the #cairo_font_face_t.
4462
 *
4463
 * <informalexample><programlisting>
4464
 * static const cairo_user_data_key_t key;
4465
 *
4466
 * font_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
4467
 * status = cairo_font_face_set_user_data (font_face, &key,
4468
 *                                ft_face, (cairo_destroy_func_t) FT_Done_Face);
4469
 * if (status) {
4470
 *    cairo_font_face_destroy (font_face);
4471
 *    FT_Done_Face (ft_face);
4472
 *    return ERROR;
4473
 * }
4474
 * </programlisting></informalexample>
4475
 *
4476
 * Return value: a newly created #cairo_font_face_t. Free with
4477
 *  cairo_font_face_destroy() when you are done using it.
4478
 *
4479
 * Since: 1.0
4480
 **/
4481
cairo_font_face_t *
4482
cairo_ft_font_face_create_for_ft_face (FT_Face         face,
4483
               int             load_flags)
4484
0
{
4485
0
    cairo_ft_unscaled_font_t *unscaled;
4486
0
    cairo_font_face_t *font_face;
4487
0
    cairo_ft_options_t ft_options;
4488
0
    cairo_status_t status;
4489
4490
0
    status = _cairo_ft_unscaled_font_create_from_face (face, &unscaled);
4491
0
    if (unlikely (status))
4492
0
  return (cairo_font_face_t *)&_cairo_font_face_nil;
4493
4494
0
    ft_options.load_flags = load_flags;
4495
0
    ft_options.synth_flags = 0;
4496
0
    _cairo_font_options_init_default (&ft_options.base);
4497
4498
0
    font_face = _cairo_ft_font_face_create (unscaled, &ft_options);
4499
0
    _cairo_unscaled_font_destroy (&unscaled->base);
4500
4501
0
    return font_face;
4502
0
}
4503
4504
/**
4505
 * cairo_ft_font_face_set_synthesize:
4506
 * @font_face: The #cairo_ft_font_face_t object to modify
4507
 * @synth_flags: the set of synthesis options to enable
4508
 *
4509
 * FreeType provides the ability to synthesize different glyphs from a base
4510
 * font, which is useful if you lack those glyphs from a true bold or oblique
4511
 * font. See also #cairo_ft_synthesize_t.
4512
 *
4513
 * Since: 1.12
4514
 **/
4515
void
4516
cairo_ft_font_face_set_synthesize (cairo_font_face_t *font_face,
4517
           unsigned int synth_flags)
4518
0
{
4519
0
    cairo_ft_font_face_t *ft;
4520
4521
0
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
4522
0
  return;
4523
4524
0
    ft = (cairo_ft_font_face_t *) font_face;
4525
0
    ft->ft_options.synth_flags |= synth_flags;
4526
0
}
4527
4528
/**
4529
 * cairo_ft_font_face_unset_synthesize:
4530
 * @font_face: The #cairo_ft_font_face_t object to modify
4531
 * @synth_flags: the set of synthesis options to disable
4532
 *
4533
 * See cairo_ft_font_face_set_synthesize().
4534
 *
4535
 * Since: 1.12
4536
 **/
4537
void
4538
cairo_ft_font_face_unset_synthesize (cairo_font_face_t *font_face,
4539
             unsigned int synth_flags)
4540
0
{
4541
0
    cairo_ft_font_face_t *ft;
4542
4543
0
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
4544
0
  return;
4545
4546
0
    ft = (cairo_ft_font_face_t *) font_face;
4547
0
    ft->ft_options.synth_flags &= ~synth_flags;
4548
0
}
4549
4550
/**
4551
 * cairo_ft_font_face_get_synthesize:
4552
 * @font_face: The #cairo_ft_font_face_t object to query
4553
 *
4554
 * See #cairo_ft_synthesize_t.
4555
 *
4556
 * Returns: the current set of synthesis options.
4557
 *
4558
 * Since: 1.12
4559
 **/
4560
unsigned int
4561
cairo_ft_font_face_get_synthesize (cairo_font_face_t *font_face)
4562
0
{
4563
0
    cairo_ft_font_face_t *ft;
4564
4565
0
    if (font_face->backend->type != CAIRO_FONT_TYPE_FT)
4566
0
  return 0;
4567
4568
0
    ft = (cairo_ft_font_face_t *) font_face;
4569
0
    return ft->ft_options.synth_flags;
4570
0
}
4571
4572
/**
4573
 * cairo_ft_scaled_font_lock_face:
4574
 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
4575
 *   object can be created by calling cairo_scaled_font_create() on a
4576
 *   FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
4577
 *   cairo_ft_font_face_create_for_ft_face()).
4578
 *
4579
 * cairo_ft_scaled_font_lock_face() gets the #FT_Face object from a FreeType
4580
 * backend font and scales it appropriately for the font and applies OpenType
4581
 * font variations if applicable. You must
4582
 * release the face with cairo_ft_scaled_font_unlock_face()
4583
 * when you are done using it.  Since the #FT_Face object can be
4584
 * shared between multiple #cairo_scaled_font_t objects, you must not
4585
 * lock any other font objects until you unlock this one. A count is
4586
 * kept of the number of times cairo_ft_scaled_font_lock_face() is
4587
 * called. cairo_ft_scaled_font_unlock_face() must be called the same number
4588
 * of times.
4589
 *
4590
 * You must be careful when using this function in a library or in a
4591
 * threaded application, because freetype's design makes it unsafe to
4592
 * call freetype functions simultaneously from multiple threads, (even
4593
 * if using distinct FT_Face objects). Because of this, application
4594
 * code that acquires an FT_Face object with this call must add its
4595
 * own locking to protect any use of that object, (and which also must
4596
 * protect any other calls into cairo as almost any cairo function
4597
 * might result in a call into the freetype library).
4598
 *
4599
 * Return value: The #FT_Face object for @font, scaled appropriately,
4600
 * or %NULL if @scaled_font is in an error state (see
4601
 * cairo_scaled_font_status()) or there is insufficient memory.
4602
 *
4603
 * Since: 1.0
4604
 **/
4605
FT_Face
4606
cairo_ft_scaled_font_lock_face (cairo_scaled_font_t *abstract_font)
4607
0
{
4608
0
    cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
4609
0
    FT_Face face;
4610
0
    cairo_status_t status;
4611
4612
0
    if (! _cairo_scaled_font_is_ft (abstract_font)) {
4613
0
  _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
4614
0
  return NULL;
4615
0
    }
4616
4617
0
    if (scaled_font->base.status)
4618
0
  return NULL;
4619
4620
0
    face = _cairo_ft_unscaled_font_lock_face (scaled_font->unscaled);
4621
0
    if (unlikely (face == NULL)) {
4622
0
  status = _cairo_scaled_font_set_error (&scaled_font->base, CAIRO_STATUS_NO_MEMORY);
4623
0
  return NULL;
4624
0
    }
4625
4626
0
    status = _cairo_ft_unscaled_font_set_scale (scaled_font->unscaled,
4627
0
                        &scaled_font->base.scale);
4628
0
    if (unlikely (status)) {
4629
0
  _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
4630
0
  status = _cairo_scaled_font_set_error (&scaled_font->base, status);
4631
0
  return NULL;
4632
0
    }
4633
4634
0
    cairo_ft_apply_variations (face, scaled_font);
4635
4636
    /* Note: We deliberately release the unscaled font's mutex here,
4637
     * so that we are not holding a lock across two separate calls to
4638
     * cairo function, (which would give the application some
4639
     * opportunity for creating deadlock. This is obviously unsafe,
4640
     * but as documented, the user must add manual locking when using
4641
     * this function. */
4642
0
     CAIRO_MUTEX_UNLOCK (scaled_font->unscaled->mutex);
4643
4644
0
    return face;
4645
0
}
4646
4647
/**
4648
 * cairo_ft_scaled_font_unlock_face:
4649
 * @scaled_font: A #cairo_scaled_font_t from the FreeType font backend. Such an
4650
 *   object can be created by calling cairo_scaled_font_create() on a
4651
 *   FreeType backend font face (see cairo_ft_font_face_create_for_pattern(),
4652
 *   cairo_ft_font_face_create_for_ft_face()).
4653
 *
4654
 * Releases a face obtained with cairo_ft_scaled_font_lock_face().
4655
 *
4656
 * Since: 1.0
4657
 **/
4658
void
4659
cairo_ft_scaled_font_unlock_face (cairo_scaled_font_t *abstract_font)
4660
0
{
4661
0
    cairo_ft_scaled_font_t *scaled_font = (cairo_ft_scaled_font_t *) abstract_font;
4662
4663
0
    if (! _cairo_scaled_font_is_ft (abstract_font)) {
4664
0
  _cairo_error_throw (CAIRO_STATUS_FONT_TYPE_MISMATCH);
4665
0
  return;
4666
0
    }
4667
4668
0
    if (scaled_font->base.status)
4669
0
  return;
4670
4671
    /* Note: We released the unscaled font's mutex at the end of
4672
     * cairo_ft_scaled_font_lock_face, so we have to acquire it again
4673
     * as _cairo_ft_unscaled_font_unlock_face expects it to be held
4674
     * when we call into it. */
4675
0
    CAIRO_MUTEX_LOCK (scaled_font->unscaled->mutex);
4676
4677
0
    _cairo_ft_unscaled_font_unlock_face (scaled_font->unscaled);
4678
0
}
4679
4680
static cairo_bool_t
4681
_cairo_ft_scaled_font_is_vertical (cairo_scaled_font_t *scaled_font)
4682
91
{
4683
91
    cairo_ft_scaled_font_t *ft_scaled_font;
4684
4685
91
    if (!_cairo_scaled_font_is_ft (scaled_font))
4686
0
  return FALSE;
4687
4688
91
    ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
4689
91
    if (ft_scaled_font->ft_options.load_flags & FT_LOAD_VERTICAL_LAYOUT)
4690
0
  return TRUE;
4691
91
    return FALSE;
4692
91
}
4693
4694
unsigned int
4695
_cairo_ft_scaled_font_get_load_flags (cairo_scaled_font_t *scaled_font)
4696
0
{
4697
0
    cairo_ft_scaled_font_t *ft_scaled_font;
4698
4699
0
    if (! _cairo_scaled_font_is_ft (scaled_font))
4700
0
  return 0;
4701
4702
0
    ft_scaled_font = (cairo_ft_scaled_font_t *) scaled_font;
4703
0
    return ft_scaled_font->ft_options.load_flags;
4704
0
}
4705
4706
void
4707
_cairo_ft_font_reset_static_data (void)
4708
0
{
4709
0
    _cairo_ft_unscaled_font_map_destroy ();
4710
0
}