Code generator (start)

This commit is contained in:
Jakub Melka 2020-03-19 18:17:08 +01:00
parent 36cdb41cdb
commit 96d2e33692
9 changed files with 388 additions and 4 deletions

View File

@ -634,7 +634,7 @@ void GeneratedBase::performOperation(GeneratedBase::Operation operation)
{ {
QObjectList items = parentItem->getItems(); QObjectList items = parentItem->getItems();
items.removeAll(createdItem); items.removeAll(createdItem);
items.insert(items.indexOf(const_cast<codegen::GeneratedBase*>(this)), createdItem); items.insert(items.indexOf(const_cast<codegen::GeneratedBase*>(this)) + 1, createdItem);
parentItem->setItems(qMove(items)); parentItem->setItems(qMove(items));
} }
break; break;

View File

@ -128,9 +128,11 @@ public:
_PDFInteger, _PDFInteger,
_PDFReal, _PDFReal,
_PDFObjectReference, _PDFObjectReference,
_PDFObject,
_QString, _QString,
_QPointF, _QPointF,
_QRectF, _QRectF,
_QColor,
_QVariant _QVariant
}; };
Q_ENUM(DataType) Q_ENUM(DataType)

View File

@ -17,10 +17,12 @@
#include "generatormainwindow.h" #include "generatormainwindow.h"
#include <QHash>
#include <QApplication> #include <QApplication>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qSetGlobalQHashSeed(0);
QApplication a(argc, argv); QApplication a(argc, argv);
GeneratorMainWindow w; GeneratorMainWindow w;
w.show(); w.show();

View File

@ -225,6 +225,13 @@ const PDFObject& PDFObjectStorage::getObject(PDFObjectReference reference) const
} }
} }
PDFObjectReference PDFObjectStorage::addObject(PDFObject object)
{
PDFObjectReference reference(m_objects.size(), 0);
m_objects.emplace_back(0, qMove(object));
return reference;
}
QByteArray PDFDocumentDataLoaderDecorator::readName(const PDFObject& object) QByteArray PDFDocumentDataLoaderDecorator::readName(const PDFObject& object)
{ {
const PDFObject& dereferencedObject = m_document->getObject(object); const PDFObject& dereferencedObject = m_document->getObject(object);

View File

@ -31,6 +31,7 @@
namespace pdf namespace pdf
{ {
class PDFDocument; class PDFDocument;
class PDFDocumentBuilder;
/// Storage for objects. This class is not thread safe for writing (calling non-const functions). Caller must ensure /// Storage for objects. This class is not thread safe for writing (calling non-const functions). Caller must ensure
/// locking, if this object is used from multiple threads. Calling const functions should be thread safe. /// locking, if this object is used from multiple threads. Calling const functions should be thread safe.
@ -77,6 +78,12 @@ public:
/// Returns security handler associated with these objects /// Returns security handler associated with these objects
const PDFSecurityHandler* getSecurityHandler() const { return m_securityHandler.data(); } const PDFSecurityHandler* getSecurityHandler() const { return m_securityHandler.data(); }
/// Adds a new object to the object list. This function
/// is not thread safe, do not call it from multiple threads.
/// \param object Object to be added
/// \returns Reference to new object
PDFObjectReference addObject(PDFObject object);
private: private:
PDFObjects m_objects; PDFObjects m_objects;
PDFObject m_trailerDictionary; PDFObject m_trailerDictionary;
@ -372,6 +379,7 @@ public:
private: private:
friend class PDFDocumentReader; friend class PDFDocumentReader;
friend class PDFDocumentBuilder;
explicit PDFDocument(PDFObjectStorage&& storage, PDFVersion version) : explicit PDFDocument(PDFObjectStorage&& storage, PDFVersion version) :
m_pdfObjectStorage(std::move(storage)) m_pdfObjectStorage(std::move(storage))

View File

@ -62,6 +62,15 @@ void PDFObjectFactory::endDictionaryItem()
std::get<PDFDictionary>(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get<PDFObject>(topItem.object))); std::get<PDFDictionary>(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get<PDFObject>(topItem.object)));
} }
PDFObject PDFObjectFactory::takeObject()
{
Q_ASSERT(m_items.size() == 1);
Q_ASSERT(m_items.back().type == ItemType::Object);
PDFObject result = qMove(std::get<PDFObject>(m_items.back().object));
m_items.clear();
return result;
}
void PDFObjectFactory::addObject(PDFObject object) void PDFObjectFactory::addObject(PDFObject object)
{ {
if (m_items.empty()) if (m_items.empty())
@ -127,4 +136,26 @@ PDFObjectFactory& PDFObjectFactory::operator<<(PDFObjectReference value)
return *this; return *this;
} }
PDFDocumentBuilder::PDFDocumentBuilder() :
m_version(1, 7)
{
}
PDFDocumentBuilder::PDFDocumentBuilder(const PDFDocument* document) :
m_storage(document->getStorage()),
m_version(document->getInfo()->version)
{
}
PDFDocument PDFDocumentBuilder::build() const
{
return PDFDocument(PDFObjectStorage(m_storage), m_version);
}
/* START GENERATED CODE */
/* END GENERATED CODE */
} // namespace pdf } // namespace pdf

View File

@ -19,6 +19,7 @@
#define PDFDOCUMENTBUILDER_H #define PDFDOCUMENTBUILDER_H
#include "pdfobject.h" #include "pdfobject.h"
#include "pdfdocument.h"
namespace pdf namespace pdf
{ {
@ -63,6 +64,8 @@ public:
return *this; return *this;
} }
PDFObject takeObject();
private: private:
void addObject(PDFObject object); void addObject(PDFObject object);
@ -111,7 +114,18 @@ private:
class PDFDocumentBuilder class PDFDocumentBuilder
{ {
public: public:
PDFDocumentBuilder(); explicit PDFDocumentBuilder();
explicit PDFDocumentBuilder(const PDFDocument* document);
PDFDocument build() const;
/* START GENERATED CODE */
/* END GENERATED CODE */
private:
PDFObjectStorage m_storage;
PDFVersion m_version;
}; };
} // namespace pdf } // namespace pdf

View File

@ -646,7 +646,7 @@ PDFMesh PDFFunctionShading::createMesh(const PDFMeshQualitySettings& settings, c
{ {
if (std::fabs(sourceColorBuffer[colorComponentIndex + i] - sourceColorBuffer[colorOtherComponentIndex + i]) > settings.tolerance) if (std::fabs(sourceColorBuffer[colorComponentIndex + i] - sourceColorBuffer[colorOtherComponentIndex + i]) > settings.tolerance)
{ {
isMeshOK.store(std::memory_order_relaxed); isMeshOK.store(false, std::memory_order_relaxed);
return; return;
} }
} }
@ -659,7 +659,7 @@ PDFMesh PDFFunctionShading::createMesh(const PDFMeshQualitySettings& settings, c
{ {
if (std::fabs(sourceColorBuffer[colorComponentIndex + i] - sourceColorBuffer[colorOtherComponentIndex + i]) > settings.tolerance) if (std::fabs(sourceColorBuffer[colorComponentIndex + i] - sourceColorBuffer[colorOtherComponentIndex + i]) > settings.tolerance)
{ {
isMeshOK.store(std::memory_order_relaxed); isMeshOK.store(false, std::memory_order_relaxed);
return; return;
} }
} }

View File

@ -0,0 +1,320 @@
<?xml version="1.0" encoding="UTF-8"?>
<root class="codegen::GeneratedCodeStorage">
<property name="objectName"></property>
<property name="functions">
<QObject class="codegen::GeneratedFunction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">page</property>
<property name="parameterType">_PDFObjectReference</property>
<property name="parameterDescription">Page to which is annotation added</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">rectangle</property>
<property name="parameterType">_QRectF</property>
<property name="parameterDescription">Area in which is rectangle displayed</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">borderWidth</property>
<property name="parameterType">_PDFReal</property>
<property name="parameterDescription">Width of the border line of rectangle</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">fillColor</property>
<property name="parameterType">_QColor</property>
<property name="parameterDescription">Fill color of rectangle (interior color). If you do not want to have area color filled, then use invalid QColor.</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">strokeColor</property>
<property name="parameterType">_QColor</property>
<property name="parameterDescription">Stroke color (color of the rectangle border). If you do not want to have a border, then use invalid QColor.</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">title</property>
<property name="parameterType">_QString</property>
<property name="parameterDescription">Title of the annotation</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">subject</property>
<property name="parameterType">_QString</property>
<property name="parameterDescription">Subject of the annotation (short description of the subject being adressed by the annotation)</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">contents</property>
<property name="parameterType">_QString</property>
<property name="parameterDescription">Contents of the annotation (text displayed, for example, in the marked annotation dialog)</property>
</QObject>
</property>
<property name="actionType">Parameters</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Subtype</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapName("Square")</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Rect</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">rectangle</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">F</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">4</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">P</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">page</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">M</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapCurrentDateTime()</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">CreationDate</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapCurrentDateTime()</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Border</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">{ 0.0, 0.0, borderWidth }</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">C</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapAnnotationColor(strokeColor)</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">IC</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapAnnotationColor(fillColor)</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">T</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">title</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Contents</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">comment</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Subj</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">subject</property>
</QObject>
</property>
<property name="dictionaryItemName"></property>
<property name="objectType">Dictionary</property>
<property name="value"></property>
</QObject>
</property>
<property name="actionType">CreateObject</property>
<property name="variableName">annotationObject</property>
<property name="variableType">_PDFObjectReference</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items"/>
<property name="actionType">Code</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code">PDFObjectReference popupAnnotation = createAnnotationPopup(page, annotationObject, getPopupWindowRect(rectangle), false);</property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Popup</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">popupAnnotation</property>
</QObject>
</property>
<property name="actionType">CreateObject</property>
<property name="variableName">updateAnnotationPopup</property>
<property name="variableType">_PDFObject</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items"/>
<property name="actionType">Code</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code">mergeTo(annotationObject, popupAnnotation);
return annotationObject;</property>
</QObject>
</property>
<property name="functionType">Annotations</property>
<property name="functionName">createAnnotationSquare</property>
<property name="functionDescription">Square annotation displays rectangle (or square). When opened, they display pop-up window containing the text of associated note (and window title). Square border/fill color can be defined, along with border width.</property>
<property name="returnType">_PDFObjectReference</property>
</QObject>
<QObject class="codegen::GeneratedFunction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">page</property>
<property name="parameterType">_PDFObjectReference</property>
<property name="parameterDescription">Page to which is annotation added</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">parentAnnotation</property>
<property name="parameterType">_PDFObjectReference</property>
<property name="parameterDescription">Parent annotation (for which is popup window displayed)</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">rectangle</property>
<property name="parameterType">_QRectF</property>
<property name="parameterDescription">Area on the page, where popup window appears</property>
</QObject>
<QObject class="codegen::GeneratedParameter">
<property name="objectName"></property>
<property name="items"/>
<property name="parameterName">opened</property>
<property name="parameterType">_bool</property>
<property name="parameterDescription">Is the window opened?</property>
</QObject>
</property>
<property name="actionType">Parameters</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items">
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Subtype</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">WrapName("Popup")</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Rect</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">rectangle</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">P</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">page</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Parent</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">parentAnnotation</property>
</QObject>
<QObject class="codegen::GeneratedPDFObject">
<property name="objectName"></property>
<property name="items"/>
<property name="dictionaryItemName">Open</property>
<property name="objectType">DictionaryItemSimple</property>
<property name="value">opened</property>
</QObject>
</property>
<property name="dictionaryItemName"></property>
<property name="objectType">Dictionary</property>
<property name="value"></property>
</QObject>
</property>
<property name="actionType">CreateObject</property>
<property name="variableName">popupAnnotation</property>
<property name="variableType">_PDFObjectReference</property>
<property name="code"></property>
</QObject>
<QObject class="codegen::GeneratedAction">
<property name="objectName"></property>
<property name="items"/>
<property name="actionType">Code</property>
<property name="variableName"></property>
<property name="variableType">_void</property>
<property name="code">return popupAnnotation;</property>
</QObject>
</property>
<property name="functionType">Annotations</property>
<property name="functionName">createAnnotationPopup</property>
<property name="functionDescription">Creates a new popup annotation on the page. Popup annotation is represented usually by floating window, which can be opened, or closed. Popup annotation is associated with parent annotation, which can be usually markup annotation. Popup annotation displays parent annotation's texts, for example, title, comment, date etc.</property>
<property name="returnType">_PDFObjectReference</property>
</QObject>
</property>
</root>