Skip to content

Commit

Permalink
Set classic locale for PDF export link attributes (#3551)
Browse files Browse the repository at this point in the history
* Set classic locale for PDF export link attributes

* Add success test for cairo surface setup in XojCairoPdfExport

* Display error message when pdf export fails

* Force C locale LC_NUMERIC to "C"
  • Loading branch information
bhennion committed Nov 15, 2021
1 parent d88fb37 commit e2a1f97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/control/XournalMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ void initLocalisation() {
"xournalpp with msvc",
e.what());
}
/**
* Force numbers to be printed out and parsed by C libraries (cairo) in the "classic" locale.
* This avoids issue with tags when exporting to PDF, see #3551
*/
setlocale(LC_NUMERIC, "C");

std::cout.imbue(std::locale());
}

Expand Down
3 changes: 1 addition & 2 deletions src/control/jobs/PdfExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ void PdfExportJob::run() {
doc->unlock();

if (!pdfe->createPdf(this->filepath, false)) {
this->errorMsg = pdfe->getLastError();
if (control->getWindow()) {
callAfterRun();
} else {
this->errorMsg = pdfe->getLastError();
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/pdf/base/XojCairoPdfExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ auto XojCairoPdfExport::startPdf(const fs::path& file) -> bool {
this->populatePdfOutline(tocModel);
#endif

return true;
return cairo_surface_status(this->surface) == CAIRO_STATUS_SUCCESS;
}

#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 16, 0)
Expand Down Expand Up @@ -68,6 +68,7 @@ void XojCairoPdfExport::populatePdfOutline(GtkTreeModel* tocModel) {
auto pageDest = pdfBgPage == npos ? npos : doc->findPdfPage(pdfBgPage); // Destination in document
if (pageDest != npos) {
std::ostringstream linkAttrBuf;
linkAttrBuf.imbue(std::locale::classic());
linkAttrBuf << "page=" << pageDest + 1;
if (dest->shouldChangeLeft() && dest->shouldChangeTop()) {
linkAttrBuf << " pos=[" << dest->getLeft() << " " << dest->getTop() << "]";
Expand Down Expand Up @@ -150,6 +151,9 @@ auto XojCairoPdfExport::createPdf(fs::path const& file, PageRangeVector& range,
}

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;
}

Expand Down Expand Up @@ -192,6 +196,9 @@ auto XojCairoPdfExport::createPdf(fs::path const& file, bool progressiveMode) ->
}

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;
}

Expand Down

0 comments on commit e2a1f97

Please sign in to comment.