summaryrefslogtreecommitdiff
blob: dee81bb541d558495bda5cfd06460e9cdafdc65a (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
/* Copyright (C) 1999, 2000 Norihito Ohmori.

   Ghostscript driver for Ricoh RPDL printer.

   This software is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
   to anyone for the consequences of using it or for whether it serves any
   particular purpose or works at all, unless he says so in writing.  Refer
   to the GNU General Public License for full details.

   Everyone is granted permission to copy, modify and redistribute
   this software, but only under the conditions described in the GNU
   General Public License.  A copy of this license is supposed to have been
   given to you along with this software so you can know your rights and
   responsibilities.  It should be in a file named COPYING.  Among other
   things, the copyright notice and this notice must be preserved on all
   copies.
 */

/*$Id: gdevrpdl.c,v 1.1 2002/10/12 23:24:34 tillkamppeter Exp $ */
/* Ricoh RPDL driver for Ghostscript */

#include "gdevlprn.h"
#include "gdevlips.h"
#include <stdlib.h>

#define DPI 240

/* The device descriptors */
static dev_proc_open_device(rpdl_open);
static dev_proc_close_device(rpdl_close);
static dev_proc_print_page_copies(rpdl_print_page_copies);
static dev_proc_image_out(rpdl_image_out);
static void rpdl_printer_initialize(gx_device_printer * pdev, gp_file * prn_stream, int num_copies);
static void rpdl_paper_set(gx_device_printer * pdev, gp_file * prn_stream);

static gx_device_procs rpdl_prn_procs =
lprn_procs(rpdl_open, gdev_prn_output_page, rpdl_close);

gx_device_lprn far_data gs_rpdl_device =
lprn_device(gx_device_lprn, rpdl_prn_procs, "rpdl",
            DPI, DPI, 0.0, 0.0, 0.0, 0.0, 1,
            rpdl_print_page_copies, rpdl_image_out);

#define ppdev ((gx_device_printer *)pdev)

/* Open the printer. */
static int
rpdl_open(gx_device * pdev)
{
    int xdpi = pdev->x_pixels_per_inch;
    int ydpi = pdev->y_pixels_per_inch;

    /* Resolution Check */
    if (xdpi != ydpi)
        return_error(gs_error_rangecheck);
    if (xdpi != 240 && xdpi != 400 && xdpi != 600)
        return_error(gs_error_rangecheck);

    return gdev_prn_open(pdev);
}

static int
rpdl_close(gx_device * pdev)
{
    int code = gdev_prn_open_printer(pdev, 1);
    if (code < 0)
        return code;
    if (ppdev->Duplex && (pdev->PageCount & 1)) {
        gp_fprintf(ppdev->file, "\014"); /* Form Feed */
    }
    return gdev_prn_close(pdev);
}

static int
rpdl_print_page_copies(gx_device_printer * pdev, gp_file * prn_stream, int num_coipes)
{
    gx_device_lprn *const lprn = (gx_device_lprn *) pdev;
    int code = 0;
    int bpl = gdev_mem_bytes_per_scan_line(pdev);
    int maxY = lprn->BlockLine / lprn->nBh * lprn->nBh;

    /* printer initialize */
    if (pdev->PageCount == 0)
        rpdl_printer_initialize(pdev, prn_stream, num_coipes);

    if (!(lprn->CompBuf = gs_malloc(pdev->memory->non_gc_memory, bpl * 3 / 2 + 1, maxY, "rpdl_print_page_copies(CompBuf)")))
        return_error(gs_error_VMerror);

    lprn->NegativePrint = false; /* Not Support */

    code = lprn_print_image(pdev, prn_stream);
    if (code < 0)
        return code;

    gs_free(pdev->memory->non_gc_memory, lprn->CompBuf, bpl * 3 / 2 + 1, maxY, "rpdl_print_page_copies(CompBuf)");

    gp_fprintf(prn_stream, "\014");	/* Form  Feed */

    return code;
}

/* Output data */
static void
rpdl_image_out(gx_device_printer * pdev, gp_file * prn_stream, int x, int y, int width, int height)
{
    gx_device_lprn *const lprn = (gx_device_lprn *) pdev;
    int Len;

    Len = lips_mode3format_encode(lprn->TmpBuf, lprn->CompBuf, width / 8 * height);

    if (Len < width / 8 * height) {
      if (pdev->x_pixels_per_inch == 240) {
        /* Unit Size is 1/720 inch */
        gp_fprintf(prn_stream, "\033\022G3,%d,%d,,4,%d,%d,%d@",
                width, height, x * 3, y * 3, Len);
      } else {
        gp_fprintf(prn_stream, "\033\022G3,%d,%d,,4,%d,%d,%d@",
                width, height, x, y, Len);
      }
      gp_fwrite(lprn->CompBuf, 1, Len, prn_stream);
    } else { /* compression result is bad. So, raw data is used. */
      if (pdev->x_pixels_per_inch == 240) {
        /* Unit Size is 1/720 inch */
        gp_fprintf(prn_stream, "\033\022G3,%d,%d,,,%d,%d@",
                width, height, x * 3, y * 3);
        gp_fwrite(lprn->TmpBuf, 1, width / 8 * height, prn_stream);
      } else {
        gp_fprintf(prn_stream, "\033\022G3,%d,%d,,,%d,%d@",
                width, height, x, y);
        gp_fwrite(lprn->TmpBuf, 1, width / 8 * height, prn_stream);
      }
    }
}

/* output printer initialize code */

/* ------ Internal routines ------ */

static void
rpdl_printer_initialize(gx_device_printer * pdev, gp_file * prn_stream, int num_copies)
{
    gx_device_lprn *const lprn = (gx_device_lprn *) pdev;
    int xdpi = (int) pdev->x_pixels_per_inch;

    /* Initialize */
    gp_fprintf(prn_stream, "\033\022!@R00\033 "); /* Change to RPDL Mode */
    gp_fprintf(prn_stream, "\0334"); /* Graphic Mode kaijyo */
    gp_fprintf(prn_stream, "\033\022YP,2 "); /* Select RPDL Mode */
    gp_fprintf(prn_stream, "\033\022YB,2 "); /* Printable Area - Maximum */
    gp_fprintf(prn_stream, "\033\022YK,1 "); /* Left Margin - 0 mm */
    gp_fprintf(prn_stream, "\033\022YL,1 "); /* Top Margin - 0 mm */
    gp_fprintf(prn_stream, "\033\022YM,1 "); /* 100 % */
    gp_fprintf(prn_stream, "\033\022YQ,2 "); /* Page Length - Maximum */

    /* Paper Size Selection */
    rpdl_paper_set(pdev, prn_stream);

    /* Option Setting */
    /* Duplex Setting */
    if (pdev->Duplex_set > 0) {
        if (pdev->Duplex) {
            gp_fprintf(prn_stream, "\033\02261,");
            if (lprn->Tumble == 0)
                gp_fprintf(prn_stream, "\033\022YA01,2 ");
            else
                gp_fprintf(prn_stream, "\033\022YA01,1 ");
        } else
            gp_fprintf(prn_stream, "\033\02260,");
    }

    /* Resolution and Unit Setting */
    /* Resolution Seting */
    switch(xdpi) {
    case 600:
      gp_fprintf(prn_stream, "\033\022YA04,3 ");
      break;
    case 400:
      gp_fprintf(prn_stream, "\033\022YA04,1 ");
      break;
    default: /* 240 dpi */
      gp_fprintf(prn_stream, "\033\022YA04,2 ");
      break;
    }

    /* Unit Setting */
    /* Graphics Unit */
    switch(xdpi) {
    case 600:
      gp_fprintf(prn_stream, "\033\022YW,3 ");
      break;
    case 400:
      gp_fprintf(prn_stream, "\033\022YW,1 ");
      break;
    default: /* 240 dpi */
      gp_fprintf(prn_stream, "\033\022YW,2 ");
      break;
    }

    /* Spacing Unit */
    switch(xdpi) {
    case 600:
      gp_fprintf(prn_stream, "\033\022Q5 ");
      break;
    case 400:
      gp_fprintf(prn_stream, "\033\022Q4 ");
      break;
    default: /* 240 dpi */
      gp_fprintf(prn_stream, "\033\022Q0 ");
      break;
    }

    /* Cartecian Unit */
    switch(xdpi) {
    case 600:
      gp_fprintf(prn_stream, "\033\022#4 ");
      break;
    case 400:
      gp_fprintf(prn_stream, "\033\022#2 ");
      break;
    }

    /* Paper Setting */
    if (pdev->MediaSize[0] > pdev->MediaSize[1])
      gp_fprintf(prn_stream, "\033\022D2 "); /* landscape */
    else
      gp_fprintf(prn_stream, "\033\022D1 "); /* portrait */

    /* Number of Copies */
    gp_fprintf(prn_stream, "\033\022N%d ", num_copies);
}

static void
rpdl_paper_set(gx_device_printer * pdev, gp_file * prn_stream)
{
    int width, height, w, h;

    /* Page size match tolerance in points */
    #define TOL 5

    width = pdev->MediaSize[0];
    height = pdev->MediaSize[1];

    if (width < height) {
        w = width;
        h = height;
    } else {
        w = height;
        h = width;
    }

    if (abs(w - 1684) <= TOL && abs(h - 2380) <= TOL) /* A1 */
      gp_fprintf(prn_stream, "\033\02251@A1R\033 ");
    else if (abs(w - 1190) <= TOL && abs(h - 1684) <= TOL) { /* A2 */
      gp_fprintf(prn_stream, "\033\02251@A2R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A2\033 ");
    } else if (abs(w - 842) <= TOL && abs(h - 1190) <= TOL) { /* A3 */
      gp_fprintf(prn_stream, "\033\02251@A3R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A3\033 ");
    } else if (abs(w - 595) <= TOL && abs(h - 842) <= TOL) { /* A4 */
      gp_fprintf(prn_stream, "\033\02251@A4R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A4\033 ");
    } else if (abs(w - 421) <= TOL && abs(h - 595) <= TOL) { /* A5 */
      gp_fprintf(prn_stream, "\033\02251@A5R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A5\033 ");
    } else if (abs(w - 297) <= TOL && abs(h - 421) <= TOL) { /* A6 */
      gp_fprintf(prn_stream, "\033\02251@A6R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A6\033 ");
    } else if (abs(w - 729) <= TOL && abs(h - 1032) <= TOL) { /* B4 */
      gp_fprintf(prn_stream, "\033\02251@B4R\033 ");
      gp_fprintf(prn_stream, "\033\02251@B4\033 ");
    } else if (abs(w - 516) <= TOL && abs(h - 729) <= TOL) { /* B5 */
      gp_fprintf(prn_stream, "\033\02251@B5R\033 ");
      gp_fprintf(prn_stream, "\033\02251@B5\033 ");
    } else if (abs(w - 363) <= TOL && abs(h - 516) <= TOL) { /* B6 */
      gp_fprintf(prn_stream, "\033\02251@A6R\033 ");
      gp_fprintf(prn_stream, "\033\02251@A6\033 ");
    } else if (abs(w - 612) <= TOL && abs(h - 792) <= TOL) { /* Letter */
      gp_fprintf(prn_stream, "\033\02251@LTR\033 ");
      gp_fprintf(prn_stream, "\033\02251@LT\033 ");
    } else if (abs(w - 612) <= TOL && abs(h - 1008) <= TOL) { /* Legal */
      gp_fprintf(prn_stream, "\033\02251@LGR\033 ");
      gp_fprintf(prn_stream, "\033\02251@LG\033 ");
    } else if (abs(w - 396) <= TOL && abs(h - 612) <= TOL) { /* Half Letter */
      gp_fprintf(prn_stream, "\033\02251@HLR\033 ");
      gp_fprintf(prn_stream, "\033\02251@HLT\033 ");
    } else if (abs(w - 792) <= TOL && abs(h - 1224) <= TOL) { /* Ledger */
      gp_fprintf(prn_stream, "\033\02251@DLT\033 ");
      gp_fprintf(prn_stream, "\033\02251@DLR\033 ");
    } else { /* Free Size (mm) */
      gp_fprintf(prn_stream, "\033\022?5%d,%d\033 ",
              (int)((w * 25.4) / 72),
              (int)((h * 25.4) / 72));
    }
}