1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
26 gboolean dual_even_left;
29 double uniform_height;
34 double* height_to_page;
35 double* dual_height_to_page;
39 EvPageCacheInfo *size_cache;
40 EvDocumentInfo *page_info;
43 struct _EvPageCacheClass
45 GObjectClass parent_class;
47 void (* page_changed) (EvPageCache *page_cache, gint page);
56 static guint signals[N_SIGNALS] = {0, };
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);
62 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
65 ev_page_cache_init (EvPageCache *page_cache)
67 page_cache->current_page = -1;
68 page_cache->max_label_chars = 0;
72 ev_page_cache_class_init (EvPageCacheClass *class)
74 GObjectClass *object_class;
76 object_class = G_OBJECT_CLASS (class);
78 object_class->finalize = ev_page_cache_finalize;
80 signals [PAGE_CHANGED] =
81 g_signal_new ("page-changed",
84 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
86 g_cclosure_marshal_VOID__INT,
93 ev_page_cache_finalize (GObject *object)
95 EvPageCache *page_cache;
97 page_cache = EV_PAGE_CACHE (object);
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);
104 ev_document_info_free (page_cache->page_info);
108 build_height_to_page (EvPageCache *page_cache)
112 double uniform_height, page_height, next_page_height;
115 swap = (page_cache->rotation == 90 ||
116 page_cache->rotation == 270);
118 g_free (page_cache->height_to_page);
119 g_free (page_cache->dual_height_to_page);
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);
125 for (i = 0; i <= page_cache->n_pages; i++) {
126 if (page_cache->uniform) {
128 uniform_height = page_cache->uniform_height;
130 uniform_height = page_cache->uniform_width;
132 page_cache->height_to_page [i] = i * uniform_height;
135 page_height = page_cache->size_cache [i].height;
137 page_height = page_cache->size_cache [i].width;
139 page_cache->height_to_page [i] = saved_height;
140 saved_height += page_height;
144 if (page_cache->dual_even_left && !page_cache->uniform) {
146 saved_height = page_cache->size_cache [0].height;
148 saved_height = page_cache->size_cache [0].width;
153 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
154 if (page_cache->uniform) {
156 uniform_height = page_cache->uniform_height;
158 uniform_height = page_cache->uniform_width;
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;
164 if (i + 1 < page_cache->n_pages) {
166 next_page_height = page_cache->size_cache [i + 1].height;
168 next_page_height = page_cache->size_cache [i + 1].width;
171 next_page_height = 0;
173 if (i < page_cache->n_pages) {
175 page_height = page_cache->size_cache [i].height;
177 page_height = page_cache->size_cache [i].width;
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);
187 page_cache->dual_height_to_page [i] = saved_height;
194 ev_page_cache_new (EvDocument *document)
196 EvPageCache *page_cache;
197 EvPageCacheInfo *info;
200 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
202 ev_document_doc_mutex_lock ();
204 /* We read page information out of the document */
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);
216 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
217 page_cache->title = g_strdup (page_cache->page_info->title);
219 page_cache->title = NULL;
222 for (i = 0; i < page_cache->n_pages; i++) {
223 double page_width = 0;
224 double page_height = 0;
226 ev_document_get_page_size (document, i, &page_width, &page_height);
228 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
230 if (page_cache->page_labels[i] != NULL) {
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;
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);
244 if (page_width > page_cache->max_width) {
245 page_cache->max_width = page_width;
248 if (page_height > page_cache->max_height) {
249 page_cache->max_height = page_height;
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. */
261 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
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;
268 page_cache->uniform = FALSE;
272 if (! page_cache->uniform) {
273 info = &(page_cache->size_cache [i]);
275 info->width = page_width;
276 info->height = page_height;
280 build_height_to_page (page_cache);
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);
288 ev_document_doc_mutex_unlock ();
290 if (page_cache->n_pages > 0)
291 ev_page_cache_set_current_page (page_cache, 0);
297 ev_page_cache_get_n_pages (EvPageCache *page_cache)
299 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
301 return page_cache->n_pages;
305 ev_page_cache_get_current_page (EvPageCache *page_cache)
307 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
309 return page_cache->current_page;
313 ev_page_cache_set_current_page (EvPageCache *page_cache,
316 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
317 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
319 if (page == page_cache->current_page)
322 page_cache->current_page = page;
323 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
327 ev_page_cache_set_page_label (EvPageCache *page_cache,
328 const char *page_label)
334 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
335 g_return_val_if_fail (page_label != NULL, FALSE);
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);
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);
352 /* convert from a page label to a page offset */
355 page < page_cache->n_pages &&
356 page_cache->page_labels[page] == NULL) {
357 ev_page_cache_set_current_page (page_cache, page);
366 ev_page_cache_get_title (EvPageCache *page_cache)
368 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
370 return page_cache->title;
374 ev_page_cache_get_size (EvPageCache *page_cache,
383 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
384 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
386 if (page_cache->uniform) {
387 w = page_cache->uniform_width;
388 h = page_cache->uniform_height;
390 EvPageCacheInfo *info;
392 info = &(page_cache->size_cache [page]);
401 if (rotation == 0 || rotation == 180) {
402 if (width) *width = (int)w;
403 if (height) *height = (int)h;
405 if (width) *width = (int)h;
406 if (height) *height = (int)w;
411 ev_page_cache_get_max_width (EvPageCache *page_cache,
416 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
419 if (rotation == 0 || rotation == 180) {
420 *width = page_cache->max_width * scale;
422 *width = page_cache->max_height * scale;
428 ev_page_cache_get_max_height (EvPageCache *page_cache,
433 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
436 if (rotation == 0 || rotation == 180) {
437 *height = page_cache->max_height * scale;
439 *height = page_cache->max_width * scale;
445 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
452 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
454 if (page_cache->rotation != rotation) {
455 page_cache->rotation = rotation;
456 build_height_to_page (page_cache);
460 *height = page_cache->height_to_page [page] * scale;
463 *dual_height = page_cache->dual_height_to_page [page] * scale;
467 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
469 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
471 return page_cache->max_label_chars;
475 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
477 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
479 return page_cache->dual_even_left;
483 ev_page_cache_get_page_label (EvPageCache *page_cache,
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);
489 if (page_cache->page_labels[page] == NULL)
490 return g_strdup_printf ("%d", page + 1);
492 return g_strdup (page_cache->page_labels[page]);
496 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
498 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
499 return page_cache->has_labels;
502 const EvDocumentInfo *
503 ev_page_cache_get_info (EvPageCache *page_cache)
505 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
507 return page_cache->page_info;
510 #define PAGE_CACHE_STRING "ev-page-cache"
513 ev_page_cache_get (EvDocument *document)
515 EvPageCache *page_cache;
517 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
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);