diff --git a/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp b/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp index 7d554d1..2479c12 100644 --- a/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp +++ b/Pdf4QtLibCore/sources/pdfpagecontenteditorcontentstreambuilder.cpp @@ -19,6 +19,7 @@ #include "pdfdocumentbuilder.h" #include "pdfobject.h" #include "pdfstreamfilters.h" +#include "pdfpainterutils.h" #include #include @@ -171,19 +172,19 @@ void PDFPageContentEditorContentStreamBuilder::writeStateDifference(QTextStream& const PDFAbstractColorSpace* fillColorSpace = m_currentState.getFillColorSpace(); if (fillColorSpace && fillColorSpace->getColorSpace() == PDFAbstractColorSpace::ColorSpace::DeviceGray) { - stream << qGray(color.rgb()) / 255.0 << " G" << Qt::endl; + stream << qGray(color.rgb()) / 255.0 << " g" << Qt::endl; } else if (fillColorSpace && fillColorSpace->getColorSpace() == PDFAbstractColorSpace::ColorSpace::DeviceCMYK) { const PDFColor& fillColor = m_currentState.getFillColorOriginal(); if (fillColor.size() >= 4) { - stream << fillColor[0] << " " << fillColor[1] << " " << fillColor[2] << " " << fillColor[3] << " K" << Qt::endl; + stream << fillColor[0] << " " << fillColor[1] << " " << fillColor[2] << " " << fillColor[3] << " k" << Qt::endl; } } else { - stream << color.redF() << " " << color.greenF() << " " << color.blueF() << " RG" << Qt::endl; + stream << color.redF() << " " << color.greenF() << " " << color.blueF() << " rg" << Qt::endl; } } @@ -779,7 +780,8 @@ void PDFPageContentEditorContentStreamBuilder::writeStyledPath(const QPainterPat PDFPageContentProcessorState newState = m_currentState; newState.setCurrentTransformationMatrix(QTransform()); - // TODO: Write pen/brush + PDFPainterHelper::applyPenToGraphicState(&newState, pen); + PDFPainterHelper::applyBrushToGraphicState(&newState, brush); QTextStream stream(&m_outputContent, QDataStream::WriteOnly | QDataStream::Append); writeStateDifference(stream, newState); diff --git a/Pdf4QtLibCore/sources/pdfpagecontenteditorprocessor.cpp b/Pdf4QtLibCore/sources/pdfpagecontenteditorprocessor.cpp index 86a7d3b..31c03f5 100644 --- a/Pdf4QtLibCore/sources/pdfpagecontenteditorprocessor.cpp +++ b/Pdf4QtLibCore/sources/pdfpagecontenteditorprocessor.cpp @@ -31,8 +31,15 @@ PDFPageContentEditorProcessor::PDFPageContentEditorProcessor(const PDFPage* page { m_clippingPaths.push(QPainterPath()); - m_content.setFontDictionary(*getFontDictionary()); - m_content.setXObjectDictionary(*getXObjectDictionary()); + if (auto fontDictionary = getFontDictionary()) + { + m_content.setFontDictionary(*fontDictionary); + } + + if (auto xObjectDictionary = getXObjectDictionary()) + { + m_content.setXObjectDictionary(*xObjectDictionary); + } } const PDFEditedPageContent& PDFPageContentEditorProcessor::getEditedPageContent() const @@ -430,12 +437,14 @@ QString PDFEditedPageContent::getOperandName(PDFPageContentProcessor::Operator o void PDFEditedPageContent::addContentPath(PDFPageContentProcessorState state, QPainterPath path, bool strokePath, bool fillPath) { - m_contentElements.emplace_back(new PDFEditedPageContentElementPath(std::move(state), std::move(path), strokePath, fillPath, state.getCurrentTransformationMatrix())); + QTransform transform = state.getCurrentTransformationMatrix(); + m_contentElements.emplace_back(new PDFEditedPageContentElementPath(std::move(state), std::move(path), strokePath, fillPath, transform)); } void PDFEditedPageContent::addContentImage(PDFPageContentProcessorState state, PDFObject imageObject, QImage image) { - m_contentElements.emplace_back(new PDFEditedPageContentElementImage(std::move(state), std::move(imageObject), std::move(image), state.getCurrentTransformationMatrix())); + QTransform transform = state.getCurrentTransformationMatrix(); + m_contentElements.emplace_back(new PDFEditedPageContentElementImage(std::move(state), std::move(imageObject), std::move(image), transform)); } void PDFEditedPageContent::addContentElement(std::unique_ptr element) diff --git a/Pdf4QtLibCore/sources/pdfpainterutils.cpp b/Pdf4QtLibCore/sources/pdfpainterutils.cpp index 10ffd48..83f0e1d 100644 --- a/Pdf4QtLibCore/sources/pdfpainterutils.cpp +++ b/Pdf4QtLibCore/sources/pdfpainterutils.cpp @@ -117,6 +117,58 @@ QBrush PDFPainterHelper::createBrushFromState(const PDFPageContentProcessorState } } +void PDFPainterHelper::applyPenToGraphicState(PDFPageContentProcessorState* graphicState, const QPen& pen) +{ + if (pen.style() != Qt::NoPen) + { + graphicState->setLineWidth(pen.widthF()); + graphicState->setLineCapStyle(pen.capStyle()); + graphicState->setLineJoinStyle(pen.joinStyle()); + graphicState->setMitterLimit(pen.miterLimit()); + + QColor color = pen.color(); + + graphicState->setAlphaStroking(color.alphaF()); + + const PDFAbstractColorSpace* strokeColorSpace = graphicState->getStrokeColorSpace(); + if (!strokeColorSpace || strokeColorSpace->getColorSpace() != PDFAbstractColorSpace::ColorSpace::DeviceRGB) + { + graphicState->setStrokeColorSpace(QSharedPointer(new PDFDeviceRGBColorSpace())); + } + graphicState->setStrokeColor(color, PDFColor(color.redF(), color.greenF(), color.blueF())); + + if (pen.style() == Qt::SolidLine) + { + graphicState->setLineDashPattern(PDFLineDashPattern()); + } + else + { + // TODO: Line Dash Pattern + /* + pen.setStyle(Qt::CustomDashLine); + pen.setDashPattern(lineDashPattern.createForQPen(pen.widthF())); + pen.setDashOffset(lineDashPattern.getDashOffset());*/ + } + } +} + +void PDFPainterHelper::applyBrushToGraphicState(PDFPageContentProcessorState* graphicState, const QBrush& brush) +{ + if (brush.style() != Qt::NoBrush) + { + QColor color = brush.color(); + + graphicState->setAlphaFilling(color.alphaF()); + + const PDFAbstractColorSpace* fillColorSpace = graphicState->getFillColorSpace(); + if (!fillColorSpace || fillColorSpace->getColorSpace() != PDFAbstractColorSpace::ColorSpace::DeviceRGB) + { + graphicState->setFillColorSpace(QSharedPointer(new PDFDeviceRGBColorSpace())); + } + graphicState->setFillColor(color, PDFColor(color.redF(), color.greenF(), color.blueF())); + } +} + PDFTransformationDecomposition PDFPainterHelper::decomposeTransform(const QTransform& transform) { PDFTransformationDecomposition result; diff --git a/Pdf4QtLibCore/sources/pdfpainterutils.h b/Pdf4QtLibCore/sources/pdfpainterutils.h index 7563bc4..008a5ed 100644 --- a/Pdf4QtLibCore/sources/pdfpainterutils.h +++ b/Pdf4QtLibCore/sources/pdfpainterutils.h @@ -73,6 +73,9 @@ public: /// Creates brush from painter graphicState static QBrush createBrushFromState(const PDFPageContentProcessorState* graphicState, double alpha); + static void applyPenToGraphicState(PDFPageContentProcessorState* graphicState, const QPen& pen); + static void applyBrushToGraphicState(PDFPageContentProcessorState* graphicState, const QBrush& brush); + /// Decompose transform static PDFTransformationDecomposition decomposeTransform(const QTransform& transform); diff --git a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp index 99a8f8b..31395d5 100644 --- a/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp +++ b/Pdf4QtLibWidgets/sources/pdfpagecontentelements.cpp @@ -2618,8 +2618,10 @@ void PDFPageContentElementEdited::drawPage(QPainter* painter, if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath()) { const PDFPageContentProcessorState& state = m_element->getState(); - painter->setPen(pdf::PDFPainterHelper::createPenFromState(&state, state.getAlphaStroking())); - painter->setBrush(pdf::PDFPainterHelper::createBrushFromState(&state, state.getAlphaFilling())); + QPen pen = pdf::PDFPainterHelper::createPenFromState(&state, state.getAlphaStroking()); + QBrush brush = pdf::PDFPainterHelper::createBrushFromState(&state, state.getAlphaFilling()); + painter->setPen(pathElement->getStrokePath() ? pen : QPen(Qt::NoPen)); + painter->setBrush(pathElement->getFillPath() ? brush : QBrush(Qt::NoBrush)); painter->drawPath(pathElement->getPath()); }