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 "synctex_parser.h"
30 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
32 typedef struct _EvPageSize
38 struct _EvDocumentPrivate
45 gdouble uniform_width;
46 gdouble uniform_height;
55 EvPageSize *page_sizes;
58 synctex_scanner_t synctex_scanner;
61 static gint _ev_document_get_n_pages (EvDocument *document);
62 static void _ev_document_get_page_size (EvDocument *document,
66 static gchar *_ev_document_get_page_label (EvDocument *document,
68 static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
69 static gboolean _ev_document_support_synctex (EvDocument *document);
71 GMutex *ev_doc_mutex = NULL;
72 GMutex *ev_fc_mutex = NULL;
74 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
77 ev_document_error_quark (void)
81 q = g_quark_from_static_string ("ev-document-error-quark");
87 ev_document_impl_get_page (EvDocument *document,
90 return ev_page_new (index);
93 static EvDocumentInfo *
94 ev_document_impl_get_info (EvDocument *document)
96 return g_new0 (EvDocumentInfo, 1);
100 ev_document_finalize (GObject *object)
102 EvDocument *document = EV_DOCUMENT (object);
104 if (document->priv->uri) {
105 g_free (document->priv->uri);
106 document->priv->uri = NULL;
109 if (document->priv->page_sizes) {
110 g_free (document->priv->page_sizes);
111 document->priv->page_sizes = NULL;
114 if (document->priv->page_labels) {
117 for (i = 0; i < document->priv->n_pages; i++) {
118 g_free (document->priv->page_labels[i]);
120 g_free (document->priv->page_labels);
121 document->priv->page_labels = NULL;
124 if (document->priv->info) {
125 ev_document_info_free (document->priv->info);
126 document->priv->info = NULL;
129 if (document->priv->synctex_scanner) {
130 synctex_scanner_free (document->priv->synctex_scanner);
131 document->priv->synctex_scanner = NULL;
134 G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
138 ev_document_init (EvDocument *document)
140 document->priv = EV_DOCUMENT_GET_PRIVATE (document);
142 /* Assume all pages are the same size until proven otherwise */
143 document->priv->uniform = TRUE;
147 ev_document_class_init (EvDocumentClass *klass)
149 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
151 g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
153 klass->get_page = ev_document_impl_get_page;
154 klass->get_info = ev_document_impl_get_info;
155 klass->get_backend_info = NULL;
157 g_object_class->finalize = ev_document_finalize;
161 ev_document_get_doc_mutex (void)
163 if (ev_doc_mutex == NULL) {
164 ev_doc_mutex = g_mutex_new ();
170 ev_document_doc_mutex_lock (void)
172 g_mutex_lock (ev_document_get_doc_mutex ());
176 ev_document_doc_mutex_unlock (void)
178 g_mutex_unlock (ev_document_get_doc_mutex ());
182 ev_document_doc_mutex_trylock (void)
184 return g_mutex_trylock (ev_document_get_doc_mutex ());
188 ev_document_get_fc_mutex (void)
190 if (ev_fc_mutex == NULL) {
191 ev_fc_mutex = g_mutex_new ();
197 ev_document_fc_mutex_lock (void)
199 g_mutex_lock (ev_document_get_fc_mutex ());
203 ev_document_fc_mutex_unlock (void)
205 g_mutex_unlock (ev_document_get_fc_mutex ());
209 ev_document_fc_mutex_trylock (void)
211 return g_mutex_trylock (ev_document_get_fc_mutex ());
216 * @document: a #EvDocument
217 * @uri: the document's URI
218 * @error: a #GError location to store an error, or %NULL
220 * Loads @document from @uri.
222 * On failure, %FALSE is returned and @error is filled in.
223 * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
224 * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
225 * is returned. Other errors are possible too, depending on the backend
226 * used to load the document and the URI, e.g. #GIOError, #GFileError, and
229 * Returns: %TRUE on success, or %FALSE on failure.
232 ev_document_load (EvDocument *document,
236 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
240 retval = klass->load (document, uri, &err);
243 g_propagate_error (error, err);
245 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
246 G_OBJECT_TYPE_NAME (document));
248 /* So upper layers don't crash */
249 g_set_error_literal (error,
251 EV_DOCUMENT_ERROR_INVALID,
252 "Internal error in backend");
256 EvDocumentPrivate *priv = document->priv;
258 /* Cache some info about the document to avoid
259 * going to the backends since it requires locks
261 priv->uri = g_strdup (uri);
262 priv->n_pages = _ev_document_get_n_pages (document);
264 for (i = 0; i < priv->n_pages; i++) {
265 EvPage *page = ev_document_get_page (document, i);
266 gdouble page_width = 0;
267 gdouble page_height = 0;
268 EvPageSize *page_size;
271 _ev_document_get_page_size (document, page, &page_width, &page_height);
274 priv->uniform_width = page_width;
275 priv->uniform_height = page_height;
276 priv->max_width = priv->uniform_width;
277 priv->max_height = priv->uniform_height;
278 priv->min_width = priv->uniform_width;
279 priv->min_height = priv->uniform_height;
280 } else if (priv->uniform &&
281 (priv->uniform_width != page_width ||
282 priv->uniform_height != page_height)) {
283 /* It's a different page size. Backfill the array. */
286 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
288 for (j = 0; j < i; j++) {
289 page_size = &(priv->page_sizes[j]);
290 page_size->width = priv->uniform_width;
291 page_size->height = priv->uniform_height;
293 priv->uniform = FALSE;
295 if (!priv->uniform) {
296 page_size = &(priv->page_sizes[i]);
298 page_size->width = page_width;
299 page_size->height = page_height;
301 if (page_width > priv->max_width)
302 priv->max_width = page_width;
303 if (page_width < priv->min_width)
304 priv->min_width = page_width;
306 if (page_height > priv->max_height)
307 priv->max_height = page_height;
308 if (page_height < priv->min_height)
309 priv->min_height = page_height;
312 page_label = _ev_document_get_page_label (document, page);
314 if (!priv->page_labels)
315 priv->page_labels = g_new0 (gchar *, priv->n_pages);
317 priv->page_labels[i] = page_label;
318 priv->max_label = MAX (priv->max_label,
319 g_utf8_strlen (page_label, 256));
322 g_object_unref (page);
325 priv->info = _ev_document_get_info (document);
326 if (_ev_document_support_synctex (document)) {
329 filename = g_filename_from_uri (uri, NULL, NULL);
330 if (filename != NULL) {
331 priv->synctex_scanner =
332 synctex_scanner_new_with_output_file (filename, NULL, 1);
344 * @uri: the target URI
345 * @error: a #GError location to store an error, or %NULL
347 * Saves @document to @uri.
349 * Returns: %TRUE on success, or %FALSE on error with @error filled in
352 ev_document_save (EvDocument *document,
356 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
358 return klass->save (document, uri, error);
362 ev_document_get_page (EvDocument *document,
365 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
367 return klass->get_page (document, index);
371 _ev_document_support_synctex (EvDocument *document)
373 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
375 return klass->support_synctex ? klass->support_synctex (document) : FALSE;
379 ev_document_has_synctex (EvDocument *document)
381 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
383 return document->priv->synctex_scanner != NULL;
387 * ev_document_synctex_backward_search:
389 * @page: the target page
393 * Peforms a Synctex backward search to obtain the TeX input file, line and
394 * (possibly) column corresponding to the position (@x,@y) (in 72dpi
395 * coordinates) in the @page of @document.
397 * Returns: A pointer to the EvSourceLink structure that holds the result. @NULL if synctex
398 * is not enabled for the document or no result is found.
399 * The EvSourceLink pointer should be freed with g_free after it is used.
402 ev_document_synctex_backward_search (EvDocument *document,
407 EvSourceLink *result = NULL;
408 synctex_scanner_t scanner;
410 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
412 scanner = document->priv->synctex_scanner;
416 if (synctex_edit_query (scanner, page_index + 1, x, y) > 0) {
419 /* We assume that a backward search returns either zero or one result_node */
420 node = synctex_next_result (scanner);
422 result = g_new (EvSourceLink, 1);
423 result->filename = synctex_scanner_get_name (scanner,
424 synctex_node_tag (node));
425 result->line = synctex_node_line (node);
426 result->col = synctex_node_column (node);
434 * ev_document_synctex_forward_search:
438 * Peforms a Synctex forward search to obtain the area in the document
439 * corresponding to the position @line and @column number in the source Tex file
441 * Returns: An EvMapping with the page number and area corresponfing to
442 * the given line in the source file. It must be free with g_free when done
445 ev_document_synctex_forward_search (EvDocument *document,
448 EvMapping *result = NULL;
449 synctex_scanner_t scanner;
451 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
453 scanner = document->priv->synctex_scanner;
457 if (synctex_display_query (scanner, link->filename, link->line, link->col) > 0) {
461 if ((node = synctex_next_result (scanner))) {
462 result = g_new (EvMapping, 1);
464 page = synctex_node_page (node) - 1;
465 result->data = GINT_TO_POINTER (page);
467 result->area.x1 = synctex_node_box_visible_h (node);
468 result->area.y1 = synctex_node_box_visible_v (node) -
469 synctex_node_box_visible_height (node);
470 result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
471 result->area.y2 = synctex_node_box_visible_depth (node) +
472 synctex_node_box_visible_height (node) + result->area.y1;
480 _ev_document_get_n_pages (EvDocument *document)
482 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
484 return klass->get_n_pages (document);
488 ev_document_get_n_pages (EvDocument *document)
490 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
492 return document->priv->n_pages;
496 _ev_document_get_page_size (EvDocument *document,
501 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
503 klass->get_page_size (document, page, width, height);
507 ev_document_get_page_size (EvDocument *document,
512 g_return_if_fail (EV_IS_DOCUMENT (document));
513 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
516 *width = document->priv->uniform ?
517 document->priv->uniform_width :
518 document->priv->page_sizes[page_index].width;
520 *height = document->priv->uniform ?
521 document->priv->uniform_height :
522 document->priv->page_sizes[page_index].height;
526 _ev_document_get_page_label (EvDocument *document,
529 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
531 return klass->get_page_label ?
532 klass->get_page_label (document, page) : NULL;
536 ev_document_get_page_label (EvDocument *document,
539 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
540 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
542 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
543 g_strdup (document->priv->page_labels[page_index]) :
544 g_strdup_printf ("%d", page_index + 1);
547 static EvDocumentInfo *
548 _ev_document_get_info (EvDocument *document)
550 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
552 return klass->get_info (document);
556 ev_document_get_info (EvDocument *document)
558 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
560 return document->priv->info;
564 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
566 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
568 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
569 if (klass->get_backend_info == NULL)
572 return klass->get_backend_info (document, info);
576 ev_document_render (EvDocument *document,
579 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
581 return klass->render (document, rc);
585 ev_document_get_uri (EvDocument *document)
587 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
589 return document->priv->uri;
593 ev_document_get_title (EvDocument *document)
595 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
597 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
598 document->priv->info->title : NULL;
602 ev_document_is_page_size_uniform (EvDocument *document)
604 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
606 return document->priv->uniform;
610 ev_document_get_max_page_size (EvDocument *document,
614 g_return_if_fail (EV_IS_DOCUMENT (document));
617 *width = document->priv->max_width;
619 *height = document->priv->max_height;
623 ev_document_get_min_page_size (EvDocument *document,
627 g_return_if_fail (EV_IS_DOCUMENT (document));
630 *width = document->priv->min_width;
632 *height = document->priv->min_height;
636 ev_document_check_dimensions (EvDocument *document)
638 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
640 return (document->priv->max_width > 0 && document->priv->max_height > 0);
644 ev_document_get_max_label_len (EvDocument *document)
646 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
648 return document->priv->max_label;
652 ev_document_has_text_page_labels (EvDocument *document)
654 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
656 return document->priv->page_labels != NULL;
660 ev_document_find_page_by_label (EvDocument *document,
661 const gchar *page_label,
666 gchar *endptr = NULL;
667 EvDocumentPrivate *priv = document->priv;
669 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
670 g_return_val_if_fail (page_label != NULL, FALSE);
671 g_return_val_if_fail (page_index != NULL, FALSE);
673 /* First, look for a literal label match */
674 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
675 if (priv->page_labels[i] != NULL &&
676 ! strcmp (page_label, priv->page_labels[i])) {
682 /* Second, look for a match with case insensitively */
683 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
684 if (priv->page_labels[i] != NULL &&
685 ! strcasecmp (page_label, priv->page_labels[i])) {
691 /* Next, parse the label, and see if the number fits */
692 value = strtol (page_label, &endptr, 10);
693 if (endptr[0] == '\0') {
694 /* Page number is an integer */
695 page = MIN (G_MAXINT, value);
697 /* convert from a page label to a page offset */
699 if (page >= 0 && page < priv->n_pages) {
709 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
712 ev_document_info_copy (EvDocumentInfo *info)
714 EvDocumentInfo *copy;
716 g_return_val_if_fail (info != NULL, NULL);
718 copy = g_new0 (EvDocumentInfo, 1);
719 copy->title = g_strdup (info->title);
720 copy->format = g_strdup (info->format);
721 copy->author = g_strdup (info->author);
722 copy->subject = g_strdup (info->subject);
723 copy->keywords = g_strdup (info->keywords);
724 copy->security = g_strdup (info->security);
725 copy->creator = g_strdup (info->creator);
726 copy->producer = g_strdup (info->producer);
727 copy->linearized = g_strdup (info->linearized);
729 copy->creation_date = info->creation_date;
730 copy->modified_date = info->modified_date;
731 copy->layout = info->layout;
732 copy->mode = info->mode;
733 copy->ui_hints = info->ui_hints;
734 copy->permissions = info->permissions;
735 copy->n_pages = info->n_pages;
736 copy->license = ev_document_license_copy (info->license);
738 copy->fields_mask = info->fields_mask;
744 ev_document_info_free (EvDocumentInfo *info)
749 g_free (info->title);
750 g_free (info->format);
751 g_free (info->author);
752 g_free (info->subject);
753 g_free (info->keywords);
754 g_free (info->creator);
755 g_free (info->producer);
756 g_free (info->linearized);
757 g_free (info->security);
758 ev_document_license_free (info->license);
763 /* EvDocumentLicense */
764 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
767 ev_document_license_new (void)
769 return g_new0 (EvDocumentLicense, 1);
773 ev_document_license_copy (EvDocumentLicense *license)
775 EvDocumentLicense *new_license;
780 new_license = ev_document_license_new ();
783 new_license->text = g_strdup (license->text);
785 new_license->uri = g_strdup (license->uri);
786 if (license->web_statement)
787 new_license->web_statement = g_strdup (license->web_statement);
793 ev_document_license_free (EvDocumentLicense *license)
798 g_free (license->text);
799 g_free (license->uri);
800 g_free (license->web_statement);
806 ev_document_license_get_text (EvDocumentLicense *license)
808 return license->text;
812 ev_document_license_get_uri (EvDocumentLicense *license)
818 ev_document_license_get_web_statement (EvDocumentLicense *license)
820 return license->web_statement;
824 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
827 ev_rectangle_new (void)
829 return g_new0 (EvRectangle, 1);
833 ev_rectangle_copy (EvRectangle *rectangle)
835 EvRectangle *new_rectangle;
837 g_return_val_if_fail (rectangle != NULL, NULL);
839 new_rectangle = g_new (EvRectangle, 1);
840 *new_rectangle = *rectangle;
842 return new_rectangle;
846 ev_rectangle_free (EvRectangle *rectangle)
851 /* Compares two rects. returns 0 if they're equal */
852 #define EPSILON 0.0000001
855 ev_rect_cmp (EvRectangle *a,
860 if (a == NULL || b == NULL)
863 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
864 (ABS (a->y1 - b->y1) < EPSILON) &&
865 (ABS (a->x2 - b->x2) < EPSILON) &&
866 (ABS (a->y2 - b->y2) < EPSILON));