]> www.fi.muni.cz Git - evince.git/blob - backend/ps/ev-spectre.c
6fe58692e1630f03d9e7047aca10c11fe6588c9b
[evince.git] / backend / ps / ev-spectre.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
4  *
5  * Evince is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Evince is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21
22 #include <config.h>
23 #include <glib/gi18n.h>
24 #include <stdlib.h>
25 #include <libspectre/spectre.h>
26
27 #include "ev-spectre.h"
28
29 #include "ev-file-exporter.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-document-misc.h"
32
33 struct _PSDocument {
34         GObject object;
35
36         SpectreDocument *doc;
37         SpectreExporter *exporter;
38 };
39
40 struct _PSDocumentClass {
41         GObjectClass parent_class;
42 };
43
44 static void ps_document_document_iface_init            (EvDocumentIface           *iface);
45 static void ps_document_file_exporter_iface_init       (EvFileExporterIface       *iface);
46 static void ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
47
48 EV_BACKEND_REGISTER_WITH_CODE (PSDocument, ps_document,
49                          {
50                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
51                                                                  ps_document_document_thumbnails_iface_init);
52                                  EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
53                                                                  ps_document_file_exporter_iface_init);
54                          });
55
56 /* PSDocument */
57 static void
58 ps_document_init (PSDocument *ps_document)
59 {
60 }
61
62 static void
63 ps_document_dispose (GObject *object)
64 {
65         PSDocument *ps = PS_DOCUMENT (object);
66
67         if (ps->doc) {
68                 spectre_document_free (ps->doc);
69                 ps->doc = NULL;
70         }
71
72         if (ps->exporter) {
73                 spectre_exporter_free (ps->exporter);
74                 ps->exporter = NULL;
75         }
76
77         G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
78 }
79
80 static void
81 ps_document_class_init (PSDocumentClass *klass)
82 {
83         GObjectClass *object_class;
84
85         object_class = G_OBJECT_CLASS (klass);
86
87         object_class->dispose = ps_document_dispose;
88 }
89
90 /* EvDocumentIface */
91 static gboolean
92 ps_document_load (EvDocument *document,
93                   const char *uri,
94                   GError    **error)
95 {
96         PSDocument *ps = PS_DOCUMENT (document);
97         gchar      *filename;
98
99         filename = g_filename_from_uri (uri, NULL, error);
100         if (!filename)
101                 return FALSE;
102         
103         ps->doc = spectre_document_new ();
104
105         spectre_document_load (ps->doc, filename);
106         if (spectre_document_status (ps->doc)) {
107                 gchar *filename_dsp;
108                 
109                 filename_dsp = g_filename_display_name (filename);
110                 g_set_error (error,
111                              G_FILE_ERROR,
112                              G_FILE_ERROR_FAILED,
113                              _("Failed to load document ā€œ%sā€"),
114                              filename_dsp);
115                 g_free (filename_dsp);
116                 g_free (filename);
117
118                 return FALSE;
119         }
120
121         g_free (filename);
122
123         return TRUE;
124 }
125
126 static gboolean
127 ps_document_save (EvDocument *document,
128                   const char *uri,
129                   GError    **error)
130 {
131         PSDocument *ps = PS_DOCUMENT (document);
132         gchar      *filename;
133
134         filename = g_filename_from_uri (uri, NULL, error);
135         if (!filename)
136                 return FALSE;
137
138         spectre_document_save (ps->doc, filename);
139         if (spectre_document_status (ps->doc)) {
140                 gchar *filename_dsp;
141
142                 filename_dsp = g_filename_display_name (filename);
143                 g_set_error (error,
144                              G_FILE_ERROR,
145                              G_FILE_ERROR_FAILED,
146                              _("Failed to save document ā€œ%sā€"),
147                              filename_dsp);
148                 g_free (filename_dsp);
149                 g_free (filename);
150
151                 return FALSE;
152         }
153
154         g_free (filename);
155
156         return TRUE;
157 }
158
159 static int
160 ps_document_get_n_pages (EvDocument *document)
161 {
162         PSDocument *ps = PS_DOCUMENT (document);
163
164         return spectre_document_get_n_pages (ps->doc);
165 }
166
167 static gint
168 get_page_rotation (SpectrePage *page)
169 {
170         switch (spectre_page_get_orientation (page)) {
171                 default:
172                 case SPECTRE_ORIENTATION_PORTRAIT:
173                         return 0;
174                 case SPECTRE_ORIENTATION_LANDSCAPE:
175                         return 90;
176                 case SPECTRE_ORIENTATION_REVERSE_PORTRAIT:
177                         return 180;
178                 case SPECTRE_ORIENTATION_REVERSE_LANDSCAPE:
179                         return 270;
180         }
181
182         return 0;
183 }
184
185 static void
186 ps_document_get_page_size (EvDocument *document,
187                            int         page,
188                            double     *width,
189                            double     *height)
190 {
191         PSDocument  *ps = PS_DOCUMENT (document);
192         SpectrePage *ps_page;
193         gdouble      page_width, page_height;
194         gint         pwidth, pheight;
195         gint         rotate;
196
197         ps_page = spectre_document_get_page (ps->doc, page);
198         spectre_page_get_size (ps_page, &pwidth, &pheight);
199
200         rotate = get_page_rotation (ps_page);
201         if (rotate == 90 || rotate == 270) {
202                 page_height = pwidth;
203                 page_width = pheight;
204         } else {
205                 page_width = pwidth;
206                 page_height = pheight;
207         }
208
209         spectre_page_free (ps_page);
210         
211         if (width) {
212                 *width = page_width;
213         }
214
215         if (height) {
216                 *height = page_height;
217         }
218 }
219
220 static char *
221 ps_document_get_page_label (EvDocument *document,
222                             int         page)
223 {
224         PSDocument  *ps = PS_DOCUMENT (document);
225         SpectrePage *ps_page;
226         gchar       *label;
227
228         ps_page = spectre_document_get_page (ps->doc, page);
229         label = g_strdup (spectre_page_get_label (ps_page));
230         spectre_page_free (ps_page);
231         
232         return label;
233 }
234
235 static EvDocumentInfo *
236 ps_document_get_info (EvDocument *document)
237 {
238         PSDocument     *ps = PS_DOCUMENT (document);
239         EvDocumentInfo *info;
240         const gchar    *creator;
241         SpectrePage    *ps_page;
242         gint            width, height;
243
244         info = g_new0 (EvDocumentInfo, 1);
245         info->fields_mask = EV_DOCUMENT_INFO_TITLE |
246                             EV_DOCUMENT_INFO_FORMAT |
247                             EV_DOCUMENT_INFO_CREATOR |
248                             EV_DOCUMENT_INFO_N_PAGES |
249                             EV_DOCUMENT_INFO_PAPER_SIZE;
250
251         creator = spectre_document_get_creator (ps->doc);
252
253         ps_page = spectre_document_get_page (ps->doc, 0);
254         spectre_page_get_size (ps_page, &width, &height);
255         spectre_page_free (ps_page);
256         
257         info->title = g_strdup (spectre_document_get_title (ps->doc));
258         info->format = g_strdup (spectre_document_get_format (ps->doc));
259         info->creator = g_strdup (creator ? creator : spectre_document_get_for (ps->doc));
260         info->n_pages = spectre_document_get_n_pages (ps->doc);
261         info->paper_width  = width / 72.0f * 25.4f;
262         info->paper_height = height / 72.0f * 25.4f;
263
264         return info;
265 }
266
267 static cairo_surface_t *
268 ps_document_render (EvDocument      *document,
269                     EvRenderContext *rc)
270 {
271         PSDocument           *ps = PS_DOCUMENT (document);
272         SpectrePage          *ps_page;
273         SpectreRenderContext *src;
274         gint                  width_points;
275         gint                  height_points;
276         gint                  width, height;
277         gint                  swidth, sheight;
278         guchar               *data = NULL;
279         gint                  stride;
280         gint                  rotation;
281         cairo_surface_t      *surface;
282         static const cairo_user_data_key_t key;
283
284         ps_page = spectre_document_get_page (ps->doc, rc->page);
285         spectre_page_get_size (ps_page, &width_points, &height_points);
286
287         width = (gint) ((width_points * rc->scale) + 0.5);
288         height = (gint) ((height_points * rc->scale) + 0.5);
289         rotation = (rc->rotation + get_page_rotation (ps_page)) % 360;
290
291         src = spectre_render_context_new ();
292         spectre_render_context_set_scale (src,
293                                           (gdouble)width / width_points,
294                                           (gdouble)height / height_points);
295         spectre_render_context_set_rotation (src, rotation);
296         spectre_page_render (ps_page, src, &data, &stride);
297         spectre_render_context_free (src);
298
299         if (!data) {
300                 spectre_page_free (ps_page);
301                 return NULL;
302         }
303
304         if (spectre_page_status (ps_page)) {
305                 g_warning (spectre_status_to_string (spectre_page_status (ps_page)));
306                 g_free (data);
307                 spectre_page_free (ps_page);
308                 
309                 return NULL;
310         }
311
312         spectre_page_free (ps_page);
313
314         if (rotation == 90 || rotation == 270) {
315                 swidth = height;
316                 sheight = width;
317         } else {
318                 swidth = width;
319                 sheight = height;
320         }
321         
322         surface = cairo_image_surface_create_for_data (data,
323                                                        CAIRO_FORMAT_RGB24,
324                                                        swidth, sheight,
325                                                        stride);
326         cairo_surface_set_user_data (surface, &key,
327                                      data, (cairo_destroy_func_t)g_free);
328         return surface;
329 }
330
331 static void
332 ps_document_document_iface_init (EvDocumentIface *iface)
333 {
334         iface->load = ps_document_load;
335         iface->save = ps_document_save;
336         iface->get_n_pages = ps_document_get_n_pages;
337         iface->get_page_size = ps_document_get_page_size;
338         iface->get_page_label = ps_document_get_page_label;
339         iface->get_info = ps_document_get_info;
340         iface->render = ps_document_render;
341 }
342
343 /* EvDocumentThumbnailsIface */
344 static GdkPixbuf *
345 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
346                                       EvRenderContext      *rc, 
347                                       gboolean              border)
348 {
349         PSDocument      *ps = PS_DOCUMENT (document_thumbnails);
350         cairo_surface_t *surface;
351         GdkPixbuf       *pixbuf = NULL;
352
353         surface = ps_document_render (EV_DOCUMENT (ps), rc);
354         if (!surface) {
355                 g_warning ("Error rendering thumbnail");
356                 return NULL;
357         }
358                 
359         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
360         cairo_surface_destroy (surface);
361
362         if (border) {
363                 GdkPixbuf *border_pixbuf;
364                 
365                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
366                 g_object_unref (pixbuf);
367                 pixbuf = border_pixbuf;
368         }
369
370         return pixbuf;
371 }
372
373 static void
374 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
375                                        EvRenderContext      *rc, 
376                                        gint                 *width,
377                                        gint                 *height)
378 {
379         PSDocument *ps = PS_DOCUMENT (document_thumbnails);
380         gdouble     page_width, page_height;
381
382         ps_document_get_page_size (EV_DOCUMENT (ps),
383                                    rc->page,
384                                    &page_width, &page_height);
385
386         if (rc->rotation == 90 || rc->rotation == 270) {
387                 *width = (gint) (page_height * rc->scale);
388                 *height = (gint) (page_width * rc->scale);
389         } else {
390                 *width = (gint) (page_width * rc->scale);
391                 *height = (gint) (page_height * rc->scale);
392         }
393 }
394
395 static void
396 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
397 {
398         iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
399         iface->get_dimensions = ps_document_thumbnails_get_dimensions;
400 }
401         
402 /* EvFileExporterIface */
403 static void
404 ps_document_file_exporter_begin (EvFileExporter        *exporter,
405                                  EvFileExporterContext *fc)
406 {
407         PSDocument *ps = PS_DOCUMENT (exporter);
408
409         if (ps->exporter)
410                 spectre_exporter_free (ps->exporter);
411
412         switch (fc->format) {
413                 case EV_FILE_FORMAT_PS:
414                         ps->exporter =
415                                 spectre_exporter_new (ps->doc,
416                                                       SPECTRE_EXPORTER_FORMAT_PS);
417                         break;
418                 case EV_FILE_FORMAT_PDF:
419                         ps->exporter =
420                                 spectre_exporter_new (ps->doc,
421                                                       SPECTRE_EXPORTER_FORMAT_PDF);
422                         break;
423                 default:
424                         g_assert_not_reached ();
425         }
426
427         spectre_exporter_begin (ps->exporter, fc->filename);
428 }
429
430 static void
431 ps_document_file_exporter_do_page (EvFileExporter  *exporter,
432                                    EvRenderContext *rc)
433 {
434         PSDocument *ps = PS_DOCUMENT (exporter);
435
436         spectre_exporter_do_page (ps->exporter, rc->page);
437 }
438
439 static void
440 ps_document_file_exporter_end (EvFileExporter *exporter)
441 {
442         PSDocument *ps = PS_DOCUMENT (exporter);
443
444         spectre_exporter_end (ps->exporter);
445 }
446
447 static EvFileExporterCapabilities
448 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
449 {
450         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
451                 EV_FILE_EXPORTER_CAN_COPIES |
452                 EV_FILE_EXPORTER_CAN_COLLATE |
453                 EV_FILE_EXPORTER_CAN_REVERSE |
454                 EV_FILE_EXPORTER_CAN_GENERATE_PS |
455                 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
456 }
457
458 static void
459 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
460 {
461         iface->begin = ps_document_file_exporter_begin;
462         iface->do_page = ps_document_file_exporter_do_page;
463         iface->end = ps_document_file_exporter_end;
464         iface->get_capabilities = ps_document_file_exporter_get_capabilities;
465 }