File indexing completed on 2024-05-12 04:20:03

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 #include "dscparse_adapter.h"
0020 
0021 using namespace std;
0022 
0023 /*-- KDSCBBOX implementation -----------------------------------------------*/
0024 
0025 KDSCBBOX::KDSCBBOX() :
0026     _llx( 0 ), _lly( 0 ),
0027     _urx( 0 ), _ury( 0 )
0028 {}
0029 
0030 KDSCBBOX::KDSCBBOX( const KDSCBBOX& b ) :
0031     _llx( b._llx ), _lly( b._lly ),
0032     _urx( b._urx ), _ury( b._ury )
0033 {}
0034 
0035 KDSCBBOX::KDSCBBOX( int llx, int lly, int urx, int ury ) :
0036     _llx( llx ), _lly( lly ), 
0037     _urx( urx ), _ury( ury ) 
0038 {}
0039 
0040 KDSCBBOX::KDSCBBOX( const CDSCBBOX& bbox ) :
0041     _llx( bbox.llx ), _lly( bbox.lly ), 
0042     _urx( bbox.urx ), _ury( bbox.ury ) 
0043 {}
0044 
0045 KDSCBBOX& KDSCBBOX::operator = ( const KDSCBBOX& b ) 
0046 { 
0047     _llx = b._llx; _lly = b._lly; _urx = b._urx; _ury = b._ury;
0048     return *this; 
0049 }
0050 
0051 bool KDSCBBOX::operator == ( const KDSCBBOX& b ) 
0052 { 
0053     return ( _llx == b._llx && _lly == b._lly 
0054       && _urx == b._urx && _ury == b._ury ); 
0055 }
0056 
0057 bool KDSCBBOX::operator != ( const KDSCBBOX& b ) 
0058 { 
0059     return !( *this == b ); 
0060 }
0061 
0062 int KDSCBBOX::llx() const { return _llx; }
0063 int KDSCBBOX::lly() const { return _lly; }
0064 int KDSCBBOX::urx() const { return _urx; }
0065 int KDSCBBOX::ury() const { return _ury; }
0066 
0067 int KDSCBBOX::width()  const { return _urx - _llx; }
0068 int KDSCBBOX::height() const { return _ury - _lly; }
0069 
0070 QSize KDSCBBOX::size() const { return QSize( width(), height() ); }
0071 
0072 ostream& operator << ( ostream& os, const KDSCBBOX& source )
0073 {
0074     os << "{ llx: "<< source.llx() << ", lly: " << source.lly()
0075        <<  " urx: "<< source.urx() << ", ury: " << source.ury() << " }";
0076     return os;
0077 }
0078 
0079 /*-- KDSCError implementation ----------------------------------------------*/
0080 
0081 KDSCError::KDSCError( Type type, Severity severity, const QByteArray& line,
0082                   unsigned int lineNumber ) :
0083     _type( type ),
0084     _severity( severity ),
0085     _line( line ),
0086     _lineNumber( lineNumber )
0087 {}
0088 
0089 KDSCError::Type KDSCError::type() const
0090 {
0091     return _type;
0092 }
0093 
0094 KDSCError::Severity KDSCError::severity() const
0095 {
0096     return _severity; 
0097 }
0098 
0099 QByteArray KDSCError::line() const
0100 {
0101     return _line; 
0102 }
0103 
0104 unsigned int KDSCError::lineNumber() const
0105 {
0106     return _lineNumber; 
0107 }
0108 
0109 /*-- KDSCOkErrorHandler implementation -------------------------------------*/
0110 
0111 KDSCErrorHandler::Response KDSCOkErrorHandler::error( const KDSCError& err ) 
0112 {
0113     cout << "KDSC: error in line " << err.lineNumber() << endl;
0114     //cout << err.line() << endl;
0115     return Ok;
0116 }
0117 
0118 /*-- KDSC implementation ---------------------------------------------------*/
0119 
0120 KDSC::KDSC() :
0121     _errorHandler( nullptr ),
0122     _commentHandler( nullptr )
0123 {
0124     _cdsc = dsc_init( this );
0125     Q_ASSERT( _cdsc != nullptr );
0126     _scanHandler = new KDSCScanHandler( _cdsc );
0127 }
0128 
0129 KDSC::~KDSC()
0130 {
0131     dsc_free( _cdsc );
0132     delete _scanHandler;
0133 }
0134 
0135 QString KDSC::dsc_version() const
0136 {
0137     return QString( _cdsc->dsc_version );   
0138 }
0139 
0140 bool KDSC::dsc() const
0141 {
0142     return ( _cdsc->dsc == TRUE );
0143 }
0144 
0145 bool KDSC::ctrld() const
0146 {
0147     return ( _cdsc->ctrld == TRUE );
0148 }
0149 
0150 bool KDSC::pjl() const
0151 {
0152     return ( _cdsc->pjl == TRUE );
0153 }
0154 
0155 bool KDSC::epsf() const
0156 {
0157     return ( _cdsc->epsf == TRUE );
0158 }
0159 
0160 bool KDSC::pdf() const
0161 {
0162     return ( _cdsc->pdf == TRUE );
0163 }
0164 
0165 unsigned int KDSC::preview() const
0166 {
0167     return _cdsc->preview;
0168 }
0169 
0170 unsigned int KDSC::language_level() const
0171 {
0172     return _cdsc->language_level;
0173 }
0174 
0175 unsigned int KDSC::document_data() const
0176 {
0177     return _cdsc->document_data;
0178 }
0179 
0180 unsigned long KDSC::begincomments() const
0181 {
0182     return _cdsc->begincomments;
0183 }
0184 
0185 unsigned long KDSC::endcomments() const
0186 {
0187     return _cdsc->endcomments;
0188 }
0189 
0190 unsigned long KDSC::beginpreview() const
0191 {
0192     return _cdsc->beginpreview;
0193 }
0194 
0195 unsigned long KDSC::endpreview() const
0196 {
0197     return _cdsc->endpreview;
0198 }
0199 
0200 unsigned long KDSC::begindefaults() const
0201 {
0202     return _cdsc->begindefaults;
0203 }
0204 
0205 unsigned long KDSC::enddefaults() const
0206 {
0207     return _cdsc->enddefaults;
0208 }
0209 
0210 unsigned long KDSC::beginprolog() const
0211 {
0212     return _cdsc->beginprolog;
0213 }
0214 
0215 unsigned long KDSC::endprolog() const
0216 {
0217     return _cdsc->endprolog;
0218 }
0219 
0220 unsigned long KDSC::beginsetup() const
0221 {
0222     return _cdsc->beginsetup;
0223 }
0224 
0225 unsigned long KDSC::endsetup() const
0226 {
0227     return _cdsc->endsetup;
0228 }
0229 
0230 unsigned long KDSC::begintrailer() const
0231 {
0232     return _cdsc->begintrailer;
0233 }
0234 
0235 unsigned long KDSC::endtrailer() const
0236 {
0237     return _cdsc->endtrailer;
0238 }
0239 
0240 CDSCPAGE* KDSC::page() const
0241 {
0242     return _cdsc->page;
0243 }
0244 
0245 unsigned int KDSC::page_count() const
0246 {
0247     return _cdsc->page_count;
0248 }
0249 
0250 unsigned int KDSC::page_pages() const
0251 {
0252     return _cdsc->page_pages;
0253 }
0254 
0255 unsigned int KDSC::page_order() const
0256 {
0257     return _cdsc->page_order;
0258 }
0259 
0260 unsigned int KDSC::page_orientation() const
0261 {
0262     return _cdsc->page_orientation;
0263 }
0264 
0265 CDSCCTM* KDSC::viewing_orientation() const
0266 {
0267     return _cdsc->viewing_orientation;
0268 }
0269 
0270 unsigned int KDSC::media_count() const
0271 {
0272     return _cdsc->media_count;
0273 }
0274 
0275 CDSCMEDIA** KDSC::media() const
0276 {
0277     return _cdsc->media;
0278 }
0279 
0280 const CDSCMEDIA* KDSC::page_media() const
0281 {
0282     return _cdsc->page_media;
0283 }
0284 
0285 std::unique_ptr<KDSCBBOX> KDSC::bbox() const
0286 {
0287     if( _cdsc->bbox == nullptr )
0288     return nullptr;
0289     else
0290     return std::make_unique<KDSCBBOX>( *_cdsc->bbox );
0291 }
0292 
0293 std::unique_ptr<KDSCBBOX> KDSC::page_bbox() const
0294 {
0295     if( _cdsc->page_bbox == nullptr )
0296     return nullptr;
0297     else
0298     return std::make_unique<KDSCBBOX>( *_cdsc->page_bbox );
0299 }
0300 
0301 QString KDSC::dsc_title() const
0302 {
0303     return QString( _cdsc->dsc_title );
0304 }
0305 
0306 QString KDSC::dsc_creator() const
0307 {
0308     return QString( _cdsc->dsc_creator );
0309 }
0310 
0311 QString KDSC::dsc_date() const
0312 {
0313     return QString( _cdsc->dsc_date );
0314 }
0315 
0316 QString KDSC::dsc_for() const
0317 {
0318     return QString( _cdsc->dsc_for );
0319 }
0320 
0321 bool KDSC::scanData( char* buffer, unsigned int count )
0322 {
0323     return _scanHandler->scanData( buffer, count );
0324 }
0325 
0326 int KDSC::fixup()
0327 {
0328     return dsc_fixup( _cdsc );
0329 }
0330 
0331 KDSCErrorHandler* KDSC::errorHandler() const
0332 {
0333     return _errorHandler;
0334 }
0335 
0336 void KDSC::setErrorHandler( KDSCErrorHandler* errorHandler )
0337 {
0338     _errorHandler = errorHandler;
0339     if( errorHandler == nullptr )
0340     dsc_set_error_function( _cdsc, nullptr );
0341     else
0342     dsc_set_error_function( _cdsc, &errorFunction );
0343 }
0344 
0345 KDSCCommentHandler* KDSC::commentHandler() const
0346 {
0347     return _commentHandler;
0348 }
0349 
0350 void KDSC::setCommentHandler( KDSCCommentHandler* commentHandler )
0351 {
0352     if( _commentHandler != nullptr && commentHandler == nullptr )
0353     {
0354     delete _scanHandler;
0355     _scanHandler = new KDSCScanHandler( _cdsc );
0356     }
0357     else if( _commentHandler == nullptr && commentHandler != nullptr )
0358     {
0359     delete _scanHandler;
0360     _scanHandler = new KDSCScanHandlerByLine( _cdsc, commentHandler );
0361     }
0362     _commentHandler = commentHandler;
0363 }
0364 
0365 bool KDSC::isStructured() const 
0366 {
0367     return epsf() ? ( page_count() > 1 ) : ( page_count() > 0 );
0368 }
0369 
0370 CDSC* KDSC::cdsc() const
0371 {
0372     return _cdsc;
0373 }
0374 
0375 int KDSC::errorFunction( void* caller_data, CDSC* dsc,
0376     unsigned int explanation, const char* line, unsigned int line_len )
0377 {
0378     KDSCError error( 
0379         static_cast< KDSCError::Type >( explanation ), 
0380         static_cast< KDSCError::Severity >( dsc->severity[explanation] ),
0381         QByteArray( line, line_len + 1 ),
0382         dsc->line_count
0383     );
0384     
0385     KDSC* kdsc = static_cast< KDSC* >( caller_data );
0386     Q_ASSERT( kdsc );
0387     
0388     return kdsc->errorHandler()->error( error );
0389 }
0390 
0391 bool KDSCScanHandlerByLine::scanData( char* buf, unsigned int count )
0392 {
0393     char* lineStart = buf;
0394     char* it = buf;
0395     while( it < buf + count )
0396     {
0397     if( *it++ == '\n' )
0398     {
0399         int retval = dsc_scan_data( _cdsc, lineStart, it - lineStart );
0400         if( retval < 0 ) 
0401         return false;
0402         else if( retval > 0 )
0403         {
0404         _commentHandler->comment( 
0405             static_cast<KDSCCommentHandler::Name>( retval ) );
0406         }
0407         lineStart = it;
0408     }
0409     }
0410   
0411     if( it != lineStart )
0412     {
0413     // Scan the remaining part of the string.
0414     return ( dsc_scan_data( _cdsc, lineStart, it - lineStart ) < 0 );
0415     }
0416     else
0417     return true;
0418 }
0419 
0420 // vim:sw=4:sts=4:ts=8:noet