-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcairo-type1-subset.c
1799 lines (1489 loc) · 52.5 KB
/
cairo-type1-subset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Red Hat, Inc.
*
* Contributor(s):
* Kristian Høgsberg <[email protected]>
*/
/*
* Useful links:
* http://partners.adobe.com/public/developer/en/font/T1_SPEC.PDF
*/
#define _BSD_SOURCE /* for snprintf(), strdup() */
#include "cairoint.h"
#include "cairo-array-private.h"
#include "cairo-error-private.h"
#if CAIRO_HAS_FONT_SUBSET
#include "cairo-type1-private.h"
#include "cairo-scaled-font-subsets-private.h"
#include "cairo-output-stream-private.h"
#include <ctype.h>
#include <locale.h>
#define TYPE1_STACKSIZE 24 /* Defined in Type 1 Font Format */
typedef struct {
int subset_index;
double width;
const char *encrypted_charstring;
int encrypted_charstring_length;
} glyph_data_t;
typedef struct _cairo_type1_font_subset {
cairo_scaled_font_subset_t *scaled_font_subset;
struct {
unsigned int font_id;
char *base_font;
unsigned int num_glyphs;
double x_min, y_min, x_max, y_max;
double ascent, descent;
double units_per_em;
const char *data;
unsigned long header_size;
unsigned long data_size;
unsigned long trailer_size;
} base;
int num_glyphs;
/* The glyphs and glyph_names arrays are indexed by the order of
* the Charstrings in the font. This is not necessarily the same
* order as the glyph index. The index_to_glyph_name() font backend
* function is used to map the glyph index to the glyph order in
* the Charstrings. */
glyph_data_t *glyphs;
char **glyph_names;
cairo_array_t glyphs_array;
cairo_array_t glyph_names_array;
int num_subrs;
cairo_bool_t subset_subrs;
struct {
const char *subr_string;
int subr_length;
const char *np;
int np_length;
cairo_bool_t used;
} *subrs;
/* Indexed by subset_index this maps to the glyph order in the
* glyph_names and glyphs arrays. Has font->num_golyphs
* elements. */
int *subset_index_to_glyphs;
cairo_output_stream_t *output;
cairo_array_t contents;
const char *rd, *nd, *np;
int lenIV;
char *type1_data;
unsigned int type1_length;
char *type1_end;
char *header_segment;
int header_segment_size;
char *eexec_segment;
int eexec_segment_size;
cairo_bool_t eexec_segment_is_ascii;
char *cleartext;
char *cleartext_end;
int header_size;
unsigned short eexec_key;
cairo_bool_t hex_encode;
int hex_column;
struct {
double stack[TYPE1_STACKSIZE];
int sp;
} build_stack;
struct {
int stack[TYPE1_STACKSIZE];
int sp;
} ps_stack;
} cairo_type1_font_subset_t;
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)
{
memset (font, 0, sizeof (*font));
font->scaled_font_subset = scaled_font_subset;
_cairo_array_init (&font->glyphs_array, sizeof (glyph_data_t));
_cairo_array_init (&font->glyph_names_array, sizeof (char *));
font->subset_index_to_glyphs = NULL;
font->base.num_glyphs = 0;
font->num_subrs = 0;
font->subset_subrs = TRUE;
font->subrs = NULL;
font->hex_encode = hex_encode;
font->num_glyphs = 0;
_cairo_array_init (&font->contents, sizeof (char));
return CAIRO_STATUS_SUCCESS;
}
static void
cairo_type1_font_subset_use_glyph (cairo_type1_font_subset_t *font, int glyph)
{
if (font->glyphs[glyph].subset_index >= 0)
return;
font->glyphs[glyph].subset_index = font->num_glyphs;
font->subset_index_to_glyphs[font->num_glyphs] = glyph;
font->num_glyphs++;
}
static cairo_bool_t
is_ps_delimiter(int c)
{
static const char delimiters[] = "()[]{}<>/% \t\r\n";
return strchr (delimiters, c) != NULL;
}
static const char *
find_token (const char *buffer, const char *end, const char *token)
{
int i, length;
/* FIXME: find substring really must be find_token */
if (buffer == NULL)
return NULL;
length = strlen (token);
for (i = 0; buffer + i < end - length + 1; i++)
if (memcmp (buffer + i, token, length) == 0)
if ((i == 0 || token[0] == '/' || is_ps_delimiter(buffer[i - 1])) &&
(buffer + i == end - length || is_ps_delimiter(buffer[i + length])))
return buffer + i;
return NULL;
}
static cairo_status_t
cairo_type1_font_subset_find_segments (cairo_type1_font_subset_t *font)
{
unsigned char *p;
const char *eexec_token;
int size, i;
p = (unsigned char *) font->type1_data;
font->type1_end = font->type1_data + font->type1_length;
if (p[0] == 0x80 && p[1] == 0x01) {
font->header_segment_size =
p[2] | (p[3] << 8) | (p[4] << 16) | (p[5] << 24);
font->header_segment = (char *) p + 6;
p += 6 + font->header_segment_size;
font->eexec_segment_size =
p[2] | (p[3] << 8) | (p[4] << 16) | (p[5] << 24);
font->eexec_segment = (char *) p + 6;
font->eexec_segment_is_ascii = (p[1] == 1);
p += 6 + font->eexec_segment_size;
while (p < (unsigned char *) (font->type1_end) && p[1] != 0x03) {
size = p[2] | (p[3] << 8) | (p[4] << 16) | (p[5] << 24);
p += 6 + size;
}
font->type1_end = (char *) p;
} else {
eexec_token = find_token ((char *) p, font->type1_end, "eexec");
if (eexec_token == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
font->header_segment_size = eexec_token - (char *) p + strlen ("eexec\n");
font->header_segment = (char *) p;
font->eexec_segment_size = font->type1_length - font->header_segment_size;
font->eexec_segment = (char *) p + font->header_segment_size;
font->eexec_segment_is_ascii = TRUE;
for (i = 0; i < 4; i++) {
if (!isxdigit(font->eexec_segment[i]))
font->eexec_segment_is_ascii = FALSE;
}
}
return CAIRO_STATUS_SUCCESS;
}
/* Search for the definition of key and erase it by overwriting with spaces.
* This function is looks for definitions of the form:
*
* /key1 1234 def
* /key2 [12 34 56] def
*
* ie a key defined as an integer or array of integers.
*
*/
static void
cairo_type1_font_erase_dict_key (cairo_type1_font_subset_t *font,
const char *key)
{
const char *start, *p, *segment_end;
segment_end = font->header_segment + font->header_segment_size;
start = font->header_segment;
do {
start = find_token (start, segment_end, key);
if (start) {
p = start + strlen(key);
/* skip integers or array of integers */
while (p < segment_end &&
(_cairo_isspace(*p) ||
_cairo_isdigit(*p) ||
*p == '[' ||
*p == ']'))
{
p++;
}
if (p + 3 < segment_end && memcmp(p, "def", 3) == 0) {
/* erase definition of the key */
memset((char *) start, ' ', p + 3 - start);
}
start += strlen(key);
}
} while (start);
}
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)
{
const char *start, *end, *segment_end;
int ret, s_max, i, j;
char *s;
struct lconv *locale_data;
const char *decimal_point;
int decimal_point_len;
locale_data = localeconv ();
decimal_point = locale_data->decimal_point;
decimal_point_len = strlen (decimal_point);
assert (decimal_point_len != 0);
segment_end = font->header_segment + font->header_segment_size;
start = find_token (font->header_segment, segment_end, name);
if (start == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
end = find_token (start, segment_end, "def");
if (end == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
s_max = end - start + 5*decimal_point_len + 1;
s = malloc (s_max);
if (unlikely (s == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
i = 0;
j = 0;
while (i < end - start && j < s_max - decimal_point_len) {
if (start[i] == '.') {
strncpy(s + j, decimal_point, decimal_point_len);
i++;
j += decimal_point_len;
} else {
s[j++] = start[i++];
}
}
s[j] = 0;
start = strpbrk (s, "{[");
if (!start) {
free (s);
return CAIRO_INT_STATUS_UNSUPPORTED;
}
start++;
ret = 0;
if (*start)
ret = sscanf(start, "%lf %lf %lf %lf", a, b, c, d);
free (s);
if (ret != 4)
return CAIRO_INT_STATUS_UNSUPPORTED;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cairo_type1_font_subset_get_bbox (cairo_type1_font_subset_t *font)
{
cairo_status_t status;
double x_min, y_min, x_max, y_max;
double xx, yx, xy, yy;
status = cairo_type1_font_subset_get_matrix (font, "/FontBBox",
&x_min,
&y_min,
&x_max,
&y_max);
if (unlikely (status))
return status;
status = cairo_type1_font_subset_get_matrix (font, "/FontMatrix",
&xx, &yx, &xy, &yy);
if (unlikely (status))
return status;
if (yy == 0.0)
return CAIRO_INT_STATUS_UNSUPPORTED;
/* Freetype uses 1/yy to get units per EM */
font->base.units_per_em = 1.0/yy;
font->base.x_min = x_min / font->base.units_per_em;
font->base.y_min = y_min / font->base.units_per_em;
font->base.x_max = x_max / font->base.units_per_em;
font->base.y_max = y_max / font->base.units_per_em;
font->base.ascent = font->base.y_max;
font->base.descent = font->base.y_min;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cairo_type1_font_subset_get_fontname (cairo_type1_font_subset_t *font)
{
const char *start, *end, *segment_end;
char *s;
int i;
segment_end = font->header_segment + font->header_segment_size;
start = find_token (font->header_segment, segment_end, "/FontName");
if (start == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
start += strlen ("/FontName");
end = find_token (start, segment_end, "def");
if (end == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
s = malloc (end - start + 1);
if (unlikely (s == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
strncpy (s, start, end - start);
s[end - start] = 0;
start = strchr (s, '/');
if (!start++ || !start) {
free (s);
return CAIRO_INT_STATUS_UNSUPPORTED;
}
/* If font name is prefixed with a subset tag, strip it off. */
if (strlen(start) > 7 && start[6] == '+') {
for (i = 0; i < 6; i++) {
if (start[i] < 'A' || start[i] > 'Z')
break;
}
if (i == 6)
start += 7;
}
font->base.base_font = strdup (start);
free (s);
if (unlikely (font->base.base_font == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
s = font->base.base_font;
while (*s && !is_ps_delimiter(*s))
s++;
*s = 0;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cairo_type1_font_subset_write_header (cairo_type1_font_subset_t *font,
const char *name)
{
const char *start, *end, *segment_end;
unsigned int i;
/* FIXME:
* This function assumes that /FontName always appears
* before /Encoding. This appears to always be the case with Type1
* fonts.
*
* The more recently added code for removing the UniqueID and XUID
* keys can not make any assumptions about the position of the
* keys in the dictionary so it is implemented by overwriting the
* key definition with spaces before we start copying the font to
* the output.
*
* This code should be rewritten to not make any assumptions about
* the order of dictionary keys. This will allow UniqueID to be
* stripped out instead of leaving a bunch of spaces in the
* output.
*/
cairo_type1_font_erase_dict_key (font, "/UniqueID");
cairo_type1_font_erase_dict_key (font, "/XUID");
segment_end = font->header_segment + font->header_segment_size;
/* Type 1 fonts created by Fontforge have some PostScript code at
* the start of the font that skips the font if the printer has a
* cached copy of the font with the same unique id. This breaks
* our subsetted font so we disable it by searching for the
* PostScript operator "known" when used to check for the
* "/UniqueID" dictionary key. We append " pop false " after it to
* pop the result of this check off the stack and replace it with
* "false" to make the PostScript code think "/UniqueID" does not
* exist.
*/
end = font->header_segment;
start = find_token (font->header_segment, segment_end, "/UniqueID");
if (start) {
start += 9;
while (start < segment_end && _cairo_isspace (*start))
start++;
if (start + 5 < segment_end && memcmp(start, "known", 5) == 0) {
_cairo_output_stream_write (font->output, font->header_segment,
start + 5 - font->header_segment);
_cairo_output_stream_printf (font->output, " pop false ");
end = start + 5;
}
}
start = find_token (end, segment_end, "/FontName");
if (start == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
_cairo_output_stream_write (font->output, end,
start - end);
_cairo_output_stream_printf (font->output, "/FontName /%s def", name);
end = find_token (start, segment_end, "def");
if (end == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
end += 3;
start = find_token (end, segment_end, "/Encoding");
if (start == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
_cairo_output_stream_write (font->output, end, start - end);
_cairo_output_stream_printf (font->output,
"/Encoding 256 array\n"
"0 1 255 {1 index exch /.notdef put} for\n");
if (font->scaled_font_subset->is_latin) {
for (i = 1; i < 256; i++) {
int subset_glyph = font->scaled_font_subset->latin_to_subset_glyph_index[i];
int glyph_num = font->subset_index_to_glyphs[subset_glyph];
if (subset_glyph > 0) {
_cairo_output_stream_printf (font->output,
"dup %d /%s put\n",
i,
font->glyph_names[glyph_num]);
}
}
} else {
for (i = 0; i < font->base.num_glyphs; i++) {
if (font->glyphs[i].subset_index <= 0)
continue;
_cairo_output_stream_printf (font->output,
"dup %d /%s put\n",
font->glyphs[i].subset_index,
font->glyph_names[i]);
}
}
_cairo_output_stream_printf (font->output, "readonly def");
end = find_token (start, segment_end, "def");
if (end == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
end += 3;
/* There are some buggy fonts that contain more than one /Encoding */
if (find_token (end, segment_end, "/Encoding"))
return CAIRO_INT_STATUS_UNSUPPORTED;
_cairo_output_stream_write (font->output, end, segment_end - end);
return font->output->status;
}
static int
hex_to_int (int ch)
{
if (ch <= '9')
return ch - '0';
else if (ch <= 'F')
return ch - 'A' + 10;
else
return ch - 'a' + 10;
}
static cairo_status_t
cairo_type1_font_subset_write_encrypted (cairo_type1_font_subset_t *font,
const char *data, unsigned int length)
{
const unsigned char *in, *end;
int c, p;
static const char hex_digits[16] = "0123456789abcdef";
char digits[3];
in = (const unsigned char *) data;
end = (const unsigned char *) data + length;
while (in < end) {
p = *in++;
c = p ^ (font->eexec_key >> 8);
font->eexec_key = (c + font->eexec_key) * CAIRO_TYPE1_ENCRYPT_C1 + CAIRO_TYPE1_ENCRYPT_C2;
if (font->hex_encode) {
digits[0] = hex_digits[c >> 4];
digits[1] = hex_digits[c & 0x0f];
digits[2] = '\n';
font->hex_column += 2;
if (font->hex_column == 78) {
_cairo_output_stream_write (font->output, digits, 3);
font->hex_column = 0;
} else {
_cairo_output_stream_write (font->output, digits, 2);
}
} else {
digits[0] = c;
_cairo_output_stream_write (font->output, digits, 1);
}
}
return font->output->status;
}
static cairo_status_t
cairo_type1_font_subset_decrypt_eexec_segment (cairo_type1_font_subset_t *font)
{
unsigned short r = CAIRO_TYPE1_PRIVATE_DICT_KEY;
unsigned char *in, *end;
char *out;
int c, p;
int i;
in = (unsigned char *) font->eexec_segment;
end = (unsigned char *) in + font->eexec_segment_size;
font->cleartext = malloc (font->eexec_segment_size + 1);
if (unlikely (font->cleartext == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
out = font->cleartext;
while (in < end) {
if (font->eexec_segment_is_ascii) {
c = *in++;
if (_cairo_isspace (c))
continue;
c = (hex_to_int (c) << 4) | hex_to_int (*in++);
} else {
c = *in++;
}
p = c ^ (r >> 8);
r = (c + r) * CAIRO_TYPE1_ENCRYPT_C1 + CAIRO_TYPE1_ENCRYPT_C2;
*out++ = p;
}
font->cleartext_end = out;
/* Overwrite random bytes with spaces.
*
* The first 4 bytes of the cleartext are the random bytes
* required by the encryption algorithm. When encrypting the
* cleartext, the first ciphertext byte must not be a white space
* character and the first 4 bytes must not be an ASCII Hex
* character. Some fonts do not check that their randomly chosen
* bytes results in ciphertext that complies with this
* restriction. This may cause problems for some PDF consumers. By
* replacing the random bytes with spaces, the first four bytes of
* ciphertext will always be 0xf9, 0x83, 0xef, 0x00 which complies
* with this restriction. Using spaces also means we don't have to
* skip over the random bytes when parsing the cleartext.
*/
for (i = 0; i < 4 && i < font->eexec_segment_size; i++)
font->cleartext[i] = ' ';
/* Ensure strtol() can not scan past the end of the cleartext */
font->cleartext[font->eexec_segment_size] = 0;
return CAIRO_STATUS_SUCCESS;
}
static const char *
skip_token (const char *p, const char *end)
{
while (p < end && _cairo_isspace(*p))
p++;
while (p < end && !_cairo_isspace(*p))
p++;
if (p == end)
return NULL;
return p;
}
static void
cairo_type1_font_subset_decrypt_charstring (const unsigned char *in, int size, unsigned char *out)
{
unsigned short r = CAIRO_TYPE1_CHARSTRING_KEY;
int c, p, i;
for (i = 0; i < size; i++) {
c = *in++;
p = c ^ (r >> 8);
r = (c + r) * CAIRO_TYPE1_ENCRYPT_C1 + CAIRO_TYPE1_ENCRYPT_C2;
*out++ = p;
}
}
static const unsigned char *
cairo_type1_font_subset_decode_integer (const unsigned char *p, int *integer)
{
if (*p <= 246) {
*integer = *p++ - 139;
} else if (*p <= 250) {
*integer = (p[0] - 247) * 256 + p[1] + 108;
p += 2;
} else if (*p <= 254) {
*integer = -(p[0] - 251) * 256 - p[1] - 108;
p += 2;
} else {
*integer = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
p += 5;
}
return p;
}
static cairo_status_t
use_standard_encoding_glyph (cairo_type1_font_subset_t *font, int index)
{
const char *glyph_name;
unsigned int i;
if (index < 0 || index > 255)
return CAIRO_STATUS_SUCCESS;
glyph_name = _cairo_ps_standard_encoding_to_glyphname (index);
if (glyph_name == NULL)
return CAIRO_STATUS_SUCCESS;
for (i = 0; i < font->base.num_glyphs; i++) {
if (font->glyph_names[i] && strcmp (font->glyph_names[i], glyph_name) == 0) {
cairo_type1_font_subset_use_glyph (font, i);
return CAIRO_STATUS_SUCCESS;
}
}
return CAIRO_INT_STATUS_UNSUPPORTED;
}
#define TYPE1_CHARSTRING_COMMAND_HSTEM 0x01
#define TYPE1_CHARSTRING_COMMAND_VSTEM 0x03
#define TYPE1_CHARSTRING_COMMAND_VMOVETO 0x04
#define TYPE1_CHARSTRING_COMMAND_RLINETO 0x05
#define TYPE1_CHARSTRING_COMMAND_HLINETO 0x06
#define TYPE1_CHARSTRING_COMMAND_VLINETO 0x07
#define TYPE1_CHARSTRING_COMMAND_RRCURVETO 0x08
#define TYPE1_CHARSTRING_COMMAND_CLOSEPATH 0x09
#define TYPE1_CHARSTRING_COMMAND_CALLSUBR 0x0a
#define TYPE1_CHARSTRING_COMMAND_RETURN 0x0b
#define TYPE1_CHARSTRING_COMMAND_ESCAPE 0x0c
#define TYPE1_CHARSTRING_COMMAND_HSBW 0x0d
#define TYPE1_CHARSTRING_COMMAND_ENDCHAR 0x0e
#define TYPE1_CHARSTRING_COMMAND_RMOVETO 0x15
#define TYPE1_CHARSTRING_COMMAND_HMOVETO 0x16
#define TYPE1_CHARSTRING_COMMAND_VHCURVETO 0x1e
#define TYPE1_CHARSTRING_COMMAND_HVCURVETO 0x1f
#define TYPE1_CHARSTRING_COMMAND_DOTSECTION 0x0c00
#define TYPE1_CHARSTRING_COMMAND_VSTEM3 0x0c01
#define TYPE1_CHARSTRING_COMMAND_HSTEM3 0x0c02
#define TYPE1_CHARSTRING_COMMAND_SEAC 0x0c06
#define TYPE1_CHARSTRING_COMMAND_SBW 0x0c07
#define TYPE1_CHARSTRING_COMMAND_DIV 0x0c0c
#define TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR 0x0c10
#define TYPE1_CHARSTRING_COMMAND_POP 0x0c11
#define TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT 0x0c21
/* Parse the charstring, including recursing into subroutines. Find
* the glyph width, subroutines called, and glyphs required by the
* SEAC operator. */
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)
{
cairo_status_t status;
unsigned char *charstring;
const unsigned char *end;
const unsigned char *p;
int command;
charstring = malloc (encrypted_charstring_length);
if (unlikely (charstring == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
cairo_type1_font_subset_decrypt_charstring ((const unsigned char *)
encrypted_charstring,
encrypted_charstring_length,
charstring);
end = charstring + encrypted_charstring_length;
p = charstring + font->lenIV;
status = CAIRO_STATUS_SUCCESS;
while (p < end) {
if (*p < 32) {
command = *p++;
switch (command) {
case TYPE1_CHARSTRING_COMMAND_HSTEM:
case TYPE1_CHARSTRING_COMMAND_VSTEM:
case TYPE1_CHARSTRING_COMMAND_VMOVETO:
case TYPE1_CHARSTRING_COMMAND_RLINETO:
case TYPE1_CHARSTRING_COMMAND_HLINETO:
case TYPE1_CHARSTRING_COMMAND_VLINETO:
case TYPE1_CHARSTRING_COMMAND_RRCURVETO:
case TYPE1_CHARSTRING_COMMAND_CLOSEPATH:
case TYPE1_CHARSTRING_COMMAND_RMOVETO:
case TYPE1_CHARSTRING_COMMAND_HMOVETO:
case TYPE1_CHARSTRING_COMMAND_VHCURVETO:
case TYPE1_CHARSTRING_COMMAND_HVCURVETO:
case TYPE1_CHARSTRING_COMMAND_RETURN:
case TYPE1_CHARSTRING_COMMAND_ENDCHAR:
default:
/* stack clearing operator */
font->build_stack.sp = 0;
break;
case TYPE1_CHARSTRING_COMMAND_CALLSUBR:
if (font->subset_subrs && font->build_stack.sp > 0) {
double int_val;
if (modf(font->build_stack.stack[--font->build_stack.sp], &int_val) == 0.0) {
int subr_num = int_val;
if (subr_num >= 0 && subr_num < font->num_subrs) {
font->subrs[subr_num].used = TRUE;
status = cairo_type1_font_subset_parse_charstring (
font,
glyph,
font->subrs[subr_num].subr_string,
font->subrs[subr_num].subr_length);
break;
}
}
}
font->subset_subrs = FALSE;
break;
case TYPE1_CHARSTRING_COMMAND_HSBW:
if (font->build_stack.sp < 2) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
font->glyphs[glyph].width = font->build_stack.stack[1]/font->base.units_per_em;
font->build_stack.sp = 0;
break;
case TYPE1_CHARSTRING_COMMAND_ESCAPE:
command = command << 8 | *p++;
switch (command) {
case TYPE1_CHARSTRING_COMMAND_DOTSECTION:
case TYPE1_CHARSTRING_COMMAND_VSTEM3:
case TYPE1_CHARSTRING_COMMAND_HSTEM3:
case TYPE1_CHARSTRING_COMMAND_SETCURRENTPOINT:
default:
/* stack clearing operator */
font->build_stack.sp = 0;
break;
case TYPE1_CHARSTRING_COMMAND_SEAC:
/* The seac command takes five integer arguments. The
* last two are glyph indices into the PS standard
* encoding give the names of the glyphs that this
* glyph is composed from. All we need to do is to
* make sure those glyphs are present in the subset
* under their standard names. */
if (font->build_stack.sp < 5) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
status = use_standard_encoding_glyph (font, font->build_stack.stack[3]);
if (unlikely (status))
goto cleanup;
status = use_standard_encoding_glyph (font, font->build_stack.stack[4]);
if (unlikely (status))
goto cleanup;
font->build_stack.sp = 0;
break;
case TYPE1_CHARSTRING_COMMAND_SBW:
if (font->build_stack.sp < 4) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
font->glyphs[glyph].width = font->build_stack.stack[2]/font->base.units_per_em;
font->build_stack.sp = 0;
break;
case TYPE1_CHARSTRING_COMMAND_DIV:
if (font->build_stack.sp < 2) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
} else {
double num1 = font->build_stack.stack[font->build_stack.sp - 2];
double num2 = font->build_stack.stack[font->build_stack.sp - 1];
font->build_stack.sp--;
if (num2 == 0.0) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
font->build_stack.stack[font->build_stack.sp - 1] = num1/num2;
}
break;
case TYPE1_CHARSTRING_COMMAND_CALLOTHERSUBR:
if (font->build_stack.sp < 1) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
font->build_stack.sp--;
font->ps_stack.sp = 0;
while (font->build_stack.sp)
font->ps_stack.stack[font->ps_stack.sp++] = font->build_stack.stack[--font->build_stack.sp];
break;
case TYPE1_CHARSTRING_COMMAND_POP:
if (font->ps_stack.sp < 1) {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
/* T1 spec states that if the interpreter does not
* support executing the callothersub, the results
* must be taken from the callothersub arguments. */
font->build_stack.stack[font->build_stack.sp++] = font->ps_stack.stack[--font->ps_stack.sp];
break;
}
break;
}
} else {
/* integer argument */
if (font->build_stack.sp < TYPE1_STACKSIZE) {
int val;
p = cairo_type1_font_subset_decode_integer (p, &val);
font->build_stack.stack[font->build_stack.sp++] = val;
} else {
status = CAIRO_INT_STATUS_UNSUPPORTED;
goto cleanup;
}
}
}
cleanup:
free (charstring);
return status;
}
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)
{
font->subrs[subr_number].subr_string = encrypted_charstring;
font->subrs[subr_number].subr_length = encrypted_charstring_length;
font->subrs[subr_number].np = np;
font->subrs[subr_number].np_length = np_length;
return CAIRO_STATUS_SUCCESS;
}
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)
{
cairo_status_t status;
char buffer[256];
int length;
if (!font->subrs[subr_number].used)
return CAIRO_STATUS_SUCCESS;
length = snprintf (buffer, sizeof buffer,
"dup %d %d %s ",
subr_number, subr_string_length, font->rd);
status = cairo_type1_font_subset_write_encrypted (font, buffer, length);
if (unlikely (status))
return status;
status = cairo_type1_font_subset_write_encrypted (font,
subr_string,
subr_string_length);
if (unlikely (status))
return status;