Coverage Report

Created: 2026-01-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kdegraphics-thumbnailers/ps/dscparse_adapter.h
Line
Count
Source
1
/** 
2
 * SPDX-FileCopyrightText: 2001 the KGhostView authors. See file AUTHORS.
3
 *  
4
 * SPDX-License-Identifier: GPL-2.0-or-later
5
 */
6
7
#ifndef DSCPARSE_ADAPTER_H
8
#define DSCPARSE_ADAPTER_H
9
10
#include <iostream>
11
#include <map>
12
#include <memory>
13
14
#include <QSize>
15
#include <QString>
16
17
#include "dscparse.h"
18
19
#ifndef KDE_USE_FINAL
20
#undef min
21
#undef max
22
#endif
23
24
class KDSCBBOX
25
{
26
public:
27
    KDSCBBOX(); 
28
    KDSCBBOX( const KDSCBBOX& b );
29
    KDSCBBOX( int llx, int lly, int urx, int ury ); 
30
    KDSCBBOX( const CDSCBBOX& bbox ); 
31
32
    KDSCBBOX& operator = ( const KDSCBBOX& b );  
33
    
34
    bool operator == ( const KDSCBBOX& b ); 
35
    bool operator != ( const KDSCBBOX& b );
36
    
37
    int llx() const;
38
    int lly() const;
39
    int urx() const;
40
    int ury() const;
41
42
    int width()  const;
43
    int height() const;
44
45
    QSize size() const;
46
47
private:
48
    int _llx, _lly, _urx, _ury;
49
};
50
51
std::ostream& operator << ( std::ostream&, const KDSCBBOX& );
52
53
54
class KDSCError
55
{
56
public:
57
    enum Type
58
    {
59
  BBox             = CDSC_MESSAGE_BBOX,
60
  EarlyTrailer     = CDSC_MESSAGE_EARLY_TRAILER,
61
  EarlyEOF         = CDSC_MESSAGE_EARLY_EOF,
62
  PageInTrailer    = CDSC_MESSAGE_PAGE_IN_TRAILER,
63
  PageOrdinal      = CDSC_MESSAGE_PAGE_ORDINAL,
64
  PagesWrong       = CDSC_MESSAGE_PAGES_WRONG,
65
  EPSNoBBox        = CDSC_MESSAGE_EPS_NO_BBOX,
66
  EPSPages         = CDSC_MESSAGE_EPS_PAGES,
67
  NoMedia          = CDSC_MESSAGE_NO_MEDIA,
68
  AtEnd            = CDSC_MESSAGE_ATEND,
69
  DuplicateComment = CDSC_MESSAGE_DUP_COMMENT,
70
  DuplicateTrailer = CDSC_MESSAGE_DUP_TRAILER,
71
  BeginEnd         = CDSC_MESSAGE_BEGIN_END,
72
  BadSection       = CDSC_MESSAGE_BAD_SECTION,
73
  LongLine         = CDSC_MESSAGE_LONG_LINE,
74
  IncorrectUsage   = CDSC_MESSAGE_INCORRECT_USAGE
75
    };
76
77
    enum Severity
78
    {
79
  Information = CDSC_ERROR_INFORM,
80
  Warning     = CDSC_ERROR_WARN,
81
  Error       = CDSC_ERROR_ERROR
82
    };
83
84
    KDSCError( Type, Severity, const QByteArray& line, 
85
         unsigned int lineNumber );
86
    
87
    Type         type()       const;
88
    Severity     severity()   const;
89
    QByteArray     line()       const;
90
    unsigned int lineNumber() const;
91
    
92
private:
93
    Type         _type;
94
    Severity     _severity;
95
    QByteArray     _line;
96
    unsigned int _lineNumber;
97
};
98
99
100
class KDSCErrorHandler
101
{
102
public:
103
0
    virtual ~KDSCErrorHandler() {}
104
    enum Response 
105
    { 
106
  Ok        = CDSC_RESPONSE_OK, 
107
  Cancel    = CDSC_RESPONSE_CANCEL,
108
  IgnoreAll = CDSC_RESPONSE_IGNORE_ALL
109
    };
110
    
111
    virtual Response error( const KDSCError& ) = 0;
112
};
113
114
class KDSCOkErrorHandler : public KDSCErrorHandler
115
{
116
    Response error( const KDSCError& ) override;
117
};
118
119
class KDSCCommentHandler
120
{
121
public:
122
5.38k
    virtual ~KDSCCommentHandler() {}
123
    enum Name
124
    {
125
  // Header section
126
  PSAdobe               = CDSC_PSADOBE,
127
  BeginComments         = CDSC_BEGINCOMMENTS,
128
  EndComments           = CDSC_ENDCOMMENTS,
129
  Pages                 = CDSC_PAGES,
130
  Creator               = CDSC_CREATOR,
131
  CreationDate          = CDSC_CREATIONDATE,
132
  Title                 = CDSC_TITLE,
133
  For                   = CDSC_FOR,
134
  LanguageLevel         = CDSC_LANGUAGELEVEL,
135
  BoundingBox           = CDSC_BOUNDINGBOX,
136
  Orientation           = CDSC_ORIENTATION,
137
  PageOrder             = CDSC_PAGEORDER,
138
  DocumentMedia         = CDSC_DOCUMENTMEDIA,
139
  DocumentPaperSizes    = CDSC_DOCUMENTPAPERSIZES,
140
  DocumentPaperForms    = CDSC_DOCUMENTPAPERFORMS,
141
  DocumentPaperColors   = CDSC_DOCUMENTPAPERCOLORS,
142
  DocumentPaperWeights  = CDSC_DOCUMENTPAPERWEIGHTS,
143
  DocumentData          = CDSC_DOCUMENTDATA,
144
  Requirements          = CDSC_REQUIREMENTS,
145
  DocumentNeededFonts   = CDSC_DOCUMENTNEEDEDFONTS,
146
  DocumentSuppliedFonts = CDSC_DOCUMENTSUPPLIEDFONTS,
147
  HiResBoundingBox      = CDSC_HIRESBOUNDINGBOX,
148
  CropBox               = CDSC_CROPBOX,
149
  
150
  // Preview section
151
  BeginPreview          = CDSC_BEGINPREVIEW,
152
  EndPreview            = CDSC_ENDPREVIEW,
153
154
  // Defaults section
155
  BeginDefaults         = CDSC_BEGINDEFAULTS,
156
  EndDefaults           = CDSC_ENDDEFAULTS,
157
  // also %%PageMedia, %%PageOrientation, %%PageBoundingBox
158
159
  // Prolog section 
160
  BeginProlog           = CDSC_BEGINPROLOG,
161
  EndProlog             = CDSC_ENDPROLOG,
162
  BeginFont             = CDSC_BEGINFONT,
163
  EndFont               = CDSC_ENDFONT,
164
  BeginFeature          = CDSC_BEGINFEATURE,
165
  EndFeature            = CDSC_ENDFEATURE,
166
  BeginResource         = CDSC_BEGINRESOURCE,
167
  EndResource           = CDSC_ENDRESOURCE,
168
  BeginProcset          = CDSC_BEGINPROCSET,
169
  EndProcset            = CDSC_ENDPROCSET,
170
171
  // Setup section
172
  BeginSetup            = CDSC_BEGINSETUP,
173
  EndSetup              = CDSC_ENDSETUP,
174
  Feature               = CDSC_FEATURE,
175
  PaperColor            = CDSC_PAPERCOLOR,
176
  PaperForm             = CDSC_PAPERFORM,
177
  PaperWeight           = CDSC_PAPERWEIGHT,
178
  PaperSize             = CDSC_PAPERSIZE,
179
  // also %%Begin/EndFeature, %%Begin/EndResource
180
181
  // Page section
182
  Page                  = CDSC_PAGE,
183
  PageTrailer           = CDSC_PAGETRAILER,
184
  BeginPageSetup        = CDSC_BEGINPAGESETUP,
185
  EndPageSetup          = CDSC_ENDPAGESETUP,
186
  PageMedia             = CDSC_PAGEMEDIA,
187
  // also %%PaperColor, %%PaperForm, %%PaperWeight, %%PaperSize
188
  PageOrientation       = CDSC_PAGEORIENTATION,
189
  PageBoundingBox       = CDSC_PAGEBOUNDINGBOX,
190
  // also %%Begin/EndFont, %%Begin/EndFeature 
191
  // also %%Begin/EndResource, %%Begin/EndProcSet 
192
  IncludeFont           = CDSC_INCLUDEFONT,
193
  ViewingOrientation    = CDSC_VIEWINGORIENTATION,
194
195
  // Trailer section
196
  Trailer               = CDSC_TRAILER,
197
  // also %%Pages, %%BoundingBox, %%Orientation, %%PageOrder, 
198
  // %%DocumentMedia
199
  // %%Page is recognised as an error
200
  // also %%DocumentNeededFonts, %%DocumentSuppliedFonts
201
202
  // End of File */
203
  Eof                    = CDSC_EOF
204
    };
205
  
206
0
    virtual void comment( Name name ) { std::cout << name << std::endl; }
207
};
208
209
class KDSCScanHandler;
210
class KDSC
211
{
212
public:
213
    KDSC();
214
    ~KDSC();
215
216
    /*--- Adapter for CDSC ------------------------------------------------*/
217
    QString dsc_version() const;
218
    
219
    bool dsc()   const;
220
    bool ctrld() const;
221
    bool pjl()   const;
222
    bool epsf()  const;
223
    bool pdf()   const;
224
    
225
    unsigned int preview()        const;
226
    unsigned int language_level() const;
227
    unsigned int document_data()  const;
228
229
    unsigned long begincomments() const;
230
    unsigned long endcomments()   const;
231
    unsigned long beginpreview()  const;
232
    unsigned long endpreview()    const;
233
    unsigned long begindefaults() const;
234
    unsigned long enddefaults()   const;
235
    unsigned long beginprolog()   const;
236
    unsigned long endprolog()     const;
237
    unsigned long beginsetup()    const;
238
    unsigned long endsetup()      const;
239
    unsigned long begintrailer()  const;
240
    unsigned long endtrailer()    const;
241
242
    CDSCPAGE* page() const;
243
244
    unsigned int page_count()       const;
245
    unsigned int page_pages()       const;
246
    unsigned int page_order()       const;
247
    unsigned int page_orientation() const;
248
249
    CDSCCTM* viewing_orientation() const;
250
    
251
    unsigned int media_count()    const;
252
    CDSCMEDIA** media()           const;
253
    const CDSCMEDIA* page_media() const;
254
255
    std::unique_ptr<KDSCBBOX> bbox()      const;
256
    std::unique_ptr<KDSCBBOX> page_bbox() const;
257
258
    // CDSCDOSEPS *doseps;
259
260
    QString dsc_title()   const;
261
    QString dsc_creator() const;
262
    QString dsc_date()    const;
263
    QString dsc_for()     const;
264
265
    // unsigned int max_error
266
   
267
    bool scanData( char*, unsigned int );
268
269
    /**
270
     * Tidy up from incorrect DSC comments.
271
     */
272
    int fixup();
273
    
274
    KDSCErrorHandler* errorHandler() const;
275
    void setErrorHandler( KDSCErrorHandler* );
276
    
277
    KDSCCommentHandler* commentHandler() const;
278
    void setCommentHandler( KDSCCommentHandler* );
279
    
280
    /*--- Extra methods for convenience -----------------------------------*/
281
    bool isStructured() const;
282
283
    /*--- Temporary -------------------------------------------------------*/
284
    CDSC* cdsc() const;
285
286
protected:
287
    static int errorFunction( void* caller_data, CDSC* dsc, 
288
                              unsigned int explanation, 
289
                              const char* line, unsigned int line_len );
290
    
291
private:
292
    CDSC*               _cdsc;
293
    KDSCErrorHandler*   _errorHandler;
294
    KDSCCommentHandler* _commentHandler;
295
    KDSCScanHandler*    _scanHandler;
296
};
297
298
class KDSCScanHandler
299
{
300
public:
301
10.7k
    virtual ~KDSCScanHandler() {}
302
10.7k
    KDSCScanHandler( CDSC* cdsc ) : _cdsc( cdsc ) {}
303
    
304
    virtual bool scanData( char* buf, unsigned int count )
305
0
    {
306
0
  return ( dsc_scan_data( _cdsc, buf, count ) >= 0 );
307
0
    }
308
    
309
protected:
310
    CDSC* _cdsc;
311
};
312
313
class KDSCScanHandlerByLine : public KDSCScanHandler
314
{
315
public:
316
    KDSCScanHandlerByLine( CDSC* cdsc, KDSCCommentHandler* commentHandler ) : 
317
5.38k
  KDSCScanHandler( cdsc ),
318
5.38k
  _commentHandler( commentHandler ) 
319
5.38k
    {}
320
    
321
    bool scanData( char* buf, unsigned int count ) override;
322
323
protected:
324
    KDSCCommentHandler* _commentHandler;
325
};
326
327
#endif
328
329
// vim:sw=4:sts=4:ts=8:noet