Editor plugin: Bugfixing

This commit is contained in:
Jakub Melka
2024-06-09 17:38:44 +02:00
parent efc6ab98da
commit f78fb54607
5 changed files with 78 additions and 10 deletions

View File

@ -19,6 +19,7 @@
#include "pdfdocumentbuilder.h" #include "pdfdocumentbuilder.h"
#include "pdfobject.h" #include "pdfobject.h"
#include "pdfstreamfilters.h" #include "pdfstreamfilters.h"
#include "pdfpainterutils.h"
#include <QStringBuilder> #include <QStringBuilder>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -171,19 +172,19 @@ void PDFPageContentEditorContentStreamBuilder::writeStateDifference(QTextStream&
const PDFAbstractColorSpace* fillColorSpace = m_currentState.getFillColorSpace(); const PDFAbstractColorSpace* fillColorSpace = m_currentState.getFillColorSpace();
if (fillColorSpace && fillColorSpace->getColorSpace() == PDFAbstractColorSpace::ColorSpace::DeviceGray) 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) else if (fillColorSpace && fillColorSpace->getColorSpace() == PDFAbstractColorSpace::ColorSpace::DeviceCMYK)
{ {
const PDFColor& fillColor = m_currentState.getFillColorOriginal(); const PDFColor& fillColor = m_currentState.getFillColorOriginal();
if (fillColor.size() >= 4) 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 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; PDFPageContentProcessorState newState = m_currentState;
newState.setCurrentTransformationMatrix(QTransform()); newState.setCurrentTransformationMatrix(QTransform());
// TODO: Write pen/brush PDFPainterHelper::applyPenToGraphicState(&newState, pen);
PDFPainterHelper::applyBrushToGraphicState(&newState, brush);
QTextStream stream(&m_outputContent, QDataStream::WriteOnly | QDataStream::Append); QTextStream stream(&m_outputContent, QDataStream::WriteOnly | QDataStream::Append);
writeStateDifference(stream, newState); writeStateDifference(stream, newState);

View File

@ -31,8 +31,15 @@ PDFPageContentEditorProcessor::PDFPageContentEditorProcessor(const PDFPage* page
{ {
m_clippingPaths.push(QPainterPath()); m_clippingPaths.push(QPainterPath());
m_content.setFontDictionary(*getFontDictionary()); if (auto fontDictionary = getFontDictionary())
m_content.setXObjectDictionary(*getXObjectDictionary()); {
m_content.setFontDictionary(*fontDictionary);
}
if (auto xObjectDictionary = getXObjectDictionary())
{
m_content.setXObjectDictionary(*xObjectDictionary);
}
} }
const PDFEditedPageContent& PDFPageContentEditorProcessor::getEditedPageContent() const 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) 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) 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<PDFEditedPageContentElement> element) void PDFEditedPageContent::addContentElement(std::unique_ptr<PDFEditedPageContentElement> element)

View File

@ -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<PDFAbstractColorSpace>(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<PDFAbstractColorSpace>(new PDFDeviceRGBColorSpace()));
}
graphicState->setFillColor(color, PDFColor(color.redF(), color.greenF(), color.blueF()));
}
}
PDFTransformationDecomposition PDFPainterHelper::decomposeTransform(const QTransform& transform) PDFTransformationDecomposition PDFPainterHelper::decomposeTransform(const QTransform& transform)
{ {
PDFTransformationDecomposition result; PDFTransformationDecomposition result;

View File

@ -73,6 +73,9 @@ public:
/// Creates brush from painter graphicState /// Creates brush from painter graphicState
static QBrush createBrushFromState(const PDFPageContentProcessorState* graphicState, double alpha); 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 /// Decompose transform
static PDFTransformationDecomposition decomposeTransform(const QTransform& transform); static PDFTransformationDecomposition decomposeTransform(const QTransform& transform);

View File

@ -2618,8 +2618,10 @@ void PDFPageContentElementEdited::drawPage(QPainter* painter,
if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath()) if (const PDFEditedPageContentElementPath* pathElement = m_element->asPath())
{ {
const PDFPageContentProcessorState& state = m_element->getState(); const PDFPageContentProcessorState& state = m_element->getState();
painter->setPen(pdf::PDFPainterHelper::createPenFromState(&state, state.getAlphaStroking())); QPen pen = pdf::PDFPainterHelper::createPenFromState(&state, state.getAlphaStroking());
painter->setBrush(pdf::PDFPainterHelper::createBrushFromState(&state, state.getAlphaFilling())); 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()); painter->drawPath(pathElement->getPath());
} }