Repository navigation
Expand file tree
/
Copy pathrenderer.cpp
More file actions
654 lines (600 loc) · 24.9 KB
/
Copy pathrenderer.cpp
File metadata and controls
654 lines (600 loc) · 24.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
Copyright © 2008-13 Qtrac Ltd. All rights reserved.
This program or module 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 of
the License, 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.
*/
#include "renderer.h"
#include "pagecompare.h"
#include "aboutform.hpp"
#include "sequence_matcher.hpp"
#include "textitem.hpp"
#include <QFont>
#include <algorithm>
#include <QImage>
#include <QPageLayout>
#include <QPainter>
#include <QPainterPath>
#include <QPixmapCache>
#include <QPrinter>
#include <QTextOption>
int RenderSettings::dpi() const
{
return static_cast<int>(POINTS_PER_INCH * (zoom / 100.0));
}
QRectF RenderSettings::pointRectForMargins(const QSize &pageSize) const
{
return rectForMargins(pageSize.width(), pageSize.height(), topMargin,
bottomMargin, leftMargin, rightMargin);
}
QRect RenderSettings::pixelRectForMargins(const QSize &imageSize) const
{
const int DPI = dpi();
int top = pixelOffsetForPointValue(DPI, topMargin);
int left = pixelOffsetForPointValue(DPI, leftMargin);
int right = pixelOffsetForPointValue(DPI, rightMargin);
int bottom = pixelOffsetForPointValue(DPI, bottomMargin);
// QSize, not a second QPoint: the two-point form of QRect is
// inclusive, so it would cover one pixel more to the right and below
// than rectForMargins() gives in points
return QRect(QPoint(left, top),
QSize(imageSize.width() - right - left,
imageSize.height() - bottom - top));
}
DifferenceRenderer::DifferenceRenderer(const RenderSettings &settings)
: settings(settings)
{
}
QPair<QString, QString> DifferenceRenderer::cacheKeys(
const QString &filename1, const QString &filename2,
const PagePair &pair) const
{
QString zoning;
if (settings.zoning)
zoning = QString("%1:%2:%3").arg(settings.columns)
.arg(settings.toleranceR).arg(settings.toleranceY);
QString margins;
if (settings.excludeMargins)
margins = QString("%1:%2:%3:%4").arg(settings.topMargin)
.arg(settings.bottomMargin).arg(settings.leftMargin)
.arg(settings.rightMargin);
const QString highlighting = QString("%1:%2:%3:%4:%5:%6:%7:%8")
.arg(settings.pen.color().name()).arg(settings.pen.style())
.arg(settings.brush.style()).arg(settings.opacity)
.arg(settings.squareSize).arg(settings.ruleWidth)
.arg(settings.overlap).arg(settings.combineTextHighlighting);
QString box;
if (settings.box != PageBox::None)
box = pageBoxName(settings.box) + "/" + boxClipName(settings.boxClip);
const QString key = QString("%1%2:%3:%4:%5:%6:%7:%8:%9")
.arg(pair.differs ? "D" : "S")
.arg(pair.hasVisualDifference ? "V" : "T")
.arg(settings.zoom).arg(settings.compareMode)
.arg(settings.compositionMode).arg(zoning).arg(margins)
.arg(box).arg(highlighting);
// documentKey() rather than the name alone: re-exporting a PDF over
// itself leaves the name unchanged, and a pixmap cached from the
// previous contents would then be shown for the new ones. The
// comparison keys its fingerprints the same way.
const QString key1 = QString("1:%1:%2:%3").arg(key).arg(pair.left)
.arg(documentKey(filename1));
const QString key2 = QString("2:%1:%2:%3").arg(key).arg(pair.right)
.arg(documentKey(filename2));
return qMakePair(key1, key2);
}
QPair<QPixmap, QPixmap> DifferenceRenderer::pixmaps(const PdfPage &page1,
const QString &filename1, const PdfPage &page2,
const QString &filename2, const PagePair &pair,
const BoxedView *view1, const BoxedView *view2) const
{
QPair<QString, QString> keys = cacheKeys(filename1, filename2, pair);
const bool wholePages = view1 && view2;
if (wholePages) {
keys.first += ":whole";
keys.second += ":whole";
}
QPixmap pixmap1;
QPixmap pixmap2;
if (QPixmapCache::find(keys.first, &pixmap1) &&
QPixmapCache::find(keys.second, &pixmap2))
return qMakePair(pixmap1, pixmap2);
const int DPI = settings.dpi();
const bool compareText = settings.compareMode != CompareAppearance;
// Highlighting the differences, rather than composing page 2 over
// page 1
const bool highlighting = compareText ||
settings.compositionMode == -1;
QImage image1 = page1->renderToImage(DPI, DPI);
QImage image2 = page2->renderToImage(DPI, DPI);
// The renderings as they are before anything is drawn on them.
// QImage is implicitly shared and painting on image1 detaches it, so
// these cost nothing until a highlight is painted: each page used to
// be rendered a second time for them -- and for the compositing
// modes, where they are not read at all, that second rendering was
// made and thrown away.
QImage plainImage1;
QImage plainImage2;
if (highlighting && (pair.hasVisualDifference || !compareText)) {
plainImage1 = image1;
plainImage2 = image2;
}
if (highlighting) {
QPainterPath highlighted1;
QPainterPath highlighted2;
if (pair.hasVisualDifference || !compareText)
computeVisualHighlights(&highlighted1, &highlighted2,
plainImage1, plainImage2);
else
computeTextHighlights(&highlighted1, &highlighted2, page1,
page2, DPI);
if (!highlighted1.isEmpty())
paintOnImage(highlighted1, &image1);
if (!highlighted2.isEmpty())
paintOnImage(highlighted2, &image2);
if (pair.differs && highlighted1.isEmpty() &&
highlighted2.isEmpty()) {
QFont font("Helvetica", 14);
font.setOverline(true);
font.setUnderline(true);
// Pages of different sizes differ even where nothing on them
// does, and there is nowhere to highlight that; say so,
// rather than call the comparison wrong
const QString banner = image1.size() != image2.size()
? tr("%1: Page Sizes Differ")
.arg(AboutForm::ProgramName)
: tr("%1: False Positive").arg(AboutForm::ProgramName);
highlighted1.addText(DPI / 4, DPI / 4, font, banner);
paintOnImage(highlighted1, &image1);
}
if (wholePages) {
image1 = onWholePage(image1, *view1, pair.left, DPI);
image2 = onWholePage(image2, *view2, pair.right, DPI);
}
pixmap1 = QPixmap::fromImage(image1);
pixmap2 = QPixmap::fromImage(image2);
} else {
pixmap1 = QPixmap::fromImage(image1);
QImage composed(image1.size(), image1.format());
QPainter painter(&composed);
painter.setCompositionMode(QPainter::CompositionMode_Source);
painter.fillRect(composed.rect(), Qt::transparent);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.drawImage(0, 0, image1);
painter.setCompositionMode(static_cast<QPainter::CompositionMode>(
settings.compositionMode));
painter.drawImage(0, 0, image2);
painter.setCompositionMode(
QPainter::CompositionMode_DestinationOver);
painter.fillRect(composed.rect(), Qt::white);
painter.end();
// The pages are composed where they are compared, in the boxes,
// so that is shown on page 1's page
if (wholePages) {
pixmap1 = QPixmap::fromImage(onWholePage(image1, *view1,
pair.left, DPI));
composed = onWholePage(composed, *view1, pair.left, DPI);
}
pixmap2 = QPixmap::fromImage(composed);
}
QPixmapCache::insert(keys.first, pixmap1);
QPixmapCache::insert(keys.second, pixmap2);
return qMakePair(pixmap1, pixmap2);
}
void DifferenceRenderer::computeTextHighlights(QPainterPath *highlighted1,
QPainterPath *highlighted2, const PdfPage &page1,
const PdfPage &page2, const int DPI) const
{
const bool ComparingWords = settings.compareMode == CompareWords;
QRectF bigRect1;
QRectF bigRect2;
// Each page's own margins, as the comparison measures them (see
// fingerprintPage()). With page 1's rectangle for both, words
// outside it on a page 2 of a different size were dropped here
// although the comparison had seen them, so the pair was reported as
// differing and then nothing was highlighted.
QRectF rect1;
QRectF rect2;
if (settings.excludeMargins) {
rect1 = settings.pointRectForMargins(page1->pageSize());
rect2 = settings.pointRectForMargins(page2->pageSize());
}
const TextBoxList list1 = getTextBoxes(page1, rect1);
const TextBoxList list2 = getTextBoxes(page2, rect2);
TextItems items1 = ComparingWords ? getWords(list1)
: getCharacters(list1);
TextItems items2 = ComparingWords ? getWords(list2)
: getCharacters(list2);
const int ToleranceY = settings.toleranceY;
if (settings.zoning) {
const int ToleranceR = settings.toleranceR;
const int Columns = settings.columns;
items1.columnZoneYxOrder(page1->pageSize().width(), ToleranceR,
ToleranceY, Columns);
items2.columnZoneYxOrder(page2->pageSize().width(), ToleranceR,
ToleranceY, Columns);
}
if (settings.debug >= DebugShowTexts) {
const bool Yx = settings.debug == DebugShowTextsAndYX;
items1.debug(1, ToleranceY, ComparingWords, Yx);
items2.debug(2, ToleranceY, ComparingWords, Yx);
}
// Texts Poppler could not read are matched by how they look, as the
// comparison matches them (see sameLookingUnmappedTexts()): left to
// the matcher, an Omega that is U+001F from one file and U+2126 from
// the other would be highlighted although nothing about it changed
const QStringList texts1 = items1.texts();
QStringList texts2 = items2.texts();
const auto unmapped = [](const QStringList &texts) {
return std::any_of(texts.cbegin(), texts.cend(),
hasUnmappedCharacter);
};
if (unmapped(texts1) || unmapped(texts2)) {
const QVector<QPair<int, int>> same = sameLookingUnmappedTexts(
texts1, items1.rects(), texts2, items2.rects(), page1,
page2);
for (const QPair<int, int> &pair : same)
texts2[pair.second] = texts1.at(pair.first);
}
SequenceMatcher matcher(texts1, texts2);
RangesPair rangesPair = computeRanges(&matcher);
rangesPair = invertRanges(rangesPair.first, items1.count(),
rangesPair.second, items2.count());
// Highlight in reading order, so that adjacent differences are
// combined; a QSet's order depends on Qt's per-process hash seed
QList<int> indexes1 = rangesPair.first.values();
QList<int> indexes2 = rangesPair.second.values();
std::sort(indexes1.begin(), indexes1.end());
std::sort(indexes2.begin(), indexes2.end());
for (int index : indexes1)
addHighlighting(&bigRect1, highlighted1, items1.at(index).rect,
DPI);
if (!bigRect1.isNull() && !indexes1.isEmpty())
highlighted1->addRect(bigRect1);
for (int index : indexes2)
addHighlighting(&bigRect2, highlighted2, items2.at(index).rect,
DPI);
if (!bigRect2.isNull() && !indexes2.isEmpty())
highlighted2->addRect(bigRect2);
}
void DifferenceRenderer::addHighlighting(QRectF *bigRect,
QPainterPath *highlighted, const QRectF wordOrCharRect,
const int DPI) const
{
const int OVERLAP = settings.overlap;
QRectF rect = wordOrCharRect;
scaleRect(DPI, &rect);
if (settings.combineTextHighlighting &&
rect.adjusted(-OVERLAP, -OVERLAP, OVERLAP, OVERLAP)
.intersects(*bigRect))
*bigRect = bigRect->united(rect);
else {
highlighted->addRect(*bigRect);
*bigRect = rect;
}
}
// The square of image at rect, with anything beyond the image white:
// past the edge of a page there is blank paper, while QImage::copy()
// pads with zeroes, which differ from white. Padding with those would
// turn a page larger than the other into one difference covering
// everything outside it, instead of showing what is actually on it.
static QImage squareAt(const QImage &image, const QRect &rect)
{
if (QRect(QPoint(0, 0), image.size()).contains(rect))
return image.copy(rect);
QImage square(rect.size(), image.format());
square.fill(Qt::white);
QPainter painter(&square);
painter.drawImage(QPoint(0, 0), image, rect);
painter.end();
return square;
}
void DifferenceRenderer::computeVisualHighlights(
QPainterPath *highlighted1, QPainterPath *highlighted2,
const QImage &plainImage1, const QImage &plainImage2) const
{
if (plainImage1.isNull() || plainImage2.isNull())
return;
const int SQUARE_SIZE = settings.squareSize;
// Over both pages, not just page 1: when they are different sizes, a
// difference outside page 1 is one the comparison has seen, and
// QImage::copy() pads whatever lies outside an image, so a square
// present on one page only comes out as a difference. Excluded
// margins are measured on each page, as the comparison measures them
// (see renderForComparison()).
QRect box = QRect(QPoint(0, 0), plainImage1.size())
.united(QRect(QPoint(0, 0), plainImage2.size()));
if (settings.excludeMargins)
box = box.intersected(
settings.pixelRectForMargins(plainImage1.size()).united(
settings.pixelRectForMargins(plainImage2.size())));
QRect target;
// The squares run from the box's own corner, and the last one in
// each direction is cut to it, so that they cover it exactly: on a
// grid from the image's origin, a box edge that did not fall on the
// grid left a band of up to SQUARE_SIZE-1 pixels unexamined, inside
// the area the comparison looks at.
for (int x = box.left(); x <= box.right(); x += SQUARE_SIZE) {
for (int y = box.top(); y <= box.bottom(); y += SQUARE_SIZE) {
const QRect rect = QRect(x, y, SQUARE_SIZE, SQUARE_SIZE)
.intersected(box);
QImage temp1 = squareAt(plainImage1, rect);
QImage temp2 = squareAt(plainImage2, rect);
if (temp1 != temp2) {
if (rect.adjusted(-1, -1, 1, 1).intersects(target))
target = target.united(rect);
else {
highlighted1->addRect(target);
highlighted2->addRect(target);
target = rect;
}
}
}
}
if (!target.isNull()) {
highlighted1->addRect(target);
highlighted2->addRect(target);
}
}
void DifferenceRenderer::paintOnImage(const QPainterPath &path,
QImage *image) const
{
const QPen &pen = settings.pen;
QPen pen_(pen);
QBrush brush_(settings.brush);
QColor color = pen.color();
color.setAlphaF(settings.opacity / 100.0);
pen_.setColor(color);
brush_.setColor(color);
QPainter painter(image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(pen_);
painter.setBrush(brush_);
const int SQUARE_SIZE = settings.squareSize;
const double RULE_WIDTH = settings.ruleWidth;
QRectF rect = path.boundingRect();
if (rect.width() < SQUARE_SIZE && rect.height() < SQUARE_SIZE) {
rect.setHeight(SQUARE_SIZE);
rect.setWidth(SQUARE_SIZE);
painter.drawRect(rect);
if (!qFuzzyCompare(RULE_WIDTH, 0.0)) {
painter.setPen(QPen(pen.color()));
painter.drawRect(0, rect.y(), RULE_WIDTH, rect.height());
}
}
else {
QPainterPath path_(path);
path_.setFillRule(Qt::WindingFill);
painter.drawPath(path_);
if (!qFuzzyCompare(RULE_WIDTH, 0.0)) {
painter.setPen(QPen(pen.color()));
QList<QPolygonF> polygons = path_.toFillPolygons();
for (const QPolygonF &polygon : polygons) {
const QRectF rect = polygon.boundingRect();
painter.drawRect(0, rect.y(), RULE_WIDTH, rect.height());
}
}
}
painter.end();
}
// box, the rendering of a page compared inside a page box, on the page it
// is on: the rest of the page is dimmed, and the box outlined
QImage DifferenceRenderer::onWholePage(const QImage &box,
const BoxedView &view, const int page, const int DPI) const
{
const PdfPage displayed = view.display->page(page);
if (!displayed || page >= view.boxRects.count())
return box;
QImage image = displayed->renderToImage(DPI, DPI);
if (image.isNull())
return box;
QRectF rect = view.boxRects.at(page);
scaleRect(DPI, &rect);
const QRect target(rect.topLeft().toPoint(), box.size());
QPainter painter(&image);
QPainterPath outside;
outside.addRect(QRectF(image.rect()));
if (!rect.isEmpty()) {
QPainterPath inside;
inside.addRect(QRectF(target));
outside = outside.subtracted(inside);
}
painter.fillPath(outside, QColor(255, 255, 255, 160));
if (!rect.isEmpty()) {
painter.drawImage(target.topLeft(), box);
painter.setPen(QPen(QColor(0, 112, 192), qMax(1, DPI / 72)));
painter.setBrush(Qt::NoBrush);
painter.drawRect(QRectF(target).adjusted(-0.5, -0.5, 0.5, 0.5));
}
painter.end();
return image;
}
bool DifferenceRenderer::paintPair(QPainter *painter,
const PdfDocument &pdf1, const QString &filename1,
const PdfDocument &pdf2, const QString &filename2,
const PagePair &pair, const QString &header,
const SavePages savePages, const QRectF &rect,
const QRectF &leftRect, const QRectF &rightRect,
const BoxedView *view1, const BoxedView *view2) const
{
if (pair.left < 0 || pair.right < 0)
return false;
PdfPage page1 = pdf1->page(pair.left);
if (!page1)
return false;
PdfPage page2 = pdf2->page(pair.right);
if (!page2)
return false;
const QPair<QPixmap, QPixmap> images = pixmaps(page1, filename1,
page2, filename2, pair, view1, view2);
if (!header.isEmpty())
painter->drawText(rect, header, QTextOption(Qt::AlignHCenter|
Qt::AlignTop));
if (savePages == SaveBothPages) {
QRectF rect = resizeRect(leftRect, images.first.size());
painter->drawPixmap(rect.toAlignedRect(), images.first);
rect = resizeRect(rightRect, images.second.size());
painter->drawPixmap(rect.toAlignedRect(), images.second);
painter->drawRect(rightRect.adjusted(2.5, 2.5, 2.5, 2.5));
} else if (savePages == SaveLeftPages) {
QRectF rect = resizeRect(leftRect, images.first.size());
painter->drawPixmap(rect.toAlignedRect(), images.first);
} else { // (savePages == SaveRightPages)
QRectF rect = resizeRect(leftRect, images.second.size());
painter->drawPixmap(rect.toAlignedRect(), images.second);
}
painter->drawRect(leftRect.adjusted(2.5, 2.5, 2.5, 2.5));
return true;
}
bool DifferenceRenderer::saveAsPdf(const QString &outputFile,
const PdfDocument &pdf1, const QString &filename1,
const PdfDocument &pdf2, const QString &filename2,
const QList<PagePair> &pairs, const QString &header,
const SavePages savePages, const BoxedView *view1,
const BoxedView *view2) const
{
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName(outputFile);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setColorMode(QPrinter::Color);
printer.setCreator(AboutForm::ProgramName);
printer.setPageOrientation(savePages == SaveBothPages
? QPageLayout::Landscape : QPageLayout::Portrait);
QPainter painter(&printer);
// The output file could not be opened. Going on would render every
// pair into a painter that is not active, which costs the whole
// comparison's rendering and fills the terminal with Qt's warnings
// before the return value says it failed.
if (!painter.isActive())
return false;
painter.setRenderHints(QPainter::Antialiasing|
QPainter::TextAntialiasing|QPainter::SmoothPixmapTransform);
painter.setFont(QFont("Helvetica", 11));
painter.setPen(Qt::darkCyan);
const QRect rect(0, 0, painter.viewport().width(),
painter.fontMetrics().height());
const int y = painter.fontMetrics().lineSpacing();
const int height = painter.viewport().height() - y;
const int gap = 30;
int width = (painter.viewport().width() / 2) - gap;
if (savePages != SaveBothPages)
width = painter.viewport().width();
const QRect leftRect(0, y, width, height);
const QRect rightRect(width + gap, y, width, height);
bool ok = true;
for (int i = 0; i < pairs.count(); ++i) {
if (!paintPair(&painter, pdf1, filename1, pdf2, filename2,
pairs.at(i), header, savePages, rect, leftRect,
rightRect, view1, view2)) {
ok = false;
continue;
}
if (i + 1 < pairs.count())
printer.newPage();
}
painter.end();
return ok && printer.printerState() != QPrinter::Error;
}
static PdfDocument preparePdf(PdfDocument pdf, bool *locked)
{
*locked = false;
if (pdf && pdf->isLocked()) {
*locked = true;
pdf.reset();
}
if (pdf) {
// Compare and highlight pages as they are displayed: without
// antialiasing, some visible differences (such as text printed
// twice in the same place, which looks bolder) render identically.
pdf->setRenderHint(Poppler::Document::Antialiasing);
pdf->setRenderHint(Poppler::Document::TextAntialiasing);
}
return pdf;
}
PdfDocument loadPdf(const QString &filename, bool *locked)
{
return preparePdf(Poppler::Document::load(filename), locked);
}
PdfDocument loadPdfData(const QByteArray &data, bool *locked)
{
return preparePdf(Poppler::Document::loadFromData(data), locked);
}
QString BoxedFile::keyFor(const QString &filename, const PageBox box,
const BoxClip clip)
{
return QString("%1\n%2/%3").arg(documentKey(filename))
.arg(pageBoxName(box), boxClipName(clip));
}
bool BoxedFile::prepare(const QString &filename, const int pageCount,
const PageBox box, const BoxClip clip,
QString *error)
{
clear();
if (!m_document.load(filename, error))
return false;
// Pages are numbered the same in both, so they must agree on how many
// there are
if (m_document.pageCount() != pageCount) {
*error = tr("qpdf finds %1 pages and Poppler %2")
.arg(m_document.pageCount()).arg(pageCount);
return false;
}
m_regions = m_document.regions(box, clip);
m_data = m_document.rewritten(m_regions, error);
if (m_data.isEmpty())
return false;
if (!load()) {
*error = tr("the rewritten copy cannot be read");
m_data.clear();
return false;
}
m_key = keyFor(filename, box, clip);
return true;
}
bool BoxedFile::isPreparedFor(const QString &filename, const PageBox box,
const BoxClip clip) const
{
return !m_key.isEmpty() && m_key == keyFor(filename, box, clip);
}
void BoxedFile::clear()
{
m_key.clear();
m_document = BoxedDocument();
m_regions.clear();
m_data.clear();
m_displayData.clear();
m_boxRects.clear();
}
PdfDocument BoxedFile::load() const
{
bool locked = false;
return loadPdfData(m_data, &locked);
}
bool BoxedFile::view(BoxedView *view)
{
if (m_data.isEmpty())
return false;
if (m_displayData.isEmpty()) {
QVector<BoxRegion> areas;
QVector<QRectF> boxRects;
for (int page = 0; page < m_document.pageCount(); ++page) {
const BoxDisplay display = boxDisplay(m_document.boxes(page),
m_regions.at(page));
BoxRegion area;
area.rect = display.area;
areas.append(area);
boxRects.append(display.boxRect);
}
QString error;
m_displayData = m_document.rewritten(areas, &error);
m_boxRects = boxRects;
}
bool locked = false;
view->display = loadPdfData(m_displayData, &locked);
view->boxRects = m_boxRects;
return view->display != nullptr;
}