summaryrefslogtreecommitdiff
path: root/devices/gdevdljm.c
blob: 8b81ad6d2d8c53a9ba75522d1843b0c5388d6b06 (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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

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

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

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

/* Generic monochrome H-P DeskJet/LaserJet driver */
#include "gdevprn.h"
#include "gdevdljm.h"

/*
 * Thanks for various improvements to:
 *      Jim Mayer (mayer@wrc.xerox.com)
 *      Jan-Mark Wams (jms@cs.vu.nl)
 *      Frans van Hoesel (hoesel@chem.rug.nl)
 *      George Cameron (g.cameron@biomed.abdn.ac.uk)
 *      Nick Duffek (nsd@bbc.com)
 * Thanks for the FS-600 driver to:
 *	Peter Schildmann (peter.schildmann@etechnik.uni-rostock.de)
 * Thanks for the LJIIID duplex capability to:
 *      PDP (Philip) Brown (phil@3soft-uk.com)
 * Thanks for the OCE 9050 driver to:
 *      William Bader (wbader@EECS.Lehigh.Edu)
 * Thanks for the LJ4D duplex capability to:
 *	Les Johnson <les@infolabs.com>
 */

/* See gdevdljm.h for the definitions of the PCL_ features. */

/* The number of blank lines that make it worthwhile to reposition */
/* the cursor. */
#define MIN_SKIP_LINES 7

/* We round up the LINE_SIZE to a multiple of a ulong for faster scanning. */
#define W sizeof(word)

/* Send a page to the printer. */
int
dljet_mono_print_page(gx_device_printer * pdev, gp_file * prn_stream,
                      int dots_per_inch, int features, const char *page_init)
{
    return dljet_mono_print_page_copies(pdev, prn_stream, 1, dots_per_inch,
                                        features, page_init, page_init, false);
}
int
dljet_mono_print_page_copies(gx_device_printer * pdev, gp_file * prn_stream,
                             int num_copies, int dots_per_inch, int features,
                             const char *odd_page_init, const char *even_page_init, bool tumble)
{
    int line_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
    int line_size_words = (line_size + W - 1) / W;
    uint storage_size_words = line_size_words * 8;	/* data, out_row, out_row_alt, prev_row */
    word *storage;
    word
        *data_words,
        *out_row_words,
        *out_row_alt_words,
        *prev_row_words;
#define data ((byte *)data_words)
#define out_row ((byte *)out_row_words)
#define out_row_alt ((byte *)out_row_alt_words)
#define prev_row ((byte *)prev_row_words)
    byte *out_data;
    int x_dpi = (int)pdev->x_pixels_per_inch;
    int y_dpi = (int)pdev->y_pixels_per_inch;
    int y_dots_per_pixel = dots_per_inch / y_dpi;
    int num_rows = dev_print_scan_lines(pdev);

    int out_count;
    int compression = -1;
    static const char *const from2to3 = "\033*b3M";
    static const char *const from3to2 = "\033*b2M";
    int penalty_from2to3 = strlen(from2to3);
    int penalty_from3to2 = strlen(from3to2);
    int paper_size = gdev_pcl_paper_size((gx_device *) pdev);
    int page_orientation = gdev_pcl_page_orientation((gx_device *) pdev);
    int code = 0;
    bool dup = pdev->Duplex;
    bool dupset = pdev->Duplex_set >= 0;

    if (num_copies != 1 && !(features & PCL_CAN_PRINT_COPIES))
        return gx_default_print_page_copies(pdev, prn_stream, num_copies);
    storage =
        (ulong *)gs_alloc_byte_array(pdev->memory, storage_size_words, W,
                                     "hpjet_print_page");
    if (storage == 0)		/* can't allocate working area */
        return_error(gs_error_VMerror);
    data_words = storage;
    out_row_words = data_words + (line_size_words * 2);
    out_row_alt_words = out_row_words + (line_size_words * 2);
    prev_row_words = out_row_alt_words + (line_size_words * 2);
    /* Clear temp storage */
    memset(data, 0, storage_size_words * W);

    /* Initialize printer. */
    if (pdev->PageCount == 0) {
        if (features & HACK__IS_A_LJET4PJL) {
            gp_fputs("\033%-12345X@PJL\r\n@PJL ENTER LANGUAGE = PCL\r\n",
                  prn_stream);
        }
        gp_fputs("\033E", prn_stream);	/* reset printer */
        /* If the printer supports it, set the paper size */
        /* based on the actual requested size. */
        gp_fprintf(prn_stream, "\033&l%dO", page_orientation);
        if (features & PCL_CAN_SET_PAPER_SIZE) {
            gp_fprintf(prn_stream, "\033&l%dA", paper_size);
        }
        /* If printer can duplex, set duplex mode appropriately. */
        if (features & PCL_HAS_DUPLEX) {
            if (dupset && dup && !tumble)
                gp_fputs("\033&l1S", prn_stream);
       else if (dupset && dup && tumble)
                gp_fputs("\033&l2S", prn_stream);
            else if (dupset && !dup)
                gp_fputs("\033&l0S", prn_stream);
            else		/* default to duplex for this printer */
                gp_fputs("\033&l1S", prn_stream);
        }
    }
    /* Put out per-page initialization. */
    /*
       Modified by karsten@sengebusch.de
       in duplex mode the sheet is alread in process, so there are some
       commands which must not be sent to the printer for the 2nd page,
       as this commands will cause the printer to eject the sheet with
       only the 1st page printed. This commands are:
       \033&l%dA (setting paper size)
       \033&l%dH (setting paper tray)
       in simplex mode we set this parameters for each page,
       in duplex mode we set this parameters for each odd page
    */

    if ((features & PCL_HAS_DUPLEX) && dupset && dup) {
       /* We are printing duplex, so change margins as needed */
       if (( (pdev->PageCount/num_copies)%2)==0) {
          gp_fprintf(prn_stream, "\033&l%dO", page_orientation);
          if (features & PCL_CAN_SET_PAPER_SIZE) {
              gp_fprintf(prn_stream, "\033&l%dA", paper_size);
          }
          gp_fputs("\033&l0l0E", prn_stream);
          gp_fputs(odd_page_init, prn_stream);
       } else
          gp_fputs(even_page_init, prn_stream);
    } else {
        gp_fprintf(prn_stream, "\033&l%dO", page_orientation);
        if (features & PCL_CAN_SET_PAPER_SIZE){
            gp_fprintf(prn_stream, "\033&l%dA", paper_size);
        }
        gp_fputs("\033&l0l0E", prn_stream);
        gp_fputs(odd_page_init, prn_stream);
    }

    gp_fprintf(prn_stream, "\033&l%dX", num_copies);	/* # of copies */

    /* End raster graphics, position cursor at top. */
    gp_fputs("\033*rB\033*p0x0Y", prn_stream);

    /* The DeskJet and DeskJet Plus reset everything upon */
    /* receiving \033*rB, so we must reinitialize graphics mode. */
    if (features & PCL_END_GRAPHICS_DOES_RESET) {
        gp_fputs(odd_page_init, prn_stream); /* Assume this does the right thing */
        gp_fprintf(prn_stream, "\033&l%dX", num_copies);	/* # of copies */
    }

    /* Set resolution. */
    gp_fprintf(prn_stream, "\033*t%dR", x_dpi);

    /* Send each scan line in turn */
    {
        int lnum;
        int num_blank_lines = 0;
        word rmask = ~(word) 0 << (-pdev->width & (W * 8 - 1));

        /* Transfer raster graphics. */
        for (lnum = 0; lnum < num_rows; lnum++) {
            register word *end_data =
            data_words + line_size_words;

            code = gdev_prn_copy_scan_lines(pdev, lnum,
                                            (byte *) data, line_size);
            if (code < 0)
                break;
            /* Mask off 1-bits beyond the line width. */
            end_data[-1] &= rmask;
            /* Remove trailing 0s. */
            while (end_data > data_words && end_data[-1] == 0)
                end_data--;
            if (end_data == data_words) {	/* Blank line */
                num_blank_lines++;
                continue;
            }
            /* We've reached a non-blank line. */
            /* Put out a spacing command if necessary. */
            if (num_blank_lines == lnum) {
                /* We're at the top of a page. */
                if (features & PCL_ANY_SPACING) {
                    if (num_blank_lines > 0)
                        gp_fprintf(prn_stream, "\033*p+%dY",
                                num_blank_lines * y_dots_per_pixel);
                    /* Start raster graphics. */
                    gp_fputs("\033*r1A", prn_stream);
                } else if (features & PCL_MODE_3_COMPRESSION) {
                    /* Start raster graphics. */
                    gp_fputs("\033*r1A", prn_stream);
#if 1				/* don't waste paper */
                    if (num_blank_lines > 0)
                        gp_fputs("\033*b0W", prn_stream);
                    num_blank_lines = 0;
#else
                    for (; num_blank_lines; num_blank_lines--)
                        fputs("\033*b0W", prn_stream);
#endif
                } else {
                    /* Start raster graphics. */
                    gp_fputs("\033*r1A", prn_stream);
                    for (; num_blank_lines; num_blank_lines--)
                        gp_fputs("\033*bW", prn_stream);
                }
            }
            /* Skip blank lines if any */
            else if (num_blank_lines != 0) {
                /*
                 * Moving down from current position causes head motion
                 * on the DeskJet, so if the number of lines is small,
                 * we're better off printing blanks.
                 */
                /*
                 * For Canon LBP4i and some others, <ESC>*b<n>Y doesn't
                 * properly clear the seed row if we are in compression mode
                 * 3.
                 */
                if ((num_blank_lines < MIN_SKIP_LINES && compression != 3) ||
                    !(features & PCL_ANY_SPACING)
                    ) {
                    bool mode_3ns =
                        (features & PCL_MODE_3_COMPRESSION) &&
                        !(features & PCL_ANY_SPACING);

                    if (mode_3ns && compression != 2) {
                        /* Switch to mode 2 */
                        gp_fputs(from3to2, prn_stream);
                        compression = 2;
                    }
                    if (features & PCL_MODE_3_COMPRESSION) {
                        /* Must clear the seed row. */
                        gp_fputs("\033*b1Y", prn_stream);
                        num_blank_lines--;
                    }
                    if (mode_3ns) {
                        for (; num_blank_lines; num_blank_lines--)
                            gp_fputs("\033*b0W", prn_stream);
                    } else {
                        for (; num_blank_lines; num_blank_lines--)
                            gp_fputs("\033*bW", prn_stream);
                    }
                } else if (features & PCL3_SPACING) {
                    gp_fprintf(prn_stream, "\033*p+%dY",
                               num_blank_lines * y_dots_per_pixel);
                } else {
                    gp_fprintf(prn_stream, "\033*b%dY",
                               num_blank_lines);
                }
                /* Clear the seed row (only matters for */
                /* mode 3 compression). */
                memset(prev_row, 0, line_size);
            }
            num_blank_lines = 0;

            /* Choose the best compression mode */
            /* for this particular line. */
            if (features & PCL_MODE_3_COMPRESSION) {
                /* Compression modes 2 and 3 are both */
                /* available.  Try both and see which one */
                /* produces the least output data. */
                int count3 = gdev_pcl_mode3compress(line_size, data,
                                                    prev_row, out_row);
                int count2 = gdev_pcl_mode2compress(data_words, end_data,
                                                    out_row_alt);
                int penalty3 =
                    (compression == 3 ? 0 : penalty_from2to3);
                int penalty2 =
                    (compression == 2 ? 0 : penalty_from3to2);

                if (count3 + penalty3 < count2 + penalty2) {
                    if (compression != 3)
                        gp_fputs(from2to3, prn_stream);
                    compression = 3;
                    out_data = out_row;
                    out_count = count3;
                } else {
                    if (compression != 2)
                        gp_fputs(from3to2, prn_stream);
                    compression = 2;
                    out_data = out_row_alt;
                    out_count = count2;
                }
            } else if (features & PCL_MODE_2_COMPRESSION) {
                out_data = out_row;
                out_count = gdev_pcl_mode2compress(data_words, end_data,
                                                   out_row);
            } else {
                out_data = data;
                out_count = (byte *) end_data - data;
            }

            /* Transfer the data */
            gp_fprintf(prn_stream, "\033*b%dW", out_count);
            gp_fwrite(out_data, sizeof(byte), out_count,
                      prn_stream);
        }
    }

    /* end raster graphics and eject page */
    gp_fputs("\033*rB\f", prn_stream);

    /* free temporary storage */
    gs_free_object(pdev->memory, storage, "hpjet_print_page");

    return code;
}