|
28 | 28 | #include "dom/imageStore.h" |
29 | 29 | #include "dom/audio.h" |
30 | 30 |
|
| 31 | +#include "draw/painter.h" |
| 32 | + |
31 | 33 | #include "rwregister.h" |
32 | 34 | #include "inoutdata.h" |
33 | 35 |
|
34 | 36 | #include "log.h" |
35 | 37 |
|
| 38 | +#ifndef NO_QT_SUPPORT |
| 39 | +#include <QPdfWriter> |
| 40 | +#endif |
| 41 | + |
36 | 42 | using namespace mu; |
37 | 43 | using namespace muse; |
38 | 44 | using namespace muse::io; |
| 45 | +using namespace draw; |
39 | 46 | using namespace mu::engraving; |
40 | 47 | using namespace mu::engraving::rw; |
41 | 48 |
|
| 49 | +#ifndef NO_QT_SUPPORT |
| 50 | +static void writePdfPreview(MasterScore* score, io::IODevice* pdfBuf) |
| 51 | +{ |
| 52 | + QByteArray qdata; |
| 53 | + QBuffer buf(&qdata); |
| 54 | + buf.open(QIODevice::WriteOnly); |
| 55 | + |
| 56 | + QPdfWriter pdfWriter(&buf); |
| 57 | + |
| 58 | + pdfWriter.setResolution(mu::engraving::DPI); |
| 59 | + pdfWriter.setCreator(QString("MuseScore Studio Version: ") + QString(score->appVersion())); |
| 60 | + pdfWriter.setPageMargins(QMarginsF()); |
| 61 | + |
| 62 | + SizeF size = score->renderer()->pageSizeInch(score); |
| 63 | + pdfWriter.setPageLayout(QPageLayout(QPageSize(size.toQSizeF(), QPageSize::Inch), QPageLayout::Orientation::Portrait, QMarginsF())); |
| 64 | + |
| 65 | + QString title = score->metaTag(u"workTitle"); |
| 66 | + if (title.isEmpty()) { |
| 67 | + title = score->name(); |
| 68 | + } |
| 69 | + pdfWriter.setTitle(title); |
| 70 | + |
| 71 | + Painter painter(&pdfWriter, "pdfwriter"); |
| 72 | + |
| 73 | + rendering::IScoreRenderer::PaintOptions opt; |
| 74 | + opt.deviceDpi = pdfWriter.logicalDpiX(); |
| 75 | + opt.onNewPage = [&pdfWriter]() { pdfWriter.newPage(); }; |
| 76 | + opt.printPageBackground = true; |
| 77 | + opt.isSetViewport = true; |
| 78 | + opt.isMultiPage = false; |
| 79 | + opt.isPrinting = true; |
| 80 | + opt.onPaintPageSheet = [](Painter* painter, const Page* page, const RectF& pageRect) { |
| 81 | + UNUSED(page); |
| 82 | + painter->fillRect(pageRect, Color::WHITE); |
| 83 | + }; |
| 84 | + |
| 85 | + score->renderer()->paintScore(&painter, score, opt); |
| 86 | + |
| 87 | + painter.endDraw(); |
| 88 | + |
| 89 | + pdfBuf->write(qdata); |
| 90 | +} |
| 91 | + |
| 92 | +#endif |
| 93 | + |
42 | 94 | bool MscSaver::writeMscz(MasterScore* score, MscWriter& mscWriter, bool onlySelection, bool doCreateThumbnail) |
43 | 95 | { |
44 | 96 | TRACEFUNC; |
@@ -147,6 +199,18 @@ bool MscSaver::writeMscz(MasterScore* score, MscWriter& mscWriter, bool onlySele |
147 | 199 | } |
148 | 200 | } |
149 | 201 |
|
| 202 | + // Write PDF file, used for macOS Quick Look |
| 203 | + // TODO: do we always want this? e.g. when is doCreateThumbnail false? |
| 204 | + #ifndef NO_QT_SUPPORT |
| 205 | + { |
| 206 | + ByteArray pdfData; |
| 207 | + Buffer pdfBuf(&pdfData); |
| 208 | + pdfBuf.open(IODevice::WriteOnly); |
| 209 | + writePdfPreview(score, &pdfBuf); |
| 210 | + mscWriter.writePdfFile(pdfData); |
| 211 | + } |
| 212 | + #endif |
| 213 | + |
150 | 214 | // Write audio |
151 | 215 | { |
152 | 216 | if (score->audio()) { |
|
0 commit comments