2 #include "ev-page-cache.h"
3 #include "ev-job-queue.h"
4 #include "ev-document-thumbnails.h"
8 #define THUMBNAIL_WIDTH 100
10 typedef struct _EvPageCacheInfo
16 typedef struct _EvPageThumbsInfo
34 gboolean dual_even_left;
37 double uniform_height;
42 double* height_to_page;
43 double* dual_height_to_page;
47 EvPageCacheInfo *size_cache;
48 EvDocumentInfo *page_info;
50 /* Thumbnail dimensions */
51 gboolean thumbs_uniform;
52 gint thumbs_uniform_width;
53 gint thumbs_uniform_height;
54 gint thumbs_max_width;
55 gint thumbs_max_height;
56 EvPageThumbsInfo *thumbs_size_cache;
59 struct _EvPageCacheClass
61 GObjectClass parent_class;
63 void (* page_changed) (EvPageCache *page_cache, gint page);
64 void (* history_changed) (EvPageCache *page_cache, gint page);
74 static guint signals[N_SIGNALS] = {0, };
76 static void ev_page_cache_init (EvPageCache *page_cache);
77 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
78 static void ev_page_cache_finalize (GObject *object);
80 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
83 ev_page_cache_init (EvPageCache *page_cache)
85 page_cache->current_page = -1;
86 page_cache->max_label_chars = 0;
90 ev_page_cache_class_init (EvPageCacheClass *class)
92 GObjectClass *object_class;
94 object_class = G_OBJECT_CLASS (class);
96 object_class->finalize = ev_page_cache_finalize;
98 signals [PAGE_CHANGED] =
99 g_signal_new ("page-changed",
102 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
104 g_cclosure_marshal_VOID__INT,
108 signals [HISTORY_CHANGED] =
109 g_signal_new ("history-changed",
112 G_STRUCT_OFFSET (EvPageCacheClass, history_changed),
114 g_cclosure_marshal_VOID__INT,
121 ev_page_cache_finalize (GObject *object)
123 EvPageCache *page_cache;
125 page_cache = EV_PAGE_CACHE (object);
127 if (page_cache->title) {
128 g_free (page_cache->title);
129 page_cache->title = NULL;
132 if (page_cache->size_cache) {
133 g_free (page_cache->size_cache);
134 page_cache->size_cache = NULL;
137 if (page_cache->height_to_page) {
138 g_free (page_cache->height_to_page);
139 page_cache->height_to_page = NULL;
142 if (page_cache->dual_height_to_page) {
143 g_free (page_cache->dual_height_to_page);
144 page_cache->dual_height_to_page = NULL;
147 if (page_cache->page_labels) {
150 for (i = 0; i < page_cache->n_pages; i++) {
151 if (page_cache->page_labels[i])
152 g_free (page_cache->page_labels[i]);
154 g_free (page_cache->page_labels);
155 page_cache->page_labels = NULL;
158 if (page_cache->page_info) {
159 ev_document_info_free (page_cache->page_info);
160 page_cache->page_info = NULL;
163 G_OBJECT_CLASS (ev_page_cache_parent_class)->finalize (object);
167 build_height_to_page (EvPageCache *page_cache)
171 double uniform_height, page_height, next_page_height;
174 swap = (page_cache->rotation == 90 ||
175 page_cache->rotation == 270);
177 g_free (page_cache->height_to_page);
178 g_free (page_cache->dual_height_to_page);
180 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
181 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
184 for (i = 0; i <= page_cache->n_pages; i++) {
185 if (page_cache->uniform) {
187 uniform_height = page_cache->uniform_height;
189 uniform_height = page_cache->uniform_width;
191 page_cache->height_to_page [i] = i * uniform_height;
193 if (i < page_cache->n_pages) {
195 page_height = page_cache->size_cache [i].height;
197 page_height = page_cache->size_cache [i].width;
202 page_cache->height_to_page [i] = saved_height;
203 saved_height += page_height;
207 if (page_cache->dual_even_left && !page_cache->uniform) {
209 saved_height = page_cache->size_cache [0].height;
211 saved_height = page_cache->size_cache [0].width;
216 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
217 if (page_cache->uniform) {
219 uniform_height = page_cache->uniform_height;
221 uniform_height = page_cache->uniform_width;
223 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
224 if (i + 1 < page_cache->n_pages + 2)
225 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
227 if (i + 1 < page_cache->n_pages) {
229 next_page_height = page_cache->size_cache [i + 1].height;
231 next_page_height = page_cache->size_cache [i + 1].width;
234 next_page_height = 0;
236 if (i < page_cache->n_pages) {
238 page_height = page_cache->size_cache [i].height;
240 page_height = page_cache->size_cache [i].width;
245 if (i + 1 < page_cache->n_pages + 2) {
246 page_cache->dual_height_to_page [i] = saved_height;
247 page_cache->dual_height_to_page [i + 1] = saved_height;
248 saved_height += MAX(page_height, next_page_height);
250 page_cache->dual_height_to_page [i] = saved_height;
257 ev_page_cache_new (EvDocument *document)
259 EvPageCache *page_cache;
260 EvPageCacheInfo *info;
261 EvPageThumbsInfo *thumb_info;
262 EvRenderContext *rc = NULL;
266 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
268 ev_document_doc_mutex_lock ();
270 /* We read page information out of the document */
272 /* Assume all pages are the same size until proven otherwise */
273 page_cache->uniform = TRUE;
274 page_cache->has_labels = FALSE;
275 page_cache->n_pages = ev_document_get_n_pages (document);
276 page_cache->dual_even_left = (page_cache->n_pages > 2);
277 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
278 page_cache->max_width = 0;
279 page_cache->max_height = 0;
280 page_cache->page_info = ev_document_get_info (document);
281 page_cache->thumbs_uniform = TRUE;
283 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
284 page_cache->title = g_strdup (page_cache->page_info->title);
286 page_cache->title = NULL;
289 has_thumbs = EV_IS_DOCUMENT_THUMBNAILS (document);
291 for (i = 0; i < page_cache->n_pages; i++) {
292 double page_width = 0;
293 double page_height = 0;
294 gint thumb_width = 0;
295 gint thumb_height = 0;
297 ev_document_get_page_size (document, i, &page_width, &page_height);
299 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
301 if (page_cache->page_labels[i] != NULL) {
303 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
304 g_utf8_strlen (page_cache->page_labels[i], 256));
305 if (!page_cache->has_labels) {
306 gchar *expected_label;
308 expected_label = g_strdup_printf ("%d", i + 1);
309 if (strcmp (expected_label, page_cache->page_labels[i]))
310 page_cache->has_labels = TRUE;
311 g_free (expected_label);
315 if (page_width > page_cache->max_width) {
316 page_cache->max_width = page_width;
319 if (page_height > page_cache->max_height) {
320 page_cache->max_height = page_height;
324 page_cache->uniform_width = page_width;
325 page_cache->uniform_height = page_height;
326 } else if (page_cache->uniform &&
327 (page_cache->uniform_width != page_width ||
328 page_cache->uniform_height != page_height)) {
329 /* It's a different page size. Backfill the array. */
332 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
334 for (j = 0; j < i; j++) {
335 info = &(page_cache->size_cache [j]);
336 info->width = page_cache->uniform_width;
337 info->height = page_cache->uniform_height;
339 page_cache->uniform = FALSE;
343 if (! page_cache->uniform) {
344 info = &(page_cache->size_cache [i]);
346 info->width = page_width;
347 info->height = page_height;
354 rc = ev_render_context_new (0, i, (gdouble)THUMBNAIL_WIDTH / page_width);
356 ev_render_context_set_page (rc, i);
357 ev_render_context_set_scale (rc, (gdouble)THUMBNAIL_WIDTH / page_width);
360 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (document),
361 rc, &thumb_width, &thumb_height);
363 if (thumb_width > page_cache->thumbs_max_width) {
364 page_cache->thumbs_max_width = thumb_width;
367 if (thumb_height > page_cache->thumbs_max_height) {
368 page_cache->thumbs_max_height = thumb_height;
372 page_cache->thumbs_uniform_width = thumb_width;
373 page_cache->thumbs_uniform_height = thumb_height;
374 } else if (page_cache->thumbs_uniform &&
375 (page_cache->thumbs_uniform_width != thumb_width ||
376 page_cache->thumbs_uniform_height != thumb_height)) {
377 /* It's a different thumbnail size. Backfill the array. */
380 page_cache->thumbs_size_cache = g_new0 (EvPageThumbsInfo, page_cache->n_pages);
382 for (j = 0; j < i; j++) {
383 thumb_info = &(page_cache->thumbs_size_cache [j]);
384 thumb_info->width = page_cache->thumbs_uniform_width;
385 thumb_info->height = page_cache->thumbs_uniform_height;
387 page_cache->thumbs_uniform = FALSE;
390 if (! page_cache->thumbs_uniform) {
391 thumb_info = &(page_cache->thumbs_size_cache [i]);
393 thumb_info->width = thumb_width;
394 thumb_info->height = thumb_height;
402 build_height_to_page (page_cache);
404 /* make some sanity check assertions */
405 if (! page_cache->uniform)
406 g_assert (page_cache->size_cache != NULL);
407 if (page_cache->uniform && page_cache->n_pages > 0)
408 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
410 ev_document_doc_mutex_unlock ();
412 if (page_cache->n_pages > 0)
413 ev_page_cache_set_current_page (page_cache, 0);
419 ev_page_cache_get_n_pages (EvPageCache *page_cache)
421 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
423 return page_cache->n_pages;
427 ev_page_cache_get_current_page (EvPageCache *page_cache)
429 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
431 return page_cache->current_page;
435 ev_page_cache_set_current_page (EvPageCache *page_cache,
438 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
439 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
441 if (page == page_cache->current_page)
444 page_cache->current_page = page;
445 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
449 ev_page_cache_set_current_page_history (EvPageCache *page_cache,
452 if (abs (page - page_cache->current_page) > 1)
453 g_signal_emit (page_cache, signals [HISTORY_CHANGED], 0, page);
455 ev_page_cache_set_current_page (page_cache, page);
459 ev_page_cache_set_page_label (EvPageCache *page_cache,
460 const char *page_label)
466 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
467 g_return_val_if_fail (page_label != NULL, FALSE);
469 /* First, look for a literal label match */
470 for (i = 0; i < page_cache->n_pages; i ++) {
471 if (page_cache->page_labels[i] != NULL &&
472 ! strcmp (page_label, page_cache->page_labels[i])) {
473 ev_page_cache_set_current_page (page_cache, i);
478 /* Next, parse the label, and see if the number fits */
479 value = strtol (page_label, &endptr, 10);
480 if (endptr[0] == '\0') {
481 /* Page number is an integer */
482 page = MIN (G_MAXINT, value);
484 /* convert from a page label to a page offset */
487 page < page_cache->n_pages) {
488 ev_page_cache_set_current_page (page_cache, page);
497 ev_page_cache_get_title (EvPageCache *page_cache)
499 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
501 return page_cache->title;
505 ev_page_cache_get_size (EvPageCache *page_cache,
514 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
515 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
517 if (page_cache->uniform) {
518 w = page_cache->uniform_width;
519 h = page_cache->uniform_height;
521 EvPageCacheInfo *info;
523 info = &(page_cache->size_cache [page]);
532 if (rotation == 0 || rotation == 180) {
533 if (width) *width = (int)w;
534 if (height) *height = (int)h;
536 if (width) *width = (int)h;
537 if (height) *height = (int)w;
542 ev_page_cache_get_max_width (EvPageCache *page_cache,
547 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
550 if (rotation == 0 || rotation == 180) {
551 *width = page_cache->max_width * scale;
553 *width = page_cache->max_height * scale;
559 ev_page_cache_get_max_height (EvPageCache *page_cache,
564 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
567 if (rotation == 0 || rotation == 180) {
568 *height = page_cache->max_height * scale;
570 *height = page_cache->max_width * scale;
576 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
583 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
584 g_return_if_fail (page >= 0);
585 g_return_if_fail (!height || page <= page_cache->n_pages);
586 g_return_if_fail (!dual_height || page <= page_cache->n_pages + 1);
588 if (page_cache->rotation != rotation) {
589 page_cache->rotation = rotation;
590 build_height_to_page (page_cache);
594 *height = page_cache->height_to_page [page] * scale;
597 *dual_height = page_cache->dual_height_to_page [page] * scale;
601 ev_page_cache_get_thumbnail_size (EvPageCache *page_cache,
609 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
610 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
612 if (page_cache->thumbs_uniform) {
613 w = page_cache->thumbs_uniform_width;
614 h = page_cache->thumbs_uniform_height;
616 EvPageThumbsInfo *info;
618 info = &(page_cache->thumbs_size_cache [page]);
624 if (rotation == 0 || rotation == 180) {
625 if (width) *width = w;
626 if (height) *height = h;
628 if (width) *width = h;
629 if (height) *height = w;
634 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
636 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
638 return page_cache->max_label_chars;
642 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
644 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
646 return page_cache->dual_even_left;
650 ev_page_cache_get_page_label (EvPageCache *page_cache,
653 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
654 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
656 if (page_cache->page_labels[page] == NULL)
657 return g_strdup_printf ("%d", page + 1);
659 return g_strdup (page_cache->page_labels[page]);
663 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
665 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
666 return page_cache->has_labels;
669 const EvDocumentInfo *
670 ev_page_cache_get_info (EvPageCache *page_cache)
672 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
674 return page_cache->page_info;
677 #define PAGE_CACHE_STRING "ev-page-cache"
680 ev_page_cache_get (EvDocument *document)
682 EvPageCache *page_cache;
684 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
686 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
687 if (page_cache == NULL) {
688 page_cache = ev_page_cache_new (document);
689 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);