Package list splix / upstream/1.0.1 src / spl2.cpp
upstream/1.0.1

Tree @upstream/1.0.1 (Download .tar.gz)

spl2.cpp @upstream/1.0.1raw · history · blame

  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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
 * 	spl2.cpp		(C) 2006, Aurélien Croc (AP²C)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the
 *  Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *  $Id: spl2.cpp 64 2006-12-14 01:37:22Z ap2c $
 *
 * ---
 *  Thanks to Keith White for his modifications, corrections and adds.
 * 
 */
#include "spl2.h"
#include "printer.h"
#include "document.h"
#include "error.h"
#include "band.h"
#include "bandanalyser.h"
#include <stdint.h>


/*
 * Constructeur - Destructeur
 * Init - Uninit
 */
SPL2::SPL2()
{
    _printer = NULL;
    _output = NULL;
}

SPL2::~SPL2()
{
}



/* 
 * Génération de l'en-tête et du pied de page PJL
 * Write PJL header and footer
 */
int SPL2::beginDocument()
{
    if (!_output || !_printer) {
        ERROR(_("SPL2::beginDocument: called with NULL parameters"));
        return -1;
    }

    _printer->newJob(_output);
    fprintf(_output, "@PJL ENTER LANGUAGE = QPDL\n");
    DEBUG("Envoie de l'en-tête du document JPL");
    return 0;
}

int SPL2::closeDocument()
{
    if (!_output || !_printer) {
        ERROR(_("SPL2::closeDocument: called with NULL parameters"));
        return -1;
    }
    _printer->endJob(_output);
    DEBUG("Envoie du pied de page du document JPL");
    return 0;
}



/*
 * Impression d'une page
 * Impress a page
 */
int SPL2::_writeColorBand(Band *band, int color)
{
    unsigned char *data, header[5];
    uint32_t checksum = 0;
    size_t size;

    // Compress
    if (!(data = band->exportBand(_printer->compVersion(), &size)))
        return 1;

    // Calculate the checksum
    if (_printer->qpdlVersion() != 0) {
        for (unsigned int j = 0; j < size; j++)
            checksum += data[j];
    }

    // Write the color header
    if (color) {
        header[0x0] = color;
        fwrite((char *)&header, 1, 1, _output);
    }
    header[0x0] = _printer->compVersion();      // Compression
    if (_printer->qpdlVersion() == 0) {
        header[0x1] = size >> 24;                // data length
        header[0x2] = size >> 16;                // data length
        header[0x3] = size >> 8;                // data length
        header[0x4] = size;                     // data length
    } else {
        header[0x1] = (size + 4) >> 24;          // data length
        header[0x2] = (size + 4) >> 16;          // data length
        header[0x3] = (size + 4) >> 8;          // data length
        header[0x4] = (size + 4);               // data length
    }
    fwrite((char *)&header, 1, 0x5, _output);

    // Write the data
    fwrite(data, 1, size, _output);
    delete[]data;

    // Write the checksum
    if (_printer->qpdlVersion() != 0) {
        header[0x0] = checksum >> 24;
        header[0x1] = checksum >> 16;
        header[0x2] = checksum >> 8;
        header[0x3] = checksum;
        fwrite((char *)&header, 1, 0x4, _output);
    }

    return 0;
}

int SPL2::printPage(Document *document, unsigned long nrCopies)
{
    unsigned long width, height, clippingX, clippingY;
    unsigned long bandNumber;
    unsigned long i;
    char header[0x11];
    char errors = 0;
    Band *bandC, *bandM, *bandY, *bandB;

    if (!document) {
        ERROR(_("SPL2::printPage: called with NULL parameter"));
        return -2;
    }
    // Load a new page
    if (document->loadPage(_printer))
        return -1;
    if (!document->height())
        return -1;


    // Send page header FIXME
    header[0x0] = 0;                                // Signature
    header[0x1] = _printer->resolutionY() / 100;    // Y Resolution
    header[0x2] = nrCopies >> 8;                    // Number of copies 8-15
    header[0x3] = nrCopies;                         // Number of copies 0-7
    header[0x4] = _printer->paperType();            // Paper type
    header[0x5] = document->width() >> 8;           // Printable area width
    header[0x6] = document->width();                // Printable area width
    header[0x7] = document->height() >> 8;          // Printable area height
    header[0x8] = document->height();               // Printable area height
    header[0x9] = _printer->paperSource();          // Paper source
    header[0xa] = _printer->docHeaderValues(0);     // ??? XXX
    header[0xb] = _printer->duplex() >> 8;          // Duplex
    header[0xc] = _printer->duplex();               // Duplex
    header[0xd] = _printer->docHeaderValues(1);     // ??? XXX
    header[0xe] = _printer->qpdlVersion();          // QPDL Version
    header[0xf] = _printer->docHeaderValues(2);     // ??? XXX
    if (_printer->resolutionY() != _printer->resolutionX())
        header[0x10] = _printer->resolutionX() / 100; // X Resolution
    else
        header[0x10] = 0;                           // X Resolution = Y Res.
    fwrite((char *)&header, 1, sizeof(header), _output);



    // Get the width, height, clipping X and clipping Y values
    if (document->width() <= _printer->areaX()) {
        clippingX = 0;
        width = document->width();
    } else if (document->width() < _printer->pageSizeX()) {
        clippingX = (unsigned long)(document->width() - _printer->areaX());
        width = document->width();
    } else {
        clippingX = (unsigned long)_printer->marginX();
        width = (unsigned long)_printer->pageSizeX();
    }
    if (document->height() <= _printer->areaY()) {
        clippingY = 0;
        height = document->height();
    } else if (document->height() < _printer->pageSizeY()) {
        clippingY = (unsigned long)(document->height() - _printer->areaY());
        height = document->height();
    } else {
        clippingY = (unsigned long)_printer->marginY();
        height = (unsigned long)_printer->pageSizeY();
    }


    // Create the band instance
    bandB = new Band((unsigned long)_printer->pageSizeX(),
        (unsigned long)_printer->bandHeight());
    if (document->isColor()) {
        bandC = new Band((unsigned long)_printer->pageSizeX(),
            (unsigned long)_printer->bandHeight());
        bandM = new Band((unsigned long)_printer->pageSizeX(),
            (unsigned long)_printer->bandHeight());
        bandY = new Band((unsigned long)_printer->pageSizeX(),
            (unsigned long)_printer->bandHeight());
    }
    bandNumber = 0;
    bandB->setClipping(clippingX);
    if (document->isColor()) {
        bandC->setClipping(clippingX);
        bandM->setClipping(clippingX);
        bandY->setClipping(clippingX);
    }

    // Clip vertically the document
    if (!document->isColor())
        clippingY = clippingY * 4;
    for (; clippingY; clippingY--)
        document->readLine();

    // Round up height to a multiple of bandHeight
    height += _printer->bandHeight() - (height % _printer->bandHeight());




    /*
     * Read and create each band
     */
    for (i = 0; i < height; i++) {
        unsigned long res;

        if (document->isColor()) {
            res = document->readLine();
            if (res < 0) {
                errors = 1;
                break;
            } else if (!res)
                break;
            if (bandC->addLine(document->lineBuffer(), (res > width ? width : 
                    res))) {
                errors = 1;
                break;
            }
            res = document->readLine();
            if (res < 0) {
                errors = 1;
                break;
            } else if (!res)
                break;
            if (bandM->addLine(document->lineBuffer(), (res > width ? width : 
                    res))) {
                errors = 1;
                break;
            }
            res = document->readLine();
            if (res < 0) {
                errors = 1;
                break;
            } else if (!res)
                break;
            if (bandY->addLine(document->lineBuffer(), (res > width ? width : 
                    res))) {
                errors = 1;
                break;
            }
        }
        res = document->readLine();
        if (res < 0) {
            errors = 1;
            break;
        } else if (!res)
            break;
        if (bandB->addLine(document->lineBuffer(), (res > width ? width : 
                res))) {
            errors = 1;
            break;
        }
        // Compress and send the band if it's complete
        if (bandB->isFull()) {
            if (document->isColor())
                correctBlackColor(bandC, bandM, bandY, bandB);
            checkEmptyBand(bandB);
            if (document->isColor()) {
                checkEmptyBand(bandC);
                checkEmptyBand(bandM);
                checkEmptyBand(bandY);
            }
            if ((!document->isColor() && bandB->isEmpty()) ||
                    (document->isColor() && bandB->isEmpty() &&
                    bandC->isEmpty() && bandM->isEmpty() && bandY->isEmpty())) {
                bandNumber++;
                bandB->clean();
                if (document->isColor()) {
                    bandC->clean();
                    bandM->clean();
                    bandY->clean();
                }
                continue;
            }
            // QPDL Version 0 or 1
            if (_printer->qpdlVersion() < 2) {
                // Write the band header
                header[0x0] = 0xC;                  // Signature
                header[0x1] = bandNumber;           // Band number
                header[0x2] = bandB->width() >> 8;  // Band width
                header[0x3] = bandB->width();       // Band width
                header[0x4] = bandB->height() >> 8; // Band height
                header[0x5] = bandB->height();      // Band height
                fwrite((char *)&header, 1, 0x6, _output);

                if (_printer->isColorPrinter()) {
                    if (document->isColor()) {
                        if (!bandC->isEmpty()) {
                            if (_writeColorBand(bandC, 1)) {
                                errors = 1;
                                break;
                            }
                        }
                        if (!bandM->isEmpty()) {
                            if (_writeColorBand(bandM, 2)) {
                                errors = 1;
                                break;
                            }
                        }
                        if (!bandY->isEmpty()) {
                            if (_writeColorBand(bandY, 3)) {
                                errors = 1;
                                break;
                            }
                        }
                    }
                    if (!bandB->isEmpty()) {
                        if (_writeColorBand(bandB, 4)) {
                            errors = 1;
                            break;
                        }
                    }
                    header[0x0] = 0;                // End color
                    fwrite((char *)&header, 1, 1, _output);
                } else {
                    if (_writeColorBand(bandB, 0)) {
                        errors = 1;
                        break;
                    }
                }

            // QPDL Version 2
            } else if (_printer->qpdlVersion() == 2) {
                if (_printer->isColorPrinter()) {
                    if (!bandB->isEmpty()) {
                        // Write the band header
                        header[0x0] = 0xC;                  // Signature
                        header[0x1] = bandNumber;           // Band number
                        header[0x2] = bandB->width() >> 8;  // Band width
                        header[0x3] = bandB->width();       // Band width
                        header[0x4] = bandB->height() >> 8; // Band height
                        header[0x5] = bandB->height();      // Band height
                        fwrite((char *)&header, 1, 0x6, _output);
                        if (_writeColorBand(bandB, 4)) {
                            errors = 1;
                            break;
                        }
                    }
                    if (document->isColor()) {
                        if (!bandC->isEmpty()) {
                            // Write the band header
                            header[0x0] = 0xC;                  // Signature
                            header[0x1] = bandNumber;           // Band number
                            header[0x2] = bandC->width() >> 8;  // Band width
                            header[0x3] = bandC->width();       // Band width
                            header[0x4] = bandC->height() >> 8; // Band height
                            header[0x5] = bandC->height();      // Band height
                            fwrite((char *)&header, 1, 0x6, _output);
                            if (_writeColorBand(bandC, 1)) {
                                errors = 1;
                                break;
                            }
                        }
                        if (!bandM->isEmpty()) {
                            // Write the band header
                            header[0x0] = 0xC;                  // Signature
                            header[0x1] = bandNumber;           // Band number
                            header[0x2] = bandM->width() >> 8;  // Band width
                            header[0x3] = bandM->width();       // Band width
                            header[0x4] = bandM->height() >> 8; // Band height
                            header[0x5] = bandM->height();      // Band height
                            fwrite((char *)&header, 1, 0x6, _output);
                            if (_writeColorBand(bandM, 2)) {
                                errors = 1;
                                break;
                            }
                        }
                        if (!bandY->isEmpty()) {
                            // Write the band header
                            header[0x0] = 0xC;                  // Signature
                            header[0x1] = bandNumber;           // Band number
                            header[0x2] = bandY->width() >> 8;  // Band width
                            header[0x3] = bandY->width();       // Band width
                            header[0x4] = bandY->height() >> 8; // Band height
                            header[0x5] = bandY->height();      // Band height
                            fwrite((char *)&header, 1, 0x6, _output);
                            if (_writeColorBand(bandY, 3)) {
                                errors = 1;
                                break;
                            }
                        }
                    }
                } else {
                    // Write the band header
                    header[0x0] = 0xC;                  // Signature
                    header[0x1] = bandNumber;           // Band number
                    header[0x2] = bandB->width() >> 8;  // Band width
                    header[0x3] = bandB->width();       // Band width
                    header[0x4] = bandB->height() >> 8; // Band height
                    header[0x5] = bandB->height();      // Band height
                    fwrite((char *)&header, 1, 0x6, _output);
                    if (_writeColorBand(bandB, 0)) {
                        errors = 1;
                        break;
                    }
                }
            }
            bandNumber++;
            bandB->clean();
            if (document->isColor()) {
                bandC->clean();
                bandM->clean();
                bandY->clean();
            }
        }
    }



    // Clean everything
    delete bandB;

    if (document->isColor()) {
        bandC->clean();
        bandM->clean();
        bandY->clean();
    }
    if (errors)
        return -11;

    // Write the end of the page
    header[0x0] = 1;                        // Signature
    header[0x1] = nrCopies >> 8;            // Number of copies 8-15
    header[0x2] = nrCopies;                 // Number of copies 0-7
    fwrite((char *)&header, 1, 3, _output);

    return 0;
}

/* vim: set expandtab tabstop=4 shiftwidth=4 smarttab tw=80 cin: */