]> www.fi.muni.cz Git - evince.git/blob - backend/ps/ev-spectre.c
Include config.h. Bug #504721.
[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                                  G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
51                                                         ps_document_document_thumbnails_iface_init);
52                                  G_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_page_size (src, width, height);
293         spectre_render_context_set_rotation (src, rotation);
294         spectre_page_render (ps_page, src, &data, &stride);
295         spectre_render_context_free (src);
296
297         if (!data) {
298                 spectre_page_free (ps_page);
299                 return NULL;
300         }
301
302         if (spectre_page_status (ps_page)) {
303                 g_warning (spectre_status_to_string (spectre_page_status (ps_page)));
304                 g_free (data);
305                 spectre_page_free (ps_page);
306                 
307                 return NULL;
308         }
309
310         spectre_page_free (ps_page);
311
312         if (rotation == 90 || rotation == 270) {
313                 swidth = height;
314                 sheight = width;
315         } else {
316                 swidth = width;
317                 sheight = height;
318         }
319         
320         surface = cairo_image_surface_create_for_data (data,
321                                                        CAIRO_FORMAT_RGB24,
322                                                        swidth, sheight,
323                                                        stride);
324         cairo_surface_set_user_data (surface, &key,
325                                      data, (cairo_destroy_func_t)g_free);
326         return surface;
327 }
328
329 static void
330 ps_document_document_iface_init (EvDocumentIface *iface)
331 {
332         iface->load = ps_document_load;
333         iface->save = ps_document_save;
334         iface->get_n_pages = ps_document_get_n_pages;
335         iface->get_page_size = ps_document_get_page_size;
336         iface->get_page_label = ps_document_get_page_label;
337         iface->get_info = ps_document_get_info;
338         iface->render = ps_document_render;
339 }
340
341 /* EvDocumentThumbnailsIface */
342 static GdkPixbuf *
343 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
344                                       EvRenderContext      *rc, 
345                                       gboolean              border)
346 {
347         PSDocument      *ps = PS_DOCUMENT (document_thumbnails);
348         cairo_surface_t *surface;
349         GdkPixbuf       *pixbuf = NULL;
350
351         surface = ps_document_render (EV_DOCUMENT (ps), rc);
352         pixbuf = ev_document_misc_pixbuf_from_surface (surface);
353         cairo_surface_destroy (surface);
354
355         if (border) {
356                 GdkPixbuf *border_pixbuf;
357                 
358                 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
359                 g_object_unref (pixbuf);
360                 pixbuf = border_pixbuf;
361         }
362
363         return pixbuf;
364 }
365
366 static void
367 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
368                                        EvRenderContext      *rc, 
369                                        gint                 *width,
370                                        gint                 *height)
371 {
372         PSDocument *ps = PS_DOCUMENT (document_thumbnails);
373         gdouble     page_width, page_height;
374
375         ps_document_get_page_size (EV_DOCUMENT (ps),
376                                    rc->page,
377                                    &page_width, &page_height);
378
379         if (rc->rotation == 90 || rc->rotation == 270) {
380                 *width = (gint) (page_height * rc->scale);
381                 *height = (gint) (page_width * rc->scale);
382         } else {
383                 *width = (gint) (page_width * rc->scale);
384                 *height = (gint) (page_height * rc->scale);
385         }
386 }
387
388 static void
389 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
390 {
391         iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
392         iface->get_dimensions = ps_document_thumbnails_get_dimensions;
393 }
394         
395 /* EvFileExporterIface */
396 static void
397 ps_document_file_exporter_begin (EvFileExporter        *exporter,
398                                  EvFileExporterContext *fc)
399 {
400         PSDocument *ps = PS_DOCUMENT (exporter);
401
402         if (ps->exporter)
403                 spectre_exporter_free (ps->exporter);
404
405         switch (fc->format) {
406                 case EV_FILE_FORMAT_PS:
407                         ps->exporter =
408                                 spectre_exporter_new (ps->doc,
409                                                       SPECTRE_EXPORTER_FORMAT_PS);
410                         break;
411                 case EV_FILE_FORMAT_PDF:
412                         ps->exporter =
413                                 spectre_exporter_new (ps->doc,
414                                                       SPECTRE_EXPORTER_FORMAT_PDF);
415                         break;
416                 default:
417                         g_assert_not_reached ();
418         }
419
420         spectre_exporter_begin (ps->exporter, fc->filename);
421 }
422
423 static void
424 ps_document_file_exporter_do_page (EvFileExporter  *exporter,
425                                    EvRenderContext *rc)
426 {
427         PSDocument *ps = PS_DOCUMENT (exporter);
428
429         spectre_exporter_do_page (ps->exporter, rc->page);
430 }
431
432 static void
433 ps_document_file_exporter_end (EvFileExporter *exporter)
434 {
435         PSDocument *ps = PS_DOCUMENT (exporter);
436
437         spectre_exporter_end (ps->exporter);
438 }
439
440 static EvFileExporterCapabilities
441 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
442 {
443         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
444                 EV_FILE_EXPORTER_CAN_COPIES |
445                 EV_FILE_EXPORTER_CAN_COLLATE |
446                 EV_FILE_EXPORTER_CAN_REVERSE |
447                 EV_FILE_EXPORTER_CAN_GENERATE_PS |
448                 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
449 }
450
451 static void
452 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
453 {
454         iface->begin = ps_document_file_exporter_begin;
455         iface->do_page = ps_document_file_exporter_do_page;
456         iface->end = ps_document_file_exporter_end;
457         iface->get_capabilities = ps_document_file_exporter_get_capabilities;
458 }