summaryrefslogtreecommitdiff
path: root/xps/xpscff.c
blob: b61ed4382b26f406531cc08169567b0d950e4498 (plain)
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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* XPS interpreter - cff font support */

#include "ghostxps.h"

#define CFF_ARGS_SIZE 48

/*
 * Big-endian memory accessor functions
 */

static inline int s16(byte *p)
{
    return (signed short)( (p[0] << 8) | p[1] );
}

static inline int u16(byte *p)
{
    return (p[0] << 8) | p[1];
}

static inline int u24(byte *p)
{
    return (p[0] << 16) | (p[1] << 8) | p[2];
}

static inline int u32(byte *p)
{
    return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}

/*
 * OpenType Tables
 *
 * Required: cmap, head, hhea, hmtx, maxp, name, OS/2, post
 * TrueType: cvt, fpgm, glyf, loca, prep
 * Postscript: CFF, VORG
 * Typographic: BASE, GDEF, GPOS, GSUB, JSTF
 * Other: DSIG, gasp, hdmx, kern, LTSH, PCLT, VDMX, vhea, vmtx
 */

static byte * xps_count_cff_index(byte *p, byte *e, int *countp);
static byte * xps_find_cff_index(byte *p, byte *e, int idx, byte **pp, byte **ep);

static int subrbias(int count)
{
    return count < 1240 ? 107 : count < 33900 ? 1131 : 32768;
}

static int uofs(byte *p, int offsize)
{
    if (offsize == 1) return p[0];
    if (offsize == 2) return u16(p);
    if (offsize == 3) return u24(p);
    if (offsize == 4) return u32(p);
    return 0;
}

static byte *
xps_read_cff_real(byte *p, byte *e, float *val)
{
    char buf[64];
    char *txt = buf;

    /* b0 was 30 */

    while (txt < buf + (sizeof buf) - 3 && p < e)
    {
        int b, n;

        b = *p++;

        n = (b >> 4) & 0xf;
        if (n < 0xA) { *txt++ = n + '0'; }
        else if (n == 0xA) { *txt++ = '.'; }
        else if (n == 0xB) { *txt++ = 'E'; }
        else if (n == 0xC) { *txt++ = 'E'; *txt++ = '-'; }
        else if (n == 0xE) { *txt++ = '-'; }
        else if (n == 0xF) { break; }

        n = b & 0xf;
        if (n < 0xA) { *txt++ = n + '0'; }
        else if (n == 0xA) { *txt++ = '.'; }
        else if (n == 0xB) { *txt++ = 'E'; }
        else if (n == 0xC) { *txt++ = 'E'; *txt++ = '-'; }
        else if (n == 0xE) { *txt++ = '-'; }
        else if (n == 0xF) { break; }
    }

    *txt = 0;

    *val = atof(buf);

    return p;
}

static byte *
xps_read_cff_integer(byte *p, byte *e, int b0, int *val)
{
    int b1, b2, b3, b4;

    if (b0 == 28)
    {
        if (p + 2 > e)
        {
            gs_throw(-1, "corrupt dictionary (integer)");
            return 0;
        }
        b1 = *p++;
        b2 = *p++;
        *val = (b1 << 8) | b2;
    }

    else if (b0 == 29)
    {
        if (p + 4 > e)
        {
            gs_throw(-1, "corrupt dictionary (integer)");
            return 0;
        }
        b1 = *p++;
        b2 = *p++;
        b3 = *p++;
        b4 = *p++;
        *val = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
    }

    else if (b0 < 247)
    {
        *val = b0 - 139;
    }

    else if (b0 < 251)
    {
        if (p + 1 > e)
        {
            gs_throw(-1, "corrupt dictionary (integer)");
            return 0;
        }
        b1 = *p++;
        *val = (b0 - 247) * 256 + b1 + 108;
    }

    else
    {
        if (p + 1 > e)
        {
            gs_throw(-1, "corrupt dictionary (integer)");
            return 0;
        }
        b1 = *p++;
        *val = -(b0 - 251) * 256 - b1 - 108;
    }

    return p;
}

static int
xps_read_cff_dict(byte *p, byte *e, xps_font_t *font, gs_font_type1 *pt1)
{
    struct { int ival; float fval; } args[CFF_ARGS_SIZE];
    int offset;
    int b0, n;

    int privatelen = 0;
    int privateofs = 0;

    memset(args, 0x00, sizeof(args));

    offset = p - font->cffdata;

    n = 0;
    while (p < e)
    {
        b0 = *p++;

        if (b0 < 22)
        {
            if (b0 == 12)
            {
                if (p + 1 > e)
                {
                    return gs_throw(-1, "corrupt dictionary (operator)");
                }
                b0 = 0x100 | *p++;
            }

            /* some CFF file offsets */

            if (b0 == 17)
            {
                if (args[0].ival < 0)
                    return gs_throw(-1, "corrupt cff file offset");
                font->charstrings = font->cffdata + args[0].ival;
            }

            if (b0 == 18)
            {
                if (args[0].ival < 0 || args[1].ival < 0)
                    return gs_throw(-1, "corrupt cff file offset");
                privatelen = args[0].ival;
                privateofs = args[1].ival;
                if ((font->cffdata + privateofs + privatelen) > font->cffend)
                    return gs_throw(-1, "corrupt cff file offset");
            }

            if (b0 == 19)
            {
                if (args[0].ival < 0)
                    return gs_throw(-1, "corrupt cff file offset");
                font->subrs = font->cffdata + offset + args[0].ival;
            }

            if (b0 == (256 | 36))
                gs_warn("cid cff fonts not supported yet");
            if (b0 == (256 | 37))
                gs_warn("cid cff fonts not supported yet");

            /* Type1 stuff that need to be set for the pt1 struct */

            if (b0 == (256 | 6))
            {
                if (args[0].ival == 1)
                {
                    pt1->data.interpret = gs_type1_interpret;
                    pt1->data.lenIV = -1; // FIXME
                }
            }

            if (b0 == (256 | 7))
            {
                pt1->FontMatrix.xx = args[0].fval;
                pt1->FontMatrix.xy = args[1].fval;
                pt1->FontMatrix.yx = args[2].fval;
                pt1->FontMatrix.yy = args[3].fval;
                pt1->FontMatrix.tx = args[4].fval;
                pt1->FontMatrix.ty = args[5].fval;
            }

            if (b0 == 5)
            {
                pt1->FontBBox.p.x = args[0].fval;
                pt1->FontBBox.p.y = args[1].fval;
                pt1->FontBBox.q.x = args[2].fval;
                pt1->FontBBox.q.y = args[3].fval;
            }

            if (b0 == 20)
                pt1->data.defaultWidthX = float2fixed(args[0].fval);

            if (b0 == 21)
                pt1->data.nominalWidthX = float2fixed(args[0].fval);

            if (b0 == (256 | 19))
                pt1->data.initialRandomSeed = args[0].ival;

            /* Monday morning blues */
#if 0
            if (b0 == 6)
            {
                pt1->data.BlueValues.count = n / 2;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.BlueValues.values[i] = f;
            }

            if (b0 == 7)
            {
                pt1->data.OtherBlues.count = n / 2;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.OtherBlues.values[i] = f;
            }

            if (b0 == 8)
            {
                pt1->data.FamilyBlues.count = n / 2;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.FamilyBlues.values[i] = f;
            }

            if (b0 == 9)
            {
                pt1->data.FamilyOtherBlues.count = n / 2;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.FamilyOtherBlues.values[i] = f;
            }

            if (b0 == 10)
            {
                pt1->data.StdHW.count = 1;
                pt1->data.StdHW.values[0] = args[0].fval;
            }

            if (b0 == 11)
            {
                pt1->data.StdVW.count = 1;
                pt1->data.StdVW.values[0] = args[0].fval;
            }

            if (b0 == (256 | 9))
                pt1->data.BlueScale = args[0].fval;

            if (b0 == (256 | 10))
                pt1->data.BlueShift = args[0].fval;

            if (b0 == (256 | 11))
                pt1->data.BlueFuzz = args[0].fval;

            if (b0 == (256 | 12))
            {
                pt1->data.StemSnapH.count = n;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.StemSnapH.values[i] = f;
            }

            if (b0 == (256 | 13))
            {
                pt1->data.StemSnapV.count = n;
                for (f = 0, i = 0; i < n; f += args[i].fval, i++)
                    pt1->data.StemSnapV.values[i] = f;
            }

            if (b0 == (256 | 14))
                pt1->data.ForceBold = args[0].ival;

            if (b0 == (256 | 17))
                pt1->data.LanguageGroup = args[0].ival;

            if (b0 == (256 | 18))
                pt1->data.ExpansionFactor = args[0].fval;

#endif

            n = 0;
        }

        else
        {
            if (b0 == 30)
            {
                if (n >= CFF_ARGS_SIZE)
                    return gs_throw(-1, "overflow in cff dict");
                p = xps_read_cff_real(p, e, &args[n].fval);
                if (!p)
                    return gs_throw(-1, "corrupt dictionary operand");
                args[n].ival = (int) args[n].fval;
                n++;
            }
            else if (b0 == 28 || b0 == 29 || (b0 >= 32 && b0 <= 254))
            {
                if (n >= CFF_ARGS_SIZE)
                    return gs_throw(-1, "overflow in cff dict");
                p = xps_read_cff_integer(p, e, b0, &args[n].ival);
                if (!p)
                    return gs_throw(-1, "corrupt dictionary operand");
                args[n].fval = (float) args[n].ival;
                n++;
            }
            else
            {
                return gs_throw1(-1, "corrupt dictionary operand (b0 = %d)", b0);
            }
        }
    }

    /* recurse for the private dictionary */
    if (privatelen)
    {
        int code = xps_read_cff_dict(
                font->cffdata + privateofs,
                font->cffdata + privateofs + privatelen,
                font, pt1);
        if (code < 0)
            return gs_rethrow(code, "cannot read private dictionary");
    }

    return 0;
}

/*
 * Get the number of items in an INDEX, and return
 * a pointer to the end of the INDEX or NULL on
 * failure.
 */
static byte *
xps_count_cff_index(byte *p, byte *e, int *countp)
{
    int count, offsize, last;

    if (p + 3 > e)
    {
        gs_throw(-1, "not enough data for index header");
        return 0;
    }

    count = u16(p); p += 2;
    *countp = count;

    if (count == 0)
        return p;

    offsize = *p++;

    if (offsize < 1 || offsize > 4)
    {
        gs_throw(-1, "corrupt index header");
        return 0;
    }

    if (p + count * offsize > e)
    {
        gs_throw(-1, "not enough data for index offset table");
        return 0;
    }

    p += count * offsize;
    last = uofs(p, offsize);
    if (last < 0 || p + last > e)
    {
        gs_throw(-1, "corrupt index header");
        return 0;
    }

    p += offsize;
    p --; /* stupid offsets */

    if (p + last > e)
    {
        gs_throw(-1, "not enough data for index data");
        return 0;
    }

    p += last;

    return p;
}

/*
 * Locate and store pointers to the data of an
 * item in the index that starts at 'p'.
 * Return pointer to the end of the index,
 * or NULL on failure.
 */
static byte *
xps_find_cff_index(byte *p, byte *e, int idx, byte **pp, byte **ep)
{
    int count, offsize, sofs, eofs, last;

    if (p == NULL)
        return 0;

    if (p + 3 > e)
    {
        gs_throw(-1, "not enough data for index header");
        return 0;
    }

    count = u16(p); p += 2;
    if (count == 0)
        return 0;

    offsize = *p++;

    if (offsize < 1 || offsize > 4)
    {
        gs_throw(-1, "corrupt index header");
        return 0;
    }

    if (p + count * offsize > e)
    {
        gs_throw(-1, "not enough data for index offset table");
        return 0;
    }

    if (idx < 0 || idx >= count)
    {
        gs_throw(-1, "tried to access non-existing index item");
        return 0;
    }

    sofs = uofs(p + idx * offsize, offsize);
    eofs = uofs(p + (idx + 1) * offsize, offsize);
    last = uofs(p + count * offsize, offsize);

    p += count * offsize;
    p += offsize;
    p --; /* stupid offsets */

    if (p + last > e)
    {
        gs_throw(-1, "not enough data for index data");
        return 0;
    }

    if (sofs < 0 || eofs < 0 || sofs > eofs || eofs > last)
    {
        gs_throw(-1, "corrupt index offset table");
        return 0;
    }

    *pp = p + sofs;
    *ep = p + eofs;

    return p + last;
}

/*
 * Scan the CFF file structure and extract important data.
 */

static int
xps_read_cff_file(xps_font_t *font, gs_font_type1 *pt1)
{
    byte *p = font->cffdata;
    byte *e = font->cffend;
    byte *dictp, *dicte;
    int ngsubrs;
    int nsubrs;
    int count;
    int code;

    /* CFF header */
    {
        int major, minor, hdrsize;

        if (p + 4 > e)
            return gs_throw(-1, "not enough data for header");

        major = *p++;
        minor = *p++;
        hdrsize = *p++;
        /*offsize = **/p++;

        if (major != 1 || minor != 0)
            return gs_throw(-1, "not a CFF 1.0 file");

        if (p + hdrsize - 4 > e)
            return gs_throw(-1, "not enough data for extended header");
    }

    /* Name INDEX */
    p = xps_count_cff_index(p, e, &count);
    if (!p)
        return gs_throw(-1, "cannot read name index");
    if (count != 1)
        return gs_throw(-1, "file did not contain exactly one font");

    /* Top Dict INDEX */
    p = xps_find_cff_index(p, e, 0, &dictp, &dicte);
    if (!p)
        return gs_throw(-1, "cannot read top dict index");

    /* String index */
    p = xps_count_cff_index(p, e, &count);
    if (!p)
        return gs_throw(-1, "cannot read string index");

    /* Global Subr INDEX */
    font->gsubrs = p;
    p = xps_count_cff_index(p, e, &ngsubrs);
    if (!p)
        return gs_throw(-1, "cannot read gsubr index");

    /* Read the top and private dictionaries */
    code = xps_read_cff_dict(dictp, dicte, font, pt1);
    if (code < 0)
        return gs_rethrow(code, "cannot read top dictionary");

    /* Check the subrs index */
    nsubrs = 0;
    if (font->subrs)
    {
        p = xps_count_cff_index(font->subrs, e, &nsubrs);
        if (!p)
            return gs_rethrow(-1, "cannot read subrs index");
    }

    /* Check the charstrings index */
    if (font->charstrings)
    {
        p = xps_count_cff_index(font->charstrings, e, &count);
        if (!p)
            return gs_rethrow(-1, "cannot read charstrings index");
    }

    pt1->data.subroutineNumberBias = subrbias(nsubrs);
    pt1->data.gsubrNumberBias = subrbias(ngsubrs);
    // nominal and defaultWidthX

    return 0;
}

/*
 * Ghostscript font machinery callbacks.
 */

static gs_glyph
xps_post_callback_encode_char(gs_font *pfont, gs_char chr, gs_glyph_space_t spc)
{
    xps_font_t *xf = pfont->client_data;
    int value;
    value = xps_encode_font_char(xf, chr);
    if (value == 0)
        return GS_NO_GLYPH;
    return value;
}

static int
xps_post_callback_decode_glyph(gs_font *p42, gs_glyph glyph, int ch, ushort *unicode_return, unsigned int length)
{
    return 0;
}

static int
xps_post_callback_glyph_name(gs_font *pf, gs_glyph glyph, gs_const_string *pstr)
{
    dmprintf1(pf->memory, "asking for CFF glyph name %lu\n", (ulong)glyph);
    return -1;
}

static int
xps_post_callback_glyph_info(gs_font *font, gs_glyph glyph,
        const gs_matrix *pmat, int members, gs_glyph_info_t *info)
{
    dmprintf1(font->memory, "asking for CFF glyph info %lu\n", (ulong)glyph);
    return -1;
}

static int
xps_post_callback_glyph_outline(gs_font *font, int wmode, gs_glyph glyph,
        const gs_matrix *pmat, gx_path *ppath, double sbw[4])
{
    dmprintf1(font->memory, "asking for CFF glyph outline %lu\n", (ulong)glyph);
    return -1;
}

typedef struct gs_type1exec_state_s
{
    gs_type1_state cis;         /* must be first */
    /* i_ctx_t *i_ctx_p; */     /* so push/pop can access o-stack */
    double sbw[4];
    gs_rect char_bbox;
    /*
     * The following elements are only used locally to make the stack clean
     * for OtherSubrs: they don't need to be declared for the garbage
     * collector.
     */
    void * save_args[6];
    int num_args;
    bool AlignToPixels;
} gs_type1exec_state;

static int
xps_post_callback_glyph_data(gs_font_type1 * pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
{
    xps_font_t *font = pfont->client_data;
    byte *s, *e;
    byte *p;

    p = xps_find_cff_index(font->charstrings, font->cffend, glyph, &s, &e);
    if (!p)
        return gs_rethrow(gs_error_rangecheck, "cannot find charstring");

    gs_glyph_data_from_string(pgd, s, e - s, NULL);

    return 0;
}

static int
xps_post_callback_subr_data(gs_font_type1 * pfont,
        int subr_num, bool global, gs_glyph_data_t *pgd)
{
    xps_font_t *font = pfont->client_data;
    byte *s, *e;
    byte *p;

    if (global)
    {
        p = xps_find_cff_index(font->gsubrs, font->cffend, subr_num, &s, &e);
        if (!p)
            return gs_rethrow(gs_error_rangecheck, "cannot find gsubr");
    }
    else
    {
        p = xps_find_cff_index(font->subrs, font->cffend, subr_num, &s, &e);
        if (!p)
            return gs_rethrow(gs_error_rangecheck, "cannot find subr");
    }

    gs_glyph_data_from_string(pgd, s, e - s, NULL);

    return 0;
}

static int
xps_post_callback_seac_data(gs_font_type1 * pfont, int ccode, gs_glyph * pglyph,
        gs_const_string *gstr, gs_glyph_data_t *pgd)
{
    return gs_throw(-1, "seac is deprecated in CFF fonts");
}

static int
xps_post_callback_push(void *callback_data, const fixed *values, int count)
{
    return gs_throw(-1, "push not implemented");
}

static int
xps_post_callback_pop(void *callback_data, fixed *value)
{
    return gs_throw(-1, "pop not implemented");
}

static int
xps_cff_append(gs_gstate *pgs, gs_font_type1 *pt1, gs_glyph glyph, int donthint)
{
    int code, value;
    gs_type1exec_state cxs;
    gs_glyph_data_t gd;
    gs_type1_state *const pcis = &cxs.cis;
    gs_glyph_data_t *pgd = &gd;
    double sbw[4];
    gs_matrix mtx;

    /* get charstring data */
    code = xps_post_callback_glyph_data(pt1, glyph, pgd);
    if (code < 0)
        return gs_rethrow(code, "cannot get glyph data");

    mtx = ctm_only(pgs);
    gs_matrix_scale(&mtx, 0.001, 0.001, &mtx);
    mtx.tx = 0;
    mtx.ty = 0;
    gs_matrix_fixed_from_matrix(&pgs->ctm, &mtx);
    pgs->flatness = 0;

    code = gs_type1_interp_init(&cxs.cis, pgs, pgs->path, NULL, NULL, donthint, 0, pt1);
    if (code < 0)
        return gs_throw(code, "cannot init type1 interpreter");

    gs_type1_set_callback_data(pcis, &cxs);

    // TODO: check if this is set in the font dict
    // gs_type1_set_lsb(pcis, &mpt);
    // gs_type1_set_width(pcis, &mpt);

    // ...

    while (1)
    {
        code = pt1->data.interpret(pcis, pgd, &value);
        switch (code)
        {
        case type1_result_callothersubr: /* unknown OtherSubr */
            return_error(-15); /* can't handle it */
        case type1_result_sbw: /* [h]sbw, just continue */
            type1_cis_get_metrics(pcis, cxs.sbw);
            type1_cis_get_metrics(pcis, sbw);
            pgd = 0;
            break;
        case 0: /* all done */
            return 0;
        default: /* code < 0, error */
            return gs_throw(code, "cannot interpret type1 data");
        }
    }
}

static int
xps_post_callback_build_char(gs_show_enum *penum, gs_gstate *pgs,
        gs_font *pfont, gs_char chr, gs_glyph glyph)
{
    gs_font_type1 *pt1 = (gs_font_type1*)pfont;
    const gs_rect *pbbox;
    float w2[6];
    int code;

    // get the metrics
    w2[0] = 0;
    w2[1] = 1;

    pbbox = &pt1->FontBBox;
    w2[2] = pbbox->p.x * 0.001;
    w2[3] = pbbox->p.y * 0.001;
    w2[4] = pbbox->q.x * 0.001;
    w2[5] = pbbox->q.y * 0.001;

    /* Expand the bbox when stroking */
    if ( pfont->PaintType )
    {
        float expand = max(1.415, gs_currentmiterlimit(pgs)) * gs_currentlinewidth(pgs) / 2;
        w2[2] -= expand, w2[3] -= expand;
        w2[4] += expand, w2[5] += expand;
    }

    if ( (code = gs_moveto(pgs, 0.0, 0.0)) < 0 )
        return code;

    if ( (code = gs_setcachedevice(penum, pgs, w2)) < 0 )
        return code;

    code = xps_cff_append(pgs, pt1, glyph,
            gs_show_in_charpath(penum) != cpm_show);
    if (code < 0)
        return code;

    code = (pfont->PaintType ? gs_stroke(pgs) : gs_fill(pgs));
    if (code < 0)
        return code;

    return 0;
}

int
xps_init_postscript_font(xps_context_t *ctx, xps_font_t *font)
{
    gs_font_type1 *pt1;
    int cffofs;
    int cfflen;
    int cffend;
    int code;

    /* Find the CFF table and parse it to create a charstring based font */
    /* We don't need to support CFF files with multiple fonts */
    /* Find the VORG table for easier vertical metrics */

    cffofs = xps_find_sfnt_table(font, "CFF ", &cfflen);
    if (cffofs < 0)
        return gs_throw(-1, "cannot find CFF table");

    /* check the table is within the buffer and no integer overflow occurs */
    cffend = cffofs + cfflen;
    if (cffend < cffofs || cfflen < 0 || cffend > font->length)
        return gs_throw(-1, "corrupt CFF table location");

    font->cffdata = font->data + cffofs;
    font->cffend = font->data + cffend;

    font->gsubrs = 0;
    font->subrs = 0;
    font->charstrings = 0;

    pt1 = (void*) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "xps_font type1");
    if (!pt1)
        return gs_throw(-1, "out of memory");

    font->font = (void*) pt1;

    /* Common to all fonts */

    pt1->next = 0;
    pt1->prev = 0;
    pt1->memory = ctx->memory;

    pt1->dir = ctx->fontdir; /* NB also set by gs_definefont later */
    pt1->base = font->font; /* NB also set by gs_definefont later */
    pt1->is_resource = false;
    gs_notify_init(&pt1->notify_list, gs_memory_stable(ctx->memory));
    pt1->id = gs_next_ids(ctx->memory, 1);

    pt1->client_data = font; /* that's us */

    gs_make_identity(&pt1->FontMatrix);
    gs_make_identity(&pt1->orig_FontMatrix);

    pt1->FontType = ft_encrypted2;
    pt1->BitmapWidths = true;
    pt1->ExactSize = fbit_use_outlines;
    pt1->InBetweenSize = fbit_use_outlines;
    pt1->TransformedChar = fbit_use_outlines;
    pt1->WMode = 0;
    pt1->PaintType = 0;
    pt1->StrokeWidth = 0;

    pt1->procs.define_font = gs_no_define_font;
    pt1->procs.make_font = gs_no_make_font;
    pt1->procs.font_info = gs_default_font_info;
    pt1->procs.same_font = gs_default_same_font;
    pt1->procs.encode_char = xps_post_callback_encode_char;
    pt1->procs.decode_glyph = xps_post_callback_decode_glyph;
    pt1->procs.enumerate_glyph = gs_no_enumerate_glyph;
    pt1->procs.glyph_info = xps_post_callback_glyph_info;
    pt1->procs.glyph_outline = xps_post_callback_glyph_outline;
    pt1->procs.glyph_name = xps_post_callback_glyph_name;
    pt1->procs.init_fstack = gs_default_init_fstack;
    pt1->procs.next_char_glyph = gs_default_next_char_glyph;
    pt1->procs.build_char = xps_post_callback_build_char;

    strcpy((char*)pt1->font_name.chars, "PostScriptFont");
    pt1->font_name.size = strlen((char*)pt1->font_name.chars);

    pt1->key_name.size = 0;

    /* Base font specific */

    pt1->FontBBox.p.x = 0; // -0.5;
    pt1->FontBBox.p.y = 0; // -0.5;
    pt1->FontBBox.q.x = 0; // 1.5;
    pt1->FontBBox.q.y = 0; // 1.5;

    uid_set_UniqueID(&pt1->UID, pt1->id);

    pt1->encoding_index = ENCODING_INDEX_UNKNOWN;
    pt1->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;

    pt1->FAPI = 0;
    pt1->FAPI_font_data = 0;

    /* Type 1/2 specific */
    /* defaults from the CFF spec */

    pt1->data.procs.glyph_data = xps_post_callback_glyph_data;
    pt1->data.procs.subr_data = xps_post_callback_subr_data;
    pt1->data.procs.seac_data = xps_post_callback_seac_data;
    pt1->data.procs.push_values = xps_post_callback_push;
    pt1->data.procs.pop_value = xps_post_callback_pop;

    pt1->data.interpret = gs_type2_interpret;
    pt1->data.proc_data = font;
    pt1->data.parent = NULL;
    pt1->data.lenIV = -1; /* DEFAULT_LENIV_2 */

    pt1->data.subroutineNumberBias = 0;
    pt1->data.gsubrNumberBias = 0;
    pt1->data.initialRandomSeed = 0;
    pt1->data.defaultWidthX = 0;
    pt1->data.nominalWidthX = 0;

    pt1->data.BlueFuzz = 1;
    pt1->data.BlueScale = 0.039625f;
    pt1->data.BlueShift = 7;
    pt1->data.BlueValues.count = 0;
    pt1->data.ExpansionFactor = 0.06f;
    pt1->data.ForceBold = 0;
    pt1->data.FamilyBlues.count = 0;
    pt1->data.FamilyOtherBlues.count = 0;
    pt1->data.LanguageGroup = 0;
    pt1->data.OtherBlues.count = 0;

    pt1->data.RndStemUp = 0;
    memset(&pt1->data.StdHW, 0, sizeof(pt1->data.StdHW));
    memset(&pt1->data.StdVW, 0, sizeof(pt1->data.StdVW));
    memset(&pt1->data.StemSnapH, 0, sizeof(pt1->data.StemSnapH));
    memset(&pt1->data.StemSnapV, 0, sizeof(pt1->data.StemSnapH));
    memset(&pt1->data.WeightVector, 0, sizeof(pt1->data.WeightVector));

    code = xps_read_cff_file(font, pt1);
    if (code < 0)
    {
        // TODO free pt1 here?
        return gs_rethrow(code, "cannot read cff file structure");
    }

    if ((code = gs_definefont(ctx->fontdir, font->font)) < 0) {
        return(code);
    }

    code = xps_fapi_passfont (font->font, NULL, NULL, font->data, font->length);
    return code;
}