Index: glib/poppler-document.cc =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler-document.cc,v retrieving revision 1.37 diff -u -8 -p -r1.37 poppler-document.cc --- glib/poppler-document.cc 19 May 2006 22:12:38 -0000 1.37 +++ glib/poppler-document.cc 16 Jul 2006 20:18:16 -0000 @@ -1343,8 +1343,58 @@ poppler_ps_file_set_duplex (PopplerPSFil * **/ void poppler_ps_file_free (PopplerPSFile *ps_file) { g_return_if_fail (ps_file != NULL); g_object_unref (ps_file); } + +void +poppler_document_set_form_field_content (PopplerDocument *document, int id, char *content) +{ + Catalog *catalog = document->doc->getCatalog(); + int numPages = catalog->getNumPages(); + for (int i=1; i<=numPages; i++) { + Form *form = catalog->getPage(i)->getForm(); + FormField *field = form->getFieldById(id); + if (field) { + field->setContentCopy(content); + document->doc->addIncrementalObject(field->getObj(), field->getRef()); + document->doc->getXRef()->setModifiedObject(field->getObj(), field->getRef()); + return; + } + } +} + +gchar * +poppler_document_get_form_field_content (PopplerDocument *document, int id) +{ + PopplerFormField *field = poppler_document_find_form_field_by_id(document, id); + if (field) + return field->content; + else { + g_warning("poppler_document_get_form_field_content, unknown id: %i", id); + return NULL; + } +} + +PopplerFormField * +poppler_document_find_form_field_by_id (PopplerDocument *document, int id) +{ + Catalog *catalog = document->doc->getCatalog(); + //decode id + int pageNum = id >> 16; + int fieldNum = (id << 16) >> 16; + FormField *field = catalog->getPage(pageNum)->getForm()->getField(fieldNum); + if (field != NULL) { + PopplerFormField *poppler_field; + poppler_field = g_new(PopplerFormField, 1); + field->getRect ((&poppler_field->area.x1), &(poppler_field->area.y1), + &(poppler_field->area.x2), &(poppler_field->area.y2)); + poppler_field->type = (PopplerFormFieldType)field->getType(); + poppler_field->id = field->getID(); + poppler_field->content = field->getContentCopy(); + return poppler_field; + } else + return NULL; +} Index: glib/poppler-document.h =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler-document.h,v retrieving revision 1.21 diff -u -8 -p -r1.21 poppler-document.h --- glib/poppler-document.h 19 May 2006 22:19:21 -0000 1.21 +++ glib/poppler-document.h 16 Jul 2006 20:18:17 -0000 @@ -103,16 +103,27 @@ PopplerPage *poppler_document_get_pa /* Attachments */ gboolean poppler_document_has_attachments (PopplerDocument *document); GList *poppler_document_get_attachments (PopplerDocument *document); /* Links */ PopplerDest *poppler_document_find_dest (PopplerDocument *document, const gchar *link_name); +/* Form */ +void poppler_document_set_form_field_content (PopplerDocument *document, + int id, + char *content); +PopplerFormField *poppler_document_find_form_field_by_id (PopplerDocument *document, + int id); + +gchar * +poppler_document_get_form_field_content (PopplerDocument *document, int id); + + /* Interface for getting the Index of a poppler_document */ #define POPPLER_TYPE_INDEX_ITER (poppler_index_iter_get_type ()) GType poppler_index_iter_get_type (void) G_GNUC_CONST; PopplerIndexIter *poppler_index_iter_new (PopplerDocument *document); PopplerIndexIter *poppler_index_iter_copy (PopplerIndexIter *iter); void poppler_index_iter_free (PopplerIndexIter *iter); PopplerIndexIter *poppler_index_iter_get_child (PopplerIndexIter *parent); Index: glib/poppler-page.cc =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler-page.cc,v retrieving revision 1.50 diff -u -8 -p -r1.50 poppler-page.cc --- glib/poppler-page.cc 19 May 2006 21:42:54 -0000 1.50 +++ glib/poppler-page.cc 16 Jul 2006 20:18:18 -0000 @@ -980,8 +980,104 @@ poppler_link_mapping_copy (PopplerLinkMa void poppler_link_mapping_free (PopplerLinkMapping *mapping) { if (mapping) poppler_action_free (mapping->action); g_free (mapping); } + + +/* Form Type */ +GType +poppler_form_field_get_type (void) +{ + static GType our_type = 0; + if (our_type == 0) + our_type = g_boxed_type_register_static("PopplerFormField", + (GBoxedCopyFunc) poppler_form_field_copy, + (GBoxedFreeFunc) poppler_form_field_free); + return our_type; +} + +PopplerFormField* +poppler_form_field_new (void) +{ + return (PopplerFormField *) g_new0 (PopplerFormField, 1); +} + +PopplerFormField* +poppler_form_field_copy (PopplerFormField* field) +{ + PopplerFormField* new_field; + new_field = poppler_form_field_new(); + new_field = field; + return new_field; +} + +void +poppler_form_field_free (PopplerFormField* field) +{ + g_free (field); +} + +/** + * poppler_page_get_form_fields + **/ + +GList* +poppler_page_get_form_fields (PopplerPage *page) +{ + GList *field_list = NULL; + Form *form; + gint i; + Object obj; + + g_return_val_if_fail (POPPLER_IS_PAGE (page), NULL); + + //form = new Form(page->page->getAnnots (&obj)); + form = page->page->getForm(); + + obj.free (); + if(form == NULL) + return NULL; + + + for(i = 0; i < form->getNumFields(); i++) { + PopplerFormField *poppler_field; + FormField *field; + field = form->getField(i); + + poppler_field = g_new(PopplerFormField, 1); + field->getRect (&(poppler_field->area.x1), &(poppler_field->area.y1), + &(poppler_field->area.x2), &(poppler_field->area.y2)); + + poppler_field->type = (PopplerFormFieldType)field->getType(); + poppler_field->id = field->getID(); + poppler_field->content = field->getContentCopy(); + field_list = g_list_prepend(field_list,poppler_field); + } + return field_list; + +} + +void +poppler_page_free_form_fields (GList *list) +{ + if (list == NULL) + return; + + g_list_foreach (list, (GFunc) (poppler_form_field_free), NULL); + g_list_free (list); + +} + +void +poppler_page_get_crop_box (PopplerPage *page, PopplerRectangle *rect) +{ + PDFRectangle* cropBox = page->page->getCropBox(); + rect->x1 = cropBox->x1; + rect->x2 = cropBox->x2; + rect->y1 = cropBox->y1; + rect->y2 = cropBox->y2; +} + Index: glib/poppler-page.h =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler-page.h,v retrieving revision 1.21 diff -u -8 -p -r1.21 poppler-page.h --- glib/poppler-page.h 12 Apr 2006 02:07:07 -0000 1.21 +++ glib/poppler-page.h 16 Jul 2006 20:18:18 -0000 @@ -75,16 +75,21 @@ GdkRegion * poppler_page_get_sel void poppler_page_render_selection (PopplerPage *page, gdouble scale, int rotation, GdkPixbuf *pixbuf, PopplerRectangle *selection, PopplerRectangle *old_selection, GdkColor *glyph_color, GdkColor *background_color); +GList *poppler_page_get_form_fields (PopplerPage *page); +void poppler_page_free_form_fields (GList *list); + +void poppler_page_get_crop_box (PopplerPage *page, + PopplerRectangle *rect); /* A rectangle on a page, with coordinates in PDF points. */ #define POPPLER_TYPE_RECTANGLE (poppler_rectangle_get_type ()) struct _PopplerRectangle { gdouble x1; gdouble y1; @@ -107,11 +112,27 @@ struct _PopplerLinkMapping PopplerAction *action; }; GType poppler_link_mapping_get_type (void) G_GNUC_CONST; PopplerLinkMapping *poppler_link_mapping_new (void); PopplerLinkMapping *poppler_link_mapping_copy (PopplerLinkMapping *mapping); void poppler_link_mapping_free (PopplerLinkMapping *mapping); +/* FormField */ +#define POPPLER_TYPE_FORM_FIELD (poppler_form_field_get_type ()) +struct _PopplerFormField +{ + PopplerRectangle area; + PopplerFormFieldType type; + gchar *content; + int id; + +}; + +GType poppler_form_field_get_type (void) G_GNUC_CONST; +PopplerFormField *poppler_form_field_new (void); +PopplerFormField *poppler_form_field_copy (PopplerFormField *field); +void poppler_form_field_free (PopplerFormField *field); + G_END_DECLS #endif /* __POPPLER_PAGE_H__ */ Index: glib/poppler-private.h =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler-private.h,v retrieving revision 1.16 diff -u -8 -p -r1.16 poppler-private.h --- glib/poppler-private.h 19 May 2006 20:54:13 -0000 1.16 +++ glib/poppler-private.h 16 Jul 2006 20:18:18 -0000 @@ -1,15 +1,16 @@ #ifndef __POPPLER_PRIVATE_H__ #define __POPPLER_PRIVATE_H__ #include #include #include #include +#include #include #include #include #include #include "poppler-attachment.h" #if defined (HAVE_CAIRO) Index: glib/poppler.h =================================================================== RCS file: /cvs/poppler/poppler/glib/poppler.h,v retrieving revision 1.12 diff -u -8 -p -r1.12 poppler.h --- glib/poppler.h 16 Apr 2006 22:59:44 -0000 1.12 +++ glib/poppler.h 16 Jul 2006 20:18:19 -0000 @@ -37,21 +37,32 @@ typedef enum typedef enum { POPPLER_ORIENTATION_PORTRAIT, POPPLER_ORIENTATION_LANDSCAPE, POPPLER_ORIENTATION_UPSIDEDOWN, POPPLER_ORIENTATION_SEASCAPE } PopplerOrientation; +/* MUST be the same than poppler/Form.h fieldType */ +typedef enum +{ + POPPLER_FORM_FIELD_BUTTON, + POPPLER_FORM_FIELD_TEXT, + POPPLER_FORM_FIELD_CHOICE, + POPPLER_FORM_FIELD_SIGNATURE +} PopplerFormFieldType; + + typedef struct _PopplerDocument PopplerDocument; typedef struct _PopplerIndexIter PopplerIndexIter; typedef struct _PopplerFontsIter PopplerFontsIter; typedef struct _PopplerRectangle PopplerRectangle; typedef struct _PopplerLinkMapping PopplerLinkMapping; +typedef struct _PopplerFormField PopplerFormField; typedef struct _PopplerPage PopplerPage; typedef struct _PopplerFontInfo PopplerFontInfo; typedef struct _PopplerPSFile PopplerPSFile; typedef union _PopplerAction PopplerAction; typedef struct _PopplerDest PopplerDest; typedef enum Index: poppler/Annot.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/Annot.cc,v retrieving revision 1.5 diff -u -8 -p -r1.5 Annot.cc --- poppler/Annot.cc 6 Feb 2006 18:50:11 -0000 1.5 +++ poppler/Annot.cc 16 Jul 2006 20:18:20 -0000 @@ -71,17 +71,17 @@ Annot::Annot(XRef *xrefA, Dict *acroForm } obj1.free(); } // check for a text-type field isTextField = dict->lookup("FT", &obj1)->isName("Tx"); obj1.free(); -#if 0 //~ appearance stream generation is not finished yet +#if 1 //~ appearance stream generation is not finished yet if (regen && isTextField) { generateAppearance(acroForm, dict); } else { #endif if (dict->lookup("AP", &apObj)->isDict()) { if (dict->lookup("AS", &asObj)->isName()) { if (apObj.dictLookup("N", &obj1)->isDict()) { if (obj1.dictLookupNF(asObj.getName(), &obj2)->isRef()) { @@ -102,17 +102,17 @@ Annot::Annot(XRef *xrefA, Dict *acroForm obj1.copy(&appearance); ok = gTrue; } obj1.free(); } asObj.free(); } apObj.free(); -#if 0 //~ appearance stream generation is not finished yet +#if 1 //~ appearance stream generation is not finished yet } #endif } Annot::~Annot() { appearance.free(); if (appearBuf) { delete appearBuf; @@ -133,82 +133,86 @@ void Annot::generateAppearance(Dict *acr daStr = daObj.getString(); // look for a font size //~ may want to parse the DS entry in place of this (if it exists) daStr1 = NULL; fontSize = 10; for (i1 = daStr->getLength() - 2; i1 >= 0; --i1) { if (daStr->getChar(i1) == 'T' && daStr->getChar(i1+1) == 'f') { - for (--i1; i1 >= 0 && Lexer::isSpace(daStr->getChar(i1)); --i1) ; - for (i0 = i1; i0 >= 0 && !Lexer::isSpace(daStr->getChar(i0)); --i0) ; - if (i0 >= 0) { - ++i0; - ++i1; - s = new GooString(daStr, i0, i1 - i0); - fontSize = atof(s->getCString()); - delete s; - - // autosize the font - if (fontSize == 0) { - fontSize = 0.67 * (yMax - yMin); - daStr1 = new GooString(daStr, 0, i0); - sprintf(buf, "%.2f", fontSize); - daStr1->append(buf); - daStr1->append(daStr->getCString() + i1, - daStr->getLength() - i1); - } - } - break; + for (--i1; i1 >= 0 && Lexer::isSpace(daStr->getChar(i1)); --i1) ; + for (i0 = i1; i0 >= 0 && !Lexer::isSpace(daStr->getChar(i0)); --i0) ; + if (i0 >= 0) { + ++i0; + ++i1; + s = new GooString(daStr, i0, i1 - i0); + fontSize = atof(s->getCString()); + delete s; + + // autosize the font + if (fontSize == 0) { + fontSize = 0.67 * (yMax - yMin); + daStr1 = new GooString(daStr, 0, i0); + sprintf(buf, "%.2f", fontSize); + daStr1->append(buf); + daStr1->append(daStr->getCString() + i1, + daStr->getLength() - i1); + } + } + break; } } // build the appearance stream contents appearBuf = new GooString(); appearBuf->append("/Tx BMC\n"); appearBuf->append("q BT\n"); appearBuf->append(daStr1 ? daStr1 : daStr)->append("\n"); if (dict->lookup("V", &vObj)->isString()) { //~ handle quadding -- this requires finding the font and using //~ the encoding and char widths sprintf(buf, "1 0 0 1 %.2f %.2f Tm\n", 2.0, yMax - yMin - fontSize); appearBuf->append(buf); sprintf(buf, "%g TL\n", fontSize); appearBuf->append(buf); - vStr = vObj.getString(); + //FIXME: perhaps unicode handling could be better + UGooString tmp (*vObj.getString()); + vStr = new GooString(tmp.getCString()); + printf("%s\n", vStr->getCString()); i0 = 0; while (i0 < vStr->getLength()) { - for (i1 = i0; - i1 < vStr->getLength() && - vStr->getChar(i1) != '\n' && vStr->getChar(i1) != '\r'; - ++i1) ; - if (i0 > 0) { - appearBuf->append("T*\n"); - } - appearBuf->append('('); - for (; i0 < i1; ++i0) { - c = vStr->getChar(i0); - if (c == '(' || c == ')' || c == '\\') { - appearBuf->append('\\'); - appearBuf->append(c); - } else if (c < 0x20 || c >= 0x80) { - sprintf(buf, "\\%03o", c); - appearBuf->append(buf); - } else { - appearBuf->append(c); - } - } - appearBuf->append(") Tj\n"); - if (i1 + 1 < vStr->getLength() && - vStr->getChar(i1) == '\r' && vStr->getChar(i1 + 1) == '\n') { - i0 = i1 + 2; - } else { - i0 = i1 + 1; - } + for (i1 = i0; + i1 < vStr->getLength() && + vStr->getChar(i1) != '\n' && vStr->getChar(i1) != '\r'; + ++i1) ; + if (i0 > 0) { + appearBuf->append("T*\n"); + } + appearBuf->append('('); + for (; i0 < i1; ++i0) { + c = vStr->getChar(i0); + if (c == '(' || c == ')' || c == '\\') { + appearBuf->append('\\'); + appearBuf->append(c); + } else if (c < 0x20 || c >= 0x80) { + sprintf(buf, "\\%03o", c); + appearBuf->append(buf); + } else { + appearBuf->append(c); + } + } + appearBuf->append(") Tj\n"); + if (i1 + 1 < vStr->getLength() && + vStr->getChar(i1) == '\r' && vStr->getChar(i1 + 1) == '\n') { + i0 = i1 + 2; + } else { + i0 = i1 + 1; + } } + delete vStr; } vObj.free(); appearBuf->append("ET Q\n"); appearBuf->append("EMC\n"); // build the appearance stream dictionary appearDict.initDict(xref); appearDict.dictAdd("Length", obj1.initInt(appearBuf->getLength())); @@ -220,41 +224,41 @@ void Annot::generateAppearance(Dict *acr obj1.arrayAdd(obj2.initReal(yMax - yMin)); appearDict.dictAdd("BBox", &obj1); // find the resource dictionary dict->lookup("DR", &drObj); if (!drObj.isDict()) { dict->lookup("Parent", &obj1); while (obj1.isDict()) { - drObj.free(); - obj1.dictLookup("DR", &drObj); - if (drObj.isDict()) { - break; - } - obj1.dictLookup("Parent", &obj2); - obj1.free(); - obj1 = obj2; + drObj.free(); + obj1.dictLookup("DR", &drObj); + if (drObj.isDict()) { + break; + } + obj1.dictLookup("Parent", &obj2); + obj1.free(); + obj1 = obj2; } obj1.free(); if (!drObj.isDict()) { - if (acroForm) { - drObj.free(); - acroForm->lookup("DR", &drObj); - } + if (acroForm) { + drObj.free(); + acroForm->lookup("DR", &drObj); + } } } if (drObj.isDict()) { appearDict.dictAdd("Resources", drObj.copy(&obj1)); } drObj.free(); // build the appearance stream appearStream = new MemStream(appearBuf->getCString(), 0, - appearBuf->getLength(), &appearDict); + appearBuf->getLength(), &appearDict); appearance.initStream(appearStream); ok = gTrue; if (daStr1) { delete daStr1; } } daObj.free(); Index: poppler/Array.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/Array.cc,v retrieving revision 1.3 diff -u -8 -p -r1.3 Array.cc --- poppler/Array.cc 27 Aug 2005 08:43:43 -0000 1.3 +++ poppler/Array.cc 16 Jul 2006 20:18:20 -0000 @@ -26,17 +26,16 @@ Array::Array(XRef *xrefA) { xref = xrefA; elems = NULL; size = length = 0; ref = 1; } Array::~Array() { int i; - for (i = 0; i < length; ++i) elems[i].free(); gfree(elems); } void Array::add(Object *elem) { if (length == size) { if (length == 0) { Index: poppler/Dict.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/Dict.cc,v retrieving revision 1.4 diff -u -8 -p -r1.4 Dict.cc --- poppler/Dict.cc 18 Jan 2006 22:32:13 -0000 1.4 +++ poppler/Dict.cc 16 Jul 2006 20:18:21 -0000 @@ -50,16 +50,28 @@ void Dict::add(const UGooString &key, Ob } entries = (DictEntry *)greallocn(entries, size, sizeof(DictEntry)); } entries[length].key = new UGooString(key); entries[length].val = *val; ++length; } +void Dict::set(const UGooString &key, Object *val) { + DictEntry *e; + e = find (key); + //printf("%s\n", val->getString()->getCString()); + if (e) { + e->val.free(); + e->val = *val; + } else { + add (key, val); + } +} + inline DictEntry *Dict::find(const UGooString &key) { int i; for (i = 0; i < length; ++i) { if (!key.cmp(entries[i].key)) return &entries[i]; } return NULL; Index: poppler/Dict.h =================================================================== RCS file: /cvs/poppler/poppler/poppler/Dict.h,v retrieving revision 1.3 diff -u -8 -p -r1.3 Dict.h --- poppler/Dict.h 18 Jan 2006 22:32:13 -0000 1.3 +++ poppler/Dict.h 16 Jul 2006 20:18:21 -0000 @@ -38,16 +38,18 @@ public: int incRef() { return ++ref; } int decRef() { return --ref; } // Get number of entries. int getLength() { return length; } // Add an entry void add(const UGooString &key, Object *val); + // Update the value of an existing entry, otherwise create it + void set(const UGooString &key, Object *val); // Check if dictionary is of specified type. GBool is(char *type); // Look up an entry and return the value. Returns a null object // if is not in the dictionary. Object *lookup(const UGooString &key, Object *obj); Object *lookupNF(const UGooString &key, Object *obj); Index: poppler/Form.cc =================================================================== RCS file: poppler/Form.cc diff -N poppler/Form.cc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ poppler/Form.cc 16 Jul 2006 20:18:21 -0000 @@ -0,0 +1,292 @@ +//======================================================================== +// +// Form.cc +// +// Copyright 1996-2003 Glyph & Cog, LLC +// Copyright 2006 Julien Rebetez +// +//======================================================================== + +#include + +#ifdef USE_GCC_PRAGMAS +#pragma implementation +#endif + +#include +#include +#include "goo/gmem.h" +#include "goo/GooString.h" +#include "Error.h" +#include "Object.h" +#include "Array.h" +#include "Dict.h" +#include "Form.h" +#include "UGooString.h" + + +//======================================================================== +// FormField +//======================================================================== + +FormField::FormField(Object *aobj, unsigned id, Ref aref) { + Object obj1, obj2, obj3i; + double t; + ref = aref; + ID = id; + ok = gFalse; + content = NULL; + aobj->copy(&obj); + Dict *dict = obj.getDict(); + + if (dict->lookup("V", &obj1)->isString()) { + if(obj1.getString()->getLength() == 2 && obj1.getString()->hasUnicodeMarker()) { + printf("Form.cc:44, skipped empty unicode string\n"); + UGooString c(*obj1.getString()); + printf("ugoo: %s\n", c.getCString()); + content = c.getCString(); + } else { + printf("Form.cc:46, V : %s\n", obj1.getString()->getCString()); + content = strdup(obj1.getString()->getCString()); + } + } + obj1.free(); + + // get rectangle + if (!dict->lookup("Rect", &obj1)->isArray()) { + error(-1, "Annotation rectangle is wrong type"); + goto err2; + } + if (!obj1.arrayGet(0, &obj2)->isNum()) { + error(-1, "Bad annotation rectangle"); + goto err1; + } + x1 = obj2.getNum(); + obj2.free(); + if (!obj1.arrayGet(1, &obj2)->isNum()) { + error(-1, "Bad annotation rectangle"); + goto err1; + } + y1 = obj2.getNum(); + obj2.free(); + if (!obj1.arrayGet(2, &obj2)->isNum()) { + error(-1, "Bad annotation rectangle"); + goto err1; + } + x2 = obj2.getNum(); + obj2.free(); + if (!obj1.arrayGet(3, &obj2)->isNum()) { + error(-1, "Bad annotation rectangle"); + goto err1; + } + y2 = obj2.getNum(); + obj2.free(); + obj1.free(); + //swap coords if needed + if (x1 > x2) { + t = x1; + x1 = x2; + x2 = t; + } + if (y1 > y2) { + t = y1; + y1 = y2; + y2 = t; + } + + + + ok = gTrue; + + err1: + obj2.free(); + err2: + obj1.free(); +} + +FormField::FormField(FormField *dest) +{ + x1 = dest->x1; + y1 = dest->y1; + x2 = dest->x2; + y2 = dest->x2; + + ok = dest->ok; + + type = dest->type; +} + +FormField::~FormField() +{ + obj.free(); +} + +void FormField::setContentCopy(char* new_content) +{ + Object obj1; + + if(content) + delete [] content; + + if (new_content == NULL) + content = NULL; + else { + content = strdup(new_content); + + GooString *cont = new GooString(content); + Object *obj1 = new Object(); + obj1->initString(cont); + //printf("%s, %s, %s\n", content, cont->getCString(), obj1->getString()); + obj.getDict()->set("V", obj1); + } +} + +//------------------------------------------------------------------------ +// FormFieldButton +//------------------------------------------------------------------------ +FormFieldButton::FormFieldButton(Object *dict, unsigned id, Ref ref) : FormField(dict, id, ref) +{ + type = fieldButton; +} + +FormFieldButton::~FormFieldButton() +{ + +} + +//------------------------------------------------------------------------ +// FormFieldText +//------------------------------------------------------------------------ +FormFieldText::FormFieldText(Object *dict, unsigned id, Ref ref) : FormField(dict, id, ref) +{ + type = fieldText; +} + +FormFieldText::~FormFieldText() +{ + +} + +//------------------------------------------------------------------------ +// FormFieldChoice +//------------------------------------------------------------------------ +FormFieldChoice::FormFieldChoice(Object *dict, unsigned id, Ref ref) : FormField(dict, id, ref) +{ + type = fieldChoice; +} + +FormFieldChoice::~FormFieldChoice() +{ + +} + +//------------------------------------------------------------------------ +// FormFieldSignature +//------------------------------------------------------------------------ +FormFieldSignature::FormFieldSignature(Object *dict, unsigned id, Ref ref): FormField(dict, id, ref) +{ + type = fieldSignature; +} + +FormFieldSignature::~FormFieldSignature() +{ + +} + +//------------------------------------------------------------------------ +// Form +//------------------------------------------------------------------------ + +Form::Form(Object* annots, unsigned page) +{ + FormField* field; + Object obj1, obj2; + int size; + int i; + unsigned count = 0; + + fields = NULL; + size = 0; + numFields = 0; + pageNum = page; + + if (annots->isArray()) { + for (i = 0; i < annots->arrayGetLength(); ++i) { + if (annots->arrayGet(i, &obj1)->isDict()) { + if (obj1.dictLookup("Subtype", &obj2)->isName("Widget")) { + unsigned id = ((unsigned)pageNum << 16) + count; + obj2.free(); + annots->arrayGetNF(i, &obj2); //store the indirect ref for further update + Ref objRef = obj2.getRef(); + obj2.free(); + //printf("num: %i\t gen:%i\n", objRef.num, objRef.gen); + if(obj1.dictLookup("FT", &obj2)->isName("Btn")) { + field = new FormFieldButton(&obj1,id,objRef); + count++; + } else if (obj2.isName("Tx")) { + field = new FormFieldText(&obj1,id,objRef); + count++; + } else if (obj2.isName("Ch")) { + field = new FormFieldChoice(&obj1,id,objRef); + count++; + } else if (obj2.isName("Sig")) { + field = new FormFieldSignature(&obj1,id,objRef); + count++; + } else { + goto err1; + } + if (field->isOk()) { + if (numFields >= size) { + size += 16; + fields = (FormField**)greallocn(fields,size,sizeof(FormField*)); + } + fields[numFields++] = field; + } else { + delete field; + } + + } +err1: + obj2.free(); + } + obj1.free(); + } + } + +} + +Form::~Form() { + int i; + for(i = 0; i< numFields; ++i) + delete fields[i]; + gfree(fields); +} + +FormField *Form::find(double x, double y) const { + int i; + for (i = numFields -1; i>= 0; --i) { + if (fields[i]->inRect(x, y)) { + return fields[i]; + } + } + return NULL; +} + +FormField* Form::getFieldById (unsigned int id) const { + int i; + for (i=0; igetID() == id) { + return fields[i]; + } + } + return NULL; +} +GBool Form::onField(double x, double y) const { + int i; + for (i =0; iinRect(x,y)) + return gTrue; + } + return gFalse; +} + Index: poppler/Form.h =================================================================== RCS file: poppler/Form.h diff -N poppler/Form.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ poppler/Form.h 16 Jul 2006 20:18:21 -0000 @@ -0,0 +1,176 @@ +//======================================================================== +// +// Form.h +// +// Copyright 2006 Julien Rebetez +// Copyright 1996-2003 Glyph & Cog, LLC +// +//======================================================================== + +#ifndef FORM_H +#define FORM_H + +#ifdef USE_GCC_PRAGMAS +#pragma interface +#endif + +#include "Object.h" + +class GooString; +class UGooString; +class Array; +class Dict; + + +//------------------------------------------------------------------------ +// FormFieldType +//------------------------------------------------------------------------ + +enum FormFieldType { + fieldButton, + fieldText, + fieldChoice, + fieldSignature +}; + +class FormField { +public: + // Destructor + virtual ~FormField(); + + // Copy a FormField + FormField *copy() { return new FormField(this); } + + // Was the FormField created successfully ? + GBool isOk() { return ok; } + + // Check if point is inside the field rectangle + GBool inRect(double x, double y) + { return x1 <= x && x <= x2 && y1 <= y && y <= y2; } + + // Get the field rectangle + void getRect(double *xa1, double *ya1, double *xa2, double *ya2) + { *xa1 = x1; *ya1 = y1; *xa2 = x2; *ya2 = y2; } + + + + // Accessors. + FormFieldType getType() { return type; } + unsigned getID () { return ID; } + Object* getObj() { return &obj; } + char* getContentCopy() { if (content == NULL) return NULL; + else return strdup(content); } + void setContentCopy(char* new_content); + Ref getRef() { return ref; } +protected: + // Build a FormField from the dict, only callable from child-classes + FormField(Object *aobj, unsigned id, Ref aref); + FormFieldType type; // field type + GBool ok; // set if created successfully + char* content; + Ref ref; + Object obj; + + + +private: + FormField() {} + FormField(FormField *dest); + /* + Field ID is a 32bits integer, calculated as follow : + the first 16 bits are the field number, relative to the page + the last 16 bits are the page number + [page number | field number] + (encoding) id = (pageNum << 16) + fieldNum; + (decoding) pageNum = id >> 16; fieldNum = (id << 16) >> 16; + */ + unsigned ID; + + double x1, y1; // lower left corner + double x2, y2; // upper right corner +}; + + +//------------------------------------------------------------------------ +// FormFieldButton +//------------------------------------------------------------------------ + +class FormFieldButton: public FormField { +public: + // Build a fieldButton from flags + FormFieldButton(Object *dict, unsigned id, Ref ref); + + // Destructor + virtual ~FormFieldButton(); +}; + +//------------------------------------------------------------------------ +// FormFieldText +//------------------------------------------------------------------------ + +class FormFieldText: public FormField { +public: + // Build a fieldText from flags + FormFieldText(Object *dict, unsigned id, Ref ref); + + // Destructor + virtual ~FormFieldText(); +}; + +//------------------------------------------------------------------------ +// FormFieldChoice +//------------------------------------------------------------------------ + +class FormFieldChoice: public FormField { +public: + // Build a fieldChoice from flags + FormFieldChoice(Object *dict, unsigned id, Ref ref); + + // Destructor + virtual ~FormFieldChoice(); +}; + +//------------------------------------------------------------------------ +// FormFieldSignature +//------------------------------------------------------------------------ + +class FormFieldSignature: public FormField { +public: + // Build a fieldSignature from flags + FormFieldSignature(Object *dict, unsigned id, Ref ref); + + // Destructor + virtual ~FormFieldSignature(); +}; + +//------------------------------------------------------------------------ +// Form +//------------------------------------------------------------------------ + +class Form { +public: + // Extract form from array of annotations + Form(Object* annots, unsigned page); + + // Destructor + ~Form(); + + // Iterate through list of fields. + int getNumFields() const { return numFields; } + FormField* getField(int i) const { return fields[i]; } + FormField* getFieldById (unsigned int id) const; + + // If point , is in a field, return the associated field; + // else return NULL + FormField *find(double x, double y) const; + + // Return true if , is in a field. + GBool onField(double x, double y) const; + +private: + FormField** fields; + int numFields; + unsigned pageNum; +}; +#endif + Index: poppler/Makefile.am =================================================================== RCS file: /cvs/poppler/poppler/poppler/Makefile.am,v retrieving revision 1.23 diff -u -8 -p -r1.23 Makefile.am --- poppler/Makefile.am 19 May 2006 22:04:17 -0000 1.23 +++ poppler/Makefile.am 16 Jul 2006 20:18:22 -0000 @@ -105,16 +105,17 @@ poppler_include_HEADERS = \ Catalog.h \ CharCodeToUnicode.h \ CMap.h \ Decrypt.h \ Dict.h \ Error.h \ FontEncodingTables.h \ FontInfo.h \ + Form.h \ Function.cc \ Function.h \ Gfx.h \ GfxFont.h \ GfxState.h \ GlobalParams.h \ JArithmeticDecoder.h \ JBIG2Stream.h \ @@ -167,16 +168,17 @@ libpoppler_la_SOURCES = \ Catalog.cc \ CharCodeToUnicode.cc \ CMap.cc \ Decrypt.cc \ Dict.cc \ Error.cc \ FontEncodingTables.cc \ FontInfo.cc \ + Form.cc \ Function.cc \ Gfx.cc \ GfxFont.cc \ GfxState.cc \ GlobalParams.cc \ JArithmeticDecoder.cc \ JBIG2Stream.cc \ JPXStream.cc \ Index: poppler/Object.h =================================================================== RCS file: /cvs/poppler/poppler/poppler/Object.h,v retrieving revision 1.2 diff -u -8 -p -r1.2 Object.h --- poppler/Object.h 18 Jan 2006 22:32:13 -0000 1.2 +++ poppler/Object.h 16 Jul 2006 20:18:22 -0000 @@ -160,16 +160,17 @@ public: int arrayGetLength(); void arrayAdd(Object *elem); Object *arrayGet(int i, Object *obj); Object *arrayGetNF(int i, Object *obj); // Dict accessors. int dictGetLength(); void dictAdd(const UGooString &key, Object *val); + void dictSet(const UGooString &key, Object *val); GBool dictIs(char *dictType); Object *dictLookup(const UGooString &key, Object *obj); Object *dictLookupNF(const UGooString &key, Object *obj); UGooString *dictGetKey(int i); Object *dictGetVal(int i, Object *obj); Object *dictGetValNF(int i, Object *obj); // Stream accessors. @@ -237,16 +238,19 @@ inline Object *Object::arrayGetNF(int i, #include "Dict.h" inline int Object::dictGetLength() { return dict->getLength(); } inline void Object::dictAdd(const UGooString &key, Object *val) { dict->add(key, val); } +inline void Object::dictSet(const UGooString &key, Object *val) + { dict->set(key, val); } + inline GBool Object::dictIs(char *dictType) { return dict->is(dictType); } inline GBool Object::isDict(char *dictType) { return type == objDict && dictIs(dictType); } inline Object *Object::dictLookup(const UGooString &key, Object *obj) { return dict->lookup(key, obj); } Index: poppler/PDFDoc.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/PDFDoc.cc,v retrieving revision 1.10 diff -u -8 -p -r1.10 PDFDoc.cc --- poppler/PDFDoc.cc 18 Jan 2006 22:32:13 -0000 1.10 +++ poppler/PDFDoc.cc 16 Jul 2006 20:18:24 -0000 @@ -37,25 +37,25 @@ #ifndef DISABLE_OUTLINE #include "Outline.h" #endif #include "PDFDoc.h" #include "UGooString.h" //------------------------------------------------------------------------ -#define headerSearchSize 1024 // read this many bytes at beginning of - // file to look for '%PDF' +#define headerSearchSize 1024 // read this many bytes at beginning of + // file to look for '%PDF' //------------------------------------------------------------------------ // PDFDoc //------------------------------------------------------------------------ PDFDoc::PDFDoc(GooString *fileNameA, GooString *ownerPassword, - GooString *userPassword, void *guiDataA) { + GooString *userPassword, void *guiDataA) { Object obj; GooString *fileName1, *fileName2; ok = gFalse; errCode = errNone; guiData = guiDataA; @@ -82,36 +82,36 @@ PDFDoc::PDFDoc(GooString *fileNameA, Goo } #else if (!(file = fopen(fileName1->getCString(), "rb"))) { fileName2 = fileName->copy(); fileName2->lowerCase(); if (!(file = fopen(fileName2->getCString(), "rb"))) { fileName2->upperCase(); if (!(file = fopen(fileName2->getCString(), "rb"))) { - error(-1, "Couldn't open file '%s'", fileName->getCString()); - delete fileName2; - errCode = errOpenFile; - return; + error(-1, "Couldn't open file '%s'", fileName->getCString()); + delete fileName2; + errCode = errOpenFile; + return; } } delete fileName2; } #endif // create stream obj.initNull(); str = new FileStream(file, 0, gFalse, 0, &obj); ok = setup(ownerPassword, userPassword); } #ifdef WIN32 PDFDoc::PDFDoc(wchar_t *fileNameA, int fileNameLen, GooString *ownerPassword, - GooString *userPassword, void *guiDataA) { + GooString *userPassword, void *guiDataA) { OSVERSIONINFO version; wchar_t fileName2[_MAX_PATH + 1]; Object obj; int i; ok = gFalse; errCode = errNone; @@ -157,17 +157,17 @@ PDFDoc::PDFDoc(wchar_t *fileNameA, int f obj.initNull(); str = new FileStream(file, 0, gFalse, 0, &obj); ok = setup(ownerPassword, userPassword); } #endif PDFDoc::PDFDoc(BaseStream *strA, GooString *ownerPassword, - GooString *userPassword, void *guiDataA) { + GooString *userPassword, void *guiDataA) { ok = gFalse; errCode = errNone; guiData = guiDataA; fileName = NULL; file = NULL; str = strA; xref = NULL; catalog = NULL; @@ -237,16 +237,20 @@ PDFDoc::~PDFDoc() { fclose(file); } if (fileName) { delete fileName; } if (links) { delete links; } + + for (int i=0; igetPos(); @@ -319,112 +323,112 @@ GBool PDFDoc::checkEncryption(GooString GBool encrypted; SecurityHandler *secHdlr; GBool ret; xref->getTrailerDict()->dictLookup("Encrypt", &encrypt); if ((encrypted = encrypt.isDict())) { if ((secHdlr = SecurityHandler::make(this, &encrypt))) { if (secHdlr->checkEncryption(ownerPassword, userPassword)) { - // authorization succeeded - xref->setEncryption(secHdlr->getPermissionFlags(), - secHdlr->getOwnerPasswordOk(), - secHdlr->getFileKey(), - secHdlr->getFileKeyLength(), - secHdlr->getEncVersion(), - secHdlr->getEncRevision()); - ret = gTrue; + // authorization succeeded + xref->setEncryption(secHdlr->getPermissionFlags(), + secHdlr->getOwnerPasswordOk(), + secHdlr->getFileKey(), + secHdlr->getFileKeyLength(), + secHdlr->getEncVersion(), + secHdlr->getEncRevision()); + ret = gTrue; } else { - // authorization failed - ret = gFalse; + // authorization failed + ret = gFalse; } delete secHdlr; } else { // couldn't find the matching security handler ret = gFalse; } } else { // document is not encrypted ret = gTrue; } encrypt.free(); return ret; } void PDFDoc::displayPage(OutputDev *out, int page, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, GBool doLinks, - GBool (*abortCheckCbk)(void *data), - void *abortCheckCbkData, + int rotate, GBool useMediaBox, GBool crop, GBool doLinks, + GBool (*abortCheckCbk)(void *data), + void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData) { Page *p; if (globalParams->getPrintCommands()) { printf("***** page %d *****\n", page); } p = catalog->getPage(page); if (doLinks) { if (links) { delete links; } getLinks(p); p->display(out, hDPI, vDPI, rotate, useMediaBox, crop, links, catalog, - abortCheckCbk, abortCheckCbkData, + abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } else { p->display(out, hDPI, vDPI, rotate, useMediaBox, crop, NULL, catalog, - abortCheckCbk, abortCheckCbkData, + abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } } void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage, - double hDPI, double vDPI, int rotate, GBool useMediaBox, - GBool crop, GBool doLinks, - GBool (*abortCheckCbk)(void *data), - void *abortCheckCbkData, + double hDPI, double vDPI, int rotate, GBool useMediaBox, + GBool crop, GBool doLinks, + GBool (*abortCheckCbk)(void *data), + void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData) { int page; for (page = firstPage; page <= lastPage; ++page) { displayPage(out, page, hDPI, vDPI, rotate, useMediaBox, crop, doLinks, - abortCheckCbk, abortCheckCbkData, + abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } } void PDFDoc::displayPageSlice(OutputDev *out, int page, - double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, GBool doLinks, - int sliceX, int sliceY, int sliceW, int sliceH, - GBool (*abortCheckCbk)(void *data), - void *abortCheckCbkData, + double hDPI, double vDPI, + int rotate, GBool useMediaBox, GBool crop, GBool doLinks, + int sliceX, int sliceY, int sliceW, int sliceH, + GBool (*abortCheckCbk)(void *data), + void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData) { Page *p; p = catalog->getPage(page); if (doLinks) { if (links) { delete links; } getLinks(p); p->displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop, - sliceX, sliceY, sliceW, sliceH, - links, catalog, + sliceX, sliceY, sliceW, sliceH, + links, catalog, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } else { p->displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop, sliceX, sliceY, sliceW, sliceH, - NULL, catalog, - abortCheckCbk, abortCheckCbkData, - annotDisplayDecideCbk, annotDisplayDecideCbkData); + NULL, catalog, + abortCheckCbk, abortCheckCbkData, + annotDisplayDecideCbk, annotDisplayDecideCbkData); } } Links *PDFDoc::takeLinks() { Links *ret; ret = links; links = NULL; @@ -434,18 +438,18 @@ Links *PDFDoc::takeLinks() { GBool PDFDoc::isLinearized() { Parser *parser; Object obj1, obj2, obj3, obj4, obj5; GBool lin; lin = gFalse; obj1.initNull(); parser = new Parser(xref, - new Lexer(xref, - str->makeSubStream(str->getStart(), gFalse, 0, &obj1))); + new Lexer(xref, + str->makeSubStream(str->getStart(), gFalse, 0, &obj1))); parser->getObj(&obj1); parser->getObj(&obj2); parser->getObj(&obj3); parser->getObj(&obj4); if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") && obj4.isDict()) { obj4.dictLookup("Linearized", &obj5); if (obj5.isNum() && obj5.getNum() > 0) { @@ -456,31 +460,194 @@ GBool PDFDoc::isLinearized() { obj4.free(); obj3.free(); obj2.free(); obj1.free(); delete parser; return lin; } +void PDFDoc::addIncrementalObject (Object *obj, Ref ref) +{ + Object *cpy = new Object(); + obj->copy(cpy); + IncrObj tmp; + tmp.ref = ref; + tmp.obj = cpy; + incrementalObjects.push_back(tmp); + +} + + GBool PDFDoc::saveAs(GooString *name) { FILE *f; int c; + int offsetPrev; if (!(f = fopen(name->getCString(), "wb"))) { error(-1, "Couldn't open file '%s'", name->getCString()); return gFalse; } str->reset(); while ((c = str->getChar()) != EOF) { fputc(c, f); } str->close(); + + //handle update + //We need to be sure that NeedAppearances is 'true' in the AcroForm entry of the Catalog + Object catDict; + Ref catRef; + Object acroForm; + xref->getCatalog(&catDict); + catRef.gen = xref->getRootGen(); + catRef.num = xref->getRootNum(); + catDict.dictLookup("AcroForm", &acroForm); + Object obj1; + obj1.initBool(true); + acroForm.dictSet("NeedAppearances", &obj1); + obj1.free(); + catDict.dictSet("AcroForm", &acroForm); + this->addIncrementalObject (&catDict, catRef); + + + XRef *uxref = new XRef(); + //first xref entry is always free, at offset 0 and with a gen number of 65535 + uxref->add(0, 65535, NULL, 0); + for(int i=0; iadd(incrementalObjects[i].ref.num, incrementalObjects[i].ref.gen, incrementalObjects[i].obj, offset); + obj1.free(); + } + Guint uxrefOffset = ftell(f); + uxref->writeToFile (f); + writeTrailer(uxrefOffset,uxref->getSize(), f, &catRef); + fclose(f); + delete uxref; return gTrue; } +void PDFDoc::writeDictionnary (Dict* dict, FILE *file) +{ + Object obj1; + fprintf(file,"<< "); + for (int i=0; igetLength(); i++) { + fprintf(file,"/%s ", dict->getKey(i)->getCString()); + writeObject(dict->getValNF(i, &obj1), NULL, file); + fprintf(file,"\n"); + obj1.free(); + } + fprintf(file,">>\n"); +} + +void PDFDoc::writeStream (Stream* str, FILE *file) +{ + int c; + fprintf(file,"stream\n"); + str->reset(); + for (int c=str->getChar(); c!= EOF; c=str->getChar()) { + fprintf(file,"%i", c); + } + fprintf(file,"\nendstream\n"); +} + +Guint PDFDoc::writeObject (Object* obj, Ref* ref, FILE *file) +{ + Array *array; + Object obj1; + Guint offset = ftell(file); + + if(ref) + fprintf(file,"%i %i obj\n", ref->num, ref->gen); + + switch (obj->getType()) { + case objBool: + fprintf(file,"%s ", obj->getBool()?"true":"false"); + break; + case objInt: + fprintf(file,"%i ", obj->getInt()); + break; + case objReal: + fprintf(file,"%f ", obj->getReal()); + break; + case objString: + fprintf(file,"(%s) ", obj->getString()->getCString()); + break; + case objName: + fprintf(file,"/%s ", obj->getName()); + break; + case objNull: + fprintf(file,"NULL "); + break; + case objArray: + array = obj->getArray(); + fprintf(file,"["); + for (int i=0; igetLength(); i++) { + writeObject(array->getNF(i, &obj1), NULL,file); + obj1.free(); + } + fprintf(file,"]"); + break; + case objDict: + writeDictionnary (obj->getDict(),file); + break; + case objStream: + writeDictionnary (obj->getStream()->getDict(),file); + writeStream (obj->getStream(),file); + + fprintf(file,"STREAM HERE\n"); + break; + case objRef: + fprintf(file,"%i %i R ", obj->getRef().num, obj->getRef().gen); + break; + case objCmd: + fprintf(file,"CMD HERE\n"); + break; + case objError: + fprintf(file,"ERROR HERE\n"); + break; + case objEOF: + fprintf(file,"EOF HERE\n"); + break; + case objNone: + fprintf(file,"NONE HERE\n"); + break; + default: + fprintf(file,"Unhandled objType : %i, please report a bug with a testcase\n", obj->getType()); + break; + } + if (ref) + fprintf(file,"endobj\n\n"); + //else + //fprintf(file,"\n"); + return offset; +} + +void PDFDoc::writeTrailer (Guint uxrefOffset, int uxrefSize, FILE* f, Ref* rootRef) +{ + Dict* trailerDict = xref->getTrailerDict()->getDict(); + Object obj1; + obj1.initInt(xref->getOffset()); + trailerDict->set("Prev", &obj1); + obj1.free(); + obj1.initInt(uxrefSize); + trailerDict->set("Size", &obj1); + obj1.free(); + if(rootRef) { + obj1.initRef(rootRef->num, rootRef->gen); + trailerDict->set("Root", &obj1); + obj1.free(); + } + fprintf(f, "trailer\n"); + writeDictionnary(trailerDict, f); + fprintf(f, "startxref\n"); + fprintf(f, "%i\n", uxrefOffset); + fprintf(f, "%%%%EOF\n"); +} + void PDFDoc::getLinks(Page *page) { Object obj; links = new Links(page->getAnnots(&obj), catalog->getBaseURI()); obj.free(); } Index: poppler/PDFDoc.h =================================================================== RCS file: /cvs/poppler/poppler/poppler/PDFDoc.h,v retrieving revision 1.7 diff -u -8 -p -r1.7 PDFDoc.h --- poppler/PDFDoc.h 18 Jan 2006 22:32:13 -0000 1.7 +++ poppler/PDFDoc.h 16 Jul 2006 20:18:25 -0000 @@ -9,16 +9,17 @@ #ifndef PDFDOC_H #define PDFDOC_H #ifdef USE_GCC_PRAGMAS #pragma interface #endif #include +#include "goo/GooVector.h" #include "XRef.h" #include "Catalog.h" #include "Page.h" #include "Annot.h" class GooString; class BaseStream; class OutputDev; @@ -158,31 +159,46 @@ public: // Return the document's Info dictionary (if any). Object *getDocInfo(Object *obj) { return xref->getDocInfo(obj); } Object *getDocInfoNF(Object *obj) { return xref->getDocInfoNF(obj); } // Return the PDF version specified by the file. double getPDFVersion() { return pdfVersion; } + // Add an object to the file stream, using incremental update + void addIncrementalObject (Object *obj, Ref ref); + // Save this file with another name. GBool saveAs(GooString *name); // Return a pointer to the GUI (XPDFCore or WinPDFCore object). void *getGUIData() { return guiData; } private: + // Add object to current file stream and return the offset of the beginning of the object + Guint writeObject (Object *obj, Ref *ref, FILE* f); + void writeDictionnary (Dict* dict, FILE* f); + void writeStream (Stream* str, FILE* f); + void writeTrailer (Guint uxrefOffset, int uxrefSize, FILE* f, Ref* rootRef); + + struct IncrObj + { + Ref ref; + Object* obj; + }; GBool setup(GooString *ownerPassword, GooString *userPassword); GBool checkFooter(); void checkHeader(); GBool checkEncryption(GooString *ownerPassword, GooString *userPassword); void getLinks(Page *page); GooString *fileName; + GooVector incrementalObjects; FILE *file; BaseStream *str; void *guiData; double pdfVersion; XRef *xref; Catalog *catalog; Links *links; #ifndef DISABLE_OUTLINE Index: poppler/Page.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/Page.cc,v retrieving revision 1.12 diff -u -8 -p -r1.12 Page.cc --- poppler/Page.cc 16 Mar 2006 22:04:56 -0000 1.12 +++ poppler/Page.cc 16 Jul 2006 20:18:25 -0000 @@ -21,16 +21,17 @@ #include "XRef.h" #include "Link.h" #include "OutputDev.h" #ifndef PDF_PARSER_ONLY #include "Gfx.h" #include "GfxState.h" #include "Annot.h" #include "TextOutputDev.h" +#include "Form.h" #endif #include "Error.h" #include "Page.h" #include "UGooString.h" //------------------------------------------------------------------------ // PageAttrs //------------------------------------------------------------------------ @@ -184,16 +185,17 @@ GBool PageAttrs::readBox(Dict *dict, cha return ok; } //------------------------------------------------------------------------ // Page //------------------------------------------------------------------------ Page::Page(XRef *xrefA, int numA, Dict *pageDict, PageAttrs *attrsA) { + Object obj; ok = gTrue; xref = xrefA; num = numA; // get attributes attrs = attrsA; // transtion @@ -208,16 +210,20 @@ Page::Page(XRef *xrefA, int numA, Dict * pageDict->lookupNF("Annots", &annots); if (!(annots.isRef() || annots.isArray() || annots.isNull())) { error(-1, "Page annotations object (page %d) is wrong type (%s)", num, annots.getTypeName()); annots.free(); goto err2; } + // forms + form = new Form(this->getAnnots(&obj),num); + obj.free(); + // contents pageDict->lookupNF("Contents", &contents); if (!(contents.isRef() || contents.isArray() || contents.isNull())) { error(-1, "Page contents object (page %d) is wrong type (%s)", num, contents.getTypeName()); contents.free(); goto err1; Index: poppler/Page.h =================================================================== RCS file: /cvs/poppler/poppler/poppler/Page.h,v retrieving revision 1.5 diff -u -8 -p -r1.5 Page.h --- poppler/Page.h 30 Oct 2005 20:29:05 -0000 1.5 +++ poppler/Page.h 16 Jul 2006 20:18:26 -0000 @@ -18,16 +18,17 @@ class Dict; class XRef; class OutputDev; class Links; class Catalog; class Annots; class Annot; class Gfx; +class Form; //------------------------------------------------------------------------ class PDFRectangle { public: double x1, y1, x2, y2; PDFRectangle() { x1 = y1 = x2 = y2 = 0; } @@ -146,16 +147,19 @@ public: // Get thumb. Object *getThumb(Object *obj) { return thumb.fetch(xref, obj); } GBool loadThumb(unsigned char **data, int *width, int *height, int *rowstride); // Get transition. Object *getTrans(Object *obj) { return trans.fetch(xref, obj); } + // Get form. + Form *getForm() { return form; } + Gfx *createGfx(OutputDev *out, double hDPI, double vDPI, int rotate, GBool useMediaBox, GBool crop, int sliceX, int sliceY, int sliceW, int sliceH, Links *links, Catalog *catalog, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData); @@ -190,11 +194,12 @@ private: XRef *xref; // the xref table for this PDF file int num; // page number PageAttrs *attrs; // page attributes Object annots; // annotations array Object contents; // page contents Object thumb; // page thumbnail Object trans; // page transition GBool ok; // true if page is valid + Form *form; // the form for that page }; #endif Index: poppler/XRef.cc =================================================================== RCS file: /cvs/poppler/poppler/poppler/XRef.cc,v retrieving revision 1.13 diff -u -8 -p -r1.13 XRef.cc --- poppler/XRef.cc 1 Apr 2006 11:25:57 -0000 1.13 +++ poppler/XRef.cc 16 Jul 2006 20:18:28 -0000 @@ -196,36 +196,51 @@ Object *ObjectStream::getObject(int objI } return objs[objIdx].copy(obj); } //------------------------------------------------------------------------ // XRef //------------------------------------------------------------------------ +XRef::XRef() { + ok = gTrue; + errCode = errNone; + entries = NULL; + size = 0; + streamEnds = NULL; + streamEndsLen = 0; + objStr = NULL; + for (int i=0; igetStart(); pos = getStartXref(); + offset = pos; // if there was a problem with the 'startxref' position, try to // reconstruct the xref table if (pos == 0) { if (!(ok = constructXRef())) { errCode = errDamaged; return; } @@ -844,16 +859,20 @@ Object *XRef::fetch(int num, int gen, Ob Parser *parser; Object obj1, obj2, obj3; // check for bogus ref - this can happen in corrupted PDF files if (num < 0 || num >= size) { goto err; } + // if this object has been modified, don't reload it from disk, return from the pool + if (objectsPool[num] != NULL) + return objectsPool[num]; + e = &entries[num]; switch (e->type) { case xrefEntryUncompressed: if (e->gen != gen) { goto err; } obj1.initNull(); @@ -962,8 +981,53 @@ Guint XRef::strToUnsigned(char *s) { int i; x = 0; for (p = s, i = 0; *p && isdigit(*p) && i < 10; ++p, ++i) { x = 10 * x + (*p - '0'); } return x; } + +void XRef::add(int num, int gen, Object *obj, Guint offs) { + size += 1; + entries = (XRefEntry *)greallocn(entries, size, sizeof(XRefEntry)); + entries[size-1].offset = offs; + entries[size-1].gen = gen; + entries[size-1].num = num; + if (obj) + entries[size-1].type = xrefEntryUncompressed; + else + entries[size-1].type = xrefEntryFree; +} + +void XRef::setModifiedObject (Object* o, Ref r) +{ + objectsPool[r.num] = new Object(); + o->copy(objectsPool[r.num]); +// objectsPool[r.num] = o; +} + +int compare (const void* a, const void* b) +{ + return (((XRefEntry*)a)->num - ((XRefEntry*)b)->num); +} + +void XRef::writeToFile(FILE* file) { + qsort(entries, size, sizeof(XRefEntry), compare); + int lastNum = -1; + int j; + fprintf(file,"xref\n"); + for (int i=0; i array - int rootNum, rootGen; // catalog dict - GBool ok; // true if xref table is valid - int errCode; // error code (if is false) - Object trailerDict; // trailer dictionary - Guint lastXRefPos; // offset of last xref table - Guint *streamEnds; // 'endstream' positions - only used in - // damaged files - int streamEndsLen; // number of valid entries in streamEnds - ObjectStream *objStr; // cached object stream - GBool encrypted; // true if file is encrypted - int encRevision; - int encVersion; // encryption algorithm - int keyLength; // length of key, in bytes - int permFlags; // permission bits - Guchar fileKey[16]; // file decryption key - GBool ownerPasswordOk; // true if owner password is correct + BaseStream *str; // input stream + Guint start; // offset in file (to allow for garbage + // at beginning of file) + XRefEntry *entries; // xref entries + int size; // size of array + int rootNum, rootGen; // catalog dict + GBool ok; // true if xref table is valid + int errCode; // error code (if is false) + Object trailerDict; // trailer dictionary + Guint lastXRefPos; // offset of last xref table + Guint *streamEnds; // 'endstream' positions - only used in + // damaged files + int streamEndsLen; // number of valid entries in streamEnds + ObjectStream *objStr; // cached object stream + GBool encrypted; // true if file is encrypted + int encRevision; + int encVersion; // encryption algorithm + int keyLength; // length of key, in bytes + int permFlags; // permission bits + Guchar fileKey[16]; // file decryption key + GBool ownerPasswordOk; // true if owner password is correct + Guint offset; // position of this xref + Object* objectsPool[XREF_SIZE]; //list of objects, indexed by object number. Unmodified objects have NULL entry, other entry adress the modified Object Guint getStartXref(); GBool readXRef(Guint *pos); GBool readXRefTable(Parser *parser, Guint *pos); GBool readXRefStreamSection(Stream *xrefStr, int *w, int first, int n); GBool readXRefStream(Stream *xrefStr, Guint *pos); GBool constructXRef(); Guint strToUnsigned(char *s); Index: test/gtk-splash-test.cc =================================================================== RCS file: /cvs/poppler/poppler/test/gtk-splash-test.cc,v retrieving revision 1.5 diff -u -8 -p -r1.5 gtk-splash-test.cc --- test/gtk-splash-test.cc 30 Oct 2005 20:29:05 -0000 1.5 +++ test/gtk-splash-test.cc 16 Jul 2006 20:18:29 -0000 @@ -271,16 +271,17 @@ int main (int argc, char *argv []) { View *v; int i; gtk_init (&argc, &argv); globalParams = new GlobalParams("/etc/xpdfrc"); + globalParams->setPrintCommands(TRUE); if (argc == 1) { fprintf (stderr, "usage: %s PDF-FILES...\n", argv[0]); return -1; }