summaryrefslogtreecommitdiffstats
path: root/kbarcode/purepostscript.cpp
blob: cdbf1e9b5de964dc9403e3598ef15fbc987c9afc (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
/***************************************************************************
                          purepostscript.cpp  -  description
                             -------------------
    begin                : Mon Jan 2 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 "purepostscript.h"
#include "barkode.h"

#include <stdlib.h>

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

#include <kstandarddirs.h>
#include <ktempfile.h>

#define MAX_LINE_LENGTH 256
#define BEGIN_TEMPLATE "--BEGIN TEMPLATE--"
#define END_TEMPLATE "--END TEMPLATE--"

TQString PurePostscriptBarcode::s_path = TQString();

PurePostscriptOptions::PurePostscriptOptions()
    : BarkodeEngineOptions()
{
    defaults();
}

const BarkodeEngineOptions& PurePostscriptOptions::operator=( const BarkodeEngineOptions& rhs )
{
    const PurePostscriptOptions* ps = (dynamic_cast<const PurePostscriptOptions*>(&rhs));

    m_check = ps->m_check;

    return *this;
}

void PurePostscriptOptions::defaults()
{
    m_check = false;
}

void PurePostscriptOptions::load( const TQDomElement* tag )
{
    m_check = (bool)tag->attribute( "pure.check", "0" ).toInt();
}

void PurePostscriptOptions::save( TQDomElement* tag )
{
    tag->setAttribute( "pure.check", (int)m_check );
}

PurePostscriptBarcode::PurePostscriptBarcode()
    : PixmapBarcode()
{
    bool append = false;
    TQString line;

    if( s_path.isNull() )
    {
        qDebug( "Cannot locate barcode writer in pure postscript." );
        return;
    }

    TQFile pureFile( s_path );
    if( pureFile.open( IO_ReadOnly ) )
    {
        while( pureFile.readLine( line, MAX_LINE_LENGTH ) != -1 )
        {
            if( append ) 
            {
                if( line.contains( END_TEMPLATE ) )
                    break;
                    
                m_program.append( line );
            }

            if( !append && line.contains( BEGIN_TEMPLATE ) )
                append = true;
        }
        pureFile.close();
    }
}

PurePostscriptBarcode::~PurePostscriptBarcode()
{
}

void PurePostscriptBarcode::init()
{
    if( s_path.isNull() )
    {
        // first look at the default location
        const char* default_barcode = "/usr/share/libpostscriptbarcode/barcode.ps";
        if( TQFile::exists( default_barcode ) )
            s_path = default_barcode;
        else
            s_path = locate( "data", "kbarcode/barcode.ps" );
    }

    if( !TQFile::exists( s_path ) )
        s_path = TQString();
}

#define START_TOKEN "% --"
#define BEGIN_ENCODER "BEGIN ENCODER "
#define DESCRIPTION "DESC: "
#define EXAMPLE "EXAM: "

void PurePostscriptBarcode::initInfo( TBarcodeInfoList* info )
{
    PurePostscriptBarcode::init();

    TQFile pureFile( s_path );
    if( pureFile.open( IO_ReadOnly ) )
    {
        TQString encoder;
        TQString description;
        TQString example;
        TQString line;
        
        while( pureFile.readLine( line, MAX_LINE_LENGTH ) != -1 )
        {
            /*
              % --BEGIN ENCODER ean13--
              % --DESC: EAN-13
              % --EXAM: 977147396801
            */
            
            if( line.startsWith( START_TOKEN ) ) 
            {
                // remove all whitespaces on the line ending (and '-')
                line = line.stripWhiteSpace();

                line = line.right( line.length() - TQString( START_TOKEN ).length() );
                if( line.startsWith( BEGIN_ENCODER ) ) 
                {
                    encoder = line.right( line.length() - TQString( BEGIN_ENCODER ).length() );

                    if( encoder.endsWith( "--" ) )
                        encoder = encoder.left( encoder.length() - 2 );
                }
                else if( line.startsWith( DESCRIPTION ) )
                    description = line.right( line.length() - TQString( DESCRIPTION ).length() );
                else if( line.startsWith( EXAMPLE ) )
                {
                    example = line.right( line.length() - TQString( EXAMPLE ).length() );

                    // we should have a complete encoder now.
                    info->append( Barkode::createInfo( TQString("ps_") + encoder, description, PURE_POSTSCRIPT, PUREADV | COLORED ) );
                }
            }
        }
        pureFile.close();
    }
}

bool PurePostscriptBarcode::hasPurePostscriptBarcode()
{
    return !s_path.isNull();
}

void PurePostscriptBarcode::createProgram( TQString & prg )
{
    const PurePostscriptOptions* options = (dynamic_cast<const PurePostscriptOptions*>(barkode->engine()->options()));
    TQString type = barkode->type().right( barkode->type().length() - 3 );
    TQString opt;
    
    opt.sprintf( "%s %s barcolor=%02X%02X%02X showbackground backgroundcolor=%02X%02X%02X textcolor=%02X%02X%02X", 
                 barkode->textVisible() ? "includetext" : "",
                 options && options->checksum() ? "includecheck" : "",
                 barkode->foreground().red(), barkode->foreground().green(), barkode->foreground().blue(),
                 barkode->background().red(), barkode->background().green(), barkode->background().blue(), 
                 barkode->textColor().red(), barkode->textColor().green(), barkode->textColor().blue()
                 );

    prg = "%!PS-Adobe-2.0 EPSF-2.0\n%%EndComments\n%%EndProlog\n";
    prg += m_program;
    prg += TQString("20 20 moveto\n(%1) (%2) %3 barcode\n")
        .tqarg( barkode->parsedValue() )
        .tqarg( opt ).tqarg( type );
}

TQRect PurePostscriptBarcode::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 ).tqarg( 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;
}

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

    if( m_program.isEmpty() )
        return false;

    createProgram( cmd );

    *postscript_size = cmd.length();
    *postscript = (char*)malloc( sizeof(char) * *postscript_size );
    if( !*postscript ) 
        return false;

    memcpy( *postscript, cmd.latin1(), *postscript_size );

    return true;
}