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
4 * Copyright © 2006 Adrian Johnson
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.
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
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/
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.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Adrian Johnson.
34 * Adrian Johnson <ajohnson@redneon.com>
35 * Eugeniy Meshcheryakov <eugen@debian.org>
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
44 #define _BSD_SOURCE /* for snprintf(), strdup() */
47 #include "cairo-array-private.h"
48 #include "cairo-error-private.h"
50 #if CAIRO_HAS_FONT_SUBSET
52 #include "cairo-scaled-font-subsets-private.h"
53 #include "cairo-truetype-subset-private.h"
57 /* CFF Dict Operators. If the high byte is 0 the command is encoded
58 * with a single byte. */
59 #define BASEFONTNAME_OP 0x0c16
60 #define CIDCOUNT_OP 0x0c22
61 #define CHARSET_OP 0x000f
62 #define CHARSTRINGS_OP 0x0011
63 #define COPYRIGHT_OP 0x0c00
64 #define DEFAULTWIDTH_OP 0x0014
65 #define ENCODING_OP 0x0010
66 #define FAMILYNAME_OP 0x0003
67 #define FDARRAY_OP 0x0c24
68 #define FDSELECT_OP 0x0c25
69 #define FONTBBOX_OP 0x0005
70 #define FONTMATRIX_OP 0x0c07
71 #define FONTNAME_OP 0x0c26
72 #define FULLNAME_OP 0x0002
73 #define LOCAL_SUB_OP 0x0013
74 #define NOMINALWIDTH_OP 0x0015
75 #define NOTICE_OP 0x0001
76 #define POSTSCRIPT_OP 0x0c15
77 #define PRIVATE_OP 0x0012
79 #define UNIQUEID_OP 0x000d
80 #define VERSION_OP 0x0000
81 #define WEIGHT_OP 0x0004
82 #define XUID_OP 0x000e
84 #define NUM_STD_STRINGS 391
86 /* Type 2 Charstring operators */
87 #define TYPE2_hstem 0x0001
88 #define TYPE2_vstem 0x0003
89 #define TYPE2_callsubr 0x000a
91 #define TYPE2_return 0x000b
92 #define TYPE2_endchar 0x000e
94 #define TYPE2_hstemhm 0x0012
95 #define TYPE2_hintmask 0x0013
96 #define TYPE2_cntrmask 0x0014
97 #define TYPE2_vstemhm 0x0017
98 #define TYPE2_callgsubr 0x001d
100 #define TYPE2_rmoveto 0x0015
101 #define TYPE2_hmoveto 0x0016
102 #define TYPE2_vmoveto 0x0004
105 #define MAX_SUBROUTINE_NESTING 10 /* From Type2 Charstring spec */
108 typedef struct _cff_header {
115 typedef struct _cff_index_element {
116 cairo_bool_t is_copy;
119 } cff_index_element_t;
121 typedef struct _cff_dict_operator {
122 cairo_hash_entry_t base;
124 unsigned short operator;
125 unsigned char *operand;
128 } cff_dict_operator_t;
130 typedef struct _cairo_cff_font {
132 cairo_scaled_font_subset_t *scaled_font_subset;
133 const cairo_scaled_font_backend_t *backend;
137 unsigned long data_length;
138 unsigned char *current_ptr;
139 unsigned char *data_end;
140 cff_header_t *header;
143 cairo_hash_table_t *top_dict;
144 cairo_hash_table_t *private_dict;
145 cairo_array_t strings_index;
146 cairo_array_t charstrings_index;
147 cairo_array_t global_sub_index;
148 cairo_array_t local_sub_index;
149 unsigned char *charset;
152 cairo_bool_t is_opentype;
156 double default_width;
157 double nominal_width;
161 unsigned int num_fontdicts;
162 cairo_hash_table_t **fd_dict;
163 cairo_hash_table_t **fd_private_dict;
164 cairo_array_t *fd_local_sub_index;
165 int *fd_local_sub_bias;
166 double *fd_default_width;
167 double *fd_nominal_width;
169 /* Subsetted Font Data */
170 char *subset_font_name;
171 cairo_array_t charstrings_subset_index;
172 cairo_array_t strings_subset_index;
174 int *fdselect_subset;
175 unsigned int num_subset_fontdicts;
177 int *private_dict_offset;
178 cairo_bool_t subset_subroutines;
179 cairo_bool_t *global_subs_used;
180 cairo_bool_t *local_subs_used;
181 cairo_bool_t **fd_local_subs_used;
182 cairo_array_t output;
186 int x_min, y_min, x_max, y_max;
189 /* Type 2 charstring data */
190 int type2_stack_size;
191 int type2_stack_top_value;
192 cairo_bool_t type2_stack_top_is_int;
194 int type2_hintmask_bytes;
195 int type2_nesting_level;
196 cairo_bool_t type2_seen_first_int;
197 cairo_bool_t type2_find_width;
198 cairo_bool_t type2_found_width;
200 cairo_bool_t type2_has_path;
204 /* Encoded integer using maximum sized encoding. This is required for
205 * operands that are later modified after encoding. */
206 static unsigned char *
207 encode_integer_max (unsigned char *p, int i)
211 *p++ = (i >> 16) & 0xff;
212 *p++ = (i >> 8) & 0xff;
217 static unsigned char *
218 encode_integer (unsigned char *p, int i)
220 if (i >= -107 && i <= 107) {
222 } else if (i >= 108 && i <= 1131) {
224 *p++ = (i >> 8)+ 247;
226 } else if (i >= -1131 && i <= -108) {
228 *p++ = (i >> 8)+ 251;
230 } else if (i >= -32768 && i <= 32767) {
232 *p++ = (i >> 8) & 0xff;
235 p = encode_integer_max (p, i);
240 static unsigned char *
241 decode_integer (unsigned char *p, int *integer)
244 *integer = (int)(p[1]<<8 | p[2]);
246 } else if (*p == 29) {
247 *integer = (int)((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
249 } else if (*p >= 32 && *p <= 246) {
250 *integer = *p++ - 139;
251 } else if (*p <= 250) {
252 *integer = (p[0] - 247) * 256 + p[1] + 108;
254 } else if (*p <= 254) {
255 *integer = -(p[0] - 251) * 256 - p[1] - 108;
265 decode_nibble (int n, char *buf)
295 static unsigned char *
296 decode_real (unsigned char *p, double *real)
298 struct lconv *locale_data;
299 const char *decimal_point;
300 int decimal_point_len;
306 char *buf_end = buffer + sizeof (buffer);
308 locale_data = localeconv ();
309 decimal_point = locale_data->decimal_point;
310 decimal_point_len = strlen (decimal_point);
312 assert (decimal_point_len != 0);
313 assert (sizeof(buffer) + decimal_point_len < sizeof(buffer2));
316 while (buf + 2 < buf_end) {
318 buf = decode_nibble (n, buf);
320 buf = decode_nibble (n, buf);
321 if ((*p & 0x0f) == 0x0f) {
330 if (strchr (buffer, '.')) {
331 q = strchr (buffer, '.');
332 strncpy (buffer2, buffer, q - buffer);
333 buf = buffer2 + (q - buffer);
334 strncpy (buf, decimal_point, decimal_point_len);
335 buf += decimal_point_len;
340 if (sscanf(buf, "%lf", real) != 1)
346 static unsigned char *
347 decode_number (unsigned char *p, double *number)
350 p = decode_real (p, number);
353 p = decode_integer (p, &i);
359 static unsigned char *
360 decode_operator (unsigned char *p, unsigned short *operator)
362 unsigned short op = 0;
373 /* return 0 if not an operand */
375 operand_length (unsigned char *p)
377 unsigned char *begin = p;
385 if (*p >= 32 && *p <= 246)
388 if (*p >= 247 && *p <= 254)
392 while ((*p & 0x0f) != 0x0f)
394 return p - begin + 1;
400 static unsigned char *
401 encode_index_offset (unsigned char *p, int offset_size, unsigned long offset)
403 while (--offset_size >= 0) {
404 p[offset_size] = (unsigned char) (offset & 0xff);
407 return p + offset_size;
411 decode_index_offset(unsigned char *p, int off_size)
413 unsigned long offset = 0;
415 while (off_size-- > 0)
416 offset = offset*256 + *p++;
421 cff_index_init (cairo_array_t *index)
423 _cairo_array_init (index, sizeof (cff_index_element_t));
426 static cairo_int_status_t
427 cff_index_read (cairo_array_t *index, unsigned char **ptr, unsigned char *end_ptr)
429 cff_index_element_t element;
430 unsigned char *data, *p;
431 cairo_status_t status;
432 int offset_size, count, start, i;
437 return CAIRO_INT_STATUS_UNSUPPORTED;
438 count = be16_to_cpu( *((uint16_t *)p) );
442 if (p + (count + 1)*offset_size > end_ptr)
443 return CAIRO_INT_STATUS_UNSUPPORTED;
444 data = p + offset_size*(count + 1) - 1;
445 start = decode_index_offset (p, offset_size);
447 for (i = 0; i < count; i++) {
448 end = decode_index_offset (p, offset_size);
451 return CAIRO_INT_STATUS_UNSUPPORTED;
452 element.length = end - start;
453 element.is_copy = FALSE;
454 element.data = data + start;
455 status = _cairo_array_append (index, &element);
456 if (unlikely (status))
464 return CAIRO_STATUS_SUCCESS;
467 static cairo_status_t
468 cff_index_write (cairo_array_t *index, cairo_array_t *output)
474 cff_index_element_t *element;
476 unsigned char buf[5];
477 cairo_status_t status;
479 num_elem = _cairo_array_num_elements (index);
480 count = cpu_to_be16 ((uint16_t) num_elem);
481 status = _cairo_array_append_multiple (output, &count, 2);
482 if (unlikely (status))
486 return CAIRO_STATUS_SUCCESS;
488 /* Find maximum offset to determine offset size */
490 for (i = 0; i < num_elem; i++) {
491 element = _cairo_array_index (index, i);
492 offset += element->length;
496 else if (offset < 0x10000)
498 else if (offset < 0x1000000)
503 buf[0] = (unsigned char) offset_size;
504 status = _cairo_array_append (output, buf);
505 if (unlikely (status))
509 encode_index_offset (buf, offset_size, offset);
510 status = _cairo_array_append_multiple (output, buf, offset_size);
511 if (unlikely (status))
514 for (i = 0; i < num_elem; i++) {
515 element = _cairo_array_index (index, i);
516 offset += element->length;
517 encode_index_offset (buf, offset_size, offset);
518 status = _cairo_array_append_multiple (output, buf, offset_size);
519 if (unlikely (status))
523 for (i = 0; i < num_elem; i++) {
524 element = _cairo_array_index (index, i);
525 if (element->length > 0) {
526 status = _cairo_array_append_multiple (output,
530 if (unlikely (status))
533 return CAIRO_STATUS_SUCCESS;
537 cff_index_set_object (cairo_array_t *index, int obj_index,
538 unsigned char *object , int length)
540 cff_index_element_t *element;
542 element = _cairo_array_index (index, obj_index);
543 if (element->is_copy)
544 free (element->data);
546 element->data = object;
547 element->length = length;
548 element->is_copy = FALSE;
551 static cairo_status_t
552 cff_index_append (cairo_array_t *index, unsigned char *object , int length)
554 cff_index_element_t element;
556 element.length = length;
557 element.is_copy = FALSE;
558 element.data = object;
560 return _cairo_array_append (index, &element);
563 static cairo_status_t
564 cff_index_append_copy (cairo_array_t *index,
565 const unsigned char *object,
568 cff_index_element_t element;
569 cairo_status_t status;
571 element.length = length;
572 element.is_copy = TRUE;
573 element.data = malloc (element.length);
574 if (unlikely (element.data == NULL))
575 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
577 memcpy (element.data, object, element.length);
579 status = _cairo_array_append (index, &element);
580 if (unlikely (status)) {
585 return CAIRO_STATUS_SUCCESS;
589 cff_index_fini (cairo_array_t *index)
591 cff_index_element_t *element;
594 for (i = 0; i < _cairo_array_num_elements (index); i++) {
595 element = _cairo_array_index (index, i);
596 if (element->is_copy && element->data)
597 free (element->data);
599 _cairo_array_fini (index);
603 _cairo_cff_dict_equal (const void *key_a, const void *key_b)
605 const cff_dict_operator_t *op_a = key_a;
606 const cff_dict_operator_t *op_b = key_b;
608 return op_a->operator == op_b->operator;
611 static cairo_status_t
612 cff_dict_init (cairo_hash_table_t **dict)
614 *dict = _cairo_hash_table_create (_cairo_cff_dict_equal);
615 if (unlikely (*dict == NULL))
616 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
618 return CAIRO_STATUS_SUCCESS;
622 _cairo_dict_init_key (cff_dict_operator_t *key, int operator)
624 key->base.hash = (unsigned long) operator;
625 key->operator = operator;
628 static cairo_status_t
629 cff_dict_create_operator (int operator,
630 unsigned char *operand,
632 cff_dict_operator_t **out)
634 cff_dict_operator_t *op;
636 op = malloc (sizeof (cff_dict_operator_t));
637 if (unlikely (op == NULL))
638 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
640 _cairo_dict_init_key (op, operator);
641 op->operand = malloc (size);
642 if (unlikely (op->operand == NULL)) {
644 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
647 memcpy (op->operand, operand, size);
648 op->operand_length = size;
649 op->operand_offset = -1;
652 return CAIRO_STATUS_SUCCESS;
655 static cairo_status_t
656 cff_dict_read (cairo_hash_table_t *dict, unsigned char *p, int dict_size)
659 cairo_array_t operands;
660 cff_dict_operator_t *op;
661 unsigned short operator;
662 cairo_status_t status = CAIRO_STATUS_SUCCESS;
666 _cairo_array_init (&operands, 1);
668 size = operand_length (p);
670 status = _cairo_array_append_multiple (&operands, p, size);
671 if (unlikely (status))
676 p = decode_operator (p, &operator);
677 status = cff_dict_create_operator (operator,
678 _cairo_array_index (&operands, 0),
679 _cairo_array_num_elements (&operands),
681 if (unlikely (status))
684 status = _cairo_hash_table_insert (dict, &op->base);
685 if (unlikely (status))
688 _cairo_array_truncate (&operands, 0);
693 _cairo_array_fini (&operands);
699 cff_dict_remove (cairo_hash_table_t *dict, unsigned short operator)
701 cff_dict_operator_t key, *op;
703 _cairo_dict_init_key (&key, operator);
704 op = _cairo_hash_table_lookup (dict, &key.base);
707 _cairo_hash_table_remove (dict, (cairo_hash_entry_t *) op);
712 static unsigned char *
713 cff_dict_get_operands (cairo_hash_table_t *dict,
714 unsigned short operator,
717 cff_dict_operator_t key, *op;
719 _cairo_dict_init_key (&key, operator);
720 op = _cairo_hash_table_lookup (dict, &key.base);
722 *size = op->operand_length;
729 static cairo_status_t
730 cff_dict_set_operands (cairo_hash_table_t *dict,
731 unsigned short operator,
732 unsigned char *operand,
735 cff_dict_operator_t key, *op;
736 cairo_status_t status;
738 _cairo_dict_init_key (&key, operator);
739 op = _cairo_hash_table_lookup (dict, &key.base);
742 op->operand = malloc (size);
743 if (unlikely (op->operand == NULL))
744 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
746 memcpy (op->operand, operand, size);
747 op->operand_length = size;
751 status = cff_dict_create_operator (operator, operand, size, &op);
752 if (unlikely (status))
755 status = _cairo_hash_table_insert (dict, &op->base);
756 if (unlikely (status))
760 return CAIRO_STATUS_SUCCESS;
764 cff_dict_get_location (cairo_hash_table_t *dict,
765 unsigned short operator,
768 cff_dict_operator_t key, *op;
770 _cairo_dict_init_key (&key, operator);
771 op = _cairo_hash_table_lookup (dict, &key.base);
773 *size = op->operand_length;
774 return op->operand_offset;
780 typedef struct _dict_write_info {
781 cairo_array_t *output;
782 cairo_status_t status;
786 cairo_dict_write_operator (cff_dict_operator_t *op, dict_write_info_t *write_info)
790 op->operand_offset = _cairo_array_num_elements (write_info->output);
791 write_info->status = _cairo_array_append_multiple (write_info->output, op->operand, op->operand_length);
792 if (write_info->status)
795 if (op->operator & 0xff00) {
796 data = op->operator >> 8;
797 write_info->status = _cairo_array_append (write_info->output, &data);
798 if (write_info->status)
801 data = op->operator & 0xff;
802 write_info->status = _cairo_array_append (write_info->output, &data);
806 _cairo_dict_collect (void *entry, void *closure)
808 dict_write_info_t *write_info = closure;
809 cff_dict_operator_t *op = entry;
811 if (write_info->status)
814 /* The ROS operator is handled separately in cff_dict_write() */
815 if (op->operator != ROS_OP)
816 cairo_dict_write_operator (op, write_info);
819 static cairo_status_t
820 cff_dict_write (cairo_hash_table_t *dict, cairo_array_t *output)
822 dict_write_info_t write_info;
823 cff_dict_operator_t key, *op;
825 write_info.output = output;
826 write_info.status = CAIRO_STATUS_SUCCESS;
828 /* The CFF specification requires that the Top Dict of CID fonts
829 * begin with the ROS operator. */
830 _cairo_dict_init_key (&key, ROS_OP);
831 op = _cairo_hash_table_lookup (dict, &key.base);
833 cairo_dict_write_operator (op, &write_info);
835 _cairo_hash_table_foreach (dict, _cairo_dict_collect, &write_info);
837 return write_info.status;
841 _cff_dict_entry_pluck (void *_entry, void *dict)
843 cff_dict_operator_t *entry = _entry;
845 _cairo_hash_table_remove (dict, &entry->base);
846 free (entry->operand);
851 cff_dict_fini (cairo_hash_table_t *dict)
853 _cairo_hash_table_foreach (dict, _cff_dict_entry_pluck, dict);
854 _cairo_hash_table_destroy (dict);
857 static cairo_int_status_t
858 cairo_cff_font_read_header (cairo_cff_font_t *font)
860 if (font->data_length < sizeof (cff_header_t))
861 return CAIRO_INT_STATUS_UNSUPPORTED;
864 font->header = (cff_header_t *) font->data;
865 font->current_ptr = font->data + font->header->header_size;
867 return CAIRO_STATUS_SUCCESS;
870 static cairo_int_status_t
871 cairo_cff_font_read_name (cairo_cff_font_t *font)
874 cairo_int_status_t status;
875 cff_index_element_t *element;
879 cff_index_init (&index);
880 status = cff_index_read (&index, &font->current_ptr, font->data_end);
881 if (!font->is_opentype) {
882 element = _cairo_array_index (&index, 0);
884 len = element->length;
886 /* If font name is prefixed with a subset tag, strip it off. */
887 if (len > 7 && p[6] == '+') {
888 for (i = 0; i < 6; i++)
889 if (p[i] < 'A' || p[i] > 'Z')
896 font->ps_name = malloc (len + 1);
897 if (unlikely (font->ps_name == NULL))
898 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
900 memcpy (font->ps_name, p, len);
901 font->ps_name[len] = 0;
903 cff_index_fini (&index);
908 static cairo_int_status_t
909 cairo_cff_font_read_private_dict (cairo_cff_font_t *font,
910 cairo_hash_table_t *private_dict,
911 cairo_array_t *local_sub_index,
913 cairo_bool_t **local_subs_used,
914 double *default_width,
915 double *nominal_width,
919 cairo_int_status_t status;
920 unsigned char buf[10];
921 unsigned char *end_buf;
924 unsigned char *operand;
928 status = cff_dict_read (private_dict, ptr, size);
929 if (unlikely (status))
932 operand = cff_dict_get_operands (private_dict, LOCAL_SUB_OP, &i);
934 decode_integer (operand, &offset);
936 status = cff_index_read (local_sub_index, &p, font->data_end);
937 if (unlikely (status))
940 /* Use maximum sized encoding to reserve space for later modification. */
941 end_buf = encode_integer_max (buf, 0);
942 status = cff_dict_set_operands (private_dict, LOCAL_SUB_OP, buf, end_buf - buf);
943 if (unlikely (status))
948 operand = cff_dict_get_operands (private_dict, DEFAULTWIDTH_OP, &i);
950 decode_number (operand, default_width);
953 operand = cff_dict_get_operands (private_dict, NOMINALWIDTH_OP, &i);
955 decode_number (operand, nominal_width);
957 num_subs = _cairo_array_num_elements (local_sub_index);
958 *local_subs_used = calloc (num_subs, sizeof (cairo_bool_t));
959 if (unlikely (*local_subs_used == NULL))
960 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
963 *local_sub_bias = 107;
964 else if (num_subs < 33900)
965 *local_sub_bias = 1131;
967 *local_sub_bias = 32768;
969 return CAIRO_STATUS_SUCCESS;
972 static cairo_int_status_t
973 cairo_cff_font_read_fdselect (cairo_cff_font_t *font, unsigned char *p)
975 int type, num_ranges, first, last, fd, i, j;
977 font->fdselect = calloc (font->num_glyphs, sizeof (int));
978 if (unlikely (font->fdselect == NULL))
979 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
984 for (i = 0; i < font->num_glyphs; i++)
985 font->fdselect[i] = *p++;
986 } else if (type == 3) {
987 num_ranges = be16_to_cpu( *((uint16_t *)p) );
989 for (i = 0; i < num_ranges; i++)
991 first = be16_to_cpu( *((uint16_t *)p) );
994 last = be16_to_cpu( *((uint16_t *)p) );
995 for (j = first; j < last; j++)
996 font->fdselect[j] = fd;
999 return CAIRO_INT_STATUS_UNSUPPORTED;
1002 return CAIRO_STATUS_SUCCESS;
1005 static cairo_int_status_t
1006 cairo_cff_font_read_cid_fontdict (cairo_cff_font_t *font, unsigned char *ptr)
1008 cairo_array_t index;
1009 cff_index_element_t *element;
1012 unsigned char *operand;
1014 cairo_int_status_t status;
1015 unsigned char buf[100];
1016 unsigned char *end_buf;
1018 cff_index_init (&index);
1019 status = cff_index_read (&index, &ptr, font->data_end);
1020 if (unlikely (status))
1023 font->num_fontdicts = _cairo_array_num_elements (&index);
1025 font->fd_dict = calloc (sizeof (cairo_hash_table_t *), font->num_fontdicts);
1026 if (unlikely (font->fd_dict == NULL)) {
1027 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1031 font->fd_private_dict = calloc (sizeof (cairo_hash_table_t *), font->num_fontdicts);
1032 if (unlikely (font->fd_private_dict == NULL)) {
1033 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1037 font->fd_local_sub_index = calloc (sizeof (cairo_array_t), font->num_fontdicts);
1038 if (unlikely (font->fd_local_sub_index == NULL)) {
1039 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1043 font->fd_local_sub_bias = calloc (sizeof (int), font->num_fontdicts);
1044 if (unlikely (font->fd_local_sub_bias == NULL)) {
1045 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1049 font->fd_local_subs_used = calloc (sizeof (cairo_bool_t *), font->num_fontdicts);
1050 if (unlikely (font->fd_local_subs_used == NULL)) {
1051 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1055 font->fd_default_width = calloc (sizeof (int), font->num_fontdicts);
1056 if (unlikely (font->fd_default_width == NULL)) {
1057 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1061 font->fd_nominal_width = calloc (sizeof (int), font->num_fontdicts);
1062 if (unlikely (font->fd_nominal_width == NULL)) {
1063 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1067 for (i = 0; i < font->num_fontdicts; i++) {
1068 status = cff_dict_init (&font->fd_dict[i]);
1069 if (unlikely (status))
1072 element = _cairo_array_index (&index, i);
1073 status = cff_dict_read (font->fd_dict[i], element->data, element->length);
1074 if (unlikely (status))
1077 operand = cff_dict_get_operands (font->fd_dict[i], PRIVATE_OP, &size);
1078 if (operand == NULL) {
1079 status = CAIRO_INT_STATUS_UNSUPPORTED;
1082 operand = decode_integer (operand, &size);
1083 decode_integer (operand, &offset);
1084 status = cff_dict_init (&font->fd_private_dict[i]);
1085 if (unlikely (status))
1088 cff_index_init (&font->fd_local_sub_index[i]);
1089 status = cairo_cff_font_read_private_dict (font,
1090 font->fd_private_dict[i],
1091 &font->fd_local_sub_index[i],
1092 &font->fd_local_sub_bias[i],
1093 &font->fd_local_subs_used[i],
1094 &font->fd_default_width[i],
1095 &font->fd_nominal_width[i],
1096 font->data + offset,
1098 if (unlikely (status))
1101 /* Set integer operand to max value to use max size encoding to reserve
1102 * space for any value later */
1103 end_buf = encode_integer_max (buf, 0);
1104 end_buf = encode_integer_max (end_buf, 0);
1105 status = cff_dict_set_operands (font->fd_dict[i], PRIVATE_OP, buf, end_buf - buf);
1106 if (unlikely (status))
1110 return CAIRO_STATUS_SUCCESS;
1113 cff_index_fini (&index);
1119 cairo_cff_font_read_font_metrics (cairo_cff_font_t *font, cairo_hash_table_t *top_dict)
1124 double x_min, y_min, x_max, y_max;
1125 double xx, yx, xy, yy;
1131 p = cff_dict_get_operands (font->top_dict, FONTBBOX_OP, &size);
1135 p = decode_number (p, &x_min);
1137 p = decode_number (p, &y_min);
1139 p = decode_number (p, &x_max);
1141 p = decode_number (p, &y_max);
1143 font->x_min = floor (x_min);
1144 font->y_min = floor (y_min);
1145 font->x_max = floor (x_max);
1146 font->y_max = floor (y_max);
1147 font->ascent = font->y_max;
1148 font->descent = font->y_min;
1154 p = cff_dict_get_operands (font->top_dict, FONTMATRIX_OP, &size);
1158 p = decode_number (p, &xx);
1160 p = decode_number (p, &yx);
1162 p = decode_number (p, &xy);
1164 p = decode_number (p, &yy);
1166 /* Freetype uses 1/yy to get units per EM */
1167 font->units_per_em = _cairo_round(1.0/yy);
1170 static cairo_int_status_t
1171 cairo_cff_font_read_top_dict (cairo_cff_font_t *font)
1173 cairo_array_t index;
1174 cff_index_element_t *element;
1175 unsigned char buf[20];
1176 unsigned char *end_buf;
1177 unsigned char *operand;
1178 cairo_int_status_t status;
1183 cff_index_init (&index);
1184 status = cff_index_read (&index, &font->current_ptr, font->data_end);
1185 if (unlikely (status))
1188 element = _cairo_array_index (&index, 0);
1189 status = cff_dict_read (font->top_dict, element->data, element->length);
1190 if (unlikely (status))
1193 if (cff_dict_get_operands (font->top_dict, ROS_OP, &size) != NULL)
1194 font->is_cid = TRUE;
1196 font->is_cid = FALSE;
1198 operand = cff_dict_get_operands (font->top_dict, CHARSTRINGS_OP, &size);
1199 decode_integer (operand, &offset);
1200 p = font->data + offset;
1201 status = cff_index_read (&font->charstrings_index, &p, font->data_end);
1202 if (unlikely (status))
1204 font->num_glyphs = _cairo_array_num_elements (&font->charstrings_index);
1207 operand = cff_dict_get_operands (font->top_dict, CHARSET_OP, &size);
1209 return CAIRO_INT_STATUS_UNSUPPORTED;
1211 decode_integer (operand, &offset);
1212 font->charset = font->data + offset;
1213 if (font->charset >= font->data_end)
1214 return CAIRO_INT_STATUS_UNSUPPORTED;
1217 if (!font->is_opentype)
1218 cairo_cff_font_read_font_metrics (font, font->top_dict);
1221 operand = cff_dict_get_operands (font->top_dict, FDSELECT_OP, &size);
1222 decode_integer (operand, &offset);
1223 status = cairo_cff_font_read_fdselect (font, font->data + offset);
1224 if (unlikely (status))
1227 operand = cff_dict_get_operands (font->top_dict, FDARRAY_OP, &size);
1228 decode_integer (operand, &offset);
1229 status = cairo_cff_font_read_cid_fontdict (font, font->data + offset);
1230 if (unlikely (status))
1233 operand = cff_dict_get_operands (font->top_dict, PRIVATE_OP, &size);
1234 operand = decode_integer (operand, &size);
1235 decode_integer (operand, &offset);
1236 status = cairo_cff_font_read_private_dict (font,
1238 &font->local_sub_index,
1239 &font->local_sub_bias,
1240 &font->local_subs_used,
1241 &font->default_width,
1242 &font->nominal_width,
1243 font->data + offset,
1245 if (unlikely (status))
1249 /* Use maximum sized encoding to reserve space for later modification. */
1250 end_buf = encode_integer_max (buf, 0);
1251 status = cff_dict_set_operands (font->top_dict,
1252 CHARSTRINGS_OP, buf, end_buf - buf);
1253 if (unlikely (status))
1256 status = cff_dict_set_operands (font->top_dict,
1257 CHARSET_OP, buf, end_buf - buf);
1258 if (unlikely (status))
1261 if (font->scaled_font_subset->is_latin) {
1262 status = cff_dict_set_operands (font->top_dict,
1263 ENCODING_OP, buf, end_buf - buf);
1264 if (unlikely (status))
1267 /* Private has two operands - size and offset */
1268 end_buf = encode_integer_max (end_buf, 0);
1269 cff_dict_set_operands (font->top_dict, PRIVATE_OP, buf, end_buf - buf);
1270 if (unlikely (status))
1274 status = cff_dict_set_operands (font->top_dict,
1275 FDSELECT_OP, buf, end_buf - buf);
1276 if (unlikely (status))
1279 status = cff_dict_set_operands (font->top_dict,
1280 FDARRAY_OP, buf, end_buf - buf);
1281 if (unlikely (status))
1284 cff_dict_remove (font->top_dict, ENCODING_OP);
1285 cff_dict_remove (font->top_dict, PRIVATE_OP);
1288 /* Remove the unique identifier operators as the subsetted font is
1289 * not the same is the original font. */
1290 cff_dict_remove (font->top_dict, UNIQUEID_OP);
1291 cff_dict_remove (font->top_dict, XUID_OP);
1294 cff_index_fini (&index);
1299 static cairo_int_status_t
1300 cairo_cff_font_read_strings (cairo_cff_font_t *font)
1302 return cff_index_read (&font->strings_index, &font->current_ptr, font->data_end);
1305 static cairo_int_status_t
1306 cairo_cff_font_read_global_subroutines (cairo_cff_font_t *font)
1308 cairo_int_status_t status;
1311 status = cff_index_read (&font->global_sub_index, &font->current_ptr, font->data_end);
1312 if (unlikely (status))
1315 num_subs = _cairo_array_num_elements (&font->global_sub_index);
1316 font->global_subs_used = calloc (num_subs, sizeof(cairo_bool_t));
1317 if (unlikely (font->global_subs_used == NULL))
1318 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1320 if (num_subs < 1240)
1321 font->global_sub_bias = 107;
1322 else if (num_subs < 33900)
1323 font->global_sub_bias = 1131;
1325 font->global_sub_bias = 32768;
1327 return CAIRO_STATUS_SUCCESS;
1330 typedef cairo_int_status_t
1331 (*font_read_t) (cairo_cff_font_t *font);
1333 static const font_read_t font_read_funcs[] = {
1334 cairo_cff_font_read_header,
1335 cairo_cff_font_read_name,
1336 cairo_cff_font_read_top_dict,
1337 cairo_cff_font_read_strings,
1338 cairo_cff_font_read_global_subroutines,
1341 static cairo_int_status_t
1342 cairo_cff_font_read_font (cairo_cff_font_t *font)
1344 cairo_int_status_t status;
1347 for (i = 0; i < ARRAY_LENGTH (font_read_funcs); i++) {
1348 status = font_read_funcs[i] (font);
1349 if (unlikely (status))
1353 return CAIRO_STATUS_SUCCESS;
1356 static cairo_status_t
1357 cairo_cff_font_set_ros_strings (cairo_cff_font_t *font)
1359 cairo_status_t status;
1360 unsigned char buf[30];
1363 const char *registry = "Adobe";
1364 const char *ordering = "Identity";
1366 sid1 = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1367 status = cff_index_append_copy (&font->strings_subset_index,
1368 (unsigned char *)registry,
1370 if (unlikely (status))
1373 sid2 = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1374 status = cff_index_append_copy (&font->strings_subset_index,
1375 (unsigned char *)ordering,
1377 if (unlikely (status))
1380 p = encode_integer (buf, sid1);
1381 p = encode_integer (p, sid2);
1382 p = encode_integer (p, 0);
1383 status = cff_dict_set_operands (font->top_dict, ROS_OP, buf, p - buf);
1384 if (unlikely (status))
1387 p = encode_integer (buf, font->scaled_font_subset->num_glyphs);
1388 status = cff_dict_set_operands (font->top_dict, CIDCOUNT_OP, buf, p - buf);
1389 if (unlikely (status))
1392 return CAIRO_STATUS_SUCCESS;
1395 static cairo_status_t
1396 cairo_cff_font_subset_dict_string(cairo_cff_font_t *font,
1397 cairo_hash_table_t *dict,
1403 unsigned char buf[100];
1404 cff_index_element_t *element;
1405 cairo_status_t status;
1407 p = cff_dict_get_operands (dict, operator, &size);
1409 return CAIRO_STATUS_SUCCESS;
1411 decode_integer (p, &sid);
1412 if (sid < NUM_STD_STRINGS)
1413 return CAIRO_STATUS_SUCCESS;
1415 element = _cairo_array_index (&font->strings_index, sid - NUM_STD_STRINGS);
1416 sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1417 status = cff_index_append (&font->strings_subset_index, element->data, element->length);
1418 if (unlikely (status))
1421 p = encode_integer (buf, sid);
1422 status = cff_dict_set_operands (dict, operator, buf, p - buf);
1423 if (unlikely (status))
1426 return CAIRO_STATUS_SUCCESS;
1429 static const int dict_strings[] = {
1441 static cairo_status_t
1442 cairo_cff_font_subset_dict_strings (cairo_cff_font_t *font,
1443 cairo_hash_table_t *dict)
1445 cairo_status_t status;
1448 for (i = 0; i < ARRAY_LENGTH (dict_strings); i++) {
1449 status = cairo_cff_font_subset_dict_string (font, dict, dict_strings[i]);
1450 if (unlikely (status))
1454 return CAIRO_STATUS_SUCCESS;
1457 static unsigned char *
1458 type2_decode_integer (unsigned char *p, int *integer)
1461 *integer = p[1] << 8 | p[2];
1463 } else if (*p <= 246) {
1464 *integer = *p++ - 139;
1465 } else if (*p <= 250) {
1466 *integer = (p[0] - 247) * 256 + p[1] + 108;
1468 } else if (*p <= 254) {
1469 *integer = -(p[0] - 251) * 256 - p[1] - 108;
1471 } else { /* *p == 255 */
1472 /* 16.16 fixed-point number. The fraction is ignored. */
1473 *integer = (int16_t)((p[1] << 8) | p[2]);
1479 /* Type 2 charstring parser for finding calls to local or global
1480 * subroutines. For non Opentype CFF fonts it also gets the glyph
1483 * When we find a subroutine operator, the subroutine is marked as in
1484 * use and recursively followed. The subroutine number is the value on
1485 * the top of the stack when the subroutine operator is executed. In
1486 * most fonts the subroutine number is encoded in an integer
1487 * immediately preceding the subroutine operator. However it is
1488 * possible for the subroutine number on the stack to be the result of
1489 * a computation (in which case there will be an operator preceding
1490 * the subroutine operator). If this occurs, subroutine subsetting is
1491 * disabled since we can't easily determine which subroutines are
1494 * The width, if present, is the first integer in the charstring. The
1495 * only way to confirm if the integer at the start of the charstring is
1496 * the width is when the first stack clearing operator is parsed,
1497 * check if there is an extra integer left over on the stack.
1499 * When the first stack clearing operator is encountered
1500 * type2_find_width is set to FALSE and type2_found_width is set to
1501 * TRUE if an extra argument is found, otherwise FALSE.
1503 static cairo_status_t
1504 cairo_cff_parse_charstring (cairo_cff_font_t *font,
1505 unsigned char *charstring, int length,
1507 cairo_bool_t need_width)
1509 unsigned char *p = charstring;
1510 unsigned char *end = charstring + length;
1514 cff_index_element_t *element;
1518 if (*p == 28 || *p >= 32) {
1520 p = type2_decode_integer (p, &integer);
1521 font->type2_stack_size++;
1522 font->type2_stack_top_value = integer;
1523 font->type2_stack_top_is_int = TRUE;
1524 if (!font->type2_seen_first_int) {
1525 font->type2_width = integer;
1526 font->type2_seen_first_int = TRUE;
1528 } else if (*p == TYPE2_hstem || *p == TYPE2_vstem ||
1529 *p == TYPE2_hstemhm || *p == TYPE2_vstemhm) {
1530 /* Hint operator. The number of hints declared by the
1531 * operator depends on the size of the stack. */
1532 font->type2_stack_top_is_int = FALSE;
1533 font->type2_num_hints += font->type2_stack_size/2;
1534 if (font->type2_find_width && font->type2_stack_size % 2)
1535 font->type2_found_width = TRUE;
1537 font->type2_stack_size = 0;
1538 font->type2_find_width = FALSE;
1540 } else if (*p == TYPE2_hintmask || *p == TYPE2_cntrmask) {
1541 /* Hintmask operator. These operators are followed by a
1542 * variable length mask where the length depends on the
1543 * number of hints declared. The first time this is called
1544 * it is also an implicit vstem if there are arguments on
1546 if (font->type2_hintmask_bytes == 0) {
1547 font->type2_stack_top_is_int = FALSE;
1548 font->type2_num_hints += font->type2_stack_size/2;
1549 if (font->type2_find_width && font->type2_stack_size % 2)
1550 font->type2_found_width = TRUE;
1552 font->type2_stack_size = 0;
1553 font->type2_find_width = FALSE;
1554 font->type2_hintmask_bytes = (font->type2_num_hints+7)/8;
1557 hint_bytes = font->type2_hintmask_bytes;
1560 } else if (*p == TYPE2_rmoveto) {
1561 if (font->type2_find_width && font->type2_stack_size > 2)
1562 font->type2_found_width = TRUE;
1564 font->type2_stack_size = 0;
1565 font->type2_find_width = FALSE;
1566 font->type2_has_path = TRUE;
1568 } else if (*p == TYPE2_hmoveto || *p == TYPE2_vmoveto) {
1569 if (font->type2_find_width && font->type2_stack_size > 1)
1570 font->type2_found_width = TRUE;
1572 font->type2_stack_size = 0;
1573 font->type2_find_width = FALSE;
1574 font->type2_has_path = TRUE;
1576 } else if (*p == TYPE2_endchar) {
1577 if (!font->type2_has_path && font->type2_stack_size > 3)
1578 return CAIRO_INT_STATUS_UNSUPPORTED; /* seac (Ref Appendix C of Type 2 Charstring Format */
1580 if (font->type2_find_width && font->type2_stack_size > 0)
1581 font->type2_found_width = TRUE;
1583 return CAIRO_STATUS_SUCCESS;
1584 } else if (*p == TYPE2_callsubr) {
1585 /* call to local subroutine */
1586 if (! font->type2_stack_top_is_int)
1587 return CAIRO_INT_STATUS_UNSUPPORTED;
1589 if (++font->type2_nesting_level > MAX_SUBROUTINE_NESTING)
1590 return CAIRO_INT_STATUS_UNSUPPORTED;
1593 font->type2_stack_top_is_int = FALSE;
1594 font->type2_stack_size--;
1595 if (font->type2_find_width && font->type2_stack_size == 0)
1596 font->type2_seen_first_int = FALSE;
1599 fd = font->fdselect[glyph_id];
1600 sub_num = font->type2_stack_top_value + font->fd_local_sub_bias[fd];
1601 element = _cairo_array_index (&font->fd_local_sub_index[fd], sub_num);
1602 if (! font->fd_local_subs_used[fd][sub_num]) {
1603 font->fd_local_subs_used[fd][sub_num] = TRUE;
1604 cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1607 sub_num = font->type2_stack_top_value + font->local_sub_bias;
1608 element = _cairo_array_index (&font->local_sub_index, sub_num);
1609 if (! font->local_subs_used[sub_num] ||
1610 (need_width && !font->type2_found_width))
1612 font->local_subs_used[sub_num] = TRUE;
1613 cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1616 font->type2_nesting_level--;
1617 } else if (*p == TYPE2_callgsubr) {
1618 /* call to global subroutine */
1619 if (! font->type2_stack_top_is_int)
1620 return CAIRO_INT_STATUS_UNSUPPORTED;
1622 if (++font->type2_nesting_level > MAX_SUBROUTINE_NESTING)
1623 return CAIRO_INT_STATUS_UNSUPPORTED;
1626 font->type2_stack_size--;
1627 font->type2_stack_top_is_int = FALSE;
1628 if (font->type2_find_width && font->type2_stack_size == 0)
1629 font->type2_seen_first_int = FALSE;
1631 sub_num = font->type2_stack_top_value + font->global_sub_bias;
1632 element = _cairo_array_index (&font->global_sub_index, sub_num);
1633 if (! font->global_subs_used[sub_num] ||
1634 (need_width && !font->type2_found_width))
1636 font->global_subs_used[sub_num] = TRUE;
1637 cairo_cff_parse_charstring (font, element->data, element->length, glyph_id, need_width);
1639 font->type2_nesting_level--;
1640 } else if (*p == 12) {
1641 /* 2 byte instruction */
1643 /* All the 2 byte operators are either not valid before a
1644 * stack clearing operator or they are one of the
1645 * arithmetic, storage, or conditional operators. */
1646 if (need_width && font->type2_find_width)
1647 return CAIRO_INT_STATUS_UNSUPPORTED;
1650 font->type2_stack_top_is_int = FALSE;
1652 /* 1 byte instruction */
1654 font->type2_stack_top_is_int = FALSE;
1658 return CAIRO_STATUS_SUCCESS;
1661 static cairo_status_t
1662 cairo_cff_find_width_and_subroutines_used (cairo_cff_font_t *font,
1663 unsigned char *charstring, int length,
1664 int glyph_id, int subset_id)
1666 cairo_status_t status;
1670 font->type2_stack_size = 0;
1671 font->type2_stack_top_value = 0;;
1672 font->type2_stack_top_is_int = FALSE;
1673 font->type2_num_hints = 0;
1674 font->type2_hintmask_bytes = 0;
1675 font->type2_nesting_level = 0;
1676 font->type2_seen_first_int = FALSE;
1677 font->type2_find_width = TRUE;
1678 font->type2_found_width = FALSE;
1679 font->type2_width = 0;
1680 font->type2_has_path = FALSE;
1682 status = cairo_cff_parse_charstring (font, charstring, length, glyph_id, TRUE);
1686 if (!font->is_opentype) {
1688 fd = font->fdselect[glyph_id];
1689 if (font->type2_found_width)
1690 width = font->fd_nominal_width[fd] + font->type2_width;
1692 width = font->fd_default_width[fd];
1694 if (font->type2_found_width)
1695 width = font->nominal_width + font->type2_width;
1697 width = font->default_width;
1699 font->widths[subset_id] = width;
1702 return CAIRO_STATUS_SUCCESS;
1705 static cairo_int_status_t
1706 cairo_cff_font_get_gid_for_cid (cairo_cff_font_t *font, unsigned long cid, unsigned long *gid)
1709 unsigned long first_gid;
1710 unsigned long first_cid;
1716 return CAIRO_STATUS_SUCCESS;
1719 switch (font->charset[0]) {
1722 p = font->charset + 1;
1724 while (g <= (unsigned)font->num_glyphs && p < font->data_end) {
1725 c = be16_to_cpu( *((uint16_t *)p) );
1728 return CAIRO_STATUS_SUCCESS;
1738 p = font->charset + 1;
1739 while (first_gid <= (unsigned)font->num_glyphs && p + 2 < font->data_end) {
1740 first_cid = be16_to_cpu( *((uint16_t *)p) );
1742 if (cid >= first_cid && cid <= first_cid + num_left) {
1743 *gid = first_gid + cid - first_cid;
1744 return CAIRO_STATUS_SUCCESS;
1746 first_gid += num_left + 1;
1754 p = font->charset + 1;
1755 while (first_gid <= (unsigned)font->num_glyphs && p + 3 < font->data_end) {
1756 first_cid = be16_to_cpu( *((uint16_t *)p) );
1757 num_left = be16_to_cpu( *((uint16_t *)(p+2)) );
1758 if (cid >= first_cid && cid <= first_cid + num_left) {
1759 *gid = first_gid + cid - first_cid;
1760 return CAIRO_STATUS_SUCCESS;
1762 first_gid += num_left + 1;
1770 return CAIRO_INT_STATUS_UNSUPPORTED;
1773 static cairo_int_status_t
1774 cairo_cff_font_subset_charstrings_and_subroutines (cairo_cff_font_t *font)
1776 cff_index_element_t *element;
1778 cairo_int_status_t status;
1779 unsigned long glyph, cid;
1781 font->subset_subroutines = TRUE;
1782 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
1784 cid = font->scaled_font_subset->glyphs[i];
1785 status = cairo_cff_font_get_gid_for_cid (font, cid, &glyph);
1786 if (unlikely (status))
1789 glyph = font->scaled_font_subset->glyphs[i];
1791 element = _cairo_array_index (&font->charstrings_index, glyph);
1792 status = cff_index_append (&font->charstrings_subset_index,
1795 if (unlikely (status))
1798 if (font->subset_subroutines) {
1799 status = cairo_cff_find_width_and_subroutines_used (font,
1800 element->data, element->length,
1802 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1803 /* If parsing the charstrings fails we embed all the
1804 * subroutines. But if the font is not opentype we
1805 * need to successfully parse all charstrings to get
1807 font->subset_subroutines = FALSE;
1808 if (!font->is_opentype)
1810 } else if (unlikely (status)) {
1816 return CAIRO_STATUS_SUCCESS;
1819 static cairo_status_t
1820 cairo_cff_font_subset_fontdict (cairo_cff_font_t *font)
1825 unsigned long cid, gid;
1826 cairo_int_status_t status;
1828 font->fdselect_subset = calloc (font->scaled_font_subset->num_glyphs,
1830 if (unlikely (font->fdselect_subset == NULL))
1831 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1833 font->fd_subset_map = calloc (font->num_fontdicts, sizeof (int));
1834 if (unlikely (font->fd_subset_map == NULL))
1835 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1837 font->private_dict_offset = calloc (font->num_fontdicts, sizeof (int));
1838 if (unlikely (font->private_dict_offset == NULL))
1839 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1841 reverse_map = calloc (font->num_fontdicts, sizeof (int));
1842 if (unlikely (reverse_map == NULL))
1843 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1845 for (i = 0; i < font->num_fontdicts; i++)
1846 reverse_map[i] = -1;
1848 font->num_subset_fontdicts = 0;
1849 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
1850 cid = font->scaled_font_subset->glyphs[i];
1851 status = cairo_cff_font_get_gid_for_cid (font, cid, &gid);
1852 if (unlikely (status))
1855 fd = font->fdselect[gid];
1856 if (reverse_map[fd] < 0) {
1857 font->fd_subset_map[font->num_subset_fontdicts] = fd;
1858 reverse_map[fd] = font->num_subset_fontdicts++;
1860 font->fdselect_subset[i] = reverse_map[fd];
1865 return CAIRO_STATUS_SUCCESS;
1868 static cairo_status_t
1869 cairo_cff_font_create_cid_fontdict (cairo_cff_font_t *font)
1871 unsigned char buf[100];
1872 unsigned char *end_buf;
1873 cairo_status_t status;
1875 font->num_fontdicts = 1;
1876 font->fd_dict = malloc (sizeof (cairo_hash_table_t *));
1877 if (unlikely (font->fd_dict == NULL))
1878 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1880 if (cff_dict_init (&font->fd_dict[0])) {
1881 free (font->fd_dict);
1882 font->fd_dict = NULL;
1883 font->num_fontdicts = 0;
1884 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1887 font->fd_subset_map = malloc (sizeof (int));
1888 if (unlikely (font->fd_subset_map == NULL))
1889 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1891 font->private_dict_offset = malloc (sizeof (int));
1892 if (unlikely (font->private_dict_offset == NULL))
1893 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1895 font->fd_subset_map[0] = 0;
1896 font->num_subset_fontdicts = 1;
1898 /* Set integer operand to max value to use max size encoding to reserve
1899 * space for any value later */
1900 end_buf = encode_integer_max (buf, 0);
1901 end_buf = encode_integer_max (end_buf, 0);
1902 status = cff_dict_set_operands (font->fd_dict[0], PRIVATE_OP, buf, end_buf - buf);
1903 if (unlikely (status))
1906 return CAIRO_STATUS_SUCCESS;
1909 static cairo_status_t
1910 cairo_cff_font_subset_strings (cairo_cff_font_t *font)
1912 cairo_status_t status;
1915 status = cairo_cff_font_subset_dict_strings (font, font->top_dict);
1916 if (unlikely (status))
1920 for (i = 0; i < font->num_subset_fontdicts; i++) {
1921 status = cairo_cff_font_subset_dict_strings (font, font->fd_dict[font->fd_subset_map[i]]);
1922 if (unlikely (status))
1925 status = cairo_cff_font_subset_dict_strings (font, font->fd_private_dict[font->fd_subset_map[i]]);
1926 if (unlikely (status))
1930 status = cairo_cff_font_subset_dict_strings (font, font->private_dict);
1936 /* The Euro is the only the only character in the winansi encoding
1937 * with a glyph name that is not a CFF standard string. As the strings
1938 * are written before the charset, we need to check during the
1939 * subsetting phase if the Euro glyph is required and add the
1940 * glyphname to the list of strings to write out.
1942 static cairo_status_t
1943 cairo_cff_font_add_euro_charset_string (cairo_cff_font_t *font)
1945 cairo_status_t status;
1948 const char *euro = "Euro";
1950 for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
1951 ch = font->scaled_font_subset->to_latin_char[i];
1953 font->euro_sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
1954 status = cff_index_append_copy (&font->strings_subset_index,
1955 (unsigned char *)euro, strlen(euro));
1960 return CAIRO_STATUS_SUCCESS;
1963 static cairo_status_t
1964 cairo_cff_font_subset_font (cairo_cff_font_t *font)
1966 cairo_status_t status;
1968 if (!font->scaled_font_subset->is_latin) {
1969 status = cairo_cff_font_set_ros_strings (font);
1970 if (unlikely (status))
1974 status = cairo_cff_font_subset_charstrings_and_subroutines (font);
1975 if (unlikely (status))
1978 if (!font->scaled_font_subset->is_latin) {
1980 status = cairo_cff_font_subset_fontdict (font);
1982 status = cairo_cff_font_create_cid_fontdict (font);
1983 if (unlikely (status))
1986 font->private_dict_offset = malloc (sizeof (int));
1987 if (unlikely (font->private_dict_offset == NULL))
1988 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1991 status = cairo_cff_font_subset_strings (font);
1992 if (unlikely (status))
1995 if (font->scaled_font_subset->is_latin)
1996 status = cairo_cff_font_add_euro_charset_string (font);
2001 /* Set the operand of the specified operator in the (already written)
2002 * top dict to point to the current position in the output
2003 * array. Operands updated with this function must have previously
2004 * been encoded with the 5-byte (max) integer encoding. */
2006 cairo_cff_font_set_topdict_operator_to_cur_pos (cairo_cff_font_t *font,
2012 unsigned char buf[10];
2013 unsigned char *buf_end;
2014 unsigned char *op_ptr;
2016 cur_pos = _cairo_array_num_elements (&font->output);
2017 buf_end = encode_integer_max (buf, cur_pos);
2018 offset = cff_dict_get_location (font->top_dict, operator, &size);
2019 assert (offset > 0);
2020 op_ptr = _cairo_array_index (&font->output, offset);
2021 memcpy (op_ptr, buf, buf_end - buf);
2024 static cairo_status_t
2025 cairo_cff_font_write_header (cairo_cff_font_t *font)
2027 return _cairo_array_append_multiple (&font->output,
2029 font->header->header_size);
2032 static cairo_status_t
2033 cairo_cff_font_write_name (cairo_cff_font_t *font)
2035 cairo_status_t status = CAIRO_STATUS_SUCCESS;
2036 cairo_array_t index;
2038 cff_index_init (&index);
2040 status = cff_index_append_copy (&index,
2041 (unsigned char *) font->ps_name,
2042 strlen(font->ps_name));
2043 if (unlikely (status))
2046 status = cff_index_write (&index, &font->output);
2047 if (unlikely (status))
2051 cff_index_fini (&index);
2056 static cairo_status_t
2057 cairo_cff_font_write_top_dict (cairo_cff_font_t *font)
2060 unsigned char buf[10];
2063 int dict_start, dict_size;
2064 int offset_size = 4;
2065 cairo_status_t status;
2067 /* Write an index containing the top dict */
2069 count = cpu_to_be16 (1);
2070 status = _cairo_array_append_multiple (&font->output, &count, 2);
2071 if (unlikely (status))
2073 buf[0] = offset_size;
2074 status = _cairo_array_append (&font->output, buf);
2075 if (unlikely (status))
2077 encode_index_offset (buf, offset_size, 1);
2078 status = _cairo_array_append_multiple (&font->output, buf, offset_size);
2079 if (unlikely (status))
2082 /* Reserve space for last element of offset array and update after
2083 * dict is written */
2084 offset_index = _cairo_array_num_elements (&font->output);
2085 status = _cairo_array_append_multiple (&font->output, buf, offset_size);
2086 if (unlikely (status))
2089 dict_start = _cairo_array_num_elements (&font->output);
2090 status = cff_dict_write (font->top_dict, &font->output);
2091 if (unlikely (status))
2093 dict_size = _cairo_array_num_elements (&font->output) - dict_start;
2095 encode_index_offset (buf, offset_size, dict_size + 1);
2096 p = _cairo_array_index (&font->output, offset_index);
2097 memcpy (p, buf, offset_size);
2099 return CAIRO_STATUS_SUCCESS;
2102 static cairo_status_t
2103 cairo_cff_font_write_strings (cairo_cff_font_t *font)
2105 return cff_index_write (&font->strings_subset_index, &font->output);
2108 static cairo_status_t
2109 cairo_cff_font_write_global_subrs (cairo_cff_font_t *font)
2112 unsigned char return_op = TYPE2_return;
2114 /* poppler and fontforge don't like zero length subroutines so we
2115 * replace unused subroutines with a 'return' instruction. */
2116 if (font->subset_subroutines) {
2117 for (i = 0; i < _cairo_array_num_elements (&font->global_sub_index); i++) {
2118 if (! font->global_subs_used[i])
2119 cff_index_set_object (&font->global_sub_index, i, &return_op, 1);
2123 return cff_index_write (&font->global_sub_index, &font->output);
2126 static cairo_status_t
2127 cairo_cff_font_write_encoding (cairo_cff_font_t *font)
2129 unsigned char buf[2];
2130 cairo_status_t status;
2133 cairo_cff_font_set_topdict_operator_to_cur_pos (font, ENCODING_OP);
2134 buf[0] = 0; /* Format 0 */
2135 buf[1] = font->scaled_font_subset->num_glyphs - 1;
2136 status = _cairo_array_append_multiple (&font->output, buf, 2);
2137 if (unlikely (status))
2140 for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
2141 unsigned char ch = font->scaled_font_subset->to_latin_char[i];
2142 status = _cairo_array_append (&font->output, &ch);
2143 if (unlikely (status))
2147 return CAIRO_STATUS_SUCCESS;
2150 static cairo_status_t
2151 cairo_cff_font_write_fdselect (cairo_cff_font_t *font)
2155 cairo_int_status_t status;
2157 cairo_cff_font_set_topdict_operator_to_cur_pos (font, FDSELECT_OP);
2161 status = _cairo_array_append (&font->output, &data);
2162 if (unlikely (status))
2165 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
2166 data = font->fdselect_subset[i];
2167 status = _cairo_array_append (&font->output, &data);
2168 if (unlikely (status))
2175 status = _cairo_array_grow_by (&font->output, 9);
2176 if (unlikely (status))
2180 status = _cairo_array_append (&font->output, &byte);
2181 assert (status == CAIRO_INT_STATUS_SUCCESS);
2183 word = cpu_to_be16 (1);
2184 status = _cairo_array_append_multiple (&font->output, &word, 2);
2185 assert (status == CAIRO_INT_STATUS_SUCCESS);
2187 word = cpu_to_be16 (0);
2188 status = _cairo_array_append_multiple (&font->output, &word, 2);
2189 assert (status == CAIRO_INT_STATUS_SUCCESS);
2192 status = _cairo_array_append (&font->output, &byte);
2193 assert (status == CAIRO_INT_STATUS_SUCCESS);
2195 word = cpu_to_be16 (font->scaled_font_subset->num_glyphs);
2196 status = _cairo_array_append_multiple (&font->output, &word, 2);
2197 assert (status == CAIRO_INT_STATUS_SUCCESS);
2200 return CAIRO_STATUS_SUCCESS;
2203 /* Winansi to CFF standard strings mapping for characters 128 to 255 */
2204 static const int winansi_to_cff_std_string[] = {
2206 0, 0, 117, 101, 118, 121, 112, 113,
2207 126, 122, 192, 107, 142, 0, 199, 0,
2209 0, 65, 8, 105, 119, 116, 111, 137,
2210 127, 153, 221, 108, 148, 0, 228, 198,
2212 0, 96, 97, 98, 103, 100, 160, 102,
2213 131, 170, 139, 106, 151, 0, 165, 128,
2215 161, 156, 164, 169, 125, 152, 115, 114,
2216 133, 150, 143, 120, 158, 155, 163, 123,
2218 174, 171, 172, 176, 173, 175, 138, 177,
2219 181, 178, 179, 180, 185, 182, 183, 184,
2221 154, 186, 190, 187, 188, 191, 189, 168,
2222 141, 196, 193, 194, 195, 197, 157, 149,
2224 203, 200, 201, 205, 202, 204, 144, 206,
2225 210, 207, 208, 209, 214, 211, 212, 213,
2227 167, 215, 219, 216, 217, 220, 218, 159,
2228 147, 225, 222, 223, 224, 226, 162, 227,
2232 cairo_cff_font_get_sid_for_winansi_char (cairo_cff_font_t *font, int ch)
2239 } else if (ch == 96) {
2242 } else if (ch >= 32 && ch <= 126) {
2245 } else if (ch == 128) {
2246 assert (font->euro_sid >= NUM_STD_STRINGS);
2247 sid = font->euro_sid;
2249 } else if (ch >= 128 && ch <= 255) {
2250 sid = winansi_to_cff_std_string[ch - 128];
2259 static cairo_status_t
2260 cairo_cff_font_write_type1_charset (cairo_cff_font_t *font)
2262 unsigned char format = 0;
2265 cairo_status_t status;
2268 cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSET_OP);
2269 status = _cairo_array_append (&font->output, &format);
2270 if (unlikely (status))
2273 for (i = 1; i < font->scaled_font_subset->num_glyphs; i++) {
2274 ch = font->scaled_font_subset->to_latin_char[i];
2275 sid = cairo_cff_font_get_sid_for_winansi_char (font, ch);
2276 if (unlikely (status))
2279 sid_be16 = cpu_to_be16(sid);
2280 status = _cairo_array_append_multiple (&font->output, &sid_be16, sizeof(sid_be16));
2281 if (unlikely (status))
2285 return CAIRO_STATUS_SUCCESS;
2288 static cairo_status_t
2289 cairo_cff_font_write_cid_charset (cairo_cff_font_t *font)
2293 cairo_status_t status;
2295 cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSET_OP);
2296 status = _cairo_array_grow_by (&font->output, 5);
2297 if (unlikely (status))
2301 status = _cairo_array_append (&font->output, &byte);
2302 assert (status == CAIRO_STATUS_SUCCESS);
2304 word = cpu_to_be16 (1);
2305 status = _cairo_array_append_multiple (&font->output, &word, 2);
2306 assert (status == CAIRO_STATUS_SUCCESS);
2308 word = cpu_to_be16 (font->scaled_font_subset->num_glyphs - 2);
2309 status = _cairo_array_append_multiple (&font->output, &word, 2);
2310 assert (status == CAIRO_STATUS_SUCCESS);
2312 return CAIRO_STATUS_SUCCESS;
2315 static cairo_status_t
2316 cairo_cff_font_write_charstrings (cairo_cff_font_t *font)
2318 cairo_cff_font_set_topdict_operator_to_cur_pos (font, CHARSTRINGS_OP);
2320 return cff_index_write (&font->charstrings_subset_index, &font->output);
2323 static cairo_status_t
2324 cairo_cff_font_write_cid_fontdict (cairo_cff_font_t *font)
2327 cairo_int_status_t status;
2328 unsigned int offset_array;
2329 uint32_t *offset_array_ptr;
2332 uint8_t offset_size = 4;
2334 cairo_cff_font_set_topdict_operator_to_cur_pos (font, FDARRAY_OP);
2335 count = cpu_to_be16 (font->num_subset_fontdicts);
2336 status = _cairo_array_append_multiple (&font->output, &count, sizeof (uint16_t));
2337 if (unlikely (status))
2339 status = _cairo_array_append (&font->output, &offset_size);
2340 if (unlikely (status))
2343 offset_array = _cairo_array_num_elements (&font->output);
2344 status = _cairo_array_allocate (&font->output,
2345 (font->num_subset_fontdicts + 1)*offset_size,
2346 (void **) &offset_array_ptr);
2347 if (unlikely (status))
2349 offset_base = _cairo_array_num_elements (&font->output) - 1;
2350 *offset_array_ptr = cpu_to_be32(1);
2351 offset_array += sizeof(uint32_t);
2352 for (i = 0; i < font->num_subset_fontdicts; i++) {
2353 status = cff_dict_write (font->fd_dict[font->fd_subset_map[i]],
2355 if (unlikely (status))
2358 offset_array_ptr = (uint32_t *) _cairo_array_index (&font->output, offset_array);
2359 *offset_array_ptr = cpu_to_be32(_cairo_array_num_elements (&font->output) - offset_base);
2360 offset_array += sizeof(uint32_t);
2363 return CAIRO_STATUS_SUCCESS;
2366 static cairo_status_t
2367 cairo_cff_font_write_private_dict (cairo_cff_font_t *font,
2369 cairo_hash_table_t *parent_dict,
2370 cairo_hash_table_t *private_dict)
2374 unsigned char buf[10];
2375 unsigned char *buf_end;
2377 cairo_status_t status;
2379 /* Write private dict and update offset and size in top dict */
2380 font->private_dict_offset[dict_num] = _cairo_array_num_elements (&font->output);
2381 status = cff_dict_write (private_dict, &font->output);
2382 if (unlikely (status))
2385 size = _cairo_array_num_elements (&font->output) - font->private_dict_offset[dict_num];
2386 /* private entry has two operands - size and offset */
2387 buf_end = encode_integer_max (buf, size);
2388 buf_end = encode_integer_max (buf_end, font->private_dict_offset[dict_num]);
2389 offset = cff_dict_get_location (parent_dict, PRIVATE_OP, &size);
2390 assert (offset > 0);
2391 p = _cairo_array_index (&font->output, offset);
2392 memcpy (p, buf, buf_end - buf);
2394 return CAIRO_STATUS_SUCCESS;
2397 static cairo_status_t
2398 cairo_cff_font_write_local_sub (cairo_cff_font_t *font,
2400 cairo_hash_table_t *private_dict,
2401 cairo_array_t *local_sub_index,
2402 cairo_bool_t *local_subs_used)
2406 unsigned char buf[10];
2407 unsigned char *buf_end;
2409 cairo_status_t status;
2411 unsigned char return_op = TYPE2_return;
2413 if (_cairo_array_num_elements (local_sub_index) > 0) {
2414 /* Write local subroutines and update offset in private
2415 * dict. Local subroutines offset is relative to start of
2417 offset = _cairo_array_num_elements (&font->output) - font->private_dict_offset[dict_num];
2418 buf_end = encode_integer_max (buf, offset);
2419 offset = cff_dict_get_location (private_dict, LOCAL_SUB_OP, &size);
2420 assert (offset > 0);
2421 p = _cairo_array_index (&font->output, offset);
2422 memcpy (p, buf, buf_end - buf);
2424 /* poppler and fontforge don't like zero length subroutines so
2425 * we replace unused subroutines with a 'return' instruction.
2427 if (font->subset_subroutines) {
2428 for (i = 0; i < _cairo_array_num_elements (local_sub_index); i++) {
2429 if (! local_subs_used[i])
2430 cff_index_set_object (local_sub_index, i, &return_op, 1);
2433 status = cff_index_write (local_sub_index, &font->output);
2434 if (unlikely (status))
2438 return CAIRO_STATUS_SUCCESS;
2442 static cairo_status_t
2443 cairo_cff_font_write_cid_private_dict_and_local_sub (cairo_cff_font_t *font)
2446 cairo_int_status_t status;
2449 for (i = 0; i < font->num_subset_fontdicts; i++) {
2450 status = cairo_cff_font_write_private_dict (
2453 font->fd_dict[font->fd_subset_map[i]],
2454 font->fd_private_dict[font->fd_subset_map[i]]);
2455 if (unlikely (status))
2459 for (i = 0; i < font->num_subset_fontdicts; i++) {
2460 status = cairo_cff_font_write_local_sub (
2463 font->fd_private_dict[font->fd_subset_map[i]],
2464 &font->fd_local_sub_index[font->fd_subset_map[i]],
2465 font->fd_local_subs_used[font->fd_subset_map[i]]);
2466 if (unlikely (status))
2470 status = cairo_cff_font_write_private_dict (font,
2473 font->private_dict);
2474 if (unlikely (status))
2477 status = cairo_cff_font_write_local_sub (font,
2480 &font->local_sub_index,
2481 font->local_subs_used);
2482 if (unlikely (status))
2486 return CAIRO_STATUS_SUCCESS;
2489 static cairo_status_t
2490 cairo_cff_font_write_type1_private_dict_and_local_sub (cairo_cff_font_t *font)
2492 cairo_int_status_t status;
2494 status = cairo_cff_font_write_private_dict (font,
2497 font->private_dict);
2498 if (unlikely (status))
2501 status = cairo_cff_font_write_local_sub (font,
2504 &font->local_sub_index,
2505 font->local_subs_used);
2506 if (unlikely (status))
2509 return CAIRO_STATUS_SUCCESS;
2513 typedef cairo_status_t
2514 (*font_write_t) (cairo_cff_font_t *font);
2516 static const font_write_t font_write_cid_funcs[] = {
2517 cairo_cff_font_write_header,
2518 cairo_cff_font_write_name,
2519 cairo_cff_font_write_top_dict,
2520 cairo_cff_font_write_strings,
2521 cairo_cff_font_write_global_subrs,
2522 cairo_cff_font_write_cid_charset,
2523 cairo_cff_font_write_fdselect,
2524 cairo_cff_font_write_charstrings,
2525 cairo_cff_font_write_cid_fontdict,
2526 cairo_cff_font_write_cid_private_dict_and_local_sub,
2529 static const font_write_t font_write_type1_funcs[] = {
2530 cairo_cff_font_write_header,
2531 cairo_cff_font_write_name,
2532 cairo_cff_font_write_top_dict,
2533 cairo_cff_font_write_strings,
2534 cairo_cff_font_write_global_subrs,
2535 cairo_cff_font_write_encoding,
2536 cairo_cff_font_write_type1_charset,
2537 cairo_cff_font_write_charstrings,
2538 cairo_cff_font_write_type1_private_dict_and_local_sub,
2541 static cairo_status_t
2542 cairo_cff_font_write_subset (cairo_cff_font_t *font)
2544 cairo_int_status_t status;
2547 if (font->scaled_font_subset->is_latin) {
2548 for (i = 0; i < ARRAY_LENGTH (font_write_type1_funcs); i++) {
2549 status = font_write_type1_funcs[i] (font);
2550 if (unlikely (status))
2554 for (i = 0; i < ARRAY_LENGTH (font_write_cid_funcs); i++) {
2555 status = font_write_cid_funcs[i] (font);
2556 if (unlikely (status))
2561 return CAIRO_STATUS_SUCCESS;
2564 static cairo_int_status_t
2565 cairo_cff_font_generate (cairo_cff_font_t *font,
2567 unsigned long *length)
2569 cairo_int_status_t status;
2571 status = cairo_cff_font_read_font (font);
2572 if (unlikely (status))
2575 /* If the PS name is not found, create a CairoFont-x-y name. */
2576 if (font->ps_name == NULL) {
2577 font->ps_name = malloc (30);
2578 if (unlikely (font->ps_name == NULL))
2579 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2581 snprintf(font->ps_name, 30, "CairoFont-%u-%u",
2582 font->scaled_font_subset->font_id,
2583 font->scaled_font_subset->subset_id);
2586 status = cairo_cff_font_subset_font (font);
2587 if (unlikely (status))
2590 status = cairo_cff_font_write_subset (font);
2591 if (unlikely (status))
2595 *data = _cairo_array_index (&font->output, 0);
2596 *length = _cairo_array_num_elements (&font->output);
2598 return CAIRO_STATUS_SUCCESS;
2601 static cairo_int_status_t
2602 cairo_cff_font_create_set_widths (cairo_cff_font_t *font)
2605 unsigned long long_entry_size;
2606 unsigned long short_entry_size;
2610 unsigned char buf[10];
2612 cairo_int_status_t status;
2614 size = sizeof (tt_hhea_t);
2615 status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2617 (unsigned char*) &hhea, &size);
2618 if (unlikely (status))
2620 num_hmetrics = be16_to_cpu (hhea.num_hmetrics);
2622 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
2623 glyph_index = font->scaled_font_subset->glyphs[i];
2624 long_entry_size = 2 * sizeof (int16_t);
2625 short_entry_size = sizeof (int16_t);
2626 if (glyph_index < num_hmetrics) {
2627 status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2629 glyph_index * long_entry_size,
2630 buf, &short_entry_size);
2631 if (unlikely (status))
2636 status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2638 (num_hmetrics - 1) * long_entry_size,
2639 buf, &short_entry_size);
2640 if (unlikely (status))
2643 font->widths[i] = be16_to_cpu (*((int16_t*)buf));
2646 return CAIRO_STATUS_SUCCESS;
2650 check_fontdata_is_cff (const unsigned char *data, long length)
2652 cff_header_t *header;
2654 if (length < (long)sizeof (cff_header_t))
2657 header = (cff_header_t *) data;
2658 if (header->major == 1 &&
2659 header->minor == 0 &&
2660 header->header_size == 4)
2668 static cairo_int_status_t
2669 _cairo_cff_font_load_opentype_cff (cairo_cff_font_t *font)
2671 const cairo_scaled_font_backend_t *backend = font->backend;
2672 cairo_status_t status;
2675 unsigned long size, data_length;
2677 if (!backend->load_truetype_table)
2678 return CAIRO_INT_STATUS_UNSUPPORTED;
2681 status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2682 TT_TAG_CFF, 0, NULL, &data_length);
2686 size = sizeof (tt_head_t);
2687 status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2689 (unsigned char *) &head, &size);
2690 if (unlikely (status))
2693 size = sizeof (tt_hhea_t);
2694 status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2696 (unsigned char *) &hhea, &size);
2697 if (unlikely (status))
2701 status = backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2702 TT_TAG_hmtx, 0, NULL, &size);
2703 if (unlikely (status))
2706 font->x_min = (int16_t) be16_to_cpu (head.x_min);
2707 font->y_min = (int16_t) be16_to_cpu (head.y_min);
2708 font->x_max = (int16_t) be16_to_cpu (head.x_max);
2709 font->y_max = (int16_t) be16_to_cpu (head.y_max);
2710 font->ascent = (int16_t) be16_to_cpu (hhea.ascender);
2711 font->descent = (int16_t) be16_to_cpu (hhea.descender);
2712 font->units_per_em = (int16_t) be16_to_cpu (head.units_per_em);
2713 if (font->units_per_em == 0)
2714 font->units_per_em = 1000;
2716 font->font_name = NULL;
2717 status = _cairo_truetype_read_font_name (font->scaled_font_subset->scaled_font,
2720 if (_cairo_status_is_error (status))
2723 font->is_opentype = TRUE;
2724 font->data_length = data_length;
2725 font->data = malloc (data_length);
2726 if (unlikely (font->data == NULL))
2727 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2729 status = font->backend->load_truetype_table (font->scaled_font_subset->scaled_font,
2730 TT_TAG_CFF, 0, font->data,
2731 &font->data_length);
2732 if (unlikely (status))
2735 if (!check_fontdata_is_cff (font->data, data_length))
2736 return CAIRO_INT_STATUS_UNSUPPORTED;
2738 return CAIRO_STATUS_SUCCESS;
2741 static cairo_int_status_t
2742 _cairo_cff_font_load_cff (cairo_cff_font_t *font)
2744 const cairo_scaled_font_backend_t *backend = font->backend;
2745 cairo_status_t status;
2746 unsigned long data_length;
2748 if (!backend->load_type1_data)
2749 return CAIRO_INT_STATUS_UNSUPPORTED;
2752 status = backend->load_type1_data (font->scaled_font_subset->scaled_font,
2753 0, NULL, &data_length);
2754 if (unlikely (status))
2757 font->font_name = NULL;
2758 font->is_opentype = FALSE;
2759 font->data_length = data_length;
2760 font->data = malloc (data_length);
2761 if (unlikely (font->data == NULL))
2762 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2764 status = font->backend->load_type1_data (font->scaled_font_subset->scaled_font,
2765 0, font->data, &font->data_length);
2766 if (unlikely (status))
2769 if (!check_fontdata_is_cff (font->data, data_length))
2770 return CAIRO_INT_STATUS_UNSUPPORTED;
2772 return CAIRO_STATUS_SUCCESS;
2775 static cairo_int_status_t
2776 _cairo_cff_font_create (cairo_scaled_font_subset_t *scaled_font_subset,
2777 cairo_cff_font_t **font_return,
2778 const char *subset_name)
2780 const cairo_scaled_font_backend_t *backend;
2781 cairo_int_status_t status;
2782 cairo_cff_font_t *font;
2784 backend = scaled_font_subset->scaled_font->backend;
2786 /* We need to use a fallback font generated from the synthesized outlines. */
2787 if (backend->is_synthetic && backend->is_synthetic (scaled_font_subset->scaled_font))
2788 return CAIRO_INT_STATUS_UNSUPPORTED;
2790 font = calloc (1, sizeof (cairo_cff_font_t));
2791 if (unlikely (font == NULL))
2792 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
2794 font->backend = backend;
2795 font->scaled_font_subset = scaled_font_subset;
2797 status = _cairo_cff_font_load_opentype_cff (font);
2798 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
2799 status = _cairo_cff_font_load_cff (font);
2803 font->data_end = font->data + font->data_length;
2804 _cairo_array_init (&font->output, sizeof (char));
2805 status = _cairo_array_grow_by (&font->output, 4096);
2806 if (unlikely (status))
2809 font->subset_font_name = strdup (subset_name);
2810 if (unlikely (font->subset_font_name == NULL)) {
2811 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2815 font->widths = calloc (font->scaled_font_subset->num_glyphs, sizeof (int));
2816 if (unlikely (font->widths == NULL)) {
2817 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2821 if (font->is_opentype) {
2822 status = cairo_cff_font_create_set_widths (font);
2823 if (unlikely (status))
2827 status = cff_dict_init (&font->top_dict);
2828 if (unlikely (status))
2831 status = cff_dict_init (&font->private_dict);
2832 if (unlikely (status))
2835 cff_index_init (&font->strings_index);
2836 cff_index_init (&font->charstrings_index);
2837 cff_index_init (&font->global_sub_index);
2838 cff_index_init (&font->local_sub_index);
2839 cff_index_init (&font->charstrings_subset_index);
2840 cff_index_init (&font->strings_subset_index);
2842 font->fdselect = NULL;
2843 font->fd_dict = NULL;
2844 font->fd_private_dict = NULL;
2845 font->fd_local_sub_index = NULL;
2846 font->fd_local_sub_bias = NULL;
2847 font->fdselect_subset = NULL;
2848 font->fd_subset_map = NULL;
2849 font->private_dict_offset = NULL;
2850 font->global_subs_used = NULL;
2851 font->local_subs_used = NULL;
2852 font->fd_local_subs_used = NULL;
2854 *font_return = font;
2856 return CAIRO_STATUS_SUCCESS;
2859 _cairo_hash_table_destroy (font->top_dict);
2861 free (font->widths);
2863 free (font->subset_font_name);
2865 free (font->ps_name);
2866 _cairo_array_fini (&font->output);
2869 free (font->font_name);
2876 cairo_cff_font_destroy (cairo_cff_font_t *font)
2880 free (font->widths);
2881 free (font->font_name);
2882 free (font->ps_name);
2883 free (font->subset_font_name);
2884 _cairo_array_fini (&font->output);
2885 cff_dict_fini (font->top_dict);
2886 cff_dict_fini (font->private_dict);
2887 cff_index_fini (&font->strings_index);
2888 cff_index_fini (&font->charstrings_index);
2889 cff_index_fini (&font->global_sub_index);
2890 cff_index_fini (&font->local_sub_index);
2891 cff_index_fini (&font->charstrings_subset_index);
2892 cff_index_fini (&font->strings_subset_index);
2894 /* If we bailed out early as a result of an error some of the
2895 * following cairo_cff_font_t members may still be NULL */
2896 if (font->fd_dict) {
2897 for (i = 0; i < font->num_fontdicts; i++) {
2898 if (font->fd_dict[i])
2899 cff_dict_fini (font->fd_dict[i]);
2901 free (font->fd_dict);
2903 free (font->global_subs_used);
2904 free (font->local_subs_used);
2905 free (font->fd_subset_map);
2906 free (font->private_dict_offset);
2909 free (font->fdselect);
2910 free (font->fdselect_subset);
2911 if (font->fd_private_dict) {
2912 for (i = 0; i < font->num_fontdicts; i++) {
2913 if (font->fd_private_dict[i])
2914 cff_dict_fini (font->fd_private_dict[i]);
2916 free (font->fd_private_dict);
2918 if (font->fd_local_sub_index) {
2919 for (i = 0; i < font->num_fontdicts; i++)
2920 cff_index_fini (&font->fd_local_sub_index[i]);
2921 free (font->fd_local_sub_index);
2923 free (font->fd_local_sub_bias);
2924 if (font->fd_local_subs_used) {
2925 for (i = 0; i < font->num_fontdicts; i++) {
2926 free (font->fd_local_subs_used[i]);
2928 free (font->fd_local_subs_used);
2930 free (font->fd_default_width);
2931 free (font->fd_nominal_width);
2940 _cairo_cff_subset_init (cairo_cff_subset_t *cff_subset,
2941 const char *subset_name,
2942 cairo_scaled_font_subset_t *font_subset)
2944 cairo_cff_font_t *font = NULL; /* squelch bogus compiler warning */
2945 cairo_status_t status;
2946 const char *data = NULL; /* squelch bogus compiler warning */
2947 unsigned long length = 0; /* squelch bogus compiler warning */
2950 status = _cairo_cff_font_create (font_subset, &font, subset_name);
2951 if (unlikely (status))
2954 status = cairo_cff_font_generate (font, &data, &length);
2955 if (unlikely (status))
2958 cff_subset->ps_name = strdup (font->ps_name);
2959 if (unlikely (cff_subset->ps_name == NULL)) {
2960 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2964 if (font->font_name) {
2965 cff_subset->family_name_utf8 = strdup (font->font_name);
2966 if (cff_subset->family_name_utf8 == NULL) {
2967 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2971 cff_subset->family_name_utf8 = NULL;
2974 cff_subset->widths = calloc (sizeof (double), font->scaled_font_subset->num_glyphs);
2975 if (unlikely (cff_subset->widths == NULL)) {
2976 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2979 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++)
2980 cff_subset->widths[i] = (double)font->widths[i]/font->units_per_em;
2982 cff_subset->x_min = (double)font->x_min/font->units_per_em;
2983 cff_subset->y_min = (double)font->y_min/font->units_per_em;
2984 cff_subset->x_max = (double)font->x_max/font->units_per_em;
2985 cff_subset->y_max = (double)font->y_max/font->units_per_em;
2986 cff_subset->ascent = (double)font->ascent/font->units_per_em;
2987 cff_subset->descent = (double)font->descent/font->units_per_em;
2989 cff_subset->data = malloc (length);
2990 if (unlikely (cff_subset->data == NULL)) {
2991 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2995 memcpy (cff_subset->data, data, length);
2996 cff_subset->data_length = length;
2998 cairo_cff_font_destroy (font);
3000 return CAIRO_STATUS_SUCCESS;
3003 free (cff_subset->widths);
3005 free (cff_subset->family_name_utf8);
3007 free (cff_subset->ps_name);
3009 cairo_cff_font_destroy (font);
3015 _cairo_cff_subset_fini (cairo_cff_subset_t *subset)
3017 free (subset->ps_name);
3018 free (subset->family_name_utf8);
3019 free (subset->widths);
3020 free (subset->data);
3024 _cairo_cff_scaled_font_is_cid_cff (cairo_scaled_font_t *scaled_font)
3026 const cairo_scaled_font_backend_t *backend;
3027 cairo_int_status_t status;
3028 unsigned char *data;
3029 unsigned long data_length;
3030 unsigned char *current_ptr;
3031 unsigned char *data_end;
3032 cff_header_t *header;
3033 cff_index_element_t *element;
3034 cairo_hash_table_t *top_dict;
3035 cairo_array_t index;
3037 cairo_bool_t is_cid = FALSE;
3039 backend = scaled_font->backend;
3042 status = CAIRO_INT_STATUS_UNSUPPORTED;
3043 /* Try to load an OpenType/CFF font */
3044 if (backend->load_truetype_table &&
3045 (status = backend->load_truetype_table (scaled_font, TT_TAG_CFF,
3046 0, NULL, &data_length)) == CAIRO_INT_STATUS_SUCCESS)
3048 data = malloc (data_length);
3049 if (unlikely (data == NULL)) {
3050 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3054 status = backend->load_truetype_table (scaled_font, TT_TAG_CFF,
3055 0, data, &data_length);
3056 if (unlikely (status))
3059 /* Try to load a CFF font */
3060 if (status == CAIRO_INT_STATUS_UNSUPPORTED &&
3061 backend->load_type1_data &&
3062 (status = backend->load_type1_data (scaled_font,
3063 0, NULL, &data_length)) == CAIRO_INT_STATUS_SUCCESS)
3065 data = malloc (data_length);
3066 if (unlikely (data == NULL)) {
3067 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3071 status = backend->load_type1_data (scaled_font, 0, data, &data_length);
3072 if (unlikely (status))
3078 /* Check if it looks like a CFF font */
3079 if (!check_fontdata_is_cff (data, data_length))
3082 data_end = data + data_length;
3085 if (data_length < sizeof (cff_header_t))
3088 header = (cff_header_t *) data;
3089 current_ptr = data + header->header_size;
3092 cff_index_init (&index);
3093 status = cff_index_read (&index, ¤t_ptr, data_end);
3094 cff_index_fini (&index);
3100 cff_index_init (&index);
3101 status = cff_index_read (&index, ¤t_ptr, data_end);
3102 if (unlikely (status))
3105 status = cff_dict_init (&top_dict);
3106 if (unlikely (status))
3109 element = _cairo_array_index (&index, 0);
3110 status = cff_dict_read (top_dict, element->data, element->length);
3111 if (unlikely (status))
3114 /* check for ROS operator indicating a CID font */
3115 if (cff_dict_get_operands (top_dict, ROS_OP, &size) != NULL)
3119 cff_dict_fini (top_dict);
3122 cff_index_fini (&index);
3130 static cairo_int_status_t
3131 _cairo_cff_font_fallback_create (cairo_scaled_font_subset_t *scaled_font_subset,
3132 cairo_cff_font_t **font_return,
3133 const char *subset_name)
3135 cairo_status_t status;
3136 cairo_cff_font_t *font;
3138 font = malloc (sizeof (cairo_cff_font_t));
3139 if (unlikely (font == NULL))
3140 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3142 font->backend = NULL;
3143 font->scaled_font_subset = scaled_font_subset;
3145 _cairo_array_init (&font->output, sizeof (char));
3146 status = _cairo_array_grow_by (&font->output, 4096);
3147 if (unlikely (status))
3150 font->subset_font_name = strdup (subset_name);
3151 if (unlikely (font->subset_font_name == NULL)) {
3152 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3156 font->ps_name = strdup (subset_name);
3157 if (unlikely (font->ps_name == NULL)) {
3158 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3161 font->font_name = NULL;
3170 font->widths = calloc (font->scaled_font_subset->num_glyphs, sizeof (int));
3171 if (unlikely (font->widths == NULL)) {
3172 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3176 font->data_length = 0;
3178 font->data_end = NULL;
3180 status = cff_dict_init (&font->top_dict);
3181 if (unlikely (status))
3184 status = cff_dict_init (&font->private_dict);
3185 if (unlikely (status))
3188 cff_index_init (&font->strings_index);
3189 cff_index_init (&font->charstrings_index);
3190 cff_index_init (&font->global_sub_index);
3191 cff_index_init (&font->local_sub_index);
3192 cff_index_init (&font->charstrings_subset_index);
3193 cff_index_init (&font->strings_subset_index);
3194 font->global_subs_used = NULL;
3195 font->local_subs_used = NULL;
3196 font->subset_subroutines = FALSE;
3197 font->fdselect = NULL;
3198 font->fd_dict = NULL;
3199 font->fd_private_dict = NULL;
3200 font->fd_local_sub_index = NULL;
3201 font->fdselect_subset = NULL;
3202 font->fd_subset_map = NULL;
3203 font->private_dict_offset = NULL;
3205 *font_return = font;
3207 return CAIRO_STATUS_SUCCESS;
3210 _cairo_hash_table_destroy (font->top_dict);
3212 free (font->widths);
3214 free (font->font_name);
3215 free (font->ps_name);
3217 free (font->subset_font_name);
3219 _cairo_array_fini (&font->output);
3224 static cairo_int_status_t
3225 cairo_cff_font_fallback_generate (cairo_cff_font_t *font,
3226 cairo_type2_charstrings_t *type2_subset,
3228 unsigned long *length)
3230 cairo_int_status_t status;
3231 cff_header_t header;
3232 cairo_array_t *charstring;
3233 unsigned char buf[40];
3234 unsigned char *end_buf, *end_buf2;
3241 header.header_size = 4;
3242 header.offset_size = 4;
3243 font->header = &header;
3245 /* Create Top Dict */
3246 font->is_cid = FALSE;
3248 snprintf((char*)buf, sizeof(buf), "CairoFont-%u-%u",
3249 font->scaled_font_subset->font_id,
3250 font->scaled_font_subset->subset_id);
3251 sid = NUM_STD_STRINGS + _cairo_array_num_elements (&font->strings_subset_index);
3252 status = cff_index_append_copy (&font->strings_subset_index,
3253 (unsigned char *)buf,
3254 strlen((char*)buf));
3255 if (unlikely (status))
3258 end_buf = encode_integer (buf, sid);
3259 status = cff_dict_set_operands (font->top_dict, FULLNAME_OP,
3260 buf, end_buf - buf);
3261 if (unlikely (status))
3264 status = cff_dict_set_operands (font->top_dict, FAMILYNAME_OP,
3265 buf, end_buf - buf);
3266 if (unlikely (status))
3269 end_buf = encode_integer (buf, type2_subset->x_min);
3270 end_buf = encode_integer (end_buf, type2_subset->y_min);
3271 end_buf = encode_integer (end_buf, type2_subset->x_max);
3272 end_buf = encode_integer (end_buf, type2_subset->y_max);
3273 status = cff_dict_set_operands (font->top_dict,
3274 FONTBBOX_OP, buf, end_buf - buf);
3275 if (unlikely (status))
3278 end_buf = encode_integer_max (buf, 0);
3279 status = cff_dict_set_operands (font->top_dict,
3280 CHARSTRINGS_OP, buf, end_buf - buf);
3281 if (unlikely (status))
3285 if (font->scaled_font_subset->is_latin) {
3286 status = cff_dict_set_operands (font->top_dict,
3287 ENCODING_OP, buf, end_buf - buf);
3288 if (unlikely (status))
3291 /* Private has two operands - size and offset */
3292 end_buf2 = encode_integer_max (end_buf, 0);
3293 cff_dict_set_operands (font->top_dict, PRIVATE_OP, buf, end_buf2 - buf);
3294 if (unlikely (status))
3298 status = cff_dict_set_operands (font->top_dict,
3299 FDSELECT_OP, buf, end_buf - buf);
3300 if (unlikely (status))
3303 status = cff_dict_set_operands (font->top_dict,
3304 FDARRAY_OP, buf, end_buf - buf);
3305 if (unlikely (status))
3309 status = cff_dict_set_operands (font->top_dict,
3310 CHARSET_OP, buf, end_buf - buf);
3311 if (unlikely (status))
3314 if (!font->scaled_font_subset->is_latin) {
3315 status = cairo_cff_font_set_ros_strings (font);
3316 if (unlikely (status))
3319 /* Create CID FD dictionary */
3320 status = cairo_cff_font_create_cid_fontdict (font);
3321 if (unlikely (status))
3324 font->private_dict_offset = malloc (sizeof (int));
3325 if (unlikely (font->private_dict_offset == NULL))
3326 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3329 /* Create charstrings */
3330 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
3331 charstring = _cairo_array_index(&type2_subset->charstrings, i);
3333 status = cff_index_append (&font->charstrings_subset_index,
3334 _cairo_array_index (charstring, 0),
3335 _cairo_array_num_elements (charstring));
3337 if (unlikely (status))
3341 if (font->scaled_font_subset->is_latin)
3342 status = cairo_cff_font_add_euro_charset_string (font);
3344 status = cairo_cff_font_write_subset (font);
3345 if (unlikely (status))
3348 *data = _cairo_array_index (&font->output, 0);
3349 *length = _cairo_array_num_elements (&font->output);
3351 return CAIRO_STATUS_SUCCESS;
3355 _cairo_cff_fallback_init (cairo_cff_subset_t *cff_subset,
3356 const char *subset_name,
3357 cairo_scaled_font_subset_t *font_subset)
3359 cairo_cff_font_t *font = NULL; /* squelch bogus compiler warning */
3360 cairo_status_t status;
3361 const char *data = NULL; /* squelch bogus compiler warning */
3362 unsigned long length = 0; /* squelch bogus compiler warning */
3364 cairo_type2_charstrings_t type2_subset;
3366 status = _cairo_cff_font_fallback_create (font_subset, &font, subset_name);
3367 if (unlikely (status))
3370 status = _cairo_type2_charstrings_init (&type2_subset, font_subset);
3371 if (unlikely (status))
3374 status = cairo_cff_font_fallback_generate (font, &type2_subset, &data, &length);
3375 if (unlikely (status))
3378 cff_subset->family_name_utf8 = NULL;
3379 cff_subset->ps_name = strdup (font->ps_name);
3380 if (unlikely (cff_subset->ps_name == NULL)) {
3381 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3385 cff_subset->widths = calloc (sizeof (double), font->scaled_font_subset->num_glyphs);
3386 if (unlikely (cff_subset->widths == NULL)) {
3387 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3391 for (i = 0; i < font->scaled_font_subset->num_glyphs; i++)
3392 cff_subset->widths[i] = (double)type2_subset.widths[i]/1000;
3394 cff_subset->x_min = (double)type2_subset.x_min/1000;
3395 cff_subset->y_min = (double)type2_subset.y_min/1000;
3396 cff_subset->x_max = (double)type2_subset.x_max/1000;
3397 cff_subset->y_max = (double)type2_subset.y_max/1000;
3398 cff_subset->ascent = (double)type2_subset.y_max/1000;
3399 cff_subset->descent = (double)type2_subset.y_min/1000;
3401 cff_subset->data = malloc (length);
3402 if (unlikely (cff_subset->data == NULL)) {
3403 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
3407 memcpy (cff_subset->data, data, length);
3408 cff_subset->data_length = length;
3410 _cairo_type2_charstrings_fini (&type2_subset);
3411 cairo_cff_font_destroy (font);
3413 return CAIRO_STATUS_SUCCESS;
3416 free (cff_subset->widths);
3418 free (cff_subset->ps_name);
3420 _cairo_type2_charstrings_fini (&type2_subset);
3422 cairo_cff_font_destroy (font);
3428 _cairo_cff_fallback_fini (cairo_cff_subset_t *subset)
3430 free (subset->ps_name);
3431 free (subset->widths);
3432 free (subset->data);
3435 #endif /* CAIRO_HAS_FONT_SUBSET */