Collections

This commit is contained in:
Jakub Melka 2020-08-08 18:18:08 +02:00
parent 79cf21775c
commit ccff2095e4
5 changed files with 591 additions and 3 deletions

View File

@ -81,7 +81,7 @@ private:
/// \param isRGB888Buffer If true, 8-bit RGB output buffer is used, otherwise FLOAT RGB output buffer is used
cmsHTRANSFORM getTransformFromICCProfile(const QByteArray& iccData, const QByteArray& iccID, RenderingIntent renderingIntent, bool isRGB888Buffer) const;
/// Returns transformation flags accordint to the current settings
/// Returns transformation flags according to the current settings
cmsUInt32Number getTransformationFlags() const;
/// Calculates effective rendering intent. If rendering intent is auto,

View File

@ -558,6 +558,21 @@ QStringList PDFDocumentDataLoaderDecorator::readTextStringList(const PDFObject&
return result;
}
QColor PDFDocumentDataLoaderDecorator::readRGBColorFromDictionary(const PDFDictionary* dictionary, const char* key, QColor defaultColor)
{
std::vector<PDFReal> colors = readNumberArrayFromDictionary(dictionary, key);
if (colors.size() == 3)
{
const PDFReal red = qBound(0.0, colors[0], 1.0);
const PDFReal green = qBound(0.0, colors[1], 1.0);
const PDFReal blue = qBound(0.0, colors[2], 1.0);
return QColor::fromRgbF(red, green, blue);
}
return defaultColor;
}
std::optional<QByteArray> PDFDocumentDataLoaderDecorator::readOptionalStringFromDictionary(const PDFDictionary* dictionary, const char* key) const
{
if (dictionary->hasKey(key))

View File

@ -25,6 +25,7 @@
#include "pdfsecurityhandler.h"
#include <QtCore>
#include <QColor>
#include <QMatrix>
#include <QDateTime>
@ -362,6 +363,9 @@ public:
/// Reads string list. If error occurs, empty list is returned.
QStringList readTextStringList(const PDFObject& object);
/// Reads RGB color from dictionary
QColor readRGBColorFromDictionary(const PDFDictionary* dictionary, const char* key, QColor defaultColor);
/// Reads list of object, using parse function defined in object
template<typename Object>
std::vector<Object> readObjectList(PDFObject object)

View File

@ -243,4 +243,304 @@ PDFFileIdentifier PDFFileIdentifier::parse(const PDFObjectStorage* storage, PDFO
return result;
}
PDFCollectionField PDFCollectionField::parse(const PDFObjectStorage* storage, PDFObject object)
{
PDFCollectionField result;
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(object))
{
PDFDocumentDataLoaderDecorator loader(storage);
constexpr const std::array fieldKinds = {
std::pair<const char*, Kind>{ "S", Kind::TextField },
std::pair<const char*, Kind>{ "D", Kind::DateField },
std::pair<const char*, Kind>{ "N", Kind::NumberField },
std::pair<const char*, Kind>{ "F", Kind::FileName },
std::pair<const char*, Kind>{ "Desc", Kind::Description },
std::pair<const char*, Kind>{ "ModDate", Kind::ModifiedDate },
std::pair<const char*, Kind>{ "CreationDate", Kind::CreationDate },
std::pair<const char*, Kind>{ "Size", Kind::Size },
std::pair<const char*, Kind>{ "CompressedSize", Kind::CompressedSize },
};
result.m_kind = loader.readEnumByName(dictionary->get("Subtype"), fieldKinds.begin(), fieldKinds.end(), Kind::Invalid);
switch (result.m_kind)
{
case Kind::Invalid:
result.m_value = Value::Invalid;
break;
case Kind::TextField:
result.m_value = Value::TextString;
break;
case Kind::DateField:
result.m_value = Value::DateTime;
break;
case Kind::NumberField:
result.m_value = Value::Number;
break;
case Kind::FileName:
result.m_value = Value::TextString;
break;
case Kind::Description:
result.m_value = Value::TextString;
break;
case Kind::ModifiedDate:
result.m_value = Value::DateTime;
break;
case Kind::CreationDate:
result.m_value = Value::DateTime;
break;
case Kind::Size:
result.m_value = Value::Number;
break;
case Kind::CompressedSize:
result.m_value = Value::Number;
break;
default:
Q_ASSERT(false);
break;
}
result.m_fieldName = loader.readTextStringFromDictionary(dictionary, "N", QString());
result.m_order = loader.readIntegerFromDictionary(dictionary, "O", 0);
result.m_visible = loader.readBooleanFromDictionary(dictionary, "V", true);
result.m_editable = loader.readBooleanFromDictionary(dictionary, "E", false);
}
return result;
}
const PDFCollectionField* PDFCollectionSchema::getField(const QByteArray& key) const
{
auto it = m_fields.find(key);
if (it != m_fields.cend())
{
return &it->second;
}
else
{
static PDFCollectionField dummy;
return &dummy;
}
}
PDFCollectionSchema PDFCollectionSchema::parse(const PDFObjectStorage* storage, PDFObject object)
{
PDFCollectionSchema result;
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(object))
{
const size_t count = dictionary->getCount();
for (size_t i = 0; i < count; ++i)
{
QByteArray key = dictionary->getKey(i).getString();
if (key == "Type")
{
continue;
}
result.m_fields[key] = PDFCollectionField::parse(storage, dictionary->getValue(i));
}
}
return result;
}
PDFCollection PDFCollection::parse(const PDFObjectStorage* storage, PDFObject object)
{
PDFCollection result;
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(object))
{
PDFDocumentDataLoaderDecorator loader(storage);
constexpr const std::array viewModes = {
std::pair<const char*, ViewMode>{ "D", ViewMode::Details },
std::pair<const char*, ViewMode>{ "T", ViewMode::Tiles },
std::pair<const char*, ViewMode>{ "H", ViewMode::Hidden },
std::pair<const char*, ViewMode>{ "C", ViewMode::Navigation }
};
result.m_schema = PDFCollectionSchema::parse(storage, dictionary->get("Schema"));
result.m_document = loader.readStringFromDictionary(dictionary, "D");
result.m_viewMode = loader.readEnumByName(dictionary->get("View"), viewModes.begin(), viewModes.end(), ViewMode::Details);
result.m_navigator = loader.readReferenceFromDictionary(dictionary, "Navigator");
const PDFDictionary* colorDictionary = storage->getDictionaryFromObject(dictionary->get("Colors"));
if (colorDictionary)
{
result.m_colors[Background] = loader.readRGBColorFromDictionary(colorDictionary, "Background", Qt::white);
result.m_colors[CardBackground] = loader.readRGBColorFromDictionary(colorDictionary, "CardBackground", Qt::white);
result.m_colors[CardBorder] = loader.readRGBColorFromDictionary(colorDictionary, "CardBorder", Qt::white);
result.m_colors[PrimaryText] = loader.readRGBColorFromDictionary(colorDictionary, "PrimaryText", Qt::black);
result.m_colors[SecondaryText] = loader.readRGBColorFromDictionary(colorDictionary, "SecondaryText", Qt::black);
}
const PDFDictionary* sortDictionary = storage->getDictionaryFromObject(dictionary->get("Sort"));
if (sortDictionary)
{
result.m_sortAscending = loader.readBooleanFromDictionary(sortDictionary, "A", true);
const PDFObject& columns = storage->getObject(sortDictionary->get("S"));
if (columns.isName())
{
result.m_sortColumns.emplace_back(SortColumn{loader.readName(columns), result.m_sortAscending});
}
else
{
std::vector<QByteArray> names = loader.readNameArray(columns);
for (QByteArray& name : names)
{
result.m_sortColumns.emplace_back(SortColumn{qMove(name), result.m_sortAscending});
}
}
const PDFObject& sortDirection = storage->getObject(sortDictionary->get("A"));
if (sortDirection.isArray())
{
const PDFArray* sortDirectionArray = sortDirection.getArray();
const size_t size = qMin(result.m_sortColumns.size(), sortDirectionArray->getCount());
for (size_t i = 0; i < size; ++i)
{
result.m_sortColumns[i].ascending = loader.readBoolean(sortDirectionArray->getItem(i), result.m_sortAscending);
}
}
}
result.m_folderRoot = loader.readReferenceFromDictionary(dictionary, "Folders");
const PDFDictionary* splitDictionary = storage->getDictionaryFromObject(dictionary->get("Split"));
if (splitDictionary)
{
constexpr const std::array splitModes = {
std::pair<const char*, SplitMode>{ "H", SplitMode::Horizontally },
std::pair<const char*, SplitMode>{ "V", SplitMode::Vertically },
std::pair<const char*, SplitMode>{ "N", SplitMode::None }
};
result.m_splitMode = loader.readEnumByName(splitDictionary->get("Direction"), splitModes.begin(), splitModes.end(), SplitMode::None);
result.m_splitProportion = loader.readNumberFromDictionary(splitDictionary, "Position", 30);
}
}
return result;
}
PDFCollectionFolder PDFCollectionFolder::parse(const PDFObjectStorage* storage, PDFObject object)
{
PDFCollectionFolder result;
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(object))
{
PDFDocumentDataLoaderDecorator loader(storage);
result.m_ID = loader.readIntegerFromDictionary(dictionary, "ID", 0);
result.m_name = loader.readTextStringFromDictionary(dictionary, "Name", QString());
result.m_parent = loader.readReferenceFromDictionary(dictionary, "Parent");
result.m_child = loader.readReferenceFromDictionary(dictionary, "Child");
result.m_next = loader.readReferenceFromDictionary(dictionary, "Next");
result.m_collection = loader.readReferenceFromDictionary(dictionary, "CI");
result.m_description = loader.readTextStringFromDictionary(dictionary, "Desc", QString());
QByteArray createdString = loader.readStringFromDictionary(dictionary, "CreationDate");
if (!createdString.isEmpty())
{
result.m_created = PDFEncoding::convertToDateTime(createdString);
}
QByteArray modifiedString = loader.readStringFromDictionary(dictionary, "ModDate");
if (!modifiedString.isEmpty())
{
result.m_modified = PDFEncoding::convertToDateTime(modifiedString);
}
result.m_thumbnail = loader.readReferenceFromDictionary(dictionary, "Thumb");
result.m_freeIds = loader.readIntegerArrayFromDictionary(dictionary, "Free");
}
return result;
}
QString PDFCollectionItem::getString(const QByteArray& key, const PDFObjectStorage* storage) const
{
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(m_object))
{
PDFDocumentDataLoaderDecorator loader(storage);
PDFObject valueObject = storage->getObject(dictionary->get(key));
if (valueObject.isDictionary())
{
return loader.readTextString(valueObject.getDictionary()->get("D"), QString());
}
return loader.readTextString(valueObject, QString());
}
return QString();
}
QDateTime PDFCollectionItem::getDateTime(const QByteArray& key, const PDFObjectStorage* storage) const
{
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(m_object))
{
PDFDocumentDataLoaderDecorator loader(storage);
PDFObject valueObject = storage->getObject(dictionary->get(key));
if (valueObject.isDictionary())
{
valueObject = storage->getObject(valueObject.getDictionary()->get("D"));
}
if (valueObject.isString())
{
return PDFEncoding::convertToDateTime(valueObject.getString());
}
}
return QDateTime();
}
PDFInteger PDFCollectionItem::getNumber(const QByteArray& key, const PDFObjectStorage* storage) const
{
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(m_object))
{
PDFDocumentDataLoaderDecorator loader(storage);
PDFObject valueObject = storage->getObject(dictionary->get(key));
if (valueObject.isDictionary())
{
return loader.readInteger(valueObject.getDictionary()->get("D"), 0);
}
return loader.readInteger(valueObject, 0);
}
return 0;
}
QString PDFCollectionItem::getPrefixString(const QByteArray& key, const PDFObjectStorage* storage) const
{
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(m_object))
{
PDFDocumentDataLoaderDecorator loader(storage);
PDFObject valueObject = storage->getObject(dictionary->get(key));
if (valueObject.isDictionary())
{
return loader.readTextString(valueObject.getDictionary()->get("P"), QString());
}
}
return QString();
}
} // namespace pdf

View File

@ -20,6 +20,7 @@
#include "pdfobject.h"
#include <QColor>
#include <QDateTime>
namespace pdf
@ -30,7 +31,7 @@ class PDFObjectStorage;
/// Each identifier consists of two parts - permanent identifier, which
/// is unique identifier based on original document, and changing identifier,
/// which is updated when document is being modified.
class PDFFileIdentifier
class PDFFORQTLIBSHARED_EXPORT PDFFileIdentifier
{
public:
explicit inline PDFFileIdentifier() = default;
@ -45,7 +46,275 @@ private:
QByteArray m_changingIdentifier;
};
class PDFEmbeddedFile
/// Provides description of collection item property field. It describes it's
/// kind, data type, if content of the property should be presented to the user,
/// and ordering, visibility and editability.
class PDFFORQTLIBSHARED_EXPORT PDFCollectionField
{
public:
explicit inline PDFCollectionField() = default;
enum class Kind
{
Invalid,
TextField,
DateField,
NumberField,
FileName,
Description,
ModifiedDate,
CreationDate,
Size,
CompressedSize
};
enum class Value
{
Invalid,
TextString,
DateTime,
Number
};
Kind getKind() const { return m_kind; }
Value getValue() const { return m_value; }
QString getFieldName() const { return m_fieldName; }
PDFInteger getOrder() const { return m_order; }
bool isVisible() const { return m_visible; }
bool isEditable() const { return m_editable; }
static PDFCollectionField parse(const PDFObjectStorage* storage, PDFObject object);
private:
Kind m_kind = Kind::Invalid;
Value m_value = Value::Invalid;
QString m_fieldName;
PDFInteger m_order = 0;
bool m_visible = true;
bool m_editable = false;
};
/// Collection schema. Contains a list of defined fields.
/// Schema can be queried for field definition.
class PDFFORQTLIBSHARED_EXPORT PDFCollectionSchema
{
public:
explicit inline PDFCollectionSchema() = default;
/// Returns true, if schema is valid, i.e. some fields
/// are defined.
bool isValid() const { return !m_fields.empty(); }
/// Returns collection field. This function always returns
/// valid field definition. If field can't be found, then
/// default field (invalid) is returned.
/// \param key Key
const PDFCollectionField* getField(const QByteArray& key) const;
/// Returns true, if field definition with given key is valid.
/// \param key Key
bool isFieldValid(const QByteArray& key) const { return getField(key)->getKind() != PDFCollectionField::Kind::Invalid;}
static PDFCollectionSchema parse(const PDFObjectStorage* storage, PDFObject object);
private:
std::map<QByteArray, PDFCollectionField> m_fields;
};
/// Collection of file attachments. In the PDF file, attached files
/// can be grouped in collection (if they are related to each other).
class PDFFORQTLIBSHARED_EXPORT PDFCollection
{
public:
explicit inline PDFCollection() = default;
enum class ViewMode
{
/// Collection items should be displayed in details mode,
/// for example, multi-column table, which contains icon,
/// file name, and all properties listed in the schema.
Details,
/// Collection should be presented in tile mode, each file
/// should have icon, and some information from the schema
/// dictionary.
Tiles,
/// Collection should be initially hidden and should be presented
/// to the user if user performs some explicit action.
Hidden,
/// Collection should be presented via Navigation entry
Navigation
};
enum class SplitMode
{
/// List of files nad preview should be splitted horizontally
Horizontally,
/// List of files and preview should be splitted vertically
Vertically,
/// No preview should be shown
None
};
enum ColorRole
{
Background,
CardBackground,
CardBorder,
PrimaryText,
SecondaryText,
LastColorRole
};
struct SortColumn
{
QByteArray field;
bool ascending = true;
};
using SortColumns = std::vector<SortColumn>;
/// Returns true, if collection schema is valid. If not,
/// default file properties should be used.
/// \returns true, if collection's schema is valid
bool isSchemaValid() const { return m_schema.isValid(); }
/// Returns collection field. This function always returns
/// valid field definition. If field can't be found, then
/// default field (invalid) is returned.
/// \param key Key
const PDFCollectionField* getField(const QByteArray& key) const { return m_schema.getField(key); }
/// Returns true, if field definition with given key is valid.
/// \param key Key
bool isFieldValid(const QByteArray& key) const { return m_schema.isFieldValid(key); }
/// Returns file field schema. Fields are properties of the files,
/// such as size, description, date/time etc.
const PDFCollectionSchema& getSchema() const { return m_schema; }
/// Returns initial document, which shall be presented to the user
/// in the ui (so interactive processor should display this file first).
const QByteArray& getDocument() const { return m_document; }
/// Returns view mode of the collection. It defines how collection
/// should appear in user interface.
ViewMode getViewMode() const { return m_viewMode; }
/// Returns reference to a navigator (if it exists)
PDFObjectReference getNavigator() const { return m_navigator; }
/// Returns color for given role. Invalid color can be returned.
/// If this occurs, default color should be used.
/// \param role Color role
QColor getColor(ColorRole role) const { return m_colors.at(role); }
/// Returns sorting information (list of columns, defining
/// sort key). If it is empty, default sorting should occur.
const SortColumns& getSortColumns() const { return m_sortColumns; }
/// If sort columns are empty, then is default sorting ascending?
bool isSortAscending() const { return m_sortAscending; }
/// Returns folder root (if collection has folders)
PDFObjectReference getFolderRoot() const { return m_folderRoot; }
/// Returns split mode of list of files and preview
SplitMode getSplitMode() const { return m_splitMode; }
/// Returns split proportion
PDFReal getSplitPropertion() const { return m_splitProportion; }
static PDFCollection parse(const PDFObjectStorage* storage, PDFObject object);
private:
PDFCollectionSchema m_schema;
QByteArray m_document;
ViewMode m_viewMode = ViewMode::Details;
PDFObjectReference m_navigator;
std::array<QColor, LastColorRole> m_colors;
SortColumns m_sortColumns;
bool m_sortAscending = true;
PDFObjectReference m_folderRoot;
SplitMode m_splitMode = SplitMode::None;
PDFReal m_splitProportion = 30;
};
/// Collection folder. Can contain subfolders and files.
class PDFFORQTLIBSHARED_EXPORT PDFCollectionFolder
{
public:
explicit inline PDFCollectionFolder() = default;
PDFInteger getInteger() const { return m_ID; }
const QString& getName() const { return m_name; }
const QString& getDescription() const { return m_description; }
PDFObjectReference getParent() const { return m_parent; }
PDFObjectReference getChild() const { return m_child; }
PDFObjectReference getNext() const { return m_next; }
PDFObjectReference getCollection() const { return m_collection; }
PDFObjectReference getThumbnail() const { return m_thumbnail; }
const QDateTime& getCreatedDate() const { return m_created; }
const QDateTime& getModifiedDate() const { return m_modified; }
const std::vector<PDFInteger>& getFreeIds() const { return m_freeIds; }
static PDFCollectionFolder parse(const PDFObjectStorage* storage, PDFObject object);
private:
PDFInteger m_ID = 0;
QString m_name;
QString m_description;
PDFObjectReference m_parent;
PDFObjectReference m_child;
PDFObjectReference m_next;
PDFObjectReference m_collection;
PDFObjectReference m_thumbnail;
QDateTime m_created;
QDateTime m_modified;
std::vector<PDFInteger> m_freeIds;
};
/// Collection item. Contains properties of the collection item,
/// for example, embedded file.
class PDFFORQTLIBSHARED_EXPORT PDFCollectionItem
{
public:
explicit inline PDFCollectionItem() = default;
explicit inline PDFCollectionItem(const PDFObject& object) : m_object(object) { }
/// Returns true, if collection item entry is valid
bool isValid() const { return m_object.isDictionary(); }
/// Returns string from property key. If property is invalid, empty
/// string is returned, no exception is thrown.
/// \param key Key
/// \param storage Storage
QString getString(const QByteArray& key, const PDFObjectStorage* storage) const;
/// Returns date/time from property key. If property is invalid, invalid
/// QDateTime is returned, no exception is thrown.
/// \param key Key
/// \param storage Storage
QDateTime getDateTime(const QByteArray& key, const PDFObjectStorage* storage) const;
/// Returns integer from property key. If property is invalid, zero
/// integer is returned, no exception is thrown.
/// \param key Key
/// \param storage Storage
PDFInteger getNumber(const QByteArray& key, const PDFObjectStorage* storage) const;
/// Returns prefix string from property key. If property is invalid, empty
/// string is returned, no exception is thrown.
/// \param key Key
/// \param storage Storage
QString getPrefixString(const QByteArray& key, const PDFObjectStorage* storage) const;
private:
PDFObject m_object;
};
class PDFFORQTLIBSHARED_EXPORT PDFEmbeddedFile
{
public:
explicit PDFEmbeddedFile() = default;