-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcairo-cff-subset.c
3435 lines (2925 loc) · 100 KB
/
cairo-cff-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; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2006 Adrian Johnson
*
* 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 Adrian Johnson.
*
* Contributor(s):
* Adrian Johnson <[email protected]>
* Eugeniy Meshcheryakov <[email protected]>
*/
/*
* Useful links:
* http://www.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5176.CFF.pdf
* http://www.adobe.com/content/dam/Adobe/en/devnet/font/pdfs/5177.Type2.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-scaled-font-subsets-private.h"
#include "cairo-truetype-subset-private.h"
#include <string.h>
#include <locale.h>
/* CFF Dict Operators. If the high byte is 0 the command is encoded
* with a single byte. */
#define BASEFONTNAME_OP 0x0c16
#define CIDCOUNT_OP 0x0c22
#define CHARSET_OP 0x000f
#define CHARSTRINGS_OP 0x0011
#define COPYRIGHT_OP 0x0c00
#define DEFAULTWIDTH_OP 0x0014
#define ENCODING_OP 0x0010
#define FAMILYNAME_OP 0x0003
#define FDARRAY_OP 0x0c24
#define FDSELECT_OP 0x0c25
#define FONTBBOX_OP 0x0005
#define FONTMATRIX_OP 0x0c07
#define FONTNAME_OP 0x0c26
#define FULLNAME_OP 0x0002
#define LOCAL_SUB_OP 0x0013
#define NOMINALWIDTH_OP 0x0015
#define NOTICE_OP 0x0001
#define POSTSCRIPT_OP 0x0c15
#define PRIVATE_OP 0x0012
#define ROS_OP 0x0c1e
#define UNIQUEID_OP 0x000d
#define VERSION_OP 0x0000
#define WEIGHT_OP 0x0004
#define XUID_OP 0x000e
#define NUM_STD_STRINGS 391
/* Type 2 Charstring operators */
#define TYPE2_hstem 0x0001
#define TYPE2_vstem 0x0003
#define TYPE2_callsubr 0x000a
#define TYPE2_return 0x000b
#define TYPE2_endchar 0x000e
#define TYPE2_hstemhm 0x0012
#define TYPE2_hintmask 0x0013
#define TYPE2_cntrmask 0x0014
#define TYPE2_vstemhm 0x0017
#define TYPE2_callgsubr 0x001d
#define TYPE2_rmoveto 0x0015
#define TYPE2_hmoveto 0x0016
#define TYPE2_vmoveto 0x0004
#define MAX_SUBROUTINE_NESTING 10 /* From Type2 Charstring spec */
typedef struct _cff_header {
uint8_t major;
uint8_t minor;
uint8_t header_size;
uint8_t offset_size;
} cff_header_t;
typedef struct _cff_index_element {
cairo_bool_t is_copy;
unsigned char *data;
int length;
} cff_index_element_t;
typedef struct _cff_dict_operator {
cairo_hash_entry_t base;
unsigned short operator;
unsigned char *operand;
int operand_length;
int operand_offset;
} cff_dict_operator_t;
typedef struct _cairo_cff_font {
cairo_scaled_font_subset_t *scaled_font_subset;
const cairo_scaled_font_backend_t *backend;
/* Font Data */
unsigned char *data;
unsigned long data_length;
unsigned char *current_ptr;
unsigned char *data_end;
cff_header_t *header;
char *font_name;
char *ps_name;
cairo_hash_table_t *top_dict;
cairo_hash_table_t *private_dict;
cairo_array_t strings_index;
cairo_array_t charstrings_index;
cairo_array_t global_sub_index;
cairo_array_t local_sub_index;
unsigned char *charset;
int num_glyphs;
cairo_bool_t is_cid;
cairo_bool_t is_opentype;
int units_per_em;
int global_sub_bias;
int local_sub_bias;
double default_width;
double nominal_width;
/* CID Font Data */
int *fdselect;
unsigned int num_fontdicts;
cairo_hash_table_t **fd_dict;
cairo_hash_table_t **fd_private_dict;
cairo_array_t *fd_local_sub_index;
int *fd_local_sub_bias;
double *fd_default_width;
double *fd_nominal_width;
/* Subsetted Font Data */
char *subset_font_name;
cairo_array_t charstrings_subset_index;
cairo_array_t strings_subset_index;
int euro_sid;
int *fdselect_subset;
unsigned int num_subset_fontdicts;
int *fd_subset_map;
int *private_dict_offset;
cairo_bool_t subset_subroutines;
cairo_bool_t *global_subs_used;
cairo_bool_t *local_subs_used;
cairo_bool_t **fd_local_subs_used;
cairo_array_t output;
/* Subset Metrics */
int *widths;
int x_min, y_min, x_max, y_max;
int ascent, descent;
/* Type 2 charstring data */
int type2_stack_size;
int type2_stack_top_value;
cairo_bool_t type2_stack_top_is_int;
int type2_num_hints;
int type2_hintmask_bytes;
int type2_nesting_level;
cairo_bool_t type2_seen_first_int;
cairo_bool_t type2_find_width;
cairo_bool_t type2_found_width;
int type2_width;
cairo_bool_t type2_has_path;
} cairo_cff_font_t;
/* Encoded integer using maximum sized encoding. This is required for
* operands that are later modified after encoding. */
static unsigned char *
encode_integer_max (unsigned char *p, int i)
{
*p++ = 29;
*p++ = i >> 24;
*p++ = (i >> 16) & 0xff;
*p++ = (i >> 8) & 0xff;
*p++ = i & 0xff;
return p;
}
static unsigned char *
encode_integer (unsigned char *p, int i)
{
if (i >= -107 && i <= 107) {
*p++ = i + 139;
} else if (i >= 108 && i <= 1131) {
i -= 108;
*p++ = (i >> 8)+ 247;
*p++ = i & 0xff;
} else if (i >= -1131 && i <= -108) {
i = -i - 108;
*p++ = (i >> 8)+ 251;
*p++ = i & 0xff;
} else if (i >= -32768 && i <= 32767) {
*p++ = 28;
*p++ = (i >> 8) & 0xff;
*p++ = i & 0xff;
} else {
p = encode_integer_max (p, i);
}
return p;
}
static unsigned char *
decode_integer (unsigned char *p, int *integer)
{
if (*p == 28) {
*integer = (int)(p[1]<<8 | p[2]);
p += 3;
} else if (*p == 29) {
*integer = (int)((p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4]);
p += 5;
} else if (*p >= 32 && *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 = 0;
p += 1;
}
return p;
}
static char *
decode_nibble (int n, char *buf)
{
switch (n)
{
case 0xa:
*buf++ = '.';
break;
case 0xb:
*buf++ = 'E';
break;
case 0xc:
*buf++ = 'E';
*buf++ = '-';
break;
case 0xd:
*buf++ = '-';
break;
case 0xe:
*buf++ = '-';
break;
case 0xf:
break;
default:
*buf++ = '0' + n;
break;
}
return buf;
}
static unsigned char *
decode_real (unsigned char *p, double *real)
{
struct lconv *locale_data;
const char *decimal_point;
int decimal_point_len;
int n;
char buffer[100];
char buffer2[200];
char *q;
char *buf = buffer;
char *buf_end = buffer + sizeof (buffer);
locale_data = localeconv ();
decimal_point = locale_data->decimal_point;
decimal_point_len = strlen (decimal_point);
assert (decimal_point_len != 0);
assert (sizeof(buffer) + decimal_point_len < sizeof(buffer2));
p++;
while (buf + 2 < buf_end) {
n = *p >> 4;
buf = decode_nibble (n, buf);
n = *p & 0x0f;
buf = decode_nibble (n, buf);
if ((*p & 0x0f) == 0x0f) {
p++;
break;
}
p++;
};
*buf = 0;
buf = buffer;
if (strchr (buffer, '.')) {
q = strchr (buffer, '.');
strncpy (buffer2, buffer, q - buffer);
buf = buffer2 + (q - buffer);
strncpy (buf, decimal_point, decimal_point_len);
buf += decimal_point_len;
strcpy (buf, q + 1);
buf = buffer2;
}
if (sscanf(buf, "%lf", real) != 1)
*real = 0.0;
return p;
}
static unsigned char *
decode_number (unsigned char *p, double *number)
{
if (*p == 30) {
p = decode_real (p, number);
} else {
int i;
p = decode_integer (p, &i);
*number = i;
}
return p;
}
static unsigned char *
decode_operator (unsigned char *p, unsigned short *operator)
{
unsigned short op = 0;
op = *p++;
if (op == 12) {
op <<= 8;
op |= *p++;
}
*operator = op;
return p;
}
/* return 0 if not an operand */
static int
operand_length (unsigned char *p)
{
unsigned char *begin = p;
if (*p == 28)
return 3;
if (*p == 29)
return 5;
if (*p >= 32 && *p <= 246)
return 1;
if (*p >= 247 && *p <= 254)
return 2;
if (*p == 30) {
while ((*p & 0x0f) != 0x0f)
p++;
return p - begin + 1;
}
return 0;
}
static unsigned char *
encode_index_offset (unsigned char *p, int offset_size, unsigned long offset)
{
while (--offset_size >= 0) {
p[offset_size] = (unsigned char) (offset & 0xff);
offset >>= 8;
}
return p + offset_size;
}
static unsigned long
decode_index_offset(unsigned char *p, int off_size)
{
unsigned long offset = 0;
while (off_size-- > 0)
offset = offset*256 + *p++;
return offset;
}
static void
cff_index_init (cairo_array_t *index)
{
_cairo_array_init (index, sizeof (cff_index_element_t));
}
static cairo_int_status_t
cff_index_read (cairo_array_t *index, unsigned char **ptr, unsigned char *end_ptr)
{
cff_index_element_t element;
unsigned char *data, *p;
cairo_status_t status;
int offset_size, count, start, i;
int end = 0;
p = *ptr;
if (p + 2 > end_ptr)
return CAIRO_INT_STATUS_UNSUPPORTED;
count = be16_to_cpu( *((uint16_t *)p) );
p += 2;
if (count > 0) {
offset_size = *p++;
if (p + (count + 1)*offset_size > end_ptr)
return CAIRO_INT_STATUS_UNSUPPORTED;
data = p + offset_size*(count + 1) - 1;
start = decode_index_offset (p, offset_size);
p += offset_size;
for (i = 0; i < count; i++) {
end = decode_index_offset (p, offset_size);
p += offset_size;
if (p > end_ptr)
return CAIRO_INT_STATUS_UNSUPPORTED;
element.length = end - start;
element.is_copy = FALSE;
element.data = data + start;
status = _cairo_array_append (index, &element);
if (unlikely (status))
return status;
start = end;
}
p = data + end;
}
*ptr = p;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cff_index_write (cairo_array_t *index, cairo_array_t *output)
{
int offset_size;
int offset;
int num_elem;
int i;
cff_index_element_t *element;
uint16_t count;
unsigned char buf[5];
cairo_status_t status;
num_elem = _cairo_array_num_elements (index);
count = cpu_to_be16 ((uint16_t) num_elem);
status = _cairo_array_append_multiple (output, &count, 2);
if (unlikely (status))
return status;
if (num_elem == 0)
return CAIRO_STATUS_SUCCESS;
/* Find maximum offset to determine offset size */
offset = 1;
for (i = 0; i < num_elem; i++) {
element = _cairo_array_index (index, i);
offset += element->length;
}
if (offset < 0x100)
offset_size = 1;
else if (offset < 0x10000)
offset_size = 2;
else if (offset < 0x1000000)
offset_size = 3;
else
offset_size = 4;
buf[0] = (unsigned char) offset_size;
status = _cairo_array_append (output, buf);
if (unlikely (status))
return status;
offset = 1;
encode_index_offset (buf, offset_size, offset);
status = _cairo_array_append_multiple (output, buf, offset_size);
if (unlikely (status))
return status;
for (i = 0; i < num_elem; i++) {
element = _cairo_array_index (index, i);
offset += element->length;
encode_index_offset (buf, offset_size, offset);
status = _cairo_array_append_multiple (output, buf, offset_size);
if (unlikely (status))
return status;
}
for (i = 0; i < num_elem; i++) {
element = _cairo_array_index (index, i);
if (element->length > 0) {
status = _cairo_array_append_multiple (output,
element->data,
element->length);
}
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
}
static void
cff_index_set_object (cairo_array_t *index, int obj_index,
unsigned char *object , int length)
{
cff_index_element_t *element;
element = _cairo_array_index (index, obj_index);
if (element->is_copy)
free (element->data);
element->data = object;
element->length = length;
element->is_copy = FALSE;
}
static cairo_status_t
cff_index_append (cairo_array_t *index, unsigned char *object , int length)
{
cff_index_element_t element;
element.length = length;
element.is_copy = FALSE;
element.data = object;
return _cairo_array_append (index, &element);
}
static cairo_status_t
cff_index_append_copy (cairo_array_t *index,
const unsigned char *object,
unsigned int length)
{
cff_index_element_t element;
cairo_status_t status;
element.length = length;
element.is_copy = TRUE;
element.data = malloc (element.length);
if (unlikely (element.data == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (element.data, object, element.length);
status = _cairo_array_append (index, &element);
if (unlikely (status)) {
free (element.data);
return status;
}
return CAIRO_STATUS_SUCCESS;
}
static void
cff_index_fini (cairo_array_t *index)
{
cff_index_element_t *element;
unsigned int i;
for (i = 0; i < _cairo_array_num_elements (index); i++) {
element = _cairo_array_index (index, i);
if (element->is_copy && element->data)
free (element->data);
}
_cairo_array_fini (index);
}
static cairo_bool_t
_cairo_cff_dict_equal (const void *key_a, const void *key_b)
{
const cff_dict_operator_t *op_a = key_a;
const cff_dict_operator_t *op_b = key_b;
return op_a->operator == op_b->operator;
}
static cairo_status_t
cff_dict_init (cairo_hash_table_t **dict)
{
*dict = _cairo_hash_table_create (_cairo_cff_dict_equal);
if (unlikely (*dict == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_dict_init_key (cff_dict_operator_t *key, int operator)
{
key->base.hash = (unsigned long) operator;
key->operator = operator;
}
static cairo_status_t
cff_dict_create_operator (int operator,
unsigned char *operand,
int size,
cff_dict_operator_t **out)
{
cff_dict_operator_t *op;
op = malloc (sizeof (cff_dict_operator_t));
if (unlikely (op == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_cairo_dict_init_key (op, operator);
op->operand = malloc (size);
if (unlikely (op->operand == NULL)) {
free (op);
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
}
memcpy (op->operand, operand, size);
op->operand_length = size;
op->operand_offset = -1;
*out = op;
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
cff_dict_read (cairo_hash_table_t *dict, unsigned char *p, int dict_size)
{
unsigned char *end;
cairo_array_t operands;
cff_dict_operator_t *op;
unsigned short operator;
cairo_status_t status = CAIRO_STATUS_SUCCESS;
int size;
end = p + dict_size;
_cairo_array_init (&operands, 1);
while (p < end) {
size = operand_length (p);
if (size != 0) {
status = _cairo_array_append_multiple (&operands, p, size);
if (unlikely (status))
goto fail;
p += size;
} else {
p = decode_operator (p, &operator);
status = cff_dict_create_operator (operator,
_cairo_array_index (&operands, 0),
_cairo_array_num_elements (&operands),
&op);
if (unlikely (status))
goto fail;
status = _cairo_hash_table_insert (dict, &op->base);
if (unlikely (status))
goto fail;
_cairo_array_truncate (&operands, 0);
}
}
fail:
_cairo_array_fini (&operands);
return status;
}
static void
cff_dict_remove (cairo_hash_table_t *dict, unsigned short operator)
{
cff_dict_operator_t key, *op;
_cairo_dict_init_key (&key, operator);
op = _cairo_hash_table_lookup (dict, &key.base);
if (op != NULL) {
free (op->operand);
_cairo_hash_table_remove (dict, (cairo_hash_entry_t *) op);
free (op);
}
}
static unsigned char *
cff_dict_get_operands (cairo_hash_table_t *dict,
unsigned short operator,
int *size)
{
cff_dict_operator_t key, *op;
_cairo_dict_init_key (&key, operator);
op = _cairo_hash_table_lookup (dict, &key.base);
if (op != NULL) {
*size = op->operand_length;
return op->operand;
}
return NULL;
}
static cairo_status_t
cff_dict_set_operands (cairo_hash_table_t *dict,
unsigned short operator,
unsigned char *operand,
int size)
{
cff_dict_operator_t key, *op;
cairo_status_t status;
_cairo_dict_init_key (&key, operator);
op = _cairo_hash_table_lookup (dict, &key.base);
if (op != NULL) {
free (op->operand);
op->operand = malloc (size);
if (unlikely (op->operand == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (op->operand, operand, size);
op->operand_length = size;
}
else
{
status = cff_dict_create_operator (operator, operand, size, &op);
if (unlikely (status))
return status;
status = _cairo_hash_table_insert (dict, &op->base);
if (unlikely (status))
return status;
}
return CAIRO_STATUS_SUCCESS;
}
static int
cff_dict_get_location (cairo_hash_table_t *dict,
unsigned short operator,
int *size)
{
cff_dict_operator_t key, *op;
_cairo_dict_init_key (&key, operator);
op = _cairo_hash_table_lookup (dict, &key.base);
if (op != NULL) {
*size = op->operand_length;
return op->operand_offset;
}
return -1;
}
typedef struct _dict_write_info {
cairo_array_t *output;
cairo_status_t status;
} dict_write_info_t;
static void
cairo_dict_write_operator (cff_dict_operator_t *op, dict_write_info_t *write_info)
{
unsigned char data;
op->operand_offset = _cairo_array_num_elements (write_info->output);
write_info->status = _cairo_array_append_multiple (write_info->output, op->operand, op->operand_length);
if (write_info->status)
return;
if (op->operator & 0xff00) {
data = op->operator >> 8;
write_info->status = _cairo_array_append (write_info->output, &data);
if (write_info->status)
return;
}
data = op->operator & 0xff;
write_info->status = _cairo_array_append (write_info->output, &data);
}
static void
_cairo_dict_collect (void *entry, void *closure)
{
dict_write_info_t *write_info = closure;
cff_dict_operator_t *op = entry;
if (write_info->status)
return;
/* The ROS operator is handled separately in cff_dict_write() */
if (op->operator != ROS_OP)
cairo_dict_write_operator (op, write_info);
}
static cairo_status_t
cff_dict_write (cairo_hash_table_t *dict, cairo_array_t *output)
{
dict_write_info_t write_info;
cff_dict_operator_t key, *op;
write_info.output = output;
write_info.status = CAIRO_STATUS_SUCCESS;
/* The CFF specification requires that the Top Dict of CID fonts
* begin with the ROS operator. */
_cairo_dict_init_key (&key, ROS_OP);
op = _cairo_hash_table_lookup (dict, &key.base);
if (op != NULL)
cairo_dict_write_operator (op, &write_info);
_cairo_hash_table_foreach (dict, _cairo_dict_collect, &write_info);
return write_info.status;
}
static void
_cff_dict_entry_pluck (void *_entry, void *dict)
{
cff_dict_operator_t *entry = _entry;
_cairo_hash_table_remove (dict, &entry->base);
free (entry->operand);
free (entry);
}
static void
cff_dict_fini (cairo_hash_table_t *dict)
{
_cairo_hash_table_foreach (dict, _cff_dict_entry_pluck, dict);
_cairo_hash_table_destroy (dict);
}
static cairo_int_status_t
cairo_cff_font_read_header (cairo_cff_font_t *font)
{
if (font->data_length < sizeof (cff_header_t))
return CAIRO_INT_STATUS_UNSUPPORTED;
font->header = (cff_header_t *) font->data;
font->current_ptr = font->data + font->header->header_size;
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
cairo_cff_font_read_name (cairo_cff_font_t *font)
{
cairo_array_t index;
cairo_int_status_t status;
cff_index_element_t *element;
unsigned char *p;
int i, len;
cff_index_init (&index);
status = cff_index_read (&index, &font->current_ptr, font->data_end);
if (!font->is_opentype) {
element = _cairo_array_index (&index, 0);
p = element->data;
len = element->length;
/* If font name is prefixed with a subset tag, strip it off. */
if (len > 7 && p[6] == '+') {
for (i = 0; i < 6; i++)
if (p[i] < 'A' || p[i] > 'Z')
break;
if (i == 6) {
p += 7;
len -= 7;
}
}
font->ps_name = malloc (len + 1);
if (unlikely (font->ps_name == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
memcpy (font->ps_name, p, len);
font->ps_name[len] = 0;
}
cff_index_fini (&index);
return status;
}
static cairo_int_status_t
cairo_cff_font_read_private_dict (cairo_cff_font_t *font,
cairo_hash_table_t *private_dict,
cairo_array_t *local_sub_index,
int *local_sub_bias,
cairo_bool_t **local_subs_used,
double *default_width,
double *nominal_width,
unsigned char *ptr,
int size)
{
cairo_int_status_t status;
unsigned char buf[10];
unsigned char *end_buf;
int offset;
int i;
unsigned char *operand;
unsigned char *p;
int num_subs;
status = cff_dict_read (private_dict, ptr, size);
if (unlikely (status))
return status;
operand = cff_dict_get_operands (private_dict, LOCAL_SUB_OP, &i);
if (operand) {
decode_integer (operand, &offset);
p = ptr + offset;
status = cff_index_read (local_sub_index, &p, font->data_end);
if (unlikely (status))
return status;
/* Use maximum sized encoding to reserve space for later modification. */
end_buf = encode_integer_max (buf, 0);
status = cff_dict_set_operands (private_dict, LOCAL_SUB_OP, buf, end_buf - buf);
if (unlikely (status))
return status;
}
*default_width = 0;
operand = cff_dict_get_operands (private_dict, DEFAULTWIDTH_OP, &i);
if (operand)
decode_number (operand, default_width);
*nominal_width = 0;
operand = cff_dict_get_operands (private_dict, NOMINALWIDTH_OP, &i);
if (operand)
decode_number (operand, nominal_width);
num_subs = _cairo_array_num_elements (local_sub_index);
*local_subs_used = calloc (num_subs, sizeof (cairo_bool_t));
if (unlikely (*local_subs_used == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
if (num_subs < 1240)
*local_sub_bias = 107;
else if (num_subs < 33900)
*local_sub_bias = 1131;
else
*local_sub_bias = 32768;
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
cairo_cff_font_read_fdselect (cairo_cff_font_t *font, unsigned char *p)
{
int type, num_ranges, first, last, fd, i, j;
font->fdselect = calloc (font->num_glyphs, sizeof (int));
if (unlikely (font->fdselect == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
type = *p++;
if (type == 0)
{
for (i = 0; i < font->num_glyphs; i++)
font->fdselect[i] = *p++;
} else if (type == 3) {
num_ranges = be16_to_cpu( *((uint16_t *)p) );
p += 2;
for (i = 0; i < num_ranges; i++)
{
first = be16_to_cpu( *((uint16_t *)p) );
p += 2;
fd = *p++;
last = be16_to_cpu( *((uint16_t *)p) );
for (j = first; j < last; j++)
font->fdselect[j] = fd;
}
} else {
return CAIRO_INT_STATUS_UNSUPPORTED;
}