+/* Thumbnails dimensions cache */
+#define EV_THUMBNAILS_SIZE_CACHE_KEY "ev-thumbnails-size-cache"
+
+static EvThumbsSizeCache *
+ev_thumbnails_size_cache_new (EvDocument *document)
+{
+ EvThumbsSizeCache *cache;
+ EvRenderContext *rc = NULL;
+ gint i, n_pages;
+ EvThumbsSize *thumb_size;
+
+ cache = g_new0 (EvThumbsSizeCache, 1);
+
+ n_pages = ev_document_get_n_pages (document);
+
+ /* Assume all pages are the same size until proven otherwise */
+ cache->uniform = TRUE;
+
+ for (i = 0; i < n_pages; i++) {
+ EvPage *page;
+ gdouble page_width, page_height;
+ gint thumb_width = 0;
+ gint thumb_height = 0;
+
+ page = ev_document_get_page (document, i);
+
+ ev_document_get_page_size (document, i, &page_width, &page_height);
+
+ if (!rc) {
+ rc = ev_render_context_new (page, 0, (gdouble)THUMBNAIL_WIDTH / page_width);
+ } else {
+ ev_render_context_set_page (rc, page);
+ ev_render_context_set_scale (rc, (gdouble)THUMBNAIL_WIDTH / page_width);
+ }
+
+ ev_document_thumbnails_get_dimensions (EV_DOCUMENT_THUMBNAILS (document),
+ rc, &thumb_width, &thumb_height);
+
+ if (i == 0) {
+ cache->uniform_width = thumb_width;
+ cache->uniform_height = thumb_height;
+ } else if (cache->uniform &&
+ (cache->uniform_width != thumb_width ||
+ cache->uniform_height != thumb_height)) {
+ /* It's a different thumbnail size. Backfill the array. */
+ int j;
+
+ cache->sizes = g_new0 (EvThumbsSize, n_pages);
+
+ for (j = 0; j < i; j++) {
+ thumb_size = &(cache->sizes[j]);
+ thumb_size->width = cache->uniform_width;
+ thumb_size->height = cache->uniform_height;
+ }
+ cache->uniform = FALSE;
+ }
+
+ if (! cache->uniform) {
+ thumb_size = &(cache->sizes[i]);
+
+ thumb_size->width = thumb_width;
+ thumb_size->height = thumb_height;
+ }
+
+ g_object_unref (page);
+ }
+
+ if (rc) {
+ g_object_unref (rc);
+ }
+
+ return cache;
+}
+
+static void
+ev_thumbnails_size_cache_get_size (EvThumbsSizeCache *cache,
+ gint page,
+ gint rotation,
+ gint *width,
+ gint *height)
+{
+ gint w, h;
+
+ if (cache->uniform) {
+ w = cache->uniform_width;
+ h = cache->uniform_height;
+ } else {
+ EvThumbsSize *thumb_size;
+
+ thumb_size = &(cache->sizes[page]);
+
+ w = thumb_size->width;
+ h = thumb_size->height;
+ }
+
+ if (rotation == 0 || rotation == 180) {
+ if (width) *width = w;
+ if (height) *height = h;
+ } else {
+ if (width) *width = h;
+ if (height) *height = w;
+ }
+}
+
+static void
+ev_thumbnails_size_cache_free (EvThumbsSizeCache *cache)
+{
+ if (cache->sizes) {
+ g_free (cache->sizes);
+ cache->sizes = NULL;
+ }
+
+ g_free (cache);
+}
+
+static EvThumbsSizeCache *
+ev_thumbnails_size_cache_get (EvDocument *document)
+{
+ EvThumbsSizeCache *cache;
+
+ cache = g_object_get_data (G_OBJECT (document), EV_THUMBNAILS_SIZE_CACHE_KEY);
+ if (!cache) {
+ cache = ev_thumbnails_size_cache_new (document);
+ g_object_set_data_full (G_OBJECT (document),
+ EV_THUMBNAILS_SIZE_CACHE_KEY,
+ cache,
+ (GDestroyNotify)ev_thumbnails_size_cache_free);
+ }
+
+ return cache;
+}
+