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