1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-selection.h"
5 #include "ev-document-images.h"
8 typedef struct _CacheJobInfo
13 /* Data we get from rendering */
17 GdkRegion *text_mapping;
20 * Selection_points are the coordinates encapsulated in selection.
21 * target_points is the target selection size. */
22 EvRectangle selection_points;
23 EvRectangle target_points;
27 GdkRegion *selection_region;
34 /* We keep a link to our containing view just for style information. */
40 /* preload_cache_size is the number of pages prior to the current
41 * visible area that we cache. It's normally 1, but could be 2 in the
44 int preload_cache_size;
45 CacheJobInfo *prev_job;
46 CacheJobInfo *job_list;
47 CacheJobInfo *next_job;
50 struct _EvPixbufCacheClass
52 GObjectClass parent_class;
54 void (* job_finished) (EvPixbufCache *pixbuf_cache);
64 static guint signals[N_SIGNALS] = {0, };
66 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
67 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
68 static void ev_pixbuf_cache_finalize (GObject *object);
69 static void ev_pixbuf_cache_dispose (GObject *object);
70 static void job_finished_cb (EvJob *job,
71 EvPixbufCache *pixbuf_cache);
72 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
74 static void copy_job_to_job_info (EvJobRender *job_render,
75 CacheJobInfo *job_info,
76 EvPixbufCache *pixbuf_cache);
77 static gboolean new_selection_pixbuf_needed(EvPixbufCache *pixbuf_cache,
78 CacheJobInfo *job_info,
83 /* These are used for iterating through the prev and next arrays */
84 #define FIRST_VISABLE_PREV(pixbuf_cache) \
85 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
86 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
87 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
88 #define PAGE_CACHE_LEN(pixbuf_cache) \
89 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
91 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
94 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
96 pixbuf_cache->start_page = 0;
97 pixbuf_cache->end_page = 0;
98 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
100 pixbuf_cache->preload_cache_size = 2;
101 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
102 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
106 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
108 GObjectClass *object_class;
110 object_class = G_OBJECT_CLASS (class);
112 object_class->finalize = ev_pixbuf_cache_finalize;
113 object_class->dispose = ev_pixbuf_cache_dispose;
115 signals[JOB_FINISHED] = g_signal_new ("job-finished",
116 G_OBJECT_CLASS_TYPE (object_class),
117 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
118 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
120 g_cclosure_marshal_VOID__VOID,
125 ev_pixbuf_cache_finalize (GObject *object)
127 EvPixbufCache *pixbuf_cache;
129 pixbuf_cache = EV_PIXBUF_CACHE (object);
131 g_free (pixbuf_cache->prev_job);
132 g_free (pixbuf_cache->job_list);
133 g_free (pixbuf_cache->next_job);
137 dispose_cache_job_info (CacheJobInfo *job_info,
140 if (job_info == NULL)
143 g_signal_handlers_disconnect_by_func (job_info->job,
144 G_CALLBACK (job_finished_cb),
146 ev_job_queue_remove_job (job_info->job);
147 g_object_unref (G_OBJECT (job_info->job));
148 job_info->job = NULL;
150 if (job_info->pixbuf) {
151 g_object_unref (G_OBJECT (job_info->pixbuf));
152 job_info->pixbuf = NULL;
154 if (job_info->link_mapping) {
155 ev_link_mapping_free (job_info->link_mapping);
156 job_info->link_mapping = NULL;
158 if (job_info->image_mapping) {
159 ev_image_mapping_free (job_info->image_mapping);
160 job_info->image_mapping = NULL;
162 if (job_info->text_mapping) {
163 gdk_region_destroy (job_info->text_mapping);
164 job_info->text_mapping = NULL;
166 if (job_info->selection) {
167 g_object_unref (G_OBJECT (job_info->selection));
168 job_info->selection = NULL;
170 if (job_info->selection_region) {
171 gdk_region_destroy (job_info->selection_region);
172 job_info->selection_region = NULL;
175 g_object_unref (G_OBJECT (job_info->rc));
179 job_info->points_set = FALSE;
183 ev_pixbuf_cache_dispose (GObject *object)
185 EvPixbufCache *pixbuf_cache;
188 pixbuf_cache = EV_PIXBUF_CACHE (object);
190 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
191 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
192 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
195 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
196 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
202 ev_pixbuf_cache_new (GtkWidget *view,
203 EvDocument *document)
205 EvPixbufCache *pixbuf_cache;
207 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
208 /* This is a backlink, so we don't ref this */
209 pixbuf_cache->view = view;
210 pixbuf_cache->document = document;
216 job_finished_cb (EvJob *job,
217 EvPixbufCache *pixbuf_cache)
219 CacheJobInfo *job_info;
220 EvJobRender *job_render = EV_JOB_RENDER (job);
222 /* If the job is outside of our interest, we silently discard it */
223 if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
224 (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
225 g_object_unref (job);
229 job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
231 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
233 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
236 /* This checks a job to see if the job would generate the right sized pixbuf
237 * given a scale. If it won't, it removes the job and clears it to NULL.
240 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
241 CacheJobInfo *job_info,
242 EvPageCache *page_cache,
250 if (job_info->job == NULL)
253 ev_page_cache_get_size (page_cache,
254 EV_JOB_RENDER (job_info->job)->rc->page,
255 EV_JOB_RENDER (job_info->job)->rc->rotation,
259 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
260 height == EV_JOB_RENDER (job_info->job)->target_height)
263 g_signal_handlers_disconnect_by_func (job_info->job,
264 G_CALLBACK (job_finished_cb),
266 ev_job_queue_remove_job (job_info->job);
267 g_object_unref (job_info->job);
268 job_info->job = NULL;
271 /* Do all function that copies a job from an older cache to it's position in the
272 * new cache. It clears the old job if it doesn't have a place.
275 move_one_job (CacheJobInfo *job_info,
276 EvPixbufCache *pixbuf_cache,
278 CacheJobInfo *new_job_list,
279 CacheJobInfo *new_prev_job,
280 CacheJobInfo *new_next_job,
283 EvJobPriority priority)
285 CacheJobInfo *target_page = NULL;
287 EvJobPriority new_priority;
289 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
290 page > (end_page + pixbuf_cache->preload_cache_size)) {
291 dispose_cache_job_info (job_info, pixbuf_cache);
295 /* find the target page to copy it over to. */
296 if (page < start_page) {
297 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
299 g_assert (page_offset >= 0 &&
300 page_offset < pixbuf_cache->preload_cache_size);
301 target_page = new_prev_job + page_offset;
302 new_priority = EV_JOB_PRIORITY_LOW;
303 } else if (page > end_page) {
304 page_offset = (page - (end_page + 1));
306 g_assert (page_offset >= 0 &&
307 page_offset < pixbuf_cache->preload_cache_size);
308 target_page = new_next_job + page_offset;
309 new_priority = EV_JOB_PRIORITY_LOW;
311 page_offset = page - start_page;
312 g_assert (page_offset >= 0 &&
313 page_offset <= ((end_page - start_page) + 1));
314 new_priority = EV_JOB_PRIORITY_HIGH;
315 target_page = new_job_list + page_offset;
318 *target_page = *job_info;
319 job_info->job = NULL;
320 job_info->pixbuf = NULL;
321 job_info->link_mapping = NULL;
322 job_info->image_mapping = NULL;
324 if (new_priority != priority && target_page->job) {
325 ev_job_queue_update_job (target_page->job, new_priority);
332 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
336 CacheJobInfo *new_job_list;
337 CacheJobInfo *new_prev_job;
338 CacheJobInfo *new_next_job;
339 EvPageCache *page_cache;
342 if (pixbuf_cache->start_page == start_page &&
343 pixbuf_cache->end_page == end_page)
346 page_cache = ev_page_cache_get (pixbuf_cache->document);
348 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
349 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
350 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
352 /* We go through each job in the old cache and either clear it or move
353 * it to a new location. */
355 /* Start with the prev cache. */
356 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
357 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
359 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
361 move_one_job (pixbuf_cache->prev_job + i,
363 new_job_list, new_prev_job, new_next_job,
364 start_page, end_page, EV_JOB_PRIORITY_LOW);
369 page = pixbuf_cache->start_page;
370 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
371 move_one_job (pixbuf_cache->job_list + i,
373 new_job_list, new_prev_job, new_next_job,
374 start_page, end_page, EV_JOB_PRIORITY_HIGH);
378 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
379 if (page >= ev_page_cache_get_n_pages (page_cache)) {
380 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
382 move_one_job (pixbuf_cache->next_job + i,
384 new_job_list, new_prev_job, new_next_job,
385 start_page, end_page, EV_JOB_PRIORITY_LOW);
390 g_free (pixbuf_cache->job_list);
391 g_free (pixbuf_cache->prev_job);
392 g_free (pixbuf_cache->next_job);
394 pixbuf_cache->job_list = new_job_list;
395 pixbuf_cache->prev_job = new_prev_job;
396 pixbuf_cache->next_job = new_next_job;
398 pixbuf_cache->start_page = start_page;
399 pixbuf_cache->end_page = end_page;
403 copy_job_to_job_info (EvJobRender *job_render,
404 CacheJobInfo *job_info,
405 EvPixbufCache *pixbuf_cache)
408 job_info->points_set = FALSE;
410 if (job_info->pixbuf) {
411 g_object_unref (G_OBJECT (job_info->pixbuf));
413 job_info->pixbuf = g_object_ref (job_render->pixbuf);
416 g_object_unref (G_OBJECT (job_info->rc));
418 job_info->rc = g_object_ref (job_render->rc);
420 if (job_render->include_links) {
421 if (job_info->link_mapping)
422 ev_link_mapping_free (job_info->link_mapping);
423 job_info->link_mapping = job_render->link_mapping;
426 if (job_render->include_images) {
427 if (job_info->image_mapping)
428 ev_image_mapping_free (job_info->image_mapping);
429 job_info->image_mapping = job_render->image_mapping;
432 if (job_render->include_text) {
433 if (job_info->text_mapping)
434 gdk_region_destroy (job_info->text_mapping);
435 job_info->text_mapping = job_render->text_mapping;
438 if (job_render->include_selection) {
439 if (job_info->selection) {
440 g_object_unref (G_OBJECT (job_info->selection));
441 job_info->selection = NULL;
443 if (job_info->selection_region) {
444 gdk_region_destroy (job_info->selection_region);
445 job_info->selection_region = NULL;
448 job_info->selection_points = job_render->selection_points;
449 job_info->selection_region = gdk_region_copy (job_render->selection_region);
450 job_info->selection = g_object_ref (job_render->selection);
451 g_assert (job_info->selection_points.x1 >= 0);
455 g_signal_handlers_disconnect_by_func (job_info->job,
456 G_CALLBACK (job_finished_cb),
458 ev_job_queue_remove_job (job_info->job);
459 g_object_unref (G_OBJECT (job_info->job));
460 job_info->job = NULL;
464 static CacheJobInfo *
465 find_job_cache (EvPixbufCache *pixbuf_cache,
470 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
471 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
474 if (page < pixbuf_cache->start_page) {
475 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
477 g_assert (page_offset >= 0 &&
478 page_offset < pixbuf_cache->preload_cache_size);
479 return pixbuf_cache->prev_job + page_offset;
482 if (page > pixbuf_cache->end_page) {
483 page_offset = (page - (pixbuf_cache->end_page + 1));
485 g_assert (page_offset >= 0 &&
486 page_offset < pixbuf_cache->preload_cache_size);
487 return pixbuf_cache->next_job + page_offset;
490 page_offset = page - pixbuf_cache->start_page;
491 g_assert (page_offset >= 0 &&
492 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
493 return pixbuf_cache->job_list + page_offset;
497 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
500 EvPageCache *page_cache;
503 page_cache = ev_page_cache_get (pixbuf_cache->document);
505 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
506 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
509 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
510 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
511 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
515 #define FIRST_VISABLE_PREV(pixbuf_cache) \
516 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
519 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
521 if (GTK_WIDGET_HAS_FOCUS (widget)) {
522 *text = &widget->style->text [GTK_STATE_SELECTED];
523 *base = &widget->style->base [GTK_STATE_SELECTED];
525 *text = &widget->style->text [GTK_STATE_ACTIVE];
526 *base = &widget->style->base [GTK_STATE_ACTIVE];
531 add_job_if_needed (EvPixbufCache *pixbuf_cache,
532 CacheJobInfo *job_info,
533 EvPageCache *page_cache,
537 EvJobPriority priority)
539 gboolean include_links = FALSE;
540 gboolean include_text = FALSE;
541 gboolean include_selection = FALSE;
542 gboolean include_images = FALSE;
544 GdkColor *text, *base;
549 ev_page_cache_get_size (page_cache, page, rotation,
550 scale, &width, &height);
552 if (job_info->pixbuf &&
553 gdk_pixbuf_get_width (job_info->pixbuf) == width &&
554 gdk_pixbuf_get_height (job_info->pixbuf) == height)
557 /* make a new job now */
558 if (job_info->rc == NULL) {
559 job_info->rc = ev_render_context_new (rotation, page, scale);
561 ev_render_context_set_rotation (job_info->rc, rotation);
562 ev_render_context_set_page (job_info->rc, page);
563 ev_render_context_set_scale (job_info->rc, scale);
566 /* Figure out what else we need for this job */
567 if (job_info->link_mapping == NULL)
568 include_links = TRUE;
569 if (job_info->image_mapping == NULL)
570 include_images = TRUE;
571 if (job_info->text_mapping == NULL)
573 if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
574 include_selection = TRUE;
577 gtk_widget_ensure_style (pixbuf_cache->view);
579 get_selection_colors (pixbuf_cache->view, &text, &base);
581 job_info->job = ev_job_render_new (pixbuf_cache->document,
584 &(job_info->target_points),
590 ev_job_queue_add_job (job_info->job, priority);
591 g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
596 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
600 EvPageCache *page_cache;
601 CacheJobInfo *job_info;
605 page_cache = ev_page_cache_get (pixbuf_cache->document);
607 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
608 job_info = (pixbuf_cache->job_list + i);
609 page = pixbuf_cache->start_page + i;
611 add_job_if_needed (pixbuf_cache, job_info,
612 page_cache, page, rotation, scale,
613 EV_JOB_PRIORITY_HIGH);
616 for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
617 job_info = (pixbuf_cache->prev_job + i);
618 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
620 add_job_if_needed (pixbuf_cache, job_info,
621 page_cache, page, rotation, scale,
622 EV_JOB_PRIORITY_LOW);
625 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
626 job_info = (pixbuf_cache->next_job + i);
627 page = pixbuf_cache->end_page + 1 + i;
629 add_job_if_needed (pixbuf_cache, job_info,
630 page_cache, page, rotation, scale,
631 EV_JOB_PRIORITY_LOW);
637 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
642 GList *selection_list)
644 EvPageCache *page_cache;
646 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
648 page_cache = ev_page_cache_get (pixbuf_cache->document);
650 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
651 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
652 g_return_if_fail (end_page >= start_page);
654 /* First, resize the page_range as needed. We cull old pages
656 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
658 /* Then, we update the current jobs to see if any of them are the wrong
659 * size, we remove them if we need to. */
660 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
662 /* Next, we update the target selection for our pages */
663 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
665 /* Finally, we add the new jobs for all the sizes that don't have a
667 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
671 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
674 CacheJobInfo *job_info;
676 job_info = find_job_cache (pixbuf_cache, page);
677 if (job_info == NULL)
680 /* We don't need to wait for the idle to handle the callback */
682 EV_JOB (job_info->job)->finished) {
683 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
686 return job_info->pixbuf;
690 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
693 CacheJobInfo *job_info;
695 job_info = find_job_cache (pixbuf_cache, page);
696 if (job_info == NULL)
699 /* We don't need to wait for the idle to handle the callback */
701 EV_JOB (job_info->job)->finished) {
702 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
705 return job_info->link_mapping;
709 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
712 CacheJobInfo *job_info;
714 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
717 job_info = find_job_cache (pixbuf_cache, page);
718 if (job_info == NULL)
721 /* We don't need to wait for the idle to handle the callback */
723 EV_JOB (job_info->job)->finished) {
724 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
727 return job_info->image_mapping;
731 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
732 CacheJobInfo *job_info,
736 EvPageCache *page_cache;
739 if (job_info->selection) {
740 page_cache = ev_page_cache_get (pixbuf_cache->document);
741 ev_page_cache_get_size (page_cache, page, job_info->rc->rotation,
742 scale, &width, &height);
744 if (width != gdk_pixbuf_get_width (job_info->selection) ||
745 height != gdk_pixbuf_get_height (job_info->selection))
748 if (job_info->points_set)
755 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
756 CacheJobInfo *job_info,
760 if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
761 if (job_info->selection)
762 g_object_unref (job_info->selection);
763 job_info->selection = NULL;
764 job_info->selection_points.x1 = -1;
769 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
772 CacheJobInfo *job_info;
774 job_info = find_job_cache (pixbuf_cache, page);
775 if (job_info == NULL)
778 /* We don't need to wait for the idle to handle the callback */
780 EV_JOB (job_info->job)->finished) {
781 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
784 return job_info->text_mapping;
787 /* Clears the cache of jobs and pixbufs.
790 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
794 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
795 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
796 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
799 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
800 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
806 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
810 /* FIXME: doesn't update running jobs. */
811 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
812 CacheJobInfo *job_info;
814 job_info = pixbuf_cache->prev_job + i;
815 if (job_info->selection) {
816 g_object_unref (G_OBJECT (job_info->selection));
817 job_info->selection = NULL;
820 job_info = pixbuf_cache->next_job + i;
821 if (job_info->selection) {
822 g_object_unref (G_OBJECT (job_info->selection));
823 job_info->selection = NULL;
827 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
828 CacheJobInfo *job_info;
830 job_info = pixbuf_cache->job_list + i;
831 if (job_info->selection) {
832 g_object_unref (G_OBJECT (job_info->selection));
833 job_info->selection = NULL;
839 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache *pixbuf_cache,
844 CacheJobInfo *job_info;
846 /* the document does not implement the selection interface */
847 if (!EV_IS_SELECTION (pixbuf_cache->document))
850 job_info = find_job_cache (pixbuf_cache, page);
851 if (job_info == NULL)
854 /* No selection on this page */
855 if (!job_info->points_set)
859 g_assert (job_info->rc);
860 ev_render_context_set_scale (job_info->rc, scale);
862 /* If we have a running job, we just return what we have under the
863 * assumption that it'll be updated later and we can scale it as need
865 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
866 return job_info->selection;
868 /* Now, lets see if we need to resize the image. If we do, we clear the
870 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
872 /* Finally, we see if the two scales are the same, and get a new pixbuf
873 * if needed. We do this synchronously for now. At some point, we
874 * _should_ be able to get rid of the doc_mutex, so the synchronicity
875 * doesn't kill us. Rendering a few glyphs should really be fast.
877 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
878 EvRectangle *old_points;
879 GdkColor *text, *base;
881 /* we need to get a new selection pixbuf */
882 ev_document_doc_mutex_lock ();
883 if (job_info->selection_points.x1 < 0) {
884 g_assert (job_info->selection == NULL);
887 g_assert (job_info->selection != NULL);
888 old_points = &(job_info->selection_points);
891 if (job_info->selection_region)
892 gdk_region_destroy (job_info->selection_region);
893 job_info->selection_region =
894 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
896 &(job_info->target_points));
898 gtk_widget_ensure_style (pixbuf_cache->view);
900 get_selection_colors (pixbuf_cache->view, &text, &base);
902 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
903 job_info->rc, &(job_info->selection),
904 &(job_info->target_points),
907 job_info->selection_points = job_info->target_points;
908 ev_document_doc_mutex_unlock ();
911 *region = job_info->selection_region;
912 return job_info->selection;
916 update_job_selection (CacheJobInfo *job_info,
917 EvViewSelection *selection)
919 job_info->points_set = TRUE;
920 job_info->target_points = selection->rect;
924 clear_job_selection (CacheJobInfo *job_info)
926 job_info->points_set = FALSE;
927 job_info->selection_points.x1 = -1;
929 if (job_info->selection) {
930 g_object_unref (job_info->selection);
931 job_info->selection = NULL;
935 /* This function will reset the selection on pages that no longer have them, and
936 * will update the target_selection on those that need it. It will _not_ free
937 * the previous selection_list -- that's up to caller to do.
940 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
941 GList *selection_list)
943 EvPageCache *page_cache;
944 EvViewSelection *selection;
945 GList *list = selection_list;
949 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
951 if (!EV_IS_SELECTION (pixbuf_cache->document))
954 page_cache = ev_page_cache_get (pixbuf_cache->document);
956 /* We check each area to see what needs updating, and what needs freeing; */
957 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
958 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
966 if (((EvViewSelection *)list->data)->page == page) {
967 selection = list->data;
969 } else if (((EvViewSelection *)list->data)->page > page)
975 update_job_selection (pixbuf_cache->prev_job + i, selection);
977 clear_job_selection (pixbuf_cache->prev_job + i);
981 page = pixbuf_cache->start_page;
982 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
985 if (((EvViewSelection *)list->data)->page == page) {
986 selection = list->data;
988 } else if (((EvViewSelection *)list->data)->page > page)
994 update_job_selection (pixbuf_cache->job_list + i, selection);
996 clear_job_selection (pixbuf_cache->job_list + i);
1000 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1001 if (page >= ev_page_cache_get_n_pages (page_cache))
1006 if (((EvViewSelection *)list->data)->page == page) {
1007 selection = list->data;
1009 } else if (((EvViewSelection *)list->data)->page > page)
1015 update_job_selection (pixbuf_cache->next_job + i, selection);
1017 clear_job_selection (pixbuf_cache->next_job + i);
1023 /* Returns what the pixbuf cache thinks is */
1026 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1028 EvPageCache *page_cache;
1029 EvViewSelection *selection;
1030 GList *retval = NULL;
1034 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1036 page_cache = ev_page_cache_get (pixbuf_cache->document);
1038 /* We check each area to see what needs updating, and what needs freeing; */
1039 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1040 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1046 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1047 selection = g_new0 (EvViewSelection, 1);
1048 selection->page = page;
1049 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1050 if (pixbuf_cache->prev_job[i].selection_region)
1051 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1052 retval = g_list_append (retval, selection);
1058 page = pixbuf_cache->start_page;
1059 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1060 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1061 selection = g_new0 (EvViewSelection, 1);
1062 selection->page = page;
1063 selection->rect = pixbuf_cache->job_list[i].selection_points;
1064 if (pixbuf_cache->job_list[i].selection_region)
1065 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1066 retval = g_list_append (retval, selection);
1072 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1073 if (page >= ev_page_cache_get_n_pages (page_cache))
1076 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1077 selection = g_new0 (EvViewSelection, 1);
1078 selection->page = page;
1079 selection->rect = pixbuf_cache->next_job[i].selection_points;
1080 if (pixbuf_cache->next_job[i].selection_region)
1081 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1082 retval = g_list_append (retval, selection);