From 54d093ab8c826ccf8ad75e5d035885b0da96f298 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Thu, 22 Feb 2024 19:43:43 +0100 Subject: [PATCH] Issue #123: Clipping images --- Pdf4QtLibCore/sources/pdfblpainter.cpp | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/Pdf4QtLibCore/sources/pdfblpainter.cpp b/Pdf4QtLibCore/sources/pdfblpainter.cpp index c2e85af..30f59fa 100644 --- a/Pdf4QtLibCore/sources/pdfblpainter.cpp +++ b/Pdf4QtLibCore/sources/pdfblpainter.cpp @@ -266,12 +266,6 @@ void PDFBLPaintEngine::updateFont(QFont newFont) void PDFBLPaintEngine::updateState(const QPaintEngineState& updatedState) { - /* DirtyBrushOrigin = 0x0004, - DirtyBackground = 0x0010, - DirtyBackgroundMode = 0x0020, - DirtyClipRegion = 0x0080, - DirtyClipPath = 0x0100,*/ - if (updatedState.state().testFlag(QPaintEngine::DirtyPen)) { m_currentPen = updatedState.pen(); @@ -534,7 +528,7 @@ void PDFBLPaintEngine::drawPathImpl(const QPainterPath& path, bool enableStroke, BLPath blPath = getBLPath(path); - if (isFillActive() && enableFill) + if ((isFillActive() && enableFill)) { m_blContext->fillPath(blPath); } @@ -622,6 +616,8 @@ void PDFBLPaintEngine::drawTextItem(const QPointF& p, const QTextItem& textItem) currentPosition += glyphPositions[i]; } + drawPathImpl(path, false, true); + m_blContext->save(); setFillRule(path.fillRule()); m_blContext->setFillStyle(BLRgba32(m_currentPen.color().rgba())); @@ -682,6 +678,26 @@ void PDFBLPaintEngine::drawImage(const QRectF& r, const QImage& pm, const QRectF image.convertTo(QImage::Format_ARGB32_Premultiplied); } + if (clipMode == ClipMode::NeedsResolve) + { + QImage mask(image.size(), QImage::Format_ARGB32); + mask.fill(Qt::transparent); + + QPainter maskPainter(&mask); + maskPainter.setCompositionMode(QPainter::CompositionMode_Source); + + QPainterPath path = m_finalClipPath; + path = m_currentTransform.inverted().map(path); + + maskPainter.fillPath(path, Qt::white); + maskPainter.end(); + + QPainter imagePainter(&image); + imagePainter.setCompositionMode(QPainter::CompositionMode_DestinationIn); + imagePainter.drawImage(0, 0, mask); + imagePainter.end(); + } + BLImage blImage; blImage.createFromData(image.width(), image.height(), BL_FORMAT_PRGB32, image.bits(), image.bytesPerLine()); @@ -1093,7 +1109,7 @@ void PDFBLPaintEngine::updateClipping(std::optional clipRegion, PDFBLPaintEngine::ClipMode PDFBLPaintEngine::resolveClipping(const QRectF& rect) const { - if (!m_currentIsClipEnabled || m_clipSingleRect || m_finalClipPath.isEmpty()) + if (!m_currentIsClipEnabled || m_clipSingleRect) { return ClipMode::NoClip; }