#include "ev-page-cache.h"
#include "ev-job-queue.h"
+#include <stdlib.h>
+#include <string.h>
typedef struct _EvPageCacheInfo
{
- gint width;
- gint height;
+ double width;
+ double height;
}
EvPageCacheInfo;
char **page_labels;
gboolean uniform;
- gint uniform_width;
- gint uniform_height;
+ double uniform_width;
+ double uniform_height;
EvPageCacheInfo *size_cache;
};
static void
ev_page_cache_init (EvPageCache *page_cache)
{
- page_cache->current_page = 0;
+ page_cache->current_page = -1;
}
static void
EvPageCache *
_ev_page_cache_new (EvDocument *document)
{
+ EvDocumentInfo *doc_info;
EvPageCache *page_cache;
EvPageCacheInfo *info;
gint i;
page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
- g_mutex_lock (EV_DOC_MUTEX);
+ ev_document_doc_mutex_lock ();
/* We read page information out of the document */
/* Assume all pages are the same size until proven otherwise */
page_cache->uniform = TRUE;
page_cache->n_pages = ev_document_get_n_pages (document);
- page_cache->title = ev_document_get_title (document);
page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
- ev_document_set_scale (document, 1.0);
+ doc_info = ev_document_get_info (document);
+ if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
+ page_cache->title = g_strdup (doc_info->title);
+ } else {
+ page_cache->title = NULL;
+ }
+ g_free (doc_info);
+
for (i = 0; i < page_cache->n_pages; i++) {
- gint page_width = 0;
- gint page_height = 0;
+ double page_width = 0;
+ double page_height = 0;
ev_document_get_page_size (document, i, &page_width, &page_height);
page_cache->page_labels[i] = ev_document_get_page_label (document, i);
page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
- for (j = 1; j < i; j++) {
- info = &(page_cache->size_cache [j - 1]);
- info->width = page_width;
- info->height = page_height;
+ for (j = 0; j < i; j++) {
+ info = &(page_cache->size_cache [j]);
+ info->width = page_cache->uniform_width;
+ info->height = page_cache->uniform_height;
}
page_cache->uniform = FALSE;
}
if (! page_cache->uniform) {
- info = &(page_cache->size_cache [i - 1]);
+ info = &(page_cache->size_cache [i]);
info->width = page_width;
info->height = page_height;
}
/* make some sanity check assertions */
- g_assert (page_cache->n_pages > 0);
if (! page_cache->uniform)
g_assert (page_cache->size_cache != NULL);
- if (page_cache->uniform)
- g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
- g_mutex_unlock (EV_DOC_MUTEX);
+ ev_document_doc_mutex_unlock ();
+
+ if (page_cache->n_pages > 0)
+ ev_page_cache_set_current_page (page_cache, 0);
return page_cache;
}
g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
}
+gboolean
+ev_page_cache_set_page_label (EvPageCache *page_cache,
+ const char *page_label)
+{
+ gint i, page;
+ long value;
+ char *endptr = NULL;
+
+ g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
+ g_return_val_if_fail (page_label != NULL, FALSE);
+
+ /* First, look for a literal label match */
+ for (i = 0; i < page_cache->n_pages; i ++) {
+ if (page_cache->page_labels[i] != NULL &&
+ ! strcmp (page_label, page_cache->page_labels[i])) {
+ ev_page_cache_set_current_page (page_cache, i);
+ return TRUE;
+ }
+ }
+
+ /* Next, parse the label, and see if the number fits */
+ value = strtol (page_label, &endptr, 10);
+ if (endptr[0] == '\0') {
+ /* Page number is an integer */
+ page = MIN (G_MAXINT, value);
+
+ /* convert from a page label to a page offset */
+ page --;
+ if (page >= 0 &&
+ page < page_cache->n_pages &&
+ page_cache->page_labels[page] == NULL) {
+ ev_page_cache_set_current_page (page_cache, page);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
void
ev_page_cache_set_link (EvPageCache *page_cache,
EvLink *link)
{
g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
- if (page_cache->current_page > page_cache->n_pages)
+ if (page_cache->current_page >= page_cache->n_pages - 1)
return FALSE;
ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);