]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
[libdocument] Remove unused method ev_link_get_page()
[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                         result = g_new (EvSourceLink, 1);
424                         result->filename = synctex_scanner_get_name (scanner,
425                                                                      synctex_node_tag (node));
426                         result->line = synctex_node_line (node);
427                         result->col = synctex_node_column (node);
428                 }
429         }
430
431         return result;
432 }
433
434 /**
435  * ev_document_synctex_forward_search:
436  * @document:
437  * @source_link:
438  *
439  * Peforms a Synctex forward search to obtain the area in the document
440  * corresponding to the position @line and @column number in the source Tex file
441  *
442  * Returns: An EvMapping with the page number and area corresponfing to
443  * the given line in the source file. It must be free with g_free when done
444  */
445 EvMapping *
446 ev_document_synctex_forward_search (EvDocument   *document,
447                                     EvSourceLink *link)
448 {
449         EvMapping        *result = NULL;
450         synctex_scanner_t scanner;
451
452         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
453
454         scanner = document->priv->synctex_scanner;
455         if (!scanner)
456                 return NULL;
457
458         if (synctex_display_query (scanner, link->filename, link->line, link->col) > 0) {
459                 synctex_node_t node;
460                 gint           page;
461
462                 if ((node = synctex_next_result (scanner))) {
463                         result = g_new (EvMapping, 1);
464
465                         page = synctex_node_page (node) - 1;
466                         result->data = GINT_TO_POINTER (page);
467
468                         result->area.x1 = synctex_node_box_visible_h (node);
469                         result->area.y1 = synctex_node_box_visible_v (node) -
470                                 synctex_node_box_visible_height (node);
471                         result->area.x2 = synctex_node_box_visible_width (node) + result->area.x1;
472                         result->area.y2 = synctex_node_box_visible_depth (node) +
473                                 synctex_node_box_visible_height (node) + result->area.y1;
474                 }
475         }
476
477         return result;
478 }
479
480 static gint
481 _ev_document_get_n_pages (EvDocument  *document)
482 {
483         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
484
485         return klass->get_n_pages (document);
486 }
487
488 gint
489 ev_document_get_n_pages (EvDocument  *document)
490 {
491         g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
492
493         return document->priv->n_pages;
494 }
495
496 static void
497 _ev_document_get_page_size (EvDocument *document,
498                             EvPage     *page,
499                             double     *width,
500                             double     *height)
501 {
502         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
503
504         klass->get_page_size (document, page, width, height);
505 }
506
507 void
508 ev_document_get_page_size (EvDocument *document,
509                            gint        page_index,
510                            double     *width,
511                            double     *height)
512 {
513         g_return_if_fail (EV_IS_DOCUMENT (document));
514         g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
515
516         if (width)
517                 *width = document->priv->uniform ?
518                         document->priv->uniform_width :
519                         document->priv->page_sizes[page_index].width;
520         if (height)
521                 *height = document->priv->uniform ?
522                         document->priv->uniform_height :
523                         document->priv->page_sizes[page_index].height;
524 }
525
526 static gchar *
527 _ev_document_get_page_label (EvDocument *document,
528                              EvPage     *page)
529 {
530         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
531
532         return klass->get_page_label ?
533                 klass->get_page_label (document, page) : NULL;
534 }
535
536 gchar *
537 ev_document_get_page_label (EvDocument *document,
538                             gint        page_index)
539 {
540         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
541         g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
542
543         return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
544                 g_strdup (document->priv->page_labels[page_index]) :
545                 g_strdup_printf ("%d", page_index + 1);
546 }
547
548 static EvDocumentInfo *
549 _ev_document_get_info (EvDocument *document)
550 {
551         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
552
553         return klass->get_info (document);
554 }
555
556 EvDocumentInfo *
557 ev_document_get_info (EvDocument *document)
558 {
559         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
560
561         return document->priv->info;
562 }
563
564 gboolean
565 ev_document_get_backend_info (EvDocument *document, EvDocumentBackendInfo *info)
566 {
567         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
568
569         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
570         if (klass->get_backend_info == NULL)
571                 return FALSE;
572
573         return klass->get_backend_info (document, info);
574 }
575
576 cairo_surface_t *
577 ev_document_render (EvDocument      *document,
578                     EvRenderContext *rc)
579 {
580         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
581
582         return klass->render (document, rc);
583 }
584
585 static GdkPixbuf *
586 _ev_document_get_thumbnail (EvDocument      *document,
587                             EvRenderContext *rc)
588 {
589         cairo_surface_t *surface;
590         GdkPixbuf       *pixbuf;
591
592         surface = ev_document_render (document, rc);
593         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
594         cairo_surface_destroy (surface);
595
596         return pixbuf;
597 }
598
599 GdkPixbuf *
600 ev_document_get_thumbnail (EvDocument      *document,
601                            EvRenderContext *rc)
602 {
603         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
604
605         if (klass->get_thumbnail)
606                 return klass->get_thumbnail (document, rc);
607
608         return _ev_document_get_thumbnail (document, rc);
609 }
610
611 const gchar *
612 ev_document_get_uri (EvDocument *document)
613 {
614         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
615
616         return document->priv->uri;
617 }
618
619 const gchar *
620 ev_document_get_title (EvDocument *document)
621 {
622         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
623
624         return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
625                 document->priv->info->title : NULL;
626 }
627
628 gboolean
629 ev_document_is_page_size_uniform (EvDocument *document)
630 {
631         g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
632
633         return document->priv->uniform;
634 }
635
636 void
637 ev_document_get_max_page_size (EvDocument *document,
638                                gdouble    *width,
639                                gdouble    *height)
640 {
641         g_return_if_fail (EV_IS_DOCUMENT (document));
642
643         if (width)
644                 *width = document->priv->max_width;
645         if (height)
646                 *height = document->priv->max_height;
647 }
648
649 void
650 ev_document_get_min_page_size (EvDocument *document,
651                                gdouble    *width,
652                                gdouble    *height)
653 {
654         g_return_if_fail (EV_IS_DOCUMENT (document));
655
656         if (width)
657                 *width = document->priv->min_width;
658         if (height)
659                 *height = document->priv->min_height;
660 }
661
662 gboolean
663 ev_document_check_dimensions (EvDocument *document)
664 {
665         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
666
667         return (document->priv->max_width > 0 && document->priv->max_height > 0);
668 }
669
670 gint
671 ev_document_get_max_label_len (EvDocument *document)
672 {
673         g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
674
675         return document->priv->max_label;
676 }
677
678 gboolean
679 ev_document_has_text_page_labels (EvDocument *document)
680 {
681         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
682
683         return document->priv->page_labels != NULL;
684 }
685
686 gboolean
687 ev_document_find_page_by_label (EvDocument  *document,
688                                 const gchar *page_label,
689                                 gint        *page_index)
690 {
691         gint i, page;
692         glong value;
693         gchar *endptr = NULL;
694         EvDocumentPrivate *priv = document->priv;
695
696         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
697         g_return_val_if_fail (page_label != NULL, FALSE);
698         g_return_val_if_fail (page_index != NULL, FALSE);
699
700         /* First, look for a literal label match */
701         for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
702                 if (priv->page_labels[i] != NULL &&
703                     ! strcmp (page_label, priv->page_labels[i])) {
704                         *page_index = i;
705                         return TRUE;
706                 }
707         }
708
709         /* Second, look for a match with case insensitively */
710         for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
711                 if (priv->page_labels[i] != NULL &&
712                     ! strcasecmp (page_label, priv->page_labels[i])) {
713                         *page_index = i;
714                         return TRUE;
715                 }
716         }
717
718         /* Next, parse the label, and see if the number fits */
719         value = strtol (page_label, &endptr, 10);
720         if (endptr[0] == '\0') {
721                 /* Page number is an integer */
722                 page = MIN (G_MAXINT, value);
723
724                 /* convert from a page label to a page offset */
725                 page --;
726                 if (page >= 0 && page < priv->n_pages) {
727                         *page_index = page;
728                         return TRUE;
729                 }
730         }
731
732         return FALSE;
733 }
734
735 /* EvDocumentInfo */
736 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
737
738 EvDocumentInfo *
739 ev_document_info_copy (EvDocumentInfo *info)
740 {
741         EvDocumentInfo *copy;
742         
743         g_return_val_if_fail (info != NULL, NULL);
744
745         copy = g_new0 (EvDocumentInfo, 1);
746         copy->title = g_strdup (info->title);
747         copy->format = g_strdup (info->format);
748         copy->author = g_strdup (info->author);
749         copy->subject = g_strdup (info->subject);
750         copy->keywords = g_strdup (info->keywords);
751         copy->security = g_strdup (info->security);
752         copy->creator = g_strdup (info->creator);
753         copy->producer = g_strdup (info->producer);
754         copy->linearized = g_strdup (info->linearized);
755         
756         copy->creation_date = info->creation_date;
757         copy->modified_date = info->modified_date;
758         copy->layout = info->layout;
759         copy->mode = info->mode;
760         copy->ui_hints = info->ui_hints;
761         copy->permissions = info->permissions;
762         copy->n_pages = info->n_pages;
763         copy->license = ev_document_license_copy (info->license);
764
765         copy->fields_mask = info->fields_mask;
766
767         return copy;
768 }
769
770 void
771 ev_document_info_free (EvDocumentInfo *info)
772 {
773         if (info == NULL)
774                 return;
775
776         g_free (info->title);
777         g_free (info->format);
778         g_free (info->author);
779         g_free (info->subject);
780         g_free (info->keywords);
781         g_free (info->creator);
782         g_free (info->producer);
783         g_free (info->linearized);
784         g_free (info->security);
785         ev_document_license_free (info->license);
786
787         g_free (info);
788 }
789
790 /* EvDocumentLicense */
791 EV_DEFINE_BOXED_TYPE (EvDocumentLicense, ev_document_license, ev_document_license_copy, ev_document_license_free)
792
793 EvDocumentLicense *
794 ev_document_license_new (void)
795 {
796         return g_new0 (EvDocumentLicense, 1);
797 }
798
799 EvDocumentLicense *
800 ev_document_license_copy (EvDocumentLicense *license)
801 {
802         EvDocumentLicense *new_license;
803
804         if (!license)
805                 return NULL;
806
807         new_license = ev_document_license_new ();
808
809         if (license->text)
810                 new_license->text = g_strdup (license->text);
811         if (license->uri)
812                 new_license->uri = g_strdup (license->uri);
813         if (license->web_statement)
814                 new_license->web_statement = g_strdup (license->web_statement);
815
816         return new_license;
817 }
818
819 void
820 ev_document_license_free (EvDocumentLicense *license)
821 {
822         if (!license)
823                 return;
824
825         g_free (license->text);
826         g_free (license->uri);
827         g_free (license->web_statement);
828
829         g_free (license);
830 }
831
832 const gchar *
833 ev_document_license_get_text (EvDocumentLicense *license)
834 {
835         return license->text;
836 }
837
838 const gchar *
839 ev_document_license_get_uri (EvDocumentLicense *license)
840 {
841         return license->uri;
842 }
843
844 const gchar *
845 ev_document_license_get_web_statement (EvDocumentLicense *license)
846 {
847         return license->web_statement;
848 }
849
850 /* EvRectangle */
851 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
852
853 EvRectangle *
854 ev_rectangle_new (void)
855 {
856         return g_new0 (EvRectangle, 1);
857 }
858
859 EvRectangle *
860 ev_rectangle_copy (EvRectangle *rectangle)
861 {
862         EvRectangle *new_rectangle;
863
864         g_return_val_if_fail (rectangle != NULL, NULL);
865
866         new_rectangle = g_new (EvRectangle, 1);
867         *new_rectangle = *rectangle;
868
869         return new_rectangle;
870 }
871
872 void
873 ev_rectangle_free (EvRectangle *rectangle)
874 {
875         g_free (rectangle);
876 }
877
878 /* Compares two rects.  returns 0 if they're equal */
879 #define EPSILON 0.0000001
880
881 gint
882 ev_rect_cmp (EvRectangle *a,
883              EvRectangle *b)
884 {
885         if (a == b)
886                 return 0;
887         if (a == NULL || b == NULL)
888                 return 1;
889
890         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
891                   (ABS (a->y1 - b->y1) < EPSILON) &&
892                   (ABS (a->x2 - b->x2) < EPSILON) &&
893                   (ABS (a->y2 - b->y2) < EPSILON));
894 }