]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-links.c
Include config.h. Bug #504721.
[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 <config.h>
22 #include <string.h>
23 #include <glib.h>
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
30
31 static gboolean number_from_miniexp(miniexp_t sexp, int *number)
32 {
33         if (miniexp_numberp (sexp)) {
34                 *number = miniexp_to_int (sexp);
35                 return TRUE;
36         } else {
37                 return FALSE;
38         }
39 }
40
41 static gboolean string_from_miniexp(miniexp_t sexp, const char **str)
42 {
43         if (miniexp_stringp (sexp)) {
44                 *str = miniexp_to_str (sexp);
45                 return TRUE;
46         } else {
47                 return FALSE;
48         }
49 }
50
51 static gboolean number_from_string_10(const gchar *str, guint64 *number)
52 {
53         gchar *end_ptr;
54
55         *number = g_ascii_strtoull(str, &end_ptr, 10);
56         if (*end_ptr == '\0') {
57                 return TRUE;
58         } else {
59                 return FALSE;
60         }
61 }
62
63 static EvLinkDest *
64 get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
65 {
66         guint64 page_num = 0;
67         gchar *end_ptr;
68
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);
74                         }
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);
78                         }
79                 } else {
80                         if (number_from_string_10 (link_name + 1, &page_num)) {
81                                 return ev_link_dest_new_page (page_num - 1);
82                         }
83                 }
84         } else {
85                 /* FIXME: component file identifiers */
86         }
87
88         return NULL;
89 }
90
91 static EvLinkAction *
92 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
93 {
94         EvLinkDest *ev_dest = NULL;
95         EvLinkAction *ev_action = NULL;
96
97         ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
98
99         if (ev_dest) {
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);
104         } else {
105                 /* FIXME: component file identifiers */
106         }
107
108         return ev_action;
109 }
110
111 static gchar *
112 str_to_utf8 (const gchar *text)
113 {
114         static const gchar *encodings_to_try[2];
115         static gint n_encodings_to_try = 0;
116         gchar *utf8_text = NULL;
117         gint i;
118
119         if (n_encodings_to_try == 0) {
120                 const gchar *charset;
121                 gboolean charset_is_utf8;
122
123                 charset_is_utf8 = g_get_charset (&charset);
124                 if (!charset_is_utf8) {
125                         encodings_to_try[n_encodings_to_try++] = charset;
126                 }
127
128                 if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
129                         encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
130                 }
131         }
132
133         for (i = 0; i < n_encodings_to_try; i++) {
134                 utf8_text = g_convert (text, -1, "UTF-8",
135                                        encodings_to_try[i],
136                                        NULL, NULL, NULL);
137                 if (utf8_text)
138                         break;
139         }
140
141         return utf8_text;
142 }
143
144 /**
145  * Builds the index GtkTreeModel from DjVu s-expr
146  *
147  * (bookmarks
148  *   ("title1" "dest1"
149  *     ("title12" "dest12"
150  *       ... )
151  *     ... )
152  *   ("title2" "dest2"
153  *     ... )
154  *   ... )
155  */
156 static void
157 build_tree (const DjvuDocument *djvu_document,
158             GtkTreeModel       *model,
159             GtkTreeIter        *parent,
160             miniexp_t           iter)
161 {
162         const char *title, *link_dest;
163         char *title_markup;
164
165         EvLinkAction *ev_action = NULL;
166         EvLink *ev_link = NULL;
167         GtkTreeIter tree_iter;
168
169         if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
170                 /* The (bookmarks) cons */
171                 iter = miniexp_cdr (iter);
172         } else if ( miniexp_length (iter) >= 2 ) {
173                 /* An entry */
174                 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
175                 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
176
177                 if (!g_utf8_validate (title, -1, NULL)) {
178                         gchar *utf8_title;
179
180                         utf8_title = str_to_utf8 (title);
181                         title_markup = g_markup_escape_text (utf8_title, -1);
182                         g_free (utf8_title);
183                 } else {
184                         title_markup = g_markup_escape_text (title, -1);
185                 }
186
187                 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
188                 
189                 if (g_str_has_suffix (link_dest, ".djvu")) {
190                         /* FIXME: component file identifiers */
191                 } else if (ev_action) {
192                         ev_link = ev_link_new (title, ev_action);
193                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
194                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
195                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
196                                             EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
197                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
198                                             -1);
199                         g_object_unref (ev_link);
200                 } else {
201                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
202                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
203                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
204                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
205                                             -1);
206                 }
207
208                 g_free (title_markup);
209                 
210                 iter = miniexp_cddr (iter);
211                 parent = &tree_iter;
212         } else {
213                 goto unknown_entry;
214         }
215
216         for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
217                 build_tree (djvu_document, model, parent, miniexp_car (iter));
218         }
219         return;
220
221  unknown_entry:
222         g_warning ("DjvuLibre error: Unknown entry in bookmarks");
223         return;
224 }
225
226 static gboolean
227 get_djvu_hyperlink_area (ddjvu_pageinfo_t   *page_info,
228                          miniexp_t           sexp,
229                          EvLinkMapping      *ev_link_mapping)
230 {
231         miniexp_t iter;
232         ddjvu_pageinfo_t info;
233
234         iter = sexp;
235         
236         if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
237             && miniexp_length (iter) == 5) {
238                 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
239                 int minx, miny, width, height;
240
241                 iter = miniexp_cdr (iter);
242                 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
243                 iter = miniexp_cdr (iter);
244                 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
245                 iter = miniexp_cdr (iter);
246                 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
247                 iter = miniexp_cdr (iter);
248                 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
249
250                 ev_link_mapping->x1 = minx;
251                 ev_link_mapping->x2 = (minx + width);
252                 ev_link_mapping->y1 = (page_info->height - (miny + height));
253                 ev_link_mapping->y2 = (page_info->height - miny);
254         } else if (miniexp_car (iter) == miniexp_symbol ("poly")
255                    && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
256                 
257                 /* FIXME: get bounding box since Evince doesn't support shaped links */
258                 int minx = G_MAXINT, miny = G_MAXINT;
259                 int maxx = G_MININT, maxy = G_MININT;
260
261                 iter = miniexp_cdr(iter);
262                 while (iter != miniexp_nil) {
263                         int x, y;
264
265                         if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
266                         iter = miniexp_cdr (iter);
267                         if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
268                         iter = miniexp_cdr (iter);
269
270                         minx = MIN (minx, x);
271                         miny = MIN (miny, y);
272                         maxx = MAX (maxx, x);
273                         maxy = MAX (maxy, y);
274                 }
275
276                 ev_link_mapping->x1 = minx;
277                 ev_link_mapping->x2 = maxx;
278                 ev_link_mapping->y1 = (page_info->height - maxy);
279                 ev_link_mapping->y2 = (page_info->height - miny);
280         } else {
281                 /* unknown */
282                 goto unknown_link;
283         }
284
285         return TRUE;
286         
287  unknown_link:
288         g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
289         return FALSE;
290 }
291
292 static EvLinkMapping *
293 get_djvu_hyperlink_mapping (DjvuDocument     *djvu_document,
294                             int               page,
295                             ddjvu_pageinfo_t *page_info,
296                             miniexp_t         sexp)
297 {
298         EvLinkMapping *ev_link_mapping = NULL;
299         EvLinkAction *ev_action = NULL;
300         miniexp_t iter;
301         const char *url, *url_target, *comment;
302
303         ev_link_mapping = g_new (EvLinkMapping, 1);
304
305         iter = sexp;
306
307         if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
308
309         iter = miniexp_cdr(iter);
310
311         if (miniexp_caar(iter) == miniexp_symbol("url")) {
312                 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
313                 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
314         } else {
315                 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
316                 url_target = NULL;
317         }
318
319         iter = miniexp_cdr (iter);
320         if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
321
322         iter = miniexp_cdr (iter);
323         if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
324
325         iter = miniexp_cdr (iter);
326         /* FIXME: DjVu hyperlink attributes are ignored */
327
328         ev_action = get_djvu_link_action (djvu_document, url, page);
329         if (!ev_action) goto unknown_mapping;
330
331         ev_link_mapping->link = ev_link_new (comment, ev_action);
332                         
333         return ev_link_mapping;
334
335  unknown_mapping:
336         if (ev_link_mapping) g_free(ev_link_mapping);
337         g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
338         return NULL;
339 }
340
341
342 gboolean
343 djvu_links_has_document_links (EvDocumentLinks *document_links)
344 {
345         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
346         miniexp_t outline;
347
348         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
349                 djvu_handle_events (djvu_document, TRUE);
350
351         if (outline) {
352                 ddjvu_miniexp_release (djvu_document->d_document, outline);
353                 return TRUE;
354         }
355         
356         return FALSE;
357 }
358
359 GList *
360 djvu_links_get_links (EvDocumentLinks *document_links,
361                       gint             page,
362                       double           scale_factor)
363 {
364         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
365         GList *retval = NULL;
366         miniexp_t page_annotations = miniexp_nil;
367         miniexp_t *hyperlinks = NULL, *iter = NULL;
368         EvLinkMapping *ev_link_mapping;
369         ddjvu_pageinfo_t page_info;
370
371         while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
372                 djvu_handle_events (djvu_document, TRUE);
373
374         while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
375                 djvu_handle_events(djvu_document, TRUE);
376
377         if (page_annotations) {
378                 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
379                 if (hyperlinks) {
380                         for (iter = hyperlinks; *iter; ++iter) {
381                                 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
382                                 if (ev_link_mapping) {
383                                         ev_link_mapping->x1 *= scale_factor;
384                                         ev_link_mapping->x2 *= scale_factor;
385                                         ev_link_mapping->y1 *= scale_factor;
386                                         ev_link_mapping->y2 *= scale_factor;
387                                         retval = g_list_prepend (retval, ev_link_mapping);
388                                 }
389                         }
390                         free (hyperlinks);
391                 }
392                 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
393         }
394         
395         return retval;
396 }
397
398 EvLinkDest *
399 djvu_links_find_link_dest (EvDocumentLinks  *document_links,
400                            const gchar      *link_name)
401 {
402         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
403         EvLinkDest *ev_dest = NULL;
404         
405         ev_dest = get_djvu_link_dest (DJVU_DOCUMENT (document_links), link_name, -1);
406
407         if (!ev_dest) {
408                 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
409         }
410         
411         return ev_dest;
412 }
413
414 GtkTreeModel *
415 djvu_links_get_links_model (EvDocumentLinks *document_links)
416 {
417         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
418         GtkTreeModel *model = NULL;
419         miniexp_t outline = miniexp_nil;
420
421         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
422                 djvu_handle_events (djvu_document, TRUE);
423
424         if (outline) {
425                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
426                                                              G_TYPE_STRING,
427                                                              G_TYPE_OBJECT,
428                                                              G_TYPE_BOOLEAN,
429                                                              G_TYPE_STRING);
430                 build_tree (djvu_document, model, NULL, outline);
431
432                 ddjvu_miniexp_release (djvu_document->d_document, outline);
433         }
434         
435         return model;
436 }