summaryrefslogtreecommitdiff
path: root/backend/xps/xps-document.c
blob: e7e9ef2b6146df85526759b6b9b95f45e3f4d89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
/* this file is part of atril, a mate document viewer
 *
 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
 *               2012 Leandro Vital <leandro.vital@yahoo.com.br>
 *
 * Atril 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 2 of the License, or
 * (at your option) any later version.
 *
 * Atril 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include <glib/gi18n-lib.h>
#include <libgxps/gxps.h>

#include "xps-document.h"
#include "ev-document-links.h"
#include "ev-document-misc.h"
#include "ev-document-thumbnails.h"
#include "ev-document-print.h"

struct _XPSDocument {
	EvDocument    object;

	GFile        *file;
	GXPSFile     *xps;
	GXPSDocument *doc;
};

struct _XPSDocumentClass {
	EvDocumentClass parent_class;
};

static void xps_document_document_links_iface_init (EvDocumentLinksInterface *iface);
static void xps_document_document_print_iface_init (EvDocumentPrintInterface *iface);
static void xps_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface);

EV_BACKEND_REGISTER_WITH_CODE (XPSDocument, xps_document,
	       {
		       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_LINKS,
						       xps_document_document_links_iface_init);
		       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_PRINT,
						       xps_document_document_print_iface_init);
		       EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
						       xps_document_document_thumbnails_iface_init);
	       })

/* XPSDocument */
static void
xps_document_init (XPSDocument *ps_document)
{
}

static void
xps_document_dispose (GObject *object)
{
	XPSDocument *xps = XPS_DOCUMENT (object);

	if (xps->file) {
		g_object_unref (xps->file);
		xps->file = NULL;
	}

	if (xps->xps) {
		g_object_unref (xps->xps);
		xps->xps = NULL;
	}

	if (xps->doc) {
		g_object_unref (xps->doc);
		xps->doc = NULL;
	}

	G_OBJECT_CLASS (xps_document_parent_class)->dispose (object);
}

/* EvDocumentIface */
static gboolean
xps_document_load (EvDocument *document,
		   const char *uri,
		   GError    **error)
{
	XPSDocument *xps = XPS_DOCUMENT (document);

	xps->file = g_file_new_for_uri (uri);
	xps->xps = gxps_file_new (xps->file, error);

	if (!xps->xps)
		return FALSE;

	/* FIXME: what if there are multiple docs? */
	xps->doc = gxps_file_get_document (xps->xps, 0, error);
	if (!xps->doc) {
		g_object_unref (xps->xps);
		xps->xps = NULL;

		return FALSE;
	}

	return TRUE;
}

static gboolean
xps_document_save (EvDocument *document,
		   const char *uri,
		   GError    **error)
{
	XPSDocument *xps = XPS_DOCUMENT (document);
	GFile       *dest;
	gboolean     retval;

	dest = g_file_new_for_uri (uri);
	retval = g_file_copy (xps->file, dest,
			      G_FILE_COPY_TARGET_DEFAULT_PERMS |
			      G_FILE_COPY_OVERWRITE,
			      NULL, NULL, NULL, error);
	g_object_unref (dest);

	return retval;
}

static gint
xps_document_get_n_pages (EvDocument *document)
{
	XPSDocument *xps = XPS_DOCUMENT (document);

	return gxps_document_get_n_pages (xps->doc);
}

static EvPage *
xps_document_get_page (EvDocument *document,
		       gint        index)
{
	XPSDocument *xps = XPS_DOCUMENT (document);
	GXPSPage    *xps_page;
	EvPage      *page;

	xps_page = gxps_document_get_page (xps->doc, index, NULL);
	page = ev_page_new (index);
	if (xps_page) {
		page->backend_page = (EvBackendPage)xps_page;
		page->backend_destroy_func = (EvBackendPageDestroyFunc)g_object_unref;
	}

	return page;
}

static void
xps_document_get_page_size (EvDocument *document,
			    EvPage     *page,
			    double     *width,
			    double     *height)
{
	gxps_page_get_size (GXPS_PAGE (page->backend_page), width, height);
}

static EvDocumentInfo *
xps_document_get_info (EvDocument *document)
{
	XPSDocument    *xps = XPS_DOCUMENT (document);
	EvDocumentInfo *info;

	info = g_new0 (EvDocumentInfo, 1);
	info->fields_mask =
		EV_DOCUMENT_INFO_N_PAGES |
		EV_DOCUMENT_INFO_PAPER_SIZE;

	if (gxps_document_get_n_pages (xps->doc) > 0) {
		ev_document_get_page_size (document, 0,
					   &(info->paper_width),
					   &(info->paper_height));
		info->paper_width  = info->paper_width / 96.0f * 25.4f;
		info->paper_height = info->paper_height / 96.0f * 25.4f;
	}

	info->n_pages = gxps_document_get_n_pages (xps->doc);

	return info;
}

static gboolean
xps_document_get_backend_info (EvDocument            *document,
			       EvDocumentBackendInfo *info)
{
	info->name = "libgxps";
	info->version = GXPS_VERSION_STRING;

	return TRUE;
}

static cairo_surface_t *
xps_document_render (EvDocument      *document,
		     EvRenderContext *rc)
{
	GXPSPage        *xps_page;
	gdouble          page_width, page_height;
	guint            width, height;
	cairo_surface_t *surface;
	cairo_t         *cr;
	GError          *error = NULL;

	xps_page = GXPS_PAGE (rc->page->backend_page);

	gxps_page_get_size (xps_page, &page_width, &page_height);
	if (rc->rotation == 90 || rc->rotation == 270) {
		width = (guint) ((page_height * rc->scale) + 0.5);
		height = (guint) ((page_width * rc->scale) + 0.5);
	} else {
		width = (guint) ((page_width * rc->scale) + 0.5);
		height = (guint) ((page_height * rc->scale) + 0.5);
	}

	surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
					      width, height);
	cr = cairo_create (surface);

	cairo_set_source_rgb (cr, 1., 1., 1.);
	cairo_paint (cr);

	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);
	gxps_page_render (xps_page, cr, &error);
	cairo_destroy (cr);

	if (error) {
		g_warning ("Error rendering page %d: %s\n",
		       rc->page->index, error->message);
		g_error_free (error);
	}

	return surface;
}

static void
xps_document_class_init (XPSDocumentClass *klass)
{
	GObjectClass    *object_class = G_OBJECT_CLASS (klass);
	EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);

	object_class->dispose = xps_document_dispose;

	ev_document_class->load = xps_document_load;
	ev_document_class->save = xps_document_save;
	ev_document_class->get_n_pages = xps_document_get_n_pages;
	ev_document_class->get_page = xps_document_get_page;
	ev_document_class->get_page_size = xps_document_get_page_size;
	ev_document_class->get_info = xps_document_get_info;
	ev_document_class->get_backend_info = xps_document_get_backend_info;
	ev_document_class->render = xps_document_render;
}

/* EvDocumentLinks */
static gboolean
xps_document_links_has_document_links (EvDocumentLinks *document_links)
{
	XPSDocument           *xps_document = XPS_DOCUMENT (document_links);
	GXPSDocumentStructure *structure;
	gboolean               retval;

	structure = gxps_document_get_structure (xps_document->doc);
	if (!structure)
		return FALSE;

	retval = gxps_document_structure_has_outline (structure);
	g_object_unref (structure);

	return retval;
}

static EvLink *
ev_link_from_target (XPSDocument    *xps_document,
		     GXPSLinkTarget *target)
{
	EvLinkAction *ev_action;

	if (gxps_link_target_is_internal (target)) {
		EvLinkDest  *dest = NULL;
		guint        doc;
		const gchar *anchor;

		anchor = gxps_link_target_get_anchor (target);

		/* FIXME: multidoc */
		doc = gxps_file_get_document_for_link_target (xps_document->xps, target);
		if (doc == 0) {
			if (!anchor)
				return NULL;

			dest = ev_link_dest_new_named (anchor);
			ev_action = ev_link_action_new_dest (dest);
		} else if (doc == -1 && anchor &&
			   gxps_document_get_page_for_anchor (xps_document->doc, anchor) >= 0) {
			/* Internal, but source is not a doc,
			 * let's try with doc = 0
			 */
			dest = ev_link_dest_new_named (anchor);
			ev_action = ev_link_action_new_dest (dest);
		} else {
			gchar *filename;

			/* FIXME: remote uri? */
			filename = g_file_get_path (xps_document->file);

			if (anchor)
				dest = ev_link_dest_new_named (anchor);
			ev_action = ev_link_action_new_remote (dest, filename);
			g_free (filename);
		}
	} else {
		const gchar *uri;

		uri = gxps_link_target_get_uri (target);
		ev_action = ev_link_action_new_external_uri (uri);
	}

	return ev_link_new (NULL, ev_action);
}

static void
build_tree (XPSDocument     *xps_document,
	    GtkTreeModel    *model,
	    GtkTreeIter     *parent,
	    GXPSOutlineIter *iter)
{
	do {
		GtkTreeIter     tree_iter;
		GXPSOutlineIter child_iter;
		EvLink         *link;
		GXPSLinkTarget *target;
		gchar          *title;

		target = gxps_outline_iter_get_target (iter);
		title = g_markup_escape_text (gxps_outline_iter_get_description (iter), -1);
		link = ev_link_from_target (xps_document, target);
		gxps_link_target_free (target);

		gtk_tree_store_append (GTK_TREE_STORE (model), &tree_iter, parent);
		gtk_tree_store_set (GTK_TREE_STORE (model), &tree_iter,
				    EV_DOCUMENT_LINKS_COLUMN_MARKUP, title,
				    EV_DOCUMENT_LINKS_COLUMN_LINK, link,
				    EV_DOCUMENT_LINKS_COLUMN_EXPAND, FALSE,
				    -1);
		g_object_unref (link);
		g_free (title);

		if (gxps_outline_iter_children (&child_iter, iter))
			build_tree (xps_document, model, &tree_iter, &child_iter);
	} while (gxps_outline_iter_next (iter));
}

static GtkTreeModel *
xps_document_links_get_links_model (EvDocumentLinks *document_links)
{
	XPSDocument           *xps_document = XPS_DOCUMENT (document_links);
	GXPSDocumentStructure *structure;
	GXPSOutlineIter        iter;
	GtkTreeModel          *model = NULL;

	structure = gxps_document_get_structure (xps_document->doc);
	if (!structure)
		return NULL;

	if (gxps_document_structure_outline_iter_init (&iter, structure)) {
		model = (GtkTreeModel *) gtk_tree_store_new (EV_DOCUMENT_LINKS_COLUMN_NUM_COLUMNS,
							     G_TYPE_STRING,
							     G_TYPE_OBJECT,
							     G_TYPE_BOOLEAN,
							     G_TYPE_STRING);
		build_tree (xps_document, model, NULL, &iter);
	}

	g_object_unref (structure);

	return model;
}

static EvMappingList *
xps_document_links_get_links (EvDocumentLinks *document_links,
			      EvPage          *page)
{
	XPSDocument *xps_document = XPS_DOCUMENT (document_links);
	GXPSPage    *xps_page;
	GList       *retval = NULL;
	GList       *mapping_list;
	GList       *list;

	xps_page = GXPS_PAGE (page->backend_page);
	mapping_list = gxps_page_get_links (xps_page, NULL);

	for (list = mapping_list; list; list = list->next) {
		GXPSLink *xps_link;
		GXPSLinkTarget *target;
		EvMapping *ev_link_mapping;
		cairo_rectangle_t area;

		xps_link = (GXPSLink *)list->data;
		ev_link_mapping = g_new (EvMapping, 1);
		gxps_link_get_area (xps_link, &area);
		target = gxps_link_get_target (xps_link);
		ev_link_mapping->data = ev_link_from_target (xps_document, target);

		ev_link_mapping->area.x1 = area.x;
		ev_link_mapping->area.x2 = area.x + area.width;
		ev_link_mapping->area.y1 = area.y;
		ev_link_mapping->area.y2 = area.y + area.height;

		retval = g_list_prepend (retval, ev_link_mapping);
		gxps_link_free (xps_link);
	}

	g_list_free (mapping_list);

	return ev_mapping_list_new (page->index, g_list_reverse (retval), (GDestroyNotify)g_object_unref);
}

static EvLinkDest *
xps_document_links_find_link_dest (EvDocumentLinks *document_links, const gchar *link_name)
{
	XPSDocument       *xps_document = XPS_DOCUMENT (document_links);
	GXPSPage          *xps_page;
	gint               page;
	cairo_rectangle_t  area;
	EvLinkDest        *dest = NULL;

	page = gxps_document_get_page_for_anchor (xps_document->doc, link_name);
	if (page == -1)
		return NULL;

	xps_page = gxps_document_get_page (xps_document->doc, page, NULL);
	if (!xps_page)
		return NULL;

	if (gxps_page_get_anchor_destination (xps_page, link_name, &area, NULL))
		dest = ev_link_dest_new_xyz (page, area.x, area.y, 1., TRUE, TRUE, FALSE);

	g_object_unref (xps_page);

	return dest;
}

static gint
xps_document_links_find_link_page (EvDocumentLinks *document_links,
				   const gchar     *link_name)
{
	XPSDocument *xps_document = XPS_DOCUMENT (document_links);

	return gxps_document_get_page_for_anchor (xps_document->doc, link_name);
}

static void
xps_document_document_links_iface_init (EvDocumentLinksInterface *iface)
{
	iface->has_document_links = xps_document_links_has_document_links;
	iface->get_links_model = xps_document_links_get_links_model;
	iface->get_links = xps_document_links_get_links;
	iface->find_link_dest = xps_document_links_find_link_dest;
	iface->find_link_page = xps_document_links_find_link_page;
}

/* EvDocumentPrint */
static void
xps_document_print_print_page (EvDocumentPrint *document,
			       EvPage          *page,
			       cairo_t         *cr)
{
	GError *error = NULL;

	gxps_page_render (GXPS_PAGE (page->backend_page), cr, &error);
	if (error) {
		g_warning ("Error rendering page %d for printing: %s\n",
			   page->index, error->message);
		g_error_free (error);
	}
}

static void
xps_document_document_print_iface_init (EvDocumentPrintInterface *iface)
{
	iface->print_page = xps_document_print_print_page;
}

static GdkPixbuf *
xps_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
                                       EvRenderContext      *rc,
                                       gboolean              border)
{
	GdkPixbuf *thumbnail;
	cairo_surface_t *surface;

	surface = xps_document_render (EV_DOCUMENT (document), rc);

	thumbnail = ev_document_misc_pixbuf_from_surface (surface);

	cairo_surface_destroy (surface);

	if (border) {
		GdkPixbuf *tmp_pixbuf = thumbnail;

		thumbnail = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
		g_object_unref (tmp_pixbuf);
	}

	return thumbnail;
}

static void
xps_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
                                        EvRenderContext      *rc,
                                        gint                 *width,
                                        gint                 *height)
{
	gdouble page_width, page_height;

	xps_document_get_page_size (EV_DOCUMENT (document), rc->page,
	                            &page_width, &page_height);

	if (rc->rotation == 90 || rc->rotation == 270) {
		*width = (gint) (page_height * rc->scale);
		*height = (gint) (page_width * rc->scale);
	} else {
		*width = (gint) (page_width * rc->scale);
		*height = (gint) (page_height * rc->scale);
	}
}

static void
xps_document_document_thumbnails_iface_init (EvDocumentThumbnailsInterface *iface)
{
	iface->get_thumbnail = xps_document_thumbnails_get_thumbnail;
	iface->get_dimensions = xps_document_thumbnails_get_dimensions;
}