// Copyright (C) 2020 Jakub Melka // // 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 . #include "pdfdocumentbuilder.h" namespace pdf { void PDFObjectFactory::beginArray() { m_items.emplace_back(ItemType::Array, PDFArray()); } void PDFObjectFactory::endArray() { Item topItem = qMove(m_items.back()); Q_ASSERT(topItem.type == ItemType::Array); m_items.pop_back(); addObject(PDFObject::createArray(std::make_shared(qMove(std::get(topItem.object))))); } void PDFObjectFactory::beginDictionary() { m_items.emplace_back(ItemType::Dictionary, PDFDictionary()); } void PDFObjectFactory::endDictionary() { Item topItem = qMove(m_items.back()); Q_ASSERT(topItem.type == ItemType::Dictionary); m_items.pop_back(); addObject(PDFObject::createDictionary(std::make_shared(qMove(std::get(topItem.object))))); } void PDFObjectFactory::beginDictionaryItem(const QByteArray& name) { m_items.emplace_back(ItemType::DictionaryItem, name, PDFObject()); } void PDFObjectFactory::endDictionaryItem() { Item topItem = qMove(m_items.back()); Q_ASSERT(topItem.type == ItemType::DictionaryItem); m_items.pop_back(); Item& dictionaryItem = m_items.back(); Q_ASSERT(dictionaryItem.type == ItemType::Dictionary); std::get(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get(topItem.object))); } void PDFObjectFactory::addObject(PDFObject object) { if (m_items.empty()) { m_items.emplace_back(ItemType::Object, qMove(object)); return; } Item& topItem = m_items.back(); switch (topItem.type) { case ItemType::Object: // Just override the object topItem.object = qMove(object); break; case ItemType::Dictionary: // Do not do anything - we are inside dictionary break; case ItemType::DictionaryItem: // Add item to dictionary item topItem.object = qMove(object); break; case ItemType::Array: std::get(topItem.object).appendItem(qMove(object)); break; default: Q_ASSERT(false); break; } } PDFObjectFactory& PDFObjectFactory::operator<<(std::nullptr_t) { addObject(PDFObject::createNull()); return *this; } PDFObjectFactory& PDFObjectFactory::operator<<(bool value) { addObject(PDFObject::createBool(value)); return *this; } PDFObjectFactory& PDFObjectFactory::operator<<(PDFReal value) { addObject(PDFObject::createReal(value)); return *this; } PDFObjectFactory& PDFObjectFactory::operator<<(PDFInteger value) { addObject(PDFObject::createInteger(value)); return *this; } PDFObjectFactory& PDFObjectFactory::operator<<(PDFObjectReference value) { addObject(PDFObject::createReference(value)); return *this; } } // namespace pdf