]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Fix build with poppler 0.5.9.
[evince.git] / shell / ev-pixbuf-cache.c
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"
6 #include "ev-image.h"
7
8 typedef struct _CacheJobInfo
9 {
10         EvJob *job;
11         EvRenderContext *rc;
12
13         /* Data we get from rendering */
14         GdkPixbuf *pixbuf;
15         GList *link_mapping;
16         GList *image_mapping;
17         GdkRegion *text_mapping;
18         
19         /* Selection data. 
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;
24         gboolean    points_set;
25         
26         GdkPixbuf *selection;
27         GdkRegion *selection_region;
28 } CacheJobInfo;
29
30 struct _EvPixbufCache
31 {
32         GObject parent;
33
34         /* We keep a link to our containing view just for style information. */
35         GtkWidget *view;
36         EvDocument *document;
37         int start_page;
38         int end_page;
39
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
42          * case of twin pages.
43          */
44         int preload_cache_size;
45         CacheJobInfo *prev_job;
46         CacheJobInfo *job_list;
47         CacheJobInfo *next_job;
48 };
49
50 struct _EvPixbufCacheClass
51 {
52         GObjectClass parent_class;
53
54         void (* job_finished) (EvPixbufCache *pixbuf_cache);
55 };
56
57
58 enum
59 {
60         JOB_FINISHED,
61         N_SIGNALS,
62 };
63
64 static guint signals[N_SIGNALS] = {0, };
65
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,
73                                                  int                 page);
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,
79                                                  gint                page,
80                                                  gfloat              scale);
81
82
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)
90
91 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
92
93 static void
94 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
95 {
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));
99
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);
103 }
104
105 static void
106 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
107 {
108         GObjectClass *object_class;
109
110         object_class = G_OBJECT_CLASS (class);
111
112         object_class->finalize = ev_pixbuf_cache_finalize;
113         object_class->dispose = ev_pixbuf_cache_dispose;
114
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),
119                                             NULL, NULL,
120                                             g_cclosure_marshal_VOID__VOID,
121                                             G_TYPE_NONE, 0);
122 }
123
124 static void
125 ev_pixbuf_cache_finalize (GObject *object)
126 {
127         EvPixbufCache *pixbuf_cache;
128
129         pixbuf_cache = EV_PIXBUF_CACHE (object);
130
131         g_free (pixbuf_cache->prev_job);
132         g_free (pixbuf_cache->job_list);
133         g_free (pixbuf_cache->next_job);
134 }
135
136 static void
137 dispose_cache_job_info (CacheJobInfo *job_info,
138                         gpointer      data)
139 {
140         if (job_info == NULL)
141                 return;
142         if (job_info->job) {
143                 g_signal_handlers_disconnect_by_func (job_info->job,
144                                                       G_CALLBACK (job_finished_cb),
145                                                       data);
146                 ev_job_queue_remove_job (job_info->job);
147                 g_object_unref (G_OBJECT (job_info->job));
148                 job_info->job = NULL;
149         }
150         if (job_info->pixbuf) {
151                 g_object_unref (G_OBJECT (job_info->pixbuf));
152                 job_info->pixbuf = NULL;
153         }
154         if (job_info->link_mapping) {
155                 ev_link_mapping_free (job_info->link_mapping);
156                 job_info->link_mapping = NULL;
157         }
158         if (job_info->image_mapping) {
159                 ev_image_mapping_free (job_info->image_mapping);
160                 job_info->image_mapping = NULL;
161         }
162         if (job_info->text_mapping) {
163                 gdk_region_destroy (job_info->text_mapping);
164                 job_info->text_mapping = NULL;
165         }
166         if (job_info->selection) {
167                 g_object_unref (G_OBJECT (job_info->selection));
168                 job_info->selection = NULL;
169         }
170         if (job_info->selection_region) {
171                 gdk_region_destroy (job_info->selection_region);
172                 job_info->selection_region = NULL;
173         }
174         if (job_info->rc) {
175                 g_object_unref (G_OBJECT (job_info->rc));
176                 job_info->rc = NULL;
177         }
178
179         job_info->points_set = FALSE;
180 }
181
182 static void
183 ev_pixbuf_cache_dispose (GObject *object)
184 {
185         EvPixbufCache *pixbuf_cache;
186         int i;
187
188         pixbuf_cache = EV_PIXBUF_CACHE (object);
189
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);
193         }
194
195         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
196                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
197         }
198 }
199
200
201 EvPixbufCache *
202 ev_pixbuf_cache_new (GtkWidget  *view,
203                      EvDocument *document)
204 {
205         EvPixbufCache *pixbuf_cache;
206
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;
211
212         return pixbuf_cache;
213 }
214
215 static void
216 job_finished_cb (EvJob         *job,
217                  EvPixbufCache *pixbuf_cache)
218 {
219         CacheJobInfo *job_info;
220         EvJobRender *job_render = EV_JOB_RENDER (job);
221
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);
226                 return;
227         }
228         
229         job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
230
231         copy_job_to_job_info (job_render, job_info, pixbuf_cache);
232         g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
233 }
234
235 /* This checks a job to see if the job would generate the right sized pixbuf
236  * given a scale.  If it won't, it removes the job and clears it to NULL.
237  */
238 static void
239 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
240                           CacheJobInfo *job_info,
241                           EvPageCache  *page_cache,
242                           gfloat        scale)
243 {
244         gint width;
245         gint height;
246
247         g_assert (job_info);
248
249         if (job_info->job == NULL)
250                 return;
251
252         ev_page_cache_get_size (page_cache,
253                                 EV_JOB_RENDER (job_info->job)->rc->page,
254                                 EV_JOB_RENDER (job_info->job)->rc->rotation,
255                                 scale,
256                                 &width, &height);
257                                 
258         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
259             height == EV_JOB_RENDER (job_info->job)->target_height)
260                 return;
261
262         g_signal_handlers_disconnect_by_func (job_info->job,
263                                               G_CALLBACK (job_finished_cb),
264                                               pixbuf_cache);
265         ev_job_queue_remove_job (job_info->job);
266         g_object_unref (job_info->job);
267         job_info->job = NULL;
268 }
269
270 /* Do all function that copies a job from an older cache to it's position in the
271  * new cache.  It clears the old job if it doesn't have a place.
272  */
273 static void
274 move_one_job (CacheJobInfo  *job_info,
275               EvPixbufCache *pixbuf_cache,
276               int            page,
277               CacheJobInfo  *new_job_list,
278               CacheJobInfo  *new_prev_job,
279               CacheJobInfo  *new_next_job,
280               int            start_page,
281               int            end_page,
282               EvJobPriority  priority)
283 {
284         CacheJobInfo *target_page = NULL;
285         int page_offset;
286         EvJobPriority new_priority;
287
288         if (page < (start_page - pixbuf_cache->preload_cache_size) ||
289             page > (end_page + pixbuf_cache->preload_cache_size)) {
290                 dispose_cache_job_info (job_info, pixbuf_cache);
291                 return;
292         }
293
294         /* find the target page to copy it over to. */
295         if (page < start_page) {
296                 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
297
298                 g_assert (page_offset >= 0 &&
299                           page_offset < pixbuf_cache->preload_cache_size);
300                 target_page = new_prev_job + page_offset;
301                 new_priority = EV_JOB_PRIORITY_LOW;
302         } else if (page > end_page) {
303                 page_offset = (page - (end_page + 1));
304
305                 g_assert (page_offset >= 0 &&
306                           page_offset < pixbuf_cache->preload_cache_size);
307                 target_page = new_next_job + page_offset;
308                 new_priority = EV_JOB_PRIORITY_LOW;
309         } else {
310                 page_offset = page - start_page;
311                 g_assert (page_offset >= 0 &&
312                           page_offset <= ((end_page - start_page) + 1));
313                 new_priority = EV_JOB_PRIORITY_HIGH;
314                 target_page = new_job_list + page_offset;
315         }
316
317         *target_page = *job_info;
318         job_info->job = NULL;
319         job_info->pixbuf = NULL;
320         job_info->link_mapping = NULL;
321         job_info->image_mapping = NULL;
322
323         if (new_priority != priority && target_page->job) {
324                 ev_job_queue_update_job (target_page->job, new_priority);
325         }
326 }
327
328
329
330 static void
331 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
332                               gint           start_page,
333                               gint           end_page)
334 {
335         CacheJobInfo *new_job_list;
336         CacheJobInfo *new_prev_job;
337         CacheJobInfo *new_next_job;
338         EvPageCache *page_cache;
339         int i, page;
340
341         if (pixbuf_cache->start_page == start_page &&
342             pixbuf_cache->end_page == end_page)
343                 return;
344
345         page_cache = ev_page_cache_get (pixbuf_cache->document);
346
347         new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
348         new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
349         new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
350
351         /* We go through each job in the old cache and either clear it or move
352          * it to a new location. */
353
354         /* Start with the prev cache. */
355         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
356         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
357                 if (page < 0) {
358                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
359                 } else {
360                         move_one_job (pixbuf_cache->prev_job + i,
361                                       pixbuf_cache, page,
362                                       new_job_list, new_prev_job, new_next_job,
363                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
364                 }
365                 page ++;
366         }
367
368         page = pixbuf_cache->start_page;
369         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
370                 move_one_job (pixbuf_cache->job_list + i,
371                               pixbuf_cache, page,
372                               new_job_list, new_prev_job, new_next_job,
373                               start_page, end_page, EV_JOB_PRIORITY_HIGH);
374                 page ++;
375         }
376
377         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
378                 if (page >= ev_page_cache_get_n_pages (page_cache)) {
379                         dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
380                 } else {
381                         move_one_job (pixbuf_cache->next_job + i,
382                                       pixbuf_cache, page,
383                                       new_job_list, new_prev_job, new_next_job,
384                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
385                 }
386                 page ++;
387         }
388
389         g_free (pixbuf_cache->job_list);
390         g_free (pixbuf_cache->prev_job);
391         g_free (pixbuf_cache->next_job);
392
393         pixbuf_cache->job_list = new_job_list;
394         pixbuf_cache->prev_job = new_prev_job;
395         pixbuf_cache->next_job = new_next_job;
396
397         pixbuf_cache->start_page = start_page;
398         pixbuf_cache->end_page = end_page;
399 }
400
401 static void
402 copy_job_to_job_info (EvJobRender   *job_render,
403                       CacheJobInfo  *job_info,
404                       EvPixbufCache *pixbuf_cache)
405 {
406
407         job_info->points_set = FALSE;
408
409         if (job_info->pixbuf) {
410                 g_object_unref (G_OBJECT (job_info->pixbuf));
411         }
412         job_info->pixbuf = g_object_ref (job_render->pixbuf);
413
414         if (job_info->rc) {
415                 g_object_unref (G_OBJECT (job_info->rc));
416         }
417         job_info->rc = g_object_ref (job_render->rc);
418
419         if (job_render->include_links) {
420                 if (job_info->link_mapping)
421                         ev_link_mapping_free (job_info->link_mapping);
422                 job_info->link_mapping = job_render->link_mapping;
423         }
424
425         if (job_render->include_images) {
426                 if (job_info->image_mapping)
427                         ev_image_mapping_free (job_info->image_mapping);
428                 job_info->image_mapping = job_render->image_mapping;
429         }
430
431         if (job_render->include_text) {
432                 if (job_info->text_mapping)
433                         gdk_region_destroy (job_info->text_mapping);
434                 job_info->text_mapping = job_render->text_mapping;
435         }
436
437         if (job_render->include_selection) {
438                 if (job_info->selection) {
439                         g_object_unref (G_OBJECT (job_info->selection));
440                         job_info->selection = NULL;
441                 }
442                 if (job_info->selection_region) {
443                         gdk_region_destroy (job_info->selection_region);
444                         job_info->selection_region = NULL;
445                 }
446                 
447                 job_info->selection_points = job_render->selection_points;
448                 job_info->selection_region = gdk_region_copy (job_render->selection_region);
449                 job_info->selection = g_object_ref (job_render->selection);
450                 g_assert (job_info->selection_points.x1 >= 0);
451                 job_info->points_set = TRUE;
452         }
453
454         if (job_info->job) {
455                 g_signal_handlers_disconnect_by_func (job_info->job,
456                                                       G_CALLBACK (job_finished_cb),
457                                                       pixbuf_cache);
458                 ev_job_queue_remove_job (job_info->job);
459                 g_object_unref (G_OBJECT (job_info->job));
460                 job_info->job = NULL;
461         }
462 }
463
464 static CacheJobInfo *
465 find_job_cache (EvPixbufCache *pixbuf_cache,
466                 int            page)
467 {
468         int page_offset;
469
470         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
471             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
472                 return NULL;
473
474         if (page < pixbuf_cache->start_page) {
475                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
476
477                 g_assert (page_offset >= 0 &&
478                           page_offset < pixbuf_cache->preload_cache_size);
479                 return pixbuf_cache->prev_job + page_offset;
480         }
481
482         if (page > pixbuf_cache->end_page) {
483                 page_offset = (page - (pixbuf_cache->end_page + 1));
484
485                 g_assert (page_offset >= 0 &&
486                           page_offset < pixbuf_cache->preload_cache_size);
487                 return pixbuf_cache->next_job + page_offset;
488         }
489
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;
494 }
495
496 static void
497 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
498                                  gfloat         scale)
499 {
500         EvPageCache *page_cache;
501         int i;
502
503         page_cache = ev_page_cache_get (pixbuf_cache->document);
504
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);
507         }
508
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);
512         }
513 }
514
515 #define FIRST_VISABLE_PREV(pixbuf_cache) \
516         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
517
518 static void
519 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
520 {
521     if (GTK_WIDGET_HAS_FOCUS (widget)) {
522         *text = &widget->style->text [GTK_STATE_SELECTED];
523         *base = &widget->style->base [GTK_STATE_SELECTED];
524     } else {
525         *text = &widget->style->text [GTK_STATE_ACTIVE];
526         *base = &widget->style->base [GTK_STATE_ACTIVE];
527     }
528 }
529
530 static void
531 add_job_if_needed (EvPixbufCache *pixbuf_cache,
532                    CacheJobInfo  *job_info,
533                    EvPageCache   *page_cache,
534                    gint           page,
535                    gint           rotation,
536                    gfloat         scale,
537                    EvJobPriority  priority)
538 {
539         gboolean include_links = FALSE;
540         gboolean include_text = FALSE;
541         gboolean include_selection = FALSE;
542         gboolean include_images = FALSE;
543         int width, height;
544         GdkColor *text, *base;
545
546         if (job_info->job)
547                 return;
548
549         ev_page_cache_get_size (page_cache, page, rotation,
550                                 scale, &width, &height);
551
552         if (job_info->pixbuf &&
553             gdk_pixbuf_get_width (job_info->pixbuf) == width &&
554             gdk_pixbuf_get_height (job_info->pixbuf) == height)
555                 return;
556
557         /* make a new job now */
558         if (job_info->rc == NULL) {
559                 job_info->rc = ev_render_context_new (rotation, page, scale);
560         } else {
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);
564         }
565
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)
572                 include_text = TRUE;
573         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
574                 include_selection = TRUE;
575         }
576
577         gtk_widget_ensure_style (pixbuf_cache->view);
578
579         get_selection_colors (pixbuf_cache->view, &text, &base);
580
581         job_info->job = ev_job_render_new (pixbuf_cache->document,
582                                            job_info->rc,
583                                            width, height,
584                                            &(job_info->target_points),
585                                            text, base,
586                                            include_links,
587                                            include_images,
588                                            include_text,
589                                            include_selection);
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);
592 }
593
594
595 static void
596 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
597                                     gint           rotation,
598                                     gfloat         scale)
599 {
600         EvPageCache *page_cache;
601         CacheJobInfo *job_info;
602         int page;
603         int i;
604
605         page_cache = ev_page_cache_get (pixbuf_cache->document);
606
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;
610
611                 add_job_if_needed (pixbuf_cache, job_info,
612                                    page_cache, page, rotation, scale,
613                                    EV_JOB_PRIORITY_HIGH);
614         }
615
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;
619
620                 add_job_if_needed (pixbuf_cache, job_info,
621                                    page_cache, page, rotation, scale,
622                                    EV_JOB_PRIORITY_LOW);
623         }
624
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;
628
629                 add_job_if_needed (pixbuf_cache, job_info,
630                                    page_cache, page, rotation, scale,
631                                    EV_JOB_PRIORITY_LOW);
632         }
633
634 }
635
636 void
637 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
638                                 gint            start_page,
639                                 gint            end_page,
640                                 gint            rotation,
641                                 gfloat          scale,
642                                 GList          *selection_list)
643 {
644         EvPageCache *page_cache;
645
646         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
647
648         page_cache = ev_page_cache_get (pixbuf_cache->document);
649
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);
653
654         /* First, resize the page_range as needed.  We cull old pages
655          * mercilessly. */
656         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
657
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);
661
662         /* Next, we update the target selection for our pages */
663         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
664
665         /* Finally, we add the new jobs for all the sizes that don't have a
666          * pixbuf */
667         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
668 }
669
670 GdkPixbuf *
671 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
672                             gint           page)
673 {
674         CacheJobInfo *job_info;
675
676         job_info = find_job_cache (pixbuf_cache, page);
677         if (job_info == NULL)
678                 return NULL;
679
680         /* We don't need to wait for the idle to handle the callback */
681         if (job_info->job &&
682             EV_JOB (job_info->job)->finished) {
683                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
684                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
685         }
686
687         return job_info->pixbuf;
688 }
689
690 GList *
691 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
692                                   gint           page)
693 {
694         CacheJobInfo *job_info;
695
696         job_info = find_job_cache (pixbuf_cache, page);
697         if (job_info == NULL)
698                 return NULL;
699
700         /* We don't need to wait for the idle to handle the callback */
701         if (job_info->job &&
702             EV_JOB (job_info->job)->finished) {
703                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
704                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
705         }
706
707         return job_info->link_mapping;
708 }
709
710 GList *
711 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
712                                    gint           page)
713 {
714         CacheJobInfo *job_info;
715
716         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
717                 return NULL;
718         
719         job_info = find_job_cache (pixbuf_cache, page);
720         if (job_info == NULL)
721                 return NULL;
722
723         /* We don't need to wait for the idle to handle the callback */
724         if (job_info->job &&
725             EV_JOB (job_info->job)->finished) {
726                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
727                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
728         }
729
730         return job_info->image_mapping;
731 }
732
733 static gboolean
734 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
735                              CacheJobInfo  *job_info,
736                              gint           page,
737                              gfloat         scale)
738 {
739         EvPageCache *page_cache;
740         gint width, height;
741
742         if (job_info->selection) {
743                 page_cache = ev_page_cache_get (pixbuf_cache->document);
744                 ev_page_cache_get_size (page_cache, page, job_info->rc->rotation,
745                                         scale, &width, &height);
746                 
747                 if (width != gdk_pixbuf_get_width (job_info->selection) ||
748                     height != gdk_pixbuf_get_height (job_info->selection))
749                         return TRUE;
750         } else {
751                 if (job_info->points_set)
752                         return TRUE;
753         }
754         return FALSE;
755 }
756
757 static void
758 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
759                            CacheJobInfo  *job_info,
760                            gint           page,
761                            gfloat         scale)
762 {
763         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
764                 if (job_info->selection)
765                         g_object_unref (job_info->selection);
766                 job_info->selection = NULL;
767                 job_info->selection_points.x1 = -1;
768         }
769 }
770
771 GdkRegion *
772 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
773                                   gint           page)
774 {
775         CacheJobInfo *job_info;
776
777         job_info = find_job_cache (pixbuf_cache, page);
778         if (job_info == NULL)
779                 return NULL;
780
781         /* We don't need to wait for the idle to handle the callback */
782         if (job_info->job &&
783             EV_JOB (job_info->job)->finished) {
784                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
785                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
786         }
787         
788         return job_info->text_mapping;
789 }
790
791 /* Clears the cache of jobs and pixbufs.
792  */
793 void
794 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
795 {
796         int i;
797
798         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
799                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
800                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
801         }
802
803         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
804                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
805         }
806 }
807
808
809 void
810 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
811 {
812         gint i;
813
814         /* FIXME: doesn't update running jobs. */
815         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
816                 CacheJobInfo *job_info;
817
818                 job_info = pixbuf_cache->prev_job + i;
819                 if (job_info->selection) {
820                         g_object_unref (G_OBJECT (job_info->selection));
821                         job_info->selection = NULL;
822                 }
823
824                 job_info = pixbuf_cache->next_job + i;
825                 if (job_info->selection) {
826                         g_object_unref (G_OBJECT (job_info->selection));
827                         job_info->selection = NULL;
828                 }
829         }
830
831         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
832                 CacheJobInfo *job_info;
833
834                 job_info = pixbuf_cache->job_list + i;
835                 if (job_info->selection) {
836                         g_object_unref (G_OBJECT (job_info->selection));
837                         job_info->selection = NULL;
838                 }
839         }
840 }
841
842 GdkPixbuf *
843 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache  *pixbuf_cache,
844                                       gint            page,
845                                       gfloat          scale,
846                                       GdkRegion     **region)
847 {
848         CacheJobInfo *job_info;
849
850         /* the document does not implement the selection interface */
851         if (!EV_IS_SELECTION (pixbuf_cache->document))
852                 return NULL;
853
854         job_info = find_job_cache (pixbuf_cache, page);
855         if (job_info == NULL)
856                 return NULL;
857
858         /* No selection on this page */
859         if (!job_info->points_set)
860                 return NULL;
861
862         /* Update the rc */
863         g_assert (job_info->rc);
864         ev_render_context_set_scale (job_info->rc, scale);
865
866         /* If we have a running job, we just return what we have under the
867          * assumption that it'll be updated later and we can scale it as need
868          * be */
869         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
870                 return job_info->selection;
871
872         /* Now, lets see if we need to resize the image.  If we do, we clear the
873          * old one. */
874         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
875
876         /* Finally, we see if the two scales are the same, and get a new pixbuf
877          * if needed.  We do this synchronously for now.  At some point, we
878          * _should_ be able to get rid of the doc_mutex, so the synchronicity
879          * doesn't kill us.  Rendering a few glyphs should really be fast.
880          */
881         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
882                 EvRectangle *old_points;
883                 GdkColor *text, *base;
884
885                 /* we need to get a new selection pixbuf */
886                 ev_document_doc_mutex_lock ();
887                 if (job_info->selection_points.x1 < 0) {
888                         g_assert (job_info->selection == NULL);
889                         old_points = NULL;
890                 } else {
891                         g_assert (job_info->selection != NULL);
892                         old_points = &(job_info->selection_points);
893                 }
894
895                 if (job_info->selection_region)
896                         gdk_region_destroy (job_info->selection_region);
897                 job_info->selection_region =
898                         ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
899                                                            job_info->rc,
900                                                            &(job_info->target_points));
901
902                 gtk_widget_ensure_style (pixbuf_cache->view);
903
904                 get_selection_colors (pixbuf_cache->view, &text, &base);
905
906                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
907                                                job_info->rc, &(job_info->selection),
908                                                &(job_info->target_points),
909                                                old_points,
910                                                text, base);
911                 job_info->selection_points = job_info->target_points;
912                 ev_document_doc_mutex_unlock ();
913         }
914         if (region)
915                 *region = job_info->selection_region;
916         return job_info->selection;
917 }
918
919 static void
920 update_job_selection (CacheJobInfo    *job_info,
921                       EvViewSelection *selection)
922 {
923         job_info->points_set = TRUE;            
924         job_info->target_points = selection->rect;
925 }
926
927 static void
928 clear_job_selection (CacheJobInfo *job_info)
929 {
930         job_info->points_set = FALSE;
931         job_info->selection_points.x1 = -1;
932
933         if (job_info->selection) {
934                 g_object_unref (job_info->selection);
935                 job_info->selection = NULL;
936         }
937 }
938
939 /* This function will reset the selection on pages that no longer have them, and
940  * will update the target_selection on those that need it.  It will _not_ free
941  * the previous selection_list -- that's up to caller to do.
942  */
943 void
944 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
945                                     GList         *selection_list)
946 {
947         EvPageCache *page_cache;
948         EvViewSelection *selection;
949         GList *list = selection_list;
950         int page;
951         int i;
952
953         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
954
955         if (!EV_IS_SELECTION (pixbuf_cache->document))
956                 return;
957
958         page_cache = ev_page_cache_get (pixbuf_cache->document);
959
960         /* We check each area to see what needs updating, and what needs freeing; */
961         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
962         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
963                 if (page < 0) {
964                         page ++;
965                         continue;
966                 }
967
968                 selection = NULL;
969                 while (list) {
970                         if (((EvViewSelection *)list->data)->page == page) {
971                                 selection = list->data;
972                                 break;
973                         } else if (((EvViewSelection *)list->data)->page > page) 
974                                 break;
975                         list = list->next;
976                 }
977
978                 if (selection)
979                         update_job_selection (pixbuf_cache->prev_job + i, selection);
980                 else
981                         clear_job_selection (pixbuf_cache->prev_job + i);
982                 page ++;
983         }
984
985         page = pixbuf_cache->start_page;
986         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
987                 selection = NULL;
988                 while (list) {
989                         if (((EvViewSelection *)list->data)->page == page) {
990                                 selection = list->data;
991                                 break;
992                         } else if (((EvViewSelection *)list->data)->page > page) 
993                                 break;
994                         list = list->next;
995                 }
996
997                 if (selection)
998                         update_job_selection (pixbuf_cache->job_list + i, selection);
999                 else
1000                         clear_job_selection (pixbuf_cache->job_list + i);
1001                 page ++;
1002         }
1003
1004         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1005                 if (page >= ev_page_cache_get_n_pages (page_cache))
1006                         break;
1007
1008                 selection = NULL;
1009                 while (list) {
1010                         if (((EvViewSelection *)list->data)->page == page) {
1011                                 selection = list->data;
1012                                 break;
1013                         } else if (((EvViewSelection *)list->data)->page > page) 
1014                                 break;
1015                         list = list->next;
1016                 }
1017
1018                 if (selection)
1019                         update_job_selection (pixbuf_cache->next_job + i, selection);
1020                 else
1021                         clear_job_selection (pixbuf_cache->next_job + i);
1022                 page ++;
1023         }
1024 }
1025
1026
1027 /* Returns what the pixbuf cache thinks is */
1028
1029 GList *
1030 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1031 {
1032         EvPageCache *page_cache;
1033         EvViewSelection *selection;
1034         GList *retval = NULL;
1035         int page;
1036         int i;
1037
1038         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1039
1040         page_cache = ev_page_cache_get (pixbuf_cache->document);
1041
1042         /* We check each area to see what needs updating, and what needs freeing; */
1043         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1044         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1045                 if (page < 0) {
1046                         page ++;
1047                         continue;
1048                 }
1049
1050                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1051                         selection = g_new0 (EvViewSelection, 1);
1052                         selection->page = page;
1053                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1054                         if (pixbuf_cache->prev_job[i].selection_region)
1055                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1056                         retval = g_list_append (retval, selection);
1057                 }
1058                 
1059                 page ++;
1060         }
1061
1062         page = pixbuf_cache->start_page;
1063         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1064                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1065                         selection = g_new0 (EvViewSelection, 1);
1066                         selection->page = page;
1067                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1068                         if (pixbuf_cache->job_list[i].selection_region)
1069                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1070                         retval = g_list_append (retval, selection);
1071                 }
1072                 
1073                 page ++;
1074         }
1075
1076         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1077                 if (page >= ev_page_cache_get_n_pages (page_cache))
1078                         break;
1079
1080                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1081                         selection = g_new0 (EvViewSelection, 1);
1082                         selection->page = page;
1083                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1084                         if (pixbuf_cache->next_job[i].selection_region)
1085                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1086                         retval = g_list_append (retval, selection);
1087                 }
1088                 
1089                 page ++;
1090         }
1091
1092         return retval;
1093 }
1094