]> www.fi.muni.cz Git - evince.git/blob - backend/djvu/djvu-links.c
Implements another history variant
[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 "djvu-document.h"
22 #include "djvu-links.h"
23 #include "djvu-document-private.h"
24 #include "ev-document-links.h"
25
26 #include <libdjvu/ddjvuapi.h>
27 #include <libdjvu/miniexp.h>
28 #include <glib.h>
29 #include <string.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         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 /**
112  * Builds the index GtkTreeModel from DjVu s-expr
113  *
114  * (bookmarks
115  *   ("title1" "dest1"
116  *     ("title12" "dest12"
117  *       ... )
118  *     ... )
119  *   ("title2" "dest2"
120  *     ... )
121  *   ... )
122  */
123 static void
124 build_tree (const DjvuDocument *djvu_document,
125             GtkTreeModel       *model,
126             GtkTreeIter        *parent,
127             miniexp_t           iter)
128 {
129         const char *title, *link_dest;
130         char *title_markup;
131
132         EvLinkAction *ev_action = NULL;
133         EvLink *ev_link = NULL;
134         GtkTreeIter tree_iter;
135
136         if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
137                 /* The (bookmarks) cons */
138                 iter = miniexp_cdr (iter);
139         } else if ( miniexp_length (iter) >= 2 ) {
140                 /* An entry */
141                 if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
142                 if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
143
144                 title_markup = g_markup_escape_text (title, -1);
145                 ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
146                 
147                 if (g_str_has_suffix (link_dest, ".djvu")) {
148                         /* FIXME: component file identifiers */
149                 } else if (ev_action) {
150                         ev_link = ev_link_new (title, ev_action);
151                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
152                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
153                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
154                                             EV_DOCUMENT_LINKS_COLUMN_LINK, ev_link,
155                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
156                                             -1);
157                         g_object_unref (ev_link);
158                 } else {
159                         gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
160                         gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
161                                             EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
162                                             EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
163                                             -1);
164                 }
165
166                 g_free (title_markup);
167                 
168                 iter = miniexp_cddr (iter);
169                 parent = &tree_iter;
170         } else {
171                 goto unknown_entry;
172         }
173
174         for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
175                 build_tree (djvu_document, model, parent, miniexp_car (iter));
176         }
177         return;
178
179  unknown_entry:
180         g_warning ("DjvuLibre error: Unknown entry in bookmarks");
181         return;
182 }
183
184 static gboolean
185 get_djvu_hyperlink_area (ddjvu_pageinfo_t   *page_info,
186                          miniexp_t           sexp,
187                          EvLinkMapping      *ev_link_mapping)
188 {
189         miniexp_t iter;
190         ddjvu_pageinfo_t info;
191
192         iter = sexp;
193         
194         if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
195             && miniexp_length (iter) == 5) {
196                 /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
197                 int minx, miny, width, height;
198
199                 iter = miniexp_cdr (iter);
200                 if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
201                 iter = miniexp_cdr (iter);
202                 if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
203                 iter = miniexp_cdr (iter);
204                 if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
205                 iter = miniexp_cdr (iter);
206                 if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
207
208                 ev_link_mapping->x1 = minx;
209                 ev_link_mapping->x2 = (minx + width);
210                 ev_link_mapping->y1 = (page_info->height - (miny + height));
211                 ev_link_mapping->y2 = (page_info->height - miny);
212         } else if (miniexp_car (iter) == miniexp_symbol ("poly")
213                    && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
214                 
215                 /* FIXME: get bounding box since Evince doesn't support shaped links */
216                 int minx = G_MAXINT, miny = G_MAXINT;
217                 int maxx = G_MININT, maxy = G_MININT;
218
219                 iter = miniexp_cdr(iter);
220                 while (iter != miniexp_nil) {
221                         int x, y;
222
223                         if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
224                         iter = miniexp_cdr (iter);
225                         if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
226                         iter = miniexp_cdr (iter);
227
228                         minx = MIN (minx, x);
229                         miny = MIN (miny, y);
230                         maxx = MAX (maxx, x);
231                         maxy = MAX (maxy, y);
232                 }
233
234                 ev_link_mapping->x1 = minx;
235                 ev_link_mapping->x2 = maxx;
236                 ev_link_mapping->y1 = (page_info->height - maxy);
237                 ev_link_mapping->y2 = (page_info->height - miny);
238         } else {
239                 /* unknown */
240                 goto unknown_link;
241         }
242
243         return TRUE;
244         
245  unknown_link:
246         g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
247         return FALSE;
248 }
249
250 static EvLinkMapping *
251 get_djvu_hyperlink_mapping (DjvuDocument     *djvu_document,
252                             int               page,
253                             ddjvu_pageinfo_t *page_info,
254                             miniexp_t         sexp)
255 {
256         EvLinkMapping *ev_link_mapping = NULL;
257         EvLinkAction *ev_action = NULL;
258         miniexp_t iter;
259         const char *url, *url_target, *comment;
260
261         ev_link_mapping = g_new (EvLinkMapping, 1);
262
263         iter = sexp;
264
265         if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
266
267         iter = miniexp_cdr(iter);
268
269         if (miniexp_caar(iter) == miniexp_symbol("url")) {
270                 if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
271                 if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
272         } else {
273                 if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
274                 url_target = NULL;
275         }
276
277         iter = miniexp_cdr (iter);
278         if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
279
280         iter = miniexp_cdr (iter);
281         if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
282
283         iter = miniexp_cdr (iter);
284         /* FIXME: DjVu hyperlink attributes are ignored */
285
286         ev_action = get_djvu_link_action (djvu_document, url, page);
287         if (!ev_action) goto unknown_mapping;
288
289         ev_link_mapping->link = ev_link_new (comment, ev_action);
290                         
291         return ev_link_mapping;
292
293  unknown_mapping:
294         if (ev_link_mapping) g_free(ev_link_mapping);
295         g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
296         return NULL;
297 }
298
299
300 gboolean
301 djvu_links_has_document_links (EvDocumentLinks *document_links)
302 {
303         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
304         miniexp_t outline;
305
306         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
307                 djvu_handle_events (djvu_document, TRUE);
308
309         if (outline) {
310                 ddjvu_miniexp_release (djvu_document->d_document, outline);
311                 return TRUE;
312         }
313         
314         return FALSE;
315 }
316
317 GList *
318 djvu_links_get_links (EvDocumentLinks *document_links,
319                       gint             page,
320                       double           scale_factor)
321 {
322         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
323         GList *retval = NULL;
324         miniexp_t page_annotations = miniexp_nil;
325         miniexp_t *hyperlinks = NULL, *iter = NULL;
326         EvLinkMapping *ev_link_mapping;
327         ddjvu_pageinfo_t page_info;
328
329         while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
330                 djvu_handle_events (djvu_document, TRUE);
331
332         while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
333                 djvu_handle_events(djvu_document, TRUE);
334
335         if (page_annotations) {
336                 hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
337                 if (hyperlinks) {
338                         for (iter = hyperlinks; *iter; ++iter) {
339                                 ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
340                                 if (ev_link_mapping) {
341                                         ev_link_mapping->x1 *= scale_factor;
342                                         ev_link_mapping->x2 *= scale_factor;
343                                         ev_link_mapping->y1 *= scale_factor;
344                                         ev_link_mapping->y2 *= scale_factor;
345                                         retval = g_list_prepend (retval, ev_link_mapping);
346                                 }
347                         }
348                         free (hyperlinks);
349                 }
350                 ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
351         }
352         
353         return retval;
354 }
355
356 EvLinkDest *
357 djvu_links_find_link_dest (EvDocumentLinks  *document_links,
358                            const gchar      *link_name)
359 {
360         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
361         EvLinkDest *ev_dest = NULL;
362         
363         ev_dest = get_djvu_link_dest (DJVU_DOCUMENT (document_links), link_name, -1);
364
365         if (!ev_dest) {
366                 g_warning ("DjvuLibre error: unknown link destination %s", link_name);
367         }
368         
369         return ev_dest;
370 }
371
372 GtkTreeModel *
373 djvu_links_get_links_model (EvDocumentLinks *document_links)
374 {
375         DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
376         GtkTreeModel *model = NULL;
377         miniexp_t outline = miniexp_nil;
378
379         while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
380                 djvu_handle_events (djvu_document, TRUE);
381
382         if (outline) {
383                 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
384                                                              G_TYPE_STRING,
385                                                              G_TYPE_OBJECT,
386                                                              G_TYPE_BOOLEAN,
387                                                              G_TYPE_STRING);
388                 build_tree (djvu_document, model, NULL, outline);
389
390                 ddjvu_miniexp_release (djvu_document->d_document, outline);
391         }
392         
393         return model;
394 }