1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
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.
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.
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.
23 #include <glib/gi18n.h>
25 #include <libspectre/spectre.h>
27 #include "ev-spectre.h"
29 #include "ev-file-exporter.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-document-misc.h"
37 SpectreExporter *exporter;
40 struct _PSDocumentClass {
41 GObjectClass parent_class;
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);
48 EV_BACKEND_REGISTER_WITH_CODE (PSDocument, ps_document,
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);
58 ps_document_init (PSDocument *ps_document)
63 ps_document_dispose (GObject *object)
65 PSDocument *ps = PS_DOCUMENT (object);
68 spectre_document_free (ps->doc);
73 spectre_exporter_free (ps->exporter);
77 G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
81 ps_document_class_init (PSDocumentClass *klass)
83 GObjectClass *object_class;
85 object_class = G_OBJECT_CLASS (klass);
87 object_class->dispose = ps_document_dispose;
92 ps_document_load (EvDocument *document,
96 PSDocument *ps = PS_DOCUMENT (document);
99 filename = g_filename_from_uri (uri, NULL, error);
103 ps->doc = spectre_document_new ();
105 spectre_document_load (ps->doc, filename);
106 if (spectre_document_status (ps->doc)) {
109 filename_dsp = g_filename_display_name (filename);
113 _("Failed to load document ā%sā"),
115 g_free (filename_dsp);
127 ps_document_save (EvDocument *document,
131 PSDocument *ps = PS_DOCUMENT (document);
134 filename = g_filename_from_uri (uri, NULL, error);
138 spectre_document_save (ps->doc, filename);
139 if (spectre_document_status (ps->doc)) {
142 filename_dsp = g_filename_display_name (filename);
146 _("Failed to save document ā%sā"),
148 g_free (filename_dsp);
160 ps_document_get_n_pages (EvDocument *document)
162 PSDocument *ps = PS_DOCUMENT (document);
164 return spectre_document_get_n_pages (ps->doc);
168 ps_document_get_page (EvDocument *document,
171 PSDocument *ps = PS_DOCUMENT (document);
172 SpectrePage *ps_page;
175 ps_page = spectre_document_get_page (ps->doc, index);
176 page = ev_page_new (index);
177 page->backend_page = (EvBackendPage)ps_page;
178 page->backend_destroy_func = (EvBackendPageDestroyFunc)spectre_page_free;
184 get_page_rotation (SpectrePage *page)
186 switch (spectre_page_get_orientation (page)) {
188 case SPECTRE_ORIENTATION_PORTRAIT:
190 case SPECTRE_ORIENTATION_LANDSCAPE:
192 case SPECTRE_ORIENTATION_REVERSE_PORTRAIT:
194 case SPECTRE_ORIENTATION_REVERSE_LANDSCAPE:
202 ps_document_get_page_size (EvDocument *document,
207 SpectrePage *ps_page;
208 gdouble page_width, page_height;
209 gint pwidth, pheight;
212 ps_page = (SpectrePage *)page->backend_page;
214 spectre_page_get_size (ps_page, &pwidth, &pheight);
216 rotate = get_page_rotation (ps_page);
217 if (rotate == 90 || rotate == 270) {
218 page_height = pwidth;
219 page_width = pheight;
222 page_height = pheight;
230 *height = page_height;
235 ps_document_get_page_label (EvDocument *document,
238 return g_strdup (spectre_page_get_label ((SpectrePage *)page->backend_page));
241 static EvDocumentInfo *
242 ps_document_get_info (EvDocument *document)
244 PSDocument *ps = PS_DOCUMENT (document);
245 EvDocumentInfo *info;
246 const gchar *creator;
247 SpectrePage *ps_page;
250 info = g_new0 (EvDocumentInfo, 1);
251 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
252 EV_DOCUMENT_INFO_FORMAT |
253 EV_DOCUMENT_INFO_CREATOR |
254 EV_DOCUMENT_INFO_N_PAGES |
255 EV_DOCUMENT_INFO_PAPER_SIZE;
257 creator = spectre_document_get_creator (ps->doc);
259 ps_page = spectre_document_get_page (ps->doc, 0);
260 spectre_page_get_size (ps_page, &width, &height);
261 spectre_page_free (ps_page);
263 info->title = g_strdup (spectre_document_get_title (ps->doc));
264 info->format = g_strdup (spectre_document_get_format (ps->doc));
265 info->creator = g_strdup (creator ? creator : spectre_document_get_for (ps->doc));
266 info->n_pages = spectre_document_get_n_pages (ps->doc);
267 info->paper_width = width / 72.0f * 25.4f;
268 info->paper_height = height / 72.0f * 25.4f;
273 static cairo_surface_t *
274 ps_document_render (EvDocument *document,
277 SpectrePage *ps_page;
278 SpectreRenderContext *src;
282 gint swidth, sheight;
286 cairo_surface_t *surface;
287 static const cairo_user_data_key_t key;
289 ps_page = (SpectrePage *)rc->page->backend_page;
291 spectre_page_get_size (ps_page, &width_points, &height_points);
293 width = (gint) ((width_points * rc->scale) + 0.5);
294 height = (gint) ((height_points * rc->scale) + 0.5);
295 rotation = (rc->rotation + get_page_rotation (ps_page)) % 360;
297 src = spectre_render_context_new ();
298 spectre_render_context_set_scale (src,
299 (gdouble)width / width_points,
300 (gdouble)height / height_points);
301 spectre_render_context_set_rotation (src, rotation);
302 spectre_page_render (ps_page, src, &data, &stride);
303 spectre_render_context_free (src);
309 if (spectre_page_status (ps_page)) {
310 g_warning ("%s", spectre_status_to_string (spectre_page_status (ps_page)));
316 if (rotation == 90 || rotation == 270) {
324 surface = cairo_image_surface_create_for_data (data,
328 cairo_surface_set_user_data (surface, &key,
329 data, (cairo_destroy_func_t)g_free);
334 ps_document_document_iface_init (EvDocumentIface *iface)
336 iface->load = ps_document_load;
337 iface->save = ps_document_save;
338 iface->get_n_pages = ps_document_get_n_pages;
339 iface->get_page = ps_document_get_page;
340 iface->get_page_size = ps_document_get_page_size;
341 iface->get_page_label = ps_document_get_page_label;
342 iface->get_info = ps_document_get_info;
343 iface->render = ps_document_render;
346 /* EvDocumentThumbnailsIface */
348 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
352 PSDocument *ps = PS_DOCUMENT (document_thumbnails);
353 cairo_surface_t *surface;
354 GdkPixbuf *pixbuf = NULL;
356 surface = ps_document_render (EV_DOCUMENT (ps), rc);
358 g_warning ("Error rendering thumbnail");
362 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
363 cairo_surface_destroy (surface);
366 GdkPixbuf *border_pixbuf;
368 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
369 g_object_unref (pixbuf);
370 pixbuf = border_pixbuf;
377 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
382 PSDocument *ps = PS_DOCUMENT (document_thumbnails);
383 gdouble page_width, page_height;
385 ps_document_get_page_size (EV_DOCUMENT (ps),
387 &page_width, &page_height);
389 if (rc->rotation == 90 || rc->rotation == 270) {
390 *width = (gint) (page_height * rc->scale);
391 *height = (gint) (page_width * rc->scale);
393 *width = (gint) (page_width * rc->scale);
394 *height = (gint) (page_height * rc->scale);
399 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
401 iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
402 iface->get_dimensions = ps_document_thumbnails_get_dimensions;
405 /* EvFileExporterIface */
407 ps_document_file_exporter_begin (EvFileExporter *exporter,
408 EvFileExporterContext *fc)
410 PSDocument *ps = PS_DOCUMENT (exporter);
413 spectre_exporter_free (ps->exporter);
415 switch (fc->format) {
416 case EV_FILE_FORMAT_PS:
418 spectre_exporter_new (ps->doc,
419 SPECTRE_EXPORTER_FORMAT_PS);
421 case EV_FILE_FORMAT_PDF:
423 spectre_exporter_new (ps->doc,
424 SPECTRE_EXPORTER_FORMAT_PDF);
427 g_assert_not_reached ();
430 spectre_exporter_begin (ps->exporter, fc->filename);
434 ps_document_file_exporter_do_page (EvFileExporter *exporter,
437 PSDocument *ps = PS_DOCUMENT (exporter);
439 spectre_exporter_do_page (ps->exporter, rc->page->index);
443 ps_document_file_exporter_end (EvFileExporter *exporter)
445 PSDocument *ps = PS_DOCUMENT (exporter);
447 spectre_exporter_end (ps->exporter);
450 static EvFileExporterCapabilities
451 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
453 return EV_FILE_EXPORTER_CAN_PAGE_SET |
454 EV_FILE_EXPORTER_CAN_COPIES |
455 EV_FILE_EXPORTER_CAN_COLLATE |
456 EV_FILE_EXPORTER_CAN_REVERSE |
457 EV_FILE_EXPORTER_CAN_GENERATE_PS |
458 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
462 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
464 iface->begin = ps_document_file_exporter_begin;
465 iface->do_page = ps_document_file_exporter_do_page;
466 iface->end = ps_document_file_exporter_end;
467 iface->get_capabilities = ps_document_file_exporter_get_capabilities;