1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
28 double uniform_height;
33 double* height_to_page;
34 double* dual_height_to_page;
38 EvPageCacheInfo *size_cache;
39 EvDocumentInfo *page_info;
42 struct _EvPageCacheClass
44 GObjectClass parent_class;
46 void (* page_changed) (EvPageCache *page_cache, gint page);
55 static guint signals[N_SIGNALS] = {0, };
57 static void ev_page_cache_init (EvPageCache *page_cache);
58 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
59 static void ev_page_cache_finalize (GObject *object);
61 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
64 ev_page_cache_init (EvPageCache *page_cache)
66 page_cache->current_page = -1;
67 page_cache->max_label_chars = 0;
71 ev_page_cache_class_init (EvPageCacheClass *class)
73 GObjectClass *object_class;
75 object_class = G_OBJECT_CLASS (class);
77 object_class->finalize = ev_page_cache_finalize;
79 signals [PAGE_CHANGED] =
80 g_signal_new ("page-changed",
83 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
85 g_cclosure_marshal_VOID__INT,
92 ev_page_cache_finalize (GObject *object)
94 EvPageCache *page_cache;
96 page_cache = EV_PAGE_CACHE (object);
98 g_free (page_cache->title);
99 g_free (page_cache->size_cache);
100 g_free (page_cache->height_to_page);
101 g_free (page_cache->dual_height_to_page);
103 ev_document_info_free (page_cache->page_info);
107 build_height_to_page (EvPageCache *page_cache)
111 double uniform_height, page_height, next_page_height;
114 swap = (page_cache->rotation == 90 ||
115 page_cache->rotation == 270);
117 g_free (page_cache->height_to_page);
118 g_free (page_cache->dual_height_to_page);
120 page_cache->height_to_page = g_new0(double, page_cache->n_pages + 1);
121 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages + 2);
124 for (i = 0; i <= page_cache->n_pages; i++) {
125 if (page_cache->uniform) {
127 uniform_height = page_cache->uniform_height;
129 uniform_height = page_cache->uniform_width;
131 page_cache->height_to_page [i] = i * uniform_height;
134 page_height = page_cache->size_cache [i].height;
136 page_height = page_cache->size_cache [i].width;
138 page_cache->height_to_page [i] = saved_height;
139 saved_height += page_height;
143 if (DUAL_EVEN_LEFT && !page_cache->uniform) {
145 saved_height = page_cache->size_cache [0].height;
147 saved_height = page_cache->size_cache [0].width;
152 for (i = DUAL_EVEN_LEFT; i < page_cache->n_pages + 2; i += 2) {
153 if (page_cache->uniform) {
155 uniform_height = page_cache->uniform_height;
157 uniform_height = page_cache->uniform_width;
159 page_cache->dual_height_to_page [i] = ((i + DUAL_EVEN_LEFT) / 2) * uniform_height;
160 if (i + 1 < page_cache->n_pages + 2)
161 page_cache->dual_height_to_page [i + 1] = ((i + DUAL_EVEN_LEFT) / 2) * uniform_height;
163 if (i + 1 < page_cache->n_pages) {
165 next_page_height = page_cache->size_cache [i + 1].height;
167 next_page_height = page_cache->size_cache [i + 1].width;
170 next_page_height = 0;
172 if (i < page_cache->n_pages) {
174 page_height = page_cache->size_cache [i].height;
176 page_height = page_cache->size_cache [i].width;
181 if (i + 1 < page_cache->n_pages + 2) {
182 page_cache->dual_height_to_page [i] = saved_height;
183 page_cache->dual_height_to_page [i + 1] = saved_height;
184 saved_height += MAX(page_height, next_page_height);
186 page_cache->dual_height_to_page [i] = saved_height;
193 ev_page_cache_new (EvDocument *document)
195 EvPageCache *page_cache;
196 EvPageCacheInfo *info;
199 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
201 ev_document_doc_mutex_lock ();
203 /* We read page information out of the document */
205 /* Assume all pages are the same size until proven otherwise */
206 page_cache->uniform = TRUE;
207 page_cache->has_labels = FALSE;
208 page_cache->n_pages = ev_document_get_n_pages (document);
209 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
210 page_cache->max_width = 0;
211 page_cache->max_height = 0;
212 page_cache->page_info = ev_document_get_info (document);
214 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
215 page_cache->title = g_strdup (page_cache->page_info->title);
217 page_cache->title = NULL;
220 for (i = 0; i < page_cache->n_pages; i++) {
221 double page_width = 0;
222 double page_height = 0;
224 ev_document_get_page_size (document, i, &page_width, &page_height);
226 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
228 if (page_cache->page_labels[i] != NULL) {
230 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
231 g_utf8_strlen (page_cache->page_labels[i], 256));
232 if (!page_cache->has_labels) {
233 gchar *expected_label;
235 expected_label = g_strdup_printf ("%d", i + 1);
236 if (strcmp (expected_label, page_cache->page_labels[i]))
237 page_cache->has_labels = TRUE;
238 g_free (expected_label);
242 if (page_width > page_cache->max_width) {
243 page_cache->max_width = page_width;
246 if (page_height > page_cache->max_height) {
247 page_cache->max_height = page_height;
251 page_cache->uniform_width = page_width;
252 page_cache->uniform_height = page_height;
253 } else if (page_cache->uniform &&
254 (page_cache->uniform_width != page_width ||
255 page_cache->uniform_height != page_height)) {
256 /* It's a different page size. Backfill the array. */
259 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
261 for (j = 0; j < i; j++) {
262 info = &(page_cache->size_cache [j]);
263 info->width = page_cache->uniform_width;
264 info->height = page_cache->uniform_height;
266 page_cache->uniform = FALSE;
270 if (! page_cache->uniform) {
271 info = &(page_cache->size_cache [i]);
273 info->width = page_width;
274 info->height = page_height;
278 build_height_to_page (page_cache);
280 /* make some sanity check assertions */
281 if (! page_cache->uniform)
282 g_assert (page_cache->size_cache != NULL);
283 if (page_cache->uniform && page_cache->n_pages > 0)
284 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
286 ev_document_doc_mutex_unlock ();
288 if (page_cache->n_pages > 0)
289 ev_page_cache_set_current_page (page_cache, 0);
295 ev_page_cache_get_n_pages (EvPageCache *page_cache)
297 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
299 return page_cache->n_pages;
303 ev_page_cache_get_current_page (EvPageCache *page_cache)
305 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
307 return page_cache->current_page;
311 ev_page_cache_set_current_page (EvPageCache *page_cache,
314 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
315 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
317 if (page == page_cache->current_page)
320 page_cache->current_page = page;
321 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
325 ev_page_cache_set_page_label (EvPageCache *page_cache,
326 const char *page_label)
332 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
333 g_return_val_if_fail (page_label != NULL, FALSE);
335 /* First, look for a literal label match */
336 for (i = 0; i < page_cache->n_pages; i ++) {
337 if (page_cache->page_labels[i] != NULL &&
338 ! strcmp (page_label, page_cache->page_labels[i])) {
339 ev_page_cache_set_current_page (page_cache, i);
344 /* Next, parse the label, and see if the number fits */
345 value = strtol (page_label, &endptr, 10);
346 if (endptr[0] == '\0') {
347 /* Page number is an integer */
348 page = MIN (G_MAXINT, value);
350 /* convert from a page label to a page offset */
353 page < page_cache->n_pages &&
354 page_cache->page_labels[page] == NULL) {
355 ev_page_cache_set_current_page (page_cache, page);
364 ev_page_cache_get_title (EvPageCache *page_cache)
366 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
368 return page_cache->title;
372 ev_page_cache_get_size (EvPageCache *page_cache,
381 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
382 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
384 if (page_cache->uniform) {
385 w = page_cache->uniform_width;
386 h = page_cache->uniform_height;
388 EvPageCacheInfo *info;
390 info = &(page_cache->size_cache [page]);
399 if (rotation == 0 || rotation == 180) {
400 if (width) *width = (int)w;
401 if (height) *height = (int)h;
403 if (width) *width = (int)h;
404 if (height) *height = (int)w;
409 ev_page_cache_get_max_width (EvPageCache *page_cache,
414 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
417 if (rotation == 0 || rotation == 180) {
418 *width = page_cache->max_width * scale;
420 *width = page_cache->max_height * scale;
426 ev_page_cache_get_max_height (EvPageCache *page_cache,
431 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
434 if (rotation == 0 || rotation == 180) {
435 *height = page_cache->max_height * scale;
437 *height = page_cache->max_width * scale;
443 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
450 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
452 if (page_cache->rotation != rotation) {
453 page_cache->rotation = rotation;
454 build_height_to_page (page_cache);
458 *height = page_cache->height_to_page [page] * scale;
461 *dual_height = page_cache->dual_height_to_page [page] * scale;
465 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
467 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
469 return page_cache->max_label_chars;
473 ev_page_cache_get_page_label (EvPageCache *page_cache,
476 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
477 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
479 if (page_cache->page_labels[page] == NULL)
480 return g_strdup_printf ("%d", page + 1);
482 return g_strdup (page_cache->page_labels[page]);
486 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
488 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
489 return page_cache->has_labels;
492 const EvDocumentInfo *
493 ev_page_cache_get_info (EvPageCache *page_cache)
495 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
497 return page_cache->page_info;
500 #define PAGE_CACHE_STRING "ev-page-cache"
503 ev_page_cache_get (EvDocument *document)
505 EvPageCache *page_cache;
507 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
509 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
510 if (page_cache == NULL) {
511 page_cache = ev_page_cache_new (document);
512 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);