summaryrefslogtreecommitdiffstats
path: root/kbarcode/gnubarcode.cpp
blob: deef484b9f93bca5864e740af8cbe0bfb672ca5d (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
/***************************************************************************
                          gnubarcode.cpp  -  description
                             -------------------
    begin                : 
    copyright            : (C) 2005 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 "gnubarcode.h"

#ifdef _ENABLE_NATIVE_GNU_BARCODE

#include <barcode.h>
#include "barkode.h"
#include <string.h>
#include <ctype.h>

#include <tqfont.h>
#include <tqpainter.h>
#include <tqpaintdevicemetrics.h>

#include <tdelocale.h>

#define FONT_SPACE (2*m_scaley)
#define SHRINK_AMOUNT 0.15
#define MUL_FACTOR 10

GnuBarcode::GnuBarcode()
    : BarkodeEngine()
{
    m_item = NULL;
    m_size = TQSize( 0, 0 );
    
    m_scalex = 1.0;
    m_scaley = 1.0;
    
    m_font_size = 10;
}

GnuBarcode::~GnuBarcode()
{
    if( m_item )
        Barcode_Delete( m_item );
}

void GnuBarcode::update( const TQPaintDevice* device )
{
    TQString val = barkode->parsedValue();
    m_valid = false;
        
    if( m_item )
    {
        tqDebug("Deleting");
        Barcode_Delete( m_item );
        m_item = NULL;
    }

    if( val.isEmpty() )
        return;

    char* value = new char[val.length()+1];
    strncpy( value, val.latin1(), val.length()+1 );
    value[val.length()] = '\0';
    
    m_item = Barcode_Create( value );
    if( !m_item )
    {
        Barcode_Delete( m_item );
        m_item = NULL;
        delete [] value;
        return;
    }
    
    if( Barcode_Encode( m_item, Barkode::internalType( barkode->type() ) ) == -1 )
    {
        Barcode_Delete( m_item );
        m_item = NULL;
        
        delete [] value;
        return;
    }

    delete [] value;
 
    TQPaintDeviceMetrics metrics( device );
    m_scalex = (double)metrics.logicalDpiX() / (double)TQPaintDevice::x11AppDpiX();
    m_scaley = (double)metrics.logicalDpiY() / (double)TQPaintDevice::x11AppDpiY();
   
    // 72.0 is the postscript resolution generated by GNU barcode
    m_scale_ps_x = (double)metrics.logicalDpiX() / 72.0;
    m_scale_ps_y = (double)metrics.logicalDpiY() / 72.0;
    
/*    m_scalex *= m_scale_ps_x;
    m_scaley *= m_scale_ps_y;*/
    
    m_font_size = (int)(barkode->fontsize() * barkode->scaling() * m_scaley);
    setupSize();
    m_valid = true;
}

const TQSize GnuBarcode::size() const
{
    if( m_size.isNull() )
        return TQSize( 100, 80 );
    else
        return m_size;
}

void GnuBarcode::drawBarcode( TQPainter & painter, int x, int y )
{
    int dx = x + barkode->quietZone();
    int dy = y + barkode->quietZone();
    
    if( !m_item )
    {
        barkode->drawInvalid( painter, x, y );
        return;
    }
    
    painter.save();
    painter.scale( m_scalex, m_scaley );

    // based on David J. Humphreys barcode-svg
    // Draw the bars
    drawBars( &painter, (int)(dx/m_scalex), (int)(dy/m_scaley) );

    // Only scale for the bars, text should be scaled 
    // correctly by TQt on TQt >= 3.3.2    
    painter.restore();
    
    // draw the text
    if( barkode->textVisible() )
        drawText( &painter, dx, dy );
}

void GnuBarcode::setupSize()
{
    m_bar_height = (int)(barkode->barHeight() * barkode->scaling() * barkode->cut());
    m_barcode_height = m_bar_height;
    if( barkode->textVisible() )
        m_barcode_height += (int)(m_font_size/2 + FONT_SPACE*2);
    
    unsigned int width = drawBars( 0 , 0, 0 );
    unsigned int twidth = drawText( 0, 0, 0 );
    
    //if(m_item->height == 0) 
    m_item->height = m_barcode_height + (2 * barkode->quietZone());
    
    width = ( width > twidth ? width : twidth );
    width += 2* barkode->quietZone();

    //if(m_item->width == 0)  
    m_item->width = width;

    m_size.setWidth( (int)(m_item->width * m_scalex) );
    m_size.setHeight( (int)(m_item->height * m_scaley) );
}

int GnuBarcode::drawBars( TQPainter* painter, int x, int y )
{
#if 0
    int height = 0; // height of the bar to draw
    int xpos = 0;   // Where the current box is drawn
    int current = 0;
    unsigned int i = 0;

    tqDebug("Partial=%s\n", m_item->partial );
    for( i = 0; i < strlen(m_item->partial); i++)
    {
        current = (int)m_item->partial[i] - ASCII_ZERO;
        /* Guide bar */
        if(current > 9)
        {
            height = m_barcode_height; 
            current = (int)m_item->partial[++i] - ASCII_ZERO;
            i++;   /* Skip the following 'a' */
        }
        else
            height = m_bar_height;

        current *= barkode->scaling();
        
        if( current < 0 )
        {
            tqDebug("current < 0: %i", current );
            current = 0;
            break;
        }
           
        if( i % 2)
        {
            painter.fillRect( x + xpos, y, current, height, barkode->foreground() );
        }
        
        xpos += current;
    }
#endif
    int xpos;
    int mode = '-'; // text below bars
    char *ptr;
    int i, j;
    int x0, yr, y0;
    
    xpos = m_item->partial[0]-'0';
    
    for (ptr = m_item->partial+1, i=1; *ptr; ptr++, i++) 
    {
        /* special cases: '+' and '-' */
        if (*ptr == '+' || *ptr == '-') 
        {
            mode = *ptr; /* don't count it */ i++; continue;
        }
        /* j is the width of this bar/space */
        if (isdigit (*ptr))   
            j = *ptr-'0';
        else
            j = *ptr-'a'+1;
        
        j = (int)( j * barkode->scaling() );
        if (i%2) 
        { 
            x0 = xpos + j/2;
            y0 = y;
            yr = m_barcode_height;
            
            if( barkode->textVisible() ) 
            { 
                // leave space for text
                if (mode == '-') 
                {
                    // text below bars
                    if( isdigit( *ptr ) )
                        yr = m_bar_height;
                    else
                        yr = m_barcode_height;
                } else { /* '+' */
                    // text above bars
                    // TODO: this needs testing and a GUI
                    if( isdigit( *ptr ) )
                        yr = m_bar_height;
                    else
                    {
                        yr = m_barcode_height;
                        y0 += (m_barcode_height - m_bar_height);
                    }
                }
            }
            
            if( painter )
            {
                painter->fillRect( x + x0, y0, j, yr, barkode->foreground() ); 
            }
            //tqDebug("Bar  = %i", j );
        }
        xpos += j;
    }
    
    return xpos;
}

int GnuBarcode::drawText( TQPainter* painter, int x, int y )
{
#if 0
    unsigned int infosz = strlen( m_item->textinfo );
    unsigned int correction = 0;    // This correction seems to be needed to align text properly
    unsigned int j = 0;
    unsigned int i = 0;
    int prev_x = 0;
    int xpos = 0;

    double dub1, fsize;
    char printer;
    char *temp_info = new char[infosz+1];

    painter.setFont( TQFont( "Helvetica", 12 ) );
    while(i < infosz)
    {
        for(j = 0; m_item->textinfo[i + j + 1] != ' ' &&
            m_item->textinfo[i + j + 1] != '\0';j++);   /* Empty loop, just increment j */
        j ++;
        strncpy(temp_info, (m_item->textinfo + i),j);
        sscanf(temp_info, "%lf:%lf:%c", &dub1, &fsize, &printer);
        i += j;

        xpos = (int)dub1;
        //if((xpos - prev_x) >= 10)
        //correction += 2;

        prev_x = xpos;
        painter.drawText( x + ((xpos-correction) * m_scalex), 
                          y + ((m_bar_height + (unsigned int)(barkode->fontsize()/ 2) + FONT_SPACE) * m_scaley), 
                          TQChar( printer ) );
    }
    delete [] temp_info;
#endif

    double f1, f2;
    int y0;
    char* ptr;
    char c;
  
    int mode = '-';
    for (ptr = m_item->textinfo; ptr; ptr = strchr(ptr, ' ')) 
    {
        while (*ptr == ' ') 
            ptr++;
        if (!*ptr) 
            break;
        if (*ptr == '+' || *ptr == '-') 
        {
            mode = *ptr; continue;
        }
        
        if (sscanf(ptr, "%lf:%lf:%c", &f1, &f2, &c) != 3) 
        {
            fprintf(stderr, "barcode: impossible data: %s\n", ptr);
            continue;
        }

        if( mode == '-' )
        {
            // text below bars
            y0 = y + (int)(m_bar_height * m_scaley + m_font_size - FONT_SPACE);
        }
        else
            y0 = (int)(y + FONT_SPACE);
            
        if( painter )
        {
            painter->setFont( TQFont( "Helvetica", m_font_size ) );
            painter->drawText( x + (int)(f1 * m_scalex * barkode->scaling() ), y0, TQChar( c ) );
        }
    }
    
    return x;
}

#endif // _ENABLE_NATIVE_GNU_BARCODE