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 File Reference
#include <config.h>
#include <string.h>
#include <glib.h>
#include <libdjvu/miniexp.h>
#include "djvu-document.h"
#include "djvu-links.h"
#include "djvu-document-private.h"
#include "ev-document-links.h"
#include "ev-mapping-list.h"
+ Include dependency graph for djvu-links.c:

Go to the source code of this file.

Functions

static gboolean number_from_miniexp (miniexp_t sexp, int *number)
 
static gboolean string_from_miniexp (miniexp_t sexp, const char **str)
 
static gboolean number_from_string_10 (const gchar *str, guint64 *number)
 
static guint64 get_djvu_link_page (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
 
static EvLinkDestget_djvu_link_dest (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
 
static EvLinkActionget_djvu_link_action (const DjvuDocument *djvu_document, const gchar *link_name, int base_page)
 
static gchar * str_to_utf8 (const gchar *text)
 
static void build_tree (const DjvuDocument *djvu_document, GtkTreeModel *model, GtkTreeIter *parent, miniexp_t iter)
 
static gboolean get_djvu_hyperlink_area (ddjvu_pageinfo_t *page_info, miniexp_t sexp, EvMapping *ev_link_mapping)
 
static EvMappingget_djvu_hyperlink_mapping (DjvuDocument *djvu_document, int page, ddjvu_pageinfo_t *page_info, miniexp_t sexp)
 
gboolean djvu_links_has_document_links (EvDocumentLinks *document_links)
 
EvMappingListdjvu_links_get_links (EvDocumentLinks *document_links, gint page, double scale_factor)
 
EvLinkDestdjvu_links_find_link_dest (EvDocumentLinks *document_links, const gchar *link_name)
 
gint djvu_links_find_link_page (EvDocumentLinks *document_links, const gchar *link_name)
 
GtkTreeModel * djvu_links_get_links_model (EvDocumentLinks *document_links)
 

Function Documentation

static void build_tree ( const DjvuDocument djvu_document,
GtkTreeModel *  model,
GtkTreeIter *  parent,
miniexp_t  iter 
)
static

Builds the index GtkTreeModel from DjVu s-expr

(bookmarks ("title1" "dest1" ("title12" "dest12" ... ) ... ) ("title2" "dest2" ... ) ... )

Definition at line 183 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

EvLinkDest* djvu_links_find_link_dest ( EvDocumentLinks document_links,
const gchar *  link_name 
)

Definition at line 424 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

gint djvu_links_find_link_page ( EvDocumentLinks document_links,
const gchar *  link_name 
)

Definition at line 440 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

EvMappingList* djvu_links_get_links ( EvDocumentLinks document_links,
gint  page,
double  scale_factor 
)

Definition at line 385 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

GtkTreeModel* djvu_links_get_links_model ( EvDocumentLinks document_links)

Definition at line 456 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

gboolean djvu_links_has_document_links ( EvDocumentLinks document_links)

Definition at line 368 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static gboolean get_djvu_hyperlink_area ( ddjvu_pageinfo_t *  page_info,
miniexp_t  sexp,
EvMapping ev_link_mapping 
)
static

Definition at line 252 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static EvMapping* get_djvu_hyperlink_mapping ( DjvuDocument djvu_document,
int  page,
ddjvu_pageinfo_t *  page_info,
miniexp_t  sexp 
)
static

Definition at line 317 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static EvLinkAction* get_djvu_link_action ( const DjvuDocument djvu_document,
const gchar *  link_name,
int  base_page 
)
static

Definition at line 118 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static EvLinkDest* get_djvu_link_dest ( const DjvuDocument djvu_document,
const gchar *  link_name,
int  base_page 
)
static

Definition at line 98 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static guint64 get_djvu_link_page ( const DjvuDocument djvu_document,
const gchar *  link_name,
int  base_page 
)
static

Definition at line 64 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static gboolean number_from_miniexp ( miniexp_t  sexp,
int *  number 
)
static

Definition at line 31 of file djvu-links.c.

32 {
33  if (miniexp_numberp (sexp)) {
34  *number = miniexp_to_int (sexp);
35  return TRUE;
36  } else {
37  return FALSE;
38  }
39 }

+ Here is the caller graph for this function:

static gboolean number_from_string_10 ( const gchar *  str,
guint64 *  number 
)
static

Definition at line 51 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static gchar* str_to_utf8 ( const gchar *  text)
static

Definition at line 138 of file djvu-links.c.

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 }

+ Here is the caller graph for this function:

static gboolean string_from_miniexp ( miniexp_t  sexp,
const char **  str 
)
static

Definition at line 41 of file djvu-links.c.

42 {
43  if (miniexp_stringp (sexp)) {
44  *str = miniexp_to_str (sexp);
45  return TRUE;
46  } else {
47  return FALSE;
48  }
49 }

+ Here is the caller graph for this function: