Commit 266f55bf authored by Masamichi Hosoda's avatar Masamichi Hosoda

Add poppler_document_build_dests_tree() to glib frontend

Builds named destinations balanced binary tree in document.
parent 8984a710
Pipeline #14716 passed with stage
in 7 minutes and 7 seconds
......@@ -3,6 +3,7 @@
*
* Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
* Copyright (C) 2018 Marek Kasik <mkasik@redhat.com>
* Copyright (C) 2019 Masamichi Hosoda <trueroad@trueroad.jp>
*
* 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
......@@ -833,6 +834,101 @@ poppler_document_find_dest (PopplerDocument *document,
return dest;
}
static gint
_poppler_dest_compare_keys (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
return g_strcmp0 (static_cast<const gchar*>(a),
static_cast<const gchar*>(b));
}
static void
_poppler_dest_destroy_key (gpointer key)
{
g_free (static_cast<gchar*>(key));
}
static void
_poppler_dest_destroy_value (gpointer value)
{
poppler_dest_free (static_cast<PopplerDest*>(value));
}
/**
* poppler_document_build_dests_tree:
* @document: A #PopplerDocument
*
* Builds named destinations balanced binary tree in @document
*
* Return value: a #GTree or %NULL if @document is not valid.
* The tree key is the #gchar* which constains a destination name.
* Note that the names are bytestrings, not string.
* So the tree keys need to be converted using
* poppler_named_dest_from_bytestring() and
* poppler_named_dest_to_bytestring() .
* The tree value is the #PopplerDest which contains a named destination.
* Returned value must be freed with #g_tree_destroy.
*
* Since: 0.74
**/
GTree *
poppler_document_build_dests_tree (PopplerDocument *document)
{
GTree *tree;
Catalog *catalog;
LinkDest *link_dest;
PopplerDest *dest;
int i, len;
gchar *key;
g_return_val_if_fail (POPPLER_IS_DOCUMENT (document), nullptr);
catalog = document->doc->getCatalog ();
if (catalog == nullptr)
return nullptr;
tree = g_tree_new_full (_poppler_dest_compare_keys, nullptr,
_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 = poppler_named_dest_from_bytestring
(reinterpret_cast<const guint8*>
(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 = nullptr;
g_tree_insert (tree, key, dest);
}
// Iterate form name-tree
len = catalog->numDestNameTree ();
for (i = 0; i < len; ++i) {
key = poppler_named_dest_from_bytestring
(reinterpret_cast<const guint8*>
(catalog->getDestNameTreeName (i)->c_str ()),
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 = nullptr;
g_tree_insert (tree, key, dest);
}
return tree;
}
char *_poppler_goo_string_to_utf8(const GooString *s)
{
if (s == nullptr) {
......
......@@ -3,6 +3,7 @@
*
* Copyright (C) 2016 Jakub Alba <jakubalba@gmail.com>
* Copyright (C) 2018 Marek Kasik <mkasik@redhat.com>
* Copyright (C) 2019 Masamichi Hosoda <trueroad@trueroad.jp>
*
* 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
......@@ -386,6 +387,8 @@ GList *poppler_document_get_attachments (PopplerDocument *do
POPPLER_PUBLIC
PopplerDest *poppler_document_find_dest (PopplerDocument *document,
const gchar *link_name);
POPPLER_PUBLIC
GTree *poppler_document_build_dests_tree (PopplerDocument *document);
/* Form */
POPPLER_PUBLIC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment