summaryrefslogtreecommitdiff
path: root/source/svg/svg-doc.c
blob: f08cceb2c3712aef6d946593ed638eaf99e742d3 (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
#include "mupdf/svg.h"

typedef struct svg_page_s svg_page;

struct svg_page_s
{
	fz_page super;
	svg_document *doc;
};

static void
svg_close_document(fz_context *ctx, fz_document *doc_)
{
	svg_document *doc = (svg_document*)doc_;
	fz_drop_tree(ctx, doc->idmap, NULL);
	fz_drop_xml(ctx, doc->root);
	fz_free(ctx, doc);
}

static int
svg_count_pages(fz_context *ctx, fz_document *doc_)
{
	return 1;
}

static fz_rect *
svg_bound_page(fz_context *ctx, fz_page *page_, fz_rect *rect)
{
	svg_page *page = (svg_page*)page_;
	svg_document *doc = page->doc;

	svg_parse_document_bounds(ctx, doc, doc->root);

	rect->x0 = 0;
	rect->y0 = 0;
	rect->x1 = doc->width;
	rect->y1 = doc->height;
	return rect;
}

static void
svg_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie)
{
	svg_page *page = (svg_page*)page_;
	svg_document *doc = page->doc;
	svg_run_document(ctx, doc, doc->root, dev, ctm);
}

static void
svg_drop_page_imp(fz_context *ctx, fz_page *page_)
{
	/* nothing */
}

static fz_page *
svg_load_page(fz_context *ctx, fz_document *doc_, int number)
{
	svg_document *doc = (svg_document*)doc_;
	svg_page *page;

	if (number != 0)
		return NULL;

	page = fz_new_page(ctx, sizeof *page);
	page->super.bound_page = svg_bound_page;
	page->super.run_page_contents = svg_run_page;
	page->super.drop_page_imp = svg_drop_page_imp;
	page->doc = doc;

	return (fz_page*)page;
}

static void
svg_build_id_map(fz_context *ctx, svg_document *doc, fz_xml *root)
{
	fz_xml *node;

	char *id_att = fz_xml_att(root, "id");
	if (id_att)
		doc->idmap = fz_tree_insert(ctx, doc->idmap, id_att, root);

	for (node = fz_xml_down(root); node; node = fz_xml_next(node))
		svg_build_id_map(ctx, doc, node);
}

static fz_document *
svg_open_document_with_stream(fz_context *ctx, fz_stream *file)
{
	svg_document *doc;
	fz_buffer *buf;
	fz_xml *root;

	buf = fz_read_all(ctx, file, 0);
	root = fz_parse_xml(ctx, buf->data, buf->len, 0);
	fz_drop_buffer(ctx, buf);

	doc = fz_malloc_struct(ctx, svg_document);
	doc->super.close = svg_close_document;
	doc->super.count_pages = svg_count_pages;
	doc->super.load_page = svg_load_page;

	doc->root = root;
	doc->idmap = NULL;

	svg_build_id_map(ctx, doc, root);

	return (fz_document*)doc;
}

static fz_document *
svg_open_document(fz_context *ctx, const char *filename)
{
	fz_stream *file;
	fz_document *doc;

	file = fz_open_file(ctx, filename);
	fz_try(ctx)
		doc = svg_open_document_with_stream(ctx, file);
	fz_always(ctx)
		fz_drop_stream(ctx, file);
	fz_catch(ctx)
		fz_rethrow(ctx);

	return doc;
}

static int
svg_recognize(fz_context *doc, const char *magic)
{
	char *ext = strrchr(magic, '.');
	if (ext && !fz_strcasecmp(ext, ".svg"))
		return 100;
	if (!strcmp(magic, "svg") || !strcmp(magic, "image/svg+xml"))
		return 100;
	return 0;
}

fz_document_handler svg_document_handler =
{
	&svg_recognize,
	&svg_open_document,
	&svg_open_document_with_stream
};