PDF4QT/Pdf4QtLib/sources/pdfnumbertreeloader.h

98 lines
3.4 KiB
C
Raw Normal View History

2021-04-30 20:12:10 +02:00
// Copyright (C) 2018-2021 Jakub Melka
2018-12-24 17:09:23 +01:00
//
2021-08-10 19:22:56 +02:00
// This file is part of PDF4QT.
2018-12-24 17:09:23 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is free software: you can redistribute it and/or modify
2018-12-24 17:09:23 +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-24 17:09:23 +01:00
//
2021-08-10 19:22:56 +02:00
// PDF4QT is distributed in the hope that it will be useful,
2018-12-24 17:09:23 +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-24 17:09:23 +01:00
#ifndef PDFNUMBERTREELOADER_H
#define PDFNUMBERTREELOADER_H
#include "pdfdocument.h"
#include <vector>
namespace pdf
{
/// This class can load a number tree into the array
template<typename Type>
class PDFNumberTreeLoader
{
public:
explicit PDFNumberTreeLoader() = delete;
using Objects = std::vector<Type>;
/// Parses the number tree and loads its items into the array. Some errors are ignored,
/// e.g. when kid is null. Type must contain methods to load object array.
2020-07-18 19:46:42 +02:00
static Objects parse(const PDFObjectStorage* storage, const PDFObject& root)
2018-12-24 17:09:23 +01:00
{
Objects result;
// First, try to load items from the tree into the array
2020-07-18 19:46:42 +02:00
parseImpl(result, storage, root);
2018-12-24 17:09:23 +01:00
// Array may not be sorted. Sort it using comparison operator for Type.
std::stable_sort(result.begin(), result.end());
return result;
}
private:
2020-07-18 19:46:42 +02:00
static void parseImpl(Objects& objects, const PDFObjectStorage* storage, const PDFObject& root)
2018-12-24 17:09:23 +01:00
{
2020-07-18 19:46:42 +02:00
if (const PDFDictionary* dictionary = storage->getDictionaryFromObject(root))
2018-12-24 17:09:23 +01:00
{
// First, load the objects into the array
2020-07-18 19:46:42 +02:00
const PDFObject& numberedItems = storage->getObject(dictionary->get("Nums"));
2018-12-24 17:09:23 +01:00
if (numberedItems.isArray())
{
const PDFArray* numberedItemsArray = numberedItems.getArray();
const size_t count = numberedItemsArray->getCount() / 2;
objects.reserve(objects.size() + count);
for (size_t i = 0; i < count; ++i)
{
const size_t numberIndex = 2 * i;
const size_t valueIndex = 2 * i + 1;
2020-07-18 19:46:42 +02:00
const PDFObject& number = storage->getObject(numberedItemsArray->getItem(numberIndex));
2018-12-24 17:09:23 +01:00
if (!number.isInt())
{
continue;
}
2020-07-18 19:46:42 +02:00
objects.emplace_back(Type::parse(number.getInteger(), storage, numberedItemsArray->getItem(valueIndex)));
2018-12-24 17:09:23 +01:00
}
}
// Then, follow the kids
2020-07-18 19:46:42 +02:00
const PDFObject& kids = storage->getObject(dictionary->get("Kids"));
2018-12-24 17:09:23 +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-07-18 19:46:42 +02:00
parseImpl(objects, storage, kidsArray->getItem(i));
2018-12-24 17:09:23 +01:00
}
}
}
}
};
} // namespace pdf
#endif // PDFNUMBERTREELOADER_H