summaryrefslogtreecommitdiff
path: root/xps/xpspath.c
blob: 68a8cf1958026482c7c9a15baf5ff9dd54d3953a (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
/* 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 - path (vector drawing) support */

#include "ghostxps.h"

#define INITIAL_DASH_SIZE 32
#define ADDITIVE_DASH_SIZE 1024

char *
xps_get_real_params(char *s, int num, float *x)
{
    int k = 0;

    if (s != NULL && *s != 0) {
        while (*s)
        {
            while (*s == 0x0d || *s == '\t' || *s == ' ' || *s == 0x0a)
                s++;
            x[k] = (float)strtod(s, &s);
            while (*s == 0x0d || *s == '\t' || *s == ' ' || *s == 0x0a)
                s++;
            if (*s == ',')
                s++;
            if (++k == num)
                break;
        }
        return s;
    } else
        return NULL;
}

char *
xps_get_point(char *s_in, float *x, float *y)
{
    char *s_out = s_in;
    float xy[2];

    s_out = xps_get_real_params(s_out, 2, xy);
    *x = xy[0];
    *y = xy[1];
    return s_out;
}

void
xps_clip(xps_context_t *ctx)
{
    if (ctx->fill_rule == 0)
        gs_eoclip(ctx->pgs);
    else
        gs_clip(ctx->pgs);
    gs_newpath(ctx->pgs);
}

void
xps_fill(xps_context_t *ctx)
{
    if (gs_getfillconstantalpha(ctx->pgs) < 0.001)
        gs_newpath(ctx->pgs);
    else if (ctx->fill_rule == 0) {
        if (gs_eofill(ctx->pgs) == gs_error_Remap_Color){
            ctx->in_high_level_pattern = true;
            xps_high_level_pattern(ctx);
            ctx->in_high_level_pattern = false;
            gs_eofill(ctx->pgs);
        }
    }
    else {
        if (gs_fill(ctx->pgs) == gs_error_Remap_Color){
            ctx->in_high_level_pattern = true;
            xps_high_level_pattern(ctx);
            ctx->in_high_level_pattern = false;
            gs_fill(ctx->pgs);
        }
    }
}

/* Draw an arc segment transformed by the matrix, we approximate with straight
 * line segments. We cannot use the gs_arc function because they only draw
 * circular arcs, we need to transform the line to make them elliptical but
 * without transforming the line width.
 *
 * We are guaranteed that on entry the point is at the point that would be
 * calculated by th0, and on exit, a point is generated for us at th0.
 */
static inline void
xps_draw_arc_segment(xps_context_t *ctx, gs_matrix *mtx, float th0, float th1, int iscw)
{
    float t, d;
    gs_point p;

    while (th1 < th0)
        th1 += (float)(M_PI * 2.0);

    d = (float)(1 * (M_PI / 180.0)); /* 1-degree precision */

    if (iscw)
    {
        for (t = th0 + d; t < th1 - d/2; t += d)
        {
            gs_point_transform(cos(t), sin(t), mtx, &p);
            gs_lineto(ctx->pgs, p.x, p.y);
        }
    }
    else
    {
        th0 += (float)(M_PI * 2);
        for (t = th0 - d; t > th1 + d/2; t -= d)
        {
            gs_point_transform(cos(t), sin(t), mtx, &p);
            gs_lineto(ctx->pgs, p.x, p.y);
        }
    }
}

/* Given two vectors find the angle between them. */
static inline double
angle_between(const gs_point u, const gs_point v)
{
    double det = u.x * v.y - u.y * v.x;
    double sign = (det < 0 ? -1.0 : 1.0);
    double magu = u.x * u.x + u.y * u.y;
    double magv = v.x * v.x + v.y * v.y;
    double udotv = u.x * v.x + u.y * v.y;
    double t = udotv / (magu * magv);
    /* guard against rounding errors when near |1| (where acos will return NaN) */
    if (t < -1.0) t = -1.0;
    if (t > 1.0) t = 1.0;
    return sign * acos(t);
}

static void
xps_draw_arc(xps_context_t *ctx,
        float size_x, float size_y, float rotation_angle,
        int is_large_arc, int is_clockwise,
        float point_x, float point_y)
{
    gs_matrix rotmat, revmat;
    gs_matrix mtx;
    gs_point pt;
    double rx, ry;
    double x1, y1, x2, y2;
    double x1t, y1t;
    double cxt, cyt, cx, cy;
    double t1, t2, t3;
    double sign;
    double th1, dth;

    gs_currentpoint(ctx->pgs, &pt);
    x1 = pt.x;
    y1 = pt.y;
    x2 = point_x;
    y2 = point_y;
    rx = size_x;
    ry = size_y;

    if (is_clockwise != is_large_arc)
        sign = 1;
    else
        sign = -1;

    gs_make_rotation(rotation_angle, &rotmat);
    gs_make_rotation(-rotation_angle, &revmat);

    /* http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes */
    /* Conversion from endpoint to center parameterization */

    /* F.6.6.1 -- ensure radii are positive and non-zero */
    rx = fabsf(rx);
    ry = fabsf(ry);
    if (rx < 0.001 || ry < 0.001 || (x1 == x2 && y1 == y2))
    {
        gs_lineto(ctx->pgs, x2, y2);
        return;
    }

    /* F.6.5.1 */
    gs_distance_transform((x1 - x2) / 2.0, (y1 - y2) / 2.0, &revmat, &pt);
    x1t = pt.x;
    y1t = pt.y;

    /* F.6.6.2 -- ensure radii are large enough */
    t1 = (x1t * x1t) / (rx * rx) + (y1t * y1t) / (ry * ry);
    if (t1 > 1.0)
    {
        rx = rx * sqrtf(t1);
        ry = ry * sqrtf(t1);
    }

    /* F.6.5.2 */
    t1 = (rx * rx * ry * ry) - (rx * rx * y1t * y1t) - (ry * ry * x1t * x1t);
    t2 = (rx * rx * y1t * y1t) + (ry * ry * x1t * x1t);
    t3 = t1 / t2;
    /* guard against rounding errors; sqrt of negative numbers is bad for your health */
    if (t3 < 0.0) t3 = 0.0;
    t3 = sqrtf(t3);

    cxt = sign * t3 * (rx * y1t) / ry;
    cyt = sign * t3 * -(ry * x1t) / rx;

    /* F.6.5.3 */
    gs_distance_transform(cxt, cyt, &rotmat, &pt);
    cx = pt.x + (x1 + x2) / 2;
    cy = pt.y + (y1 + y2) / 2;

    /* F.6.5.4 */
    {
        gs_point coord1, coord2, coord3, coord4;
        coord1.x = 1;
        coord1.y = 0;
        coord2.x = (x1t - cxt) / rx;
        coord2.y = (y1t - cyt) / ry;
        coord3.x = (x1t - cxt) / rx;
        coord3.y = (y1t - cyt) / ry;
        coord4.x = (-x1t - cxt) / rx;
        coord4.y = (-y1t - cyt) / ry;
        th1 = angle_between(coord1, coord2);
        dth = angle_between(coord3, coord4);
        if (dth < 0 && !is_clockwise)
            dth += (degrees_to_radians * 360);
        if (dth > 0 && is_clockwise)
            dth -= (degrees_to_radians * 360);
    }

    gs_make_identity(&mtx);
    gs_matrix_translate(&mtx, cx, cy, &mtx);
    gs_matrix_rotate(&mtx, rotation_angle, &mtx);
    gs_matrix_scale(&mtx, rx, ry, &mtx);
    xps_draw_arc_segment(ctx, &mtx, th1, th1 + dth, is_clockwise);

    gs_lineto(ctx->pgs, point_x, point_y);
}

/*
 * Parse an abbreviated geometry string, and call
 * ghostscript moveto/lineto/curveto functions to
 * build up a path.
 */

void
xps_parse_abbreviated_geometry(xps_context_t *ctx, char *geom)
{
    char **args;
    char **pargs;
    char *s = geom;
    gs_point pt;
    int i, n;
    int cmd, old;
    float x1, y1, x2, y2, x3, y3;
    float smooth_x, smooth_y; /* saved cubic bezier control point for smooth curves */
    int reset_smooth;

    args = xps_alloc(ctx, sizeof(char*) * (strlen(geom) + 1));
    if (!args) {
        gs_throw(gs_error_VMerror, "out of memory: args.\n");
        return;
    }
    pargs = args;

    //dmprintf1(ctx->memory, "new path (%.70s)\n", geom);
    gs_newpath(ctx->pgs);

    while (*s)
    {
        if ((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))
        {
            *pargs++ = s++;
        }
        else if ((*s >= '0' && *s <= '9') || *s == '.' || *s == '+' || *s == '-' || *s == 'e' || *s == 'E')
        {
            *pargs++ = s;
            while ((*s >= '0' && *s <= '9') || *s == '.' || *s == '+' || *s == '-' || *s == 'e' || *s == 'E')
                s ++;
        }
        else
        {
            s++;
        }
    }

    *pargs = s;

    n = pargs - args;
    i = 0;

    old = 0;

    reset_smooth = 1;
    smooth_x = 0.0;
    smooth_y = 0.0;

    while (i < n)
    {
        cmd = args[i][0];
        if (cmd == '+' || cmd == '.' || cmd == '-' || (cmd >= '0' && cmd <= '9'))
            cmd = old; /* it's a number, repeat old command */
        else
            i ++;

        if (reset_smooth)
        {
            smooth_x = 0.0;
            smooth_y = 0.0;
        }

        reset_smooth = 1;

        switch (cmd)
        {
        case 'F':
            if (i + 1 <= n)
            {
                ctx->fill_rule = atoi(args[i]);
                i++;
            }
            break;

        case 'M':
            if (i + 2 <= n)
            {
                gs_moveto(ctx->pgs, atof(args[i]), atof(args[i+1]));
                //dmprintf2(ctx->memory, "moveto %g %g\n", atof(args[i]), atof(args[i+1]));
                i += 2;
            }
            break;
        case 'm':
            if (i + 2 <= n)
            {
                gs_rmoveto(ctx->pgs, atof(args[i]), atof(args[i+1]));
                //dmprintf2(ctx->memory, "rmoveto %g %g\n", atof(args[i]), atof(args[i+1]));
                i += 2;
            }
            break;

        case 'L':
            if (i + 2 <= n)
            {
                gs_lineto(ctx->pgs, atof(args[i]), atof(args[i+1]));
                //dmprintf2(ctx->memory, "lineto %g %g\n", atof(args[i]), atof(args[i+1]));
                i += 2;
            }
            break;
        case 'l':
            if (i + 2 <= n)
            {
                gs_rlineto(ctx->pgs, atof(args[i]), atof(args[i+1]));
                //dmprintf2(ctx->memory, "rlineto %g %g\n", atof(args[i]), atof(args[i+1]));
                i += 2;
            }
            break;

        case 'H':
            if (i + 1 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                gs_lineto(ctx->pgs, atof(args[i]), pt.y);
                //dmprintf1(ctx->memory, "hlineto %g\n", atof(args[i]));
                i += 1;
            }
            break;
        case 'h':
            if (i + 1 <= n)
            {
                gs_rlineto(ctx->pgs, atof(args[i]), 0.0);
                //dmprintf1(ctx->memory, "rhlineto %g\n", atof(args[i]));
                i += 1;
            }
            break;

        case 'V':
            if (i + 1 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                gs_lineto(ctx->pgs, pt.x, atof(args[i]));
                //dmprintf1(ctx->memory, "vlineto %g\n", atof(args[i]));
                i += 1;
            }
            break;
        case 'v':
            if (i + 1 <= n)
            {
                gs_rlineto(ctx->pgs, 0.0, atof(args[i]));
                //dmprintf1(ctx->memory, "rvlineto %g\n", atof(args[i]));
                i += 1;
            }
            break;

        case 'C':
            if (i + 6 <= n)
            {
                x1 = atof(args[i+0]);
                y1 = atof(args[i+1]);
                x2 = atof(args[i+2]);
                y2 = atof(args[i+3]);
                x3 = atof(args[i+4]);
                y3 = atof(args[i+5]);
                gs_curveto(ctx->pgs, x1, y1, x2, y2, x3, y3);
                i += 6;
                reset_smooth = 0;
                smooth_x = x3 - x2;
                smooth_y = y3 - y2;
            }
            break;

        case 'c':
            if (i + 6 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                x1 = atof(args[i+0]) + pt.x;
                y1 = atof(args[i+1]) + pt.y;
                x2 = atof(args[i+2]) + pt.x;
                y2 = atof(args[i+3]) + pt.y;
                x3 = atof(args[i+4]) + pt.x;
                y3 = atof(args[i+5]) + pt.y;
                gs_curveto(ctx->pgs, x1, y1, x2, y2, x3, y3);
                i += 6;
                reset_smooth = 0;
                smooth_x = x3 - x2;
                smooth_y = y3 - y2;
            }
            break;

        case 'S':
            if (i + 4 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                x1 = atof(args[i+0]);
                y1 = atof(args[i+1]);
                x2 = atof(args[i+2]);
                y2 = atof(args[i+3]);
                //dmprintf2(ctx->memory, "smooth %g %g\n", smooth_x, smooth_y);
                gs_curveto(ctx->pgs, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
                i += 4;
                reset_smooth = 0;
                smooth_x = x2 - x1;
                smooth_y = y2 - y1;
            }
            break;

        case 's':
            if (i + 4 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                x1 = atof(args[i+0]) + pt.x;
                y1 = atof(args[i+1]) + pt.y;
                x2 = atof(args[i+2]) + pt.x;
                y2 = atof(args[i+3]) + pt.y;
                //dmprintf2(ctx->memory, "smooth %g %g\n", smooth_x, smooth_y);
                gs_curveto(ctx->pgs, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
                i += 4;
                reset_smooth = 0;
                smooth_x = x2 - x1;
                smooth_y = y2 - y1;
            }
            break;

        case 'Q':
            if (i + 4 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                x1 = atof(args[i+0]);
                y1 = atof(args[i+1]);
                x2 = atof(args[i+2]);
                y2 = atof(args[i+3]);
                //dmprintf4(ctx->memory, "conicto %g %g %g %g\n", x1, y1, x2, y2);
                gs_curveto(ctx->pgs,
                        (pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3,
                        (x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3,
                        x2, y2);
                i += 4;
            }
            break;
        case 'q':
            if (i + 4 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                x1 = atof(args[i+0]) + pt.x;
                y1 = atof(args[i+1]) + pt.y;
                x2 = atof(args[i+2]) + pt.x;
                y2 = atof(args[i+3]) + pt.y;
                //dmprintf4(ctx->memory, "conicto %g %g %g %g\n", x1, y1, x2, y2);
                gs_curveto(ctx->pgs,
                        (pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3,
                        (x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3,
                        x2, y2);
                i += 4;
            }
            break;

        case 'A':
            if (i + 7 <= n)
            {
                xps_draw_arc(ctx,
                        atof(args[i+0]), atof(args[i+1]), atof(args[i+2]),
                        atoi(args[i+3]), atoi(args[i+4]),
                        atof(args[i+5]), atof(args[i+6]));
                i += 7;
            }
            break;
        case 'a':
            if (i + 7 <= n)
            {
                gs_currentpoint(ctx->pgs, &pt);
                xps_draw_arc(ctx,
                        atof(args[i+0]), atof(args[i+1]), atof(args[i+2]),
                        atoi(args[i+3]), atoi(args[i+4]),
                        atof(args[i+5]) + pt.x, atof(args[i+6]) + pt.y);
                i += 7;
            }
            break;

        case 'Z':
        case 'z':
            gs_closepath(ctx->pgs);
            //dmputs(ctx->memory, "closepath\n");
            break;

        default:
            /* eek */
            if (old == cmd) /* avoid infinite loop */
                i++;
            break;
        }

        old = cmd;
    }

    xps_free(ctx, args);
}

static void
xps_parse_arc_segment(xps_context_t *ctx, xps_item_t *root, int stroking, int *skipped_stroke)
{
    /* ArcSegment pretty much follows the SVG algorithm for converting an
     * arc in endpoint representation to an arc in centerpoint
     * representation. Once in centerpoint it can be given to the
     * graphics library in the form of a postscript arc. */

    float rotation_angle;
    int is_large_arc, is_clockwise;
    float point_x, point_y;
    float size_x, size_y;
    int is_stroked;

    char *point_att = xps_att(root, "Point");
    char *size_att = xps_att(root, "Size");
    char *rotation_angle_att = xps_att(root, "RotationAngle");
    char *is_large_arc_att = xps_att(root, "IsLargeArc");
    char *sweep_direction_att = xps_att(root, "SweepDirection");
    char *is_stroked_att = xps_att(root, "IsStroked");

    if (!point_att || !size_att || !rotation_angle_att || !is_large_arc_att || !sweep_direction_att)
    {
        gs_warn("ArcSegment element is missing attributes");
        return;
    }

    is_stroked = 1;
    if (is_stroked_att && !strcmp(is_stroked_att, "false"))
            is_stroked = 0;
    if (!is_stroked)
        *skipped_stroke = 1;

    xps_get_point(point_att, &point_x, &point_y);
    xps_get_point(size_att, &size_x, &size_y);
    rotation_angle = atof(rotation_angle_att);
    is_large_arc = !strcmp(is_large_arc_att, "true");
    is_clockwise = !strcmp(sweep_direction_att, "Clockwise");

    if (stroking && !is_stroked)
    {
        gs_moveto(ctx->pgs, point_x, point_y);
        return;
    }

    xps_draw_arc(ctx, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y);
}

static void
xps_parse_poly_quadratic_bezier_segment(xps_context_t *ctx, xps_item_t *root, int stroking, int *skipped_stroke)
{
    char *points_att = xps_att(root, "Points");
    char *is_stroked_att = xps_att(root, "IsStroked");
    float x[2], y[2];
    int is_stroked;
    gs_point pt;
    char *s;
    int n;

    if (!points_att)
    {
        gs_warn("PolyQuadraticBezierSegment element has no points");
        return;
    }

    is_stroked = 1;
    if (is_stroked_att && !strcmp(is_stroked_att, "false"))
            is_stroked = 0;
    if (!is_stroked)
        *skipped_stroke = 1;

    s = points_att;
    n = 0;
    while (*s != 0)
    {
        while (*s == ' ') s++;
        s = xps_get_point(s, &x[n], &y[n]);
        n ++;
        if (n == 2)
        {
            if (stroking && !is_stroked)
            {
                gs_moveto(ctx->pgs, x[1], y[1]);
            }
            else
            {
                gs_currentpoint(ctx->pgs, &pt);
                gs_curveto(ctx->pgs,
                        (pt.x + 2 * x[0]) / 3, (pt.y + 2 * y[0]) / 3,
                        (x[1] + 2 * x[0]) / 3, (y[1] + 2 * y[0]) / 3,
                        x[1], y[1]);
            }
            n = 0;
        }
    }
}

static void
xps_parse_poly_bezier_segment(xps_context_t *ctx, xps_item_t *root, int stroking, int *skipped_stroke)
{
    char *points_att = xps_att(root, "Points");
    char *is_stroked_att = xps_att(root, "IsStroked");
    float x[3], y[3];
    int is_stroked;
    char *s;
    int n;

    if (!points_att)
    {
        gs_warn("PolyBezierSegment element has no points");
        return;
    }

    is_stroked = 1;
    if (is_stroked_att && !strcmp(is_stroked_att, "false"))
            is_stroked = 0;
    if (!is_stroked)
        *skipped_stroke = 1;

    s = points_att;
    n = 0;
    while (*s != 0)
    {
        while (*s == ' ') s++;
        s = xps_get_point(s, &x[n], &y[n]);
        n ++;
        if (n == 3)
        {
            if (stroking && !is_stroked)
                gs_moveto(ctx->pgs, x[2], y[2]);
            else
                gs_curveto(ctx->pgs, x[0], y[0], x[1], y[1], x[2], y[2]);
            n = 0;
        }
    }
}

static void
xps_parse_poly_line_segment(xps_context_t *ctx, xps_item_t *root, int stroking, int *skipped_stroke)
{
    char *points_att = xps_att(root, "Points");
    char *is_stroked_att = xps_att(root, "IsStroked");
    int is_stroked;
    float xy[2];
    char *s;

    if (!points_att)
    {
        gs_warn("PolyLineSegment element has no points");
        return;
    }

    is_stroked = 1;
    if (is_stroked_att && !strcmp(is_stroked_att, "false"))
            is_stroked = 0;
    if (!is_stroked)
        *skipped_stroke = 1;

    s = points_att;
    while (*s != 0)
    {
        s = xps_get_real_params(s, 2, &xy[0]);
        if (stroking && !is_stroked)
            gs_moveto(ctx->pgs, xy[0], xy[1]);
        else
            gs_lineto(ctx->pgs, xy[0], xy[1]);
    }
}

static void
xps_parse_path_figure(xps_context_t *ctx, xps_item_t *root, int stroking)
{
    xps_item_t *node;

    char *is_closed_att;
    char *start_point_att;
    char *is_filled_att;

    int is_closed = 0;
    int is_filled = 1;
    float start_x = 0.0;
    float start_y = 0.0;

    int skipped_stroke = 0;

    is_closed_att = xps_att(root, "IsClosed");
    start_point_att = xps_att(root, "StartPoint");
    is_filled_att = xps_att(root, "IsFilled");

    if (is_closed_att)
        is_closed = !strcmp(is_closed_att, "true");
    if (is_filled_att)
        is_filled = !strcmp(is_filled_att, "true");
    if (start_point_att)
        xps_get_point(start_point_att, &start_x, &start_y);

    if (!stroking && !is_filled) /* not filled, when filling */
        return;

    gs_moveto(ctx->pgs, start_x, start_y);

    for (node = xps_down(root); node; node = xps_next(node))
    {
        if (!strcmp(xps_tag(node), "ArcSegment"))
            xps_parse_arc_segment(ctx, node, stroking, &skipped_stroke);
        if (!strcmp(xps_tag(node), "PolyBezierSegment"))
            xps_parse_poly_bezier_segment(ctx, node, stroking, &skipped_stroke);
        if (!strcmp(xps_tag(node), "PolyLineSegment"))
            xps_parse_poly_line_segment(ctx, node, stroking, &skipped_stroke);
        if (!strcmp(xps_tag(node), "PolyQuadraticBezierSegment"))
            xps_parse_poly_quadratic_bezier_segment(ctx, node, stroking, &skipped_stroke);
    }

    if (is_closed)
    {
        if (stroking && skipped_stroke)
            gs_lineto(ctx->pgs, start_x, start_y); /* we've skipped using gs_moveto... */
        else
            gs_closepath(ctx->pgs); /* no skipped segments, safe to closepath properly */
    }
}

void
xps_parse_path_geometry(xps_context_t *ctx, xps_resource_t *dict, xps_item_t *root, int stroking)
{
    xps_item_t *node;

    char *figures_att;
    char *fill_rule_att;
    char *transform_att;

    xps_item_t *transform_tag = NULL;
    xps_item_t *figures_tag = NULL; /* only used by resource */

    gs_matrix transform;
    gs_matrix saved_transform;

    gs_newpath(ctx->pgs);

    figures_att = xps_att(root, "Figures");
    fill_rule_att = xps_att(root, "FillRule");
    transform_att = xps_att(root, "Transform");

    for (node = xps_down(root); node; node = xps_next(node))
    {
        if (!strcmp(xps_tag(node), "PathGeometry.Transform"))
            transform_tag = xps_down(node);
    }

    xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL);
    xps_resolve_resource_reference(ctx, dict, &figures_att, &figures_tag, NULL);

    if (fill_rule_att)
    {
        if (!strcmp(fill_rule_att, "NonZero"))
            ctx->fill_rule = 1;
        if (!strcmp(fill_rule_att, "EvenOdd"))
            ctx->fill_rule = 0;
    }

    gs_make_identity(&transform);
    if (transform_att || transform_tag)
    {
        if (transform_att)
            xps_parse_render_transform(ctx, transform_att, &transform);
        if (transform_tag)
            xps_parse_matrix_transform(ctx, transform_tag, &transform);
    }

    gs_currentmatrix(ctx->pgs, &saved_transform);
    gs_concat(ctx->pgs, &transform);

    if (figures_att)
    {
        xps_parse_abbreviated_geometry(ctx, figures_att);
    }

    if (figures_tag)
    {
        xps_parse_path_figure(ctx, figures_tag, stroking);
    }

    for (node = xps_down(root); node; node = xps_next(node))
    {
        if (!strcmp(xps_tag(node), "PathFigure"))
            xps_parse_path_figure(ctx, node, stroking);
    }

    gs_setmatrix(ctx->pgs, &saved_transform);
}

static int
xps_parse_line_cap(char *attr)
{
    if (attr)
    {
        if (!strcmp(attr, "Flat")) return gs_cap_butt;
        if (!strcmp(attr, "Square")) return gs_cap_square;
        if (!strcmp(attr, "Round")) return gs_cap_round;
        if (!strcmp(attr, "Triangle")) return gs_cap_triangle;
    }
    return gs_cap_butt;
}

/*
 * Parse an XPS <Path> element, and call relevant ghostscript
 * functions for drawing and/or clipping the child elements.
 */

int
xps_parse_path(xps_context_t *ctx, char *base_uri, xps_resource_t *dict, xps_item_t *root)
{
    xps_item_t *node;
    int code;

    char *fill_uri;
    char *stroke_uri;
    char *opacity_mask_uri;

    char *transform_att;
    char *clip_att;
    char *data_att;
    char *fill_att;
    char *stroke_att;
    char *opacity_att;
    char *opacity_mask_att;

    xps_item_t *transform_tag = NULL;
    xps_item_t *clip_tag = NULL;
    xps_item_t *data_tag = NULL;
    xps_item_t *fill_tag = NULL;
    xps_item_t *stroke_tag = NULL;
    xps_item_t *opacity_mask_tag = NULL;

    char *fill_opacity_att = NULL;
    char *stroke_opacity_att = NULL;

    char *stroke_dash_array_att;
    char *stroke_dash_cap_att;
    char *stroke_dash_offset_att;
    char *stroke_end_line_cap_att;
    char *stroke_start_line_cap_att;
    char *stroke_line_join_att;
    char *stroke_miter_limit_att;
    char *stroke_thickness_att;

    gs_line_join linejoin;
    float linewidth;
    float miterlimit;
    float samples[XPS_MAX_COLORS];

    bool opacity_pushed = false;
    bool uses_stroke = false;

    gs_gsave(ctx->pgs);

    ctx->fill_rule = 0;

    /*
     * Extract attributes and extended attributes.
     */

    transform_att = xps_att(root, "RenderTransform");
    clip_att = xps_att(root, "Clip");
    data_att = xps_att(root, "Data");
    fill_att = xps_att(root, "Fill");
    stroke_att = xps_att(root, "Stroke");
    opacity_att = xps_att(root, "Opacity");
    opacity_mask_att = xps_att(root, "OpacityMask");

    stroke_dash_array_att = xps_att(root, "StrokeDashArray");
    stroke_dash_cap_att = xps_att(root, "StrokeDashCap");
    stroke_dash_offset_att = xps_att(root, "StrokeDashOffset");
    stroke_end_line_cap_att = xps_att(root, "StrokeEndLineCap");
    stroke_start_line_cap_att = xps_att(root, "StrokeStartLineCap");
    stroke_line_join_att = xps_att(root, "StrokeLineJoin");
    stroke_miter_limit_att = xps_att(root, "StrokeMiterLimit");
    stroke_thickness_att = xps_att(root, "StrokeThickness");

    for (node = xps_down(root); node; node = xps_next(node))
    {
        if (!strcmp(xps_tag(node), "Path.RenderTransform"))
            transform_tag = xps_down(node);

        if (!strcmp(xps_tag(node), "Path.OpacityMask"))
            opacity_mask_tag = xps_down(node);

        if (!strcmp(xps_tag(node), "Path.Clip"))
            clip_tag = xps_down(node);

        if (!strcmp(xps_tag(node), "Path.Fill"))
            fill_tag = xps_down(node);

        if (!strcmp(xps_tag(node), "Path.Stroke"))
            stroke_tag = xps_down(node);

        if (!strcmp(xps_tag(node), "Path.Data"))
            data_tag = xps_down(node);
    }

    fill_uri = base_uri;
    stroke_uri = base_uri;
    opacity_mask_uri = base_uri;

    xps_resolve_resource_reference(ctx, dict, &data_att, &data_tag, NULL);
    xps_resolve_resource_reference(ctx, dict, &clip_att, &clip_tag, NULL);
    xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL);
    xps_resolve_resource_reference(ctx, dict, &fill_att, &fill_tag, &fill_uri);
    xps_resolve_resource_reference(ctx, dict, &stroke_att, &stroke_tag, &stroke_uri);
    xps_resolve_resource_reference(ctx, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri);

    /*
     * Act on the information we have gathered:
     */

    if (fill_tag && !strcmp(xps_tag(fill_tag), "SolidColorBrush"))
    {
        fill_opacity_att = xps_att(fill_tag, "Opacity");
        fill_att = xps_att(fill_tag, "Color");
        fill_tag = NULL;
    }

    if (stroke_tag && !strcmp(xps_tag(stroke_tag), "SolidColorBrush"))
    {
        stroke_opacity_att = xps_att(stroke_tag, "Opacity");
        stroke_att = xps_att(stroke_tag, "Color");
        stroke_tag = NULL;
    }

    gs_setlinestartcap(ctx->pgs, xps_parse_line_cap(stroke_start_line_cap_att));
    gs_setlineendcap(ctx->pgs, xps_parse_line_cap(stroke_end_line_cap_att));
    gs_setlinedashcap(ctx->pgs, xps_parse_line_cap(stroke_dash_cap_att));

    linejoin = gs_join_miter;
    if (stroke_line_join_att)
    {
        if (!strcmp(stroke_line_join_att, "Miter")) linejoin = gs_join_miter;
        if (!strcmp(stroke_line_join_att, "Bevel")) linejoin = gs_join_bevel;
        if (!strcmp(stroke_line_join_att, "Round")) linejoin = gs_join_round;
    }
    gs_setlinejoin(ctx->pgs, linejoin);

    miterlimit = 10.0;
    if (stroke_miter_limit_att)
        miterlimit = atof(stroke_miter_limit_att);
    gs_setmiterlimit(ctx->pgs, miterlimit);

    linewidth = 1.0;
    if (stroke_thickness_att)
        linewidth = atof(stroke_thickness_att);
    gs_setlinewidth(ctx->pgs, linewidth);

    if (stroke_dash_array_att)
    {
        char *s = stroke_dash_array_att;
        float *dash_array;
        float dash_offset = 0.0;
        int dash_count = 0;
        int dash_mem_count = 0;

        /* Do an initial reasonable allocation. If that
           runs out, double until we get to a max size
           and then just add that max each overrun. */
        dash_array = xps_alloc(ctx, sizeof(float) * INITIAL_DASH_SIZE);
        if (dash_array == NULL)
        {
            gs_throw(gs_error_VMerror, "out of memory: dash_array.\n");
            return gs_error_VMerror;
        }
        dash_mem_count = INITIAL_DASH_SIZE;

        if (stroke_dash_offset_att)
            dash_offset = atof(stroke_dash_offset_att) * linewidth;

        while (*s)
        {
            while (*s == ' ')
                s++;
            if (*s) /* needed in case of a space before the last quote */
            {
                /* Double up to a max size of ADDITIVE_DASH_SIZE and then add
                   that amount each time */
                if (dash_count > (dash_mem_count - 1))
                {
                    if (dash_mem_count < ADDITIVE_DASH_SIZE)
                        dash_mem_count = dash_mem_count * 2;
                    else
                        dash_mem_count = dash_mem_count + ADDITIVE_DASH_SIZE;
                    dash_array = (float*) xps_realloc(ctx, dash_array, sizeof(float) * dash_mem_count);
                    if (dash_array == NULL)
                    {
                        gs_throw(gs_error_VMerror, "out of memory: dash_array realloc.\n");
                        return gs_error_VMerror;
                    }
                }
                dash_array[dash_count++] = atof(s) * linewidth;
            }
            while (*s && *s != ' ')
                s++;
        }

        if (dash_count > 0)
        {
            float phase_len = 0;
            int i;
            for (i = 0; i < dash_count; ++i)
                phase_len += dash_array[i];
            if (phase_len == 0)
                dash_count = 0;
        }
        gs_setdash(ctx->pgs, dash_array, dash_count, dash_offset);
        xps_free(ctx, dash_array);
    }
    else
    {
        gs_setdash(ctx->pgs, NULL, 0, 0.0);
    }

    if (transform_att || transform_tag)
    {
        gs_matrix transform;

        if (transform_att)
            xps_parse_render_transform(ctx, transform_att, &transform);
        if (transform_tag)
            xps_parse_matrix_transform(ctx, transform_tag, &transform);

        gs_concat(ctx->pgs, &transform);
    }

    if (clip_att || clip_tag)
    {
        if (clip_att)
            xps_parse_abbreviated_geometry(ctx, clip_att);
        if (clip_tag)
            xps_parse_path_geometry(ctx, dict, clip_tag, 0);
        xps_clip(ctx);
    }

#if 0 // XXX
    if (opacity_att || opacity_mask_tag)
    {
        /* clip the bounds with the actual path */
        if (data_att)
            xps_parse_abbreviated_geometry(ctx, data_att);
        if (data_tag)
            xps_parse_path_geometry(ctx, dict, data_tag, 0);
        xps_update_bounds(ctx, &saved_bounds_opacity);
        gs_newpath(ctx->pgs);
    }
#endif
    /* xps_begin_opacity put into the fill_att, etc loops so that we can
       push groups of smaller sizes.  This makes it necessary to add a pushed
       flag so that we don't do multiple pushes if we have a fill and stroke
       attribute for the same group. */
    if (stroke_att || stroke_tag) {
        uses_stroke = true;
    }
    if (fill_att)
    {
        gs_color_space *colorspace;

        if (data_att)
            xps_parse_abbreviated_geometry(ctx, data_att);
        if (data_tag)
            xps_parse_path_geometry(ctx, dict, data_tag, 0);

        code = xps_begin_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag, true, uses_stroke);
        if (code)
        {
            gs_grestore(ctx->pgs);
            return gs_rethrow(code, "cannot create transparency group");
        }

        /* Color must be set *after* we begin opacity */
        xps_parse_color(ctx, base_uri, fill_att, &colorspace, samples);
        if (fill_opacity_att)
            samples[0] *= atof(fill_opacity_att);
        xps_set_color(ctx, colorspace, samples);
        rc_decrement(colorspace, "xps_parse_path");

        opacity_pushed = true;
        xps_fill(ctx);
    }

    if (fill_tag)
    {
        if (data_att)
            xps_parse_abbreviated_geometry(ctx, data_att);
        if (data_tag)
            xps_parse_path_geometry(ctx, dict, data_tag, 0);

        if (!opacity_pushed) {
            code = xps_begin_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag, true, uses_stroke);
            if (code)
            {
                gs_grestore(ctx->pgs);
                return gs_rethrow(code, "cannot create transparency group");
            }
            opacity_pushed = true;
        }

        code = xps_parse_brush(ctx, fill_uri, dict, fill_tag);
        if (code < 0)
        {
            xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
            gs_grestore(ctx->pgs);
            return gs_rethrow(code, "cannot parse fill brush");
        }
    }

    if (stroke_att)
    {
        gs_color_space *colorspace;

        if (data_att)
            xps_parse_abbreviated_geometry(ctx, data_att);
        if (data_tag)
            xps_parse_path_geometry(ctx, dict, data_tag, 1);

        if (!opacity_pushed) {
            code = xps_begin_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag, true, true);
            if (code)
            {
                gs_grestore(ctx->pgs);
                return gs_rethrow(code, "cannot create transparency group");
            }
            opacity_pushed = true;
        }

        /* Color must be set *after* the group is pushed */
        xps_parse_color(ctx, base_uri, stroke_att, &colorspace, samples);
        if (stroke_opacity_att)
            samples[0] *= atof(stroke_opacity_att);
        xps_set_color(ctx, colorspace, samples);
        rc_decrement(colorspace, "xps_parse_path");

        gs_stroke(ctx->pgs);
    }

    if (stroke_tag)
    {
        if (data_att)
            xps_parse_abbreviated_geometry(ctx, data_att);
        if (data_tag)
            xps_parse_path_geometry(ctx, dict, data_tag, 1);

        if (!opacity_pushed) {
            code = xps_begin_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag, true, true);
            if (code)
            {
                gs_grestore(ctx->pgs);
                return gs_rethrow(code, "cannot create transparency group");
            }
            opacity_pushed = true;
        }

        ctx->fill_rule = 1; /* over-ride fill rule when converting outline to stroked */
        gs_strokepath2(ctx->pgs);

        code = xps_parse_brush(ctx, stroke_uri, dict, stroke_tag);
        if (code < 0)
        {
            xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
            gs_grestore(ctx->pgs);
            return gs_rethrow(code, "cannot parse stroke brush");
        }
    }

    xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
    gs_grestore(ctx->pgs);
    return 0;
}