2021-04-30 20:12:10 +02:00
|
|
|
// Copyright (C) 2018-2021 Jakub Melka
|
2018-12-26 18:00:17 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// This file is part of PDF4QT.
|
2018-12-26 18:00:17 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is free software: you can redistribute it and/or modify
|
2018-12-26 18:00:17 +01:00
|
|
|
// 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
|
2021-04-30 20:12:10 +02:00
|
|
|
// with the written consent of the copyright owner, any later version.
|
2018-12-26 18:00:17 +01:00
|
|
|
//
|
2021-08-10 19:22:56 +02:00
|
|
|
// PDF4QT is distributed in the hope that it will be useful,
|
2018-12-26 18:00:17 +01:00
|
|
|
// 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
|
2021-08-10 19:22:56 +02:00
|
|
|
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
#include "pdfpage.h"
|
|
|
|
#include "pdfdocument.h"
|
2019-04-29 17:03:19 +02:00
|
|
|
#include "pdfexception.h"
|
2020-08-01 15:53:54 +02:00
|
|
|
#include "pdfencoding.h"
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
namespace pdf
|
|
|
|
{
|
|
|
|
|
|
|
|
PDFPageInheritableAttributes PDFPageInheritableAttributes::parse(const PDFPageInheritableAttributes& templateAttributes,
|
|
|
|
const PDFObject& dictionary,
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObjectStorage* storage)
|
2018-12-26 18:00:17 +01:00
|
|
|
{
|
|
|
|
PDFPageInheritableAttributes result(templateAttributes);
|
|
|
|
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObject& dereferencedDictionary = storage->getObject(dictionary);
|
2018-12-26 18:00:17 +01:00
|
|
|
if (dereferencedDictionary.isDictionary())
|
|
|
|
{
|
2020-10-31 14:23:13 +01:00
|
|
|
PDFDocumentDataLoaderDecorator loader(storage);
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
const PDFDictionary* dictionary = dereferencedDictionary.getDictionary();
|
|
|
|
if (dictionary->hasKey("MediaBox"))
|
|
|
|
{
|
|
|
|
result.m_mediaBox = loader.readRectangle(dictionary->get("MediaBox"), result.getMediaBox());
|
|
|
|
}
|
|
|
|
if (dictionary->hasKey("CropBox"))
|
|
|
|
{
|
|
|
|
result.m_cropBox = loader.readRectangle(dictionary->get("CropBox"), result.getCropBox());
|
|
|
|
}
|
|
|
|
if (dictionary->hasKey("Resources"))
|
|
|
|
{
|
|
|
|
result.m_resources = dictionary->get("Resources");
|
|
|
|
}
|
|
|
|
if (dictionary->hasKey("Rotate"))
|
|
|
|
{
|
|
|
|
PDFInteger rotation = loader.readInteger(dictionary->get("Rotate"), 0);
|
|
|
|
|
|
|
|
// PDF specification says, that angle can be multiple of 90, so we can have here
|
|
|
|
// for example, 450° (90° * 5), or even negative angles. We must get rid of them.
|
|
|
|
PDFInteger fullCircles = rotation / 360;
|
|
|
|
if (fullCircles != 0)
|
|
|
|
{
|
|
|
|
rotation = rotation - fullCircles * 360;
|
|
|
|
}
|
|
|
|
|
2020-08-01 18:24:33 +02:00
|
|
|
if (rotation < 0)
|
|
|
|
{
|
|
|
|
rotation += 360;
|
|
|
|
}
|
|
|
|
|
2018-12-26 18:00:17 +01:00
|
|
|
switch (rotation)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
result.m_pageRotation = PageRotation::None;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 90:
|
|
|
|
{
|
|
|
|
result.m_pageRotation = PageRotation::Rotate90;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 180:
|
|
|
|
{
|
|
|
|
result.m_pageRotation = PageRotation::Rotate180;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 270:
|
|
|
|
{
|
|
|
|
result.m_pageRotation = PageRotation::Rotate270;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Invalid page rotation."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PageRotation PDFPageInheritableAttributes::getPageRotation() const
|
|
|
|
{
|
|
|
|
if (m_pageRotation)
|
|
|
|
{
|
|
|
|
return m_pageRotation.value();
|
|
|
|
}
|
|
|
|
return PageRotation::None;
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:23:13 +01:00
|
|
|
std::vector<PDFPage> PDFPage::parse(const PDFObjectStorage* storage, const PDFObject& root)
|
2018-12-26 18:00:17 +01:00
|
|
|
{
|
|
|
|
std::vector<PDFPage> result;
|
|
|
|
std::set<PDFObjectReference> visited;
|
2020-10-31 14:23:13 +01:00
|
|
|
parseImpl(result, visited, PDFPageInheritableAttributes(), root, storage);
|
2018-12-26 18:00:17 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:55:06 +01:00
|
|
|
QRectF PDFPage::getRectMM(const QRectF& rect) const
|
|
|
|
{
|
|
|
|
return QRectF(convertPDFPointToMM(rect.left()),
|
|
|
|
convertPDFPointToMM(rect.top()),
|
|
|
|
convertPDFPointToMM(rect.width()),
|
|
|
|
convertPDFPointToMM(rect.height()));
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:16:37 +02:00
|
|
|
QRectF PDFPage::getRotatedMediaBox() const
|
|
|
|
{
|
|
|
|
return getRotatedBox(getMediaBox(), getPageRotation());
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:28:02 +01:00
|
|
|
QRectF PDFPage::getRotatedMediaBoxMM() const
|
|
|
|
{
|
|
|
|
return getRotatedBox(getMediaBoxMM(), getPageRotation());
|
|
|
|
}
|
|
|
|
|
2019-09-01 15:44:22 +02:00
|
|
|
QRectF PDFPage::getRotatedCropBox() const
|
|
|
|
{
|
|
|
|
return getRotatedBox(getCropBox(), getPageRotation());
|
|
|
|
}
|
|
|
|
|
2020-08-01 15:53:54 +02:00
|
|
|
PDFObject PDFPage::getObjectFromPageDictionary(const PDFObjectStorage* storage, const char* key) const
|
|
|
|
{
|
|
|
|
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(m_pageObject))
|
|
|
|
{
|
|
|
|
return dictionary->get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PDFObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getBoxColorInfo(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "BoxColorInfo");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getTransparencyGroup(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "Group");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getThumbnail(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "Thumb");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getTransition(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "Trans");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getAdditionalActions(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "AA");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getMetadata(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "Metadata");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getPieceDictionary(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "PieceInfo");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getColorSeparationInfo(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "SeparationInfo");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getFirstSubpageNavigationNode(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "PresSteps");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getViewports(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "VP");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getAssociatedFiles(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "AF");
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFObject PDFPage::getOutputIntents(const PDFObjectStorage* storage) const
|
|
|
|
{
|
|
|
|
return getObjectFromPageDictionary(storage, "OutputIntents");
|
|
|
|
}
|
|
|
|
|
2021-07-24 17:57:11 +02:00
|
|
|
QSizeF PDFPage::getRotatedSize(const QSizeF& size, PageRotation rotation)
|
|
|
|
{
|
|
|
|
switch (rotation)
|
|
|
|
{
|
|
|
|
case PageRotation::None:
|
|
|
|
case PageRotation::Rotate180:
|
|
|
|
// Preserve rotation
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PageRotation::Rotate90:
|
|
|
|
case PageRotation::Rotate270:
|
|
|
|
return size.transposed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:55:06 +01:00
|
|
|
QRectF PDFPage::getRotatedBox(const QRectF& rect, PageRotation rotation)
|
|
|
|
{
|
|
|
|
switch (rotation)
|
|
|
|
{
|
|
|
|
case PageRotation::None:
|
|
|
|
case PageRotation::Rotate180:
|
|
|
|
// Preserve rotation
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PageRotation::Rotate90:
|
|
|
|
case PageRotation::Rotate270:
|
|
|
|
return rect.transposed();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2018-12-26 18:00:17 +01:00
|
|
|
void PDFPage::parseImpl(std::vector<PDFPage>& pages,
|
|
|
|
std::set<PDFObjectReference>& visitedReferences,
|
|
|
|
const PDFPageInheritableAttributes& templateAttributes,
|
|
|
|
const PDFObject& root,
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObjectStorage* storage)
|
2018-12-26 18:00:17 +01:00
|
|
|
{
|
|
|
|
// Are we in internal node, or leaf (page object)?
|
2019-11-30 16:26:32 +01:00
|
|
|
PDFObjectReference objectReference = root.isReference() ? root.getReference() : PDFObjectReference();
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObject& dereferenced = storage->getObject(root);
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
if (dereferenced.isDictionary())
|
|
|
|
{
|
|
|
|
const PDFDictionary* dictionary = dereferenced.getDictionary();
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObject& typeObject = storage->getObject(dictionary->get("Type"));
|
2018-12-26 18:00:17 +01:00
|
|
|
if (typeObject.isName())
|
|
|
|
{
|
2020-10-31 14:23:13 +01:00
|
|
|
PDFPageInheritableAttributes currentInheritableAttributes = PDFPageInheritableAttributes::parse(templateAttributes, root, storage);
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
QByteArray typeString = typeObject.getString();
|
|
|
|
if (typeString == "Pages")
|
|
|
|
{
|
2020-10-31 14:23:13 +01:00
|
|
|
const PDFObject& kids = storage->getObject(dictionary->get("Kids"));
|
2018-12-26 18:00:17 +01:00
|
|
|
if (kids.isArray())
|
|
|
|
{
|
|
|
|
const PDFArray* kidsArray = kids.getArray();
|
|
|
|
const size_t count = kidsArray->getCount();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
const PDFObject& kid = kidsArray->getItem(i);
|
|
|
|
|
|
|
|
// Check reference
|
|
|
|
if (!kid.isReference())
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Expected valid kids in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check cycles
|
|
|
|
if (visitedReferences.count(kid.getReference()))
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Detected cycles in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
visitedReferences.insert(kid.getReference());
|
2020-10-31 14:23:13 +01:00
|
|
|
parseImpl(pages, visitedReferences, currentInheritableAttributes, kid, storage);
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Expected valid kids in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeString == "Page")
|
|
|
|
{
|
|
|
|
PDFPage page;
|
|
|
|
|
2020-08-01 15:53:54 +02:00
|
|
|
page.m_pageObject = dereferenced;
|
2019-11-30 16:26:32 +01:00
|
|
|
page.m_pageReference = objectReference;
|
2018-12-26 18:00:17 +01:00
|
|
|
page.m_mediaBox = currentInheritableAttributes.getMediaBox();
|
|
|
|
page.m_cropBox = currentInheritableAttributes.getCropBox();
|
2020-10-31 14:23:13 +01:00
|
|
|
page.m_resources = storage->getObject(currentInheritableAttributes.getResources());
|
2018-12-26 18:00:17 +01:00
|
|
|
page.m_pageRotation = currentInheritableAttributes.getPageRotation();
|
|
|
|
|
|
|
|
if (!page.m_cropBox.isValid())
|
|
|
|
{
|
|
|
|
page.m_cropBox = page.m_mediaBox;
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:23:13 +01:00
|
|
|
PDFDocumentDataLoaderDecorator loader(storage);
|
2018-12-26 18:00:17 +01:00
|
|
|
page.m_bleedBox = loader.readRectangle(dictionary->get("BleedBox"), page.getCropBox());
|
|
|
|
page.m_trimBox = loader.readRectangle(dictionary->get("TrimBox"), page.getCropBox());
|
|
|
|
page.m_artBox = loader.readRectangle(dictionary->get("ArtBox"), page.getCropBox());
|
2020-10-31 14:23:13 +01:00
|
|
|
page.m_contents = storage->getObject(dictionary->get("Contents"));
|
2020-03-07 17:38:50 +01:00
|
|
|
page.m_annots = loader.readReferenceArrayFromDictionary(dictionary, "Annots");
|
2020-08-01 15:53:54 +02:00
|
|
|
page.m_lastModified = PDFEncoding::convertToDateTime(loader.readStringFromDictionary(dictionary, "LastModified"));
|
|
|
|
page.m_thumbnailReference = loader.readReferenceFromDictionary(dictionary, "Thumb");
|
|
|
|
page.m_beads = loader.readReferenceArrayFromDictionary(dictionary, "B");
|
|
|
|
page.m_duration = loader.readIntegerFromDictionary(dictionary, "Dur", 0);
|
|
|
|
page.m_structParent = loader.readIntegerFromDictionary(dictionary, "StructParents", 0);
|
|
|
|
page.m_webCaptureContentSetId = loader.readStringFromDictionary(dictionary, "ID");
|
|
|
|
page.m_preferredZoom = loader.readNumberFromDictionary(dictionary, "PZ", 0.0);
|
|
|
|
|
|
|
|
constexpr const std::array<std::pair<const char*, PageTabOrder>, 5> tabStops =
|
|
|
|
{
|
|
|
|
std::pair<const char*, PageTabOrder>{ "R", PageTabOrder::Row },
|
|
|
|
std::pair<const char*, PageTabOrder>{ "C", PageTabOrder::Column },
|
|
|
|
std::pair<const char*, PageTabOrder>{ "S", PageTabOrder::Structure },
|
|
|
|
std::pair<const char*, PageTabOrder>{ "A", PageTabOrder::Array },
|
|
|
|
std::pair<const char*, PageTabOrder>{ "W", PageTabOrder::Widget }
|
|
|
|
};
|
|
|
|
|
|
|
|
page.m_pageTabOrder = loader.readEnumByName(dictionary->get("Tabs"), tabStops.cbegin(), tabStops.cend(), PageTabOrder::Invalid);
|
|
|
|
page.m_templateName = loader.readNameFromDictionary(dictionary, "TemplateInstantiated");
|
|
|
|
page.m_userUnit = loader.readNumberFromDictionary(dictionary, "UserUnit", 1.0);
|
|
|
|
page.m_documentPart = loader.readReferenceFromDictionary(dictionary, "DPart");
|
2018-12-26 18:00:17 +01:00
|
|
|
|
|
|
|
pages.emplace_back(std::move(page));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Expected valid type item in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Expected valid type item in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-27 18:41:56 +02:00
|
|
|
throw PDFException(PDFTranslationContext::tr("Expected dictionary in page tree."));
|
2018-12-26 18:00:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace pdf
|