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