mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-02-09 16:28:46 +01:00
183 lines
5.8 KiB
C++
183 lines
5.8 KiB
C++
// 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/>.
|
|
|
|
#include "pdfrenderer.h"
|
|
#include "pdfdocument.h"
|
|
|
|
namespace pdf
|
|
{
|
|
|
|
// Graphic state operators - mapping from PDF name to the enum, splitted into groups.
|
|
// Please see Table 4.1 in PDF Reference 1.7, chapter 4.1 - Graphic Objects.
|
|
//
|
|
// General graphic state: w, J, j, M, d, ri, i, gs
|
|
// Special graphic state: q, Q, cm
|
|
// Path construction: m, l, c, v, y, h, re
|
|
// Path painting: S, s, F, f, f*, B, B*, b, b*, n
|
|
// Clipping paths: W, W*
|
|
// Text object: BT, ET
|
|
// Text state: Tc, Tw, Tz, TL, Tf, Tr, Ts
|
|
// Text positioning: Td, TD, Tm, T*
|
|
// Text showing: Tj, TJ, ', "
|
|
// Type 3 font: d0, d1
|
|
// Color: CS, cs, SC, SCN, sc, scn, G, g, RG, rg, K, k
|
|
// Shading pattern: sh
|
|
// Inline images: BI, ID, EI
|
|
// XObject: Do
|
|
// Marked content: MP, DP, BMC, BDC, EMC
|
|
// Compatibility: BX, EX
|
|
|
|
|
|
PDFRenderer::PDFRenderer(const PDFDocument* document) :
|
|
m_document(document),
|
|
m_features(Antialasing | TextAntialiasing)
|
|
{
|
|
Q_ASSERT(document);
|
|
}
|
|
|
|
QList<PDFRenderError> PDFRenderer::render(QPainter* painter, const QRectF& rectangle, size_t pageIndex) const
|
|
{
|
|
Q_UNUSED(painter);
|
|
Q_UNUSED(rectangle);
|
|
|
|
const PDFCatalog* catalog = m_document->getCatalog();
|
|
if (pageIndex >= catalog->getPageCount() || !catalog->getPage(pageIndex))
|
|
{
|
|
// Invalid page index
|
|
return { PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Page %1 doesn't exist.").arg(pageIndex + 1)) };
|
|
}
|
|
|
|
const PDFPage* page = catalog->getPage(pageIndex);
|
|
Q_ASSERT(page);
|
|
|
|
PDFPageContentProcessor processor(page, m_document);
|
|
return processor.processContents();
|
|
}
|
|
|
|
PDFPageContentProcessor::PDFPageContentProcessor(const PDFPage* page, const PDFDocument* document) :
|
|
m_page(page),
|
|
m_document(document)
|
|
{
|
|
Q_ASSERT(page);
|
|
Q_ASSERT(document);
|
|
}
|
|
|
|
QList<PDFRenderError> PDFPageContentProcessor::processContents()
|
|
{
|
|
const PDFObject& contents = m_page->getContents();
|
|
|
|
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()));
|
|
}
|
|
else
|
|
{
|
|
errors.append(PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")));
|
|
}
|
|
}
|
|
|
|
return std::move(errors);
|
|
}
|
|
else if (contents.isStream())
|
|
{
|
|
return processContentStream(contents.getStream());
|
|
}
|
|
else
|
|
{
|
|
return { PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Invalid page contents.")) };
|
|
}
|
|
}
|
|
|
|
QList<PDFRenderError> PDFPageContentProcessor::processContentStream(const PDFStream* stream)
|
|
{
|
|
QByteArray content = m_document->getDecodedStream(stream);
|
|
|
|
PDFLexicalAnalyzer parser(content.constBegin(), content.constEnd());
|
|
|
|
QList<PDFRenderError> errors;
|
|
while (!parser.isAtEnd())
|
|
{
|
|
try
|
|
{
|
|
PDFLexicalAnalyzer::Token token = parser.fetch();
|
|
switch (token.type)
|
|
{
|
|
case PDFLexicalAnalyzer::TokenType::Command:
|
|
{
|
|
// Process the command, then clear the operand stack
|
|
processCommand(token.data.toByteArray(), errors);
|
|
m_operands.clear();
|
|
break;
|
|
}
|
|
|
|
case PDFLexicalAnalyzer::TokenType::EndOfFile:
|
|
{
|
|
// Do nothing, just break, we are at the end
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
// Push the operand onto the operand stack
|
|
m_operands.push_back(std::move(token));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (PDFParserException exception)
|
|
{
|
|
errors.append(PDFRenderError(RenderErrorType::Error, exception.getMessage()));
|
|
}
|
|
}
|
|
|
|
return errors;
|
|
}
|
|
|
|
PDFPageContentProcessor::PDFPageContentProcessorState::PDFPageContentProcessorState() :
|
|
m_currentTransformationMatrix(),
|
|
m_fillColorSpace(),
|
|
m_strokeColorSpace(),
|
|
m_fillColor(Qt::black),
|
|
m_strokeColor(Qt::black),
|
|
m_lineWidth(1.0),
|
|
m_lineCapStyle(Qt::FlatCap),
|
|
m_lineJoinStyle(Qt::MiterJoin),
|
|
m_mitterLimit(10.0),
|
|
m_renderingIntent(),
|
|
m_flatness(1.0),
|
|
m_smoothness(0.01)
|
|
{
|
|
m_fillColorSpace.reset(new PDFDeviceGrayColorSpace);
|
|
m_strokeColorSpace = m_fillColorSpace;
|
|
}
|
|
|
|
PDFPageContentProcessor::PDFPageContentProcessorState::~PDFPageContentProcessorState()
|
|
{
|
|
|
|
}
|
|
|
|
} // namespace pdf
|