]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
a74a0e397c79f9932089a2964b16838204cd3605
[evince.git] / libdocument / ev-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2009 Carlos Garcia Campos
4  *  Copyright (C) 2004 Marco Pesenti Gritti
5  *
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)
9  *  any later version.
10  *
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.
15  *
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.
19  *
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "ev-document.h"
28
29 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
30
31 typedef struct _EvPageSize
32 {
33         gdouble width;
34         gdouble height;
35 } EvPageSize;
36
37 struct _EvDocumentPrivate
38 {
39         gint            n_pages;
40
41         gboolean        uniform;
42         gdouble         uniform_width;
43         gdouble         uniform_height;
44
45         gdouble         max_width;
46         gdouble         max_height;
47         gint            max_label;
48
49         gchar         **page_labels;
50         EvPageSize     *page_sizes;
51         EvDocumentInfo *info;
52 };
53
54 static gint            _ev_document_get_n_pages   (EvDocument  *document);
55 static void            _ev_document_get_page_size (EvDocument *document,
56                                                    EvPage     *page,
57                                                    double     *width,
58                                                    double     *height);
59 static gchar          *_ev_document_get_page_label (EvDocument *document,
60                                                     EvPage     *page);
61 static EvDocumentInfo *_ev_document_get_info       (EvDocument *document);
62
63 GMutex *ev_doc_mutex = NULL;
64 GMutex *ev_fc_mutex = NULL;
65
66 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
67
68 GQuark
69 ev_document_error_quark (void)
70 {
71   static GQuark q = 0;
72   if (q == 0)
73     q = g_quark_from_static_string ("ev-document-error-quark");
74
75   return q;
76 }
77
78 static EvPage *
79 ev_document_impl_get_page (EvDocument *document,
80                            gint        index)
81 {
82         return ev_page_new (index);
83 }
84
85 static EvDocumentInfo *
86 ev_document_impl_get_info (EvDocument *document)
87 {
88         return g_new0 (EvDocumentInfo, 1);
89 }
90
91 static void
92 ev_document_finalize (GObject *object)
93 {
94         EvDocument *document = EV_DOCUMENT (object);
95
96         if (document->priv->page_sizes) {
97                 g_free (document->priv->page_sizes);
98                 document->priv->page_sizes = NULL;
99         }
100
101         if (document->priv->page_labels) {
102                 gint i;
103
104                 for (i = 0; i < document->priv->n_pages; i++) {
105                         g_free (document->priv->page_labels[i]);
106                 }
107                 g_free (document->priv->page_labels);
108                 document->priv->page_labels = NULL;
109         }
110
111         if (document->priv->info) {
112                 ev_document_info_free (document->priv->info);
113                 document->priv->info = NULL;
114         }
115
116         G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
117 }
118
119 static void
120 ev_document_init (EvDocument *document)
121 {
122         document->priv = EV_DOCUMENT_GET_PRIVATE (document);
123
124         /* Assume all pages are the same size until proven otherwise */
125         document->priv->uniform = TRUE;
126 }
127
128 static void
129 ev_document_class_init (EvDocumentClass *klass)
130 {
131         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
132
133         g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
134
135         klass->get_page = ev_document_impl_get_page;
136         klass->get_info = ev_document_impl_get_info;
137
138         g_object_class->finalize = ev_document_finalize;
139 }
140
141 GMutex *
142 ev_document_get_doc_mutex (void)
143 {
144         if (ev_doc_mutex == NULL) {
145                 ev_doc_mutex = g_mutex_new ();
146         }
147         return ev_doc_mutex;
148 }
149
150 void
151 ev_document_doc_mutex_lock (void)
152 {
153         g_mutex_lock (ev_document_get_doc_mutex ());
154 }
155
156 void
157 ev_document_doc_mutex_unlock (void)
158 {
159         g_mutex_unlock (ev_document_get_doc_mutex ());
160 }
161
162 gboolean
163 ev_document_doc_mutex_trylock (void)
164 {
165         return g_mutex_trylock (ev_document_get_doc_mutex ());
166 }
167
168 GMutex *
169 ev_document_get_fc_mutex (void)
170 {
171         if (ev_fc_mutex == NULL) {
172                 ev_fc_mutex = g_mutex_new ();
173         }
174         return ev_fc_mutex;
175 }
176
177 void
178 ev_document_fc_mutex_lock (void)
179 {
180         g_mutex_lock (ev_document_get_fc_mutex ());
181 }
182
183 void
184 ev_document_fc_mutex_unlock (void)
185 {
186         g_mutex_unlock (ev_document_get_fc_mutex ());
187 }
188
189 gboolean
190 ev_document_fc_mutex_trylock (void)
191 {
192         return g_mutex_trylock (ev_document_get_fc_mutex ());
193 }
194
195 /**
196  * ev_document_load:
197  * @document: a #EvDocument
198  * @uri: the document's URI
199  * @error: a #GError location to store an error, or %NULL
200  *
201  * Loads @document from @uri.
202  * 
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
208  * #GConvertError.
209  *
210  * Returns: %TRUE on success, or %FALSE on failure.
211  */
212 gboolean
213 ev_document_load (EvDocument  *document,
214                   const char  *uri,
215                   GError     **error)
216 {
217         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
218         gboolean retval;
219         GError *err = NULL;
220
221         retval = klass->load (document, uri, &err);
222         if (!retval) {
223                 if (err) {
224                         g_propagate_error (error, err);
225                 } else {
226                         g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
227                                    G_OBJECT_TYPE_NAME (document));
228
229                         /* So upper layers don't crash */
230                         g_set_error_literal (error,
231                                              EV_DOCUMENT_ERROR,
232                                              EV_DOCUMENT_ERROR_INVALID,
233                                              "Internal error in backend");
234                 }
235         } else {
236                 gint i;
237                 EvDocumentPrivate *priv = document->priv;
238
239                 /* Cache some info about the document to avoid
240                  * going to the backends since it requires locks
241                  */
242                 priv->n_pages = _ev_document_get_n_pages (document);
243                 priv->info = _ev_document_get_info (document);
244
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;
250                         gchar      *page_label;
251
252                         _ev_document_get_page_size (document, page, &page_width, &page_height);
253
254                         if (i == 0) {
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. */
261                                 int j;
262
263                                 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
264
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;
269                                 }
270                                 priv->uniform = FALSE;
271                         }
272                         if (!priv->uniform) {
273                                 page_size = &(priv->page_sizes[i]);
274
275                                 page_size->width = page_width;
276                                 page_size->height = page_height;
277
278                                 if (page_width > priv->max_width)
279                                         priv->max_width = page_width;
280
281                                 if (page_height > priv->max_height)
282                                         priv->max_height = page_height;
283                         }
284
285                         page_label = _ev_document_get_page_label (document, page);
286                         if (page_label) {
287                                 if (priv->page_labels) {
288                                         priv->page_labels[i] = page_label;
289                                 } else {
290                                         gchar *numeric_label;
291
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;
296                                         }
297                                         g_free (numeric_label);
298                                 }
299                                 priv->max_label = MAX (priv->max_label,
300                                                        g_utf8_strlen (page_label, 256));
301                         }
302
303                         g_object_unref (page);
304                 }
305
306                 if (priv->uniform) {
307                         priv->max_width = priv->uniform_width;
308                         priv->max_height = priv->uniform_height;
309                 }
310         }
311
312         return retval;
313 }
314
315 /**
316  * ev_document_save:
317  * @document:
318  * @uri: the target URI
319  * @error: a #GError location to store an error, or %NULL
320  *
321  * Saves @document to @uri.
322  * 
323  * Returns: %TRUE on success, or %FALSE on error with @error filled in
324  */
325 gboolean
326 ev_document_save (EvDocument  *document,
327                   const char  *uri,
328                   GError     **error)
329 {
330         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
331
332         return klass->save (document, uri, error);
333 }
334
335 EvPage *
336 ev_document_get_page (EvDocument *document,
337                       gint        index)
338 {
339         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
340
341         return klass->get_page (document, index);
342 }
343
344 static gint
345 _ev_document_get_n_pages (EvDocument  *document)
346 {
347         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
348
349         return klass->get_n_pages (document);
350 }
351
352 gint
353 ev_document_get_n_pages (EvDocument  *document)
354 {
355         return document->priv->n_pages;
356 }
357
358 static void
359 _ev_document_get_page_size (EvDocument *document,
360                             EvPage     *page,
361                             double     *width,
362                             double     *height)
363 {
364         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
365
366         klass->get_page_size (document, page, width, height);
367 }
368
369 void
370 ev_document_get_page_size (EvDocument *document,
371                            gint        page_index,
372                            double     *width,
373                            double     *height)
374 {
375         if (width)
376                 *width = document->priv->uniform ?
377                         document->priv->uniform_width :
378                         document->priv->page_sizes[page_index].width;
379         if (height)
380                 *height = document->priv->uniform ?
381                         document->priv->uniform_height :
382                         document->priv->page_sizes[page_index].height;
383 }
384
385 static gchar *
386 _ev_document_get_page_label (EvDocument *document,
387                              EvPage     *page)
388 {
389         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
390
391         return klass->get_page_label ?
392                 klass->get_page_label (document, page) : NULL;
393 }
394
395 gchar *
396 ev_document_get_page_label (EvDocument *document,
397                             gint        page_index)
398 {
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);
402 }
403
404 static EvDocumentInfo *
405 _ev_document_get_info (EvDocument *document)
406 {
407         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
408
409         return klass->get_info (document);
410 }
411
412 EvDocumentInfo *
413 ev_document_get_info (EvDocument *document)
414 {
415         return document->priv->info;
416 }
417
418 cairo_surface_t *
419 ev_document_render (EvDocument      *document,
420                     EvRenderContext *rc)
421 {
422         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
423
424         return klass->render (document, rc);
425 }
426
427 const gchar *
428 ev_document_get_title (EvDocument *document)
429 {
430         return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
431                 document->priv->info->title : NULL;
432 }
433
434 gboolean
435 ev_document_is_page_size_uniform (EvDocument *document)
436 {
437         return document->priv->uniform;
438 }
439
440 void
441 ev_document_get_max_page_size (EvDocument *document,
442                                gdouble    *width,
443                                gdouble    *height)
444 {
445         if (width)
446                 *width = document->priv->max_width;
447         if (height)
448                 *height = document->priv->max_height;
449 }
450
451 gint
452 ev_document_get_max_label_len (EvDocument *document)
453 {
454         return document->priv->max_label;
455 }
456
457 gboolean
458 ev_document_has_text_page_labels (EvDocument *document)
459 {
460         return document->priv->page_labels != NULL;
461 }
462
463 gboolean
464 ev_document_find_page_by_label (EvDocument  *document,
465                                 const gchar *page_label,
466                                 gint        *page_index)
467 {
468         gint i, page;
469         glong value;
470         gchar *endptr = NULL;
471         EvDocumentPrivate *priv = document->priv;
472
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])) {
477                         *page_index = i;
478                         return TRUE;
479                 }
480         }
481
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])) {
486                         *page_index = i;
487                         return TRUE;
488                 }
489         }
490
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);
496
497                 /* convert from a page label to a page offset */
498                 page --;
499                 if (page >= 0 && page < priv->n_pages) {
500                         *page_index = page;
501                         return TRUE;
502                 }
503         }
504
505         return FALSE;
506 }
507
508 /* EvDocumentInfo */
509 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
510
511 EvDocumentInfo *
512 ev_document_info_copy (EvDocumentInfo *info)
513 {
514         EvDocumentInfo *copy;
515         
516         g_return_val_if_fail (info != NULL, NULL);
517
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);
528         
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;
537
538         return copy;
539 }
540
541 void
542 ev_document_info_free (EvDocumentInfo *info)
543 {
544         if (info == NULL)
545                 return;
546
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);
556         
557         g_free (info);
558 }
559
560 /* EvRectangle */
561 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
562
563 EvRectangle *
564 ev_rectangle_new (void)
565 {
566         return g_new0 (EvRectangle, 1);
567 }
568
569 EvRectangle *
570 ev_rectangle_copy (EvRectangle *rectangle)
571 {
572         EvRectangle *new_rectangle;
573
574         g_return_val_if_fail (rectangle != NULL, NULL);
575
576         new_rectangle = g_new (EvRectangle, 1);
577         *new_rectangle = *rectangle;
578
579         return new_rectangle;
580 }
581
582 void
583 ev_rectangle_free (EvRectangle *rectangle)
584 {
585         g_free (rectangle);
586 }
587
588 /* Compares two rects.  returns 0 if they're equal */
589 #define EPSILON 0.0000001
590
591 gint
592 ev_rect_cmp (EvRectangle *a,
593              EvRectangle *b)
594 {
595         if (a == b)
596                 return 0;
597         if (a == NULL || b == NULL)
598                 return 1;
599
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));
604 }