1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2009 Carlos Garcia Campos
4 * Copyright (C) 2004 Marco Pesenti Gritti
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)
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.
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.
27 #include "ev-document.h"
28 #include "ev-document-misc.h"
29 #include "synctex_parser.h"
31 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
33 typedef struct _EvPageSize
39 struct _EvDocumentPrivate
46 gdouble uniform_width;
47 gdouble uniform_height;
56 EvPageSize *page_sizes;
59 synctex_scanner_t synctex_scanner;
62 static gint _ev_document_get_n_pages (EvDocument *document);
63 static void _ev_document_get_page_size (EvDocument *document,
67 static gchar *_ev_document_get_page_label (EvDocument *document,
69 static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
70 static gboolean _ev_document_support_synctex (EvDocument *document);
72 GMutex *ev_doc_mutex = NULL;
73 GMutex *ev_fc_mutex = NULL;
75 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
78 ev_document_error_quark (void)
82 q = g_quark_from_static_string ("ev-document-error-quark");
88 ev_document_impl_get_page (EvDocument *document,
91 return ev_page_new (index);
94 static EvDocumentInfo *
95 ev_document_impl_get_info (EvDocument *document)
97 return g_new0 (EvDocumentInfo, 1);
101 ev_document_finalize (GObject *object)
103 EvDocument *document = EV_DOCUMENT (object);
105 if (document->priv->uri) {
106 g_free (document->priv->uri);
107 document->priv->uri = NULL;
110 if (document->priv->page_sizes) {
111 g_free (document->priv->page_sizes);
112 document->priv->page_sizes = NULL;
115 if (document->priv->page_labels) {
118 for (i = 0; i < document->priv->n_pages; i++) {
119 g_free (document->priv->page_labels[i]);
121 g_free (document->priv->page_labels);
122 document->priv->page_labels = NULL;
125 if (document->priv->info) {
126 ev_document_info_free (document->priv->info);
127 document->priv->info = NULL;
130 if (document->priv->synctex_scanner) {
131 synctex_scanner_free (document->priv->synctex_scanner);
132 document->priv->synctex_scanner = NULL;
135 G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
139 ev_document_init (EvDocument *document)
141 document->priv = EV_DOCUMENT_GET_PRIVATE (document);
143 /* Assume all pages are the same size until proven otherwise */
144 document->priv->uniform = TRUE;
148 ev_document_class_init (EvDocumentClass *klass)
150 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
152 g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
154 klass->get_page = ev_document_impl_get_page;
155 klass->get_info = ev_document_impl_get_info;
156 klass->get_backend_info = NULL;
158 g_object_class->finalize = ev_document_finalize;
162 ev_document_get_doc_mutex (void)
164 if (ev_doc_mutex == NULL) {
165 ev_doc_mutex = g_mutex_new ();
171 ev_document_doc_mutex_lock (void)
173 g_mutex_lock (ev_document_get_doc_mutex ());
177 ev_document_doc_mutex_unlock (void)
179 g_mutex_unlock (ev_document_get_doc_mutex ());
183 ev_document_doc_mutex_trylock (void)
185 return g_mutex_trylock (ev_document_get_doc_mutex ());
189 ev_document_get_fc_mutex (void)
191 if (ev_fc_mutex == NULL) {
192 ev_fc_mutex = g_mutex_new ();
198 ev_document_fc_mutex_lock (void)
200 g_mutex_lock (ev_document_get_fc_mutex ());
204 ev_document_fc_mutex_unlock (void)
206 g_mutex_unlock (ev_document_get_fc_mutex ());
210 ev_document_fc_mutex_trylock (void)
212 return g_mutex_trylock (ev_document_get_fc_mutex ());
217 * @document: a #EvDocument
218 * @uri: the document's URI
219 * @error: a #GError location to store an error, or %NULL
221 * Loads @document from @uri.
223 * On failure, %FALSE is returned and @error is filled in.
224 * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
225 * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
226 * is returned. Other errors are possible too, depending on the backend
227 * used to load the document and the URI, e.g. #GIOError, #GFileError, and
230 * Returns: %TRUE on success, or %FALSE on failure.
233 ev_document_load (EvDocument *document,
237 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
241 retval = klass->load (document, uri, &err);
244 g_propagate_error (error, err);
246 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
247 G_OBJECT_TYPE_NAME (document));
249 /* So upper layers don't crash */
250 g_set_error_literal (error,
252 EV_DOCUMENT_ERROR_INVALID,
253 "Internal error in backend");
257 EvDocumentPrivate *priv = document->priv;
259 /* Cache some info about the document to avoid
260 * going to the backends since it requires locks
262 priv->uri = g_strdup (uri);
263 priv->n_pages = _ev_document_get_n_pages (document);
265 for (i = 0; i < priv->n_pages; i++) {
266 EvPage *page = ev_document_get_page (document, i);
267 gdouble page_width = 0;
268 gdouble page_height = 0;
269 EvPageSize *page_size;
272 _ev_document_get_page_size (document, page, &page_width, &page_height);
275 priv->uniform_width = page_width;
276 priv->uniform_height = page_height;
277 priv->max_width = priv->uniform_width;
278 priv->max_height = priv->uniform_height;
279 priv->min_width = priv->uniform_width;
280 priv->min_height = priv->uniform_height;
281 } else if (priv->uniform &&
282 (priv->uniform_width != page_width ||
283 priv->uniform_height != page_height)) {
284 /* It's a different page size. Backfill the array. */
287 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
289 for (j = 0; j < i; j++) {
290 page_size = &(priv->page_sizes[j]);
291 page_size->width = priv->uniform_width;
292 page_size->height = priv->uniform_height;
294 priv->uniform = FALSE;
296 if (!priv->uniform) {
297 page_size = &(priv->page_sizes[i]);
299 page_size->width = page_width;
300 page_size->height = page_height;
302 if (page_width > priv->max_width)
303 priv->max_width = page_width;
304 if (page_width < priv->min_width)
305 priv->min_width = page_width;
307 if (page_height > priv->max_height)
308 priv->max_height = page_height;
309 if (page_height < priv->min_height)
310 priv->min_height = page_height;
313 page_label = _ev_document_get_page_label (document, page);
315 if (!priv->page_labels)
316 priv->page_labels = g_new0 (gchar *, priv->n_pages);
318 priv->page_labels[i] = page_label;
319 priv->max_label = MAX (priv->max_label,
320 g_utf8_strlen (page_label, 256));
323 g_object_unref (page);
326 priv->info = _ev_document_get_info (document);
327 if (_ev_document_support_synctex (document)) {
330 filename = g_filename_from_uri (uri, NULL, NULL);
331 if (filename != NULL) {
332 priv->synctex_scanner =
333 synctex_scanner_new_with_output_file (filename, NULL, 1);
345 * @uri: the target URI
346 * @error: a #GError location to store an error, or %NULL
348 * Saves @document to @uri.
350 * Returns: %TRUE on success, or %FALSE on error with @error filled in
353 ev_document_save (EvDocument *document,
357 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
359 return klass->save (document, uri, error);
363 ev_document_get_page (EvDocument *document,
366 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
368 return klass->get_page (document, index);
372 _ev_document_support_synctex (EvDocument *document)
374 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
376 return klass->support_synctex ? klass->support_synctex (document) : FALSE;
380 ev_document_has_synctex (EvDocument *document)
382 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
384 return document->priv->synctex_scanner != NULL;
388 * ev_document_synctex_backward_search:
390 * @page: the target page
394 * Peforms a Synctex backward search to obtain the TeX input file, line and
395 * (possibly) column corresponding to the position (@x,@y) (in 72dpi
396 * coordinates) in the @page of @document.
398 * Returns: A pointer to the EvSourceLink structure that holds the result. @NULL if synctex
399 * is not enabled for the document or no result is found.
400 * The EvSourceLink pointer should be freed with g_free after it is used.
403 ev_document_synctex_backward_search (EvDocument *document,
408 EvSourceLink *result = NULL;
409 synctex_scanner_t scanner;
411 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
413 scanner = document->priv->synctex_scanner;
417 if (synctex_edit_query (scanner, page_index + 1, x, y) > 0) {
420 /* We assume that a backward search returns either zero or one result_node */
421 node = synctex_next_result (scanner);
423 const gchar *filename;
425 filename = synctex_scanner_get_name (scanner, synctex_node_tag (node));
428 result = g_new (EvSourceLink, 1);
429 result->filename = filename;
430 result->line = synctex_node_line (node);
431 result->col = synctex_node_column (node);
440 * ev_document_synctex_forward_search:
444 * Peforms a Synctex forward search to obtain the area in the document
445 * corresponding to the position @line and @column number in the source Tex file
447 * Returns: An EvMapping with the page number and area corresponfing to
448 * the given line in the source file. It must be free with g_free when done
451 ev_document_synctex_forward_search (EvDocument *document,
454 EvMapping *result = NULL;
455 synctex_scanner_t scanner;
457 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
459 scanner = document->priv->synctex_scanner;
463 if (synctex_display_query (scanner, link->filename, link->line, link->col) > 0) {
467 if ((node = synctex_next_result (scanner))) {
468 result = g_new (EvMapping, 1);
470 page = synctex_node_page (node) - 1;
471 result->data = GINT_TO_POINTER (page);
473 result->area.x1 = synctex_node_box_visible_h (node);
474 result->area.y1 = synctex_node_box_visible_v (node) -
475 synctex_node_box_visible_height (node);
476 result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
477 result->area.y2 = synctex_node_box_visible_depth (node) +
478 synctex_node_box_visible_height (node) + result->area.y1;
486 _ev_document_get_n_pages (EvDocument *document)
488 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
490 return klass->get_n_pages (document);
494 ev_document_get_n_pages (EvDocument *document)
496 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
498 return document->priv->n_pages;
502 _ev_document_get_page_size (EvDocument *document,
507 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
509 klass->get_page_size (document, page, width, height);
513 ev_document_get_page_size (EvDocument *document,
518 g_return_if_fail (EV_IS_DOCUMENT (document));
519 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
522 *width = document->priv->uniform ?
523 document->priv->uniform_width :
524 document->priv->page_sizes[page_index].width;
526 *height = document->priv->uniform ?
527 document->priv->uniform_height :
528 document->priv->page_sizes[page_index].height;
532 _ev_document_get_page_label (EvDocument *document,
535 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
537 return klass->get_page_label ?
538 klass->get_page_label (document, page) : NULL;
542 ev_document_get_page_label (EvDocument *document,
545 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
546 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
548 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
549 g_strdup (document->priv->page_labels[page_index]) :
550 g_strdup_printf ("%d", page_index + 1);
553 static EvDocumentInfo *
554 _ev_document_get_info (EvDocument *document)
556 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
558 return klass->get_info (document);
562 ev_document_get_info (EvDocument *document)
564 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
566 return document->priv->info;
570 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
572 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
574 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
575 if (klass->get_backend_info == NULL)
578 return klass->get_backend_info (document, info);
582 ev_document_render (EvDocument *document,
585 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
587 return klass->render (document, rc);
591 _ev_document_get_thumbnail (EvDocument *document,
594 cairo_surface_t *surface;
597 surface = ev_document_render (document, rc);
598 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
599 cairo_surface_destroy (surface);
605 ev_document_get_thumbnail (EvDocument *document,
608 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
610 if (klass->get_thumbnail)
611 return klass->get_thumbnail (document, rc);
613 return _ev_document_get_thumbnail (document, rc);
617 ev_document_get_uri (EvDocument *document)
619 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
621 return document->priv->uri;
625 ev_document_get_title (EvDocument *document)
627 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
629 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
630 document->priv->info->title : NULL;
634 ev_document_is_page_size_uniform (EvDocument *document)
636 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
638 return document->priv->uniform;
642 ev_document_get_max_page_size (EvDocument *document,
646 g_return_if_fail (EV_IS_DOCUMENT (document));
649 *width = document->priv->max_width;
651 *height = document->priv->max_height;
655 ev_document_get_min_page_size (EvDocument *document,
659 g_return_if_fail (EV_IS_DOCUMENT (document));
662 *width = document->priv->min_width;
664 *height = document->priv->min_height;
668 ev_document_check_dimensions (EvDocument *document)
670 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
672 return (document->priv->max_width > 0 && document->priv->max_height > 0);
676 ev_document_get_max_label_len (EvDocument *document)
678 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
680 return document->priv->max_label;
684 ev_document_has_text_page_labels (EvDocument *document)
686 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
688 return document->priv->page_labels != NULL;
692 ev_document_find_page_by_label (EvDocument *document,
693 const gchar *page_label,
698 gchar *endptr = NULL;
699 EvDocumentPrivate *priv = document->priv;
701 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
702 g_return_val_if_fail (page_label != NULL, FALSE);
703 g_return_val_if_fail (page_index != NULL, FALSE);
705 /* First, look for a literal label match */
706 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
707 if (priv->page_labels[i] != NULL &&
708 ! strcmp (page_label, priv->page_labels[i])) {
714 /* Second, look for a match with case insensitively */
715 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
716 if (priv->page_labels[i] != NULL &&
717 ! strcasecmp (page_label, priv->page_labels[i])) {
723 /* Next, parse the label, and see if the number fits */
724 value = strtol (page_label, &endptr, 10);
725 if (endptr[0] == '\0') {
726 /* Page number is an integer */
727 page = MIN (G_MAXINT, value);
729 /* convert from a page label to a page offset */
731 if (page >= 0 && page < priv->n_pages) {
741 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
744 ev_document_info_copy (EvDocumentInfo *info)
746 EvDocumentInfo *copy;
748 g_return_val_if_fail (info != NULL, NULL);
750 copy = g_new0 (EvDocumentInfo, 1);
751 copy->title = g_strdup (info->title);
752 copy->format = g_strdup (info->format);
753 copy->author = g_strdup (info->author);
754 copy->subject = g_strdup (info->subject);
755 copy->keywords = g_strdup (info->keywords);
756 copy->security = g_strdup (info->security);
757 copy->creator = g_strdup (info->creator);
758 copy->producer = g_strdup (info->producer);
759 copy->linearized = g_strdup (info->linearized);
761 copy->creation_date = info->creation_date;
762 copy->modified_date = info->modified_date;
763 copy->layout = info->layout;
764 copy->mode = info->mode;
765 copy->ui_hints = info->ui_hints;
766 copy->permissions = info->permissions;
767 copy->n_pages = info->n_pages;
768 copy->license = ev_document_license_copy (info->license);
770 copy->fields_mask = info->fields_mask;
776 ev_document_info_free (EvDocumentInfo *info)
781 g_free (info->title);
782 g_free (info->format);
783 g_free (info->author);
784 g_free (info->subject);
785 g_free (info->keywords);
786 g_free (info->creator);
787 g_free (info->producer);
788 g_free (info->linearized);
789 g_free (info->security);
790 ev_document_license_free (info->license);
795 /* EvDocumentLicense */
796 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
799 ev_document_license_new (void)
801 return g_new0 (EvDocumentLicense, 1);
805 ev_document_license_copy (EvDocumentLicense *license)
807 EvDocumentLicense *new_license;
812 new_license = ev_document_license_new ();
815 new_license->text = g_strdup (license->text);
817 new_license->uri = g_strdup (license->uri);
818 if (license->web_statement)
819 new_license->web_statement = g_strdup (license->web_statement);
825 ev_document_license_free (EvDocumentLicense *license)
830 g_free (license->text);
831 g_free (license->uri);
832 g_free (license->web_statement);
838 ev_document_license_get_text (EvDocumentLicense *license)
840 return license->text;
844 ev_document_license_get_uri (EvDocumentLicense *license)
850 ev_document_license_get_web_statement (EvDocumentLicense *license)
852 return license->web_statement;
856 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
859 ev_rectangle_new (void)
861 return g_new0 (EvRectangle, 1);
865 ev_rectangle_copy (EvRectangle *rectangle)
867 EvRectangle *new_rectangle;
869 g_return_val_if_fail (rectangle != NULL, NULL);
871 new_rectangle = g_new (EvRectangle, 1);
872 *new_rectangle = *rectangle;
874 return new_rectangle;
878 ev_rectangle_free (EvRectangle *rectangle)
883 /* Compares two rects. returns 0 if they're equal */
884 #define EPSILON 0.0000001
887 ev_rect_cmp (EvRectangle *a,
892 if (a == NULL || b == NULL)
895 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
896 (ABS (a->y1 - b->y1) < EPSILON) &&
897 (ABS (a->x2 - b->x2) < EPSILON) &&
898 (ABS (a->y2 - b->y2) < EPSILON));