From 96d2e33692f30dd9d9916bbe0bcb934f1e6936c1 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Thu, 19 Mar 2020 18:17:08 +0100 Subject: [PATCH] Code generator (start) --- CodeGenerator/codegenerator.cpp | 2 +- CodeGenerator/codegenerator.h | 2 + CodeGenerator/main.cpp | 2 + PdfForQtLib/sources/pdfdocument.cpp | 7 + PdfForQtLib/sources/pdfdocument.h | 8 + PdfForQtLib/sources/pdfdocumentbuilder.cpp | 31 ++ PdfForQtLib/sources/pdfdocumentbuilder.h | 16 +- PdfForQtLib/sources/pdfpattern.cpp | 4 +- generated_code_definition.xml | 320 +++++++++++++++++++++ 9 files changed, 388 insertions(+), 4 deletions(-) create mode 100644 generated_code_definition.xml diff --git a/CodeGenerator/codegenerator.cpp b/CodeGenerator/codegenerator.cpp index 375f6be..72cb354 100644 --- a/CodeGenerator/codegenerator.cpp +++ b/CodeGenerator/codegenerator.cpp @@ -634,7 +634,7 @@ void GeneratedBase::performOperation(GeneratedBase::Operation operation) { QObjectList items = parentItem->getItems(); items.removeAll(createdItem); - items.insert(items.indexOf(const_cast(this)), createdItem); + items.insert(items.indexOf(const_cast(this)) + 1, createdItem); parentItem->setItems(qMove(items)); } break; diff --git a/CodeGenerator/codegenerator.h b/CodeGenerator/codegenerator.h index f1cecc2..c9ee168 100644 --- a/CodeGenerator/codegenerator.h +++ b/CodeGenerator/codegenerator.h @@ -128,9 +128,11 @@ public: _PDFInteger, _PDFReal, _PDFObjectReference, + _PDFObject, _QString, _QPointF, _QRectF, + _QColor, _QVariant }; Q_ENUM(DataType) diff --git a/CodeGenerator/main.cpp b/CodeGenerator/main.cpp index cae4c84..d28291c 100644 --- a/CodeGenerator/main.cpp +++ b/CodeGenerator/main.cpp @@ -17,10 +17,12 @@ #include "generatormainwindow.h" +#include #include int main(int argc, char *argv[]) { + qSetGlobalQHashSeed(0); QApplication a(argc, argv); GeneratorMainWindow w; w.show(); diff --git a/PdfForQtLib/sources/pdfdocument.cpp b/PdfForQtLib/sources/pdfdocument.cpp index fd1d8bf..3da7289 100644 --- a/PdfForQtLib/sources/pdfdocument.cpp +++ b/PdfForQtLib/sources/pdfdocument.cpp @@ -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) { const PDFObject& dereferencedObject = m_document->getObject(object); diff --git a/PdfForQtLib/sources/pdfdocument.h b/PdfForQtLib/sources/pdfdocument.h index dfcebe7..4f561a6 100644 --- a/PdfForQtLib/sources/pdfdocument.h +++ b/PdfForQtLib/sources/pdfdocument.h @@ -31,6 +31,7 @@ namespace pdf { class PDFDocument; +class PDFDocumentBuilder; /// 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. @@ -77,6 +78,12 @@ public: /// Returns security handler associated with these objects 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: PDFObjects m_objects; PDFObject m_trailerDictionary; @@ -372,6 +379,7 @@ public: private: friend class PDFDocumentReader; + friend class PDFDocumentBuilder; explicit PDFDocument(PDFObjectStorage&& storage, PDFVersion version) : m_pdfObjectStorage(std::move(storage)) diff --git a/PdfForQtLib/sources/pdfdocumentbuilder.cpp b/PdfForQtLib/sources/pdfdocumentbuilder.cpp index cfc19ca..af9cef6 100644 --- a/PdfForQtLib/sources/pdfdocumentbuilder.cpp +++ b/PdfForQtLib/sources/pdfdocumentbuilder.cpp @@ -62,6 +62,15 @@ void PDFObjectFactory::endDictionaryItem() std::get(dictionaryItem.object).addEntry(qMove(topItem.itemName), qMove(std::get(topItem.object))); } +PDFObject PDFObjectFactory::takeObject() +{ + Q_ASSERT(m_items.size() == 1); + Q_ASSERT(m_items.back().type == ItemType::Object); + PDFObject result = qMove(std::get(m_items.back().object)); + m_items.clear(); + return result; +} + void PDFObjectFactory::addObject(PDFObject object) { if (m_items.empty()) @@ -127,4 +136,26 @@ PDFObjectFactory& PDFObjectFactory::operator<<(PDFObjectReference value) 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 diff --git a/PdfForQtLib/sources/pdfdocumentbuilder.h b/PdfForQtLib/sources/pdfdocumentbuilder.h index 997f629..8166f57 100644 --- a/PdfForQtLib/sources/pdfdocumentbuilder.h +++ b/PdfForQtLib/sources/pdfdocumentbuilder.h @@ -19,6 +19,7 @@ #define PDFDOCUMENTBUILDER_H #include "pdfobject.h" +#include "pdfdocument.h" namespace pdf { @@ -63,6 +64,8 @@ public: return *this; } + PDFObject takeObject(); + private: void addObject(PDFObject object); @@ -111,7 +114,18 @@ private: class PDFDocumentBuilder { 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 diff --git a/PdfForQtLib/sources/pdfpattern.cpp b/PdfForQtLib/sources/pdfpattern.cpp index c03df65..ed7d773 100644 --- a/PdfForQtLib/sources/pdfpattern.cpp +++ b/PdfForQtLib/sources/pdfpattern.cpp @@ -646,7 +646,7 @@ PDFMesh PDFFunctionShading::createMesh(const PDFMeshQualitySettings& settings, c { 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; } } @@ -659,7 +659,7 @@ PDFMesh PDFFunctionShading::createMesh(const PDFMeshQualitySettings& settings, c { 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; } } diff --git a/generated_code_definition.xml b/generated_code_definition.xml new file mode 100644 index 0000000..5b3a812 --- /dev/null +++ b/generated_code_definition.xml @@ -0,0 +1,320 @@ + + + + + + + + + + + + + + page + _PDFObjectReference + Page to which is annotation added + + + + + rectangle + _QRectF + Area in which is rectangle displayed + + + + + borderWidth + _PDFReal + Width of the border line of rectangle + + + + + fillColor + _QColor + Fill color of rectangle (interior color). If you do not want to have area color filled, then use invalid QColor. + + + + + strokeColor + _QColor + Stroke color (color of the rectangle border). If you do not want to have a border, then use invalid QColor. + + + + + title + _QString + Title of the annotation + + + + + subject + _QString + Subject of the annotation (short description of the subject being adressed by the annotation) + + + + + contents + _QString + Contents of the annotation (text displayed, for example, in the marked annotation dialog) + + + Parameters + + _void + + + + + + + + + + + + Subtype + DictionaryItemSimple + WrapName("Square") + + + + + Rect + DictionaryItemSimple + rectangle + + + + + F + DictionaryItemSimple + 4 + + + + + P + DictionaryItemSimple + page + + + + + M + DictionaryItemSimple + WrapCurrentDateTime() + + + + + CreationDate + DictionaryItemSimple + WrapCurrentDateTime() + + + + + Border + DictionaryItemSimple + { 0.0, 0.0, borderWidth } + + + + + C + DictionaryItemSimple + WrapAnnotationColor(strokeColor) + + + + + IC + DictionaryItemSimple + WrapAnnotationColor(fillColor) + + + + + T + DictionaryItemSimple + title + + + + + Contents + DictionaryItemSimple + comment + + + + + Subj + DictionaryItemSimple + subject + + + + Dictionary + + + + CreateObject + annotationObject + _PDFObjectReference + + + + + + Code + + _void + PDFObjectReference popupAnnotation = createAnnotationPopup(page, annotationObject, getPopupWindowRect(rectangle), false); + + + + + + + + Popup + DictionaryItemSimple + popupAnnotation + + + CreateObject + updateAnnotationPopup + _PDFObject + + + + + + Code + + _void + mergeTo(annotationObject, popupAnnotation); +return annotationObject; + + + Annotations + createAnnotationSquare + 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. + _PDFObjectReference + + + + + + + + + + + page + _PDFObjectReference + Page to which is annotation added + + + + + parentAnnotation + _PDFObjectReference + Parent annotation (for which is popup window displayed) + + + + + rectangle + _QRectF + Area on the page, where popup window appears + + + + + opened + _bool + Is the window opened? + + + Parameters + + _void + + + + + + + + + + + + Subtype + DictionaryItemSimple + WrapName("Popup") + + + + + Rect + DictionaryItemSimple + rectangle + + + + + P + DictionaryItemSimple + page + + + + + Parent + DictionaryItemSimple + parentAnnotation + + + + + Open + DictionaryItemSimple + opened + + + + Dictionary + + + + CreateObject + popupAnnotation + _PDFObjectReference + + + + + + Code + + _void + return popupAnnotation; + + + Annotations + createAnnotationPopup + 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. + _PDFObjectReference + + +