From ce4b974c5432b7f3c3035008130a61506598c621 Mon Sep 17 00:00:00 2001 From: Masamichi Hosoda Date: Sun, 28 Aug 2016 16:55:20 +0900 Subject: [PATCH v2 7/7] Add poppler_document_build_dests_hashtable() to glib frontend Builds named destinations hash table in document. --- glib/poppler-document.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 1 + 2 files changed, 83 insertions(+) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index 15d8d44..365a159 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -818,6 +818,88 @@ poppler_document_free_dests_namelist (GList *list) g_list_free_full (list, _poppler_dest_destroy_bytes); } +static void +_poppler_dest_destroy_key (gpointer key) +{ + g_bytes_unref (static_cast(key)); +} + +static void +_poppler_dest_destroy_value (gpointer value) +{ + poppler_dest_free (static_cast(value)); +} + +/** + * poppler_document_build_dests_hashtable: + * @document: A #PopplerDocument + * + * Builds named destinations hash table in @document + * + * Return value: a #GHashTable or %NULL if @document is not valid. + * The hash key is the #GBytes which constains a destination name. + * Note that the names can contain \0, so it is GBytes instead of gchar*. + * The hash value the #PopplerDest which contains a named destination. + * Returned value must be freed with #g_hash_table_destroy. + * + * Since: 0.48 + **/ +GHashTable * +poppler_document_build_dests_hashtable (PopplerDocument *document) +{ + GHashTable *hash; + Catalog *catalog; + LinkDest *link_dest; + PopplerDest *dest; + int i, len; + GBytes *key; + + g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), NULL); + + catalog = document->doc->getCatalog (); + if (catalog == NULL) + return NULL; + + hash = g_hash_table_new_full (g_bytes_hash, poppler_dest_equal, + _poppler_dest_destroy_key, + _poppler_dest_destroy_value); + + // Iterate from name-dict + len = catalog->numDests (); + for (i = 0; i < len; ++i) { + // The names of name-dict cannot contain \0, + // so we can use strlen(). + key = g_bytes_new (catalog->getDestsName (i), + strlen (catalog->getDestsName (i))); + link_dest = catalog->getDestsDest (i); + if (link_dest) { + dest = _poppler_dest_new_goto (document, link_dest); + delete link_dest; + } else + dest = NULL; + g_hash_table_insert (hash, key, dest); + } + + // Iterate form name-tree + len = catalog->numDestNameTree (); + for (i = 0; i < len; ++i) { + // The names of name-tree can contain \0, + // so we use GBytes instead of gchar*. + key = g_bytes_new + (catalog->getDestNameTreeName (i)->getCString (), + catalog->getDestNameTreeName (i)->getLength ()); + link_dest = catalog->getDestNameTreeDest (i); + if (link_dest) { + dest = _poppler_dest_new_goto (document, link_dest); + delete link_dest; + } else + dest = NULL; + g_hash_table_insert (hash, key, dest); + } + + return hash; +} + char *_poppler_goo_string_to_utf8(GooString *s) { if (s == NULL) { diff --git a/glib/poppler-document.h b/glib/poppler-document.h index 1e8adb7..2855837 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -245,6 +245,7 @@ PopplerDest *poppler_document_find_dest_bytes (PopplerDocument *do GBytes *link_name); GList *poppler_document_build_dests_namelist (PopplerDocument *document); void poppler_document_free_dests_namelist (GList *list); +GHashTable *poppler_document_build_dests_hashtable (PopplerDocument *document); /* Form */ PopplerFormField *poppler_document_get_form_field (PopplerDocument *document, -- 2.8.3