]> www.fi.muni.cz Git - evince.git/blob - shell/ev-page-cache.c
Hungarian translation updated.
[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
104         ev_document_info_free (page_cache->page_info);
105 }
106
107 static void
108 build_height_to_page (EvPageCache *page_cache)
109 {
110         gboolean swap;
111         int i;
112         double uniform_height, page_height, next_page_height;
113         double saved_height;
114
115         swap = (page_cache->rotation == 90 ||
116                 page_cache->rotation == 270);
117
118         g_free (page_cache->height_to_page);
119         g_free (page_cache->dual_height_to_page);
120
121         page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
122         page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
123         
124         saved_height = 0;
125         for (i = 0; i <= page_cache->n_pages; i++) {
126                 if (page_cache->uniform) {
127                         if (!swap) {
128                                 uniform_height = page_cache->uniform_height;
129                         } else {
130                                 uniform_height = page_cache->uniform_width;
131                         }
132                         page_cache->height_to_page [i] = i * uniform_height;
133                 } else {
134                         if (!swap) {
135                                 page_height = page_cache->size_cache [i].height;
136                         } else {
137                                 page_height = page_cache->size_cache [i].width;
138                         }
139                         page_cache->height_to_page [i] = saved_height;
140                         saved_height += page_height;
141                 }
142         }
143
144         if (page_cache->dual_even_left && !page_cache->uniform) {
145                 if (!swap) {
146                         saved_height = page_cache->size_cache [0].height;
147                 } else {
148                         saved_height = page_cache->size_cache [0].width;
149                 }
150         } else {
151                 saved_height = 0;
152         }
153         for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
154                 if (page_cache->uniform) {
155                         if (!swap) {
156                                 uniform_height = page_cache->uniform_height;
157                         } else {
158                                 uniform_height = page_cache->uniform_width;
159                         }
160                         page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
161                         if (i + 1 < page_cache->n_pages + 2)
162                                 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
163                 } else {
164                         if (i + 1 < page_cache->n_pages) {
165                                 if (!swap) {
166                                         next_page_height = page_cache->size_cache [i + 1].height;
167                                 } else {
168                                         next_page_height = page_cache->size_cache [i + 1].width;
169                                 }
170                         } else {
171                                 next_page_height = 0;
172                         }
173                         if (i < page_cache->n_pages) {
174                                 if (!swap) {
175                                         page_height = page_cache->size_cache [i].height;
176                                 } else {
177                                         page_height = page_cache->size_cache [i].width;
178                                 }
179                         } else {
180                                 page_height = 0;
181                         }
182                         if (i + 1 < page_cache->n_pages + 2) {
183                                 page_cache->dual_height_to_page [i] = saved_height;
184                                 page_cache->dual_height_to_page [i + 1] = saved_height;
185                                 saved_height += MAX(page_height, next_page_height);
186                         } else {
187                                 page_cache->dual_height_to_page [i] = saved_height;
188                         }
189                 }
190         }
191 }
192
193 EvPageCache *
194 ev_page_cache_new (EvDocument *document)
195 {
196         EvPageCache *page_cache;
197         EvPageCacheInfo *info;
198         gint i;
199
200         page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
201
202         ev_document_doc_mutex_lock ();
203
204         /* We read page information out of the document */
205
206         /* Assume all pages are the same size until proven otherwise */
207         page_cache->uniform = TRUE;
208         page_cache->has_labels = FALSE;
209         page_cache->n_pages = ev_document_get_n_pages (document);
210         page_cache->dual_even_left = (page_cache->n_pages > 2);
211         page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
212         page_cache->max_width = 0;
213         page_cache->max_height = 0;
214         page_cache->page_info = ev_document_get_info (document);
215
216         if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
217                 page_cache->title = g_strdup (page_cache->page_info->title);
218         } else {
219                 page_cache->title = NULL;
220         }
221
222         for (i = 0; i < page_cache->n_pages; i++) {
223                 double page_width = 0;
224                 double page_height = 0;
225                 
226                 ev_document_get_page_size (document, i, &page_width, &page_height);
227
228                 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
229                 
230                 if (page_cache->page_labels[i] != NULL) {
231                 
232                         page_cache->max_label_chars = MAX(page_cache->max_label_chars, 
233                                                             g_utf8_strlen (page_cache->page_labels[i], 256));
234                         if (!page_cache->has_labels) {
235                                 gchar *expected_label;
236                         
237                                 expected_label = g_strdup_printf ("%d", i + 1);
238                                 if (strcmp (expected_label, page_cache->page_labels[i]))  
239                                         page_cache->has_labels = TRUE;
240                                 g_free (expected_label);
241                         }
242                 }
243
244                 if (page_width > page_cache->max_width) {
245                         page_cache->max_width = page_width;
246                 }
247
248                 if (page_height > page_cache->max_height) {
249                         page_cache->max_height = page_height;
250                 }
251                         
252                 if (i == 0) {
253                         page_cache->uniform_width = page_width;
254                         page_cache->uniform_height = page_height;
255                 } else if (page_cache->uniform &&
256                            (page_cache->uniform_width != page_width ||
257                             page_cache->uniform_height != page_height)) {
258                         /* It's a different page size.  Backfill the array. */
259                         int j;
260
261                         page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
262
263                         for (j = 0; j < i; j++) {
264                                 info = &(page_cache->size_cache [j]);
265                                 info->width = page_cache->uniform_width;
266                                 info->height = page_cache->uniform_height;
267                         }
268                         page_cache->uniform = FALSE;
269
270                 }
271
272                 if (! page_cache->uniform) {
273                         info = &(page_cache->size_cache [i]);
274
275                         info->width = page_width;
276                         info->height = page_height;
277                 }
278         }
279
280         build_height_to_page (page_cache);
281
282         /* make some sanity check assertions */
283         if (! page_cache->uniform)
284                 g_assert (page_cache->size_cache != NULL);
285         if (page_cache->uniform && page_cache->n_pages > 0)
286                 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
287
288         ev_document_doc_mutex_unlock ();
289
290         if (page_cache->n_pages > 0)
291                 ev_page_cache_set_current_page (page_cache, 0);
292
293         return page_cache;
294 }
295
296 gint
297 ev_page_cache_get_n_pages (EvPageCache *page_cache)
298 {
299         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
300
301         return page_cache->n_pages;
302 }
303
304 gint
305 ev_page_cache_get_current_page (EvPageCache *page_cache)
306 {
307         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
308
309         return page_cache->current_page;
310 }
311
312 void
313 ev_page_cache_set_current_page (EvPageCache *page_cache,
314                                 int          page)
315 {
316         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
317         g_return_if_fail (page >= 0 || page < page_cache->n_pages);
318
319         if (page == page_cache->current_page)
320                 return;
321
322         page_cache->current_page = page;
323         g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
324 }
325
326 gboolean
327 ev_page_cache_set_page_label (EvPageCache *page_cache,
328                               const char  *page_label)
329 {
330         gint i, page;
331         long value;
332         char *endptr = NULL;
333         
334         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
335         g_return_val_if_fail (page_label != NULL, FALSE);
336
337         /* First, look for a literal label match */
338         for (i = 0; i < page_cache->n_pages; i ++) {
339                 if (page_cache->page_labels[i] != NULL &&
340                     ! strcmp (page_label, page_cache->page_labels[i])) {
341                         ev_page_cache_set_current_page (page_cache, i);
342                         return TRUE;
343                 }
344         }
345
346         /* Next, parse the label, and see if the number fits */
347         value = strtol (page_label, &endptr, 10);
348         if (endptr[0] == '\0') {
349                 /* Page number is an integer */
350                 page = MIN (G_MAXINT, value);
351
352                 /* convert from a page label to a page offset */
353                 page --;
354                 if (page >= 0 &&
355                     page < page_cache->n_pages &&
356                     page_cache->page_labels[page] == NULL) {
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 }