Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PdfExport of PDF background #5681

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/control/PrintHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ void drawPage(GtkPrintOperation* /*operation*/, GtkPrintContext* context, int pa
}
}

xoj::view::BackgroundFlags flags = xoj::view::BACKGROUND_SHOW_ALL;
flags.showPDF = xoj::view::HIDE_PDF_BACKGROUND; // Already printed (if any)

DocumentView view;
view.drawPage(page, cr, true /* dont render eraseable */, true /* dont show pdf background*/);
view.drawPage(page, cr, true /* dont render eraseable */, flags);
}

void requestPageSetup(GtkPrintOperation* /*op*/, GtkPrintContext* /*ctx*/, int pageNr, GtkPageSetup* setup,
Expand Down
14 changes: 9 additions & 5 deletions src/core/control/jobs/ImageExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,17 @@ void ImageExport::exportImagePage(size_t pageId, size_t id, double zoomRatio, Ex
}
}

xoj::view::BackgroundFlags flags;
flags.showPDF = xoj::view::HIDE_PDF_BACKGROUND; // Already exported (if any)
flags.showImage = exportBackground == EXPORT_BACKGROUND_NONE ? xoj::view::HIDE_IMAGE_BACKGROUND :
xoj::view::SHOW_IMAGE_BACKGROUND;
flags.showRuling = exportBackground <= EXPORT_BACKGROUND_UNRULED ? xoj::view::HIDE_RULING_BACKGROUND :
xoj::view::SHOW_RULING_BACKGROUND;

if (layerRange) {
view.drawLayersOfPage(*layerRange, page, this->cr, true /* dont render eraseable */,
true /* don't rerender the pdf background */, exportBackground == EXPORT_BACKGROUND_NONE,
exportBackground <= EXPORT_BACKGROUND_UNRULED);
view.drawLayersOfPage(*layerRange, page, this->cr, true /* dont render eraseable */, flags);
} else {
view.drawPage(page, this->cr, true /* dont render eraseable */, true /* don't rerender the pdf background */,
exportBackground == EXPORT_BACKGROUND_NONE, exportBackground <= EXPORT_BACKGROUND_UNRULED);
view.drawPage(page, this->cr, true /* dont render eraseable */, flags);
}

if (!freeSurface(id)) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/control/jobs/PreviewJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "view/DocumentView.h" // for Doc...
#include "view/LayerView.h" // for Lay...
#include "view/View.h" // for Con...
#include "view/background/BackgroundView.h" // for BAC...
#include "view/background/BackgroundFlags.h" // for BAC...

PreviewJob::PreviewJob(SidebarPreviewBaseEntry* sidebar): sidebarPreview(sidebar) {}

Expand Down Expand Up @@ -82,7 +82,7 @@ void PreviewJob::drawPage() {
flags.forceVisible = xoj::view::FORCE_VISIBLE;
view.drawBackground(flags);
} else {
view.drawBackground(xoj::view::BACKGROUND_SHOW_ONLY_BACKGROUND_COLOR);
view.drawBackground(xoj::view::BACKGROUND_FORCE_PAINT_BACKGROUND_COLOR_ONLY);
Layer* drawLayer = (*page->getLayers())[layer - 1];
xoj::view::LayerView layerView(drawLayer);
layerView.draw(context);
Expand Down
7 changes: 6 additions & 1 deletion src/core/control/jobs/SaveJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void SaveJob::updatePreview(Control* control) {
cairo_t* cr = cairo_create(crBuffer);
cairo_scale(cr, zoom, zoom);

xoj::view::BackgroundFlags flags = xoj::view::BACKGROUND_SHOW_ALL;

// We don't have access to a PdfCache on which DocumentView relies for PDF backgrounds.
// We thus print the PDF background by hand.
if (page->getBackgroundType().isPdfPage()) {
Expand All @@ -78,10 +80,13 @@ void SaveJob::updatePreview(Control* control) {
if (popplerPage) {
popplerPage->render(cr);
}
flags.showPDF = xoj::view::HIDE_PDF_BACKGROUND; // Already printed (if any)
} else {
flags.forceBackgroundColor = xoj::view::FORCE_AT_LEAST_BACKGROUND_COLOR;
}

DocumentView view;
view.drawPage(page, cr, true /* don't render erasable */, true /* Don't rerender the pdf background */);
view.drawPage(page, cr, true /* don't render erasable */, flags);
cairo_destroy(cr);
doc->setPreview(crBuffer);
cairo_surface_destroy(crBuffer);
Expand Down
14 changes: 9 additions & 5 deletions src/core/pdf/base/XojCairoPdfExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ void XojCairoPdfExport::exportPage(size_t page) {
popplerPage->renderForPrinting(cr);
}

xoj::view::BackgroundFlags flags;
flags.showPDF = xoj::view::HIDE_PDF_BACKGROUND; // Already exported (if any)
flags.showImage = exportBackground == EXPORT_BACKGROUND_NONE ? xoj::view::HIDE_IMAGE_BACKGROUND :
xoj::view::SHOW_IMAGE_BACKGROUND;
flags.showRuling = exportBackground <= EXPORT_BACKGROUND_UNRULED ? xoj::view::HIDE_RULING_BACKGROUND :
xoj::view::SHOW_RULING_BACKGROUND;

if (layerRange) {
view.drawLayersOfPage(*layerRange, p, this->cr, true /* dont render eraseable */,
true /* don't rerender the pdf background */, exportBackground == EXPORT_BACKGROUND_NONE,
exportBackground <= EXPORT_BACKGROUND_UNRULED);
view.drawLayersOfPage(*layerRange, p, this->cr, true /* dont render eraseable */, flags);
} else {
view.drawPage(p, this->cr, true /* dont render eraseable */, true /* don't rerender the pdf background */,
exportBackground == EXPORT_BACKGROUND_NONE, exportBackground <= EXPORT_BACKGROUND_UNRULED);
view.drawPage(p, this->cr, true /* dont render eraseable */, flags);
}

// next page
Expand Down
29 changes: 4 additions & 25 deletions src/core/view/DocumentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,10 @@ void DocumentView::drawBackground(xoj::view::BackgroundFlags bgFlags) const {
bgView->draw(cr);
}

/**
* Draw the full page, usually you would like to call this method
* @param page The page to draw
* @param cr Draw to thgis context
* @param dontRenderEditingStroke false to draw currently drawing stroke
* @param hideBackground true to hide the background
*/
void DocumentView::drawPage(PageRef page, cairo_t* cr, bool dontRenderEditingStroke, bool hidePdfBackground,
bool hideImageBackground, bool hideRulingBackground) {
void DocumentView::drawPage(PageRef page, cairo_t* cr, bool dontRenderEditingStroke, xoj::view::BackgroundFlags flags) {
initDrawing(page, cr, dontRenderEditingStroke);

{ // Draw background
xoj::view::BackgroundFlags bgFlags;
bgFlags.showImage = (xoj::view::ImageBackgroundTreatment)!hideImageBackground;
bgFlags.showPDF = (xoj::view::PDFBackgroundTreatment)!hidePdfBackground;
bgFlags.showRuling = (xoj::view::RulingBackgroundTreatment)!hideRulingBackground;
drawBackground(bgFlags);
}
drawBackground(flags);

xoj::view::Context context{cr, (xoj::view::NonAudioTreatment)this->markAudioStroke,
(xoj::view::EditionTreatment) !this->dontRenderEditingStroke, xoj::view::NORMAL_COLOR};
Expand All @@ -98,17 +84,10 @@ void DocumentView::drawPage(PageRef page, cairo_t* cr, bool dontRenderEditingStr


void DocumentView::drawLayersOfPage(const LayerRangeVector& layerRange, PageRef page, cairo_t* cr,
bool dontRenderEditingStroke, bool hidePdfBackground, bool hideImageBackground,
bool hideRulingBackground) {
bool dontRenderEditingStroke, xoj::view::BackgroundFlags flags) {
initDrawing(page, cr, dontRenderEditingStroke);

{ // Draw background
xoj::view::BackgroundFlags bgFlags;
bgFlags.showImage = (xoj::view::ImageBackgroundTreatment)!hideImageBackground;
bgFlags.showPDF = (xoj::view::PDFBackgroundTreatment)!hidePdfBackground;
bgFlags.showRuling = (xoj::view::RulingBackgroundTreatment)!hideRulingBackground;
drawBackground(bgFlags);
}
drawBackground(flags);

size_t layerCount = page->getLayerCount();
std::vector<bool> visible(layerCount, false);
Expand Down
16 changes: 6 additions & 10 deletions src/core/view/DocumentView.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "model/PageRef.h" // for PageRef
#include "util/ElementRange.h"
#include "view/background/BackgroundFlags.h"

class PdfCache;

Expand All @@ -33,26 +34,21 @@ class DocumentView {
* @param page The page to draw
* @param cr Draw to thgis context
* @param dontRenderEditingStroke false to draw currently drawing stroke
* @param hidePdfBackground true to hide the PDF background
* @param hideImageBackground true to hide the PDF background
* @param hideRulingBacground true to hide the ruling background
* @param flags show/hide various background components
*/
void drawPage(PageRef page, cairo_t* cr, bool dontRenderEditingStroke, bool hidePdfBackground = false,
bool hideImageBackground = false, bool hideRulingBackground = false);
void drawPage(PageRef page, cairo_t* cr, bool dontRenderEditingStroke,
xoj::view::BackgroundFlags flags = xoj::view::BACKGROUND_SHOW_ALL);

/**
* Only draws the prescribed layers of the given page, regardless of the layer's current visibility.
* @param layerRange Range of layers to draw
* @param page The page to draw
* @param cr Draw to this context
* @param dontRenderEditingStroke false to draw currently drawing stroke
* @param hidePdfBackground true to hide the PDF background
* @param hideImageBackground true to hide the PDF background
* @param hideRulingBacground true to hide the ruling background
* @param flags show/hide various background components
*/
void drawLayersOfPage(const LayerRangeVector& layerRange, PageRef page, cairo_t* cr, bool dontRenderEditingStroke,
bool hidePdfBackground = false, bool hideImageBackground = false,
bool hideRulingBackground = false);
xoj::view::BackgroundFlags flags = xoj::view::BACKGROUND_SHOW_ALL);

/**
* Mark stroke with Audio
Expand Down
37 changes: 37 additions & 0 deletions src/core/view/background/BackgroundFlags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Xournal++
*
* Flags for background painting
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/

#pragma once

namespace xoj::view {

enum PDFBackgroundTreatment : bool { SHOW_PDF_BACKGROUND = true, HIDE_PDF_BACKGROUND = false };
enum ImageBackgroundTreatment : bool { SHOW_IMAGE_BACKGROUND = true, HIDE_IMAGE_BACKGROUND = false };
enum RulingBackgroundTreatment : bool { SHOW_RULING_BACKGROUND = true, HIDE_RULING_BACKGROUND = false };
enum BackgroundColorTreatment : bool { FORCE_AT_LEAST_BACKGROUND_COLOR = true, DONT_FORCE_BACKGROUND_COLOR = false };
enum VisibilityTreatment : bool { FORCE_VISIBLE = true, USE_DOCUMENT_VISIBILITY = false };

struct BackgroundFlags {
PDFBackgroundTreatment showPDF;
ImageBackgroundTreatment showImage;
RulingBackgroundTreatment showRuling;
BackgroundColorTreatment forceBackgroundColor = DONT_FORCE_BACKGROUND_COLOR;
VisibilityTreatment forceVisible = USE_DOCUMENT_VISIBILITY;
};

static constexpr BackgroundFlags BACKGROUND_SHOW_ALL = {SHOW_PDF_BACKGROUND, SHOW_IMAGE_BACKGROUND,
SHOW_RULING_BACKGROUND, DONT_FORCE_BACKGROUND_COLOR,
USE_DOCUMENT_VISIBILITY};

static constexpr BackgroundFlags BACKGROUND_FORCE_PAINT_BACKGROUND_COLOR_ONLY = {
HIDE_PDF_BACKGROUND, HIDE_IMAGE_BACKGROUND, HIDE_RULING_BACKGROUND, FORCE_AT_LEAST_BACKGROUND_COLOR,
FORCE_VISIBLE};
} // namespace xoj::view
8 changes: 6 additions & 2 deletions src/core/view/background/BackgroundView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ auto BackgroundView::createForPage(PageRef page, BackgroundFlags bgFlags, PdfCac
return createRuled(width, height, page->getBackgroundColor(), pt);
}
}
// Only colorize the page
return std::make_unique<PlainBackgroundView>(width, height, page->getBackgroundColor());

if (bgFlags.forceBackgroundColor) {
return std::make_unique<PlainBackgroundView>(width, height, page->getBackgroundColor());
} else {
return std::make_unique<BackgroundView>(width, height); // Dummy no-op view
}
}
21 changes: 2 additions & 19 deletions src/core/view/background/BackgroundView.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,13 @@
#include "model/PageRef.h" // for PageRef
#include "util/Color.h" // for Color

#include "BackgroundFlags.h"

class PdfCache;
class PageType;

namespace xoj {
namespace view {

enum PDFBackgroundTreatment : bool { SHOW_PDF_BACKGROUND = true, HIDE_PDF_BACKGROUND = false };
enum ImageBackgroundTreatment : bool { SHOW_IMAGE_BACKGROUND = true, HIDE_IMAGE_BACKGROUND = false };
enum RulingBackgroundTreatment : bool { SHOW_RULING_BACKGROUND = true, HIDE_RULING_BACKGROUND = false };
enum VisibilityTreatment : bool { FORCE_VISIBLE = true, USE_DOCUMENT_VISIBILITY = false };

struct BackgroundFlags {
PDFBackgroundTreatment showPDF;
ImageBackgroundTreatment showImage;
RulingBackgroundTreatment showRuling;
VisibilityTreatment forceVisible = USE_DOCUMENT_VISIBILITY;
};

static constexpr BackgroundFlags BACKGROUND_SHOW_ALL = {SHOW_PDF_BACKGROUND, SHOW_IMAGE_BACKGROUND,
SHOW_RULING_BACKGROUND};

static constexpr BackgroundFlags BACKGROUND_SHOW_ONLY_BACKGROUND_COLOR = {HIDE_PDF_BACKGROUND, HIDE_IMAGE_BACKGROUND,
HIDE_RULING_BACKGROUND, FORCE_VISIBLE};

class BackgroundView {
public:
BackgroundView(double pageWidth, double pageHeight): pageWidth(pageWidth), pageHeight(pageHeight) {}
Expand Down