]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-cache.c
*** empty log message ***
[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 + 1);
121         page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
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 * 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;
139                         saved_height += page_height;
140                 }
141         }
142
143         if (DUAL_EVEN_LEFT && !page_cache->uniform) {
144                 if (!swap) {
145                         saved_height = page_cache->size_cache [0].height;
146                 } else {
147                         saved_height = page_cache->size_cache [0].width;
148                 }
149         } else {
150                 saved_height = 0;
151         }
152         for (i = DUAL_EVEN_LEFT; i < page_cache->n_pages + 2; i += 2) {
153                 if (page_cache->uniform) {
154                         if (!swap) {
155                                 uniform_height = page_cache->uniform_height;
156                         } else {
157                                 uniform_height = page_cache->uniform_width;
158                         }
159                         page_cache->dual_height_to_page [i] = ((i + DUAL_EVEN_LEFT) / 2) * uniform_height;
160                         if (i + 1 < page_cache->n_pages + 2)
161                                 page_cache->dual_height_to_page [i + 1] = ((i + DUAL_EVEN_LEFT) / 2) * uniform_height;
162                 } else {
163                         if (i + 1 < page_cache->n_pages) {
164                                 if (!swap) {
165                                         next_page_height = page_cache->size_cache [i + 1].height;
166                                 } else {
167                                         next_page_height = page_cache->size_cache [i + 1].width;
168                                 }
169                         } else {
170                                 next_page_height = 0;
171                         }
172                         if (i < page_cache->n_pages) {
173                                 if (!swap) {
174                                         page_height = page_cache->size_cache [i].height;
175                                 } else {
176                                         page_height = page_cache->size_cache [i].width;
177                                 }
178                         } else {
179                                 page_height = 0;
180                         }
181                         if (i + 1 < page_cache->n_pages + 2) {
182                                 page_cache->dual_height_to_page [i] = saved_height;
183                                 page_cache->dual_height_to_page [i + 1] = saved_height;
184                                 saved_height += MAX(page_height, next_page_height);
185                         } else {
186                                 page_cache->dual_height_to_page [i] = saved_height;
187                         }
188                 }
189         }
190 }
191
192 EvPageCache *
193 ev_page_cache_new (EvDocument *document)
194 {
195         EvPageCache *page_cache;
196         EvPageCacheInfo *info;
197         gint i;
198
199         page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
200
201         ev_document_doc_mutex_lock ();
202
203         /* We read page information out of the document */
204
205         /* Assume all pages are the same size until proven otherwise */
206         page_cache->uniform = TRUE;
207         page_cache->has_labels = FALSE;
208         page_cache->n_pages = ev_document_get_n_pages (document);
209         page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
210         page_cache->max_width = 0;
211         page_cache->max_height = 0;
212         page_cache->page_info = ev_document_get_info (document);
213
214         if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
215                 page_cache->title = g_strdup (page_cache->page_info->title);
216         } else {
217                 page_cache->title = NULL;
218         }
219
220         for (i = 0; i < page_cache->n_pages; i++) {
221                 double page_width = 0;
222                 double page_height = 0;
223                 
224                 ev_document_get_page_size (document, i, &page_width, &page_height);
225
226                 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
227                 
228                 if (page_cache->page_labels[i] != NULL) {
229                 
230                         page_cache->max_label_chars = MAX(page_cache->max_label_chars, 
231                                                             g_utf8_strlen (page_cache->page_labels[i], 256));
232                         if (!page_cache->has_labels) {
233                                 gchar *expected_label;
234                         
235                                 expected_label = g_strdup_printf ("%d", i + 1);
236                                 if (strcmp (expected_label, page_cache->page_labels[i]))  
237                                         page_cache->has_labels = TRUE;
238                                 g_free (expected_label);
239                         }
240                 }
241
242                 if (page_width > page_cache->max_width) {
243                         page_cache->max_width = page_width;
244                 }
245
246                 if (page_height > page_cache->max_height) {
247                         page_cache->max_height = page_height;
248                 }
249                         
250                 if (i == 0) {
251                         page_cache->uniform_width = page_width;
252                         page_cache->uniform_height = page_height;
253                 } else if (page_cache->uniform &&
254                            (page_cache->uniform_width != page_width ||
255                             page_cache->uniform_height != page_height)) {
256                         /* It's a different page size.  Backfill the array. */
257                         int j;
258
259                         page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
260
261                         for (j = 0; j < i; j++) {
262                                 info = &(page_cache->size_cache [j]);
263                                 info->width = page_cache->uniform_width;
264                                 info->height = page_cache->uniform_height;
265                         }
266                         page_cache->uniform = FALSE;
267
268                 }
269
270                 if (! page_cache->uniform) {
271                         info = &(page_cache->size_cache [i]);
272
273                         info->width = page_width;
274                         info->height = page_height;
275                 }
276         }
277
278         build_height_to_page (page_cache);
279
280         /* make some sanity check assertions */
281         if (! page_cache->uniform)
282                 g_assert (page_cache->size_cache != NULL);
283         if (page_cache->uniform && page_cache->n_pages > 0)
284                 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
285
286         ev_document_doc_mutex_unlock ();
287
288         if (page_cache->n_pages > 0)
289                 ev_page_cache_set_current_page (page_cache, 0);
290
291         return page_cache;
292 }
293
294 gint
295 ev_page_cache_get_n_pages (EvPageCache *page_cache)
296 {
297         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
298
299         return page_cache->n_pages;
300 }
301
302 gint
303 ev_page_cache_get_current_page (EvPageCache *page_cache)
304 {
305         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
306
307         return page_cache->current_page;
308 }
309
310 void
311 ev_page_cache_set_current_page (EvPageCache *page_cache,
312                                 int          page)
313 {
314         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
315         g_return_if_fail (page >= 0 || page < page_cache->n_pages);
316
317         if (page == page_cache->current_page)
318                 return;
319
320         page_cache->current_page = page;
321         g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
322 }
323
324 gboolean
325 ev_page_cache_set_page_label (EvPageCache *page_cache,
326                               const char  *page_label)
327 {
328         gint i, page;
329         long value;
330         char *endptr = NULL;
331         
332         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
333         g_return_val_if_fail (page_label != NULL, FALSE);
334
335         /* First, look for a literal label match */
336         for (i = 0; i < page_cache->n_pages; i ++) {
337                 if (page_cache->page_labels[i] != NULL &&
338                     ! strcmp (page_label, page_cache->page_labels[i])) {
339                         ev_page_cache_set_current_page (page_cache, i);
340                         return TRUE;
341                 }
342         }
343
344         /* Next, parse the label, and see if the number fits */
345         value = strtol (page_label, &endptr, 10);
346         if (endptr[0] == '\0') {
347                 /* Page number is an integer */
348                 page = MIN (G_MAXINT, value);
349
350                 /* convert from a page label to a page offset */
351                 page --;
352                 if (page >= 0 &&
353                     page < page_cache->n_pages &&
354                     page_cache->page_labels[page] == NULL) {
355                         ev_page_cache_set_current_page (page_cache, page);
356                         return TRUE;
357                 }
358         }
359
360         return FALSE;
361 }
362
363 const char *
364 ev_page_cache_get_title (EvPageCache *page_cache)
365 {
366         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
367
368         return page_cache->title;
369 }
370
371 void
372 ev_page_cache_get_size (EvPageCache  *page_cache,
373                         gint          page,
374                         gint          rotation,
375                         gfloat        scale,
376                         gint         *width,
377                         gint         *height)
378 {
379         double w, h;
380
381         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
382         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
383
384         if (page_cache->uniform) {
385                 w = page_cache->uniform_width;
386                 h = page_cache->uniform_height;
387         } else {
388                 EvPageCacheInfo *info;
389
390                 info = &(page_cache->size_cache [page]);
391                 
392                 w = info->width;
393                 h = info->height;
394         }
395
396         w = w * scale + 0.5;
397         h = h * scale + 0.5;
398
399         if (rotation == 0 || rotation == 180) {
400                 if (width) *width = (int)w;
401                 if (height) *height = (int)h;
402         } else {
403                 if (width) *width = (int)h;
404                 if (height) *height = (int)w;
405         }
406 }
407
408 void
409 ev_page_cache_get_max_width (EvPageCache   *page_cache,
410                              gint           rotation,
411                              gfloat         scale,
412                              gint          *width)
413 {
414         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
415
416         if (width) {
417                 if (rotation == 0 || rotation == 180) {
418                         *width = page_cache->max_width * scale;
419                 } else {
420                         *width = page_cache->max_height * scale;
421                 }
422         }
423 }
424
425 void
426 ev_page_cache_get_max_height (EvPageCache   *page_cache,
427                               gint           rotation,
428                               gfloat         scale,
429                               gint          *height)
430 {
431         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
432
433         if (height) {
434                 if (rotation == 0 || rotation == 180) {
435                         *height = page_cache->max_height * scale;
436                 } else {
437                         *height = page_cache->max_width * scale;
438                 }
439         }
440 }
441
442 void    
443 ev_page_cache_get_height_to_page (EvPageCache   *page_cache,
444                                   gint           page,
445                                   gint           rotation,
446                                   gfloat         scale,
447                                   gint          *height,
448                                   gint          *dual_height)
449 {
450         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
451
452         if (page_cache->rotation != rotation) {
453                 page_cache->rotation = rotation;
454                 build_height_to_page (page_cache);
455         }
456         
457         if (height)
458                 *height = page_cache->height_to_page [page] * scale;
459
460         if (dual_height)
461                 *dual_height = page_cache->dual_height_to_page [page] * scale;
462 }
463
464 gint
465 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
466 {
467         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
468         
469         return page_cache->max_label_chars;
470 }
471
472 gchar *
473 ev_page_cache_get_page_label (EvPageCache *page_cache,
474                               gint         page)
475 {
476         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
477         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
478
479         if (page_cache->page_labels[page] == NULL)
480                 return g_strdup_printf ("%d", page + 1);
481
482         return g_strdup (page_cache->page_labels[page]);
483 }
484
485 gboolean
486 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
487 {
488         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
489         return page_cache->has_labels;
490 }
491
492 const EvDocumentInfo *
493 ev_page_cache_get_info (EvPageCache *page_cache)
494 {
495         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
496
497         return page_cache->page_info;
498 }
499
500 #define PAGE_CACHE_STRING "ev-page-cache"
501
502 EvPageCache *
503 ev_page_cache_get (EvDocument *document)
504 {
505         EvPageCache *page_cache;
506
507         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
508
509         page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
510         if (page_cache == NULL) {
511                 page_cache = ev_page_cache_new (document);
512                 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);
513         }
514
515         return page_cache;
516 }