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 get_page_rotation (SpectrePage *page)
170 switch (spectre_page_get_orientation (page)) {
172 case SPECTRE_ORIENTATION_PORTRAIT:
174 case SPECTRE_ORIENTATION_LANDSCAPE:
176 case SPECTRE_ORIENTATION_REVERSE_PORTRAIT:
178 case SPECTRE_ORIENTATION_REVERSE_LANDSCAPE:
186 ps_document_get_page_size (EvDocument *document,
191 PSDocument *ps = PS_DOCUMENT (document);
192 SpectrePage *ps_page;
193 gdouble page_width, page_height;
194 gint pwidth, pheight;
197 ps_page = spectre_document_get_page (ps->doc, page);
198 spectre_page_get_size (ps_page, &pwidth, &pheight);
200 rotate = get_page_rotation (ps_page);
201 if (rotate == 90 || rotate == 270) {
202 page_height = pwidth;
203 page_width = pheight;
206 page_height = pheight;
209 spectre_page_free (ps_page);
216 *height = page_height;
221 ps_document_get_page_label (EvDocument *document,
224 PSDocument *ps = PS_DOCUMENT (document);
225 SpectrePage *ps_page;
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);
235 static EvDocumentInfo *
236 ps_document_get_info (EvDocument *document)
238 PSDocument *ps = PS_DOCUMENT (document);
239 EvDocumentInfo *info;
240 const gchar *creator;
241 SpectrePage *ps_page;
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;
251 creator = spectre_document_get_creator (ps->doc);
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);
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;
267 static cairo_surface_t *
268 ps_document_render (EvDocument *document,
271 PSDocument *ps = PS_DOCUMENT (document);
272 SpectrePage *ps_page;
273 SpectreRenderContext *src;
277 gint swidth, sheight;
281 cairo_surface_t *surface;
282 static const cairo_user_data_key_t key;
284 ps_page = spectre_document_get_page (ps->doc, rc->page);
285 spectre_page_get_size (ps_page, &width_points, &height_points);
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;
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);
300 spectre_page_free (ps_page);
304 if (spectre_page_status (ps_page)) {
305 g_warning (spectre_status_to_string (spectre_page_status (ps_page)));
307 spectre_page_free (ps_page);
312 spectre_page_free (ps_page);
314 if (rotation == 90 || rotation == 270) {
322 surface = cairo_image_surface_create_for_data (data,
326 cairo_surface_set_user_data (surface, &key,
327 data, (cairo_destroy_func_t)g_free);
332 ps_document_document_iface_init (EvDocumentIface *iface)
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;
343 /* EvDocumentThumbnailsIface */
345 ps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document_thumbnails,
349 PSDocument *ps = PS_DOCUMENT (document_thumbnails);
350 cairo_surface_t *surface;
351 GdkPixbuf *pixbuf = NULL;
353 surface = ps_document_render (EV_DOCUMENT (ps), rc);
355 g_warning ("Error rendering thumbnail");
359 pixbuf = ev_document_misc_pixbuf_from_surface (surface);
360 cairo_surface_destroy (surface);
363 GdkPixbuf *border_pixbuf;
365 border_pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, pixbuf);
366 g_object_unref (pixbuf);
367 pixbuf = border_pixbuf;
374 ps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document_thumbnails,
379 PSDocument *ps = PS_DOCUMENT (document_thumbnails);
380 gdouble page_width, page_height;
382 ps_document_get_page_size (EV_DOCUMENT (ps),
384 &page_width, &page_height);
386 if (rc->rotation == 90 || rc->rotation == 270) {
387 *width = (gint) (page_height * rc->scale);
388 *height = (gint) (page_width * rc->scale);
390 *width = (gint) (page_width * rc->scale);
391 *height = (gint) (page_height * rc->scale);
396 ps_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
398 iface->get_thumbnail = ps_document_thumbnails_get_thumbnail;
399 iface->get_dimensions = ps_document_thumbnails_get_dimensions;
402 /* EvFileExporterIface */
404 ps_document_file_exporter_begin (EvFileExporter *exporter,
405 EvFileExporterContext *fc)
407 PSDocument *ps = PS_DOCUMENT (exporter);
410 spectre_exporter_free (ps->exporter);
412 switch (fc->format) {
413 case EV_FILE_FORMAT_PS:
415 spectre_exporter_new (ps->doc,
416 SPECTRE_EXPORTER_FORMAT_PS);
418 case EV_FILE_FORMAT_PDF:
420 spectre_exporter_new (ps->doc,
421 SPECTRE_EXPORTER_FORMAT_PDF);
424 g_assert_not_reached ();
427 spectre_exporter_begin (ps->exporter, fc->filename);
431 ps_document_file_exporter_do_page (EvFileExporter *exporter,
434 PSDocument *ps = PS_DOCUMENT (exporter);
436 spectre_exporter_do_page (ps->exporter, rc->page);
440 ps_document_file_exporter_end (EvFileExporter *exporter)
442 PSDocument *ps = PS_DOCUMENT (exporter);
444 spectre_exporter_end (ps->exporter);
447 static EvFileExporterCapabilities
448 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
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;
459 ps_document_file_exporter_iface_init (EvFileExporterIface *iface)
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;