File indexing completed on 2024-05-12 08:17:50

0001 /** 
0002  * Copyright (C) 2001 the KGhostView authors. See file AUTHORS.
0003  *  
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #ifndef DSCPARSE_ADAPTER_H
0020 #define DSCPARSE_ADAPTER_H
0021 
0022 #include <iostream>
0023 #include <map>
0024 #include <memory>
0025 
0026 #include <QSize>
0027 #include <QString>
0028 
0029 #include "dscparse.h"
0030 
0031 #ifndef KDE_USE_FINAL
0032 #undef min
0033 #undef max
0034 #endif
0035 
0036 class KDSCBBOX
0037 {
0038 public:
0039     KDSCBBOX(); 
0040     KDSCBBOX( const KDSCBBOX& b );
0041     KDSCBBOX( int llx, int lly, int urx, int ury ); 
0042     KDSCBBOX( const CDSCBBOX& bbox ); 
0043 
0044     KDSCBBOX& operator = ( const KDSCBBOX& b );  
0045     
0046     bool operator == ( const KDSCBBOX& b ); 
0047     bool operator != ( const KDSCBBOX& b );
0048     
0049     int llx() const;
0050     int lly() const;
0051     int urx() const;
0052     int ury() const;
0053 
0054     int width()  const;
0055     int height() const;
0056 
0057     QSize size() const;
0058 
0059 private:
0060     int _llx, _lly, _urx, _ury;
0061 };
0062 
0063 std::ostream& operator << ( std::ostream&, const KDSCBBOX& );
0064 
0065 
0066 class KDSCError
0067 {
0068 public:
0069     enum Type
0070     {
0071     BBox             = CDSC_MESSAGE_BBOX,
0072     EarlyTrailer     = CDSC_MESSAGE_EARLY_TRAILER,
0073     EarlyEOF         = CDSC_MESSAGE_EARLY_EOF,
0074     PageInTrailer    = CDSC_MESSAGE_PAGE_IN_TRAILER,
0075     PageOrdinal      = CDSC_MESSAGE_PAGE_ORDINAL,
0076     PagesWrong       = CDSC_MESSAGE_PAGES_WRONG,
0077     EPSNoBBox        = CDSC_MESSAGE_EPS_NO_BBOX,
0078     EPSPages         = CDSC_MESSAGE_EPS_PAGES,
0079     NoMedia          = CDSC_MESSAGE_NO_MEDIA,
0080     AtEnd            = CDSC_MESSAGE_ATEND,
0081     DuplicateComment = CDSC_MESSAGE_DUP_COMMENT,
0082     DuplicateTrailer = CDSC_MESSAGE_DUP_TRAILER,
0083     BeginEnd         = CDSC_MESSAGE_BEGIN_END,
0084     BadSection       = CDSC_MESSAGE_BAD_SECTION,
0085     LongLine         = CDSC_MESSAGE_LONG_LINE,
0086     IncorrectUsage   = CDSC_MESSAGE_INCORRECT_USAGE
0087     };
0088 
0089     enum Severity
0090     {
0091     Information = CDSC_ERROR_INFORM,
0092     Warning     = CDSC_ERROR_WARN,
0093     Error       = CDSC_ERROR_ERROR
0094     };
0095 
0096     KDSCError( Type, Severity, const QByteArray& line, 
0097            unsigned int lineNumber );
0098     
0099     Type         type()       const;
0100     Severity     severity()   const;
0101     QByteArray     line()       const;
0102     unsigned int lineNumber() const;
0103     
0104 private:
0105     Type         _type;
0106     Severity     _severity;
0107     QByteArray     _line;
0108     unsigned int _lineNumber;
0109 };
0110 
0111 
0112 class KDSCErrorHandler
0113 {
0114 public:
0115     virtual ~KDSCErrorHandler() {}
0116     enum Response 
0117     { 
0118     Ok        = CDSC_RESPONSE_OK, 
0119     Cancel    = CDSC_RESPONSE_CANCEL,
0120     IgnoreAll = CDSC_RESPONSE_IGNORE_ALL
0121     };
0122     
0123     virtual Response error( const KDSCError& ) = 0;
0124 };
0125 
0126 class KDSCOkErrorHandler : public KDSCErrorHandler
0127 {
0128     Response error( const KDSCError& ) override;
0129 };
0130 
0131 class KDSCCommentHandler
0132 {
0133 public:
0134     virtual ~KDSCCommentHandler() {}
0135     enum Name
0136     {
0137     // Header section
0138     PSAdobe               = CDSC_PSADOBE,
0139     BeginComments         = CDSC_BEGINCOMMENTS,
0140     EndComments           = CDSC_ENDCOMMENTS,
0141     Pages                 = CDSC_PAGES,
0142     Creator               = CDSC_CREATOR,
0143     CreationDate          = CDSC_CREATIONDATE,
0144     Title                 = CDSC_TITLE,
0145     For                   = CDSC_FOR,
0146     LanguageLevel         = CDSC_LANGUAGELEVEL,
0147     BoundingBox           = CDSC_BOUNDINGBOX,
0148     Orientation           = CDSC_ORIENTATION,
0149     PageOrder             = CDSC_PAGEORDER,
0150     DocumentMedia         = CDSC_DOCUMENTMEDIA,
0151     DocumentPaperSizes    = CDSC_DOCUMENTPAPERSIZES,
0152     DocumentPaperForms    = CDSC_DOCUMENTPAPERFORMS,
0153     DocumentPaperColors   = CDSC_DOCUMENTPAPERCOLORS,
0154     DocumentPaperWeights  = CDSC_DOCUMENTPAPERWEIGHTS,
0155     DocumentData          = CDSC_DOCUMENTDATA,
0156     Requirements          = CDSC_REQUIREMENTS,
0157     DocumentNeededFonts   = CDSC_DOCUMENTNEEDEDFONTS,
0158     DocumentSuppliedFonts = CDSC_DOCUMENTSUPPLIEDFONTS,
0159     HiResBoundingBox      = CDSC_HIRESBOUNDINGBOX,
0160     CropBox               = CDSC_CROPBOX,
0161     
0162     // Preview section
0163     BeginPreview          = CDSC_BEGINPREVIEW,
0164     EndPreview            = CDSC_ENDPREVIEW,
0165 
0166     // Defaults section
0167     BeginDefaults         = CDSC_BEGINDEFAULTS,
0168     EndDefaults           = CDSC_ENDDEFAULTS,
0169     // also %%PageMedia, %%PageOrientation, %%PageBoundingBox
0170 
0171     // Prolog section 
0172     BeginProlog           = CDSC_BEGINPROLOG,
0173     EndProlog             = CDSC_ENDPROLOG,
0174     BeginFont             = CDSC_BEGINFONT,
0175     EndFont               = CDSC_ENDFONT,
0176     BeginFeature          = CDSC_BEGINFEATURE,
0177     EndFeature            = CDSC_ENDFEATURE,
0178     BeginResource         = CDSC_BEGINRESOURCE,
0179     EndResource           = CDSC_ENDRESOURCE,
0180     BeginProcset          = CDSC_BEGINPROCSET,
0181     EndProcset            = CDSC_ENDPROCSET,
0182 
0183     // Setup section
0184     BeginSetup            = CDSC_BEGINSETUP,
0185     EndSetup              = CDSC_ENDSETUP,
0186     Feature               = CDSC_FEATURE,
0187     PaperColor            = CDSC_PAPERCOLOR,
0188     PaperForm             = CDSC_PAPERFORM,
0189     PaperWeight           = CDSC_PAPERWEIGHT,
0190     PaperSize             = CDSC_PAPERSIZE,
0191     // also %%Begin/EndFeature, %%Begin/EndResource
0192 
0193     // Page section
0194     Page                  = CDSC_PAGE,
0195     PageTrailer           = CDSC_PAGETRAILER,
0196     BeginPageSetup        = CDSC_BEGINPAGESETUP,
0197     EndPageSetup          = CDSC_ENDPAGESETUP,
0198     PageMedia             = CDSC_PAGEMEDIA,
0199     // also %%PaperColor, %%PaperForm, %%PaperWeight, %%PaperSize
0200     PageOrientation       = CDSC_PAGEORIENTATION,
0201     PageBoundingBox       = CDSC_PAGEBOUNDINGBOX,
0202     // also %%Begin/EndFont, %%Begin/EndFeature 
0203     // also %%Begin/EndResource, %%Begin/EndProcSet 
0204     IncludeFont           = CDSC_INCLUDEFONT,
0205     ViewingOrientation    = CDSC_VIEWINGORIENTATION,
0206 
0207     // Trailer section
0208     Trailer               = CDSC_TRAILER,
0209     // also %%Pages, %%BoundingBox, %%Orientation, %%PageOrder, 
0210     // %%DocumentMedia
0211     // %%Page is recognised as an error
0212     // also %%DocumentNeededFonts, %%DocumentSuppliedFonts
0213 
0214     // End of File */
0215     Eof                    = CDSC_EOF
0216     };
0217     
0218     virtual void comment( Name name ) { std::cout << name << std::endl; }
0219 };
0220 
0221 class KDSCScanHandler;
0222 class KDSC
0223 {
0224 public:
0225     KDSC();
0226     ~KDSC();
0227 
0228     /*--- Adapter for CDSC ------------------------------------------------*/
0229     QString dsc_version() const;
0230     
0231     bool dsc()   const;
0232     bool ctrld() const;
0233     bool pjl()   const;
0234     bool epsf()  const;
0235     bool pdf()   const;
0236     
0237     unsigned int preview()        const;
0238     unsigned int language_level() const;
0239     unsigned int document_data()  const;
0240 
0241     unsigned long begincomments() const;
0242     unsigned long endcomments()   const;
0243     unsigned long beginpreview()  const;
0244     unsigned long endpreview()    const;
0245     unsigned long begindefaults() const;
0246     unsigned long enddefaults()   const;
0247     unsigned long beginprolog()   const;
0248     unsigned long endprolog()     const;
0249     unsigned long beginsetup()    const;
0250     unsigned long endsetup()      const;
0251     unsigned long begintrailer()  const;
0252     unsigned long endtrailer()    const;
0253 
0254     CDSCPAGE* page() const;
0255 
0256     unsigned int page_count()       const;
0257     unsigned int page_pages()       const;
0258     unsigned int page_order()       const;
0259     unsigned int page_orientation() const;
0260 
0261     CDSCCTM* viewing_orientation() const;
0262     
0263     unsigned int media_count()    const;
0264     CDSCMEDIA** media()           const;
0265     const CDSCMEDIA* page_media() const;
0266 
0267     std::unique_ptr<KDSCBBOX> bbox()      const;
0268     std::unique_ptr<KDSCBBOX> page_bbox() const;
0269 
0270     // CDSCDOSEPS *doseps;
0271 
0272     QString dsc_title()   const;
0273     QString dsc_creator() const;
0274     QString dsc_date()    const;
0275     QString dsc_for()     const;
0276 
0277     // unsigned int max_error
0278    
0279     bool scanData( char*, unsigned int );
0280 
0281     /**
0282      * Tidy up from incorrect DSC comments.
0283      */
0284     int fixup();
0285     
0286     KDSCErrorHandler* errorHandler() const;
0287     void setErrorHandler( KDSCErrorHandler* );
0288     
0289     KDSCCommentHandler* commentHandler() const;
0290     void setCommentHandler( KDSCCommentHandler* );
0291     
0292     /*--- Extra methods for convenience -----------------------------------*/
0293     bool isStructured() const;
0294 
0295     /*--- Temporary -------------------------------------------------------*/
0296     CDSC* cdsc() const;
0297 
0298 protected:
0299     static int errorFunction( void* caller_data, CDSC* dsc, 
0300                               unsigned int explanation, 
0301                               const char* line, unsigned int line_len );
0302     
0303 private:
0304     CDSC*               _cdsc;
0305     KDSCErrorHandler*   _errorHandler;
0306     KDSCCommentHandler* _commentHandler;
0307     KDSCScanHandler*    _scanHandler;
0308 };
0309 
0310 class KDSCScanHandler
0311 {
0312 public:
0313     virtual ~KDSCScanHandler() {}
0314     KDSCScanHandler( CDSC* cdsc ) : _cdsc( cdsc ) {}
0315     
0316     virtual bool scanData( char* buf, unsigned int count )
0317     {
0318     return ( dsc_scan_data( _cdsc, buf, count ) >= 0 );
0319     }
0320     
0321 protected:
0322     CDSC* _cdsc;
0323 };
0324 
0325 class KDSCScanHandlerByLine : public KDSCScanHandler
0326 {
0327 public:
0328     KDSCScanHandlerByLine( CDSC* cdsc, KDSCCommentHandler* commentHandler ) : 
0329     KDSCScanHandler( cdsc ),
0330     _commentHandler( commentHandler ) 
0331     {}
0332     
0333     bool scanData( char* buf, unsigned int count ) override;
0334 
0335 protected:
0336     KDSCCommentHandler* _commentHandler;
0337 };
0338 
0339 #endif
0340 
0341 // vim:sw=4:sts=4:ts=8:noet