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;
178 page_height = page_cache->size_cache [i].height;
180 page_height = page_cache->size_cache [i].width;
182 page_cache->height_to_page [i] = saved_height;
183 saved_height += page_height;
187 if (page_cache->dual_even_left && !page_cache->uniform) {
189 saved_height = page_cache->size_cache [0].height;
191 saved_height = page_cache->size_cache [0].width;
196 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
197 if (page_cache->uniform) {
199 uniform_height = page_cache->uniform_height;
201 uniform_height = page_cache->uniform_width;
203 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
204 if (i + 1 < page_cache->n_pages + 2)
205 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
207 if (i + 1 < page_cache->n_pages) {
209 next_page_height = page_cache->size_cache [i + 1].height;
211 next_page_height = page_cache->size_cache [i + 1].width;
214 next_page_height = 0;
216 if (i < page_cache->n_pages) {
218 page_height = page_cache->size_cache [i].height;
220 page_height = page_cache->size_cache [i].width;
225 if (i + 1 < page_cache->n_pages + 2) {
226 page_cache->dual_height_to_page [i] = saved_height;
227 page_cache->dual_height_to_page [i + 1] = saved_height;
228 saved_height += MAX(page_height, next_page_height);
230 page_cache->dual_height_to_page [i] = saved_height;
237 ev_page_cache_new (EvDocument *document)
239 EvPageCache *page_cache;
240 EvPageCacheInfo *info;
243 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
245 ev_document_doc_mutex_lock ();
247 /* We read page information out of the document */
249 /* Assume all pages are the same size until proven otherwise */
250 page_cache->uniform = TRUE;
251 page_cache->has_labels = FALSE;
252 page_cache->n_pages = ev_document_get_n_pages (document);
253 page_cache->dual_even_left = (page_cache->n_pages > 2);
254 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
255 page_cache->max_width = 0;
256 page_cache->max_height = 0;
257 page_cache->page_info = ev_document_get_info (document);
259 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
260 page_cache->title = g_strdup (page_cache->page_info->title);
262 page_cache->title = NULL;
265 for (i = 0; i < page_cache->n_pages; i++) {
266 double page_width = 0;
267 double page_height = 0;
269 ev_document_get_page_size (document, i, &page_width, &page_height);
271 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
273 if (page_cache->page_labels[i] != NULL) {
275 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
276 g_utf8_strlen (page_cache->page_labels[i], 256));
277 if (!page_cache->has_labels) {
278 gchar *expected_label;
280 expected_label = g_strdup_printf ("%d", i + 1);
281 if (strcmp (expected_label, page_cache->page_labels[i]))
282 page_cache->has_labels = TRUE;
283 g_free (expected_label);
287 if (page_width > page_cache->max_width) {
288 page_cache->max_width = page_width;
291 if (page_height > page_cache->max_height) {
292 page_cache->max_height = page_height;
296 page_cache->uniform_width = page_width;
297 page_cache->uniform_height = page_height;
298 } else if (page_cache->uniform &&
299 (page_cache->uniform_width != page_width ||
300 page_cache->uniform_height != page_height)) {
301 /* It's a different page size. Backfill the array. */
304 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
306 for (j = 0; j < i; j++) {
307 info = &(page_cache->size_cache [j]);
308 info->width = page_cache->uniform_width;
309 info->height = page_cache->uniform_height;
311 page_cache->uniform = FALSE;
315 if (! page_cache->uniform) {
316 info = &(page_cache->size_cache [i]);
318 info->width = page_width;
319 info->height = page_height;
323 build_height_to_page (page_cache);
325 /* make some sanity check assertions */
326 if (! page_cache->uniform)
327 g_assert (page_cache->size_cache != NULL);
328 if (page_cache->uniform && page_cache->n_pages > 0)
329 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
331 ev_document_doc_mutex_unlock ();
333 if (page_cache->n_pages > 0)
334 ev_page_cache_set_current_page (page_cache, 0);
340 ev_page_cache_get_n_pages (EvPageCache *page_cache)
342 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
344 return page_cache->n_pages;
348 ev_page_cache_get_current_page (EvPageCache *page_cache)
350 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
352 return page_cache->current_page;
356 ev_page_cache_set_current_page (EvPageCache *page_cache,
359 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
360 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
362 if (page == page_cache->current_page)
365 page_cache->current_page = page;
366 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
370 ev_page_cache_set_current_page_history (EvPageCache *page_cache,
373 if (abs (page - page_cache->current_page) > 1)
374 g_signal_emit (page_cache, signals [HISTORY_CHANGED], 0, page);
376 ev_page_cache_set_current_page (page_cache, page);
380 ev_page_cache_set_page_label (EvPageCache *page_cache,
381 const char *page_label)
387 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
388 g_return_val_if_fail (page_label != NULL, FALSE);
390 /* First, look for a literal label match */
391 for (i = 0; i < page_cache->n_pages; i ++) {
392 if (page_cache->page_labels[i] != NULL &&
393 ! strcmp (page_label, page_cache->page_labels[i])) {
394 ev_page_cache_set_current_page (page_cache, i);
399 /* Next, parse the label, and see if the number fits */
400 value = strtol (page_label, &endptr, 10);
401 if (endptr[0] == '\0') {
402 /* Page number is an integer */
403 page = MIN (G_MAXINT, value);
405 /* convert from a page label to a page offset */
408 page < page_cache->n_pages) {
409 ev_page_cache_set_current_page (page_cache, page);
418 ev_page_cache_get_title (EvPageCache *page_cache)
420 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
422 return page_cache->title;
426 ev_page_cache_get_size (EvPageCache *page_cache,
435 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
436 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
438 if (page_cache->uniform) {
439 w = page_cache->uniform_width;
440 h = page_cache->uniform_height;
442 EvPageCacheInfo *info;
444 info = &(page_cache->size_cache [page]);
453 if (rotation == 0 || rotation == 180) {
454 if (width) *width = (int)w;
455 if (height) *height = (int)h;
457 if (width) *width = (int)h;
458 if (height) *height = (int)w;
463 ev_page_cache_get_max_width (EvPageCache *page_cache,
468 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
471 if (rotation == 0 || rotation == 180) {
472 *width = page_cache->max_width * scale;
474 *width = page_cache->max_height * scale;
480 ev_page_cache_get_max_height (EvPageCache *page_cache,
485 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
488 if (rotation == 0 || rotation == 180) {
489 *height = page_cache->max_height * scale;
491 *height = page_cache->max_width * scale;
497 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
504 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
506 if (page_cache->rotation != rotation) {
507 page_cache->rotation = rotation;
508 build_height_to_page (page_cache);
512 *height = page_cache->height_to_page [page] * scale;
515 *dual_height = page_cache->dual_height_to_page [page] * scale;
519 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
521 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
523 return page_cache->max_label_chars;
527 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
529 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
531 return page_cache->dual_even_left;
535 ev_page_cache_get_page_label (EvPageCache *page_cache,
538 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
539 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
541 if (page_cache->page_labels[page] == NULL)
542 return g_strdup_printf ("%d", page + 1);
544 return g_strdup (page_cache->page_labels[page]);
548 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
550 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
551 return page_cache->has_labels;
554 const EvDocumentInfo *
555 ev_page_cache_get_info (EvPageCache *page_cache)
557 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
559 return page_cache->page_info;
562 #define PAGE_CACHE_STRING "ev-page-cache"
565 ev_page_cache_get (EvDocument *document)
567 EvPageCache *page_cache;
569 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
571 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
572 if (page_cache == NULL) {
573 page_cache = ev_page_cache_new (document);
574 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);