]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
[dualscreen] fix crash on ctrl+w and fix control window closing
[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 #include "ev-document-misc.h"
29 #include "synctex_parser.h"
30
31 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
32
33 typedef struct _EvPageSize
34 {
35         gdouble width;
36         gdouble height;
37 } EvPageSize;
38
39 struct _EvDocumentPrivate
40 {
41         gchar          *uri;
42
43         gint            n_pages;
44
45         gboolean        uniform;
46         gdouble         uniform_width;
47         gdouble         uniform_height;
48
49         gdouble         max_width;
50         gdouble         max_height;
51         gdouble         min_width;
52         gdouble         min_height;
53         gint            max_label;
54
55         gchar         **page_labels;
56         EvPageSize     *page_sizes;
57         EvDocumentInfo *info;
58
59         synctex_scanner_t synctex_scanner;
60 };
61
62 static gint            _ev_document_get_n_pages     (EvDocument *document);
63 static void            _ev_document_get_page_size   (EvDocument *document,
64                                                      EvPage     *page,
65                                                      double     *width,
66                                                      double     *height);
67 static gchar          *_ev_document_get_page_label  (EvDocument *document,
68                                                      EvPage     *page);
69 static EvDocumentInfo *_ev_document_get_info        (EvDocument *document);
70 static gboolean        _ev_document_support_synctex (EvDocument *document);
71
72 GMutex *ev_doc_mutex = NULL;
73 GMutex *ev_fc_mutex = NULL;
74
75 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
76
77 GQuark
78 ev_document_error_quark (void)
79 {
80   static GQuark q = 0;
81   if (q == 0)
82     q = g_quark_from_static_string ("ev-document-error-quark");
83
84   return q;
85 }
86
87 static EvPage *
88 ev_document_impl_get_page (EvDocument *document,
89                            gint        index)
90 {
91         return ev_page_new (index);
92 }
93
94 static EvDocumentInfo *
95 ev_document_impl_get_info (EvDocument *document)
96 {
97         return g_new0 (EvDocumentInfo, 1);
98 }
99
100 static void
101 ev_document_finalize (GObject *object)
102 {
103         EvDocument *document = EV_DOCUMENT (object);
104
105         if (document->priv->uri) {
106                 g_free (document->priv->uri);
107                 document->priv->uri = NULL;
108         }
109
110         if (document->priv->page_sizes) {
111                 g_free (document->priv->page_sizes);
112                 document->priv->page_sizes = NULL;
113         }
114
115         if (document->priv->page_labels) {
116                 gint i;
117
118                 for (i = 0; i < document->priv->n_pages; i++) {
119                         g_free (document->priv->page_labels[i]);
120                 }
121                 g_free (document->priv->page_labels);
122                 document->priv->page_labels = NULL;
123         }
124
125         if (document->priv->info) {
126                 ev_document_info_free (document->priv->info);
127                 document->priv->info = NULL;
128         }
129
130         if (document->priv->synctex_scanner) {
131                 synctex_scanner_free (document->priv->synctex_scanner);
132                 document->priv->synctex_scanner = NULL;
133         }
134
135         G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
136 }
137
138 static void
139 ev_document_init (EvDocument *document)
140 {
141         document->priv = EV_DOCUMENT_GET_PRIVATE (document);
142
143         /* Assume all pages are the same size until proven otherwise */
144         document->priv->uniform = TRUE;
145 }
146
147 static void
148 ev_document_class_init (EvDocumentClass *klass)
149 {
150         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
151
152         g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
153
154         klass->get_page = ev_document_impl_get_page;
155         klass->get_info = ev_document_impl_get_info;
156         klass->get_backend_info = NULL;
157
158         g_object_class->finalize = ev_document_finalize;
159 }
160
161 GMutex *
162 ev_document_get_doc_mutex (void)
163 {
164         if (ev_doc_mutex == NULL) {
165                 ev_doc_mutex = g_mutex_new ();
166         }
167         return ev_doc_mutex;
168 }
169
170 void
171 ev_document_doc_mutex_lock (void)
172 {
173         g_mutex_lock (ev_document_get_doc_mutex ());
174 }
175
176 void
177 ev_document_doc_mutex_unlock (void)
178 {
179         g_mutex_unlock (ev_document_get_doc_mutex ());
180 }
181
182 gboolean
183 ev_document_doc_mutex_trylock (void)
184 {
185         return g_mutex_trylock (ev_document_get_doc_mutex ());
186 }
187
188 GMutex *
189 ev_document_get_fc_mutex (void)
190 {
191         if (ev_fc_mutex == NULL) {
192                 ev_fc_mutex = g_mutex_new ();
193         }
194         return ev_fc_mutex;
195 }
196
197 void
198 ev_document_fc_mutex_lock (void)
199 {
200         g_mutex_lock (ev_document_get_fc_mutex ());
201 }
202
203 void
204 ev_document_fc_mutex_unlock (void)
205 {
206         g_mutex_unlock (ev_document_get_fc_mutex ());
207 }
208
209 gboolean
210 ev_document_fc_mutex_trylock (void)
211 {
212         return g_mutex_trylock (ev_document_get_fc_mutex ());
213 }
214
215 /**
216  * ev_document_load:
217  * @document: a #EvDocument
218  * @uri: the document's URI
219  * @error: a #GError location to store an error, or %NULL
220  *
221  * Loads @document from @uri.
222  * 
223  * On failure, %FALSE is returned and @error is filled in.
224  * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
225  * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
226  * is returned. Other errors are possible too, depending on the backend
227  * used to load the document and the URI, e.g. #GIOError, #GFileError, and
228  * #GConvertError.
229  *
230  * Returns: %TRUE on success, or %FALSE on failure.
231  */
232 gboolean
233 ev_document_load (EvDocument  *document,
234                   const char  *uri,
235                   GError     **error)
236 {
237         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
238         gboolean retval;
239         GError *err = NULL;
240
241         retval = klass->load (document, uri, &err);
242         if (!retval) {
243                 if (err) {
244                         g_propagate_error (error, err);
245                 } else {
246                         g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
247                                    G_OBJECT_TYPE_NAME (document));
248
249                         /* So upper layers don't crash */
250                         g_set_error_literal (error,
251                                              EV_DOCUMENT_ERROR,
252                                              EV_DOCUMENT_ERROR_INVALID,
253                                              "Internal error in backend");
254                 }
255         } else {
256                 gint i;
257                 EvDocumentPrivate *priv = document->priv;
258
259                 /* Cache some info about the document to avoid
260                  * going to the backends since it requires locks
261                  */
262                 priv->uri = g_strdup (uri);
263                 priv->n_pages = _ev_document_get_n_pages (document);
264
265                 for (i = 0; i < priv->n_pages; i++) {
266                         EvPage     *page = ev_document_get_page (document, i);
267                         gdouble     page_width = 0;
268                         gdouble     page_height = 0;
269                         EvPageSize *page_size;
270                         gchar      *page_label;
271
272                         _ev_document_get_page_size (document, page, &page_width, &page_height);
273
274                         if (i == 0) {
275                                 priv->uniform_width = page_width;
276                                 priv->uniform_height = page_height;
277                                 priv->max_width = priv->uniform_width;
278                                 priv->max_height = priv->uniform_height;
279                                 priv->min_width = priv->uniform_width;
280                                 priv->min_height = priv->uniform_height;
281                         } else if (priv->uniform &&
282                                    (priv->uniform_width != page_width ||
283                                     priv->uniform_height != page_height)) {
284                                 /* It's a different page size.  Backfill the array. */
285                                 int j;
286
287                                 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
288
289                                 for (j = 0; j < i; j++) {
290                                         page_size = &(priv->page_sizes[j]);
291                                         page_size->width = priv->uniform_width;
292                                         page_size->height = priv->uniform_height;
293                                 }
294                                 priv->uniform = FALSE;
295                         }
296                         if (!priv->uniform) {
297                                 page_size = &(priv->page_sizes[i]);
298
299                                 page_size->width = page_width;
300                                 page_size->height = page_height;
301
302                                 if (page_width > priv->max_width)
303                                         priv->max_width = page_width;
304                                 if (page_width < priv->min_width)
305                                         priv->min_width = page_width;
306
307                                 if (page_height > priv->max_height)
308                                         priv->max_height = page_height;
309                                 if (page_height < priv->min_height)
310                                         priv->min_height = page_height;
311                         }
312
313                         page_label = _ev_document_get_page_label (document, page);
314                         if (page_label) {
315                                 if (!priv->page_labels)
316                                         priv->page_labels = g_new0 (gchar *, priv->n_pages);
317
318                                 priv->page_labels[i] = page_label;
319                                 priv->max_label = MAX (priv->max_label,
320                                                        g_utf8_strlen (page_label, 256));
321                         }
322
323                         g_object_unref (page);
324                 }
325
326                 priv->info = _ev_document_get_info (document);
327                 if (_ev_document_support_synctex (document)) {
328                         gchar *filename;
329
330                         filename = g_filename_from_uri (uri, NULL, NULL);
331                         if (filename != NULL) {
332                                 priv->synctex_scanner =
333                                         synctex_scanner_new_with_output_file (filename, NULL, 1);
334                                 g_free (filename);
335                         }
336                 }
337         }
338
339         return retval;
340 }
341
342 /**
343  * ev_document_save:
344  * @document:
345  * @uri: the target URI
346  * @error: a #GError location to store an error, or %NULL
347  *
348  * Saves @document to @uri.
349  * 
350  * Returns: %TRUE on success, or %FALSE on error with @error filled in
351  */
352 gboolean
353 ev_document_save (EvDocument  *document,
354                   const char  *uri,
355                   GError     **error)
356 {
357         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
358
359         return klass->save (document, uri, error);
360 }
361
362 EvPage *
363 ev_document_get_page (EvDocument *document,
364                       gint        index)
365 {
366         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
367
368         return klass->get_page (document, index);
369 }
370
371 static gboolean
372 _ev_document_support_synctex (EvDocument *document)
373 {
374         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
375
376         return klass->support_synctex ? klass->support_synctex (document) : FALSE;
377 }
378
379 gboolean
380 ev_document_has_synctex (EvDocument *document)
381 {
382         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
383
384         return document->priv->synctex_scanner != NULL;
385 }
386
387 /**
388  * ev_document_synctex_backward_search:
389  * @document:
390  * @page: the target page
391  * @x:
392  * @y:
393  *
394  * Peforms a Synctex backward search to obtain the TeX input file, line and
395  * (possibly) column  corresponding to the  position (@x,@y) (in 72dpi
396  * coordinates) in the  @page of @document.
397  *
398  * Returns: A pointer to the EvSourceLink structure that holds the result. @NULL if synctex
399  * is not enabled for the document or no result is found.
400  * The EvSourceLink pointer should be freed with g_free after it is used.
401  */
402 EvSourceLink *
403 ev_document_synctex_backward_search (EvDocument *document,
404                                      gint        page_index,
405                                      gfloat      x,
406                                      gfloat      y)
407 {
408         EvSourceLink *result = NULL;
409         synctex_scanner_t scanner;
410
411         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
412
413         scanner = document->priv->synctex_scanner;
414         if (!scanner)
415                 return NULL;
416
417         if (synctex_edit_query (scanner, page_index + 1, x, y) > 0) {
418                 synctex_node_t node;
419
420                 /* We assume that a backward search returns either zero or one result_node */
421                 node = synctex_next_result (scanner);
422                 if (node != NULL) {
423                         const gchar *filename;
424
425                         filename = synctex_scanner_get_name (scanner, synctex_node_tag (node));
426                         
427                         if (filename) {
428                                 result = g_new (EvSourceLink, 1);
429                                 result->filename = filename;
430                                 result->line = synctex_node_line (node);
431                                 result->col = synctex_node_column (node);
432                         }
433                 }
434         }
435
436         return result;
437 }
438
439 /**
440  * ev_document_synctex_forward_search:
441  * @document:
442  * @source_link:
443  *
444  * Peforms a Synctex forward search to obtain the area in the document
445  * corresponding to the position @line and @column number in the source Tex file
446  *
447  * Returns: An EvMapping with the page number and area corresponfing to
448  * the given line in the source file. It must be free with g_free when done
449  */
450 EvMapping *
451 ev_document_synctex_forward_search (EvDocument   *document,
452                                     EvSourceLink *link)
453 {
454         EvMapping        *result = NULL;
455         synctex_scanner_t scanner;
456
457         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
458
459         scanner = document->priv->synctex_scanner;
460         if (!scanner)
461                 return NULL;
462
463         if (synctex_display_query (scanner, link->filename, link->line, link->col) > 0) {
464                 synctex_node_t node;
465                 gint           page;
466
467                 if ((node = synctex_next_result (scanner))) {
468                         result = g_new (EvMapping, 1);
469
470                         page = synctex_node_page (node) - 1;
471                         result->data = GINT_TO_POINTER (page);
472
473                         result->area.x1 = synctex_node_box_visible_h (node);
474                         result->area.y1 = synctex_node_box_visible_v (node) -
475                                 synctex_node_box_visible_height (node);
476                         result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
477                         result->area.y2 = synctex_node_box_visible_depth (node) +
478                                 synctex_node_box_visible_height (node) + result->area.y1;
479                 }
480         }
481
482         return result;
483 }
484
485 static gint
486 _ev_document_get_n_pages (EvDocument  *document)
487 {
488         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
489
490         return klass->get_n_pages (document);
491 }
492
493 gint
494 ev_document_get_n_pages (EvDocument  *document)
495 {
496         g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
497
498         return document->priv->n_pages;
499 }
500
501 static void
502 _ev_document_get_page_size (EvDocument *document,
503                             EvPage     *page,
504                             double     *width,
505                             double     *height)
506 {
507         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
508
509         klass->get_page_size (document, page, width, height);
510 }
511
512 void
513 ev_document_get_page_size (EvDocument *document,
514                            gint        page_index,
515                            double     *width,
516                            double     *height)
517 {
518         g_return_if_fail (EV_IS_DOCUMENT (document));
519         g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
520
521         if (width)
522                 *width = document->priv->uniform ?
523                         document->priv->uniform_width :
524                         document->priv->page_sizes[page_index].width;
525         if (height)
526                 *height = document->priv->uniform ?
527                         document->priv->uniform_height :
528                         document->priv->page_sizes[page_index].height;
529 }
530
531 static gchar *
532 _ev_document_get_page_label (EvDocument *document,
533                              EvPage     *page)
534 {
535         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
536
537         return klass->get_page_label ?
538                 klass->get_page_label (document, page) : NULL;
539 }
540
541 gchar *
542 ev_document_get_page_label (EvDocument *document,
543                             gint        page_index)
544 {
545         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
546         g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
547
548         return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
549                 g_strdup (document->priv->page_labels[page_index]) :
550                 g_strdup_printf ("%d", page_index + 1);
551 }
552
553 static EvDocumentInfo *
554 _ev_document_get_info (EvDocument *document)
555 {
556         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
557
558         return klass->get_info (document);
559 }
560
561 EvDocumentInfo *
562 ev_document_get_info (EvDocument *document)
563 {
564         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
565
566         return document->priv->info;
567 }
568
569 gboolean
570 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
571 {
572         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
573
574         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
575         if (klass->get_backend_info == NULL)
576                 return FALSE;
577
578         return klass->get_backend_info (document, info);
579 }
580
581 cairo_surface_t *
582 ev_document_render (EvDocument      *document,
583                     EvRenderContext *rc)
584 {
585         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
586
587         return klass->render (document, rc);
588 }
589
590 static GdkPixbuf *
591 _ev_document_get_thumbnail (EvDocument      *document,
592                             EvRenderContext *rc)
593 {
594         cairo_surface_t *surface;
595         GdkPixbuf       *pixbuf;
596
597         surface = ev_document_render (document, rc);
598         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
599         cairo_surface_destroy (surface);
600
601         return pixbuf;
602 }
603
604 GdkPixbuf *
605 ev_document_get_thumbnail (EvDocument      *document,
606                            EvRenderContext *rc)
607 {
608         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
609
610         if (klass->get_thumbnail)
611                 return klass->get_thumbnail (document, rc);
612
613         return _ev_document_get_thumbnail (document, rc);
614 }
615
616 const gchar *
617 ev_document_get_uri (EvDocument *document)
618 {
619         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
620
621         return document->priv->uri;
622 }
623
624 const gchar *
625 ev_document_get_title (EvDocument *document)
626 {
627         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
628
629         return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
630                 document->priv->info->title : NULL;
631 }
632
633 gboolean
634 ev_document_is_page_size_uniform (EvDocument *document)
635 {
636         g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
637
638         return document->priv->uniform;
639 }
640
641 void
642 ev_document_get_max_page_size (EvDocument *document,
643                                gdouble    *width,
644                                gdouble    *height)
645 {
646         g_return_if_fail (EV_IS_DOCUMENT (document));
647
648         if (width)
649                 *width = document->priv->max_width;
650         if (height)
651                 *height = document->priv->max_height;
652 }
653
654 void
655 ev_document_get_min_page_size (EvDocument *document,
656                                gdouble    *width,
657                                gdouble    *height)
658 {
659         g_return_if_fail (EV_IS_DOCUMENT (document));
660
661         if (width)
662                 *width = document->priv->min_width;
663         if (height)
664                 *height = document->priv->min_height;
665 }
666
667 gboolean
668 ev_document_check_dimensions (EvDocument *document)
669 {
670         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
671
672         return (document->priv->max_width > 0 && document->priv->max_height > 0);
673 }
674
675 gint
676 ev_document_get_max_label_len (EvDocument *document)
677 {
678         g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
679
680         return document->priv->max_label;
681 }
682
683 gboolean
684 ev_document_has_text_page_labels (EvDocument *document)
685 {
686         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
687
688         return document->priv->page_labels != NULL;
689 }
690
691 gboolean
692 ev_document_find_page_by_label (EvDocument  *document,
693                                 const gchar *page_label,
694                                 gint        *page_index)
695 {
696         gint i, page;
697         glong value;
698         gchar *endptr = NULL;
699         EvDocumentPrivate *priv = document->priv;
700
701         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
702         g_return_val_if_fail (page_label != NULL, FALSE);
703         g_return_val_if_fail (page_index != NULL, FALSE);
704
705         /* First, look for a literal label match */
706         for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
707                 if (priv->page_labels[i] != NULL &&
708                     ! strcmp (page_label, priv->page_labels[i])) {
709                         *page_index = i;
710                         return TRUE;
711                 }
712         }
713
714         /* Second, look for a match with case insensitively */
715         for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
716                 if (priv->page_labels[i] != NULL &&
717                     ! strcasecmp (page_label, priv->page_labels[i])) {
718                         *page_index = i;
719                         return TRUE;
720                 }
721         }
722
723         /* Next, parse the label, and see if the number fits */
724         value = strtol (page_label, &endptr, 10);
725         if (endptr[0] == '\0') {
726                 /* Page number is an integer */
727                 page = MIN (G_MAXINT, value);
728
729                 /* convert from a page label to a page offset */
730                 page --;
731                 if (page >= 0 && page < priv->n_pages) {
732                         *page_index = page;
733                         return TRUE;
734                 }
735         }
736
737         return FALSE;
738 }
739
740 /* EvDocumentInfo */
741 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
742
743 EvDocumentInfo *
744 ev_document_info_copy (EvDocumentInfo *info)
745 {
746         EvDocumentInfo *copy;
747         
748         g_return_val_if_fail (info != NULL, NULL);
749
750         copy = g_new0 (EvDocumentInfo, 1);
751         copy->title = g_strdup (info->title);
752         copy->format = g_strdup (info->format);
753         copy->author = g_strdup (info->author);
754         copy->subject = g_strdup (info->subject);
755         copy->keywords = g_strdup (info->keywords);
756         copy->security = g_strdup (info->security);
757         copy->creator = g_strdup (info->creator);
758         copy->producer = g_strdup (info->producer);
759         copy->linearized = g_strdup (info->linearized);
760         
761         copy->creation_date = info->creation_date;
762         copy->modified_date = info->modified_date;
763         copy->layout = info->layout;
764         copy->mode = info->mode;
765         copy->ui_hints = info->ui_hints;
766         copy->permissions = info->permissions;
767         copy->n_pages = info->n_pages;
768         copy->license = ev_document_license_copy (info->license);
769
770         copy->fields_mask = info->fields_mask;
771
772         return copy;
773 }
774
775 void
776 ev_document_info_free (EvDocumentInfo *info)
777 {
778         if (info == NULL)
779                 return;
780
781         g_free (info->title);
782         g_free (info->format);
783         g_free (info->author);
784         g_free (info->subject);
785         g_free (info->keywords);
786         g_free (info->creator);
787         g_free (info->producer);
788         g_free (info->linearized);
789         g_free (info->security);
790         ev_document_license_free (info->license);
791
792         g_free (info);
793 }
794
795 /* EvDocumentLicense */
796 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
797
798 EvDocumentLicense *
799 ev_document_license_new (void)
800 {
801         return g_new0 (EvDocumentLicense, 1);
802 }
803
804 EvDocumentLicense *
805 ev_document_license_copy (EvDocumentLicense *license)
806 {
807         EvDocumentLicense *new_license;
808
809         if (!license)
810                 return NULL;
811
812         new_license = ev_document_license_new ();
813
814         if (license->text)
815                 new_license->text = g_strdup (license->text);
816         if (license->uri)
817                 new_license->uri = g_strdup (license->uri);
818         if (license->web_statement)
819                 new_license->web_statement = g_strdup (license->web_statement);
820
821         return new_license;
822 }
823
824 void
825 ev_document_license_free (EvDocumentLicense *license)
826 {
827         if (!license)
828                 return;
829
830         g_free (license->text);
831         g_free (license->uri);
832         g_free (license->web_statement);
833
834         g_free (license);
835 }
836
837 const gchar *
838 ev_document_license_get_text (EvDocumentLicense *license)
839 {
840         return license->text;
841 }
842
843 const gchar *
844 ev_document_license_get_uri (EvDocumentLicense *license)
845 {
846         return license->uri;
847 }
848
849 const gchar *
850 ev_document_license_get_web_statement (EvDocumentLicense *license)
851 {
852         return license->web_statement;
853 }
854
855 /* EvRectangle */
856 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
857
858 EvRectangle *
859 ev_rectangle_new (void)
860 {
861         return g_new0 (EvRectangle, 1);
862 }
863
864 EvRectangle *
865 ev_rectangle_copy (EvRectangle *rectangle)
866 {
867         EvRectangle *new_rectangle;
868
869         g_return_val_if_fail (rectangle != NULL, NULL);
870
871         new_rectangle = g_new (EvRectangle, 1);
872         *new_rectangle = *rectangle;
873
874         return new_rectangle;
875 }
876
877 void
878 ev_rectangle_free (EvRectangle *rectangle)
879 {
880         g_free (rectangle);
881 }
882
883 /* Compares two rects.  returns 0 if they're equal */
884 #define EPSILON 0.0000001
885
886 gint
887 ev_rect_cmp (EvRectangle *a,
888              EvRectangle *b)
889 {
890         if (a == b)
891                 return 0;
892         if (a == NULL || b == NULL)
893                 return 1;
894
895         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
896                   (ABS (a->y1 - b->y1) < EPSILON) &&
897                   (ABS (a->x2 - b->x2) < EPSILON) &&
898                   (ABS (a->y2 - b->y2) < EPSILON));
899 }