File: gxps-document.c

package info (click to toggle)
libgxps 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,756 kB
  • ctags: 1,513
  • sloc: ansic: 10,000; sh: 4,136; makefile: 349
file content (595 lines) | stat: -rw-r--r-- 14,941 bytes parent folder | download
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* GXPSDocument
 *
 * Copyright (C) 2010  Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>

#include "gxps-document.h"
#include "gxps-archive.h"
#include "gxps-links.h"
#include "gxps-private.h"
#include "gxps-error.h"

/**
 * SECTION:gxps-document
 * @Short_description: XPS Documents
 * @Title: GXPSDocument
 * @See_also: #GXPSFile, #GXPSPage, #GXPSDocumentStructure
 *
 * #GXPSDocument represents a document in a #GXPSFile. #GXPSDocument
 * objects can not be created directly, they are retrieved from a
 * #GXPSFile with gxps_file_get_document().
 */

enum {
	PROP_0,
	PROP_ARCHIVE,
	PROP_SOURCE
};

typedef struct _Page {
	gchar *source;
	gint   width;
	gint   height;
	GList *links;
} Page;

struct _GXPSDocumentPrivate {
	GXPSArchive *zip;
	gchar       *source;
	gboolean     has_rels;
	gchar       *structure;

	gboolean     initialized;
	GError      *init_error;

	Page       **pages;
	guint        n_pages;
};

static void initable_iface_init (GInitableIface *initable_iface);

G_DEFINE_TYPE_WITH_CODE (GXPSDocument, gxps_document, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init))

#define REL_DOCUMENT_STRUCTURE "http://schemas.microsoft.com/xps/2005/06/documentstructure"

static Page *
page_new (void)
{
	return g_slice_new0 (Page);
}

static void
page_free (Page *page)
{
	g_free (page->source);
	g_list_foreach (page->links, (GFunc)g_free, NULL);
	g_list_free (page->links);

	g_slice_free (Page, page);
}

/* FixedDoc parser */
typedef struct _FixedDocParserData {
	GXPSDocument *doc;
	Page         *page;
	guint         n_pages;
	GList        *pages;
} FixedDocParserData;

static void
fixed_doc_start_element (GMarkupParseContext  *context,
			 const gchar          *element_name,
			 const gchar         **names,
			 const gchar         **values,
			 gpointer              user_data,
			 GError              **error)
{
	FixedDocParserData *data = (FixedDocParserData *)user_data;
	gint                i;

	if (strcmp (element_name, "PageContent") == 0) {
		gchar  *source = NULL;
		gdouble width = -1, height = -1;

		for (i = 0; names[i]; i++) {
			if (strcmp (names[i], "Source") == 0) {
				source = gxps_resolve_relative_path (data->doc->priv->source,
								     values[i]);
			} else if (strcmp (names[i], "Width") == 0) {
				if (!gxps_value_get_double_positive (values[i], &width))
					width = 0;
			} else if (strcmp (names[i], "Height") == 0) {
				if (!gxps_value_get_double_positive (values[i], &height))
					height = 0;
			}
		}

		if (!source) {
			gxps_parse_error (context,
					  data->doc->priv->source,
					  G_MARKUP_ERROR_MISSING_ATTRIBUTE,
					  element_name, "Source", NULL, error);
			return;
		}

		data->page = page_new ();
		data->page->source = source;
		data->page->width = width;
		data->page->height = height;
	} else if (strcmp (element_name, "LinkTarget") == 0) {
		if (!data->page) {
			/* TODO: error */
			return;
		}

		for (i = 0; names[i]; i++) {
			if (strcmp (names[i], "Name") == 0) {
				data->page->links = g_list_prepend (data->page->links, g_strdup (values[i]));
			}
		}
	} else if (strcmp (element_name, "PageContent.LinkTargets") == 0) {
	} else if (strcmp (element_name, "FixedDocument") == 0) {
		/* Nothing to do */
	} else {
		gxps_parse_error (context,
				  data->doc->priv->source,
				  G_MARKUP_ERROR_UNKNOWN_ELEMENT,
				  element_name, NULL, NULL, error);
	}
}

static void
fixed_doc_end_element (GMarkupParseContext  *context,
		       const gchar          *element_name,
		       gpointer              user_data,
		       GError              **error)
{
	FixedDocParserData *data = (FixedDocParserData *)user_data;

	if (strcmp (element_name, "PageContent") == 0) {
		data->n_pages++;
		data->pages = g_list_prepend (data->pages, data->page);
		data->page = NULL;
	} else if (strcmp (element_name, "PageContent.LinkTargets") == 0) {
		if (!data->page) {
			/* TODO: error */
			return;
		}
		data->page->links = g_list_reverse (data->page->links);
	} else if (strcmp (element_name, "FixedDocument") == 0) {
		GList *l;

		data->doc->priv->n_pages = data->n_pages;
                if (data->doc->priv->n_pages > 0) {
                        data->doc->priv->pages = g_new (Page *, data->n_pages);

                        for (l = data->pages; l; l = g_list_next (l))
                                data->doc->priv->pages[--data->n_pages] = (Page *)l->data;
                }
                g_list_free (data->pages);
	} else if (strcmp (element_name, "LinkTarget") == 0) {
		/* Do Nothing */
	} else {
		gxps_parse_error (context,
				  data->doc->priv->source,
				  G_MARKUP_ERROR_UNKNOWN_ELEMENT,
				  element_name, NULL, NULL, error);
	}
}

static const GMarkupParser fixed_doc_parser = {
	fixed_doc_start_element,
	fixed_doc_end_element,
	NULL,
	NULL,
	NULL
};

static gboolean
gxps_document_parse_fixed_doc (GXPSDocument *doc,
			       GError      **error)
{
	GInputStream        *stream;
	GMarkupParseContext *ctx;
	FixedDocParserData  *parser_data;

	stream = gxps_archive_open (doc->priv->zip,
				    doc->priv->source);
	if (!stream) {
		g_set_error (error,
			     GXPS_ERROR,
			     GXPS_ERROR_SOURCE_NOT_FOUND,
			     "Document source %s not found in archive",
			     doc->priv->source);
		return FALSE;
	}

	parser_data = g_new0 (FixedDocParserData, 1);
	parser_data->doc = doc;

	ctx = g_markup_parse_context_new (&fixed_doc_parser, 0, parser_data, NULL);
	gxps_parse_stream (ctx, stream, error);
	g_object_unref (stream);

	g_free (parser_data);
	g_markup_parse_context_free (ctx);

	return (*error != NULL) ? FALSE : TRUE;
}

static void
doc_rels_start_element (GMarkupParseContext  *context,
			const gchar          *element_name,
			const gchar         **names,
			const gchar         **values,
			gpointer              user_data,
			GError              **error)
{
	GXPSDocument *doc = GXPS_DOCUMENT (user_data);

	if (strcmp (element_name, "Relationship") == 0) {
		const gchar *type = NULL;
		const gchar *target = NULL;
		gint         i;

		for (i = 0; names[i]; i++) {
			if (strcmp (names[i], "Type") == 0) {
				type = values[i];
			} else if (strcmp (names[i], "Target") == 0) {
				target = values[i];
			} else if (strcmp (names[i], "Id") == 0) {
				/* Ignore ids for now */
			}
		}

		if (g_strcmp0 (type, REL_DOCUMENT_STRUCTURE) == 0) {
			doc->priv->structure = target ? gxps_resolve_relative_path (doc->priv->source, target) : NULL;
		}
	}
}

static const GMarkupParser doc_rels_parser = {
	doc_rels_start_element,
	NULL,
	NULL,
	NULL,
	NULL
};

static gboolean
gxps_document_parse_rels (GXPSDocument *doc,
			  GError      **error)
{
	GInputStream        *stream;
	GMarkupParseContext *ctx;
	gchar               *filename;
	gchar               *rels, *doc_rels;
	gboolean             retval;

	if (!doc->priv->has_rels)
		return FALSE;

	filename = g_path_get_basename (doc->priv->source);
	rels = g_strconcat ("_rels/", filename, ".rels", NULL);
	doc_rels = gxps_resolve_relative_path (doc->priv->source, rels);
	g_free (filename);
	g_free (rels);

	stream = gxps_archive_open (doc->priv->zip, doc_rels);
	if (!stream) {
		doc->priv->has_rels = FALSE;
		g_free (doc_rels);

		return FALSE;
	}

	ctx = g_markup_parse_context_new (&doc_rels_parser, 0, doc, NULL);
	retval = gxps_parse_stream (ctx, stream, error);
	g_object_unref (stream);
	g_free (doc_rels);

	g_markup_parse_context_free (ctx);

	return retval;
}

static void
gxps_document_finalize (GObject *object)
{
	GXPSDocument *doc = GXPS_DOCUMENT (object);

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

	if (doc->priv->source) {
		g_free (doc->priv->source);
		doc->priv->source = NULL;
	}

	if (doc->priv->structure) {
		g_free (doc->priv->structure);
		doc->priv->structure = NULL;
	}

	if (doc->priv->pages) {
		gint i;

		for (i = 0; i < doc->priv->n_pages; i++)
			page_free (doc->priv->pages[i]);
		g_free (doc->priv->pages);
		doc->priv->pages = NULL;
	}

	g_clear_error (&doc->priv->init_error);

	G_OBJECT_CLASS (gxps_document_parent_class)->finalize (object);
}

static void
gxps_document_init (GXPSDocument *doc)
{
	doc->priv = G_TYPE_INSTANCE_GET_PRIVATE (doc,
						 GXPS_TYPE_DOCUMENT,
						 GXPSDocumentPrivate);
	doc->priv->has_rels = TRUE;
}

static void
gxps_document_set_property (GObject      *object,
			    guint         prop_id,
			    const GValue *value,
			    GParamSpec   *pspec)
{
	GXPSDocument *doc = GXPS_DOCUMENT (object);

	switch (prop_id) {
	case PROP_ARCHIVE:
		doc->priv->zip = g_value_dup_object (value);
		break;
	case PROP_SOURCE:
		doc->priv->source = g_value_dup_string (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}

static void
gxps_document_class_init (GXPSDocumentClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->set_property = gxps_document_set_property;
	object_class->finalize = gxps_document_finalize;

	g_object_class_install_property (object_class,
					 PROP_ARCHIVE,
					 g_param_spec_object ("archive",
							      "Archive",
							      "The document archive",
							      GXPS_TYPE_ARCHIVE,
							      G_PARAM_WRITABLE |
							      G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (object_class,
					 PROP_SOURCE,
					 g_param_spec_string ("source",
							      "Source",
							      "The Document Source File",
							      NULL,
							      G_PARAM_WRITABLE |
							      G_PARAM_CONSTRUCT_ONLY));

	g_type_class_add_private (klass, sizeof (GXPSDocumentPrivate));
}

static gboolean
gxps_document_initable_init (GInitable     *initable,
			     GCancellable  *cancellable,
			     GError       **error)
{
	GXPSDocument *doc = GXPS_DOCUMENT (initable);

	if (doc->priv->initialized) {
		if (doc->priv->init_error) {
			g_propagate_error (error, g_error_copy (doc->priv->init_error));

			return FALSE;
		}

		return TRUE;
	}

	doc->priv->initialized = TRUE;

	if (!gxps_document_parse_fixed_doc (doc, &doc->priv->init_error)) {
		g_propagate_error (error, g_error_copy (doc->priv->init_error));
		return FALSE;
	}

	return TRUE;
}

static void
initable_iface_init (GInitableIface *initable_iface)
{
	initable_iface->init = gxps_document_initable_init;
}

GXPSDocument *
_gxps_document_new (GXPSArchive *zip,
		    const gchar *source,
		    GError     **error)
{
	return g_initable_new (GXPS_TYPE_DOCUMENT,
			       NULL, error,
			       "archive", zip,
			       "source", source,
			       NULL);
}

/**
 * gxps_document_get_n_pages:
 * @doc: a #GXPSDocument
 *
 * Gets the number of pages in @doc.
 *
 * Returns: the number of pages.
 */
guint
gxps_document_get_n_pages (GXPSDocument *doc)
{
	g_return_val_if_fail (GXPS_IS_DOCUMENT (doc), 0);

	return doc->priv->n_pages;
}

/**
 * gxps_document_get_page:
 * @doc: a #GXPSDocument
 * @n_page: the index of the page to get
 * @error: #GError for error reporting, or %NULL to ignore
 *
 * Creates a new #GXPSPage representing the page at
 * index @n_doc in @doc document.
 *
 * Returns: (transfer full): a new #GXPSPage or %NULL on error.
 *     Free the returned object with g_object_unref().
 */
GXPSPage *
gxps_document_get_page (GXPSDocument *doc,
			guint         n_page,
			GError      **error)
{
	const gchar *source;

	g_return_val_if_fail (GXPS_IS_DOCUMENT (doc), NULL);
	g_return_val_if_fail (n_page < doc->priv->n_pages, NULL);

	source = doc->priv->pages[n_page]->source;
	g_assert (source != NULL);

	return _gxps_page_new (doc->priv->zip, source, error);
}

/**
 * gxps_document_get_page_size:
 * @doc: a #GXPSDocument
 * @n_page: the index of a page in @doc
 * @width: (out) (allow-none): return location for the width of @n_page
 * @height: (out) (allow-none): return location for the height of @n_page
 *
 * Gets the typical size of the page at index @n_page in @doc document.
 * This function is useful to get the advisory size of pages in a document
 * without creating #GXPSPage objects. This page size might be different than
 * the actual page size so page dimension might need to be updated once the
 * page is loaded. Advisory page sizes are not always available in @doc,
 * in which case this function returns %FALSE.
 * To get the authoritative size of a page you should use gxps_page_get_size()
 * instead.
 *
 * Returns: %TRUE if the page size information is available in @doc,
 *     %FALSE otherwise.
 */
gboolean
gxps_document_get_page_size (GXPSDocument *doc,
			     guint         n_page,
			     gdouble      *width,
			     gdouble      *height)
{
	Page *page;

	g_return_val_if_fail (GXPS_IS_DOCUMENT (doc), FALSE);
	g_return_val_if_fail (n_page < doc->priv->n_pages, FALSE);

	page = doc->priv->pages[n_page];
	if (page->width == 0 || page->height == 0)
		return FALSE;

	if (width)
		*width = page->width;
	if (height)
		*height = page->height;

	return TRUE;
}

/**
 * gxps_document_get_page_for_anchor:
 * @doc: a #GXPSDocument
 * @anchor: the name of an anchor
 *
 * Gets the index of the page in @doc where the given
 * anchor is.
 *
 * Returns: the page index of the given anchor.
 */
gint
gxps_document_get_page_for_anchor (GXPSDocument *doc,
				   const gchar  *anchor)
{
	guint i;

	g_return_val_if_fail (GXPS_IS_DOCUMENT (doc), -1);
	g_return_val_if_fail (anchor != NULL, -1);

	for (i = 0; i < doc->priv->n_pages; i++) {
		if (g_list_find_custom (doc->priv->pages[i]->links, anchor, (GCompareFunc)strcmp))
			return i;
	}

	return -1;
}

/**
 * gxps_document_get_structure:
 * @doc: a a #GXPSDocument
 *
 * Creates a new #GXPSDocumentStructure representing the document
 * structure of @doc.
 *
 * Returns: (transfer full): a new #GXPSDocumentStructure or %NULL if document doesn't have a structure.
 *     Free the returned object with g_object_unref().
 */
GXPSDocumentStructure *
gxps_document_get_structure (GXPSDocument *doc)
{
	g_return_val_if_fail (GXPS_IS_DOCUMENT (doc), NULL);

	if (!doc->priv->structure) {
		if (!gxps_document_parse_rels (doc, NULL))
			return NULL;
	}

	if (!doc->priv->structure)
		return NULL;

	if (!gxps_archive_has_entry (doc->priv->zip, doc->priv->structure))
		return NULL;

	return _gxps_document_structure_new (doc->priv->zip,
					     doc->priv->structure);
}