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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <glib/gi18n-lib.h>
25 #include <libspectre/spectre.h>
27 #include "ev-spectre.h"
29 #include "ev-file-exporter.h"
30 #include "ev-document-misc.h"
36 SpectreExporter *exporter;
39 struct _PSDocumentClass {
40 EvDocumentClass parent_class;
43 static void ps_document_file_exporter_iface_init (EvFileExporterInterface *iface);
45 EV_BACKEND_REGISTER_WITH_CODE (PSDocument, ps_document,
47 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
48 ps_document_file_exporter_iface_init);
53 ps_document_init (PSDocument *ps_document)
58 ps_document_dispose (GObject *object)
60 PSDocument *ps = PS_DOCUMENT (object);
63 spectre_document_free (ps->doc);
68 spectre_exporter_free (ps->exporter);
72 G_OBJECT_CLASS (ps_document_parent_class)->dispose (object);
77 ps_document_load (EvDocument *document,
81 PSDocument *ps = PS_DOCUMENT (document);
84 filename = g_filename_from_uri (uri, NULL, error);
88 ps->doc = spectre_document_new ();
90 spectre_document_load (ps->doc, filename);
91 if (spectre_document_status (ps->doc)) {
94 filename_dsp = g_filename_display_name (filename);
98 _("Failed to load document ā%sā"),
100 g_free (filename_dsp);
112 ps_document_save (EvDocument *document,
116 PSDocument *ps = PS_DOCUMENT (document);
119 filename = g_filename_from_uri (uri, NULL, error);
123 spectre_document_save (ps->doc, filename);
124 if (spectre_document_status (ps->doc)) {
127 filename_dsp = g_filename_display_name (filename);
131 _("Failed to save document ā%sā"),
133 g_free (filename_dsp);
145 ps_document_get_n_pages (EvDocument *document)
147 PSDocument *ps = PS_DOCUMENT (document);
149 return spectre_document_get_n_pages (ps->doc);
153 ps_document_get_page (EvDocument *document,
156 PSDocument *ps = PS_DOCUMENT (document);
157 SpectrePage *ps_page;
160 ps_page = spectre_document_get_page (ps->doc, index);
161 page = ev_page_new (index);
162 page->backend_page = (EvBackendPage)ps_page;
163 page->backend_destroy_func = (EvBackendPageDestroyFunc)spectre_page_free;
169 get_page_rotation (SpectrePage *page)
171 switch (spectre_page_get_orientation (page)) {
173 case SPECTRE_ORIENTATION_PORTRAIT:
175 case SPECTRE_ORIENTATION_LANDSCAPE:
177 case SPECTRE_ORIENTATION_REVERSE_PORTRAIT:
179 case SPECTRE_ORIENTATION_REVERSE_LANDSCAPE:
187 ps_document_get_page_size (EvDocument *document,
192 SpectrePage *ps_page;
193 gdouble page_width, page_height;
194 gint pwidth, pheight;
197 ps_page = (SpectrePage *)page->backend_page;
199 spectre_page_get_size (ps_page, &pwidth, &pheight);
201 rotate = get_page_rotation (ps_page);
202 if (rotate == 90 || rotate == 270) {
203 page_height = pwidth;
204 page_width = pheight;
207 page_height = pheight;
215 *height = page_height;
220 ps_document_get_page_label (EvDocument *document,
223 return g_strdup (spectre_page_get_label ((SpectrePage *)page->backend_page));
226 static EvDocumentInfo *
227 ps_document_get_info (EvDocument *document)
229 PSDocument *ps = PS_DOCUMENT (document);
230 EvDocumentInfo *info;
231 const gchar *creator;
232 SpectrePage *ps_page;
235 info = g_new0 (EvDocumentInfo, 1);
236 info->fields_mask = EV_DOCUMENT_INFO_TITLE |
237 EV_DOCUMENT_INFO_FORMAT |
238 EV_DOCUMENT_INFO_CREATOR |
239 EV_DOCUMENT_INFO_N_PAGES |
240 EV_DOCUMENT_INFO_PAPER_SIZE;
242 creator = spectre_document_get_creator (ps->doc);
244 ps_page = spectre_document_get_page (ps->doc, 0);
245 spectre_page_get_size (ps_page, &width, &height);
246 spectre_page_free (ps_page);
248 info->title = g_strdup (spectre_document_get_title (ps->doc));
249 info->format = g_strdup (spectre_document_get_format (ps->doc));
250 info->creator = g_strdup (creator ? creator : spectre_document_get_for (ps->doc));
251 info->n_pages = spectre_document_get_n_pages (ps->doc);
252 info->paper_width = width / 72.0f * 25.4f;
253 info->paper_height = height / 72.0f * 25.4f;
259 ps_document_get_backend_info (EvDocument *document,
260 EvDocumentBackendInfo *info)
262 info->name = "libspectre";
263 info->version = SPECTRE_VERSION_STRING;
268 static cairo_surface_t *
269 ps_document_render (EvDocument *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 = (SpectrePage *)rc->page->backend_page;
286 spectre_page_get_size (ps_page, &width_points, &height_points);
288 width = (gint) ((width_points * rc->scale) + 0.5);
289 height = (gint) ((height_points * rc->scale) + 0.5);
290 rotation = (rc->rotation + get_page_rotation (ps_page)) % 360;
292 src = spectre_render_context_new ();
293 spectre_render_context_set_scale (src,
294 (gdouble)width / width_points,
295 (gdouble)height / height_points);
296 spectre_render_context_set_rotation (src, rotation);
297 spectre_page_render (ps_page, src, &data, &stride);
298 spectre_render_context_free (src);
304 if (spectre_page_status (ps_page)) {
305 g_warning ("%s", spectre_status_to_string (spectre_page_status (ps_page)));
311 if (rotation == 90 || rotation == 270) {
319 surface = cairo_image_surface_create_for_data (data,
323 cairo_surface_set_user_data (surface, &key,
324 data, (cairo_destroy_func_t)g_free);
329 ps_document_class_init (PSDocumentClass *klass)
331 GObjectClass *object_class = G_OBJECT_CLASS (klass);
332 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
334 object_class->dispose = ps_document_dispose;
336 ev_document_class->load = ps_document_load;
337 ev_document_class->save = ps_document_save;
338 ev_document_class->get_n_pages = ps_document_get_n_pages;
339 ev_document_class->get_page = ps_document_get_page;
340 ev_document_class->get_page_size = ps_document_get_page_size;
341 ev_document_class->get_page_label = ps_document_get_page_label;
342 ev_document_class->get_info = ps_document_get_info;
343 ev_document_class->get_backend_info = ps_document_get_backend_info;
344 ev_document_class->render = ps_document_render;
347 /* EvFileExporterIface */
349 ps_document_file_exporter_begin (EvFileExporter *exporter,
350 EvFileExporterContext *fc)
352 PSDocument *ps = PS_DOCUMENT (exporter);
355 spectre_exporter_free (ps->exporter);
357 switch (fc->format) {
358 case EV_FILE_FORMAT_PS:
360 spectre_exporter_new (ps->doc,
361 SPECTRE_EXPORTER_FORMAT_PS);
363 case EV_FILE_FORMAT_PDF:
365 spectre_exporter_new (ps->doc,
366 SPECTRE_EXPORTER_FORMAT_PDF);
369 g_assert_not_reached ();
372 spectre_exporter_begin (ps->exporter, fc->filename);
376 ps_document_file_exporter_do_page (EvFileExporter *exporter,
379 PSDocument *ps = PS_DOCUMENT (exporter);
381 spectre_exporter_do_page (ps->exporter, rc->page->index);
385 ps_document_file_exporter_end (EvFileExporter *exporter)
387 PSDocument *ps = PS_DOCUMENT (exporter);
389 spectre_exporter_end (ps->exporter);
392 static EvFileExporterCapabilities
393 ps_document_file_exporter_get_capabilities (EvFileExporter *exporter)
395 return EV_FILE_EXPORTER_CAN_PAGE_SET |
396 EV_FILE_EXPORTER_CAN_COPIES |
397 EV_FILE_EXPORTER_CAN_COLLATE |
398 EV_FILE_EXPORTER_CAN_REVERSE |
399 EV_FILE_EXPORTER_CAN_GENERATE_PS |
400 EV_FILE_EXPORTER_CAN_GENERATE_PDF;
404 ps_document_file_exporter_iface_init (EvFileExporterInterface *iface)
406 iface->begin = ps_document_file_exporter_begin;
407 iface->do_page = ps_document_file_exporter_do_page;
408 iface->end = ps_document_file_exporter_end;
409 iface->get_capabilities = ps_document_file_exporter_get_capabilities;