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"
29 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
31 typedef struct _EvPageSize
37 struct _EvDocumentPrivate
44 gdouble uniform_width;
45 gdouble uniform_height;
54 EvPageSize *page_sizes;
58 static gint _ev_document_get_n_pages (EvDocument *document);
59 static void _ev_document_get_page_size (EvDocument *document,
63 static gchar *_ev_document_get_page_label (EvDocument *document,
65 static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
67 GMutex *ev_doc_mutex = NULL;
68 GMutex *ev_fc_mutex = NULL;
70 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
73 ev_document_error_quark (void)
77 q = g_quark_from_static_string ("ev-document-error-quark");
83 ev_document_impl_get_page (EvDocument *document,
86 return ev_page_new (index);
89 static EvDocumentInfo *
90 ev_document_impl_get_info (EvDocument *document)
92 return g_new0 (EvDocumentInfo, 1);
96 ev_document_finalize (GObject *object)
98 EvDocument *document = EV_DOCUMENT (object);
100 if (document->priv->uri) {
101 g_free (document->priv->uri);
102 document->priv->uri = NULL;
105 if (document->priv->page_sizes) {
106 g_free (document->priv->page_sizes);
107 document->priv->page_sizes = NULL;
110 if (document->priv->page_labels) {
113 for (i = 0; i < document->priv->n_pages; i++) {
114 g_free (document->priv->page_labels[i]);
116 g_free (document->priv->page_labels);
117 document->priv->page_labels = NULL;
120 if (document->priv->info) {
121 ev_document_info_free (document->priv->info);
122 document->priv->info = NULL;
125 G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
129 ev_document_init (EvDocument *document)
131 document->priv = EV_DOCUMENT_GET_PRIVATE (document);
133 /* Assume all pages are the same size until proven otherwise */
134 document->priv->uniform = TRUE;
138 ev_document_class_init (EvDocumentClass *klass)
140 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
142 g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
144 klass->get_page = ev_document_impl_get_page;
145 klass->get_info = ev_document_impl_get_info;
146 klass->get_backend_info = NULL;
148 g_object_class->finalize = ev_document_finalize;
152 ev_document_get_doc_mutex (void)
154 if (ev_doc_mutex == NULL) {
155 ev_doc_mutex = g_mutex_new ();
161 ev_document_doc_mutex_lock (void)
163 g_mutex_lock (ev_document_get_doc_mutex ());
167 ev_document_doc_mutex_unlock (void)
169 g_mutex_unlock (ev_document_get_doc_mutex ());
173 ev_document_doc_mutex_trylock (void)
175 return g_mutex_trylock (ev_document_get_doc_mutex ());
179 ev_document_get_fc_mutex (void)
181 if (ev_fc_mutex == NULL) {
182 ev_fc_mutex = g_mutex_new ();
188 ev_document_fc_mutex_lock (void)
190 g_mutex_lock (ev_document_get_fc_mutex ());
194 ev_document_fc_mutex_unlock (void)
196 g_mutex_unlock (ev_document_get_fc_mutex ());
200 ev_document_fc_mutex_trylock (void)
202 return g_mutex_trylock (ev_document_get_fc_mutex ());
207 * @document: a #EvDocument
208 * @uri: the document's URI
209 * @error: a #GError location to store an error, or %NULL
211 * Loads @document from @uri.
213 * On failure, %FALSE is returned and @error is filled in.
214 * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
215 * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
216 * is returned. Other errors are possible too, depending on the backend
217 * used to load the document and the URI, e.g. #GIOError, #GFileError, and
220 * Returns: %TRUE on success, or %FALSE on failure.
223 ev_document_load (EvDocument *document,
227 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
231 retval = klass->load (document, uri, &err);
234 g_propagate_error (error, err);
236 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
237 G_OBJECT_TYPE_NAME (document));
239 /* So upper layers don't crash */
240 g_set_error_literal (error,
242 EV_DOCUMENT_ERROR_INVALID,
243 "Internal error in backend");
247 EvDocumentPrivate *priv = document->priv;
249 /* Cache some info about the document to avoid
250 * going to the backends since it requires locks
252 priv->uri = g_strdup (uri);
253 priv->n_pages = _ev_document_get_n_pages (document);
255 for (i = 0; i < priv->n_pages; i++) {
256 EvPage *page = ev_document_get_page (document, i);
257 gdouble page_width = 0;
258 gdouble page_height = 0;
259 EvPageSize *page_size;
262 _ev_document_get_page_size (document, page, &page_width, &page_height);
265 priv->uniform_width = page_width;
266 priv->uniform_height = page_height;
267 priv->max_width = priv->uniform_width;
268 priv->max_height = priv->uniform_height;
269 priv->min_width = priv->uniform_width;
270 priv->min_height = priv->uniform_height;
271 } else if (priv->uniform &&
272 (priv->uniform_width != page_width ||
273 priv->uniform_height != page_height)) {
274 /* It's a different page size. Backfill the array. */
277 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
279 for (j = 0; j < i; j++) {
280 page_size = &(priv->page_sizes[j]);
281 page_size->width = priv->uniform_width;
282 page_size->height = priv->uniform_height;
284 priv->uniform = FALSE;
286 if (!priv->uniform) {
287 page_size = &(priv->page_sizes[i]);
289 page_size->width = page_width;
290 page_size->height = page_height;
292 if (page_width > priv->max_width)
293 priv->max_width = page_width;
294 if (page_width < priv->min_width)
295 priv->min_width = page_width;
297 if (page_height > priv->max_height)
298 priv->max_height = page_height;
299 if (page_height < priv->min_height)
300 priv->min_height = page_height;
303 page_label = _ev_document_get_page_label (document, page);
305 if (!priv->page_labels)
306 priv->page_labels = g_new0 (gchar *, priv->n_pages);
308 priv->page_labels[i] = page_label;
309 priv->max_label = MAX (priv->max_label,
310 g_utf8_strlen (page_label, 256));
313 g_object_unref (page);
316 priv->info = _ev_document_get_info (document);
325 * @uri: the target URI
326 * @error: a #GError location to store an error, or %NULL
328 * Saves @document to @uri.
330 * Returns: %TRUE on success, or %FALSE on error with @error filled in
333 ev_document_save (EvDocument *document,
337 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
339 return klass->save (document, uri, error);
343 ev_document_get_page (EvDocument *document,
346 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
348 return klass->get_page (document, index);
352 _ev_document_get_n_pages (EvDocument *document)
354 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
356 return klass->get_n_pages (document);
360 ev_document_get_n_pages (EvDocument *document)
362 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
364 return document->priv->n_pages;
368 _ev_document_get_page_size (EvDocument *document,
373 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
375 klass->get_page_size (document, page, width, height);
379 ev_document_get_page_size (EvDocument *document,
384 g_return_if_fail (EV_IS_DOCUMENT (document));
385 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
388 *width = document->priv->uniform ?
389 document->priv->uniform_width :
390 document->priv->page_sizes[page_index].width;
392 *height = document->priv->uniform ?
393 document->priv->uniform_height :
394 document->priv->page_sizes[page_index].height;
398 _ev_document_get_page_label (EvDocument *document,
401 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
403 return klass->get_page_label ?
404 klass->get_page_label (document, page) : NULL;
408 ev_document_get_page_label (EvDocument *document,
411 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
412 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
414 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
415 g_strdup (document->priv->page_labels[page_index]) :
416 g_strdup_printf ("%d", page_index + 1);
419 static EvDocumentInfo *
420 _ev_document_get_info (EvDocument *document)
422 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
424 return klass->get_info (document);
428 ev_document_get_info (EvDocument *document)
430 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
432 return document->priv->info;
436 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
438 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
440 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
441 if (klass->get_backend_info == NULL)
444 return klass->get_backend_info (document, info);
448 ev_document_render (EvDocument *document,
451 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
453 return klass->render (document, rc);
457 ev_document_get_uri (EvDocument *document)
459 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
461 return document->priv->uri;
465 ev_document_get_title (EvDocument *document)
467 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
469 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
470 document->priv->info->title : NULL;
474 ev_document_is_page_size_uniform (EvDocument *document)
476 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
478 return document->priv->uniform;
482 ev_document_get_max_page_size (EvDocument *document,
486 g_return_if_fail (EV_IS_DOCUMENT (document));
489 *width = document->priv->max_width;
491 *height = document->priv->max_height;
495 ev_document_get_min_page_size (EvDocument *document,
499 g_return_if_fail (EV_IS_DOCUMENT (document));
502 *width = document->priv->min_width;
504 *height = document->priv->min_height;
508 ev_document_check_dimensions (EvDocument *document)
510 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
512 return (document->priv->max_width > 0 && document->priv->max_height > 0);
516 ev_document_get_max_label_len (EvDocument *document)
518 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
520 return document->priv->max_label;
524 ev_document_has_text_page_labels (EvDocument *document)
526 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
528 return document->priv->page_labels != NULL;
532 ev_document_find_page_by_label (EvDocument *document,
533 const gchar *page_label,
538 gchar *endptr = NULL;
539 EvDocumentPrivate *priv = document->priv;
541 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
542 g_return_val_if_fail (page_label != NULL, FALSE);
543 g_return_val_if_fail (page_index != NULL, FALSE);
545 /* First, look for a literal label match */
546 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
547 if (priv->page_labels[i] != NULL &&
548 ! strcmp (page_label, priv->page_labels[i])) {
554 /* Second, look for a match with case insensitively */
555 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
556 if (priv->page_labels[i] != NULL &&
557 ! strcasecmp (page_label, priv->page_labels[i])) {
563 /* Next, parse the label, and see if the number fits */
564 value = strtol (page_label, &endptr, 10);
565 if (endptr[0] == '\0') {
566 /* Page number is an integer */
567 page = MIN (G_MAXINT, value);
569 /* convert from a page label to a page offset */
571 if (page >= 0 && page < priv->n_pages) {
581 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
584 ev_document_info_copy (EvDocumentInfo *info)
586 EvDocumentInfo *copy;
588 g_return_val_if_fail (info != NULL, NULL);
590 copy = g_new0 (EvDocumentInfo, 1);
591 copy->title = g_strdup (info->title);
592 copy->format = g_strdup (info->format);
593 copy->author = g_strdup (info->author);
594 copy->subject = g_strdup (info->subject);
595 copy->keywords = g_strdup (info->keywords);
596 copy->security = g_strdup (info->security);
597 copy->creator = g_strdup (info->creator);
598 copy->producer = g_strdup (info->producer);
599 copy->linearized = g_strdup (info->linearized);
601 copy->creation_date = info->creation_date;
602 copy->modified_date = info->modified_date;
603 copy->layout = info->layout;
604 copy->mode = info->mode;
605 copy->ui_hints = info->ui_hints;
606 copy->permissions = info->permissions;
607 copy->n_pages = info->n_pages;
608 copy->license = ev_document_license_copy (info->license);
610 copy->fields_mask = info->fields_mask;
616 ev_document_info_free (EvDocumentInfo *info)
621 g_free (info->title);
622 g_free (info->format);
623 g_free (info->author);
624 g_free (info->subject);
625 g_free (info->keywords);
626 g_free (info->creator);
627 g_free (info->producer);
628 g_free (info->linearized);
629 g_free (info->security);
630 ev_document_license_free (info->license);
635 /* EvDocumentLicense */
636 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
639 ev_document_license_new (void)
641 return g_new0 (EvDocumentLicense, 1);
645 ev_document_license_copy (EvDocumentLicense *license)
647 EvDocumentLicense *new_license;
652 new_license = ev_document_license_new ();
655 new_license->text = g_strdup (license->text);
657 new_license->uri = g_strdup (license->uri);
658 if (license->web_statement)
659 new_license->web_statement = g_strdup (license->web_statement);
665 ev_document_license_free (EvDocumentLicense *license)
670 g_free (license->text);
671 g_free (license->uri);
672 g_free (license->web_statement);
678 ev_document_license_get_text (EvDocumentLicense *license)
680 return license->text;
684 ev_document_license_get_uri (EvDocumentLicense *license)
690 ev_document_license_get_web_statement (EvDocumentLicense *license)
692 return license->web_statement;
696 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
699 ev_rectangle_new (void)
701 return g_new0 (EvRectangle, 1);
705 ev_rectangle_copy (EvRectangle *rectangle)
707 EvRectangle *new_rectangle;
709 g_return_val_if_fail (rectangle != NULL, NULL);
711 new_rectangle = g_new (EvRectangle, 1);
712 *new_rectangle = *rectangle;
714 return new_rectangle;
718 ev_rectangle_free (EvRectangle *rectangle)
723 /* Compares two rects. returns 0 if they're equal */
724 #define EPSILON 0.0000001
727 ev_rect_cmp (EvRectangle *a,
732 if (a == NULL || b == NULL)
735 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
736 (ABS (a->y1 - b->y1) < EPSILON) &&
737 (ABS (a->x2 - b->x2) < EPSILON) &&
738 (ABS (a->y2 - b->y2) < EPSILON));