]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
[libdocument] Add EvDocumentText interface
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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         gchar          *uri;
40
41         gint            n_pages;
42
43         gboolean        uniform;
44         gdouble         uniform_width;
45         gdouble         uniform_height;
46
47         gdouble         max_width;
48         gdouble         max_height;
49         gdouble         min_width;
50         gdouble         min_height;
51         gint            max_label;
52
53         gchar         **page_labels;
54         EvPageSize     *page_sizes;
55         EvDocumentInfo *info;
56 };
57
58 static gint            _ev_document_get_n_pages   (EvDocument  *document);
59 static void            _ev_document_get_page_size (EvDocument *document,
60                                                    EvPage     *page,
61                                                    double     *width,
62                                                    double     *height);
63 static gchar          *_ev_document_get_page_label (EvDocument *document,
64                                                     EvPage     *page);
65 static EvDocumentInfo *_ev_document_get_info       (EvDocument *document);
66
67 GMutex *ev_doc_mutex = NULL;
68 GMutex *ev_fc_mutex = NULL;
69
70 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
71
72 GQuark
73 ev_document_error_quark (void)
74 {
75   static GQuark q = 0;
76   if (q == 0)
77     q = g_quark_from_static_string ("ev-document-error-quark");
78
79   return q;
80 }
81
82 static EvPage *
83 ev_document_impl_get_page (EvDocument *document,
84                            gint        index)
85 {
86         return ev_page_new (index);
87 }
88
89 static EvDocumentInfo *
90 ev_document_impl_get_info (EvDocument *document)
91 {
92         return g_new0 (EvDocumentInfo, 1);
93 }
94
95 static void
96 ev_document_finalize (GObject *object)
97 {
98         EvDocument *document = EV_DOCUMENT (object);
99
100         if (document->priv->uri) {
101                 g_free (document->priv->uri);
102                 document->priv->uri = NULL;
103         }
104
105         if (document->priv->page_sizes) {
106                 g_free (document->priv->page_sizes);
107                 document->priv->page_sizes = NULL;
108         }
109
110         if (document->priv->page_labels) {
111                 gint i;
112
113                 for (i = 0; i < document->priv->n_pages; i++) {
114                         g_free (document->priv->page_labels[i]);
115                 }
116                 g_free (document->priv->page_labels);
117                 document->priv->page_labels = NULL;
118         }
119
120         if (document->priv->info) {
121                 ev_document_info_free (document->priv->info);
122                 document->priv->info = NULL;
123         }
124
125         G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
126 }
127
128 static void
129 ev_document_init (EvDocument *document)
130 {
131         document->priv = EV_DOCUMENT_GET_PRIVATE (document);
132
133         /* Assume all pages are the same size until proven otherwise */
134         document->priv->uniform = TRUE;
135 }
136
137 static void
138 ev_document_class_init (EvDocumentClass *klass)
139 {
140         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
141
142         g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
143
144         klass->get_page = ev_document_impl_get_page;
145         klass->get_info = ev_document_impl_get_info;
146         klass->get_backend_info = NULL;
147
148         g_object_class->finalize = ev_document_finalize;
149 }
150
151 GMutex *
152 ev_document_get_doc_mutex (void)
153 {
154         if (ev_doc_mutex == NULL) {
155                 ev_doc_mutex = g_mutex_new ();
156         }
157         return ev_doc_mutex;
158 }
159
160 void
161 ev_document_doc_mutex_lock (void)
162 {
163         g_mutex_lock (ev_document_get_doc_mutex ());
164 }
165
166 void
167 ev_document_doc_mutex_unlock (void)
168 {
169         g_mutex_unlock (ev_document_get_doc_mutex ());
170 }
171
172 gboolean
173 ev_document_doc_mutex_trylock (void)
174 {
175         return g_mutex_trylock (ev_document_get_doc_mutex ());
176 }
177
178 GMutex *
179 ev_document_get_fc_mutex (void)
180 {
181         if (ev_fc_mutex == NULL) {
182                 ev_fc_mutex = g_mutex_new ();
183         }
184         return ev_fc_mutex;
185 }
186
187 void
188 ev_document_fc_mutex_lock (void)
189 {
190         g_mutex_lock (ev_document_get_fc_mutex ());
191 }
192
193 void
194 ev_document_fc_mutex_unlock (void)
195 {
196         g_mutex_unlock (ev_document_get_fc_mutex ());
197 }
198
199 gboolean
200 ev_document_fc_mutex_trylock (void)
201 {
202         return g_mutex_trylock (ev_document_get_fc_mutex ());
203 }
204
205 /**
206  * ev_document_load:
207  * @document: a #EvDocument
208  * @uri: the document's URI
209  * @error: a #GError location to store an error, or %NULL
210  *
211  * Loads @document from @uri.
212  * 
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
218  * #GConvertError.
219  *
220  * Returns: %TRUE on success, or %FALSE on failure.
221  */
222 gboolean
223 ev_document_load (EvDocument  *document,
224                   const char  *uri,
225                   GError     **error)
226 {
227         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
228         gboolean retval;
229         GError *err = NULL;
230
231         retval = klass->load (document, uri, &err);
232         if (!retval) {
233                 if (err) {
234                         g_propagate_error (error, err);
235                 } else {
236                         g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
237                                    G_OBJECT_TYPE_NAME (document));
238
239                         /* So upper layers don't crash */
240                         g_set_error_literal (error,
241                                              EV_DOCUMENT_ERROR,
242                                              EV_DOCUMENT_ERROR_INVALID,
243                                              "Internal error in backend");
244                 }
245         } else {
246                 gint i;
247                 EvDocumentPrivate *priv = document->priv;
248
249                 /* Cache some info about the document to avoid
250                  * going to the backends since it requires locks
251                  */
252                 priv->uri = g_strdup (uri);
253                 priv->n_pages = _ev_document_get_n_pages (document);
254
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;
260                         gchar      *page_label;
261
262                         _ev_document_get_page_size (document, page, &page_width, &page_height);
263
264                         if (i == 0) {
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. */
275                                 int j;
276
277                                 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
278
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;
283                                 }
284                                 priv->uniform = FALSE;
285                         }
286                         if (!priv->uniform) {
287                                 page_size = &(priv->page_sizes[i]);
288
289                                 page_size->width = page_width;
290                                 page_size->height = page_height;
291
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;
296
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;
301                         }
302
303                         page_label = _ev_document_get_page_label (document, page);
304                         if (page_label) {
305                                 if (!priv->page_labels)
306                                         priv->page_labels = g_new0 (gchar *, priv->n_pages);
307
308                                 priv->page_labels[i] = page_label;
309                                 priv->max_label = MAX (priv->max_label,
310                                                        g_utf8_strlen (page_label, 256));
311                         }
312
313                         g_object_unref (page);
314                 }
315
316                 priv->info = _ev_document_get_info (document);
317         }
318
319         return retval;
320 }
321
322 /**
323  * ev_document_save:
324  * @document:
325  * @uri: the target URI
326  * @error: a #GError location to store an error, or %NULL
327  *
328  * Saves @document to @uri.
329  * 
330  * Returns: %TRUE on success, or %FALSE on error with @error filled in
331  */
332 gboolean
333 ev_document_save (EvDocument  *document,
334                   const char  *uri,
335                   GError     **error)
336 {
337         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
338
339         return klass->save (document, uri, error);
340 }
341
342 EvPage *
343 ev_document_get_page (EvDocument *document,
344                       gint        index)
345 {
346         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
347
348         return klass->get_page (document, index);
349 }
350
351 static gint
352 _ev_document_get_n_pages (EvDocument  *document)
353 {
354         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
355
356         return klass->get_n_pages (document);
357 }
358
359 gint
360 ev_document_get_n_pages (EvDocument  *document)
361 {
362         g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
363
364         return document->priv->n_pages;
365 }
366
367 static void
368 _ev_document_get_page_size (EvDocument *document,
369                             EvPage     *page,
370                             double     *width,
371                             double     *height)
372 {
373         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
374
375         klass->get_page_size (document, page, width, height);
376 }
377
378 void
379 ev_document_get_page_size (EvDocument *document,
380                            gint        page_index,
381                            double     *width,
382                            double     *height)
383 {
384         g_return_if_fail (EV_IS_DOCUMENT (document));
385         g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
386
387         if (width)
388                 *width = document->priv->uniform ?
389                         document->priv->uniform_width :
390                         document->priv->page_sizes[page_index].width;
391         if (height)
392                 *height = document->priv->uniform ?
393                         document->priv->uniform_height :
394                         document->priv->page_sizes[page_index].height;
395 }
396
397 static gchar *
398 _ev_document_get_page_label (EvDocument *document,
399                              EvPage     *page)
400 {
401         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
402
403         return klass->get_page_label ?
404                 klass->get_page_label (document, page) : NULL;
405 }
406
407 gchar *
408 ev_document_get_page_label (EvDocument *document,
409                             gint        page_index)
410 {
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);
413
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);
417 }
418
419 static EvDocumentInfo *
420 _ev_document_get_info (EvDocument *document)
421 {
422         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
423
424         return klass->get_info (document);
425 }
426
427 EvDocumentInfo *
428 ev_document_get_info (EvDocument *document)
429 {
430         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
431
432         return document->priv->info;
433 }
434
435 gboolean
436 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
437 {
438         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
439
440         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
441         if (klass->get_backend_info == NULL)
442                 return FALSE;
443
444         return klass->get_backend_info (document, info);
445 }
446
447 cairo_surface_t *
448 ev_document_render (EvDocument      *document,
449                     EvRenderContext *rc)
450 {
451         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
452
453         return klass->render (document, rc);
454 }
455
456 const gchar *
457 ev_document_get_uri (EvDocument *document)
458 {
459         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
460
461         return document->priv->uri;
462 }
463
464 const gchar *
465 ev_document_get_title (EvDocument *document)
466 {
467         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
468
469         return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
470                 document->priv->info->title : NULL;
471 }
472
473 gboolean
474 ev_document_is_page_size_uniform (EvDocument *document)
475 {
476         g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
477
478         return document->priv->uniform;
479 }
480
481 void
482 ev_document_get_max_page_size (EvDocument *document,
483                                gdouble    *width,
484                                gdouble    *height)
485 {
486         g_return_if_fail (EV_IS_DOCUMENT (document));
487
488         if (width)
489                 *width = document->priv->max_width;
490         if (height)
491                 *height = document->priv->max_height;
492 }
493
494 void
495 ev_document_get_min_page_size (EvDocument *document,
496                                gdouble    *width,
497                                gdouble    *height)
498 {
499         g_return_if_fail (EV_IS_DOCUMENT (document));
500
501         if (width)
502                 *width = document->priv->min_width;
503         if (height)
504                 *height = document->priv->min_height;
505 }
506
507 gboolean
508 ev_document_check_dimensions (EvDocument *document)
509 {
510         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
511
512         return (document->priv->max_width > 0 && document->priv->max_height > 0);
513 }
514
515 gint
516 ev_document_get_max_label_len (EvDocument *document)
517 {
518         g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
519
520         return document->priv->max_label;
521 }
522
523 gboolean
524 ev_document_has_text_page_labels (EvDocument *document)
525 {
526         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
527
528         return document->priv->page_labels != NULL;
529 }
530
531 gboolean
532 ev_document_find_page_by_label (EvDocument  *document,
533                                 const gchar *page_label,
534                                 gint        *page_index)
535 {
536         gint i, page;
537         glong value;
538         gchar *endptr = NULL;
539         EvDocumentPrivate *priv = document->priv;
540
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);
544
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])) {
549                         *page_index = i;
550                         return TRUE;
551                 }
552         }
553
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])) {
558                         *page_index = i;
559                         return TRUE;
560                 }
561         }
562
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);
568
569                 /* convert from a page label to a page offset */
570                 page --;
571                 if (page >= 0 && page < priv->n_pages) {
572                         *page_index = page;
573                         return TRUE;
574                 }
575         }
576
577         return FALSE;
578 }
579
580 /* EvDocumentInfo */
581 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
582
583 EvDocumentInfo *
584 ev_document_info_copy (EvDocumentInfo *info)
585 {
586         EvDocumentInfo *copy;
587         
588         g_return_val_if_fail (info != NULL, NULL);
589
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);
600         
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);
609
610         copy->fields_mask = info->fields_mask;
611
612         return copy;
613 }
614
615 void
616 ev_document_info_free (EvDocumentInfo *info)
617 {
618         if (info == NULL)
619                 return;
620
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);
631
632         g_free (info);
633 }
634
635 /* EvDocumentLicense */
636 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
637
638 EvDocumentLicense *
639 ev_document_license_new (void)
640 {
641         return g_new0 (EvDocumentLicense, 1);
642 }
643
644 EvDocumentLicense *
645 ev_document_license_copy (EvDocumentLicense *license)
646 {
647         EvDocumentLicense *new_license;
648
649         if (!license)
650                 return NULL;
651
652         new_license = ev_document_license_new ();
653
654         if (license->text)
655                 new_license->text = g_strdup (license->text);
656         if (license->uri)
657                 new_license->uri = g_strdup (license->uri);
658         if (license->web_statement)
659                 new_license->web_statement = g_strdup (license->web_statement);
660
661         return new_license;
662 }
663
664 void
665 ev_document_license_free (EvDocumentLicense *license)
666 {
667         if (!license)
668                 return;
669
670         g_free (license->text);
671         g_free (license->uri);
672         g_free (license->web_statement);
673
674         g_free (license);
675 }
676
677 const gchar *
678 ev_document_license_get_text (EvDocumentLicense *license)
679 {
680         return license->text;
681 }
682
683 const gchar *
684 ev_document_license_get_uri (EvDocumentLicense *license)
685 {
686         return license->uri;
687 }
688
689 const gchar *
690 ev_document_license_get_web_statement (EvDocumentLicense *license)
691 {
692         return license->web_statement;
693 }
694
695 /* EvRectangle */
696 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
697
698 EvRectangle *
699 ev_rectangle_new (void)
700 {
701         return g_new0 (EvRectangle, 1);
702 }
703
704 EvRectangle *
705 ev_rectangle_copy (EvRectangle *rectangle)
706 {
707         EvRectangle *new_rectangle;
708
709         g_return_val_if_fail (rectangle != NULL, NULL);
710
711         new_rectangle = g_new (EvRectangle, 1);
712         *new_rectangle = *rectangle;
713
714         return new_rectangle;
715 }
716
717 void
718 ev_rectangle_free (EvRectangle *rectangle)
719 {
720         g_free (rectangle);
721 }
722
723 /* Compares two rects.  returns 0 if they're equal */
724 #define EPSILON 0.0000001
725
726 gint
727 ev_rect_cmp (EvRectangle *a,
728              EvRectangle *b)
729 {
730         if (a == b)
731                 return 0;
732         if (a == NULL || b == NULL)
733                 return 1;
734
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));
739 }