filters

pdfdocument.cpp
1/*
2 * Copyright (c) 2002 Nicolas HADACEK (hadacek@kde.org)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "pdfdocument.h"
21
22#include <math.h>
23
24#include <kdebug.h>
25#include <tdeglobal.h>
26
27#include "GlobalParams.h"
28#include "PDFDoc.h"
29
30#include "FilterDevice.h"
31#include "misc.h"
32
33
34namespace PDFImport
35{
36
38 : _file(0), _object(0), _fileStream(0), _document(0), _device(0)
39{}
40
41KoFilter::ConversionStatus
42Document::init(const TQString &name, const TQString &ownerPassword,
43 const TQString &userPassword)
44{
45 clear();
46
47 _file = new TQFile(name);
48 if( !_file->open(IO_ReadOnly) ) return KoFilter::FileNotFound;
49
50 FILE *fd = fdopen(_file->handle(), "r");
51 if ( fd==0 ) return KoFilter::InternalError;
52
53 globalParams = new GlobalParams(0);
54 globalParams->setErrQuiet(gFalse);
55
56 _object = new Object;
57 _object->initNull();
58 _fileStream = new FileStream(fd, 0, gFalse, 0, _object);
59 GString *owner =
60 (ownerPassword.isEmpty() ? 0 : new GString(ownerPassword.latin1()));
61 GString *user =
62 (userPassword.isEmpty() ? 0 : new GString(userPassword.latin1()));
63 _document = new PDFDoc(_fileStream, owner, user);
64 delete user;
65 delete owner;
66
67 if ( !_document->isOk() ) return KoFilter::WrongFormat;
68 Font::init();
69 return KoFilter::OK;
70}
71
72void Document::clear()
73{
74 Font::cleanup();
75 delete _device;
76 _device = 0;
77 delete _document;
78 _document = 0;
79 // _fileStream is deleted by PDFDoc
80 // _object is free()ed by FileStream
81 _object = 0;
82 _fileStream = 0;
83 delete globalParams;
84 globalParams = 0;
85 delete _file;
86 _file = 0;
87 _imageIndex = 1;
88}
89
90TQString Document::info(const TQCString &key) const
91{
92 TQString res;
93 Object info;
94 _document->getDocInfo(&info);
95 if ( info.isDict() ) {
96 Object tmp;
97 if ( info.getDict()->lookup(key.data(), &tmp)->isString() ) {
98 GString *s = tmp.getString();
99 bool isUnicode = false;
100 int i = 0;
101 Unicode u;
102 if ( (s->getChar(0) & 0xff)==0xfe &&
103 (s->getChar(1) & 0xff)==0xff ) {
104 isUnicode = true;
105 i = 2;
106 }
107 while ( i<s->getLength() ) {
108 if (isUnicode) {
109 u = ( (s->getChar(i) & 0xff)<<8 ) |
110 (s->getChar(i+1) & 0xff);
111 i++;
112 } else u = s->getChar(i) & 0xff;
113 i++;
114 res += TQChar(u);
115 }
116 tmp.free();
117 }
118 }
119 info.free();
120 return res;
121}
122
123uint Document::nbPages() const
124{
125 return _document->getNumPages();
126}
127
128KoOrientation Document::paperOrientation() const
129{
130 if ( nbPages()==0 ) return PG_PORTRAIT;
131 return ( _document->getPageWidth(1)<_document->getPageHeight(1) ?
132 PG_PORTRAIT : PG_LANDSCAPE );
133}
134
135DRect Document::paperSize(KoFormat &format) const
136{
137 KoOrientation orientation = paperOrientation();
138
139 double w, h;
140 if ( nbPages()==0 ) {
141 format = PG_DIN_A4;
142 w = mmToPoint(KoPageFormat::width(format, orientation));
143 h = mmToPoint(KoPageFormat::height(format, orientation));
144 return DRect(0, w, 0, h);
145 }
146
147 w = _document->getPageWidth(1);
148 h = _document->getPageHeight(1);
149 format = PG_CUSTOM;
150 double min = kMin(w, h);
151 double max = kMax(w, h);
152 double best = 2;
153 double width = w;
154 double height = h;
155 for (uint i=0; i<=PG_LAST_FORMAT; i++) {
156 if ( i==PG_CUSTOM || i==PG_SCREEN ) continue; // #### koffice 1.2
157 w = mmToPoint(KoPageFormat::width(KoFormat(i), orientation));
158 h = mmToPoint(KoPageFormat::height(KoFormat(i), orientation));
159 double v = fabs(min/w - 1) + fabs(max/h - 1);
160 if ( v<best ) {
161 best = v;
162 if ( best<0.1 ) {
163 format = KoFormat(i);
164 width = w;
165 height = h;
166 }
167 }
168 }
169 return DRect(0, width, 0, height);
170}
171
172void Document::initDevice(Data &data)
173{
174 Q_ASSERT( _device==0 );
175 _device = new Device(data);
176}
177
178void Document::init()
179{
180 _device->init();
181}
182
183void Document::treatPage(uint i)
184{
185 if (i == 0)
186 i = 1;
187 _document->displayPage(_device, i, int(72*1), 0, gTrue);
188}
189
190bool Document::isEncrypted() const
191{
192 return _document->getXRef()->isEncrypted();
193}
194
195void Document::dumpPage(uint i)
196{
197 _device->dumpPage(i);
198}
199
200} // namespace