refactor background export treatment

Before introducing more export options later on, refactor the current
code: turn the boolean noBackground/withoutBackground/hideBackground
into an enum ExportBackgroundType exportBackground which currently can
have two values EXPORT_BACKGROUND_ALL and EXPORT_BACKGROUND_NONE.
That way we can extend the enum later on without changing call
signatures; also, enum values are hopefully self-explaining.

No change of behaviour with this commit.
upstream-master
Michael J Gruber 5 years ago committed by Roland Lötscher
parent 6febcee29e
commit aef955d844
  1. 26
      src/control/XournalMain.cpp
  2. 10
      src/control/jobs/BaseExportJob.h
  3. 20
      src/control/jobs/CustomExportJob.cpp
  4. 10
      src/control/jobs/ImageExport.cpp
  5. 6
      src/control/jobs/ImageExport.h
  6. 8
      src/pdf/base/XojCairoPdfExport.cpp
  7. 5
      src/pdf/base/XojCairoPdfExport.h
  8. 2
      src/pdf/base/XojPdfExport.cpp
  9. 4
      src/pdf/base/XojPdfExport.h

@ -52,10 +52,10 @@ auto migrateSettings() -> MigrateResult;
void checkForErrorlog();
void checkForEmergencySave(Control* control);
auto exportPdf(const char* input, const char* output, const char* range, bool noBackground, bool progressiveMode)
-> int;
auto exportPdf(const char* input, const char* output, const char* range, ExportBackgroundType exportBackground,
bool progressiveMode) -> int;
auto exportImg(const char* input, const char* output, const char* range, int pngDpi, int pngWidth, int pngHeight,
bool noBackground) -> int;
ExportBackgroundType exportBackground) -> int;
void initResourcePath(GladeSearchpath* gladePath, const gchar* relativePathAndFile, bool failIfNotFound = true);
@ -235,14 +235,14 @@ void checkForEmergencySave(Control* control) {
* @param pngDpi Set dpi for Png files. Non positive values are ignored
* @param pngWidth Set the width for Png files. Non positive values are ignored
* @param pngHeight Set the height for Png files. Non positive values are ignored
* @param noBackground If true, the exported image file has transparent background
* @param exportBackground If EXPORT_BACKGROUND_NONE, the exported image file has transparent background
*
* The priority is: pngDpi overwrites pngWidth overwrites pngHeight
*
* @return 0 on success, -2 on failure opening the input file, -3 on export failure
*/
auto exportImg(const char* input, const char* output, const char* range, int pngDpi, int pngWidth, int pngHeight,
bool noBackground) -> int {
ExportBackgroundType exportBackground) -> int {
LoadHandler loader;
Document* doc = loader.loadDocument(input);
@ -267,7 +267,7 @@ auto exportImg(const char* input, const char* output, const char* range, int png
DummyProgressListener progress;
ImageExport imgExport(doc, path, format, noBackground, exportRange);
ImageExport imgExport(doc, path, format, exportBackground, exportRange);
if (format == EXPORT_GRAPHICS_PNG) {
if (pngDpi > 0) {
@ -301,14 +301,14 @@ auto exportImg(const char* input, const char* output, const char* range, int png
* @param input Path to the input file
* @param output Path to the output file
* @param range Page range to be parsed. If range=nullptr, exports the whole file
* @param noBackground If true, the exported pdf file has white background
* @param exportBackground If EXPORT_BACKGROUND_NONE, the exported pdf file has white background
* @param progressiveMode If true, then for each xournalpp page, instead of rendering one PDF page, the page layers are
* rendered one by one to produce as many pages as there are layers.
*
* @return 0 on success, -2 on failure opening the input file, -3 on export failure
*/
auto exportPdf(const char* input, const char* output, const char* range, bool noBackground, bool progressiveMode)
-> int {
auto exportPdf(const char* input, const char* output, const char* range, ExportBackgroundType exportBackground,
bool progressiveMode) -> int {
LoadHandler loader;
Document* doc = loader.loadDocument(input);
@ -319,7 +319,7 @@ auto exportPdf(const char* input, const char* output, const char* range, bool no
GFile* file = g_file_new_for_commandline_arg(output);
XojPdfExport* pdfe = XojPdfExportFactory::createExport(doc, nullptr);
pdfe->setNoBackgroundExport(noBackground);
pdfe->setExportBackground(exportBackground);
char* cpath = g_file_get_path(file);
string path = cpath;
g_free(cpath);
@ -584,11 +584,13 @@ auto on_handle_local_options(GApplication*, GVariantDict*, XMPtr app_data) -> gi
if (app_data->pdfFilename && app_data->optFilename && *app_data->optFilename) {
return exportPdf(*app_data->optFilename, app_data->pdfFilename, app_data->exportRange,
app_data->exportNoBackground, app_data->progressiveMode);
app_data->exportNoBackground ? EXPORT_BACKGROUND_NONE : EXPORT_BACKGROUND_ALL,
app_data->progressiveMode);
}
if (app_data->imgFilename && app_data->optFilename && *app_data->optFilename) {
return exportImg(*app_data->optFilename, app_data->imgFilename, app_data->exportRange, app_data->exportPngDpi,
app_data->exportPngWidth, app_data->exportPngHeight, app_data->exportNoBackground);
app_data->exportPngWidth, app_data->exportPngHeight,
app_data->exportNoBackground ? EXPORT_BACKGROUND_NONE : EXPORT_BACKGROUND_ALL);
}
return -1;
}

@ -19,6 +19,12 @@
#include "XournalType.h"
#include "filesystem.h"
/**
* @brief List of types for the export of background components.
* Keep the order so that one can check for intermediate types using comparsion.
*/
enum ExportBackgroundType { EXPORT_BACKGROUND_NONE, EXPORT_BACKGROUND_ALL };
class Control;
class BaseExportJob: public BlockingJob {
@ -56,8 +62,8 @@ protected:
class ExportType {
public:
string extension;
bool withoutBackground;
ExportBackgroundType exportBackground;
ExportType(string ext, bool hideBg): extension(ext), withoutBackground(hideBg) {}
ExportType(string ext, ExportBackgroundType exportBg): extension(ext), exportBackground(exportBg) {}
};
};

@ -18,13 +18,13 @@
CustomExportJob::CustomExportJob(Control* control): BaseExportJob(control, _("Custom Export")) {
// Supported filters
filters[_("PDF files")] = new ExportType(".pdf", false);
filters[_("PDF with plain background")] = new ExportType(".pdf", true);
filters[_("PNG graphics")] = new ExportType(".png", false);
filters[_("PNG with transparent background")] = new ExportType(".png", true);
filters[_("SVG graphics")] = new ExportType(".svg", false);
filters[_("SVG with transparent background")] = new ExportType(".svg", true);
filters[_("Xournal (Compatibility)")] = new ExportType(".xoj", false);
filters[_("PDF files")] = new ExportType(".pdf", EXPORT_BACKGROUND_ALL);
filters[_("PDF with plain background")] = new ExportType(".pdf", EXPORT_BACKGROUND_NONE);
filters[_("PNG graphics")] = new ExportType(".png", EXPORT_BACKGROUND_ALL);
filters[_("PNG with transparent background")] = new ExportType(".png", EXPORT_BACKGROUND_NONE);
filters[_("SVG graphics")] = new ExportType(".svg", EXPORT_BACKGROUND_ALL);
filters[_("SVG with transparent background")] = new ExportType(".svg", EXPORT_BACKGROUND_NONE);
filters[_("Xournal (Compatibility)")] = new ExportType(".xoj", EXPORT_BACKGROUND_ALL);
}
CustomExportJob::~CustomExportJob() {
@ -109,8 +109,8 @@ auto CustomExportJob::showFilechooser() -> bool {
* Create one Graphics file per page
*/
void CustomExportJob::exportGraphics() {
bool hideBackground = filters.at(this->chosenFilterName)->withoutBackground;
ImageExport imgExport(control->getDocument(), filepath, format, hideBackground, exportRange);
ExportBackgroundType exportBackground = filters.at(this->chosenFilterName)->exportBackground;
ImageExport imgExport(control->getDocument(), filepath, format, exportBackground, exportRange);
if (format == EXPORT_GRAPHICS_PNG) {
imgExport.setQualityParameter(pngQualityParameter);
}
@ -141,7 +141,7 @@ void CustomExportJob::run() {
XojPdfExport* pdfe = XojPdfExportFactory::createExport(doc, control);
pdfe->setNoBackgroundExport(filters[this->chosenFilterName]->withoutBackground);
pdfe->setExportBackground(filters[this->chosenFilterName]->exportBackground);
if (!pdfe->createPdf(this->filepath, exportRange, progressiveMode)) {
this->errorMsg = pdfe->getLastError();

@ -13,9 +13,9 @@
#include "i18n.h"
ImageExport::ImageExport(Document* doc, fs::path file, ExportGraphicsFormat format, bool hideBackground,
PageRangeVector& exportRange):
doc(doc), file(std::move(file)), format(format), hideBackground(hideBackground), exportRange(exportRange) {}
ImageExport::ImageExport(Document* doc, fs::path file, ExportGraphicsFormat format,
ExportBackgroundType exportBackground, PageRangeVector& exportRange):
doc(doc), file(std::move(file)), format(format), exportBackground(exportBackground), exportRange(exportRange) {}
ImageExport::~ImageExport() = default;
@ -144,14 +144,14 @@ void ImageExport::exportImagePage(int pageId, int id, double zoomRatio, ExportGr
return;
}
if (page->getBackgroundType().isPdfPage() && !hideBackground) {
if (page->getBackgroundType().isPdfPage() && (exportBackground == EXPORT_BACKGROUND_ALL)) {
int pgNo = page->getPdfPageNr();
XojPdfPageSPtr popplerPage = doc->getPdfPage(pgNo);
PdfView::drawPage(nullptr, popplerPage, cr, zoomRatio, page->getWidth(), page->getHeight());
}
view.drawPage(page, this->cr, true, hideBackground);
view.drawPage(page, this->cr, true, exportBackground == EXPORT_BACKGROUND_NONE);
if (!freeSurface(id)) {
// could not create this file...

@ -18,6 +18,7 @@
#include "view/DocumentView.h"
#include "BaseExportJob.h"
#include "PageRange.h"
#include "XournalType.h"
#include "filesystem.h"
@ -27,7 +28,6 @@ class ProgressListener;
enum ExportGraphicsFormat { EXPORT_GRAPHICS_UNDEFINED, EXPORT_GRAPHICS_PDF, EXPORT_GRAPHICS_PNG, EXPORT_GRAPHICS_SVG };
/**
* @brief List of available criterion for determining a PNG export quality.
* The order must agree with the corresponding listAvailableCriterion in ui/exportSettings.glade
@ -68,7 +68,7 @@ private:
*/
class ImageExport {
public:
ImageExport(Document* doc, fs::path file, ExportGraphicsFormat format, bool hideBackground,
ImageExport(Document* doc, fs::path file, ExportGraphicsFormat format, ExportBackgroundType exportBackground,
PageRangeVector& exportRange);
virtual ~ImageExport();
@ -155,7 +155,7 @@ public:
/**
* Do not export the Background
*/
bool hideBackground = false;
ExportBackgroundType exportBackground = EXPORT_BACKGROUND_ALL;
/**
* The range to export

@ -24,8 +24,8 @@ XojCairoPdfExport::~XojCairoPdfExport() {
/**
* Export without background
*/
void XojCairoPdfExport::setNoBackgroundExport(bool noBackgroundExport) {
this->noBackgroundExport = noBackgroundExport;
void XojCairoPdfExport::setExportBackground(ExportBackgroundType exportBackground) {
this->exportBackground = exportBackground;
}
auto XojCairoPdfExport::startPdf(const fs::path& file) -> bool {
@ -106,14 +106,14 @@ void XojCairoPdfExport::exportPage(size_t page) {
DocumentView view;
cairo_save(this->cr);
if (p->getBackgroundType().isPdfPage() && !noBackgroundExport) {
if (p->getBackgroundType().isPdfPage() && (exportBackground == EXPORT_BACKGROUND_ALL)) {
int pgNo = p->getPdfPageNr();
XojPdfPageSPtr popplerPage = doc->getPdfPage(pgNo);
popplerPage->render(cr, true);
}
view.drawPage(p, this->cr, true /* dont render eraseable */, noBackgroundExport);
view.drawPage(p, this->cr, true /* dont render eraseable */, exportBackground == EXPORT_BACKGROUND_NONE);
// next page
cairo_show_page(this->cr);

@ -11,6 +11,7 @@
#pragma once
#include "control/jobs/BaseExportJob.h"
#include "control/jobs/ProgressListener.h"
#include "model/Document.h"
@ -30,7 +31,7 @@ public:
/**
* Export without background
*/
virtual void setNoBackgroundExport(bool noBackgroundExport);
virtual void setExportBackground(ExportBackgroundType exportBackground);
private:
bool startPdf(const fs::path& file);
@ -59,7 +60,7 @@ private:
cairo_surface_t* surface = nullptr;
cairo_t* cr = nullptr;
bool noBackgroundExport = false;
ExportBackgroundType exportBackground = EXPORT_BACKGROUND_ALL;
string lastError;
};

@ -7,6 +7,6 @@ XojPdfExport::~XojPdfExport() = default;
/**
* Export without background
*/
void XojPdfExport::setNoBackgroundExport(bool noBackgroundExport) {
void XojPdfExport::setExportBackground(ExportBackgroundType exportBackground) {
// Does nothing in the base class
}

@ -14,6 +14,8 @@
#include <string>
#include <vector>
#include "control/jobs/BaseExportJob.h"
#include "PageRange.h"
#include "XournalType.h"
#include "filesystem.h"
@ -31,7 +33,7 @@ public:
/**
* Export without background
*/
virtual void setNoBackgroundExport(bool noBackgroundExport);
virtual void setExportBackground(ExportBackgroundType exportBackground);
private:
};

Loading…
Cancel
Save