summaryrefslogtreecommitdiff
path: root/pcl/pcpage.c
blob: ee21809981b09d4a63d811abad508bf72a5a2a77 (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
/* Copyright (C) 2001-2012 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.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


/* pcpage.c - PCL5 page and transformation control commands */

#include "math_.h"
#include "std.h"
#include "pcommand.h"
#include "pcstate.h"
#include "pcdraw.h"
#include "pcparam.h"
#include "pcparse.h"            /* for pcl_execute_macro */
#include "pcfont.h"             /* for underline interface */
#include "pcpatxfm.h"
#include "pcursor.h"
#include "pcpage.h"
#include "pgmand.h"
#include "pginit.h"
#include "plmain.h"             /* for finish page */
#include "plvalue.h"
#include "gsmatrix.h"           /* for gsdevice.h */
#include "gsnogc.h"
#include "gscoord.h"
#include "gsdevice.h"
#include "gspaint.h"
#include "gspath.h"
#include "gxdevice.h"
#include "pjtop.h"
#include "rtgmode.h"
#include <stdlib.h>             /* for atol() */

/*
 * The PCL printable region. HP always sets the boundary of this region to be
 * 1/6" in from the edge of the paper, irrespecitve of the paper size or the
 * device. Since this dimension affects some default values related to rasters,
 * it is important that it be assigned this value.
 */
#define PRINTABLE_MARGIN_CP inch2coord(0.0)

#define round(x)    (((x) < 0.0) ? (ceil ((x) - 0.5)) : (floor ((x) + 0.5)))

/* Procedures */

/*
 * Preserve the current point and text margin set by transfroming them into
 * logical page space.
 */
static void
preserve_cap_and_margins(const pcl_state_t * pcs,
                         gs_point * pcur_pt, gs_rect * ptext_rect)
{
    pcur_pt->x = (double)pcs->cap.x;
    pcur_pt->y = (double)pcs->cap.y;
    gs_point_transform(pcur_pt->x,
                       pcur_pt->y, &(pcs->xfm_state.pd2lp_mtx), pcur_pt);
    ptext_rect->p.x = (double)pcs->margins.left;
    ptext_rect->p.y = (double)pcs->margins.top;
    ptext_rect->q.x = (double)pcs->margins.right;
    ptext_rect->q.y = (double)(pcs->margins.top + pcs->margins.length);
    pcl_transform_rect(pcs->memory, ptext_rect, ptext_rect,
                       &(pcs->xfm_state.pd2lp_mtx));
}

/*
 * Convert the current point and text margin set back into "pseudo print
 * direction" space.
 */
static void
restore_cap_and_margins(pcl_state_t * pcs,
                        const gs_point * pcur_pt, const gs_rect * ptext_rect)
{
    gs_matrix lp2pd;
    gs_point tmp_pt;
    gs_rect tmp_rect;

    pcl_invert_mtx(&(pcs->xfm_state.pd2lp_mtx), &lp2pd);
    gs_point_transform(pcur_pt->x, pcur_pt->y, &lp2pd, &tmp_pt);
    pcs->cap.x = (coord) tmp_pt.x;
    pcs->cap.y = (coord) tmp_pt.y;
    pcl_transform_rect(pcs->memory, ptext_rect, &tmp_rect, &lp2pd);
    pcs->margins.left = (coord) tmp_rect.p.x;
    pcs->margins.top = (coord) tmp_rect.p.y;
    pcs->margins.right = (coord) tmp_rect.q.x;
    pcs->margins.length = (coord) tmp_rect.q.y - pcs->margins.top;
}

/*
 * Update the transformations stored in the PCL state. This will also update
 * the device clipping region information in device and logical page space.
 * The text region margins are preserved.
 *
 * This routine should be called for:
 *
 *     changes in the paper size
 *     transition from page front to page back for duplexing
 *         (this facility is not currently implemented)
 *     change of left or top offset registration
 *     change of logical page orientation
 *     change of print direction
 *
 * The paper size, left/top offsets, logical page orientation, and print
 * direction should be set before this procedure is called.
 */
static void
update_xfm_state(pcl_state_t * pcs, bool reset_initial)
{
    pcl_xfm_state_t *pxfmst = &(pcs->xfm_state);
    const pcl_paper_size_t *psize = pxfmst->paper_size;
    coord offset;
    gs_matrix pg2dev, pg2lp;
    gs_rect print_rect, dev_rect, text_rect;
    gs_point cur_pt;
    double loff = pxfmst->left_offset_cp;
    double toff = pxfmst->top_offset_cp;

    /* preserve the current point and text rectangle in logical page space */
    if (!reset_initial)
        preserve_cap_and_margins(pcs, &cur_pt, &text_rect);

    /* get the page to device transformation */
    gs_defaultmatrix(pcs->pgs, &pg2dev);

    /*
     * Get the logical to page space transformation, and the dimensions of the
     * logical page.
     *
     * NOT YET IMPLEMENT - if on back of a duplex page, change size of offsets
     *
     * if (duplex_back(pcs)) {
     *    loff = -loff;
     *    toff = -toff;
     * }
     */
    {
        gx_device *pdev = gs_currentdevice(pcs->pgs);

        /* get the LeadingEdge from the device
         * This has probably no influence on the resulting calculations,
         * as the offsetregistration is only influenced by pageside & bindingedge
         * (not feededge) */
        if ((pdev->LeadingEdge & 1) == 1) {  /* longedgefeed */
            if (pcs->back_side) {
                if (pcs->bind_short_edge) {
                    toff = -toff;
                } else {  /* bind_long_edge */
                    loff = -loff;
                }
            } else {
                /* do not change LOFF */
                /* TOFF ? */
            }
        } else {  /* shortedgefeed */
            if (pcs->back_side) {
                if (pcs->bind_short_edge) {
                    toff = -toff;
                } else {  /* bind_long_edge */
                    loff = -loff;
                }
            } else {
                /* do nothing */
            }
        }
    }

    pcl_make_rotation(pxfmst->lp_orient,
                      (double) (psize->width),
                      (double) (psize->height), &(pxfmst->lp2pg_mtx)
        );
    pxfmst->lp2pg_mtx.tx += loff;
    pxfmst->lp2pg_mtx.ty += toff;
    if (pcs->personality == rtl)
        offset = 0;
    else
        offset = ((pxfmst->lp_orient & 0x1) != 0 ? psize->offset_landscape
                  : psize->offset_portrait);

    /* we need an extra 1/10 inch on each side to support 80
       characters vs. 78 at 10 cpi.  Only apply to A4. */
    if ((pcs->wide_a4) && (psize->width == 59520) && (psize->height == 84168))
        offset -= inch2coord(1.0 / 10.0);

    gs_matrix_translate(&(pxfmst->lp2pg_mtx),
                        (double) offset, 0.0, &(pxfmst->lp2pg_mtx)
        );
    if ((pxfmst->lp_orient & 0x1) != 0) {
        pxfmst->lp_size.x = psize->height - 2 * offset;
        pxfmst->lp_size.y = psize->width;
    } else {
        pxfmst->lp_size.x = psize->width - 2 * offset;
        pxfmst->lp_size.y = psize->height;
    }

    /* then the logical page to device transformation */
    gs_matrix_multiply(&(pxfmst->lp2pg_mtx), &pg2dev, &(pxfmst->lp2dev_mtx));
    pg2dev.ty = round(pg2dev.ty);
    pg2dev.tx = round(pg2dev.tx);
    pxfmst->lp2dev_mtx.tx = round(pxfmst->lp2dev_mtx.tx);
    pxfmst->lp2dev_mtx.ty = round(pxfmst->lp2dev_mtx.ty);
    /* the "pseudo page direction to logical page/device transformations */
    pcl_make_rotation(pxfmst->print_dir,
                      (double) pxfmst->lp_size.x,
                      (double) pxfmst->lp_size.y, &(pxfmst->pd2lp_mtx)
        );
    gs_matrix_multiply(&(pxfmst->pd2lp_mtx),
                       &(pxfmst->lp2dev_mtx), &(pxfmst->pd2dev_mtx)
        );

    /* calculate the print direction page size */
    if ((pxfmst->print_dir & 0x1) || (pcs->personality == rtl)) {
        pxfmst->pd_size.x = pxfmst->lp_size.y;
        pxfmst->pd_size.y = pxfmst->lp_size.x;
    } else
        pxfmst->pd_size = pxfmst->lp_size;

    {
        gx_device *pdev = gs_currentdevice(pcs->pgs);

        /* We must not set up a clipping region beyond the hardware
           margins of the device, but the pcl language definition
           requires hardware margins to be 1/6".  We set all margins
           to the the maximum of the PCL language defined 1/6" and the
           actual hardware margin.  If 1/6" is not available pcl will
           not work correctly all of the time.  The HPGL-2/RTL mode
           does not use margins. */
        if (pcs->personality == rtl) {
            print_rect.p.x = inch2coord(pdev->HWMargins[1]) / 72.0;
            print_rect.p.y = inch2coord(pdev->HWMargins[0] / 72.0);
            print_rect.q.x =
                psize->height - inch2coord(pdev->HWMargins[3] / 72.0);
            print_rect.q.y =
                psize->width - inch2coord(pdev->HWMargins[2] / 72.0);
        } else {
            print_rect.p.x =
                max(PRINTABLE_MARGIN_CP,
                    inch2coord(pdev->HWMargins[0] / 72.0));
            print_rect.p.y =
                max(PRINTABLE_MARGIN_CP,
                    inch2coord(pdev->HWMargins[1]) / 72.0);
            print_rect.q.x =
                psize->width - max(PRINTABLE_MARGIN_CP,
                                   inch2coord(pdev->HWMargins[2] / 72.0));
            print_rect.q.y =
                psize->height - max(PRINTABLE_MARGIN_CP,
                                    inch2coord(pdev->HWMargins[3] / 72.0));
        }
        pcl_transform_rect(pcs->memory, &print_rect, &dev_rect, &pg2dev);
    }
    pcl_invert_mtx(&(pxfmst->lp2pg_mtx), &pg2lp);
    pcl_transform_rect(pcs->memory, &print_rect, &(pxfmst->lp_print_rect),
                       &pg2lp);

    /* restablish the current point and text region */
    if (!reset_initial)
        restore_cap_and_margins(pcs, &cur_pt, &text_rect);

    /*
     * No need to worry about pat_orient or pat_ref_pt; these will always
     * be recalculated just prior to use.
     */
}

/* default margins, relative to the logical page boundaries */
#define DFLT_TOP_MARGIN     (pcs->personality == rtl ? 0 : inch2coord(0.5))
#define DFLT_LEFT_MARGIN    inch2coord(0.0)
#define DFLT_RIGHT_MARGIN   inch2coord(0.0)
#define DFLT_BOTTOM_MARGIN  (pcs->personality == rtl ? 0 : inch2coord(0.5))

#define DFLT_TOP_MARGIN_PASSTHROUGH inch2coord(1.0/6.0)
#define DFLT_BOTOM_MARGIN_PASSTHROUGH inch2coord(1.0/6.0)

#define TOP_MARGIN(hgt, tmarg)  ((hgt) > (tmarg) ? (tmarg) : 0)
#define PAGE_LENGTH(hgt, bmarg) ((hgt) > (bmarg) ? (hgt) - (bmarg) : (hgt))

/*
 * Reset the top margin an text length.
 *
 * Note that, even though changing the print direction merely relabels (but does
 * not relocate) the margins, the preint direction does change the location of
 * the default margins.
 */
static void
reset_vertical_margins(pcl_state_t * pcs, bool for_passthrough)
{
    pcl_margins_t *pmar = &(pcs->margins);
    coord hgt = pcs->xfm_state.pd_size.y;
    coord tm = (for_passthrough ?
                DFLT_TOP_MARGIN_PASSTHROUGH : DFLT_TOP_MARGIN);
    coord bm = (for_passthrough ?
                DFLT_BOTOM_MARGIN_PASSTHROUGH : DFLT_BOTTOM_MARGIN);
    pmar->top = TOP_MARGIN(hgt, tm);
    pmar->length = PAGE_LENGTH(hgt - pmar->top, bm);
}

/*
 * Reset horizontal margins
 *
 * Note that, even though changing the print direction merely relabels (but does
 * not relocate) the margins, the preint direction does change the location of
 * the default margins.
 */
static void
reset_horizontal_margins(pcl_state_t * pcs)
{
    pcl_margins_t *pmar = &(pcs->margins);

    pmar->left = DFLT_LEFT_MARGIN;
    pmar->right = pcs->xfm_state.pd_size.x - DFLT_RIGHT_MARGIN;
}

/*
 * Reset both the horizontal and vertical margins
 */
static void
reset_margins(pcl_state_t * pcs, bool for_passthrough)
{
    reset_horizontal_margins(pcs);
    reset_vertical_margins(pcs, for_passthrough);
}

/*
 * Reset all parameters which must be reset whenever the page size changes.
 *
 * The third operand indicates if this routine is being called as part of
 * an initial reset. In that case, done't call HPGL's reset - the reset
 * will do that later.
 */
static void
new_page_size(pcl_state_t * pcs,
              const pcl_paper_size_t * psize,
              bool reset_initial, bool for_passthrough)
{
    double width_pts = psize->width * 0.01;
    double height_pts = psize->height * 0.01;
    float page_size[2];
    float old_page_size[2];
    gs_state *pgs = pcs->pgs;
    gs_matrix mat;
    bool changed_page_size;

    page_size[0] = width_pts;
    page_size[1] = height_pts;

    old_page_size[0] = gs_currentdevice(pcs->pgs)->MediaSize[0];
    old_page_size[1] = gs_currentdevice(pcs->pgs)->MediaSize[1];

    put_param1_float_array(pcs, "PageSize", page_size);

    /*
     * Reset the default transformation.
     *
     * The graphic library provides a coordinate system in points, with the
     * origin at the lower left corner of the page. The PCL code uses a
     * coordinate system in centi-points, with the origin at the upper left
     * corner of the page.
     */
    gs_setdefaultmatrix(pgs, NULL);
    gs_initmatrix(pgs);
    gs_currentmatrix(pgs, &mat);

    if (pcs->personality != rtl) {
        gs_matrix_translate(&mat, 0.0, height_pts, &mat);
        gs_matrix_scale(&mat, 0.01, -0.01, &mat);
    } else {
        gs_matrix_rotate(&mat, -90, &mat);
        gs_matrix_scale(&mat, -0.01, 0.01, &mat);
    }

    gs_setdefaultmatrix(pgs, &mat);
    pcs->xfm_state.paper_size = psize;
    pcs->overlay_enabled = false;
    update_xfm_state(pcs, reset_initial);
    reset_margins(pcs, for_passthrough);

    /* check if update_xfm_state changed the page size */
    changed_page_size = !((int)old_page_size[0] == pcs->xfm_state.paper_size->width/100 && 
                          (int)old_page_size[1] == pcs->xfm_state.paper_size->height/100);

    /*
     * make sure underlining is disabled (homing the cursor may cause
     * an underline to be put out.
     */
    pcs->underline_enabled = false;
    pcl_home_cursor(pcs);
    /*
     * this is were we initialized the cursor position
     */
    pcs->cursor_moved = false;

    pcl_xfm_reset_pcl_pat_ref_pt(pcs);

    if (!reset_initial)
        hpgl_do_reset(pcs, pcl_reset_page_params);

    if (pcs->end_page == pcl_end_page_top) {    /* don't erase in snippet mode */
        if (pcs->page_marked || changed_page_size) {
            gs_erasepage(pcs->pgs);
            pcs->page_marked = false;
        }
    }
}

/*
 * Define the known paper sizes.
 *
 * The values are taken from the H-P manual and are in 1/300" units,
 * but the structure values are in centipoints (1/7200").
 */
#define p_size(t, n, w, h, offp, offl)                                  \
    { (t), (n), { (w) * 24L, (h) * 24L, (offp) * 24L, (offl) * 24L } }

pcl_paper_type_t paper_types_proto[] = {
    p_size(1, "executive", 2175, 3150, 75, 60),
    p_size(2, "letter", 2550, 3300, 75, 60),
    p_size(3, "legal", 2550, 4200, 75, 60),
    p_size(6, "ledger", 3300, 5100, 75, 60),
    p_size(26, "a4", 2480, 3507, 71, 59),
    p_size(27, "a3", 3507, 4960, 71, 59),
    p_size(78, "index_3x5", 900, 1500, 75, 60),
    p_size(80, "monarch", 1162, 2250, 75, 60),
    p_size(81, "com_10", 1237, 2850, 75, 60),
    p_size(90, "dl", 1299, 2598, 71, 59),
    p_size(91, "c5", 1913, 2704, 71, 59),
    p_size(100, "b5", 2078, 2952, 71, 59),
    /* initial values for custom: copied from default(letter) */
    /* offset_portrait/offset_landscape ? */
    p_size(101, "custom", 2550, 3300, 75, 60)
};

const int pcl_paper_type_count = countof(paper_types_proto);

/*
 * Reset all parameters which must be reset whenever the logical page
 * orientation changes.
 *
 * The last operand indicates if this routine is being called as part of
 * an initial resete.
 */
void
new_logical_page(pcl_state_t * pcs,
                 int lp_orient,
                 const pcl_paper_size_t * psize,
                 bool reset_initial, bool for_passthrough)
{
    pcl_xfm_state_t *pxfmst = &(pcs->xfm_state);

    pxfmst->lp_orient = lp_orient;
    pxfmst->print_dir = 0;
    new_page_size(pcs, psize, reset_initial, for_passthrough);
}


#define PAPER_SIZES pcs->ppaper_type_table

int
pcl_new_logical_page_for_passthrough(pcl_state_t * pcs, int orient,
                                     gs_point * pdims)
{
    int i;
    pcl_paper_size_t *psize;
    /* points to centipoints */
    coord cp_width = (coord) (pdims->x * 100 + 0.5);
    coord cp_height = (coord) (pdims->y * 100 + 0.5);
    bool found = false;

    for (i = 0; i < pcl_paper_type_count; i++) {
        psize = &(PAPER_SIZES[i].psize);
        if (psize->width == cp_width && psize->height == cp_height) {
            found = true;
            break;
        }
    }

    if (!found)
        return -1;
    new_logical_page(pcs, orient, psize, false, true);
    return 0;
}

/* page marking routines */


void
pcl_mark_page_for_current_pos(pcl_state_t * pcs)
{
    /* nothing to do */
    if (pcs->page_marked)
        return;

    /* convert current point to device space and check if it is inside
       device rectangle for the page */
    {
        gs_fixed_rect page_box;
        gs_fixed_point pt;
        int code;

        code = gx_default_clip_box(pcs->pgs, &page_box);
        if (code < 0)
            /* shouldn't happen. */
            return;
        
        code = gx_path_current_point(gx_current_path(pcs->pgs), &pt);
        if (code < 0)
            /* shouldn't happen */
            return;
        

        /* half-open lower - not sure this is correct */
        if (pt.x >= page_box.p.x && pt.y >= page_box.p.y &&
            pt.x < page_box.q.x && pt.y < page_box.q.y)
            pcs->page_marked = true;
    }
}

/* 
 *  Just use the current position to see if we marked the page.  This
 *  could be improved by using the bounding box of the path object but
 *  for page marking that case does not seem to come up in practice.
 */
void
pcl_mark_page_for_path(pcl_state_t * pcs)
{

    pcl_mark_page_for_current_pos(pcs);
    return;
}

/* returns the bounding box coordinates for the current device and a
   boolean to indicate marked status. 0 - unmarked 1 - marked -1 error */
int
pcl_page_marked(pcl_state_t * pcs)
{
    return pcs->page_marked;
}

int
pcl_cursor_moved(pcl_state_t * pcs)
{
    return pcs->cursor_moved;
}

/*
 * End a page, either unconditionally or only if there are marks on it.
 * Return 1 if the page was actually printed and erased.
 */
int
pcl_end_page(pcl_state_t * pcs, pcl_print_condition_t condition)
{
    int code = 0;

    pcl_break_underline(pcs);   /* (could mark page) */

    /* If we are conditionally printing (normal case) check if the
       page is marked */
    if (condition != pcl_print_always) {
        if (!pcl_page_marked(pcs))
            return 0;
    }

    /* finish up graphics mode in case we finished the page in the
       middle of a raster stream */
    if (pcs->raster_state.graphics_mode)
        pcl_end_graphics_mode(pcs);

    /* If there's an overlay macro, execute it now. */
    if (pcs->overlay_enabled) {
        void *value;

        if (pl_dict_find(&pcs->macros,
                         id_key(pcs->overlay_macro_id), 2, &value)) {
            pcs->overlay_enabled = false;   /**** IN reset_overlay ****/
            code = pcl_execute_macro((const pcl_macro_t *)value,
                                     pcs,
                                     pcl_copy_before_overlay,
                                     pcl_reset_overlay,
                                     pcl_copy_after_overlay);
            if (code < 0)
                return code;

            pcs->overlay_enabled = true; /**** IN copy_after ****/
        }
    }
    /* output the page */
    code = (*pcs->end_page) (pcs, pcs->num_copies, true);
    if (code < 0)
        return code;

    if (pcs->end_page == pcl_end_page_top)
        code = gs_erasepage(pcs->pgs);

    pcs->page_marked = false;

    /*
     * Advance of a page may move from a page front to a page back. This may
     * change the applicable transformations.
     */
    /*
     * Keep track of the side you are on
     */
    if (pcs->duplex) {
        pcs->back_side = ! pcs->back_side;
    } else {
        pcs->back_side = false;
    }
    put_param1_bool(pcs,"FirstSide", !pcs->back_side);
    update_xfm_state(pcs, 0);

    pcl_continue_underline(pcs);
    return (code < 0 ? code : 1);
}

/* Commands */

/*
 * ESC & l <psize_enum> A
 *
 * Select paper size
 */
static int
set_page_size(pcl_args_t * pargs, pcl_state_t * pcs)
{
    /* Note: not all values are implemented on all printers.  If -g
       has been given on the command line we use the "Custom" page
       tag, the height and width are set to the device media values
       which were filled in when the -g option was processed. */

    uint tag = (pcs->page_set_on_command_line ? 101 : uint_arg(pargs));
    int i;
    int code = 0;
    const pcl_paper_size_t *psize = 0;

    /* oddly the command goes to the next page irrespective of
       arguments */
    code = pcl_end_page_if_marked(pcs);
    if (code < 0)
        return code;
    pcl_home_cursor(pcs);

    for (i = 0; i < pcl_paper_type_count; i++) {
        if (tag == PAPER_SIZES[i].tag) {
            psize = &(PAPER_SIZES[i].psize);
            break;
        }
    }
    if ((psize != 0) && ((code = pcl_end_page_if_marked(pcs)) >= 0)) {
        pcs->xfm_state.print_dir = 0;
        new_page_size(pcs, psize, false, false);
    }
    return code;
}

/*
 * ESC & l <feed_enum> H
 *
 * Set paper source
 */
static int
set_paper_source(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint i = uint_arg(pargs);
    /* oddly the command goes to the next page irrespective of
       arguments */
    int code = pcl_end_page_if_marked(pcs);

    if (code < 0)
        return code;
    pcl_home_cursor(pcs);
    /* Do not change the page side if the wanted paper source is the same as the actual one */
    if (pcs->paper_source != i) {
        pcs->back_side = false;
        put_param1_bool(pcs, "FirstSide", !pcs->back_side);
    }
    pcs->paper_source = i;
    /* Note: not all printers support all possible values. */
    if (i <= 6) {
        code = 0;
        if (i > 0)
            code = put_param1_int(pcs, "%MediaSource", i);
        return (code < 0 ? code : 0);
    } else
        return e_Range;
}

/*
 * ESC & l <xoff_dp> U
 *
 * Note that this shifts the logical page, but does NOT change its size nor
 * does it reset any logical-page related parameters.
 */
static int
set_left_offset_registration(pcl_args_t * pargs, pcl_state_t * pcs)
{
    pcs->xfm_state.left_offset_cp = float_arg(pargs) * 10.0;
    update_xfm_state(pcs, 0);
    return 0;
}

/*
 * ESC & l <yoff_dp> Z
 *
 * Note that this shifts the logical page, but does NOT change its size nor
 * does it reset any logical-page related parameters.
 */
static int
set_top_offset_registration(pcl_args_t * pargs, pcl_state_t * pcs)
{
    pcs->xfm_state.top_offset_cp = float_arg(pargs) * 10;
    update_xfm_state(pcs, 0);
    return 0;
}

/*
 * ESC & l <orient> O
 *
 * Set logical page orientation.
 */
static int
set_logical_page_orientation(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint i = uint_arg(pargs);
    int code;
    /* the command is ignored if it is value is out of range */
    if (i > 3)
        return 0;

    /* this command is ignored in pcl xl snippet mode.  NB we need a
       better flag for snippet mode. */
    if (pcs->end_page != pcl_end_page_top) {
        return 0;
    }

    /* If orientation is same as before ignore the command */
    if (i == pcs->xfm_state.lp_orient) {
        return 0;
    }

    /* ok to execute - clear the page, set up the transformations and
       set the flag disabling the orientation command for this page. */
    code = pcl_end_page_if_marked(pcs);
    if (code >= 0) {
        new_logical_page(pcs, i, pcs->xfm_state.paper_size, false, false);
        pcs->hmi_cp = HMI_DEFAULT;
        pcs->vmi_cp = VMI_DEFAULT;
    }
    return code;
}

/*
 * ESC & a <angle> P
 */
static int
set_print_direction(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint i = uint_arg(pargs);

    if ((i <= 270) && (i % 90 == 0)) {
        i /= 90;
        if (i != pcs->xfm_state.print_dir) {
            pcl_break_underline(pcs);
            pcs->xfm_state.print_dir = i;
            update_xfm_state(pcs, 0);
            pcl_continue_underline(pcs);
        } else {
            pcs->xfm_state.print_dir = i;
            update_xfm_state(pcs, 0);
        }
    }
    return 0;
}

/*
 * ESC & a <col> L
 *
 * Set left margin.
 */
static int
set_left_margin(pcl_args_t * pargs, pcl_state_t * pcs)
{
    coord lmarg = uint_arg(pargs) * pcl_hmi(pcs);

    /* adjust underlining if the left margin passes to the right of
       the underline start position */
    if ((pcs->underline_enabled) && (lmarg > pcs->underline_start.x))
        pcs->underline_start.x = lmarg;

    if (lmarg < pcs->margins.right) {
        pcs->margins.left = lmarg;
        if (pcs->cap.x < lmarg)
            pcl_set_cap_x(pcs, lmarg, false, false);
    }
    return 0;
}

/*
 * ESC & a <col> M
 *
 * Set right margin. The right margin is set to the *right* edge of the
 * specified column, so we need to add 1 to the column number.
 */
static int
set_right_margin(pcl_args_t * pargs, pcl_state_t * pcs)
{
    coord rmarg = (uint_arg(pargs) + 1) * pcl_hmi(pcs);

    if (rmarg > pcs->xfm_state.pd_size.x)
        rmarg = pcs->xfm_state.pd_size.x;
    if (rmarg > pcs->margins.left) {
        pcs->margins.right = rmarg;
        if (pcs->cap.x > rmarg)
            pcl_set_cap_x(pcs, rmarg, false, false);
    }

    return 0;
}

/*
 * ESC 9
 *
 * Clear horizontal margins.
 */
static int
clear_horizontal_margins(pcl_args_t * pargs,    /* ignored */
                         pcl_state_t * pcs)
{
    reset_horizontal_margins(pcs);
    return 0;
}

/*
 * ESC & l <line> E
 *
 * Set top margin. This will also reset the page length.
 */
static int
set_top_margin(pcl_args_t * pargs, pcl_state_t * pcs)
{
    coord hgt = pcs->xfm_state.pd_size.y;
    coord tmarg = uint_arg(pargs) * pcs->vmi_cp;

    if ((pcs->vmi_cp != 0) && (tmarg <= hgt)) {
        pcs->margins.top = tmarg;
        pcs->margins.length = PAGE_LENGTH(hgt - tmarg, DFLT_BOTTOM_MARGIN);
        /* See the discussion in the Implementor's guide concerning
           "fixed" and "floating" cap.  If the page is not dirty we
           home the cursor - the guide talks about tracking the cursor
           for any movement and checking for data on the page */
        if (!pcl_page_marked(pcs) && !pcl_cursor_moved(pcs))
            return pcl_set_cap_y(pcs, 0L, false, false, true, false);
    }
    return 0;
}

/*
 * ESC & l <lines> F
 *
 * Set text length (which indirectly sets the bottom margin).
 */
static int
set_text_length(pcl_args_t * pargs, pcl_state_t * pcs)
{
    coord len = uint_arg(pargs) * pcs->vmi_cp;

    if (len == 0) {
        len =
            pcs->xfm_state.pd_size.y - pcs->margins.top -
            inch2coord(1.0 / 2.0);
        if (len < 0) {
            len += inch2coord(1.0 / 2.0);
        }
    }
    if ((len >= 0) && (pcs->margins.top + len <= pcs->xfm_state.pd_size.y))
        pcs->margins.length = len;
    return 0;
}

/*
 * ESC & l <enable> L
 *
 * Set perforation skip mode. Though performation skip is more closely related
 * to vertical motion than to margins, the command is included here because it
 * resets the vertical margins (top margin and text length) to their defaults.
 */
static int
set_perforation_skip(pcl_args_t * pargs, pcl_state_t * pcs)
{
    bool new_skip = uint_arg(pargs);

    if ((new_skip != pcs->perforation_skip) && (new_skip <= 1))
        pcs->perforation_skip = new_skip;
    return 0;
}

/*
 * (From PCL5 Comparison Guide, p. 1-98)
 *
 * ESC & l <type> M
 *
 * Set media type.
 */
static int
pcl_media_type(pcl_args_t * pargs, pcl_state_t * pcs)
{
    int type = uint_arg(pargs);

    if (type <= 4) {
        int code = pcl_end_page_if_marked(pcs);

        if (code >= 0)
            pcl_home_cursor(pcs);
        return (code < 0 ? code : e_Unimplemented);
    } else
        return e_Range;
}

/*
 * Logical Page Setup (From HP D640 Technical Manual)
 */

typedef struct pcl_logical_page_s
{
    byte LeftOffset[2];
    byte TopOffset[2];
    byte Orientation;
    byte Reserved;
    byte Width[2];
    byte Height[2];
} pcl_logical_page_t;

/*
 * ESC & a # W
 *
 * Set logical page.
 */

static int
set_logical_page(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint count = uint_arg(pargs);

    const pcl_logical_page_t *plogpage =
        (pcl_logical_page_t *) arg_data(pargs);
    pcl_paper_size_t *pcur_paper;

#ifdef DEBUG
    if (gs_debug_c('i')) {
        pcl_debug_dump_data(pcs->memory, arg_data(pargs), uint_arg(pargs));
    }
#endif

    /* the currently selected paper size */
    pcur_paper = (pcl_paper_size_t *) pcs->xfm_state.paper_size;

    /* the command can set width, height and offsets (10) or just
       offsets (4) */
    if (count != 10 && count != 4)
        return e_Range;

    if (count == 10) {
        pcur_paper->width = pl_get_uint16(plogpage->Width) * 10;
        pcur_paper->height = pl_get_uint16(plogpage->Height) * 10;
        if (pcur_paper->width == 0 || pcur_paper->height == 0)
            return e_Range;
    }

    pcur_paper->offset_portrait = pl_get_int16(plogpage->LeftOffset) * 10;
    pcur_paper->offset_landscape = pl_get_int16(plogpage->TopOffset) * 10;

    new_page_size(pcs, pcur_paper, false, false);
    gs_erasepage(pcs->pgs);
    pcs->page_marked = false;
    return 0;
}

/* 
 * Custom Paper Width/Length 
 * from Windows Driver "HP Universal Printing PCL 5" (.GPD files) and
 * http://www.office.xerox.com/support/dctips/dc10cc0471.pdf
 *
 * ESC & f <decipoints> I
 *
 */
static int
set_paper_width(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint decipoints = uint_arg(pargs);
    bool found = false;
    int i;
    pcl_paper_size_t *psize;

    for (i = 0; i < pcl_paper_type_count; i++) {
        if (101 == PAPER_SIZES[i].tag) {
            psize = &(PAPER_SIZES[i].psize);
            found = true;
            break;
        }
    }

    if (found)
        psize->width = decipoints * 10L;
    else
        gs_warn("Page table does not contain a custom entry");

    return 0;
}

int 
pcl_set_custom_paper_size(pcl_state_t *pcs, pcl_paper_size_t *p)
{
    pcl_paper_size_t *psize = NULL;
    bool found = false;
    int i;

    for (i = 0; i < pcl_paper_type_count; i++) {
        if (101 == PAPER_SIZES[i].tag) {
            psize = &(PAPER_SIZES[i].psize);
            found = true;
            break;
        }
    }
    if (found)
        *psize = *p;
    else
        /* this should never happen - the customer paper size is
           always in the table */
        return -1;
    
    new_logical_page(pcs, 0, psize, false, false);

    return 0;
}
/*
 * ESC & f <decipoints> J
 */

static int
set_paper_length(pcl_args_t * pargs, pcl_state_t * pcs)
{
    uint decipoints = uint_arg(pargs);
    int i;
    pcl_paper_size_t *psize;

    for (i = 0; i < pcl_paper_type_count; i++) {
        if (101 == PAPER_SIZES[i].tag) {
            psize = &(PAPER_SIZES[i].psize);
            break;
        }
    }
    psize->height = decipoints * 10L;
    return 0;
}

/*
 * (From PCL5 Comparison Guide, p. 1-99)
 *
 * ESC * o <quality> Q
 *
 * Set print quality.
 */
static int
pcl_print_quality(pcl_args_t * pargs, pcl_state_t * pcs)
{
    int quality = int_arg(pargs);

    if ((quality >= -1) && (quality <= 1)) {
        int code = pcl_end_page_if_marked(pcs);

        if (code >= 0)
            pcl_home_cursor(pcs);
        return (code < 0 ? code : 0);
    } else
        return e_Range;
}

/*
 * Initialization
 */
static int
pcpage_do_registration(pcl_parser_state_t * pcl_parser_state, gs_memory_t * pmem        /* ignored */
    )
{
    /* Register commands */
    DEFINE_CLASS('&') {
        'l', 'A',
            PCL_COMMAND("Page Size",
                        set_page_size, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'H',
            PCL_COMMAND("Paper Source",
                        set_paper_source, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'U',
            PCL_COMMAND("Left Offset Registration",
                        set_left_offset_registration,
                        pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'Z',
            PCL_COMMAND("Top Offset Registration",
                        set_top_offset_registration,
                        pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'O',
            PCL_COMMAND("Page Orientation",
                        set_logical_page_orientation,
                        pca_neg_ok | pca_big_ignore)
    }, {
        'a', 'P',
            PCL_COMMAND("Print Direction",
                        set_print_direction, pca_neg_ok | pca_big_ignore)
    }, {
        'a', 'L',
            PCL_COMMAND("Left Margin",
                        set_left_margin, pca_neg_ok | pca_big_ignore)
    }, {
        'a', 'M',
            PCL_COMMAND("Right Margin",
                        set_right_margin, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'E',
            PCL_COMMAND("Top Margin",
                        set_top_margin, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'F',
            PCL_COMMAND("Text Length",
                        set_text_length, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'L',
            PCL_COMMAND("Perforation Skip",
                        set_perforation_skip, pca_neg_ok | pca_big_ignore)
    }, {
        'l', 'M',
            PCL_COMMAND("Media Type",
                        pcl_media_type, pca_neg_ok | pca_big_ignore)
    }, {
        'a', 'W', PCL_COMMAND("Set Logical Page", set_logical_page, pca_bytes)
    }, END_CLASS DEFINE_CLASS('&') {
        'f', 'I',
            PCL_COMMAND("Set Custom Paper Width",
                        set_paper_width, pca_neg_ok | pca_big_ignore)
    }, {
        'f', 'J',
            PCL_COMMAND("Set Custom Paper Length",
                        set_paper_length, pca_neg_ok | pca_big_ignore)
    }, END_CLASS
        DEFINE_ESCAPE('9',
                      "Clear Horizontal Margins", clear_horizontal_margins)

        DEFINE_CLASS_COMMAND_ARGS('*',
                                  'o',
                                  'Q',
                                  "Print Quality",
                                  pcl_print_quality,
                                  pca_neg_ok | pca_big_ignore)
        return 0;
}

pcl_paper_size_t *
pcl_get_default_paper(pcl_state_t * pcs)
{
    int i;
    pjl_envvar_t *pwidth = pjl_proc_get_envvar(pcs->pjls, "paperwidth");
    pjl_envvar_t *plength = pjl_proc_get_envvar(pcs->pjls, "paperlength");
    pjl_envvar_t *psize = pjl_proc_get_envvar(pcs->pjls, "paper");

    /* build the state's paper table if it doesn't exist */
    if (!pcs->ppaper_type_table)
        pcs->ppaper_type_table =
            (pcl_paper_type_t *) gs_alloc_bytes(pcs->memory,
                                                sizeof(paper_types_proto),
                                                "Paper Table");

    if (!pcs->ppaper_type_table)
        /* should never fail */
        return NULL;

    /* restore default table values - this would only reset custom paper sizes */
    memcpy(pcs->ppaper_type_table, paper_types_proto,
           sizeof(paper_types_proto));

    pcs->wide_a4 = false;
    if (pcs->page_set_on_command_line || (*pwidth && *plength)) {
        for (i = 0; i < pcl_paper_type_count; i++)
            if (!pjl_proc_compare(pcs->pjls, "custom", PAPER_SIZES[i].pname)) {
                if (pcs->page_set_on_command_line) {
                    PAPER_SIZES[i].psize.width =
                        gs_currentdevice(pcs->pgs)->MediaSize[0] * 100;
                    PAPER_SIZES[i].psize.height =
                        gs_currentdevice(pcs->pgs)->MediaSize[1] * 100;
                } else {
                    PAPER_SIZES[i].psize.width = atol(pwidth) * 10L;
                    PAPER_SIZES[i].psize.height = atol(plength) * 10L;
                }
                /* just a guess, values copied from letter entry in
                   table PAPER_SIZES. NB offsets should be 0 for
                   RTL. */
                PAPER_SIZES[i].psize.offset_portrait = 75 * 24L;
                PAPER_SIZES[i].psize.offset_landscape = 60 * 24L;
                return &(PAPER_SIZES[i].psize);
            }
    }

    for (i = 0; i < pcl_paper_type_count; i++)
        if (!pjl_proc_compare(pcs->pjls, psize, PAPER_SIZES[i].pname)) {
            /* set wide a4, only used if the paper is a4 */
            if (!pjl_proc_compare
                (pcs->pjls, pjl_proc_get_envvar(pcs->pjls, "widea4"), "YES"))
                pcs->wide_a4 = true;
            return &(PAPER_SIZES[i].psize);
        }
    dmprintf(pcs->memory,
             "system does not support requested paper setting\n");
    return &(PAPER_SIZES[1].psize);
}

static void
pcpage_do_reset(pcl_state_t * pcs, pcl_reset_type_t type)
{

    /* NB hack for snippet mode */
    if (pcs->end_page != pcl_end_page_top) {
        pcs->xfm_state.print_dir = 0;
        pcs->xfm_state.left_offset_cp = 0.0;
        pcs->xfm_state.top_offset_cp = 0.0;
        update_xfm_state(pcs, 0);

        /* if we are in snippet mode, ESC E (reset) will set the
           default margins otherwise we should use the unusual
           passthrough margins (1/6") see above */
        if (type & pcl_reset_printer)
            reset_margins(pcs, false);
        else
            reset_margins(pcs, true);
        return;
    }

    if ((type & (pcl_reset_initial | pcl_reset_printer)) != 0) {
        pcs->paper_source = 0;  /* ??? */
        pcs->xfm_state.left_offset_cp = 0.0;
        pcs->xfm_state.top_offset_cp = 0.0;
        pcs->perforation_skip = 1;
        new_logical_page(pcs,
                         !pjl_proc_compare(pcs->pjls,
                                           pjl_proc_get_envvar(pcs->pjls,
                                                               "orientation"),
                                           "portrait") ? 0 : 1,
                         pcl_get_default_paper(pcs),
                         (type & pcl_reset_initial) != 0, false);
    } else if ((type & pcl_reset_overlay) != 0) {
        pcs->perforation_skip = 1;
        update_xfm_state(pcs, 0);
        reset_margins(pcs, false);
        pcl_xfm_reset_pcl_pat_ref_pt(pcs);
    } else if ((type & pcl_reset_permanent) != 0) {
        if (pcs->ppaper_type_table) {
            gs_free_object(pcs->memory, pcs->ppaper_type_table,
                           "Paper Table");
            pcs->ppaper_type_table = 0;
        }
    }
}

const pcl_init_t pcpage_init = { pcpage_do_registration, pcpage_do_reset, 0 };