summaryrefslogtreecommitdiffstats
path: root/kghostview/dscparse/dscparse_adapter.cpp
blob: b04e89bc0b2ee5facafbee68974fd345ce305e01 (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
/** 
 * Copyright (C) 2001 the KGhostView authors. See file AUTHORS.
 * 	
 * 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.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "dscparse_adapter.h"

using namespace std;

/*-- KDSCBBOX implementation -----------------------------------------------*/

KDSCBBOX::KDSCBBOX() :
    _llx( 0 ), _lly( 0 ),
    _urx( 0 ), _ury( 0 )
{}

KDSCBBOX::KDSCBBOX( const KDSCBBOX& b ) :
    _llx( b._llx ), _lly( b._lly ),
    _urx( b._urx ), _ury( b._ury )
{}

KDSCBBOX::KDSCBBOX( int llx, int lly, int urx, int ury ) :
    _llx( llx ), _lly( lly ), 
    _urx( urx ), _ury( ury ) 
{}

KDSCBBOX::KDSCBBOX( const CDSCBBOX& bbox ) :
    _llx( bbox.llx ), _lly( bbox.lly ), 
    _urx( bbox.urx ), _ury( bbox.ury ) 
{}

KDSCBBOX& KDSCBBOX::operator = ( const KDSCBBOX& b ) 
{ 
    _llx = b._llx; _lly = b._lly; _urx = b._urx; _ury = b._ury;
    return *this; 
}

bool KDSCBBOX::operator == ( const KDSCBBOX& b ) 
{ 
    return ( _llx == b._llx && _lly == b._lly 
	  && _urx == b._urx && _ury == b._ury ); 
}

bool KDSCBBOX::operator != ( const KDSCBBOX& b ) 
{ 
    return !( *this == b ); 
}

int KDSCBBOX::llx() const { return _llx; }
int KDSCBBOX::lly() const { return _lly; }
int KDSCBBOX::urx() const { return _urx; }
int KDSCBBOX::ury() const { return _ury; }

int KDSCBBOX::width()  const { return _urx - _llx; }
int KDSCBBOX::height() const { return _ury - _lly; }

TQSize KDSCBBOX::size() const { return TQSize( width(), height() ); }

ostream& operator << ( ostream& os, const KDSCBBOX& source )
{
    os << "{ llx: "<< source.llx() << ", lly: " << source.lly()
       <<  " urx: "<< source.urx() << ", ury: " << source.ury() << " }";
    return os;
}

/*-- KDSCError implementation ----------------------------------------------*/

KDSCError::KDSCError( Type type, Severity severity, const TQCString& line,
	              unsigned int lineNumber ) :
    _type( type ),
    _severity( severity ),
    _line( line ),
    _lineNumber( lineNumber )
{}

KDSCError::Type KDSCError::type() const
{
    return _type;
}

KDSCError::Severity KDSCError::severity() const
{
    return _severity; 
}

TQCString KDSCError::line() const
{
    return _line; 
}

unsigned int KDSCError::lineNumber() const
{
    return _lineNumber; 
}

/*-- KDSCOkErrorHandler implementation -------------------------------------*/

KDSCErrorHandler::Response KDSCOkErrorHandler::error( const KDSCError& err ) 
{
    cout << "KDSC: error in line " << err.lineNumber() << endl;
    cout << err.line() << endl;
    return Ok;
}

/*-- KDSC implementation ---------------------------------------------------*/

KDSC::KDSC() :
    _errorHandler( 0 ),
    _commentHandler( 0 )
{
    _cdsc = dsc_init( this );
    Q_ASSERT( _cdsc != 0 );
    _scanHandler = new KDSCScanHandler( _cdsc );
}

KDSC::~KDSC()
{
    dsc_free( _cdsc );
    delete _scanHandler;
}

TQString KDSC::dsc_version() const
{
    return TQString( _cdsc->dsc_version );   
}

bool KDSC::dsc() const
{
    return ( _cdsc->dsc == TRUE );
}

bool KDSC::ctrld() const
{
    return ( _cdsc->ctrld == TRUE );
}

bool KDSC::pjl() const
{
    return ( _cdsc->pjl == TRUE );
}

bool KDSC::epsf() const
{
    return ( _cdsc->epsf == TRUE );
}

bool KDSC::pdf() const
{
    return ( _cdsc->pdf == TRUE );
}

unsigned int KDSC::preview() const
{
    return _cdsc->preview;
}

unsigned int KDSC::language_level() const
{
    return _cdsc->language_level;
}

unsigned int KDSC::document_data() const
{
    return _cdsc->document_data;
}

unsigned long KDSC::begincomments() const
{
    return _cdsc->begincomments;
}

unsigned long KDSC::endcomments() const
{
    return _cdsc->endcomments;
}

unsigned long KDSC::beginpreview() const
{
    return _cdsc->beginpreview;
}

unsigned long KDSC::endpreview() const
{
    return _cdsc->endpreview;
}

unsigned long KDSC::begindefaults() const
{
    return _cdsc->begindefaults;
}

unsigned long KDSC::enddefaults() const
{
    return _cdsc->enddefaults;
}

unsigned long KDSC::beginprolog() const
{
    return _cdsc->beginprolog;
}

unsigned long KDSC::endprolog() const
{
    return _cdsc->endprolog;
}

unsigned long KDSC::beginsetup() const
{
    return _cdsc->beginsetup;
}

unsigned long KDSC::endsetup() const
{
    return _cdsc->endsetup;
}

unsigned long KDSC::begintrailer() const
{
    return _cdsc->begintrailer;
}

unsigned long KDSC::endtrailer() const
{
    return _cdsc->endtrailer;
}

CDSCPAGE* KDSC::page() const
{
    return _cdsc->page;
}

unsigned int KDSC::page_count() const
{
    return _cdsc->page_count;
}

unsigned int KDSC::page_pages() const
{
    return _cdsc->page_pages;
}

unsigned int KDSC::page_order() const
{
    return _cdsc->page_order;
}

unsigned int KDSC::page_orientation() const
{
    return _cdsc->page_orientation;
}

CDSCCTM* KDSC::viewing_orientation() const
{
    return _cdsc->viewing_orientation;
}

unsigned int KDSC::media_count() const
{
    return _cdsc->media_count;
}

CDSCMEDIA** KDSC::media() const
{
    return _cdsc->media;
}

const CDSCMEDIA* KDSC::page_media() const
{
    return _cdsc->page_media;
}

auto_ptr<KDSCBBOX> KDSC::bbox() const
{
    if( _cdsc->bbox == 0 )
	return auto_ptr<KDSCBBOX>( 0 );
    else
	return auto_ptr<KDSCBBOX>( new KDSCBBOX( *_cdsc->bbox ) );
}

auto_ptr<KDSCBBOX> KDSC::page_bbox() const
{
    if( _cdsc->page_bbox == 0 )
	return auto_ptr<KDSCBBOX>( 0 );
    else
	return auto_ptr<KDSCBBOX>( new KDSCBBOX( *_cdsc->page_bbox ) );
}

TQString KDSC::dsc_title() const
{
    return TQString( _cdsc->dsc_title );
}

TQString KDSC::dsc_creator() const
{
    return TQString( _cdsc->dsc_creator );
}

TQString KDSC::dsc_date() const
{
    return TQString( _cdsc->dsc_date );
}

TQString KDSC::dsc_for() const
{
    return TQString( _cdsc->dsc_for );
}

bool KDSC::scanData( char* buffer, unsigned int count )
{
    return _scanHandler->scanData( buffer, count );
}

int KDSC::fixup()
{
    return dsc_fixup( _cdsc );
}

KDSCErrorHandler* KDSC::errorHandler() const
{
    return _errorHandler;
}

void KDSC::setErrorHandler( KDSCErrorHandler* errorHandler )
{
    _errorHandler = errorHandler;
    if( errorHandler == 0 )
	dsc_set_error_function( _cdsc, 0 );
    else
	dsc_set_error_function( _cdsc, &errorFunction );
}

KDSCCommentHandler* KDSC::commentHandler() const
{
    return _commentHandler;
}

void KDSC::setCommentHandler( KDSCCommentHandler* commentHandler )
{
    if( _commentHandler != 0 && commentHandler == 0 )
    {
	delete _scanHandler;
	_scanHandler = new KDSCScanHandler( _cdsc );
    }
    else if( _commentHandler == 0 && commentHandler != 0 )
    {
	delete _scanHandler;
	_scanHandler = new KDSCScanHandlerByLine( _cdsc, commentHandler );
    }
    _commentHandler = commentHandler;
}

bool KDSC::isStructured() const 
{
    return epsf() ? ( page_count() > 1 ) : ( page_count() > 0 );
}

CDSC* KDSC::cdsc() const
{
    return _cdsc;
}

int KDSC::errorFunction( void* caller_data, CDSC* dsc,
	unsigned int explanation, const char* line, unsigned int line_len )
{
    KDSCError error( 
	    static_cast< KDSCError::Type >( explanation ), 
	    static_cast< KDSCError::Severity >( dsc->severity[explanation] ),
	    TQCString( line, line_len + 1 ),
	    dsc->line_count
    );
    
    KDSC* kdsc = static_cast< KDSC* >( caller_data );
    Q_ASSERT( kdsc );
    
    return kdsc->errorHandler()->error( error );
}

bool KDSCScanHandlerByLine::scanData( char* buf, unsigned int count )
{
    char* lineStart = buf;
    char* it = buf;
    while( it < buf + count )
    {
	if( *it++ == '\n' )
	{
	    int retval = dsc_scan_data( _cdsc, lineStart, it - lineStart );
	    if( retval < 0 ) 
		return false;
	    else if( retval > 0 )
	    {
		_commentHandler->comment( 
			static_cast<KDSCCommentHandler::Name>( retval ) );
	    }
	    lineStart = it;
	}
    }
  
    if( it != lineStart )
    {
	// Scan the remaining part of the string.
	return ( dsc_scan_data( _cdsc, lineStart, it - lineStart ) < 0 );
    }
    else
	return true;
}

// vim:sw=4:sts=4:ts=8:noet