1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* poppler-ps-converter.cc: qt interface to poppler
* Copyright (C) 2007, 2009, 2010, Albert Astals Cid <aacid@kde.org>
* Copyright (C) 2008, Pino Toscano <pino@kde.org>
* Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
* Copyright (C) 2011 Glad Deschrijver <glad.deschrijver@gmail.com>
* Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it>
* Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "poppler-qt4.h"
#include "poppler-private.h"
#include "poppler-converter-private.h"
#include "PSOutputDev.h"
static void outputToQIODevice(void *stream, const char *data, int len)
{
static_cast<QIODevice*>(stream)->write(data, len);
}
namespace Poppler {
class PSConverterPrivate : public BaseConverterPrivate
{
public:
PSConverterPrivate();
QList<int> pageList;
QString title;
double hDPI;
double vDPI;
int rotate;
int paperWidth;
int paperHeight;
int marginRight;
int marginBottom;
int marginLeft;
int marginTop;
PSConverter::PSOptions opts;
void (* pageConvertedCallback)(int page, void *payload);
void *pageConvertedPayload;
};
PSConverterPrivate::PSConverterPrivate()
: BaseConverterPrivate(),
hDPI(72), vDPI(72), rotate(0), paperWidth(-1), paperHeight(-1),
marginRight(0), marginBottom(0), marginLeft(0), marginTop(0),
opts(PSConverter::Printing), pageConvertedCallback(0),
pageConvertedPayload(0)
{
}
PSConverter::PSConverter(DocumentData *document)
: BaseConverter(*new PSConverterPrivate())
{
Q_D(PSConverter);
d->document = document;
}
PSConverter::~PSConverter()
{
}
void PSConverter::setPageList(const QList<int> &pageList)
{
Q_D(PSConverter);
d->pageList = pageList;
}
void PSConverter::setTitle(const QString &title)
{
Q_D(PSConverter);
d->title = title;
}
void PSConverter::setHDPI(double hDPI)
{
Q_D(PSConverter);
d->hDPI = hDPI;
}
void PSConverter::setVDPI(double vDPI)
{
Q_D(PSConverter);
d->vDPI = vDPI;
}
void PSConverter::setRotate(int rotate)
{
Q_D(PSConverter);
d->rotate = rotate;
}
void PSConverter::setPaperWidth(int paperWidth)
{
Q_D(PSConverter);
d->paperWidth = paperWidth;
}
void PSConverter::setPaperHeight(int paperHeight)
{
Q_D(PSConverter);
d->paperHeight = paperHeight;
}
void PSConverter::setRightMargin(int marginRight)
{
Q_D(PSConverter);
d->marginRight = marginRight;
}
void PSConverter::setBottomMargin(int marginBottom)
{
Q_D(PSConverter);
d->marginBottom = marginBottom;
}
void PSConverter::setLeftMargin(int marginLeft)
{
Q_D(PSConverter);
d->marginLeft = marginLeft;
}
void PSConverter::setTopMargin(int marginTop)
{
Q_D(PSConverter);
d->marginTop = marginTop;
}
void PSConverter::setStrictMargins(bool strictMargins)
{
Q_D(PSConverter);
if (strictMargins)
d->opts |= StrictMargins;
else
d->opts &= ~StrictMargins;
}
void PSConverter::setForceRasterize(bool forceRasterize)
{
Q_D(PSConverter);
if (forceRasterize)
d->opts |= ForceRasterization;
else
d->opts &= ~ForceRasterization;
}
void PSConverter::setPSOptions(PSConverter::PSOptions options)
{
Q_D(PSConverter);
d->opts = options;
}
PSConverter::PSOptions PSConverter::psOptions() const
{
Q_D(const PSConverter);
return d->opts;
}
void PSConverter::setPageConvertedCallback(void (* callback)(int page, void *payload), void *payload)
{
Q_D(PSConverter);
d->pageConvertedCallback = callback;
d->pageConvertedPayload = payload;
}
static GBool annotDisplayDecideCbk(Annot *annot, void *user_data)
{
if (annot->getType() == Annot::typeWidget)
return gTrue; // Never hide forms
else
return *(GBool*)user_data;
}
bool PSConverter::convert()
{
Q_D(PSConverter);
d->lastError = NoError;
Q_ASSERT(!d->pageList.isEmpty());
Q_ASSERT(d->paperWidth != -1);
Q_ASSERT(d->paperHeight != -1);
if (d->document->locked)
{
d->lastError = FileLockedError;
return false;
}
QIODevice *dev = d->openDevice();
if (!dev)
{
d->lastError = OpenOutputError;
return false;
}
QByteArray pstitle8Bit = d->title.toLocal8Bit();
char* pstitlechar;
if (!d->title.isEmpty()) pstitlechar = pstitle8Bit.data();
else pstitlechar = 0;
PSOutputDev *psOut = new PSOutputDev(outputToQIODevice, dev,
pstitlechar,
d->document->doc,
1,
d->document->doc->getNumPages(),
(d->opts & PrintToEPS) ? psModeEPS : psModePS,
d->paperWidth,
d->paperHeight,
gFalse,
gFalse,
d->marginLeft,
d->marginBottom,
d->paperWidth - d->marginRight,
d->paperHeight - d->marginTop,
(d->opts & ForceRasterization));
if (d->opts & StrictMargins)
{
double xScale = ((double)d->paperWidth - (double)d->marginLeft - (double)d->marginRight) / (double)d->paperWidth;
double yScale = ((double)d->paperHeight - (double)d->marginBottom - (double)d->marginTop) / (double)d->paperHeight;
psOut->setScale(xScale, yScale);
}
if (psOut->isOk())
{
GBool isPrinting = (d->opts & Printing) ? gTrue : gFalse;
GBool showAnnotations = (d->opts & HideAnnotations) ? gFalse : gTrue;
foreach(int page, d->pageList)
{
d->document->doc->displayPage(psOut,
page,
d->hDPI,
d->vDPI,
d->rotate,
gFalse,
gTrue,
isPrinting,
NULL,
NULL,
annotDisplayDecideCbk,
&showAnnotations, gTrue);
if (d->pageConvertedCallback)
(*d->pageConvertedCallback)(page, d->pageConvertedPayload);
}
delete psOut;
d->closeDevice();
return true;
}
else
{
delete psOut;
d->closeDevice();
return false;
}
}
}
|