mirror of https://github.com/JakubMelka/PDF4QT.git
Path constructing operands
This commit is contained in:
parent
b01a135dfd
commit
648879eadf
|
@ -22,3 +22,5 @@ SUBDIRS += \
|
|||
UnitTests \
|
||||
PdfForQtViewer
|
||||
|
||||
UnitTests.depends = PdfForQtLib
|
||||
PdfForQtViewer.depends = PdfForQtLib
|
||||
|
|
|
@ -70,7 +70,8 @@ HEADERS += \
|
|||
sources/pdfdrawwidget.h \
|
||||
sources/pdfflatarray.h \
|
||||
sources/pdfcolorspaces.h \
|
||||
sources/pdfrenderer.h
|
||||
sources/pdfrenderer.h \
|
||||
sources/pdfrenderer_impl.h
|
||||
|
||||
unix {
|
||||
target.path = /usr/lib
|
||||
|
|
|
@ -533,6 +533,17 @@ QByteArray PDFLexicalAnalyzer::fetchByteArray(PDFInteger length)
|
|||
return result;
|
||||
}
|
||||
|
||||
QString PDFLexicalAnalyzer::getStringFromOperandType(TokenType type)
|
||||
{
|
||||
QMetaEnum metaEnum = QMetaEnum::fromType<TokenType>();
|
||||
Q_ASSERT(metaEnum.isValid());
|
||||
|
||||
const char* typeName = metaEnum.valueToKey(static_cast<int>(type));
|
||||
Q_ASSERT(typeName);
|
||||
|
||||
return typeName;
|
||||
}
|
||||
|
||||
bool PDFLexicalAnalyzer::fetchChar(const char character)
|
||||
{
|
||||
if (!isAtEnd() && lookChar() == character)
|
||||
|
|
|
@ -170,6 +170,10 @@ public:
|
|||
/// \param character Character to be tested
|
||||
static constexpr bool isRegular(char character) { return !isWhitespace(character) && !isDelimiter(character); }
|
||||
|
||||
/// Returns string from operand type
|
||||
/// \param type Token type
|
||||
static QString getStringFromOperandType(TokenType type);
|
||||
|
||||
private:
|
||||
inline char lookChar() const { Q_ASSERT(m_current != m_end); return *m_current; }
|
||||
|
||||
|
|
|
@ -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() :
|
||||
|
|
|
@ -19,13 +19,8 @@
|
|||
#define PDFRENDERER_H
|
||||
|
||||
#include "pdfpage.h"
|
||||
#include "pdfparser.h"
|
||||
#include "pdfcolorspaces.h"
|
||||
|
||||
#include <QMatrix>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <stack>
|
||||
class QPainter;
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
@ -78,161 +73,6 @@ private:
|
|||
Features m_features;
|
||||
};
|
||||
|
||||
/// Process the contents of the page.
|
||||
class PDFPageContentProcessor
|
||||
{
|
||||
public:
|
||||
explicit PDFPageContentProcessor(const PDFPage* page, const PDFDocument* document);
|
||||
|
||||
enum class Operator
|
||||
{
|
||||
// General graphic state w, J, j, M, d, ri, i, gs
|
||||
SetLineWidth, ///< w, sets the line width
|
||||
SetLineCap, ///< J, sets the line cap
|
||||
SetLineJoin, ///< j, sets the line join
|
||||
SetMitterLimit, ///< M, sets the mitter limit
|
||||
SetLineDashPattern, ///< d, sets the line dash pattern
|
||||
SetRenderingIntent, ///< ri, sets the rendering intent
|
||||
SetFlatness, ///< i, sets the flattness (number in range from 0 to 100)
|
||||
SetGraphicState, ///< gs, sets the whole graphic state (stored in resource dictionary)
|
||||
|
||||
// Special graphic state: q, Q, cm
|
||||
SaveGraphicState, ///< q, saves the graphic state
|
||||
RestoreGraphicState, ///< Q, restores the graphic state
|
||||
AdjustCurrentTransformationMatrix, ///< cm, modify the current transformation matrix by matrix multiplication
|
||||
|
||||
// Path construction: m, l, c, v, y, h, re
|
||||
MoveCurrentPoint, ///< m, begin a new subpath by moving to the desired point
|
||||
LineTo, ///< l, appends a straight line segment to the subpath
|
||||
Bezier123To, ///< c, appends a Bézier curve with control points 1, 2, 3
|
||||
Bezier23To, ///< v, appends a Bézier curve with control points 2, 3
|
||||
Bezier13To, ///< y, appends a Bézier curve with control points 1, 3
|
||||
EndSubpath, ///< h, ends current subpath by adding straight line segment from the last point to the beginning
|
||||
|
||||
// Path painting: S, s, f, F, f*, B, B*, b, b*, n
|
||||
StrokePath, ///< S, stroke the path
|
||||
CloseAndStrokePath, ///< s, close the path and then stroke (equivalent of operators h S)
|
||||
FillPathWinding, ///< f, close the path, and then fill the path using "Non zero winding number rule"
|
||||
FillPathWinding2, ///< F, same as previous, see PDF Reference 1.7, Table 4.10
|
||||
FillPathEvenOdd, ///< f*, fill the path using "Even-odd rule"
|
||||
StrokeAndFillWinding, ///< B, stroke and fill path, using "Non zero winding number rule"
|
||||
StrokeAndFillEvenOdd, ///< B*, stroke and fill path, using "Even-odd rule"
|
||||
CloseAndStrokeAndFillWinding, ///< b, close, stroke and fill path, using "Non zero winding number rule", equivalent of operators h B
|
||||
CloseAndStrokeAndFillEvenOdd, ///< b*, close, stroke and fill path, using "Even-odd rule", equivalent of operators h B*
|
||||
ClearPath, ///< n, clear parh (close current) path, "no-operation", used with clipping
|
||||
|
||||
// Clipping paths: W, W*
|
||||
ClipWinding, ///< W, modify current clipping path by intersecting it with current path using "Non zero winding number rule"
|
||||
ClipEvenOdd, ///< W*, modify current clipping path by intersecting it with current path using "Even-odd rule"
|
||||
|
||||
// Text object: BT, ET
|
||||
TextBegin, ///< BT, begin text object, initialize text matrices, cannot be nested
|
||||
TextEnd, ///< ET, end text object, cannot be nested
|
||||
|
||||
// Text state: Tc, Tw, Tz, TL, Tf, Tr, Ts
|
||||
TextSetCharacterSpacing, ///< Tc, set text character spacing
|
||||
TextSetWordSpacing, ///< Tw, set text word spacing
|
||||
TextSetHorizontalScale, ///< Tz, set text horizontal scaling (in percents, 100% = normal scaling)
|
||||
TextSetLeading, ///< TL, set text leading
|
||||
TextSetFontAndFontSize, ///< Tf, set text font (name from dictionary) and its size
|
||||
TextSetRenderMode, ///< Tr, set text render mode
|
||||
TextSetRise, ///< Ts, set text rise
|
||||
|
||||
// Text positioning: Td, TD, Tm, T*
|
||||
TextMoveByOffset, ///< Td, move by offset
|
||||
TextSetLeadingAndMoveByOffset, ///< TD, sets thext leading and moves by offset, x y TD is equivalent to sequence -y TL x y Td
|
||||
TextSetMatrix, ///< Tm, set text matrix
|
||||
TextMoveByLeading, ///< T*, moves text by leading, equivalent to 0 leading Td
|
||||
|
||||
// Text showing: Tj, TJ, ', "
|
||||
TextShowTextString, ///< Tj, show text string
|
||||
TextShowTextIndividualSpacing, ///< TJ, show text, allow individual text spacing
|
||||
TextNextLineShowText, ///< ', move to the next line and show text ("string '" is equivalent to "T* string Tj")
|
||||
TextSetSpacingAndShowText, ///< ", move to the next line, set spacing and show text (equivalent to sequence "w1 Tw w2 Tc string '")
|
||||
|
||||
// Type 3 font: d0, d1
|
||||
Type3FontSetOffset, ///< d0, set width information, see PDF 1.7 Reference, Table 5.10
|
||||
Type3FontSetOffsetAndBB, ///< d1, set offset and glyph bounding box
|
||||
|
||||
// Color: CS, cs, SC, SCN, sc, scn, G, g, RG, rg, K, k
|
||||
ColorSetStrokingColorSpace, ///< CS, set current color space for stroking operations
|
||||
ColorSetFillingColorSpace, ///< cs, set current color space for filling operations
|
||||
ColorSetStrokingColor, ///< SC, set current stroking color
|
||||
ColorSetStrokingColorN, ///< SCN, same as SC, but also supports Pattern, Separtion, DeviceN and ICCBased color spaces
|
||||
ColorSetFillingColor, ///< sc, set current filling color
|
||||
ColorSetFillingColorN, ///< scn, same as sc, but also supports Pattern, Separtion, DeviceN and ICCBased color spaces
|
||||
ColorSetDeviceGrayStroking, ///< G, set DeviceGray color space for stroking color and set color
|
||||
ColorSetDeviceGrayFilling, ///< g, set DeviceGray color space for filling color and set color
|
||||
ColorSetDeviceRGBStroking, ///< RG, set DeviceRGB color space for stroking color and set color
|
||||
ColorSetDeviceRGBFilling, ///< rg, set DeviceRGB color space for filling color and set color
|
||||
ColorSetDeviceCMYKStroking, ///< K, set DeviceCMYK color space for stroking color and set color
|
||||
ColorSetDeviceCMYKFilling, ///< k, set DeviceCMYK color space for filling color and set color
|
||||
|
||||
// Shading pattern: sh
|
||||
ShadingPaintShape, ///< sh, paint shape
|
||||
|
||||
// Inline images: BI, ID, EI
|
||||
InlineImageBegin, ///< BI, begin inline image
|
||||
InlineImageData, ///< ID, inline image data
|
||||
InlineImageEnd, ///< EI, end of inline image
|
||||
|
||||
// XObject: Do
|
||||
PaintXObject, ///< Do, paint the X Object (image, form, ...)
|
||||
|
||||
// Marked content: MP, DP, BMC, BDC, EMC
|
||||
MarkedContentPoint, ///< MP, marked content point
|
||||
MarkedContentPointWithProperties, ///< DP, marked content point with properties
|
||||
MarkedContentBegin, ///< BMC, begin of sequence of marked content
|
||||
MarkedContentBeginWithProperties, ///< BDC, begin of sequence of marked content with properties
|
||||
MarkedContentEnd, ///< EMC, end of marked content sequence
|
||||
|
||||
// Compatibility: BX, EX
|
||||
CompatibilityBegin, ///< BX, Compatibility mode begin (unrecognized operators are ignored)
|
||||
CompatibilityEnd, ///< EX, Compatibility mode end
|
||||
Invalid ///< Invalid operator, use for error reporting
|
||||
};
|
||||
|
||||
/// Process the contents of the page
|
||||
QList<PDFRenderError> processContents();
|
||||
|
||||
protected:
|
||||
/// Process the content stream
|
||||
QList<PDFRenderError> processContentStream(const PDFStream* stream);
|
||||
|
||||
/// Represents graphic state of the PDF (holding current graphic state parameters).
|
||||
/// Please see PDF Reference 1.7, Chapter 4.3 "Graphic State"
|
||||
class PDFPageContentProcessorState
|
||||
{
|
||||
public:
|
||||
explicit PDFPageContentProcessorState();
|
||||
~PDFPageContentProcessorState();
|
||||
|
||||
private:
|
||||
QMatrix m_currentTransformationMatrix;
|
||||
QSharedPointer<PDFAbstractColorSpace> m_fillColorSpace;
|
||||
QSharedPointer<PDFAbstractColorSpace> m_strokeColorSpace;
|
||||
QColor m_fillColor;
|
||||
QColor m_strokeColor;
|
||||
PDFReal m_lineWidth;
|
||||
Qt::PenCapStyle m_lineCapStyle;
|
||||
Qt::PenJoinStyle m_lineJoinStyle;
|
||||
PDFReal m_mitterLimit;
|
||||
QByteArray m_renderingIntent;
|
||||
PDFReal m_flatness;
|
||||
PDFReal m_smoothness;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
const PDFPage* m_page;
|
||||
const PDFDocument* m_document;
|
||||
|
||||
/// Array with current operand arguments
|
||||
PDFFlatArray<PDFLexicalAnalyzer::Token, 33> m_operands;
|
||||
|
||||
/// Stack with current graphic states
|
||||
std::stack<PDFPageContentProcessorState> m_stack;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
|
|
|
@ -0,0 +1,255 @@
|
|||
// Copyright (C) 2019 Jakub Melka
|
||||
//
|
||||
// This file is part of PdfForQt.
|
||||
//
|
||||
// PdfForQt is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// PdfForQt is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef PDFRENDERER_IMPL_H
|
||||
#define PDFRENDERER_IMPL_H
|
||||
|
||||
#include "pdfrenderer.h"
|
||||
#include "pdfparser.h"
|
||||
#include "pdfcolorspaces.h"
|
||||
|
||||
#include <QMatrix>
|
||||
#include <QPainterPath>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <stack>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
class PDFRendererException : public std::exception
|
||||
{
|
||||
public:
|
||||
explicit PDFRendererException(RenderErrorType type, QString message) :
|
||||
m_error(type, std::move(message))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const PDFRenderError& getError() const { return m_error; }
|
||||
|
||||
private:
|
||||
PDFRenderError m_error;
|
||||
};
|
||||
|
||||
/// Process the contents of the page.
|
||||
class PDFPageContentProcessor
|
||||
{
|
||||
public:
|
||||
explicit PDFPageContentProcessor(const PDFPage* page, const PDFDocument* document);
|
||||
|
||||
enum class Operator
|
||||
{
|
||||
// General graphic state w, J, j, M, d, ri, i, gs
|
||||
SetLineWidth, ///< w, sets the line width
|
||||
SetLineCap, ///< J, sets the line cap
|
||||
SetLineJoin, ///< j, sets the line join
|
||||
SetMitterLimit, ///< M, sets the mitter limit
|
||||
SetLineDashPattern, ///< d, sets the line dash pattern
|
||||
SetRenderingIntent, ///< ri, sets the rendering intent
|
||||
SetFlatness, ///< i, sets the flattness (number in range from 0 to 100)
|
||||
SetGraphicState, ///< gs, sets the whole graphic state (stored in resource dictionary)
|
||||
|
||||
// Special graphic state: q, Q, cm
|
||||
SaveGraphicState, ///< q, saves the graphic state
|
||||
RestoreGraphicState, ///< Q, restores the graphic state
|
||||
AdjustCurrentTransformationMatrix, ///< cm, modify the current transformation matrix by matrix multiplication
|
||||
|
||||
// Path construction: m, l, c, v, y, h, re
|
||||
MoveCurrentPoint, ///< m, begin a new subpath by moving to the desired point
|
||||
LineTo, ///< l, appends a straight line segment to the subpath
|
||||
Bezier123To, ///< c, appends a Bézier curve with control points 1, 2, 3
|
||||
Bezier23To, ///< v, appends a Bézier curve with control points 2, 3
|
||||
Bezier13To, ///< y, appends a Bézier curve with control points 1, 3
|
||||
EndSubpath, ///< h, ends current subpath by adding straight line segment from the last point to the beginning
|
||||
Rectangle, ///< re, adds rectangle
|
||||
|
||||
// Path painting: S, s, f, F, f*, B, B*, b, b*, n
|
||||
StrokePath, ///< S, stroke the path
|
||||
CloseAndStrokePath, ///< s, close the path and then stroke (equivalent of operators h S)
|
||||
FillPathWinding, ///< f, close the path, and then fill the path using "Non zero winding number rule"
|
||||
FillPathWinding2, ///< F, same as previous, see PDF Reference 1.7, Table 4.10
|
||||
FillPathEvenOdd, ///< f*, fill the path using "Even-odd rule"
|
||||
StrokeAndFillWinding, ///< B, stroke and fill path, using "Non zero winding number rule"
|
||||
StrokeAndFillEvenOdd, ///< B*, stroke and fill path, using "Even-odd rule"
|
||||
CloseAndStrokeAndFillWinding, ///< b, close, stroke and fill path, using "Non zero winding number rule", equivalent of operators h B
|
||||
CloseAndStrokeAndFillEvenOdd, ///< b*, close, stroke and fill path, using "Even-odd rule", equivalent of operators h B*
|
||||
ClearPath, ///< n, clear parh (close current) path, "no-operation", used with clipping
|
||||
|
||||
// Clipping paths: W, W*
|
||||
ClipWinding, ///< W, modify current clipping path by intersecting it with current path using "Non zero winding number rule"
|
||||
ClipEvenOdd, ///< W*, modify current clipping path by intersecting it with current path using "Even-odd rule"
|
||||
|
||||
// Text object: BT, ET
|
||||
TextBegin, ///< BT, begin text object, initialize text matrices, cannot be nested
|
||||
TextEnd, ///< ET, end text object, cannot be nested
|
||||
|
||||
// Text state: Tc, Tw, Tz, TL, Tf, Tr, Ts
|
||||
TextSetCharacterSpacing, ///< Tc, set text character spacing
|
||||
TextSetWordSpacing, ///< Tw, set text word spacing
|
||||
TextSetHorizontalScale, ///< Tz, set text horizontal scaling (in percents, 100% = normal scaling)
|
||||
TextSetLeading, ///< TL, set text leading
|
||||
TextSetFontAndFontSize, ///< Tf, set text font (name from dictionary) and its size
|
||||
TextSetRenderMode, ///< Tr, set text render mode
|
||||
TextSetRise, ///< Ts, set text rise
|
||||
|
||||
// Text positioning: Td, TD, Tm, T*
|
||||
TextMoveByOffset, ///< Td, move by offset
|
||||
TextSetLeadingAndMoveByOffset, ///< TD, sets thext leading and moves by offset, x y TD is equivalent to sequence -y TL x y Td
|
||||
TextSetMatrix, ///< Tm, set text matrix
|
||||
TextMoveByLeading, ///< T*, moves text by leading, equivalent to 0 leading Td
|
||||
|
||||
// Text showing: Tj, TJ, ', "
|
||||
TextShowTextString, ///< Tj, show text string
|
||||
TextShowTextIndividualSpacing, ///< TJ, show text, allow individual text spacing
|
||||
TextNextLineShowText, ///< ', move to the next line and show text ("string '" is equivalent to "T* string Tj")
|
||||
TextSetSpacingAndShowText, ///< ", move to the next line, set spacing and show text (equivalent to sequence "w1 Tw w2 Tc string '")
|
||||
|
||||
// Type 3 font: d0, d1
|
||||
Type3FontSetOffset, ///< d0, set width information, see PDF 1.7 Reference, Table 5.10
|
||||
Type3FontSetOffsetAndBB, ///< d1, set offset and glyph bounding box
|
||||
|
||||
// Color: CS, cs, SC, SCN, sc, scn, G, g, RG, rg, K, k
|
||||
ColorSetStrokingColorSpace, ///< CS, set current color space for stroking operations
|
||||
ColorSetFillingColorSpace, ///< cs, set current color space for filling operations
|
||||
ColorSetStrokingColor, ///< SC, set current stroking color
|
||||
ColorSetStrokingColorN, ///< SCN, same as SC, but also supports Pattern, Separtion, DeviceN and ICCBased color spaces
|
||||
ColorSetFillingColor, ///< sc, set current filling color
|
||||
ColorSetFillingColorN, ///< scn, same as sc, but also supports Pattern, Separtion, DeviceN and ICCBased color spaces
|
||||
ColorSetDeviceGrayStroking, ///< G, set DeviceGray color space for stroking color and set color
|
||||
ColorSetDeviceGrayFilling, ///< g, set DeviceGray color space for filling color and set color
|
||||
ColorSetDeviceRGBStroking, ///< RG, set DeviceRGB color space for stroking color and set color
|
||||
ColorSetDeviceRGBFilling, ///< rg, set DeviceRGB color space for filling color and set color
|
||||
ColorSetDeviceCMYKStroking, ///< K, set DeviceCMYK color space for stroking color and set color
|
||||
ColorSetDeviceCMYKFilling, ///< k, set DeviceCMYK color space for filling color and set color
|
||||
|
||||
// Shading pattern: sh
|
||||
ShadingPaintShape, ///< sh, paint shape
|
||||
|
||||
// Inline images: BI, ID, EI
|
||||
InlineImageBegin, ///< BI, begin inline image
|
||||
InlineImageData, ///< ID, inline image data
|
||||
InlineImageEnd, ///< EI, end of inline image
|
||||
|
||||
// XObject: Do
|
||||
PaintXObject, ///< Do, paint the X Object (image, form, ...)
|
||||
|
||||
// Marked content: MP, DP, BMC, BDC, EMC
|
||||
MarkedContentPoint, ///< MP, marked content point
|
||||
MarkedContentPointWithProperties, ///< DP, marked content point with properties
|
||||
MarkedContentBegin, ///< BMC, begin of sequence of marked content
|
||||
MarkedContentBeginWithProperties, ///< BDC, begin of sequence of marked content with properties
|
||||
MarkedContentEnd, ///< EMC, end of marked content sequence
|
||||
|
||||
// Compatibility: BX, EX
|
||||
CompatibilityBegin, ///< BX, Compatibility mode begin (unrecognized operators are ignored)
|
||||
CompatibilityEnd, ///< EX, Compatibility mode end
|
||||
Invalid ///< Invalid operator, use for error reporting
|
||||
};
|
||||
|
||||
/// Process the contents of the page
|
||||
QList<PDFRenderError> processContents();
|
||||
|
||||
private:
|
||||
/// Process the content stream
|
||||
void processContentStream(const PDFStream* stream);
|
||||
|
||||
/// Processes single command
|
||||
void processCommand(const QByteArray& command);
|
||||
|
||||
template<typename T>
|
||||
T readOperand(size_t index) const;
|
||||
|
||||
template<>
|
||||
PDFReal readOperand<PDFReal>(size_t index) const;
|
||||
|
||||
template<size_t index, typename T>
|
||||
inline T readOperand() const { return readOperand<T>(index); }
|
||||
|
||||
template<typename Tuple, class F, std::size_t... I>
|
||||
inline void invokeOperatorImpl(F function, std::index_sequence<I...>)
|
||||
{
|
||||
(this->*function)(readOperand<I, typename std::tuple_element<I, Tuple>::type>()...);
|
||||
}
|
||||
|
||||
/// Function invokes operator (function) with given arguments. For that reason, variadic
|
||||
/// templates are used. Basically, for each argument of the function, we need type of the argument,
|
||||
/// and its index. To retrieve it, we use std::tuple, variadic template and functionality
|
||||
/// analogic to std::apply implementation.
|
||||
template<typename... Operands>
|
||||
inline void invokeOperator(void(PDFPageContentProcessor::* function)(Operands...))
|
||||
{
|
||||
invokeOperatorImpl<std::tuple<Operands...>>(function, std::make_index_sequence<std::tuple_size_v<std::remove_reference_t<std::tuple<Operands...>>>>{});
|
||||
}
|
||||
|
||||
/// Returns the current poin in the path. If path doesn't exist, then
|
||||
/// exception is thrown.
|
||||
QPointF getCurrentPoint() const;
|
||||
|
||||
// Path construction operators
|
||||
void operatorMoveCurrentPoint(PDFReal x, PDFReal y);
|
||||
void operatorLineTo(PDFReal x, PDFReal y);
|
||||
void operatorBezier123To(PDFReal x1, PDFReal y1, PDFReal x2, PDFReal y2, PDFReal x3, PDFReal y3);
|
||||
void operatorBezier23To(PDFReal x2, PDFReal y2, PDFReal x3, PDFReal y3);
|
||||
void operatorBezier13To(PDFReal x1, PDFReal y1, PDFReal x3, PDFReal y3);
|
||||
void operatorEndSubpath();
|
||||
void operatorRectangle(PDFReal x, PDFReal y, PDFReal width, PDFReal height);
|
||||
|
||||
/// Represents graphic state of the PDF (holding current graphic state parameters).
|
||||
/// Please see PDF Reference 1.7, Chapter 4.3 "Graphic State"
|
||||
class PDFPageContentProcessorState
|
||||
{
|
||||
public:
|
||||
explicit PDFPageContentProcessorState();
|
||||
~PDFPageContentProcessorState();
|
||||
|
||||
private:
|
||||
QMatrix m_currentTransformationMatrix;
|
||||
QSharedPointer<PDFAbstractColorSpace> m_fillColorSpace;
|
||||
QSharedPointer<PDFAbstractColorSpace> m_strokeColorSpace;
|
||||
QColor m_fillColor;
|
||||
QColor m_strokeColor;
|
||||
PDFReal m_lineWidth;
|
||||
Qt::PenCapStyle m_lineCapStyle;
|
||||
Qt::PenJoinStyle m_lineJoinStyle;
|
||||
PDFReal m_mitterLimit;
|
||||
QByteArray m_renderingIntent;
|
||||
PDFReal m_flatness;
|
||||
PDFReal m_smoothness;
|
||||
};
|
||||
|
||||
const PDFPage* m_page;
|
||||
const PDFDocument* m_document;
|
||||
|
||||
/// Array with current operand arguments
|
||||
PDFFlatArray<PDFLexicalAnalyzer::Token, 33> m_operands;
|
||||
|
||||
/// Stack with current graphic states
|
||||
std::stack<PDFPageContentProcessorState> m_stack;
|
||||
|
||||
/// List of errors
|
||||
QList<PDFRenderError> m_errorList;
|
||||
|
||||
/// Current painter path
|
||||
QPainterPath m_currentPath;
|
||||
};
|
||||
|
||||
} // namespace pdf
|
||||
|
||||
#endif // PDFRENDERER_IMPL_H
|
Loading…
Reference in New Issue