/src/ghostpdl/pcl/pcl/pcpage.c
Line | Count | Source |
1 | | /* Copyright (C) 2001-2025 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* pcpage.c - PCL5 page and transformation control commands */ |
18 | | |
19 | | #include "math_.h" |
20 | | #include "std.h" |
21 | | #include "pcommand.h" |
22 | | #include "pcstate.h" |
23 | | #include "pcdraw.h" |
24 | | #include "pcparam.h" |
25 | | #include "pcparse.h" /* for pcl_execute_macro */ |
26 | | #include "pcfont.h" /* for underline interface */ |
27 | | #include "pcpatxfm.h" |
28 | | #include "pcursor.h" |
29 | | #include "pcpage.h" |
30 | | #include "pgmand.h" |
31 | | #include "pginit.h" |
32 | | #include "plmain.h" /* for finish page */ |
33 | | #include "plvalue.h" |
34 | | #include "gsmatrix.h" /* for gsdevice.h */ |
35 | | #include "gscoord.h" |
36 | | #include "gsdevice.h" |
37 | | #include "gspaint.h" |
38 | | #include "gspath.h" |
39 | | #include "gxdevice.h" |
40 | | #include "pjtop.h" |
41 | | #include "rtgmode.h" |
42 | | #include <stdlib.h> /* for atol() */ |
43 | | |
44 | | /* |
45 | | * The PCL printable region. In older HP printers this region was set to |
46 | | * 1/6" from the edge of the paper, irrespecitve of the paper size or the |
47 | | * device. However, recent HP printers allow the margin to be configurable |
48 | | * and even set to 0 for full bleed. If a non-zero margin is needed it can be |
49 | | * set on the command line via -H (e.g. -H12x12x12x12 restores the previous |
50 | | * 1/6" margin). See doc/ghostpdl.pdf for details. |
51 | | */ |
52 | | #define PRINTABLE_MARGIN_CP inch2coord(0.0) |
53 | | |
54 | 865k | #define round(x) (((x) < 0.0) ? (ceil ((x) - 0.5)) : (floor ((x) + 0.5))) |
55 | | |
56 | | /* Procedures */ |
57 | | |
58 | | /* |
59 | | * Preserve the current point and text margin set by transfroming them into |
60 | | * logical page space. |
61 | | */ |
62 | | static void |
63 | | preserve_cap_and_margins(const pcl_state_t * pcs, |
64 | | gs_point * pcur_pt, gs_rect * ptext_rect) |
65 | 197k | { |
66 | 197k | pcur_pt->x = (double)pcs->cap.x; |
67 | 197k | pcur_pt->y = (double)pcs->cap.y; |
68 | 197k | gs_point_transform(pcur_pt->x, |
69 | 197k | pcur_pt->y, &(pcs->xfm_state.pd2lp_mtx), pcur_pt); |
70 | 197k | ptext_rect->p.x = (double)pcs->margins.left; |
71 | 197k | ptext_rect->p.y = (double)pcs->margins.top; |
72 | 197k | ptext_rect->q.x = (double)pcs->margins.right; |
73 | 197k | ptext_rect->q.y = (double)(pcs->margins.top + pcs->margins.length); |
74 | 197k | pcl_transform_rect(ptext_rect, ptext_rect, &(pcs->xfm_state.pd2lp_mtx)); |
75 | 197k | } |
76 | | |
77 | | /* |
78 | | * Convert the current point and text margin set back into "pseudo print |
79 | | * direction" space. |
80 | | */ |
81 | | static void |
82 | | restore_cap_and_margins(pcl_state_t * pcs, |
83 | | const gs_point * pcur_pt, const gs_rect * ptext_rect) |
84 | 197k | { |
85 | 197k | gs_matrix lp2pd; |
86 | 197k | gs_point tmp_pt; |
87 | 197k | gs_rect tmp_rect; |
88 | | |
89 | 197k | pcl_invert_mtx(&(pcs->xfm_state.pd2lp_mtx), &lp2pd); |
90 | 197k | gs_point_transform(pcur_pt->x, pcur_pt->y, &lp2pd, &tmp_pt); |
91 | 197k | pcs->cap.x = (coord) tmp_pt.x; |
92 | 197k | pcs->cap.y = (coord) tmp_pt.y; |
93 | 197k | pcl_transform_rect(ptext_rect, &tmp_rect, &lp2pd); |
94 | 197k | pcs->margins.left = (coord) tmp_rect.p.x; |
95 | 197k | pcs->margins.top = (coord) tmp_rect.p.y; |
96 | 197k | pcs->margins.right = (coord) tmp_rect.q.x; |
97 | 197k | pcs->margins.length = (coord) tmp_rect.q.y - pcs->margins.top; |
98 | 197k | } |
99 | | |
100 | | /* |
101 | | * Update the transformations stored in the PCL state. This will also update |
102 | | * the device clipping region information in device and logical page space. |
103 | | * The text region margins are preserved. |
104 | | * |
105 | | * This routine should be called for: |
106 | | * |
107 | | * changes in the paper size |
108 | | * transition from page front to page back for duplexing |
109 | | * (this facility is not currently implemented) |
110 | | * change of left or top offset registration |
111 | | * change of logical page orientation |
112 | | * change of print direction |
113 | | * |
114 | | * The paper size, left/top offsets, logical page orientation, and print |
115 | | * direction should be set before this procedure is called. |
116 | | */ |
117 | | static void |
118 | | update_xfm_state(pcl_state_t * pcs, bool reset_initial) |
119 | 216k | { |
120 | 216k | pcl_xfm_state_t *pxfmst = &(pcs->xfm_state); |
121 | 216k | const pcl_paper_size_t *psize = pxfmst->paper_size; |
122 | 216k | coord offset; |
123 | 216k | gs_matrix pg2dev, pg2lp; |
124 | 216k | gs_rect print_rect, dev_rect, text_rect; |
125 | 216k | gs_point cur_pt; |
126 | 216k | double loff = pxfmst->left_offset_cp; |
127 | 216k | double toff = pxfmst->top_offset_cp; |
128 | | |
129 | | /* preserve the current point and text rectangle in logical page space */ |
130 | 216k | if (!reset_initial) |
131 | 197k | preserve_cap_and_margins(pcs, &cur_pt, &text_rect); |
132 | | |
133 | | /* get the page to device transformation */ |
134 | 216k | gs_defaultmatrix(pcs->pgs, &pg2dev); |
135 | | |
136 | | /* |
137 | | * Get the logical to page space transformation, and the dimensions of the |
138 | | * logical page. |
139 | | * |
140 | | * NOT YET IMPLEMENT - if on back of a duplex page, change size of offsets |
141 | | * |
142 | | * if (duplex_back(pcs)) { |
143 | | * loff = -loff; |
144 | | * toff = -toff; |
145 | | * } |
146 | | */ |
147 | 216k | { |
148 | 216k | gx_device *pdev = gs_currentdevice(pcs->pgs); |
149 | | |
150 | | /* get the LeadingEdge from the device |
151 | | * This has probably no influence on the resulting calculations, |
152 | | * as the offsetregistration is only influenced by pageside & bindingedge |
153 | | * (not feededge) */ |
154 | 216k | if ((pdev->LeadingEdge & 1) == 1) { /* longedgefeed */ |
155 | 0 | if (pcs->back_side) { |
156 | 0 | if (pcs->bind_short_edge) { |
157 | 0 | toff = -toff; |
158 | 0 | } else { /* bind_long_edge */ |
159 | 0 | loff = -loff; |
160 | 0 | } |
161 | 0 | } else { |
162 | | /* do not change LOFF */ |
163 | | /* TOFF ? */ |
164 | 0 | } |
165 | 216k | } else { /* shortedgefeed */ |
166 | 216k | if (pcs->back_side) { |
167 | 410 | if (pcs->bind_short_edge) { |
168 | 9 | toff = -toff; |
169 | 401 | } else { /* bind_long_edge */ |
170 | 401 | loff = -loff; |
171 | 401 | } |
172 | 215k | } else { |
173 | | /* do nothing */ |
174 | 215k | } |
175 | 216k | } |
176 | 216k | } |
177 | | |
178 | 216k | pcl_make_rotation(pxfmst->lp_orient, |
179 | 216k | (double) (psize->width), |
180 | 216k | (double) (psize->height), &(pxfmst->lp2pg_mtx) |
181 | 216k | ); |
182 | 216k | pxfmst->lp2pg_mtx.tx += loff; |
183 | 216k | pxfmst->lp2pg_mtx.ty += toff; |
184 | | |
185 | | /* if RTL mode or there is PJL requesting full bleed the offsets |
186 | | * of the logical page are 0. |
187 | | */ |
188 | 216k | if ((pcs->personality == rtl) || |
189 | 216k | (!pjl_proc_compare(pcs->pjls, |
190 | 216k | pjl_proc_get_envvar(pcs->pjls, "edgetoedge"), "YES"))) |
191 | | |
192 | 0 | offset = 0; |
193 | 216k | else |
194 | 216k | offset = ((pxfmst->lp_orient & 0x1) != 0 ? psize->offset_landscape |
195 | 216k | : psize->offset_portrait); |
196 | | |
197 | | /* we need an extra 1/10 inch on each side to support 80 |
198 | | characters vs. 78 at 10 cpi. Only apply to A4. */ |
199 | 216k | if ((pcs->wide_a4) && (psize->width == 59520) && (psize->height == 84168)) |
200 | 0 | offset -= inch2coord(1.0 / 10.0); |
201 | | |
202 | 216k | gs_matrix_translate(&(pxfmst->lp2pg_mtx), |
203 | 216k | (double) offset, 0.0, &(pxfmst->lp2pg_mtx) |
204 | 216k | ); |
205 | 216k | if ((pxfmst->lp_orient & 0x1) != 0) { |
206 | 4.31k | pxfmst->lp_size.x = psize->height - 2 * offset; |
207 | 4.31k | pxfmst->lp_size.y = psize->width; |
208 | 211k | } else { |
209 | 211k | pxfmst->lp_size.x = psize->width - 2 * offset; |
210 | 211k | pxfmst->lp_size.y = psize->height; |
211 | 211k | } |
212 | | |
213 | | /* then the logical page to device transformation */ |
214 | 216k | gs_matrix_multiply(&(pxfmst->lp2pg_mtx), &pg2dev, &(pxfmst->lp2dev_mtx)); |
215 | 216k | pg2dev.ty = round(pg2dev.ty); |
216 | 216k | pg2dev.tx = round(pg2dev.tx); |
217 | 216k | pxfmst->lp2dev_mtx.tx = round(pxfmst->lp2dev_mtx.tx); |
218 | 216k | pxfmst->lp2dev_mtx.ty = round(pxfmst->lp2dev_mtx.ty); |
219 | | /* the "pseudo page direction to logical page/device transformations */ |
220 | 216k | pcl_make_rotation(pxfmst->print_dir, |
221 | 216k | (double) pxfmst->lp_size.x, |
222 | 216k | (double) pxfmst->lp_size.y, &(pxfmst->pd2lp_mtx) |
223 | 216k | ); |
224 | 216k | gs_matrix_multiply(&(pxfmst->pd2lp_mtx), |
225 | 216k | &(pxfmst->lp2dev_mtx), &(pxfmst->pd2dev_mtx) |
226 | 216k | ); |
227 | | |
228 | | /* calculate the print direction page size */ |
229 | 216k | if ((pxfmst->print_dir & 0x1) || (pcs->personality == rtl)) { |
230 | 2.26k | pxfmst->pd_size.x = pxfmst->lp_size.y; |
231 | 2.26k | pxfmst->pd_size.y = pxfmst->lp_size.x; |
232 | 2.26k | } else |
233 | 214k | pxfmst->pd_size = pxfmst->lp_size; |
234 | | |
235 | 216k | { |
236 | 216k | gx_device *pdev = gs_currentdevice(pcs->pgs); |
237 | | |
238 | | /* We must not set up a clipping region beyond the hardware |
239 | | margins of the device, but the pcl language definition |
240 | | requires hardware margins to be 1/6". We set all margins |
241 | | to the the maximum of the PCL language defined 1/6" and the |
242 | | actual hardware margin. If 1/6" is not available pcl will |
243 | | not work correctly all of the time. The HPGL-2/RTL mode |
244 | | does not use margins. */ |
245 | 216k | if (pcs->personality == rtl) { |
246 | 0 | print_rect.p.x = inch2coord(pdev->HWMargins[1]) / 72.0; |
247 | 0 | print_rect.p.y = inch2coord(pdev->HWMargins[0] / 72.0); |
248 | 0 | print_rect.q.x = |
249 | 0 | psize->height - inch2coord(pdev->HWMargins[3] / 72.0); |
250 | 0 | print_rect.q.y = |
251 | 0 | psize->width - inch2coord(pdev->HWMargins[2] / 72.0); |
252 | 216k | } else { |
253 | 216k | print_rect.p.x = |
254 | 216k | max(PRINTABLE_MARGIN_CP, |
255 | 216k | inch2coord(pdev->HWMargins[0] / 72.0)); |
256 | 216k | print_rect.p.y = |
257 | 216k | max(PRINTABLE_MARGIN_CP, |
258 | 216k | inch2coord(pdev->HWMargins[1]) / 72.0); |
259 | 216k | print_rect.q.x = |
260 | 216k | psize->width - max(PRINTABLE_MARGIN_CP, |
261 | 216k | inch2coord(pdev->HWMargins[2] / 72.0)); |
262 | 216k | print_rect.q.y = |
263 | 216k | psize->height - max(PRINTABLE_MARGIN_CP, |
264 | 216k | inch2coord(pdev->HWMargins[3] / 72.0)); |
265 | 216k | } |
266 | 216k | pcl_transform_rect(&print_rect, &dev_rect, &pg2dev); |
267 | 216k | } |
268 | 216k | pcl_invert_mtx(&(pxfmst->lp2pg_mtx), &pg2lp); |
269 | 216k | pcl_transform_rect(&print_rect, &(pxfmst->lp_print_rect), &pg2lp); |
270 | | |
271 | | /* restablish the current point and text region */ |
272 | 216k | if (!reset_initial) |
273 | 197k | restore_cap_and_margins(pcs, &cur_pt, &text_rect); |
274 | | |
275 | | /* |
276 | | * No need to worry about pat_orient or pat_ref_pt; these will always |
277 | | * be recalculated just prior to use. |
278 | | */ |
279 | 216k | } |
280 | | |
281 | | /* default margins, relative to the logical page boundaries */ |
282 | 66.6k | #define DFLT_TOP_MARGIN (pcs->personality == rtl ? 0 : inch2coord(0.5)) |
283 | 67.8k | #define DFLT_LEFT_MARGIN inch2coord(0.0) |
284 | 67.8k | #define DFLT_RIGHT_MARGIN inch2coord(0.0) |
285 | 66.6k | #define DFLT_BOTTOM_MARGIN (pcs->personality == rtl ? 0 : inch2coord(0.5)) |
286 | | |
287 | 53 | #define DFLT_TOP_MARGIN_PASSTHROUGH inch2coord(1.0/6.0) |
288 | 53 | #define DFLT_BOTOM_MARGIN_PASSTHROUGH inch2coord(1.0/6.0) |
289 | | |
290 | 66.7k | #define TOP_MARGIN(hgt, tmarg) ((hgt) > (tmarg) ? (tmarg) : 0) |
291 | 68.8k | #define PAGE_LENGTH(hgt, bmarg) ((hgt) > (bmarg) ? (hgt) - (bmarg) : (hgt)) |
292 | | |
293 | | /* |
294 | | * Reset the top margin an text length. |
295 | | * |
296 | | * Note that, even though changing the print direction merely relabels (but does |
297 | | * not relocate) the margins, the preint direction does change the location of |
298 | | * the default margins. |
299 | | */ |
300 | | static void |
301 | | reset_vertical_margins(pcl_state_t * pcs, bool for_passthrough) |
302 | 66.7k | { |
303 | 66.7k | pcl_margins_t *pmar = &(pcs->margins); |
304 | 66.7k | coord hgt = pcs->xfm_state.pd_size.y; |
305 | 66.7k | coord tm = (for_passthrough ? |
306 | 66.7k | DFLT_TOP_MARGIN_PASSTHROUGH : DFLT_TOP_MARGIN); |
307 | 66.7k | coord bm = (for_passthrough ? |
308 | 66.7k | DFLT_BOTOM_MARGIN_PASSTHROUGH : DFLT_BOTTOM_MARGIN); |
309 | 66.7k | pmar->top = TOP_MARGIN(hgt, tm); |
310 | 66.7k | pmar->length = PAGE_LENGTH(hgt - pmar->top, bm); |
311 | 66.7k | } |
312 | | |
313 | | /* |
314 | | * Reset horizontal margins |
315 | | * |
316 | | * Note that, even though changing the print direction merely relabels (but does |
317 | | * not relocate) the margins, the preint direction does change the location of |
318 | | * the default margins. |
319 | | */ |
320 | | static void |
321 | | reset_horizontal_margins(pcl_state_t * pcs) |
322 | 67.8k | { |
323 | 67.8k | pcl_margins_t *pmar = &(pcs->margins); |
324 | | |
325 | 67.8k | pmar->left = DFLT_LEFT_MARGIN; |
326 | 67.8k | pmar->right = pcs->xfm_state.pd_size.x - DFLT_RIGHT_MARGIN; |
327 | 67.8k | } |
328 | | |
329 | | /* |
330 | | * Reset both the horizontal and vertical margins |
331 | | */ |
332 | | static void |
333 | | reset_margins(pcl_state_t * pcs, bool for_passthrough) |
334 | 66.7k | { |
335 | 66.7k | reset_horizontal_margins(pcs); |
336 | 66.7k | reset_vertical_margins(pcs, for_passthrough); |
337 | 66.7k | } |
338 | | |
339 | | static void |
340 | | reset_default_transformation(pcl_state_t *pcs) |
341 | 210k | { |
342 | 210k | gs_gstate *pgs = pcs->pgs; |
343 | 210k | gs_matrix mat; |
344 | 210k | double height_pts = pcs->xfm_state.paper_size->height * 0.01; |
345 | | |
346 | | /* |
347 | | * Reset the default transformation. |
348 | | * |
349 | | * The graphic library provides a coordinate system in points, with the |
350 | | * origin at the lower left corner of the page. The PCL code uses a |
351 | | * coordinate system in centi-points, with the origin at the upper left |
352 | | * corner of the page. |
353 | | */ |
354 | 210k | gs_setdefaultmatrix(pgs, NULL); |
355 | 210k | gs_initmatrix(pgs); |
356 | 210k | gs_currentmatrix(pgs, &mat); |
357 | | |
358 | 210k | if (pcs->personality != rtl) { |
359 | 210k | gs_matrix_translate(&mat, 0.0, height_pts, &mat); |
360 | 210k | gs_matrix_scale(&mat, 0.01, -0.01, &mat); |
361 | 210k | } else { |
362 | 0 | gs_matrix_rotate(&mat, -90, &mat); |
363 | 0 | gs_matrix_scale(&mat, -0.01, 0.01, &mat); |
364 | 0 | } |
365 | | |
366 | 210k | gs_setdefaultmatrix(pgs, &mat); |
367 | 210k | } |
368 | | |
369 | | /* |
370 | | * Reset all parameters which must be reset whenever the page size changes. |
371 | | * |
372 | | * The third operand indicates if this routine is being called as part of |
373 | | * an initial reset. In that case, done't call HPGL's reset - the reset |
374 | | * will do that later. |
375 | | */ |
376 | | static int |
377 | | new_page_size(pcl_state_t * pcs, |
378 | | const pcl_paper_size_t * psize, |
379 | | bool reset_initial, bool for_passthrough) |
380 | 66.7k | { |
381 | 66.7k | double width_pts = psize->width * 0.01; |
382 | 66.7k | double height_pts = psize->height * 0.01; |
383 | 66.7k | float page_size[2]; |
384 | 66.7k | float old_page_size[2]; |
385 | 66.7k | bool changed_page_size; |
386 | 66.7k | int code = 0; |
387 | | |
388 | 66.7k | page_size[0] = width_pts; |
389 | 66.7k | page_size[1] = height_pts; |
390 | | |
391 | 66.7k | old_page_size[0] = gs_currentdevice(pcs->pgs)->MediaSize[0]; |
392 | 66.7k | old_page_size[1] = gs_currentdevice(pcs->pgs)->MediaSize[1]; |
393 | | |
394 | 66.7k | code = put_param1_float_array(pcs, "PageSize", page_size); |
395 | 66.7k | if (code < 0) return code; |
396 | | |
397 | 66.7k | pcs->xfm_state.paper_size = psize; |
398 | 66.7k | reset_default_transformation(pcs); |
399 | 66.7k | pcs->overlay_enabled = false; |
400 | 66.7k | update_xfm_state(pcs, reset_initial); |
401 | 66.7k | reset_margins(pcs, for_passthrough); |
402 | | |
403 | | /* check if update_xfm_state changed the page size */ |
404 | 66.7k | changed_page_size = !((int)old_page_size[0] == pcs->xfm_state.paper_size->width/100 && |
405 | 48.1k | (int)old_page_size[1] == pcs->xfm_state.paper_size->height/100); |
406 | | |
407 | | /* |
408 | | * make sure underlining is disabled (homing the cursor may cause |
409 | | * an underline to be put out. |
410 | | */ |
411 | 66.7k | pcs->underline_enabled = false; |
412 | 66.7k | code = pcl_home_cursor(pcs); |
413 | 66.7k | if (code < 0) return code; |
414 | | /* |
415 | | * this is were we initialized the cursor position |
416 | | */ |
417 | 66.7k | pcs->cursor_moved = false; |
418 | | |
419 | 66.7k | pcl_xfm_reset_pcl_pat_ref_pt(pcs); |
420 | | |
421 | 66.7k | if (!reset_initial) |
422 | 48.2k | code = hpgl_do_reset(pcs, pcl_reset_page_params); |
423 | 66.7k | if (code < 0) return code; |
424 | | |
425 | 66.7k | if (pcs->end_page == pcl_end_page_top) { /* don't erase in snippet mode */ |
426 | 66.6k | if (pcs->page_marked || changed_page_size) { |
427 | 18.6k | code = gs_erasepage(pcs->pgs); |
428 | 18.6k | pcs->page_marked = false; |
429 | 18.6k | } |
430 | 66.6k | } |
431 | | |
432 | 66.7k | return code; |
433 | 66.7k | } |
434 | | |
435 | | /* |
436 | | * Define the known paper sizes. |
437 | | * |
438 | | * The values are taken from the H-P manual and are in 1/300" units, |
439 | | * but the structure values are in centipoints (1/7200"). |
440 | | */ |
441 | | #define p_size(t, n, w, h, offp, offl) \ |
442 | | { (t), (n), { (w) * 24L, (h) * 24L, (offp) * 24L, (offl) * 24L } } |
443 | | |
444 | | pcl_paper_type_t paper_types_proto[] = { |
445 | | p_size(1, "executive", 2175, 3150, 75, 60), |
446 | | p_size(2, "letter", 2550, 3300, 75, 60), |
447 | | p_size(3, "legal", 2550, 4200, 75, 60), |
448 | | p_size(6, "ledger", 3300, 5100, 75, 60), |
449 | | p_size(26, "a4", 2480, 3507, 71, 59), |
450 | | p_size(27, "a3", 3507, 4960, 71, 59), |
451 | | p_size(78, "index_3x5", 900, 1500, 75, 60), |
452 | | p_size(80, "monarch", 1162, 2250, 75, 60), |
453 | | p_size(81, "com_10", 1237, 2850, 75, 60), |
454 | | p_size(90, "dl", 1299, 2598, 71, 59), |
455 | | p_size(91, "c5", 1913, 2704, 71, 59), |
456 | | p_size(100, "b5", 2078, 2952, 71, 59), |
457 | | /* initial values for custom: copied from default(letter) */ |
458 | | /* offset_portrait/offset_landscape ? */ |
459 | | p_size(101, "custom", 2550, 3300, 75, 60) |
460 | | }; |
461 | | |
462 | | const int pcl_paper_type_count = countof(paper_types_proto); |
463 | | |
464 | | /* |
465 | | * Reset all parameters which must be reset whenever the logical page |
466 | | * orientation changes. |
467 | | * |
468 | | * The last operand indicates if this routine is being called as part of |
469 | | * an initial resete. |
470 | | */ |
471 | | int |
472 | | new_logical_page(pcl_state_t * pcs, |
473 | | int lp_orient, |
474 | | const pcl_paper_size_t * psize, |
475 | | bool reset_initial, bool for_passthrough) |
476 | 64.3k | { |
477 | 64.3k | pcl_xfm_state_t *pxfmst = &(pcs->xfm_state); |
478 | | |
479 | 64.3k | pxfmst->lp_orient = lp_orient; |
480 | 64.3k | pxfmst->print_dir = 0; |
481 | 64.3k | return new_page_size(pcs, psize, reset_initial, for_passthrough); |
482 | 64.3k | } |
483 | | |
484 | | |
485 | 198k | #define PAPER_SIZES pcs->ppaper_type_table |
486 | | |
487 | | int |
488 | | pcl_new_logical_page_for_passthrough(pcl_state_t * pcs, int orient, |
489 | | gs_point * pdims) |
490 | 53 | { |
491 | 53 | int i; |
492 | 53 | pcl_paper_size_t *psize; |
493 | | /* points to centipoints */ |
494 | 53 | coord cp_width = (coord) (pdims->x * 100 + 0.5); |
495 | 53 | coord cp_height = (coord) (pdims->y * 100 + 0.5); |
496 | 53 | bool found = false; |
497 | | |
498 | 106 | for (i = 0; i < pcl_paper_type_count; i++) { |
499 | 106 | psize = &(PAPER_SIZES[i].psize); |
500 | 106 | if (psize->width == cp_width && psize->height == cp_height) { |
501 | 53 | found = true; |
502 | 53 | break; |
503 | 53 | } |
504 | 106 | } |
505 | | |
506 | 53 | if (!found) |
507 | 0 | return -1; |
508 | 53 | return new_logical_page(pcs, orient, psize, false, true); |
509 | 53 | } |
510 | | |
511 | | /* page marking routines */ |
512 | | |
513 | | |
514 | | int |
515 | | pcl_mark_page_for_current_pos(pcl_state_t * pcs) |
516 | 2.99M | { |
517 | 2.99M | int code = 0; |
518 | | |
519 | | /* nothing to do */ |
520 | 2.99M | if (pcs->page_marked) |
521 | 2.98M | return code; |
522 | | |
523 | | /* convert current point to device space and check if it is inside |
524 | | device rectangle for the page */ |
525 | 15.8k | { |
526 | 15.8k | gs_fixed_rect page_box; |
527 | 15.8k | gs_fixed_point pt; |
528 | | |
529 | 15.8k | code = gx_default_clip_box(pcs->pgs, &page_box); |
530 | 15.8k | if (code < 0) |
531 | | /* shouldn't happen. */ |
532 | 0 | return code; |
533 | | |
534 | 15.8k | code = gx_path_current_point(gx_current_path(pcs->pgs), &pt); |
535 | 15.8k | if (code < 0) |
536 | | /* shouldn't happen */ |
537 | 0 | return code; |
538 | | |
539 | | |
540 | | /* half-open lower - not sure this is correct */ |
541 | 15.8k | if (pt.x >= page_box.p.x && pt.y >= page_box.p.y && |
542 | 8.17k | pt.x < page_box.q.x && pt.y < page_box.q.y) |
543 | 2.89k | pcs->page_marked = true; |
544 | 15.8k | } |
545 | 0 | return code; |
546 | 15.8k | } |
547 | | |
548 | | |
549 | | int |
550 | | pcl_mark_page_for_character(pcl_state_t * pcs, gs_fixed_point *org) |
551 | 26.6M | { |
552 | | /* nothing to do */ |
553 | 26.6M | if (pcs->page_marked) |
554 | 26.5M | return 0; |
555 | | |
556 | | /* convert current point to device space and check if it is inside |
557 | | device rectangle for the page */ |
558 | 78.5k | { |
559 | 78.5k | gs_fixed_rect page_box; |
560 | 78.5k | gs_fixed_point pt; |
561 | 78.5k | int code; |
562 | | |
563 | 78.5k | code = gx_default_clip_box(pcs->pgs, &page_box); |
564 | 78.5k | if (code < 0) |
565 | | /* shouldn't happen. */ |
566 | 0 | return code; |
567 | | |
568 | 78.5k | code = gx_path_current_point(gx_current_path(pcs->pgs), &pt); |
569 | 78.5k | if (code < 0) |
570 | | /* shouldn't happen */ |
571 | 0 | return code; |
572 | | |
573 | | |
574 | | /* half-open lower - not sure this is correct */ |
575 | 78.5k | if (pt.x >= page_box.p.x && pt.y >= page_box.p.y && |
576 | 78.5k | org->x < page_box.q.x && org->y < page_box.q.y) |
577 | 78.2k | pcs->page_marked = true; |
578 | 78.5k | } |
579 | 0 | return 0; |
580 | 78.5k | } |
581 | | |
582 | | /* |
583 | | * Just use the current position to see if we marked the page. This |
584 | | * could be improved by using the bounding box of the path object but |
585 | | * for page marking that case does not seem to come up in practice. |
586 | | */ |
587 | | int |
588 | | pcl_mark_page_for_path(pcl_state_t * pcs) |
589 | 1.86M | { |
590 | 1.86M | return pcl_mark_page_for_current_pos(pcs); |
591 | 1.86M | } |
592 | | |
593 | | /* returns the bounding box coordinates for the current device and a |
594 | | boolean to indicate marked status. 0 - unmarked 1 - marked -1 error */ |
595 | | int |
596 | | pcl_page_marked(pcl_state_t * pcs) |
597 | 55.6k | { |
598 | 55.6k | return pcs->page_marked; |
599 | 55.6k | } |
600 | | |
601 | | int |
602 | | pcl_cursor_moved(pcl_state_t * pcs) |
603 | 1.13k | { |
604 | 1.13k | return pcs->cursor_moved; |
605 | 1.13k | } |
606 | | |
607 | | /* |
608 | | * End a page, either unconditionally or only if there are marks on it. |
609 | | * Return 1 if the page was actually printed and erased. |
610 | | */ |
611 | | int |
612 | | pcl_end_page(pcl_state_t * pcs, pcl_print_condition_t condition) |
613 | 163k | { |
614 | 163k | int code = pcl_break_underline(pcs); /* (could mark page) */ |
615 | 163k | if (code < 0) |
616 | 0 | return code; |
617 | | |
618 | | /* If we are conditionally printing (normal case) check if the |
619 | | page is marked */ |
620 | 163k | if (condition != pcl_print_always) { |
621 | 44.6k | if (!pcl_page_marked(pcs)) |
622 | 20.4k | return 0; |
623 | 44.6k | } |
624 | | |
625 | | /* finish up graphics mode in case we finished the page in the |
626 | | middle of a raster stream */ |
627 | 143k | if (pcs->raster_state.graphics_mode) |
628 | 761 | code = pcl_end_graphics_mode(pcs); |
629 | 143k | if (code < 0) |
630 | 0 | return code; |
631 | | |
632 | | /* If there's an overlay macro, execute it now. */ |
633 | 143k | if (pcs->overlay_enabled) { |
634 | 0 | void *value; |
635 | |
|
636 | 0 | if (pl_dict_find(&pcs->macros, |
637 | 0 | id_key(pcs->overlay_macro_id), 2, &value)) { |
638 | 0 | pcs->overlay_enabled = false; /**** IN reset_overlay ****/ |
639 | 0 | code = pcl_execute_macro((const pcl_macro_t *)value, |
640 | 0 | pcs, |
641 | 0 | pcl_copy_before_overlay, |
642 | 0 | pcl_reset_overlay, |
643 | 0 | pcl_copy_after_overlay); |
644 | 0 | if (code < 0) |
645 | 0 | return code; |
646 | | |
647 | 0 | pcs->overlay_enabled = true; /**** IN copy_after ****/ |
648 | 0 | } |
649 | 0 | } |
650 | | /* output the page */ |
651 | 143k | code = (*pcs->end_page) (pcs, pcs->num_copies, true); |
652 | 143k | if (code < 0) |
653 | 0 | return code; |
654 | | |
655 | 143k | if (pcs->end_page == pcl_end_page_top) |
656 | 143k | code = gs_erasepage(pcs->pgs); |
657 | 143k | if (code < 0) |
658 | 0 | return code; |
659 | | |
660 | 143k | pcs->page_marked = false; |
661 | | |
662 | | /* |
663 | | * Advance of a page may move from a page front to a page back. This may |
664 | | * change the applicable transformations. |
665 | | */ |
666 | | /* |
667 | | * Keep track of the side you are on |
668 | | */ |
669 | 143k | if (pcs->duplex) { |
670 | 637 | pcs->back_side = ! pcs->back_side; |
671 | 142k | } else { |
672 | 142k | pcs->back_side = false; |
673 | 142k | } |
674 | 143k | code = put_param1_bool(pcs,"FirstSide", !pcs->back_side); |
675 | 143k | reset_default_transformation(pcs); |
676 | 143k | update_xfm_state(pcs, 0); |
677 | | |
678 | 143k | pcl_continue_underline(pcs); |
679 | 143k | return (code < 0 ? code : 1); |
680 | 143k | } |
681 | | |
682 | | /* Commands */ |
683 | | |
684 | | /* |
685 | | * ESC & l <psize_enum> A |
686 | | * |
687 | | * Select paper size |
688 | | */ |
689 | | static int |
690 | | set_page_size(pcl_args_t * pargs, pcl_state_t * pcs) |
691 | 2.38k | { |
692 | | /* Note: not all values are implemented on all printers. If -g |
693 | | has been given on the command line we use the "Custom" page |
694 | | tag, the height and width are set to the device media values |
695 | | which were filled in when the -g option was processed. */ |
696 | | |
697 | 2.38k | uint tag = (pcs->page_set_on_command_line ? 101 : uint_arg(pargs)); |
698 | 2.38k | int i; |
699 | 2.38k | int code = 0; |
700 | 2.38k | const pcl_paper_size_t *psize = 0; |
701 | | |
702 | | /* oddly the command goes to the next page irrespective of |
703 | | arguments */ |
704 | 2.38k | code = pcl_end_page_if_marked(pcs); |
705 | 2.38k | if (code < 0) |
706 | 0 | return code; |
707 | 2.38k | code = pcl_home_cursor(pcs); |
708 | 2.38k | if (code < 0) |
709 | 0 | return code; |
710 | | |
711 | 5.09k | for (i = 0; i < pcl_paper_type_count; i++) { |
712 | 5.06k | if (tag == PAPER_SIZES[i].tag) { |
713 | 2.35k | psize = &(PAPER_SIZES[i].psize); |
714 | 2.35k | break; |
715 | 2.35k | } |
716 | 5.06k | } |
717 | 2.38k | if ((psize != 0) && ((code = pcl_end_page_if_marked(pcs)) >= 0)) { |
718 | 2.35k | pcs->xfm_state.print_dir = 0; |
719 | 2.35k | code = new_page_size(pcs, psize, false, false); |
720 | 2.35k | } |
721 | 2.38k | return code; |
722 | 2.38k | } |
723 | | |
724 | | /* |
725 | | * ESC & l <feed_enum> H |
726 | | * |
727 | | * Set paper source |
728 | | */ |
729 | | static int |
730 | | set_paper_source(pcl_args_t * pargs, pcl_state_t * pcs) |
731 | 1.61k | { |
732 | 1.61k | uint i = uint_arg(pargs); |
733 | | /* oddly the command goes to the next page irrespective of |
734 | | arguments */ |
735 | 1.61k | int code = pcl_end_page_if_marked(pcs); |
736 | | |
737 | 1.61k | if (code < 0) |
738 | 0 | return code; |
739 | 1.61k | code = pcl_home_cursor(pcs); |
740 | 1.61k | if (code < 0) |
741 | 0 | return code; |
742 | | /* Do not change the page side if the wanted paper source is the same as the actual one */ |
743 | 1.61k | if (pcs->paper_source != i) { |
744 | 1.40k | pcs->back_side = false; |
745 | 1.40k | code = put_param1_bool(pcs, "FirstSide", !pcs->back_side); |
746 | 1.40k | if (code < 0) |
747 | 0 | return code; |
748 | 1.40k | } |
749 | 1.61k | pcs->paper_source = i; |
750 | | /* Note: not all printers support all possible values. */ |
751 | 1.61k | if (i <= 6) { |
752 | 540 | code = 0; |
753 | 540 | if (i > 0) |
754 | 436 | code = put_param1_int(pcs, "%MediaSource", i); |
755 | 540 | return (code < 0 ? code : 0); |
756 | 540 | } else |
757 | 1.07k | return e_Range; |
758 | 1.61k | } |
759 | | |
760 | | /* |
761 | | * ESC & l <xoff_dp> U |
762 | | * |
763 | | * Note that this shifts the logical page, but does NOT change its size nor |
764 | | * does it reset any logical-page related parameters. |
765 | | */ |
766 | | static int |
767 | | set_left_offset_registration(pcl_args_t * pargs, pcl_state_t * pcs) |
768 | 214 | { |
769 | 214 | pcs->xfm_state.left_offset_cp = float_arg(pargs) * 10.0; |
770 | 214 | update_xfm_state(pcs, 0); |
771 | 214 | return 0; |
772 | 214 | } |
773 | | |
774 | | /* |
775 | | * ESC & l <yoff_dp> Z |
776 | | * |
777 | | * Note that this shifts the logical page, but does NOT change its size nor |
778 | | * does it reset any logical-page related parameters. |
779 | | */ |
780 | | static int |
781 | | set_top_offset_registration(pcl_args_t * pargs, pcl_state_t * pcs) |
782 | 175 | { |
783 | 175 | pcs->xfm_state.top_offset_cp = float_arg(pargs) * 10; |
784 | 175 | update_xfm_state(pcs, 0); |
785 | 175 | return 0; |
786 | 175 | } |
787 | | |
788 | | /* |
789 | | * ESC & l <orient> O |
790 | | * |
791 | | * Set logical page orientation. |
792 | | */ |
793 | | static int |
794 | | set_logical_page_orientation(pcl_args_t * pargs, pcl_state_t * pcs) |
795 | 3.64k | { |
796 | 3.64k | uint i = uint_arg(pargs); |
797 | 3.64k | int code; |
798 | | /* the command is ignored if it is value is out of range */ |
799 | 3.64k | if (i > 3) |
800 | 1 | return 0; |
801 | | |
802 | | /* this command is ignored in pcl xl snippet mode. NB we need a |
803 | | better flag for snippet mode. */ |
804 | 3.64k | if (pcs->end_page != pcl_end_page_top) { |
805 | 0 | return 0; |
806 | 0 | } |
807 | | |
808 | | /* If orientation is same as before ignore the command */ |
809 | 3.64k | if (i == pcs->xfm_state.lp_orient) { |
810 | 1.73k | return 0; |
811 | 1.73k | } |
812 | | |
813 | | /* ok to execute - clear the page, set up the transformations and |
814 | | set the flag disabling the orientation command for this page. */ |
815 | 1.90k | code = pcl_end_page_if_marked(pcs); |
816 | 1.90k | if (code >= 0) { |
817 | | /* a page_orientation change is not the same as a new page. |
818 | | If a cursor has moved this should be remembered */ |
819 | 1.90k | bool cursor_moved = pcs->cursor_moved; |
820 | 1.90k | code = new_logical_page(pcs, i, pcs->xfm_state.paper_size, false, false); |
821 | 1.90k | pcs->cursor_moved = cursor_moved; |
822 | 1.90k | pcs->hmi_cp = HMI_DEFAULT; |
823 | 1.90k | pcs->vmi_cp = VMI_DEFAULT; |
824 | 1.90k | } |
825 | 1.90k | return code; |
826 | 3.64k | } |
827 | | |
828 | | /* |
829 | | * ESC & a <angle> P |
830 | | */ |
831 | | static int |
832 | | set_print_direction(pcl_args_t * pargs, pcl_state_t * pcs) |
833 | 6.71k | { |
834 | 6.71k | uint i = uint_arg(pargs); |
835 | | |
836 | 6.71k | if ((i <= 270) && (i % 90 == 0)) { |
837 | 5.87k | i /= 90; |
838 | 5.87k | if (i != pcs->xfm_state.print_dir) { |
839 | 2.30k | int code = pcl_break_underline(pcs); |
840 | 2.30k | if (code < 0) |
841 | 0 | return code; |
842 | 2.30k | pcs->xfm_state.print_dir = i; |
843 | 2.30k | update_xfm_state(pcs, 0); |
844 | 2.30k | pcl_continue_underline(pcs); |
845 | 3.57k | } else { |
846 | 3.57k | pcs->xfm_state.print_dir = i; |
847 | 3.57k | update_xfm_state(pcs, 0); |
848 | 3.57k | } |
849 | 5.87k | } |
850 | 6.71k | return 0; |
851 | 6.71k | } |
852 | | |
853 | | /* |
854 | | * ESC & a <col> L |
855 | | * |
856 | | * Set left margin. |
857 | | */ |
858 | | static int |
859 | | set_left_margin(pcl_args_t * pargs, pcl_state_t * pcs) |
860 | 1.59k | { |
861 | 1.59k | int code = pcl_update_hmi_cp(pcs); |
862 | 1.59k | coord lmarg = uint_arg(pargs) * pcs->hmi_cp; |
863 | | |
864 | 1.59k | if (code < 0) |
865 | 0 | return code; |
866 | | |
867 | | /* adjust underlining if the left margin passes to the right of |
868 | | the underline start position */ |
869 | 1.59k | if ((pcs->underline_enabled) && (lmarg > pcs->underline_start.x)) |
870 | 1 | pcs->underline_start.x = lmarg; |
871 | | |
872 | 1.59k | if (lmarg < pcs->margins.right) { |
873 | 1.58k | pcs->margins.left = lmarg; |
874 | 1.58k | if (pcs->cap.x < lmarg) |
875 | 325 | code = pcl_set_cap_x(pcs, lmarg, false, false); |
876 | 1.58k | } |
877 | 1.59k | return code; |
878 | 1.59k | } |
879 | | |
880 | | /* |
881 | | * ESC & a <col> M |
882 | | * |
883 | | * Set right margin. The right margin is set to the *right* edge of the |
884 | | * specified column, so we need to add 1 to the column number. |
885 | | */ |
886 | | static int |
887 | | set_right_margin(pcl_args_t * pargs, pcl_state_t * pcs) |
888 | 327 | { |
889 | 327 | int code = pcl_update_hmi_cp(pcs); |
890 | 327 | coord rmarg = (uint_arg(pargs) + 1) * pcs->hmi_cp; |
891 | | |
892 | 327 | if (code < 0) |
893 | 0 | return code; |
894 | | |
895 | 327 | if (rmarg > pcs->xfm_state.pd_size.x) |
896 | 16 | rmarg = pcs->xfm_state.pd_size.x; |
897 | 327 | if (rmarg > pcs->margins.left) { |
898 | 165 | pcs->margins.right = rmarg; |
899 | 165 | if (pcs->cap.x > rmarg) |
900 | 14 | code = pcl_set_cap_x(pcs, rmarg, false, false); |
901 | 165 | } |
902 | | |
903 | 327 | return code; |
904 | 327 | } |
905 | | |
906 | | /* |
907 | | * ESC 9 |
908 | | * |
909 | | * Clear horizontal margins. |
910 | | */ |
911 | | static int |
912 | | clear_horizontal_margins(pcl_args_t * pargs, /* ignored */ |
913 | | pcl_state_t * pcs) |
914 | 1.15k | { |
915 | 1.15k | reset_horizontal_margins(pcs); |
916 | 1.15k | return 0; |
917 | 1.15k | } |
918 | | |
919 | | /* |
920 | | * ESC & l <line> E |
921 | | * |
922 | | * Set top margin. This will also reset the page length. |
923 | | */ |
924 | | static int |
925 | | set_top_margin(pcl_args_t * pargs, pcl_state_t * pcs) |
926 | 2.16k | { |
927 | 2.16k | coord hgt = pcs->xfm_state.pd_size.y; |
928 | 2.16k | coord tmarg = uint_arg(pargs) * pcs->vmi_cp; |
929 | | |
930 | 2.16k | if ((pcs->vmi_cp != 0) && (tmarg <= hgt)) { |
931 | 2.15k | pcs->margins.top = tmarg; |
932 | 2.15k | pcs->margins.length = PAGE_LENGTH(hgt - tmarg, DFLT_BOTTOM_MARGIN); |
933 | | /* See the discussion in the Implementor's guide concerning |
934 | | "fixed" and "floating" cap. If the page is not dirty we |
935 | | home the cursor - the guide talks about tracking the cursor |
936 | | for any movement and checking for data on the page */ |
937 | 2.15k | if (!pcl_page_marked(pcs) && !pcl_cursor_moved(pcs)) |
938 | 1.10k | return pcl_set_cap_y(pcs, 0L, false, false, true, false); |
939 | 2.15k | } |
940 | 1.06k | return 0; |
941 | 2.16k | } |
942 | | |
943 | | /* |
944 | | * ESC & l <lines> F |
945 | | * |
946 | | * Set text length (which indirectly sets the bottom margin). |
947 | | */ |
948 | | static int |
949 | | set_text_length(pcl_args_t * pargs, pcl_state_t * pcs) |
950 | 2.30k | { |
951 | 2.30k | coord len = uint_arg(pargs) * pcs->vmi_cp; |
952 | | |
953 | 2.30k | if (len == 0) { |
954 | 377 | len = |
955 | 377 | pcs->xfm_state.pd_size.y - pcs->margins.top - |
956 | 377 | inch2coord(1.0 / 2.0); |
957 | 377 | if (len < 0) { |
958 | 0 | len += inch2coord(1.0 / 2.0); |
959 | 0 | } |
960 | 377 | } |
961 | 2.30k | if ((len >= 0) && (pcs->margins.top + len <= pcs->xfm_state.pd_size.y)) |
962 | 2.23k | pcs->margins.length = len; |
963 | 2.30k | return 0; |
964 | 2.30k | } |
965 | | |
966 | | /* |
967 | | * ESC & l <enable> L |
968 | | * |
969 | | * Set perforation skip mode. Though performation skip is more closely related |
970 | | * to vertical motion than to margins, the command is included here because it |
971 | | * resets the vertical margins (top margin and text length) to their defaults. |
972 | | */ |
973 | | static int |
974 | | set_perforation_skip(pcl_args_t * pargs, pcl_state_t * pcs) |
975 | 1.43k | { |
976 | 1.43k | int new_skip = (int)uint_arg(pargs); |
977 | | |
978 | 1.43k | if ((new_skip != pcs->perforation_skip) && (new_skip <= 1)) |
979 | 833 | pcs->perforation_skip = new_skip; |
980 | 1.43k | return 0; |
981 | 1.43k | } |
982 | | |
983 | | /* |
984 | | * (From PCL5 Comparison Guide, p. 1-98) |
985 | | * |
986 | | * ESC & l <type> M |
987 | | * |
988 | | * Set media type. |
989 | | */ |
990 | | static int |
991 | | pcl_media_type(pcl_args_t * pargs, pcl_state_t * pcs) |
992 | 13 | { |
993 | 13 | int type = uint_arg(pargs); |
994 | | |
995 | 13 | if (type <= 4) { |
996 | 12 | int code = pcl_end_page_if_marked(pcs); |
997 | | |
998 | 12 | if (code >= 0) |
999 | 12 | code = pcl_home_cursor(pcs); |
1000 | 12 | return (code < 0 ? code : e_Unimplemented); |
1001 | 12 | } else |
1002 | 1 | return e_Range; |
1003 | 13 | } |
1004 | | |
1005 | | /* |
1006 | | * Logical Page Setup (From HP D640 Technical Manual) |
1007 | | */ |
1008 | | |
1009 | | typedef struct pcl_logical_page_s |
1010 | | { |
1011 | | byte LeftOffset[2]; |
1012 | | byte TopOffset[2]; |
1013 | | byte Orientation; |
1014 | | byte Reserved; |
1015 | | byte Width[2]; |
1016 | | byte Height[2]; |
1017 | | } pcl_logical_page_t; |
1018 | | |
1019 | | /* |
1020 | | * ESC & a # W |
1021 | | * |
1022 | | * Set logical page. |
1023 | | */ |
1024 | | |
1025 | | static int |
1026 | | set_logical_page(pcl_args_t * pargs, pcl_state_t * pcs) |
1027 | 58 | { |
1028 | 58 | uint count = uint_arg(pargs); |
1029 | 58 | int code = 0; |
1030 | | |
1031 | 58 | const pcl_logical_page_t *plogpage = |
1032 | 58 | (pcl_logical_page_t *) arg_data(pargs); |
1033 | 58 | pcl_paper_size_t *pcur_paper; |
1034 | | |
1035 | | #ifdef DEBUG |
1036 | | if (gs_debug_c('i')) { |
1037 | | pcl_debug_dump_data(pcs->memory, arg_data(pargs), uint_arg(pargs)); |
1038 | | } |
1039 | | #endif |
1040 | | |
1041 | | /* the currently selected paper size */ |
1042 | 58 | pcur_paper = (pcl_paper_size_t *) pcs->xfm_state.paper_size; |
1043 | | |
1044 | | /* the command can set width, height and offsets (10) or just |
1045 | | offsets (4) */ |
1046 | 58 | if (count != 10 && count != 4) |
1047 | 58 | return e_Range; |
1048 | | |
1049 | 0 | if (count == 10) { |
1050 | 0 | pcur_paper->width = pl_get_uint16(plogpage->Width) * 10; |
1051 | 0 | pcur_paper->height = pl_get_uint16(plogpage->Height) * 10; |
1052 | 0 | if (pcur_paper->width == 0 || pcur_paper->height == 0) |
1053 | 0 | return e_Range; |
1054 | 0 | } |
1055 | | |
1056 | 0 | pcur_paper->offset_portrait = pl_get_int16(plogpage->LeftOffset) * 10; |
1057 | 0 | pcur_paper->offset_landscape = pl_get_int16(plogpage->TopOffset) * 10; |
1058 | |
|
1059 | 0 | code = new_page_size(pcs, pcur_paper, false, false); |
1060 | 0 | if (code < 0) return code; |
1061 | | |
1062 | 0 | code = gs_erasepage(pcs->pgs); |
1063 | 0 | pcs->page_marked = false; |
1064 | 0 | return code; |
1065 | 0 | } |
1066 | | |
1067 | | /* |
1068 | | * Custom Paper Width/Length |
1069 | | * from Windows Driver "HP Universal Printing PCL 5" (.GPD files) and |
1070 | | * http://www.office.xerox.com/support/dctips/dc10cc0471.pdf |
1071 | | * |
1072 | | * ESC & f <decipoints> I |
1073 | | * |
1074 | | */ |
1075 | | static int |
1076 | | set_paper_width(pcl_args_t * pargs, pcl_state_t * pcs) |
1077 | 146 | { |
1078 | 146 | uint decipoints = uint_arg(pargs); |
1079 | 146 | bool found = false; |
1080 | 146 | int i; |
1081 | 146 | pcl_paper_size_t *psize; |
1082 | | |
1083 | 1.89k | for (i = 0; i < pcl_paper_type_count; i++) { |
1084 | 1.89k | if (101 == PAPER_SIZES[i].tag) { |
1085 | 146 | psize = &(PAPER_SIZES[i].psize); |
1086 | 146 | found = true; |
1087 | 146 | break; |
1088 | 146 | } |
1089 | 1.89k | } |
1090 | | |
1091 | 146 | if (found) |
1092 | 146 | psize->width = decipoints * 10L; |
1093 | 0 | else |
1094 | 0 | gs_warn("Page table does not contain a custom entry"); |
1095 | | |
1096 | 146 | return 0; |
1097 | 146 | } |
1098 | | |
1099 | | int |
1100 | | pcl_set_custom_paper_size(pcl_state_t *pcs, pcl_paper_size_t *p) |
1101 | 0 | { |
1102 | 0 | pcl_paper_size_t *psize = NULL; |
1103 | 0 | bool found = false; |
1104 | 0 | int i; |
1105 | |
|
1106 | 0 | for (i = 0; i < pcl_paper_type_count; i++) { |
1107 | 0 | if (101 == PAPER_SIZES[i].tag) { |
1108 | 0 | psize = &(PAPER_SIZES[i].psize); |
1109 | 0 | found = true; |
1110 | 0 | break; |
1111 | 0 | } |
1112 | 0 | } |
1113 | 0 | if (found) |
1114 | 0 | *psize = *p; |
1115 | 0 | else |
1116 | | /* this should never happen - the custom paper size is |
1117 | | always in the table */ |
1118 | 0 | return -1; |
1119 | | |
1120 | 0 | return new_logical_page(pcs, 0, psize, false, false); |
1121 | 0 | } |
1122 | | /* |
1123 | | * ESC & f <decipoints> J |
1124 | | */ |
1125 | | |
1126 | | static int |
1127 | | set_paper_length(pcl_args_t * pargs, pcl_state_t * pcs) |
1128 | 0 | { |
1129 | 0 | uint decipoints = uint_arg(pargs); |
1130 | 0 | bool found = false; |
1131 | 0 | int i; |
1132 | 0 | pcl_paper_size_t *psize; |
1133 | |
|
1134 | 0 | for (i = 0; i < pcl_paper_type_count; i++) { |
1135 | 0 | if (101 == PAPER_SIZES[i].tag) { |
1136 | 0 | psize = &(PAPER_SIZES[i].psize); |
1137 | 0 | found = true; |
1138 | 0 | break; |
1139 | 0 | } |
1140 | 0 | } |
1141 | 0 | if (found) |
1142 | 0 | psize->height = decipoints * 10L; |
1143 | 0 | else |
1144 | | /* never happens the, custom paper size is always in the |
1145 | | table */ |
1146 | 0 | return -1; |
1147 | 0 | return 0; |
1148 | 0 | } |
1149 | | |
1150 | | /* |
1151 | | * (From PCL5 Comparison Guide, p. 1-99) |
1152 | | * |
1153 | | * ESC * o <quality> Q |
1154 | | * |
1155 | | * Set print quality. |
1156 | | * |
1157 | | * We don't implement this command, it appears to only be supported on |
1158 | | * DeskJet 1200C, an obsolete inkjet, which is not a target of |
1159 | | * emulation. |
1160 | | */ |
1161 | | |
1162 | | static int |
1163 | | pcl_print_quality(pcl_args_t * pargs, pcl_state_t * pcs) |
1164 | 0 | { |
1165 | |
|
1166 | 0 | return_error(e_Unimplemented); |
1167 | 0 | } |
1168 | | |
1169 | | /* |
1170 | | * Initialization |
1171 | | */ |
1172 | | static int |
1173 | | pcpage_do_registration(pcl_parser_state_t * pcl_parser_state, gs_memory_t * pmem /* ignored */ |
1174 | | ) |
1175 | 18.4k | { |
1176 | | /* Register commands */ |
1177 | 18.4k | DEFINE_CLASS('&') { |
1178 | 18.4k | 'l', 'A', |
1179 | 18.4k | PCL_COMMAND("Page Size", |
1180 | 18.4k | set_page_size, pca_neg_ok | pca_big_ignore) |
1181 | 18.4k | }, { |
1182 | 18.4k | 'l', 'H', |
1183 | 18.4k | PCL_COMMAND("Paper Source", |
1184 | 18.4k | set_paper_source, pca_neg_ok | pca_big_ignore) |
1185 | 18.4k | }, { |
1186 | 18.4k | 'l', 'U', |
1187 | 18.4k | PCL_COMMAND("Left Offset Registration", |
1188 | 18.4k | set_left_offset_registration, |
1189 | 18.4k | pca_neg_ok | pca_big_ignore) |
1190 | 18.4k | }, { |
1191 | 18.4k | 'l', 'Z', |
1192 | 18.4k | PCL_COMMAND("Top Offset Registration", |
1193 | 18.4k | set_top_offset_registration, |
1194 | 18.4k | pca_neg_ok | pca_big_ignore) |
1195 | 18.4k | }, { |
1196 | 18.4k | 'l', 'O', |
1197 | 18.4k | PCL_COMMAND("Page Orientation", |
1198 | 18.4k | set_logical_page_orientation, |
1199 | 18.4k | pca_neg_ok | pca_big_ignore) |
1200 | 18.4k | }, { |
1201 | 18.4k | 'a', 'P', |
1202 | 18.4k | PCL_COMMAND("Print Direction", |
1203 | 18.4k | set_print_direction, pca_neg_ok | pca_big_ignore) |
1204 | 18.4k | }, { |
1205 | 18.4k | 'a', 'L', |
1206 | 18.4k | PCL_COMMAND("Left Margin", |
1207 | 18.4k | set_left_margin, pca_neg_ok | pca_big_ignore) |
1208 | 18.4k | }, { |
1209 | 18.4k | 'a', 'M', |
1210 | 18.4k | PCL_COMMAND("Right Margin", |
1211 | 18.4k | set_right_margin, pca_neg_ok | pca_big_ignore) |
1212 | 18.4k | }, { |
1213 | 18.4k | 'l', 'E', |
1214 | 18.4k | PCL_COMMAND("Top Margin", |
1215 | 18.4k | set_top_margin, pca_neg_ok | pca_big_ignore) |
1216 | 18.4k | }, { |
1217 | 18.4k | 'l', 'F', |
1218 | 18.4k | PCL_COMMAND("Text Length", |
1219 | 18.4k | set_text_length, pca_neg_ok | pca_big_ignore) |
1220 | 18.4k | }, { |
1221 | 18.4k | 'l', 'L', |
1222 | 18.4k | PCL_COMMAND("Perforation Skip", |
1223 | 18.4k | set_perforation_skip, pca_neg_ok | pca_big_ignore) |
1224 | 18.4k | }, { |
1225 | 18.4k | 'l', 'M', |
1226 | 18.4k | PCL_COMMAND("Media Type", |
1227 | 18.4k | pcl_media_type, pca_neg_ok | pca_big_ignore) |
1228 | 18.4k | }, { |
1229 | 18.4k | 'a', 'W', PCL_COMMAND("Set Logical Page", set_logical_page, pca_bytes) |
1230 | 18.4k | }, END_CLASS DEFINE_CLASS('&') { |
1231 | 18.4k | 'f', 'I', |
1232 | 18.4k | PCL_COMMAND("Set Custom Paper Width", |
1233 | 18.4k | set_paper_width, pca_neg_ok | pca_big_ignore) |
1234 | 18.4k | }, { |
1235 | 18.4k | 'f', 'J', |
1236 | 18.4k | PCL_COMMAND("Set Custom Paper Length", |
1237 | 18.4k | set_paper_length, pca_neg_ok | pca_big_ignore) |
1238 | 18.4k | }, END_CLASS |
1239 | 18.4k | DEFINE_ESCAPE('9', |
1240 | 18.4k | "Clear Horizontal Margins", clear_horizontal_margins) |
1241 | | |
1242 | 18.4k | DEFINE_CLASS_COMMAND_ARGS('*', |
1243 | 18.4k | 'o', |
1244 | 18.4k | 'Q', |
1245 | 18.4k | "Print Quality", |
1246 | 18.4k | pcl_print_quality, |
1247 | 18.4k | pca_neg_ok | pca_big_ignore) |
1248 | 18.4k | return 0; |
1249 | 18.4k | } |
1250 | | |
1251 | | pcl_paper_size_t * |
1252 | | pcl_get_default_paper(pcl_state_t * pcs) |
1253 | 62.5k | { |
1254 | 62.5k | int i; |
1255 | 62.5k | pjl_envvar_t *pwidth = pjl_proc_get_envvar(pcs->pjls, "paperwidth"); |
1256 | 62.5k | pjl_envvar_t *plength = pjl_proc_get_envvar(pcs->pjls, "paperlength"); |
1257 | 62.5k | pjl_envvar_t *psize = pjl_proc_get_envvar(pcs->pjls, "paper"); |
1258 | | |
1259 | | /* build the state's paper table if it doesn't exist */ |
1260 | 62.5k | if (!pcs->ppaper_type_table) |
1261 | 18.5k | pcs->ppaper_type_table = |
1262 | 18.5k | (pcl_paper_type_t *) gs_alloc_bytes(pcs->memory, |
1263 | 62.5k | sizeof(paper_types_proto), |
1264 | 62.5k | "Paper Table"); |
1265 | | |
1266 | 62.5k | if (!pcs->ppaper_type_table) |
1267 | | /* should never fail */ |
1268 | 0 | return NULL; |
1269 | | |
1270 | | /* restore default table values - this would only reset custom paper sizes */ |
1271 | 62.5k | memcpy(pcs->ppaper_type_table, paper_types_proto, |
1272 | 62.5k | sizeof(paper_types_proto)); |
1273 | | |
1274 | 62.5k | pcs->wide_a4 = false; |
1275 | 62.5k | if (pcs->page_set_on_command_line || (*pwidth && *plength)) { |
1276 | 1.37k | for (i = 0; i < pcl_paper_type_count; i++) |
1277 | 1.37k | if (!pjl_proc_compare(pcs->pjls, "custom", PAPER_SIZES[i].pname)) { |
1278 | 106 | if (pcs->page_set_on_command_line) { |
1279 | 0 | PAPER_SIZES[i].psize.width = |
1280 | 0 | (coord)(gs_currentdevice(pcs->pgs)->MediaSize[0] * 100); |
1281 | 0 | PAPER_SIZES[i].psize.height = |
1282 | 0 | (coord)(gs_currentdevice(pcs->pgs)->MediaSize[1] * 100); |
1283 | 106 | } else { |
1284 | 106 | PAPER_SIZES[i].psize.width = atol(pwidth) * 10L; |
1285 | 106 | PAPER_SIZES[i].psize.height = atol(plength) * 10L; |
1286 | 106 | } |
1287 | | /* just a guess, values copied from letter entry in |
1288 | | table PAPER_SIZES. NB offsets should be 0 for |
1289 | | RTL. */ |
1290 | 106 | PAPER_SIZES[i].psize.offset_portrait = 75 * 24L; |
1291 | 106 | PAPER_SIZES[i].psize.offset_landscape = 60 * 24L; |
1292 | 106 | return &(PAPER_SIZES[i].psize); |
1293 | 106 | } |
1294 | 106 | } |
1295 | | |
1296 | 124k | for (i = 0; i < pcl_paper_type_count; i++) |
1297 | 124k | if (!pjl_proc_compare(pcs->pjls, psize, PAPER_SIZES[i].pname)) { |
1298 | | /* set wide a4, only used if the paper is a4 */ |
1299 | 62.4k | if (!pjl_proc_compare |
1300 | 62.4k | (pcs->pjls, pjl_proc_get_envvar(pcs->pjls, "widea4"), "YES")) |
1301 | 0 | pcs->wide_a4 = true; |
1302 | 62.4k | return &(PAPER_SIZES[i].psize); |
1303 | 62.4k | } |
1304 | 62.4k | dmprintf(pcs->memory, |
1305 | 2 | "system does not support requested paper setting\n"); |
1306 | 2 | return &(PAPER_SIZES[1].psize); |
1307 | 62.4k | } |
1308 | | |
1309 | | static int |
1310 | | pcpage_do_reset(pcl_state_t * pcs, pcl_reset_type_t type) |
1311 | 62.4k | { |
1312 | 62.4k | int code = 0; |
1313 | | |
1314 | | /* NB hack for snippet mode */ |
1315 | 62.4k | if (pcs->end_page != pcl_end_page_top) { |
1316 | 0 | pcs->xfm_state.print_dir = 0; |
1317 | 0 | pcs->xfm_state.left_offset_cp = 0.0; |
1318 | 0 | pcs->xfm_state.top_offset_cp = 0.0; |
1319 | 0 | update_xfm_state(pcs, 0); |
1320 | | |
1321 | | /* if we are in snippet mode, ESC E (reset) will set the |
1322 | | default margins otherwise we should use the unusual |
1323 | | passthrough margins (1/6") see above */ |
1324 | 0 | if (type & pcl_reset_printer) |
1325 | 0 | reset_margins(pcs, false); |
1326 | 0 | else |
1327 | 0 | reset_margins(pcs, true); |
1328 | 0 | return 0; |
1329 | 0 | } |
1330 | | |
1331 | 62.4k | if ((type & (pcl_reset_initial | pcl_reset_printer)) != 0) { |
1332 | 62.4k | pcl_paper_size_t *psize = pcl_get_default_paper(pcs); |
1333 | 62.4k | if (psize == NULL) return_error(gs_error_VMerror); |
1334 | | |
1335 | 62.4k | pcs->paper_source = 0; /* ??? */ |
1336 | 62.4k | pcs->xfm_state.left_offset_cp = 0.0; |
1337 | 62.4k | pcs->xfm_state.top_offset_cp = 0.0; |
1338 | 62.4k | pcs->perforation_skip = 1; |
1339 | 62.4k | code = new_logical_page(pcs, |
1340 | 62.4k | !pjl_proc_compare(pcs->pjls, |
1341 | 62.4k | pjl_proc_get_envvar(pcs->pjls, |
1342 | 62.4k | "orientation"), |
1343 | 62.4k | "portrait") ? 0 : 1, |
1344 | 62.4k | psize, |
1345 | 62.4k | (type & pcl_reset_initial) != 0, false); |
1346 | 62.4k | if (code < 0) goto cleanup; |
1347 | 62.4k | } else if ((type & pcl_reset_overlay) != 0) { |
1348 | 0 | pcs->perforation_skip = 1; |
1349 | 0 | update_xfm_state(pcs, 0); |
1350 | 0 | reset_margins(pcs, false); |
1351 | 0 | pcl_xfm_reset_pcl_pat_ref_pt(pcs); |
1352 | 0 | } else if ((type & pcl_reset_permanent) != 0) { |
1353 | 0 | goto cleanup; |
1354 | 0 | } |
1355 | 62.4k | return 0; |
1356 | | |
1357 | 0 | cleanup: |
1358 | 0 | if (pcs->ppaper_type_table) { |
1359 | | gs_free_object(pcs->memory, pcs->ppaper_type_table, "Paper Table"); |
1360 | 0 | pcs->ppaper_type_table = 0; |
1361 | 0 | } |
1362 | 0 | return code; |
1363 | 62.4k | } |
1364 | | |
1365 | | const pcl_init_t pcpage_init = { pcpage_do_registration, pcpage_do_reset, 0 }; |