1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2010 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.
22 #include <glib/gi18n-lib.h>
23 #include <libgxps/gxps.h>
25 #include "xps-document.h"
26 #include "ev-document-links.h"
27 #include "ev-document-misc.h"
37 struct _XPSDocumentClass {
38 EvDocumentClass parent_class;
41 static void xps_document_document_links_iface_init (EvDocumentLinksInterface *iface);
43 EV_BACKEND_REGISTER_WITH_CODE (XPSDocument, xps_document,
45 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
46 xps_document_document_links_iface_init);
51 xps_document_init (XPSDocument *ps_document)
56 xps_document_dispose (GObject *object)
58 XPSDocument *xps = XPS_DOCUMENT (object);
61 g_object_unref (xps->file);
66 g_object_unref (xps->xps);
71 g_object_unref (xps->doc);
75 G_OBJECT_CLASS (xps_document_parent_class)->dispose (object);
80 xps_document_load (EvDocument *document,
84 XPSDocument *xps = XPS_DOCUMENT (document);
86 xps->file = g_file_new_for_uri (uri);
87 xps->xps = gxps_file_new (xps->file, error);
92 /* FIXME: what if there are multiple docs? */
93 xps->doc = gxps_file_get_document (xps->xps, 0, error);
95 g_object_unref (xps->xps);
105 xps_document_save (EvDocument *document,
109 XPSDocument *xps = XPS_DOCUMENT (document);
113 dest = g_file_new_for_uri (uri);
114 retval = g_file_copy (xps->file, dest,
115 G_FILE_COPY_TARGET_DEFAULT_PERMS |
116 G_FILE_COPY_OVERWRITE,
117 NULL, NULL, NULL, error);
118 g_object_unref (dest);
124 xps_document_get_n_pages (EvDocument *document)
126 XPSDocument *xps = XPS_DOCUMENT (document);
128 return gxps_document_get_n_pages (xps->doc);
132 xps_document_get_page (EvDocument *document,
135 XPSDocument *xps = XPS_DOCUMENT (document);
139 xps_page = gxps_document_get_page (xps->doc, index, NULL);
140 page = ev_page_new (index);
142 page->backend_page = (EvBackendPage)xps_page;
143 page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
150 xps_document_get_page_size (EvDocument *document,
158 xps_page = GXPS_PAGE (page->backend_page);
160 gxps_page_get_size (xps_page, &w, &h);
165 *height = (gdouble)h;
168 static EvDocumentInfo *
169 xps_document_get_info (EvDocument *document)
171 XPSDocument *xps = XPS_DOCUMENT (document);
172 EvDocumentInfo *info;
174 info = g_new0 (EvDocumentInfo, 1);
176 EV_DOCUMENT_INFO_N_PAGES |
177 EV_DOCUMENT_INFO_PAPER_SIZE;
180 if (gxps_document_get_n_pages (xps->doc) > 0) {
181 ev_document_get_page_size (document, 0,
182 &(info->paper_width),
183 &(info->paper_height));
184 info->paper_width = info->paper_width / 96.0f * 25.4f;
185 info->paper_height = info->paper_height / 96.0f * 25.4f;
188 info->n_pages = gxps_document_get_n_pages (xps->doc);
194 xps_document_get_backend_info (EvDocument *document,
195 EvDocumentBackendInfo *info)
197 info->name = "libgxps";
204 static cairo_surface_t *
205 xps_document_render (EvDocument *document,
209 guint page_width, page_height;
211 cairo_surface_t *surface;
213 GError *error = NULL;
215 xps_page = GXPS_PAGE (rc->page->backend_page);
217 gxps_page_get_size (xps_page, &page_width, &page_height);
218 if (rc->rotation == 90 || rc->rotation == 270) {
219 width = (guint) ((page_height * rc->scale) + 0.5);
220 height = (guint) ((page_width * rc->scale) + 0.5);
222 width = (guint) ((page_width * rc->scale) + 0.5);
223 height = (guint) ((page_height * rc->scale) + 0.5);
226 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
228 cr = cairo_create (surface);
230 cairo_set_source_rgb (cr, 1., 1., 1.);
233 switch (rc->rotation) {
235 cairo_translate (cr, width, 0);
238 cairo_translate (cr, width, height);
241 cairo_translate (cr, 0, height);
244 cairo_translate (cr, 0, 0);
247 cairo_scale (cr, rc->scale, rc->scale);
248 cairo_rotate (cr, rc->rotation * G_PI / 180.0);
249 gxps_page_render (xps_page, cr, &error);
253 g_warning ("Error rendering page %d: %s\n",
254 rc->page->index, error->message);
255 g_error_free (error);
262 xps_document_class_init (XPSDocumentClass *klass)
264 GObjectClass *object_class = G_OBJECT_CLASS (klass);
265 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
267 object_class->dispose = xps_document_dispose;
269 ev_document_class->load = xps_document_load;
270 ev_document_class->save = xps_document_save;
271 ev_document_class->get_n_pages = xps_document_get_n_pages;
272 ev_document_class->get_page = xps_document_get_page;
273 ev_document_class->get_page_size = xps_document_get_page_size;
274 ev_document_class->get_info = xps_document_get_info;
275 ev_document_class->get_backend_info = xps_document_get_backend_info;
276 ev_document_class->render = xps_document_render;
279 /* EvDocumentLinks */
281 xps_document_links_has_document_links (EvDocumentLinks *document_links)
283 XPSDocument *xps_document = XPS_DOCUMENT (document_links);
284 GXPSDocumentStructure *structure;
287 structure = gxps_document_get_structure (xps_document->doc);
291 retval = gxps_document_structure_has_outline (structure);
292 g_object_unref (structure);
298 ev_link_from_target (XPSDocument *xps_document,
299 GXPSLinkTarget *target)
301 EvLinkAction *ev_action;
303 if (gxps_link_target_is_internal (target)) {
304 EvLinkDest *dest = NULL;
308 anchor = gxps_link_target_get_anchor (target);
310 /* FIXME: multidoc */
311 doc = gxps_file_get_document_for_link_target (xps_document->xps, target);
316 dest = ev_link_dest_new_named (anchor);
317 ev_action = ev_link_action_new_dest (dest);
321 /* FIXME: remote uri? */
322 filename = g_file_get_path (xps_document->file);
325 dest = ev_link_dest_new_named (anchor);
326 ev_action = ev_link_action_new_remote (dest, filename);
332 uri = gxps_link_target_get_uri (target);
333 ev_action = ev_link_action_new_external_uri (uri);
336 return ev_link_new (NULL, ev_action);
340 build_tree (XPSDocument *xps_document,
343 GXPSOutlineIter *iter)
346 GtkTreeIter tree_iter;
347 GXPSOutlineIter child_iter;
349 GXPSLinkTarget *target;
352 target = gxps_outline_iter_get_target (iter);
353 title = g_markup_escape_text (gxps_outline_iter_get_description (iter), -1);
354 link = ev_link_from_target (xps_document, target);
355 gxps_link_target_free (target);
357 gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
358 gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
359 EV_DOCUMENT_LINKS_COLUMN_MARKUP, title,
360 EV_DOCUMENT_LINKS_COLUMN_LINK, link,
361 EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
363 g_object_unref (link);
366 if (gxps_outline_iter_children (&child_iter, iter))
367 build_tree (xps_document, model, &tree_iter, &child_iter);
368 } while (gxps_outline_iter_next (iter));
371 static GtkTreeModel *
372 xps_document_links_get_links_model (EvDocumentLinks *document_links)
374 XPSDocument *xps_document = XPS_DOCUMENT (document_links);
375 GXPSDocumentStructure *structure;
376 GXPSOutlineIter iter;
377 GtkTreeModel *model = NULL;
379 structure = gxps_document_get_structure (xps_document->doc);
383 if (gxps_document_structure_outline_iter_init (&iter, structure)) {
384 model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
389 build_tree (xps_document, model, NULL, &iter);
392 g_object_unref (structure);
397 static EvMappingList *
398 xps_document_links_get_links (EvDocumentLinks *document_links,
401 XPSDocument *xps_document = XPS_DOCUMENT (document_links);
403 GList *retval = NULL;
407 xps_page = GXPS_PAGE (page->backend_page);
408 mapping_list = gxps_page_get_links (xps_page, NULL);
410 for (list = mapping_list; list; list = list->next) {
412 GXPSLinkTarget *target;
413 EvMapping *ev_link_mapping;
414 cairo_rectangle_t area;
416 xps_link = (GXPSLink *)list->data;
417 ev_link_mapping = g_new (EvMapping, 1);
418 gxps_link_get_area (xps_link, &area);
419 target = gxps_link_get_target (xps_link);
420 gxps_link_get_area (xps_link, &area);
421 ev_link_mapping->data = ev_link_from_target (xps_document, target);
423 ev_link_mapping->area.x1 = area.x;
424 ev_link_mapping->area.x2 = area.x + area.width;
425 ev_link_mapping->area.y1 = area.y;
426 ev_link_mapping->area.y2 = area.y + area.height;
428 retval = g_list_prepend (retval, ev_link_mapping);
429 gxps_link_free (xps_link);
432 g_list_free (mapping_list);
434 return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
438 xps_document_links_find_link_dest (EvDocumentLinks *document_links,
439 const gchar *link_name)
441 XPSDocument *xps_document = XPS_DOCUMENT (document_links);
444 cairo_rectangle_t area;
445 EvLinkDest *dest = NULL;
447 page = gxps_document_get_page_for_anchor (xps_document->doc, link_name);
451 xps_page = gxps_document_get_page (xps_document->doc, page, NULL);
455 if (gxps_page_get_anchor_destination (xps_page, link_name, &area, NULL))
456 dest = ev_link_dest_new_xyz (page, area.x, area.y, 1., TRUE, TRUE, FALSE);
458 g_object_unref (xps_page);
464 xps_document_links_find_link_page (EvDocumentLinks *document_links,
465 const gchar *link_name)
467 XPSDocument *xps_document = XPS_DOCUMENT (document_links);
469 return gxps_document_get_page_for_anchor (xps_document->doc, link_name);
473 xps_document_document_links_iface_init (EvDocumentLinksInterface *iface)
475 iface->has_document_links = xps_document_links_has_document_links;
476 iface->get_links_model = xps_document_links_get_links_model;
477 iface->get_links = xps_document_links_get_links;
478 iface->find_link_dest = xps_document_links_find_link_dest;
479 iface->find_link_page = xps_document_links_find_link_page;