PDF4QT/PdfForQtLib/sources/pdffile.cpp

184 lines
5.7 KiB
C++
Raw Normal View History

2020-01-18 11:38:54 +01:00
// Copyright (C) 2019-2020 Jakub Melka
2019-11-17 17:41:07 +01:00
//
// 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 "pdffile.h"
#include "pdfdocument.h"
#include "pdfencoding.h"
namespace pdf
{
QString PDFFileSpecification::getPlatformFileName() const
{
// UF has maximal precedence, because it is unicode string
if (!m_UF.isEmpty())
{
return m_UF;
}
if (!m_F.isEmpty())
{
return QString::fromLatin1(m_F);
}
#ifdef Q_OS_WIN
for (const QByteArray& platformName : { m_DOS, m_Mac, m_Unix })
{
if (!platformName.isEmpty())
{
return QString::fromLatin1(platformName);
}
}
#endif
#ifdef Q_OS_UNIX
for (const QByteArray& platformName : { m_Unix, m_Mac, m_DOS })
{
if (!platformName.isEmpty())
{
return QString::fromLatin1(platformName);
}
}
#endif
#ifdef Q_OS_MAC
for (const QByteArray& platformName : { m_Mac, m_Unix, m_DOS })
{
if (!platformName.isEmpty())
{
return QString::fromLatin1(platformName);
}
}
#endif
return QString();
}
const PDFEmbeddedFile* PDFFileSpecification::getPlatformFile() const
{
if (m_embeddedFiles.count("UF"))
{
return &m_embeddedFiles.at("UF");
}
if (m_embeddedFiles.count("F"))
{
return &m_embeddedFiles.at("F");
}
#ifdef Q_OS_WIN
if (m_embeddedFiles.count("DOS"))
{
return &m_embeddedFiles.at("DOS");
}
#endif
#ifdef Q_OS_UNIX
if (m_embeddedFiles.count("Unix"))
{
return &m_embeddedFiles.at("Unix");
}
#endif
#ifdef Q_OS_MAC
if (m_embeddedFiles.count("Mac"))
{
return &m_embeddedFiles.at("Mac");
}
#endif
return nullptr;
}
2020-03-29 18:53:04 +02:00
PDFFileSpecification PDFFileSpecification::parse(const PDFObjectStorage* storage, PDFObject object)
2019-11-17 17:41:07 +01:00
{
PDFFileSpecification result;
2020-03-29 18:53:04 +02:00
object = storage->getObject(object);
2019-11-17 17:41:07 +01:00
if (object.isString())
{
result.m_UF = PDFEncoding::convertTextString(object.getString());
}
else if (object.isDictionary())
{
2020-03-29 18:53:04 +02:00
PDFDocumentDataLoaderDecorator loader(storage);
2019-11-17 17:41:07 +01:00
const PDFDictionary* dictionary = object.getDictionary();
PDFObject collectionObject = dictionary->get("Cl");
result.m_fileSystem = loader.readNameFromDictionary(dictionary, "FS");
result.m_F = loader.readStringFromDictionary(dictionary, "F");
result.m_UF = loader.readTextStringFromDictionary(dictionary, "UF", QString());
result.m_DOS = loader.readStringFromDictionary(dictionary, "DOS");
result.m_Mac = loader.readStringFromDictionary(dictionary, "Mac");
result.m_Unix = loader.readStringFromDictionary(dictionary, "Unix");
result.m_volatile = loader.readBooleanFromDictionary(dictionary, "V", false);
result.m_description = loader.readTextStringFromDictionary(dictionary, "Desc", QString());
result.m_collection = collectionObject.isReference() ? collectionObject.getReference() : PDFObjectReference();
2020-03-29 18:53:04 +02:00
PDFObject embeddedFiles = storage->getObject(dictionary->get("EF"));
2019-11-17 17:41:07 +01:00
if (embeddedFiles.isDictionary())
{
const PDFDictionary* embeddedFilesDictionary = embeddedFiles.getDictionary();
for (size_t i = 0; i < embeddedFilesDictionary->getCount(); ++i)
{
2020-03-29 18:53:04 +02:00
result.m_embeddedFiles[embeddedFilesDictionary->getKey(i)] = PDFEmbeddedFile::parse(storage, embeddedFilesDictionary->getValue(i));
2019-11-17 17:41:07 +01:00
}
}
}
return result;
}
2020-03-29 18:53:04 +02:00
PDFEmbeddedFile PDFEmbeddedFile::parse(const PDFObjectStorage* storage, PDFObject object)
2019-11-17 17:41:07 +01:00
{
PDFEmbeddedFile result;
2020-03-29 18:53:04 +02:00
object = storage->getObject(object);
2019-11-17 17:41:07 +01:00
if (object.isStream())
{
const PDFStream* stream = object.getStream();
const PDFDictionary* dictionary = stream->getDictionary();
2020-03-29 18:53:04 +02:00
PDFDocumentDataLoaderDecorator loader(storage);
2019-11-17 17:41:07 +01:00
result.m_stream = object;
result.m_subtype = loader.readNameFromDictionary(dictionary, "Subtype");
2020-03-29 18:53:04 +02:00
const PDFObject& paramsObject = storage->getObject(dictionary->get("Params"));
2019-11-17 17:41:07 +01:00
if (paramsObject.isDictionary())
{
const PDFDictionary* paramsDictionary = paramsObject.getDictionary();
auto getDateTime = [&loader, paramsDictionary](const char* name)
{
QByteArray ba = loader.readStringFromDictionary(paramsDictionary, name);
if (!ba.isEmpty())
{
return PDFEncoding::convertToDateTime(ba);
}
return QDateTime();
};
result.m_size = loader.readIntegerFromDictionary(paramsDictionary, "Size", -1);
result.m_creationDate = getDateTime("CreationDate");
result.m_modifiedDate = getDateTime("ModDate");
result.m_checksum = loader.readStringFromDictionary(paramsDictionary, "CheckSum");
}
}
return result;
}
} // namespace pdf