2 #include "ev-page-cache.h"
3 #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->thumbs_size_cache) {
138 g_free (page_cache->thumbs_size_cache);
139 page_cache->thumbs_size_cache = NULL;
142 if (page_cache->height_to_page) {
143 g_free (page_cache->height_to_page);
144 page_cache->height_to_page = NULL;
147 if (page_cache->dual_height_to_page) {
148 g_free (page_cache->dual_height_to_page);
149 page_cache->dual_height_to_page = NULL;
152 if (page_cache->page_labels) {
155 for (i = 0; i < page_cache->n_pages; i++) {
156 if (page_cache->page_labels[i])
157 g_free (page_cache->page_labels[i]);
159 g_free (page_cache->page_labels);
160 page_cache->page_labels = NULL;
163 if (page_cache->page_info) {
164 ev_document_info_free (page_cache->page_info);
165 page_cache->page_info = NULL;
168 G_OBJECT_CLASS (ev_page_cache_parent_class)->finalize (object);
172 build_height_to_page (EvPageCache *page_cache)
176 double uniform_height, page_height, next_page_height;
179 swap = (page_cache->rotation == 90 ||
180 page_cache->rotation == 270);
182 g_free (page_cache->height_to_page);
183 g_free (page_cache->dual_height_to_page);
185 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
186 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
189 for (i = 0; i <= page_cache->n_pages; i++) {
190 if (page_cache->uniform) {
192 uniform_height = page_cache->uniform_height;
194 uniform_height = page_cache->uniform_width;
196 page_cache->height_to_page [i] = i * uniform_height;
198 if (i < page_cache->n_pages) {
200 page_height = page_cache->size_cache [i].height;
202 page_height = page_cache->size_cache [i].width;
207 page_cache->height_to_page [i] = saved_height;
208 saved_height += page_height;
212 if (page_cache->dual_even_left && !page_cache->uniform) {
214 saved_height = page_cache->size_cache [0].height;
216 saved_height = page_cache->size_cache [0].width;
221 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
222 if (page_cache->uniform) {
224 uniform_height = page_cache->uniform_height;
226 uniform_height = page_cache->uniform_width;
228 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
229 if (i + 1 < page_cache->n_pages + 2)
230 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
232 if (i + 1 < page_cache->n_pages) {
234 next_page_height = page_cache->size_cache [i + 1].height;
236 next_page_height = page_cache->size_cache [i + 1].width;
239 next_page_height = 0;
241 if (i < page_cache->n_pages) {
243 page_height = page_cache->size_cache [i].height;
245 page_height = page_cache->size_cache [i].width;
250 if (i + 1 < page_cache->n_pages + 2) {
251 page_cache->dual_height_to_page [i] = saved_height;
252 page_cache->dual_height_to_page [i + 1] = saved_height;
253 saved_height += MAX(page_height, next_page_height);
255 page_cache->dual_height_to_page [i] = saved_height;
262 ev_page_cache_new (EvDocument *document)
264 EvPageCache *page_cache;
265 EvPageCacheInfo *info;
266 EvPageThumbsInfo *thumb_info;
267 EvRenderContext *rc = NULL;
271 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
273 ev_document_doc_mutex_lock ();
275 /* We read page information out of the document */
277 /* Assume all pages are the same size until proven otherwise */
278 page_cache->uniform = TRUE;
279 page_cache->has_labels = FALSE;
280 page_cache->n_pages = ev_document_get_n_pages (document);
281 page_cache->dual_even_left = (page_cache->n_pages > 2);
282 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
283 page_cache->max_width = 0;
284 page_cache->max_height = 0;
285 page_cache->page_info = ev_document_get_info (document);
286 page_cache->thumbs_uniform = TRUE;
288 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
289 page_cache->title = g_strdup (page_cache->page_info->title);
291 page_cache->title = NULL;
294 has_thumbs = EV_IS_DOCUMENT_THUMBNAILS (document);
296 for (i = 0; i < page_cache->n_pages; i++) {
298 double page_width = 0;
299 double page_height = 0;
300 gint thumb_width = 0;
301 gint thumb_height = 0;
303 page = ev_document_get_page (document, i);
305 ev_document_get_page_size (document, page, &page_width, &page_height);
307 page_cache->page_labels[i] = ev_document_get_page_label (document, page);
309 if (page_cache->page_labels[i] != NULL) {
311 page_cache->max_label_chars = MAX (page_cache->max_label_chars,
312 g_utf8_strlen (page_cache->page_labels[i], 256));
313 if (!page_cache->has_labels) {
314 gchar *expected_label;
316 expected_label = g_strdup_printf ("%d", i + 1);
317 if (strcmp (expected_label, page_cache->page_labels[i]))
318 page_cache->has_labels = TRUE;
319 g_free (expected_label);
323 if (page_width > page_cache->max_width) {
324 page_cache->max_width = page_width;
327 if (page_height > page_cache->max_height) {
328 page_cache->max_height = page_height;
332 page_cache->uniform_width = page_width;
333 page_cache->uniform_height = page_height;
334 } else if (page_cache->uniform &&
335 (page_cache->uniform_width != page_width ||
336 page_cache->uniform_height != page_height)) {
337 /* It's a different page size. Backfill the array. */
340 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
342 for (j = 0; j < i; j++) {
343 info = &(page_cache->size_cache [j]);
344 info->width = page_cache->uniform_width;
345 info->height = page_cache->uniform_height;
347 page_cache->uniform = FALSE;
351 if (! page_cache->uniform) {
352 info = &(page_cache->size_cache [i]);
354 info->width = page_width;
355 info->height = page_height;
359 g_object_unref (page);
364 rc = ev_render_context_new (page, 0, (gdouble)THUMBNAIL_WIDTH / page_width);
366 ev_render_context_set_page (rc, page);
367 ev_render_context_set_scale (rc, (gdouble)THUMBNAIL_WIDTH / page_width);
370 ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (document),
371 rc, &thumb_width, &thumb_height);
373 if (thumb_width > page_cache->thumbs_max_width) {
374 page_cache->thumbs_max_width = thumb_width;
377 if (thumb_height > page_cache->thumbs_max_height) {
378 page_cache->thumbs_max_height = thumb_height;
382 page_cache->thumbs_uniform_width = thumb_width;
383 page_cache->thumbs_uniform_height = thumb_height;
384 } else if (page_cache->thumbs_uniform &&
385 (page_cache->thumbs_uniform_width != thumb_width ||
386 page_cache->thumbs_uniform_height != thumb_height)) {
387 /* It's a different thumbnail size. Backfill the array. */
390 page_cache->thumbs_size_cache = g_new0 (EvPageThumbsInfo, page_cache->n_pages);
392 for (j = 0; j < i; j++) {
393 thumb_info = &(page_cache->thumbs_size_cache [j]);
394 thumb_info->width = page_cache->thumbs_uniform_width;
395 thumb_info->height = page_cache->thumbs_uniform_height;
397 page_cache->thumbs_uniform = FALSE;
400 if (! page_cache->thumbs_uniform) {
401 thumb_info = &(page_cache->thumbs_size_cache [i]);
403 thumb_info->width = thumb_width;
404 thumb_info->height = thumb_height;
407 g_object_unref (page);
414 build_height_to_page (page_cache);
416 /* make some sanity check assertions */
417 if (! page_cache->uniform)
418 g_assert (page_cache->size_cache != NULL);
419 if (page_cache->uniform && page_cache->n_pages > 0)
420 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
422 ev_document_doc_mutex_unlock ();
424 if (page_cache->n_pages > 0)
425 ev_page_cache_set_current_page (page_cache, 0);
431 ev_page_cache_get_n_pages (EvPageCache *page_cache)
433 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
435 return page_cache->n_pages;
439 ev_page_cache_get_current_page (EvPageCache *page_cache)
441 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
443 return page_cache->current_page;
447 ev_page_cache_set_current_page (EvPageCache *page_cache,
450 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
451 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
453 if (page == page_cache->current_page)
456 page_cache->current_page = page;
457 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
461 ev_page_cache_set_current_page_history (EvPageCache *page_cache,
464 if (abs (page - page_cache->current_page) > 1)
465 g_signal_emit (page_cache, signals [HISTORY_CHANGED], 0, page);
467 ev_page_cache_set_current_page (page_cache, page);
471 ev_page_cache_set_page_label (EvPageCache *page_cache,
472 const char *page_label)
478 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
479 g_return_val_if_fail (page_label != NULL, FALSE);
481 /* First, look for a literal label match */
482 for (i = 0; i < page_cache->n_pages; i ++) {
483 if (page_cache->page_labels[i] != NULL &&
484 ! strcmp (page_label, page_cache->page_labels[i])) {
485 ev_page_cache_set_current_page (page_cache, i);
490 /* Second, look for a match with case insensitively */
491 for (i = 0; i < page_cache->n_pages; i++) {
492 if (page_cache->page_labels[i] != NULL &&
493 ! strcasecmp (page_label, page_cache->page_labels[i])) {
494 ev_page_cache_set_current_page (page_cache, i);
499 /* Next, parse the label, and see if the number fits */
500 value = strtol (page_label, &endptr, 10);
501 if (endptr[0] == '\0') {
502 /* Page number is an integer */
503 page = MIN (G_MAXINT, value);
505 /* convert from a page label to a page offset */
508 page < page_cache->n_pages) {
509 ev_page_cache_set_current_page (page_cache, page);
518 ev_page_cache_get_title (EvPageCache *page_cache)
520 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
522 return page_cache->title;
526 ev_page_cache_get_size (EvPageCache *page_cache,
535 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
536 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
538 if (page_cache->uniform) {
539 w = page_cache->uniform_width;
540 h = page_cache->uniform_height;
542 EvPageCacheInfo *info;
544 info = &(page_cache->size_cache [page]);
553 if (rotation == 0 || rotation == 180) {
554 if (width) *width = (int)w;
555 if (height) *height = (int)h;
557 if (width) *width = (int)h;
558 if (height) *height = (int)w;
563 ev_page_cache_get_max_width (EvPageCache *page_cache,
568 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
571 if (rotation == 0 || rotation == 180) {
572 *width = page_cache->max_width * scale;
574 *width = page_cache->max_height * scale;
580 ev_page_cache_get_max_height (EvPageCache *page_cache,
585 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
588 if (rotation == 0 || rotation == 180) {
589 *height = page_cache->max_height * scale;
591 *height = page_cache->max_width * scale;
597 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
604 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
605 g_return_if_fail (page >= 0);
606 g_return_if_fail (!height || page <= page_cache->n_pages);
607 g_return_if_fail (!dual_height || page <= page_cache->n_pages + 1);
609 if (page_cache->rotation != rotation) {
610 page_cache->rotation = rotation;
611 build_height_to_page (page_cache);
615 *height = page_cache->height_to_page [page] * scale;
618 *dual_height = page_cache->dual_height_to_page [page] * scale;
622 ev_page_cache_get_thumbnail_size (EvPageCache *page_cache,
630 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
631 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
633 if (page_cache->thumbs_uniform) {
634 w = page_cache->thumbs_uniform_width;
635 h = page_cache->thumbs_uniform_height;
637 EvPageThumbsInfo *info;
639 info = &(page_cache->thumbs_size_cache [page]);
645 if (rotation == 0 || rotation == 180) {
646 if (width) *width = w;
647 if (height) *height = h;
649 if (width) *width = h;
650 if (height) *height = w;
655 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
657 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
659 return page_cache->max_label_chars;
663 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
665 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
667 return page_cache->dual_even_left;
671 ev_page_cache_get_page_label (EvPageCache *page_cache,
674 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
675 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
677 if (page_cache->page_labels[page] == NULL)
678 return g_strdup_printf ("%d", page + 1);
680 return g_strdup (page_cache->page_labels[page]);
684 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
686 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
687 return page_cache->has_labels;
690 const EvDocumentInfo *
691 ev_page_cache_get_info (EvPageCache *page_cache)
693 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
695 return page_cache->page_info;
698 #define PAGE_CACHE_STRING "ev-page-cache"
701 ev_page_cache_get (EvDocument *document)
703 EvPageCache *page_cache;
705 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
707 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
708 if (page_cache == NULL) {
709 page_cache = ev_page_cache_new (document);
710 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);