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 result = g_new (EvSourceLink, 1);
424 result->filename = synctex_scanner_get_name (scanner,
425 synctex_node_tag (node));
426 result->line = synctex_node_line (node);
427 result->col = synctex_node_column (node);
435 * ev_document_synctex_forward_search:
439 * Peforms a Synctex forward search to obtain the area in the document
440 * corresponding to the position @line and @column number in the source Tex file
442 * Returns: An EvMapping with the page number and area corresponfing to
443 * the given line in the source file. It must be free with g_free when done
446 ev_document_synctex_forward_search (EvDocument *document,
449 EvMapping *result = NULL;
450 synctex_scanner_t scanner;
452 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
454 scanner = document->priv->synctex_scanner;
458 if (synctex_display_query (scanner, link->filename, link->line, link->col) > 0) {
462 if ((node = synctex_next_result (scanner))) {
463 result = g_new (EvMapping, 1);
465 page = synctex_node_page (node) - 1;
466 result->data = GINT_TO_POINTER (page);
468 result->area.x1 = synctex_node_box_visible_h (node);
469 result->area.y1 = synctex_node_box_visible_v (node) -
470 synctex_node_box_visible_height (node);
471 result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
472 result->area.y2 = synctex_node_box_visible_depth (node) +
473 synctex_node_box_visible_height (node) + result->area.y1;
481 _ev_document_get_n_pages (EvDocument *document)
483 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
485 return klass->get_n_pages (document);
489 ev_document_get_n_pages (EvDocument *document)
491 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
493 return document->priv->n_pages;
497 _ev_document_get_page_size (EvDocument *document,
502 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
504 klass->get_page_size (document, page, width, height);
508 ev_document_get_page_size (EvDocument *document,
513 g_return_if_fail (EV_IS_DOCUMENT (document));
514 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
517 *width = document->priv->uniform ?
518 document->priv->uniform_width :
519 document->priv->page_sizes[page_index].width;
521 *height = document->priv->uniform ?
522 document->priv->uniform_height :
523 document->priv->page_sizes[page_index].height;
527 _ev_document_get_page_label (EvDocument *document,
530 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
532 return klass->get_page_label ?
533 klass->get_page_label (document, page) : NULL;
537 ev_document_get_page_label (EvDocument *document,
540 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
541 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
543 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
544 g_strdup (document->priv->page_labels[page_index]) :
545 g_strdup_printf ("%d", page_index + 1);
548 static EvDocumentInfo *
549 _ev_document_get_info (EvDocument *document)
551 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
553 return klass->get_info (document);
557 ev_document_get_info (EvDocument *document)
559 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
561 return document->priv->info;
565 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
567 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
569 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
570 if (klass->get_backend_info == NULL)
573 return klass->get_backend_info (document, info);
577 ev_document_render (EvDocument *document,
580 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
582 return klass->render (document, rc);
586 _ev_document_get_thumbnail (EvDocument *document,
589 cairo_surface_t *surface;
592 surface = ev_document_render (document, rc);
593 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
594 cairo_surface_destroy (surface);
600 ev_document_get_thumbnail (EvDocument *document,
603 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
605 if (klass->get_thumbnail)
606 return klass->get_thumbnail (document, rc);
608 return _ev_document_get_thumbnail (document, rc);
612 ev_document_get_uri (EvDocument *document)
614 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
616 return document->priv->uri;
620 ev_document_get_title (EvDocument *document)
622 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
624 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
625 document->priv->info->title : NULL;
629 ev_document_is_page_size_uniform (EvDocument *document)
631 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
633 return document->priv->uniform;
637 ev_document_get_max_page_size (EvDocument *document,
641 g_return_if_fail (EV_IS_DOCUMENT (document));
644 *width = document->priv->max_width;
646 *height = document->priv->max_height;
650 ev_document_get_min_page_size (EvDocument *document,
654 g_return_if_fail (EV_IS_DOCUMENT (document));
657 *width = document->priv->min_width;
659 *height = document->priv->min_height;
663 ev_document_check_dimensions (EvDocument *document)
665 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
667 return (document->priv->max_width > 0 && document->priv->max_height > 0);
671 ev_document_get_max_label_len (EvDocument *document)
673 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
675 return document->priv->max_label;
679 ev_document_has_text_page_labels (EvDocument *document)
681 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
683 return document->priv->page_labels != NULL;
687 ev_document_find_page_by_label (EvDocument *document,
688 const gchar *page_label,
693 gchar *endptr = NULL;
694 EvDocumentPrivate *priv = document->priv;
696 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
697 g_return_val_if_fail (page_label != NULL, FALSE);
698 g_return_val_if_fail (page_index != NULL, FALSE);
700 /* First, look for a literal label match */
701 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
702 if (priv->page_labels[i] != NULL &&
703 ! strcmp (page_label, priv->page_labels[i])) {
709 /* Second, look for a match with case insensitively */
710 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
711 if (priv->page_labels[i] != NULL &&
712 ! strcasecmp (page_label, priv->page_labels[i])) {
718 /* Next, parse the label, and see if the number fits */
719 value = strtol (page_label, &endptr, 10);
720 if (endptr[0] == '\0') {
721 /* Page number is an integer */
722 page = MIN (G_MAXINT, value);
724 /* convert from a page label to a page offset */
726 if (page >= 0 && page < priv->n_pages) {
736 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
739 ev_document_info_copy (EvDocumentInfo *info)
741 EvDocumentInfo *copy;
743 g_return_val_if_fail (info != NULL, NULL);
745 copy = g_new0 (EvDocumentInfo, 1);
746 copy->title = g_strdup (info->title);
747 copy->format = g_strdup (info->format);
748 copy->author = g_strdup (info->author);
749 copy->subject = g_strdup (info->subject);
750 copy->keywords = g_strdup (info->keywords);
751 copy->security = g_strdup (info->security);
752 copy->creator = g_strdup (info->creator);
753 copy->producer = g_strdup (info->producer);
754 copy->linearized = g_strdup (info->linearized);
756 copy->creation_date = info->creation_date;
757 copy->modified_date = info->modified_date;
758 copy->layout = info->layout;
759 copy->mode = info->mode;
760 copy->ui_hints = info->ui_hints;
761 copy->permissions = info->permissions;
762 copy->n_pages = info->n_pages;
763 copy->license = ev_document_license_copy (info->license);
765 copy->fields_mask = info->fields_mask;
771 ev_document_info_free (EvDocumentInfo *info)
776 g_free (info->title);
777 g_free (info->format);
778 g_free (info->author);
779 g_free (info->subject);
780 g_free (info->keywords);
781 g_free (info->creator);
782 g_free (info->producer);
783 g_free (info->linearized);
784 g_free (info->security);
785 ev_document_license_free (info->license);
790 /* EvDocumentLicense */
791 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
794 ev_document_license_new (void)
796 return g_new0 (EvDocumentLicense, 1);
800 ev_document_license_copy (EvDocumentLicense *license)
802 EvDocumentLicense *new_license;
807 new_license = ev_document_license_new ();
810 new_license->text = g_strdup (license->text);
812 new_license->uri = g_strdup (license->uri);
813 if (license->web_statement)
814 new_license->web_statement = g_strdup (license->web_statement);
820 ev_document_license_free (EvDocumentLicense *license)
825 g_free (license->text);
826 g_free (license->uri);
827 g_free (license->web_statement);
833 ev_document_license_get_text (EvDocumentLicense *license)
835 return license->text;
839 ev_document_license_get_uri (EvDocumentLicense *license)
845 ev_document_license_get_web_statement (EvDocumentLicense *license)
847 return license->web_statement;
851 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
854 ev_rectangle_new (void)
856 return g_new0 (EvRectangle, 1);
860 ev_rectangle_copy (EvRectangle *rectangle)
862 EvRectangle *new_rectangle;
864 g_return_val_if_fail (rectangle != NULL, NULL);
866 new_rectangle = g_new (EvRectangle, 1);
867 *new_rectangle = *rectangle;
869 return new_rectangle;
873 ev_rectangle_free (EvRectangle *rectangle)
878 /* Compares two rects. returns 0 if they're equal */
879 #define EPSILON 0.0000001
882 ev_rect_cmp (EvRectangle *a,
887 if (a == NULL || b == NULL)
890 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
891 (ABS (a->y1 - b->y1) < EPSILON) &&
892 (ABS (a->x2 - b->x2) < EPSILON) &&
893 (ABS (a->y2 - b->y2) < EPSILON));