1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
28 double uniform_height;
32 double* height_to_page;
33 double* dual_height_to_page;
35 EvPageCacheInfo *size_cache;
36 EvDocumentInfo *page_info;
39 struct _EvPageCacheClass
41 GObjectClass parent_class;
43 void (* page_changed) (EvPageCache *page_cache, gint page);
52 static guint signals[N_SIGNALS] = {0, };
54 static void ev_page_cache_init (EvPageCache *page_cache);
55 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
56 static void ev_page_cache_finalize (GObject *object);
58 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
61 ev_page_cache_init (EvPageCache *page_cache)
63 page_cache->current_page = -1;
64 page_cache->max_label_chars = 0;
68 ev_page_cache_class_init (EvPageCacheClass *class)
70 GObjectClass *object_class;
72 object_class = G_OBJECT_CLASS (class);
74 object_class->finalize = ev_page_cache_finalize;
76 signals [PAGE_CHANGED] =
77 g_signal_new ("page-changed",
80 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
82 g_cclosure_marshal_VOID__INT,
89 ev_page_cache_finalize (GObject *object)
91 EvPageCache *page_cache;
93 page_cache = EV_PAGE_CACHE (object);
95 g_free (page_cache->title);
96 g_free (page_cache->size_cache);
97 g_free (page_cache->height_to_page);
98 g_free (page_cache->dual_height_to_page);
100 ev_document_info_free (page_cache->page_info);
104 ev_page_cache_new (EvDocument *document)
106 EvPageCache *page_cache;
107 EvPageCacheInfo *info;
111 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
113 ev_document_doc_mutex_lock ();
115 /* We read page information out of the document */
117 /* Assume all pages are the same size until proven otherwise */
118 page_cache->uniform = TRUE;
119 page_cache->has_labels = FALSE;
120 page_cache->n_pages = ev_document_get_n_pages (document);
121 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
122 page_cache->max_width = 0;
123 page_cache->max_height = 0;
124 page_cache->page_info = ev_document_get_info (document);
126 if (page_cache->page_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
127 page_cache->title = g_strdup (page_cache->page_info->title);
129 page_cache->title = NULL;
132 for (i = 0; i < page_cache->n_pages; i++) {
133 double page_width = 0;
134 double page_height = 0;
136 ev_document_get_page_size (document, i, &page_width, &page_height);
138 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
140 if (page_cache->page_labels[i] != NULL) {
142 page_cache->max_label_chars = MAX(page_cache->max_label_chars,
143 g_utf8_strlen (page_cache->page_labels[i], 256));
144 if (!page_cache->has_labels) {
145 gchar *expected_label;
147 expected_label = g_strdup_printf ("%d", i + 1);
148 if (strcmp (expected_label, page_cache->page_labels[i]))
149 page_cache->has_labels = TRUE;
150 g_free (expected_label);
154 if (page_width > page_cache->max_width) {
155 page_cache->max_width = page_width;
158 if (page_height > page_cache->max_height) {
159 page_cache->max_height = page_height;
163 page_cache->uniform_width = page_width;
164 page_cache->uniform_height = page_height;
165 } else if (page_cache->uniform &&
166 (page_cache->uniform_width != page_width ||
167 page_cache->uniform_height != page_height)) {
168 /* It's a different page size. Backfill the array. */
171 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
173 for (j = 0; j < i; j++) {
174 info = &(page_cache->size_cache [j]);
175 info->width = page_cache->uniform_width;
176 info->height = page_cache->uniform_height;
178 page_cache->uniform = FALSE;
182 if (! page_cache->uniform) {
183 info = &(page_cache->size_cache [i]);
185 info->width = page_width;
186 info->height = page_height;
190 page_cache->height_to_page = g_new0(double, page_cache->n_pages);
191 page_cache->dual_height_to_page = g_new0(double, page_cache->n_pages / 2 + 1);
194 for (i = 0; i < page_cache->n_pages; i++) {
196 if (page_cache->uniform) {
197 page_cache->height_to_page [i] = (i + 1) * page_cache->uniform_height;
199 page_cache->height_to_page [i] = saved_height + page_cache->size_cache [i].height;
200 saved_height = page_cache->height_to_page [i];
205 for (i = 0; i < page_cache->n_pages; i += 2) {
207 if (page_cache->uniform) {
208 page_cache->dual_height_to_page [i / 2] = (i / 2 + 1) * page_cache->uniform_height;
210 if (i == page_cache->n_pages - 1) {
211 page_cache->dual_height_to_page [i / 2] =
212 saved_height + page_cache->size_cache [i].height;
215 page_cache->dual_height_to_page [i / 2] = saved_height +
216 MAX(page_cache->size_cache [i].height,
217 page_cache->size_cache [i + 1].height);
218 saved_height = page_cache->dual_height_to_page [i / 2];
223 /* make some sanity check assertions */
224 if (! page_cache->uniform)
225 g_assert (page_cache->size_cache != NULL);
226 if (page_cache->uniform)
227 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
229 ev_document_doc_mutex_unlock ();
231 if (page_cache->n_pages > 0)
232 ev_page_cache_set_current_page (page_cache, 0);
238 ev_page_cache_get_n_pages (EvPageCache *page_cache)
240 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
242 return page_cache->n_pages;
246 ev_page_cache_get_current_page (EvPageCache *page_cache)
248 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
250 return page_cache->current_page;
254 ev_page_cache_set_current_page (EvPageCache *page_cache,
257 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
258 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
260 if (page == page_cache->current_page)
263 page_cache->current_page = page;
264 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
268 ev_page_cache_set_page_label (EvPageCache *page_cache,
269 const char *page_label)
275 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
276 g_return_val_if_fail (page_label != NULL, FALSE);
278 /* First, look for a literal label match */
279 for (i = 0; i < page_cache->n_pages; i ++) {
280 if (page_cache->page_labels[i] != NULL &&
281 ! strcmp (page_label, page_cache->page_labels[i])) {
282 ev_page_cache_set_current_page (page_cache, i);
287 /* Next, parse the label, and see if the number fits */
288 value = strtol (page_label, &endptr, 10);
289 if (endptr[0] == '\0') {
290 /* Page number is an integer */
291 page = MIN (G_MAXINT, value);
293 /* convert from a page label to a page offset */
296 page < page_cache->n_pages &&
297 page_cache->page_labels[page] == NULL) {
298 ev_page_cache_set_current_page (page_cache, page);
307 ev_page_cache_set_link (EvPageCache *page_cache,
310 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
311 g_return_if_fail (EV_IS_LINK (link));
313 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
317 ev_page_cache_get_title (EvPageCache *page_cache)
319 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
321 return page_cache->title;
325 ev_page_cache_get_size (EvPageCache *page_cache,
331 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
332 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
334 if (page_cache->uniform) {
336 *width = page_cache->uniform_width;
338 *height = page_cache->uniform_height;
340 EvPageCacheInfo *info;
342 info = &(page_cache->size_cache [page]);
345 *width = info->width;
347 *height = info->height;
351 *width = (int) ((*width) * scale + 0.5);
353 *height = (int) ((*height) * scale + 0.5);
359 ev_page_cache_get_max_width (EvPageCache *page_cache,
363 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
366 *width = page_cache->max_width * scale;
370 ev_page_cache_get_max_height (EvPageCache *page_cache,
374 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
377 *height = page_cache->max_height * scale;
381 ev_page_cache_get_height_to_page (EvPageCache *page_cache,
388 double dual_result = 0.0;
390 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
393 result = page_cache->height_to_page [page - 1];
396 *height = result * scale;
399 dual_result = page_cache->dual_height_to_page [page / 2 - 1];
402 *dual_height = dual_result * scale;
406 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
408 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
410 return page_cache->max_label_chars;
414 ev_page_cache_get_page_label (EvPageCache *page_cache,
417 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
418 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
420 if (page_cache->page_labels[page] == NULL)
421 return g_strdup_printf ("%d", page + 1);
423 return g_strdup (page_cache->page_labels[page]);
427 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
429 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
430 return page_cache->has_labels;
433 const EvDocumentInfo *
434 ev_page_cache_get_info (EvPageCache *page_cache)
436 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
438 return page_cache->page_info;
443 ev_page_cache_next_page (EvPageCache *page_cache)
445 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
447 if (page_cache->current_page >= page_cache->n_pages - 1)
450 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
456 ev_page_cache_prev_page (EvPageCache *page_cache)
458 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
460 if (page_cache->current_page <= 0)
463 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);
467 #define PAGE_CACHE_STRING "ev-page-cache"
470 ev_page_cache_get (EvDocument *document)
472 EvPageCache *page_cache;
474 g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
476 page_cache = g_object_get_data (G_OBJECT (document), PAGE_CACHE_STRING);
477 if (page_cache == NULL) {
478 page_cache = ev_page_cache_new (document);
479 g_object_set_data_full (G_OBJECT (document), PAGE_CACHE_STRING, page_cache, g_object_unref);