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.
23 #include <libdjvu/miniexp.h>
24 #include "djvu-document.h"
25 #include "djvu-links.h"
26 #include "djvu-document-private.h"
27 #include "ev-document-links.h"
30 static gboolean number_from_miniexp(miniexp_t sexp, int *number)
32 if (miniexp_numberp (sexp)) {
33 *number = miniexp_to_int (sexp);
40 static gboolean string_from_miniexp(miniexp_t sexp, const char **str)
42 if (miniexp_stringp (sexp)) {
43 *str = miniexp_to_str (sexp);
50 static gboolean number_from_string_10(const gchar *str, guint64 *number)
54 *number = g_ascii_strtoull(str, &end_ptr, 10);
55 if (*end_ptr == '\0') {
63 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 ) {
173 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
174 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
176 if (!g_utf8_validate (title, -1, NULL)) {
179 utf8_title = str_to_utf8 (title);
180 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 (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)
231 ddjvu_pageinfo_t info;
235 if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
236 && miniexp_length (iter) == 5) {
237 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
238 int minx, miny, width, height;
240 iter = miniexp_cdr (iter);
241 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
242 iter = miniexp_cdr (iter);
243 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
244 iter = miniexp_cdr (iter);
245 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
246 iter = miniexp_cdr (iter);
247 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
249 ev_link_mapping->x1 = minx;
250 ev_link_mapping->x2 = (minx + width);
251 ev_link_mapping->y1 = (page_info->height - (miny + height));
252 ev_link_mapping->y2 = (page_info->height - miny);
253 } else if (miniexp_car (iter) == miniexp_symbol ("poly")
254 && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
256 /* FIXME: get bounding box since Evince doesn't support shaped links */
257 int minx = G_MAXINT, miny = G_MAXINT;
258 int maxx = G_MININT, maxy = G_MININT;
260 iter = miniexp_cdr(iter);
261 while (iter != miniexp_nil) {
264 if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
265 iter = miniexp_cdr (iter);
266 if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
267 iter = miniexp_cdr (iter);
269 minx = MIN (minx, x);
270 miny = MIN (miny, y);
271 maxx = MAX (maxx, x);
272 maxy = MAX (maxy, y);
275 ev_link_mapping->x1 = minx;
276 ev_link_mapping->x2 = maxx;
277 ev_link_mapping->y1 = (page_info->height - maxy);
278 ev_link_mapping->y2 = (page_info->height - miny);
287 g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
291 static EvLinkMapping *
292 get_djvu_hyperlink_mapping (DjvuDocument *djvu_document,
294 ddjvu_pageinfo_t *page_info,
297 EvLinkMapping *ev_link_mapping = NULL;
298 EvLinkAction *ev_action = NULL;
300 const char *url, *url_target, *comment;
302 ev_link_mapping = g_new (EvLinkMapping, 1);
306 if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
308 iter = miniexp_cdr(iter);
310 if (miniexp_caar(iter) == miniexp_symbol("url")) {
311 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
312 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
314 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
318 iter = miniexp_cdr (iter);
319 if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
321 iter = miniexp_cdr (iter);
322 if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
324 iter = miniexp_cdr (iter);
325 /* FIXME: DjVu hyperlink attributes are ignored */
327 ev_action = get_djvu_link_action (djvu_document, url, page);
328 if (!ev_action) goto unknown_mapping;
330 ev_link_mapping->link = ev_link_new (comment, ev_action);
332 return ev_link_mapping;
335 if (ev_link_mapping) g_free(ev_link_mapping);
336 g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
342 djvu_links_has_document_links (EvDocumentLinks *document_links)
344 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
347 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
348 djvu_handle_events (djvu_document, TRUE);
351 ddjvu_miniexp_release (djvu_document->d_document, outline);
359 djvu_links_get_links (EvDocumentLinks *document_links,
363 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
364 GList *retval = NULL;
365 miniexp_t page_annotations = miniexp_nil;
366 miniexp_t *hyperlinks = NULL, *iter = NULL;
367 EvLinkMapping *ev_link_mapping;
368 ddjvu_pageinfo_t page_info;
370 while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
371 djvu_handle_events (djvu_document, TRUE);
373 while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
374 djvu_handle_events(djvu_document, TRUE);
376 if (page_annotations) {
377 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
379 for (iter = hyperlinks; *iter; ++iter) {
380 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
381 if (ev_link_mapping) {
382 ev_link_mapping->x1 *= scale_factor;
383 ev_link_mapping->x2 *= scale_factor;
384 ev_link_mapping->y1 *= scale_factor;
385 ev_link_mapping->y2 *= scale_factor;
386 retval = g_list_prepend (retval, ev_link_mapping);
391 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
398 djvu_links_find_link_dest (EvDocumentLinks *document_links,
399 const gchar *link_name)
401 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
402 EvLinkDest *ev_dest = NULL;
404 ev_dest = get_djvu_link_dest (DJVU_DOCUMENT (document_links), link_name, -1);
407 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
414 djvu_links_get_links_model (EvDocumentLinks *document_links)
416 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
417 GtkTreeModel *model = NULL;
418 miniexp_t outline = miniexp_nil;
420 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
421 djvu_handle_events (djvu_document, TRUE);
424 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
429 build_tree (djvu_document, model, NULL, outline);
431 ddjvu_miniexp_release (djvu_document->d_document, outline);