summaryrefslogtreecommitdiffstats
path: root/kbarcode/pixmapbarcode.cpp
blob: 785441b8165bed33369f0b27460e389144770a2c (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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/***************************************************************************
                         pixmapbarcode.cpp  -  description
                             -------------------
    begin                : Mon Nov 22 2004
    copyright            : (C) 2004 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
                                                                          
    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; either version 2 of the License, or     
    (at your option) any later version.                                   
                                                                          
 ***************************************************************************/

#include "pixmapbarcode.h"
#include "barkode.h"

// patch from Ali Akcaagac says that this include is needed
#include <stdlib.h>

#include <tdeapplication.h>
#include <kprocess.h>
#include <tdetempfile.h>

#include <tqbuffer.h>
#include <tqdom.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqpainter.h>
#include <tqpaintdevicemetrics.h>
#include <tqpixmap.h>
#include <tqtextstream.h>

/* Margin added by GNU Barcode to the barcodes */
#define BARCODE_MARGIN 10     

/* Use a 5KB buffer for pipes */
#define BUFFER_SIZE 1024 * 5 

PDF417Options::PDF417Options()
{
    defaults();
}

const PDF417Options& PDF417Options::operator=( const BarkodeEngineOptions& rhs )
{
    const PDF417Options* pdf = (dynamic_cast<const PDF417Options*>(&rhs));

    this->m_row = pdf->m_row;
    this->m_col = pdf->m_col;
    this->m_err = pdf->m_err;

    return *this;
}

void PDF417Options::defaults()
{
    m_row = 24;
    m_col = 8;
    m_err = 5;
}

void PDF417Options::load( const TQDomElement* tag )
{
    m_row = tag->attribute( "pdf417.row", "24" ).toInt();
    m_col = tag->attribute( "pdf417.col", "8" ).toInt();
    m_err = tag->attribute( "pdf417.err", "5" ).toInt();
}

void PDF417Options::save( TQDomElement* tag )
{
    tag->setAttribute( "pdf417.row", m_row );
    tag->setAttribute( "pdf417.col", m_col );
    tag->setAttribute( "pdf417.err", m_err );
}

PixmapBarcode::PixmapBarcode()
 : BarkodeEngine()
{
}


PixmapBarcode::~PixmapBarcode()
{
}

const PixmapBarcode & PixmapBarcode::operator=( const BarkodeEngine & rhs )
{
    const PixmapBarcode* pix = dynamic_cast<const PixmapBarcode*>(&rhs);

    if( pix ) 
    {
        m_pdf417_options = pix->m_pdf417_options;
        p                = pix->p;
    }

    return *this;
}

const TQSize PixmapBarcode::size() const
{
    return ( p.size().isNull() ? TQSize( 100, 80 ) : p.size() );
}

void PixmapBarcode::update( const TQPaintDevice* device )
{
    p.resize( 0, 0 );
    createBarcode( &p, device );
}

void PixmapBarcode::drawBarcode( TQPainter & painter, int x, int y )
{
    if( p.isNull() )
        createBarcode( &p, TQT_TQPAINTDEVICE(painter.device()) );
    
    if( p.isNull() ) // still no barcode....
    {
        barkode->drawInvalid( painter, x, y );
        return;
    }
        
    painter.drawPixmap( x, y, p );
}

bool PixmapBarcode::createPixmap( TQPixmap* target, int resx, int resy )
{
    char* postscript = NULL;
    long postscript_size = 0;
    TQString cmd;
    bool bMonocrome;

    bMonocrome = ( barkode->foreground() == TQt::black && 
                   barkode->background() == TQt::white &&
                   barkode->textColor() == TQt::black );

    KTempFile* input = new KTempFile( TQString(), bMonocrome ? ".pbm" : ".ppm" );
    input->file()->close();

    if( Barkode::engineForType( barkode->type() ) == PDF417 ) {
        if(!createPdf417( input )) {
            cleanUp( input, target );
            return false;
        }

	target->load( input->name(), "GIF" );
    } else { 
        if( !createPostscript( &postscript, &postscript_size ) )
        {
            cleanUp( input, target );
            return false;
        }

	FILE* gs_pipe;

	if( !postscript_size )
	{
	    // GNU Barcode was not able to encode this barcode
	    cleanUp( input, target );
	    return false;
	}

        TQRect size = bbox( postscript, postscript_size );
        double sw = (double)(size.x() + size.width())/72 * resx;
        double sh = (double)(size.y() + size.height())/72 * resy;

        if( Barkode::engineForType( barkode->type() ) == TBARCODE )
        {
            sw = (double)(size.x() + size.width());
            sh = (double)(size.y() + size.height());
        }

	cmd = TQString("gs -g%1x%2").arg(int(sw*(double)barkode->scaling())).arg(int(sh*(double)barkode->scaling()));
	cmd += " -r" + TQString::number( resx*(double)barkode->scaling()) + "x" + TQString::number( resy*(double)barkode->scaling() );
	cmd += TQString(" -sDEVICE=%1 -sOutputFile=").arg( bMonocrome ? "pbmraw" : "ppm" );
        cmd += input->name();
	cmd += " -sNOPAUSE -q - -c showpage quit";
	
        tqDebug("cmd: %s", cmd.ascii() );
	gs_pipe = popen( cmd.latin1(), "w" );
        if( !gs_pipe )
	{
	    tqDebug("ERROR: cannot open Ghostscript pipe!");
            cleanUp( input, target );
	    return false;
	}

	fwrite( postscript, sizeof(char), postscript_size, gs_pipe );
	pclose( gs_pipe );

        target->load( input->name(), "PBM" );
    }
        

    free( postscript );

    input->unlink();
    delete input;

    return true;
}

bool PixmapBarcode::createPostscript( char** postscript, long* postscript_size )
{
    TQString cmd;

    /*
    if( Barkode::engineForType( barkode->type() ) == TBARCODE ) 
    {
        cmd = createTBarcodeCmd();
        tqDebug("tbarcodeclient commandline: %s", cmd.latin1() );
    }
    else // GNU_BARCODE
    */
    {
        cmd = "barcode -E -b ";
        cmd += KShellProcess::quote( barkode->parsedValue() ) + (barkode->textVisible() ? "" : " -n");
        cmd += " -e " + barkode->type();
    }
    
    if( !readFromPipe( cmd.latin1(), postscript, postscript_size ) )
        return false;

    return true;
}

TQRect PixmapBarcode::bbox( const char* data, long size )  
{
    int x = 0, y = 0, w = 0, h = 0;
    const char* bbox = "%%BoundingBox:";
    int len = strlen( bbox );

    TQRect s(0,0,0,0);
    TQByteArray array;
    array.setRawData( data, size );
    
    TQBuffer b( array );
    if( !b.open( IO_ReadOnly ) )
        return s;
    
    TQTextStream t( &b );
        
    TQString text = t.readLine();
    while( !text.isNull() )
    {
        if( text.startsWith( bbox ) )
        {
            text = text.right( text.length() - len );
            sscanf( text.latin1(), "%d %d %d %d", &x, &y, &w, &h );
            s = TQRect( x, y, w, h );
            break;
        }

	text = t.readLine();
    }

    b.close();
    array.resetRawData( data, size );

    return s;
}

bool PixmapBarcode::readFromPipe( const char* command, char** buffer, long* buffer_size )
{
    FILE* pipe = popen( command, "r" );
    if( !pipe )
    {
        tqDebug("ERROR: cannot open pipe %s!", command );
        return false;
    }

    char* buf = (char*)malloc( BUFFER_SIZE );
    char* tmp = NULL;
    int s     = 0;

    *buffer_size = 0;
    do {
        s = fread( buf, sizeof(char), BUFFER_SIZE, pipe );

        // Special case:
        // GNU Barcode Error
        if( !s ) 
            break;

        // there won't be more data to read
        tmp = (char*)malloc( *buffer_size + s );
        
        if( *buffer ) 
        {
            memcpy( tmp, *buffer, *buffer_size );
            free( *buffer );
        }
        
        memcpy( tmp+ *buffer_size, buf, s );
        *buffer = tmp;
        *buffer_size += s;
    } while( s == BUFFER_SIZE );
    
    pclose( pipe );
    free( buf );

    return true;
}

void PixmapBarcode::createBarcode( TQPixmap* target, const TQPaintDevice* device )
{
    TQPaintDeviceMetrics pdm( device );
    int resx = pdm.logicalDpiX();
    int resy = pdm.logicalDpiY();

    TQPixmap* cached = 0;//BarcodeCache::instance()->read( barcode, resx, resy, value );

    // no matching barcode found in cache
    if( !cached ) {
        if( !createPixmap( target, resx, resy ) )
            return;
    } else {
        *target = *cached;
        delete cached;
    }
    
    if( Barkode::hasFeature( barkode->type(), PDF417 ) ) {
        // we have to scale to the correct resolution.
        // we scale already here and not at the end,
        // so that the addMargin function does not get a scaled margin.
        TQPaintDeviceMetrics pdm( TDEApplication::desktop() );
        int screenresx = pdm.logicalDpiX();
        int screenresy = pdm.logicalDpiY();
    
        TQWMatrix m;
        double scalex = (resx/screenresx)*barkode->scaling();
        double scaley = (resy/screenresy)*barkode->scaling();
        m.scale( scalex, scaley );
        *target = target->xForm( m );
    }
    *target = cut( target, barkode->cut() );
    *target = addMargin( target );

    // Rotate
    TQWMatrix m;
    m.rotate( (double)barkode->rotation() );
    *target = target->xForm( m );

    //barcode.valid = true;
}

bool PixmapBarcode::createPdf417( KTempFile* output )
{
    const PDF417Options* options = (dynamic_cast<const PDF417Options*>(barkode->engine()->options()));

    if( !options ) 
    {
        tqDebug("No PDF417Options available!");
        return false;
    }

    KTempFile text( TQString(), ".txt" );
    TQTextStream t( text.file() );
    t << barkode->parsedValue();
    text.file()->close();

    // ps does not work because bounding box information is missing
    // pbm cannot be loaded by KImgIO (works fine in GIMP)
    // gif is the only other option
    KShellProcess proc;
    proc << "pdf417_enc" << "-tgif" << text.name() << output->name()
         << options->row()
         << options->col()
         << options->err();
         
    proc.start( TDEProcess::Block, TDEProcess::NoCommunication );
    proc.resume();

    if( proc.exitStatus() ) {
        text.unlink();
        return false;
    }

    text.unlink();    
    return true;    
}

#if 0
TQString PixmapBarcode::createTBarcodeCmd()
{
    TQString cmd;

    // print text
    TQString flag = barkode->textVisible() ? " Ton" : " n Toff"; // we pass the old parameter Ton and the new one: n
    // escape text
    flag.append( barkode->tbarcodeOptions()->escape() ? " son" : " soff" );
    // autocorrection
    flag.append( barkode->tbarcodeOptions()->autocorrect() ? " Aon" : " Aoff" );
    // barcode height
    flag.append( TQString( " h%1" ).arg( barkode->tbarcodeOptions()->height() ) );
    // text above
    if( barkode->tbarcodeOptions()->above() )
        flag.append( " a" );
    
    cmd = "tbarcodeclient "; 
    if( !Barkode::hasFeature( barkode->type(), BARCODE2D ) )
        cmd += TQString( " m%1" ).arg( barkode->tbarcodeOptions()->moduleWidth() * 1000 );

    if( Barkode::hasFeature( barkode->type(), DATAMATRIX ) ) 
        cmd += TQString( " Ds%1" ).arg( barkode->datamatrixSize() );

    if( Barkode::hasFeature( barkode->type(), PDF417BARCODE ) )
        cmd += TQString( " Pr%1 Pc%2 Pe%3" ).arg( barkode->pdf417Options()->row() )
                                           .arg( barkode->pdf417Options()->col() )
                                           .arg( barkode->pdf417Options()->err() );
        
    cmd += " " + barkode->type() + TQString(" tPS c%1").arg( barkode->tbarcodeOptions()->checksum() );
    cmd += flag + " d" + KShellProcess::quote(  barkode->parsedValue() );

    return cmd;
}
#endif // 0

void PixmapBarcode::cleanUp( KTempFile* file, TQPixmap* target )
{
    target->resize( 0, 0 );

    file->unlink();
    delete file;
}

TQPixmap PixmapBarcode::cut( TQPixmap* pic, double cut)
{
    if( cut == 1.0 )
        return (*pic);

    TQPixmap pcut( pic->width(), int((double)pic->height() * cut) );
    pcut.fill( TQt::white ); // barcode.bg

    TQWMatrix m;
    /*
     * if text is above the barcode cut from
     * below the barcode.
     */

    // TODO: put this into one if, I am to stupid today.....
    if( Barkode::hasFeature( barkode->type(), TBARCODEADV ) ) {
        //if( !barcode.tbarcode.above )
            m.rotate( 180 );
    } else
        m.rotate( 180 );

    TQPainter painter( &pcut );
    painter.drawPixmap( 0, 0, pic->xForm( m ) );

    return pcut.xForm( m );
}

TQPixmap PixmapBarcode::addMargin( TQPixmap* pic )
{
    TQPixmap p;

    /* We have to handle UPC special because of the checksum character
     * which is printed on the right margin.
     * The samve goes for ISBN codes.
     * Any other formats??
     */

    bool gnubarcode = (Barkode::engineForType( barkode->type() ) == GNU_BARCODE) ||
        (Barkode::engineForType( barkode->type() ) == PURE_POSTSCRIPT);
    double barm = gnubarcode ? BARCODE_MARGIN * barkode->scaling() : 0.0;

    // Add margin
    double sx = barm;
    double sy = barm;
    double sw = pic->width()  - (barm * 2);
    double sh = pic->height() - (barm * 2);
    int margin = (int)(barkode->quietZone()*2 - barm*2);

    if( gnubarcode && (barkode->type() == "upc" || barkode->type() == "isbn") ) 
    {
        sw = pic->width() - barm;

        p.resize( pic->width() + int(barkode->quietZone()*2 - barm), pic->height() + margin );
    } 
    else
        p.resize( pic->width() + margin, pic->height() + margin );

    p.fill( barkode->background() );
    TQPainter painter( &p );
    painter.drawPixmap( barkode->quietZone(), barkode->quietZone(), *pic, (int)sx, (int)sy, (int)sw, (int)sh );
    painter.end();

    return p;
}