~ubuntu-branches/ubuntu/oneiric/evince/oneiric-updates

« back to all changes in this revision

Viewing changes to backend/djvu/djvu-document.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette, Josselin Mouette, Marc 'HE' Brockschmidt
  • Date: 2008-12-31 16:41:58 UTC
  • mfrom: (1.1.36 upstream)
  • mto: (1.5.1 sid)
  • mto: This revision was merged to the branch mainline in revision 109.
  • Revision ID: james.westby@ubuntu.com-20081231164158-xnobl1sokvvc6ho8
Tags: 2.24.2-1
[ Josselin Mouette ]
* README.Debian: document that you need to install poppler-data.
  Closes: #506836.

[ Marc 'HE' Brockschmidt ]
* debian/control: Make the Gnome team maintainer. I'm not doing the job
   anyway.

[ Josselin Mouette ]
* New upstream release.
* Require nautilus 2.22 to build the extension for the correct 
  version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include <config.h>
23
23
#include "djvu-document.h"
24
 
#include "djvu-text.h"
 
24
#include "djvu-text-page.h"
25
25
#include "djvu-links.h"
26
26
#include "djvu-document-private.h"
27
27
#include "ev-document-thumbnails.h"
68
68
     });
69
69
 
70
70
 
71
 
void 
72
 
djvu_handle_events (DjvuDocument *djvu_document, int wait)
 
71
#define EV_DJVU_ERROR ev_djvu_error_quark ()
 
72
 
 
73
static GQuark
 
74
ev_djvu_error_quark (void)
 
75
{
 
76
        static GQuark q = 0;
 
77
        if (q == 0)
 
78
                q = g_quark_from_static_string ("ev-djvu-quark");
 
79
        
 
80
        return q;
 
81
}
 
82
 
 
83
static void
 
84
handle_message (const ddjvu_message_t *msg, GError **error)
 
85
{
 
86
        switch (msg->m_any.tag) {
 
87
                case DDJVU_ERROR: {
 
88
                        gchar *error_str;
 
89
                        
 
90
                        if (msg->m_error.filename) {
 
91
                                error_str = g_strdup_printf ("DjvuLibre error: %s:%d",
 
92
                                                             msg->m_error.filename,
 
93
                                                             msg->m_error.lineno);
 
94
                        } else {
 
95
                                error_str = g_strdup_printf ("DjvuLibre error: %s",
 
96
                                                             msg->m_error.message);
 
97
                        }
 
98
                        
 
99
                        if (error) {
 
100
                                g_set_error_literal (error, EV_DJVU_ERROR, 0, error_str);
 
101
                        } else {
 
102
                                g_warning ("%s", error_str);
 
103
                        }
 
104
                                
 
105
                        g_free (error_str);
 
106
                        return;
 
107
                        }                                                    
 
108
                        break;
 
109
                default:
 
110
                        break;
 
111
        }
 
112
}
 
113
 
 
114
void
 
115
djvu_handle_events (DjvuDocument *djvu_document, int wait, GError **error)
73
116
{
74
117
        ddjvu_context_t *ctx = djvu_document->d_context;
75
118
        const ddjvu_message_t *msg;
78
121
                return;
79
122
 
80
123
        if (wait)
81
 
                msg = ddjvu_message_wait (ctx);
 
124
                ddjvu_message_wait (ctx);
82
125
 
83
126
        while ((msg = ddjvu_message_peek (ctx))) {
84
 
                switch (msg->m_any.tag) {
85
 
                        case DDJVU_ERROR:
86
 
                                g_warning ("DjvuLibre error: %s", 
87
 
                                           msg->m_error.message);
88
 
                                if (msg->m_error.filename)
89
 
                                        g_warning ("DjvuLibre error: %s:%d", 
90
 
                                                   msg->m_error.filename,
91
 
                                                   msg->m_error.lineno);
92
 
                                break;
93
 
                        default:
94
 
                                break;
95
 
                }
96
 
                ddjvu_message_pop (ctx);
97
 
        }
 
127
                handle_message (msg, error);
 
128
                ddjvu_message_pop (ctx);
 
129
                if (error && *error)
 
130
                        return;
 
131
        }
 
132
}
 
133
 
 
134
static void
 
135
djvu_wait_for_message (DjvuDocument *djvu_document, ddjvu_message_tag_t message, GError **error)
 
136
{
 
137
        ddjvu_context_t *ctx = djvu_document->d_context;
 
138
        const ddjvu_message_t *msg;
 
139
 
 
140
        ddjvu_message_wait (ctx);
 
141
        while ((msg = ddjvu_message_peek (ctx)) && (msg->m_any.tag != message)) {
 
142
                handle_message (msg, error);
 
143
                ddjvu_message_pop (ctx);
 
144
                if (error && *error)
 
145
                        return;
 
146
        }
 
147
        if (msg && msg->m_any.tag == message)
 
148
                ddjvu_message_pop (ctx);
98
149
}
99
150
 
100
151
static gboolean
106
157
        ddjvu_document_t *doc;
107
158
        gchar *filename;
108
159
        gboolean missing_files = FALSE;
 
160
        GError *djvu_error = NULL;
109
161
 
110
162
        /* FIXME: We could actually load uris  */
111
163
        filename = g_filename_from_uri (uri, NULL, error);
124
176
 
125
177
        djvu_document->d_document = doc;
126
178
 
127
 
        while (!ddjvu_document_decoding_done (djvu_document->d_document)) 
128
 
                djvu_handle_events (djvu_document, TRUE);
 
179
        djvu_wait_for_message (djvu_document, DDJVU_DOCINFO, &djvu_error);
 
180
        if (djvu_error) {
 
181
                g_set_error_literal (error,
 
182
                                     EV_DOCUMENT_ERROR,
 
183
                                     EV_DOCUMENT_ERROR_INVALID,
 
184
                                     djvu_error->message);
 
185
                g_error_free (djvu_error);
 
186
                g_free (filename);
 
187
                ddjvu_document_release (djvu_document->d_document);
 
188
                djvu_document->d_document = NULL;
 
189
 
 
190
                return FALSE;
 
191
        }
 
192
 
 
193
        if (ddjvu_document_decoding_error (djvu_document->d_document))
 
194
                djvu_handle_events (djvu_document, TRUE, &djvu_error);
 
195
 
 
196
        if (djvu_error) {
 
197
                g_set_error_literal (error,
 
198
                                     EV_DOCUMENT_ERROR,
 
199
                                     EV_DOCUMENT_ERROR_INVALID,
 
200
                                     djvu_error->message);
 
201
                g_error_free (djvu_error);
 
202
                g_free (filename);
 
203
                ddjvu_document_release (djvu_document->d_document);
 
204
                djvu_document->d_document = NULL;
 
205
                
 
206
                return FALSE;
 
207
        }
 
208
        
129
209
        g_free (djvu_document->uri);
130
210
        djvu_document->uri = g_strdup (uri);
131
211
 
195
275
}
196
276
 
197
277
static void
198
 
djvu_document_get_page_size (EvDocument   *document,
199
 
                             int           page,
200
 
                             double       *width,
201
 
                             double       *height)
 
278
document_get_page_size (DjvuDocument *djvu_document,
 
279
                        gint          page,
 
280
                        double       *width,
 
281
                        double       *height)
202
282
{
203
 
        DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
204
 
        ddjvu_pageinfo_t info;
 
283
        ddjvu_pageinfo_t info;
205
284
        ddjvu_status_t r;
206
285
        
207
 
        g_return_if_fail (djvu_document->d_document);
208
 
 
209
286
        while ((r = ddjvu_document_get_pageinfo(djvu_document->d_document, page, &info)) < DDJVU_JOB_OK)
210
 
                djvu_handle_events(djvu_document, TRUE);
 
287
                djvu_handle_events(djvu_document, TRUE, NULL);
211
288
        
212
289
        if (r >= DDJVU_JOB_FAILED)
213
 
                djvu_handle_events(djvu_document, TRUE);
 
290
                djvu_handle_events(djvu_document, TRUE, NULL);
214
291
 
215
292
        *width = info.width * SCALE_FACTOR; 
216
293
        *height = info.height * SCALE_FACTOR;
217
294
}
218
295
 
 
296
static void
 
297
djvu_document_get_page_size (EvDocument   *document,
 
298
                             EvPage       *page,
 
299
                             double       *width,
 
300
                             double       *height)
 
301
{
 
302
        DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
 
303
 
 
304
        g_return_if_fail (djvu_document->d_document);
 
305
 
 
306
        document_get_page_size (djvu_document, page->index,
 
307
                                width, height);
 
308
}
 
309
 
219
310
static cairo_surface_t *
220
311
djvu_document_render (EvDocument      *document, 
221
312
                      EvRenderContext *rc)
231
322
        double page_width, page_height, tmp;
232
323
        static const cairo_user_data_key_t key;
233
324
 
234
 
        d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page);
 
325
        d_page = ddjvu_page_create_by_pageno (djvu_document->d_document, rc->page->index);
235
326
        
236
327
        while (!ddjvu_page_decoding_done (d_page))
237
 
                djvu_handle_events(djvu_document, TRUE);
 
328
                djvu_handle_events(djvu_document, TRUE, NULL);
238
329
 
239
330
        page_width = ddjvu_page_get_width (d_page) * rc->scale * SCALE_FACTOR + 0.5;
240
331
        page_height = ddjvu_page_get_height (d_page) * rc->scale * SCALE_FACTOR + 0.5;
297
388
{
298
389
        DjvuDocument *djvu_document = DJVU_DOCUMENT (object);
299
390
 
300
 
        if (djvu_document->search)
301
 
            djvu_text_free (djvu_document->search);
302
 
 
303
391
        if (djvu_document->d_document)
304
392
            ddjvu_document_release (djvu_document->d_document);
305
393
            
347
435
}
348
436
 
349
437
static gchar *
 
438
djvu_text_copy (DjvuDocument *djvu_document,
 
439
                gint           page,
 
440
                EvRectangle  *rectangle)
 
441
{
 
442
        miniexp_t page_text;
 
443
        gchar    *text = NULL;
 
444
 
 
445
        while ((page_text =
 
446
                ddjvu_document_get_pagetext (djvu_document->d_document,
 
447
                                             page, "char")) == miniexp_dummy)
 
448
                djvu_handle_events (djvu_document, TRUE, NULL);
 
449
 
 
450
        if (page_text != miniexp_nil) {
 
451
                DjvuTextPage *page = djvu_text_page_new (page_text);
 
452
                
 
453
                text = djvu_text_page_copy (page, rectangle);
 
454
                djvu_text_page_free (page);
 
455
                ddjvu_miniexp_release (djvu_document->d_document, page_text);
 
456
        }
 
457
 
 
458
        return text;
 
459
}
 
460
 
 
461
static gchar *
350
462
djvu_selection_get_selected_text (EvSelection     *selection,
351
463
                                  EvRenderContext *rc,
352
464
                                  EvSelectionStyle style,
364
476
        rectangle.x2 = points->x2 / SCALE_FACTOR;
365
477
        rectangle.y2 = (height - points->y1) / SCALE_FACTOR;
366
478
                
367
 
        text = djvu_text_copy (djvu_document, rc->page, &rectangle);
 
479
        text = djvu_text_copy (djvu_document, rc->page->index, &rectangle);
368
480
      
369
481
        if (text == NULL)
370
482
                text = g_strdup ("");
423
535
        gdk_pixbuf_fill (pixbuf, 0xffffffff);
424
536
        pixels = gdk_pixbuf_get_pixels (pixbuf);
425
537
        
426
 
        while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page, 1) < DDJVU_JOB_OK)
427
 
                djvu_handle_events(djvu_document, TRUE);
 
538
        while (ddjvu_thumbnail_status (djvu_document->d_document, rc->page->index, 1) < DDJVU_JOB_OK)
 
539
                djvu_handle_events(djvu_document, TRUE, NULL);
428
540
                    
429
 
        ddjvu_thumbnail_render (djvu_document->d_document, rc->page, 
 
541
        ddjvu_thumbnail_render (djvu_document->d_document, rc->page->index, 
430
542
                                &thumb_width, &thumb_height,
431
543
                                djvu_document->thumbs_format,
432
544
                                gdk_pixbuf_get_rowstride (pixbuf), 
472
584
{
473
585
        DjvuDocument *djvu_document = DJVU_DOCUMENT (exporter);
474
586
        
475
 
        g_string_append_printf (djvu_document->opts, "%d,", (rc->page) + 1); 
 
587
        g_string_append_printf (djvu_document->opts, "%d,", (rc->page->index) + 1); 
476
588
}
477
589
 
478
590
static void
493
605
 
494
606
        ddjvu_job_t * job = ddjvu_document_print(djvu_document->d_document, fn, d_optc, d_optv);
495
607
        while (!ddjvu_job_done(job)) {  
496
 
                djvu_handle_events (djvu_document, TRUE);
 
608
                djvu_handle_events (djvu_document, TRUE, NULL);
497
609
        }
498
610
 
499
611
        fclose(fn); 
536
648
        djvu_document->d_document = NULL;
537
649
}
538
650
 
539
 
static void
540
 
djvu_document_find_begin (EvDocumentFind   *document,
541
 
                          int               page,
542
 
                          const char       *search_string,
543
 
                          gboolean          case_sensitive)
 
651
static GList *
 
652
djvu_document_find_find_text (EvDocumentFind   *document,
 
653
                              EvPage           *page,
 
654
                              const char       *text,
 
655
                              gboolean          case_sensitive)
544
656
{
545
657
        DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
546
 
 
547
 
        if (djvu_document->search && 
548
 
            strcmp (search_string, djvu_text_get_text (djvu_document->search)) == 0)
549
 
                return;
550
 
 
551
 
        if (djvu_document->search)
552
 
                djvu_text_free (djvu_document->search);
553
 
 
554
 
        djvu_document->search = djvu_text_new (djvu_document,
555
 
                                                          page,
556
 
                                                          case_sensitive,
557
 
                                                          search_string);
558
 
}
559
 
 
560
 
static int
561
 
djvu_document_find_get_n_results (EvDocumentFind *document_find, int page)
562
 
{
563
 
        DjvuText *search = DJVU_DOCUMENT (document_find)->search;
564
 
 
565
 
        if (search) {
566
 
                return djvu_text_n_results (search, page);
567
 
        } else {
568
 
                return 0;
569
 
        }
570
 
}
571
 
 
572
 
static gboolean
573
 
djvu_document_find_get_result (EvDocumentFind *document_find,
574
 
                               int             page,
575
 
                               int             n_result,
576
 
                               EvRectangle    *rectangle)
577
 
{
578
 
        DjvuDocument *djvu_document = DJVU_DOCUMENT (document_find);
579
 
        DjvuText *search = djvu_document->search;
580
 
        EvRectangle *r;
581
 
        double width, height;
582
 
 
583
 
        if (search == NULL)
584
 
                return FALSE;
585
 
 
586
 
        r = djvu_text_get_result (search, page, n_result);
587
 
        if (r == NULL)
588
 
                return FALSE;
589
 
 
590
 
        djvu_document_get_page_size (EV_DOCUMENT (djvu_document), 
591
 
                page, &width, &height);
592
 
        rectangle->x1 = r->x1 * SCALE_FACTOR;
593
 
        rectangle->y1 = height - r->y2 * SCALE_FACTOR;
594
 
        rectangle->x2 = r->x2 * SCALE_FACTOR;
595
 
        rectangle->y2 = height - r->y1 * SCALE_FACTOR;
596
 
                
597
 
        return TRUE;
598
 
}
599
 
 
600
 
static int
601
 
djvu_document_find_page_has_results (EvDocumentFind *document_find,
602
 
                                    int             page)
603
 
{
604
 
        DjvuText *search = DJVU_DOCUMENT (document_find)->search;
605
 
 
606
 
        return search && djvu_text_has_results (search, page);
607
 
}
608
 
 
609
 
static double
610
 
djvu_document_find_get_progress (EvDocumentFind *document_find)
611
 
{
612
 
        DjvuText *search = DJVU_DOCUMENT (document_find)->search;
 
658
        miniexp_t page_text;
 
659
        gdouble width, height;
 
660
        GList *matches = NULL, *l;
 
661
 
 
662
        g_return_val_if_fail (text != NULL, NULL);
 
663
 
 
664
        while ((page_text = ddjvu_document_get_pagetext (djvu_document->d_document,
 
665
                                                         page->index,
 
666
                                                         "char")) == miniexp_dummy)
 
667
                djvu_handle_events (djvu_document, TRUE, NULL);
 
668
 
 
669
        if (page_text != miniexp_nil) {
 
670
                DjvuTextPage *tpage = djvu_text_page_new (page_text);
 
671
                
 
672
                djvu_text_page_prepare_search (tpage, case_sensitive);
 
673
                if (tpage->links->len > 0) {
 
674
                        djvu_text_page_search (tpage, text);
 
675
                        matches = tpage->results;
 
676
                }
 
677
                djvu_text_page_free (tpage);
 
678
                ddjvu_miniexp_release (djvu_document->d_document, page_text);
 
679
        }
 
680
 
 
681
        if (!matches)
 
682
                return NULL;
 
683
 
 
684
        document_get_page_size (djvu_document, page->index, &width, &height);
 
685
        for (l = matches; l && l->data; l = g_list_next (l)) {
 
686
                EvRectangle *r = (EvRectangle *)l->data;
 
687
                gdouble      tmp;
 
688
 
 
689
                tmp = r->y1;
 
690
                
 
691
                r->x1 *= SCALE_FACTOR;
 
692
                r->x2 *= SCALE_FACTOR;
 
693
 
 
694
                tmp = r->y1;
 
695
                r->y1 = height - r->y2 * SCALE_FACTOR;
 
696
                r->y2 = height - tmp * SCALE_FACTOR;
 
697
        }
613
698
        
614
 
        if (search == NULL) {
615
 
                return 0;
616
 
        }
617
 
 
618
 
        return djvu_text_get_progress (search);
619
 
}
620
 
 
621
 
static void
622
 
djvu_document_find_cancel (EvDocumentFind *document)
623
 
{
624
 
        DjvuDocument *djvu_document = DJVU_DOCUMENT (document);
625
 
 
626
 
        if (djvu_document->search) {
627
 
                djvu_text_free (djvu_document->search);
628
 
                djvu_document->search = NULL;
629
 
        }
 
699
 
 
700
        return matches;
630
701
}
631
702
 
632
703
static void
633
704
djvu_document_find_iface_init (EvDocumentFindIface *iface)
634
705
{
635
 
        iface->begin = djvu_document_find_begin;
636
 
        iface->get_n_results = djvu_document_find_get_n_results;
637
 
        iface->get_result = djvu_document_find_get_result;
638
 
        iface->page_has_results = djvu_document_find_page_has_results;
639
 
        iface->get_progress = djvu_document_find_get_progress;
640
 
        iface->cancel = djvu_document_find_cancel;
 
706
        iface->find_text = djvu_document_find_find_text;
641
707
}
642
708
 
643
709
static GList *