+ swap = (rotation == 90 || rotation == 270);
+
+ uniform = ev_document_is_page_size_uniform (document);
+ n_pages = ev_document_get_n_pages (document);
+ dual_even_left = (n_pages > 2);
+
+ g_free (cache->height_to_page);
+ g_free (cache->dual_height_to_page);
+
+ cache->rotation = rotation;
+ cache->height_to_page = g_new0 (gdouble, n_pages + 1);
+ cache->dual_height_to_page = g_new0 (gdouble, n_pages + 2);
+
+ if (uniform)
+ ev_document_get_page_size (document, 0, &u_width, &u_height);
+
+ saved_height = 0;
+ for (i = 0; i <= n_pages; i++) {
+ if (uniform) {
+ uniform_height = swap ? u_width : u_height;
+ cache->height_to_page[i] = i * uniform_height;
+ } else {
+ if (i < n_pages) {
+ gdouble w, h;
+
+ ev_document_get_page_size (document, i, &w, &h);
+ page_height = swap ? w : h;
+ } else {
+ page_height = 0;
+ }
+ cache->height_to_page[i] = saved_height;
+ saved_height += page_height;
+ }
+ }
+
+ if (dual_even_left && !uniform) {
+ gdouble w, h;
+
+ ev_document_get_page_size (document, 0, &w, &h);
+ saved_height = swap ? w : h;
+ } else {
+ saved_height = 0;
+ }
+
+ for (i = dual_even_left; i < n_pages + 2; i += 2) {
+ if (uniform) {
+ uniform_height = swap ? u_width : u_height;
+ cache->dual_height_to_page[i] = ((i + dual_even_left) / 2) * uniform_height;
+ if (i + 1 < n_pages + 2)
+ cache->dual_height_to_page[i + 1] = ((i + dual_even_left) / 2) * uniform_height;
+ } else {
+ if (i + 1 < n_pages) {
+ gdouble w, h;
+
+ ev_document_get_page_size (document, i + 1, &w, &h);
+ next_page_height = swap ? w : h;
+ } else {
+ next_page_height = 0;
+ }
+
+ if (i < n_pages) {
+ gdouble w, h;
+
+ ev_document_get_page_size (document, i, &w, &h);
+ page_height = swap ? w : h;
+ } else {
+ page_height = 0;
+ }
+
+ if (i + 1 < n_pages + 2) {
+ cache->dual_height_to_page[i] = saved_height;
+ cache->dual_height_to_page[i + 1] = saved_height;
+ saved_height += MAX(page_height, next_page_height);
+ } else {
+ cache->dual_height_to_page[i] = saved_height;
+ }
+ }
+ }
+}
+
+static void
+ev_height_to_page_cache_get_height (EvHeightToPageCache *cache,
+ EvDocument *document,
+ gint page,
+ gint rotation,
+ gdouble *height,
+ gdouble *dual_height)
+{
+ if (cache->rotation != rotation)
+ build_height_to_page (cache, document, rotation);
+
+ *height = cache->height_to_page[page];
+ *dual_height = cache->dual_height_to_page[page];
+}
+
+static void
+ev_height_to_page_cache_free (EvHeightToPageCache *cache)
+{
+ if (cache->height_to_page) {
+ g_free (cache->height_to_page);
+ cache->height_to_page = NULL;