-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocassembler.cpp
More file actions
310 lines (267 loc) · 9.63 KB
/
docassembler.cpp
File metadata and controls
310 lines (267 loc) · 9.63 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
/***********************************************
gSafe document editor
Author:
(C) 2026 Deák Péter (hyper80@gmail.com)
*/
#include <docassembler.h>
DocAssembler::DocAssembler(QString documentSource)
{
rawDocumentSource = documentSource;
textProcessor = new HTextProcessor();
workingDirectory = QDir::currentPath() + QDir::separator() + "work";
sourceDocDirectory = QDir::currentPath() + QDir::separator() + "documents";
enable_render_warnings = false;
}
DocAssembler::~DocAssembler()
{
delete textProcessor;
}
void DocAssembler::generatePdfDocument(QString outputFile)
{
minumimGenPageCount = 0;
if(QFile(outputFile).exists())
if(!QFile::remove(outputFile))
{
throw GSafeException(tr("Error, Cannot remove existing output file: %1").arg(outputFile));
}
preprocessDocument();
generateFilenames();
generateBasePdf();
finishingPdf();
deleteWorkfileIfExists(outputFile);
if(!QFile::copy(filenames["result"],outputFile))
{
throw GSafeException(tr("Error, Cannot copy result file to output file: %1").arg(outputFile));
}
deleteWorkfileIfExists(filenames["base"]);
deleteWorkfileIfExists(filenames["underlay"]);
deleteWorkfileIfExists(filenames["result"]);
}
int DocAssembler::getPageCountOfPdf(QString filename)
{
filenames["qpdf"] = QDir::currentPath() + QDir::separator() + "qpdf" + QDir::separator() + "qpdf.exe";
if(!QFile::exists(filenames["qpdf"]))
{
sdebug(QString("Error, qpdf.exe not found..."));
throw GSafeException(tr("Error, the qpdf.exe not found in qpdf directory.\nPlease download the qpdf binary from https://github.com/qpdf/qpdf"));
return -1;
}
QProcess qpdf;
QStringList arguments;
arguments << "--show-npages" << filename;
qpdf.setReadChannel(QProcess::StandardOutput);
qpdf.start(filenames["qpdf"],arguments);
if(!qpdf.waitForFinished(-1))
{
throw GSafeException(tr("Error, Cannot run qpdf --show-npages command to the underlay pdf!"));
return -1;
}
QString output = qpdf.readAllStandardOutput().trimmed();
bool ok;
int pageCount = output.toInt(&ok);
if (!ok) {
throw GSafeException(tr("Error, Cannot parse output of qpdf --show-npages command on the underlay pdf!"));
return -1;
}
return pageCount;
if(qpdf.exitStatus() != QProcess::NormalExit || qpdf.exitCode() != 0)
{
throw GSafeException(tr("Error, The qpdf --show-npages command to failed to run on the underlay pdf!"));
return -1;
}
return pageCount;
}
int DocAssembler::deleteWorkfileIfExists(QString filename)
{
if(QFile::exists(filename))
{
if(!QFile::remove(filename))
{
sdebug(QString("Error deleting file: %1").arg(filename));
return 1;
}
}
return 0;
}
int DocAssembler::preprocessDocument()
{
preprocessedDoc = "";
read_annotations.clear();
preprocessedDoc = textProcessor->processDoc(rawDocumentSource);
read_annotations = textProcessor->annotations();
return 0;
}
int DocAssembler::generateFilenames()
{
QDir currdir = QDir::current();
if(!currdir.mkpath(workingDirectory))
{
throw GSafeException(tr("Error, Cannot create working directory: %1").arg(workingDirectory));
}
if(!QDir(workingDirectory).exists())
{
throw GSafeException(tr("Error, Cannot create working directory: %1").arg(workingDirectory));
}
filenames["base"] = workingDirectory + QDir::separator() + "gen.pdf";
filenames["result"] = filenames["base"];
if(read_annotations.contains("UnderlayPdf"))
{
filenames["original_underlay"] = sourceDocDirectory + QDir::separator() + read_annotations["UnderlayPdf"];
if(QFile(filenames["original_underlay"]).exists())
{
filenames["underlay"] = workingDirectory + QDir::separator() + "underlay.pdf";
filenames["result"] = workingDirectory + QDir::separator() + "result.pdf";
if(QFile(filenames["underlay"]).exists())
if(!QFile::remove(filenames["underlay"]))
throw GSafeException(tr("Error, Cannot remove previous work file: %1").arg(filenames["underlay"]));
if(!QFile::copy(filenames["original_underlay"],filenames["underlay"]))
throw GSafeException(tr("Error, Cannot copy original underlay file to working directory: %1").arg(filenames["underlay"]));
minumimGenPageCount = getPageCountOfPdf(filenames["underlay"]);
return 0;
}
else
{
throw GSafeException(tr("Error, The underlay pdf file specified in the document annotation not found: %1").arg(filenames["original_underlay"]));
}
}
return 0;
}
int DocAssembler::generateBasePdf()
{
int dpi = 200;
if(QFile(filenames["base"]).exists())
if(!QFile::remove(filenames["base"]))
throw GSafeException(tr("Error, Cannot remove previous work file: %1").arg(filenames["base"]));
QPdfWriter pw(filenames["base"]);
pw.setResolution(dpi);
pw.setPageSize(QPageSize(QPageSize::A4)); /*8.26 x 11.69 -> *200 -> 1652 x 2338*/
pw.setPageOrientation(QPageLayout::Portrait);
/*
QMap<QString,QString>::Iterator i;
for(i = attachmentFiles.begin() ; i != attachmentFiles.end() ; ++i )
pw.addFileAttachment(i.key(),i.value().toUtf8());
*/
QPainter pp(&pw);
pp.setWindow(0,0,1652,2338); // PageSite A4 on 200 dpi (Set elsewhere...)
HPageTileRenderer renderer(&pp);
renderer.resolutionDpi = dpi;
if(enable_render_warnings)
renderer.setUnknownCommandWarning(true);
connect(&renderer, &HPageTileRenderer::startNewPage,
this,[&pw]() { pw.newPage(); });
if(minumimGenPageCount > 0)
preprocessedDoc += QString("\nnpuc#%1").arg(minumimGenPageCount);
renderer.renderFromInstructions(preprocessedDoc);
lastRenderStoredPositions = renderer.storedPositions();
pp.end();
return 0;
}
int DocAssembler::finishingPdf()
{
if(!filenames.contains("underlay") || filenames["underlay"].isEmpty())
return 0;
//It should be checked earlier
if(!filenames.contains("qpdf") || filenames["qpdf"].isEmpty())
return 0;
if(QFile::exists(filenames["result"]))
if(!QFile::remove(filenames["result"]))
throw GSafeException(tr("Error, Cannot remove previous work file: %1").arg(filenames["result"]));
QProcess qpdf;
QStringList arguments;
arguments << "--overlay" << filenames["base"] << "--" << filenames["underlay"] << filenames["result"];
qpdf.start(filenames["qpdf"],arguments);
if(!qpdf.waitForFinished(-1))
{
throw GSafeException(tr("Error, Cannot run qpdf to merge the underlay and base pdf files!"));
return 1;
}
if(qpdf.exitStatus() != QProcess::NormalExit || qpdf.exitCode() != 0)
{
throw GSafeException(tr("Error, Qpdf returned error: %1 %2").arg(qpdf.exitCode()).arg(qpdf.readAllStandardError()));
return 1;
}
return 0;
}
void DocAssembler::addValueMap(QString name,const QMap<QString,QString>& m)
{
textProcessor->addValueMap(name,m);
}
void DocAssembler::addValueList(QString name,const QList<QString>& l)
{
textProcessor->addValueList(name,l);
}
void DocAssembler::addValueMapPtr(QString name,QMap<QString,QString>* m)
{
textProcessor->addValueMapPtr(name,m);
}
void DocAssembler::clearValueMaps()
{
textProcessor->clearValueMaps();
}
QMap<QString,QString> getFilenameTitlePairsFromFolder(QString folder)
{
QMap<QString,QString> result;
QDir dir(folder);
if(!dir.exists())
return result;
QStringList nameFilters;
nameFilters << "*.pot" << "*.POT" << "*.Pot";
QFileInfoList fileList = dir.entryInfoList(nameFilters, QDir::Files | QDir::NoSymLinks);
foreach (const QFileInfo &fileInfo, fileList)
{
QString filename = fileInfo.fileName();
QMap<QString, QString> annotations = getAnnotationValuesFromFile(fileInfo.absoluteFilePath());
QString title = annotations.value("Title", fileInfo.baseName());
result[filename] = title;
}
return result;
}
QMap<QString,QString> getAnnotationValuesFromFile(QString filename)
{
QMap<QString,QString> result;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return result;
QTextStream in(&file);
result = getAnnotationValuesFromText(in.readAll());
file.close();
return result;
}
QMap<QString,QString> getAnnotationValuesFromText(QString documentSource)
{
QMap<QString,QString> result;
QTextStream in(&documentSource);
while (!in.atEnd())
{
QString line = in.readLine().trimmed();
if (line.startsWith("//") && line.contains("@") && line.contains(":"))
{
QString commentsubline = line.mid(2).trimmed();
if(commentsubline.startsWith("@"))
{
QStringList parts = commentsubline.mid(1).split(":");
if(parts.count() == 2)
result[parts[0].trimmed()] = parts[1].trimmed();
}
continue;
}
}
return result;
}
QList<QString> getAnnotationLinesFromText(QString documentSource)
{
QList<QString> result;
QTextStream in(&documentSource);
while (!in.atEnd())
{
QString line = in.readLine().trimmed();
if (line.startsWith("//") && line.contains("@") && line.contains(":"))
{
QString commentsubline = line.mid(2).trimmed();
if(commentsubline.startsWith("@"))
result.push_back(commentsubline.mid(1).trimmed());
continue;
}
}
return result;
}