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