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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
29 #include "ev-mapping-list.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_page (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 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 base_page - page_num;
79 if (number_from_string_10 (link_name + 1, &page_num)) {
84 /* FIXME: component file identifiers */
91 get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
93 return ev_link_dest_new_page (get_djvu_link_page (djvu_document, link_name, base_page));
97 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
99 EvLinkDest *ev_dest = NULL;
100 EvLinkAction *ev_action = NULL;
102 ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
105 ev_action = ev_link_action_new_dest (ev_dest);
106 } else if (strstr(link_name, "://") != NULL) {
107 /* It's probably an URI */
108 ev_action = ev_link_action_new_external_uri (link_name);
110 /* FIXME: component file identifiers */
117 str_to_utf8 (const gchar *text)
119 static const gchar *encodings_to_try[2];
120 static gint n_encodings_to_try = 0;
121 gchar *utf8_text = NULL;
124 if (n_encodings_to_try == 0) {
125 const gchar *charset;
126 gboolean charset_is_utf8;
128 charset_is_utf8 = g_get_charset (&charset);
129 if (!charset_is_utf8) {
130 encodings_to_try[n_encodings_to_try++] = charset;
133 if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
134 encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
138 for (i = 0; i < n_encodings_to_try; i++) {
139 utf8_text = g_convert (text, -1, "UTF-8",
150 * Builds the index GtkTreeModel from DjVu s-expr
154 * ("title12" "dest12"
162 build_tree (const DjvuDocument *djvu_document,
167 const char *title, *link_dest;
170 EvLinkAction *ev_action = NULL;
171 EvLink *ev_link = NULL;
172 GtkTreeIter tree_iter;
174 if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
175 /* The (bookmarks) cons */
176 iter = miniexp_cdr (iter);
177 } else if ( miniexp_length (iter) >= 2 ) {
178 gchar *utf8_title = NULL;
181 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
182 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
185 if (!g_utf8_validate (title, -1, NULL)) {
186 utf8_title = str_to_utf8 (title);
187 title_markup = g_markup_escape_text (utf8_title, -1);
189 title_markup = g_markup_escape_text (title, -1);
192 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
194 if (g_str_has_suffix (link_dest, ".djvu")) {
195 /* FIXME: component file identifiers */
196 } else if (ev_action) {
197 ev_link = ev_link_new (utf8_title ? utf8_title : title, ev_action);
198 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
199 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
200 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
201 EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
202 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
204 g_object_unref (ev_link);
206 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
207 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
208 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
209 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
213 g_free (title_markup);
215 iter = miniexp_cddr (iter);
221 for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
222 build_tree (djvu_document, model, parent, miniexp_car (iter));
227 g_warning ("DjvuLibre error: Unknown entry in bookmarks");
232 get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info,
234 EvMapping *ev_link_mapping)
240 if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
241 && miniexp_length (iter) == 5) {
242 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
243 int minx, miny, width, height;
245 iter = miniexp_cdr (iter);
246 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
247 iter = miniexp_cdr (iter);
248 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
249 iter = miniexp_cdr (iter);
250 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
251 iter = miniexp_cdr (iter);
252 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
254 ev_link_mapping->area.x1 = minx;
255 ev_link_mapping->area.x2 = (minx + width);
256 ev_link_mapping->area.y1 = (page_info->height - (miny + height));
257 ev_link_mapping->area.y2 = (page_info->height - miny);
258 } else if (miniexp_car (iter) == miniexp_symbol ("poly")
259 && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
261 /* FIXME: get bounding box since Evince doesn't support shaped links */
262 int minx = G_MAXINT, miny = G_MAXINT;
263 int maxx = G_MININT, maxy = G_MININT;
265 iter = miniexp_cdr(iter);
266 while (iter != miniexp_nil) {
269 if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
270 iter = miniexp_cdr (iter);
271 if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
272 iter = miniexp_cdr (iter);
274 minx = MIN (minx, x);
275 miny = MIN (miny, y);
276 maxx = MAX (maxx, x);
277 maxy = MAX (maxy, y);
280 ev_link_mapping->area.x1 = minx;
281 ev_link_mapping->area.x2 = maxx;
282 ev_link_mapping->area.y1 = (page_info->height - maxy);
283 ev_link_mapping->area.y2 = (page_info->height - miny);
292 g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
297 get_djvu_hyperlink_mapping (DjvuDocument *djvu_document,
299 ddjvu_pageinfo_t *page_info,
302 EvMapping *ev_link_mapping = NULL;
303 EvLinkAction *ev_action = NULL;
305 const char *url, *url_target, *comment;
307 ev_link_mapping = g_new (EvMapping, 1);
311 if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
313 iter = miniexp_cdr(iter);
315 if (miniexp_caar(iter) == miniexp_symbol("url")) {
316 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
317 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
319 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
323 iter = miniexp_cdr (iter);
324 if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
326 iter = miniexp_cdr (iter);
327 if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
329 iter = miniexp_cdr (iter);
330 /* FIXME: DjVu hyperlink attributes are ignored */
332 ev_action = get_djvu_link_action (djvu_document, url, page);
333 if (!ev_action) goto unknown_mapping;
335 ev_link_mapping->data = ev_link_new (comment, ev_action);
337 return ev_link_mapping;
340 if (ev_link_mapping) g_free(ev_link_mapping);
341 g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
347 djvu_links_has_document_links (EvDocumentLinks *document_links)
349 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
352 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
353 djvu_handle_events (djvu_document, TRUE, NULL);
356 ddjvu_miniexp_release (djvu_document->d_document, outline);
364 djvu_links_get_links (EvDocumentLinks *document_links,
368 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
369 GList *retval = NULL;
370 miniexp_t page_annotations = miniexp_nil;
371 miniexp_t *hyperlinks = NULL, *iter = NULL;
372 EvMapping *ev_link_mapping;
373 ddjvu_pageinfo_t page_info;
375 while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
376 djvu_handle_events (djvu_document, TRUE, NULL);
378 while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
379 djvu_handle_events(djvu_document, TRUE, NULL);
381 if (page_annotations) {
382 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
384 for (iter = hyperlinks; *iter; ++iter) {
385 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
386 if (ev_link_mapping) {
387 ev_link_mapping->area.x1 *= scale_factor;
388 ev_link_mapping->area.x2 *= scale_factor;
389 ev_link_mapping->area.y1 *= scale_factor;
390 ev_link_mapping->area.y2 *= scale_factor;
391 retval = g_list_prepend (retval, ev_link_mapping);
396 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
399 return ev_mapping_list_new (page, retval, (GDestroyNotify)g_object_unref);
403 djvu_links_find_link_dest (EvDocumentLinks *document_links,
404 const gchar *link_name)
406 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
407 EvLinkDest *ev_dest = NULL;
409 ev_dest = get_djvu_link_dest (djvu_document, link_name, -1);
412 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
419 djvu_links_find_link_page (EvDocumentLinks *document_links,
420 const gchar *link_name)
422 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
425 page = get_djvu_link_page (djvu_document, link_name, -1);
428 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
435 djvu_links_get_links_model (EvDocumentLinks *document_links)
437 DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
438 GtkTreeModel *model = NULL;
439 miniexp_t outline = miniexp_nil;
441 while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
442 djvu_handle_events (djvu_document, TRUE, NULL);
445 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
450 build_tree (djvu_document, model, NULL, outline);
452 ddjvu_miniexp_release (djvu_document->d_document, outline);