]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-links.c
Add EvMappingList data struct instead of using a GList
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include "ev-mapping-list.h"
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
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                 gchar *utf8_title = NULL;
173                 
174                 /* An entry */
175                 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
176                 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
177
178                 
179                 if (!g_utf8_validate (title, -1, NULL)) {
180                         utf8_title = str_to_utf8 (title);
181                         title_markup = g_markup_escape_text (utf8_title, -1);
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 (utf8_title ? utf8_title : 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                 g_free (utf8_title);
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                          EvMapping        *ev_link_mapping)
229 {
230         miniexp_t iter;
231
232         iter = sexp;
233         
234         if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
235             && miniexp_length (iter) == 5) {
236                 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
237                 int minx, miny, width, height;
238
239                 iter = miniexp_cdr (iter);
240                 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
241                 iter = miniexp_cdr (iter);
242                 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
243                 iter = miniexp_cdr (iter);
244                 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
245                 iter = miniexp_cdr (iter);
246                 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
247
248                 ev_link_mapping->area.x1 = minx;
249                 ev_link_mapping->area.x2 = (minx + width);
250                 ev_link_mapping->area.y1 = (page_info->height - (miny + height));
251                 ev_link_mapping->area.y2 = (page_info->height - miny);
252         } else if (miniexp_car (iter) == miniexp_symbol ("poly")
253                    && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
254                 
255                 /* FIXME: get bounding box since Evince doesn't support shaped links */
256                 int minx = G_MAXINT, miny = G_MAXINT;
257                 int maxx = G_MININT, maxy = G_MININT;
258
259                 iter = miniexp_cdr(iter);
260                 while (iter != miniexp_nil) {
261                         int x, y;
262
263                         if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
264                         iter = miniexp_cdr (iter);
265                         if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
266                         iter = miniexp_cdr (iter);
267
268                         minx = MIN (minx, x);
269                         miny = MIN (miny, y);
270                         maxx = MAX (maxx, x);
271                         maxy = MAX (maxy, y);
272                 }
273
274                 ev_link_mapping->area.x1 = minx;
275                 ev_link_mapping->area.x2 = maxx;
276                 ev_link_mapping->area.y1 = (page_info->height - maxy);
277                 ev_link_mapping->area.y2 = (page_info->height - miny);
278         } else {
279                 /* unknown */
280                 goto unknown_link;
281         }
282
283         return TRUE;
284         
285  unknown_link:
286         g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
287         return FALSE;
288 }
289
290 static EvMapping *
291 get_djvu_hyperlink_mapping (DjvuDocument     *djvu_document,
292                             int               page,
293                             ddjvu_pageinfo_t *page_info,
294                             miniexp_t         sexp)
295 {
296         EvMapping *ev_link_mapping = NULL;
297         EvLinkAction *ev_action = NULL;
298         miniexp_t iter;
299         const char *url, *url_target, *comment;
300
301         ev_link_mapping = g_new (EvMapping, 1);
302
303         iter = sexp;
304
305         if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
306
307         iter = miniexp_cdr(iter);
308
309         if (miniexp_caar(iter) == miniexp_symbol("url")) {
310                 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
311                 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
312         } else {
313                 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
314                 url_target = NULL;
315         }
316
317         iter = miniexp_cdr (iter);
318         if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
319
320         iter = miniexp_cdr (iter);
321         if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
322
323         iter = miniexp_cdr (iter);
324         /* FIXME: DjVu hyperlink attributes are ignored */
325
326         ev_action = get_djvu_link_action (djvu_document, url, page);
327         if (!ev_action) goto unknown_mapping;
328
329         ev_link_mapping->data = ev_link_new (comment, ev_action);
330                         
331         return ev_link_mapping;
332
333  unknown_mapping:
334         if (ev_link_mapping) g_free(ev_link_mapping);
335         g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
336         return NULL;
337 }
338
339
340 gboolean
341 djvu_links_has_document_links (EvDocumentLinks *document_links)
342 {
343         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
344         miniexp_t outline;
345
346         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
347                 djvu_handle_events (djvu_document, TRUE, NULL);
348
349         if (outline) {
350                 ddjvu_miniexp_release (djvu_document->d_document, outline);
351                 return TRUE;
352         }
353         
354         return FALSE;
355 }
356
357 EvMappingList *
358 djvu_links_get_links (EvDocumentLinks *document_links,
359                       gint             page,
360                       double           scale_factor)
361 {
362         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
363         GList *retval = NULL;
364         miniexp_t page_annotations = miniexp_nil;
365         miniexp_t *hyperlinks = NULL, *iter = NULL;
366         EvMapping *ev_link_mapping;
367         ddjvu_pageinfo_t page_info;
368
369         while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
370                 djvu_handle_events (djvu_document, TRUE, NULL);
371
372         while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
373                 djvu_handle_events(djvu_document, TRUE, NULL);
374
375         if (page_annotations) {
376                 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
377                 if (hyperlinks) {
378                         for (iter = hyperlinks; *iter; ++iter) {
379                                 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
380                                 if (ev_link_mapping) {
381                                         ev_link_mapping->area.x1 *= scale_factor;
382                                         ev_link_mapping->area.x2 *= scale_factor;
383                                         ev_link_mapping->area.y1 *= scale_factor;
384                                         ev_link_mapping->area.y2 *= scale_factor;
385                                         retval = g_list_prepend (retval, ev_link_mapping);
386                                 }
387                         }
388                         free (hyperlinks);
389                 }
390                 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
391         }
392         
393         return ev_mapping_list_new (page, retval, (GDestroyNotify)g_object_unref);
394 }
395
396 EvLinkDest *
397 djvu_links_find_link_dest (EvDocumentLinks  *document_links,
398                            const gchar      *link_name)
399 {
400         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
401         EvLinkDest *ev_dest = NULL;
402         
403         ev_dest = get_djvu_link_dest (djvu_document, link_name, -1);
404
405         if (!ev_dest) {
406                 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
407         }
408         
409         return ev_dest;
410 }
411
412 GtkTreeModel *
413 djvu_links_get_links_model (EvDocumentLinks *document_links)
414 {
415         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
416         GtkTreeModel *model = NULL;
417         miniexp_t outline = miniexp_nil;
418
419         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
420                 djvu_handle_events (djvu_document, TRUE, NULL);
421
422         if (outline) {
423                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
424                                                              G_TYPE_STRING,
425                                                              G_TYPE_OBJECT,
426                                                              G_TYPE_BOOLEAN,
427                                                              G_TYPE_STRING);
428                 build_tree (djvu_document, model, NULL, outline);
429
430                 ddjvu_miniexp_release (djvu_document->d_document, outline);
431         }
432         
433         return model;
434 }