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;
36 EvOrientation orientation;
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->orientation == EV_ORIENTATION_LANDSCAPE ||
115 page_cache->orientation == EV_ORIENTATION_SEASCAPE);
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);
121 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages / 2 + 1);
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 + 1) * 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 + page_height;
139 saved_height = page_cache->height_to_page [i];
144 for (i = 0; i < page_cache->n_pages; i += 2) {
145 if (page_cache->uniform) {
147 uniform_height = page_cache->uniform_height;
149 uniform_height = page_cache->uniform_width;
151 page_cache->dual_height_to_page [i / 2] = (i / 2 + 1) * uniform_height;
154 page_height = page_cache->size_cache [i].height;
155 next_page_height = page_cache->size_cache [i + 1].height;
157 page_height = page_cache->size_cache [i].width;
158 next_page_height = page_cache->size_cache [i + 1].width;
160 if (i == page_cache->n_pages - 1) {
161 page_cache->dual_height_to_page [i / 2] =
162 saved_height + page_height;
165 page_cache->dual_height_to_page [i / 2] = saved_height +
166 MAX(page_height, next_page_height);
167 saved_height = page_cache->dual_height_to_page [i / 2];
174 ev_page_cache_new (EvDocument *document)
176 EvPageCache *page_cache;
177 EvPageCacheInfo *info;
180 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
182 ev_document_doc_mutex_lock ();
184 /* We read page information out of the document */
186 /* Assume all pages are the same size until proven otherwise */
187 page_cache->uniform = TRUE;
188 page_cache->has_labels = FALSE;
189 page_cache->orientation = ev_document_get_orientation (document);
190 page_cache->n_pages = ev_document_get_n_pages (document);
191 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
192 page_cache->max_width = 0;
193 page_cache->max_height = 0;
194 page_cache->page_info = ev_document_get_info (document);
196 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
197 page_cache->title = g_strdup (page_cache->page_info->title);
199 page_cache->title = NULL;
202 for (i = 0; i < page_cache->n_pages; i++) {
203 double page_width = 0;
204 double page_height = 0;
206 ev_document_get_page_size (document, i, &page_width, &page_height);
208 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
210 if (page_cache->page_labels[i] != NULL) {
212 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
213 g_utf8_strlen (page_cache->page_labels[i], 256));
214 if (!page_cache->has_labels) {
215 gchar *expected_label;
217 expected_label = g_strdup_printf ("%d", i + 1);
218 if (strcmp (expected_label, page_cache->page_labels[i]))
219 page_cache->has_labels = TRUE;
220 g_free (expected_label);
224 if (page_width > page_cache->max_width) {
225 page_cache->max_width = page_width;
228 if (page_height > page_cache->max_height) {
229 page_cache->max_height = page_height;
233 page_cache->uniform_width = page_width;
234 page_cache->uniform_height = page_height;
235 } else if (page_cache->uniform &&
236 (page_cache->uniform_width != page_width ||
237 page_cache->uniform_height != page_height)) {
238 /* It's a different page size. Backfill the array. */
241 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
243 for (j = 0; j < i; j++) {
244 info = &(page_cache->size_cache [j]);
245 info->width = page_cache->uniform_width;
246 info->height = page_cache->uniform_height;
248 page_cache->uniform = FALSE;
252 if (! page_cache->uniform) {
253 info = &(page_cache->size_cache [i]);
255 info->width = page_width;
256 info->height = page_height;
260 build_height_to_page (page_cache);
262 /* make some sanity check assertions */
263 if (! page_cache->uniform)
264 g_assert (page_cache->size_cache != NULL);
265 if (page_cache->uniform)
266 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
268 ev_document_doc_mutex_unlock ();
270 if (page_cache->n_pages > 0)
271 ev_page_cache_set_current_page (page_cache, 0);
277 ev_page_cache_get_n_pages (EvPageCache *page_cache)
279 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
281 return page_cache->n_pages;
285 ev_page_cache_get_current_page (EvPageCache *page_cache)
287 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
289 return page_cache->current_page;
293 ev_page_cache_set_current_page (EvPageCache *page_cache,
296 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
297 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
299 if (page == page_cache->current_page)
302 page_cache->current_page = page;
303 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
307 ev_page_cache_set_page_label (EvPageCache *page_cache,
308 const char *page_label)
314 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
315 g_return_val_if_fail (page_label != NULL, FALSE);
317 /* First, look for a literal label match */
318 for (i = 0; i < page_cache->n_pages; i ++) {
319 if (page_cache->page_labels[i] != NULL &&
320 ! strcmp (page_label, page_cache->page_labels[i])) {
321 ev_page_cache_set_current_page (page_cache, i);
326 /* Next, parse the label, and see if the number fits */
327 value = strtol (page_label, &endptr, 10);
328 if (endptr[0] == '\0') {
329 /* Page number is an integer */
330 page = MIN (G_MAXINT, value);
332 /* convert from a page label to a page offset */
335 page < page_cache->n_pages &&
336 page_cache->page_labels[page] == NULL) {
337 ev_page_cache_set_current_page (page_cache, page);
346 ev_page_cache_set_link (EvPageCache *page_cache,
349 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
350 g_return_if_fail (EV_IS_LINK (link));
352 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
356 ev_page_cache_get_title (EvPageCache *page_cache)
358 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
360 return page_cache->title;
364 ev_page_cache_get_size (EvPageCache *page_cache,
366 EvOrientation orientation,
371 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
372 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
374 if (page_cache->uniform) {
376 *width = page_cache->uniform_width;
378 *height = page_cache->uniform_height;
380 EvPageCacheInfo *info;
382 info = &(page_cache->size_cache [page]);
385 *width = info->width;
387 *height = info->height;
390 if (orientation == EV_ORIENTATION_PORTRAIT ||
391 orientation == EV_ORIENTATION_UPSIDEDOWN) {
393 *width = (int) ((*width) * scale + 0.5);
395 *height = (int) ((*height) * scale + 0.5);
398 *width = (int) ((*height) * scale + 0.5);
400 *height = (int) ((*width) * scale + 0.5);
405 ev_page_cache_get_max_width (EvPageCache *page_cache,
406 EvOrientation orientation,
410 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
413 if (orientation == EV_ORIENTATION_PORTRAIT ||
414 orientation == EV_ORIENTATION_UPSIDEDOWN) {
415 *width = page_cache->max_width * scale;
417 *width = page_cache->max_height * scale;
423 ev_page_cache_get_max_height (EvPageCache *page_cache,
424 EvOrientation orientation,
428 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
431 if (orientation == EV_ORIENTATION_PORTRAIT ||
432 orientation == EV_ORIENTATION_UPSIDEDOWN) {
433 *height = page_cache->max_height * scale;
435 *height = page_cache->max_width * scale;
441 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
443 EvOrientation orientation,
449 double dual_result = 0.0;
451 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
453 if (page_cache->orientation != orientation) {
454 page_cache->orientation = orientation;
455 build_height_to_page (page_cache);
459 result = page_cache->height_to_page [page - 1];
462 *height = result * scale;
465 dual_result = page_cache->dual_height_to_page [page / 2 - 1];
468 *dual_height = dual_result * scale;
472 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
474 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
476 return page_cache->max_label_chars;
480 ev_page_cache_get_page_label (EvPageCache *page_cache,
483 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
484 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
486 if (page_cache->page_labels[page] == NULL)
487 return g_strdup_printf ("%d", page + 1);
489 return g_strdup (page_cache->page_labels[page]);
493 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
495 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
496 return page_cache->has_labels;
499 const EvDocumentInfo *
500 ev_page_cache_get_info (EvPageCache *page_cache)
502 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
504 return page_cache->page_info;
509 ev_page_cache_next_page (EvPageCache *page_cache)
511 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
513 if (page_cache->current_page >= page_cache->n_pages - 1)
516 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
522 ev_page_cache_prev_page (EvPageCache *page_cache)
524 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
526 if (page_cache->current_page <= 0)
529 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);
533 #define PAGE_CACHE_STRING "ev-page-cache"
536 ev_page_cache_get (EvDocument *document)
538 EvPageCache *page_cache;
540 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
542 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
543 if (page_cache == NULL) {
544 page_cache = ev_page_cache_new (document);
545 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);