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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
52 EvPageSize *page_sizes;
56 static gint _ev_document_get_n_pages (EvDocument *document);
57 static void _ev_document_get_page_size (EvDocument *document,
61 static gchar *_ev_document_get_page_label (EvDocument *document,
63 static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
65 GMutex *ev_doc_mutex = NULL;
66 GMutex *ev_fc_mutex = NULL;
68 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
71 ev_document_error_quark (void)
75 q = g_quark_from_static_string ("ev-document-error-quark");
81 ev_document_impl_get_page (EvDocument *document,
84 return ev_page_new (index);
87 static EvDocumentInfo *
88 ev_document_impl_get_info (EvDocument *document)
90 return g_new0 (EvDocumentInfo, 1);
94 ev_document_finalize (GObject *object)
96 EvDocument *document = EV_DOCUMENT (object);
98 if (document->priv->uri) {
99 g_free (document->priv->uri);
100 document->priv->uri = NULL;
103 if (document->priv->page_sizes) {
104 g_free (document->priv->page_sizes);
105 document->priv->page_sizes = NULL;
108 if (document->priv->page_labels) {
111 for (i = 0; i < document->priv->n_pages; i++) {
112 g_free (document->priv->page_labels[i]);
114 g_free (document->priv->page_labels);
115 document->priv->page_labels = NULL;
118 if (document->priv->info) {
119 ev_document_info_free (document->priv->info);
120 document->priv->info = NULL;
123 G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
127 ev_document_init (EvDocument *document)
129 document->priv = EV_DOCUMENT_GET_PRIVATE (document);
131 /* Assume all pages are the same size until proven otherwise */
132 document->priv->uniform = TRUE;
136 ev_document_class_init (EvDocumentClass *klass)
138 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
140 g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
142 klass->get_page = ev_document_impl_get_page;
143 klass->get_info = ev_document_impl_get_info;
145 g_object_class->finalize = ev_document_finalize;
149 ev_document_get_doc_mutex (void)
151 if (ev_doc_mutex == NULL) {
152 ev_doc_mutex = g_mutex_new ();
158 ev_document_doc_mutex_lock (void)
160 g_mutex_lock (ev_document_get_doc_mutex ());
164 ev_document_doc_mutex_unlock (void)
166 g_mutex_unlock (ev_document_get_doc_mutex ());
170 ev_document_doc_mutex_trylock (void)
172 return g_mutex_trylock (ev_document_get_doc_mutex ());
176 ev_document_get_fc_mutex (void)
178 if (ev_fc_mutex == NULL) {
179 ev_fc_mutex = g_mutex_new ();
185 ev_document_fc_mutex_lock (void)
187 g_mutex_lock (ev_document_get_fc_mutex ());
191 ev_document_fc_mutex_unlock (void)
193 g_mutex_unlock (ev_document_get_fc_mutex ());
197 ev_document_fc_mutex_trylock (void)
199 return g_mutex_trylock (ev_document_get_fc_mutex ());
204 * @document: a #EvDocument
205 * @uri: the document's URI
206 * @error: a #GError location to store an error, or %NULL
208 * Loads @document from @uri.
210 * On failure, %FALSE is returned and @error is filled in.
211 * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
212 * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
213 * is returned. Other errors are possible too, depending on the backend
214 * used to load the document and the URI, e.g. #GIOError, #GFileError, and
217 * Returns: %TRUE on success, or %FALSE on failure.
220 ev_document_load (EvDocument *document,
224 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
228 retval = klass->load (document, uri, &err);
231 g_propagate_error (error, err);
233 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
234 G_OBJECT_TYPE_NAME (document));
236 /* So upper layers don't crash */
237 g_set_error_literal (error,
239 EV_DOCUMENT_ERROR_INVALID,
240 "Internal error in backend");
244 EvDocumentPrivate *priv = document->priv;
246 /* Cache some info about the document to avoid
247 * going to the backends since it requires locks
249 priv->uri = g_strdup (uri);
250 priv->n_pages = _ev_document_get_n_pages (document);
252 for (i = 0; i < priv->n_pages; i++) {
253 EvPage *page = ev_document_get_page (document, i);
254 gdouble page_width = 0;
255 gdouble page_height = 0;
256 EvPageSize *page_size;
259 _ev_document_get_page_size (document, page, &page_width, &page_height);
262 priv->uniform_width = page_width;
263 priv->uniform_height = page_height;
264 priv->max_width = priv->uniform_width;
265 priv->max_height = priv->uniform_height;
266 } else if (priv->uniform &&
267 (priv->uniform_width != page_width ||
268 priv->uniform_height != page_height)) {
269 /* It's a different page size. Backfill the array. */
272 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
274 for (j = 0; j < i; j++) {
275 page_size = &(priv->page_sizes[j]);
276 page_size->width = priv->uniform_width;
277 page_size->height = priv->uniform_height;
279 priv->uniform = FALSE;
281 if (!priv->uniform) {
282 page_size = &(priv->page_sizes[i]);
284 page_size->width = page_width;
285 page_size->height = page_height;
287 if (page_width > priv->max_width)
288 priv->max_width = page_width;
290 if (page_height > priv->max_height)
291 priv->max_height = page_height;
294 page_label = _ev_document_get_page_label (document, page);
296 if (priv->page_labels) {
297 priv->page_labels[i] = page_label;
299 gchar *numeric_label;
301 numeric_label = g_strdup_printf ("%d", i + 1);
302 if (strcmp (numeric_label, page_label) != 0) {
303 priv->page_labels = g_new0 (gchar *, priv->n_pages);
304 priv->page_labels[i] = page_label;
306 g_free (numeric_label);
308 priv->max_label = MAX (priv->max_label,
309 g_utf8_strlen (page_label, 256));
312 g_object_unref (page);
315 priv->info = _ev_document_get_info (document);
324 * @uri: the target URI
325 * @error: a #GError location to store an error, or %NULL
327 * Saves @document to @uri.
329 * Returns: %TRUE on success, or %FALSE on error with @error filled in
332 ev_document_save (EvDocument *document,
336 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
338 return klass->save (document, uri, error);
342 ev_document_get_page (EvDocument *document,
345 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
347 return klass->get_page (document, index);
351 _ev_document_get_n_pages (EvDocument *document)
353 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
355 return klass->get_n_pages (document);
359 ev_document_get_n_pages (EvDocument *document)
361 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
363 return document->priv->n_pages;
367 _ev_document_get_page_size (EvDocument *document,
372 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
374 klass->get_page_size (document, page, width, height);
378 ev_document_get_page_size (EvDocument *document,
383 g_return_if_fail (EV_IS_DOCUMENT (document));
384 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
387 *width = document->priv->uniform ?
388 document->priv->uniform_width :
389 document->priv->page_sizes[page_index].width;
391 *height = document->priv->uniform ?
392 document->priv->uniform_height :
393 document->priv->page_sizes[page_index].height;
397 _ev_document_get_page_label (EvDocument *document,
400 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
402 return klass->get_page_label ?
403 klass->get_page_label (document, page) : NULL;
407 ev_document_get_page_label (EvDocument *document,
410 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
411 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
413 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
414 g_strdup (document->priv->page_labels[page_index]) :
415 g_strdup_printf ("%d", page_index + 1);
418 static EvDocumentInfo *
419 _ev_document_get_info (EvDocument *document)
421 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
423 return klass->get_info (document);
427 ev_document_get_info (EvDocument *document)
429 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
431 return document->priv->info;
435 ev_document_render (EvDocument *document,
438 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
440 return klass->render (document, rc);
444 ev_document_get_uri (EvDocument *document)
446 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
448 return document->priv->uri;
452 ev_document_get_title (EvDocument *document)
454 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
456 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
457 document->priv->info->title : NULL;
461 ev_document_is_page_size_uniform (EvDocument *document)
463 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
465 return document->priv->uniform;
469 ev_document_get_max_page_size (EvDocument *document,
473 g_return_if_fail (EV_IS_DOCUMENT (document));
476 *width = document->priv->max_width;
478 *height = document->priv->max_height;
482 ev_document_check_dimensions (EvDocument *document)
484 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
486 return (document->priv->max_width > 0 && document->priv->max_height > 0);
490 ev_document_get_max_label_len (EvDocument *document)
492 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
494 return document->priv->max_label;
498 ev_document_has_text_page_labels (EvDocument *document)
500 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
502 return document->priv->page_labels != NULL;
506 ev_document_find_page_by_label (EvDocument *document,
507 const gchar *page_label,
512 gchar *endptr = NULL;
513 EvDocumentPrivate *priv = document->priv;
515 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
516 g_return_val_if_fail (page_label != NULL, FALSE);
517 g_return_val_if_fail (page_index != NULL, FALSE);
519 /* First, look for a literal label match */
520 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
521 if (priv->page_labels[i] != NULL &&
522 ! strcmp (page_label, priv->page_labels[i])) {
528 /* Second, look for a match with case insensitively */
529 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
530 if (priv->page_labels[i] != NULL &&
531 ! strcasecmp (page_label, priv->page_labels[i])) {
537 /* Next, parse the label, and see if the number fits */
538 value = strtol (page_label, &endptr, 10);
539 if (endptr[0] == '\0') {
540 /* Page number is an integer */
541 page = MIN (G_MAXINT, value);
543 /* convert from a page label to a page offset */
545 if (page >= 0 && page < priv->n_pages) {
555 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
558 ev_document_info_copy (EvDocumentInfo *info)
560 EvDocumentInfo *copy;
562 g_return_val_if_fail (info != NULL, NULL);
564 copy = g_new0 (EvDocumentInfo, 1);
565 copy->title = g_strdup (info->title);
566 copy->format = g_strdup (info->format);
567 copy->author = g_strdup (info->author);
568 copy->subject = g_strdup (info->subject);
569 copy->keywords = g_strdup (info->keywords);
570 copy->security = g_strdup (info->security);
571 copy->creator = g_strdup (info->creator);
572 copy->producer = g_strdup (info->producer);
573 copy->linearized = g_strdup (info->linearized);
575 copy->creation_date = info->creation_date;
576 copy->modified_date = info->modified_date;
577 copy->layout = info->layout;
578 copy->mode = info->mode;
579 copy->ui_hints = info->ui_hints;
580 copy->permissions = info->permissions;
581 copy->n_pages = info->n_pages;
582 copy->license = ev_document_license_copy (info->license);
584 copy->fields_mask = info->fields_mask;
590 ev_document_info_free (EvDocumentInfo *info)
595 g_free (info->title);
596 g_free (info->format);
597 g_free (info->author);
598 g_free (info->subject);
599 g_free (info->keywords);
600 g_free (info->creator);
601 g_free (info->producer);
602 g_free (info->linearized);
603 g_free (info->security);
604 ev_document_license_free (info->license);
609 /* EvDocumentLicense */
610 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
613 ev_document_license_new (void)
615 return g_new0 (EvDocumentLicense, 1);
619 ev_document_license_copy (EvDocumentLicense *license)
621 EvDocumentLicense *new_license;
626 new_license = ev_document_license_new ();
629 new_license->text = g_strdup (license->text);
631 new_license->uri = g_strdup (license->uri);
632 if (license->web_statement)
633 new_license->web_statement = g_strdup (license->web_statement);
639 ev_document_license_free (EvDocumentLicense *license)
644 g_free (license->text);
645 g_free (license->uri);
646 g_free (license->web_statement);
652 ev_document_license_get_text (EvDocumentLicense *license)
654 return license->text;
658 ev_document_license_get_uri (EvDocumentLicense *license)
664 ev_document_license_get_web_statement (EvDocumentLicense *license)
666 return license->web_statement;
670 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
673 ev_rectangle_new (void)
675 return g_new0 (EvRectangle, 1);
679 ev_rectangle_copy (EvRectangle *rectangle)
681 EvRectangle *new_rectangle;
683 g_return_val_if_fail (rectangle != NULL, NULL);
685 new_rectangle = g_new (EvRectangle, 1);
686 *new_rectangle = *rectangle;
688 return new_rectangle;
692 ev_rectangle_free (EvRectangle *rectangle)
697 /* Compares two rects. returns 0 if they're equal */
698 #define EPSILON 0.0000001
701 ev_rect_cmp (EvRectangle *a,
706 if (a == NULL || b == NULL)
709 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
710 (ABS (a->y1 - b->y1) < EPSILON) &&
711 (ABS (a->x2 - b->x2) < EPSILON) &&
712 (ABS (a->y2 - b->y2) < EPSILON));