summaryrefslogtreecommitdiffstats
path: root/kbarcode/tbarcode2.cpp
blob: 27e57882a3e3e47e56167b5ef95f34a94ce3b0bd (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
/***************************************************************************
                       tbarcode2.cpp  -  description
                             -------------------
    begin                : Mon Feb 20 2006
    copyright            : (C) 2006 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 "tbarcode2.h"
#include "barkode.h"

#include <stdlib.h>

#include <tqdom.h>
#include <tqfile.h>

#include <kprocess.h>
#include <ktempfile.h>

TBarcodeOptions::TBarcodeOptions()
{
    defaults();
}

void TBarcodeOptions::defaults()
{
    m_escape = false;
    m_above = false;
    m_autocorrect = false;
    m_checksum = 0;
    m_modulewidth = 0.353;
    m_height = 20;
}

void TBarcodeOptions::load( const TQDomElement* tag )
{
    this->setModuleWidth( tag->attribute( "tbarcode.modulewidth", "0.353" ).toDouble() );
    this->setEscape( tag->attribute( "tbarcode.escape", "0" ).toInt() );
    this->setAbove( tag->attribute( "tbarcode.above", "0" ).toInt() );
    this->setAutocorrect( tag->attribute ( "tbarcode.autocorrect", "0" ).toInt() );
    this->setCheckSum( tag->attribute( "tbarcode.checksum", "0" ).toInt() );
    this->setHeight( tag->attribute( "tbarcode.height", "0" ).toInt() );
}

void TBarcodeOptions::save( TQDomElement* tag )
{
    tag->setAttribute( "tbarcode.modulewidth", this->moduleWidth() );
    tag->setAttribute( "tbarcode.escape", this->escape() );
    tag->setAttribute( "tbarcode.above", this->above() );
    tag->setAttribute( "tbarcode.autocorrect", this->autocorrect() );
    tag->setAttribute( "tbarcode.checksum", this->checksum() );
    tag->setAttribute( "tbarcode.height", this->height() );
}

const TBarcodeOptions& TBarcodeOptions::operator=( const BarkodeEngineOptions& rhs )
{
    const TBarcodeOptions* tbarcode = (dynamic_cast<const TBarcodeOptions*>(&rhs));

    this->m_escape      = tbarcode->m_escape;
    this->m_above       = tbarcode->m_above;
    this->m_autocorrect = tbarcode->m_autocorrect;
    this->m_checksum    = tbarcode->m_checksum;
    this->m_modulewidth = tbarcode->m_modulewidth;
    this->m_height      = tbarcode->m_height;

    return *this;
}

TBarcode2::TBarcode2()
    : PixmapBarcode()
{

}

TBarcode2::~TBarcode2()
{

}

const TBarcode2 & TBarcode2::operator=( const BarkodeEngine & rhs )
{
    const TBarcode2* barcode = dynamic_cast<const TBarcode2*>(&rhs);

    if( barcode ) 
    {
        m_options = barcode->m_options;
    }

    return *this;
}

bool TBarcode2::createPostscript( char** postscript, long* postscript_size )
{
    TQString cmd;
    const char* text;

    const TBarcodeOptions* tbarcode = (dynamic_cast<TBarcodeOptions*>(barkode->engine()->options()));
    if( !tbarcode )
        return false;

    if( !barkode->textVisible() )
        text = "hide";
    else if( tbarcode->above() )
        text = "above";
    else
        text = "below";

    cmd = cmd.sprintf( "tbarcode -f PS -b %s -d %s  -t %s --translation=%s --autocorrect=%s --modulewidth=%.3f -h %i --checkdigit=%i --72dpiraster\n", 
                       barkode->type().latin1(), KShellProcess::quote(  barkode->parsedValue() ).latin1(), 
                       text, tbarcode->escape() ? "on" : "off", 
                       tbarcode->autocorrect() ? "on" : "off", 
                       tbarcode->moduleWidth(), 
                       tbarcode->height(),
                       tbarcode->checksum() );

    qDebug( "Cmd = %s", cmd.ascii() );
    if( !readFromPipe( cmd.latin1(), postscript, postscript_size ) )
        return false;

    return true;
}

TQRect TBarcode2::bbox( const char* postscript, long postscript_size )
{
    const char* gs_bbox = "gs -sDEVICE=bbox -sNOPAUSE -q %1 -c showpage quit 2>&1";

    char*   buffer = NULL;
    long    len    = 0;
    TQRect   size;

    KTempFile psfile( TQString(), ".ps" );
    psfile.file()->writeBlock( postscript, postscript_size );
    psfile.file()->close();

    if( !readFromPipe( TQString( gs_bbox ).arg( psfile.file()->name() ).latin1(), &buffer, &len ) || !len )
    {
        psfile.unlink();
        return TQRect( 0, 0, 0, 0 );
    }
    else
        psfile.unlink();

    size = PixmapBarcode::bbox( buffer, len );
    free( buffer );

    return size;

}