]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-cache.c
c1af7d228efe52684030bd00aafeb9bde7af1fca
[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                         ev_page_cache_set_current_page (page_cache, page);
358                         return TRUE;
359                 }
360         }
361
362         return FALSE;
363 }
364
365 const char *
366 ev_page_cache_get_title (EvPageCache *page_cache)
367 {
368         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
369
370         return page_cache->title;
371 }
372
373 void
374 ev_page_cache_get_size (EvPageCache  *page_cache,
375                         gint          page,
376                         gint          rotation,
377                         gfloat        scale,
378                         gint         *width,
379                         gint         *height)
380 {
381         double w, h;
382
383         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
384         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
385
386         if (page_cache->uniform) {
387                 w = page_cache->uniform_width;
388                 h = page_cache->uniform_height;
389         } else {
390                 EvPageCacheInfo *info;
391
392                 info = &(page_cache->size_cache [page]);
393                 
394                 w = info->width;
395                 h = info->height;
396         }
397
398         w = w * scale + 0.5;
399         h = h * scale + 0.5;
400
401         if (rotation == 0 || rotation == 180) {
402                 if (width) *width = (int)w;
403                 if (height) *height = (int)h;
404         } else {
405                 if (width) *width = (int)h;
406                 if (height) *height = (int)w;
407         }
408 }
409
410 void
411 ev_page_cache_get_max_width (EvPageCache   *page_cache,
412                              gint           rotation,
413                              gfloat         scale,
414                              gint          *width)
415 {
416         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
417
418         if (width) {
419                 if (rotation == 0 || rotation == 180) {
420                         *width = page_cache->max_width * scale;
421                 } else {
422                         *width = page_cache->max_height * scale;
423                 }
424         }
425 }
426
427 void
428 ev_page_cache_get_max_height (EvPageCache   *page_cache,
429                               gint           rotation,
430                               gfloat         scale,
431                               gint          *height)
432 {
433         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
434
435         if (height) {
436                 if (rotation == 0 || rotation == 180) {
437                         *height = page_cache->max_height * scale;
438                 } else {
439                         *height = page_cache->max_width * scale;
440                 }
441         }
442 }
443
444 void    
445 ev_page_cache_get_height_to_page (EvPageCache   *page_cache,
446                                   gint           page,
447                                   gint           rotation,
448                                   gfloat         scale,
449                                   gint          *height,
450                                   gint          *dual_height)
451 {
452         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
453
454         if (page_cache->rotation != rotation) {
455                 page_cache->rotation = rotation;
456                 build_height_to_page (page_cache);
457         }
458         
459         if (height)
460                 *height = page_cache->height_to_page [page] * scale;
461
462         if (dual_height)
463                 *dual_height = page_cache->dual_height_to_page [page] * scale;
464 }
465
466 gint
467 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
468 {
469         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
470         
471         return page_cache->max_label_chars;
472 }
473
474 gboolean
475 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
476 {
477         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
478         
479         return page_cache->dual_even_left;
480 }
481
482 gchar *
483 ev_page_cache_get_page_label (EvPageCache *page_cache,
484                               gint         page)
485 {
486         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
487         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
488
489         if (page_cache->page_labels[page] == NULL)
490                 return g_strdup_printf ("%d", page + 1);
491
492         return g_strdup (page_cache->page_labels[page]);
493 }
494
495 gboolean
496 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
497 {
498         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
499         return page_cache->has_labels;
500 }
501
502 const EvDocumentInfo *
503 ev_page_cache_get_info (EvPageCache *page_cache)
504 {
505         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
506
507         return page_cache->page_info;
508 }
509
510 #define PAGE_CACHE_STRING "ev-page-cache"
511
512 EvPageCache *
513 ev_page_cache_get (EvDocument *document)
514 {
515         EvPageCache *page_cache;
516
517         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
518
519         page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
520         if (page_cache == NULL) {
521                 page_cache = ev_page_cache_new (document);
522                 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);
523         }
524
525         return page_cache;
526 }