+/* Thumbnails dimensions cache */
+#define EV_THUMBNAILS_SIZE_CACHE_KEY "ev-thumbnails-size-cache"
+
+static void
+get_thumbnail_size_for_page (EvDocument *document,
+ guint page,
+ gint *width,
+ gint *height)
+{
+ gdouble scale;
+ gdouble w, h;
+
+ ev_document_get_page_size (document, page, &w, &h);
+ scale = (gdouble)THUMBNAIL_WIDTH / w;
+
+ *width = MAX ((gint)(w * scale + 0.5), 1);
+ *height = MAX ((gint)(h * scale + 0.5), 1);
+}
+
+static EvThumbsSizeCache *
+ev_thumbnails_size_cache_new (EvDocument *document)
+{
+ EvThumbsSizeCache *cache;
+ gint i, n_pages;
+ EvThumbsSize *thumb_size;
+
+ cache = g_new0 (EvThumbsSizeCache, 1);
+
+ if (ev_document_is_page_size_uniform (document)) {
+ cache->uniform = TRUE;
+ get_thumbnail_size_for_page (document, 0,
+ &cache->uniform_width,
+ &cache->uniform_height);
+ return cache;
+ }
+
+ n_pages = ev_document_get_n_pages (document);
+ cache->sizes = g_new0 (EvThumbsSize, n_pages);
+
+ for (i = 0; i < n_pages; i++) {
+ thumb_size = &(cache->sizes[i]);
+ get_thumbnail_size_for_page (document, i,
+ &thumb_size->width,
+ &thumb_size->height);
+ }
+
+ 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;
+}
+