]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-links.c
87057d49d4f7dcbadbe36b9e21a2c3baab1242cf
[evince.git] / backend / djvu / djvu-links.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Implements hyperlink functionality for Djvu files.
4  * Copyright (C) 2006 Pauli Virtanen <pav@iki.fi>
5  *
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)
9  * any later version.
10  *
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.
15  *
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.
19  */
20
21 #include <string.h>
22 #include <glib.h>
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"
28
29
30 static gboolean number_from_miniexp(miniexp_t sexp, int *number)
31 {
32         if (miniexp_numberp (sexp)) {
33                 *number = miniexp_to_int (sexp);
34                 return TRUE;
35         } else {
36                 return FALSE;
37         }
38 }
39
40 static gboolean string_from_miniexp(miniexp_t sexp, const char **str)
41 {
42         if (miniexp_stringp (sexp)) {
43                 *str = miniexp_to_str (sexp);
44                 return TRUE;
45         } else {
46                 return FALSE;
47         }
48 }
49
50 static gboolean number_from_string_10(const gchar *str, guint64 *number)
51 {
52         gchar *end_ptr;
53
54         *number = g_ascii_strtoull(str, &end_ptr, 10);
55         if (*end_ptr == '\0') {
56                 return TRUE;
57         } else {
58                 return FALSE;
59         }
60 }
61
62 static EvLinkDest *
63 get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
64 {
65         guint64 page_num = 0;
66         gchar *end_ptr;
67
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);
73                         }
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);
77                         }
78                 } else {
79                         if (number_from_string_10 (link_name + 1, &page_num)) {
80                                 return ev_link_dest_new_page (page_num - 1);
81                         }
82                 }
83         } else {
84                 /* FIXME: component file identifiers */
85         }
86
87         return NULL;
88 }
89
90 static EvLinkAction *
91 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
92 {
93         EvLinkDest *ev_dest = NULL;
94         EvLinkAction *ev_action = NULL;
95
96         ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
97
98         if (ev_dest) {
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);
103         } else {
104                 /* FIXME: component file identifiers */
105         }
106
107         return ev_action;
108 }
109
110 static gchar *
111 str_to_utf8 (const gchar *text)
112 {
113         static const gchar *encodings_to_try[2];
114         static gint n_encodings_to_try = 0;
115         gchar *utf8_text = NULL;
116         gint i;
117
118         if (n_encodings_to_try == 0) {
119                 const gchar *charset;
120                 gboolean charset_is_utf8;
121
122                 charset_is_utf8 = g_get_charset (&charset);
123                 if (!charset_is_utf8) {
124                         encodings_to_try[n_encodings_to_try++] = charset;
125                 }
126
127                 if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
128                         encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
129                 }
130         }
131
132         for (i = 0; i < n_encodings_to_try; i++) {
133                 utf8_text = g_convert (text, -1, "UTF-8",
134                                        encodings_to_try[i],
135                                        NULL, NULL, NULL);
136                 if (utf8_text)
137                         break;
138         }
139
140         return utf8_text;
141 }
142
143 /**
144  * Builds the index GtkTreeModel from DjVu s-expr
145  *
146  * (bookmarks
147  *   ("title1" "dest1"
148  *     ("title12" "dest12"
149  *       ... )
150  *     ... )
151  *   ("title2" "dest2"
152  *     ... )
153  *   ... )
154  */
155 static void
156 build_tree (const DjvuDocument *djvu_document,
157             GtkTreeModel       *model,
158             GtkTreeIter        *parent,
159             miniexp_t           iter)
160 {
161         const char *title, *link_dest;
162         char *title_markup;
163
164         EvLinkAction *ev_action = NULL;
165         EvLink *ev_link = NULL;
166         GtkTreeIter tree_iter;
167
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                 /* An entry */
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;
175
176                 if (!g_utf8_validate (title, -1, NULL)) {
177                         gchar *utf8_title;
178
179                         utf8_title = str_to_utf8 (title);
180                         title_markup = g_markup_escape_text (utf8_title, -1);
181                         g_free (utf8_title);
182                 } else {
183                         title_markup = g_markup_escape_text (title, -1);
184                 }
185
186                 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
187                 
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,
197                                             -1);
198                         g_object_unref (ev_link);
199                 } else {
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,
204                                             -1);
205                 }
206
207                 g_free (title_markup);
208                 
209                 iter = miniexp_cddr (iter);
210                 parent = &tree_iter;
211         } else {
212                 goto unknown_entry;
213         }
214
215         for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
216                 build_tree (djvu_document, model, parent, miniexp_car (iter));
217         }
218         return;
219
220  unknown_entry:
221         g_warning ("DjvuLibre error: Unknown entry in bookmarks");
222         return;
223 }
224
225 static gboolean
226 get_djvu_hyperlink_area (ddjvu_pageinfo_t   *page_info,
227                          miniexp_t           sexp,
228                          EvLinkMapping      *ev_link_mapping)
229 {
230         miniexp_t iter;
231         ddjvu_pageinfo_t info;
232
233         iter = sexp;
234         
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;
239
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;
248
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) {
255                 
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;
259
260                 iter = miniexp_cdr(iter);
261                 while (iter != miniexp_nil) {
262                         int x, y;
263
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);
268
269                         minx = MIN (minx, x);
270                         miny = MIN (miny, y);
271                         maxx = MAX (maxx, x);
272                         maxy = MAX (maxy, y);
273                 }
274
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);
279         } else {
280                 /* unknown */
281                 goto unknown_link;
282         }
283
284         return TRUE;
285         
286  unknown_link:
287         g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
288         return FALSE;
289 }
290
291 static EvLinkMapping *
292 get_djvu_hyperlink_mapping (DjvuDocument     *djvu_document,
293                             int               page,
294                             ddjvu_pageinfo_t *page_info,
295                             miniexp_t         sexp)
296 {
297         EvLinkMapping *ev_link_mapping = NULL;
298         EvLinkAction *ev_action = NULL;
299         miniexp_t iter;
300         const char *url, *url_target, *comment;
301
302         ev_link_mapping = g_new (EvLinkMapping, 1);
303
304         iter = sexp;
305
306         if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
307
308         iter = miniexp_cdr(iter);
309
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;
313         } else {
314                 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
315                 url_target = NULL;
316         }
317
318         iter = miniexp_cdr (iter);
319         if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
320
321         iter = miniexp_cdr (iter);
322         if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
323
324         iter = miniexp_cdr (iter);
325         /* FIXME: DjVu hyperlink attributes are ignored */
326
327         ev_action = get_djvu_link_action (djvu_document, url, page);
328         if (!ev_action) goto unknown_mapping;
329
330         ev_link_mapping->link = ev_link_new (comment, ev_action);
331                         
332         return ev_link_mapping;
333
334  unknown_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)));
337         return NULL;
338 }
339
340
341 gboolean
342 djvu_links_has_document_links (EvDocumentLinks *document_links)
343 {
344         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
345         miniexp_t outline;
346
347         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
348                 djvu_handle_events (djvu_document, TRUE);
349
350         if (outline) {
351                 ddjvu_miniexp_release (djvu_document->d_document, outline);
352                 return TRUE;
353         }
354         
355         return FALSE;
356 }
357
358 GList *
359 djvu_links_get_links (EvDocumentLinks *document_links,
360                       gint             page,
361                       double           scale_factor)
362 {
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;
369
370         while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
371                 djvu_handle_events (djvu_document, TRUE);
372
373         while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
374                 djvu_handle_events(djvu_document, TRUE);
375
376         if (page_annotations) {
377                 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
378                 if (hyperlinks) {
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);
387                                 }
388                         }
389                         free (hyperlinks);
390                 }
391                 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
392         }
393         
394         return retval;
395 }
396
397 EvLinkDest *
398 djvu_links_find_link_dest (EvDocumentLinks  *document_links,
399                            const gchar      *link_name)
400 {
401         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
402         EvLinkDest *ev_dest = NULL;
403         
404         ev_dest = get_djvu_link_dest (DJVU_DOCUMENT (document_links), link_name, -1);
405
406         if (!ev_dest) {
407                 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
408         }
409         
410         return ev_dest;
411 }
412
413 GtkTreeModel *
414 djvu_links_get_links_model (EvDocumentLinks *document_links)
415 {
416         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
417         GtkTreeModel *model = NULL;
418         miniexp_t outline = miniexp_nil;
419
420         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
421                 djvu_handle_events (djvu_document, TRUE);
422
423         if (outline) {
424                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
425                                                              G_TYPE_STRING,
426                                                              G_TYPE_OBJECT,
427                                                              G_TYPE_BOOLEAN,
428                                                              G_TYPE_STRING);
429                 build_tree (djvu_document, model, NULL, outline);
430
431                 ddjvu_miniexp_release (djvu_document->d_document, outline);
432         }
433         
434         return model;
435 }