-
Notifications
You must be signed in to change notification settings - Fork 772
/
XojCairoPdfExport.cpp
227 lines (182 loc) · 7.07 KB
/
XojCairoPdfExport.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
221
222
223
224
225
226
227
#include "XojCairoPdfExport.h"
#include <map>
#include <sstream>
#include <stack>
#include <cairo-pdf.h>
#include <config.h>
#include "util/Util.h"
#include "util/i18n.h"
#include "util/serdesstream.h"
#include "view/DocumentView.h"
#include "filesystem.h"
XojCairoPdfExport::XojCairoPdfExport(Document* doc, ProgressListener* progressListener):
doc(doc), progressListener(progressListener) {}
XojCairoPdfExport::~XojCairoPdfExport() {
if (this->surface != nullptr) {
endPdf();
}
}
/**
* Export without background
*/
void XojCairoPdfExport::setExportBackground(ExportBackgroundType exportBackground) {
this->exportBackground = exportBackground;
}
auto XojCairoPdfExport::startPdf(const fs::path& file) -> bool {
this->surface = cairo_pdf_surface_create(file.u8string().c_str(), 0, 0);
this->cr = cairo_create(surface);
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
cairo_pdf_surface_set_metadata(surface, CAIRO_PDF_METADATA_TITLE, doc->getFilepath().filename().u8string().c_str());
cairo_pdf_surface_set_metadata(surface, CAIRO_PDF_METADATA_CREATOR, PROJECT_STRING);
GtkTreeModel* tocModel = doc->getContentsModel();
this->populatePdfOutline(tocModel);
#endif
return cairo_surface_status(this->surface) == CAIRO_STATUS_SUCCESS;
}
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
void XojCairoPdfExport::populatePdfOutline(GtkTreeModel* tocModel) {
if (tocModel == nullptr)
return;
int idCounter = CAIRO_PDF_OUTLINE_ROOT;
std::stack<std::pair<GtkTreeIter, int>> nodeStack;
GtkTreeIter firstIter = {0};
if (!gtk_tree_model_get_iter_first(tocModel, &firstIter)) {
// Outline is empty, so do nothing.
return;
}
nodeStack.push(std::make_pair(firstIter, idCounter));
while (!nodeStack.empty()) {
auto [iter, parentId] = nodeStack.top();
nodeStack.pop();
const int currentId = ++idCounter;
XojLinkDest* link = nullptr;
gtk_tree_model_get(tocModel, &iter, DOCUMENT_LINKS_COLUMN_LINK, &link, -1);
auto dest = link->dest;
auto pdfBgPage = dest->getPdfPage(); // Link destination in original background PDF
auto pageDest = pdfBgPage == npos ? npos : doc->findPdfPage(pdfBgPage); // Destination in document
if (pageDest != npos) {
auto linkAttrBuf = serdes_stream<std::ostringstream>();
linkAttrBuf << "page=" << pageDest + 1;
if (dest->shouldChangeLeft() && dest->shouldChangeTop()) {
linkAttrBuf << " pos=[" << dest->getLeft() << " " << dest->getTop() << "]";
}
const auto linkAttr = linkAttrBuf.str();
auto outlineFlags = dest->getExpand() ? CAIRO_PDF_OUTLINE_FLAG_OPEN : 0;
cairo_pdf_surface_add_outline(this->surface, parentId, link->dest->getName().data(), linkAttr.data(),
static_cast<cairo_pdf_outline_flags_t>(outlineFlags));
}
g_object_unref(link);
GtkTreeIter childIter;
if (gtk_tree_model_iter_children(tocModel, &childIter, &iter)) {
nodeStack.push(std::make_pair(childIter, currentId));
}
if (gtk_tree_model_iter_next(tocModel, &iter)) {
nodeStack.push(std::make_pair(iter, parentId));
}
}
}
#endif
void XojCairoPdfExport::endPdf() {
cairo_destroy(this->cr);
this->cr = nullptr;
cairo_surface_destroy(this->surface);
this->surface = nullptr;
}
void XojCairoPdfExport::exportPage(size_t page) {
PageRef p = doc->getPage(page);
cairo_pdf_surface_set_size(this->surface, p->getWidth(), p->getHeight());
DocumentView view;
cairo_save(this->cr);
if (p->getBackgroundType().isPdfPage() && (exportBackground >= EXPORT_BACKGROUND_UNRULED)) {
int pgNo = p->getPdfPageNr();
XojPdfPageSPtr popplerPage = doc->getPdfPage(pgNo);
popplerPage->render(cr, true);
}
view.drawPage(p, this->cr, true /* dont render eraseable */, exportBackground == EXPORT_BACKGROUND_NONE,
exportBackground == EXPORT_BACKGROUND_NONE, exportBackground <= EXPORT_BACKGROUND_UNRULED);
// next page
cairo_show_page(this->cr);
cairo_restore(this->cr);
}
// export layers one by one to produce as many PDF pages as there are layers.
void XojCairoPdfExport::exportPageLayers(size_t page) {
PageRef p = doc->getPage(page);
// We keep a copy of the layers initial Visible state
std::map<Layer*, bool> initialVisibility;
for (const auto& layer: *p->getLayers()) {
initialVisibility[layer] = layer->isVisible();
layer->setVisible(false);
}
// We draw as many pages as there are layers. The first page has
// only Layer 1 visible, the last has all layers visible.
for (const auto& layer: *p->getLayers()) {
layer->setVisible(true);
exportPage(page);
}
// We restore the initial visibilities
for (const auto& layer: *p->getLayers()) layer->setVisible(initialVisibility[layer]);
}
auto XojCairoPdfExport::createPdf(fs::path const& file, PageRangeVector& range, bool progressiveMode) -> bool {
if (range.empty()) {
this->lastError = _("No pages to export!");
return false;
}
if (!startPdf(file)) {
this->lastError = _("Failed to initialize PDF Cairo surface");
this->lastError += "\nCairo error: ";
this->lastError += cairo_status_to_string(cairo_surface_status(this->surface));
return false;
}
int count = 0;
for (PageRangeEntry* e: range) { count += e->getLast() - e->getFirst() + 1; }
if (this->progressListener) {
this->progressListener->setMaximumState(count);
}
int c = 0;
for (PageRangeEntry* e: range) {
for (int i = e->getFirst(); i <= e->getLast(); i++) {
if (i < 0 || i >= static_cast<int>(doc->getPageCount())) {
continue;
}
if (progressiveMode) {
exportPageLayers(i);
} else {
exportPage(i);
}
if (this->progressListener) {
this->progressListener->setCurrentState(c++);
}
}
}
endPdf();
return true;
}
auto XojCairoPdfExport::createPdf(fs::path const& file, bool progressiveMode) -> bool {
if (doc->getPageCount() < 1) {
lastError = _("No pages to export!");
return false;
}
if (!startPdf(file)) {
this->lastError = _("Failed to initialize PDF Cairo surface");
this->lastError += "\nCairo error: ";
this->lastError += cairo_status_to_string(cairo_surface_status(this->surface));
return false;
}
int count = doc->getPageCount();
if (this->progressListener) {
this->progressListener->setMaximumState(count);
}
for (int i = 0; i < count; i++) {
if (progressiveMode) {
exportPageLayers(i);
} else {
exportPage(i);
}
if (this->progressListener) {
this->progressListener->setCurrentState(i);
}
}
endPdf();
return true;
}
auto XojCairoPdfExport::getLastError() -> std::string { return lastError; }