]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-cache.c
Fix memory leak in ev-page-cache.
[evince.git] / shell / ev-page-cache.c
1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
3 #include <stdlib.h>
4 #include <string.h>
5
6 typedef struct _EvPageCacheInfo
7 {
8         double width;
9         double height;
10 }
11 EvPageCacheInfo;
12
13
14 struct _EvPageCache
15 {
16         GObject parent;
17
18         gint current_page;
19         int n_pages;
20         char *title;
21         char **page_labels;
22         
23         gint max_label_chars;
24         gboolean has_labels;
25         gboolean uniform;
26         gboolean dual_even_left;
27         
28         double uniform_width;
29         double uniform_height;
30
31         double  max_width;
32         double  max_height;
33
34         double* height_to_page;
35         double* dual_height_to_page;
36
37         int rotation;
38
39         EvPageCacheInfo *size_cache;
40         EvDocumentInfo *page_info;
41 };
42
43 struct _EvPageCacheClass
44 {
45         GObjectClass parent_class;
46
47         void (* page_changed) (EvPageCache *page_cache, gint page);
48 };
49
50 enum
51 {
52         PAGE_CHANGED,
53         N_SIGNALS,
54 };
55
56 static guint signals[N_SIGNALS] = {0, };
57
58 static void ev_page_cache_init       (EvPageCache      *page_cache);
59 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
60 static void ev_page_cache_finalize   (GObject *object);
61
62 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
63
64 static void
65 ev_page_cache_init (EvPageCache *page_cache)
66 {
67         page_cache->current_page = -1;
68         page_cache->max_label_chars = 0;
69 }
70
71 static void
72 ev_page_cache_class_init (EvPageCacheClass *class)
73 {
74         GObjectClass *object_class;
75
76         object_class = G_OBJECT_CLASS (class);
77
78         object_class->finalize = ev_page_cache_finalize;
79
80         signals [PAGE_CHANGED] =
81                 g_signal_new ("page-changed",
82                               EV_TYPE_PAGE_CACHE,
83                               G_SIGNAL_RUN_LAST,
84                               G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
85                               NULL, NULL,
86                               g_cclosure_marshal_VOID__INT,
87                               G_TYPE_NONE, 1,
88                               G_TYPE_INT);
89
90 }
91
92 static void
93 ev_page_cache_finalize (GObject *object)
94 {
95         EvPageCache *page_cache;
96
97         page_cache = EV_PAGE_CACHE (object);
98
99         g_free (page_cache->title);
100         g_free (page_cache->size_cache);
101         g_free (page_cache->height_to_page);
102         g_free (page_cache->dual_height_to_page);
103         g_strfreev (page_cache->page_labels);
104
105         ev_document_info_free (page_cache->page_info);
106 }
107
108 static void
109 build_height_to_page (EvPageCache *page_cache)
110 {
111         gboolean swap;
112         int i;
113         double uniform_height, page_height, next_page_height;
114         double saved_height;
115
116         swap = (page_cache->rotation == 90 ||
117                 page_cache->rotation == 270);
118
119         g_free (page_cache->height_to_page);
120         g_free (page_cache->dual_height_to_page);
121
122         page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
123         page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
124         
125         saved_height = 0;
126         for (i = 0; i <= page_cache->n_pages; i++) {
127                 if (page_cache->uniform) {
128                         if (!swap) {
129                                 uniform_height = page_cache->uniform_height;
130                         } else {
131                                 uniform_height = page_cache->uniform_width;
132                         }
133                         page_cache->height_to_page [i] = i * uniform_height;
134                 } else {
135                         if (!swap) {
136                                 page_height = page_cache->size_cache [i].height;
137                         } else {
138                                 page_height = page_cache->size_cache [i].width;
139                         }
140                         page_cache->height_to_page [i] = saved_height;
141                         saved_height += page_height;
142                 }
143         }
144
145         if (page_cache->dual_even_left && !page_cache->uniform) {
146                 if (!swap) {
147                         saved_height = page_cache->size_cache [0].height;
148                 } else {
149                         saved_height = page_cache->size_cache [0].width;
150                 }
151         } else {
152                 saved_height = 0;
153         }
154         for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
155                 if (page_cache->uniform) {
156                         if (!swap) {
157                                 uniform_height = page_cache->uniform_height;
158                         } else {
159                                 uniform_height = page_cache->uniform_width;
160                         }
161                         page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
162                         if (i + 1 < page_cache->n_pages + 2)
163                                 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
164                 } else {
165                         if (i + 1 < page_cache->n_pages) {
166                                 if (!swap) {
167                                         next_page_height = page_cache->size_cache [i + 1].height;
168                                 } else {
169                                         next_page_height = page_cache->size_cache [i + 1].width;
170                                 }
171                         } else {
172                                 next_page_height = 0;
173                         }
174                         if (i < page_cache->n_pages) {
175                                 if (!swap) {
176                                         page_height = page_cache->size_cache [i].height;
177                                 } else {
178                                         page_height = page_cache->size_cache [i].width;
179                                 }
180                         } else {
181                                 page_height = 0;
182                         }
183                         if (i + 1 < page_cache->n_pages + 2) {
184                                 page_cache->dual_height_to_page [i] = saved_height;
185                                 page_cache->dual_height_to_page [i + 1] = saved_height;
186                                 saved_height += MAX(page_height, next_page_height);
187                         } else {
188                                 page_cache->dual_height_to_page [i] = saved_height;
189                         }
190                 }
191         }
192 }
193
194 EvPageCache *
195 ev_page_cache_new (EvDocument *document)
196 {
197         EvPageCache *page_cache;
198         EvPageCacheInfo *info;
199         gint i;
200
201         page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
202
203         ev_document_doc_mutex_lock ();
204
205         /* We read page information out of the document */
206
207         /* Assume all pages are the same size until proven otherwise */
208         page_cache->uniform = TRUE;
209         page_cache->has_labels = FALSE;
210         page_cache->n_pages = ev_document_get_n_pages (document);
211         page_cache->dual_even_left = (page_cache->n_pages > 2);
212         page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
213         page_cache->max_width = 0;
214         page_cache->max_height = 0;
215         page_cache->page_info = ev_document_get_info (document);
216
217         if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
218                 page_cache->title = g_strdup (page_cache->page_info->title);
219         } else {
220                 page_cache->title = NULL;
221         }
222
223         for (i = 0; i < page_cache->n_pages; i++) {
224                 double page_width = 0;
225                 double page_height = 0;
226                 
227                 ev_document_get_page_size (document, i, &page_width, &page_height);
228
229                 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
230                 
231                 if (page_cache->page_labels[i] != NULL) {
232                 
233                         page_cache->max_label_chars = MAX(page_cache->max_label_chars, 
234                                                             g_utf8_strlen (page_cache->page_labels[i], 256));
235                         if (!page_cache->has_labels) {
236                                 gchar *expected_label;
237                         
238                                 expected_label = g_strdup_printf ("%d", i + 1);
239                                 if (strcmp (expected_label, page_cache->page_labels[i]))  
240                                         page_cache->has_labels = TRUE;
241                                 g_free (expected_label);
242                         }
243                 }
244
245                 if (page_width > page_cache->max_width) {
246                         page_cache->max_width = page_width;
247                 }
248
249                 if (page_height > page_cache->max_height) {
250                         page_cache->max_height = page_height;
251                 }
252                         
253                 if (i == 0) {
254                         page_cache->uniform_width = page_width;
255                         page_cache->uniform_height = page_height;
256                 } else if (page_cache->uniform &&
257                            (page_cache->uniform_width != page_width ||
258                             page_cache->uniform_height != page_height)) {
259                         /* It's a different page size.  Backfill the array. */
260                         int j;
261
262                         page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
263
264                         for (j = 0; j < i; j++) {
265                                 info = &(page_cache->size_cache [j]);
266                                 info->width = page_cache->uniform_width;
267                                 info->height = page_cache->uniform_height;
268                         }
269                         page_cache->uniform = FALSE;
270
271                 }
272
273                 if (! page_cache->uniform) {
274                         info = &(page_cache->size_cache [i]);
275
276                         info->width = page_width;
277                         info->height = page_height;
278                 }
279         }
280
281         build_height_to_page (page_cache);
282
283         /* make some sanity check assertions */
284         if (! page_cache->uniform)
285                 g_assert (page_cache->size_cache != NULL);
286         if (page_cache->uniform && page_cache->n_pages > 0)
287                 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
288
289         ev_document_doc_mutex_unlock ();
290
291         if (page_cache->n_pages > 0)
292                 ev_page_cache_set_current_page (page_cache, 0);
293
294         return page_cache;
295 }
296
297 gint
298 ev_page_cache_get_n_pages (EvPageCache *page_cache)
299 {
300         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
301
302         return page_cache->n_pages;
303 }
304
305 gint
306 ev_page_cache_get_current_page (EvPageCache *page_cache)
307 {
308         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
309
310         return page_cache->current_page;
311 }
312
313 void
314 ev_page_cache_set_current_page (EvPageCache *page_cache,
315                                 int          page)
316 {
317         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
318         g_return_if_fail (page >= 0 || page < page_cache->n_pages);
319
320         if (page == page_cache->current_page)
321                 return;
322
323         page_cache->current_page = page;
324         g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
325 }
326
327 gboolean
328 ev_page_cache_set_page_label (EvPageCache *page_cache,
329                               const char  *page_label)
330 {
331         gint i, page;
332         long value;
333         char *endptr = NULL;
334         
335         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
336         g_return_val_if_fail (page_label != NULL, FALSE);
337
338         /* First, look for a literal label match */
339         for (i = 0; i < page_cache->n_pages; i ++) {
340                 if (page_cache->page_labels[i] != NULL &&
341                     ! strcmp (page_label, page_cache->page_labels[i])) {
342                         ev_page_cache_set_current_page (page_cache, i);
343                         return TRUE;
344                 }
345         }
346
347         /* Next, parse the label, and see if the number fits */
348         value = strtol (page_label, &endptr, 10);
349         if (endptr[0] == '\0') {
350                 /* Page number is an integer */
351                 page = MIN (G_MAXINT, value);
352
353                 /* convert from a page label to a page offset */
354                 page --;
355                 if (page >= 0 &&
356                     page < page_cache->n_pages &&
357                     page_cache->page_labels[page] == NULL) {
358                         ev_page_cache_set_current_page (page_cache, page);
359                         return TRUE;
360                 }
361         }
362
363         return FALSE;
364 }
365
366 const char *
367 ev_page_cache_get_title (EvPageCache *page_cache)
368 {
369         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
370
371         return page_cache->title;
372 }
373
374 void
375 ev_page_cache_get_size (EvPageCache  *page_cache,
376                         gint          page,
377                         gint          rotation,
378                         gfloat        scale,
379                         gint         *width,
380                         gint         *height)
381 {
382         double w, h;
383
384         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
385         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
386
387         if (page_cache->uniform) {
388                 w = page_cache->uniform_width;
389                 h = page_cache->uniform_height;
390         } else {
391                 EvPageCacheInfo *info;
392
393                 info = &(page_cache->size_cache [page]);
394                 
395                 w = info->width;
396                 h = info->height;
397         }
398
399         w = w * scale + 0.5;
400         h = h * scale + 0.5;
401
402         if (rotation == 0 || rotation == 180) {
403                 if (width) *width = (int)w;
404                 if (height) *height = (int)h;
405         } else {
406                 if (width) *width = (int)h;
407                 if (height) *height = (int)w;
408         }
409 }
410
411 void
412 ev_page_cache_get_max_width (EvPageCache   *page_cache,
413                              gint           rotation,
414                              gfloat         scale,
415                              gint          *width)
416 {
417         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
418
419         if (width) {
420                 if (rotation == 0 || rotation == 180) {
421                         *width = page_cache->max_width * scale;
422                 } else {
423                         *width = page_cache->max_height * scale;
424                 }
425         }
426 }
427
428 void
429 ev_page_cache_get_max_height (EvPageCache   *page_cache,
430                               gint           rotation,
431                               gfloat         scale,
432                               gint          *height)
433 {
434         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
435
436         if (height) {
437                 if (rotation == 0 || rotation == 180) {
438                         *height = page_cache->max_height * scale;
439                 } else {
440                         *height = page_cache->max_width * scale;
441                 }
442         }
443 }
444
445 void    
446 ev_page_cache_get_height_to_page (EvPageCache   *page_cache,
447                                   gint           page,
448                                   gint           rotation,
449                                   gfloat         scale,
450                                   gint          *height,
451                                   gint          *dual_height)
452 {
453         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
454
455         if (page_cache->rotation != rotation) {
456                 page_cache->rotation = rotation;
457                 build_height_to_page (page_cache);
458         }
459         
460         if (height)
461                 *height = page_cache->height_to_page [page] * scale;
462
463         if (dual_height)
464                 *dual_height = page_cache->dual_height_to_page [page] * scale;
465 }
466
467 gint
468 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
469 {
470         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
471         
472         return page_cache->max_label_chars;
473 }
474
475 gboolean
476 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
477 {
478         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
479         
480         return page_cache->dual_even_left;
481 }
482
483 gchar *
484 ev_page_cache_get_page_label (EvPageCache *page_cache,
485                               gint         page)
486 {
487         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
488         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
489
490         if (page_cache->page_labels[page] == NULL)
491                 return g_strdup_printf ("%d", page + 1);
492
493         return g_strdup (page_cache->page_labels[page]);
494 }
495
496 gboolean
497 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
498 {
499         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
500         return page_cache->has_labels;
501 }
502
503 const EvDocumentInfo *
504 ev_page_cache_get_info (EvPageCache *page_cache)
505 {
506         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
507
508         return page_cache->page_info;
509 }
510
511 #define PAGE_CACHE_STRING "ev-page-cache"
512
513 EvPageCache *
514 ev_page_cache_get (EvDocument *document)
515 {
516         EvPageCache *page_cache;
517
518         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
519
520         page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
521         if (page_cache == NULL) {
522                 page_cache = ev_page_cache_new (document);
523                 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);
524         }
525
526         return page_cache;
527 }