Skip to content

Commit

Permalink
Add transparency option to PDF export #25407 (#25688)
Browse files Browse the repository at this point in the history
Co-authored-by: Casper Jeukendrup <48658420+cbjeukendrup@users.noreply.github.com>
  • Loading branch information
LeoTaoeee and cbjeukendrup authored Dec 13, 2024
1 parent 1fa892d commit 483fb38
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/importexport/imagesexport/iimagesexportconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class IImagesExportConfiguration : MODULE_EXPORT_INTERFACE
virtual int exportPdfDpiResolution() const = 0;
virtual void setExportPdfDpiResolution(int dpi) = 0;

virtual bool exportPdfWithTransparentBackground() const = 0;
virtual void setExportPdfWithTransparentBackground(bool transparent) = 0;

// Png
virtual float exportPngDpiResolution() const = 0;
virtual void setExportPngDpiResolution(float dpi) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ using namespace mu;
using namespace mu::iex::imagesexport;

static const Settings::Key EXPORT_PDF_DPI_RESOLUTION_KEY("iex_imagesexport", "export/pdf/dpi");
static const Settings::Key EXPORT_PDF_USE_TRANSPARENCY_KEY("iex_imagesexport", "export/pdf/useTransparency");
static const Settings::Key EXPORT_PNG_DPI_RESOLUTION_KEY("iex_imagesexport", "export/png/resolution");
static const Settings::Key EXPORT_PNG_USE_TRANSPARENCY_KEY("iex_imagesexport", "export/png/useTransparency");
static const Settings::Key EXPORT_SVG_USE_TRANSPARENCY_KEY("iex_imagesexport", "export/svg/useTransparency");
Expand All @@ -51,6 +52,16 @@ void ImagesExportConfiguration::setExportPdfDpiResolution(int dpi)
settings()->setSharedValue(EXPORT_PDF_DPI_RESOLUTION_KEY, Val(dpi));
}

bool ImagesExportConfiguration::exportPdfWithTransparentBackground() const
{
return settings()->value(EXPORT_PDF_USE_TRANSPARENCY_KEY).toBool();
}

void ImagesExportConfiguration::setExportPdfWithTransparentBackground(bool transparent)
{
settings()->setSharedValue(EXPORT_PDF_USE_TRANSPARENCY_KEY, Val(transparent));
}

float ImagesExportConfiguration::exportPngDpiResolution() const
{
if (m_customExportPngDpiOverride) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class ImagesExportConfiguration : public IImagesExportConfiguration
int exportPdfDpiResolution() const override;
void setExportPdfDpiResolution(int dpi) override;

bool exportPdfWithTransparentBackground() const override;
void setExportPdfWithTransparentBackground(bool transparent) override;

float exportPngDpiResolution() const override;
void setExportPngDpiResolution(float dpi) override;
void setExportPngDpiResolutionOverride(std::optional<float> dpi) override;
Expand Down
8 changes: 8 additions & 0 deletions src/importexport/imagesexport/internal/pdfwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ Ret PdfWriter::write(INotationPtr notation, io::IODevice& destinationDevice, con
return false;
}

const bool TRANSPARENT_BACKGROUND = muse::value(options, OptionKey::TRANSPARENT_BACKGROUND,
Val(configuration()->exportPdfWithTransparentBackground())).toBool();

INotationPainting::Options opt;
opt.deviceDpi = pdfWriter.logicalDpiX();
opt.onNewPage = [&pdfWriter]() { pdfWriter.newPage(); };
opt.printPageBackground = !TRANSPARENT_BACKGROUND;

notation->painting()->paintPdf(&painter, opt);

Expand Down Expand Up @@ -107,9 +111,13 @@ Ret PdfWriter::writeList(const INotationPtrList& notations, io::IODevice& destin
return false;
}

const bool TRANSPARENT_BACKGROUND = muse::value(options, OptionKey::TRANSPARENT_BACKGROUND,
Val(configuration()->exportPdfWithTransparentBackground())).toBool();

INotationPainting::Options opt;
opt.deviceDpi = pdfWriter.logicalDpiX();
opt.onNewPage = [&pdfWriter]() { pdfWriter.newPage(); };
opt.printPageBackground = !TRANSPARENT_BACKGROUND;

for (const auto& notation : notations) {
IF_ASSERT_FAILED(notation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,19 @@ ExportSettingsPage {
}
}
}

CheckBox {
width: parent.width
text: qsTrc("project/export", "Transparent background")

navigation.name: "TransparentBackgroundCheckbox"
navigation.panel: root.navigationPanel
navigation.row: root.navigationOrder + 2

checked: root.model.pdfTransparentBackground

onClicked: {
root.model.pdfTransparentBackground = !checked
}
}
}
15 changes: 15 additions & 0 deletions src/project/view/exportdialogmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,21 @@ void ExportDialogModel::setPdfResolution(const int& resolution)
emit pdfResolutionChanged(resolution);
}

bool ExportDialogModel::pdfTransparentBackground() const
{
return imageExportConfiguration()->exportPdfWithTransparentBackground();
}

void ExportDialogModel::setPdfTransparentBackground(const bool& transparent)
{
if (transparent == pdfTransparentBackground()) {
return;
}

imageExportConfiguration()->setExportPdfWithTransparentBackground(transparent);
emit pdfTransparentBackgroundChanged(transparent);
}

int ExportDialogModel::pngResolution() const
{
return imageExportConfiguration()->exportPngDpiResolution();
Expand Down
8 changes: 8 additions & 0 deletions src/project/view/exportdialogmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class ExportDialogModel : public QAbstractListModel, public muse::async::Asyncab
Q_PROPERTY(int selectedUnitType READ selectedUnitType WRITE setUnitType NOTIFY selectedUnitTypeChanged)

Q_PROPERTY(int pdfResolution READ pdfResolution WRITE setPdfResolution NOTIFY pdfResolutionChanged)
Q_PROPERTY(
bool pdfTransparentBackground READ pdfTransparentBackground WRITE setPdfTransparentBackground NOTIFY pdfTransparentBackgroundChanged)

Q_PROPERTY(int pngResolution READ pngResolution WRITE setPngResolution NOTIFY pngResolutionChanged)
Q_PROPERTY(
bool pngTransparentBackground READ pngTransparentBackground WRITE setPngTransparentBackground NOTIFY pngTransparentBackgroundChanged)
Expand Down Expand Up @@ -117,6 +120,9 @@ class ExportDialogModel : public QAbstractListModel, public muse::async::Asyncab
int pdfResolution() const;
void setPdfResolution(const int& resolution);

bool pdfTransparentBackground() const;
void setPdfTransparentBackground(const bool& transparent);

int pngResolution() const;
void setPngResolution(const int& resolution);

Expand Down Expand Up @@ -167,6 +173,8 @@ class ExportDialogModel : public QAbstractListModel, public muse::async::Asyncab
void selectedUnitTypeChanged(project::INotationWriter::UnitType newUnitType);

void pdfResolutionChanged(int resolution);
void pdfTransparentBackgroundChanged(bool transparent);

void pngResolutionChanged(int resolution);
void pngTransparentBackgroundChanged(bool transparent);

Expand Down

0 comments on commit 483fb38

Please sign in to comment.