rsvg-shapes.c 55.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* vim: set sw=4: -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
   rsvg-shapes.c: Draw SVG shapes

   Copyright (C) 2000 Eazel, Inc.
   Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>

   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>
*/
25
#include <string.h>
Dom Lachowicz's avatar
Dom Lachowicz committed
26
#include <math.h>
27
#include <errno.h>
Caleb Michael Moore's avatar
markers  
Caleb Michael Moore committed
28
#include <stdio.h>
29

30
#include "rsvg-private.h"
31
#include "rsvg-styles.h"
32
#include "rsvg-shapes.h"
33 34 35
#include "rsvg-css.h"
#include "rsvg-bpath-util.h"
#include "rsvg-path.h"
36
#include "rsvg-defs.h"
Caleb Michael Moore's avatar
Caleb Michael Moore committed
37
#include "rsvg-filter.h"
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

#include <libart_lgpl/art_affine.h>
#include <libart_lgpl/art_vpath_bpath.h>
#include <libart_lgpl/art_render_svp.h>
#include <libart_lgpl/art_svp_vpath.h>
#include <libart_lgpl/art_svp_intersect.h>
#include <libart_lgpl/art_svp_vpath.h>
#include <libart_lgpl/art_rgb_affine.h>
#include <libart_lgpl/art_rgb_rgba_affine.h>

/* 4/3 * (1-cos 45ƒ)/sin 45ƒ = 4/3 * sqrt(2) - 1 */
#define RSVG_ARC_MAGIC ((double) 0.5522847498)

/**
 * rsvg_close_vpath: Close a vector path.
 * @src: Source vector path.
 *
 * Closes any open subpaths in the vector path.
 *
 * Return value: Closed vector path, allocated with g_new.
 **/
static ArtVpath *
rsvg_close_vpath (const ArtVpath *src)
{
	ArtVpath *result;
	int n_result, n_result_max;
	int src_ix;
	double beg_x, beg_y;
	gboolean open;
	
	n_result = 0;
	n_result_max = 16;
	result = g_new (ArtVpath, n_result_max);
	
	beg_x = 0;
	beg_y = 0;
	open = FALSE;
	
	for (src_ix = 0; src[src_ix].code != ART_END; src_ix++)
		{
			if (n_result == n_result_max)
				result = g_renew (ArtVpath, result, n_result_max <<= 1);
			result[n_result].code = src[src_ix].code == ART_MOVETO_OPEN ?
				ART_MOVETO : src[src_ix].code;
			result[n_result].x = src[src_ix].x;
			result[n_result].y = src[src_ix].y;
			n_result++;
			if (src[src_ix].code == ART_MOVETO_OPEN)
				{
					beg_x = src[src_ix].x;
					beg_y = src[src_ix].y;
					open = TRUE;
				}
			else if (src[src_ix + 1].code != ART_LINETO)
				{
					if (open && (beg_x != src[src_ix].x || beg_y != src[src_ix].y))
						{
							if (n_result == n_result_max)
								result = g_renew (ArtVpath, result, n_result_max <<= 1);
							result[n_result].code = ART_LINETO;
							result[n_result].x = beg_x;
							result[n_result].y = beg_y;
							n_result++;
						}
					open = FALSE;
				}
		}
	if (n_result == n_result_max)
		result = g_renew (ArtVpath, result, n_result_max <<= 1);
	result[n_result].code = ART_END;
	result[n_result].x = 0.0;
	result[n_result].y = 0.0;
	return result;
}

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
/* calculates how big an svp is */
static ArtIRect
rsvg_calculate_svp_bounds (const ArtSVP *svp)
{
	int i, j;	
	int bigx, littlex, bigy, littley, assignedonce;
	ArtIRect output;

	bigx = littlex = bigy = littley = assignedonce = 0;	

	for (i = 0; i < svp->n_segs; i++)
		for (j = 0; j < svp->segs[i].n_points; j++)
			{
				if (!assignedonce)
					{
						bigx = svp->segs[i].points[j].x;
						littlex = svp->segs[i].points[j].x;
						bigy = svp->segs[i].points[j].y; 
						littley = svp->segs[i].points[j].y;
						assignedonce = 1;
					}
				if (svp->segs[i].points[j].x > bigx)
					bigx = svp->segs[i].points[j].x;
				if (svp->segs[i].points[j].x < littlex)
					littlex = svp->segs[i].points[j].x;
				if (svp->segs[i].points[j].y > bigy)
					bigy = svp->segs[i].points[j].y; 
				if (svp->segs[i].points[j].y < littley)
					littley = svp->segs[i].points[j].y;
			}
	output.x0 = littlex;
	output.y0 = littley;
	output.x1 = bigx;
	output.y1 = bigy;
	return output;
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/**
 * rsvg_render_svp: Render an SVP.
 * @ctx: Context in which to render.
 * @svp: SVP to render.
 * @ps: Paint server for rendering.
 * @opacity: Opacity as 0..0xff.
 *
 * Renders the SVP over the pixbuf in @ctx.
 **/
static void
rsvg_render_svp (RsvgHandle *ctx, const ArtSVP *svp,
				 RsvgPaintServer *ps, int opacity)
{
	GdkPixbuf *pixbuf;
	ArtRender *render;
	gboolean has_alpha;
166 167
	ArtIRect temprect;
	RsvgPSCtx gradctx;
Dom Lachowicz's avatar
Dom Lachowicz committed
168 169 170
	RsvgState *state;
	int i;	

171 172 173 174 175 176 177 178
	pixbuf = ctx->pixbuf;
	if (pixbuf == NULL)
		{
			/* FIXME: What warning/GError here? */
			return;
		}
	
	has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
179

180 181 182 183 184 185 186 187 188 189
	render = art_render_new (0, 0,
							 gdk_pixbuf_get_width (pixbuf),
							 gdk_pixbuf_get_height (pixbuf),
							 gdk_pixbuf_get_pixels (pixbuf),
							 gdk_pixbuf_get_rowstride (pixbuf),
							 gdk_pixbuf_get_n_channels (pixbuf) -
							 (has_alpha ? 1 : 0),
							 gdk_pixbuf_get_bits_per_sample (pixbuf),
							 has_alpha ? ART_ALPHA_SEPARATE : ART_ALPHA_NONE,
							 NULL);
190

191 192
	art_render_svp (render, svp);
	art_render_mask_solid (render, (opacity << 8) + opacity + (opacity >> 7));
193 194 195

	temprect = rsvg_calculate_svp_bounds(svp);

196 197
	art_irect_union(&ctx->bbox, &ctx->bbox, &temprect);

198 199 200 201
	gradctx.x0 = temprect.x0;
	gradctx.y0 = temprect.y0;
	gradctx.x1 = temprect.x1;
	gradctx.y1 = temprect.y1;
202
	gradctx.ctx = ctx;
Dom Lachowicz's avatar
Dom Lachowicz committed
203 204 205 206

	state = rsvg_state_current(ctx);
	for (i = 0; i < 6; i++)
		gradctx.affine[i] = state->affine[i];
Caleb Michael Moore's avatar
Caleb Michael Moore committed
207 208
	
	gradctx.color = state->current_color;
209
	rsvg_render_paint_server (render, ps, &gradctx);
210 211 212 213 214 215 216 217 218 219 220 221 222 223
	art_render_invoke (render);
}

static void
rsvg_render_bpath (RsvgHandle *ctx, const ArtBpath *bpath)
{
	RsvgState *state;
	ArtBpath *affine_bpath;
	ArtVpath *vpath;
	ArtSVP *svp;
	GdkPixbuf *pixbuf;
	gboolean need_tmpbuf;
	int opacity;
	int tmp;
Dom Lachowicz's avatar
Dom Lachowicz committed
224

225 226 227 228 229 230 231
	pixbuf = ctx->pixbuf;
	if (pixbuf == NULL)
		{
			/* FIXME: What warning/GError here? */
			return;
		}
	
232
	state = rsvg_state_current (ctx);
Dom Lachowicz's avatar
Dom Lachowicz committed
233 234 235 236

	/* todo: handle visibility stuff earlier for performance benefits 
	 * handles all path based shapes. will handle text and images separately
	 */
Dom Lachowicz's avatar
Dom Lachowicz committed
237
	if (!state->visible || !state->cond_true)
Dom Lachowicz's avatar
Dom Lachowicz committed
238 239
		return;

240 241 242 243 244 245
	affine_bpath = art_bpath_affine_transform (bpath,
											   state->affine);
	
	vpath = art_bez_path_to_vec (affine_bpath, 0.25);
	art_free (affine_bpath);
	
246
	need_tmpbuf = ((state->fill != NULL) && (state->stroke != NULL) &&
Caleb Michael Moore's avatar
Caleb Michael Moore committed
247
				   state->opacity != 0xff) || rsvg_needs_discrete_layer(state);
248 249
	
	if (need_tmpbuf)
250
		rsvg_push_discrete_layer (ctx);
251 252 253 254 255 256 257 258 259 260 261
	
	if (state->fill != NULL)
		{
			ArtVpath *closed_vpath;
			ArtSVP *svp2;
			ArtSvpWriter *swr;
			
			closed_vpath = rsvg_close_vpath (vpath);
			svp = art_svp_from_vpath (closed_vpath);
			g_free (closed_vpath);
			
Dom Lachowicz's avatar
Dom Lachowicz committed
262 263 264 265 266
			if (state->fill_rule == FILL_RULE_EVENODD)
				swr = art_svp_writer_rewind_new (ART_WIND_RULE_ODDEVEN);
			else /* state->fill_rule == FILL_RULE_NONZERO */
				swr = art_svp_writer_rewind_new (ART_WIND_RULE_NONZERO);

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
			art_svp_intersector (svp, swr);
			
			svp2 = art_svp_writer_rewind_reap (swr);
			art_svp_free (svp);
			
			opacity = state->fill_opacity;
			if (!need_tmpbuf && state->opacity != 0xff)
				{
					tmp = opacity * state->opacity + 0x80;
					opacity = (tmp + (tmp >> 8)) >> 8;
				}
			rsvg_render_svp (ctx, svp2, state->fill, opacity);
			art_svp_free (svp2);
		}
	
	if (state->stroke != NULL)
		{
			/* todo: libart doesn't yet implement anamorphic scaling of strokes */
			double stroke_width = state->stroke_width *
				art_affine_expansion (state->affine);
			
			if (stroke_width < 0.25)
				stroke_width = 0.25;
			
			/* if the path is dashed, stroke it */
			if (state->dash.n_dash > 0) 
				{
					ArtVpath * dashed_vpath = art_vpath_dash (vpath, &state->dash);
					art_free (vpath);
					vpath = dashed_vpath;
				}
			
			svp = art_svp_vpath_stroke (vpath, state->join, state->cap,
										stroke_width, state->miter_limit, 0.25);
			opacity = state->stroke_opacity;
			if (!need_tmpbuf && state->opacity != 0xff)
				{
					tmp = opacity * state->opacity + 0x80;
					opacity = (tmp + (tmp >> 8)) >> 8;
				}
			rsvg_render_svp (ctx, svp, state->stroke, opacity);
			art_svp_free (svp);
		}
310

311
	if (need_tmpbuf)
312
		rsvg_pop_discrete_layer (ctx);	
313 314 315 316
	
	art_free (vpath);
}

Caleb Michael Moore's avatar
markers  
Caleb Michael Moore committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static void
rsvg_render_markers(RsvgBpathDef * bpath_def, RsvgHandle *ctx)
{
	int i;

	double x, y;
	double lastx, lasty;
	double nextx, nexty;	
    double linewidth;

	RsvgState * state;
	RsvgMarker * startmarker;
	RsvgMarker * middlemarker;
	RsvgMarker * endmarker;

	state = rsvg_state_current(ctx);
	
	linewidth = state->stroke_width;
	startmarker = (RsvgMarker *)state->startMarker;
	middlemarker = (RsvgMarker *)state->middleMarker;
	endmarker = (RsvgMarker *)state->endMarker;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
338 339 340 341

	if (!startmarker && !middlemarker && !endmarker)
		return;

Caleb Michael Moore's avatar
markers  
Caleb Michael Moore committed
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
	x = 0;
	y = 0;
	nextx = state->affine[0] * bpath_def->bpath[0].x3 + 
		state->affine[2] * bpath_def->bpath[0].y3 + state->affine[4];
	nexty = state->affine[1] * bpath_def->bpath[0].x3 + 
		state->affine[3] * bpath_def->bpath[0].y3 + state->affine[5];

	for (i = 0; i < bpath_def->n_bpath - 1; i++)
		{
			lastx = x;
			lasty = y;
			x = nextx;
			y = nexty;
			nextx = state->affine[0] * bpath_def->bpath[i + 1].x3 + 
				state->affine[2] * bpath_def->bpath[i + 1].y3 + state->affine[4];
			nexty = state->affine[1] * bpath_def->bpath[i + 1].x3 + 
				state->affine[3] * bpath_def->bpath[i + 1].y3 + state->affine[5];

			
			if(bpath_def->bpath[i + 1].code == ART_MOVETO || 
					bpath_def->bpath[i + 1].code == ART_MOVETO_OPEN || 
					bpath_def->bpath[i + 1].code == ART_END)
				{
					if (endmarker)
						rsvg_marker_render (endmarker, x, y, atan2(y - lasty, x - lastx), linewidth, ctx);
				}
			else if (bpath_def->bpath[i].code == ART_MOVETO || bpath_def->bpath[i].code == ART_MOVETO_OPEN)
				{		
					if (startmarker)
						rsvg_marker_render (startmarker, x, y, atan2(nexty - y, nextx - x), linewidth, ctx);
				}
			else
				{			
					if (middlemarker)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
						{
							double xdifin, ydifin, xdifout, ydifout, intot, outtot, angle;
							
							xdifin = x - lastx;
							ydifin = y - lasty;
							xdifout = nextx - x;
							ydifout = nexty - y;
							
							intot = sqrt(xdifin * xdifin + ydifin * ydifin);
							outtot = sqrt(xdifout * xdifout + ydifout * ydifout);
							
							xdifin /= intot;
							ydifin /= intot;
							xdifout /= outtot;
							ydifout /= outtot;
							
							angle = atan2((ydifin + ydifout) / 2, (xdifin + xdifout) / 2);
							rsvg_marker_render (middlemarker, x, y, angle, linewidth, ctx);
						}
Caleb Michael Moore's avatar
markers  
Caleb Michael Moore committed
395 396 397 398
				}
		}
}

399
void
400 401 402 403 404 405 406 407 408
rsvg_render_path(RsvgHandle *ctx, const char *d)
{
	RsvgBpathDef *bpath_def;
	
	bpath_def = rsvg_parse_path (d);
	rsvg_bpath_def_art_finish (bpath_def);
	
	rsvg_render_bpath (ctx, bpath_def->bpath);
	
Caleb Michael Moore's avatar
markers  
Caleb Michael Moore committed
409 410
	rsvg_render_markers(bpath_def, ctx);

411 412 413
	rsvg_bpath_def_free (bpath_def);
}

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
void 
rsvg_defs_drawable_draw (RsvgDefsDrawable * self, RsvgHandle *ctx,
						 int dominate)
{
	self->draw(self, ctx, dominate);
}

static void 
rsvg_defs_drawable_path_free (RsvgDefVal *self)
{
	RsvgDefsDrawablePath *z = (RsvgDefsDrawablePath *)self;
	rsvg_state_finalize (&z->super.state);
	g_free (z);
}

static void 
rsvg_defs_drawable_path_draw (RsvgDefsDrawable * self, RsvgHandle *ctx, 
							  int dominate)
{
	RsvgState *state = rsvg_state_current (ctx);
	RsvgDefsDrawablePath *path = (RsvgDefsDrawablePath*)self;

	/* combine state definitions */

	rsvg_state_clone (state, &self->state);
	if (ctx->n_state > 1)
		{
			if (dominate)
				rsvg_state_dominate(state, &ctx->state[ctx->n_state - 2]);
			else
				rsvg_state_reinherit(state, &ctx->state[ctx->n_state - 2]);
		}
Caleb Michael Moore's avatar
Caleb Michael Moore committed
446

447 448 449 450 451
	/* always want to render inside of a <use/> */
	rsvg_render_path (ctx, path->d);
	
}

452
static void 
453
rsvg_defs_drawable_group_free (RsvgDefVal *self)
454
{
455 456 457
	RsvgDefsDrawableGroup *z = (RsvgDefsDrawableGroup *)self;
	rsvg_state_finalize (&z->super.state);
	g_ptr_array_free(z->children, FALSE);
458 459 460
	g_free (z);
}

461 462 463 464 465 466 467
static void 
rsvg_defs_drawable_group_draw (RsvgDefsDrawable * self, RsvgHandle *ctx, 
							  int dominate)
{
	RsvgState *state = rsvg_state_current (ctx);
	RsvgDefsDrawableGroup *group = (RsvgDefsDrawableGroup*)self;
	guint i;
468 469 470 471 472 473
	double tempaffine[6];

	for (i = 0; i < 6; i++)
		{
			 tempaffine[i] = ctx->state[ctx->n_state - 1].affine[i];
		}	
474 475 476 477 478

	/* combine state definitions */
	rsvg_state_clone (state, &self->state);
	if (ctx->n_state > 1)
		{
479 480 481 482 483 484 485 486 487 488
			/*This is a special domination mode for patterns, the style
			  is simply reinherited, wheras the transform is totally overridden*/
			if (dominate == 2)
				{
					for (i = 0; i < 6; i++)
						{
							state->affine[i] = tempaffine[i];
						}	
				}		
			else if (dominate)
489 490 491 492 493
				rsvg_state_dominate(state, &ctx->state[ctx->n_state - 2]);
			else
				rsvg_state_reinherit(state, &ctx->state[ctx->n_state - 2]);
		}

Caleb Michael Moore's avatar
Caleb Michael Moore committed
494
	rsvg_push_discrete_layer (ctx);
495 496 497 498 499 500 501 502 503 504 505 506 507

	for (i = 0; i < group->children->len; i++)
		{
			/* push the state stack */
			if (ctx->n_state == ctx->n_state_max)
				ctx->state = g_renew (RsvgState, ctx->state, 
									  ctx->n_state_max <<= 1);
			if (ctx->n_state)
				rsvg_state_inherit (&ctx->state[ctx->n_state],
									&ctx->state[ctx->n_state - 1]);
			else
				rsvg_state_init (ctx->state);
			ctx->n_state++;
508

509 510 511 512 513 514 515 516
			rsvg_defs_drawable_draw (g_ptr_array_index(group->children, i), 
									 ctx, 0);
	
			/* pop the state stack */
			ctx->n_state--;
			rsvg_state_finalize (&ctx->state[ctx->n_state]);
		}			

Caleb Michael Moore's avatar
Caleb Michael Moore committed
517
	rsvg_pop_discrete_layer (ctx);
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
}

static void 
rsvg_defs_drawable_use_free (RsvgDefVal *self)
{
	RsvgDefsDrawableUse *z = (RsvgDefsDrawableUse *)self;
	rsvg_state_finalize (&z->super.state);
	g_free (z);
}

static void 
rsvg_defs_drawable_use_draw (RsvgDefsDrawable * self, RsvgHandle *ctx, 
							  int dominate)
{
	RsvgState *state = rsvg_state_current (ctx);
	RsvgDefsDrawableUse *use = (RsvgDefsDrawableUse*)self;

	/* combine state definitions */
	
	rsvg_state_clone (state, &self->state);
	if (ctx->n_state > 1)
		{
			if (dominate)
				rsvg_state_dominate(state, &ctx->state[ctx->n_state - 2]);
			else
				rsvg_state_reinherit(state, &ctx->state[ctx->n_state - 2]);
		}	
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
546
	if (state->opacity != 0xff || rsvg_needs_discrete_layer(state))
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
		rsvg_push_discrete_layer (ctx);


	/* push the state stack */
	if (ctx->n_state == ctx->n_state_max)
		ctx->state = g_renew (RsvgState, ctx->state, 
							  ctx->n_state_max <<= 1);
	if (ctx->n_state)
		rsvg_state_inherit (&ctx->state[ctx->n_state],
							&ctx->state[ctx->n_state - 1]);
	else
		rsvg_state_init (ctx->state);
	ctx->n_state++;
	
	rsvg_defs_drawable_draw (use->child, ctx, 1);
	
	/* pop the state stack */
	ctx->n_state--;
	rsvg_state_finalize (&ctx->state[ctx->n_state]);

Caleb Michael Moore's avatar
Caleb Michael Moore committed
567
	if (state->opacity != 0xff || rsvg_needs_discrete_layer(state))
568 569 570 571 572 573 574 575 576 577
		rsvg_pop_discrete_layer (ctx);
}			

static void 
rsvg_defs_drawable_group_pack (RsvgDefsDrawableGroup *self, RsvgDefsDrawable *child)
{
	RsvgDefsDrawableGroup *z = (RsvgDefsDrawableGroup *)self;
	g_ptr_array_add(z->children, child);
}

578 579

RsvgDefsDrawable * 
580 581 582 583
rsvg_push_def_group (RsvgHandle *ctx, const char * id)
{
	RsvgDefsDrawableGroup *group;
	if (!ctx->in_defs)
584
		return NULL;	
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600

	group = g_new (RsvgDefsDrawableGroup, 1);
	group->children = g_ptr_array_new();
	rsvg_state_clone (&group->super.state, rsvg_state_current (ctx));

	group->super.super.type = RSVG_DEF_PATH;
	group->super.super.free = rsvg_defs_drawable_group_free;
	group->super.draw = rsvg_defs_drawable_group_draw;

	rsvg_defs_set (ctx->defs, id, &group->super.super);

	group->super.parent = (RsvgDefsDrawable *)ctx->current_defs_group;
	if (group->super.parent != NULL)
		rsvg_defs_drawable_group_pack((RsvgDefsDrawableGroup *)group->super.parent, 
									  &group->super);
	ctx->current_defs_group = group;
601
	return &group->super;
602 603 604 605 606 607 608 609 610 611 612 613 614 615
}

void
rsvg_pop_def_group (RsvgHandle *ctx)
{
	RsvgDefsDrawableGroup * group;
	if (!ctx->in_defs)
		return;
	group = (RsvgDefsDrawableGroup *)ctx->current_defs_group;
	if (group == NULL)
		return;
	ctx->current_defs_group = group->super.parent;
}

616
void
617 618 619 620 621
rsvg_handle_path (RsvgHandle *ctx, const char * d, const char * id)
{
	if (!ctx->in_defs)
		rsvg_render_path (ctx, d);
	else {
Caleb Michael Moore's avatar
Caleb Michael Moore committed
622
	   
623
		RsvgDefsDrawablePath *path;
624

625
		path = g_new (RsvgDefsDrawablePath, 1);
626
		path->d = g_strdup(d);
627 628 629 630 631 632 633 634 635 636
		rsvg_state_clone (&path->super.state, rsvg_state_current (ctx));
		path->super.super.type = RSVG_DEF_PATH;
		path->super.super.free = rsvg_defs_drawable_path_free;
		path->super.draw = rsvg_defs_drawable_path_draw;
		rsvg_defs_set (ctx->defs, id, &path->super.super);

		path->super.parent = (RsvgDefsDrawable *)ctx->current_defs_group;
		if (path->super.parent != NULL)
			rsvg_defs_drawable_group_pack((RsvgDefsDrawableGroup *)path->super.parent, 
										  &path->super);
637 638 639
	}
}

640
void
641
rsvg_start_path (RsvgHandle *ctx, RsvgPropertyBag *atts)
642
{
643
	const char * klazz = NULL, * id = NULL, *value, *d = NULL;
644
	
645
	if (rsvg_property_bag_size (atts))
646
		{
647 648 649 650 651 652 653 654
			if ((value = rsvg_property_bag_lookup (atts, "d")))
				d = value;
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
				id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), "path", klazz, id, atts);
655 656 657 658 659
		}
	
	if (d == NULL)
		return;
	
660
	rsvg_handle_path (ctx, d, id);
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
}

static GString *
rsvg_make_poly_point_list(const char * points)
{
	guint idx = 0, size = strlen(points);
	GString * str = g_string_sized_new (size);
	
	while (idx < size) 
		{
			/* scan for first point */
			while (!g_ascii_isdigit (points[idx]) && (points[idx] != '.') 
				   && (points[idx] != '-') && (idx < size))
				idx++;
			
			/* now build up the point list (everything until next letter!) */
			if (idx < size && points[idx] == '-')
				g_string_append_c (str, points[idx++]); /* handle leading '-' */
			while ((g_ascii_isdigit (points[idx]) || (points[idx] == '.')) && (idx < size)) 
				g_string_append_c (str, points[idx++]);
			
			g_string_append_c (str, ' ');
		}
	
	return str;
}

static void
689
rsvg_start_any_poly(RsvgHandle *ctx, RsvgPropertyBag *atts, gboolean is_polyline)
690
{
691
	/* the only difference between polygon and polyline is
692 693 694 695 696
	   that a polyline closes the path */
	
	const char * verts = (const char *)NULL;
	GString * g = NULL;
	gchar ** pointlist = NULL;
697
	const char * klazz = NULL, * id = NULL, *value;
698

699
	if (rsvg_property_bag_size (atts))
700
		{
701 702 703 704 705 706 707 708 709
			/* support for svg < 1.0 which used verts */
			if ((value = rsvg_property_bag_lookup (atts, "verts")) || (value = rsvg_property_bag_lookup (atts, "points")))
				verts = value;
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
				id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), (is_polyline ? "polyline" : "polygon"), klazz, id, atts);
710 711 712
		}
	
	if (!verts)
713
		return;	
714 715 716 717 718 719 720 721 722
	
	/* todo: make the following more memory and CPU friendly */
	g = rsvg_make_poly_point_list (verts);
	pointlist = g_strsplit (g->str, " ", -1);
	g_string_free (g, TRUE);

	/* represent as a "moveto, lineto*, close" path */  
	if (pointlist)
		{
723
			int i;
724 725 726 727 728 729 730 731 732
			GString * d = g_string_sized_new (strlen(verts));
			g_string_append_printf (d, "M %s %s ", pointlist[0], pointlist[1] );
			
			for (i = 2; pointlist[i] != NULL && pointlist[i][0] != '\0'; i += 2)
				g_string_append_printf (d, "L %s %s ", pointlist[i], pointlist[i+1]);
			
			if (!is_polyline)
				g_string_append (d, "Z");
			
733
			g_strfreev(pointlist);
734
			rsvg_handle_path (ctx, d->str, id);
735 736 737 738 739
			g_string_free (d, TRUE);
		}
}

void
740
rsvg_start_polygon (RsvgHandle *ctx, RsvgPropertyBag *atts)
741 742 743 744 745
{
	rsvg_start_any_poly (ctx, atts, FALSE);
}

void
746
rsvg_start_polyline (RsvgHandle *ctx, RsvgPropertyBag *atts)
747 748 749 750 751
{
	rsvg_start_any_poly (ctx, atts, TRUE);
}

void
752
rsvg_start_line (RsvgHandle *ctx, RsvgPropertyBag *atts)
753 754
{
	double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
755
	GString * d = NULL;
756
	const char * klazz = NULL, * id = NULL, *value;
757
	char buf [G_ASCII_DTOSTR_BUF_SIZE];
758
	double font_size;
759

760
	font_size = rsvg_state_current_font_size (ctx);
761

762
	if (rsvg_property_bag_size (atts))
763
		{
764
			if ((value = rsvg_property_bag_lookup (atts, "x1")))
765
				x1 = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
766
			if ((value = rsvg_property_bag_lookup (atts, "y1")))
767
				y1 = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
768
			if ((value = rsvg_property_bag_lookup (atts, "x2")))
769
				x2 = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
770
			if ((value = rsvg_property_bag_lookup (atts, "y2")))
771
				y2 = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
772 773 774 775 776 777
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
				id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), "line", klazz, id, atts);
778 779 780
		}
	
	/* emulate a line using a path */
781 782 783 784 785 786 787 788 789 790 791
	/* ("M %f %f L %f %f", x1, y1, x2, y2) */
	d = g_string_new ("M ");   

	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x1));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y1));
	g_string_append (d, " L ");	
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x2));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y2));    

792
	rsvg_handle_path (ctx, d->str, id);
793
	g_string_free (d, TRUE);
794 795 796
}

void
797
rsvg_start_rect (RsvgHandle *ctx, RsvgPropertyBag *atts)
798
{
Dom Lachowicz's avatar
Dom Lachowicz committed
799
	double x = 0., y = 0., w = 0, h = 0, rx = 0., ry = 0.;
800
	GString * d = NULL;
801
	const char * klazz = NULL, * id = NULL, *value;
802 803
	char buf [G_ASCII_DTOSTR_BUF_SIZE];
	gboolean got_rx = FALSE, got_ry = FALSE;
804
	double font_size;
805

806
	font_size = rsvg_state_current_font_size (ctx);
807

808
	if (rsvg_property_bag_size (atts))
809
		{
810
			if ((value = rsvg_property_bag_lookup (atts, "x")))
811
				x = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
812
			if ((value = rsvg_property_bag_lookup (atts, "y")))
813
				y = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
814
			if ((value = rsvg_property_bag_lookup (atts, "width")))
815
				w = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
816
			if ((value = rsvg_property_bag_lookup (atts, "height")))
817
				h = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
818
			if ((value = rsvg_property_bag_lookup (atts, "rx"))) {
819
				rx = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
820 821 822
				got_rx = TRUE;
			}
			if ((value = rsvg_property_bag_lookup (atts, "ry"))) {
823
				ry = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
824 825 826 827 828 829 830 831
				got_ry = TRUE;
			}
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
				id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), "rect", klazz, id, atts);
832
		}
833 834 835

	if (got_rx && !got_ry)
		ry = rx;
836
	else if (got_ry && !got_rx)
837 838
		rx = ry;	

Dom Lachowicz's avatar
Dom Lachowicz committed
839
	if (w == 0. || h == 0. || rx < 0. || ry < 0.)
840
		return;
841

Dom Lachowicz's avatar
Dom Lachowicz committed
842 843 844
	if (rx > fabs(w / 2.))
		rx = fabs(w / 2.);
	if (ry > fabs(h / 2.))
845
		ry = fabs(h / 2.);   
846 847
	
	/* incrementing y by 1 properly draws borders. this is a HACK */
Dom Lachowicz's avatar
Dom Lachowicz committed
848
	y += .01;
849 850
	
	/* emulate a rect using a path */
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	d = g_string_new ("M ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x + rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y));

	g_string_append (d, " H ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x + w - rx));

	g_string_append (d, " A");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 1.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x+w));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y+ry));

	g_string_append (d, " V ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y+h-ry));

	g_string_append (d, " A");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 1.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x + w - rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y + h));

	g_string_append (d, " H ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x + rx));

	g_string_append (d, " A");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');	
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 1.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y + h - ry));

Caleb Michael Moore's avatar
Caleb Michael Moore committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	g_string_append (d, " V ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y+ry));

	g_string_append (d, " A");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');	
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 0.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), 1.));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), x+rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), y));

Caleb Michael Moore's avatar
Caleb Michael Moore committed
928
	g_string_append (d, " Z");
929

930
	rsvg_handle_path (ctx, d->str, id);
931
	g_string_free (d, TRUE);
932 933 934
}

void
935
rsvg_start_circle (RsvgHandle *ctx, RsvgPropertyBag *atts)
936 937
{
	double cx = 0, cy = 0, r = 0;
938
	GString * d = NULL;
939
	const char * klazz = NULL, * id = NULL, *value;
940
	char buf [G_ASCII_DTOSTR_BUF_SIZE];
941 942
	double font_size;
	
943
	font_size = rsvg_state_current_font_size (ctx);
944

945
	if (rsvg_property_bag_size (atts))
946
		{
947
			if ((value = rsvg_property_bag_lookup (atts, "cx")))
948
				cx = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
949
			if ((value = rsvg_property_bag_lookup (atts, "cy")))
950
				cy = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
951
			if ((value = rsvg_property_bag_lookup (atts, "r")))
952
				r = rsvg_css_parse_normalized_length (value, rsvg_dpi_percentage (ctx), 
953 954 955 956 957 958 959 960
													  rsvg_viewport_percentage((gdouble)ctx->width, (gdouble)ctx->height), 
													  font_size);
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
				id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), "circle", klazz, id, atts);
961 962
		}
	
Dom Lachowicz's avatar
Dom Lachowicz committed
963
	if (r <= 0.)
964
		return;   
965 966
	
	/* approximate a circle using 4 bezier curves */
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

	d = g_string_new ("M ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx+r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx+r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + r));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - r));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - r * RSVG_ARC_MAGIC));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + r));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " Z");

1027
	rsvg_handle_path (ctx, d->str, id);
1028
	g_string_free (d, TRUE);
1029 1030 1031
}

void
1032
rsvg_start_ellipse (RsvgHandle *ctx, RsvgPropertyBag *atts)
1033 1034
{
	double cx = 0, cy = 0, rx = 0, ry = 0;
1035
	GString * d = NULL;
1036
	const char * klazz = NULL, * id = NULL, *value;
1037
	char buf [G_ASCII_DTOSTR_BUF_SIZE];
1038 1039
	double font_size;
	
1040
	font_size = rsvg_state_current_font_size (ctx);
1041

1042
	if (rsvg_property_bag_size (atts))
1043
		{
1044
			if ((value = rsvg_property_bag_lookup (atts, "cx")))
1045
				cx = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
1046
			if ((value = rsvg_property_bag_lookup (atts, "cy")))
1047
				cy = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
1048
			if ((value = rsvg_property_bag_lookup (atts, "rx")))
1049
				rx = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, font_size);
1050
			if ((value = rsvg_property_bag_lookup (atts, "ry")))
1051
				ry = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, font_size);
1052 1053 1054 1055 1056 1057
			if ((value = rsvg_property_bag_lookup (atts, "class")))
				klazz = value;
			if ((value = rsvg_property_bag_lookup (atts, "id")))
						id = value;

			rsvg_parse_style_attrs (ctx, rsvg_state_current (ctx), "ellipse", klazz, id, atts);
1058 1059
		}
	
Dom Lachowicz's avatar
Dom Lachowicz committed
1060
	if (rx <= 0. || ry <= 0.)
1061
		return;   
1062 1063
	
	/* approximate an ellipse using 4 bezier curves */
1064

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	d = g_string_new ("M ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - RSVG_ARC_MAGIC * ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + RSVG_ARC_MAGIC * rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - ry));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - RSVG_ARC_MAGIC * rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy - RSVG_ARC_MAGIC * ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + RSVG_ARC_MAGIC * ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx - RSVG_ARC_MAGIC * rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + ry));

	g_string_append (d, " C ");
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + RSVG_ARC_MAGIC * rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy + RSVG_ARC_MAGIC * ry));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cx + rx));
	g_string_append_c (d, ' ');
	g_string_append (d, g_ascii_dtostr (buf, sizeof (buf), cy));

	g_string_append (d, " Z");

1124
	rsvg_handle_path (ctx, d->str, id);
1125
	g_string_free (d, TRUE);
1126 1127
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
static const char s_UTF8_B64Alphabet[64] = {
	0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, /* A-Z */
	0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, /* a-z */
	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, /* 0-9 */
	0x2b, /* + */
	0x2f  /* / */
};
static const char utf8_b64_pad = 0x3d;

static gboolean b64_decode_char (char c, int * b64)
{
	if ((c >= 0x41) && (c <= 0x5a))
		{
			*b64 = c - 0x41;
			return TRUE;
		}
	if ((c >= 0x61) && (c <= 0x7a))
		{
			*b64 = c - (0x61 - 26);
			return TRUE;
		}
	if ((c >= 0x30) && (c <= 0x39))
		{
			*b64 = c + (52 - 0x30);
			return TRUE;
		}
	if (c == 0x2b)
		{
			*b64 = 62;
			return TRUE;
		}
	if (c == 0x2f)
		{
			*b64 = 63;
			return TRUE;
		}
	return FALSE;
}

static gboolean utf8_base64_decode(char ** binptr, size_t * binlen, const char * b64ptr, size_t b64len)
{
	gboolean decoded = TRUE;
	gboolean padding = FALSE;
	
	int i = 0;
	glong ucs4_len, j;

	unsigned char byte1 = 0;
	unsigned char byte2;
	
	gunichar ucs4, * ucs4_str;
	
	if (b64len == 0) 
		return TRUE;
	
	if ((binptr == 0) || (b64ptr == 0)) 
		return FALSE;
	
	ucs4_str = g_utf8_to_ucs4_fast(b64ptr, b64len, &ucs4_len);
	
	for(j = 0; j < ucs4_len; j++)
		{
			ucs4 = ucs4_str[j];
			if ((ucs4 & 0x7f) == ucs4)
				{
					int b64;
					char c = (char)(ucs4);

					if (b64_decode_char (c, &b64))
						{
							if (padding || (*binlen == 0))
								{
									decoded = FALSE;
									break;
								}

							switch (i)
								{
								case 0:
									byte1 = (unsigned char)(b64) << 2;
									i++;
									break;
								case 1:
									byte2 = (unsigned char)(b64);
									byte1 |= byte2 >> 4;
									*(*binptr)++ = (char)(byte1);
									(*binlen)--;
									byte1 = (byte2 & 0x0f) << 4;
									i++;
									break;
								case 2:
									byte2 = (unsigned char)(b64);
									byte1 |= byte2 >> 2;
									*(*binptr)++ = (char)(byte1);
									(*binlen)--;
									byte1 = (byte2 & 0x03) << 6;
									i++;
									break;
								default:
									byte1 |= (unsigned char)(b64);
									*(*binptr)++ = (char)(byte1);
									(*binlen)--;
									i = 0;
									break;
								}
							
							if (!decoded) 
								break;

							continue;
						}
					else if (c == utf8_b64_pad)
						{
							switch (i)
								{
								case 0:
								case 1:
									decoded = FALSE;
									break;
								case 2:
									if (*binlen == 0) 
										decoded = FALSE;
									else
										{
											*(*binptr)++ = (char)(byte1);
											(*binlen)--;
											padding = TRUE;
										}
									i++;
									break;
								default:
									if (!padding)
										{
											if (*binlen == 0) 
												decoded = FALSE;
											else
												{
													*(*binptr)++ = (char)(byte1);
													(*binlen)--;
													padding = TRUE;
												}
										}
									i = 0;
									break;
								}
							if (!decoded) 
								break;

							continue;
						}
				}
			if (g_unichar_isspace (ucs4)) 
				continue;

			decoded = FALSE;
			break;
		}
	return decoded;
}

Dom Lachowicz's avatar
Dom Lachowicz committed
1290
static GdkPixbuf *
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
rsvg_pixbuf_new_from_data_at_size (const char *data,
								   GError    **error)
{
	GdkPixbufLoader *loader;
	GdkPixbuf       *pixbuf;
	
	char * buffer, *bufptr;
	size_t buffer_len, buffer_max_len, data_len;

	g_return_val_if_fail (data != NULL, NULL);

	while (*data) if (*data++ == ',') break;

	data_len = strlen(data);
	
	buffer_max_len = ((data_len >> 2) + 1) * 3;
	buffer_len = buffer_max_len;
	buffer = g_new(char, buffer_max_len);
	bufptr = buffer;

	if(!utf8_base64_decode(&bufptr, &buffer_len, data, data_len)) {
		g_free(buffer);
		return NULL;
	}

	buffer_len = buffer_max_len - buffer_len;

	loader = gdk_pixbuf_loader_new ();

	if (!gdk_pixbuf_loader_write (loader, buffer, buffer_len, error)) {
		gdk_pixbuf_loader_close (loader, NULL);
		g_object_unref (loader);
		return NULL;
	}
	
	if (!gdk_pixbuf_loader_close (loader, error)) {
		g_object_unref (loader);
		return NULL;
	}
	
	pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
	
	if (!pixbuf) {
		g_object_unref (loader);
		g_set_error (error,
					 GDK_PIXBUF_ERROR,
					 GDK_PIXBUF_ERROR_FAILED,
1338
					 _("Failed to load image: reason not known, probably a corrupt image."));
1339 1340 1341 1342 1343 1344 1345 1346 1347
		return NULL;
	}
	
	g_object_ref (pixbuf);
	
	g_object_unref (loader);
	
	g_free(buffer);
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1348
	return pixbuf;
1349 1350
}

Dom Lachowicz's avatar
Dom Lachowicz committed
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
static gchar *
rsvg_get_file_path (const gchar * filename, const gchar *basedir)
{
	gchar *absolute_filename;

	if (g_path_is_absolute(filename)) {
		absolute_filename = g_strdup (filename);
	} else {
		gchar *tmpcdir;

		if (basedir)
			tmpcdir = g_path_get_dirname (basedir);
		else
			tmpcdir = g_get_current_dir ();

		absolute_filename = g_build_filename (tmpcdir, filename, NULL);
		g_free(tmpcdir);
	}

	return absolute_filename;
}

Dom Lachowicz's avatar
Dom Lachowicz committed
1373
static GdkPixbuf *
1374
rsvg_pixbuf_new_from_file_at_size (const char *filename,
Dom Lachowicz's avatar
Dom Lachowicz committed
1375
								   const char *base_uri,
1376 1377 1378 1379
								   GError    **error)
{
	GdkPixbufLoader *loader;
	GdkPixbuf       *pixbuf;
Dom Lachowicz's avatar
Dom Lachowicz committed
1380 1381
	gchar *path;

1382 1383 1384
	guchar buffer [4096];
	int length;
	FILE *f;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1385

1386 1387
	g_return_val_if_fail (filename != NULL, NULL);
	
Dom Lachowicz's avatar
Dom Lachowicz committed
1388 1389 1390 1391
	path = rsvg_get_file_path (filename, base_uri);
	f = fopen (path, "rb");
	g_free (path);
	
1392 1393 1394 1395
	if (!f) {
		g_set_error (error,
					 G_FILE_ERROR,
					 g_file_error_from_errno (errno),
1396
					 _("Failed to open file '%s': %s"),
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
					 filename, g_strerror (errno));
		return NULL;
	}
	
	loader = gdk_pixbuf_loader_new ();
	
	while (!feof (f)) {
		length = fread (buffer, 1, sizeof (buffer), f);
		if (length > 0)
			if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
				gdk_pixbuf_loader_close (loader, NULL);
				fclose (f);
				g_object_unref (loader);
				return NULL;
			}
	}
	
	fclose (f);
	
	if (!gdk_pixbuf_loader_close (loader, error)) {
		g_object_unref (loader);
		return NULL;
	}
	
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1421 1422
	pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);	

1423 1424 1425 1426 1427
	if (!pixbuf) {
		g_object_unref (loader);
		g_set_error (error,
					 GDK_PIXBUF_ERROR,
					 GDK_PIXBUF_ERROR_FAILED,
1428
					 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
1429 1430 1431 1432 1433 1434 1435
					 filename);
		return NULL;
	}
	
	g_object_ref (pixbuf);
	
	g_object_unref (loader);
1436

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1437
	return pixbuf;
1438 1439
}

Dom Lachowicz's avatar
Dom Lachowicz committed
1440 1441 1442 1443 1444 1445
#ifdef HAVE_GNOME_VFS

#include <libgnomevfs/gnome-vfs.h>

static GdkPixbuf *
rsvg_pixbuf_new_from_vfs_at_size (const char *filename,
Dom Lachowicz's avatar
Dom Lachowicz committed
1446
								  const char *base_uri,
Dom Lachowicz's avatar
Dom Lachowicz committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
								  GError    **error)
{
	GdkPixbufLoader *loader;
	GdkPixbuf       *pixbuf;
	
	guchar buffer [4096];
	GnomeVFSFileSize length;
	GnomeVFSHandle *f = NULL;
	GnomeVFSResult res;
	
	g_return_val_if_fail (filename != NULL, NULL);
	
	if (!gnome_vfs_initialized())
		gnome_vfs_init();

	res = gnome_vfs_open (&f, filename, GNOME_VFS_OPEN_READ);

Dom Lachowicz's avatar
Dom Lachowicz committed
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	if (res != GNOME_VFS_OK) {
		if (base_uri) {
			GnomeVFSURI * base = gnome_vfs_uri_new (base_uri);
			if (base) {
				GnomeVFSURI * uri = gnome_vfs_uri_resolve_relative (base, filename);
				if (uri) {
					res = gnome_vfs_open_uri (&f, uri, GNOME_VFS_OPEN_READ);
					gnome_vfs_uri_unref (uri);
				}

				gnome_vfs_uri_unref (base);
			}
		}
	}

Dom Lachowicz's avatar
Dom Lachowicz committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
	if (res != GNOME_VFS_OK) {
		g_set_error (error, rsvg_error_quark (), (gint) res,
					 gnome_vfs_result_to_string (res));
		return NULL;
	}
	
	loader = gdk_pixbuf_loader_new ();
	
	while (TRUE) {
		res = gnome_vfs_read (f, buffer, sizeof (buffer), &length);
		if (res == GNOME_VFS_OK && length > 0) {
			if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
				gdk_pixbuf_loader_close (loader, NULL);
				gnome_vfs_close (f);
				g_object_unref (loader);
				return NULL;
			}
		} else {
			break;
		}
	}
	
	gnome_vfs_close (f);
	
	if (!gdk_pixbuf_loader_close (loader, error)) {
		g_object_unref (loader);
		return NULL;
	}
	
	pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
	
	if (!pixbuf) {
		g_object_unref (loader);
		g_set_error (error,
					 GDK_PIXBUF_ERROR,
					 GDK_PIXBUF_ERROR_FAILED,
					 _("Failed to load image '%s': reason not known, probably a corrupt image file"),
					 filename);
		return NULL;
	}
	
	g_object_ref (pixbuf);
	
	g_object_unref (loader);

Caleb Michael Moore's avatar
Caleb Michael Moore committed
1524
	return pixbuf;
Dom Lachowicz's avatar
Dom Lachowicz committed
1525 1526 1527 1528 1529 1530
}

#endif

GdkPixbuf *
rsvg_pixbuf_new_from_href (const char *href,
Dom Lachowicz's avatar
Dom Lachowicz committed
1531
						   const char *base_uri,
Dom Lachowicz's avatar
Dom Lachowicz committed
1532 1533 1534 1535 1536
						   GError    **err)
{
	GdkPixbuf * img = NULL;

	if(!strncmp(href, "data:", 5))
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1537
		img = rsvg_pixbuf_new_from_data_at_size (href, err);
Dom Lachowicz's avatar
Dom Lachowicz committed
1538 1539
	
	if(!img)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1540
		img = rsvg_pixbuf_new_from_file_at_size (href, base_uri, err);
Dom Lachowicz's avatar
Dom Lachowicz committed
1541 1542 1543

#ifdef HAVE_GNOME_VFS
	if(!img)
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1544
		img = rsvg_pixbuf_new_from_vfs_at_size (href, base_uri, err);
Dom Lachowicz's avatar
Dom Lachowicz committed
1545 1546 1547 1548 1549
#endif

	return img;
}

1550 1551 1552 1553 1554
/* TODO 1: issue with affining alpha images - this is gdkpixbuf's fault...
 * TODO 2: issue with rotating images - do we want to rotate the whole
 *         canvas 2x to get this right, only to have #1 bite us?
 */
void
1555
rsvg_start_image (RsvgHandle *ctx, RsvgPropertyBag *atts)
1556 1557 1558
{
	double x = 0., y = 0., w = -1., h = -1.;
	const char * href = NULL;
1559 1560
	const char * klazz = NULL, * id = NULL, *value;
	gboolean has_alpha;
1561
	int aspect_ratio = RSVG_ASPECT_RATIO_NONE;
1562
	ArtIRect temprect;
1563 1564 1565 1566
	GdkPixbuf *img;
	GError *err = NULL;
	
	double tmp_affine[6];
1567
	RsvgState *state;
Caleb Michael Moore's avatar
Caleb Michael Moore committed
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
	
	double tmp_tmp_affine[6];
	double inv_tmp_affine[6];
	double raw_inv_tmp_affine[6];
	GdkPixbuf * intermediate;
	int intstride;
	int basestride;	
	int basex, basey;
	double rawx, rawy;
	guchar * intpix;
	guchar * basepix;
	int i, j, k, basebpp;
1580 1581 1582 1583

	/* skip over defs entries for now */
	if (ctx->in_defs) return;

1584
	state = rsvg_state_current (ctx);
1585
	
1586
	if (rsvg_property_bag_size (atts))
1587
		{
1588
			if ((value = rsvg_property_bag_lookup (atts, "x")))
1589
				x = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, state->font_size);
1590
			if ((value = rsvg_property_bag_lookup (atts, "y")))
1591
				y = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, state->font_size);
1592
			if ((value = rsvg_property_bag_lookup (atts, "width")))
1593
				w = rsvg_css_parse_normalized_length (value, ctx->dpi_x, (gdouble)ctx->width, state->font_size);
1594
			if ((value = rsvg_property_bag_lookup (atts, "height")))
1595
				h = rsvg_css_parse_normalized_length (value, ctx->dpi_y, (gdouble)ctx->height, state->font_size);
<