From 81f39efe4cc4a326bba02b6787d5a64df320c170 Mon Sep 17 00:00:00 2001 From: Scott West Date: Sun, 5 Oct 2014 21:09:30 +0200 Subject: [PATCH] PDFDoc Info API (rev. 2) Fixes useless checkin of PDF's XRef and style issues. --- glib/poppler-document.cc | 58 ++++++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 6 +++++ poppler/PDFDoc.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ poppler/PDFDoc.h | 6 +++++ 4 files changed, 135 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..783b791 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -536,6 +536,71 @@ 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 void setInfoModified (PDFDoc *doc, Object *info_obj) +{ + XRef *doc_xref = doc->getXRef(); + Object info_obj_ref; + + doc_xref->getDocInfoNF(&info_obj_ref); + doc_xref->setModifiedObject(info_obj, info_obj_ref.getRef()); + + info_obj_ref.free(); +} + +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); + setInfoModified(this, &info_obj); + success = gTrue; + } + + 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