filters

pdfdocument.cpp

00001 /*
00002  * Copyright (c) 2002 Nicolas HADACEK (hadacek@kde.org)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "pdfdocument.h"
00021 
00022 #include <math.h>
00023 
00024 #include <kdebug.h>
00025 #include <kglobal.h>
00026 
00027 #include "GlobalParams.h"
00028 #include "PDFDoc.h"
00029 
00030 #include "FilterDevice.h"
00031 #include "misc.h"
00032 
00033 
00034 namespace PDFImport
00035 {
00036 
00037 Document::Document()
00038     : _file(0), _object(0), _fileStream(0), _document(0), _device(0)
00039 {}
00040 
00041 KoFilter::ConversionStatus
00042 Document::init(const QString &name, const QString &ownerPassword,
00043                const QString &userPassword)
00044 {
00045     clear();
00046 
00047     _file = new QFile(name);
00048     if( !_file->open(IO_ReadOnly) ) return KoFilter::FileNotFound;
00049 
00050     FILE *fd = fdopen(_file->handle(), "r");
00051     if ( fd==0 ) return KoFilter::InternalError;
00052 
00053     globalParams = new GlobalParams(0);
00054     globalParams->setErrQuiet(gFalse);
00055 
00056     _object = new Object;
00057     _object->initNull();
00058     _fileStream = new FileStream(fd, 0, gFalse, 0, _object);
00059     GString *owner =
00060         (ownerPassword.isEmpty() ? 0 : new GString(ownerPassword.latin1()));
00061     GString *user =
00062         (userPassword.isEmpty() ? 0 : new GString(userPassword.latin1()));
00063     _document = new PDFDoc(_fileStream, owner, user);
00064     delete user;
00065     delete owner;
00066 
00067     if ( !_document->isOk() ) return KoFilter::WrongFormat;
00068     Font::init();
00069     return KoFilter::OK;
00070 }
00071 
00072 void Document::clear()
00073 {
00074     Font::cleanup();
00075     delete _device;
00076     _device = 0;
00077     delete _document;
00078     _document = 0;
00079     // _fileStream is deleted by PDFDoc
00080     // _object is free()ed by FileStream
00081     _object = 0;
00082     _fileStream = 0;
00083     delete globalParams;
00084     globalParams = 0;
00085     delete _file;
00086     _file = 0;
00087     _imageIndex = 1;
00088 }
00089 
00090 QString Document::info(const QCString &key) const
00091 {
00092     QString res;
00093     Object info;
00094     _document->getDocInfo(&info);
00095     if ( info.isDict() ) {
00096         Object tmp;
00097         if ( info.getDict()->lookup(key.data(), &tmp)->isString() ) {
00098             GString *s = tmp.getString();
00099             bool isUnicode = false;
00100             int i = 0;
00101             Unicode u;
00102             if ( (s->getChar(0) & 0xff)==0xfe &&
00103                  (s->getChar(1) & 0xff)==0xff ) {
00104                 isUnicode = true;
00105                 i = 2;
00106             }
00107             while ( i<s->getLength() ) {
00108                 if (isUnicode) {
00109                     u = ( (s->getChar(i) & 0xff)<<8 ) |
00110                         (s->getChar(i+1) & 0xff);
00111                     i++;
00112                 } else u = s->getChar(i) & 0xff;
00113                 i++;
00114                 res += QChar(u);
00115             }
00116             tmp.free();
00117         }
00118     }
00119     info.free();
00120     return res;
00121 }
00122 
00123 uint Document::nbPages() const
00124 {
00125     return _document->getNumPages();
00126 }
00127 
00128 KoOrientation Document::paperOrientation() const
00129 {
00130     if ( nbPages()==0 ) return PG_PORTRAIT;
00131     return ( _document->getPageWidth(1)<_document->getPageHeight(1) ?
00132              PG_PORTRAIT : PG_LANDSCAPE );
00133 }
00134 
00135 DRect Document::paperSize(KoFormat &format) const
00136 {
00137     KoOrientation orientation = paperOrientation();
00138 
00139     double w, h;
00140     if ( nbPages()==0 ) {
00141         format = PG_DIN_A4;
00142         w = mmToPoint(KoPageFormat::width(format, orientation));
00143         h = mmToPoint(KoPageFormat::height(format, orientation));
00144         return DRect(0, w, 0, h);
00145     }
00146 
00147     w = _document->getPageWidth(1);
00148     h = _document->getPageHeight(1);
00149     format = PG_CUSTOM;
00150     double min = kMin(w, h);
00151     double max = kMax(w, h);
00152     double best = 2;
00153     double width = w;
00154     double height = h;
00155     for (uint i=0; i<=PG_LAST_FORMAT; i++) {
00156         if ( i==PG_CUSTOM || i==PG_SCREEN ) continue; // #### koffice 1.2
00157         w = mmToPoint(KoPageFormat::width(KoFormat(i), orientation));
00158         h = mmToPoint(KoPageFormat::height(KoFormat(i), orientation));
00159         double v = fabs(min/w - 1) + fabs(max/h - 1);
00160         if ( v<best ) {
00161             best = v;
00162             if ( best<0.1 ) {
00163                 format = KoFormat(i);
00164                 width = w;
00165                 height = h;
00166             }
00167         }
00168     }
00169     return DRect(0, width, 0, height);
00170 }
00171 
00172 void Document::initDevice(Data &data)
00173 {
00174     Q_ASSERT( _device==0 );
00175     _device = new Device(data);
00176 }
00177 
00178 void Document::init()
00179 {
00180     _device->init();
00181 }
00182 
00183 void Document::treatPage(uint i)
00184 {
00185     if (i == 0)
00186         i = 1;
00187     _document->displayPage(_device, i, int(72*1), 0, gTrue);
00188 }
00189 
00190 bool Document::isEncrypted() const
00191 {
00192     return _document->getXRef()->isEncrypted();
00193 }
00194 
00195 void Document::dumpPage(uint i)
00196 {
00197     _device->dumpPage(i);
00198 }
00199 
00200 } // namespace
KDE Home | KDE Accessibility Home | Description of Access Keys