mirror of https://github.com/JakubMelka/PDF4QT.git
Removing simple objects
This commit is contained in:
parent
680252b634
commit
db4980d245
|
@ -94,6 +94,9 @@ public:
|
|||
/// Returns array of objects stored in this storage
|
||||
const PDFObjects& getObjects() const { return m_objects; }
|
||||
|
||||
/// Returns array of objects stored in this storage
|
||||
PDFObjects& getObjects() { return m_objects; }
|
||||
|
||||
/// Returns trailer dictionary
|
||||
const PDFObject& getTrailerDictionary() const { return m_trailerDictionary; }
|
||||
|
||||
|
|
|
@ -16,10 +16,140 @@
|
|||
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "pdfoptimizer.h"
|
||||
#include "pdfvisitor.h"
|
||||
#include "pdfexecutionpolicy.h"
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
|
||||
class PDFRemoveSimpleObjectsVisitor : public PDFAbstractVisitor
|
||||
{
|
||||
public:
|
||||
explicit PDFRemoveSimpleObjectsVisitor(const PDFObjectStorage* storage, std::atomic<PDFInteger>* counter) :
|
||||
m_storage(storage),
|
||||
m_counter(counter)
|
||||
{
|
||||
m_objectStack.reserve(32);
|
||||
}
|
||||
|
||||
virtual void visitNull() override;
|
||||
virtual void visitBool(bool value) override;
|
||||
virtual void visitInt(PDFInteger value) override;
|
||||
virtual void visitReal(PDFReal value) override;
|
||||
virtual void visitString(PDFStringRef string) override;
|
||||
virtual void visitName(PDFStringRef name) override;
|
||||
virtual void visitArray(const PDFArray* array) override;
|
||||
virtual void visitDictionary(const PDFDictionary* dictionary) override;
|
||||
virtual void visitStream(const PDFStream* stream) override;
|
||||
virtual void visitReference(const PDFObjectReference reference) override;
|
||||
|
||||
PDFObject getObject();
|
||||
|
||||
private:
|
||||
const PDFObjectStorage* m_storage;
|
||||
std::atomic<PDFInteger>* m_counter;
|
||||
std::vector<PDFObject> m_objectStack;
|
||||
};
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitNull()
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createNull());
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitBool(bool value)
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createBool(value));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitInt(PDFInteger value)
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createInteger(value));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitReal(PDFReal value)
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createReal(value));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitString(PDFStringRef string)
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createString(string));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitName(PDFStringRef name)
|
||||
{
|
||||
m_objectStack.push_back(PDFObject::createName(name));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitArray(const PDFArray* array)
|
||||
{
|
||||
acceptArray(array);
|
||||
|
||||
// We have all objects on the stack
|
||||
Q_ASSERT(array->getCount() <= m_objectStack.size());
|
||||
|
||||
auto it = std::next(m_objectStack.cbegin(), m_objectStack.size() - array->getCount());
|
||||
std::vector<PDFObject> objects(it, m_objectStack.cend());
|
||||
PDFObject object = PDFObject::createArray(std::make_shared<PDFArray>(qMove(objects)));
|
||||
m_objectStack.erase(it, m_objectStack.cend());
|
||||
m_objectStack.push_back(object);
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitDictionary(const PDFDictionary* dictionary)
|
||||
{
|
||||
Q_ASSERT(dictionary);
|
||||
|
||||
std::vector<PDFDictionary::DictionaryEntry> entries;
|
||||
entries.reserve(dictionary->getCount());
|
||||
|
||||
for (size_t i = 0, count = dictionary->getCount(); i < count; ++i)
|
||||
{
|
||||
dictionary->getValue(i).accept(this);
|
||||
entries.emplace_back(dictionary->getKey(i), m_objectStack.back());
|
||||
m_objectStack.pop_back();
|
||||
}
|
||||
|
||||
m_objectStack.push_back(PDFObject::createDictionary(std::make_shared<PDFDictionary>(qMove(entries))));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitStream(const PDFStream* stream)
|
||||
{
|
||||
const PDFDictionary* dictionary = stream->getDictionary();
|
||||
|
||||
visitDictionary(dictionary);
|
||||
PDFObject dictionaryObject = m_objectStack.back();
|
||||
m_objectStack.pop_back();
|
||||
m_objectStack.push_back(PDFObject::createStream(std::make_shared<PDFStream>(PDFDictionary(*dictionaryObject.getDictionary()), QByteArray(*stream->getContent()))));
|
||||
}
|
||||
|
||||
void PDFRemoveSimpleObjectsVisitor::visitReference(const PDFObjectReference reference)
|
||||
{
|
||||
PDFObject object = m_storage->getObjectByReference(reference);
|
||||
|
||||
switch (object.getType())
|
||||
{
|
||||
case PDFObject::Type::Null:
|
||||
case PDFObject::Type::Bool:
|
||||
case PDFObject::Type::Int:
|
||||
case PDFObject::Type::Real:
|
||||
case PDFObject::Type::String:
|
||||
case PDFObject::Type::Name:
|
||||
++*m_counter;
|
||||
m_objectStack.push_back(qMove(object));
|
||||
break;
|
||||
|
||||
default:
|
||||
m_objectStack.push_back(PDFObject::createReference(reference));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PDFObject PDFRemoveSimpleObjectsVisitor::getObject()
|
||||
{
|
||||
Q_ASSERT(m_objectStack.size() == 1);
|
||||
return qMove(m_objectStack.back());
|
||||
}
|
||||
|
||||
PDFOptimizer::PDFOptimizer(OptimizationFlags flags, QObject* parent) :
|
||||
QObject(parent),
|
||||
m_flags(flags)
|
||||
|
@ -94,6 +224,19 @@ void PDFOptimizer::setFlags(OptimizationFlags flags)
|
|||
|
||||
bool PDFOptimizer::performDereferenceSimpleObjects()
|
||||
{
|
||||
std::atomic<PDFInteger> counter = 0;
|
||||
|
||||
PDFObjectStorage::PDFObjects& objects = m_storage.getObjects();
|
||||
auto processEntry = [this, &counter](PDFObjectStorage::Entry& entry)
|
||||
{
|
||||
PDFRemoveSimpleObjectsVisitor visitor(&m_storage, &counter);
|
||||
entry.object.accept(&visitor);
|
||||
entry.object = visitor.getObject();
|
||||
};
|
||||
|
||||
PDFExecutionPolicy::execute(PDFExecutionPolicy::Scope::Unknown, objects.begin(), objects.end(), processEntry);
|
||||
emit optimizationProgress(tr("Simple objects dereferenced and embedded: %1").arg(counter));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue