w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cairo-type1-subset.c
Go to the documentation of this file.
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
3  *
4  * Copyright © 2006 Red Hat, Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it either under the terms of the GNU Lesser General Public
8  * License version 2.1 as published by the Free Software Foundation
9  * (the "LGPL") or, at your option, under the terms of the Mozilla
10  * Public License Version 1.1 (the "MPL"). If you do not alter this
11  * notice, a recipient may use your version of this file under either
12  * the MPL or the LGPL.
13  *
14  * You should have received a copy of the LGPL along with this library
15  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17  * You should have received a copy of the MPL along with this library
18  * in the file COPYING-MPL-1.1
19  *
20  * The contents of this file are subject to the Mozilla Public License
21  * Version 1.1 (the "License"); you may not use this file except in
22  * compliance with the License. You may obtain a copy of the License at
23  * http://www.mozilla.org/MPL/
24  *
25  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27  * the specific language governing rights and limitations.
28  *
29  * The Original Code is the cairo graphics library.
30  *
31  * The Initial Developer of the Original Code is Red Hat, Inc.
32  *
33  * Contributor(s):
34  * Kristian Høgsberg <krh@redhat.com>
35  */
36 
37 /*
38  * Useful links:
39  * http://partners.adobe.com/public/developer/en/font/T1_SPEC.PDF
40  */
41 
42 
43 #define _DEFAULT_SOURCE /* for snprintf(), strdup() */
44 #include "cairoint.h"
45 
46 #include "cairo-array-private.h"
47 #include "cairo-error-private.h"
48 
49 #if CAIRO_HAS_FONT_SUBSET
50 
51 #include "cairo-type1-private.h"
54 
55 #include <ctype.h>
56 
57 #define TYPE1_STACKSIZE 24 /* Defined in Type 1 Font Format */
58 
59 
60 typedef struct {
61  int subset_index;
62  double width;
63  const char *encrypted_charstring;
64  int encrypted_charstring_length;
65 } glyph_data_t;
66 
67 typedef struct _cairo_type1_font_subset {
69 
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;
77 
78  const char *data;
79  unsigned long header_size;
80  unsigned long data_size;
81  unsigned long trailer_size;
82  } base;
83 
84  int num_glyphs;
85 
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. */
91 
93  char **glyph_names;
96 
97  int num_subrs;
99  struct {
100  const char *subr_string;
101  int subr_length;
102  const char *np;
103  int np_length;
105  } *subrs;
106 
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. */
111 
114 
115  const char *rd, *nd, *np;
116 
117  int lenIV;
118 
119  char *type1_data;
120  unsigned int type1_length;
121  char *type1_end;
122 
123  char *header_segment;
125  char *eexec_segment;
126  int eexec_segment_size;
128 
129  char *cleartext;
130  char *cleartext_end;
131 
132  int header_size;
133 
134  unsigned short eexec_key;
136  int hex_column;
137 
138  struct {
139  double stack[TYPE1_STACKSIZE];
140  int sp;
142 
143  struct {
144  int stack[TYPE1_STACKSIZE];
145  int sp;
147 
148 
150 
151 
152 static cairo_status_t
154  cairo_scaled_font_subset_t *scaled_font_subset,
155  cairo_bool_t hex_encode)
156 {
157  memset (font, 0, sizeof (*font));
158  font->scaled_font_subset = scaled_font_subset;
159 
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;
167 
168  font->hex_encode = hex_encode;
169  font->num_glyphs = 0;
170 
171  _cairo_array_init (&font->contents, sizeof (char));
172 
173  return CAIRO_STATUS_SUCCESS;
174 }
175 
176 static void
178 {
179  if (font->glyphs[glyph].subset_index >= 0)
180  return;
181 
182  font->glyphs[glyph].subset_index = font->num_glyphs;
183  font->subset_index_to_glyphs[font->num_glyphs] = glyph;
184  font->num_glyphs++;
185 }
186 
187 static cairo_bool_t
189 {
190  static const char delimiters[] = "()[]{}<>/% \t\r\n";
191 
192  return strchr (delimiters, c) != NULL;
193 }
194 
195 static const char *
196 find_token (const char *buffer, const char *end, const char *token)
197 {
198  int i, length;
199  /* FIXME: find substring really must be find_token */
200 
201  if (buffer == NULL)
202  return NULL;
203 
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;
210 
211  return NULL;
212 }
213 
214 static cairo_status_t
216 {
217  unsigned char *p;
218  const char *eexec_token;
219  int size, i;
220 
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;
227 
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);
233 
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;
238  }
239  font->type1_end = (char *) p;
240  } else {
241  eexec_token = find_token ((char *) p, font->type1_end, "eexec");
242  if (eexec_token == NULL)
244 
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;
253  }
254  }
255 
256  return CAIRO_STATUS_SUCCESS;
257 }
258 
259 /* Search for the definition of key and erase it by overwriting with spaces.
260  * This function is looks for definitions of the form:
261  *
262  * /key1 1234 def
263  * /key2 [12 34 56] def
264  *
265  * ie a key defined as an integer or array of integers.
266  *
267  */
268 static void
270  const char *key)
271 {
272  const char *start, *p, *segment_end;
273 
274  segment_end = font->header_segment + font->header_segment_size;
275 
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 == ']'))
287  {
288  p++;
289  }
290 
291  if (p + 3 < segment_end && memcmp(p, "def", 3) == 0) {
292  /* erase definition of the key */
293  memset((char *) start, ' ', p + 3 - start);
294  }
295  start += strlen(key);
296  }
297  } while (start);
298 }
299 
300 static cairo_status_t
302  const char *name,
303  double *a,
304  double *b,
305  double *c,
306  double *d)
307 {
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;
313 
314  decimal_point = _cairo_get_locale_decimal_point ();
315  decimal_point_len = strlen (decimal_point);
316 
317  assert (decimal_point_len != 0);
318 
319  segment_end = font->header_segment + font->header_segment_size;
320  start = find_token (font->header_segment, segment_end, name);
321  if (start == NULL)
323 
324  end = find_token (start, segment_end, "def");
325  if (end == NULL)
327 
328  s_max = end - start + 5*decimal_point_len + 1;
329  s = _cairo_malloc (s_max);
330  if (unlikely (s == NULL))
332 
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++];
342  }
343  }
344  s[j] = 0;
345 
346  start = strpbrk (s, "{[");
347  if (!start) {
348  free (s);
350  }
351 
352  start++;
353  ret = 0;
354  if (*start)
355  ret = sscanf(start, "%lf %lf %lf %lf", a, b, c, d);
356 
357  free (s);
358 
359  if (ret != 4)
361 
362  return CAIRO_STATUS_SUCCESS;
363 }
364 
365 static cairo_status_t
367 {
369  double x_min, y_min, x_max, y_max;
370  double xx, yx, xy, yy;
371 
373  &x_min,
374  &y_min,
375  &x_max,
376  &y_max);
377  if (unlikely (status))
378  return status;
379 
381  &xx, &yx, &xy, &yy);
382  if (unlikely (status))
383  return status;
384 
385  if (yy == 0.0)
387 
388  /* Freetype uses 1/yy to get units per EM */
389  font->base.units_per_em = 1.0/yy;
390 
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;
397 
398  return CAIRO_STATUS_SUCCESS;
399 }
400 
401 static cairo_status_t
403 {
404  const char *start, *end, *segment_end;
405  char *s;
406  int i;
408 
409  segment_end = font->header_segment + font->header_segment_size;
410  start = find_token (font->header_segment, segment_end, "/FontName");
411  if (start == NULL)
413 
414  start += strlen ("/FontName");
415 
416  end = find_token (start, segment_end, "def");
417  if (end == NULL)
419 
420  while (end > start && _cairo_isspace(end[-1]))
421  end--;
422 
423  s = _cairo_malloc (end - start + 1);
424  if (unlikely (s == NULL))
426 
427  strncpy (s, start, end - start);
428  s[end - start] = 0;
429 
430  start = strchr (s, '/');
431  if (!start++ || !start) {
432  free (s);
434  }
435 
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;
441  }
442  if (i == 6)
443  start += 7;
444  }
445 
446  font->base.base_font = strdup (start);
447  free (s);
448  if (unlikely (font->base.base_font == NULL))
450 
451  status = _cairo_escape_ps_name (&font->base.base_font);
452 
453  return status;
454 }
455 
456 static cairo_status_t
458  const char *name)
459 {
460  const char *start, *end, *segment_end;
461  unsigned int i;
462 
463  /* FIXME:
464  * This function assumes that /FontName always appears
465  * before /Encoding. This appears to always be the case with Type1
466  * fonts.
467  *
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.
473  *
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.
478  */
479  cairo_type1_font_erase_dict_key (font, "/UniqueID");
481 
482  segment_end = font->header_segment + font->header_segment_size;
483 
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.
493  */
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;
505  }
506  }
507 
508  start = find_token (end, segment_end, "/FontName");
509  if (start == NULL)
511 
513  start - end);
514 
515  _cairo_output_stream_printf (font->output, "/FontName /%s def", name);
516 
517  end = find_token (start, segment_end, "def");
518  if (end == NULL)
520  end += 3;
521 
522  start = find_token (end, segment_end, "/Encoding");
523  if (start == NULL)
526 
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];
533 
534  if (subset_glyph > 0) {
536  "dup %d /%s put\n",
537  i,
539  }
540  }
541  } else {
542  for (i = 0; i < font->base.num_glyphs; i++) {
543  if (font->glyphs[i].subset_index <= 0)
544  continue;
546  "dup %d /%s put\n",
547  font->glyphs[i].subset_index,
548  font->glyph_names[i]);
549  }
550  }
551  _cairo_output_stream_printf (font->output, "readonly def");
552 
553  end = find_token (start, segment_end, "def");
554  if (end == NULL)
556  end += 3;
557 
558  /* There are some buggy fonts that contain more than one /Encoding */
559  if (find_token (end, segment_end, "/Encoding"))
561 
562  _cairo_output_stream_write (font->output, end, segment_end - end);
563 
564  return font->output->status;
565 }
566 
567 static int
569 {
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;
576 }
577 
578 static cairo_status_t
580  const char *data, unsigned int length)
581 {
582  const unsigned char *in, *end;
583  int c, p;
584  static const char hex_digits[16] = "0123456789abcdef";
585  char digits[3];
586 
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;
593 
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;
599 
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);
605  }
606  } else {
607  digits[0] = c;
608  _cairo_output_stream_write (font->output, digits, 1);
609  }
610  }
611 
612  return font->output->status;
613 }
614 
615 static cairo_status_t
617 {
618  unsigned short r = CAIRO_TYPE1_PRIVATE_DICT_KEY;
619  unsigned char *in, *end;
620  char *out;
621  int c, p;
622  int i;
623 
624  in = (unsigned char *) font->eexec_segment;
625  end = (unsigned char *) in + font->eexec_segment_size;
626 
627  font->cleartext = _cairo_malloc (font->eexec_segment_size + 1);
628  if (unlikely (font->cleartext == NULL))
630 
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++;
640  }
641  p = c ^ (r >> 8);
643 
644  *out++ = p;
645  }
646  font->cleartext_end = out;
647 
648  /* Overwrite random bytes with spaces.
649  *
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.
661  */
662  for (i = 0; i < 4 && i < font->eexec_segment_size; i++)
663  font->cleartext[i] = ' ';
664 
665  /* Ensure strtol() can not scan past the end of the cleartext */
666  font->cleartext[font->eexec_segment_size] = 0;
667 
668  return CAIRO_STATUS_SUCCESS;
669 }
670 
671 static const char *
672 skip_token (const char *p, const char *end)
673 {
674  while (p < end && _cairo_isspace(*p))
675  p++;
676 
677  while (p < end && !_cairo_isspace(*p))
678  p++;
679 
680  if (p == end)
681  return NULL;
682 
683  return p;
684 }
685 
686 static void
687 cairo_type1_font_subset_decrypt_charstring (const unsigned char *in, int size, unsigned char *out)
688 {
689  unsigned short r = CAIRO_TYPE1_CHARSTRING_KEY;
690  int c, p, i;
691 
692  for (i = 0; i < size; i++) {
693  c = *in++;
694  p = c ^ (r >> 8);
696  *out++ = p;
697  }
698 }
699 
700 static const unsigned char *
702 {
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;
714  }
715 
716  return p;
717 }
718 
719 static cairo_status_t
721 {
722  const char *glyph_name;
723  unsigned int i;
724 
725  if (index < 0 || index > 255)
726  return CAIRO_STATUS_SUCCESS;
727 
729  if (glyph_name == NULL)
730  return CAIRO_STATUS_SUCCESS;
731 
732  for (i = 0; i < font->base.num_glyphs; i++) {
733  if (font->glyph_names[i] && strcmp (font->glyph_names[i], glyph_name) == 0) {
735 
736  return CAIRO_STATUS_SUCCESS;
737  }
738  }
739 
741 }
742 
743 
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
770 
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
776  int glyph,
777  const char *encrypted_charstring,
778  int encrypted_charstring_length)
779 {
781  unsigned char *charstring;
782  const unsigned char *end;
783  const unsigned char *p;
784  int command;
785 
786  charstring = _cairo_malloc (encrypted_charstring_length);
787  if (unlikely (charstring == NULL))
789 
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;
797  while (p < end) {
798  if (*p < 32) {
799  command = *p++;
800  switch (command) {
815  default:
816  /* stack clearing operator */
817  font->build_stack.sp = 0;
818  break;
819 
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;
828  font,
829  glyph,
830  font->subrs[subr_num].subr_string,
831  font->subrs[subr_num].subr_length);
832  break;
833  }
834  }
835  }
836  font->subset_subrs = FALSE;
837  break;
838 
840  if (font->build_stack.sp < 2) {
842  goto cleanup;
843  }
844 
845  font->glyphs[glyph].width = font->build_stack.stack[1]/font->base.units_per_em;
846  font->build_stack.sp = 0;
847  break;
848 
850  command = command << 8 | *p++;
851  switch (command) {
856  default:
857  /* stack clearing operator */
858  font->build_stack.sp = 0;
859  break;
860 
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) {
870  goto cleanup;
871  }
872 
873  status = use_standard_encoding_glyph (font, font->build_stack.stack[3]);
874  if (unlikely (status))
875  goto cleanup;
876 
877  status = use_standard_encoding_glyph (font, font->build_stack.stack[4]);
878  if (unlikely (status))
879  goto cleanup;
880 
881  font->build_stack.sp = 0;
882  break;
883 
885  if (font->build_stack.sp < 4) {
887  goto cleanup;
888  }
889 
890  font->glyphs[glyph].width = font->build_stack.stack[2]/font->base.units_per_em;
891  font->build_stack.sp = 0;
892  break;
893 
895  if (font->build_stack.sp < 2) {
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) {
904  goto cleanup;
905  }
906  font->build_stack.stack[font->build_stack.sp - 1] = num1/num2;
907  }
908  break;
909 
911  if (font->build_stack.sp < 1) {
913  goto cleanup;
914  }
915 
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];
920 
921  break;
922 
924  if (font->ps_stack.sp < 1) {
926  goto cleanup;
927  }
928 
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;
934  }
935  break;
936  }
937  } else {
938  /* integer argument */
939  if (font->build_stack.sp < TYPE1_STACKSIZE) {
940  int val;
942  font->build_stack.stack[font->build_stack.sp++] = val;
943  } else {
945  goto cleanup;
946  }
947  }
948  }
949 
950 cleanup:
951  free (charstring);
952 
953  return status;
954 }
955 
956 static cairo_status_t
958  int subr_number,
959  const char *encrypted_charstring, int encrypted_charstring_length,
960  const char *np, int np_length)
961 {
962 
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;
967 
968  return CAIRO_STATUS_SUCCESS;
969 }
970 
971 static cairo_status_t
973  int subr_number,
974  const char *subr_string, int subr_string_length,
975  const char *np, int np_length)
976 {
978  char buffer[256];
979  int length;
980 
981  if (!font->subrs[subr_number].used)
982  return CAIRO_STATUS_SUCCESS;
983 
984  length = snprintf (buffer, sizeof buffer,
985  "dup %d %d %s ",
986  subr_number, subr_string_length, font->rd);
988  if (unlikely (status))
989  return status;
990 
992  subr_string,
993  subr_string_length);
994  if (unlikely (status))
995  return status;
996 
997  if (np) {
999  } else {
1000  length = snprintf (buffer, sizeof buffer, "%s\n", font->np);
1002  }
1003  if (unlikely (status))
1004  return status;
1005 
1006  return CAIRO_STATUS_SUCCESS;
1007 }
1008 
1010  int subr_number,
1011  const char *subr_string, int subr_string_length,
1012  const char *np, int np_length);
1013 
1014 static cairo_status_t
1016  const char *array_start,
1017  const char *cleartext_end,
1018  subr_func_t func,
1019  const char **array_end)
1020 {
1021  const char *p, *subr_string;
1022  char *end;
1023  int subr_num, subr_length;
1024  const char *np;
1025  int np_length;
1027 
1028  /* We're looking at "dup" at the start of the first subroutine. The subroutines
1029  * definitions are on the form:
1030  *
1031  * dup 5 23 RD <23 binary bytes> NP
1032  *
1033  * or alternatively using -| and |- instead of RD and ND.
1034  * The first number is the subroutine number.
1035  */
1036 
1037  p = array_start;
1038  while (p + 3 < cleartext_end && strncmp (p, "dup", 3) == 0) {
1039  p = skip_token (p, cleartext_end);
1040 
1041  /* get subr number */
1042  subr_num = strtol (p, &end, 10);
1043  if (p == end)
1045 
1046  if (subr_num < 0 || subr_num >= font->num_subrs)
1048 
1049  /* get subr length */
1050  p = end;
1051  subr_length = strtol (p, &end, 10);
1052  if (p == end)
1054 
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;
1058 
1059  np = NULL;
1060  np_length = 0;
1061 
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++;
1066 
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++;
1072 
1073  np = subr_string + subr_length;
1074  np_length = p - np;
1075  }
1076 
1077  status = func (font, subr_num,
1078  subr_string, subr_length, np, np_length);
1079  if (unlikely (status))
1080  return status;
1081 
1082  }
1083 
1084  *array_end = (char *) p;
1085 
1086  return CAIRO_STATUS_SUCCESS;
1087 }
1088 
1089 static cairo_status_t
1091  int glyph_number,
1092  const char *name, int name_length,
1093  const char *encrypted_charstring, int encrypted_charstring_length)
1094 {
1095  char *s;
1098 
1099  s = _cairo_malloc (name_length + 1);
1100  if (unlikely (s == NULL))
1102 
1103  strncpy (s, name, name_length);
1104  s[name_length] = 0;
1105 
1106  status = _cairo_array_append (&font->glyph_names_array, &s);
1107  if (unlikely (status))
1108  return status;
1109 
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);
1115 
1116  return status;
1117 }
1118 
1119 static cairo_status_t
1121  int glyph_number,
1122  const char *name, int name_length,
1123  const char *charstring, int charstring_length)
1124 {
1126  char buffer[256];
1127  int length;
1128  unsigned int subset_id;
1129  int ch;
1130  const char *wa_name;
1131 
1132  if (font->glyphs[glyph_number].subset_index < 0)
1133  return CAIRO_STATUS_SUCCESS;
1134 
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.
1140  **/
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.
1146  */
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);
1153  }
1154  }
1155  }
1156 
1157  length = snprintf (buffer, sizeof buffer,
1158  "/%.*s %d %s ",
1159  name_length, name, charstring_length, font->rd);
1161  if (unlikely (status))
1162  return status;
1163 
1165  charstring,
1166  charstring_length);
1167  if (unlikely (status))
1168  return status;
1169 
1170  length = snprintf (buffer, sizeof buffer, "%s\n", font->nd);
1172  if (unlikely (status))
1173  return status;
1174 
1175  return CAIRO_STATUS_SUCCESS;
1176 }
1177 
1179  int glyph_number,
1180  const char *name, int name_length,
1181  const char *charstring, int charstring_length);
1182 
1183 static cairo_status_t
1185  const char *dict_start,
1186  const char *dict_end,
1188  const char **dict_out)
1189 {
1190  int charstring_length, name_length;
1191  const char *p, *charstring, *name;
1192  char *end;
1194  int glyph_count;
1195 
1196  /* We're looking at '/' in the name of the first glyph. The glyph
1197  * definitions are on the form:
1198  *
1199  * /name 23 RD <23 binary bytes> ND
1200  *
1201  * or alternatively using -| and |- instead of RD and ND.
1202  *
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 '/'.
1208  */
1209 
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;
1216 
1217  charstring_length = strtol (p, &end, 10);
1218  if (p == end)
1220 
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;
1224 
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++;
1229 
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)
1234 
1235  status = func (font, glyph_count++,
1236  name, name_length,
1237  charstring, charstring_length);
1238  if (unlikely (status))
1239  return status;
1240  }
1241 
1242  *dict_out = p;
1243 
1244  return CAIRO_STATUS_SUCCESS;
1245 }
1246 
1247 
1248 static cairo_status_t
1250  const char *name)
1251 {
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;
1258  unsigned int i;
1259  int glyph, j;
1260 
1261  /* The private dict holds hint information, common subroutines and
1262  * the actual glyph definitions (charstrings).
1263  *
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.
1268  *
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.
1273  *
1274  * Finally, the private dict is copied to the subset font minus the
1275  * subroutines and charstrings not required.
1276  */
1277 
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)
1287 
1288  lenIV_str = _cairo_malloc (lenIV_end - lenIV_start + 1);
1289  if (unlikely (lenIV_str == NULL))
1291 
1292  strncpy (lenIV_str, lenIV_start, lenIV_end - lenIV_start);
1293  lenIV_str[lenIV_end - lenIV_start] = 0;
1294 
1295  ret = sscanf(lenIV_str, "%d", &lenIV);
1296  free(lenIV_str);
1297 
1298  if (unlikely (ret <= 0))
1300 
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))
1306 
1307  font->lenIV = lenIV;
1308  }
1309 
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;
1317  }
1318 
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)
1324 
1325  if (font->num_subrs <= 0)
1327 
1328  font->subrs = calloc (font->num_subrs, sizeof (font->subrs[0]));
1329  if (unlikely (font->subrs == NULL))
1331 
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 (array_start == NULL)
1336 
1337  /* Read in the subroutines */
1339  array_start,
1340  font->cleartext_end,
1342  &array_end);
1343  if (unlikely(status))
1344  return status;
1345 
1346  p = array_end;
1347 skip_subrs:
1348 
1349  /* Find start of CharStrings */
1350  charstrings = find_token (p, font->cleartext_end, "/CharStrings");
1351  if (charstrings == NULL)
1353 
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)
1359 
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)
1367  dict_start = p;
1368 
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. */
1373  dict_start,
1374  font->cleartext_end,
1376  &dict_end);
1377  if (unlikely(status))
1378  return status;
1379 
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))
1386 
1387  backend = font->scaled_font_subset->scaled_font->backend;
1388  if (!backend->index_to_glyph_name)
1390 
1391  /* Find the glyph number corresponding to each glyph in the subset
1392  * and mark it as in use */
1393 
1394  for (i = 0; i < font->scaled_font_subset->num_glyphs; i++) {
1395  unsigned long index;
1396 
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;
1404 
1406  }
1407 
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;
1417  glyph,
1418  font->glyphs[glyph].encrypted_charstring,
1419  font->glyphs[glyph].encrypted_charstring_length);
1420  if (unlikely (status))
1421  return status;
1422  }
1423 
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;
1428  }
1429 
1430  closefile_token = find_token (dict_end, font->cleartext_end, "closefile");
1431  if (closefile_token == NULL)
1433 
1434  /* We're ready to start outputting. First write the header,
1435  * i.e. the public part of the font dict.*/
1437  if (unlikely (status))
1438  return status;
1439 
1440  font->base.header_size = _cairo_output_stream_get_position (font->output);
1441 
1442  /* Start outputting the private dict */
1443  if (font->subset_subrs) {
1444  /* First output everything up to the start of the Subrs array. */
1446  array_start - font->cleartext);
1447  if (unlikely (status))
1448  return status;
1449 
1450  /* Write out the subr definitions for each of the glyphs in
1451  * the subset. */
1453  array_start,
1454  font->cleartext_end,
1456  &p);
1457  if (unlikely (status))
1458  return status;
1459  } else {
1460  p = font->cleartext;
1461  }
1462 
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. */
1468  if (unlikely (status))
1469  return status;
1470 
1471  /* Write out new charstring count */
1472  length = snprintf (buffer, sizeof buffer,
1473  "/CharStrings %d", font->num_glyphs);
1475  if (unlikely (status))
1476  return status;
1477 
1478  /* Write out text between the charstring count and the first
1479  * charstring definition */
1481  dict_start - glyph_count_end);
1482  if (unlikely (status))
1483  return status;
1484 
1485  /* Write out the charstring definitions for each of the glyphs in
1486  * the subset. */
1488  dict_start,
1489  font->cleartext_end,
1491  &p);
1492  if (unlikely (status))
1493  return status;
1494 
1495  /* Output what's left between the end of the glyph definitions and
1496  * the end of the private dict to the output. */
1498  closefile_token - p + strlen ("closefile") + 1);
1499  if (unlikely (status))
1500  return status;
1501 
1502  if (font->hex_encode)
1503  _cairo_output_stream_write (font->output, "\n", 1);
1504 
1505  return CAIRO_STATUS_SUCCESS;
1506 }
1507 
1508 static cairo_status_t
1510 {
1511  const char *cleartomark_token;
1512  int i;
1513  static const char zeros[65] =
1514  "0000000000000000000000000000000000000000000000000000000000000000\n";
1515 
1516 
1517  for (i = 0; i < 8; i++)
1518  _cairo_output_stream_write (font->output, zeros, sizeof zeros);
1519 
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'. */
1525 
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");
1530 
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. */
1535 
1536  _cairo_output_stream_printf (font->output, "cleartomark\n");
1537  } else {
1539  }
1540 
1541  /* some fonts do not have a newline at the end of the last line */
1542  _cairo_output_stream_printf (font->output, "\n");
1543 
1544  return CAIRO_STATUS_SUCCESS;
1545 }
1546 
1547 static cairo_status_t
1548 type1_font_write (void *closure, const unsigned char *data, unsigned int length)
1549 {
1551 
1552  return _cairo_array_append_multiple (&font->contents, data, length);
1553 }
1554 
1555 static cairo_status_t
1557  const char *name)
1558 {
1560 
1562  if (unlikely (status))
1563  return status;
1564 
1566  if (unlikely (status))
1567  return status;
1568 
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... */
1581  }
1582 
1583  font->eexec_key = CAIRO_TYPE1_PRIVATE_DICT_KEY;
1584  font->hex_column = 0;
1585 
1587  if (unlikely (status))
1588  return status;
1589 
1591  if (unlikely (status))
1592  return status;
1593 
1595  if (unlikely (status))
1596  return status;
1597 
1598  font->base.data_size = _cairo_output_stream_get_position (font->output) -
1599  font->base.header_size;
1600 
1602  if (unlikely (status))
1603  return status;
1604 
1605  font->base.trailer_size =
1607  font->base.header_size - font->base.data_size;
1608 
1609  return CAIRO_STATUS_SUCCESS;
1610 }
1611 
1612 static cairo_bool_t
1613 check_fontdata_is_type1 (const unsigned char *data, long length)
1614 {
1615  /* Test for Type 1 Binary (PFB) */
1616  if (length > 2 && data[0] == 0x80 && data[1] == 0x01)
1617  return TRUE;
1618 
1619  /* Test for Type 1 1 ASCII (PFA) */
1620  if (length > 2 && data[0] == '%' && data[1] == '!')
1621  return TRUE;
1622 
1623  return FALSE;
1624 }
1625 
1626 static cairo_status_t
1628  const char *name)
1629 
1630 {
1631  cairo_type1_font_subset_t *font = abstract_font;
1632  cairo_scaled_font_t *scaled_font;
1634  unsigned long data_length;
1635 
1636  scaled_font = font->scaled_font_subset->scaled_font;
1637  if (!scaled_font->backend->load_type1_data)
1639 
1640  status = scaled_font->backend->load_type1_data (scaled_font, 0, NULL, &data_length);
1641  if (status)
1643 
1644  font->type1_length = data_length;
1645  font->type1_data = _cairo_malloc (font->type1_length);
1646  if (unlikely (font->type1_data == NULL))
1648 
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;
1654 
1655  if (!check_fontdata_is_type1 ((unsigned char *)font->type1_data, data_length))
1657 
1658  status = _cairo_array_grow_by (&font->contents, 4096);
1659  if (unlikely (status))
1660  return status;
1661 
1663  if (unlikely ((status = font->output->status)))
1664  return status;
1665 
1667  if (unlikely (status))
1668  return status;
1669 
1670  font->base.data = _cairo_array_index (&font->contents, 0);
1671 
1672  return status;
1673 }
1674 
1675 static cairo_status_t
1677 {
1679  unsigned int i;
1680 
1681  /* If the subset generation failed, some of the pointers below may
1682  * be NULL depending on at which point the error occurred. */
1683 
1684  _cairo_array_fini (&font->contents);
1685 
1686  free (font->type1_data);
1687  for (i = 0; i < _cairo_array_num_elements (&font->glyph_names_array); i++) {
1688  char **s;
1689 
1690  s = _cairo_array_index (&font->glyph_names_array, i);
1691  free (*s);
1692  }
1693  _cairo_array_fini (&font->glyph_names_array);
1694  _cairo_array_fini (&font->glyphs_array);
1695 
1696  free (font->subrs);
1697 
1698  if (font->output != NULL)
1700 
1701  free (font->base.base_font);
1702 
1703  free (font->subset_index_to_glyphs);
1704 
1705  free (font->cleartext);
1706 
1707  return status;
1708 }
1709 
1712  const char *name,
1713  cairo_scaled_font_subset_t *scaled_font_subset,
1714  cairo_bool_t hex_encode)
1715 {
1718  cairo_bool_t is_synthetic;
1719  unsigned long length;
1720  unsigned int i;
1721  char buf[30];
1722 
1723  /* We need to use a fallback font if this font differs from the type1 outlines. */
1724  if (scaled_font_subset->scaled_font->backend->is_synthetic) {
1725  status = scaled_font_subset->scaled_font->backend->is_synthetic (scaled_font_subset->scaled_font, &is_synthetic);
1726  if (unlikely (status))
1727  return status;
1728 
1729  if (is_synthetic)
1731  }
1732 
1733  status = _cairo_type1_font_subset_init (&font, scaled_font_subset, hex_encode);
1734  if (unlikely (status))
1735  return status;
1736 
1738  if (unlikely (status))
1739  goto fail1;
1740 
1741  if (font.base.base_font) {
1742  type1_subset->base_font = strdup (font.base.base_font);
1743  } else {
1744  snprintf(buf, sizeof (buf), "CairoFont-%u-%u",
1745  scaled_font_subset->font_id, scaled_font_subset->subset_id);
1746  type1_subset->base_font = strdup (buf);
1747  }
1748  if (unlikely (type1_subset->base_font == NULL))
1749  goto fail1;
1750 
1751  type1_subset->widths = calloc (sizeof (double), font.num_glyphs);
1752  if (unlikely (type1_subset->widths == NULL))
1753  goto fail2;
1754  for (i = 0; i < font.base.num_glyphs; i++) {
1755  if (font.glyphs[i].subset_index < 0)
1756  continue;
1757  type1_subset->widths[font.glyphs[i].subset_index] =
1758  font.glyphs[i].width;
1759  }
1760 
1761  type1_subset->x_min = font.base.x_min;
1762  type1_subset->y_min = font.base.y_min;
1763  type1_subset->x_max = font.base.x_max;
1764  type1_subset->y_max = font.base.y_max;
1765  type1_subset->ascent = font.base.ascent;
1766  type1_subset->descent = font.base.descent;
1767 
1768  length = font.base.header_size +
1769  font.base.data_size +
1770  font.base.trailer_size;
1771  type1_subset->data = _cairo_malloc (length);
1772  if (unlikely (type1_subset->data == NULL))
1773  goto fail3;
1774 
1775  memcpy (type1_subset->data,
1776  _cairo_array_index (&font.contents, 0), length);
1777 
1778  type1_subset->header_length = font.base.header_size;
1779  type1_subset->data_length = font.base.data_size;
1780  type1_subset->trailer_length = font.base.trailer_size;
1781 
1783 
1784  fail3:
1785  free (type1_subset->widths);
1786  fail2:
1787  free (type1_subset->base_font);
1788  fail1:
1790 
1791  return status;
1792 }
1793 
1794 void
1796 {
1797  free (subset->base_font);
1798  free (subset->widths);
1799  free (subset->data);
1800 }
1801 
1804 {
1806  unsigned long length;
1807  unsigned char buf[64];
1808 
1809  if (!scaled_font->backend->load_type1_data)
1810  return FALSE;
1811 
1812  status = scaled_font->backend->load_type1_data (scaled_font, 0, NULL, &length);
1813  if (status)
1814  return FALSE;
1815 
1816  /* We only need a few bytes to test for Type 1 */
1817  if (length > sizeof (buf))
1818  length = sizeof (buf);
1819 
1820  status = scaled_font->backend->load_type1_data (scaled_font, 0, buf, &length);
1821  if (status)
1822  return FALSE;
1823 
1825 }
1826 
1827 #endif /* CAIRO_HAS_FONT_SUBSET */
double __cdecl modf(double _X, double *_Y)
long __cdecl strtol(char const *_String, char **_EndPtr, int _Radix)
#define strdup
Definition: Utility.h:167
#define font
Definition: aptex-macros.h:175
#define width(a)
Definition: aptex-macros.h:198
#define num1(a)
#define name
#define num2(a)
#define int_val
char *__cdecl strpbrk(char const *_Str, char const *_Control)
END BEGIN name_length
Definition: bibtex-2.c:2032
cairo_status_t _cairo_array_append_multiple(cairo_array_t *array, const void *elements, unsigned int num_elements)
Definition: cairo-array.c:281
void _cairo_array_fini(cairo_array_t *array)
Definition: cairo-array.c:75
cairo_status_t _cairo_array_grow_by(cairo_array_t *array, unsigned int additional)
Definition: cairo-array.c:89
unsigned int _cairo_array_num_elements(const cairo_array_t *array)
Definition: cairo-array.c:338
void * _cairo_array_index(cairo_array_t *array, unsigned int index)
Definition: cairo-array.c:166
void _cairo_array_init(cairo_array_t *array, unsigned int element_size)
Definition: cairo-array.c:58
cairo_status_t _cairo_array_append(cairo_array_t *array, const void *element)
Definition: cairo-array.c:262
cairo_status_t _cairo_error(cairo_status_t status)
Definition: cairo-error.c:65
@ CAIRO_INT_STATUS_UNSUPPORTED
#define _cairo_malloc(size)
const char * _cairo_get_locale_decimal_point(void)
Definition: cairo-misc.c:781
void _cairo_output_stream_write(cairo_output_stream_t *stream, const void *data, size_t length)
cairo_status_t _cairo_output_stream_destroy(cairo_output_stream_t *stream)
cairo_output_stream_t * _cairo_output_stream_create(cairo_write_func_t write_func, cairo_close_func_t close_func, void *closure)
long _cairo_output_stream_get_position(cairo_output_stream_t *stream)
void _cairo_output_stream_printf(cairo_output_stream_t *stream, const char *fmt,...)
const char * _cairo_ps_standard_encoding_to_glyphname(int glyph)
cairo_int_status_t _cairo_escape_ps_name(char **ps_name)
const char * _cairo_winansi_to_glyphname(int glyph)
#define CAIRO_TYPE1_ENCRYPT_C1
#define CAIRO_TYPE1_ENCRYPT_C2
#define CAIRO_TYPE1_CHARSTRING_KEY
#define CAIRO_TYPE1_PRIVATE_DICT_KEY
cairo_status_t _cairo_type1_subset_init(cairo_type1_subset_t *type1_subset, const char *name, cairo_scaled_font_subset_t *scaled_font_subset, cairo_bool_t hex_encode)
cairo_status_t(* glyph_func_t)(cairo_type1_font_subset_t *font, int glyph_number, const char *name, int name_length, const char *charstring, int charstring_length)
cairo_status_t(* subr_func_t)(cairo_type1_font_subset_t *font, int subr_number, const char *subr_string, int subr_string_length, const char *np, int np_length)
cairo_bool_t _cairo_type1_scaled_font_is_type1(cairo_scaled_font_t *scaled_font)
void _cairo_type1_subset_fini(cairo_type1_subset_t *subset)
struct _cairo_type1_font_subset cairo_type1_font_subset_t
int cairo_bool_t
Definition: cairo.h:107
@ CAIRO_STATUS_SUCCESS
Definition: cairo.h:315
@ CAIRO_STATUS_NO_MEMORY
Definition: cairo.h:317
enum _cairo_status cairo_status_t
static int _cairo_isspace(int c)
Definition: cairoint.h:280
static int _cairo_isdigit(int c)
Definition: cairoint.h:286
#define TYPE1_CHARSTRING_COMMAND_POP
static cairo_status_t cairo_type1_font_subset_write(cairo_type1_font_subset_t *font, const char *name)
static cairo_status_t cairo_type1_font_for_each_subr(cairo_type1_font_subset_t *font, const char *array_start, const char *cleartext_end, subr_func_t func, const char **array_end)
#define TYPE1_CHARSTRING_COMMAND_CLOSEPATH
#define TYPE1_CHARSTRING_COMMAND_RLINETO
#define TYPE1_CHARSTRING_COMMAND_HSTEM3
#define TYPE1_CHARSTRING_COMMAND_VSTEM3
#define TYPE1_STACKSIZE
static cairo_status_t cairo_type1_font_subset_for_each_glyph(cairo_type1_font_subset_t *font, const char *dict_start, const char *dict_end, glyph_func_t func, const char **dict_out)
static cairo_status_t cairo_type1_font_subset_get_bbox(cairo_type1_font_subset_t *font)
static cairo_status_t use_standard_encoding_glyph(cairo_type1_font_subset_t *font, int index)
#define TYPE1_CHARSTRING_COMMAND_VSTEM
static cairo_status_t cairo_type1_font_subset_get_fontname(cairo_type1_font_subset_t *font)
#define TYPE1_CHARSTRING_COMMAND_HMOVETO
static const char * skip_token(const char *p, const char *end)
static cairo_status_t _cairo_type1_font_subset_fini(cairo_type1_font_subset_t *font)
static cairo_status_t cairo_type1_font_subset_parse_charstring(cairo_type1_font_subset_t *font, int glyph, const char *encrypted_charstring, int encrypted_charstring_length)
static cairo_status_t type1_font_write(void *closure, const unsigned char *data, unsigned int length)
static cairo_status_t cairo_type1_font_subset_build_glyph_list(cairo_type1_font_subset_t *font, int glyph_number, const char *name, int name_length, const char *encrypted_charstring, int encrypted_charstring_length)
#define TYPE1_CHARSTRING_COMMAND_SBW
#define TYPE1_CHARSTRING_COMMAND_ENDCHAR
#define TYPE1_CHARSTRING_COMMAND_ESCAPE
static const unsigned char * cairo_type1_font_subset_decode_integer(const unsigned char *p, int *integer)
static cairo_status_t cairo_type1_font_subset_get_matrix(cairo_type1_font_subset_t *font, const char *name, double *a, double *b, double *c, double *d)
#define TYPE1_CHARSTRING_COMMAND_VHCURVETO
static cairo_status_t cairo_type1_font_subset_write_private_dict(cairo_type1_font_subset_t *font, const char *name)
static const char * find_token(const char *buffer, const char *end, const char *token)
#define TYPE1_CHARSTRING_COMMAND_RRCURVETO
static cairo_status_t write_used_subrs(cairo_type1_font_subset_t *font, int subr_number, const char *subr_string, int subr_string_length, const char *np, int np_length)
static void cairo_type1_font_subset_decrypt_charstring(const unsigned char *in, int size, unsigned char *out)
static cairo_status_t cairo_type1_font_subset_write_encrypted(cairo_type1_font_subset_t *font, const char *data, unsigned int length)
static cairo_status_t write_used_glyphs(cairo_type1_font_subset_t *font, int glyph_number, const char *name, int name_length, const char *charstring, int charstring_length)
#define TYPE1_CHARSTRING_COMMAND_SEAC
#define TYPE1_CHARSTRING_COMMAND_HSBW
#define TYPE1_CHARSTRING_COMMAND_VMOVETO
static cairo_status_t cairo_type1_font_subset_build_subr_list(cairo_type1_font_subset_t *font, int subr_number, const char *encrypted_charstring, int encrypted_charstring_length, const char *np, int np_length)
static int hex_to_int(int ch)
static cairo_status_t _cairo_type1_font_subset_init(cairo_type1_font_subset_t *font, cairo_scaled_font_subset_t *scaled_font_subset, cairo_bool_t hex_encode)
static cairo_status_t cairo_type1_font_subset_decrypt_eexec_segment(cairo_type1_font_subset_t *font)
static cairo_status_t cairo_type1_font_subset_write_trailer(cairo_type1_font_subset_t *font)
static cairo_bool_t is_ps_delimiter(int c)
#define TYPE1_CHARSTRING_COMMAND_DIV
#define TYPE1_CHARSTRING_COMMAND_HSTEM
static void cairo_type1_font_subset_use_glyph(cairo_type1_font_subset_t *font, int glyph)
#define TYPE1_CHARSTRING_COMMAND_DOTSECTION
#define TYPE1_CHARSTRING_COMMAND_CALLSUBR
#define TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT
static cairo_bool_t check_fontdata_is_type1(const unsigned char *data, long length)
static cairo_status_t cairo_type1_font_subset_find_segments(cairo_type1_font_subset_t *font)
static cairo_status_t cairo_type1_font_subset_write_header(cairo_type1_font_subset_t *font, const char *name)
#define TYPE1_CHARSTRING_COMMAND_VLINETO
#define TYPE1_CHARSTRING_COMMAND_HLINETO
static void cairo_type1_font_erase_dict_key(cairo_type1_font_subset_t *font, const char *key)
#define TYPE1_CHARSTRING_COMMAND_RETURN
static cairo_status_t cairo_type1_font_subset_generate(void *abstract_font, const char *name)
#define TYPE1_CHARSTRING_COMMAND_RMOVETO
#define TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR
#define TYPE1_CHARSTRING_COMMAND_HVCURVETO
#define b
Definition: jpegint.h:372
@ FALSE
Definition: dd.h:101
@ TRUE
Definition: dd.h:102
#define free(a)
Definition: decNumber.cpp:310
int command
Definition: t4ht.c:934
static int np
Definition: bifont.c:64
static int xx[600]
Definition: psspecial.c:35
static int yy[600]
Definition: psspecial.c:35
char * strncpy()
int strcmp()
Definition: coll.cpp:143
int sscanf()
struct rect data
Definition: dvipdfm.c:64
int x_max
int y_min
int y_max
int x_min
#define s
Definition: afcover.h:80
#define c(n)
Definition: gpos-common.c:150
#define a(n)
Definition: gpos-common.c:148
#define d(n)
Definition: gpos-common.c:151
#define strchr
Definition: gsftopk.c:59
#define memcmp(s1, s2, n)
Definition: gsftopk.c:66
#define memcpy(d, s, n)
Definition: gsftopk.c:64
FILE * out
Definition: hbf2gf.c:286
assert(pcxLoadImage24((char *)((void *) 0), fp, pinfo, hdr))
#define unlikely(x)
Definition: jbig2arith.cc:116
#define MIN(a, b)
Definition: jpegint.h:269
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p
Definition: afcover.h:72
small capitals from c petite p scientific i
Definition: afcover.h:80
sizeof(AF_ModuleRec)
for(n=0;n< outline->n_points;n++)
Definition: ftbbox.c:494
lenIV
Definition: t1tokens.h:60
voidp calloc()
#define buf
static int ret
Definition: convert.c:72
char * closure
Definition: font.h:85
#define length(c)
Definition: ctangleboot.c:65
int strncmp()
#define snprintf
Definition: snprintf.c:41
static luaL_Reg func[]
Definition: except.c:32
static PermString lenIV_str
Definition: t1font.cc:30
unsigned digits[12]
Definition: out_routines.c:255
int integer
Definition: pbmtopk.c:38
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld cleanup[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1 beq endif SRC MASK if dst_r_bpp DST_R else add endif PF add sub src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head pixblock_size cache_preload_simple process_pixblock_tail pixinterleave dst_w_basereg irp beq endif process_pixblock_tail_head tst beq irp if pixblock_size chunk_size tst beq pixld_src SRC pixld MASK if DST_R else pixld DST_R endif if src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head if pixblock_size cache_preload_simple endif process_pixblock_tail pixinterleave dst_w_basereg irp if pixblock_size chunk_size tst beq if DST_W else pixst DST_W else mov ORIG_W endif add lsl if lsl endif if lsl endif lsl endif lsl endif lsl endif subs mov DST_W if regs_shortage str endif bge start_of_loop_label endm macro generate_composite_function
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
#define index(s, c)
Definition: plain2.h:351
static int size
Definition: ppmlabel.c:24
int r
Definition: ppmqvga.c:68
bstring c int memset(void *s, int c, int length)
#define status
static backend_t backend[]
Definition: main.c:103
static FILE * in
Definition: squeeze.c:36
cairo_int_status_t(* load_type1_data)(void *scaled_font, long offset, unsigned char *buffer, unsigned long *length)
Definition: cairoint.h:613
cairo_int_status_t(* is_synthetic)(void *scaled_font, cairo_bool_t *is_synthetic)
Definition: cairoint.h:576
unsigned int font_id
Definition: cairoint.h:492
cairo_scaled_font_t * scaled_font
Definition: cairoint.h:491
unsigned int subset_id
Definition: cairoint.h:493
const cairo_scaled_font_backend_t * backend
cairo_output_stream_t * output
struct _cairo_type1_font_subset::@464 base
cairo_scaled_font_subset_t * scaled_font_subset
struct _cairo_type1_font_subset::@466 build_stack
struct _cairo_type1_font_subset::@465 * subrs
struct _cairo_type1_font_subset::@467 ps_stack
Definition: utils.c:300
Definition: pbmfont.h:11
Definition: pbmfont.h:4
int width
Definition: pbmfont.h:5
Definition: mendex.h:20
Definition: spc_misc.c:56
Definition: parser.c:43
Definition: strexpr.c:21
int subrs
Definition: t1part.c:260
ch
Definition: t4ht.c:1443
int j
Definition: t4ht.c:1589
*job_name strlen((char *) job_name) - 4)
#define key
Definition: tex2xindy.c:753
val
Definition: tex4ht.c:3227
TT_Glyph glyph
Definition: ttf2pfb.c:162
@ start
Definition: preamble.c:52
#define end(cp)
Definition: zic.c:71