From b3e2c91e3916fc9f99648b95966911aed9ee53a2 Mon Sep 17 00:00:00 2001 From: Masamichi Hosoda Date: Sun, 28 Aug 2016 16:55:20 +0900 Subject: [PATCH v3 2/2] Add poppler_document_build_dests_hashtable() to glib frontend Builds named destinations hash table in document. bug 97262 --- glib/poppler-document.cc | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ glib/poppler-document.h | 2 ++ 2 files changed, 85 insertions(+) diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc index a03f1d9..961b63d 100644 --- a/glib/poppler-document.cc +++ b/glib/poppler-document.cc @@ -2,6 +2,7 @@ * Copyright (C) 2005, Red Hat, Inc. * * Copyright (C) 2016 Jakub Kucharski + * Copyright (C) 2016 Masamichi Hosoda * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -712,6 +713,88 @@ poppler_document_find_dest (PopplerDocument *document, return dest; } +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 fb0ac72..b98fe13 100644 --- a/glib/poppler-document.h +++ b/glib/poppler-document.h @@ -2,6 +2,7 @@ * Copyright (C) 2004, Red Hat, Inc. * * Copyright (C) 2016 Jakub Kucharski + * Copyright (C) 2016 Masamichi Hosoda * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -240,6 +241,7 @@ GList *poppler_document_get_attachments (PopplerDocument *do /* Links */ PopplerDest *poppler_document_find_dest (PopplerDocument *document, const gchar *link_name); +GHashTable *poppler_document_build_dests_hashtable (PopplerDocument *document); /* Form */ PopplerFormField *poppler_document_get_form_field (PopplerDocument *document, -- 2.8.3