PDF4QT/PdfForQtLib/sources/pdfnametreeloader.h

95 lines
3.4 KiB
C
Raw Normal View History

2020-01-18 11:38:54 +01:00
// Copyright (C) 2019-2020 Jakub Melka
2019-11-30 16:26:32 +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/>.
#ifndef PDFNAMETREELOADER_H
#define PDFNAMETREELOADER_H
#include "pdfdocument.h"
#include <map>
#include <functional>
namespace pdf
{
/// This class can load a number tree into the array
template<typename Type>
class PDFNameTreeLoader
{
public:
explicit PDFNameTreeLoader() = delete;
using MappedObjects = std::map<QByteArray, Type>;
2020-03-29 18:53:04 +02:00
using LoadMethod = std::function<Type(const PDFObjectStorage*, const PDFObject&)>;
2019-11-30 16:26:32 +01:00
/// Parses the name tree and loads its items into the map. Some errors are ignored,
/// e.g. when kid is null. Objects are retrieved by \p loadMethod.
2020-03-29 18:53:04 +02:00
/// \param storage Object storage
2019-11-30 16:26:32 +01:00
/// \param root Root of the name tree
/// \param loadMethod Parsing method, which retrieves parsed object
2020-03-29 18:53:04 +02:00
static MappedObjects parse(const PDFObjectStorage* storage, const PDFObject& root, const LoadMethod& loadMethod)
2019-11-30 16:26:32 +01:00
{
MappedObjects result;
2020-03-29 18:53:04 +02:00
parseImpl(result, storage, root, loadMethod);
2019-11-30 16:26:32 +01:00
return result;
}
private:
2020-03-29 18:53:04 +02:00
static void parseImpl(MappedObjects& objects, const PDFObjectStorage* storage, const PDFObject& root, const LoadMethod& loadMethod)
2019-11-30 16:26:32 +01:00
{
2020-03-29 18:53:04 +02:00
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(root))
2019-11-30 16:26:32 +01:00
{
// Jakub Melka: First, load the objects into the map
2020-03-29 18:53:04 +02:00
const PDFObject& namedItems = storage->getObject(dictionary->get("Names"));
2019-11-30 16:26:32 +01:00
if (namedItems.isArray())
{
const PDFArray* namedItemsArray = namedItems.getArray();
const size_t count = namedItemsArray->getCount() / 2;
for (size_t i = 0; i < count; ++i)
{
const size_t numberIndex = 2 * i;
const size_t valueIndex = 2 * i + 1;
2020-03-29 18:53:04 +02:00
const PDFObject& name = storage->getObject(namedItemsArray->getItem(numberIndex));
2019-11-30 16:26:32 +01:00
if (!name.isString())
{
continue;
}
2020-03-29 18:53:04 +02:00
objects[name.getString()] = loadMethod(storage, namedItemsArray->getItem(valueIndex));
2019-11-30 16:26:32 +01:00
}
}
// Then, follow the kids
2020-03-29 18:53:04 +02:00
const PDFObject& kids = storage->getObject(dictionary->get("Kids"));
2019-11-30 16:26:32 +01:00
if (kids.isArray())
{
const PDFArray* kidsArray = kids.getArray();
const size_t count = kidsArray->getCount();
for (size_t i = 0; i < count; ++i)
{
2020-03-29 18:53:04 +02:00
parseImpl(objects, storage, kidsArray->getItem(i), loadMethod);
2019-11-30 16:26:32 +01:00
}
}
}
}
};
} // namespace pdf
#endif // PDFNAMETREELOADER_H