summaryrefslogtreecommitdiff
path: root/gs/base/gdevpdfd.c
blob: 0c55fde695b070a880595fb67e7a973e1a4cce58 (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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
/* Copyright (C) 2001-2006 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 that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id$ */
/* Path drawing procedures for pdfwrite driver */
#include "math_.h"
#include "memory_.h"
#include "gx.h"
#include "gxdevice.h"
#include "gxfixed.h"
#include "gxistate.h"
#include "gxpaint.h"
#include "gxcoord.h"
#include "gxdevmem.h"
#include "gxcolor2.h"
#include "gxhldevc.h"
#include "gsstate.h"
#include "gxstate.h"
#include "gserrors.h"
#include "gsptype2.h"
#include "gsshade.h"
#include "gzpath.h"
#include "gzcpath.h"
#include "gdevpdfx.h"
#include "gdevpdfg.h"
#include "gdevpdfo.h"
#include "gsutil.h"
#include "gdevpdtf.h"
#include "gdevpdts.h"
#include "gxdevsop.h"

/* ---------------- Drawing ---------------- */

/* Fill a rectangle. */
int
gdev_pdf_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
                        gx_color_index color)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    int code;

    code = pdf_open_page(pdev, PDF_IN_STREAM);
    if (code < 0)
        return code;
    /* Make sure we aren't being clipped. */
    code = pdf_put_clip_path(pdev, NULL);
    if (code < 0)
        return code;
    pdf_set_pure_color(pdev, color, &pdev->saved_fill_color,
                       &pdev->fill_used_process_color,
                       &psdf_set_fill_color_commands);
    if (!pdev->HaveStrokeColor)
        pdev->saved_stroke_color = pdev->saved_fill_color;
    pprintd4(pdev->strm, "%d %d %d %d re f\n", x, y, w, h);
    return 0;
}

/* ---------------- Path drawing ---------------- */

/* ------ Vector device implementation ------ */

static int
pdf_setlinewidth(gx_device_vector * vdev, floatp width)
{
    /* Acrobat Reader doesn't accept negative line widths. */
    return psdf_setlinewidth(vdev, fabs(width));
}

static int
pdf_can_handle_hl_color(gx_device_vector * vdev, const gs_imager_state * pis,
                 const gx_drawing_color * pdc)
{
    return pis != NULL;
}

static int
pdf_setfillcolor(gx_device_vector * vdev, const gs_imager_state * pis,
                 const gx_drawing_color * pdc)
{
    gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
    bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pis, pdc);
    const gs_imager_state *pis_for_hl_color = (hl_color ? pis : NULL);

    if (!pdev->HaveStrokeColor) {
        /* opdfread.ps assumes same color for stroking and non-stroking operations. */
        int code = pdf_set_drawing_color(pdev, pis_for_hl_color, pdc, &pdev->saved_stroke_color,
                                    &pdev->stroke_used_process_color,
                                    &psdf_set_stroke_color_commands);
        if (code < 0)
            return code;
    }
    return pdf_set_drawing_color(pdev, pis_for_hl_color, pdc, &pdev->saved_fill_color,
                                 &pdev->fill_used_process_color,
                                 &psdf_set_fill_color_commands);
}

static int
pdf_setstrokecolor(gx_device_vector * vdev, const gs_imager_state * pis,
                   const gx_drawing_color * pdc)
{
    gx_device_pdf *const pdev = (gx_device_pdf *)vdev;
    bool hl_color = (*vdev_proc(vdev, can_handle_hl_color)) (vdev, pis, pdc);
    const gs_imager_state *pis_for_hl_color = (hl_color ? pis : NULL);

    if (!pdev->HaveStrokeColor) {
        /* opdfread.ps assumes same color for stroking and non-stroking operations. */
        int code = pdf_set_drawing_color(pdev, pis_for_hl_color, pdc, &pdev->saved_fill_color,
                                 &pdev->fill_used_process_color,
                                 &psdf_set_fill_color_commands);
        if (code < 0)
            return code;
    }
    return pdf_set_drawing_color(pdev, pis_for_hl_color, pdc, &pdev->saved_stroke_color,
                                 &pdev->stroke_used_process_color,
                                 &psdf_set_stroke_color_commands);
}

static int
pdf_dorect(gx_device_vector * vdev, fixed x0, fixed y0, fixed x1, fixed y1,
           gx_path_type_t type)
{
    gx_device_pdf *pdev = (gx_device_pdf *)vdev;
    fixed xmax = int2fixed(32766), ymax = int2fixed(32766);
    int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0);
    fixed xmin = (pdev->sbstack_depth > bottom ? -xmax : 0);
    fixed ymin = (pdev->sbstack_depth > bottom ? -ymax : 0);

    /*
     * If we're doing a stroke operation, expand the checking box by the
     * stroke width.
     */
    if (type & gx_path_type_stroke) {
        double w = vdev->state.line_params.half_width;
        double xw = w * (fabs(vdev->state.ctm.xx) + fabs(vdev->state.ctm.yx));
        int d = float2fixed(xw) + fixed_1;

        xmin -= d;
        xmax += d;
        ymin -= d;
        ymax += d;
    }
    if (!(type & gx_path_type_clip) &&
        (x0 > xmax || x1 < xmin || y0 > ymax || y1 < ymin ||
         x0 > x1 || y0 > y1)
        )
        return 0;		/* nothing to fill or stroke */
    /*
     * Clamp coordinates to avoid tripping over Acrobat Reader's limit
     * of 32K on user coordinate values.
     */
    if (x0 < xmin)
        x0 = xmin;
    if (x1 > xmax)
        x1 = xmax;
    if (y0 < ymin)
        y0 = ymin;
    if (y1 > ymax)
        y1 = ymax;
    return psdf_dorect(vdev, x0, y0, x1, y1, type);
}

static int
pdf_endpath(gx_device_vector * vdev, gx_path_type_t type)
{
    return 0;			/* always handled by caller */
}

const gx_device_vector_procs pdf_vector_procs = {
        /* Page management */
    NULL,
        /* Imager state */
    pdf_setlinewidth,
    psdf_setlinecap,
    psdf_setlinejoin,
    psdf_setmiterlimit,
    psdf_setdash,
    psdf_setflat,
    psdf_setlogop,
        /* Other state */
    pdf_can_handle_hl_color,
    pdf_setfillcolor,
    pdf_setstrokecolor,
        /* Paths */
    psdf_dopath,
    pdf_dorect,
    psdf_beginpath,
    psdf_moveto,
    psdf_lineto,
    psdf_curveto,
    psdf_closepath,
    pdf_endpath
};

/* ------ Utilities ------ */

/* Store a copy of clipping path. */
int
pdf_remember_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
{
    /* Used for skipping redundant clip paths. SF bug #624168. */
    if (pdev->clip_path != 0) {
        gx_path_free(pdev->clip_path, "pdf clip path");
    }
    if (pcpath == 0) {
        pdev->clip_path = 0;
        return 0;
    }
    pdev->clip_path = gx_path_alloc(pdev->pdf_memory, "pdf clip path");
    if (pdev->clip_path == 0)
        return_error(gs_error_VMerror);
    return gx_cpath_to_path((gx_clip_path *)pcpath, pdev->clip_path);
}

/* Check if same clipping path. */
static int
pdf_is_same_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
{
    /* Used for skipping redundant clip paths. SF bug #624168. */
    gs_cpath_enum cenum;
    gs_path_enum penum;
    gs_fixed_point vs0[3], vs1[3];
    int code, pe_op;

    if ((pdev->clip_path != 0) != (pcpath != 0))
        return 0;
    /* Both clip paths are empty, so the same */
    if (pdev->clip_path == 0)
        return 1;
    code = gx_path_enum_init(&penum, pdev->clip_path);
    if (code < 0)
        return code;
    code = gx_cpath_enum_init(&cenum, (gx_clip_path *)pcpath);
    if (code < 0)
        return code;
    /* This flags a warning in Coverity, uninitialised variable cenum.first_visit */
    /* This is because gx_cpath_enum_init doesn't initialise first_visit, but the */
    /* variable can be used in enum_next. However, this is not truly used this    */
    /* way. The enum_init sets the 'state' to 'scan', and the first thing that happens */
    /* in enum_next when state is 'scan' is to set first_visit. */
    while ((code = gx_cpath_enum_next(&cenum, vs0)) > 0) {
        pe_op = gx_path_enum_next(&penum, vs1);
        if (pe_op < 0)
            return pe_op;
        if (pe_op != code)
            return 0;
        switch (pe_op) {
            case gs_pe_curveto:
                if (vs0[1].x != vs1[1].x || vs0[1].y != vs1[1].y ||
                    vs0[2].x != vs1[2].x || vs0[2].y != vs1[2].y)
                    return 0;
            case gs_pe_moveto:
            case gs_pe_lineto:
                if (vs0[0].x != vs1[0].x || vs0[0].y != vs1[0].y)
                    return 0;
        }
    }
    if (code < 0)
        return code;
    code = gx_path_enum_next(&penum, vs1);
    if (code < 0)
        return code;
    return (code == 0);
}

/* Test whether we will need to put the clipping path. */
bool
pdf_must_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
{
    if (pcpath == NULL) {
        if (pdev->clip_path_id == pdev->no_clip_path_id)
            return false;
    } else {
        if (pdev->clip_path_id == pcpath->id)
            return false;
        if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
                                        int2fixed(pdev->width),
                                        int2fixed(pdev->height)))
            if (pdev->clip_path_id == pdev->no_clip_path_id)
                return false;
        if (pdf_is_same_clip_path(pdev, pcpath) > 0) {
            pdev->clip_path_id = pcpath->id;
            return false;
        }
    }
    return true;
}

/* Put a single element of a clipping path list. */
static int
pdf_put_clip_path_list_elem(gx_device_pdf * pdev, gx_cpath_path_list *e,
        gs_path_enum *cenum, gdev_vector_dopath_state_t *state,
        gs_fixed_point vs[3])
{   /* This recursive function provides a reverse order of the list elements. */
    int pe_op;

    if (e->next != NULL) {
        int code = pdf_put_clip_path_list_elem(pdev, e->next, cenum, state, vs);

        if (code != 0)
            return code;
    }
    gx_path_enum_init(cenum, &e->path);
    while ((pe_op = gx_path_enum_next(cenum, vs)) > 0)
        gdev_vector_dopath_segment(state, pe_op, vs);
    pprints1(pdev->strm, "%s n\n", (e->rule <= 0 ? "W" : "W*"));
    if (pe_op < 0)
        return pe_op;
    return 0;
}

/* Put a clipping path on the output file. */
int
pdf_put_clip_path(gx_device_pdf * pdev, const gx_clip_path * pcpath)
{
    int code;
    stream *s = pdev->strm;
    gs_id new_id;

    /* Check for no update needed. */
    if (pcpath == NULL) {
        if (pdev->clip_path_id == pdev->no_clip_path_id)
            return 0;
        new_id = pdev->no_clip_path_id;
    } else {
        if (pdev->clip_path_id == pcpath->id)
            return 0;
        new_id = pcpath->id;
        if (gx_cpath_includes_rectangle(pcpath, fixed_0, fixed_0,
                                        int2fixed(pdev->width),
                                        int2fixed(pdev->height))
            ) {
            if (pdev->clip_path_id == pdev->no_clip_path_id)
                return 0;
            new_id = pdev->no_clip_path_id;
        }
        code = pdf_is_same_clip_path(pdev, pcpath);
        if (code < 0)
            return code;
        if (code) {
            pdev->clip_path_id = new_id;
            return 0;
        }
    }
    /*
     * The contents must be open already, so the following will only exit
     * text or string context.
     */
    code = pdf_open_contents(pdev, PDF_IN_STREAM);
    if (code < 0)
        return code;
    /* Use Q to unwind the old clipping path. */
    if (pdev->vgstack_depth > pdev->vgstack_bottom) {
        code = pdf_restore_viewer_state(pdev, s);
        if (code < 0)
            return code;
    }
    if (new_id != pdev->no_clip_path_id) {
        const gs_fixed_rect *rect = cpath_is_rectangle(pcpath);

        /* Use q to allow the new clipping path to unwind.  */
        code = pdf_save_viewer_state(pdev, s);
        if (code < 0)
            return code;
        if (rect != NULL) {
            /* Use unrounded coordinates. */
            pprintg4(s, "%g %g %g %g re",
                fixed2float(rect->p.x), fixed2float(rect->p.y),
                fixed2float(rect->q.x - rect->p.x),
                fixed2float(rect->q.y - rect->p.y));
            pprints1(s, " %s n\n", (pcpath->rule <= 0 ? "W" : "W*"));
        } else {
            gdev_vector_dopath_state_t state;
            gs_fixed_point vs[3];
            int pe_op;

            gdev_vector_dopath_init(&state, (gx_device_vector *)pdev,
                                    gx_path_type_fill, NULL);
            if (pcpath->path_list == NULL) {
                /*
                 * We think this should be never executed.
                 * This obsolete branch writes a clip path intersection
                 * as a set of rectangles computed by
                 * gx_cpath_intersect_path_slow.
                 * Those rectangles use coordinates rounded to pixels,
                 * therefore the precision may be unsatisfactory -
                 * see Bug 688407.
                 */
                gs_cpath_enum cenum;

                /*
                 * We have to break 'const' here because the clip path
                 * enumeration logic uses some internal mark bits.
                 * This is very unfortunate, but until we can come up with
                 * a better algorithm, it's necessary.
                 */
                gx_cpath_enum_init(&cenum, (gx_clip_path *) pcpath);
                while ((pe_op = gx_cpath_enum_next(&cenum, vs)) > 0)
                    gdev_vector_dopath_segment(&state, pe_op, vs);
                pprints1(s, "%s n\n", (pcpath->rule <= 0 ? "W" : "W*"));
                if (pe_op < 0)
                    return pe_op;
            } else {
                gs_path_enum cenum;

                code = pdf_put_clip_path_list_elem(pdev, pcpath->path_list, &cenum, &state, vs);
                if (code < 0)
                    return code;
            }
        }
    }
    pdev->clip_path_id = new_id;
    return pdf_remember_clip_path(pdev,
            (pdev->clip_path_id == pdev->no_clip_path_id ? NULL : pcpath));
}

/*
 * Compute the scaling to ensure that user coordinates for a path are within
 * Acrobat's range.  Return true if scaling was needed.  In this case, the
 * CTM will be multiplied by *pscale, and all coordinates will be divided by
 * *pscale.
 */
static bool
make_rect_scaling(const gx_device_pdf *pdev, const gs_fixed_rect *bbox,
                  floatp prescale, double *pscale)
{
    double bmin, bmax;

    bmin = min(bbox->p.x / pdev->scale.x, bbox->p.y / pdev->scale.y) * prescale;
    bmax = max(bbox->q.x / pdev->scale.x, bbox->q.y / pdev->scale.y) * prescale;
    if (bmin <= int2fixed(-MAX_USER_COORD) ||
        bmax > int2fixed(MAX_USER_COORD)
        ) {
        /* Rescale the path. */
        *pscale = max(bmin / int2fixed(-MAX_USER_COORD),
                      bmax / int2fixed(MAX_USER_COORD));
        return true;
    } else {
        *pscale = 1;
        return false;
    }
}

/*
 * Prepare a fill with a color anc a clipping path.
 * Return 1 if there is nothing to paint.
 * Changes *box to the clipping box.
 */
static int
prepare_fill_with_clip(gx_device_pdf *pdev, const gs_imager_state * pis,
              gs_fixed_rect *box, bool have_path,
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
{
    bool new_clip;
    int code;

    /*
     * Check for an empty clipping path.
     */
    if (pcpath) {
        gs_fixed_rect cbox;

        gx_cpath_outer_box(pcpath, &cbox);
        if (cbox.p.x >= cbox.q.x || cbox.p.y >= cbox.q.y)
            return 1;		/* empty clipping path */
        *box = cbox;
    }
    new_clip = pdf_must_put_clip_path(pdev, pcpath);
    if (have_path || pdev->context == PDF_IN_NONE || new_clip) {
        if (new_clip)
            code = pdf_unclip(pdev);
        else
            code = pdf_open_page(pdev, PDF_IN_STREAM);
        if (code < 0)
            return code;
    }
    code = pdf_prepare_fill(pdev, pis);
    if (code < 0)
        return code;
    return pdf_put_clip_path(pdev, pcpath);
}

/* -------------A local image converter device. -----------------------------*/

public_st_pdf_lcvd_t();

static int
lcvd_fill_rectangle_shifted(gx_device *dev, int x, int y, int width, int height, gx_color_index color)
{
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;

    return cvd->std_fill_rectangle((gx_device *)&cvd->mdev,
        x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color);
}
static int
lcvd_fill_rectangle_shifted2(gx_device *dev, int x, int y, int width, int height, gx_color_index color)
{
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
    int code;

    code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask,
        x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, (gx_color_index)1);
    if (code < 0)
        return code;
    return cvd->std_fill_rectangle((gx_device *)&cvd->mdev,
        x - cvd->mdev.mapped_x, y - cvd->mdev.mapped_y, width, height, color);
}
static void
lcvd_get_clipping_box_shifted_from_mdev(gx_device *dev, gs_fixed_rect *pbox)
{
    fixed ofs;
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;

    cvd->std_get_clipping_box((gx_device *)&cvd->mdev, pbox);
    ofs = int2fixed(cvd->mdev.mapped_x);
    pbox->p.x += ofs;
    pbox->q.x += ofs;
    ofs = int2fixed(cvd->mdev.mapped_y);
    pbox->p.y += ofs;
    pbox->q.y += ofs;
}
static int
lcvd_dev_spec_op(gx_device *pdev1, int dev_spec_op,
                void *data, int size)
{
    switch (dev_spec_op) {
        case gxdso_pattern_shading_area:
            return 1; /* Request shading area. */
        case gxdso_pattern_can_accum:
        case gxdso_pattern_start_accum:
        case gxdso_pattern_finish_accum:
        case gxdso_pattern_load:
        case gxdso_pattern_is_cpath_accum:
        case gxdso_pattern_shfill_doesnt_need_path:
        case gxdso_pattern_handles_clip_path:
            return 0;
    }
    return gx_default_dev_spec_op(pdev1, dev_spec_op, data, size);
}
static int
lcvd_close_device_with_writing(gx_device *pdev)
{
    /* Assuming 'mdev' is being closed before 'mask' - see gx_image3_end_image. */
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)pdev;
    int code, code1;

    code = pdf_dump_converted_image(cvd->pdev, cvd);
    code1 = cvd->std_close_device((gx_device *)&cvd->mdev);
    return code < 0 ? code : code1;
}

static int
write_image(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m)
{
    gs_image_t image;
    pdf_image_writer writer;
    const int sourcex = 0;
    int code;

    if (m != NULL)
        pdf_put_matrix(pdev, NULL, m, " cm\n");
    code = pdf_copy_color_data(pdev, mdev->base, sourcex,
                mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height,
                &image, &writer, 2);
    if (code == 1)
        code = 0; /* Empty image. */
    else if (code == 0)
        code = pdf_do_image(pdev, writer.pres, NULL, true);
    return code;
}
static int
write_mask(gx_device_pdf *pdev, gx_device_memory *mdev, gs_matrix *m)
{
    const int sourcex = 0;
    gs_id save_clip_id = pdev->clip_path_id;
    bool save_skip_color = pdev->skip_colors;
    int code;

    if (m != NULL)
        pdf_put_matrix(pdev, NULL, m, " cm\n");
    pdev->clip_path_id = pdev->no_clip_path_id;
    pdev->skip_colors = true;
    code = gdev_pdf_copy_mono((gx_device *)pdev, mdev->base, sourcex,
                mdev->raster, gx_no_bitmap_id, 0, 0, mdev->width, mdev->height,
                gx_no_color_index, (gx_color_index)0);
    pdev->clip_path_id = save_clip_id;
    pdev->skip_colors = save_skip_color;
    return code;
}

static void
max_subimage_width(int width, byte *base, int x0, long count1, int *x1, long *count)
{
    long c = 0, c1 = count1 - 1;
    int x = x0;
    byte p = 1; /* The inverse of the previous bit. */
    byte r;     /* The inverse of the current  bit. */
    byte *q = base + (x / 8), m = 0x80 >> (x % 8);

    for (; x < width; x++) {
        r = !(*q & m);
        if (p != r) {
            if (c >= c1) {
                if (!r)
                    goto ex; /* stop before the upgrade. */
            }
            c++;
        }
        p = r;
        m >>= 1;
        if (!m) {
            m = 0x80;
            q++;
        }
    }
    if (p)
        c++; /* Account the last downgrade. */
ex:
    *count = c;
    *x1 = x;
}

static void
compute_subimage(int width, int height, int raster, byte *base,
                 int x0, int y0, long MaxClipPathSize, int *x1, int *y1)
{
    /* Returns a semiopen range : [x0:x1)*[y0:y1). */
    if (x0 != 0) {
        long count;

        /* A partial single scanline. */
        max_subimage_width(width, base + y0 * raster, x0, MaxClipPathSize / 4, x1, &count);
        *y1 = y0;
    } else {
        int xx, y = y0, yy;
        long count, count1 = MaxClipPathSize / 4;

        for(; y < height && count1 > 0; ) {
            max_subimage_width(width, base + y * raster, 0, count1, &xx, &count);
            if (xx < width) {
                if (y == y0) {
                    /* Partial single scanline. */
                    *y1 = y + 1;
                    *x1 = xx;
                    return;
                } else {
                    /* Full lines before this scanline. */
                    break;
                }
            }
            count1 -= count;
            yy = y + 1;
            for (; yy < height; yy++)
                if (memcmp(base + raster * y, base + raster * yy, raster))
                    break;
            y = yy;

        }
        *y1 = y;
        *x1 = width;
    }
}

static int
image_line_to_clip(gx_device_pdf *pdev, byte *base, int x0, int x1, int y0, int y1, bool started)
{   /* returns the number of segments or error code. */
    int x = x0, xx;
    byte *q = base + (x / 8), m = 0x80 >> (x % 8);
    long c = 0;

    for (;;) {
        /* Look for upgrade : */
        for (; x < x1; x++) {
            if (*q & m)
                break;
            m >>= 1;
            if (!m) {
                m = 0x80;
                q++;
            }
        }
        if (x == x1)
            return c;
        xx = x;
        /* Look for downgrade : */
        for (; x < x1; x++) {
            if (!(*q & m))
                break;
            m >>= 1;
            if (!m) {
                m = 0x80;
                q++;
            }
        }
        /* Found the interval [xx:x). */
        if (!started) {
            stream_puts(pdev->strm, "n\n");
            started = true;
        }
        pprintld2(pdev->strm, "%ld %ld m ", xx, y0);
        pprintld2(pdev->strm, "%ld %ld l ", x, y0);
        pprintld2(pdev->strm, "%ld %ld l ", x, y1);
        pprintld2(pdev->strm, "%ld %ld l h\n", xx, y1);
        c += 4;
    }
    return c;
}

static int
mask_to_clip(gx_device_pdf *pdev, int width, int height,
             int raster, byte *base, int x0, int y0, int x1, int y1)
{
    int y, yy, code = 0;
    bool has_segments = false;

    for (y = y0; y < y1 && code >= 0;) {
        yy = y + 1;
        if (x0 == 0) {
        for (; yy < y1; yy++)
            if (memcmp(base + raster * y, base + raster * yy, raster))
                break;
        }
        code = image_line_to_clip(pdev, base + raster * y, x0, x1, y, yy, has_segments);
        if (code > 0)
            has_segments = true;
        y = yy;
    }
    if (has_segments)
        stream_puts(pdev->strm, "W n\n");
    return code < 0 ? code : has_segments ? 1 : 0;
}

static int
write_subimage(gx_device_pdf *pdev, gx_device_memory *mdev, int x, int y, int x1, int y1)
{
    gs_image_t image;
    pdf_image_writer writer;
    /* expand in 1 pixel to provide a proper color interpolation */
    int X = max(0, x - 1);
    int Y = max(0, y - 1);
    int X1 = min(mdev->width, x1 + 1);
    int Y1 = min(mdev->height, y1 + 1);
    int code;

    code = pdf_copy_color_data(pdev, mdev->base + mdev->raster * Y, X,
                mdev->raster, gx_no_bitmap_id,
                X, Y, X1 - X, Y1 - Y,
                &image, &writer, 2);
    if (code < 0)
        return code;
    if (!writer.pres)
        return 0; /* inline image. */
    return pdf_do_image(pdev, writer.pres, NULL, true);
}

static int
write_image_with_clip(gx_device_pdf *pdev, pdf_lcvd_t *cvd)
{
    int x = 0, y = 0;
    int code, code1;

    if (cvd->write_matrix)
        pdf_put_matrix(pdev, NULL, &cvd->m, " cm q\n");
    for(;;) {
        int x1, y1;

        compute_subimage(cvd->mask->width, cvd->mask->height,
                         cvd->mask->raster, cvd->mask->base,
                         x, y, max(pdev->MaxClipPathSize, 100), &x1, &y1);
        code = mask_to_clip(pdev,
                         cvd->mask->width, cvd->mask->height,
                         cvd->mask->raster, cvd->mask->base,
                         x, y, x1, y1);
        if (code < 0)
            return code;
        if (code > 0) {
            code1 = write_subimage(pdev, &cvd->mdev, x, y, x1, y1);
            if (code1 < 0)
                return code1;
        }
        if (x1 >= cvd->mdev.width && y1 >= cvd->mdev.height)
            break;
        if (code > 0)
            stream_puts(pdev->strm, "Q q\n");
        if (x1 == cvd->mask->width) {
            x = 0;
            y = y1;
        } else {
            x = x1;
            y = y1;
        }
    }
    if (cvd->write_matrix)
        stream_puts(pdev->strm, "Q\n");
    return 0;
}

int
pdf_dump_converted_image(gx_device_pdf *pdev, pdf_lcvd_t *cvd)
{
    int code = 0;

    if (!cvd->path_is_empty || cvd->has_background) {
        if (!cvd->has_background)
            stream_puts(pdev->strm, "W n\n");
        code = write_image(pdev, &cvd->mdev, (cvd->write_matrix ? &cvd->m : NULL));
        cvd->path_is_empty = true;
    } else if (!cvd->mask_is_empty && pdev->PatternImagemask) {
        /* Convert to imagemask with a pattern color. */
        /* See also use_image_as_pattern in gdevpdfi.c . */
        gs_imager_state s;
        gs_pattern1_instance_t inst;
        gs_id id = gs_next_ids(cvd->mdev.memory, 1);
        cos_value_t v;
        const pdf_resource_t *pres;

        memset(&s, 0, sizeof(s));
        s.ctm.xx = cvd->m.xx;
        s.ctm.xy = cvd->m.xy;
        s.ctm.yx = cvd->m.yx;
        s.ctm.yy = cvd->m.yy;
        s.ctm.tx = cvd->m.tx;
        s.ctm.ty = cvd->m.ty;
        memset(&inst, 0, sizeof(inst));
        inst.saved = (gs_state *)&s; /* HACK : will use s.ctm only. */
        inst.template.PaintType = 1;
        inst.template.TilingType = 1;
        inst.template.BBox.p.x = inst.template.BBox.p.y = 0;
        inst.template.BBox.q.x = cvd->mdev.width;
        inst.template.BBox.q.y = cvd->mdev.height;
        inst.template.XStep = (float)cvd->mdev.width;
        inst.template.YStep = (float)cvd->mdev.height;
        code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
            gxdso_pattern_start_accum, &inst, id);
        if (code >= 0) {
            stream_puts(pdev->strm, "W n\n");
            code = write_image(pdev, &cvd->mdev, NULL);
        }
        pres = pdev->accumulating_substream_resource;
        if (code >= 0)
            code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
                gxdso_pattern_finish_accum, &inst, id);
        if (code >= 0)
            code = (*dev_proc(pdev, dev_spec_op))((gx_device *)pdev,
                gxdso_pattern_load, &inst, id);
        if (code >= 0)
            code = pdf_cs_Pattern_colored(pdev, &v);
        if (code >= 0) {
            cos_value_write(&v, pdev);
            pprintld1(pdev->strm, " cs /R%ld scn ", pdf_resource_id(pres));
        }
        if (code >= 0)
            code = write_mask(pdev, cvd->mask, (cvd->write_matrix ? &cvd->m : NULL));
        cvd->mask_is_empty = true;
    } else if (!cvd->mask_is_empty && !pdev->PatternImagemask) {
        /* Convert to image with a clipping path. */
        stream_puts(pdev->strm, "q\n");
        code = write_image_with_clip(pdev, cvd);
        stream_puts(pdev->strm, "Q\n");
    }
    if (code > 0)
        code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev,
                0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0);
    return code;
}
static int
lcvd_handle_fill_path_as_shading_coverage(gx_device *dev,
    const gs_imager_state *pis, gx_path *ppath,
    const gx_fill_params *params,
    const gx_drawing_color *pdcolor, const gx_clip_path *pcpath)
{
    pdf_lcvd_t *cvd = (pdf_lcvd_t *)dev;
    gx_device_pdf *pdev = (gx_device_pdf *)cvd->mdev.target;
    int code;

    if (cvd->has_background)
        return 0;
    if (gx_path_is_null(ppath)) {
        /* use the mask. */
        if (!cvd->path_is_empty) {
            code = pdf_dump_converted_image(pdev, cvd);
            if (code < 0)
                return code;
            stream_puts(pdev->strm, "Q q\n");
            dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted2;
        }
        if (!cvd->mask_is_clean || !cvd->path_is_empty) {
            code = (*dev_proc(cvd->mask, fill_rectangle))((gx_device *)cvd->mask,
                        0, 0, cvd->mask->width, cvd->mask->height, (gx_color_index)0);
            if (code < 0)
                return code;
            cvd->mask_is_clean = true;
        }
        cvd->path_is_empty = true;
        cvd->mask_is_empty = false;
    } else {
        gs_matrix m;

        gs_make_translation(cvd->path_offset.x, cvd->path_offset.y, &m);
        /* use the clipping. */
        if (!cvd->mask_is_empty) {
            code = pdf_dump_converted_image(pdev, cvd);
            if (code < 0)
                return code;
            stream_puts(pdev->strm, "Q q\n");
            dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted;
            cvd->mask_is_empty = true;
        }
        code = gdev_vector_dopath((gx_device_vector *)pdev, ppath,
                            gx_path_type_fill | gx_path_type_optimize, &m);
        if (code < 0)
            return code;
        stream_puts(pdev->strm, "h\n");
        cvd->path_is_empty = false;
    }
    return 0;
}

int
pdf_setup_masked_image_converter(gx_device_pdf *pdev, gs_memory_t *mem, const gs_matrix *m, pdf_lcvd_t **pcvd,
                                 bool need_mask, int x, int y, int w, int h, bool write_on_close)
{
    int code;
    gx_device_memory *mask = 0;
    pdf_lcvd_t *cvd = *pcvd;

    if (cvd == NULL) {
        cvd = gs_alloc_struct(mem, pdf_lcvd_t, &st_pdf_lcvd_t, "pdf_setup_masked_image_converter");
        if (cvd == NULL)
            return_error(gs_error_VMerror);
        *pcvd = cvd;
    }
    cvd->pdev = pdev;
    gs_make_mem_device(&cvd->mdev, gdev_mem_device_for_bits(pdev->color_info.depth),
                mem, 0, (gx_device *)pdev);
    cvd->mdev.width  = w;
    cvd->mdev.height = h;
    cvd->mdev.mapped_x = x;
    cvd->mdev.mapped_y = y;
    cvd->mdev.bitmap_memory = mem;
    cvd->mdev.color_info = pdev->color_info;
    cvd->path_is_empty = true;
    cvd->mask_is_empty = true;
    cvd->mask_is_clean = false;
    cvd->has_background = false;
    cvd->mask = 0;
    cvd->write_matrix = true;
    code = (*dev_proc(&cvd->mdev, open_device))((gx_device *)&cvd->mdev);
    if (code < 0)
        return code;
    code = (*dev_proc(&cvd->mdev, fill_rectangle))((gx_device *)&cvd->mdev,
                0, 0, cvd->mdev.width, cvd->mdev.height, (gx_color_index)0);
    if (code < 0)
        return code;
    if (need_mask) {
        mask = gs_alloc_struct(mem, gx_device_memory, &st_device_memory, "pdf_setup_masked_image_converter");
        if (mask == NULL)
            return_error(gs_error_VMerror);
        cvd->mask = mask;
        gs_make_mem_mono_device(mask, mem, (gx_device *)pdev);
        mask->width = cvd->mdev.width;
        mask->height = cvd->mdev.height;
        mask->bitmap_memory = mem;
        code = (*dev_proc(mask, open_device))((gx_device *)mask);
        if (code < 0)
            return code;
        if (write_on_close) {
            code = (*dev_proc(mask, fill_rectangle))((gx_device *)mask,
                        0, 0, mask->width, mask->height, (gx_color_index)0);
            if (code < 0)
                return code;
        }
    }
    cvd->std_fill_rectangle = dev_proc(&cvd->mdev, fill_rectangle);
    cvd->std_close_device = dev_proc(&cvd->mdev, close_device);
    cvd->std_get_clipping_box = dev_proc(&cvd->mdev, get_clipping_box);
    if (!write_on_close) {
        /* Type 3 images will write to the mask directly. */
        dev_proc(&cvd->mdev, fill_rectangle) = (need_mask ? lcvd_fill_rectangle_shifted2
                                                          : lcvd_fill_rectangle_shifted);
        dev_proc(&cvd->mdev, get_clipping_box) = lcvd_get_clipping_box_shifted_from_mdev;
    } else {
        dev_proc(&cvd->mdev, fill_rectangle) = lcvd_fill_rectangle_shifted;
        dev_proc(&cvd->mdev, get_clipping_box) = lcvd_get_clipping_box_shifted_from_mdev;
    }
    dev_proc(&cvd->mdev, dev_spec_op) = lcvd_dev_spec_op;
    dev_proc(&cvd->mdev, fill_path) = lcvd_handle_fill_path_as_shading_coverage;
    cvd->m = *m;
    if (write_on_close) {
        cvd->mdev.is_open = true;
        if (mask)
            mask->is_open = true;
        dev_proc(&cvd->mdev, close_device) = lcvd_close_device_with_writing;
    }
    return 0;
}

void
pdf_remove_masked_image_converter(gx_device_pdf *pdev, pdf_lcvd_t *cvd, bool need_mask)
{
    (*dev_proc(&cvd->mdev, close_device))((gx_device *)&cvd->mdev);
    if (cvd->mask) {
        (*dev_proc(cvd->mask, close_device))((gx_device *)cvd->mask);
        gs_free_object(cvd->mask->memory, cvd->mask, "pdf_remove_masked_image_converter");
    }
}

/* ------ Driver procedures ------ */

/* Fill a path. */
int
gdev_pdf_fill_path(gx_device * dev, const gs_imager_state * pis, gx_path * ppath,
                   const gx_fill_params * params,
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    int code;
    /*
     * HACK: we fill an empty path in order to set the clipping path
     * and the color for writing text.  If it weren't for this, we
     * could detect and skip empty paths before putting out the clip
     * path or the color.  We also clip with an empty path in order
     * to advance currentpoint for show operations without actually
     * drawing anything.
     */
    bool have_path;
    gs_fixed_rect box = {{0, 0}, {0, 0}}, box1;

    have_path = !gx_path_is_void(ppath);
    if (!have_path && !pdev->vg_initial_set) {
        /* See lib/gs_pdfwr.ps about "initial graphic state". */
        pdf_prepare_initial_viewer_state(pdev, pis);
        pdf_reset_graphics(pdev);
        return 0;
    }
    if (have_path) {
        code = gx_path_bbox(ppath, &box);
        if (code < 0)
            return code;
    }
    box1 = box;

    code = prepare_fill_with_clip(pdev, pis, &box, have_path, pdcolor, pcpath);
    if (code == gs_error_rangecheck) {
        /* Fallback to the default implermentation for handling
           a transparency with CompatibilityLevel<=1.3 . */
        return gx_default_fill_path((gx_device *)pdev, pis, ppath, params, pdcolor, pcpath);
    }
    if (code < 0)
        return code;
    if (code == 1)
        return 0; /* Nothing to paint. */
    if (!have_path)
        return 0;
    code = pdf_setfillcolor((gx_device_vector *)pdev, pis, pdcolor);
    if (code == gs_error_rangecheck) {
        const bool convert_to_image = (pdev->CompatibilityLevel <= 1.2 &&
                gx_dc_is_pattern2_color(pdcolor));

        if (!convert_to_image) {
            /* Fallback to the default implermentation for handling
            a shading with CompatibilityLevel<=1.2 . */
            return gx_default_fill_path(dev, pis, ppath, params, pdcolor, pcpath);
        } else {
            /* Convert a shading into a bitmap
               with CompatibilityLevel<=1.2 . */
            pdf_lcvd_t cvd, *pcvd = &cvd;
            int sx, sy;
            gs_fixed_rect bbox, bbox1;
            bool need_mask = gx_dc_pattern2_can_overlap(pdcolor);
            gs_matrix m, save_ctm = ctm_only(pis), ms, msi, mm;
            gs_int_point rect_size;
            /* double scalex = 1.9, scaley = 1.4; debug purpose only. */
            double scale, scalex, scaley;
            int log2_scale_x = 0, log2_scale_y = 0;
            gx_drawing_color dc = *pdcolor;
            gs_pattern2_instance_t pi = *(gs_pattern2_instance_t *)dc.ccolor.pattern;
            gs_state *pgs = gs_state_copy(pi.saved, gs_state_memory(pi.saved));

            if (pgs == NULL)
                return_error(gs_error_VMerror);
            dc.ccolor.pattern = (gs_pattern_instance_t *)&pi;
            pi.saved = pgs;
            code = gx_path_bbox(ppath, &bbox);
            if (code < 0)
                return code;
            rect_intersect(bbox, box);
            code = gx_dc_pattern2_get_bbox(pdcolor, &bbox1);
            if (code < 0)
                return code;
            if (code)
                rect_intersect(bbox, bbox1);
            if (bbox.p.x >= bbox.q.x || bbox.p.y >= bbox.q.y)
                return 0;
            sx = fixed2int(bbox.p.x);
            sy = fixed2int(bbox.p.y);
            gs_make_identity(&m);
            rect_size.x = fixed2int(bbox.q.x + fixed_half) - sx;
            rect_size.y = fixed2int(bbox.q.y + fixed_half) - sy;
            if (rect_size.x == 0 || rect_size.y == 0)
                return 0;
            m.tx = (float)sx;
            m.ty = (float)sy;
            cvd.path_offset.x = sx;
            cvd.path_offset.y = sy;
            scale = (double)rect_size.x * rect_size.y * pdev->color_info.num_components /
                    pdev->MaxShadingBitmapSize;
            if (scale > 1) {
                /* This section (together with the call to 'path_scale' below)
                   sets up a downscaling when converting the shading into bitmap.
                   We used floating point numbers to debug it, but in production
                   we prefer to deal only with integers being powers of 2
                   in order to avoid possible distorsions when scaling paths.
                */
                log2_scale_x = log2_scale_y = ilog2((int)ceil(sqrt(scale)));
                if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale)
                    log2_scale_y++;
                if ((double)(1 << log2_scale_x) * (1 << log2_scale_y) < scale)
                    log2_scale_x++;
                scalex = (double)(1 << log2_scale_x);
                scaley = (double)(1 << log2_scale_y);
                rect_size.x = (int)floor(rect_size.x / scalex + 0.5);
                rect_size.y = (int)floor(rect_size.y / scaley + 0.5);
                gs_make_scaling(1.0 / scalex, 1.0 / scaley, &ms);
                gs_make_scaling(scalex, scaley, &msi);
                gs_matrix_multiply(&msi, &m, &m);
                gs_matrix_multiply(&ctm_only(pis), &ms, &mm);
                gs_setmatrix((gs_state *)pis, &mm);
                gs_matrix_multiply(&ctm_only((gs_imager_state *)pgs), &ms, &mm);
                gs_setmatrix((gs_state *)pgs, &mm);
                sx = fixed2int(bbox.p.x / (int)scalex);
                sy = fixed2int(bbox.p.y / (int)scaley);
                cvd.path_offset.x = sx; /* m.tx / scalex */
                cvd.path_offset.y = sy;
            }
            code = pdf_setup_masked_image_converter(pdev, pdev->memory, &m, &pcvd, need_mask, sx, sy,
                            rect_size.x, rect_size.y, false);
            pcvd->has_background = gx_dc_pattern2_has_background(pdcolor);
            stream_puts(pdev->strm, "q\n");
            if (code >= 0) {
                code = gdev_vector_dopath((gx_device_vector *)pdev, ppath,
                                        gx_path_type_clip, NULL);
                if (code >= 0)
                    stream_puts(pdev->strm, "W n\n");
            }
            pdf_put_matrix(pdev, NULL, &cvd.m, " cm q\n");
            cvd.write_matrix = false;
            if (code >= 0)
                code = gs_shading_do_fill_rectangle(pi.template.Shading,
                     NULL, (gx_device *)&cvd.mdev, (gs_imager_state *)pgs, !pi.shfill);
            if (code >= 0)
                code = pdf_dump_converted_image(pdev, &cvd);
            stream_puts(pdev->strm, "Q Q\n");
            pdf_remove_masked_image_converter(pdev, &cvd, need_mask);
            gs_setmatrix((gs_state *)pis, &save_ctm);
            gs_state_free(pgs);
            return code;
        }
    }
    if (code < 0)
        return code;
    {
        stream *s = pdev->strm;
        double scale;
        gs_matrix smat;
        gs_matrix *psmat = NULL;

        if (pcpath) {
            rect_intersect(box1, box);
            if (box1.p.x > box1.q.x || box1.p.y > box1.q.y)
                return 0;		/* outside the clipping path */
        }
        if (params->flatness != pdev->state.flatness) {
            pprintg1(s, "%g i\n", params->flatness);
            pdev->state.flatness = params->flatness;
        }
        if (make_rect_scaling(pdev, &box1, 1.0, &scale)) {
            gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale,
                            &smat);
            pdf_put_matrix(pdev, "q ", &smat, "cm\n");
            psmat = &smat;
        }
        gdev_vector_dopath((gx_device_vector *)pdev, ppath,
                           gx_path_type_fill | gx_path_type_optimize,
                           psmat);
        stream_puts(s, (params->rule < 0 ? "f\n" : "f*\n"));
        if (psmat)
            stream_puts(s, "Q\n");
    }
    return 0;
}

/* Stroke a path. */
int
gdev_pdf_stroke_path(gx_device * dev, const gs_imager_state * pis,
                     gx_path * ppath, const gx_stroke_params * params,
              const gx_drawing_color * pdcolor, const gx_clip_path * pcpath)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    stream *s;
    int code;
    double scale, path_scale;
    bool set_ctm;
    gs_matrix mat;
    double prescale = 1;
    gs_fixed_rect bbox;

    if (gx_path_is_void(ppath))
        return 0;		/* won't mark the page */
    if (pdf_must_put_clip_path(pdev, pcpath))
        code = pdf_unclip(pdev);
    else if ((pdev->last_charpath_op & TEXT_DO_FALSE_CHARPATH) && ppath->current_subpath &&
        (ppath->last_charpath_segment == ppath->current_subpath->last)) {
        bool hl_color = pdf_can_handle_hl_color((gx_device_vector *)pdev, pis, pdcolor);
        const gs_imager_state *pis_for_hl_color = (hl_color ? pis : NULL);

        if (pdf_modify_text_render_mode(pdev->text->text_state, 1)) {
            /* Set the colour for the stroke */
            code = pdf_reset_color(pdev, pis_for_hl_color, pdcolor, &pdev->saved_stroke_color,
                        &pdev->stroke_used_process_color, &psdf_set_stroke_color_commands);
            if (code == 0) {
                s = pdev->strm;
                /* Text is emitted scaled so that the CTM is an identity matrix, the line width
                 * needs to be scaled to match otherwise we will get the default, or the current
                 * width scaled by the CTM before the text, either of which would be wrong.
                 */
                scale = 72 / pdev->HWResolution[0];
                scale *= pis->ctm.xx;
                pprintg1(s, "%g w\n", (pis->line_params.half_width * 2) * (float)scale);
                /* Some trickery here. We have altered the colour, text render mode and linewidth,
                 * we don't want those to persist. By switching to a stream context we will flush the
                 * pending text. This has the beneficial side effect of executing a grestore. So
                 * everything works out neatly.
                 */
                code = pdf_open_page(pdev, PDF_IN_STREAM);
                return(code);
            }
        }
        /* Can only get here if any of the above steps fail, in which case we proceed to
         * emit the charpath as a normal path, and stroke it.
         */
        code = pdf_open_page(pdev, PDF_IN_STREAM);
    } else
        code = pdf_open_page(pdev, PDF_IN_STREAM);
    if (code < 0)
        return code;
    code = pdf_prepare_stroke(pdev, pis);
    if (code == gs_error_rangecheck) {
        /* Fallback to the default implermentation for handling
           a transparency with CompatibilityLevel<=1.3 . */
        return gx_default_stroke_path((gx_device *)dev, pis, ppath, params, pdcolor, pcpath);
    }
    if (code < 0)
        return code;
    code = pdf_put_clip_path(pdev, pcpath);
    if (code < 0)
        return code;
    /*
     * If the CTM is not uniform, stroke width depends on angle.
     * We'd like to avoid resetting the CTM, so we check for uniform
     * CTMs explicitly.  Note that in PDF, unlike PostScript, it is
     * the CTM at the time of the stroke operation, not the CTM at
     * the time the path was constructed, that is used for transforming
     * the points of the path; so if we have to reset the CTM, we must
     * do it before constructing the path, and inverse-transform all
     * the coordinates.
     */
    set_ctm = (bool)gdev_vector_stroke_scaling((gx_device_vector *)pdev,
                                               pis, &scale, &mat);
    if (set_ctm && ((pis->ctm.xx == 0 && pis->ctm.xy == 0) ||
                    (pis->ctm.yx == 0 && pis->ctm.yy == 0))) {
        /* Acrobat Reader 5 and Adobe Reader 6 issues
           the "Wrong operand type" error with matrices, which have 3 zero coefs.
           Besides that, we found that Acrobat Reader 4, Acrobat Reader 5
           and Adobe Reader 6 all store the current path in user space
           and apply CTM in the time of stroking - See the bug 687901.
           Therefore a precise conversion of Postscript to PDF isn't possible in this case.
           Adobe viewers render a line with a constant width instead.
           At last, with set_ctm == true we need the inverse matrix in
           gdev_vector_dopath. Therefore we exclude projection matrices
           (see bug 688363). */
        set_ctm = false;
        scale = fabs(pis->ctm.xx + pis->ctm.xy + pis->ctm.yx + pis->ctm.yy) /* Using the non-zero coeff. */
                / sqrt(2); /* Empirically from Adobe. */
    }
    if (set_ctm) {
        /*
         * We want a scaling factor that will bring the largest reasonable
         * user coordinate within bounds.  We choose a factor based on the
         * minor axis of the transformation.  Thanks to Raph Levien for
         * the following formula.
         */
        double a = mat.xx, b = mat.xy, c = mat.yx, d = mat.yy;
        double u = fabs(a * d - b * c);
        double v = a * a + b * b + c * c + d * d;
        double minor = (sqrt(v + 2 * u) - sqrt(v - 2 * u)) * 0.5;

        prescale = (minor == 0 || minor > 1 ? 1 : 1 / minor);
    }
    gx_path_bbox(ppath, &bbox);
    {
        /* Check whether a painting appears inside the clipping box.
           Doing so after writing the clipping path due to /SP pdfmark
           uses a special hack with painting outside the clipping box
           for synchronizing the clipping path (see lib/gs_pdfwr.ps).
           That hack appeared because there is no way to pass
           the imager state through gdev_pdf_put_params,
           which pdfmark is implemented with.
        */
        gs_fixed_rect clip_box, stroke_bbox = bbox;
        gs_point d0, d1;
        gs_fixed_point p0, p1;
        fixed bbox_expansion_x, bbox_expansion_y;

        gs_distance_transform(pis->line_params.half_width, 0, &ctm_only(pis), &d0);
        gs_distance_transform(0, pis->line_params.half_width, &ctm_only(pis), &d1);
        p0.x = float2fixed(any_abs(d0.x));
        p0.y = float2fixed(any_abs(d0.y));
        p1.x = float2fixed(any_abs(d1.x));
        p1.y = float2fixed(any_abs(d1.y));
        bbox_expansion_x = max(p0.x, p1.x) + fixed_1 * 2;
        bbox_expansion_y = max(p0.y, p1.y) + fixed_1 * 2;
        stroke_bbox.p.x -= bbox_expansion_x;
        stroke_bbox.p.y -= bbox_expansion_y;
        stroke_bbox.q.x += bbox_expansion_x;
        stroke_bbox.q.y += bbox_expansion_y;
        gx_cpath_outer_box(pcpath, &clip_box);
        rect_intersect(stroke_bbox, clip_box);
        if (stroke_bbox.q.x < stroke_bbox.p.x || stroke_bbox.q.y < stroke_bbox.p.y)
            return 0;
    }
    if (make_rect_scaling(pdev, &bbox, prescale, &path_scale)) {
        scale /= path_scale;
        if (set_ctm)
            gs_matrix_scale(&mat, path_scale, path_scale, &mat);
        else {
            gs_make_scaling(path_scale, path_scale, &mat);
            set_ctm = true;
        }
    }
    code = gdev_vector_prepare_stroke((gx_device_vector *)pdev, pis, params,
                                      pdcolor, scale);
    if (code < 0)
        return gx_default_stroke_path(dev, pis, ppath, params, pdcolor,
                                      pcpath);
    if (!pdev->HaveStrokeColor)
        pdev->saved_fill_color = pdev->saved_stroke_color;
    if (set_ctm)
        pdf_put_matrix(pdev, "q ", &mat, "cm\n");
    code = gdev_vector_dopath((gx_device_vector *)pdev, ppath,
                              gx_path_type_stroke | gx_path_type_optimize,
                              (set_ctm ? &mat : (const gs_matrix *)0));
    if (code < 0)
        return code;
    s = pdev->strm;
    stream_puts(s, (code ? "s" : "S"));
    stream_puts(s, (set_ctm ? " Q\n" : "\n"));
    return 0;
}

/*
   The fill_rectangle_hl_color device method.
   See gxdevcli.h about return codes.
 */
int
gdev_pdf_fill_rectangle_hl_color(gx_device *dev, const gs_fixed_rect *rect,
    const gs_imager_state *pis, const gx_drawing_color *pdcolor,
    const gx_clip_path *pcpath)
{
    int code;
    gs_fixed_rect box1 = *rect, box = box1;
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    double scale;
    gs_matrix smat;
    gs_matrix *psmat = NULL;
    const bool convert_to_image = (pdev->CompatibilityLevel <= 1.2 &&
            gx_dc_is_pattern2_color(pdcolor));

    if (rect->p.x == rect->q.x)
        return 0;
    if (!convert_to_image) {
        code = prepare_fill_with_clip(pdev, pis, &box, true, pdcolor, pcpath);
        if (code < 0)
            return code;
        if (code == 1)
            return 0; /* Nothing to paint. */
        code = pdf_setfillcolor((gx_device_vector *)pdev, pis, pdcolor);
        if (code < 0)
            return code;
        if (pcpath)
            rect_intersect(box1, box);
        if (box1.p.x > box1.q.x || box1.p.y > box1.q.y)
            return 0;		/* outside the clipping path */
        if (make_rect_scaling(pdev, &box1, 1.0, &scale)) {
            gs_make_scaling(pdev->scale.x * scale, pdev->scale.y * scale, &smat);
            pdf_put_matrix(pdev, "q ", &smat, "cm\n");
            psmat = &smat;
        }
        pprintg4(pdev->strm, "%g %g %g %g re f\n",
                fixed2float(box1.p.x) / scale, fixed2float(box1.p.y) / scale,
                fixed2float(box1.q.x - box1.p.x) / scale, fixed2float(box1.q.y - box1.p.y) / scale);
        if (psmat)
            stream_puts(pdev->strm, "Q\n");
        return 0;
    } else {
        gx_fill_params params;
        gx_path path;

        params.rule = 1; /* Not important because the path is a rectange. */
        params.adjust.x = params.adjust.y = 0;
        params.flatness = pis->flatness;
        gx_path_init_local(&path, pis->memory);
        code = gx_path_add_rectangle(&path, rect->p.x, rect->p.y, rect->q.x, rect->q.y);
        if (code < 0)
            return code;
        code = gdev_pdf_fill_path(dev, pis, &path, &params, pdcolor, pcpath);
        if (code < 0)
            return code;
        gx_path_free(&path, "gdev_pdf_fill_rectangle_hl_color");
        return code;

    }
}

int
gdev_pdf_fillpage(gx_device *dev, gs_imager_state * pis, gx_device_color *pdevc)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0);

    if (gx_dc_pure_color(pdevc) == pdev->white && !is_in_page(pdev) && pdev->sbstack_depth <= bottom) {
        /* PDF doesn't need to erase the page if its plain white */
        return 0;
    }
    else
        return gx_default_fillpage(dev, pis, pdevc);
}