From bea5f731a80b386e71759e5e7ad4188c6efd318d Mon Sep 17 00:00:00 2001 From: Scott West Date: Sat, 4 Oct 2014 12:05:05 +0200 Subject: [PATCH] Squashed commit of the following: commit fec13325f19e32ff02e8176e601231facf0d550a Author: Scott West Date: Sat Oct 4 12:01:27 2014 +0200 Expand comments for info dict API. commit 78cc6ce4d228c382b7171b39a7cf1f0b0e523079 Author: Scott West Date: Sat Oct 4 11:40:28 2014 +0200 Move common functionality into PDFDoc. commit 7c082b6c16e6703982901eff0a21351c0a7187b8 Author: Scott West Date: Sat Oct 4 10:21:50 2014 +0200 Revert "Get/set title now uses info dict API." This reverts commit 8cb28a87c5d8560fd659b7e92f489fd2e6455feb. commit 2fb20d0a4e3f75ae3b3647a9bb0032a53605ab13 Author: Scott West Date: Thu Oct 2 23:16:52 2014 +0200 Add ability to remove entries from info dict. commit 8cb28a87c5d8560fd659b7e92f489fd2e6455feb Author: Scott West Date: Wed Oct 1 12:28:23 2014 +0200 Get/set title now uses info dict API. commit 34d12fbf2ef3c744f099d697d284eebda5f51022 Author: Scott West Date: Wed Oct 1 12:27:55 2014 +0200 Add glib API to set info dict values. --- glib/poppler-document.cc | 58 ++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 6 ++++ poppler/PDFDoc.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ poppler/PDFDoc.h | 6 ++++ 4 files changed, 143 insertions(+) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 61d92e8..6c41b46 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -737,6 +737,27 @@ char *_poppler_goo_string_to_utf8(GooString *s) return result; } +static GooString * +_poppler_utf8_to_goo_string(const gchar *str) +{ + gsize written_bytes; + gchar *utf16; + + utf16 = g_convert(str, strlen(str), "UTF-16BE", "UTF-8", NULL, &written_bytes, NULL); + + if (!utf16) { + return NULL; + } + + GooString *goo_str = NULL; + + goo_str = new GooString(utf16, written_bytes); + + g_free(utf16); + + return goo_str; +} + static gchar * info_dict_get_string (Dict *info_dict, const gchar *key) { @@ -756,6 +777,43 @@ info_dict_get_string (Dict *info_dict, const gchar *key) return result; } +gchar * +poppler_document_info_get_string (PopplerDocument *document, + const gchar *key) +{ + GooString *goo_str; + gchar *result = NULL; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); + + goo_str = document->doc->getDocInfoStringEntry(key); + + if (!goo_str) { + return result; + } + + result = _poppler_goo_string_to_utf8(goo_str); + delete goo_str; + + return result; +} + +gboolean +poppler_document_info_set_string (PopplerDocument *document, + const gchar *key, + gchar *value) +{ + GooString *goo_str = NULL; + + g_return_val_if_fail(POPPLER_IS_DOCUMENT(document), FALSE); + + if (value) { + goo_str = _poppler_utf8_to_goo_string(value); + } + + return document->doc->setDocInfoStringEntry(key, goo_str); +} + static time_t info_dict_get_date (Dict *info_dict, const gchar *key) { diff --git a/glib/poppler-document.h b/glib/poppler-document.h index a34e88c..cc9d017 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -214,6 +214,12 @@ PopplerPageMode poppler_document_get_page_mode (PopplerDocument *doc PopplerPermissions poppler_document_get_permissions (PopplerDocument *document); gchar *poppler_document_get_metadata (PopplerDocument *document); +/* Info setters/getters */ +gboolean poppler_document_info_set_string (PopplerDocument *document, + const gchar *key, + gchar *value); +gchar *poppler_document_info_get_string (PopplerDocument *document, + const gchar *key); /* Attachments */ guint poppler_document_get_n_attachments (PopplerDocument *document); gboolean poppler_document_has_attachments (PopplerDocument *document); diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index f8ac85d..44eb046 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -536,6 +536,79 @@ GBool PDFDoc::isLinearized(GBool tryingToReconstruct) { } } +// Set/get doc info string entries +GooString * +PDFDoc::getDocInfoStringEntry(const char *key) +{ + Object info_obj; + GooString *result = NULL; + + getDocInfo(&info_obj); + if (info_obj.isDict()) { + Object result_obj; + + if (info_obj.dictLookup(key, &result_obj, 0)->isString()) { + result = new GooString(result_obj.getString()); + } + + result_obj.free(); + } + + info_obj.free(); + + return result; +} + +static GBool +setInfoModified (PDFDoc *doc, Object *info_obj) +{ + XRef *doc_xref = doc->getXRef(); + Object info_obj_ref; + + if (!doc_xref) { + return gFalse; + } + + doc_xref->getDocInfoNF(&info_obj_ref); + doc_xref->setModifiedObject(info_obj, info_obj_ref.getRef()); + + info_obj_ref.free(); + + return gTrue; +} + +GBool +PDFDoc::setDocInfoStringEntry(const char *key, GooString *value) +{ + Object info_obj; + GBool success = gFalse; + + getDocInfo(&info_obj); + + if (info_obj.isDict()) { + Object goo_str_obj; + + if (value) { + if (!value->hasUnicodeMarker()) { + value->insert(0, 0xff); + value->insert(0, 0xfe); + } + + goo_str_obj.initString(value); + } else { + // Setting a null object will cause a removal. + goo_str_obj.initNull(); + } + + info_obj.dictSet(key, &goo_str_obj); + success = setInfoModified(this, &info_obj); + } + + info_obj.free(); + + return success; +} + static GBool get_id (GooString *encodedidstring, GooString *id) { const char *encodedid = encodedidstring->getCString(); diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index a73953c..a637021 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -225,6 +225,12 @@ public: Object *getDocInfo(Object *obj) { return xref->getDocInfo(obj); } Object *getDocInfoNF(Object *obj) { return xref->getDocInfoNF(obj); } + // Get doc info string entry, the result is a newly allocated GooString. + GooString *getDocInfoStringEntry(const char *key); + + // Set doc info string entry, if value is NULL, then the entry will be removed. + GBool setDocInfoStringEntry(const char *key, GooString *value); + // Return the PDF version specified by the file. int getPDFMajorVersion() { return pdfMajorVersion; } int getPDFMinorVersion() { return pdfMinorVersion; } -- 2.1.2