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.
21 #include "djvu-document.h"
22 #include "djvu-links.h"
23 #include "djvu-document-private.h"
24 #include "ev-document-links.h"
26 #include <libdjvu/ddjvuapi.h>
27 #include <libdjvu/miniexp.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)
69 /* #pagenum, #+pageoffset, #-pageoffset */
70 if (g_str_has_prefix (link_name, "#")) {
71 if (base_page > 0 && g_str_has_prefix (link_name+1, "+")) {
72 if (number_from_string_10 (link_name + 2, &page_num)) {
73 return ev_link_dest_new_page (base_page + page_num);
75 } else if (base_page > 0 && g_str_has_prefix (link_name+1, "-")) {
76 if (number_from_string_10 (link_name + 2, &page_num)) {
77 return ev_link_dest_new_page (base_page - page_num);
80 if (number_from_string_10 (link_name + 1, &page_num)) {
81 return ev_link_dest_new_page (page_num - 1);
85 /* FIXME: component file identifiers */
92 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
94 EvLinkDest *ev_dest = NULL;
95 EvLinkAction *ev_action = NULL;
97 ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
100 ev_action = ev_link_action_new_dest (ev_dest);
101 } else if (strstr(link_name, "://") != NULL) {
102 /* It's probably an URI */
103 ev_action = ev_link_action_new_external_uri (link_name);
105 /* FIXME: component file identifiers */
112 * Builds the index GtkTreeModel from DjVu s-expr
116 * ("title12" "dest12"
124 build_tree (const DjvuDocument *djvu_document,
129 const char *title, *link_dest;
132 EvLinkAction *ev_action = NULL;
133 EvLink *ev_link = NULL;
134 GtkTreeIter tree_iter;
136 if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
137 /* The (bookmarks) cons */
138 iter = miniexp_cdr (iter);
139 } else if ( miniexp_length (iter) >= 2 ) {
141 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
142 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
144 title_markup = g_markup_escape_text (title, -1);
145 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
147 if (g_str_has_suffix (link_dest, ".djvu")) {
148 /* FIXME: component file identifiers */
149 } else if (ev_action) {
150 ev_link = ev_link_new (title, ev_action);
151 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
152 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
153 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
154 EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
155 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
158 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
159 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
160 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
161 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
165 g_free (title_markup);
167 iter = miniexp_cddr (iter);
173 for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
174 build_tree (djvu_document, model, parent, miniexp_car (iter));
179 g_warning ("DjvuLibre error: Unknown entry in bookmarks");
184 get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info,
186 EvLinkMapping *ev_link_mapping)
189 ddjvu_pageinfo_t info;
193 if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
194 && miniexp_length (iter) == 5) {
195 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
196 int minx, miny, width, height;
198 iter = miniexp_cdr (iter);
199 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
200 iter = miniexp_cdr (iter);
201 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
202 iter = miniexp_cdr (iter);
203 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
204 iter = miniexp_cdr (iter);
205 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
207 ev_link_mapping->x1 = minx;
208 ev_link_mapping->x2 = (minx + width);
209 ev_link_mapping->y1 = (page_info->height - (miny + height));
210 ev_link_mapping->y2 = (page_info->height - miny);
211 } else if (miniexp_car (iter) == miniexp_symbol ("poly")
212 && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
214 /* FIXME: get bounding box since Evince doesn't support shaped links */
215 int minx = G_MAXINT, miny = G_MAXINT;
216 int maxx = G_MININT, maxy = G_MININT;
218 iter = miniexp_cdr(iter);
219 while (iter != miniexp_nil) {
222 if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
223 iter = miniexp_cdr (iter);
224 if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
225 iter = miniexp_cdr (iter);
227 minx = MIN (minx, x);
228 miny = MIN (miny, y);
229 maxx = MAX (maxx, x);
230 maxy = MAX (maxy, y);
233 ev_link_mapping->x1 = minx;
234 ev_link_mapping->x2 = maxx;
235 ev_link_mapping->y1 = (page_info->height - maxy);
236 ev_link_mapping->y2 = (page_info->height - miny);
245 g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
249 static EvLinkMapping *
250 get_djvu_hyperlink_mapping (DjvuDocument *djvu_document,
252 ddjvu_pageinfo_t *page_info,
255 EvLinkMapping *ev_link_mapping = NULL;
256 EvLinkAction *ev_action = NULL;
258 const char *url, *url_target, *comment;
260 ev_link_mapping = g_new (EvLinkMapping, 1);
264 if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
266 iter = miniexp_cdr(iter);
268 if (miniexp_caar(iter) == miniexp_symbol("url")) {
269 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
270 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
272 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
276 iter = miniexp_cdr (iter);
277 if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
279 iter = miniexp_cdr (iter);
280 if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
282 iter = miniexp_cdr (iter);
283 /* FIXME: DjVu hyperlink attributes are ignored */
285 ev_action = get_djvu_link_action (djvu_document, url, page);
286 if (!ev_action) goto unknown_mapping;
288 ev_link_mapping->link = ev_link_new (comment, ev_action);
290 return ev_link_mapping;
293 if (ev_link_mapping) g_free(ev_link_mapping);
294 g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
300 djvu_links_has_document_links (EvDocumentLinks *document_links)
302 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
305 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
306 djvu_handle_events (djvu_document, TRUE);
309 ddjvu_miniexp_release (djvu_document->d_document, outline);
317 djvu_links_get_links (EvDocumentLinks *document_links,
321 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
322 GList *retval = NULL;
323 miniexp_t page_annotations = miniexp_nil;
324 miniexp_t *hyperlinks = NULL, *iter = NULL;
325 EvLinkMapping *ev_link_mapping;
326 ddjvu_pageinfo_t page_info;
328 while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
329 djvu_handle_events (djvu_document, TRUE);
331 while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
332 djvu_handle_events(djvu_document, TRUE);
334 if (page_annotations) {
335 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
337 for (iter = hyperlinks; *iter; ++iter) {
338 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
339 if (ev_link_mapping) {
340 ev_link_mapping->x1 *= scale_factor;
341 ev_link_mapping->x2 *= scale_factor;
342 ev_link_mapping->y1 *= scale_factor;
343 ev_link_mapping->y2 *= scale_factor;
344 retval = g_list_prepend (retval, ev_link_mapping);
349 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
356 djvu_links_find_link_dest (EvDocumentLinks *document_links,
357 const gchar *link_name)
359 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
360 EvLinkDest *ev_dest = NULL;
362 ev_dest = get_djvu_link_dest (DJVU_DOCUMENT (document_links), link_name, -1);
365 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
372 djvu_links_get_links_model (EvDocumentLinks *document_links)
374 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
375 GtkTreeModel *model = NULL;
376 miniexp_t outline = miniexp_nil;
378 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
379 djvu_handle_events (djvu_document, TRUE);
382 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
386 build_tree (djvu_document, model, NULL, outline);
388 ddjvu_miniexp_release (djvu_document->d_document, outline);