Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
djvu-links.c
Go to the documentation of this file.
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 guint64
64 get_djvu_link_page (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 (g_str_has_suffix (link_name,".djvu")) {
71  /* File identifiers */
72  gpointer page = NULL;
73 
74  if (g_hash_table_lookup_extended (djvu_document->file_ids, link_name + 1, NULL, &page)) {
75  return GPOINTER_TO_INT (page);
76  }
77  } else if (base_page > 0 && g_str_has_prefix (link_name + 1, "+")) {
78  if (number_from_string_10 (link_name + 2, &page_num)) {
79  return base_page + page_num;
80  }
81  } else if (base_page > 0 && g_str_has_prefix (link_name + 1, "-")) {
82  if (number_from_string_10 (link_name + 2, &page_num)) {
83  return base_page - page_num;
84  }
85  } else {
86  if (number_from_string_10 (link_name + 1, &page_num)) {
87  return page_num - 1;
88  }
89  }
90  } else {
91  /* FIXME: should we handle this case */
92  }
93 
94  return page_num;
95 }
96 
97 static EvLinkDest *
98 get_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
99 {
100  /* #+pagenum #-pagenum #file_id.djvu */
101  if (g_str_has_prefix (link_name, "#")) {
102  if (g_str_has_suffix (link_name, ".djvu") ||
103  (base_page > 0 && g_str_has_prefix (link_name + 1, "+")) ||
104  (base_page > 0 && g_str_has_prefix (link_name + 1, "-"))) {
105  return ev_link_dest_new_page (get_djvu_link_page (djvu_document, link_name, base_page));
106  } else {
107  /* #pagenum #page_label: the djvu spec is not clear on whether #pagenum represents
108  * a link to a page number or to a page label. Here we mimick djview,
109  * and always treat #pagenum as a link to a page label */
110  return ev_link_dest_new_page_label (link_name + 1);
111  }
112  }
113 
114  return NULL;
115 }
116 
117 static EvLinkAction *
118 get_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
119 {
120  EvLinkDest *ev_dest = NULL;
121  EvLinkAction *ev_action = NULL;
122 
123  /* File component identifiers are handled by get_djvu_link_dest */
124 
125  ev_dest = get_djvu_link_dest (djvu_document, link_name, base_page);
126  if (ev_dest) {
127  ev_action = ev_link_action_new_dest (ev_dest);
128  g_object_unref (ev_dest);
129  } else if (strstr(link_name, "://") != NULL) {
130  /* It's probably an URI */
131  ev_action = ev_link_action_new_external_uri (link_name);
132  }
133 
134  return ev_action;
135 }
136 
137 static gchar *
138 str_to_utf8 (const gchar *text)
139 {
140  static const gchar *encodings_to_try[2];
141  static gint n_encodings_to_try = 0;
142  gchar *utf8_text = NULL;
143  gint i;
144 
145  if (n_encodings_to_try == 0) {
146  const gchar *charset;
147  gboolean charset_is_utf8;
148 
149  charset_is_utf8 = g_get_charset (&charset);
150  if (!charset_is_utf8) {
151  encodings_to_try[n_encodings_to_try++] = charset;
152  }
153 
154  if (g_ascii_strcasecmp (charset, "ISO-8859-1") != 0) {
155  encodings_to_try[n_encodings_to_try++] = "ISO-8859-1";
156  }
157  }
158 
159  for (i = 0; i < n_encodings_to_try; i++) {
160  utf8_text = g_convert (text, -1, "UTF-8",
161  encodings_to_try[i],
162  NULL, NULL, NULL);
163  if (utf8_text)
164  break;
165  }
166 
167  return utf8_text;
168 }
169 
182 static void
183 build_tree (const DjvuDocument *djvu_document,
184  GtkTreeModel *model,
185  GtkTreeIter *parent,
186  miniexp_t iter)
187 {
188  const char *title, *link_dest;
189  char *title_markup;
190 
191  EvLinkAction *ev_action = NULL;
192  EvLink *ev_link = NULL;
193  GtkTreeIter tree_iter;
194 
195  if (miniexp_car (iter) == miniexp_symbol ("bookmarks")) {
196  /* The (bookmarks) cons */
197  iter = miniexp_cdr (iter);
198  } else if ( miniexp_length (iter) >= 2 ) {
199  gchar *utf8_title = NULL;
200 
201  /* An entry */
202  if (!string_from_miniexp (miniexp_car (iter), &title)) goto unknown_entry;
203  if (!string_from_miniexp (miniexp_cadr (iter), &link_dest)) goto unknown_entry;
204 
205 
206  if (!g_utf8_validate (title, -1, NULL)) {
207  utf8_title = str_to_utf8 (title);
208  title_markup = g_markup_escape_text (utf8_title, -1);
209  } else {
210  title_markup = g_markup_escape_text (title, -1);
211  }
212 
213  ev_action = get_djvu_link_action (djvu_document, link_dest, -1);
214 
215  if (ev_action) {
216  ev_link = ev_link_new (utf8_title ? utf8_title : title, ev_action);
217  gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
218  gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
219  EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
222  -1);
223  g_object_unref (ev_action);
224  g_object_unref (ev_link);
225  } else {
226  gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
227  gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
228  EV_DOCUMENT_LINKS_COLUMN_MARKUP, title_markup,
230  -1);
231  }
232 
233  g_free (title_markup);
234  g_free (utf8_title);
235  iter = miniexp_cddr (iter);
236  parent = &tree_iter;
237  } else {
238  goto unknown_entry;
239  }
240 
241  for (; iter != miniexp_nil; iter = miniexp_cdr (iter)) {
242  build_tree (djvu_document, model, parent, miniexp_car (iter));
243  }
244  return;
245 
246  unknown_entry:
247  g_warning ("DjvuLibre error: Unknown entry in bookmarks");
248  return;
249 }
250 
251 static gboolean
252 get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info,
253  miniexp_t sexp,
254  EvMapping *ev_link_mapping)
255 {
256  miniexp_t iter;
257 
258  iter = sexp;
259 
260  if ((miniexp_car (iter) == miniexp_symbol ("rect") || miniexp_car (iter) == miniexp_symbol ("oval"))
261  && miniexp_length (iter) == 5) {
262  /* FIXME: get bounding box for (oval) since Evince doesn't support shaped links */
263  int minx, miny, width, height;
264 
265  iter = miniexp_cdr (iter);
266  if (!number_from_miniexp (miniexp_car (iter), &minx)) goto unknown_link;
267  iter = miniexp_cdr (iter);
268  if (!number_from_miniexp (miniexp_car (iter), &miny)) goto unknown_link;
269  iter = miniexp_cdr (iter);
270  if (!number_from_miniexp (miniexp_car (iter), &width)) goto unknown_link;
271  iter = miniexp_cdr (iter);
272  if (!number_from_miniexp (miniexp_car (iter), &height)) goto unknown_link;
273 
274  ev_link_mapping->area.x1 = minx;
275  ev_link_mapping->area.x2 = (minx + width);
276  ev_link_mapping->area.y1 = (page_info->height - (miny + height));
277  ev_link_mapping->area.y2 = (page_info->height - miny);
278  } else if (miniexp_car (iter) == miniexp_symbol ("poly")
279  && miniexp_length (iter) >= 5 && miniexp_length (iter) % 2 == 1) {
280 
281  /* FIXME: get bounding box since Evince doesn't support shaped links */
282  int minx = G_MAXINT, miny = G_MAXINT;
283  int maxx = G_MININT, maxy = G_MININT;
284 
285  iter = miniexp_cdr(iter);
286  while (iter != miniexp_nil) {
287  int x, y;
288 
289  if (!number_from_miniexp (miniexp_car(iter), &x)) goto unknown_link;
290  iter = miniexp_cdr (iter);
291  if (!number_from_miniexp (miniexp_car(iter), &y)) goto unknown_link;
292  iter = miniexp_cdr (iter);
293 
294  minx = MIN (minx, x);
295  miny = MIN (miny, y);
296  maxx = MAX (maxx, x);
297  maxy = MAX (maxy, y);
298  }
299 
300  ev_link_mapping->area.x1 = minx;
301  ev_link_mapping->area.x2 = maxx;
302  ev_link_mapping->area.y1 = (page_info->height - maxy);
303  ev_link_mapping->area.y2 = (page_info->height - miny);
304  } else {
305  /* unknown */
306  goto unknown_link;
307  }
308 
309  return TRUE;
310 
311  unknown_link:
312  g_warning("DjvuLibre error: Unknown hyperlink area %s", miniexp_to_name(miniexp_car(sexp)));
313  return FALSE;
314 }
315 
316 static EvMapping *
318  int page,
319  ddjvu_pageinfo_t *page_info,
320  miniexp_t sexp)
321 {
322  EvMapping *ev_link_mapping = NULL;
323  EvLinkAction *ev_action = NULL;
324  miniexp_t iter;
325  const char *url, *url_target, *comment;
326 
327  ev_link_mapping = g_new (EvMapping, 1);
328 
329  iter = sexp;
330 
331  if (miniexp_car (iter) != miniexp_symbol ("maparea")) goto unknown_mapping;
332 
333  iter = miniexp_cdr(iter);
334 
335  if (miniexp_caar(iter) == miniexp_symbol("url")) {
336  if (!string_from_miniexp (miniexp_cadr (miniexp_car (iter)), &url)) goto unknown_mapping;
337  if (!string_from_miniexp (miniexp_caddr (miniexp_car (iter)), &url_target)) goto unknown_mapping;
338  } else {
339  if (!string_from_miniexp (miniexp_car(iter), &url)) goto unknown_mapping;
340  url_target = NULL;
341  }
342 
343  iter = miniexp_cdr (iter);
344  if (!string_from_miniexp (miniexp_car(iter), &comment)) goto unknown_mapping;
345 
346  iter = miniexp_cdr (iter);
347  if (!get_djvu_hyperlink_area (page_info, miniexp_car(iter), ev_link_mapping)) goto unknown_mapping;
348 
349  iter = miniexp_cdr (iter);
350  /* FIXME: DjVu hyperlink attributes are ignored */
351 
352  ev_action = get_djvu_link_action (djvu_document, url, page);
353  if (!ev_action) goto unknown_mapping;
354 
355  ev_link_mapping->data = ev_link_new (comment, ev_action);
356  g_object_unref (ev_action);
357 
358  return ev_link_mapping;
359 
360  unknown_mapping:
361  if (ev_link_mapping) g_free(ev_link_mapping);
362  g_warning("DjvuLibre error: Unknown hyperlink %s", miniexp_to_name(miniexp_car(sexp)));
363  return NULL;
364 }
365 
366 
367 gboolean
369 {
370  DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
371  miniexp_t outline;
372 
373  while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
374  djvu_handle_events (djvu_document, TRUE, NULL);
375 
376  if (outline) {
377  ddjvu_miniexp_release (djvu_document->d_document, outline);
378  return TRUE;
379  }
380 
381  return FALSE;
382 }
383 
386  gint page,
387  double scale_factor)
388 {
389  DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
390  GList *retval = NULL;
391  miniexp_t page_annotations = miniexp_nil;
392  miniexp_t *hyperlinks = NULL, *iter = NULL;
393  EvMapping *ev_link_mapping;
394  ddjvu_pageinfo_t page_info;
395 
396  while ((page_annotations = ddjvu_document_get_pageanno (djvu_document->d_document, page)) == miniexp_dummy)
397  djvu_handle_events (djvu_document, TRUE, NULL);
398 
399  while (ddjvu_document_get_pageinfo (djvu_document->d_document, page, &page_info) < DDJVU_JOB_OK)
400  djvu_handle_events(djvu_document, TRUE, NULL);
401 
402  if (page_annotations) {
403  hyperlinks = ddjvu_anno_get_hyperlinks (page_annotations);
404  if (hyperlinks) {
405  for (iter = hyperlinks; *iter; ++iter) {
406  ev_link_mapping = get_djvu_hyperlink_mapping (djvu_document, page, &page_info, *iter);
407  if (ev_link_mapping) {
408  ev_link_mapping->area.x1 *= scale_factor;
409  ev_link_mapping->area.x2 *= scale_factor;
410  ev_link_mapping->area.y1 *= scale_factor;
411  ev_link_mapping->area.y2 *= scale_factor;
412  retval = g_list_prepend (retval, ev_link_mapping);
413  }
414  }
415  free (hyperlinks);
416  }
417  ddjvu_miniexp_release (djvu_document->d_document, page_annotations);
418  }
419 
420  return ev_mapping_list_new (page, retval, (GDestroyNotify)g_object_unref);
421 }
422 
423 EvLinkDest *
425  const gchar *link_name)
426 {
427  DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
428  EvLinkDest *ev_dest = NULL;
429 
430  ev_dest = get_djvu_link_dest (djvu_document, link_name, -1);
431 
432  if (!ev_dest) {
433  g_warning ("DjvuLibre error: unknown link destination %s", link_name);
434  }
435 
436  return ev_dest;
437 }
438 
439 gint
441  const gchar *link_name)
442 {
443  DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
444  gint page;
445 
446  page = get_djvu_link_page (djvu_document, link_name, -1);
447 
448  if (page == -1) {
449  g_warning ("DjvuLibre error: unknown link destination %s", link_name);
450  }
451 
452  return page;
453 }
454 
455 GtkTreeModel *
457 {
458  DjvuDocument *djvu_document = DJVU_DOCUMENT (document_links);
459  GtkTreeModel *model = NULL;
460  miniexp_t outline = miniexp_nil;
461 
462  while ((outline = ddjvu_document_get_outline (djvu_document->d_document)) == miniexp_dummy)
463  djvu_handle_events (djvu_document, TRUE, NULL);
464 
465  if (outline) {
466  model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
467  G_TYPE_STRING,
468  G_TYPE_OBJECT,
469  G_TYPE_BOOLEAN,
470  G_TYPE_STRING);
471  build_tree (djvu_document, model, NULL, outline);
472 
473  ddjvu_miniexp_release (djvu_document->d_document, outline);
474  }
475 
476  return model;
477 }