> > [PATCH 5/7] Added Array::remove (and Object::arrayRemove)
> Please add a return if the range is wrong and DEBUG_MEM is not defined.
> Also please use a memmove instead of a for loop to move the elems to the left
> > [PATCH 6/7] Basic support for Annot appearance stream removal and
> > invalidation poppler/Annot.cc
> Please add braces {} to single line if/elses
> > [PATCH 7/7] Annotation removal
> Same thing about braces here
Thank you Albert. These are the patches with the corrections you suggested.
From 6e9f42387013fe5c385edac169500be7f4c6d59c Mon Sep 17 00:00:00 2001
From: Fabio D'Urso <[email protected]>
Date: Mon, 19 Mar 2012 00:05:49 +0100
Subject: [PATCH 1/3] Added Array::remove (and Object::arrayRemove)
---
poppler/Array.cc | 13 +++++++++++++
poppler/Array.h | 4 ++++
poppler/Object.h | 5 +++++
3 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/poppler/Array.cc b/poppler/Array.cc
index e7ec4e7..cb22432 100644
--- a/poppler/Array.cc
+++ b/poppler/Array.cc
@@ -14,6 +14,7 @@
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <[email protected]>
+// Copyright (C) 2012 Fabio D'Urso <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -64,6 +65,18 @@ void Array::add(Object *elem) {
++length;
}
+void Array::remove(int i) {
+ if (i < 0 || i >= length) {
+#ifdef DEBUG_MEM
+ abort();
+#else
+ return;
+#endif
+ }
+ --length;
+ memmove( elems + i, elems + i + 1, sizeof(elems[0]) * (length - i) );
+}
+
Object *Array::get(int i, Object *obj) {
if (i < 0 || i >= length) {
#ifdef DEBUG_MEM
diff --git a/poppler/Array.h b/poppler/Array.h
index a6aca26..666a409 100644
--- a/poppler/Array.h
+++ b/poppler/Array.h
@@ -14,6 +14,7 @@
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <[email protected]>
+// Copyright (C) 2012 Fabio D'Urso <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -54,6 +55,9 @@ public:
// Add an element.
void add(Object *elem);
+ // Remove an element by position
+ void remove(int i);
+
// Accessors.
Object *get(int i, Object *obj);
Object *getNF(int i, Object *obj);
diff --git a/poppler/Object.h b/poppler/Object.h
index a67b403..c885d73 100644
--- a/poppler/Object.h
+++ b/poppler/Object.h
@@ -17,6 +17,7 @@
// Copyright (C) 2008 Kees Cook <[email protected]>
// Copyright (C) 2008, 2010 Albert Astals Cid <[email protected]>
// Copyright (C) 2009 Jakub Wilk <[email protected]>
+// Copyright (C) 2012 Fabio D'Urso <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
@@ -205,6 +206,7 @@ public:
// Array accessors.
int arrayGetLength();
void arrayAdd(Object *elem);
+ void arrayRemove(int i);
Object *arrayGet(int i, Object *obj);
Object *arrayGetNF(int i, Object *obj);
@@ -273,6 +275,9 @@ inline int Object::arrayGetLength()
inline void Object::arrayAdd(Object *elem)
{ OBJECT_TYPE_CHECK(objArray); array->add(elem); }
+inline void Object::arrayRemove(int i)
+ { OBJECT_TYPE_CHECK(objArray); array->remove(i); }
+
inline Object *Object::arrayGet(int i, Object *obj)
{ OBJECT_TYPE_CHECK(objArray); return array->get(i, obj); }
--
1.7.6.5
From 3a42a1a0f018a318b44bb7bf48e5b059ed796f18 Mon Sep 17 00:00:00 2001
From: Fabio D'Urso <[email protected]>
Date: Mon, 19 Mar 2012 19:10:20 +0100
Subject: [PATCH 2/3] Basic support for Annot appearance stream removal and
invalidation
---
poppler/Annot.cc | 176 +++++++++++++++++++++++++++++++++++++++--------------
poppler/Annot.h | 26 +++++++-
2 files changed, 152 insertions(+), 50 deletions(-)
diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 405f5f6..4b4d0bd 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -776,6 +776,98 @@ AnnotIconFit::AnnotIconFit(Dict* dict) {
}
//------------------------------------------------------------------------
+// AnnotAppearance
+//------------------------------------------------------------------------
+
+AnnotAppearance::AnnotAppearance(PDFDoc *docA, Dict *dict) {
+ xref = docA->getXRef();
+ appearDict = new Dict(dict);
+}
+
+void AnnotAppearance::getAppearanceStream(AnnotAppearance::AnnotAppearanceType type, const char *state, Object *dest) {
+ Object apData;
+ // Obtain dictionary or stream associated to appearance type
+ if (type == appearRollover && appearDict->hasKey("R")) {
+ appearDict->lookupNF("R", &apData);
+ } else if (type == appearDown && appearDict->hasKey("D")) {
+ appearDict->lookupNF("D", &apData);
+ } else {
+ appearDict->lookupNF("N", &apData); // Normal appearance
+ }
+
+ // Search state if it's a subdictionary
+ if (apData.isDict() && state) {
+ Object obj1;
+ apData.dictLookupNF(state, &obj1);
+ apData.free();
+ obj1.copy(&apData);
+ obj1.free();
+ }
+
+ if (apData.isRef()) {
+ apData.copy(dest); // Return reference to stream
+ } else {
+ dest->initNull();
+ }
+
+ apData.free();
+}
+
+GooString * AnnotAppearance::getStateKey(int i) {
+ Object obj1;
+ GooString * res = NULL;
+ appearDict->lookupNF("N", &obj1);
+ if (obj1.isDict()) {
+ res = new GooString(obj1.dictGetKey(i));
+ }
+ obj1.free();
+ return res;
+}
+
+int AnnotAppearance::getNumStates() {
+ Object obj1;
+ int res = 0;
+ appearDict->lookupNF("N", &obj1);
+ if (obj1.isDict()) {
+ res = obj1.dictGetLength();
+ }
+ obj1.free();
+ return res;
+}
+
+// Removes stream if obj is a Ref, or removes pointed streams if obj is a Dict
+void AnnotAppearance::removeStateStreams(Object *obj1) {
+ // TODO: Remove XObject resources, check for XObjects shared by multiple
+ // annotations, delete streams from name table (if any)
+ if (obj1->isRef()) {
+ xref->removeIndirectObject(obj1->getRef());
+ } else if (obj1->isDict()) {
+ const int size = obj1->dictGetLength();
+ for (int i = 0; i < size; ++i) {
+ Object obj2;
+ obj1->dictGetValNF(i, &obj2);
+ if (obj2.isRef()) {
+ xref->removeIndirectObject(obj2.getRef());
+ }
+ obj2.free();
+ }
+ }
+}
+
+void AnnotAppearance::removeAllStreams() {
+ Object obj1;
+ appearDict->lookupNF("N", &obj1);
+ removeStateStreams(&obj1);
+ obj1.free();
+ appearDict->lookupNF("R", &obj1);
+ removeStateStreams(&obj1);
+ obj1.free();
+ appearDict->lookupNF("D", &obj1);
+ removeStateStreams(&obj1);
+ obj1.free();
+}
+
+//------------------------------------------------------------------------
// AnnotAppearanceCharacs
//------------------------------------------------------------------------
@@ -912,13 +1004,12 @@ Annot::Annot(PDFDoc *docA, Dict *dict, Object *obj) {
}
void Annot::initialize(PDFDoc *docA, Dict *dict) {
- Object apObj, asObj, obj1, obj2, obj3;
+ Object apObj, asObj, obj1, obj2;
- appRef.num = 0;
- appRef.gen = 65535;
ok = gTrue;
doc = docA;
xref = doc->getXRef();
+ appearDict = NULL;
appearState = NULL;
appearBuf = NULL;
fontSize = 0;
@@ -995,22 +1086,25 @@ void Annot::initialize(PDFDoc *docA, Dict *dict) {
}
obj1.free();
- //----- get the appearance state
-
+ //----- get the annotation appearance dictionary
dict->lookup("AP", &apObj);
+ if (apObj.isDict()) {
+ appearDict = new AnnotAppearance(doc, apObj.getDict());
+ }
+ apObj.free();
+
+ //----- get the appearance state
dict->lookup("AS", &asObj);
if (asObj.isName()) {
appearState = new GooString(asObj.getName());
- } else if (apObj.isDict()) {
- if (apObj.dictLookup("N", &obj1)->isDict()) {
- error (errSyntaxError, -1, "Invalid or missing AS value in annotation containing one or more appearance subdictionaries");
- // AS value is required in this case, but if the
- // N dictionary contains only one entry
- // take it as default appearance.
- if (obj1.dictGetLength() == 1)
- appearState = new GooString(obj1.dictGetKey(0));
+ } else if (appearDict && appearDict->getNumStates() != 0) {
+ error (errSyntaxError, -1, "Invalid or missing AS value in annotation containing one or more appearance subdictionaries");
+ // AS value is required in this case, but if the
+ // N dictionary contains only one entry
+ // take it as default appearance.
+ if (appearDict->getNumStates() == 1) {
+ appearState = appearDict->getStateKey(0);
}
- obj1.free();
}
if (!appearState) {
appearState = new GooString("Off");
@@ -1018,22 +1112,9 @@ void Annot::initialize(PDFDoc *docA, Dict *dict) {
asObj.free();
//----- get the annotation appearance
-
- if (apObj.isDict()) {
- apObj.dictLookup("N", &obj1);
- apObj.dictLookupNF("N", &obj2);
- if (obj1.isDict()) {
- if (obj1.dictLookupNF(appearState->getCString(), &obj3)->isRef()) {
- obj3.copy(&appearance);
- }
- obj3.free();
- } else if (obj2.isRef()) {
- obj2.copy(&appearance);
- }
- obj1.free();
- obj2.free();
+ if (appearDict) {
+ appearDict->getAppearanceStream(AnnotAppearance::appearNormal, appearState->getCString(), &appearance);
}
- apObj.free();
//----- parse the border style
if (dict->lookup("BS", &obj1)->isDict()) {
@@ -1218,9 +1299,6 @@ void Annot::setAppearanceState(const char *state) {
if (!state)
return;
- if (appearState && appearState->cmp(state) == 0)
- return;
-
delete appearState;
appearState = new GooString(state);
@@ -1229,23 +1307,25 @@ void Annot::setAppearanceState(const char *state) {
update ("AS", &obj1);
// The appearance state determines the current appearance stream
- Object obj2;
- if (annotObj.dictLookup("AP", &obj2)->isDict()) {
- Object obj3;
-
- if (obj2.dictLookup("N", &obj3)->isDict()) {
- Object obj4;
+ appearance.free();
+ if (appearDict) {
+ appearDict->getAppearanceStream(AnnotAppearance::appearNormal, appearState->getCString(), &appearance);
+ } else {
+ appearance.initNull();
+ }
+}
- appearance.free();
- if (obj3.dictLookupNF(state, &obj4)->isRef())
- obj4.copy(&appearance);
- else
- appearance.initNull();
- obj4.free();
- }
- obj3.free();
+void Annot::invalidateAppearance() {
+ if (appearDict) { // Remove existing appearance streams
+ appearDict->removeAllStreams();
}
- obj2.free();
+ delete appearDict;
+ appearDict = NULL;
+ Object obj1;
+ obj1.initNull();
+ update ("AP", &obj1); // Removes AP
+ update ("AS", &obj1); // Removes AS
+ setAppearanceState("Off"); // Default value
}
double Annot::getXMin() {
@@ -1292,6 +1372,8 @@ Annot::~Annot() {
if (modified)
delete modified;
+ delete appearDict;
+
appearance.free();
if (appearState)
diff --git a/poppler/Annot.h b/poppler/Annot.h
index 8d40a8a..9e2754a 100644
--- a/poppler/Annot.h
+++ b/poppler/Annot.h
@@ -368,7 +368,25 @@ public:
appearDown
};
- AnnotAppearance(Dict *dict);
+ AnnotAppearance(PDFDoc *docA, Dict *dict);
+
+ // State is ignored if no subdictionary is present
+ void getAppearanceStream(AnnotAppearanceType type, const char *state, Object *dest);
+
+ // Access keys in normal appearance subdictionary (N)
+ GooString * getStateKey(int i);
+ int getNumStates();
+
+ // Removes all associated streams in the xref table. Caller is required to
+ // reset parent annotation's AP and AS after this call.
+ void removeAllStreams();
+
+private:
+ void removeStateStreams(Object *state);
+
+protected:
+ XRef *xref; // the xref table for this PDF file
+ Dict *appearDict; // Annotation's AP
};
//------------------------------------------------------------------------
@@ -504,6 +522,9 @@ public:
void setAppearanceState(const char *state);
+ // Delete appearance stream and reset appearance state
+ void invalidateAppearance();
+
// getters
PDFDoc *getDoc() const { return doc; }
XRef *getXRef() const { return xref; }
@@ -564,8 +585,7 @@ protected:
GooString *name; // NM
GooString *modified; // M
Guint flags; // F (must be a 32 bit unsigned int)
- //Dict *appearDict; // AP (should be correctly parsed)
- Ref appRef; //the reference to the indirect appearance object in XRef
+ AnnotAppearance *appearDict; // AP
Object appearance; // a reference to the Form XObject stream
// for the normal appearance
GooString *appearState; // AS
--
1.7.6.5
From 0c9baf1d467ef5396a6ffaa93f9022d5b5bb51df Mon Sep 17 00:00:00 2001
From: Fabio D'Urso <[email protected]>
Date: Tue, 20 Mar 2012 00:56:50 +0100
Subject: [PATCH 3/3] Annotation removal
---
poppler/Annot.cc | 17 +++++++++++++++++
poppler/Annot.h | 1 +
poppler/Page.cc | 36 ++++++++++++++++++++++++++++++++++++
poppler/Page.h | 2 ++
4 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index 4b4d0bd..6dd82c3 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -5712,6 +5712,23 @@ void Annots::appendAnnot(Annot *annot) {
}
}
+GBool Annots::removeAnnot(Annot *annot) {
+ int idx = -1;
+ // Search annot and remove it by swapping with last element
+ for (int i = 0; idx == -1 && i < nAnnots; i++) {
+ if (annots[i] == annot) {
+ idx = i;
+ }
+ }
+ if (idx == -1) {
+ return gFalse;
+ } else {
+ annots[idx] = annots[--nAnnots];
+ annot->decRefCnt();
+ return gTrue;
+ }
+}
+
Annot *Annots::createAnnot(Dict* dict, Object *obj) {
Annot *annot = NULL;
Object obj1;
diff --git a/poppler/Annot.h b/poppler/Annot.h
index 9e2754a..534c00e 100644
--- a/poppler/Annot.h
+++ b/poppler/Annot.h
@@ -1329,6 +1329,7 @@ public:
int getNumAnnots() { return nAnnots; }
Annot *getAnnot(int i) { return annots[i]; }
void appendAnnot(Annot *annot);
+ GBool removeAnnot(Annot *annot);
private:
Annot* createAnnot(Dict* dict, Object *obj);
diff --git a/poppler/Page.cc b/poppler/Page.cc
index 48ed647..a2a4a0d 100644
--- a/poppler/Page.cc
+++ b/poppler/Page.cc
@@ -394,6 +394,42 @@ void Page::addAnnot(Annot *annot) {
annot->setPage(&pageRef, num);
}
+// Note: Caller is responsible for deleting popup and appearance streams too
+void Page::removeAnnot(Annot *annot) {
+ Ref annotRef = annot->getRef();
+ Object annArray;
+
+ getAnnots(&annArray);
+ if (annArray.isArray()) {
+ int idx = -1;
+ // Get annotation position
+ for (int i = 0; idx == -1 && i < annArray.arrayGetLength(); ++i) {
+ Object tmp;
+ Ref currAnnot = annArray.arrayGetNF(i, &tmp)->getRef();
+ tmp.free();
+ if (currAnnot.num == annotRef.num && currAnnot.gen == annotRef.gen) {
+ idx = i;
+ }
+ }
+
+ if (idx == -1) {
+ error(errInternal, -1, "Annotation doesn't belong to this page");
+ annArray.free();
+ return;
+ }
+ annots->removeAnnot(annot); // Gracefully fails on popup windows
+ annArray.arrayRemove(idx);
+ xref->removeIndirectObject(annotRef);
+
+ if (annotsObj.isRef()) {
+ xref->setModifiedObject (&annArray, annotsObj.getRef());
+ } else {
+ xref->setModifiedObject (&pageObj, pageRef);
+ }
+ }
+ annArray.free();
+}
+
Links *Page::getLinks() {
return new Links(getAnnots());
}
diff --git a/poppler/Page.h b/poppler/Page.h
index 70141d0..e22353b 100644
--- a/poppler/Page.h
+++ b/poppler/Page.h
@@ -171,6 +171,8 @@ public:
Object *getAnnots(Object *obj) { return annotsObj.fetch(xref, obj); }
// Add a new annotation to the page
void addAnnot(Annot *annot);
+ // Remove an existing annotation from the page
+ void removeAnnot(Annot *annot);
// Return a list of links.
Links *getLinks();
--
1.7.6.5
_______________________________________________
poppler mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/poppler