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