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