summaryrefslogtreecommitdiff
path: root/rsvg-art-paint-server.c
blob: 7a2f5687de2ccbbe94538967fc8dbcad0bc76e19 (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
/* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* 
   rsvg-art-paint-server.c: Implement the SVG paint servers using libart
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program 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
   Library General Public License for more details.
  
   You should have received a copy of the GNU Library General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Raph Levien <raph@artofcode.com>
*/

#include "config.h"
#include "rsvg-private.h"
#include "rsvg-defs.h"
#include "rsvg-art-paint-server.h"
#include "rsvg-styles.h"
#include "rsvg-image.h"
#include "rsvg-art-render.h"

#include <glib/gmem.h>
#include <glib/gmessages.h>
#include <glib/gstrfuncs.h>
#include <libart_lgpl/art_affine.h>
#include <libart_lgpl/art_render_mask.h>
#include <libart_lgpl/art_rgba.h>
#include <libart_lgpl/art_render.h>
#include <string.h>
#include <math.h>

static ArtGradientStop *
rsvg_paint_art_stops_from_rsvg (RsvgGradientStops *rstops, 
								guint32 current_color)
{
	ArtGradientStop *stops;
	int n_stop = rstops->n_stop;
	int i;
	
	stops = g_new (ArtGradientStop, n_stop);
	for (i = 0; i < n_stop; i++)
		{
			guint32 rgba;
			guint32 r, g, b, a;
			
			stops[i].offset = rstops->stop[i].offset;
			if (!rstops->stop[i].is_current_color)
				rgba = rstops->stop[i].rgba;
			else
				rgba = current_color << 8;
			/* convert from separated to premultiplied alpha */
			a = rstops->stop[i].rgba & 0xff;
			r = (rgba >> 24) * a + 0x80;
			r = (r + (r >> 8)) >> 8;
			g = ((rgba >> 16) & 0xff) * a + 0x80;
			g = (g + (g >> 8)) >> 8;
			b = ((rgba >> 8) & 0xff) * a + 0x80;
			b = (b + (b >> 8)) >> 8;
			stops[i].color[0] = ART_PIX_MAX_FROM_8(r);
			stops[i].color[1] = ART_PIX_MAX_FROM_8(g);
			stops[i].color[2] = ART_PIX_MAX_FROM_8(b);
			stops[i].color[3] = ART_PIX_MAX_FROM_8(a);
		}
	return stops;
}


static void
rsvg_art_paint_server_solid_render (RsvgSolidColour *z, ArtRender *ar,
									const RsvgPSCtx *ctx)
{
	ArtPixMaxDepth color[3];
	guint32 rgb = z->rgb;
	if (z->currentcolour)
		rgb = rsvg_state_current(ctx->ctx)->current_color;
	
	color[0] = ART_PIX_MAX_FROM_8 (rgb >> 16);
	color[1] = ART_PIX_MAX_FROM_8 ((rgb >> 8) & 0xff);
	color[2] = ART_PIX_MAX_FROM_8 (rgb  & 0xff);
	
	art_render_image_solid (ar, color);
}

static void
rsvg_art_paint_server_lin_grad_render (RsvgLinearGradient *rlg, ArtRender *ar,
									   const RsvgPSCtx *ctx)
{
	ArtGradientLinear *agl;
	double x1, y1, x2, y2;
	double dx, dy, scale;
	double affine[6];
	guint32 current_color;
	int i;
	double xchange, ychange, pointlen,unitlen;
	double cx, cy, cxt, cyt;
	double px, py, pxt, pyt;
	double x2t, y2t;

	if (rlg->has_current_color)
		current_color = rlg->current_color;
	else
		current_color = ctx->color;
	if (rlg->stops->n_stop == 0)
		{
			return;
		}

	agl = g_new (ArtGradientLinear, 1);
	agl->n_stops = rlg->stops->n_stop;
	agl->stops = rsvg_paint_art_stops_from_rsvg (rlg->stops, current_color);
	
	if (rlg->obj_bbox) {
		affine[0] = ctx->x1 - ctx->x0;
		affine[1] = 0.;		
		affine[2] = 0.;
		affine[3] = ctx->y1 - ctx->y0;
		affine[4] = ctx->x0;
		affine[5] = ctx->y0;
		_rsvg_affine_multiply(affine, affine, ctx->affine);
	} else {
		for (i = 0; i < 6; i++)
			affine[i] = ctx->affine[i];
	}

	_rsvg_affine_multiply(affine, rlg->affine, affine);

	/*
	in case I am hit by a bus, here is how the following code works:

	in the spec, the various transformations are not applied to the coordinates
	that determine the gradient, they are applied to the gradient itself, which
	is logical. However after transformation, these things become different.
	which is where this code comes in. The effective gradient is two things:
	the slope of the lines of the same colour (perpendicular to the gradient 
	without transform), and the distance between the first and the last of 
	these strips. What this code figures out, is the slope of the lines of
	equal colour, and the distance between them. It transforms them both and 
	finally spits out a new two point gradient which is basically the original
	(x1, y1) point, and the second point which is the point on the line where
	(x2, y2) lay that is closest to (x1, y1)

	I'm not sure if this is the right solution to the problem, but it works
	for now.

	***Start explained section***/

	/*calculate (nx2, ny2), the point perpendicular to the gradient*/
	cx = (rlg->x2 + rlg->x1) / 2;
	cy = (rlg->y2 + rlg->y1) / 2;
	xchange = cx - rlg->x1;
	ychange = cy - rlg->y1;
	px = cx - ychange;
	py = cy + xchange;

	/* compute [xy][12] in pixel space */
	cxt = cx * affine[0] + cy * affine[2] + affine[4];
	cyt = cx * affine[1] + cy * affine[3] + affine[5];
	x2t = rlg->x2 * affine[0] + rlg->y2 * affine[2] + affine[4];
	y2t = rlg->x2 * affine[1] + rlg->y2 * affine[3] + affine[5];
	pxt = px * affine[0] + py * affine[2] + affine[4];
	pyt = px * affine[1] + py * affine[3] + affine[5];

	pointlen = abs((pxt - cxt)*(cyt - y2t)  - (cxt - x2t)*(pyt - cyt)) / 
		sqrt((pxt - cxt) * (pxt - cxt) + (pyt - cyt) * (pyt - cyt));

	xchange = pxt - cxt;
	ychange = pyt - cyt;
	unitlen = sqrt(xchange*xchange + ychange*ychange);

	if (unitlen == 0) {
		x2 = x1 = cxt; 
		y2 = y1 = cyt;
	} else {
		x1 = cxt - ychange / unitlen * pointlen;
		y1 = cyt + xchange / unitlen * pointlen;
		x2 = cxt + ychange / unitlen * pointlen;
		y2 = cyt - xchange / unitlen * pointlen;
	}

	/***end explained section***/

	/* solve a, b, c so ax1 + by1 + c = 0 and ax2 + by2 + c = 1, maximum
	   gradient is in x1,y1 to x2,y2 dir */
	dx = x2 - x1;
	dy = y2 - y1;

	/* workaround for an evil devide by 0 bug - not sure if this is sufficient */

	if (fabs(dx) + fabs(dy) <= 0.0000001)
		scale = 100000000.;
	else
		scale = 1.0 / (dx * dx + dy * dy);
	agl->a = dx * scale;
	agl->b = dy * scale;
	agl->c = -(x1 * agl->a + y1 * agl->b);
	
	agl->spread = rlg->spread;

	art_render_gradient_linear (ar, agl, ART_FILTER_NEAREST);

	g_free (agl->stops);
	g_free (agl);
}

static void
rsvg_art_paint_server_rad_grad_render (RsvgRadialGradient *rrg, ArtRender *ar,
									   const RsvgPSCtx *ctx)
{
	ArtGradientRadial *agr;
	double aff1[6], aff2[6], affine[6];
	guint32 current_color;
	int i;

	if (rrg->obj_bbox) {
		affine[0] = ctx->x1 - ctx->x0;
		affine[1] = 0.;		
		affine[2] = 0.;
		affine[3] = ctx->y1 - ctx->y0;
		affine[4] = ctx->x0;
		affine[5] = ctx->y0;
		_rsvg_affine_multiply(affine, affine, ctx->affine);
	} else {
		for (i = 0; i < 6; i++)
			affine[i] = ctx->affine[i];
	}

	_rsvg_affine_multiply(affine, rrg->affine, affine);

	if (rrg->has_current_color)
		current_color = rrg->current_color;
	else
		current_color = ctx->color;
	if (rrg->stops->n_stop == 0)
		{
			return;
		}
	agr = g_new (ArtGradientRadial, 1);
	agr->n_stops = rrg->stops->n_stop;
	agr->stops = rsvg_paint_art_stops_from_rsvg (rrg->stops, current_color);
	
	_rsvg_affine_scale (aff1, rrg->r, rrg->r);
	_rsvg_affine_translate (aff2, rrg->cx, rrg->cy);
	_rsvg_affine_multiply (aff1, aff1, aff2);
	_rsvg_affine_multiply (aff1, aff1, affine);
	_rsvg_affine_invert (agr->affine, aff1);
	
	/* todo: libart doesn't support spreads on radial gradients */

	agr->fx = (rrg->fx - rrg->cx) / rrg->r;
	agr->fy = (rrg->fy - rrg->cy) / rrg->r;
	
	art_render_gradient_radial (ar, agr, ART_FILTER_NEAREST);

	g_free (agr->stops);
	g_free (agr);
}


typedef struct {
	ArtImageSource super;
	gchar * pixels;
	gdouble x, y, width, height, xoffset, yoffset;
	gint realwidth, realheight; 
	gint rowstride;
	art_boolean init;
	gdouble affine[6];
	gdouble invaffine[6];
} RsvgImageSourcePattern;

static void
render_image_pattern_done (ArtRenderCallback *self, ArtRender *render)
{
	RsvgImageSourcePattern *z;
	z = (RsvgImageSourcePattern *) self;
	g_free(z->pixels);
	g_free(self);
}

/*the commented out regions in the next bit allow overflow*/

static void
render_image_pattern_render(ArtRenderCallback *self, ArtRender *render,
							art_u8 *dest, int y)
{
	RsvgImageSourcePattern *z = (RsvgImageSourcePattern *)self;
	int i;
	int x0 = render->x0;
	int x1 = render->x1;

	int sx, sy;
	double px, py, gx, gy, gnx, gny, tx, ty;

	tx = -z->x * z->affine[0] + -z->y * z->affine[2] + z->affine[4];
	ty = -z->x * z->affine[1] + -z->y * z->affine[3] + z->affine[5];

	for (i = 0; i < x1 - x0; i++)
		{
			px = i;
			py = y;
			
			gx = px * z->invaffine[0] + py * z->invaffine[2] + z->invaffine[4] - z->x;
			gy = px * z->invaffine[1] + py * z->invaffine[3] + z->invaffine[5] - z->y;
			
			gnx = floor (gx / z->width);
			gny = floor (gy / z->height);
	
			sx = px - gnx * z->width * z->affine[0] - gny * z->height * z->affine[2] - z->affine[4]
				+ z->xoffset + tx;
			sy = py - gnx * z->width * z->affine[1] - gny * z->height * z->affine[3] - z->affine[5]
				+ z->yoffset + ty;
			
			if (sx < 0 || sx >= z->realwidth || sy < 0 || sy >= z->realheight){
				render->image_buf[i * 4 + 3] = 0;
				continue;
			}
			render->image_buf[i * 4 + 0] = z->pixels[sx * 4 + z->rowstride * sy];
			render->image_buf[i * 4 + 1] = z->pixels[sx * 4 + z->rowstride * sy + 1];
			render->image_buf[i * 4 + 2] = z->pixels[sx * 4 + z->rowstride * sy + 2];
			render->image_buf[i * 4 + 3] = z->pixels[sx * 4 + z->rowstride * sy + 3];
			
		}
}

static void
render_image_pattern_negotiate (ArtImageSource *self, ArtRender *render,
				  ArtImageSourceFlags *p_flags,
				  int *p_buf_depth, ArtAlphaType *p_alpha)
{
	self->super.render = render_image_pattern_render;
	*p_flags = 0;
	*p_buf_depth = 8;
	*p_alpha = ART_ALPHA_SEPARATE;
}

static void
render_image_pattern (ArtRender *render, guchar * pixels, gdouble x, gdouble y, 
					  gdouble width, gdouble height, gint realwidth, gint realheight, gint rowstride,
					  gdouble xoffset, gdouble yoffset, double * affine)
{	
	RsvgImageSourcePattern *image_source;
	int i;
	
	image_source = art_new (RsvgImageSourcePattern, 1);
	image_source->super.super.render = NULL;
	image_source->super.super.done = render_image_pattern_done;
	image_source->super.negotiate = render_image_pattern_negotiate;
	
	image_source->pixels = g_new(gchar, rowstride * realheight);
	
	image_source->rowstride = rowstride;
	image_source->width = width;
	image_source->height = height;
	image_source->realwidth = realwidth;
	image_source->realheight = realheight;
	image_source->x = x;
	image_source->y = y;
	image_source->xoffset = xoffset;
	image_source->yoffset = yoffset;
		
	for (i = 0; i < rowstride * realheight; i++)
		image_source->pixels[i] = pixels[i];  
	
	for (i = 0; i < 6; i++)
		image_source->affine[i] = affine[i];  

	_rsvg_affine_invert(image_source->invaffine, affine);

	image_source->init = ART_FALSE;
	
	art_render_add_image_source (render, &image_source->super);
}

static void
rsvg_art_paint_server_pattern_render (RsvgPattern *pattern, ArtRender *ar,
									  const RsvgPSCtx *ctx)
{
	RsvgNode *drawable = (RsvgNode *)pattern->g;
	RsvgDrawingCtx *hctx = ctx->ctx;
	GdkPixbuf *pixbuf = ((RsvgArtRender *)hctx->render)->pixbuf; 
	double affine[6];
	double caffine[6];
	int i, j;
	gdouble minx, miny, maxx, maxy, xcoord, ycoord, xoffset, yoffset;
	GdkPixbuf *save, *render;

	if (ctx->ctx == NULL)
		return;

	if (pattern->obj_bbox) {
		affine[0] = ctx->x1 - ctx->x0;
		affine[1] = 0.;		
		affine[2] = 0.;
		affine[3] = ctx->y1 - ctx->y0;
		affine[4] = ctx->x0;
		affine[5] = ctx->y0;
		_rsvg_affine_multiply(affine, affine, ctx->affine);
	} else {
		for (i = 0; i < 6; i++)
			affine[i] = ctx->affine[i];
	}

	if (pattern->vbox) {
		double w, h, x, y;
		w = pattern->width;
		h = pattern->height;
		x = 0;
		y = 0;

		rsvg_preserve_aspect_ratio(pattern->preserve_aspect_ratio,
								   pattern->vbw, pattern->vbh, 
								   &w, &h, &x, &y);

		x -= pattern->vbx * w / pattern->vbw;
		y -= pattern->vby * h / pattern->vbh;

		caffine[0] = w / pattern->vbw;
		caffine[1] = 0.;		
		caffine[2] = 0.;
		caffine[3] = h / pattern->vbh;
		caffine[4] = x;
		caffine[5] = y;
		_rsvg_affine_multiply(caffine, caffine, affine);
	}
	else if (pattern->obj_cbbox) {
		caffine[0] = ctx->x1 - ctx->x0;
		caffine[1] = 0.;		
		caffine[2] = 0.;
		caffine[3] = ctx->y1 - ctx->y0;
		caffine[4] = ctx->x0;
		caffine[5] = ctx->y0;
		_rsvg_affine_multiply(caffine, caffine, ctx->affine);
	} else {
		for (i = 0; i < 6; i++)
			caffine[i] = ctx->affine[i];
	}

	_rsvg_affine_multiply(affine, affine, pattern->affine);
	_rsvg_affine_multiply(caffine, caffine, pattern->affine);

	/*check if everything is going to be within the boundaries of the rendering surface*/
	maxx = maxy = minx = miny = xoffset = yoffset = 0;

	for (i = 0; i < 2; i++)
		for (j = 0; j < 2; j++)
			{
				xcoord = affine[0] * pattern->width * i + affine[2] * pattern->height * j + affine[4];
				ycoord = affine[1] * pattern->width * i + affine[3] * pattern->height * j + affine[5];
				if (xcoord < minx)
					minx = xcoord;
				if (xcoord > maxx)
					maxx = xcoord;
				if (ycoord < miny)
					miny = ycoord;
				if (ycoord > maxy)
					maxy = ycoord;
			}

	xoffset = -minx;
	yoffset = -miny;

	render = _rsvg_pixbuf_new_cleared(GDK_COLORSPACE_RGB, 1, 8, 
									  maxx - minx, maxy - miny);
	
	save = pixbuf;

	((RsvgArtRender *)hctx->render)->pixbuf = render;

	rsvg_state_push(ctx->ctx);
	
	caffine[4] += xoffset;
	caffine[5] += yoffset;

	for (i = 0; i < 6; i++)
		{
			rsvg_state_current(hctx)->personal_affine[i] = caffine[i];
			rsvg_state_current(hctx)->affine[i] = caffine[i];
		}

	if (((RsvgNodeGroup *)drawable)->children->len ||
		pattern->gfallback == NULL)
		rsvg_node_draw (drawable, hctx, 2);
	else
		rsvg_node_draw ((RsvgNode *)pattern->gfallback, hctx, 2);		

	rsvg_state_pop(ctx->ctx);

  	((RsvgArtRender *)hctx->render)->pixbuf = save;

	render_image_pattern (ar, gdk_pixbuf_get_pixels (render),
						  pattern->x, pattern->y, 
						  pattern->width, pattern->height, 
						  gdk_pixbuf_get_width (render),
						  gdk_pixbuf_get_height (render),
						  gdk_pixbuf_get_rowstride (render), 
						  xoffset, yoffset, affine);

	g_object_unref(G_OBJECT(render));
}

void
rsvg_art_render_paint_server (ArtRender *ar, RsvgPaintServer *ps,
							  const RsvgPSCtx *ctx)
{
	switch(ps->type)
		{
		case RSVG_PAINT_SERVER_LIN_GRAD:
			rsvg_art_paint_server_lin_grad_render(ps->core.lingrad, ar, ctx);
			break;
		case RSVG_PAINT_SERVER_RAD_GRAD:
			rsvg_art_paint_server_rad_grad_render(ps->core.radgrad, ar, ctx);
			break;
		case RSVG_PAINT_SERVER_SOLID:
			rsvg_art_paint_server_solid_render(ps->core.colour, ar, ctx);
			break;
		case RSVG_PAINT_SERVER_PATTERN:
			rsvg_art_paint_server_pattern_render(ps->core.pattern, ar, ctx);
			break;
		}
}