/* * sample-document.c * * Copyright (C) 2012, Hodong Kim * Copyright (C) 2009, Juanjo MarĂ­n * Copyright (C) 2004, Red Hat, Inc. * * sample-document.c is based on ev-poppler.cc * http://git.gnome.org/browse/evince/tree/backend/pdf/ev-poppler.cc * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "config.h" #define GETTEXT_PACKAGE "evince-sample-backend" #include #include #include "sample-document.h" struct _SampleDocument { EvDocument parent_instance; ExampleDocument *document; }; struct _SampleDocumentClass { EvDocumentClass parent_class; }; EV_BACKEND_REGISTER (SampleDocument, sample_document) static void sample_document_init (SampleDocument *sample_document) { } static void sample_document_dispose (GObject *object) { SampleDocument *sample_document = SAMPLE_DOCUMENT (object); if (sample_document->document) { g_object_unref (sample_document->document); sample_document->document = NULL; } G_OBJECT_CLASS (sample_document_parent_class)->dispose (object); } static gboolean sample_document_load (EvDocument *document, const char *uri, GError **error) { SampleDocument *sample_document = SAMPLE_DOCUMENT (document); sample_document->document = example_document_new_from_uri (uri, error); if (!sample_document->document) { return FALSE; } return TRUE; } static gint sample_document_get_n_pages (EvDocument *document) { SampleDocument *sample_document = SAMPLE_DOCUMENT (document); return example_document_get_n_pages (sample_document->document); } static EvPage * sample_document_get_page (EvDocument *document, gint index) { SampleDocument *sample_document = SAMPLE_DOCUMENT (document); ExamplePage *example_page; EvPage *page; example_page = example_document_get_page (sample_document->document, index); page = ev_page_new (index); page->backend_page = (EvBackendPage)g_object_ref (example_page); page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref; g_object_unref (example_page); return page; } static void sample_document_get_page_size (EvDocument *document, EvPage *page, double *width, double *height) { g_return_if_fail (EXAMPLE_IS_PAGE (page->backend_page)); example_page_get_size (EXAMPLE_PAGE (page->backend_page), width, height); } static cairo_surface_t * sample_document_render (EvDocument *document, EvRenderContext *rc) { ExamplePage *example_page; gdouble width_points, height_points; guint width, height; cairo_surface_t *surface; cairo_t *cr; example_page = EXAMPLE_PAGE (rc->page->backend_page); example_page_get_size (example_page, &width_points, &height_points); if (rc->rotation == 90 || rc->rotation == 270) { width = (guint) ((height_points * rc->scale) + 0.5); height = (guint) ((width_points * rc->scale) + 0.5); } else { width = (guint) ((width_points * rc->scale) + 0.5); height = (guint) ((height_points * rc->scale) + 0.5); } surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); cr = cairo_create (surface); switch (rc->rotation) { case 90: cairo_translate (cr, width, 0); break; case 180: cairo_translate (cr, width, height); break; case 270: cairo_translate (cr, 0, height); break; default: cairo_translate (cr, 0, 0); } cairo_scale (cr, rc->scale, rc->scale); cairo_rotate (cr, rc->rotation * G_PI / 180.0); example_page_render (example_page, cr); cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER); cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); cairo_paint (cr); cairo_destroy (cr); return surface; } static void sample_document_class_init (SampleDocumentClass *klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass); gobject_class->dispose = sample_document_dispose; ev_document_class->load = sample_document_load; ev_document_class->get_n_pages = sample_document_get_n_pages; ev_document_class->get_page = sample_document_get_page; ev_document_class->get_page_size = sample_document_get_page_size; ev_document_class->render = sample_document_render; }