1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Implements hyperlink functionality for Djvu files.
4 * Copyright (C) 2006 Pauli Virtanen <pav@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <libdjvu/miniexp.h>
25 #include "djvu-document.h"
26 #include "djvu-links.h"
27 #include "djvu-document-private.h"
28 #include "ev-document-links.h"
31 static gboolean number_from_miniexp(miniexp_t sexp, int *number)
33 if (miniexp_numberp (sexp)) {
34 *number = miniexp_to_int (sexp);
41 static gboolean string_from_miniexp(miniexp_t sexp, const char **str)
43 if (miniexp_stringp (sexp)) {
44 *str = miniexp_to_str (sexp);
51 static gboolean number_from_string_10(const gchar *str, guint64 *number)
55 *number = g_ascii_strtoull(str, &end_ptr, 10);
56 if (*end_ptr == '\0') {
64 get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
68 /* #pagenum, #+pageoffset, #-pageoffset */
69 if (g_str_has_prefix (link_name, "#")) {
70 if (base_page > 0 && g_str_has_prefix (link_name+1, "+")) {
71 if (number_from_string_10 (link_name + 2, &page_num)) {
72 return ev_link_dest_new_page (base_page + page_num);
74 } else if (base_page > 0 && g_str_has_prefix (link_name+1, "-")) {
75 if (number_from_string_10 (link_name + 2, &page_num)) {
76 return ev_link_dest_new_page (base_page - page_num);
79 if (number_from_string_10 (link_name + 1, &page_num)) {
80 return ev_link_dest_new_page (page_num - 1);
84 /* FIXME: component file identifiers */
91 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
93 EvLinkDest *ev_dest = NULL;
94 EvLinkAction *ev_action = NULL;
96 ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
99 ev_action = ev_link_action_new_dest (ev_dest);
100 } else if (strstr(link_name, "://") != NULL) {
101 /* It's probably an URI */
102 ev_action = ev_link_action_new_external_uri (link_name);
104 /* FIXME: component file identifiers */
111 str_to_utf8 (const gchar *text)
113 static const gchar *encodings_to_try[2];
114 static gint n_encodings_to_try = 0;
115 gchar *utf8_text = NULL;
118 if (n_encodings_to_try == 0) {
119 const gchar *charset;
120 gboolean charset_is_utf8;
122 charset_is_utf8 = g_get_charset (&charset);
123 if (!charset_is_utf8) {
124 encodings_to_try[n_encodings_to_try++] = charset;
127 if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
128 encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
132 for (i = 0; i < n_encodings_to_try; i++) {
133 utf8_text = g_convert (text, -1, "UTF-8",
144 * Builds the index GtkTreeModel from DjVu s-expr
148 * ("title12" "dest12"
156 build_tree (const DjvuDocument *djvu_document,
161 const char *title, *link_dest;
164 EvLinkAction *ev_action = NULL;
165 EvLink *ev_link = NULL;
166 GtkTreeIter tree_iter;
168 if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
169 /* The (bookmarks) cons */
170 iter = miniexp_cdr (iter);
171 } else if ( miniexp_length (iter) >= 2 ) {
172 gchar *utf8_title = NULL;
175 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
176 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
179 if (!g_utf8_validate (title, -1, NULL)) {
180 utf8_title = str_to_utf8 (title);
181 title_markup = g_markup_escape_text (utf8_title, -1);
183 title_markup = g_markup_escape_text (title, -1);
186 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
188 if (g_str_has_suffix (link_dest, ".djvu")) {
189 /* FIXME: component file identifiers */
190 } else if (ev_action) {
191 ev_link = ev_link_new (utf8_title ? utf8_title : title, ev_action);
192 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
193 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
194 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
195 EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
196 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
198 g_object_unref (ev_link);
200 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
201 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
202 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
203 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
207 g_free (title_markup);
209 iter = miniexp_cddr (iter);
215 for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
216 build_tree (djvu_document, model, parent, miniexp_car (iter));
221 g_warning ("DjvuLibre error: Unknown entry in bookmarks");
226 get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info,
228 EvLinkMapping *ev_link_mapping)
234 if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
235 && miniexp_length (iter) == 5) {
236 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
237 int minx, miny, width, height;
239 iter = miniexp_cdr (iter);
240 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
241 iter = miniexp_cdr (iter);
242 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
243 iter = miniexp_cdr (iter);
244 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
245 iter = miniexp_cdr (iter);
246 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
248 ev_link_mapping->x1 = minx;
249 ev_link_mapping->x2 = (minx + width);
250 ev_link_mapping->y1 = (page_info->height - (miny + height));
251 ev_link_mapping->y2 = (page_info->height - miny);
252 } else if (miniexp_car (iter) == miniexp_symbol ("poly")
253 && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
255 /* FIXME: get bounding box since Evince doesn't support shaped links */
256 int minx = G_MAXINT, miny = G_MAXINT;
257 int maxx = G_MININT, maxy = G_MININT;
259 iter = miniexp_cdr(iter);
260 while (iter != miniexp_nil) {
263 if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
264 iter = miniexp_cdr (iter);
265 if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
266 iter = miniexp_cdr (iter);
268 minx = MIN (minx, x);
269 miny = MIN (miny, y);
270 maxx = MAX (maxx, x);
271 maxy = MAX (maxy, y);
274 ev_link_mapping->x1 = minx;
275 ev_link_mapping->x2 = maxx;
276 ev_link_mapping->y1 = (page_info->height - maxy);
277 ev_link_mapping->y2 = (page_info->height - miny);
286 g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
290 static EvLinkMapping *
291 get_djvu_hyperlink_mapping (DjvuDocument *djvu_document,
293 ddjvu_pageinfo_t *page_info,
296 EvLinkMapping *ev_link_mapping = NULL;
297 EvLinkAction *ev_action = NULL;
299 const char *url, *url_target, *comment;
301 ev_link_mapping = g_new (EvLinkMapping, 1);
305 if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
307 iter = miniexp_cdr(iter);
309 if (miniexp_caar(iter) == miniexp_symbol("url")) {
310 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
311 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
313 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
317 iter = miniexp_cdr (iter);
318 if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
320 iter = miniexp_cdr (iter);
321 if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
323 iter = miniexp_cdr (iter);
324 /* FIXME: DjVu hyperlink attributes are ignored */
326 ev_action = get_djvu_link_action (djvu_document, url, page);
327 if (!ev_action) goto unknown_mapping;
329 ev_link_mapping->link = ev_link_new (comment, ev_action);
331 return ev_link_mapping;
334 if (ev_link_mapping) g_free(ev_link_mapping);
335 g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
341 djvu_links_has_document_links (EvDocumentLinks *document_links)
343 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
346 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
347 djvu_handle_events (djvu_document, TRUE, NULL);
350 ddjvu_miniexp_release (djvu_document->d_document, outline);
358 djvu_links_get_links (EvDocumentLinks *document_links,
362 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
363 GList *retval = NULL;
364 miniexp_t page_annotations = miniexp_nil;
365 miniexp_t *hyperlinks = NULL, *iter = NULL;
366 EvLinkMapping *ev_link_mapping;
367 ddjvu_pageinfo_t page_info;
369 while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
370 djvu_handle_events (djvu_document, TRUE, NULL);
372 while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
373 djvu_handle_events(djvu_document, TRUE, NULL);
375 if (page_annotations) {
376 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
378 for (iter = hyperlinks; *iter; ++iter) {
379 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
380 if (ev_link_mapping) {
381 ev_link_mapping->x1 *= scale_factor;
382 ev_link_mapping->x2 *= scale_factor;
383 ev_link_mapping->y1 *= scale_factor;
384 ev_link_mapping->y2 *= scale_factor;
385 retval = g_list_prepend (retval, ev_link_mapping);
390 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
397 djvu_links_find_link_dest (EvDocumentLinks *document_links,
398 const gchar *link_name)
400 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
401 EvLinkDest *ev_dest = NULL;
403 ev_dest = get_djvu_link_dest (djvu_document, link_name, -1);
406 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
413 djvu_links_get_links_model (EvDocumentLinks *document_links)
415 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
416 GtkTreeModel *model = NULL;
417 miniexp_t outline = miniexp_nil;
419 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
420 djvu_handle_events (djvu_document, TRUE, NULL);
423 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
428 build_tree (djvu_document, model, NULL, outline);
430 ddjvu_miniexp_release (djvu_document->d_document, outline);