1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2006 Red Hat, Inc
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 Red Hat, Inc.
34 * Kristian Høgsberg <krh@redhat.com>
39 * http://partners.adobe.com/public/developer/en/font/T1_SPEC.PDF
43 #define _BSD_SOURCE /* for snprintf(), strdup() */
46 #include "cairo-array-private.h"
47 #include "cairo-error-private.h"
49 #if CAIRO_HAS_FONT_SUBSET
51 #include "cairo-type1-private.h"
52 #include "cairo-scaled-font-subsets-private.h"
53 #include "cairo-output-stream-private.h"
57 #define TYPE1_STACKSIZE 24 /* Defined in Type 1 Font Format */
63 const char *encrypted_charstring
;
64 int encrypted_charstring_length
;
67 typedef struct _cairo_type1_font_subset
{
68 cairo_scaled_font_subset_t
*scaled_font_subset
;
73 unsigned int num_glyphs
;
74 double x_min
, y_min
, x_max
, y_max
;
75 double ascent
, descent
;
79 unsigned long header_size
;
80 unsigned long data_size
;
81 unsigned long trailer_size
;
86 /* The glyphs and glyph_names arrays are indexed by the order of
87 * the Charstrings in the font. This is not necessarily the same
88 * order as the glyph index. The index_to_glyph_name() font backend
89 * function is used to map the glyph index to the glyph order in
94 cairo_array_t glyphs_array
;
95 cairo_array_t glyph_names_array
;
98 cairo_bool_t subset_subrs
;
100 const char *subr_string
;
107 /* Indexed by subset_index this maps to the glyph order in the
108 * glyph_names and glyphs arrays. Has font->num_golyphs
110 int *subset_index_to_glyphs
;
112 cairo_output_stream_t
*output
;
113 cairo_array_t contents
;
115 const char *rd
, *nd
, *np
;
120 unsigned int type1_length
;
123 char *header_segment
;
124 int header_segment_size
;
126 int eexec_segment_size
;
127 cairo_bool_t eexec_segment_is_ascii
;
134 unsigned short eexec_key
;
135 cairo_bool_t hex_encode
;
139 double stack
[TYPE1_STACKSIZE
];
144 int stack
[TYPE1_STACKSIZE
];
149 } cairo_type1_font_subset_t
;
152 static cairo_status_t
153 _cairo_type1_font_subset_init (cairo_type1_font_subset_t
*font
,
154 cairo_scaled_font_subset_t
*scaled_font_subset
,
155 cairo_bool_t hex_encode
)
157 memset (font
, 0, sizeof (*font
));
158 font
->scaled_font_subset
= scaled_font_subset
;
160 _cairo_array_init (&font
->glyphs_array
, sizeof (glyph_data_t
));
161 _cairo_array_init (&font
->glyph_names_array
, sizeof (char *));
162 font
->subset_index_to_glyphs
= NULL
;
163 font
->base
.num_glyphs
= 0;
165 font
->subset_subrs
= TRUE
;
168 font
->hex_encode
= hex_encode
;
169 font
->num_glyphs
= 0;
171 _cairo_array_init (&font
->contents
, sizeof (char));
173 return CAIRO_STATUS_SUCCESS
;
177 cairo_type1_font_subset_use_glyph (cairo_type1_font_subset_t
*font
, int glyph
)
179 if (font
->glyphs
[glyph
].subset_index
>= 0)
182 font
->glyphs
[glyph
].subset_index
= font
->num_glyphs
;
183 font
->subset_index_to_glyphs
[font
->num_glyphs
] = glyph
;
188 is_ps_delimiter(int c
)
190 static const char delimiters
[] = "()[]{}<>/% \t\r\n";
192 return strchr (delimiters
, c
) != NULL
;
196 find_token (const char *buffer
, const char *end
, const char *token
)
199 /* FIXME: find substring really must be find_token */
204 length
= strlen (token
);
205 for (i
= 0; buffer
+ i
< end
- length
+ 1; i
++)
206 if (memcmp (buffer
+ i
, token
, length
) == 0)
207 if ((i
== 0 || token
[0] == '/' || is_ps_delimiter(buffer
[i
- 1])) &&
208 (buffer
+ i
== end
- length
|| is_ps_delimiter(buffer
[i
+ length
])))
214 static cairo_status_t
215 cairo_type1_font_subset_find_segments (cairo_type1_font_subset_t
*font
)
218 const char *eexec_token
;
221 p
= (unsigned char *) font
->type1_data
;
222 font
->type1_end
= font
->type1_data
+ font
->type1_length
;
223 if (p
[0] == 0x80 && p
[1] == 0x01) {
224 font
->header_segment_size
=
225 p
[2] | (p
[3] << 8) | (p
[4] << 16) | (p
[5] << 24);
226 font
->header_segment
= (char *) p
+ 6;
228 p
+= 6 + font
->header_segment_size
;
229 font
->eexec_segment_size
=
230 p
[2] | (p
[3] << 8) | (p
[4] << 16) | (p
[5] << 24);
231 font
->eexec_segment
= (char *) p
+ 6;
232 font
->eexec_segment_is_ascii
= (p
[1] == 1);
234 p
+= 6 + font
->eexec_segment_size
;
235 while (p
< (unsigned char *) (font
->type1_end
) && p
[1] != 0x03) {
236 size
= p
[2] | (p
[3] << 8) | (p
[4] << 16) | (p
[5] << 24);
239 font
->type1_end
= (char *) p
;
241 eexec_token
= find_token ((char *) p
, font
->type1_end
, "eexec");
242 if (eexec_token
== NULL
)
243 return CAIRO_INT_STATUS_UNSUPPORTED
;
245 font
->header_segment_size
= eexec_token
- (char *) p
+ strlen ("eexec\n");
246 font
->header_segment
= (char *) p
;
247 font
->eexec_segment_size
= font
->type1_length
- font
->header_segment_size
;
248 font
->eexec_segment
= (char *) p
+ font
->header_segment_size
;
249 font
->eexec_segment_is_ascii
= TRUE
;
250 for (i
= 0; i
< 4; i
++) {
251 if (!isxdigit(font
->eexec_segment
[i
]))
252 font
->eexec_segment_is_ascii
= FALSE
;
256 return CAIRO_STATUS_SUCCESS
;
259 /* Search for the definition of key and erase it by overwriting with spaces.
260 * This function is looks for definitions of the form:
263 * /key2 [12 34 56] def
265 * ie a key defined as an integer or array of integers.
269 cairo_type1_font_erase_dict_key (cairo_type1_font_subset_t
*font
,
272 const char *start
, *p
, *segment_end
;
274 segment_end
= font
->header_segment
+ font
->header_segment_size
;
276 start
= font
->header_segment
;
278 start
= find_token (start
, segment_end
, key
);
280 p
= start
+ strlen(key
);
281 /* skip integers or array of integers */
282 while (p
< segment_end
&&
283 (_cairo_isspace(*p
) ||
284 _cairo_isdigit(*p
) ||
291 if (p
+ 3 < segment_end
&& memcmp(p
, "def", 3) == 0) {
292 /* erase definition of the key */
293 memset((char *) start
, ' ', p
+ 3 - start
);
295 start
+= strlen(key
);
300 static cairo_status_t
301 cairo_type1_font_subset_get_matrix (cairo_type1_font_subset_t
*font
,
308 const char *start
, *end
, *segment_end
;
309 int ret
, s_max
, i
, j
;
311 const char *decimal_point
;
312 int decimal_point_len
;
314 decimal_point
= cairo_get_locale_decimal_point ();
315 decimal_point_len
= strlen (decimal_point
);
317 assert (decimal_point_len
!= 0);
319 segment_end
= font
->header_segment
+ font
->header_segment_size
;
320 start
= find_token (font
->header_segment
, segment_end
, name
);
322 return CAIRO_INT_STATUS_UNSUPPORTED
;
324 end
= find_token (start
, segment_end
, "def");
326 return CAIRO_INT_STATUS_UNSUPPORTED
;
328 s_max
= end
- start
+ 5*decimal_point_len
+ 1;
330 if (unlikely (s
== NULL
))
331 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
335 while (i
< end
- start
&& j
< s_max
- decimal_point_len
) {
336 if (start
[i
] == '.') {
337 strncpy(s
+ j
, decimal_point
, decimal_point_len
);
339 j
+= decimal_point_len
;
346 start
= strpbrk (s
, "{[");
349 return CAIRO_INT_STATUS_UNSUPPORTED
;
355 ret
= sscanf(start
, "%lf %lf %lf %lf", a
, b
, c
, d
);
360 return CAIRO_INT_STATUS_UNSUPPORTED
;
362 return CAIRO_STATUS_SUCCESS
;
365 static cairo_status_t
366 cairo_type1_font_subset_get_bbox (cairo_type1_font_subset_t
*font
)
368 cairo_status_t status
;
369 double x_min
, y_min
, x_max
, y_max
;
370 double xx
, yx
, xy
, yy
;
372 status
= cairo_type1_font_subset_get_matrix (font
, "/FontBBox",
377 if (unlikely (status
))
380 status
= cairo_type1_font_subset_get_matrix (font
, "/FontMatrix",
382 if (unlikely (status
))
386 return CAIRO_INT_STATUS_UNSUPPORTED
;
388 /* Freetype uses 1/yy to get units per EM */
389 font
->base
.units_per_em
= 1.0/yy
;
391 font
->base
.x_min
= x_min
/ font
->base
.units_per_em
;
392 font
->base
.y_min
= y_min
/ font
->base
.units_per_em
;
393 font
->base
.x_max
= x_max
/ font
->base
.units_per_em
;
394 font
->base
.y_max
= y_max
/ font
->base
.units_per_em
;
395 font
->base
.ascent
= font
->base
.y_max
;
396 font
->base
.descent
= font
->base
.y_min
;
398 return CAIRO_STATUS_SUCCESS
;
401 static cairo_status_t
402 cairo_type1_font_subset_get_fontname (cairo_type1_font_subset_t
*font
)
404 const char *start
, *end
, *segment_end
;
407 cairo_status_t status
;
409 segment_end
= font
->header_segment
+ font
->header_segment_size
;
410 start
= find_token (font
->header_segment
, segment_end
, "/FontName");
412 return CAIRO_INT_STATUS_UNSUPPORTED
;
414 start
+= strlen ("/FontName");
416 end
= find_token (start
, segment_end
, "def");
418 return CAIRO_INT_STATUS_UNSUPPORTED
;
420 while (end
> start
&& _cairo_isspace(end
[-1]))
423 s
= malloc (end
- start
+ 1);
424 if (unlikely (s
== NULL
))
425 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
427 strncpy (s
, start
, end
- start
);
430 start
= strchr (s
, '/');
431 if (!start
++ || !start
) {
433 return CAIRO_INT_STATUS_UNSUPPORTED
;
436 /* If font name is prefixed with a subset tag, strip it off. */
437 if (strlen(start
) > 7 && start
[6] == '+') {
438 for (i
= 0; i
< 6; i
++) {
439 if (start
[i
] < 'A' || start
[i
] > 'Z')
446 font
->base
.base_font
= strdup (start
);
448 if (unlikely (font
->base
.base_font
== NULL
))
449 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
451 status
= _cairo_escape_ps_name (&font
->base
.base_font
);
456 static cairo_status_t
457 cairo_type1_font_subset_write_header (cairo_type1_font_subset_t
*font
,
460 const char *start
, *end
, *segment_end
;
464 * This function assumes that /FontName always appears
465 * before /Encoding. This appears to always be the case with Type1
468 * The more recently added code for removing the UniqueID and XUID
469 * keys can not make any assumptions about the position of the
470 * keys in the dictionary so it is implemented by overwriting the
471 * key definition with spaces before we start copying the font to
474 * This code should be rewritten to not make any assumptions about
475 * the order of dictionary keys. This will allow UniqueID to be
476 * stripped out instead of leaving a bunch of spaces in the
479 cairo_type1_font_erase_dict_key (font
, "/UniqueID");
480 cairo_type1_font_erase_dict_key (font
, "/XUID");
482 segment_end
= font
->header_segment
+ font
->header_segment_size
;
484 /* Type 1 fonts created by Fontforge have some PostScript code at
485 * the start of the font that skips the font if the printer has a
486 * cached copy of the font with the same unique id. This breaks
487 * our subsetted font so we disable it by searching for the
488 * PostScript operator "known" when used to check for the
489 * "/UniqueID" dictionary key. We append " pop false " after it to
490 * pop the result of this check off the stack and replace it with
491 * "false" to make the PostScript code think "/UniqueID" does not
494 end
= font
->header_segment
;
495 start
= find_token (font
->header_segment
, segment_end
, "/UniqueID");
498 while (start
< segment_end
&& _cairo_isspace (*start
))
500 if (start
+ 5 < segment_end
&& memcmp(start
, "known", 5) == 0) {
501 _cairo_output_stream_write (font
->output
, font
->header_segment
,
502 start
+ 5 - font
->header_segment
);
503 _cairo_output_stream_printf (font
->output
, " pop false ");
508 start
= find_token (end
, segment_end
, "/FontName");
510 return CAIRO_INT_STATUS_UNSUPPORTED
;
512 _cairo_output_stream_write (font
->output
, end
,
515 _cairo_output_stream_printf (font
->output
, "/FontName /%s def", name
);
517 end
= find_token (start
, segment_end
, "def");
519 return CAIRO_INT_STATUS_UNSUPPORTED
;
522 start
= find_token (end
, segment_end
, "/Encoding");
524 return CAIRO_INT_STATUS_UNSUPPORTED
;
525 _cairo_output_stream_write (font
->output
, end
, start
- end
);
527 _cairo_output_stream_printf (font
->output
,
528 "/Encoding 256 array\n"
529 "0 1 255 {1 index exch /.notdef put} for\n");
530 if (font
->scaled_font_subset
->is_latin
) {
531 for (i
= 1; i
< 256; i
++) {
532 int subset_glyph
= font
->scaled_font_subset
->latin_to_subset_glyph_index
[i
];
534 if (subset_glyph
> 0) {
535 _cairo_output_stream_printf (font
->output
,
538 _cairo_winansi_to_glyphname (i
));
542 for (i
= 0; i
< font
->base
.num_glyphs
; i
++) {
543 if (font
->glyphs
[i
].subset_index
<= 0)
545 _cairo_output_stream_printf (font
->output
,
547 font
->glyphs
[i
].subset_index
,
548 font
->glyph_names
[i
]);
551 _cairo_output_stream_printf (font
->output
, "readonly def");
553 end
= find_token (start
, segment_end
, "def");
555 return CAIRO_INT_STATUS_UNSUPPORTED
;
558 /* There are some buggy fonts that contain more than one /Encoding */
559 if (find_token (end
, segment_end
, "/Encoding"))
560 return CAIRO_INT_STATUS_UNSUPPORTED
;
562 _cairo_output_stream_write (font
->output
, end
, segment_end
- end
);
564 return font
->output
->status
;
573 return ch
- 'A' + 10;
575 return ch
- 'a' + 10;
578 static cairo_status_t
579 cairo_type1_font_subset_write_encrypted (cairo_type1_font_subset_t
*font
,
580 const char *data
, unsigned int length
)
582 const unsigned char *in
, *end
;
584 static const char hex_digits
[16] = "0123456789abcdef";
587 in
= (const unsigned char *) data
;
588 end
= (const unsigned char *) data
+ length
;
591 c
= p
^ (font
->eexec_key
>> 8);
592 font
->eexec_key
= (c
+ font
->eexec_key
) * CAIRO_TYPE1_ENCRYPT_C1
+ CAIRO_TYPE1_ENCRYPT_C2
;
594 if (font
->hex_encode
) {
595 digits
[0] = hex_digits
[c
>> 4];
596 digits
[1] = hex_digits
[c
& 0x0f];
598 font
->hex_column
+= 2;
600 if (font
->hex_column
== 78) {
601 _cairo_output_stream_write (font
->output
, digits
, 3);
602 font
->hex_column
= 0;
604 _cairo_output_stream_write (font
->output
, digits
, 2);
608 _cairo_output_stream_write (font
->output
, digits
, 1);
612 return font
->output
->status
;
615 static cairo_status_t
616 cairo_type1_font_subset_decrypt_eexec_segment (cairo_type1_font_subset_t
*font
)
618 unsigned short r
= CAIRO_TYPE1_PRIVATE_DICT_KEY
;
619 unsigned char *in
, *end
;
624 in
= (unsigned char *) font
->eexec_segment
;
625 end
= (unsigned char *) in
+ font
->eexec_segment_size
;
627 font
->cleartext
= malloc (font
->eexec_segment_size
+ 1);
628 if (unlikely (font
->cleartext
== NULL
))
629 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
631 out
= font
->cleartext
;
633 if (font
->eexec_segment_is_ascii
) {
635 if (_cairo_isspace (c
))
637 c
= (hex_to_int (c
) << 4) | hex_to_int (*in
++);
642 r
= (c
+ r
) * CAIRO_TYPE1_ENCRYPT_C1
+ CAIRO_TYPE1_ENCRYPT_C2
;
646 font
->cleartext_end
= out
;
648 /* Overwrite random bytes with spaces.
650 * The first 4 bytes of the cleartext are the random bytes
651 * required by the encryption algorithm. When encrypting the
652 * cleartext, the first ciphertext byte must not be a white space
653 * character and the first 4 bytes must not be an ASCII Hex
654 * character. Some fonts do not check that their randomly chosen
655 * bytes results in ciphertext that complies with this
656 * restriction. This may cause problems for some PDF consumers. By
657 * replacing the random bytes with spaces, the first four bytes of
658 * ciphertext will always be 0xf9, 0x83, 0xef, 0x00 which complies
659 * with this restriction. Using spaces also means we don't have to
660 * skip over the random bytes when parsing the cleartext.
662 for (i
= 0; i
< 4 && i
< font
->eexec_segment_size
; i
++)
663 font
->cleartext
[i
] = ' ';
665 /* Ensure strtol() can not scan past the end of the cleartext */
666 font
->cleartext
[font
->eexec_segment_size
] = 0;
668 return CAIRO_STATUS_SUCCESS
;
672 skip_token (const char *p
, const char *end
)
674 while (p
< end
&& _cairo_isspace(*p
))
677 while (p
< end
&& !_cairo_isspace(*p
))
687 cairo_type1_font_subset_decrypt_charstring (const unsigned char *in
, int size
, unsigned char *out
)
689 unsigned short r
= CAIRO_TYPE1_CHARSTRING_KEY
;
692 for (i
= 0; i
< size
; i
++) {
695 r
= (c
+ r
) * CAIRO_TYPE1_ENCRYPT_C1
+ CAIRO_TYPE1_ENCRYPT_C2
;
700 static const unsigned char *
701 cairo_type1_font_subset_decode_integer (const unsigned char *p
, int *integer
)
704 *integer
= *p
++ - 139;
705 } else if (*p
<= 250) {
706 *integer
= (p
[0] - 247) * 256 + p
[1] + 108;
708 } else if (*p
<= 254) {
709 *integer
= -(p
[0] - 251) * 256 - p
[1] - 108;
712 *integer
= (p
[1] << 24) | (p
[2] << 16) | (p
[3] << 8) | p
[4];
719 static cairo_status_t
720 use_standard_encoding_glyph (cairo_type1_font_subset_t
*font
, int index
)
722 const char *glyph_name
;
725 if (index
< 0 || index
> 255)
726 return CAIRO_STATUS_SUCCESS
;
728 glyph_name
= _cairo_ps_standard_encoding_to_glyphname (index
);
729 if (glyph_name
== NULL
)
730 return CAIRO_STATUS_SUCCESS
;
732 for (i
= 0; i
< font
->base
.num_glyphs
; i
++) {
733 if (font
->glyph_names
[i
] && strcmp (font
->glyph_names
[i
], glyph_name
) == 0) {
734 cairo_type1_font_subset_use_glyph (font
, i
);
736 return CAIRO_STATUS_SUCCESS
;
740 return CAIRO_INT_STATUS_UNSUPPORTED
;
744 #define TYPE1_CHARSTRING_COMMAND_HSTEM 0x01
745 #define TYPE1_CHARSTRING_COMMAND_VSTEM 0x03
746 #define TYPE1_CHARSTRING_COMMAND_VMOVETO 0x04
747 #define TYPE1_CHARSTRING_COMMAND_RLINETO 0x05
748 #define TYPE1_CHARSTRING_COMMAND_HLINETO 0x06
749 #define TYPE1_CHARSTRING_COMMAND_VLINETO 0x07
750 #define TYPE1_CHARSTRING_COMMAND_RRCURVETO 0x08
751 #define TYPE1_CHARSTRING_COMMAND_CLOSEPATH 0x09
752 #define TYPE1_CHARSTRING_COMMAND_CALLSUBR 0x0a
753 #define TYPE1_CHARSTRING_COMMAND_RETURN 0x0b
754 #define TYPE1_CHARSTRING_COMMAND_ESCAPE 0x0c
755 #define TYPE1_CHARSTRING_COMMAND_HSBW 0x0d
756 #define TYPE1_CHARSTRING_COMMAND_ENDCHAR 0x0e
757 #define TYPE1_CHARSTRING_COMMAND_RMOVETO 0x15
758 #define TYPE1_CHARSTRING_COMMAND_HMOVETO 0x16
759 #define TYPE1_CHARSTRING_COMMAND_VHCURVETO 0x1e
760 #define TYPE1_CHARSTRING_COMMAND_HVCURVETO 0x1f
761 #define TYPE1_CHARSTRING_COMMAND_DOTSECTION 0x0c00
762 #define TYPE1_CHARSTRING_COMMAND_VSTEM3 0x0c01
763 #define TYPE1_CHARSTRING_COMMAND_HSTEM3 0x0c02
764 #define TYPE1_CHARSTRING_COMMAND_SEAC 0x0c06
765 #define TYPE1_CHARSTRING_COMMAND_SBW 0x0c07
766 #define TYPE1_CHARSTRING_COMMAND_DIV 0x0c0c
767 #define TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR 0x0c10
768 #define TYPE1_CHARSTRING_COMMAND_POP 0x0c11
769 #define TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT 0x0c21
771 /* Parse the charstring, including recursing into subroutines. Find
772 * the glyph width, subroutines called, and glyphs required by the
774 static cairo_status_t
775 cairo_type1_font_subset_parse_charstring (cairo_type1_font_subset_t
*font
,
777 const char *encrypted_charstring
,
778 int encrypted_charstring_length
)
780 cairo_status_t status
;
781 unsigned char *charstring
;
782 const unsigned char *end
;
783 const unsigned char *p
;
786 charstring
= malloc (encrypted_charstring_length
);
787 if (unlikely (charstring
== NULL
))
788 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
790 cairo_type1_font_subset_decrypt_charstring ((const unsigned char *)
791 encrypted_charstring
,
792 encrypted_charstring_length
,
794 end
= charstring
+ encrypted_charstring_length
;
795 p
= charstring
+ font
->lenIV
;
796 status
= CAIRO_STATUS_SUCCESS
;
801 case TYPE1_CHARSTRING_COMMAND_HSTEM
:
802 case TYPE1_CHARSTRING_COMMAND_VSTEM
:
803 case TYPE1_CHARSTRING_COMMAND_VMOVETO
:
804 case TYPE1_CHARSTRING_COMMAND_RLINETO
:
805 case TYPE1_CHARSTRING_COMMAND_HLINETO
:
806 case TYPE1_CHARSTRING_COMMAND_VLINETO
:
807 case TYPE1_CHARSTRING_COMMAND_RRCURVETO
:
808 case TYPE1_CHARSTRING_COMMAND_CLOSEPATH
:
809 case TYPE1_CHARSTRING_COMMAND_RMOVETO
:
810 case TYPE1_CHARSTRING_COMMAND_HMOVETO
:
811 case TYPE1_CHARSTRING_COMMAND_VHCURVETO
:
812 case TYPE1_CHARSTRING_COMMAND_HVCURVETO
:
813 case TYPE1_CHARSTRING_COMMAND_RETURN
:
814 case TYPE1_CHARSTRING_COMMAND_ENDCHAR
:
816 /* stack clearing operator */
817 font
->build_stack
.sp
= 0;
820 case TYPE1_CHARSTRING_COMMAND_CALLSUBR
:
821 if (font
->subset_subrs
&& font
->build_stack
.sp
> 0) {
823 if (modf(font
->build_stack
.stack
[--font
->build_stack
.sp
], &int_val
) == 0.0) {
824 int subr_num
= int_val
;
825 if (subr_num
>= 0 && subr_num
< font
->num_subrs
) {
826 font
->subrs
[subr_num
].used
= TRUE
;
827 status
= cairo_type1_font_subset_parse_charstring (
830 font
->subrs
[subr_num
].subr_string
,
831 font
->subrs
[subr_num
].subr_length
);
836 font
->subset_subrs
= FALSE
;
839 case TYPE1_CHARSTRING_COMMAND_HSBW
:
840 if (font
->build_stack
.sp
< 2) {
841 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
845 font
->glyphs
[glyph
].width
= font
->build_stack
.stack
[1]/font
->base
.units_per_em
;
846 font
->build_stack
.sp
= 0;
849 case TYPE1_CHARSTRING_COMMAND_ESCAPE
:
850 command
= command
<< 8 | *p
++;
852 case TYPE1_CHARSTRING_COMMAND_DOTSECTION
:
853 case TYPE1_CHARSTRING_COMMAND_VSTEM3
:
854 case TYPE1_CHARSTRING_COMMAND_HSTEM3
:
855 case TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT
:
857 /* stack clearing operator */
858 font
->build_stack
.sp
= 0;
861 case TYPE1_CHARSTRING_COMMAND_SEAC
:
862 /* The seac command takes five integer arguments. The
863 * last two are glyph indices into the PS standard
864 * encoding give the names of the glyphs that this
865 * glyph is composed from. All we need to do is to
866 * make sure those glyphs are present in the subset
867 * under their standard names. */
868 if (font
->build_stack
.sp
< 5) {
869 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
873 status
= use_standard_encoding_glyph (font
, font
->build_stack
.stack
[3]);
874 if (unlikely (status
))
877 status
= use_standard_encoding_glyph (font
, font
->build_stack
.stack
[4]);
878 if (unlikely (status
))
881 font
->build_stack
.sp
= 0;
884 case TYPE1_CHARSTRING_COMMAND_SBW
:
885 if (font
->build_stack
.sp
< 4) {
886 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
890 font
->glyphs
[glyph
].width
= font
->build_stack
.stack
[2]/font
->base
.units_per_em
;
891 font
->build_stack
.sp
= 0;
894 case TYPE1_CHARSTRING_COMMAND_DIV
:
895 if (font
->build_stack
.sp
< 2) {
896 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
899 double num1
= font
->build_stack
.stack
[font
->build_stack
.sp
- 2];
900 double num2
= font
->build_stack
.stack
[font
->build_stack
.sp
- 1];
901 font
->build_stack
.sp
--;
903 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
906 font
->build_stack
.stack
[font
->build_stack
.sp
- 1] = num1
/num2
;
910 case TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR
:
911 if (font
->build_stack
.sp
< 1) {
912 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
916 font
->build_stack
.sp
--;
917 font
->ps_stack
.sp
= 0;
918 while (font
->build_stack
.sp
)
919 font
->ps_stack
.stack
[font
->ps_stack
.sp
++] = font
->build_stack
.stack
[--font
->build_stack
.sp
];
923 case TYPE1_CHARSTRING_COMMAND_POP
:
924 if (font
->ps_stack
.sp
< 1) {
925 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
929 /* T1 spec states that if the interpreter does not
930 * support executing the callothersub, the results
931 * must be taken from the callothersub arguments. */
932 font
->build_stack
.stack
[font
->build_stack
.sp
++] = font
->ps_stack
.stack
[--font
->ps_stack
.sp
];
938 /* integer argument */
939 if (font
->build_stack
.sp
< TYPE1_STACKSIZE
) {
941 p
= cairo_type1_font_subset_decode_integer (p
, &val
);
942 font
->build_stack
.stack
[font
->build_stack
.sp
++] = val
;
944 status
= CAIRO_INT_STATUS_UNSUPPORTED
;
956 static cairo_status_t
957 cairo_type1_font_subset_build_subr_list (cairo_type1_font_subset_t
*font
,
959 const char *encrypted_charstring
, int encrypted_charstring_length
,
960 const char *np
, int np_length
)
963 font
->subrs
[subr_number
].subr_string
= encrypted_charstring
;
964 font
->subrs
[subr_number
].subr_length
= encrypted_charstring_length
;
965 font
->subrs
[subr_number
].np
= np
;
966 font
->subrs
[subr_number
].np_length
= np_length
;
968 return CAIRO_STATUS_SUCCESS
;
971 static cairo_status_t
972 write_used_subrs (cairo_type1_font_subset_t
*font
,
974 const char *subr_string
, int subr_string_length
,
975 const char *np
, int np_length
)
977 cairo_status_t status
;
981 if (!font
->subrs
[subr_number
].used
)
982 return CAIRO_STATUS_SUCCESS
;
984 length
= snprintf (buffer
, sizeof buffer
,
986 subr_number
, subr_string_length
, font
->rd
);
987 status
= cairo_type1_font_subset_write_encrypted (font
, buffer
, length
);
988 if (unlikely (status
))
991 status
= cairo_type1_font_subset_write_encrypted (font
,
994 if (unlikely (status
))
998 status
= cairo_type1_font_subset_write_encrypted (font
, np
, np_length
);
1000 length
= snprintf (buffer
, sizeof buffer
, "%s\n", font
->np
);
1001 status
= cairo_type1_font_subset_write_encrypted (font
, buffer
, length
);
1003 if (unlikely (status
))
1006 return CAIRO_STATUS_SUCCESS
;
1009 typedef cairo_status_t (*subr_func_t
) (cairo_type1_font_subset_t
*font
,
1011 const char *subr_string
, int subr_string_length
,
1012 const char *np
, int np_length
);
1014 static cairo_status_t
1015 cairo_type1_font_for_each_subr (cairo_type1_font_subset_t
*font
,
1016 const char *array_start
,
1017 const char *cleartext_end
,
1019 const char **array_end
)
1021 const char *p
, *subr_string
;
1023 int subr_num
, subr_length
;
1026 cairo_status_t status
;
1028 /* We're looking at "dup" at the start of the first subroutine. The subroutines
1029 * definitions are on the form:
1031 * dup 5 23 RD <23 binary bytes> NP
1033 * or alternatively using -| and |- instead of RD and ND.
1034 * The first number is the subroutine number.
1038 while (p
+ 3 < cleartext_end
&& strncmp (p
, "dup", 3) == 0) {
1039 p
= skip_token (p
, cleartext_end
);
1041 /* get subr number */
1042 subr_num
= strtol (p
, &end
, 10);
1044 return CAIRO_INT_STATUS_UNSUPPORTED
;
1046 if (subr_num
< 0 || subr_num
>= font
->num_subrs
)
1047 return CAIRO_INT_STATUS_UNSUPPORTED
;
1049 /* get subr length */
1051 subr_length
= strtol (p
, &end
, 10);
1053 return CAIRO_INT_STATUS_UNSUPPORTED
;
1055 /* Skip past -| or RD to binary data. There is exactly one space
1056 * between the -| or RD token and the encrypted data, thus '+ 1'. */
1057 subr_string
= skip_token (end
, cleartext_end
) + 1;
1062 /* Skip binary data and | or NP token. */
1063 p
= skip_token (subr_string
+ subr_length
, cleartext_end
);
1064 while (p
< cleartext_end
&& _cairo_isspace(*p
))
1067 /* Some fonts have "noaccess put" instead of "NP" */
1068 if (p
+ 3 < cleartext_end
&& strncmp (p
, "put", 3) == 0) {
1069 p
= skip_token (p
, cleartext_end
);
1070 while (p
< cleartext_end
&& _cairo_isspace(*p
))
1073 np
= subr_string
+ subr_length
;
1077 status
= func (font
, subr_num
,
1078 subr_string
, subr_length
, np
, np_length
);
1079 if (unlikely (status
))
1084 *array_end
= (char *) p
;
1086 return CAIRO_STATUS_SUCCESS
;
1089 static cairo_status_t
1090 cairo_type1_font_subset_build_glyph_list (cairo_type1_font_subset_t
*font
,
1092 const char *name
, int name_length
,
1093 const char *encrypted_charstring
, int encrypted_charstring_length
)
1097 cairo_status_t status
;
1099 s
= malloc (name_length
+ 1);
1100 if (unlikely (s
== NULL
))
1101 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1103 strncpy (s
, name
, name_length
);
1106 status
= _cairo_array_append (&font
->glyph_names_array
, &s
);
1107 if (unlikely (status
))
1110 glyph
.subset_index
= -1;
1112 glyph
.encrypted_charstring
= encrypted_charstring
;
1113 glyph
.encrypted_charstring_length
= encrypted_charstring_length
;
1114 status
= _cairo_array_append (&font
->glyphs_array
, &glyph
);
1119 static cairo_status_t
1120 write_used_glyphs (cairo_type1_font_subset_t
*font
,
1122 const char *name
, int name_length
,
1123 const char *charstring
, int charstring_length
)
1125 cairo_status_t status
;
1128 unsigned int subset_id
;
1130 const char *wa_name
;
1132 if (font
->glyphs
[glyph_number
].subset_index
< 0)
1133 return CAIRO_STATUS_SUCCESS
;
1135 if (font
->scaled_font_subset
->is_latin
) {
1136 /* When using the WinAnsi encoding in PDF, the /Encoding array
1137 * is ignored and instead glyphs are keyed by glyph names. To
1138 * ensure correct rendering we replace the glyph name in the
1139 * font with the standard name.
1141 subset_id
= font
->glyphs
[glyph_number
].subset_index
;
1142 /* Any additional glyph included for use by the seac operator
1143 * will either have subset_id >= font->scaled_font_subset->num_glyphs
1144 * or will not map to a winansi name (wa_name = NULL). In this
1145 * case the original name is used.
1147 if (subset_id
> 0 && subset_id
< font
->scaled_font_subset
->num_glyphs
) {
1148 ch
= font
->scaled_font_subset
->to_latin_char
[subset_id
];
1149 wa_name
= _cairo_winansi_to_glyphname (ch
);
1152 name_length
= strlen(name
);
1157 length
= snprintf (buffer
, sizeof buffer
,
1159 name_length
, name
, charstring_length
, font
->rd
);
1160 status
= cairo_type1_font_subset_write_encrypted (font
, buffer
, length
);
1161 if (unlikely (status
))
1164 status
= cairo_type1_font_subset_write_encrypted (font
,
1167 if (unlikely (status
))
1170 length
= snprintf (buffer
, sizeof buffer
, "%s\n", font
->nd
);
1171 status
= cairo_type1_font_subset_write_encrypted (font
, buffer
, length
);
1172 if (unlikely (status
))
1175 return CAIRO_STATUS_SUCCESS
;
1178 typedef cairo_status_t (*glyph_func_t
) (cairo_type1_font_subset_t
*font
,
1180 const char *name
, int name_length
,
1181 const char *charstring
, int charstring_length
);
1183 static cairo_status_t
1184 cairo_type1_font_subset_for_each_glyph (cairo_type1_font_subset_t
*font
,
1185 const char *dict_start
,
1186 const char *dict_end
,
1188 const char **dict_out
)
1190 int charstring_length
, name_length
;
1191 const char *p
, *charstring
, *name
;
1193 cairo_status_t status
;
1196 /* We're looking at '/' in the name of the first glyph. The glyph
1197 * definitions are on the form:
1199 * /name 23 RD <23 binary bytes> ND
1201 * or alternatively using -| and |- instead of RD and ND.
1203 * We parse the glyph name and see if it is in the subset. If it
1204 * is, we call the specified callback with the glyph name and
1205 * glyph data, otherwise we just skip it. We need to parse
1206 * through a glyph definition; we can't just find the next '/',
1207 * since the binary data could contain a '/'.
1214 p
= skip_token (p
, dict_end
);
1215 name_length
= p
- name
;
1217 charstring_length
= strtol (p
, &end
, 10);
1219 return CAIRO_INT_STATUS_UNSUPPORTED
;
1221 /* Skip past -| or RD to binary data. There is exactly one space
1222 * between the -| or RD token and the encrypted data, thus '+ 1'. */
1223 charstring
= skip_token (end
, dict_end
) + 1;
1225 /* Skip binary data and |- or ND token. */
1226 p
= skip_token (charstring
+ charstring_length
, dict_end
);
1227 while (p
< dict_end
&& _cairo_isspace(*p
))
1230 /* In case any of the skip_token() calls above reached EOF, p will
1231 * be equal to dict_end. */
1233 return CAIRO_INT_STATUS_UNSUPPORTED
;
1235 status
= func (font
, glyph_count
++,
1237 charstring
, charstring_length
);
1238 if (unlikely (status
))
1244 return CAIRO_STATUS_SUCCESS
;
1248 static cairo_status_t
1249 cairo_type1_font_subset_write_private_dict (cairo_type1_font_subset_t
*font
,
1252 cairo_status_t status
;
1253 const char *p
, *subrs
, *charstrings
, *array_start
, *array_end
, *dict_start
, *dict_end
;
1254 const char *lenIV_start
, *lenIV_end
, *closefile_token
;
1255 char buffer
[32], *lenIV_str
, *subr_count_end
, *glyph_count_end
;
1256 int ret
, lenIV
, length
;
1257 const cairo_scaled_font_backend_t
*backend
;
1261 /* The private dict holds hint information, common subroutines and
1262 * the actual glyph definitions (charstrings).
1264 * What we do here is scan directly to the /Subrs token, which
1265 * marks the beginning of the subroutines. We read in all the
1266 * subroutines, then move on to the /CharString token, which marks
1267 * the beginning of the glyph definitions, and read in the charstrings.
1269 * The charstrings are parsed to extract glyph widths, work out
1270 * which subroutines are called, and to see if any extra glyphs
1271 * need to be included due to the use of the seac glyph combining
1274 * Finally, the private dict is copied to the subset font minus the
1275 * subroutines and charstrings not required.
1278 /* Determine lenIV, the number of random characters at the start of
1279 each encrypted charstring. The default is 4, but this can be
1280 overridden in the private dict. */
1282 if ((lenIV_start
= find_token (font
->cleartext
, font
->cleartext_end
, "/lenIV")) != NULL
) {
1284 lenIV_end
= find_token (lenIV_start
, font
->cleartext_end
, "def");
1285 if (lenIV_end
== NULL
)
1286 return CAIRO_INT_STATUS_UNSUPPORTED
;
1288 lenIV_str
= malloc (lenIV_end
- lenIV_start
+ 1);
1289 if (unlikely (lenIV_str
== NULL
))
1290 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1292 strncpy (lenIV_str
, lenIV_start
, lenIV_end
- lenIV_start
);
1293 lenIV_str
[lenIV_end
- lenIV_start
] = 0;
1295 ret
= sscanf(lenIV_str
, "%d", &lenIV
);
1298 if (unlikely (ret
<= 0))
1299 return CAIRO_INT_STATUS_UNSUPPORTED
;
1301 /* Apparently some fonts signal unencrypted charstrings with a negative lenIV,
1302 though this is not part of the Type 1 Font Format specification. See, e.g.
1303 http://lists.gnu.org/archive/html/freetype-devel/2000-06/msg00064.html. */
1304 if (unlikely (lenIV
< 0))
1305 return CAIRO_INT_STATUS_UNSUPPORTED
;
1307 font
->lenIV
= lenIV
;
1310 /* Find start of Subrs */
1311 subrs
= find_token (font
->cleartext
, font
->cleartext_end
, "/Subrs");
1312 if (subrs
== NULL
) {
1313 font
->subset_subrs
= FALSE
;
1314 p
= font
->cleartext
;
1319 /* Scan past /Subrs and get the array size. */
1320 p
= subrs
+ strlen ("/Subrs");
1321 font
->num_subrs
= strtol (p
, &subr_count_end
, 10);
1322 if (subr_count_end
== p
)
1323 return CAIRO_INT_STATUS_UNSUPPORTED
;
1325 if (font
->num_subrs
<= 0)
1326 return CAIRO_INT_STATUS_UNSUPPORTED
;
1328 font
->subrs
= calloc (font
->num_subrs
, sizeof (font
->subrs
[0]));
1329 if (unlikely (font
->subrs
== NULL
))
1330 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1332 /* look for "dup" which marks the beginning of the first subr */
1333 array_start
= find_token (subr_count_end
, font
->cleartext_end
, "dup");
1335 return CAIRO_INT_STATUS_UNSUPPORTED
;
1337 /* Read in the subroutines */
1338 status
= cairo_type1_font_for_each_subr (font
,
1340 font
->cleartext_end
,
1341 cairo_type1_font_subset_build_subr_list
,
1343 if (unlikely(status
))
1349 /* Find start of CharStrings */
1350 charstrings
= find_token (p
, font
->cleartext_end
, "/CharStrings");
1351 if (charstrings
== NULL
)
1352 return CAIRO_INT_STATUS_UNSUPPORTED
;
1354 /* Scan past /CharStrings and the integer following it. */
1355 p
= charstrings
+ strlen ("/CharStrings");
1356 strtol (p
, &glyph_count_end
, 10);
1357 if (p
== glyph_count_end
)
1358 return CAIRO_INT_STATUS_UNSUPPORTED
;
1360 /* Look for a '/' which marks the beginning of the first glyph
1362 for (p
= glyph_count_end
; p
< font
->cleartext_end
; p
++)
1365 if (p
== font
->cleartext_end
)
1366 return CAIRO_INT_STATUS_UNSUPPORTED
;
1369 /* Now that we have the private dictionary broken down in
1370 * sections, do the first pass through the glyph definitions to
1371 * build a list of glyph names and charstrings. */
1372 status
= cairo_type1_font_subset_for_each_glyph (font
,
1374 font
->cleartext_end
,
1375 cairo_type1_font_subset_build_glyph_list
,
1377 if (unlikely(status
))
1380 font
->glyphs
= _cairo_array_index (&font
->glyphs_array
, 0);
1381 font
->glyph_names
= _cairo_array_index (&font
->glyph_names_array
, 0);
1382 font
->base
.num_glyphs
= _cairo_array_num_elements (&font
->glyphs_array
);
1383 font
->subset_index_to_glyphs
= calloc (font
->base
.num_glyphs
, sizeof font
->subset_index_to_glyphs
[0]);
1384 if (unlikely (font
->subset_index_to_glyphs
== NULL
))
1385 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1387 backend
= font
->scaled_font_subset
->scaled_font
->backend
;
1388 if (!backend
->index_to_glyph_name
)
1389 return CAIRO_INT_STATUS_UNSUPPORTED
;
1391 /* Find the glyph number corresponding to each glyph in the subset
1392 * and mark it as in use */
1394 for (i
= 0; i
< font
->scaled_font_subset
->num_glyphs
; i
++) {
1395 unsigned long index
;
1397 status
= backend
->index_to_glyph_name (font
->scaled_font_subset
->scaled_font
,
1399 font
->base
.num_glyphs
,
1400 font
->scaled_font_subset
->glyphs
[i
],
1402 if (unlikely(status
))
1405 cairo_type1_font_subset_use_glyph (font
, index
);
1408 /* Go through the charstring of each glyph in use, get the glyph
1409 * width and figure out which extra glyphs may be required by the
1410 * seac operator (which may cause font->num_glyphs to increase
1411 * while this loop is executing). Also subset the Subrs. */
1412 for (j
= 0; j
< font
->num_glyphs
; j
++) {
1413 glyph
= font
->subset_index_to_glyphs
[j
];
1414 font
->build_stack
.sp
= 0;
1415 font
->ps_stack
.sp
= 0;
1416 status
= cairo_type1_font_subset_parse_charstring (font
,
1418 font
->glyphs
[glyph
].encrypted_charstring
,
1419 font
->glyphs
[glyph
].encrypted_charstring_length
);
1420 if (unlikely (status
))
1424 /* Always include the first five subroutines in case the Flex/hint mechanism is
1426 for (j
= 0; j
< MIN (font
->num_subrs
, 5); j
++) {
1427 font
->subrs
[j
].used
= TRUE
;
1430 closefile_token
= find_token (dict_end
, font
->cleartext_end
, "closefile");
1431 if (closefile_token
== NULL
)
1432 return CAIRO_INT_STATUS_UNSUPPORTED
;
1434 /* We're ready to start outputting. First write the header,
1435 * i.e. the public part of the font dict.*/
1436 status
= cairo_type1_font_subset_write_header (font
, name
);
1437 if (unlikely (status
))
1440 font
->base
.header_size
= _cairo_output_stream_get_position (font
->output
);
1442 /* Start outputting the private dict */
1443 if (font
->subset_subrs
) {
1444 /* First output everything up to the start of the Subrs array. */
1445 status
= cairo_type1_font_subset_write_encrypted (font
, font
->cleartext
,
1446 array_start
- font
->cleartext
);
1447 if (unlikely (status
))
1450 /* Write out the subr definitions for each of the glyphs in
1452 status
= cairo_type1_font_for_each_subr (font
,
1454 font
->cleartext_end
,
1457 if (unlikely (status
))
1460 p
= font
->cleartext
;
1463 /* If subr subsetting, output everything from end of subrs to
1464 * start of /CharStrings token. If not subr subsetting, output
1465 * everything start of private dict to start of /CharStrings
1467 status
= cairo_type1_font_subset_write_encrypted (font
, p
, charstrings
- p
);
1468 if (unlikely (status
))
1471 /* Write out new charstring count */
1472 length
= snprintf (buffer
, sizeof buffer
,
1473 "/CharStrings %d", font
->num_glyphs
);
1474 status
= cairo_type1_font_subset_write_encrypted (font
, buffer
, length
);
1475 if (unlikely (status
))
1478 /* Write out text between the charstring count and the first
1479 * charstring definition */
1480 status
= cairo_type1_font_subset_write_encrypted (font
, glyph_count_end
,
1481 dict_start
- glyph_count_end
);
1482 if (unlikely (status
))
1485 /* Write out the charstring definitions for each of the glyphs in
1487 status
= cairo_type1_font_subset_for_each_glyph (font
,
1489 font
->cleartext_end
,
1492 if (unlikely (status
))
1495 /* Output what's left between the end of the glyph definitions and
1496 * the end of the private dict to the output. */
1497 status
= cairo_type1_font_subset_write_encrypted (font
, p
,
1498 closefile_token
- p
+ strlen ("closefile") + 1);
1499 if (unlikely (status
))
1502 if (font
->hex_encode
)
1503 _cairo_output_stream_write (font
->output
, "\n", 1);
1505 return CAIRO_STATUS_SUCCESS
;
1508 static cairo_status_t
1509 cairo_type1_font_subset_write_trailer(cairo_type1_font_subset_t
*font
)
1511 const char *cleartomark_token
;
1513 static const char zeros
[65] =
1514 "0000000000000000000000000000000000000000000000000000000000000000\n";
1517 for (i
= 0; i
< 8; i
++)
1518 _cairo_output_stream_write (font
->output
, zeros
, sizeof zeros
);
1520 cleartomark_token
= find_token (font
->type1_data
, font
->type1_end
, "cleartomark");
1521 if (cleartomark_token
) {
1522 /* Some fonts have conditional save/restore around the entire
1523 * font dict, so we need to retain whatever postscript code
1524 * that may come after 'cleartomark'. */
1526 _cairo_output_stream_write (font
->output
, cleartomark_token
,
1527 font
->type1_end
- cleartomark_token
);
1528 if (*(font
->type1_end
- 1) != '\n')
1529 _cairo_output_stream_printf (font
->output
, "\n");
1531 } else if (!font
->eexec_segment_is_ascii
) {
1532 /* Fonts embedded in PDF may omit the fixed-content portion
1533 * that includes the 'cleartomark' operator. Type 1 in PDF is
1536 _cairo_output_stream_printf (font
->output
, "cleartomark\n");
1538 return CAIRO_INT_STATUS_UNSUPPORTED
;
1541 /* some fonts do not have a newline at the end of the last line */
1542 _cairo_output_stream_printf (font
->output
, "\n");
1544 return CAIRO_STATUS_SUCCESS
;
1547 static cairo_status_t
1548 type1_font_write (void *closure
, const unsigned char *data
, unsigned int length
)
1550 cairo_type1_font_subset_t
*font
= closure
;
1552 return _cairo_array_append_multiple (&font
->contents
, data
, length
);
1555 static cairo_status_t
1556 cairo_type1_font_subset_write (cairo_type1_font_subset_t
*font
,
1559 cairo_status_t status
;
1561 status
= cairo_type1_font_subset_find_segments (font
);
1562 if (unlikely (status
))
1565 status
= cairo_type1_font_subset_decrypt_eexec_segment (font
);
1566 if (unlikely (status
))
1569 /* Determine which glyph definition delimiters to use. */
1570 if (find_token (font
->cleartext
, font
->cleartext_end
, "/-|") != NULL
) {
1574 } else if (find_token (font
->cleartext
, font
->cleartext_end
, "/RD") != NULL
) {
1579 /* Don't know *what* kind of font this is... */
1580 return CAIRO_INT_STATUS_UNSUPPORTED
;
1583 font
->eexec_key
= CAIRO_TYPE1_PRIVATE_DICT_KEY
;
1584 font
->hex_column
= 0;
1586 status
= cairo_type1_font_subset_get_bbox (font
);
1587 if (unlikely (status
))
1590 status
= cairo_type1_font_subset_get_fontname (font
);
1591 if (unlikely (status
))
1594 status
= cairo_type1_font_subset_write_private_dict (font
, name
);
1595 if (unlikely (status
))
1598 font
->base
.data_size
= _cairo_output_stream_get_position (font
->output
) -
1599 font
->base
.header_size
;
1601 status
= cairo_type1_font_subset_write_trailer (font
);
1602 if (unlikely (status
))
1605 font
->base
.trailer_size
=
1606 _cairo_output_stream_get_position (font
->output
) -
1607 font
->base
.header_size
- font
->base
.data_size
;
1609 return CAIRO_STATUS_SUCCESS
;
1613 check_fontdata_is_type1 (const unsigned char *data
, long length
)
1615 /* Test for Type 1 Binary (PFB) */
1616 if (length
> 2 && data
[0] == 0x80 && data
[1] == 0x01)
1619 /* Test for Type 1 1 ASCII (PFA) */
1620 if (length
> 2 && data
[0] == '%' && data
[1] == '!')
1626 static cairo_status_t
1627 cairo_type1_font_subset_generate (void *abstract_font
,
1631 cairo_type1_font_subset_t
*font
= abstract_font
;
1632 cairo_scaled_font_t
*scaled_font
;
1633 cairo_status_t status
;
1634 unsigned long data_length
;
1636 scaled_font
= font
->scaled_font_subset
->scaled_font
;
1637 if (!scaled_font
->backend
->load_type1_data
)
1638 return CAIRO_INT_STATUS_UNSUPPORTED
;
1640 status
= scaled_font
->backend
->load_type1_data (scaled_font
, 0, NULL
, &data_length
);
1642 return CAIRO_INT_STATUS_UNSUPPORTED
;
1644 font
->type1_length
= data_length
;
1645 font
->type1_data
= malloc (font
->type1_length
);
1646 if (unlikely (font
->type1_data
== NULL
))
1647 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1649 status
= scaled_font
->backend
->load_type1_data (scaled_font
, 0,
1650 (unsigned char *) font
->type1_data
,
1652 if (unlikely (status
))
1655 if (!check_fontdata_is_type1 ((unsigned char *)font
->type1_data
, data_length
))
1656 return CAIRO_INT_STATUS_UNSUPPORTED
;
1658 status
= _cairo_array_grow_by (&font
->contents
, 4096);
1659 if (unlikely (status
))
1662 font
->output
= _cairo_output_stream_create (type1_font_write
, NULL
, font
);
1663 if (unlikely ((status
= font
->output
->status
)))
1666 status
= cairo_type1_font_subset_write (font
, name
);
1667 if (unlikely (status
))
1670 font
->base
.data
= _cairo_array_index (&font
->contents
, 0);
1675 static cairo_status_t
1676 _cairo_type1_font_subset_fini (cairo_type1_font_subset_t
*font
)
1678 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
1681 /* If the subset generation failed, some of the pointers below may
1682 * be NULL depending on at which point the error occurred. */
1684 _cairo_array_fini (&font
->contents
);
1686 free (font
->type1_data
);
1687 for (i
= 0; i
< _cairo_array_num_elements (&font
->glyph_names_array
); i
++) {
1690 s
= _cairo_array_index (&font
->glyph_names_array
, i
);
1693 _cairo_array_fini (&font
->glyph_names_array
);
1694 _cairo_array_fini (&font
->glyphs_array
);
1698 if (font
->output
!= NULL
)
1699 status
= _cairo_output_stream_destroy (font
->output
);
1701 free (font
->base
.base_font
);
1703 free (font
->subset_index_to_glyphs
);
1705 free (font
->cleartext
);
1711 _cairo_type1_subset_init (cairo_type1_subset_t
*type1_subset
,
1713 cairo_scaled_font_subset_t
*scaled_font_subset
,
1714 cairo_bool_t hex_encode
)
1716 cairo_type1_font_subset_t font
;
1717 cairo_status_t status
;
1718 unsigned long length
;
1722 /* We need to use a fallback font generated from the synthesized outlines. */
1723 if (scaled_font_subset
->scaled_font
->backend
->is_synthetic
&&
1724 scaled_font_subset
->scaled_font
->backend
->is_synthetic (scaled_font_subset
->scaled_font
))
1725 return CAIRO_INT_STATUS_UNSUPPORTED
;
1727 status
= _cairo_type1_font_subset_init (&font
, scaled_font_subset
, hex_encode
);
1728 if (unlikely (status
))
1731 status
= cairo_type1_font_subset_generate (&font
, name
);
1732 if (unlikely (status
))
1735 if (font
.base
.base_font
) {
1736 type1_subset
->base_font
= strdup (font
.base
.base_font
);
1738 snprintf(buf
, sizeof (buf
), "CairoFont-%u-%u",
1739 scaled_font_subset
->font_id
, scaled_font_subset
->subset_id
);
1740 type1_subset
->base_font
= strdup (buf
);
1742 if (unlikely (type1_subset
->base_font
== NULL
))
1745 type1_subset
->widths
= calloc (sizeof (double), font
.num_glyphs
);
1746 if (unlikely (type1_subset
->widths
== NULL
))
1748 for (i
= 0; i
< font
.base
.num_glyphs
; i
++) {
1749 if (font
.glyphs
[i
].subset_index
< 0)
1751 type1_subset
->widths
[font
.glyphs
[i
].subset_index
] =
1752 font
.glyphs
[i
].width
;
1755 type1_subset
->x_min
= font
.base
.x_min
;
1756 type1_subset
->y_min
= font
.base
.y_min
;
1757 type1_subset
->x_max
= font
.base
.x_max
;
1758 type1_subset
->y_max
= font
.base
.y_max
;
1759 type1_subset
->ascent
= font
.base
.ascent
;
1760 type1_subset
->descent
= font
.base
.descent
;
1762 length
= font
.base
.header_size
+
1763 font
.base
.data_size
+
1764 font
.base
.trailer_size
;
1765 type1_subset
->data
= malloc (length
);
1766 if (unlikely (type1_subset
->data
== NULL
))
1769 memcpy (type1_subset
->data
,
1770 _cairo_array_index (&font
.contents
, 0), length
);
1772 type1_subset
->header_length
= font
.base
.header_size
;
1773 type1_subset
->data_length
= font
.base
.data_size
;
1774 type1_subset
->trailer_length
= font
.base
.trailer_size
;
1776 return _cairo_type1_font_subset_fini (&font
);
1779 free (type1_subset
->widths
);
1781 free (type1_subset
->base_font
);
1783 _cairo_type1_font_subset_fini (&font
);
1789 _cairo_type1_subset_fini (cairo_type1_subset_t
*subset
)
1791 free (subset
->base_font
);
1792 free (subset
->widths
);
1793 free (subset
->data
);
1797 _cairo_type1_scaled_font_is_type1 (cairo_scaled_font_t
*scaled_font
)
1799 cairo_status_t status
;
1800 unsigned long length
;
1801 unsigned char buf
[64];
1803 if (!scaled_font
->backend
->load_type1_data
)
1806 status
= scaled_font
->backend
->load_type1_data (scaled_font
, 0, NULL
, &length
);
1810 /* We only need a few bytes to test for Type 1 */
1811 if (length
> sizeof (buf
))
1812 length
= sizeof (buf
);
1814 status
= scaled_font
->backend
->load_type1_data (scaled_font
, 0, buf
, &length
);
1818 return check_fontdata_is_type1 (buf
, length
);
1821 #endif /* CAIRO_HAS_FONT_SUBSET */