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