1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
27 double uniform_height;
29 double max_width_page_width;
30 double max_width_page_height;
31 double max_height_page_width;
32 double max_height_page_height;
34 EvPageCacheInfo *size_cache;
35 EvDocumentInfo *page_info;
38 struct _EvPageCacheClass
40 GObjectClass parent_class;
42 void (* page_changed) (EvPageCache *page_cache, gint page);
51 static guint signals[N_SIGNALS] = {0, };
53 static void ev_page_cache_init (EvPageCache *page_cache);
54 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
55 static void ev_page_cache_finalize (GObject *object);
57 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
60 ev_page_cache_init (EvPageCache *page_cache)
62 page_cache->current_page = -1;
66 ev_page_cache_class_init (EvPageCacheClass *class)
68 GObjectClass *object_class;
70 object_class = G_OBJECT_CLASS (class);
72 object_class->finalize = ev_page_cache_finalize;
74 signals [PAGE_CHANGED] =
75 g_signal_new ("page-changed",
78 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
80 g_cclosure_marshal_VOID__INT,
87 ev_page_cache_finalize (GObject *object)
89 EvPageCache *page_cache;
91 page_cache = EV_PAGE_CACHE (object);
93 g_free (page_cache->title);
94 g_free (page_cache->size_cache);
95 ev_document_info_free (page_cache->page_info);
99 _ev_page_cache_new (EvDocument *document)
101 EvDocumentInfo *doc_info;
102 EvPageCache *page_cache;
103 EvPageCacheInfo *info;
106 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
108 ev_document_doc_mutex_lock ();
110 /* We read page information out of the document */
112 /* Assume all pages are the same size until proven otherwise */
113 page_cache->uniform = TRUE;
114 page_cache->has_labels = FALSE;
115 page_cache->n_pages = ev_document_get_n_pages (document);
116 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
117 page_cache->max_width_page_width = 0;
118 page_cache->max_width_page_height = 0;
119 page_cache->max_height_page_width = 0;
120 page_cache->max_height_page_height = 0;
122 doc_info = ev_document_get_info (document);
123 if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
124 page_cache->title = g_strdup (doc_info->title);
126 page_cache->title = NULL;
130 for (i = 0; i < page_cache->n_pages; i++) {
131 double page_width = 0;
132 double page_height = 0;
134 ev_document_get_page_size (document, i, &page_width, &page_height);
136 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
138 if (!page_cache->has_labels && page_cache->page_labels[i] != NULL) {
139 gchar *expected_label;
141 expected_label = g_strdup_printf ("%d", i + 1);
142 if (strcmp (expected_label, page_cache->page_labels[i]))
143 page_cache->has_labels = TRUE;
144 g_free (expected_label);
147 if (page_width > page_cache->max_width_page_width) {
148 page_cache->max_width_page_width = page_width;
149 page_cache->max_width_page_height = page_height;
152 if (page_height > page_cache->max_height_page_height) {
153 page_cache->max_height_page_width = page_width;
154 page_cache->max_height_page_height = page_height;
158 page_cache->uniform_width = page_width;
159 page_cache->uniform_height = page_height;
160 } else if (page_cache->uniform &&
161 (page_cache->uniform_width != page_width ||
162 page_cache->uniform_height != page_height)) {
163 /* It's a different page size. Backfill the array. */
166 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
168 for (j = 0; j < i; j++) {
169 info = &(page_cache->size_cache [j]);
170 info->width = page_cache->uniform_width;
171 info->height = page_cache->uniform_height;
173 page_cache->uniform = FALSE;
177 if (! page_cache->uniform) {
178 info = &(page_cache->size_cache [i]);
180 info->width = page_width;
181 info->height = page_height;
185 page_cache->page_info = ev_document_get_info (document);
187 /* make some sanity check assertions */
188 if (! page_cache->uniform)
189 g_assert (page_cache->size_cache != NULL);
190 if (page_cache->uniform)
191 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
193 ev_document_doc_mutex_unlock ();
195 if (page_cache->n_pages > 0)
196 ev_page_cache_set_current_page (page_cache, 0);
202 ev_page_cache_get_n_pages (EvPageCache *page_cache)
204 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
206 return page_cache->n_pages;
210 ev_page_cache_get_current_page (EvPageCache *page_cache)
212 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
214 return page_cache->current_page;
218 ev_page_cache_set_current_page (EvPageCache *page_cache,
221 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
222 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
224 if (page == page_cache->current_page)
227 page_cache->current_page = page;
228 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
232 ev_page_cache_set_page_label (EvPageCache *page_cache,
233 const char *page_label)
239 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
240 g_return_val_if_fail (page_label != NULL, FALSE);
242 /* First, look for a literal label match */
243 for (i = 0; i < page_cache->n_pages; i ++) {
244 if (page_cache->page_labels[i] != NULL &&
245 ! strcmp (page_label, page_cache->page_labels[i])) {
246 ev_page_cache_set_current_page (page_cache, i);
251 /* Next, parse the label, and see if the number fits */
252 value = strtol (page_label, &endptr, 10);
253 if (endptr[0] == '\0') {
254 /* Page number is an integer */
255 page = MIN (G_MAXINT, value);
257 /* convert from a page label to a page offset */
260 page < page_cache->n_pages &&
261 page_cache->page_labels[page] == NULL) {
262 ev_page_cache_set_current_page (page_cache, page);
271 ev_page_cache_set_link (EvPageCache *page_cache,
274 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
275 g_return_if_fail (EV_IS_LINK (link));
277 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
281 ev_page_cache_get_title (EvPageCache *page_cache)
283 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
285 return page_cache->title;
289 ev_page_cache_get_size (EvPageCache *page_cache,
295 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
296 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
298 if (page_cache->uniform) {
300 *width = page_cache->uniform_width;
302 *height = page_cache->uniform_height;
304 EvPageCacheInfo *info;
306 info = &(page_cache->size_cache [page]);
309 *width = info->width;
311 *height = info->height;
315 *width = (int) ((*width) * scale + 0.5);
317 *height = (int) ((*height) * scale + 0.5);
322 /* Note that these aren't necessarily from the same page.
325 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
330 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
333 *width = page_cache->max_width_page_width * scale;
335 *height = page_cache->max_width_page_height * scale;
339 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
344 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
347 *width = page_cache->max_height_page_width * scale;
349 *height = page_cache->max_height_page_height * scale;
354 ev_page_cache_get_page_label (EvPageCache *page_cache,
357 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
358 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
360 if (page_cache->page_labels[page] == NULL)
361 return g_strdup_printf ("%d", page + 1);
363 return g_strdup (page_cache->page_labels[page]);
367 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
369 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
370 return page_cache->has_labels;
373 const EvDocumentInfo *
374 ev_page_cache_get_info (EvPageCache *page_cache)
376 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
378 return page_cache->page_info;
383 ev_page_cache_next_page (EvPageCache *page_cache)
385 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
387 if (page_cache->current_page >= page_cache->n_pages - 1)
390 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
396 ev_page_cache_prev_page (EvPageCache *page_cache)
398 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
400 if (page_cache->current_page <= 0)
403 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);