1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
26 gboolean dual_even_left;
29 double uniform_height;
34 double* height_to_page;
35 double* dual_height_to_page;
39 EvPageCacheInfo *size_cache;
40 EvDocumentInfo *page_info;
43 struct _EvPageCacheClass
45 GObjectClass parent_class;
47 void (* page_changed) (EvPageCache *page_cache, gint page);
48 void (* history_changed) (EvPageCache *page_cache, gint page);
58 static guint signals[N_SIGNALS] = {0, };
60 static void ev_page_cache_init (EvPageCache *page_cache);
61 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
62 static void ev_page_cache_finalize (GObject *object);
64 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
67 ev_page_cache_init (EvPageCache *page_cache)
69 page_cache->current_page = -1;
70 page_cache->max_label_chars = 0;
74 ev_page_cache_class_init (EvPageCacheClass *class)
76 GObjectClass *object_class;
78 object_class = G_OBJECT_CLASS (class);
80 object_class->finalize = ev_page_cache_finalize;
82 signals [PAGE_CHANGED] =
83 g_signal_new ("page-changed",
86 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
88 g_cclosure_marshal_VOID__INT,
92 signals [HISTORY_CHANGED] =
93 g_signal_new ("history-changed",
96 G_STRUCT_OFFSET (EvPageCacheClass, history_changed),
98 g_cclosure_marshal_VOID__INT,
105 ev_page_cache_finalize (GObject *object)
107 EvPageCache *page_cache;
109 page_cache = EV_PAGE_CACHE (object);
111 g_free (page_cache->title);
112 g_free (page_cache->size_cache);
113 g_free (page_cache->height_to_page);
114 g_free (page_cache->dual_height_to_page);
115 g_strfreev (page_cache->page_labels);
117 ev_document_info_free (page_cache->page_info);
121 build_height_to_page (EvPageCache *page_cache)
125 double uniform_height, page_height, next_page_height;
128 swap = (page_cache->rotation == 90 ||
129 page_cache->rotation == 270);
131 g_free (page_cache->height_to_page);
132 g_free (page_cache->dual_height_to_page);
134 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
135 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
138 for (i = 0; i <= page_cache->n_pages; i++) {
139 if (page_cache->uniform) {
141 uniform_height = page_cache->uniform_height;
143 uniform_height = page_cache->uniform_width;
145 page_cache->height_to_page [i] = i * uniform_height;
148 page_height = page_cache->size_cache [i].height;
150 page_height = page_cache->size_cache [i].width;
152 page_cache->height_to_page [i] = saved_height;
153 saved_height += page_height;
157 if (page_cache->dual_even_left && !page_cache->uniform) {
159 saved_height = page_cache->size_cache [0].height;
161 saved_height = page_cache->size_cache [0].width;
166 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
167 if (page_cache->uniform) {
169 uniform_height = page_cache->uniform_height;
171 uniform_height = page_cache->uniform_width;
173 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
174 if (i + 1 < page_cache->n_pages + 2)
175 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
177 if (i + 1 < page_cache->n_pages) {
179 next_page_height = page_cache->size_cache [i + 1].height;
181 next_page_height = page_cache->size_cache [i + 1].width;
184 next_page_height = 0;
186 if (i < page_cache->n_pages) {
188 page_height = page_cache->size_cache [i].height;
190 page_height = page_cache->size_cache [i].width;
195 if (i + 1 < page_cache->n_pages + 2) {
196 page_cache->dual_height_to_page [i] = saved_height;
197 page_cache->dual_height_to_page [i + 1] = saved_height;
198 saved_height += MAX(page_height, next_page_height);
200 page_cache->dual_height_to_page [i] = saved_height;
207 ev_page_cache_new (EvDocument *document)
209 EvPageCache *page_cache;
210 EvPageCacheInfo *info;
213 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
215 ev_document_doc_mutex_lock ();
217 /* We read page information out of the document */
219 /* Assume all pages are the same size until proven otherwise */
220 page_cache->uniform = TRUE;
221 page_cache->has_labels = FALSE;
222 page_cache->n_pages = ev_document_get_n_pages (document);
223 page_cache->dual_even_left = (page_cache->n_pages > 2);
224 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
225 page_cache->max_width = 0;
226 page_cache->max_height = 0;
227 page_cache->page_info = ev_document_get_info (document);
229 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
230 page_cache->title = g_strdup (page_cache->page_info->title);
232 page_cache->title = NULL;
235 for (i = 0; i < page_cache->n_pages; i++) {
236 double page_width = 0;
237 double page_height = 0;
239 ev_document_get_page_size (document, i, &page_width, &page_height);
241 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
243 if (page_cache->page_labels[i] != NULL) {
245 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
246 g_utf8_strlen (page_cache->page_labels[i], 256));
247 if (!page_cache->has_labels) {
248 gchar *expected_label;
250 expected_label = g_strdup_printf ("%d", i + 1);
251 if (strcmp (expected_label, page_cache->page_labels[i]))
252 page_cache->has_labels = TRUE;
253 g_free (expected_label);
257 if (page_width > page_cache->max_width) {
258 page_cache->max_width = page_width;
261 if (page_height > page_cache->max_height) {
262 page_cache->max_height = page_height;
266 page_cache->uniform_width = page_width;
267 page_cache->uniform_height = page_height;
268 } else if (page_cache->uniform &&
269 (page_cache->uniform_width != page_width ||
270 page_cache->uniform_height != page_height)) {
271 /* It's a different page size. Backfill the array. */
274 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
276 for (j = 0; j < i; j++) {
277 info = &(page_cache->size_cache [j]);
278 info->width = page_cache->uniform_width;
279 info->height = page_cache->uniform_height;
281 page_cache->uniform = FALSE;
285 if (! page_cache->uniform) {
286 info = &(page_cache->size_cache [i]);
288 info->width = page_width;
289 info->height = page_height;
293 build_height_to_page (page_cache);
295 /* make some sanity check assertions */
296 if (! page_cache->uniform)
297 g_assert (page_cache->size_cache != NULL);
298 if (page_cache->uniform && page_cache->n_pages > 0)
299 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
301 ev_document_doc_mutex_unlock ();
303 if (page_cache->n_pages > 0)
304 ev_page_cache_set_current_page (page_cache, 0);
310 ev_page_cache_get_n_pages (EvPageCache *page_cache)
312 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
314 return page_cache->n_pages;
318 ev_page_cache_get_current_page (EvPageCache *page_cache)
320 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
322 return page_cache->current_page;
326 ev_page_cache_set_current_page (EvPageCache *page_cache,
329 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
330 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
332 if (page == page_cache->current_page)
335 page_cache->current_page = page;
336 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
340 ev_page_cache_set_current_page_history (EvPageCache *page_cache,
343 if (abs (page - page_cache->current_page) > 1)
344 g_signal_emit (page_cache, signals [HISTORY_CHANGED], 0, page);
346 ev_page_cache_set_current_page (page_cache, page);
350 ev_page_cache_set_page_label (EvPageCache *page_cache,
351 const char *page_label)
357 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
358 g_return_val_if_fail (page_label != NULL, FALSE);
360 /* First, look for a literal label match */
361 for (i = 0; i < page_cache->n_pages; i ++) {
362 if (page_cache->page_labels[i] != NULL &&
363 ! strcmp (page_label, page_cache->page_labels[i])) {
364 ev_page_cache_set_current_page (page_cache, i);
369 /* Next, parse the label, and see if the number fits */
370 value = strtol (page_label, &endptr, 10);
371 if (endptr[0] == '\0') {
372 /* Page number is an integer */
373 page = MIN (G_MAXINT, value);
375 /* convert from a page label to a page offset */
378 page < page_cache->n_pages) {
379 ev_page_cache_set_current_page (page_cache, page);
388 ev_page_cache_get_title (EvPageCache *page_cache)
390 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
392 return page_cache->title;
396 ev_page_cache_get_size (EvPageCache *page_cache,
405 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
406 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
408 if (page_cache->uniform) {
409 w = page_cache->uniform_width;
410 h = page_cache->uniform_height;
412 EvPageCacheInfo *info;
414 info = &(page_cache->size_cache [page]);
423 if (rotation == 0 || rotation == 180) {
424 if (width) *width = (int)w;
425 if (height) *height = (int)h;
427 if (width) *width = (int)h;
428 if (height) *height = (int)w;
433 ev_page_cache_get_max_width (EvPageCache *page_cache,
438 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
441 if (rotation == 0 || rotation == 180) {
442 *width = page_cache->max_width * scale;
444 *width = page_cache->max_height * scale;
450 ev_page_cache_get_max_height (EvPageCache *page_cache,
455 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
458 if (rotation == 0 || rotation == 180) {
459 *height = page_cache->max_height * scale;
461 *height = page_cache->max_width * scale;
467 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
474 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
476 if (page_cache->rotation != rotation) {
477 page_cache->rotation = rotation;
478 build_height_to_page (page_cache);
482 *height = page_cache->height_to_page [page] * scale;
485 *dual_height = page_cache->dual_height_to_page [page] * scale;
489 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
491 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
493 return page_cache->max_label_chars;
497 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
499 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
501 return page_cache->dual_even_left;
505 ev_page_cache_get_page_label (EvPageCache *page_cache,
508 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
509 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
511 if (page_cache->page_labels[page] == NULL)
512 return g_strdup_printf ("%d", page + 1);
514 return g_strdup (page_cache->page_labels[page]);
518 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
520 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
521 return page_cache->has_labels;
524 const EvDocumentInfo *
525 ev_page_cache_get_info (EvPageCache *page_cache)
527 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
529 return page_cache->page_info;
532 #define PAGE_CACHE_STRING "ev-page-cache"
535 ev_page_cache_get (EvDocument *document)
537 EvPageCache *page_cache;
539 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
541 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
542 if (page_cache == NULL) {
543 page_cache = ev_page_cache_new (document);
544 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);