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