summaryrefslogtreecommitdiff
path: root/contrib/japanese/gdevalps.c
blob: badad8106c06c393415987c1a79048b4a30ae091 (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
/* Copyright (C) 1990, 1995, 1997 Aladdin Enterprises.  All rights reserved.

  This file is part of Aladdin Ghostscript.

  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.

  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

/*
  This driver supports ALPS MD Series printer 600dpi monochrome mode only.

   Feb.  3 1999   Ver. 0.1      MD5000 monochrome mode support.
   Mar. 19 1999   Ver. 0.2      old MD series monochrome compressed mode
                                support. contributed by Kousuke Ikeda.

  There is no printer command refernces, so the sequence of initializing,
  inc cartridge selecting, paper feeding and many specs are not clear yet.

  ESC 0x2a 0x62 n2 n1 0x59          raster line skip command.
                                    skip (n1 * 0x100 + n2) lines.
  ESC 0x2a 0x62 n2 n1 0x54 s2 s1    raster line print command.
                                    skip (s1 * 0x100 + s2) * 8 dots from
                                    paper side, then (n1 * 0x100 + n2) bytes
                                    of MSB first data streams following.
  ESC 0x2a 0x62 n2 n1 0x57          raster line print command for MD1300.
                                    (applicable to MD1xxx and MD2xxx ?)
                                    (n1 * 0x100 + n2) bytes of MSB first data
                                    streams following,
                                    but the data must be compressed.
*/

/* gdevalps.c */
/* Alps Micro Dry 600dpi monochrome printer driver */
#include "gdevprn.h"

#define MD_TOP_MARGIN		0.47f
#define MD_BOTTOM_MARGIN	0.59f
#define MD_SIDE_MARGIN		0.13f

#define X_DPI 600
#define Y_DPI 600
#define LINE_SIZE ((X_DPI * 84 / 10 + 7) / 8)	/* bytes per line for letter */

static int md50_print_page(gx_device_printer *, gp_file *, const char *, int);
static dev_proc_open_device(md_open);
static dev_proc_print_page(md50m_print_page);
static dev_proc_print_page(md50e_print_page);
static dev_proc_print_page(md1xm_print_page);

static int
md_initialize(gx_device *dev)
{
    int code = gdev_prn_initialize_mono(dev);

    if (code < 0)
        return code;

    set_dev_proc(dev, open_device, md_open);

    return 0;
}

static gx_device_procs prn_md_procs =
  devprocs_initialize(md_initialize);

gx_device_printer far_data gs_md50Mono_device =
  prn_device(prn_md_procs, "md50Mono",
        DEFAULT_WIDTH_10THS,
        DEFAULT_HEIGHT_10THS,
        600,				/* x_dpi */
        600,				/* y_dpi */
        0, 0, 0, 0,			/* margins */
        1, md50m_print_page);

gx_device_printer far_data gs_md50Eco_device =
  prn_device(prn_md_procs, "md50Eco",
        DEFAULT_WIDTH_10THS,
        DEFAULT_HEIGHT_10THS,
        600,				/* x_dpi */
        600,				/* y_dpi */
        0, 0, 0, 0,			/* margins */
        1, md50e_print_page);

gx_device_printer far_data gs_md1xMono_device =
  prn_device(prn_md_procs, "md1xMono",
        DEFAULT_WIDTH_10THS,
        DEFAULT_HEIGHT_10THS,
        600,				/* x_dpi */
        600,				/* y_dpi */
        0, 0, 0, 0,			/* margins */
        1, md1xm_print_page);

/* Normal black 600 x 600 dpi mode. */
static const char init_50mono[] = {
0x1b, 0x65,
0x1b, 0x25, 0x80, 0x41,
0x1b, 0x1a, 0x00, 0x00, 0x4c,
0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
0x1b, 0x26, 0x6c, 0x07, 0x00, 0x4d,
0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
0x1b, 0x2a, 0x72, 0x00, 0x55,
0x1b, 0x2a, 0x74, 0x03, 0x52,
0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
0x1b, 0x1a, 0x00, 0x00, 0x41,
0x1b, 0x26, 0x6c, 0x01, 0x00, 0x43, 0x00,
0x1b, 0x1a, 0x00, 0x00, 0x55,
0x1b, 0x2a, 0x72, 0x01, 0x41,
0x1b, 0x2a, 0x62, 0x00, 0x00, 0x4d,
0x1b, 0x1a, 0x00, 0x80, 0x72,
};

/* ECO black 600 x 600 dpi mode. */
/* If you wanto to use ECO black casette, use this sequence for initialize. */
static const char init_50eco[] = {
0x1b, 0x65,
0x1b, 0x25, 0x80, 0x41,
0x1b, 0x1a, 0x00, 0x00, 0x4c,
0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
0x1b, 0x26, 0x6c, 0x07, 0x00, 0x4d,
0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
0x1b, 0x2a, 0x72, 0x01, 0x55,
0x1b, 0x2a, 0x74, 0x03, 0x52,
0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
0x1b, 0x1a, 0x00, 0x00, 0x41,
0x1b, 0x1a, 0x01, 0x00, 0x43,
0x1b, 0x26, 0x6c, 0x01, 0x00, 0x43, 0x17,
0x1b, 0x1a, 0x00, 0x00, 0x55,
0x1b, 0x2a, 0x72, 0x01, 0x41,
0x1b, 0x2a, 0x62, 0x00, 0x00, 0x4d,
0x1b, 0x1a, 0x16, 0x80, 0x72,
};

/* Mono Black 600x600 mode for MD1300 */
static const char init_md13[] = {
0x1b, 0x65,
0x1b, 0x25, 0x80, 0x41,
0x1b, 0x1a, 0x00, 0x00, 0x4c,
0x1b, 0x26, 0x6c, 0x01, 0x00, 0x48,
0x1b, 0x26, 0x6c, 0x00, 0x00, 0x4d,
0x1b, 0x26, 0x6c, 0x04, 0x00, 0x41,
0x1b, 0x2a, 0x72, 0x00, 0x55,
0x1b, 0x2a, 0x74, 0x03, 0x52,
0x1b, 0x26, 0x6c, 0xe5, 0x18, 0x50,
0x1b, 0x1a, 0x00, 0x00, 0x41,
0x1b, 0x2a, 0x72, 0x00, 0x41,
0x1b, 0x2a, 0x62, 0x02, 0x00, 0x4d,
0x1b, 0x1a, 0x00, 0x00, 0x72,
};

static const char end_md[] = {
0x0c,
0x1b, 0x2a, 0x72, 0x43,
0x1b, 0x25, 0x00, 0x58
};
/* ------ Internal routines ------ */

/* Open the printer, and set the margins. */
static int
md_open(gx_device *pdev)
{
    static const float md_margins[4] =
    {
        MD_SIDE_MARGIN, MD_BOTTOM_MARGIN,
        MD_SIDE_MARGIN, MD_TOP_MARGIN
    };

    if (pdev->HWResolution[0] != 600)
    {
        emprintf(pdev->memory, "device must have an X resolution of 600dpi\n");
        return_error(gs_error_rangecheck);
    }

    gx_device_set_margins(pdev, md_margins, true);
    return gdev_prn_open(pdev);
}

/* MD5000 monochrome mode entrance. */
static int
md50m_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
  return(md50_print_page(pdev, prn_stream, init_50mono, sizeof(init_50mono)));
}

/* MD5000 Eco mode monochrome mode entrance. */
static int
md50e_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
  return(md50_print_page(pdev, prn_stream, init_50eco, sizeof(init_50eco)));
}

/* MD5000 monochrome mode print. */
static int
md50_print_page(gx_device_printer *pdev, gp_file *prn_stream,
              const char *init_str, int init_size)
{
  int lnum;
  int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  byte *data = (byte *)gs_malloc(pdev->memory->non_gc_memory, 8, line_size, "md50_print_page(data)" );
  int skipping = 0;
  int nbyte;
  int nskip;
  int n;

    /* Load Paper & Select Inc Cartridge */
  gp_fwrite(init_str, sizeof(char), init_size, prn_stream);
  gp_fflush(prn_stream);

  for ( lnum = 0; lnum <= pdev->height; lnum++ ) {
    byte *end_data = data + line_size;
    byte *start_data = data;
    memset(data, 0, LINE_SIZE);
    n = gdev_prn_copy_scan_lines(pdev, lnum,
                             (byte *)data, line_size);
    if (n != 1)
      return n;

    /* Remove trailing 0s. */
    while ( end_data > data && end_data[-1] == 0 )
      end_data--;
    /* Count pre print skip octets */
    while ( start_data < end_data && *start_data == 0 )
      start_data++;
    nbyte = end_data - start_data;
    nskip = start_data - data;

    if(nbyte == 0)
      {
        skipping++;
        continue;
      }
    else
      {
        if(skipping)
          {
            gp_fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
                       skipping & 0xff, (skipping & 0xff00) / 0x100, 0x59);
            skipping = 0;
          }
        gp_fprintf(prn_stream, "%c%c%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
                   nbyte & 0xff, (nbyte & 0xff00) / 0x100, 0x54,
                   nskip & 0xff, (nskip & 0xff00) / 0x100);
        gp_fwrite(start_data, sizeof(char), nbyte, prn_stream);
      }
  }

  /* Eject Page */
  gp_fwrite(end_md, sizeof(char), sizeof(end_md), prn_stream);
  gp_fflush(prn_stream);

  return 0;
}

/* all? MD series monochrome mode print with data compression. */
static int
md1xm_print_page(gx_device_printer *pdev, gp_file *prn_stream)
{
  int lnum;
  int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  byte *data = (byte *)gs_malloc(pdev->memory->non_gc_memory, 8, line_size, "md1xm_print_page(data)");
  byte *out_start = (byte *)gs_malloc(pdev->memory->non_gc_memory, 8, line_size, "md1xm_print_page(data)");
  int skipping = 0;
  int nbyte;

  /* Load Paper & Select Inc Cartridge */
  gp_fwrite(&init_md13[0], sizeof(char), sizeof(init_md13), prn_stream);
  gp_fflush(prn_stream);

  for ( lnum = 0; lnum <= pdev->height; lnum++ ) {
    byte *end_data = data + line_size;
    byte *data_p = data;
    byte *out_data = out_start;
    byte *p, *q;
    int count;

    gdev_prn_copy_scan_lines(pdev, lnum, data, line_size);
    /* Remove trailing 0s. */
    while ( end_data > data && end_data[-1] == 0 )
      end_data--;

     nbyte = end_data - data_p;

    if(nbyte == 0)
      {
        skipping++;
        continue;
      }
    else
      {
        if(skipping)
          {
            gp_fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
                       skipping & 0xff, (skipping & 0xff00) / 0x100, 0x59);
            skipping = 0;
          }

        /* Following codes are borrowed from gdevescp.c */

        for ( p = data_p, q = data_p + 1; q < end_data; ){

          if( *p != *q ) {

            p += 2;
            q += 2;

          } else {
            /*
            ** Check behind us, just in case:
            */

            if( p > data_p && *p == *(p-1) )
              p--;

            /*
            ** walk forward, looking for matches:
            */

            for( q++ ; *q == *p && q < end_data ; q++ ) {
              if( (q-p) >= 128 ) {
                if( p > data_p ) {
                  count = p - data_p;
                  while( count > 128 ) {
                    *out_data++ = '\177';
                    memcpy(out_data, data_p, 128);	/* data */
                    data_p += 128;
                    out_data += 128;
                    count -= 128;
                  }
                  *out_data++ = (char) (count - 1); /* count */
                  memcpy(out_data, data_p, count);	/* data */
                  out_data += count;
                }
                *out_data++ = '\201';	/* Repeat 128 times */
                *out_data++ = *p;
                p += 128;
                data_p = p;
              }
            }

            if( (q - p) > 2 ) {	/* output this sequence */
              if( p > data_p ) {
                count = p - data_p;
                while( count > 128 ) {
                  *out_data++ = '\177';
                  memcpy(out_data, data_p, 128);	/* data */
                  data_p += 128;
                  out_data += 128;
                  count -= 128;
                }
                *out_data++ = (char) (count - 1);	/* byte count */
                memcpy(out_data, data_p, count);	/* data */
                out_data += count;
              }
              count = q - p;
              *out_data++ = (char) (256 - count + 1);
              *out_data++ = *p;
              p += count;
              data_p = p;
            } else	/* add to non-repeating data list */
              p = q;
            if( q < end_data )
              q++;
          }
        }
        /*
        ** copy remaining part of line:
        */

        if( data_p < end_data ) {

          count = end_data - data_p;

          /*
          ** If we've had a long run of varying data followed by a
          ** sequence of repeated data and then hit the end of line,
          ** it's possible to get data counts > 128.
          */

          while( count > 128 ) {
            *out_data++ = '\177';
            memcpy(out_data, data_p, 128);	/* data */
            data_p += 128;
            out_data += 128;
            count -= 128;
          }

          *out_data++ = (char) (count - 1);	/* byte count */
          memcpy(out_data, data_p, count);	/* data */
          out_data += count;
        }

        nbyte = out_data - out_start;

        gp_fprintf(prn_stream, "%c%c%c%c%c%c", 0x1b, 0x2a, 0x62,
                   nbyte & 0xff, (nbyte & 0xff00) / 0x100, 0x57);
        gp_fwrite(out_start, sizeof(char), nbyte, prn_stream);
      }
  }

  /* Eject Page */
  gp_fwrite(end_md, sizeof(char), sizeof(end_md), prn_stream);
  gp_fflush(prn_stream);

  return 0;
}