diff --git a/src/pdf/PdfExport.cpp b/src/pdf/PdfExport.cpp index b98cb204..8af080b4 100644 --- a/src/pdf/PdfExport.cpp +++ b/src/pdf/PdfExport.cpp @@ -140,7 +140,7 @@ bool PdfExport::writeCrossRef() { } bool PdfExport::writePagesindex() { - this->xref->setXref(0, this->writer->getDataCount()); + this->xref->setXref(1, this->writer->getDataCount()); //Pages root this->writer->write("1 0 obj\n"); this->writer->write("<images; l != NULL; l = l->next) { - // PdfExportImage * img = (PdfExportImage *) l->data; - // this->writer->writef("/I%i %i 0 R\n", i, img->getObjectId()); - // - // i++; - // } - + for (GList * l = this->images; l != NULL; l = l->next) { + PdfExportImage * img = (PdfExportImage *) l->data; + this->writer->writef("/I%i %i 0 R\n", img->imageId, img->objectId); + } return true; } @@ -216,7 +210,6 @@ bool PdfExport::writeFonts() { this->writer->writeObj(); font->objectId = this->writer->getObjectId() - 1; - printf("::objectid1: %i\n", font->objectId); writeObject(font->object, font->doc); this->writer->write("\nendobj\n"); } @@ -224,6 +217,20 @@ bool PdfExport::writeFonts() { return true; } +bool PdfExport::writeImages() { + for (GList * l = this->images; l != NULL; l = l->next) { + PdfExportImage * img = (PdfExportImage *) l->data; + + this->xref->setXref(img->objectId, this->writer->getDataCount()); + bool res = this->writer->writef("%i 0 obj\n", img->objectId); + + writeObject(img->object, img->doc); + this->writer->write("\nendobj\n"); + } + + return true; +} + bool PdfExport::writeCopiedObjects() { bool allWritten = false; while (!allWritten) { @@ -231,7 +238,7 @@ bool PdfExport::writeCopiedObjects() { for (GList * l = g_hash_table_get_values(this->updatedReferenced); l != NULL; l = l->next) { UpdateRef * uref = (UpdateRef *) l->data; if (!uref->wroteOut) { - this->xref->setXref(uref->objectId - 1, this->writer->getDataCount()); + this->xref->setXref(uref->objectId, this->writer->getDataCount()); this->writer->writef("%i 0 obj\n", uref->objectId); writeObject(&uref->object, uref->doc); @@ -255,12 +262,17 @@ bool PdfExport::writeResources() { if (!writeFonts()) { return false; } + + if (!writeImages()) { + return false; + } + if (!writeCopiedObjects()) { return false; } //Resource dictionary - this->xref->setXref(1, this->writer->getDataCount()); + this->xref->setXref(2, this->writer->getDataCount()); this->writer->write("2 0 obj\n"); this->writer->write("<<\n"); @@ -535,17 +547,21 @@ void PdfExport::writeGzStream(Stream * str, GList * replacementList) { str->reset(); } -int PdfExport::lookupImage(String name, Ref ref) { +int PdfExport::lookupImage(String name, Ref ref, Object * object) { for (GList * l = this->images; l != NULL; l = l->next) { PdfExportImage * i = (PdfExportImage *) l->data; if (i->equalsRef(ref)) { + delete object; return i->objectId; } } int id = this->imageId++; - this->images = g_list_append(this->images, new PdfExportImage(id, ref)); + + PdfExportImage * img = new PdfExportImage(this->writer->getNextObjectId(), ref, object, id, this->currentPdfDoc); + this->xref->addXref(0); + this->images = g_list_append(this->images, img); return id; } @@ -719,9 +735,12 @@ bool PdfExport::addPopplerPage(XojPopplerPage * pdf, XojPopplerDocument doc) { Dict * xobjectDict = o.getDict(); for (int u = 0; u < xobjectDict->getLength(); u++) { Object xobjectObject; + + Object * objImage = new Object(); xobjectDict->getValNF(u, &xobjectObject); + xobjectDict->getVal(u, objImage); - int image = this->lookupImage(xobjectDict->getKey(u), xobjectObject.getRef()); + int image = this->lookupImage(xobjectDict->getKey(u), xobjectObject.getRef(), objImage); RefReplacement * replacement = new RefReplacement(xobjectDict->getKey(u), image, 'I'); replacementList = g_list_append(replacementList, replacement); diff --git a/src/pdf/PdfExport.h b/src/pdf/PdfExport.h index 4c20f171..9ee08bcc 100644 --- a/src/pdf/PdfExport.h +++ b/src/pdf/PdfExport.h @@ -49,7 +49,7 @@ private: void writeStream(const char * str, int len, GList * replacementList); int lookupFont(String name, Ref ref); - int lookupImage(String name, Ref ref); + int lookupImage(String name, Ref ref, Object * object); bool parseFooter(); bool writeFooter(); @@ -63,6 +63,7 @@ private: bool writeResources(); bool writeFonts(); + bool writeImages(); bool writeCopiedObjects(); private: diff --git a/src/pdf/PdfExportImage.h b/src/pdf/PdfExportImage.h index cd38663e..19b1156d 100644 --- a/src/pdf/PdfExportImage.h +++ b/src/pdf/PdfExportImage.h @@ -16,12 +16,19 @@ class PdfExportImage { public: - PdfExportImage(int objectId, Ref ref) { + PdfExportImage(int objectId, Ref ref, Object * object, int imageId, XojPopplerDocument doc) { this->objectId = objectId; this->ref = ref; + this->imageId = imageId; + this->doc = doc; + this->object = object; } ~PdfExportImage() { + if (this->object) { + delete this->object; + } + this->object = NULL; } bool equalsRef(const Ref & ref) { @@ -30,7 +37,10 @@ public: public: int objectId; + int imageId; Ref ref; + XojPopplerDocument doc; + Object * object; }; #endif /* __PDFEXPORTIMAGE_H__ */ diff --git a/src/pdf/PdfXRef.cpp b/src/pdf/PdfXRef.cpp index 47ac9208..906d5048 100644 --- a/src/pdf/PdfXRef.cpp +++ b/src/pdf/PdfXRef.cpp @@ -21,11 +21,15 @@ void PdfXRef::addXref(int ref) { } void PdfXRef::setXref(int id, int ref) { - if (id >= this->xrefNr) { + if (id < 1) { + g_warning("PdfXRef::setXref try to set XREF-ID: %i it needs to be at least 1", id, this->xrefNr); + return; + } + if (id - 1 >= this->xrefNr) { g_warning("PdfXRef::setXref try to set XREF-ID: %i but there are only %i", id, this->xrefNr); return; } - this->xref[id] = ref; + this->xref[id - 1] = ref; } int PdfXRef::getXrefCount() {