summaryrefslogtreecommitdiff
path: root/xps/xpstile.c
blob: db6e665587eb68966faff48fb0a9e46fd0d4a4b1 (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
#include "fitz.h"
#include "muxps.h"

/*
 * Parse a tiling brush (visual and image brushes at this time) common
 * properties. Use the callback to draw the individual tiles.
 */

enum { TILE_NONE, TILE_TILE, TILE_FLIP_X, TILE_FLIP_Y, TILE_FLIP_X_Y };

struct closure
{
	char *base_uri;
	xps_resource *dict;
	xps_item *root;
	void *user;
	void (*func)(xps_context*, fz_matrix, char*, xps_resource*, xps_item*, void*);
};

static void
xps_paint_tiling_brush_clipped(xps_context *ctx, fz_matrix ctm, fz_rect viewbox, struct closure *c)
{
	ctx->path = fz_newpath();
	fz_moveto(ctx->path, viewbox.x0, viewbox.y0);
	fz_lineto(ctx->path, viewbox.x0, viewbox.y1);
	fz_lineto(ctx->path, viewbox.x1, viewbox.y1);
	fz_lineto(ctx->path, viewbox.x1, viewbox.y0);
	fz_closepath(ctx->path);
	xps_clip(ctx, ctm);

	c->func(ctx, ctm, c->base_uri, c->dict, c->root, c->user);

	ctx->dev->popclip(ctx->dev->user);
}

static void
xps_paint_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect viewbox, int tile_mode, struct closure *c)
{
	fz_matrix ttm;

	xps_paint_tiling_brush_clipped(ctx, ctm, viewbox, c);

	if (tile_mode == TILE_FLIP_X || tile_mode == TILE_FLIP_X_Y)
	{
		ttm = fz_concat(fz_translate(viewbox.x1 * 2, 0), ctm);
		ttm = fz_concat(fz_scale(-1, 1), ttm);
		xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c);
	}

	if (tile_mode == TILE_FLIP_Y || tile_mode == TILE_FLIP_X_Y)
	{
		ttm = fz_concat(fz_translate(0, viewbox.y1 * 2), ctm);
		ttm = fz_concat(fz_scale(1, -1), ttm);
		xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c);
	}

	if (tile_mode == TILE_FLIP_X_Y)
	{
		ttm = fz_concat(fz_translate(viewbox.x1 * 2, viewbox.y1 * 2), ctm);
		ttm = fz_concat(fz_scale(-1, -1), ttm);
		xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c);
	}
}

void
xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm,
	char *base_uri, xps_resource *dict, xps_item *root,
	void (*func)(xps_context*, fz_matrix, char*, xps_resource*, xps_item*, void*), void *user)
{
	xps_item *node;
	struct closure c;

	char *opacity_att;
	char *transform_att;
	char *viewbox_att;
	char *viewport_att;
	char *tile_mode_att;
	char *viewbox_units_att;
	char *viewport_units_att;

	xps_item *transform_tag = NULL;

	fz_matrix transform;
	fz_rect viewbox;
	fz_rect viewport;
	float scalex, scaley;
	int tile_mode;

	opacity_att = xps_att(root, "Opacity");
	transform_att = xps_att(root, "Transform");
	viewbox_att = xps_att(root, "Viewbox");
	viewport_att = xps_att(root, "Viewport");
	tile_mode_att = xps_att(root, "TileMode");
	viewbox_units_att = xps_att(root, "ViewboxUnits");
	viewport_units_att = xps_att(root, "ViewportUnits");

	c.base_uri = base_uri;
	c.dict = dict;
	c.root = root;
	c.user = user;
	c.func = func;

	for (node = xps_down(root); node; node = xps_next(node))
	{
		if (!strcmp(xps_tag(node), "ImageBrush.Transform"))
			transform_tag = xps_down(node);
		if (!strcmp(xps_tag(node), "VisualBrush.Transform"))
			transform_tag = xps_down(node);
	}

	xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL);

	transform = fz_identity;
	if (transform_att)
		xps_parse_render_transform(ctx, transform_att, &transform);
	if (transform_tag)
		xps_parse_matrix_transform(ctx, transform_tag, &transform);

	viewbox = fz_unitrect;
	if (viewbox_att)
		xps_parse_rectangle(ctx, viewbox_att, &viewbox);

	viewport = fz_unitrect;
	if (viewport_att)
		xps_parse_rectangle(ctx, viewport_att, &viewport);

	/* some sanity checks on the viewport/viewbox size */
	if (fabs(viewport.x1 - viewport.x0) < 0.01) return;
	if (fabs(viewport.y1 - viewport.y0) < 0.01) return;
	if (fabs(viewbox.x1 - viewbox.x0) < 0.01) return;
	if (fabs(viewbox.y1 - viewbox.y0) < 0.01) return;

	scalex = (viewport.x1 - viewport.x0) / (viewbox.x1 - viewbox.x0);
	scaley = (viewport.y1 - viewport.y0) / (viewbox.y1 - viewbox.y0);

	tile_mode = TILE_NONE;
	if (tile_mode_att)
	{
		if (!strcmp(tile_mode_att, "None"))
			tile_mode = TILE_NONE;
		if (!strcmp(tile_mode_att, "Tile"))
			tile_mode = TILE_TILE;
		if (!strcmp(tile_mode_att, "FlipX"))
			tile_mode = TILE_FLIP_X;
		if (!strcmp(tile_mode_att, "FlipY"))
			tile_mode = TILE_FLIP_Y;
		if (!strcmp(tile_mode_att, "FlipXY"))
			tile_mode = TILE_FLIP_X_Y;
	}

	xps_clip(ctx, ctm);

	xps_begin_opacity(ctx, ctm, base_uri, dict, opacity_att, NULL);

	ctm = fz_concat(transform, ctm);
	ctm = fz_concat(fz_translate(viewport.x0, viewport.y0), ctm);
	ctm = fz_concat(fz_scale(scalex, scaley), ctm);
	ctm = fz_concat(fz_translate(-viewbox.x0, -viewbox.y0), ctm);

	if (tile_mode != TILE_NONE)
	{
		float w = viewbox.x1 - viewbox.x0;
		float h = viewbox.y1 - viewbox.y0;
		fz_matrix ttm;
		int x, y;

		/* TODO: loop in visible area */
		for (y = 0; y < 2; y++)
		{
			for (x = 0; x < 2; x++)
			{
				ttm = fz_concat(fz_translate(w*x, h*y), ctm);
				xps_paint_tiling_brush(ctx, ttm, viewbox, tile_mode, &c);
			}
		}
	}
	else
	{
		xps_paint_tiling_brush(ctx, ctm, viewbox, tile_mode, &c);
	}

	xps_end_opacity(ctx, base_uri, dict, opacity_att, NULL);

	ctx->dev->popclip(ctx->dev->user);
}