1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
28 double uniform_height;
30 double max_width_page_width;
31 double max_width_page_height;
32 double max_height_page_width;
33 double max_height_page_height;
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 ev_document_info_free (page_cache->page_info);
101 _ev_page_cache_new (EvDocument *document)
103 EvDocumentInfo *doc_info;
104 EvPageCache *page_cache;
105 EvPageCacheInfo *info;
108 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
110 ev_document_doc_mutex_lock ();
112 /* We read page information out of the document */
114 /* Assume all pages are the same size until proven otherwise */
115 page_cache->uniform = TRUE;
116 page_cache->has_labels = FALSE;
117 page_cache->n_pages = ev_document_get_n_pages (document);
118 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
119 page_cache->max_width_page_width = 0;
120 page_cache->max_width_page_height = 0;
121 page_cache->max_height_page_width = 0;
122 page_cache->max_height_page_height = 0;
124 doc_info = ev_document_get_info (document);
125 if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
126 page_cache->title = g_strdup (doc_info->title);
128 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_page_width) {
155 page_cache->max_width_page_width = page_width;
156 page_cache->max_width_page_height = page_height;
159 if (page_height > page_cache->max_height_page_height) {
160 page_cache->max_height_page_width = page_width;
161 page_cache->max_height_page_height = page_height;
165 page_cache->uniform_width = page_width;
166 page_cache->uniform_height = page_height;
167 } else if (page_cache->uniform &&
168 (page_cache->uniform_width != page_width ||
169 page_cache->uniform_height != page_height)) {
170 /* It's a different page size. Backfill the array. */
173 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
175 for (j = 0; j < i; j++) {
176 info = &(page_cache->size_cache [j]);
177 info->width = page_cache->uniform_width;
178 info->height = page_cache->uniform_height;
180 page_cache->uniform = FALSE;
184 if (! page_cache->uniform) {
185 info = &(page_cache->size_cache [i]);
187 info->width = page_width;
188 info->height = page_height;
192 page_cache->page_info = ev_document_get_info (document);
194 /* make some sanity check assertions */
195 if (! page_cache->uniform)
196 g_assert (page_cache->size_cache != NULL);
197 if (page_cache->uniform)
198 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
200 ev_document_doc_mutex_unlock ();
202 if (page_cache->n_pages > 0)
203 ev_page_cache_set_current_page (page_cache, 0);
209 ev_page_cache_get_n_pages (EvPageCache *page_cache)
211 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
213 return page_cache->n_pages;
217 ev_page_cache_get_current_page (EvPageCache *page_cache)
219 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
221 return page_cache->current_page;
225 ev_page_cache_set_current_page (EvPageCache *page_cache,
228 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
229 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
231 if (page == page_cache->current_page)
234 page_cache->current_page = page;
235 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
239 ev_page_cache_set_page_label (EvPageCache *page_cache,
240 const char *page_label)
246 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
247 g_return_val_if_fail (page_label != NULL, FALSE);
249 /* First, look for a literal label match */
250 for (i = 0; i < page_cache->n_pages; i ++) {
251 if (page_cache->page_labels[i] != NULL &&
252 ! strcmp (page_label, page_cache->page_labels[i])) {
253 ev_page_cache_set_current_page (page_cache, i);
258 /* Next, parse the label, and see if the number fits */
259 value = strtol (page_label, &endptr, 10);
260 if (endptr[0] == '\0') {
261 /* Page number is an integer */
262 page = MIN (G_MAXINT, value);
264 /* convert from a page label to a page offset */
267 page < page_cache->n_pages &&
268 page_cache->page_labels[page] == NULL) {
269 ev_page_cache_set_current_page (page_cache, page);
278 ev_page_cache_set_link (EvPageCache *page_cache,
281 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
282 g_return_if_fail (EV_IS_LINK (link));
284 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
288 ev_page_cache_get_title (EvPageCache *page_cache)
290 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
292 return page_cache->title;
296 ev_page_cache_get_size (EvPageCache *page_cache,
302 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
303 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
305 if (page_cache->uniform) {
307 *width = page_cache->uniform_width;
309 *height = page_cache->uniform_height;
311 EvPageCacheInfo *info;
313 info = &(page_cache->size_cache [page]);
316 *width = info->width;
318 *height = info->height;
322 *width = (int) ((*width) * scale + 0.5);
324 *height = (int) ((*height) * scale + 0.5);
329 /* Note that these aren't necessarily from the same page.
332 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
337 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
340 *width = page_cache->max_width_page_width * scale;
342 *height = page_cache->max_width_page_height * scale;
346 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
351 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
354 *width = page_cache->max_height_page_width * scale;
356 *height = page_cache->max_height_page_height * scale;
360 ev_page_cache_get_max_label_chars (EvPageCache *page_cache)
362 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
364 return page_cache->max_label_chars;
368 ev_page_cache_get_page_label (EvPageCache *page_cache,
371 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
372 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
374 if (page_cache->page_labels[page] == NULL)
375 return g_strdup_printf ("%d", page + 1);
377 return g_strdup (page_cache->page_labels[page]);
381 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
383 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
384 return page_cache->has_labels;
387 const EvDocumentInfo *
388 ev_page_cache_get_info (EvPageCache *page_cache)
390 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
392 return page_cache->page_info;
397 ev_page_cache_next_page (EvPageCache *page_cache)
399 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
401 if (page_cache->current_page >= page_cache->n_pages - 1)
404 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
410 ev_page_cache_prev_page (EvPageCache *page_cache)
412 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
414 if (page_cache->current_page <= 0)
417 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);