1 #include "ev-page-cache.h"
2 #include "ev-job-queue.h"
6 typedef struct _EvPageCacheInfo
27 double uniform_height;
29 double max_width_page_width;
30 double max_width_page_height;
31 double max_height_page_width;
32 double max_height_page_height;
34 EvPageCacheInfo *size_cache;
37 struct _EvPageCacheClass
39 GObjectClass parent_class;
41 void (* page_changed) (EvPageCache *page_cache, gint page);
50 static guint signals[N_SIGNALS] = {0, };
52 static void ev_page_cache_init (EvPageCache *page_cache);
53 static void ev_page_cache_class_init (EvPageCacheClass *page_cache);
54 static void ev_page_cache_finalize (GObject *object);
56 G_DEFINE_TYPE (EvPageCache, ev_page_cache, G_TYPE_OBJECT)
59 ev_page_cache_init (EvPageCache *page_cache)
61 page_cache->current_page = -1;
65 ev_page_cache_class_init (EvPageCacheClass *class)
67 GObjectClass *object_class;
69 object_class = G_OBJECT_CLASS (class);
71 object_class->finalize = ev_page_cache_finalize;
73 signals [PAGE_CHANGED] =
74 g_signal_new ("page-changed",
77 G_STRUCT_OFFSET (EvPageCacheClass, page_changed),
79 g_cclosure_marshal_VOID__INT,
86 ev_page_cache_finalize (GObject *object)
88 EvPageCache *page_cache;
90 page_cache = EV_PAGE_CACHE (object);
92 g_free (page_cache->title);
93 g_free (page_cache->size_cache);
97 _ev_page_cache_new (EvDocument *document)
99 EvDocumentInfo *doc_info;
100 EvPageCache *page_cache;
101 EvPageCacheInfo *info;
104 page_cache = (EvPageCache *) g_object_new (EV_TYPE_PAGE_CACHE, NULL);
106 ev_document_doc_mutex_lock ();
108 /* We read page information out of the document */
110 /* Assume all pages are the same size until proven otherwise */
111 page_cache->uniform = TRUE;
112 page_cache->has_labels = FALSE;
113 page_cache->n_pages = ev_document_get_n_pages (document);
114 page_cache->page_labels = g_new0 (char *, page_cache->n_pages);
115 page_cache->max_width_page_width = 0;
116 page_cache->max_width_page_height = 0;
117 page_cache->max_height_page_width = 0;
118 page_cache->max_height_page_height = 0;
120 doc_info = ev_document_get_info (document);
121 if (doc_info->fields_mask & EV_DOCUMENT_INFO_TITLE) {
122 page_cache->title = g_strdup (doc_info->title);
124 page_cache->title = NULL;
128 for (i = 0; i < page_cache->n_pages; i++) {
129 double page_width = 0;
130 double page_height = 0;
132 ev_document_get_page_size (document, i, &page_width, &page_height);
134 page_cache->page_labels[i] = ev_document_get_page_label (document, i);
136 if (!page_cache->has_labels && page_cache->page_labels[i] != NULL) {
137 gchar *expected_label;
139 expected_label = g_strdup_printf ("%d", i + 1);
140 if (strcmp (expected_label, page_cache->page_labels[i]))
141 page_cache->has_labels = TRUE;
142 g_free (expected_label);
145 if (page_width > page_cache->max_width_page_width) {
146 page_cache->max_width_page_width = page_width;
147 page_cache->max_width_page_height = page_height;
150 if (page_height > page_cache->max_height_page_height) {
151 page_cache->max_height_page_width = page_width;
152 page_cache->max_height_page_height = page_height;
156 page_cache->uniform_width = page_width;
157 page_cache->uniform_height = page_height;
158 } else if (page_cache->uniform &&
159 (page_cache->uniform_width != page_width ||
160 page_cache->uniform_height != page_height)) {
161 /* It's a different page size. Backfill the array. */
164 page_cache->size_cache = g_new0 (EvPageCacheInfo, page_cache->n_pages);
166 for (j = 0; j < i; j++) {
167 info = &(page_cache->size_cache [j]);
168 info->width = page_cache->uniform_width;
169 info->height = page_cache->uniform_height;
171 page_cache->uniform = FALSE;
175 if (! page_cache->uniform) {
176 info = &(page_cache->size_cache [i]);
178 info->width = page_width;
179 info->height = page_height;
183 /* make some sanity check assertions */
184 if (! page_cache->uniform)
185 g_assert (page_cache->size_cache != NULL);
186 if (page_cache->uniform)
187 g_assert (page_cache->uniform_width > 0 && page_cache->uniform_height > 0);
189 ev_document_doc_mutex_unlock ();
191 if (page_cache->n_pages > 0)
192 ev_page_cache_set_current_page (page_cache, 0);
198 ev_page_cache_get_n_pages (EvPageCache *page_cache)
200 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
202 return page_cache->n_pages;
206 ev_page_cache_get_current_page (EvPageCache *page_cache)
208 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), 0);
210 return page_cache->current_page;
214 ev_page_cache_set_current_page (EvPageCache *page_cache,
217 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
218 g_return_if_fail (page >= 0 || page < page_cache->n_pages);
220 if (page == page_cache->current_page)
223 page_cache->current_page = page;
224 g_signal_emit (page_cache, signals[PAGE_CHANGED], 0, page);
228 ev_page_cache_set_page_label (EvPageCache *page_cache,
229 const char *page_label)
235 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
236 g_return_val_if_fail (page_label != NULL, FALSE);
238 /* First, look for a literal label match */
239 for (i = 0; i < page_cache->n_pages; i ++) {
240 if (page_cache->page_labels[i] != NULL &&
241 ! strcmp (page_label, page_cache->page_labels[i])) {
242 ev_page_cache_set_current_page (page_cache, i);
247 /* Next, parse the label, and see if the number fits */
248 value = strtol (page_label, &endptr, 10);
249 if (endptr[0] == '\0') {
250 /* Page number is an integer */
251 page = MIN (G_MAXINT, value);
253 /* convert from a page label to a page offset */
256 page < page_cache->n_pages &&
257 page_cache->page_labels[page] == NULL) {
258 ev_page_cache_set_current_page (page_cache, page);
267 ev_page_cache_set_link (EvPageCache *page_cache,
270 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
271 g_return_if_fail (EV_IS_LINK (link));
273 ev_page_cache_set_current_page (page_cache, ev_link_get_page (link));
277 ev_page_cache_get_title (EvPageCache *page_cache)
279 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
281 return page_cache->title;
285 ev_page_cache_get_size (EvPageCache *page_cache,
291 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
292 g_return_if_fail (page >= 0 && page < page_cache->n_pages);
294 if (page_cache->uniform) {
296 *width = page_cache->uniform_width;
298 *height = page_cache->uniform_height;
300 EvPageCacheInfo *info;
302 info = &(page_cache->size_cache [page]);
305 *width = info->width;
307 *height = info->height;
311 *width = (*width) * scale;
313 *height = (*height) * scale;
318 /* Note that these aren't necessarily from the same page.
321 ev_page_cache_get_max_width_size (EvPageCache *page_cache,
326 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
329 *width = page_cache->max_width_page_width * scale;
331 *height = page_cache->max_width_page_height * scale;
335 ev_page_cache_get_max_height_size (EvPageCache *page_cache,
340 g_return_if_fail (EV_IS_PAGE_CACHE (page_cache));
343 *width = page_cache->max_height_page_width * scale;
345 *height = page_cache->max_height_page_height * scale;
350 ev_page_cache_get_page_label (EvPageCache *page_cache,
353 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), NULL);
354 g_return_val_if_fail (page >= 0 && page < page_cache->n_pages, NULL);
356 if (page_cache->page_labels[page] == NULL)
357 return g_strdup_printf ("%d", page + 1);
359 return g_strdup (page_cache->page_labels[page]);
363 ev_page_cache_has_nonnumeric_page_labels (EvPageCache *page_cache)
365 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
366 return page_cache->has_labels;
370 ev_page_cache_next_page (EvPageCache *page_cache)
372 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
374 if (page_cache->current_page >= page_cache->n_pages - 1)
377 ev_page_cache_set_current_page (page_cache, page_cache->current_page + 1);
383 ev_page_cache_prev_page (EvPageCache *page_cache)
385 g_return_val_if_fail (EV_IS_PAGE_CACHE (page_cache), FALSE);
387 if (page_cache->current_page <= 0)
390 ev_page_cache_set_current_page (page_cache, page_cache->current_page - 1);