Coverage Report

Created: 2026-08-14 08:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cairo/src/cairo-cff-subset.c
Line
Count
Source
1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2006 Adrian Johnson
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Adrian Johnson.
32
 *
33
 * Contributor(s):
34
 *  Adrian Johnson <ajohnson@redneon.com>
35
 *      Eugeniy Meshcheryakov <eugen@debian.org>
36
 */
37
38
/*
39
 * Useful links:
40
 * http://www.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf
41
 * http://www.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5177.Type2.pdf
42
 */
43
44
#define _DEFAULT_SOURCE /* for snprintf(), strdup() */
45
#include "cairoint.h"
46
47
#include "cairo-array-private.h"
48
#include "cairo-compiler-private.h"
49
#include "cairo-error-private.h"
50
51
#if CAIRO_HAS_FONT_SUBSET
52
53
#include "cairo-scaled-font-subsets-private.h"
54
#include "cairo-truetype-subset-private.h"
55
#include <string.h>
56
#include <locale.h>
57
58
/* CFF Dict Operators. If the high byte is 0 the command is encoded
59
 * with a single byte. */
60
#define BASEFONTNAME_OP     0x0c16
61
314
#define CIDCOUNT_OP         0x0c22
62
2.11k
#define CHARSET_OP          0x000f
63
2.74k
#define CHARSTRINGS_OP      0x0011
64
#define COPYRIGHT_OP        0x0c00
65
643
#define DEFAULTWIDTH_OP     0x0014
66
1.58k
#define ENCODING_OP         0x0010
67
388
#define FAMILYNAME_OP       0x0003
68
760
#define FDARRAY_OP          0x0c24
69
760
#define FDSELECT_OP         0x0c25
70
1.05k
#define FONTBBOX_OP         0x0005
71
671
#define FONTMATRIX_OP       0x0c07
72
#define FONTNAME_OP         0x0c26
73
388
#define FULLNAME_OP         0x0002
74
719
#define LOCAL_SUB_OP        0x0013
75
643
#define NOMINALWIDTH_OP     0x0015
76
#define NOTICE_OP           0x0001
77
#define POSTSCRIPT_OP       0x0c15
78
2.98k
#define PRIVATE_OP          0x0012
79
16.3k
#define ROS_OP              0x0c1e
80
629
#define UNIQUEID_OP         0x000d
81
#define VERSION_OP          0x0000
82
#define WEIGHT_OP           0x0004
83
629
#define XUID_OP             0x000e
84
240
#define BLUEVALUES_OP       0x0006
85
224
#define OTHERBLUES_OP       0x0007
86
224
#define FAMILYBLUES_OP      0x0008
87
188
#define FAMILYOTHERBLUES_OP 0x0009
88
188
#define STEMSNAPH_OP        0x0c0c
89
67
#define STEMSNAPV_OP        0x0c0d
90
91
6.46k
#define NUM_STD_STRINGS 391
92
93
/* Type 2 Charstring operators */
94
353k
#define TYPE2_hstem     0x0001
95
349k
#define TYPE2_vstem     0x0003
96
137k
#define TYPE2_callsubr  0x000a
97
98
1.95k
#define TYPE2_return    0x000b
99
146k
#define TYPE2_endchar   0x000e
100
101
345k
#define TYPE2_hstemhm   0x0012
102
332k
#define TYPE2_hintmask  0x0013
103
160k
#define TYPE2_cntrmask  0x0014
104
166k
#define TYPE2_vstemhm   0x0017
105
136k
#define TYPE2_callgsubr 0x001d
106
107
160k
#define TYPE2_rmoveto   0x0015
108
297k
#define TYPE2_hmoveto   0x0016
109
147k
#define TYPE2_vmoveto   0x0004
110
111
112
1.36k
#define MAX_SUBROUTINE_NESTING 10 /* From Type2 Charstring spec */
113
114
115
typedef struct _cff_header {
116
    uint8_t major;
117
    uint8_t minor;
118
    uint8_t header_size;
119
    uint8_t offset_size;
120
} cff_header_t;
121
122
typedef struct _cff_index_element {
123
    cairo_bool_t   is_copy;
124
    unsigned char *data;
125
    int            length;
126
} cff_index_element_t;
127
128
typedef struct _cff_dict_operator {
129
    cairo_hash_entry_t base;
130
131
    unsigned short operator;
132
    unsigned char *operand;
133
    int            operand_length;
134
    int            operand_offset;
135
} cff_dict_operator_t;
136
137
typedef struct _cairo_cff_font {
138
139
    cairo_scaled_font_subset_t *scaled_font_subset;
140
    const cairo_scaled_font_backend_t *backend;
141
142
    /* Font Data */
143
    unsigned char       *data;
144
    unsigned long        data_length;
145
    unsigned char       *current_ptr;
146
    unsigned char       *data_end;
147
    cff_header_t        *header;
148
    char                *font_name;
149
    char                *ps_name;
150
    cairo_hash_table_t  *top_dict;
151
    cairo_hash_table_t  *private_dict;
152
    cairo_array_t        strings_index;
153
    cairo_array_t        charstrings_index;
154
    cairo_array_t        global_sub_index;
155
    cairo_array_t        local_sub_index;
156
    unsigned char       *charset;
157
    int                  num_glyphs;
158
    cairo_bool_t         is_cid;
159
    cairo_bool_t         is_opentype;
160
    int      units_per_em;
161
    int      global_sub_bias;
162
    int      local_sub_bias;
163
    double               default_width;
164
    double               nominal_width;
165
166
    /* CID Font Data */
167
    int                 *fdselect;
168
    unsigned int         num_fontdicts;
169
    cairo_hash_table_t **fd_dict;
170
    cairo_hash_table_t **fd_private_dict;
171
    cairo_array_t       *fd_local_sub_index;
172
    int     *fd_local_sub_bias;
173
    double              *fd_default_width;
174
    double              *fd_nominal_width;
175
176
    /* Subsetted Font Data */
177
    char                *subset_font_name;
178
    cairo_array_t        charstrings_subset_index;
179
    cairo_array_t        strings_subset_index;
180
    int      euro_sid;
181
    int                 *fdselect_subset;
182
    unsigned int         num_subset_fontdicts;
183
    int                 *fd_subset_map;
184
    int                 *private_dict_offset;
185
    cairo_bool_t         subset_subroutines;
186
    cairo_bool_t  *global_subs_used;
187
    cairo_bool_t  *local_subs_used;
188
    cairo_bool_t       **fd_local_subs_used;
189
    cairo_array_t        output;
190
191
    /* Subset Metrics */
192
    int                 *widths;
193
    int                  x_min, y_min, x_max, y_max;
194
    int                  ascent, descent;
195
196
    /* Type 2 charstring data */
197
    int      type2_stack_size;
198
    int      type2_stack_top_value;
199
    cairo_bool_t   type2_stack_top_is_int;
200
    int      type2_num_hints;
201
    int      type2_hintmask_bytes;
202
    int                  type2_nesting_level;
203
    cairo_bool_t         type2_seen_first_int;
204
    cairo_bool_t         type2_find_width;
205
    cairo_bool_t         type2_found_width;
206
    int                  type2_width;
207
    cairo_bool_t         type2_has_path;
208
209
} cairo_cff_font_t;
210
211
/* Encoded integer using maximum sized encoding. This is required for
212
 * operands that are later modified after encoding. */
213
static unsigned char *
214
encode_integer_max (unsigned char *p, int i)
215
7.62k
{
216
7.62k
    *p++ = 29;
217
7.62k
    *p++ = i >> 24;
218
7.62k
    *p++ = (i >> 16) & 0xff;
219
7.62k
    *p++ = (i >> 8)  & 0xff;
220
7.62k
    *p++ = i & 0xff;
221
7.62k
    return p;
222
7.62k
}
223
224
static unsigned char *
225
encode_integer (unsigned char *p, int i)
226
4.86k
{
227
4.86k
    if (i >= -107 && i <= 107) {
228
1.57k
        *p++ = i + 139;
229
3.29k
    } else if (i >= 108 && i <= 1131) {
230
3.04k
        i -= 108;
231
3.04k
        *p++ = (i >> 8)+ 247;
232
3.04k
        *p++ = i & 0xff;
233
3.04k
    } else if (i >= -1131 && i <= -108) {
234
197
        i = -i - 108;
235
197
        *p++ = (i >> 8)+ 251;
236
197
        *p++ = i & 0xff;
237
197
    } else if (i >= -32768 && i <= 32767) {
238
45
        *p++ = 28;
239
45
        *p++ = (i >> 8)  & 0xff;
240
45
        *p++ = i & 0xff;
241
45
    } else {
242
3
        p = encode_integer_max (p, i);
243
3
    }
244
4.86k
    return p;
245
4.86k
}
246
247
static unsigned char *
248
decode_integer (unsigned char *p, int *integer)
249
8.31k
{
250
8.31k
    if (*p == 28) {
251
1.06k
        *integer = (int16_t)(p[1]<<8 | p[2]);
252
1.06k
        p += 3;
253
7.25k
    } else if (*p == 29) {
254
475
        *integer = (int32_t)(((uint32_t)p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
255
475
        p += 5;
256
6.77k
    } else if (*p >= 32 && *p <= 246) {
257
935
        *integer = *p++ - 139;
258
5.84k
    } else if (*p <= 250) {
259
4.75k
        *integer = (p[0] - 247) * 256 + p[1] + 108;
260
4.75k
        p += 2;
261
4.75k
    } else if (*p <= 254) {
262
1.09k
        *integer = -(p[0] - 251) * 256 - p[1] - 108;
263
1.09k
        p += 2;
264
1.09k
    } else {
265
0
        *integer = 0;
266
0
        p += 1;
267
0
    }
268
8.31k
    return p;
269
8.31k
}
270
271
static char *
272
decode_nibble (int n, char *buf)
273
82
{
274
82
    switch (n)
275
82
    {
276
6
    case 0xa:
277
6
  *buf++ = '.';
278
6
  break;
279
0
    case 0xb:
280
0
  *buf++ = 'E';
281
0
  break;
282
3
    case 0xc:
283
3
  *buf++ = 'E';
284
3
  *buf++ = '-';
285
3
  break;
286
0
    case 0xd:
287
0
  *buf++ = '-';
288
0
  break;
289
2
    case 0xe:
290
2
  *buf++ = '-';
291
2
  break;
292
12
    case 0xf:
293
12
  break;
294
59
    default:
295
59
  *buf++ = '0' + n;
296
59
  break;
297
82
    }
298
299
82
    return buf;
300
82
}
301
302
static unsigned char *
303
decode_real (unsigned char *p, double *real)
304
5
{
305
5
    char buffer[100];
306
5
    char *buf = buffer;
307
5
    char *buf_end = buffer + sizeof (buffer);
308
5
    char *end;
309
5
    int n;
310
311
5
    p++;
312
41
    while (buf + 2 < buf_end) {
313
41
  n = *p >> 4;
314
41
  buf = decode_nibble (n, buf);
315
41
  n = *p & 0x0f;
316
41
  buf = decode_nibble (n, buf);
317
41
        if ((*p & 0x0f) == 0x0f) {
318
5
      p++;
319
5
            break;
320
5
  }
321
36
  p++;
322
36
    };
323
5
    *buf = 0;
324
325
5
    *real = _cairo_strtod (buffer, &end);
326
327
5
    return p;
328
5
}
329
330
static unsigned char *
331
decode_number (unsigned char *p, double *number)
332
3.55k
{
333
3.55k
    if (*p == 30) {
334
5
        p = decode_real (p, number);
335
3.55k
    } else {
336
3.55k
        int i;
337
3.55k
        p = decode_integer (p, &i);
338
3.55k
        *number = i;
339
3.55k
    }
340
3.55k
    return p;
341
3.55k
}
342
343
/* return null if the operator would go past the end pointer */
344
static unsigned char *
345
decode_operator (unsigned char *p, unsigned char* end, unsigned short *operator)
346
17.1k
{
347
17.1k
    unsigned short op = 0;
348
349
17.1k
    op = *p++;
350
17.1k
    if (op == 12) {
351
3.97k
        if (p == end) {
352
0
            return NULL;
353
0
        }
354
3.97k
        op <<= 8;
355
3.97k
        op |= *p++;
356
3.97k
    }
357
17.1k
    *operator = op;
358
17.1k
    return p;
359
17.1k
}
360
361
/* return 0 if not an operand */
362
static int
363
operand_length (unsigned char *p, unsigned char* end)
364
48.1k
{
365
48.1k
    unsigned char *begin = p;
366
367
48.1k
    if (*p == 28)
368
2.19k
        return 3;
369
370
45.9k
    if (*p == 29)
371
1.20k
        return 5;
372
373
44.7k
    if (*p >= 32 && *p <= 246)
374
10.6k
        return 1;
375
376
34.0k
    if (*p >= 247 && *p <= 254)
377
16.5k
        return 2;
378
379
17.4k
    if (*p == 30) {
380
1.78k
        while (p < end && (*p & 0x0f) != 0x0f)
381
1.44k
            p++;
382
333
        return p - begin + 1;
383
333
    }
384
385
17.1k
    return 0;
386
17.4k
}
387
388
static unsigned char *
389
encode_index_offset (unsigned char *p, int offset_size, unsigned long offset)
390
33.6k
{
391
96.3k
    while (--offset_size >= 0) {
392
62.6k
        p[offset_size] = (unsigned char) (offset & 0xff);
393
62.6k
        offset >>= 8;
394
62.6k
    }
395
33.6k
    return p + offset_size;
396
33.6k
}
397
398
static size_t
399
decode_index_offset(unsigned char *p, int off_size)
400
85.1k
{
401
85.1k
    unsigned long offset = 0;
402
403
256k
    while (off_size-- > 0)
404
171k
        offset = offset*256 + *p++;
405
85.1k
    return offset;
406
85.1k
}
407
408
static void
409
cff_index_init (cairo_array_t *index)
410
11.0k
{
411
11.0k
    _cairo_array_init (index, sizeof (cff_index_element_t));
412
11.0k
}
413
414
static cairo_int_status_t
415
cff_index_read (cairo_array_t *index, unsigned char **ptr, unsigned char *end_ptr)
416
5.11k
{
417
5.11k
    cff_index_element_t element;
418
5.11k
    unsigned char *data, *p;
419
5.11k
    cairo_status_t status;
420
5.11k
    int offset_size, count, i;
421
5.11k
    size_t start, end = 0;
422
423
5.11k
    p = *ptr;
424
5.11k
    if (p + 2 > end_ptr)
425
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
426
5.11k
    count = get_unaligned_be16 (p);
427
5.11k
    p += 2;
428
5.11k
    if (count > 0) {
429
4.52k
        offset_size = *p++;
430
4.52k
        if (p + (count + 1)*offset_size > end_ptr || offset_size > 4)
431
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
432
4.52k
        data = p + offset_size*(count + 1) - 1;
433
4.52k
        start = decode_index_offset (p, offset_size);
434
4.52k
        p += offset_size;
435
85.1k
        for (i = 0; i < count; i++) {
436
80.6k
            end = decode_index_offset (p, offset_size);
437
80.6k
            p += offset_size;
438
80.6k
            if (p > end_ptr || end < start || data + end > end_ptr)
439
42
                return CAIRO_INT_STATUS_UNSUPPORTED;
440
80.6k
            element.length = end - start;
441
80.6k
            element.is_copy = FALSE;
442
80.6k
            element.data = data + start;
443
80.6k
            status = _cairo_array_append (index, &element);
444
80.6k
            if (unlikely (status))
445
0
                return status;
446
80.6k
            start = end;
447
80.6k
        }
448
4.48k
        p = data + end;
449
4.48k
    }
450
5.07k
    *ptr = p;
451
452
5.07k
    return CAIRO_STATUS_SUCCESS;
453
5.11k
}
454
455
static cairo_status_t
456
cff_index_write (cairo_array_t *index, cairo_array_t *output)
457
3.91k
{
458
3.91k
    int offset_size;
459
3.91k
    int offset;
460
3.91k
    int num_elem;
461
3.91k
    int i;
462
3.91k
    cff_index_element_t *element;
463
3.91k
    uint16_t count;
464
3.91k
    unsigned char buf[5];
465
3.91k
    cairo_status_t status;
466
467
3.91k
    num_elem = _cairo_array_num_elements (index);
468
3.91k
    count = cpu_to_be16 ((uint16_t) num_elem);
469
3.91k
    status = _cairo_array_append_multiple (output, &count, 2);
470
3.91k
    if (unlikely (status))
471
0
        return status;
472
473
3.91k
    if (num_elem == 0)
474
947
        return CAIRO_STATUS_SUCCESS;
475
476
    /* Find maximum offset to determine offset size */
477
2.97k
    offset = 1;
478
31.6k
    for (i = 0; i < num_elem; i++) {
479
28.7k
        element = _cairo_array_index (index, i);
480
28.7k
        offset += element->length;
481
28.7k
    }
482
2.97k
    if (offset < 0x100)
483
2.13k
        offset_size = 1;
484
839
    else if (offset < 0x10000)
485
839
        offset_size = 2;
486
0
    else if (offset < 0x1000000)
487
0
        offset_size = 3;
488
0
    else
489
0
        offset_size = 4;
490
491
2.97k
    buf[0] = (unsigned char) offset_size;
492
2.97k
    status = _cairo_array_append (output, buf);
493
2.97k
    if (unlikely (status))
494
0
        return status;
495
496
2.97k
    offset = 1;
497
2.97k
    encode_index_offset (buf, offset_size, offset);
498
2.97k
    status = _cairo_array_append_multiple (output, buf, offset_size);
499
2.97k
    if (unlikely (status))
500
0
        return status;
501
502
31.6k
    for (i = 0; i < num_elem; i++) {
503
28.7k
        element = _cairo_array_index (index, i);
504
28.7k
        offset += element->length;
505
28.7k
        encode_index_offset (buf, offset_size, offset);
506
28.7k
        status = _cairo_array_append_multiple (output, buf, offset_size);
507
28.7k
        if (unlikely (status))
508
0
            return status;
509
28.7k
    }
510
511
31.6k
    for (i = 0; i < num_elem; i++) {
512
28.7k
        element = _cairo_array_index (index, i);
513
28.7k
        if (element->length > 0) {
514
28.7k
            status = _cairo_array_append_multiple (output,
515
28.7k
                                                   element->data,
516
28.7k
                                                   element->length);
517
28.7k
        }
518
28.7k
        if (unlikely (status))
519
0
            return status;
520
28.7k
    }
521
2.97k
    return CAIRO_STATUS_SUCCESS;
522
2.97k
}
523
524
static void
525
cff_index_set_object (cairo_array_t *index, int obj_index,
526
                      unsigned char *object , int length)
527
7.65k
{
528
7.65k
    cff_index_element_t *element;
529
530
7.65k
    element = _cairo_array_index (index, obj_index);
531
7.65k
    if (element->is_copy)
532
0
        free (element->data);
533
534
7.65k
    element->data = object;
535
7.65k
    element->length = length;
536
7.65k
    element->is_copy = FALSE;
537
7.65k
}
538
539
static cairo_status_t
540
cff_index_append (cairo_array_t *index, unsigned char *object , int length)
541
16.8k
{
542
16.8k
    cff_index_element_t element;
543
544
16.8k
    element.length = length;
545
16.8k
    element.is_copy = FALSE;
546
16.8k
    element.data = object;
547
548
16.8k
    return _cairo_array_append (index, &element);
549
16.8k
}
550
551
static cairo_status_t
552
cff_index_append_copy (cairo_array_t *index,
553
                       const unsigned char *object,
554
                       unsigned int length)
555
1.99k
{
556
1.99k
    cff_index_element_t element;
557
1.99k
    cairo_status_t status;
558
559
1.99k
    element.length = length;
560
1.99k
    element.is_copy = TRUE;
561
1.99k
    element.data = _cairo_malloc (element.length);
562
1.99k
    if (unlikely (element.data == NULL && length != 0))
563
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
564
565
1.99k
    memcpy (element.data, object, element.length);
566
567
1.99k
    status = _cairo_array_append (index, &element);
568
1.99k
    if (unlikely (status)) {
569
0
  free (element.data);
570
0
  return status;
571
0
    }
572
573
1.99k
    return CAIRO_STATUS_SUCCESS;
574
1.99k
}
575
576
static void
577
cff_index_fini (cairo_array_t *index)
578
11.0k
{
579
11.0k
    cff_index_element_t *element;
580
11.0k
    unsigned int i;
581
582
110k
    for (i = 0; i < _cairo_array_num_elements (index); i++) {
583
99.4k
        element = _cairo_array_index (index, i);
584
99.4k
        if (element->is_copy && element->data)
585
1.99k
            free (element->data);
586
99.4k
    }
587
11.0k
    _cairo_array_fini (index);
588
11.0k
}
589
590
static cairo_bool_t
591
_cairo_cff_dict_equal (const void *key_a, const void *key_b)
592
14.7k
{
593
14.7k
    const cff_dict_operator_t *op_a = key_a;
594
14.7k
    const cff_dict_operator_t *op_b = key_b;
595
596
14.7k
    return op_a->operator == op_b->operator;
597
14.7k
}
598
599
static cairo_status_t
600
cff_dict_init (cairo_hash_table_t **dict)
601
3.47k
{
602
3.47k
    *dict = _cairo_hash_table_create (_cairo_cff_dict_equal);
603
3.47k
    if (unlikely (*dict == NULL))
604
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
605
606
3.47k
    return CAIRO_STATUS_SUCCESS;
607
3.47k
}
608
609
static void
610
_cairo_dict_init_key (cff_dict_operator_t *key, int operator)
611
55.4k
{
612
55.4k
    key->base.hash = (unsigned long) operator;
613
55.4k
    key->operator = operator;
614
55.4k
}
615
616
static cairo_status_t
617
cff_dict_create_operator (int            operator,
618
                          unsigned char *operand,
619
                          int            size,
620
        cff_dict_operator_t **out)
621
20.8k
{
622
20.8k
    cff_dict_operator_t *op;
623
624
20.8k
    op = _cairo_calloc (sizeof (cff_dict_operator_t));
625
20.8k
    if (unlikely (op == NULL))
626
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
627
628
20.8k
    _cairo_dict_init_key (op, operator);
629
20.8k
    if (size != 0) {
630
20.7k
  op->operand = _cairo_malloc (size);
631
20.7k
  if (unlikely (op->operand == NULL)) {
632
0
      free (op);
633
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
634
0
  }
635
20.7k
  memcpy (op->operand, operand, size);
636
20.7k
    } else {
637
120
  op->operand = NULL;
638
  /* Delta-encoded arrays can be empty. */
639
120
  if (operator != BLUEVALUES_OP &&
640
104
      operator != OTHERBLUES_OP &&
641
104
      operator != FAMILYBLUES_OP &&
642
68
      operator != FAMILYOTHERBLUES_OP &&
643
68
      operator != STEMSNAPH_OP &&
644
67
      operator != STEMSNAPV_OP) {
645
67
      free (op);
646
67
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
647
67
  }
648
120
    }
649
650
20.7k
    op->operand_length = size;
651
20.7k
    op->operand_offset = -1;
652
653
20.7k
    *out = op;
654
20.7k
    return CAIRO_STATUS_SUCCESS;
655
20.8k
}
656
657
static cairo_int_status_t
658
cff_dict_read (cairo_hash_table_t *dict, unsigned char *p, int dict_size)
659
2.36k
{
660
2.36k
    unsigned char *end;
661
2.36k
    cairo_array_t operands;
662
2.36k
    cff_dict_operator_t *op;
663
2.36k
    unsigned short operator;
664
2.36k
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
665
2.36k
    int size;
666
667
2.36k
    end = p + dict_size;
668
2.36k
    _cairo_array_init (&operands, 1);
669
50.3k
    while (p < end) {
670
48.1k
        size = operand_length (p, end);
671
48.1k
        if (size != 0) {
672
30.9k
            if (unlikely(size > end - p)) {
673
35
                status = CAIRO_INT_STATUS_UNSUPPORTED;
674
35
                goto fail;
675
35
            }
676
30.9k
            status = _cairo_array_append_multiple (&operands, p, size);
677
30.9k
            if (unlikely (status))
678
0
                goto fail;
679
680
30.9k
            p += size;
681
30.9k
        } else {
682
17.1k
            p = decode_operator (p, end, &operator);
683
17.1k
            if (unlikely (!p)) {
684
0
                status = CAIRO_INT_STATUS_UNSUPPORTED;
685
0
                goto fail;
686
0
            }
687
17.1k
            status = cff_dict_create_operator (operator,
688
17.1k
                                          _cairo_array_index (&operands, 0),
689
17.1k
                                          _cairo_array_num_elements (&operands),
690
17.1k
            &op);
691
17.1k
            if (unlikely (status))
692
67
                goto fail;
693
694
17.0k
            status = _cairo_hash_table_insert (dict, &op->base);
695
17.0k
            if (unlikely (status))
696
0
                goto fail;
697
698
17.0k
            _cairo_array_truncate (&operands, 0);
699
17.0k
        }
700
48.1k
    }
701
702
2.36k
fail:
703
2.36k
    _cairo_array_fini (&operands);
704
705
2.36k
    return status;
706
2.36k
}
707
708
static void
709
cff_dict_remove (cairo_hash_table_t *dict, unsigned short operator)
710
1.73k
{
711
1.73k
    cff_dict_operator_t key, *op;
712
713
1.73k
    _cairo_dict_init_key (&key, operator);
714
1.73k
    op = _cairo_hash_table_lookup (dict, &key.base);
715
1.73k
    if (op != NULL) {
716
248
        free (op->operand);
717
248
        _cairo_hash_table_remove (dict, (cairo_hash_entry_t *) op);
718
248
        free (op);
719
248
    }
720
1.73k
}
721
722
static unsigned char *
723
cff_dict_get_operands (cairo_hash_table_t *dict,
724
                       unsigned short      operator,
725
                       int                *size)
726
18.4k
{
727
18.4k
    cff_dict_operator_t key, *op;
728
729
18.4k
    _cairo_dict_init_key (&key, operator);
730
18.4k
    op = _cairo_hash_table_lookup (dict, &key.base);
731
18.4k
    if (op != NULL) {
732
5.81k
        *size = op->operand_length;
733
5.81k
        return op->operand;
734
5.81k
    }
735
736
12.6k
    return NULL;
737
18.4k
}
738
739
static cairo_status_t
740
cff_dict_set_operands (cairo_hash_table_t *dict,
741
                       unsigned short      operator,
742
                       unsigned char      *operand,
743
                       int                 size)
744
7.89k
{
745
7.89k
    cff_dict_operator_t key, *op;
746
7.89k
    cairo_status_t status;
747
748
7.89k
    _cairo_dict_init_key (&key, operator);
749
7.89k
    op = _cairo_hash_table_lookup (dict, &key.base);
750
7.89k
    if (op != NULL) {
751
4.17k
        free (op->operand);
752
4.17k
        op->operand = _cairo_malloc (size);
753
4.17k
  if (unlikely (op->operand == NULL))
754
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
755
756
4.17k
        memcpy (op->operand, operand, size);
757
4.17k
        op->operand_length = size;
758
4.17k
    }
759
3.71k
    else
760
3.71k
    {
761
3.71k
        status = cff_dict_create_operator (operator, operand, size, &op);
762
3.71k
        if (unlikely (status))
763
0
      return status;
764
765
3.71k
  status = _cairo_hash_table_insert (dict, &op->base);
766
3.71k
  if (unlikely (status))
767
0
      return status;
768
3.71k
    }
769
770
7.89k
    return CAIRO_STATUS_SUCCESS;
771
7.89k
}
772
773
static int
774
cff_dict_get_location (cairo_hash_table_t *dict,
775
                       unsigned short      operator,
776
                       int                *size)
777
4.24k
{
778
4.24k
    cff_dict_operator_t key, *op;
779
780
4.24k
    _cairo_dict_init_key (&key, operator);
781
4.24k
    op = _cairo_hash_table_lookup (dict, &key.base);
782
4.24k
    if (op != NULL) {
783
4.24k
        *size = op->operand_length;
784
4.24k
        return op->operand_offset;
785
4.24k
    }
786
787
0
    return -1;
788
4.24k
}
789
790
typedef struct _dict_write_info {
791
    cairo_array_t *output;
792
    cairo_status_t status;
793
} dict_write_info_t;
794
795
static void
796
cairo_dict_write_operator (cff_dict_operator_t *op, dict_write_info_t *write_info)
797
12.3k
{
798
12.3k
    unsigned char data;
799
800
12.3k
    op->operand_offset = _cairo_array_num_elements (write_info->output);
801
12.3k
    write_info->status = _cairo_array_append_multiple (write_info->output, op->operand, op->operand_length);
802
12.3k
    if (write_info->status)
803
0
        return;
804
805
12.3k
    if (op->operator & 0xff00) {
806
2.92k
        data = op->operator >> 8;
807
2.92k
        write_info->status = _cairo_array_append (write_info->output, &data);
808
2.92k
        if (write_info->status)
809
0
            return;
810
2.92k
    }
811
12.3k
    data = op->operator & 0xff;
812
12.3k
    write_info->status = _cairo_array_append (write_info->output, &data);
813
12.3k
}
814
815
static void
816
_cairo_dict_collect (void *entry, void *closure)
817
12.3k
{
818
12.3k
    dict_write_info_t   *write_info = closure;
819
12.3k
    cff_dict_operator_t *op = entry;
820
821
12.3k
    if (write_info->status)
822
0
        return;
823
824
    /* The ROS operator is handled separately in cff_dict_write() */
825
12.3k
    if (op->operator != ROS_OP)
826
12.0k
        cairo_dict_write_operator (op, write_info);
827
12.3k
}
828
829
static cairo_status_t
830
cff_dict_write (cairo_hash_table_t *dict, cairo_array_t *output)
831
2.27k
{
832
2.27k
    dict_write_info_t write_info;
833
2.27k
    cff_dict_operator_t key, *op;
834
835
2.27k
    write_info.output = output;
836
2.27k
    write_info.status = CAIRO_STATUS_SUCCESS;
837
838
    /* The CFF specification requires that the Top Dict of CID fonts
839
     * begin with the ROS operator. */
840
2.27k
    _cairo_dict_init_key (&key, ROS_OP);
841
2.27k
    op = _cairo_hash_table_lookup (dict, &key.base);
842
2.27k
    if (op != NULL)
843
311
        cairo_dict_write_operator (op, &write_info);
844
845
2.27k
    _cairo_hash_table_foreach (dict, _cairo_dict_collect, &write_info);
846
847
2.27k
    return write_info.status;
848
2.27k
}
849
850
static void
851
_cff_dict_entry_pluck (void *_entry, void *dict)
852
20.5k
{
853
20.5k
    cff_dict_operator_t *entry = _entry;
854
855
20.5k
    _cairo_hash_table_remove (dict, &entry->base);
856
20.5k
    free (entry->operand);
857
20.5k
    free (entry);
858
20.5k
}
859
860
static void
861
cff_dict_fini (cairo_hash_table_t *dict)
862
3.47k
{
863
3.47k
    _cairo_hash_table_foreach (dict, _cff_dict_entry_pluck, dict);
864
3.47k
    _cairo_hash_table_destroy (dict);
865
3.47k
}
866
867
static cairo_int_status_t
868
cairo_cff_font_read_header (cairo_cff_font_t *font)
869
762
{
870
762
    if (font->data_length < sizeof (cff_header_t))
871
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
872
873
874
762
    font->header = (cff_header_t *) font->data;
875
762
    font->current_ptr = font->data + font->header->header_size;
876
877
762
    return CAIRO_STATUS_SUCCESS;
878
762
}
879
880
static cairo_int_status_t
881
cairo_cff_font_read_name (cairo_cff_font_t *font)
882
762
{
883
762
    cairo_array_t index;
884
762
    cairo_int_status_t status;
885
762
    cff_index_element_t *element;
886
762
    unsigned char *p;
887
762
    int i, len;
888
889
762
    cff_index_init (&index);
890
762
    status = cff_index_read (&index, &font->current_ptr, font->data_end);
891
762
    if (status == CAIRO_INT_STATUS_SUCCESS && !font->is_opentype) {
892
697
        element = _cairo_array_index (&index, 0);
893
697
  p = element->data;
894
697
  len = element->length;
895
896
  /* If font name is prefixed with a subset tag, strip it off. */
897
697
  if (len > 7 && p[6] == '+') {
898
4.43k
      for (i = 0; i < 6; i++)
899
3.80k
    if (p[i] < 'A' || p[i] > 'Z')
900
0
        break;
901
634
      if (i == 6) {
902
634
    p += 7;
903
634
    len -= 7;
904
634
      }
905
634
  }
906
907
697
  font->ps_name = _cairo_strndup ((char*)p, len);
908
697
  if (unlikely (font->ps_name == NULL))
909
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
910
911
697
        status = _cairo_escape_ps_name (&font->ps_name);
912
697
    }
913
762
    cff_index_fini (&index);
914
915
762
    return status;
916
762
}
917
918
static cairo_int_status_t
919
cairo_cff_font_read_private_dict (cairo_cff_font_t   *font,
920
                                  cairo_hash_table_t *private_dict,
921
                                  cairo_array_t      *local_sub_index,
922
                                  int                *local_sub_bias,
923
                                  cairo_bool_t      **local_subs_used,
924
                                  double             *default_width,
925
                                  double             *nominal_width,
926
                                  unsigned char      *ptr,
927
                                  int                 size)
928
747
{
929
747
    cairo_int_status_t status;
930
747
    unsigned char buf[10];
931
747
    unsigned char *end_buf;
932
747
    int offset;
933
747
    int i;
934
747
    unsigned char *operand;
935
747
    unsigned char *p;
936
747
    int num_subs;
937
938
747
    status = cff_dict_read (private_dict, ptr, size);
939
747
    if (unlikely (status))
940
102
  return status;
941
942
645
    operand = cff_dict_get_operands (private_dict, LOCAL_SUB_OP, &i);
943
645
    if (operand) {
944
39
        decode_integer (operand, &offset);
945
39
        p = ptr + offset;
946
39
        if (unlikely (p < font->data || p > font->data_end))
947
2
            return CAIRO_INT_STATUS_UNSUPPORTED;
948
37
        status = cff_index_read (local_sub_index, &p, font->data_end);
949
37
  if (unlikely (status))
950
0
      return status;
951
952
  /* Use maximum sized encoding to reserve space for later modification. */
953
37
  end_buf = encode_integer_max (buf, 0);
954
37
  status = cff_dict_set_operands (private_dict, LOCAL_SUB_OP, buf, end_buf - buf);
955
37
  if (unlikely (status))
956
0
      return status;
957
37
    }
958
959
643
    *default_width = 0;
960
643
    operand = cff_dict_get_operands (private_dict, DEFAULTWIDTH_OP, &i);
961
643
    if (operand)
962
437
        decode_number (operand, default_width);
963
964
643
    *nominal_width = 0;
965
643
    operand = cff_dict_get_operands (private_dict, NOMINALWIDTH_OP, &i);
966
643
    if (operand)
967
432
   decode_number (operand, nominal_width);
968
969
643
    num_subs = _cairo_array_num_elements (local_sub_index);
970
643
    if (num_subs > 0) {
971
37
  *local_subs_used = _cairo_calloc_ab (num_subs, sizeof (cairo_bool_t));
972
37
  if (unlikely (*local_subs_used == NULL))
973
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
974
606
    } else {
975
606
  *local_subs_used = NULL;
976
606
    }
977
978
643
    if (num_subs < 1240)
979
643
  *local_sub_bias = 107;
980
0
    else if (num_subs < 33900)
981
0
  *local_sub_bias = 1131;
982
0
    else
983
0
  *local_sub_bias = 32768;
984
985
643
    return CAIRO_STATUS_SUCCESS;
986
643
}
987
988
static cairo_int_status_t
989
cairo_cff_font_read_fdselect (cairo_cff_font_t *font, unsigned char *p)
990
123
{
991
123
    int type, num_ranges, first, last, fd, i, j;
992
993
123
    font->fdselect = _cairo_calloc_ab (font->num_glyphs, sizeof (int));
994
123
    if (unlikely (font->fdselect == NULL))
995
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
996
997
123
    type = *p++;
998
123
    if (type == 0)
999
51
    {
1000
51
        if (p + font->num_glyphs > font->data_end)
1001
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1002
187
        for (i = 0; i < font->num_glyphs; i++)
1003
136
            font->fdselect[i] = *p++;
1004
72
    } else if (type == 3) {
1005
72
        if (p + 2 > font->data_end)
1006
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1007
72
        num_ranges = get_unaligned_be16 (p);
1008
72
        p += 2;
1009
72
        if (p + (3 * num_ranges) + 2 > font->data_end)
1010
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1011
151
        for  (i = 0; i < num_ranges; i++)
1012
79
        {
1013
79
            first = get_unaligned_be16 (p);
1014
79
            p += 2;
1015
79
            fd = *p++;
1016
79
            last = get_unaligned_be16 (p);
1017
79
            if (last > font->num_glyphs)
1018
0
                return CAIRO_INT_STATUS_UNSUPPORTED;
1019
1.80k
            for (j = first; j < last; j++)
1020
1.72k
                font->fdselect[j] = fd;
1021
79
        }
1022
72
    } else {
1023
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
1024
0
    }
1025
1026
123
    return CAIRO_STATUS_SUCCESS;
1027
123
}
1028
1029
static cairo_int_status_t
1030
cairo_cff_font_read_cid_fontdict (cairo_cff_font_t *font, unsigned char *ptr)
1031
123
{
1032
123
    cairo_array_t index;
1033
123
    cff_index_element_t *element;
1034
123
    unsigned int i;
1035
123
    int size;
1036
123
    unsigned char *operand;
1037
123
    int offset;
1038
123
    cairo_int_status_t status;
1039
123
    unsigned char buf[100];
1040
123
    unsigned char *end_buf;
1041
123
    size_t end_offset;
1042
1043
123
    cff_index_init (&index);
1044
123
    status = cff_index_read (&index, &ptr, font->data_end);
1045
123
    if (unlikely (status))
1046
0
        goto fail;
1047
1048
123
    font->num_fontdicts = _cairo_array_num_elements (&index);
1049
1050
123
    font->fd_dict = _cairo_calloc_ab (font->num_fontdicts, sizeof (cairo_hash_table_t *));
1051
123
    if (unlikely (font->fd_dict == NULL)) {
1052
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1053
0
        goto fail;
1054
0
    }
1055
1056
123
    font->fd_private_dict = _cairo_calloc_ab (font->num_fontdicts, sizeof (cairo_hash_table_t *));
1057
123
    if (unlikely (font->fd_private_dict == NULL)) {
1058
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1059
0
        goto fail;
1060
0
    }
1061
1062
123
    font->fd_local_sub_index = _cairo_calloc_ab (font->num_fontdicts, sizeof (cairo_array_t));
1063
123
    if (unlikely (font->fd_local_sub_index == NULL)) {
1064
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1065
0
        goto fail;
1066
0
    }
1067
1068
123
    font->fd_local_sub_bias = _cairo_calloc_ab (font->num_fontdicts, sizeof (int));
1069
123
    if (unlikely (font->fd_local_sub_bias == NULL)) {
1070
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1071
0
        goto fail;
1072
0
    }
1073
1074
123
    font->fd_local_subs_used = _cairo_calloc_ab (font->num_fontdicts, sizeof (cairo_bool_t *));
1075
123
    if (unlikely (font->fd_local_subs_used == NULL)) {
1076
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1077
0
        goto fail;
1078
0
    }
1079
1080
123
    font->fd_default_width = _cairo_calloc_ab (font->num_fontdicts, sizeof (double));
1081
123
    if (unlikely (font->fd_default_width == NULL)) {
1082
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1083
0
        goto fail;
1084
0
    }
1085
1086
123
    font->fd_nominal_width = _cairo_calloc_ab (font->num_fontdicts, sizeof (double));
1087
123
    if (unlikely (font->fd_nominal_width == NULL)) {
1088
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1089
0
        goto fail;
1090
0
    }
1091
1092
260
    for (i = 0; i < font->num_fontdicts; i++) {
1093
137
        status = cff_dict_init (&font->fd_dict[i]);
1094
137
        if (unlikely (status))
1095
0
            goto fail;
1096
1097
137
        element = _cairo_array_index (&index, i);
1098
137
        status = cff_dict_read (font->fd_dict[i], element->data, element->length);
1099
137
        if (unlikely (status))
1100
0
            goto fail;
1101
1102
137
        operand = cff_dict_get_operands (font->fd_dict[i], PRIVATE_OP, &size);
1103
137
        if (operand == NULL) {
1104
0
            status = CAIRO_INT_STATUS_UNSUPPORTED;
1105
0
            goto fail;
1106
0
        }
1107
137
        operand = decode_integer (operand, &size);
1108
137
        if (unlikely (size < 0)) {
1109
0
            status = CAIRO_INT_STATUS_UNSUPPORTED;
1110
0
            goto fail;
1111
0
        }
1112
137
        decode_integer (operand, &offset);
1113
137
        if (unlikely (offset < 0)) {
1114
0
            status = CAIRO_INT_STATUS_UNSUPPORTED;
1115
0
            goto fail;
1116
0
        }
1117
137
        if (unlikely (_cairo_add_size_t_overflow (size, offset, &end_offset))) {
1118
0
            status = CAIRO_INT_STATUS_UNSUPPORTED;
1119
0
            goto fail;
1120
0
        }
1121
137
        if (unlikely (end_offset > font->data_length)) {
1122
0
            status = CAIRO_INT_STATUS_UNSUPPORTED;
1123
0
            goto fail;
1124
0
        }
1125
137
        status = cff_dict_init (&font->fd_private_dict[i]);
1126
137
  if (unlikely (status))
1127
0
            goto fail;
1128
1129
137
        cff_index_init (&font->fd_local_sub_index[i]);
1130
137
        status = cairo_cff_font_read_private_dict (font,
1131
137
                                                   font->fd_private_dict[i],
1132
137
                                                   &font->fd_local_sub_index[i],
1133
137
                                                   &font->fd_local_sub_bias[i],
1134
137
                                                   &font->fd_local_subs_used[i],
1135
137
                                                   &font->fd_default_width[i],
1136
137
                                                   &font->fd_nominal_width[i],
1137
137
                                                   font->data + offset,
1138
137
                                                   size);
1139
137
        if (unlikely (status))
1140
0
            goto fail;
1141
1142
  /* Set integer operand to max value to use max size encoding to reserve
1143
         * space for any value later */
1144
137
        end_buf = encode_integer_max (buf, 0);
1145
137
        end_buf = encode_integer_max (end_buf, 0);
1146
137
        status = cff_dict_set_operands (font->fd_dict[i], PRIVATE_OP, buf, end_buf - buf);
1147
137
        if (unlikely (status))
1148
0
            goto fail;
1149
137
    }
1150
1151
123
    status = CAIRO_STATUS_SUCCESS;
1152
1153
123
fail:
1154
123
    cff_index_fini (&index);
1155
1156
123
    return status;
1157
123
}
1158
1159
static void
1160
cairo_cff_font_read_font_metrics (cairo_cff_font_t *font, cairo_hash_table_t  *top_dict)
1161
671
{
1162
671
    unsigned char *p;
1163
671
    unsigned char *end;
1164
671
    int size;
1165
671
    double x_min, y_min, x_max, y_max;
1166
671
    double xx, yx, xy, yy;
1167
1168
671
    x_min = 0.0;
1169
671
    y_min = 0.0;
1170
671
    x_max = 0.0;
1171
671
    y_max = 0.0;
1172
671
    p = cff_dict_get_operands (font->top_dict, FONTBBOX_OP, &size);
1173
671
    if (p) {
1174
671
        end = p + size;
1175
671
        if (p < end)
1176
671
            p = decode_number (p, &x_min);
1177
671
        if (p < end)
1178
670
            p = decode_number (p, &y_min);
1179
671
        if (p < end)
1180
670
            p = decode_number (p, &x_max);
1181
671
        if (p < end)
1182
670
            p = decode_number (p, &y_max);
1183
671
    }
1184
671
    font->x_min = floor (x_min);
1185
671
    font->y_min = floor (y_min);
1186
671
    font->x_max = floor (x_max);
1187
671
    font->y_max = floor (y_max);
1188
671
    font->ascent = font->y_max;
1189
671
    font->descent = font->y_min;
1190
1191
671
    xx = 0.001;
1192
671
    yx = 0.0;
1193
671
    xy = 0.0;
1194
671
    yy = 0.001;
1195
671
    p = cff_dict_get_operands (font->top_dict, FONTMATRIX_OP, &size);
1196
671
    if (p) {
1197
2
        end = p + size;
1198
2
        if (p < end)
1199
2
            p = decode_number (p, &xx);
1200
2
        if (p < end)
1201
2
            p = decode_number (p, &yx);
1202
2
        if (p < end)
1203
2
            p = decode_number (p, &xy);
1204
2
        if (p < end)
1205
2
            p = decode_number (p, &yy);
1206
2
    }
1207
    /* FreeType uses 1/abs(yy) to get units per EM */
1208
671
    font->units_per_em = _cairo_round(1.0/fabs(yy));
1209
671
}
1210
1211
static cairo_int_status_t
1212
cairo_cff_font_read_top_dict (cairo_cff_font_t *font)
1213
762
{
1214
762
    cairo_array_t index;
1215
762
    cff_index_element_t *element;
1216
762
    unsigned char buf[20];
1217
762
    unsigned char *end_buf;
1218
762
    unsigned char *operand;
1219
762
    cairo_int_status_t status;
1220
762
    unsigned char *p;
1221
762
    int size;
1222
762
    int offset;
1223
1224
762
    cff_index_init (&index);
1225
762
    status = cff_index_read (&index, &font->current_ptr, font->data_end);
1226
762
    if (unlikely (status))
1227
0
        goto fail;
1228
1229
762
    element = _cairo_array_index (&index, 0);
1230
762
    if (element == NULL)
1231
0
        return CAIRO_STATUS_NO_MEMORY;
1232
762
    status = cff_dict_read (font->top_dict, element->data, element->length);
1233
762
    if (unlikely (status))
1234
0
        goto fail;
1235
1236
762
    if (cff_dict_get_operands (font->top_dict, ROS_OP, &size) != NULL)
1237
123
        font->is_cid = TRUE;
1238
639
    else
1239
639
        font->is_cid = FALSE;
1240
1241
762
    operand = cff_dict_get_operands (font->top_dict, CHARSTRINGS_OP, &size);
1242
762
    decode_integer (operand, &offset);
1243
762
    p = font->data + offset;
1244
762
    if (unlikely (p < font->data || p > font->data_end))
1245
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
1246
762
    status = cff_index_read (&font->charstrings_index, &p, font->data_end);
1247
762
    if (unlikely (status))
1248
29
        goto fail;
1249
733
    font->num_glyphs = _cairo_array_num_elements (&font->charstrings_index);
1250
1251
733
    if (font->is_cid) {
1252
123
   operand = cff_dict_get_operands (font->top_dict, CHARSET_OP, &size);
1253
123
   if (!operand)
1254
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
1255
1256
123
   decode_integer (operand, &offset);
1257
123
   font->charset = font->data + offset;
1258
123
   if (unlikely (font->charset < font->data || font->charset >= font->data_end))
1259
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
1260
123
    }
1261
1262
733
    if (!font->is_opentype)
1263
671
        cairo_cff_font_read_font_metrics (font, font->top_dict);
1264
1265
733
    if (font->is_cid) {
1266
123
        operand = cff_dict_get_operands (font->top_dict, FDSELECT_OP, &size);
1267
123
        decode_integer (operand, &offset);
1268
123
        p = font->data + offset;
1269
123
        if (unlikely (p < font->data || p > font->data_end))
1270
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1271
123
        status = cairo_cff_font_read_fdselect (font, p);
1272
123
  if (unlikely (status))
1273
0
      goto fail;
1274
1275
123
        operand = cff_dict_get_operands (font->top_dict, FDARRAY_OP, &size);
1276
123
        decode_integer (operand, &offset);
1277
123
        p = font->data + offset;
1278
123
        if (unlikely (p < font->data || p > font->data_end))
1279
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1280
123
        status = cairo_cff_font_read_cid_fontdict (font, p);
1281
123
  if (unlikely (status))
1282
0
      goto fail;
1283
610
    } else {
1284
610
        operand = cff_dict_get_operands (font->top_dict, PRIVATE_OP, &size);
1285
610
        operand = decode_integer (operand, &size);
1286
610
        if (unlikely (size < 0))
1287
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1288
610
        decode_integer (operand, &offset);
1289
610
        p = font->data + offset;
1290
610
        if (unlikely (p < font->data || p + size > font->data_end))
1291
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1292
610
        status = cairo_cff_font_read_private_dict (font,
1293
610
                                                   font->private_dict,
1294
610
               &font->local_sub_index,
1295
610
               &font->local_sub_bias,
1296
610
               &font->local_subs_used,
1297
610
                                                   &font->default_width,
1298
610
                                                   &font->nominal_width,
1299
610
               p,
1300
610
               size);
1301
610
  if (unlikely (status))
1302
104
      goto fail;
1303
610
    }
1304
1305
    /* Use maximum sized encoding to reserve space for later modification. */
1306
629
    end_buf = encode_integer_max (buf, 0);
1307
629
    status = cff_dict_set_operands (font->top_dict,
1308
629
                              CHARSTRINGS_OP, buf, end_buf - buf);
1309
629
    if (unlikely (status))
1310
0
  goto fail;
1311
1312
629
    status = cff_dict_set_operands (font->top_dict,
1313
629
                              CHARSET_OP, buf, end_buf - buf);
1314
629
    if (unlikely (status))
1315
0
  goto fail;
1316
1317
629
    if (font->scaled_font_subset->is_latin) {
1318
393
        status = cff_dict_set_operands (font->top_dict,
1319
393
                                        ENCODING_OP, buf, end_buf - buf);
1320
393
        if (unlikely (status))
1321
0
            goto fail;
1322
1323
  /* Private has two operands - size and offset */
1324
393
  end_buf = encode_integer_max (end_buf, 0);
1325
393
  cff_dict_set_operands (font->top_dict, PRIVATE_OP, buf, end_buf - buf);
1326
393
        if (unlikely (status))
1327
0
            goto fail;
1328
1329
393
    } else {
1330
236
        status = cff_dict_set_operands (font->top_dict,
1331
236
                                        FDSELECT_OP, buf, end_buf - buf);
1332
236
        if (unlikely (status))
1333
0
            goto fail;
1334
1335
236
        status = cff_dict_set_operands (font->top_dict,
1336
236
                                        FDARRAY_OP, buf, end_buf - buf);
1337
236
        if (unlikely (status))
1338
0
            goto fail;
1339
1340
236
        cff_dict_remove (font->top_dict, ENCODING_OP);
1341
236
        cff_dict_remove (font->top_dict, PRIVATE_OP);
1342
236
    }
1343
1344
    /* Remove the unique identifier operators as the subsetted font is
1345
     * not the same is the original font. */
1346
629
    cff_dict_remove (font->top_dict, UNIQUEID_OP);
1347
629
    cff_dict_remove (font->top_dict, XUID_OP);
1348
1349
762
fail:
1350
762
    cff_index_fini (&index);
1351
1352
762
    return status;
1353
629
}
1354
1355
static cairo_int_status_t
1356
cairo_cff_font_read_strings (cairo_cff_font_t *font)
1357
629
{
1358
629
    return cff_index_read (&font->strings_index, &font->current_ptr, font->data_end);
1359
629
}
1360
1361
static cairo_int_status_t
1362
cairo_cff_font_read_global_subroutines (cairo_cff_font_t *font)
1363
616
{
1364
616
    cairo_int_status_t status;
1365
616
    int num_subs;
1366
1367
616
    status = cff_index_read (&font->global_sub_index, &font->current_ptr, font->data_end);
1368
616
    if (unlikely (status))
1369
0
  return status;
1370
1371
616
    num_subs = _cairo_array_num_elements (&font->global_sub_index);
1372
616
    if (num_subs > 0) {
1373
24
  font->global_subs_used = _cairo_calloc_ab (num_subs, sizeof(cairo_bool_t));
1374
24
  if (unlikely (font->global_subs_used == NULL))
1375
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1376
592
    } else {
1377
592
  font->global_subs_used = NULL;
1378
592
    }
1379
1380
616
    if (num_subs < 1240)
1381
616
        font->global_sub_bias = 107;
1382
0
    else if (num_subs < 33900)
1383
0
        font->global_sub_bias = 1131;
1384
0
    else
1385
0
        font->global_sub_bias = 32768;
1386
1387
616
    return CAIRO_STATUS_SUCCESS;
1388
616
}
1389
1390
typedef cairo_int_status_t
1391
(*font_read_t) (cairo_cff_font_t *font);
1392
1393
static const font_read_t font_read_funcs[] = {
1394
    cairo_cff_font_read_header,
1395
    cairo_cff_font_read_name,
1396
    cairo_cff_font_read_top_dict,
1397
    cairo_cff_font_read_strings,
1398
    cairo_cff_font_read_global_subroutines,
1399
};
1400
1401
static cairo_int_status_t
1402
cairo_cff_font_read_font (cairo_cff_font_t *font)
1403
762
{
1404
762
    cairo_int_status_t status;
1405
762
    unsigned int i;
1406
1407
4.14k
    for (i = 0; i < ARRAY_LENGTH (font_read_funcs); i++) {
1408
3.53k
        status = font_read_funcs[i] (font);
1409
3.53k
        if (unlikely (status))
1410
146
            return status;
1411
3.53k
    }
1412
1413
616
    return CAIRO_STATUS_SUCCESS;
1414
762
}
1415
1416
static cairo_status_t
1417
cairo_cff_font_set_ros_strings (cairo_cff_font_t *font)
1418
314
{
1419
314
    cairo_status_t status;
1420
314
    unsigned char buf[30];
1421
314
    unsigned char *p;
1422
314
    int sid1, sid2;
1423
314
    const char *registry = "Adobe";
1424
314
    const char *ordering = "Identity";
1425
1426
314
    sid1 = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1427
314
    status = cff_index_append_copy (&font->strings_subset_index,
1428
314
                                    (unsigned char *)registry,
1429
314
                                    strlen(registry));
1430
314
    if (unlikely (status))
1431
0
  return status;
1432
1433
314
    sid2 = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1434
314
    status = cff_index_append_copy (&font->strings_subset_index,
1435
314
                                    (unsigned char *)ordering,
1436
314
            strlen(ordering));
1437
314
    if (unlikely (status))
1438
0
  return status;
1439
1440
314
    p = encode_integer (buf, sid1);
1441
314
    p = encode_integer (p, sid2);
1442
314
    p = encode_integer (p, 0);
1443
314
    status = cff_dict_set_operands (font->top_dict, ROS_OP, buf, p - buf);
1444
314
    if (unlikely (status))
1445
0
  return status;
1446
1447
314
    p = encode_integer (buf, font->scaled_font_subset->num_glyphs);
1448
314
    status = cff_dict_set_operands (font->top_dict, CIDCOUNT_OP, buf, p - buf);
1449
314
    if (unlikely (status))
1450
0
  return status;
1451
1452
314
    return CAIRO_STATUS_SUCCESS;
1453
314
}
1454
1455
static cairo_status_t
1456
cairo_cff_font_subset_dict_string(cairo_cff_font_t   *font,
1457
                                  cairo_hash_table_t *dict,
1458
                                  int                 operator)
1459
11.8k
{
1460
11.8k
    int size;
1461
11.8k
    unsigned char *p;
1462
11.8k
    int sid;
1463
11.8k
    unsigned char buf[100];
1464
11.8k
    cff_index_element_t *element;
1465
11.8k
    cairo_status_t status;
1466
1467
11.8k
    p = cff_dict_get_operands (dict, operator, &size);
1468
11.8k
    if (!p)
1469
9.73k
        return CAIRO_STATUS_SUCCESS;
1470
1471
2.10k
    decode_integer (p, &sid);
1472
2.10k
    if (sid < NUM_STD_STRINGS)
1473
429
        return CAIRO_STATUS_SUCCESS;
1474
1475
1.67k
    element = _cairo_array_index (&font->strings_index, sid - NUM_STD_STRINGS);
1476
1.67k
    sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1477
1.67k
    status = cff_index_append (&font->strings_subset_index, element->data, element->length);
1478
1.67k
    if (unlikely (status))
1479
0
        return status;
1480
1481
1.67k
    p = encode_integer (buf, sid);
1482
1.67k
    status = cff_dict_set_operands (dict, operator, buf, p - buf);
1483
1.67k
    if (unlikely (status))
1484
0
  return status;
1485
1486
1.67k
    return CAIRO_STATUS_SUCCESS;
1487
1.67k
}
1488
1489
static const int dict_strings[] = {
1490
    VERSION_OP,
1491
    NOTICE_OP,
1492
    COPYRIGHT_OP,
1493
    FULLNAME_OP,
1494
    FAMILYNAME_OP,
1495
    WEIGHT_OP,
1496
    POSTSCRIPT_OP,
1497
    BASEFONTNAME_OP,
1498
    FONTNAME_OP,
1499
};
1500
1501
static cairo_status_t
1502
cairo_cff_font_subset_dict_strings (cairo_cff_font_t   *font,
1503
                                    cairo_hash_table_t *dict)
1504
1.31k
{
1505
1.31k
    cairo_status_t status;
1506
1.31k
    unsigned int i;
1507
1508
13.1k
    for (i = 0; i < ARRAY_LENGTH (dict_strings); i++) {
1509
11.8k
        status = cairo_cff_font_subset_dict_string (font, dict, dict_strings[i]);
1510
11.8k
        if (unlikely (status))
1511
0
            return status;
1512
11.8k
    }
1513
1514
1.31k
    return CAIRO_STATUS_SUCCESS;
1515
1.31k
}
1516
1517
static unsigned char *
1518
type2_decode_integer (unsigned char *p, int *integer)
1519
856k
{
1520
856k
    if (*p == 28) {
1521
8
  *integer = p[1] << 8 | p[2];
1522
8
  p += 3;
1523
856k
    } else if (*p <= 246) {
1524
751k
        *integer = *p++ - 139;
1525
751k
    } else if (*p <= 250) {
1526
66.2k
        *integer = (p[0] - 247) * 256 + p[1] + 108;
1527
66.2k
        p += 2;
1528
66.2k
    } else if (*p <= 254) {
1529
36.1k
        *integer = -(p[0] - 251) * 256 - p[1] - 108;
1530
36.1k
        p += 2;
1531
36.1k
    } else { /* *p == 255 */
1532
   /* 16.16 fixed-point number. The fraction is ignored. */
1533
1.82k
   *integer = (int16_t)((p[1] << 8) | p[2]);
1534
1.82k
        p += 5;
1535
1.82k
    }
1536
856k
    return p;
1537
856k
}
1538
1539
/* Type 2 charstring parser for finding calls to local or global
1540
 * subroutines. For non Opentype CFF fonts it also gets the glyph
1541
 * widths.
1542
 *
1543
 * When we find a subroutine operator, the subroutine is marked as in
1544
 * use and recursively followed. The subroutine number is the value on
1545
 * the top of the stack when the subroutine operator is executed. In
1546
 * most fonts the subroutine number is encoded in an integer
1547
 * immediately preceding the subroutine operator. However it is
1548
 * possible for the subroutine number on the stack to be the result of
1549
 * a computation (in which case there will be an operator preceding
1550
 * the subroutine operator). If this occurs, subroutine subsetting is
1551
 * disabled since we can't easily determine which subroutines are
1552
 * used.
1553
 *
1554
 * The width, if present, is the first integer in the charstring. The
1555
 * only way to confirm if the integer at the start of the charstring is
1556
 * the width is when the first stack clearing operator is parsed,
1557
 * check if there is an extra integer left over on the stack.
1558
 *
1559
 * When the first stack clearing operator is encountered
1560
 * type2_find_width is set to FALSE and type2_found_width is set to
1561
 * TRUE if an extra argument is found, otherwise FALSE.
1562
 */
1563
static cairo_status_t
1564
cairo_cff_parse_charstring (cairo_cff_font_t *font,
1565
                            unsigned char *charstring, int length,
1566
                            int glyph_id,
1567
          cairo_bool_t need_width)
1568
10.8k
{
1569
10.8k
    unsigned char *p = charstring;
1570
10.8k
    unsigned char *end = charstring + length;
1571
10.8k
    int integer;
1572
10.8k
    int hint_bytes;
1573
10.8k
    int sub_num;
1574
10.8k
    cff_index_element_t *element;
1575
10.8k
    int fd;
1576
1577
1.03M
    while (p < end) {
1578
1.03M
        if (*p == 28 || *p >= 32) {
1579
            /* Integer value */
1580
856k
            p = type2_decode_integer (p, &integer);
1581
856k
            font->type2_stack_size++;
1582
856k
            font->type2_stack_top_value = integer;
1583
856k
            font->type2_stack_top_is_int = TRUE;
1584
856k
      if (!font->type2_seen_first_int) {
1585
9.61k
    font->type2_width = integer;
1586
9.61k
    font->type2_seen_first_int = TRUE;
1587
9.61k
      }
1588
856k
  } else if (*p == TYPE2_hstem || *p == TYPE2_vstem ||
1589
168k
       *p == TYPE2_hstemhm || *p == TYPE2_vstemhm) {
1590
            /* Hint operator. The number of hints declared by the
1591
             * operator depends on the size of the stack. */
1592
10.6k
      font->type2_stack_top_is_int = FALSE;
1593
10.6k
      font->type2_num_hints += font->type2_stack_size/2;
1594
10.6k
      if (font->type2_find_width && font->type2_stack_size % 2)
1595
4.47k
    font->type2_found_width = TRUE;
1596
1597
10.6k
      font->type2_stack_size = 0;
1598
10.6k
      font->type2_find_width = FALSE;
1599
10.6k
      p++;
1600
166k
  } else if (*p == TYPE2_hintmask || *p == TYPE2_cntrmask) {
1601
      /* Hintmask operator. These operators are followed by a
1602
       * variable length mask where the length depends on the
1603
       * number of hints declared. The first time this is called
1604
       * it is also an implicit vstem if there are arguments on
1605
       * the stack. */
1606
5.77k
      if (font->type2_hintmask_bytes == 0) {
1607
1.77k
    font->type2_stack_top_is_int = FALSE;
1608
1.77k
    font->type2_num_hints += font->type2_stack_size/2;
1609
1.77k
    if (font->type2_find_width && font->type2_stack_size % 2)
1610
0
        font->type2_found_width = TRUE;
1611
1612
1.77k
    font->type2_stack_size = 0;
1613
1.77k
    font->type2_find_width = FALSE;
1614
1.77k
    font->type2_hintmask_bytes = (font->type2_num_hints+7)/8;
1615
1.77k
      }
1616
1617
5.77k
      hint_bytes = font->type2_hintmask_bytes;
1618
5.77k
      p++;
1619
5.77k
      p += hint_bytes;
1620
160k
  } else if (*p == TYPE2_rmoveto) {
1621
11.7k
      if (font->type2_find_width && font->type2_stack_size > 2)
1622
2.21k
    font->type2_found_width = TRUE;
1623
1624
11.7k
      font->type2_stack_size = 0;
1625
11.7k
      font->type2_find_width = FALSE;
1626
11.7k
      font->type2_has_path = TRUE;
1627
11.7k
      p++;
1628
148k
  } else if (*p == TYPE2_hmoveto || *p == TYPE2_vmoveto) {
1629
2.15k
      if (font->type2_find_width && font->type2_stack_size > 1)
1630
103
    font->type2_found_width = TRUE;
1631
1632
2.15k
      font->type2_stack_size = 0;
1633
2.15k
      font->type2_find_width = FALSE;
1634
2.15k
      font->type2_has_path = TRUE;
1635
2.15k
      p++;
1636
146k
  } else if (*p == TYPE2_endchar) {
1637
9.53k
      if (!font->type2_has_path && font->type2_stack_size > 3)
1638
40
    return CAIRO_INT_STATUS_UNSUPPORTED; /* seac (Ref Appendix C of Type 2 Charstring Format */
1639
1640
9.49k
      if (font->type2_find_width && font->type2_stack_size > 0)
1641
414
    font->type2_found_width = TRUE;
1642
1643
9.49k
      return CAIRO_STATUS_SUCCESS;
1644
137k
        } else if (*p == TYPE2_callsubr) {
1645
            /* call to local subroutine */
1646
697
      if (! font->type2_stack_top_is_int)
1647
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1648
1649
697
            if (++font->type2_nesting_level > MAX_SUBROUTINE_NESTING)
1650
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1651
1652
697
            p++;
1653
697
      font->type2_stack_top_is_int = FALSE;
1654
697
            font->type2_stack_size--;
1655
697
      if (font->type2_find_width && font->type2_stack_size == 0)
1656
84
    font->type2_seen_first_int = FALSE;
1657
1658
697
            if (font->is_cid) {
1659
0
                fd = font->fdselect[glyph_id];
1660
0
    sub_num = font->type2_stack_top_value + font->fd_local_sub_bias[fd];
1661
0
    if (sub_num >= (int)_cairo_array_num_elements(&font->fd_local_sub_index[fd]))
1662
0
        return CAIRO_INT_STATUS_UNSUPPORTED;
1663
0
                element = _cairo_array_index (&font->fd_local_sub_index[fd], sub_num);
1664
0
                if (! font->fd_local_subs_used[fd][sub_num]) {
1665
0
        font->fd_local_subs_used[fd][sub_num] = TRUE;
1666
0
        cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1667
0
    }
1668
697
            } else {
1669
697
    sub_num = font->type2_stack_top_value + font->local_sub_bias;
1670
697
    if (sub_num >= (int)_cairo_array_num_elements(&font->local_sub_index))
1671
1
        return CAIRO_INT_STATUS_UNSUPPORTED;
1672
696
                element = _cairo_array_index (&font->local_sub_index, sub_num);
1673
696
                if (! font->local_subs_used[sub_num] ||
1674
105
        (need_width && !font->type2_found_width))
1675
650
    {
1676
650
        font->local_subs_used[sub_num] = TRUE;
1677
650
        cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1678
650
    }
1679
696
            }
1680
696
            font->type2_nesting_level--;
1681
136k
        } else if (*p == TYPE2_callgsubr) {
1682
            /* call to global subroutine */
1683
664
      if (! font->type2_stack_top_is_int)
1684
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1685
1686
664
            if (++font->type2_nesting_level > MAX_SUBROUTINE_NESTING)
1687
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1688
1689
664
            p++;
1690
664
            font->type2_stack_size--;
1691
664
      font->type2_stack_top_is_int = FALSE;
1692
664
      if (font->type2_find_width && font->type2_stack_size == 0)
1693
89
    font->type2_seen_first_int = FALSE;
1694
1695
664
      sub_num = font->type2_stack_top_value + font->global_sub_bias;
1696
664
      if (sub_num >= (int)_cairo_array_num_elements(&font->global_sub_index))
1697
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1698
664
      element = _cairo_array_index (&font->global_sub_index, sub_num);
1699
664
            if (! font->global_subs_used[sub_num] ||
1700
133
    (need_width && !font->type2_found_width))
1701
576
      {
1702
576
                font->global_subs_used[sub_num] = TRUE;
1703
576
                cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1704
576
            }
1705
664
            font->type2_nesting_level--;
1706
135k
        } else if (*p == 12) {
1707
            /* 2 byte instruction */
1708
1709
      /* All the 2 byte operators are either not valid before a
1710
       * stack clearing operator or they are one of the
1711
       * arithmetic, storage, or conditional operators. */
1712
33
      if (need_width && font->type2_find_width)
1713
2
    return CAIRO_INT_STATUS_UNSUPPORTED;
1714
1715
31
            p += 2;
1716
31
      font->type2_stack_top_is_int = FALSE;
1717
135k
  } else {
1718
            /* 1 byte instruction */
1719
135k
            p++;
1720
135k
      font->type2_stack_top_is_int = FALSE;
1721
135k
        }
1722
1.03M
    }
1723
1724
1.27k
    return CAIRO_STATUS_SUCCESS;
1725
10.8k
}
1726
1727
static cairo_status_t
1728
cairo_cff_find_width_and_subroutines_used (cairo_cff_font_t  *font,
1729
             unsigned char *charstring, int length,
1730
             int glyph_id, int subset_id)
1731
9.58k
{
1732
9.58k
    cairo_status_t status;
1733
9.58k
    int width;
1734
9.58k
    int fd;
1735
1736
9.58k
    font->type2_stack_size = 0;
1737
9.58k
    font->type2_stack_top_value = 0;;
1738
9.58k
    font->type2_stack_top_is_int = FALSE;
1739
9.58k
    font->type2_num_hints = 0;
1740
9.58k
    font->type2_hintmask_bytes = 0;
1741
9.58k
    font->type2_nesting_level = 0;
1742
9.58k
    font->type2_seen_first_int = FALSE;
1743
9.58k
    font->type2_find_width = TRUE;
1744
9.58k
    font->type2_found_width = FALSE;
1745
9.58k
    font->type2_width = 0;
1746
9.58k
    font->type2_has_path = FALSE;
1747
1748
9.58k
    status = cairo_cff_parse_charstring (font, charstring, length, glyph_id, TRUE);
1749
9.58k
    if (status)
1750
43
  return status;
1751
1752
9.54k
    if (!font->is_opentype) {
1753
9.27k
        if (font->is_cid) {
1754
1.26k
            fd = font->fdselect[glyph_id];
1755
1.26k
            if (font->type2_found_width)
1756
979
                width = font->fd_nominal_width[fd] + font->type2_width;
1757
282
            else
1758
282
                width = font->fd_default_width[fd];
1759
8.00k
        } else {
1760
8.00k
            if (font->type2_found_width)
1761
5.95k
                width = font->nominal_width + font->type2_width;
1762
2.05k
            else
1763
2.05k
                width = font->default_width;
1764
8.00k
        }
1765
9.27k
        font->widths[subset_id] = width;
1766
9.27k
    }
1767
1768
9.54k
    return CAIRO_STATUS_SUCCESS;
1769
9.58k
}
1770
1771
static cairo_int_status_t
1772
cairo_cff_font_get_gid_for_cid (cairo_cff_font_t  *font, unsigned long cid, unsigned long *gid)
1773
2.52k
{
1774
2.52k
    unsigned char *p;
1775
2.52k
    unsigned long first_gid;
1776
2.52k
    unsigned long first_cid;
1777
2.52k
    int num_left;
1778
2.52k
    unsigned long c, g;
1779
1780
2.52k
    if (cid == 0) {
1781
246
  *gid = 0;
1782
246
  return CAIRO_STATUS_SUCCESS;
1783
246
    }
1784
1785
2.27k
    switch (font->charset[0]) {
1786
  /* Format 0 */
1787
1.92k
  case 0:
1788
1.92k
      p = font->charset + 1;
1789
1.92k
      g = 1;
1790
36.6k
      while (g <= (unsigned)font->num_glyphs && p < font->data_end) {
1791
36.6k
    c = get_unaligned_be16 (p);
1792
36.6k
    if (c == cid) {
1793
1.92k
        *gid = g;
1794
1.92k
        return CAIRO_STATUS_SUCCESS;
1795
1.92k
    }
1796
34.6k
    g++;
1797
34.6k
    p += 2;
1798
34.6k
      }
1799
0
      break;
1800
1801
  /* Format 1 */
1802
352
  case 1:
1803
352
      first_gid = 1;
1804
352
      p = font->charset + 1;
1805
2.52k
      while (first_gid <= (unsigned)font->num_glyphs && p + 2 < font->data_end) {
1806
2.52k
    first_cid = get_unaligned_be16 (p);
1807
2.52k
    num_left = p[2];
1808
2.52k
    if (cid >= first_cid && cid <= first_cid + num_left) {
1809
352
        *gid = first_gid + cid - first_cid;
1810
352
        return CAIRO_STATUS_SUCCESS;
1811
352
    }
1812
2.17k
    first_gid += num_left + 1;
1813
2.17k
    p += 3;
1814
2.17k
      }
1815
0
      break;
1816
1817
  /* Format 2 */
1818
0
  case 2:
1819
0
      first_gid = 1;
1820
0
      p = font->charset + 1;
1821
0
      while (first_gid <= (unsigned)font->num_glyphs && p + 3 < font->data_end) {
1822
0
    first_cid = get_unaligned_be16 (p);
1823
0
    num_left = get_unaligned_be16 (p+2);
1824
0
    if (cid >= first_cid && cid <= first_cid + num_left) {
1825
0
        *gid = first_gid + cid - first_cid;
1826
0
        return CAIRO_STATUS_SUCCESS;
1827
0
    }
1828
0
    first_gid += num_left + 1;
1829
0
    p += 4;
1830
0
      }
1831
0
      break;
1832
1833
0
  default:
1834
0
      break;
1835
2.27k
    }
1836
0
    return CAIRO_INT_STATUS_UNSUPPORTED;
1837
2.27k
}
1838
1839
static cairo_int_status_t
1840
cairo_cff_font_subset_charstrings_and_subroutines (cairo_cff_font_t  *font)
1841
616
{
1842
616
    cff_index_element_t *element;
1843
616
    unsigned int i;
1844
616
    cairo_int_status_t status;
1845
616
    unsigned long glyph, cid;
1846
1847
616
    font->subset_subroutines = TRUE;
1848
10.1k
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
1849
9.61k
  if (font->is_cid && !font->is_opentype) {
1850
1.26k
      cid = font->scaled_font_subset->glyphs[i];
1851
1.26k
      status = cairo_cff_font_get_gid_for_cid (font, cid, &glyph);
1852
1.26k
      if (unlikely (status))
1853
0
    return status;
1854
8.35k
  } else {
1855
8.35k
      glyph = font->scaled_font_subset->glyphs[i];
1856
8.35k
  }
1857
9.61k
        element = _cairo_array_index (&font->charstrings_index, glyph);
1858
9.61k
        status = cff_index_append (&font->charstrings_subset_index,
1859
9.61k
                                   element->data,
1860
9.61k
                                   element->length);
1861
9.61k
        if (unlikely (status))
1862
0
            return status;
1863
1864
9.61k
  if (font->subset_subroutines) {
1865
9.58k
      status = cairo_cff_find_width_and_subroutines_used (font,
1866
9.58k
                element->data, element->length,
1867
9.58k
                glyph, i);
1868
9.58k
      if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1869
    /* If parsing the charstrings fails we embed all the
1870
     * subroutines. But if the font is not opentype we
1871
     * need to successfully parse all charstrings to get
1872
     * the widths. */
1873
43
    font->subset_subroutines = FALSE;
1874
43
    if (!font->is_opentype)
1875
34
        return status;
1876
9.54k
      } else if (unlikely (status)) {
1877
0
                return status;
1878
0
      }
1879
9.58k
        }
1880
9.61k
    }
1881
1882
582
    return CAIRO_STATUS_SUCCESS;
1883
616
}
1884
1885
static cairo_status_t
1886
cairo_cff_font_subset_fontdict (cairo_cff_font_t  *font)
1887
123
{
1888
123
    unsigned int i;
1889
123
    int fd;
1890
123
    int *reverse_map;
1891
123
    unsigned long cid, gid;
1892
123
    cairo_int_status_t status;
1893
1894
123
    font->fdselect_subset = _cairo_calloc_ab (font->scaled_font_subset->num_glyphs,
1895
123
                                     sizeof (int));
1896
123
    if (unlikely (font->fdselect_subset == NULL))
1897
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1898
1899
123
    font->fd_subset_map = _cairo_calloc_ab (font->num_fontdicts, sizeof (int));
1900
123
    if (unlikely (font->fd_subset_map == NULL))
1901
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1902
1903
123
    font->private_dict_offset = _cairo_calloc_ab (font->num_fontdicts, sizeof (int));
1904
123
    if (unlikely (font->private_dict_offset == NULL))
1905
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1906
1907
123
    reverse_map = _cairo_calloc_ab (font->num_fontdicts, sizeof (int));
1908
123
    if (unlikely (reverse_map == NULL))
1909
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1910
1911
260
    for (i = 0; i < font->num_fontdicts; i++)
1912
137
        reverse_map[i] = -1;
1913
1914
123
    font->num_subset_fontdicts = 0;
1915
1.38k
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
1916
1.26k
  if (font->is_opentype) {
1917
0
      gid = font->scaled_font_subset->glyphs[i];
1918
1.26k
  } else {
1919
1.26k
      cid = font->scaled_font_subset->glyphs[i];
1920
1.26k
      status = cairo_cff_font_get_gid_for_cid (font, cid, &gid);
1921
1.26k
      if (unlikely (status)) {
1922
0
    free (reverse_map);
1923
0
    return status;
1924
0
      }
1925
1.26k
  }
1926
1927
1.26k
        fd = font->fdselect[gid];
1928
1.26k
        if (fd < 0 || (unsigned int) fd >= font->num_fontdicts) {
1929
0
            free (reverse_map);
1930
0
            return CAIRO_INT_STATUS_UNSUPPORTED;
1931
0
        }
1932
1.26k
        if (reverse_map[fd] < 0) {
1933
137
            font->fd_subset_map[font->num_subset_fontdicts] = fd;
1934
137
            reverse_map[fd] = font->num_subset_fontdicts++;
1935
137
        }
1936
1.26k
        font->fdselect_subset[i] = reverse_map[fd];
1937
1.26k
    }
1938
1939
123
    free (reverse_map);
1940
1941
123
    return CAIRO_STATUS_SUCCESS;
1942
123
}
1943
1944
static cairo_status_t
1945
cairo_cff_font_create_cid_fontdict (cairo_cff_font_t *font)
1946
188
{
1947
188
    unsigned char buf[100];
1948
188
    unsigned char *end_buf;
1949
188
    cairo_status_t status;
1950
1951
188
    font->num_fontdicts = 1;
1952
188
    font->fd_dict = _cairo_malloc (sizeof (cairo_hash_table_t *));
1953
188
    if (unlikely (font->fd_dict == NULL))
1954
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1955
1956
188
    if (cff_dict_init (&font->fd_dict[0])) {
1957
0
  free (font->fd_dict);
1958
0
  font->fd_dict = NULL;
1959
0
  font->num_fontdicts = 0;
1960
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1961
0
    }
1962
1963
188
    font->fd_subset_map = _cairo_malloc (sizeof (int));
1964
188
    if (unlikely (font->fd_subset_map == NULL))
1965
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1966
1967
188
    font->private_dict_offset = _cairo_malloc (sizeof (int));
1968
188
    if (unlikely (font->private_dict_offset == NULL))
1969
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1970
1971
188
    font->fd_subset_map[0] = 0;
1972
188
    font->num_subset_fontdicts = 1;
1973
1974
    /* Set integer operand to max value to use max size encoding to reserve
1975
     * space for any value later */
1976
188
    end_buf = encode_integer_max (buf, 0);
1977
188
    end_buf = encode_integer_max (end_buf, 0);
1978
188
    status = cff_dict_set_operands (font->fd_dict[0], PRIVATE_OP, buf, end_buf - buf);
1979
188
    if (unlikely (status))
1980
0
  return status;
1981
1982
188
    return CAIRO_STATUS_SUCCESS;
1983
188
}
1984
1985
static cairo_status_t
1986
cairo_cff_font_subset_strings (cairo_cff_font_t *font)
1987
582
{
1988
582
    cairo_status_t status;
1989
582
    unsigned int i;
1990
1991
582
    status = cairo_cff_font_subset_dict_strings (font, font->top_dict);
1992
582
    if (unlikely (status))
1993
0
        return status;
1994
1995
582
    if (font->is_cid) {
1996
260
        for (i = 0; i < font->num_subset_fontdicts; i++) {
1997
137
            status = cairo_cff_font_subset_dict_strings (font, font->fd_dict[font->fd_subset_map[i]]);
1998
137
            if (unlikely (status))
1999
0
                return status;
2000
2001
137
            status = cairo_cff_font_subset_dict_strings (font, font->fd_private_dict[font->fd_subset_map[i]]);
2002
137
            if (unlikely (status))
2003
0
                return status;
2004
137
        }
2005
459
    } else {
2006
459
        status = cairo_cff_font_subset_dict_strings (font, font->private_dict);
2007
459
    }
2008
2009
582
    return status;
2010
582
}
2011
2012
/* The Euro is the only the only character in the winansi encoding
2013
 * with a glyph name that is not a CFF standard string. As the strings
2014
 * are written before the charset, we need to check during the
2015
 * subsetting phase if the Euro glyph is required and add the
2016
 * glyphname to the list of strings to write out.
2017
 */
2018
static cairo_status_t
2019
cairo_cff_font_add_euro_charset_string (cairo_cff_font_t *font)
2020
659
{
2021
659
    cairo_status_t status;
2022
659
    unsigned int i;
2023
659
    int ch;
2024
659
    const char *euro = "Euro";
2025
2026
10.6k
    for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
2027
10.0k
  ch = font->scaled_font_subset->to_latin_char[i];
2028
10.0k
  if (ch == 128) {
2029
4
      font->euro_sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
2030
4
      status = cff_index_append_copy (&font->strings_subset_index,
2031
4
              (unsigned char *)euro, strlen(euro));
2032
4
      return status;
2033
4
  }
2034
10.0k
    }
2035
2036
655
    return CAIRO_STATUS_SUCCESS;
2037
659
}
2038
2039
static cairo_status_t
2040
cairo_cff_font_subset_font (cairo_cff_font_t  *font)
2041
616
{
2042
616
    cairo_status_t status;
2043
2044
616
    if (!font->scaled_font_subset->is_latin) {
2045
224
  status = cairo_cff_font_set_ros_strings (font);
2046
224
  if (unlikely (status))
2047
0
      return status;
2048
224
    }
2049
2050
616
    status = cairo_cff_font_subset_charstrings_and_subroutines (font);
2051
616
    if (unlikely (status))
2052
34
        return status;
2053
2054
582
    if (!font->scaled_font_subset->is_latin) {
2055
221
  if (font->is_cid)
2056
123
      status = cairo_cff_font_subset_fontdict (font);
2057
98
  else
2058
98
      status = cairo_cff_font_create_cid_fontdict (font);
2059
221
  if (unlikely (status))
2060
0
      return status;
2061
361
    }  else {
2062
361
  font->private_dict_offset = _cairo_malloc (sizeof (int));
2063
361
  if (unlikely (font->private_dict_offset == NULL))
2064
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2065
361
    }
2066
2067
582
    status = cairo_cff_font_subset_strings (font);
2068
582
    if (unlikely (status))
2069
0
        return status;
2070
2071
582
    if (font->scaled_font_subset->is_latin)
2072
361
  status = cairo_cff_font_add_euro_charset_string (font);
2073
2074
582
    return status;
2075
582
}
2076
2077
/* Set the operand of the specified operator in the (already written)
2078
 * top dict to point to the current position in the output
2079
 * array. Operands updated with this function must have previously
2080
 * been encoded with the 5-byte (max) integer encoding. */
2081
static void
2082
cairo_cff_font_set_topdict_operator_to_cur_pos (cairo_cff_font_t  *font,
2083
                                                int                operator)
2084
3.22k
{
2085
3.22k
    int cur_pos;
2086
3.22k
    int offset;
2087
3.22k
    int size;
2088
3.22k
    unsigned char buf[10];
2089
3.22k
    unsigned char *buf_end;
2090
3.22k
    unsigned char *op_ptr;
2091
2092
3.22k
    cur_pos = _cairo_array_num_elements (&font->output);
2093
3.22k
    buf_end = encode_integer_max (buf, cur_pos);
2094
3.22k
    offset = cff_dict_get_location (font->top_dict, operator, &size);
2095
3.22k
    assert (offset > 0);
2096
3.22k
    op_ptr = _cairo_array_index (&font->output, offset);
2097
3.22k
    memcpy (op_ptr, buf, buf_end - buf);
2098
3.22k
}
2099
2100
static cairo_status_t
2101
cairo_cff_font_write_header (cairo_cff_font_t *font)
2102
970
{
2103
970
    return _cairo_array_append_multiple (&font->output,
2104
970
                                         font->header,
2105
970
                                         font->header->header_size);
2106
970
}
2107
2108
static cairo_status_t
2109
cairo_cff_font_write_name (cairo_cff_font_t *font)
2110
970
{
2111
970
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
2112
970
    cairo_array_t index;
2113
2114
970
    cff_index_init (&index);
2115
2116
970
    status = cff_index_append_copy (&index,
2117
970
                                    (unsigned char *) font->ps_name,
2118
970
                                    strlen(font->ps_name));
2119
970
    if (unlikely (status))
2120
0
  goto FAIL;
2121
2122
970
    status = cff_index_write (&index, &font->output);
2123
970
    if (unlikely (status))
2124
0
        goto FAIL;
2125
2126
970
FAIL:
2127
970
    cff_index_fini (&index);
2128
2129
970
    return status;
2130
970
}
2131
2132
static cairo_status_t
2133
cairo_cff_font_write_top_dict (cairo_cff_font_t *font)
2134
970
{
2135
970
    uint16_t count;
2136
970
    unsigned char buf[10];
2137
970
    unsigned char *p;
2138
970
    int offset_index;
2139
970
    int dict_start, dict_size;
2140
970
    int offset_size = 4;
2141
970
    cairo_status_t status;
2142
2143
    /* Write an index containing the top dict */
2144
2145
970
    count = cpu_to_be16 (1);
2146
970
    status = _cairo_array_append_multiple (&font->output, &count, 2);
2147
970
    if (unlikely (status))
2148
0
        return status;
2149
970
    buf[0] = offset_size;
2150
970
    status = _cairo_array_append (&font->output, buf);
2151
970
    if (unlikely (status))
2152
0
        return status;
2153
970
    encode_index_offset (buf, offset_size, 1);
2154
970
    status = _cairo_array_append_multiple (&font->output, buf, offset_size);
2155
970
    if (unlikely (status))
2156
0
        return status;
2157
2158
    /* Reserve space for last element of offset array and update after
2159
     * dict is written */
2160
970
    offset_index = _cairo_array_num_elements (&font->output);
2161
970
    status = _cairo_array_append_multiple (&font->output, buf, offset_size);
2162
970
    if (unlikely (status))
2163
0
        return status;
2164
2165
970
    dict_start = _cairo_array_num_elements (&font->output);
2166
970
    status = cff_dict_write (font->top_dict, &font->output);
2167
970
    if (unlikely (status))
2168
0
        return status;
2169
970
    dict_size = _cairo_array_num_elements (&font->output) - dict_start;
2170
2171
970
    encode_index_offset (buf, offset_size, dict_size + 1);
2172
970
    p = _cairo_array_index (&font->output, offset_index);
2173
970
    memcpy (p, buf, offset_size);
2174
2175
970
    return CAIRO_STATUS_SUCCESS;
2176
970
}
2177
2178
static cairo_status_t
2179
cairo_cff_font_write_strings (cairo_cff_font_t  *font)
2180
970
{
2181
970
    return cff_index_write (&font->strings_subset_index, &font->output);
2182
970
}
2183
2184
static cairo_status_t
2185
cairo_cff_font_write_global_subrs (cairo_cff_font_t  *font)
2186
970
{
2187
970
    unsigned int i;
2188
970
    unsigned char return_op = TYPE2_return;
2189
2190
    /* poppler and fontforge don't like zero length subroutines so we
2191
     * replace unused subroutines with a 'return' instruction. */
2192
970
    if (font->subset_subroutines) {
2193
5.02k
        for (i = 0; i < _cairo_array_num_elements (&font->global_sub_index); i++) {
2194
4.45k
            if (! font->global_subs_used[i])
2195
4.04k
                cff_index_set_object (&font->global_sub_index, i, &return_op, 1);
2196
4.45k
        }
2197
573
    }
2198
2199
970
    return cff_index_write (&font->global_sub_index, &font->output);
2200
970
}
2201
2202
static cairo_status_t
2203
cairo_cff_font_write_encoding (cairo_cff_font_t  *font)
2204
659
{
2205
659
    unsigned char buf[2];
2206
659
    cairo_status_t status;
2207
659
    unsigned int i;
2208
2209
659
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, ENCODING_OP);
2210
659
    buf[0] = 0; /* Format 0 */
2211
659
    buf[1] = font->scaled_font_subset->num_glyphs - 1;
2212
659
    status = _cairo_array_append_multiple (&font->output, buf, 2);
2213
659
    if (unlikely (status))
2214
0
  return status;
2215
2216
10.7k
    for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
2217
10.1k
  unsigned char ch = font->scaled_font_subset->to_latin_char[i];
2218
10.1k
        status = _cairo_array_append (&font->output, &ch);
2219
10.1k
        if (unlikely (status))
2220
0
            return status;
2221
10.1k
    }
2222
2223
659
    return CAIRO_STATUS_SUCCESS;
2224
659
}
2225
2226
static cairo_status_t
2227
cairo_cff_font_write_fdselect (cairo_cff_font_t  *font)
2228
311
{
2229
311
    unsigned char data;
2230
311
    unsigned int i;
2231
311
    cairo_int_status_t status;
2232
2233
311
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, FDSELECT_OP);
2234
2235
311
    if (font->is_cid) {
2236
123
        data = 0;
2237
123
        status = _cairo_array_append (&font->output, &data);
2238
123
        if (unlikely (status))
2239
0
            return status;
2240
2241
1.38k
        for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
2242
1.26k
            data = font->fdselect_subset[i];
2243
1.26k
            status = _cairo_array_append (&font->output, &data);
2244
1.26k
            if (unlikely (status))
2245
0
                return status;
2246
1.26k
        }
2247
188
    } else {
2248
188
        unsigned char byte;
2249
188
        uint16_t word;
2250
2251
188
        status = _cairo_array_grow_by (&font->output, 9);
2252
188
        if (unlikely (status))
2253
0
            return status;
2254
2255
188
        byte = 3;
2256
188
        status = _cairo_array_append (&font->output, &byte);
2257
188
        assert (status == CAIRO_INT_STATUS_SUCCESS);
2258
2259
188
        word = cpu_to_be16 (1);
2260
188
        status = _cairo_array_append_multiple (&font->output, &word, 2);
2261
188
        assert (status == CAIRO_INT_STATUS_SUCCESS);
2262
2263
188
        word = cpu_to_be16 (0);
2264
188
        status = _cairo_array_append_multiple (&font->output, &word, 2);
2265
188
        assert (status == CAIRO_INT_STATUS_SUCCESS);
2266
2267
188
        byte = 0;
2268
188
        status = _cairo_array_append (&font->output, &byte);
2269
188
        assert (status == CAIRO_INT_STATUS_SUCCESS);
2270
2271
188
        word = cpu_to_be16 (font->scaled_font_subset->num_glyphs);
2272
188
        status = _cairo_array_append_multiple (&font->output, &word, 2);
2273
188
        assert (status == CAIRO_INT_STATUS_SUCCESS);
2274
188
    }
2275
2276
311
    return CAIRO_STATUS_SUCCESS;
2277
311
}
2278
2279
/* Winansi to CFF standard strings mapping for characters 128 to 255 */
2280
static const int winansi_to_cff_std_string[] = {
2281
  /* 128 */
2282
      0,   0, 117, 101, 118, 121, 112, 113,
2283
    126, 122, 192, 107, 142,   0, 199,   0,
2284
  /* 144 */
2285
      0,  65,   8, 105, 119, 116, 111, 137,
2286
    127, 153, 221, 108, 148,   0, 228, 198,
2287
  /* 160 */
2288
      0,  96,  97,  98, 103, 100, 160, 102,
2289
    131, 170, 139, 106, 151,   0, 165, 128,
2290
  /* 176 */
2291
    161, 156, 164, 169, 125, 152, 115, 114,
2292
    133, 150, 143, 120, 158, 155, 163, 123,
2293
  /* 192 */
2294
    174, 171, 172, 176, 173, 175, 138, 177,
2295
    181, 178, 179, 180, 185, 182, 183, 184,
2296
  /* 208 */
2297
    154, 186, 190, 187, 188, 191, 189, 168,
2298
    141, 196, 193, 194, 195, 197, 157, 149,
2299
  /* 224 */
2300
    203, 200, 201, 205, 202, 204, 144, 206,
2301
    210, 207, 208, 209, 214, 211, 212, 213,
2302
  /* 240 */
2303
    167, 215, 219, 216, 217, 220, 218, 159,
2304
    147, 225, 222, 223, 224, 226, 162, 227,
2305
};
2306
2307
static int
2308
cairo_cff_font_get_sid_for_winansi_char (cairo_cff_font_t  *font, int ch)
2309
10.1k
{
2310
10.1k
    int sid;
2311
2312
10.1k
    if (ch == 39) {
2313
11
  sid = 104;
2314
2315
10.1k
    } else if (ch == 96) {
2316
0
  sid = 124;
2317
2318
10.1k
    } else if (ch >= 32 && ch <= 126) {
2319
9.90k
  sid = ch - 31;
2320
2321
9.90k
    } else if (ch == 128) {
2322
4
  assert (font->euro_sid >= NUM_STD_STRINGS);
2323
4
  sid = font->euro_sid;
2324
2325
218
    } else if (ch >= 128 && ch <= 255) {
2326
218
  sid = winansi_to_cff_std_string[ch - 128];
2327
2328
218
    } else {
2329
0
  sid = 0;
2330
0
    }
2331
2332
10.1k
    return sid;
2333
10.1k
}
2334
2335
static cairo_status_t
2336
cairo_cff_font_write_type1_charset (cairo_cff_font_t  *font)
2337
659
{
2338
659
    unsigned char format = 0;
2339
659
    unsigned int i;
2340
659
    int ch, sid;
2341
659
    cairo_status_t status;
2342
659
    uint16_t sid_be16;
2343
2344
659
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSET_OP);
2345
659
    status = _cairo_array_append (&font->output, &format);
2346
659
    if (unlikely (status))
2347
0
  return status;
2348
2349
10.7k
    for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
2350
10.1k
  ch = font->scaled_font_subset->to_latin_char[i];
2351
10.1k
  sid = cairo_cff_font_get_sid_for_winansi_char (font, ch);
2352
10.1k
        if (unlikely (status))
2353
0
      return status;
2354
2355
10.1k
  sid_be16 = cpu_to_be16(sid);
2356
10.1k
  status = _cairo_array_append_multiple (&font->output, &sid_be16, sizeof(sid_be16));
2357
10.1k
        if (unlikely (status))
2358
0
            return status;
2359
10.1k
    }
2360
2361
659
    return CAIRO_STATUS_SUCCESS;
2362
659
}
2363
2364
static cairo_status_t
2365
cairo_cff_font_write_cid_charset (cairo_cff_font_t  *font)
2366
311
{
2367
311
    unsigned char byte;
2368
311
    uint16_t word;
2369
311
    cairo_status_t status;
2370
2371
311
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSET_OP);
2372
311
    status = _cairo_array_grow_by (&font->output, 5);
2373
311
    if (unlikely (status))
2374
0
        return status;
2375
2376
311
    byte = 2;
2377
311
    status = _cairo_array_append (&font->output, &byte);
2378
311
    assert (status == CAIRO_STATUS_SUCCESS);
2379
2380
311
    word = cpu_to_be16 (1);
2381
311
    status = _cairo_array_append_multiple (&font->output, &word, 2);
2382
311
    assert (status == CAIRO_STATUS_SUCCESS);
2383
2384
311
    word = cpu_to_be16 (font->scaled_font_subset->num_glyphs - 2);
2385
311
    status = _cairo_array_append_multiple (&font->output, &word, 2);
2386
311
    assert (status == CAIRO_STATUS_SUCCESS);
2387
2388
311
    return CAIRO_STATUS_SUCCESS;
2389
311
}
2390
2391
static cairo_status_t
2392
cairo_cff_font_write_charstrings (cairo_cff_font_t  *font)
2393
970
{
2394
970
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSTRINGS_OP);
2395
2396
970
    return cff_index_write (&font->charstrings_subset_index, &font->output);
2397
970
}
2398
2399
static cairo_status_t
2400
cairo_cff_font_write_cid_fontdict (cairo_cff_font_t *font)
2401
311
{
2402
311
    unsigned int i;
2403
311
    cairo_int_status_t status;
2404
311
    unsigned int offset_array;
2405
311
    unsigned char *offset_array_ptr;
2406
311
    int offset_base;
2407
311
    uint16_t count;
2408
311
    uint8_t offset_size = 4;
2409
2410
311
    cairo_cff_font_set_topdict_operator_to_cur_pos (font, FDARRAY_OP);
2411
311
    count = cpu_to_be16 (font->num_subset_fontdicts);
2412
311
    status = _cairo_array_append_multiple (&font->output, &count, sizeof (uint16_t));
2413
311
    if (unlikely (status))
2414
0
        return status;
2415
311
    status = _cairo_array_append (&font->output, &offset_size);
2416
311
    if (unlikely (status))
2417
0
        return status;
2418
2419
311
    offset_array = _cairo_array_num_elements (&font->output);
2420
311
    status = _cairo_array_allocate (&font->output,
2421
311
                                    (font->num_subset_fontdicts + 1)*offset_size,
2422
311
                                    (void **) &offset_array_ptr);
2423
311
    if (unlikely (status))
2424
0
        return status;
2425
311
    offset_base = _cairo_array_num_elements (&font->output) - 1;
2426
311
    put_unaligned_be32(1, offset_array_ptr);
2427
311
    offset_array += sizeof(uint32_t);
2428
636
    for (i = 0; i < font->num_subset_fontdicts; i++) {
2429
325
        status = cff_dict_write (font->fd_dict[font->fd_subset_map[i]],
2430
325
                                 &font->output);
2431
325
        if (unlikely (status))
2432
0
            return status;
2433
2434
325
  offset_array_ptr = _cairo_array_index (&font->output, offset_array);
2435
325
  put_unaligned_be32 (_cairo_array_num_elements (&font->output) - offset_base,
2436
325
          offset_array_ptr);
2437
325
  offset_array += sizeof(uint32_t);
2438
325
    }
2439
2440
311
    return CAIRO_STATUS_SUCCESS;
2441
311
}
2442
2443
static cairo_status_t
2444
cairo_cff_font_write_private_dict (cairo_cff_font_t   *font,
2445
           int                 dict_num,
2446
           cairo_hash_table_t *parent_dict,
2447
           cairo_hash_table_t *private_dict)
2448
984
{
2449
984
    int offset;
2450
984
    int size;
2451
984
    unsigned char buf[10];
2452
984
    unsigned char *buf_end;
2453
984
    unsigned char *p;
2454
984
    cairo_status_t status;
2455
2456
    /* Write private dict and update offset and size in top dict */
2457
984
    font->private_dict_offset[dict_num] = _cairo_array_num_elements (&font->output);
2458
984
    status = cff_dict_write (private_dict, &font->output);
2459
984
    if (unlikely (status))
2460
0
        return status;
2461
2462
984
    size = _cairo_array_num_elements (&font->output) - font->private_dict_offset[dict_num];
2463
    /* private entry has two operands - size and offset */
2464
984
    buf_end = encode_integer_max (buf, size);
2465
984
    buf_end = encode_integer_max (buf_end, font->private_dict_offset[dict_num]);
2466
984
    offset = cff_dict_get_location (parent_dict, PRIVATE_OP, &size);
2467
984
    assert (offset > 0);
2468
984
    p = _cairo_array_index (&font->output, offset);
2469
984
    memcpy (p, buf, buf_end - buf);
2470
2471
984
    return CAIRO_STATUS_SUCCESS;
2472
984
}
2473
2474
static cairo_status_t
2475
cairo_cff_font_write_local_sub (cairo_cff_font_t   *font,
2476
                                int                 dict_num,
2477
                                cairo_hash_table_t *private_dict,
2478
                                cairo_array_t      *local_sub_index,
2479
                                cairo_bool_t       *local_subs_used)
2480
984
{
2481
984
    int offset;
2482
984
    int size;
2483
984
    unsigned char buf[10];
2484
984
    unsigned char *buf_end;
2485
984
    unsigned char *p;
2486
984
    cairo_status_t status;
2487
984
    unsigned int i;
2488
984
    unsigned char return_op = TYPE2_return;
2489
2490
984
    if (_cairo_array_num_elements (local_sub_index) > 0) {
2491
        /* Write local subroutines and update offset in private
2492
         * dict. Local subroutines offset is relative to start of
2493
         * private dict */
2494
37
        offset = _cairo_array_num_elements (&font->output) - font->private_dict_offset[dict_num];
2495
37
        buf_end = encode_integer_max (buf, offset);
2496
37
        offset = cff_dict_get_location (private_dict, LOCAL_SUB_OP, &size);
2497
37
        assert (offset > 0);
2498
37
        p = _cairo_array_index (&font->output, offset);
2499
37
        memcpy (p, buf, buf_end - buf);
2500
2501
  /* poppler and fontforge don't like zero length subroutines so
2502
   * we replace unused subroutines with a 'return' instruction.
2503
   */
2504
37
        if (font->subset_subroutines) {
2505
4.11k
            for (i = 0; i < _cairo_array_num_elements (local_sub_index); i++) {
2506
4.08k
                if (! local_subs_used[i])
2507
3.61k
                    cff_index_set_object (local_sub_index, i, &return_op, 1);
2508
4.08k
            }
2509
33
        }
2510
37
        status = cff_index_write (local_sub_index, &font->output);
2511
37
        if (unlikely (status))
2512
0
            return status;
2513
37
    }
2514
2515
984
    return CAIRO_STATUS_SUCCESS;
2516
984
}
2517
2518
2519
static cairo_status_t
2520
cairo_cff_font_write_cid_private_dict_and_local_sub (cairo_cff_font_t  *font)
2521
311
{
2522
311
    unsigned int i;
2523
311
    cairo_int_status_t status;
2524
2525
311
    if (font->is_cid) {
2526
260
        for (i = 0; i < font->num_subset_fontdicts; i++) {
2527
137
            status = cairo_cff_font_write_private_dict (
2528
137
                            font,
2529
137
                            i,
2530
137
                            font->fd_dict[font->fd_subset_map[i]],
2531
137
                            font->fd_private_dict[font->fd_subset_map[i]]);
2532
137
            if (unlikely (status))
2533
0
                return status;
2534
137
        }
2535
2536
260
        for (i = 0; i < font->num_subset_fontdicts; i++) {
2537
137
            status = cairo_cff_font_write_local_sub (
2538
137
                            font,
2539
137
                            i,
2540
137
                            font->fd_private_dict[font->fd_subset_map[i]],
2541
137
                            &font->fd_local_sub_index[font->fd_subset_map[i]],
2542
137
                            font->fd_local_subs_used[font->fd_subset_map[i]]);
2543
137
            if (unlikely (status))
2544
0
                return status;
2545
137
        }
2546
188
    } else {
2547
188
        status = cairo_cff_font_write_private_dict (font,
2548
188
                                                    0,
2549
188
                                                    font->fd_dict[0],
2550
188
                                                    font->private_dict);
2551
188
  if (unlikely (status))
2552
0
      return status;
2553
2554
188
        status = cairo_cff_font_write_local_sub (font,
2555
188
                                                 0,
2556
188
                                                 font->private_dict,
2557
188
                                                 &font->local_sub_index,
2558
188
                                                 font->local_subs_used);
2559
188
  if (unlikely (status))
2560
0
      return status;
2561
188
    }
2562
2563
311
    return CAIRO_STATUS_SUCCESS;
2564
311
}
2565
2566
static cairo_status_t
2567
cairo_cff_font_write_type1_private_dict_and_local_sub (cairo_cff_font_t  *font)
2568
659
{
2569
659
    cairo_int_status_t status;
2570
2571
659
    status = cairo_cff_font_write_private_dict (font,
2572
659
            0,
2573
659
            font->top_dict,
2574
659
            font->private_dict);
2575
659
    if (unlikely (status))
2576
0
  return status;
2577
2578
659
    status = cairo_cff_font_write_local_sub (font,
2579
659
               0,
2580
659
               font->private_dict,
2581
659
               &font->local_sub_index,
2582
659
               font->local_subs_used);
2583
659
    if (unlikely (status))
2584
0
  return status;
2585
2586
659
    return CAIRO_STATUS_SUCCESS;
2587
659
}
2588
2589
2590
typedef cairo_status_t
2591
(*font_write_t) (cairo_cff_font_t *font);
2592
2593
static const font_write_t font_write_cid_funcs[] = {
2594
    cairo_cff_font_write_header,
2595
    cairo_cff_font_write_name,
2596
    cairo_cff_font_write_top_dict,
2597
    cairo_cff_font_write_strings,
2598
    cairo_cff_font_write_global_subrs,
2599
    cairo_cff_font_write_cid_charset,
2600
    cairo_cff_font_write_fdselect,
2601
    cairo_cff_font_write_charstrings,
2602
    cairo_cff_font_write_cid_fontdict,
2603
    cairo_cff_font_write_cid_private_dict_and_local_sub,
2604
};
2605
2606
static const font_write_t font_write_type1_funcs[] = {
2607
    cairo_cff_font_write_header,
2608
    cairo_cff_font_write_name,
2609
    cairo_cff_font_write_top_dict,
2610
    cairo_cff_font_write_strings,
2611
    cairo_cff_font_write_global_subrs,
2612
    cairo_cff_font_write_encoding,
2613
    cairo_cff_font_write_type1_charset,
2614
    cairo_cff_font_write_charstrings,
2615
    cairo_cff_font_write_type1_private_dict_and_local_sub,
2616
};
2617
2618
static cairo_status_t
2619
cairo_cff_font_write_subset (cairo_cff_font_t *font)
2620
970
{
2621
970
    cairo_int_status_t status;
2622
970
    unsigned int i;
2623
2624
970
    if (font->scaled_font_subset->is_latin) {
2625
6.59k
        for (i = 0; i < ARRAY_LENGTH (font_write_type1_funcs); i++) {
2626
5.93k
            status = font_write_type1_funcs[i] (font);
2627
5.93k
            if (unlikely (status))
2628
0
                return status;
2629
5.93k
        }
2630
659
    } else {
2631
3.42k
        for (i = 0; i < ARRAY_LENGTH (font_write_cid_funcs); i++) {
2632
3.11k
            status = font_write_cid_funcs[i] (font);
2633
3.11k
            if (unlikely (status))
2634
0
                return status;
2635
3.11k
        }
2636
311
    }
2637
2638
970
    return CAIRO_STATUS_SUCCESS;
2639
970
}
2640
2641
static cairo_int_status_t
2642
cairo_cff_font_generate (cairo_cff_font_t  *font,
2643
                         const char       **data,
2644
                         unsigned long     *length)
2645
762
{
2646
762
    cairo_int_status_t status;
2647
2648
762
    status = cairo_cff_font_read_font (font);
2649
762
    if (unlikely (status))
2650
146
        return status;
2651
2652
    /* If the PS name is not found, create a CairoFont-x-y name. */
2653
616
    if (font->ps_name == NULL) {
2654
1
        font->ps_name = _cairo_malloc (30);
2655
1
        if (unlikely (font->ps_name == NULL))
2656
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2657
2658
1
        snprintf(font->ps_name, 30, "CairoFont-%u-%u",
2659
1
                 font->scaled_font_subset->font_id,
2660
1
                 font->scaled_font_subset->subset_id);
2661
1
    }
2662
2663
616
    status = cairo_cff_font_subset_font (font);
2664
616
    if (unlikely (status))
2665
34
        return status;
2666
2667
582
    status = cairo_cff_font_write_subset (font);
2668
582
    if (unlikely (status))
2669
0
        return status;
2670
2671
2672
582
    *data = _cairo_array_index (&font->output, 0);
2673
582
    *length = _cairo_array_num_elements (&font->output);
2674
2675
582
    return CAIRO_STATUS_SUCCESS;
2676
582
}
2677
2678
static cairo_int_status_t
2679
cairo_cff_font_create_set_widths (cairo_cff_font_t *font)
2680
66
{
2681
66
    unsigned long size;
2682
66
    unsigned long long_entry_size;
2683
66
    unsigned long short_entry_size;
2684
66
    unsigned int i;
2685
66
    tt_hhea_t hhea;
2686
66
    int num_hmetrics;
2687
66
    uint16_t short_entry;
2688
66
    int glyph_index;
2689
66
    cairo_int_status_t status;
2690
2691
66
    size = sizeof (tt_hhea_t);
2692
66
    status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2693
66
                                                 TT_TAG_hhea, 0,
2694
66
                                                 (unsigned char*) &hhea, &size);
2695
66
    if (unlikely (status))
2696
0
        return status;
2697
66
    num_hmetrics = be16_to_cpu (hhea.num_hmetrics);
2698
2699
624
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
2700
559
        glyph_index = font->scaled_font_subset->glyphs[i];
2701
559
        long_entry_size = 2 * sizeof (int16_t);
2702
559
        short_entry_size = sizeof (int16_t);
2703
559
        if (glyph_index < num_hmetrics) {
2704
543
            status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2705
543
                                                         TT_TAG_hmtx,
2706
543
                                                         glyph_index * long_entry_size,
2707
543
                                                         (unsigned char *) &short_entry,
2708
543
               &short_entry_size);
2709
543
            if (unlikely (status))
2710
1
                return status;
2711
543
        }
2712
16
        else
2713
16
        {
2714
16
            status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2715
16
                                                         TT_TAG_hmtx,
2716
16
                                                         (num_hmetrics - 1) * long_entry_size,
2717
16
                                                         (unsigned char *) &short_entry,
2718
16
               &short_entry_size);
2719
16
            if (unlikely (status))
2720
0
                return status;
2721
16
        }
2722
558
  font->widths[i] = be16_to_cpu (short_entry);
2723
558
    }
2724
2725
65
    return CAIRO_STATUS_SUCCESS;
2726
66
}
2727
2728
static cairo_bool_t
2729
check_fontdata_is_cff (const unsigned char *data, long length)
2730
2.89k
{
2731
2.89k
    cff_header_t *header;
2732
2733
2.89k
    if (length < (long)sizeof (cff_header_t))
2734
0
        return FALSE;
2735
2736
2.89k
    header = (cff_header_t *) data;
2737
2.89k
    if (header->major == 1 &&
2738
1.47k
  header->minor == 0 &&
2739
1.47k
  header->header_size == 4)
2740
1.47k
    {
2741
1.47k
  return TRUE;
2742
1.47k
    }
2743
2744
1.42k
    return FALSE;
2745
2.89k
}
2746
2747
static cairo_int_status_t
2748
_cairo_cff_font_load_opentype_cff (cairo_cff_font_t  *font)
2749
4.77k
{
2750
4.77k
    const cairo_scaled_font_backend_t *backend = font->backend;
2751
4.77k
    cairo_status_t status;
2752
4.77k
    tt_head_t head;
2753
4.77k
    tt_hhea_t hhea;
2754
4.77k
    unsigned long size, data_length;
2755
2756
4.77k
    if (!backend->load_truetype_table)
2757
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
2758
2759
4.77k
    data_length = 0;
2760
4.77k
    status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2761
4.77k
                                           TT_TAG_CFF, 0, NULL, &data_length);
2762
4.77k
    if (status)
2763
4.70k
        return status;
2764
2765
67
    size = sizeof (tt_head_t);
2766
67
    status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2767
67
                                           TT_TAG_head, 0,
2768
67
                                           (unsigned char *) &head, &size);
2769
67
    if (unlikely (status))
2770
1
        return status;
2771
2772
66
    size = sizeof (tt_hhea_t);
2773
66
    status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2774
66
                                           TT_TAG_hhea, 0,
2775
66
                                           (unsigned char *) &hhea, &size);
2776
66
    if (unlikely (status))
2777
0
        return status;
2778
2779
66
    size = 0;
2780
66
    status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2781
66
                                           TT_TAG_hmtx, 0, NULL, &size);
2782
66
    if (unlikely (status))
2783
0
        return status;
2784
2785
66
    font->x_min = (int16_t) be16_to_cpu (head.x_min);
2786
66
    font->y_min = (int16_t) be16_to_cpu (head.y_min);
2787
66
    font->x_max = (int16_t) be16_to_cpu (head.x_max);
2788
66
    font->y_max = (int16_t) be16_to_cpu (head.y_max);
2789
66
    font->ascent = (int16_t) be16_to_cpu (hhea.ascender);
2790
66
    font->descent = (int16_t) be16_to_cpu (hhea.descender);
2791
66
    font->units_per_em = (int16_t) be16_to_cpu (head.units_per_em);
2792
66
    if (font->units_per_em == 0)
2793
0
        font->units_per_em = 1000;
2794
2795
66
    font->font_name = NULL;
2796
66
    status = _cairo_truetype_read_font_name (font->scaled_font_subset->scaled_font,
2797
66
               &font->ps_name,
2798
66
               &font->font_name);
2799
66
    if (_cairo_status_is_error (status))
2800
0
  return status;
2801
2802
66
    font->is_opentype = TRUE;
2803
66
    font->data_length = data_length;
2804
66
    font->data = _cairo_malloc (data_length);
2805
66
    if (unlikely (font->data == NULL))
2806
0
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2807
2808
66
    status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2809
66
             TT_TAG_CFF, 0, font->data,
2810
66
             &font->data_length);
2811
66
    if (unlikely (status))
2812
0
        return status;
2813
2814
66
    if (!check_fontdata_is_cff (font->data, data_length))
2815
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
2816
2817
66
    return CAIRO_STATUS_SUCCESS;
2818
66
}
2819
2820
static cairo_int_status_t
2821
_cairo_cff_font_load_cff (cairo_cff_font_t  *font)
2822
4.71k
{
2823
4.71k
    const cairo_scaled_font_backend_t *backend = font->backend;
2824
4.71k
    cairo_status_t status;
2825
4.71k
    unsigned long data_length;
2826
2827
4.71k
    if (!backend->load_type1_data)
2828
0
  return CAIRO_INT_STATUS_UNSUPPORTED;
2829
2830
4.71k
    data_length = 0;
2831
4.71k
    status = backend->load_type1_data (font->scaled_font_subset->scaled_font,
2832
4.71k
              0, NULL, &data_length);
2833
4.71k
    if (unlikely (status))
2834
3.25k
        return status;
2835
2836
1.45k
    font->font_name = NULL;
2837
1.45k
    font->is_opentype = FALSE;
2838
1.45k
    font->data_length = data_length;
2839
1.45k
    font->data = _cairo_malloc (data_length);
2840
1.45k
    if (unlikely (font->data == NULL))
2841
0
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2842
2843
1.45k
    status = font->backend->load_type1_data (font->scaled_font_subset->scaled_font,
2844
1.45k
               0, font->data, &font->data_length);
2845
1.45k
    if (unlikely (status))
2846
0
        return status;
2847
2848
1.45k
    if (!check_fontdata_is_cff (font->data, data_length))
2849
762
  return CAIRO_INT_STATUS_UNSUPPORTED;
2850
2851
697
    return CAIRO_STATUS_SUCCESS;
2852
1.45k
}
2853
2854
static cairo_int_status_t
2855
_cairo_cff_font_create (cairo_scaled_font_subset_t  *scaled_font_subset,
2856
                        cairo_cff_font_t           **font_return,
2857
                        const char                  *subset_name)
2858
4.77k
{
2859
4.77k
    const cairo_scaled_font_backend_t *backend;
2860
4.77k
    cairo_int_status_t status;
2861
4.77k
    cairo_bool_t is_synthetic;
2862
4.77k
    cairo_cff_font_t *font;
2863
2864
4.77k
    backend = scaled_font_subset->scaled_font->backend;
2865
2866
    /* We need to use a fallback font if this font differs from the CFF outlines. */
2867
4.77k
    if (backend->is_synthetic) {
2868
4.77k
  status = backend->is_synthetic (scaled_font_subset->scaled_font, &is_synthetic);
2869
4.77k
  if (unlikely (status))
2870
0
      return status;
2871
2872
4.77k
  if (is_synthetic)
2873
0
      return CAIRO_INT_STATUS_UNSUPPORTED;
2874
4.77k
    }
2875
2876
4.77k
    font = _cairo_calloc (sizeof (cairo_cff_font_t));
2877
4.77k
    if (unlikely (font == NULL))
2878
0
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2879
2880
4.77k
    font->backend = backend;
2881
4.77k
    font->scaled_font_subset = scaled_font_subset;
2882
2883
4.77k
    status = _cairo_cff_font_load_opentype_cff (font);
2884
4.77k
    if (status == CAIRO_INT_STATUS_UNSUPPORTED)
2885
4.71k
  status = _cairo_cff_font_load_cff (font);
2886
4.77k
    if (status)
2887
4.01k
  goto fail1;
2888
2889
763
    font->data_end = font->data + font->data_length;
2890
763
    _cairo_array_init (&font->output, sizeof (char));
2891
763
    status = _cairo_array_grow_by (&font->output, 4096);
2892
763
    if (unlikely (status))
2893
0
  goto fail2;
2894
2895
763
    font->subset_font_name = strdup (subset_name);
2896
763
    if (unlikely (font->subset_font_name == NULL)) {
2897
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2898
0
  goto fail2;
2899
0
    }
2900
2901
763
    font->widths = _cairo_calloc_ab (font->scaled_font_subset->num_glyphs, sizeof (int));
2902
763
    if (unlikely (font->widths == NULL)) {
2903
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2904
0
        goto fail3;
2905
0
    }
2906
2907
763
    if (font->is_opentype) {
2908
66
  status = cairo_cff_font_create_set_widths (font);
2909
66
  if (unlikely (status))
2910
1
      goto fail4;
2911
66
    }
2912
2913
762
    status = cff_dict_init (&font->top_dict);
2914
762
    if (unlikely (status))
2915
0
  goto fail4;
2916
2917
762
    status = cff_dict_init (&font->private_dict);
2918
762
    if (unlikely (status))
2919
0
  goto fail5;
2920
2921
762
    cff_index_init (&font->strings_index);
2922
762
    cff_index_init (&font->charstrings_index);
2923
762
    cff_index_init (&font->global_sub_index);
2924
762
    cff_index_init (&font->local_sub_index);
2925
762
    cff_index_init (&font->charstrings_subset_index);
2926
762
    cff_index_init (&font->strings_subset_index);
2927
762
    font->euro_sid = 0;
2928
762
    font->fdselect = NULL;
2929
762
    font->fd_dict = NULL;
2930
762
    font->fd_private_dict = NULL;
2931
762
    font->fd_local_sub_index = NULL;
2932
762
    font->fd_local_sub_bias = NULL;
2933
762
    font->fdselect_subset = NULL;
2934
762
    font->fd_subset_map = NULL;
2935
762
    font->private_dict_offset = NULL;
2936
762
    font->global_subs_used = NULL;
2937
762
    font->local_subs_used = NULL;
2938
762
    font->fd_local_subs_used = NULL;
2939
2940
762
    *font_return = font;
2941
2942
762
    return CAIRO_STATUS_SUCCESS;
2943
2944
0
fail5:
2945
0
    _cairo_hash_table_destroy (font->top_dict);
2946
1
fail4:
2947
1
    free (font->widths);
2948
1
fail3:
2949
1
    free (font->subset_font_name);
2950
1
fail2:
2951
1
    free (font->ps_name);
2952
1
    _cairo_array_fini (&font->output);
2953
4.01k
fail1:
2954
4.01k
    free (font->data);
2955
4.01k
    free (font->font_name);
2956
4.01k
    free (font);
2957
2958
4.01k
    return status;
2959
1
}
2960
2961
static void
2962
cairo_cff_font_destroy (cairo_cff_font_t *font)
2963
1.15k
{
2964
1.15k
    unsigned int i;
2965
2966
1.15k
    free (font->widths);
2967
1.15k
    free (font->font_name);
2968
1.15k
    free (font->ps_name);
2969
1.15k
    free (font->subset_font_name);
2970
1.15k
    _cairo_array_fini (&font->output);
2971
1.15k
    cff_dict_fini (font->top_dict);
2972
1.15k
    cff_dict_fini (font->private_dict);
2973
1.15k
    cff_index_fini (&font->strings_index);
2974
1.15k
    cff_index_fini (&font->charstrings_index);
2975
1.15k
    cff_index_fini (&font->global_sub_index);
2976
1.15k
    cff_index_fini (&font->local_sub_index);
2977
1.15k
    cff_index_fini (&font->charstrings_subset_index);
2978
1.15k
    cff_index_fini (&font->strings_subset_index);
2979
2980
    /* If we bailed out early as a result of an error some of the
2981
     * following cairo_cff_font_t members may still be NULL */
2982
1.15k
    if (font->fd_dict) {
2983
636
        for (i = 0; i < font->num_fontdicts; i++) {
2984
325
            if (font->fd_dict[i])
2985
325
                cff_dict_fini (font->fd_dict[i]);
2986
325
        }
2987
311
        free (font->fd_dict);
2988
311
    }
2989
1.15k
    free (font->global_subs_used);
2990
1.15k
    free (font->local_subs_used);
2991
1.15k
    free (font->fd_subset_map);
2992
1.15k
    free (font->private_dict_offset);
2993
2994
1.15k
    if (font->is_cid) {
2995
123
  free (font->fdselect);
2996
123
  free (font->fdselect_subset);
2997
123
        if (font->fd_private_dict) {
2998
260
            for (i = 0; i < font->num_fontdicts; i++) {
2999
137
                if (font->fd_private_dict[i])
3000
137
                    cff_dict_fini (font->fd_private_dict[i]);
3001
137
            }
3002
123
            free (font->fd_private_dict);
3003
123
        }
3004
123
        if (font->fd_local_sub_index) {
3005
260
            for (i = 0; i < font->num_fontdicts; i++)
3006
137
                cff_index_fini (&font->fd_local_sub_index[i]);
3007
123
            free (font->fd_local_sub_index);
3008
123
        }
3009
123
  free (font->fd_local_sub_bias);
3010
123
        if (font->fd_local_subs_used) {
3011
260
            for (i = 0; i < font->num_fontdicts; i++) {
3012
137
    free (font->fd_local_subs_used[i]);
3013
137
            }
3014
123
            free (font->fd_local_subs_used);
3015
123
        }
3016
123
  free (font->fd_default_width);
3017
123
  free (font->fd_nominal_width);
3018
123
    }
3019
3020
1.15k
    free (font->data);
3021
3022
1.15k
    free (font);
3023
1.15k
}
3024
3025
cairo_status_t
3026
_cairo_cff_subset_init (cairo_cff_subset_t          *cff_subset,
3027
                        const char        *subset_name,
3028
                        cairo_scaled_font_subset_t  *font_subset)
3029
4.77k
{
3030
4.77k
    cairo_cff_font_t *font = NULL; /* squelch bogus compiler warning */
3031
4.77k
    cairo_status_t status;
3032
4.77k
    const char *data = NULL; /* squelch bogus compiler warning */
3033
4.77k
    unsigned long length = 0; /* squelch bogus compiler warning */
3034
4.77k
    unsigned int i;
3035
3036
4.77k
    status = _cairo_cff_font_create (font_subset, &font, subset_name);
3037
4.77k
    if (unlikely (status))
3038
4.01k
  return status;
3039
3040
762
    status = cairo_cff_font_generate (font, &data, &length);
3041
762
    if (unlikely (status))
3042
180
  goto fail1;
3043
3044
582
    cff_subset->ps_name = strdup (font->ps_name);
3045
582
    if (unlikely (cff_subset->ps_name == NULL)) {
3046
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3047
0
  goto fail1;
3048
0
    }
3049
3050
582
    if (font->font_name) {
3051
25
  cff_subset->family_name_utf8 = strdup (font->font_name);
3052
25
  if (cff_subset->family_name_utf8 == NULL) {
3053
0
      status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3054
0
      goto fail2;
3055
0
  }
3056
557
    } else {
3057
557
  cff_subset->family_name_utf8 = NULL;
3058
557
    }
3059
3060
582
    cff_subset->widths = _cairo_calloc_ab (font->scaled_font_subset->num_glyphs,
3061
582
             sizeof (double));
3062
582
    if (unlikely (cff_subset->widths == NULL)) {
3063
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3064
0
  goto fail3;
3065
0
    }
3066
9.95k
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++)
3067
9.37k
        cff_subset->widths[i] = (double)font->widths[i]/font->units_per_em;
3068
3069
582
    cff_subset->x_min = (double)font->x_min/font->units_per_em;
3070
582
    cff_subset->y_min = (double)font->y_min/font->units_per_em;
3071
582
    cff_subset->x_max = (double)font->x_max/font->units_per_em;
3072
582
    cff_subset->y_max = (double)font->y_max/font->units_per_em;
3073
582
    cff_subset->ascent = (double)font->ascent/font->units_per_em;
3074
582
    cff_subset->descent = (double)font->descent/font->units_per_em;
3075
3076
582
    cff_subset->data = _cairo_malloc (length);
3077
582
    if (unlikely (cff_subset->data == NULL)) {
3078
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3079
0
  goto fail4;
3080
0
    }
3081
3082
582
    memcpy (cff_subset->data, data, length);
3083
582
    cff_subset->data_length = length;
3084
3085
582
    cairo_cff_font_destroy (font);
3086
3087
582
    return CAIRO_STATUS_SUCCESS;
3088
3089
0
 fail4:
3090
0
    free (cff_subset->widths);
3091
0
 fail3:
3092
0
    free (cff_subset->family_name_utf8);
3093
0
 fail2:
3094
0
    free (cff_subset->ps_name);
3095
180
 fail1:
3096
180
    cairo_cff_font_destroy (font);
3097
3098
180
    return status;
3099
0
}
3100
3101
void
3102
_cairo_cff_subset_fini (cairo_cff_subset_t *subset)
3103
582
{
3104
582
    free (subset->ps_name);
3105
582
    free (subset->family_name_utf8);
3106
582
    free (subset->widths);
3107
582
    free (subset->data);
3108
582
}
3109
3110
cairo_bool_t
3111
_cairo_cff_scaled_font_is_cid_cff (cairo_scaled_font_t *scaled_font)
3112
4.27k
{
3113
4.27k
    const cairo_scaled_font_backend_t *backend;
3114
4.27k
    cairo_int_status_t status;
3115
4.27k
    unsigned char *data;
3116
4.27k
    unsigned long data_length;
3117
4.27k
    unsigned char *current_ptr;
3118
4.27k
    unsigned char *data_end;
3119
4.27k
    cff_header_t  *header;
3120
4.27k
    cff_index_element_t *element;
3121
4.27k
    cairo_hash_table_t  *top_dict;
3122
4.27k
    cairo_array_t index;
3123
4.27k
    int size;
3124
4.27k
    cairo_bool_t is_cid = FALSE;
3125
3126
4.27k
    backend = scaled_font->backend;
3127
4.27k
    data = NULL;
3128
4.27k
    data_length = 0;
3129
4.27k
    status = CAIRO_INT_STATUS_UNSUPPORTED;
3130
    /* Try to load an OpenType/CFF font */
3131
4.27k
    if (backend->load_truetype_table &&
3132
4.27k
  (status = backend->load_truetype_table (scaled_font, TT_TAG_CFF,
3133
4.27k
            0, NULL, &data_length)) == CAIRO_INT_STATUS_SUCCESS)
3134
66
    {
3135
66
  data = _cairo_malloc (data_length);
3136
66
  if (unlikely (data == NULL)) {
3137
0
      status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3138
0
      return FALSE;
3139
0
  }
3140
3141
66
  status = backend->load_truetype_table (scaled_font, TT_TAG_CFF,
3142
66
             0, data, &data_length);
3143
66
  if (unlikely (status))
3144
0
      goto fail1;
3145
66
    }
3146
    /* Try to load a CFF font */
3147
4.27k
    if (status == CAIRO_INT_STATUS_UNSUPPORTED &&
3148
4.20k
  backend->load_type1_data &&
3149
4.20k
  (status = backend->load_type1_data (scaled_font,
3150
4.20k
              0, NULL, &data_length)) == CAIRO_INT_STATUS_SUCCESS)
3151
1.30k
    {
3152
1.30k
  data = _cairo_malloc (data_length);
3153
1.30k
  if (unlikely (data == NULL)) {
3154
0
      status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3155
0
      return FALSE;
3156
0
  }
3157
3158
1.30k
  status = backend->load_type1_data (scaled_font, 0, data, &data_length);
3159
1.30k
  if (unlikely (status))
3160
0
      goto fail1;
3161
1.30k
    }
3162
4.27k
    if (status)
3163
2.90k
  goto fail1;
3164
3165
    /* Check if it looks like a CFF font */
3166
1.37k
    if (!check_fontdata_is_cff (data, data_length))
3167
659
  goto fail1;
3168
3169
714
    data_end = data + data_length;
3170
3171
    /* skip header */
3172
714
    if (data_length < sizeof (cff_header_t))
3173
0
        goto fail1;
3174
3175
714
    header = (cff_header_t *) data;
3176
714
    current_ptr = data + header->header_size;
3177
3178
    /* skip name */
3179
714
    cff_index_init (&index);
3180
714
    status = cff_index_read (&index, &current_ptr, data_end);
3181
714
    cff_index_fini (&index);
3182
3183
714
    if (status)
3184
0
        goto fail1;
3185
3186
    /* read top dict */
3187
714
    cff_index_init (&index);
3188
714
    status = cff_index_read (&index, &current_ptr, data_end);
3189
714
    if (unlikely (status))
3190
0
        goto fail2;
3191
3192
714
    status = cff_dict_init (&top_dict);
3193
714
    if (unlikely (status))
3194
0
  goto fail2;
3195
3196
714
    element = _cairo_array_index (&index, 0);
3197
714
    if (element == NULL)
3198
0
        goto fail3;
3199
714
    status = cff_dict_read (top_dict, element->data, element->length);
3200
714
    if (unlikely (status))
3201
0
        goto fail3;
3202
3203
    /* check for ROS operator indicating a CID font */
3204
714
    if (cff_dict_get_operands (top_dict, ROS_OP, &size) != NULL)
3205
129
        is_cid = TRUE;
3206
3207
714
fail3:
3208
714
    cff_dict_fini (top_dict);
3209
3210
714
fail2:
3211
714
    cff_index_fini (&index);
3212
3213
4.27k
fail1:
3214
4.27k
    free (data);
3215
3216
4.27k
    return is_cid;
3217
714
}
3218
3219
static cairo_int_status_t
3220
_cairo_cff_font_fallback_create (cairo_scaled_font_subset_t  *scaled_font_subset,
3221
                                 cairo_cff_font_t           **font_return,
3222
                                 const char                  *subset_name)
3223
388
{
3224
388
    cairo_status_t status;
3225
388
    cairo_cff_font_t *font;
3226
3227
388
    font = _cairo_calloc (sizeof (cairo_cff_font_t));
3228
388
    if (unlikely (font == NULL))
3229
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3230
3231
388
    font->backend = NULL;
3232
388
    font->scaled_font_subset = scaled_font_subset;
3233
3234
388
    _cairo_array_init (&font->output, sizeof (char));
3235
388
    status = _cairo_array_grow_by (&font->output, 4096);
3236
388
    if (unlikely (status))
3237
0
  goto fail1;
3238
3239
388
    font->subset_font_name = strdup (subset_name);
3240
388
    if (unlikely (font->subset_font_name == NULL)) {
3241
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3242
0
  goto fail1;
3243
0
    }
3244
3245
388
    font->ps_name = strdup (subset_name);
3246
388
    if (unlikely (font->ps_name == NULL)) {
3247
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3248
0
  goto fail2;
3249
0
    }
3250
388
    font->font_name = NULL;
3251
3252
388
    font->x_min = 0;
3253
388
    font->y_min = 0;
3254
388
    font->x_max = 0;
3255
388
    font->y_max = 0;
3256
388
    font->ascent = 0;
3257
388
    font->descent = 0;
3258
3259
388
    font->widths = _cairo_calloc_ab (font->scaled_font_subset->num_glyphs, sizeof (int));
3260
388
    if (unlikely (font->widths == NULL)) {
3261
0
        status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3262
0
        goto fail3;
3263
0
    }
3264
3265
388
    font->data_length = 0;
3266
388
    font->data = NULL;
3267
388
    font->data_end = NULL;
3268
3269
388
    status = cff_dict_init (&font->top_dict);
3270
388
    if (unlikely (status))
3271
0
  goto fail4;
3272
3273
388
    status = cff_dict_init (&font->private_dict);
3274
388
    if (unlikely (status))
3275
0
  goto fail5;
3276
3277
388
    cff_index_init (&font->strings_index);
3278
388
    cff_index_init (&font->charstrings_index);
3279
388
    cff_index_init (&font->global_sub_index);
3280
388
    cff_index_init (&font->local_sub_index);
3281
388
    cff_index_init (&font->charstrings_subset_index);
3282
388
    cff_index_init (&font->strings_subset_index);
3283
388
    font->global_subs_used = NULL;
3284
388
    font->local_subs_used = NULL;
3285
388
    font->subset_subroutines = FALSE;
3286
388
    font->fdselect = NULL;
3287
388
    font->fd_dict = NULL;
3288
388
    font->fd_private_dict = NULL;
3289
388
    font->fd_local_sub_index = NULL;
3290
388
    font->fdselect_subset = NULL;
3291
388
    font->fd_subset_map = NULL;
3292
388
    font->private_dict_offset = NULL;
3293
3294
388
    *font_return = font;
3295
3296
388
    return CAIRO_STATUS_SUCCESS;
3297
3298
0
fail5:
3299
0
    _cairo_hash_table_destroy (font->top_dict);
3300
0
fail4:
3301
0
    free (font->widths);
3302
0
fail3:
3303
0
    free (font->font_name);
3304
0
    free (font->ps_name);
3305
0
fail2:
3306
0
    free (font->subset_font_name);
3307
0
fail1:
3308
0
    _cairo_array_fini (&font->output);
3309
0
    free (font);
3310
0
    return status;
3311
0
}
3312
3313
static cairo_int_status_t
3314
cairo_cff_font_fallback_generate (cairo_cff_font_t           *font,
3315
                                  cairo_type2_charstrings_t  *type2_subset,
3316
                                  const char                **data,
3317
                                  unsigned long              *length)
3318
388
{
3319
388
    cairo_int_status_t status;
3320
388
    cff_header_t header;
3321
388
    cairo_array_t *charstring;
3322
388
    unsigned char buf[40];
3323
388
    unsigned char *end_buf, *end_buf2;
3324
388
    unsigned int i;
3325
388
    int sid;
3326
3327
    /* Create header */
3328
388
    header.major = 1;
3329
388
    header.minor = 0;
3330
388
    header.header_size = 4;
3331
388
    header.offset_size = 4;
3332
388
    font->header = &header;
3333
3334
    /* Create Top Dict */
3335
388
    font->is_cid = FALSE;
3336
3337
388
    snprintf((char*)buf, sizeof(buf), "CairoFont-%u-%u",
3338
388
       font->scaled_font_subset->font_id,
3339
388
       font->scaled_font_subset->subset_id);
3340
388
    sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
3341
388
    status = cff_index_append_copy (&font->strings_subset_index,
3342
388
                                    (unsigned char *)buf,
3343
388
                                    strlen((char*)buf));
3344
388
    if (unlikely (status))
3345
0
  return status;
3346
3347
388
    end_buf = encode_integer (buf, sid);
3348
388
    status = cff_dict_set_operands (font->top_dict, FULLNAME_OP,
3349
388
            buf, end_buf - buf);
3350
388
    if (unlikely (status))
3351
0
  return status;
3352
3353
388
    status = cff_dict_set_operands (font->top_dict, FAMILYNAME_OP,
3354
388
            buf, end_buf - buf);
3355
388
    if (unlikely (status))
3356
0
  return status;
3357
3358
388
    end_buf = encode_integer (buf, type2_subset->x_min);
3359
388
    end_buf = encode_integer (end_buf, type2_subset->y_min);
3360
388
    end_buf = encode_integer (end_buf, type2_subset->x_max);
3361
388
    end_buf = encode_integer (end_buf, type2_subset->y_max);
3362
388
    status = cff_dict_set_operands (font->top_dict,
3363
388
                              FONTBBOX_OP, buf, end_buf - buf);
3364
388
    if (unlikely (status))
3365
0
  return status;
3366
3367
388
    end_buf = encode_integer_max (buf, 0);
3368
388
    status = cff_dict_set_operands (font->top_dict,
3369
388
                              CHARSTRINGS_OP, buf, end_buf - buf);
3370
388
    if (unlikely (status))
3371
0
  return status;
3372
3373
3374
388
    if (font->scaled_font_subset->is_latin) {
3375
298
  status = cff_dict_set_operands (font->top_dict,
3376
298
                                        ENCODING_OP, buf, end_buf - buf);
3377
298
        if (unlikely (status))
3378
0
      return status;
3379
3380
  /* Private has two operands - size and offset */
3381
298
  end_buf2 = encode_integer_max (end_buf, 0);
3382
298
  cff_dict_set_operands (font->top_dict, PRIVATE_OP, buf, end_buf2 - buf);
3383
298
        if (unlikely (status))
3384
0
      return status;
3385
3386
298
    } else {
3387
90
  status = cff_dict_set_operands (font->top_dict,
3388
90
          FDSELECT_OP, buf, end_buf - buf);
3389
90
  if (unlikely (status))
3390
0
      return status;
3391
3392
90
  status = cff_dict_set_operands (font->top_dict,
3393
90
          FDARRAY_OP, buf, end_buf - buf);
3394
90
  if (unlikely (status))
3395
0
      return status;
3396
90
    }
3397
3398
388
    status = cff_dict_set_operands (font->top_dict,
3399
388
                              CHARSET_OP, buf, end_buf - buf);
3400
388
    if (unlikely (status))
3401
0
  return status;
3402
3403
388
    if (!font->scaled_font_subset->is_latin) {
3404
90
  status = cairo_cff_font_set_ros_strings (font);
3405
90
  if (unlikely (status))
3406
0
      return status;
3407
3408
  /* Create CID FD dictionary */
3409
90
  status = cairo_cff_font_create_cid_fontdict (font);
3410
90
  if (unlikely (status))
3411
0
      return status;
3412
298
    } else {
3413
298
  font->private_dict_offset = _cairo_malloc (sizeof (int));
3414
298
  if (unlikely (font->private_dict_offset == NULL))
3415
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3416
298
    }
3417
3418
    /* Create charstrings */
3419
5.91k
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
3420
5.52k
        charstring = _cairo_array_index(&type2_subset->charstrings, i);
3421
3422
5.52k
        status = cff_index_append (&font->charstrings_subset_index,
3423
5.52k
                                   _cairo_array_index (charstring, 0),
3424
5.52k
                                   _cairo_array_num_elements (charstring));
3425
3426
5.52k
        if (unlikely (status))
3427
0
            return status;
3428
5.52k
    }
3429
3430
388
    if (font->scaled_font_subset->is_latin)
3431
298
  status = cairo_cff_font_add_euro_charset_string (font);
3432
3433
388
    status = cairo_cff_font_write_subset (font);
3434
388
    if (unlikely (status))
3435
0
        return status;
3436
3437
388
    *data = _cairo_array_index (&font->output, 0);
3438
388
    *length = _cairo_array_num_elements (&font->output);
3439
3440
388
    return CAIRO_STATUS_SUCCESS;
3441
388
}
3442
3443
cairo_status_t
3444
_cairo_cff_fallback_init (cairo_cff_subset_t          *cff_subset,
3445
                          const char          *subset_name,
3446
                          cairo_scaled_font_subset_t  *font_subset)
3447
388
{
3448
388
    cairo_cff_font_t *font = NULL; /* squelch bogus compiler warning */
3449
388
    cairo_status_t status;
3450
388
    const char *data = NULL; /* squelch bogus compiler warning */
3451
388
    unsigned long length = 0; /* squelch bogus compiler warning */
3452
388
    unsigned int i;
3453
388
    cairo_type2_charstrings_t type2_subset;
3454
3455
388
    status = _cairo_cff_font_fallback_create (font_subset, &font, subset_name);
3456
388
    if (unlikely (status))
3457
0
  return status;
3458
3459
388
    status = _cairo_type2_charstrings_init (&type2_subset, font_subset);
3460
388
    if (unlikely (status))
3461
0
  goto fail1;
3462
3463
388
    status = cairo_cff_font_fallback_generate (font, &type2_subset, &data, &length);
3464
388
    if (unlikely (status))
3465
0
  goto fail2;
3466
3467
388
    cff_subset->family_name_utf8 = NULL;
3468
388
    cff_subset->ps_name = strdup (font->ps_name);
3469
388
    if (unlikely (cff_subset->ps_name == NULL)) {
3470
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3471
0
  goto fail2;
3472
0
    }
3473
3474
388
    cff_subset->widths = _cairo_calloc_ab (font->scaled_font_subset->num_glyphs, sizeof (double));
3475
388
    if (unlikely (cff_subset->widths == NULL)) {
3476
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3477
0
  goto fail3;
3478
0
    }
3479
3480
5.91k
    for (i = 0; i < font->scaled_font_subset->num_glyphs; i++)
3481
5.52k
        cff_subset->widths[i] = (double)type2_subset.widths[i]/1000;
3482
3483
388
    cff_subset->x_min = (double)type2_subset.x_min/1000;
3484
388
    cff_subset->y_min = (double)type2_subset.y_min/1000;
3485
388
    cff_subset->x_max = (double)type2_subset.x_max/1000;
3486
388
    cff_subset->y_max = (double)type2_subset.y_max/1000;
3487
388
    cff_subset->ascent = (double)type2_subset.y_max/1000;
3488
388
    cff_subset->descent = (double)type2_subset.y_min/1000;
3489
3490
388
    cff_subset->data = _cairo_malloc (length);
3491
388
    if (unlikely (cff_subset->data == NULL)) {
3492
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3493
0
  goto fail4;
3494
0
    }
3495
3496
388
    memcpy (cff_subset->data, data, length);
3497
388
    cff_subset->data_length = length;
3498
3499
388
    _cairo_type2_charstrings_fini (&type2_subset);
3500
388
    cairo_cff_font_destroy (font);
3501
3502
388
    return CAIRO_STATUS_SUCCESS;
3503
3504
0
 fail4:
3505
0
    free (cff_subset->widths);
3506
0
 fail3:
3507
0
    free (cff_subset->ps_name);
3508
0
 fail2:
3509
0
    _cairo_type2_charstrings_fini (&type2_subset);
3510
0
 fail1:
3511
0
    cairo_cff_font_destroy (font);
3512
3513
0
    return status;
3514
0
}
3515
3516
void
3517
_cairo_cff_fallback_fini (cairo_cff_subset_t *subset)
3518
388
{
3519
388
    free (subset->ps_name);
3520
388
    free (subset->widths);
3521
388
    free (subset->data);
3522
388
}
3523
3524
#endif /* CAIRO_HAS_FONT_SUBSET */