<commit_before>dojo.provide("dojox.gfx.canvas");

dojo.require("dojox.gfx._base");
dojo.require("dojox.gfx.shape");
dojo.require("dojox.gfx.path");
dojo.require("dojox.gfx.arc");
dojo.require("dojox.gfx.decompose");

dojo.experimental("dojox.gfx.canvas");

(function(){
	var g = dojox.gfx, gs = g.shape, m = g.matrix, twoPI = 2 * Math.PI;
	
	dojo.extend(g.Shape, {
		render: function(/* Object */ ctx){
			// summary: render the shape
			ctx.save();
			this._renderTransform(ctx);
			this._renderShape(ctx);
			this._renderFill(ctx, true);
			this._renderStroke(ctx, true);
			ctx.restore();
		},
		_renderTransform: function(/* Object */ ctx){
			if("canvasTransform" in this){
				var t = this.canvasTransform;
				ctx.translate(t.dx, t.dy);
				ctx.rotate(t.angle2);
				ctx.scale(t.sx, t.sy);
				ctx.rotate(t.angle1);
				// The future implementation when vendors catch up with the spec:
				// var t = this.matrix;
				// ctx.transform(t.xx, t.yx, t.xy, t.yy, t.dx, t.dy);
			}
		},
		_renderShape: function(/* Object */ ctx){
			// nothing
		},
		_renderFill: function(/* Object */ ctx, /* Boolean */ apply){
			if("canvasFill" in this){
				ctx.fillStyle = this.canvasFill;
				if(apply){ ctx.fill(); }
			}else{
				ctx.fillStyle = "rgba(0,0,0,0.0)";
			}
		},
		_renderStroke: function(/* Object */ ctx, /* Boolean */ apply){
			var s = this.strokeStyle;
			if(s){
				ctx.strokeStyle = s.color.toString();
				ctx.lineWidth = s.width;
				ctx.lineCap = s.cap;
				if(typeof s.join == "number"){
					ctx.lineJoin = "miter";
					ctx.miterLimit = s.join;
				}else{
					ctx.lineJoin = s.join;
				}
				if(apply){ ctx.stroke(); }
			}else if(!apply){
				ctx.strokeStyle = "rgba(0,0,0,0.0)";
			}
		},
		
		// events are not implemented
		getEventSource: function(){ return null; },
		connect:		function(){},
		disconnect:		function(){}
	});
	
	var modifyMethod = function(shape, method, extra){
			var old = shape.prototype[method];
			shape.prototype[method] = extra ?
				function(){
					this.surface.makeDirty();
					old.apply(this, arguments);
					extra.call(this);
					return this;
				} :
				function(){
					this.surface.makeDirty();
					return old.apply(this, arguments);
				};
		};

	modifyMethod(g.Shape, "setTransform", 		
		function(){
			// prepare Canvas-specific structures
			if(this.matrix){
				this.canvasTransform = g.decompose(this.matrix);
			}else{
				delete this.canvasTransform;
			}
		});

	modifyMethod(g.Shape, "setFill",
		function(){
			// prepare Canvas-specific structures
			var fs = this.fillStyle, f;
			if(fs){
				if(typeof(fs) == "object" && "type" in fs){
					var ctx = this.surface.rawNode.getContext("2d");
					switch(fs.type){
						case "linear":
						case "radial":
							f = fs.type == "linear" ? 
								ctx.createLinearGradient(fs.x1, fs.y1, fs.x2, fs.y2) :
								ctx.createRadialGradient(fs.cx, fs.cy, 0, fs.cx, fs.cy, fs.r);
							dojo.forEach(fs.colors, function(step){
								f.addColorStop(step.offset, g.normalizeColor(step.color).toString());
							});
							break;
						case "pattern":
							var image = Image(fs.width, fs.height);
							image.src = fs.src;
							f = ctx.createPattern(image, "repeat");
					}
				}else{
					// Set fill color using CSS RGBA func style
					f = fs.toString();
				}
				this.canvasFill = f;
			}else{
				delete this.canvasFill;
			}
		});
	
	modifyMethod(g.Shape, "setStroke");
	modifyMethod(g.Shape, "setShape");
		
	dojo.declare("dojox.gfx.Group", g.Shape, {
		// summary: a group shape (Canvas), which can be used 
		//	to logically group shapes (e.g, to propagate matricies)
		constructor: function(){
			gs.Container._init.call(this);
		},
		render: function(/* Object */ ctx){
			// summary: render the group
			ctx.save();
			this._renderTransform(ctx);
			this._renderFill(ctx);
			this._renderStroke(ctx);
			for(var i = 0; i < this.children.length; ++i){
				this.children[i].render(ctx);
			}
			ctx.restore();
		}
	});

	dojo.declare("dojox.gfx.Rect", gs.Rect, {
		// summary: a rectangle shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape, r = Math.min(s.r, s.height / 2, s.width / 2),
				xl = s.x, xr = xl + s.width, yt = s.y, yb = yt + s.height,
				xl2 = xl + r, xr2 = xr - r, yt2 = yt + r, yb2 = yb - r;
			ctx.beginPath();
			ctx.moveTo(xl2, yt);
			ctx.lineTo(xr2, yt);
			if(r){ ctx.arcTo(xr, yt, xr, yt2, r); }
			ctx.lineTo(xr, yb2);
			if(r){ ctx.arcTo(xr, yb, xr2, yb, r); }
			ctx.lineTo(xl2, yb);
			if(r){ ctx.arcTo(xl, yb, xl, yb2, r); }
			ctx.lineTo(xl, yt2);
			if(r){ ctx.arcTo(xl, yt, xl2, yt, r); }
	 		ctx.closePath();
		}
	});
	
	dojo.declare("dojox.gfx.Ellipse", gs.Ellipse, {
		// summary: an ellipse shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape, r = Math.max(s.rx, s.ry);
			ctx.scale(s.rx / r, s.ry / r);
			ctx.beginPath();
			ctx.arc(s.cx, s.cy, r, 0, twoPI, 1);
		}
	});

	dojo.declare("dojox.gfx.Circle", gs.Circle, {
		// summary: a circle shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			ctx.beginPath();
			ctx.arc(s.cx, s.cy, s.r, 0, twoPI, 1);
		}
	});

	dojo.declare("dojox.gfx.Line", gs.Line, {
		// summary: a line shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			ctx.beginPath();
			ctx.moveTo(s.x1, s.y1);
			ctx.lineTo(s.x2, s.y2);
		}
	});

	dojo.declare("dojox.gfx.Polyline", gs.Polyline, {
		// summary: a polyline/polygon shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var p = this.shape.points, f = p[0], c, i;
			if(p.length){
				ctx.beginPath();
				if(typeof f == "number"){
					ctx.moveTo(f, p[1]);
					i = 2;
				}else{
					ctx.moveTo(f.x, f.y);
					i = 0
				}
				for(; i < p.length; ++i){
					c = p[i];
					if(typeof c == "number"){
						ctx.lineTo(c, p[++i]);
					}else{
						ctx.lineTo(c.x, c.y);
					}
				}
			}
		}
	});
	
	dojo.declare("dojox.gfx.Image", gs.Image, {
		// summary: an image shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			// nothing for the moment
		}
	});
	
	dojo.declare("dojox.gfx.Text", gs.Text, {
		// summary: a text shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			// nothing for the moment
		}
	});
	modifyMethod(g.Text, "setFont");
	
	var pathRenderers = {
		M: "_moveToA", m: "_moveToR", 
		L: "_lineToA", l: "_lineToR", <commit_msg>gfx: canvas polyline split into two functions to improve performance.<commit_after>dojo.provide("dojox.gfx.canvas");

dojo.require("dojox.gfx._base");
dojo.require("dojox.gfx.shape");
dojo.require("dojox.gfx.path");
dojo.require("dojox.gfx.arc");
dojo.require("dojox.gfx.decompose");

dojo.experimental("dojox.gfx.canvas");

(function(){
	var g = dojox.gfx, gs = g.shape, m = g.matrix, twoPI = 2 * Math.PI;
	
	dojo.extend(g.Shape, {
		render: function(/* Object */ ctx){
			// summary: render the shape
			ctx.save();
			this._renderTransform(ctx);
			this._renderShape(ctx);
			this._renderFill(ctx, true);
			this._renderStroke(ctx, true);
			ctx.restore();
		},
		_renderTransform: function(/* Object */ ctx){
			if("canvasTransform" in this){
				var t = this.canvasTransform;
				ctx.translate(t.dx, t.dy);
				ctx.rotate(t.angle2);
				ctx.scale(t.sx, t.sy);
				ctx.rotate(t.angle1);
				// The future implementation when vendors catch up with the spec:
				// var t = this.matrix;
				// ctx.transform(t.xx, t.yx, t.xy, t.yy, t.dx, t.dy);
			}
		},
		_renderShape: function(/* Object */ ctx){
			// nothing
		},
		_renderFill: function(/* Object */ ctx, /* Boolean */ apply){
			if("canvasFill" in this){
				ctx.fillStyle = this.canvasFill;
				if(apply){ ctx.fill(); }
			}else{
				ctx.fillStyle = "rgba(0,0,0,0.0)";
			}
		},
		_renderStroke: function(/* Object */ ctx, /* Boolean */ apply){
			var s = this.strokeStyle;
			if(s){
				ctx.strokeStyle = s.color.toString();
				ctx.lineWidth = s.width;
				ctx.lineCap = s.cap;
				if(typeof s.join == "number"){
					ctx.lineJoin = "miter";
					ctx.miterLimit = s.join;
				}else{
					ctx.lineJoin = s.join;
				}
				if(apply){ ctx.stroke(); }
			}else if(!apply){
				ctx.strokeStyle = "rgba(0,0,0,0.0)";
			}
		},
		
		// events are not implemented
		getEventSource: function(){ return null; },
		connect:		function(){},
		disconnect:		function(){}
	});
	
	var modifyMethod = function(shape, method, extra){
			var old = shape.prototype[method];
			shape.prototype[method] = extra ?
				function(){
					this.surface.makeDirty();
					old.apply(this, arguments);
					extra.call(this);
					return this;
				} :
				function(){
					this.surface.makeDirty();
					return old.apply(this, arguments);
				};
		};

	modifyMethod(g.Shape, "setTransform", 		
		function(){
			// prepare Canvas-specific structures
			if(this.matrix){
				this.canvasTransform = g.decompose(this.matrix);
			}else{
				delete this.canvasTransform;
			}
		});

	modifyMethod(g.Shape, "setFill",
		function(){
			// prepare Canvas-specific structures
			var fs = this.fillStyle, f;
			if(fs){
				if(typeof(fs) == "object" && "type" in fs){
					var ctx = this.surface.rawNode.getContext("2d");
					switch(fs.type){
						case "linear":
						case "radial":
							f = fs.type == "linear" ? 
								ctx.createLinearGradient(fs.x1, fs.y1, fs.x2, fs.y2) :
								ctx.createRadialGradient(fs.cx, fs.cy, 0, fs.cx, fs.cy, fs.r);
							dojo.forEach(fs.colors, function(step){
								f.addColorStop(step.offset, g.normalizeColor(step.color).toString());
							});
							break;
						case "pattern":
							var image = Image(fs.width, fs.height);
							image.src = fs.src;
							f = ctx.createPattern(image, "repeat");
					}
				}else{
					// Set fill color using CSS RGBA func style
					f = fs.toString();
				}
				this.canvasFill = f;
			}else{
				delete this.canvasFill;
			}
		});
	
	modifyMethod(g.Shape, "setStroke");
	modifyMethod(g.Shape, "setShape");
		
	dojo.declare("dojox.gfx.Group", g.Shape, {
		// summary: a group shape (Canvas), which can be used 
		//	to logically group shapes (e.g, to propagate matricies)
		constructor: function(){
			gs.Container._init.call(this);
		},
		render: function(/* Object */ ctx){
			// summary: render the group
			ctx.save();
			this._renderTransform(ctx);
			this._renderFill(ctx);
			this._renderStroke(ctx);
			for(var i = 0; i < this.children.length; ++i){
				this.children[i].render(ctx);
			}
			ctx.restore();
		}
	});

	dojo.declare("dojox.gfx.Rect", gs.Rect, {
		// summary: a rectangle shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape, r = Math.min(s.r, s.height / 2, s.width / 2),
				xl = s.x, xr = xl + s.width, yt = s.y, yb = yt + s.height,
				xl2 = xl + r, xr2 = xr - r, yt2 = yt + r, yb2 = yb - r;
			ctx.beginPath();
			ctx.moveTo(xl2, yt);
			ctx.lineTo(xr2, yt);
			if(r){ ctx.arcTo(xr, yt, xr, yt2, r); }
			ctx.lineTo(xr, yb2);
			if(r){ ctx.arcTo(xr, yb, xr2, yb, r); }
			ctx.lineTo(xl2, yb);
			if(r){ ctx.arcTo(xl, yb, xl, yb2, r); }
			ctx.lineTo(xl, yt2);
			if(r){ ctx.arcTo(xl, yt, xl2, yt, r); }
	 		ctx.closePath();
		}
	});
	
	dojo.declare("dojox.gfx.Ellipse", gs.Ellipse, {
		// summary: an ellipse shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape, r = Math.max(s.rx, s.ry);
			ctx.scale(s.rx / r, s.ry / r);
			ctx.beginPath();
			ctx.arc(s.cx, s.cy, r, 0, twoPI, 1);
		}
	});

	dojo.declare("dojox.gfx.Circle", gs.Circle, {
		// summary: a circle shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			ctx.beginPath();
			ctx.arc(s.cx, s.cy, s.r, 0, twoPI, 1);
		}
	});

	dojo.declare("dojox.gfx.Line", gs.Line, {
		// summary: a line shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			ctx.beginPath();
			ctx.moveTo(s.x1, s.y1);
			ctx.lineTo(s.x2, s.y2);
		}
	});

	dojo.declare("dojox.gfx.Polyline", gs.Polyline, {
		// summary: a polyline/polygon shape (Canvas)
		setShape: function(){
			gs.Polyline.prototype.setShape.apply(this, arguments);
			var p = this.shape.points, f = p[0], r = [], c, i;
			if(p.length){
				if(typeof f == "number"){
					r.push(f, p[1]);
					i = 2;
				}else{
					r.push(f.x, f.y);
					i = 1;
				}
				for(; i < p.length; ++i){
					c = p[i];
					if(typeof c == "number"){
						r.push(c, p[++i]);
					}else{
						r.push(c.x, c.y);
					}
				}
			}
			this.canvasPolyline = r;
			return this;
		},
		_renderShape: function(/* Object */ ctx){
			var p = this.canvasPolyline;
			if(p.length){
				ctx.beginPath();
				ctx.moveTo(p[0], p[1]);
				for(var i = 2; i < p.length; i += 2){
					ctx.lineTo(p[i], p[i + 1]);
				}
			}
		}
	});
	
	dojo.declare("dojox.gfx.Image", gs.Image, {
		// summary: an image shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			// nothing for the moment
		}
	});
	
	dojo.declare("dojox.gfx.Text", gs.Text, {
		// summary: a text shape (Canvas)
		_renderShape: function(/* Object */ ctx){
			var s = this.shape;
			// nothing for the moment
		}
	});
	modifyMethod(g.Text, "setFont");
	
	var pathRenderers = {
		M: "_moveToA", m: "_moveToR", 
		L: "_lineToA", l: "_lineToR", 
{'fraction_non_alphanumeric': 0.18736200989722115, 'fraction_numerical': 0.0075371145793681005, 'mean_word_length': 2.681614349775785, 'pattern_counts': {'":': 6, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3894278', 'n_tokens_mistral': 6200, 'n_tokens_neox': 5132, 'n_words': 1082}