PDF4QT/PdfForQtLib/sources/pdfrenderer.cpp

114 lines
3.9 KiB
C++
Raw Normal View History

2019-02-09 18:40:56 +01:00
// 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"
2019-02-24 17:48:37 +01:00
#include "pdfpainter.h"
2019-02-09 18:40:56 +01:00
#include "pdfdocument.h"
namespace pdf
{
2019-07-06 15:55:37 +02:00
PDFRenderer::PDFRenderer(const PDFDocument* document, const PDFFontCache* fontCache, const PDFOptionalContentActivity* optionalContentActivity, Features features) :
2019-02-09 18:40:56 +01:00
m_document(document),
2019-04-12 19:17:19 +02:00
m_fontCache(fontCache),
m_optionalContentActivity(optionalContentActivity),
2019-07-06 15:55:37 +02:00
m_features(features)
2019-02-09 18:40:56 +01:00
{
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);
2019-08-25 18:16:37 +02:00
QRectF mediaBox = page->getRotatedMediaBox();
2019-02-16 18:26:16 +01:00
2019-02-24 17:48:37 +01:00
QMatrix matrix;
2019-08-25 18:16:37 +02:00
switch (page->getPageRotation())
2019-07-24 19:15:03 +02:00
{
case PageRotation::None:
{
matrix.translate(rectangle.left(), rectangle.bottom());
matrix.scale(rectangle.width() / mediaBox.width(), -rectangle.height() / mediaBox.height());
break;
}
case PageRotation::Rotate90:
{
matrix.translate(rectangle.left(), rectangle.top());
matrix.rotate(90);
matrix.scale(rectangle.width() / mediaBox.width(), -rectangle.height() / mediaBox.height());
break;
}
case PageRotation::Rotate270:
{
matrix.translate(rectangle.right(), rectangle.top());
matrix.rotate(-90);
matrix.scale(rectangle.width() / mediaBox.width(), -rectangle.height() / mediaBox.height());
break;
}
case PageRotation::Rotate180:
{
matrix.translate(rectangle.left(), rectangle.top());
matrix.scale(rectangle.width() / mediaBox.width(), rectangle.height() / mediaBox.height());
break;
}
default:
{
Q_ASSERT(false);
break;
}
}
2019-02-16 18:26:16 +01:00
PDFPainter processor(painter, m_features, matrix, page, m_document, m_fontCache, m_optionalContentActivity);
2019-02-24 17:48:37 +01:00
return processor.processContents();
2019-02-17 18:01:22 +01:00
}
2019-02-24 17:48:37 +01:00
QList<PDFRenderError> PDFRenderer::render(QPainter* painter, const QMatrix& matrix, size_t pageIndex) const
2019-02-21 19:35:07 +01:00
{
2019-02-24 17:48:37 +01:00
Q_UNUSED(painter);
2019-02-21 19:35:07 +01:00
2019-02-24 17:48:37 +01:00
const PDFCatalog* catalog = m_document->getCatalog();
if (pageIndex >= catalog->getPageCount() || !catalog->getPage(pageIndex))
2019-02-17 18:01:22 +01:00
{
2019-02-24 17:48:37 +01:00
// Invalid page index
return { PDFRenderError(RenderErrorType::Error, PDFTranslationContext::tr("Page %1 doesn't exist.").arg(pageIndex + 1)) };
2019-02-17 18:01:22 +01:00
}
2019-02-24 17:48:37 +01:00
const PDFPage* page = catalog->getPage(pageIndex);
Q_ASSERT(page);
2019-02-17 18:01:22 +01:00
PDFPainter processor(painter, m_features, matrix, page, m_document, m_fontCache, m_optionalContentActivity);
2019-02-24 17:48:37 +01:00
return processor.processContents();
2019-02-17 18:01:22 +01:00
}
2019-02-09 18:40:56 +01:00
} // namespace pdf