Coverage Report

Created: 2026-09-28 10:59

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