beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-type1-subset.c
blobb15663509d3812b1299d105c47b7b592ecf83008
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.
33 * Contributor(s):
34 * Kristian Høgsberg <krh@redhat.com>
38 * Useful links:
39 * http://partners.adobe.com/public/developer/en/font/T1_SPEC.PDF
43 #define _BSD_SOURCE /* for snprintf(), strdup() */
44 #include "cairoint.h"
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"
55 #include <ctype.h>
57 #define TYPE1_STACKSIZE 24 /* Defined in Type 1 Font Format */
60 typedef struct {
61 int subset_index;
62 double width;
63 const char *encrypted_charstring;
64 int encrypted_charstring_length;
65 } glyph_data_t;
67 typedef struct _cairo_type1_font_subset {
68 cairo_scaled_font_subset_t *scaled_font_subset;
70 struct {
71 unsigned int font_id;
72 char *base_font;
73 unsigned int num_glyphs;
74 double x_min, y_min, x_max, y_max;
75 double ascent, descent;
76 double units_per_em;
78 const char *data;
79 unsigned long header_size;
80 unsigned long data_size;
81 unsigned long trailer_size;
82 } base;
84 int num_glyphs;
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
90 * the Charstrings. */
92 glyph_data_t *glyphs;
93 char **glyph_names;
94 cairo_array_t glyphs_array;
95 cairo_array_t glyph_names_array;
97 int num_subrs;
98 cairo_bool_t subset_subrs;
99 struct {
100 const char *subr_string;
101 int subr_length;
102 const char *np;
103 int np_length;
104 cairo_bool_t used;
105 } *subrs;
107 /* Indexed by subset_index this maps to the glyph order in the
108 * glyph_names and glyphs arrays. Has font->num_golyphs
109 * elements. */
110 int *subset_index_to_glyphs;
112 cairo_output_stream_t *output;
113 cairo_array_t contents;
115 const char *rd, *nd, *np;
117 int lenIV;
119 char *type1_data;
120 unsigned int type1_length;
121 char *type1_end;
123 char *header_segment;
124 int header_segment_size;
125 char *eexec_segment;
126 int eexec_segment_size;
127 cairo_bool_t eexec_segment_is_ascii;
129 char *cleartext;
130 char *cleartext_end;
132 int header_size;
134 unsigned short eexec_key;
135 cairo_bool_t hex_encode;
136 int hex_column;
138 struct {
139 double stack[TYPE1_STACKSIZE];
140 int sp;
141 } build_stack;
143 struct {
144 int stack[TYPE1_STACKSIZE];
145 int sp;
146 } ps_stack;
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;
164 font->num_subrs = 0;
165 font->subset_subrs = TRUE;
166 font->subrs = NULL;
168 font->hex_encode = hex_encode;
169 font->num_glyphs = 0;
171 _cairo_array_init (&font->contents, sizeof (char));
173 return CAIRO_STATUS_SUCCESS;
176 static void
177 cairo_type1_font_subset_use_glyph (cairo_type1_font_subset_t *font, int glyph)
179 if (font->glyphs[glyph].subset_index >= 0)
180 return;
182 font->glyphs[glyph].subset_index = font->num_glyphs;
183 font->subset_index_to_glyphs[font->num_glyphs] = glyph;
184 font->num_glyphs++;
187 static cairo_bool_t
188 is_ps_delimiter(int c)
190 static const char delimiters[] = "()[]{}<>/% \t\r\n";
192 return strchr (delimiters, c) != NULL;
195 static const char *
196 find_token (const char *buffer, const char *end, const char *token)
198 int i, length;
199 /* FIXME: find substring really must be find_token */
201 if (buffer == NULL)
202 return NULL;
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])))
209 return buffer + i;
211 return NULL;
214 static cairo_status_t
215 cairo_type1_font_subset_find_segments (cairo_type1_font_subset_t *font)
217 unsigned char *p;
218 const char *eexec_token;
219 int size, i;
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);
237 p += 6 + size;
239 font->type1_end = (char *) p;
240 } else {
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:
262 * /key1 1234 def
263 * /key2 [12 34 56] def
265 * ie a key defined as an integer or array of integers.
268 static void
269 cairo_type1_font_erase_dict_key (cairo_type1_font_subset_t *font,
270 const char *key)
272 const char *start, *p, *segment_end;
274 segment_end = font->header_segment + font->header_segment_size;
276 start = font->header_segment;
277 do {
278 start = find_token (start, segment_end, key);
279 if (start) {
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) ||
285 *p == '[' ||
286 *p == ']'))
288 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);
297 } while (start);
300 static cairo_status_t
301 cairo_type1_font_subset_get_matrix (cairo_type1_font_subset_t *font,
302 const char *name,
303 double *a,
304 double *b,
305 double *c,
306 double *d)
308 const char *start, *end, *segment_end;
309 int ret, s_max, i, j;
310 char *s;
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);
321 if (start == NULL)
322 return CAIRO_INT_STATUS_UNSUPPORTED;
324 end = find_token (start, segment_end, "def");
325 if (end == NULL)
326 return CAIRO_INT_STATUS_UNSUPPORTED;
328 s_max = end - start + 5*decimal_point_len + 1;
329 s = malloc (s_max);
330 if (unlikely (s == NULL))
331 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
333 i = 0;
334 j = 0;
335 while (i < end - start && j < s_max - decimal_point_len) {
336 if (start[i] == '.') {
337 strncpy(s + j, decimal_point, decimal_point_len);
338 i++;
339 j += decimal_point_len;
340 } else {
341 s[j++] = start[i++];
344 s[j] = 0;
346 start = strpbrk (s, "{[");
347 if (!start) {
348 free (s);
349 return CAIRO_INT_STATUS_UNSUPPORTED;
352 start++;
353 ret = 0;
354 if (*start)
355 ret = sscanf(start, "%lf %lf %lf %lf", a, b, c, d);
357 free (s);
359 if (ret != 4)
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",
373 &x_min,
374 &y_min,
375 &x_max,
376 &y_max);
377 if (unlikely (status))
378 return status;
380 status = cairo_type1_font_subset_get_matrix (font, "/FontMatrix",
381 &xx, &yx, &xy, &yy);
382 if (unlikely (status))
383 return status;
385 if (yy == 0.0)
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;
405 char *s;
406 int i;
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");
411 if (start == NULL)
412 return CAIRO_INT_STATUS_UNSUPPORTED;
414 start += strlen ("/FontName");
416 end = find_token (start, segment_end, "def");
417 if (end == NULL)
418 return CAIRO_INT_STATUS_UNSUPPORTED;
420 while (end > start && _cairo_isspace(end[-1]))
421 end--;
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);
428 s[end - start] = 0;
430 start = strchr (s, '/');
431 if (!start++ || !start) {
432 free (s);
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')
440 break;
442 if (i == 6)
443 start += 7;
446 font->base.base_font = strdup (start);
447 free (s);
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);
453 return status;
456 static cairo_status_t
457 cairo_type1_font_subset_write_header (cairo_type1_font_subset_t *font,
458 const char *name)
460 const char *start, *end, *segment_end;
461 unsigned int i;
463 /* FIXME:
464 * This function assumes that /FontName always appears
465 * before /Encoding. This appears to always be the case with Type1
466 * fonts.
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
472 * the output.
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
477 * output.
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
492 * exist.
494 end = font->header_segment;
495 start = find_token (font->header_segment, segment_end, "/UniqueID");
496 if (start) {
497 start += 9;
498 while (start < segment_end && _cairo_isspace (*start))
499 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 ");
504 end = start + 5;
508 start = find_token (end, segment_end, "/FontName");
509 if (start == NULL)
510 return CAIRO_INT_STATUS_UNSUPPORTED;
512 _cairo_output_stream_write (font->output, end,
513 start - end);
515 _cairo_output_stream_printf (font->output, "/FontName /%s def", name);
517 end = find_token (start, segment_end, "def");
518 if (end == NULL)
519 return CAIRO_INT_STATUS_UNSUPPORTED;
520 end += 3;
522 start = find_token (end, segment_end, "/Encoding");
523 if (start == NULL)
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,
536 "dup %d /%s put\n",
538 _cairo_winansi_to_glyphname (i));
541 } else {
542 for (i = 0; i < font->base.num_glyphs; i++) {
543 if (font->glyphs[i].subset_index <= 0)
544 continue;
545 _cairo_output_stream_printf (font->output,
546 "dup %d /%s put\n",
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");
554 if (end == NULL)
555 return CAIRO_INT_STATUS_UNSUPPORTED;
556 end += 3;
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;
567 static int
568 hex_to_int (int ch)
570 if (ch <= '9')
571 return ch - '0';
572 else if (ch <= 'F')
573 return ch - 'A' + 10;
574 else
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;
583 int c, p;
584 static const char hex_digits[16] = "0123456789abcdef";
585 char digits[3];
587 in = (const unsigned char *) data;
588 end = (const unsigned char *) data + length;
589 while (in < end) {
590 p = *in++;
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];
597 digits[2] = '\n';
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;
603 } else {
604 _cairo_output_stream_write (font->output, digits, 2);
606 } else {
607 digits[0] = c;
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;
620 char *out;
621 int c, p;
622 int i;
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;
632 while (in < end) {
633 if (font->eexec_segment_is_ascii) {
634 c = *in++;
635 if (_cairo_isspace (c))
636 continue;
637 c = (hex_to_int (c) << 4) | hex_to_int (*in++);
638 } else {
639 c = *in++;
641 p = c ^ (r >> 8);
642 r = (c + r) * CAIRO_TYPE1_ENCRYPT_C1 + CAIRO_TYPE1_ENCRYPT_C2;
644 *out++ = p;
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;
671 static const char *
672 skip_token (const char *p, const char *end)
674 while (p < end && _cairo_isspace(*p))
675 p++;
677 while (p < end && !_cairo_isspace(*p))
678 p++;
680 if (p == end)
681 return NULL;
683 return p;
686 static void
687 cairo_type1_font_subset_decrypt_charstring (const unsigned char *in, int size, unsigned char *out)
689 unsigned short r = CAIRO_TYPE1_CHARSTRING_KEY;
690 int c, p, i;
692 for (i = 0; i < size; i++) {
693 c = *in++;
694 p = c ^ (r >> 8);
695 r = (c + r) * CAIRO_TYPE1_ENCRYPT_C1 + CAIRO_TYPE1_ENCRYPT_C2;
696 *out++ = p;
700 static const unsigned char *
701 cairo_type1_font_subset_decode_integer (const unsigned char *p, int *integer)
703 if (*p <= 246) {
704 *integer = *p++ - 139;
705 } else if (*p <= 250) {
706 *integer = (p[0] - 247) * 256 + p[1] + 108;
707 p += 2;
708 } else if (*p <= 254) {
709 *integer = -(p[0] - 251) * 256 - p[1] - 108;
710 p += 2;
711 } else {
712 *integer = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
713 p += 5;
716 return p;
719 static cairo_status_t
720 use_standard_encoding_glyph (cairo_type1_font_subset_t *font, int index)
722 const char *glyph_name;
723 unsigned int i;
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
773 * SEAC operator. */
774 static cairo_status_t
775 cairo_type1_font_subset_parse_charstring (cairo_type1_font_subset_t *font,
776 int glyph,
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;
784 int command;
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,
793 charstring);
794 end = charstring + encrypted_charstring_length;
795 p = charstring + font->lenIV;
796 status = CAIRO_STATUS_SUCCESS;
797 while (p < end) {
798 if (*p < 32) {
799 command = *p++;
800 switch (command) {
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:
815 default:
816 /* stack clearing operator */
817 font->build_stack.sp = 0;
818 break;
820 case TYPE1_CHARSTRING_COMMAND_CALLSUBR:
821 if (font->subset_subrs && font->build_stack.sp > 0) {
822 double int_val;
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 (
828 font,
829 glyph,
830 font->subrs[subr_num].subr_string,
831 font->subrs[subr_num].subr_length);
832 break;
836 font->subset_subrs = FALSE;
837 break;
839 case TYPE1_CHARSTRING_COMMAND_HSBW:
840 if (font->build_stack.sp < 2) {
841 status = CAIRO_INT_STATUS_UNSUPPORTED;
842 goto cleanup;
845 font->glyphs[glyph].width = font->build_stack.stack[1]/font->base.units_per_em;
846 font->build_stack.sp = 0;
847 break;
849 case TYPE1_CHARSTRING_COMMAND_ESCAPE:
850 command = command << 8 | *p++;
851 switch (command) {
852 case TYPE1_CHARSTRING_COMMAND_DOTSECTION:
853 case TYPE1_CHARSTRING_COMMAND_VSTEM3:
854 case TYPE1_CHARSTRING_COMMAND_HSTEM3:
855 case TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT:
856 default:
857 /* stack clearing operator */
858 font->build_stack.sp = 0;
859 break;
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;
870 goto cleanup;
873 status = use_standard_encoding_glyph (font, font->build_stack.stack[3]);
874 if (unlikely (status))
875 goto cleanup;
877 status = use_standard_encoding_glyph (font, font->build_stack.stack[4]);
878 if (unlikely (status))
879 goto cleanup;
881 font->build_stack.sp = 0;
882 break;
884 case TYPE1_CHARSTRING_COMMAND_SBW:
885 if (font->build_stack.sp < 4) {
886 status = CAIRO_INT_STATUS_UNSUPPORTED;
887 goto cleanup;
890 font->glyphs[glyph].width = font->build_stack.stack[2]/font->base.units_per_em;
891 font->build_stack.sp = 0;
892 break;
894 case TYPE1_CHARSTRING_COMMAND_DIV:
895 if (font->build_stack.sp < 2) {
896 status = CAIRO_INT_STATUS_UNSUPPORTED;
897 goto cleanup;
898 } else {
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--;
902 if (num2 == 0.0) {
903 status = CAIRO_INT_STATUS_UNSUPPORTED;
904 goto cleanup;
906 font->build_stack.stack[font->build_stack.sp - 1] = num1/num2;
908 break;
910 case TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR:
911 if (font->build_stack.sp < 1) {
912 status = CAIRO_INT_STATUS_UNSUPPORTED;
913 goto cleanup;
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];
921 break;
923 case TYPE1_CHARSTRING_COMMAND_POP:
924 if (font->ps_stack.sp < 1) {
925 status = CAIRO_INT_STATUS_UNSUPPORTED;
926 goto cleanup;
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];
933 break;
935 break;
937 } else {
938 /* integer argument */
939 if (font->build_stack.sp < TYPE1_STACKSIZE) {
940 int val;
941 p = cairo_type1_font_subset_decode_integer (p, &val);
942 font->build_stack.stack[font->build_stack.sp++] = val;
943 } else {
944 status = CAIRO_INT_STATUS_UNSUPPORTED;
945 goto cleanup;
950 cleanup:
951 free (charstring);
953 return status;
956 static cairo_status_t
957 cairo_type1_font_subset_build_subr_list (cairo_type1_font_subset_t *font,
958 int subr_number,
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,
973 int subr_number,
974 const char *subr_string, int subr_string_length,
975 const char *np, int np_length)
977 cairo_status_t status;
978 char buffer[256];
979 int length;
981 if (!font->subrs[subr_number].used)
982 return CAIRO_STATUS_SUCCESS;
984 length = snprintf (buffer, sizeof buffer,
985 "dup %d %d %s ",
986 subr_number, subr_string_length, font->rd);
987 status = cairo_type1_font_subset_write_encrypted (font, buffer, length);
988 if (unlikely (status))
989 return status;
991 status = cairo_type1_font_subset_write_encrypted (font,
992 subr_string,
993 subr_string_length);
994 if (unlikely (status))
995 return status;
997 if (np) {
998 status = cairo_type1_font_subset_write_encrypted (font, np, np_length);
999 } else {
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))
1004 return status;
1006 return CAIRO_STATUS_SUCCESS;
1009 typedef cairo_status_t (*subr_func_t) (cairo_type1_font_subset_t *font,
1010 int subr_number,
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,
1018 subr_func_t func,
1019 const char **array_end)
1021 const char *p, *subr_string;
1022 char *end;
1023 int subr_num, subr_length;
1024 const char *np;
1025 int np_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.
1037 p = array_start;
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);
1043 if (p == end)
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 */
1050 p = end;
1051 subr_length = strtol (p, &end, 10);
1052 if (p == end)
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;
1059 np = NULL;
1060 np_length = 0;
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))
1065 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))
1071 p++;
1073 np = subr_string + subr_length;
1074 np_length = p - np;
1077 status = func (font, subr_num,
1078 subr_string, subr_length, np, np_length);
1079 if (unlikely (status))
1080 return 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,
1091 int glyph_number,
1092 const char *name, int name_length,
1093 const char *encrypted_charstring, int encrypted_charstring_length)
1095 char *s;
1096 glyph_data_t glyph;
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);
1104 s[name_length] = 0;
1106 status = _cairo_array_append (&font->glyph_names_array, &s);
1107 if (unlikely (status))
1108 return status;
1110 glyph.subset_index = -1;
1111 glyph.width = 0;
1112 glyph.encrypted_charstring = encrypted_charstring;
1113 glyph.encrypted_charstring_length = encrypted_charstring_length;
1114 status = _cairo_array_append (&font->glyphs_array, &glyph);
1116 return status;
1119 static cairo_status_t
1120 write_used_glyphs (cairo_type1_font_subset_t *font,
1121 int glyph_number,
1122 const char *name, int name_length,
1123 const char *charstring, int charstring_length)
1125 cairo_status_t status;
1126 char buffer[256];
1127 int length;
1128 unsigned int subset_id;
1129 int ch;
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);
1150 if (wa_name) {
1151 name = wa_name;
1152 name_length = strlen(name);
1157 length = snprintf (buffer, sizeof buffer,
1158 "/%.*s %d %s ",
1159 name_length, name, charstring_length, font->rd);
1160 status = cairo_type1_font_subset_write_encrypted (font, buffer, length);
1161 if (unlikely (status))
1162 return status;
1164 status = cairo_type1_font_subset_write_encrypted (font,
1165 charstring,
1166 charstring_length);
1167 if (unlikely (status))
1168 return 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))
1173 return status;
1175 return CAIRO_STATUS_SUCCESS;
1178 typedef cairo_status_t (*glyph_func_t) (cairo_type1_font_subset_t *font,
1179 int glyph_number,
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,
1187 glyph_func_t func,
1188 const char **dict_out)
1190 int charstring_length, name_length;
1191 const char *p, *charstring, *name;
1192 char *end;
1193 cairo_status_t status;
1194 int glyph_count;
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 '/'.
1210 p = dict_start;
1211 glyph_count = 0;
1212 while (*p == '/') {
1213 name = p + 1;
1214 p = skip_token (p, dict_end);
1215 name_length = p - name;
1217 charstring_length = strtol (p, &end, 10);
1218 if (p == end)
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))
1228 p++;
1230 /* In case any of the skip_token() calls above reached EOF, p will
1231 * be equal to dict_end. */
1232 if (p == dict_end)
1233 return CAIRO_INT_STATUS_UNSUPPORTED;
1235 status = func (font, glyph_count++,
1236 name, name_length,
1237 charstring, charstring_length);
1238 if (unlikely (status))
1239 return status;
1242 *dict_out = p;
1244 return CAIRO_STATUS_SUCCESS;
1248 static cairo_status_t
1249 cairo_type1_font_subset_write_private_dict (cairo_type1_font_subset_t *font,
1250 const char *name)
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;
1258 unsigned int i;
1259 int glyph, j;
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
1272 * operator.
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. */
1281 font->lenIV = 4;
1282 if ((lenIV_start = find_token (font->cleartext, font->cleartext_end, "/lenIV")) != NULL) {
1283 lenIV_start += 6;
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);
1296 free(lenIV_str);
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;
1315 array_start = NULL;
1316 goto skip_subrs;
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");
1334 if (subrs == NULL)
1335 return CAIRO_INT_STATUS_UNSUPPORTED;
1337 /* Read in the subroutines */
1338 status = cairo_type1_font_for_each_subr (font,
1339 array_start,
1340 font->cleartext_end,
1341 cairo_type1_font_subset_build_subr_list,
1342 &array_end);
1343 if (unlikely(status))
1344 return status;
1346 p = array_end;
1347 skip_subrs:
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
1361 * definition. */
1362 for (p = glyph_count_end; p < font->cleartext_end; p++)
1363 if (*p == '/')
1364 break;
1365 if (p == font->cleartext_end)
1366 return CAIRO_INT_STATUS_UNSUPPORTED;
1367 dict_start = p;
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,
1373 dict_start,
1374 font->cleartext_end,
1375 cairo_type1_font_subset_build_glyph_list,
1376 &dict_end);
1377 if (unlikely(status))
1378 return 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,
1398 font->glyph_names,
1399 font->base.num_glyphs,
1400 font->scaled_font_subset->glyphs[i],
1401 &index);
1402 if (unlikely(status))
1403 return 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,
1417 glyph,
1418 font->glyphs[glyph].encrypted_charstring,
1419 font->glyphs[glyph].encrypted_charstring_length);
1420 if (unlikely (status))
1421 return status;
1424 /* Always include the first five subroutines in case the Flex/hint mechanism is
1425 * being used. */
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))
1438 return 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))
1448 return status;
1450 /* Write out the subr definitions for each of the glyphs in
1451 * the subset. */
1452 status = cairo_type1_font_for_each_subr (font,
1453 array_start,
1454 font->cleartext_end,
1455 write_used_subrs,
1456 &p);
1457 if (unlikely (status))
1458 return status;
1459 } else {
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
1466 * token. */
1467 status = cairo_type1_font_subset_write_encrypted (font, p, charstrings - p);
1468 if (unlikely (status))
1469 return 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))
1476 return 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))
1483 return status;
1485 /* Write out the charstring definitions for each of the glyphs in
1486 * the subset. */
1487 status = cairo_type1_font_subset_for_each_glyph (font,
1488 dict_start,
1489 font->cleartext_end,
1490 write_used_glyphs,
1491 &p);
1492 if (unlikely (status))
1493 return 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))
1500 return 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;
1512 int i;
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
1534 * always binary. */
1536 _cairo_output_stream_printf (font->output, "cleartomark\n");
1537 } else {
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,
1557 const char *name)
1559 cairo_status_t status;
1561 status = cairo_type1_font_subset_find_segments (font);
1562 if (unlikely (status))
1563 return status;
1565 status = cairo_type1_font_subset_decrypt_eexec_segment (font);
1566 if (unlikely (status))
1567 return status;
1569 /* Determine which glyph definition delimiters to use. */
1570 if (find_token (font->cleartext, font->cleartext_end, "/-|") != NULL) {
1571 font->rd = "-|";
1572 font->nd = "|-";
1573 font->np = "|";
1574 } else if (find_token (font->cleartext, font->cleartext_end, "/RD") != NULL) {
1575 font->rd = "RD";
1576 font->nd = "ND";
1577 font->np = "NP";
1578 } else {
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))
1588 return status;
1590 status = cairo_type1_font_subset_get_fontname (font);
1591 if (unlikely (status))
1592 return status;
1594 status = cairo_type1_font_subset_write_private_dict (font, name);
1595 if (unlikely (status))
1596 return 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))
1603 return 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;
1612 static cairo_bool_t
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)
1617 return TRUE;
1619 /* Test for Type 1 1 ASCII (PFA) */
1620 if (length > 2 && data[0] == '%' && data[1] == '!')
1621 return TRUE;
1623 return FALSE;
1626 static cairo_status_t
1627 cairo_type1_font_subset_generate (void *abstract_font,
1628 const char *name)
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);
1641 if (status)
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,
1651 &data_length);
1652 if (unlikely (status))
1653 return 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))
1660 return status;
1662 font->output = _cairo_output_stream_create (type1_font_write, NULL, font);
1663 if (unlikely ((status = font->output->status)))
1664 return status;
1666 status = cairo_type1_font_subset_write (font, name);
1667 if (unlikely (status))
1668 return status;
1670 font->base.data = _cairo_array_index (&font->contents, 0);
1672 return status;
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;
1679 unsigned int i;
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++) {
1688 char **s;
1690 s = _cairo_array_index (&font->glyph_names_array, i);
1691 free (*s);
1693 _cairo_array_fini (&font->glyph_names_array);
1694 _cairo_array_fini (&font->glyphs_array);
1696 free (font->subrs);
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);
1707 return status;
1710 cairo_status_t
1711 _cairo_type1_subset_init (cairo_type1_subset_t *type1_subset,
1712 const char *name,
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;
1719 unsigned int i;
1720 char buf[30];
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))
1729 return status;
1731 status = cairo_type1_font_subset_generate (&font, name);
1732 if (unlikely (status))
1733 goto fail1;
1735 if (font.base.base_font) {
1736 type1_subset->base_font = strdup (font.base.base_font);
1737 } else {
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))
1743 goto fail1;
1745 type1_subset->widths = calloc (sizeof (double), font.num_glyphs);
1746 if (unlikely (type1_subset->widths == NULL))
1747 goto fail2;
1748 for (i = 0; i < font.base.num_glyphs; i++) {
1749 if (font.glyphs[i].subset_index < 0)
1750 continue;
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))
1767 goto fail3;
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);
1778 fail3:
1779 free (type1_subset->widths);
1780 fail2:
1781 free (type1_subset->base_font);
1782 fail1:
1783 _cairo_type1_font_subset_fini (&font);
1785 return status;
1788 void
1789 _cairo_type1_subset_fini (cairo_type1_subset_t *subset)
1791 free (subset->base_font);
1792 free (subset->widths);
1793 free (subset->data);
1796 cairo_bool_t
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)
1804 return FALSE;
1806 status = scaled_font->backend->load_type1_data (scaled_font, 0, NULL, &length);
1807 if (status)
1808 return FALSE;
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);
1815 if (status)
1816 return FALSE;
1818 return check_fontdata_is_type1 (buf, length);
1821 #endif /* CAIRO_HAS_FONT_SUBSET */