forked from greenantdotcom/diffpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.main.cpp
220 lines (179 loc) · 7.15 KB
/
cli.main.cpp
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
/*
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 "mainwindow.hpp"
#include <QApplication>
#include <QIcon>
#include <QLibraryInfo>
#include <QLocale>
#include <QSettings>
#include <QTextCodec>
#include <QTextStream>
#include <QTranslator>
#include <QPrinter>
#include <QPainter>
#include <QLocale>
#include <QLineEdit>
#include "aboutform.hpp"
#include "generic.hpp"
#include "helpform.hpp"
#include "label.hpp"
#include "lineedit.hpp"
#include "optionsform.hpp"
#include "mainwindow.hpp"
#include "sequence_matcher.hpp"
#include "textitem.hpp"
#ifdef DEBUG
#include <QtDebug>
#endif
#include <QApplication>
#include <QBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QDir>
#include <QDockWidget>
#include <QEvent>
#include <QFileDialog>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPainter>
#include <QPixmapCache>
#include <QPlainTextEdit>
#include <QPrinter>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollArea>
#include <QScrollBar>
#include <QSettings>
#include <QSpinBox>
#include <QSplitter>
#include <QTextStream>
typedef QSharedPointer<Poppler::Document> PdfDocument;
void saveAsPdf(const int start, const int end,
const PdfDocument &pdf1, const PdfDocument &pdf2,
const QString &header, const QString &saveFilename);
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QStringList args = app.arguments().mid(1);
// Comparison mode
/* TODO: HACKED TO ONLY DO WHAT WE WANT… */
// InitialComparisonMode comparisonMode = static_cast<InitialComparisonMode>(settings.value("InitialComparisonMode", CompareWords).toInt());
InitialComparisonMode comparisonMode = CompareAppearance;
// Language
QString language = QLocale::system().name();
QString filename1;
QString filename2;
QString outputFilename;
QString *header = new QString( "" );
bool optionsOK = true;
Debug debug = DebugOff;
const QString LanguageOption = "--language=";
QTextStream out(stdout);
foreach (const QString arg, args) {
if (optionsOK && (arg == "--appearance" || arg == "-a"))
comparisonMode = CompareAppearance;
else if (optionsOK && (arg == "--characters" || arg == "-c"))
comparisonMode = CompareCharacters;
else if (optionsOK && (arg == "--words" || arg == "-w"))
comparisonMode = CompareWords;
else if (optionsOK && arg.startsWith(LanguageOption))
language = arg.mid(LanguageOption.length());
else if (optionsOK && (arg == "--help" || arg == "-h")) {
out << "usage: diffpdf [options] file1.pdf file2.pdf output.pdf HEADER_STRING\n\n"
"A GUI program that compares two PDF files and stores "
"their differences into a third file.\n"
"options:\n"
"--help -h show this usage text and terminate "
"(run the program without this option and press F1 for "
"online help)\n"
"--appearance -a set the initial comparison mode to "
"Appearance\n"
"--characters -c set the initial comparison mode to "
"Characters\n"
"--words -w set the initial comparison mode to "
"Words\n"
"--language=xx set the program to use the given "
"translation language, e.g., en for English, cz for "
"Czech; English will be used if there is no translation "
"available\n"
"--debug=2 write the text fed to the sequence "
"matcher into temporary files (e.g., /tmp/page1.txt "
"etc.)\n"
"--debug=3 as --debug=3 but also includes "
"coordinates in y, x order\n"
"\nRun the program without the --help option and click "
"About to see copyright and license details\n"
;
return 0;
}
else if (optionsOK && (arg == "--debug" || arg == "--debug=1" ||
arg == "--debug1"))
; // basic debug mode currently does nothing (did show zones)
else if (optionsOK && (arg == "--debug=2" || arg == "--debug2"))
debug = DebugShowTexts;
else if (optionsOK && (arg == "--debug=3" || arg == "--debug3"))
debug = DebugShowTextsAndYX;
else if (optionsOK && arg == "--")
optionsOK = false;
else if (filename1.isEmpty() && arg.toLower().endsWith(".pdf"))
filename1 = arg;
else if (filename2.isEmpty() && arg.toLower().endsWith(".pdf"))
filename2 = arg;
else if (outputFilename.isEmpty() && arg.toLower().endsWith(".pdf"))
outputFilename = arg;
else if (!arg.trimmed().isEmpty())
header = new QString( arg.trimmed() );
else
out << "unrecognized argument '" << arg << "'\n";
}
if(filename1.isEmpty() || filename2.isEmpty() || outputFilename.isEmpty()){
out << "You must provide both the before/after files, and a path to the output file. Exiting…\n";
return 1;
}
PdfDocument pdf1(Poppler::Document::load(filename1));
if (!pdf1){
out << "Could not load the first file<" << filename1 << ">. Exiting…\n";
return 20;
}
PdfDocument pdf2(Poppler::Document::load(filename2));
if (!pdf2){
out << "Could not load the second file<" << filename2 << ">. Exiting…\n";
return 21;
}
// Ok, so we've made it this far - let's make sure we have a legitimate header for the resultant file
if(header->trimmed().isEmpty()){
QString newHeader = QString( "PDF Comparison - %1 vs. %2" ).arg( filename1, filename2 );
header = new QString( newHeader );
}
// out << *header << "\n";
// return 10;
// out << pdf1->numPages() << " // " << pdf2->numPages() << "\n";
int minIndex = 0;
int maxIndex = std::max(pdf1->numPages(),pdf2->numPages());
// // return 10;
// out << "WOOT" << "\n";;
// out << filename1 << " / " << filename2 << " / " << outputFilename << "\n";;
/*
*
*/
MainWindow *window = new MainWindow( DebugShowTexts, comparisonMode, filename1, filename2, "en" );
window->saveFilename = outputFilename;
window->savePages = SaveBothPages;
window->setFile1( filename1 );
window->setFile2( filename2 );
out << window->filename1LineEdit->text() << " / " << window->filename2LineEdit->text() << "\n";
window->compare();
window->saveAsPdf( minIndex, maxIndex, pdf1, pdf2, *header );
return 2;
}