]> www.fi.muni.cz Git - evince.git/blob - backend/ps/ps-document.c
086f082125762db12612b7c11f1b694b4ae97700
[evince.git] / backend / ps / ps-document.c
1 /* Ghostscript widget for GTK/GNOME
2  *
3  * Copyright (C) 1998 - 2005 the Free Software Foundation
4  *
5  * Authors: Jonathan Blandford, Jaka Mocnik, Carlos Garcia Campos
6  *
7  * Based on code by: Federico Mena (Quartic), Szekeres Istvan (Pista)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "config.h"
26
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
29 #include <gdk/gdkpixbuf.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include "ps-document.h"
34 #include "ps-interpreter.h"
35 #include "ps.h"
36 #include "gstypes.h"
37 #include "gsdefaults.h"
38 #include "ev-file-exporter.h"
39 #include "ev-async-renderer.h"
40 #include "ev-document-thumbnails.h"
41 #include "ev-document-misc.h"
42
43 struct _PSDocument {
44         GObject object;
45
46         gchar *filename;
47         struct document *doc;
48         gboolean structured_doc;
49
50         PSInterpreter *gs;
51
52         /* Document Thumbnails */
53         PSInterpreter   *thumbs_gs;
54         GdkPixbuf       *thumbnail;
55         EvRenderContext *thumbs_rc;
56         GMutex          *thumbs_mutex;
57         GCond           *thumbs_cond;
58         
59         /* File exporter */
60         gint  *ps_export_pagelist;
61         gchar *ps_export_filename;
62 };
63
64 struct _PSDocumentClass {
65         GObjectClass parent_class;
66 }; 
67
68 static void     ps_document_document_iface_init            (EvDocumentIface           *iface);
69 static void     ps_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
70 static void     ps_async_renderer_iface_init               (EvAsyncRendererIface      *iface);
71 static void     ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
72
73 static void     ps_interpreter_page_rendered               (PSInterpreter             *gs,
74                                                             GdkPixbuf                 *pixbuf,
75                                                             PSDocument                *ps_document);
76
77 G_DEFINE_TYPE_WITH_CODE (PSDocument, ps_document, G_TYPE_OBJECT,
78                          {
79                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
80                                                         ps_document_document_iface_init);
81                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
82                                                         ps_document_document_thumbnails_iface_init);
83                                  G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
84                                                         ps_document_file_exporter_iface_init);
85                                  G_IMPLEMENT_INTERFACE (EV_TYPE_ASYNC_RENDERER,
86                                                         ps_async_renderer_iface_init);
87                          });
88
89 /* PSDocument */
90 static void
91 ps_document_init (PSDocument *ps_document)
92 {
93 }
94
95 static void
96 ps_document_dispose (GObject *object)
97 {
98         PSDocument *ps_document = PS_DOCUMENT (object);
99
100         if (ps_document->gs) {
101                 g_object_unref (ps_document->gs);
102                 ps_document->gs = NULL;
103         }
104
105         if (ps_document->thumbs_gs) {
106                 g_object_unref (ps_document->thumbs_gs);
107                 ps_document->thumbs_gs = NULL;
108         }
109         
110         if (ps_document->filename) {
111                 g_free (ps_document->filename);
112                 ps_document->filename = NULL;
113         }
114
115         if (ps_document->doc) {
116                 psfree (ps_document->doc);
117                 ps_document->doc = NULL;
118         }
119
120         if (ps_document->thumbnail) {
121                 g_object_unref (ps_document->thumbnail);
122                 ps_document->thumbnail = NULL;
123         }
124
125         if (ps_document->thumbs_mutex) {
126                 g_mutex_free (ps_document->thumbs_mutex);
127                 ps_document->thumbs_mutex = NULL;
128         }
129         
130         if (ps_document->thumbs_cond) {
131                 g_cond_free (ps_document->thumbs_cond);
132                 ps_document->thumbs_cond = NULL;
133         }
134
135         if (ps_document->thumbs_rc) {
136                 g_object_unref (ps_document->thumbs_rc);
137                 ps_document->thumbs_rc = NULL;
138         }
139
140         G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
141 }
142
143 static void
144 ps_document_class_init (PSDocumentClass *klass)
145 {
146         GObjectClass *object_class;
147
148         object_class = G_OBJECT_CLASS (klass);
149
150         object_class->dispose = ps_document_dispose;
151 }
152
153 /* EvDocumentIface */
154 static gboolean
155 document_load (PSDocument *ps_document, const gchar *fname, GError **error)
156 {
157         FILE *fd;
158         
159         ps_document->filename = g_strdup (fname);
160
161         /*
162          * We need to make sure that the file is loadable/exists!
163          * otherwise we want to exit without loading new stuff...
164          */
165         if (!g_file_test (fname, G_FILE_TEST_IS_REGULAR)) {
166                 gchar *filename_dsp;
167
168                 filename_dsp = g_filename_display_name (fname);
169                 g_set_error (error,
170                              G_FILE_ERROR,
171                              G_FILE_ERROR_NOENT,
172                              _("Cannot open file ā€œ%sā€.\n"), /* FIXME: remove \n after freeze */
173                              filename_dsp);
174                 g_free (filename_dsp);
175                 
176                 return FALSE;
177         }
178
179         if ((fd = fopen (ps_document->filename, "r")) == NULL) {
180                 gchar *filename_dsp;
181
182                 filename_dsp = g_filename_display_name (fname);
183                 g_set_error (error,
184                              G_FILE_ERROR,
185                              G_FILE_ERROR_NOENT,
186                              _("Cannot open file ā€œ%sā€.\n"), /* FIXME: remove \n after freeze */
187                              filename_dsp);
188                 g_free (filename_dsp);
189                 
190                 return FALSE;
191         }
192         
193         /* we grab the vital statistics!!! */
194         ps_document->doc = psscan (fd, TRUE, ps_document->filename);
195         fclose (fd);
196         if (!ps_document->doc)
197                 return FALSE;
198
199         ps_document->structured_doc =
200                 ((!ps_document->doc->epsf && ps_document->doc->numpages > 0) ||
201                  (ps_document->doc->epsf && ps_document->doc->numpages > 1));
202
203         ps_document->gs = ps_interpreter_new (ps_document->filename,
204                                               ps_document->doc);
205         g_signal_connect (G_OBJECT (ps_document->gs), "page_rendered",
206                           G_CALLBACK (ps_interpreter_page_rendered),
207                           (gpointer) ps_document);
208         
209         return TRUE;
210 }
211
212 static gboolean
213 ps_document_load (EvDocument  *document,
214                   const char  *uri,
215                   GError     **error)
216 {
217         char *filename;
218         char *gs_path;
219         gboolean result;
220
221         filename = g_filename_from_uri (uri, NULL, error);
222         if (!filename)
223                 return FALSE;
224
225         gs_path = g_find_program_in_path ("gs");
226         if (!gs_path) {
227                 gchar *filename_dsp;
228                 
229                 filename_dsp = g_filename_display_name (filename);
230                 g_set_error (error,
231                              G_FILE_ERROR,
232                              G_FILE_ERROR_NOENT,
233                              _("Failed to load document ā€œ%sā€. Ghostscript interpreter was not found in path"),
234                              filename);
235                 g_free (filename_dsp);
236                 g_free (filename);
237                 
238                 return FALSE;
239         }
240         g_free (gs_path);
241
242         result = document_load (PS_DOCUMENT (document), filename, error);
243         if (!result && !(*error)) {
244                 gchar *filename_dsp;
245                 
246                 filename_dsp = g_filename_display_name (filename);
247                 g_set_error (error,
248                              G_FILE_ERROR,
249                              G_FILE_ERROR_FAILED,
250                              _("Failed to load document ā€œ%sā€"),
251                              filename_dsp);
252                 g_free (filename_dsp);
253         }
254         
255         g_free (filename);
256
257         return result;
258 }
259
260 static gboolean
261 save_document (PSDocument *document, const char *filename)
262 {
263         gboolean result = TRUE;
264         GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
265         FILE *f, *src_file;
266         gchar *buf;
267
268         src_file = fopen (document->filename, "r");
269         if (src_file) {
270                 struct stat stat_rec;
271
272                 if (stat (document->filename, &stat_rec) == 0) {
273                         pscopy (src_file, sink, 0, stat_rec.st_size - 1);
274                 }
275
276                 fclose (src_file);
277         }
278         
279         buf = gtk_gs_doc_sink_get_buffer (sink);
280         if (buf == NULL) {
281                 return FALSE;
282         }
283         
284         f = fopen (filename, "w");
285         if (f) {
286                 fputs (buf, f);
287                 fclose (f);
288         } else {
289                 result = FALSE;
290         }
291
292         g_free (buf);
293         gtk_gs_doc_sink_free (sink);
294         g_free (sink);
295
296         return result;
297 }
298
299 static gboolean
300 save_page_list (PSDocument *document, int *page_list, const char *filename)
301 {
302         gboolean result = TRUE;
303         GtkGSDocSink *sink = gtk_gs_doc_sink_new ();
304         FILE *f;
305         gchar *buf;
306
307         pscopydoc (sink, document->filename, 
308                    document->doc, page_list);
309         
310         buf = gtk_gs_doc_sink_get_buffer (sink);
311         
312         f = fopen (filename, "w");
313         if (f) {
314                 fputs (buf, f);
315                 fclose (f);
316         } else {
317                 result = FALSE;
318         }
319
320         g_free (buf);
321         gtk_gs_doc_sink_free (sink);
322         g_free (sink);
323
324         return result;
325 }
326
327 static gboolean
328 ps_document_save (EvDocument  *document,
329                   const char  *uri,
330                   GError     **error)
331 {
332         PSDocument *ps = PS_DOCUMENT (document);
333         gboolean result;
334         char *filename;
335
336         filename = g_filename_from_uri (uri, NULL, error);
337         if (!filename)
338                 return FALSE;
339
340         result = save_document (ps, filename);
341
342         g_free (filename);
343
344         return result;
345 }
346
347 static int
348 ps_document_get_n_pages (EvDocument *document)
349 {
350         PSDocument *ps = PS_DOCUMENT (document);
351
352         if (!ps->filename || !ps->doc) {
353                 return -1;
354         }
355
356         return ps->structured_doc ? ps->doc->numpages : 1;
357 }
358
359
360 static void
361 ps_document_get_page_size (EvDocument *document,
362                            int         page,
363                            double     *width,
364                            double     *height)
365 {
366         PSDocument *ps_document = PS_DOCUMENT (document);
367         int urx, ury, llx, lly;
368
369         psgetpagebox (ps_document->doc, page, &urx, &ury, &llx, &lly);
370
371         if (width) {
372                 *width = (urx - llx) + 0.5;
373         }
374
375         if (height) {
376                 *height = (ury - lly) + 0.5;
377         }
378 }
379
380 static gboolean
381 ps_document_can_get_text (EvDocument *document)
382 {
383         return FALSE;
384 }
385
386 static EvDocumentInfo *
387 ps_document_get_info (EvDocument *document)
388 {
389         EvDocumentInfo *info;
390         PSDocument *ps = PS_DOCUMENT (document);
391         int urx, ury, llx, lly;
392
393         info = g_new0 (EvDocumentInfo, 1);
394         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
395                             EV_DOCUMENT_INFO_FORMAT |
396                             EV_DOCUMENT_INFO_CREATOR |
397                             EV_DOCUMENT_INFO_N_PAGES |
398                             EV_DOCUMENT_INFO_PAPER_SIZE;
399
400         info->title = g_strdup (ps->doc->title);
401         info->format = ps->doc->epsf ? g_strdup (_("Encapsulated PostScript"))
402                                      : g_strdup (_("PostScript"));
403         info->creator = g_strdup (ps->doc->creator);
404         info->n_pages = ev_document_get_n_pages (document);
405         
406         psgetpagebox (PS_DOCUMENT (document)->doc, 0, &urx, &ury, &llx, &lly);
407
408         info->paper_width  = (urx - llx) / 72.0f * 25.4f;
409         info->paper_height = (ury - lly) / 72.0f * 25.4f;
410
411         return info;
412 }
413
414 static void
415 ps_document_document_iface_init (EvDocumentIface *iface)
416 {
417         iface->load = ps_document_load;
418         iface->save = ps_document_save;
419         iface->can_get_text = ps_document_can_get_text;
420         iface->get_n_pages = ps_document_get_n_pages;
421         iface->get_page_size = ps_document_get_page_size;
422         iface->get_info = ps_document_get_info;
423 }
424
425 /* EvAsyncRendererIface */
426 static void
427 ps_interpreter_page_rendered (PSInterpreter *gs,
428                               GdkPixbuf     *pixbuf,
429                               PSDocument    *ps_document)
430 {
431         g_signal_emit_by_name (ps_document, "render_finished", pixbuf);
432 }
433
434 static void
435 ps_async_renderer_render_pixbuf (EvAsyncRenderer *renderer,
436                                  gint             page,
437                                  gdouble          scale,
438                                  gint             rotation)
439 {
440         PSDocument *ps_document = PS_DOCUMENT (renderer);
441
442         g_return_if_fail (PS_IS_INTERPRETER (ps_document->gs));
443
444         ps_interpreter_render_page (ps_document->gs, page, scale, rotation);
445 }
446
447 static void
448 ps_async_renderer_iface_init (EvAsyncRendererIface *iface)
449 {
450         iface->render_pixbuf = ps_async_renderer_render_pixbuf;
451 }
452
453 /* EvDocumentThumbnailsIface */
454 static void
455 ps_interpreter_thumbnail_rendered (PSInterpreter *gs,
456                                    GdkPixbuf     *pixbuf,
457                                    PSDocument    *ps_document)
458 {
459         if (ps_document->thumbnail)
460                 g_object_unref (ps_document->thumbnail);
461         ps_document->thumbnail = g_object_ref (pixbuf);
462
463         g_cond_broadcast (ps_document->thumbs_cond);
464 }
465
466 static gboolean
467 ps_document_render_thumbnail (PSDocument *ps_document)
468 {
469         ps_interpreter_render_page (ps_document->thumbs_gs,
470                                     ps_document->thumbs_rc->page,
471                                     ps_document->thumbs_rc->scale,
472                                     ps_document->thumbs_rc->rotation);
473
474         return FALSE;
475 }
476
477 static GdkPixbuf *
478 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
479                                       gint                  page,
480                                       gint                  rotation,
481                                       gint                  size,
482                                       gboolean              border)
483 {
484         PSDocument *ps_document;
485         GdkPixbuf  *pixbuf = NULL;
486         gdouble     page_width, page_height;
487         gdouble     scale;
488
489         ps_document = PS_DOCUMENT (document_thumbnails);
490         
491         g_return_val_if_fail (ps_document->filename != NULL, NULL);
492         g_return_val_if_fail (ps_document->doc != NULL, NULL);
493                 
494         if (!ps_document->thumbs_gs) {
495                 ps_document->thumbs_gs = ps_interpreter_new (ps_document->filename,
496                                                              ps_document->doc);
497                 g_signal_connect (G_OBJECT (ps_document->thumbs_gs), "page_rendered",
498                                   G_CALLBACK (ps_interpreter_thumbnail_rendered),
499                                   (gpointer) ps_document);
500         }
501
502         if (!ps_document->thumbs_mutex)
503                 ps_document->thumbs_mutex = g_mutex_new ();
504         ps_document->thumbs_cond = g_cond_new ();
505
506         ps_document_get_page_size (EV_DOCUMENT (ps_document), page,
507                                    &page_width, &page_height);
508         scale = size / page_width;
509         
510         if (!ps_document->thumbs_rc) {
511                 ps_document->thumbs_rc = ev_render_context_new (rotation, page, scale);
512         } else {
513                 ev_render_context_set_page (ps_document->thumbs_rc, page);
514                 ev_render_context_set_scale (ps_document->thumbs_rc, scale);
515                 ev_render_context_set_rotation (ps_document->thumbs_rc, rotation);
516         }
517
518         ev_document_doc_mutex_unlock ();
519         g_mutex_lock (ps_document->thumbs_mutex);
520         g_idle_add ((GSourceFunc)ps_document_render_thumbnail, ps_document);
521         g_cond_wait (ps_document->thumbs_cond, ps_document->thumbs_mutex);
522         g_cond_free (ps_document->thumbs_cond);
523         ps_document->thumbs_cond = NULL;
524         g_mutex_unlock (ps_document->thumbs_mutex);
525         ev_document_doc_mutex_lock ();
526         
527         pixbuf = ps_document->thumbnail;
528         ps_document->thumbnail = NULL;
529         
530         if (border) {
531                 GdkPixbuf *border_pixbuf;
532                 
533                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, rotation, pixbuf);
534                 g_object_unref (pixbuf);
535                 pixbuf = border_pixbuf;
536         }
537
538         return pixbuf;
539 }
540
541 static void
542 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
543                                        gint                  page,
544                                        gint                  size,
545                                        gint                 *width,
546                                        gint                 *height)
547 {
548         PSDocument *ps_document;
549         gdouble     page_width, page_height;
550
551         ps_document = PS_DOCUMENT (document_thumbnails);
552         
553         ps_document_get_page_size (EV_DOCUMENT (ps_document),
554                                    page,
555                                    &page_width, &page_height);
556         *width = size;
557         *height = (int) (size * page_height / page_width);
558 }
559
560 static void
561 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
562 {
563         iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
564         iface->get_dimensions = ps_document_thumbnails_get_dimensions;
565 }
566
567 /* EvFileExporterIface */
568 static gboolean
569 ps_document_file_exporter_format_supported (EvFileExporter      *exporter,
570                                             EvFileExporterFormat format)
571 {
572         return (format == EV_FILE_FORMAT_PS);
573 }
574
575 static void
576 ps_document_file_exporter_begin (EvFileExporter      *exporter,
577                                  EvFileExporterFormat format,
578                                  const char          *filename,
579                                  int                  first_page,
580                                  int                  last_page,
581                                  double               width,
582                                  double               height,
583                                  gboolean             duplex)
584 {
585         PSDocument *document = PS_DOCUMENT (exporter);
586
587         if (document->structured_doc) {
588                 g_free (document->ps_export_pagelist);
589         
590                 document->ps_export_pagelist = g_new0 (int, document->doc->numpages);
591         }
592
593         document->ps_export_filename = g_strdup (filename);
594 }
595
596 static void
597 ps_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
598 {
599         PSDocument *document = PS_DOCUMENT (exporter);
600         
601         if (document->structured_doc) {
602                 document->ps_export_pagelist[rc->page] = 1;
603         }
604 }
605
606 static void
607 ps_document_file_exporter_end (EvFileExporter *exporter)
608 {
609         PSDocument *document = PS_DOCUMENT (exporter);
610
611         if (!document->structured_doc) {
612                 save_document (document, document->ps_export_filename);
613         } else {
614                 save_page_list (document, document->ps_export_pagelist,
615                                 document->ps_export_filename);
616                 g_free (document->ps_export_pagelist);
617                 g_free (document->ps_export_filename);  
618                 document->ps_export_pagelist = NULL;
619                 document->ps_export_filename = NULL;
620         }
621 }
622
623 static void
624 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
625 {
626         iface->format_supported = ps_document_file_exporter_format_supported;
627         iface->begin = ps_document_file_exporter_begin;
628         iface->do_page = ps_document_file_exporter_do_page;
629         iface->end = ps_document_file_exporter_end;
630 }