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);
56 static guint signals[N_SIGNALS] = {0, };
58 static void ev_page_cache_init (EvPageCache *page_cache);
59 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
60 static void ev_page_cache_finalize (GObject *object);
62 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
65 ev_page_cache_init (EvPageCache *page_cache)
67 page_cache->current_page = -1;
68 page_cache->max_label_chars = 0;
72 ev_page_cache_class_init (EvPageCacheClass *class)
74 GObjectClass *object_class;
76 object_class = G_OBJECT_CLASS (class);
78 object_class->finalize = ev_page_cache_finalize;
80 signals [PAGE_CHANGED] =
81 g_signal_new ("page-changed",
84 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
86 g_cclosure_marshal_VOID__INT,
93 ev_page_cache_finalize (GObject *object)
95 EvPageCache *page_cache;
97 page_cache = EV_PAGE_CACHE (object);
99 g_free (page_cache->title);
100 g_free (page_cache->size_cache);
101 g_free (page_cache->height_to_page);
102 g_free (page_cache->dual_height_to_page);
103 g_strfreev (page_cache->page_labels);
105 ev_document_info_free (page_cache->page_info);
109 build_height_to_page (EvPageCache *page_cache)
113 double uniform_height, page_height, next_page_height;
116 swap = (page_cache->rotation == 90 ||
117 page_cache->rotation == 270);
119 g_free (page_cache->height_to_page);
120 g_free (page_cache->dual_height_to_page);
122 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
123 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
126 for (i = 0; i <= page_cache->n_pages; i++) {
127 if (page_cache->uniform) {
129 uniform_height = page_cache->uniform_height;
131 uniform_height = page_cache->uniform_width;
133 page_cache->height_to_page [i] = i * uniform_height;
136 page_height = page_cache->size_cache [i].height;
138 page_height = page_cache->size_cache [i].width;
140 page_cache->height_to_page [i] = saved_height;
141 saved_height += page_height;
145 if (page_cache->dual_even_left && !page_cache->uniform) {
147 saved_height = page_cache->size_cache [0].height;
149 saved_height = page_cache->size_cache [0].width;
154 for (i = page_cache->dual_even_left; i < page_cache->n_pages + 2; i += 2) {
155 if (page_cache->uniform) {
157 uniform_height = page_cache->uniform_height;
159 uniform_height = page_cache->uniform_width;
161 page_cache->dual_height_to_page [i] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
162 if (i + 1 < page_cache->n_pages + 2)
163 page_cache->dual_height_to_page [i + 1] = ((i + page_cache->dual_even_left) / 2) * uniform_height;
165 if (i + 1 < page_cache->n_pages) {
167 next_page_height = page_cache->size_cache [i + 1].height;
169 next_page_height = page_cache->size_cache [i + 1].width;
172 next_page_height = 0;
174 if (i < page_cache->n_pages) {
176 page_height = page_cache->size_cache [i].height;
178 page_height = page_cache->size_cache [i].width;
183 if (i + 1 < page_cache->n_pages + 2) {
184 page_cache->dual_height_to_page [i] = saved_height;
185 page_cache->dual_height_to_page [i + 1] = saved_height;
186 saved_height += MAX(page_height, next_page_height);
188 page_cache->dual_height_to_page [i] = saved_height;
195 ev_page_cache_new (EvDocument *document)
197 EvPageCache *page_cache;
198 EvPageCacheInfo *info;
201 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
203 ev_document_doc_mutex_lock ();
205 /* We read page information out of the document */
207 /* Assume all pages are the same size until proven otherwise */
208 page_cache->uniform = TRUE;
209 page_cache->has_labels = FALSE;
210 page_cache->n_pages = ev_document_get_n_pages (document);
211 page_cache->dual_even_left = (page_cache->n_pages > 2);
212 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
213 page_cache->max_width = 0;
214 page_cache->max_height = 0;
215 page_cache->page_info = ev_document_get_info (document);
217 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
218 page_cache->title = g_strdup (page_cache->page_info->title);
220 page_cache->title = NULL;
223 for (i = 0; i < page_cache->n_pages; i++) {
224 double page_width = 0;
225 double page_height = 0;
227 ev_document_get_page_size (document, i, &page_width, &page_height);
229 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
231 if (page_cache->page_labels[i] != NULL) {
233 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
234 g_utf8_strlen (page_cache->page_labels[i], 256));
235 if (!page_cache->has_labels) {
236 gchar *expected_label;
238 expected_label = g_strdup_printf ("%d", i + 1);
239 if (strcmp (expected_label, page_cache->page_labels[i]))
240 page_cache->has_labels = TRUE;
241 g_free (expected_label);
245 if (page_width > page_cache->max_width) {
246 page_cache->max_width = page_width;
249 if (page_height > page_cache->max_height) {
250 page_cache->max_height = page_height;
254 page_cache->uniform_width = page_width;
255 page_cache->uniform_height = page_height;
256 } else if (page_cache->uniform &&
257 (page_cache->uniform_width != page_width ||
258 page_cache->uniform_height != page_height)) {
259 /* It's a different page size. Backfill the array. */
262 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
264 for (j = 0; j < i; j++) {
265 info = &(page_cache->size_cache [j]);
266 info->width = page_cache->uniform_width;
267 info->height = page_cache->uniform_height;
269 page_cache->uniform = FALSE;
273 if (! page_cache->uniform) {
274 info = &(page_cache->size_cache [i]);
276 info->width = page_width;
277 info->height = page_height;
281 build_height_to_page (page_cache);
283 /* make some sanity check assertions */
284 if (! page_cache->uniform)
285 g_assert (page_cache->size_cache != NULL);
286 if (page_cache->uniform && page_cache->n_pages > 0)
287 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
289 ev_document_doc_mutex_unlock ();
291 if (page_cache->n_pages > 0)
292 ev_page_cache_set_current_page (page_cache, 0);
298 ev_page_cache_get_n_pages (EvPageCache *page_cache)
300 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
302 return page_cache->n_pages;
306 ev_page_cache_get_current_page (EvPageCache *page_cache)
308 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
310 return page_cache->current_page;
314 ev_page_cache_set_current_page (EvPageCache *page_cache,
317 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
318 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
320 if (page == page_cache->current_page)
323 page_cache->current_page = page;
324 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
328 ev_page_cache_set_page_label (EvPageCache *page_cache,
329 const char *page_label)
335 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
336 g_return_val_if_fail (page_label != NULL, FALSE);
338 /* First, look for a literal label match */
339 for (i = 0; i < page_cache->n_pages; i ++) {
340 if (page_cache->page_labels[i] != NULL &&
341 ! strcmp (page_label, page_cache->page_labels[i])) {
342 ev_page_cache_set_current_page (page_cache, i);
347 /* Next, parse the label, and see if the number fits */
348 value = strtol (page_label, &endptr, 10);
349 if (endptr[0] == '\0') {
350 /* Page number is an integer */
351 page = MIN (G_MAXINT, value);
353 /* convert from a page label to a page offset */
356 page < page_cache->n_pages &&
357 page_cache->page_labels[page] == NULL) {
358 ev_page_cache_set_current_page (page_cache, page);
367 ev_page_cache_get_title (EvPageCache *page_cache)
369 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
371 return page_cache->title;
375 ev_page_cache_get_size (EvPageCache *page_cache,
384 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
385 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
387 if (page_cache->uniform) {
388 w = page_cache->uniform_width;
389 h = page_cache->uniform_height;
391 EvPageCacheInfo *info;
393 info = &(page_cache->size_cache [page]);
402 if (rotation == 0 || rotation == 180) {
403 if (width) *width = (int)w;
404 if (height) *height = (int)h;
406 if (width) *width = (int)h;
407 if (height) *height = (int)w;
412 ev_page_cache_get_max_width (EvPageCache *page_cache,
417 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
420 if (rotation == 0 || rotation == 180) {
421 *width = page_cache->max_width * scale;
423 *width = page_cache->max_height * scale;
429 ev_page_cache_get_max_height (EvPageCache *page_cache,
434 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
437 if (rotation == 0 || rotation == 180) {
438 *height = page_cache->max_height * scale;
440 *height = page_cache->max_width * scale;
446 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
453 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
455 if (page_cache->rotation != rotation) {
456 page_cache->rotation = rotation;
457 build_height_to_page (page_cache);
461 *height = page_cache->height_to_page [page] * scale;
464 *dual_height = page_cache->dual_height_to_page [page] * scale;
468 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
470 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
472 return page_cache->max_label_chars;
476 ev_page_cache_get_dual_even_left (EvPageCache *page_cache)
478 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
480 return page_cache->dual_even_left;
484 ev_page_cache_get_page_label (EvPageCache *page_cache,
487 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
488 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
490 if (page_cache->page_labels[page] == NULL)
491 return g_strdup_printf ("%d", page + 1);
493 return g_strdup (page_cache->page_labels[page]);
497 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
499 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
500 return page_cache->has_labels;
503 const EvDocumentInfo *
504 ev_page_cache_get_info (EvPageCache *page_cache)
506 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
508 return page_cache->page_info;
511 #define PAGE_CACHE_STRING "ev-page-cache"
514 ev_page_cache_get (EvDocument *document)
516 EvPageCache *page_cache;
518 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
520 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
521 if (page_cache == NULL) {
522 page_cache = ev_page_cache_new (document);
523 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);