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 if (page_cache->title) {
112 g_free (page_cache->title);
113 page_cache->title = NULL;
116 if (page_cache->size_cache) {
117 g_free (page_cache->size_cache);
118 page_cache->size_cache = NULL;
121 if (page_cache->height_to_page) {
122 g_free (page_cache->height_to_page);
123 page_cache->height_to_page = NULL;
126 if (page_cache->dual_height_to_page) {
127 g_free (page_cache->dual_height_to_page);
128 page_cache->dual_height_to_page = NULL;
131 if (page_cache->page_labels) {
134 for (i = 0; i < page_cache->n_pages; i++) {
135 if (page_cache->page_labels[i])
136 g_free (page_cache->page_labels[i]);
138 g_free (page_cache->page_labels);
139 page_cache->page_labels = NULL;
142 if (page_cache->page_info) {
143 ev_document_info_free (page_cache->page_info);
144 page_cache->page_info = NULL;
147 G_OBJECT_CLASS (ev_page_cache_parent_class)->finalize (object);
151 build_height_to_page (EvPageCache *page_cache)
155 double uniform_height, page_height, next_page_height;
158 swap = (page_cache->rotation == 90 ||
159 page_cache->rotation == 270);
161 g_free (page_cache->height_to_page);
162 g_free (page_cache->dual_height_to_page);
164 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
165 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
168 for (i = 0; i <= page_cache->n_pages; i++) {
169 if (page_cache->uniform) {
171 uniform_height = page_cache->uniform_height;
173 uniform_height = page_cache->uniform_width;
175 page_cache->height_to_page [i] = i * uniform_height;
177 if (i < page_cache->n_pages) {
179 page_height = page_cache->size_cache [i].height;
181 page_height = page_cache->size_cache [i].width;
186 page_cache->height_to_page [i] = saved_height;
187 saved_height += page_height;
191 if (page_cache->dual_even_left && !page_cache->uniform) {
193 saved_height = page_cache->size_cache [0].height;
195 saved_height = page_cache->size_cache [0].width;
200 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
201 if (page_cache->uniform) {
203 uniform_height = page_cache->uniform_height;
205 uniform_height = page_cache->uniform_width;
207 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
208 if (i + 1 < page_cache->n_pages + 2)
209 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
211 if (i + 1 < page_cache->n_pages) {
213 next_page_height = page_cache->size_cache [i + 1].height;
215 next_page_height = page_cache->size_cache [i + 1].width;
218 next_page_height = 0;
220 if (i < page_cache->n_pages) {
222 page_height = page_cache->size_cache [i].height;
224 page_height = page_cache->size_cache [i].width;
229 if (i + 1 < page_cache->n_pages + 2) {
230 page_cache->dual_height_to_page [i] = saved_height;
231 page_cache->dual_height_to_page [i + 1] = saved_height;
232 saved_height += MAX(page_height, next_page_height);
234 page_cache->dual_height_to_page [i] = saved_height;
241 ev_page_cache_new (EvDocument *document)
243 EvPageCache *page_cache;
244 EvPageCacheInfo *info;
247 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
249 ev_document_doc_mutex_lock ();
251 /* We read page information out of the document */
253 /* Assume all pages are the same size until proven otherwise */
254 page_cache->uniform = TRUE;
255 page_cache->has_labels = FALSE;
256 page_cache->n_pages = ev_document_get_n_pages (document);
257 page_cache->dual_even_left = (page_cache->n_pages > 2);
258 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
259 page_cache->max_width = 0;
260 page_cache->max_height = 0;
261 page_cache->page_info = ev_document_get_info (document);
263 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
264 page_cache->title = g_strdup (page_cache->page_info->title);
266 page_cache->title = NULL;
269 for (i = 0; i < page_cache->n_pages; i++) {
270 double page_width = 0;
271 double page_height = 0;
273 ev_document_get_page_size (document, i, &page_width, &page_height);
275 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
277 if (page_cache->page_labels[i] != NULL) {
279 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
280 g_utf8_strlen (page_cache->page_labels[i], 256));
281 if (!page_cache->has_labels) {
282 gchar *expected_label;
284 expected_label = g_strdup_printf ("%d", i + 1);
285 if (strcmp (expected_label, page_cache->page_labels[i]))
286 page_cache->has_labels = TRUE;
287 g_free (expected_label);
291 if (page_width > page_cache->max_width) {
292 page_cache->max_width = page_width;
295 if (page_height > page_cache->max_height) {
296 page_cache->max_height = page_height;
300 page_cache->uniform_width = page_width;
301 page_cache->uniform_height = page_height;
302 } else if (page_cache->uniform &&
303 (page_cache->uniform_width != page_width ||
304 page_cache->uniform_height != page_height)) {
305 /* It's a different page size. Backfill the array. */
308 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
310 for (j = 0; j < i; j++) {
311 info = &(page_cache->size_cache [j]);
312 info->width = page_cache->uniform_width;
313 info->height = page_cache->uniform_height;
315 page_cache->uniform = FALSE;
319 if (! page_cache->uniform) {
320 info = &(page_cache->size_cache [i]);
322 info->width = page_width;
323 info->height = page_height;
327 build_height_to_page (page_cache);
329 /* make some sanity check assertions */
330 if (! page_cache->uniform)
331 g_assert (page_cache->size_cache != NULL);
332 if (page_cache->uniform && page_cache->n_pages > 0)
333 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
335 ev_document_doc_mutex_unlock ();
337 if (page_cache->n_pages > 0)
338 ev_page_cache_set_current_page (page_cache, 0);
344 ev_page_cache_get_n_pages (EvPageCache *page_cache)
346 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
348 return page_cache->n_pages;
352 ev_page_cache_get_current_page (EvPageCache *page_cache)
354 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
356 return page_cache->current_page;
360 ev_page_cache_set_current_page (EvPageCache *page_cache,
363 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
364 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
366 if (page == page_cache->current_page)
369 page_cache->current_page = page;
370 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
374 ev_page_cache_set_current_page_history (EvPageCache *page_cache,
377 if (abs (page - page_cache->current_page) > 1)
378 g_signal_emit (page_cache, signals [HISTORY_CHANGED], 0, page);
380 ev_page_cache_set_current_page (page_cache, page);
384 ev_page_cache_set_page_label (EvPageCache *page_cache,
385 const char *page_label)
391 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
392 g_return_val_if_fail (page_label != NULL, FALSE);
394 /* First, look for a literal label match */
395 for (i = 0; i < page_cache->n_pages; i ++) {
396 if (page_cache->page_labels[i] != NULL &&
397 ! strcmp (page_label, page_cache->page_labels[i])) {
398 ev_page_cache_set_current_page (page_cache, i);
403 /* Next, parse the label, and see if the number fits */
404 value = strtol (page_label, &endptr, 10);
405 if (endptr[0] == '\0') {
406 /* Page number is an integer */
407 page = MIN (G_MAXINT, value);
409 /* convert from a page label to a page offset */
412 page < page_cache->n_pages) {
413 ev_page_cache_set_current_page (page_cache, page);
422 ev_page_cache_get_title (EvPageCache *page_cache)
424 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
426 return page_cache->title;
430 ev_page_cache_get_size (EvPageCache *page_cache,
439 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
440 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
442 if (page_cache->uniform) {
443 w = page_cache->uniform_width;
444 h = page_cache->uniform_height;
446 EvPageCacheInfo *info;
448 info = &(page_cache->size_cache [page]);
457 if (rotation == 0 || rotation == 180) {
458 if (width) *width = (int)w;
459 if (height) *height = (int)h;
461 if (width) *width = (int)h;
462 if (height) *height = (int)w;
467 ev_page_cache_get_max_width (EvPageCache *page_cache,
472 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
475 if (rotation == 0 || rotation == 180) {
476 *width = page_cache->max_width * scale;
478 *width = page_cache->max_height * scale;
484 ev_page_cache_get_max_height (EvPageCache *page_cache,
489 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
492 if (rotation == 0 || rotation == 180) {
493 *height = page_cache->max_height * scale;
495 *height = page_cache->max_width * scale;
501 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
508 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
509 g_return_if_fail (page >= 0);
510 g_return_if_fail (!height || page <= page_cache->n_pages);
511 g_return_if_fail (!dual_height || page <= page_cache->n_pages + 1);
513 if (page_cache->rotation != rotation) {
514 page_cache->rotation = rotation;
515 build_height_to_page (page_cache);
519 *height = page_cache->height_to_page [page] * scale;
522 *dual_height = page_cache->dual_height_to_page [page] * scale;
526 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
528 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
530 return page_cache->max_label_chars;
534 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
536 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
538 return page_cache->dual_even_left;
542 ev_page_cache_get_page_label (EvPageCache *page_cache,
545 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
546 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
548 if (page_cache->page_labels[page] == NULL)
549 return g_strdup_printf ("%d", page + 1);
551 return g_strdup (page_cache->page_labels[page]);
555 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
557 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
558 return page_cache->has_labels;
561 const EvDocumentInfo *
562 ev_page_cache_get_info (EvPageCache *page_cache)
564 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
566 return page_cache->page_info;
569 #define PAGE_CACHE_STRING "ev-page-cache"
572 ev_page_cache_get (EvDocument *document)
574 EvPageCache *page_cache;
576 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
578 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
579 if (page_cache == NULL) {
580 page_cache = ev_page_cache_new (document);
581 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);