summaryrefslogtreecommitdiff
path: root/contrib/gdevln03.c
blob: c5f087e30c9ab77e5b8d7abc049b0867fd0be486 (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
/* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.  All rights reserved.

This file is part of Ghostscript.

Ghostscript 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 Ghostscript,
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
Ghostscript so you can know your rights and responsibilities.  It should be
in a file named COPYING or COPYLEFT.  Among other things, the copyright
notice and this notice must be preserved on all copies.
 */

/*
gdevln03.c
Ghostscript driver for DEC LN03 printer

Ulrich Mueller, Div. PPE, CERN, CH-1211 Geneva 23 <ulm@vsnhd1.cern.ch>
This code is subject to the GNU General Public License

ulm 91-02-13 created as driver for gs 2.1.1
ulm 91-07-23 adapted to gs 2.2
ulm 91-08-21 changed memory allocation to gs_malloc,
             ported to VMS (contributed by Martin Stiftinger, TU Vienna)
lpd 91-11-24 sped up by removing multiplies from inner loop
ijmp 92-04-14 add support for la75/la50 (macphed@dvinci.usask.ca)
ulm 92-09-25 support letter size paper (8.5" x 11")
bbl 93-06-10 added la70 mode (bruce@csugrad.cs.vt.edu)
lpd/ab 94-02-04 added la75plus mode (Andre_Beck@IRS.Inf.TU-Dresden.de)
pbk 94-02-28 keep lines less than 80 chars for systems where files
             typed to terminal don't work otherwise; define separate
             eject string for each device for flexibility;
             add support for CRT sixels (keegstra@tonga.gsfc.nasa.gov)
nb 1999-05-03 added dl2100 code (nick.brown@coe.int)
*/

#include "gdevprn.h"

/* Forward references */
static int sixel_print_page(gx_device_printer *pdev, gp_file *prn_stream,
                             const char *init, const char *eject);

/* The device descriptor */
static dev_proc_output_page(sixel_output_page);
static dev_proc_print_page(ln03_print_page);

/* We have to supply our own procs, since we have to intercept */
/* output_page so we can open the printer in text mode. */
static void
sixel_initialize_device_procs(gx_device *dev)
{
    gdev_prn_initialize_device_procs_mono(dev);

    set_dev_proc(dev, output_page, sixel_output_page);
}

#ifdef A4
#  define BOTTOM_MARGIN 0.5
#else
#  define BOTTOM_MARGIN 0.4
#endif
gx_device_printer gs_ln03_device =
    prn_device(sixel_initialize_device_procs, "ln03",
               DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
               300, 300,		/* x_dpi, y_dpi */
               0, BOTTOM_MARGIN, 0, 0,	/* left, bottom, right, top margin */
               1, ln03_print_page);

/*
 * Initialization string: switch to graphics mode, 300 dpi
 * <ESC>[!p	    DECSTR	soft terminal reset
 * <ESC>[11h	    PUM		select unit of measurement
 * <ESC>[7 I	    SSU		select pixel as size unit
 * <ESC>[?52h	    DECOPM	origin is upper-left corner
 * <ESC>[0t	    DECSLPP	set maximum form length
 * <ESC>[1;2475s    DECSLRM	set left and right margins
 * <ESC>P0;0;1q			select sixel graphics mode
 * "1;1		    DECGRA	aspect ratio (1:1)
 */

#define LN03_INIT \
 "\033[!p\033[11h\033[7 I\033[?52h\033[0t\033[1;2475s\033P0;0;1q\"1;1"

/* leave sixel graphics mode, eject page
      <ESC>\		ST	string terminator
      <FF>		FF	form feed */
#define LN03_EJECT "\033\\\f"

static int
ln03_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,LN03_INIT,LN03_EJECT));
}

/*
 * DEClaser 2100 printer - very similar to the LN03.
 */
static dev_proc_print_page(dl2100_print_page);
gx_device_printer gs_dl2100_device =
    prn_device(sixel_initialize_device_procs, "dl2100",
               DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
               300, 300,		/* x_dpi, y_dpi */
               0, 0, 0, 0,	/* left, bottom, right, top margin */
               1, dl2100_print_page);

/*
 * Initialization string: same as for LN03, plus top/bottom margins.
 */

#define DL2100_INIT \
 "\033[!p\033[11h\033[7 I\033[?52h\033[0t\033[1;2475s\033[1;3510r\033P0;0;1q\"1;1"

static int
dl2100_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,DL2100_INIT,LN03_EJECT));
}

/*
 * LA50 dot matrix printer device.
 * This uses North American 8.5 x 11 inch paper size.
 */
static dev_proc_print_page(la50_print_page);
gx_device_printer gs_la50_device =
    prn_device(sixel_initialize_device_procs, "la50",
               85,
               110,
               144, 72,
               0, 0, 0.5, 0,
               1, la50_print_page);
/* LA50's use a very primitive form of initialization */

#define LA50_INIT "\033Pq"

/* leave sixel graphics mode, eject page
      <ESC>\		ST	string terminator
      <FF>		FF	form feed */
#define LA50_EJECT "\033\\\f"

static int
la50_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,LA50_INIT,LA50_EJECT));
}

/*
 * LA70 dot matrix printer device.
 * This uses North American 8.5 x 11 inch paper size.
 */
static dev_proc_print_page(la70_print_page);
gx_device_printer gs_la70_device =
    prn_device(sixel_initialize_device_procs, "la70",
               85,
               110,
               144, 144,
               0, 0, 0.5, 0,
               1, la70_print_page);

#define LA70_INIT "\033P0;0;0q\"1;1"

/* leave sixel graphics mode, eject page
      <ESC>\		ST	string terminator
      <FF>		FF	form feed */
#define LA70_EJECT "\033\\\f"

static int
la70_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,LA70_INIT,LA70_EJECT));
}

/*
 * LA75 dot matrix printer device.
 * This uses North American 8.5 x 11 inch paper size.
 */
static dev_proc_print_page(la75_print_page);
gx_device_printer gs_la75_device =
    prn_device(sixel_initialize_device_procs, "la75",
               85,
               110,
               144, 72,
               0, 0, 0.5, 0,
               1, la75_print_page);

#define LA75_INIT "\033P0;0;0q"

/* leave sixel graphics mode, eject page
      <ESC>\		ST	string terminator
      <FF>		FF	form feed */
#define LA75_EJECT "\033\\\f"

static int
la75_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,LA75_INIT,LA75_EJECT));
}

/*
 * LA75+ dot matrix printer device (24 needles).
 * This uses either A4 or US paper size.
 * Last changed: 03.02.94 -abp
 */
static dev_proc_print_page(la75plus_print_page);
gx_device_printer gs_la75plus_device =
    prn_device(sixel_initialize_device_procs, "la75plus",
               85,
               110,
               180, 180,
               0, 0, BOTTOM_MARGIN, 0,
               1, la75plus_print_page);

/*
 * Init String:
 * <ESC>c               full reset
 * <DCS>0;0;1q          start sixel printing at max resolution
 * "1;1                 aspect ratio 1:1
 */

#define LA75PLUS_INIT "\033c\033P0;0;1q\"1;1"

/* leave sixel graphics mode, eject page
      <ESC>\		ST	string terminator
      <FF>		FF	form feed */
#define LA75PLUS_EJECT "\033\\\f"

static int
la75plus_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,LA75PLUS_INIT,LA75PLUS_EJECT));
}

/*
 * CRT sixels, e.g. for display by VT240-like terminals or MSKERMIT.
 * Parameters set so MSKERMIT using sixels matches native EGA.
 * COBE/DMR prefers (145, 100, 56.8, 28.5) to match its program DPSI.
 */
static dev_proc_print_page(sxlcrt_print_page);
gx_device_printer gs_sxlcrt_device =
    prn_device(sixel_initialize_device_procs,
               "sxlcrt",
               180,
               110,
               42.6667f, 32.0,
               0, 0, 0, 0,
               1, sxlcrt_print_page);

/* Use init and eject strings similar to COBE/DMR program DQUSIXEL */
/* Add an exit Tek emulation sequence so kermit displays properly  */
#define SXLCRT_INIT "\033[?38l\033P0q"
/* leave sixel graphics mode, home cursor */
#define SXLCRT_EJECT "\033\\\033[23;0H"

static int
sxlcrt_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
    return (sixel_print_page(pdev,prn_stream,SXLCRT_INIT,SXLCRT_EJECT));
}

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

/* Open the printer in text mode before gdev_prn_output_page */
/* opens it in binary mode. */
static int
sixel_output_page(gx_device *pdev, int num_copies, int flush)
{	int code = gdev_prn_open_printer(pdev, 0);
        if ( code < 0 )
                return code;
        return gdev_prn_output_page(pdev, num_copies, flush);
}

/* Send the page to the printer. */
/* Keep all lines <= 80 chars */
static int
sixel_print_page(gx_device_printer *pdev, gp_file *prn_stream,
                 const char *init, const char *eject)
{
    byte *in, *inp;
    int lnum, lcount, l, count, empty, mask, c, oldc, line_size, in_size;
    int ccount;

    line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
    in_size = line_size * 6;
    in = (byte *)gs_malloc(pdev->memory->non_gc_memory, in_size, 1, "sixel_print_page");

    /* Check allocation */
    if (!in) return(-1);

    gp_fputs(init,prn_stream);
    ccount = strlen(init);

    /* Print lines of graphics */
    for (lnum = lcount = 0; lnum < pdev->height; lnum+=6, lcount++) {
        gdev_prn_copy_scan_lines(pdev, lnum, inp = in, line_size * 6);

        mask = 0200;
        oldc = 077;
        empty = 1;

        for (l = pdev->width, count = 0; --l >= 0; count++) {
            /* transpose 6*8 rectangle */
            register byte *iptr = inp;
            c = 077;
            if (*iptr & mask)
                c += 1;
            if (*(iptr += line_size) & mask)
                c += 2;
            if (*(iptr += line_size) & mask)
                c += 4;
            if (*(iptr += line_size) & mask)
                c += 010;
            if (*(iptr += line_size) & mask)
                c += 020;
            if (*(iptr += line_size) & mask)
                c += 040;
            if (!(mask >>= 1)) {
                mask = 0200;
                inp++;
            }

            if (c != oldc) {
                if (empty) {
                    while (--lcount >= 0) {

                        /* terminate record.
                           this LF is ignored by the LN03 */
                        if (ccount > 78) {
                            gp_fputc('\n', prn_stream);
                            ccount = 0;
                        }
                        /* terminate previous line */
                        gp_fputc('-', prn_stream);
                        ccount++;
                    }
                    empty = lcount = 0;
                }
                if (count > 3) {
                    /* use run length encoding */
                    if (ccount > 74) {
                        gp_fputc('\n', prn_stream);
                        ccount = 0;
                    }
                    /* we know lines will not exceed 10000 pixels */
                    ccount = ccount + 3 + (count > 9)
                                        + (count > 99)
                                        + (count > 999);
                    gp_fprintf(prn_stream, "!%d%c", count, oldc);
                }
                else {
                    while (--count >= 0) {
                        if (ccount > 78) {
                            gp_fputc('\n', prn_stream);
                            ccount = 0;
                        }
                        gp_fputc(oldc, prn_stream);
                        ccount++;
                    }
                }
                oldc = c;
                count = 0;
            }
        }
        if (c != 077) {
           if (count > 3) {
                /* use run length encoding */
                if (ccount > 74) {
                    gp_fputc('\n', prn_stream);
                    ccount = 0;
                }
                /* we know lines will not exceed 10000 pixels */
                ccount = ccount + 3 + (count > 9)
                                    + (count > 99)
                                    + (count > 999);
                gp_fprintf(prn_stream, "!%d%c", count, c);
            }
            else {
                while (--count >= 0) {
                    if (ccount > 78) {
                        gp_fputc('\n', prn_stream);
                        ccount = 0;
                    }
                    gp_fputc(c, prn_stream);
                    ccount++;
                }
            }
        }
    }

    /* leave sixel graphics mode, eject page */
    if (ccount + strlen(eject) > 79) gp_fputc('\n', prn_stream);
    gp_fputs(eject, prn_stream);
    gp_fflush(prn_stream);

    gs_free(pdev->memory->non_gc_memory, (char *)in, in_size, 1, "sixel_print_page");

    return(0);
}