From 69ba66ae04608feaff501e9e88556cd787b91822 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 1 Sep 2019 15:44:22 +0200 Subject: [PATCH] Minor bufixes in axial shading, clipping on crop box --- PdfForQtLib/sources/pdffont.cpp | 2 +- PdfForQtLib/sources/pdfpage.cpp | 5 +++ PdfForQtLib/sources/pdfpage.h | 1 + PdfForQtLib/sources/pdfpainter.cpp | 12 +++++++ PdfForQtLib/sources/pdfpattern.cpp | 50 ++++++++++++++++++++++------- PdfForQtLib/sources/pdfrenderer.cpp | 2 -- PdfForQtLib/sources/pdfrenderer.h | 3 +- 7 files changed, 60 insertions(+), 15 deletions(-) diff --git a/PdfForQtLib/sources/pdffont.cpp b/PdfForQtLib/sources/pdffont.cpp index 2760225..bef4688 100644 --- a/PdfForQtLib/sources/pdffont.cpp +++ b/PdfForQtLib/sources/pdffont.cpp @@ -1229,7 +1229,7 @@ PDFFontPointer PDFFont::createFont(const PDFObject& object, const PDFDocument* d if (characterContentStreamObject.isStream()) { QByteArray contentStream = document->getDecodedStream(characterContentStreamObject.getStream()); - characterContentStreams[currentOffset] = qMove(contentStream); + characterContentStreams[static_cast(currentOffset)] = qMove(contentStream); } ++currentOffset; diff --git a/PdfForQtLib/sources/pdfpage.cpp b/PdfForQtLib/sources/pdfpage.cpp index 4a07468..43b68b7 100644 --- a/PdfForQtLib/sources/pdfpage.cpp +++ b/PdfForQtLib/sources/pdfpage.cpp @@ -121,6 +121,11 @@ QRectF PDFPage::getRotatedMediaBox() const return getRotatedBox(getMediaBox(), getPageRotation()); } +QRectF PDFPage::getRotatedCropBox() const +{ + return getRotatedBox(getCropBox(), getPageRotation()); +} + QRectF PDFPage::getRotatedBox(const QRectF& rect, PageRotation rotation) { switch (rotation) diff --git a/PdfForQtLib/sources/pdfpage.h b/PdfForQtLib/sources/pdfpage.h index 89ffd88..83b5b3a 100644 --- a/PdfForQtLib/sources/pdfpage.h +++ b/PdfForQtLib/sources/pdfpage.h @@ -95,6 +95,7 @@ public: inline QRectF getArtBoxMM() const { return getRectMM(m_artBox); } QRectF getRotatedMediaBox() const; + QRectF getRotatedCropBox() const; static QRectF getRotatedBox(const QRectF& rect, PageRotation rotation); diff --git a/PdfForQtLib/sources/pdfpainter.cpp b/PdfForQtLib/sources/pdfpainter.cpp index 2155722..d19c644 100644 --- a/PdfForQtLib/sources/pdfpainter.cpp +++ b/PdfForQtLib/sources/pdfpainter.cpp @@ -38,6 +38,18 @@ PDFPainter::PDFPainter(QPainter* painter, Q_ASSERT(pagePointToDevicePointMatrix.isInvertible()); m_painter->save(); + + if (features.testFlag(PDFRenderer::ClipToCropBox)) + { + QRectF cropBox = page->getRotatedCropBox(); + if (cropBox.isValid()) + { + QPainterPath path; + path.addPolygon(pagePointToDevicePointMatrix.map(cropBox)); + + m_painter->setClipPath(path, Qt::IntersectClip); + } + } } PDFPainter::~PDFPainter() diff --git a/PdfForQtLib/sources/pdfpattern.cpp b/PdfForQtLib/sources/pdfpattern.cpp index 9e609e5..44165af 100644 --- a/PdfForQtLib/sources/pdfpattern.cpp +++ b/PdfForQtLib/sources/pdfpattern.cpp @@ -239,6 +239,12 @@ PDFMesh PDFAxialShading::createMesh(const PDFMeshQualitySettings& settings) cons xCoords.push_back(x); } } + + if (xCoords.back() + PDF_EPSILON < p2m.x()) + { + xCoords.push_back(p2m.x()); + } + if (!qFuzzyCompare(xCoords.back(), xr)) { xCoords.push_back(xr); @@ -319,18 +325,21 @@ PDFMesh PDFAxialShading::createMesh(const PDFMeshQualitySettings& settings) cons const std::pair& currentItem = *it; const std::pair& nextItem = *itNext; - if (prevItem.second == currentItem.second && currentItem.second == nextItem.second) + if (currentItem.first != p1m.x() && currentItem.first != p2m.x()) { - // Colors are same, skip the test - continue; - } + if (prevItem.second == currentItem.second && currentItem.second == nextItem.second) + { + // Colors are same, skip the test + continue; + } - if (PDFAbstractColorSpace::isColorEqual(prevItem.second, currentItem.second, settings.tolerance) && - PDFAbstractColorSpace::isColorEqual(currentItem.second, nextItem.second, settings.tolerance) && - PDFAbstractColorSpace::isColorEqual(prevItem.second, nextItem.second, settings.tolerance) && - (nextItem.first - prevItem.first < settings.preferredMeshResolution)) - { - continue; + if (PDFAbstractColorSpace::isColorEqual(prevItem.second, currentItem.second, settings.tolerance) && + PDFAbstractColorSpace::isColorEqual(currentItem.second, nextItem.second, settings.tolerance) && + PDFAbstractColorSpace::isColorEqual(prevItem.second, nextItem.second, settings.tolerance) && + (nextItem.first - prevItem.first < settings.preferredMeshResolution)) + { + continue; + } } } @@ -370,7 +379,26 @@ PDFMesh PDFAxialShading::createMesh(const PDFMeshQualitySettings& settings) cons } // Create background color triangles - // TODO: Create background color for axial shading + if (m_backgroundColor.isValid() && (!m_extendStart || !m_extendEnd)) + { + if (!m_extendStart && xl + PDF_EPSILON < p1m.x()) + { + uint32_t topLeft = mesh.addVertex(QPointF(xl, yt)); + uint32_t topRight = mesh.addVertex(QPointF(p1m.x(), yt)); + uint32_t bottomLeft = mesh.addVertex(QPointF(xl, yb)); + uint32_t bottomRight = mesh.addVertex(QPointF(p1m.x(), yb)); + mesh.addQuad(topLeft, topRight, bottomRight, bottomLeft, m_backgroundColor.rgb()); + } + + if (!m_extendEnd && p2m.x() + PDF_EPSILON < xr) + { + uint32_t topRight = mesh.addVertex(QPointF(xr, yt)); + uint32_t topLeft = mesh.addVertex(QPointF(p2m.x(), yt)); + uint32_t bottomRight = mesh.addVertex(QPointF(xr, yb)); + uint32_t bottomLeft = mesh.addVertex(QPointF(p2m.x(), yb)); + mesh.addQuad(topLeft, topRight, bottomRight, bottomLeft, m_backgroundColor.rgb()); + } + } // Transform mesh to the device space coordinates mesh.transform(p1p2LCS); diff --git a/PdfForQtLib/sources/pdfrenderer.cpp b/PdfForQtLib/sources/pdfrenderer.cpp index 3d0459a..bea5f34 100644 --- a/PdfForQtLib/sources/pdfrenderer.cpp +++ b/PdfForQtLib/sources/pdfrenderer.cpp @@ -31,8 +31,6 @@ PDFRenderer::PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCa Q_ASSERT(document); } -// TODO: Clipovani na stranku - QList PDFRenderer::render(QPainter* painter, const QRectF& rectangle, size_t pageIndex) const { Q_UNUSED(painter); diff --git a/PdfForQtLib/sources/pdfrenderer.h b/PdfForQtLib/sources/pdfrenderer.h index 6785ef0..1bde704 100644 --- a/PdfForQtLib/sources/pdfrenderer.h +++ b/PdfForQtLib/sources/pdfrenderer.h @@ -39,6 +39,7 @@ public: TextAntialiasing = 0x0002, ///< Antialiasing for drawing text SmoothImages = 0x0004, ///< Adjust images to the device space using smooth transformation (slower, but better image quality) IgnoreOptionalContent = 0x0008, ///< Ignore optional content (so all is drawn ignoring settings of optional content) + ClipToCropBox = 0x0010, ///< Clip page content to crop box (items outside crop box will not be visible) }; Q_DECLARE_FLAGS(Features, Feature) @@ -59,7 +60,7 @@ public: QList render(QPainter* painter, const QMatrix& matrix, size_t pageIndex) const; /// Returns default renderer features - static constexpr Features getDefaultFeatures() { return Antialiasing | TextAntialiasing; } + static constexpr Features getDefaultFeatures() { return Antialiasing | TextAntialiasing | ClipToCropBox; } private: const PDFDocument* m_document;