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 return document->priv->n_pages;
359 _ev_document_get_page_size (EvDocument *document,
364 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
366 klass->get_page_size (document, page, width, height);
370 ev_document_get_page_size (EvDocument *document,
376 *width = document->priv->uniform ?
377 document->priv->uniform_width :
378 document->priv->page_sizes[page_index].width;
380 *height = document->priv->uniform ?
381 document->priv->uniform_height :
382 document->priv->page_sizes[page_index].height;
386 _ev_document_get_page_label (EvDocument *document,
389 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
391 return klass->get_page_label ?
392 klass->get_page_label (document, page) : NULL;
396 ev_document_get_page_label (EvDocument *document,
399 return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
400 g_strdup (document->priv->page_labels[page_index]) :
401 g_strdup_printf ("%d", page_index + 1);
404 static EvDocumentInfo *
405 _ev_document_get_info (EvDocument *document)
407 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
409 return klass->get_info (document);
413 ev_document_get_info (EvDocument *document)
415 return document->priv->info;
419 ev_document_render (EvDocument *document,
422 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
424 return klass->render (document, rc);
428 ev_document_get_title (EvDocument *document)
430 return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
431 document->priv->info->title : NULL;
435 ev_document_is_page_size_uniform (EvDocument *document)
437 return document->priv->uniform;
441 ev_document_get_max_page_size (EvDocument *document,
446 *width = document->priv->max_width;
448 *height = document->priv->max_height;
452 ev_document_get_max_label_len (EvDocument *document)
454 return document->priv->max_label;
458 ev_document_has_text_page_labels (EvDocument *document)
460 return document->priv->page_labels != NULL;
464 ev_document_find_page_by_label (EvDocument *document,
465 const gchar *page_label,
470 gchar *endptr = NULL;
471 EvDocumentPrivate *priv = document->priv;
473 /* First, look for a literal label match */
474 for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
475 if (priv->page_labels[i] != NULL &&
476 ! strcmp (page_label, priv->page_labels[i])) {
482 /* Second, look for a match with case insensitively */
483 for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
484 if (priv->page_labels[i] != NULL &&
485 ! strcasecmp (page_label, priv->page_labels[i])) {
491 /* Next, parse the label, and see if the number fits */
492 value = strtol (page_label, &endptr, 10);
493 if (endptr[0] == '\0') {
494 /* Page number is an integer */
495 page = MIN (G_MAXINT, value);
497 /* convert from a page label to a page offset */
499 if (page >= 0 && page < priv->n_pages) {
509 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
512 ev_document_info_copy (EvDocumentInfo *info)
514 EvDocumentInfo *copy;
516 g_return_val_if_fail (info != NULL, NULL);
518 copy = g_new0 (EvDocumentInfo, 1);
519 copy->title = g_strdup (info->title);
520 copy->format = g_strdup (info->format);
521 copy->author = g_strdup (info->author);
522 copy->subject = g_strdup (info->subject);
523 copy->keywords = g_strdup (info->keywords);
524 copy->security = g_strdup (info->security);
525 copy->creator = g_strdup (info->creator);
526 copy->producer = g_strdup (info->producer);
527 copy->linearized = g_strdup (info->linearized);
529 copy->creation_date = info->creation_date;
530 copy->modified_date = info->modified_date;
531 copy->layout = info->layout;
532 copy->mode = info->mode;
533 copy->ui_hints = info->ui_hints;
534 copy->permissions = info->permissions;
535 copy->n_pages = info->n_pages;
536 copy->fields_mask = info->fields_mask;
542 ev_document_info_free (EvDocumentInfo *info)
547 g_free (info->title);
548 g_free (info->format);
549 g_free (info->author);
550 g_free (info->subject);
551 g_free (info->keywords);
552 g_free (info->creator);
553 g_free (info->producer);
554 g_free (info->linearized);
555 g_free (info->security);
561 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
564 ev_rectangle_new (void)
566 return g_new0 (EvRectangle, 1);
570 ev_rectangle_copy (EvRectangle *rectangle)
572 EvRectangle *new_rectangle;
574 g_return_val_if_fail (rectangle != NULL, NULL);
576 new_rectangle = g_new (EvRectangle, 1);
577 *new_rectangle = *rectangle;
579 return new_rectangle;
583 ev_rectangle_free (EvRectangle *rectangle)
588 /* Compares two rects. returns 0 if they're equal */
589 #define EPSILON 0.0000001
592 ev_rect_cmp (EvRectangle *a,
597 if (a == NULL || b == NULL)
600 return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
601 (ABS (a->y1 - b->y1) < EPSILON) &&
602 (ABS (a->x2 - b->x2) < EPSILON) &&
603 (ABS (a->y2 - b->y2) < EPSILON));