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 const char *decimal_point
;
299 int decimal_point_len
;
305 char *buf_end
= buffer
+ sizeof (buffer
);
307 decimal_point
= cairo_get_locale_decimal_point ();
308 decimal_point_len
= strlen (decimal_point
);
310 assert (decimal_point_len
!= 0);
311 assert (sizeof(buffer
) + decimal_point_len
< sizeof(buffer2
));
314 while (buf
+ 2 < buf_end
) {
316 buf
= decode_nibble (n
, buf
);
318 buf
= decode_nibble (n
, buf
);
319 if ((*p
& 0x0f) == 0x0f) {
328 if (strchr (buffer
, '.')) {
329 q
= strchr (buffer
, '.');
330 strncpy (buffer2
, buffer
, q
- buffer
);
331 buf
= buffer2
+ (q
- buffer
);
332 strncpy (buf
, decimal_point
, decimal_point_len
);
333 buf
+= decimal_point_len
;
338 if (sscanf(buf
, "%lf", real
) != 1)
344 static unsigned char *
345 decode_number (unsigned char *p
, double *number
)
348 p
= decode_real (p
, number
);
351 p
= decode_integer (p
, &i
);
357 static unsigned char *
358 decode_operator (unsigned char *p
, unsigned short *operator)
360 unsigned short op
= 0;
371 /* return 0 if not an operand */
373 operand_length (unsigned char *p
)
375 unsigned char *begin
= p
;
383 if (*p
>= 32 && *p
<= 246)
386 if (*p
>= 247 && *p
<= 254)
390 while ((*p
& 0x0f) != 0x0f)
392 return p
- begin
+ 1;
398 static unsigned char *
399 encode_index_offset (unsigned char *p
, int offset_size
, unsigned long offset
)
401 while (--offset_size
>= 0) {
402 p
[offset_size
] = (unsigned char) (offset
& 0xff);
405 return p
+ offset_size
;
409 decode_index_offset(unsigned char *p
, int off_size
)
411 unsigned long offset
= 0;
413 while (off_size
-- > 0)
414 offset
= offset
*256 + *p
++;
419 cff_index_init (cairo_array_t
*index
)
421 _cairo_array_init (index
, sizeof (cff_index_element_t
));
424 static cairo_int_status_t
425 cff_index_read (cairo_array_t
*index
, unsigned char **ptr
, unsigned char *end_ptr
)
427 cff_index_element_t element
;
428 unsigned char *data
, *p
;
429 cairo_status_t status
;
430 int offset_size
, count
, start
, i
;
435 return CAIRO_INT_STATUS_UNSUPPORTED
;
436 count
= get_unaligned_be16 (p
);
440 if (p
+ (count
+ 1)*offset_size
> end_ptr
)
441 return CAIRO_INT_STATUS_UNSUPPORTED
;
442 data
= p
+ offset_size
*(count
+ 1) - 1;
443 start
= decode_index_offset (p
, offset_size
);
445 for (i
= 0; i
< count
; i
++) {
446 end
= decode_index_offset (p
, offset_size
);
449 return CAIRO_INT_STATUS_UNSUPPORTED
;
450 element
.length
= end
- start
;
451 element
.is_copy
= FALSE
;
452 element
.data
= data
+ start
;
453 status
= _cairo_array_append (index
, &element
);
454 if (unlikely (status
))
462 return CAIRO_STATUS_SUCCESS
;
465 static cairo_status_t
466 cff_index_write (cairo_array_t
*index
, cairo_array_t
*output
)
472 cff_index_element_t
*element
;
474 unsigned char buf
[5];
475 cairo_status_t status
;
477 num_elem
= _cairo_array_num_elements (index
);
478 count
= cpu_to_be16 ((uint16_t) num_elem
);
479 status
= _cairo_array_append_multiple (output
, &count
, 2);
480 if (unlikely (status
))
484 return CAIRO_STATUS_SUCCESS
;
486 /* Find maximum offset to determine offset size */
488 for (i
= 0; i
< num_elem
; i
++) {
489 element
= _cairo_array_index (index
, i
);
490 offset
+= element
->length
;
494 else if (offset
< 0x10000)
496 else if (offset
< 0x1000000)
501 buf
[0] = (unsigned char) offset_size
;
502 status
= _cairo_array_append (output
, buf
);
503 if (unlikely (status
))
507 encode_index_offset (buf
, offset_size
, offset
);
508 status
= _cairo_array_append_multiple (output
, buf
, offset_size
);
509 if (unlikely (status
))
512 for (i
= 0; i
< num_elem
; i
++) {
513 element
= _cairo_array_index (index
, i
);
514 offset
+= element
->length
;
515 encode_index_offset (buf
, offset_size
, offset
);
516 status
= _cairo_array_append_multiple (output
, buf
, offset_size
);
517 if (unlikely (status
))
521 for (i
= 0; i
< num_elem
; i
++) {
522 element
= _cairo_array_index (index
, i
);
523 if (element
->length
> 0) {
524 status
= _cairo_array_append_multiple (output
,
528 if (unlikely (status
))
531 return CAIRO_STATUS_SUCCESS
;
535 cff_index_set_object (cairo_array_t
*index
, int obj_index
,
536 unsigned char *object
, int length
)
538 cff_index_element_t
*element
;
540 element
= _cairo_array_index (index
, obj_index
);
541 if (element
->is_copy
)
542 free (element
->data
);
544 element
->data
= object
;
545 element
->length
= length
;
546 element
->is_copy
= FALSE
;
549 static cairo_status_t
550 cff_index_append (cairo_array_t
*index
, unsigned char *object
, int length
)
552 cff_index_element_t element
;
554 element
.length
= length
;
555 element
.is_copy
= FALSE
;
556 element
.data
= object
;
558 return _cairo_array_append (index
, &element
);
561 static cairo_status_t
562 cff_index_append_copy (cairo_array_t
*index
,
563 const unsigned char *object
,
566 cff_index_element_t element
;
567 cairo_status_t status
;
569 element
.length
= length
;
570 element
.is_copy
= TRUE
;
571 element
.data
= malloc (element
.length
);
572 if (unlikely (element
.data
== NULL
))
573 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
575 memcpy (element
.data
, object
, element
.length
);
577 status
= _cairo_array_append (index
, &element
);
578 if (unlikely (status
)) {
583 return CAIRO_STATUS_SUCCESS
;
587 cff_index_fini (cairo_array_t
*index
)
589 cff_index_element_t
*element
;
592 for (i
= 0; i
< _cairo_array_num_elements (index
); i
++) {
593 element
= _cairo_array_index (index
, i
);
594 if (element
->is_copy
&& element
->data
)
595 free (element
->data
);
597 _cairo_array_fini (index
);
601 _cairo_cff_dict_equal (const void *key_a
, const void *key_b
)
603 const cff_dict_operator_t
*op_a
= key_a
;
604 const cff_dict_operator_t
*op_b
= key_b
;
606 return op_a
->operator == op_b
->operator;
609 static cairo_status_t
610 cff_dict_init (cairo_hash_table_t
**dict
)
612 *dict
= _cairo_hash_table_create (_cairo_cff_dict_equal
);
613 if (unlikely (*dict
== NULL
))
614 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
616 return CAIRO_STATUS_SUCCESS
;
620 _cairo_dict_init_key (cff_dict_operator_t
*key
, int operator)
622 key
->base
.hash
= (unsigned long) operator;
623 key
->operator = operator;
626 static cairo_status_t
627 cff_dict_create_operator (int operator,
628 unsigned char *operand
,
630 cff_dict_operator_t
**out
)
632 cff_dict_operator_t
*op
;
634 op
= malloc (sizeof (cff_dict_operator_t
));
635 if (unlikely (op
== NULL
))
636 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
638 _cairo_dict_init_key (op
, operator);
639 op
->operand
= malloc (size
);
640 if (unlikely (op
->operand
== NULL
)) {
642 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
645 memcpy (op
->operand
, operand
, size
);
646 op
->operand_length
= size
;
647 op
->operand_offset
= -1;
650 return CAIRO_STATUS_SUCCESS
;
653 static cairo_status_t
654 cff_dict_read (cairo_hash_table_t
*dict
, unsigned char *p
, int dict_size
)
657 cairo_array_t operands
;
658 cff_dict_operator_t
*op
;
659 unsigned short operator;
660 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
664 _cairo_array_init (&operands
, 1);
666 size
= operand_length (p
);
668 status
= _cairo_array_append_multiple (&operands
, p
, size
);
669 if (unlikely (status
))
674 p
= decode_operator (p
, &operator);
675 status
= cff_dict_create_operator (operator,
676 _cairo_array_index (&operands
, 0),
677 _cairo_array_num_elements (&operands
),
679 if (unlikely (status
))
682 status
= _cairo_hash_table_insert (dict
, &op
->base
);
683 if (unlikely (status
))
686 _cairo_array_truncate (&operands
, 0);
691 _cairo_array_fini (&operands
);
697 cff_dict_remove (cairo_hash_table_t
*dict
, unsigned short operator)
699 cff_dict_operator_t key
, *op
;
701 _cairo_dict_init_key (&key
, operator);
702 op
= _cairo_hash_table_lookup (dict
, &key
.base
);
705 _cairo_hash_table_remove (dict
, (cairo_hash_entry_t
*) op
);
710 static unsigned char *
711 cff_dict_get_operands (cairo_hash_table_t
*dict
,
712 unsigned short operator,
715 cff_dict_operator_t key
, *op
;
717 _cairo_dict_init_key (&key
, operator);
718 op
= _cairo_hash_table_lookup (dict
, &key
.base
);
720 *size
= op
->operand_length
;
727 static cairo_status_t
728 cff_dict_set_operands (cairo_hash_table_t
*dict
,
729 unsigned short operator,
730 unsigned char *operand
,
733 cff_dict_operator_t key
, *op
;
734 cairo_status_t status
;
736 _cairo_dict_init_key (&key
, operator);
737 op
= _cairo_hash_table_lookup (dict
, &key
.base
);
740 op
->operand
= malloc (size
);
741 if (unlikely (op
->operand
== NULL
))
742 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
744 memcpy (op
->operand
, operand
, size
);
745 op
->operand_length
= size
;
749 status
= cff_dict_create_operator (operator, operand
, size
, &op
);
750 if (unlikely (status
))
753 status
= _cairo_hash_table_insert (dict
, &op
->base
);
754 if (unlikely (status
))
758 return CAIRO_STATUS_SUCCESS
;
762 cff_dict_get_location (cairo_hash_table_t
*dict
,
763 unsigned short operator,
766 cff_dict_operator_t key
, *op
;
768 _cairo_dict_init_key (&key
, operator);
769 op
= _cairo_hash_table_lookup (dict
, &key
.base
);
771 *size
= op
->operand_length
;
772 return op
->operand_offset
;
778 typedef struct _dict_write_info
{
779 cairo_array_t
*output
;
780 cairo_status_t status
;
784 cairo_dict_write_operator (cff_dict_operator_t
*op
, dict_write_info_t
*write_info
)
788 op
->operand_offset
= _cairo_array_num_elements (write_info
->output
);
789 write_info
->status
= _cairo_array_append_multiple (write_info
->output
, op
->operand
, op
->operand_length
);
790 if (write_info
->status
)
793 if (op
->operator & 0xff00) {
794 data
= op
->operator >> 8;
795 write_info
->status
= _cairo_array_append (write_info
->output
, &data
);
796 if (write_info
->status
)
799 data
= op
->operator & 0xff;
800 write_info
->status
= _cairo_array_append (write_info
->output
, &data
);
804 _cairo_dict_collect (void *entry
, void *closure
)
806 dict_write_info_t
*write_info
= closure
;
807 cff_dict_operator_t
*op
= entry
;
809 if (write_info
->status
)
812 /* The ROS operator is handled separately in cff_dict_write() */
813 if (op
->operator != ROS_OP
)
814 cairo_dict_write_operator (op
, write_info
);
817 static cairo_status_t
818 cff_dict_write (cairo_hash_table_t
*dict
, cairo_array_t
*output
)
820 dict_write_info_t write_info
;
821 cff_dict_operator_t key
, *op
;
823 write_info
.output
= output
;
824 write_info
.status
= CAIRO_STATUS_SUCCESS
;
826 /* The CFF specification requires that the Top Dict of CID fonts
827 * begin with the ROS operator. */
828 _cairo_dict_init_key (&key
, ROS_OP
);
829 op
= _cairo_hash_table_lookup (dict
, &key
.base
);
831 cairo_dict_write_operator (op
, &write_info
);
833 _cairo_hash_table_foreach (dict
, _cairo_dict_collect
, &write_info
);
835 return write_info
.status
;
839 _cff_dict_entry_pluck (void *_entry
, void *dict
)
841 cff_dict_operator_t
*entry
= _entry
;
843 _cairo_hash_table_remove (dict
, &entry
->base
);
844 free (entry
->operand
);
849 cff_dict_fini (cairo_hash_table_t
*dict
)
851 _cairo_hash_table_foreach (dict
, _cff_dict_entry_pluck
, dict
);
852 _cairo_hash_table_destroy (dict
);
855 static cairo_int_status_t
856 cairo_cff_font_read_header (cairo_cff_font_t
*font
)
858 if (font
->data_length
< sizeof (cff_header_t
))
859 return CAIRO_INT_STATUS_UNSUPPORTED
;
862 font
->header
= (cff_header_t
*) font
->data
;
863 font
->current_ptr
= font
->data
+ font
->header
->header_size
;
865 return CAIRO_STATUS_SUCCESS
;
868 static cairo_int_status_t
869 cairo_cff_font_read_name (cairo_cff_font_t
*font
)
872 cairo_int_status_t status
;
873 cff_index_element_t
*element
;
877 cff_index_init (&index
);
878 status
= cff_index_read (&index
, &font
->current_ptr
, font
->data_end
);
879 if (!font
->is_opentype
) {
880 element
= _cairo_array_index (&index
, 0);
882 len
= element
->length
;
884 /* If font name is prefixed with a subset tag, strip it off. */
885 if (len
> 7 && p
[6] == '+') {
886 for (i
= 0; i
< 6; i
++)
887 if (p
[i
] < 'A' || p
[i
] > 'Z')
894 font
->ps_name
= malloc (len
+ 1);
895 if (unlikely (font
->ps_name
== NULL
))
896 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
898 memcpy (font
->ps_name
, p
, len
);
899 font
->ps_name
[len
] = 0;
901 status
= _cairo_escape_ps_name (&font
->ps_name
);
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
= get_unaligned_be16 (p
);
989 for (i
= 0; i
< num_ranges
; i
++)
991 first
= get_unaligned_be16 (p
);
994 last
= get_unaligned_be16 (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 (font
->num_fontdicts
, sizeof (double));
1056 if (unlikely (font
->fd_default_width
== NULL
)) {
1057 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1061 font
->fd_nominal_width
= calloc (font
->num_fontdicts
, sizeof (double));
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/abs(yy) to get units per EM */
1167 font
->units_per_em
= _cairo_round(1.0/fabs(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
= get_unaligned_be16 (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
= get_unaligned_be16 (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
= get_unaligned_be16 (p
);
1757 num_left
= get_unaligned_be16 (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
)) {
1857 fd
= font
->fdselect
[gid
];
1858 if (reverse_map
[fd
] < 0) {
1859 font
->fd_subset_map
[font
->num_subset_fontdicts
] = fd
;
1860 reverse_map
[fd
] = font
->num_subset_fontdicts
++;
1862 font
->fdselect_subset
[i
] = reverse_map
[fd
];
1867 return CAIRO_STATUS_SUCCESS
;
1870 static cairo_status_t
1871 cairo_cff_font_create_cid_fontdict (cairo_cff_font_t
*font
)
1873 unsigned char buf
[100];
1874 unsigned char *end_buf
;
1875 cairo_status_t status
;
1877 font
->num_fontdicts
= 1;
1878 font
->fd_dict
= malloc (sizeof (cairo_hash_table_t
*));
1879 if (unlikely (font
->fd_dict
== NULL
))
1880 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1882 if (cff_dict_init (&font
->fd_dict
[0])) {
1883 free (font
->fd_dict
);
1884 font
->fd_dict
= NULL
;
1885 font
->num_fontdicts
= 0;
1886 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1889 font
->fd_subset_map
= malloc (sizeof (int));
1890 if (unlikely (font
->fd_subset_map
== NULL
))
1891 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1893 font
->private_dict_offset
= malloc (sizeof (int));
1894 if (unlikely (font
->private_dict_offset
== NULL
))
1895 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1897 font
->fd_subset_map
[0] = 0;
1898 font
->num_subset_fontdicts
= 1;
1900 /* Set integer operand to max value to use max size encoding to reserve
1901 * space for any value later */
1902 end_buf
= encode_integer_max (buf
, 0);
1903 end_buf
= encode_integer_max (end_buf
, 0);
1904 status
= cff_dict_set_operands (font
->fd_dict
[0], PRIVATE_OP
, buf
, end_buf
- buf
);
1905 if (unlikely (status
))
1908 return CAIRO_STATUS_SUCCESS
;
1911 static cairo_status_t
1912 cairo_cff_font_subset_strings (cairo_cff_font_t
*font
)
1914 cairo_status_t status
;
1917 status
= cairo_cff_font_subset_dict_strings (font
, font
->top_dict
);
1918 if (unlikely (status
))
1922 for (i
= 0; i
< font
->num_subset_fontdicts
; i
++) {
1923 status
= cairo_cff_font_subset_dict_strings (font
, font
->fd_dict
[font
->fd_subset_map
[i
]]);
1924 if (unlikely (status
))
1927 status
= cairo_cff_font_subset_dict_strings (font
, font
->fd_private_dict
[font
->fd_subset_map
[i
]]);
1928 if (unlikely (status
))
1932 status
= cairo_cff_font_subset_dict_strings (font
, font
->private_dict
);
1938 /* The Euro is the only the only character in the winansi encoding
1939 * with a glyph name that is not a CFF standard string. As the strings
1940 * are written before the charset, we need to check during the
1941 * subsetting phase if the Euro glyph is required and add the
1942 * glyphname to the list of strings to write out.
1944 static cairo_status_t
1945 cairo_cff_font_add_euro_charset_string (cairo_cff_font_t
*font
)
1947 cairo_status_t status
;
1950 const char *euro
= "Euro";
1952 for (i
= 1; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
1953 ch
= font
->scaled_font_subset
->to_latin_char
[i
];
1955 font
->euro_sid
= NUM_STD_STRINGS
+ _cairo_array_num_elements (&font
->strings_subset_index
);
1956 status
= cff_index_append_copy (&font
->strings_subset_index
,
1957 (unsigned char *)euro
, strlen(euro
));
1962 return CAIRO_STATUS_SUCCESS
;
1965 static cairo_status_t
1966 cairo_cff_font_subset_font (cairo_cff_font_t
*font
)
1968 cairo_status_t status
;
1970 if (!font
->scaled_font_subset
->is_latin
) {
1971 status
= cairo_cff_font_set_ros_strings (font
);
1972 if (unlikely (status
))
1976 status
= cairo_cff_font_subset_charstrings_and_subroutines (font
);
1977 if (unlikely (status
))
1980 if (!font
->scaled_font_subset
->is_latin
) {
1982 status
= cairo_cff_font_subset_fontdict (font
);
1984 status
= cairo_cff_font_create_cid_fontdict (font
);
1985 if (unlikely (status
))
1988 font
->private_dict_offset
= malloc (sizeof (int));
1989 if (unlikely (font
->private_dict_offset
== NULL
))
1990 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1993 status
= cairo_cff_font_subset_strings (font
);
1994 if (unlikely (status
))
1997 if (font
->scaled_font_subset
->is_latin
)
1998 status
= cairo_cff_font_add_euro_charset_string (font
);
2003 /* Set the operand of the specified operator in the (already written)
2004 * top dict to point to the current position in the output
2005 * array. Operands updated with this function must have previously
2006 * been encoded with the 5-byte (max) integer encoding. */
2008 cairo_cff_font_set_topdict_operator_to_cur_pos (cairo_cff_font_t
*font
,
2014 unsigned char buf
[10];
2015 unsigned char *buf_end
;
2016 unsigned char *op_ptr
;
2018 cur_pos
= _cairo_array_num_elements (&font
->output
);
2019 buf_end
= encode_integer_max (buf
, cur_pos
);
2020 offset
= cff_dict_get_location (font
->top_dict
, operator, &size
);
2021 assert (offset
> 0);
2022 op_ptr
= _cairo_array_index (&font
->output
, offset
);
2023 memcpy (op_ptr
, buf
, buf_end
- buf
);
2026 static cairo_status_t
2027 cairo_cff_font_write_header (cairo_cff_font_t
*font
)
2029 return _cairo_array_append_multiple (&font
->output
,
2031 font
->header
->header_size
);
2034 static cairo_status_t
2035 cairo_cff_font_write_name (cairo_cff_font_t
*font
)
2037 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
2038 cairo_array_t index
;
2040 cff_index_init (&index
);
2042 status
= cff_index_append_copy (&index
,
2043 (unsigned char *) font
->ps_name
,
2044 strlen(font
->ps_name
));
2045 if (unlikely (status
))
2048 status
= cff_index_write (&index
, &font
->output
);
2049 if (unlikely (status
))
2053 cff_index_fini (&index
);
2058 static cairo_status_t
2059 cairo_cff_font_write_top_dict (cairo_cff_font_t
*font
)
2062 unsigned char buf
[10];
2065 int dict_start
, dict_size
;
2066 int offset_size
= 4;
2067 cairo_status_t status
;
2069 /* Write an index containing the top dict */
2071 count
= cpu_to_be16 (1);
2072 status
= _cairo_array_append_multiple (&font
->output
, &count
, 2);
2073 if (unlikely (status
))
2075 buf
[0] = offset_size
;
2076 status
= _cairo_array_append (&font
->output
, buf
);
2077 if (unlikely (status
))
2079 encode_index_offset (buf
, offset_size
, 1);
2080 status
= _cairo_array_append_multiple (&font
->output
, buf
, offset_size
);
2081 if (unlikely (status
))
2084 /* Reserve space for last element of offset array and update after
2085 * dict is written */
2086 offset_index
= _cairo_array_num_elements (&font
->output
);
2087 status
= _cairo_array_append_multiple (&font
->output
, buf
, offset_size
);
2088 if (unlikely (status
))
2091 dict_start
= _cairo_array_num_elements (&font
->output
);
2092 status
= cff_dict_write (font
->top_dict
, &font
->output
);
2093 if (unlikely (status
))
2095 dict_size
= _cairo_array_num_elements (&font
->output
) - dict_start
;
2097 encode_index_offset (buf
, offset_size
, dict_size
+ 1);
2098 p
= _cairo_array_index (&font
->output
, offset_index
);
2099 memcpy (p
, buf
, offset_size
);
2101 return CAIRO_STATUS_SUCCESS
;
2104 static cairo_status_t
2105 cairo_cff_font_write_strings (cairo_cff_font_t
*font
)
2107 return cff_index_write (&font
->strings_subset_index
, &font
->output
);
2110 static cairo_status_t
2111 cairo_cff_font_write_global_subrs (cairo_cff_font_t
*font
)
2114 unsigned char return_op
= TYPE2_return
;
2116 /* poppler and fontforge don't like zero length subroutines so we
2117 * replace unused subroutines with a 'return' instruction. */
2118 if (font
->subset_subroutines
) {
2119 for (i
= 0; i
< _cairo_array_num_elements (&font
->global_sub_index
); i
++) {
2120 if (! font
->global_subs_used
[i
])
2121 cff_index_set_object (&font
->global_sub_index
, i
, &return_op
, 1);
2125 return cff_index_write (&font
->global_sub_index
, &font
->output
);
2128 static cairo_status_t
2129 cairo_cff_font_write_encoding (cairo_cff_font_t
*font
)
2131 unsigned char buf
[2];
2132 cairo_status_t status
;
2135 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, ENCODING_OP
);
2136 buf
[0] = 0; /* Format 0 */
2137 buf
[1] = font
->scaled_font_subset
->num_glyphs
- 1;
2138 status
= _cairo_array_append_multiple (&font
->output
, buf
, 2);
2139 if (unlikely (status
))
2142 for (i
= 1; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
2143 unsigned char ch
= font
->scaled_font_subset
->to_latin_char
[i
];
2144 status
= _cairo_array_append (&font
->output
, &ch
);
2145 if (unlikely (status
))
2149 return CAIRO_STATUS_SUCCESS
;
2152 static cairo_status_t
2153 cairo_cff_font_write_fdselect (cairo_cff_font_t
*font
)
2157 cairo_int_status_t status
;
2159 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, FDSELECT_OP
);
2163 status
= _cairo_array_append (&font
->output
, &data
);
2164 if (unlikely (status
))
2167 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
2168 data
= font
->fdselect_subset
[i
];
2169 status
= _cairo_array_append (&font
->output
, &data
);
2170 if (unlikely (status
))
2177 status
= _cairo_array_grow_by (&font
->output
, 9);
2178 if (unlikely (status
))
2182 status
= _cairo_array_append (&font
->output
, &byte
);
2183 assert (status
== CAIRO_INT_STATUS_SUCCESS
);
2185 word
= cpu_to_be16 (1);
2186 status
= _cairo_array_append_multiple (&font
->output
, &word
, 2);
2187 assert (status
== CAIRO_INT_STATUS_SUCCESS
);
2189 word
= cpu_to_be16 (0);
2190 status
= _cairo_array_append_multiple (&font
->output
, &word
, 2);
2191 assert (status
== CAIRO_INT_STATUS_SUCCESS
);
2194 status
= _cairo_array_append (&font
->output
, &byte
);
2195 assert (status
== CAIRO_INT_STATUS_SUCCESS
);
2197 word
= cpu_to_be16 (font
->scaled_font_subset
->num_glyphs
);
2198 status
= _cairo_array_append_multiple (&font
->output
, &word
, 2);
2199 assert (status
== CAIRO_INT_STATUS_SUCCESS
);
2202 return CAIRO_STATUS_SUCCESS
;
2205 /* Winansi to CFF standard strings mapping for characters 128 to 255 */
2206 static const int winansi_to_cff_std_string
[] = {
2208 0, 0, 117, 101, 118, 121, 112, 113,
2209 126, 122, 192, 107, 142, 0, 199, 0,
2211 0, 65, 8, 105, 119, 116, 111, 137,
2212 127, 153, 221, 108, 148, 0, 228, 198,
2214 0, 96, 97, 98, 103, 100, 160, 102,
2215 131, 170, 139, 106, 151, 0, 165, 128,
2217 161, 156, 164, 169, 125, 152, 115, 114,
2218 133, 150, 143, 120, 158, 155, 163, 123,
2220 174, 171, 172, 176, 173, 175, 138, 177,
2221 181, 178, 179, 180, 185, 182, 183, 184,
2223 154, 186, 190, 187, 188, 191, 189, 168,
2224 141, 196, 193, 194, 195, 197, 157, 149,
2226 203, 200, 201, 205, 202, 204, 144, 206,
2227 210, 207, 208, 209, 214, 211, 212, 213,
2229 167, 215, 219, 216, 217, 220, 218, 159,
2230 147, 225, 222, 223, 224, 226, 162, 227,
2234 cairo_cff_font_get_sid_for_winansi_char (cairo_cff_font_t
*font
, int ch
)
2241 } else if (ch
== 96) {
2244 } else if (ch
>= 32 && ch
<= 126) {
2247 } else if (ch
== 128) {
2248 assert (font
->euro_sid
>= NUM_STD_STRINGS
);
2249 sid
= font
->euro_sid
;
2251 } else if (ch
>= 128 && ch
<= 255) {
2252 sid
= winansi_to_cff_std_string
[ch
- 128];
2261 static cairo_status_t
2262 cairo_cff_font_write_type1_charset (cairo_cff_font_t
*font
)
2264 unsigned char format
= 0;
2267 cairo_status_t status
;
2270 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, CHARSET_OP
);
2271 status
= _cairo_array_append (&font
->output
, &format
);
2272 if (unlikely (status
))
2275 for (i
= 1; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
2276 ch
= font
->scaled_font_subset
->to_latin_char
[i
];
2277 sid
= cairo_cff_font_get_sid_for_winansi_char (font
, ch
);
2278 if (unlikely (status
))
2281 sid_be16
= cpu_to_be16(sid
);
2282 status
= _cairo_array_append_multiple (&font
->output
, &sid_be16
, sizeof(sid_be16
));
2283 if (unlikely (status
))
2287 return CAIRO_STATUS_SUCCESS
;
2290 static cairo_status_t
2291 cairo_cff_font_write_cid_charset (cairo_cff_font_t
*font
)
2295 cairo_status_t status
;
2297 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, CHARSET_OP
);
2298 status
= _cairo_array_grow_by (&font
->output
, 5);
2299 if (unlikely (status
))
2303 status
= _cairo_array_append (&font
->output
, &byte
);
2304 assert (status
== CAIRO_STATUS_SUCCESS
);
2306 word
= cpu_to_be16 (1);
2307 status
= _cairo_array_append_multiple (&font
->output
, &word
, 2);
2308 assert (status
== CAIRO_STATUS_SUCCESS
);
2310 word
= cpu_to_be16 (font
->scaled_font_subset
->num_glyphs
- 2);
2311 status
= _cairo_array_append_multiple (&font
->output
, &word
, 2);
2312 assert (status
== CAIRO_STATUS_SUCCESS
);
2314 return CAIRO_STATUS_SUCCESS
;
2317 static cairo_status_t
2318 cairo_cff_font_write_charstrings (cairo_cff_font_t
*font
)
2320 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, CHARSTRINGS_OP
);
2322 return cff_index_write (&font
->charstrings_subset_index
, &font
->output
);
2325 static cairo_status_t
2326 cairo_cff_font_write_cid_fontdict (cairo_cff_font_t
*font
)
2329 cairo_int_status_t status
;
2330 unsigned int offset_array
;
2331 unsigned char *offset_array_ptr
;
2334 uint8_t offset_size
= 4;
2336 cairo_cff_font_set_topdict_operator_to_cur_pos (font
, FDARRAY_OP
);
2337 count
= cpu_to_be16 (font
->num_subset_fontdicts
);
2338 status
= _cairo_array_append_multiple (&font
->output
, &count
, sizeof (uint16_t));
2339 if (unlikely (status
))
2341 status
= _cairo_array_append (&font
->output
, &offset_size
);
2342 if (unlikely (status
))
2345 offset_array
= _cairo_array_num_elements (&font
->output
);
2346 status
= _cairo_array_allocate (&font
->output
,
2347 (font
->num_subset_fontdicts
+ 1)*offset_size
,
2348 (void **) &offset_array_ptr
);
2349 if (unlikely (status
))
2351 offset_base
= _cairo_array_num_elements (&font
->output
) - 1;
2352 put_unaligned_be32(1, offset_array_ptr
);
2353 offset_array
+= sizeof(uint32_t);
2354 for (i
= 0; i
< font
->num_subset_fontdicts
; i
++) {
2355 status
= cff_dict_write (font
->fd_dict
[font
->fd_subset_map
[i
]],
2357 if (unlikely (status
))
2360 offset_array_ptr
= _cairo_array_index (&font
->output
, offset_array
);
2361 put_unaligned_be32 (_cairo_array_num_elements (&font
->output
) - offset_base
,
2363 offset_array
+= sizeof(uint32_t);
2366 return CAIRO_STATUS_SUCCESS
;
2369 static cairo_status_t
2370 cairo_cff_font_write_private_dict (cairo_cff_font_t
*font
,
2372 cairo_hash_table_t
*parent_dict
,
2373 cairo_hash_table_t
*private_dict
)
2377 unsigned char buf
[10];
2378 unsigned char *buf_end
;
2380 cairo_status_t status
;
2382 /* Write private dict and update offset and size in top dict */
2383 font
->private_dict_offset
[dict_num
] = _cairo_array_num_elements (&font
->output
);
2384 status
= cff_dict_write (private_dict
, &font
->output
);
2385 if (unlikely (status
))
2388 size
= _cairo_array_num_elements (&font
->output
) - font
->private_dict_offset
[dict_num
];
2389 /* private entry has two operands - size and offset */
2390 buf_end
= encode_integer_max (buf
, size
);
2391 buf_end
= encode_integer_max (buf_end
, font
->private_dict_offset
[dict_num
]);
2392 offset
= cff_dict_get_location (parent_dict
, PRIVATE_OP
, &size
);
2393 assert (offset
> 0);
2394 p
= _cairo_array_index (&font
->output
, offset
);
2395 memcpy (p
, buf
, buf_end
- buf
);
2397 return CAIRO_STATUS_SUCCESS
;
2400 static cairo_status_t
2401 cairo_cff_font_write_local_sub (cairo_cff_font_t
*font
,
2403 cairo_hash_table_t
*private_dict
,
2404 cairo_array_t
*local_sub_index
,
2405 cairo_bool_t
*local_subs_used
)
2409 unsigned char buf
[10];
2410 unsigned char *buf_end
;
2412 cairo_status_t status
;
2414 unsigned char return_op
= TYPE2_return
;
2416 if (_cairo_array_num_elements (local_sub_index
) > 0) {
2417 /* Write local subroutines and update offset in private
2418 * dict. Local subroutines offset is relative to start of
2420 offset
= _cairo_array_num_elements (&font
->output
) - font
->private_dict_offset
[dict_num
];
2421 buf_end
= encode_integer_max (buf
, offset
);
2422 offset
= cff_dict_get_location (private_dict
, LOCAL_SUB_OP
, &size
);
2423 assert (offset
> 0);
2424 p
= _cairo_array_index (&font
->output
, offset
);
2425 memcpy (p
, buf
, buf_end
- buf
);
2427 /* poppler and fontforge don't like zero length subroutines so
2428 * we replace unused subroutines with a 'return' instruction.
2430 if (font
->subset_subroutines
) {
2431 for (i
= 0; i
< _cairo_array_num_elements (local_sub_index
); i
++) {
2432 if (! local_subs_used
[i
])
2433 cff_index_set_object (local_sub_index
, i
, &return_op
, 1);
2436 status
= cff_index_write (local_sub_index
, &font
->output
);
2437 if (unlikely (status
))
2441 return CAIRO_STATUS_SUCCESS
;
2445 static cairo_status_t
2446 cairo_cff_font_write_cid_private_dict_and_local_sub (cairo_cff_font_t
*font
)
2449 cairo_int_status_t status
;
2452 for (i
= 0; i
< font
->num_subset_fontdicts
; i
++) {
2453 status
= cairo_cff_font_write_private_dict (
2456 font
->fd_dict
[font
->fd_subset_map
[i
]],
2457 font
->fd_private_dict
[font
->fd_subset_map
[i
]]);
2458 if (unlikely (status
))
2462 for (i
= 0; i
< font
->num_subset_fontdicts
; i
++) {
2463 status
= cairo_cff_font_write_local_sub (
2466 font
->fd_private_dict
[font
->fd_subset_map
[i
]],
2467 &font
->fd_local_sub_index
[font
->fd_subset_map
[i
]],
2468 font
->fd_local_subs_used
[font
->fd_subset_map
[i
]]);
2469 if (unlikely (status
))
2473 status
= cairo_cff_font_write_private_dict (font
,
2476 font
->private_dict
);
2477 if (unlikely (status
))
2480 status
= cairo_cff_font_write_local_sub (font
,
2483 &font
->local_sub_index
,
2484 font
->local_subs_used
);
2485 if (unlikely (status
))
2489 return CAIRO_STATUS_SUCCESS
;
2492 static cairo_status_t
2493 cairo_cff_font_write_type1_private_dict_and_local_sub (cairo_cff_font_t
*font
)
2495 cairo_int_status_t status
;
2497 status
= cairo_cff_font_write_private_dict (font
,
2500 font
->private_dict
);
2501 if (unlikely (status
))
2504 status
= cairo_cff_font_write_local_sub (font
,
2507 &font
->local_sub_index
,
2508 font
->local_subs_used
);
2509 if (unlikely (status
))
2512 return CAIRO_STATUS_SUCCESS
;
2516 typedef cairo_status_t
2517 (*font_write_t
) (cairo_cff_font_t
*font
);
2519 static const font_write_t font_write_cid_funcs
[] = {
2520 cairo_cff_font_write_header
,
2521 cairo_cff_font_write_name
,
2522 cairo_cff_font_write_top_dict
,
2523 cairo_cff_font_write_strings
,
2524 cairo_cff_font_write_global_subrs
,
2525 cairo_cff_font_write_cid_charset
,
2526 cairo_cff_font_write_fdselect
,
2527 cairo_cff_font_write_charstrings
,
2528 cairo_cff_font_write_cid_fontdict
,
2529 cairo_cff_font_write_cid_private_dict_and_local_sub
,
2532 static const font_write_t font_write_type1_funcs
[] = {
2533 cairo_cff_font_write_header
,
2534 cairo_cff_font_write_name
,
2535 cairo_cff_font_write_top_dict
,
2536 cairo_cff_font_write_strings
,
2537 cairo_cff_font_write_global_subrs
,
2538 cairo_cff_font_write_encoding
,
2539 cairo_cff_font_write_type1_charset
,
2540 cairo_cff_font_write_charstrings
,
2541 cairo_cff_font_write_type1_private_dict_and_local_sub
,
2544 static cairo_status_t
2545 cairo_cff_font_write_subset (cairo_cff_font_t
*font
)
2547 cairo_int_status_t status
;
2550 if (font
->scaled_font_subset
->is_latin
) {
2551 for (i
= 0; i
< ARRAY_LENGTH (font_write_type1_funcs
); i
++) {
2552 status
= font_write_type1_funcs
[i
] (font
);
2553 if (unlikely (status
))
2557 for (i
= 0; i
< ARRAY_LENGTH (font_write_cid_funcs
); i
++) {
2558 status
= font_write_cid_funcs
[i
] (font
);
2559 if (unlikely (status
))
2564 return CAIRO_STATUS_SUCCESS
;
2567 static cairo_int_status_t
2568 cairo_cff_font_generate (cairo_cff_font_t
*font
,
2570 unsigned long *length
)
2572 cairo_int_status_t status
;
2574 status
= cairo_cff_font_read_font (font
);
2575 if (unlikely (status
))
2578 /* If the PS name is not found, create a CairoFont-x-y name. */
2579 if (font
->ps_name
== NULL
) {
2580 font
->ps_name
= malloc (30);
2581 if (unlikely (font
->ps_name
== NULL
))
2582 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2584 snprintf(font
->ps_name
, 30, "CairoFont-%u-%u",
2585 font
->scaled_font_subset
->font_id
,
2586 font
->scaled_font_subset
->subset_id
);
2589 status
= cairo_cff_font_subset_font (font
);
2590 if (unlikely (status
))
2593 status
= cairo_cff_font_write_subset (font
);
2594 if (unlikely (status
))
2598 *data
= _cairo_array_index (&font
->output
, 0);
2599 *length
= _cairo_array_num_elements (&font
->output
);
2601 return CAIRO_STATUS_SUCCESS
;
2604 static cairo_int_status_t
2605 cairo_cff_font_create_set_widths (cairo_cff_font_t
*font
)
2608 unsigned long long_entry_size
;
2609 unsigned long short_entry_size
;
2613 uint16_t short_entry
;
2615 cairo_int_status_t status
;
2617 size
= sizeof (tt_hhea_t
);
2618 status
= font
->backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2620 (unsigned char*) &hhea
, &size
);
2621 if (unlikely (status
))
2623 num_hmetrics
= be16_to_cpu (hhea
.num_hmetrics
);
2625 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
2626 glyph_index
= font
->scaled_font_subset
->glyphs
[i
];
2627 long_entry_size
= 2 * sizeof (int16_t);
2628 short_entry_size
= sizeof (int16_t);
2629 if (glyph_index
< num_hmetrics
) {
2630 status
= font
->backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2632 glyph_index
* long_entry_size
,
2633 (unsigned char *) &short_entry
,
2635 if (unlikely (status
))
2640 status
= font
->backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2642 (num_hmetrics
- 1) * long_entry_size
,
2643 (unsigned char *) &short_entry
,
2645 if (unlikely (status
))
2648 font
->widths
[i
] = be16_to_cpu (short_entry
);
2651 return CAIRO_STATUS_SUCCESS
;
2655 check_fontdata_is_cff (const unsigned char *data
, long length
)
2657 cff_header_t
*header
;
2659 if (length
< (long)sizeof (cff_header_t
))
2662 header
= (cff_header_t
*) data
;
2663 if (header
->major
== 1 &&
2664 header
->minor
== 0 &&
2665 header
->header_size
== 4)
2673 static cairo_int_status_t
2674 _cairo_cff_font_load_opentype_cff (cairo_cff_font_t
*font
)
2676 const cairo_scaled_font_backend_t
*backend
= font
->backend
;
2677 cairo_status_t status
;
2680 unsigned long size
, data_length
;
2682 if (!backend
->load_truetype_table
)
2683 return CAIRO_INT_STATUS_UNSUPPORTED
;
2686 status
= backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2687 TT_TAG_CFF
, 0, NULL
, &data_length
);
2691 size
= sizeof (tt_head_t
);
2692 status
= backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2694 (unsigned char *) &head
, &size
);
2695 if (unlikely (status
))
2698 size
= sizeof (tt_hhea_t
);
2699 status
= backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2701 (unsigned char *) &hhea
, &size
);
2702 if (unlikely (status
))
2706 status
= backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2707 TT_TAG_hmtx
, 0, NULL
, &size
);
2708 if (unlikely (status
))
2711 font
->x_min
= (int16_t) be16_to_cpu (head
.x_min
);
2712 font
->y_min
= (int16_t) be16_to_cpu (head
.y_min
);
2713 font
->x_max
= (int16_t) be16_to_cpu (head
.x_max
);
2714 font
->y_max
= (int16_t) be16_to_cpu (head
.y_max
);
2715 font
->ascent
= (int16_t) be16_to_cpu (hhea
.ascender
);
2716 font
->descent
= (int16_t) be16_to_cpu (hhea
.descender
);
2717 font
->units_per_em
= (int16_t) be16_to_cpu (head
.units_per_em
);
2718 if (font
->units_per_em
== 0)
2719 font
->units_per_em
= 1000;
2721 font
->font_name
= NULL
;
2722 status
= _cairo_truetype_read_font_name (font
->scaled_font_subset
->scaled_font
,
2725 if (_cairo_status_is_error (status
))
2728 font
->is_opentype
= TRUE
;
2729 font
->data_length
= data_length
;
2730 font
->data
= malloc (data_length
);
2731 if (unlikely (font
->data
== NULL
))
2732 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2734 status
= font
->backend
->load_truetype_table (font
->scaled_font_subset
->scaled_font
,
2735 TT_TAG_CFF
, 0, font
->data
,
2736 &font
->data_length
);
2737 if (unlikely (status
))
2740 if (!check_fontdata_is_cff (font
->data
, data_length
))
2741 return CAIRO_INT_STATUS_UNSUPPORTED
;
2743 return CAIRO_STATUS_SUCCESS
;
2746 static cairo_int_status_t
2747 _cairo_cff_font_load_cff (cairo_cff_font_t
*font
)
2749 const cairo_scaled_font_backend_t
*backend
= font
->backend
;
2750 cairo_status_t status
;
2751 unsigned long data_length
;
2753 if (!backend
->load_type1_data
)
2754 return CAIRO_INT_STATUS_UNSUPPORTED
;
2757 status
= backend
->load_type1_data (font
->scaled_font_subset
->scaled_font
,
2758 0, NULL
, &data_length
);
2759 if (unlikely (status
))
2762 font
->font_name
= NULL
;
2763 font
->is_opentype
= FALSE
;
2764 font
->data_length
= data_length
;
2765 font
->data
= malloc (data_length
);
2766 if (unlikely (font
->data
== NULL
))
2767 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2769 status
= font
->backend
->load_type1_data (font
->scaled_font_subset
->scaled_font
,
2770 0, font
->data
, &font
->data_length
);
2771 if (unlikely (status
))
2774 if (!check_fontdata_is_cff (font
->data
, data_length
))
2775 return CAIRO_INT_STATUS_UNSUPPORTED
;
2777 return CAIRO_STATUS_SUCCESS
;
2780 static cairo_int_status_t
2781 _cairo_cff_font_create (cairo_scaled_font_subset_t
*scaled_font_subset
,
2782 cairo_cff_font_t
**font_return
,
2783 const char *subset_name
)
2785 const cairo_scaled_font_backend_t
*backend
;
2786 cairo_int_status_t status
;
2787 cairo_cff_font_t
*font
;
2789 backend
= scaled_font_subset
->scaled_font
->backend
;
2791 /* We need to use a fallback font generated from the synthesized outlines. */
2792 if (backend
->is_synthetic
&& backend
->is_synthetic (scaled_font_subset
->scaled_font
))
2793 return CAIRO_INT_STATUS_UNSUPPORTED
;
2795 font
= calloc (1, sizeof (cairo_cff_font_t
));
2796 if (unlikely (font
== NULL
))
2797 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2799 font
->backend
= backend
;
2800 font
->scaled_font_subset
= scaled_font_subset
;
2802 status
= _cairo_cff_font_load_opentype_cff (font
);
2803 if (status
== CAIRO_INT_STATUS_UNSUPPORTED
)
2804 status
= _cairo_cff_font_load_cff (font
);
2808 font
->data_end
= font
->data
+ font
->data_length
;
2809 _cairo_array_init (&font
->output
, sizeof (char));
2810 status
= _cairo_array_grow_by (&font
->output
, 4096);
2811 if (unlikely (status
))
2814 font
->subset_font_name
= strdup (subset_name
);
2815 if (unlikely (font
->subset_font_name
== NULL
)) {
2816 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2820 font
->widths
= calloc (font
->scaled_font_subset
->num_glyphs
, sizeof (int));
2821 if (unlikely (font
->widths
== NULL
)) {
2822 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2826 if (font
->is_opentype
) {
2827 status
= cairo_cff_font_create_set_widths (font
);
2828 if (unlikely (status
))
2832 status
= cff_dict_init (&font
->top_dict
);
2833 if (unlikely (status
))
2836 status
= cff_dict_init (&font
->private_dict
);
2837 if (unlikely (status
))
2840 cff_index_init (&font
->strings_index
);
2841 cff_index_init (&font
->charstrings_index
);
2842 cff_index_init (&font
->global_sub_index
);
2843 cff_index_init (&font
->local_sub_index
);
2844 cff_index_init (&font
->charstrings_subset_index
);
2845 cff_index_init (&font
->strings_subset_index
);
2847 font
->fdselect
= NULL
;
2848 font
->fd_dict
= NULL
;
2849 font
->fd_private_dict
= NULL
;
2850 font
->fd_local_sub_index
= NULL
;
2851 font
->fd_local_sub_bias
= NULL
;
2852 font
->fdselect_subset
= NULL
;
2853 font
->fd_subset_map
= NULL
;
2854 font
->private_dict_offset
= NULL
;
2855 font
->global_subs_used
= NULL
;
2856 font
->local_subs_used
= NULL
;
2857 font
->fd_local_subs_used
= NULL
;
2859 *font_return
= font
;
2861 return CAIRO_STATUS_SUCCESS
;
2864 _cairo_hash_table_destroy (font
->top_dict
);
2866 free (font
->widths
);
2868 free (font
->subset_font_name
);
2870 free (font
->ps_name
);
2871 _cairo_array_fini (&font
->output
);
2874 free (font
->font_name
);
2881 cairo_cff_font_destroy (cairo_cff_font_t
*font
)
2885 free (font
->widths
);
2886 free (font
->font_name
);
2887 free (font
->ps_name
);
2888 free (font
->subset_font_name
);
2889 _cairo_array_fini (&font
->output
);
2890 cff_dict_fini (font
->top_dict
);
2891 cff_dict_fini (font
->private_dict
);
2892 cff_index_fini (&font
->strings_index
);
2893 cff_index_fini (&font
->charstrings_index
);
2894 cff_index_fini (&font
->global_sub_index
);
2895 cff_index_fini (&font
->local_sub_index
);
2896 cff_index_fini (&font
->charstrings_subset_index
);
2897 cff_index_fini (&font
->strings_subset_index
);
2899 /* If we bailed out early as a result of an error some of the
2900 * following cairo_cff_font_t members may still be NULL */
2901 if (font
->fd_dict
) {
2902 for (i
= 0; i
< font
->num_fontdicts
; i
++) {
2903 if (font
->fd_dict
[i
])
2904 cff_dict_fini (font
->fd_dict
[i
]);
2906 free (font
->fd_dict
);
2908 free (font
->global_subs_used
);
2909 free (font
->local_subs_used
);
2910 free (font
->fd_subset_map
);
2911 free (font
->private_dict_offset
);
2914 free (font
->fdselect
);
2915 free (font
->fdselect_subset
);
2916 if (font
->fd_private_dict
) {
2917 for (i
= 0; i
< font
->num_fontdicts
; i
++) {
2918 if (font
->fd_private_dict
[i
])
2919 cff_dict_fini (font
->fd_private_dict
[i
]);
2921 free (font
->fd_private_dict
);
2923 if (font
->fd_local_sub_index
) {
2924 for (i
= 0; i
< font
->num_fontdicts
; i
++)
2925 cff_index_fini (&font
->fd_local_sub_index
[i
]);
2926 free (font
->fd_local_sub_index
);
2928 free (font
->fd_local_sub_bias
);
2929 if (font
->fd_local_subs_used
) {
2930 for (i
= 0; i
< font
->num_fontdicts
; i
++) {
2931 free (font
->fd_local_subs_used
[i
]);
2933 free (font
->fd_local_subs_used
);
2935 free (font
->fd_default_width
);
2936 free (font
->fd_nominal_width
);
2945 _cairo_cff_subset_init (cairo_cff_subset_t
*cff_subset
,
2946 const char *subset_name
,
2947 cairo_scaled_font_subset_t
*font_subset
)
2949 cairo_cff_font_t
*font
= NULL
; /* squelch bogus compiler warning */
2950 cairo_status_t status
;
2951 const char *data
= NULL
; /* squelch bogus compiler warning */
2952 unsigned long length
= 0; /* squelch bogus compiler warning */
2955 status
= _cairo_cff_font_create (font_subset
, &font
, subset_name
);
2956 if (unlikely (status
))
2959 status
= cairo_cff_font_generate (font
, &data
, &length
);
2960 if (unlikely (status
))
2963 cff_subset
->ps_name
= strdup (font
->ps_name
);
2964 if (unlikely (cff_subset
->ps_name
== NULL
)) {
2965 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2969 if (font
->font_name
) {
2970 cff_subset
->family_name_utf8
= strdup (font
->font_name
);
2971 if (cff_subset
->family_name_utf8
== NULL
) {
2972 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2976 cff_subset
->family_name_utf8
= NULL
;
2979 cff_subset
->widths
= calloc (sizeof (double), font
->scaled_font_subset
->num_glyphs
);
2980 if (unlikely (cff_subset
->widths
== NULL
)) {
2981 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
2984 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++)
2985 cff_subset
->widths
[i
] = (double)font
->widths
[i
]/font
->units_per_em
;
2987 cff_subset
->x_min
= (double)font
->x_min
/font
->units_per_em
;
2988 cff_subset
->y_min
= (double)font
->y_min
/font
->units_per_em
;
2989 cff_subset
->x_max
= (double)font
->x_max
/font
->units_per_em
;
2990 cff_subset
->y_max
= (double)font
->y_max
/font
->units_per_em
;
2991 cff_subset
->ascent
= (double)font
->ascent
/font
->units_per_em
;
2992 cff_subset
->descent
= (double)font
->descent
/font
->units_per_em
;
2994 cff_subset
->data
= malloc (length
);
2995 if (unlikely (cff_subset
->data
== NULL
)) {
2996 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3000 memcpy (cff_subset
->data
, data
, length
);
3001 cff_subset
->data_length
= length
;
3003 cairo_cff_font_destroy (font
);
3005 return CAIRO_STATUS_SUCCESS
;
3008 free (cff_subset
->widths
);
3010 free (cff_subset
->family_name_utf8
);
3012 free (cff_subset
->ps_name
);
3014 cairo_cff_font_destroy (font
);
3020 _cairo_cff_subset_fini (cairo_cff_subset_t
*subset
)
3022 free (subset
->ps_name
);
3023 free (subset
->family_name_utf8
);
3024 free (subset
->widths
);
3025 free (subset
->data
);
3029 _cairo_cff_scaled_font_is_cid_cff (cairo_scaled_font_t
*scaled_font
)
3031 const cairo_scaled_font_backend_t
*backend
;
3032 cairo_int_status_t status
;
3033 unsigned char *data
;
3034 unsigned long data_length
;
3035 unsigned char *current_ptr
;
3036 unsigned char *data_end
;
3037 cff_header_t
*header
;
3038 cff_index_element_t
*element
;
3039 cairo_hash_table_t
*top_dict
;
3040 cairo_array_t index
;
3042 cairo_bool_t is_cid
= FALSE
;
3044 backend
= scaled_font
->backend
;
3047 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
3048 /* Try to load an OpenType/CFF font */
3049 if (backend
->load_truetype_table
&&
3050 (status
= backend
->load_truetype_table (scaled_font
, TT_TAG_CFF
,
3051 0, NULL
, &data_length
)) == CAIRO_INT_STATUS_SUCCESS
)
3053 data
= malloc (data_length
);
3054 if (unlikely (data
== NULL
)) {
3055 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3059 status
= backend
->load_truetype_table (scaled_font
, TT_TAG_CFF
,
3060 0, data
, &data_length
);
3061 if (unlikely (status
))
3064 /* Try to load a CFF font */
3065 if (status
== CAIRO_INT_STATUS_UNSUPPORTED
&&
3066 backend
->load_type1_data
&&
3067 (status
= backend
->load_type1_data (scaled_font
,
3068 0, NULL
, &data_length
)) == CAIRO_INT_STATUS_SUCCESS
)
3070 data
= malloc (data_length
);
3071 if (unlikely (data
== NULL
)) {
3072 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3076 status
= backend
->load_type1_data (scaled_font
, 0, data
, &data_length
);
3077 if (unlikely (status
))
3083 /* Check if it looks like a CFF font */
3084 if (!check_fontdata_is_cff (data
, data_length
))
3087 data_end
= data
+ data_length
;
3090 if (data_length
< sizeof (cff_header_t
))
3093 header
= (cff_header_t
*) data
;
3094 current_ptr
= data
+ header
->header_size
;
3097 cff_index_init (&index
);
3098 status
= cff_index_read (&index
, ¤t_ptr
, data_end
);
3099 cff_index_fini (&index
);
3105 cff_index_init (&index
);
3106 status
= cff_index_read (&index
, ¤t_ptr
, data_end
);
3107 if (unlikely (status
))
3110 status
= cff_dict_init (&top_dict
);
3111 if (unlikely (status
))
3114 element
= _cairo_array_index (&index
, 0);
3115 status
= cff_dict_read (top_dict
, element
->data
, element
->length
);
3116 if (unlikely (status
))
3119 /* check for ROS operator indicating a CID font */
3120 if (cff_dict_get_operands (top_dict
, ROS_OP
, &size
) != NULL
)
3124 cff_dict_fini (top_dict
);
3127 cff_index_fini (&index
);
3135 static cairo_int_status_t
3136 _cairo_cff_font_fallback_create (cairo_scaled_font_subset_t
*scaled_font_subset
,
3137 cairo_cff_font_t
**font_return
,
3138 const char *subset_name
)
3140 cairo_status_t status
;
3141 cairo_cff_font_t
*font
;
3143 font
= malloc (sizeof (cairo_cff_font_t
));
3144 if (unlikely (font
== NULL
))
3145 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3147 font
->backend
= NULL
;
3148 font
->scaled_font_subset
= scaled_font_subset
;
3150 _cairo_array_init (&font
->output
, sizeof (char));
3151 status
= _cairo_array_grow_by (&font
->output
, 4096);
3152 if (unlikely (status
))
3155 font
->subset_font_name
= strdup (subset_name
);
3156 if (unlikely (font
->subset_font_name
== NULL
)) {
3157 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3161 font
->ps_name
= strdup (subset_name
);
3162 if (unlikely (font
->ps_name
== NULL
)) {
3163 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3166 font
->font_name
= NULL
;
3175 font
->widths
= calloc (font
->scaled_font_subset
->num_glyphs
, sizeof (int));
3176 if (unlikely (font
->widths
== NULL
)) {
3177 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3181 font
->data_length
= 0;
3183 font
->data_end
= NULL
;
3185 status
= cff_dict_init (&font
->top_dict
);
3186 if (unlikely (status
))
3189 status
= cff_dict_init (&font
->private_dict
);
3190 if (unlikely (status
))
3193 cff_index_init (&font
->strings_index
);
3194 cff_index_init (&font
->charstrings_index
);
3195 cff_index_init (&font
->global_sub_index
);
3196 cff_index_init (&font
->local_sub_index
);
3197 cff_index_init (&font
->charstrings_subset_index
);
3198 cff_index_init (&font
->strings_subset_index
);
3199 font
->global_subs_used
= NULL
;
3200 font
->local_subs_used
= NULL
;
3201 font
->subset_subroutines
= FALSE
;
3202 font
->fdselect
= NULL
;
3203 font
->fd_dict
= NULL
;
3204 font
->fd_private_dict
= NULL
;
3205 font
->fd_local_sub_index
= NULL
;
3206 font
->fdselect_subset
= NULL
;
3207 font
->fd_subset_map
= NULL
;
3208 font
->private_dict_offset
= NULL
;
3210 *font_return
= font
;
3212 return CAIRO_STATUS_SUCCESS
;
3215 _cairo_hash_table_destroy (font
->top_dict
);
3217 free (font
->widths
);
3219 free (font
->font_name
);
3220 free (font
->ps_name
);
3222 free (font
->subset_font_name
);
3224 _cairo_array_fini (&font
->output
);
3229 static cairo_int_status_t
3230 cairo_cff_font_fallback_generate (cairo_cff_font_t
*font
,
3231 cairo_type2_charstrings_t
*type2_subset
,
3233 unsigned long *length
)
3235 cairo_int_status_t status
;
3236 cff_header_t header
;
3237 cairo_array_t
*charstring
;
3238 unsigned char buf
[40];
3239 unsigned char *end_buf
, *end_buf2
;
3246 header
.header_size
= 4;
3247 header
.offset_size
= 4;
3248 font
->header
= &header
;
3250 /* Create Top Dict */
3251 font
->is_cid
= FALSE
;
3253 snprintf((char*)buf
, sizeof(buf
), "CairoFont-%u-%u",
3254 font
->scaled_font_subset
->font_id
,
3255 font
->scaled_font_subset
->subset_id
);
3256 sid
= NUM_STD_STRINGS
+ _cairo_array_num_elements (&font
->strings_subset_index
);
3257 status
= cff_index_append_copy (&font
->strings_subset_index
,
3258 (unsigned char *)buf
,
3259 strlen((char*)buf
));
3260 if (unlikely (status
))
3263 end_buf
= encode_integer (buf
, sid
);
3264 status
= cff_dict_set_operands (font
->top_dict
, FULLNAME_OP
,
3265 buf
, end_buf
- buf
);
3266 if (unlikely (status
))
3269 status
= cff_dict_set_operands (font
->top_dict
, FAMILYNAME_OP
,
3270 buf
, end_buf
- buf
);
3271 if (unlikely (status
))
3274 end_buf
= encode_integer (buf
, type2_subset
->x_min
);
3275 end_buf
= encode_integer (end_buf
, type2_subset
->y_min
);
3276 end_buf
= encode_integer (end_buf
, type2_subset
->x_max
);
3277 end_buf
= encode_integer (end_buf
, type2_subset
->y_max
);
3278 status
= cff_dict_set_operands (font
->top_dict
,
3279 FONTBBOX_OP
, buf
, end_buf
- buf
);
3280 if (unlikely (status
))
3283 end_buf
= encode_integer_max (buf
, 0);
3284 status
= cff_dict_set_operands (font
->top_dict
,
3285 CHARSTRINGS_OP
, buf
, end_buf
- buf
);
3286 if (unlikely (status
))
3290 if (font
->scaled_font_subset
->is_latin
) {
3291 status
= cff_dict_set_operands (font
->top_dict
,
3292 ENCODING_OP
, buf
, end_buf
- buf
);
3293 if (unlikely (status
))
3296 /* Private has two operands - size and offset */
3297 end_buf2
= encode_integer_max (end_buf
, 0);
3298 cff_dict_set_operands (font
->top_dict
, PRIVATE_OP
, buf
, end_buf2
- buf
);
3299 if (unlikely (status
))
3303 status
= cff_dict_set_operands (font
->top_dict
,
3304 FDSELECT_OP
, buf
, end_buf
- buf
);
3305 if (unlikely (status
))
3308 status
= cff_dict_set_operands (font
->top_dict
,
3309 FDARRAY_OP
, buf
, end_buf
- buf
);
3310 if (unlikely (status
))
3314 status
= cff_dict_set_operands (font
->top_dict
,
3315 CHARSET_OP
, buf
, end_buf
- buf
);
3316 if (unlikely (status
))
3319 if (!font
->scaled_font_subset
->is_latin
) {
3320 status
= cairo_cff_font_set_ros_strings (font
);
3321 if (unlikely (status
))
3324 /* Create CID FD dictionary */
3325 status
= cairo_cff_font_create_cid_fontdict (font
);
3326 if (unlikely (status
))
3329 font
->private_dict_offset
= malloc (sizeof (int));
3330 if (unlikely (font
->private_dict_offset
== NULL
))
3331 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3334 /* Create charstrings */
3335 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
3336 charstring
= _cairo_array_index(&type2_subset
->charstrings
, i
);
3338 status
= cff_index_append (&font
->charstrings_subset_index
,
3339 _cairo_array_index (charstring
, 0),
3340 _cairo_array_num_elements (charstring
));
3342 if (unlikely (status
))
3346 if (font
->scaled_font_subset
->is_latin
)
3347 status
= cairo_cff_font_add_euro_charset_string (font
);
3349 status
= cairo_cff_font_write_subset (font
);
3350 if (unlikely (status
))
3353 *data
= _cairo_array_index (&font
->output
, 0);
3354 *length
= _cairo_array_num_elements (&font
->output
);
3356 return CAIRO_STATUS_SUCCESS
;
3360 _cairo_cff_fallback_init (cairo_cff_subset_t
*cff_subset
,
3361 const char *subset_name
,
3362 cairo_scaled_font_subset_t
*font_subset
)
3364 cairo_cff_font_t
*font
= NULL
; /* squelch bogus compiler warning */
3365 cairo_status_t status
;
3366 const char *data
= NULL
; /* squelch bogus compiler warning */
3367 unsigned long length
= 0; /* squelch bogus compiler warning */
3369 cairo_type2_charstrings_t type2_subset
;
3371 status
= _cairo_cff_font_fallback_create (font_subset
, &font
, subset_name
);
3372 if (unlikely (status
))
3375 status
= _cairo_type2_charstrings_init (&type2_subset
, font_subset
);
3376 if (unlikely (status
))
3379 status
= cairo_cff_font_fallback_generate (font
, &type2_subset
, &data
, &length
);
3380 if (unlikely (status
))
3383 cff_subset
->family_name_utf8
= NULL
;
3384 cff_subset
->ps_name
= strdup (font
->ps_name
);
3385 if (unlikely (cff_subset
->ps_name
== NULL
)) {
3386 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3390 cff_subset
->widths
= calloc (sizeof (double), font
->scaled_font_subset
->num_glyphs
);
3391 if (unlikely (cff_subset
->widths
== NULL
)) {
3392 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3396 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++)
3397 cff_subset
->widths
[i
] = (double)type2_subset
.widths
[i
]/1000;
3399 cff_subset
->x_min
= (double)type2_subset
.x_min
/1000;
3400 cff_subset
->y_min
= (double)type2_subset
.y_min
/1000;
3401 cff_subset
->x_max
= (double)type2_subset
.x_max
/1000;
3402 cff_subset
->y_max
= (double)type2_subset
.y_max
/1000;
3403 cff_subset
->ascent
= (double)type2_subset
.y_max
/1000;
3404 cff_subset
->descent
= (double)type2_subset
.y_min
/1000;
3406 cff_subset
->data
= malloc (length
);
3407 if (unlikely (cff_subset
->data
== NULL
)) {
3408 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
3412 memcpy (cff_subset
->data
, data
, length
);
3413 cff_subset
->data_length
= length
;
3415 _cairo_type2_charstrings_fini (&type2_subset
);
3416 cairo_cff_font_destroy (font
);
3418 return CAIRO_STATUS_SUCCESS
;
3421 free (cff_subset
->widths
);
3423 free (cff_subset
->ps_name
);
3425 _cairo_type2_charstrings_fini (&type2_subset
);
3427 cairo_cff_font_destroy (font
);
3433 _cairo_cff_fallback_fini (cairo_cff_subset_t
*subset
)
3435 free (subset
->ps_name
);
3436 free (subset
->widths
);
3437 free (subset
->data
);
3440 #endif /* CAIRO_HAS_FONT_SUBSET */