]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-cache.c
Really make use of the orientation bit of the render context. Use the
[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                         EvOrientation orientation,
328                         gfloat        scale,
329                         gint         *width,
330                         gint         *height)
331 {
332         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
333         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
334
335         if (page_cache->uniform) {
336                 if (width)
337                         *width = page_cache->uniform_width;
338                 if (height)
339                         *height = page_cache->uniform_height;
340         } else {
341                 EvPageCacheInfo *info;
342
343                 info = &(page_cache->size_cache [page]);
344                 
345                 if (width)
346                         *width = info->width;
347                 if (height)
348                         *height = info->height;
349         }
350
351         if (width)
352                 *width = (int) ((*width) * scale + 0.5);
353         if (width)
354                 *height = (int) ((*height) * scale + 0.5);
355
356 }
357
358
359 void
360 ev_page_cache_get_max_width      (EvPageCache *page_cache,
361                                   gfloat       scale,
362                                   gint        *width)
363 {
364         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
365
366         if (width)
367                 *width = page_cache->max_width * scale;
368 }
369
370 void
371 ev_page_cache_get_max_height      (EvPageCache *page_cache,
372                                    gfloat       scale,
373                                    gint        *height)
374 {
375         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
376
377         if (height)
378                 *height = page_cache->max_height * scale;
379 }
380
381 void    
382 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
383                                   gint page,
384                                   gfloat       scale,
385                                   gint        *height,
386                                   gint        *dual_height)
387 {
388         double result = 0.0;
389         double dual_result = 0.0;
390         
391         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
392
393         if (page > 0)
394                 result = page_cache->height_to_page [page - 1]; 
395         
396         if (height)
397                 *height = result * scale;
398
399         if (page > 1)
400                 dual_result = page_cache->dual_height_to_page [page / 2 - 1];   
401         
402         if (dual_height)
403                 *dual_height = dual_result * scale;
404 }
405
406 gint
407 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
408 {
409         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
410         
411         return page_cache->max_label_chars;
412 }
413
414 gchar *
415 ev_page_cache_get_page_label (EvPageCache *page_cache,
416                               gint         page)
417 {
418         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
419         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
420
421         if (page_cache->page_labels[page] == NULL)
422                 return g_strdup_printf ("%d", page + 1);
423
424         return g_strdup (page_cache->page_labels[page]);
425 }
426
427 gboolean
428 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
429 {
430         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
431         return page_cache->has_labels;
432 }
433
434 const EvDocumentInfo *
435 ev_page_cache_get_info (EvPageCache *page_cache)
436 {
437         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
438
439         return page_cache->page_info;
440 }
441
442
443 gboolean
444 ev_page_cache_next_page (EvPageCache *page_cache)
445 {
446         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
447
448         if (page_cache->current_page >= page_cache->n_pages - 1)
449                 return FALSE;
450
451         ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
452         return TRUE;
453
454 }
455
456 gboolean
457 ev_page_cache_prev_page (EvPageCache *page_cache)
458 {
459         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
460
461         if (page_cache->current_page <= 0)
462                 return FALSE;
463
464         ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);
465         return TRUE;
466 }
467
468 #define PAGE_CACHE_STRING "ev-page-cache"
469
470 EvPageCache *
471 ev_page_cache_get (EvDocument *document)
472 {
473         EvPageCache *page_cache;
474
475         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
476
477         page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
478         if (page_cache == NULL) {
479                 page_cache = ev_page_cache_new (document);
480                 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);
481         }
482
483         return page_cache;
484 }