Path constructing operands

This commit is contained in:
Jakub Melka
2019-02-14 19:45:07 +01:00
parent b01a135dfd
commit 648879eadf
7 changed files with 552 additions and 174 deletions

View File

@ -16,6 +16,7 @@
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#include "pdfrenderer.h"
#include "pdfrenderer_impl.h"
#include "pdfdocument.h"
namespace pdf
@ -41,6 +42,113 @@ namespace pdf
// Marked content: MP, DP, BMC, BDC, EMC
// Compatibility: BX, EX
static constexpr const std::pair<const char*, PDFPageContentProcessor::Operator> operators[] =
{
// General graphic state w, J, j, M, d, ri, i, gs
{ "w", PDFPageContentProcessor::Operator::SetLineWidth },
{ "J", PDFPageContentProcessor::Operator::SetLineCap },
{ "j", PDFPageContentProcessor::Operator::SetLineJoin },
{ "M", PDFPageContentProcessor::Operator::SetMitterLimit },
{ "d", PDFPageContentProcessor::Operator::SetLineDashPattern },
{ "ri", PDFPageContentProcessor::Operator::SetRenderingIntent },
{ "i", PDFPageContentProcessor::Operator::SetFlatness },
{ "gs", PDFPageContentProcessor::Operator::SetGraphicState },
// Special graphic state: q, Q, cm
{ "q", PDFPageContentProcessor::Operator::SaveGraphicState },
{ "Q", PDFPageContentProcessor::Operator::RestoreGraphicState },
{ "cm", PDFPageContentProcessor::Operator::AdjustCurrentTransformationMatrix },
// Path construction: m, l, c, v, y, h, re
{ "m", PDFPageContentProcessor::Operator::MoveCurrentPoint },
{ "l", PDFPageContentProcessor::Operator::LineTo },
{ "c", PDFPageContentProcessor::Operator::Bezier123To },
{ "v", PDFPageContentProcessor::Operator::Bezier23To },
{ "y", PDFPageContentProcessor::Operator::Bezier13To },
{ "h", PDFPageContentProcessor::Operator::EndSubpath },
{ "re", PDFPageContentProcessor::Operator::Rectangle },
// Path painting: S, s, f, F, f*, B, B*, b, b*, n
{ "S", PDFPageContentProcessor::Operator::StrokePath },
{ "s", PDFPageContentProcessor::Operator::CloseAndStrokePath },
{ "f", PDFPageContentProcessor::Operator::FillPathWinding },
{ "F", PDFPageContentProcessor::Operator::FillPathWinding2 },
{ "f*", PDFPageContentProcessor::Operator::FillPathEvenOdd },
{ "B", PDFPageContentProcessor::Operator::StrokeAndFillWinding },
{ "B*", PDFPageContentProcessor::Operator::StrokeAndFillEvenOdd },
{ "b", PDFPageContentProcessor::Operator::CloseAndStrokeAndFillWinding },
{ "b*", PDFPageContentProcessor::Operator::CloseAndStrokeAndFillEvenOdd },
{ "n", PDFPageContentProcessor::Operator::ClearPath },
// Clipping paths: W, W*
{ "W", PDFPageContentProcessor::Operator::ClipWinding },
{ "W*", PDFPageContentProcessor::Operator::ClipEvenOdd },
// Text object: BT, ET
{ "BT", PDFPageContentProcessor::Operator::TextBegin },
{ "ET", PDFPageContentProcessor::Operator::TextEnd },
// Text state: Tc, Tw, Tz, TL, Tf, Tr, Ts
{ "Tc", PDFPageContentProcessor::Operator::TextSetCharacterSpacing },
{ "Tw", PDFPageContentProcessor::Operator::TextSetWordSpacing },
{ "Tz", PDFPageContentProcessor::Operator::TextSetHorizontalScale },
{ "TL", PDFPageContentProcessor::Operator::TextSetLeading },
{ "Tf", PDFPageContentProcessor::Operator::TextSetFontAndFontSize },
{ "Tr", PDFPageContentProcessor::Operator::TextSetRenderMode },
{ "Ts", PDFPageContentProcessor::Operator::TextSetRise },
// Text positioning: Td, TD, Tm, T*
{ "Td", PDFPageContentProcessor::Operator::TextMoveByOffset },
{ "TD", PDFPageContentProcessor::Operator::TextSetLeadingAndMoveByOffset },
{ "Tm", PDFPageContentProcessor::Operator::TextSetMatrix },
{ "T*", PDFPageContentProcessor::Operator::TextMoveByLeading },
// Text showing: Tj, TJ, ', "
{ "Tj", PDFPageContentProcessor::Operator::TextShowTextString },
{ "TJ", PDFPageContentProcessor::Operator::TextShowTextIndividualSpacing },
{ "'", PDFPageContentProcessor::Operator::TextNextLineShowText },
{ "\"", PDFPageContentProcessor::Operator::TextSetSpacingAndShowText },
// Type 3 font: d0, d1
{ "d0", PDFPageContentProcessor::Operator::Type3FontSetOffset },
{ "d1", PDFPageContentProcessor::Operator::Type3FontSetOffsetAndBB },
// Color: CS, cs, SC, SCN, sc, scn, G, g, RG, rg, K, k
{ "CS", PDFPageContentProcessor::Operator::ColorSetStrokingColorSpace },
{ "cs", PDFPageContentProcessor::Operator::ColorSetFillingColorSpace },
{ "SC", PDFPageContentProcessor::Operator::ColorSetStrokingColor },
{ "SCN", PDFPageContentProcessor::Operator::ColorSetStrokingColorN },
{ "sc", PDFPageContentProcessor::Operator::ColorSetFillingColor },
{ "scn", PDFPageContentProcessor::Operator::ColorSetFillingColorN },
{ "G", PDFPageContentProcessor::Operator::ColorSetDeviceGrayStroking },
{ "g", PDFPageContentProcessor::Operator::ColorSetDeviceGrayFilling },
{ "RG", PDFPageContentProcessor::Operator::ColorSetDeviceRGBStroking },
{ "rg", PDFPageContentProcessor::Operator::ColorSetDeviceRGBFilling },
{ "K", PDFPageContentProcessor::Operator::ColorSetDeviceCMYKStroking },
{ "k", PDFPageContentProcessor::Operator::ColorSetDeviceCMYKFilling },
// Shading pattern: sh
{ "sh", PDFPageContentProcessor::Operator::ShadingPaintShape },
// Inline images: BI, ID, EI
{ "BI", PDFPageContentProcessor::Operator::InlineImageBegin },
{ "ID", PDFPageContentProcessor::Operator::InlineImageData },
{ "EI", PDFPageContentProcessor::Operator::InlineImageEnd },
// XObject: Do
{ "Do", PDFPageContentProcessor::Operator::PaintXObject },
// Marked content: MP, DP, BMC, BDC, EMC
{ "MP", PDFPageContentProcessor::Operator::MarkedContentPoint },
{ "DP", PDFPageContentProcessor::Operator::MarkedContentPointWithProperties },
{ "BMC", PDFPageContentProcessor::Operator::MarkedContentBegin },
{ "BDC", PDFPageContentProcessor::Operator::MarkedContentBeginWithProperties },
{ "EMC", PDFPageContentProcessor::Operator::MarkedContentEnd },
// Compatibility: BX, EX
{ "BX", PDFPageContentProcessor::Operator::CompatibilityBegin },
{ "EX", PDFPageContentProcessor::Operator::CompatibilityEnd }
};
PDFRenderer::PDFRenderer(const PDFDocument* document) :
m_document(document),
@ -80,44 +188,45 @@ QList<PDFRenderError> PDFPageContentProcessor::processContents()
{
const PDFObject& contents = m_page->getContents();
// Clear the old errors
m_errorList.clear();
if (contents.isArray())
{
const PDFArray* array = contents.getArray();
const size_t count = array->getCount();
QList<PDFRenderError> errors;
for (size_t i = 0; i < count; ++i)
{
const PDFObject& streamObject = m_document->getObject(array->getItem(i));
if (streamObject.isStream())
{
errors.append(processContentStream(streamObject.getStream()));
processContentStream(streamObject.getStream());
}
else
{
errors.append(PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")));
m_errorList.append(PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")));
}
}
return std::move(errors);
}
else if (contents.isStream())
{
return processContentStream(contents.getStream());
processContentStream(contents.getStream());
}
else
{
return { PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")) };
m_errorList.append(PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")));
}
return m_errorList;
}
QList<PDFRenderError> PDFPageContentProcessor::processContentStream(const PDFStream* stream)
void PDFPageContentProcessor::processContentStream(const PDFStream* stream)
{
QByteArray content = m_document->getDecodedStream(stream);
PDFLexicalAnalyzer parser(content.constBegin(), content.constEnd());
QList<PDFRenderError> errors;
while (!parser.isAtEnd())
{
try
@ -128,7 +237,7 @@ QList<PDFRenderError> PDFPageContentProcessor::processContentStream(const PDFStr
case PDFLexicalAnalyzer::TokenType::Command:
{
// Process the command, then clear the operand stack
processCommand(token.data.toByteArray(), errors);
processCommand(token.data.toByteArray());
m_operands.clear();
break;
}
@ -149,11 +258,167 @@ QList<PDFRenderError> PDFPageContentProcessor::processContentStream(const PDFStr
}
catch (PDFParserException exception)
{
errors.append(PDFRenderError(RenderErrorType::Error, exception.getMessage()));
m_errorList.append(PDFRenderError(RenderErrorType::Error, exception.getMessage()));
}
catch (PDFRendererException exception)
{
m_errorList.append(exception.getError());
}
}
}
void PDFPageContentProcessor::processCommand(const QByteArray& command)
{
Operator op = Operator::Invalid;
// Find the command in the command array
for (const std::pair<const char*, PDFPageContentProcessor::Operator>& operatorDescriptor : operators)
{
if (command == operatorDescriptor.first)
{
op = operatorDescriptor.second;
break;
}
}
return errors;
switch (op)
{
case Operator::MoveCurrentPoint:
{
invokeOperator(&PDFPageContentProcessor::operatorMoveCurrentPoint);
break;
}
case Operator::LineTo:
{
invokeOperator(&PDFPageContentProcessor::operatorLineTo);
break;
}
case Operator::Bezier123To:
{
invokeOperator(&PDFPageContentProcessor::operatorBezier123To);
break;
}
case Operator::Bezier23To:
{
invokeOperator(&PDFPageContentProcessor::operatorBezier23To);
break;
}
case Operator::Bezier13To:
{
invokeOperator(&PDFPageContentProcessor::operatorBezier13To);
break;
}
case Operator::EndSubpath:
{
// Call directly, no parameters involved
operatorEndSubpath();
break;
}
case Operator::Rectangle:
{
invokeOperator(&PDFPageContentProcessor::operatorRectangle);
break;
}
case Operator::Invalid:
{
m_errorList.append(PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Unknown operator '%1'.").arg(QString::fromLatin1(command))));
break;
}
default:
{
m_errorList.append(PDFRenderError(RenderErrorType::NotImplemented, PDFTranslationContext::tr("Not implemented operator '%1'.").arg(QString::fromLatin1(command))));
break;
}
}
}
QPointF PDFPageContentProcessor::getCurrentPoint() const
{
const int elementCount = m_currentPath.elementCount();
if (elementCount > 0)
{
QPainterPath::Element element = m_currentPath.elementAt(elementCount - 1);
return QPointF(element.x, element.y);
}
else
{
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Current point of path is not set. Path is empty."));
}
return QPointF();
}
template<>
PDFReal PDFPageContentProcessor::readOperand<PDFReal>(size_t index) const
{
if (m_operands.size() < index)
{
const PDFLexicalAnalyzer::Token& token = m_operands[index];
switch (token.type)
{
case PDFLexicalAnalyzer::TokenType::Real:
case PDFLexicalAnalyzer::TokenType::Integer:
return token.data.value<PDFReal>();
default:
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Can't read operand (real number) on index %1. Operand is of type '%2'.").arg(index + 1).arg(PDFLexicalAnalyzer::getStringFromOperandType(token.type)));
}
}
else
{
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Can't read operand (real number) on index %1. Only %2 operands provided.").arg(index + 1).arg(m_operands.size()));
}
return 0.0;
}
void PDFPageContentProcessor::operatorMoveCurrentPoint(PDFReal x, PDFReal y)
{
m_currentPath.moveTo(x, y);;
}
void PDFPageContentProcessor::operatorLineTo(PDFReal x, PDFReal y)
{
m_currentPath.lineTo(x, y);
}
void PDFPageContentProcessor::operatorBezier123To(PDFReal x1, PDFReal y1, PDFReal x2, PDFReal y2, PDFReal x3, PDFReal y3)
{
m_currentPath.cubicTo(x1, y1, x2, y2, x3, y3);
}
void PDFPageContentProcessor::operatorBezier23To(PDFReal x2, PDFReal y2, PDFReal x3, PDFReal y3)
{
QPointF currentPoint = getCurrentPoint();
m_currentPath.cubicTo(currentPoint.x(), currentPoint.y(), x2, y2, x3, y3);
}
void PDFPageContentProcessor::operatorBezier13To(PDFReal x1, PDFReal y1, PDFReal x3, PDFReal y3)
{
m_currentPath.cubicTo(x1, y1, x3, y3, x3, y3);
}
void PDFPageContentProcessor::operatorEndSubpath()
{
m_currentPath.closeSubpath();
}
void PDFPageContentProcessor::operatorRectangle(PDFReal x, PDFReal y, PDFReal width, PDFReal height)
{
if (width < 0 || height < 0)
{
throw PDFRendererException(RenderErrorType::Error, PDFTranslationContext::tr("Invalid size of rectangle (%1 x %2).").arg(width).arg(height));
}
m_currentPath.addRect(QRectF(x, y, width, height));
}
PDFPageContentProcessor::PDFPageContentProcessorState::PDFPageContentProcessorState() :