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
42 gdouble uniform_width;
43 gdouble uniform_height;
50 EvPageSize *page_sizes;
54 static gint _ev_document_get_n_pages (EvDocument *document);
55 static void _ev_document_get_page_size (EvDocument *document,
59 static gchar *_ev_document_get_page_label (EvDocument *document,
61 static EvDocumentInfo *_ev_document_get_info (EvDocument *document);
63 GMutex *ev_doc_mutex = NULL;
64 GMutex *ev_fc_mutex = NULL;
66 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
69 ev_document_error_quark (void)
73 q = g_quark_from_static_string ("ev-document-error-quark");
79 ev_document_impl_get_page (EvDocument *document,
82 return ev_page_new (index);
85 static EvDocumentInfo *
86 ev_document_impl_get_info (EvDocument *document)
88 return g_new0 (EvDocumentInfo, 1);
92 ev_document_finalize (GObject *object)
94 EvDocument *document = EV_DOCUMENT (object);
96 if (document->priv->page_sizes) {
97 g_free (document->priv->page_sizes);
98 document->priv->page_sizes = NULL;
101 if (document->priv->page_labels) {
104 for (i = 0; i < document->priv->n_pages; i++) {
105 g_free (document->priv->page_labels[i]);
107 g_free (document->priv->page_labels);
108 document->priv->page_labels = NULL;
111 if (document->priv->info) {
112 ev_document_info_free (document->priv->info);
113 document->priv->info = NULL;
116 G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
120 ev_document_init (EvDocument *document)
122 document->priv = EV_DOCUMENT_GET_PRIVATE (document);
124 /* Assume all pages are the same size until proven otherwise */
125 document->priv->uniform = TRUE;
129 ev_document_class_init (EvDocumentClass *klass)
131 GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
133 g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
135 klass->get_page = ev_document_impl_get_page;
136 klass->get_info = ev_document_impl_get_info;
138 g_object_class->finalize = ev_document_finalize;
142 ev_document_get_doc_mutex (void)
144 if (ev_doc_mutex == NULL) {
145 ev_doc_mutex = g_mutex_new ();
151 ev_document_doc_mutex_lock (void)
153 g_mutex_lock (ev_document_get_doc_mutex ());
157 ev_document_doc_mutex_unlock (void)
159 g_mutex_unlock (ev_document_get_doc_mutex ());
163 ev_document_doc_mutex_trylock (void)
165 return g_mutex_trylock (ev_document_get_doc_mutex ());
169 ev_document_get_fc_mutex (void)
171 if (ev_fc_mutex == NULL) {
172 ev_fc_mutex = g_mutex_new ();
178 ev_document_fc_mutex_lock (void)
180 g_mutex_lock (ev_document_get_fc_mutex ());
184 ev_document_fc_mutex_unlock (void)
186 g_mutex_unlock (ev_document_get_fc_mutex ());
190 ev_document_fc_mutex_trylock (void)
192 return g_mutex_trylock (ev_document_get_fc_mutex ());
197 * @document: a #EvDocument
198 * @uri: the document's URI
199 * @error: a #GError location to store an error, or %NULL
201 * Loads @document from @uri.
203 * On failure, %FALSE is returned and @error is filled in.
204 * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
205 * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
206 * is returned. Other errors are possible too, depending on the backend
207 * used to load the document and the URI, e.g. #GIOError, #GFileError, and
210 * Returns: %TRUE on success, or %FALSE on failure.
213 ev_document_load (EvDocument *document,
217 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
221 retval = klass->load (document, uri, &err);
224 g_propagate_error (error, err);
226 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
227 G_OBJECT_TYPE_NAME (document));
229 /* So upper layers don't crash */
230 g_set_error_literal (error,
232 EV_DOCUMENT_ERROR_INVALID,
233 "Internal error in backend");
237 EvDocumentPrivate *priv = document->priv;
239 /* Cache some info about the document to avoid
240 * going to the backends since it requires locks
242 priv->n_pages = _ev_document_get_n_pages (document);
243 priv->info = _ev_document_get_info (document);
245 for (i = 0; i < priv->n_pages; i++) {
246 EvPage *page = ev_document_get_page (document, i);
247 gdouble page_width = 0;
248 gdouble page_height = 0;
249 EvPageSize *page_size;
252 _ev_document_get_page_size (document, page, &page_width, &page_height);
255 priv->uniform_width = page_width;
256 priv->uniform_height = page_height;
257 } else if (priv->uniform &&
258 (priv->uniform_width != page_width ||
259 priv->uniform_height != page_height)) {
260 /* It's a different page size. Backfill the array. */
263 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
265 for (j = 0; j < i; j++) {
266 page_size = &(priv->page_sizes[j]);
267 page_size->width = priv->uniform_width;
268 page_size->height = priv->uniform_height;
270 priv->uniform = FALSE;
272 if (!priv->uniform) {
273 page_size = &(priv->page_sizes[i]);
275 page_size->width = page_width;
276 page_size->height = page_height;
278 if (page_width > priv->max_width)
279 priv->max_width = page_width;
281 if (page_height > priv->max_height)
282 priv->max_height = page_height;
285 page_label = _ev_document_get_page_label (document, page);
287 if (priv->page_labels) {
288 priv->page_labels[i] = page_label;
290 gchar *numeric_label;
292 numeric_label = g_strdup_printf ("%d", i + 1);
293 if (strcmp (numeric_label, page_label) != 0) {
294 priv->page_labels = g_new0 (gchar *, priv->n_pages);
295 priv->page_labels[i] = page_label;
297 g_free (numeric_label);
299 priv->max_label = MAX (priv->max_label,
300 g_utf8_strlen (page_label, 256));
303 g_object_unref (page);
307 priv->max_width = priv->uniform_width;
308 priv->max_height = priv->uniform_height;
318 * @uri: the target URI
319 * @error: a #GError location to store an error, or %NULL
321 * Saves @document to @uri.
323 * Returns: %TRUE on success, or %FALSE on error with @error filled in
326 ev_document_save (EvDocument *document,
330 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
332 return klass->save (document, uri, error);
336 ev_document_get_page (EvDocument *document,
339 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
341 return klass->get_page (document, index);
345 _ev_document_get_n_pages (EvDocument *document)
347 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
349 return klass->get_n_pages (document);
353 ev_document_get_n_pages (EvDocument *document)
355 g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
357 return document->priv->n_pages;
361 _ev_document_get_page_size (EvDocument *document,
366 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
368 klass->get_page_size (document, page, width, height);
372 ev_document_get_page_size (EvDocument *document,
377 g_return_if_fail (EV_IS_DOCUMENT (document));
378 g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
381 *width = document->priv->uniform ?
382 document->priv->uniform_width :
383 document->priv->page_sizes[page_index].width;
385 *height = document->priv->uniform ?
386 document->priv->uniform_height :
387 document->priv->page_sizes[page_index].height;
391 _ev_document_get_page_label (EvDocument *document,
394 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
396 return klass->get_page_label ?
397 klass->get_page_label (document, page) : NULL;
401 ev_document_get_page_label (EvDocument *document,
404 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
405 g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
407 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
408 g_strdup (document->priv->page_labels[page_index]) :
409 g_strdup_printf ("%d", page_index + 1);
412 static EvDocumentInfo *
413 _ev_document_get_info (EvDocument *document)
415 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
417 return klass->get_info (document);
421 ev_document_get_info (EvDocument *document)
423 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
425 return document->priv->info;
429 ev_document_render (EvDocument *document,
432 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
434 return klass->render (document, rc);
438 ev_document_get_title (EvDocument *document)
440 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
442 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
443 document->priv->info->title : NULL;
447 ev_document_is_page_size_uniform (EvDocument *document)
449 g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
451 return document->priv->uniform;
455 ev_document_get_max_page_size (EvDocument *document,
459 g_return_if_fail (EV_IS_DOCUMENT (document));
462 *width = document->priv->max_width;
464 *height = document->priv->max_height;
468 ev_document_get_max_label_len (EvDocument *document)
470 g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
472 return document->priv->max_label;
476 ev_document_has_text_page_labels (EvDocument *document)
478 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
480 return document->priv->page_labels != NULL;
484 ev_document_find_page_by_label (EvDocument *document,
485 const gchar *page_label,
490 gchar *endptr = NULL;
491 EvDocumentPrivate *priv = document->priv;
493 g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
494 g_return_val_if_fail (page_label != NULL, FALSE);
495 g_return_val_if_fail (page_index != NULL, FALSE);
497 /* First, look for a literal label match */
498 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
499 if (priv->page_labels[i] != NULL &&
500 ! strcmp (page_label, priv->page_labels[i])) {
506 /* Second, look for a match with case insensitively */
507 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
508 if (priv->page_labels[i] != NULL &&
509 ! strcasecmp (page_label, priv->page_labels[i])) {
515 /* Next, parse the label, and see if the number fits */
516 value = strtol (page_label, &endptr, 10);
517 if (endptr[0] == '\0') {
518 /* Page number is an integer */
519 page = MIN (G_MAXINT, value);
521 /* convert from a page label to a page offset */
523 if (page >= 0 && page < priv->n_pages) {
533 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
536 ev_document_info_copy (EvDocumentInfo *info)
538 EvDocumentInfo *copy;
540 g_return_val_if_fail (info != NULL, NULL);
542 copy = g_new0 (EvDocumentInfo, 1);
543 copy->title = g_strdup (info->title);
544 copy->format = g_strdup (info->format);
545 copy->author = g_strdup (info->author);
546 copy->subject = g_strdup (info->subject);
547 copy->keywords = g_strdup (info->keywords);
548 copy->security = g_strdup (info->security);
549 copy->creator = g_strdup (info->creator);
550 copy->producer = g_strdup (info->producer);
551 copy->linearized = g_strdup (info->linearized);
553 copy->creation_date = info->creation_date;
554 copy->modified_date = info->modified_date;
555 copy->layout = info->layout;
556 copy->mode = info->mode;
557 copy->ui_hints = info->ui_hints;
558 copy->permissions = info->permissions;
559 copy->n_pages = info->n_pages;
560 copy->fields_mask = info->fields_mask;
566 ev_document_info_free (EvDocumentInfo *info)
571 g_free (info->title);
572 g_free (info->format);
573 g_free (info->author);
574 g_free (info->subject);
575 g_free (info->keywords);
576 g_free (info->creator);
577 g_free (info->producer);
578 g_free (info->linearized);
579 g_free (info->security);
585 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
588 ev_rectangle_new (void)
590 return g_new0 (EvRectangle, 1);
594 ev_rectangle_copy (EvRectangle *rectangle)
596 EvRectangle *new_rectangle;
598 g_return_val_if_fail (rectangle != NULL, NULL);
600 new_rectangle = g_new (EvRectangle, 1);
601 *new_rectangle = *rectangle;
603 return new_rectangle;
607 ev_rectangle_free (EvRectangle *rectangle)
612 /* Compares two rects. returns 0 if they're equal */
613 #define EPSILON 0.0000001
616 ev_rect_cmp (EvRectangle *a,
621 if (a == NULL || b == NULL)
624 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
625 (ABS (a->y1 - b->y1) < EPSILON) &&
626 (ABS (a->x2 - b->x2) < EPSILON) &&
627 (ABS (a->y2 - b->y2) < EPSILON));