1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
25 double uniform_height;
27 double max_width_page_width;
28 double max_width_page_height;
29 double max_height_page_width;
30 double max_height_page_height;
32 EvPageCacheInfo *size_cache;
35 struct _EvPageCacheClass
37 GObjectClass parent_class;
39 void (* page_changed) (EvPageCache *page_cache, gint page);
48 static guint signals[N_SIGNALS] = {0, };
50 static void ev_page_cache_init (EvPageCache *page_cache);
51 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
52 static void ev_page_cache_finalize (GObject *object);
54 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
57 ev_page_cache_init (EvPageCache *page_cache)
59 page_cache->current_page = -1;
63 ev_page_cache_class_init (EvPageCacheClass *class)
65 GObjectClass *object_class;
67 object_class = G_OBJECT_CLASS (class);
69 object_class->finalize = ev_page_cache_finalize;
71 signals [PAGE_CHANGED] =
72 g_signal_new ("page-changed",
75 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
77 g_cclosure_marshal_VOID__INT,
84 ev_page_cache_finalize (GObject *object)
86 EvPageCache *page_cache;
88 page_cache = EV_PAGE_CACHE (object);
90 g_free (page_cache->title);
91 g_free (page_cache->size_cache);
95 _ev_page_cache_new (EvDocument *document)
97 EvDocumentInfo *doc_info;
98 EvPageCache *page_cache;
99 EvPageCacheInfo *info;
102 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
104 ev_document_doc_mutex_lock ();
106 /* We read page information out of the document */
108 /* Assume all pages are the same size until proven otherwise */
109 page_cache->uniform = TRUE;
110 page_cache->n_pages = ev_document_get_n_pages (document);
111 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
112 page_cache->max_width_page_width = 0;
113 page_cache->max_width_page_height = 0;
114 page_cache->max_height_page_width = 0;
115 page_cache->max_height_page_height = 0;
117 doc_info = ev_document_get_info (document);
118 if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
119 page_cache->title = g_strdup (doc_info->title);
121 page_cache->title = NULL;
125 for (i = 0; i < page_cache->n_pages; i++) {
126 double page_width = 0;
127 double page_height = 0;
129 ev_document_get_page_size (document, i, &page_width, &page_height);
130 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
132 if (page_width > page_cache->max_width_page_width) {
133 page_cache->max_width_page_width = page_width;
134 page_cache->max_width_page_height = page_height;
137 if (page_height > page_cache->max_height_page_height) {
138 page_cache->max_height_page_width = page_width;
139 page_cache->max_height_page_height = page_height;
143 page_cache->uniform_width = page_width;
144 page_cache->uniform_height = page_height;
145 } else if (page_cache->uniform &&
146 (page_cache->uniform_width != page_width ||
147 page_cache->uniform_height != page_height)) {
148 /* It's a different page size. Backfill the array. */
151 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
153 for (j = 0; j < i; j++) {
154 info = &(page_cache->size_cache [j]);
155 info->width = page_cache->uniform_width;
156 info->height = page_cache->uniform_height;
158 page_cache->uniform = FALSE;
162 if (! page_cache->uniform) {
163 info = &(page_cache->size_cache [i]);
165 info->width = page_width;
166 info->height = page_height;
170 /* make some sanity check assertions */
171 if (! page_cache->uniform)
172 g_assert (page_cache->size_cache != NULL);
173 if (page_cache->uniform)
174 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
176 ev_document_doc_mutex_unlock ();
178 if (page_cache->n_pages > 0)
179 ev_page_cache_set_current_page (page_cache, 0);
185 ev_page_cache_get_n_pages (EvPageCache *page_cache)
187 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
189 return page_cache->n_pages;
193 ev_page_cache_get_current_page (EvPageCache *page_cache)
195 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
197 return page_cache->current_page;
201 ev_page_cache_set_current_page (EvPageCache *page_cache,
204 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
205 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
207 if (page == page_cache->current_page)
210 page_cache->current_page = page;
211 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
215 ev_page_cache_set_page_label (EvPageCache *page_cache,
216 const char *page_label)
222 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
223 g_return_val_if_fail (page_label != NULL, FALSE);
225 /* First, look for a literal label match */
226 for (i = 0; i < page_cache->n_pages; i ++) {
227 if (page_cache->page_labels[i] != NULL &&
228 ! strcmp (page_label, page_cache->page_labels[i])) {
229 ev_page_cache_set_current_page (page_cache, i);
234 /* Next, parse the label, and see if the number fits */
235 value = strtol (page_label, &endptr, 10);
236 if (endptr[0] == '\0') {
237 /* Page number is an integer */
238 page = MIN (G_MAXINT, value);
240 /* convert from a page label to a page offset */
243 page < page_cache->n_pages &&
244 page_cache->page_labels[page] == NULL) {
245 ev_page_cache_set_current_page (page_cache, page);
254 ev_page_cache_set_link (EvPageCache *page_cache,
257 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
258 g_return_if_fail (EV_IS_LINK (link));
260 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
264 ev_page_cache_get_title (EvPageCache *page_cache)
266 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
268 return page_cache->title;
272 ev_page_cache_get_size (EvPageCache *page_cache,
278 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
279 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
281 if (page_cache->uniform) {
283 *width = page_cache->uniform_width;
285 *height = page_cache->uniform_height;
287 EvPageCacheInfo *info;
289 info = &(page_cache->size_cache [page]);
292 *width = info->width;
294 *height = info->height;
298 *width = (*width) * scale;
300 *height = (*height) * scale;
305 /* Note that these aren't necessarily from the same page.
308 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
313 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
316 *width = page_cache->max_width_page_width * scale;
318 *height = page_cache->max_width_page_height * scale;
322 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
327 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
330 *width = page_cache->max_height_page_width * scale;
332 *height = page_cache->max_height_page_height * scale;
337 ev_page_cache_get_page_label (EvPageCache *page_cache,
340 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
341 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
343 if (page_cache->page_labels[page] == NULL)
344 return g_strdup_printf ("%d", page + 1);
346 return g_strdup (page_cache->page_labels[page]);
351 ev_page_cache_next_page (EvPageCache *page_cache)
353 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
355 if (page_cache->current_page >= page_cache->n_pages - 1)
358 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
364 ev_page_cache_prev_page (EvPageCache *page_cache)
366 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
368 if (page_cache->current_page <= 0)
371 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);