]> www.fi.muni.cz Git - evince.git/blob - backend/ev-page-cache.c
6302c42e03b973fcc8b79bdd8cdaf62d3b6d1346
[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         gboolean has_labels;
24         gboolean uniform;
25         
26         double uniform_width;
27         double uniform_height;
28
29         double max_width_page_width;
30         double max_width_page_height;
31         double max_height_page_width;
32         double max_height_page_height;
33
34         EvPageCacheInfo *size_cache;
35 };
36
37 struct _EvPageCacheClass
38 {
39         GObjectClass parent_class;
40
41         void (* page_changed) (EvPageCache *page_cache, gint page);
42 };
43
44 enum
45 {
46         PAGE_CHANGED,
47         N_SIGNALS,
48 };
49
50 static guint signals[N_SIGNALS] = {0, };
51
52 static void ev_page_cache_init       (EvPageCache      *page_cache);
53 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
54 static void ev_page_cache_finalize   (GObject *object);
55
56 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
57
58 static void
59 ev_page_cache_init (EvPageCache *page_cache)
60 {
61         page_cache->current_page = -1;
62 }
63
64 static void
65 ev_page_cache_class_init (EvPageCacheClass *class)
66 {
67         GObjectClass *object_class;
68
69         object_class = G_OBJECT_CLASS (class);
70
71         object_class->finalize = ev_page_cache_finalize;
72
73         signals [PAGE_CHANGED] =
74                 g_signal_new ("page-changed",
75                               EV_TYPE_PAGE_CACHE,
76                               G_SIGNAL_RUN_LAST,
77                               G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
78                               NULL, NULL,
79                               g_cclosure_marshal_VOID__INT,
80                               G_TYPE_NONE, 1,
81                               G_TYPE_INT);
82
83 }
84
85 static void
86 ev_page_cache_finalize (GObject *object)
87 {
88         EvPageCache *page_cache;
89
90         page_cache = EV_PAGE_CACHE (object);
91
92         g_free (page_cache->title);
93         g_free (page_cache->size_cache);
94 }
95
96 EvPageCache *
97 _ev_page_cache_new (EvDocument *document)
98 {
99         EvDocumentInfo *doc_info;
100         EvPageCache *page_cache;
101         EvPageCacheInfo *info;
102         gint i;
103
104         page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
105
106         ev_document_doc_mutex_lock ();
107
108         /* We read page information out of the document */
109
110         /* Assume all pages are the same size until proven otherwise */
111         page_cache->uniform = TRUE;
112         page_cache->has_labels = FALSE;
113         page_cache->n_pages = ev_document_get_n_pages (document);
114         page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
115         page_cache->max_width_page_width = 0;
116         page_cache->max_width_page_height = 0;
117         page_cache->max_height_page_width = 0;
118         page_cache->max_height_page_height = 0;
119
120         doc_info = ev_document_get_info (document);
121         if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
122                 page_cache->title = g_strdup (doc_info->title);
123         } else {
124                 page_cache->title = NULL;
125         }
126         g_free (doc_info);
127
128         for (i = 0; i < page_cache->n_pages; i++) {
129                 double page_width = 0;
130                 double page_height = 0;
131                 
132                 ev_document_get_page_size (document, i, &page_width, &page_height);
133
134                 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
135
136                 if (!page_cache->has_labels && page_cache->page_labels[i] != NULL) {
137                         gchar *expected_label;
138                         
139                         expected_label = g_strdup_printf ("%d", i + 1);
140                         if (strcmp (expected_label, page_cache->page_labels[i]))  
141                                 page_cache->has_labels = TRUE;
142                         g_free (expected_label);
143                 }
144
145                 if (page_width > page_cache->max_width_page_width) {
146                         page_cache->max_width_page_width = page_width;
147                         page_cache->max_width_page_height = page_height;
148                 }
149
150                 if (page_height > page_cache->max_height_page_height) {
151                         page_cache->max_height_page_width = page_width;
152                         page_cache->max_height_page_height = page_height;
153                 }
154
155                 if (i == 0) {
156                         page_cache->uniform_width = page_width;
157                         page_cache->uniform_height = page_height;
158                 } else if (page_cache->uniform &&
159                            (page_cache->uniform_width != page_width ||
160                             page_cache->uniform_height != page_height)) {
161                         /* It's a different page size.  Backfill the array. */
162                         int j;
163
164                         page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
165
166                         for (j = 0; j < i; j++) {
167                                 info = &(page_cache->size_cache [j]);
168                                 info->width = page_cache->uniform_width;
169                                 info->height = page_cache->uniform_height;
170                         }
171                         page_cache->uniform = FALSE;
172
173                 }
174
175                 if (! page_cache->uniform) {
176                         info = &(page_cache->size_cache [i]);
177
178                         info->width = page_width;
179                         info->height = page_height;
180                 }
181         }
182
183         /* make some sanity check assertions */
184         if (! page_cache->uniform)
185                 g_assert (page_cache->size_cache != NULL);
186         if (page_cache->uniform)
187                 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
188
189         ev_document_doc_mutex_unlock ();
190
191         if (page_cache->n_pages > 0)
192                 ev_page_cache_set_current_page (page_cache, 0);
193
194         return page_cache;
195 }
196
197 gint
198 ev_page_cache_get_n_pages (EvPageCache *page_cache)
199 {
200         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
201
202         return page_cache->n_pages;
203 }
204
205 gint
206 ev_page_cache_get_current_page (EvPageCache *page_cache)
207 {
208         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
209
210         return page_cache->current_page;
211 }
212
213 void
214 ev_page_cache_set_current_page (EvPageCache *page_cache,
215                                 int          page)
216 {
217         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
218         g_return_if_fail (page >= 0 || page < page_cache->n_pages);
219
220         if (page == page_cache->current_page)
221                 return;
222
223         page_cache->current_page = page;
224         g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
225 }
226
227 gboolean
228 ev_page_cache_set_page_label (EvPageCache *page_cache,
229                               const char  *page_label)
230 {
231         gint i, page;
232         long value;
233         char *endptr = NULL;
234         
235         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
236         g_return_val_if_fail (page_label != NULL, FALSE);
237
238         /* First, look for a literal label match */
239         for (i = 0; i < page_cache->n_pages; i ++) {
240                 if (page_cache->page_labels[i] != NULL &&
241                     ! strcmp (page_label, page_cache->page_labels[i])) {
242                         ev_page_cache_set_current_page (page_cache, i);
243                         return TRUE;
244                 }
245         }
246
247         /* Next, parse the label, and see if the number fits */
248         value = strtol (page_label, &endptr, 10);
249         if (endptr[0] == '\0') {
250                 /* Page number is an integer */
251                 page = MIN (G_MAXINT, value);
252
253                 /* convert from a page label to a page offset */
254                 page --;
255                 if (page >= 0 &&
256                     page < page_cache->n_pages &&
257                     page_cache->page_labels[page] == NULL) {
258                         ev_page_cache_set_current_page (page_cache, page);
259                         return TRUE;
260                 }
261         }
262
263         return FALSE;
264 }
265
266 void
267 ev_page_cache_set_link (EvPageCache *page_cache,
268                         EvLink      *link)
269 {
270         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
271         g_return_if_fail (EV_IS_LINK (link));
272
273         ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
274 }
275
276 char *
277 ev_page_cache_get_title (EvPageCache *page_cache)
278 {
279         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
280
281         return page_cache->title;
282 }
283
284 void
285 ev_page_cache_get_size (EvPageCache *page_cache,
286                         gint         page,
287                         gfloat       scale,
288                         gint        *width,
289                         gint        *height)
290 {
291         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
292         g_return_if_fail (page >= 0 && page < page_cache->n_pages);
293
294         if (page_cache->uniform) {
295                 if (width)
296                         *width = page_cache->uniform_width;
297                 if (height)
298                         *height = page_cache->uniform_height;
299         } else {
300                 EvPageCacheInfo *info;
301
302                 info = &(page_cache->size_cache [page]);
303                 
304                 if (width)
305                         *width = info->width;
306                 if (height)
307                         *height = info->height;
308         }
309
310         if (width)
311                 *width = (int) ((*width) * scale + 0.5);
312         if (width)
313                 *height = (int) ((*height) * scale + 0.5);
314
315 }
316
317
318 /* Note that these aren't necessarily from the same page.
319  */
320 void
321 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
322                                   gfloat       scale,
323                                   gint        *width,
324                                   gint        *height)
325 {
326         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
327
328         if (width)
329                 *width = page_cache->max_width_page_width * scale;
330         if (height)
331                 *height = page_cache->max_width_page_height * scale;
332 }
333
334 void
335 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
336                                    gfloat       scale,
337                                    gint        *width,
338                                    gint        *height)
339 {
340         g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
341
342         if (width)
343                 *width = page_cache->max_height_page_width * scale;
344         if (height)
345                 *height = page_cache->max_height_page_height * scale;
346 }
347
348
349 gchar *
350 ev_page_cache_get_page_label (EvPageCache *page_cache,
351                               gint         page)
352 {
353         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
354         g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
355
356         if (page_cache->page_labels[page] == NULL)
357                 return g_strdup_printf ("%d", page + 1);
358
359         return g_strdup (page_cache->page_labels[page]);
360 }
361
362 gboolean
363 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
364 {
365         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
366         return page_cache->has_labels;
367 }
368
369 gboolean
370 ev_page_cache_next_page (EvPageCache *page_cache)
371 {
372         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
373
374         if (page_cache->current_page >= page_cache->n_pages - 1)
375                 return FALSE;
376
377         ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
378         return TRUE;
379
380 }
381
382 gboolean
383 ev_page_cache_prev_page (EvPageCache *page_cache)
384 {
385         g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
386
387         if (page_cache->current_page <= 0)
388                 return FALSE;
389
390         ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);
391         return TRUE;
392 }
393