diff --git a/component.json b/component.json index 1fdeacfd..485411c8 100644 --- a/component.json +++ b/component.json @@ -1,6 +1,6 @@ { "name": "paper", - "version": "0.9.24", + "version": "0.9.25", "description": "The Swiss Army Knife of Vector Graphics Scripting", "license": "MIT", "repo": "paperjs/paper.js", diff --git a/dist/paper-core.js b/dist/paper-core.js index 18306b9d..6568d2c8 100644 --- a/dist/paper-core.js +++ b/dist/paper-core.js @@ -1,5 +1,5 @@ /*! - * Paper.js v0.9.24 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.25 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Fri Aug 21 16:39:41 2015 +0200 + * Date: Sun Oct 25 11:23:38 2015 +0100 * *** * @@ -581,7 +581,7 @@ var Emitter = { handlers = handlers[type] = handlers[type] || []; if (handlers.indexOf(func) === -1) { handlers.push(func); - if (entry && entry.install && handlers.length == 1) + if (entry && entry.install && handlers.length === 1) entry.install.call(this, type); } } @@ -746,7 +746,7 @@ var PaperScope = Base.extend({ } }, - version: '0.9.24', + version: "0.9.25", getView: function() { return this.project && this.project.getView(); @@ -929,18 +929,26 @@ var Numerical = new function() { var abs = Math.abs, sqrt = Math.sqrt, pow = Math.pow, - TOLERANCE = 1e-6, EPSILON = 1e-12, MACHINE_EPSILON = 1.12e-16; + function clip(value, min, max) { + return value < min ? min : value > max ? max : value; + } + return { - TOLERANCE: TOLERANCE, + TOLERANCE: 1e-6, EPSILON: EPSILON, MACHINE_EPSILON: MACHINE_EPSILON, + CURVETIME_EPSILON: 4e-7, + GEOMETRIC_EPSILON: 2e-7, + WINDING_EPSILON: 2e-7, + TRIGONOMETRIC_EPSILON: 1e-7, + CLIPPING_EPSILON: 1e-7, KAPPA: 4 * (sqrt(2) - 1) / 3, isZero: function(val) { - return abs(val) <= EPSILON; + return val >= -EPSILON && val <= EPSILON; }, integrate: function(f, a, b, n) { @@ -978,16 +986,18 @@ var Numerical = new function() { solveQuadratic: function(a, b, c, roots, min, max) { var count = 0, + eMin = min - EPSILON, + eMax = max + EPSILON, x1, x2 = Infinity, B = b, D; - b /= 2; + b /= -2; D = b * b - a * c; if (D !== 0 && abs(D) < MACHINE_EPSILON) { var gmC = pow(abs(a * b * c), 1 / 3); if (gmC < 1e-8) { - var mult = pow(10, abs( - Math.floor(Math.log(gmC) * Math.LOG10E))); + var mult = pow(10, + abs(Math.floor(Math.log(gmC) * Math.LOG10E))); if (!isFinite(mult)) mult = 0; a *= mult; @@ -1000,25 +1010,22 @@ var Numerical = new function() { if (abs(B) < EPSILON) return abs(c) < EPSILON ? -1 : 0; x1 = -c / B; - } else { - if (D >= -MACHINE_EPSILON) { - D = D < 0 ? 0 : D; - var R = sqrt(D); - if (b >= MACHINE_EPSILON && b <= MACHINE_EPSILON) { - x1 = abs(a) >= abs(c) ? R / a : -c / R; - x2 = -x1; - } else { - var q = -(b + (b < 0 ? -1 : 1) * R); - x1 = q / a; - x2 = c / q; - } + } else if (D >= -MACHINE_EPSILON) { + var Q = D < 0 ? 0 : sqrt(D), + R = b + (b < 0 ? -Q : Q); + if (R === 0) { + x1 = c / a; + x2 = -x1; + } else { + x1 = R / a; + x2 = c / R; } } - if (isFinite(x1) && (min == null || x1 >= min && x1 <= max)) - roots[count++] = x1; + if (isFinite(x1) && (min == null || x1 > eMin && x1 < eMax)) + roots[count++] = min == null ? x1 : clip(x1, min, max); if (x2 !== x1 - && isFinite(x2) && (min == null || x2 >= min && x2 <= max)) - roots[count++] = x2; + && isFinite(x2) && (min == null || x2 > eMin && x2 < eMax)) + roots[count++] = min == null ? x2 : clip(x2, min, max); return count; }, @@ -1071,8 +1078,8 @@ var Numerical = new function() { } var count = Numerical.solveQuadratic(a, b1, c2, roots, min, max); if (isFinite(x) && (count === 0 || x !== roots[count - 1]) - && (min == null || x >= min && x <= max)) - roots[count++] = x; + && (min == null || x > min - EPSILON && x < max + EPSILON)) + roots[count++] = min == null ? x : clip(x, min, max); return count; } }; @@ -1259,11 +1266,11 @@ var Point = Base.extend({ return this.clone(); angle = angle * Math.PI / 180; var point = center ? this.subtract(center) : this, - s = Math.sin(angle), - c = Math.cos(angle); + sin = Math.sin(angle), + cos = Math.cos(angle); point = new Point( - point.x * c - point.y * s, - point.x * s + point.y * c + point.x * cos - point.y * sin, + point.x * sin + point.y * cos ); return center ? point.add(center) : point; }, @@ -1305,18 +1312,22 @@ var Point = Base.extend({ return Rectangle.read(arguments).contains(this); }, - isClose: function(point, tolerance) { + isClose: function() { + var point = Point.read(arguments), + tolerance = Base.read(arguments); return this.getDistance(point) < tolerance; }, - isCollinear: function(point) { - return Math.abs(this.cross(point)) < 0.000001; + isCollinear: function() { + var point = Point.read(arguments); + return Point.isCollinear(this.x, this.y, point.x, point.y); }, isColinear: '#isCollinear', - isOrthogonal: function(point) { - return Math.abs(this.dot(point)) < 0.000001; + isOrthogonal: function() { + var point = Point.read(arguments); + return Point.isOrthogonal(this.x, this.y, point.x, point.y); }, isZero: function() { @@ -1338,16 +1349,12 @@ var Point = Base.extend({ }, project: function() { - var point = Point.read(arguments); - if (point.isZero()) { - return new Point(0, 0); - } else { - var scale = this.dot(point) / point.dot(point); - return new Point( - point.x * scale, - point.y * scale - ); - } + var point = Point.read(arguments), + scale = point.isZero() ? 0 : this.dot(point) / point.dot(point); + return new Point( + point.x * scale, + point.y * scale + ); }, statics: { @@ -1371,6 +1378,18 @@ var Point = Base.extend({ random: function() { return new Point(Math.random(), Math.random()); + }, + + isCollinear: function(x1, y1, x2, y2) { + return Math.abs(x1 * y2 - y1 * x2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; + }, + + isOrthogonal: function(x1, y1, x2, y2) { + return Math.abs(x1 * x2 + y1 * y2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; } } }, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) { @@ -1922,7 +1941,8 @@ var LinkedRectangle = Rectangle.extend({ this._owner[this._setter](this); return this; } -}, new function() { +}, +new function() { var proto = Rectangle.prototype; return Base.each(['x', 'y', 'width', 'height'], function(key) { @@ -2255,7 +2275,7 @@ var Matrix = Base.extend({ _transformBounds: function(bounds, dest, _dontNotify) { var coords = this._transformCorners(bounds), min = coords.slice(0, 2), - max = coords.slice(); + max = min.slice(); for (var i = 2; i < 8; i++) { var val = coords[i], j = i & 1; @@ -2417,10 +2437,10 @@ var Line = Base.extend({ true, isInfinite); }, - getSide: function(point) { + getSide: function(point, isInfinite) { return Line.getSide( this._px, this._py, this._vx, this._vy, - point.x, point.y, true); + point.x, point.y, true, isInfinite); }, getDistance: function(point) { @@ -2429,29 +2449,45 @@ var Line = Base.extend({ point.x, point.y, true)); }, + isCollinear: function(line) { + return Point.isCollinear(this._vx, this._vy, line._vx, line._vy); + }, + + isOrthogonal: function(line) { + return Point.isOrthogonal(this._vx, this._vy, line._vx, line._vy); + }, + statics: { - intersect: function(apx, apy, avx, avy, bpx, bpy, bvx, bvy, asVector, + intersect: function(p1x, p1y, v1x, v1y, p2x, p2y, v2x, v2y, asVector, isInfinite) { if (!asVector) { - avx -= apx; - avy -= apy; - bvx -= bpx; - bvy -= bpy; + v1x -= p1x; + v1y -= p1y; + v2x -= p2x; + v2y -= p2y; } - var cross = avx * bvy - avy * bvx; + var cross = v1x * v2y - v1y * v2x; if (!Numerical.isZero(cross)) { - var dx = apx - bpx, - dy = apy - bpy, - ta = (bvx * dy - bvy * dx) / cross, - tb = (avx * dy - avy * dx) / cross; - if (isInfinite || 0 <= ta && ta <= 1 && 0 <= tb && tb <= 1) + var dx = p1x - p2x, + dy = p1y - p2y, + u1 = (v2x * dy - v2y * dx) / cross, + u2 = (v1x * dy - v1y * dx) / cross, + epsilon = 1e-12, + uMin = -epsilon, + uMax = 1 + epsilon; + if (isInfinite + || uMin < u1 && u1 < uMax && uMin < u2 && u2 < uMax) { + if (!isInfinite) { + u1 = u1 <= 0 ? 0 : u1 >= 1 ? 1 : u1; + } return new Point( - apx + ta * avx, - apy + ta * avy); + p1x + u1 * v1x, + p1y + u1 * v1y); + } } }, - getSide: function(px, py, vx, vy, x, y, asVector) { + getSide: function(px, py, vx, vy, x, y, asVector, isInfinite) { if (!asVector) { vx -= px; vy -= py; @@ -2459,15 +2495,10 @@ var Line = Base.extend({ var v2x = x - px, v2y = y - py, ccw = v2x * vy - v2y * vx; - if (ccw === 0) { - ccw = v2x * vx + v2y * vy; - if (ccw > 0) { - v2x -= vx; - v2y -= vy; - ccw = v2x * vx + v2y * vy; - if (ccw < 0) - ccw = 0; - } + if (ccw === 0 && !isInfinite) { + ccw = (v2x * vx + v2x * vx) / (vx * vx + vy * vy); + if (ccw >= 0 && ccw <= 1) + ccw = 0; } return ccw < 0 ? -1 : ccw > 0 ? 1 : 0; }, @@ -2477,11 +2508,9 @@ var Line = Base.extend({ vx -= px; vy -= py; } - return Numerical.isZero(vx) - ? vy >= 0 ? px - x : x - px - : Numerical.isZero(vy) - ? vx >= 0 ? y - py : py - y - : (vx * (y - py) - vy * (x - px)) / Math.sqrt(vx * vx + vy * vy); + return vx === 0 ? vy > 0 ? x - px : px - x + : vy === 0 ? vx < 0 ? y - py : py - y + : ((x-px) * vy - (y-py) * vx) / Math.sqrt(vx * vx + vy * vy); } } }); @@ -2783,70 +2812,32 @@ var Item = Base.extend(Emitter, { return hasProps; }, - _events: new function() { - - var mouseFlags = { - mousedown: { - mousedown: 1, - mousedrag: 1, - click: 1, - doubleclick: 1 - }, - mouseup: { - mouseup: 1, - mousedrag: 1, - click: 1, - doubleclick: 1 - }, - mousemove: { - mousedrag: 1, - mousemove: 1, - mouseenter: 1, - mouseleave: 1 - } - }; - - var mouseEvent = { - install: function(type) { - var counters = this.getView()._eventCounters; - if (counters) { - for (var key in mouseFlags) { - counters[key] = (counters[key] || 0) - + (mouseFlags[key][type] || 0); - } - } - }, - uninstall: function(type) { - var counters = this.getView()._eventCounters; - if (counters) { - for (var key in mouseFlags) - counters[key] -= mouseFlags[key][type] || 0; - } - } - }; - - return Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', + _events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', 'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'], - function(name) { - this[name] = mouseEvent; - }, { - onFrame: { - install: function() { - this._animateItem(true); - }, - uninstall: function() { - this._animateItem(false); - } + function(name) { + this[name] = { + install: function(type) { + this.getView()._installEvent(type); }, - onLoad: {} - } - ); - }, + uninstall: function(type) { + this.getView()._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.getView()._animateItem(this, true); + }, - _animateItem: function(animate) { - this.getView()._animateItem(this, animate); - }, + uninstall: function() { + this.getView()._animateItem(this, false); + } + }, + + onLoad: {} + } + ), _serialize: function(options, dictionary) { var props = {}, @@ -3452,8 +3443,8 @@ var Item = Base.extend(Emitter, { intersects: function(item, _matrix) { if (!(item instanceof Item)) return false; - return this._asPathItem().getIntersections(item._asPathItem(), - _matrix || item._matrix).length > 0; + return this._asPathItem().getIntersections(item._asPathItem(), null, + _matrix || item._matrix, true).length > 0; }, hitTest: function() { @@ -3476,7 +3467,7 @@ var Item = Base.extend(Emitter, { tolerancePadding = options._tolerancePadding = new Size( Path._getPenPadding(1, totalMatrix.inverted()) ).multiply( - Math.max(options.tolerance, 0.000001) + Math.max(options.tolerance, 1e-6) ); point = matrix._inverseTransform(point); @@ -3599,7 +3590,7 @@ var Item = Base.extend(Emitter, { var overlapping = match.overlapping, inside = match.inside, bounds = overlapping || inside, - rect = bounds && Rectangle.read([bounds]); + rect = bounds && Rectangle.read([bounds]); param = { items: [], inside: !!inside, @@ -3903,6 +3894,10 @@ var Item = Base.extend(Emitter, { return item ? item.isDescendant(this) : false; }, + isSibling: function(item) { + return this._parent === item._parent; + }, + isGroupedWith: function(item) { var parent = this._parent; while (parent) { @@ -4178,7 +4173,7 @@ var Item = Base.extend(Emitter, { if (this._drawSelected) this._drawSelected(ctx, mx, selectedItems); if (this._boundsSelected) { - var half = size / 2; + var half = size / 2, coords = mx._transformCorners(this.getInternalBounds()); ctx.beginPath(); for (var i = 0; i < 8; i++) @@ -4557,7 +4552,6 @@ var Shape = Item.extend({ } }, new function() { - function getCornerCenter(that, point, expand) { var radius = that._radius; if (!radius.isZero()) { @@ -4902,7 +4896,7 @@ var Raster = Item.extend({ if (/^data:/.test(src)) return src; var canvas = this.getCanvas(); - return canvas ? canvas.toDataURL() : null; + return canvas ? canvas.toDataURL.apply(canvas, arguments) : null; }, drawImage: function(image ) { @@ -5133,7 +5127,7 @@ var Segment = Base.extend({ point, handleIn, handleOut; if (count === 0) { } else if (count === 1) { - if (arg0.point) { + if ('point' in arg0) { point = arg0.point; handleIn = arg0.handleIn; handleOut = arg0.handleOut; @@ -5157,8 +5151,9 @@ var Segment = Base.extend({ }, _serialize: function(options) { - return Base.serialize(this.isStraight() ? this._point - : [this._point, this._handleIn, this._handleOut], + return Base.serialize(this.hasHandles() + ? [this._point, this._handleIn, this._handleOut] + : this._point, options, true); }, @@ -5209,34 +5204,14 @@ var Segment = Base.extend({ }, hasHandles: function() { - return !this.isStraight(); + return !this._handleIn.isZero() || !this._handleOut.isZero(); }, - isStraight: function() { - return this._handleIn.isZero() && this._handleOut.isZero(); + clearHandles: function() { + this._handleIn.set(0, 0); + this._handleOut.set(0, 0); }, - isLinear: function() { - return Segment.isLinear(this, this.getNext()); - }, - - isCollinear: function(segment) { - return Segment.isCollinear(this, this.getNext(), - segment, segment.getNext()); - }, - - isColinear: '#isCollinear', - - isOrthogonal: function() { - return Segment.isOrthogonal(this.getPrevious(), this, this.getNext()); - }, - - isOrthogonalArc: function() { - return Segment.isOrthogonalArc(this, this.getNext()); - }, - - isArc: '#isOrthogonalArc', - _selectionState: 0, isSelected: function(_point) { @@ -5309,7 +5284,25 @@ var Segment = Base.extend({ || this._path._closed && segments[segments.length - 1]) || null; }, + isFirst: function() { + return this._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._index === path._segments.length - 1 || false; + }, + reverse: function() { + var handleIn = this._handleIn, + handleOut = this._handleOut, + inX = handleIn._x, + inY = handleIn._y; + handleIn.set(handleOut._x, handleOut._y); + handleOut.set(inX, inY); + }, + + reversed: function() { return new Segment(this._point, this._handleOut, this._handleIn); }, @@ -5390,46 +5383,6 @@ var Segment = Base.extend({ } } return coords; - }, - - statics: { - - isLinear: function(seg1, seg2) { - var l = seg2._point.subtract(seg1._point); - return l.isCollinear(seg1._handleOut) - && l.isCollinear(seg2._handleIn); - }, - - isCollinear: function(seg1, seg2, seg3, seg4) { - return seg1._handleOut.isZero() && seg2._handleIn.isZero() - && seg3._handleOut.isZero() && seg4._handleIn.isZero() - && seg2._point.subtract(seg1._point).isCollinear( - seg4._point.subtract(seg3._point)); - }, - - isOrthogonal: function(seg1, seg2, seg3) { - return seg1._handleOut.isZero() && seg2._handleIn.isZero() - && seg2._handleOut.isZero() && seg3._handleIn.isZero() - && seg2._point.subtract(seg1._point).isOrthogonal( - seg3._point.subtract(seg2._point)); - }, - - isOrthogonalArc: function(seg1, seg2) { - var handle1 = seg1._handleOut, - handle2 = seg2._handleIn, - kappa = 0.5522847498307936; - if (handle1.isOrthogonal(handle2)) { - var pt1 = seg1._point, - pt2 = seg2._point, - corner = new Line(pt1, handle1, true).intersect( - new Line(pt2, handle2, true), true); - return corner && Numerical.isZero(handle1.getLength() / - corner.subtract(pt1).getLength() - kappa) - && Numerical.isZero(handle2.getLength() / - corner.subtract(pt2).getLength() - kappa); - } - return false; - }, } }); @@ -5508,42 +5461,88 @@ var Curve = Base.extend({ _class: 'Curve', initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { - var count = arguments.length; + var count = arguments.length, + seg1, seg2, + point1, point2, + handle1, handle2; if (count === 3) { this._path = arg0; - this._segment1 = arg1; - this._segment2 = arg2; + seg1 = arg1; + seg2 = arg2; } else if (count === 0) { - this._segment1 = new Segment(); - this._segment2 = new Segment(); + seg1 = new Segment(); + seg2 = new Segment(); } else if (count === 1) { - this._segment1 = new Segment(arg0.segment1); - this._segment2 = new Segment(arg0.segment2); - } else if (count === 2) { - this._segment1 = new Segment(arg0); - this._segment2 = new Segment(arg1); - } else { - var point1, handle1, handle2, point2; - if (count === 4) { - point1 = arg0; - handle1 = arg1; - handle2 = arg2; - point2 = arg3; - } else if (count === 8) { - point1 = [arg0, arg1]; - point2 = [arg6, arg7]; - handle1 = [arg2 - arg0, arg3 - arg1]; - handle2 = [arg4 - arg6, arg5 - arg7]; + if ('segment1' in arg0) { + seg1 = new Segment(arg0.segment1); + seg2 = new Segment(arg0.segment2); + } else if ('point1' in arg0) { + point1 = arg0.point1; + handle1 = arg0.handle1; + handle2 = arg0.handle2; + point2 = arg0.point2; + } else if (Array.isArray(arg0)) { + point1 = [arg0[0], arg0[1]]; + point2 = [arg0[6], arg0[7]]; + handle1 = [arg0[2] - arg0[0], arg0[3] - arg0[1]]; + handle2 = [arg0[4] - arg0[6], arg0[5] - arg0[7]]; } - this._segment1 = new Segment(point1, null, handle1); - this._segment2 = new Segment(point2, handle2, null); + } else if (count === 2) { + seg1 = new Segment(arg0); + seg2 = new Segment(arg1); + } else if (count === 4) { + point1 = arg0; + handle1 = arg1; + handle2 = arg2; + point2 = arg3; + } else if (count === 8) { + point1 = [arg0, arg1]; + point2 = [arg6, arg7]; + handle1 = [arg2 - arg0, arg3 - arg1]; + handle2 = [arg4 - arg6, arg5 - arg7]; } + this._segment1 = seg1 || new Segment(point1, null, handle1); + this._segment2 = seg2 || new Segment(point2, handle2, null); + }, + + _serialize: function(options) { + return Base.serialize(this.hasHandles() + ? [this.getPoint1(), this.getHandle1(), this.getHandle2(), + this.getPoint2()] + : [this.getPoint1(), this.getPoint2()], + options, true); }, _changed: function() { this._length = this._bounds = undefined; }, + clone: function() { + return new Curve(this._segment1, this._segment2); + }, + + toString: function() { + var parts = [ 'point1: ' + this._segment1._point ]; + if (!this._segment1._handleOut.isZero()) + parts.push('handle1: ' + this._segment1._handleOut); + if (!this._segment2._handleIn.isZero()) + parts.push('handle2: ' + this._segment2._handleIn); + parts.push('point2: ' + this._segment2._point); + return '{ ' + parts.join(', ') + ' }'; + }, + + remove: function() { + var removed = false; + if (this._path) { + var segment2 = this._segment2, + handleOut = segment2._handleOut; + removed = segment2.remove(); + if (removed) + this._segment1._handleOut.set(handleOut.x, handleOut.y); + } + return removed; + }, + getPoint1: function() { return this._segment1._point; }, @@ -5608,6 +5607,16 @@ var Curve = Base.extend({ || this._path._closed && curves[curves.length - 1]) || null; }, + isFirst: function() { + return this._segment1._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._segment1._index === path._curves.length - 1 + || false; + }, + isSelected: function() { return this.getPoint1().isSelected() && this.getHandle2().isSelected() @@ -5635,11 +5644,8 @@ var Curve = Base.extend({ }, getLength: function() { - if (this._length == null) { - this._length = this.isLinear() - ? this._segment2._point.getDistance(this._segment1._point) - : Curve.getLength(this.getValues(), 0, 1); - } + if (this._length == null) + this._length = Curve.getLength(this.getValues(), 0, 1); return this._length; }, @@ -5647,6 +5653,10 @@ var Curve = Base.extend({ return Curve.getArea(this.getValues()); }, + getLine: function() { + return new Line(this._segment1._point, this._segment2._point); + }, + getPart: function(from, to) { return new Curve(Curve.getPart(this.getValues(), from, to)); }, @@ -5655,27 +5665,10 @@ var Curve = Base.extend({ return Curve.getLength(this.getValues(), from, to); }, - hasHandles: function() { - return !this._segment1._handleOut.isZero() - || !this._segment2._handleIn.isZero(); - }, - - isLinear: function() { - return Segment.isLinear(this._segment1, this._segment2); - }, - - isCollinear: function(curve) { - return Ssegment.isCollinear(this._segment1, this._segment2, - curve._segment1, curve._segment2); - }, - - isOrthogonalArc: function() { - return Segment.isOrthogonalArc(this._segment1, this._segment2); - }, - getIntersections: function(curve) { - return Curve.filterIntersections(Curve.getIntersections( - this.getValues(), curve.getValues(), this, curve, [])); + return Curve._getIntersections(this.getValues(), + curve && curve !== this ? curve.getValues() : null, + this, curve, [], {}); }, _getParameter: function(offset, isParameter) { @@ -5688,39 +5681,35 @@ var Curve = Base.extend({ : this.getParameterAt(offset, 0); }, - divide: function(offset, isParameter, ignoreLinear) { + divide: function(offset, isParameter, _setHandles) { var parameter = this._getParameter(offset, isParameter), - tolerance = 0.000001, + tMin = 4e-7, + tMax = 1 - tMin, res = null; - if (parameter > tolerance && parameter < 1 - tolerance) { + if (parameter >= tMin && parameter <= tMax) { var parts = Curve.subdivide(this.getValues(), parameter), - isLinear = ignoreLinear ? false : this.isLinear(), left = parts[0], - right = parts[1]; - - if (!isLinear) { - this._segment1._handleOut.set(left[2] - left[0], + right = parts[1], + setHandles = _setHandles || this.hasHandles(), + segment1 = this._segment1, + segment2 = this._segment2, + path = this._path; + if (setHandles) { + segment1._handleOut.set(left[2] - left[0], left[3] - left[1]); - this._segment2._handleIn.set(right[4] - right[6], + segment2._handleIn.set(right[4] - right[6], right[5] - right[7]); } - var x = left[6], y = left[7], segment = new Segment(new Point(x, y), - !isLinear && new Point(left[4] - x, left[5] - y), - !isLinear && new Point(right[2] - x, right[3] - y)); - - if (this._path) { - if (this._segment1._index > 0 && this._segment2._index === 0) { - this._path.add(segment); - } else { - this._path.insert(this._segment2._index, segment); - } - res = this; + setHandles && new Point(left[4] - x, left[5] - y), + setHandles && new Point(right[2] - x, right[3] - y)); + if (path) { + path.insert(segment1._index + 1, segment); + res = this.getNext(); } else { - var end = this._segment2; this._segment2 = segment; - res = new Curve(segment, end); + res = new Curve(segment, segment2); } } return res; @@ -5733,34 +5722,13 @@ var Curve = Base.extend({ : null; }, - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); + reversed: function() { + return new Curve(this._segment2.reversed(), this._segment1.reversed()); }, - remove: function() { - var removed = false; - if (this._path) { - var segment2 = this._segment2, - handleOut = segment2._handleOut; - removed = segment2.remove(); - if (removed) - this._segment1._handleOut.set(handleOut.x, handleOut.y); - } - return removed; - }, - - clone: function() { - return new Curve(this._segment1, this._segment2); - }, - - toString: function() { - var parts = [ 'point1: ' + this._segment1._point ]; - if (!this._segment1._handleOut.isZero()) - parts.push('handle1: ' + this._segment1._handleOut); - if (!this._segment2._handleIn.isZero()) - parts.push('handle2: ' + this._segment2._handleIn); - parts.push('point2: ' + this._segment2._point); - return '{ ' + parts.join(', ') + ' }'; + clearHandles: function() { + this._segment1._handleOut.set(0, 0); + this._segment2._handleIn.set(0, 0); }, statics: { @@ -5811,43 +5779,86 @@ statics: { return Numerical.solveCubic(a, b, c, p1 - val, roots, min, max); }, - getParameterOf: function(v, x, y) { - var tolerance = 0.000001; - if (Math.abs(v[0] - x) < tolerance && Math.abs(v[1] - y) < tolerance) - return 0; - if (Math.abs(v[6] - x) < tolerance && Math.abs(v[7] - y) < tolerance) - return 1; - var txs = [], - tys = [], - sx = Curve.solveCubic(v, 0, x, txs, 0, 1), - sy = Curve.solveCubic(v, 1, y, tys, 0, 1), - tx, ty; - for (var cx = 0; sx === -1 || cx < sx;) { - if (sx === -1 || (tx = txs[cx++]) > 0 && tx < 1) { - for (var cy = 0; sy === -1 || cy < sy;) { - if (sy === -1 || (ty = tys[cy++]) > 0 && ty < 1) { - if (sx === -1) { - tx = ty; - } else if (sy === -1) { - ty = tx; - } - if (Math.abs(tx - ty) < tolerance) - return (tx + ty) * 0.5; - } - } - if (sx === -1) - break; + getParameterOf: function(v, point) { + var p1 = new Point(v[0], v[1]), + p2 = new Point(v[6], v[7]), + epsilon = 1e-12, + t = point.isClose(p1, epsilon) ? 0 + : point.isClose(p2, epsilon) ? 1 + : null; + if (t !== null) + return t; + var coords = [point.x, point.y], + roots = [], + geomEpsilon = 2e-7; + for (var c = 0; c < 2; c++) { + var count = Curve.solveCubic(v, c, coords[c], roots, 0, 1); + for (var i = 0; i < count; i++) { + t = roots[i]; + if (point.isClose(Curve.getPoint(v, t), geomEpsilon)) + return t; } } - return null; + return point.isClose(p1, geomEpsilon) ? 0 + : point.isClose(p2, geomEpsilon) ? 1 + : null; + }, + + getNearestParameter: function(v, point) { + if (Curve.isStraight(v)) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + vx = p2x - p1x, vy = p2y - p1y, + det = vx * vx + vy * vy; + if (det === 0) + return 0; + var u = ((point.x - p1x) * vx + (point.y - p1y) * vy) / det; + return u < 1e-12 ? 0 + : u > 0.999999999999 ? 1 + : Curve.getParameterOf(v, + new Point(p1x + u * vx, p1y + u * vy)); + } + + var count = 100, + minDist = Infinity, + minT = 0; + + function refine(t) { + if (t >= 0 && t <= 1) { + var dist = point.getDistance(Curve.getPoint(v, t), true); + if (dist < minDist) { + minDist = dist; + minT = t; + return true; + } + } + } + + for (var i = 0; i <= count; i++) + refine(i / count); + + var step = 1 / (count * 2); + while (step > 4e-7) { + if (!refine(minT - step) && !refine(minT + step)) + step /= 2; + } + return minT; }, getPart: function(v, from, to) { + var flip = from > to; + if (flip) { + var tmp = from; + from = to; + to = tmp; + } if (from > 0) v = Curve.subdivide(v, from)[1]; if (to < 1) v = Curve.subdivide(v, (to - from) / (1 - from))[0]; - return v; + return flip + ? [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]] + : v; }, hasHandles: function(v) { @@ -5856,14 +5867,6 @@ statics: { && isZero(v[4] - v[6]) && isZero(v[5] - v[7])); }, - isLinear: function(v) { - var p1x = v[0], p1y = v[1], - p2x = v[6], p2y = v[7], - l = new Point(p2x - p1x, p2y - p1y); - return l.isCollinear(new Point(v[2] - p1x, v[3] - p1y)) - && l.isCollinear(new Point(v[4] - p2x, v[5] - p2y)); - }, - isFlatEnough: function(v, tolerance) { var p1x = v[0], p1y = v[1], c1x = v[2], c1y = v[3], @@ -5879,21 +5882,14 @@ statics: { getArea: function(v) { var p1x = v[0], p1y = v[1], - c1x = v[2], c1y = v[3], - c2x = v[4], c2y = v[5], - p2x = v[6], p2y = v[7]; - return ( 3.0 * c1y * p1x - 1.5 * c1y * c2x - - 1.5 * c1y * p2x - 3.0 * p1y * c1x - - 1.5 * p1y * c2x - 0.5 * p1y * p2x - + 1.5 * c2y * p1x + 1.5 * c2y * c1x - - 3.0 * c2y * p2x + 0.5 * p2y * p1x - + 1.5 * p2y * c1x + 3.0 * p2y * c2x) / 10; - }, - - getEdgeSum: function(v) { - return (v[0] - v[2]) * (v[3] + v[1]) - + (v[2] - v[4]) * (v[5] + v[3]) - + (v[4] - v[6]) * (v[7] + v[5]); + p2x = v[6], p2y = v[7], + h1x = (v[2] + p1x) / 2, + h1y = (v[3] + p1y) / 2, + h2x = (v[4] + v[6]) / 2, + h2y = (v[5] + v[7]) / 2; + return 6 * ((p1x - h1x) * (h1y + p1y) + + (h1x - h2x) * (h2y + h1y) + + (h2x - p2x) * (p2y + h2y)) / 10; }, getBounds: function(v) { @@ -5919,7 +5915,7 @@ statics: { b = 2 * (v0 + v2) - 4 * v1, c = v1 - v0, count = Numerical.solveQuadratic(a, b, c, roots), - tMin = 0.000001, + tMin = 4e-7, tMax = 1 - tMin; add(v3, 0); for (var i = 0; i < count; i++) { @@ -5941,14 +5937,72 @@ statics: { this._bounds = {}; var bounds = this._bounds[name]; if (!bounds) { - bounds = this._bounds[name] = Path[name]([this._segment1, - this._segment2], false, this._path.getStyle()); + var path = this._path; + bounds = this._bounds[name] = Path[name]( + [this._segment1, this._segment2], false, + path && path.getStyle()); } return bounds.clone(); }; }, { +}), Base.each({ + isStraight: function(l, h1, h2) { + if (h1.isZero() && h2.isZero()) { + return true; + } else if (l.isZero()) { + return false; + } else if (h1.isCollinear(l) && h2.isCollinear(l)) { + var div = l.dot(l), + p1 = l.dot(h1) / div, + p2 = l.dot(h2) / div; + return p1 >= 0 && p1 <= 1 && p2 <= 0 && p2 >= -1; + } + return false; + }, + + isLinear: function(l, h1, h2) { + var third = l.divide(3); + return h1.equals(third) && h2.negate().equals(third); + } +}, function(test, name) { + this[name] = function() { + var seg1 = this._segment1, + seg2 = this._segment2; + return test(seg2._point.subtract(seg1._point), + seg1._handleOut, seg2._handleIn); + }; + + this.statics[name] = function(v) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7]; + return test(new Point(p2x - p1x, p2y - p1y), + new Point(v[2] - p1x, v[3] - p1y), + new Point(v[4] - p2x, v[5] - p2y)); + }; +}, { + statics: {}, + + hasHandles: function() { + return !this._segment1._handleOut.isZero() + || !this._segment2._handleIn.isZero(); + }, + + isCollinear: function(curve) { + return curve && this.isStraight() && curve.isStraight() + && this.getLine().isCollinear(curve.getLine()); + }, + + isHorizontal: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).y) + < 1e-7; + }, + + isVertical: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).x) + < 1e-7; + } }), { beans: false, @@ -5957,8 +6011,7 @@ statics: { }, getParameterOf: function() { - var point = Point.read(arguments); - return Curve.getParameterOf(this.getValues(), point.x, point.y); + return Curve.getParameterOf(this.getValues(), Point.read(arguments)); }, getLocationAt: function(offset, isParameter) { @@ -5981,32 +6034,9 @@ statics: { getNearestLocation: function() { var point = Point.read(arguments), values = this.getValues(), - count = 100, - minDist = Infinity, - minT = 0; - - function refine(t) { - if (t >= 0 && t <= 1) { - var dist = point.getDistance(Curve.getPoint(values, t), true); - if (dist < minDist) { - minDist = dist; - minT = t; - return true; - } - } - } - - for (var i = 0; i <= count; i++) - refine(i / count); - - var step = 1 / (count * 2); - while (step > 0.000001) { - if (!refine(minT - step) && !refine(minT + step)) - step /= 2; - } - var pt = Curve.getPoint(values, minT); - return new CurveLocation(this, minT, pt, null, null, null, - point.getDistance(pt)); + t = Curve.getNearestParameter(values, point), + pt = Curve.getPoint(values, t); + return new CurveLocation(this, t, pt, null, point.getDistance(pt)); }, getNearestPoint: function() { @@ -6064,11 +6094,12 @@ new function() { c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], - tolerance = 0.000001, + tMin = 4e-7, + tMax = 1 - tMin, x, y; - if (type === 0 && (t < tolerance || t > 1 - tolerance)) { - var isZero = t < tolerance; + if (type === 0 && (t < tMin || t > tMax)) { + var isZero = t < tMin; x = isZero ? p1x : p2x; y = isZero ? p1y : p2y; } else { @@ -6083,10 +6114,10 @@ new function() { x = ((ax * t + bx) * t + cx) * t + p1x; y = ((ay * t + by) * t + cy) * t + p1y; } else { - if (t < tolerance) { + if (t < tMin) { x = cx; y = cy; - } else if (t > 1 - tolerance) { + } else if (t > tMax) { x = 3 * (p2x - c2x); y = 3 * (p2y - c2y); } else { @@ -6094,14 +6125,15 @@ new function() { y = (3 * ay * t + 2 * by) * t + cy; } if (normalized) { - if (x === 0 && y === 0 - && (t < tolerance || t > 1 - tolerance)) { + if (x === 0 && y === 0 && (t < tMin || t > tMax)) { x = c2x - c1x; y = c2y - c1y; } var len = Math.sqrt(x * x + y * y); - x /= len; - y /= len; + if (len) { + x /= len; + y /= len; + } } if (type === 3) { var x2 = 6 * ax * t + 2 * bx, @@ -6115,18 +6147,14 @@ new function() { return type === 2 ? new Point(y, -x) : new Point(x, y); } - return { - statics: true, + return { statics: { getLength: function(v, a, b) { if (a === undefined) a = 0; if (b === undefined) b = 1; - var isZero = Numerical.isZero; - if (a === 0 && b === 1 - && isZero(v[0] - v[2]) && isZero(v[1] - v[3]) - && isZero(v[6] - v[4]) && isZero(v[7] - v[5])) { + if (a === 0 && b === 1 && Curve.isStraight(v)) { var dx = v[6] - v[0], dy = v[7] - v[1]; return Math.sqrt(dx * dx + dy * dy); @@ -6140,15 +6168,14 @@ new function() { start = offset < 0 ? 1 : 0 if (offset === 0) return start; - var tolerance = 0.000001, - abs = Math.abs, + var abs = Math.abs, forward = offset > 0, a = forward ? start : 0, b = forward ? 1 : start, ds = getLengthIntegrand(v), rangeLength = Numerical.integrate(ds, a, b, getIterations(a, b)); - if (abs(offset - rangeLength) < tolerance) { + if (abs(offset - rangeLength) < 1e-12) { return forward ? b : a; } else if (abs(offset) > rangeLength) { return null; @@ -6161,8 +6188,8 @@ new function() { start = t; return length - offset; } - return Numerical.findRoot(f, ds, start + guess, a, b, 16, - tolerance); + return Numerical.findRoot(f, ds, start + guess, a, b, 32, + 1e-12); }, getPoint: function(v, t) { @@ -6188,24 +6215,55 @@ new function() { getCurvature: function(v, t) { return evaluate(v, t, 3, false).x; } - }; -}, new function() { - function addLocation(locations, include, curve1, t1, point1, curve2, t2, - point2) { - var loc = new CurveLocation(curve1, t1, point1, curve2, t2, point2); - if (!include || include(loc)) - locations.push(loc); + }}; +}, +new function() { + + function addLocation(locations, param, v1, c1, t1, p1, v2, c2, t2, p2, + overlap) { + var startConnected = param.startConnected, + endConnected = param.endConnected, + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 == null) + t1 = Curve.getParameterOf(v1, p1); + if (t1 !== null && t1 >= (startConnected ? tMin : 0) && + t1 <= (endConnected ? tMax : 1)) { + if (t2 == null) + t2 = Curve.getParameterOf(v2, p2); + if (t2 !== null && t2 >= (endConnected ? tMin : 0) && + t2 <= (startConnected ? tMax : 1)) { + var renormalize = param.renormalize; + if (renormalize) { + var res = renormalize(t1, t2); + t1 = res[0]; + t2 = res[1]; + } + var loc1 = new CurveLocation(c1, t1, + p1 || Curve.getPoint(v1, t1), overlap), + loc2 = new CurveLocation(c2, t2, + p2 || Curve.getPoint(v2, t2), overlap), + flip = loc1.getPath() === loc2.getPath() + && loc1.getIndex() > loc2.getIndex(), + loc = flip ? loc2 : loc1, + include = param.include; + loc1._intersection = loc2; + loc2._intersection = loc1; + if (!include || include(loc)) { + CurveLocation.insert(locations, loc, true); + } + } + } } - function addCurveIntersections(v1, v2, curve1, curve2, locations, include, + function addCurveIntersections(v1, v2, c1, c2, locations, param, tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { - if (recursion > 32) + if (++recursion >= 24) return; var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], - tolerance = 0.000001, getSignedDistance = Line.getSignedDistance, - d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]) || 0, - d2 = getSignedDistance(q0x, q0y, q3x, q3y, v2[4], v2[5]) || 0, + d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]), + d2 = getSignedDistance(q0x, q0y, q3x, q3y, v2[4], v2[5]), factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, dMin = factor * Math.min(0, d1, d2), dMax = factor * Math.max(0, d1, d2), @@ -6213,61 +6271,51 @@ new function() { dp1 = getSignedDistance(q0x, q0y, q3x, q3y, v1[2], v1[3]), dp2 = getSignedDistance(q0x, q0y, q3x, q3y, v1[4], v1[5]), dp3 = getSignedDistance(q0x, q0y, q3x, q3y, v1[6], v1[7]), - tMinNew, tMaxNew, tDiff; - if (q0x === q3x && uMax - uMin < tolerance && recursion > 3) { - tMaxNew = tMinNew = (tMax + tMin) / 2; - tDiff = 0; - } else { - var hull = getConvexHull(dp0, dp1, dp2, dp3), - top = hull[0], - bottom = hull[1], - tMinClip, tMaxClip; - tMinClip = clipConvexHull(top, bottom, dMin, dMax); - top.reverse(); - bottom.reverse(); - tMaxClip = clipConvexHull(top, bottom, dMin, dMax); - if (tMinClip == null || tMaxClip == null) - return; - v1 = Curve.getPart(v1, tMinClip, tMaxClip); - tDiff = tMaxClip - tMinClip; - tMinNew = tMax * tMinClip + tMin * (1 - tMinClip); - tMaxNew = tMax * tMaxClip + tMin * (1 - tMaxClip); - } + hull = getConvexHull(dp0, dp1, dp2, dp3), + top = hull[0], + bottom = hull[1], + tMinClip, + tMaxClip; + if ((tMinClip = clipConvexHull(top, bottom, dMin, dMax)) == null || + (tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(), + dMin, dMax)) == null) + return; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + var tDiff = tMaxClip - tMinClip, + tMinNew = tMin + (tMax - tMin) * tMinClip, + tMaxNew = tMin + (tMax - tMin) * tMaxClip; if (oldTDiff > 0.5 && tDiff > 0.5) { if (tMaxNew - tMinNew > uMax - uMin) { var parts = Curve.subdivide(v1, 0.5), t = tMinNew + (tMaxNew - tMinNew) / 2; addCurveIntersections( - v2, parts[0], curve2, curve1, locations, include, - uMin, uMax, tMinNew, t, tDiff, !reverse, ++recursion); + v2, parts[0], c2, c1, locations, param, + uMin, uMax, tMinNew, t, tDiff, !reverse, recursion); addCurveIntersections( - v2, parts[1], curve2, curve1, locations, include, + v2, parts[1], c2, c1, locations, param, uMin, uMax, t, tMaxNew, tDiff, !reverse, recursion); } else { var parts = Curve.subdivide(v2, 0.5), t = uMin + (uMax - uMin) / 2; addCurveIntersections( - parts[0], v1, curve2, curve1, locations, include, - uMin, t, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); + parts[0], v1, c2, c1, locations, param, + uMin, t, tMinNew, tMaxNew, tDiff, !reverse, recursion); addCurveIntersections( - parts[1], v1, curve2, curve1, locations, include, + parts[1], v1, c2, c1, locations, param, t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } - } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) < tolerance) { + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) + < 1e-7) { var t1 = tMinNew + (tMaxNew - tMinNew) / 2, t2 = uMin + (uMax - uMin) / 2; - if (reverse) { - addLocation(locations, include, - curve2, t2, Curve.getPoint(v2, t2), - curve1, t1, Curve.getPoint(v1, t1)); - } else { - addLocation(locations, include, - curve1, t1, Curve.getPoint(v1, t1), - curve2, t2, Curve.getPoint(v2, t2)); - } - } else if (tDiff > 0) { - addCurveIntersections(v2, v1, curve2, curve1, locations, include, - uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); + v1 = c1.getValues(); + v2 = c2.getValues(); + addLocation(locations, param, + reverse ? v2 : v1, reverse ? c2 : c1, reverse ? t2 : t1, null, + reverse ? v1 : v2, reverse ? c1 : c2, reverse ? t1 : t2, null); + } else if (tDiff > 1e-12) { + addCurveIntersections(v2, v1, c2, c1, locations, param, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } } @@ -6276,32 +6324,21 @@ new function() { p1 = [ 1 / 3, dq1 ], p2 = [ 2 / 3, dq2 ], p3 = [ 1, dq3 ], - getSignedDistance = Line.getSignedDistance, - dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), - flip = false, + dist1 = dq1 - (2 * dq0 + dq3) / 3, + dist2 = dq2 - (dq0 + 2 * dq3) / 3, hull; if (dist1 * dist2 < 0) { hull = [[p0, p1, p3], [p0, p2, p3]]; - flip = dist1 < 0; } else { - var pmax, cross = 0, - distZero = dist1 === 0 || dist2 === 0; - if (Math.abs(dist1) > Math.abs(dist2)) { - pmax = p1; - cross = (dq3 - dq2 - (dq3 - dq0) / 3) - * (2 * (dq3 - dq2) - dq3 + dq1) / 3; - } else { - pmax = p2; - cross = (dq1 - dq0 + (dq0 - dq3) / 3) - * (-2 * (dq0 - dq1) + dq0 - dq2) / 3; - } - hull = cross < 0 || distZero - ? [[p0, pmax, p3], [p0, p3]] - : [[p0, p1, p2, p3], [p0, p3]]; - flip = dist1 ? dist1 < 0 : dist2 < 0; + var distRatio = dist1 / dist2; + hull = [ + distRatio >= 2 ? [p0, p1, p3] + : distRatio <= .5 ? [p0, p2, p3] + : [p0, p1, p2, p3], + [p0, p3] + ]; } - return flip ? hull.reverse() : hull; + return (dist1 || dist2) < 0 ? hull.reverse() : hull; } function clipConvexHull(hullTop, hullBottom, dMin, dMax) { @@ -6320,17 +6357,18 @@ new function() { for (var i = 1, l = part.length; i < l; i++) { var qx = part[i][0], qy = part[i][1]; - if (top ? qy >= threshold : qy <= threshold) - return px + (threshold - py) * (qx - px) / (qy - py); + if (top ? qy >= threshold : qy <= threshold) { + return qy === threshold ? qx + : px + (threshold - py) * (qx - px) / (qy - py); + } px = qx; py = qy; } return null; } - function addCurveLineIntersections(v1, v2, curve1, curve2, locations, - include) { - var flip = Curve.isLinear(v1), + function addCurveLineIntersections(v1, v2, c1, c2, locations, param) { + var flip = Curve.isStraight(v1), vc = flip ? v2 : v1, vl = flip ? v1 : v2, lx1 = vl[0], ly1 = vl[1], @@ -6340,113 +6378,221 @@ new function() { angle = Math.atan2(-ldy, ldx), sin = Math.sin(angle), cos = Math.cos(angle), - rlx2 = ldx * cos - ldy * sin, - rvl = [0, 0, 0, 0, rlx2, 0, rlx2, 0], rvc = []; for(var i = 0; i < 8; i += 2) { var x = vc[i] - lx1, y = vc[i + 1] - ly1; rvc.push( x * cos - y * sin, - y * cos + x * sin); + x * sin + y * cos); } var roots = [], count = Curve.solveCubic(rvc, 1, 0, roots, 0, 1); for (var i = 0; i < count; i++) { var tc = roots[i], - x = Curve.getPoint(rvc, tc).x; - if (x >= 0 && x <= rlx2) { - var tl = Curve.getParameterOf(rvl, x, 0), + pc = Curve.getPoint(vc, tc), + tl = Curve.getParameterOf(vl, pc); + if (tl !== null) { + var pl = Curve.getPoint(vl, tl), t1 = flip ? tl : tc, t2 = flip ? tc : tl; - addLocation(locations, include, - curve1, t1, Curve.getPoint(v1, t1), - curve2, t2, Curve.getPoint(v2, t2)); + if (!param.endConnected || t2 > Numerical.CURVETIME_EPSILON) { + addLocation(locations, param, + v1, c1, t1, flip ? pl : pc, + v2, c2, t2, flip ? pc : pl); + } } } } - function addLineIntersection(v1, v2, curve1, curve2, locations, include) { - var point = Line.intersect( + function addLineIntersection(v1, v2, c1, c2, locations, param) { + var pt = Line.intersect( v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]); - if (point) { - var x = point.x, - y = point.y; - addLocation(locations, include, - curve1, Curve.getParameterOf(v1, x, y), point, - curve2, Curve.getParameterOf(v2, x, y), point); + if (pt) { + addLocation(locations, param, v1, c1, null, pt, v2, c2, null, pt); } } return { statics: { - getIntersections: function(v1, v2, c1, c2, locations, include) { - var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2), - c1p1 = c1.getPoint1(), - c1p2 = c1.getPoint2(), - c2p1 = c2.getPoint1(), - c2p2 = c2.getPoint2(), - tolerance = 0.000001; - if (c1p1.isClose(c2p1, tolerance)) - addLocation(locations, include, c1, 0, c1p1, c2, 0, c1p1); - if (c1p1.isClose(c2p2, tolerance)) - addLocation(locations, include, c1, 0, c1p1, c2, 1, c1p1); - (linear1 && linear2 + _getIntersections: function(v1, v2, c1, c2, locations, param) { + if (!v2) { + return Curve._getSelfIntersection(v1, c1, locations, param); + } + var c1p1x = v1[0], c1p1y = v1[1], + c1p2x = v1[6], c1p2y = v1[7], + c2p1x = v2[0], c2p1y = v2[1], + c2p2x = v2[6], c2p2y = v2[7], + c1s1x = (3 * v1[2] + c1p1x) / 4, + c1s1y = (3 * v1[3] + c1p1y) / 4, + c1s2x = (3 * v1[4] + c1p2x) / 4, + c1s2y = (3 * v1[5] + c1p2y) / 4, + c2s1x = (3 * v2[2] + c2p1x) / 4, + c2s1y = (3 * v2[3] + c2p1y) / 4, + c2s2x = (3 * v2[4] + c2p2x) / 4, + c2s2y = (3 * v2[5] + c2p2y) / 4, + min = Math.min, + max = Math.max; + if (!( max(c1p1x, c1s1x, c1s2x, c1p2x) >= + min(c2p1x, c2s1x, c2s2x, c2p2x) && + min(c1p1x, c1s1x, c1s2x, c1p2x) <= + max(c2p1x, c2s1x, c2s2x, c2p2x) && + max(c1p1y, c1s1y, c1s2y, c1p2y) >= + min(c2p1y, c2s1y, c2s2y, c2p2y) && + min(c1p1y, c1s1y, c1s2y, c1p2y) <= + max(c2p1y, c2s1y, c2s2y, c2p2y))) + return locations; + if (!param.startConnected && !param.endConnected) { + var overlaps = Curve.getOverlaps(v1, v2); + if (overlaps) { + for (var i = 0; i < 2; i++) { + var overlap = overlaps[i]; + addLocation(locations, param, + v1, c1, overlap[0], null, + v2, c2, overlap[1], null, true); + } + return locations; + } + } + + var straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2, + epsilon = 1e-12, + before = locations.length; + (straight ? addLineIntersection - : linear1 || linear2 + : straight1 || straight2 ? addCurveLineIntersections : addCurveIntersections)( - v1, v2, c1, c2, locations, include, + v1, v2, c1, c2, locations, param, 0, 1, 0, 1, 0, false, 0); - if (c1p2.isClose(c2p1, tolerance)) - addLocation(locations, include, c1, 1, c1p2, c2, 0, c1p2); - if (c1p2.isClose(c2p2, tolerance)) - addLocation(locations, include, c1, 1, c1p2, c2, 1, c1p2); + if (straight && locations.length > before) + return locations; + var c1p1 = new Point(c1p1x, c1p1y), + c1p2 = new Point(c1p2x, c1p2y), + c2p1 = new Point(c2p1x, c2p1y), + c2p2 = new Point(c2p2x, c2p2y); + if (c1p1.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 0, c2p1); + if (!param.startConnected && c1p1.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 1, c2p2); + if (!param.endConnected && c1p2.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 0, c2p1); + if (c1p2.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 1, c2p2); return locations; }, - filterIntersections: function(locations, _expand) { - var last = locations.length - 1, - tMax = 1 - 0.000001; - for (var i = last; i >= 0; i--) { - var loc = locations[i], - next = loc._curve.getNext(), - next2 = loc._curve2.getNext(); - if (next && loc._parameter >= tMax) { - loc._parameter = 0; - loc._curve = next; - } - if (next2 && loc._parameter2 >= tMax) { - loc._parameter2 = 0; - loc._curve2 = next2; - } + _getSelfIntersection: function(v1, c1, locations, param) { + var p1x = v1[0], p1y = v1[1], + h1x = v1[2], h1y = v1[3], + h2x = v1[4], h2y = v1[5], + p2x = v1[6], p2y = v1[7]; + var line = new Line(p1x, p1y, p2x, p2y, false), + side1 = line.getSide(new Point(h1x, h1y), true), + side2 = line.getSide(new Point(h2x, h2y), true); + if (side1 === side2) { + var edgeSum = (p1x - h2x) * (h1y - p2y) + + (h1x - p2x) * (h2y - p1y); + if (edgeSum * side1 > 0) + return locations; } - - function compare(loc1, loc2) { - var path1 = loc1.getPath(), - path2 = loc2.getPath(); - return path1 === path2 - ? (loc1.getIndex() + loc1.getParameter()) - - (loc2.getIndex() + loc2.getParameter()) - : path1._id - path2._id; - } - - if (last > 0) { - locations.sort(compare); - for (var i = last; i > 0; i--) { - if (locations[i].equals(locations[i - 1])) { - locations.splice(i, 1); - last--; + var ax = p2x - 3 * h2x + 3 * h1x - p1x, + bx = h2x - 2 * h1x + p1x, + cx = h1x - p1x, + ay = p2y - 3 * h2y + 3 * h1y - p1y, + by = h2y - 2 * h1y + p1y, + cy = h1y - p1y, + ac = ay * cx - ax * cy, + ab = ay * bx - ax * by, + bc = by * cx - bx * cy; + if (ac * ac - 4 * ab * bc < 0) { + var roots = [], + tSplit, + count = Numerical.solveCubic( + ax * ax + ay * ay, + 3 * (ax * bx + ay * by), + 2 * (bx * bx + by * by) + ax * cx + ay * cy, + bx * cx + by * cy, + roots, 0, 1); + if (count > 0) { + for (var i = 0, maxCurvature = 0; i < count; i++) { + var curvature = Math.abs( + c1.getCurvatureAt(roots[i], true)); + if (curvature > maxCurvature) { + maxCurvature = curvature; + tSplit = roots[i]; + } } + var parts = Curve.subdivide(v1, tSplit); + param.endConnected = true; + param.renormalize = function(t1, t2) { + return [t1 * tSplit, t2 * (1 - tSplit) + tSplit]; + }; + Curve._getIntersections(parts[0], parts[1], c1, c1, + locations, param); } } - if (_expand) { - for (var i = last; i >= 0; i--) - locations.push(locations[i].getIntersection()); - locations.sort(compare); - } return locations; + }, + + getOverlaps: function(v1, v2) { + var abs = Math.abs, + timeEpsilon = 4e-7, + geomEpsilon = 2e-7, + straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2; + + function getLineLengthSquared(v) { + var x = v[6] - v[0], + y = v[7] - v[1]; + return x * x + y * y; + } + + if (straight) { + var flip = getLineLengthSquared(v1) < getLineLengthSquared(v2), + l1 = flip ? v2 : v1, + l2 = flip ? v1 : v2, + line = new Line(l1[0], l1[1], l1[6], l1[7]); + if (line.getDistance(new Point(l2[0], l2[1])) > geomEpsilon || + line.getDistance(new Point(l2[6], l2[7])) > geomEpsilon) + return null; + } else if (straight1 ^ straight2) { + return null; + } + + var v = [v1, v2], + pairs = []; + for (var i = 0, t1 = 0; + i < 2 && pairs.length < 2; + i += t1 === 0 ? 0 : 1, t1 = t1 ^ 1) { + var t2 = Curve.getParameterOf(v[i ^ 1], new Point( + v[i][t1 === 0 ? 0 : 6], + v[i][t1 === 0 ? 1 : 7])); + if (t2 != null) { + var pair = i === 0 ? [t1, t2] : [t2, t1]; + if (pairs.length === 0 || + abs(pair[0] - pairs[0][0]) > timeEpsilon && + abs(pair[1] - pairs[0][1]) > timeEpsilon) + pairs.push(pair); + } + if (i === 1 && pairs.length === 0) + break; + } + if (pairs.length !== 2) { + pairs = null; + } else if (!straight) { + var o1 = Curve.getPart(v1, pairs[0][0], pairs[1][0]), + o2 = Curve.getPart(v2, pairs[0][1], pairs[1][1]); + if (abs(o2[2] - o1[2]) > geomEpsilon || + abs(o2[3] - o1[3]) > geomEpsilon || + abs(o2[4] - o1[4]) > geomEpsilon || + abs(o2[5] - o1[5]) > geomEpsilon) + pairs = null; + } + return pairs; } }}; }); @@ -6455,58 +6601,82 @@ var CurveLocation = Base.extend({ _class: 'CurveLocation', beans: true, - initialize: function CurveLocation(curve, parameter, point, _curve2, - _parameter2, _point2, _distance) { + initialize: function CurveLocation(curve, parameter, point, + _overlap, _distance) { + if (parameter > 0.9999996) { + var next = curve.getNext(); + if (next) { + parameter = 0; + curve = next; + } + } this._id = UID.get(CurveLocation); + this._setCurve(curve); + this._parameter = parameter; + this._point = point || curve.getPointAt(parameter, true); + this._overlap = _overlap; + this._distance = _distance; + this._intersection = this._next = this._prev = null; + }, + + _setCurve: function(curve) { var path = curve._path; this._version = path ? path._version : 0; this._curve = curve; - this._parameter = parameter; - this._point = point || curve.getPointAt(parameter, true); - this._curve2 = _curve2; - this._parameter2 = _parameter2; - this._point2 = _point2; - this._distance = _distance; + this._segment = null; this._segment1 = curve._segment1; this._segment2 = curve._segment2; }, - getSegment: function(_preferFirst) { - if (!this._segment) { - var curve = this.getCurve(), - parameter = this.getParameter(); - if (parameter === 1) { - this._segment = curve._segment2; - } else if (parameter === 0 || _preferFirst) { - this._segment = curve._segment1; - } else if (parameter == null) { - return null; - } else { - this._segment = curve.getPartLength(0, parameter) + _setSegment: function(segment) { + this._setCurve(segment.getCurve()); + this._segment = segment; + this._parameter = segment === this._segment1 ? 0 : 1; + this._point = segment._point.clone(); + }, + + getSegment: function() { + var curve = this.getCurve(), + segment = this._segment; + if (!segment) { + var parameter = this.getParameter(); + if (parameter === 0) { + segment = curve._segment1; + } else if (parameter === 1) { + segment = curve._segment2; + } else if (parameter != null) { + segment = curve.getPartLength(0, parameter) < curve.getPartLength(parameter, 1) ? curve._segment1 : curve._segment2; } + this._segment = segment; } - return this._segment; + return segment; }, getCurve: function() { var curve = this._curve, - path = curve && curve._path; + path = curve && curve._path, + that = this; if (path && path._version !== this._version) { - curve = null; - this._parameter = null; + curve = this._parameter = this._curve = this._offset = null; } - if (!curve) { - curve = this._segment1.getCurve(); - if (curve.getParameterOf(this._point) == null) - curve = this._segment2.getPrevious().getCurve(); - this._curve = curve; - path = curve._path; - this._version = path ? path._version : 0; + + function trySegment(segment) { + var curve = segment && segment.getCurve(); + if (curve && (that._parameter = curve.getParameterOf(that._point)) + != null) { + that._setCurve(curve); + that._segment = segment; + return curve; + } } - return curve; + + return curve + || trySegment(this._segment) + || trySegment(this._segment1) + || trySegment(this._segment2.getPrevious()); }, getPath: function() { @@ -6532,8 +6702,19 @@ var CurveLocation = Base.extend({ }, getOffset: function() { - var path = this.getPath(); - return path ? path._getOffset(this) : this.getCurveOffset(); + var offset = this._offset; + if (offset == null) { + offset = 0; + var path = this.getPath(), + index = this.getIndex(); + if (path && index != null) { + var curves = path.getCurves(); + for (var i = 0; i < index; i++) + offset += curves[i].getLength(); + } + this._offset = offset += this.getCurveOffset(); + } + return offset; }, getCurveOffset: function() { @@ -6543,13 +6724,7 @@ var CurveLocation = Base.extend({ }, getIntersection: function() { - var intersection = this._intersection; - if (!intersection && this._curve2) { - this._intersection = intersection = new CurveLocation(this._curve2, - this._parameter2, this._point2 || this._point, this); - intersection._intersection = this; - } - return intersection; + return this._intersection; }, getDistance: function() { @@ -6557,25 +6732,44 @@ var CurveLocation = Base.extend({ }, divide: function() { - var curve = this.getCurve(); - return curve && curve.divide(this.getParameter(), true); + var curve = this.getCurve(), + res = null; + if (curve) { + res = curve.divide(this.getParameter(), true); + if (res) + this._setSegment(res._segment1); + } + return res; }, split: function() { var curve = this.getCurve(); - return curve && curve.split(this.getParameter(), true); + return curve ? curve.split(this.getParameter(), true) : null; }, - equals: function(loc) { - var abs = Math.abs, - tolerance = 0.000001; - return this === loc - || loc instanceof CurveLocation - && this.getCurve() === loc.getCurve() - && abs(this.getParameter() - loc.getParameter()) < tolerance - && this._curve2 === loc._curve2 - && abs(this._parameter2 - loc._parameter2) < tolerance - || false; + equals: function(loc, _ignoreOther) { + var res = this === loc, + epsilon = 2e-7; + if (!res && loc instanceof CurveLocation + && this.getPath() === loc.getPath() + && this.getPoint().isClose(loc.getPoint(), epsilon)) { + var c1 = this.getCurve(), + c2 = loc.getCurve(), + abs = Math.abs, + diff = abs( + ((c1.isLast() && c2.isFirst() ? -1 : c1.getIndex()) + + this.getParameter()) - + ((c2.isLast() && c1.isFirst() ? -1 : c2.getIndex()) + + loc.getParameter())); + res = (diff < 4e-7 + || ((diff = abs(this.getOffset() - loc.getOffset())) < epsilon + || abs(this.getPath().getLength() - diff) < epsilon)) + && (_ignoreOther + || (!this._intersection && !loc._intersection + || this._intersection && this._intersection.equals( + loc._intersection, true))); + } + return res; }, toString: function() { @@ -6593,17 +6787,123 @@ var CurveLocation = Base.extend({ if (this._distance != null) parts.push('distance: ' + f.number(this._distance)); return '{ ' + parts.join(', ') + ' }'; + }, + + isTouching: function() { + var inter = this._intersection; + if (inter && this.getTangent().isCollinear(inter.getTangent())) { + var curve1 = this.getCurve(), + curve2 = inter.getCurve(); + return !(curve1.isStraight() && curve2.isStraight() + && curve1.getLine().intersect(curve2.getLine())); + } + return false; + }, + + isCrossing: function() { + var inter = this._intersection; + if (!inter) + return false; + var t1 = this.getParameter(), + t2 = inter.getParameter(), + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 >= tMin && t1 <= tMax || t2 >= tMin && t2 <= tMax) + return !this.isTouching(); + var c2 = this.getCurve(), + c1 = c2.getPrevious(), + c4 = inter.getCurve(), + c3 = c4.getPrevious(), + PI = Math.PI; + if (!c1 || !c3) + return false; + + function isInRange(angle, min, max) { + return min < max + ? angle > min && angle < max + : angle > min && angle <= PI || angle >= -PI && angle < max; + } + + var a1 = c1.getTangentAt(tMax, true).negate().getAngleInRadians(), + a2 = c2.getTangentAt(tMin, true).getAngleInRadians(), + a3 = c3.getTangentAt(tMax, true).negate().getAngleInRadians(), + a4 = c4.getTangentAt(tMin, true).getAngleInRadians(); + + return (isInRange(a3, a1, a2) ^ isInRange(a4, a1, a2)) + && (isInRange(a3, a2, a1) ^ isInRange(a4, a2, a1)); + }, + + isOverlap: function() { + return !!this._overlap; } }, Base.each(Curve.evaluateMethods, function(name) { - if (name !== 'getPoint') { - var get = name + 'At'; - this[name] = function() { - var parameter = this.getParameter(), - curve = this.getCurve(); - return parameter != null && curve && curve[get](parameter, true); - }; + var get = name + 'At'; + this[name] = function() { + var parameter = this.getParameter(), + curve = this.getCurve(); + return parameter != null && curve && curve[get](parameter, true); + }; +}, { + preserve: true +}), +new function() { + + function insert(locations, loc, merge) { + var length = locations.length, + l = 0, + r = length - 1; + + function search(index, dir) { + for (var i = index + dir; i >= -1 && i <= length; i += dir) { + var loc2 = locations[((i % length) + length) % length]; + if (!loc.getPoint().isClose(loc2.getPoint(), + 2e-7)) + break; + if (loc.equals(loc2)) + return loc2; + } + return null; + } + + while (l <= r) { + var m = (l + r) >>> 1, + loc2 = locations[m], + found; + if (merge && (found = loc.equals(loc2) ? loc2 + : (search(m, -1) || search(m, 1)))) { + if (loc._overlap) { + found._overlap = found._intersection._overlap = true; + } + return found; + } + var path1 = loc.getPath(), + path2 = loc2.getPath(), + diff = path1 === path2 + ? (loc.getIndex() + loc.getParameter()) + - (loc2.getIndex() + loc2.getParameter()) + : path1._id - path2._id; + if (diff < 0) { + r = m - 1; + } else { + l = m + 1; + } + } + locations.splice(l, 0, loc); + return loc; } -}, {})); + + return { statics: { + insert: insert, + + expand: function(locations) { + var expanded = locations.slice(); + for (var i = 0, l = locations.length; i < l; i++) { + insert(expanded, locations[i]._intersection, false); + } + return expanded; + } + }}; +}); var PathItem = Item.extend({ _class: 'PathItem', @@ -6611,60 +6911,64 @@ var PathItem = Item.extend({ initialize: function PathItem() { }, - getIntersections: function(path, _matrix, _expand) { - if (this === path) - path = null; - var locations = [], - curves1 = this.getCurves(), - curves2 = path ? path.getCurves() : curves1, + getIntersections: function(path, include, _matrix, _returnFirst) { + var self = this === path || !path, matrix1 = this._matrix.orNullIfIdentity(), - matrix2 = path ? (_matrix || path._matrix).orNullIfIdentity() - : matrix1, - length1 = curves1.length, - length2 = path ? curves2.length : length1, - values2 = [], - tMin = 0.000001, - tMax = 1 - tMin; - if (path && !this.getBounds(matrix1).touches(path.getBounds(matrix2))) + matrix2 = self ? matrix1 + : (_matrix || path._matrix).orNullIfIdentity(); + if (!self && !this.getBounds(matrix1).touches(path.getBounds(matrix2))) return []; + var curves1 = this.getCurves(), + curves2 = self ? curves1 : path.getCurves(), + length1 = curves1.length, + length2 = self ? length1 : curves2.length, + values2 = [], + arrays = [], + locations, + path; for (var i = 0; i < length2; i++) values2[i] = curves2[i].getValues(matrix2); for (var i = 0; i < length1; i++) { var curve1 = curves1[i], - values1 = path ? curve1.getValues(matrix1) : values2[i]; - if (!path) { - var seg1 = curve1.getSegment1(), - seg2 = curve1.getSegment2(), - h1 = seg1._handleOut, - h2 = seg2._handleIn; - if (new Line(seg1._point.subtract(h1), h1.multiply(2), true) - .intersect(new Line(seg2._point.subtract(h2), - h2.multiply(2), true), false)) { - var parts = Curve.subdivide(values1); - Curve.getIntersections( - parts[0], parts[1], curve1, curve1, locations, - function(loc) { - if (loc._parameter <= tMax) { - loc._parameter /= 2; - loc._parameter2 = 0.5 + loc._parameter2 / 2; - return true; - } - } - ); - } + values1 = self ? values2[i] : curve1.getValues(matrix1), + path1 = curve1.getPath(); + if (path1 !== path) { + path = path1; + locations = []; + arrays.push(locations); } - for (var j = path ? 0 : i + 1; j < length2; j++) { - Curve.getIntersections( - values1, values2[j], curve1, curves2[j], locations, - !path && (j === i + 1 || j === length2 - 1 && i === 0) - && function(loc) { - var t = loc._parameter; - return t >= tMin && t <= tMax; - } + if (self) { + Curve._getSelfIntersection(values1, curve1, locations, { + include: include, + startConnected: length1 === 1 && + curve1.getPoint1().equals(curve1.getPoint2()) + }); + } + for (var j = self ? i + 1 : 0; j < length2; j++) { + if (_returnFirst && locations.length) + return locations; + var curve2 = curves2[j]; + Curve._getIntersections( + values1, values2[j], curve1, curve2, locations, + { + include: include, + startConnected: self && curve1.getPrevious() === curve2, + endConnected: self && curve1.getNext() === curve2 + } ); } } - return Curve.filterIntersections(locations, _expand); + locations = []; + for (var i = 0, l = arrays.length; i < l; i++) { + locations.push.apply(locations, arrays[i]); + } + return locations; + }, + + getCrossings: function(path) { + return this.getIntersections(path, function(inter) { + return inter.isCrossing(); + }); }, _asPathItem: function() { @@ -6840,14 +7144,14 @@ var Path = PathItem.extend({ var parent = this._parent; if (parent) parent._currentPath = undefined; - this._length = this._clockwise = undefined; + this._length = this._area = this._clockwise = this._monoCurves = + undefined; if (flags & 16) { this._version++; } else if (this._curves) { for (var i = 0, l = this._curves.length; i < l; i++) this._curves[i]._changed(); } - this._monoCurves = undefined; } else if (flags & 32) { this._bounds = undefined; } @@ -6977,24 +7281,6 @@ var Path = PathItem.extend({ return this._segments.length === 0; }, - isLinear: function() { - var segments = this._segments; - for (var i = 0, l = segments.length; i < l; i++) { - if (!segments[i].isLinear()) - return false; - } - return true; - }, - - hasHandles: function() { - var segments = this._segments; - for (var i = 0, l = segments.length; i < l; i++) { - if (segments[i].hasHandles()) - return true; - } - return false; - }, - _transformContent: function(matrix) { var coords = new Array(6); for (var i = 0, l = this._segments.length; i < l; i++) @@ -7024,12 +7310,11 @@ var Path = PathItem.extend({ for (var i = index + amount, l = segments.length; i < l; i++) segments[i]._index = i; } - if (curves || segs._curves) { - if (!curves) - curves = this._curves = []; - var from = index > 0 ? index - 1 : index, + if (curves) { + var total = this._countCurves(), + from = index + amount - 1 === total ? index - 1 : index, start = from, - to = Math.min(from + amount, this._countCurves()); + to = Math.min(from + amount, total); if (segs._curves) { curves.splice.apply(curves, [from, 0].concat(segs._curves)); start += segs._curves.length; @@ -7134,22 +7419,57 @@ var Path = PathItem.extend({ clear: '#removeSegments', + hasHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) { + if (segments[i].hasHandles()) + return true; + } + return false; + }, + + clearHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) + segments[i].clearHandles(); + }, + getLength: function() { if (this._length == null) { - var curves = this.getCurves(); - this._length = 0; + var curves = this.getCurves(), + length = 0; for (var i = 0, l = curves.length; i < l; i++) - this._length += curves[i].getLength(); + length += curves[i].getLength(); + this._length = length; } return this._length; }, getArea: function() { - var curves = this.getCurves(); - var area = 0; - for (var i = 0, l = curves.length; i < l; i++) - area += curves[i].getArea(); - return area; + if (this._area == null) { + var segments = this._segments, + count = segments.length, + last = count - 1, + area = 0; + for (var i = 0, l = this._closed ? count : last; i < l; i++) { + area += Curve.getArea(Curve.getValues( + segments[i], segments[i < last ? i + 1 : 0])); + } + this._area = area; + } + return this._area; + }, + + isClockwise: function() { + if (this._clockwise !== undefined) + return this._clockwise; + return this.getArea() >= 0; + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() != (clockwise = !!clockwise)) + this.reverse(); + this._clockwise = clockwise; }, isFullySelected: function() { @@ -7203,7 +7523,8 @@ var Path = PathItem.extend({ var curves = this.getCurves(); for (var i = curves.length - 1; i >= 0; i--) { var curve = curves[i]; - if (curve.isLinear() && curve.getLength() === 0) + if (!curve.hasHandles() && (curve.getLength() === 0 + || curve.isCollinear(curve.getNext()))) curve.remove(); } return this; @@ -7228,14 +7549,15 @@ var Path = PathItem.extend({ index = arg.index; parameter = arg.parameter; } - var tolerance = 0.000001; - if (parameter >= 1 - tolerance) { + var tMin = 4e-7, + tMax = 1 - tMin; + if (parameter >= tMax) { index++; parameter--; } var curves = this.getCurves(); if (index >= 0 && index < curves.length) { - if (parameter > tolerance) { + if (parameter >= tMin) { curves[index++].divide(parameter, true); } var segs = this.removeSegments(index, this._segments.length, true), @@ -7244,7 +7566,9 @@ var Path = PathItem.extend({ this.setClosed(false); path = this; } else { - path = this._clone(new Path().insertAbove(this, true)); + path = new Path(Item.NO_INSERT); + path.insertAbove(this, true); + this._clone(path); } path._add(segs, 0); this.addSegment(segs[0]); @@ -7253,18 +7577,6 @@ var Path = PathItem.extend({ return null; }, - isClockwise: function() { - if (this._clockwise !== undefined) - return this._clockwise; - return Path.isClockwise(this._segments); - }, - - setClockwise: function(clockwise) { - if (this.isClockwise() != (clockwise = !!clockwise)) - this.reverse(); - this._clockwise = clockwise; - }, - reverse: function() { this._segments.reverse(); for (var i = 0, l = this._segments.length; i < l; i++) { @@ -7305,7 +7617,7 @@ var Path = PathItem.extend({ this._add(segments.slice()); } } - if (path.closed) + if (path._closed) this._add([segments[0]]); path.remove(); } @@ -7330,15 +7642,43 @@ var Path = PathItem.extend({ topCenter; function isCollinear(i, j) { - return segments[i].isCollinear(segments[j]); + var seg1 = segments[i], + seg2 = seg1.getNext(), + seg3 = segments[j], + seg4 = seg3.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg3._handleOut.isZero() && seg4._handleIn.isZero() + && seg2._point.subtract(seg1._point).isCollinear( + seg4._point.subtract(seg3._point)); } function isOrthogonal(i) { - return segments[i].isOrthogonal(); + var seg2 = segments[i], + seg1 = seg2.getPrevious(), + seg3 = seg2.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg2._handleOut.isZero() && seg3._handleIn.isZero() + && seg2._point.subtract(seg1._point).isOrthogonal( + seg3._point.subtract(seg2._point)); } function isArc(i) { - return segments[i].isOrthogonalArc(); + var seg1 = segments[i], + seg2 = seg1.getNext(), + handle1 = seg1._handleOut, + handle2 = seg2._handleIn, + kappa = 0.5522847498307936; + if (handle1.isOrthogonal(handle2)) { + var pt1 = seg1._point, + pt2 = seg2._point, + corner = new Line(pt1, handle1, true).intersect( + new Line(pt2, handle2, true), true); + return corner && Numerical.isZero(handle1.getLength() / + corner.subtract(pt1).getLength() - kappa) + && Numerical.isZero(handle2.getLength() / + corner.subtract(pt2).getLength() - kappa); + } + return false; } function getDistance(i, j) { @@ -7515,22 +7855,6 @@ var Path = PathItem.extend({ { beans: false, - _getOffset: function(location) { - var index = location && location.getIndex(); - if (index != null) { - var curves = this.getCurves(), - offset = 0; - for (var i = 0; i < index; i++) - offset += curves[i].getLength(); - var curve = curves[index], - parameter = location.getParameter(); - if (parameter > 0) - offset += curve.getPartLength(0, parameter); - return offset; - } - return null; - }, - getLocationOf: function() { var point = Point.read(arguments), curves = this.getCurves(); @@ -7563,7 +7887,7 @@ var Path = PathItem.extend({ return curve.getLocationAt(offset - start); } } - if (offset <= this.getLength()) + if (curves.length > 0 && offset <= this.getLength()) return new CurveLocation(curves[curves.length - 1], 1); return null; }, @@ -7586,7 +7910,8 @@ var Path = PathItem.extend({ getNearestPoint: function() { return this.getNearestLocation.apply(this, arguments).getPoint(); } -}), new function() { +}), +new function() { function drawHandles(ctx, segments, matrix, size) { var half = size / 2; @@ -7750,8 +8075,8 @@ var Path = PathItem.extend({ drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); } }; -}, new function() { - +}, +new function() { function getFirstControlPoints(rhs) { var n = rhs.length, x = [], @@ -7843,7 +8168,8 @@ var Path = PathItem.extend({ } } }; -}, new function() { +}, +new function() { function getCurrentSegment(that) { var segments = that._segments; if (segments.length === 0) @@ -7929,7 +8255,6 @@ var Path = PathItem.extend({ x = pt.x, y = pt.y, abs = Math.abs, - epsilon = 1e-12, rx = abs(radius.width), ry = abs(radius.height), rxSq = rx * rx, @@ -7945,7 +8270,7 @@ var Path = PathItem.extend({ } factor = (rxSq * rySq - rxSq * ySq - rySq * xSq) / (rxSq * ySq + rySq * xSq); - if (abs(factor) < epsilon) + if (abs(factor) < 1e-12) factor = 0; if (factor < 0) throw new Error( @@ -8075,14 +8400,6 @@ var Path = PathItem.extend({ }, statics: { - isClockwise: function(segments) { - var sum = 0; - for (var i = 0, l = segments.length; i < l; i++) - sum += Curve.getEdgeSum(Curve.getValues( - segments[i], segments[i + 1 < l ? i + 1 : 0])); - return sum > 0; - }, - getBounds: function(segments, closed, style, matrix, strokePadding) { var first = segments[0]; if (!first) @@ -8427,6 +8744,13 @@ var CompoundPath = PathItem.extend({ }, insertChildren: function insertChildren(index, items, _preserve) { + for (var i = items.length - 1; i >= 0; i--) { + var item = items[i]; + if (item instanceof CompoundPath) { + items.splice.apply(items, [i, 1].concat(item.removeChildren())); + item.remove(); + } + } items = insertChildren.base.call(this, index, items, _preserve, Path); for (var i = 0, l = !_preserve && items && items.length; i < l; i++) { var item = items[i]; @@ -8448,15 +8772,20 @@ var CompoundPath = PathItem.extend({ }, reduce: function reduce() { - if (this._children.length === 0) { + var children = this._children; + for (var i = children.length - 1; i >= 0; i--) { + var path = children[i].reduce(); + if (path.isEmpty()) + children.splice(i, 1); + } + if (children.length === 0) { var path = new Path(Item.NO_INSERT); path.insertAbove(this); path.setStyle(this._style); this.remove(); return path; - } else { - return reduce.base.call(this); } + return reduce.base.call(this); }, isClockwise: function() { @@ -8514,7 +8843,7 @@ var CompoundPath = PathItem.extend({ var child = children[i], mx = child._matrix; paths.push(child.getPathData(_matrix && !mx.isIdentity() - ? _matrix.chain(mx) : mx, _precision)); + ? _matrix.chain(mx) : _matrix, _precision)); } return paths.join(' '); } @@ -8562,7 +8891,8 @@ var CompoundPath = PathItem.extend({ : matrix.chain(mx)); } } -}, new function() { +}, +new function() { function getCurrentPath(that, check) { var children = that._children; if (check && children.length === 0) @@ -8573,7 +8903,8 @@ var CompoundPath = PathItem.extend({ var fields = { moveTo: function() { var current = getCurrentPath(this), - path = current && current.isEmpty() ? current : new Path(); + path = current && current.isEmpty() ? current + : new Path(Item.NO_INSERT); if (path !== current) this.addChild(path); path.moveTo.apply(path, arguments); @@ -8623,24 +8954,40 @@ PathItem.inject(new function() { } }; - function computeBoolean(path1, path2, operation) { - var operator = operators[operation]; - function preparePath(path) { - return path.clone(false).reduce().reorient().transform(null, true, - true); - } + function preparePath(path, resolve) { + var res = path.clone(false).reduce().transform(null, true, true); + return resolve ? res.resolveCrossings().reorient() : res; + } - var _path1 = preparePath(path1), - _path2 = path2 && path1 !== path2 && preparePath(path2); + function finishBoolean(ctor, paths, path1, path2, reduce) { + var result = new ctor(Item.NO_INSERT); + result.addChildren(paths, true); + if (reduce) + result = result.reduce(); + result.insertAbove(path2 && path1.isSibling(path2) + && path1.getIndex() < path2.getIndex() + ? path2 : path1); + result.setStyle(path1._style); + return result; + } + + function computeBoolean(path1, path2, operation) { + if (!path1._children && !path1._closed) + return computeOpenBoolean(path1, path2, operation); + var _path1 = preparePath(path1, true), + _path2 = path2 && path1 !== path2 && preparePath(path2, true); if (_path2 && /^(subtract|exclude)$/.test(operation) ^ (_path2.isClockwise() !== _path1.isClockwise())) _path2.reverse(); - splitPath(_path1.getIntersections(_path2, null, true)); + var intersections = CurveLocation.expand( + _path1.getIntersections(_path2, function(inter) { + return _path2 && inter.isOverlap() || inter.isCrossing(); + }) + ); + divideLocations(intersections); - var chain = [], - segments = [], - monoCurves = [], - tolerance = 0.000001; + var segments = [], + monoCurves = []; function collect(paths) { for (var i = 0, l = paths.length; i < l; i++) { @@ -8653,114 +9000,121 @@ PathItem.inject(new function() { collect(_path1._children || [_path1]); if (_path2) collect(_path2._children || [_path2]); - segments.sort(function(a, b) { - var _a = a._intersection, - _b = b._intersection; - return !_a && !_b || _a && _b ? 0 : _a ? -1 : 1; - }); + for (var i = 0, l = intersections.length; i < l; i++) { + propagateWinding(intersections[i]._segment, _path1, _path2, + monoCurves, operation); + } for (var i = 0, l = segments.length; i < l; i++) { var segment = segments[i]; - if (segment._winding != null) - continue; - chain.length = 0; - var startSeg = segment, - totalLength = 0, - windingSum = 0; - do { - var length = segment.getCurve().getLength(); - chain.push({ segment: segment, length: length }); - totalLength += length; - segment = segment.getNext(); - } while (segment && !segment._intersection && segment !== startSeg); - for (var j = 0; j < 3; j++) { - var length = totalLength * (j + 1) / 4; - for (var k = 0, m = chain.length; k < m; k++) { - var node = chain[k], - curveLength = node.length; - if (length <= curveLength) { - if (length < tolerance - || curveLength - length < tolerance) - length = curveLength / 2; - var curve = node.segment.getCurve(), - pt = curve.getPointAt(length), - hor = curve.isLinear() && Math.abs(curve - .getTangentAt(0.5, true).y) < tolerance, - path = curve._path; - if (path._parent instanceof CompoundPath) - path = path._parent; - windingSum += operation === 'subtract' && _path2 - && (path === _path1 && _path2._getWinding(pt, hor) - || path === _path2 && !_path1._getWinding(pt, hor)) - ? 0 - : getWinding(pt, monoCurves, hor); - break; - } - length -= curveLength; - } + if (segment._winding == null) { + propagateWinding(segment, _path1, _path2, monoCurves, + operation); } - var winding = Math.round(windingSum / 3); - for (var j = chain.length - 1; j >= 0; j--) - chain[j].segment._winding = winding; } - var result = new CompoundPath(Item.NO_INSERT); - result.insertAbove(path1); - result.addChildren(tracePaths(segments, operator), true); - result = result.reduce(); - result.setStyle(path1._style); - return result; + return finishBoolean(CompoundPath, tracePaths(segments, operation), + path1, path2, true); } - function splitPath(intersections) { - var tMin = 0.000001, + function computeOpenBoolean(path1, path2, operation) { + if (!path2 || !path2._children && !path2._closed + || !/^(subtract|intersect)$/.test(operation)) + return null; + var _path1 = preparePath(path1, false), + _path2 = preparePath(path2, false), + intersections = _path1.getIntersections(_path2, function(inter) { + return inter.isOverlap() || inter.isCrossing(); + }), + sub = operation === 'subtract', + paths = []; + + function addPath(path) { + if (_path2.contains(path.getPointAt(path.getLength() / 2)) ^ sub) { + paths.unshift(path); + return true; + } + } + + for (var i = intersections.length - 1; i >= 0; i--) { + var path = intersections[i].split(); + if (path) { + if (addPath(path)) + path.getFirstSegment().setHandleIn(0, 0); + _path1.getLastSegment().setHandleOut(0, 0); + } + } + addPath(_path1); + return finishBoolean(Group, paths, path1, path2); + } + + function linkIntersections(from, to) { + var prev = from; + while (prev) { + if (prev === to) + return; + prev = prev._prev; + } + while (from._next && from._next !== to) + from = from._next; + if (!from._next) { + while (to._prev) + to = to._prev; + from._next = to; + to._prev = from; + } + } + + function divideLocations(locations) { + var tMin = 4e-7, tMax = 1 - tMin, - linearHandles; + noHandles = false, + clearSegments = [], + prevCurve, + prevT; - function resetLinear() { - for (var i = 0, l = linearHandles.length; i < l; i++) - linearHandles[i].set(0, 0); - } - - for (var i = intersections.length - 1, curve, prev; i >= 0; i--) { - var loc = intersections[i], - t = loc._parameter; - if (prev && prev._curve === loc._curve && prev._parameter > 0) { - t /= prev._parameter; - } else { - curve = loc._curve; - if (linearHandles) - resetLinear(); - linearHandles = curve.isLinear() ? [ - curve._segment1._handleOut, - curve._segment2._handleIn - ] : null; + for (var i = locations.length - 1; i >= 0; i--) { + var loc = locations[i], + curve = loc._curve, + t = loc._parameter, + origT = t; + if (curve !== prevCurve) { + noHandles = !curve.hasHandles(); + } else if (prevT > 0) { + t /= prevT; } - var newCurve, - segment; - if (newCurve = curve.divide(t, true, true)) { - segment = newCurve._segment1; - curve = newCurve.getPrevious(); - if (linearHandles) - linearHandles.push(segment._handleOut, segment._handleIn); + var segment; + if (t < tMin) { + segment = curve._segment1; + } else if (t > tMax) { + segment = curve._segment2; } else { - segment = t < tMin - ? curve._segment1 - : t > tMax - ? curve._segment2 - : curve.getPartLength(0, t) < curve.getPartLength(t, 1) - ? curve._segment1 - : curve._segment2; + segment = curve.divide(t, true, true)._segment1; + if (noHandles) + clearSegments.push(segment); } - segment._intersection = loc.getIntersection(); - loc._segment = segment; - prev = loc; + loc._setSegment(segment); + var inter = segment._intersection, + dest = loc._intersection; + if (inter) { + linkIntersections(inter, dest); + var other = inter; + while (other) { + linkIntersections(other._intersection, inter); + other = other._next; + } + } else { + segment._intersection = dest; + } + prevCurve = curve; + prevT = origT; + } + for (var i = 0, l = clearSegments.length; i < l; i++) { + clearSegments[i].clearHandles(); } - if (linearHandles) - resetLinear(); } function getWinding(point, curves, horizontal, testContains) { - var tolerance = 0.000001, - tMin = tolerance, + var epsilon = 2e-7, + tMin = 4e-7, tMax = 1 - tMin, px = point.x, py = point.y, @@ -8771,8 +9125,8 @@ PathItem.inject(new function() { if (horizontal) { var yTop = -Infinity, yBottom = Infinity, - yBefore = py - tolerance, - yAfter = py + tolerance; + yBefore = py - epsilon, + yAfter = py + epsilon; for (var i = 0, l = curves.length; i < l; i++) { var values = curves[i].values; if (Curve.solveCubic(values, 0, px, roots, 0, 1) > 0) { @@ -8789,12 +9143,14 @@ PathItem.inject(new function() { yTop = (yTop + py) / 2; yBottom = (yBottom + py) / 2; if (yTop > -Infinity) - windLeft = getWinding(new Point(px, yTop), curves); + windLeft = getWinding(new Point(px, yTop), curves, false, + testContains); if (yBottom < Infinity) - windRight = getWinding(new Point(px, yBottom), curves); + windRight = getWinding(new Point(px, yBottom), curves, false, + testContains); } else { - var xBefore = px - tolerance, - xAfter = px + tolerance; + var xBefore = px - epsilon, + xAfter = px + epsilon; var startCounted = false, prevCurve, prevT; @@ -8814,7 +9170,7 @@ PathItem.inject(new function() { var x = Curve.getPoint(values, t).x, slope = Curve.getTangent(values, t).y, counted = false; - if (Numerical.isZero(slope) && !Curve.isLinear(values) + if (Numerical.isZero(slope) && !Curve.isStraight(values) || t < tMin && slope * Curve.getTangent( curve.previous.values, 1).y < 0 || t > tMax && slope * Curve.getTangent( @@ -8842,81 +9198,157 @@ PathItem.inject(new function() { return Math.max(abs(windLeft), abs(windRight)); } - function tracePaths(segments, operator, selfOp) { + function propagateWinding(segment, path1, path2, monoCurves, operation) { + var epsilon = 2e-7, + chain = [], + start = segment, + totalLength = 0, + windingSum = 0; + do { + var curve = segment.getCurve(), + length = curve.getLength(); + chain.push({ segment: segment, curve: curve, length: length }); + totalLength += length; + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== start); + for (var i = 0; i < 3; i++) { + var length = totalLength * (i + 1) / 4; + for (var k = 0, m = chain.length; k < m; k++) { + var node = chain[k], + curveLength = node.length; + if (length <= curveLength) { + if (length < epsilon || curveLength - length < epsilon) + length = curveLength / 2; + var curve = node.curve, + path = curve._path, + parent = path._parent, + pt = curve.getPointAt(length), + hor = curve.isHorizontal(); + if (parent instanceof CompoundPath) + path = parent; + windingSum += operation === 'subtract' && path2 + && (path === path1 && path2._getWinding(pt, hor) + || path === path2 && !path1._getWinding(pt, hor)) + ? 0 + : getWinding(pt, monoCurves, hor); + break; + } + length -= curveLength; + } + } + var winding = Math.round(windingSum / 3); + for (var j = chain.length - 1; j >= 0; j--) + chain[j].segment._winding = winding; + } + + function tracePaths(segments, operation) { var paths = [], - tMin = 0.000001, - tMax = 1 - tMin; - for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) { - seg = startSeg = segments[i]; - if (seg._visited || !operator(seg._winding)) + start, + otherStart, + operator = operators[operation], + overlapWinding = { + unite: { 1: 2 }, + intersect: { 2: 1 } + }[operation]; + + function isValid(seg, adjusted) { + if (seg._visited) + return false; + if (!operator) + return true; + var winding = seg._winding, + inter = seg._intersection; + if (inter && adjusted && overlapWinding && inter.isOverlap()) + winding = overlapWinding[winding] || winding; + return operator(winding); + } + + function isStart(seg) { + return seg === start || seg === otherStart; + } + + function findBestIntersection(inter, strict) { + if (!inter._next) + return inter; + while (inter) { + var seg = inter._segment, + nextSeg = seg.getNext(), + nextInter = nextSeg._intersection; + if (isStart(nextSeg) + || !seg._visited && !nextSeg._visited + && (!operator + || (!strict || isValid(seg)) + && (!(strict && nextInter && nextInter.isOverlap()) + && isValid(nextSeg) + || !strict && nextInter + && isValid(nextInter._segment)) + )) + return inter; + inter = inter._next; + } + return null; + } + + function findStartSegment(inter, next) { + while (inter) { + var seg = inter._segment; + if (isStart(seg)) + return seg; + inter = inter[next ? '_next' : '_prev']; + } + } + + for (var i = 0, l = segments.length; i < l; i++) { + var seg = segments[i], + path = null, + finished = false; + if (!isValid(seg, true)) continue; - var path = new Path(Item.NO_INSERT), - inter = seg._intersection, - startInterSeg = inter && inter._segment, - added = false, - dir = 1; - do { - var handleIn = dir > 0 ? seg._handleIn : seg._handleOut, - handleOut = dir > 0 ? seg._handleOut : seg._handleIn, - interSeg; - if (added && (!operator(seg._winding) || selfOp) - && (inter = seg._intersection) - && (interSeg = inter._segment) - && interSeg !== startSeg) { - if (selfOp) { - seg._visited = interSeg._visited; - seg = interSeg; - dir = 1; - } else { - var c1 = seg.getCurve(); - if (dir > 0) - c1 = c1.getPrevious(); - var t1 = c1.getTangentAt(dir < 1 ? tMin : tMax, true), - c4 = interSeg.getCurve(), - c3 = c4.getPrevious(), - t3 = c3.getTangentAt(tMax, true), - t4 = c4.getTangentAt(tMin, true), - w3 = t1.cross(t3), - w4 = t1.cross(t4); - if (w3 * w4 !== 0) { - var curve = w3 < w4 ? c3 : c4, - nextCurve = operator(curve._segment1._winding) - ? curve - : w3 < w4 ? c4 : c3, - nextSeg = nextCurve._segment1; - dir = nextCurve === c3 ? -1 : 1; - if (nextSeg._visited && seg._path !== nextSeg._path - || !operator(nextSeg._winding)) { - dir = 1; - } else { - seg._visited = interSeg._visited; - seg = interSeg; - if (nextSeg._visited) - dir = 1; - } - } else { - dir = 1; + start = otherStart = null; + while (!finished) { + var inter = seg._intersection, + handleIn = path && seg._handleIn; + inter = inter && (findBestIntersection(inter, true) + || findBestIntersection(inter, false)) || inter; + var other = inter && inter._segment; + if (other && isValid(other)) + seg = other; + if (seg._visited) { + finished = isStart(seg); + if (!finished && inter) { + var found = findStartSegment(inter, true) + || findStartSegment(inter, false); + if (found) { + seg = found; + finished = true; } } - handleOut = dir > 0 ? seg._handleOut : seg._handleIn; + break; } - path.add(new Segment(seg._point, added && handleIn, handleOut)); - added = true; + if (!path) { + path = new Path(Item.NO_INSERT); + start = seg; + otherStart = other; + } + path.add(new Segment(seg._point, handleIn, seg._handleOut)); seg._visited = true; - seg = dir > 0 ? seg.getNext() : seg. getPrevious(); - } while (seg && !seg._visited - && seg !== startSeg && seg !== startInterSeg - && (seg._intersection || operator(seg._winding))); - if (seg && (seg === startSeg || seg === startInterSeg)) { - path.firstSegment.setHandleIn((seg === startInterSeg - ? startInterSeg : seg)._handleIn); - path.setClosed(true); - } else { - path.lastSegment._handleOut.set(0, 0); + seg = seg.getNext(); + finished = isStart(seg); } - if (path._segments.length > - (path._closed ? path.isLinear() ? 2 : 0 : 1)) + if (finished) { + path.firstSegment.setHandleIn(seg._handleIn); + path.setClosed(true); + } else if (path) { + console.error('Boolean operation resulted in open path', + 'segments =', path._segments.length, + 'length =', path.getLength()); + path = null; + } + if (path && (path._segments.length > 8 + || !Numerical.isZero(path.getArea()))) { paths.push(path); + path = null; + } } return paths; } @@ -8944,7 +9376,23 @@ PathItem.inject(new function() { }, divide: function(path) { - return new Group([this.subtract(path), this.intersect(path)]); + return finishBoolean(Group, + [this.subtract(path), this.intersect(path)], + this, path, true); + }, + + resolveCrossings: function() { + var crossings = this.getCrossings(); + if (!crossings.length) + return this; + divideLocations(CurveLocation.expand(crossings)); + var paths = this._children || [this], + segments = []; + for (var i = 0, l = paths.length; i < l; i++) { + segments.push.apply(segments, paths[i]._segments); + } + return finishBoolean(CompoundPath, tracePaths(segments), + this, null, false); } }; }); @@ -8980,24 +9428,24 @@ Path.inject({ y1 = v[3], y2 = v[5], y3 = v[7]; - if (Curve.isLinear(v)) { + if (Curve.isStraight(v)) { insertCurve(v); } else { var a = 3 * (y1 - y2) - y0 + y3, b = 2 * (y0 + y2) - 4 * y1, c = y1 - y0, - tolerance = 0.000001, - roots = []; - var count = Numerical.solveQuadratic(a, b, c, roots, tolerance, - 1 - tolerance); - if (count === 0) { + tMin = 4e-7, + tMax = 1 - tMin, + roots = [], + n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax); + if (n === 0) { insertCurve(v); } else { roots.sort(); var t = roots[0], parts = Curve.subdivide(v, t); insertCurve(parts[0]); - if (count > 1) { + if (n > 1) { t = (roots[1] - t) / (1 - t); parts = Curve.subdivide(parts[1], t); insertCurve(parts[0]); @@ -9112,7 +9560,7 @@ var PathIterator = Base.extend({ function computeParts(curve, index, minT, maxT) { if ((maxT - minT) > minDifference && !Curve.isFlatEnough(curve, tolerance || 0.25)) { - var split = Curve.subdivide(curve), + var split = Curve.subdivide(curve, 0.5), halfT = (minT + maxT) / 2; computeParts(split[0], index, minT, halfT); computeParts(split[1], index, halfT, maxT); @@ -9120,7 +9568,7 @@ var PathIterator = Base.extend({ var x = curve[6] - curve[0], y = curve[7] - curve[1], dist = Math.sqrt(x * x + y * y); - if (dist > 0.000001) { + if (dist > 1e-6) { length += dist; parts.push({ offset: length, @@ -9366,7 +9814,7 @@ var PathFitter = Base.extend({ pt2 = this.evaluate(1, curve2, u), diff = pt.subtract(point), df = pt1.dot(pt1) + diff.dot(pt2); - if (Math.abs(df) < 0.000001) + if (Math.abs(df) < 1e-6) return u; return u - diff.dot(pt1) / df; }, @@ -9801,7 +10249,7 @@ var Color = Base.extend(new function() { : 'gray' in arg ? 'gray' : 'rgb'; - var properties = types[type]; + var properties = types[type], parsers = componentParsers[type]; this._components = components = []; for (var i = 0, l = properties.length; i < l; i++) { @@ -10003,7 +10451,8 @@ var Color = Base.extend(new function() { } } }); -}, new function() { +}, +new function() { var operators = { add: function(a, b) { return a + b; @@ -10039,24 +10488,6 @@ var Color = Base.extend(new function() { }); }); -Base.each(Color._types, function(properties, type) { - var ctor = this[Base.capitalize(type) + 'Color'] = function(arg) { - var argType = arg != null && typeof arg, - components = argType === 'object' && arg.length != null - ? arg - : argType === 'string' - ? null - : arguments; - return components - ? new Color(type, components) - : new Color(arg); - }; - if (type.length == 3) { - var acronym = type.toUpperCase(); - Color[acronym] = this[acronym + 'Color'] = ctor; - } -}, Base.exports); - var Gradient = Base.extend({ _class: 'Gradient', @@ -10681,19 +11112,29 @@ var View = Base.extend(Emitter, { return true; }, - _events: { - onFrame: { - install: function() { - this.play(); - }, + _events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove'], + function(name) { + this[name] = { + install: function(type) { + this._installEvent(type); + }, - uninstall: function() { - this.pause(); + uninstall: function(type) { + this._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.play(); + }, + + uninstall: function() { + this.pause(); + } } - }, - - onResize: {} - }, + } + ), _animate: false, _time: 0, @@ -10888,7 +11329,8 @@ var View = Base.extend(Emitter, { return new CanvasView(project, element); } } -}, new function() { +}, +new function() { var tool, prevFocus, tempFocus, @@ -11011,11 +11453,50 @@ var View = Base.extend(Emitter, { load: updateFocus }); + var mouseFlags = { + mousedown: { + mousedown: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mouseup: { + mouseup: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mousemove: { + mousedrag: 1, + mousemove: 1, + mouseenter: 1, + mouseleave: 1 + } + }; + return { _viewEvents: viewEvents, _handleEvent: function() {}, + _installEvent: function(type) { + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) { + counters[key] = (counters[key] || 0) + + (mouseFlags[key][type] || 0); + } + } + }, + + _uninstallEvent: function(type) { + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) + counters[key] -= mouseFlags[key][type] || 0; + } + }, + statics: { updateFocus: updateFocus } @@ -11105,8 +11586,8 @@ var CanvasView = View.extend({ project._needsUpdate = false; return true; } -}, new function() { - +}, +new function() { var downPoint, lastPoint, overPoint, @@ -11551,8 +12032,7 @@ var Tool = PaperScopeItem.extend({ }, setFixedDistance: function(distance) { - this._minDistance = distance; - this._maxDistance = distance; + this._minDistance = this._maxDistance = distance; }, _updateEvent: function(type, point, minDistance, maxDistance, start, diff --git a/dist/paper-core.min.js b/dist/paper-core.min.js index ea86f5a8..73a4f46f 100644 --- a/dist/paper-core.min.js +++ b/dist/paper-core.min.js @@ -1,5 +1,5 @@ /*! - * Paper.js v0.9.24 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.25 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Fri Aug 21 16:39:41 2015 +0200 + * Date: Sun Oct 25 11:23:38 2015 +0100 * *** * @@ -29,9 +29,9 @@ * created by Marijn Haverbeke and released under an MIT license. * */ -var paper=new function(t){var e=new function(){function i(t,i,n,r,a){function o(s,o){o=o||(o=u(i,s))&&(o.get?o:o.value),"string"==typeof o&&"#"===o[0]&&(o=t[o.substring(1)]||o);var l,d="function"==typeof o,_=o,f=a||d&&!o.base?o&&o.get?s in t:t[s]:null;a&&f||(d&&f&&(o.base=f),d&&r!==!1&&(l=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(h[l[3].toLowerCase()+l[4]]=l[2]),_&&!d&&_.get&&"function"==typeof _.get&&e.isPlainObject(_)||(_={value:_,writable:!0}),(u(t,s)||{configurable:!0}).configurable&&(_.configurable=!0,_.enumerable=n),c(t,s,_))}var h={};if(i){for(var l in i)i.hasOwnProperty(l)&&!s.test(l)&&o(l);for(var l in h){var d=h[l],_=t["set"+d],f=t["get"+d]||_&&t["is"+d];!f||r!==!0&&0!==f.length||o(l,{get:f,set:_})}}return t}function n(t,e,i){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?a:o).call(t,e,i=i||t),i}function r(t,e,i){for(var n in e)!e.hasOwnProperty(n)||i&&i[n]||(t[n]=e[n]);return t}var s=/^(statics|enumerable|beans|preserve)$/,a=[].forEach||function(t,e){for(var i=0,n=this.length;n>i;i++)t.call(e,this[i],i,this)},o=function(t,e){for(var i in this)this.hasOwnProperty(i)&&t.call(e,this[i],i,this)},h=Object.create||function(t){return{__proto__:t}},u=Object.getOwnPropertyDescriptor||function(t,e){var i=t.__lookupGetter__&&t.__lookupGetter__(e);return i?{get:i,set:t.__lookupSetter__(e),enumerable:!0,configurable:!0}:t.hasOwnProperty(e)?{value:t[e],enumerable:!0,configurable:!0,writable:!0}:null},l=Object.defineProperty||function(t,e,i){return(i.get||i.set)&&t.__defineGetter__?(i.get&&t.__defineGetter__(e,i.get),i.set&&t.__defineSetter__(e,i.set)):t[e]=i.value,t},c=function(t,e,i){return delete t[e],l(t,e,i)};return i(function(){for(var t=0,e=arguments.length;e>t;t++)r(this,arguments[t])},{inject:function(t){if(t){var e=t.statics===!0?t:t.statics,n=t.beans,r=t.preserve;e!==t&&i(this.prototype,t,t.enumerable,n,r),i(this,e,!0,n,r)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e,n=this,r=0,s=arguments.length;s>r&&!(t=arguments[r].initialize);r++);return t=t||function(){n.apply(this,arguments)},e=t.prototype=h(this.prototype),c(e,"constructor",{value:t,writable:!0,configurable:!0}),i(t,this,!0),arguments.length&&this.inject.apply(t,arguments),t.base=n,t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++){var n=arguments[t];n&&i(this,n,n.enumerable,n.beans,n.preserve)}return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return n(this,t,e)},set:function(t){return r(this,t)},clone:function(){return new this.constructor(this)},statics:{each:n,create:h,define:c,describe:u,set:r,clone:function(t){return r(new t.constructor,t)},isPlainObject:function(t){var i=null!=t&&t.constructor;return i&&(i===Object||i===e||"Object"===i.name)},pick:function(e,i){return e!==t?e:i}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({toString:function(){return null!=this._id?(this._class||"Object")+(this._name?" '"+this._name+"'":" @"+this._id):"{ "+e.each(this,function(t,e){if(!/^_/.test(e)){var i=typeof t;this.push(e+": "+("number"===i?s.instance.number(t):"string"===i?"'"+t+"'":t))}},[]).join(", ")+" }"},getClassName:function(){return this._class||""},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(i,n,r){if(i&&(r||e.isPlainObject(i))){for(var s=Object.keys(i._filtering||i),a=0,o=s.length;o>a;a++){var h=s[a];if(!n||!n[h]){var u=i[h];u!==t&&(this[h]=u)}}return!0}},statics:{exports:{enumerable:!0},extend:function tt(){var t=tt.base.apply(this,arguments),i=t.prototype._class;return i&&!e.exports[i]&&(e.exports[i]=t),t},equals:function(t,i){if(t===i)return!0;if(t&&t.equals)return t.equals(i);if(i&&i.equals)return i.equals(t);if(t&&i&&"object"==typeof t&&"object"==typeof i){if(Array.isArray(t)&&Array.isArray(i)){var n=t.length;if(n!==i.length)return!1;for(;n--;)if(!e.equals(t[n],i[n]))return!1}else{var r=Object.keys(t),n=r.length;if(n!==Object.keys(i).length)return!1;for(;n--;){var s=r[n];if(!i.hasOwnProperty(s)||!e.equals(t[s],i[s]))return!1}}return!0}return!1},read:function(i,n,r,s){if(this===e){var a=this.peek(i,n);return i.__index++,a}var o=this.prototype,h=o._readIndex,u=n||h&&i.__index||0;s||(s=i.length-u);var l=i[u];return l instanceof this||r&&r.readNull&&null==l&&1>=s?(h&&(i.__index=u+1),l&&r&&r.clone?l.clone():l):(l=e.create(this.prototype),h&&(l.__read=!0),l=l.initialize.apply(l,u>0||ss;s++)r.push(Array.isArray(n=t[s])?this.read(n,0,i):this.read(t,s,i,1));return r},readNamed:function(i,n,r,s,a){var o=this.getNamed(i,n),h=o!==t;if(h){var u=i._filtered;u||(u=i._filtered=e.create(i[0]),u._filtering=i[0]),u[n]=t}return this.read(h?[o]:i,r,s,a)},getNamed:function(i,n){var r=i[0];return i._hasObject===t&&(i._hasObject=1===i.length&&e.isPlainObject(r)),i._hasObject?n?r[n]:i._filtered||r:t},hasNamed:function(t,e){return!!this.getNamed(t,e)},isPlainValue:function(t,e){return this.isPlainObject(t)||Array.isArray(t)||e&&"string"==typeof t},serialize:function(t,i,n,r){i=i||{};var a,o=!r;if(o&&(i.formatter=new s(i.precision),r={length:0,definitions:{},references:{},add:function(t,e){var i="#"+t._id,n=this.references[i];if(!n){this.length++;var r=e.call(t),s=t._class;s&&r[0]!==s&&r.unshift(s),this.definitions[i]=r,n=this.references[i]=[i]}return n}}),t&&t._serialize){a=t._serialize(i,r);var h=t._class;!h||n||a._compact||a[0]===h||a.unshift(h)}else if(Array.isArray(t)){a=[];for(var u=0,l=t.length;l>u;u++)a[u]=e.serialize(t[u],i,n,r);n&&(a._compact=!0)}else if(e.isPlainObject(t)){a={};for(var c=Object.keys(t),u=0,l=c.length;l>u;u++){var d=c[u];a[d]=e.serialize(t[d],i,n,r)}}else a="number"==typeof t?i.formatter.number(t,i.precision):t;return o&&r.length>0?[["dictionary",r.definitions],a]:a},deserialize:function(t,i,n,r){var s=t,a=!n;if(n=n||{},Array.isArray(t)){var o=t[0],h="dictionary"===o;if(1==t.length&&/^#/.test(o))return n.dictionary[o];o=e.exports[o],s=[],r&&(n.dictionary=s);for(var u=o?1:0,l=t.length;l>u;u++)s.push(e.deserialize(t[u],i,n,h));if(o){var c=s;i?s=i(o,c):(s=e.create(o.prototype),o.apply(s,c))}}else if(e.isPlainObject(t)){s={},r&&(n.dictionary=s);for(var d in t)s[d]=e.deserialize(t[d],i,n)}return a&&t&&t.length&&"dictionary"===t[0][0]?s[1]:s},exportJSON:function(t,i){var n=e.serialize(t,i);return i&&i.asString===!1?n:JSON.stringify(n)},importJSON:function(t,i){return e.deserialize("string"==typeof t?JSON.parse(t):t,function(t,n){var r=i&&i.constructor===t?i:e.create(t.prototype),s=r===i;if(1===n.length&&r instanceof m&&(s||!(r instanceof w))){var a=n[0];e.isPlainObject(a)&&(a.insert=!1)}return t.apply(r,n),s&&(i=null),r})},splice:function(e,i,n,r){var s=i&&i.length,a=n===t;n=a?e.length:n,n>e.length&&(n=e.length);for(var o=0;s>o;o++)i[o]._index=n+o;if(a)return e.push.apply(e,i),[];var h=[n,r];i&&h.push.apply(h,i);for(var u=e.splice.apply(e,h),o=0,l=u.length;l>o;o++)u[o]._index=t;for(var o=n+s,l=e.length;l>o;o++)e[o]._index=o;return u},capitalize:function(t){return t.replace(/\b[a-z]/g,function(t){return t.toUpperCase()})},camelize:function(t){return t.replace(/-(.)/g,function(t,e){return e.toUpperCase()})},hyphenate:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}}});var i={on:function(t,i){if("string"!=typeof t)e.each(t,function(t,e){this.on(e,t)},this);else{var n=this._eventTypes,r=n&&n[t],s=this._callbacks=this._callbacks||{};s=s[t]=s[t]||[],-1===s.indexOf(i)&&(s.push(i),r&&r.install&&1==s.length&&r.install.call(this,t))}return this},off:function(i,n){if("string"!=typeof i)return e.each(i,function(t,e){this.off(e,t)},this),t;var r,s=this._eventTypes,a=s&&s[i],o=this._callbacks&&this._callbacks[i];return o&&(!n||-1!==(r=o.indexOf(n))&&1===o.length?(a&&a.uninstall&&a.uninstall.call(this,i),delete this._callbacks[i]):-1!==r&&o.splice(r,1)),this},once:function(t,e){return this.on(t,function(){e.apply(this,arguments),this.off(t,e)})},emit:function(t,e){var i=this._callbacks&&this._callbacks[t];if(!i)return!1;var n=[].slice.call(arguments,1);i=i.slice();for(var r=0,s=i.length;s>r;r++)if(i[r].apply(this,n)===!1){e&&e.stop&&e.stop();break}return!0},responds:function(t){return!(!this._callbacks||!this._callbacks[t])},attach:"#on",detach:"#off",fire:"#emit",_installEvents:function(t){var e=this._callbacks,i=t?"install":"uninstall";for(var n in e)if(e[n].length>0){var r=this._eventTypes,s=r&&r[n],a=s&&s[i];a&&a.call(this,n)}},statics:{inject:function et(t){var i=t._events;if(i){var n={};e.each(i,function(i,r){var s="string"==typeof i,a=s?i:r,o=e.capitalize(a),h=a.substring(2).toLowerCase();n[h]=s?{}:i,a="_"+a,t["get"+o]=function(){return this[a]},t["set"+o]=function(t){var e=this[a];e&&this.off(h,e),t&&this.on(h,t),this[a]=t}}),t._eventTypes=n}return et.base.apply(this,arguments)}}},n=e.extend({_class:"PaperScope",initialize:function it(){paper=this,this.settings=new e({applyMatrix:!0,handleSize:4,hitTolerance:0}),this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=it._id++,it._scopes[this._id]=this;var t=it.prototype;if(!this.support){var i=Y.getContext(1,1);t.support={nativeDash:"setLineDash"in i||"mozDash"in i,nativeBlendModes:$.nativeModes},Y.release(i)}if(!this.browser){var n=navigator.userAgent.toLowerCase(),r=(/(win)/.exec(n)||/(mac)/.exec(n)||/(linux)/.exec(n)||[])[0],s=t.browser={platform:r};r&&(s[r]=!0),n.replace(/(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,function(t,e,i,n,r){if(!s.chrome){var a="opera"===e?n:i;"trident"===e&&(a=r,e="msie"),s.version=a,s.versionNumber=parseFloat(a),s.name=e,s[e]=!0}}),s.chrome&&delete s.webkit,s.atom&&delete s.chrome}},version:"0.9.24",getView:function(){return this.project&&this.project.getView()},getPaper:function(){return this},execute:function(t,e,i){paper.PaperScript.execute(t,this,e,i),V.updateFocus()},install:function(t){var i=this;e.each(["project","view","tool"],function(n){e.define(t,n,{configurable:!0,get:function(){return i[n]}})});for(var n in this)!/^_/.test(n)&&this[n]&&(t[n]=this[n])},setup:function(t){return paper=this,this.project=new v(t),this},activate:function(){paper=this},clear:function(){for(var t=this.projects.length-1;t>=0;t--)this.projects[t].remove();for(var t=this.tools.length-1;t>=0;t--)this.tools[t].remove();for(var t=this.palettes.length-1;t>=0;t--)this.palettes[t].remove()},remove:function(){this.clear(),delete n._scopes[this._id]},statics:new function(){function t(t){return t+="Attribute",function(e,i){return e[t](i)||e[t]("data-paper-"+i)}}return{_scopes:{},_id:0,get:function(t){return this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),r=e.extend(i,{initialize:function(t){this._scope=paper,this._index=this._scope[this._list].push(this)-1,(t||!this._scope[this._reference])&&this.activate()},activate:function(){if(!this._scope)return!1;var t=this._scope[this._reference];return t&&t!==this&&t.emit("deactivate"),this._scope[this._reference]=this,this.emit("activate",t),!0},isActive:function(){return this._scope[this._reference]===this},remove:function(){return null==this._index?!1:(e.splice(this._scope[this._list],null,this._index,1),this._scope[this._reference]==this&&(this._scope[this._reference]=null),this._scope=null,!0)}}),s=e.extend({initialize:function(t){this.precision=t||5,this.multiplier=Math.pow(10,this.precision)},number:function(t){return Math.round(t*this.multiplier)/this.multiplier},pair:function(t,e,i){return this.number(t)+(i||",")+this.number(e)},point:function(t,e){return this.number(t.x)+(e||",")+this.number(t.y)},size:function(t,e){return this.number(t.width)+(e||",")+this.number(t.height)},rectangle:function(t,e){return this.point(t,e)+(e||",")+this.size(t,e)}});s.instance=new s;var a=new function(){var t=[[.5773502691896257],[0,.7745966692414834],[.33998104358485626,.8611363115940526],[0,.5384693101056831,.906179845938664],[.2386191860831969,.6612093864662645,.932469514203152],[0,.4058451513773972,.7415311855993945,.9491079123427585],[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],[0,.3242534234038089,.6133714327005904,.8360311073266358,.9681602395076261],[.14887433898163122,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],[0,.26954315595234496,.5190961292068118,.7301520055740494,.8870625997680953,.978228658146057],[.1252334085114689,.3678314989981802,.5873179542866175,.7699026741943047,.9041172563704749,.9815606342467192],[0,.2304583159551348,.44849275103644687,.6423493394403402,.8015780907333099,.9175983992229779,.9841830547185881],[.10805494870734367,.31911236892788974,.5152486363581541,.6872929048116855,.827201315069765,.9284348836635735,.9862838086968123],[0,.20119409399743451,.3941513470775634,.5709721726085388,.7244177313601701,.8482065834104272,.937273392400706,.9879925180204854],[.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]],e=[[1],[.8888888888888888,.5555555555555556],[.6521451548625461,.34785484513745385],[.5688888888888889,.47862867049936647,.23692688505618908],[.46791393457269104,.3607615730481386,.17132449237917036],[.4179591836734694,.3818300505051189,.27970539148927664,.1294849661688697],[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626],[.3302393550012598,.31234707704000286,.26061069640293544,.1806481606948574,.08127438836157441],[.29552422471475287,.26926671930999635,.21908636251598204,.1494513491505806,.06667134430868814],[.2729250867779006,.26280454451024665,.23319376459199048,.18629021092773426,.1255803694649046,.05566856711617366],[.24914704581340277,.2334925365383548,.20316742672306592,.16007832854334622,.10693932599531843,.04717533638651183],[.2325515532308739,.22628318026289723,.2078160475368885,.17814598076194574,.13887351021978725,.09212149983772845,.04048400476531588],[.2152638534631578,.2051984637212956,.18553839747793782,.15720316715819355,.12151857068790319,.08015808715976021,.03511946033175186],[.2025782419255613,.19843148532711158,.1861610000155622,.16626920581699392,.13957067792615432,.10715922046717194,.07036604748810812,.03075324199611727],[.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096]],i=Math.abs,n=Math.sqrt,r=Math.pow,s=1e-6,o=1e-12,h=1.12e-16;return{TOLERANCE:s,EPSILON:o,MACHINE_EPSILON:h,KAPPA:4*(n(2)-1)/3,isZero:function(t){return i(t)<=o},integrate:function(i,n,r,s){for(var a=t[s-2],o=e[s-2],h=.5*(r-n),u=h+n,l=0,c=s+1>>1,d=1&s?o[l++]*i(u):0;c>l;){var _=h*a[l];d+=o[l++]*(i(u+_)+i(u-_))}return h*d},findRoot:function(t,e,n,r,s,a,o){for(var h=0;a>h;h++){var u=t(n),l=u/e(n),c=n-l;if(i(l)0?(s=n,n=r>=c?.5*(r+s):c):(r=n,n=c>=s?.5*(r+s):c)}return n},solveQuadratic:function(t,e,s,a,u,l){var c,d,_=0,f=1/0,g=e;if(e/=2,d=e*e-t*s,0!==d&&i(d)v){var p=r(10,i(Math.floor(Math.log(v)*Math.LOG10E)));isFinite(p)||(p=0),t*=p,e*=p,s*=p,d=e*e-t*s}}if(i(t)=-h){d=0>d?0:d;var m=n(d);if(e>=h&&h>=e)c=i(t)>=i(s)?m/t:-s/m,f=-c;else{var y=-(e+(0>e?-1:1)*m);c=y/t,f=s/y}}return isFinite(c)&&(null==u||c>=u&&l>=c)&&(a[_++]=c),f!==c&&isFinite(f)&&(null==u||f>=u&&l>=f)&&(a[_++]=f),_},solveCubic:function(t,e,s,u,l,c,d){var _,f,g,v=0;if(i(t)w?-1:1,w=-y/t,x=w>0?1.3247179572*Math.max(x,n(w)):x,p=_-b*x,p!==_){do if(_=p,C=t*_,f=C+e,g=f*_+s,y=(C+f)*_+g,m=g*_+u,p=0===y?_:_-m/y/S,p===_){_=p;break}while(b*p>b*_);i(t)*_*_>i(u/_)&&(g=-u/_,f=(g-s)/_)}}var v=a.solveQuadratic(t,f,g,l,c,d);return isFinite(_)&&(0===v||_!==l[v-1])&&(null==c||_>=c&&d>=_)&&(l[v++]=_),v}}},o={_id:1,_pools:{},get:function(t){if(t){var e=t._class,i=this._pools[e];return i||(i=this._pools[e]={_id:1}),i._id++}return this._id++}},h=e.extend({_class:"Point",_readIndex:!0,initialize:function(t,e){var i=typeof t;if("number"===i){var n="number"==typeof e;this.x=t,this.y=n?e:t,this.__read&&(this.__read=n?2:1)}else"undefined"===i||null===t?(this.x=this.y=0,this.__read&&(this.__read=null===t?1:0)):(Array.isArray(t)?(this.x=t[0],this.y=t.length>1?t[1]:t[0]):null!=t.x?(this.x=t.x,this.y=t.y):null!=t.width?(this.x=t.width,this.y=t.height):null!=t.angle?(this.x=t.length,this.y=0,this.setAngle(t.angle)):(this.x=this.y=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.x=t,this.y=e,this},equals:function(t){return this===t||t&&(this.x===t.x&&this.y===t.y||Array.isArray(t)&&this.x===t[0]&&this.y===t[1])||!1},clone:function(){return new h(this.x,this.y)},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y)]},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(t){if(this.isZero()){var e=this._angle||0;this.set(Math.cos(e)*t,Math.sin(e)*t)}else{var i=t/this.getLength();a.isZero(i)&&this.getAngle(),this.set(this.x*i,this.y*i)}},getAngle:function(){return 180*this.getAngleInRadians.apply(this,arguments)/Math.PI},setAngle:function(t){this.setAngleInRadians.call(this,t*Math.PI/180)},getAngleInDegrees:"#getAngle",setAngleInDegrees:"#setAngle",getAngleInRadians:function(){if(arguments.length){var t=h.read(arguments),e=this.getLength()*t.getLength();if(a.isZero(e))return NaN;var i=this.dot(t)/e;return Math.acos(-1>i?-1:i>1?1:i)}return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x)},setAngleInRadians:function(t){if(this._angle=t,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3}},{beans:!1,getDirectedAngle:function(){var t=h.read(arguments);return 180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},getDistance:function(){var t=h.read(arguments),i=t.x-this.x,n=t.y-this.y,r=i*i+n*n,s=e.read(arguments);return s?r:Math.sqrt(r)},normalize:function(e){e===t&&(e=1);var i=this.getLength(),n=0!==i?e/i:0,r=new h(this.x*n,this.y*n);return n>=0&&(r._angle=this._angle),r},rotate:function(t,e){if(0===t)return this.clone();t=t*Math.PI/180;var i=e?this.subtract(e):this,n=Math.sin(t),r=Math.cos(t);return i=new h(i.x*r-i.y*n,i.x*n+i.y*r),e?i.add(e):i},transform:function(t){return t?t._transformPoint(this):this},add:function(){var t=h.read(arguments);return new h(this.x+t.x,this.y+t.y)},subtract:function(){var t=h.read(arguments);return new h(this.x-t.x,this.y-t.y)},multiply:function(){var t=h.read(arguments);return new h(this.x*t.x,this.y*t.y)},divide:function(){var t=h.read(arguments);return new h(this.x/t.x,this.y/t.y)},modulo:function(){var t=h.read(arguments);return new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},isInside:function(){return d.read(arguments).contains(this)},isClose:function(t,e){return this.getDistance(t)1?t[1]:t[0]):null!=t.width?(this.width=t.width,this.height=t.height):null!=t.x?(this.width=t.x,this.height=t.y):(this.width=this.height=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.width=t,this.height=e,this},equals:function(t){return t===this||t&&(this.width===t.width&&this.height===t.height||Array.isArray(t)&&this.width===t[0]&&this.height===t[1])||!1},clone:function(){return new l(this.width,this.height)},toString:function(){var t=s.instance;return"{ width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.width),e.number(this.height)]},add:function(){var t=l.read(arguments);return new l(this.width+t.width,this.height+t.height)},subtract:function(){var t=l.read(arguments);return new l(this.width-t.width,this.height-t.height)},multiply:function(){var t=l.read(arguments);return new l(this.width*t.width,this.height*t.height)},divide:function(){var t=l.read(arguments);return new l(this.width/t.width,this.height/t.height)},modulo:function(){var t=l.read(arguments);return new l(this.width%t.width,this.height%t.height)},negate:function(){return new l(-this.width,-this.height)},isZero:function(){return a.isZero(this.width)&&a.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new l(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new l(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new l(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new l(e(this.width),e(this.height))}},{})),c=l.extend({initialize:function(t,e,i,n){this._width=t,this._height=e,this._owner=i,this._setter=n},set:function(t,e,i){return this._width=t,this._height=e,i||this._owner[this._setter](this),this},getWidth:function(){return this._width},setWidth:function(t){this._width=t,this._owner[this._setter](this)},getHeight:function(){return this._height},setHeight:function(t){this._height=t,this._owner[this._setter](this)}}),d=e.extend({_class:"Rectangle",_readIndex:!0,beans:!0,initialize:function(i,n,r,s){var a=typeof i,o=0;if("number"===a?(this.x=i,this.y=n,this.width=r,this.height=s,o=4):"undefined"===a||null===i?(this.x=this.y=this.width=this.height=0,o=null===i?1:0):1===arguments.length&&(Array.isArray(i)?(this.x=i[0],this.y=i[1],this.width=i[2],this.height=i[3],o=1):i.x!==t||i.width!==t?(this.x=i.x||0,this.y=i.y||0,this.width=i.width||0,this.height=i.height||0,o=1):i.from===t&&i.to===t&&(this.x=this.y=this.width=this.height=0,this._set(i),o=1)),!o){var u=h.readNamed(arguments,"from"),c=e.peek(arguments);if(this.x=u.x,this.y=u.y,c&&c.x!==t||e.hasNamed(arguments,"to")){var d=h.readNamed(arguments,"to");this.width=d.x-u.x,this.height=d.y-u.y,this.width<0&&(this.x=d.x,this.width=-this.width),this.height<0&&(this.y=d.y,this.height=-this.height)}else{var _=l.read(arguments);this.width=_.width,this.height=_.height}o=arguments.__index}this.__read&&(this.__read=o)},set:function(t,e,i,n){return this.x=t,this.y=e,this.width=i,this.height=n,this},clone:function(){return new d(this.x,this.y,this.width,this.height)},equals:function(t){var i=e.isPlainValue(t)?d.read(arguments):t;return i===this||i&&this.x===i.x&&this.y===i.y&&this.width===i.width&&this.height===i.height||!1},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+", width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y),e.number(this.width),e.number(this.height)]},getPoint:function(t){var e=t?h:u;return new e(this.x,this.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.x=t.x,this.y=t.y},getSize:function(t){var e=t?l:c;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=l.read(arguments);this._fixX&&(this.x+=(this.width-t.width)*this._fixX),this._fixY&&(this.y+=(this.height-t.height)*this._fixY),this.width=t.width,this.height=t.height,this._fixW=1,this._fixH=1},getLeft:function(){return this.x},setLeft:function(t){this._fixW||(this.width-=t-this.x),this.x=t,this._fixX=0},getTop:function(){return this.y},setTop:function(t){this._fixH||(this.height-=t-this.y),this.y=t,this._fixY=0},getRight:function(){return this.x+this.width},setRight:function(e){this._fixX!==t&&1!==this._fixX&&(this._fixW=0),this._fixW?this.x=e-this.width:this.width=e-this.x,this._fixX=1},getBottom:function(){return this.y+this.height},setBottom:function(e){this._fixY!==t&&1!==this._fixY&&(this._fixH=0),this._fixH?this.y=e-this.height:this.height=e-this.y,this._fixY=1},getCenterX:function(){return this.x+.5*this.width},setCenterX:function(t){this.x=t-.5*this.width,this._fixX=.5},getCenterY:function(){return this.y+.5*this.height},setCenterY:function(t){this.y=t-.5*this.height,this._fixY=.5},getCenter:function(t){var e=t?h:u;return new e(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(){var t=h.read(arguments);return this.setCenterX(t.x),this.setCenterY(t.y),this},getArea:function(){return this.width*this.height},isEmpty:function(){return 0===this.width||0===this.height},contains:function(e){return e&&e.width!==t||4==(Array.isArray(e)?e:arguments).length?this._containsRectangle(d.read(arguments)):this._containsPoint(h.read(arguments))},_containsPoint:function(t){var e=t.x,i=t.y;return e>=this.x&&i>=this.y&&e<=this.x+this.width&&i<=this.y+this.height},_containsRectangle:function(t){var e=t.x,i=t.y;return e>=this.x&&i>=this.y&&e+t.width<=this.x+this.width&&i+t.height<=this.y+this.height},intersects:function(){var t=d.read(arguments);return t.x+t.width>this.x&&t.y+t.height>this.y&&t.x=this.x&&t.y+t.height>=this.y&&t.x<=this.x+this.width&&t.y<=this.y+this.height},intersect:function(){var t=d.read(arguments),e=Math.max(this.x,t.x),i=Math.max(this.y,t.y),n=Math.min(this.x+this.width,t.x+t.width),r=Math.min(this.y+this.height,t.y+t.height);return new d(e,i,n-e,r-i)},unite:function(){var t=d.read(arguments),e=Math.min(this.x,t.x),i=Math.min(this.y,t.y),n=Math.max(this.x+this.width,t.x+t.width),r=Math.max(this.y+this.height,t.y+t.height);return new d(e,i,n-e,r-i)},include:function(){var t=h.read(arguments),e=Math.min(this.x,t.x),i=Math.min(this.y,t.y),n=Math.max(this.x+this.width,t.x),r=Math.max(this.y+this.height,t.y);return new d(e,i,n-e,r-i)},expand:function(){var t=l.read(arguments),e=t.width,i=t.height;return new d(this.x-e/2,this.y-i/2,this.width+e,this.height+i)},scale:function(e,i){return this.expand(this.width*e-this.width,this.height*(i===t?e:i)-this.height)}},e.each([["Top","Left"],["Top","Right"],["Bottom","Left"],["Bottom","Right"],["Left","Center"],["Top","Center"],["Right","Center"],["Bottom","Center"]],function(t,e){var i=t.join(""),n=/^[RL]/.test(i);e>=4&&(t[1]+=n?"Y":"X");var r=t[n?0:1],s=t[n?1:0],a="get"+r,o="get"+s,l="set"+r,c="set"+s,d="get"+i,_="set"+i;this[d]=function(t){var e=t?h:u;return new e(this[a](),this[o](),this,_)},this[_]=function(){var t=h.read(arguments);this[l](t.x),this[c](t.y)}},{beans:!0})),_=d.extend({initialize:function(t,e,i,n,r,s){this.set(t,e,i,n,!0),this._owner=r,this._setter=s},set:function(t,e,i,n,r){return this._x=t,this._y=e,this._width=i,this._height=n,r||this._owner[this._setter](this),this}},new function(){var t=d.prototype;return e.each(["x","y","width","height"],function(t){var i=e.capitalize(t),n="_"+t;this["get"+i]=function(){return this[n]},this["set"+i]=function(t){this[n]=t,this._dontNotify||this._owner[this._setter](this)}},e.each(["Point","Size","Center","Left","Top","Right","Bottom","CenterX","CenterY","TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],function(e){var i="set"+e;this[i]=function(){this._dontNotify=!0,t[i].apply(this,arguments),this._dontNotify=!1,this._owner[this._setter](this)}},{isSelected:function(){return this._owner._boundsSelected},setSelected:function(t){var e=this._owner;e.setSelected&&(e._boundsSelected=t,e.setSelected(t||e._selectedSegmentState>0))}}))}),f=e.extend({_class:"Matrix",initialize:function nt(t){var e=arguments.length,i=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof nt?this.set(t._a,t._c,t._b,t._d,t._tx,t._ty):Array.isArray(t)?this.set.apply(this,t):i=!1:0===e?this.reset():i=!1,!i)throw Error("Unsupported matrix parameters")},set:function(t,e,i,n,r,s,a){return this._a=t,this._c=e,this._b=i,this._d=n,this._tx=r,this._ty=s,a||this._changed(),this},_serialize:function(t){return e.serialize(this.getValues(),t)},_changed:function(){var t=this._owner;t&&(t._applyMatrix?t.transform(null,!0):t._changed(9))},clone:function(){return new f(this._a,this._c,this._b,this._d,this._tx,this._ty)},equals:function(t){return t===this||t&&this._a===t._a&&this._b===t._b&&this._c===t._c&&this._d===t._d&&this._tx===t._tx&&this._ty===t._ty||!1},toString:function(){var t=s.instance;return"[["+[t.number(this._a),t.number(this._b),t.number(this._tx)].join(", ")+"], ["+[t.number(this._c),t.number(this._d),t.number(this._ty)].join(", ")+"]]"},reset:function(t){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,t||this._changed(),this},apply:function(t,i){var n=this._owner;return n?(n.transform(null,!0,e.pick(t,!0),i),this.isIdentity()):!1},translate:function(){var t=h.read(arguments),e=t.x,i=t.y;return this._tx+=e*this._a+i*this._b,this._ty+=e*this._c+i*this._d,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});return e&&this.translate(e),this._a*=t.x,this._c*=t.x,this._b*=t.y,this._d*=t.y,e&&this.translate(e.negate()),this._changed(),this},rotate:function(t){t*=Math.PI/180;var e=h.read(arguments,1),i=e.x,n=e.y,r=Math.cos(t),s=Math.sin(t),a=i-i*r+n*s,o=n-i*s-n*r,u=this._a,l=this._b,c=this._c,d=this._d;return this._a=r*u+s*l,this._b=-s*u+r*l,this._c=r*c+s*d,this._d=-s*c+r*d,this._tx+=a*u+o*l,this._ty+=a*c+o*d,this._changed(),this},shear:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});e&&this.translate(e);var i=this._a,n=this._c;return this._a+=t.y*this._b,this._c+=t.y*this._d,this._b+=t.x*i,this._d+=t.x*n,e&&this.translate(e.negate()),this._changed(),this},skew:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0}),i=Math.PI/180,n=new h(Math.tan(t.x*i),Math.tan(t.y*i));return this.shear(n,e)},concatenate:function(t){var e=this._a,i=this._b,n=this._c,r=this._d,s=t._a,a=t._b,o=t._c,h=t._d,u=t._tx,l=t._ty;return this._a=s*e+o*i,this._b=a*e+h*i,this._c=s*n+o*r,this._d=a*n+h*r,this._tx+=u*e+l*i,this._ty+=u*n+l*r,this._changed(),this},preConcatenate:function(t){var e=this._a,i=this._b,n=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return this._a=o*e+h*n,this._b=o*i+h*r,this._c=u*e+l*n,this._d=u*i+l*r,this._tx=o*s+h*a+c,this._ty=u*s+l*a+d,this._changed(),this},chain:function(t){var e=this._a,i=this._b,n=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty; -return new f(o*e+u*i,o*n+u*r,h*e+l*i,h*n+l*r,s+c*e+d*i,a+c*n+d*r)},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},orNullIfIdentity:function(){return this.isIdentity()?null:this},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,i){return arguments.length<3?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,i)},_transformPoint:function(t,e,i){var n=t.x,r=t.y;return e||(e=new h),e.set(n*this._a+r*this._b+this._tx,n*this._c+r*this._d+this._ty,i)},_transformCoordinates:function(t,e,i){for(var n=0,r=0,s=2*i;s>n;){var a=t[n++],o=t[n++];e[r++]=a*this._a+o*this._b+this._tx,e[r++]=a*this._c+o*this._d+this._ty}return e},_transformCorners:function(t){var e=t.x,i=t.y,n=e+t.width,r=i+t.height,s=[e,i,n,i,n,r,e,r];return this._transformCoordinates(s,s,4)},_transformBounds:function(t,e,i){for(var n=this._transformCorners(t),r=n.slice(0,2),s=n.slice(),a=2;8>a;a++){var o=n[a],h=1&a;os[h]&&(s[h]=o)}return e||(e=new d),e.set(r[0],r[1],s[0]-r[0],s[1]-r[1],i)},inverseTransform:function(){return this._inverseTransform(h.read(arguments))},_getDeterminant:function(){var t=this._a*this._d-this._b*this._c;return isFinite(t)&&!a.isZero(t)&&isFinite(this._tx)&&isFinite(this._ty)?t:null},_inverseTransform:function(t,e,i){var n=this._getDeterminant();if(!n)return null;var r=t.x-this._tx,s=t.y-this._ty;return e||(e=new h),e.set((r*this._d-s*this._b)/n,(s*this._a-r*this._c)/n,i)},decompose:function(){var t=this._a,e=this._b,i=this._c,n=this._d;if(a.isZero(t*n-e*i))return null;var r=Math.sqrt(t*t+e*e);t/=r,e/=r;var s=t*i+e*n;i-=t*s,n-=e*s;var o=Math.sqrt(i*i+n*n);return i/=o,n/=o,s/=o,e*i>t*n&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,o),rotation:180*-Math.atan2(e,t)/Math.PI,shearing:s}},getValues:function(){return[this._a,this._c,this._b,this._d,this._tx,this._ty]},getTranslation:function(){return new h(this._tx,this._ty)},getScaling:function(){return(this.decompose()||{}).scaling},getRotation:function(){return(this.decompose()||{}).rotation},inverted:function(){var t=this._getDeterminant();return t&&new f(this._d/t,-this._c/t,-this._b/t,this._a/t,(this._b*this._ty-this._d*this._tx)/t,(this._c*this._tx-this._a*this._ty)/t)},shiftless:function(){return new f(this._a,this._c,this._b,this._d,0,0)},applyToContext:function(t){t.transform(this._a,this._c,this._b,this._d,this._tx,this._ty)}},e.each(["a","c","b","d","tx","ty"],function(t){var i=e.capitalize(t),n="_"+t;this["get"+i]=function(){return this[n]},this["set"+i]=function(t){this[n]=t,this._changed()}},{})),g=e.extend({_class:"Line",initialize:function(t,e,i,n,r){var s=!1;arguments.length>=4?(this._px=t,this._py=e,this._vx=i,this._vy=n,s=r):(this._px=t.x,this._py=t.y,this._vx=e.x,this._vy=e.y,s=i),s||(this._vx-=this._px,this._vy-=this._py)},getPoint:function(){return new h(this._px,this._py)},getVector:function(){return new h(this._vx,this._vy)},getLength:function(){return this.getVector().getLength()},intersect:function(t,e){return g.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return g.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(g.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,i,n,r,s,o,u,l,c){l||(i-=t,n-=e,o-=r,u-=s);var d=i*u-n*o;if(!a.isZero(d)){var _=t-r,f=e-s,g=(o*f-u*_)/d,v=(i*f-n*_)/d;if(c||g>=0&&1>=g&&v>=0&&1>=v)return new h(t+g*i,e+g*n)}},getSide:function(t,e,i,n,r,s,a){a||(i-=t,n-=e);var o=r-t,h=s-e,u=o*n-h*i;return 0===u&&(u=o*i+h*n,u>0&&(o-=i,h-=n,u=o*i+h*n,0>u&&(u=0))),0>u?-1:u>0?1:0},getSignedDistance:function(t,e,i,n,r,s,o){return o||(i-=t,n-=e),a.isZero(i)?n>=0?t-r:r-t:a.isZero(n)?i>=0?s-e:e-s:(i*(s-e)-n*(r-t))/Math.sqrt(i*i+n*n)}}}),v=r.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){r.call(this,!0),this.layers=[],this._activeLayer=null,this.symbols=[],this._currentStyle=new R(null,null,this),this._view=V.create(this,t||Y.getCanvas(1,1)),this._selectedItems={},this._selectedItemCount=0,this._updateVersion=0},_serialize:function(t,i){return e.serialize(this.layers,t,!0,i)},clear:function(){for(var t=this.layers.length-1;t>=0;t--)this.layers[t].remove();this.symbols=[]},isEmpty:function(){return 0===this.layers.length},remove:function rt(){return rt.base.call(this)?(this._view&&this._view.remove(),!0):!1},getView:function(){return this._view},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},getOptions:function(){return this._scope.settings},getActiveLayer:function(){return this._activeLayer||new w({project:this})},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var i=this._selectedItems[e];i.isInserted()&&t.push(i)}return t},insertChild:function(t,i,n){return i instanceof w?(i._remove(!1,!0),e.splice(this.layers,[i],t,0),i._setProject(this,!0),this._changes&&i._changed(5),this._activeLayer||(this._activeLayer=i)):i instanceof m?(this._activeLayer||this.insertChild(t,new w(m.NO_INSERT))).insertChild(t,i,n):i=null,i},addChild:function(e,i){return this.insertChild(t,e,i)},_updateSelection:function(t){var e=t._id,i=this._selectedItems;t._selected?i[e]!==t&&(this._selectedItemCount++,i[e]=t):i[e]===t&&(this._selectedItemCount--,delete i[e])},selectAll:function(){for(var t=this.layers,e=0,i=t.length;i>e;e++)t[e].setFullySelected(!0)},deselectAll:function(){var t=this._selectedItems;for(var e in t)t[e].setFullySelected(!1)},hitTest:function(){for(var t=h.read(arguments),i=S.getOptions(e.read(arguments)),n=this.layers.length-1;n>=0;n--){var r=this.layers[n]._hitTest(t,i);if(r)return r}return null},getItems:function(t){return m._getItems(this.layers,t)},getItem:function(t){return m._getItems(this.layers,t,null,null,!0)[0]||null},importJSON:function(t){this.activate();var i=this._activeLayer;return e.importJSON(t,i&&i.isEmpty()&&i)},draw:function(t,i,n){this._updateVersion++,t.save(),i.applyToContext(t);for(var r=new e({offset:new h(0,0),pixelRatio:n,viewMatrix:i.isIdentity()?null:i,matrices:[new f],updateMatrix:!0}),s=0,a=this.layers,o=a.length;o>s;s++)a[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;var u=this._selectedItems,l=this._scope.settings.handleSize,c=this._updateVersion;for(var d in u)u[d]._drawSelection(t,i,l,u,c);t.restore()}}}),p=e.extend({_class:"Symbol",initialize:function(t,e){this._id=o.get(),this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e)},_serialize:function(t,i){return i.add(this,function(){return e.serialize([this._class,this._definition],t,!1,i)})},_changed:function(t){8&t&&m._clearBoundsCache(this),1&t&&(this.project._needsUpdate=!0)},getDefinition:function(){return this._definition},setDefinition:function(t,e){t._parentSymbol&&(t=t.clone()),this._definition&&(this._definition._parentSymbol=null),this._definition=t,t.remove(),t.setSelected(!1),e||t.setPosition(new h),t._parentSymbol=this,this._changed(9)},place:function(t){return new C(this,t)},clone:function(){return new p(this._definition.clone(!1))},equals:function(t){return t===this||t&&this.definition.equals(t.definition)||!1}}),m=e.extend(i,{statics:{extend:function st(t){return t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields)),st.base.apply(this,arguments)},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,applyMatrix:null,matrix:new f,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,i){var n=t&&e.isPlainObject(t),r=n&&t.internal===!0,s=this._matrix=new f,a=n&&t.project||paper.project;return r||(this._id=o.get()),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,i&&s.translate(i),s._owner=this,this._style=new R(a._currentStyle,this,a),this._project||(r||n&&t.insert===!1?this._setProject(a):n&&t.parent?this.setParent(t.parent):(a._activeLayer||new w).addChild(this)),n&&t!==m.NO_INSERT&&this._set(t,{insert:!0,project:!0,parent:!0},!0),n},_events:new function(){var t={mousedown:{mousedown:1,mousedrag:1,click:1,doubleclick:1},mouseup:{mouseup:1,mousedrag:1,click:1,doubleclick:1},mousemove:{mousedrag:1,mousemove:1,mouseenter:1,mouseleave:1}},i={install:function(e){var i=this.getView()._eventCounters;if(i)for(var n in t)i[n]=(i[n]||0)+(t[n][e]||0)},uninstall:function(e){var i=this.getView()._eventCounters;if(i)for(var n in t)i[n]-=t[n][e]||0}};return e.each(["onMouseDown","onMouseUp","onMouseDrag","onClick","onDoubleClick","onMouseMove","onMouseEnter","onMouseLeave"],function(t){this[t]=i},{onFrame:{install:function(){this._animateItem(!0)},uninstall:function(){this._animateItem(!1)}},onLoad:{}})},_animateItem:function(t){this.getView()._animateItem(this,t)},_serialize:function(t,i){function n(n){for(var a in n){var o=s[a];e.equals(o,"leading"===a?1.2*n.fontSize:n[a])||(r[a]=e.serialize(o,t,"data"!==a,i))}}var r={},s=this;return n(this._serializeFields),this instanceof y||n(this._style._defaults),[this._class,r]},_changed:function(e){var i=this._parentSymbol,n=this._parent||i,r=this._project;if(8&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=this._currentPath=t),n&&40&e&&m._clearBoundsCache(n),2&e&&m._clearBoundsCache(this),r&&(1&e&&(r._needsUpdate=!0),r._changes)){var s=r._changesById[this._id];s?s.flags|=e:(s={item:this,flags:e},r._changesById[this._id]=s,r._changes.push(s))}i&&i._changed(e)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getName:function(){return this._name},setName:function(e,i){if(this._name&&this._removeNamed(),e===+e+"")throw Error("Names consisting only of numbers are not supported.");var n=this._parent;if(e&&n){for(var r=n._children,s=n._namedChildren,a=e,o=1;i&&r[e];)e=a+" "+o++;(s[e]=s[e]||[]).push(this),r[e]=this}this._name=e||t,this._changed(128)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)}},e.each(["locked","visible","blendMode","opacity","guide"],function(t){var i=e.capitalize(t),t="_"+t;this["get"+i]=function(){return this[t]},this["set"+i]=function(e){e!=this[t]&&(this[t]=e,this._changed("_locked"===t?128:129))}},{}),{beans:!0,_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=this._children,e=0,i=t.length;i>e;e++)if(t[e].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var i=this._children,n=0,r=i.length;r>n;n++)i[n].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(129))},_selected:!1,isFullySelected:function(){var t=this._children;if(t&&this._selected){for(var e=0,i=t.length;i>e;e++)if(!t[e].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){var e=this._children;if(e)for(var i=0,n=e.length;n>i;i++)e[i].setFullySelected(t);this.setSelected(t,!0)},isClipMask:function(){return this._clipMask},setClipMask:function(t){this._clipMask!=(t=!!t)&&(this._clipMask=t,t&&(this.setFillColor(null),this.setStrokeColor(null)),this._changed(129),this._parent&&this._parent._changed(1024))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(t){var e=this._position,i=t?h:u;if(!e){var n=this._pivot;e=this._position=n?this._matrix._transformPoint(n):this.getBounds().getCenter(!0)}return new i(e.x,e.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))},getPivot:function(t){var e=this._pivot;if(e){var i=t?h:u;e=new i(e.x,e.y,this,"setPivot")}return e},setPivot:function(){this._pivot=h.read(arguments,0,{clone:!0,readNull:!0}),this._position=t},_pivot:null},e.each(["bounds","strokeBounds","handleBounds","roughBounds","internalBounds","internalRoughBounds"],function(t){var i="get"+e.capitalize(t),n=t.match(/^internal(.*)$/),r=n?"get"+n[1]:null;this[i]=function(e){var n=this._boundsGetter,s=!r&&("string"==typeof n?n:n&&n[i])||i,a=this._getCachedBounds(s,e,this,r);return"bounds"===t?new _(a.x,a.y,a.width,a.height,this,"setBounds"):a}},{beans:!0,_getBounds:function(t,e,i){var n=this._children;if(!n||0==n.length)return new d;m._updateBoundsCache(this,i);for(var r=1/0,s=-r,a=r,o=s,h=0,u=n.length;u>h;h++){var l=n[h];if(l._visible&&!l.isEmpty()){var c=l._getCachedBounds(t,e&&e.chain(l._matrix),i);r=Math.min(c.x,r),a=Math.min(c.y,a),s=Math.max(c.x+c.width,s),o=Math.max(c.y+c.height,o)}}return isFinite(r)?new d(r,a,s-r,o-a):new d},setBounds:function(){var t=d.read(arguments),e=this.getBounds(),i=new f,n=t.getCenter();i.translate(n),(t.width!=e.width||t.height!=e.height)&&i.scale(0!=e.width?t.width/e.width:1,0!=e.height?t.height/e.height:1),n=e.getCenter(),i.translate(-n.x,-n.y),this.transform(i)},_getCachedBounds:function(t,e,i,n){e=e&&e.orNullIfIdentity();var r=n?null:this._matrix.orNullIfIdentity(),s=(!e||e.equals(r))&&t;if(m._updateBoundsCache(this._parent||this._parentSymbol,i),s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();var a=this._getBounds(n||t,e||r,i);if(s){this._bounds||(this._bounds={});var o=this._bounds[s]=a.clone();o._internal=!!n}return a},statics:{_updateBoundsCache:function(t,e){if(t){var i=e._id,n=t._boundsCache=t._boundsCache||{ids:{},list:[]};n.ids[i]||(n.list.push(e),n.ids[i]=e)}},_clearBoundsCache:function(e){var i=e._boundsCache;if(i){e._bounds=e._position=e._boundsCache=t;for(var n=0,r=i.list,s=r.length;s>n;n++){var a=r[n];a!==e&&(a._bounds=a._position=t,a._boundsCache&&m._clearBoundsCache(a))}}}}}),{beans:!0,_decompose:function(){return this._decomposed=this._matrix.decompose()},getRotation:function(){var t=this._decomposed||this._decompose();return t&&t.rotation},setRotation:function(t){var e=this.getRotation();if(null!=e&&null!=t){var i=this._decomposed;this.rotate(t-e),i.rotation=t,this._decomposed=i}},getScaling:function(t){var e=this._decomposed||this._decompose(),i=e&&e.scaling,n=t?h:u;return i&&new n(i.x,i.y,this,"setScaling")},setScaling:function(){var t=this.getScaling();if(t){var e=h.read(arguments,0,{clone:!0}),i=this._decomposed;this.scale(e.x/t.x,e.y/t.y),i.scaling=e,this._decomposed=i}},getMatrix:function(){return this._matrix},setMatrix:function(){var t=this._matrix;t.initialize.apply(t,arguments),this._applyMatrix?this.transform(null,!0):this._changed(9)},getGlobalMatrix:function(t){var e=this._globalMatrix,i=this._project._updateVersion;if(e&&e._updateVersion!==i&&(e=null),!e){e=this._globalMatrix=this._matrix.clone();var n=this._parent;n&&e.preConcatenate(n.getGlobalMatrix(!0)),e._updateVersion=i}return t?e:e.clone()},getApplyMatrix:function(){return this._applyMatrix},setApplyMatrix:function(t){(this._applyMatrix=this._canApplyMatrix&&!!t)&&this.transform(null,!0)},getTransformContent:"#getApplyMatrix",setTransformContent:"#setApplyMatrix"},{getProject:function(){return this._project},_setProject:function(t,e){if(this._project!==t){this._project&&this._installEvents(!1),this._project=t;for(var i=this._children,n=0,r=i&&i.length;r>n;n++)i[n]._setProject(t);e=!0}e&&this._installEvents(!0)},getView:function(){return this._project.getView()},_installEvents:function at(t){at.base.call(this,t);for(var e=this._children,i=0,n=e&&e.length;n>i;i++)e[i]._installEvents(t)},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof w)return t;return null},getParent:function(){return this._parent},setParent:function(t){return t.addChild(this)},getChildren:function(){return this._children},setChildren:function(t){this.removeChildren(),this.addChildren(t)},getFirstChild:function(){return this._children&&this._children[0]||null},getLastChild:function(){return this._children&&this._children[this._children.length-1]||null},getNextSibling:function(){return this._parent&&this._parent._children[this._index+1]||null},getPreviousSibling:function(){return this._parent&&this._parent._children[this._index-1]||null},getIndex:function(){return this._index},equals:function(t){return t===this||t&&this._class===t._class&&this._style.equals(t._style)&&this._matrix.equals(t._matrix)&&this._locked===t._locked&&this._visible===t._visible&&this._blendMode===t._blendMode&&this._opacity===t._opacity&&this._clipMask===t._clipMask&&this._guide===t._guide&&this._equals(t)||!1},_equals:function(t){return e.equals(this._children,t._children)},clone:function(t){return this._clone(new this.constructor(m.NO_INSERT),t)},_clone:function(i,n,r){var s=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],a=this._children;i.setStyle(this._style);for(var o=0,h=a&&a.length;h>o;o++)i.addChild(a[o].clone(!1),!0);for(var o=0,h=s.length;h>o;o++){var u=s[o];this.hasOwnProperty(u)&&(i[u]=this[u])}return r!==!1&&i._matrix.initialize(this._matrix),i.setApplyMatrix(this._applyMatrix),i.setPivot(this._pivot),i.setSelected(this._selected),i._data=this._data?e.clone(this._data):null,(n||n===t)&&i.insertAbove(this),this._name&&i.setName(this._name,!0),i},copyTo:function(t){return t.addChild(this.clone(!1))},rasterize:function(t){var i=this.getStrokeBounds(),n=(t||this.getView().getResolution())/72,r=i.getTopLeft().floor(),s=i.getBottomRight().ceil(),a=new l(s.subtract(r)),o=Y.getCanvas(a.multiply(n)),h=o.getContext("2d"),u=(new f).scale(n).translate(r.negate());h.save(),u.applyToContext(h),this.draw(h,new e({matrices:[u]})),h.restore();var c=new b(m.NO_INSERT);return c.setCanvas(o),c.transform((new f).translate(r.add(a.divide(2))).scale(1/n)),c.insertAbove(this),c},contains:function(){return!!this._contains(this._matrix._inverseTransform(h.read(arguments)))},_contains:function(t){if(this._children){for(var e=this._children.length-1;e>=0;e--)if(this._children[e].contains(t))return!0;return!1}return t.isInside(this.getInternalBounds())},isInside:function(){return d.read(arguments).contains(this.getBounds())},_asPathItem:function(){return new O.Rectangle({rectangle:this.getInternalBounds(),matrix:this._matrix,insert:!1})},intersects:function(t,e){return t instanceof m?this._asPathItem().getIntersections(t._asPathItem(),e||t._matrix).length>0:!1},hitTest:function(){return this._hitTest(h.read(arguments),S.getOptions(e.read(arguments)))},_hitTest:function(i,n){function r(n,r){var s=f["get"+r]();return i.subtract(s).divide(u).length<=1?new S(n,_,{name:e.hyphenate(r),point:s}):t}if(this._locked||!this._visible||this._guide&&!n.guides||this.isEmpty())return null;var s=this._matrix,a=n._totalMatrix,o=this.getView(),h=n._totalMatrix=a?a.chain(s):this.getGlobalMatrix().preConcatenate(o._matrix),u=n._tolerancePadding=new l(O._getPenPadding(1,h.inverted())).multiply(Math.max(n.tolerance,1e-6));if(i=s._inverseTransform(i),!this._children&&!this.getInternalRoughBounds().expand(u.multiply(2))._containsPoint(i))return null;var c,d=!(n.guides&&!this._guide||n.selected&&!this._selected||n.type&&n.type!==e.hyphenate(this._class)||n["class"]&&!(this instanceof n["class"])),_=this;if(d&&(n.center||n.bounds)&&this._parent){var f=this.getInternalBounds();if(n.center&&(c=r("center","Center")),!c&&n.bounds)for(var g=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],v=0;8>v&&!c;v++)c=r("bounds",g[v])}var p=!c&&this._children;if(p)for(var m=this._getChildHitTestOptions(n),v=p.length-1;v>=0&&!c;v--)c=p[v]._hitTest(i,m);return!c&&d&&(c=this._hitTestSelf(i,n)),c&&c.point&&(c.point=s.transform(c.point)),n._totalMatrix=a,c},_getChildHitTestOptions:function(t){return t},_hitTestSelf:function(e,i){return i.fill&&this.hasFill()&&this._contains(e)?new S("fill",this):t},matches:function(t,i){function n(t,i){for(var r in t)if(t.hasOwnProperty(r)){var s=t[r],a=i[r];if(e.isPlainObject(s)&&e.isPlainObject(a)){if(!n(s,a))return!1}else if(!e.equals(s,a))return!1}return!0}var r=typeof t;if("object"===r){for(var s in t)if(t.hasOwnProperty(s)&&!this.matches(s,t[s]))return!1}else{if("function"===r)return t(this);var a=/^(empty|editable)$/.test(t)?this["is"+e.capitalize(t)]():"type"===t?e.hyphenate(this._class):this[t];if(/^(constructor|class)$/.test(t)){if(!(this instanceof i))return!1}else if(i instanceof RegExp){if(!i.test(a))return!1}else if("function"==typeof i){if(!i(a))return!1}else if(e.isPlainObject(i)){if(!n(i,a))return!1}else if(!e.equals(a,i))return!1}return!0},getItems:function(t){return m._getItems(this._children,t,this._matrix)},getItem:function(t){return m._getItems(this._children,t,this._matrix,null,!0)[0]||null},statics:{_getItems:function ot(t,i,n,r,s){if(!r&&"object"==typeof i){var a=i.overlapping,o=i.inside,h=a||o,u=h&&d.read([h]);r={items:[],inside:!!o,overlapping:!!a,rect:u,path:a&&new O.Rectangle({rectangle:u,insert:!1})},h&&(i=e.set({},i,{inside:!0,overlapping:!0}))}var l=r&&r.items,u=r&&r.rect;n=u&&(n||new f);for(var c=0,_=t&&t.length;_>c;c++){var g=t[c],v=n&&n.chain(g._matrix),p=!0;if(u){var h=g.getBounds(v);if(!u.intersects(h))continue;r.inside&&u.contains(h)||r.overlapping&&(h.contains(u)||r.path.intersects(g,v))||(p=!1)}if(p&&g.matches(i)&&(l.push(g),s))break;if(ot(g._children,i,v,r,s),s&&l.length>0)break}return l}}},{importJSON:function(t){var i=e.importJSON(t,this);return i!==this?this.addChild(i):i},addChild:function(e,i){return this.insertChild(t,e,i)},insertChild:function(t,e,i){var n=e?this.insertChildren(t,[e],i):null;return n&&n[0]},addChildren:function(t,e){return this.insertChildren(this._children.length,t,e)},insertChildren:function(t,i,n,r){var s=this._children;if(s&&i&&i.length>0){i=Array.prototype.slice.apply(i);for(var a=i.length-1;a>=0;a--){var o=i[a];if(!r||o instanceof r){var h=o._parent===this&&o._indexa;a++){var o=i[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name),l&&this._changed(5)}this._changed(11)}else i=null;return i},_insertSibling:function(t,e,i){return this._parent?this._parent.insertChild(t,e,i):null},insertAbove:function(t,e){return t._insertSibling(t._index+1,this,e)},insertBelow:function(t,e){return t._insertSibling(t._index,this,e)},sendToBack:function(){return(this._parent||this instanceof w&&this._project).insertChild(0,this)},bringToFront:function(){return(this._parent||this instanceof w&&this._project).addChild(this)},appendTop:"#addChild",appendBottom:function(t){return this.insertChild(0,t)},moveAbove:"#insertAbove",moveBelow:"#insertBelow",reduce:function(){if(this._children&&1===this._children.length){var t=this._children[0].reduce();return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent;if(t){var e=t._children,i=t._namedChildren,n=this._name,r=i[n],s=r?r.indexOf(this):-1;-1!==s&&(e[n]==this&&delete e[n],r.splice(s,1),r.length?e[n]=r[r.length-1]:delete i[n])}},_remove:function(t,i){var n=this._parent;if(n){if(this._name&&this._removeNamed(),null!=this._index&&e.splice(n._children,null,this._index,1),this._installEvents(!1),t){var r=this._project;r&&r._changes&&this._changed(5)}return i&&n._changed(11),this._parent=null,!0}return!1},remove:function(){return this._remove(!0,!0)},replaceWith:function(t){var e=t&&t.insertBelow(this);return e&&this.remove(),e},removeChildren:function(t,i){if(!this._children)return null;t=t||0,i=e.pick(i,this._children.length);for(var n=e.splice(this._children,null,t,i-t),r=n.length-1;r>=0;r--)n[r]._remove(!0,!1);return n.length>0&&this._changed(11),n},clear:"#removeChildren",reverseChildren:function(){if(this._children){this._children.reverse();for(var t=0,e=this._children.length;e>t;t++)this._children[t]._index=t;this._changed(11)}},isEmpty:function(){return!this._children||0===this._children.length},isEditable:function(){for(var t=this;t;){if(!t._visible||t._locked)return!1;t=t._parent}return!0},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()},_getOrder:function(t){function e(t){var e=[];do e.unshift(t);while(t=t._parent);return e}for(var i=e(this),n=e(t),r=0,s=Math.min(i.length,n.length);s>r;r++)if(i[r]!=n[r])return i[r]._index0},isInserted:function(){return this._parent?this._parent.isInserted():!1},isAbove:function(t){return-1===this._getOrder(t)},isBelow:function(t){return 1===this._getOrder(t)},isParent:function(t){return this._parent===t},isChild:function(t){return t&&t._parent===this},isDescendant:function(t){for(var e=this;e=e._parent;)if(e==t)return!0;return!1},isAncestor:function(t){return t?t.isDescendant(this):!1},isGroupedWith:function(t){for(var e=this._parent;e;){if(e._parent&&/^(Group|Layer|CompoundPath)$/.test(e._class)&&t.isDescendant(e))return!0;e=e._parent}return!1},translate:function(){var t=new f;return this.transform(t.translate.apply(t,arguments))},rotate:function(t){return this.transform((new f).rotate(t,h.read(arguments,1,{readNull:!0})||this.getPosition(!0)))}},e.each(["scale","shear","skew"],function(t){this[t]=function(){var e=h.read(arguments),i=h.read(arguments,0,{readNull:!0});return this.transform((new f)[t](e,i||this.getPosition(!0)))}},{}),{transform:function(t,e,i,n){t&&t.isIdentity()&&(t=null);var r=this._matrix,s=(e||this._applyMatrix)&&(!r.isIdentity()||t||e&&i&&this._children);if(!t&&!s)return this;if(t&&r.preConcatenate(t),s=s&&this._transformContent(r,i,n)){var a=this._pivot,o=this._style,h=o.getFillColor(!0),u=o.getStrokeColor(!0);a&&r._transformPoint(a,a,!0),h&&h.transform(r),u&&u.transform(r),r.reset(!0),n&&this._canApplyMatrix&&(this._applyMatrix=!0)}var l=this._bounds,c=this._position;this._changed(9);var d=l&&t&&t.decompose();if(d&&!d.shearing&&d.rotation%90===0){for(var _ in l){var f=l[_];(s||!f._internal)&&t._transformBounds(f,f)}var g=this._boundsGetter,f=l[g&&g.getBounds||g||"getBounds"];f&&(this._position=f.getCenter(!0)),this._bounds=l}else t&&c&&(this._position=t._transformPoint(c,c));return this},_transformContent:function(t,e,i){var n=this._children;if(n){for(var r=0,s=n.length;s>r;r++)n[r].transform(t,!0,e,i);return!0}},globalToLocal:function(){return this.getGlobalMatrix(!0)._inverseTransform(h.read(arguments))},localToGlobal:function(){return this.getGlobalMatrix(!0)._transformPoint(h.read(arguments))},parentToLocal:function(){return this._matrix._inverseTransform(h.read(arguments))},localToParent:function(){return this._matrix._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=d.read(arguments);var i=this.getBounds(),n=i.height/i.width,r=t.height/t.width,s=(e?n>r:r>n)?t.width/i.width:t.height/i.height,a=new d(new h,new l(i.width*s,i.height*s));a.setCenter(t.getCenter()),this.setBounds(a)},_setStyles:function(t){var e=this._style,i=e.getFillColor(),n=e.getStrokeColor(),r=e.getShadowColor();if(i&&(t.fillStyle=i.toCanvasStyle(t)),n){var s=e.getStrokeWidth();if(s>0){t.strokeStyle=n.toCanvasStyle(t),t.lineWidth=s;var a=e.getStrokeJoin(),o=e.getStrokeCap(),h=e.getMiterLimit();if(a&&(t.lineJoin=a),o&&(t.lineCap=o),h&&(t.miterLimit=h),paper.support.nativeDash){var u=e.getDashArray(),l=e.getDashOffset();u&&u.length&&("setLineDash"in t?(t.setLineDash(u),t.lineDashOffset=l):(t.mozDash=u,t.mozDashOffset=l))}}}if(r){var c=e.getShadowBlur();if(c>0){t.shadowColor=r.toCanvasStyle(t),t.shadowBlur=c;var d=this.getShadowOffset();t.shadowOffsetX=d.x,t.shadowOffsetY=d.y}}},draw:function(t,e,i){function n(t){return a?a.chain(t):t}var r=this._updateVersion=this._project._updateVersion;if(this._visible&&0!==this._opacity){var s=e.matrices,a=e.viewMatrix,o=this._matrix,h=s[s.length-1].chain(o);if(h.isInvertible()){s.push(h),e.updateMatrix&&(h._updateVersion=r,this._globalMatrix=h);var u,l,c,d=this._blendMode,_=this._opacity,f="normal"===d,g=$.nativeModes[d],v=f&&1===_||e.dontStart||e.clip||(g||f&&1>_)&&this._canComposite(),p=e.pixelRatio||1;if(!v){var m=this.getStrokeBounds(n(h));if(!m.width||!m.height)return;c=e.offset,l=e.offset=m.getTopLeft().floor(),u=t,t=Y.getContext(m.getSize().ceil().add(1).multiply(p)),1!==p&&t.scale(p,p)}t.save();var y=i?i.chain(o):!this.getStrokeScaling(!0)&&n(h),w=!v&&e.clipItem,x=!y||w;if(v?(t.globalAlpha=_,g&&(t.globalCompositeOperation=d)):x&&t.translate(-l.x,-l.y),x&&(v?o:n(h)).applyToContext(t),w&&e.clipItem.draw(t,e.extend({clip:!0})),y){t.setTransform(p,0,0,p,0,0);var b=e.offset;b&&t.translate(-b.x,-b.y)}this._draw(t,e,y),t.restore(),s.pop(),e.clip&&!e.dontFinish&&t.clip(),v||($.process(d,t,u,_,l.subtract(c).multiply(p)),Y.release(t),e.offset=c)}}},_isUpdated:function(t){var e=this._parent;if(e instanceof A)return e._isUpdated(t);var i=this._updateVersion===t;return!i&&e&&e._visible&&e._isUpdated(t)&&(this._updateVersion=t,i=!0),i},_drawSelection:function(t,e,i,n,r){if((this._drawSelected||this._boundsSelected)&&this._isUpdated(r)){var s=this.getSelectedColor(!0)||this.getLayer().getSelectedColor(!0),a=e.chain(this.getGlobalMatrix(!0));if(t.strokeStyle=t.fillStyle=s?s.toCanvasStyle(t):"#009dec",this._drawSelected&&this._drawSelected(t,a,n),this._boundsSelected){var o=i/2;coords=a._transformCorners(this.getInternalBounds()),t.beginPath();for(var h=0;8>h;h++)t[0===h?"moveTo":"lineTo"](coords[h],coords[++h]);t.closePath(),t.stroke();for(var h=0;8>h;h++)t.fillRect(coords[h]-o,coords[++h]-o,i,i)}}},_canComposite:function(){return!1}},e.each(["down","drag","up","move"],function(t){this["removeOn"+e.capitalize(t)]=function(){var e={};return e[t]=!0,this.removeOn(e)}},{removeOn:function(t){for(var e in t)if(t[e]){var i="mouse"+e,n=this._project,r=n._removeSets=n._removeSets||{};r[i]=r[i]||{},r[i][this._id]=this}return this}})),y=m.extend({_class:"Group",_selectChildren:!0,_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ht(e){ht.base.call(this,e),1026&e&&(this._clipItem=t)},_getClipItem:function(){var e=this._clipItem;if(e===t){e=null;for(var i=0,n=this._children.length;n>i;i++){var r=this._children[i];if(r._clipMask){e=r;break}}this._clipItem=e}return e},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var i=e.clip,n=!i&&this._getClipItem(),r=!0;if(e=e.extend({clipItem:n,clip:!1}),i?this._currentPath?(t.currentPath=this._currentPath,r=!1):(t.beginPath(),e.dontStart=e.dontFinish=!0):n&&n.draw(t,e.extend({clip:!0})),r)for(var s=0,a=this._children.length;a>s;s++){var o=this._children[s];o!==n&&o.draw(t,e)}i&&(this._currentPath=t.currentPath)}}),w=y.extend({_class:"Layer",initialize:function(i){var n=e.isPlainObject(i)?new e(i):{children:Array.isArray(i)?i:arguments},r=n.insert;n.insert=!1,y.call(this,n),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function ut(t,i){if(this._parent)return ut.base.call(this,t,i);if(null!=this._index){var n=this._project;return n._activeLayer===this&&(n._activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(n.layers,null,this._index,1),this._installEvents(!1),t&&n._changes&&this._changed(5),i&&(n._needsUpdate=!0),!0}return!1},getNextSibling:function lt(){return this._parent?lt.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function ct(){return this._parent?ct.base.call(this):this._project.layers[this._index-1]||null},isInserted:function dt(){return this._parent?dt.base.call(this):null!=this._index},activate:function(){this._project._activeLayer=this},_insertSibling:function _t(t,e,i){return this._parent?_t.base.call(this,t,e,i):this._project.insertChild(t,e,i)}}),x=m.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{type:null,size:null,radius:null -},initialize:function(t){this._initialize(t)},_equals:function(t){return this._type===t._type&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new x(m.NO_INSERT);return e.setType(this._type),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getType:function(){return this._type},setType:function(t){this._type=t},getShape:"#getType",setShape:"#setType",getSize:function(){var t=this._size;return new c(t.width,t.height,this,"setSize")},setSize:function(){var t=l.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._type,i=t.width,n=t.height;if("rectangle"===e){var r=l.min(this._radius,t.divide(2));this._radius.set(r.width,r.height)}else"circle"===e?(i=n=(i+n)/2,this._radius=i/2):"ellipse"===e&&this._radius.set(i/2,n/2);this._size.set(i,n),this._changed(9)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._type?t:new c(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._type;if("circle"===e){if(t===this._radius)return;var i=2*t;this._radius=t,this._size.set(i,i)}else if(t=l.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var i=l.max(this._size,t.multiply(2));this._size.set(i.width,i.height)}else"ellipse"===e&&this._size.set(2*t.width,2*t.height)}else this._radius=t.clone();this._changed(9)},isEmpty:function(){return!1},toPath:function(t){var i=this._clone(new(O[e.capitalize(this._type)])({center:new h,size:this._size,radius:this._radius,insert:!1}),t);return paper.settings.applyMatrix&&i.setApplyMatrix(!0),i},_draw:function(t,e,i){var n=this._style,r=n.hasFill(),s=n.hasStroke(),a=e.dontFinish||e.clip,o=!i;if(r||s||a){var h=this._type,u=this._radius,l="circle"===h;if(e.dontStart||t.beginPath(),o&&l)t.arc(0,0,u,0,2*Math.PI,!0);else{var c=l?u:u.width,d=l?u:u.height,_=this._size,f=_.width,g=_.height;if(o&&"rectangle"===h&&0===c&&0===d)t.rect(-f/2,-g/2,f,g);else{var v=f/2,p=g/2,m=.44771525016920644,y=c*m,w=d*m,x=[-v,-p+d,-v,-p+w,-v+y,-p,-v+c,-p,v-c,-p,v-y,-p,v,-p+w,v,-p+d,v,p-d,v,p-w,v-y,p,v-c,p,-v+c,p,-v+y,p,-v,p-w,-v,p-d];i&&i.transform(x,x,32),t.moveTo(x[0],x[1]),t.bezierCurveTo(x[2],x[3],x[4],x[5],x[6],x[7]),v!==c&&t.lineTo(x[8],x[9]),t.bezierCurveTo(x[10],x[11],x[12],x[13],x[14],x[15]),p!==d&&t.lineTo(x[16],x[17]),t.bezierCurveTo(x[18],x[19],x[20],x[21],x[22],x[23]),v!==c&&t.lineTo(x[24],x[25]),t.bezierCurveTo(x[26],x[27],x[28],x[29],x[30],x[31])}}t.closePath()}a||!r&&!s||(this._setStyles(t),r&&(t.fill(n.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var i=new d(this._size).setCenter(0,0);return"getBounds"!==t&&this.hasStroke()&&(i=i.expand(this.getStrokeWidth())),e?e._transformBounds(i):i}},new function(){function t(t,e,i){var n=t._radius;if(!n.isZero())for(var r=t._size.divide(2),s=0;4>s;s++){var a=new h(1&s?1:-1,s>1?1:-1),o=a.multiply(r),u=o.subtract(a.multiply(n)),l=new d(o,u);if((i?l.expand(i):l).contains(e))return u}}function e(t,e){var i=t.getAngleInRadians(),n=2*e.width,r=2*e.height,s=n*Math.sin(i),a=r*Math.cos(i);return n*r/(2*Math.sqrt(s*s+a*a))}return{_contains:function i(e){if("rectangle"===this._type){var n=t(this,e);return n?e.subtract(n).divide(this._radius).getLength()<=1:i.base.call(this,e)}return e.divide(this.size).getLength()<=.5},_hitTestSelf:function n(i,r){var s=!1;if(this.hasStroke()){var a=this._type,o=this._radius,h=this.getStrokeWidth()+2*r.tolerance;if("rectangle"===a){var u=t(this,i,h);if(u){var l=i.subtract(u);s=2*Math.abs(l.getLength()-e(l,o))<=h}else{var c=new d(this._size).setCenter(0,0),_=c.expand(h),f=c.expand(-h);s=_._containsPoint(i)&&!f._containsPoint(i)}}else"ellipse"===a&&(o=e(i,o)),s=2*Math.abs(i.getLength()-o)<=h}return s?new S("stroke",this):n.base.apply(this,arguments)}}},{statics:new function(){function t(t,i,n,r,s){var a=new x(e.getNamed(s));return a._type=t,a._size=n,a._radius=r,a.translate(i)}return{Circle:function(){var i=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"radius");return t("circle",i,new l(2*n),n,arguments)},Rectangle:function(){var e=d.readNamed(arguments,"rectangle"),i=l.min(l.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),i,arguments)},Ellipse:function(){var e=x._readEllipse(arguments),i=e.radius;return t("ellipse",e.center,i.multiply(2),i,arguments)},_readEllipse:function(t){var i,n;if(e.hasNamed(t,"radius"))i=h.readNamed(t,"center"),n=l.readNamed(t,"radius");else{var r=d.readNamed(t,"rectangle");i=r.getCenter(!0),n=r.getSize(!0).divide(2)}return{center:i,radius:n}}}}}),b=m.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{crossOrigin:null,source:null},initialize:function(e,i){this._initialize(e,i!==t&&h.read(arguments,1))||("string"==typeof e?this.setSource(e):this.setImage(e)),this._size||(this._size=new l,this._loaded=!1)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new b(m.NO_INSERT),i=this._image,n=this._canvas;if(i)e.setImage(i);else if(n){var r=Y.getCanvas(this._size);r.getContext("2d").drawImage(n,0,0),e.setImage(r)}return e._crossOrigin=this._crossOrigin,this._clone(e,t)},getSize:function(){var t=this._size;return new c(t?t.width:0,t?t.height:0,this,"setSize")},setSize:function(){var t=l.read(arguments);if(!t.equals(this._size))if(t.width>0&&t.height>0){var e=this.getElement();this.setImage(Y.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}else this._canvas&&Y.release(this._canvas),this._size=t.clone()},getWidth:function(){return this._size?this._size.width:0},setWidth:function(t){this.setSize(t,this.getHeight())},getHeight:function(){return this._size?this._size.height:0},setHeight:function(t){this.setSize(this.getWidth(),t)},isEmpty:function(){var t=this._size;return!t||0===t.width&&0===t.height},getResolution:function(){var t=this._matrix,e=new h(0,0).transform(t),i=new h(1,0).transform(t).subtract(e),n=new h(0,1).transform(t).subtract(e);return new l(72/i.getLength(),72/n.getLength())},getPpi:"#getResolution",getImage:function(){return this._image},setImage:function(t){this._canvas&&Y.release(this._canvas),t&&t.getContext?(this._image=null,this._canvas=t,this._loaded=!0):(this._image=t,this._canvas=null,this._loaded=t&&t.complete),this._size=new l(t?t.naturalWidth||t.width:0,t?t.naturalHeight||t.height:0),this._context=null,this._changed(521)},getCanvas:function(){if(!this._canvas){var t=Y.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){Y.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(t){return this._context||(this._context=this.getCanvas().getContext("2d")),t&&(this._image=null,this._changed(513)),this._context},setContext:function(t){this._context=t},getSource:function(){return this._image&&this._image.src||this.toDataURL()},setSource:function(t){function e(){var t=n.getView();t&&(paper=t._scope,n.setImage(i),n.emit("load"),t.update())}var i,n=this,r=this._crossOrigin;i=document.getElementById(t)||new Image,r&&(i.crossOrigin=r),i.naturalWidth&&i.naturalHeight?setTimeout(e,0):(q.add(i,{load:e}),i.src||(i.src=t)),this.setImage(i)},getCrossOrigin:function(){return this._image&&this._image.crossOrigin||this._crossOrigin||""},setCrossOrigin:function(t){this._crossOrigin=t,this._image&&(this._image.crossOrigin=t)},getElement:function(){return this._canvas||this._loaded&&this._image}},{beans:!1,getSubCanvas:function(){var t=d.read(arguments),e=Y.getContext(t.getSize());return e.drawImage(this.getCanvas(),t.x,t.y,t.width,t.height,0,0,t.width,t.height),e.canvas},getSubRaster:function(){var t=d.read(arguments),e=new b(m.NO_INSERT);return e.setImage(this.getSubCanvas(t)),e.translate(t.getCenter().subtract(this.getSize().divide(2))),e._matrix.preConcatenate(this._matrix),e.insertAbove(this),e},toDataURL:function(){var t=this._image&&this._image.src;if(/^data:/.test(t))return t;var e=this.getCanvas();return e?e.toDataURL():null},drawImage:function(t){var e=h.read(arguments,1);this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var i,n;t?t instanceof k?(n=t,i=t.getBounds()):t.width?i=new d(t):t.x&&(i=new d(t.x-.5,t.y-.5,1,1)):i=this.getBounds();var r=32,s=Math.min(i.width,r),a=Math.min(i.height,r),o=b._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=b._sampleContext=Y.getContext(new l(r)),o.save();var h=(new f).scale(s/i.width,a/i.height).translate(-i.x,-i.y);h.applyToContext(o),n&&n.draw(o,new e({clip:!0,matrices:[h]})),this._matrix.applyToContext(o);var u=this.getElement(),c=this._size;u&&o.drawImage(u,-c.width/2,-c.height/2),o.restore();for(var _=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,g=[0,0,0],v=0,p=0,m=_.length;m>p;p+=4){var y=_[p+3];v+=y,y/=255,g[0]+=_[p]*y,g[1]+=_[p+1]*y,g[2]+=_[p+2]*y}for(var p=0;3>p;p++)g[p]/=v;return v?B.read(g):null},getPixel:function(){var t=h.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new B("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=B.read(arguments),i=e._convert("rgb"),n=e._alpha,r=this.getContext(!0),s=r.createImageData(1,1),a=s.data;a[0]=255*i[0],a[1]=255*i[1],a[2]=255*i[2],a[3]=null!=n?255*n:255,r.putImageData(s,t.x,t.y)},createImageData:function(){var t=l.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(){var t=d.read(arguments);return t.isEmpty()&&(t=new d(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t){var e=h.read(arguments,1);this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var i=new d(this._size).setCenter(0,0);return e?e._transformBounds(i):i},_hitTestSelf:function(t){if(this._contains(t)){var e=this;return new S("pixel",e,{offset:t.add(e._size.divide(2)).round(),color:{get:function(){return e.getPixel(this.offset)}}})}},_draw:function(t){var e=this.getElement();e&&(t.globalAlpha=this._opacity,t.drawImage(e,-this._size.width/2,-this._size.height/2))},_canComposite:function(){return!0}}),C=m.extend({_class:"PlacedSymbol",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:{getBounds:"getStrokeBounds"},_boundsSelected:!0,_serializeFields:{symbol:null},initialize:function(e,i){this._initialize(e,i!==t&&h.read(arguments,1))||this.setSymbol(e instanceof p?e:new p(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(9)},clone:function(t){var e=new C(m.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,i){var n=this.symbol._definition;return n._getCachedBounds(t,e&&e.chain(n._matrix),i)},_hitTestSelf:function(t,e){var i=this._symbol._definition._hitTest(t,e);return i&&(i.item=this),i},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),S=e.extend({_class:"HitResult",initialize:function(t,e,i){this.type=t,this.item=e,i&&(i.enumerable=!0,this.inject(i))},statics:{getOptions:function(t){return new e({type:null,tolerance:paper.settings.hitTolerance,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1},t)}}}),P=e.extend({_class:"Segment",beans:!0,initialize:function(e,i,n,r,s,a){var o,h,u,l=arguments.length;0===l||(1===l?e.point?(o=e.point,h=e.handleIn,u=e.handleOut):o=e:2===l&&"number"==typeof e?o=arguments:3>=l?(o=e,h=i,u=n):(o=e!==t?[e,i]:null,h=n!==t?[n,r]:null,u=s!==t?[s,a]:null)),new M(o,this,"_point"),new M(h,this,"_handleIn"),new M(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.isStraight()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){var e=this._path;if(e){var i,n=e._curves,r=this._index;n&&(t&&t!==this._point&&t!==this._handleIn||!(i=r>0?n[r-1]:e._closed?n[n.length-1]:null)||i._changed(),t&&t!==this._point&&t!==this._handleOut||!(i=n[r])||i._changed()),e._changed(25)}},getPoint:function(){return this._point},setPoint:function(){var t=h.read(arguments);this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(){var t=h.read(arguments);this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(){var t=h.read(arguments);this._handleOut.set(t.x,t.y)},hasHandles:function(){return!this.isStraight()},isStraight:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},isLinear:function(){return P.isLinear(this,this.getNext())},isCollinear:function(t){return P.isCollinear(this,this.getNext(),t,t.getNext())},isColinear:"#isCollinear",isOrthogonal:function(){return P.isOrthogonal(this.getPrevious(),this,this.getNext())},isOrthogonalArc:function(){return P.isOrthogonalArc(this,this.getNext())},isArc:"#isOrthogonalArc",_selectionState:0,isSelected:function(t){var e=this._selectionState;return t?t===this._point?!!(4&e):t===this._handleIn?!!(1&e):t===this._handleOut?!!(2&e):!1:!!(7&e)},setSelected:function(t,e){var i=this._path,t=!!t,n=this._selectionState,r=n,s=e?e===this._point?4:e===this._handleIn?1:e===this._handleOut?2:0:7;t?n|=s:n&=~s,this._selectionState=n,i&&n!==r&&(i._updateSelection(this,r,n),i._changed(129))},getIndex:function(){return this._index!==t?this._index:null},getPath:function(){return this._path||null},getCurve:function(){var t=this._path,e=this._index;return t?(e>0&&!t._closed&&e===t._segments.length-1&&e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new I(t,this===t._segment1?0:1):null},getNext:function(){var t=this._path&&this._path._segments;return t&&(t[this._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._segments;return t&&(t[this._index-1]||this._path._closed&&t[t.length-1])||null},reverse:function(){return new P(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new P(this._point,this._handleIn,this._handleOut)},equals:function(t){return t===this||t&&this._class===t._class&&this._point.equals(t._point)&&this._handleIn.equals(t._handleIn)&&this._handleOut.equals(t._handleOut)||!1},toString:function(){var t=["point: "+this._point];return this._handleIn.isZero()||t.push("handleIn: "+this._handleIn),this._handleOut.isZero()||t.push("handleOut: "+this._handleOut),"{ "+t.join(", ")+" }"},transform:function(t){this._transformCoordinates(t,Array(6),!0),this._changed()},_transformCoordinates:function(t,e,i){var n=this._point,r=i&&this._handleIn.isZero()?null:this._handleIn,s=i&&this._handleOut.isZero()?null:this._handleOut,a=n._x,o=n._y,h=2;return e[0]=a,e[1]=o,r&&(e[h++]=r._x+a,e[h++]=r._y+o),s&&(e[h++]=s._x+a,e[h++]=s._y+o),t&&(t._transformCoordinates(e,e,h/2),a=e[0],o=e[1],i?(n._x=a,n._y=o,h=2,r&&(r._x=e[h++]-a,r._y=e[h++]-o),s&&(s._x=e[h++]-a,s._y=e[h++]-o)):(r||(e[h++]=a,e[h++]=o),s||(e[h++]=a,e[h++]=o))),e},statics:{isLinear:function(t,e){var i=e._point.subtract(t._point);return i.isCollinear(t._handleOut)&&i.isCollinear(e._handleIn)},isCollinear:function(t,e,i,n){return t._handleOut.isZero()&&e._handleIn.isZero()&&i._handleOut.isZero()&&n._handleIn.isZero()&&e._point.subtract(t._point).isCollinear(n._point.subtract(i._point))},isOrthogonal:function(t,e,i){return t._handleOut.isZero()&&e._handleIn.isZero()&&e._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(t._point).isOrthogonal(i._point.subtract(e._point))},isOrthogonalArc:function(t,e){var i=t._handleOut,n=e._handleIn,r=.5522847498307936;if(i.isOrthogonal(n)){var s=t._point,o=e._point,h=new g(s,i,!0).intersect(new g(o,n,!0),!0);return h&&a.isZero(i.getLength()/h.subtract(s).getLength()-r)&&a.isZero(n.getLength()/h.subtract(o).getLength()-r)}return!1}}}),M=h.extend({initialize:function(e,i,n){var r,s,a;if(e)if((r=e[0])!==t)s=e[1];else{var o=e;(r=o.x)===t&&(o=h.read(arguments),r=o.x),s=o.y,a=o.selected}else r=s=0;this._x=r,this._y=s,this._owner=i,i[n]=this,a&&this.setSelected(!0)},set:function(t,e){return this._x=t,this._y=e,this._owner._changed(this),this},_serialize:function(t){var e=t.formatter,i=e.number(this._x),n=e.number(this._y);return this.isSelected()?{x:i,y:n,selected:!0}:[i,n]},getX:function(){return this._x},setX:function(t){this._x=t,this._owner._changed(this)},getY:function(){return this._y},setY:function(t){this._y=t,this._owner._changed(this)},isZero:function(){return a.isZero(this._x)&&a.isZero(this._y)},setSelected:function(t){this._owner.setSelected(t,this)},isSelected:function(){return this._owner.isSelected(this)}}),z=e.extend({_class:"Curve",initialize:function(t,e,i,n,r,s,a,o){var h=arguments.length;if(3===h)this._path=t,this._segment1=e,this._segment2=i;else if(0===h)this._segment1=new P,this._segment2=new P;else if(1===h)this._segment1=new P(t.segment1),this._segment2=new P(t.segment2);else if(2===h)this._segment1=new P(t),this._segment2=new P(e);else{var u,l,c,d;4===h?(u=t,l=e,c=i,d=n):8===h&&(u=[t,e],d=[a,o],l=[i-t,n-e],c=[r-a,s-o]),this._segment1=new P(u,null,l),this._segment2=new P(d,c,null)}},_changed:function(){this._length=this._bounds=t},getPoint1:function(){return this._segment1._point},setPoint1:function(){var t=h.read(arguments);this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(){var t=h.read(arguments);this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(){var t=h.read(arguments);this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(){var t=h.read(arguments);this._segment2._handleIn.set(t.x,t.y)},getSegment1:function(){return this._segment1},getSegment2:function(){return this._segment2},getPath:function(){return this._path},getIndex:function(){return this._segment1._index},getNext:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index-1]||this._path._closed&&t[t.length-1])||null},isSelected:function(){return this.getPoint1().isSelected()&&this.getHandle2().isSelected()&&this.getHandle2().isSelected()&&this.getPoint2().isSelected()},setSelected:function(t){this.getPoint1().setSelected(t),this.getHandle1().setSelected(t),this.getHandle2().setSelected(t),this.getPoint2().setSelected(t)},getValues:function(t){return z.getValues(this._segment1,this._segment2,t)},getPoints:function(){for(var t=this.getValues(),e=[],i=0;8>i;i+=2)e.push(new h(t[i],t[i+1]));return e},getLength:function(){return null==this._length&&(this._length=this.isLinear()?this._segment2._point.getDistance(this._segment1._point):z.getLength(this.getValues(),0,1)),this._length},getArea:function(){return z.getArea(this.getValues())},getPart:function(t,e){return new z(z.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return z.getLength(this.getValues(),t,e)},hasHandles:function(){return!this._segment1._handleOut.isZero()||!this._segment2._handleIn.isZero()},isLinear:function(){return P.isLinear(this._segment1,this._segment2)},isCollinear:function(t){return Ssegment.isCollinear(this._segment1,this._segment2,t._segment1,t._segment2)},isOrthogonalArc:function(){return P.isOrthogonalArc(this._segment1,this._segment2)},getIntersections:function(t){return z.filterIntersections(z.getIntersections(this.getValues(),t.getValues(),this,t,[]))},_getParameter:function(e,i){return i?e:e&&e.curve===this?e.parameter:e===t&&i===t?.5:this.getParameterAt(e,0)},divide:function(t,e,i){var n=this._getParameter(t,e),r=1e-6,s=null;if(n>r&&1-r>n){var a=z.subdivide(this.getValues(),n),o=i?!1:this.isLinear(),u=a[0],l=a[1];o||(this._segment1._handleOut.set(u[2]-u[0],u[3]-u[1]),this._segment2._handleIn.set(l[4]-l[6],l[5]-l[7]));var c=u[6],d=u[7],_=new P(new h(c,d),!o&&new h(u[4]-c,u[5]-d),!o&&new h(l[2]-c,l[3]-d));if(this._path)this._segment1._index>0&&0===this._segment2._index?this._path.add(_):this._path.insert(this._segment2._index,_),s=this;else{var f=this._segment2;this._segment2=_,s=new z(_,f)}}return s},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},reverse:function(){return new z(this._segment2.reverse(),this._segment1.reverse())},remove:function(){var t=!1;if(this._path){var e=this._segment2,i=e._handleOut;t=e.remove(),t&&this._segment1._handleOut.set(i.x,i.y)}return t},clone:function(){return new z(this._segment1,this._segment2)},toString:function(){var t=["point1: "+this._segment1._point];return this._segment1._handleOut.isZero()||t.push("handle1: "+this._segment1._handleOut),this._segment2._handleIn.isZero()||t.push("handle2: "+this._segment2._handleIn),t.push("point2: "+this._segment2._point),"{ "+t.join(", ")+" }"},statics:{getValues:function(t,e,i){var n=t._point,r=t._handleOut,s=e._handleIn,a=e._point,o=[n._x,n._y,n._x+r._x,n._y+r._y,a._x+s._x,a._y+s._y,a._x,a._y];return i&&i._transformCoordinates(o,o,4),o},subdivide:function(e,i){var n=e[0],r=e[1],s=e[2],a=e[3],o=e[4],h=e[5],u=e[6],l=e[7];i===t&&(i=.5);var c=1-i,d=c*n+i*s,_=c*r+i*a,f=c*s+i*o,g=c*a+i*h,v=c*o+i*u,p=c*h+i*l,m=c*d+i*f,y=c*_+i*g,w=c*f+i*v,x=c*g+i*p,b=c*m+i*w,C=c*y+i*x;return[[n,r,d,_,m,y,b,C],[b,C,w,x,v,p,u,l]]},solveCubic:function(t,e,i,n,r,s){var o=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-o),d=3*(u-h)-c,_=l-o-c-d;return a.solveCubic(_,d,c,o-i,n,r,s)},getParameterOf:function(t,e,i){var n=1e-6;if(Math.abs(t[0]-e)l;)if(-1===h||(r=a[l++])>0&&1>r){for(var c=0;-1===u||u>c;)if((-1===u||(s=o[c++])>0&&1>s)&&(-1===h?r=s:-1===u&&(s=r),Math.abs(r-s)0&&(t=z.subdivide(t,e)[1]),1>i&&(t=z.subdivide(t,(i-e)/(1-e))[0]),t},hasHandles:function(t){var e=a.isZero;return!(e(t[0]-t[2])&&e(t[1]-t[3])&&e(t[4]-t[6])&&e(t[5]-t[7]))},isLinear:function(t){var e=t[0],i=t[1],n=t[6],r=t[7],s=new h(n-e,r-i);return s.isCollinear(new h(t[2]-e,t[3]-i))&&s.isCollinear(new h(t[4]-n,t[5]-r))},isFlatEnough:function(t,e){var i=t[0],n=t[1],r=t[2],s=t[3],a=t[4],o=t[5],h=t[6],u=t[7],l=3*r-2*i-h,c=3*s-2*n-u,d=3*a-2*h-i,_=3*o-2*u-n;return Math.max(l*l,d*d)+Math.max(c*c,_*_)<10*e*e},getArea:function(t){var e=t[0],i=t[1],n=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7];return(3*r*e-1.5*r*s-1.5*r*o-3*i*n-1.5*i*s-.5*i*o+1.5*a*e+1.5*a*n-3*a*o+.5*h*e+1.5*h*n+3*h*s)/10},getEdgeSum:function(t){return(t[0]-t[2])*(t[3]+t[1])+(t[2]-t[4])*(t[5]+t[3])+(t[4]-t[6])*(t[7]+t[5])},getBounds:function(t){for(var e=t.slice(0,2),i=e.slice(),n=[0,0],r=0;2>r;r++)z._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,i,n);return new d(e[0],e[1],i[0]-e[0],i[1]-e[1])},_addBounds:function(t,e,i,n,r,s,o,h,u){function l(t,e){var i=t-e,n=t+e;ih[r]&&(h[r]=n)}var c=3*(e-i)-t+n,d=2*(t+i)-4*e,_=e-t,f=a.solveQuadratic(c,d,_,u),g=1e-6,v=1-g;l(n,0);for(var p=0;f>p;p++){var m=u[p],y=1-m;m>g&&v>m&&l(y*y*y*t+3*y*y*m*e+3*y*m*m*i+m*m*m*n,s)}}}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){this._bounds||(this._bounds={});var e=this._bounds[t];return e||(e=this._bounds[t]=O[t]([this._segment1,this._segment2],!1,this._path.getStyle())),e.clone()}},{}),{beans:!1,getParameterAt:function(t,e){return z.getParameterAt(this.getValues(),t,e)},getParameterOf:function(){var t=h.read(arguments);return z.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){var i=e?t:this.getParameterAt(t);return null!=i&&i>=0&&1>=i?new I(this,i):null},getLocationOf:function(){return this.getLocationAt(this.getParameterOf(h.read(arguments)),!0)},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getNearestLocation:function(){function t(t){if(t>=0&&1>=t){var n=e.getDistance(z.getPoint(i,t),!0);if(r>n)return r=n,s=t,!0}}for(var e=h.read(arguments),i=this.getValues(),n=100,r=1/0,s=0,a=0;n>=a;a++)t(a/n);for(var o=1/(2*n);o>1e-6;)t(s-o)||t(s+o)||(o/=2);var u=z.getPoint(i,s);return new I(this,s,u,null,null,null,e.getDistance(u))},getNearestPoint:function(){return this.getNearestLocation.apply(this,arguments).getPoint()}},new function(){var t=["getPoint","getTangent","getNormal","getWeightedTangent","getWeightedNormal","getCurvature"];return e.each(t,function(t){this[t+"At"]=function(e,i){var n=this.getValues();return z[t](n,i?e:z.getParameterAt(n,e,0))}},{statics:{evaluateMethods:t}})},new function(){function e(t){var e=t[0],i=t[1],n=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7],u=9*(n-s)+3*(o-e),l=6*(e+s)-12*n,c=3*(n-e),d=9*(r-a)+3*(h-i),_=6*(i+a)-12*r,f=3*(r-i);return function(t){var e=(u*t+l)*t+c,i=(d*t+_)*t+f;return Math.sqrt(e*e+i*i)}}function i(t,e){return Math.max(2,Math.min(16,Math.ceil(32*Math.abs(e-t))))}function n(t,e,i,n){if(null==e||0>e||e>1)return null;var r,s,a=t[0],o=t[1],u=t[2],l=t[3],c=t[4],d=t[5],_=t[6],f=t[7],g=1e-6;if(0===i&&(g>e||e>1-g)){var v=g>e;r=v?a:_,s=v?o:f}else{var p=3*(u-a),m=3*(c-u)-p,y=_-a-p-m,w=3*(l-o),x=3*(d-l)-w,b=f-o-w-x;if(0===i)r=((y*e+m)*e+p)*e+a,s=((b*e+x)*e+w)*e+o;else{if(g>e?(r=p,s=w):e>1-g?(r=3*(_-c),s=3*(f-d)):(r=(3*y*e+2*m)*e+p,s=(3*b*e+2*x)*e+w),n){0===r&&0===s&&(g>e||e>1-g)&&(r=c-u,s=d-l);var C=Math.sqrt(r*r+s*s);r/=C,s/=C}if(3===i){var S=6*y*e+2*m,P=6*b*e+2*x,M=Math.pow(r*r+s*s,1.5);r=0!==M?(r*P-s*S)/M:0,s=0}}}return 2===i?new h(s,-r):new h(r,s)}return{statics:!0,getLength:function(n,r,s){r===t&&(r=0),s===t&&(s=1);var o=a.isZero;if(0===r&&1===s&&o(n[0]-n[2])&&o(n[1]-n[3])&&o(n[6]-n[4])&&o(n[7]-n[5])){var h=n[6]-n[0],u=n[7]-n[1];return Math.sqrt(h*h+u*u)}var l=e(n);return a.integrate(l,r,s,i(r,s))},getParameterAt:function(n,r,s){function o(t){return v+=a.integrate(_,s,t,i(s,t)),s=t,v-r}if(s===t&&(s=0>r?1:0),0===r)return s;var h=1e-6,u=Math.abs,l=r>0,c=l?s:0,d=l?1:s,_=e(n),f=a.integrate(_,c,d,i(c,d));if(u(r-f)f)return null;var g=r/f,v=0;return a.findRoot(o,_,s+g,c,d,16,h)},getPoint:function(t,e){return n(t,e,0,!1)},getTangent:function(t,e){return n(t,e,1,!0)},getWeightedTangent:function(t,e){return n(t,e,1,!1)},getNormal:function(t,e){return n(t,e,2,!0)},getWeightedNormal:function(t,e){return n(t,e,2,!1)},getCurvature:function(t,e){return n(t,e,3,!1).x}}},new function(){function t(t,e,i,n,r,s,a,o){var h=new I(i,n,r,s,a,o);(!e||e(h))&&t.push(h)}function e(r,s,a,o,h,u,l,c,d,_,f,v,p){if(!(p>32)){var m,y,w,x=s[0],b=s[1],C=s[6],S=s[7],P=1e-6,M=g.getSignedDistance,I=M(x,b,C,S,s[2],s[3])||0,k=M(x,b,C,S,s[4],s[5])||0,O=I*k>0?.75:4/9,A=O*Math.min(0,I,k),T=O*Math.max(0,I,k),L=M(x,b,C,S,r[0],r[1]),N=M(x,b,C,S,r[2],r[3]),j=M(x,b,C,S,r[4],r[5]),B=M(x,b,C,S,r[6],r[7]);if(x===C&&P>_-d&&p>3)y=m=(c+l)/2,w=0;else{var D,E,R=i(L,N,j,B),F=R[0],q=R[1];if(D=n(F,q,A,T),F.reverse(),q.reverse(),E=n(F,q,A,T),null==D||null==E)return;r=z.getPart(r,D,E),w=E-D,m=c*D+l*(1-D),y=c*E+l*(1-E)}if(f>.5&&w>.5)if(y-m>_-d){var V=z.subdivide(r,.5),Z=m+(y-m)/2;e(s,V[0],o,a,h,u,d,_,m,Z,w,!v,++p),e(s,V[1],o,a,h,u,d,_,Z,y,w,!v,p)}else{var V=z.subdivide(s,.5),Z=d+(_-d)/2;e(V[0],r,o,a,h,u,d,Z,m,y,w,!v,++p),e(V[1],r,o,a,h,u,Z,_,m,y,w,!v,p)}else if(Math.max(_-d,y-m)0&&e(s,r,o,a,h,u,d,_,m,y,w,!v,++p)}}function i(t,e,i,n){var r,s=[0,t],a=[1/3,e],o=[2/3,i],h=[1,n],u=g.getSignedDistance,l=u(0,t,1,n,1/3,e),c=u(0,t,1,n,2/3,i),d=!1;if(0>l*c)r=[[s,a,h],[s,o,h]],d=0>l;else{var _,f=0,v=0===l||0===c;Math.abs(l)>Math.abs(c)?(_=a,f=(n-i-(n-t)/3)*(2*(n-i)-n+e)/3):(_=o,f=(e-t+(t-n)/3)*(-2*(t-e)+t-i)/3),r=0>f||v?[[s,_,h],[s,h]]:[[s,a,o,h],[s,h]],d=l?0>l:0>c}return d?r.reverse():r}function n(t,e,i,n){return t[0][1]n?r(e,!1,n):t[0][0]}function r(t,e,i){for(var n=t[0][0],r=t[0][1],s=1,a=t.length;a>s;s++){var o=t[s][0],h=t[s][1];if(e?h>=i:i>=h)return n+(i-r)*(o-n)/(h-r);n=o,r=h}return null}function s(e,i,n,r,s,a){for(var o=z.isLinear(e),h=o?i:e,u=o?e:i,l=u[0],c=u[1],d=u[6],_=u[7],f=d-l,g=_-c,v=Math.atan2(-g,f),p=Math.sin(v),m=Math.cos(v),y=f*m-g*p,w=[0,0,0,0,y,0,y,0],x=[],b=0;8>b;b+=2){var C=h[b]-l,S=h[b+1]-c;x.push(C*m-S*p,S*m+C*p)}for(var P=[],M=z.solveCubic(x,1,0,P,0,1),b=0;M>b;b++){var I=P[b],C=z.getPoint(x,I).x;if(C>=0&&y>=C){var k=z.getParameterOf(w,C,0),O=o?k:I,A=o?I:k;t(s,a,n,O,z.getPoint(e,O),r,A,z.getPoint(i,A))}}}function a(e,i,n,r,s,a){var o=g.intersect(e[0],e[1],e[6],e[7],i[0],i[1],i[6],i[7]);if(o){var h=o.x,u=o.y;t(s,a,n,z.getParameterOf(e,h,u),o,r,z.getParameterOf(i,h,u),o)}}return{statics:{getIntersections:function(i,n,r,o,h,u){var l=z.isLinear(i),c=z.isLinear(n),d=r.getPoint1(),_=r.getPoint2(),f=o.getPoint1(),g=o.getPoint2(),v=1e-6;return d.isClose(f,v)&&t(h,u,r,0,d,o,0,d),d.isClose(g,v)&&t(h,u,r,0,d,o,1,d),(l&&c?a:l||c?s:e)(i,n,r,o,h,u,0,1,0,1,0,!1,0),_.isClose(f,v)&&t(h,u,r,1,_,o,0,_),_.isClose(g,v)&&t(h,u,r,1,_,o,1,_),h},filterIntersections:function(t,e){function i(t,e){var i=t.getPath(),n=e.getPath();return i===n?t.getIndex()+t.getParameter()-(e.getIndex()+e.getParameter()):i._id-n._id}for(var n=t.length-1,r=1-1e-6,s=n;s>=0;s--){var a=t[s],o=a._curve.getNext(),h=a._curve2.getNext();o&&a._parameter>=r&&(a._parameter=0,a._curve=o),h&&a._parameter2>=r&&(a._parameter2=0,a._curve2=h)}if(n>0){t.sort(i);for(var s=n;s>0;s--)t[s].equals(t[s-1])&&(t.splice(s,1),n--)}if(e){for(var s=n;s>=0;s--)t.push(t[s].getIntersection());t.sort(i)}return t}}}}),I=e.extend({_class:"CurveLocation",beans:!0,initialize:function ft(t,e,i,n,r,s,a){this._id=o.get(ft);var h=t._path;this._version=h?h._version:0,this._curve=t,this._parameter=e,this._point=i||t.getPointAt(e,!0),this._curve2=n,this._parameter2=r,this._point2=s,this._distance=a,this._segment1=t._segment1,this._segment2=t._segment2},getSegment:function(t){if(!this._segment){var e=this.getCurve(),i=this.getParameter();if(1===i)this._segment=e._segment2;else if(0===i||t)this._segment=e._segment1;else{if(null==i)return null;this._segment=e.getPartLength(0,i)f;f++)c[f]=a[f].getValues(h);for(var f=0;u>f;f++){var v=s[f],p=e?v.getValues(o):c[f];if(!e){var m=v.getSegment1(),y=v.getSegment2(),w=m._handleOut,x=y._handleIn;if(new g(m._point.subtract(w),w.multiply(2),!0).intersect(new g(y._point.subtract(x),x.multiply(2),!0),!1)){var b=z.subdivide(p);z.getIntersections(b[0],b[1],v,v,r,function(e){return e._parameter<=_?(e._parameter/=2,e._parameter2=.5+e._parameter2/2,!0):t})}}for(var C=e?0:f+1;l>C;C++)z.getIntersections(p,c[C],v,a[C],r,!e&&(C===f+1||C===l-1&&0===f)&&function(t){var e=t._parameter;return e>=d&&_>=e})}return z.filterIntersections(r,n)},_asPathItem:function(){return this},setPathData:function(t){function e(t,e){var i=+n[t];return o&&(i+=u[e]),i}function i(t){return new h(e(t,"x"),e(t+1,"y"))}var n,r,s,a=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),o=!1,u=new h,c=new h;this.clear();for(var d=0,_=a&&a.length;_>d;d++){var f=a[d],g=f[0],v=g.toLowerCase();n=f.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var p=n&&n.length;switch(o=g===v,"z"!==r||/[mz]/.test(v)||this.moveTo(u=c),v){case"m":case"l":for(var m="m"===v,y=0;p>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=i(y));s=u,m&&(c=u);break;case"h":case"v":for(var w="h"===v?"x":"y",y=0;p>y;y++)u[w]=e(y,w),this.lineTo(u);s=u;break;case"c":for(var y=0;p>y;y+=6)this.cubicCurveTo(i(y),s=i(y+2),u=i(y+4));break;case"s":for(var y=0;p>y;y+=4)this.cubicCurveTo(/[cs]/.test(r)?u.multiply(2).subtract(s):u,s=i(y),u=i(y+2)),r=v;break;case"q":for(var y=0;p>y;y+=4)this.quadraticCurveTo(s=i(y),u=i(y+2));break;case"t":for(var y=0;p>y;y+=2)this.quadraticCurveTo(s=/[qt]/.test(r)?u.multiply(2).subtract(s):u,u=i(y)),r=v;break;case"a":for(var y=0;p>y;y+=7)this.arcTo(u=i(y+5),new l(+n[y],+n[y+1]),+n[y+2],+n[y+4],+n[y+3]);break;case"z":this.closePath(!0)}r=v}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t,!1,!0);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),O=k.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[],this._version=0;var i=Array.isArray(e)?"object"==typeof e[0]?e:arguments:!e||e.size!==t||e.x===t&&e.point===t?null:arguments;i&&i.length>0?this.setSegments(i):(this._curves=t,this._selectedSegmentState=0,i||"string"!=typeof e||(this.setPathData(e),e=null)),this._initialize(!i&&e)},_equals:function(t){return this._closed===t._closed&&e.equals(this._segments,t._segments)},clone:function(e){var i=new O(m.NO_INSERT);return i.setSegments(this._segments),i._closed=this._closed,this._clockwise!==t&&(i._clockwise=this._clockwise),this._clone(i,e)},_changed:function gt(e){if(gt.base.call(this,e),8&e){var i=this._parent;if(i&&(i._currentPath=t),this._length=this._clockwise=t,16&e)this._version++;else if(this._curves)for(var n=0,r=this._curves.length;r>n;n++)this._curves[n]._changed();this._monoCurves=t}else 32&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof A?t:this)._style},getSegments:function(){return this._segments},setSegments:function(e){var i=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,this._curves=t,e&&e.length>0&&this._add(P.readAll(e)),i&&this.setFullySelected(!0)},getFirstSegment:function(){return this._segments[0]},getLastSegment:function(){return this._segments[this._segments.length-1]},getCurves:function(){var t=this._curves,e=this._segments;if(!t){var i=this._countCurves();t=this._curves=Array(i);for(var n=0;i>n;n++)t[n]=new z(this,e[n],e[n+1]||e[0])}return t},getFirstCurve:function(){return this.getCurves()[0]},getLastCurve:function(){var t=this.getCurves();return t[t.length-1]},isClosed:function(){return this._closed},setClosed:function(t){if(this._closed!=(t=!!t)){if(this._closed=t,this._curves){var e=this._curves.length=this._countCurves();t&&(this._curves[e-1]=new z(this,this._segments[e-1],this._segments[0]))}this._changed(25)}}},{beans:!0,getPathData:function(t,e){function i(e,i){e._transformCoordinates(t,g,!1),n=g[0],r=g[1],v?(p.push("M"+f.pair(n,r)),v=!1):(h=g[2],u=g[3],h===n&&u===r&&l===a&&c===o?i||p.push("l"+f.pair(n-a,r-o)):p.push("c"+f.pair(l-a,c-o)+" "+f.pair(h-a,u-o)+" "+f.pair(n-a,r-o))),a=n,o=r,l=g[4],c=g[5]}var n,r,a,o,h,u,l,c,d=this._segments,_=d.length,f=new s(e),g=Array(6),v=!0,p=[];if(0===_)return"";for(var m=0;_>m;m++)i(d[m]);return this._closed&&_>0&&(i(d[0],!0),p.push("z")),p.join("")}},{isEmpty:function(){return 0===this._segments.length},isLinear:function(){for(var t=this._segments,e=0,i=t.length;i>e;e++)if(!t[e].isLinear())return!1;return!0},hasHandles:function(){for(var t=this._segments,e=0,i=t.length;i>e;e++)if(t[e].hasHandles())return!0;return!1},_transformContent:function(t){for(var e=Array(6),i=0,n=this._segments.length;n>i;i++)this._segments[i]._transformCoordinates(t,e,!0);return!0},_add:function(t,e){for(var i=this._segments,n=this._curves,r=t.length,s=null==e,e=s?i.length:e,a=0;r>a;a++){var o=t[a];o._path&&(o=t[a]=o.clone()),o._path=this,o._index=e+a,o._selectionState&&this._updateSelection(o,0,o._selectionState)}if(s)i.push.apply(i,t);else{i.splice.apply(i,[e,0].concat(t));for(var a=e+r,h=i.length;h>a;a++)i[a]._index=a}if(n||t._curves){n||(n=this._curves=[]);var u=e>0?e-1:e,l=u,c=Math.min(u+r,this._countCurves());t._curves&&(n.splice.apply(n,[u,0].concat(t._curves)),l+=t._curves.length);for(var a=l;c>a;a++)n.splice(a,0,new z(this,null,null));this._adjustCurves(u,c)}return this._changed(25),t},_adjustCurves:function(t,e){for(var i,n=this._segments,r=this._curves,s=t;e>s;s++)i=r[s],i._path=this,i._segment1=n[s],i._segment2=n[s+1]||n[0],i._changed();(i=r[this._closed&&0===t?n.length-1:t-1])&&(i._segment2=n[t]||n[0],i._changed()),(i=r[e])&&(i._segment1=n[e],i._changed())},_countCurves:function(){var t=this._segments.length;return!this._closed&&t>0?t-1:t},add:function(t){return arguments.length>1&&"number"!=typeof t?this._add(P.readAll(arguments)):this._add([P.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(P.readAll(arguments,1),t):this._add([P.read(arguments,1)],t)[0]},addSegment:function(){return this._add([P.read(arguments)])[0]},insertSegment:function(t){return this._add([P.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(P.readAll(t))},insertSegments:function(t,e){return this._add(P.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,i,n){t=t||0,i=e.pick(i,this._segments.length);var r=this._segments,s=this._curves,a=r.length,o=r.splice(t,i-t),h=o.length;if(!h)return o;for(var u=0;h>u;u++){var l=o[u];l._selectionState&&this._updateSelection(l,l._selectionState,0),l._index=l._path=null}for(var u=t,c=r.length;c>u;u++)r[u]._index=u;if(s){var d=t>0&&i===a+(this._closed?1:0)?t-1:t,s=s.splice(d,h);n&&(o._curves=s.slice(1)),this._adjustCurves(d,d)}return this._changed(25),o},clear:"#removeSegments",getLength:function(){if(null==this._length){var t=this.getCurves();this._length=0;for(var e=0,i=t.length;i>e;e++)this._length+=t[e].getLength()}return this._length},getArea:function(){for(var t=this.getCurves(),e=0,i=0,n=t.length;n>i;i++)e+=t[i].getArea();return e},isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===7*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function vt(t){t||this._selectSegments(!1),vt.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?7*e:0;for(var i=0;e>i;i++)this._segments[i]._selectionState=t?7:0},_updateSelection:function(t,e,i){t._selectionState=i;var n=this._selectedSegmentState+=i-e;n>0&&this.setSelected(!0)},flatten:function(t){for(var e=new T(this,64,.1),i=0,n=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-n:n)/2,s=[];r>=i;)s.push(new P(e.getPointAt(i))),i+=n;this.setSegments(s)},reduce:function(){for(var t=this.getCurves(),e=t.length-1;e>=0;e--){var i=t[e];i.isLinear()&&0===i.getLength()&&i.remove()}return this},simplify:function(t){if(this._segments.length>2){var e=new L(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null===e)return null;if(1===arguments.length){var i=t;if("number"==typeof i&&(i=this.getLocationAt(i)),!i)return null;t=i.index,e=i.parameter}var n=1e-6;e>=1-n&&(t++,e--);var r=this.getCurves();if(t>=0&&tn&&r[t++].divide(e,!0);var s,a=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),s=this):s=this._clone((new O).insertAbove(this,!0)),s._add(a,0),this.addSegment(a[0]),s}return null},isClockwise:function(){return this._clockwise!==t?this._clockwise:O.isClockwise(this._segments)},setClockwise:function(t){this.isClockwise()!=(t=!!t)&&this.reverse(),this._clockwise=t},reverse:function(){this._segments.reverse();for(var e=0,i=this._segments.length;i>e;e++){var n=this._segments[e],r=n._handleIn;n._handleIn=n._handleOut,n._handleOut=r,n._index=e}this._curves=null,this._clockwise!==t&&(this._clockwise=!this._clockwise),this._changed(9)},join:function(t){if(t){var e=t._segments,i=this.getLastSegment(),n=t.getLastSegment();if(!n)return this;i&&i._point.equals(n._point)&&t.reverse();var r=t.getFirstSegment();if(i&&i._point.equals(r._point))i.setHandleOut(r._handleOut),this._add(e.slice(1));else{var s=this.getFirstSegment();s&&s._point.equals(r._point)&&t.reverse(),n=t.getLastSegment(),s&&s._point.equals(n._point)?(s.setHandleIn(n._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())}t.closed&&this._add([e[0]]),t.remove()}var a=this.getFirstSegment(),o=this.getLastSegment();return a!==o&&a._point.equals(o._point)&&(a.setHandleIn(o._handleIn),o.remove(),this.setClosed(!0)),this},toShape:function(t){function e(t,e){return c[t].isCollinear(c[e])}function i(t){return c[t].isOrthogonal()}function n(t){return c[t].isOrthogonalArc()}function r(t,e){return c[t]._point.getDistance(c[e]._point)}if(!this._closed)return null;var s,o,h,u,c=this._segments;if(!this.hasHandles()&&4===c.length&&e(0,2)&&e(1,3)&&i(1)?(s=x.Rectangle,o=new l(r(0,3),r(0,1)),u=c[1]._point.add(c[2]._point).divide(2)):8===c.length&&n(0)&&n(2)&&n(4)&&n(6)&&e(1,5)&&e(3,7)?(s=x.Rectangle,o=new l(r(1,6),r(0,3)),h=o.subtract(new l(r(0,7),r(1,2))).divide(2),u=c[3]._point.add(c[4]._point).divide(2)):4===c.length&&n(0)&&n(1)&&n(2)&&n(3)&&(a.isZero(r(0,2)-r(1,3))?(s=x.Circle,h=r(0,2)/2):(s=x.Ellipse,h=new l(r(2,0)/2,r(3,1)/2)),u=c[1]._point),s){var d=this.getPosition(!0),_=this._clone(new s({center:d,size:o,radius:h,insert:!1}),t,!1);return _.rotate(u.subtract(d).getAngle()+90),_}return null},_hitTestSelf:function(t,e){function i(e,i){return t.subtract(e).divide(i).length<=1}function n(t,n,r){if(!e.selected||n.isSelected()){var s=t._point;if(n!==s&&(n=n.add(s)),i(n,w))return new S(r,f,{segment:t,point:n})}}function r(t,i){return(i||e.segments)&&n(t,t._point,"segment")||!i&&e.handles&&(n(t,t._handleIn,"handle-in")||n(t,t._handleOut,"handle-out"))}function s(t){c.add(t)}function a(e){if(("round"!==o||"round"!==u)&&(c=new O({internal:!0,closed:!0}),m||e._index>0&&e._index0||C?0:null;if(null!==P&&(P>0?(o=g.getStrokeJoin(),u=g.getStrokeCap(),l=P*g.getMiterLimit(),w=y.add(new h(P,P))):o=u="round"),!e.ends||e.segments||m){if(e.segments||e.handles)for(var M=0;p>M;M++)if(_=r(v[M]))return _}else if(_=r(v[0],!0)||r(v[p-1],!0))return _;if(null!==P){if(d=this.getNearestLocation(t)){var z=d.getParameter();0===z||1===z&&p>1?a(d.getSegment())||(d=null):i(d.getPoint(),w)||(d=null)}if(!d&&"miter"===o&&p>1)for(var M=0;p>M;M++){var I=v[M];if(t.getDistance(I._point)<=l&&a(I)){d=I.getLocation();break}}}return!d&&b&&this._contains(t)||d&&!x&&!C?new S("fill",this):d?new S(x?"stroke":"curve",this,{location:d,point:d.getPoint()}):null}},e.each(z.evaluateMethods,function(t){this[t+"At"]=function(e,i){var n=this.getLocationAt(e,i);return n&&n[t]()}},{beans:!1,_getOffset:function(t){var e=t&&t.getIndex();if(null!=e){for(var i=this.getCurves(),n=0,r=0;e>r;r++)n+=i[r].getLength();var s=i[e],a=t.getParameter();return a>0&&(n+=s.getPartLength(0,a)),n}return null},getLocationOf:function(){for(var t=h.read(arguments),e=this.getCurves(),i=0,n=e.length;n>i;i++){var r=e[i].getLocationOf(t);if(r)return r}return null},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getLocationAt:function(t,e){var i=this.getCurves(),n=0;if(e){var r=~~t,s=i[r];return s?s.getLocationAt(t-r,!0):null}for(var a=0,o=i.length;o>a;a++){var h=n,s=i[a];if(n+=s.getLength(),n>t)return s.getLocationAt(t-h)}return t<=this.getLength()?new I(i[i.length-1],1):null},getNearestLocation:function(){for(var t=h.read(arguments),e=this.getCurves(),i=1/0,n=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceo;o++){var u=e[o];u._transformCoordinates(i,a,!1);var l=u._selectionState,c=a[0],d=a[1];if(1&l&&r(2),2&l&&r(4),t.fillRect(c-s,d-s,n,n),!(4&l)){var _=t.fillStyle;t.fillStyle="#ffffff",t.fillRect(c-s+1,d-s+1,n-2,n-2),t.fillStyle=_}}}function e(t,e,i){function n(e){if(i)e._transformCoordinates(i,f,!1),r=f[0],s=f[1];else{var n=e._point;r=n._x,s=n._y}if(g)t.moveTo(r,s),g=!1;else{if(i)h=f[2],u=f[3];else{var d=e._handleIn;h=r+d._x,u=s+d._y}h===r&&u===s&&l===a&&c===o?t.lineTo(r,s):t.bezierCurveTo(l,c,h,u,r,s)}if(a=r,o=s,i)l=f[4],c=f[5];else{var d=e._handleOut;l=a+d._x,c=o+d._y}}for(var r,s,a,o,h,u,l,c,d=e._segments,_=d.length,f=Array(6),g=!0,v=0;_>v;v++)n(d[v]);e._closed&&_>0&&n(d[0])}return{_draw:function(t,i,n){function r(t){return l[(t%c+c)%c]}var s=i.dontStart,a=i.dontFinish||i.clip,o=this.getStyle(),h=o.hasFill(),u=o.hasStroke(),l=o.getDashArray(),c=!paper.support.nativeDash&&u&&l&&l.length;if(s||t.beginPath(),!s&&this._currentPath?t.currentPath=this._currentPath:(h||u&&!c||a)&&(e(t,this,n),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!a&&(h||u)&&(this._setStyles(t),h&&(t.fill(o.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),u)){if(c){s||t.beginPath();var d,_=new T(this,32,.25,n),f=_.length,g=-o.getDashOffset(),v=0;for(g%=f;g>0;)g-=r(v--)+r(v--);for(;f>g;)d=g+r(v++),(g>0||d>0)&&_.drawPart(t,Math.max(g,0),Math.max(d,0)),g=d+r(v++)}t.stroke()}},_drawSelected:function(i,n){i.beginPath(),e(i,this,n),i.stroke(),t(i,this._segments,n,paper.settings.handleSize)}}},new function(){function t(t){var e=t.length,i=[],n=[],r=2;i[0]=t[0]/r;for(var s=1;e>s;s++)n[s]=1/r,r=(e-1>s?4:2)-n[s],i[s]=(t[s]-i[s-1])/r;for(var s=1;e>s;s++)i[e-s-1]-=n[e-s]*i[e-s];return i}return{smooth:function(){var e=this._segments,i=e.length,n=this._closed,r=i,s=0;if(!(2>=i)){n&&(s=Math.min(i,4),r+=2*Math.min(i,s));for(var a=[],o=0;i>o;o++)a[o+s]=e[o]._point;if(n)for(var o=0;s>o;o++)a[o]=e[o+i-s]._point,a[o+i+s]=e[o]._point;else r--;for(var u=[],o=1;r-1>o;o++)u[o]=4*a[o]._x+2*a[o+1]._x;u[0]=a[0]._x+2*a[1]._x,u[r-1]=3*a[r-1]._x;for(var l=t(u),o=1;r-1>o;o++)u[o]=4*a[o]._y+2*a[o+1]._y;u[0]=a[0]._y+2*a[1]._y,u[r-1]=3*a[r-1]._y;var c=t(u);if(n){for(var o=0,d=i;s>o;o++,d++){var _=o/s,f=1-_,g=o+s,v=d+s;l[d]=l[o]*_+l[d]*f,c[d]=c[o]*_+c[d]*f,l[v]=l[g]*f+l[v]*_,c[v]=c[g]*f+c[v]*_}r--}for(var p=null,o=s;r-s>=o;o++){var m=e[o-s];p&&m.setHandleIn(p.subtract(m._point)),r>o&&(m.setHandleOut(new h(l[o],c[o]).subtract(m._point)),p=r-1>o?new h(2*a[o+1]._x-l[o+1],2*a[o+1]._y-c[o+1]):new h((a[r]._x+l[r-1])/2,(a[r]._y+c[r-1])/2))}if(n&&p){var m=this._segments[0];m.setHandleIn(p.subtract(m._point))}}}}},new function(){function t(t){var e=t._segments;if(0===e.length)throw Error("Use a moveTo() command first");return e[e.length-1]}return{moveTo:function(){var t=this._segments;1===t.length&&this.removeSegment(0),t.length||this._add([new P(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new P(h.read(arguments))])},cubicCurveTo:function(){var e=h.read(arguments),i=h.read(arguments),n=h.read(arguments),r=t(this);r.setHandleOut(e.subtract(r._point)),this._add([new P(n,i.subtract(n))])},quadraticCurveTo:function(){var e=h.read(arguments),i=h.read(arguments),n=t(this)._point;this.cubicCurveTo(e.add(n.subtract(e).multiply(1/3)),e.add(i.subtract(e).multiply(1/3)),i)},curveTo:function(){var i=h.read(arguments),n=h.read(arguments),r=e.pick(e.read(arguments),.5),s=1-r,a=t(this)._point,o=i.subtract(a.multiply(s*s)).subtract(n.multiply(r*r)).divide(2*r*s);if(o.isNaN())throw Error("Cannot put a curve through points with parameter = "+r);this.quadraticCurveTo(o,n)},arcTo:function(){var i,n,r,s,a,o=t(this),u=o._point,c=h.read(arguments),d=e.peek(arguments),_=e.pick(d,!0);if("boolean"==typeof _)var v=u.add(c).divide(2),i=v.add(v.subtract(u).rotate(_?-90:90));else if(e.remain(arguments)<=2)i=c,c=h.read(arguments);else{var p=l.read(arguments);if(p.isZero())return this.lineTo(c);var m=e.read(arguments),_=!!e.read(arguments),y=!!e.read(arguments),v=u.add(c).divide(2),w=u.subtract(v).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=1e-12,M=C(p.width),z=C(p.height),I=M*M,k=z*z,O=x*x,A=b*b,T=Math.sqrt(O/I+A/k);if(T>1&&(M*=T,z*=T,I=M*M,k=z*z),T=(I*k-I*A-k*O)/(I*A+k*O),C(T)T)throw Error("Cannot create an arc with the given arguments");n=new h(M*b/z,-z*x/M).multiply((y===_?-1:1)*Math.sqrt(T)).rotate(m).add(v),a=(new f).translate(n).rotate(m).scale(M,z),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(c)),!_&&r>0?r-=360:_&&0>r&&(r+=360)}if(i){var L=new g(u.add(i).divide(2),i.subtract(u).rotate(90),!0),N=new g(i.add(c).divide(2),c.subtract(i).rotate(90),!0),j=new g(u,c),B=j.getSide(i);if(n=L.intersect(N,!0),!n){if(!B)return this.lineTo(c);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(n),r=s.getDirectedAngle(c.subtract(n));var D=j.getSide(n);0===D?r=B*Math.abs(r):B===D&&(r+=0>r?360:-360)}for(var E=Math.abs(r),R=E>=360?4:Math.ceil(E/90),F=r/R,q=F*Math.PI/360,V=4/3*Math.sin(q)/(1+Math.cos(q)),Z=[],H=0;R>=H;H++){var w=c,W=null;if(R>H&&(W=s.rotate(90).multiply(V),a?(w=a._transformPoint(s),W=a._transformPoint(s.add(W)).subtract(w)):w=n.add(s)),0===H)o.setHandleOut(W);else{var G=s.rotate(-90).multiply(V);a&&(G=a._transformPoint(s.add(G)).subtract(w)),Z.push(new P(w,G,W))}s=s.rotate(F)}this._add(Z)},lineBy:function(){var e=h.read(arguments),i=t(this)._point;this.lineTo(i.add(e))},curveBy:function(){var i=h.read(arguments),n=h.read(arguments),r=e.read(arguments),s=t(this)._point;this.curveTo(s.add(i),s.add(n),r)},cubicCurveBy:function(){var e=h.read(arguments),i=h.read(arguments),n=h.read(arguments),r=t(this)._point;this.cubicCurveTo(r.add(e),r.add(i),r.add(n))},quadraticCurveBy:function(){var e=h.read(arguments),i=h.read(arguments),n=t(this)._point;this.quadraticCurveTo(n.add(e),n.add(i))},arcBy:function(){var i=t(this)._point,n=i.add(h.read(arguments)),r=e.pick(e.peek(arguments),!0);"boolean"==typeof r?this.arcTo(n,r):this.arcTo(n,i.add(h.read(arguments)))},closePath:function(t){this.setClosed(!0),t&&this.join()}}},{_getBounds:function(t,e){return O[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,i=0,n=t.length;n>i;i++)e+=z.getEdgeSum(z.getValues(t[i],t[n>i+1?i+1:0]));return e>0},getBounds:function(t,e,i,n,r){function s(t){t._transformCoordinates(n,o,!1);for(var e=0;2>e;e++)z._addBounds(h[e],h[e+4],o[e+2],o[e],e,r?r[e]:0,u,l,c);var i=h;h=o,o=i}var a=t[0];if(!a)return new d;for(var o=Array(6),h=a._transformCoordinates(n,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),_=1,f=t.length;f>_;_++)s(t[_]);return e&&s(a),new d(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,i,n){function r(t){_=_.include(n?n._transformPoint(t,t):t)}function s(t){_=_.unite(p.setCenter(n?n._transformPoint(t._point):t._point))}function a(t,e){var i=t._handleIn,n=t._handleOut;"round"===e||!i.isZero()&&!n.isZero()&&i.isCollinear(n)?s(t):O._addBevelJoin(t,e,u,v,r)}function o(t,e){"round"===e?s(t):O._addSquareCap(t,e,u,r)}if(!i.hasStroke())return O.getBounds(t,e,i,n);for(var h=t.length-(e?0:1),u=i.getStrokeWidth()/2,c=O._getPenPadding(u,n),_=O.getBounds(t,e,i,n,c),f=i.getStrokeJoin(),g=i.getStrokeCap(),v=u*i.getMiterLimit(),p=new d(new l(c).multiply(2)),m=1;h>m;m++)a(t[m],f);return e?a(t[0],f):h>0&&(o(t[0],g),o(t[t.length-1],g)),_},_getPenPadding:function(t,e){if(!e)return[t,t];var i=e.shiftless(),n=i.transform(new h(t,0)),r=i.transform(new h(0,t)),s=n.getAngleInRadians(),a=n.getLength(),o=r.getLength(),u=Math.sin(s),l=Math.cos(s),c=Math.tan(s),d=-Math.atan(o*c/a),_=Math.atan(o/(c*a));return[Math.abs(a*Math.cos(d)*l-o*Math.sin(d)*u),Math.abs(o*Math.sin(_)*l+a*Math.cos(_)*u)]},_addBevelJoin:function(t,e,i,n,r,s){var a=t.getCurve(),o=a.getPrevious(),u=a.getPointAt(0,!0),l=o.getNormalAt(1,!0),c=a.getNormalAt(0,!0),d=l.getDirectedAngle(c)<0?-i:i;if(l.setLength(d),c.setLength(d),s&&(r(u),r(u.add(l))),"miter"===e){var _=new g(u.add(l),new h(-l.y,l.x),!0).intersect(new g(u.add(c),new h(-c.y,c.x),!0),!0);if(_&&u.getDistance(_)<=n&&(r(_),!s))return}s||r(u.add(l)),r(u.add(c))},_addSquareCap:function(t,e,i,n,r){var s=t._point,a=t.getLocation(),o=a.getNormal().multiply(i);r&&(n(s.subtract(o)),n(s.add(o))),"square"===e&&(s=s.add(o.rotate(0===a.getParameter()?-90:90))),n(s.add(o)),n(s.subtract(o))},getHandleBounds:function(t,e,i,n,r,s){for(var a=Array(6),o=1/0,h=-o,u=o,l=h,c=0,_=t.length;_>c;c++){var f=t[c];f._transformCoordinates(n,a,!1);for(var g=0;6>g;g+=2){var v=0===g?s:r,p=v?v[0]:0,m=v?v[1]:0,y=a[g],w=a[g+1],x=y-p,b=y+p,C=w-m,S=w+m;o>x&&(o=x),b>h&&(h=b),u>C&&(u=C),S>l&&(l=S)}}return new d(o,u,h-o,l-u)},getRoughBounds:function(t,e,i,n){var r=i.hasStroke()?i.getStrokeWidth()/2:0,s=r;return r>0&&("miter"===i.getStrokeJoin()&&(s=r*i.getMiterLimit()),"square"===i.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),O.getHandleBounds(t,e,i,n,O._getPenPadding(r,n),O._getPenPadding(s,n))}}});O.inject({statics:new function(){function t(t,i,n){var r=e.getNamed(n),s=new O(r&&r.insert===!1&&m.NO_INSERT);return s._add(t),s._closed=i,s.set(r)}function i(e,i,n){for(var s=Array(4),a=0;4>a;a++){var o=r[a];s[a]=new P(o._point.multiply(i).add(e),o._handleIn.multiply(i),o._handleOut.multiply(i))}return t(s,!0,n)}var n=.5522847498307936,r=[new P([-1,0],[0,n],[0,-n]),new P([0,-1],[-n,0],[n,0]),new P([1,0],[0,-n],[0,n]),new P([0,1],[n,0],[-n,0])];return{Line:function(){return t([new P(h.readNamed(arguments,"from")),new P(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"radius");return i(t,new l(n),arguments)},Rectangle:function(){var e,i=d.readNamed(arguments,"rectangle"),r=l.readNamed(arguments,"radius",0,{readNull:!0}),s=i.getBottomLeft(!0),a=i.getTopLeft(!0),o=i.getTopRight(!0),h=i.getBottomRight(!0);if(!r||r.isZero())e=[new P(s),new P(a),new P(o),new P(h)];else{r=l.min(r,i.getSize(!0).divide(2));var u=r.width,c=r.height,_=u*n,f=c*n;e=[new P(s.add(u,0),null,[-_,0]),new P(s.subtract(0,c),[0,f]),new P(a.add(0,c),null,[0,-f]),new P(a.add(u,0),[-_,0],null),new P(o.subtract(u,0),null,[_,0]),new P(o.add(0,c),[0,-f],null),new P(h.subtract(0,c),null,[0,f]),new P(h.subtract(u,0),[_,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=x._readEllipse(arguments);return i(t.center,t.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),i=h.readNamed(arguments,"through"),n=h.readNamed(arguments,"to"),r=e.getNamed(arguments),s=new O(r&&r.insert===!1&&m.NO_INSERT);return s.moveTo(t),s.arcTo(i,n),s.set(r)},RegularPolygon:function(){for(var i=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"sides"),r=e.readNamed(arguments,"radius"),s=360/n,a=!(n%3),o=new h(0,a?-r:r),u=a?-1:.5,l=Array(n),c=0;n>c;c++)l[c]=new P(i.add(o.rotate((c+u)*s)));return t(l,!0,arguments)},Star:function(){for(var i=h.readNamed(arguments,"center"),n=2*e.readNamed(arguments,"points"),r=e.readNamed(arguments,"radius1"),s=e.readNamed(arguments,"radius2"),a=360/n,o=new h(0,-1),u=Array(n),l=0;n>l;l++)u[l]=new P(i.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var A=k.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||("string"==typeof t?this.setPathData(t):this.addChildren(Array.isArray(t)?t:arguments))},insertChildren:function pt(e,i,n){i=pt.base.call(this,e,i,n,O);for(var r=0,s=!n&&i&&i.length;s>r;r++){var a=i[r];a._clockwise===t&&a.setClockwise(0===a._index)}return i},reverse:function(){for(var t=this._children,e=0,i=t.length;i>e;e++)t[e].reverse()},smooth:function(){for(var t=0,e=this._children.length;e>t;t++)this._children[t].smooth()},reduce:function mt(){if(0===this._children.length){var t=new O(m.NO_INSERT);return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return mt.base.call(this)},isClockwise:function(){var t=this.getFirstChild();return t&&t.isClockwise()},setClockwise:function(t){this.isClockwise()!==!!t&&this.reverse()},getFirstSegment:function(){var t=this.getFirstChild();return t&&t.getFirstSegment()},getLastSegment:function(){var t=this.getLastChild();return t&&t.getLastSegment()},getCurves:function(){for(var t=this._children,e=[],i=0,n=t.length;n>i;i++)e.push.apply(e,t[i].getCurves());return e},getFirstCurve:function(){var t=this.getFirstChild();return t&&t.getFirstCurve()},getLastCurve:function(){var t=this.getLastChild();return t&&t.getFirstCurve()},getArea:function(){for(var t=this._children,e=0,i=0,n=t.length;n>i;i++)e+=t[i].getArea();return e}},{beans:!0,getPathData:function(t,e){for(var i=this._children,n=[],r=0,s=i.length;s>r;r++){var a=i[r],o=a._matrix;n.push(a.getPathData(t&&!o.isIdentity()?t.chain(o):o,e))}return n.join(" ")}},{_getChildHitTestOptions:function(t){return t["class"]===O||"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e,i){var n=this._children;if(0!==n.length){if(this._currentPath)t.currentPath=this._currentPath;else{e=e.extend({dontStart:!0,dontFinish:!0}),t.beginPath();for(var r=0,s=n.length;s>r;r++)n[r].draw(t,e,i);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var a=this._style;a.hasFill()&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),a.hasStroke()&&t.stroke()}}},_drawSelected:function(t,e,i){for(var n=this._children,r=0,s=n.length;s>r;r++){var a=n[r],o=a._matrix;i[a._id]||a._drawSelected(t,o.isIdentity()?e:e.chain(o))}}},new function(){function t(t,e){var i=t._children;if(e&&0===i.length)throw Error("Use a moveTo() command first");return i[i.length-1]}var i={moveTo:function(){var e=t(this),i=e&&e.isEmpty()?e:new O;i!==e&&this.addChild(i),i.moveTo.apply(i,arguments)},moveBy:function(){var e=t(this,!0),i=e&&e.getLastSegment(),n=h.read(arguments);this.moveTo(i?n.add(i._point):n)},closePath:function(e){t(this,!0).closePath(e)}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){i[e]=function(){var i=t(this,!0);i[e].apply(i,arguments)}}),i});k.inject(new function(){function t(t,s,a){function o(t){return t.clone(!1).reduce().reorient().transform(null,!0,!0)}function h(t){for(var e=0,i=t.length;i>e;e++){var n=t[e];_.push.apply(_,n._segments),f.push.apply(f,n._getMonoCurves())}}var u=r[a],l=o(t),c=s&&t!==s&&o(s);c&&/^(subtract|exclude)$/.test(a)^c.isClockwise()!==l.isClockwise()&&c.reverse(),e(l.getIntersections(c,null,!0));var d=[],_=[],f=[],g=1e-6;h(l._children||[l]),c&&h(c._children||[c]),_.sort(function(t,e){var i=t._intersection,n=e._intersection;return!i&&!n||i&&n?0:i?-1:1});for(var v=0,p=_.length;p>v;v++){var y=_[v];if(null==y._winding){d.length=0;var w=y,x=0,b=0;do{var C=y.getCurve().getLength();d.push({segment:y,length:C}),x+=C,y=y.getNext()}while(y&&!y._intersection&&y!==w);for(var S=0;3>S;S++)for(var C=x*(S+1)/4,P=0,M=d.length;M>P;P++){var z=d[P],I=z.length;if(I>=C){(g>C||g>I-C)&&(C=I/2);var k=z.segment.getCurve(),O=k.getPointAt(C),T=k.isLinear()&&Math.abs(k.getTangentAt(.5,!0).y)=0;S--)d[S].segment._winding=N}}var j=new A(m.NO_INSERT);return j.insertAbove(t),j.addChildren(n(_,u),!0),j=j.reduce(),j.setStyle(t._style),j}function e(t){function e(){for(var t=0,e=i.length;e>t;t++)i[t].set(0,0)}for(var i,n,r,s=1e-6,a=1-s,o=t.length-1;o>=0;o--){var h=t[o],u=h._parameter;r&&r._curve===h._curve&&r._parameter>0?u/=r._parameter:(n=h._curve,i&&e(),i=n.isLinear()?[n._segment1._handleOut,n._segment2._handleIn]:null);var l,c;(l=n.divide(u,!0,!0))?(c=l._segment1,n=l.getPrevious(),i&&i.push(c._handleOut,c._handleIn)):c=s>u?n._segment1:u>a?n._segment2:n.getPartLength(0,u)w;w++){var b=e[w].values;if(z.solveCubic(b,0,l,f,0,1)>0)for(var C=f.length-1;C>=0;C--){var S=z.getPoint(b,f[C]).y;m>S&&S>v?v=S:S>y&&p>S&&(p=S)}}v=(v+c)/2,p=(p+c)/2,v>-(1/0)&&(d=i(new h(l,v),e)),1/0>p&&(_=i(new h(l,p),e))}else for(var P,M,I=l-s,k=l+s,O=!1,w=0,x=e.length;x>w;w++){var A=e[w],b=A.values,T=A.winding;if(T&&(1===T&&c>=b[1]&&c<=b[7]||c>=b[7]&&c<=b[1])&&1===z.solveCubic(b,1,c,f,0,1)){var L=f[0];if(!(L>u&&O&&A.next!==e[w+1]||o>L&&M>u&&A.previous===P)){var N=z.getPoint(b,L).x,j=z.getTangent(b,L).y,B=!1;a.isZero(j)&&!z.isLinear(b)||o>L&&j*z.getTangent(A.previous.values,1).y<0||L>u&&j*z.getTangent(A.next.values,0).y<0?r&&N>=I&&k>=N&&(++d,++_,B=!0):I>=N?(d+=T,B=!0):N>=k&&(_+=T,B=!0),A.previous!==e[w-1]&&(O=o>L&&B)}P=A,M=L}}return Math.max(g(d),g(_))}function n(t,e,i){for(var n,r,s=[],a=1e-6,o=1-a,h=0,u=t.length;u>h;h++)if(n=r=t[h],!n._visited&&e(n._winding)){var l=new O(m.NO_INSERT),c=n._intersection,d=c&&c._segment,_=!1,f=1;do{var g,v=f>0?n._handleIn:n._handleOut,p=f>0?n._handleOut:n._handleIn;if(_&&(!e(n._winding)||i)&&(c=n._intersection)&&(g=c._segment)&&g!==r){if(i)n._visited=g._visited,n=g,f=1;else{var y=n.getCurve();f>0&&(y=y.getPrevious());var w=y.getTangentAt(1>f?a:o,!0),x=g.getCurve(),b=x.getPrevious(),C=b.getTangentAt(o,!0),S=x.getTangentAt(a,!0),M=w.cross(C),z=w.cross(S);if(M*z!==0){var I=z>M?b:x,k=e(I._segment1._winding)?I:z>M?x:b,A=k._segment1;f=k===b?-1:1,A._visited&&n._path!==A._path||!e(A._winding)?f=1:(n._visited=g._visited,n=g,A._visited&&(f=1))}else f=1}p=f>0?n._handleOut:n._handleIn}l.add(new P(n._point,_&&v,p)),_=!0,n._visited=!0,n=f>0?n.getNext():n.getPrevious()}while(n&&!n._visited&&n!==r&&n!==d&&(n._intersection||e(n._winding)));!n||n!==r&&n!==d?l.lastSegment._handleOut.set(0,0):(l.firstSegment.setHandleIn((n===d?d:n)._handleIn),l.setClosed(!0)),l._segments.length>(l._closed?l.isLinear()?2:0:1)&&s.push(l)}return s}var r={unite:function(t){return 1===t||0===t},intersect:function(t){return 2===t},subtract:function(t){return 1===t},exclude:function(t){ -return 1===t}};return{_getWinding:function(t,e,n){return i(t,this._getMonoCurves(),e,n)},unite:function(e){return t(this,e,"unite")},intersect:function(e){return t(this,e,"intersect")},subtract:function(e){return t(this,e,"subtract")},exclude:function(e){return t(this,e,"exclude")},divide:function(t){return new y([this.subtract(t),this.intersect(t)])}}}),O.inject({_getMonoCurves:function(){function t(t){var e=t[1],r=t[7],s={values:t,winding:e===r?0:e>r?-1:1,previous:i,next:null};i&&(i.next=s),n.push(s),i=s}function e(e){if(0!==z.getLength(e)){var i=e[1],n=e[3],r=e[5],s=e[7];if(z.isLinear(e))t(e);else{var o=3*(n-r)-i+s,h=2*(i+r)-4*n,u=n-i,l=1e-6,c=[],d=a.solveQuadratic(o,h,u,c,l,1-l);if(0===d)t(e);else{c.sort();var _=c[0],f=z.subdivide(e,_);t(f[0]),d>1&&(_=(c[1]-_)/(1-_),f=z.subdivide(f[1],_),t(f[0])),t(f[1])}}}}var i,n=this._monoCurves;if(!n){n=this._monoCurves=[];for(var r=this.getCurves(),s=this._segments,o=0,h=r.length;h>o;o++)e(r[o].getValues());if(!this._closed&&s.length>1){var u=s[s.length-1]._point,l=s[0]._point,c=u._x,d=u._y,_=l._x,f=l._y;e([c,d,c,d,_,f,_,f])}if(n.length>0){var g=n[0],v=n[n.length-1];g.previous=v,v.next=g}}return n},getInteriorPoint:function(){var t=this.getBounds(),e=t.getCenter(!0);if(!this.contains(e)){for(var i=this._getMonoCurves(),n=[],r=e.y,s=[],a=0,o=i.length;o>a;a++){var h=i[a].values;if((1===i[a].winding&&r>=h[1]&&r<=h[7]||r>=h[7]&&r<=h[1])&&z.solveCubic(h,1,r,n,0,1)>0)for(var u=n.length-1;u>=0;u--)s.push(z.getPoint(h,n[u]).x);if(s.length>1)break}e.x=(s[0]+s[1])/2}return e},reorient:function(){return this.setClockwise(!0),this}}),A.inject({_getMonoCurves:function(){for(var t=this._children,e=[],i=0,n=t.length;n>i;i++)e.push.apply(e,t[i]._getMonoCurves());return e},reorient:function(){var t=this.removeChildren().sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()});if(t.length>0){this.addChildren(t);for(var e=t[0].isClockwise(),i=1,n=t.length;n>i;i++){for(var r=t[i].getInteriorPoint(),s=0,a=i-1;a>=0;a--)t[a].contains(r)&&s++;t[i].setClockwise(s%2===0&&e)}}return this}});var T=e.extend({_class:"PathIterator",initialize:function(t,e,i,n){function r(t,e){var i=z.getValues(t,e,n);o.push(i),s(i,t._index,0,1)}function s(t,e,n,r){if(r-n>l&&!z.isFlatEnough(t,i||.25)){var a=z.subdivide(t),o=(n+r)/2;s(a[0],e,n,o),s(a[1],e,o,r)}else{var c=t[6]-t[0],d=t[7]-t[1],_=Math.sqrt(c*c+d*d);_>1e-6&&(u+=_,h.push({offset:u,value:r,index:e}))}}for(var a,o=[],h=[],u=0,l=1/(e||32),c=t._segments,d=c[0],_=1,f=c.length;f>_;_++)a=c[_],r(d,a),d=a;t._closed&&r(a,c[0]),this.curves=o,this.parts=h,this.length=u,this.index=0},getParameterAt:function(t){for(var e,i=this.index;e=i,!(0==i||this.parts[--i].offsete;e++){var r=this.parts[e];if(r.offset>=t){this.index=e;var s=this.parts[e-1],a=s&&s.index==r.index?s.value:0,o=s?s.offset:0;return{value:a+(r.value-a)*(t-o)/(r.offset-o),index:r.index}}}var r=this.parts[this.parts.length-1];return{value:1,index:r.index}},drawPart:function(t,e,i){e=this.getParameterAt(e),i=this.getParameterAt(i);for(var n=e.index;n<=i.index;n++){var r=z.getPart(this.curves[n],n==e.index?e.value:0,n==i.index?i.value:1);n==e.index&&t.moveTo(r[0],r[1]),t.bezierCurveTo.apply(t,r.slice(2))}}},e.each(z.evaluateMethods,function(t){this[t+"At"]=function(e,i){var n=this.getParameterAt(e);return z[t](this.curves[n.index],n.value,i)}},{})),L=e.extend({initialize:function(t,e){for(var i,n=this.points=[],r=t._segments,s=0,a=r.length;a>s;s++){var o=r[s].point.clone();i&&i.equals(o)||(n.push(o),i=o)}t._closed&&(this.closed=!0,n.unshift(n[n.length-1]),n.push(n[1])),this.error=e},fit:function(){var t=this.points,e=t.length,i=this.segments=e>0?[new P(t[0])]:[];return e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.closed&&(i.shift(),i.pop()),i},fitCubic:function(e,i,n,r){if(i-e==1){var s=this.points[e],a=this.points[i],o=s.getDistance(a)/3;return this.addCurve([s,s.add(n.normalize(o)),a.add(r.normalize(o)),a]),t}for(var h,u=this.chordLengthParameterize(e,i),l=Math.max(this.error,this.error*this.error),c=!0,d=0;4>=d;d++){var _=this.generateBezier(e,i,u,n,r),f=this.findMaxError(e,i,_,u);if(f.error=l)break;c=this.reparameterize(e,i,u,_),l=f.error}var g=this.points[h-1].subtract(this.points[h]),v=this.points[h].subtract(this.points[h+1]),p=g.add(v).divide(2).normalize();this.fitCubic(e,h,n,p),this.fitCubic(h,i,p.negate(),r)},addCurve:function(t){var e=this.segments[this.segments.length-1];e.setHandleOut(t[1].subtract(t[0])),this.segments.push(new P(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,i,n,r){for(var s=1e-12,a=this.points[t],o=this.points[e],h=[[0,0],[0,0]],u=[0,0],l=0,c=e-t+1;c>l;l++){var d=i[l],_=1-d,f=3*d*_,g=_*_*_,v=f*_,p=f*d,m=d*d*d,y=n.normalize(v),w=r.normalize(p),x=this.points[t+l].subtract(a.multiply(g+v)).subtract(o.multiply(p+m));h[0][0]+=y.dot(y),h[0][1]+=y.dot(w),h[1][0]=h[0][1],h[1][1]+=w.dot(w),u[0]+=y.dot(x),u[1]+=w.dot(x)}var b,C,S=h[0][0]*h[1][1]-h[1][0]*h[0][1];if(Math.abs(S)>s){var P=h[0][0]*u[1]-h[1][0]*u[0],M=u[0]*h[1][1]-u[1]*h[0][1];b=M/S,C=P/S}else{var z=h[0][0]+h[0][1],I=h[1][0]+h[1][1];b=C=Math.abs(z)>s?u[0]/z:Math.abs(I)>s?u[1]/I:0}var k,O,A=o.getDistance(a),T=s*A;if(T>b||T>C)b=C=A/3;else{var L=o.subtract(a);k=n.normalize(b),O=r.normalize(C),k.dot(L)-O.dot(L)>A*A&&(b=C=A/3,k=O=null)}return[a,a.add(k||n.normalize(b)),o.add(O||r.normalize(C)),o]},reparameterize:function(t,e,i,n){for(var r=t;e>=r;r++)i[r-t]=this.findRoot(n,this.points[r],i[r-t]);for(var r=1,s=i.length;s>r;r++)if(i[r]<=i[r-1])return!1;return!0},findRoot:function(t,e,i){for(var n=[],r=[],s=0;2>=s;s++)n[s]=t[s+1].subtract(t[s]).multiply(3);for(var s=0;1>=s;s++)r[s]=n[s+1].subtract(n[s]).multiply(2);var a=this.evaluate(3,t,i),o=this.evaluate(2,n,i),h=this.evaluate(1,r,i),u=a.subtract(e),l=o.dot(o)+u.dot(h);return Math.abs(l)<1e-6?i:i-u.dot(o)/l},evaluate:function(t,e,i){for(var n=e.slice(),r=1;t>=r;r++)for(var s=0;t-r>=s;s++)n[s]=n[s].multiply(1-i).add(n[s+1].multiply(i));return n[0]},chordLengthParameterize:function(t,e){for(var i=[0],n=t+1;e>=n;n++)i[n-t]=i[n-t-1]+this.points[n].getDistance(this.points[n-1]);for(var n=1,r=e-t;r>=n;n++)i[n]/=i[r];return i},findMaxError:function(t,e,i,n){for(var r=Math.floor((e-t+1)/2),s=0,a=t+1;e>a;a++){var o=this.evaluate(3,i,n[a-t]),h=o.subtract(this.points[a]),u=h.x*h.x+h.y*h.y;u>=s&&(s=u,r=a)}return{error:s,index:r}}}),N=m.extend({_class:"TextItem",_boundsSelected:!0,_applyMatrix:!1,_canApplyMatrix:!1,_serializeFields:{content:null},_boundsGetter:"getBounds",initialize:function(i){this._content="",this._lines=[];var n=i&&e.isPlainObject(i)&&i.x===t&&i.y===t;this._initialize(n&&i,!n&&h.read(arguments))},_equals:function(t){return this._content===t._content},_clone:function yt(t,e,i){return t.setContent(this._content),yt.base.call(this,t,e,i)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(265)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),j=N.extend({_class:"PointText",initialize:function(){N.apply(this,arguments)},clone:function(t){return this._clone(new j(m.NO_INSERT),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.translate(t.subtract(this._matrix.getTranslation()))},_draw:function(t){if(this._content){this._setStyles(t);var e=this._style,i=this._lines,n=e.getLeading(),r=t.shadowColor;t.font=e.getFontStyle(),t.textAlign=e.getJustification();for(var s=0,a=i.length;a>s;s++){t.shadowColor=r;var o=i[s];e.hasFill()&&(t.fillText(o,0,0),t.shadowColor="rgba(0,0,0,0)"),e.hasStroke()&&t.strokeText(o,0,0),t.translate(0,n)}}},_getBounds:function(t,e){var i=this._style,n=this._lines,r=n.length,s=i.getJustification(),a=i.getLeading(),o=this.getView().getTextWidth(i.getFontStyle(),n),h=0;"left"!==s&&(h-=o/("center"===s?2:1));var u=new d(h,r?-.75*a:0,o,r*a);return e?e._transformBounds(u,u):u}}),B=e.extend(new function(){function t(t){var e,n=t.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/);if(n){e=[0,0,0];for(var r=0;3>r;r++){var s=n[r+1];e[r]=parseInt(1==s.length?s+s:s,16)/255}}else if(n=t.match(/^rgba?\((.*)\)$/)){e=n[1].split(",");for(var r=0,o=e.length;o>r;r++){var s=+e[r];e[r]=3>r?s/255:s}}else{var h=a[t];if(!h){i||(i=Y.getContext(1,1),i.globalCompositeOperation="copy"),i.fillStyle="rgba(0,0,0,0)",i.fillStyle=t,i.fillRect(0,0,1,1);var u=i.getImageData(0,0,1,1).data;h=a[t]=[u[0]/255,u[1]/255,u[2]/255]}e=h.slice()}return e}var i,n={gray:["gray"],rgb:["red","green","blue"],hsb:["hue","saturation","brightness"],hsl:["hue","saturation","lightness"],gradient:["gradient","origin","destination","highlight"]},r={},a={},u=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],l={"rgb-hsb":function(t,e,i){var n=Math.max(t,e,i),r=Math.min(t,e,i),s=n-r,a=0===s?0:60*(n==t?(e-i)/s+(i>e?6:0):n==e?(i-t)/s+2:(t-e)/s+4);return[a,0===n?0:s/n,n]},"hsb-rgb":function(t,e,i){t=(t/60%6+6)%6;var n=Math.floor(t),r=t-n,n=u[n],s=[i,i*(1-e),i*(1-e*r),i*(1-e*(1-r))];return[s[n[0]],s[n[1]],s[n[2]]]},"rgb-hsl":function(t,e,i){var n=Math.max(t,e,i),r=Math.min(t,e,i),s=n-r,a=0===s,o=a?0:60*(n==t?(e-i)/s+(i>e?6:0):n==e?(i-t)/s+2:(t-e)/s+4),h=(n+r)/2,u=a?0:.5>h?s/(n+r):s/(2-n-r);return[o,u,h]},"hsl-rgb":function(t,e,i){if(t=(t/360%1+1)%1,0===e)return[i,i,i];for(var n=[t+1/3,t,t-1/3],r=.5>i?i*(1+e):i+e-i*e,s=2*i-r,a=[],o=0;3>o;o++){var h=n[o];0>h&&(h+=1),h>1&&(h-=1),a[o]=1>6*h?s+6*(r-s)*h:1>2*h?r:2>3*h?s+(r-s)*(2/3-h)*6:s}return a},"rgb-gray":function(t,e,i){return[.2989*t+.587*e+.114*i]},"gray-rgb":function(t){return[t,t,t]},"gray-hsb":function(t){return[0,0,t]},"gray-hsl":function(t){return[0,0,t]},"gradient-rgb":function(){return[]},"rgb-gradient":function(){return[]}};return e.each(n,function(t,i){r[i]=[],e.each(t,function(t,s){var a=e.capitalize(t),o=/^(hue|saturation)$/.test(t),u=r[i][s]="gradient"===t?function(t){var e=this._components[0];return t=D.read(Array.isArray(t)?t:arguments,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"gradient"===i?function(){return h.read(arguments,0,{readNull:"highlight"===t,clone:!0})}:function(t){return null==t||isNaN(t)?0:t};this["get"+a]=function(){return this._type===i||o&&/^hs[bl]$/.test(this._type)?this._components[s]:this._convert(i)[s]},this["set"+a]=function(t){this._type===i||o&&/^hs[bl]$/.test(this._type)||(this._components=this._convert(i),this._properties=n[i],this._type=i),this._components[s]=u.call(this,t),this._changed()}},this)},{_class:"Color",_readIndex:!0,initialize:function c(e){var i,s,a,h,u=Array.prototype.slice,l=arguments,d=0;Array.isArray(e)&&(l=e,e=l[0]);var _=null!=e&&typeof e;if("string"===_&&e in n&&(i=e,e=l[1],Array.isArray(e)?(s=e,a=l[2]):(this.__read&&(d=1),l=u.call(l,1),_=typeof e)),!s){if(h="number"===_?l:"object"===_&&null!=e.length?e:null){i||(i=h.length>=3?"rgb":"gray");var f=n[i].length;a=h[f],this.__read&&(d+=h===arguments?f+(null!=a?1:0):1),h.length>f&&(h=u.call(h,0,f))}else if("string"===_)i="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===_)if(e.constructor===c){if(i=e._type,s=e._components.slice(),a=e._alpha,"gradient"===i)for(var g=1,v=s.length;v>g;g++){var p=s[g];p&&(s[g]=p.clone())}}else if(e.constructor===D)i="gradient",h=l;else{i="hue"in e?"lightness"in e?"hsl":"hsb":"gradient"in e||"stops"in e||"radial"in e?"gradient":"gray"in e?"gray":"rgb";var m=n[i];w=r[i],this._components=s=[];for(var g=0,v=m.length;v>g;g++){var y=e[m[g]];null==y&&0===g&&"gradient"===i&&"stops"in e&&(y={stops:e.stops,radial:e.radial}),y=w[g].call(this,y),null!=y&&(s[g]=y)}a=e.alpha}this.__read&&i&&(d=1)}if(this._type=i||"rgb",this._id=o.get(c),!s){this._components=s=[];for(var w=r[this._type],g=0,v=w.length;v>g;g++){var y=w[g].call(this,h&&h[g]);null!=y&&(s[g]=y)}}this._components=s,this._properties=n[this._type],this._alpha=a,this.__read&&(this.__read=d)},_serialize:function(t,i){var n=this.getComponents();return e.serialize(/^(gray|rgb)$/.test(this._type)?n:[this._type].concat(n),t,!0,i)},_changed:function(){this._canvasStyle=null,this._owner&&this._owner._changed(65)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=l[this._type+"-"+t])?e.apply(this,this._components):l["rgb-"+t].apply(this,l[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new B(t,this._convert(t),this._alpha)},getType:function(){return this._type},setType:function(t){this._components=this._convert(t),this._properties=n[t],this._type=t},getComponents:function(){var t=this._components.slice();return null!=this._alpha&&t.push(this._alpha),t},getAlpha:function(){return null!=this._alpha?this._alpha:1},setAlpha:function(t){this._alpha=null==t?null:Math.min(Math.max(t,0),1),this._changed()},hasAlpha:function(){return null!=this._alpha},equals:function(t){var i=e.isPlainValue(t,!0)?B.read(arguments):t;return i===this||i&&this._class===i._class&&this._type===i._type&&this._alpha===i._alpha&&e.equals(this._components,i._components)||!1},toString:function(){for(var t=this._properties,e=[],i="gradient"===this._type,n=s.instance,r=0,a=t.length;a>r;r++){var o=this._components[r];null!=o&&e.push(t[r]+": "+(i?o:n.number(o)))}return null!=this._alpha&&e.push("alpha: "+n.number(this._alpha)),"{ "+e.join(", ")+" }"},toCSS:function(t){function e(t){return Math.round(255*(0>t?0:t>1?1:t))}var i=this._convert("rgb"),n=t||null==this._alpha?1:this._alpha;return i=[e(i[0]),e(i[1]),e(i[2])],1>n&&i.push(0>n?0:n),t?"#"+((1<<24)+(i[0]<<16)+(i[1]<<8)+i[2]).toString(16).slice(1):(4==i.length?"rgba(":"rgb(")+i.join(",")+")"},toCanvasStyle:function(t){if(this._canvasStyle)return this._canvasStyle;if("gradient"!==this._type)return this._canvasStyle=this.toCSS();var e,i=this._components,n=i[0],r=n._stops,s=i[1],a=i[2];if(n._radial){var o=a.getDistance(s),h=i[3];if(h){var u=h.subtract(s);u.getLength()>o&&(h=s.add(u.normalize(o-.1)))}var l=h||s;e=t.createRadialGradient(l.x,l.y,0,s.x,s.y,o)}else e=t.createLinearGradient(s.x,s.y,a.x,a.y);for(var c=0,d=r.length;d>c;c++){var _=r[c];e.addColorStop(_._rampPoint,_._color.toCanvasStyle())}return this._canvasStyle=e},transform:function(t){if("gradient"===this._type){for(var e=this._components,i=1,n=e.length;n>i;i++){var r=e[i];t._transformPoint(r,r,!0)}this._changed()}},statics:{_types:n,random:function(){var t=Math.random;return new B(t(),t(),t())}}})},new function(){var t={add:function(t,e){return t+e},subtract:function(t,e){return t-e},multiply:function(t,e){return t*e},divide:function(t,e){return t/e}};return e.each(t,function(t,e){this[e]=function(e){e=B.read(arguments);for(var i=this._type,n=this._components,r=e._convert(i),s=0,a=n.length;a>s;s++)r[s]=t(n[s],r[s]);return new B(i,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(B._types,function(t,i){var n=this[e.capitalize(i)+"Color"]=function(t){var e=null!=t&&typeof t,n="object"===e&&null!=t.length?t:"string"===e?null:arguments;return n?new B(i,n):new B(t)};if(3==i.length){var r=i.toUpperCase();B[r]=this[r+"Color"]=n}},e.exports);var D=e.extend({_class:"Gradient",initialize:function(t,e){this._id=o.get(),t&&this._set(t)&&(t=e=null),this._stops||this.setStops(t||["white","black"]),null==this._radial&&this.setRadial("string"==typeof e&&"radial"===e||e||!1)},_serialize:function(t,i){return i.add(this,function(){return e.serialize([this._stops,this._radial],t,!0,i)})},_changed:function(){for(var t=0,e=this._owners&&this._owners.length;e>t;t++)this._owners[t]._changed()},_addOwner:function(t){this._owners||(this._owners=[]),this._owners.push(t)},_removeOwner:function(e){var i=this._owners?this._owners.indexOf(e):-1;-1!=i&&(this._owners.splice(i,1),0===this._owners.length&&(this._owners=t))},clone:function(){for(var t=[],e=0,i=this._stops.length;i>e;e++)t[e]=this._stops[e].clone();return new D(t,this._radial)},getStops:function(){return this._stops},setStops:function(e){if(this.stops)for(var i=0,n=this._stops.length;n>i;i++)this._stops[i]._owner=t;if(e.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=E.readAll(e,0,{clone:!0});for(var i=0,n=this._stops.length;n>i;i++){var r=this._stops[i];r._owner=this,r._defaultRamp&&r.setRampPoint(i/(n-1))}this._changed()},getRadial:function(){return this._radial},setRadial:function(t){this._radial=t,this._changed()},equals:function(t){if(t===this)return!0;if(t&&this._class===t._class&&this._stops.length===t._stops.length){for(var e=0,i=this._stops.length;i>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),E=e.extend({_class:"GradientStop",initialize:function(e,i){if(e){var n,r;i===t&&Array.isArray(e)?(n=e[0],r=e[1]):e.color?(n=e.color,r=e.rampPoint):(n=e,r=i),this.setColor(n),this.setRampPoint(r)}},clone:function(){return new E(this._color.clone(),this._rampPoint)},_serialize:function(t,i){return e.serialize([this._color,this._rampPoint],t,!0,i)},_changed:function(){this._owner&&this._owner._changed(65)},getRampPoint:function(){return this._rampPoint},setRampPoint:function(t){this._defaultRamp=null==t,this._rampPoint=t||0,this._changed()},getColor:function(){return this._color},setColor:function(t){this._color=B.read(arguments),this._color===t&&(this._color=t.clone()),this._color._owner=this,this._changed()},equals:function(t){return t===this||t&&this._class===t._class&&this._color.equals(t._color)&&this._rampPoint==t._rampPoint||!1}}),R=e.extend(new function(){var i={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",strokeScaling:!0,miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,fontFamily:"sans-serif",fontWeight:"normal",fontSize:12,font:"sans-serif",leading:null,justification:"left"},n={strokeWidth:97,strokeCap:97,strokeJoin:97,strokeScaling:105,miterLimit:97,fontFamily:9,fontWeight:9,fontSize:9,font:9,leading:9,justification:9},r={beans:!0},s={_defaults:i,_textDefaults:new e(i,{fillColor:new B}),beans:!0};return e.each(i,function(i,a){var o=/Color$/.test(a),u="shadowOffset"===a,l=e.capitalize(a),c=n[a],d="set"+l,_="get"+l;s[d]=function(e){var i=this._owner,n=i&&i._children;if(n&&n.length>0&&!(i instanceof A))for(var r=0,s=n.length;s>r;r++)n[r]._style[d](e);else{var h=this._values[a];h!==e&&(o&&(h&&(h._owner=t),e&&e.constructor===B&&(e._owner&&(e=e.clone()),e._owner=i)),this._values[a]=e,i&&i._changed(c||65))}},s[_]=function(i){var n,r=this._owner,s=r&&r._children;if(!s||0===s.length||i||r instanceof A){var n=this._values[a];if(n===t)n=this._defaults[a],n&&n.clone&&(n=n.clone());else{var l=o?B:u?h:null;!l||n&&n.constructor===l||(this._values[a]=n=l.read([n],0,{readNull:!0,clone:!0}),n&&o&&(n._owner=r))}return n}for(var c=0,d=s.length;d>c;c++){var f=s[c]._style[_]();if(0===c)n=f;else if(!e.equals(n,f))return t}return n},r[_]=function(t){return this._style[_](t)},r[d]=function(t){this._style[d](t)}}),m.inject(r),s},{_class:"Style",initialize:function(t,e,i){this._values={},this._owner=e,this._project=e&&e._project||i||paper.project,e instanceof N&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof R,i=e?t._values:t;if(i)for(var n in i)if(n in this._defaults){var r=i[n];this[n]=r&&e&&r.clone?r.clone():r}},equals:function(t){return t===this||t&&this._class===t._class&&e.equals(this._values,t._values)||!1},hasFill:function(){return!!this.getFillColor()},hasStroke:function(){return!!this.getStrokeColor()&&this.getStrokeWidth()>0},hasShadow:function(){return!!this.getShadowColor()&&this.getShadowBlur()>0},getView:function(){return this._project.getView()},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function wt(){var t=wt.base.call(this),e=this.getFontSize();return/pt|em|%|px/.test(e)&&(e=this.getView().getPixelSize(e)),null!=t?t:1.2*e}}),F=new function(){function t(t,e,i,n){for(var r=["","webkit","moz","Moz","ms","o"],s=e[0].toUpperCase()+e.substring(1),a=0;6>a;a++){var o=r[a],h=o?o+s:e;if(h in t){if(!i)return t[h];t[h]=n;break}}}return{getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,i=e&&e.defaultView;return i&&i.getComputedStyle(t,"")},getBounds:function(t,e){var i,n=t.ownerDocument,r=n.body,s=n.documentElement;try{i=t.getBoundingClientRect()}catch(a){i={left:0,top:0,width:0,height:0}}var o=i.left-(s.clientLeft||r.clientLeft||0),h=i.top-(s.clientTop||r.clientTop||0);if(!e){var u=n.defaultView;o+=u.pageXOffset||s.scrollLeft||r.scrollLeft,h+=u.pageYOffset||s.scrollTop||r.scrollTop}return new d(o,h,i.width,i.height)},getViewportBounds:function(t){var e=t.ownerDocument,i=e.defaultView,n=e.documentElement;return new d(0,0,i.innerWidth||n.clientWidth,i.innerHeight||n.clientHeight)},getOffset:function(t,e){return F.getBounds(t,e).getPoint()},getSize:function(t){return F.getBounds(t,!0).getSize()},isInvisible:function(t){return F.getSize(t).equals(new l(0,0))},isInView:function(t){return!F.isInvisible(t)&&F.getViewportBounds(t).intersects(F.getBounds(t,!0))},getPrefixed:function(e,i){return t(e,i)},setPrefixed:function(e,i,n){if("object"==typeof i)for(var r in i)t(e,r,!0,i[r]);else t(e,i,!0,n)}}},q={add:function(t,e){for(var i in e)for(var n=e[i],r=i.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.addEventListener(r[s],n,!1)},remove:function(t,e){for(var i in e)for(var n=e[i],r=i.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.removeEventListener(r[s],n,!1)},getPoint:function(t){var e=t.targetTouches?t.targetTouches.length?t.targetTouches[0]:t.changedTouches[0]:t;return new h(e.pageX||e.clientX+document.documentElement.scrollLeft,e.pageY||e.clientY+document.documentElement.scrollTop)},getTarget:function(t){return t.target||t.srcElement},getRelatedTarget:function(t){return t.relatedTarget||t.toElement},getOffset:function(t,e){return q.getPoint(t).subtract(F.getOffset(e||q.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};q.requestAnimationFrame=new function(){function t(){for(var e=s.length-1;e>=0;e--){var o=s[e],h=o[0],u=o[1];(!u||("true"==n.getAttribute(u,"keepalive")||a)&&F.isInView(u))&&(s.splice(e,1),h())}i&&(s.length?i(t):r=!1)}var e,i=F.getPrefixed(window,"requestAnimationFrame"),r=!1,s=[],a=!0;return q.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(n,a){s.push([n,a]),i?r||(i(t),r=!0):e||(e=setInterval(t,1e3/60))}};var V=e.extend(i,{_class:"View",initialize:function xt(t,e){function i(t){return e[t]||parseInt(e.getAttribute(t),10)}function r(){var t=F.getSize(e);return t.isNaN()||t.isZero()?new l(i("width"),i("height")):t}this._project=t,this._scope=t._scope,this._element=e;var s;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=e.getAttribute("id"),null==this._id&&e.setAttribute("id",this._id="view-"+xt._id++),q.add(e,this._viewEvents);var a="none";if(F.setPrefixed(e.style,{userSelect:a,touchAction:a,touchCallout:a,contentZooming:a,userDrag:a,tapHighlightColor:"rgba(0,0,0,0)"}),n.hasAttribute(e,"resize")){var o=this;q.add(window,this._windowEvents={resize:function(){o.setViewSize(r())}})}if(this._setViewSize(s=r()),n.hasAttribute(e,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var h=this._stats.domElement,u=h.style,c=F.getOffset(e);u.position="absolute",u.left=c.x+"px",u.top=c.y+"px",document.body.appendChild(h)}xt._views.push(this),xt._viewsById[this._id]=this,this._viewSize=s,(this._matrix=new f)._owner=this,this._zoom=1,xt._focused||(xt._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(V._focused===this&&(V._focused=null),V._views.splice(V._views.indexOf(this),1),delete V._viewsById[this._id],this._project._view===this&&(this._project._view=null),q.remove(this._element,this._viewEvents),q.remove(window,this._windowEvents),this._element=this._project=null,this.off("frame"),this._animate=!1,this._frameItems={},!0):!1},_events:{onFrame:{install:function(){this.play()},uninstall:function(){this.pause()}},onResize:{}},_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;q.requestAnimationFrame(function(){t._requested=!1,t._animate&&(t._requestFrame(),t._handleFrame())},this._element),this._requested=!0},_handleFrame:function(){paper=this._scope;var t=Date.now()/1e3,i=this._before?t-this._before:0;this._before=t,this._handlingFrame=!0,this.emit("frame",new e({delta:i,time:this._time+=i,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.update()},_animateItem:function(t,e){var i=this._frameItems;e?(i[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.on("frame",this._handleFrameItems)):(delete i[t._id],0===--this._frameItemCount&&this.off("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var i in this._frameItems){var n=this._frameItems[i];n.item.emit("frame",new e(t,{time:n.time+=t.delta,count:n.count++}))}},_update:function(){this._project._needsUpdate=!0,this._handlingFrame||(this._animate?this._handleFrame():this.update())},_changed:function(t){1&t&&(this._project._needsUpdate=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._update()},getElement:function(){return this._element},getPixelRatio:function(){return this._pixelRatio},getResolution:function(){return 72*this._pixelRatio},getViewSize:function(){var t=this._viewSize;return new c(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=l.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.emit("resize",{size:t,delta:e}),this._update())},_setViewSize:function(t){var e=this._element;e.width=t.width,e.height=t.height},getBounds:function(){return this._bounds||(this._bounds=this._matrix.inverted()._transformBounds(new d(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(){var t=h.read(arguments);this.scrollBy(t.subtract(this.getCenter()))},getZoom:function(){return this._zoom},setZoom:function(t){this._transform((new f).scale(t/this._zoom,this.getCenter())),this._zoom=t},isVisible:function(){return F.isInView(this._element)},scrollBy:function(){this._transform((new f).translate(h.read(arguments).negate()))},play:function(){this._animate=!0,this._requested||this._requestFrame()},pause:function(){this._animate=!1},draw:function(){this.update()},projectToView:function(){return this._matrix._transformPoint(h.read(arguments))},viewToProject:function(){return this._matrix._inverseTransform(h.read(arguments))}},{statics:{_views:[],_viewsById:{},_id:0,create:function(t,e){return"string"==typeof e&&(e=document.getElementById(e)),new Z(t,e)}}},new function(){function t(t){var e=q.getTarget(t);return e.getAttribute&&V._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(q.getOffset(e,t._element))}function i(){if(!V._focused||!V._focused.isVisible())for(var t=0,e=V._views.length;e>t;t++){var i=V._views[t];if(i&&i.isVisible()){V._focused=a=i;break}}}function n(t,e,i){t._handleEvent("mousemove",e,i);var n=t._scope.tool;return n&&n._handleEvent(l&&n.responds("mousedrag")?"mousedrag":"mousemove",e,i),t.update(),n}var r,s,a,o,h,u,l=!1,c=window.navigator;c.pointerEnabled||c.msPointerEnabled?(o="pointerdown MSPointerDown",h="pointermove MSPointerMove",u="pointerup pointercancel MSPointerUp MSPointerCancel"):(o="touchstart",h="touchmove",u="touchend touchcancel","ontouchstart"in window&&c.userAgent.match(/mobile|tablet|ip(ad|hone|od)|android|silk/i)||(o+=" mousedown",h+=" mousemove",u+=" mouseup"));var d={"selectstart dragstart":function(t){l&&t.preventDefault()}},_={mouseout:function(t){var i=V._focused,r=q.getRelatedTarget(t);!i||r&&"HTML"!==r.nodeName||n(i,e(i,t),t)},scroll:i};return d[o]=function(i){var n=V._focused=t(i),s=e(n,i);l=!0,n._handleEvent("mousedown",s,i),(r=n._scope.tool)&&r._handleEvent("mousedown",s,i),n.update()},_[h]=function(o){var h=V._focused;if(!l){var u=t(o);u?(h!==u&&n(h,e(h,o),o),s=h,h=V._focused=a=u):a&&a===h&&(h=V._focused=s,i())}if(h){var c=e(h,o);(l||h.getBounds().contains(c))&&(r=n(h,c,o))}},_[u]=function(t){var i=V._focused;if(i&&l){var n=e(i,t);l=!1,i._handleEvent("mouseup",n,t),r&&r._handleEvent("mouseup",n,t),i.update()}},q.add(document,_),q.add(window,{load:i}),{_viewEvents:d,_handleEvent:function(){},statics:{updateFocus:i}}}),Z=V.extend({_class:"CanvasView",initialize:function(t,e){if(!(e instanceof HTMLCanvasElement)){var i=l.read(arguments,1);if(i.isZero())throw Error("Cannot create CanvasView with the provided argument: "+[].slice.call(arguments,1));e=Y.getCanvas(i)}if(this._context=e.getContext("2d"),this._eventCounters={},this._pixelRatio=1,!/^off|false$/.test(n.getAttribute(e,"hidpi"))){var r=window.devicePixelRatio||1,s=F.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=r/s}V.call(this,t,e)},_setViewSize:function(t){var e=this._element,i=this._pixelRatio,r=t.width,s=t.height;if(e.width=r*i,e.height=s*i,1!==i){if(!n.hasAttribute(e,"resize")){var a=e.style;a.width=r+"px",a.height=s+"px"}this._context.scale(i,i)}},getPixelSize:function(t){var e,i=paper.browser;if(i&&i.firefox){var n=this._element.parentNode,r=document.createElement("div");r.style.fontSize=t,n.appendChild(r),e=parseFloat(F.getStyles(r).fontSize),n.removeChild(r)}else{var s=this._context,a=s.font;s.font=t+" serif",e=parseFloat(s.font),s.font=a}return e},getTextWidth:function(t,e){var i=this._context,n=i.font,r=0;i.font=t;for(var s=0,a=e.length;a>s;s++)r=Math.max(r,i.measureText(e[s]).width);return i.font=n,r},update:function(t){var e=this._project;if(!e||!t&&!e._needsUpdate)return!1;var i=this._context,n=this._viewSize;return i.clearRect(0,0,n.width+1,n.height+1),e.draw(i,this._matrix,this._pixelRatio),e._needsUpdate=!1,!0}},new function(){function e(e,i,n,r,s,a){function o(e){return e.responds(i)&&(h||(h=new U(i,n,r,s,a?r.subtract(a):null)),e.emit(i,h)&&h.isStopped)?(n.preventDefault(),!0):t}for(var h,u=s;u;){if(o(u))return!0;u=u.getParent()}return o(e)?!0:!1}var i,n,r,s,a,o,h,u,l;return{_handleEvent:function(t,c,d){if(this._eventCounters[t]){var _=this._project,f=_.hitTest(c,{tolerance:0,fill:!0,stroke:!0}),g=f&&f.item,v=!1;switch(t){case"mousedown":for(v=e(this,t,d,c,g),u=a==g&&Date.now()-l<300,s=a=g,i=n=r=c,h=!v&&g;h&&!h.responds("mousedrag");)h=h._parent;break;case"mouseup":v=e(this,t,d,c,g,i),h&&(n&&!n.equals(c)&&e(this,"mousedrag",d,c,h,n),g!==h&&(r=c,e(this,"mousemove",d,c,g,r))),!v&&g&&g===s&&(l=Date.now(),e(this,u&&s.responds("doubleclick")?"doubleclick":"click",d,i,g),u=!1),s=h=null;break;case"mousemove":h&&(v=e(this,"mousedrag",d,c,h,n)),v||(g!==o&&(r=c),v=e(this,t,d,c,g,r)),n=r=c,g!==o&&(e(this,"mouseleave",d,c,o),o=g,e(this,"mouseenter",d,c,g))}return v}}}}),H=e.extend({_class:"Event",initialize:function(t){this.event=t},isPrevented:!1,isStopped:!1,preventDefault:function(){this.isPrevented=!0,this.event.preventDefault()},stopPropagation:function(){this.isStopped=!0,this.event.stopPropagation()},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return G.modifiers}}),W=H.extend({_class:"KeyEvent",initialize:function(t,e,i,n){H.call(this,n),this.type=t?"keydown":"keyup",this.key=e,this.character=i},toString:function(){return"{ type: '"+this.type+"', key: '"+this.key+"', character: '"+this.character+"', modifiers: "+this.getModifiers()+" }"}}),G=new function(){function t(n,s,u,l){var c,d=u?String.fromCharCode(u):"",_=r[s],f=_||d.toLowerCase(),g=n?"keydown":"keyup",v=V._focused,p=v&&v.isVisible()&&v._scope,m=p&&p.tool;if(h[f]=n,n?o[s]=u:delete o[s],_&&(c=e.camelize(_))in a){a[c]=n;var y=paper.browser;if("command"===c&&y&&y.mac)if(n)i={};else{for(var w in i)w in o&&t(!1,w,i[w],l);i=null}}else n&&i&&(i[s]=u);m&&m.responds(g)&&(paper=p,m.emit(g,new W(n,f,d,l)), -v&&v.update())}var i,n,r={8:"backspace",9:"tab",13:"enter",16:"shift",17:"control",18:"option",19:"pause",20:"caps-lock",27:"escape",32:"space",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",46:"delete",91:"command",93:"command",224:"command"},s={9:!0,13:!0,32:!0},a=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),o={},h={};return q.add(document,{keydown:function(e){var i=e.which||e.keyCode;i in r||a.command?t(!0,i,i in s||a.command?i:0,e):n=i},keypress:function(e){null!=n&&(t(!0,n,e.which||e.keyCode,e),n=null)},keyup:function(e){var i=e.which||e.keyCode;i in o&&t(!1,i,o[i],e)}}),q.add(window,{blur:function(e){for(var i in o)t(!1,i,o[i],e)}}),{modifiers:a,isDown:function(t){return!!h[t]}}},U=H.extend({_class:"MouseEvent",initialize:function(t,e,i,n,r){H.call(this,e),this.type=t,this.point=i,this.target=n,this.delta=r},toString:function(){return"{ type: '"+this.type+"', point: "+this.point+", target: "+this.target+(this.delta?", delta: "+this.delta:"")+", modifiers: "+this.getModifiers()+" }"}}),J=H.extend({_class:"ToolEvent",_item:null,initialize:function(t,e,i){this.tool=t,this.type=e,this.event=i},_choosePoint:function(t,e){return t?t:e?e.clone():null},getPoint:function(){return this._choosePoint(this._point,this.tool._point)},setPoint:function(t){this._point=t},getLastPoint:function(){return this._choosePoint(this._lastPoint,this.tool._lastPoint)},setLastPoint:function(t){this._lastPoint=t},getDownPoint:function(){return this._choosePoint(this._downPoint,this.tool._downPoint)},setDownPoint:function(t){this._downPoint=t},getMiddlePoint:function(){return!this._middlePoint&&this.tool._lastPoint?this.tool._point.add(this.tool._lastPoint).divide(2):this._middlePoint},setMiddlePoint:function(t){this._middlePoint=t},getDelta:function(){return!this._delta&&this.tool._lastPoint?this.tool._point.subtract(this.tool._lastPoint):this._delta},setDelta:function(t){this._delta=t},getCount:function(){return/^mouse(down|up)$/.test(this.type)?this.tool._downCount:this.tool._count},setCount:function(t){this.tool[/^mouse(down|up)$/.test(this.type)?"downCount":"count"]=t},getItem:function(){if(!this._item){var t=this.tool._scope.project.hitTest(this.getPoint());if(t){for(var e=t.item,i=e._parent;/^(Group|CompoundPath)$/.test(i._class);)e=i,i=i._parent;this._item=e}}return this._item},setItem:function(t){this._item=t},toString:function(){return"{ type: "+this.type+", point: "+this.getPoint()+", count: "+this.getCount()+", modifiers: "+this.getModifiers()+" }"}}),X=(r.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){r.call(this),this._firstMove=!0,this._count=0,this._downCount=0,this._set(t)},getMinDistance:function(){return this._minDistance},setMinDistance:function(t){this._minDistance=t,null!=t&&null!=this._maxDistance&&t>this._maxDistance&&(this._maxDistance=t)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=t&&tu)return!1;if(null!=n&&0!=n)if(u>n)e=this._point.add(h.normalize(n));else if(a)return!1}if(s&&e.equals(this._point))return!1}switch(this._lastPoint=r&&"mousemove"==t?e:this._point,this._point=e,t){case"mousedown":this._lastPoint=this._downPoint,this._downPoint=this._point,this._downCount++;break;case"mouseup":this._lastPoint=this._downPoint}return this._count=r?0:this._count+1,!0},_fireEvent:function(t,e){var i=paper.project._removeSets;if(i){"mouseup"===t&&(i.mousedrag=null);var n=i[t];if(n){for(var r in n){var s=n[r];for(var a in i){var o=i[a];o&&o!=n&&delete o[s._id]}s.remove()}i[t]=null}}return this.responds(t)&&this.emit(t,new J(this,t,e))},_handleEvent:function(t,e,i){paper=this._scope;var n=!1;switch(t){case"mousedown":this._updateEvent(t,e,null,null,!0,!1,!1),n=this._fireEvent(t,i);break;case"mousedrag":for(var r=!1,s=!1;this._updateEvent(t,e,this.minDistance,this.maxDistance,!1,r,s);)n=this._fireEvent(t,i)||n,r=!0,s=!0;break;case"mouseup":!e.equals(this._point)&&this._updateEvent("mousedrag",e,this.minDistance,this.maxDistance,!1,!1,!1)&&(n=this._fireEvent("mousedrag",i)),this._updateEvent(t,e,null,this.maxDistance,!1,!1,!1),n=this._fireEvent(t,i)||n,this._updateEvent(t,e,null,null,!0,!1,!1),this._firstMove=!0;break;case"mousemove":for(;this._updateEvent(t,e,this.minDistance,this.maxDistance,this._firstMove,!0,!1);)n=this._fireEvent(t,i)||n,this._firstMove=!1}return n&&i.preventDefault(),n}}),{request:function(e,i,n,r){r=r===t?!0:r;var s=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return s.open(e.toUpperCase(),i,r),"overrideMimeType"in s&&s.overrideMimeType("text/plain"),s.onreadystatechange=function(){if(4===s.readyState){var t=s.status;if(0!==t&&200!==t)throw Error("Could not load "+i+" (Error "+t+")");n.call(s,s.responseText)}},s.send(null)}}),Y={canvases:[],getCanvas:function(t,e){var i,n=!0;"object"==typeof t&&(e=t.height,t=t.width),i=this.canvases.length?this.canvases.pop():document.createElement("canvas");var r=i.getContext("2d");return i.width===t&&i.height===e?n&&r.clearRect(0,0,t+1,e+1):(i.width=t,i.height=e),r.save(),i},getContext:function(t,e){return this.getCanvas(t,e).getContext("2d")},release:function(t){var e=t.canvas?t.canvas:t;e.getContext("2d").restore(),this.canvases.push(e)}},$=new function(){function t(t,e,i){return.2989*t+.587*e+.114*i}function i(e,i,n,r){var s=r-t(e,i,n);_=e+s,f=i+s,g=n+s;var r=t(_,f,g),a=v(_,f,g),o=p(_,f,g);if(0>a){var h=r-a;_=r+(_-r)*r/h,f=r+(f-r)*r/h,g=r+(g-r)*r/h}if(o>255){var u=255-r,l=o-r;_=r+(_-r)*u/l,f=r+(f-r)*u/l,g=r+(g-r)*u/l}}function n(t,e,i){return p(t,e,i)-v(t,e,i)}function r(t,e,i,n){var r,s=[t,e,i],a=p(t,e,i),o=v(t,e,i);o=o===t?0:o===e?1:2,a=a===t?0:a===e?1:2,r=0===v(o,a)?1===p(o,a)?2:1:0,s[a]>s[o]?(s[r]=(s[r]-s[o])*n/(s[a]-s[o]),s[a]=n):s[r]=s[a]=0,s[o]=0,_=s[0],f=s[1],g=s[2]}var s,a,o,h,u,l,c,d,_,f,g,v=Math.min,p=Math.max,m=Math.abs,y={multiply:function(){_=u*s/255,f=l*a/255,g=c*o/255},screen:function(){_=u+s-u*s/255,f=l+a-l*a/255,g=c+o-c*o/255},overlay:function(){_=128>u?2*u*s/255:255-2*(255-u)*(255-s)/255,f=128>l?2*l*a/255:255-2*(255-l)*(255-a)/255,g=128>c?2*c*o/255:255-2*(255-c)*(255-o)/255},"soft-light":function(){var t=s*u/255;_=t+u*(255-(255-u)*(255-s)/255-t)/255,t=a*l/255,f=t+l*(255-(255-l)*(255-a)/255-t)/255,t=o*c/255,g=t+c*(255-(255-c)*(255-o)/255-t)/255},"hard-light":function(){_=128>s?2*s*u/255:255-2*(255-s)*(255-u)/255,f=128>a?2*a*l/255:255-2*(255-a)*(255-l)/255,g=128>o?2*o*c/255:255-2*(255-o)*(255-c)/255},"color-dodge":function(){_=0===u?0:255===s?255:v(255,255*u/(255-s)),f=0===l?0:255===a?255:v(255,255*l/(255-a)),g=0===c?0:255===o?255:v(255,255*c/(255-o))},"color-burn":function(){_=255===u?255:0===s?0:p(0,255-255*(255-u)/s),f=255===l?255:0===a?0:p(0,255-255*(255-l)/a),g=255===c?255:0===o?0:p(0,255-255*(255-c)/o)},darken:function(){_=s>u?u:s,f=a>l?l:a,g=o>c?c:o},lighten:function(){_=u>s?u:s,f=l>a?l:a,g=c>o?c:o},difference:function(){_=u-s,0>_&&(_=-_),f=l-a,0>f&&(f=-f),g=c-o,0>g&&(g=-g)},exclusion:function(){_=u+s*(255-u-u)/255,f=l+a*(255-l-l)/255,g=c+o*(255-c-c)/255},hue:function(){r(s,a,o,n(u,l,c)),i(_,f,g,t(u,l,c))},saturation:function(){r(u,l,c,n(s,a,o)),i(_,f,g,t(u,l,c))},luminosity:function(){i(u,l,c,t(s,a,o))},color:function(){i(s,a,o,t(u,l,c))},add:function(){_=v(u+s,255),f=v(l+a,255),g=v(c+o,255)},subtract:function(){_=p(u-s,0),f=p(l-a,0),g=p(c-o,0)},average:function(){_=(u+s)/2,f=(l+a)/2,g=(c+o)/2},negation:function(){_=255-m(255-s-u),f=255-m(255-a-l),g=255-m(255-o-c)}},w=this.nativeModes=e.each(["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","darker","copy","xor"],function(t){this[t]=!0},{}),x=Y.getContext(1,1);e.each(y,function(t,e){var i="darken"===e,n=!1;x.save();try{x.fillStyle=i?"#300":"#a00",x.fillRect(0,0,1,1),x.globalCompositeOperation=e,x.globalCompositeOperation===e&&(x.fillStyle=i?"#a00":"#300",x.fillRect(0,0,1,1),n=x.getImageData(0,0,1,1).data[0]!==i?170:51)}catch(r){}x.restore(),w[e]=n}),Y.release(x),this.process=function(t,e,i,n,r){var v=e.canvas,p="normal"===t;if(p||w[t])i.save(),i.setTransform(1,0,0,1,0,0),i.globalAlpha=n,p||(i.globalCompositeOperation=t),i.drawImage(v,r.x,r.y),i.restore();else{var m=y[t];if(!m)return;for(var x=i.getImageData(r.x,r.y,v.width,v.height),b=x.data,C=e.getImageData(0,0,v.width,v.height).data,S=0,P=b.length;P>S;S+=4){s=C[S],u=b[S],a=C[S+1],l=b[S+1],o=C[S+2],c=b[S+2],h=C[S+3],d=b[S+3],m();var M=h*n/255,z=1-M;b[S]=M*_+z*u,b[S+1]=M*f+z*l,b[S+2]=M*g+z*c,b[S+3]=h*n+z*d}i.putImageData(x,r.x,r.y)}}},K=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],strokeScaling:["vector-effect","lookup",{"true":"none","false":"non-scaling-stroke"},function(t,e){return!e&&(t instanceof k||t instanceof x||t instanceof N)}],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],fontFamily:["font-family","string"],fontWeight:["font-weight","string"],fontSize:["font-size","number"],justification:["text-anchor","lookup",{left:"start",center:"middle",right:"end"}],opacity:["opacity","number"],blendMode:["mix-blend-mode","string"]},function(t,i){var n=e.capitalize(i),r=t[2];this[i]={type:t[1],property:i,attribute:t[0],toSVG:r,fromSVG:r&&e.each(r,function(t,e){this[t]=e},{}),exportFilter:t[3],get:"get"+n,set:"set"+n}},{}),Q={href:"http://www.w3.org/1999/xlink",xlink:"http://www.w3.org/2000/xmlns"};return new function(){function t(t,e){for(var i in e){var n=e[i],r=Q[i];"number"==typeof n&&(n=b.number(n)),r?t.setAttributeNS(r,i,n):t.setAttribute(i,n)}return t}function i(e,i){return t(document.createElementNS("http://www.w3.org/2000/svg",e),i)}function n(t,i,n){var r=new e,s=t.getTranslation();if(i){t=t.shiftless();var o=t._inverseTransform(s);r[n?"cx":"x"]=o.x,r[n?"cy":"y"]=o.y,s=null}if(!t.isIdentity()){var h=t.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;s&&!s.isZero()&&u.push("translate("+b.point(s)+")"),a.isZero(c.x-1)&&a.isZero(c.y-1)||u.push("scale("+b.point(c)+")"),l&&u.push("rotate("+b.number(l)+")"),r.transform=u.join(" ")}else r.transform="matrix("+t.getValues().join(",")+")"}return r}function r(e,r){for(var s=n(e._matrix),a=e._children,o=i("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=w(l,r);if(c)if(l.isClipMask()){var d=i("clipPath");d.appendChild(c),p(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function o(t,e){var r=n(t._matrix,!0),s=t.getSize(),a=t.getImage();return r.x-=s.width/2,r.y-=s.height/2,r.width=s.width,r.height=s.height,r.href=e.embedImages===!1&&a&&a.src||t.toDataURL(),i("image",r)}function h(t,e){var r=e.matchShapes;if(r){var s=t.toShape(!1);if(s)return u(s,e)}var a,o=t._segments,h=n(t._matrix);if(0===o.length)return null;if(r&&!t.hasHandles())if(o.length>=3){a=t._closed?"polygon":"polyline";for(var l=[],c=0,d=o.length;d>c;c++)l.push(b.point(o[c]._point));h.points=l.join(" ")}else{a="line";var _=o[0]._point,f=o[o.length-1]._point;h.set({x1:_.x,y1:_.y,x2:f.x,y2:f.y})}else a="path",h.d=t.getPathData(null,e.precision);return i(a,h)}function u(t){var e=t._type,r=t._radius,s=n(t._matrix,!0,"rectangle"!==e);if("rectangle"===e){e="rect";var a=t._size,o=a.width,h=a.height;s.x-=o/2,s.y-=h/2,s.width=o,s.height=h,r.isZero()&&(r=null)}return r&&("circle"===e?s.r=r:(s.rx=r.width,s.ry=r.height)),i(e,s)}function l(t,e){var r=n(t._matrix),s=t.getPathData(null,e.precision);return s&&(r.d=s),i("path",r)}function c(t,e){var r=n(t._matrix,!0),s=t.getSymbol(),a=g(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=i("symbol",{viewBox:b.rectangle(h)}),a.appendChild(w(o,e)),p(s,a,"symbol")),r.href="#"+a.id,r.x+=h.x,r.y+=h.y,r.width=b.number(h.width),r.height=b.number(h.height),r.overflow="visible",i("use",r)}function d(t){var e=g(t,"color");if(!e){var n,r=t.getGradient(),s=r._radial,a=t.getOrigin().transform(),o=t.getDestination().transform();if(s){n={cx:a.x,cy:a.y,r:a.getDistance(o)};var h=t.getHighlight();h&&(h=h.transform(),n.fx=h.x,n.fy=h.y)}else n={x1:a.x,y1:a.y,x2:o.x,y2:o.y};n.gradientUnits="userSpaceOnUse",e=i((s?"radial":"linear")+"Gradient",n);for(var u=r._stops,l=0,c=u.length;c>l;l++){var d=u[l],_=d._color,f=_.getAlpha();n={offset:d._rampPoint,"stop-color":_.toCSS(!0)},1>f&&(n["stop-opacity"]=f),e.appendChild(i("stop",n))}p(t,e,"color")}return"url(#"+e.id+")"}function _(t){var e=i("text",n(t._matrix,!0));return e.textContent=t._content,e}function f(i,n,r){var s={},a=!r&&i.getParent();return null!=i._name&&(s.id=i._name),e.each(K,function(t){var n=t.get,r=t.type,o=i[n]();if(t.exportFilter?t.exportFilter(i,o):!a||!e.equals(a[n](),o)){if("color"===r&&null!=o){var h=o.getAlpha();1>h&&(s[t.attribute+"-opacity"]=h)}s[t.attribute]=null==o?"none":"number"===r?b.number(o):"color"===r?o.gradient?d(o,i):o.toCSS(!0):"array"===r?o.join(","):"lookup"===r?t.toSVG[o]:o}}),1===s.opacity&&delete s.opacity,i._visible||(s.visibility="hidden"),t(n,s)}function g(t,e){return C||(C={ids:{},svgs:{}}),t&&C.svgs[e+"-"+t._id]}function p(t,e,i){C||g();var n=C.ids[i]=(C.ids[i]||0)+1;e.id=i+"-"+n,C.svgs[i+"-"+t._id]=e}function y(t,e){var n=t,r=null;if(C){n="svg"===t.nodeName.toLowerCase()&&t;for(var s in C.svgs)r||(n||(n=i("svg"),n.appendChild(t)),r=n.insertBefore(i("defs"),n.firstChild)),r.appendChild(C.svgs[s]);C=null}return e.asString?(new XMLSerializer).serializeToString(n):n}function w(t,e,i){var n=S[t._class],r=n&&n(t,e);if(r){var s=e.onExport;s&&(r=s(t,r,e)||r);var a=JSON.stringify(t._data);a&&"{}"!==a&&"null"!==a&&r.setAttribute("data-paper-data",a)}return r&&f(t,r,i)}function x(t){return t||(t={}),b=new s(t.precision),t}var b,C,S={Group:r,Layer:r,Raster:o,Path:h,Shape:u,CompoundPath:l,PlacedSymbol:c,PointText:_};m.inject({exportSVG:function(t){return t=x(t),y(w(this,t,!0),t)}}),v.inject({exportSVG:function(t){t=x(t);var e=this.layers,r=this.getView(),s=r.getViewSize(),a=i("svg",{x:0,y:0,width:s.width,height:s.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),o=a,h=r._matrix;h.isIdentity()||(o=a.appendChild(i("g",n(h))));for(var u=0,l=e.length;l>u;u++)o.appendChild(w(e[u],t,!0));return y(a,t)}})},new function(){function i(t,e,i,n){var r=Q[e],s=r?t.getAttributeNS(r,e):t.getAttribute(e);return"null"===s&&(s=null),null==s?n?null:i?"":0:i?s:parseFloat(s)}function n(t,e,n,r){return e=i(t,e,!1,r),n=i(t,n,!1,r),!r||null!=e&&null!=n?new h(e,n):null}function r(t,e,n,r){return e=i(t,e,!1,r),n=i(t,n,!1,r),!r||null!=e&&null!=n?new l(e,n):null}function s(t,e,i){return"none"===t?null:"number"===e?parseFloat(t):"array"===e?t?t.split(/[\s,]+/g).map(parseFloat):[]:"color"===e?S(t)||t:"lookup"===e?i[t]:t}function a(t,e,i,n){var r=t.childNodes,s="clippath"===e,a=new y,o=a._project,h=o._currentStyle,u=[];if(s||(a=C(a,t,n),o._currentStyle=a._style.clone()),n)for(var l=t.querySelectorAll("defs"),c=0,d=l.length;d>c;c++)P(l[c],i,!1);for(var c=0,d=r.length;d>c;c++){var _,f=r[c];1!==f.nodeType||"defs"===f.nodeName.toLowerCase()||!(_=P(f,i,!1))||_ instanceof p||u.push(_)}return a.addChildren(u),s&&(a=C(a.reduce(),t,n)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){for(var i=t.getAttribute("points").match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g),n=[],r=0,s=i.length;s>r;r+=2)n.push(new h(parseFloat(i[r]),parseFloat(i[r+1])));var a=new O(n);return"polygon"===e&&a.closePath(),a}function u(t){var e=t.getAttribute("d"),i={pathData:e};return(e.match(/m/gi)||[]).length>1||/z\S+/i.test(e)?new A(i):new O(i)}function c(t,e){var r,s=(i(t,"href",!0)||"").substring(1),a="radialgradient"===e;if(s)r=I[s].getGradient();else{for(var o=t.childNodes,h=[],u=0,l=o.length;l>u;u++){var c=o[u];1===c.nodeType&&h.push(C(new E,c))}r=new D(h,a)}var d,_,f;return a?(d=n(t,"cx","cy"),_=d.add(i(t,"r"),0),f=n(t,"fx","fy",!0)):(d=n(t,"x1","y1"),_=n(t,"x2","y2")),C(new B(r,d,_,f),t),null}function _(t,e,i,n){for(var r=(n.getAttribute(i)||"").split(/\)\s*/g),s=new f,a=0,o=r.length;o>a;a++){var h=r[a];if(!h)break;for(var u=h.split(/\(\s*/),l=u[0],c=u[1].split(/[\s,]+/g),d=0,_=c.length;_>d;d++)c[d]=parseFloat(c[d]);switch(l){case"matrix":s.concatenate(new f(c[0],c[1],c[2],c[3],c[4],c[5]));break;case"rotate":s.rotate(c[0],c[1],c[2]);break;case"translate":s.translate(c[0],c[1]);break;case"scale":s.scale(c);break;case"skewX":s.skew(c[0],0);break;case"skewY":s.skew(0,c[0])}}t.transform(s)}function g(t,e,i){var n=t["fill-opacity"===i?"getFillColor":"getStrokeColor"]();n&&n.setAlpha(parseFloat(e))}function w(i,n,r){var s=i.attributes[n],a=s&&s.value;if(!a){var o=e.camelize(n);a=i.style[o],a||r.node[o]===r.parent[o]||(a=r.node[o])}return a?"none"===a?null:a:t}function C(i,n,r){var s={node:F.getStyles(n)||{},parent:!r&&F.getStyles(n.parentNode)||{}};return e.each(z,function(r,a){var o=w(n,a,s);o!==t&&(i=e.pick(r(i,o,a,n,s),i))}),i}function S(t){var e=t&&t.match(/\((?:#|)([^)']+)/);return e&&I[e[1]]}function P(t,i,n){function r(t){paper=a;var e=P(t,i,n),r=i.onLoad,s=a.project&&a.getView();r&&r.call(this,e),s.update()}if(!t)return null;i?"function"==typeof i&&(i={onLoad:i}):i={};var s=t,a=paper;if(n)if("string"!=typeof t||/^.*s;s++){var o=r[s];if(1===o.nodeType){var h=o.nextSibling;document.body.appendChild(o);var u=P(o,i,n);return h?t.insertBefore(o,h):t.appendChild(o),u}}},g:a,svg:a,clippath:a,polygon:o,polyline:o,path:u,lineargradient:c,radialgradient:c,image:function(t){var e=new b(i(t,"href",!0));return e.on("load",function(){var e=r(t,"width","height");this.setSize(e);var i=this._matrix._transformPoint(n(t,"x","y").add(e.divide(2)));this.translate(i)}),e},symbol:function(t,e,i,n){return new p(a(t,e,i,n),!0)},defs:a,use:function(t){var e=(i(t,"href",!0)||"").substring(1),r=I[e],s=n(t,"x","y");return r?r instanceof p?r.place(s):r.clone().translate(s):null},circle:function(t){return new x.Circle(n(t,"cx","cy"),i(t,"r"))},ellipse:function(t){return new x.Ellipse({center:n(t,"cx","cy"),radius:r(t,"rx","ry")})},rect:function(t){var e=n(t,"x","y"),i=r(t,"width","height"),s=r(t,"rx","ry");return new x.Rectangle(new d(e,i),s)},line:function(t){return new O.Line(n(t,"x1","y1"),n(t,"x2","y2"))},text:function(t){var e=new j(n(t,"x","y").add(n(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},z=e.set(e.each(K,function(t){this[t.attribute]=function(e,i){if(e[t.set](s(i,t.type,t.fromSVG)),"color"===t.type&&e instanceof x){var n=e[t.get]();n&&n.transform((new f).translate(e.getPosition(!0).negate()))}}},{}),{id:function(t,e){I[e]=t,t.setName&&t.setName(e)},"clip-path":function(t,e){var i=S(e);if(i){if(i=i.clone(),i.setClipMask(!0),!(t instanceof y))return new y(i,t);t.insertChild(0,i)}},gradientTransform:_,transform:_,"fill-opacity":g,"stroke-opacity":g,visibility:function(t,e){t.setVisible("visible"===e)},display:function(t,e){t.setVisible(null!==e)},"stop-color":function(t,e){t.setColor&&t.setColor(e)},"stop-opacity":function(t,e){t._color&&t._color.setAlpha(parseFloat(e))},offset:function(t,e){var i=e.match(/(.*)%$/);t.setRampPoint(i?i[1]/100:parseFloat(e))},viewBox:function(t,e,i,n,a){var o=new d(s(e,"array")),h=r(n,"width","height",!0);if(t instanceof y){var u=h?o.getSize().divide(h):1,l=(new f).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof p){h&&o.setSize(h);var c="visible"!=w(n,"overflow",a),_=t._definition;c&&!o.contains(_.getBounds())&&(c=new x.Rectangle(o).transform(_._matrix),c.setClipMask(!0),_.addChild(c))}}}),I={};m.inject({importSVG:function(t,e){return this.addChild(P(t,e,!0))}}),v.inject({importSVG:function(t,e){return this.activate(),P(t,e,!0)}})},paper=new(n.inject(e.exports,{enumerable:!0,Base:e,Numerical:a,Key:G})),"function"==typeof define&&define.amd?define("paper",paper):"object"==typeof module&&module&&(module.exports=paper),paper}; \ No newline at end of file +var paper=new function(t){var e=new function(){function n(t,n,i,r,a){function o(s,o){o=o||(o=u(n,s))&&(o.get?o:o.value),"string"==typeof o&&"#"===o[0]&&(o=t[o.substring(1)]||o);var l,d="function"==typeof o,_=o,f=a||d&&!o.base?o&&o.get?s in t:t[s]:null;a&&f||(d&&f&&(o.base=f),d&&r!==!1&&(l=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(h[l[3].toLowerCase()+l[4]]=l[2]),_&&!d&&_.get&&"function"==typeof _.get&&e.isPlainObject(_)||(_={value:_,writable:!0}),(u(t,s)||{configurable:!0}).configurable&&(_.configurable=!0,_.enumerable=i),c(t,s,_))}var h={};if(n){for(var l in n)n.hasOwnProperty(l)&&!s.test(l)&&o(l);for(var l in h){var d=h[l],_=t["set"+d],f=t["get"+d]||_&&t["is"+d];!f||r!==!0&&0!==f.length||o(l,{get:f,set:_})}}return t}function i(t,e,n){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?a:o).call(t,e,n=n||t),n}function r(t,e,n){for(var i in e)!e.hasOwnProperty(i)||n&&n[i]||(t[i]=e[i]);return t}var s=/^(statics|enumerable|beans|preserve)$/,a=[].forEach||function(t,e){for(var n=0,i=this.length;i>n;n++)t.call(e,this[n],n,this)},o=function(t,e){for(var n in this)this.hasOwnProperty(n)&&t.call(e,this[n],n,this)},h=Object.create||function(t){return{__proto__:t}},u=Object.getOwnPropertyDescriptor||function(t,e){var n=t.__lookupGetter__&&t.__lookupGetter__(e);return n?{get:n,set:t.__lookupSetter__(e),enumerable:!0,configurable:!0}:t.hasOwnProperty(e)?{value:t[e],enumerable:!0,configurable:!0,writable:!0}:null},l=Object.defineProperty||function(t,e,n){return(n.get||n.set)&&t.__defineGetter__?(n.get&&t.__defineGetter__(e,n.get),n.set&&t.__defineSetter__(e,n.set)):t[e]=n.value,t},c=function(t,e,n){return delete t[e],l(t,e,n)};return n(function(){for(var t=0,e=arguments.length;e>t;t++)r(this,arguments[t])},{inject:function(t){if(t){var e=t.statics===!0?t:t.statics,i=t.beans,r=t.preserve;e!==t&&n(this.prototype,t,t.enumerable,i,r),n(this,e,!0,i,r)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e,i=this,r=0,s=arguments.length;s>r&&!(t=arguments[r].initialize);r++);return t=t||function(){i.apply(this,arguments)},e=t.prototype=h(this.prototype),c(e,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length&&this.inject.apply(t,arguments),t.base=i,t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++){var i=arguments[t];i&&n(this,i,i.enumerable,i.beans,i.preserve)}return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},set:function(t){return r(this,t)},clone:function(){return new this.constructor(this)},statics:{each:i,create:h,define:c,describe:u,set:r,clone:function(t){return r(new t.constructor,t)},isPlainObject:function(t){var n=null!=t&&t.constructor;return n&&(n===Object||n===e||"Object"===n.name)},pick:function(e,n){return e!==t?e:n}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({toString:function(){return null!=this._id?(this._class||"Object")+(this._name?" '"+this._name+"'":" @"+this._id):"{ "+e.each(this,function(t,e){if(!/^_/.test(e)){var n=typeof t;this.push(e+": "+("number"===n?s.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},getClassName:function(){return this._class||""},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i,r){if(n&&(r||e.isPlainObject(n))){for(var s=Object.keys(n._filtering||n),a=0,o=s.length;o>a;a++){var h=s[a];if(!i||!i[h]){var u=n[h];u!==t&&(this[h]=u)}}return!0}},statics:{exports:{enumerable:!0},extend:function tt(){var t=tt.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(t&&n&&"object"==typeof t&&"object"==typeof n){if(Array.isArray(t)&&Array.isArray(n)){var i=t.length;if(i!==n.length)return!1;for(;i--;)if(!e.equals(t[i],n[i]))return!1}else{var r=Object.keys(t),i=r.length;if(i!==Object.keys(n).length)return!1;for(;i--;){var s=r[i];if(!n.hasOwnProperty(s)||!e.equals(t[s],n[s]))return!1}}return!0}return!1},read:function(n,i,r,s){if(this===e){var a=this.peek(n,i);return n.__index++,a}var o=this.prototype,h=o._readIndex,u=i||h&&n.__index||0;s||(s=n.length-u);var l=n[u];return l instanceof this||r&&r.readNull&&null==l&&1>=s?(h&&(n.__index=u+1),l&&r&&r.clone?l.clone():l):(l=e.create(this.prototype),h&&(l.__read=!0),l=l.initialize.apply(l,u>0||ss;s++)r.push(Array.isArray(i=t[s])?this.read(i,0,n):this.read(t,s,n,1));return r},readNamed:function(n,i,r,s,a){var o=this.getNamed(n,i),h=o!==t;if(h){var u=n._filtered;u||(u=n._filtered=e.create(n[0]),u._filtering=n[0]),u[i]=t}return this.read(h?[o]:n,r,s,a)},getNamed:function(n,i){var r=n[0];return n._hasObject===t&&(n._hasObject=1===n.length&&e.isPlainObject(r)),n._hasObject?i?r[i]:n._filtered||r:t},hasNamed:function(t,e){return!!this.getNamed(t,e)},isPlainValue:function(t,e){return this.isPlainObject(t)||Array.isArray(t)||e&&"string"==typeof t},serialize:function(t,n,i,r){n=n||{};var a,o=!r;if(o&&(n.formatter=new s(n.precision),r={length:0,definitions:{},references:{},add:function(t,e){var n="#"+t._id,i=this.references[n];if(!i){this.length++;var r=e.call(t),s=t._class;s&&r[0]!==s&&r.unshift(s),this.definitions[n]=r,i=this.references[n]=[n]}return i}}),t&&t._serialize){a=t._serialize(n,r);var h=t._class;!h||i||a._compact||a[0]===h||a.unshift(h)}else if(Array.isArray(t)){a=[];for(var u=0,l=t.length;l>u;u++)a[u]=e.serialize(t[u],n,i,r);i&&(a._compact=!0)}else if(e.isPlainObject(t)){a={};for(var c=Object.keys(t),u=0,l=c.length;l>u;u++){var d=c[u];a[d]=e.serialize(t[d],n,i,r)}}else a="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],a]:a},deserialize:function(t,n,i,r){var s=t,a=!i;if(i=i||{},Array.isArray(t)){var o=t[0],h="dictionary"===o;if(1==t.length&&/^#/.test(o))return i.dictionary[o];o=e.exports[o],s=[],r&&(i.dictionary=s);for(var u=o?1:0,l=t.length;l>u;u++)s.push(e.deserialize(t[u],n,i,h));if(o){var c=s;n?s=n(o,c):(s=e.create(o.prototype),o.apply(s,c))}}else if(e.isPlainObject(t)){s={},r&&(i.dictionary=s);for(var d in t)s[d]=e.deserialize(t[d],n,i)}return a&&t&&t.length&&"dictionary"===t[0][0]?s[1]:s},exportJSON:function(t,n){var i=e.serialize(t,n);return n&&n.asString===!1?i:JSON.stringify(i)},importJSON:function(t,n){return e.deserialize("string"==typeof t?JSON.parse(t):t,function(t,i){var r=n&&n.constructor===t?n:e.create(t.prototype),s=r===n;if(1===i.length&&r instanceof m&&(s||!(r instanceof w))){var a=i[0];e.isPlainObject(a)&&(a.insert=!1)}return t.apply(r,i),s&&(n=null),r})},splice:function(e,n,i,r){var s=n&&n.length,a=i===t;i=a?e.length:i,i>e.length&&(i=e.length);for(var o=0;s>o;o++)n[o]._index=i+o;if(a)return e.push.apply(e,n),[];var h=[i,r];n&&h.push.apply(h,n);for(var u=e.splice.apply(e,h),o=0,l=u.length;l>o;o++)u[o]._index=t;for(var o=i+s,l=e.length;l>o;o++)e[o]._index=o;return u},capitalize:function(t){return t.replace(/\b[a-z]/g,function(t){return t.toUpperCase()})},camelize:function(t){return t.replace(/-(.)/g,function(t,e){return e.toUpperCase()})},hyphenate:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}}});var n={on:function(t,n){if("string"!=typeof t)e.each(t,function(t,e){this.on(e,t)},this);else{var i=this._eventTypes,r=i&&i[t],s=this._callbacks=this._callbacks||{};s=s[t]=s[t]||[],-1===s.indexOf(n)&&(s.push(n),r&&r.install&&1===s.length&&r.install.call(this,t))}return this},off:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.off(e,t)},this),t;var r,s=this._eventTypes,a=s&&s[n],o=this._callbacks&&this._callbacks[n];return o&&(!i||-1!==(r=o.indexOf(i))&&1===o.length?(a&&a.uninstall&&a.uninstall.call(this,n),delete this._callbacks[n]):-1!==r&&o.splice(r,1)),this},once:function(t,e){return this.on(t,function(){e.apply(this,arguments),this.off(t,e)})},emit:function(t,e){var n=this._callbacks&&this._callbacks[t];if(!n)return!1;var i=[].slice.call(arguments,1);n=n.slice();for(var r=0,s=n.length;s>r;r++)if(n[r].apply(this,i)===!1){e&&e.stop&&e.stop();break}return!0},responds:function(t){return!(!this._callbacks||!this._callbacks[t])},attach:"#on",detach:"#off",fire:"#emit",_installEvents:function(t){var e=this._callbacks,n=t?"install":"uninstall";for(var i in e)if(e[i].length>0){var r=this._eventTypes,s=r&&r[i],a=s&&s[n];a&&a.call(this,i)}},statics:{inject:function et(t){var n=t._events;if(n){var i={};e.each(n,function(n,r){var s="string"==typeof n,a=s?n:r,o=e.capitalize(a),h=a.substring(2).toLowerCase();i[h]=s?{}:n,a="_"+a,t["get"+o]=function(){return this[a]},t["set"+o]=function(t){var e=this[a];e&&this.off(h,e),t&&this.on(h,t),this[a]=t}}),t._eventTypes=i}return et.base.apply(this,arguments)}}},i=e.extend({_class:"PaperScope",initialize:function nt(){paper=this,this.settings=new e({applyMatrix:!0,handleSize:4,hitTolerance:0}),this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=nt._id++,nt._scopes[this._id]=this;var t=nt.prototype;if(!this.support){var n=Y.getContext(1,1);t.support={nativeDash:"setLineDash"in n||"mozDash"in n,nativeBlendModes:$.nativeModes},Y.release(n)}if(!this.browser){var i=navigator.userAgent.toLowerCase(),r=(/(win)/.exec(i)||/(mac)/.exec(i)||/(linux)/.exec(i)||[])[0],s=t.browser={platform:r};r&&(s[r]=!0),i.replace(/(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,function(t,e,n,i,r){if(!s.chrome){var a="opera"===e?i:n;"trident"===e&&(a=r,e="msie"),s.version=a,s.versionNumber=parseFloat(a),s.name=e,s[e]=!0}}),s.chrome&&delete s.webkit,s.atom&&delete s.chrome}},version:"0.9.25",getView:function(){return this.project&&this.project.getView()},getPaper:function(){return this},execute:function(t,e,n){paper.PaperScript.execute(t,this,e,n),V.updateFocus()},install:function(t){var n=this;e.each(["project","view","tool"],function(i){e.define(t,i,{configurable:!0,get:function(){return n[i]}})});for(var i in this)!/^_/.test(i)&&this[i]&&(t[i]=this[i])},setup:function(t){return paper=this,this.project=new v(t),this},activate:function(){paper=this},clear:function(){for(var t=this.projects.length-1;t>=0;t--)this.projects[t].remove();for(var t=this.tools.length-1;t>=0;t--)this.tools[t].remove();for(var t=this.palettes.length-1;t>=0;t--)this.palettes[t].remove()},remove:function(){this.clear(),delete i._scopes[this._id]},statics:new function(){function t(t){return t+="Attribute",function(e,n){return e[t](n)||e[t]("data-paper-"+n)}}return{_scopes:{},_id:0,get:function(t){return this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),r=e.extend(n,{initialize:function(t){this._scope=paper,this._index=this._scope[this._list].push(this)-1,(t||!this._scope[this._reference])&&this.activate()},activate:function(){if(!this._scope)return!1;var t=this._scope[this._reference];return t&&t!==this&&t.emit("deactivate"),this._scope[this._reference]=this,this.emit("activate",t),!0},isActive:function(){return this._scope[this._reference]===this},remove:function(){return null==this._index?!1:(e.splice(this._scope[this._list],null,this._index,1),this._scope[this._reference]==this&&(this._scope[this._reference]=null),this._scope=null,!0)}}),s=e.extend({initialize:function(t){this.precision=t||5,this.multiplier=Math.pow(10,this.precision)},number:function(t){return Math.round(t*this.multiplier)/this.multiplier},pair:function(t,e,n){return this.number(t)+(n||",")+this.number(e)},point:function(t,e){return this.number(t.x)+(e||",")+this.number(t.y)},size:function(t,e){return this.number(t.width)+(e||",")+this.number(t.height)},rectangle:function(t,e){return this.point(t,e)+(e||",")+this.size(t,e)}});s.instance=new s;var a=new function(){function t(t,e,n){return e>t?e:t>n?n:t}var e=[[.5773502691896257],[0,.7745966692414834],[.33998104358485626,.8611363115940526],[0,.5384693101056831,.906179845938664],[.2386191860831969,.6612093864662645,.932469514203152],[0,.4058451513773972,.7415311855993945,.9491079123427585],[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],[0,.3242534234038089,.6133714327005904,.8360311073266358,.9681602395076261],[.14887433898163122,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],[0,.26954315595234496,.5190961292068118,.7301520055740494,.8870625997680953,.978228658146057],[.1252334085114689,.3678314989981802,.5873179542866175,.7699026741943047,.9041172563704749,.9815606342467192],[0,.2304583159551348,.44849275103644687,.6423493394403402,.8015780907333099,.9175983992229779,.9841830547185881],[.10805494870734367,.31911236892788974,.5152486363581541,.6872929048116855,.827201315069765,.9284348836635735,.9862838086968123],[0,.20119409399743451,.3941513470775634,.5709721726085388,.7244177313601701,.8482065834104272,.937273392400706,.9879925180204854],[.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]],n=[[1],[.8888888888888888,.5555555555555556],[.6521451548625461,.34785484513745385],[.5688888888888889,.47862867049936647,.23692688505618908],[.46791393457269104,.3607615730481386,.17132449237917036],[.4179591836734694,.3818300505051189,.27970539148927664,.1294849661688697],[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626],[.3302393550012598,.31234707704000286,.26061069640293544,.1806481606948574,.08127438836157441],[.29552422471475287,.26926671930999635,.21908636251598204,.1494513491505806,.06667134430868814],[.2729250867779006,.26280454451024665,.23319376459199048,.18629021092773426,.1255803694649046,.05566856711617366],[.24914704581340277,.2334925365383548,.20316742672306592,.16007832854334622,.10693932599531843,.04717533638651183],[.2325515532308739,.22628318026289723,.2078160475368885,.17814598076194574,.13887351021978725,.09212149983772845,.04048400476531588],[.2152638534631578,.2051984637212956,.18553839747793782,.15720316715819355,.12151857068790319,.08015808715976021,.03511946033175186],[.2025782419255613,.19843148532711158,.1861610000155622,.16626920581699392,.13957067792615432,.10715922046717194,.07036604748810812,.03075324199611727],[.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096]],i=Math.abs,r=Math.sqrt,s=Math.pow,o=1e-12,h=1.12e-16;return{TOLERANCE:1e-6,EPSILON:o,MACHINE_EPSILON:h,CURVETIME_EPSILON:4e-7,GEOMETRIC_EPSILON:2e-7,WINDING_EPSILON:2e-7,TRIGONOMETRIC_EPSILON:1e-7,CLIPPING_EPSILON:1e-7,KAPPA:4*(r(2)-1)/3,isZero:function(t){return t>=-o&&o>=t},integrate:function(t,i,r,s){for(var a=e[s-2],o=n[s-2],h=.5*(r-i),u=h+i,l=0,c=s+1>>1,d=1&s?o[l++]*t(u):0;c>l;){var _=h*a[l];d+=o[l++]*(t(u+_)+t(u-_))}return h*d},findRoot:function(t,e,n,r,s,a,o){for(var h=0;a>h;h++){var u=t(n),l=u/e(n),c=n-l;if(i(l)0?(s=n,n=r>=c?.5*(r+s):c):(r=n,n=c>=s?.5*(r+s):c)}return n},solveQuadratic:function(e,n,a,u,l,c){var d,_,f=0,g=l-o,v=c+o,p=1/0,m=n;if(n/=-2,_=n*n-e*a,0!==_&&i(_)y){var w=s(10,i(Math.floor(Math.log(y)*Math.LOG10E)));isFinite(w)||(w=0),e*=w,n*=w,a*=w,_=n*n-e*a}}if(i(e)=-h){var x=0>_?0:r(_),b=n+(0>n?-x:x);0===b?(d=a/e,p=-d):(d=b/e,p=a/b)}return isFinite(d)&&(null==l||d>g&&v>d)&&(u[f++]=null==l?d:t(d,l,c)),p!==d&&isFinite(p)&&(null==l||p>g&&v>p)&&(u[f++]=null==l?p:t(p,l,c)),f},solveCubic:function(e,n,u,l,c,d,_){var f,g,v,p=0;if(i(e)x?-1:1,x=-w/e,b=x>0?1.3247179572*Math.max(b,r(x)):b,m=f-C*b,m!==f){do if(f=m,S=e*f,g=S+n,v=g*f+u,w=(S+g)*f+v,y=v*f+l,m=0===w?f:f-y/w/P,m===f){f=m;break}while(C*m>C*f);i(e)*f*f>i(l/f)&&(v=-l/f,g=(v-u)/f)}}var p=a.solveQuadratic(e,g,v,c,d,_);return isFinite(f)&&(0===p||f!==c[p-1])&&(null==d||f>d-o&&_+o>f)&&(c[p++]=null==d?f:t(f,d,_)),p}}},o={_id:1,_pools:{},get:function(t){if(t){var e=t._class,n=this._pools[e];return n||(n=this._pools[e]={_id:1}),n._id++}return this._id++}},h=e.extend({_class:"Point",_readIndex:!0,initialize:function(t,e){var n=typeof t;if("number"===n){var i="number"==typeof e;this.x=t,this.y=i?e:t,this.__read&&(this.__read=i?2:1)}else"undefined"===n||null===t?(this.x=this.y=0,this.__read&&(this.__read=null===t?1:0)):(Array.isArray(t)?(this.x=t[0],this.y=t.length>1?t[1]:t[0]):null!=t.x?(this.x=t.x,this.y=t.y):null!=t.width?(this.x=t.width,this.y=t.height):null!=t.angle?(this.x=t.length,this.y=0,this.setAngle(t.angle)):(this.x=this.y=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.x=t,this.y=e,this},equals:function(t){return this===t||t&&(this.x===t.x&&this.y===t.y||Array.isArray(t)&&this.x===t[0]&&this.y===t[1])||!1},clone:function(){return new h(this.x,this.y)},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y)]},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(t){if(this.isZero()){var e=this._angle||0;this.set(Math.cos(e)*t,Math.sin(e)*t)}else{var n=t/this.getLength();a.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}},getAngle:function(){return 180*this.getAngleInRadians.apply(this,arguments)/Math.PI},setAngle:function(t){this.setAngleInRadians.call(this,t*Math.PI/180)},getAngleInDegrees:"#getAngle",setAngleInDegrees:"#setAngle",getAngleInRadians:function(){if(arguments.length){var t=h.read(arguments),e=this.getLength()*t.getLength();if(a.isZero(e))return NaN;var n=this.dot(t)/e;return Math.acos(-1>n?-1:n>1?1:n)}return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x)},setAngleInRadians:function(t){if(this._angle=t,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3}},{beans:!1,getDirectedAngle:function(){var t=h.read(arguments);return 180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},getDistance:function(){var t=h.read(arguments),n=t.x-this.x,i=t.y-this.y,r=n*n+i*i,s=e.read(arguments);return s?r:Math.sqrt(r)},normalize:function(e){e===t&&(e=1);var n=this.getLength(),i=0!==n?e/n:0,r=new h(this.x*i,this.y*i);return i>=0&&(r._angle=this._angle),r},rotate:function(t,e){if(0===t)return this.clone();t=t*Math.PI/180;var n=e?this.subtract(e):this,i=Math.sin(t),r=Math.cos(t);return n=new h(n.x*r-n.y*i,n.x*i+n.y*r),e?n.add(e):n},transform:function(t){return t?t._transformPoint(this):this},add:function(){var t=h.read(arguments);return new h(this.x+t.x,this.y+t.y)},subtract:function(){var t=h.read(arguments);return new h(this.x-t.x,this.y-t.y)},multiply:function(){var t=h.read(arguments);return new h(this.x*t.x,this.y*t.y)},divide:function(){var t=h.read(arguments);return new h(this.x/t.x,this.y/t.y)},modulo:function(){var t=h.read(arguments);return new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},isInside:function(){return d.read(arguments).contains(this)},isClose:function(){var t=h.read(arguments),n=e.read(arguments);return this.getDistance(t)1?t[1]:t[0]):null!=t.width?(this.width=t.width,this.height=t.height):null!=t.x?(this.width=t.x,this.height=t.y):(this.width=this.height=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.width=t,this.height=e,this},equals:function(t){return t===this||t&&(this.width===t.width&&this.height===t.height||Array.isArray(t)&&this.width===t[0]&&this.height===t[1])||!1},clone:function(){return new l(this.width,this.height)},toString:function(){var t=s.instance;return"{ width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.width),e.number(this.height)]},add:function(){var t=l.read(arguments);return new l(this.width+t.width,this.height+t.height)},subtract:function(){var t=l.read(arguments);return new l(this.width-t.width,this.height-t.height)},multiply:function(){var t=l.read(arguments);return new l(this.width*t.width,this.height*t.height)},divide:function(){var t=l.read(arguments);return new l(this.width/t.width,this.height/t.height)},modulo:function(){var t=l.read(arguments);return new l(this.width%t.width,this.height%t.height)},negate:function(){return new l(-this.width,-this.height)},isZero:function(){return a.isZero(this.width)&&a.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new l(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new l(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new l(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new l(e(this.width),e(this.height))}},{})),c=l.extend({initialize:function(t,e,n,i){this._width=t,this._height=e,this._owner=n,this._setter=i},set:function(t,e,n){return this._width=t,this._height=e,n||this._owner[this._setter](this),this},getWidth:function(){return this._width},setWidth:function(t){this._width=t,this._owner[this._setter](this)},getHeight:function(){return this._height},setHeight:function(t){this._height=t,this._owner[this._setter](this)}}),d=e.extend({_class:"Rectangle",_readIndex:!0,beans:!0,initialize:function(n,i,r,s){var a=typeof n,o=0;if("number"===a?(this.x=n,this.y=i,this.width=r,this.height=s,o=4):"undefined"===a||null===n?(this.x=this.y=this.width=this.height=0,o=null===n?1:0):1===arguments.length&&(Array.isArray(n)?(this.x=n[0],this.y=n[1],this.width=n[2],this.height=n[3],o=1):n.x!==t||n.width!==t?(this.x=n.x||0,this.y=n.y||0,this.width=n.width||0,this.height=n.height||0,o=1):n.from===t&&n.to===t&&(this.x=this.y=this.width=this.height=0,this._set(n),o=1)),!o){var u=h.readNamed(arguments,"from"),c=e.peek(arguments);if(this.x=u.x,this.y=u.y,c&&c.x!==t||e.hasNamed(arguments,"to")){var d=h.readNamed(arguments,"to");this.width=d.x-u.x,this.height=d.y-u.y,this.width<0&&(this.x=d.x,this.width=-this.width),this.height<0&&(this.y=d.y,this.height=-this.height)}else{var _=l.read(arguments);this.width=_.width,this.height=_.height}o=arguments.__index}this.__read&&(this.__read=o)},set:function(t,e,n,i){return this.x=t,this.y=e,this.width=n,this.height=i,this},clone:function(){return new d(this.x,this.y,this.width,this.height)},equals:function(t){var n=e.isPlainValue(t)?d.read(arguments):t;return n===this||n&&this.x===n.x&&this.y===n.y&&this.width===n.width&&this.height===n.height||!1},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+", width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y),e.number(this.width),e.number(this.height)]},getPoint:function(t){var e=t?h:u;return new e(this.x,this.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.x=t.x,this.y=t.y},getSize:function(t){var e=t?l:c;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=l.read(arguments);this._fixX&&(this.x+=(this.width-t.width)*this._fixX),this._fixY&&(this.y+=(this.height-t.height)*this._fixY),this.width=t.width,this.height=t.height,this._fixW=1,this._fixH=1},getLeft:function(){return this.x},setLeft:function(t){this._fixW||(this.width-=t-this.x),this.x=t,this._fixX=0},getTop:function(){return this.y},setTop:function(t){this._fixH||(this.height-=t-this.y),this.y=t,this._fixY=0},getRight:function(){return this.x+this.width},setRight:function(e){this._fixX!==t&&1!==this._fixX&&(this._fixW=0),this._fixW?this.x=e-this.width:this.width=e-this.x,this._fixX=1},getBottom:function(){return this.y+this.height},setBottom:function(e){this._fixY!==t&&1!==this._fixY&&(this._fixH=0),this._fixH?this.y=e-this.height:this.height=e-this.y,this._fixY=1},getCenterX:function(){return this.x+.5*this.width},setCenterX:function(t){this.x=t-.5*this.width,this._fixX=.5},getCenterY:function(){return this.y+.5*this.height},setCenterY:function(t){this.y=t-.5*this.height,this._fixY=.5},getCenter:function(t){var e=t?h:u;return new e(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(){var t=h.read(arguments);return this.setCenterX(t.x),this.setCenterY(t.y),this},getArea:function(){return this.width*this.height},isEmpty:function(){return 0===this.width||0===this.height},contains:function(e){return e&&e.width!==t||4==(Array.isArray(e)?e:arguments).length?this._containsRectangle(d.read(arguments)):this._containsPoint(h.read(arguments))},_containsPoint:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e<=this.x+this.width&&n<=this.y+this.height},_containsRectangle:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e+t.width<=this.x+this.width&&n+t.height<=this.y+this.height},intersects:function(){var t=d.read(arguments);return t.x+t.width>this.x&&t.y+t.height>this.y&&t.x=this.x&&t.y+t.height>=this.y&&t.x<=this.x+this.width&&t.y<=this.y+this.height},intersect:function(){var t=d.read(arguments),e=Math.max(this.x,t.x),n=Math.max(this.y,t.y),i=Math.min(this.x+this.width,t.x+t.width),r=Math.min(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},unite:function(){var t=d.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x+t.width),r=Math.max(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},include:function(){var t=h.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x),r=Math.max(this.y+this.height,t.y);return new d(e,n,i-e,r-n)},expand:function(){var t=l.read(arguments),e=t.width,n=t.height;return new d(this.x-e/2,this.y-n/2,this.width+e,this.height+n)},scale:function(e,n){return this.expand(this.width*e-this.width,this.height*(n===t?e:n)-this.height)}},e.each([["Top","Left"],["Top","Right"],["Bottom","Left"],["Bottom","Right"],["Left","Center"],["Top","Center"],["Right","Center"],["Bottom","Center"]],function(t,e){var n=t.join(""),i=/^[RL]/.test(n);e>=4&&(t[1]+=i?"Y":"X");var r=t[i?0:1],s=t[i?1:0],a="get"+r,o="get"+s,l="set"+r,c="set"+s,d="get"+n,_="set"+n;this[d]=function(t){var e=t?h:u;return new e(this[a](),this[o](),this,_)},this[_]=function(){var t=h.read(arguments);this[l](t.x),this[c](t.y)}},{beans:!0})),_=d.extend({initialize:function(t,e,n,i,r,s){this.set(t,e,n,i,!0),this._owner=r,this._setter=s},set:function(t,e,n,i,r){return this._x=t,this._y=e,this._width=n,this._height=i,r||this._owner[this._setter](this),this}},new function(){var t=d.prototype;return e.each(["x","y","width","height"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._dontNotify||this._owner[this._setter](this)}},e.each(["Point","Size","Center","Left","Top","Right","Bottom","CenterX","CenterY","TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],function(e){var n="set"+e;this[n]=function(){this._dontNotify=!0,t[n].apply(this,arguments),this._dontNotify=!1,this._owner[this._setter](this)}},{isSelected:function(){return this._owner._boundsSelected},setSelected:function(t){var e=this._owner;e.setSelected&&(e._boundsSelected=t,e.setSelected(t||e._selectedSegmentState>0))}}))}),f=e.extend({_class:"Matrix",initialize:function it(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof it?this.set(t._a,t._c,t._b,t._d,t._tx,t._ty):Array.isArray(t)?this.set.apply(this,t):n=!1:0===e?this.reset():n=!1,!n)throw Error("Unsupported matrix parameters")},set:function(t,e,n,i,r,s,a){return this._a=t,this._c=e,this._b=n,this._d=i,this._tx=r,this._ty=s,a||this._changed(),this},_serialize:function(t){return e.serialize(this.getValues(),t)},_changed:function(){var t=this._owner;t&&(t._applyMatrix?t.transform(null,!0):t._changed(9))},clone:function(){return new f(this._a,this._c,this._b,this._d,this._tx,this._ty)},equals:function(t){return t===this||t&&this._a===t._a&&this._b===t._b&&this._c===t._c&&this._d===t._d&&this._tx===t._tx&&this._ty===t._ty||!1},toString:function(){var t=s.instance;return"[["+[t.number(this._a),t.number(this._b),t.number(this._tx)].join(", ")+"], ["+[t.number(this._c),t.number(this._d),t.number(this._ty)].join(", ")+"]]"},reset:function(t){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,t||this._changed(),this},apply:function(t,n){var i=this._owner;return i?(i.transform(null,!0,e.pick(t,!0),n),this.isIdentity()):!1},translate:function(){var t=h.read(arguments),e=t.x,n=t.y;return this._tx+=e*this._a+n*this._b,this._ty+=e*this._c+n*this._d,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});return e&&this.translate(e),this._a*=t.x,this._c*=t.x,this._b*=t.y,this._d*=t.y,e&&this.translate(e.negate()),this._changed(),this},rotate:function(t){t*=Math.PI/180;var e=h.read(arguments,1),n=e.x,i=e.y,r=Math.cos(t),s=Math.sin(t),a=n-n*r+i*s,o=i-n*s-i*r,u=this._a,l=this._b,c=this._c,d=this._d;return this._a=r*u+s*l,this._b=-s*u+r*l,this._c=r*c+s*d,this._d=-s*c+r*d,this._tx+=a*u+o*l,this._ty+=a*c+o*d,this._changed(),this},shear:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});e&&this.translate(e);var n=this._a,i=this._c;return this._a+=t.y*this._b,this._c+=t.y*this._d,this._b+=t.x*n,this._d+=t.x*i,e&&this.translate(e.negate()),this._changed(),this},skew:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0}),n=Math.PI/180,i=new h(Math.tan(t.x*n),Math.tan(t.y*n));return this.shear(i,e)},concatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=t._a,a=t._b,o=t._c,h=t._d,u=t._tx,l=t._ty;return this._a=s*e+o*n, +this._b=a*e+h*n,this._c=s*i+o*r,this._d=a*i+h*r,this._tx+=u*e+l*n,this._ty+=u*i+l*r,this._changed(),this},preConcatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return this._a=o*e+h*i,this._b=o*n+h*r,this._c=u*e+l*i,this._d=u*n+l*r,this._tx=o*s+h*a+c,this._ty=u*s+l*a+d,this._changed(),this},chain:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return new f(o*e+u*n,o*i+u*r,h*e+l*n,h*i+l*r,s+c*e+d*n,a+c*i+d*r)},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},orNullIfIdentity:function(){return this.isIdentity()?null:this},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,n){return arguments.length<3?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,n)},_transformPoint:function(t,e,n){var i=t.x,r=t.y;return e||(e=new h),e.set(i*this._a+r*this._b+this._tx,i*this._c+r*this._d+this._ty,n)},_transformCoordinates:function(t,e,n){for(var i=0,r=0,s=2*n;s>i;){var a=t[i++],o=t[i++];e[r++]=a*this._a+o*this._b+this._tx,e[r++]=a*this._c+o*this._d+this._ty}return e},_transformCorners:function(t){var e=t.x,n=t.y,i=e+t.width,r=n+t.height,s=[e,n,i,n,i,r,e,r];return this._transformCoordinates(s,s,4)},_transformBounds:function(t,e,n){for(var i=this._transformCorners(t),r=i.slice(0,2),s=r.slice(),a=2;8>a;a++){var o=i[a],h=1&a;os[h]&&(s[h]=o)}return e||(e=new d),e.set(r[0],r[1],s[0]-r[0],s[1]-r[1],n)},inverseTransform:function(){return this._inverseTransform(h.read(arguments))},_getDeterminant:function(){var t=this._a*this._d-this._b*this._c;return isFinite(t)&&!a.isZero(t)&&isFinite(this._tx)&&isFinite(this._ty)?t:null},_inverseTransform:function(t,e,n){var i=this._getDeterminant();if(!i)return null;var r=t.x-this._tx,s=t.y-this._ty;return e||(e=new h),e.set((r*this._d-s*this._b)/i,(s*this._a-r*this._c)/i,n)},decompose:function(){var t=this._a,e=this._b,n=this._c,i=this._d;if(a.isZero(t*i-e*n))return null;var r=Math.sqrt(t*t+e*e);t/=r,e/=r;var s=t*n+e*i;n-=t*s,i-=e*s;var o=Math.sqrt(n*n+i*i);return n/=o,i/=o,s/=o,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,o),rotation:180*-Math.atan2(e,t)/Math.PI,shearing:s}},getValues:function(){return[this._a,this._c,this._b,this._d,this._tx,this._ty]},getTranslation:function(){return new h(this._tx,this._ty)},getScaling:function(){return(this.decompose()||{}).scaling},getRotation:function(){return(this.decompose()||{}).rotation},inverted:function(){var t=this._getDeterminant();return t&&new f(this._d/t,-this._c/t,-this._b/t,this._a/t,(this._b*this._ty-this._d*this._tx)/t,(this._c*this._tx-this._a*this._ty)/t)},shiftless:function(){return new f(this._a,this._c,this._b,this._d,0,0)},applyToContext:function(t){t.transform(this._a,this._c,this._b,this._d,this._tx,this._ty)}},e.each(["a","c","b","d","tx","ty"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._changed()}},{})),g=e.extend({_class:"Line",initialize:function(t,e,n,i,r){var s=!1;arguments.length>=4?(this._px=t,this._py=e,this._vx=n,this._vy=i,s=r):(this._px=t.x,this._py=t.y,this._vx=e.x,this._vy=e.y,s=n),s||(this._vx-=this._px,this._vy-=this._py)},getPoint:function(){return new h(this._px,this._py)},getVector:function(){return new h(this._vx,this._vy)},getLength:function(){return this.getVector().getLength()},intersect:function(t,e){return g.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t,e){return g.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0,e)},getDistance:function(t){return Math.abs(g.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},isCollinear:function(t){return h.isCollinear(this._vx,this._vy,t._vx,t._vy)},isOrthogonal:function(t){return h.isOrthogonal(this._vx,this._vy,t._vx,t._vy)},statics:{intersect:function(t,e,n,i,r,s,o,u,l,c){l||(n-=t,i-=e,o-=r,u-=s);var d=n*u-i*o;if(!a.isZero(d)){var _=t-r,f=e-s,g=(o*f-u*_)/d,v=(n*f-i*_)/d,p=1e-12,m=-p,y=1+p;if(c||g>m&&y>g&&v>m&&y>v)return c||(g=0>=g?0:g>=1?1:g),new h(t+g*n,e+g*i)}},getSide:function(t,e,n,i,r,s,a,o){a||(n-=t,i-=e);var h=r-t,u=s-e,l=h*i-u*n;return 0!==l||o||(l=(h*n+h*n)/(n*n+i*i),l>=0&&1>=l&&(l=0)),0>l?-1:l>0?1:0},getSignedDistance:function(t,e,n,i,r,s,a){return a||(n-=t,i-=e),0===n?i>0?r-t:t-r:0===i?0>n?s-e:e-s:((r-t)*i-(s-e)*n)/Math.sqrt(n*n+i*i)}}}),v=r.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){r.call(this,!0),this.layers=[],this._activeLayer=null,this.symbols=[],this._currentStyle=new R(null,null,this),this._view=V.create(this,t||Y.getCanvas(1,1)),this._selectedItems={},this._selectedItemCount=0,this._updateVersion=0},_serialize:function(t,n){return e.serialize(this.layers,t,!0,n)},clear:function(){for(var t=this.layers.length-1;t>=0;t--)this.layers[t].remove();this.symbols=[]},isEmpty:function(){return 0===this.layers.length},remove:function rt(){return rt.base.call(this)?(this._view&&this._view.remove(),!0):!1},getView:function(){return this._view},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},getOptions:function(){return this._scope.settings},getActiveLayer:function(){return this._activeLayer||new w({project:this})},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},insertChild:function(t,n,i){return n instanceof w?(n._remove(!1,!0),e.splice(this.layers,[n],t,0),n._setProject(this,!0),this._changes&&n._changed(5),this._activeLayer||(this._activeLayer=n)):n instanceof m?(this._activeLayer||this.insertChild(t,new w(m.NO_INSERT))).insertChild(t,n,i):n=null,n},addChild:function(e,n){return this.insertChild(t,e,n)},_updateSelection:function(t){var e=t._id,n=this._selectedItems;t._selected?n[e]!==t&&(this._selectedItemCount++,n[e]=t):n[e]===t&&(this._selectedItemCount--,delete n[e])},selectAll:function(){for(var t=this.layers,e=0,n=t.length;n>e;e++)t[e].setFullySelected(!0)},deselectAll:function(){var t=this._selectedItems;for(var e in t)t[e].setFullySelected(!1)},hitTest:function(){for(var t=h.read(arguments),n=S.getOptions(e.read(arguments)),i=this.layers.length-1;i>=0;i--){var r=this.layers[i]._hitTest(t,n);if(r)return r}return null},getItems:function(t){return m._getItems(this.layers,t)},getItem:function(t){return m._getItems(this.layers,t,null,null,!0)[0]||null},importJSON:function(t){this.activate();var n=this._activeLayer;return e.importJSON(t,n&&n.isEmpty()&&n)},draw:function(t,n,i){this._updateVersion++,t.save(),n.applyToContext(t);for(var r=new e({offset:new h(0,0),pixelRatio:i,viewMatrix:n.isIdentity()?null:n,matrices:[new f],updateMatrix:!0}),s=0,a=this.layers,o=a.length;o>s;s++)a[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;var u=this._selectedItems,l=this._scope.settings.handleSize,c=this._updateVersion;for(var d in u)u[d]._drawSelection(t,n,l,u,c);t.restore()}}}),p=e.extend({_class:"Symbol",initialize:function(t,e){this._id=o.get(),this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._class,this._definition],t,!1,n)})},_changed:function(t){8&t&&m._clearBoundsCache(this),1&t&&(this.project._needsUpdate=!0)},getDefinition:function(){return this._definition},setDefinition:function(t,e){t._parentSymbol&&(t=t.clone()),this._definition&&(this._definition._parentSymbol=null),this._definition=t,t.remove(),t.setSelected(!1),e||t.setPosition(new h),t._parentSymbol=this,this._changed(9)},place:function(t){return new C(this,t)},clone:function(){return new p(this._definition.clone(!1))},equals:function(t){return t===this||t&&this.definition.equals(t.definition)||!1}}),m=e.extend(n,{statics:{extend:function st(t){return t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields)),st.base.apply(this,arguments)},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,applyMatrix:null,matrix:new f,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,n){var i=t&&e.isPlainObject(t),r=i&&t.internal===!0,s=this._matrix=new f,a=i&&t.project||paper.project;return r||(this._id=o.get()),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,n&&s.translate(n),s._owner=this,this._style=new R(a._currentStyle,this,a),this._project||(r||i&&t.insert===!1?this._setProject(a):i&&t.parent?this.setParent(t.parent):(a._activeLayer||new w).addChild(this)),i&&t!==m.NO_INSERT&&this._set(t,{insert:!0,project:!0,parent:!0},!0),i},_events:e.each(["onMouseDown","onMouseUp","onMouseDrag","onClick","onDoubleClick","onMouseMove","onMouseEnter","onMouseLeave"],function(t){this[t]={install:function(t){this.getView()._installEvent(t)},uninstall:function(t){this.getView()._uninstallEvent(t)}}},{onFrame:{install:function(){this.getView()._animateItem(this,!0)},uninstall:function(){this.getView()._animateItem(this,!1)}},onLoad:{}}),_serialize:function(t,n){function i(i){for(var a in i){var o=s[a];e.equals(o,"leading"===a?1.2*i.fontSize:i[a])||(r[a]=e.serialize(o,t,"data"!==a,n))}}var r={},s=this;return i(this._serializeFields),this instanceof y||i(this._style._defaults),[this._class,r]},_changed:function(e){var n=this._parentSymbol,i=this._parent||n,r=this._project;if(8&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=this._currentPath=t),i&&40&e&&m._clearBoundsCache(i),2&e&&m._clearBoundsCache(this),r&&(1&e&&(r._needsUpdate=!0),r._changes)){var s=r._changesById[this._id];s?s.flags|=e:(s={item:this,flags:e},r._changesById[this._id]=s,r._changes.push(s))}n&&n._changed(e)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getName:function(){return this._name},setName:function(e,n){if(this._name&&this._removeNamed(),e===+e+"")throw Error("Names consisting only of numbers are not supported.");var i=this._parent;if(e&&i){for(var r=i._children,s=i._namedChildren,a=e,o=1;n&&r[e];)e=a+" "+o++;(s[e]=s[e]||[]).push(this),r[e]=this}this._name=e||t,this._changed(128)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)}},e.each(["locked","visible","blendMode","opacity","guide"],function(t){var n=e.capitalize(t),t="_"+t;this["get"+n]=function(){return this[t]},this["set"+n]=function(e){e!=this[t]&&(this[t]=e,this._changed("_locked"===t?128:129))}},{}),{beans:!0,_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=this._children,e=0,n=t.length;n>e;e++)if(t[e].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var n=this._children,i=0,r=n.length;r>i;i++)n[i].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(129))},_selected:!1,isFullySelected:function(){var t=this._children;if(t&&this._selected){for(var e=0,n=t.length;n>e;e++)if(!t[e].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){var e=this._children;if(e)for(var n=0,i=e.length;i>n;n++)e[n].setFullySelected(t);this.setSelected(t,!0)},isClipMask:function(){return this._clipMask},setClipMask:function(t){this._clipMask!=(t=!!t)&&(this._clipMask=t,t&&(this.setFillColor(null),this.setStrokeColor(null)),this._changed(129),this._parent&&this._parent._changed(1024))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(t){var e=this._position,n=t?h:u;if(!e){var i=this._pivot;e=this._position=i?this._matrix._transformPoint(i):this.getBounds().getCenter(!0)}return new n(e.x,e.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))},getPivot:function(t){var e=this._pivot;if(e){var n=t?h:u;e=new n(e.x,e.y,this,"setPivot")}return e},setPivot:function(){this._pivot=h.read(arguments,0,{clone:!0,readNull:!0}),this._position=t},_pivot:null},e.each(["bounds","strokeBounds","handleBounds","roughBounds","internalBounds","internalRoughBounds"],function(t){var n="get"+e.capitalize(t),i=t.match(/^internal(.*)$/),r=i?"get"+i[1]:null;this[n]=function(e){var i=this._boundsGetter,s=!r&&("string"==typeof i?i:i&&i[n])||n,a=this._getCachedBounds(s,e,this,r);return"bounds"===t?new _(a.x,a.y,a.width,a.height,this,"setBounds"):a}},{beans:!0,_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new d;m._updateBoundsCache(this,n);for(var r=1/0,s=-r,a=r,o=s,h=0,u=i.length;u>h;h++){var l=i[h];if(l._visible&&!l.isEmpty()){var c=l._getCachedBounds(t,e&&e.chain(l._matrix),n);r=Math.min(c.x,r),a=Math.min(c.y,a),s=Math.max(c.x+c.width,s),o=Math.max(c.y+c.height,o)}}return isFinite(r)?new d(r,a,s-r,o-a):new d},setBounds:function(){var t=d.read(arguments),e=this.getBounds(),n=new f,i=t.getCenter();n.translate(i),(t.width!=e.width||t.height!=e.height)&&n.scale(0!=e.width?t.width/e.width:1,0!=e.height?t.height/e.height:1),i=e.getCenter(),n.translate(-i.x,-i.y),this.transform(n)},_getCachedBounds:function(t,e,n,i){e=e&&e.orNullIfIdentity();var r=i?null:this._matrix.orNullIfIdentity(),s=(!e||e.equals(r))&&t;if(m._updateBoundsCache(this._parent||this._parentSymbol,n),s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();var a=this._getBounds(i||t,e||r,n);if(s){this._bounds||(this._bounds={});var o=this._bounds[s]=a.clone();o._internal=!!i}return a},statics:{_updateBoundsCache:function(t,e){if(t){var n=e._id,i=t._boundsCache=t._boundsCache||{ids:{},list:[]};i.ids[n]||(i.list.push(e),i.ids[n]=e)}},_clearBoundsCache:function(e){var n=e._boundsCache;if(n){e._bounds=e._position=e._boundsCache=t;for(var i=0,r=n.list,s=r.length;s>i;i++){var a=r[i];a!==e&&(a._bounds=a._position=t,a._boundsCache&&m._clearBoundsCache(a))}}}}}),{beans:!0,_decompose:function(){return this._decomposed=this._matrix.decompose()},getRotation:function(){var t=this._decomposed||this._decompose();return t&&t.rotation},setRotation:function(t){var e=this.getRotation();if(null!=e&&null!=t){var n=this._decomposed;this.rotate(t-e),n.rotation=t,this._decomposed=n}},getScaling:function(t){var e=this._decomposed||this._decompose(),n=e&&e.scaling,i=t?h:u;return n&&new i(n.x,n.y,this,"setScaling")},setScaling:function(){var t=this.getScaling();if(t){var e=h.read(arguments,0,{clone:!0}),n=this._decomposed;this.scale(e.x/t.x,e.y/t.y),n.scaling=e,this._decomposed=n}},getMatrix:function(){return this._matrix},setMatrix:function(){var t=this._matrix;t.initialize.apply(t,arguments),this._applyMatrix?this.transform(null,!0):this._changed(9)},getGlobalMatrix:function(t){var e=this._globalMatrix,n=this._project._updateVersion;if(e&&e._updateVersion!==n&&(e=null),!e){e=this._globalMatrix=this._matrix.clone();var i=this._parent;i&&e.preConcatenate(i.getGlobalMatrix(!0)),e._updateVersion=n}return t?e:e.clone()},getApplyMatrix:function(){return this._applyMatrix},setApplyMatrix:function(t){(this._applyMatrix=this._canApplyMatrix&&!!t)&&this.transform(null,!0)},getTransformContent:"#getApplyMatrix",setTransformContent:"#setApplyMatrix"},{getProject:function(){return this._project},_setProject:function(t,e){if(this._project!==t){this._project&&this._installEvents(!1),this._project=t;for(var n=this._children,i=0,r=n&&n.length;r>i;i++)n[i]._setProject(t);e=!0}e&&this._installEvents(!0)},getView:function(){return this._project.getView()},_installEvents:function at(t){at.base.call(this,t);for(var e=this._children,n=0,i=e&&e.length;i>n;n++)e[n]._installEvents(t)},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof w)return t;return null},getParent:function(){return this._parent},setParent:function(t){return t.addChild(this)},getChildren:function(){return this._children},setChildren:function(t){this.removeChildren(),this.addChildren(t)},getFirstChild:function(){return this._children&&this._children[0]||null},getLastChild:function(){return this._children&&this._children[this._children.length-1]||null},getNextSibling:function(){return this._parent&&this._parent._children[this._index+1]||null},getPreviousSibling:function(){return this._parent&&this._parent._children[this._index-1]||null},getIndex:function(){return this._index},equals:function(t){return t===this||t&&this._class===t._class&&this._style.equals(t._style)&&this._matrix.equals(t._matrix)&&this._locked===t._locked&&this._visible===t._visible&&this._blendMode===t._blendMode&&this._opacity===t._opacity&&this._clipMask===t._clipMask&&this._guide===t._guide&&this._equals(t)||!1},_equals:function(t){return e.equals(this._children,t._children)},clone:function(t){return this._clone(new this.constructor(m.NO_INSERT),t)},_clone:function(n,i,r){var s=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],a=this._children;n.setStyle(this._style);for(var o=0,h=a&&a.length;h>o;o++)n.addChild(a[o].clone(!1),!0);for(var o=0,h=s.length;h>o;o++){var u=s[o];this.hasOwnProperty(u)&&(n[u]=this[u])}return r!==!1&&n._matrix.initialize(this._matrix),n.setApplyMatrix(this._applyMatrix),n.setPivot(this._pivot),n.setSelected(this._selected),n._data=this._data?e.clone(this._data):null,(i||i===t)&&n.insertAbove(this),this._name&&n.setName(this._name,!0),n},copyTo:function(t){return t.addChild(this.clone(!1))},rasterize:function(t){var n=this.getStrokeBounds(),i=(t||this.getView().getResolution())/72,r=n.getTopLeft().floor(),s=n.getBottomRight().ceil(),a=new l(s.subtract(r)),o=Y.getCanvas(a.multiply(i)),h=o.getContext("2d"),u=(new f).scale(i).translate(r.negate());h.save(),u.applyToContext(h),this.draw(h,new e({matrices:[u]})),h.restore();var c=new b(m.NO_INSERT);return c.setCanvas(o),c.transform((new f).translate(r.add(a.divide(2))).scale(1/i)),c.insertAbove(this),c},contains:function(){return!!this._contains(this._matrix._inverseTransform(h.read(arguments)))},_contains:function(t){if(this._children){for(var e=this._children.length-1;e>=0;e--)if(this._children[e].contains(t))return!0;return!1}return t.isInside(this.getInternalBounds())},isInside:function(){return d.read(arguments).contains(this.getBounds())},_asPathItem:function(){return new O.Rectangle({rectangle:this.getInternalBounds(),matrix:this._matrix,insert:!1})},intersects:function(t,e){return t instanceof m?this._asPathItem().getIntersections(t._asPathItem(),null,e||t._matrix,!0).length>0:!1},hitTest:function(){return this._hitTest(h.read(arguments),S.getOptions(e.read(arguments)))},_hitTest:function(n,i){function r(i,r){var s=f["get"+r]();return n.subtract(s).divide(u).length<=1?new S(i,_,{name:e.hyphenate(r),point:s}):t}if(this._locked||!this._visible||this._guide&&!i.guides||this.isEmpty())return null;var s=this._matrix,a=i._totalMatrix,o=this.getView(),h=i._totalMatrix=a?a.chain(s):this.getGlobalMatrix().preConcatenate(o._matrix),u=i._tolerancePadding=new l(O._getPenPadding(1,h.inverted())).multiply(Math.max(i.tolerance,1e-6));if(n=s._inverseTransform(n),!this._children&&!this.getInternalRoughBounds().expand(u.multiply(2))._containsPoint(n))return null;var c,d=!(i.guides&&!this._guide||i.selected&&!this._selected||i.type&&i.type!==e.hyphenate(this._class)||i["class"]&&!(this instanceof i["class"])),_=this;if(d&&(i.center||i.bounds)&&this._parent){var f=this.getInternalBounds();if(i.center&&(c=r("center","Center")),!c&&i.bounds)for(var g=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],v=0;8>v&&!c;v++)c=r("bounds",g[v])}var p=!c&&this._children;if(p)for(var m=this._getChildHitTestOptions(i),v=p.length-1;v>=0&&!c;v--)c=p[v]._hitTest(n,m);return!c&&d&&(c=this._hitTestSelf(n,i)),c&&c.point&&(c.point=s.transform(c.point)),i._totalMatrix=a,c},_getChildHitTestOptions:function(t){return t},_hitTestSelf:function(e,n){return n.fill&&this.hasFill()&&this._contains(e)?new S("fill",this):t},matches:function(t,n){function i(t,n){for(var r in t)if(t.hasOwnProperty(r)){var s=t[r],a=n[r];if(e.isPlainObject(s)&&e.isPlainObject(a)){if(!i(s,a))return!1}else if(!e.equals(s,a))return!1}return!0}var r=typeof t;if("object"===r){for(var s in t)if(t.hasOwnProperty(s)&&!this.matches(s,t[s]))return!1}else{if("function"===r)return t(this);var a=/^(empty|editable)$/.test(t)?this["is"+e.capitalize(t)]():"type"===t?e.hyphenate(this._class):this[t];if(/^(constructor|class)$/.test(t)){if(!(this instanceof n))return!1}else if(n instanceof RegExp){if(!n.test(a))return!1}else if("function"==typeof n){if(!n(a))return!1}else if(e.isPlainObject(n)){if(!i(n,a))return!1}else if(!e.equals(a,n))return!1}return!0},getItems:function(t){return m._getItems(this._children,t,this._matrix)},getItem:function(t){return m._getItems(this._children,t,this._matrix,null,!0)[0]||null},statics:{_getItems:function ot(t,n,i,r,s){if(!r&&"object"==typeof n){var a=n.overlapping,o=n.inside,h=a||o,u=h&&d.read([h]);r={items:[],inside:!!o,overlapping:!!a,rect:u,path:a&&new O.Rectangle({rectangle:u,insert:!1})},h&&(n=e.set({},n,{inside:!0,overlapping:!0}))}var l=r&&r.items,u=r&&r.rect;i=u&&(i||new f);for(var c=0,_=t&&t.length;_>c;c++){var g=t[c],v=i&&i.chain(g._matrix),p=!0;if(u){var h=g.getBounds(v);if(!u.intersects(h))continue;r.inside&&u.contains(h)||r.overlapping&&(h.contains(u)||r.path.intersects(g,v))||(p=!1)}if(p&&g.matches(n)&&(l.push(g),s))break;if(ot(g._children,n,v,r,s),s&&l.length>0)break}return l}}},{importJSON:function(t){var n=e.importJSON(t,this);return n!==this?this.addChild(n):n},addChild:function(e,n){return this.insertChild(t,e,n)},insertChild:function(t,e,n){var i=e?this.insertChildren(t,[e],n):null;return i&&i[0]},addChildren:function(t,e){return this.insertChildren(this._children.length,t,e)},insertChildren:function(t,n,i,r){var s=this._children;if(s&&n&&n.length>0){n=Array.prototype.slice.apply(n);for(var a=n.length-1;a>=0;a--){var o=n[a];if(!r||o instanceof r){var h=o._parent===this&&o._indexa;a++){var o=n[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name),l&&this._changed(5)}this._changed(11)}else n=null;return n},_insertSibling:function(t,e,n){return this._parent?this._parent.insertChild(t,e,n):null},insertAbove:function(t,e){return t._insertSibling(t._index+1,this,e)},insertBelow:function(t,e){return t._insertSibling(t._index,this,e)},sendToBack:function(){return(this._parent||this instanceof w&&this._project).insertChild(0,this)},bringToFront:function(){return(this._parent||this instanceof w&&this._project).addChild(this)},appendTop:"#addChild",appendBottom:function(t){return this.insertChild(0,t)},moveAbove:"#insertAbove",moveBelow:"#insertBelow",reduce:function(){if(this._children&&1===this._children.length){var t=this._children[0].reduce();return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent;if(t){var e=t._children,n=t._namedChildren,i=this._name,r=n[i],s=r?r.indexOf(this):-1;-1!==s&&(e[i]==this&&delete e[i],r.splice(s,1),r.length?e[i]=r[r.length-1]:delete n[i])}},_remove:function(t,n){var i=this._parent;if(i){if(this._name&&this._removeNamed(),null!=this._index&&e.splice(i._children,null,this._index,1),this._installEvents(!1),t){var r=this._project;r&&r._changes&&this._changed(5)}return n&&i._changed(11),this._parent=null,!0}return!1},remove:function(){return this._remove(!0,!0)},replaceWith:function(t){var e=t&&t.insertBelow(this);return e&&this.remove(),e},removeChildren:function(t,n){if(!this._children)return null;t=t||0,n=e.pick(n,this._children.length);for(var i=e.splice(this._children,null,t,n-t),r=i.length-1;r>=0;r--)i[r]._remove(!0,!1);return i.length>0&&this._changed(11),i},clear:"#removeChildren",reverseChildren:function(){if(this._children){this._children.reverse();for(var t=0,e=this._children.length;e>t;t++)this._children[t]._index=t;this._changed(11)}},isEmpty:function(){return!this._children||0===this._children.length},isEditable:function(){for(var t=this;t;){if(!t._visible||t._locked)return!1;t=t._parent}return!0},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()},_getOrder:function(t){function e(t){var e=[];do e.unshift(t);while(t=t._parent);return e}for(var n=e(this),i=e(t),r=0,s=Math.min(n.length,i.length);s>r;r++)if(n[r]!=i[r])return n[r]._index0},isInserted:function(){return this._parent?this._parent.isInserted():!1},isAbove:function(t){return-1===this._getOrder(t)},isBelow:function(t){return 1===this._getOrder(t)},isParent:function(t){return this._parent===t},isChild:function(t){return t&&t._parent===this},isDescendant:function(t){for(var e=this;e=e._parent;)if(e==t)return!0;return!1},isAncestor:function(t){return t?t.isDescendant(this):!1},isSibling:function(t){return this._parent===t._parent},isGroupedWith:function(t){for(var e=this._parent;e;){if(e._parent&&/^(Group|Layer|CompoundPath)$/.test(e._class)&&t.isDescendant(e))return!0;e=e._parent}return!1},translate:function(){var t=new f;return this.transform(t.translate.apply(t,arguments))},rotate:function(t){return this.transform((new f).rotate(t,h.read(arguments,1,{readNull:!0})||this.getPosition(!0)))}},e.each(["scale","shear","skew"],function(t){this[t]=function(){var e=h.read(arguments),n=h.read(arguments,0,{readNull:!0});return this.transform((new f)[t](e,n||this.getPosition(!0)))}},{}),{transform:function(t,e,n,i){t&&t.isIdentity()&&(t=null);var r=this._matrix,s=(e||this._applyMatrix)&&(!r.isIdentity()||t||e&&n&&this._children);if(!t&&!s)return this;if(t&&r.preConcatenate(t),s=s&&this._transformContent(r,n,i)){var a=this._pivot,o=this._style,h=o.getFillColor(!0),u=o.getStrokeColor(!0);a&&r._transformPoint(a,a,!0),h&&h.transform(r),u&&u.transform(r),r.reset(!0),i&&this._canApplyMatrix&&(this._applyMatrix=!0)}var l=this._bounds,c=this._position;this._changed(9);var d=l&&t&&t.decompose();if(d&&!d.shearing&&d.rotation%90===0){for(var _ in l){var f=l[_];(s||!f._internal)&&t._transformBounds(f,f)}var g=this._boundsGetter,f=l[g&&g.getBounds||g||"getBounds"];f&&(this._position=f.getCenter(!0)),this._bounds=l}else t&&c&&(this._position=t._transformPoint(c,c));return this},_transformContent:function(t,e,n){var i=this._children;if(i){for(var r=0,s=i.length;s>r;r++)i[r].transform(t,!0,e,n);return!0}},globalToLocal:function(){return this.getGlobalMatrix(!0)._inverseTransform(h.read(arguments))},localToGlobal:function(){return this.getGlobalMatrix(!0)._transformPoint(h.read(arguments))},parentToLocal:function(){return this._matrix._inverseTransform(h.read(arguments))},localToParent:function(){return this._matrix._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=d.read(arguments);var n=this.getBounds(),i=n.height/n.width,r=t.height/t.width,s=(e?i>r:r>i)?t.width/n.width:t.height/n.height,a=new d(new h,new l(n.width*s,n.height*s));a.setCenter(t.getCenter()),this.setBounds(a)},_setStyles:function(t){var e=this._style,n=e.getFillColor(),i=e.getStrokeColor(),r=e.getShadowColor();if(n&&(t.fillStyle=n.toCanvasStyle(t)),i){var s=e.getStrokeWidth();if(s>0){t.strokeStyle=i.toCanvasStyle(t),t.lineWidth=s;var a=e.getStrokeJoin(),o=e.getStrokeCap(),h=e.getMiterLimit();if(a&&(t.lineJoin=a),o&&(t.lineCap=o),h&&(t.miterLimit=h),paper.support.nativeDash){var u=e.getDashArray(),l=e.getDashOffset();u&&u.length&&("setLineDash"in t?(t.setLineDash(u),t.lineDashOffset=l):(t.mozDash=u,t.mozDashOffset=l))}}}if(r){var c=e.getShadowBlur();if(c>0){t.shadowColor=r.toCanvasStyle(t),t.shadowBlur=c;var d=this.getShadowOffset();t.shadowOffsetX=d.x,t.shadowOffsetY=d.y}}},draw:function(t,e,n){function i(t){return a?a.chain(t):t}var r=this._updateVersion=this._project._updateVersion;if(this._visible&&0!==this._opacity){var s=e.matrices,a=e.viewMatrix,o=this._matrix,h=s[s.length-1].chain(o);if(h.isInvertible()){s.push(h),e.updateMatrix&&(h._updateVersion=r,this._globalMatrix=h);var u,l,c,d=this._blendMode,_=this._opacity,f="normal"===d,g=$.nativeModes[d],v=f&&1===_||e.dontStart||e.clip||(g||f&&1>_)&&this._canComposite(),p=e.pixelRatio||1;if(!v){var m=this.getStrokeBounds(i(h));if(!m.width||!m.height)return;c=e.offset,l=e.offset=m.getTopLeft().floor(),u=t,t=Y.getContext(m.getSize().ceil().add(1).multiply(p)),1!==p&&t.scale(p,p)}t.save();var y=n?n.chain(o):!this.getStrokeScaling(!0)&&i(h),w=!v&&e.clipItem,x=!y||w;if(v?(t.globalAlpha=_,g&&(t.globalCompositeOperation=d)):x&&t.translate(-l.x,-l.y),x&&(v?o:i(h)).applyToContext(t),w&&e.clipItem.draw(t,e.extend({clip:!0})),y){t.setTransform(p,0,0,p,0,0);var b=e.offset;b&&t.translate(-b.x,-b.y)}this._draw(t,e,y),t.restore(),s.pop(),e.clip&&!e.dontFinish&&t.clip(),v||($.process(d,t,u,_,l.subtract(c).multiply(p)),Y.release(t),e.offset=c)}}},_isUpdated:function(t){var e=this._parent;if(e instanceof A)return e._isUpdated(t);var n=this._updateVersion===t;return!n&&e&&e._visible&&e._isUpdated(t)&&(this._updateVersion=t,n=!0),n},_drawSelection:function(t,e,n,i,r){if((this._drawSelected||this._boundsSelected)&&this._isUpdated(r)){var s=this.getSelectedColor(!0)||this.getLayer().getSelectedColor(!0),a=e.chain(this.getGlobalMatrix(!0));if(t.strokeStyle=t.fillStyle=s?s.toCanvasStyle(t):"#009dec",this._drawSelected&&this._drawSelected(t,a,i),this._boundsSelected){var o=n/2,h=a._transformCorners(this.getInternalBounds());t.beginPath();for(var u=0;8>u;u++)t[0===u?"moveTo":"lineTo"](h[u],h[++u]);t.closePath(),t.stroke();for(var u=0;8>u;u++)t.fillRect(h[u]-o,h[++u]-o,n,n)}}},_canComposite:function(){return!1}},e.each(["down","drag","up","move"],function(t){this["removeOn"+e.capitalize(t)]=function(){var e={};return e[t]=!0,this.removeOn(e)}},{removeOn:function(t){for(var e in t)if(t[e]){var n="mouse"+e,i=this._project,r=i._removeSets=i._removeSets||{};r[n]=r[n]||{},r[n][this._id]=this}return this}})),y=m.extend({_class:"Group",_selectChildren:!0,_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ht(e){ht.base.call(this,e),1026&e&&(this._clipItem=t)},_getClipItem:function(){var e=this._clipItem;if(e===t){e=null;for(var n=0,i=this._children.length;i>n;n++){var r=this._children[n];if(r._clipMask){e=r;break}}this._clipItem=e}return e},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clip,i=!n&&this._getClipItem(),r=!0;if(e=e.extend({clipItem:i,clip:!1}),n?this._currentPath?(t.currentPath=this._currentPath,r=!1):(t.beginPath(),e.dontStart=e.dontFinish=!0):i&&i.draw(t,e.extend({clip:!0})),r)for(var s=0,a=this._children.length;a>s;s++){var o=this._children[s];o!==i&&o.draw(t,e)}n&&(this._currentPath=t.currentPath)}}),w=y.extend({_class:"Layer",initialize:function(n){var i=e.isPlainObject(n)?new e(n):{children:Array.isArray(n)?n:arguments},r=i.insert;i.insert=!1,y.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function ut(t,n){if(this._parent)return ut.base.call(this,t,n);if(null!=this._index){var i=this._project;return i._activeLayer===this&&(i._activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(i.layers,null,this._index,1),this._installEvents(!1),t&&i._changes&&this._changed(5),n&&(i._needsUpdate=!0),!0}return!1},getNextSibling:function lt(){return this._parent?lt.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function ct(){return this._parent?ct.base.call(this):this._project.layers[this._index-1]||null; +},isInserted:function dt(){return this._parent?dt.base.call(this):null!=this._index},activate:function(){this._project._activeLayer=this},_insertSibling:function _t(t,e,n){return this._parent?_t.base.call(this,t,e,n):this._project.insertChild(t,e,n)}}),x=m.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{type:null,size:null,radius:null},initialize:function(t){this._initialize(t)},_equals:function(t){return this._type===t._type&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new x(m.NO_INSERT);return e.setType(this._type),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getType:function(){return this._type},setType:function(t){this._type=t},getShape:"#getType",setShape:"#setType",getSize:function(){var t=this._size;return new c(t.width,t.height,this,"setSize")},setSize:function(){var t=l.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._type,n=t.width,i=t.height;if("rectangle"===e){var r=l.min(this._radius,t.divide(2));this._radius.set(r.width,r.height)}else"circle"===e?(n=i=(n+i)/2,this._radius=n/2):"ellipse"===e&&this._radius.set(n/2,i/2);this._size.set(n,i),this._changed(9)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._type?t:new c(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._type;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else if(t=l.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=l.max(this._size,t.multiply(2));this._size.set(n.width,n.height)}else"ellipse"===e&&this._size.set(2*t.width,2*t.height)}else this._radius=t.clone();this._changed(9)},isEmpty:function(){return!1},toPath:function(t){var n=this._clone(new(O[e.capitalize(this._type)])({center:new h,size:this._size,radius:this._radius,insert:!1}),t);return paper.settings.applyMatrix&&n.setApplyMatrix(!0),n},_draw:function(t,e,n){var i=this._style,r=i.hasFill(),s=i.hasStroke(),a=e.dontFinish||e.clip,o=!n;if(r||s||a){var h=this._type,u=this._radius,l="circle"===h;if(e.dontStart||t.beginPath(),o&&l)t.arc(0,0,u,0,2*Math.PI,!0);else{var c=l?u:u.width,d=l?u:u.height,_=this._size,f=_.width,g=_.height;if(o&&"rectangle"===h&&0===c&&0===d)t.rect(-f/2,-g/2,f,g);else{var v=f/2,p=g/2,m=.44771525016920644,y=c*m,w=d*m,x=[-v,-p+d,-v,-p+w,-v+y,-p,-v+c,-p,v-c,-p,v-y,-p,v,-p+w,v,-p+d,v,p-d,v,p-w,v-y,p,v-c,p,-v+c,p,-v+y,p,-v,p-w,-v,p-d];n&&n.transform(x,x,32),t.moveTo(x[0],x[1]),t.bezierCurveTo(x[2],x[3],x[4],x[5],x[6],x[7]),v!==c&&t.lineTo(x[8],x[9]),t.bezierCurveTo(x[10],x[11],x[12],x[13],x[14],x[15]),p!==d&&t.lineTo(x[16],x[17]),t.bezierCurveTo(x[18],x[19],x[20],x[21],x[22],x[23]),v!==c&&t.lineTo(x[24],x[25]),t.bezierCurveTo(x[26],x[27],x[28],x[29],x[30],x[31])}}t.closePath()}a||!r&&!s||(this._setStyles(t),r&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return"getBounds"!==t&&this.hasStroke()&&(n=n.expand(this.getStrokeWidth())),e?e._transformBounds(n):n}},new function(){function t(t,e,n){var i=t._radius;if(!i.isZero())for(var r=t._size.divide(2),s=0;4>s;s++){var a=new h(1&s?1:-1,s>1?1:-1),o=a.multiply(r),u=o.subtract(a.multiply(i)),l=new d(o,u);if((n?l.expand(n):l).contains(e))return u}}function e(t,e){var n=t.getAngleInRadians(),i=2*e.width,r=2*e.height,s=i*Math.sin(n),a=r*Math.cos(n);return i*r/(2*Math.sqrt(s*s+a*a))}return{_contains:function n(e){if("rectangle"===this._type){var i=t(this,e);return i?e.subtract(i).divide(this._radius).getLength()<=1:n.base.call(this,e)}return e.divide(this.size).getLength()<=.5},_hitTestSelf:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._type,o=this._radius,h=this.getStrokeWidth()+2*r.tolerance;if("rectangle"===a){var u=t(this,n,h);if(u){var l=n.subtract(u);s=2*Math.abs(l.getLength()-e(l,o))<=h}else{var c=new d(this._size).setCenter(0,0),_=c.expand(h),f=c.expand(-h);s=_._containsPoint(n)&&!f._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new S("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){var a=new x(e.getNamed(s));return a._type=t,a._size=i,a._radius=r,a.translate(n)}return{Circle:function(){var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t("circle",n,new l(2*i),i,arguments)},Rectangle:function(){var e=d.readNamed(arguments,"rectangle"),n=l.min(l.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=x._readEllipse(arguments),n=e.radius;return t("ellipse",e.center,n.multiply(2),n,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=h.readNamed(t,"center"),i=l.readNamed(t,"radius");else{var r=d.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),b=m.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{crossOrigin:null,source:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||("string"==typeof e?this.setSource(e):this.setImage(e)),this._size||(this._size=new l,this._loaded=!1)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new b(m.NO_INSERT),n=this._image,i=this._canvas;if(n)e.setImage(n);else if(i){var r=Y.getCanvas(this._size);r.getContext("2d").drawImage(i,0,0),e.setImage(r)}return e._crossOrigin=this._crossOrigin,this._clone(e,t)},getSize:function(){var t=this._size;return new c(t?t.width:0,t?t.height:0,this,"setSize")},setSize:function(){var t=l.read(arguments);if(!t.equals(this._size))if(t.width>0&&t.height>0){var e=this.getElement();this.setImage(Y.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}else this._canvas&&Y.release(this._canvas),this._size=t.clone()},getWidth:function(){return this._size?this._size.width:0},setWidth:function(t){this.setSize(t,this.getHeight())},getHeight:function(){return this._size?this._size.height:0},setHeight:function(t){this.setSize(this.getWidth(),t)},isEmpty:function(){var t=this._size;return!t||0===t.width&&0===t.height},getResolution:function(){var t=this._matrix,e=new h(0,0).transform(t),n=new h(1,0).transform(t).subtract(e),i=new h(0,1).transform(t).subtract(e);return new l(72/n.getLength(),72/i.getLength())},getPpi:"#getResolution",getImage:function(){return this._image},setImage:function(t){this._canvas&&Y.release(this._canvas),t&&t.getContext?(this._image=null,this._canvas=t,this._loaded=!0):(this._image=t,this._canvas=null,this._loaded=t&&t.complete),this._size=new l(t?t.naturalWidth||t.width:0,t?t.naturalHeight||t.height:0),this._context=null,this._changed(521)},getCanvas:function(){if(!this._canvas){var t=Y.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){Y.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(t){return this._context||(this._context=this.getCanvas().getContext("2d")),t&&(this._image=null,this._changed(513)),this._context},setContext:function(t){this._context=t},getSource:function(){return this._image&&this._image.src||this.toDataURL()},setSource:function(t){function e(){var t=i.getView();t&&(paper=t._scope,i.setImage(n),i.emit("load"),t.update())}var n,i=this,r=this._crossOrigin;n=document.getElementById(t)||new Image,r&&(n.crossOrigin=r),n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(q.add(n,{load:e}),n.src||(n.src=t)),this.setImage(n)},getCrossOrigin:function(){return this._image&&this._image.crossOrigin||this._crossOrigin||""},setCrossOrigin:function(t){this._crossOrigin=t,this._image&&(this._image.crossOrigin=t)},getElement:function(){return this._canvas||this._loaded&&this._image}},{beans:!1,getSubCanvas:function(){var t=d.read(arguments),e=Y.getContext(t.getSize());return e.drawImage(this.getCanvas(),t.x,t.y,t.width,t.height,0,0,t.width,t.height),e.canvas},getSubRaster:function(){var t=d.read(arguments),e=new b(m.NO_INSERT);return e.setImage(this.getSubCanvas(t)),e.translate(t.getCenter().subtract(this.getSize().divide(2))),e._matrix.preConcatenate(this._matrix),e.insertAbove(this),e},toDataURL:function(){var t=this._image&&this._image.src;if(/^data:/.test(t))return t;var e=this.getCanvas();return e?e.toDataURL.apply(e,arguments):null},drawImage:function(t){var e=h.read(arguments,1);this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var n,i;t?t instanceof k?(i=t,n=t.getBounds()):t.width?n=new d(t):t.x&&(n=new d(t.x-.5,t.y-.5,1,1)):n=this.getBounds();var r=32,s=Math.min(n.width,r),a=Math.min(n.height,r),o=b._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=b._sampleContext=Y.getContext(new l(r)),o.save();var h=(new f).scale(s/n.width,a/n.height).translate(-n.x,-n.y);h.applyToContext(o),i&&i.draw(o,new e({clip:!0,matrices:[h]})),this._matrix.applyToContext(o);var u=this.getElement(),c=this._size;u&&o.drawImage(u,-c.width/2,-c.height/2),o.restore();for(var _=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,g=[0,0,0],v=0,p=0,m=_.length;m>p;p+=4){var y=_[p+3];v+=y,y/=255,g[0]+=_[p]*y,g[1]+=_[p+1]*y,g[2]+=_[p+2]*y}for(var p=0;3>p;p++)g[p]/=v;return v?B.read(g):null},getPixel:function(){var t=h.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new B("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=B.read(arguments),n=e._convert("rgb"),i=e._alpha,r=this.getContext(!0),s=r.createImageData(1,1),a=s.data;a[0]=255*n[0],a[1]=255*n[1],a[2]=255*n[2],a[3]=null!=i?255*i:255,r.putImageData(s,t.x,t.y)},createImageData:function(){var t=l.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(){var t=d.read(arguments);return t.isEmpty()&&(t=new d(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t){var e=h.read(arguments,1);this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTestSelf:function(t){if(this._contains(t)){var e=this;return new S("pixel",e,{offset:t.add(e._size.divide(2)).round(),color:{get:function(){return e.getPixel(this.offset)}}})}},_draw:function(t){var e=this.getElement();e&&(t.globalAlpha=this._opacity,t.drawImage(e,-this._size.width/2,-this._size.height/2))},_canComposite:function(){return!0}}),C=m.extend({_class:"PlacedSymbol",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:{getBounds:"getStrokeBounds"},_boundsSelected:!0,_serializeFields:{symbol:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||this.setSymbol(e instanceof p?e:new p(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(9)},clone:function(t){var e=new C(m.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,n){var i=this.symbol._definition;return i._getCachedBounds(t,e&&e.chain(i._matrix),n)},_hitTestSelf:function(t,e){var n=this._symbol._definition._hitTest(t,e);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),S=e.extend({_class:"HitResult",initialize:function(t,e,n){this.type=t,this.item=e,n&&(n.enumerable=!0,this.inject(n))},statics:{getOptions:function(t){return new e({type:null,tolerance:paper.settings.hitTolerance,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1},t)}}}),P=e.extend({_class:"Segment",beans:!0,initialize:function(e,n,i,r,s,a){var o,h,u,l=arguments.length;0===l||(1===l?"point"in e?(o=e.point,h=e.handleIn,u=e.handleOut):o=e:2===l&&"number"==typeof e?o=arguments:3>=l?(o=e,h=n,u=i):(o=e!==t?[e,n]:null,h=i!==t?[i,r]:null,u=s!==t?[s,a]:null)),new M(o,this,"_point"),new M(h,this,"_handleIn"),new M(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.hasHandles()?[this._point,this._handleIn,this._handleOut]:this._point,t,!0)},_changed:function(t){var e=this._path;if(e){var n,i=e._curves,r=this._index;i&&(t&&t!==this._point&&t!==this._handleIn||!(n=r>0?i[r-1]:e._closed?i[i.length-1]:null)||n._changed(),t&&t!==this._point&&t!==this._handleOut||!(n=i[r])||n._changed()),e._changed(25)}},getPoint:function(){return this._point},setPoint:function(){var t=h.read(arguments);this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(){var t=h.read(arguments);this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(){var t=h.read(arguments);this._handleOut.set(t.x,t.y)},hasHandles:function(){return!this._handleIn.isZero()||!this._handleOut.isZero()},clearHandles:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},_selectionState:0,isSelected:function(t){var e=this._selectionState;return t?t===this._point?!!(4&e):t===this._handleIn?!!(1&e):t===this._handleOut?!!(2&e):!1:!!(7&e)},setSelected:function(t,e){var n=this._path,t=!!t,i=this._selectionState,r=i,s=e?e===this._point?4:e===this._handleIn?1:e===this._handleOut?2:0:7;t?i|=s:i&=~s,this._selectionState=i,n&&i!==r&&(n._updateSelection(this,r,i),n._changed(129))},getIndex:function(){return this._index!==t?this._index:null},getPath:function(){return this._path||null},getCurve:function(){var t=this._path,e=this._index;return t?(e>0&&!t._closed&&e===t._segments.length-1&&e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new z(t,this===t._segment1?0:1):null},getNext:function(){var t=this._path&&this._path._segments;return t&&(t[this._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._segments;return t&&(t[this._index-1]||this._path._closed&&t[t.length-1])||null},isFirst:function(){return 0===this._index},isLast:function(){var t=this._path;return t&&this._index===t._segments.length-1||!1},reverse:function(){var t=this._handleIn,e=this._handleOut,n=t._x,i=t._y;t.set(e._x,e._y),e.set(n,i)},reversed:function(){return new P(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new P(this._point,this._handleIn,this._handleOut)},equals:function(t){return t===this||t&&this._class===t._class&&this._point.equals(t._point)&&this._handleIn.equals(t._handleIn)&&this._handleOut.equals(t._handleOut)||!1},toString:function(){var t=["point: "+this._point];return this._handleIn.isZero()||t.push("handleIn: "+this._handleIn),this._handleOut.isZero()||t.push("handleOut: "+this._handleOut),"{ "+t.join(", ")+" }"},transform:function(t){this._transformCoordinates(t,Array(6),!0),this._changed()},_transformCoordinates:function(t,e,n){var i=this._point,r=n&&this._handleIn.isZero()?null:this._handleIn,s=n&&this._handleOut.isZero()?null:this._handleOut,a=i._x,o=i._y,h=2;return e[0]=a,e[1]=o,r&&(e[h++]=r._x+a,e[h++]=r._y+o),s&&(e[h++]=s._x+a,e[h++]=s._y+o),t&&(t._transformCoordinates(e,e,h/2),a=e[0],o=e[1],n?(i._x=a,i._y=o,h=2,r&&(r._x=e[h++]-a,r._y=e[h++]-o),s&&(s._x=e[h++]-a,s._y=e[h++]-o)):(r||(e[h++]=a,e[h++]=o),s||(e[h++]=a,e[h++]=o))),e}}),M=h.extend({initialize:function(e,n,i){var r,s,a;if(e)if((r=e[0])!==t)s=e[1];else{var o=e;(r=o.x)===t&&(o=h.read(arguments),r=o.x),s=o.y,a=o.selected}else r=s=0;this._x=r,this._y=s,this._owner=n,n[i]=this,a&&this.setSelected(!0)},set:function(t,e){return this._x=t,this._y=e,this._owner._changed(this),this},_serialize:function(t){var e=t.formatter,n=e.number(this._x),i=e.number(this._y);return this.isSelected()?{x:n,y:i,selected:!0}:[n,i]},getX:function(){return this._x},setX:function(t){this._x=t,this._owner._changed(this)},getY:function(){return this._y},setY:function(t){this._y=t,this._owner._changed(this)},isZero:function(){return a.isZero(this._x)&&a.isZero(this._y)},setSelected:function(t){this._owner.setSelected(t,this)},isSelected:function(){return this._owner.isSelected(this)}}),I=e.extend({_class:"Curve",initialize:function(t,e,n,i,r,s,a,o){var h,u,l,c,d,_,f=arguments.length;3===f?(this._path=t,h=e,u=n):0===f?(h=new P,u=new P):1===f?"segment1"in t?(h=new P(t.segment1),u=new P(t.segment2)):"point1"in t?(l=t.point1,d=t.handle1,_=t.handle2,c=t.point2):Array.isArray(t)&&(l=[t[0],t[1]],c=[t[6],t[7]],d=[t[2]-t[0],t[3]-t[1]],_=[t[4]-t[6],t[5]-t[7]]):2===f?(h=new P(t),u=new P(e)):4===f?(l=t,d=e,_=n,c=i):8===f&&(l=[t,e],c=[a,o],d=[n-t,i-e],_=[r-a,s-o]),this._segment1=h||new P(l,null,d),this._segment2=u||new P(c,_,null)},_serialize:function(t){return e.serialize(this.hasHandles()?[this.getPoint1(),this.getHandle1(),this.getHandle2(),this.getPoint2()]:[this.getPoint1(),this.getPoint2()],t,!0)},_changed:function(){this._length=this._bounds=t},clone:function(){return new I(this._segment1,this._segment2)},toString:function(){var t=["point1: "+this._segment1._point];return this._segment1._handleOut.isZero()||t.push("handle1: "+this._segment1._handleOut),this._segment2._handleIn.isZero()||t.push("handle2: "+this._segment2._handleIn),t.push("point2: "+this._segment2._point),"{ "+t.join(", ")+" }"},remove:function(){var t=!1;if(this._path){var e=this._segment2,n=e._handleOut;t=e.remove(),t&&this._segment1._handleOut.set(n.x,n.y)}return t},getPoint1:function(){return this._segment1._point},setPoint1:function(){var t=h.read(arguments);this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(){var t=h.read(arguments);this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(){var t=h.read(arguments);this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(){var t=h.read(arguments);this._segment2._handleIn.set(t.x,t.y)},getSegment1:function(){return this._segment1},getSegment2:function(){return this._segment2},getPath:function(){return this._path},getIndex:function(){return this._segment1._index},getNext:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index-1]||this._path._closed&&t[t.length-1])||null},isFirst:function(){return 0===this._segment1._index},isLast:function(){var t=this._path;return t&&this._segment1._index===t._curves.length-1||!1},isSelected:function(){return this.getPoint1().isSelected()&&this.getHandle2().isSelected()&&this.getHandle2().isSelected()&&this.getPoint2().isSelected()},setSelected:function(t){this.getPoint1().setSelected(t),this.getHandle1().setSelected(t),this.getHandle2().setSelected(t),this.getPoint2().setSelected(t)},getValues:function(t){return I.getValues(this._segment1,this._segment2,t)},getPoints:function(){for(var t=this.getValues(),e=[],n=0;8>n;n+=2)e.push(new h(t[n],t[n+1]));return e},getLength:function(){return null==this._length&&(this._length=I.getLength(this.getValues(),0,1)),this._length},getArea:function(){return I.getArea(this.getValues())},getLine:function(){return new g(this._segment1._point,this._segment2._point)},getPart:function(t,e){return new I(I.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return I.getLength(this.getValues(),t,e)},getIntersections:function(t){return I._getIntersections(this.getValues(),t&&t!==this?t.getValues():null,this,t,[],{})},_getParameter:function(e,n){return n?e:e&&e.curve===this?e.parameter:e===t&&n===t?.5:this.getParameterAt(e,0)},divide:function(t,e,n){var i=this._getParameter(t,e),r=4e-7,s=1-r,a=null;if(i>=r&&s>=i){var o=I.subdivide(this.getValues(),i),u=o[0],l=o[1],c=n||this.hasHandles(),d=this._segment1,_=this._segment2,f=this._path;c&&(d._handleOut.set(u[2]-u[0],u[3]-u[1]),_._handleIn.set(l[4]-l[6],l[5]-l[7]));var g=u[6],v=u[7],p=new P(new h(g,v),c&&new h(u[4]-g,u[5]-v),c&&new h(l[2]-g,l[3]-v));f?(f.insert(d._index+1,p),a=this.getNext()):(this._segment2=p,a=new I(p,_))}return a},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},reversed:function(){return new I(this._segment2.reversed(),this._segment1.reversed())},clearHandles:function(){this._segment1._handleOut.set(0,0),this._segment2._handleIn.set(0,0)},statics:{getValues:function(t,e,n){var i=t._point,r=t._handleOut,s=e._handleIn,a=e._point,o=[i._x,i._y,i._x+r._x,i._y+r._y,a._x+s._x,a._y+s._y,a._x,a._y];return n&&n._transformCoordinates(o,o,4),o},subdivide:function(e,n){var i=e[0],r=e[1],s=e[2],a=e[3],o=e[4],h=e[5],u=e[6],l=e[7];n===t&&(n=.5);var c=1-n,d=c*i+n*s,_=c*r+n*a,f=c*s+n*o,g=c*a+n*h,v=c*o+n*u,p=c*h+n*l,m=c*d+n*f,y=c*_+n*g,w=c*f+n*v,x=c*g+n*p,b=c*m+n*w,C=c*y+n*x;return[[i,r,d,_,m,y,b,C],[b,C,w,x,v,p,u,l]]},solveCubic:function(t,e,n,i,r,s){var o=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-o),d=3*(u-h)-c,_=l-o-c-d;return a.solveCubic(_,d,c,o-n,i,r,s)},getParameterOf:function(t,e){var n=new h(t[0],t[1]),i=new h(t[6],t[7]),r=1e-12,s=e.isClose(n,r)?0:e.isClose(i,r)?1:null;if(null!==s)return s;for(var a=[e.x,e.y],o=[],u=2e-7,l=0;2>l;l++)for(var c=I.solveCubic(t,l,a[l],o,0,1),d=0;c>d;d++)if(s=o[d],e.isClose(I.getPoint(t,s),u))return s;return e.isClose(n,u)?0:e.isClose(i,u)?1:null},getNearestParameter:function(t,e){function n(n){if(n>=0&&1>=n){var i=e.getDistance(I.getPoint(t,n),!0);if(_>i)return _=i,f=n,!0}}if(I.isStraight(t)){var i=t[0],r=t[1],s=t[6],a=t[7],o=s-i,u=a-r,l=o*o+u*u;if(0===l)return 0;var c=((e.x-i)*o+(e.y-r)*u)/l;return 1e-12>c?0:c>.999999999999?1:I.getParameterOf(t,new h(i+c*o,r+c*u))}for(var d=100,_=1/0,f=0,g=0;d>=g;g++)n(g/d);for(var v=1/(2*d);v>4e-7;)n(f-v)||n(f+v)||(v/=2);return f},getPart:function(t,e,n){var i=e>n;if(i){var r=e;e=n,n=r}return e>0&&(t=I.subdivide(t,e)[1]),1>n&&(t=I.subdivide(t,(n-e)/(1-e))[0]),i?[t[6],t[7],t[4],t[5],t[2],t[3],t[0],t[1]]:t},hasHandles:function(t){var e=a.isZero;return!(e(t[0]-t[2])&&e(t[1]-t[3])&&e(t[4]-t[6])&&e(t[5]-t[7]))},isFlatEnough:function(t,e){var n=t[0],i=t[1],r=t[2],s=t[3],a=t[4],o=t[5],h=t[6],u=t[7],l=3*r-2*n-h,c=3*s-2*i-u,d=3*a-2*h-n,_=3*o-2*u-i;return Math.max(l*l,d*d)+Math.max(c*c,_*_)<10*e*e},getArea:function(t){var e=t[0],n=t[1],i=t[6],r=t[7],s=(t[2]+e)/2,a=(t[3]+n)/2,o=(t[4]+t[6])/2,h=(t[5]+t[7])/2;return 6*((e-s)*(a+n)+(s-o)*(h+a)+(o-i)*(r+h))/10},getBounds:function(t){for(var e=t.slice(0,2),n=e.slice(),i=[0,0],r=0;2>r;r++)I._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,n,i);return new d(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,o,h,u){function l(t,e){var n=t-e,i=t+e;nh[r]&&(h[r]=i)}var c=3*(e-n)-t+i,d=2*(t+n)-4*e,_=e-t,f=a.solveQuadratic(c,d,_,u),g=4e-7,v=1-g;l(i,0);for(var p=0;f>p;p++){var m=u[p],y=1-m;m>g&&v>m&&l(y*y*y*t+3*y*y*m*e+3*y*m*m*n+m*m*m*i,s)}}}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){this._bounds||(this._bounds={});var e=this._bounds[t];if(!e){var n=this._path;e=this._bounds[t]=O[t]([this._segment1,this._segment2],!1,n&&n.getStyle())}return e.clone()}},{}),e.each({isStraight:function(t,e,n){if(e.isZero()&&n.isZero())return!0;if(t.isZero())return!1;if(e.isCollinear(t)&&n.isCollinear(t)){var i=t.dot(t),r=t.dot(e)/i,s=t.dot(n)/i;return r>=0&&1>=r&&0>=s&&s>=-1}return!1},isLinear:function(t,e,n){var i=t.divide(3);return e.equals(i)&&n.negate().equals(i)}},function(t,e){this[e]=function(){var e=this._segment1,n=this._segment2;return t(n._point.subtract(e._point),e._handleOut,n._handleIn)},this.statics[e]=function(e){var n=e[0],i=e[1],r=e[6],s=e[7];return t(new h(r-n,s-i),new h(e[2]-n,e[3]-i),new h(e[4]-r,e[5]-s))}},{statics:{},hasHandles:function(){return!this._segment1._handleOut.isZero()||!this._segment2._handleIn.isZero()},isCollinear:function(t){return t&&this.isStraight()&&t.isStraight()&&this.getLine().isCollinear(t.getLine())},isHorizontal:function(){return this.isStraight()&&Math.abs(this.getTangentAt(.5,!0).y)<1e-7},isVertical:function(){return this.isStraight()&&Math.abs(this.getTangentAt(.5,!0).x)<1e-7}}),{beans:!1,getParameterAt:function(t,e){return I.getParameterAt(this.getValues(),t,e)},getParameterOf:function(){return I.getParameterOf(this.getValues(),h.read(arguments))},getLocationAt:function(t,e){var n=e?t:this.getParameterAt(t);return null!=n&&n>=0&&1>=n?new z(this,n):null},getLocationOf:function(){return this.getLocationAt(this.getParameterOf(h.read(arguments)),!0)},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getNearestLocation:function(){var t=h.read(arguments),e=this.getValues(),n=I.getNearestParameter(e,t),i=I.getPoint(e,n);return new z(this,n,i,null,t.getDistance(i))},getNearestPoint:function(){return this.getNearestLocation.apply(this,arguments).getPoint()}},new function(){var t=["getPoint","getTangent","getNormal","getWeightedTangent","getWeightedNormal","getCurvature"];return e.each(t,function(t){this[t+"At"]=function(e,n){var i=this.getValues();return I[t](i,n?e:I.getParameterAt(i,e,0))}},{statics:{evaluateMethods:t}})},new function(){function e(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7],u=9*(i-s)+3*(o-e),l=6*(e+s)-12*i,c=3*(i-e),d=9*(r-a)+3*(h-n),_=6*(n+a)-12*r,f=3*(r-n);return function(t){var e=(u*t+l)*t+c,n=(d*t+_)*t+f;return Math.sqrt(e*e+n*n)}}function n(t,e){return Math.max(2,Math.min(16,Math.ceil(32*Math.abs(e-t))))}function i(t,e,n,i){if(null==e||0>e||e>1)return null;var r,s,a=t[0],o=t[1],u=t[2],l=t[3],c=t[4],d=t[5],_=t[6],f=t[7],g=4e-7,v=1-g;if(0===n&&(g>e||e>v)){var p=g>e;r=p?a:_,s=p?o:f}else{var m=3*(u-a),y=3*(c-u)-m,w=_-a-m-y,x=3*(l-o),b=3*(d-l)-x,C=f-o-x-b;if(0===n)r=((w*e+y)*e+m)*e+a,s=((C*e+b)*e+x)*e+o;else{if(g>e?(r=m,s=x):e>v?(r=3*(_-c),s=3*(f-d)):(r=(3*w*e+2*y)*e+m,s=(3*C*e+2*b)*e+x),i){0===r&&0===s&&(g>e||e>v)&&(r=c-u,s=d-l);var S=Math.sqrt(r*r+s*s);S&&(r/=S,s/=S)}if(3===n){var P=6*w*e+2*y,M=6*C*e+2*b,I=Math.pow(r*r+s*s,1.5);r=0!==I?(r*M-s*P)/I:0,s=0}}}return 2===n?new h(s,-r):new h(r,s)}return{statics:{getLength:function(i,r,s){if(r===t&&(r=0),s===t&&(s=1),0===r&&1===s&&I.isStraight(i)){var o=i[6]-i[0],h=i[7]-i[1];return Math.sqrt(o*o+h*h)}var u=e(i);return a.integrate(u,r,s,n(r,s))},getParameterAt:function(i,r,s){function o(t){return g+=a.integrate(d,s,t,n(s,t)),s=t,g-r}if(s===t&&(s=0>r?1:0),0===r)return s;var h=Math.abs,u=r>0,l=u?s:0,c=u?1:s,d=e(i),_=a.integrate(d,l,c,n(l,c));if(h(r-_)<1e-12)return u?c:l;if(h(r)>_)return null;var f=r/_,g=0;return a.findRoot(o,d,s+f,l,c,32,1e-12)},getPoint:function(t,e){return i(t,e,0,!1)},getTangent:function(t,e){return i(t,e,1,!0)},getWeightedTangent:function(t,e){return i(t,e,1,!1)},getNormal:function(t,e){return i(t,e,2,!0)},getWeightedNormal:function(t,e){return i(t,e,2,!1)},getCurvature:function(t,e){return i(t,e,3,!1).x}}}},new function(){function t(t,e,n,i,r,s,a,o,h,u,l){var c=e.startConnected,d=e.endConnected,_=4e-7,f=1-_;if(null==r&&(r=I.getParameterOf(n,s)),null!==r&&r>=(c?_:0)&&(d?f:1)>=r&&(null==h&&(h=I.getParameterOf(a,u)),null!==h&&h>=(d?_:0)&&(c?f:1)>=h)){var g=e.renormalize;if(g){var v=g(r,h);r=v[0],h=v[1]}var p=new z(i,r,s||I.getPoint(n,r),l),m=new z(o,h,u||I.getPoint(a,h),l),y=p.getPath()===m.getPath()&&p.getIndex()>m.getIndex(),w=y?m:p,x=e.include;p._intersection=m,m._intersection=p,(!x||x(w))&&z.insert(t,w,!0)}}function e(r,s,a,o,h,u,l,c,d,_,f,v,p){if(!(++p>=24)){var m,y,w=s[0],x=s[1],b=s[6],C=s[7],S=g.getSignedDistance,P=S(w,x,b,C,s[2],s[3]),M=S(w,x,b,C,s[4],s[5]),z=P*M>0?.75:4/9,k=z*Math.min(0,P,M),O=z*Math.max(0,P,M),A=S(w,x,b,C,r[0],r[1]),T=S(w,x,b,C,r[2],r[3]),N=S(w,x,b,C,r[4],r[5]),L=S(w,x,b,C,r[6],r[7]),E=n(A,T,N,L),B=E[0],j=E[1];if(null!=(m=i(B,j,k,O))&&null!=(y=i(B.reverse(),j.reverse(),k,O))){r=I.getPart(r,m,y);var D=y-m,R=l+(c-l)*m,F=l+(c-l)*y;if(f>.5&&D>.5)if(F-R>_-d){var q=I.subdivide(r,.5),V=R+(F-R)/2;e(s,q[0],o,a,h,u,d,_,R,V,D,!v,p),e(s,q[1],o,a,h,u,d,_,V,F,D,!v,p)}else{var q=I.subdivide(s,.5),V=d+(_-d)/2;e(q[0],r,o,a,h,u,d,V,R,F,D,!v,p),e(q[1],r,o,a,h,u,V,_,R,F,D,!v,p)}else if(Math.max(_-d,F-R)<1e-7){var H=R+(F-R)/2,Z=d+(_-d)/2;r=a.getValues(),s=o.getValues(),t(h,u,v?s:r,v?o:a,v?Z:H,null,v?r:s,v?a:o,v?H:Z,null)}else D>1e-12&&e(s,r,o,a,h,u,d,_,R,F,D,!v,p)}}}function n(t,e,n,i){var r,s=[0,t],a=[1/3,e],o=[2/3,n],h=[1,i],u=e-(2*t+i)/3,l=n-(t+2*i)/3;if(0>u*l)r=[[s,a,h],[s,o,h]];else{var c=u/l;r=[c>=2?[s,a,h]:.5>=c?[s,o,h]:[s,a,o,h],[s,h]]}return 0>(u||l)?r.reverse():r}function i(t,e,n,i){return t[0][1]i?r(e,!1,i):t[0][0]}function r(t,e,n){for(var i=t[0][0],r=t[0][1],s=1,a=t.length;a>s;s++){var o=t[s][0],h=t[s][1];if(e?h>=n:n>=h)return h===n?o:i+(n-r)*(o-i)/(h-r);i=o,r=h}return null}function s(e,n,i,r,s,o){for(var h=I.isStraight(e),u=h?n:e,l=h?e:n,c=l[0],d=l[1],_=l[6],f=l[7],g=_-c,v=f-d,p=Math.atan2(-v,g),m=Math.sin(p),y=Math.cos(p),w=[],x=0;8>x;x+=2){var b=u[x]-c,C=u[x+1]-d;w.push(b*y-C*m,b*m+C*y)}for(var S=[],P=I.solveCubic(w,1,0,S,0,1),x=0;P>x;x++){var M=S[x],z=I.getPoint(u,M),k=I.getParameterOf(l,z);if(null!==k){var O=I.getPoint(l,k),A=h?k:M,T=h?M:k;(!o.endConnected||T>a.CURVETIME_EPSILON)&&t(s,o,e,i,A,h?O:z,n,r,T,h?z:O)}}}function o(e,n,i,r,s,a){var o=g.intersect(e[0],e[1],e[6],e[7],n[0],n[1],n[6],n[7]);o&&t(s,a,e,i,null,o,n,r,null,o)}return{statics:{_getIntersections:function(n,i,r,a,u,l){if(!i)return I._getSelfIntersection(n,r,u,l);var c=n[0],d=n[1],_=n[6],f=n[7],g=i[0],v=i[1],p=i[6],m=i[7],y=(3*n[2]+c)/4,w=(3*n[3]+d)/4,x=(3*n[4]+_)/4,b=(3*n[5]+f)/4,C=(3*i[2]+g)/4,S=(3*i[3]+v)/4,P=(3*i[4]+p)/4,M=(3*i[5]+m)/4,z=Math.min,k=Math.max;if(!(k(c,y,x,_)>=z(g,C,P,p)&&z(c,y,x,_)<=k(g,C,P,p)&&k(d,w,b,f)>=z(v,S,M,m)&&z(d,w,b,f)<=k(v,S,M,m)))return u;if(!l.startConnected&&!l.endConnected){var O=I.getOverlaps(n,i);if(O){for(var A=0;2>A;A++){var T=O[A];t(u,l,n,r,T[0],null,i,a,T[1],null,!0)}return u}}var N=I.isStraight(n),L=I.isStraight(i),E=N&&L,B=1e-12,j=u.length;if((E?o:N||L?s:e)(n,i,r,a,u,l,0,1,0,1,0,!1,0),E&&u.length>j)return u;var D=new h(c,d),R=new h(_,f),F=new h(g,v),q=new h(p,m);return D.isClose(F,B)&&t(u,l,n,r,0,D,i,a,0,F),!l.startConnected&&D.isClose(q,B)&&t(u,l,n,r,0,D,i,a,1,q),!l.endConnected&&R.isClose(F,B)&&t(u,l,n,r,1,R,i,a,0,F),R.isClose(q,B)&&t(u,l,n,r,1,R,i,a,1,q),u},_getSelfIntersection:function(t,e,n,i){var r=t[0],s=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],_=t[7],f=new g(r,s,d,_,!1),v=f.getSide(new h(o,u),!0),p=f.getSide(new h(l,c),!0);if(v===p){var m=(r-l)*(u-_)+(o-d)*(c-s);if(m*v>0)return n}var y=d-3*l+3*o-r,w=l-2*o+r,x=o-r,b=_-3*c+3*u-s,C=c-2*u+s,S=u-s,P=b*x-y*S,M=b*w-y*C,z=C*x-w*S;if(0>P*P-4*M*z){var k,O=[],A=a.solveCubic(y*y+b*b,3*(y*w+b*C),2*(w*w+C*C)+y*x+b*S,w*x+C*S,O,0,1);if(A>0){for(var T=0,N=0;A>T;T++){var L=Math.abs(e.getCurvatureAt(O[T],!0));L>N&&(N=L,k=O[T])}var E=I.subdivide(t,k);i.endConnected=!0,i.renormalize=function(t,e){return[t*k,e*(1-k)+k]},I._getIntersections(E[0],E[1],e,e,n,i)}}return n},getOverlaps:function(t,e){function n(t){var e=t[6]-t[0],n=t[7]-t[1];return e*e+n*n}var i=Math.abs,r=4e-7,s=2e-7,a=I.isStraight(t),o=I.isStraight(e),u=a&&o;if(u){var l=n(t)s||_.getDistance(new h(d[6],d[7]))>s)return null}else if(a^o)return null;for(var f=[t,e],v=[],p=0,m=0;2>p&&v.length<2;p+=0===m?0:1,m=1^m){var y=I.getParameterOf(f[1^p],new h(f[p][0===m?0:6],f[p][0===m?1:7]));if(null!=y){var w=0===p?[m,y]:[y,m];(0===v.length||i(w[0]-v[0][0])>r&&i(w[1]-v[0][1])>r)&&v.push(w)}if(1===p&&0===v.length)break}if(2!==v.length)v=null;else if(!u){var x=I.getPart(t,v[0][0],v[1][0]),b=I.getPart(e,v[0][1],v[1][1]);(i(b[2]-x[2])>s||i(b[3]-x[3])>s||i(b[4]-x[4])>s||i(b[5]-x[5])>s)&&(v=null)}return v}}}}),z=e.extend({_class:"CurveLocation",beans:!0,initialize:function ft(t,e,n,i,r){if(e>.9999996){var s=t.getNext();s&&(e=0,t=s)}this._id=o.get(ft),this._setCurve(t),this._parameter=e,this._point=n||t.getPointAt(e,!0),this._overlap=i,this._distance=r,this._intersection=this._next=this._prev=null},_setCurve:function(t){var e=t._path; +this._version=e?e._version:0,this._curve=t,this._segment=null,this._segment1=t._segment1,this._segment2=t._segment2},_setSegment:function(t){this._setCurve(t.getCurve()),this._segment=t,this._parameter=t===this._segment1?0:1,this._point=t._point.clone()},getSegment:function(){var t=this.getCurve(),e=this._segment;if(!e){var n=this.getParameter();0===n?e=t._segment1:1===n?e=t._segment2:null!=n&&(e=t.getPartLength(0,n)r;r++)t+=i[r].getLength();this._offset=t+=this.getCurveOffset()}return t},getCurveOffset:function(){var t=this.getCurve(),e=this.getParameter();return null!=e&&t&&t.getPartLength(0,e)},getIntersection:function(){return this._intersection},getDistance:function(){return this._distance},divide:function(){var t=this.getCurve(),e=null;return t&&(e=t.divide(this.getParameter(),!0),e&&this._setSegment(e._segment1)),e},split:function(){var t=this.getCurve();return t?t.split(this.getParameter(),!0):null},equals:function(t,e){var n=this===t,i=2e-7;if(!n&&t instanceof z&&this.getPath()===t.getPath()&&this.getPoint().isClose(t.getPoint(),i)){var r=this.getCurve(),s=t.getCurve(),a=Math.abs,o=a((r.isLast()&&s.isFirst()?-1:r.getIndex())+this.getParameter()-((s.isLast()&&r.isFirst()?-1:s.getIndex())+t.getParameter()));n=(4e-7>o||(o=a(this.getOffset()-t.getOffset()))e?t>e&&n>t:t>e&&l>=t||t>=-l&&n>t}var e=this._intersection;if(!e)return!1;var n=this.getParameter(),i=e.getParameter(),r=4e-7,s=1-r;if(n>=r&&s>=n||i>=r&&s>=i)return!this.isTouching();var a=this.getCurve(),o=a.getPrevious(),h=e.getCurve(),u=h.getPrevious(),l=Math.PI;if(!o||!u)return!1;var c=o.getTangentAt(s,!0).negate().getAngleInRadians(),d=a.getTangentAt(r,!0).getAngleInRadians(),_=u.getTangentAt(s,!0).negate().getAngleInRadians(),f=h.getTangentAt(r,!0).getAngleInRadians();return t(_,c,d)^t(f,c,d)&&t(_,d,c)^t(f,d,c)},isOverlap:function(){return!!this._overlap}},e.each(I.evaluateMethods,function(t){var e=t+"At";this[t]=function(){var t=this.getParameter(),n=this.getCurve();return null!=t&&n&&n[e](t,!0)}},{preserve:!0}),new function(){function t(t,e,n){function i(n,i){for(var s=n+i;s>=-1&&r>=s;s+=i){var a=t[(s%r+r)%r];if(!e.getPoint().isClose(a.getPoint(),2e-7))break;if(e.equals(a))return a}return null}for(var r=t.length,s=0,a=r-1;a>=s;){var o,h=s+a>>>1,u=t[h];if(n&&(o=e.equals(u)?u:i(h,-1)||i(h,1)))return e._overlap&&(o._overlap=o._intersection._overlap=!0),o;var l=e.getPath(),c=u.getPath(),d=l===c?e.getIndex()+e.getParameter()-(u.getIndex()+u.getParameter()):l._id-c._id;0>d?a=h-1:s=h+1}return t.splice(s,0,e),e}return{statics:{insert:t,expand:function(e){for(var n=e.slice(),i=0,r=e.length;r>i;i++)t(n,e[i]._intersection,!1);return n}}}}),k=m.extend({_class:"PathItem",initialize:function(){},getIntersections:function(t,e,n,i){var r=this===t||!t,s=this._matrix.orNullIfIdentity(),a=r?s:(n||t._matrix).orNullIfIdentity();if(!r&&!this.getBounds(s).touches(t.getBounds(a)))return[];for(var o,t,h=this.getCurves(),u=r?h:t.getCurves(),l=h.length,c=r?l:u.length,d=[],_=[],f=0;c>f;f++)d[f]=u[f].getValues(a);for(var f=0;l>f;f++){var g=h[f],v=r?d[f]:g.getValues(s),p=g.getPath();p!==t&&(t=p,o=[],_.push(o)),r&&I._getSelfIntersection(v,g,o,{include:e,startConnected:1===l&&g.getPoint1().equals(g.getPoint2())});for(var m=r?f+1:0;c>m;m++){if(i&&o.length)return o;var y=u[m];I._getIntersections(v,d[m],g,y,o,{include:e,startConnected:r&&g.getPrevious()===y,endConnected:r&&g.getNext()===y})}}o=[];for(var f=0,w=_.length;w>f;f++)o.push.apply(o,_[f]);return o},getCrossings:function(t){return this.getIntersections(t,function(t){return t.isCrossing()})},_asPathItem:function(){return this},setPathData:function(t){function e(t,e){var n=+i[t];return o&&(n+=u[e]),n}function n(t){return new h(e(t,"x"),e(t+1,"y"))}var i,r,s,a=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),o=!1,u=new h,c=new h;this.clear();for(var d=0,_=a&&a.length;_>d;d++){var f=a[d],g=f[0],v=g.toLowerCase();i=f.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var p=i&&i.length;switch(o=g===v,"z"!==r||/[mz]/.test(v)||this.moveTo(u=c),v){case"m":case"l":for(var m="m"===v,y=0;p>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=n(y));s=u,m&&(c=u);break;case"h":case"v":for(var w="h"===v?"x":"y",y=0;p>y;y++)u[w]=e(y,w),this.lineTo(u);s=u;break;case"c":for(var y=0;p>y;y+=6)this.cubicCurveTo(n(y),s=n(y+2),u=n(y+4));break;case"s":for(var y=0;p>y;y+=4)this.cubicCurveTo(/[cs]/.test(r)?u.multiply(2).subtract(s):u,s=n(y),u=n(y+2)),r=v;break;case"q":for(var y=0;p>y;y+=4)this.quadraticCurveTo(s=n(y),u=n(y+2));break;case"t":for(var y=0;p>y;y+=2)this.quadraticCurveTo(s=/[qt]/.test(r)?u.multiply(2).subtract(s):u,u=n(y)),r=v;break;case"a":for(var y=0;p>y;y+=7)this.arcTo(u=n(y+5),new l(+i[y],+i[y+1]),+i[y+2],+i[y+4],+i[y+3]);break;case"z":this.closePath(!0)}r=v}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t,!1,!0);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),O=k.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[],this._version=0;var n=Array.isArray(e)?"object"==typeof e[0]?e:arguments:!e||e.size!==t||e.x===t&&e.point===t?null:arguments;n&&n.length>0?this.setSegments(n):(this._curves=t,this._selectedSegmentState=0,n||"string"!=typeof e||(this.setPathData(e),e=null)),this._initialize(!n&&e)},_equals:function(t){return this._closed===t._closed&&e.equals(this._segments,t._segments)},clone:function(e){var n=new O(m.NO_INSERT);return n.setSegments(this._segments),n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),this._clone(n,e)},_changed:function gt(e){if(gt.base.call(this,e),8&e){var n=this._parent;if(n&&(n._currentPath=t),this._length=this._area=this._clockwise=this._monoCurves=t,16&e)this._version++;else if(this._curves)for(var i=0,r=this._curves.length;r>i;i++)this._curves[i]._changed()}else 32&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof A?t:this)._style},getSegments:function(){return this._segments},setSegments:function(e){var n=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,this._curves=t,e&&e.length>0&&this._add(P.readAll(e)),n&&this.setFullySelected(!0)},getFirstSegment:function(){return this._segments[0]},getLastSegment:function(){return this._segments[this._segments.length-1]},getCurves:function(){var t=this._curves,e=this._segments;if(!t){var n=this._countCurves();t=this._curves=Array(n);for(var i=0;n>i;i++)t[i]=new I(this,e[i],e[i+1]||e[0])}return t},getFirstCurve:function(){return this.getCurves()[0]},getLastCurve:function(){var t=this.getCurves();return t[t.length-1]},isClosed:function(){return this._closed},setClosed:function(t){if(this._closed!=(t=!!t)){if(this._closed=t,this._curves){var e=this._curves.length=this._countCurves();t&&(this._curves[e-1]=new I(this,this._segments[e-1],this._segments[0]))}this._changed(25)}}},{beans:!0,getPathData:function(t,e){function n(e,n){e._transformCoordinates(t,g,!1),i=g[0],r=g[1],v?(p.push("M"+f.pair(i,r)),v=!1):(h=g[2],u=g[3],h===i&&u===r&&l===a&&c===o?n||p.push("l"+f.pair(i-a,r-o)):p.push("c"+f.pair(l-a,c-o)+" "+f.pair(h-a,u-o)+" "+f.pair(i-a,r-o))),a=i,o=r,l=g[4],c=g[5]}var i,r,a,o,h,u,l,c,d=this._segments,_=d.length,f=new s(e),g=Array(6),v=!0,p=[];if(0===_)return"";for(var m=0;_>m;m++)n(d[m]);return this._closed&&_>0&&(n(d[0],!0),p.push("z")),p.join("")}},{isEmpty:function(){return 0===this._segments.length},_transformContent:function(t){for(var e=Array(6),n=0,i=this._segments.length;i>n;n++)this._segments[n]._transformCoordinates(t,e,!0);return!0},_add:function(t,e){for(var n=this._segments,i=this._curves,r=t.length,s=null==e,e=s?n.length:e,a=0;r>a;a++){var o=t[a];o._path&&(o=t[a]=o.clone()),o._path=this,o._index=e+a,o._selectionState&&this._updateSelection(o,0,o._selectionState)}if(s)n.push.apply(n,t);else{n.splice.apply(n,[e,0].concat(t));for(var a=e+r,h=n.length;h>a;a++)n[a]._index=a}if(i){var u=this._countCurves(),l=e+r-1===u?e-1:e,c=l,d=Math.min(l+r,u);t._curves&&(i.splice.apply(i,[l,0].concat(t._curves)),c+=t._curves.length);for(var a=c;d>a;a++)i.splice(a,0,new I(this,null,null));this._adjustCurves(l,d)}return this._changed(25),t},_adjustCurves:function(t,e){for(var n,i=this._segments,r=this._curves,s=t;e>s;s++)n=r[s],n._path=this,n._segment1=i[s],n._segment2=i[s+1]||i[0],n._changed();(n=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0],n._changed()),(n=r[e])&&(n._segment1=i[e],n._changed())},_countCurves:function(){var t=this._segments.length;return!this._closed&&t>0?t-1:t},add:function(t){return arguments.length>1&&"number"!=typeof t?this._add(P.readAll(arguments)):this._add([P.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(P.readAll(arguments,1),t):this._add([P.read(arguments,1)],t)[0]},addSegment:function(){return this._add([P.read(arguments)])[0]},insertSegment:function(t){return this._add([P.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(P.readAll(t))},insertSegments:function(t,e){return this._add(P.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,n,i){t=t||0,n=e.pick(n,this._segments.length);var r=this._segments,s=this._curves,a=r.length,o=r.splice(t,n-t),h=o.length;if(!h)return o;for(var u=0;h>u;u++){var l=o[u];l._selectionState&&this._updateSelection(l,l._selectionState,0),l._index=l._path=null}for(var u=t,c=r.length;c>u;u++)r[u]._index=u;if(s){var d=t>0&&n===a+(this._closed?1:0)?t-1:t,s=s.splice(d,h);i&&(o._curves=s.slice(1)),this._adjustCurves(d,d)}return this._changed(25),o},clear:"#removeSegments",hasHandles:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)if(t[e].hasHandles())return!0;return!1},clearHandles:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)t[e].clearHandles()},getLength:function(){if(null==this._length){for(var t=this.getCurves(),e=0,n=0,i=t.length;i>n;n++)e+=t[n].getLength();this._length=e}return this._length},getArea:function(){if(null==this._area){for(var t=this._segments,e=t.length,n=e-1,i=0,r=0,s=this._closed?e:n;s>r;r++)i+=I.getArea(I.getValues(t[r],t[n>r?r+1:0]));this._area=i}return this._area},isClockwise:function(){return this._clockwise!==t?this._clockwise:this.getArea()>=0},setClockwise:function(t){this.isClockwise()!=(t=!!t)&&this.reverse(),this._clockwise=t},isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===7*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function vt(t){t||this._selectSegments(!1),vt.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?7*e:0;for(var n=0;e>n;n++)this._segments[n]._selectionState=t?7:0},_updateSelection:function(t,e,n){t._selectionState=n;var i=this._selectedSegmentState+=n-e;i>0&&this.setSelected(!0)},flatten:function(t){for(var e=new T(this,64,.1),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new P(e.getPointAt(n))),n+=i;this.setSegments(s)},reduce:function(){for(var t=this.getCurves(),e=t.length-1;e>=0;e--){var n=t[e];n.hasHandles()||0!==n.getLength()&&!n.isCollinear(n.getNext())||n.remove()}return this},simplify:function(t){if(this._segments.length>2){var e=new N(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null===e)return null;if(1===arguments.length){var n=t;if("number"==typeof n&&(n=this.getLocationAt(n)),!n)return null;t=n.index,e=n.parameter}var i=4e-7,r=1-i;e>=r&&(t++,e--);var s=this.getCurves();if(t>=0&&t=i&&s[t++].divide(e,!0);var a,o=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),a=this):(a=new O(m.NO_INSERT),a.insertAbove(this,!0),this._clone(a)),a._add(o,0),this.addSegment(o[0]),a}return null},reverse:function(){this._segments.reverse();for(var e=0,n=this._segments.length;n>e;e++){var i=this._segments[e],r=i._handleIn;i._handleIn=i._handleOut,i._handleOut=r,i._index=e}this._curves=null,this._clockwise!==t&&(this._clockwise=!this._clockwise),this._changed(9)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();if(!i)return this;n&&n._point.equals(i._point)&&t.reverse();var r=t.getFirstSegment();if(n&&n._point.equals(r._point))n.setHandleOut(r._handleOut),this._add(e.slice(1));else{var s=this.getFirstSegment();s&&s._point.equals(r._point)&&t.reverse(),i=t.getLastSegment(),s&&s._point.equals(i._point)?(s.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())}t._closed&&this._add([e[0]]),t.remove()}var a=this.getFirstSegment(),o=this.getLastSegment();return a!==o&&a._point.equals(o._point)&&(a.setHandleIn(o._handleIn),o.remove(),this.setClosed(!0)),this},toShape:function(t){function e(t,e){var n=c[t],i=n.getNext(),r=c[e],s=r.getNext();return n._handleOut.isZero()&&i._handleIn.isZero()&&r._handleOut.isZero()&&s._handleIn.isZero()&&i._point.subtract(n._point).isCollinear(s._point.subtract(r._point))}function n(t){var e=c[t],n=e.getPrevious(),i=e.getNext();return n._handleOut.isZero()&&e._handleIn.isZero()&&e._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(n._point).isOrthogonal(i._point.subtract(e._point))}function i(t){var e=c[t],n=e.getNext(),i=e._handleOut,r=n._handleIn,s=.5522847498307936;if(i.isOrthogonal(r)){var o=e._point,h=n._point,u=new g(o,i,!0).intersect(new g(h,r,!0),!0);return u&&a.isZero(i.getLength()/u.subtract(o).getLength()-s)&&a.isZero(r.getLength()/u.subtract(h).getLength()-s)}return!1}function r(t,e){return c[t]._point.getDistance(c[e]._point)}if(!this._closed)return null;var s,o,h,u,c=this._segments;if(!this.hasHandles()&&4===c.length&&e(0,2)&&e(1,3)&&n(1)?(s=x.Rectangle,o=new l(r(0,3),r(0,1)),u=c[1]._point.add(c[2]._point).divide(2)):8===c.length&&i(0)&&i(2)&&i(4)&&i(6)&&e(1,5)&&e(3,7)?(s=x.Rectangle,o=new l(r(1,6),r(0,3)),h=o.subtract(new l(r(0,7),r(1,2))).divide(2),u=c[3]._point.add(c[4]._point).divide(2)):4===c.length&&i(0)&&i(1)&&i(2)&&i(3)&&(a.isZero(r(0,2)-r(1,3))?(s=x.Circle,h=r(0,2)/2):(s=x.Ellipse,h=new l(r(2,0)/2,r(3,1)/2)),u=c[1]._point),s){var d=this.getPosition(!0),_=this._clone(new s({center:d,size:o,radius:h,insert:!1}),t,!1);return _.rotate(u.subtract(d).getAngle()+90),_}return null},_hitTestSelf:function(t,e){function n(e,n){return t.subtract(e).divide(n).length<=1}function i(t,i,r){if(!e.selected||i.isSelected()){var s=t._point;if(i!==s&&(i=i.add(s)),n(i,w))return new S(r,f,{segment:t,point:i})}}function r(t,n){return(n||e.segments)&&i(t,t._point,"segment")||!n&&e.handles&&(i(t,t._handleIn,"handle-in")||i(t,t._handleOut,"handle-out"))}function s(t){c.add(t)}function a(e){if(("round"!==o||"round"!==u)&&(c=new O({internal:!0,closed:!0}),m||e._index>0&&e._index0||C?0:null;if(null!==P&&(P>0?(o=g.getStrokeJoin(),u=g.getStrokeCap(),l=P*g.getMiterLimit(),w=y.add(new h(P,P))):o=u="round"),!e.ends||e.segments||m){if(e.segments||e.handles)for(var M=0;p>M;M++)if(_=r(v[M]))return _}else if(_=r(v[0],!0)||r(v[p-1],!0))return _;if(null!==P){if(d=this.getNearestLocation(t)){var I=d.getParameter();0===I||1===I&&p>1?a(d.getSegment())||(d=null):n(d.getPoint(),w)||(d=null)}if(!d&&"miter"===o&&p>1)for(var M=0;p>M;M++){var z=v[M];if(t.getDistance(z._point)<=l&&a(z)){d=z.getLocation();break}}}return!d&&b&&this._contains(t)||d&&!x&&!C?new S("fill",this):d?new S(x?"stroke":"curve",this,{location:d,point:d.getPoint()}):null}},e.each(I.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getLocationAt(e,n);return i&&i[t]()}},{beans:!1,getLocationOf:function(){for(var t=h.read(arguments),e=this.getCurves(),n=0,i=e.length;i>n;n++){var r=e[n].getLocationOf(t);if(r)return r}return null},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t,s=n[r];return s?s.getLocationAt(t-r,!0):null}for(var a=0,o=n.length;o>a;a++){var h=i,s=n[a];if(i+=s.getLength(),i>t)return s.getLocationAt(t-h)}return n.length>0&&t<=this.getLength()?new z(n[n.length-1],1):null},getNearestLocation:function(){for(var t=h.read(arguments),e=this.getCurves(),n=1/0,i=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceo;o++){var u=e[o];u._transformCoordinates(n,a,!1);var l=u._selectionState,c=a[0],d=a[1];if(1&l&&r(2),2&l&&r(4),t.fillRect(c-s,d-s,i,i),!(4&l)){var _=t.fillStyle;t.fillStyle="#ffffff",t.fillRect(c-s+1,d-s+1,i-2,i-2),t.fillStyle=_}}}function e(t,e,n){function i(e){if(n)e._transformCoordinates(n,f,!1),r=f[0],s=f[1];else{var i=e._point;r=i._x,s=i._y}if(g)t.moveTo(r,s),g=!1;else{if(n)h=f[2],u=f[3];else{var d=e._handleIn;h=r+d._x,u=s+d._y}h===r&&u===s&&l===a&&c===o?t.lineTo(r,s):t.bezierCurveTo(l,c,h,u,r,s)}if(a=r,o=s,n)l=f[4],c=f[5];else{var d=e._handleOut;l=a+d._x,c=o+d._y}}for(var r,s,a,o,h,u,l,c,d=e._segments,_=d.length,f=Array(6),g=!0,v=0;_>v;v++)i(d[v]);e._closed&&_>0&&i(d[0])}return{_draw:function(t,n,i){function r(t){return l[(t%c+c)%c]}var s=n.dontStart,a=n.dontFinish||n.clip,o=this.getStyle(),h=o.hasFill(),u=o.hasStroke(),l=o.getDashArray(),c=!paper.support.nativeDash&&u&&l&&l.length;if(s||t.beginPath(),!s&&this._currentPath?t.currentPath=this._currentPath:(h||u&&!c||a)&&(e(t,this,i),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!a&&(h||u)&&(this._setStyles(t),h&&(t.fill(o.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),u)){if(c){s||t.beginPath();var d,_=new T(this,32,.25,i),f=_.length,g=-o.getDashOffset(),v=0;for(g%=f;g>0;)g-=r(v--)+r(v--);for(;f>g;)d=g+r(v++),(g>0||d>0)&&_.drawPart(t,Math.max(g,0),Math.max(d,0)),g=d+r(v++)}t.stroke()}},_drawSelected:function(n,i){n.beginPath(),e(n,this,i),n.stroke(),t(n,this._segments,i,paper.settings.handleSize)}}},new function(){function t(t){var e=t.length,n=[],i=[],r=2;n[0]=t[0]/r;for(var s=1;e>s;s++)i[s]=1/r,r=(e-1>s?4:2)-i[s],n[s]=(t[s]-n[s-1])/r;for(var s=1;e>s;s++)n[e-s-1]-=i[e-s]*n[e-s];return n}return{smooth:function(){var e=this._segments,n=e.length,i=this._closed,r=n,s=0;if(!(2>=n)){i&&(s=Math.min(n,4),r+=2*Math.min(n,s));for(var a=[],o=0;n>o;o++)a[o+s]=e[o]._point;if(i)for(var o=0;s>o;o++)a[o]=e[o+n-s]._point,a[o+n+s]=e[o]._point;else r--;for(var u=[],o=1;r-1>o;o++)u[o]=4*a[o]._x+2*a[o+1]._x;u[0]=a[0]._x+2*a[1]._x,u[r-1]=3*a[r-1]._x;for(var l=t(u),o=1;r-1>o;o++)u[o]=4*a[o]._y+2*a[o+1]._y;u[0]=a[0]._y+2*a[1]._y,u[r-1]=3*a[r-1]._y;var c=t(u);if(i){for(var o=0,d=n;s>o;o++,d++){var _=o/s,f=1-_,g=o+s,v=d+s;l[d]=l[o]*_+l[d]*f,c[d]=c[o]*_+c[d]*f,l[v]=l[g]*f+l[v]*_,c[v]=c[g]*f+c[v]*_}r--}for(var p=null,o=s;r-s>=o;o++){var m=e[o-s];p&&m.setHandleIn(p.subtract(m._point)),r>o&&(m.setHandleOut(new h(l[o],c[o]).subtract(m._point)),p=r-1>o?new h(2*a[o+1]._x-l[o+1],2*a[o+1]._y-c[o+1]):new h((a[r]._x+l[r-1])/2,(a[r]._y+c[r-1])/2))}if(i&&p){var m=this._segments[0];m.setHandleIn(p.subtract(m._point))}}}}},new function(){function t(t){var e=t._segments;if(0===e.length)throw Error("Use a moveTo() command first");return e[e.length-1]}return{moveTo:function(){var t=this._segments;1===t.length&&this.removeSegment(0),t.length||this._add([new P(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new P(h.read(arguments))])},cubicCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this);r.setHandleOut(e.subtract(r._point)),this._add([new P(i,n.subtract(i))])},quadraticCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.cubicCurveTo(e.add(i.subtract(e).multiply(1/3)),e.add(n.subtract(e).multiply(1/3)),n)},curveTo:function(){var n=h.read(arguments),i=h.read(arguments),r=e.pick(e.read(arguments),.5),s=1-r,a=t(this)._point,o=n.subtract(a.multiply(s*s)).subtract(i.multiply(r*r)).divide(2*r*s);if(o.isNaN())throw Error("Cannot put a curve through points with parameter = "+r);this.quadraticCurveTo(o,i)},arcTo:function(){var n,i,r,s,a,o=t(this),u=o._point,c=h.read(arguments),d=e.peek(arguments),_=e.pick(d,!0);if("boolean"==typeof _)var v=u.add(c).divide(2),n=v.add(v.subtract(u).rotate(_?-90:90));else if(e.remain(arguments)<=2)n=c,c=h.read(arguments);else{var p=l.read(arguments);if(p.isZero())return this.lineTo(c);var m=e.read(arguments),_=!!e.read(arguments),y=!!e.read(arguments),v=u.add(c).divide(2),w=u.subtract(v).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=C(p.width),M=C(p.height),I=S*S,z=M*M,k=x*x,O=b*b,A=Math.sqrt(k/I+O/z);if(A>1&&(S*=A,M*=A,I=S*S,z=M*M),A=(I*z-I*O-z*k)/(I*O+z*k),C(A)<1e-12&&(A=0),0>A)throw Error("Cannot create an arc with the given arguments");i=new h(S*b/M,-M*x/S).multiply((y===_?-1:1)*Math.sqrt(A)).rotate(m).add(v),a=(new f).translate(i).rotate(m).scale(S,M),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(c)),!_&&r>0?r-=360:_&&0>r&&(r+=360)}if(n){var T=new g(u.add(n).divide(2),n.subtract(u).rotate(90),!0),N=new g(n.add(c).divide(2),c.subtract(n).rotate(90),!0),L=new g(u,c),E=L.getSide(n);if(i=T.intersect(N,!0),!i){if(!E)return this.lineTo(c);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(i),r=s.getDirectedAngle(c.subtract(i));var B=L.getSide(i);0===B?r=E*Math.abs(r):E===B&&(r+=0>r?360:-360)}for(var j=Math.abs(r),D=j>=360?4:Math.ceil(j/90),R=r/D,F=R*Math.PI/360,q=4/3*Math.sin(F)/(1+Math.cos(F)),V=[],H=0;D>=H;H++){var w=c,Z=null;if(D>H&&(Z=s.rotate(90).multiply(q),a?(w=a._transformPoint(s),Z=a._transformPoint(s.add(Z)).subtract(w)):w=i.add(s)),0===H)o.setHandleOut(Z);else{var W=s.rotate(-90).multiply(q);a&&(W=a._transformPoint(s.add(W)).subtract(w)),V.push(new P(w,W,Z))}s=s.rotate(R)}this._add(V)},lineBy:function(){var e=h.read(arguments),n=t(this)._point;this.lineTo(n.add(e))},curveBy:function(){var n=h.read(arguments),i=h.read(arguments),r=e.read(arguments),s=t(this)._point;this.curveTo(s.add(n),s.add(i),r)},cubicCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this)._point;this.cubicCurveTo(r.add(e),r.add(n),r.add(i))},quadraticCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.quadraticCurveTo(i.add(e),i.add(n))},arcBy:function(){var n=t(this)._point,i=n.add(h.read(arguments)),r=e.pick(e.peek(arguments),!0);"boolean"==typeof r?this.arcTo(i,r):this.arcTo(i,n.add(h.read(arguments)))},closePath:function(t){this.setClosed(!0),t&&this.join()}}},{_getBounds:function(t,e){return O[t](this._segments,this._closed,this.getStyle(),e)},statics:{getBounds:function(t,e,n,i,r){function s(t){t._transformCoordinates(i,o,!1);for(var e=0;2>e;e++)I._addBounds(h[e],h[e+4],o[e+2],o[e],e,r?r[e]:0,u,l,c);var n=h;h=o,o=n}var a=t[0];if(!a)return new d;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),_=1,f=t.length;f>_;_++)s(t[_]);return e&&s(a),new d(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(t){_=_.include(i?i._transformPoint(t,t):t)}function s(t){_=_.unite(p.setCenter(i?i._transformPoint(t._point):t._point))}function a(t,e){var n=t._handleIn,i=t._handleOut;"round"===e||!n.isZero()&&!i.isZero()&&n.isCollinear(i)?s(t):O._addBevelJoin(t,e,u,v,r)}function o(t,e){"round"===e?s(t):O._addSquareCap(t,e,u,r)}if(!n.hasStroke())return O.getBounds(t,e,n,i);for(var h=t.length-(e?0:1),u=n.getStrokeWidth()/2,c=O._getPenPadding(u,i),_=O.getBounds(t,e,n,i,c),f=n.getStrokeJoin(),g=n.getStrokeCap(),v=u*n.getMiterLimit(),p=new d(new l(c).multiply(2)),m=1;h>m;m++)a(t[m],f);return e?a(t[0],f):h>0&&(o(t[0],g),o(t[t.length-1],g)),_},_getPenPadding:function(t,e){if(!e)return[t,t];var n=e.shiftless(),i=n.transform(new h(t,0)),r=n.transform(new h(0,t)),s=i.getAngleInRadians(),a=i.getLength(),o=r.getLength(),u=Math.sin(s),l=Math.cos(s),c=Math.tan(s),d=-Math.atan(o*c/a),_=Math.atan(o/(c*a));return[Math.abs(a*Math.cos(d)*l-o*Math.sin(d)*u),Math.abs(o*Math.sin(_)*l+a*Math.cos(_)*u)]},_addBevelJoin:function(t,e,n,i,r,s){var a=t.getCurve(),o=a.getPrevious(),u=a.getPointAt(0,!0),l=o.getNormalAt(1,!0),c=a.getNormalAt(0,!0),d=l.getDirectedAngle(c)<0?-n:n;if(l.setLength(d),c.setLength(d),s&&(r(u),r(u.add(l))),"miter"===e){var _=new g(u.add(l),new h(-l.y,l.x),!0).intersect(new g(u.add(c),new h(-c.y,c.x),!0),!0);if(_&&u.getDistance(_)<=i&&(r(_),!s))return}s||r(u.add(l)),r(u.add(c))},_addSquareCap:function(t,e,n,i,r){var s=t._point,a=t.getLocation(),o=a.getNormal().multiply(n);r&&(i(s.subtract(o)),i(s.add(o))),"square"===e&&(s=s.add(o.rotate(0===a.getParameter()?-90:90))),i(s.add(o)),i(s.subtract(o))},getHandleBounds:function(t,e,n,i,r,s){for(var a=Array(6),o=1/0,h=-o,u=o,l=h,c=0,_=t.length;_>c;c++){var f=t[c];f._transformCoordinates(i,a,!1);for(var g=0;6>g;g+=2){var v=0===g?s:r,p=v?v[0]:0,m=v?v[1]:0,y=a[g],w=a[g+1],x=y-p,b=y+p,C=w-m,S=w+m;o>x&&(o=x),b>h&&(h=b),u>C&&(u=C),S>l&&(l=S)}}return new d(o,u,h-o,l-u)},getRoughBounds:function(t,e,n,i){var r=n.hasStroke()?n.getStrokeWidth()/2:0,s=r;return r>0&&("miter"===n.getStrokeJoin()&&(s=r*n.getMiterLimit()),"square"===n.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),O.getHandleBounds(t,e,n,i,O._getPenPadding(r,i),O._getPenPadding(s,i))}}});O.inject({statics:new function(){function t(t,n,i){var r=e.getNamed(i),s=new O(r&&r.insert===!1&&m.NO_INSERT);return s._add(t),s._closed=n,s.set(r)}function n(e,n,i){for(var s=Array(4),a=0;4>a;a++){var o=r[a];s[a]=new P(o._point.multiply(n).add(e),o._handleIn.multiply(n),o._handleOut.multiply(n))}return t(s,!0,i)}var i=.5522847498307936,r=[new P([-1,0],[0,i],[0,-i]),new P([0,-1],[-i,0],[i,0]),new P([1,0],[0,-i],[0,i]),new P([0,1],[i,0],[-i,0])];return{Line:function(){return t([new P(h.readNamed(arguments,"from")),new P(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return n(t,new l(i),arguments)},Rectangle:function(){var e,n=d.readNamed(arguments,"rectangle"),r=l.readNamed(arguments,"radius",0,{readNull:!0}),s=n.getBottomLeft(!0),a=n.getTopLeft(!0),o=n.getTopRight(!0),h=n.getBottomRight(!0);if(!r||r.isZero())e=[new P(s),new P(a),new P(o),new P(h)];else{r=l.min(r,n.getSize(!0).divide(2));var u=r.width,c=r.height,_=u*i,f=c*i;e=[new P(s.add(u,0),null,[-_,0]),new P(s.subtract(0,c),[0,f]),new P(a.add(0,c),null,[0,-f]),new P(a.add(u,0),[-_,0],null),new P(o.subtract(u,0),null,[_,0]),new P(o.add(0,c),[0,-f],null),new P(h.subtract(0,c),null,[0,f]),new P(h.subtract(u,0),[_,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=x._readEllipse(arguments);return n(t.center,t.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),n=h.readNamed(arguments,"through"),i=h.readNamed(arguments,"to"),r=e.getNamed(arguments),s=new O(r&&r.insert===!1&&m.NO_INSERT);return s.moveTo(t),s.arcTo(n,i),s.set(r)},RegularPolygon:function(){for(var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"sides"),r=e.readNamed(arguments,"radius"),s=360/i,a=!(i%3),o=new h(0,a?-r:r),u=a?-1:.5,l=Array(i),c=0;i>c;c++)l[c]=new P(n.add(o.rotate((c+u)*s)));return t(l,!0,arguments)},Star:function(){for(var n=h.readNamed(arguments,"center"),i=2*e.readNamed(arguments,"points"),r=e.readNamed(arguments,"radius1"),s=e.readNamed(arguments,"radius2"),a=360/i,o=new h(0,-1),u=Array(i),l=0;i>l;l++)u[l]=new P(n.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var A=k.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||("string"==typeof t?this.setPathData(t):this.addChildren(Array.isArray(t)?t:arguments))},insertChildren:function pt(e,n,i){for(var r=n.length-1;r>=0;r--){var s=n[r];s instanceof A&&(n.splice.apply(n,[r,1].concat(s.removeChildren())),s.remove())}n=pt.base.call(this,e,n,i,O);for(var r=0,a=!i&&n&&n.length;a>r;r++){var s=n[r];s._clockwise===t&&s.setClockwise(0===s._index)}return n},reverse:function(){for(var t=this._children,e=0,n=t.length;n>e;e++)t[e].reverse()},smooth:function(){for(var t=0,e=this._children.length;e>t;t++)this._children[t].smooth()},reduce:function mt(){for(var t=this._children,e=t.length-1;e>=0;e--){var n=t[e].reduce();n.isEmpty()&&t.splice(e,1)}if(0===t.length){var n=new O(m.NO_INSERT);return n.insertAbove(this),n.setStyle(this._style),this.remove(),n}return mt.base.call(this)},isClockwise:function(){var t=this.getFirstChild();return t&&t.isClockwise()},setClockwise:function(t){this.isClockwise()!==!!t&&this.reverse()},getFirstSegment:function(){var t=this.getFirstChild();return t&&t.getFirstSegment()},getLastSegment:function(){var t=this.getLastChild();return t&&t.getLastSegment()},getCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n].getCurves());return e},getFirstCurve:function(){var t=this.getFirstChild();return t&&t.getFirstCurve()},getLastCurve:function(){var t=this.getLastChild();return t&&t.getFirstCurve()},getArea:function(){for(var t=this._children,e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e}},{beans:!0,getPathData:function(t,e){for(var n=this._children,i=[],r=0,s=n.length;s>r;r++){var a=n[r],o=a._matrix;i.push(a.getPathData(t&&!o.isIdentity()?t.chain(o):t,e))}return i.join(" ")}},{_getChildHitTestOptions:function(t){return t["class"]===O||"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e,n){var i=this._children;if(0!==i.length){if(this._currentPath)t.currentPath=this._currentPath;else{e=e.extend({dontStart:!0,dontFinish:!0}),t.beginPath();for(var r=0,s=i.length;s>r;r++)i[r].draw(t,e,n);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var a=this._style;a.hasFill()&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),a.hasStroke()&&t.stroke()}}},_drawSelected:function(t,e,n){for(var i=this._children,r=0,s=i.length;s>r;r++){ +var a=i[r],o=a._matrix;n[a._id]||a._drawSelected(t,o.isIdentity()?e:e.chain(o))}}},new function(){function t(t,e){var n=t._children;if(e&&0===n.length)throw Error("Use a moveTo() command first");return n[n.length-1]}var n={moveTo:function(){var e=t(this),n=e&&e.isEmpty()?e:new O(m.NO_INSERT);n!==e&&this.addChild(n),n.moveTo.apply(n,arguments)},moveBy:function(){var e=t(this,!0),n=e&&e.getLastSegment(),i=h.read(arguments);this.moveTo(n?i.add(n._point):i)},closePath:function(e){t(this,!0).closePath(e)}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){n[e]=function(){var n=t(this,!0);n[e].apply(n,arguments)}}),n});k.inject(new function(){function e(t,e){var n=t.clone(!1).reduce().transform(null,!0,!0);return e?n.resolveCrossings().reorient():n}function n(t,e,n,i,r){var s=new t(m.NO_INSERT);return s.addChildren(e,!0),r&&(s=s.reduce()),s.insertAbove(i&&n.isSibling(i)&&n.getIndex()e;e++){var i=t[e];_.push.apply(_,i._segments),f.push.apply(f,i._getMonoCurves())}}if(!t._children&&!t._closed)return r(t,i,s);var h=e(t,!0),u=i&&t!==i&&e(i,!0);u&&/^(subtract|exclude)$/.test(s)^u.isClockwise()!==h.isClockwise()&&u.reverse();var d=z.expand(h.getIntersections(u,function(t){return u&&t.isOverlap()||t.isCrossing()}));o(d);var _=[],f=[];a(h._children||[h]),u&&a(u._children||[u]);for(var g=0,v=d.length;v>g;g++)l(d[g]._segment,h,u,f,s);for(var g=0,v=_.length;v>g;g++){var p=_[g];null==p._winding&&l(p,h,u,f,s)}return n(A,c(_,s),t,i,!0)}function r(i,r,s){function a(e){return h.contains(e.getPointAt(e.getLength()/2))^l?(c.unshift(e),!0):t}if(!r||!r._children&&!r._closed||!/^(subtract|intersect)$/.test(s))return null;for(var o=e(i,!1),h=e(r,!1),u=o.getIntersections(h,function(t){return t.isOverlap()||t.isCrossing()}),l="subtract"===s,c=[],d=u.length-1;d>=0;d--){var _=u[d].split();_&&(a(_)&&_.getFirstSegment().setHandleIn(0,0),o.getLastSegment().setHandleOut(0,0))}return a(o),n(y,c,i,r)}function s(t,e){for(var n=t;n;){if(n===e)return;n=n._prev}for(;t._next&&t._next!==e;)t=t._next;if(!t._next){for(;e._prev;)e=e._prev;t._next=e,e._prev=t}}function o(t){for(var e,n,i=4e-7,r=1-i,a=!1,o=[],h=t.length-1;h>=0;h--){var u=t[h],l=u._curve,c=u._parameter,d=c;l!==e?a=!l.hasHandles():n>0&&(c/=n);var _;i>c?_=l._segment1:c>r?_=l._segment2:(_=l.divide(c,!0,!0)._segment1,a&&o.push(_)),u._setSegment(_);var f=_._intersection,g=u._intersection;if(f){s(f,g);for(var v=f;v;)s(v._intersection,f),v=v._next}else _._intersection=g;e=l,n=d}for(var h=0,p=o.length;p>h;h++)o[h].clearHandles()}function u(t,e,n,i){var r=2e-7,s=4e-7,o=1-s,l=t.x,c=t.y,d=0,_=0,f=[],g=Math.abs;if(n){for(var v=-(1/0),p=1/0,m=c-r,y=c+r,w=0,x=e.length;x>w;w++){var b=e[w].values;if(I.solveCubic(b,0,l,f,0,1)>0)for(var C=f.length-1;C>=0;C--){var S=I.getPoint(b,f[C]).y;m>S&&S>v?v=S:S>y&&p>S&&(p=S)}}v=(v+c)/2,p=(p+c)/2,v>-(1/0)&&(d=u(new h(l,v),e,!1,i)),1/0>p&&(_=u(new h(l,p),e,!1,i))}else for(var P,M,z=l-r,k=l+r,O=!1,w=0,x=e.length;x>w;w++){var A=e[w],b=A.values,T=A.winding;if(T&&(1===T&&c>=b[1]&&c<=b[7]||c>=b[7]&&c<=b[1])&&1===I.solveCubic(b,1,c,f,0,1)){var N=f[0];if(!(N>o&&O&&A.next!==e[w+1]||s>N&&M>o&&A.previous===P)){var L=I.getPoint(b,N).x,E=I.getTangent(b,N).y,B=!1;a.isZero(E)&&!I.isStraight(b)||s>N&&E*I.getTangent(A.previous.values,1).y<0||N>o&&E*I.getTangent(A.next.values,0).y<0?i&&L>=z&&k>=L&&(++d,++_,B=!0):z>=L?(d+=T,B=!0):L>=k&&(_+=T,B=!0),A.previous!==e[w-1]&&(O=s>N&&B)}P=A,M=N}}return Math.max(g(d),g(_))}function l(t,e,n,i,r){var s=2e-7,a=[],o=t,h=0,l=0;do{var c=t.getCurve(),d=c.getLength();a.push({segment:t,curve:c,length:d}),h+=d,t=t.getNext()}while(t&&!t._intersection&&t!==o);for(var _=0;3>_;_++)for(var d=h*(_+1)/4,f=0,g=a.length;g>f;f++){var v=a[f],p=v.length;if(p>=d){(s>d||s>p-d)&&(d=p/2);var c=v.curve,m=c._path,y=m._parent,w=c.getPointAt(d),x=c.isHorizontal();y instanceof A&&(m=y),l+="subtract"===r&&n&&(m===e&&n._getWinding(w,x)||m===n&&!e._getWinding(w,x))?0:u(w,i,x);break}d-=p}for(var b=Math.round(l/3),C=a.length-1;C>=0;C--)a[C].segment._winding=b}function c(t,e){function n(t,e){if(t._visited)return!1;if(!l)return!0;var n=t._winding,i=t._intersection;return i&&e&&c&&i.isOverlap()&&(n=c[n]||n),l(n)}function i(t){return t===o||t===h}function r(t,e){if(!t._next)return t;for(;t;){var r=t._segment,s=r.getNext(),a=s._intersection;if(i(s)||!r._visited&&!s._visited&&(!l||(!e||n(r))&&(!(e&&a&&a.isOverlap())&&n(s)||!e&&a&&n(a._segment))))return t;t=t._next}return null}function s(t,e){for(;t;){var n=t._segment;if(i(n))return n;t=t[e?"_next":"_prev"]}}for(var o,h,u=[],l=d[e],c={unite:{1:2},intersect:{2:1}}[e],_=0,f=t.length;f>_;_++){var g=t[_],v=null,p=!1;if(n(g,!0)){for(o=h=null;!p;){var y=g._intersection,w=v&&g._handleIn;y=y&&(r(y,!0)||r(y,!1))||y;var x=y&&y._segment;if(x&&n(x)&&(g=x),g._visited){if(p=i(g),!p&&y){var b=s(y,!0)||s(y,!1);b&&(g=b,p=!0)}break}v||(v=new O(m.NO_INSERT),o=g,h=x),v.add(new P(g._point,w,g._handleOut)),g._visited=!0,g=g.getNext(),p=i(g)}p?(v.firstSegment.setHandleIn(g._handleIn),v.setClosed(!0)):v&&(console.error("Boolean operation resulted in open path","segments =",v._segments.length,"length =",v.getLength()),v=null),v&&(v._segments.length>8||!a.isZero(v.getArea()))&&(u.push(v),v=null)}}return u}var d={unite:function(t){return 1===t||0===t},intersect:function(t){return 2===t},subtract:function(t){return 1===t},exclude:function(t){return 1===t}};return{_getWinding:function(t,e,n){return u(t,this._getMonoCurves(),e,n)},unite:function(t){return i(this,t,"unite")},intersect:function(t){return i(this,t,"intersect")},subtract:function(t){return i(this,t,"subtract")},exclude:function(t){return i(this,t,"exclude")},divide:function(t){return n(y,[this.subtract(t),this.intersect(t)],this,t,!0)},resolveCrossings:function(){var t=this.getCrossings();if(!t.length)return this;o(z.expand(t));for(var e=this._children||[this],i=[],r=0,s=e.length;s>r;r++)i.push.apply(i,e[r]._segments);return n(A,c(i),this,null,!1)}}}),O.inject({_getMonoCurves:function(){function t(t){var e=t[1],r=t[7],s={values:t,winding:e===r?0:e>r?-1:1,previous:n,next:null};n&&(n.next=s),i.push(s),n=s}function e(e){if(0!==I.getLength(e)){var n=e[1],i=e[3],r=e[5],s=e[7];if(I.isStraight(e))t(e);else{var o=3*(i-r)-n+s,h=2*(n+r)-4*i,u=i-n,l=4e-7,c=1-l,d=[],_=a.solveQuadratic(o,h,u,d,l,c);if(0===_)t(e);else{d.sort();var f=d[0],g=I.subdivide(e,f);t(g[0]),_>1&&(f=(d[1]-f)/(1-f),g=I.subdivide(g[1],f),t(g[0])),t(g[1])}}}}var n,i=this._monoCurves;if(!i){i=this._monoCurves=[];for(var r=this.getCurves(),s=this._segments,o=0,h=r.length;h>o;o++)e(r[o].getValues());if(!this._closed&&s.length>1){var u=s[s.length-1]._point,l=s[0]._point,c=u._x,d=u._y,_=l._x,f=l._y;e([c,d,c,d,_,f,_,f])}if(i.length>0){var g=i[0],v=i[i.length-1];g.previous=v,v.next=g}}return i},getInteriorPoint:function(){var t=this.getBounds(),e=t.getCenter(!0);if(!this.contains(e)){for(var n=this._getMonoCurves(),i=[],r=e.y,s=[],a=0,o=n.length;o>a;a++){var h=n[a].values;if((1===n[a].winding&&r>=h[1]&&r<=h[7]||r>=h[7]&&r<=h[1])&&I.solveCubic(h,1,r,i,0,1)>0)for(var u=i.length-1;u>=0;u--)s.push(I.getPoint(h,i[u]).x);if(s.length>1)break}e.x=(s[0]+s[1])/2}return e},reorient:function(){return this.setClockwise(!0),this}}),A.inject({_getMonoCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n]._getMonoCurves());return e},reorient:function(){var t=this.removeChildren().sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()});if(t.length>0){this.addChildren(t);for(var e=t[0].isClockwise(),n=1,i=t.length;i>n;n++){for(var r=t[n].getInteriorPoint(),s=0,a=n-1;a>=0;a--)t[a].contains(r)&&s++;t[n].setClockwise(s%2===0&&e)}}return this}});var T=e.extend({_class:"PathIterator",initialize:function(t,e,n,i){function r(t,e){var n=I.getValues(t,e,i);o.push(n),s(n,t._index,0,1)}function s(t,e,i,r){if(r-i>l&&!I.isFlatEnough(t,n||.25)){var a=I.subdivide(t,.5),o=(i+r)/2;s(a[0],e,i,o),s(a[1],e,o,r)}else{var c=t[6]-t[0],d=t[7]-t[1],_=Math.sqrt(c*c+d*d);_>1e-6&&(u+=_,h.push({offset:u,value:r,index:e}))}}for(var a,o=[],h=[],u=0,l=1/(e||32),c=t._segments,d=c[0],_=1,f=c.length;f>_;_++)a=c[_],r(d,a),d=a;t._closed&&r(a,c[0]),this.curves=o,this.parts=h,this.length=u,this.index=0},getParameterAt:function(t){for(var e,n=this.index;e=n,!(0==n||this.parts[--n].offsete;e++){var r=this.parts[e];if(r.offset>=t){this.index=e;var s=this.parts[e-1],a=s&&s.index==r.index?s.value:0,o=s?s.offset:0;return{value:a+(r.value-a)*(t-o)/(r.offset-o),index:r.index}}}var r=this.parts[this.parts.length-1];return{value:1,index:r.index}},drawPart:function(t,e,n){e=this.getParameterAt(e),n=this.getParameterAt(n);for(var i=e.index;i<=n.index;i++){var r=I.getPart(this.curves[i],i==e.index?e.value:0,i==n.index?n.value:1);i==e.index&&t.moveTo(r[0],r[1]),t.bezierCurveTo.apply(t,r.slice(2))}}},e.each(I.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getParameterAt(e);return I[t](this.curves[i.index],i.value,n)}},{})),N=e.extend({initialize:function(t,e){for(var n,i=this.points=[],r=t._segments,s=0,a=r.length;a>s;s++){var o=r[s].point.clone();n&&n.equals(o)||(i.push(o),n=o)}t._closed&&(this.closed=!0,i.unshift(i[i.length-1]),i.push(i[1])),this.error=e},fit:function(){var t=this.points,e=t.length,n=this.segments=e>0?[new P(t[0])]:[];return e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.closed&&(n.shift(),n.pop()),n},fitCubic:function(e,n,i,r){if(n-e==1){var s=this.points[e],a=this.points[n],o=s.getDistance(a)/3;return this.addCurve([s,s.add(i.normalize(o)),a.add(r.normalize(o)),a]),t}for(var h,u=this.chordLengthParameterize(e,n),l=Math.max(this.error,this.error*this.error),c=!0,d=0;4>=d;d++){var _=this.generateBezier(e,n,u,i,r),f=this.findMaxError(e,n,_,u);if(f.error=l)break;c=this.reparameterize(e,n,u,_),l=f.error}var g=this.points[h-1].subtract(this.points[h]),v=this.points[h].subtract(this.points[h+1]),p=g.add(v).divide(2).normalize();this.fitCubic(e,h,i,p),this.fitCubic(h,n,p.negate(),r)},addCurve:function(t){var e=this.segments[this.segments.length-1];e.setHandleOut(t[1].subtract(t[0])),this.segments.push(new P(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-12,a=this.points[t],o=this.points[e],h=[[0,0],[0,0]],u=[0,0],l=0,c=e-t+1;c>l;l++){var d=n[l],_=1-d,f=3*d*_,g=_*_*_,v=f*_,p=f*d,m=d*d*d,y=i.normalize(v),w=r.normalize(p),x=this.points[t+l].subtract(a.multiply(g+v)).subtract(o.multiply(p+m));h[0][0]+=y.dot(y),h[0][1]+=y.dot(w),h[1][0]=h[0][1],h[1][1]+=w.dot(w),u[0]+=y.dot(x),u[1]+=w.dot(x)}var b,C,S=h[0][0]*h[1][1]-h[1][0]*h[0][1];if(Math.abs(S)>s){var P=h[0][0]*u[1]-h[1][0]*u[0],M=u[0]*h[1][1]-u[1]*h[0][1];b=M/S,C=P/S}else{var I=h[0][0]+h[0][1],z=h[1][0]+h[1][1];b=C=Math.abs(I)>s?u[0]/I:Math.abs(z)>s?u[1]/z:0}var k,O,A=o.getDistance(a),T=s*A;if(T>b||T>C)b=C=A/3;else{var N=o.subtract(a);k=i.normalize(b),O=r.normalize(C),k.dot(N)-O.dot(N)>A*A&&(b=C=A/3,k=O=null)}return[a,a.add(k||i.normalize(b)),o.add(O||r.normalize(C)),o]},reparameterize:function(t,e,n,i){for(var r=t;e>=r;r++)n[r-t]=this.findRoot(i,this.points[r],n[r-t]);for(var r=1,s=n.length;s>r;r++)if(n[r]<=n[r-1])return!1;return!0},findRoot:function(t,e,n){for(var i=[],r=[],s=0;2>=s;s++)i[s]=t[s+1].subtract(t[s]).multiply(3);for(var s=0;1>=s;s++)r[s]=i[s+1].subtract(i[s]).multiply(2);var a=this.evaluate(3,t,n),o=this.evaluate(2,i,n),h=this.evaluate(1,r,n),u=a.subtract(e),l=o.dot(o)+u.dot(h);return Math.abs(l)<1e-6?n:n-u.dot(o)/l},evaluate:function(t,e,n){for(var i=e.slice(),r=1;t>=r;r++)for(var s=0;t-r>=s;s++)i[s]=i[s].multiply(1-n).add(i[s+1].multiply(n));return i[0]},chordLengthParameterize:function(t,e){for(var n=[0],i=t+1;e>=i;i++)n[i-t]=n[i-t-1]+this.points[i].getDistance(this.points[i-1]);for(var i=1,r=e-t;r>=i;i++)n[i]/=n[r];return n},findMaxError:function(t,e,n,i){for(var r=Math.floor((e-t+1)/2),s=0,a=t+1;e>a;a++){var o=this.evaluate(3,n,i[a-t]),h=o.subtract(this.points[a]),u=h.x*h.x+h.y*h.y;u>=s&&(s=u,r=a)}return{error:s,index:r}}}),L=m.extend({_class:"TextItem",_boundsSelected:!0,_applyMatrix:!1,_canApplyMatrix:!1,_serializeFields:{content:null},_boundsGetter:"getBounds",initialize:function(n){this._content="",this._lines=[];var i=n&&e.isPlainObject(n)&&n.x===t&&n.y===t;this._initialize(i&&n,!i&&h.read(arguments))},_equals:function(t){return this._content===t._content},_clone:function yt(t,e,n){return t.setContent(this._content),yt.base.call(this,t,e,n)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(265)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),E=L.extend({_class:"PointText",initialize:function(){L.apply(this,arguments)},clone:function(t){return this._clone(new E(m.NO_INSERT),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.translate(t.subtract(this._matrix.getTranslation()))},_draw:function(t){if(this._content){this._setStyles(t);var e=this._style,n=this._lines,i=e.getLeading(),r=t.shadowColor;t.font=e.getFontStyle(),t.textAlign=e.getJustification();for(var s=0,a=n.length;a>s;s++){t.shadowColor=r;var o=n[s];e.hasFill()&&(t.fillText(o,0,0),t.shadowColor="rgba(0,0,0,0)"),e.hasStroke()&&t.strokeText(o,0,0),t.translate(0,i)}}},_getBounds:function(t,e){var n=this._style,i=this._lines,r=i.length,s=n.getJustification(),a=n.getLeading(),o=this.getView().getTextWidth(n.getFontStyle(),i),h=0;"left"!==s&&(h-=o/("center"===s?2:1));var u=new d(h,r?-.75*a:0,o,r*a);return e?e._transformBounds(u,u):u}}),B=e.extend(new function(){function t(t){var e,i=t.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/);if(i){e=[0,0,0];for(var r=0;3>r;r++){var s=i[r+1];e[r]=parseInt(1==s.length?s+s:s,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var s=+e[r];e[r]=3>r?s/255:s}}else{var h=a[t];if(!h){n||(n=Y.getContext(1,1),n.globalCompositeOperation="copy"),n.fillStyle="rgba(0,0,0,0)",n.fillStyle=t,n.fillRect(0,0,1,1);var u=n.getImageData(0,0,1,1).data;h=a[t]=[u[0]/255,u[1]/255,u[2]/255]}e=h.slice()}return e}var n,i={gray:["gray"],rgb:["red","green","blue"],hsb:["hue","saturation","brightness"],hsl:["hue","saturation","lightness"],gradient:["gradient","origin","destination","highlight"]},r={},a={},u=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],l={"rgb-hsb":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4);return[a,0===i?0:s/i,i]},"hsb-rgb":function(t,e,n){t=(t/60%6+6)%6;var i=Math.floor(t),r=t-i,i=u[i],s=[n,n*(1-e),n*(1-e*r),n*(1-e*(1-r))];return[s[i[0]],s[i[1]],s[i[2]]]},"rgb-hsl":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s,o=a?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4),h=(i+r)/2,u=a?0:.5>h?s/(i+r):s/(2-i-r);return[o,u,h]},"hsl-rgb":function(t,e,n){if(t=(t/360%1+1)%1,0===e)return[n,n,n];for(var i=[t+1/3,t,t-1/3],r=.5>n?n*(1+e):n+e-n*e,s=2*n-r,a=[],o=0;3>o;o++){var h=i[o];0>h&&(h+=1),h>1&&(h-=1),a[o]=1>6*h?s+6*(r-s)*h:1>2*h?r:2>3*h?s+(r-s)*(2/3-h)*6:s}return a},"rgb-gray":function(t,e,n){return[.2989*t+.587*e+.114*n]},"gray-rgb":function(t){return[t,t,t]},"gray-hsb":function(t){return[0,0,t]},"gray-hsl":function(t){return[0,0,t]},"gradient-rgb":function(){return[]},"rgb-gradient":function(){return[]}};return e.each(i,function(t,n){r[n]=[],e.each(t,function(t,s){var a=e.capitalize(t),o=/^(hue|saturation)$/.test(t),u=r[n][s]="gradient"===t?function(t){var e=this._components[0];return t=j.read(Array.isArray(t)?t:arguments,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"gradient"===n?function(){return h.read(arguments,0,{readNull:"highlight"===t,clone:!0})}:function(t){return null==t||isNaN(t)?0:t};this["get"+a]=function(){return this._type===n||o&&/^hs[bl]$/.test(this._type)?this._components[s]:this._convert(n)[s]},this["set"+a]=function(t){this._type===n||o&&/^hs[bl]$/.test(this._type)||(this._components=this._convert(n),this._properties=i[n],this._type=n),this._components[s]=u.call(this,t),this._changed()}},this)},{_class:"Color",_readIndex:!0,initialize:function c(e){var n,s,a,h,u=Array.prototype.slice,l=arguments,d=0;Array.isArray(e)&&(l=e,e=l[0]);var _=null!=e&&typeof e;if("string"===_&&e in i&&(n=e,e=l[1],Array.isArray(e)?(s=e,a=l[2]):(this.__read&&(d=1),l=u.call(l,1),_=typeof e)),!s){if(h="number"===_?l:"object"===_&&null!=e.length?e:null){n||(n=h.length>=3?"rgb":"gray");var f=i[n].length;a=h[f],this.__read&&(d+=h===arguments?f+(null!=a?1:0):1),h.length>f&&(h=u.call(h,0,f))}else if("string"===_)n="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===_)if(e.constructor===c){if(n=e._type,s=e._components.slice(),a=e._alpha,"gradient"===n)for(var g=1,v=s.length;v>g;g++){var p=s[g];p&&(s[g]=p.clone())}}else if(e.constructor===j)n="gradient",h=l;else{n="hue"in e?"lightness"in e?"hsl":"hsb":"gradient"in e||"stops"in e||"radial"in e?"gradient":"gray"in e?"gray":"rgb";var m=i[n],y=r[n];this._components=s=[];for(var g=0,v=m.length;v>g;g++){var w=e[m[g]];null==w&&0===g&&"gradient"===n&&"stops"in e&&(w={stops:e.stops,radial:e.radial}),w=y[g].call(this,w),null!=w&&(s[g]=w)}a=e.alpha}this.__read&&n&&(d=1)}if(this._type=n||"rgb",this._id=o.get(c),!s){this._components=s=[];for(var y=r[this._type],g=0,v=y.length;v>g;g++){var w=y[g].call(this,h&&h[g]);null!=w&&(s[g]=w)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=d)},_serialize:function(t,n){var i=this.getComponents();return e.serialize(/^(gray|rgb)$/.test(this._type)?i:[this._type].concat(i),t,!0,n)},_changed:function(){this._canvasStyle=null,this._owner&&this._owner._changed(65)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=l[this._type+"-"+t])?e.apply(this,this._components):l["rgb-"+t].apply(this,l[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new B(t,this._convert(t),this._alpha)},getType:function(){return this._type},setType:function(t){this._components=this._convert(t),this._properties=i[t],this._type=t},getComponents:function(){var t=this._components.slice();return null!=this._alpha&&t.push(this._alpha),t},getAlpha:function(){return null!=this._alpha?this._alpha:1},setAlpha:function(t){this._alpha=null==t?null:Math.min(Math.max(t,0),1),this._changed()},hasAlpha:function(){return null!=this._alpha},equals:function(t){var n=e.isPlainValue(t,!0)?B.read(arguments):t;return n===this||n&&this._class===n._class&&this._type===n._type&&this._alpha===n._alpha&&e.equals(this._components,n._components)||!1},toString:function(){for(var t=this._properties,e=[],n="gradient"===this._type,i=s.instance,r=0,a=t.length;a>r;r++){var o=this._components[r];null!=o&&e.push(t[r]+": "+(n?o:i.number(o)))}return null!=this._alpha&&e.push("alpha: "+i.number(this._alpha)),"{ "+e.join(", ")+" }"},toCSS:function(t){function e(t){return Math.round(255*(0>t?0:t>1?1:t))}var n=this._convert("rgb"),i=t||null==this._alpha?1:this._alpha;return n=[e(n[0]),e(n[1]),e(n[2])],1>i&&n.push(0>i?0:i),t?"#"+((1<<24)+(n[0]<<16)+(n[1]<<8)+n[2]).toString(16).slice(1):(4==n.length?"rgba(":"rgb(")+n.join(",")+")"},toCanvasStyle:function(t){if(this._canvasStyle)return this._canvasStyle;if("gradient"!==this._type)return this._canvasStyle=this.toCSS();var e,n=this._components,i=n[0],r=i._stops,s=n[1],a=n[2];if(i._radial){var o=a.getDistance(s),h=n[3];if(h){var u=h.subtract(s);u.getLength()>o&&(h=s.add(u.normalize(o-.1)))}var l=h||s;e=t.createRadialGradient(l.x,l.y,0,s.x,s.y,o)}else e=t.createLinearGradient(s.x,s.y,a.x,a.y);for(var c=0,d=r.length;d>c;c++){var _=r[c];e.addColorStop(_._rampPoint,_._color.toCanvasStyle())}return this._canvasStyle=e},transform:function(t){if("gradient"===this._type){for(var e=this._components,n=1,i=e.length;i>n;n++){var r=e[n];t._transformPoint(r,r,!0)}this._changed()}},statics:{_types:i,random:function(){var t=Math.random;return new B(t(),t(),t())}}})},new function(){var t={add:function(t,e){return t+e},subtract:function(t,e){return t-e},multiply:function(t,e){return t*e},divide:function(t,e){return t/e}};return e.each(t,function(t,e){this[e]=function(e){e=B.read(arguments);for(var n=this._type,i=this._components,r=e._convert(n),s=0,a=i.length;a>s;s++)r[s]=t(i[s],r[s]);return new B(n,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})}),j=e.extend({_class:"Gradient",initialize:function(t,e){this._id=o.get(),t&&this._set(t)&&(t=e=null),this._stops||this.setStops(t||["white","black"]),null==this._radial&&this.setRadial("string"==typeof e&&"radial"===e||e||!1)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._stops,this._radial],t,!0,n)})},_changed:function(){for(var t=0,e=this._owners&&this._owners.length;e>t;t++)this._owners[t]._changed()},_addOwner:function(t){this._owners||(this._owners=[]),this._owners.push(t)},_removeOwner:function(e){var n=this._owners?this._owners.indexOf(e):-1;-1!=n&&(this._owners.splice(n,1),0===this._owners.length&&(this._owners=t))},clone:function(){for(var t=[],e=0,n=this._stops.length;n>e;e++)t[e]=this._stops[e].clone();return new j(t,this._radial)},getStops:function(){return this._stops},setStops:function(e){if(this.stops)for(var n=0,i=this._stops.length;i>n;n++)this._stops[n]._owner=t;if(e.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=D.readAll(e,0,{clone:!0});for(var n=0,i=this._stops.length;i>n;n++){var r=this._stops[n];r._owner=this,r._defaultRamp&&r.setRampPoint(n/(i-1))}this._changed()},getRadial:function(){return this._radial},setRadial:function(t){this._radial=t,this._changed()},equals:function(t){if(t===this)return!0;if(t&&this._class===t._class&&this._stops.length===t._stops.length){for(var e=0,n=this._stops.length;n>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),D=e.extend({_class:"GradientStop",initialize:function(e,n){if(e){var i,r;n===t&&Array.isArray(e)?(i=e[0],r=e[1]):e.color?(i=e.color,r=e.rampPoint):(i=e,r=n),this.setColor(i),this.setRampPoint(r)}},clone:function(){return new D(this._color.clone(),this._rampPoint)},_serialize:function(t,n){return e.serialize([this._color,this._rampPoint],t,!0,n)},_changed:function(){this._owner&&this._owner._changed(65)},getRampPoint:function(){return this._rampPoint},setRampPoint:function(t){this._defaultRamp=null==t,this._rampPoint=t||0,this._changed()},getColor:function(){return this._color},setColor:function(t){this._color=B.read(arguments),this._color===t&&(this._color=t.clone()),this._color._owner=this,this._changed()},equals:function(t){return t===this||t&&this._class===t._class&&this._color.equals(t._color)&&this._rampPoint==t._rampPoint||!1}}),R=e.extend(new function(){var n={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",strokeScaling:!0,miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,fontFamily:"sans-serif",fontWeight:"normal",fontSize:12,font:"sans-serif",leading:null,justification:"left"},i={strokeWidth:97,strokeCap:97,strokeJoin:97,strokeScaling:105,miterLimit:97,fontFamily:9,fontWeight:9,fontSize:9,font:9,leading:9,justification:9},r={beans:!0},s={_defaults:n,_textDefaults:new e(n,{fillColor:new B}),beans:!0};return e.each(n,function(n,a){var o=/Color$/.test(a),u="shadowOffset"===a,l=e.capitalize(a),c=i[a],d="set"+l,_="get"+l;s[d]=function(e){var n=this._owner,i=n&&n._children;if(i&&i.length>0&&!(n instanceof A))for(var r=0,s=i.length;s>r;r++)i[r]._style[d](e);else{var h=this._values[a];h!==e&&(o&&(h&&(h._owner=t),e&&e.constructor===B&&(e._owner&&(e=e.clone()),e._owner=n)),this._values[a]=e,n&&n._changed(c||65))}},s[_]=function(n){var i,r=this._owner,s=r&&r._children;if(!s||0===s.length||n||r instanceof A){var i=this._values[a];if(i===t)i=this._defaults[a],i&&i.clone&&(i=i.clone());else{var l=o?B:u?h:null;!l||i&&i.constructor===l||(this._values[a]=i=l.read([i],0,{readNull:!0,clone:!0}),i&&o&&(i._owner=r))}return i}for(var c=0,d=s.length;d>c;c++){var f=s[c]._style[_]();if(0===c)i=f;else if(!e.equals(i,f))return t}return i},r[_]=function(t){return this._style[_](t)},r[d]=function(t){this._style[d](t)}}),m.inject(r),s},{_class:"Style",initialize:function(t,e,n){this._values={},this._owner=e,this._project=e&&e._project||n||paper.project,e instanceof L&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof R,n=e?t._values:t;if(n)for(var i in n)if(i in this._defaults){var r=n[i];this[i]=r&&e&&r.clone?r.clone():r}},equals:function(t){return t===this||t&&this._class===t._class&&e.equals(this._values,t._values)||!1},hasFill:function(){return!!this.getFillColor()},hasStroke:function(){return!!this.getStrokeColor()&&this.getStrokeWidth()>0},hasShadow:function(){return!!this.getShadowColor()&&this.getShadowBlur()>0},getView:function(){return this._project.getView()},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function wt(){var t=wt.base.call(this),e=this.getFontSize();return/pt|em|%|px/.test(e)&&(e=this.getView().getPixelSize(e)),null!=t?t:1.2*e}}),F=new function(){function t(t,e,n,i){for(var r=["","webkit","moz","Moz","ms","o"],s=e[0].toUpperCase()+e.substring(1),a=0;6>a;a++){var o=r[a],h=o?o+s:e;if(h in t){if(!n)return t[h];t[h]=i;break}}}return{getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getBounds:function(t,e){var n,i=t.ownerDocument,r=i.body,s=i.documentElement;try{n=t.getBoundingClientRect()}catch(a){n={left:0,top:0,width:0,height:0}}var o=n.left-(s.clientLeft||r.clientLeft||0),h=n.top-(s.clientTop||r.clientTop||0);if(!e){var u=i.defaultView;o+=u.pageXOffset||s.scrollLeft||r.scrollLeft,h+=u.pageYOffset||s.scrollTop||r.scrollTop}return new d(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new d(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return F.getBounds(t,e).getPoint()},getSize:function(t){return F.getBounds(t,!0).getSize()},isInvisible:function(t){return F.getSize(t).equals(new l(0,0))},isInView:function(t){return!F.isInvisible(t)&&F.getViewportBounds(t).intersects(F.getBounds(t,!0))},getPrefixed:function(e,n){return t(e,n)},setPrefixed:function(e,n,i){if("object"==typeof n)for(var r in n)t(e,r,!0,n[r]);else t(e,n,!0,i)}}},q={add:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.addEventListener(r[s],i,!1)},remove:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.removeEventListener(r[s],i,!1)},getPoint:function(t){var e=t.targetTouches?t.targetTouches.length?t.targetTouches[0]:t.changedTouches[0]:t;return new h(e.pageX||e.clientX+document.documentElement.scrollLeft,e.pageY||e.clientY+document.documentElement.scrollTop)},getTarget:function(t){return t.target||t.srcElement},getRelatedTarget:function(t){return t.relatedTarget||t.toElement},getOffset:function(t,e){return q.getPoint(t).subtract(F.getOffset(e||q.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};q.requestAnimationFrame=new function(){function t(){for(var e=s.length-1;e>=0;e--){var o=s[e],h=o[0],u=o[1];(!u||("true"==i.getAttribute(u,"keepalive")||a)&&F.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):r=!1)}var e,n=F.getPrefixed(window,"requestAnimationFrame"),r=!1,s=[],a=!0;return q.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(i,a){s.push([i,a]),n?r||(n(t),r=!0):e||(e=setInterval(t,1e3/60))}};var V=e.extend(n,{_class:"View",initialize:function xt(t,e){function n(t){return e[t]||parseInt(e.getAttribute(t),10)}function r(){var t=F.getSize(e);return t.isNaN()||t.isZero()?new l(n("width"),n("height")):t}this._project=t,this._scope=t._scope,this._element=e;var s;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=e.getAttribute("id"),null==this._id&&e.setAttribute("id",this._id="view-"+xt._id++),q.add(e,this._viewEvents);var a="none";if(F.setPrefixed(e.style,{userSelect:a,touchAction:a,touchCallout:a,contentZooming:a,userDrag:a,tapHighlightColor:"rgba(0,0,0,0)"}),i.hasAttribute(e,"resize")){var o=this;q.add(window,this._windowEvents={resize:function(){o.setViewSize(r())}})}if(this._setViewSize(s=r()),i.hasAttribute(e,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var h=this._stats.domElement,u=h.style,c=F.getOffset(e);u.position="absolute",u.left=c.x+"px",u.top=c.y+"px",document.body.appendChild(h)}xt._views.push(this),xt._viewsById[this._id]=this,this._viewSize=s,(this._matrix=new f)._owner=this,this._zoom=1,xt._focused||(xt._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(V._focused===this&&(V._focused=null),V._views.splice(V._views.indexOf(this),1),delete V._viewsById[this._id],this._project._view===this&&(this._project._view=null),q.remove(this._element,this._viewEvents),q.remove(window,this._windowEvents),this._element=this._project=null,this.off("frame"),this._animate=!1,this._frameItems={},!0):!1},_events:e.each(["onResize","onMouseDown","onMouseUp","onMouseMove"],function(t){this[t]={install:function(t){this._installEvent(t)},uninstall:function(t){this._uninstallEvent(t)}}},{onFrame:{install:function(){this.play()},uninstall:function(){this.pause()}}}),_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;q.requestAnimationFrame(function(){t._requested=!1,t._animate&&(t._requestFrame(),t._handleFrame())},this._element),this._requested=!0},_handleFrame:function(){paper=this._scope;var t=Date.now()/1e3,n=this._before?t-this._before:0;this._before=t,this._handlingFrame=!0,this.emit("frame",new e({delta:n,time:this._time+=n,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.update()},_animateItem:function(t,e){var n=this._frameItems;e?(n[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.on("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.off("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.emit("frame",new e(t,{time:i.time+=t.delta,count:i.count++}))}},_update:function(){this._project._needsUpdate=!0,this._handlingFrame||(this._animate?this._handleFrame():this.update())},_changed:function(t){1&t&&(this._project._needsUpdate=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._update()},getElement:function(){return this._element},getPixelRatio:function(){return this._pixelRatio},getResolution:function(){return 72*this._pixelRatio},getViewSize:function(){var t=this._viewSize;return new c(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=l.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.emit("resize",{size:t,delta:e}),this._update())},_setViewSize:function(t){var e=this._element;e.width=t.width,e.height=t.height},getBounds:function(){return this._bounds||(this._bounds=this._matrix.inverted()._transformBounds(new d(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(){var t=h.read(arguments);this.scrollBy(t.subtract(this.getCenter()))},getZoom:function(){return this._zoom},setZoom:function(t){this._transform((new f).scale(t/this._zoom,this.getCenter())),this._zoom=t},isVisible:function(){return F.isInView(this._element)},scrollBy:function(){ +this._transform((new f).translate(h.read(arguments).negate()))},play:function(){this._animate=!0,this._requested||this._requestFrame()},pause:function(){this._animate=!1},draw:function(){this.update()},projectToView:function(){return this._matrix._transformPoint(h.read(arguments))},viewToProject:function(){return this._matrix._inverseTransform(h.read(arguments))}},{statics:{_views:[],_viewsById:{},_id:0,create:function(t,e){return"string"==typeof e&&(e=document.getElementById(e)),new H(t,e)}}},new function(){function t(t){var e=q.getTarget(t);return e.getAttribute&&V._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(q.getOffset(e,t._element))}function n(){if(!V._focused||!V._focused.isVisible())for(var t=0,e=V._views.length;e>t;t++){var n=V._views[t];if(n&&n.isVisible()){V._focused=a=n;break}}}function i(t,e,n){t._handleEvent("mousemove",e,n);var i=t._scope.tool;return i&&i._handleEvent(l&&i.responds("mousedrag")?"mousedrag":"mousemove",e,n),t.update(),i}var r,s,a,o,h,u,l=!1,c=window.navigator;c.pointerEnabled||c.msPointerEnabled?(o="pointerdown MSPointerDown",h="pointermove MSPointerMove",u="pointerup pointercancel MSPointerUp MSPointerCancel"):(o="touchstart",h="touchmove",u="touchend touchcancel","ontouchstart"in window&&c.userAgent.match(/mobile|tablet|ip(ad|hone|od)|android|silk/i)||(o+=" mousedown",h+=" mousemove",u+=" mouseup"));var d={"selectstart dragstart":function(t){l&&t.preventDefault()}},_={mouseout:function(t){var n=V._focused,r=q.getRelatedTarget(t);!n||r&&"HTML"!==r.nodeName||i(n,e(n,t),t)},scroll:n};d[o]=function(n){var i=V._focused=t(n),s=e(i,n);l=!0,i._handleEvent("mousedown",s,n),(r=i._scope.tool)&&r._handleEvent("mousedown",s,n),i.update()},_[h]=function(o){var h=V._focused;if(!l){var u=t(o);u?(h!==u&&i(h,e(h,o),o),s=h,h=V._focused=a=u):a&&a===h&&(h=V._focused=s,n())}if(h){var c=e(h,o);(l||h.getBounds().contains(c))&&(r=i(h,c,o))}},_[u]=function(t){var n=V._focused;if(n&&l){var i=e(n,t);l=!1,n._handleEvent("mouseup",i,t),r&&r._handleEvent("mouseup",i,t),n.update()}},q.add(document,_),q.add(window,{load:n});var f={mousedown:{mousedown:1,mousedrag:1,click:1,doubleclick:1},mouseup:{mouseup:1,mousedrag:1,click:1,doubleclick:1},mousemove:{mousedrag:1,mousemove:1,mouseenter:1,mouseleave:1}};return{_viewEvents:d,_handleEvent:function(){},_installEvent:function(t){var e=this._eventCounters;if(e)for(var n in f)e[n]=(e[n]||0)+(f[n][t]||0)},_uninstallEvent:function(t){var e=this._eventCounters;if(e)for(var n in f)e[n]-=f[n][t]||0},statics:{updateFocus:n}}}),H=V.extend({_class:"CanvasView",initialize:function(t,e){if(!(e instanceof HTMLCanvasElement)){var n=l.read(arguments,1);if(n.isZero())throw Error("Cannot create CanvasView with the provided argument: "+[].slice.call(arguments,1));e=Y.getCanvas(n)}if(this._context=e.getContext("2d"),this._eventCounters={},this._pixelRatio=1,!/^off|false$/.test(i.getAttribute(e,"hidpi"))){var r=window.devicePixelRatio||1,s=F.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=r/s}V.call(this,t,e)},_setViewSize:function(t){var e=this._element,n=this._pixelRatio,r=t.width,s=t.height;if(e.width=r*n,e.height=s*n,1!==n){if(!i.hasAttribute(e,"resize")){var a=e.style;a.width=r+"px",a.height=s+"px"}this._context.scale(n,n)}},getPixelSize:function(t){var e,n=paper.browser;if(n&&n.firefox){var i=this._element.parentNode,r=document.createElement("div");r.style.fontSize=t,i.appendChild(r),e=parseFloat(F.getStyles(r).fontSize),i.removeChild(r)}else{var s=this._context,a=s.font;s.font=t+" serif",e=parseFloat(s.font),s.font=a}return e},getTextWidth:function(t,e){var n=this._context,i=n.font,r=0;n.font=t;for(var s=0,a=e.length;a>s;s++)r=Math.max(r,n.measureText(e[s]).width);return n.font=i,r},update:function(t){var e=this._project;if(!e||!t&&!e._needsUpdate)return!1;var n=this._context,i=this._viewSize;return n.clearRect(0,0,i.width+1,i.height+1),e.draw(n,this._matrix,this._pixelRatio),e._needsUpdate=!1,!0}},new function(){function e(e,n,i,r,s,a){function o(e){return e.responds(n)&&(h||(h=new U(n,i,r,s,a?r.subtract(a):null)),e.emit(n,h)&&h.isStopped)?(i.preventDefault(),!0):t}for(var h,u=s;u;){if(o(u))return!0;u=u.getParent()}return o(e)?!0:!1}var n,i,r,s,a,o,h,u,l;return{_handleEvent:function(t,c,d){if(this._eventCounters[t]){var _=this._project,f=_.hitTest(c,{tolerance:0,fill:!0,stroke:!0}),g=f&&f.item,v=!1;switch(t){case"mousedown":for(v=e(this,t,d,c,g),u=a==g&&Date.now()-l<300,s=a=g,n=i=r=c,h=!v&&g;h&&!h.responds("mousedrag");)h=h._parent;break;case"mouseup":v=e(this,t,d,c,g,n),h&&(i&&!i.equals(c)&&e(this,"mousedrag",d,c,h,i),g!==h&&(r=c,e(this,"mousemove",d,c,g,r))),!v&&g&&g===s&&(l=Date.now(),e(this,u&&s.responds("doubleclick")?"doubleclick":"click",d,n,g),u=!1),s=h=null;break;case"mousemove":h&&(v=e(this,"mousedrag",d,c,h,i)),v||(g!==o&&(r=c),v=e(this,t,d,c,g,r)),i=r=c,g!==o&&(e(this,"mouseleave",d,c,o),o=g,e(this,"mouseenter",d,c,g))}return v}}}}),Z=e.extend({_class:"Event",initialize:function(t){this.event=t},isPrevented:!1,isStopped:!1,preventDefault:function(){this.isPrevented=!0,this.event.preventDefault()},stopPropagation:function(){this.isStopped=!0,this.event.stopPropagation()},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return G.modifiers}}),W=Z.extend({_class:"KeyEvent",initialize:function(t,e,n,i){Z.call(this,i),this.type=t?"keydown":"keyup",this.key=e,this.character=n},toString:function(){return"{ type: '"+this.type+"', key: '"+this.key+"', character: '"+this.character+"', modifiers: "+this.getModifiers()+" }"}}),G=new function(){function t(i,s,u,l){var c,d=u?String.fromCharCode(u):"",_=r[s],f=_||d.toLowerCase(),g=i?"keydown":"keyup",v=V._focused,p=v&&v.isVisible()&&v._scope,m=p&&p.tool;if(h[f]=i,i?o[s]=u:delete o[s],_&&(c=e.camelize(_))in a){a[c]=i;var y=paper.browser;if("command"===c&&y&&y.mac)if(i)n={};else{for(var w in n)w in o&&t(!1,w,n[w],l);n=null}}else i&&n&&(n[s]=u);m&&m.responds(g)&&(paper=p,m.emit(g,new W(i,f,d,l)),v&&v.update())}var n,i,r={8:"backspace",9:"tab",13:"enter",16:"shift",17:"control",18:"option",19:"pause",20:"caps-lock",27:"escape",32:"space",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",46:"delete",91:"command",93:"command",224:"command"},s={9:!0,13:!0,32:!0},a=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),o={},h={};return q.add(document,{keydown:function(e){var n=e.which||e.keyCode;n in r||a.command?t(!0,n,n in s||a.command?n:0,e):i=n},keypress:function(e){null!=i&&(t(!0,i,e.which||e.keyCode,e),i=null)},keyup:function(e){var n=e.which||e.keyCode;n in o&&t(!1,n,o[n],e)}}),q.add(window,{blur:function(e){for(var n in o)t(!1,n,o[n],e)}}),{modifiers:a,isDown:function(t){return!!h[t]}}},U=Z.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){Z.call(this,e),this.type=t,this.point=n,this.target=i,this.delta=r},toString:function(){return"{ type: '"+this.type+"', point: "+this.point+", target: "+this.target+(this.delta?", delta: "+this.delta:"")+", modifiers: "+this.getModifiers()+" }"}}),J=Z.extend({_class:"ToolEvent",_item:null,initialize:function(t,e,n){this.tool=t,this.type=e,this.event=n},_choosePoint:function(t,e){return t?t:e?e.clone():null},getPoint:function(){return this._choosePoint(this._point,this.tool._point)},setPoint:function(t){this._point=t},getLastPoint:function(){return this._choosePoint(this._lastPoint,this.tool._lastPoint)},setLastPoint:function(t){this._lastPoint=t},getDownPoint:function(){return this._choosePoint(this._downPoint,this.tool._downPoint)},setDownPoint:function(t){this._downPoint=t},getMiddlePoint:function(){return!this._middlePoint&&this.tool._lastPoint?this.tool._point.add(this.tool._lastPoint).divide(2):this._middlePoint},setMiddlePoint:function(t){this._middlePoint=t},getDelta:function(){return!this._delta&&this.tool._lastPoint?this.tool._point.subtract(this.tool._lastPoint):this._delta},setDelta:function(t){this._delta=t},getCount:function(){return/^mouse(down|up)$/.test(this.type)?this.tool._downCount:this.tool._count},setCount:function(t){this.tool[/^mouse(down|up)$/.test(this.type)?"downCount":"count"]=t},getItem:function(){if(!this._item){var t=this.tool._scope.project.hitTest(this.getPoint());if(t){for(var e=t.item,n=e._parent;/^(Group|CompoundPath)$/.test(n._class);)e=n,n=n._parent;this._item=e}}return this._item},setItem:function(t){this._item=t},toString:function(){return"{ type: "+this.type+", point: "+this.getPoint()+", count: "+this.getCount()+", modifiers: "+this.getModifiers()+" }"}}),X=(r.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){r.call(this),this._firstMove=!0,this._count=0,this._downCount=0,this._set(t)},getMinDistance:function(){return this._minDistance},setMinDistance:function(t){this._minDistance=t,null!=t&&null!=this._maxDistance&&t>this._maxDistance&&(this._maxDistance=t)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=t&&tu)return!1;if(null!=i&&0!=i)if(u>i)e=this._point.add(h.normalize(i));else if(a)return!1}if(s&&e.equals(this._point))return!1}switch(this._lastPoint=r&&"mousemove"==t?e:this._point,this._point=e,t){case"mousedown":this._lastPoint=this._downPoint,this._downPoint=this._point,this._downCount++;break;case"mouseup":this._lastPoint=this._downPoint}return this._count=r?0:this._count+1,!0},_fireEvent:function(t,e){var n=paper.project._removeSets;if(n){"mouseup"===t&&(n.mousedrag=null);var i=n[t];if(i){for(var r in i){var s=i[r];for(var a in n){var o=n[a];o&&o!=i&&delete o[s._id]}s.remove()}n[t]=null}}return this.responds(t)&&this.emit(t,new J(this,t,e))},_handleEvent:function(t,e,n){paper=this._scope;var i=!1;switch(t){case"mousedown":this._updateEvent(t,e,null,null,!0,!1,!1),i=this._fireEvent(t,n);break;case"mousedrag":for(var r=!1,s=!1;this._updateEvent(t,e,this.minDistance,this.maxDistance,!1,r,s);)i=this._fireEvent(t,n)||i,r=!0,s=!0;break;case"mouseup":!e.equals(this._point)&&this._updateEvent("mousedrag",e,this.minDistance,this.maxDistance,!1,!1,!1)&&(i=this._fireEvent("mousedrag",n)),this._updateEvent(t,e,null,this.maxDistance,!1,!1,!1),i=this._fireEvent(t,n)||i,this._updateEvent(t,e,null,null,!0,!1,!1),this._firstMove=!0;break;case"mousemove":for(;this._updateEvent(t,e,this.minDistance,this.maxDistance,this._firstMove,!0,!1);)i=this._fireEvent(t,n)||i,this._firstMove=!1}return i&&n.preventDefault(),i}}),{request:function(e,n,i,r){r=r===t?!0:r;var s=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return s.open(e.toUpperCase(),n,r),"overrideMimeType"in s&&s.overrideMimeType("text/plain"),s.onreadystatechange=function(){if(4===s.readyState){var t=s.status;if(0!==t&&200!==t)throw Error("Could not load "+n+" (Error "+t+")");i.call(s,s.responseText)}},s.send(null)}}),Y={canvases:[],getCanvas:function(t,e){var n,i=!0;"object"==typeof t&&(e=t.height,t=t.width),n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var r=n.getContext("2d");return n.width===t&&n.height===e?i&&r.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),r.save(),n},getContext:function(t,e){return this.getCanvas(t,e).getContext("2d")},release:function(t){var e=t.canvas?t.canvas:t;e.getContext("2d").restore(),this.canvases.push(e)}},$=new function(){function t(t,e,n){return.2989*t+.587*e+.114*n}function n(e,n,i,r){var s=r-t(e,n,i);_=e+s,f=n+s,g=i+s;var r=t(_,f,g),a=v(_,f,g),o=p(_,f,g);if(0>a){var h=r-a;_=r+(_-r)*r/h,f=r+(f-r)*r/h,g=r+(g-r)*r/h}if(o>255){var u=255-r,l=o-r;_=r+(_-r)*u/l,f=r+(f-r)*u/l,g=r+(g-r)*u/l}}function i(t,e,n){return p(t,e,n)-v(t,e,n)}function r(t,e,n,i){var r,s=[t,e,n],a=p(t,e,n),o=v(t,e,n);o=o===t?0:o===e?1:2,a=a===t?0:a===e?1:2,r=0===v(o,a)?1===p(o,a)?2:1:0,s[a]>s[o]?(s[r]=(s[r]-s[o])*i/(s[a]-s[o]),s[a]=i):s[r]=s[a]=0,s[o]=0,_=s[0],f=s[1],g=s[2]}var s,a,o,h,u,l,c,d,_,f,g,v=Math.min,p=Math.max,m=Math.abs,y={multiply:function(){_=u*s/255,f=l*a/255,g=c*o/255},screen:function(){_=u+s-u*s/255,f=l+a-l*a/255,g=c+o-c*o/255},overlay:function(){_=128>u?2*u*s/255:255-2*(255-u)*(255-s)/255,f=128>l?2*l*a/255:255-2*(255-l)*(255-a)/255,g=128>c?2*c*o/255:255-2*(255-c)*(255-o)/255},"soft-light":function(){var t=s*u/255;_=t+u*(255-(255-u)*(255-s)/255-t)/255,t=a*l/255,f=t+l*(255-(255-l)*(255-a)/255-t)/255,t=o*c/255,g=t+c*(255-(255-c)*(255-o)/255-t)/255},"hard-light":function(){_=128>s?2*s*u/255:255-2*(255-s)*(255-u)/255,f=128>a?2*a*l/255:255-2*(255-a)*(255-l)/255,g=128>o?2*o*c/255:255-2*(255-o)*(255-c)/255},"color-dodge":function(){_=0===u?0:255===s?255:v(255,255*u/(255-s)),f=0===l?0:255===a?255:v(255,255*l/(255-a)),g=0===c?0:255===o?255:v(255,255*c/(255-o))},"color-burn":function(){_=255===u?255:0===s?0:p(0,255-255*(255-u)/s),f=255===l?255:0===a?0:p(0,255-255*(255-l)/a),g=255===c?255:0===o?0:p(0,255-255*(255-c)/o)},darken:function(){_=s>u?u:s,f=a>l?l:a,g=o>c?c:o},lighten:function(){_=u>s?u:s,f=l>a?l:a,g=c>o?c:o},difference:function(){_=u-s,0>_&&(_=-_),f=l-a,0>f&&(f=-f),g=c-o,0>g&&(g=-g)},exclusion:function(){_=u+s*(255-u-u)/255,f=l+a*(255-l-l)/255,g=c+o*(255-c-c)/255},hue:function(){r(s,a,o,i(u,l,c)),n(_,f,g,t(u,l,c))},saturation:function(){r(u,l,c,i(s,a,o)),n(_,f,g,t(u,l,c))},luminosity:function(){n(u,l,c,t(s,a,o))},color:function(){n(s,a,o,t(u,l,c))},add:function(){_=v(u+s,255),f=v(l+a,255),g=v(c+o,255)},subtract:function(){_=p(u-s,0),f=p(l-a,0),g=p(c-o,0)},average:function(){_=(u+s)/2,f=(l+a)/2,g=(c+o)/2},negation:function(){_=255-m(255-s-u),f=255-m(255-a-l),g=255-m(255-o-c)}},w=this.nativeModes=e.each(["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","darker","copy","xor"],function(t){this[t]=!0},{}),x=Y.getContext(1,1);e.each(y,function(t,e){var n="darken"===e,i=!1;x.save();try{x.fillStyle=n?"#300":"#a00",x.fillRect(0,0,1,1),x.globalCompositeOperation=e,x.globalCompositeOperation===e&&(x.fillStyle=n?"#a00":"#300",x.fillRect(0,0,1,1),i=x.getImageData(0,0,1,1).data[0]!==n?170:51)}catch(r){}x.restore(),w[e]=i}),Y.release(x),this.process=function(t,e,n,i,r){var v=e.canvas,p="normal"===t;if(p||w[t])n.save(),n.setTransform(1,0,0,1,0,0),n.globalAlpha=i,p||(n.globalCompositeOperation=t),n.drawImage(v,r.x,r.y),n.restore();else{var m=y[t];if(!m)return;for(var x=n.getImageData(r.x,r.y,v.width,v.height),b=x.data,C=e.getImageData(0,0,v.width,v.height).data,S=0,P=b.length;P>S;S+=4){s=C[S],u=b[S],a=C[S+1],l=b[S+1],o=C[S+2],c=b[S+2],h=C[S+3],d=b[S+3],m();var M=h*i/255,I=1-M;b[S]=M*_+I*u,b[S+1]=M*f+I*l,b[S+2]=M*g+I*c,b[S+3]=h*i+I*d}n.putImageData(x,r.x,r.y)}}},K=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],strokeScaling:["vector-effect","lookup",{"true":"none","false":"non-scaling-stroke"},function(t,e){return!e&&(t instanceof k||t instanceof x||t instanceof L)}],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],fontFamily:["font-family","string"],fontWeight:["font-weight","string"],fontSize:["font-size","number"],justification:["text-anchor","lookup",{left:"start",center:"middle",right:"end"}],opacity:["opacity","number"],blendMode:["mix-blend-mode","string"]},function(t,n){var i=e.capitalize(n),r=t[2];this[n]={type:t[1],property:n,attribute:t[0],toSVG:r,fromSVG:r&&e.each(r,function(t,e){this[t]=e},{}),exportFilter:t[3],get:"get"+i,set:"set"+i}},{}),Q={href:"http://www.w3.org/1999/xlink",xlink:"http://www.w3.org/2000/xmlns"};return new function(){function t(t,e){for(var n in e){var i=e[n],r=Q[n];"number"==typeof i&&(i=b.number(i)),r?t.setAttributeNS(r,n,i):t.setAttribute(n,i)}return t}function n(e,n){return t(document.createElementNS("http://www.w3.org/2000/svg",e),n)}function i(t,n,i){var r=new e,s=t.getTranslation();if(n){t=t.shiftless();var o=t._inverseTransform(s);r[i?"cx":"x"]=o.x,r[i?"cy":"y"]=o.y,s=null}if(!t.isIdentity()){var h=t.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;s&&!s.isZero()&&u.push("translate("+b.point(s)+")"),a.isZero(c.x-1)&&a.isZero(c.y-1)||u.push("scale("+b.point(c)+")"),l&&u.push("rotate("+b.number(l)+")"),r.transform=u.join(" ")}else r.transform="matrix("+t.getValues().join(",")+")"}return r}function r(e,r){for(var s=i(e._matrix),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=w(l,r);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),p(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function o(t,e){var r=i(t._matrix,!0),s=t.getSize(),a=t.getImage();return r.x-=s.width/2,r.y-=s.height/2,r.width=s.width,r.height=s.height,r.href=e.embedImages===!1&&a&&a.src||t.toDataURL(),n("image",r)}function h(t,e){var r=e.matchShapes;if(r){var s=t.toShape(!1);if(s)return u(s,e)}var a,o=t._segments,h=i(t._matrix);if(0===o.length)return null;if(r&&!t.hasHandles())if(o.length>=3){a=t._closed?"polygon":"polyline";for(var l=[],c=0,d=o.length;d>c;c++)l.push(b.point(o[c]._point));h.points=l.join(" ")}else{a="line";var _=o[0]._point,f=o[o.length-1]._point;h.set({x1:_.x,y1:_.y,x2:f.x,y2:f.y})}else a="path",h.d=t.getPathData(null,e.precision);return n(a,h)}function u(t){var e=t._type,r=t._radius,s=i(t._matrix,!0,"rectangle"!==e);if("rectangle"===e){e="rect";var a=t._size,o=a.width,h=a.height;s.x-=o/2,s.y-=h/2,s.width=o,s.height=h,r.isZero()&&(r=null)}return r&&("circle"===e?s.r=r:(s.rx=r.width,s.ry=r.height)),n(e,s)}function l(t,e){var r=i(t._matrix),s=t.getPathData(null,e.precision);return s&&(r.d=s),n("path",r)}function c(t,e){var r=i(t._matrix,!0),s=t.getSymbol(),a=g(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:b.rectangle(h)}),a.appendChild(w(o,e)),p(s,a,"symbol")),r.href="#"+a.id,r.x+=h.x,r.y+=h.y,r.width=b.number(h.width),r.height=b.number(h.height),r.overflow="visible",n("use",r)}function d(t){var e=g(t,"color");if(!e){var i,r=t.getGradient(),s=r._radial,a=t.getOrigin().transform(),o=t.getDestination().transform();if(s){i={cx:a.x,cy:a.y,r:a.getDistance(o)};var h=t.getHighlight();h&&(h=h.transform(),i.fx=h.x,i.fy=h.y)}else i={x1:a.x,y1:a.y,x2:o.x,y2:o.y};i.gradientUnits="userSpaceOnUse",e=n((s?"radial":"linear")+"Gradient",i);for(var u=r._stops,l=0,c=u.length;c>l;l++){var d=u[l],_=d._color,f=_.getAlpha();i={offset:d._rampPoint,"stop-color":_.toCSS(!0)},1>f&&(i["stop-opacity"]=f),e.appendChild(n("stop",i))}p(t,e,"color")}return"url(#"+e.id+")"}function _(t){var e=n("text",i(t._matrix,!0));return e.textContent=t._content,e}function f(n,i,r){var s={},a=!r&&n.getParent();return null!=n._name&&(s.id=n._name),e.each(K,function(t){var i=t.get,r=t.type,o=n[i]();if(t.exportFilter?t.exportFilter(n,o):!a||!e.equals(a[i](),o)){if("color"===r&&null!=o){var h=o.getAlpha();1>h&&(s[t.attribute+"-opacity"]=h)}s[t.attribute]=null==o?"none":"number"===r?b.number(o):"color"===r?o.gradient?d(o,n):o.toCSS(!0):"array"===r?o.join(","):"lookup"===r?t.toSVG[o]:o}}),1===s.opacity&&delete s.opacity,n._visible||(s.visibility="hidden"),t(i,s)}function g(t,e){return C||(C={ids:{},svgs:{}}),t&&C.svgs[e+"-"+t._id]}function p(t,e,n){C||g();var i=C.ids[n]=(C.ids[n]||0)+1;e.id=n+"-"+i,C.svgs[n+"-"+t._id]=e}function y(t,e){var i=t,r=null;if(C){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in C.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(C.svgs[s]);C=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function w(t,e,n){var i=S[t._class],r=i&&i(t,e);if(r){var s=e.onExport;s&&(r=s(t,r,e)||r);var a=JSON.stringify(t._data);a&&"{}"!==a&&"null"!==a&&r.setAttribute("data-paper-data",a)}return r&&f(t,r,n)}function x(t){return t||(t={}),b=new s(t.precision),t}var b,C,S={Group:r,Layer:r,Raster:o,Path:h,Shape:u,CompoundPath:l,PlacedSymbol:c,PointText:_};m.inject({exportSVG:function(t){return t=x(t),y(w(this,t,!0),t)}}),v.inject({exportSVG:function(t){t=x(t);var e=this.layers,r=this.getView(),s=r.getViewSize(),a=n("svg",{x:0,y:0,width:s.width,height:s.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),o=a,h=r._matrix;h.isIdentity()||(o=a.appendChild(n("g",i(h))));for(var u=0,l=e.length;l>u;u++)o.appendChild(w(e[u],t,!0));return y(a,t)}})},new function(){function n(t,e,n,i){var r=Q[e],s=r?t.getAttributeNS(r,e):t.getAttribute(e);return"null"===s&&(s=null),null==s?i?null:n?"":0:n?s:parseFloat(s)}function i(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new h(e,i):null}function r(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new l(e,i):null}function s(t,e,n){return"none"===t?null:"number"===e?parseFloat(t):"array"===e?t?t.split(/[\s,]+/g).map(parseFloat):[]:"color"===e?S(t)||t:"lookup"===e?n[t]:t}function a(t,e,n,i){var r=t.childNodes,s="clippath"===e,a=new y,o=a._project,h=o._currentStyle,u=[];if(s||(a=C(a,t,i),o._currentStyle=a._style.clone()),i)for(var l=t.querySelectorAll("defs"),c=0,d=l.length;d>c;c++)P(l[c],n,!1);for(var c=0,d=r.length;d>c;c++){var _,f=r[c];1!==f.nodeType||"defs"===f.nodeName.toLowerCase()||!(_=P(f,n,!1))||_ instanceof p||u.push(_)}return a.addChildren(u),s&&(a=C(a.reduce(),t,i)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){for(var n=t.getAttribute("points").match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g),i=[],r=0,s=n.length;s>r;r+=2)i.push(new h(parseFloat(n[r]),parseFloat(n[r+1])));var a=new O(i);return"polygon"===e&&a.closePath(),a}function u(t){var e=t.getAttribute("d"),n={pathData:e};return(e.match(/m/gi)||[]).length>1||/z\S+/i.test(e)?new A(n):new O(n)}function c(t,e){var r,s=(n(t,"href",!0)||"").substring(1),a="radialgradient"===e;if(s)r=z[s].getGradient();else{for(var o=t.childNodes,h=[],u=0,l=o.length;l>u;u++){var c=o[u];1===c.nodeType&&h.push(C(new D,c))}r=new j(h,a)}var d,_,f;return a?(d=i(t,"cx","cy"),_=d.add(n(t,"r"),0),f=i(t,"fx","fy",!0)):(d=i(t,"x1","y1"),_=i(t,"x2","y2")),C(new B(r,d,_,f),t),null}function _(t,e,n,i){for(var r=(i.getAttribute(n)||"").split(/\)\s*/g),s=new f,a=0,o=r.length;o>a;a++){var h=r[a];if(!h)break;for(var u=h.split(/\(\s*/),l=u[0],c=u[1].split(/[\s,]+/g),d=0,_=c.length;_>d;d++)c[d]=parseFloat(c[d]);switch(l){case"matrix":s.concatenate(new f(c[0],c[1],c[2],c[3],c[4],c[5]));break;case"rotate":s.rotate(c[0],c[1],c[2]);break;case"translate":s.translate(c[0],c[1]);break;case"scale":s.scale(c);break;case"skewX":s.skew(c[0],0);break;case"skewY":s.skew(0,c[0])}}t.transform(s)}function g(t,e,n){var i=t["fill-opacity"===n?"getFillColor":"getStrokeColor"]();i&&i.setAlpha(parseFloat(e))}function w(n,i,r){var s=n.attributes[i],a=s&&s.value;if(!a){var o=e.camelize(i);a=n.style[o],a||r.node[o]===r.parent[o]||(a=r.node[o])}return a?"none"===a?null:a:t}function C(n,i,r){var s={node:F.getStyles(i)||{},parent:!r&&F.getStyles(i.parentNode)||{}};return e.each(I,function(r,a){var o=w(i,a,s);o!==t&&(n=e.pick(r(n,o,a,i,s),n))}),n}function S(t){var e=t&&t.match(/\((?:#|)([^)']+)/);return e&&z[e[1]]}function P(t,n,i){function r(t){paper=a;var e=P(t,n,i),r=n.onLoad,s=a.project&&a.getView();r&&r.call(this,e),s.update()}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var s=t,a=paper;if(i)if("string"!=typeof t||/^.*s;s++){var o=r[s];if(1===o.nodeType){var h=o.nextSibling;document.body.appendChild(o);var u=P(o,n,i);return h?t.insertBefore(o,h):t.appendChild(o),u}}},g:a,svg:a,clippath:a,polygon:o,polyline:o,path:u,lineargradient:c,radialgradient:c,image:function(t){var e=new b(n(t,"href",!0));return e.on("load",function(){var e=r(t,"width","height");this.setSize(e);var n=this._matrix._transformPoint(i(t,"x","y").add(e.divide(2)));this.translate(n)}),e},symbol:function(t,e,n,i){return new p(a(t,e,n,i),!0)},defs:a,use:function(t){var e=(n(t,"href",!0)||"").substring(1),r=z[e],s=i(t,"x","y");return r?r instanceof p?r.place(s):r.clone().translate(s):null},circle:function(t){return new x.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new x.Ellipse({center:i(t,"cx","cy"),radius:r(t,"rx","ry")})},rect:function(t){var e=i(t,"x","y"),n=r(t,"width","height"),s=r(t,"rx","ry");return new x.Rectangle(new d(e,n),s)},line:function(t){return new O.Line(i(t,"x1","y1"),i(t,"x2","y2"))},text:function(t){var e=new E(i(t,"x","y").add(i(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},I=e.set(e.each(K,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof x){var i=e[t.get]();i&&i.transform((new f).translate(e.getPosition(!0).negate()))}}},{}),{id:function(t,e){z[e]=t,t.setName&&t.setName(e)},"clip-path":function(t,e){var n=S(e);if(n){if(n=n.clone(),n.setClipMask(!0),!(t instanceof y))return new y(n,t);t.insertChild(0,n)}},gradientTransform:_,transform:_,"fill-opacity":g,"stroke-opacity":g,visibility:function(t,e){t.setVisible("visible"===e)},display:function(t,e){t.setVisible(null!==e)},"stop-color":function(t,e){t.setColor&&t.setColor(e)},"stop-opacity":function(t,e){t._color&&t._color.setAlpha(parseFloat(e))},offset:function(t,e){var n=e.match(/(.*)%$/);t.setRampPoint(n?n[1]/100:parseFloat(e))},viewBox:function(t,e,n,i,a){var o=new d(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof y){var u=h?o.getSize().divide(h):1,l=(new f).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof p){h&&o.setSize(h);var c="visible"!=w(i,"overflow",a),_=t._definition;c&&!o.contains(_.getBounds())&&(c=new x.Rectangle(o).transform(_._matrix),c.setClipMask(!0),_.addChild(c))}}}),z={};m.inject({importSVG:function(t,e){return this.addChild(P(t,e,!0))}}),v.inject({importSVG:function(t,e){return this.activate(),P(t,e,!0)}})},paper=new(i.inject(e.exports,{enumerable:!0,Base:e,Numerical:a,Key:G})),"function"==typeof define&&define.amd?define("paper",paper):"object"==typeof module&&module&&(module.exports=paper),paper}; \ No newline at end of file diff --git a/dist/paper-full.js b/dist/paper-full.js deleted file mode 120000 index 37e257c7..00000000 --- a/dist/paper-full.js +++ /dev/null @@ -1 +0,0 @@ -../src/load.js \ No newline at end of file diff --git a/dist/paper-full.js b/dist/paper-full.js new file mode 100644 index 00000000..657eaf0c --- /dev/null +++ b/dist/paper-full.js @@ -0,0 +1,13704 @@ +/*! + * Paper.js v0.9.25 - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey + * http://scratchdisk.com/ & http://jonathanpuckey.com/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + * + * Date: Sun Oct 25 11:23:38 2015 +0100 + * + *** + * + * Straps.js - Class inheritance library with support for bean-style accessors + * + * Copyright (c) 2006 - 2013 Juerg Lehni + * http://scratchdisk.com/ + * + * Distributed under the MIT license. + * + *** + * + * Acorn.js + * http://marijnhaverbeke.nl/acorn/ + * + * Acorn is a tiny, fast JavaScript parser written in JavaScript, + * created by Marijn Haverbeke and released under an MIT license. + * + */ + +var paper = new function(undefined) { + +var Base = new function() { + var hidden = /^(statics|enumerable|beans|preserve)$/, + + forEach = [].forEach || function(iter, bind) { + for (var i = 0, l = this.length; i < l; i++) + iter.call(bind, this[i], i, this); + }, + + forIn = function(iter, bind) { + for (var i in this) + if (this.hasOwnProperty(i)) + iter.call(bind, this[i], i, this); + }, + + create = Object.create || function(proto) { + return { __proto__: proto }; + }, + + describe = Object.getOwnPropertyDescriptor || function(obj, name) { + var get = obj.__lookupGetter__ && obj.__lookupGetter__(name); + return get + ? { get: get, set: obj.__lookupSetter__(name), + enumerable: true, configurable: true } + : obj.hasOwnProperty(name) + ? { value: obj[name], enumerable: true, + configurable: true, writable: true } + : null; + }, + + _define = Object.defineProperty || function(obj, name, desc) { + if ((desc.get || desc.set) && obj.__defineGetter__) { + if (desc.get) + obj.__defineGetter__(name, desc.get); + if (desc.set) + obj.__defineSetter__(name, desc.set); + } else { + obj[name] = desc.value; + } + return obj; + }, + + define = function(obj, name, desc) { + delete obj[name]; + return _define(obj, name, desc); + }; + + function inject(dest, src, enumerable, beans, preserve) { + var beansNames = {}; + + function field(name, val) { + val = val || (val = describe(src, name)) + && (val.get ? val : val.value); + if (typeof val === 'string' && val[0] === '#') + val = dest[val.substring(1)] || val; + var isFunc = typeof val === 'function', + res = val, + prev = preserve || isFunc && !val.base + ? (val && val.get ? name in dest : dest[name]) + : null, + bean; + if (!preserve || !prev) { + if (isFunc && prev) + val.base = prev; + if (isFunc && beans !== false + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beansNames[bean[3].toLowerCase() + bean[4]] = bean[2]; + if (!res || isFunc || !res.get || typeof res.get !== 'function' + || !Base.isPlainObject(res)) + res = { value: res, writable: true }; + if ((describe(dest, name) + || { configurable: true }).configurable) { + res.configurable = true; + res.enumerable = enumerable; + } + define(dest, name, res); + } + } + if (src) { + for (var name in src) { + if (src.hasOwnProperty(name) && !hidden.test(name)) + field(name); + } + for (var name in beansNames) { + var part = beansNames[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (beans === true || get.length === 0)) + field(name, { get: get, set: set }); + } + } + return dest; + } + + function each(obj, iter, bind) { + if (obj) + ('length' in obj && !obj.getLength + && typeof obj.length === 'number' + ? forEach + : forIn).call(obj, iter, bind = bind || obj); + return bind; + } + + function set(obj, props, exclude) { + for (var key in props) + if (props.hasOwnProperty(key) && !(exclude && exclude[key])) + obj[key] = props[key]; + return obj; + } + + return inject(function Base() { + for (var i = 0, l = arguments.length; i < l; i++) + set(this, arguments[i]); + }, { + inject: function(src) { + if (src) { + var statics = src.statics === true ? src : src.statics, + beans = src.beans, + preserve = src.preserve; + if (statics !== src) + inject(this.prototype, src, src.enumerable, beans, preserve); + inject(this, statics, true, beans, preserve); + } + for (var i = 1, l = arguments.length; i < l; i++) + this.inject(arguments[i]); + return this; + }, + + extend: function() { + var base = this, + ctor, + proto; + for (var i = 0, l = arguments.length; i < l; i++) + if (ctor = arguments[i].initialize) + break; + ctor = ctor || function() { + base.apply(this, arguments); + }; + proto = ctor.prototype = create(this.prototype); + define(proto, 'constructor', + { value: ctor, writable: true, configurable: true }); + inject(ctor, this, true); + if (arguments.length) + this.inject.apply(ctor, arguments); + ctor.base = base; + return ctor; + } + }, true).inject({ + inject: function() { + for (var i = 0, l = arguments.length; i < l; i++) { + var src = arguments[i]; + if (src) + inject(this, src, src.enumerable, src.beans, src.preserve); + } + return this; + }, + + extend: function() { + var res = create(this); + return res.inject.apply(res, arguments); + }, + + each: function(iter, bind) { + return each(this, iter, bind); + }, + + set: function(props) { + return set(this, props); + }, + + clone: function() { + return new this.constructor(this); + }, + + statics: { + each: each, + create: create, + define: define, + describe: describe, + set: set, + + clone: function(obj) { + return set(new obj.constructor(), obj); + }, + + isPlainObject: function(obj) { + var ctor = obj != null && obj.constructor; + return ctor && (ctor === Object || ctor === Base + || ctor.name === 'Object'); + }, + + pick: function(a, b) { + return a !== undefined ? a : b; + } + } + }); +}; + +if (typeof module !== 'undefined') + module.exports = Base; + +Base.inject({ + toString: function() { + return this._id != null + ? (this._class || 'Object') + (this._name + ? " '" + this._name + "'" + : ' @' + this._id) + : '{ ' + Base.each(this, function(value, key) { + if (!/^_/.test(key)) { + var type = typeof value; + this.push(key + ': ' + (type === 'number' + ? Formatter.instance.number(value) + : type === 'string' ? "'" + value + "'" : value)); + } + }, []).join(', ') + ' }'; + }, + + getClassName: function() { + return this._class || ''; + }, + + exportJSON: function(options) { + return Base.exportJSON(this, options); + }, + + toJSON: function() { + return Base.serialize(this); + }, + + _set: function(props, exclude, dontCheck) { + if (props && (dontCheck || Base.isPlainObject(props))) { + var keys = Object.keys(props._filtering || props); + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + if (!(exclude && exclude[key])) { + var value = props[key]; + if (value !== undefined) + this[key] = value; + } + } + return true; + } + }, + + statics: { + + exports: { + enumerable: true + }, + + extend: function extend() { + var res = extend.base.apply(this, arguments), + name = res.prototype._class; + if (name && !Base.exports[name]) + Base.exports[name] = res; + return res; + }, + + equals: function(obj1, obj2) { + if (obj1 === obj2) + return true; + if (obj1 && obj1.equals) + return obj1.equals(obj2); + if (obj2 && obj2.equals) + return obj2.equals(obj1); + if (obj1 && obj2 + && typeof obj1 === 'object' && typeof obj2 === 'object') { + if (Array.isArray(obj1) && Array.isArray(obj2)) { + var length = obj1.length; + if (length !== obj2.length) + return false; + while (length--) { + if (!Base.equals(obj1[length], obj2[length])) + return false; + } + } else { + var keys = Object.keys(obj1), + length = keys.length; + if (length !== Object.keys(obj2).length) + return false; + while (length--) { + var key = keys[length]; + if (!(obj2.hasOwnProperty(key) + && Base.equals(obj1[key], obj2[key]))) + return false; + } + } + return true; + } + return false; + }, + + read: function(list, start, options, length) { + if (this === Base) { + var value = this.peek(list, start); + list.__index++; + return value; + } + var proto = this.prototype, + readIndex = proto._readIndex, + index = start || readIndex && list.__index || 0; + if (!length) + length = list.length - index; + var obj = list[index]; + if (obj instanceof this + || options && options.readNull && obj == null && length <= 1) { + if (readIndex) + list.__index = index + 1; + return obj && options && options.clone ? obj.clone() : obj; + } + obj = Base.create(this.prototype); + if (readIndex) + obj.__read = true; + obj = obj.initialize.apply(obj, index > 0 || length < list.length + ? Array.prototype.slice.call(list, index, index + length) + : list) || obj; + if (readIndex) { + list.__index = index + obj.__read; + obj.__read = undefined; + } + return obj; + }, + + peek: function(list, start) { + return list[list.__index = start || list.__index || 0]; + }, + + remain: function(list) { + return list.length - (list.__index || 0); + }, + + readAll: function(list, start, options) { + var res = [], + entry; + for (var i = start || 0, l = list.length; i < l; i++) { + res.push(Array.isArray(entry = list[i]) + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); + } + return res; + }, + + readNamed: function(list, name, start, options, length) { + var value = this.getNamed(list, name), + hasObject = value !== undefined; + if (hasObject) { + var filtered = list._filtered; + if (!filtered) { + filtered = list._filtered = Base.create(list[0]); + filtered._filtering = list[0]; + } + filtered[name] = undefined; + } + return this.read(hasObject ? [value] : list, start, options, length); + }, + + getNamed: function(list, name) { + var arg = list[0]; + if (list._hasObject === undefined) + list._hasObject = list.length === 1 && Base.isPlainObject(arg); + if (list._hasObject) + return name ? arg[name] : list._filtered || arg; + }, + + hasNamed: function(list, name) { + return !!this.getNamed(list, name); + }, + + isPlainValue: function(obj, asString) { + return this.isPlainObject(obj) || Array.isArray(obj) + || asString && typeof obj === 'string'; + }, + + serialize: function(obj, options, compact, dictionary) { + options = options || {}; + + var root = !dictionary, + res; + if (root) { + options.formatter = new Formatter(options.precision); + dictionary = { + length: 0, + definitions: {}, + references: {}, + add: function(item, create) { + var id = '#' + item._id, + ref = this.references[id]; + if (!ref) { + this.length++; + var res = create.call(item), + name = item._class; + if (name && res[0] !== name) + res.unshift(name); + this.definitions[id] = res; + ref = this.references[id] = [id]; + } + return ref; + } + }; + } + if (obj && obj._serialize) { + res = obj._serialize(options, dictionary); + var name = obj._class; + if (name && !compact && !res._compact && res[0] !== name) + res.unshift(name); + } else if (Array.isArray(obj)) { + res = []; + for (var i = 0, l = obj.length; i < l; i++) + res[i] = Base.serialize(obj[i], options, compact, + dictionary); + if (compact) + res._compact = true; + } else if (Base.isPlainObject(obj)) { + res = {}; + var keys = Object.keys(obj); + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + res[key] = Base.serialize(obj[key], options, compact, + dictionary); + } + } else if (typeof obj === 'number') { + res = options.formatter.number(obj, options.precision); + } else { + res = obj; + } + return root && dictionary.length > 0 + ? [['dictionary', dictionary.definitions], res] + : res; + }, + + deserialize: function(json, create, _data, _isDictionary) { + var res = json, + isRoot = !_data; + _data = _data || {}; + if (Array.isArray(json)) { + var type = json[0], + isDictionary = type === 'dictionary'; + if (json.length == 1 && /^#/.test(type)) + return _data.dictionary[type]; + type = Base.exports[type]; + res = []; + if (_isDictionary) + _data.dictionary = res; + for (var i = type ? 1 : 0, l = json.length; i < l; i++) + res.push(Base.deserialize(json[i], create, _data, + isDictionary)); + if (type) { + var args = res; + if (create) { + res = create(type, args); + } else { + res = Base.create(type.prototype); + type.apply(res, args); + } + } + } else if (Base.isPlainObject(json)) { + res = {}; + if (_isDictionary) + _data.dictionary = res; + for (var key in json) + res[key] = Base.deserialize(json[key], create, _data); + } + return isRoot && json && json.length && json[0][0] === 'dictionary' + ? res[1] + : res; + }, + + exportJSON: function(obj, options) { + var json = Base.serialize(obj, options); + return options && options.asString === false + ? json + : JSON.stringify(json); + }, + + importJSON: function(json, target) { + return Base.deserialize( + typeof json === 'string' ? JSON.parse(json) : json, + function(type, args) { + var obj = target && target.constructor === type + ? target + : Base.create(type.prototype), + isTarget = obj === target; + if (args.length === 1 && obj instanceof Item + && (isTarget || !(obj instanceof Layer))) { + var arg = args[0]; + if (Base.isPlainObject(arg)) + arg.insert = false; + } + type.apply(obj, args); + if (isTarget) + target = null; + return obj; + }); + }, + + splice: function(list, items, index, remove) { + var amount = items && items.length, + append = index === undefined; + index = append ? list.length : index; + if (index > list.length) + index = list.length; + for (var i = 0; i < amount; i++) + items[i]._index = index + i; + if (append) { + list.push.apply(list, items); + return []; + } else { + var args = [index, remove]; + if (items) + args.push.apply(args, items); + var removed = list.splice.apply(list, args); + for (var i = 0, l = removed.length; i < l; i++) + removed[i]._index = undefined; + for (var i = index + amount, l = list.length; i < l; i++) + list[i]._index = i; + return removed; + } + }, + + capitalize: function(str) { + return str.replace(/\b[a-z]/g, function(match) { + return match.toUpperCase(); + }); + }, + + camelize: function(str) { + return str.replace(/-(.)/g, function(all, chr) { + return chr.toUpperCase(); + }); + }, + + hyphenate: function(str) { + return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); + } + } +}); + +var Emitter = { + on: function(type, func) { + if (typeof type !== 'string') { + Base.each(type, function(value, key) { + this.on(key, value); + }, this); + } else { + var types = this._eventTypes, + entry = types && types[type], + handlers = this._callbacks = this._callbacks || {}; + handlers = handlers[type] = handlers[type] || []; + if (handlers.indexOf(func) === -1) { + handlers.push(func); + if (entry && entry.install && handlers.length === 1) + entry.install.call(this, type); + } + } + return this; + }, + + off: function(type, func) { + if (typeof type !== 'string') { + Base.each(type, function(value, key) { + this.off(key, value); + }, this); + return; + } + var types = this._eventTypes, + entry = types && types[type], + handlers = this._callbacks && this._callbacks[type], + index; + if (handlers) { + if (!func || (index = handlers.indexOf(func)) !== -1 + && handlers.length === 1) { + if (entry && entry.uninstall) + entry.uninstall.call(this, type); + delete this._callbacks[type]; + } else if (index !== -1) { + handlers.splice(index, 1); + } + } + return this; + }, + + once: function(type, func) { + return this.on(type, function() { + func.apply(this, arguments); + this.off(type, func); + }); + }, + + emit: function(type, event) { + var handlers = this._callbacks && this._callbacks[type]; + if (!handlers) + return false; + var args = [].slice.call(arguments, 1); + handlers = handlers.slice(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(this, args) === false) { + if (event && event.stop) + event.stop(); + break; + } + } + return true; + }, + + responds: function(type) { + return !!(this._callbacks && this._callbacks[type]); + }, + + attach: '#on', + detach: '#off', + fire: '#emit', + + _installEvents: function(install) { + var handlers = this._callbacks, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var types = this._eventTypes, + entry = types && types[type], + func = entry && entry[key]; + if (func) + func.call(this, type); + } + } + }, + + statics: { + inject: function inject(src) { + var events = src._events; + if (events) { + var types = {}; + Base.each(events, function(entry, key) { + var isString = typeof entry === 'string', + name = isString ? entry : key, + part = Base.capitalize(name), + type = name.substring(2).toLowerCase(); + types[type] = isString ? {} : entry; + name = '_' + name; + src['get' + part] = function() { + return this[name]; + }; + src['set' + part] = function(func) { + var prev = this[name]; + if (prev) + this.off(type, prev); + if (func) + this.on(type, func); + this[name] = func; + }; + }); + src._eventTypes = types; + } + return inject.base.apply(this, arguments); + } + } +}; + +var PaperScope = Base.extend({ + _class: 'PaperScope', + + initialize: function PaperScope() { + paper = this; + this.settings = new Base({ + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }); + this.project = null; + this.projects = []; + this.tools = []; + this.palettes = []; + this._id = PaperScope._id++; + PaperScope._scopes[this._id] = this; + var proto = PaperScope.prototype; + if (!this.support) { + var ctx = CanvasProvider.getContext(1, 1); + proto.support = { + nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx, + nativeBlendModes: BlendMode.nativeModes + }; + CanvasProvider.release(ctx); + } + + if (!this.browser) { + var agent = navigator.userAgent.toLowerCase(), + platform = (/(win)/.exec(agent) + || /(mac)/.exec(agent) + || /(linux)/.exec(agent) + || [])[0], + browser = proto.browser = { platform: platform }; + if (platform) + browser[platform] = true; + agent.replace( + /(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g, + function(all, n, v1, v2, rv) { + if (!browser.chrome) { + var v = n === 'opera' ? v2 : v1; + if (n === 'trident') { + v = rv; + n = 'msie'; + } + browser.version = v; + browser.versionNumber = parseFloat(v); + browser.name = n; + browser[n] = true; + } + } + ); + if (browser.chrome) + delete browser.webkit; + if (browser.atom) + delete browser.chrome; + } + }, + + version: "0.9.25", + + getView: function() { + return this.project && this.project.getView(); + }, + + getPaper: function() { + return this; + }, + + execute: function(code, url, options) { + paper.PaperScript.execute(code, this, url, options); + View.updateFocus(); + }, + + install: function(scope) { + var that = this; + Base.each(['project', 'view', 'tool'], function(key) { + Base.define(scope, key, { + configurable: true, + get: function() { + return that[key]; + } + }); + }); + for (var key in this) + if (!/^_/.test(key) && this[key]) + scope[key] = this[key]; + }, + + setup: function(element) { + paper = this; + this.project = new Project(element); + return this; + }, + + activate: function() { + paper = this; + }, + + clear: function() { + for (var i = this.projects.length - 1; i >= 0; i--) + this.projects[i].remove(); + for (var i = this.tools.length - 1; i >= 0; i--) + this.tools[i].remove(); + for (var i = this.palettes.length - 1; i >= 0; i--) + this.palettes[i].remove(); + }, + + remove: function() { + this.clear(); + delete PaperScope._scopes[this._id]; + }, + + statics: new function() { + function handleAttribute(name) { + name += 'Attribute'; + return function(el, attr) { + return el[name](attr) || el[name]('data-paper-' + attr); + }; + } + + return { + _scopes: {}, + _id: 0, + + get: function(id) { + return this._scopes[id] || null; + }, + + getAttribute: handleAttribute('get'), + hasAttribute: handleAttribute('has') + }; + } +}); + +var PaperScopeItem = Base.extend(Emitter, { + + initialize: function(activate) { + this._scope = paper; + this._index = this._scope[this._list].push(this) - 1; + if (activate || !this._scope[this._reference]) + this.activate(); + }, + + activate: function() { + if (!this._scope) + return false; + var prev = this._scope[this._reference]; + if (prev && prev !== this) + prev.emit('deactivate'); + this._scope[this._reference] = this; + this.emit('activate', prev); + return true; + }, + + isActive: function() { + return this._scope[this._reference] === this; + }, + + remove: function() { + if (this._index == null) + return false; + Base.splice(this._scope[this._list], null, this._index, 1); + if (this._scope[this._reference] == this) + this._scope[this._reference] = null; + this._scope = null; + return true; + } +}); + +var Formatter = Base.extend({ + initialize: function(precision) { + this.precision = precision || 5; + this.multiplier = Math.pow(10, this.precision); + }, + + number: function(val) { + return Math.round(val * this.multiplier) / this.multiplier; + }, + + pair: function(val1, val2, separator) { + return this.number(val1) + (separator || ',') + this.number(val2); + }, + + point: function(val, separator) { + return this.number(val.x) + (separator || ',') + this.number(val.y); + }, + + size: function(val, separator) { + return this.number(val.width) + (separator || ',') + + this.number(val.height); + }, + + rectangle: function(val, separator) { + return this.point(val, separator) + (separator || ',') + + this.size(val, separator); + } +}); + +Formatter.instance = new Formatter(); + +var Numerical = new function() { + + var abscissas = [ + [ 0.5773502691896257645091488], + [0,0.7745966692414833770358531], + [ 0.3399810435848562648026658,0.8611363115940525752239465], + [0,0.5384693101056830910363144,0.9061798459386639927976269], + [ 0.2386191860831969086305017,0.6612093864662645136613996,0.9324695142031520278123016], + [0,0.4058451513773971669066064,0.7415311855993944398638648,0.9491079123427585245261897], + [ 0.1834346424956498049394761,0.5255324099163289858177390,0.7966664774136267395915539,0.9602898564975362316835609], + [0,0.3242534234038089290385380,0.6133714327005903973087020,0.8360311073266357942994298,0.9681602395076260898355762], + [ 0.1488743389816312108848260,0.4333953941292471907992659,0.6794095682990244062343274,0.8650633666889845107320967,0.9739065285171717200779640], + [0,0.2695431559523449723315320,0.5190961292068118159257257,0.7301520055740493240934163,0.8870625997680952990751578,0.9782286581460569928039380], + [ 0.1252334085114689154724414,0.3678314989981801937526915,0.5873179542866174472967024,0.7699026741943046870368938,0.9041172563704748566784659,0.9815606342467192506905491], + [0,0.2304583159551347940655281,0.4484927510364468528779129,0.6423493394403402206439846,0.8015780907333099127942065,0.9175983992229779652065478,0.9841830547185881494728294], + [ 0.1080549487073436620662447,0.3191123689278897604356718,0.5152486363581540919652907,0.6872929048116854701480198,0.8272013150697649931897947,0.9284348836635735173363911,0.9862838086968123388415973], + [0,0.2011940939974345223006283,0.3941513470775633698972074,0.5709721726085388475372267,0.7244177313601700474161861,0.8482065834104272162006483,0.9372733924007059043077589,0.9879925180204854284895657], + [ 0.0950125098376374401853193,0.2816035507792589132304605,0.4580167776572273863424194,0.6178762444026437484466718,0.7554044083550030338951012,0.8656312023878317438804679,0.9445750230732325760779884,0.9894009349916499325961542] + ]; + + var weights = [ + [1], + [0.8888888888888888888888889,0.5555555555555555555555556], + [0.6521451548625461426269361,0.3478548451374538573730639], + [0.5688888888888888888888889,0.4786286704993664680412915,0.2369268850561890875142640], + [0.4679139345726910473898703,0.3607615730481386075698335,0.1713244923791703450402961], + [0.4179591836734693877551020,0.3818300505051189449503698,0.2797053914892766679014678,0.1294849661688696932706114], + [0.3626837833783619829651504,0.3137066458778872873379622,0.2223810344533744705443560,0.1012285362903762591525314], + [0.3302393550012597631645251,0.3123470770400028400686304,0.2606106964029354623187429,0.1806481606948574040584720,0.0812743883615744119718922], + [0.2955242247147528701738930,0.2692667193099963550912269,0.2190863625159820439955349,0.1494513491505805931457763,0.0666713443086881375935688], + [0.2729250867779006307144835,0.2628045445102466621806889,0.2331937645919904799185237,0.1862902109277342514260976,0.1255803694649046246346943,0.0556685671161736664827537], + [0.2491470458134027850005624,0.2334925365383548087608499,0.2031674267230659217490645,0.1600783285433462263346525,0.1069393259953184309602547,0.0471753363865118271946160], + [0.2325515532308739101945895,0.2262831802628972384120902,0.2078160475368885023125232,0.1781459807619457382800467,0.1388735102197872384636018,0.0921214998377284479144218,0.0404840047653158795200216], + [0.2152638534631577901958764,0.2051984637212956039659241,0.1855383974779378137417166,0.1572031671581935345696019,0.1215185706879031846894148,0.0801580871597602098056333,0.0351194603317518630318329], + [0.2025782419255612728806202,0.1984314853271115764561183,0.1861610000155622110268006,0.1662692058169939335532009,0.1395706779261543144478048,0.1071592204671719350118695,0.0703660474881081247092674,0.0307532419961172683546284], + [0.1894506104550684962853967,0.1826034150449235888667637,0.1691565193950025381893121,0.1495959888165767320815017,0.1246289712555338720524763,0.0951585116824927848099251,0.0622535239386478928628438,0.0271524594117540948517806] + ]; + + var abs = Math.abs, + sqrt = Math.sqrt, + pow = Math.pow, + EPSILON = 1e-12, + MACHINE_EPSILON = 1.12e-16; + + function clip(value, min, max) { + return value < min ? min : value > max ? max : value; + } + + return { + TOLERANCE: 1e-6, + EPSILON: EPSILON, + MACHINE_EPSILON: MACHINE_EPSILON, + CURVETIME_EPSILON: 4e-7, + GEOMETRIC_EPSILON: 2e-7, + WINDING_EPSILON: 2e-7, + TRIGONOMETRIC_EPSILON: 1e-7, + CLIPPING_EPSILON: 1e-7, + KAPPA: 4 * (sqrt(2) - 1) / 3, + + isZero: function(val) { + return val >= -EPSILON && val <= EPSILON; + }, + + integrate: function(f, a, b, n) { + var x = abscissas[n - 2], + w = weights[n - 2], + A = (b - a) * 0.5, + B = A + a, + i = 0, + m = (n + 1) >> 1, + sum = n & 1 ? w[i++] * f(B) : 0; + while (i < m) { + var Ax = A * x[i]; + sum += w[i++] * (f(B + Ax) + f(B - Ax)); + } + return A * sum; + }, + + findRoot: function(f, df, x, a, b, n, tolerance) { + for (var i = 0; i < n; i++) { + var fx = f(x), + dx = fx / df(x), + nx = x - dx; + if (abs(dx) < tolerance) + return nx; + if (fx > 0) { + b = x; + x = nx <= a ? (a + b) * 0.5 : nx; + } else { + a = x; + x = nx >= b ? (a + b) * 0.5 : nx; + } + } + return x; + }, + + solveQuadratic: function(a, b, c, roots, min, max) { + var count = 0, + eMin = min - EPSILON, + eMax = max + EPSILON, + x1, x2 = Infinity, + B = b, + D; + b /= -2; + D = b * b - a * c; + if (D !== 0 && abs(D) < MACHINE_EPSILON) { + var gmC = pow(abs(a * b * c), 1 / 3); + if (gmC < 1e-8) { + var mult = pow(10, + abs(Math.floor(Math.log(gmC) * Math.LOG10E))); + if (!isFinite(mult)) + mult = 0; + a *= mult; + b *= mult; + c *= mult; + D = b * b - a * c; + } + } + if (abs(a) < EPSILON) { + if (abs(B) < EPSILON) + return abs(c) < EPSILON ? -1 : 0; + x1 = -c / B; + } else if (D >= -MACHINE_EPSILON) { + var Q = D < 0 ? 0 : sqrt(D), + R = b + (b < 0 ? -Q : Q); + if (R === 0) { + x1 = c / a; + x2 = -x1; + } else { + x1 = R / a; + x2 = c / R; + } + } + if (isFinite(x1) && (min == null || x1 > eMin && x1 < eMax)) + roots[count++] = min == null ? x1 : clip(x1, min, max); + if (x2 !== x1 + && isFinite(x2) && (min == null || x2 > eMin && x2 < eMax)) + roots[count++] = min == null ? x2 : clip(x2, min, max); + return count; + }, + + solveCubic: function(a, b, c, d, roots, min, max) { + var count = 0, + x, b1, c2; + if (abs(a) < EPSILON) { + a = b; + b1 = c; + c2 = d; + x = Infinity; + } else if (abs(d) < EPSILON) { + b1 = b; + c2 = c; + x = 0; + } else { + var ec = 1 + MACHINE_EPSILON, + x0, q, qd, t, r, s, tmp; + x = -(b / a) / 3; + tmp = a * x, + b1 = tmp + b, + c2 = b1 * x + c, + qd = (tmp + b1) * x + c2, + q = c2 * x + d; + t = q /a; + r = pow(abs(t), 1/3); + s = t < 0 ? -1 : 1; + t = -qd / a; + r = t > 0 ? 1.3247179572 * Math.max(r, sqrt(t)) : r; + x0 = x - s * r; + if (x0 !== x) { + do { + x = x0; + tmp = a * x, + b1 = tmp + b, + c2 = b1 * x + c, + qd = (tmp + b1) * x + c2, + q = c2 * x + d; + x0 = qd === 0 ? x : x - q / qd / ec; + if (x0 === x) { + x = x0; + break; + } + } while (s * x0 > s * x); + if (abs(a) * x * x > abs(d / x)) { + c2 = -d / x; + b1 = (c2 - c) / x; + } + } + } + var count = Numerical.solveQuadratic(a, b1, c2, roots, min, max); + if (isFinite(x) && (count === 0 || x !== roots[count - 1]) + && (min == null || x > min - EPSILON && x < max + EPSILON)) + roots[count++] = min == null ? x : clip(x, min, max); + return count; + } + }; +}; + +var UID = { + _id: 1, + _pools: {}, + + get: function(ctor) { + if (ctor) { + var name = ctor._class, + pool = this._pools[name]; + if (!pool) + pool = this._pools[name] = { _id: 1 }; + return pool._id++; + } else { + return this._id++; + } + } +}; + +var Point = Base.extend({ + _class: 'Point', + _readIndex: true, + + initialize: function Point(arg0, arg1) { + var type = typeof arg0; + if (type === 'number') { + var hasY = typeof arg1 === 'number'; + this.x = arg0; + this.y = hasY ? arg1 : arg0; + if (this.__read) + this.__read = hasY ? 2 : 1; + } else if (type === 'undefined' || arg0 === null) { + this.x = this.y = 0; + if (this.__read) + this.__read = arg0 === null ? 1 : 0; + } else { + if (Array.isArray(arg0)) { + this.x = arg0[0]; + this.y = arg0.length > 1 ? arg0[1] : arg0[0]; + } else if (arg0.x != null) { + this.x = arg0.x; + this.y = arg0.y; + } else if (arg0.width != null) { + this.x = arg0.width; + this.y = arg0.height; + } else if (arg0.angle != null) { + this.x = arg0.length; + this.y = 0; + this.setAngle(arg0.angle); + } else { + this.x = this.y = 0; + if (this.__read) + this.__read = 0; + } + if (this.__read) + this.__read = 1; + } + }, + + set: function(x, y) { + this.x = x; + this.y = y; + return this; + }, + + equals: function(point) { + return this === point || point + && (this.x === point.x && this.y === point.y + || Array.isArray(point) + && this.x === point[0] && this.y === point[1]) + || false; + }, + + clone: function() { + return new Point(this.x, this.y); + }, + + toString: function() { + var f = Formatter.instance; + return '{ x: ' + f.number(this.x) + ', y: ' + f.number(this.y) + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.x), f.number(this.y)]; + }, + + getLength: function() { + return Math.sqrt(this.x * this.x + this.y * this.y); + }, + + setLength: function(length) { + if (this.isZero()) { + var angle = this._angle || 0; + this.set( + Math.cos(angle) * length, + Math.sin(angle) * length + ); + } else { + var scale = length / this.getLength(); + if (Numerical.isZero(scale)) + this.getAngle(); + this.set( + this.x * scale, + this.y * scale + ); + } + }, + getAngle: function() { + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; + }, + + setAngle: function(angle) { + this.setAngleInRadians.call(this, angle * Math.PI / 180); + }, + + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + + getAngleInRadians: function() { + if (!arguments.length) { + return this.isZero() + ? this._angle || 0 + : this._angle = Math.atan2(this.y, this.x); + } else { + var point = Point.read(arguments), + div = this.getLength() * point.getLength(); + if (Numerical.isZero(div)) { + return NaN; + } else { + var a = this.dot(point) / div; + return Math.acos(a < -1 ? -1 : a > 1 ? 1 : a); + } + } + }, + + setAngleInRadians: function(angle) { + this._angle = angle; + if (!this.isZero()) { + var length = this.getLength(); + this.set( + Math.cos(angle) * length, + Math.sin(angle) * length + ); + } + }, + + getQuadrant: function() { + return this.x >= 0 ? this.y >= 0 ? 1 : 4 : this.y >= 0 ? 2 : 3; + } +}, { + beans: false, + + getDirectedAngle: function() { + var point = Point.read(arguments); + return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; + }, + + getDistance: function() { + var point = Point.read(arguments), + x = point.x - this.x, + y = point.y - this.y, + d = x * x + y * y, + squared = Base.read(arguments); + return squared ? d : Math.sqrt(d); + }, + + normalize: function(length) { + if (length === undefined) + length = 1; + var current = this.getLength(), + scale = current !== 0 ? length / current : 0, + point = new Point(this.x * scale, this.y * scale); + if (scale >= 0) + point._angle = this._angle; + return point; + }, + + rotate: function(angle, center) { + if (angle === 0) + return this.clone(); + angle = angle * Math.PI / 180; + var point = center ? this.subtract(center) : this, + sin = Math.sin(angle), + cos = Math.cos(angle); + point = new Point( + point.x * cos - point.y * sin, + point.x * sin + point.y * cos + ); + return center ? point.add(center) : point; + }, + + transform: function(matrix) { + return matrix ? matrix._transformPoint(this) : this; + }, + + add: function() { + var point = Point.read(arguments); + return new Point(this.x + point.x, this.y + point.y); + }, + + subtract: function() { + var point = Point.read(arguments); + return new Point(this.x - point.x, this.y - point.y); + }, + + multiply: function() { + var point = Point.read(arguments); + return new Point(this.x * point.x, this.y * point.y); + }, + + divide: function() { + var point = Point.read(arguments); + return new Point(this.x / point.x, this.y / point.y); + }, + + modulo: function() { + var point = Point.read(arguments); + return new Point(this.x % point.x, this.y % point.y); + }, + + negate: function() { + return new Point(-this.x, -this.y); + }, + + isInside: function() { + return Rectangle.read(arguments).contains(this); + }, + + isClose: function() { + var point = Point.read(arguments), + tolerance = Base.read(arguments); + return this.getDistance(point) < tolerance; + }, + + isCollinear: function() { + var point = Point.read(arguments); + return Point.isCollinear(this.x, this.y, point.x, point.y); + }, + + isColinear: '#isCollinear', + + isOrthogonal: function() { + var point = Point.read(arguments); + return Point.isOrthogonal(this.x, this.y, point.x, point.y); + }, + + isZero: function() { + return Numerical.isZero(this.x) && Numerical.isZero(this.y); + }, + + isNaN: function() { + return isNaN(this.x) || isNaN(this.y); + }, + + dot: function() { + var point = Point.read(arguments); + return this.x * point.x + this.y * point.y; + }, + + cross: function() { + var point = Point.read(arguments); + return this.x * point.y - this.y * point.x; + }, + + project: function() { + var point = Point.read(arguments), + scale = point.isZero() ? 0 : this.dot(point) / point.dot(point); + return new Point( + point.x * scale, + point.y * scale + ); + }, + + statics: { + min: function() { + var point1 = Point.read(arguments), + point2 = Point.read(arguments); + return new Point( + Math.min(point1.x, point2.x), + Math.min(point1.y, point2.y) + ); + }, + + max: function() { + var point1 = Point.read(arguments), + point2 = Point.read(arguments); + return new Point( + Math.max(point1.x, point2.x), + Math.max(point1.y, point2.y) + ); + }, + + random: function() { + return new Point(Math.random(), Math.random()); + }, + + isCollinear: function(x1, y1, x2, y2) { + return Math.abs(x1 * y2 - y1 * x2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; + }, + + isOrthogonal: function(x1, y1, x2, y2) { + return Math.abs(x1 * x2 + y1 * y2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; + } + } +}, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) { + var op = Math[name]; + this[name] = function() { + return new Point(op(this.x), op(this.y)); + }; +}, {})); + +var LinkedPoint = Point.extend({ + initialize: function Point(x, y, owner, setter) { + this._x = x; + this._y = y; + this._owner = owner; + this._setter = setter; + }, + + set: function(x, y, _dontNotify) { + this._x = x; + this._y = y; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + }, + + getX: function() { + return this._x; + }, + + setX: function(x) { + this._x = x; + this._owner[this._setter](this); + }, + + getY: function() { + return this._y; + }, + + setY: function(y) { + this._y = y; + this._owner[this._setter](this); + } +}); + +var Size = Base.extend({ + _class: 'Size', + _readIndex: true, + + initialize: function Size(arg0, arg1) { + var type = typeof arg0; + if (type === 'number') { + var hasHeight = typeof arg1 === 'number'; + this.width = arg0; + this.height = hasHeight ? arg1 : arg0; + if (this.__read) + this.__read = hasHeight ? 2 : 1; + } else if (type === 'undefined' || arg0 === null) { + this.width = this.height = 0; + if (this.__read) + this.__read = arg0 === null ? 1 : 0; + } else { + if (Array.isArray(arg0)) { + this.width = arg0[0]; + this.height = arg0.length > 1 ? arg0[1] : arg0[0]; + } else if (arg0.width != null) { + this.width = arg0.width; + this.height = arg0.height; + } else if (arg0.x != null) { + this.width = arg0.x; + this.height = arg0.y; + } else { + this.width = this.height = 0; + if (this.__read) + this.__read = 0; + } + if (this.__read) + this.__read = 1; + } + }, + + set: function(width, height) { + this.width = width; + this.height = height; + return this; + }, + + equals: function(size) { + return size === this || size && (this.width === size.width + && this.height === size.height + || Array.isArray(size) && this.width === size[0] + && this.height === size[1]) || false; + }, + + clone: function() { + return new Size(this.width, this.height); + }, + + toString: function() { + var f = Formatter.instance; + return '{ width: ' + f.number(this.width) + + ', height: ' + f.number(this.height) + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.width), + f.number(this.height)]; + }, + + add: function() { + var size = Size.read(arguments); + return new Size(this.width + size.width, this.height + size.height); + }, + + subtract: function() { + var size = Size.read(arguments); + return new Size(this.width - size.width, this.height - size.height); + }, + + multiply: function() { + var size = Size.read(arguments); + return new Size(this.width * size.width, this.height * size.height); + }, + + divide: function() { + var size = Size.read(arguments); + return new Size(this.width / size.width, this.height / size.height); + }, + + modulo: function() { + var size = Size.read(arguments); + return new Size(this.width % size.width, this.height % size.height); + }, + + negate: function() { + return new Size(-this.width, -this.height); + }, + + isZero: function() { + return Numerical.isZero(this.width) && Numerical.isZero(this.height); + }, + + isNaN: function() { + return isNaN(this.width) || isNaN(this.height); + }, + + statics: { + min: function(size1, size2) { + return new Size( + Math.min(size1.width, size2.width), + Math.min(size1.height, size2.height)); + }, + + max: function(size1, size2) { + return new Size( + Math.max(size1.width, size2.width), + Math.max(size1.height, size2.height)); + }, + + random: function() { + return new Size(Math.random(), Math.random()); + } + } +}, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) { + var op = Math[name]; + this[name] = function() { + return new Size(op(this.width), op(this.height)); + }; +}, {})); + +var LinkedSize = Size.extend({ + initialize: function Size(width, height, owner, setter) { + this._width = width; + this._height = height; + this._owner = owner; + this._setter = setter; + }, + + set: function(width, height, _dontNotify) { + this._width = width; + this._height = height; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + }, + + getWidth: function() { + return this._width; + }, + + setWidth: function(width) { + this._width = width; + this._owner[this._setter](this); + }, + + getHeight: function() { + return this._height; + }, + + setHeight: function(height) { + this._height = height; + this._owner[this._setter](this); + } +}); + +var Rectangle = Base.extend({ + _class: 'Rectangle', + _readIndex: true, + beans: true, + + initialize: function Rectangle(arg0, arg1, arg2, arg3) { + var type = typeof arg0, + read = 0; + if (type === 'number') { + this.x = arg0; + this.y = arg1; + this.width = arg2; + this.height = arg3; + read = 4; + } else if (type === 'undefined' || arg0 === null) { + this.x = this.y = this.width = this.height = 0; + read = arg0 === null ? 1 : 0; + } else if (arguments.length === 1) { + if (Array.isArray(arg0)) { + this.x = arg0[0]; + this.y = arg0[1]; + this.width = arg0[2]; + this.height = arg0[3]; + read = 1; + } else if (arg0.x !== undefined || arg0.width !== undefined) { + this.x = arg0.x || 0; + this.y = arg0.y || 0; + this.width = arg0.width || 0; + this.height = arg0.height || 0; + read = 1; + } else if (arg0.from === undefined && arg0.to === undefined) { + this.x = this.y = this.width = this.height = 0; + this._set(arg0); + read = 1; + } + } + if (!read) { + var point = Point.readNamed(arguments, 'from'), + next = Base.peek(arguments); + this.x = point.x; + this.y = point.y; + if (next && next.x !== undefined || Base.hasNamed(arguments, 'to')) { + var to = Point.readNamed(arguments, 'to'); + this.width = to.x - point.x; + this.height = to.y - point.y; + if (this.width < 0) { + this.x = to.x; + this.width = -this.width; + } + if (this.height < 0) { + this.y = to.y; + this.height = -this.height; + } + } else { + var size = Size.read(arguments); + this.width = size.width; + this.height = size.height; + } + read = arguments.__index; + } + if (this.__read) + this.__read = read; + }, + + set: function(x, y, width, height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + return this; + }, + + clone: function() { + return new Rectangle(this.x, this.y, this.width, this.height); + }, + + equals: function(rect) { + var rt = Base.isPlainValue(rect) + ? Rectangle.read(arguments) + : rect; + return rt === this + || rt && this.x === rt.x && this.y === rt.y + && this.width === rt.width && this.height === rt.height + || false; + }, + + toString: function() { + var f = Formatter.instance; + return '{ x: ' + f.number(this.x) + + ', y: ' + f.number(this.y) + + ', width: ' + f.number(this.width) + + ', height: ' + f.number(this.height) + + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.x), + f.number(this.y), + f.number(this.width), + f.number(this.height)]; + }, + + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); + }, + + setPoint: function() { + var point = Point.read(arguments); + this.x = point.x; + this.y = point.y; + }, + + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (this._fixX) + this.x += (this.width - size.width) * this._fixX; + if (this._fixY) + this.y += (this.height - size.height) * this._fixY; + this.width = size.width; + this.height = size.height; + this._fixW = 1; + this._fixH = 1; + }, + + getLeft: function() { + return this.x; + }, + + setLeft: function(left) { + if (!this._fixW) + this.width -= left - this.x; + this.x = left; + this._fixX = 0; + }, + + getTop: function() { + return this.y; + }, + + setTop: function(top) { + if (!this._fixH) + this.height -= top - this.y; + this.y = top; + this._fixY = 0; + }, + + getRight: function() { + return this.x + this.width; + }, + + setRight: function(right) { + if (this._fixX !== undefined && this._fixX !== 1) + this._fixW = 0; + if (this._fixW) + this.x = right - this.width; + else + this.width = right - this.x; + this._fixX = 1; + }, + + getBottom: function() { + return this.y + this.height; + }, + + setBottom: function(bottom) { + if (this._fixY !== undefined && this._fixY !== 1) + this._fixH = 0; + if (this._fixH) + this.y = bottom - this.height; + else + this.height = bottom - this.y; + this._fixY = 1; + }, + + getCenterX: function() { + return this.x + this.width * 0.5; + }, + + setCenterX: function(x) { + this.x = x - this.width * 0.5; + this._fixX = 0.5; + }, + + getCenterY: function() { + return this.y + this.height * 0.5; + }, + + setCenterY: function(y) { + this.y = y - this.height * 0.5; + this._fixY = 0.5; + }, + + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); + }, + + setCenter: function() { + var point = Point.read(arguments); + this.setCenterX(point.x); + this.setCenterY(point.y); + return this; + }, + + getArea: function() { + return this.width * this.height; + }, + + isEmpty: function() { + return this.width === 0 || this.height === 0; + }, + + contains: function(arg) { + return arg && arg.width !== undefined + || (Array.isArray(arg) ? arg : arguments).length == 4 + ? this._containsRectangle(Rectangle.read(arguments)) + : this._containsPoint(Point.read(arguments)); + }, + + _containsPoint: function(point) { + var x = point.x, + y = point.y; + return x >= this.x && y >= this.y + && x <= this.x + this.width + && y <= this.y + this.height; + }, + + _containsRectangle: function(rect) { + var x = rect.x, + y = rect.y; + return x >= this.x && y >= this.y + && x + rect.width <= this.x + this.width + && y + rect.height <= this.y + this.height; + }, + + intersects: function() { + var rect = Rectangle.read(arguments); + return rect.x + rect.width > this.x + && rect.y + rect.height > this.y + && rect.x < this.x + this.width + && rect.y < this.y + this.height; + }, + + touches: function() { + var rect = Rectangle.read(arguments); + return rect.x + rect.width >= this.x + && rect.y + rect.height >= this.y + && rect.x <= this.x + this.width + && rect.y <= this.y + this.height; + }, + + intersect: function() { + var rect = Rectangle.read(arguments), + x1 = Math.max(this.x, rect.x), + y1 = Math.max(this.y, rect.y), + x2 = Math.min(this.x + this.width, rect.x + rect.width), + y2 = Math.min(this.y + this.height, rect.y + rect.height); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + unite: function() { + var rect = Rectangle.read(arguments), + x1 = Math.min(this.x, rect.x), + y1 = Math.min(this.y, rect.y), + x2 = Math.max(this.x + this.width, rect.x + rect.width), + y2 = Math.max(this.y + this.height, rect.y + rect.height); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + include: function() { + var point = Point.read(arguments); + var x1 = Math.min(this.x, point.x), + y1 = Math.min(this.y, point.y), + x2 = Math.max(this.x + this.width, point.x), + y2 = Math.max(this.y + this.height, point.y); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + expand: function() { + var amount = Size.read(arguments), + hor = amount.width, + ver = amount.height; + return new Rectangle(this.x - hor / 2, this.y - ver / 2, + this.width + hor, this.height + ver); + }, + + scale: function(hor, ver) { + return this.expand(this.width * hor - this.width, + this.height * (ver === undefined ? hor : ver) - this.height); + } +}, Base.each([ + ['Top', 'Left'], ['Top', 'Right'], + ['Bottom', 'Left'], ['Bottom', 'Right'], + ['Left', 'Center'], ['Top', 'Center'], + ['Right', 'Center'], ['Bottom', 'Center'] + ], + function(parts, index) { + var part = parts.join(''); + var xFirst = /^[RL]/.test(part); + if (index >= 4) + parts[1] += xFirst ? 'Y' : 'X'; + var x = parts[xFirst ? 0 : 1], + y = parts[xFirst ? 1 : 0], + getX = 'get' + x, + getY = 'get' + y, + setX = 'set' + x, + setY = 'set' + y, + get = 'get' + part, + set = 'set' + part; + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); + }; + this[set] = function() { + var point = Point.read(arguments); + this[setX](point.x); + this[setY](point.y); + }; + }, { + beans: true + } +)); + +var LinkedRectangle = Rectangle.extend({ + initialize: function Rectangle(x, y, width, height, owner, setter) { + this.set(x, y, width, height, true); + this._owner = owner; + this._setter = setter; + }, + + set: function(x, y, width, height, _dontNotify) { + this._x = x; + this._y = y; + this._width = width; + this._height = height; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + } +}, +new function() { + var proto = Rectangle.prototype; + + return Base.each(['x', 'y', 'width', 'height'], function(key) { + var part = Base.capitalize(key); + var internal = '_' + key; + this['get' + part] = function() { + return this[internal]; + }; + + this['set' + part] = function(value) { + this[internal] = value; + if (!this._dontNotify) + this._owner[this._setter](this); + }; + }, Base.each(['Point', 'Size', 'Center', + 'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY', + 'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', + 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter'], + function(key) { + var name = 'set' + key; + this[name] = function() { + this._dontNotify = true; + proto[name].apply(this, arguments); + this._dontNotify = false; + this._owner[this._setter](this); + }; + }, { + isSelected: function() { + return this._owner._boundsSelected; + }, + + setSelected: function(selected) { + var owner = this._owner; + if (owner.setSelected) { + owner._boundsSelected = selected; + owner.setSelected(selected || owner._selectedSegmentState > 0); + } + } + }) + ); +}); + +var Matrix = Base.extend({ + _class: 'Matrix', + + initialize: function Matrix(arg) { + var count = arguments.length, + ok = true; + if (count === 6) { + this.set.apply(this, arguments); + } else if (count === 1) { + if (arg instanceof Matrix) { + this.set(arg._a, arg._c, arg._b, arg._d, arg._tx, arg._ty); + } else if (Array.isArray(arg)) { + this.set.apply(this, arg); + } else { + ok = false; + } + } else if (count === 0) { + this.reset(); + } else { + ok = false; + } + if (!ok) + throw new Error('Unsupported matrix parameters'); + }, + + set: function(a, c, b, d, tx, ty, _dontNotify) { + this._a = a; + this._c = c; + this._b = b; + this._d = d; + this._tx = tx; + this._ty = ty; + if (!_dontNotify) + this._changed(); + return this; + }, + + _serialize: function(options) { + return Base.serialize(this.getValues(), options); + }, + + _changed: function() { + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(9); + } + } + }, + + clone: function() { + return new Matrix(this._a, this._c, this._b, this._d, + this._tx, this._ty); + }, + + equals: function(mx) { + return mx === this || mx && this._a === mx._a && this._b === mx._b + && this._c === mx._c && this._d === mx._d + && this._tx === mx._tx && this._ty === mx._ty + || false; + }, + + toString: function() { + var f = Formatter.instance; + return '[[' + [f.number(this._a), f.number(this._b), + f.number(this._tx)].join(', ') + '], [' + + [f.number(this._c), f.number(this._d), + f.number(this._ty)].join(', ') + ']]'; + }, + + reset: function(_dontNotify) { + this._a = this._d = 1; + this._c = this._b = this._tx = this._ty = 0; + if (!_dontNotify) + this._changed(); + return this; + }, + + apply: function(recursively, _setApplyMatrix) { + var owner = this._owner; + if (owner) { + owner.transform(null, true, Base.pick(recursively, true), + _setApplyMatrix); + return this.isIdentity(); + } + return false; + }, + + translate: function() { + var point = Point.read(arguments), + x = point.x, + y = point.y; + this._tx += x * this._a + y * this._b; + this._ty += x * this._c + y * this._d; + this._changed(); + return this; + }, + + scale: function() { + var scale = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + if (center) + this.translate(center); + this._a *= scale.x; + this._c *= scale.x; + this._b *= scale.y; + this._d *= scale.y; + if (center) + this.translate(center.negate()); + this._changed(); + return this; + }, + + rotate: function(angle ) { + angle *= Math.PI / 180; + var center = Point.read(arguments, 1), + x = center.x, + y = center.y, + cos = Math.cos(angle), + sin = Math.sin(angle), + tx = x - x * cos + y * sin, + ty = y - x * sin - y * cos, + a = this._a, + b = this._b, + c = this._c, + d = this._d; + this._a = cos * a + sin * b; + this._b = -sin * a + cos * b; + this._c = cos * c + sin * d; + this._d = -sin * c + cos * d; + this._tx += tx * a + ty * b; + this._ty += tx * c + ty * d; + this._changed(); + return this; + }, + + shear: function() { + var shear = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + if (center) + this.translate(center); + var a = this._a, + c = this._c; + this._a += shear.y * this._b; + this._c += shear.y * this._d; + this._b += shear.x * a; + this._d += shear.x * c; + if (center) + this.translate(center.negate()); + this._changed(); + return this; + }, + + skew: function() { + var skew = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }), + toRadians = Math.PI / 180, + shear = new Point(Math.tan(skew.x * toRadians), + Math.tan(skew.y * toRadians)); + return this.shear(shear, center); + }, + + concatenate: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + this._a = a2 * a1 + c2 * b1; + this._b = b2 * a1 + d2 * b1; + this._c = a2 * c1 + c2 * d1; + this._d = b2 * c1 + d2 * d1; + this._tx += tx2 * a1 + ty2 * b1; + this._ty += tx2 * c1 + ty2 * d1; + this._changed(); + return this; + }, + + preConcatenate: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + tx1 = this._tx, + ty1 = this._ty, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + this._a = a2 * a1 + b2 * c1; + this._b = a2 * b1 + b2 * d1; + this._c = c2 * a1 + d2 * c1; + this._d = c2 * b1 + d2 * d1; + this._tx = a2 * tx1 + b2 * ty1 + tx2; + this._ty = c2 * tx1 + d2 * ty1 + ty2; + this._changed(); + return this; + }, + + chain: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + tx1 = this._tx, + ty1 = this._ty, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + return new Matrix( + a2 * a1 + c2 * b1, + a2 * c1 + c2 * d1, + b2 * a1 + d2 * b1, + b2 * c1 + d2 * d1, + tx1 + tx2 * a1 + ty2 * b1, + ty1 + tx2 * c1 + ty2 * d1); + }, + + isIdentity: function() { + return this._a === 1 && this._c === 0 && this._b === 0 && this._d === 1 + && this._tx === 0 && this._ty === 0; + }, + + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + + isInvertible: function() { + return !!this._getDeterminant(); + }, + + isSingular: function() { + return !this._getDeterminant(); + }, + + transform: function( src, dst, count) { + return arguments.length < 3 + ? this._transformPoint(Point.read(arguments)) + : this._transformCoordinates(src, dst, count); + }, + + _transformPoint: function(point, dest, _dontNotify) { + var x = point.x, + y = point.y; + if (!dest) + dest = new Point(); + return dest.set( + x * this._a + y * this._b + this._tx, + x * this._c + y * this._d + this._ty, + _dontNotify + ); + }, + + _transformCoordinates: function(src, dst, count) { + var i = 0, + j = 0, + max = 2 * count; + while (i < max) { + var x = src[i++], + y = src[i++]; + dst[j++] = x * this._a + y * this._b + this._tx; + dst[j++] = x * this._c + y * this._d + this._ty; + } + return dst; + }, + + _transformCorners: function(rect) { + var x1 = rect.x, + y1 = rect.y, + x2 = x1 + rect.width, + y2 = y1 + rect.height, + coords = [ x1, y1, x2, y1, x2, y2, x1, y2 ]; + return this._transformCoordinates(coords, coords, 4); + }, + + _transformBounds: function(bounds, dest, _dontNotify) { + var coords = this._transformCorners(bounds), + min = coords.slice(0, 2), + max = min.slice(); + for (var i = 2; i < 8; i++) { + var val = coords[i], + j = i & 1; + if (val < min[j]) + min[j] = val; + else if (val > max[j]) + max[j] = val; + } + if (!dest) + dest = new Rectangle(); + return dest.set(min[0], min[1], max[0] - min[0], max[1] - min[1], + _dontNotify); + }, + + inverseTransform: function() { + return this._inverseTransform(Point.read(arguments)); + }, + + _getDeterminant: function() { + var det = this._a * this._d - this._b * this._c; + return isFinite(det) && !Numerical.isZero(det) + && isFinite(this._tx) && isFinite(this._ty) + ? det : null; + }, + + _inverseTransform: function(point, dest, _dontNotify) { + var det = this._getDeterminant(); + if (!det) + return null; + var x = point.x - this._tx, + y = point.y - this._ty; + if (!dest) + dest = new Point(); + return dest.set( + (x * this._d - y * this._b) / det, + (y * this._a - x * this._c) / det, + _dontNotify + ); + }, + + decompose: function() { + var a = this._a, b = this._b, c = this._c, d = this._d; + if (Numerical.isZero(a * d - b * c)) + return null; + + var scaleX = Math.sqrt(a * a + b * b); + a /= scaleX; + b /= scaleX; + + var shear = a * c + b * d; + c -= a * shear; + d -= b * shear; + + var scaleY = Math.sqrt(c * c + d * d); + c /= scaleY; + d /= scaleY; + shear /= scaleY; + + if (a * d < b * c) { + a = -a; + b = -b; + shear = -shear; + scaleX = -scaleX; + } + + return { + scaling: new Point(scaleX, scaleY), + rotation: -Math.atan2(b, a) * 180 / Math.PI, + shearing: shear + }; + }, + + getValues: function() { + return [ this._a, this._c, this._b, this._d, this._tx, this._ty ]; + }, + + getTranslation: function() { + return new Point(this._tx, this._ty); + }, + + getScaling: function() { + return (this.decompose() || {}).scaling; + }, + + getRotation: function() { + return (this.decompose() || {}).rotation; + }, + + inverted: function() { + var det = this._getDeterminant(); + return det && new Matrix( + this._d / det, + -this._c / det, + -this._b / det, + this._a / det, + (this._b * this._ty - this._d * this._tx) / det, + (this._c * this._tx - this._a * this._ty) / det); + }, + + shiftless: function() { + return new Matrix(this._a, this._c, this._b, this._d, 0, 0); + }, + + applyToContext: function(ctx) { + ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); + } +}, Base.each(['a', 'c', 'b', 'd', 'tx', 'ty'], function(name) { + var part = Base.capitalize(name), + prop = '_' + name; + this['get' + part] = function() { + return this[prop]; + }; + this['set' + part] = function(value) { + this[prop] = value; + this._changed(); + }; +}, {})); + +var Line = Base.extend({ + _class: 'Line', + + initialize: function Line(arg0, arg1, arg2, arg3, arg4) { + var asVector = false; + if (arguments.length >= 4) { + this._px = arg0; + this._py = arg1; + this._vx = arg2; + this._vy = arg3; + asVector = arg4; + } else { + this._px = arg0.x; + this._py = arg0.y; + this._vx = arg1.x; + this._vy = arg1.y; + asVector = arg2; + } + if (!asVector) { + this._vx -= this._px; + this._vy -= this._py; + } + }, + + getPoint: function() { + return new Point(this._px, this._py); + }, + + getVector: function() { + return new Point(this._vx, this._vy); + }, + + getLength: function() { + return this.getVector().getLength(); + }, + + intersect: function(line, isInfinite) { + return Line.intersect( + this._px, this._py, this._vx, this._vy, + line._px, line._py, line._vx, line._vy, + true, isInfinite); + }, + + getSide: function(point, isInfinite) { + return Line.getSide( + this._px, this._py, this._vx, this._vy, + point.x, point.y, true, isInfinite); + }, + + getDistance: function(point) { + return Math.abs(Line.getSignedDistance( + this._px, this._py, this._vx, this._vy, + point.x, point.y, true)); + }, + + isCollinear: function(line) { + return Point.isCollinear(this._vx, this._vy, line._vx, line._vy); + }, + + isOrthogonal: function(line) { + return Point.isOrthogonal(this._vx, this._vy, line._vx, line._vy); + }, + + statics: { + intersect: function(p1x, p1y, v1x, v1y, p2x, p2y, v2x, v2y, asVector, + isInfinite) { + if (!asVector) { + v1x -= p1x; + v1y -= p1y; + v2x -= p2x; + v2y -= p2y; + } + var cross = v1x * v2y - v1y * v2x; + if (!Numerical.isZero(cross)) { + var dx = p1x - p2x, + dy = p1y - p2y, + u1 = (v2x * dy - v2y * dx) / cross, + u2 = (v1x * dy - v1y * dx) / cross, + epsilon = 1e-12, + uMin = -epsilon, + uMax = 1 + epsilon; + if (isInfinite + || uMin < u1 && u1 < uMax && uMin < u2 && u2 < uMax) { + if (!isInfinite) { + u1 = u1 <= 0 ? 0 : u1 >= 1 ? 1 : u1; + } + return new Point( + p1x + u1 * v1x, + p1y + u1 * v1y); + } + } + }, + + getSide: function(px, py, vx, vy, x, y, asVector, isInfinite) { + if (!asVector) { + vx -= px; + vy -= py; + } + var v2x = x - px, + v2y = y - py, + ccw = v2x * vy - v2y * vx; + if (ccw === 0 && !isInfinite) { + ccw = (v2x * vx + v2x * vx) / (vx * vx + vy * vy); + if (ccw >= 0 && ccw <= 1) + ccw = 0; + } + return ccw < 0 ? -1 : ccw > 0 ? 1 : 0; + }, + + getSignedDistance: function(px, py, vx, vy, x, y, asVector) { + if (!asVector) { + vx -= px; + vy -= py; + } + return vx === 0 ? vy > 0 ? x - px : px - x + : vy === 0 ? vx < 0 ? y - py : py - y + : ((x-px) * vy - (y-py) * vx) / Math.sqrt(vx * vx + vy * vy); + } + } +}); + +var Project = PaperScopeItem.extend({ + _class: 'Project', + _list: 'projects', + _reference: 'project', + + initialize: function Project(element) { + PaperScopeItem.call(this, true); + this.layers = []; + this._activeLayer = null; + this.symbols = []; + this._currentStyle = new Style(null, null, this); + this._view = View.create(this, + element || CanvasProvider.getCanvas(1, 1)); + this._selectedItems = {}; + this._selectedItemCount = 0; + this._updateVersion = 0; + }, + + _serialize: function(options, dictionary) { + return Base.serialize(this.layers, options, true, dictionary); + }, + + clear: function() { + for (var i = this.layers.length - 1; i >= 0; i--) + this.layers[i].remove(); + this.symbols = []; + }, + + isEmpty: function() { + return this.layers.length === 0; + }, + + remove: function remove() { + if (!remove.base.call(this)) + return false; + if (this._view) + this._view.remove(); + return true; + }, + + getView: function() { + return this._view; + }, + + getCurrentStyle: function() { + return this._currentStyle; + }, + + setCurrentStyle: function(style) { + this._currentStyle.initialize(style); + }, + + getIndex: function() { + return this._index; + }, + + getOptions: function() { + return this._scope.settings; + }, + + getActiveLayer: function() { + return this._activeLayer || new Layer({ project: this }); + }, + + getSelectedItems: function() { + var items = []; + for (var id in this._selectedItems) { + var item = this._selectedItems[id]; + if (item.isInserted()) + items.push(item); + } + return items; + }, + + insertChild: function(index, item, _preserve) { + if (item instanceof Layer) { + item._remove(false, true); + Base.splice(this.layers, [item], index, 0); + item._setProject(this, true); + if (this._changes) + item._changed(5); + if (!this._activeLayer) + this._activeLayer = item; + } else if (item instanceof Item) { + (this._activeLayer + || this.insertChild(index, new Layer(Item.NO_INSERT))) + .insertChild(index, item, _preserve); + } else { + item = null; + } + return item; + }, + + addChild: function(item, _preserve) { + return this.insertChild(undefined, item, _preserve); + }, + + _updateSelection: function(item) { + var id = item._id, + selectedItems = this._selectedItems; + if (item._selected) { + if (selectedItems[id] !== item) { + this._selectedItemCount++; + selectedItems[id] = item; + } + } else if (selectedItems[id] === item) { + this._selectedItemCount--; + delete selectedItems[id]; + } + }, + + selectAll: function() { + var layers = this.layers; + for (var i = 0, l = layers.length; i < l; i++) + layers[i].setFullySelected(true); + }, + + deselectAll: function() { + var selectedItems = this._selectedItems; + for (var i in selectedItems) + selectedItems[i].setFullySelected(false); + }, + + hitTest: function() { + var point = Point.read(arguments), + options = HitResult.getOptions(Base.read(arguments)); + for (var i = this.layers.length - 1; i >= 0; i--) { + var res = this.layers[i]._hitTest(point, options); + if (res) return res; + } + return null; + }, + + getItems: function(match) { + return Item._getItems(this.layers, match); + }, + + getItem: function(match) { + return Item._getItems(this.layers, match, null, null, true)[0] || null; + }, + + importJSON: function(json) { + this.activate(); + var layer = this._activeLayer; + return Base.importJSON(json, layer && layer.isEmpty() && layer); + }, + + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; + ctx.save(); + matrix.applyToContext(ctx); + var param = new Base({ + offset: new Point(0, 0), + pixelRatio: pixelRatio, + viewMatrix: matrix.isIdentity() ? null : matrix, + matrices: [new Matrix()], + updateMatrix: true + }); + for (var i = 0, layers = this.layers, l = layers.length; i < l; i++) + layers[i].draw(ctx, param); + ctx.restore(); + + if (this._selectedItemCount > 0) { + ctx.save(); + ctx.strokeWidth = 1; + var items = this._selectedItems, + size = this._scope.settings.handleSize, + version = this._updateVersion; + for (var id in items) + items[id]._drawSelection(ctx, matrix, size, items, version); + ctx.restore(); + } + } +}); + +var Symbol = Base.extend({ + _class: 'Symbol', + + initialize: function Symbol(item, dontCenter) { + this._id = UID.get(); + this.project = paper.project; + this.project.symbols.push(this); + if (item) + this.setDefinition(item, dontCenter); + }, + + _serialize: function(options, dictionary) { + return dictionary.add(this, function() { + return Base.serialize([this._class, this._definition], + options, false, dictionary); + }); + }, + + _changed: function(flags) { + if (flags & 8) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } + }, + + getDefinition: function() { + return this._definition; + }, + + setDefinition: function(item, _dontCenter) { + if (item._parentSymbol) + item = item.clone(); + if (this._definition) + this._definition._parentSymbol = null; + this._definition = item; + item.remove(); + item.setSelected(false); + if (!_dontCenter) + item.setPosition(new Point()); + item._parentSymbol = this; + this._changed(9); + }, + + place: function(position) { + return new PlacedSymbol(this, position); + }, + + clone: function() { + return new Symbol(this._definition.clone(false)); + }, + + equals: function(symbol) { + return symbol === this + || symbol && this.definition.equals(symbol.definition) + || false; + } +}); + +var Item = Base.extend(Emitter, { + statics: { + extend: function extend(src) { + if (src._serializeFields) + src._serializeFields = new Base( + this.prototype._serializeFields, src._serializeFields); + return extend.base.apply(this, arguments); + }, + + NO_INSERT: { insert: false } + }, + + _class: 'Item', + _applyMatrix: true, + _canApplyMatrix: true, + _boundsSelected: false, + _selectChildren: false, + _serializeFields: { + name: null, + applyMatrix: null, + matrix: new Matrix(), + pivot: null, + locked: false, + visible: true, + blendMode: 'normal', + opacity: 1, + guide: false, + selected: false, + clipMask: false, + data: {} + }, + + initialize: function Item() { + }, + + _initialize: function(props, point) { + var hasProps = props && Base.isPlainObject(props), + internal = hasProps && props.internal === true, + matrix = this._matrix = new Matrix(), + project = hasProps && props.project || paper.project; + if (!internal) + this._id = UID.get(); + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this, project); + if (!this._project) { + if (internal || hasProps && props.insert === false) { + this._setProject(project); + } else if (hasProps && props.parent) { + this.setParent(props.parent); + } else { + (project._activeLayer || new Layer()).addChild(this); + } + } + if (hasProps && props !== Item.NO_INSERT) + this._set(props, { insert: true, project: true, parent: true }, + true); + return hasProps; + }, + + _events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', + 'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'], + function(name) { + this[name] = { + install: function(type) { + this.getView()._installEvent(type); + }, + + uninstall: function(type) { + this.getView()._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.getView()._animateItem(this, true); + }, + + uninstall: function() { + this.getView()._animateItem(this, false); + } + }, + + onLoad: {} + } + ), + + _serialize: function(options, dictionary) { + var props = {}, + that = this; + + function serialize(fields) { + for (var key in fields) { + var value = that[key]; + if (!Base.equals(value, key === 'leading' + ? fields.fontSize * 1.2 : fields[key])) { + props[key] = Base.serialize(value, options, + key !== 'data', dictionary); + } + } + } + + serialize(this._serializeFields); + if (!(this instanceof Group)) + serialize(this._style._defaults); + return [ this._class, props ]; + }, + + _changed: function(flags) { + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; + if (flags & 8) { + this._bounds = this._position = this._decomposed = + this._globalMatrix = this._currentPath = undefined; + } + if (cacheParent + && (flags & 40)) { + Item._clearBoundsCache(cacheParent); + } + if (flags & 2) { + Item._clearBoundsCache(this); + } + if (project) { + if (flags & 1) { + project._needsUpdate = true; + } + if (project._changes) { + var entry = project._changesById[this._id]; + if (entry) { + entry.flags |= flags; + } else { + entry = { item: this, flags: flags }; + project._changesById[this._id] = entry; + project._changes.push(entry); + } + } + } + if (symbol) + symbol._changed(flags); + }, + + set: function(props) { + if (props) + this._set(props); + return this; + }, + + getId: function() { + return this._id; + }, + + getName: function() { + return this._name; + }, + + setName: function(name, unique) { + + if (this._name) + this._removeNamed(); + if (name === (+name) + '') + throw new Error( + 'Names consisting only of numbers are not supported.'); + var parent = this._parent; + if (name && parent) { + var children = parent._children, + namedChildren = parent._namedChildren, + orig = name, + i = 1; + while (unique && children[name]) + name = orig + ' ' + (i++); + (namedChildren[name] = namedChildren[name] || []).push(this); + children[name] = this; + } + this._name = name || undefined; + this._changed(128); + }, + + getStyle: function() { + return this._style; + }, + + setStyle: function(style) { + this.getStyle().set(style); + } +}, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'], + function(name) { + var part = Base.capitalize(name), + name = '_' + name; + this['get' + part] = function() { + return this[name]; + }; + this['set' + part] = function(value) { + if (value != this[name]) { + this[name] = value; + this._changed(name === '_locked' + ? 128 : 129); + } + }; + }, +{}), { + beans: true, + + _locked: false, + + _visible: true, + + _blendMode: 'normal', + + _opacity: 1, + + _guide: false, + + isSelected: function() { + if (this._selectChildren) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + if (children[i].isSelected()) + return true; + } + return this._selected; + }, + + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + children[i].setSelected(selected); + } + if ((selected = !!selected) ^ this._selected) { + this._selected = selected; + this._project._updateSelection(this); + this._changed(129); + } + }, + + _selected: false, + + isFullySelected: function() { + var children = this._children; + if (children && this._selected) { + for (var i = 0, l = children.length; i < l; i++) + if (!children[i].isFullySelected()) + return false; + return true; + } + return this._selected; + }, + + setFullySelected: function(selected) { + var children = this._children; + if (children) { + for (var i = 0, l = children.length; i < l; i++) + children[i].setFullySelected(selected); + } + this.setSelected(selected, true); + }, + + isClipMask: function() { + return this._clipMask; + }, + + setClipMask: function(clipMask) { + if (this._clipMask != (clipMask = !!clipMask)) { + this._clipMask = clipMask; + if (clipMask) { + this.setFillColor(null); + this.setStrokeColor(null); + } + this._changed(129); + if (this._parent) + this._parent._changed(1024); + } + }, + + _clipMask: false, + + getData: function() { + if (!this._data) + this._data = {}; + return this._data; + }, + + setData: function(data) { + this._data = data; + }, + + getPosition: function(_dontLink) { + var position = this._position, + ctor = _dontLink ? Point : LinkedPoint; + if (!position) { + var pivot = this._pivot; + position = this._position = pivot + ? this._matrix._transformPoint(pivot) + : this.getBounds().getCenter(true); + } + return new ctor(position.x, position.y, this, 'setPosition'); + }, + + setPosition: function() { + this.translate(Point.read(arguments).subtract(this.getPosition(true))); + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setPivot'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments, 0, { clone: true, readNull: true }); + this._position = undefined; + }, + + _pivot: null, +}, Base.each(['bounds', 'strokeBounds', 'handleBounds', 'roughBounds', + 'internalBounds', 'internalRoughBounds'], + function(key) { + var getter = 'get' + Base.capitalize(key), + match = key.match(/^internal(.*)$/), + internalGetter = match ? 'get' + match[1] : null; + this[getter] = function(_matrix) { + var boundsGetter = this._boundsGetter, + name = !internalGetter && (typeof boundsGetter === 'string' + ? boundsGetter : boundsGetter && boundsGetter[getter]) + || getter, + bounds = this._getCachedBounds(name, _matrix, this, + internalGetter); + return key === 'bounds' + ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, + bounds.height, this, 'setBounds') + : bounds; + }; + }, +{ + beans: true, + + _getBounds: function(getter, matrix, cacheItem) { + var children = this._children; + if (!children || children.length == 0) + return new Rectangle(); + Item._updateBoundsCache(this, cacheItem); + var x1 = Infinity, + x2 = -x1, + y1 = x1, + y2 = x2; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i]; + if (child._visible && !child.isEmpty()) { + var rect = child._getCachedBounds(getter, + matrix && matrix.chain(child._matrix), cacheItem); + x1 = Math.min(rect.x, x1); + y1 = Math.min(rect.y, y1); + x2 = Math.max(rect.x + rect.width, x2); + y2 = Math.max(rect.y + rect.height, y2); + } + } + return isFinite(x1) + ? new Rectangle(x1, y1, x2 - x1, y2 - y1) + : new Rectangle(); + }, + + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), + matrix = new Matrix(), + center = rect.getCenter(); + matrix.translate(center); + if (rect.width != bounds.width || rect.height != bounds.height) { + matrix.scale( + bounds.width != 0 ? rect.width / bounds.width : 1, + bounds.height != 0 ? rect.height / bounds.height : 1); + } + center = bounds.getCenter(); + matrix.translate(-center.x, -center.y); + this.transform(matrix); + }, + + _getCachedBounds: function(getter, matrix, cacheItem, internalGetter) { + matrix = matrix && matrix.orNullIfIdentity(); + var _matrix = internalGetter ? null : this._matrix.orNullIfIdentity(), + cache = (!matrix || matrix.equals(_matrix)) && getter; + Item._updateBoundsCache(this._parent || this._parentSymbol, cacheItem); + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + var bounds = this._getBounds(internalGetter || getter, + matrix || _matrix, cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _updateBoundsCache: function(parent, item) { + if (parent) { + var id = item._id, + ref = parent._boundsCache = parent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(item); + ref.ids[id] = item; + } + } + }, + + _clearBoundsCache: function(item) { + var cache = item._boundsCache; + if (cache) { + item._bounds = item._position = item._boundsCache = undefined; + for (var i = 0, list = cache.list, l = list.length; i < l; i++){ + var other = list[i]; + if (other !== item) { + other._bounds = other._position = undefined; + if (other._boundsCache) + Item._clearBoundsCache(other); + } + } + } + } + } + +}), { + beans: true, + + _decompose: function() { + return this._decomposed = this._matrix.decompose(); + }, + + getRotation: function() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.rotation; + }, + + setRotation: function(rotation) { + var current = this.getRotation(); + if (current != null && rotation != null) { + var decomposed = this._decomposed; + this.rotate(rotation - current); + decomposed.rotation = rotation; + this._decomposed = decomposed; + } + }, + + getScaling: function(_dontLink) { + var decomposed = this._decomposed || this._decompose(), + scaling = decomposed && decomposed.scaling, + ctor = _dontLink ? Point : LinkedPoint; + return scaling && new ctor(scaling.x, scaling.y, this, 'setScaling'); + }, + + setScaling: function() { + var current = this.getScaling(); + if (current) { + var scaling = Point.read(arguments, 0, { clone: true }), + decomposed = this._decomposed; + this.scale(scaling.x / current.x, scaling.y / current.y); + decomposed.scaling = scaling; + this._decomposed = decomposed; + } + }, + + getMatrix: function() { + return this._matrix; + }, + + setMatrix: function() { + var matrix = this._matrix; + matrix.initialize.apply(matrix, arguments); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(9); + } + }, + + getGlobalMatrix: function(_dontClone) { + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + var parent = this._parent; + if (parent) + matrix.preConcatenate(parent.getGlobalMatrix(true)); + matrix._updateVersion = updateVersion; + } + return _dontClone ? matrix : matrix.clone(); + }, + + getApplyMatrix: function() { + return this._applyMatrix; + }, + + setApplyMatrix: function(apply) { + if (this._applyMatrix = this._canApplyMatrix && !!apply) + this.transform(null, true); + }, + + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', +}, { + getProject: function() { + return this._project; + }, + + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); + this._project = project; + var children = this._children; + for (var i = 0, l = children && children.length; i < l; i++) + children[i]._setProject(project); + installEvents = true; + } + if (installEvents) + this._installEvents(true); + }, + + getView: function() { + return this._project.getView(); + }, + + _installEvents: function _installEvents(install) { + _installEvents.base.call(this, install); + var children = this._children; + for (var i = 0, l = children && children.length; i < l; i++) + children[i]._installEvents(install); + }, + + getLayer: function() { + var parent = this; + while (parent = parent._parent) { + if (parent instanceof Layer) + return parent; + } + return null; + }, + + getParent: function() { + return this._parent; + }, + + setParent: function(item) { + return item.addChild(this); + }, + + getChildren: function() { + return this._children; + }, + + setChildren: function(items) { + this.removeChildren(); + this.addChildren(items); + }, + + getFirstChild: function() { + return this._children && this._children[0] || null; + }, + + getLastChild: function() { + return this._children && this._children[this._children.length - 1] + || null; + }, + + getNextSibling: function() { + return this._parent && this._parent._children[this._index + 1] || null; + }, + + getPreviousSibling: function() { + return this._parent && this._parent._children[this._index - 1] || null; + }, + + getIndex: function() { + return this._index; + }, + + equals: function(item) { + return item === this || item && this._class === item._class + && this._style.equals(item._style) + && this._matrix.equals(item._matrix) + && this._locked === item._locked + && this._visible === item._visible + && this._blendMode === item._blendMode + && this._opacity === item._opacity + && this._clipMask === item._clipMask + && this._guide === item._guide + && this._equals(item) + || false; + }, + + _equals: function(item) { + return Base.equals(this._children, item._children); + }, + + clone: function(insert) { + return this._clone(new this.constructor(Item.NO_INSERT), insert); + }, + + _clone: function(copy, insert, includeMatrix) { + var keys = ['_locked', '_visible', '_blendMode', '_opacity', + '_clipMask', '_guide'], + children = this._children; + copy.setStyle(this._style); + for (var i = 0, l = children && children.length; i < l; i++) { + copy.addChild(children[i].clone(false), true); + } + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + if (this.hasOwnProperty(key)) + copy[key] = this[key]; + } + if (includeMatrix !== false) + copy._matrix.initialize(this._matrix); + copy.setApplyMatrix(this._applyMatrix); + copy.setPivot(this._pivot); + copy.setSelected(this._selected); + copy._data = this._data ? Base.clone(this._data) : null; + if (insert || insert === undefined) + copy.insertAbove(this); + if (this._name) + copy.setName(this._name, true); + return copy; + }, + + copyTo: function(itemOrProject) { + return itemOrProject.addChild(this.clone(false)); + }, + + rasterize: function(resolution) { + var bounds = this.getStrokeBounds(), + scale = (resolution || this.getView().getResolution()) / 72, + topLeft = bounds.getTopLeft().floor(), + bottomRight = bounds.getBottomRight().ceil(), + size = new Size(bottomRight.subtract(topLeft)), + canvas = CanvasProvider.getCanvas(size.multiply(scale)), + ctx = canvas.getContext('2d'), + matrix = new Matrix().scale(scale).translate(topLeft.negate()); + ctx.save(); + matrix.applyToContext(ctx); + this.draw(ctx, new Base({ matrices: [matrix] })); + ctx.restore(); + var raster = new Raster(Item.NO_INSERT); + raster.setCanvas(canvas); + raster.transform(new Matrix().translate(topLeft.add(size.divide(2))) + .scale(1 / scale)); + raster.insertAbove(this); + return raster; + }, + + contains: function() { + return !!this._contains( + this._matrix._inverseTransform(Point.read(arguments))); + }, + + _contains: function(point) { + if (this._children) { + for (var i = this._children.length - 1; i >= 0; i--) { + if (this._children[i].contains(point)) + return true; + } + return false; + } + return point.isInside(this.getInternalBounds()); + }, + + isInside: function() { + return Rectangle.read(arguments).contains(this.getBounds()); + }, + + _asPathItem: function() { + return new Path.Rectangle({ + rectangle: this.getInternalBounds(), + matrix: this._matrix, + insert: false, + }); + }, + + intersects: function(item, _matrix) { + if (!(item instanceof Item)) + return false; + return this._asPathItem().getIntersections(item._asPathItem(), null, + _matrix || item._matrix, true).length > 0; + }, + + hitTest: function() { + return this._hitTest( + Point.read(arguments), + HitResult.getOptions(Base.read(arguments))); + }, + + _hitTest: function(point, options) { + if (this._locked || !this._visible || this._guide && !options.guides + || this.isEmpty()) + return null; + + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this.getView(), + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.chain(matrix) + : this.getGlobalMatrix().preConcatenate(view._matrix), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 1e-6) + ); + point = matrix._inverseTransform(point); + + if (!this._children && !this.getInternalRoughBounds() + .expand(tolerancePadding.multiply(2))._containsPoint(point)) + return null; + var checkSelf = !(options.guides && !this._guide + || options.selected && !this._selected + || options.type && options.type !== Base.hyphenate(this._class) + || options.class && !(this instanceof options.class)), + that = this, + res; + + function checkBounds(type, part) { + var pt = bounds['get' + part](); + if (point.subtract(pt).divide(tolerancePadding).length <= 1) + return new HitResult(type, that, + { name: Base.hyphenate(part), point: pt }); + } + + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); + if (options.center) + res = checkBounds('center', 'Center'); + if (!res && options.bounds) { + var points = [ + 'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', + 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter' + ]; + for (var i = 0; i < 8 && !res; i++) + res = checkBounds('bounds', points[i]); + } + } + + var children = !res && this._children; + if (children) { + var opts = this._getChildHitTestOptions(options); + for (var i = children.length - 1; i >= 0 && !res; i--) + res = children[i]._hitTest(point, opts); + } + if (!res && checkSelf) + res = this._hitTestSelf(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; + return res; + }, + + _getChildHitTestOptions: function(options) { + return options; + }, + + _hitTestSelf: function(point, options) { + if (options.fill && this.hasFill() && this._contains(point)) + return new HitResult('fill', this); + }, + + matches: function(name, compare) { + function matchObject(obj1, obj2) { + for (var i in obj1) { + if (obj1.hasOwnProperty(i)) { + var val1 = obj1[i], + val2 = obj2[i]; + if (Base.isPlainObject(val1) && Base.isPlainObject(val2)) { + if (!matchObject(val1, val2)) + return false; + } else if (!Base.equals(val1, val2)) { + return false; + } + } + } + return true; + } + var type = typeof name; + if (type === 'object') { + for (var key in name) { + if (name.hasOwnProperty(key) && !this.matches(key, name[key])) + return false; + } + } else if (type === 'function') { + return name(this); + } else { + var value = /^(empty|editable)$/.test(name) + ? this['is' + Base.capitalize(name)]() + : name === 'type' + ? Base.hyphenate(this._class) + : this[name]; + if (/^(constructor|class)$/.test(name)) { + if (!(this instanceof compare)) + return false; + } else if (compare instanceof RegExp) { + if (!compare.test(value)) + return false; + } else if (typeof compare === 'function') { + if (!compare(value)) + return false; + } else if (Base.isPlainObject(compare)) { + if (!matchObject(compare, value)) + return false; + } else if (!Base.equals(value, compare)) { + return false; + } + } + return true; + }, + + getItems: function(match) { + return Item._getItems(this._children, match, this._matrix); + }, + + getItem: function(match) { + return Item._getItems(this._children, match, this._matrix, null, true) + [0] || null; + }, + + statics: { + _getItems: function _getItems(children, match, matrix, param, + firstOnly) { + if (!param && typeof match === 'object') { + var overlapping = match.overlapping, + inside = match.inside, + bounds = overlapping || inside, + rect = bounds && Rectangle.read([bounds]); + param = { + items: [], + inside: !!inside, + overlapping: !!overlapping, + rect: rect, + path: overlapping && new Path.Rectangle({ + rectangle: rect, + insert: false + }) + }; + if (bounds) + match = Base.set({}, match, + { inside: true, overlapping: true }); + } + var items = param && param.items, + rect = param && param.rect; + matrix = rect && (matrix || new Matrix()); + for (var i = 0, l = children && children.length; i < l; i++) { + var child = children[i], + childMatrix = matrix && matrix.chain(child._matrix), + add = true; + if (rect) { + var bounds = child.getBounds(childMatrix); + if (!rect.intersects(bounds)) + continue; + if (!(param.inside && rect.contains(bounds)) + && !(param.overlapping && (bounds.contains(rect) + || param.path.intersects(child, childMatrix)))) + add = false; + } + if (add && child.matches(match)) { + items.push(child); + if (firstOnly) + break; + } + _getItems(child._children, match, + childMatrix, param, + firstOnly); + if (firstOnly && items.length > 0) + break; + } + return items; + } + } +}, { + + importJSON: function(json) { + var res = Base.importJSON(json, this); + return res !== this + ? this.addChild(res) + : res; + }, + + addChild: function(item, _preserve) { + return this.insertChild(undefined, item, _preserve); + }, + + insertChild: function(index, item, _preserve) { + var res = item ? this.insertChildren(index, [item], _preserve) : null; + return res && res[0]; + }, + + addChildren: function(items, _preserve) { + return this.insertChildren(this._children.length, items, _preserve); + }, + + insertChildren: function(index, items, _preserve, _proto) { + var children = this._children; + if (children && items && items.length > 0) { + items = Array.prototype.slice.apply(items); + for (var i = items.length - 1; i >= 0; i--) { + var item = items[i]; + if (_proto && !(item instanceof _proto)) { + items.splice(i, 1); + } else { + var shift = item._parent === this && item._index < index; + if (item._remove(false, true) && shift) + index--; + } + } + Base.splice(children, items, index, 0); + var project = this._project, + notifySelf = project && project._changes; + for (var i = 0, l = items.length; i < l; i++) { + var item = items[i]; + item._parent = this; + item._setProject(this._project, true); + if (item._name) + item.setName(item._name); + if (notifySelf) + this._changed(5); + } + this._changed(11); + } else { + items = null; + } + return items; + }, + + _insertSibling: function(index, item, _preserve) { + return this._parent + ? this._parent.insertChild(index, item, _preserve) + : null; + }, + + insertAbove: function(item, _preserve) { + return item._insertSibling(item._index + 1, this, _preserve); + }, + + insertBelow: function(item, _preserve) { + return item._insertSibling(item._index, this, _preserve); + }, + + sendToBack: function() { + return (this._parent || this instanceof Layer && this._project) + .insertChild(0, this); + }, + + bringToFront: function() { + return (this._parent || this instanceof Layer && this._project) + .addChild(this); + }, + + appendTop: '#addChild', + + appendBottom: function(item) { + return this.insertChild(0, item); + }, + + moveAbove: '#insertAbove', + + moveBelow: '#insertBelow', + + reduce: function() { + if (this._children && this._children.length === 1) { + var child = this._children[0].reduce(); + child.insertAbove(this); + child.setStyle(this._style); + this.remove(); + return child; + } + return this; + }, + + _removeNamed: function() { + var parent = this._parent; + if (parent) { + var children = parent._children, + namedChildren = parent._namedChildren, + name = this._name, + namedArray = namedChildren[name], + index = namedArray ? namedArray.indexOf(this) : -1; + if (index !== -1) { + if (children[name] == this) + delete children[name]; + namedArray.splice(index, 1); + if (namedArray.length) { + children[name] = namedArray[namedArray.length - 1]; + } else { + delete namedChildren[name]; + } + } + } + }, + + _remove: function(notifySelf, notifyParent) { + var parent = this._parent; + if (parent) { + if (this._name) + this._removeNamed(); + if (this._index != null) + Base.splice(parent._children, null, this._index, 1); + this._installEvents(false); + if (notifySelf) { + var project = this._project; + if (project && project._changes) + this._changed(5); + } + if (notifyParent) + parent._changed(11); + this._parent = null; + return true; + } + return false; + }, + + remove: function() { + return this._remove(true, true); + }, + + replaceWith: function(item) { + var ok = item && item.insertBelow(this); + if (ok) + this.remove(); + return ok; + }, + + removeChildren: function(from, to) { + if (!this._children) + return null; + from = from || 0; + to = Base.pick(to, this._children.length); + var removed = Base.splice(this._children, null, from, to - from); + for (var i = removed.length - 1; i >= 0; i--) { + removed[i]._remove(true, false); + } + if (removed.length > 0) + this._changed(11); + return removed; + }, + + clear: '#removeChildren', + + reverseChildren: function() { + if (this._children) { + this._children.reverse(); + for (var i = 0, l = this._children.length; i < l; i++) + this._children[i]._index = i; + this._changed(11); + } + }, + + isEmpty: function() { + return !this._children || this._children.length === 0; + }, + + isEditable: function() { + var item = this; + while (item) { + if (!item._visible || item._locked) + return false; + item = item._parent; + } + return true; + }, + + hasFill: function() { + return this.getStyle().hasFill(); + }, + + hasStroke: function() { + return this.getStyle().hasStroke(); + }, + + hasShadow: function() { + return this.getStyle().hasShadow(); + }, + + _getOrder: function(item) { + function getList(item) { + var list = []; + do { + list.unshift(item); + } while (item = item._parent); + return list; + } + var list1 = getList(this), + list2 = getList(item); + for (var i = 0, l = Math.min(list1.length, list2.length); i < l; i++) { + if (list1[i] != list2[i]) { + return list1[i]._index < list2[i]._index ? 1 : -1; + } + } + return 0; + }, + + hasChildren: function() { + return this._children && this._children.length > 0; + }, + + isInserted: function() { + return this._parent ? this._parent.isInserted() : false; + }, + + isAbove: function(item) { + return this._getOrder(item) === -1; + }, + + isBelow: function(item) { + return this._getOrder(item) === 1; + }, + + isParent: function(item) { + return this._parent === item; + }, + + isChild: function(item) { + return item && item._parent === this; + }, + + isDescendant: function(item) { + var parent = this; + while (parent = parent._parent) { + if (parent == item) + return true; + } + return false; + }, + + isAncestor: function(item) { + return item ? item.isDescendant(this) : false; + }, + + isSibling: function(item) { + return this._parent === item._parent; + }, + + isGroupedWith: function(item) { + var parent = this._parent; + while (parent) { + if (parent._parent + && /^(Group|Layer|CompoundPath)$/.test(parent._class) + && item.isDescendant(parent)) + return true; + parent = parent._parent; + } + return false; + }, + + translate: function() { + var mx = new Matrix(); + return this.transform(mx.translate.apply(mx, arguments)); + }, + + rotate: function(angle ) { + return this.transform(new Matrix().rotate(angle, + Point.read(arguments, 1, { readNull: true }) + || this.getPosition(true))); + } +}, Base.each(['scale', 'shear', 'skew'], function(name) { + this[name] = function() { + var point = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + return this.transform(new Matrix()[name](point, + center || this.getPosition(true))); + }; +}, { + +}), { + transform: function(matrix, _applyMatrix, _applyRecursively, + _setApplyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && ((!_matrix.isIdentity() || matrix) + || _applyMatrix && _applyRecursively && this._children); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix, + _applyRecursively, _setApplyMatrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + _matrix._transformPoint(pivot, pivot, true); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); + if (_setApplyMatrix && this._canApplyMatrix) + this._applyMatrix = true; + } + var bounds = this._bounds, + position = this._position; + this._changed(9); + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { + for (var key in bounds) { + var rect = bounds[key]; + if (applyMatrix || !rect._internal) + matrix._transformBounds(rect, rect); + } + var getter = this._boundsGetter, + rect = bounds[getter && getter.getBounds || getter || 'getBounds']; + if (rect) + this._position = rect.getCenter(true); + this._bounds = bounds; + } else if (matrix && position) { + this._position = matrix._transformPoint(position, position); + } + return this; + }, + + _transformContent: function(matrix, applyRecursively, setApplyMatrix) { + var children = this._children; + if (children) { + for (var i = 0, l = children.length; i < l; i++) + children[i].transform(matrix, true, applyRecursively, + setApplyMatrix); + return true; + } + }, + + globalToLocal: function() { + return this.getGlobalMatrix(true)._inverseTransform( + Point.read(arguments)); + }, + + localToGlobal: function() { + return this.getGlobalMatrix(true)._transformPoint( + Point.read(arguments)); + }, + + parentToLocal: function() { + return this._matrix._inverseTransform(Point.read(arguments)); + }, + + localToParent: function() { + return this._matrix._transformPoint(Point.read(arguments)); + }, + + fitBounds: function(rectangle, fill) { + rectangle = Rectangle.read(arguments); + var bounds = this.getBounds(), + itemRatio = bounds.height / bounds.width, + rectRatio = rectangle.height / rectangle.width, + scale = (fill ? itemRatio > rectRatio : itemRatio < rectRatio) + ? rectangle.width / bounds.width + : rectangle.height / bounds.height, + newBounds = new Rectangle(new Point(), + new Size(bounds.width * scale, bounds.height * scale)); + newBounds.setCenter(rectangle.getCenter()); + this.setBounds(newBounds); + }, + + _setStyles: function(ctx) { + var style = this._style, + fillColor = style.getFillColor(), + strokeColor = style.getStrokeColor(), + shadowColor = style.getShadowColor(); + if (fillColor) + ctx.fillStyle = fillColor.toCanvasStyle(ctx); + if (strokeColor) { + var strokeWidth = style.getStrokeWidth(); + if (strokeWidth > 0) { + ctx.strokeStyle = strokeColor.toCanvasStyle(ctx); + ctx.lineWidth = strokeWidth; + var strokeJoin = style.getStrokeJoin(), + strokeCap = style.getStrokeCap(), + miterLimit = style.getMiterLimit(); + if (strokeJoin) + ctx.lineJoin = strokeJoin; + if (strokeCap) + ctx.lineCap = strokeCap; + if (miterLimit) + ctx.miterLimit = miterLimit; + if (paper.support.nativeDash) { + var dashArray = style.getDashArray(), + dashOffset = style.getDashOffset(); + if (dashArray && dashArray.length) { + if ('setLineDash' in ctx) { + ctx.setLineDash(dashArray); + ctx.lineDashOffset = dashOffset; + } else { + ctx.mozDash = dashArray; + ctx.mozDashOffset = dashOffset; + } + } + } + } + } + if (shadowColor) { + var shadowBlur = style.getShadowBlur(); + if (shadowBlur > 0) { + ctx.shadowColor = shadowColor.toCanvasStyle(ctx); + ctx.shadowBlur = shadowBlur; + var offset = this.getShadowOffset(); + ctx.shadowOffsetX = offset.x; + ctx.shadowOffsetY = offset.y; + } + } + }, + + draw: function(ctx, param, parentStrokeMatrix) { + var updateVersion = this._updateVersion = this._project._updateVersion; + if (!this._visible || this._opacity === 0) + return; + var matrices = param.matrices, + viewMatrix = param.viewMatrix, + matrix = this._matrix, + globalMatrix = matrices[matrices.length - 1].chain(matrix); + if (!globalMatrix.isInvertible()) + return; + + function getViewMatrix(matrix) { + return viewMatrix ? viewMatrix.chain(matrix) : matrix; + } + + matrices.push(globalMatrix); + if (param.updateMatrix) { + globalMatrix._updateVersion = updateVersion; + this._globalMatrix = globalMatrix; + } + + var blendMode = this._blendMode, + opacity = this._opacity, + normalBlend = blendMode === 'normal', + nativeBlend = BlendMode.nativeModes[blendMode], + direct = normalBlend && opacity === 1 + || param.dontStart + || param.clip + || (nativeBlend || normalBlend && opacity < 1) + && this._canComposite(), + pixelRatio = param.pixelRatio || 1, + mainCtx, itemOffset, prevOffset; + if (!direct) { + var bounds = this.getStrokeBounds(getViewMatrix(globalMatrix)); + if (!bounds.width || !bounds.height) + return; + prevOffset = param.offset; + itemOffset = param.offset = bounds.getTopLeft().floor(); + mainCtx = ctx; + ctx = CanvasProvider.getContext(bounds.getSize().ceil().add(1) + .multiply(pixelRatio)); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); + } + ctx.save(); + var strokeMatrix = parentStrokeMatrix + ? parentStrokeMatrix.chain(matrix) + : !this.getStrokeScaling(true) && getViewMatrix(globalMatrix), + clip = !direct && param.clipItem, + transform = !strokeMatrix || clip; + if (direct) { + ctx.globalAlpha = opacity; + if (nativeBlend) + ctx.globalCompositeOperation = blendMode; + } else if (transform) { + ctx.translate(-itemOffset.x, -itemOffset.y); + } + if (transform) + (direct ? matrix : getViewMatrix(globalMatrix)).applyToContext(ctx); + if (clip) + param.clipItem.draw(ctx, param.extend({ clip: true })); + if (strokeMatrix) { + ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); + var offset = param.offset; + if (offset) + ctx.translate(-offset.x, -offset.y); + } + this._draw(ctx, param, strokeMatrix); + ctx.restore(); + matrices.pop(); + if (param.clip && !param.dontFinish) + ctx.clip(); + if (!direct) { + BlendMode.process(blendMode, ctx, mainCtx, opacity, + itemOffset.subtract(prevOffset).multiply(pixelRatio)); + CanvasProvider.release(ctx); + param.offset = prevOffset; + } + }, + + _isUpdated: function(updateVersion) { + var parent = this._parent; + if (parent instanceof CompoundPath) + return parent._isUpdated(updateVersion); + var updated = this._updateVersion === updateVersion; + if (!updated && parent && parent._visible + && parent._isUpdated(updateVersion)) { + this._updateVersion = updateVersion; + updated = true; + } + return updated; + }, + + _drawSelection: function(ctx, matrix, size, selectedItems, updateVersion) { + if ((this._drawSelected || this._boundsSelected) + && this._isUpdated(updateVersion)) { + var color = this.getSelectedColor(true) + || this.getLayer().getSelectedColor(true), + mx = matrix.chain(this.getGlobalMatrix(true)); + ctx.strokeStyle = ctx.fillStyle = color + ? color.toCanvasStyle(ctx) : '#009dec'; + if (this._drawSelected) + this._drawSelected(ctx, mx, selectedItems); + if (this._boundsSelected) { + var half = size / 2, + coords = mx._transformCorners(this.getInternalBounds()); + ctx.beginPath(); + for (var i = 0; i < 8; i++) + ctx[i === 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]); + ctx.closePath(); + ctx.stroke(); + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); + } + } + }, + + _canComposite: function() { + return false; + } +}, Base.each(['down', 'drag', 'up', 'move'], function(name) { + this['removeOn' + Base.capitalize(name)] = function() { + var hash = {}; + hash[name] = true; + return this.removeOn(hash); + }; +}, { + + removeOn: function(obj) { + for (var name in obj) { + if (obj[name]) { + var key = 'mouse' + name, + project = this._project, + sets = project._removeSets = project._removeSets || {}; + sets[key] = sets[key] || {}; + sets[key][this._id] = this; + } + } + return this; + } +})); + +var Group = Item.extend({ + _class: 'Group', + _selectChildren: true, + _serializeFields: { + children: [] + }, + + initialize: function Group(arg) { + this._children = []; + this._namedChildren = {}; + if (!this._initialize(arg)) + this.addChildren(Array.isArray(arg) ? arg : arguments); + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & 1026) { + this._clipItem = undefined; + } + }, + + _getClipItem: function() { + var clipItem = this._clipItem; + if (clipItem === undefined) { + clipItem = null; + for (var i = 0, l = this._children.length; i < l; i++) { + var child = this._children[i]; + if (child._clipMask) { + clipItem = child; + break; + } + } + this._clipItem = clipItem; + } + return clipItem; + }, + + isClipped: function() { + return !!this._getClipItem(); + }, + + setClipped: function(clipped) { + var child = this.getFirstChild(); + if (child) + child.setClipMask(clipped); + }, + + _draw: function(ctx, param) { + var clip = param.clip, + clipItem = !clip && this._getClipItem(), + draw = true; + param = param.extend({ clipItem: clipItem, clip: false }); + if (clip) { + if (this._currentPath) { + ctx.currentPath = this._currentPath; + draw = false; + } else { + ctx.beginPath(); + param.dontStart = param.dontFinish = true; + } + } else if (clipItem) { + clipItem.draw(ctx, param.extend({ clip: true })); + } + if (draw) { + for (var i = 0, l = this._children.length; i < l; i++) { + var item = this._children[i]; + if (item !== clipItem) + item.draw(ctx, param); + } + } + if (clip) { + this._currentPath = ctx.currentPath; + } + } +}); + +var Layer = Group.extend({ + _class: 'Layer', + + initialize: function Layer(arg) { + var props = Base.isPlainObject(arg) + ? new Base(arg) + : { children: Array.isArray(arg) ? arg : arguments }, + insert = props.insert; + props.insert = false; + Group.call(this, props); + if (insert || insert === undefined) { + this._project.addChild(this); + this.activate(); + } + }, + + _remove: function _remove(notifySelf, notifyParent) { + if (this._parent) + return _remove.base.call(this, notifySelf, notifyParent); + if (this._index != null) { + var project = this._project; + if (project._activeLayer === this) + project._activeLayer = this.getNextSibling() + || this.getPreviousSibling(); + Base.splice(project.layers, null, this._index, 1); + this._installEvents(false); + if (notifySelf && project._changes) + this._changed(5); + if (notifyParent) { + project._needsUpdate = true; + } + return true; + } + return false; + }, + + getNextSibling: function getNextSibling() { + return this._parent ? getNextSibling.base.call(this) + : this._project.layers[this._index + 1] || null; + }, + + getPreviousSibling: function getPreviousSibling() { + return this._parent ? getPreviousSibling.base.call(this) + : this._project.layers[this._index - 1] || null; + }, + + isInserted: function isInserted() { + return this._parent ? isInserted.base.call(this) : this._index != null; + }, + + activate: function() { + this._project._activeLayer = this; + }, + + _insertSibling: function _insertSibling(index, item, _preserve) { + return !this._parent + ? this._project.insertChild(index, item, _preserve) + : _insertSibling.base.call(this, index, item, _preserve); + } +}); + +var Shape = Item.extend({ + _class: 'Shape', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsSelected: true, + _serializeFields: { + type: null, + size: null, + radius: null + }, + + initialize: function Shape(props) { + this._initialize(props); + }, + + _equals: function(item) { + return this._type === item._type + && this._size.equals(item._size) + && Base.equals(this._radius, item._radius); + }, + + clone: function(insert) { + var copy = new Shape(Item.NO_INSERT); + copy.setType(this._type); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); + }, + + getType: function() { + return this._type; + }, + + setType: function(type) { + this._type = type; + }, + + getShape: '#getType', + setShape: '#setType', + + getSize: function() { + var size = this._size; + return new LinkedSize(size.width, size.height, this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var type = this._type, + width = size.width, + height = size.height; + if (type === 'rectangle') { + var radius = Size.min(this._radius, size.divide(2)); + this._radius.set(radius.width, radius.height); + } else if (type === 'circle') { + width = height = (width + height) / 2; + this._radius = width / 2; + } else if (type === 'ellipse') { + this._radius.set(width / 2, height / 2); + } + this._size.set(width, height); + this._changed(9); + } + }, + + getRadius: function() { + var rad = this._radius; + return this._type === 'circle' + ? rad + : new LinkedSize(rad.width, rad.height, this, 'setRadius'); + }, + + setRadius: function(radius) { + var type = this._type; + if (type === 'circle') { + if (radius === this._radius) + return; + var size = radius * 2; + this._radius = radius; + this._size.set(size, size); + } else { + radius = Size.read(arguments); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (type === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (type === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } + } + } + this._changed(9); + }, + + isEmpty: function() { + return false; + }, + + toPath: function(insert) { + var path = this._clone(new Path[Base.capitalize(this._type)]({ + center: new Point(), + size: this._size, + radius: this._radius, + insert: false + }), insert); + if (paper.settings.applyMatrix) + path.setApplyMatrix(true); + return path; + }, + + _draw: function(ctx, param, strokeMatrix) { + var style = this._style, + hasFill = style.hasFill(), + hasStroke = style.hasStroke(), + dontPaint = param.dontFinish || param.clip, + untransformed = !strokeMatrix; + if (hasFill || hasStroke || dontPaint) { + var type = this._type, + radius = this._radius, + isCircle = type === 'circle'; + if (!param.dontStart) + ctx.beginPath(); + if (untransformed && isCircle) { + ctx.arc(0, 0, radius, 0, Math.PI * 2, true); + } else { + var rx = isCircle ? radius : radius.width, + ry = isCircle ? radius : radius.height, + size = this._size, + width = size.width, + height = size.height; + if (untransformed && type === 'rectangle' && rx === 0 && ry === 0) { + ctx.rect(-width / 2, -height / 2, width, height); + } else { + var x = width / 2, + y = height / 2, + kappa = 1 - 0.5522847498307936, + cx = rx * kappa, + cy = ry * kappa, + c = [ + -x, -y + ry, + -x, -y + cy, + -x + cx, -y, + -x + rx, -y, + x - rx, -y, + x - cx, -y, + x, -y + cy, + x, -y + ry, + x, y - ry, + x, y - cy, + x - cx, y, + x - rx, y, + -x + rx, y, + -x + cx, y, + -x, y - cy, + -x, y - ry + ]; + if (strokeMatrix) + strokeMatrix.transform(c, c, 32); + ctx.moveTo(c[0], c[1]); + ctx.bezierCurveTo(c[2], c[3], c[4], c[5], c[6], c[7]); + if (x !== rx) + ctx.lineTo(c[8], c[9]); + ctx.bezierCurveTo(c[10], c[11], c[12], c[13], c[14], c[15]); + if (y !== ry) + ctx.lineTo(c[16], c[17]); + ctx.bezierCurveTo(c[18], c[19], c[20], c[21], c[22], c[23]); + if (x !== rx) + ctx.lineTo(c[24], c[25]); + ctx.bezierCurveTo(c[26], c[27], c[28], c[29], c[30], c[31]); + } + } + ctx.closePath(); + } + if (!dontPaint && (hasFill || hasStroke)) { + this._setStyles(ctx); + if (hasFill) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (hasStroke) + ctx.stroke(); + } + }, + + _canComposite: function() { + return !(this.hasFill() && this.hasStroke()); + }, + + _getBounds: function(getter, matrix) { + var rect = new Rectangle(this._size).setCenter(0, 0); + if (getter !== 'getBounds' && this.hasStroke()) + rect = rect.expand(this.getStrokeWidth()); + return matrix ? matrix._transformBounds(rect) : rect; + } +}, +new function() { + function getCornerCenter(that, point, expand) { + var radius = that._radius; + if (!radius.isZero()) { + var halfSize = that._size.divide(2); + for (var i = 0; i < 4; i++) { + var dir = new Point(i & 1 ? 1 : -1, i > 1 ? 1 : -1), + corner = dir.multiply(halfSize), + center = corner.subtract(dir.multiply(radius)), + rect = new Rectangle(corner, center); + if ((expand ? rect.expand(expand) : rect).contains(point)) + return center; + } + } + } + + function getEllipseRadius(point, radius) { + var angle = point.getAngleInRadians(), + width = radius.width * 2, + height = radius.height * 2, + x = width * Math.sin(angle), + y = height * Math.cos(angle); + return width * height / (2 * Math.sqrt(x * x + y * y)); + } + + return { + _contains: function _contains(point) { + if (this._type === 'rectangle') { + var center = getCornerCenter(this, point); + return center + ? point.subtract(center).divide(this._radius) + .getLength() <= 1 + : _contains.base.call(this, point); + } else { + return point.divide(this.size).getLength() <= 0.5; + } + }, + + _hitTestSelf: function _hitTestSelf(point, options) { + var hit = false; + if (this.hasStroke()) { + var type = this._type, + radius = this._radius, + strokeWidth = this.getStrokeWidth() + 2 * options.tolerance; + if (type === 'rectangle') { + var center = getCornerCenter(this, point, strokeWidth); + if (center) { + var pt = point.subtract(center); + hit = 2 * Math.abs(pt.getLength() + - getEllipseRadius(pt, radius)) <= strokeWidth; + } else { + var rect = new Rectangle(this._size).setCenter(0, 0), + outer = rect.expand(strokeWidth), + inner = rect.expand(-strokeWidth); + hit = outer._containsPoint(point) + && !inner._containsPoint(point); + } + } else { + if (type === 'ellipse') + radius = getEllipseRadius(point, radius); + hit = 2 * Math.abs(point.getLength() - radius) + <= strokeWidth; + } + } + return hit + ? new HitResult('stroke', this) + : _hitTestSelf.base.apply(this, arguments); + } + }; +}, { + +statics: new function() { + function createShape(type, point, size, radius, args) { + var item = new Shape(Base.getNamed(args)); + item._type = type; + item._size = size; + item._radius = radius; + return item.translate(point); + } + + return { + Circle: function() { + var center = Point.readNamed(arguments, 'center'), + radius = Base.readNamed(arguments, 'radius'); + return createShape('circle', center, new Size(radius * 2), radius, + arguments); + }, + + Rectangle: function() { + var rect = Rectangle.readNamed(arguments, 'rectangle'), + radius = Size.min(Size.readNamed(arguments, 'radius'), + rect.getSize(true).divide(2)); + return createShape('rectangle', rect.getCenter(true), + rect.getSize(true), radius, arguments); + }, + + Ellipse: function() { + var ellipse = Shape._readEllipse(arguments), + radius = ellipse.radius; + return createShape('ellipse', ellipse.center, radius.multiply(2), + radius, arguments); + }, + + _readEllipse: function(args) { + var center, + radius; + if (Base.hasNamed(args, 'radius')) { + center = Point.readNamed(args, 'center'); + radius = Size.readNamed(args, 'radius'); + } else { + var rect = Rectangle.readNamed(args, 'rectangle'); + center = rect.getCenter(true); + radius = rect.getSize(true).divide(2); + } + return { center: center, radius: radius }; + } + }; +}}); + +var Raster = Item.extend({ + _class: 'Raster', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsGetter: 'getBounds', + _boundsSelected: true, + _serializeFields: { + crossOrigin: null, + source: null + }, + + initialize: function Raster(object, position) { + if (!this._initialize(object, + position !== undefined && Point.read(arguments, 1))) { + if (typeof object === 'string') { + this.setSource(object); + } else { + this.setImage(object); + } + } + if (!this._size) { + this._size = new Size(); + this._loaded = false; + } + }, + + _equals: function(item) { + return this.getSource() === item.getSource(); + }, + + clone: function(insert) { + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; + if (image) { + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setImage(copyCanvas); + } + copy._crossOrigin = this._crossOrigin; + return this._clone(copy, insert); + }, + + getSize: function() { + var size = this._size; + return new LinkedSize(size ? size.width : 0, size ? size.height : 0, + this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (!size.equals(this._size)) { + if (size.width > 0 && size.height > 0) { + var element = this.getElement(); + this.setImage(CanvasProvider.getCanvas(size)); + if (element) + this.getContext(true).drawImage(element, 0, 0, + size.width, size.height); + } else { + if (this._canvas) + CanvasProvider.release(this._canvas); + this._size = size.clone(); + } + } + }, + + getWidth: function() { + return this._size ? this._size.width : 0; + }, + + setWidth: function(width) { + this.setSize(width, this.getHeight()); + }, + + getHeight: function() { + return this._size ? this._size.height : 0; + }, + + setHeight: function(height) { + this.setSize(this.getWidth(), height); + }, + + isEmpty: function() { + var size = this._size; + return !size || size.width === 0 && size.height === 0; + }, + + getResolution: function() { + var matrix = this._matrix, + orig = new Point(0, 0).transform(matrix), + u = new Point(1, 0).transform(matrix).subtract(orig), + v = new Point(0, 1).transform(matrix).subtract(orig); + return new Size( + 72 / u.getLength(), + 72 / v.getLength() + ); + }, + + getPpi: '#getResolution', + + getImage: function() { + return this._image; + }, + + setImage: function(image) { + if (this._canvas) + CanvasProvider.release(this._canvas); + if (image && image.getContext) { + this._image = null; + this._canvas = image; + this._loaded = true; + } else { + this._image = image; + this._canvas = null; + this._loaded = image && image.complete; + } + this._size = new Size( + image ? image.naturalWidth || image.width : 0, + image ? image.naturalHeight || image.height : 0); + this._context = null; + this._changed(521); + }, + + getCanvas: function() { + if (!this._canvas) { + var ctx = CanvasProvider.getContext(this._size); + try { + if (this._image) + ctx.drawImage(this._image, 0, 0); + this._canvas = ctx.canvas; + } catch (e) { + CanvasProvider.release(ctx); + } + } + return this._canvas; + }, + + setCanvas: '#setImage', + + getContext: function(modify) { + if (!this._context) + this._context = this.getCanvas().getContext('2d'); + if (modify) { + this._image = null; + this._changed(513); + } + return this._context; + }, + + setContext: function(context) { + this._context = context; + }, + + getSource: function() { + return this._image && this._image.src || this.toDataURL(); + }, + + setSource: function(src) { + var that = this, + crossOrigin = this._crossOrigin, + image; + + function loaded() { + var view = that.getView(); + if (view) { + paper = view._scope; + that.setImage(image); + that.emit('load'); + view.update(); + } + } + + image = document.getElementById(src) || new Image(); + if (crossOrigin) + image.crossOrigin = crossOrigin; + if (image.naturalWidth && image.naturalHeight) { + setTimeout(loaded, 0); + } else { + DomEvent.add(image, { load: loaded }); + if (!image.src) + image.src = src; + } + this.setImage(image); + }, + + getCrossOrigin: function() { + return this._image && this._image.crossOrigin || this._crossOrigin || ''; + }, + + setCrossOrigin: function(crossOrigin) { + this._crossOrigin = crossOrigin; + if (this._image) + this._image.crossOrigin = crossOrigin; + }, + + getElement: function() { + return this._canvas || this._loaded && this._image; + } +}, { + beans: false, + + getSubCanvas: function() { + var rect = Rectangle.read(arguments), + ctx = CanvasProvider.getContext(rect.getSize()); + ctx.drawImage(this.getCanvas(), rect.x, rect.y, + rect.width, rect.height, 0, 0, rect.width, rect.height); + return ctx.canvas; + }, + + getSubRaster: function() { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setImage(this.getSubCanvas(rect)); + raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); + raster._matrix.preConcatenate(this._matrix); + raster.insertAbove(this); + return raster; + }, + + toDataURL: function() { + var src = this._image && this._image.src; + if (/^data:/.test(src)) + return src; + var canvas = this.getCanvas(); + return canvas ? canvas.toDataURL.apply(canvas, arguments) : null; + }, + + drawImage: function(image ) { + var point = Point.read(arguments, 1); + this.getContext(true).drawImage(image, point.x, point.y); + }, + + getAverageColor: function(object) { + var bounds, path; + if (!object) { + bounds = this.getBounds(); + } else if (object instanceof PathItem) { + path = object; + bounds = object.getBounds(); + } else if (object.width) { + bounds = new Rectangle(object); + } else if (object.x) { + bounds = new Rectangle(object.x - 0.5, object.y - 0.5, 1, 1); + } + var sampleSize = 32, + width = Math.min(bounds.width, sampleSize), + height = Math.min(bounds.height, sampleSize); + var ctx = Raster._sampleContext; + if (!ctx) { + ctx = Raster._sampleContext = CanvasProvider.getContext( + new Size(sampleSize)); + } else { + ctx.clearRect(0, 0, sampleSize + 1, sampleSize + 1); + } + ctx.save(); + var matrix = new Matrix() + .scale(width / bounds.width, height / bounds.height) + .translate(-bounds.x, -bounds.y); + matrix.applyToContext(ctx); + if (path) + path.draw(ctx, new Base({ clip: true, matrices: [matrix] })); + this._matrix.applyToContext(ctx); + var element = this.getElement(), + size = this._size; + if (element) + ctx.drawImage(element, -size.width / 2, -size.height / 2); + ctx.restore(); + var pixels = ctx.getImageData(0.5, 0.5, Math.ceil(width), + Math.ceil(height)).data, + channels = [0, 0, 0], + total = 0; + for (var i = 0, l = pixels.length; i < l; i += 4) { + var alpha = pixels[i + 3]; + total += alpha; + alpha /= 255; + channels[0] += pixels[i] * alpha; + channels[1] += pixels[i + 1] * alpha; + channels[2] += pixels[i + 2] * alpha; + } + for (var i = 0; i < 3; i++) + channels[i] /= total; + return total ? Color.read(channels) : null; + }, + + getPixel: function() { + var point = Point.read(arguments); + var data = this.getContext().getImageData(point.x, point.y, 1, 1).data; + return new Color('rgb', [data[0] / 255, data[1] / 255, data[2] / 255], + data[3] / 255); + }, + + setPixel: function() { + var point = Point.read(arguments), + color = Color.read(arguments), + components = color._convert('rgb'), + alpha = color._alpha, + ctx = this.getContext(true), + imageData = ctx.createImageData(1, 1), + data = imageData.data; + data[0] = components[0] * 255; + data[1] = components[1] * 255; + data[2] = components[2] * 255; + data[3] = alpha != null ? alpha * 255 : 255; + ctx.putImageData(imageData, point.x, point.y); + }, + + createImageData: function() { + var size = Size.read(arguments); + return this.getContext().createImageData(size.width, size.height); + }, + + getImageData: function() { + var rect = Rectangle.read(arguments); + if (rect.isEmpty()) + rect = new Rectangle(this._size); + return this.getContext().getImageData(rect.x, rect.y, + rect.width, rect.height); + }, + + setImageData: function(data ) { + var point = Point.read(arguments, 1); + this.getContext(true).putImageData(data, point.x, point.y); + }, + + _getBounds: function(getter, matrix) { + var rect = new Rectangle(this._size).setCenter(0, 0); + return matrix ? matrix._transformBounds(rect) : rect; + }, + + _hitTestSelf: function(point) { + if (this._contains(point)) { + var that = this; + return new HitResult('pixel', that, { + offset: point.add(that._size.divide(2)).round(), + color: { + get: function() { + return that.getPixel(this.offset); + } + } + }); + } + }, + + _draw: function(ctx) { + var element = this.getElement(); + if (element) { + ctx.globalAlpha = this._opacity; + ctx.drawImage(element, + -this._size.width / 2, -this._size.height / 2); + } + }, + + _canComposite: function() { + return true; + } +}); + +var PlacedSymbol = Item.extend({ + _class: 'PlacedSymbol', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsGetter: { getBounds: 'getStrokeBounds' }, + _boundsSelected: true, + _serializeFields: { + symbol: null + }, + + initialize: function PlacedSymbol(arg0, arg1) { + if (!this._initialize(arg0, + arg1 !== undefined && Point.read(arguments, 1))) + this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0)); + }, + + _equals: function(item) { + return this._symbol === item._symbol; + }, + + getSymbol: function() { + return this._symbol; + }, + + setSymbol: function(symbol) { + this._symbol = symbol; + this._changed(9); + }, + + clone: function(insert) { + var copy = new PlacedSymbol(Item.NO_INSERT); + copy.setSymbol(this._symbol); + return this._clone(copy, insert); + }, + + isEmpty: function() { + return this._symbol._definition.isEmpty(); + }, + + _getBounds: function(getter, matrix, cacheItem) { + var definition = this.symbol._definition; + return definition._getCachedBounds(getter, + matrix && matrix.chain(definition._matrix), cacheItem); + }, + + _hitTestSelf: function(point, options) { + var res = this._symbol._definition._hitTest(point, options); + if (res) + res.item = this; + return res; + }, + + _draw: function(ctx, param) { + this.symbol._definition.draw(ctx, param); + } + +}); + +var HitResult = Base.extend({ + _class: 'HitResult', + + initialize: function HitResult(type, item, values) { + this.type = type; + this.item = item; + if (values) { + values.enumerable = true; + this.inject(values); + } + }, + + statics: { + getOptions: function(options) { + return new Base({ + type: null, + tolerance: paper.settings.hitTolerance, + fill: !options, + stroke: !options, + segments: !options, + handles: false, + ends: false, + center: false, + bounds: false, + guides: false, + selected: false + }, options); + } + } +}); + +var Segment = Base.extend({ + _class: 'Segment', + beans: true, + + initialize: function Segment(arg0, arg1, arg2, arg3, arg4, arg5) { + var count = arguments.length, + point, handleIn, handleOut; + if (count === 0) { + } else if (count === 1) { + if ('point' in arg0) { + point = arg0.point; + handleIn = arg0.handleIn; + handleOut = arg0.handleOut; + } else { + point = arg0; + } + } else if (count === 2 && typeof arg0 === 'number') { + point = arguments; + } else if (count <= 3) { + point = arg0; + handleIn = arg1; + handleOut = arg2; + } else { + point = arg0 !== undefined ? [ arg0, arg1 ] : null; + handleIn = arg2 !== undefined ? [ arg2, arg3 ] : null; + handleOut = arg4 !== undefined ? [ arg4, arg5 ] : null; + } + new SegmentPoint(point, this, '_point'); + new SegmentPoint(handleIn, this, '_handleIn'); + new SegmentPoint(handleOut, this, '_handleOut'); + }, + + _serialize: function(options) { + return Base.serialize(this.hasHandles() + ? [this._point, this._handleIn, this._handleOut] + : this._point, + options, true); + }, + + _changed: function(point) { + var path = this._path; + if (!path) + return; + var curves = path._curves, + index = this._index, + curve; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curve = index > 0 ? curves[index - 1] : path._closed + ? curves[curves.length - 1] : null)) + curve._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curve = curves[index])) + curve._changed(); + } + path._changed(25); + }, + + getPoint: function() { + return this._point; + }, + + setPoint: function() { + var point = Point.read(arguments); + this._point.set(point.x, point.y); + }, + + getHandleIn: function() { + return this._handleIn; + }, + + setHandleIn: function() { + var point = Point.read(arguments); + this._handleIn.set(point.x, point.y); + }, + + getHandleOut: function() { + return this._handleOut; + }, + + setHandleOut: function() { + var point = Point.read(arguments); + this._handleOut.set(point.x, point.y); + }, + + hasHandles: function() { + return !this._handleIn.isZero() || !this._handleOut.isZero(); + }, + + clearHandles: function() { + this._handleIn.set(0, 0); + this._handleOut.set(0, 0); + }, + + _selectionState: 0, + + isSelected: function(_point) { + var state = this._selectionState; + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) + : false; + }, + + setSelected: function(selected, _point) { + var path = this._path, + selected = !!selected, + state = this._selectionState, + oldState = state, + flag = !_point ? 7 + : _point === this._point ? 4 + : _point === this._handleIn ? 1 + : _point === this._handleOut ? 2 + : 0; + if (selected) { + state |= flag; + } else { + state &= ~flag; + } + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); + path._changed(129); + } + }, + + getIndex: function() { + return this._index !== undefined ? this._index : null; + }, + + getPath: function() { + return this._path || null; + }, + + getCurve: function() { + var path = this._path, + index = this._index; + if (path) { + if (index > 0 && !path._closed + && index === path._segments.length - 1) + index--; + return path.getCurves()[index] || null; + } + return null; + }, + + getLocation: function() { + var curve = this.getCurve(); + return curve + ? new CurveLocation(curve, this === curve._segment1 ? 0 : 1) + : null; + }, + + getNext: function() { + var segments = this._path && this._path._segments; + return segments && (segments[this._index + 1] + || this._path._closed && segments[0]) || null; + }, + + getPrevious: function() { + var segments = this._path && this._path._segments; + return segments && (segments[this._index - 1] + || this._path._closed && segments[segments.length - 1]) || null; + }, + + isFirst: function() { + return this._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._index === path._segments.length - 1 || false; + }, + + reverse: function() { + var handleIn = this._handleIn, + handleOut = this._handleOut, + inX = handleIn._x, + inY = handleIn._y; + handleIn.set(handleOut._x, handleOut._y); + handleOut.set(inX, inY); + }, + + reversed: function() { + return new Segment(this._point, this._handleOut, this._handleIn); + }, + + remove: function() { + return this._path ? !!this._path.removeSegment(this._index) : false; + }, + + clone: function() { + return new Segment(this._point, this._handleIn, this._handleOut); + }, + + equals: function(segment) { + return segment === this || segment && this._class === segment._class + && this._point.equals(segment._point) + && this._handleIn.equals(segment._handleIn) + && this._handleOut.equals(segment._handleOut) + || false; + }, + + toString: function() { + var parts = [ 'point: ' + this._point ]; + if (!this._handleIn.isZero()) + parts.push('handleIn: ' + this._handleIn); + if (!this._handleOut.isZero()) + parts.push('handleOut: ' + this._handleOut); + return '{ ' + parts.join(', ') + ' }'; + }, + + transform: function(matrix) { + this._transformCoordinates(matrix, new Array(6), true); + this._changed(); + }, + + _transformCoordinates: function(matrix, coords, change) { + var point = this._point, + handleIn = !change || !this._handleIn.isZero() + ? this._handleIn : null, + handleOut = !change || !this._handleOut.isZero() + ? this._handleOut : null, + x = point._x, + y = point._y, + i = 2; + coords[0] = x; + coords[1] = y; + if (handleIn) { + coords[i++] = handleIn._x + x; + coords[i++] = handleIn._y + y; + } + if (handleOut) { + coords[i++] = handleOut._x + x; + coords[i++] = handleOut._y + y; + } + if (matrix) { + matrix._transformCoordinates(coords, coords, i / 2); + x = coords[0]; + y = coords[1]; + if (change) { + point._x = x; + point._y = y; + i = 2; + if (handleIn) { + handleIn._x = coords[i++] - x; + handleIn._y = coords[i++] - y; + } + if (handleOut) { + handleOut._x = coords[i++] - x; + handleOut._y = coords[i++] - y; + } + } else { + if (!handleIn) { + coords[i++] = x; + coords[i++] = y; + } + if (!handleOut) { + coords[i++] = x; + coords[i++] = y; + } + } + } + return coords; + } +}); + +var SegmentPoint = Point.extend({ + initialize: function SegmentPoint(point, owner, key) { + var x, y, selected; + if (!point) { + x = y = 0; + } else if ((x = point[0]) !== undefined) { + y = point[1]; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; + } + y = pt.y; + selected = pt.selected; + } + this._x = x; + this._y = y; + this._owner = owner; + owner[key] = this; + if (selected) + this.setSelected(true); + }, + + set: function(x, y) { + this._x = x; + this._y = y; + this._owner._changed(this); + return this; + }, + + _serialize: function(options) { + var f = options.formatter, + x = f.number(this._x), + y = f.number(this._y); + return this.isSelected() + ? { x: x, y: y, selected: true } + : [x, y]; + }, + + getX: function() { + return this._x; + }, + + setX: function(x) { + this._x = x; + this._owner._changed(this); + }, + + getY: function() { + return this._y; + }, + + setY: function(y) { + this._y = y; + this._owner._changed(this); + }, + + isZero: function() { + return Numerical.isZero(this._x) && Numerical.isZero(this._y); + }, + + setSelected: function(selected) { + this._owner.setSelected(selected, this); + }, + + isSelected: function() { + return this._owner.isSelected(this); + } +}); + +var Curve = Base.extend({ + _class: 'Curve', + + initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { + var count = arguments.length, + seg1, seg2, + point1, point2, + handle1, handle2; + if (count === 3) { + this._path = arg0; + seg1 = arg1; + seg2 = arg2; + } else if (count === 0) { + seg1 = new Segment(); + seg2 = new Segment(); + } else if (count === 1) { + if ('segment1' in arg0) { + seg1 = new Segment(arg0.segment1); + seg2 = new Segment(arg0.segment2); + } else if ('point1' in arg0) { + point1 = arg0.point1; + handle1 = arg0.handle1; + handle2 = arg0.handle2; + point2 = arg0.point2; + } else if (Array.isArray(arg0)) { + point1 = [arg0[0], arg0[1]]; + point2 = [arg0[6], arg0[7]]; + handle1 = [arg0[2] - arg0[0], arg0[3] - arg0[1]]; + handle2 = [arg0[4] - arg0[6], arg0[5] - arg0[7]]; + } + } else if (count === 2) { + seg1 = new Segment(arg0); + seg2 = new Segment(arg1); + } else if (count === 4) { + point1 = arg0; + handle1 = arg1; + handle2 = arg2; + point2 = arg3; + } else if (count === 8) { + point1 = [arg0, arg1]; + point2 = [arg6, arg7]; + handle1 = [arg2 - arg0, arg3 - arg1]; + handle2 = [arg4 - arg6, arg5 - arg7]; + } + this._segment1 = seg1 || new Segment(point1, null, handle1); + this._segment2 = seg2 || new Segment(point2, handle2, null); + }, + + _serialize: function(options) { + return Base.serialize(this.hasHandles() + ? [this.getPoint1(), this.getHandle1(), this.getHandle2(), + this.getPoint2()] + : [this.getPoint1(), this.getPoint2()], + options, true); + }, + + _changed: function() { + this._length = this._bounds = undefined; + }, + + clone: function() { + return new Curve(this._segment1, this._segment2); + }, + + toString: function() { + var parts = [ 'point1: ' + this._segment1._point ]; + if (!this._segment1._handleOut.isZero()) + parts.push('handle1: ' + this._segment1._handleOut); + if (!this._segment2._handleIn.isZero()) + parts.push('handle2: ' + this._segment2._handleIn); + parts.push('point2: ' + this._segment2._point); + return '{ ' + parts.join(', ') + ' }'; + }, + + remove: function() { + var removed = false; + if (this._path) { + var segment2 = this._segment2, + handleOut = segment2._handleOut; + removed = segment2.remove(); + if (removed) + this._segment1._handleOut.set(handleOut.x, handleOut.y); + } + return removed; + }, + + getPoint1: function() { + return this._segment1._point; + }, + + setPoint1: function() { + var point = Point.read(arguments); + this._segment1._point.set(point.x, point.y); + }, + + getPoint2: function() { + return this._segment2._point; + }, + + setPoint2: function() { + var point = Point.read(arguments); + this._segment2._point.set(point.x, point.y); + }, + + getHandle1: function() { + return this._segment1._handleOut; + }, + + setHandle1: function() { + var point = Point.read(arguments); + this._segment1._handleOut.set(point.x, point.y); + }, + + getHandle2: function() { + return this._segment2._handleIn; + }, + + setHandle2: function() { + var point = Point.read(arguments); + this._segment2._handleIn.set(point.x, point.y); + }, + + getSegment1: function() { + return this._segment1; + }, + + getSegment2: function() { + return this._segment2; + }, + + getPath: function() { + return this._path; + }, + + getIndex: function() { + return this._segment1._index; + }, + + getNext: function() { + var curves = this._path && this._path._curves; + return curves && (curves[this._segment1._index + 1] + || this._path._closed && curves[0]) || null; + }, + + getPrevious: function() { + var curves = this._path && this._path._curves; + return curves && (curves[this._segment1._index - 1] + || this._path._closed && curves[curves.length - 1]) || null; + }, + + isFirst: function() { + return this._segment1._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._segment1._index === path._curves.length - 1 + || false; + }, + + isSelected: function() { + return this.getPoint1().isSelected() + && this.getHandle2().isSelected() + && this.getHandle2().isSelected() + && this.getPoint2().isSelected(); + }, + + setSelected: function(selected) { + this.getPoint1().setSelected(selected); + this.getHandle1().setSelected(selected); + this.getHandle2().setSelected(selected); + this.getPoint2().setSelected(selected); + }, + + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); + }, + + getPoints: function() { + var coords = this.getValues(), + points = []; + for (var i = 0; i < 8; i += 2) + points.push(new Point(coords[i], coords[i + 1])); + return points; + }, + + getLength: function() { + if (this._length == null) + this._length = Curve.getLength(this.getValues(), 0, 1); + return this._length; + }, + + getArea: function() { + return Curve.getArea(this.getValues()); + }, + + getLine: function() { + return new Line(this._segment1._point, this._segment2._point); + }, + + getPart: function(from, to) { + return new Curve(Curve.getPart(this.getValues(), from, to)); + }, + + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + + getIntersections: function(curve) { + return Curve._getIntersections(this.getValues(), + curve && curve !== this ? curve.getValues() : null, + this, curve, [], {}); + }, + + _getParameter: function(offset, isParameter) { + return isParameter + ? offset + : offset && offset.curve === this + ? offset.parameter + : offset === undefined && isParameter === undefined + ? 0.5 + : this.getParameterAt(offset, 0); + }, + + divide: function(offset, isParameter, _setHandles) { + var parameter = this._getParameter(offset, isParameter), + tMin = 4e-7, + tMax = 1 - tMin, + res = null; + if (parameter >= tMin && parameter <= tMax) { + var parts = Curve.subdivide(this.getValues(), parameter), + left = parts[0], + right = parts[1], + setHandles = _setHandles || this.hasHandles(), + segment1 = this._segment1, + segment2 = this._segment2, + path = this._path; + if (setHandles) { + segment1._handleOut.set(left[2] - left[0], + left[3] - left[1]); + segment2._handleIn.set(right[4] - right[6], + right[5] - right[7]); + } + var x = left[6], y = left[7], + segment = new Segment(new Point(x, y), + setHandles && new Point(left[4] - x, left[5] - y), + setHandles && new Point(right[2] - x, right[3] - y)); + if (path) { + path.insert(segment1._index + 1, segment); + res = this.getNext(); + } else { + this._segment2 = segment; + res = new Curve(segment, segment2); + } + } + return res; + }, + + split: function(offset, isParameter) { + return this._path + ? this._path.split(this._segment1._index, + this._getParameter(offset, isParameter)) + : null; + }, + + reversed: function() { + return new Curve(this._segment2.reversed(), this._segment1.reversed()); + }, + + clearHandles: function() { + this._segment1._handleOut.set(0, 0); + this._segment2._handleIn.set(0, 0); + }, + +statics: { + getValues: function(segment1, segment2, matrix) { + var p1 = segment1._point, + h1 = segment1._handleOut, + h2 = segment2._handleIn, + p2 = segment2._point, + values = [ + p1._x, p1._y, + p1._x + h1._x, p1._y + h1._y, + p2._x + h2._x, p2._y + h2._y, + p2._x, p2._y + ]; + if (matrix) + matrix._transformCoordinates(values, values, 4); + return values; + }, + + subdivide: function(v, t) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7]; + if (t === undefined) + t = 0.5; + var u = 1 - t, + p3x = u * p1x + t * c1x, p3y = u * p1y + t * c1y, + p4x = u * c1x + t * c2x, p4y = u * c1y + t * c2y, + p5x = u * c2x + t * p2x, p5y = u * c2y + t * p2y, + p6x = u * p3x + t * p4x, p6y = u * p3y + t * p4y, + p7x = u * p4x + t * p5x, p7y = u * p4y + t * p5y, + p8x = u * p6x + t * p7x, p8y = u * p6y + t * p7y; + return [ + [p1x, p1y, p3x, p3y, p6x, p6y, p8x, p8y], + [p8x, p8y, p7x, p7y, p5x, p5y, p2x, p2y] + ]; + }, + + solveCubic: function (v, coord, val, roots, min, max) { + var p1 = v[coord], + c1 = v[coord + 2], + c2 = v[coord + 4], + p2 = v[coord + 6], + c = 3 * (c1 - p1), + b = 3 * (c2 - c1) - c, + a = p2 - p1 - c - b; + return Numerical.solveCubic(a, b, c, p1 - val, roots, min, max); + }, + + getParameterOf: function(v, point) { + var p1 = new Point(v[0], v[1]), + p2 = new Point(v[6], v[7]), + epsilon = 1e-12, + t = point.isClose(p1, epsilon) ? 0 + : point.isClose(p2, epsilon) ? 1 + : null; + if (t !== null) + return t; + var coords = [point.x, point.y], + roots = [], + geomEpsilon = 2e-7; + for (var c = 0; c < 2; c++) { + var count = Curve.solveCubic(v, c, coords[c], roots, 0, 1); + for (var i = 0; i < count; i++) { + t = roots[i]; + if (point.isClose(Curve.getPoint(v, t), geomEpsilon)) + return t; + } + } + return point.isClose(p1, geomEpsilon) ? 0 + : point.isClose(p2, geomEpsilon) ? 1 + : null; + }, + + getNearestParameter: function(v, point) { + if (Curve.isStraight(v)) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + vx = p2x - p1x, vy = p2y - p1y, + det = vx * vx + vy * vy; + if (det === 0) + return 0; + var u = ((point.x - p1x) * vx + (point.y - p1y) * vy) / det; + return u < 1e-12 ? 0 + : u > 0.999999999999 ? 1 + : Curve.getParameterOf(v, + new Point(p1x + u * vx, p1y + u * vy)); + } + + var count = 100, + minDist = Infinity, + minT = 0; + + function refine(t) { + if (t >= 0 && t <= 1) { + var dist = point.getDistance(Curve.getPoint(v, t), true); + if (dist < minDist) { + minDist = dist; + minT = t; + return true; + } + } + } + + for (var i = 0; i <= count; i++) + refine(i / count); + + var step = 1 / (count * 2); + while (step > 4e-7) { + if (!refine(minT - step) && !refine(minT + step)) + step /= 2; + } + return minT; + }, + + getPart: function(v, from, to) { + var flip = from > to; + if (flip) { + var tmp = from; + from = to; + to = tmp; + } + if (from > 0) + v = Curve.subdivide(v, from)[1]; + if (to < 1) + v = Curve.subdivide(v, (to - from) / (1 - from))[0]; + return flip + ? [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]] + : v; + }, + + hasHandles: function(v) { + var isZero = Numerical.isZero; + return !(isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + && isZero(v[4] - v[6]) && isZero(v[5] - v[7])); + }, + + isFlatEnough: function(v, tolerance) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + ux = 3 * c1x - 2 * p1x - p2x, + uy = 3 * c1y - 2 * p1y - p2y, + vx = 3 * c2x - 2 * p2x - p1x, + vy = 3 * c2y - 2 * p2y - p1y; + return Math.max(ux * ux, vx * vx) + Math.max(uy * uy, vy * vy) + < 10 * tolerance * tolerance; + }, + + getArea: function(v) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + h1x = (v[2] + p1x) / 2, + h1y = (v[3] + p1y) / 2, + h2x = (v[4] + v[6]) / 2, + h2y = (v[5] + v[7]) / 2; + return 6 * ((p1x - h1x) * (h1y + p1y) + + (h1x - h2x) * (h2y + h1y) + + (h2x - p2x) * (p2y + h2y)) / 10; + }, + + getBounds: function(v) { + var min = v.slice(0, 2), + max = min.slice(), + roots = [0, 0]; + for (var i = 0; i < 2; i++) + Curve._addBounds(v[i], v[i + 2], v[i + 4], v[i + 6], + i, 0, min, max, roots); + return new Rectangle(min[0], min[1], max[0] - min[0], max[1] - min[1]); + }, + + _addBounds: function(v0, v1, v2, v3, coord, padding, min, max, roots) { + function add(value, padding) { + var left = value - padding, + right = value + padding; + if (left < min[coord]) + min[coord] = left; + if (right > max[coord]) + max[coord] = right; + } + var a = 3 * (v1 - v2) - v0 + v3, + b = 2 * (v0 + v2) - 4 * v1, + c = v1 - v0, + count = Numerical.solveQuadratic(a, b, c, roots), + tMin = 4e-7, + tMax = 1 - tMin; + add(v3, 0); + for (var i = 0; i < count; i++) { + var t = roots[i], + u = 1 - t; + if (tMin < t && t < tMax) + add(u * u * u * v0 + + 3 * u * u * t * v1 + + 3 * u * t * t * v2 + + t * t * t * v3, + padding); + } + } +}}, Base.each( + ['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], + function(name) { + this[name] = function() { + if (!this._bounds) + this._bounds = {}; + var bounds = this._bounds[name]; + if (!bounds) { + var path = this._path; + bounds = this._bounds[name] = Path[name]( + [this._segment1, this._segment2], false, + path && path.getStyle()); + } + return bounds.clone(); + }; + }, +{ + +}), Base.each({ + isStraight: function(l, h1, h2) { + if (h1.isZero() && h2.isZero()) { + return true; + } else if (l.isZero()) { + return false; + } else if (h1.isCollinear(l) && h2.isCollinear(l)) { + var div = l.dot(l), + p1 = l.dot(h1) / div, + p2 = l.dot(h2) / div; + return p1 >= 0 && p1 <= 1 && p2 <= 0 && p2 >= -1; + } + return false; + }, + + isLinear: function(l, h1, h2) { + var third = l.divide(3); + return h1.equals(third) && h2.negate().equals(third); + } +}, function(test, name) { + this[name] = function() { + var seg1 = this._segment1, + seg2 = this._segment2; + return test(seg2._point.subtract(seg1._point), + seg1._handleOut, seg2._handleIn); + }; + + this.statics[name] = function(v) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7]; + return test(new Point(p2x - p1x, p2y - p1y), + new Point(v[2] - p1x, v[3] - p1y), + new Point(v[4] - p2x, v[5] - p2y)); + }; +}, { + statics: {}, + + hasHandles: function() { + return !this._segment1._handleOut.isZero() + || !this._segment2._handleIn.isZero(); + }, + + isCollinear: function(curve) { + return curve && this.isStraight() && curve.isStraight() + && this.getLine().isCollinear(curve.getLine()); + }, + + isHorizontal: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).y) + < 1e-7; + }, + + isVertical: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).x) + < 1e-7; + } +}), { + beans: false, + + getParameterAt: function(offset, start) { + return Curve.getParameterAt(this.getValues(), offset, start); + }, + + getParameterOf: function() { + return Curve.getParameterOf(this.getValues(), Point.read(arguments)); + }, + + getLocationAt: function(offset, isParameter) { + var t = isParameter ? offset : this.getParameterAt(offset); + return t != null && t >= 0 && t <= 1 + ? new CurveLocation(this, t) + : null; + }, + + getLocationOf: function() { + return this.getLocationAt(this.getParameterOf(Point.read(arguments)), + true); + }, + + getOffsetOf: function() { + var loc = this.getLocationOf.apply(this, arguments); + return loc ? loc.getOffset() : null; + }, + + getNearestLocation: function() { + var point = Point.read(arguments), + values = this.getValues(), + t = Curve.getNearestParameter(values, point), + pt = Curve.getPoint(values, t); + return new CurveLocation(this, t, pt, null, point.getDistance(pt)); + }, + + getNearestPoint: function() { + return this.getNearestLocation.apply(this, arguments).getPoint(); + } + +}, +new function() { + var methods = ['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', + 'getWeightedNormal', 'getCurvature']; + return Base.each(methods, + function(name) { + this[name + 'At'] = function(offset, isParameter) { + var values = this.getValues(); + return Curve[name](values, isParameter ? offset + : Curve.getParameterAt(values, offset, 0)); + }; + }, { + statics: { + evaluateMethods: methods + } + }) +}, +new function() { + + function getLengthIntegrand(v) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + + ax = 9 * (c1x - c2x) + 3 * (p2x - p1x), + bx = 6 * (p1x + c2x) - 12 * c1x, + cx = 3 * (c1x - p1x), + + ay = 9 * (c1y - c2y) + 3 * (p2y - p1y), + by = 6 * (p1y + c2y) - 12 * c1y, + cy = 3 * (c1y - p1y); + + return function(t) { + var dx = (ax * t + bx) * t + cx, + dy = (ay * t + by) * t + cy; + return Math.sqrt(dx * dx + dy * dy); + }; + } + + function getIterations(a, b) { + return Math.max(2, Math.min(16, Math.ceil(Math.abs(b - a) * 32))); + } + + function evaluate(v, t, type, normalized) { + if (t == null || t < 0 || t > 1) + return null; + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + tMin = 4e-7, + tMax = 1 - tMin, + x, y; + + if (type === 0 && (t < tMin || t > tMax)) { + var isZero = t < tMin; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; + } else { + var cx = 3 * (c1x - p1x), + bx = 3 * (c2x - c1x) - cx, + ax = p2x - p1x - cx - bx, + + cy = 3 * (c1y - p1y), + by = 3 * (c2y - c1y) - cy, + ay = p2y - p1y - cy - by; + if (type === 0) { + x = ((ax * t + bx) * t + cx) * t + p1x; + y = ((ay * t + by) * t + cy) * t + p1y; + } else { + if (t < tMin) { + x = cx; + y = cy; + } else if (t > tMax) { + x = 3 * (p2x - c2x); + y = 3 * (p2y - c2y); + } else { + x = (3 * ax * t + 2 * bx) * t + cx; + y = (3 * ay * t + 2 * by) * t + cy; + } + if (normalized) { + if (x === 0 && y === 0 && (t < tMin || t > tMax)) { + x = c2x - c1x; + y = c2y - c1y; + } + var len = Math.sqrt(x * x + y * y); + if (len) { + x /= len; + y /= len; + } + } + if (type === 3) { + var x2 = 6 * ax * t + 2 * bx, + y2 = 6 * ay * t + 2 * by, + d = Math.pow(x * x + y * y, 3 / 2); + x = d !== 0 ? (x * y2 - y * x2) / d : 0; + y = 0; + } + } + } + return type === 2 ? new Point(y, -x) : new Point(x, y); + } + + return { statics: { + + getLength: function(v, a, b) { + if (a === undefined) + a = 0; + if (b === undefined) + b = 1; + if (a === 0 && b === 1 && Curve.isStraight(v)) { + var dx = v[6] - v[0], + dy = v[7] - v[1]; + return Math.sqrt(dx * dx + dy * dy); + } + var ds = getLengthIntegrand(v); + return Numerical.integrate(ds, a, b, getIterations(a, b)); + }, + + getParameterAt: function(v, offset, start) { + if (start === undefined) + start = offset < 0 ? 1 : 0 + if (offset === 0) + return start; + var abs = Math.abs, + forward = offset > 0, + a = forward ? start : 0, + b = forward ? 1 : start, + ds = getLengthIntegrand(v), + rangeLength = Numerical.integrate(ds, a, b, + getIterations(a, b)); + if (abs(offset - rangeLength) < 1e-12) { + return forward ? b : a; + } else if (abs(offset) > rangeLength) { + return null; + } + var guess = offset / rangeLength, + length = 0; + function f(t) { + length += Numerical.integrate(ds, start, t, + getIterations(start, t)); + start = t; + return length - offset; + } + return Numerical.findRoot(f, ds, start + guess, a, b, 32, + 1e-12); + }, + + getPoint: function(v, t) { + return evaluate(v, t, 0, false); + }, + + getTangent: function(v, t) { + return evaluate(v, t, 1, true); + }, + + getWeightedTangent: function(v, t) { + return evaluate(v, t, 1, false); + }, + + getNormal: function(v, t) { + return evaluate(v, t, 2, true); + }, + + getWeightedNormal: function(v, t) { + return evaluate(v, t, 2, false); + }, + + getCurvature: function(v, t) { + return evaluate(v, t, 3, false).x; + } + }}; +}, +new function() { + + function addLocation(locations, param, v1, c1, t1, p1, v2, c2, t2, p2, + overlap) { + var startConnected = param.startConnected, + endConnected = param.endConnected, + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 == null) + t1 = Curve.getParameterOf(v1, p1); + if (t1 !== null && t1 >= (startConnected ? tMin : 0) && + t1 <= (endConnected ? tMax : 1)) { + if (t2 == null) + t2 = Curve.getParameterOf(v2, p2); + if (t2 !== null && t2 >= (endConnected ? tMin : 0) && + t2 <= (startConnected ? tMax : 1)) { + var renormalize = param.renormalize; + if (renormalize) { + var res = renormalize(t1, t2); + t1 = res[0]; + t2 = res[1]; + } + var loc1 = new CurveLocation(c1, t1, + p1 || Curve.getPoint(v1, t1), overlap), + loc2 = new CurveLocation(c2, t2, + p2 || Curve.getPoint(v2, t2), overlap), + flip = loc1.getPath() === loc2.getPath() + && loc1.getIndex() > loc2.getIndex(), + loc = flip ? loc2 : loc1, + include = param.include; + loc1._intersection = loc2; + loc2._intersection = loc1; + if (!include || include(loc)) { + CurveLocation.insert(locations, loc, true); + } + } + } + } + + function addCurveIntersections(v1, v2, c1, c2, locations, param, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { + if (++recursion >= 24) + return; + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + getSignedDistance = Line.getSignedDistance, + d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]), + d2 = getSignedDistance(q0x, q0y, q3x, q3y, v2[4], v2[5]), + factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, + dMin = factor * Math.min(0, d1, d2), + dMax = factor * Math.max(0, d1, d2), + dp0 = getSignedDistance(q0x, q0y, q3x, q3y, v1[0], v1[1]), + dp1 = getSignedDistance(q0x, q0y, q3x, q3y, v1[2], v1[3]), + dp2 = getSignedDistance(q0x, q0y, q3x, q3y, v1[4], v1[5]), + dp3 = getSignedDistance(q0x, q0y, q3x, q3y, v1[6], v1[7]), + hull = getConvexHull(dp0, dp1, dp2, dp3), + top = hull[0], + bottom = hull[1], + tMinClip, + tMaxClip; + if ((tMinClip = clipConvexHull(top, bottom, dMin, dMax)) == null || + (tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(), + dMin, dMax)) == null) + return; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + var tDiff = tMaxClip - tMinClip, + tMinNew = tMin + (tMax - tMin) * tMinClip, + tMaxNew = tMin + (tMax - tMin) * tMaxClip; + if (oldTDiff > 0.5 && tDiff > 0.5) { + if (tMaxNew - tMinNew > uMax - uMin) { + var parts = Curve.subdivide(v1, 0.5), + t = tMinNew + (tMaxNew - tMinNew) / 2; + addCurveIntersections( + v2, parts[0], c2, c1, locations, param, + uMin, uMax, tMinNew, t, tDiff, !reverse, recursion); + addCurveIntersections( + v2, parts[1], c2, c1, locations, param, + uMin, uMax, t, tMaxNew, tDiff, !reverse, recursion); + } else { + var parts = Curve.subdivide(v2, 0.5), + t = uMin + (uMax - uMin) / 2; + addCurveIntersections( + parts[0], v1, c2, c1, locations, param, + uMin, t, tMinNew, tMaxNew, tDiff, !reverse, recursion); + addCurveIntersections( + parts[1], v1, c2, c1, locations, param, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); + } + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) + < 1e-7) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + v1 = c1.getValues(); + v2 = c2.getValues(); + addLocation(locations, param, + reverse ? v2 : v1, reverse ? c2 : c1, reverse ? t2 : t1, null, + reverse ? v1 : v2, reverse ? c1 : c2, reverse ? t1 : t2, null); + } else if (tDiff > 1e-12) { + addCurveIntersections(v2, v1, c2, c1, locations, param, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); + } + } + + function getConvexHull(dq0, dq1, dq2, dq3) { + var p0 = [ 0, dq0 ], + p1 = [ 1 / 3, dq1 ], + p2 = [ 2 / 3, dq2 ], + p3 = [ 1, dq3 ], + dist1 = dq1 - (2 * dq0 + dq3) / 3, + dist2 = dq2 - (dq0 + 2 * dq3) / 3, + hull; + if (dist1 * dist2 < 0) { + hull = [[p0, p1, p3], [p0, p2, p3]]; + } else { + var distRatio = dist1 / dist2; + hull = [ + distRatio >= 2 ? [p0, p1, p3] + : distRatio <= .5 ? [p0, p2, p3] + : [p0, p1, p2, p3], + [p0, p3] + ]; + } + return (dist1 || dist2) < 0 ? hull.reverse() : hull; + } + + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + if (hullTop[0][1] < dMin) { + return clipConvexHullPart(hullTop, true, dMin); + } else if (hullBottom[0][1] > dMax) { + return clipConvexHullPart(hullBottom, false, dMax); + } else { + return hullTop[0][0]; + } + } + + function clipConvexHullPart(part, top, threshold) { + var px = part[0][0], + py = part[0][1]; + for (var i = 1, l = part.length; i < l; i++) { + var qx = part[i][0], + qy = part[i][1]; + if (top ? qy >= threshold : qy <= threshold) { + return qy === threshold ? qx + : px + (threshold - py) * (qx - px) / (qy - py); + } + px = qx; + py = qy; + } + return null; + } + + function addCurveLineIntersections(v1, v2, c1, c2, locations, param) { + var flip = Curve.isStraight(v1), + vc = flip ? v2 : v1, + vl = flip ? v1 : v2, + lx1 = vl[0], ly1 = vl[1], + lx2 = vl[6], ly2 = vl[7], + ldx = lx2 - lx1, + ldy = ly2 - ly1, + angle = Math.atan2(-ldy, ldx), + sin = Math.sin(angle), + cos = Math.cos(angle), + rvc = []; + for(var i = 0; i < 8; i += 2) { + var x = vc[i] - lx1, + y = vc[i + 1] - ly1; + rvc.push( + x * cos - y * sin, + x * sin + y * cos); + } + var roots = [], + count = Curve.solveCubic(rvc, 1, 0, roots, 0, 1); + for (var i = 0; i < count; i++) { + var tc = roots[i], + pc = Curve.getPoint(vc, tc), + tl = Curve.getParameterOf(vl, pc); + if (tl !== null) { + var pl = Curve.getPoint(vl, tl), + t1 = flip ? tl : tc, + t2 = flip ? tc : tl; + if (!param.endConnected || t2 > Numerical.CURVETIME_EPSILON) { + addLocation(locations, param, + v1, c1, t1, flip ? pl : pc, + v2, c2, t2, flip ? pc : pl); + } + } + } + } + + function addLineIntersection(v1, v2, c1, c2, locations, param) { + var pt = Line.intersect( + v1[0], v1[1], v1[6], v1[7], + v2[0], v2[1], v2[6], v2[7]); + if (pt) { + addLocation(locations, param, v1, c1, null, pt, v2, c2, null, pt); + } + } + + return { statics: { + _getIntersections: function(v1, v2, c1, c2, locations, param) { + if (!v2) { + return Curve._getSelfIntersection(v1, c1, locations, param); + } + var c1p1x = v1[0], c1p1y = v1[1], + c1p2x = v1[6], c1p2y = v1[7], + c2p1x = v2[0], c2p1y = v2[1], + c2p2x = v2[6], c2p2y = v2[7], + c1s1x = (3 * v1[2] + c1p1x) / 4, + c1s1y = (3 * v1[3] + c1p1y) / 4, + c1s2x = (3 * v1[4] + c1p2x) / 4, + c1s2y = (3 * v1[5] + c1p2y) / 4, + c2s1x = (3 * v2[2] + c2p1x) / 4, + c2s1y = (3 * v2[3] + c2p1y) / 4, + c2s2x = (3 * v2[4] + c2p2x) / 4, + c2s2y = (3 * v2[5] + c2p2y) / 4, + min = Math.min, + max = Math.max; + if (!( max(c1p1x, c1s1x, c1s2x, c1p2x) >= + min(c2p1x, c2s1x, c2s2x, c2p2x) && + min(c1p1x, c1s1x, c1s2x, c1p2x) <= + max(c2p1x, c2s1x, c2s2x, c2p2x) && + max(c1p1y, c1s1y, c1s2y, c1p2y) >= + min(c2p1y, c2s1y, c2s2y, c2p2y) && + min(c1p1y, c1s1y, c1s2y, c1p2y) <= + max(c2p1y, c2s1y, c2s2y, c2p2y))) + return locations; + if (!param.startConnected && !param.endConnected) { + var overlaps = Curve.getOverlaps(v1, v2); + if (overlaps) { + for (var i = 0; i < 2; i++) { + var overlap = overlaps[i]; + addLocation(locations, param, + v1, c1, overlap[0], null, + v2, c2, overlap[1], null, true); + } + return locations; + } + } + + var straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2, + epsilon = 1e-12, + before = locations.length; + (straight + ? addLineIntersection + : straight1 || straight2 + ? addCurveLineIntersections + : addCurveIntersections)( + v1, v2, c1, c2, locations, param, + 0, 1, 0, 1, 0, false, 0); + if (straight && locations.length > before) + return locations; + var c1p1 = new Point(c1p1x, c1p1y), + c1p2 = new Point(c1p2x, c1p2y), + c2p1 = new Point(c2p1x, c2p1y), + c2p2 = new Point(c2p2x, c2p2y); + if (c1p1.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 0, c2p1); + if (!param.startConnected && c1p1.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 1, c2p2); + if (!param.endConnected && c1p2.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 0, c2p1); + if (c1p2.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 1, c2p2); + return locations; + }, + + _getSelfIntersection: function(v1, c1, locations, param) { + var p1x = v1[0], p1y = v1[1], + h1x = v1[2], h1y = v1[3], + h2x = v1[4], h2y = v1[5], + p2x = v1[6], p2y = v1[7]; + var line = new Line(p1x, p1y, p2x, p2y, false), + side1 = line.getSide(new Point(h1x, h1y), true), + side2 = line.getSide(new Point(h2x, h2y), true); + if (side1 === side2) { + var edgeSum = (p1x - h2x) * (h1y - p2y) + + (h1x - p2x) * (h2y - p1y); + if (edgeSum * side1 > 0) + return locations; + } + var ax = p2x - 3 * h2x + 3 * h1x - p1x, + bx = h2x - 2 * h1x + p1x, + cx = h1x - p1x, + ay = p2y - 3 * h2y + 3 * h1y - p1y, + by = h2y - 2 * h1y + p1y, + cy = h1y - p1y, + ac = ay * cx - ax * cy, + ab = ay * bx - ax * by, + bc = by * cx - bx * cy; + if (ac * ac - 4 * ab * bc < 0) { + var roots = [], + tSplit, + count = Numerical.solveCubic( + ax * ax + ay * ay, + 3 * (ax * bx + ay * by), + 2 * (bx * bx + by * by) + ax * cx + ay * cy, + bx * cx + by * cy, + roots, 0, 1); + if (count > 0) { + for (var i = 0, maxCurvature = 0; i < count; i++) { + var curvature = Math.abs( + c1.getCurvatureAt(roots[i], true)); + if (curvature > maxCurvature) { + maxCurvature = curvature; + tSplit = roots[i]; + } + } + var parts = Curve.subdivide(v1, tSplit); + param.endConnected = true; + param.renormalize = function(t1, t2) { + return [t1 * tSplit, t2 * (1 - tSplit) + tSplit]; + }; + Curve._getIntersections(parts[0], parts[1], c1, c1, + locations, param); + } + } + return locations; + }, + + getOverlaps: function(v1, v2) { + var abs = Math.abs, + timeEpsilon = 4e-7, + geomEpsilon = 2e-7, + straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2; + + function getLineLengthSquared(v) { + var x = v[6] - v[0], + y = v[7] - v[1]; + return x * x + y * y; + } + + if (straight) { + var flip = getLineLengthSquared(v1) < getLineLengthSquared(v2), + l1 = flip ? v2 : v1, + l2 = flip ? v1 : v2, + line = new Line(l1[0], l1[1], l1[6], l1[7]); + if (line.getDistance(new Point(l2[0], l2[1])) > geomEpsilon || + line.getDistance(new Point(l2[6], l2[7])) > geomEpsilon) + return null; + } else if (straight1 ^ straight2) { + return null; + } + + var v = [v1, v2], + pairs = []; + for (var i = 0, t1 = 0; + i < 2 && pairs.length < 2; + i += t1 === 0 ? 0 : 1, t1 = t1 ^ 1) { + var t2 = Curve.getParameterOf(v[i ^ 1], new Point( + v[i][t1 === 0 ? 0 : 6], + v[i][t1 === 0 ? 1 : 7])); + if (t2 != null) { + var pair = i === 0 ? [t1, t2] : [t2, t1]; + if (pairs.length === 0 || + abs(pair[0] - pairs[0][0]) > timeEpsilon && + abs(pair[1] - pairs[0][1]) > timeEpsilon) + pairs.push(pair); + } + if (i === 1 && pairs.length === 0) + break; + } + if (pairs.length !== 2) { + pairs = null; + } else if (!straight) { + var o1 = Curve.getPart(v1, pairs[0][0], pairs[1][0]), + o2 = Curve.getPart(v2, pairs[0][1], pairs[1][1]); + if (abs(o2[2] - o1[2]) > geomEpsilon || + abs(o2[3] - o1[3]) > geomEpsilon || + abs(o2[4] - o1[4]) > geomEpsilon || + abs(o2[5] - o1[5]) > geomEpsilon) + pairs = null; + } + return pairs; + } + }}; +}); + +var CurveLocation = Base.extend({ + _class: 'CurveLocation', + beans: true, + + initialize: function CurveLocation(curve, parameter, point, + _overlap, _distance) { + if (parameter > 0.9999996) { + var next = curve.getNext(); + if (next) { + parameter = 0; + curve = next; + } + } + this._id = UID.get(CurveLocation); + this._setCurve(curve); + this._parameter = parameter; + this._point = point || curve.getPointAt(parameter, true); + this._overlap = _overlap; + this._distance = _distance; + this._intersection = this._next = this._prev = null; + }, + + _setCurve: function(curve) { + var path = curve._path; + this._version = path ? path._version : 0; + this._curve = curve; + this._segment = null; + this._segment1 = curve._segment1; + this._segment2 = curve._segment2; + }, + + _setSegment: function(segment) { + this._setCurve(segment.getCurve()); + this._segment = segment; + this._parameter = segment === this._segment1 ? 0 : 1; + this._point = segment._point.clone(); + }, + + getSegment: function() { + var curve = this.getCurve(), + segment = this._segment; + if (!segment) { + var parameter = this.getParameter(); + if (parameter === 0) { + segment = curve._segment1; + } else if (parameter === 1) { + segment = curve._segment2; + } else if (parameter != null) { + segment = curve.getPartLength(0, parameter) + < curve.getPartLength(parameter, 1) + ? curve._segment1 + : curve._segment2; + } + this._segment = segment; + } + return segment; + }, + + getCurve: function() { + var curve = this._curve, + path = curve && curve._path, + that = this; + if (path && path._version !== this._version) { + curve = this._parameter = this._curve = this._offset = null; + } + + function trySegment(segment) { + var curve = segment && segment.getCurve(); + if (curve && (that._parameter = curve.getParameterOf(that._point)) + != null) { + that._setCurve(curve); + that._segment = segment; + return curve; + } + } + + return curve + || trySegment(this._segment) + || trySegment(this._segment1) + || trySegment(this._segment2.getPrevious()); + }, + + getPath: function() { + var curve = this.getCurve(); + return curve && curve._path; + }, + + getIndex: function() { + var curve = this.getCurve(); + return curve && curve.getIndex(); + }, + + getParameter: function() { + var curve = this.getCurve(), + parameter = this._parameter; + return curve && parameter == null + ? this._parameter = curve.getParameterOf(this._point) + : parameter; + }, + + getPoint: function() { + return this._point; + }, + + getOffset: function() { + var offset = this._offset; + if (offset == null) { + offset = 0; + var path = this.getPath(), + index = this.getIndex(); + if (path && index != null) { + var curves = path.getCurves(); + for (var i = 0; i < index; i++) + offset += curves[i].getLength(); + } + this._offset = offset += this.getCurveOffset(); + } + return offset; + }, + + getCurveOffset: function() { + var curve = this.getCurve(), + parameter = this.getParameter(); + return parameter != null && curve && curve.getPartLength(0, parameter); + }, + + getIntersection: function() { + return this._intersection; + }, + + getDistance: function() { + return this._distance; + }, + + divide: function() { + var curve = this.getCurve(), + res = null; + if (curve) { + res = curve.divide(this.getParameter(), true); + if (res) + this._setSegment(res._segment1); + } + return res; + }, + + split: function() { + var curve = this.getCurve(); + return curve ? curve.split(this.getParameter(), true) : null; + }, + + equals: function(loc, _ignoreOther) { + var res = this === loc, + epsilon = 2e-7; + if (!res && loc instanceof CurveLocation + && this.getPath() === loc.getPath() + && this.getPoint().isClose(loc.getPoint(), epsilon)) { + var c1 = this.getCurve(), + c2 = loc.getCurve(), + abs = Math.abs, + diff = abs( + ((c1.isLast() && c2.isFirst() ? -1 : c1.getIndex()) + + this.getParameter()) - + ((c2.isLast() && c1.isFirst() ? -1 : c2.getIndex()) + + loc.getParameter())); + res = (diff < 4e-7 + || ((diff = abs(this.getOffset() - loc.getOffset())) < epsilon + || abs(this.getPath().getLength() - diff) < epsilon)) + && (_ignoreOther + || (!this._intersection && !loc._intersection + || this._intersection && this._intersection.equals( + loc._intersection, true))); + } + return res; + }, + + toString: function() { + var parts = [], + point = this.getPoint(), + f = Formatter.instance; + if (point) + parts.push('point: ' + point); + var index = this.getIndex(); + if (index != null) + parts.push('index: ' + index); + var parameter = this.getParameter(); + if (parameter != null) + parts.push('parameter: ' + f.number(parameter)); + if (this._distance != null) + parts.push('distance: ' + f.number(this._distance)); + return '{ ' + parts.join(', ') + ' }'; + }, + + isTouching: function() { + var inter = this._intersection; + if (inter && this.getTangent().isCollinear(inter.getTangent())) { + var curve1 = this.getCurve(), + curve2 = inter.getCurve(); + return !(curve1.isStraight() && curve2.isStraight() + && curve1.getLine().intersect(curve2.getLine())); + } + return false; + }, + + isCrossing: function() { + var inter = this._intersection; + if (!inter) + return false; + var t1 = this.getParameter(), + t2 = inter.getParameter(), + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 >= tMin && t1 <= tMax || t2 >= tMin && t2 <= tMax) + return !this.isTouching(); + var c2 = this.getCurve(), + c1 = c2.getPrevious(), + c4 = inter.getCurve(), + c3 = c4.getPrevious(), + PI = Math.PI; + if (!c1 || !c3) + return false; + + function isInRange(angle, min, max) { + return min < max + ? angle > min && angle < max + : angle > min && angle <= PI || angle >= -PI && angle < max; + } + + var a1 = c1.getTangentAt(tMax, true).negate().getAngleInRadians(), + a2 = c2.getTangentAt(tMin, true).getAngleInRadians(), + a3 = c3.getTangentAt(tMax, true).negate().getAngleInRadians(), + a4 = c4.getTangentAt(tMin, true).getAngleInRadians(); + + return (isInRange(a3, a1, a2) ^ isInRange(a4, a1, a2)) + && (isInRange(a3, a2, a1) ^ isInRange(a4, a2, a1)); + }, + + isOverlap: function() { + return !!this._overlap; + } +}, Base.each(Curve.evaluateMethods, function(name) { + var get = name + 'At'; + this[name] = function() { + var parameter = this.getParameter(), + curve = this.getCurve(); + return parameter != null && curve && curve[get](parameter, true); + }; +}, { + preserve: true +}), +new function() { + + function insert(locations, loc, merge) { + var length = locations.length, + l = 0, + r = length - 1; + + function search(index, dir) { + for (var i = index + dir; i >= -1 && i <= length; i += dir) { + var loc2 = locations[((i % length) + length) % length]; + if (!loc.getPoint().isClose(loc2.getPoint(), + 2e-7)) + break; + if (loc.equals(loc2)) + return loc2; + } + return null; + } + + while (l <= r) { + var m = (l + r) >>> 1, + loc2 = locations[m], + found; + if (merge && (found = loc.equals(loc2) ? loc2 + : (search(m, -1) || search(m, 1)))) { + if (loc._overlap) { + found._overlap = found._intersection._overlap = true; + } + return found; + } + var path1 = loc.getPath(), + path2 = loc2.getPath(), + diff = path1 === path2 + ? (loc.getIndex() + loc.getParameter()) + - (loc2.getIndex() + loc2.getParameter()) + : path1._id - path2._id; + if (diff < 0) { + r = m - 1; + } else { + l = m + 1; + } + } + locations.splice(l, 0, loc); + return loc; + } + + return { statics: { + insert: insert, + + expand: function(locations) { + var expanded = locations.slice(); + for (var i = 0, l = locations.length; i < l; i++) { + insert(expanded, locations[i]._intersection, false); + } + return expanded; + } + }}; +}); + +var PathItem = Item.extend({ + _class: 'PathItem', + + initialize: function PathItem() { + }, + + getIntersections: function(path, include, _matrix, _returnFirst) { + var self = this === path || !path, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = self ? matrix1 + : (_matrix || path._matrix).orNullIfIdentity(); + if (!self && !this.getBounds(matrix1).touches(path.getBounds(matrix2))) + return []; + var curves1 = this.getCurves(), + curves2 = self ? curves1 : path.getCurves(), + length1 = curves1.length, + length2 = self ? length1 : curves2.length, + values2 = [], + arrays = [], + locations, + path; + for (var i = 0; i < length2; i++) + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { + var curve1 = curves1[i], + values1 = self ? values2[i] : curve1.getValues(matrix1), + path1 = curve1.getPath(); + if (path1 !== path) { + path = path1; + locations = []; + arrays.push(locations); + } + if (self) { + Curve._getSelfIntersection(values1, curve1, locations, { + include: include, + startConnected: length1 === 1 && + curve1.getPoint1().equals(curve1.getPoint2()) + }); + } + for (var j = self ? i + 1 : 0; j < length2; j++) { + if (_returnFirst && locations.length) + return locations; + var curve2 = curves2[j]; + Curve._getIntersections( + values1, values2[j], curve1, curve2, locations, + { + include: include, + startConnected: self && curve1.getPrevious() === curve2, + endConnected: self && curve1.getNext() === curve2 + } + ); + } + } + locations = []; + for (var i = 0, l = arrays.length; i < l; i++) { + locations.push.apply(locations, arrays[i]); + } + return locations; + }, + + getCrossings: function(path) { + return this.getIntersections(path, function(inter) { + return inter.isCrossing(); + }); + }, + + _asPathItem: function() { + return this; + }, + + setPathData: function(data) { + + var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), + coords, + relative = false, + previous, + control, + current = new Point(), + start = new Point(); + + function getCoord(index, coord) { + var val = +coords[index]; + if (relative) + val += current[coord]; + return val; + } + + function getPoint(index) { + return new Point( + getCoord(index, 'x'), + getCoord(index + 1, 'y') + ); + } + + this.clear(); + + for (var i = 0, l = parts && parts.length; i < l; i++) { + var part = parts[i], + command = part[0], + lower = command.toLowerCase(); + coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); + var length = coords && coords.length; + relative = command === lower; + if (previous === 'z' && !/[mz]/.test(lower)) + this.moveTo(current = start); + switch (lower) { + case 'm': + case 'l': + var move = lower === 'm'; + for (var j = 0; j < length; j += 2) + this[j === 0 && move ? 'moveTo' : 'lineTo']( + current = getPoint(j)); + control = current; + if (move) + start = current; + break; + case 'h': + case 'v': + var coord = lower === 'h' ? 'x' : 'y'; + for (var j = 0; j < length; j++) { + current[coord] = getCoord(j, coord); + this.lineTo(current); + } + control = current; + break; + case 'c': + for (var j = 0; j < length; j += 6) { + this.cubicCurveTo( + getPoint(j), + control = getPoint(j + 2), + current = getPoint(j + 4)); + } + break; + case 's': + for (var j = 0; j < length; j += 4) { + this.cubicCurveTo( + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, + control = getPoint(j), + current = getPoint(j + 2)); + previous = lower; + } + break; + case 'q': + for (var j = 0; j < length; j += 4) { + this.quadraticCurveTo( + control = getPoint(j), + current = getPoint(j + 2)); + } + break; + case 't': + for (var j = 0; j < length; j += 2) { + this.quadraticCurveTo( + control = (/[qt]/.test(previous) + ? current.multiply(2).subtract(control) + : current), + current = getPoint(j)); + previous = lower; + } + break; + case 'a': + for (var j = 0; j < length; j += 7) { + this.arcTo(current = getPoint(j + 5), + new Size(+coords[j], +coords[j + 1]), + +coords[j + 2], +coords[j + 4], +coords[j + 3]); + } + break; + case 'z': + this.closePath(true); + break; + } + previous = lower; + } + }, + + _canComposite: function() { + return !(this.hasFill() && this.hasStroke()); + }, + + _contains: function(point) { + var winding = this._getWinding(point, false, true); + return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); + } + +}); + +var Path = PathItem.extend({ + _class: 'Path', + _serializeFields: { + segments: [], + closed: false + }, + + initialize: function Path(arg) { + this._closed = false; + this._segments = []; + this._version = 0; + var segments = Array.isArray(arg) + ? typeof arg[0] === 'object' + ? arg + : arguments + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) + ? arguments + : null; + if (segments && segments.length > 0) { + this.setSegments(segments); + } else { + this._curves = undefined; + this._selectedSegmentState = 0; + if (!segments && typeof arg === 'string') { + this.setPathData(arg); + arg = null; + } + } + this._initialize(!segments && arg); + }, + + _equals: function(item) { + return this._closed === item._closed + && Base.equals(this._segments, item._segments); + }, + + clone: function(insert) { + var copy = new Path(Item.NO_INSERT); + copy.setSegments(this._segments); + copy._closed = this._closed; + if (this._clockwise !== undefined) + copy._clockwise = this._clockwise; + return this._clone(copy, insert); + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & 8) { + var parent = this._parent; + if (parent) + parent._currentPath = undefined; + this._length = this._area = this._clockwise = this._monoCurves = + undefined; + if (flags & 16) { + this._version++; + } else if (this._curves) { + for (var i = 0, l = this._curves.length; i < l; i++) + this._curves[i]._changed(); + } + } else if (flags & 32) { + this._bounds = undefined; + } + }, + + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + + getSegments: function() { + return this._segments; + }, + + setSegments: function(segments) { + var fullySelected = this.isFullySelected(); + this._segments.length = 0; + this._selectedSegmentState = 0; + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); + if (fullySelected) + this.setFullySelected(true); + }, + + getFirstSegment: function() { + return this._segments[0]; + }, + + getLastSegment: function() { + return this._segments[this._segments.length - 1]; + }, + + getCurves: function() { + var curves = this._curves, + segments = this._segments; + if (!curves) { + var length = this._countCurves(); + curves = this._curves = new Array(length); + for (var i = 0; i < length; i++) + curves[i] = new Curve(this, segments[i], + segments[i + 1] || segments[0]); + } + return curves; + }, + + getFirstCurve: function() { + return this.getCurves()[0]; + }, + + getLastCurve: function() { + var curves = this.getCurves(); + return curves[curves.length - 1]; + }, + + isClosed: function() { + return this._closed; + }, + + setClosed: function(closed) { + if (this._closed != (closed = !!closed)) { + this._closed = closed; + if (this._curves) { + var length = this._curves.length = this._countCurves(); + if (closed) + this._curves[length - 1] = new Curve(this, + this._segments[length - 1], this._segments[0]); + } + this._changed(25); + } + } +}, { + beans: true, + + getPathData: function(_matrix, _precision) { + var segments = this._segments, + length = segments.length, + f = new Formatter(_precision), + coords = new Array(6), + first = true, + curX, curY, + prevX, prevY, + inX, inY, + outX, outY, + parts = []; + + function addSegment(segment, skipLine) { + segment._transformCoordinates(_matrix, coords, false); + curX = coords[0]; + curY = coords[1]; + if (first) { + parts.push('M' + f.pair(curX, curY)); + first = false; + } else { + inX = coords[2]; + inY = coords[3]; + if (inX === curX && inY === curY + && outX === prevX && outY === prevY) { + if (!skipLine) + parts.push('l' + f.pair(curX - prevX, curY - prevY)); + } else { + parts.push('c' + f.pair(outX - prevX, outY - prevY) + + ' ' + f.pair(inX - prevX, inY - prevY) + + ' ' + f.pair(curX - prevX, curY - prevY)); + } + } + prevX = curX; + prevY = curY; + outX = coords[4]; + outY = coords[5]; + } + + if (length === 0) + return ''; + + for (var i = 0; i < length; i++) + addSegment(segments[i]); + if (this._closed && length > 0) { + addSegment(segments[0], true); + parts.push('z'); + } + return parts.join(''); + } +}, { + + isEmpty: function() { + return this._segments.length === 0; + }, + + _transformContent: function(matrix) { + var coords = new Array(6); + for (var i = 0, l = this._segments.length; i < l; i++) + this._segments[i]._transformCoordinates(matrix, coords, true); + return true; + }, + + _add: function(segs, index) { + var segments = this._segments, + curves = this._curves, + amount = segs.length, + append = index == null, + index = append ? segments.length : index; + for (var i = 0; i < amount; i++) { + var segment = segs[i]; + if (segment._path) + segment = segs[i] = segment.clone(); + segment._path = this; + segment._index = index + i; + if (segment._selectionState) + this._updateSelection(segment, 0, segment._selectionState); + } + if (append) { + segments.push.apply(segments, segs); + } else { + segments.splice.apply(segments, [index, 0].concat(segs)); + for (var i = index + amount, l = segments.length; i < l; i++) + segments[i]._index = i; + } + if (curves) { + var total = this._countCurves(), + from = index + amount - 1 === total ? index - 1 : index, + start = from, + to = Math.min(from + amount, total); + if (segs._curves) { + curves.splice.apply(curves, [from, 0].concat(segs._curves)); + start += segs._curves.length; + } + for (var i = start; i < to; i++) + curves.splice(i, 0, new Curve(this, null, null)); + this._adjustCurves(from, to); + } + this._changed(25); + return segs; + }, + + _adjustCurves: function(from, to) { + var segments = this._segments, + curves = this._curves, + curve; + for (var i = from; i < to; i++) { + curve = curves[i]; + curve._path = this; + curve._segment1 = segments[i]; + curve._segment2 = segments[i + 1] || segments[0]; + curve._changed(); + } + if (curve = curves[this._closed && from === 0 ? segments.length - 1 + : from - 1]) { + curve._segment2 = segments[from] || segments[0]; + curve._changed(); + } + if (curve = curves[to]) { + curve._segment1 = segments[to]; + curve._changed(); + } + }, + + _countCurves: function() { + var length = this._segments.length; + return !this._closed && length > 0 ? length - 1 : length; + }, + + add: function(segment1 ) { + return arguments.length > 1 && typeof segment1 !== 'number' + ? this._add(Segment.readAll(arguments)) + : this._add([ Segment.read(arguments) ])[0]; + }, + + insert: function(index, segment1 ) { + return arguments.length > 2 && typeof segment1 !== 'number' + ? this._add(Segment.readAll(arguments, 1), index) + : this._add([ Segment.read(arguments, 1) ], index)[0]; + }, + + addSegment: function() { + return this._add([ Segment.read(arguments) ])[0]; + }, + + insertSegment: function(index ) { + return this._add([ Segment.read(arguments, 1) ], index)[0]; + }, + + addSegments: function(segments) { + return this._add(Segment.readAll(segments)); + }, + + insertSegments: function(index, segments) { + return this._add(Segment.readAll(segments), index); + }, + + removeSegment: function(index) { + return this.removeSegments(index, index + 1)[0] || null; + }, + + removeSegments: function(from, to, _includeCurves) { + from = from || 0; + to = Base.pick(to, this._segments.length); + var segments = this._segments, + curves = this._curves, + count = segments.length, + removed = segments.splice(from, to - from), + amount = removed.length; + if (!amount) + return removed; + for (var i = 0; i < amount; i++) { + var segment = removed[i]; + if (segment._selectionState) + this._updateSelection(segment, segment._selectionState, 0); + segment._index = segment._path = null; + } + for (var i = from, l = segments.length; i < l; i++) + segments[i]._index = i; + if (curves) { + var index = from > 0 && to === count + (this._closed ? 1 : 0) + ? from - 1 + : from, + curves = curves.splice(index, amount); + if (_includeCurves) + removed._curves = curves.slice(1); + this._adjustCurves(index, index); + } + this._changed(25); + return removed; + }, + + clear: '#removeSegments', + + hasHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) { + if (segments[i].hasHandles()) + return true; + } + return false; + }, + + clearHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) + segments[i].clearHandles(); + }, + + getLength: function() { + if (this._length == null) { + var curves = this.getCurves(), + length = 0; + for (var i = 0, l = curves.length; i < l; i++) + length += curves[i].getLength(); + this._length = length; + } + return this._length; + }, + + getArea: function() { + if (this._area == null) { + var segments = this._segments, + count = segments.length, + last = count - 1, + area = 0; + for (var i = 0, l = this._closed ? count : last; i < l; i++) { + area += Curve.getArea(Curve.getValues( + segments[i], segments[i < last ? i + 1 : 0])); + } + this._area = area; + } + return this._area; + }, + + isClockwise: function() { + if (this._clockwise !== undefined) + return this._clockwise; + return this.getArea() >= 0; + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() != (clockwise = !!clockwise)) + this.reverse(); + this._clockwise = clockwise; + }, + + isFullySelected: function() { + var length = this._segments.length; + return this._selected && length > 0 && this._selectedSegmentState + === length * 7; + }, + + setFullySelected: function(selected) { + if (selected) + this._selectSegments(true); + this.setSelected(selected); + }, + + setSelected: function setSelected(selected) { + if (!selected) + this._selectSegments(false); + setSelected.base.call(this, selected); + }, + + _selectSegments: function(selected) { + var length = this._segments.length; + this._selectedSegmentState = selected + ? length * 7 : 0; + for (var i = 0; i < length; i++) + this._segments[i]._selectionState = selected + ? 7 : 0; + }, + + _updateSelection: function(segment, oldState, newState) { + segment._selectionState = newState; + var total = this._selectedSegmentState += newState - oldState; + if (total > 0) + this.setSelected(true); + }, + + flatten: function(maxDistance) { + var iterator = new PathIterator(this, 64, 0.1), + pos = 0, + step = iterator.length / Math.ceil(iterator.length / maxDistance), + end = iterator.length + (this._closed ? -step : step) / 2; + var segments = []; + while (pos <= end) { + segments.push(new Segment(iterator.getPointAt(pos))); + pos += step; + } + this.setSegments(segments); + }, + + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (!curve.hasHandles() && (curve.getLength() === 0 + || curve.isCollinear(curve.getNext()))) + curve.remove(); + } + return this; + }, + + simplify: function(tolerance) { + if (this._segments.length > 2) { + var fitter = new PathFitter(this, tolerance || 2.5); + this.setSegments(fitter.fit()); + } + }, + + split: function(index, parameter) { + if (parameter === null) + return null; + if (arguments.length === 1) { + var arg = index; + if (typeof arg === 'number') + arg = this.getLocationAt(arg); + if (!arg) + return null + index = arg.index; + parameter = arg.parameter; + } + var tMin = 4e-7, + tMax = 1 - tMin; + if (parameter >= tMax) { + index++; + parameter--; + } + var curves = this.getCurves(); + if (index >= 0 && index < curves.length) { + if (parameter >= tMin) { + curves[index++].divide(parameter, true); + } + var segs = this.removeSegments(index, this._segments.length, true), + path; + if (this._closed) { + this.setClosed(false); + path = this; + } else { + path = new Path(Item.NO_INSERT); + path.insertAbove(this, true); + this._clone(path); + } + path._add(segs, 0); + this.addSegment(segs[0]); + return path; + } + return null; + }, + + reverse: function() { + this._segments.reverse(); + for (var i = 0, l = this._segments.length; i < l; i++) { + var segment = this._segments[i]; + var handleIn = segment._handleIn; + segment._handleIn = segment._handleOut; + segment._handleOut = handleIn; + segment._index = i; + } + this._curves = null; + if (this._clockwise !== undefined) + this._clockwise = !this._clockwise; + this._changed(9); + }, + + join: function(path) { + if (path) { + var segments = path._segments, + last1 = this.getLastSegment(), + last2 = path.getLastSegment(); + if (!last2) + return this; + if (last1 && last1._point.equals(last2._point)) + path.reverse(); + var first2 = path.getFirstSegment(); + if (last1 && last1._point.equals(first2._point)) { + last1.setHandleOut(first2._handleOut); + this._add(segments.slice(1)); + } else { + var first1 = this.getFirstSegment(); + if (first1 && first1._point.equals(first2._point)) + path.reverse(); + last2 = path.getLastSegment(); + if (first1 && first1._point.equals(last2._point)) { + first1.setHandleIn(last2._handleIn); + this._add(segments.slice(0, segments.length - 1), 0); + } else { + this._add(segments.slice()); + } + } + if (path._closed) + this._add([segments[0]]); + path.remove(); + } + var first = this.getFirstSegment(), + last = this.getLastSegment(); + if (first !== last && first._point.equals(last._point)) { + first.setHandleIn(last._handleIn); + last.remove(); + this.setClosed(true); + } + return this; + }, + + toShape: function(insert) { + if (!this._closed) + return null; + + var segments = this._segments, + type, + size, + radius, + topCenter; + + function isCollinear(i, j) { + var seg1 = segments[i], + seg2 = seg1.getNext(), + seg3 = segments[j], + seg4 = seg3.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg3._handleOut.isZero() && seg4._handleIn.isZero() + && seg2._point.subtract(seg1._point).isCollinear( + seg4._point.subtract(seg3._point)); + } + + function isOrthogonal(i) { + var seg2 = segments[i], + seg1 = seg2.getPrevious(), + seg3 = seg2.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg2._handleOut.isZero() && seg3._handleIn.isZero() + && seg2._point.subtract(seg1._point).isOrthogonal( + seg3._point.subtract(seg2._point)); + } + + function isArc(i) { + var seg1 = segments[i], + seg2 = seg1.getNext(), + handle1 = seg1._handleOut, + handle2 = seg2._handleIn, + kappa = 0.5522847498307936; + if (handle1.isOrthogonal(handle2)) { + var pt1 = seg1._point, + pt2 = seg2._point, + corner = new Line(pt1, handle1, true).intersect( + new Line(pt2, handle2, true), true); + return corner && Numerical.isZero(handle1.getLength() / + corner.subtract(pt1).getLength() - kappa) + && Numerical.isZero(handle2.getLength() / + corner.subtract(pt2).getLength() - kappa); + } + return false; + } + + function getDistance(i, j) { + return segments[i]._point.getDistance(segments[j]._point); + } + + if (!this.hasHandles() && segments.length === 4 + && isCollinear(0, 2) && isCollinear(1, 3) && isOrthogonal(1)) { + type = Shape.Rectangle; + size = new Size(getDistance(0, 3), getDistance(0, 1)); + topCenter = segments[1]._point.add(segments[2]._point).divide(2); + } else if (segments.length === 8 && isArc(0) && isArc(2) && isArc(4) + && isArc(6) && isCollinear(1, 5) && isCollinear(3, 7)) { + type = Shape.Rectangle; + size = new Size(getDistance(1, 6), getDistance(0, 3)); + radius = size.subtract(new Size(getDistance(0, 7), + getDistance(1, 2))).divide(2); + topCenter = segments[3]._point.add(segments[4]._point).divide(2); + } else if (segments.length === 4 + && isArc(0) && isArc(1) && isArc(2) && isArc(3)) { + if (Numerical.isZero(getDistance(0, 2) - getDistance(1, 3))) { + type = Shape.Circle; + radius = getDistance(0, 2) / 2; + } else { + type = Shape.Ellipse; + radius = new Size(getDistance(2, 0) / 2, getDistance(3, 1) / 2); + } + topCenter = segments[1]._point; + } + + if (type) { + var center = this.getPosition(true), + shape = this._clone(new type({ + center: center, + size: size, + radius: radius, + insert: false + }), insert, false); + shape.rotate(topCenter.subtract(center).getAngle() + 90); + return shape; + } + return null; + }, + + _hitTestSelf: function(point, options) { + var that = this, + style = this.getStyle(), + segments = this._segments, + numSegments = segments.length, + closed = this._closed, + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hitStroke = options.stroke && style.hasStroke(), + hitFill = options.fill && style.hasFill(), + hitCurves = options.curves, + radius = hitStroke + ? style.getStrokeWidth() / 2 + : hitFill && options.tolerance > 0 || hitCurves + ? 0 : null; + if (radius !== null) { + if (radius > 0) { + join = style.getStrokeJoin(); + cap = style.getStrokeCap(); + miterLimit = radius * style.getMiterLimit(); + strokePadding = tolerancePadding.add(new Point(radius, radius)); + } else { + join = cap = 'round'; + } + } + + function isCloseEnough(pt, padding) { + return point.subtract(pt).divide(padding).length <= 1; + } + + function checkSegmentPoint(seg, pt, name) { + if (!options.selected || pt.isSelected()) { + var anchor = seg._point; + if (pt !== anchor) + pt = pt.add(anchor); + if (isCloseEnough(pt, strokePadding)) { + return new HitResult(name, that, { + segment: seg, + point: pt + }); + } + } + } + + function checkSegmentPoints(seg, ends) { + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') + || (!ends && options.handles) && ( + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); + } + + function addToArea(point) { + area.add(point); + } + + function checkSegmentStroke(segment) { + if (join !== 'round' || cap !== 'round') { + area = new Path({ internal: true, closed: true }); + if (closed || segment._index > 0 + && segment._index < numSegments - 1) { + if (join !== 'round' && (segment._handleIn.isZero() + || segment._handleOut.isZero())) + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); + } else if (cap !== 'round') { + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); + } + } + return isCloseEnough(segment._point, strokePadding); + } + + if (options.ends && !options.segments && !closed) { + if (res = checkSegmentPoints(segments[0], true) + || checkSegmentPoints(segments[numSegments - 1], true)) + return res; + } else if (options.segments || options.handles) { + for (var i = 0; i < numSegments; i++) + if (res = checkSegmentPoints(segments[i])) + return res; + } + if (radius !== null) { + loc = this.getNearestLocation(point); + if (loc) { + var parameter = loc.getParameter(); + if (parameter === 0 || parameter === 1 && numSegments > 1) { + if (!checkSegmentStroke(loc.getSegment())) + loc = null; + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { + loc = null; + } + } + if (!loc && join === 'miter' && numSegments > 1) { + for (var i = 0; i < numSegments; i++) { + var segment = segments[i]; + if (point.getDistance(segment._point) <= miterLimit + && checkSegmentStroke(segment)) { + loc = segment.getLocation(); + break; + } + } + } + } + return !loc && hitFill && this._contains(point) + || loc && !hitStroke && !hitCurves + ? new HitResult('fill', this) + : loc + ? new HitResult(hitStroke ? 'stroke' : 'curve', this, { + location: loc, + point: loc.getPoint() + }) + : null; + } + +}, Base.each(Curve.evaluateMethods, + function(name) { + this[name + 'At'] = function(offset, isParameter) { + var loc = this.getLocationAt(offset, isParameter); + return loc && loc[name](); + }; + }, +{ + beans: false, + + getLocationOf: function() { + var point = Point.read(arguments), + curves = this.getCurves(); + for (var i = 0, l = curves.length; i < l; i++) { + var loc = curves[i].getLocationOf(point); + if (loc) + return loc; + } + return null; + }, + + getOffsetOf: function() { + var loc = this.getLocationOf.apply(this, arguments); + return loc ? loc.getOffset() : null; + }, + + getLocationAt: function(offset, isParameter) { + var curves = this.getCurves(), + length = 0; + if (isParameter) { + var index = ~~offset, + curve = curves[index]; + return curve ? curve.getLocationAt(offset - index, true) : null; + } + for (var i = 0, l = curves.length; i < l; i++) { + var start = length, + curve = curves[i]; + length += curve.getLength(); + if (length > offset) { + return curve.getLocationAt(offset - start); + } + } + if (curves.length > 0 && offset <= this.getLength()) + return new CurveLocation(curves[curves.length - 1], 1); + return null; + }, + + getNearestLocation: function() { + var point = Point.read(arguments), + curves = this.getCurves(), + minDist = Infinity, + minLoc = null; + for (var i = 0, l = curves.length; i < l; i++) { + var loc = curves[i].getNearestLocation(point); + if (loc._distance < minDist) { + minDist = loc._distance; + minLoc = loc; + } + } + return minLoc; + }, + + getNearestPoint: function() { + return this.getNearestLocation.apply(this, arguments).getPoint(); + } +}), +new function() { + + function drawHandles(ctx, segments, matrix, size) { + var half = size / 2; + + function drawHandle(index) { + var hX = coords[index], + hY = coords[index + 1]; + if (pX != hX || pY != hY) { + ctx.beginPath(); + ctx.moveTo(pX, pY); + ctx.lineTo(hX, hY); + ctx.stroke(); + ctx.beginPath(); + ctx.arc(hX, hY, half, 0, Math.PI * 2, true); + ctx.fill(); + } + } + + var coords = new Array(6); + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + segment._transformCoordinates(matrix, coords, false); + var state = segment._selectionState, + pX = coords[0], + pY = coords[1]; + if (state & 1) + drawHandle(2); + if (state & 2) + drawHandle(4); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; + ctx.fillStyle = '#ffffff'; + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; + } + } + } + + function drawSegments(ctx, path, matrix) { + var segments = path._segments, + length = segments.length, + coords = new Array(6), + first = true, + curX, curY, + prevX, prevY, + inX, inY, + outX, outY; + + function drawSegment(segment) { + if (matrix) { + segment._transformCoordinates(matrix, coords, false); + curX = coords[0]; + curY = coords[1]; + } else { + var point = segment._point; + curX = point._x; + curY = point._y; + } + if (first) { + ctx.moveTo(curX, curY); + first = false; + } else { + if (matrix) { + inX = coords[2]; + inY = coords[3]; + } else { + var handle = segment._handleIn; + inX = curX + handle._x; + inY = curY + handle._y; + } + if (inX === curX && inY === curY + && outX === prevX && outY === prevY) { + ctx.lineTo(curX, curY); + } else { + ctx.bezierCurveTo(outX, outY, inX, inY, curX, curY); + } + } + prevX = curX; + prevY = curY; + if (matrix) { + outX = coords[4]; + outY = coords[5]; + } else { + var handle = segment._handleOut; + outX = prevX + handle._x; + outY = prevY + handle._y; + } + } + + for (var i = 0; i < length; i++) + drawSegment(segments[i]); + if (path._closed && length > 0) + drawSegment(segments[0]); + } + + return { + _draw: function(ctx, param, strokeMatrix) { + var dontStart = param.dontStart, + dontPaint = param.dontFinish || param.clip, + style = this.getStyle(), + hasFill = style.hasFill(), + hasStroke = style.hasStroke(), + dashArray = style.getDashArray(), + dashLength = !paper.support.nativeDash && hasStroke + && dashArray && dashArray.length; + + if (!dontStart) + ctx.beginPath(); + + if (!dontStart && this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || dontPaint) { + drawSegments(ctx, this, strokeMatrix); + if (this._closed) + ctx.closePath(); + if (!dontStart) + this._currentPath = ctx.currentPath; + } + + function getOffset(i) { + return dashArray[((i % dashLength) + dashLength) % dashLength]; + } + + if (!dontPaint && (hasFill || hasStroke)) { + this._setStyles(ctx); + if (hasFill) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (hasStroke) { + if (dashLength) { + if (!dontStart) + ctx.beginPath(); + var iterator = new PathIterator(this, 32, 0.25, + strokeMatrix), + length = iterator.length, + from = -style.getDashOffset(), to, + i = 0; + from = from % length; + while (from > 0) { + from -= getOffset(i--) + getOffset(i--); + } + while (from < length) { + to = from + getOffset(i++); + if (from > 0 || to > 0) + iterator.drawPart(ctx, + Math.max(from, 0), Math.max(to, 0)); + from = to + getOffset(i++); + } + } + ctx.stroke(); + } + } + }, + + _drawSelected: function(ctx, matrix) { + ctx.beginPath(); + drawSegments(ctx, this, matrix); + ctx.stroke(); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); + } + }; +}, +new function() { + function getFirstControlPoints(rhs) { + var n = rhs.length, + x = [], + tmp = [], + b = 2; + x[0] = rhs[0] / b; + for (var i = 1; i < n; i++) { + tmp[i] = 1 / b; + b = (i < n - 1 ? 4 : 2) - tmp[i]; + x[i] = (rhs[i] - x[i - 1]) / b; + } + for (var i = 1; i < n; i++) { + x[n - i - 1] -= tmp[n - i] * x[n - i]; + } + return x; + } + + return { + smooth: function() { + var segments = this._segments, + size = segments.length, + closed = this._closed, + n = size, + overlap = 0; + if (size <= 2) + return; + if (closed) { + overlap = Math.min(size, 4); + n += Math.min(size, overlap) * 2; + } + var knots = []; + for (var i = 0; i < size; i++) + knots[i + overlap] = segments[i]._point; + if (closed) { + for (var i = 0; i < overlap; i++) { + knots[i] = segments[i + size - overlap]._point; + knots[i + size + overlap] = segments[i]._point; + } + } else { + n--; + } + var rhs = []; + + for (var i = 1; i < n - 1; i++) + rhs[i] = 4 * knots[i]._x + 2 * knots[i + 1]._x; + rhs[0] = knots[0]._x + 2 * knots[1]._x; + rhs[n - 1] = 3 * knots[n - 1]._x; + var x = getFirstControlPoints(rhs); + + for (var i = 1; i < n - 1; i++) + rhs[i] = 4 * knots[i]._y + 2 * knots[i + 1]._y; + rhs[0] = knots[0]._y + 2 * knots[1]._y; + rhs[n - 1] = 3 * knots[n - 1]._y; + var y = getFirstControlPoints(rhs); + + if (closed) { + for (var i = 0, j = size; i < overlap; i++, j++) { + var f1 = i / overlap, + f2 = 1 - f1, + ie = i + overlap, + je = j + overlap; + x[j] = x[i] * f1 + x[j] * f2; + y[j] = y[i] * f1 + y[j] * f2; + x[je] = x[ie] * f2 + x[je] * f1; + y[je] = y[ie] * f2 + y[je] * f1; + } + n--; + } + var handleIn = null; + for (var i = overlap; i <= n - overlap; i++) { + var segment = segments[i - overlap]; + if (handleIn) + segment.setHandleIn(handleIn.subtract(segment._point)); + if (i < n) { + segment.setHandleOut( + new Point(x[i], y[i]).subtract(segment._point)); + handleIn = i < n - 1 + ? new Point( + 2 * knots[i + 1]._x - x[i + 1], + 2 * knots[i + 1]._y - y[i + 1]) + : new Point( + (knots[n]._x + x[n - 1]) / 2, + (knots[n]._y + y[n - 1]) / 2); + } + } + if (closed && handleIn) { + var segment = this._segments[0]; + segment.setHandleIn(handleIn.subtract(segment._point)); + } + } + }; +}, +new function() { + function getCurrentSegment(that) { + var segments = that._segments; + if (segments.length === 0) + throw new Error('Use a moveTo() command first'); + return segments[segments.length - 1]; + } + + return { + moveTo: function() { + var segments = this._segments; + if (segments.length === 1) + this.removeSegment(0); + if (!segments.length) + this._add([ new Segment(Point.read(arguments)) ]); + }, + + moveBy: function() { + throw new Error('moveBy() is unsupported on Path items.'); + }, + + lineTo: function() { + this._add([ new Segment(Point.read(arguments)) ]); + }, + + cubicCurveTo: function() { + var handle1 = Point.read(arguments), + handle2 = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this); + current.setHandleOut(handle1.subtract(current._point)); + this._add([ new Segment(to, handle2.subtract(to)) ]); + }, + + quadraticCurveTo: function() { + var handle = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.cubicCurveTo( + handle.add(current.subtract(handle).multiply(1 / 3)), + handle.add(to.subtract(handle).multiply(1 / 3)), + to + ); + }, + + curveTo: function() { + var through = Point.read(arguments), + to = Point.read(arguments), + t = Base.pick(Base.read(arguments), 0.5), + t1 = 1 - t, + current = getCurrentSegment(this)._point, + handle = through.subtract(current.multiply(t1 * t1)) + .subtract(to.multiply(t * t)).divide(2 * t * t1); + if (handle.isNaN()) + throw new Error( + 'Cannot put a curve through points with parameter = ' + t); + this.quadraticCurveTo(handle, to); + }, + + arcTo: function() { + var current = getCurrentSegment(this), + from = current._point, + to = Point.read(arguments), + through, + peek = Base.peek(arguments), + clockwise = Base.pick(peek, true), + center, extent, vector, matrix; + if (typeof clockwise === 'boolean') { + var middle = from.add(to).divide(2), + through = middle.add(middle.subtract(from).rotate( + clockwise ? -90 : 90)); + } else if (Base.remain(arguments) <= 2) { + through = to; + to = Point.read(arguments); + } else { + var radius = Size.read(arguments); + if (radius.isZero()) + return this.lineTo(to); + var rotation = Base.read(arguments), + clockwise = !!Base.read(arguments), + large = !!Base.read(arguments), + middle = from.add(to).divide(2), + pt = from.subtract(middle).rotate(-rotation), + x = pt.x, + y = pt.y, + abs = Math.abs, + rx = abs(radius.width), + ry = abs(radius.height), + rxSq = rx * rx, + rySq = ry * ry, + xSq = x * x, + ySq = y * y; + var factor = Math.sqrt(xSq / rxSq + ySq / rySq); + if (factor > 1) { + rx *= factor; + ry *= factor; + rxSq = rx * rx; + rySq = ry * ry; + } + factor = (rxSq * rySq - rxSq * ySq - rySq * xSq) / + (rxSq * ySq + rySq * xSq); + if (abs(factor) < 1e-12) + factor = 0; + if (factor < 0) + throw new Error( + 'Cannot create an arc with the given arguments'); + center = new Point(rx * y / ry, -ry * x / rx) + .multiply((large === clockwise ? -1 : 1) + * Math.sqrt(factor)) + .rotate(rotation).add(middle); + matrix = new Matrix().translate(center).rotate(rotation) + .scale(rx, ry); + vector = matrix._inverseTransform(from); + extent = vector.getDirectedAngle(matrix._inverseTransform(to)); + if (!clockwise && extent > 0) + extent -= 360; + else if (clockwise && extent < 0) + extent += 360; + } + if (through) { + var l1 = new Line(from.add(through).divide(2), + through.subtract(from).rotate(90), true), + l2 = new Line(through.add(to).divide(2), + to.subtract(through).rotate(90), true), + line = new Line(from, to), + throughSide = line.getSide(through); + center = l1.intersect(l2, true); + if (!center) { + if (!throughSide) + return this.lineTo(to); + throw new Error( + 'Cannot create an arc with the given arguments'); + } + vector = from.subtract(center); + extent = vector.getDirectedAngle(to.subtract(center)); + var centerSide = line.getSide(center); + if (centerSide === 0) { + extent = throughSide * Math.abs(extent); + } else if (throughSide === centerSide) { + extent += extent < 0 ? 360 : -360; + } + } + var ext = Math.abs(extent), + count = ext >= 360 ? 4 : Math.ceil(ext / 90), + inc = extent / count, + half = inc * Math.PI / 360, + z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), + segments = []; + for (var i = 0; i <= count; i++) { + var pt = to, + out = null; + if (i < count) { + out = vector.rotate(90).multiply(z); + if (matrix) { + pt = matrix._transformPoint(vector); + out = matrix._transformPoint(vector.add(out)) + .subtract(pt); + } else { + pt = center.add(vector); + } + } + if (i === 0) { + current.setHandleOut(out); + } else { + var _in = vector.rotate(-90).multiply(z); + if (matrix) { + _in = matrix._transformPoint(vector.add(_in)) + .subtract(pt); + } + segments.push(new Segment(pt, _in, out)); + } + vector = vector.rotate(inc); + } + this._add(segments); + }, + + lineBy: function() { + var to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.lineTo(current.add(to)); + }, + + curveBy: function() { + var through = Point.read(arguments), + to = Point.read(arguments), + parameter = Base.read(arguments), + current = getCurrentSegment(this)._point; + this.curveTo(current.add(through), current.add(to), parameter); + }, + + cubicCurveBy: function() { + var handle1 = Point.read(arguments), + handle2 = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.cubicCurveTo(current.add(handle1), current.add(handle2), + current.add(to)); + }, + + quadraticCurveBy: function() { + var handle = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.quadraticCurveTo(current.add(handle), current.add(to)); + }, + + arcBy: function() { + var current = getCurrentSegment(this)._point, + point = current.add(Point.read(arguments)), + clockwise = Base.pick(Base.peek(arguments), true); + if (typeof clockwise === 'boolean') { + this.arcTo(point, clockwise); + } else { + this.arcTo(point, current.add(Point.read(arguments))); + } + }, + + closePath: function(join) { + this.setClosed(true); + if (join) + this.join(); + } + }; +}, { + + _getBounds: function(getter, matrix) { + return Path[getter](this._segments, this._closed, this.getStyle(), + matrix); + }, + +statics: { + getBounds: function(segments, closed, style, matrix, strokePadding) { + var first = segments[0]; + if (!first) + return new Rectangle(); + var coords = new Array(6), + prevCoords = first._transformCoordinates(matrix, new Array(6), false), + min = prevCoords.slice(0, 2), + max = min.slice(), + roots = new Array(2); + + function processSegment(segment) { + segment._transformCoordinates(matrix, coords, false); + for (var i = 0; i < 2; i++) { + Curve._addBounds( + prevCoords[i], + prevCoords[i + 4], + coords[i + 2], + coords[i], + i, strokePadding ? strokePadding[i] : 0, min, max, roots); + } + var tmp = prevCoords; + prevCoords = coords; + coords = tmp; + } + + for (var i = 1, l = segments.length; i < l; i++) + processSegment(segments[i]); + if (closed) + processSegment(first); + return new Rectangle(min[0], min[1], max[0] - min[0], max[1] - min[1]); + }, + + getStrokeBounds: function(segments, closed, style, matrix) { + if (!style.hasStroke()) + return Path.getBounds(segments, closed, style, matrix); + var length = segments.length - (closed ? 0 : 1), + radius = style.getStrokeWidth() / 2, + padding = Path._getPenPadding(radius, matrix), + bounds = Path.getBounds(segments, closed, style, matrix, padding), + join = style.getStrokeJoin(), + cap = style.getStrokeCap(), + miterLimit = radius * style.getMiterLimit(); + var joinBounds = new Rectangle(new Size(padding).multiply(2)); + + function add(point) { + bounds = bounds.include(matrix + ? matrix._transformPoint(point, point) : point); + } + + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + + function addJoin(segment, join) { + var handleIn = segment._handleIn, + handleOut = segment._handleOut; + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isCollinear(handleOut)) { + addRound(segment); + } else { + Path._addBevelJoin(segment, join, radius, miterLimit, add); + } + } + + function addCap(segment, cap) { + if (cap === 'round') { + addRound(segment); + } else { + Path._addSquareCap(segment, cap, radius, add); + } + } + + for (var i = 1; i < length; i++) + addJoin(segments[i], join); + if (closed) { + addJoin(segments[0], join); + } else if (length > 0) { + addCap(segments[0], cap); + addCap(segments[segments.length - 1], cap); + } + return bounds; + }, + + _getPenPadding: function(radius, matrix) { + if (!matrix) + return [radius, radius]; + var mx = matrix.shiftless(), + hor = mx.transform(new Point(radius, 0)), + ver = mx.transform(new Point(0, radius)), + phi = hor.getAngleInRadians(), + a = hor.getLength(), + b = ver.getLength(); + var sin = Math.sin(phi), + cos = Math.cos(phi), + tan = Math.tan(phi), + tx = -Math.atan(b * tan / a), + ty = Math.atan(b / (tan * a)); + return [Math.abs(a * Math.cos(tx) * cos - b * Math.sin(tx) * sin), + Math.abs(b * Math.sin(ty) * cos + a * Math.cos(ty) * sin)]; + }, + + _addBevelJoin: function(segment, join, radius, miterLimit, addPoint, area) { + var curve2 = segment.getCurve(), + curve1 = curve2.getPrevious(), + point = curve2.getPointAt(0, true), + normal1 = curve1.getNormalAt(1, true), + normal2 = curve2.getNormalAt(0, true), + step = normal1.getDirectedAngle(normal2) < 0 ? -radius : radius; + normal1.setLength(step); + normal2.setLength(step); + if (area) { + addPoint(point); + addPoint(point.add(normal1)); + } + if (join === 'miter') { + var corner = new Line( + point.add(normal1), + new Point(-normal1.y, normal1.x), true + ).intersect(new Line( + point.add(normal2), + new Point(-normal2.y, normal2.x), true + ), true); + if (corner && point.getDistance(corner) <= miterLimit) { + addPoint(corner); + if (!area) + return; + } + } + if (!area) + addPoint(point.add(normal1)); + addPoint(point.add(normal2)); + }, + + _addSquareCap: function(segment, cap, radius, addPoint, area) { + var point = segment._point, + loc = segment.getLocation(), + normal = loc.getNormal().multiply(radius); + if (area) { + addPoint(point.subtract(normal)); + addPoint(point.add(normal)); + } + if (cap === 'square') + point = point.add(normal.rotate(loc.getParameter() === 0 ? -90 : 90)); + addPoint(point.add(normal)); + addPoint(point.subtract(normal)); + }, + + getHandleBounds: function(segments, closed, style, matrix, strokePadding, + joinPadding) { + var coords = new Array(6), + x1 = Infinity, + x2 = -x1, + y1 = x1, + y2 = x2; + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + segment._transformCoordinates(matrix, coords, false); + for (var j = 0; j < 6; j += 2) { + var padding = j === 0 ? joinPadding : strokePadding, + paddingX = padding ? padding[0] : 0, + paddingY = padding ? padding[1] : 0, + x = coords[j], + y = coords[j + 1], + xn = x - paddingX, + xx = x + paddingX, + yn = y - paddingY, + yx = y + paddingY; + if (xn < x1) x1 = xn; + if (xx > x2) x2 = xx; + if (yn < y1) y1 = yn; + if (yx > y2) y2 = yx; + } + } + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + getRoughBounds: function(segments, closed, style, matrix) { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { + if (style.getStrokeJoin() === 'miter') + joinRadius = strokeRadius * style.getMiterLimit(); + if (style.getStrokeCap() === 'square') + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); + } + return Path.getHandleBounds(segments, closed, style, matrix, + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); + } +}}); + +Path.inject({ statics: new function() { + + var kappa = 0.5522847498307936, + ellipseSegments = [ + new Segment([-1, 0], [0, kappa ], [0, -kappa]), + new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), + new Segment([1, 0], [0, -kappa], [0, kappa ]), + new Segment([0, 1], [kappa, 0 ], [-kappa, 0]) + ]; + + function createPath(segments, closed, args) { + var props = Base.getNamed(args), + path = new Path(props && props.insert === false && Item.NO_INSERT); + path._add(segments); + path._closed = closed; + return path.set(props); + } + + function createEllipse(center, radius, args) { + var segments = new Array(4); + for (var i = 0; i < 4; i++) { + var segment = ellipseSegments[i]; + segments[i] = new Segment( + segment._point.multiply(radius).add(center), + segment._handleIn.multiply(radius), + segment._handleOut.multiply(radius) + ); + } + return createPath(segments, true, args); + } + + return { + Line: function() { + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); + }, + + Circle: function() { + var center = Point.readNamed(arguments, 'center'), + radius = Base.readNamed(arguments, 'radius'); + return createEllipse(center, new Size(radius), arguments); + }, + + Rectangle: function() { + var rect = Rectangle.readNamed(arguments, 'rectangle'), + radius = Size.readNamed(arguments, 'radius', 0, + { readNull: true }), + bl = rect.getBottomLeft(true), + tl = rect.getTopLeft(true), + tr = rect.getTopRight(true), + br = rect.getBottomRight(true), + segments; + if (!radius || radius.isZero()) { + segments = [ + new Segment(bl), + new Segment(tl), + new Segment(tr), + new Segment(br) + ]; + } else { + radius = Size.min(radius, rect.getSize(true).divide(2)); + var rx = radius.width, + ry = radius.height, + hx = rx * kappa, + hy = ry * kappa; + segments = [ + new Segment(bl.add(rx, 0), null, [-hx, 0]), + new Segment(bl.subtract(0, ry), [0, hy]), + new Segment(tl.add(0, ry), null, [0, -hy]), + new Segment(tl.add(rx, 0), [-hx, 0], null), + new Segment(tr.subtract(rx, 0), null, [hx, 0]), + new Segment(tr.add(0, ry), [0, -hy], null), + new Segment(br.subtract(0, ry), null, [0, hy]), + new Segment(br.subtract(rx, 0), [hx, 0]) + ]; + } + return createPath(segments, true, arguments); + }, + + RoundRectangle: '#Rectangle', + + Ellipse: function() { + var ellipse = Shape._readEllipse(arguments); + return createEllipse(ellipse.center, ellipse.radius, arguments); + }, + + Oval: '#Ellipse', + + Arc: function() { + var from = Point.readNamed(arguments, 'from'), + through = Point.readNamed(arguments, 'through'), + to = Point.readNamed(arguments, 'to'), + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); + path.moveTo(from); + path.arcTo(through, to); + return path.set(props); + }, + + RegularPolygon: function() { + var center = Point.readNamed(arguments, 'center'), + sides = Base.readNamed(arguments, 'sides'), + radius = Base.readNamed(arguments, 'radius'), + step = 360 / sides, + three = !(sides % 3), + vector = new Point(0, three ? -radius : radius), + offset = three ? -1 : 0.5, + segments = new Array(sides); + for (var i = 0; i < sides; i++) + segments[i] = new Segment(center.add( + vector.rotate((i + offset) * step))); + return createPath(segments, true, arguments); + }, + + Star: function() { + var center = Point.readNamed(arguments, 'center'), + points = Base.readNamed(arguments, 'points') * 2, + radius1 = Base.readNamed(arguments, 'radius1'), + radius2 = Base.readNamed(arguments, 'radius2'), + step = 360 / points, + vector = new Point(0, -1), + segments = new Array(points); + for (var i = 0; i < points; i++) + segments[i] = new Segment(center.add(vector.rotate(step * i) + .multiply(i % 2 ? radius2 : radius1))); + return createPath(segments, true, arguments); + } + }; +}}); + +var CompoundPath = PathItem.extend({ + _class: 'CompoundPath', + _serializeFields: { + children: [] + }, + + initialize: function CompoundPath(arg) { + this._children = []; + this._namedChildren = {}; + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + insertChildren: function insertChildren(index, items, _preserve) { + for (var i = items.length - 1; i >= 0; i--) { + var item = items[i]; + if (item instanceof CompoundPath) { + items.splice.apply(items, [i, 1].concat(item.removeChildren())); + item.remove(); + } + } + items = insertChildren.base.call(this, index, items, _preserve, Path); + for (var i = 0, l = !_preserve && items && items.length; i < l; i++) { + var item = items[i]; + if (item._clockwise === undefined) + item.setClockwise(item._index === 0); + } + return items; + }, + + reverse: function() { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + children[i].reverse(); + }, + + smooth: function() { + for (var i = 0, l = this._children.length; i < l; i++) + this._children[i].smooth(); + }, + + reduce: function reduce() { + var children = this._children; + for (var i = children.length - 1; i >= 0; i--) { + var path = children[i].reduce(); + if (path.isEmpty()) + children.splice(i, 1); + } + if (children.length === 0) { + var path = new Path(Item.NO_INSERT); + path.insertAbove(this); + path.setStyle(this._style); + this.remove(); + return path; + } + return reduce.base.call(this); + }, + + isClockwise: function() { + var child = this.getFirstChild(); + return child && child.isClockwise(); + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() !== !!clockwise) + this.reverse(); + }, + + getFirstSegment: function() { + var first = this.getFirstChild(); + return first && first.getFirstSegment(); + }, + + getLastSegment: function() { + var last = this.getLastChild(); + return last && last.getLastSegment(); + }, + + getCurves: function() { + var children = this._children, + curves = []; + for (var i = 0, l = children.length; i < l; i++) + curves.push.apply(curves, children[i].getCurves()); + return curves; + }, + + getFirstCurve: function() { + var first = this.getFirstChild(); + return first && first.getFirstCurve(); + }, + + getLastCurve: function() { + var last = this.getLastChild(); + return last && last.getFirstCurve(); + }, + + getArea: function() { + var children = this._children, + area = 0; + for (var i = 0, l = children.length; i < l; i++) + area += children[i].getArea(); + return area; + } +}, { + beans: true, + + getPathData: function(_matrix, _precision) { + var children = this._children, + paths = []; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i], + mx = child._matrix; + paths.push(child.getPathData(_matrix && !mx.isIdentity() + ? _matrix.chain(mx) : _matrix, _precision)); + } + return paths.join(' '); + } +}, { + _getChildHitTestOptions: function(options) { + return options.class === Path || options.type === 'path' + ? options + : new Base(options, { fill: false }); + }, + + _draw: function(ctx, param, strokeMatrix) { + var children = this._children; + if (children.length === 0) + return; + + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + param = param.extend({ dontStart: true, dontFinish: true }); + ctx.beginPath(); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param, strokeMatrix); + this._currentPath = ctx.currentPath; + } + + if (!param.clip) { + this._setStyles(ctx); + var style = this._style; + if (style.hasFill()) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (style.hasStroke()) + ctx.stroke(); + } + }, + + _drawSelected: function(ctx, matrix, selectedItems) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i], + mx = child._matrix; + if (!selectedItems[child._id]) + child._drawSelected(ctx, mx.isIdentity() ? matrix + : matrix.chain(mx)); + } + } +}, +new function() { + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) + throw new Error('Use a moveTo() command first'); + return children[children.length - 1]; + } + + var fields = { + moveTo: function() { + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current + : new Path(Item.NO_INSERT); + if (path !== current) + this.addChild(path); + path.moveTo.apply(path, arguments); + }, + + moveBy: function() { + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); + }, + + closePath: function(join) { + getCurrentPath(this, true).closePath(join); + } + }; + + Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo', 'arcTo', + 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], + function(key) { + fields[key] = function() { + var path = getCurrentPath(this, true); + path[key].apply(path, arguments); + }; + } + ); + + return fields; +}); + +PathItem.inject(new function() { + var operators = { + unite: function(w) { + return w === 1 || w === 0; + }, + + intersect: function(w) { + return w === 2; + }, + + subtract: function(w) { + return w === 1; + }, + + exclude: function(w) { + return w === 1; + } + }; + + function preparePath(path, resolve) { + var res = path.clone(false).reduce().transform(null, true, true); + return resolve ? res.resolveCrossings().reorient() : res; + } + + function finishBoolean(ctor, paths, path1, path2, reduce) { + var result = new ctor(Item.NO_INSERT); + result.addChildren(paths, true); + if (reduce) + result = result.reduce(); + result.insertAbove(path2 && path1.isSibling(path2) + && path1.getIndex() < path2.getIndex() + ? path2 : path1); + result.setStyle(path1._style); + return result; + } + + function computeBoolean(path1, path2, operation) { + if (!path1._children && !path1._closed) + return computeOpenBoolean(path1, path2, operation); + var _path1 = preparePath(path1, true), + _path2 = path2 && path1 !== path2 && preparePath(path2, true); + if (_path2 && /^(subtract|exclude)$/.test(operation) + ^ (_path2.isClockwise() !== _path1.isClockwise())) + _path2.reverse(); + var intersections = CurveLocation.expand( + _path1.getIntersections(_path2, function(inter) { + return _path2 && inter.isOverlap() || inter.isCrossing(); + }) + ); + divideLocations(intersections); + + var segments = [], + monoCurves = []; + + function collect(paths) { + for (var i = 0, l = paths.length; i < l; i++) { + var path = paths[i]; + segments.push.apply(segments, path._segments); + monoCurves.push.apply(monoCurves, path._getMonoCurves()); + } + } + + collect(_path1._children || [_path1]); + if (_path2) + collect(_path2._children || [_path2]); + for (var i = 0, l = intersections.length; i < l; i++) { + propagateWinding(intersections[i]._segment, _path1, _path2, + monoCurves, operation); + } + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding == null) { + propagateWinding(segment, _path1, _path2, monoCurves, + operation); + } + } + return finishBoolean(CompoundPath, tracePaths(segments, operation), + path1, path2, true); + } + + function computeOpenBoolean(path1, path2, operation) { + if (!path2 || !path2._children && !path2._closed + || !/^(subtract|intersect)$/.test(operation)) + return null; + var _path1 = preparePath(path1, false), + _path2 = preparePath(path2, false), + intersections = _path1.getIntersections(_path2, function(inter) { + return inter.isOverlap() || inter.isCrossing(); + }), + sub = operation === 'subtract', + paths = []; + + function addPath(path) { + if (_path2.contains(path.getPointAt(path.getLength() / 2)) ^ sub) { + paths.unshift(path); + return true; + } + } + + for (var i = intersections.length - 1; i >= 0; i--) { + var path = intersections[i].split(); + if (path) { + if (addPath(path)) + path.getFirstSegment().setHandleIn(0, 0); + _path1.getLastSegment().setHandleOut(0, 0); + } + } + addPath(_path1); + return finishBoolean(Group, paths, path1, path2); + } + + function linkIntersections(from, to) { + var prev = from; + while (prev) { + if (prev === to) + return; + prev = prev._prev; + } + while (from._next && from._next !== to) + from = from._next; + if (!from._next) { + while (to._prev) + to = to._prev; + from._next = to; + to._prev = from; + } + } + + function divideLocations(locations) { + var tMin = 4e-7, + tMax = 1 - tMin, + noHandles = false, + clearSegments = [], + prevCurve, + prevT; + + for (var i = locations.length - 1; i >= 0; i--) { + var loc = locations[i], + curve = loc._curve, + t = loc._parameter, + origT = t; + if (curve !== prevCurve) { + noHandles = !curve.hasHandles(); + } else if (prevT > 0) { + t /= prevT; + } + var segment; + if (t < tMin) { + segment = curve._segment1; + } else if (t > tMax) { + segment = curve._segment2; + } else { + segment = curve.divide(t, true, true)._segment1; + if (noHandles) + clearSegments.push(segment); + } + loc._setSegment(segment); + var inter = segment._intersection, + dest = loc._intersection; + if (inter) { + linkIntersections(inter, dest); + var other = inter; + while (other) { + linkIntersections(other._intersection, inter); + other = other._next; + } + } else { + segment._intersection = dest; + } + prevCurve = curve; + prevT = origT; + } + for (var i = 0, l = clearSegments.length; i < l; i++) { + clearSegments[i].clearHandles(); + } + } + + function getWinding(point, curves, horizontal, testContains) { + var epsilon = 2e-7, + tMin = 4e-7, + tMax = 1 - tMin, + px = point.x, + py = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = py - epsilon, + yAfter = py + epsilon; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, px, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y = Curve.getPoint(values, roots[j]).y; + if (y < yBefore && y > yTop) { + yTop = y; + } else if (y > yAfter && y < yBottom) { + yBottom = y; + } + } + } + } + yTop = (yTop + py) / 2; + yBottom = (yBottom + py) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(px, yTop), curves, false, + testContains); + if (yBottom < Infinity) + windRight = getWinding(new Point(px, yBottom), curves, false, + testContains); + } else { + var xBefore = px - epsilon, + xAfter = px + epsilon; + var startCounted = false, + prevCurve, + prevT; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding; + if (winding && (winding === 1 + && py >= values[1] && py <= values[7] + || py >= values[7] && py <= values[1]) + && Curve.solveCubic(values, 1, py, roots, 0, 1) === 1) { + var t = roots[0]; + if (!( + t > tMax && startCounted && curve.next !== curves[i + 1] + || t < tMin && prevT > tMax + && curve.previous === prevCurve)) { + var x = Curve.getPoint(values, t).x, + slope = Curve.getTangent(values, t).y, + counted = false; + if (Numerical.isZero(slope) && !Curve.isStraight(values) + || t < tMin && slope * Curve.getTangent( + curve.previous.values, 1).y < 0 + || t > tMax && slope * Curve.getTangent( + curve.next.values, 0).y < 0) { + if (testContains && x >= xBefore && x <= xAfter) { + ++windLeft; + ++windRight; + counted = true; + } + } else if (x <= xBefore) { + windLeft += winding; + counted = true; + } else if (x >= xAfter) { + windRight += winding; + counted = true; + } + if (curve.previous !== curves[i - 1]) + startCounted = t < tMin && counted; + } + prevCurve = curve; + prevT = t; + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function propagateWinding(segment, path1, path2, monoCurves, operation) { + var epsilon = 2e-7, + chain = [], + start = segment, + totalLength = 0, + windingSum = 0; + do { + var curve = segment.getCurve(), + length = curve.getLength(); + chain.push({ segment: segment, curve: curve, length: length }); + totalLength += length; + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== start); + for (var i = 0; i < 3; i++) { + var length = totalLength * (i + 1) / 4; + for (var k = 0, m = chain.length; k < m; k++) { + var node = chain[k], + curveLength = node.length; + if (length <= curveLength) { + if (length < epsilon || curveLength - length < epsilon) + length = curveLength / 2; + var curve = node.curve, + path = curve._path, + parent = path._parent, + pt = curve.getPointAt(length), + hor = curve.isHorizontal(); + if (parent instanceof CompoundPath) + path = parent; + windingSum += operation === 'subtract' && path2 + && (path === path1 && path2._getWinding(pt, hor) + || path === path2 && !path1._getWinding(pt, hor)) + ? 0 + : getWinding(pt, monoCurves, hor); + break; + } + length -= curveLength; + } + } + var winding = Math.round(windingSum / 3); + for (var j = chain.length - 1; j >= 0; j--) + chain[j].segment._winding = winding; + } + + function tracePaths(segments, operation) { + var paths = [], + start, + otherStart, + operator = operators[operation], + overlapWinding = { + unite: { 1: 2 }, + intersect: { 2: 1 } + }[operation]; + + function isValid(seg, adjusted) { + if (seg._visited) + return false; + if (!operator) + return true; + var winding = seg._winding, + inter = seg._intersection; + if (inter && adjusted && overlapWinding && inter.isOverlap()) + winding = overlapWinding[winding] || winding; + return operator(winding); + } + + function isStart(seg) { + return seg === start || seg === otherStart; + } + + function findBestIntersection(inter, strict) { + if (!inter._next) + return inter; + while (inter) { + var seg = inter._segment, + nextSeg = seg.getNext(), + nextInter = nextSeg._intersection; + if (isStart(nextSeg) + || !seg._visited && !nextSeg._visited + && (!operator + || (!strict || isValid(seg)) + && (!(strict && nextInter && nextInter.isOverlap()) + && isValid(nextSeg) + || !strict && nextInter + && isValid(nextInter._segment)) + )) + return inter; + inter = inter._next; + } + return null; + } + + function findStartSegment(inter, next) { + while (inter) { + var seg = inter._segment; + if (isStart(seg)) + return seg; + inter = inter[next ? '_next' : '_prev']; + } + } + + for (var i = 0, l = segments.length; i < l; i++) { + var seg = segments[i], + path = null, + finished = false; + if (!isValid(seg, true)) + continue; + start = otherStart = null; + while (!finished) { + var inter = seg._intersection, + handleIn = path && seg._handleIn; + inter = inter && (findBestIntersection(inter, true) + || findBestIntersection(inter, false)) || inter; + var other = inter && inter._segment; + if (other && isValid(other)) + seg = other; + if (seg._visited) { + finished = isStart(seg); + if (!finished && inter) { + var found = findStartSegment(inter, true) + || findStartSegment(inter, false); + if (found) { + seg = found; + finished = true; + } + } + break; + } + if (!path) { + path = new Path(Item.NO_INSERT); + start = seg; + otherStart = other; + } + path.add(new Segment(seg._point, handleIn, seg._handleOut)); + seg._visited = true; + seg = seg.getNext(); + finished = isStart(seg); + } + if (finished) { + path.firstSegment.setHandleIn(seg._handleIn); + path.setClosed(true); + } else if (path) { + console.error('Boolean operation resulted in open path', + 'segments =', path._segments.length, + 'length =', path.getLength()); + path = null; + } + if (path && (path._segments.length > 8 + || !Numerical.isZero(path.getArea()))) { + paths.push(path); + path = null; + } + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, 'unite'); + }, + + intersect: function(path) { + return computeBoolean(this, path, 'intersect'); + }, + + subtract: function(path) { + return computeBoolean(this, path, 'subtract'); + }, + + exclude: function(path) { + return computeBoolean(this, path, 'exclude'); + }, + + divide: function(path) { + return finishBoolean(Group, + [this.subtract(path), this.intersect(path)], + this, path, true); + }, + + resolveCrossings: function() { + var crossings = this.getCrossings(); + if (!crossings.length) + return this; + divideLocations(CurveLocation.expand(crossings)); + var paths = this._children || [this], + segments = []; + for (var i = 0, l = paths.length; i < l; i++) { + segments.push.apply(segments, paths[i]._segments); + } + return finishBoolean(CompoundPath, tracePaths(segments), + this, null, false); + } + }; +}); + +Path.inject({ + _getMonoCurves: function() { + var monoCurves = this._monoCurves, + prevCurve; + + function insertCurve(v) { + var y0 = v[1], + y1 = v[7], + curve = { + values: v, + winding: y0 === y1 + ? 0 + : y0 > y1 + ? -1 + : 1, + previous: prevCurve, + next: null + }; + if (prevCurve) + prevCurve.next = curve; + monoCurves.push(curve); + prevCurve = curve; + } + + function handleCurve(v) { + if (Curve.getLength(v) === 0) + return; + var y0 = v[1], + y1 = v[3], + y2 = v[5], + y3 = v[7]; + if (Curve.isStraight(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + tMin = 4e-7, + tMax = 1 - tMin, + roots = [], + n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax); + if (n === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (n > 1) { + t = (roots[1] - t) / (1 - t); + parts = Curve.subdivide(parts[1], t); + insertCurve(parts[0]); + } + insertCurve(parts[1]); + } + } + } + + if (!monoCurves) { + monoCurves = this._monoCurves = []; + var curves = this.getCurves(), + segments = this._segments; + for (var i = 0, l = curves.length; i < l; i++) + handleCurve(curves[i].getValues()); + if (!this._closed && segments.length > 1) { + var p1 = segments[segments.length - 1]._point, + p2 = segments[0]._point, + p1x = p1._x, p1y = p1._y, + p2x = p2._x, p2y = p2._y; + handleCurve([p1x, p1y, p1x, p1y, p2x, p2y, p2x, p2y]); + } + if (monoCurves.length > 0) { + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + } + return monoCurves; + }, + + getInteriorPoint: function() { + var bounds = this.getBounds(), + point = bounds.getCenter(true); + if (!this.contains(point)) { + var curves = this._getMonoCurves(), + roots = [], + y = point.y, + xIntercepts = []; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if ((curves[i].winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) + xIntercepts.push(Curve.getPoint(values, roots[j]).x); + } + if (xIntercepts.length > 1) + break; + } + point.x = (xIntercepts[0] + xIntercepts[1]) / 2; + } + return point; + }, + + reorient: function() { + this.setClockwise(true); + return this; + } +}); + +CompoundPath.inject({ + _getMonoCurves: function() { + var children = this._children, + monoCurves = []; + for (var i = 0, l = children.length; i < l; i++) + monoCurves.push.apply(monoCurves, children[i]._getMonoCurves()); + return monoCurves; + }, + + reorient: function() { + var children = this.removeChildren().sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + if (children.length > 0) { + this.addChildren(children); + var clockwise = children[0].isClockwise(); + for (var i = 1, l = children.length; i < l; i++) { + var point = children[i].getInteriorPoint(), + counters = 0; + for (var j = i - 1; j >= 0; j--) { + if (children[j].contains(point)) + counters++; + } + children[i].setClockwise(counters % 2 === 0 && clockwise); + } + } + return this; + } +}); + +var PathIterator = Base.extend({ + _class: 'PathIterator', + + initialize: function(path, maxRecursion, tolerance, matrix) { + var curves = [], + parts = [], + length = 0, + minDifference = 1 / (maxRecursion || 32), + segments = path._segments, + segment1 = segments[0], + segment2; + + function addCurve(segment1, segment2) { + var curve = Curve.getValues(segment1, segment2, matrix); + curves.push(curve); + computeParts(curve, segment1._index, 0, 1); + } + + function computeParts(curve, index, minT, maxT) { + if ((maxT - minT) > minDifference + && !Curve.isFlatEnough(curve, tolerance || 0.25)) { + var split = Curve.subdivide(curve, 0.5), + halfT = (minT + maxT) / 2; + computeParts(split[0], index, minT, halfT); + computeParts(split[1], index, halfT, maxT); + } else { + var x = curve[6] - curve[0], + y = curve[7] - curve[1], + dist = Math.sqrt(x * x + y * y); + if (dist > 1e-6) { + length += dist; + parts.push({ + offset: length, + value: maxT, + index: index + }); + } + } + } + + for (var i = 1, l = segments.length; i < l; i++) { + segment2 = segments[i]; + addCurve(segment1, segment2); + segment1 = segment2; + } + if (path._closed) + addCurve(segment2, segments[0]); + + this.curves = curves; + this.parts = parts; + this.length = length; + this.index = 0; + }, + + getParameterAt: function(offset) { + var i, j = this.index; + for (;;) { + i = j; + if (j == 0 || this.parts[--j].offset < offset) + break; + } + for (var l = this.parts.length; i < l; i++) { + var part = this.parts[i]; + if (part.offset >= offset) { + this.index = i; + var prev = this.parts[i - 1]; + var prevVal = prev && prev.index == part.index ? prev.value : 0, + prevLen = prev ? prev.offset : 0; + return { + value: prevVal + (part.value - prevVal) + * (offset - prevLen) / (part.offset - prevLen), + index: part.index + }; + } + } + var part = this.parts[this.parts.length - 1]; + return { + value: 1, + index: part.index + }; + }, + + drawPart: function(ctx, from, to) { + from = this.getParameterAt(from); + to = this.getParameterAt(to); + for (var i = from.index; i <= to.index; i++) { + var curve = Curve.getPart(this.curves[i], + i == from.index ? from.value : 0, + i == to.index ? to.value : 1); + if (i == from.index) + ctx.moveTo(curve[0], curve[1]); + ctx.bezierCurveTo.apply(ctx, curve.slice(2)); + } + } +}, Base.each(Curve.evaluateMethods, + function(name) { + this[name + 'At'] = function(offset, weighted) { + var param = this.getParameterAt(offset); + return Curve[name](this.curves[param.index], param.value, weighted); + }; + }, {}) +); + +var PathFitter = Base.extend({ + initialize: function(path, error) { + var points = this.points = [], + segments = path._segments, + prev; + for (var i = 0, l = segments.length; i < l; i++) { + var point = segments[i].point.clone(); + if (!prev || !prev.equals(point)) { + points.push(point); + prev = point; + } + } + + if (path._closed) { + this.closed = true; + points.unshift(points[points.length - 1]); + points.push(points[1]); + } + + this.error = error; + }, + + fit: function() { + var points = this.points, + length = points.length, + segments = this.segments = length > 0 + ? [new Segment(points[0])] : []; + if (length > 1) + this.fitCubic(0, length - 1, + points[1].subtract(points[0]).normalize(), + points[length - 2].subtract(points[length - 1]).normalize()); + + if (this.closed) { + segments.shift(); + segments.pop(); + } + + return segments; + }, + + fitCubic: function(first, last, tan1, tan2) { + if (last - first == 1) { + var pt1 = this.points[first], + pt2 = this.points[last], + dist = pt1.getDistance(pt2) / 3; + this.addCurve([pt1, pt1.add(tan1.normalize(dist)), + pt2.add(tan2.normalize(dist)), pt2]); + return; + } + var uPrime = this.chordLengthParameterize(first, last), + maxError = Math.max(this.error, this.error * this.error), + split, + parametersInOrder = true; + for (var i = 0; i <= 4; i++) { + var curve = this.generateBezier(first, last, uPrime, tan1, tan2); + var max = this.findMaxError(first, last, curve, uPrime); + if (max.error < this.error && parametersInOrder) { + this.addCurve(curve); + return; + } + split = max.index; + if (max.error >= maxError) + break; + parametersInOrder = this.reparameterize(first, last, uPrime, curve); + maxError = max.error; + } + var V1 = this.points[split - 1].subtract(this.points[split]), + V2 = this.points[split].subtract(this.points[split + 1]), + tanCenter = V1.add(V2).divide(2).normalize(); + this.fitCubic(first, split, tan1, tanCenter); + this.fitCubic(split, last, tanCenter.negate(), tan2); + }, + + addCurve: function(curve) { + var prev = this.segments[this.segments.length - 1]; + prev.setHandleOut(curve[1].subtract(curve[0])); + this.segments.push( + new Segment(curve[3], curve[2].subtract(curve[3]))); + }, + + generateBezier: function(first, last, uPrime, tan1, tan2) { + var epsilon = 1e-12, + pt1 = this.points[first], + pt2 = this.points[last], + C = [[0, 0], [0, 0]], + X = [0, 0]; + + for (var i = 0, l = last - first + 1; i < l; i++) { + var u = uPrime[i], + t = 1 - u, + b = 3 * u * t, + b0 = t * t * t, + b1 = b * t, + b2 = b * u, + b3 = u * u * u, + a1 = tan1.normalize(b1), + a2 = tan2.normalize(b2), + tmp = this.points[first + i] + .subtract(pt1.multiply(b0 + b1)) + .subtract(pt2.multiply(b2 + b3)); + C[0][0] += a1.dot(a1); + C[0][1] += a1.dot(a2); + C[1][0] = C[0][1]; + C[1][1] += a2.dot(a2); + X[0] += a1.dot(tmp); + X[1] += a2.dot(tmp); + } + + var detC0C1 = C[0][0] * C[1][1] - C[1][0] * C[0][1], + alpha1, alpha2; + if (Math.abs(detC0C1) > epsilon) { + var detC0X = C[0][0] * X[1] - C[1][0] * X[0], + detXC1 = X[0] * C[1][1] - X[1] * C[0][1]; + alpha1 = detXC1 / detC0C1; + alpha2 = detC0X / detC0C1; + } else { + var c0 = C[0][0] + C[0][1], + c1 = C[1][0] + C[1][1]; + if (Math.abs(c0) > epsilon) { + alpha1 = alpha2 = X[0] / c0; + } else if (Math.abs(c1) > epsilon) { + alpha1 = alpha2 = X[1] / c1; + } else { + alpha1 = alpha2 = 0; + } + } + + var segLength = pt2.getDistance(pt1), + eps = epsilon * segLength, + handle1, + handle2; + if (alpha1 < eps || alpha2 < eps) { + alpha1 = alpha2 = segLength / 3; + } else { + var line = pt2.subtract(pt1); + handle1 = tan1.normalize(alpha1); + handle2 = tan2.normalize(alpha2); + if (handle1.dot(line) - handle2.dot(line) > segLength * segLength) { + alpha1 = alpha2 = segLength / 3; + handle1 = handle2 = null; + } + } + + return [pt1, pt1.add(handle1 || tan1.normalize(alpha1)), + pt2.add(handle2 || tan2.normalize(alpha2)), pt2]; + }, + + reparameterize: function(first, last, u, curve) { + for (var i = first; i <= last; i++) { + u[i - first] = this.findRoot(curve, this.points[i], u[i - first]); + } + for (var i = 1, l = u.length; i < l; i++) { + if (u[i] <= u[i - 1]) + return false; + } + return true; + }, + + findRoot: function(curve, point, u) { + var curve1 = [], + curve2 = []; + for (var i = 0; i <= 2; i++) { + curve1[i] = curve[i + 1].subtract(curve[i]).multiply(3); + } + for (var i = 0; i <= 1; i++) { + curve2[i] = curve1[i + 1].subtract(curve1[i]).multiply(2); + } + var pt = this.evaluate(3, curve, u), + pt1 = this.evaluate(2, curve1, u), + pt2 = this.evaluate(1, curve2, u), + diff = pt.subtract(point), + df = pt1.dot(pt1) + diff.dot(pt2); + if (Math.abs(df) < 1e-6) + return u; + return u - diff.dot(pt1) / df; + }, + + evaluate: function(degree, curve, t) { + var tmp = curve.slice(); + for (var i = 1; i <= degree; i++) { + for (var j = 0; j <= degree - i; j++) { + tmp[j] = tmp[j].multiply(1 - t).add(tmp[j + 1].multiply(t)); + } + } + return tmp[0]; + }, + + chordLengthParameterize: function(first, last) { + var u = [0]; + for (var i = first + 1; i <= last; i++) { + u[i - first] = u[i - first - 1] + + this.points[i].getDistance(this.points[i - 1]); + } + for (var i = 1, m = last - first; i <= m; i++) { + u[i] /= u[m]; + } + return u; + }, + + findMaxError: function(first, last, curve, u) { + var index = Math.floor((last - first + 1) / 2), + maxDist = 0; + for (var i = first + 1; i < last; i++) { + var P = this.evaluate(3, curve, u[i - first]); + var v = P.subtract(this.points[i]); + var dist = v.x * v.x + v.y * v.y; + if (dist >= maxDist) { + maxDist = dist; + index = i; + } + } + return { + error: maxDist, + index: index + }; + } +}); + +var TextItem = Item.extend({ + _class: 'TextItem', + _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, + _serializeFields: { + content: null + }, + _boundsGetter: 'getBounds', + + initialize: function TextItem(arg) { + this._content = ''; + this._lines = []; + var hasProps = arg && Base.isPlainObject(arg) + && arg.x === undefined && arg.y === undefined; + this._initialize(hasProps && arg, !hasProps && Point.read(arguments)); + }, + + _equals: function(item) { + return this._content === item._content; + }, + + _clone: function _clone(copy, insert, includeMatrix) { + copy.setContent(this._content); + return _clone.base.call(this, copy, insert, includeMatrix); + }, + + getContent: function() { + return this._content; + }, + + setContent: function(content) { + this._content = '' + content; + this._lines = this._content.split(/\r\n|\n|\r/mg); + this._changed(265); + }, + + isEmpty: function() { + return !this._content; + }, + + getCharacterStyle: '#getStyle', + setCharacterStyle: '#setStyle', + + getParagraphStyle: '#getStyle', + setParagraphStyle: '#setStyle' +}); + +var PointText = TextItem.extend({ + _class: 'PointText', + + initialize: function PointText() { + TextItem.apply(this, arguments); + }, + + clone: function(insert) { + return this._clone(new PointText(Item.NO_INSERT), insert); + }, + + getPoint: function() { + var point = this._matrix.getTranslation(); + return new LinkedPoint(point.x, point.y, this, 'setPoint'); + }, + + setPoint: function() { + var point = Point.read(arguments); + this.translate(point.subtract(this._matrix.getTranslation())); + }, + + _draw: function(ctx) { + if (!this._content) + return; + this._setStyles(ctx); + var style = this._style, + lines = this._lines, + leading = style.getLeading(), + shadowColor = ctx.shadowColor; + ctx.font = style.getFontStyle(); + ctx.textAlign = style.getJustification(); + for (var i = 0, l = lines.length; i < l; i++) { + ctx.shadowColor = shadowColor; + var line = lines[i]; + if (style.hasFill()) { + ctx.fillText(line, 0, 0); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (style.hasStroke()) + ctx.strokeText(line, 0, 0); + ctx.translate(0, leading); + } + }, + + _getBounds: function(getter, matrix) { + var style = this._style, + lines = this._lines, + numLines = lines.length, + justification = style.getJustification(), + leading = style.getLeading(), + width = this.getView().getTextWidth(style.getFontStyle(), lines), + x = 0; + if (justification !== 'left') + x -= width / (justification === 'center' ? 2: 1); + var bounds = new Rectangle(x, + numLines ? - 0.75 * leading : 0, + width, numLines * leading); + return matrix ? matrix._transformBounds(bounds, bounds) : bounds; + } +}); + +var Color = Base.extend(new function() { + var types = { + gray: ['gray'], + rgb: ['red', 'green', 'blue'], + hsb: ['hue', 'saturation', 'brightness'], + hsl: ['hue', 'saturation', 'lightness'], + gradient: ['gradient', 'origin', 'destination', 'highlight'] + }; + + var componentParsers = {}, + colorCache = {}, + colorCtx; + + function fromCSS(string) { + var match = string.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/), + components; + if (match) { + components = [0, 0, 0]; + for (var i = 0; i < 3; i++) { + var value = match[i + 1]; + components[i] = parseInt(value.length == 1 + ? value + value : value, 16) / 255; + } + } else if (match = string.match(/^rgba?\((.*)\)$/)) { + components = match[1].split(','); + for (var i = 0, l = components.length; i < l; i++) { + var value = +components[i]; + components[i] = i < 3 ? value / 255 : value; + } + } else { + var cached = colorCache[string]; + if (!cached) { + if (!colorCtx) { + colorCtx = CanvasProvider.getContext(1, 1); + colorCtx.globalCompositeOperation = 'copy'; + } + colorCtx.fillStyle = 'rgba(0,0,0,0)'; + colorCtx.fillStyle = string; + colorCtx.fillRect(0, 0, 1, 1); + var data = colorCtx.getImageData(0, 0, 1, 1).data; + cached = colorCache[string] = [ + data[0] / 255, + data[1] / 255, + data[2] / 255 + ]; + } + components = cached.slice(); + } + return components; + } + + var hsbIndices = [ + [0, 3, 1], + [2, 0, 1], + [1, 0, 3], + [1, 2, 0], + [3, 1, 0], + [0, 1, 2] + ]; + + var converters = { + 'rgb-hsb': function(r, g, b) { + var max = Math.max(r, g, b), + min = Math.min(r, g, b), + delta = max - min, + h = delta === 0 ? 0 + : ( max == r ? (g - b) / delta + (g < b ? 6 : 0) + : max == g ? (b - r) / delta + 2 + : (r - g) / delta + 4) * 60; + return [h, max === 0 ? 0 : delta / max, max]; + }, + + 'hsb-rgb': function(h, s, b) { + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), + f = h - i, + i = hsbIndices[i], + v = [ + b, + b * (1 - s), + b * (1 - s * f), + b * (1 - s * (1 - f)) + ]; + return [v[i[0]], v[i[1]], v[i[2]]]; + }, + + 'rgb-hsl': function(r, g, b) { + var max = Math.max(r, g, b), + min = Math.min(r, g, b), + delta = max - min, + achromatic = delta === 0, + h = achromatic ? 0 + : ( max == r ? (g - b) / delta + (g < b ? 6 : 0) + : max == g ? (b - r) / delta + 2 + : (r - g) / delta + 4) * 60, + l = (max + min) / 2, + s = achromatic ? 0 : l < 0.5 + ? delta / (max + min) + : delta / (2 - max - min); + return [h, s, l]; + }, + + 'hsl-rgb': function(h, s, l) { + h = (((h / 360) % 1) + 1) % 1; + if (s === 0) + return [l, l, l]; + var t3s = [ h + 1 / 3, h, h - 1 / 3 ], + t2 = l < 0.5 ? l * (1 + s) : l + s - l * s, + t1 = 2 * l - t2, + c = []; + for (var i = 0; i < 3; i++) { + var t3 = t3s[i]; + if (t3 < 0) t3 += 1; + if (t3 > 1) t3 -= 1; + c[i] = 6 * t3 < 1 + ? t1 + (t2 - t1) * 6 * t3 + : 2 * t3 < 1 + ? t2 + : 3 * t3 < 2 + ? t1 + (t2 - t1) * ((2 / 3) - t3) * 6 + : t1; + } + return c; + }, + + 'rgb-gray': function(r, g, b) { + return [r * 0.2989 + g * 0.587 + b * 0.114]; + }, + + 'gray-rgb': function(g) { + return [g, g, g]; + }, + + 'gray-hsb': function(g) { + return [0, 0, g]; + }, + + 'gray-hsl': function(g) { + return [0, 0, g]; + }, + + 'gradient-rgb': function() { + return []; + }, + + 'rgb-gradient': function() { + return []; + } + + }; + + return Base.each(types, function(properties, type) { + componentParsers[type] = []; + Base.each(properties, function(name, index) { + var part = Base.capitalize(name), + hasOverlap = /^(hue|saturation)$/.test(name), + parser = componentParsers[type][index] = name === 'gradient' + ? function(value) { + var current = this._components[0]; + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); + if (current !== value) { + if (current) + current._removeOwner(this); + if (value) + value._addOwner(this); + } + return value; + } + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); + } + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; + + this['get' + part] = function() { + return this._type === type + || hasOverlap && /^hs[bl]$/.test(this._type) + ? this._components[index] + : this._convert(type)[index]; + }; + + this['set' + part] = function(value) { + if (this._type !== type + && !(hasOverlap && /^hs[bl]$/.test(this._type))) { + this._components = this._convert(type); + this._properties = types[type]; + this._type = type; + } + this._components[index] = parser.call(this, value); + this._changed(); + }; + }, this); + }, { + _class: 'Color', + _readIndex: true, + + initialize: function Color(arg) { + var slice = Array.prototype.slice, + args = arguments, + read = 0, + type, + components, + alpha, + values; + if (Array.isArray(arg)) { + args = arg; + arg = args[0]; + } + var argType = arg != null && typeof arg; + if (argType === 'string' && arg in types) { + type = arg; + arg = args[1]; + if (Array.isArray(arg)) { + components = arg; + alpha = args[2]; + } else { + if (this.__read) + read = 1; + args = slice.call(args, 1); + argType = typeof arg; + } + } + if (!components) { + values = argType === 'number' + ? args + : argType === 'object' && arg.length != null + ? arg + : null; + if (values) { + if (!type) + type = values.length >= 3 + ? 'rgb' + : 'gray'; + var length = types[type].length; + alpha = values[length]; + if (this.__read) + read += values === arguments + ? length + (alpha != null ? 1 : 0) + : 1; + if (values.length > length) + values = slice.call(values, 0, length); + } else if (argType === 'string') { + type = 'rgb'; + components = fromCSS(arg); + if (components.length === 4) { + alpha = components[3]; + components.length--; + } + } else if (argType === 'object') { + if (arg.constructor === Color) { + type = arg._type; + components = arg._components.slice(); + alpha = arg._alpha; + if (type === 'gradient') { + for (var i = 1, l = components.length; i < l; i++) { + var point = components[i]; + if (point) + components[i] = point.clone(); + } + } + } else if (arg.constructor === Gradient) { + type = 'gradient'; + values = args; + } else { + type = 'hue' in arg + ? 'lightness' in arg + ? 'hsl' + : 'hsb' + : 'gradient' in arg || 'stops' in arg + || 'radial' in arg + ? 'gradient' + : 'gray' in arg + ? 'gray' + : 'rgb'; + var properties = types[type], + parsers = componentParsers[type]; + this._components = components = []; + for (var i = 0, l = properties.length; i < l; i++) { + var value = arg[properties[i]]; + if (value == null && i === 0 && type === 'gradient' + && 'stops' in arg) { + value = { + stops: arg.stops, + radial: arg.radial + }; + } + value = parsers[i].call(this, value); + if (value != null) + components[i] = value; + } + alpha = arg.alpha; + } + } + if (this.__read && type) + read = 1; + } + this._type = type || 'rgb'; + this._id = UID.get(Color); + if (!components) { + this._components = components = []; + var parsers = componentParsers[this._type]; + for (var i = 0, l = parsers.length; i < l; i++) { + var value = parsers[i].call(this, values && values[i]); + if (value != null) + components[i] = value; + } + } + this._components = components; + this._properties = types[this._type]; + this._alpha = alpha; + if (this.__read) + this.__read = read; + }, + + _serialize: function(options, dictionary) { + var components = this.getComponents(); + return Base.serialize( + /^(gray|rgb)$/.test(this._type) + ? components + : [this._type].concat(components), + options, true, dictionary); + }, + + _changed: function() { + this._canvasStyle = null; + if (this._owner) + this._owner._changed(65); + }, + + _convert: function(type) { + var converter; + return this._type === type + ? this._components.slice() + : (converter = converters[this._type + '-' + type]) + ? converter.apply(this, this._components) + : converters['rgb-' + type].apply(this, + converters[this._type + '-rgb'].apply(this, + this._components)); + }, + + convert: function(type) { + return new Color(type, this._convert(type), this._alpha); + }, + + getType: function() { + return this._type; + }, + + setType: function(type) { + this._components = this._convert(type); + this._properties = types[type]; + this._type = type; + }, + + getComponents: function() { + var components = this._components.slice(); + if (this._alpha != null) + components.push(this._alpha); + return components; + }, + + getAlpha: function() { + return this._alpha != null ? this._alpha : 1; + }, + + setAlpha: function(alpha) { + this._alpha = alpha == null ? null : Math.min(Math.max(alpha, 0), 1); + this._changed(); + }, + + hasAlpha: function() { + return this._alpha != null; + }, + + equals: function(color) { + var col = Base.isPlainValue(color, true) + ? Color.read(arguments) + : color; + return col === this || col && this._class === col._class + && this._type === col._type + && this._alpha === col._alpha + && Base.equals(this._components, col._components) + || false; + }, + + toString: function() { + var properties = this._properties, + parts = [], + isGradient = this._type === 'gradient', + f = Formatter.instance; + for (var i = 0, l = properties.length; i < l; i++) { + var value = this._components[i]; + if (value != null) + parts.push(properties[i] + ': ' + + (isGradient ? value : f.number(value))); + } + if (this._alpha != null) + parts.push('alpha: ' + f.number(this._alpha)); + return '{ ' + parts.join(', ') + ' }'; + }, + + toCSS: function(hex) { + var components = this._convert('rgb'), + alpha = hex || this._alpha == null ? 1 : this._alpha; + function convert(val) { + return Math.round((val < 0 ? 0 : val > 1 ? 1 : val) * 255); + } + components = [ + convert(components[0]), + convert(components[1]), + convert(components[2]) + ]; + if (alpha < 1) + components.push(alpha < 0 ? 0 : alpha); + return hex + ? '#' + ((1 << 24) + (components[0] << 16) + + (components[1] << 8) + + components[2]).toString(16).slice(1) + : (components.length == 4 ? 'rgba(' : 'rgb(') + + components.join(',') + ')'; + }, + + toCanvasStyle: function(ctx) { + if (this._canvasStyle) + return this._canvasStyle; + if (this._type !== 'gradient') + return this._canvasStyle = this.toCSS(); + var components = this._components, + gradient = components[0], + stops = gradient._stops, + origin = components[1], + destination = components[2], + canvasGradient; + if (gradient._radial) { + var radius = destination.getDistance(origin), + highlight = components[3]; + if (highlight) { + var vector = highlight.subtract(origin); + if (vector.getLength() > radius) + highlight = origin.add(vector.normalize(radius - 0.1)); + } + var start = highlight || origin; + canvasGradient = ctx.createRadialGradient(start.x, start.y, + 0, origin.x, origin.y, radius); + } else { + canvasGradient = ctx.createLinearGradient(origin.x, origin.y, + destination.x, destination.y); + } + for (var i = 0, l = stops.length; i < l; i++) { + var stop = stops[i]; + canvasGradient.addColorStop(stop._rampPoint, + stop._color.toCanvasStyle()); + } + return this._canvasStyle = canvasGradient; + }, + + transform: function(matrix) { + if (this._type === 'gradient') { + var components = this._components; + for (var i = 1, l = components.length; i < l; i++) { + var point = components[i]; + matrix._transformPoint(point, point, true); + } + this._changed(); + } + }, + + statics: { + _types: types, + + random: function() { + var random = Math.random; + return new Color(random(), random(), random()); + } + } + }); +}, +new function() { + var operators = { + add: function(a, b) { + return a + b; + }, + + subtract: function(a, b) { + return a - b; + }, + + multiply: function(a, b) { + return a * b; + }, + + divide: function(a, b) { + return a / b; + } + }; + + return Base.each(operators, function(operator, name) { + this[name] = function(color) { + color = Color.read(arguments); + var type = this._type, + components1 = this._components, + components2 = color._convert(type); + for (var i = 0, l = components1.length; i < l; i++) + components2[i] = operator(components1[i], components2[i]); + return new Color(type, components2, + this._alpha != null + ? operator(this._alpha, color.getAlpha()) + : null); + }; + }, { + }); +}); + +var Gradient = Base.extend({ + _class: 'Gradient', + + initialize: function Gradient(stops, radial) { + this._id = UID.get(); + if (stops && this._set(stops)) + stops = radial = null; + if (!this._stops) + this.setStops(stops || ['white', 'black']); + if (this._radial == null) + this.setRadial(typeof radial === 'string' && radial === 'radial' + || radial || false); + }, + + _serialize: function(options, dictionary) { + return dictionary.add(this, function() { + return Base.serialize([this._stops, this._radial], + options, true, dictionary); + }); + }, + + _changed: function() { + for (var i = 0, l = this._owners && this._owners.length; i < l; i++) + this._owners[i]._changed(); + }, + + _addOwner: function(color) { + if (!this._owners) + this._owners = []; + this._owners.push(color); + }, + + _removeOwner: function(color) { + var index = this._owners ? this._owners.indexOf(color) : -1; + if (index != -1) { + this._owners.splice(index, 1); + if (this._owners.length === 0) + this._owners = undefined; + } + }, + + clone: function() { + var stops = []; + for (var i = 0, l = this._stops.length; i < l; i++) + stops[i] = this._stops[i].clone(); + return new Gradient(stops, this._radial); + }, + + getStops: function() { + return this._stops; + }, + + setStops: function(stops) { + if (this.stops) { + for (var i = 0, l = this._stops.length; i < l; i++) + this._stops[i]._owner = undefined; + } + if (stops.length < 2) + throw new Error( + 'Gradient stop list needs to contain at least two stops.'); + this._stops = GradientStop.readAll(stops, 0, { clone: true }); + for (var i = 0, l = this._stops.length; i < l; i++) { + var stop = this._stops[i]; + stop._owner = this; + if (stop._defaultRamp) + stop.setRampPoint(i / (l - 1)); + } + this._changed(); + }, + + getRadial: function() { + return this._radial; + }, + + setRadial: function(radial) { + this._radial = radial; + this._changed(); + }, + + equals: function(gradient) { + if (gradient === this) + return true; + if (gradient && this._class === gradient._class + && this._stops.length === gradient._stops.length) { + for (var i = 0, l = this._stops.length; i < l; i++) { + if (!this._stops[i].equals(gradient._stops[i])) + return false; + } + return true; + } + return false; + } +}); + +var GradientStop = Base.extend({ + _class: 'GradientStop', + + initialize: function GradientStop(arg0, arg1) { + if (arg0) { + var color, rampPoint; + if (arg1 === undefined && Array.isArray(arg0)) { + color = arg0[0]; + rampPoint = arg0[1]; + } else if (arg0.color) { + color = arg0.color; + rampPoint = arg0.rampPoint; + } else { + color = arg0; + rampPoint = arg1; + } + this.setColor(color); + this.setRampPoint(rampPoint); + } + }, + + clone: function() { + return new GradientStop(this._color.clone(), this._rampPoint); + }, + + _serialize: function(options, dictionary) { + return Base.serialize([this._color, this._rampPoint], options, true, + dictionary); + }, + + _changed: function() { + if (this._owner) + this._owner._changed(65); + }, + + getRampPoint: function() { + return this._rampPoint; + }, + + setRampPoint: function(rampPoint) { + this._defaultRamp = rampPoint == null; + this._rampPoint = rampPoint || 0; + this._changed(); + }, + + getColor: function() { + return this._color; + }, + + setColor: function(color) { + this._color = Color.read(arguments); + if (this._color === color) + this._color = color.clone(); + this._color._owner = this; + this._changed(); + }, + + equals: function(stop) { + return stop === this || stop && this._class === stop._class + && this._color.equals(stop._color) + && this._rampPoint == stop._rampPoint + || false; + } +}); + +var Style = Base.extend(new function() { + var defaults = { + fillColor: undefined, + strokeColor: undefined, + strokeWidth: 1, + strokeCap: 'butt', + strokeJoin: 'miter', + strokeScaling: true, + miterLimit: 10, + dashOffset: 0, + dashArray: [], + windingRule: 'nonzero', + shadowColor: undefined, + shadowBlur: 0, + shadowOffset: new Point(), + selectedColor: undefined, + fontFamily: 'sans-serif', + fontWeight: 'normal', + fontSize: 12, + font: 'sans-serif', + leading: null, + justification: 'left' + }; + + var flags = { + strokeWidth: 97, + strokeCap: 97, + strokeJoin: 97, + strokeScaling: 105, + miterLimit: 97, + fontFamily: 9, + fontWeight: 9, + fontSize: 9, + font: 9, + leading: 9, + justification: 9 + }; + + var item = { beans: true }, + fields = { + _defaults: defaults, + _textDefaults: new Base(defaults, { + fillColor: new Color() + }), + beans: true + }; + + Base.each(defaults, function(value, key) { + var isColor = /Color$/.test(key), + isPoint = key === 'shadowOffset', + part = Base.capitalize(key), + flag = flags[key], + set = 'set' + part, + get = 'get' + part; + + fields[set] = function(value) { + var owner = this._owner, + children = owner && owner._children; + if (children && children.length > 0 + && !(owner instanceof CompoundPath)) { + for (var i = 0, l = children.length; i < l; i++) + children[i]._style[set](value); + } else { + var old = this._values[key]; + if (old !== value) { + if (isColor) { + if (old) + old._owner = undefined; + if (value && value.constructor === Color) { + if (value._owner) + value = value.clone(); + value._owner = owner; + } + } + this._values[key] = value; + if (owner) + owner._changed(flag || 65); + } + } + }; + + fields[get] = function(_dontMerge) { + var owner = this._owner, + children = owner && owner._children, + value; + if (!children || children.length === 0 || _dontMerge + || owner instanceof CompoundPath) { + var value = this._values[key]; + if (value === undefined) { + value = this._defaults[key]; + if (value && value.clone) + value = value.clone(); + } else { + var ctor = isColor ? Color : isPoint ? Point : null; + if (ctor && !(value && value.constructor === ctor)) { + this._values[key] = value = ctor.read([value], 0, + { readNull: true, clone: true }); + if (value && isColor) + value._owner = owner; + } + } + return value; + } + for (var i = 0, l = children.length; i < l; i++) { + var childValue = children[i]._style[get](); + if (i === 0) { + value = childValue; + } else if (!Base.equals(value, childValue)) { + return undefined; + } + } + return value; + }; + + item[get] = function(_dontMerge) { + return this._style[get](_dontMerge); + }; + + item[set] = function(value) { + this._style[set](value); + }; + }); + + Item.inject(item); + return fields; +}, { + _class: 'Style', + + initialize: function Style(style, _owner, _project) { + this._values = {}; + this._owner = _owner; + this._project = _owner && _owner._project || _project || paper.project; + if (_owner instanceof TextItem) + this._defaults = this._textDefaults; + if (style) + this.set(style); + }, + + set: function(style) { + var isStyle = style instanceof Style, + values = isStyle ? style._values : style; + if (values) { + for (var key in values) { + if (key in this._defaults) { + var value = values[key]; + this[key] = value && isStyle && value.clone + ? value.clone() : value; + } + } + } + }, + + equals: function(style) { + return style === this || style && this._class === style._class + && Base.equals(this._values, style._values) + || false; + }, + + hasFill: function() { + return !!this.getFillColor(); + }, + + hasStroke: function() { + return !!this.getStrokeColor() && this.getStrokeWidth() > 0; + }, + + hasShadow: function() { + return !!this.getShadowColor() && this.getShadowBlur() > 0; + }, + + getView: function() { + return this._project.getView(); + }, + + getFontStyle: function() { + var fontSize = this.getFontSize(); + return this.getFontWeight() + + ' ' + fontSize + (/[a-z]/i.test(fontSize + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + setFont: '#setFontFamily', + + getLeading: function getLeading() { + var leading = getLeading.base.call(this), + fontSize = this.getFontSize(); + if (/pt|em|%|px/.test(fontSize)) + fontSize = this.getView().getPixelSize(fontSize); + return leading != null ? leading : fontSize * 1.2; + } + +}); + +var DomElement = new function() { + function handlePrefix(el, name, set, value) { + var prefixes = ['', 'webkit', 'moz', 'Moz', 'ms', 'o'], + suffix = name[0].toUpperCase() + name.substring(1); + for (var i = 0; i < 6; i++) { + var prefix = prefixes[i], + key = prefix ? prefix + suffix : name; + if (key in el) { + if (set) { + el[key] = value; + } else { + return el[key]; + } + break; + } + } + } + + return { + getStyles: function(el) { + var doc = el && el.nodeType !== 9 ? el.ownerDocument : el, + view = doc && doc.defaultView; + return view && view.getComputedStyle(el, ''); + }, + + getBounds: function(el, viewport) { + var doc = el.ownerDocument, + body = doc.body, + html = doc.documentElement, + rect; + try { + rect = el.getBoundingClientRect(); + } catch (e) { + rect = { left: 0, top: 0, width: 0, height: 0 }; + } + var x = rect.left - (html.clientLeft || body.clientLeft || 0), + y = rect.top - (html.clientTop || body.clientTop || 0); + if (!viewport) { + var view = doc.defaultView; + x += view.pageXOffset || html.scrollLeft || body.scrollLeft; + y += view.pageYOffset || html.scrollTop || body.scrollTop; + } + return new Rectangle(x, y, rect.width, rect.height); + }, + + getViewportBounds: function(el) { + var doc = el.ownerDocument, + view = doc.defaultView, + html = doc.documentElement; + return new Rectangle(0, 0, + view.innerWidth || html.clientWidth, + view.innerHeight || html.clientHeight + ); + }, + + getOffset: function(el, viewport) { + return DomElement.getBounds(el, viewport).getPoint(); + }, + + getSize: function(el) { + return DomElement.getBounds(el, true).getSize(); + }, + + isInvisible: function(el) { + return DomElement.getSize(el).equals(new Size(0, 0)); + }, + + isInView: function(el) { + return !DomElement.isInvisible(el) + && DomElement.getViewportBounds(el).intersects( + DomElement.getBounds(el, true)); + }, + + getPrefixed: function(el, name) { + return handlePrefix(el, name); + }, + + setPrefixed: function(el, name, value) { + if (typeof name === 'object') { + for (var key in name) + handlePrefix(el, key, true, name[key]); + } else { + handlePrefix(el, name, true, value); + } + } + }; +}; + +var DomEvent = { + add: function(el, events) { + for (var type in events) { + var func = events[type], + parts = type.split(/[\s,]+/g); + for (var i = 0, l = parts.length; i < l; i++) + el.addEventListener(parts[i], func, false); + } + }, + + remove: function(el, events) { + for (var type in events) { + var func = events[type], + parts = type.split(/[\s,]+/g); + for (var i = 0, l = parts.length; i < l; i++) + el.removeEventListener(parts[i], func, false); + } + }, + + getPoint: function(event) { + var pos = event.targetTouches + ? event.targetTouches.length + ? event.targetTouches[0] + : event.changedTouches[0] + : event; + return new Point( + pos.pageX || pos.clientX + document.documentElement.scrollLeft, + pos.pageY || pos.clientY + document.documentElement.scrollTop + ); + }, + + getTarget: function(event) { + return event.target || event.srcElement; + }, + + getRelatedTarget: function(event) { + return event.relatedTarget || event.toElement; + }, + + getOffset: function(event, target) { + return DomEvent.getPoint(event).subtract(DomElement.getOffset( + target || DomEvent.getTarget(event))); + }, + + stop: function(event) { + event.stopPropagation(); + event.preventDefault(); + } +}; + +DomEvent.requestAnimationFrame = new function() { + var nativeRequest = DomElement.getPrefixed(window, 'requestAnimationFrame'), + requested = false, + callbacks = [], + focused = true, + timer; + + DomEvent.add(window, { + focus: function() { + focused = true; + }, + blur: function() { + focused = false; + } + }); + + function handleCallbacks() { + for (var i = callbacks.length - 1; i >= 0; i--) { + var entry = callbacks[i], + func = entry[0], + el = entry[1]; + if (!el || (PaperScope.getAttribute(el, 'keepalive') == 'true' + || focused) && DomElement.isInView(el)) { + callbacks.splice(i, 1); + func(); + } + } + if (nativeRequest) { + if (callbacks.length) { + nativeRequest(handleCallbacks); + } else { + requested = false; + } + } + } + + return function(callback, element) { + callbacks.push([callback, element]); + if (nativeRequest) { + if (!requested) { + nativeRequest(handleCallbacks); + requested = true; + } + } else if (!timer) { + timer = setInterval(handleCallbacks, 1000 / 60); + } + }; +}; + +var View = Base.extend(Emitter, { + _class: 'View', + + initialize: function View(project, element) { + this._project = project; + this._scope = project._scope; + this._element = element; + var size; + if (!this._pixelRatio) + this._pixelRatio = window.devicePixelRatio || 1; + this._id = element.getAttribute('id'); + if (this._id == null) + element.setAttribute('id', this._id = 'view-' + View._id++); + DomEvent.add(element, this._viewEvents); + var none = 'none'; + DomElement.setPrefixed(element.style, { + userSelect: none, + touchAction: none, + touchCallout: none, + contentZooming: none, + userDrag: none, + tapHighlightColor: 'rgba(0,0,0,0)' + }); + + function getSize(name) { + return element[name] || parseInt(element.getAttribute(name), 10); + }; + + function getCanvasSize() { + var size = DomElement.getSize(element); + return size.isNaN() || size.isZero() + ? new Size(getSize('width'), getSize('height')) + : size; + }; + + if (PaperScope.hasAttribute(element, 'resize')) { + var that = this; + DomEvent.add(window, this._windowEvents = { + resize: function() { + that.setViewSize(getCanvasSize()); + } + }); + } + this._setViewSize(size = getCanvasSize()); + if (PaperScope.hasAttribute(element, 'stats') + && typeof Stats !== 'undefined') { + this._stats = new Stats(); + var stats = this._stats.domElement, + style = stats.style, + offset = DomElement.getOffset(element); + style.position = 'absolute'; + style.left = offset.x + 'px'; + style.top = offset.y + 'px'; + document.body.appendChild(stats); + } + View._views.push(this); + View._viewsById[this._id] = this; + this._viewSize = size; + (this._matrix = new Matrix())._owner = this; + this._zoom = 1; + if (!View._focused) + View._focused = this; + this._frameItems = {}; + this._frameItemCount = 0; + }, + + remove: function() { + if (!this._project) + return false; + if (View._focused === this) + View._focused = null; + View._views.splice(View._views.indexOf(this), 1); + delete View._viewsById[this._id]; + if (this._project._view === this) + this._project._view = null; + DomEvent.remove(this._element, this._viewEvents); + DomEvent.remove(window, this._windowEvents); + this._element = this._project = null; + this.off('frame'); + this._animate = false; + this._frameItems = {}; + return true; + }, + + _events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove'], + function(name) { + this[name] = { + install: function(type) { + this._installEvent(type); + }, + + uninstall: function(type) { + this._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.play(); + }, + + uninstall: function() { + this.pause(); + } + } + } + ), + + _animate: false, + _time: 0, + _count: 0, + + _requestFrame: function() { + var that = this; + DomEvent.requestAnimationFrame(function() { + that._requested = false; + if (!that._animate) + return; + that._requestFrame(); + that._handleFrame(); + }, this._element); + this._requested = true; + }, + + _handleFrame: function() { + paper = this._scope; + var now = Date.now() / 1000, + delta = this._before ? now - this._before : 0; + this._before = now; + this._handlingFrame = true; + this.emit('frame', new Base({ + delta: delta, + time: this._time += delta, + count: this._count++ + })); + if (this._stats) + this._stats.update(); + this._handlingFrame = false; + this.update(); + }, + + _animateItem: function(item, animate) { + var items = this._frameItems; + if (animate) { + items[item._id] = { + item: item, + time: 0, + count: 0 + }; + if (++this._frameItemCount === 1) + this.on('frame', this._handleFrameItems); + } else { + delete items[item._id]; + if (--this._frameItemCount === 0) { + this.off('frame', this._handleFrameItems); + } + } + }, + + _handleFrameItems: function(event) { + for (var i in this._frameItems) { + var entry = this._frameItems[i]; + entry.item.emit('frame', new Base(event, { + time: entry.time += event.delta, + count: entry.count++ + })); + } + }, + + _update: function() { + this._project._needsUpdate = true; + if (this._handlingFrame) + return; + if (this._animate) { + this._handleFrame(); + } else { + this.update(); + } + }, + + _changed: function(flags) { + if (flags & 1) + this._project._needsUpdate = true; + }, + + _transform: function(matrix) { + this._matrix.concatenate(matrix); + this._bounds = null; + this._update(); + }, + + getElement: function() { + return this._element; + }, + + getPixelRatio: function() { + return this._pixelRatio; + }, + + getResolution: function() { + return this._pixelRatio * 72; + }, + + getViewSize: function() { + var size = this._viewSize; + return new LinkedSize(size.width, size.height, this, 'setViewSize'); + }, + + setViewSize: function() { + var size = Size.read(arguments), + delta = size.subtract(this._viewSize); + if (delta.isZero()) + return; + this._viewSize.set(size.width, size.height); + this._setViewSize(size); + this._bounds = null; + this.emit('resize', { + size: size, + delta: delta + }); + this._update(); + }, + + _setViewSize: function(size) { + var element = this._element; + element.width = size.width; + element.height = size.height; + }, + + getBounds: function() { + if (!this._bounds) + this._bounds = this._matrix.inverted()._transformBounds( + new Rectangle(new Point(), this._viewSize)); + return this._bounds; + }, + + getSize: function() { + return this.getBounds().getSize(); + }, + + getCenter: function() { + return this.getBounds().getCenter(); + }, + + setCenter: function() { + var center = Point.read(arguments); + this.scrollBy(center.subtract(this.getCenter())); + }, + + getZoom: function() { + return this._zoom; + }, + + setZoom: function(zoom) { + this._transform(new Matrix().scale(zoom / this._zoom, + this.getCenter())); + this._zoom = zoom; + }, + + isVisible: function() { + return DomElement.isInView(this._element); + }, + + scrollBy: function() { + this._transform(new Matrix().translate(Point.read(arguments).negate())); + }, + + play: function() { + this._animate = true; + if (!this._requested) + this._requestFrame(); + }, + + pause: function() { + this._animate = false; + }, + + draw: function() { + this.update(); + }, + + projectToView: function() { + return this._matrix._transformPoint(Point.read(arguments)); + }, + + viewToProject: function() { + return this._matrix._inverseTransform(Point.read(arguments)); + } + +}, { + statics: { + _views: [], + _viewsById: {}, + _id: 0, + + create: function(project, element) { + if (typeof element === 'string') + element = document.getElementById(element); + return new CanvasView(project, element); + } + } +}, +new function() { + var tool, + prevFocus, + tempFocus, + dragging = false; + + function getView(event) { + var target = DomEvent.getTarget(event); + return target.getAttribute && View._viewsById[target.getAttribute('id')]; + } + + function viewToProject(view, event) { + return view.viewToProject(DomEvent.getOffset(event, view._element)); + } + + function updateFocus() { + if (!View._focused || !View._focused.isVisible()) { + for (var i = 0, l = View._views.length; i < l; i++) { + var view = View._views[i]; + if (view && view.isVisible()) { + View._focused = tempFocus = view; + break; + } + } + } + } + + function handleMouseMove(view, point, event) { + view._handleEvent('mousemove', point, event); + var tool = view._scope.tool; + if (tool) { + tool._handleEvent(dragging && tool.responds('mousedrag') + ? 'mousedrag' : 'mousemove', point, event); + } + view.update(); + return tool; + } + + var navigator = window.navigator, + mousedown, mousemove, mouseup; + if (navigator.pointerEnabled || navigator.msPointerEnabled) { + mousedown = 'pointerdown MSPointerDown'; + mousemove = 'pointermove MSPointerMove'; + mouseup = 'pointerup pointercancel MSPointerUp MSPointerCancel'; + } else { + mousedown = 'touchstart'; + mousemove = 'touchmove'; + mouseup = 'touchend touchcancel'; + if (!('ontouchstart' in window && navigator.userAgent.match( + /mobile|tablet|ip(ad|hone|od)|android|silk/i))) { + mousedown += ' mousedown'; + mousemove += ' mousemove'; + mouseup += ' mouseup'; + } + } + + var viewEvents = { + 'selectstart dragstart': function(event) { + if (dragging) + event.preventDefault(); + } + }; + + var docEvents = { + mouseout: function(event) { + var view = View._focused, + target = DomEvent.getRelatedTarget(event); + if (view && (!target || target.nodeName === 'HTML')) + handleMouseMove(view, viewToProject(view, event), event); + }, + + scroll: updateFocus + }; + + viewEvents[mousedown] = function(event) { + var view = View._focused = getView(event), + point = viewToProject(view, event); + dragging = true; + view._handleEvent('mousedown', point, event); + if (tool = view._scope.tool) + tool._handleEvent('mousedown', point, event); + view.update(); + }; + + docEvents[mousemove] = function(event) { + var view = View._focused; + if (!dragging) { + var target = getView(event); + if (target) { + if (view !== target) + handleMouseMove(view, viewToProject(view, event), event); + prevFocus = view; + view = View._focused = tempFocus = target; + } else if (tempFocus && tempFocus === view) { + view = View._focused = prevFocus; + updateFocus(); + } + } + if (view) { + var point = viewToProject(view, event); + if (dragging || view.getBounds().contains(point)) + tool = handleMouseMove(view, point, event); + } + }; + + docEvents[mouseup] = function(event) { + var view = View._focused; + if (!view || !dragging) + return; + var point = viewToProject(view, event); + dragging = false; + view._handleEvent('mouseup', point, event); + if (tool) + tool._handleEvent('mouseup', point, event); + view.update(); + }; + + DomEvent.add(document, docEvents); + + DomEvent.add(window, { + load: updateFocus + }); + + var mouseFlags = { + mousedown: { + mousedown: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mouseup: { + mouseup: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mousemove: { + mousedrag: 1, + mousemove: 1, + mouseenter: 1, + mouseleave: 1 + } + }; + + return { + _viewEvents: viewEvents, + + _handleEvent: function() {}, + + _installEvent: function(type) { + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) { + counters[key] = (counters[key] || 0) + + (mouseFlags[key][type] || 0); + } + } + }, + + _uninstallEvent: function(type) { + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) + counters[key] -= mouseFlags[key][type] || 0; + } + }, + + statics: { + updateFocus: updateFocus + } + }; +}); + +var CanvasView = View.extend({ + _class: 'CanvasView', + + initialize: function CanvasView(project, canvas) { + if (!(canvas instanceof HTMLCanvasElement)) { + var size = Size.read(arguments, 1); + if (size.isZero()) + throw new Error( + 'Cannot create CanvasView with the provided argument: ' + + [].slice.call(arguments, 1)); + canvas = CanvasProvider.getCanvas(size); + } + this._context = canvas.getContext('2d'); + this._eventCounters = {}; + this._pixelRatio = 1; + if (!/^off|false$/.test(PaperScope.getAttribute(canvas, 'hidpi'))) { + var deviceRatio = window.devicePixelRatio || 1, + backingStoreRatio = DomElement.getPrefixed(this._context, + 'backingStorePixelRatio') || 1; + this._pixelRatio = deviceRatio / backingStoreRatio; + } + View.call(this, project, canvas); + }, + + _setViewSize: function(size) { + var element = this._element, + pixelRatio = this._pixelRatio, + width = size.width, + height = size.height; + element.width = width * pixelRatio; + element.height = height * pixelRatio; + if (pixelRatio !== 1) { + if (!PaperScope.hasAttribute(element, 'resize')) { + var style = element.style; + style.width = width + 'px'; + style.height = height + 'px'; + } + this._context.scale(pixelRatio, pixelRatio); + } + }, + + getPixelSize: function(size) { + var browser = paper.browser, + pixels; + if (browser && browser.firefox) { + var parent = this._element.parentNode, + temp = document.createElement('div'); + temp.style.fontSize = size; + parent.appendChild(temp); + pixels = parseFloat(DomElement.getStyles(temp).fontSize); + parent.removeChild(temp); + } else { + var ctx = this._context, + prevFont = ctx.font; + ctx.font = size + ' serif'; + pixels = parseFloat(ctx.font); + ctx.font = prevFont; + } + return pixels; + }, + + getTextWidth: function(font, lines) { + var ctx = this._context, + prevFont = ctx.font, + width = 0; + ctx.font = font; + for (var i = 0, l = lines.length; i < l; i++) + width = Math.max(width, ctx.measureText(lines[i]).width); + ctx.font = prevFont; + return width; + }, + + update: function(force) { + var project = this._project; + if (!project || !force && !project._needsUpdate) + return false; + var ctx = this._context, + size = this._viewSize; + ctx.clearRect(0, 0, size.width + 1, size.height + 1); + project.draw(ctx, this._matrix, this._pixelRatio); + project._needsUpdate = false; + return true; + } +}, +new function() { + var downPoint, + lastPoint, + overPoint, + downItem, + lastItem, + overItem, + dragItem, + dblClick, + clickTime; + + function callEvent(view, type, event, point, target, lastPoint) { + var item = target, + mouseEvent; + + function call(obj) { + if (obj.responds(type)) { + if (!mouseEvent) { + mouseEvent = new MouseEvent(type, event, point, target, + lastPoint ? point.subtract(lastPoint) : null); + } + if (obj.emit(type, mouseEvent) && mouseEvent.isStopped) { + event.preventDefault(); + return true; + } + } + } + + while (item) { + if (call(item)) + return true; + item = item.getParent(); + } + if (call(view)) + return true; + return false; + } + + return { + _handleEvent: function(type, point, event) { + if (!this._eventCounters[type]) + return; + var project = this._project, + hit = project.hitTest(point, { + tolerance: 0, + fill: true, + stroke: true + }), + item = hit && hit.item, + stopped = false; + switch (type) { + case 'mousedown': + stopped = callEvent(this, type, event, point, item); + dblClick = lastItem == item && (Date.now() - clickTime < 300); + downItem = lastItem = item; + downPoint = lastPoint = overPoint = point; + dragItem = !stopped && item; + while (dragItem && !dragItem.responds('mousedrag')) + dragItem = dragItem._parent; + break; + case 'mouseup': + stopped = callEvent(this, type, event, point, item, downPoint); + if (dragItem) { + if (lastPoint && !lastPoint.equals(point)) + callEvent(this, 'mousedrag', event, point, dragItem, + lastPoint); + if (item !== dragItem) { + overPoint = point; + callEvent(this, 'mousemove', event, point, item, + overPoint); + } + } + if (!stopped && item && item === downItem) { + clickTime = Date.now(); + callEvent(this, dblClick && downItem.responds('doubleclick') + ? 'doubleclick' : 'click', event, downPoint, item); + dblClick = false; + } + downItem = dragItem = null; + break; + case 'mousemove': + if (dragItem) + stopped = callEvent(this, 'mousedrag', event, point, + dragItem, lastPoint); + if (!stopped) { + if (item !== overItem) + overPoint = point; + stopped = callEvent(this, type, event, point, item, + overPoint); + } + lastPoint = overPoint = point; + if (item !== overItem) { + callEvent(this, 'mouseleave', event, point, overItem); + overItem = item; + callEvent(this, 'mouseenter', event, point, item); + } + break; + } + return stopped; + } + }; +}); + +var Event = Base.extend({ + _class: 'Event', + + initialize: function Event(event) { + this.event = event; + }, + + isPrevented: false, + isStopped: false, + + preventDefault: function() { + this.isPrevented = true; + this.event.preventDefault(); + }, + + stopPropagation: function() { + this.isStopped = true; + this.event.stopPropagation(); + }, + + stop: function() { + this.stopPropagation(); + this.preventDefault(); + }, + + getModifiers: function() { + return Key.modifiers; + } +}); + +var KeyEvent = Event.extend({ + _class: 'KeyEvent', + + initialize: function KeyEvent(down, key, character, event) { + Event.call(this, event); + this.type = down ? 'keydown' : 'keyup'; + this.key = key; + this.character = character; + }, + + toString: function() { + return "{ type: '" + this.type + + "', key: '" + this.key + + "', character: '" + this.character + + "', modifiers: " + this.getModifiers() + + " }"; + } +}); + +var Key = new function() { + + var specialKeys = { + 8: 'backspace', + 9: 'tab', + 13: 'enter', + 16: 'shift', + 17: 'control', + 18: 'option', + 19: 'pause', + 20: 'caps-lock', + 27: 'escape', + 32: 'space', + 35: 'end', + 36: 'home', + 37: 'left', + 38: 'up', + 39: 'right', + 40: 'down', + 46: 'delete', + 91: 'command', + 93: 'command', + 224: 'command' + }, + + specialChars = { + 9: true, + 13: true, + 32: true + }, + + modifiers = new Base({ + shift: false, + control: false, + option: false, + command: false, + capsLock: false, + space: false + }), + + charCodeMap = {}, + keyMap = {}, + commandFixMap, + downCode; + + function handleKey(down, keyCode, charCode, event) { + var character = charCode ? String.fromCharCode(charCode) : '', + specialKey = specialKeys[keyCode], + key = specialKey || character.toLowerCase(), + type = down ? 'keydown' : 'keyup', + view = View._focused, + scope = view && view.isVisible() && view._scope, + tool = scope && scope.tool, + name; + keyMap[key] = down; + if (down) { + charCodeMap[keyCode] = charCode; + } else { + delete charCodeMap[keyCode]; + } + if (specialKey && (name = Base.camelize(specialKey)) in modifiers) { + modifiers[name] = down; + var browser = paper.browser; + if (name === 'command' && browser && browser.mac) { + if (down) { + commandFixMap = {}; + } else { + for (var code in commandFixMap) { + if (code in charCodeMap) + handleKey(false, code, commandFixMap[code], event); + } + commandFixMap = null; + } + } + } else if (down && commandFixMap) { + commandFixMap[keyCode] = charCode; + } + if (tool && tool.responds(type)) { + paper = scope; + tool.emit(type, new KeyEvent(down, key, character, event)); + if (view) + view.update(); + } + } + + DomEvent.add(document, { + keydown: function(event) { + var code = event.which || event.keyCode; + if (code in specialKeys || modifiers.command) { + handleKey(true, code, + code in specialChars || modifiers.command ? code : 0, + event); + } else { + downCode = code; + } + }, + + keypress: function(event) { + if (downCode != null) { + handleKey(true, downCode, event.which || event.keyCode, event); + downCode = null; + } + }, + + keyup: function(event) { + var code = event.which || event.keyCode; + if (code in charCodeMap) + handleKey(false, code, charCodeMap[code], event); + } + }); + + DomEvent.add(window, { + blur: function(event) { + for (var code in charCodeMap) + handleKey(false, code, charCodeMap[code], event); + } + }); + + return { + modifiers: modifiers, + + isDown: function(key) { + return !!keyMap[key]; + } + }; +}; + +var MouseEvent = Event.extend({ + _class: 'MouseEvent', + + initialize: function MouseEvent(type, event, point, target, delta) { + Event.call(this, event); + this.type = type; + this.point = point; + this.target = target; + this.delta = delta; + }, + + toString: function() { + return "{ type: '" + this.type + + "', point: " + this.point + + ', target: ' + this.target + + (this.delta ? ', delta: ' + this.delta : '') + + ', modifiers: ' + this.getModifiers() + + ' }'; + } +}); + +var ToolEvent = Event.extend({ + _class: 'ToolEvent', + _item: null, + + initialize: function ToolEvent(tool, type, event) { + this.tool = tool; + this.type = type; + this.event = event; + }, + + _choosePoint: function(point, toolPoint) { + return point ? point : toolPoint ? toolPoint.clone() : null; + }, + + getPoint: function() { + return this._choosePoint(this._point, this.tool._point); + }, + + setPoint: function(point) { + this._point = point; + }, + + getLastPoint: function() { + return this._choosePoint(this._lastPoint, this.tool._lastPoint); + }, + + setLastPoint: function(lastPoint) { + this._lastPoint = lastPoint; + }, + + getDownPoint: function() { + return this._choosePoint(this._downPoint, this.tool._downPoint); + }, + + setDownPoint: function(downPoint) { + this._downPoint = downPoint; + }, + + getMiddlePoint: function() { + if (!this._middlePoint && this.tool._lastPoint) { + return this.tool._point.add(this.tool._lastPoint).divide(2); + } + return this._middlePoint; + }, + + setMiddlePoint: function(middlePoint) { + this._middlePoint = middlePoint; + }, + + getDelta: function() { + return !this._delta && this.tool._lastPoint + ? this.tool._point.subtract(this.tool._lastPoint) + : this._delta; + }, + + setDelta: function(delta) { + this._delta = delta; + }, + + getCount: function() { + return /^mouse(down|up)$/.test(this.type) + ? this.tool._downCount + : this.tool._count; + }, + + setCount: function(count) { + this.tool[/^mouse(down|up)$/.test(this.type) ? 'downCount' : 'count'] + = count; + }, + + getItem: function() { + if (!this._item) { + var result = this.tool._scope.project.hitTest(this.getPoint()); + if (result) { + var item = result.item, + parent = item._parent; + while (/^(Group|CompoundPath)$/.test(parent._class)) { + item = parent; + parent = parent._parent; + } + this._item = item; + } + } + return this._item; + }, + + setItem: function(item) { + this._item = item; + }, + + toString: function() { + return '{ type: ' + this.type + + ', point: ' + this.getPoint() + + ', count: ' + this.getCount() + + ', modifiers: ' + this.getModifiers() + + ' }'; + } +}); + +var Tool = PaperScopeItem.extend({ + _class: 'Tool', + _list: 'tools', + _reference: 'tool', + _events: [ 'onActivate', 'onDeactivate', 'onEditOptions', + 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', + 'onKeyDown', 'onKeyUp' ], + + initialize: function Tool(props) { + PaperScopeItem.call(this); + this._firstMove = true; + this._count = 0; + this._downCount = 0; + this._set(props); + }, + + getMinDistance: function() { + return this._minDistance; + }, + + setMinDistance: function(minDistance) { + this._minDistance = minDistance; + if (minDistance != null && this._maxDistance != null + && minDistance > this._maxDistance) { + this._maxDistance = minDistance; + } + }, + + getMaxDistance: function() { + return this._maxDistance; + }, + + setMaxDistance: function(maxDistance) { + this._maxDistance = maxDistance; + if (this._minDistance != null && maxDistance != null + && maxDistance < this._minDistance) { + this._minDistance = maxDistance; + } + }, + + getFixedDistance: function() { + return this._minDistance == this._maxDistance + ? this._minDistance : null; + }, + + setFixedDistance: function(distance) { + this._minDistance = this._maxDistance = distance; + }, + + _updateEvent: function(type, point, minDistance, maxDistance, start, + needsChange, matchMaxDistance) { + if (!start) { + if (minDistance != null || maxDistance != null) { + var minDist = minDistance != null ? minDistance : 0, + vector = point.subtract(this._point), + distance = vector.getLength(); + if (distance < minDist) + return false; + if (maxDistance != null && maxDistance != 0) { + if (distance > maxDistance) { + point = this._point.add(vector.normalize(maxDistance)); + } else if (matchMaxDistance) { + return false; + } + } + } + if (needsChange && point.equals(this._point)) + return false; + } + this._lastPoint = start && type == 'mousemove' ? point : this._point; + this._point = point; + switch (type) { + case 'mousedown': + this._lastPoint = this._downPoint; + this._downPoint = this._point; + this._downCount++; + break; + case 'mouseup': + this._lastPoint = this._downPoint; + break; + } + this._count = start ? 0 : this._count + 1; + return true; + }, + + _fireEvent: function(type, event) { + var sets = paper.project._removeSets; + if (sets) { + if (type === 'mouseup') + sets.mousedrag = null; + var set = sets[type]; + if (set) { + for (var id in set) { + var item = set[id]; + for (var key in sets) { + var other = sets[key]; + if (other && other != set) + delete other[item._id]; + } + item.remove(); + } + sets[type] = null; + } + } + return this.responds(type) + && this.emit(type, new ToolEvent(this, type, event)); + }, + + _handleEvent: function(type, point, event) { + paper = this._scope; + var called = false; + switch (type) { + case 'mousedown': + this._updateEvent(type, point, null, null, true, false, false); + called = this._fireEvent(type, event); + break; + case 'mousedrag': + var needsChange = false, + matchMaxDistance = false; + while (this._updateEvent(type, point, this.minDistance, + this.maxDistance, false, needsChange, matchMaxDistance)) { + called = this._fireEvent(type, event) || called; + needsChange = true; + matchMaxDistance = true; + } + break; + case 'mouseup': + if (!point.equals(this._point) + && this._updateEvent('mousedrag', point, this.minDistance, + this.maxDistance, false, false, false)) { + called = this._fireEvent('mousedrag', event); + } + this._updateEvent(type, point, null, this.maxDistance, false, + false, false); + called = this._fireEvent(type, event) || called; + this._updateEvent(type, point, null, null, true, false, false); + this._firstMove = true; + break; + case 'mousemove': + while (this._updateEvent(type, point, this.minDistance, + this.maxDistance, this._firstMove, true, false)) { + called = this._fireEvent(type, event) || called; + this._firstMove = false; + } + break; + } + if (called) + event.preventDefault(); + return called; + } + +}); + +var Http = { + request: function(method, url, callback, async) { + async = (async === undefined) ? true : async; + var xhr = new (window.ActiveXObject || XMLHttpRequest)( + 'Microsoft.XMLHTTP'); + xhr.open(method.toUpperCase(), url, async); + if ('overrideMimeType' in xhr) + xhr.overrideMimeType('text/plain'); + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + var status = xhr.status; + if (status === 0 || status === 200) { + callback.call(xhr, xhr.responseText); + } else { + throw new Error('Could not load ' + url + ' (Error ' + + status + ')'); + } + } + }; + return xhr.send(null); + } +}; + +var CanvasProvider = { + canvases: [], + + getCanvas: function(width, height) { + var canvas, + clear = true; + if (typeof width === 'object') { + height = width.height; + width = width.width; + } + if (this.canvases.length) { + canvas = this.canvases.pop(); + } else { + canvas = document.createElement('canvas'); + } + var ctx = canvas.getContext('2d'); + if (canvas.width === width && canvas.height === height) { + if (clear) + ctx.clearRect(0, 0, width + 1, height + 1); + } else { + canvas.width = width; + canvas.height = height; + } + ctx.save(); + return canvas; + }, + + getContext: function(width, height) { + return this.getCanvas(width, height).getContext('2d'); + }, + + release: function(obj) { + var canvas = obj.canvas ? obj.canvas : obj; + canvas.getContext('2d').restore(); + this.canvases.push(canvas); + } +}; + +var BlendMode = new function() { + var min = Math.min, + max = Math.max, + abs = Math.abs, + sr, sg, sb, sa, + br, bg, bb, ba, + dr, dg, db; + + function getLum(r, g, b) { + return 0.2989 * r + 0.587 * g + 0.114 * b; + } + + function setLum(r, g, b, l) { + var d = l - getLum(r, g, b); + dr = r + d; + dg = g + d; + db = b + d; + var l = getLum(dr, dg, db), + mn = min(dr, dg, db), + mx = max(dr, dg, db); + if (mn < 0) { + var lmn = l - mn; + dr = l + (dr - l) * l / lmn; + dg = l + (dg - l) * l / lmn; + db = l + (db - l) * l / lmn; + } + if (mx > 255) { + var ln = 255 - l, + mxl = mx - l; + dr = l + (dr - l) * ln / mxl; + dg = l + (dg - l) * ln / mxl; + db = l + (db - l) * ln / mxl; + } + } + + function getSat(r, g, b) { + return max(r, g, b) - min(r, g, b); + } + + function setSat(r, g, b, s) { + var col = [r, g, b], + mx = max(r, g, b), + mn = min(r, g, b), + md; + mn = mn === r ? 0 : mn === g ? 1 : 2; + mx = mx === r ? 0 : mx === g ? 1 : 2; + md = min(mn, mx) === 0 ? max(mn, mx) === 1 ? 2 : 1 : 0; + if (col[mx] > col[mn]) { + col[md] = (col[md] - col[mn]) * s / (col[mx] - col[mn]); + col[mx] = s; + } else { + col[md] = col[mx] = 0; + } + col[mn] = 0; + dr = col[0]; + dg = col[1]; + db = col[2]; + } + + var modes = { + multiply: function() { + dr = br * sr / 255; + dg = bg * sg / 255; + db = bb * sb / 255; + }, + + screen: function() { + dr = br + sr - (br * sr / 255); + dg = bg + sg - (bg * sg / 255); + db = bb + sb - (bb * sb / 255); + }, + + overlay: function() { + dr = br < 128 ? 2 * br * sr / 255 : 255 - 2 * (255 - br) * (255 - sr) / 255; + dg = bg < 128 ? 2 * bg * sg / 255 : 255 - 2 * (255 - bg) * (255 - sg) / 255; + db = bb < 128 ? 2 * bb * sb / 255 : 255 - 2 * (255 - bb) * (255 - sb) / 255; + }, + + 'soft-light': function() { + var t = sr * br / 255; + dr = t + br * (255 - (255 - br) * (255 - sr) / 255 - t) / 255; + t = sg * bg / 255; + dg = t + bg * (255 - (255 - bg) * (255 - sg) / 255 - t) / 255; + t = sb * bb / 255; + db = t + bb * (255 - (255 - bb) * (255 - sb) / 255 - t) / 255; + }, + + 'hard-light': function() { + dr = sr < 128 ? 2 * sr * br / 255 : 255 - 2 * (255 - sr) * (255 - br) / 255; + dg = sg < 128 ? 2 * sg * bg / 255 : 255 - 2 * (255 - sg) * (255 - bg) / 255; + db = sb < 128 ? 2 * sb * bb / 255 : 255 - 2 * (255 - sb) * (255 - bb) / 255; + }, + + 'color-dodge': function() { + dr = br === 0 ? 0 : sr === 255 ? 255 : min(255, 255 * br / (255 - sr)); + dg = bg === 0 ? 0 : sg === 255 ? 255 : min(255, 255 * bg / (255 - sg)); + db = bb === 0 ? 0 : sb === 255 ? 255 : min(255, 255 * bb / (255 - sb)); + }, + + 'color-burn': function() { + dr = br === 255 ? 255 : sr === 0 ? 0 : max(0, 255 - (255 - br) * 255 / sr); + dg = bg === 255 ? 255 : sg === 0 ? 0 : max(0, 255 - (255 - bg) * 255 / sg); + db = bb === 255 ? 255 : sb === 0 ? 0 : max(0, 255 - (255 - bb) * 255 / sb); + }, + + darken: function() { + dr = br < sr ? br : sr; + dg = bg < sg ? bg : sg; + db = bb < sb ? bb : sb; + }, + + lighten: function() { + dr = br > sr ? br : sr; + dg = bg > sg ? bg : sg; + db = bb > sb ? bb : sb; + }, + + difference: function() { + dr = br - sr; + if (dr < 0) + dr = -dr; + dg = bg - sg; + if (dg < 0) + dg = -dg; + db = bb - sb; + if (db < 0) + db = -db; + }, + + exclusion: function() { + dr = br + sr * (255 - br - br) / 255; + dg = bg + sg * (255 - bg - bg) / 255; + db = bb + sb * (255 - bb - bb) / 255; + }, + + hue: function() { + setSat(sr, sg, sb, getSat(br, bg, bb)); + setLum(dr, dg, db, getLum(br, bg, bb)); + }, + + saturation: function() { + setSat(br, bg, bb, getSat(sr, sg, sb)); + setLum(dr, dg, db, getLum(br, bg, bb)); + }, + + luminosity: function() { + setLum(br, bg, bb, getLum(sr, sg, sb)); + }, + + color: function() { + setLum(sr, sg, sb, getLum(br, bg, bb)); + }, + + add: function() { + dr = min(br + sr, 255); + dg = min(bg + sg, 255); + db = min(bb + sb, 255); + }, + + subtract: function() { + dr = max(br - sr, 0); + dg = max(bg - sg, 0); + db = max(bb - sb, 0); + }, + + average: function() { + dr = (br + sr) / 2; + dg = (bg + sg) / 2; + db = (bb + sb) / 2; + }, + + negation: function() { + dr = 255 - abs(255 - sr - br); + dg = 255 - abs(255 - sg - bg); + db = 255 - abs(255 - sb - bb); + } + }; + + var nativeModes = this.nativeModes = Base.each([ + 'source-over', 'source-in', 'source-out', 'source-atop', + 'destination-over', 'destination-in', 'destination-out', + 'destination-atop', 'lighter', 'darker', 'copy', 'xor' + ], function(mode) { + this[mode] = true; + }, {}); + + var ctx = CanvasProvider.getContext(1, 1); + Base.each(modes, function(func, mode) { + var darken = mode === 'darken', + ok = false; + ctx.save(); + try { + ctx.fillStyle = darken ? '#300' : '#a00'; + ctx.fillRect(0, 0, 1, 1); + ctx.globalCompositeOperation = mode; + if (ctx.globalCompositeOperation === mode) { + ctx.fillStyle = darken ? '#a00' : '#300'; + ctx.fillRect(0, 0, 1, 1); + ok = ctx.getImageData(0, 0, 1, 1).data[0] !== darken ? 170 : 51; + } + } catch (e) {} + ctx.restore(); + nativeModes[mode] = ok; + }); + CanvasProvider.release(ctx); + + this.process = function(mode, srcContext, dstContext, alpha, offset) { + var srcCanvas = srcContext.canvas, + normal = mode === 'normal'; + if (normal || nativeModes[mode]) { + dstContext.save(); + dstContext.setTransform(1, 0, 0, 1, 0, 0); + dstContext.globalAlpha = alpha; + if (!normal) + dstContext.globalCompositeOperation = mode; + dstContext.drawImage(srcCanvas, offset.x, offset.y); + dstContext.restore(); + } else { + var process = modes[mode]; + if (!process) + return; + var dstData = dstContext.getImageData(offset.x, offset.y, + srcCanvas.width, srcCanvas.height), + dst = dstData.data, + src = srcContext.getImageData(0, 0, + srcCanvas.width, srcCanvas.height).data; + for (var i = 0, l = dst.length; i < l; i += 4) { + sr = src[i]; + br = dst[i]; + sg = src[i + 1]; + bg = dst[i + 1]; + sb = src[i + 2]; + bb = dst[i + 2]; + sa = src[i + 3]; + ba = dst[i + 3]; + process(); + var a1 = sa * alpha / 255, + a2 = 1 - a1; + dst[i] = a1 * dr + a2 * br; + dst[i + 1] = a1 * dg + a2 * bg; + dst[i + 2] = a1 * db + a2 * bb; + dst[i + 3] = sa * alpha + a2 * ba; + } + dstContext.putImageData(dstData, offset.x, offset.y); + } + }; +}; + +var SVGStyles = Base.each({ + fillColor: ['fill', 'color'], + strokeColor: ['stroke', 'color'], + strokeWidth: ['stroke-width', 'number'], + strokeCap: ['stroke-linecap', 'string'], + strokeJoin: ['stroke-linejoin', 'string'], + strokeScaling: ['vector-effect', 'lookup', { + true: 'none', + false: 'non-scaling-stroke' + }, function(item, value) { + return !value + && (item instanceof PathItem + || item instanceof Shape + || item instanceof TextItem); + }], + miterLimit: ['stroke-miterlimit', 'number'], + dashArray: ['stroke-dasharray', 'array'], + dashOffset: ['stroke-dashoffset', 'number'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], + fontSize: ['font-size', 'number'], + justification: ['text-anchor', 'lookup', { + left: 'start', + center: 'middle', + right: 'end' + }], + opacity: ['opacity', 'number'], + blendMode: ['mix-blend-mode', 'string'] +}, function(entry, key) { + var part = Base.capitalize(key), + lookup = entry[2]; + this[key] = { + type: entry[1], + property: key, + attribute: entry[0], + toSVG: lookup, + fromSVG: lookup && Base.each(lookup, function(value, name) { + this[value] = name; + }, {}), + exportFilter: entry[3], + get: 'get' + part, + set: 'set' + part + }; +}, {}); + +var SVGNamespaces = { + href: 'http://www.w3.org/1999/xlink', + xlink: 'http://www.w3.org/2000/xmlns' +}; + +new function() { + var formatter; + + function setAttributes(node, attrs) { + for (var key in attrs) { + var val = attrs[key], + namespace = SVGNamespaces[key]; + if (typeof val === 'number') + val = formatter.number(val); + if (namespace) { + node.setAttributeNS(namespace, key, val); + } else { + node.setAttribute(key, val); + } + } + return node; + } + + function createElement(tag, attrs) { + return setAttributes( + document.createElementNS('http://www.w3.org/2000/svg', tag), attrs); + } + + function getTransform(matrix, coordinates, center) { + var attrs = new Base(), + trans = matrix.getTranslation(); + if (coordinates) { + matrix = matrix.shiftless(); + var point = matrix._inverseTransform(trans); + attrs[center ? 'cx' : 'x'] = point.x; + attrs[center ? 'cy' : 'y'] = point.y; + trans = null; + } + if (!matrix.isIdentity()) { + var decomposed = matrix.decompose(); + if (decomposed && !decomposed.shearing) { + var parts = [], + angle = decomposed.rotation, + scale = decomposed.scaling; + if (trans && !trans.isZero()) + parts.push('translate(' + formatter.point(trans) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + if (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } + } + return attrs; + } + + function exportGroup(item, options) { + var attrs = getTransform(item._matrix), + children = item._children; + var node = createElement('g', attrs); + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i]; + var childNode = exportSVG(child, options); + if (childNode) { + if (child.isClipMask()) { + var clip = createElement('clipPath'); + clip.appendChild(childNode); + setDefinition(child, clip, 'clip'); + setAttributes(node, { + 'clip-path': 'url(#' + clip.id + ')' + }); + } else { + node.appendChild(childNode); + } + } + } + return node; + } + + function exportRaster(item, options) { + var attrs = getTransform(item._matrix, true), + size = item.getSize(), + image = item.getImage(); + attrs.x -= size.width / 2; + attrs.y -= size.height / 2; + attrs.width = size.width; + attrs.height = size.height; + attrs.href = options.embedImages === false && image && image.src + || item.toDataURL(); + return createElement('image', attrs); + } + + function exportPath(item, options) { + var matchShapes = options.matchShapes; + if (matchShapes) { + var shape = item.toShape(false); + if (shape) + return exportShape(shape, options); + } + var segments = item._segments, + type, + attrs = getTransform(item._matrix); + if (segments.length === 0) + return null; + if (matchShapes && !item.hasHandles()) { + if (segments.length >= 3) { + type = item._closed ? 'polygon' : 'polyline'; + var parts = []; + for(var i = 0, l = segments.length; i < l; i++) + parts.push(formatter.point(segments[i]._point)); + attrs.points = parts.join(' '); + } else { + type = 'line'; + var first = segments[0]._point, + last = segments[segments.length - 1]._point; + attrs.set({ + x1: first.x, + y1: first.y, + x2: last.x, + y2: last.y + }); + } + } else { + type = 'path'; + attrs.d = item.getPathData(null, options.precision); + } + return createElement(type, attrs); + } + + function exportShape(item) { + var type = item._type, + radius = item._radius, + attrs = getTransform(item._matrix, true, type !== 'rectangle'); + if (type === 'rectangle') { + type = 'rect'; + var size = item._size, + width = size.width, + height = size.height; + attrs.x -= width / 2; + attrs.y -= height / 2; + attrs.width = width; + attrs.height = height; + if (radius.isZero()) + radius = null; + } + if (radius) { + if (type === 'circle') { + attrs.r = radius; + } else { + attrs.rx = radius.width; + attrs.ry = radius.height; + } + } + return createElement(type, attrs); + } + + function exportCompoundPath(item, options) { + var attrs = getTransform(item._matrix); + var data = item.getPathData(null, options.precision); + if (data) + attrs.d = data; + return createElement('path', attrs); + } + + function exportPlacedSymbol(item, options) { + var attrs = getTransform(item._matrix, true), + symbol = item.getSymbol(), + symbolNode = getDefinition(symbol, 'symbol'), + definition = symbol.getDefinition(), + bounds = definition.getBounds(); + if (!symbolNode) { + symbolNode = createElement('symbol', { + viewBox: formatter.rectangle(bounds) + }); + symbolNode.appendChild(exportSVG(definition, options)); + setDefinition(symbol, symbolNode, 'symbol'); + } + attrs.href = '#' + symbolNode.id; + attrs.x += bounds.x; + attrs.y += bounds.y; + attrs.width = formatter.number(bounds.width); + attrs.height = formatter.number(bounds.height); + attrs.overflow = 'visible'; + return createElement('use', attrs); + } + + function exportGradient(color) { + var gradientNode = getDefinition(color, 'color'); + if (!gradientNode) { + var gradient = color.getGradient(), + radial = gradient._radial, + origin = color.getOrigin().transform(), + destination = color.getDestination().transform(), + attrs; + if (radial) { + attrs = { + cx: origin.x, + cy: origin.y, + r: origin.getDistance(destination) + }; + var highlight = color.getHighlight(); + if (highlight) { + highlight = highlight.transform(); + attrs.fx = highlight.x; + attrs.fy = highlight.y; + } + } else { + attrs = { + x1: origin.x, + y1: origin.y, + x2: destination.x, + y2: destination.y + }; + } + attrs.gradientUnits = 'userSpaceOnUse'; + gradientNode = createElement( + (radial ? 'radial' : 'linear') + 'Gradient', attrs); + var stops = gradient._stops; + for (var i = 0, l = stops.length; i < l; i++) { + var stop = stops[i], + stopColor = stop._color, + alpha = stopColor.getAlpha(); + attrs = { + offset: stop._rampPoint, + 'stop-color': stopColor.toCSS(true) + }; + if (alpha < 1) + attrs['stop-opacity'] = alpha; + gradientNode.appendChild(createElement('stop', attrs)); + } + setDefinition(color, gradientNode, 'color'); + } + return 'url(#' + gradientNode.id + ')'; + } + + function exportText(item) { + var node = createElement('text', getTransform(item._matrix, true)); + node.textContent = item._content; + return node; + } + + var exporters = { + Group: exportGroup, + Layer: exportGroup, + Raster: exportRaster, + Path: exportPath, + Shape: exportShape, + CompoundPath: exportCompoundPath, + PlacedSymbol: exportPlacedSymbol, + PointText: exportText + }; + + function applyStyle(item, node, isRoot) { + var attrs = {}, + parent = !isRoot && item.getParent(); + + if (item._name != null) + attrs.id = item._name; + + Base.each(SVGStyles, function(entry) { + var get = entry.get, + type = entry.type, + value = item[get](); + if (entry.exportFilter + ? entry.exportFilter(item, value) + : !parent || !Base.equals(parent[get](), value)) { + if (type === 'color' && value != null) { + var alpha = value.getAlpha(); + if (alpha < 1) + attrs[entry.attribute + '-opacity'] = alpha; + } + attrs[entry.attribute] = value == null + ? 'none' + : type === 'number' + ? formatter.number(value) + : type === 'color' + ? value.gradient + ? exportGradient(value, item) + : value.toCSS(true) + : type === 'array' + ? value.join(',') + : type === 'lookup' + ? entry.toSVG[value] + : value; + } + }); + + if (attrs.opacity === 1) + delete attrs.opacity; + + if (!item._visible) + attrs.visibility = 'hidden'; + + return setAttributes(node, attrs); + } + + var definitions; + function getDefinition(item, type) { + if (!definitions) + definitions = { ids: {}, svgs: {} }; + return item && definitions.svgs[type + '-' + item._id]; + } + + function setDefinition(item, node, type) { + if (!definitions) + getDefinition(); + var id = definitions.ids[type] = (definitions.ids[type] || 0) + 1; + node.id = type + '-' + id; + definitions.svgs[type + '-' + item._id] = node; + } + + function exportDefinitions(node, options) { + var svg = node, + defs = null; + if (definitions) { + svg = node.nodeName.toLowerCase() === 'svg' && node; + for (var i in definitions.svgs) { + if (!defs) { + if (!svg) { + svg = createElement('svg'); + svg.appendChild(node); + } + defs = svg.insertBefore(createElement('defs'), + svg.firstChild); + } + defs.appendChild(definitions.svgs[i]); + } + definitions = null; + } + return options.asString + ? new XMLSerializer().serializeToString(svg) + : svg; + } + + function exportSVG(item, options, isRoot) { + var exporter = exporters[item._class], + node = exporter && exporter(item, options); + if (node) { + var onExport = options.onExport; + if (onExport) + node = onExport(item, node, options) || node; + var data = JSON.stringify(item._data); + if (data && data !== '{}' && data !== 'null') + node.setAttribute('data-paper-data', data); + } + return node && applyStyle(item, node, isRoot); + } + + function setOptions(options) { + if (!options) + options = {}; + formatter = new Formatter(options.precision); + return options; + } + + Item.inject({ + exportSVG: function(options) { + options = setOptions(options); + return exportDefinitions(exportSVG(this, options, true), options); + } + }); + + Project.inject({ + exportSVG: function(options) { + options = setOptions(options); + var layers = this.layers, + view = this.getView(), + size = view.getViewSize(), + node = createElement('svg', { + x: 0, + y: 0, + width: size.width, + height: size.height, + version: '1.1', + xmlns: 'http://www.w3.org/2000/svg', + 'xmlns:xlink': 'http://www.w3.org/1999/xlink' + }), + parent = node, + matrix = view._matrix; + if (!matrix.isIdentity()) + parent = node.appendChild( + createElement('g', getTransform(matrix))); + for (var i = 0, l = layers.length; i < l; i++) + parent.appendChild(exportSVG(layers[i], options, true)); + return exportDefinitions(node, options); + } + }); +}; + +new function() { + + function getValue(node, name, isString, allowNull) { + var namespace = SVGNamespaces[name], + value = namespace + ? node.getAttributeNS(namespace, name) + : node.getAttribute(name); + if (value === 'null') + value = null; + return value == null + ? allowNull + ? null + : isString + ? '' + : 0 + : isString + ? value + : parseFloat(value); + } + + function getPoint(node, x, y, allowNull) { + x = getValue(node, x, false, allowNull); + y = getValue(node, y, false, allowNull); + return allowNull && (x == null || y == null) ? null + : new Point(x, y); + } + + function getSize(node, w, h, allowNull) { + w = getValue(node, w, false, allowNull); + h = getValue(node, h, false, allowNull); + return allowNull && (w == null || h == null) ? null + : new Size(w, h); + } + + function convertValue(value, type, lookup) { + return value === 'none' + ? null + : type === 'number' + ? parseFloat(value) + : type === 'array' + ? value ? value.split(/[\s,]+/g).map(parseFloat) : [] + : type === 'color' + ? getDefinition(value) || value + : type === 'lookup' + ? lookup[value] + : value; + } + + function importGroup(node, type, options, isRoot) { + var nodes = node.childNodes, + isClip = type === 'clippath', + item = new Group(), + project = item._project, + currentStyle = project._currentStyle, + children = []; + if (!isClip) { + item = applyAttributes(item, node, isRoot); + project._currentStyle = item._style.clone(); + } + if (isRoot) { + var defs = node.querySelectorAll('defs'); + for (var i = 0, l = defs.length; i < l; i++) { + importSVG(defs[i], options, false); + } + } + for (var i = 0, l = nodes.length; i < l; i++) { + var childNode = nodes[i], + child; + if (childNode.nodeType === 1 + && childNode.nodeName.toLowerCase() !== 'defs' + && (child = importSVG(childNode, options, false)) + && !(child instanceof Symbol)) + children.push(child); + } + item.addChildren(children); + if (isClip) + item = applyAttributes(item.reduce(), node, isRoot); + project._currentStyle = currentStyle; + if (isClip || type === 'defs') { + item.remove(); + item = null; + } + return item; + } + + function importPoly(node, type) { + var coords = node.getAttribute('points').match( + /[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g), + points = []; + for (var i = 0, l = coords.length; i < l; i += 2) + points.push(new Point( + parseFloat(coords[i]), + parseFloat(coords[i + 1]))); + var path = new Path(points); + if (type === 'polygon') + path.closePath(); + return path; + } + + function importPath(node) { + var data = node.getAttribute('d'), + param = { pathData: data }; + return (data.match(/m/gi) || []).length > 1 || /z\S+/i.test(data) + ? new CompoundPath(param) + : new Path(param); + } + + function importGradient(node, type) { + var id = (getValue(node, 'href', true) || '').substring(1), + isRadial = type === 'radialgradient', + gradient; + if (id) { + gradient = definitions[id].getGradient(); + } else { + var nodes = node.childNodes, + stops = []; + for (var i = 0, l = nodes.length; i < l; i++) { + var child = nodes[i]; + if (child.nodeType === 1) + stops.push(applyAttributes(new GradientStop(), child)); + } + gradient = new Gradient(stops, isRadial); + } + var origin, destination, highlight; + if (isRadial) { + origin = getPoint(node, 'cx', 'cy'); + destination = origin.add(getValue(node, 'r'), 0); + highlight = getPoint(node, 'fx', 'fy', true); + } else { + origin = getPoint(node, 'x1', 'y1'); + destination = getPoint(node, 'x2', 'y2'); + } + applyAttributes( + new Color(gradient, origin, destination, highlight), node); + return null; + } + + var importers = { + '#document': function (node, type, options, isRoot) { + var nodes = node.childNodes; + for (var i = 0, l = nodes.length; i < l; i++) { + var child = nodes[i]; + if (child.nodeType === 1) { + var next = child.nextSibling; + document.body.appendChild(child); + var item = importSVG(child, options, isRoot); + if (next) { + node.insertBefore(child, next); + } else { + node.appendChild(child); + } + return item; + } + } + }, + g: importGroup, + svg: importGroup, + clippath: importGroup, + polygon: importPoly, + polyline: importPoly, + path: importPath, + lineargradient: importGradient, + radialgradient: importGradient, + + image: function (node) { + var raster = new Raster(getValue(node, 'href', true)); + raster.on('load', function() { + var size = getSize(node, 'width', 'height'); + this.setSize(size); + var center = this._matrix._transformPoint( + getPoint(node, 'x', 'y').add(size.divide(2))); + this.translate(center); + }); + return raster; + }, + + symbol: function(node, type, options, isRoot) { + return new Symbol(importGroup(node, type, options, isRoot), true); + }, + + defs: importGroup, + + use: function(node) { + var id = (getValue(node, 'href', true) || '').substring(1), + definition = definitions[id], + point = getPoint(node, 'x', 'y'); + return definition + ? definition instanceof Symbol + ? definition.place(point) + : definition.clone().translate(point) + : null; + }, + + circle: function(node) { + return new Shape.Circle(getPoint(node, 'cx', 'cy'), + getValue(node, 'r')); + }, + + ellipse: function(node) { + return new Shape.Ellipse({ + center: getPoint(node, 'cx', 'cy'), + radius: getSize(node, 'rx', 'ry') + }); + }, + + rect: function(node) { + var point = getPoint(node, 'x', 'y'), + size = getSize(node, 'width', 'height'), + radius = getSize(node, 'rx', 'ry'); + return new Shape.Rectangle(new Rectangle(point, size), radius); + }, + + line: function(node) { + return new Path.Line(getPoint(node, 'x1', 'y1'), + getPoint(node, 'x2', 'y2')); + }, + + text: function(node) { + var text = new PointText(getPoint(node, 'x', 'y') + .add(getPoint(node, 'dx', 'dy'))); + text.setContent(node.textContent.trim() || ''); + return text; + } + }; + + function applyTransform(item, value, name, node) { + var transforms = (node.getAttribute(name) || '').split(/\)\s*/g), + matrix = new Matrix(); + for (var i = 0, l = transforms.length; i < l; i++) { + var transform = transforms[i]; + if (!transform) + break; + var parts = transform.split(/\(\s*/), + command = parts[0], + v = parts[1].split(/[\s,]+/g); + for (var j = 0, m = v.length; j < m; j++) + v[j] = parseFloat(v[j]); + switch (command) { + case 'matrix': + matrix.concatenate( + new Matrix(v[0], v[1], v[2], v[3], v[4], v[5])); + break; + case 'rotate': + matrix.rotate(v[0], v[1], v[2]); + break; + case 'translate': + matrix.translate(v[0], v[1]); + break; + case 'scale': + matrix.scale(v); + break; + case 'skewX': + matrix.skew(v[0], 0); + break; + case 'skewY': + matrix.skew(0, v[0]); + break; + } + } + item.transform(matrix); + } + + function applyOpacity(item, value, name) { + var color = item[name === 'fill-opacity' ? 'getFillColor' + : 'getStrokeColor'](); + if (color) + color.setAlpha(parseFloat(value)); + } + + var attributes = Base.set(Base.each(SVGStyles, function(entry) { + this[entry.attribute] = function(item, value) { + item[entry.set](convertValue(value, entry.type, entry.fromSVG)); + if (entry.type === 'color' && item instanceof Shape) { + var color = item[entry.get](); + if (color) + color.transform(new Matrix().translate( + item.getPosition(true).negate())); + } + }; + }, {}), { + id: function(item, value) { + definitions[value] = item; + if (item.setName) + item.setName(value); + }, + + 'clip-path': function(item, value) { + var clip = getDefinition(value); + if (clip) { + clip = clip.clone(); + clip.setClipMask(true); + if (item instanceof Group) { + item.insertChild(0, clip); + } else { + return new Group(clip, item); + } + } + }, + + gradientTransform: applyTransform, + transform: applyTransform, + + 'fill-opacity': applyOpacity, + 'stroke-opacity': applyOpacity, + + visibility: function(item, value) { + item.setVisible(value === 'visible'); + }, + + display: function(item, value) { + item.setVisible(value !== null); + }, + + 'stop-color': function(item, value) { + if (item.setColor) + item.setColor(value); + }, + + 'stop-opacity': function(item, value) { + if (item._color) + item._color.setAlpha(parseFloat(value)); + }, + + offset: function(item, value) { + var percentage = value.match(/(.*)%$/); + item.setRampPoint(percentage + ? percentage[1] / 100 + : parseFloat(value)); + }, + + viewBox: function(item, value, name, node, styles) { + var rect = new Rectangle(convertValue(value, 'array')), + size = getSize(node, 'width', 'height', true); + if (item instanceof Group) { + var scale = size ? rect.getSize().divide(size) : 1, + matrix = new Matrix().translate(rect.getPoint()).scale(scale); + item.transform(matrix.inverted()); + } else if (item instanceof Symbol) { + if (size) + rect.setSize(size); + var clip = getAttribute(node, 'overflow', styles) != 'visible', + group = item._definition; + if (clip && !rect.contains(group.getBounds())) { + clip = new Shape.Rectangle(rect).transform(group._matrix); + clip.setClipMask(true); + group.addChild(clip); + } + } + } + }); + + function getAttribute(node, name, styles) { + var attr = node.attributes[name], + value = attr && attr.value; + if (!value) { + var style = Base.camelize(name); + value = node.style[style]; + if (!value && styles.node[style] !== styles.parent[style]) + value = styles.node[style]; + } + return !value + ? undefined + : value === 'none' + ? null + : value; + } + + function applyAttributes(item, node, isRoot) { + var styles = { + node: DomElement.getStyles(node) || {}, + parent: !isRoot && DomElement.getStyles(node.parentNode) || {} + }; + Base.each(attributes, function(apply, name) { + var value = getAttribute(node, name, styles); + if (value !== undefined) + item = Base.pick(apply(item, value, name, node, styles), item); + }); + return item; + } + + var definitions = {}; + function getDefinition(value) { + var match = value && value.match(/\((?:#|)([^)']+)/); + return match && definitions[match[1]]; + } + + function importSVG(source, options, isRoot) { + if (!source) + return null; + if (!options) { + options = {}; + } else if (typeof options === 'function') { + options = { onLoad: options }; + } + + var node = source, + scope = paper; + + function onLoadCallback(svg) { + paper = scope; + var item = importSVG(svg, options, isRoot), + onLoad = options.onLoad, + view = scope.project && scope.getView(); + if (onLoad) + onLoad.call(this, item); + view.update(); + } + + if (isRoot) { + if (typeof source === 'string' && !/^.*3){n.sort(function(e,r){return r.length-e.length}),t+="switch(str.length){";for(var a=0;abr&&10!==t&&13!==t&&8232!==t&&8233!==t;)++br,t=dr.charCodeAt(br);fr.onComment&&fr.onComment(!1,dr.slice(e+2,br),e,br,r,fr.locations&&new a)}function u(){for(;pr>br;){var e=dr.charCodeAt(br);if(32===e)++br;else if(13===e){++br;var r=dr.charCodeAt(br);10===r&&++br,fr.locations&&(++Ar,Sr=br)}else if(10===e||8232===e||8233===e)++br,fr.locations&&(++Ar,Sr=br);else if(e>8&&14>e)++br;else if(47===e){var r=dr.charCodeAt(br+1);if(42===r)s();else{if(47!==r)break;c()}}else if(160===e)++br;else{if(!(e>=5760&&Jt.test(String.fromCharCode(e))))break;++br}}}function l(){var e=dr.charCodeAt(br+1);return e>=48&&57>=e?E(!0):(++br,i(xt))}function f(){var e=dr.charCodeAt(br+1);return Er?(++br,k()):61===e?x(Et,2):x(wt,1)}function d(){var e=dr.charCodeAt(br+1);return 61===e?x(Et,2):x(Dt,1)}function p(e){var r=dr.charCodeAt(br+1);return r===e?x(124===e?Lt:Ut,2):61===r?x(Et,2):x(124===e?Rt:Tt,1)}function h(){var e=dr.charCodeAt(br+1);return 61===e?x(Et,2):x(Vt,1)}function m(e){var r=dr.charCodeAt(br+1);return r===e?45==r&&62==dr.charCodeAt(br+2)&&Gt.test(dr.slice(Lr,br))?(br+=3,c(),u(),g()):x(St,2):61===r?x(Et,2):x(At,1)}function v(e){var r=dr.charCodeAt(br+1),t=1;return r===e?(t=62===e&&62===dr.charCodeAt(br+2)?3:2,61===dr.charCodeAt(br+t)?x(Et,t+1):x(jt,t)):33==r&&60==e&&45==dr.charCodeAt(br+2)&&45==dr.charCodeAt(br+3)?(br+=4,c(),u(),g()):(61===r&&(t=61===dr.charCodeAt(br+2)?3:2),x(Ot,t))}function b(e){var r=dr.charCodeAt(br+1);return 61===r?x(qt,61===dr.charCodeAt(br+2)?3:2):x(61===e?Ct:It,1)}function y(e){switch(e){case 46:return l();case 40:return++br,i(mt);case 41:return++br,i(vt);case 59:return++br,i(yt);case 44:return++br,i(bt);case 91:return++br,i(ft);case 93:return++br,i(dt);case 123:return++br,i(pt);case 125:return++br,i(ht);case 58:return++br,i(gt);case 63:return++br,i(kt);case 48:var r=dr.charCodeAt(br+1);if(120===r||88===r)return C();case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return E(!1);case 34:case 39:return A(e);case 47:return f(e);case 37:case 42:return d();case 124:case 38:return p(e);case 94:return h();case 43:case 45:return m(e);case 60:case 62:return v(e);case 61:case 33:return b(e);case 126:return x(It,1)}return!1}function g(e){if(e?br=yr+1:yr=br,fr.locations&&(xr=new a),e)return k();if(br>=pr)return i(Br);var r=dr.charCodeAt(br);if(Qt(r)||92===r)return L();var n=y(r);if(n===!1){var o=String.fromCharCode(r);if("\\"===o||$t.test(o))return L();t(br,"Unexpected character '"+o+"'")}return n}function x(e,r){var t=dr.slice(br,br+r);br+=r,i(e,t)}function k(){for(var e,r,n="",a=br;;){br>=pr&&t(a,"Unterminated regular expression");var o=dr.charAt(br);if(Gt.test(o)&&t(a,"Unterminated regular expression"),e)e=!1;else{if("["===o)r=!0;else if("]"===o&&r)r=!1;else if("/"===o&&!r)break;e="\\"===o}++br}var n=dr.slice(a,br);++br;var s=I();return s&&!/^[gmsiy]*$/.test(s)&&t(a,"Invalid regexp flag"),i(jr,new RegExp(n,s))}function w(e,r){for(var t=br,n=0,a=0,o=null==r?1/0:r;o>a;++a){var i,s=dr.charCodeAt(br);if(i=s>=97?s-97+10:s>=65?s-65+10:s>=48&&57>=s?s-48:1/0,i>=e)break;++br,n=n*e+i}return br===t||null!=r&&br-t!==r?null:n}function C(){br+=2;var e=w(16);return null==e&&t(yr+2,"Expected hexadecimal number"),Qt(dr.charCodeAt(br))&&t(br,"Identifier directly after number"),i(Or,e)}function E(e){var r=br,n=!1,a=48===dr.charCodeAt(br);e||null!==w(10)||t(r,"Invalid number"),46===dr.charCodeAt(br)&&(++br,w(10),n=!0);var o=dr.charCodeAt(br);(69===o||101===o)&&(o=dr.charCodeAt(++br),(43===o||45===o)&&++br,null===w(10)&&t(r,"Invalid number"),n=!0),Qt(dr.charCodeAt(br))&&t(br,"Identifier directly after number");var s,c=dr.slice(r,br);return n?s=parseFloat(c):a&&1!==c.length?/[89]/.test(c)||Tr?t(r,"Invalid number"):s=parseInt(c,8):s=parseInt(c,10),i(Or,s)}function A(e){br++;for(var r="";;){br>=pr&&t(yr,"Unterminated string constant");var n=dr.charCodeAt(br);if(n===e)return++br,i(Dr,r);if(92===n){n=dr.charCodeAt(++br);var a=/^[0-7]+/.exec(dr.slice(br,br+3));for(a&&(a=a[0]);a&&parseInt(a,8)>255;)a=a.slice(0,a.length-1);if("0"===a&&(a=null),++br,a)Tr&&t(br-2,"Octal literal in strict mode"),r+=String.fromCharCode(parseInt(a,8)),br+=a.length-1;else switch(n){case 110:r+="\n";break;case 114:r+="\r";break;case 120:r+=String.fromCharCode(S(2));break;case 117:r+=String.fromCharCode(S(4));break;case 85:r+=String.fromCharCode(S(8));break;case 116:r+=" ";break;case 98:r+="\b";break;case 118:r+=" ";break;case 102:r+="\f";break;case 48:r+="\0";break;case 13:10===dr.charCodeAt(br)&&++br;case 10:fr.locations&&(Sr=br,++Ar);break;default:r+=String.fromCharCode(n)}}else(13===n||10===n||8232===n||8233===n)&&t(yr,"Unterminated string constant"),r+=String.fromCharCode(n),++br}}function S(e){var r=w(16,e);return null===r&&t(yr,"Bad character escape sequence"),r}function I(){Bt=!1;for(var e,r=!0,n=br;;){var a=dr.charCodeAt(br);if(Yt(a))Bt&&(e+=dr.charAt(br)),++br;else{if(92!==a)break;Bt||(e=dr.slice(n,br)),Bt=!0,117!=dr.charCodeAt(++br)&&t(br,"Expecting Unicode escape sequence \\uXXXX"),++br;var o=S(4),i=String.fromCharCode(o);i||t(br-1,"Invalid Unicode escape"),(r?Qt(o):Yt(o))||t(br-4,"Invalid Unicode escape"),e+=i}r=!1}return Bt?e:dr.slice(n,br)}function L(){var e=I(),r=Fr;return Bt||(Wt(e)?r=lt[e]:(fr.forbidReserved&&(3===fr.ecmaVersion?Mt:zt)(e)||Tr&&Xt(e))&&t(yr,"The keyword '"+e+"' is reserved")),i(r,e)}function U(){Ir=yr,Lr=gr,Ur=kr,g()}function R(e){if(Tr=e,br=Lr,fr.locations)for(;Sr>br;)Sr=dr.lastIndexOf("\n",Sr-2)+1,--Ar;u(),g()}function V(){this.type=null,this.start=yr,this.end=null}function T(){this.start=xr,this.end=null,null!==hr&&(this.source=hr)}function q(){var e=new V;return fr.locations&&(e.loc=new T),fr.ranges&&(e.range=[yr,0]),e}function O(e){var r=new V;return r.start=e.start,fr.locations&&(r.loc=new T,r.loc.start=e.loc.start),fr.ranges&&(r.range=[e.range[0],0]),r}function j(e,r){return e.type=r,e.end=Lr,fr.locations&&(e.loc.end=Ur),fr.ranges&&(e.range[1]=Lr),e}function D(e){return fr.ecmaVersion>=5&&"ExpressionStatement"===e.type&&"Literal"===e.expression.type&&"use strict"===e.expression.value}function F(e){return wr===e?(U(),!0):void 0}function B(){return!fr.strictSemicolons&&(wr===Br||wr===ht||Gt.test(dr.slice(Lr,yr)))}function M(){F(yt)||B()||X()}function z(e){wr===e?U():X()}function X(){t(yr,"Unexpected token")}function N(e){"Identifier"!==e.type&&"MemberExpression"!==e.type&&t(e.start,"Assigning to rvalue"),Tr&&"Identifier"===e.type&&Nt(e.name)&&t(e.start,"Assigning to "+e.name+" in strict mode")}function W(e){Ir=Lr=br,fr.locations&&(Ur=new a),Rr=Tr=null,Vr=[],g();var r=e||q(),t=!0;for(e||(r.body=[]);wr!==Br;){var n=J();r.body.push(n),t&&D(n)&&R(!0),t=!1}return j(r,"Program")}function J(){(wr===wt||wr===Et&&"/="==Cr)&&g(!0);var e=wr,r=q();switch(e){case Mr:case Nr:U();var n=e===Mr;F(yt)||B()?r.label=null:wr!==Fr?X():(r.label=lr(),M());for(var a=0;ar){var a=O(e);a.left=e,a.operator=Cr,U(),a.right=er(rr(),n,t);var o=j(a,/&&|\|\|/.test(a.operator)?"LogicalExpression":"BinaryExpression");return er(o,r,t)}return e}function rr(){if(wr.prefix){var e=q(),r=wr.isUpdate;return e.operator=Cr,e.prefix=!0,Er=!0,U(),e.argument=rr(),r?N(e.argument):Tr&&"delete"===e.operator&&"Identifier"===e.argument.type&&t(e.start,"Deleting local variable in strict mode"),j(e,r?"UpdateExpression":"UnaryExpression")}for(var n=tr();wr.postfix&&!B();){var e=O(n);e.operator=Cr,e.prefix=!1,e.argument=n,N(n),U(),n=j(e,"UpdateExpression")}return n}function tr(){return nr(ar())}function nr(e,r){if(F(xt)){var t=O(e);return t.object=e,t.property=lr(!0),t.computed=!1,nr(j(t,"MemberExpression"),r)}if(F(ft)){var t=O(e);return t.object=e,t.property=K(),t.computed=!0,z(dt),nr(j(t,"MemberExpression"),r)}if(!r&&F(mt)){var t=O(e);return t.callee=e,t.arguments=ur(vt,!1),nr(j(t,"CallExpression"),r)}return e}function ar(){switch(wr){case ot:var e=q();return U(),j(e,"ThisExpression");case Fr:return lr();case Or:case Dr:case jr:var e=q();return e.value=Cr,e.raw=dr.slice(yr,gr),U(),j(e,"Literal");case it:case st:case ct:var e=q();return e.value=wr.atomValue,e.raw=wr.keyword,U(),j(e,"Literal");case mt:var r=xr,t=yr;U();var n=K();return n.start=t,n.end=gr,fr.locations&&(n.loc.start=r,n.loc.end=kr),fr.ranges&&(n.range=[t,gr]),z(vt),n;case ft:var e=q();return U(),e.elements=ur(dt,!0,!0),j(e,"ArrayExpression");case pt:return ir();case Gr:var e=q();return U(),cr(e,!1);case at:return or();default:X()}}function or(){var e=q();return U(),e.callee=nr(ar(),!0),e.arguments=F(mt)?ur(vt,!1):qr,j(e,"NewExpression")}function ir(){var e=q(),r=!0,n=!1;for(e.properties=[],U();!F(ht);){if(r)r=!1;else if(z(bt),fr.allowTrailingCommas&&F(ht))break;var a,o={key:sr()},i=!1;if(F(gt)?(o.value=K(!0),a=o.kind="init"):fr.ecmaVersion>=5&&"Identifier"===o.key.type&&("get"===o.key.name||"set"===o.key.name)?(i=n=!0,a=o.kind=o.key.name,o.key=sr(),wr!==mt&&X(),o.value=cr(q(),!1)):X(),"Identifier"===o.key.type&&(Tr||n))for(var s=0;si?e.id:e.params[i];if((Xt(s.name)||Nt(s.name))&&t(s.start,"Defining '"+s.name+"' in strict mode"),i>=0)for(var c=0;i>c;++c)s.name===e.params[c].name&&t(s.start,"Argument name clash in strict mode")}return j(e,r?"FunctionDeclaration":"FunctionExpression")}function ur(e,r,t){for(var n=[],a=!0;!F(e);){if(a)a=!1;else if(z(bt),r&&fr.allowTrailingCommas&&F(e))break;t&&wr===bt?n.push(null):n.push(K(!0))}return n}function lr(e){var r=q();return r.name=wr===Fr?Cr:e&&!fr.forbidReserved&&wr.keyword||X(),Er=!1,U(),j(r,"Identifier")}e.version="0.4.0";var fr,dr,pr,hr;e.parse=function(e,t){return dr=String(e),pr=dr.length,r(t),o(),W(fr.program)};var mr=e.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},vr=e.getLineInfo=function(e,r){for(var t=1,n=0;;){Kt.lastIndex=n;var a=Kt.exec(e);if(!(a&&a.indexe?36===e:91>e?!0:97>e?95===e:123>e?!0:e>=170&&$t.test(String.fromCharCode(e))},Yt=e.isIdentifierChar=function(e){return 48>e?36===e:58>e?!0:65>e?!1:91>e?!0:97>e?95===e:123>e?!0:e>=170&&_t.test(String.fromCharCode(e))},Zt={kind:"loop"},en={kind:"switch"}}); + + var binaryOperators = { + '+': '__add', + '-': '__subtract', + '*': '__multiply', + '/': '__divide', + '%': '__modulo', + '==': 'equals', + '!=': 'equals' + }; + + var unaryOperators = { + '-': '__negate', + '+': null + }; + + var fields = Base.each( + ['add', 'subtract', 'multiply', 'divide', 'modulo', 'negate'], + function(name) { + this['__' + name] = '#' + name; + }, + {} + ); + Point.inject(fields); + Size.inject(fields); + Color.inject(fields); + + function __$__(left, operator, right) { + var handler = binaryOperators[operator]; + if (left && left[handler]) { + var res = left[handler](right); + return operator === '!=' ? !res : res; + } + switch (operator) { + case '+': return left + right; + case '-': return left - right; + case '*': return left * right; + case '/': return left / right; + case '%': return left % right; + case '==': return left == right; + case '!=': return left != right; + } + } + + function $__(operator, value) { + var handler = unaryOperators[operator]; + if (handler && value && value[handler]) + return value[handler](); + switch (operator) { + case '+': return +value; + case '-': return -value; + } + } + + function parse(code, options) { + return scope.acorn.parse(code, options); + } + + function compile(code, url, options) { + if (!code) + return ''; + options = options || {}; + url = url || ''; + + var insertions = []; + + function getOffset(offset) { + for (var i = 0, l = insertions.length; i < l; i++) { + var insertion = insertions[i]; + if (insertion[0] >= offset) + break; + offset += insertion[1]; + } + return offset; + } + + function getCode(node) { + return code.substring(getOffset(node.range[0]), + getOffset(node.range[1])); + } + + function getBetween(left, right) { + return code.substring(getOffset(left.range[1]), + getOffset(right.range[0])); + } + + function replaceCode(node, str) { + var start = getOffset(node.range[0]), + end = getOffset(node.range[1]), + insert = 0; + for (var i = insertions.length - 1; i >= 0; i--) { + if (start > insertions[i][0]) { + insert = i + 1; + break; + } + } + insertions.splice(insert, 0, [start, str.length - end + start]); + code = code.substring(0, start) + str + code.substring(end); + } + + function walkAST(node, parent) { + if (!node) + return; + for (var key in node) { + if (key === 'range' || key === 'loc') + continue; + var value = node[key]; + if (Array.isArray(value)) { + for (var i = 0, l = value.length; i < l; i++) + walkAST(value[i], node); + } else if (value && typeof value === 'object') { + walkAST(value, node); + } + } + switch (node.type) { + case 'UnaryExpression': + if (node.operator in unaryOperators + && node.argument.type !== 'Literal') { + var arg = getCode(node.argument); + replaceCode(node, '$__("' + node.operator + '", ' + + arg + ')'); + } + break; + case 'BinaryExpression': + if (node.operator in binaryOperators + && node.left.type !== 'Literal') { + var left = getCode(node.left), + right = getCode(node.right), + between = getBetween(node.left, node.right), + operator = node.operator; + replaceCode(node, '__$__(' + left + ',' + + between.replace(new RegExp('\\' + operator), + '"' + operator + '"') + + ', ' + right + ')'); + } + break; + case 'UpdateExpression': + case 'AssignmentExpression': + var parentType = parent && parent.type; + if (!( + parentType === 'ForStatement' + || parentType === 'BinaryExpression' + && /^[=!<>]/.test(parent.operator) + || parentType === 'MemberExpression' && parent.computed + )) { + if (node.type === 'UpdateExpression') { + var arg = getCode(node.argument), + exp = '__$__(' + arg + ', "' + node.operator[0] + + '", 1)', + str = arg + ' = ' + exp; + if (!node.prefix + && (parentType === 'AssignmentExpression' + || parentType === 'VariableDeclarator')) { + if (getCode(parent.left || parent.id) === arg) + str = exp; + str = arg + '; ' + str; + } + replaceCode(node, str); + } else { + if (/^.=$/.test(node.operator) + && node.left.type !== 'Literal') { + var left = getCode(node.left), + right = getCode(node.right); + replaceCode(node, left + ' = __$__(' + left + ', "' + + node.operator[0] + '", ' + right + ')'); + } + } + } + break; + } + } + var sourceMap = null, + browser = paper.browser, + version = browser.versionNumber, + lineBreaks = /\r\n|\n|\r/mg; + if (browser.chrome && version >= 30 + || browser.webkit && version >= 537.76 + || browser.firefox && version >= 23) { + var offset = 0; + if (window.location.href.indexOf(url) === 0) { + var html = document.getElementsByTagName('html')[0].innerHTML; + offset = html.substr(0, html.indexOf(code) + 1).match( + lineBreaks).length + 1; + } + var mappings = ['AAAA']; + mappings.length = (code.match(lineBreaks) || []).length + 1 + offset; + sourceMap = { + version: 3, + file: url, + names:[], + mappings: mappings.join(';AACA'), + sourceRoot: '', + sources: [url] + }; + var source = options.source || !url && code; + if (source) + sourceMap.sourcesContent = [source]; + } + walkAST(parse(code, { ranges: true })); + if (sourceMap) { + code = new Array(offset + 1).join('\n') + code + + "\n//# sourceMappingURL=data:application/json;base64," + + (btoa(unescape(encodeURIComponent( + JSON.stringify(sourceMap))))) + + "\n//# sourceURL=" + (url || 'paperscript'); + } + return code; + } + + function execute(code, scope, url, options) { + paper = scope; + var view = scope.getView(), + tool = /\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(code) + ? new Tool() + : null, + toolHandlers = tool ? tool._events : [], + handlers = ['onFrame', 'onResize'].concat(toolHandlers), + params = [], + args = [], + func; + code = compile(code, url, options); + function expose(scope, hidden) { + for (var key in scope) { + if ((hidden || !/^_/.test(key)) && new RegExp('([\\b\\s\\W]|^)' + + key.replace(/\$/g, '\\$') + '\\b').test(code)) { + params.push(key); + args.push(scope[key]); + } + } + } + expose({ __$__: __$__, $__: $__, paper: scope, view: view, tool: tool }, + true); + expose(scope); + handlers = Base.each(handlers, function(key) { + if (new RegExp('\\s+' + key + '\\b').test(code)) { + params.push(key); + this.push(key + ': ' + key); + } + }, []).join(', '); + if (handlers) + code += '\nreturn { ' + handlers + ' };'; + var browser = paper.browser; + if (browser.chrome || browser.firefox) { + var script = document.createElement('script'), + head = document.head || document.getElementsByTagName('head')[0]; + if (browser.firefox) + code = '\n' + code; + script.appendChild(document.createTextNode( + 'paper._execute = function(' + params + ') {' + code + '\n}' + )); + head.appendChild(script); + func = paper._execute; + delete paper._execute; + head.removeChild(script); + } else { + func = Function(params, code); + } + var res = func.apply(scope, args) || {}; + Base.each(toolHandlers, function(key) { + var value = res[key]; + if (value) + tool[key] = value; + }); + if (view) { + if (res.onResize) + view.setOnResize(res.onResize); + view.emit('resize', { + size: view.size, + delta: new Point() + }); + if (res.onFrame) + view.setOnFrame(res.onFrame); + view.update(); + } + } + + function loadScript(script) { + if (/^text\/(?:x-|)paperscript$/.test(script.type) + && PaperScope.getAttribute(script, 'ignore') !== 'true') { + var canvasId = PaperScope.getAttribute(script, 'canvas'), + canvas = document.getElementById(canvasId), + src = script.src || script.getAttribute('data-src'), + async = PaperScope.hasAttribute(script, 'async'), + scopeAttribute = 'data-paper-scope'; + if (!canvas) + throw new Error('Unable to find canvas with id "' + + canvasId + '"'); + var scope = PaperScope.get(canvas.getAttribute(scopeAttribute)) + || new PaperScope().setup(canvas); + canvas.setAttribute(scopeAttribute, scope._id); + if (src) { + Http.request('get', src, function(code) { + execute(code, scope, src); + }, async); + } else { + execute(script.innerHTML, scope, script.baseURI); + } + script.setAttribute('data-paper-ignore', 'true'); + return scope; + } + } + + function loadAll() { + Base.each(document.getElementsByTagName('script'), loadScript); + } + + function load(script) { + return script ? loadScript(script) : loadAll(); + } + + if (document.readyState === 'complete') { + setTimeout(loadAll); + } else { + DomEvent.add(window, { load: loadAll }); + } + + return { + compile: compile, + execute: execute, + load: load, + parse: parse + }; + +}).call(this); + +paper = new (PaperScope.inject(Base.exports, { + enumerable: true, + Base: Base, + Numerical: Numerical, + Key: Key +}))(); + +if (typeof define === 'function' && define.amd) { + define('paper', paper); +} else if (typeof module === 'object' && module) { + module.exports = paper; +} + +return paper; +}; diff --git a/dist/paper-full.min.js b/dist/paper-full.min.js index ca672f3e..2fd6449f 100644 --- a/dist/paper-full.min.js +++ b/dist/paper-full.min.js @@ -1,5 +1,5 @@ /*! - * Paper.js v0.9.24 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.25 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey @@ -9,7 +9,7 @@ * * All rights reserved. * - * Date: Fri Aug 21 16:39:41 2015 +0200 + * Date: Sun Oct 25 11:23:38 2015 +0100 * *** * @@ -29,10 +29,10 @@ * created by Marijn Haverbeke and released under an MIT license. * */ -var paper=new function(t){var e=new function(){function n(t,n,i,r,a){function o(s,o){o=o||(o=u(n,s))&&(o.get?o:o.value),"string"==typeof o&&"#"===o[0]&&(o=t[o.substring(1)]||o);var l,d="function"==typeof o,f=o,_=a||d&&!o.base?o&&o.get?s in t:t[s]:null;a&&_||(d&&_&&(o.base=_),d&&r!==!1&&(l=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(h[l[3].toLowerCase()+l[4]]=l[2]),f&&!d&&f.get&&"function"==typeof f.get&&e.isPlainObject(f)||(f={value:f,writable:!0}),(u(t,s)||{configurable:!0}).configurable&&(f.configurable=!0,f.enumerable=i),c(t,s,f))}var h={};if(n){for(var l in n)n.hasOwnProperty(l)&&!s.test(l)&&o(l);for(var l in h){var d=h[l],f=t["set"+d],_=t["get"+d]||f&&t["is"+d];!_||r!==!0&&0!==_.length||o(l,{get:_,set:f})}}return t}function i(t,e,n){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?a:o).call(t,e,n=n||t),n}function r(t,e,n){for(var i in e)!e.hasOwnProperty(i)||n&&n[i]||(t[i]=e[i]);return t}var s=/^(statics|enumerable|beans|preserve)$/,a=[].forEach||function(t,e){for(var n=0,i=this.length;i>n;n++)t.call(e,this[n],n,this)},o=function(t,e){for(var n in this)this.hasOwnProperty(n)&&t.call(e,this[n],n,this)},h=Object.create||function(t){return{__proto__:t}},u=Object.getOwnPropertyDescriptor||function(t,e){var n=t.__lookupGetter__&&t.__lookupGetter__(e);return n?{get:n,set:t.__lookupSetter__(e),enumerable:!0,configurable:!0}:t.hasOwnProperty(e)?{value:t[e],enumerable:!0,configurable:!0,writable:!0}:null},l=Object.defineProperty||function(t,e,n){return(n.get||n.set)&&t.__defineGetter__?(n.get&&t.__defineGetter__(e,n.get),n.set&&t.__defineSetter__(e,n.set)):t[e]=n.value,t},c=function(t,e,n){return delete t[e],l(t,e,n)};return n(function(){for(var t=0,e=arguments.length;e>t;t++)r(this,arguments[t])},{inject:function(t){if(t){var e=t.statics===!0?t:t.statics,i=t.beans,r=t.preserve;e!==t&&n(this.prototype,t,t.enumerable,i,r),n(this,e,!0,i,r)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e,i=this,r=0,s=arguments.length;s>r&&!(t=arguments[r].initialize);r++);return t=t||function(){i.apply(this,arguments)},e=t.prototype=h(this.prototype),c(e,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length&&this.inject.apply(t,arguments),t.base=i,t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++){var i=arguments[t];i&&n(this,i,i.enumerable,i.beans,i.preserve)}return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},set:function(t){return r(this,t)},clone:function(){return new this.constructor(this)},statics:{each:i,create:h,define:c,describe:u,set:r,clone:function(t){return r(new t.constructor,t)},isPlainObject:function(t){var n=null!=t&&t.constructor;return n&&(n===Object||n===e||"Object"===n.name)},pick:function(e,n){return e!==t?e:n}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({toString:function(){return null!=this._id?(this._class||"Object")+(this._name?" '"+this._name+"'":" @"+this._id):"{ "+e.each(this,function(t,e){if(!/^_/.test(e)){var n=typeof t;this.push(e+": "+("number"===n?s.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},getClassName:function(){return this._class||""},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i,r){if(n&&(r||e.isPlainObject(n))){for(var s=Object.keys(n._filtering||n),a=0,o=s.length;o>a;a++){var h=s[a];if(!i||!i[h]){var u=n[h];u!==t&&(this[h]=u)}}return!0}},statics:{exports:{enumerable:!0},extend:function et(){var t=et.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(t&&n&&"object"==typeof t&&"object"==typeof n){if(Array.isArray(t)&&Array.isArray(n)){var i=t.length;if(i!==n.length)return!1;for(;i--;)if(!e.equals(t[i],n[i]))return!1}else{var r=Object.keys(t),i=r.length;if(i!==Object.keys(n).length)return!1;for(;i--;){var s=r[i];if(!n.hasOwnProperty(s)||!e.equals(t[s],n[s]))return!1}}return!0}return!1},read:function(n,i,r,s){if(this===e){var a=this.peek(n,i);return n.__index++,a}var o=this.prototype,h=o._readIndex,u=i||h&&n.__index||0;s||(s=n.length-u);var l=n[u];return l instanceof this||r&&r.readNull&&null==l&&1>=s?(h&&(n.__index=u+1),l&&r&&r.clone?l.clone():l):(l=e.create(this.prototype),h&&(l.__read=!0),l=l.initialize.apply(l,u>0||ss;s++)r.push(Array.isArray(i=t[s])?this.read(i,0,n):this.read(t,s,n,1));return r},readNamed:function(n,i,r,s,a){var o=this.getNamed(n,i),h=o!==t;if(h){var u=n._filtered;u||(u=n._filtered=e.create(n[0]),u._filtering=n[0]),u[i]=t}return this.read(h?[o]:n,r,s,a)},getNamed:function(n,i){var r=n[0];return n._hasObject===t&&(n._hasObject=1===n.length&&e.isPlainObject(r)),n._hasObject?i?r[i]:n._filtered||r:t},hasNamed:function(t,e){return!!this.getNamed(t,e)},isPlainValue:function(t,e){return this.isPlainObject(t)||Array.isArray(t)||e&&"string"==typeof t},serialize:function(t,n,i,r){n=n||{};var a,o=!r;if(o&&(n.formatter=new s(n.precision),r={length:0,definitions:{},references:{},add:function(t,e){var n="#"+t._id,i=this.references[n];if(!i){this.length++;var r=e.call(t),s=t._class;s&&r[0]!==s&&r.unshift(s),this.definitions[n]=r,i=this.references[n]=[n]}return i}}),t&&t._serialize){a=t._serialize(n,r);var h=t._class;!h||i||a._compact||a[0]===h||a.unshift(h)}else if(Array.isArray(t)){a=[];for(var u=0,l=t.length;l>u;u++)a[u]=e.serialize(t[u],n,i,r);i&&(a._compact=!0)}else if(e.isPlainObject(t)){a={};for(var c=Object.keys(t),u=0,l=c.length;l>u;u++){var d=c[u];a[d]=e.serialize(t[d],n,i,r)}}else a="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],a]:a},deserialize:function(t,n,i,r){var s=t,a=!i;if(i=i||{},Array.isArray(t)){var o=t[0],h="dictionary"===o;if(1==t.length&&/^#/.test(o))return i.dictionary[o];o=e.exports[o],s=[],r&&(i.dictionary=s);for(var u=o?1:0,l=t.length;l>u;u++)s.push(e.deserialize(t[u],n,i,h));if(o){var c=s;n?s=n(o,c):(s=e.create(o.prototype),o.apply(s,c))}}else if(e.isPlainObject(t)){s={},r&&(i.dictionary=s);for(var d in t)s[d]=e.deserialize(t[d],n,i)}return a&&t&&t.length&&"dictionary"===t[0][0]?s[1]:s},exportJSON:function(t,n){var i=e.serialize(t,n);return n&&n.asString===!1?i:JSON.stringify(i)},importJSON:function(t,n){return e.deserialize("string"==typeof t?JSON.parse(t):t,function(t,i){var r=n&&n.constructor===t?n:e.create(t.prototype),s=r===n;if(1===i.length&&r instanceof m&&(s||!(r instanceof w))){var a=i[0];e.isPlainObject(a)&&(a.insert=!1)}return t.apply(r,i),s&&(n=null),r})},splice:function(e,n,i,r){var s=n&&n.length,a=i===t;i=a?e.length:i,i>e.length&&(i=e.length);for(var o=0;s>o;o++)n[o]._index=i+o;if(a)return e.push.apply(e,n),[];var h=[i,r];n&&h.push.apply(h,n);for(var u=e.splice.apply(e,h),o=0,l=u.length;l>o;o++)u[o]._index=t;for(var o=i+s,l=e.length;l>o;o++)e[o]._index=o;return u},capitalize:function(t){return t.replace(/\b[a-z]/g,function(t){return t.toUpperCase()})},camelize:function(t){return t.replace(/-(.)/g,function(t,e){return e.toUpperCase()})},hyphenate:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}}});var n={on:function(t,n){if("string"!=typeof t)e.each(t,function(t,e){this.on(e,t)},this);else{var i=this._eventTypes,r=i&&i[t],s=this._callbacks=this._callbacks||{};s=s[t]=s[t]||[],-1===s.indexOf(n)&&(s.push(n),r&&r.install&&1==s.length&&r.install.call(this,t))}return this},off:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.off(e,t)},this),t;var r,s=this._eventTypes,a=s&&s[n],o=this._callbacks&&this._callbacks[n];return o&&(!i||-1!==(r=o.indexOf(i))&&1===o.length?(a&&a.uninstall&&a.uninstall.call(this,n),delete this._callbacks[n]):-1!==r&&o.splice(r,1)),this},once:function(t,e){return this.on(t,function(){e.apply(this,arguments),this.off(t,e)})},emit:function(t,e){var n=this._callbacks&&this._callbacks[t];if(!n)return!1;var i=[].slice.call(arguments,1);n=n.slice();for(var r=0,s=n.length;s>r;r++)if(n[r].apply(this,i)===!1){e&&e.stop&&e.stop();break}return!0},responds:function(t){return!(!this._callbacks||!this._callbacks[t])},attach:"#on",detach:"#off",fire:"#emit",_installEvents:function(t){var e=this._callbacks,n=t?"install":"uninstall";for(var i in e)if(e[i].length>0){var r=this._eventTypes,s=r&&r[i],a=s&&s[n];a&&a.call(this,i)}},statics:{inject:function nt(t){var n=t._events;if(n){var i={};e.each(n,function(n,r){var s="string"==typeof n,a=s?n:r,o=e.capitalize(a),h=a.substring(2).toLowerCase();i[h]=s?{}:n,a="_"+a,t["get"+o]=function(){return this[a]},t["set"+o]=function(t){var e=this[a];e&&this.off(h,e),t&&this.on(h,t),this[a]=t}}),t._eventTypes=i}return nt.base.apply(this,arguments)}}},i=e.extend({_class:"PaperScope",initialize:function it(){paper=this,this.settings=new e({applyMatrix:!0,handleSize:4,hitTolerance:0}),this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=it._id++,it._scopes[this._id]=this;var t=it.prototype;if(!this.support){var n=Y.getContext(1,1);t.support={nativeDash:"setLineDash"in n||"mozDash"in n,nativeBlendModes:K.nativeModes},Y.release(n)}if(!this.browser){var i=navigator.userAgent.toLowerCase(),r=(/(win)/.exec(i)||/(mac)/.exec(i)||/(linux)/.exec(i)||[])[0],s=t.browser={platform:r};r&&(s[r]=!0),i.replace(/(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,function(t,e,n,i,r){if(!s.chrome){var a="opera"===e?i:n;"trident"===e&&(a=r,e="msie"),s.version=a,s.versionNumber=parseFloat(a),s.name=e,s[e]=!0}}),s.chrome&&delete s.webkit,s.atom&&delete s.chrome}},version:"0.9.24",getView:function(){return this.project&&this.project.getView()},getPaper:function(){return this},execute:function(t,e,n){paper.PaperScript.execute(t,this,e,n),V.updateFocus()},install:function(t){var n=this;e.each(["project","view","tool"],function(i){e.define(t,i,{configurable:!0,get:function(){return n[i]}})});for(var i in this)!/^_/.test(i)&&this[i]&&(t[i]=this[i])},setup:function(t){return paper=this,this.project=new p(t),this},activate:function(){paper=this},clear:function(){for(var t=this.projects.length-1;t>=0;t--)this.projects[t].remove();for(var t=this.tools.length-1;t>=0;t--)this.tools[t].remove();for(var t=this.palettes.length-1;t>=0;t--)this.palettes[t].remove()},remove:function(){this.clear(),delete i._scopes[this._id]},statics:new function(){function t(t){return t+="Attribute",function(e,n){return e[t](n)||e[t]("data-paper-"+n)}}return{_scopes:{},_id:0,get:function(t){return this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),r=e.extend(n,{initialize:function(t){this._scope=paper,this._index=this._scope[this._list].push(this)-1,(t||!this._scope[this._reference])&&this.activate()},activate:function(){if(!this._scope)return!1;var t=this._scope[this._reference];return t&&t!==this&&t.emit("deactivate"),this._scope[this._reference]=this,this.emit("activate",t),!0},isActive:function(){return this._scope[this._reference]===this},remove:function(){return null==this._index?!1:(e.splice(this._scope[this._list],null,this._index,1),this._scope[this._reference]==this&&(this._scope[this._reference]=null),this._scope=null,!0)}}),s=e.extend({initialize:function(t){this.precision=t||5,this.multiplier=Math.pow(10,this.precision)},number:function(t){return Math.round(t*this.multiplier)/this.multiplier},pair:function(t,e,n){return this.number(t)+(n||",")+this.number(e)},point:function(t,e){return this.number(t.x)+(e||",")+this.number(t.y)},size:function(t,e){return this.number(t.width)+(e||",")+this.number(t.height)},rectangle:function(t,e){return this.point(t,e)+(e||",")+this.size(t,e)}});s.instance=new s;var a=new function(){var t=[[.5773502691896257],[0,.7745966692414834],[.33998104358485626,.8611363115940526],[0,.5384693101056831,.906179845938664],[.2386191860831969,.6612093864662645,.932469514203152],[0,.4058451513773972,.7415311855993945,.9491079123427585],[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],[0,.3242534234038089,.6133714327005904,.8360311073266358,.9681602395076261],[.14887433898163122,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],[0,.26954315595234496,.5190961292068118,.7301520055740494,.8870625997680953,.978228658146057],[.1252334085114689,.3678314989981802,.5873179542866175,.7699026741943047,.9041172563704749,.9815606342467192],[0,.2304583159551348,.44849275103644687,.6423493394403402,.8015780907333099,.9175983992229779,.9841830547185881],[.10805494870734367,.31911236892788974,.5152486363581541,.6872929048116855,.827201315069765,.9284348836635735,.9862838086968123],[0,.20119409399743451,.3941513470775634,.5709721726085388,.7244177313601701,.8482065834104272,.937273392400706,.9879925180204854],[.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]],e=[[1],[.8888888888888888,.5555555555555556],[.6521451548625461,.34785484513745385],[.5688888888888889,.47862867049936647,.23692688505618908],[.46791393457269104,.3607615730481386,.17132449237917036],[.4179591836734694,.3818300505051189,.27970539148927664,.1294849661688697],[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626],[.3302393550012598,.31234707704000286,.26061069640293544,.1806481606948574,.08127438836157441],[.29552422471475287,.26926671930999635,.21908636251598204,.1494513491505806,.06667134430868814],[.2729250867779006,.26280454451024665,.23319376459199048,.18629021092773426,.1255803694649046,.05566856711617366],[.24914704581340277,.2334925365383548,.20316742672306592,.16007832854334622,.10693932599531843,.04717533638651183],[.2325515532308739,.22628318026289723,.2078160475368885,.17814598076194574,.13887351021978725,.09212149983772845,.04048400476531588],[.2152638534631578,.2051984637212956,.18553839747793782,.15720316715819355,.12151857068790319,.08015808715976021,.03511946033175186],[.2025782419255613,.19843148532711158,.1861610000155622,.16626920581699392,.13957067792615432,.10715922046717194,.07036604748810812,.03075324199611727],[.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096]],n=Math.abs,i=Math.sqrt,r=Math.pow,s=1e-6,o=1e-12,h=1.12e-16;return{TOLERANCE:s,EPSILON:o,MACHINE_EPSILON:h,KAPPA:4*(i(2)-1)/3,isZero:function(t){return n(t)<=o},integrate:function(n,i,r,s){for(var a=t[s-2],o=e[s-2],h=.5*(r-i),u=h+i,l=0,c=s+1>>1,d=1&s?o[l++]*n(u):0;c>l;){var f=h*a[l];d+=o[l++]*(n(u+f)+n(u-f))}return h*d},findRoot:function(t,e,i,r,s,a,o){for(var h=0;a>h;h++){var u=t(i),l=u/e(i),c=i-l;if(n(l)0?(s=i,i=r>=c?.5*(r+s):c):(r=i,i=c>=s?.5*(r+s):c)}return i},solveQuadratic:function(t,e,s,a,u,l){var c,d,f=0,_=1/0,g=e;if(e/=2,d=e*e-t*s,0!==d&&n(d)p){var v=r(10,n(Math.floor(Math.log(p)*Math.LOG10E)));isFinite(v)||(v=0),t*=v,e*=v,s*=v,d=e*e-t*s}}if(n(t)=-h){d=0>d?0:d;var m=i(d);if(e>=h&&h>=e)c=n(t)>=n(s)?m/t:-s/m,_=-c;else{var y=-(e+(0>e?-1:1)*m);c=y/t,_=s/y}}return isFinite(c)&&(null==u||c>=u&&l>=c)&&(a[f++]=c),_!==c&&isFinite(_)&&(null==u||_>=u&&l>=_)&&(a[f++]=_),f},solveCubic:function(t,e,s,u,l,c,d){var f,_,g,p=0;if(n(t)w?-1:1,w=-y/t,x=w>0?1.3247179572*Math.max(x,i(w)):x,v=f-b*x,v!==f){do if(f=v,C=t*f,_=C+e,g=_*f+s,y=(C+_)*f+g,m=g*f+u,v=0===y?f:f-m/y/S,v===f){f=v;break}while(b*v>b*f);n(t)*f*f>n(u/f)&&(g=-u/f,_=(g-s)/f)}}var p=a.solveQuadratic(t,_,g,l,c,d);return isFinite(f)&&(0===p||f!==l[p-1])&&(null==c||f>=c&&d>=f)&&(l[p++]=f),p}}},o={_id:1,_pools:{},get:function(t){if(t){var e=t._class,n=this._pools[e];return n||(n=this._pools[e]={_id:1}),n._id++}return this._id++}},h=e.extend({_class:"Point",_readIndex:!0,initialize:function(t,e){var n=typeof t;if("number"===n){var i="number"==typeof e;this.x=t,this.y=i?e:t,this.__read&&(this.__read=i?2:1)}else"undefined"===n||null===t?(this.x=this.y=0,this.__read&&(this.__read=null===t?1:0)):(Array.isArray(t)?(this.x=t[0],this.y=t.length>1?t[1]:t[0]):null!=t.x?(this.x=t.x,this.y=t.y):null!=t.width?(this.x=t.width,this.y=t.height):null!=t.angle?(this.x=t.length,this.y=0,this.setAngle(t.angle)):(this.x=this.y=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.x=t,this.y=e,this},equals:function(t){return this===t||t&&(this.x===t.x&&this.y===t.y||Array.isArray(t)&&this.x===t[0]&&this.y===t[1])||!1},clone:function(){return new h(this.x,this.y)},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y)]},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(t){if(this.isZero()){var e=this._angle||0;this.set(Math.cos(e)*t,Math.sin(e)*t)}else{var n=t/this.getLength();a.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}},getAngle:function(){return 180*this.getAngleInRadians.apply(this,arguments)/Math.PI},setAngle:function(t){this.setAngleInRadians.call(this,t*Math.PI/180)},getAngleInDegrees:"#getAngle",setAngleInDegrees:"#setAngle",getAngleInRadians:function(){if(arguments.length){var t=h.read(arguments),e=this.getLength()*t.getLength();if(a.isZero(e))return NaN;var n=this.dot(t)/e;return Math.acos(-1>n?-1:n>1?1:n)}return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x)},setAngleInRadians:function(t){if(this._angle=t,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3}},{beans:!1,getDirectedAngle:function(){var t=h.read(arguments);return 180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},getDistance:function(){var t=h.read(arguments),n=t.x-this.x,i=t.y-this.y,r=n*n+i*i,s=e.read(arguments);return s?r:Math.sqrt(r)},normalize:function(e){e===t&&(e=1);var n=this.getLength(),i=0!==n?e/n:0,r=new h(this.x*i,this.y*i);return i>=0&&(r._angle=this._angle),r},rotate:function(t,e){if(0===t)return this.clone();t=t*Math.PI/180;var n=e?this.subtract(e):this,i=Math.sin(t),r=Math.cos(t);return n=new h(n.x*r-n.y*i,n.x*i+n.y*r),e?n.add(e):n},transform:function(t){return t?t._transformPoint(this):this},add:function(){var t=h.read(arguments);return new h(this.x+t.x,this.y+t.y)},subtract:function(){var t=h.read(arguments);return new h(this.x-t.x,this.y-t.y)},multiply:function(){var t=h.read(arguments);return new h(this.x*t.x,this.y*t.y)},divide:function(){var t=h.read(arguments);return new h(this.x/t.x,this.y/t.y)},modulo:function(){var t=h.read(arguments);return new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},isInside:function(){return d.read(arguments).contains(this)},isClose:function(t,e){return this.getDistance(t)1?t[1]:t[0]):null!=t.width?(this.width=t.width,this.height=t.height):null!=t.x?(this.width=t.x,this.height=t.y):(this.width=this.height=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.width=t,this.height=e,this},equals:function(t){return t===this||t&&(this.width===t.width&&this.height===t.height||Array.isArray(t)&&this.width===t[0]&&this.height===t[1])||!1},clone:function(){return new l(this.width,this.height)},toString:function(){var t=s.instance;return"{ width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.width),e.number(this.height)]},add:function(){var t=l.read(arguments);return new l(this.width+t.width,this.height+t.height)},subtract:function(){var t=l.read(arguments);return new l(this.width-t.width,this.height-t.height)},multiply:function(){var t=l.read(arguments);return new l(this.width*t.width,this.height*t.height)},divide:function(){var t=l.read(arguments);return new l(this.width/t.width,this.height/t.height)},modulo:function(){var t=l.read(arguments);return new l(this.width%t.width,this.height%t.height)},negate:function(){return new l(-this.width,-this.height)},isZero:function(){return a.isZero(this.width)&&a.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new l(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new l(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new l(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new l(e(this.width),e(this.height))}},{})),c=l.extend({initialize:function(t,e,n,i){this._width=t,this._height=e,this._owner=n,this._setter=i},set:function(t,e,n){return this._width=t,this._height=e,n||this._owner[this._setter](this),this},getWidth:function(){return this._width},setWidth:function(t){this._width=t,this._owner[this._setter](this)},getHeight:function(){return this._height},setHeight:function(t){this._height=t,this._owner[this._setter](this)}}),d=e.extend({_class:"Rectangle",_readIndex:!0,beans:!0,initialize:function(n,i,r,s){var a=typeof n,o=0;if("number"===a?(this.x=n,this.y=i,this.width=r,this.height=s,o=4):"undefined"===a||null===n?(this.x=this.y=this.width=this.height=0,o=null===n?1:0):1===arguments.length&&(Array.isArray(n)?(this.x=n[0],this.y=n[1],this.width=n[2],this.height=n[3],o=1):n.x!==t||n.width!==t?(this.x=n.x||0,this.y=n.y||0,this.width=n.width||0,this.height=n.height||0,o=1):n.from===t&&n.to===t&&(this.x=this.y=this.width=this.height=0,this._set(n),o=1)),!o){var u=h.readNamed(arguments,"from"),c=e.peek(arguments);if(this.x=u.x,this.y=u.y,c&&c.x!==t||e.hasNamed(arguments,"to")){var d=h.readNamed(arguments,"to");this.width=d.x-u.x,this.height=d.y-u.y,this.width<0&&(this.x=d.x,this.width=-this.width),this.height<0&&(this.y=d.y,this.height=-this.height)}else{var f=l.read(arguments);this.width=f.width,this.height=f.height}o=arguments.__index}this.__read&&(this.__read=o)},set:function(t,e,n,i){return this.x=t,this.y=e,this.width=n,this.height=i,this},clone:function(){return new d(this.x,this.y,this.width,this.height)},equals:function(t){var n=e.isPlainValue(t)?d.read(arguments):t;return n===this||n&&this.x===n.x&&this.y===n.y&&this.width===n.width&&this.height===n.height||!1},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+", width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y),e.number(this.width),e.number(this.height)]},getPoint:function(t){var e=t?h:u;return new e(this.x,this.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.x=t.x,this.y=t.y},getSize:function(t){var e=t?l:c;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=l.read(arguments);this._fixX&&(this.x+=(this.width-t.width)*this._fixX),this._fixY&&(this.y+=(this.height-t.height)*this._fixY),this.width=t.width,this.height=t.height,this._fixW=1,this._fixH=1},getLeft:function(){return this.x},setLeft:function(t){this._fixW||(this.width-=t-this.x),this.x=t,this._fixX=0},getTop:function(){return this.y},setTop:function(t){this._fixH||(this.height-=t-this.y),this.y=t,this._fixY=0},getRight:function(){return this.x+this.width},setRight:function(e){this._fixX!==t&&1!==this._fixX&&(this._fixW=0),this._fixW?this.x=e-this.width:this.width=e-this.x,this._fixX=1},getBottom:function(){return this.y+this.height},setBottom:function(e){this._fixY!==t&&1!==this._fixY&&(this._fixH=0),this._fixH?this.y=e-this.height:this.height=e-this.y,this._fixY=1},getCenterX:function(){return this.x+.5*this.width},setCenterX:function(t){this.x=t-.5*this.width,this._fixX=.5},getCenterY:function(){return this.y+.5*this.height},setCenterY:function(t){this.y=t-.5*this.height,this._fixY=.5},getCenter:function(t){var e=t?h:u;return new e(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(){var t=h.read(arguments);return this.setCenterX(t.x),this.setCenterY(t.y),this},getArea:function(){return this.width*this.height},isEmpty:function(){return 0===this.width||0===this.height},contains:function(e){return e&&e.width!==t||4==(Array.isArray(e)?e:arguments).length?this._containsRectangle(d.read(arguments)):this._containsPoint(h.read(arguments))},_containsPoint:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e<=this.x+this.width&&n<=this.y+this.height},_containsRectangle:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e+t.width<=this.x+this.width&&n+t.height<=this.y+this.height},intersects:function(){var t=d.read(arguments);return t.x+t.width>this.x&&t.y+t.height>this.y&&t.x=this.x&&t.y+t.height>=this.y&&t.x<=this.x+this.width&&t.y<=this.y+this.height},intersect:function(){var t=d.read(arguments),e=Math.max(this.x,t.x),n=Math.max(this.y,t.y),i=Math.min(this.x+this.width,t.x+t.width),r=Math.min(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},unite:function(){var t=d.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x+t.width),r=Math.max(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},include:function(){var t=h.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x),r=Math.max(this.y+this.height,t.y);return new d(e,n,i-e,r-n)},expand:function(){var t=l.read(arguments),e=t.width,n=t.height;return new d(this.x-e/2,this.y-n/2,this.width+e,this.height+n)},scale:function(e,n){return this.expand(this.width*e-this.width,this.height*(n===t?e:n)-this.height)}},e.each([["Top","Left"],["Top","Right"],["Bottom","Left"],["Bottom","Right"],["Left","Center"],["Top","Center"],["Right","Center"],["Bottom","Center"]],function(t,e){var n=t.join(""),i=/^[RL]/.test(n);e>=4&&(t[1]+=i?"Y":"X");var r=t[i?0:1],s=t[i?1:0],a="get"+r,o="get"+s,l="set"+r,c="set"+s,d="get"+n,f="set"+n;this[d]=function(t){var e=t?h:u;return new e(this[a](),this[o](),this,f)},this[f]=function(){var t=h.read(arguments);this[l](t.x),this[c](t.y)}},{beans:!0})),f=d.extend({initialize:function(t,e,n,i,r,s){this.set(t,e,n,i,!0),this._owner=r,this._setter=s},set:function(t,e,n,i,r){return this._x=t,this._y=e,this._width=n,this._height=i,r||this._owner[this._setter](this),this}},new function(){var t=d.prototype;return e.each(["x","y","width","height"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._dontNotify||this._owner[this._setter](this)}},e.each(["Point","Size","Center","Left","Top","Right","Bottom","CenterX","CenterY","TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],function(e){var n="set"+e;this[n]=function(){this._dontNotify=!0,t[n].apply(this,arguments),this._dontNotify=!1,this._owner[this._setter](this)}},{isSelected:function(){return this._owner._boundsSelected},setSelected:function(t){var e=this._owner;e.setSelected&&(e._boundsSelected=t,e.setSelected(t||e._selectedSegmentState>0))}}))}),_=e.extend({_class:"Matrix",initialize:function rt(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof rt?this.set(t._a,t._c,t._b,t._d,t._tx,t._ty):Array.isArray(t)?this.set.apply(this,t):n=!1:0===e?this.reset():n=!1,!n)throw Error("Unsupported matrix parameters")},set:function(t,e,n,i,r,s,a){return this._a=t,this._c=e,this._b=n,this._d=i,this._tx=r,this._ty=s,a||this._changed(),this},_serialize:function(t){return e.serialize(this.getValues(),t)},_changed:function(){var t=this._owner;t&&(t._applyMatrix?t.transform(null,!0):t._changed(9))},clone:function(){return new _(this._a,this._c,this._b,this._d,this._tx,this._ty)},equals:function(t){return t===this||t&&this._a===t._a&&this._b===t._b&&this._c===t._c&&this._d===t._d&&this._tx===t._tx&&this._ty===t._ty||!1},toString:function(){var t=s.instance;return"[["+[t.number(this._a),t.number(this._b),t.number(this._tx)].join(", ")+"], ["+[t.number(this._c),t.number(this._d),t.number(this._ty)].join(", ")+"]]"},reset:function(t){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,t||this._changed(),this},apply:function(t,n){var i=this._owner;return i?(i.transform(null,!0,e.pick(t,!0),n),this.isIdentity()):!1},translate:function(){var t=h.read(arguments),e=t.x,n=t.y;return this._tx+=e*this._a+n*this._b,this._ty+=e*this._c+n*this._d,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});return e&&this.translate(e),this._a*=t.x,this._c*=t.x,this._b*=t.y,this._d*=t.y,e&&this.translate(e.negate()),this._changed(),this},rotate:function(t){t*=Math.PI/180;var e=h.read(arguments,1),n=e.x,i=e.y,r=Math.cos(t),s=Math.sin(t),a=n-n*r+i*s,o=i-n*s-i*r,u=this._a,l=this._b,c=this._c,d=this._d;return this._a=r*u+s*l,this._b=-s*u+r*l,this._c=r*c+s*d,this._d=-s*c+r*d,this._tx+=a*u+o*l,this._ty+=a*c+o*d,this._changed(),this},shear:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});e&&this.translate(e);var n=this._a,i=this._c;return this._a+=t.y*this._b,this._c+=t.y*this._d,this._b+=t.x*n,this._d+=t.x*i,e&&this.translate(e.negate()),this._changed(),this},skew:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0}),n=Math.PI/180,i=new h(Math.tan(t.x*n),Math.tan(t.y*n));return this.shear(i,e)},concatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=t._a,a=t._b,o=t._c,h=t._d,u=t._tx,l=t._ty;return this._a=s*e+o*n,this._b=a*e+h*n,this._c=s*i+o*r,this._d=a*i+h*r,this._tx+=u*e+l*n,this._ty+=u*i+l*r,this._changed(),this},preConcatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return this._a=o*e+h*i,this._b=o*n+h*r,this._c=u*e+l*i,this._d=u*n+l*r,this._tx=o*s+h*a+c,this._ty=u*s+l*a+d,this._changed(),this},chain:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty; -return new _(o*e+u*n,o*i+u*r,h*e+l*n,h*i+l*r,s+c*e+d*n,a+c*i+d*r)},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},orNullIfIdentity:function(){return this.isIdentity()?null:this},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,n){return arguments.length<3?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,n)},_transformPoint:function(t,e,n){var i=t.x,r=t.y;return e||(e=new h),e.set(i*this._a+r*this._b+this._tx,i*this._c+r*this._d+this._ty,n)},_transformCoordinates:function(t,e,n){for(var i=0,r=0,s=2*n;s>i;){var a=t[i++],o=t[i++];e[r++]=a*this._a+o*this._b+this._tx,e[r++]=a*this._c+o*this._d+this._ty}return e},_transformCorners:function(t){var e=t.x,n=t.y,i=e+t.width,r=n+t.height,s=[e,n,i,n,i,r,e,r];return this._transformCoordinates(s,s,4)},_transformBounds:function(t,e,n){for(var i=this._transformCorners(t),r=i.slice(0,2),s=i.slice(),a=2;8>a;a++){var o=i[a],h=1&a;os[h]&&(s[h]=o)}return e||(e=new d),e.set(r[0],r[1],s[0]-r[0],s[1]-r[1],n)},inverseTransform:function(){return this._inverseTransform(h.read(arguments))},_getDeterminant:function(){var t=this._a*this._d-this._b*this._c;return isFinite(t)&&!a.isZero(t)&&isFinite(this._tx)&&isFinite(this._ty)?t:null},_inverseTransform:function(t,e,n){var i=this._getDeterminant();if(!i)return null;var r=t.x-this._tx,s=t.y-this._ty;return e||(e=new h),e.set((r*this._d-s*this._b)/i,(s*this._a-r*this._c)/i,n)},decompose:function(){var t=this._a,e=this._b,n=this._c,i=this._d;if(a.isZero(t*i-e*n))return null;var r=Math.sqrt(t*t+e*e);t/=r,e/=r;var s=t*n+e*i;n-=t*s,i-=e*s;var o=Math.sqrt(n*n+i*i);return n/=o,i/=o,s/=o,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,o),rotation:180*-Math.atan2(e,t)/Math.PI,shearing:s}},getValues:function(){return[this._a,this._c,this._b,this._d,this._tx,this._ty]},getTranslation:function(){return new h(this._tx,this._ty)},getScaling:function(){return(this.decompose()||{}).scaling},getRotation:function(){return(this.decompose()||{}).rotation},inverted:function(){var t=this._getDeterminant();return t&&new _(this._d/t,-this._c/t,-this._b/t,this._a/t,(this._b*this._ty-this._d*this._tx)/t,(this._c*this._tx-this._a*this._ty)/t)},shiftless:function(){return new _(this._a,this._c,this._b,this._d,0,0)},applyToContext:function(t){t.transform(this._a,this._c,this._b,this._d,this._tx,this._ty)}},e.each(["a","c","b","d","tx","ty"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._changed()}},{})),g=e.extend({_class:"Line",initialize:function(t,e,n,i,r){var s=!1;arguments.length>=4?(this._px=t,this._py=e,this._vx=n,this._vy=i,s=r):(this._px=t.x,this._py=t.y,this._vx=e.x,this._vy=e.y,s=n),s||(this._vx-=this._px,this._vy-=this._py)},getPoint:function(){return new h(this._px,this._py)},getVector:function(){return new h(this._vx,this._vy)},getLength:function(){return this.getVector().getLength()},intersect:function(t,e){return g.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return g.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(g.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,n,i,r,s,o,u,l,c){l||(n-=t,i-=e,o-=r,u-=s);var d=n*u-i*o;if(!a.isZero(d)){var f=t-r,_=e-s,g=(o*_-u*f)/d,p=(n*_-i*f)/d;if(c||g>=0&&1>=g&&p>=0&&1>=p)return new h(t+g*n,e+g*i)}},getSide:function(t,e,n,i,r,s,a){a||(n-=t,i-=e);var o=r-t,h=s-e,u=o*i-h*n;return 0===u&&(u=o*n+h*i,u>0&&(o-=n,h-=i,u=o*n+h*i,0>u&&(u=0))),0>u?-1:u>0?1:0},getSignedDistance:function(t,e,n,i,r,s,o){return o||(n-=t,i-=e),a.isZero(n)?i>=0?t-r:r-t:a.isZero(i)?n>=0?s-e:e-s:(n*(s-e)-i*(r-t))/Math.sqrt(n*n+i*i)}}}),p=r.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){r.call(this,!0),this.layers=[],this._activeLayer=null,this.symbols=[],this._currentStyle=new R(null,null,this),this._view=V.create(this,t||Y.getCanvas(1,1)),this._selectedItems={},this._selectedItemCount=0,this._updateVersion=0},_serialize:function(t,n){return e.serialize(this.layers,t,!0,n)},clear:function(){for(var t=this.layers.length-1;t>=0;t--)this.layers[t].remove();this.symbols=[]},isEmpty:function(){return 0===this.layers.length},remove:function st(){return st.base.call(this)?(this._view&&this._view.remove(),!0):!1},getView:function(){return this._view},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},getOptions:function(){return this._scope.settings},getActiveLayer:function(){return this._activeLayer||new w({project:this})},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},insertChild:function(t,n,i){return n instanceof w?(n._remove(!1,!0),e.splice(this.layers,[n],t,0),n._setProject(this,!0),this._changes&&n._changed(5),this._activeLayer||(this._activeLayer=n)):n instanceof m?(this._activeLayer||this.insertChild(t,new w(m.NO_INSERT))).insertChild(t,n,i):n=null,n},addChild:function(e,n){return this.insertChild(t,e,n)},_updateSelection:function(t){var e=t._id,n=this._selectedItems;t._selected?n[e]!==t&&(this._selectedItemCount++,n[e]=t):n[e]===t&&(this._selectedItemCount--,delete n[e])},selectAll:function(){for(var t=this.layers,e=0,n=t.length;n>e;e++)t[e].setFullySelected(!0)},deselectAll:function(){var t=this._selectedItems;for(var e in t)t[e].setFullySelected(!1)},hitTest:function(){for(var t=h.read(arguments),n=S.getOptions(e.read(arguments)),i=this.layers.length-1;i>=0;i--){var r=this.layers[i]._hitTest(t,n);if(r)return r}return null},getItems:function(t){return m._getItems(this.layers,t)},getItem:function(t){return m._getItems(this.layers,t,null,null,!0)[0]||null},importJSON:function(t){this.activate();var n=this._activeLayer;return e.importJSON(t,n&&n.isEmpty()&&n)},draw:function(t,n,i){this._updateVersion++,t.save(),n.applyToContext(t);for(var r=new e({offset:new h(0,0),pixelRatio:i,viewMatrix:n.isIdentity()?null:n,matrices:[new _],updateMatrix:!0}),s=0,a=this.layers,o=a.length;o>s;s++)a[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;var u=this._selectedItems,l=this._scope.settings.handleSize,c=this._updateVersion;for(var d in u)u[d]._drawSelection(t,n,l,u,c);t.restore()}}}),v=e.extend({_class:"Symbol",initialize:function(t,e){this._id=o.get(),this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._class,this._definition],t,!1,n)})},_changed:function(t){8&t&&m._clearBoundsCache(this),1&t&&(this.project._needsUpdate=!0)},getDefinition:function(){return this._definition},setDefinition:function(t,e){t._parentSymbol&&(t=t.clone()),this._definition&&(this._definition._parentSymbol=null),this._definition=t,t.remove(),t.setSelected(!1),e||t.setPosition(new h),t._parentSymbol=this,this._changed(9)},place:function(t){return new C(this,t)},clone:function(){return new v(this._definition.clone(!1))},equals:function(t){return t===this||t&&this.definition.equals(t.definition)||!1}}),m=e.extend(n,{statics:{extend:function at(t){return t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields)),at.base.apply(this,arguments)},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,applyMatrix:null,matrix:new _,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,n){var i=t&&e.isPlainObject(t),r=i&&t.internal===!0,s=this._matrix=new _,a=i&&t.project||paper.project;return r||(this._id=o.get()),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,n&&s.translate(n),s._owner=this,this._style=new R(a._currentStyle,this,a),this._project||(r||i&&t.insert===!1?this._setProject(a):i&&t.parent?this.setParent(t.parent):(a._activeLayer||new w).addChild(this)),i&&t!==m.NO_INSERT&&this._set(t,{insert:!0,project:!0,parent:!0},!0),i},_events:new function(){var t={mousedown:{mousedown:1,mousedrag:1,click:1,doubleclick:1},mouseup:{mouseup:1,mousedrag:1,click:1,doubleclick:1},mousemove:{mousedrag:1,mousemove:1,mouseenter:1,mouseleave:1}},n={install:function(e){var n=this.getView()._eventCounters;if(n)for(var i in t)n[i]=(n[i]||0)+(t[i][e]||0)},uninstall:function(e){var n=this.getView()._eventCounters;if(n)for(var i in t)n[i]-=t[i][e]||0}};return e.each(["onMouseDown","onMouseUp","onMouseDrag","onClick","onDoubleClick","onMouseMove","onMouseEnter","onMouseLeave"],function(t){this[t]=n},{onFrame:{install:function(){this._animateItem(!0)},uninstall:function(){this._animateItem(!1)}},onLoad:{}})},_animateItem:function(t){this.getView()._animateItem(this,t)},_serialize:function(t,n){function i(i){for(var a in i){var o=s[a];e.equals(o,"leading"===a?1.2*i.fontSize:i[a])||(r[a]=e.serialize(o,t,"data"!==a,n))}}var r={},s=this;return i(this._serializeFields),this instanceof y||i(this._style._defaults),[this._class,r]},_changed:function(e){var n=this._parentSymbol,i=this._parent||n,r=this._project;if(8&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=this._currentPath=t),i&&40&e&&m._clearBoundsCache(i),2&e&&m._clearBoundsCache(this),r&&(1&e&&(r._needsUpdate=!0),r._changes)){var s=r._changesById[this._id];s?s.flags|=e:(s={item:this,flags:e},r._changesById[this._id]=s,r._changes.push(s))}n&&n._changed(e)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getName:function(){return this._name},setName:function(e,n){if(this._name&&this._removeNamed(),e===+e+"")throw Error("Names consisting only of numbers are not supported.");var i=this._parent;if(e&&i){for(var r=i._children,s=i._namedChildren,a=e,o=1;n&&r[e];)e=a+" "+o++;(s[e]=s[e]||[]).push(this),r[e]=this}this._name=e||t,this._changed(128)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)}},e.each(["locked","visible","blendMode","opacity","guide"],function(t){var n=e.capitalize(t),t="_"+t;this["get"+n]=function(){return this[t]},this["set"+n]=function(e){e!=this[t]&&(this[t]=e,this._changed("_locked"===t?128:129))}},{}),{beans:!0,_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=this._children,e=0,n=t.length;n>e;e++)if(t[e].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var n=this._children,i=0,r=n.length;r>i;i++)n[i].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(129))},_selected:!1,isFullySelected:function(){var t=this._children;if(t&&this._selected){for(var e=0,n=t.length;n>e;e++)if(!t[e].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){var e=this._children;if(e)for(var n=0,i=e.length;i>n;n++)e[n].setFullySelected(t);this.setSelected(t,!0)},isClipMask:function(){return this._clipMask},setClipMask:function(t){this._clipMask!=(t=!!t)&&(this._clipMask=t,t&&(this.setFillColor(null),this.setStrokeColor(null)),this._changed(129),this._parent&&this._parent._changed(1024))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(t){var e=this._position,n=t?h:u;if(!e){var i=this._pivot;e=this._position=i?this._matrix._transformPoint(i):this.getBounds().getCenter(!0)}return new n(e.x,e.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))},getPivot:function(t){var e=this._pivot;if(e){var n=t?h:u;e=new n(e.x,e.y,this,"setPivot")}return e},setPivot:function(){this._pivot=h.read(arguments,0,{clone:!0,readNull:!0}),this._position=t},_pivot:null},e.each(["bounds","strokeBounds","handleBounds","roughBounds","internalBounds","internalRoughBounds"],function(t){var n="get"+e.capitalize(t),i=t.match(/^internal(.*)$/),r=i?"get"+i[1]:null;this[n]=function(e){var i=this._boundsGetter,s=!r&&("string"==typeof i?i:i&&i[n])||n,a=this._getCachedBounds(s,e,this,r);return"bounds"===t?new f(a.x,a.y,a.width,a.height,this,"setBounds"):a}},{beans:!0,_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new d;m._updateBoundsCache(this,n);for(var r=1/0,s=-r,a=r,o=s,h=0,u=i.length;u>h;h++){var l=i[h];if(l._visible&&!l.isEmpty()){var c=l._getCachedBounds(t,e&&e.chain(l._matrix),n);r=Math.min(c.x,r),a=Math.min(c.y,a),s=Math.max(c.x+c.width,s),o=Math.max(c.y+c.height,o)}}return isFinite(r)?new d(r,a,s-r,o-a):new d},setBounds:function(){var t=d.read(arguments),e=this.getBounds(),n=new _,i=t.getCenter();n.translate(i),(t.width!=e.width||t.height!=e.height)&&n.scale(0!=e.width?t.width/e.width:1,0!=e.height?t.height/e.height:1),i=e.getCenter(),n.translate(-i.x,-i.y),this.transform(n)},_getCachedBounds:function(t,e,n,i){e=e&&e.orNullIfIdentity();var r=i?null:this._matrix.orNullIfIdentity(),s=(!e||e.equals(r))&&t;if(m._updateBoundsCache(this._parent||this._parentSymbol,n),s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();var a=this._getBounds(i||t,e||r,n);if(s){this._bounds||(this._bounds={});var o=this._bounds[s]=a.clone();o._internal=!!i}return a},statics:{_updateBoundsCache:function(t,e){if(t){var n=e._id,i=t._boundsCache=t._boundsCache||{ids:{},list:[]};i.ids[n]||(i.list.push(e),i.ids[n]=e)}},_clearBoundsCache:function(e){var n=e._boundsCache;if(n){e._bounds=e._position=e._boundsCache=t;for(var i=0,r=n.list,s=r.length;s>i;i++){var a=r[i];a!==e&&(a._bounds=a._position=t,a._boundsCache&&m._clearBoundsCache(a))}}}}}),{beans:!0,_decompose:function(){return this._decomposed=this._matrix.decompose()},getRotation:function(){var t=this._decomposed||this._decompose();return t&&t.rotation},setRotation:function(t){var e=this.getRotation();if(null!=e&&null!=t){var n=this._decomposed;this.rotate(t-e),n.rotation=t,this._decomposed=n}},getScaling:function(t){var e=this._decomposed||this._decompose(),n=e&&e.scaling,i=t?h:u;return n&&new i(n.x,n.y,this,"setScaling")},setScaling:function(){var t=this.getScaling();if(t){var e=h.read(arguments,0,{clone:!0}),n=this._decomposed;this.scale(e.x/t.x,e.y/t.y),n.scaling=e,this._decomposed=n}},getMatrix:function(){return this._matrix},setMatrix:function(){var t=this._matrix;t.initialize.apply(t,arguments),this._applyMatrix?this.transform(null,!0):this._changed(9)},getGlobalMatrix:function(t){var e=this._globalMatrix,n=this._project._updateVersion;if(e&&e._updateVersion!==n&&(e=null),!e){e=this._globalMatrix=this._matrix.clone();var i=this._parent;i&&e.preConcatenate(i.getGlobalMatrix(!0)),e._updateVersion=n}return t?e:e.clone()},getApplyMatrix:function(){return this._applyMatrix},setApplyMatrix:function(t){(this._applyMatrix=this._canApplyMatrix&&!!t)&&this.transform(null,!0)},getTransformContent:"#getApplyMatrix",setTransformContent:"#setApplyMatrix"},{getProject:function(){return this._project},_setProject:function(t,e){if(this._project!==t){this._project&&this._installEvents(!1),this._project=t;for(var n=this._children,i=0,r=n&&n.length;r>i;i++)n[i]._setProject(t);e=!0}e&&this._installEvents(!0)},getView:function(){return this._project.getView()},_installEvents:function ot(t){ot.base.call(this,t);for(var e=this._children,n=0,i=e&&e.length;i>n;n++)e[n]._installEvents(t)},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof w)return t;return null},getParent:function(){return this._parent},setParent:function(t){return t.addChild(this)},getChildren:function(){return this._children},setChildren:function(t){this.removeChildren(),this.addChildren(t)},getFirstChild:function(){return this._children&&this._children[0]||null},getLastChild:function(){return this._children&&this._children[this._children.length-1]||null},getNextSibling:function(){return this._parent&&this._parent._children[this._index+1]||null},getPreviousSibling:function(){return this._parent&&this._parent._children[this._index-1]||null},getIndex:function(){return this._index},equals:function(t){return t===this||t&&this._class===t._class&&this._style.equals(t._style)&&this._matrix.equals(t._matrix)&&this._locked===t._locked&&this._visible===t._visible&&this._blendMode===t._blendMode&&this._opacity===t._opacity&&this._clipMask===t._clipMask&&this._guide===t._guide&&this._equals(t)||!1},_equals:function(t){return e.equals(this._children,t._children)},clone:function(t){return this._clone(new this.constructor(m.NO_INSERT),t)},_clone:function(n,i,r){var s=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],a=this._children;n.setStyle(this._style);for(var o=0,h=a&&a.length;h>o;o++)n.addChild(a[o].clone(!1),!0);for(var o=0,h=s.length;h>o;o++){var u=s[o];this.hasOwnProperty(u)&&(n[u]=this[u])}return r!==!1&&n._matrix.initialize(this._matrix),n.setApplyMatrix(this._applyMatrix),n.setPivot(this._pivot),n.setSelected(this._selected),n._data=this._data?e.clone(this._data):null,(i||i===t)&&n.insertAbove(this),this._name&&n.setName(this._name,!0),n},copyTo:function(t){return t.addChild(this.clone(!1))},rasterize:function(t){var n=this.getStrokeBounds(),i=(t||this.getView().getResolution())/72,r=n.getTopLeft().floor(),s=n.getBottomRight().ceil(),a=new l(s.subtract(r)),o=Y.getCanvas(a.multiply(i)),h=o.getContext("2d"),u=(new _).scale(i).translate(r.negate());h.save(),u.applyToContext(h),this.draw(h,new e({matrices:[u]})),h.restore();var c=new b(m.NO_INSERT);return c.setCanvas(o),c.transform((new _).translate(r.add(a.divide(2))).scale(1/i)),c.insertAbove(this),c},contains:function(){return!!this._contains(this._matrix._inverseTransform(h.read(arguments)))},_contains:function(t){if(this._children){for(var e=this._children.length-1;e>=0;e--)if(this._children[e].contains(t))return!0;return!1}return t.isInside(this.getInternalBounds())},isInside:function(){return d.read(arguments).contains(this.getBounds())},_asPathItem:function(){return new A.Rectangle({rectangle:this.getInternalBounds(),matrix:this._matrix,insert:!1})},intersects:function(t,e){return t instanceof m?this._asPathItem().getIntersections(t._asPathItem(),e||t._matrix).length>0:!1},hitTest:function(){return this._hitTest(h.read(arguments),S.getOptions(e.read(arguments)))},_hitTest:function(n,i){function r(i,r){var s=_["get"+r]();return n.subtract(s).divide(u).length<=1?new S(i,f,{name:e.hyphenate(r),point:s}):t}if(this._locked||!this._visible||this._guide&&!i.guides||this.isEmpty())return null;var s=this._matrix,a=i._totalMatrix,o=this.getView(),h=i._totalMatrix=a?a.chain(s):this.getGlobalMatrix().preConcatenate(o._matrix),u=i._tolerancePadding=new l(A._getPenPadding(1,h.inverted())).multiply(Math.max(i.tolerance,1e-6));if(n=s._inverseTransform(n),!this._children&&!this.getInternalRoughBounds().expand(u.multiply(2))._containsPoint(n))return null;var c,d=!(i.guides&&!this._guide||i.selected&&!this._selected||i.type&&i.type!==e.hyphenate(this._class)||i["class"]&&!(this instanceof i["class"])),f=this;if(d&&(i.center||i.bounds)&&this._parent){var _=this.getInternalBounds();if(i.center&&(c=r("center","Center")),!c&&i.bounds)for(var g=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],p=0;8>p&&!c;p++)c=r("bounds",g[p])}var v=!c&&this._children;if(v)for(var m=this._getChildHitTestOptions(i),p=v.length-1;p>=0&&!c;p--)c=v[p]._hitTest(n,m);return!c&&d&&(c=this._hitTestSelf(n,i)),c&&c.point&&(c.point=s.transform(c.point)),i._totalMatrix=a,c},_getChildHitTestOptions:function(t){return t},_hitTestSelf:function(e,n){return n.fill&&this.hasFill()&&this._contains(e)?new S("fill",this):t},matches:function(t,n){function i(t,n){for(var r in t)if(t.hasOwnProperty(r)){var s=t[r],a=n[r];if(e.isPlainObject(s)&&e.isPlainObject(a)){if(!i(s,a))return!1}else if(!e.equals(s,a))return!1}return!0}var r=typeof t;if("object"===r){for(var s in t)if(t.hasOwnProperty(s)&&!this.matches(s,t[s]))return!1}else{if("function"===r)return t(this);var a=/^(empty|editable)$/.test(t)?this["is"+e.capitalize(t)]():"type"===t?e.hyphenate(this._class):this[t];if(/^(constructor|class)$/.test(t)){if(!(this instanceof n))return!1}else if(n instanceof RegExp){if(!n.test(a))return!1}else if("function"==typeof n){if(!n(a))return!1}else if(e.isPlainObject(n)){if(!i(n,a))return!1}else if(!e.equals(a,n))return!1}return!0},getItems:function(t){return m._getItems(this._children,t,this._matrix)},getItem:function(t){return m._getItems(this._children,t,this._matrix,null,!0)[0]||null},statics:{_getItems:function ht(t,n,i,r,s){if(!r&&"object"==typeof n){var a=n.overlapping,o=n.inside,h=a||o,u=h&&d.read([h]);r={items:[],inside:!!o,overlapping:!!a,rect:u,path:a&&new A.Rectangle({rectangle:u,insert:!1})},h&&(n=e.set({},n,{inside:!0,overlapping:!0}))}var l=r&&r.items,u=r&&r.rect;i=u&&(i||new _);for(var c=0,f=t&&t.length;f>c;c++){var g=t[c],p=i&&i.chain(g._matrix),v=!0;if(u){var h=g.getBounds(p);if(!u.intersects(h))continue;r.inside&&u.contains(h)||r.overlapping&&(h.contains(u)||r.path.intersects(g,p))||(v=!1)}if(v&&g.matches(n)&&(l.push(g),s))break;if(ht(g._children,n,p,r,s),s&&l.length>0)break}return l}}},{importJSON:function(t){var n=e.importJSON(t,this);return n!==this?this.addChild(n):n},addChild:function(e,n){return this.insertChild(t,e,n)},insertChild:function(t,e,n){var i=e?this.insertChildren(t,[e],n):null;return i&&i[0]},addChildren:function(t,e){return this.insertChildren(this._children.length,t,e)},insertChildren:function(t,n,i,r){var s=this._children;if(s&&n&&n.length>0){n=Array.prototype.slice.apply(n);for(var a=n.length-1;a>=0;a--){var o=n[a];if(!r||o instanceof r){var h=o._parent===this&&o._indexa;a++){var o=n[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name),l&&this._changed(5)}this._changed(11)}else n=null;return n},_insertSibling:function(t,e,n){return this._parent?this._parent.insertChild(t,e,n):null},insertAbove:function(t,e){return t._insertSibling(t._index+1,this,e)},insertBelow:function(t,e){return t._insertSibling(t._index,this,e)},sendToBack:function(){return(this._parent||this instanceof w&&this._project).insertChild(0,this)},bringToFront:function(){return(this._parent||this instanceof w&&this._project).addChild(this)},appendTop:"#addChild",appendBottom:function(t){return this.insertChild(0,t)},moveAbove:"#insertAbove",moveBelow:"#insertBelow",reduce:function(){if(this._children&&1===this._children.length){var t=this._children[0].reduce();return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent;if(t){var e=t._children,n=t._namedChildren,i=this._name,r=n[i],s=r?r.indexOf(this):-1;-1!==s&&(e[i]==this&&delete e[i],r.splice(s,1),r.length?e[i]=r[r.length-1]:delete n[i])}},_remove:function(t,n){var i=this._parent;if(i){if(this._name&&this._removeNamed(),null!=this._index&&e.splice(i._children,null,this._index,1),this._installEvents(!1),t){var r=this._project;r&&r._changes&&this._changed(5)}return n&&i._changed(11),this._parent=null,!0}return!1},remove:function(){return this._remove(!0,!0)},replaceWith:function(t){var e=t&&t.insertBelow(this);return e&&this.remove(),e},removeChildren:function(t,n){if(!this._children)return null;t=t||0,n=e.pick(n,this._children.length);for(var i=e.splice(this._children,null,t,n-t),r=i.length-1;r>=0;r--)i[r]._remove(!0,!1);return i.length>0&&this._changed(11),i},clear:"#removeChildren",reverseChildren:function(){if(this._children){this._children.reverse();for(var t=0,e=this._children.length;e>t;t++)this._children[t]._index=t;this._changed(11)}},isEmpty:function(){return!this._children||0===this._children.length},isEditable:function(){for(var t=this;t;){if(!t._visible||t._locked)return!1;t=t._parent}return!0},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()},_getOrder:function(t){function e(t){var e=[];do e.unshift(t);while(t=t._parent);return e}for(var n=e(this),i=e(t),r=0,s=Math.min(n.length,i.length);s>r;r++)if(n[r]!=i[r])return n[r]._index0},isInserted:function(){return this._parent?this._parent.isInserted():!1},isAbove:function(t){return-1===this._getOrder(t)},isBelow:function(t){return 1===this._getOrder(t)},isParent:function(t){return this._parent===t},isChild:function(t){return t&&t._parent===this},isDescendant:function(t){for(var e=this;e=e._parent;)if(e==t)return!0;return!1},isAncestor:function(t){return t?t.isDescendant(this):!1},isGroupedWith:function(t){for(var e=this._parent;e;){if(e._parent&&/^(Group|Layer|CompoundPath)$/.test(e._class)&&t.isDescendant(e))return!0;e=e._parent}return!1},translate:function(){var t=new _;return this.transform(t.translate.apply(t,arguments))},rotate:function(t){return this.transform((new _).rotate(t,h.read(arguments,1,{readNull:!0})||this.getPosition(!0)))}},e.each(["scale","shear","skew"],function(t){this[t]=function(){var e=h.read(arguments),n=h.read(arguments,0,{readNull:!0});return this.transform((new _)[t](e,n||this.getPosition(!0)))}},{}),{transform:function(t,e,n,i){t&&t.isIdentity()&&(t=null);var r=this._matrix,s=(e||this._applyMatrix)&&(!r.isIdentity()||t||e&&n&&this._children);if(!t&&!s)return this;if(t&&r.preConcatenate(t),s=s&&this._transformContent(r,n,i)){var a=this._pivot,o=this._style,h=o.getFillColor(!0),u=o.getStrokeColor(!0);a&&r._transformPoint(a,a,!0),h&&h.transform(r),u&&u.transform(r),r.reset(!0),i&&this._canApplyMatrix&&(this._applyMatrix=!0)}var l=this._bounds,c=this._position;this._changed(9);var d=l&&t&&t.decompose();if(d&&!d.shearing&&d.rotation%90===0){for(var f in l){var _=l[f];(s||!_._internal)&&t._transformBounds(_,_)}var g=this._boundsGetter,_=l[g&&g.getBounds||g||"getBounds"];_&&(this._position=_.getCenter(!0)),this._bounds=l}else t&&c&&(this._position=t._transformPoint(c,c));return this},_transformContent:function(t,e,n){var i=this._children;if(i){for(var r=0,s=i.length;s>r;r++)i[r].transform(t,!0,e,n);return!0}},globalToLocal:function(){return this.getGlobalMatrix(!0)._inverseTransform(h.read(arguments))},localToGlobal:function(){return this.getGlobalMatrix(!0)._transformPoint(h.read(arguments))},parentToLocal:function(){return this._matrix._inverseTransform(h.read(arguments))},localToParent:function(){return this._matrix._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=d.read(arguments);var n=this.getBounds(),i=n.height/n.width,r=t.height/t.width,s=(e?i>r:r>i)?t.width/n.width:t.height/n.height,a=new d(new h,new l(n.width*s,n.height*s));a.setCenter(t.getCenter()),this.setBounds(a)},_setStyles:function(t){var e=this._style,n=e.getFillColor(),i=e.getStrokeColor(),r=e.getShadowColor();if(n&&(t.fillStyle=n.toCanvasStyle(t)),i){var s=e.getStrokeWidth();if(s>0){t.strokeStyle=i.toCanvasStyle(t),t.lineWidth=s;var a=e.getStrokeJoin(),o=e.getStrokeCap(),h=e.getMiterLimit();if(a&&(t.lineJoin=a),o&&(t.lineCap=o),h&&(t.miterLimit=h),paper.support.nativeDash){var u=e.getDashArray(),l=e.getDashOffset();u&&u.length&&("setLineDash"in t?(t.setLineDash(u),t.lineDashOffset=l):(t.mozDash=u,t.mozDashOffset=l))}}}if(r){var c=e.getShadowBlur();if(c>0){t.shadowColor=r.toCanvasStyle(t),t.shadowBlur=c;var d=this.getShadowOffset();t.shadowOffsetX=d.x,t.shadowOffsetY=d.y}}},draw:function(t,e,n){function i(t){return a?a.chain(t):t}var r=this._updateVersion=this._project._updateVersion;if(this._visible&&0!==this._opacity){var s=e.matrices,a=e.viewMatrix,o=this._matrix,h=s[s.length-1].chain(o);if(h.isInvertible()){s.push(h),e.updateMatrix&&(h._updateVersion=r,this._globalMatrix=h);var u,l,c,d=this._blendMode,f=this._opacity,_="normal"===d,g=K.nativeModes[d],p=_&&1===f||e.dontStart||e.clip||(g||_&&1>f)&&this._canComposite(),v=e.pixelRatio||1;if(!p){var m=this.getStrokeBounds(i(h));if(!m.width||!m.height)return;c=e.offset,l=e.offset=m.getTopLeft().floor(),u=t,t=Y.getContext(m.getSize().ceil().add(1).multiply(v)),1!==v&&t.scale(v,v)}t.save();var y=n?n.chain(o):!this.getStrokeScaling(!0)&&i(h),w=!p&&e.clipItem,x=!y||w;if(p?(t.globalAlpha=f,g&&(t.globalCompositeOperation=d)):x&&t.translate(-l.x,-l.y),x&&(p?o:i(h)).applyToContext(t),w&&e.clipItem.draw(t,e.extend({clip:!0})),y){t.setTransform(v,0,0,v,0,0);var b=e.offset;b&&t.translate(-b.x,-b.y)}this._draw(t,e,y),t.restore(),s.pop(),e.clip&&!e.dontFinish&&t.clip(),p||(K.process(d,t,u,f,l.subtract(c).multiply(v)),Y.release(t),e.offset=c)}}},_isUpdated:function(t){var e=this._parent;if(e instanceof O)return e._isUpdated(t);var n=this._updateVersion===t;return!n&&e&&e._visible&&e._isUpdated(t)&&(this._updateVersion=t,n=!0),n},_drawSelection:function(t,e,n,i,r){if((this._drawSelected||this._boundsSelected)&&this._isUpdated(r)){var s=this.getSelectedColor(!0)||this.getLayer().getSelectedColor(!0),a=e.chain(this.getGlobalMatrix(!0));if(t.strokeStyle=t.fillStyle=s?s.toCanvasStyle(t):"#009dec",this._drawSelected&&this._drawSelected(t,a,i),this._boundsSelected){var o=n/2;coords=a._transformCorners(this.getInternalBounds()),t.beginPath();for(var h=0;8>h;h++)t[0===h?"moveTo":"lineTo"](coords[h],coords[++h]);t.closePath(),t.stroke();for(var h=0;8>h;h++)t.fillRect(coords[h]-o,coords[++h]-o,n,n)}}},_canComposite:function(){return!1}},e.each(["down","drag","up","move"],function(t){this["removeOn"+e.capitalize(t)]=function(){var e={};return e[t]=!0,this.removeOn(e)}},{removeOn:function(t){for(var e in t)if(t[e]){var n="mouse"+e,i=this._project,r=i._removeSets=i._removeSets||{};r[n]=r[n]||{},r[n][this._id]=this}return this}})),y=m.extend({_class:"Group",_selectChildren:!0,_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ut(e){ut.base.call(this,e),1026&e&&(this._clipItem=t)},_getClipItem:function(){var e=this._clipItem;if(e===t){e=null;for(var n=0,i=this._children.length;i>n;n++){var r=this._children[n];if(r._clipMask){e=r;break}}this._clipItem=e}return e},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clip,i=!n&&this._getClipItem(),r=!0;if(e=e.extend({clipItem:i,clip:!1}),n?this._currentPath?(t.currentPath=this._currentPath,r=!1):(t.beginPath(),e.dontStart=e.dontFinish=!0):i&&i.draw(t,e.extend({clip:!0})),r)for(var s=0,a=this._children.length;a>s;s++){var o=this._children[s];o!==i&&o.draw(t,e)}n&&(this._currentPath=t.currentPath)}}),w=y.extend({_class:"Layer",initialize:function(n){var i=e.isPlainObject(n)?new e(n):{children:Array.isArray(n)?n:arguments},r=i.insert;i.insert=!1,y.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function lt(t,n){if(this._parent)return lt.base.call(this,t,n);if(null!=this._index){var i=this._project;return i._activeLayer===this&&(i._activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(i.layers,null,this._index,1),this._installEvents(!1),t&&i._changes&&this._changed(5),n&&(i._needsUpdate=!0),!0}return!1},getNextSibling:function ct(){return this._parent?ct.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function dt(){return this._parent?dt.base.call(this):this._project.layers[this._index-1]||null},isInserted:function ft(){return this._parent?ft.base.call(this):null!=this._index},activate:function(){this._project._activeLayer=this},_insertSibling:function _t(t,e,n){return this._parent?_t.base.call(this,t,e,n):this._project.insertChild(t,e,n)}}),x=m.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{type:null,size:null,radius:null -},initialize:function(t){this._initialize(t)},_equals:function(t){return this._type===t._type&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new x(m.NO_INSERT);return e.setType(this._type),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getType:function(){return this._type},setType:function(t){this._type=t},getShape:"#getType",setShape:"#setType",getSize:function(){var t=this._size;return new c(t.width,t.height,this,"setSize")},setSize:function(){var t=l.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._type,n=t.width,i=t.height;if("rectangle"===e){var r=l.min(this._radius,t.divide(2));this._radius.set(r.width,r.height)}else"circle"===e?(n=i=(n+i)/2,this._radius=n/2):"ellipse"===e&&this._radius.set(n/2,i/2);this._size.set(n,i),this._changed(9)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._type?t:new c(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._type;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else if(t=l.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=l.max(this._size,t.multiply(2));this._size.set(n.width,n.height)}else"ellipse"===e&&this._size.set(2*t.width,2*t.height)}else this._radius=t.clone();this._changed(9)},isEmpty:function(){return!1},toPath:function(t){var n=this._clone(new(A[e.capitalize(this._type)])({center:new h,size:this._size,radius:this._radius,insert:!1}),t);return paper.settings.applyMatrix&&n.setApplyMatrix(!0),n},_draw:function(t,e,n){var i=this._style,r=i.hasFill(),s=i.hasStroke(),a=e.dontFinish||e.clip,o=!n;if(r||s||a){var h=this._type,u=this._radius,l="circle"===h;if(e.dontStart||t.beginPath(),o&&l)t.arc(0,0,u,0,2*Math.PI,!0);else{var c=l?u:u.width,d=l?u:u.height,f=this._size,_=f.width,g=f.height;if(o&&"rectangle"===h&&0===c&&0===d)t.rect(-_/2,-g/2,_,g);else{var p=_/2,v=g/2,m=.44771525016920644,y=c*m,w=d*m,x=[-p,-v+d,-p,-v+w,-p+y,-v,-p+c,-v,p-c,-v,p-y,-v,p,-v+w,p,-v+d,p,v-d,p,v-w,p-y,v,p-c,v,-p+c,v,-p+y,v,-p,v-w,-p,v-d];n&&n.transform(x,x,32),t.moveTo(x[0],x[1]),t.bezierCurveTo(x[2],x[3],x[4],x[5],x[6],x[7]),p!==c&&t.lineTo(x[8],x[9]),t.bezierCurveTo(x[10],x[11],x[12],x[13],x[14],x[15]),v!==d&&t.lineTo(x[16],x[17]),t.bezierCurveTo(x[18],x[19],x[20],x[21],x[22],x[23]),p!==c&&t.lineTo(x[24],x[25]),t.bezierCurveTo(x[26],x[27],x[28],x[29],x[30],x[31])}}t.closePath()}a||!r&&!s||(this._setStyles(t),r&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return"getBounds"!==t&&this.hasStroke()&&(n=n.expand(this.getStrokeWidth())),e?e._transformBounds(n):n}},new function(){function t(t,e,n){var i=t._radius;if(!i.isZero())for(var r=t._size.divide(2),s=0;4>s;s++){var a=new h(1&s?1:-1,s>1?1:-1),o=a.multiply(r),u=o.subtract(a.multiply(i)),l=new d(o,u);if((n?l.expand(n):l).contains(e))return u}}function e(t,e){var n=t.getAngleInRadians(),i=2*e.width,r=2*e.height,s=i*Math.sin(n),a=r*Math.cos(n);return i*r/(2*Math.sqrt(s*s+a*a))}return{_contains:function n(e){if("rectangle"===this._type){var i=t(this,e);return i?e.subtract(i).divide(this._radius).getLength()<=1:n.base.call(this,e)}return e.divide(this.size).getLength()<=.5},_hitTestSelf:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._type,o=this._radius,h=this.getStrokeWidth()+2*r.tolerance;if("rectangle"===a){var u=t(this,n,h);if(u){var l=n.subtract(u);s=2*Math.abs(l.getLength()-e(l,o))<=h}else{var c=new d(this._size).setCenter(0,0),f=c.expand(h),_=c.expand(-h);s=f._containsPoint(n)&&!_._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new S("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){var a=new x(e.getNamed(s));return a._type=t,a._size=i,a._radius=r,a.translate(n)}return{Circle:function(){var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t("circle",n,new l(2*i),i,arguments)},Rectangle:function(){var e=d.readNamed(arguments,"rectangle"),n=l.min(l.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=x._readEllipse(arguments),n=e.radius;return t("ellipse",e.center,n.multiply(2),n,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=h.readNamed(t,"center"),i=l.readNamed(t,"radius");else{var r=d.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),b=m.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{crossOrigin:null,source:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||("string"==typeof e?this.setSource(e):this.setImage(e)),this._size||(this._size=new l,this._loaded=!1)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new b(m.NO_INSERT),n=this._image,i=this._canvas;if(n)e.setImage(n);else if(i){var r=Y.getCanvas(this._size);r.getContext("2d").drawImage(i,0,0),e.setImage(r)}return e._crossOrigin=this._crossOrigin,this._clone(e,t)},getSize:function(){var t=this._size;return new c(t?t.width:0,t?t.height:0,this,"setSize")},setSize:function(){var t=l.read(arguments);if(!t.equals(this._size))if(t.width>0&&t.height>0){var e=this.getElement();this.setImage(Y.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}else this._canvas&&Y.release(this._canvas),this._size=t.clone()},getWidth:function(){return this._size?this._size.width:0},setWidth:function(t){this.setSize(t,this.getHeight())},getHeight:function(){return this._size?this._size.height:0},setHeight:function(t){this.setSize(this.getWidth(),t)},isEmpty:function(){var t=this._size;return!t||0===t.width&&0===t.height},getResolution:function(){var t=this._matrix,e=new h(0,0).transform(t),n=new h(1,0).transform(t).subtract(e),i=new h(0,1).transform(t).subtract(e);return new l(72/n.getLength(),72/i.getLength())},getPpi:"#getResolution",getImage:function(){return this._image},setImage:function(t){this._canvas&&Y.release(this._canvas),t&&t.getContext?(this._image=null,this._canvas=t,this._loaded=!0):(this._image=t,this._canvas=null,this._loaded=t&&t.complete),this._size=new l(t?t.naturalWidth||t.width:0,t?t.naturalHeight||t.height:0),this._context=null,this._changed(521)},getCanvas:function(){if(!this._canvas){var t=Y.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){Y.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(t){return this._context||(this._context=this.getCanvas().getContext("2d")),t&&(this._image=null,this._changed(513)),this._context},setContext:function(t){this._context=t},getSource:function(){return this._image&&this._image.src||this.toDataURL()},setSource:function(t){function e(){var t=i.getView();t&&(paper=t._scope,i.setImage(n),i.emit("load"),t.update())}var n,i=this,r=this._crossOrigin;n=document.getElementById(t)||new Image,r&&(n.crossOrigin=r),n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(q.add(n,{load:e}),n.src||(n.src=t)),this.setImage(n)},getCrossOrigin:function(){return this._image&&this._image.crossOrigin||this._crossOrigin||""},setCrossOrigin:function(t){this._crossOrigin=t,this._image&&(this._image.crossOrigin=t)},getElement:function(){return this._canvas||this._loaded&&this._image}},{beans:!1,getSubCanvas:function(){var t=d.read(arguments),e=Y.getContext(t.getSize());return e.drawImage(this.getCanvas(),t.x,t.y,t.width,t.height,0,0,t.width,t.height),e.canvas},getSubRaster:function(){var t=d.read(arguments),e=new b(m.NO_INSERT);return e.setImage(this.getSubCanvas(t)),e.translate(t.getCenter().subtract(this.getSize().divide(2))),e._matrix.preConcatenate(this._matrix),e.insertAbove(this),e},toDataURL:function(){var t=this._image&&this._image.src;if(/^data:/.test(t))return t;var e=this.getCanvas();return e?e.toDataURL():null},drawImage:function(t){var e=h.read(arguments,1);this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var n,i;t?t instanceof z?(i=t,n=t.getBounds()):t.width?n=new d(t):t.x&&(n=new d(t.x-.5,t.y-.5,1,1)):n=this.getBounds();var r=32,s=Math.min(n.width,r),a=Math.min(n.height,r),o=b._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=b._sampleContext=Y.getContext(new l(r)),o.save();var h=(new _).scale(s/n.width,a/n.height).translate(-n.x,-n.y);h.applyToContext(o),i&&i.draw(o,new e({clip:!0,matrices:[h]})),this._matrix.applyToContext(o);var u=this.getElement(),c=this._size;u&&o.drawImage(u,-c.width/2,-c.height/2),o.restore();for(var f=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,g=[0,0,0],p=0,v=0,m=f.length;m>v;v+=4){var y=f[v+3];p+=y,y/=255,g[0]+=f[v]*y,g[1]+=f[v+1]*y,g[2]+=f[v+2]*y}for(var v=0;3>v;v++)g[v]/=p;return p?j.read(g):null},getPixel:function(){var t=h.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new j("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=j.read(arguments),n=e._convert("rgb"),i=e._alpha,r=this.getContext(!0),s=r.createImageData(1,1),a=s.data;a[0]=255*n[0],a[1]=255*n[1],a[2]=255*n[2],a[3]=null!=i?255*i:255,r.putImageData(s,t.x,t.y)},createImageData:function(){var t=l.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(){var t=d.read(arguments);return t.isEmpty()&&(t=new d(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t){var e=h.read(arguments,1);this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTestSelf:function(t){if(this._contains(t)){var e=this;return new S("pixel",e,{offset:t.add(e._size.divide(2)).round(),color:{get:function(){return e.getPixel(this.offset)}}})}},_draw:function(t){var e=this.getElement();e&&(t.globalAlpha=this._opacity,t.drawImage(e,-this._size.width/2,-this._size.height/2))},_canComposite:function(){return!0}}),C=m.extend({_class:"PlacedSymbol",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:{getBounds:"getStrokeBounds"},_boundsSelected:!0,_serializeFields:{symbol:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||this.setSymbol(e instanceof v?e:new v(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(9)},clone:function(t){var e=new C(m.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,n){var i=this.symbol._definition;return i._getCachedBounds(t,e&&e.chain(i._matrix),n)},_hitTestSelf:function(t,e){var n=this._symbol._definition._hitTest(t,e);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),S=e.extend({_class:"HitResult",initialize:function(t,e,n){this.type=t,this.item=e,n&&(n.enumerable=!0,this.inject(n))},statics:{getOptions:function(t){return new e({type:null,tolerance:paper.settings.hitTolerance,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1},t)}}}),P=e.extend({_class:"Segment",beans:!0,initialize:function(e,n,i,r,s,a){var o,h,u,l=arguments.length;0===l||(1===l?e.point?(o=e.point,h=e.handleIn,u=e.handleOut):o=e:2===l&&"number"==typeof e?o=arguments:3>=l?(o=e,h=n,u=i):(o=e!==t?[e,n]:null,h=i!==t?[i,r]:null,u=s!==t?[s,a]:null)),new k(o,this,"_point"),new k(h,this,"_handleIn"),new k(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.isStraight()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){var e=this._path;if(e){var n,i=e._curves,r=this._index;i&&(t&&t!==this._point&&t!==this._handleIn||!(n=r>0?i[r-1]:e._closed?i[i.length-1]:null)||n._changed(),t&&t!==this._point&&t!==this._handleOut||!(n=i[r])||n._changed()),e._changed(25)}},getPoint:function(){return this._point},setPoint:function(){var t=h.read(arguments);this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(){var t=h.read(arguments);this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(){var t=h.read(arguments);this._handleOut.set(t.x,t.y)},hasHandles:function(){return!this.isStraight()},isStraight:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},isLinear:function(){return P.isLinear(this,this.getNext())},isCollinear:function(t){return P.isCollinear(this,this.getNext(),t,t.getNext())},isColinear:"#isCollinear",isOrthogonal:function(){return P.isOrthogonal(this.getPrevious(),this,this.getNext())},isOrthogonalArc:function(){return P.isOrthogonalArc(this,this.getNext())},isArc:"#isOrthogonalArc",_selectionState:0,isSelected:function(t){var e=this._selectionState;return t?t===this._point?!!(4&e):t===this._handleIn?!!(1&e):t===this._handleOut?!!(2&e):!1:!!(7&e)},setSelected:function(t,e){var n=this._path,t=!!t,i=this._selectionState,r=i,s=e?e===this._point?4:e===this._handleIn?1:e===this._handleOut?2:0:7;t?i|=s:i&=~s,this._selectionState=i,n&&i!==r&&(n._updateSelection(this,r,i),n._changed(129))},getIndex:function(){return this._index!==t?this._index:null},getPath:function(){return this._path||null},getCurve:function(){var t=this._path,e=this._index;return t?(e>0&&!t._closed&&e===t._segments.length-1&&e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new I(t,this===t._segment1?0:1):null},getNext:function(){var t=this._path&&this._path._segments;return t&&(t[this._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._segments;return t&&(t[this._index-1]||this._path._closed&&t[t.length-1])||null},reverse:function(){return new P(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new P(this._point,this._handleIn,this._handleOut)},equals:function(t){return t===this||t&&this._class===t._class&&this._point.equals(t._point)&&this._handleIn.equals(t._handleIn)&&this._handleOut.equals(t._handleOut)||!1},toString:function(){var t=["point: "+this._point];return this._handleIn.isZero()||t.push("handleIn: "+this._handleIn),this._handleOut.isZero()||t.push("handleOut: "+this._handleOut),"{ "+t.join(", ")+" }"},transform:function(t){this._transformCoordinates(t,Array(6),!0),this._changed()},_transformCoordinates:function(t,e,n){var i=this._point,r=n&&this._handleIn.isZero()?null:this._handleIn,s=n&&this._handleOut.isZero()?null:this._handleOut,a=i._x,o=i._y,h=2;return e[0]=a,e[1]=o,r&&(e[h++]=r._x+a,e[h++]=r._y+o),s&&(e[h++]=s._x+a,e[h++]=s._y+o),t&&(t._transformCoordinates(e,e,h/2),a=e[0],o=e[1],n?(i._x=a,i._y=o,h=2,r&&(r._x=e[h++]-a,r._y=e[h++]-o),s&&(s._x=e[h++]-a,s._y=e[h++]-o)):(r||(e[h++]=a,e[h++]=o),s||(e[h++]=a,e[h++]=o))),e},statics:{isLinear:function(t,e){var n=e._point.subtract(t._point);return n.isCollinear(t._handleOut)&&n.isCollinear(e._handleIn)},isCollinear:function(t,e,n,i){return t._handleOut.isZero()&&e._handleIn.isZero()&&n._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(t._point).isCollinear(i._point.subtract(n._point))},isOrthogonal:function(t,e,n){return t._handleOut.isZero()&&e._handleIn.isZero()&&e._handleOut.isZero()&&n._handleIn.isZero()&&e._point.subtract(t._point).isOrthogonal(n._point.subtract(e._point))},isOrthogonalArc:function(t,e){var n=t._handleOut,i=e._handleIn,r=.5522847498307936;if(n.isOrthogonal(i)){var s=t._point,o=e._point,h=new g(s,n,!0).intersect(new g(o,i,!0),!0);return h&&a.isZero(n.getLength()/h.subtract(s).getLength()-r)&&a.isZero(i.getLength()/h.subtract(o).getLength()-r)}return!1}}}),k=h.extend({initialize:function(e,n,i){var r,s,a;if(e)if((r=e[0])!==t)s=e[1];else{var o=e;(r=o.x)===t&&(o=h.read(arguments),r=o.x),s=o.y,a=o.selected}else r=s=0;this._x=r,this._y=s,this._owner=n,n[i]=this,a&&this.setSelected(!0)},set:function(t,e){return this._x=t,this._y=e,this._owner._changed(this),this},_serialize:function(t){var e=t.formatter,n=e.number(this._x),i=e.number(this._y);return this.isSelected()?{x:n,y:i,selected:!0}:[n,i]},getX:function(){return this._x},setX:function(t){this._x=t,this._owner._changed(this)},getY:function(){return this._y},setY:function(t){this._y=t,this._owner._changed(this)},isZero:function(){return a.isZero(this._x)&&a.isZero(this._y)},setSelected:function(t){this._owner.setSelected(t,this)},isSelected:function(){return this._owner.isSelected(this)}}),M=e.extend({_class:"Curve",initialize:function(t,e,n,i,r,s,a,o){var h=arguments.length;if(3===h)this._path=t,this._segment1=e,this._segment2=n;else if(0===h)this._segment1=new P,this._segment2=new P;else if(1===h)this._segment1=new P(t.segment1),this._segment2=new P(t.segment2);else if(2===h)this._segment1=new P(t),this._segment2=new P(e);else{var u,l,c,d;4===h?(u=t,l=e,c=n,d=i):8===h&&(u=[t,e],d=[a,o],l=[n-t,i-e],c=[r-a,s-o]),this._segment1=new P(u,null,l),this._segment2=new P(d,c,null)}},_changed:function(){this._length=this._bounds=t},getPoint1:function(){return this._segment1._point},setPoint1:function(){var t=h.read(arguments);this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(){var t=h.read(arguments);this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(){var t=h.read(arguments);this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(){var t=h.read(arguments);this._segment2._handleIn.set(t.x,t.y)},getSegment1:function(){return this._segment1},getSegment2:function(){return this._segment2},getPath:function(){return this._path},getIndex:function(){return this._segment1._index},getNext:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index-1]||this._path._closed&&t[t.length-1])||null},isSelected:function(){return this.getPoint1().isSelected()&&this.getHandle2().isSelected()&&this.getHandle2().isSelected()&&this.getPoint2().isSelected()},setSelected:function(t){this.getPoint1().setSelected(t),this.getHandle1().setSelected(t),this.getHandle2().setSelected(t),this.getPoint2().setSelected(t)},getValues:function(t){return M.getValues(this._segment1,this._segment2,t)},getPoints:function(){for(var t=this.getValues(),e=[],n=0;8>n;n+=2)e.push(new h(t[n],t[n+1]));return e},getLength:function(){return null==this._length&&(this._length=this.isLinear()?this._segment2._point.getDistance(this._segment1._point):M.getLength(this.getValues(),0,1)),this._length},getArea:function(){return M.getArea(this.getValues())},getPart:function(t,e){return new M(M.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return M.getLength(this.getValues(),t,e)},hasHandles:function(){return!this._segment1._handleOut.isZero()||!this._segment2._handleIn.isZero()},isLinear:function(){return P.isLinear(this._segment1,this._segment2)},isCollinear:function(t){return Ssegment.isCollinear(this._segment1,this._segment2,t._segment1,t._segment2)},isOrthogonalArc:function(){return P.isOrthogonalArc(this._segment1,this._segment2)},getIntersections:function(t){return M.filterIntersections(M.getIntersections(this.getValues(),t.getValues(),this,t,[]))},_getParameter:function(e,n){return n?e:e&&e.curve===this?e.parameter:e===t&&n===t?.5:this.getParameterAt(e,0)},divide:function(t,e,n){var i=this._getParameter(t,e),r=1e-6,s=null;if(i>r&&1-r>i){var a=M.subdivide(this.getValues(),i),o=n?!1:this.isLinear(),u=a[0],l=a[1];o||(this._segment1._handleOut.set(u[2]-u[0],u[3]-u[1]),this._segment2._handleIn.set(l[4]-l[6],l[5]-l[7]));var c=u[6],d=u[7],f=new P(new h(c,d),!o&&new h(u[4]-c,u[5]-d),!o&&new h(l[2]-c,l[3]-d));if(this._path)this._segment1._index>0&&0===this._segment2._index?this._path.add(f):this._path.insert(this._segment2._index,f),s=this;else{var _=this._segment2;this._segment2=f,s=new M(f,_)}}return s},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},reverse:function(){return new M(this._segment2.reverse(),this._segment1.reverse())},remove:function(){var t=!1;if(this._path){var e=this._segment2,n=e._handleOut;t=e.remove(),t&&this._segment1._handleOut.set(n.x,n.y)}return t},clone:function(){return new M(this._segment1,this._segment2)},toString:function(){var t=["point1: "+this._segment1._point];return this._segment1._handleOut.isZero()||t.push("handle1: "+this._segment1._handleOut),this._segment2._handleIn.isZero()||t.push("handle2: "+this._segment2._handleIn),t.push("point2: "+this._segment2._point),"{ "+t.join(", ")+" }"},statics:{getValues:function(t,e,n){var i=t._point,r=t._handleOut,s=e._handleIn,a=e._point,o=[i._x,i._y,i._x+r._x,i._y+r._y,a._x+s._x,a._y+s._y,a._x,a._y];return n&&n._transformCoordinates(o,o,4),o},subdivide:function(e,n){var i=e[0],r=e[1],s=e[2],a=e[3],o=e[4],h=e[5],u=e[6],l=e[7];n===t&&(n=.5);var c=1-n,d=c*i+n*s,f=c*r+n*a,_=c*s+n*o,g=c*a+n*h,p=c*o+n*u,v=c*h+n*l,m=c*d+n*_,y=c*f+n*g,w=c*_+n*p,x=c*g+n*v,b=c*m+n*w,C=c*y+n*x;return[[i,r,d,f,m,y,b,C],[b,C,w,x,p,v,u,l]]},solveCubic:function(t,e,n,i,r,s){var o=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-o),d=3*(u-h)-c,f=l-o-c-d;return a.solveCubic(f,d,c,o-n,i,r,s)},getParameterOf:function(t,e,n){var i=1e-6;if(Math.abs(t[0]-e)l;)if(-1===h||(r=a[l++])>0&&1>r){for(var c=0;-1===u||u>c;)if((-1===u||(s=o[c++])>0&&1>s)&&(-1===h?r=s:-1===u&&(s=r),Math.abs(r-s)0&&(t=M.subdivide(t,e)[1]),1>n&&(t=M.subdivide(t,(n-e)/(1-e))[0]),t},hasHandles:function(t){var e=a.isZero;return!(e(t[0]-t[2])&&e(t[1]-t[3])&&e(t[4]-t[6])&&e(t[5]-t[7]))},isLinear:function(t){var e=t[0],n=t[1],i=t[6],r=t[7],s=new h(i-e,r-n);return s.isCollinear(new h(t[2]-e,t[3]-n))&&s.isCollinear(new h(t[4]-i,t[5]-r))},isFlatEnough:function(t,e){var n=t[0],i=t[1],r=t[2],s=t[3],a=t[4],o=t[5],h=t[6],u=t[7],l=3*r-2*n-h,c=3*s-2*i-u,d=3*a-2*h-n,f=3*o-2*u-i;return Math.max(l*l,d*d)+Math.max(c*c,f*f)<10*e*e},getArea:function(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7];return(3*r*e-1.5*r*s-1.5*r*o-3*n*i-1.5*n*s-.5*n*o+1.5*a*e+1.5*a*i-3*a*o+.5*h*e+1.5*h*i+3*h*s)/10},getEdgeSum:function(t){return(t[0]-t[2])*(t[3]+t[1])+(t[2]-t[4])*(t[5]+t[3])+(t[4]-t[6])*(t[7]+t[5])},getBounds:function(t){for(var e=t.slice(0,2),n=e.slice(),i=[0,0],r=0;2>r;r++)M._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,n,i);return new d(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,o,h,u){function l(t,e){var n=t-e,i=t+e;nh[r]&&(h[r]=i)}var c=3*(e-n)-t+i,d=2*(t+n)-4*e,f=e-t,_=a.solveQuadratic(c,d,f,u),g=1e-6,p=1-g;l(i,0);for(var v=0;_>v;v++){var m=u[v],y=1-m;m>g&&p>m&&l(y*y*y*t+3*y*y*m*e+3*y*m*m*n+m*m*m*i,s)}}}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){this._bounds||(this._bounds={});var e=this._bounds[t];return e||(e=this._bounds[t]=A[t]([this._segment1,this._segment2],!1,this._path.getStyle())),e.clone()}},{}),{beans:!1,getParameterAt:function(t,e){return M.getParameterAt(this.getValues(),t,e)},getParameterOf:function(){var t=h.read(arguments);return M.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){var n=e?t:this.getParameterAt(t);return null!=n&&n>=0&&1>=n?new I(this,n):null},getLocationOf:function(){return this.getLocationAt(this.getParameterOf(h.read(arguments)),!0)},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getNearestLocation:function(){function t(t){if(t>=0&&1>=t){var i=e.getDistance(M.getPoint(n,t),!0);if(r>i)return r=i,s=t,!0}}for(var e=h.read(arguments),n=this.getValues(),i=100,r=1/0,s=0,a=0;i>=a;a++)t(a/i);for(var o=1/(2*i);o>1e-6;)t(s-o)||t(s+o)||(o/=2);var u=M.getPoint(n,s);return new I(this,s,u,null,null,null,e.getDistance(u))},getNearestPoint:function(){return this.getNearestLocation.apply(this,arguments).getPoint()}},new function(){var t=["getPoint","getTangent","getNormal","getWeightedTangent","getWeightedNormal","getCurvature"];return e.each(t,function(t){this[t+"At"]=function(e,n){var i=this.getValues();return M[t](i,n?e:M.getParameterAt(i,e,0))}},{statics:{evaluateMethods:t}})},new function(){function e(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7],u=9*(i-s)+3*(o-e),l=6*(e+s)-12*i,c=3*(i-e),d=9*(r-a)+3*(h-n),f=6*(n+a)-12*r,_=3*(r-n);return function(t){var e=(u*t+l)*t+c,n=(d*t+f)*t+_;return Math.sqrt(e*e+n*n)}}function n(t,e){return Math.max(2,Math.min(16,Math.ceil(32*Math.abs(e-t))))}function i(t,e,n,i){if(null==e||0>e||e>1)return null;var r,s,a=t[0],o=t[1],u=t[2],l=t[3],c=t[4],d=t[5],f=t[6],_=t[7],g=1e-6;if(0===n&&(g>e||e>1-g)){var p=g>e;r=p?a:f,s=p?o:_}else{var v=3*(u-a),m=3*(c-u)-v,y=f-a-v-m,w=3*(l-o),x=3*(d-l)-w,b=_-o-w-x;if(0===n)r=((y*e+m)*e+v)*e+a,s=((b*e+x)*e+w)*e+o;else{if(g>e?(r=v,s=w):e>1-g?(r=3*(f-c),s=3*(_-d)):(r=(3*y*e+2*m)*e+v,s=(3*b*e+2*x)*e+w),i){0===r&&0===s&&(g>e||e>1-g)&&(r=c-u,s=d-l);var C=Math.sqrt(r*r+s*s);r/=C,s/=C}if(3===n){var S=6*y*e+2*m,P=6*b*e+2*x,k=Math.pow(r*r+s*s,1.5);r=0!==k?(r*P-s*S)/k:0,s=0}}}return 2===n?new h(s,-r):new h(r,s)}return{statics:!0,getLength:function(i,r,s){r===t&&(r=0),s===t&&(s=1);var o=a.isZero;if(0===r&&1===s&&o(i[0]-i[2])&&o(i[1]-i[3])&&o(i[6]-i[4])&&o(i[7]-i[5])){var h=i[6]-i[0],u=i[7]-i[1];return Math.sqrt(h*h+u*u)}var l=e(i);return a.integrate(l,r,s,n(r,s))},getParameterAt:function(i,r,s){function o(t){return p+=a.integrate(f,s,t,n(s,t)),s=t,p-r}if(s===t&&(s=0>r?1:0),0===r)return s;var h=1e-6,u=Math.abs,l=r>0,c=l?s:0,d=l?1:s,f=e(i),_=a.integrate(f,c,d,n(c,d));if(u(r-_)_)return null;var g=r/_,p=0;return a.findRoot(o,f,s+g,c,d,16,h)},getPoint:function(t,e){return i(t,e,0,!1)},getTangent:function(t,e){return i(t,e,1,!0)},getWeightedTangent:function(t,e){return i(t,e,1,!1)},getNormal:function(t,e){return i(t,e,2,!0)},getWeightedNormal:function(t,e){return i(t,e,2,!1)},getCurvature:function(t,e){return i(t,e,3,!1).x}}},new function(){function t(t,e,n,i,r,s,a,o){var h=new I(n,i,r,s,a,o);(!e||e(h))&&t.push(h)}function e(r,s,a,o,h,u,l,c,d,f,_,p,v){if(!(v>32)){var m,y,w,x=s[0],b=s[1],C=s[6],S=s[7],P=1e-6,k=g.getSignedDistance,I=k(x,b,C,S,s[2],s[3])||0,z=k(x,b,C,S,s[4],s[5])||0,A=I*z>0?.75:4/9,O=A*Math.min(0,I,z),T=A*Math.max(0,I,z),L=k(x,b,C,S,r[0],r[1]),E=k(x,b,C,S,r[2],r[3]),N=k(x,b,C,S,r[4],r[5]),j=k(x,b,C,S,r[6],r[7]);if(x===C&&P>f-d&&v>3)y=m=(c+l)/2,w=0;else{var B,D,R=n(L,E,N,j),F=R[0],q=R[1];if(B=i(F,q,O,T),F.reverse(),q.reverse(),D=i(F,q,O,T),null==B||null==D)return;r=M.getPart(r,B,D),w=D-B,m=c*B+l*(1-B),y=c*D+l*(1-D)}if(_>.5&&w>.5)if(y-m>f-d){var V=M.subdivide(r,.5),Z=m+(y-m)/2;e(s,V[0],o,a,h,u,d,f,m,Z,w,!p,++v),e(s,V[1],o,a,h,u,d,f,Z,y,w,!p,v)}else{var V=M.subdivide(s,.5),Z=d+(f-d)/2;e(V[0],r,o,a,h,u,d,Z,m,y,w,!p,++v),e(V[1],r,o,a,h,u,Z,f,m,y,w,!p,v)}else if(Math.max(f-d,y-m)0&&e(s,r,o,a,h,u,d,f,m,y,w,!p,++v)}}function n(t,e,n,i){var r,s=[0,t],a=[1/3,e],o=[2/3,n],h=[1,i],u=g.getSignedDistance,l=u(0,t,1,i,1/3,e),c=u(0,t,1,i,2/3,n),d=!1;if(0>l*c)r=[[s,a,h],[s,o,h]],d=0>l;else{var f,_=0,p=0===l||0===c;Math.abs(l)>Math.abs(c)?(f=a,_=(i-n-(i-t)/3)*(2*(i-n)-i+e)/3):(f=o,_=(e-t+(t-i)/3)*(-2*(t-e)+t-n)/3),r=0>_||p?[[s,f,h],[s,h]]:[[s,a,o,h],[s,h]],d=l?0>l:0>c}return d?r.reverse():r}function i(t,e,n,i){return t[0][1]i?r(e,!1,i):t[0][0]}function r(t,e,n){for(var i=t[0][0],r=t[0][1],s=1,a=t.length;a>s;s++){var o=t[s][0],h=t[s][1];if(e?h>=n:n>=h)return i+(n-r)*(o-i)/(h-r);i=o,r=h}return null}function s(e,n,i,r,s,a){for(var o=M.isLinear(e),h=o?n:e,u=o?e:n,l=u[0],c=u[1],d=u[6],f=u[7],_=d-l,g=f-c,p=Math.atan2(-g,_),v=Math.sin(p),m=Math.cos(p),y=_*m-g*v,w=[0,0,0,0,y,0,y,0],x=[],b=0;8>b;b+=2){var C=h[b]-l,S=h[b+1]-c;x.push(C*m-S*v,S*m+C*v)}for(var P=[],k=M.solveCubic(x,1,0,P,0,1),b=0;k>b;b++){var I=P[b],C=M.getPoint(x,I).x;if(C>=0&&y>=C){var z=M.getParameterOf(w,C,0),A=o?z:I,O=o?I:z;t(s,a,i,A,M.getPoint(e,A),r,O,M.getPoint(n,O))}}}function a(e,n,i,r,s,a){var o=g.intersect(e[0],e[1],e[6],e[7],n[0],n[1],n[6],n[7]);if(o){var h=o.x,u=o.y;t(s,a,i,M.getParameterOf(e,h,u),o,r,M.getParameterOf(n,h,u),o)}}return{statics:{getIntersections:function(n,i,r,o,h,u){var l=M.isLinear(n),c=M.isLinear(i),d=r.getPoint1(),f=r.getPoint2(),_=o.getPoint1(),g=o.getPoint2(),p=1e-6;return d.isClose(_,p)&&t(h,u,r,0,d,o,0,d),d.isClose(g,p)&&t(h,u,r,0,d,o,1,d),(l&&c?a:l||c?s:e)(n,i,r,o,h,u,0,1,0,1,0,!1,0),f.isClose(_,p)&&t(h,u,r,1,f,o,0,f),f.isClose(g,p)&&t(h,u,r,1,f,o,1,f),h},filterIntersections:function(t,e){function n(t,e){var n=t.getPath(),i=e.getPath();return n===i?t.getIndex()+t.getParameter()-(e.getIndex()+e.getParameter()):n._id-i._id}for(var i=t.length-1,r=1-1e-6,s=i;s>=0;s--){var a=t[s],o=a._curve.getNext(),h=a._curve2.getNext();o&&a._parameter>=r&&(a._parameter=0,a._curve=o),h&&a._parameter2>=r&&(a._parameter2=0,a._curve2=h)}if(i>0){t.sort(n);for(var s=i;s>0;s--)t[s].equals(t[s-1])&&(t.splice(s,1),i--)}if(e){for(var s=i;s>=0;s--)t.push(t[s].getIntersection());t.sort(n)}return t}}}}),I=e.extend({_class:"CurveLocation",beans:!0,initialize:function gt(t,e,n,i,r,s,a){this._id=o.get(gt);var h=t._path;this._version=h?h._version:0,this._curve=t,this._parameter=e,this._point=n||t.getPointAt(e,!0),this._curve2=i,this._parameter2=r,this._point2=s,this._distance=a,this._segment1=t._segment1,this._segment2=t._segment2},getSegment:function(t){if(!this._segment){var e=this.getCurve(),n=this.getParameter();if(1===n)this._segment=e._segment2;else if(0===n||t)this._segment=e._segment1;else{if(null==n)return null;this._segment=e.getPartLength(0,n)_;_++)c[_]=a[_].getValues(h);for(var _=0;u>_;_++){var p=s[_],v=e?p.getValues(o):c[_];if(!e){var m=p.getSegment1(),y=p.getSegment2(),w=m._handleOut,x=y._handleIn;if(new g(m._point.subtract(w),w.multiply(2),!0).intersect(new g(y._point.subtract(x),x.multiply(2),!0),!1)){var b=M.subdivide(v);M.getIntersections(b[0],b[1],p,p,r,function(e){return e._parameter<=f?(e._parameter/=2,e._parameter2=.5+e._parameter2/2,!0):t})}}for(var C=e?0:_+1;l>C;C++)M.getIntersections(v,c[C],p,a[C],r,!e&&(C===_+1||C===l-1&&0===_)&&function(t){var e=t._parameter;return e>=d&&f>=e})}return M.filterIntersections(r,i)},_asPathItem:function(){return this},setPathData:function(t){function e(t,e){var n=+i[t];return o&&(n+=u[e]),n}function n(t){return new h(e(t,"x"),e(t+1,"y"))}var i,r,s,a=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),o=!1,u=new h,c=new h;this.clear();for(var d=0,f=a&&a.length;f>d;d++){var _=a[d],g=_[0],p=g.toLowerCase();i=_.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var v=i&&i.length;switch(o=g===p,"z"!==r||/[mz]/.test(p)||this.moveTo(u=c),p){case"m":case"l":for(var m="m"===p,y=0;v>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=n(y));s=u,m&&(c=u);break;case"h":case"v":for(var w="h"===p?"x":"y",y=0;v>y;y++)u[w]=e(y,w),this.lineTo(u);s=u;break;case"c":for(var y=0;v>y;y+=6)this.cubicCurveTo(n(y),s=n(y+2),u=n(y+4));break;case"s":for(var y=0;v>y;y+=4)this.cubicCurveTo(/[cs]/.test(r)?u.multiply(2).subtract(s):u,s=n(y),u=n(y+2)),r=p;break;case"q":for(var y=0;v>y;y+=4)this.quadraticCurveTo(s=n(y),u=n(y+2));break;case"t":for(var y=0;v>y;y+=2)this.quadraticCurveTo(s=/[qt]/.test(r)?u.multiply(2).subtract(s):u,u=n(y)),r=p;break;case"a":for(var y=0;v>y;y+=7)this.arcTo(u=n(y+5),new l(+i[y],+i[y+1]),+i[y+2],+i[y+4],+i[y+3]);break;case"z":this.closePath(!0)}r=p}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t,!1,!0);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),A=z.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[],this._version=0;var n=Array.isArray(e)?"object"==typeof e[0]?e:arguments:!e||e.size!==t||e.x===t&&e.point===t?null:arguments;n&&n.length>0?this.setSegments(n):(this._curves=t,this._selectedSegmentState=0,n||"string"!=typeof e||(this.setPathData(e),e=null)),this._initialize(!n&&e)},_equals:function(t){return this._closed===t._closed&&e.equals(this._segments,t._segments)},clone:function(e){var n=new A(m.NO_INSERT);return n.setSegments(this._segments),n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),this._clone(n,e)},_changed:function pt(e){if(pt.base.call(this,e),8&e){var n=this._parent;if(n&&(n._currentPath=t),this._length=this._clockwise=t,16&e)this._version++;else if(this._curves)for(var i=0,r=this._curves.length;r>i;i++)this._curves[i]._changed();this._monoCurves=t}else 32&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof O?t:this)._style},getSegments:function(){return this._segments},setSegments:function(e){var n=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,this._curves=t,e&&e.length>0&&this._add(P.readAll(e)),n&&this.setFullySelected(!0)},getFirstSegment:function(){return this._segments[0]},getLastSegment:function(){return this._segments[this._segments.length-1]},getCurves:function(){var t=this._curves,e=this._segments;if(!t){var n=this._countCurves();t=this._curves=Array(n);for(var i=0;n>i;i++)t[i]=new M(this,e[i],e[i+1]||e[0])}return t},getFirstCurve:function(){return this.getCurves()[0]},getLastCurve:function(){var t=this.getCurves();return t[t.length-1]},isClosed:function(){return this._closed},setClosed:function(t){if(this._closed!=(t=!!t)){if(this._closed=t,this._curves){var e=this._curves.length=this._countCurves();t&&(this._curves[e-1]=new M(this,this._segments[e-1],this._segments[0]))}this._changed(25)}}},{beans:!0,getPathData:function(t,e){function n(e,n){e._transformCoordinates(t,g,!1),i=g[0],r=g[1],p?(v.push("M"+_.pair(i,r)),p=!1):(h=g[2],u=g[3],h===i&&u===r&&l===a&&c===o?n||v.push("l"+_.pair(i-a,r-o)):v.push("c"+_.pair(l-a,c-o)+" "+_.pair(h-a,u-o)+" "+_.pair(i-a,r-o))),a=i,o=r,l=g[4],c=g[5]}var i,r,a,o,h,u,l,c,d=this._segments,f=d.length,_=new s(e),g=Array(6),p=!0,v=[];if(0===f)return"";for(var m=0;f>m;m++)n(d[m]);return this._closed&&f>0&&(n(d[0],!0),v.push("z")),v.join("")}},{isEmpty:function(){return 0===this._segments.length},isLinear:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)if(!t[e].isLinear())return!1;return!0},hasHandles:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)if(t[e].hasHandles())return!0;return!1},_transformContent:function(t){for(var e=Array(6),n=0,i=this._segments.length;i>n;n++)this._segments[n]._transformCoordinates(t,e,!0);return!0},_add:function(t,e){for(var n=this._segments,i=this._curves,r=t.length,s=null==e,e=s?n.length:e,a=0;r>a;a++){var o=t[a];o._path&&(o=t[a]=o.clone()),o._path=this,o._index=e+a,o._selectionState&&this._updateSelection(o,0,o._selectionState)}if(s)n.push.apply(n,t);else{n.splice.apply(n,[e,0].concat(t));for(var a=e+r,h=n.length;h>a;a++)n[a]._index=a}if(i||t._curves){i||(i=this._curves=[]);var u=e>0?e-1:e,l=u,c=Math.min(u+r,this._countCurves());t._curves&&(i.splice.apply(i,[u,0].concat(t._curves)),l+=t._curves.length);for(var a=l;c>a;a++)i.splice(a,0,new M(this,null,null));this._adjustCurves(u,c)}return this._changed(25),t},_adjustCurves:function(t,e){for(var n,i=this._segments,r=this._curves,s=t;e>s;s++)n=r[s],n._path=this,n._segment1=i[s],n._segment2=i[s+1]||i[0],n._changed();(n=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0],n._changed()),(n=r[e])&&(n._segment1=i[e],n._changed())},_countCurves:function(){var t=this._segments.length;return!this._closed&&t>0?t-1:t},add:function(t){return arguments.length>1&&"number"!=typeof t?this._add(P.readAll(arguments)):this._add([P.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(P.readAll(arguments,1),t):this._add([P.read(arguments,1)],t)[0]},addSegment:function(){return this._add([P.read(arguments)])[0]},insertSegment:function(t){return this._add([P.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(P.readAll(t))},insertSegments:function(t,e){return this._add(P.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,n,i){t=t||0,n=e.pick(n,this._segments.length);var r=this._segments,s=this._curves,a=r.length,o=r.splice(t,n-t),h=o.length;if(!h)return o;for(var u=0;h>u;u++){var l=o[u];l._selectionState&&this._updateSelection(l,l._selectionState,0),l._index=l._path=null}for(var u=t,c=r.length;c>u;u++)r[u]._index=u;if(s){var d=t>0&&n===a+(this._closed?1:0)?t-1:t,s=s.splice(d,h);i&&(o._curves=s.slice(1)),this._adjustCurves(d,d)}return this._changed(25),o},clear:"#removeSegments",getLength:function(){if(null==this._length){var t=this.getCurves();this._length=0;for(var e=0,n=t.length;n>e;e++)this._length+=t[e].getLength()}return this._length},getArea:function(){for(var t=this.getCurves(),e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e},isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===7*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function vt(t){t||this._selectSegments(!1),vt.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?7*e:0;for(var n=0;e>n;n++)this._segments[n]._selectionState=t?7:0},_updateSelection:function(t,e,n){t._selectionState=n;var i=this._selectedSegmentState+=n-e;i>0&&this.setSelected(!0)},flatten:function(t){for(var e=new T(this,64,.1),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new P(e.getPointAt(n))),n+=i;this.setSegments(s)},reduce:function(){for(var t=this.getCurves(),e=t.length-1;e>=0;e--){var n=t[e];n.isLinear()&&0===n.getLength()&&n.remove()}return this},simplify:function(t){if(this._segments.length>2){var e=new L(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null===e)return null;if(1===arguments.length){var n=t;if("number"==typeof n&&(n=this.getLocationAt(n)),!n)return null;t=n.index,e=n.parameter}var i=1e-6;e>=1-i&&(t++,e--);var r=this.getCurves();if(t>=0&&ti&&r[t++].divide(e,!0);var s,a=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),s=this):s=this._clone((new A).insertAbove(this,!0)),s._add(a,0),this.addSegment(a[0]),s}return null},isClockwise:function(){return this._clockwise!==t?this._clockwise:A.isClockwise(this._segments)},setClockwise:function(t){this.isClockwise()!=(t=!!t)&&this.reverse(),this._clockwise=t},reverse:function(){this._segments.reverse();for(var e=0,n=this._segments.length;n>e;e++){var i=this._segments[e],r=i._handleIn;i._handleIn=i._handleOut,i._handleOut=r,i._index=e}this._curves=null,this._clockwise!==t&&(this._clockwise=!this._clockwise),this._changed(9)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();if(!i)return this;n&&n._point.equals(i._point)&&t.reverse();var r=t.getFirstSegment();if(n&&n._point.equals(r._point))n.setHandleOut(r._handleOut),this._add(e.slice(1));else{var s=this.getFirstSegment();s&&s._point.equals(r._point)&&t.reverse(),i=t.getLastSegment(),s&&s._point.equals(i._point)?(s.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())}t.closed&&this._add([e[0]]),t.remove()}var a=this.getFirstSegment(),o=this.getLastSegment();return a!==o&&a._point.equals(o._point)&&(a.setHandleIn(o._handleIn),o.remove(),this.setClosed(!0)),this},toShape:function(t){function e(t,e){return c[t].isCollinear(c[e])}function n(t){return c[t].isOrthogonal()}function i(t){return c[t].isOrthogonalArc()}function r(t,e){return c[t]._point.getDistance(c[e]._point)}if(!this._closed)return null;var s,o,h,u,c=this._segments;if(!this.hasHandles()&&4===c.length&&e(0,2)&&e(1,3)&&n(1)?(s=x.Rectangle,o=new l(r(0,3),r(0,1)),u=c[1]._point.add(c[2]._point).divide(2)):8===c.length&&i(0)&&i(2)&&i(4)&&i(6)&&e(1,5)&&e(3,7)?(s=x.Rectangle,o=new l(r(1,6),r(0,3)),h=o.subtract(new l(r(0,7),r(1,2))).divide(2),u=c[3]._point.add(c[4]._point).divide(2)):4===c.length&&i(0)&&i(1)&&i(2)&&i(3)&&(a.isZero(r(0,2)-r(1,3))?(s=x.Circle,h=r(0,2)/2):(s=x.Ellipse,h=new l(r(2,0)/2,r(3,1)/2)),u=c[1]._point),s){var d=this.getPosition(!0),f=this._clone(new s({center:d,size:o,radius:h,insert:!1}),t,!1);return f.rotate(u.subtract(d).getAngle()+90),f}return null},_hitTestSelf:function(t,e){function n(e,n){return t.subtract(e).divide(n).length<=1}function i(t,i,r){if(!e.selected||i.isSelected()){var s=t._point;if(i!==s&&(i=i.add(s)),n(i,w))return new S(r,_,{segment:t,point:i})}}function r(t,n){return(n||e.segments)&&i(t,t._point,"segment")||!n&&e.handles&&(i(t,t._handleIn,"handle-in")||i(t,t._handleOut,"handle-out"))}function s(t){c.add(t)}function a(e){if(("round"!==o||"round"!==u)&&(c=new A({internal:!0,closed:!0}),m||e._index>0&&e._index0||C?0:null;if(null!==P&&(P>0?(o=g.getStrokeJoin(),u=g.getStrokeCap(),l=P*g.getMiterLimit(),w=y.add(new h(P,P))):o=u="round"),!e.ends||e.segments||m){if(e.segments||e.handles)for(var k=0;v>k;k++)if(f=r(p[k]))return f}else if(f=r(p[0],!0)||r(p[v-1],!0))return f;if(null!==P){if(d=this.getNearestLocation(t)){var M=d.getParameter();0===M||1===M&&v>1?a(d.getSegment())||(d=null):n(d.getPoint(),w)||(d=null)}if(!d&&"miter"===o&&v>1)for(var k=0;v>k;k++){var I=p[k];if(t.getDistance(I._point)<=l&&a(I)){d=I.getLocation();break}}}return!d&&b&&this._contains(t)||d&&!x&&!C?new S("fill",this):d?new S(x?"stroke":"curve",this,{location:d,point:d.getPoint()}):null}},e.each(M.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getLocationAt(e,n);return i&&i[t]()}},{beans:!1,_getOffset:function(t){var e=t&&t.getIndex();if(null!=e){for(var n=this.getCurves(),i=0,r=0;e>r;r++)i+=n[r].getLength();var s=n[e],a=t.getParameter();return a>0&&(i+=s.getPartLength(0,a)),i}return null},getLocationOf:function(){for(var t=h.read(arguments),e=this.getCurves(),n=0,i=e.length;i>n;n++){var r=e[n].getLocationOf(t);if(r)return r}return null},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t,s=n[r];return s?s.getLocationAt(t-r,!0):null}for(var a=0,o=n.length;o>a;a++){var h=i,s=n[a];if(i+=s.getLength(),i>t)return s.getLocationAt(t-h)}return t<=this.getLength()?new I(n[n.length-1],1):null},getNearestLocation:function(){for(var t=h.read(arguments),e=this.getCurves(),n=1/0,i=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceo;o++){var u=e[o];u._transformCoordinates(n,a,!1);var l=u._selectionState,c=a[0],d=a[1];if(1&l&&r(2),2&l&&r(4),t.fillRect(c-s,d-s,i,i),!(4&l)){var f=t.fillStyle;t.fillStyle="#ffffff",t.fillRect(c-s+1,d-s+1,i-2,i-2),t.fillStyle=f}}}function e(t,e,n){function i(e){if(n)e._transformCoordinates(n,_,!1),r=_[0],s=_[1];else{var i=e._point;r=i._x,s=i._y}if(g)t.moveTo(r,s),g=!1;else{if(n)h=_[2],u=_[3];else{var d=e._handleIn;h=r+d._x,u=s+d._y}h===r&&u===s&&l===a&&c===o?t.lineTo(r,s):t.bezierCurveTo(l,c,h,u,r,s)}if(a=r,o=s,n)l=_[4],c=_[5];else{var d=e._handleOut;l=a+d._x,c=o+d._y}}for(var r,s,a,o,h,u,l,c,d=e._segments,f=d.length,_=Array(6),g=!0,p=0;f>p;p++)i(d[p]);e._closed&&f>0&&i(d[0])}return{_draw:function(t,n,i){function r(t){return l[(t%c+c)%c]}var s=n.dontStart,a=n.dontFinish||n.clip,o=this.getStyle(),h=o.hasFill(),u=o.hasStroke(),l=o.getDashArray(),c=!paper.support.nativeDash&&u&&l&&l.length;if(s||t.beginPath(),!s&&this._currentPath?t.currentPath=this._currentPath:(h||u&&!c||a)&&(e(t,this,i),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!a&&(h||u)&&(this._setStyles(t),h&&(t.fill(o.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),u)){if(c){s||t.beginPath();var d,f=new T(this,32,.25,i),_=f.length,g=-o.getDashOffset(),p=0;for(g%=_;g>0;)g-=r(p--)+r(p--);for(;_>g;)d=g+r(p++),(g>0||d>0)&&f.drawPart(t,Math.max(g,0),Math.max(d,0)),g=d+r(p++)}t.stroke()}},_drawSelected:function(n,i){n.beginPath(),e(n,this,i),n.stroke(),t(n,this._segments,i,paper.settings.handleSize)}}},new function(){function t(t){var e=t.length,n=[],i=[],r=2;n[0]=t[0]/r;for(var s=1;e>s;s++)i[s]=1/r,r=(e-1>s?4:2)-i[s],n[s]=(t[s]-n[s-1])/r;for(var s=1;e>s;s++)n[e-s-1]-=i[e-s]*n[e-s];return n}return{smooth:function(){var e=this._segments,n=e.length,i=this._closed,r=n,s=0;if(!(2>=n)){i&&(s=Math.min(n,4),r+=2*Math.min(n,s));for(var a=[],o=0;n>o;o++)a[o+s]=e[o]._point;if(i)for(var o=0;s>o;o++)a[o]=e[o+n-s]._point,a[o+n+s]=e[o]._point;else r--;for(var u=[],o=1;r-1>o;o++)u[o]=4*a[o]._x+2*a[o+1]._x;u[0]=a[0]._x+2*a[1]._x,u[r-1]=3*a[r-1]._x;for(var l=t(u),o=1;r-1>o;o++)u[o]=4*a[o]._y+2*a[o+1]._y;u[0]=a[0]._y+2*a[1]._y,u[r-1]=3*a[r-1]._y;var c=t(u);if(i){for(var o=0,d=n;s>o;o++,d++){var f=o/s,_=1-f,g=o+s,p=d+s;l[d]=l[o]*f+l[d]*_,c[d]=c[o]*f+c[d]*_,l[p]=l[g]*_+l[p]*f,c[p]=c[g]*_+c[p]*f}r--}for(var v=null,o=s;r-s>=o;o++){var m=e[o-s];v&&m.setHandleIn(v.subtract(m._point)),r>o&&(m.setHandleOut(new h(l[o],c[o]).subtract(m._point)),v=r-1>o?new h(2*a[o+1]._x-l[o+1],2*a[o+1]._y-c[o+1]):new h((a[r]._x+l[r-1])/2,(a[r]._y+c[r-1])/2))}if(i&&v){var m=this._segments[0];m.setHandleIn(v.subtract(m._point))}}}}},new function(){function t(t){var e=t._segments;if(0===e.length)throw Error("Use a moveTo() command first");return e[e.length-1]}return{moveTo:function(){var t=this._segments;1===t.length&&this.removeSegment(0),t.length||this._add([new P(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new P(h.read(arguments))])},cubicCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this);r.setHandleOut(e.subtract(r._point)),this._add([new P(i,n.subtract(i))])},quadraticCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.cubicCurveTo(e.add(i.subtract(e).multiply(1/3)),e.add(n.subtract(e).multiply(1/3)),n)},curveTo:function(){var n=h.read(arguments),i=h.read(arguments),r=e.pick(e.read(arguments),.5),s=1-r,a=t(this)._point,o=n.subtract(a.multiply(s*s)).subtract(i.multiply(r*r)).divide(2*r*s);if(o.isNaN())throw Error("Cannot put a curve through points with parameter = "+r);this.quadraticCurveTo(o,i)},arcTo:function(){var n,i,r,s,a,o=t(this),u=o._point,c=h.read(arguments),d=e.peek(arguments),f=e.pick(d,!0);if("boolean"==typeof f)var p=u.add(c).divide(2),n=p.add(p.subtract(u).rotate(f?-90:90));else if(e.remain(arguments)<=2)n=c,c=h.read(arguments);else{var v=l.read(arguments);if(v.isZero())return this.lineTo(c);var m=e.read(arguments),f=!!e.read(arguments),y=!!e.read(arguments),p=u.add(c).divide(2),w=u.subtract(p).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=1e-12,k=C(v.width),M=C(v.height),I=k*k,z=M*M,A=x*x,O=b*b,T=Math.sqrt(A/I+O/z);if(T>1&&(k*=T,M*=T,I=k*k,z=M*M),T=(I*z-I*O-z*A)/(I*O+z*A),C(T)T)throw Error("Cannot create an arc with the given arguments");i=new h(k*b/M,-M*x/k).multiply((y===f?-1:1)*Math.sqrt(T)).rotate(m).add(p),a=(new _).translate(i).rotate(m).scale(k,M),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(c)),!f&&r>0?r-=360:f&&0>r&&(r+=360)}if(n){var L=new g(u.add(n).divide(2),n.subtract(u).rotate(90),!0),E=new g(n.add(c).divide(2),c.subtract(n).rotate(90),!0),N=new g(u,c),j=N.getSide(n);if(i=L.intersect(E,!0),!i){if(!j)return this.lineTo(c);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(i),r=s.getDirectedAngle(c.subtract(i));var B=N.getSide(i);0===B?r=j*Math.abs(r):j===B&&(r+=0>r?360:-360)}for(var D=Math.abs(r),R=D>=360?4:Math.ceil(D/90),F=r/R,q=F*Math.PI/360,V=4/3*Math.sin(q)/(1+Math.cos(q)),Z=[],H=0;R>=H;H++){var w=c,U=null;if(R>H&&(U=s.rotate(90).multiply(V),a?(w=a._transformPoint(s),U=a._transformPoint(s.add(U)).subtract(w)):w=i.add(s)),0===H)o.setHandleOut(U);else{var W=s.rotate(-90).multiply(V);a&&(W=a._transformPoint(s.add(W)).subtract(w)),Z.push(new P(w,W,U))}s=s.rotate(F)}this._add(Z)},lineBy:function(){var e=h.read(arguments),n=t(this)._point;this.lineTo(n.add(e))},curveBy:function(){var n=h.read(arguments),i=h.read(arguments),r=e.read(arguments),s=t(this)._point;this.curveTo(s.add(n),s.add(i),r)},cubicCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this)._point;this.cubicCurveTo(r.add(e),r.add(n),r.add(i))},quadraticCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.quadraticCurveTo(i.add(e),i.add(n))},arcBy:function(){var n=t(this)._point,i=n.add(h.read(arguments)),r=e.pick(e.peek(arguments),!0);"boolean"==typeof r?this.arcTo(i,r):this.arcTo(i,n.add(h.read(arguments)))},closePath:function(t){this.setClosed(!0),t&&this.join()}}},{_getBounds:function(t,e){return A[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,n=0,i=t.length;i>n;n++)e+=M.getEdgeSum(M.getValues(t[n],t[i>n+1?n+1:0]));return e>0},getBounds:function(t,e,n,i,r){function s(t){t._transformCoordinates(i,o,!1);for(var e=0;2>e;e++)M._addBounds(h[e],h[e+4],o[e+2],o[e],e,r?r[e]:0,u,l,c);var n=h;h=o,o=n}var a=t[0];if(!a)return new d;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),f=1,_=t.length;_>f;f++)s(t[f]);return e&&s(a),new d(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(t){f=f.include(i?i._transformPoint(t,t):t)}function s(t){f=f.unite(v.setCenter(i?i._transformPoint(t._point):t._point))}function a(t,e){var n=t._handleIn,i=t._handleOut;"round"===e||!n.isZero()&&!i.isZero()&&n.isCollinear(i)?s(t):A._addBevelJoin(t,e,u,p,r)}function o(t,e){"round"===e?s(t):A._addSquareCap(t,e,u,r)}if(!n.hasStroke())return A.getBounds(t,e,n,i);for(var h=t.length-(e?0:1),u=n.getStrokeWidth()/2,c=A._getPenPadding(u,i),f=A.getBounds(t,e,n,i,c),_=n.getStrokeJoin(),g=n.getStrokeCap(),p=u*n.getMiterLimit(),v=new d(new l(c).multiply(2)),m=1;h>m;m++)a(t[m],_);return e?a(t[0],_):h>0&&(o(t[0],g),o(t[t.length-1],g)),f},_getPenPadding:function(t,e){if(!e)return[t,t];var n=e.shiftless(),i=n.transform(new h(t,0)),r=n.transform(new h(0,t)),s=i.getAngleInRadians(),a=i.getLength(),o=r.getLength(),u=Math.sin(s),l=Math.cos(s),c=Math.tan(s),d=-Math.atan(o*c/a),f=Math.atan(o/(c*a));return[Math.abs(a*Math.cos(d)*l-o*Math.sin(d)*u),Math.abs(o*Math.sin(f)*l+a*Math.cos(f)*u)]},_addBevelJoin:function(t,e,n,i,r,s){var a=t.getCurve(),o=a.getPrevious(),u=a.getPointAt(0,!0),l=o.getNormalAt(1,!0),c=a.getNormalAt(0,!0),d=l.getDirectedAngle(c)<0?-n:n;if(l.setLength(d),c.setLength(d),s&&(r(u),r(u.add(l))),"miter"===e){var f=new g(u.add(l),new h(-l.y,l.x),!0).intersect(new g(u.add(c),new h(-c.y,c.x),!0),!0);if(f&&u.getDistance(f)<=i&&(r(f),!s))return}s||r(u.add(l)),r(u.add(c))},_addSquareCap:function(t,e,n,i,r){var s=t._point,a=t.getLocation(),o=a.getNormal().multiply(n);r&&(i(s.subtract(o)),i(s.add(o))),"square"===e&&(s=s.add(o.rotate(0===a.getParameter()?-90:90))),i(s.add(o)),i(s.subtract(o))},getHandleBounds:function(t,e,n,i,r,s){for(var a=Array(6),o=1/0,h=-o,u=o,l=h,c=0,f=t.length;f>c;c++){var _=t[c];_._transformCoordinates(i,a,!1);for(var g=0;6>g;g+=2){var p=0===g?s:r,v=p?p[0]:0,m=p?p[1]:0,y=a[g],w=a[g+1],x=y-v,b=y+v,C=w-m,S=w+m;o>x&&(o=x),b>h&&(h=b),u>C&&(u=C),S>l&&(l=S)}}return new d(o,u,h-o,l-u)},getRoughBounds:function(t,e,n,i){var r=n.hasStroke()?n.getStrokeWidth()/2:0,s=r;return r>0&&("miter"===n.getStrokeJoin()&&(s=r*n.getMiterLimit()),"square"===n.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),A.getHandleBounds(t,e,n,i,A._getPenPadding(r,i),A._getPenPadding(s,i))}}});A.inject({statics:new function(){function t(t,n,i){var r=e.getNamed(i),s=new A(r&&r.insert===!1&&m.NO_INSERT);return s._add(t),s._closed=n,s.set(r)}function n(e,n,i){for(var s=Array(4),a=0;4>a;a++){var o=r[a];s[a]=new P(o._point.multiply(n).add(e),o._handleIn.multiply(n),o._handleOut.multiply(n))}return t(s,!0,i)}var i=.5522847498307936,r=[new P([-1,0],[0,i],[0,-i]),new P([0,-1],[-i,0],[i,0]),new P([1,0],[0,-i],[0,i]),new P([0,1],[i,0],[-i,0])];return{Line:function(){return t([new P(h.readNamed(arguments,"from")),new P(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return n(t,new l(i),arguments)},Rectangle:function(){var e,n=d.readNamed(arguments,"rectangle"),r=l.readNamed(arguments,"radius",0,{readNull:!0}),s=n.getBottomLeft(!0),a=n.getTopLeft(!0),o=n.getTopRight(!0),h=n.getBottomRight(!0);if(!r||r.isZero())e=[new P(s),new P(a),new P(o),new P(h)];else{r=l.min(r,n.getSize(!0).divide(2));var u=r.width,c=r.height,f=u*i,_=c*i;e=[new P(s.add(u,0),null,[-f,0]),new P(s.subtract(0,c),[0,_]),new P(a.add(0,c),null,[0,-_]),new P(a.add(u,0),[-f,0],null),new P(o.subtract(u,0),null,[f,0]),new P(o.add(0,c),[0,-_],null),new P(h.subtract(0,c),null,[0,_]),new P(h.subtract(u,0),[f,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=x._readEllipse(arguments);return n(t.center,t.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),n=h.readNamed(arguments,"through"),i=h.readNamed(arguments,"to"),r=e.getNamed(arguments),s=new A(r&&r.insert===!1&&m.NO_INSERT);return s.moveTo(t),s.arcTo(n,i),s.set(r)},RegularPolygon:function(){for(var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"sides"),r=e.readNamed(arguments,"radius"),s=360/i,a=!(i%3),o=new h(0,a?-r:r),u=a?-1:.5,l=Array(i),c=0;i>c;c++)l[c]=new P(n.add(o.rotate((c+u)*s)));return t(l,!0,arguments)},Star:function(){for(var n=h.readNamed(arguments,"center"),i=2*e.readNamed(arguments,"points"),r=e.readNamed(arguments,"radius1"),s=e.readNamed(arguments,"radius2"),a=360/i,o=new h(0,-1),u=Array(i),l=0;i>l;l++)u[l]=new P(n.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var O=z.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||("string"==typeof t?this.setPathData(t):this.addChildren(Array.isArray(t)?t:arguments))},insertChildren:function mt(e,n,i){n=mt.base.call(this,e,n,i,A);for(var r=0,s=!i&&n&&n.length;s>r;r++){var a=n[r];a._clockwise===t&&a.setClockwise(0===a._index)}return n},reverse:function(){for(var t=this._children,e=0,n=t.length;n>e;e++)t[e].reverse()},smooth:function(){for(var t=0,e=this._children.length;e>t;t++)this._children[t].smooth()},reduce:function yt(){if(0===this._children.length){var t=new A(m.NO_INSERT);return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return yt.base.call(this)},isClockwise:function(){var t=this.getFirstChild();return t&&t.isClockwise()},setClockwise:function(t){this.isClockwise()!==!!t&&this.reverse()},getFirstSegment:function(){var t=this.getFirstChild();return t&&t.getFirstSegment()},getLastSegment:function(){var t=this.getLastChild();return t&&t.getLastSegment()},getCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n].getCurves());return e},getFirstCurve:function(){var t=this.getFirstChild();return t&&t.getFirstCurve()},getLastCurve:function(){var t=this.getLastChild();return t&&t.getFirstCurve()},getArea:function(){for(var t=this._children,e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e}},{beans:!0,getPathData:function(t,e){for(var n=this._children,i=[],r=0,s=n.length;s>r;r++){var a=n[r],o=a._matrix;i.push(a.getPathData(t&&!o.isIdentity()?t.chain(o):o,e))}return i.join(" ")}},{_getChildHitTestOptions:function(t){return t["class"]===A||"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e,n){var i=this._children;if(0!==i.length){if(this._currentPath)t.currentPath=this._currentPath;else{e=e.extend({dontStart:!0,dontFinish:!0}),t.beginPath();for(var r=0,s=i.length;s>r;r++)i[r].draw(t,e,n);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var a=this._style;a.hasFill()&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),a.hasStroke()&&t.stroke()}}},_drawSelected:function(t,e,n){for(var i=this._children,r=0,s=i.length;s>r;r++){var a=i[r],o=a._matrix;n[a._id]||a._drawSelected(t,o.isIdentity()?e:e.chain(o))}}},new function(){function t(t,e){var n=t._children;if(e&&0===n.length)throw Error("Use a moveTo() command first");return n[n.length-1]}var n={moveTo:function(){var e=t(this),n=e&&e.isEmpty()?e:new A;n!==e&&this.addChild(n),n.moveTo.apply(n,arguments)},moveBy:function(){var e=t(this,!0),n=e&&e.getLastSegment(),i=h.read(arguments);this.moveTo(n?i.add(n._point):i)},closePath:function(e){t(this,!0).closePath(e)}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){n[e]=function(){var n=t(this,!0);n[e].apply(n,arguments)}}),n});z.inject(new function(){function t(t,s,a){function o(t){return t.clone(!1).reduce().reorient().transform(null,!0,!0)}function h(t){for(var e=0,n=t.length;n>e;e++){var i=t[e];f.push.apply(f,i._segments),_.push.apply(_,i._getMonoCurves())}}var u=r[a],l=o(t),c=s&&t!==s&&o(s);c&&/^(subtract|exclude)$/.test(a)^c.isClockwise()!==l.isClockwise()&&c.reverse(),e(l.getIntersections(c,null,!0));var d=[],f=[],_=[],g=1e-6;h(l._children||[l]),c&&h(c._children||[c]),f.sort(function(t,e){var n=t._intersection,i=e._intersection;return!n&&!i||n&&i?0:n?-1:1});for(var p=0,v=f.length;v>p;p++){var y=f[p];if(null==y._winding){d.length=0;var w=y,x=0,b=0;do{var C=y.getCurve().getLength();d.push({segment:y,length:C}),x+=C,y=y.getNext()}while(y&&!y._intersection&&y!==w);for(var S=0;3>S;S++)for(var C=x*(S+1)/4,P=0,k=d.length;k>P;P++){var M=d[P],I=M.length;if(I>=C){(g>C||g>I-C)&&(C=I/2);var z=M.segment.getCurve(),A=z.getPointAt(C),T=z.isLinear()&&Math.abs(z.getTangentAt(.5,!0).y)=0;S--)d[S].segment._winding=E}}var N=new O(m.NO_INSERT);return N.insertAbove(t),N.addChildren(i(f,u),!0),N=N.reduce(),N.setStyle(t._style),N}function e(t){function e(){for(var t=0,e=n.length;e>t;t++)n[t].set(0,0)}for(var n,i,r,s=1e-6,a=1-s,o=t.length-1;o>=0;o--){var h=t[o],u=h._parameter;r&&r._curve===h._curve&&r._parameter>0?u/=r._parameter:(i=h._curve,n&&e(),n=i.isLinear()?[i._segment1._handleOut,i._segment2._handleIn]:null);var l,c;(l=i.divide(u,!0,!0))?(c=l._segment1,i=l.getPrevious(),n&&n.push(c._handleOut,c._handleIn)):c=s>u?i._segment1:u>a?i._segment2:i.getPartLength(0,u)w;w++){var b=e[w].values;if(M.solveCubic(b,0,l,_,0,1)>0)for(var C=_.length-1;C>=0;C--){var S=M.getPoint(b,_[C]).y;m>S&&S>p?p=S:S>y&&v>S&&(v=S)}}p=(p+c)/2,v=(v+c)/2,p>-(1/0)&&(d=n(new h(l,p),e)),1/0>v&&(f=n(new h(l,v),e))}else for(var P,k,I=l-s,z=l+s,A=!1,w=0,x=e.length;x>w;w++){var O=e[w],b=O.values,T=O.winding;if(T&&(1===T&&c>=b[1]&&c<=b[7]||c>=b[7]&&c<=b[1])&&1===M.solveCubic(b,1,c,_,0,1)){var L=_[0];if(!(L>u&&A&&O.next!==e[w+1]||o>L&&k>u&&O.previous===P)){var E=M.getPoint(b,L).x,N=M.getTangent(b,L).y,j=!1;a.isZero(N)&&!M.isLinear(b)||o>L&&N*M.getTangent(O.previous.values,1).y<0||L>u&&N*M.getTangent(O.next.values,0).y<0?r&&E>=I&&z>=E&&(++d,++f,j=!0):I>=E?(d+=T,j=!0):E>=z&&(f+=T,j=!0),O.previous!==e[w-1]&&(A=o>L&&j)}P=O,k=L}}return Math.max(g(d),g(f))}function i(t,e,n){for(var i,r,s=[],a=1e-6,o=1-a,h=0,u=t.length;u>h;h++)if(i=r=t[h],!i._visited&&e(i._winding)){var l=new A(m.NO_INSERT),c=i._intersection,d=c&&c._segment,f=!1,_=1;do{var g,p=_>0?i._handleIn:i._handleOut,v=_>0?i._handleOut:i._handleIn;if(f&&(!e(i._winding)||n)&&(c=i._intersection)&&(g=c._segment)&&g!==r){if(n)i._visited=g._visited,i=g,_=1;else{var y=i.getCurve();_>0&&(y=y.getPrevious());var w=y.getTangentAt(1>_?a:o,!0),x=g.getCurve(),b=x.getPrevious(),C=b.getTangentAt(o,!0),S=x.getTangentAt(a,!0),k=w.cross(C),M=w.cross(S);if(k*M!==0){var I=M>k?b:x,z=e(I._segment1._winding)?I:M>k?x:b,O=z._segment1;_=z===b?-1:1,O._visited&&i._path!==O._path||!e(O._winding)?_=1:(i._visited=g._visited,i=g,O._visited&&(_=1))}else _=1}v=_>0?i._handleOut:i._handleIn}l.add(new P(i._point,f&&p,v)),f=!0,i._visited=!0,i=_>0?i.getNext():i.getPrevious()}while(i&&!i._visited&&i!==r&&i!==d&&(i._intersection||e(i._winding)));!i||i!==r&&i!==d?l.lastSegment._handleOut.set(0,0):(l.firstSegment.setHandleIn((i===d?d:i)._handleIn),l.setClosed(!0)),l._segments.length>(l._closed?l.isLinear()?2:0:1)&&s.push(l)}return s}var r={unite:function(t){return 1===t||0===t},intersect:function(t){return 2===t},subtract:function(t){return 1===t},exclude:function(t){ -return 1===t}};return{_getWinding:function(t,e,i){return n(t,this._getMonoCurves(),e,i)},unite:function(e){return t(this,e,"unite")},intersect:function(e){return t(this,e,"intersect")},subtract:function(e){return t(this,e,"subtract")},exclude:function(e){return t(this,e,"exclude")},divide:function(t){return new y([this.subtract(t),this.intersect(t)])}}}),A.inject({_getMonoCurves:function(){function t(t){var e=t[1],r=t[7],s={values:t,winding:e===r?0:e>r?-1:1,previous:n,next:null};n&&(n.next=s),i.push(s),n=s}function e(e){if(0!==M.getLength(e)){var n=e[1],i=e[3],r=e[5],s=e[7];if(M.isLinear(e))t(e);else{var o=3*(i-r)-n+s,h=2*(n+r)-4*i,u=i-n,l=1e-6,c=[],d=a.solveQuadratic(o,h,u,c,l,1-l);if(0===d)t(e);else{c.sort();var f=c[0],_=M.subdivide(e,f);t(_[0]),d>1&&(f=(c[1]-f)/(1-f),_=M.subdivide(_[1],f),t(_[0])),t(_[1])}}}}var n,i=this._monoCurves;if(!i){i=this._monoCurves=[];for(var r=this.getCurves(),s=this._segments,o=0,h=r.length;h>o;o++)e(r[o].getValues());if(!this._closed&&s.length>1){var u=s[s.length-1]._point,l=s[0]._point,c=u._x,d=u._y,f=l._x,_=l._y;e([c,d,c,d,f,_,f,_])}if(i.length>0){var g=i[0],p=i[i.length-1];g.previous=p,p.next=g}}return i},getInteriorPoint:function(){var t=this.getBounds(),e=t.getCenter(!0);if(!this.contains(e)){for(var n=this._getMonoCurves(),i=[],r=e.y,s=[],a=0,o=n.length;o>a;a++){var h=n[a].values;if((1===n[a].winding&&r>=h[1]&&r<=h[7]||r>=h[7]&&r<=h[1])&&M.solveCubic(h,1,r,i,0,1)>0)for(var u=i.length-1;u>=0;u--)s.push(M.getPoint(h,i[u]).x);if(s.length>1)break}e.x=(s[0]+s[1])/2}return e},reorient:function(){return this.setClockwise(!0),this}}),O.inject({_getMonoCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n]._getMonoCurves());return e},reorient:function(){var t=this.removeChildren().sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()});if(t.length>0){this.addChildren(t);for(var e=t[0].isClockwise(),n=1,i=t.length;i>n;n++){for(var r=t[n].getInteriorPoint(),s=0,a=n-1;a>=0;a--)t[a].contains(r)&&s++;t[n].setClockwise(s%2===0&&e)}}return this}});var T=e.extend({_class:"PathIterator",initialize:function(t,e,n,i){function r(t,e){var n=M.getValues(t,e,i);o.push(n),s(n,t._index,0,1)}function s(t,e,i,r){if(r-i>l&&!M.isFlatEnough(t,n||.25)){var a=M.subdivide(t),o=(i+r)/2;s(a[0],e,i,o),s(a[1],e,o,r)}else{var c=t[6]-t[0],d=t[7]-t[1],f=Math.sqrt(c*c+d*d);f>1e-6&&(u+=f,h.push({offset:u,value:r,index:e}))}}for(var a,o=[],h=[],u=0,l=1/(e||32),c=t._segments,d=c[0],f=1,_=c.length;_>f;f++)a=c[f],r(d,a),d=a;t._closed&&r(a,c[0]),this.curves=o,this.parts=h,this.length=u,this.index=0},getParameterAt:function(t){for(var e,n=this.index;e=n,!(0==n||this.parts[--n].offsete;e++){var r=this.parts[e];if(r.offset>=t){this.index=e;var s=this.parts[e-1],a=s&&s.index==r.index?s.value:0,o=s?s.offset:0;return{value:a+(r.value-a)*(t-o)/(r.offset-o),index:r.index}}}var r=this.parts[this.parts.length-1];return{value:1,index:r.index}},drawPart:function(t,e,n){e=this.getParameterAt(e),n=this.getParameterAt(n);for(var i=e.index;i<=n.index;i++){var r=M.getPart(this.curves[i],i==e.index?e.value:0,i==n.index?n.value:1);i==e.index&&t.moveTo(r[0],r[1]),t.bezierCurveTo.apply(t,r.slice(2))}}},e.each(M.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getParameterAt(e);return M[t](this.curves[i.index],i.value,n)}},{})),L=e.extend({initialize:function(t,e){for(var n,i=this.points=[],r=t._segments,s=0,a=r.length;a>s;s++){var o=r[s].point.clone();n&&n.equals(o)||(i.push(o),n=o)}t._closed&&(this.closed=!0,i.unshift(i[i.length-1]),i.push(i[1])),this.error=e},fit:function(){var t=this.points,e=t.length,n=this.segments=e>0?[new P(t[0])]:[];return e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.closed&&(n.shift(),n.pop()),n},fitCubic:function(e,n,i,r){if(n-e==1){var s=this.points[e],a=this.points[n],o=s.getDistance(a)/3;return this.addCurve([s,s.add(i.normalize(o)),a.add(r.normalize(o)),a]),t}for(var h,u=this.chordLengthParameterize(e,n),l=Math.max(this.error,this.error*this.error),c=!0,d=0;4>=d;d++){var f=this.generateBezier(e,n,u,i,r),_=this.findMaxError(e,n,f,u);if(_.error=l)break;c=this.reparameterize(e,n,u,f),l=_.error}var g=this.points[h-1].subtract(this.points[h]),p=this.points[h].subtract(this.points[h+1]),v=g.add(p).divide(2).normalize();this.fitCubic(e,h,i,v),this.fitCubic(h,n,v.negate(),r)},addCurve:function(t){var e=this.segments[this.segments.length-1];e.setHandleOut(t[1].subtract(t[0])),this.segments.push(new P(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-12,a=this.points[t],o=this.points[e],h=[[0,0],[0,0]],u=[0,0],l=0,c=e-t+1;c>l;l++){var d=n[l],f=1-d,_=3*d*f,g=f*f*f,p=_*f,v=_*d,m=d*d*d,y=i.normalize(p),w=r.normalize(v),x=this.points[t+l].subtract(a.multiply(g+p)).subtract(o.multiply(v+m));h[0][0]+=y.dot(y),h[0][1]+=y.dot(w),h[1][0]=h[0][1],h[1][1]+=w.dot(w),u[0]+=y.dot(x),u[1]+=w.dot(x)}var b,C,S=h[0][0]*h[1][1]-h[1][0]*h[0][1];if(Math.abs(S)>s){var P=h[0][0]*u[1]-h[1][0]*u[0],k=u[0]*h[1][1]-u[1]*h[0][1];b=k/S,C=P/S}else{var M=h[0][0]+h[0][1],I=h[1][0]+h[1][1];b=C=Math.abs(M)>s?u[0]/M:Math.abs(I)>s?u[1]/I:0}var z,A,O=o.getDistance(a),T=s*O;if(T>b||T>C)b=C=O/3;else{var L=o.subtract(a);z=i.normalize(b),A=r.normalize(C),z.dot(L)-A.dot(L)>O*O&&(b=C=O/3,z=A=null)}return[a,a.add(z||i.normalize(b)),o.add(A||r.normalize(C)),o]},reparameterize:function(t,e,n,i){for(var r=t;e>=r;r++)n[r-t]=this.findRoot(i,this.points[r],n[r-t]);for(var r=1,s=n.length;s>r;r++)if(n[r]<=n[r-1])return!1;return!0},findRoot:function(t,e,n){for(var i=[],r=[],s=0;2>=s;s++)i[s]=t[s+1].subtract(t[s]).multiply(3);for(var s=0;1>=s;s++)r[s]=i[s+1].subtract(i[s]).multiply(2);var a=this.evaluate(3,t,n),o=this.evaluate(2,i,n),h=this.evaluate(1,r,n),u=a.subtract(e),l=o.dot(o)+u.dot(h);return Math.abs(l)<1e-6?n:n-u.dot(o)/l},evaluate:function(t,e,n){for(var i=e.slice(),r=1;t>=r;r++)for(var s=0;t-r>=s;s++)i[s]=i[s].multiply(1-n).add(i[s+1].multiply(n));return i[0]},chordLengthParameterize:function(t,e){for(var n=[0],i=t+1;e>=i;i++)n[i-t]=n[i-t-1]+this.points[i].getDistance(this.points[i-1]);for(var i=1,r=e-t;r>=i;i++)n[i]/=n[r];return n},findMaxError:function(t,e,n,i){for(var r=Math.floor((e-t+1)/2),s=0,a=t+1;e>a;a++){var o=this.evaluate(3,n,i[a-t]),h=o.subtract(this.points[a]),u=h.x*h.x+h.y*h.y;u>=s&&(s=u,r=a)}return{error:s,index:r}}}),E=m.extend({_class:"TextItem",_boundsSelected:!0,_applyMatrix:!1,_canApplyMatrix:!1,_serializeFields:{content:null},_boundsGetter:"getBounds",initialize:function(n){this._content="",this._lines=[];var i=n&&e.isPlainObject(n)&&n.x===t&&n.y===t;this._initialize(i&&n,!i&&h.read(arguments))},_equals:function(t){return this._content===t._content},_clone:function wt(t,e,n){return t.setContent(this._content),wt.base.call(this,t,e,n)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(265)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),N=E.extend({_class:"PointText",initialize:function(){E.apply(this,arguments)},clone:function(t){return this._clone(new N(m.NO_INSERT),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.translate(t.subtract(this._matrix.getTranslation()))},_draw:function(t){if(this._content){this._setStyles(t);var e=this._style,n=this._lines,i=e.getLeading(),r=t.shadowColor;t.font=e.getFontStyle(),t.textAlign=e.getJustification();for(var s=0,a=n.length;a>s;s++){t.shadowColor=r;var o=n[s];e.hasFill()&&(t.fillText(o,0,0),t.shadowColor="rgba(0,0,0,0)"),e.hasStroke()&&t.strokeText(o,0,0),t.translate(0,i)}}},_getBounds:function(t,e){var n=this._style,i=this._lines,r=i.length,s=n.getJustification(),a=n.getLeading(),o=this.getView().getTextWidth(n.getFontStyle(),i),h=0;"left"!==s&&(h-=o/("center"===s?2:1));var u=new d(h,r?-.75*a:0,o,r*a);return e?e._transformBounds(u,u):u}}),j=e.extend(new function(){function t(t){var e,i=t.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/);if(i){e=[0,0,0];for(var r=0;3>r;r++){var s=i[r+1];e[r]=parseInt(1==s.length?s+s:s,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var s=+e[r];e[r]=3>r?s/255:s}}else{var h=a[t];if(!h){n||(n=Y.getContext(1,1),n.globalCompositeOperation="copy"),n.fillStyle="rgba(0,0,0,0)",n.fillStyle=t,n.fillRect(0,0,1,1);var u=n.getImageData(0,0,1,1).data;h=a[t]=[u[0]/255,u[1]/255,u[2]/255]}e=h.slice()}return e}var n,i={gray:["gray"],rgb:["red","green","blue"],hsb:["hue","saturation","brightness"],hsl:["hue","saturation","lightness"],gradient:["gradient","origin","destination","highlight"]},r={},a={},u=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],l={"rgb-hsb":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4);return[a,0===i?0:s/i,i]},"hsb-rgb":function(t,e,n){t=(t/60%6+6)%6;var i=Math.floor(t),r=t-i,i=u[i],s=[n,n*(1-e),n*(1-e*r),n*(1-e*(1-r))];return[s[i[0]],s[i[1]],s[i[2]]]},"rgb-hsl":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s,o=a?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4),h=(i+r)/2,u=a?0:.5>h?s/(i+r):s/(2-i-r);return[o,u,h]},"hsl-rgb":function(t,e,n){if(t=(t/360%1+1)%1,0===e)return[n,n,n];for(var i=[t+1/3,t,t-1/3],r=.5>n?n*(1+e):n+e-n*e,s=2*n-r,a=[],o=0;3>o;o++){var h=i[o];0>h&&(h+=1),h>1&&(h-=1),a[o]=1>6*h?s+6*(r-s)*h:1>2*h?r:2>3*h?s+(r-s)*(2/3-h)*6:s}return a},"rgb-gray":function(t,e,n){return[.2989*t+.587*e+.114*n]},"gray-rgb":function(t){return[t,t,t]},"gray-hsb":function(t){return[0,0,t]},"gray-hsl":function(t){return[0,0,t]},"gradient-rgb":function(){return[]},"rgb-gradient":function(){return[]}};return e.each(i,function(t,n){r[n]=[],e.each(t,function(t,s){var a=e.capitalize(t),o=/^(hue|saturation)$/.test(t),u=r[n][s]="gradient"===t?function(t){var e=this._components[0];return t=B.read(Array.isArray(t)?t:arguments,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"gradient"===n?function(){return h.read(arguments,0,{readNull:"highlight"===t,clone:!0})}:function(t){return null==t||isNaN(t)?0:t};this["get"+a]=function(){return this._type===n||o&&/^hs[bl]$/.test(this._type)?this._components[s]:this._convert(n)[s]},this["set"+a]=function(t){this._type===n||o&&/^hs[bl]$/.test(this._type)||(this._components=this._convert(n),this._properties=i[n],this._type=n),this._components[s]=u.call(this,t),this._changed()}},this)},{_class:"Color",_readIndex:!0,initialize:function c(e){var n,s,a,h,u=Array.prototype.slice,l=arguments,d=0;Array.isArray(e)&&(l=e,e=l[0]);var f=null!=e&&typeof e;if("string"===f&&e in i&&(n=e,e=l[1],Array.isArray(e)?(s=e,a=l[2]):(this.__read&&(d=1),l=u.call(l,1),f=typeof e)),!s){if(h="number"===f?l:"object"===f&&null!=e.length?e:null){n||(n=h.length>=3?"rgb":"gray");var _=i[n].length;a=h[_],this.__read&&(d+=h===arguments?_+(null!=a?1:0):1),h.length>_&&(h=u.call(h,0,_))}else if("string"===f)n="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===f)if(e.constructor===c){if(n=e._type,s=e._components.slice(),a=e._alpha,"gradient"===n)for(var g=1,p=s.length;p>g;g++){var v=s[g];v&&(s[g]=v.clone())}}else if(e.constructor===B)n="gradient",h=l;else{n="hue"in e?"lightness"in e?"hsl":"hsb":"gradient"in e||"stops"in e||"radial"in e?"gradient":"gray"in e?"gray":"rgb";var m=i[n];w=r[n],this._components=s=[];for(var g=0,p=m.length;p>g;g++){var y=e[m[g]];null==y&&0===g&&"gradient"===n&&"stops"in e&&(y={stops:e.stops,radial:e.radial}),y=w[g].call(this,y),null!=y&&(s[g]=y)}a=e.alpha}this.__read&&n&&(d=1)}if(this._type=n||"rgb",this._id=o.get(c),!s){this._components=s=[];for(var w=r[this._type],g=0,p=w.length;p>g;g++){var y=w[g].call(this,h&&h[g]);null!=y&&(s[g]=y)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=d)},_serialize:function(t,n){var i=this.getComponents();return e.serialize(/^(gray|rgb)$/.test(this._type)?i:[this._type].concat(i),t,!0,n)},_changed:function(){this._canvasStyle=null,this._owner&&this._owner._changed(65)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=l[this._type+"-"+t])?e.apply(this,this._components):l["rgb-"+t].apply(this,l[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new j(t,this._convert(t),this._alpha)},getType:function(){return this._type},setType:function(t){this._components=this._convert(t),this._properties=i[t],this._type=t},getComponents:function(){var t=this._components.slice();return null!=this._alpha&&t.push(this._alpha),t},getAlpha:function(){return null!=this._alpha?this._alpha:1},setAlpha:function(t){this._alpha=null==t?null:Math.min(Math.max(t,0),1),this._changed()},hasAlpha:function(){return null!=this._alpha},equals:function(t){var n=e.isPlainValue(t,!0)?j.read(arguments):t;return n===this||n&&this._class===n._class&&this._type===n._type&&this._alpha===n._alpha&&e.equals(this._components,n._components)||!1},toString:function(){for(var t=this._properties,e=[],n="gradient"===this._type,i=s.instance,r=0,a=t.length;a>r;r++){var o=this._components[r];null!=o&&e.push(t[r]+": "+(n?o:i.number(o)))}return null!=this._alpha&&e.push("alpha: "+i.number(this._alpha)),"{ "+e.join(", ")+" }"},toCSS:function(t){function e(t){return Math.round(255*(0>t?0:t>1?1:t))}var n=this._convert("rgb"),i=t||null==this._alpha?1:this._alpha;return n=[e(n[0]),e(n[1]),e(n[2])],1>i&&n.push(0>i?0:i),t?"#"+((1<<24)+(n[0]<<16)+(n[1]<<8)+n[2]).toString(16).slice(1):(4==n.length?"rgba(":"rgb(")+n.join(",")+")"},toCanvasStyle:function(t){if(this._canvasStyle)return this._canvasStyle;if("gradient"!==this._type)return this._canvasStyle=this.toCSS();var e,n=this._components,i=n[0],r=i._stops,s=n[1],a=n[2];if(i._radial){var o=a.getDistance(s),h=n[3];if(h){var u=h.subtract(s);u.getLength()>o&&(h=s.add(u.normalize(o-.1)))}var l=h||s;e=t.createRadialGradient(l.x,l.y,0,s.x,s.y,o)}else e=t.createLinearGradient(s.x,s.y,a.x,a.y);for(var c=0,d=r.length;d>c;c++){var f=r[c];e.addColorStop(f._rampPoint,f._color.toCanvasStyle())}return this._canvasStyle=e},transform:function(t){if("gradient"===this._type){for(var e=this._components,n=1,i=e.length;i>n;n++){var r=e[n];t._transformPoint(r,r,!0)}this._changed()}},statics:{_types:i,random:function(){var t=Math.random;return new j(t(),t(),t())}}})},new function(){var t={add:function(t,e){return t+e},subtract:function(t,e){return t-e},multiply:function(t,e){return t*e},divide:function(t,e){return t/e}};return e.each(t,function(t,e){this[e]=function(e){e=j.read(arguments);for(var n=this._type,i=this._components,r=e._convert(n),s=0,a=i.length;a>s;s++)r[s]=t(i[s],r[s]);return new j(n,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(j._types,function(t,n){var i=this[e.capitalize(n)+"Color"]=function(t){var e=null!=t&&typeof t,i="object"===e&&null!=t.length?t:"string"===e?null:arguments;return i?new j(n,i):new j(t)};if(3==n.length){var r=n.toUpperCase();j[r]=this[r+"Color"]=i}},e.exports);var B=e.extend({_class:"Gradient",initialize:function(t,e){this._id=o.get(),t&&this._set(t)&&(t=e=null),this._stops||this.setStops(t||["white","black"]),null==this._radial&&this.setRadial("string"==typeof e&&"radial"===e||e||!1)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._stops,this._radial],t,!0,n)})},_changed:function(){for(var t=0,e=this._owners&&this._owners.length;e>t;t++)this._owners[t]._changed()},_addOwner:function(t){this._owners||(this._owners=[]),this._owners.push(t)},_removeOwner:function(e){var n=this._owners?this._owners.indexOf(e):-1;-1!=n&&(this._owners.splice(n,1),0===this._owners.length&&(this._owners=t))},clone:function(){for(var t=[],e=0,n=this._stops.length;n>e;e++)t[e]=this._stops[e].clone();return new B(t,this._radial)},getStops:function(){return this._stops},setStops:function(e){if(this.stops)for(var n=0,i=this._stops.length;i>n;n++)this._stops[n]._owner=t;if(e.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=D.readAll(e,0,{clone:!0});for(var n=0,i=this._stops.length;i>n;n++){var r=this._stops[n];r._owner=this,r._defaultRamp&&r.setRampPoint(n/(i-1))}this._changed()},getRadial:function(){return this._radial},setRadial:function(t){this._radial=t,this._changed()},equals:function(t){if(t===this)return!0;if(t&&this._class===t._class&&this._stops.length===t._stops.length){for(var e=0,n=this._stops.length;n>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),D=e.extend({_class:"GradientStop",initialize:function(e,n){if(e){var i,r;n===t&&Array.isArray(e)?(i=e[0],r=e[1]):e.color?(i=e.color,r=e.rampPoint):(i=e,r=n),this.setColor(i),this.setRampPoint(r)}},clone:function(){return new D(this._color.clone(),this._rampPoint)},_serialize:function(t,n){return e.serialize([this._color,this._rampPoint],t,!0,n)},_changed:function(){this._owner&&this._owner._changed(65)},getRampPoint:function(){return this._rampPoint},setRampPoint:function(t){this._defaultRamp=null==t,this._rampPoint=t||0,this._changed()},getColor:function(){return this._color},setColor:function(t){this._color=j.read(arguments),this._color===t&&(this._color=t.clone()),this._color._owner=this,this._changed()},equals:function(t){return t===this||t&&this._class===t._class&&this._color.equals(t._color)&&this._rampPoint==t._rampPoint||!1}}),R=e.extend(new function(){var n={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",strokeScaling:!0,miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,fontFamily:"sans-serif",fontWeight:"normal",fontSize:12,font:"sans-serif",leading:null,justification:"left"},i={strokeWidth:97,strokeCap:97,strokeJoin:97,strokeScaling:105,miterLimit:97,fontFamily:9,fontWeight:9,fontSize:9,font:9,leading:9,justification:9},r={beans:!0},s={_defaults:n,_textDefaults:new e(n,{fillColor:new j}),beans:!0};return e.each(n,function(n,a){var o=/Color$/.test(a),u="shadowOffset"===a,l=e.capitalize(a),c=i[a],d="set"+l,f="get"+l;s[d]=function(e){var n=this._owner,i=n&&n._children;if(i&&i.length>0&&!(n instanceof O))for(var r=0,s=i.length;s>r;r++)i[r]._style[d](e);else{var h=this._values[a];h!==e&&(o&&(h&&(h._owner=t),e&&e.constructor===j&&(e._owner&&(e=e.clone()),e._owner=n)),this._values[a]=e,n&&n._changed(c||65))}},s[f]=function(n){var i,r=this._owner,s=r&&r._children;if(!s||0===s.length||n||r instanceof O){var i=this._values[a];if(i===t)i=this._defaults[a],i&&i.clone&&(i=i.clone());else{var l=o?j:u?h:null;!l||i&&i.constructor===l||(this._values[a]=i=l.read([i],0,{readNull:!0,clone:!0}),i&&o&&(i._owner=r))}return i}for(var c=0,d=s.length;d>c;c++){var _=s[c]._style[f]();if(0===c)i=_;else if(!e.equals(i,_))return t}return i},r[f]=function(t){return this._style[f](t)},r[d]=function(t){this._style[d](t)}}),m.inject(r),s},{_class:"Style",initialize:function(t,e,n){this._values={},this._owner=e,this._project=e&&e._project||n||paper.project,e instanceof E&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof R,n=e?t._values:t;if(n)for(var i in n)if(i in this._defaults){var r=n[i];this[i]=r&&e&&r.clone?r.clone():r}},equals:function(t){return t===this||t&&this._class===t._class&&e.equals(this._values,t._values)||!1},hasFill:function(){return!!this.getFillColor()},hasStroke:function(){return!!this.getStrokeColor()&&this.getStrokeWidth()>0},hasShadow:function(){return!!this.getShadowColor()&&this.getShadowBlur()>0},getView:function(){return this._project.getView()},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function xt(){var t=xt.base.call(this),e=this.getFontSize();return/pt|em|%|px/.test(e)&&(e=this.getView().getPixelSize(e)),null!=t?t:1.2*e}}),F=new function(){function t(t,e,n,i){for(var r=["","webkit","moz","Moz","ms","o"],s=e[0].toUpperCase()+e.substring(1),a=0;6>a;a++){var o=r[a],h=o?o+s:e;if(h in t){if(!n)return t[h];t[h]=i;break}}}return{getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getBounds:function(t,e){var n,i=t.ownerDocument,r=i.body,s=i.documentElement;try{n=t.getBoundingClientRect()}catch(a){n={left:0,top:0,width:0,height:0}}var o=n.left-(s.clientLeft||r.clientLeft||0),h=n.top-(s.clientTop||r.clientTop||0);if(!e){var u=i.defaultView;o+=u.pageXOffset||s.scrollLeft||r.scrollLeft,h+=u.pageYOffset||s.scrollTop||r.scrollTop}return new d(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new d(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return F.getBounds(t,e).getPoint()},getSize:function(t){return F.getBounds(t,!0).getSize()},isInvisible:function(t){return F.getSize(t).equals(new l(0,0))},isInView:function(t){return!F.isInvisible(t)&&F.getViewportBounds(t).intersects(F.getBounds(t,!0))},getPrefixed:function(e,n){return t(e,n)},setPrefixed:function(e,n,i){if("object"==typeof n)for(var r in n)t(e,r,!0,n[r]);else t(e,n,!0,i)}}},q={add:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.addEventListener(r[s],i,!1)},remove:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.removeEventListener(r[s],i,!1)},getPoint:function(t){var e=t.targetTouches?t.targetTouches.length?t.targetTouches[0]:t.changedTouches[0]:t;return new h(e.pageX||e.clientX+document.documentElement.scrollLeft,e.pageY||e.clientY+document.documentElement.scrollTop)},getTarget:function(t){return t.target||t.srcElement},getRelatedTarget:function(t){return t.relatedTarget||t.toElement},getOffset:function(t,e){return q.getPoint(t).subtract(F.getOffset(e||q.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};q.requestAnimationFrame=new function(){function t(){for(var e=s.length-1;e>=0;e--){var o=s[e],h=o[0],u=o[1];(!u||("true"==i.getAttribute(u,"keepalive")||a)&&F.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):r=!1)}var e,n=F.getPrefixed(window,"requestAnimationFrame"),r=!1,s=[],a=!0;return q.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(i,a){s.push([i,a]),n?r||(n(t),r=!0):e||(e=setInterval(t,1e3/60))}};var V=e.extend(n,{_class:"View",initialize:function bt(t,e){function n(t){return e[t]||parseInt(e.getAttribute(t),10)}function r(){var t=F.getSize(e);return t.isNaN()||t.isZero()?new l(n("width"),n("height")):t}this._project=t,this._scope=t._scope,this._element=e;var s;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=e.getAttribute("id"),null==this._id&&e.setAttribute("id",this._id="view-"+bt._id++),q.add(e,this._viewEvents);var a="none";if(F.setPrefixed(e.style,{userSelect:a,touchAction:a,touchCallout:a,contentZooming:a,userDrag:a,tapHighlightColor:"rgba(0,0,0,0)"}),i.hasAttribute(e,"resize")){var o=this;q.add(window,this._windowEvents={resize:function(){o.setViewSize(r())}})}if(this._setViewSize(s=r()),i.hasAttribute(e,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var h=this._stats.domElement,u=h.style,c=F.getOffset(e);u.position="absolute",u.left=c.x+"px",u.top=c.y+"px",document.body.appendChild(h)}bt._views.push(this),bt._viewsById[this._id]=this,this._viewSize=s,(this._matrix=new _)._owner=this,this._zoom=1,bt._focused||(bt._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(V._focused===this&&(V._focused=null),V._views.splice(V._views.indexOf(this),1),delete V._viewsById[this._id],this._project._view===this&&(this._project._view=null),q.remove(this._element,this._viewEvents),q.remove(window,this._windowEvents),this._element=this._project=null,this.off("frame"),this._animate=!1,this._frameItems={},!0):!1},_events:{onFrame:{install:function(){this.play()},uninstall:function(){this.pause()}},onResize:{}},_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;q.requestAnimationFrame(function(){t._requested=!1,t._animate&&(t._requestFrame(),t._handleFrame())},this._element),this._requested=!0},_handleFrame:function(){paper=this._scope;var t=Date.now()/1e3,n=this._before?t-this._before:0;this._before=t,this._handlingFrame=!0,this.emit("frame",new e({delta:n,time:this._time+=n,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.update()},_animateItem:function(t,e){var n=this._frameItems;e?(n[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.on("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.off("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.emit("frame",new e(t,{time:i.time+=t.delta,count:i.count++}))}},_update:function(){this._project._needsUpdate=!0,this._handlingFrame||(this._animate?this._handleFrame():this.update())},_changed:function(t){1&t&&(this._project._needsUpdate=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._update()},getElement:function(){return this._element},getPixelRatio:function(){return this._pixelRatio},getResolution:function(){return 72*this._pixelRatio},getViewSize:function(){var t=this._viewSize;return new c(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=l.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.emit("resize",{size:t,delta:e}),this._update())},_setViewSize:function(t){var e=this._element;e.width=t.width,e.height=t.height},getBounds:function(){return this._bounds||(this._bounds=this._matrix.inverted()._transformBounds(new d(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(){var t=h.read(arguments);this.scrollBy(t.subtract(this.getCenter()))},getZoom:function(){return this._zoom},setZoom:function(t){this._transform((new _).scale(t/this._zoom,this.getCenter())),this._zoom=t},isVisible:function(){return F.isInView(this._element)},scrollBy:function(){this._transform((new _).translate(h.read(arguments).negate()))},play:function(){this._animate=!0,this._requested||this._requestFrame()},pause:function(){this._animate=!1},draw:function(){this.update()},projectToView:function(){return this._matrix._transformPoint(h.read(arguments))},viewToProject:function(){return this._matrix._inverseTransform(h.read(arguments))}},{statics:{_views:[],_viewsById:{},_id:0,create:function(t,e){return"string"==typeof e&&(e=document.getElementById(e)),new Z(t,e)}}},new function(){function t(t){var e=q.getTarget(t);return e.getAttribute&&V._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(q.getOffset(e,t._element))}function n(){if(!V._focused||!V._focused.isVisible())for(var t=0,e=V._views.length;e>t;t++){var n=V._views[t];if(n&&n.isVisible()){V._focused=a=n;break}}}function i(t,e,n){t._handleEvent("mousemove",e,n);var i=t._scope.tool;return i&&i._handleEvent(l&&i.responds("mousedrag")?"mousedrag":"mousemove",e,n),t.update(),i}var r,s,a,o,h,u,l=!1,c=window.navigator;c.pointerEnabled||c.msPointerEnabled?(o="pointerdown MSPointerDown",h="pointermove MSPointerMove",u="pointerup pointercancel MSPointerUp MSPointerCancel"):(o="touchstart",h="touchmove",u="touchend touchcancel","ontouchstart"in window&&c.userAgent.match(/mobile|tablet|ip(ad|hone|od)|android|silk/i)||(o+=" mousedown",h+=" mousemove",u+=" mouseup"));var d={"selectstart dragstart":function(t){l&&t.preventDefault()}},f={mouseout:function(t){var n=V._focused,r=q.getRelatedTarget(t);!n||r&&"HTML"!==r.nodeName||i(n,e(n,t),t)},scroll:n};return d[o]=function(n){var i=V._focused=t(n),s=e(i,n);l=!0,i._handleEvent("mousedown",s,n),(r=i._scope.tool)&&r._handleEvent("mousedown",s,n),i.update()},f[h]=function(o){var h=V._focused;if(!l){var u=t(o);u?(h!==u&&i(h,e(h,o),o),s=h,h=V._focused=a=u):a&&a===h&&(h=V._focused=s,n())}if(h){var c=e(h,o);(l||h.getBounds().contains(c))&&(r=i(h,c,o))}},f[u]=function(t){var n=V._focused;if(n&&l){var i=e(n,t);l=!1,n._handleEvent("mouseup",i,t),r&&r._handleEvent("mouseup",i,t),n.update()}},q.add(document,f),q.add(window,{load:n}),{_viewEvents:d,_handleEvent:function(){},statics:{updateFocus:n}}}),Z=V.extend({_class:"CanvasView",initialize:function(t,e){if(!(e instanceof HTMLCanvasElement)){var n=l.read(arguments,1);if(n.isZero())throw Error("Cannot create CanvasView with the provided argument: "+[].slice.call(arguments,1));e=Y.getCanvas(n)}if(this._context=e.getContext("2d"),this._eventCounters={},this._pixelRatio=1,!/^off|false$/.test(i.getAttribute(e,"hidpi"))){var r=window.devicePixelRatio||1,s=F.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=r/s}V.call(this,t,e)},_setViewSize:function(t){var e=this._element,n=this._pixelRatio,r=t.width,s=t.height;if(e.width=r*n,e.height=s*n,1!==n){if(!i.hasAttribute(e,"resize")){var a=e.style;a.width=r+"px",a.height=s+"px"}this._context.scale(n,n)}},getPixelSize:function(t){var e,n=paper.browser;if(n&&n.firefox){var i=this._element.parentNode,r=document.createElement("div");r.style.fontSize=t,i.appendChild(r),e=parseFloat(F.getStyles(r).fontSize),i.removeChild(r)}else{var s=this._context,a=s.font;s.font=t+" serif",e=parseFloat(s.font),s.font=a}return e},getTextWidth:function(t,e){var n=this._context,i=n.font,r=0;n.font=t;for(var s=0,a=e.length;a>s;s++)r=Math.max(r,n.measureText(e[s]).width);return n.font=i,r},update:function(t){var e=this._project;if(!e||!t&&!e._needsUpdate)return!1;var n=this._context,i=this._viewSize;return n.clearRect(0,0,i.width+1,i.height+1),e.draw(n,this._matrix,this._pixelRatio),e._needsUpdate=!1,!0}},new function(){function e(e,n,i,r,s,a){function o(e){return e.responds(n)&&(h||(h=new G(n,i,r,s,a?r.subtract(a):null)),e.emit(n,h)&&h.isStopped)?(i.preventDefault(),!0):t}for(var h,u=s;u;){if(o(u))return!0;u=u.getParent()}return o(e)?!0:!1}var n,i,r,s,a,o,h,u,l;return{_handleEvent:function(t,c,d){if(this._eventCounters[t]){var f=this._project,_=f.hitTest(c,{tolerance:0,fill:!0,stroke:!0}),g=_&&_.item,p=!1;switch(t){case"mousedown":for(p=e(this,t,d,c,g),u=a==g&&Date.now()-l<300,s=a=g,n=i=r=c,h=!p&&g;h&&!h.responds("mousedrag");)h=h._parent;break;case"mouseup":p=e(this,t,d,c,g,n),h&&(i&&!i.equals(c)&&e(this,"mousedrag",d,c,h,i),g!==h&&(r=c,e(this,"mousemove",d,c,g,r))),!p&&g&&g===s&&(l=Date.now(),e(this,u&&s.responds("doubleclick")?"doubleclick":"click",d,n,g),u=!1),s=h=null;break;case"mousemove":h&&(p=e(this,"mousedrag",d,c,h,i)),p||(g!==o&&(r=c),p=e(this,t,d,c,g,r)),i=r=c,g!==o&&(e(this,"mouseleave",d,c,o),o=g,e(this,"mouseenter",d,c,g))}return p}}}}),H=e.extend({_class:"Event",initialize:function(t){this.event=t},isPrevented:!1,isStopped:!1,preventDefault:function(){this.isPrevented=!0,this.event.preventDefault()},stopPropagation:function(){this.isStopped=!0,this.event.stopPropagation()},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return W.modifiers}}),U=H.extend({_class:"KeyEvent",initialize:function(t,e,n,i){H.call(this,i),this.type=t?"keydown":"keyup",this.key=e,this.character=n},toString:function(){return"{ type: '"+this.type+"', key: '"+this.key+"', character: '"+this.character+"', modifiers: "+this.getModifiers()+" }"}}),W=new function(){function t(i,s,u,l){var c,d=u?String.fromCharCode(u):"",f=r[s],_=f||d.toLowerCase(),g=i?"keydown":"keyup",p=V._focused,v=p&&p.isVisible()&&p._scope,m=v&&v.tool;if(h[_]=i,i?o[s]=u:delete o[s],f&&(c=e.camelize(f))in a){a[c]=i;var y=paper.browser;if("command"===c&&y&&y.mac)if(i)n={};else{for(var w in n)w in o&&t(!1,w,n[w],l);n=null}}else i&&n&&(n[s]=u);m&&m.responds(g)&&(paper=v,m.emit(g,new U(i,_,d,l)), -p&&p.update())}var n,i,r={8:"backspace",9:"tab",13:"enter",16:"shift",17:"control",18:"option",19:"pause",20:"caps-lock",27:"escape",32:"space",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",46:"delete",91:"command",93:"command",224:"command"},s={9:!0,13:!0,32:!0},a=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),o={},h={};return q.add(document,{keydown:function(e){var n=e.which||e.keyCode;n in r||a.command?t(!0,n,n in s||a.command?n:0,e):i=n},keypress:function(e){null!=i&&(t(!0,i,e.which||e.keyCode,e),i=null)},keyup:function(e){var n=e.which||e.keyCode;n in o&&t(!1,n,o[n],e)}}),q.add(window,{blur:function(e){for(var n in o)t(!1,n,o[n],e)}}),{modifiers:a,isDown:function(t){return!!h[t]}}},G=H.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){H.call(this,e),this.type=t,this.point=n,this.target=i,this.delta=r},toString:function(){return"{ type: '"+this.type+"', point: "+this.point+", target: "+this.target+(this.delta?", delta: "+this.delta:"")+", modifiers: "+this.getModifiers()+" }"}}),$=H.extend({_class:"ToolEvent",_item:null,initialize:function(t,e,n){this.tool=t,this.type=e,this.event=n},_choosePoint:function(t,e){return t?t:e?e.clone():null},getPoint:function(){return this._choosePoint(this._point,this.tool._point)},setPoint:function(t){this._point=t},getLastPoint:function(){return this._choosePoint(this._lastPoint,this.tool._lastPoint)},setLastPoint:function(t){this._lastPoint=t},getDownPoint:function(){return this._choosePoint(this._downPoint,this.tool._downPoint)},setDownPoint:function(t){this._downPoint=t},getMiddlePoint:function(){return!this._middlePoint&&this.tool._lastPoint?this.tool._point.add(this.tool._lastPoint).divide(2):this._middlePoint},setMiddlePoint:function(t){this._middlePoint=t},getDelta:function(){return!this._delta&&this.tool._lastPoint?this.tool._point.subtract(this.tool._lastPoint):this._delta},setDelta:function(t){this._delta=t},getCount:function(){return/^mouse(down|up)$/.test(this.type)?this.tool._downCount:this.tool._count},setCount:function(t){this.tool[/^mouse(down|up)$/.test(this.type)?"downCount":"count"]=t},getItem:function(){if(!this._item){var t=this.tool._scope.project.hitTest(this.getPoint());if(t){for(var e=t.item,n=e._parent;/^(Group|CompoundPath)$/.test(n._class);)e=n,n=n._parent;this._item=e}}return this._item},setItem:function(t){this._item=t},toString:function(){return"{ type: "+this.type+", point: "+this.getPoint()+", count: "+this.getCount()+", modifiers: "+this.getModifiers()+" }"}}),X=r.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){r.call(this),this._firstMove=!0,this._count=0,this._downCount=0,this._set(t)},getMinDistance:function(){return this._minDistance},setMinDistance:function(t){this._minDistance=t,null!=t&&null!=this._maxDistance&&t>this._maxDistance&&(this._maxDistance=t)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=t&&tu)return!1;if(null!=i&&0!=i)if(u>i)e=this._point.add(h.normalize(i));else if(a)return!1}if(s&&e.equals(this._point))return!1}switch(this._lastPoint=r&&"mousemove"==t?e:this._point,this._point=e,t){case"mousedown":this._lastPoint=this._downPoint,this._downPoint=this._point,this._downCount++;break;case"mouseup":this._lastPoint=this._downPoint}return this._count=r?0:this._count+1,!0},_fireEvent:function(t,e){var n=paper.project._removeSets;if(n){"mouseup"===t&&(n.mousedrag=null);var i=n[t];if(i){for(var r in i){var s=i[r];for(var a in n){var o=n[a];o&&o!=i&&delete o[s._id]}s.remove()}n[t]=null}}return this.responds(t)&&this.emit(t,new $(this,t,e))},_handleEvent:function(t,e,n){paper=this._scope;var i=!1;switch(t){case"mousedown":this._updateEvent(t,e,null,null,!0,!1,!1),i=this._fireEvent(t,n);break;case"mousedrag":for(var r=!1,s=!1;this._updateEvent(t,e,this.minDistance,this.maxDistance,!1,r,s);)i=this._fireEvent(t,n)||i,r=!0,s=!0;break;case"mouseup":!e.equals(this._point)&&this._updateEvent("mousedrag",e,this.minDistance,this.maxDistance,!1,!1,!1)&&(i=this._fireEvent("mousedrag",n)),this._updateEvent(t,e,null,this.maxDistance,!1,!1,!1),i=this._fireEvent(t,n)||i,this._updateEvent(t,e,null,null,!0,!1,!1),this._firstMove=!0;break;case"mousemove":for(;this._updateEvent(t,e,this.minDistance,this.maxDistance,this._firstMove,!0,!1);)i=this._fireEvent(t,n)||i,this._firstMove=!1}return i&&n.preventDefault(),i}}),J={request:function(e,n,i,r){r=r===t?!0:r;var s=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return s.open(e.toUpperCase(),n,r),"overrideMimeType"in s&&s.overrideMimeType("text/plain"),s.onreadystatechange=function(){if(4===s.readyState){var t=s.status;if(0!==t&&200!==t)throw Error("Could not load "+n+" (Error "+t+")");i.call(s,s.responseText)}},s.send(null)}},Y={canvases:[],getCanvas:function(t,e){var n,i=!0;"object"==typeof t&&(e=t.height,t=t.width),n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var r=n.getContext("2d");return n.width===t&&n.height===e?i&&r.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),r.save(),n},getContext:function(t,e){return this.getCanvas(t,e).getContext("2d")},release:function(t){var e=t.canvas?t.canvas:t;e.getContext("2d").restore(),this.canvases.push(e)}},K=new function(){function t(t,e,n){return.2989*t+.587*e+.114*n}function n(e,n,i,r){var s=r-t(e,n,i);f=e+s,_=n+s,g=i+s;var r=t(f,_,g),a=p(f,_,g),o=v(f,_,g);if(0>a){var h=r-a;f=r+(f-r)*r/h,_=r+(_-r)*r/h,g=r+(g-r)*r/h}if(o>255){var u=255-r,l=o-r;f=r+(f-r)*u/l,_=r+(_-r)*u/l,g=r+(g-r)*u/l}}function i(t,e,n){return v(t,e,n)-p(t,e,n)}function r(t,e,n,i){var r,s=[t,e,n],a=v(t,e,n),o=p(t,e,n);o=o===t?0:o===e?1:2,a=a===t?0:a===e?1:2,r=0===p(o,a)?1===v(o,a)?2:1:0,s[a]>s[o]?(s[r]=(s[r]-s[o])*i/(s[a]-s[o]),s[a]=i):s[r]=s[a]=0,s[o]=0,f=s[0],_=s[1],g=s[2]}var s,a,o,h,u,l,c,d,f,_,g,p=Math.min,v=Math.max,m=Math.abs,y={multiply:function(){f=u*s/255,_=l*a/255,g=c*o/255},screen:function(){f=u+s-u*s/255,_=l+a-l*a/255,g=c+o-c*o/255},overlay:function(){f=128>u?2*u*s/255:255-2*(255-u)*(255-s)/255,_=128>l?2*l*a/255:255-2*(255-l)*(255-a)/255,g=128>c?2*c*o/255:255-2*(255-c)*(255-o)/255},"soft-light":function(){var t=s*u/255;f=t+u*(255-(255-u)*(255-s)/255-t)/255,t=a*l/255,_=t+l*(255-(255-l)*(255-a)/255-t)/255,t=o*c/255,g=t+c*(255-(255-c)*(255-o)/255-t)/255},"hard-light":function(){f=128>s?2*s*u/255:255-2*(255-s)*(255-u)/255,_=128>a?2*a*l/255:255-2*(255-a)*(255-l)/255,g=128>o?2*o*c/255:255-2*(255-o)*(255-c)/255},"color-dodge":function(){f=0===u?0:255===s?255:p(255,255*u/(255-s)),_=0===l?0:255===a?255:p(255,255*l/(255-a)),g=0===c?0:255===o?255:p(255,255*c/(255-o))},"color-burn":function(){f=255===u?255:0===s?0:v(0,255-255*(255-u)/s),_=255===l?255:0===a?0:v(0,255-255*(255-l)/a),g=255===c?255:0===o?0:v(0,255-255*(255-c)/o)},darken:function(){f=s>u?u:s,_=a>l?l:a,g=o>c?c:o},lighten:function(){f=u>s?u:s,_=l>a?l:a,g=c>o?c:o},difference:function(){f=u-s,0>f&&(f=-f),_=l-a,0>_&&(_=-_),g=c-o,0>g&&(g=-g)},exclusion:function(){f=u+s*(255-u-u)/255,_=l+a*(255-l-l)/255,g=c+o*(255-c-c)/255},hue:function(){r(s,a,o,i(u,l,c)),n(f,_,g,t(u,l,c))},saturation:function(){r(u,l,c,i(s,a,o)),n(f,_,g,t(u,l,c))},luminosity:function(){n(u,l,c,t(s,a,o))},color:function(){n(s,a,o,t(u,l,c))},add:function(){f=p(u+s,255),_=p(l+a,255),g=p(c+o,255)},subtract:function(){f=v(u-s,0),_=v(l-a,0),g=v(c-o,0)},average:function(){f=(u+s)/2,_=(l+a)/2,g=(c+o)/2},negation:function(){f=255-m(255-s-u),_=255-m(255-a-l),g=255-m(255-o-c)}},w=this.nativeModes=e.each(["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","darker","copy","xor"],function(t){this[t]=!0},{}),x=Y.getContext(1,1);e.each(y,function(t,e){var n="darken"===e,i=!1;x.save();try{x.fillStyle=n?"#300":"#a00",x.fillRect(0,0,1,1),x.globalCompositeOperation=e,x.globalCompositeOperation===e&&(x.fillStyle=n?"#a00":"#300",x.fillRect(0,0,1,1),i=x.getImageData(0,0,1,1).data[0]!==n?170:51)}catch(r){}x.restore(),w[e]=i}),Y.release(x),this.process=function(t,e,n,i,r){var p=e.canvas,v="normal"===t;if(v||w[t])n.save(),n.setTransform(1,0,0,1,0,0),n.globalAlpha=i,v||(n.globalCompositeOperation=t),n.drawImage(p,r.x,r.y),n.restore();else{var m=y[t];if(!m)return;for(var x=n.getImageData(r.x,r.y,p.width,p.height),b=x.data,C=e.getImageData(0,0,p.width,p.height).data,S=0,P=b.length;P>S;S+=4){s=C[S],u=b[S],a=C[S+1],l=b[S+1],o=C[S+2],c=b[S+2],h=C[S+3],d=b[S+3],m();var k=h*i/255,M=1-k;b[S]=k*f+M*u,b[S+1]=k*_+M*l,b[S+2]=k*g+M*c,b[S+3]=h*i+M*d}n.putImageData(x,r.x,r.y)}}},Q=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],strokeScaling:["vector-effect","lookup",{"true":"none","false":"non-scaling-stroke"},function(t,e){return!e&&(t instanceof z||t instanceof x||t instanceof E)}],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],fontFamily:["font-family","string"],fontWeight:["font-weight","string"],fontSize:["font-size","number"],justification:["text-anchor","lookup",{left:"start",center:"middle",right:"end"}],opacity:["opacity","number"],blendMode:["mix-blend-mode","string"]},function(t,n){var i=e.capitalize(n),r=t[2];this[n]={type:t[1],property:n,attribute:t[0],toSVG:r,fromSVG:r&&e.each(r,function(t,e){this[t]=e},{}),exportFilter:t[3],get:"get"+i,set:"set"+i}},{}),tt={href:"http://www.w3.org/1999/xlink",xlink:"http://www.w3.org/2000/xmlns"};return new function(){function t(t,e){for(var n in e){var i=e[n],r=tt[n];"number"==typeof i&&(i=b.number(i)),r?t.setAttributeNS(r,n,i):t.setAttribute(n,i)}return t}function n(e,n){return t(document.createElementNS("http://www.w3.org/2000/svg",e),n)}function i(t,n,i){var r=new e,s=t.getTranslation();if(n){t=t.shiftless();var o=t._inverseTransform(s);r[i?"cx":"x"]=o.x,r[i?"cy":"y"]=o.y,s=null}if(!t.isIdentity()){var h=t.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;s&&!s.isZero()&&u.push("translate("+b.point(s)+")"),a.isZero(c.x-1)&&a.isZero(c.y-1)||u.push("scale("+b.point(c)+")"),l&&u.push("rotate("+b.number(l)+")"),r.transform=u.join(" ")}else r.transform="matrix("+t.getValues().join(",")+")"}return r}function r(e,r){for(var s=i(e._matrix),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=w(l,r);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),v(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function o(t,e){var r=i(t._matrix,!0),s=t.getSize(),a=t.getImage();return r.x-=s.width/2,r.y-=s.height/2,r.width=s.width,r.height=s.height,r.href=e.embedImages===!1&&a&&a.src||t.toDataURL(),n("image",r)}function h(t,e){var r=e.matchShapes;if(r){var s=t.toShape(!1);if(s)return u(s,e)}var a,o=t._segments,h=i(t._matrix);if(0===o.length)return null;if(r&&!t.hasHandles())if(o.length>=3){a=t._closed?"polygon":"polyline";for(var l=[],c=0,d=o.length;d>c;c++)l.push(b.point(o[c]._point));h.points=l.join(" ")}else{a="line";var f=o[0]._point,_=o[o.length-1]._point;h.set({x1:f.x,y1:f.y,x2:_.x,y2:_.y})}else a="path",h.d=t.getPathData(null,e.precision);return n(a,h)}function u(t){var e=t._type,r=t._radius,s=i(t._matrix,!0,"rectangle"!==e);if("rectangle"===e){e="rect";var a=t._size,o=a.width,h=a.height;s.x-=o/2,s.y-=h/2,s.width=o,s.height=h,r.isZero()&&(r=null)}return r&&("circle"===e?s.r=r:(s.rx=r.width,s.ry=r.height)),n(e,s)}function l(t,e){var r=i(t._matrix),s=t.getPathData(null,e.precision);return s&&(r.d=s),n("path",r)}function c(t,e){var r=i(t._matrix,!0),s=t.getSymbol(),a=g(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:b.rectangle(h)}),a.appendChild(w(o,e)),v(s,a,"symbol")),r.href="#"+a.id,r.x+=h.x,r.y+=h.y,r.width=b.number(h.width),r.height=b.number(h.height),r.overflow="visible",n("use",r)}function d(t){var e=g(t,"color");if(!e){var i,r=t.getGradient(),s=r._radial,a=t.getOrigin().transform(),o=t.getDestination().transform();if(s){i={cx:a.x,cy:a.y,r:a.getDistance(o)};var h=t.getHighlight();h&&(h=h.transform(),i.fx=h.x,i.fy=h.y)}else i={x1:a.x,y1:a.y,x2:o.x,y2:o.y};i.gradientUnits="userSpaceOnUse",e=n((s?"radial":"linear")+"Gradient",i);for(var u=r._stops,l=0,c=u.length;c>l;l++){var d=u[l],f=d._color,_=f.getAlpha();i={offset:d._rampPoint,"stop-color":f.toCSS(!0)},1>_&&(i["stop-opacity"]=_),e.appendChild(n("stop",i))}v(t,e,"color")}return"url(#"+e.id+")"}function f(t){var e=n("text",i(t._matrix,!0));return e.textContent=t._content,e}function _(n,i,r){var s={},a=!r&&n.getParent();return null!=n._name&&(s.id=n._name),e.each(Q,function(t){var i=t.get,r=t.type,o=n[i]();if(t.exportFilter?t.exportFilter(n,o):!a||!e.equals(a[i](),o)){if("color"===r&&null!=o){var h=o.getAlpha();1>h&&(s[t.attribute+"-opacity"]=h)}s[t.attribute]=null==o?"none":"number"===r?b.number(o):"color"===r?o.gradient?d(o,n):o.toCSS(!0):"array"===r?o.join(","):"lookup"===r?t.toSVG[o]:o}}),1===s.opacity&&delete s.opacity,n._visible||(s.visibility="hidden"),t(i,s)}function g(t,e){return C||(C={ids:{},svgs:{}}),t&&C.svgs[e+"-"+t._id]}function v(t,e,n){C||g();var i=C.ids[n]=(C.ids[n]||0)+1;e.id=n+"-"+i,C.svgs[n+"-"+t._id]=e}function y(t,e){var i=t,r=null;if(C){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in C.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(C.svgs[s]);C=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function w(t,e,n){var i=S[t._class],r=i&&i(t,e);if(r){var s=e.onExport;s&&(r=s(t,r,e)||r);var a=JSON.stringify(t._data);a&&"{}"!==a&&"null"!==a&&r.setAttribute("data-paper-data",a)}return r&&_(t,r,n)}function x(t){return t||(t={}),b=new s(t.precision),t}var b,C,S={Group:r,Layer:r,Raster:o,Path:h,Shape:u,CompoundPath:l,PlacedSymbol:c,PointText:f};m.inject({exportSVG:function(t){return t=x(t),y(w(this,t,!0),t)}}),p.inject({exportSVG:function(t){t=x(t);var e=this.layers,r=this.getView(),s=r.getViewSize(),a=n("svg",{x:0,y:0,width:s.width,height:s.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),o=a,h=r._matrix;h.isIdentity()||(o=a.appendChild(n("g",i(h))));for(var u=0,l=e.length;l>u;u++)o.appendChild(w(e[u],t,!0));return y(a,t)}})},new function(){function n(t,e,n,i){var r=tt[e],s=r?t.getAttributeNS(r,e):t.getAttribute(e);return"null"===s&&(s=null),null==s?i?null:n?"":0:n?s:parseFloat(s)}function i(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new h(e,i):null}function r(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new l(e,i):null}function s(t,e,n){return"none"===t?null:"number"===e?parseFloat(t):"array"===e?t?t.split(/[\s,]+/g).map(parseFloat):[]:"color"===e?S(t)||t:"lookup"===e?n[t]:t}function a(t,e,n,i){var r=t.childNodes,s="clippath"===e,a=new y,o=a._project,h=o._currentStyle,u=[];if(s||(a=C(a,t,i),o._currentStyle=a._style.clone()),i)for(var l=t.querySelectorAll("defs"),c=0,d=l.length;d>c;c++)P(l[c],n,!1);for(var c=0,d=r.length;d>c;c++){var f,_=r[c];1!==_.nodeType||"defs"===_.nodeName.toLowerCase()||!(f=P(_,n,!1))||f instanceof v||u.push(f)}return a.addChildren(u),s&&(a=C(a.reduce(),t,i)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){for(var n=t.getAttribute("points").match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g),i=[],r=0,s=n.length;s>r;r+=2)i.push(new h(parseFloat(n[r]),parseFloat(n[r+1])));var a=new A(i);return"polygon"===e&&a.closePath(),a}function u(t){var e=t.getAttribute("d"),n={pathData:e};return(e.match(/m/gi)||[]).length>1||/z\S+/i.test(e)?new O(n):new A(n)}function c(t,e){var r,s=(n(t,"href",!0)||"").substring(1),a="radialgradient"===e;if(s)r=I[s].getGradient();else{for(var o=t.childNodes,h=[],u=0,l=o.length;l>u;u++){var c=o[u];1===c.nodeType&&h.push(C(new D,c))}r=new B(h,a)}var d,f,_;return a?(d=i(t,"cx","cy"),f=d.add(n(t,"r"),0),_=i(t,"fx","fy",!0)):(d=i(t,"x1","y1"),f=i(t,"x2","y2")),C(new j(r,d,f,_),t),null}function f(t,e,n,i){for(var r=(i.getAttribute(n)||"").split(/\)\s*/g),s=new _,a=0,o=r.length;o>a;a++){var h=r[a];if(!h)break;for(var u=h.split(/\(\s*/),l=u[0],c=u[1].split(/[\s,]+/g),d=0,f=c.length;f>d;d++)c[d]=parseFloat(c[d]);switch(l){case"matrix":s.concatenate(new _(c[0],c[1],c[2],c[3],c[4],c[5]));break;case"rotate":s.rotate(c[0],c[1],c[2]);break;case"translate":s.translate(c[0],c[1]);break;case"scale":s.scale(c);break;case"skewX":s.skew(c[0],0);break;case"skewY":s.skew(0,c[0])}}t.transform(s)}function g(t,e,n){var i=t["fill-opacity"===n?"getFillColor":"getStrokeColor"]();i&&i.setAlpha(parseFloat(e))}function w(n,i,r){var s=n.attributes[i],a=s&&s.value;if(!a){var o=e.camelize(i);a=n.style[o],a||r.node[o]===r.parent[o]||(a=r.node[o])}return a?"none"===a?null:a:t}function C(n,i,r){var s={node:F.getStyles(i)||{},parent:!r&&F.getStyles(i.parentNode)||{}};return e.each(M,function(r,a){var o=w(i,a,s);o!==t&&(n=e.pick(r(n,o,a,i,s),n))}),n}function S(t){var e=t&&t.match(/\((?:#|)([^)']+)/);return e&&I[e[1]]}function P(t,n,i){function r(t){paper=a;var e=P(t,n,i),r=n.onLoad,s=a.project&&a.getView();r&&r.call(this,e),s.update()}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var s=t,a=paper;if(i)if("string"!=typeof t||/^.*s;s++){var o=r[s];if(1===o.nodeType){var h=o.nextSibling;document.body.appendChild(o);var u=P(o,n,i);return h?t.insertBefore(o,h):t.appendChild(o),u}}},g:a,svg:a,clippath:a,polygon:o,polyline:o,path:u,lineargradient:c,radialgradient:c,image:function(t){var e=new b(n(t,"href",!0));return e.on("load",function(){var e=r(t,"width","height");this.setSize(e);var n=this._matrix._transformPoint(i(t,"x","y").add(e.divide(2)));this.translate(n)}),e},symbol:function(t,e,n,i){return new v(a(t,e,n,i),!0)},defs:a,use:function(t){var e=(n(t,"href",!0)||"").substring(1),r=I[e],s=i(t,"x","y");return r?r instanceof v?r.place(s):r.clone().translate(s):null},circle:function(t){return new x.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new x.Ellipse({center:i(t,"cx","cy"),radius:r(t,"rx","ry")})},rect:function(t){var e=i(t,"x","y"),n=r(t,"width","height"),s=r(t,"rx","ry");return new x.Rectangle(new d(e,n),s)},line:function(t){return new A.Line(i(t,"x1","y1"),i(t,"x2","y2"))},text:function(t){var e=new N(i(t,"x","y").add(i(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},M=e.set(e.each(Q,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof x){var i=e[t.get]();i&&i.transform((new _).translate(e.getPosition(!0).negate()))}}},{}),{id:function(t,e){I[e]=t,t.setName&&t.setName(e)},"clip-path":function(t,e){var n=S(e);if(n){if(n=n.clone(),n.setClipMask(!0),!(t instanceof y))return new y(n,t);t.insertChild(0,n)}},gradientTransform:f,transform:f,"fill-opacity":g,"stroke-opacity":g,visibility:function(t,e){t.setVisible("visible"===e)},display:function(t,e){t.setVisible(null!==e)},"stop-color":function(t,e){t.setColor&&t.setColor(e)},"stop-opacity":function(t,e){t._color&&t._color.setAlpha(parseFloat(e))},offset:function(t,e){var n=e.match(/(.*)%$/);t.setRampPoint(n?n[1]/100:parseFloat(e))},viewBox:function(t,e,n,i,a){var o=new d(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof y){var u=h?o.getSize().divide(h):1,l=(new _).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof v){h&&o.setSize(h);var c="visible"!=w(i,"overflow",a),f=t._definition;c&&!o.contains(f.getBounds())&&(c=new x.Rectangle(o).transform(f._matrix),c.setClipMask(!0),f.addChild(c))}}}),I={};m.inject({importSVG:function(t,e){return this.addChild(P(t,e,!0))}}),p.inject({importSVG:function(t,e){return this.activate(),P(t,e,!0)}})},e.exports.PaperScript=function(){function t(t,e,n){var i=g[e];if(t&&t[i]){var r=t[i](n);return"!="===e?!r:r}switch(e){case"+":return t+n;case"-":return t-n;case"*":return t*n;case"/":return t/n;case"%":return t%n;case"==":return t==n;case"!=":return t!=n}}function n(t,e){var n=p[t];if(n&&e&&e[n])return e[n]();switch(t){case"+":return+e;case"-":return-e}}function r(t,e){return _.acorn.parse(t,e)}function s(t,e,n){function i(t){for(var e=0,n=u.length;n>e;e++){var i=u[e];if(i[0]>=t)break;t+=i[1]}return t}function s(e){return t.substring(i(e.range[0]),i(e.range[1]))}function a(e,n){return t.substring(i(e.range[1]),i(n.range[0]))}function o(e,n){for(var r=i(e.range[0]),s=i(e.range[1]),a=0,o=u.length-1;o>=0;o--)if(r>u[o][0]){a=o+1;break}u.splice(a,0,[r,n.length-s+r]),t=t.substring(0,r)+n+t.substring(s)}function h(t,e){if(t){for(var n in t)if("range"!==n&&"loc"!==n){var i=t[n];if(Array.isArray(i))for(var r=0,u=i.length;u>r;r++)h(i[r],t);else i&&"object"==typeof i&&h(i,t)}switch(t.type){case"UnaryExpression":if(t.operator in p&&"Literal"!==t.argument.type){var l=s(t.argument);o(t,'$__("'+t.operator+'", '+l+")")}break;case"BinaryExpression":if(t.operator in g&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right),f=a(t.left,t.right),_=t.operator;o(t,"__$__("+c+","+f.replace(RegExp("\\"+_),'"'+_+'"')+", "+d+")")}break;case"UpdateExpression":case"AssignmentExpression":var v=e&&e.type;if(!("ForStatement"===v||"BinaryExpression"===v&&/^[=!<>]/.test(e.operator)||"MemberExpression"===v&&e.computed))if("UpdateExpression"===t.type){var l=s(t.argument),m="__$__("+l+', "'+t.operator[0]+'", 1)',y=l+" = "+m;t.prefix||"AssignmentExpression"!==v&&"VariableDeclarator"!==v||(s(e.left||e.id)===l&&(y=m),y=l+"; "+y),o(t,y)}else if(/^.=$/.test(t.operator)&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right);o(t,c+" = __$__("+c+', "'+t.operator[0]+'", '+d+")")}}}}if(!t)return"";n=n||{},e=e||"";var u=[],l=null,c=paper.browser,d=c.versionNumber,f=/\r\n|\n|\r/gm;if(c.chrome&&d>=30||c.webkit&&d>=537.76||c.firefox&&d>=23){var _=0;if(0===window.location.href.indexOf(e)){var v=document.getElementsByTagName("html")[0].innerHTML;_=v.substr(0,v.indexOf(t)+1).match(f).length+1}var m=["AAAA"];m.length=(t.match(f)||[]).length+1+_,l={version:3,file:e,names:[],mappings:m.join(";AACA"),sourceRoot:"",sources:[e]};var y=n.source||!e&&t;y&&(l.sourcesContent=[y])}return h(r(t,{ranges:!0})),l&&(t=Array(_+1).join("\n")+t+"\n//# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(l))))+"\n//# sourceURL="+(e||"paperscript")),t}function a(i,r,a,o){function u(t,e){for(var n in t)!e&&/^_/.test(n)||!RegExp("([\\b\\s\\W]|^)"+n.replace(/\$/g,"\\$")+"\\b").test(i)||(g.push(n),p.push(t[n]))}paper=r;var l,c=r.getView(),d=/\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(i)?new X:null,f=d?d._events:[],_=["onFrame","onResize"].concat(f),g=[],p=[];i=s(i,a,o),u({__$__:t,$__:n,paper:r,view:c,tool:d},!0),u(r),_=e.each(_,function(t){RegExp("\\s+"+t+"\\b").test(i)&&(g.push(t),this.push(t+": "+t))},[]).join(", "),_&&(i+="\nreturn { "+_+" };");var v=paper.browser;if(v.chrome||v.firefox){var m=document.createElement("script"),y=document.head||document.getElementsByTagName("head")[0];v.firefox&&(i="\n"+i),m.appendChild(document.createTextNode("paper._execute = function("+g+") {"+i+"\n}")),y.appendChild(m),l=paper._execute,delete paper._execute,y.removeChild(m)}else l=Function(g,i);var w=l.apply(r,p)||{};e.each(f,function(t){var e=w[t];e&&(d[t]=e)}),c&&(w.onResize&&c.setOnResize(w.onResize),c.emit("resize",{size:c.size,delta:new h}),w.onFrame&&c.setOnFrame(w.onFrame),c.update())}function o(t){if(/^text\/(?:x-|)paperscript$/.test(t.type)&&"true"!==i.getAttribute(t,"ignore")){var e=i.getAttribute(t,"canvas"),n=document.getElementById(e),r=t.src||t.getAttribute("data-src"),s=i.hasAttribute(t,"async"),o="data-paper-scope";if(!n)throw Error('Unable to find canvas with id "'+e+'"');var h=i.get(n.getAttribute(o))||(new i).setup(n);return n.setAttribute(o,h._id),r?J.request("get",r,function(t){a(t,h,r)},s):a(t.innerHTML,h,t.baseURI),t.setAttribute("data-paper-ignore","true"),h}}function u(){e.each(document.getElementsByTagName("script"),o)}function c(t){return t?o(t):u()}var d,f,_=this;!function(t,e){return"object"==typeof d&&"object"==typeof module?e(d):"function"==typeof f&&f.amd?f(["exports"],e):void e(t.acorn||(t.acorn={}))}(this,function(t){"use strict";function e(t){ct=t||{};for(var e in gt)Object.prototype.hasOwnProperty.call(ct,e)||(ct[e]=gt[e]);_t=ct.sourceFile||null}function n(t,e){var n=pt(dt,t);e+=" ("+n.line+":"+n.column+")";var i=new SyntaxError(e);throw i.pos=t,i.loc=n,i.raisedAt=vt,i}function i(t){function e(t){if(1==t.length)return n+="return str === "+JSON.stringify(t[0])+";";n+="switch(str){";for(var e=0;e3){i.sort(function(t,e){return e.length-t.length}),n+="switch(str.length){";for(var r=0;rvt&&10!==n&&13!==n&&8232!==n&&8233!==n;)++vt,n=dt.charCodeAt(vt);ct.onComment&&ct.onComment(!1,dt.slice(t+2,vt),t,vt,e,ct.locations&&new r)}function u(){for(;ft>vt;){var t=dt.charCodeAt(vt);if(32===t)++vt;else if(13===t){++vt;var e=dt.charCodeAt(vt);10===e&&++vt,ct.locations&&(++Pt,kt=vt)}else if(10===t||8232===t||8233===t)++vt,ct.locations&&(++Pt,kt=vt);else if(t>8&&14>t)++vt;else if(47===t){var e=dt.charCodeAt(vt+1);if(42===e)o();else{if(47!==e)break;h()}}else if(160===t)++vt;else{if(!(t>=5760&&He.test(String.fromCharCode(t))))break;++vt}}}function l(){var t=dt.charCodeAt(vt+1);return t>=48&&57>=t?S(!0):(++vt,a(we))}function c(){var t=dt.charCodeAt(vt+1);return St?(++vt,x()):61===t?w(Se,2):w(be,1)}function d(){var t=dt.charCodeAt(vt+1);return 61===t?w(Se,2):w(je,1)}function f(t){var e=dt.charCodeAt(vt+1);return e===t?w(124===t?Ie:ze,2):61===e?w(Se,2):w(124===t?Ae:Te,1)}function _(){var t=dt.charCodeAt(vt+1);return 61===t?w(Se,2):w(Oe,1)}function g(t){var e=dt.charCodeAt(vt+1);return e===t?45==e&&62==dt.charCodeAt(vt+2)&&Xe.test(dt.slice(It,vt))?(vt+=3,h(),u(),y()):w(ke,2):61===e?w(Se,2):w(Pe,1)}function p(t){var e=dt.charCodeAt(vt+1),n=1;return e===t?(n=62===t&&62===dt.charCodeAt(vt+2)?3:2,61===dt.charCodeAt(vt+n)?w(Se,n+1):w(Ne,n)):33==e&&60==t&&45==dt.charCodeAt(vt+2)&&45==dt.charCodeAt(vt+3)?(vt+=4,h(),u(),y()):(61===e&&(n=61===dt.charCodeAt(vt+2)?3:2),w(Ee,n))}function v(t){var e=dt.charCodeAt(vt+1);return 61===e?w(Le,61===dt.charCodeAt(vt+2)?3:2):w(61===t?Ce:Me,1)}function m(t){switch(t){case 46:return l();case 40:return++vt,a(ge);case 41:return++vt,a(pe);case 59:return++vt,a(me);case 44:return++vt,a(ve);case 91:return++vt,a(ce);case 93:return++vt,a(de);case 123:return++vt,a(fe);case 125:return++vt,a(_e);case 58:return++vt,a(ye);case 63:return++vt,a(xe);case 48:var e=dt.charCodeAt(vt+1);if(120===e||88===e)return C();case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return S(!1);case 34:case 39:return P(t);case 47:return c(t);case 37:case 42:return d();case 124:case 38:return f(t);case 94:return _();case 43:case 45:return g(t);case 60:case 62:return p(t);case 61:case 33:return v(t);case 126:return w(Me,1)}return!1}function y(t){if(t?vt=mt+1:mt=vt,ct.locations&&(wt=new r),t)return x();if(vt>=ft)return a(Dt);var e=dt.charCodeAt(vt);if(Ye(e)||92===e)return I();var i=m(e);if(i===!1){var s=String.fromCharCode(e);if("\\"===s||Ge.test(s))return I();n(vt,"Unexpected character '"+s+"'")}return i}function w(t,e){var n=dt.slice(vt,vt+e);vt+=e,a(t,n)}function x(){for(var t,e,i="",r=vt;;){vt>=ft&&n(r,"Unterminated regular expression");var s=dt.charAt(vt);if(Xe.test(s)&&n(r,"Unterminated regular expression"),t)t=!1;else{if("["===s)e=!0;else if("]"===s&&e)e=!1;else if("/"===s&&!e)break;t="\\"===s}++vt}var i=dt.slice(r,vt);++vt;var o=M();return o&&!/^[gmsiy]*$/.test(o)&&n(r,"Invalid regexp flag"),a(Nt,RegExp(i,o))}function b(t,e){for(var n=vt,i=0,r=0,s=null==e?1/0:e;s>r;++r){var a,o=dt.charCodeAt(vt);if(a=o>=97?o-97+10:o>=65?o-65+10:o>=48&&57>=o?o-48:1/0,a>=t)break;++vt,i=i*t+a}return vt===n||null!=e&&vt-n!==e?null:i}function C(){vt+=2;var t=b(16);return null==t&&n(mt+2,"Expected hexadecimal number"),Ye(dt.charCodeAt(vt))&&n(vt,"Identifier directly after number"),a(Et,t)}function S(t){var e=vt,i=!1,r=48===dt.charCodeAt(vt);t||null!==b(10)||n(e,"Invalid number"),46===dt.charCodeAt(vt)&&(++vt,b(10),i=!0);var s=dt.charCodeAt(vt);(69===s||101===s)&&(s=dt.charCodeAt(++vt),(43===s||45===s)&&++vt,null===b(10)&&n(e,"Invalid number"),i=!0),Ye(dt.charCodeAt(vt))&&n(vt,"Identifier directly after number");var o,h=dt.slice(e,vt);return i?o=parseFloat(h):r&&1!==h.length?/[89]/.test(h)||Tt?n(e,"Invalid number"):o=parseInt(h,8):o=parseInt(h,10),a(Et,o)}function P(t){vt++;for(var e="";;){vt>=ft&&n(mt,"Unterminated string constant");var i=dt.charCodeAt(vt);if(i===t)return++vt,a(jt,e);if(92===i){i=dt.charCodeAt(++vt);var r=/^[0-7]+/.exec(dt.slice(vt,vt+3));for(r&&(r=r[0]);r&&parseInt(r,8)>255;)r=r.slice(0,r.length-1);if("0"===r&&(r=null),++vt,r)Tt&&n(vt-2,"Octal literal in strict mode"),e+=String.fromCharCode(parseInt(r,8)),vt+=r.length-1;else switch(i){case 110:e+="\n";break;case 114:e+="\r";break;case 120:e+=String.fromCharCode(k(2));break;case 117:e+=String.fromCharCode(k(4));break;case 85:e+=String.fromCharCode(k(8));break;case 116:e+=" ";break;case 98:e+="\b";break;case 118:e+=" ";break;case 102:e+="\f";break;case 48:e+="\x00";break;case 13:10===dt.charCodeAt(vt)&&++vt;case 10:ct.locations&&(kt=vt,++Pt);break;default:e+=String.fromCharCode(i)}}else(13===i||10===i||8232===i||8233===i)&&n(mt,"Unterminated string constant"),e+=String.fromCharCode(i),++vt}}function k(t){var e=b(16,t);return null===e&&n(mt,"Bad character escape sequence"),e}function M(){De=!1;for(var t,e=!0,i=vt;;){var r=dt.charCodeAt(vt);if(Ke(r))De&&(t+=dt.charAt(vt)),++vt;else{if(92!==r)break;De||(t=dt.slice(i,vt)),De=!0,117!=dt.charCodeAt(++vt)&&n(vt,"Expecting Unicode escape sequence \\uXXXX"),++vt;var s=k(4),a=String.fromCharCode(s);a||n(vt-1,"Invalid Unicode escape"),(e?Ye(s):Ke(s))||n(vt-4,"Invalid Unicode escape"),t+=a}e=!1}return De?t:dt.slice(i,vt)}function I(){var t=M(),e=Bt;return De||(Ze(t)?e=le[t]:(ct.forbidReserved&&(3===ct.ecmaVersion?Re:Fe)(t)||Tt&&qe(t))&&n(mt,"The keyword '"+t+"' is reserved")),a(e,t)}function z(){Mt=mt,It=yt,zt=xt,y()}function A(t){if(Tt=t,vt=It,ct.locations)for(;kt>vt;)kt=dt.lastIndexOf("\n",kt-2)+1, ---Pt;u(),y()}function O(){this.type=null,this.start=mt,this.end=null}function T(){this.start=wt,this.end=null,null!==_t&&(this.source=_t)}function L(){var t=new O;return ct.locations&&(t.loc=new T),ct.ranges&&(t.range=[mt,0]),t}function E(t){var e=new O;return e.start=t.start,ct.locations&&(e.loc=new T,e.loc.start=t.loc.start),ct.ranges&&(e.range=[t.range[0],0]),e}function N(t,e){return t.type=e,t.end=It,ct.locations&&(t.loc.end=zt),ct.ranges&&(t.range[1]=It),t}function j(t){return ct.ecmaVersion>=5&&"ExpressionStatement"===t.type&&"Literal"===t.expression.type&&"use strict"===t.expression.value}function B(t){return bt===t?(z(),!0):void 0}function D(){return!ct.strictSemicolons&&(bt===Dt||bt===_e||Xe.test(dt.slice(It,mt)))}function R(){B(me)||D()||q()}function F(t){bt===t?z():q()}function q(){n(mt,"Unexpected token")}function V(t){"Identifier"!==t.type&&"MemberExpression"!==t.type&&n(t.start,"Assigning to rvalue"),Tt&&"Identifier"===t.type&&Ve(t.name)&&n(t.start,"Assigning to "+t.name+" in strict mode")}function Z(t){Mt=It=vt,ct.locations&&(zt=new r),At=Tt=null,Ot=[],y();var e=t||L(),n=!0;for(t||(e.body=[]);bt!==Dt;){var i=H();e.body.push(i),n&&j(i)&&A(!0),n=!1}return N(e,"Program")}function H(){(bt===be||bt===Se&&"/="==Ct)&&y(!0);var t=bt,e=L();switch(t){case Rt:case Vt:z();var i=t===Rt;B(me)||D()?e.label=null:bt!==Bt?q():(e.label=lt(),R());for(var r=0;re){var r=E(t);r.left=t,r.operator=Ct,z(),r.right=tt(et(),i,n);var s=N(r,/&&|\|\|/.test(r.operator)?"LogicalExpression":"BinaryExpression");return tt(s,e,n)}return t}function et(){if(bt.prefix){var t=L(),e=bt.isUpdate;return t.operator=Ct,t.prefix=!0,St=!0,z(),t.argument=et(),e?V(t.argument):Tt&&"delete"===t.operator&&"Identifier"===t.argument.type&&n(t.start,"Deleting local variable in strict mode"),N(t,e?"UpdateExpression":"UnaryExpression")}for(var i=nt();bt.postfix&&!D();){var t=E(i);t.operator=Ct,t.prefix=!1,t.argument=i,V(i),z(),i=N(t,"UpdateExpression")}return i}function nt(){return it(rt())}function it(t,e){if(B(we)){var n=E(t);return n.object=t,n.property=lt(!0),n.computed=!1,it(N(n,"MemberExpression"),e)}if(B(ce)){var n=E(t);return n.object=t,n.property=J(),n.computed=!0,F(de),it(N(n,"MemberExpression"),e)}if(!e&&B(ge)){var n=E(t);return n.callee=t,n.arguments=ut(pe,!1),it(N(n,"CallExpression"),e)}return t}function rt(){switch(bt){case se:var t=L();return z(),N(t,"ThisExpression");case Bt:return lt();case Et:case jt:case Nt:var t=L();return t.value=Ct,t.raw=dt.slice(mt,yt),z(),N(t,"Literal");case ae:case oe:case he:var t=L();return t.value=bt.atomValue,t.raw=bt.keyword,z(),N(t,"Literal");case ge:var e=wt,n=mt;z();var i=J();return i.start=n,i.end=yt,ct.locations&&(i.loc.start=e,i.loc.end=xt),ct.ranges&&(i.range=[n,yt]),F(pe),i;case ce:var t=L();return z(),t.elements=ut(de,!0,!0),N(t,"ArrayExpression");case fe:return at();case Xt:var t=L();return z(),ht(t,!1);case re:return st();default:q()}}function st(){var t=L();return z(),t.callee=it(rt(),!0),t.arguments=B(ge)?ut(pe,!1):Lt,N(t,"NewExpression")}function at(){var t=L(),e=!0,i=!1;for(t.properties=[],z();!B(_e);){if(e)e=!1;else if(F(ve),ct.allowTrailingCommas&&B(_e))break;var r,s={key:ot()},a=!1;if(B(ye)?(s.value=J(!0),r=s.kind="init"):ct.ecmaVersion>=5&&"Identifier"===s.key.type&&("get"===s.key.name||"set"===s.key.name)?(a=i=!0,r=s.kind=s.key.name,s.key=ot(),bt!==ge&&q(),s.value=ht(L(),!1)):q(),"Identifier"===s.key.type&&(Tt||i))for(var o=0;oa?t.id:t.params[a];if((qe(o.name)||Ve(o.name))&&n(o.start,"Defining '"+o.name+"' in strict mode"),a>=0)for(var h=0;a>h;++h)o.name===t.params[h].name&&n(o.start,"Argument name clash in strict mode")}return N(t,e?"FunctionDeclaration":"FunctionExpression")}function ut(t,e,n){for(var i=[],r=!0;!B(t);){if(r)r=!1;else if(F(ve),e&&ct.allowTrailingCommas&&B(t))break;n&&bt===ve?i.push(null):i.push(J(!0))}return i}function lt(t){var e=L();return e.name=bt===Bt?Ct:t&&!ct.forbidReserved&&bt.keyword||q(),St=!1,z(),N(e,"Identifier")}t.version="0.4.0";var ct,dt,ft,_t;t.parse=function(t,n){return dt=t+"",ft=dt.length,e(n),s(),Z(ct.program)};var gt=t.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},pt=t.getLineInfo=function(t,e){for(var n=1,i=0;;){Je.lastIndex=i;var r=Je.exec(t);if(!(r&&r.indext?36===t:91>t?!0:97>t?95===t:123>t?!0:t>=170&&Ge.test(String.fromCharCode(t))},Ke=t.isIdentifierChar=function(t){return 48>t?36===t:58>t?!0:65>t?!1:91>t?!0:97>t?95===t:123>t?!0:t>=170&&$e.test(String.fromCharCode(t))},Qe={kind:"loop"},tn={kind:"switch"}});var g={"+":"__add","-":"__subtract","*":"__multiply","/":"__divide","%":"__modulo","==":"equals","!=":"equals"},p={"-":"__negate","+":null},v=e.each(["add","subtract","multiply","divide","modulo","negate"],function(t){this["__"+t]="#"+t},{});return h.inject(v),l.inject(v),j.inject(v),"complete"===document.readyState?setTimeout(u):q.add(window,{load:u}),{compile:s,execute:a,load:c,parse:r}}.call(this),paper=new(i.inject(e.exports,{enumerable:!0,Base:e,Numerical:a,Key:W})),"function"==typeof define&&define.amd?define("paper",paper):"object"==typeof module&&module&&(module.exports=paper),paper}; \ No newline at end of file +var paper=new function(t){var e=new function(){function n(t,n,i,r,a){function o(s,o){o=o||(o=u(n,s))&&(o.get?o:o.value),"string"==typeof o&&"#"===o[0]&&(o=t[o.substring(1)]||o);var l,d="function"==typeof o,f=o,g=a||d&&!o.base?o&&o.get?s in t:t[s]:null;a&&g||(d&&g&&(o.base=g),d&&r!==!1&&(l=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(h[l[3].toLowerCase()+l[4]]=l[2]),f&&!d&&f.get&&"function"==typeof f.get&&e.isPlainObject(f)||(f={value:f,writable:!0}),(u(t,s)||{configurable:!0}).configurable&&(f.configurable=!0,f.enumerable=i),c(t,s,f))}var h={};if(n){for(var l in n)n.hasOwnProperty(l)&&!s.test(l)&&o(l);for(var l in h){var d=h[l],f=t["set"+d],g=t["get"+d]||f&&t["is"+d];!g||r!==!0&&0!==g.length||o(l,{get:g,set:f})}}return t}function i(t,e,n){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?a:o).call(t,e,n=n||t),n}function r(t,e,n){for(var i in e)!e.hasOwnProperty(i)||n&&n[i]||(t[i]=e[i]);return t}var s=/^(statics|enumerable|beans|preserve)$/,a=[].forEach||function(t,e){for(var n=0,i=this.length;i>n;n++)t.call(e,this[n],n,this)},o=function(t,e){for(var n in this)this.hasOwnProperty(n)&&t.call(e,this[n],n,this)},h=Object.create||function(t){return{__proto__:t}},u=Object.getOwnPropertyDescriptor||function(t,e){var n=t.__lookupGetter__&&t.__lookupGetter__(e);return n?{get:n,set:t.__lookupSetter__(e),enumerable:!0,configurable:!0}:t.hasOwnProperty(e)?{value:t[e],enumerable:!0,configurable:!0,writable:!0}:null},l=Object.defineProperty||function(t,e,n){return(n.get||n.set)&&t.__defineGetter__?(n.get&&t.__defineGetter__(e,n.get),n.set&&t.__defineSetter__(e,n.set)):t[e]=n.value,t},c=function(t,e,n){return delete t[e],l(t,e,n)};return n(function(){for(var t=0,e=arguments.length;e>t;t++)r(this,arguments[t])},{inject:function(t){if(t){var e=t.statics===!0?t:t.statics,i=t.beans,r=t.preserve;e!==t&&n(this.prototype,t,t.enumerable,i,r),n(this,e,!0,i,r)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e,i=this,r=0,s=arguments.length;s>r&&!(t=arguments[r].initialize);r++);return t=t||function(){i.apply(this,arguments)},e=t.prototype=h(this.prototype),c(e,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length&&this.inject.apply(t,arguments),t.base=i,t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++){var i=arguments[t];i&&n(this,i,i.enumerable,i.beans,i.preserve)}return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},set:function(t){return r(this,t)},clone:function(){return new this.constructor(this)},statics:{each:i,create:h,define:c,describe:u,set:r,clone:function(t){return r(new t.constructor,t)},isPlainObject:function(t){var n=null!=t&&t.constructor;return n&&(n===Object||n===e||"Object"===n.name)},pick:function(e,n){return e!==t?e:n}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({toString:function(){return null!=this._id?(this._class||"Object")+(this._name?" '"+this._name+"'":" @"+this._id):"{ "+e.each(this,function(t,e){if(!/^_/.test(e)){var n=typeof t;this.push(e+": "+("number"===n?s.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},getClassName:function(){return this._class||""},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i,r){if(n&&(r||e.isPlainObject(n))){for(var s=Object.keys(n._filtering||n),a=0,o=s.length;o>a;a++){var h=s[a];if(!i||!i[h]){var u=n[h];u!==t&&(this[h]=u)}}return!0}},statics:{exports:{enumerable:!0},extend:function et(){var t=et.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(t&&n&&"object"==typeof t&&"object"==typeof n){if(Array.isArray(t)&&Array.isArray(n)){var i=t.length;if(i!==n.length)return!1;for(;i--;)if(!e.equals(t[i],n[i]))return!1}else{var r=Object.keys(t),i=r.length;if(i!==Object.keys(n).length)return!1;for(;i--;){var s=r[i];if(!n.hasOwnProperty(s)||!e.equals(t[s],n[s]))return!1}}return!0}return!1},read:function(n,i,r,s){if(this===e){var a=this.peek(n,i);return n.__index++,a}var o=this.prototype,h=o._readIndex,u=i||h&&n.__index||0;s||(s=n.length-u);var l=n[u];return l instanceof this||r&&r.readNull&&null==l&&1>=s?(h&&(n.__index=u+1),l&&r&&r.clone?l.clone():l):(l=e.create(this.prototype),h&&(l.__read=!0),l=l.initialize.apply(l,u>0||ss;s++)r.push(Array.isArray(i=t[s])?this.read(i,0,n):this.read(t,s,n,1));return r},readNamed:function(n,i,r,s,a){var o=this.getNamed(n,i),h=o!==t;if(h){var u=n._filtered;u||(u=n._filtered=e.create(n[0]),u._filtering=n[0]),u[i]=t}return this.read(h?[o]:n,r,s,a)},getNamed:function(n,i){var r=n[0];return n._hasObject===t&&(n._hasObject=1===n.length&&e.isPlainObject(r)),n._hasObject?i?r[i]:n._filtered||r:t},hasNamed:function(t,e){return!!this.getNamed(t,e)},isPlainValue:function(t,e){return this.isPlainObject(t)||Array.isArray(t)||e&&"string"==typeof t},serialize:function(t,n,i,r){n=n||{};var a,o=!r;if(o&&(n.formatter=new s(n.precision),r={length:0,definitions:{},references:{},add:function(t,e){var n="#"+t._id,i=this.references[n];if(!i){this.length++;var r=e.call(t),s=t._class;s&&r[0]!==s&&r.unshift(s),this.definitions[n]=r,i=this.references[n]=[n]}return i}}),t&&t._serialize){a=t._serialize(n,r);var h=t._class;!h||i||a._compact||a[0]===h||a.unshift(h)}else if(Array.isArray(t)){a=[];for(var u=0,l=t.length;l>u;u++)a[u]=e.serialize(t[u],n,i,r);i&&(a._compact=!0)}else if(e.isPlainObject(t)){a={};for(var c=Object.keys(t),u=0,l=c.length;l>u;u++){var d=c[u];a[d]=e.serialize(t[d],n,i,r)}}else a="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],a]:a},deserialize:function(t,n,i,r){var s=t,a=!i;if(i=i||{},Array.isArray(t)){var o=t[0],h="dictionary"===o;if(1==t.length&&/^#/.test(o))return i.dictionary[o];o=e.exports[o],s=[],r&&(i.dictionary=s);for(var u=o?1:0,l=t.length;l>u;u++)s.push(e.deserialize(t[u],n,i,h));if(o){var c=s;n?s=n(o,c):(s=e.create(o.prototype),o.apply(s,c))}}else if(e.isPlainObject(t)){s={},r&&(i.dictionary=s);for(var d in t)s[d]=e.deserialize(t[d],n,i)}return a&&t&&t.length&&"dictionary"===t[0][0]?s[1]:s},exportJSON:function(t,n){var i=e.serialize(t,n);return n&&n.asString===!1?i:JSON.stringify(i)},importJSON:function(t,n){return e.deserialize("string"==typeof t?JSON.parse(t):t,function(t,i){var r=n&&n.constructor===t?n:e.create(t.prototype),s=r===n;if(1===i.length&&r instanceof m&&(s||!(r instanceof w))){var a=i[0];e.isPlainObject(a)&&(a.insert=!1)}return t.apply(r,i),s&&(n=null),r})},splice:function(e,n,i,r){var s=n&&n.length,a=i===t;i=a?e.length:i,i>e.length&&(i=e.length);for(var o=0;s>o;o++)n[o]._index=i+o;if(a)return e.push.apply(e,n),[];var h=[i,r];n&&h.push.apply(h,n);for(var u=e.splice.apply(e,h),o=0,l=u.length;l>o;o++)u[o]._index=t;for(var o=i+s,l=e.length;l>o;o++)e[o]._index=o;return u},capitalize:function(t){return t.replace(/\b[a-z]/g,function(t){return t.toUpperCase()})},camelize:function(t){return t.replace(/-(.)/g,function(t,e){return e.toUpperCase()})},hyphenate:function(t){return t.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()}}});var n={on:function(t,n){if("string"!=typeof t)e.each(t,function(t,e){this.on(e,t)},this);else{var i=this._eventTypes,r=i&&i[t],s=this._callbacks=this._callbacks||{};s=s[t]=s[t]||[],-1===s.indexOf(n)&&(s.push(n),r&&r.install&&1===s.length&&r.install.call(this,t))}return this},off:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.off(e,t)},this),t;var r,s=this._eventTypes,a=s&&s[n],o=this._callbacks&&this._callbacks[n];return o&&(!i||-1!==(r=o.indexOf(i))&&1===o.length?(a&&a.uninstall&&a.uninstall.call(this,n),delete this._callbacks[n]):-1!==r&&o.splice(r,1)),this},once:function(t,e){return this.on(t,function(){e.apply(this,arguments),this.off(t,e)})},emit:function(t,e){var n=this._callbacks&&this._callbacks[t];if(!n)return!1;var i=[].slice.call(arguments,1);n=n.slice();for(var r=0,s=n.length;s>r;r++)if(n[r].apply(this,i)===!1){e&&e.stop&&e.stop();break}return!0},responds:function(t){return!(!this._callbacks||!this._callbacks[t])},attach:"#on",detach:"#off",fire:"#emit",_installEvents:function(t){var e=this._callbacks,n=t?"install":"uninstall";for(var i in e)if(e[i].length>0){var r=this._eventTypes,s=r&&r[i],a=s&&s[n];a&&a.call(this,i)}},statics:{inject:function nt(t){var n=t._events;if(n){var i={};e.each(n,function(n,r){var s="string"==typeof n,a=s?n:r,o=e.capitalize(a),h=a.substring(2).toLowerCase();i[h]=s?{}:n,a="_"+a,t["get"+o]=function(){return this[a]},t["set"+o]=function(t){var e=this[a];e&&this.off(h,e),t&&this.on(h,t),this[a]=t}}),t._eventTypes=i}return nt.base.apply(this,arguments)}}},i=e.extend({_class:"PaperScope",initialize:function it(){paper=this,this.settings=new e({applyMatrix:!0,handleSize:4,hitTolerance:0}),this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=it._id++,it._scopes[this._id]=this;var t=it.prototype;if(!this.support){var n=Y.getContext(1,1);t.support={nativeDash:"setLineDash"in n||"mozDash"in n,nativeBlendModes:K.nativeModes},Y.release(n)}if(!this.browser){var i=navigator.userAgent.toLowerCase(),r=(/(win)/.exec(i)||/(mac)/.exec(i)||/(linux)/.exec(i)||[])[0],s=t.browser={platform:r};r&&(s[r]=!0),i.replace(/(opera|chrome|safari|webkit|firefox|msie|trident|atom)\/?\s*([.\d]+)(?:.*version\/([.\d]+))?(?:.*rv\:([.\d]+))?/g,function(t,e,n,i,r){if(!s.chrome){var a="opera"===e?i:n;"trident"===e&&(a=r,e="msie"),s.version=a,s.versionNumber=parseFloat(a),s.name=e,s[e]=!0}}),s.chrome&&delete s.webkit,s.atom&&delete s.chrome}},version:"0.9.25",getView:function(){return this.project&&this.project.getView()},getPaper:function(){return this},execute:function(t,e,n){paper.PaperScript.execute(t,this,e,n),V.updateFocus()},install:function(t){var n=this;e.each(["project","view","tool"],function(i){e.define(t,i,{configurable:!0,get:function(){return n[i]}})});for(var i in this)!/^_/.test(i)&&this[i]&&(t[i]=this[i])},setup:function(t){return paper=this,this.project=new v(t),this},activate:function(){paper=this},clear:function(){for(var t=this.projects.length-1;t>=0;t--)this.projects[t].remove();for(var t=this.tools.length-1;t>=0;t--)this.tools[t].remove();for(var t=this.palettes.length-1;t>=0;t--)this.palettes[t].remove()},remove:function(){this.clear(),delete i._scopes[this._id]},statics:new function(){function t(t){return t+="Attribute",function(e,n){return e[t](n)||e[t]("data-paper-"+n)}}return{_scopes:{},_id:0,get:function(t){return this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),r=e.extend(n,{initialize:function(t){this._scope=paper,this._index=this._scope[this._list].push(this)-1,(t||!this._scope[this._reference])&&this.activate()},activate:function(){if(!this._scope)return!1;var t=this._scope[this._reference];return t&&t!==this&&t.emit("deactivate"),this._scope[this._reference]=this,this.emit("activate",t),!0},isActive:function(){return this._scope[this._reference]===this},remove:function(){return null==this._index?!1:(e.splice(this._scope[this._list],null,this._index,1),this._scope[this._reference]==this&&(this._scope[this._reference]=null),this._scope=null,!0)}}),s=e.extend({initialize:function(t){this.precision=t||5,this.multiplier=Math.pow(10,this.precision)},number:function(t){return Math.round(t*this.multiplier)/this.multiplier},pair:function(t,e,n){return this.number(t)+(n||",")+this.number(e)},point:function(t,e){return this.number(t.x)+(e||",")+this.number(t.y)},size:function(t,e){return this.number(t.width)+(e||",")+this.number(t.height)},rectangle:function(t,e){return this.point(t,e)+(e||",")+this.size(t,e)}});s.instance=new s;var a=new function(){function t(t,e,n){return e>t?e:t>n?n:t}var e=[[.5773502691896257],[0,.7745966692414834],[.33998104358485626,.8611363115940526],[0,.5384693101056831,.906179845938664],[.2386191860831969,.6612093864662645,.932469514203152],[0,.4058451513773972,.7415311855993945,.9491079123427585],[.1834346424956498,.525532409916329,.7966664774136267,.9602898564975363],[0,.3242534234038089,.6133714327005904,.8360311073266358,.9681602395076261],[.14887433898163122,.4333953941292472,.6794095682990244,.8650633666889845,.9739065285171717],[0,.26954315595234496,.5190961292068118,.7301520055740494,.8870625997680953,.978228658146057],[.1252334085114689,.3678314989981802,.5873179542866175,.7699026741943047,.9041172563704749,.9815606342467192],[0,.2304583159551348,.44849275103644687,.6423493394403402,.8015780907333099,.9175983992229779,.9841830547185881],[.10805494870734367,.31911236892788974,.5152486363581541,.6872929048116855,.827201315069765,.9284348836635735,.9862838086968123],[0,.20119409399743451,.3941513470775634,.5709721726085388,.7244177313601701,.8482065834104272,.937273392400706,.9879925180204854],[.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]],n=[[1],[.8888888888888888,.5555555555555556],[.6521451548625461,.34785484513745385],[.5688888888888889,.47862867049936647,.23692688505618908],[.46791393457269104,.3607615730481386,.17132449237917036],[.4179591836734694,.3818300505051189,.27970539148927664,.1294849661688697],[.362683783378362,.31370664587788727,.22238103445337448,.10122853629037626],[.3302393550012598,.31234707704000286,.26061069640293544,.1806481606948574,.08127438836157441],[.29552422471475287,.26926671930999635,.21908636251598204,.1494513491505806,.06667134430868814],[.2729250867779006,.26280454451024665,.23319376459199048,.18629021092773426,.1255803694649046,.05566856711617366],[.24914704581340277,.2334925365383548,.20316742672306592,.16007832854334622,.10693932599531843,.04717533638651183],[.2325515532308739,.22628318026289723,.2078160475368885,.17814598076194574,.13887351021978725,.09212149983772845,.04048400476531588],[.2152638534631578,.2051984637212956,.18553839747793782,.15720316715819355,.12151857068790319,.08015808715976021,.03511946033175186],[.2025782419255613,.19843148532711158,.1861610000155622,.16626920581699392,.13957067792615432,.10715922046717194,.07036604748810812,.03075324199611727],[.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096]],i=Math.abs,r=Math.sqrt,s=Math.pow,o=1e-12,h=1.12e-16;return{TOLERANCE:1e-6,EPSILON:o,MACHINE_EPSILON:h,CURVETIME_EPSILON:4e-7,GEOMETRIC_EPSILON:2e-7,WINDING_EPSILON:2e-7,TRIGONOMETRIC_EPSILON:1e-7,CLIPPING_EPSILON:1e-7,KAPPA:4*(r(2)-1)/3,isZero:function(t){return t>=-o&&o>=t},integrate:function(t,i,r,s){for(var a=e[s-2],o=n[s-2],h=.5*(r-i),u=h+i,l=0,c=s+1>>1,d=1&s?o[l++]*t(u):0;c>l;){var f=h*a[l];d+=o[l++]*(t(u+f)+t(u-f))}return h*d},findRoot:function(t,e,n,r,s,a,o){for(var h=0;a>h;h++){var u=t(n),l=u/e(n),c=n-l;if(i(l)0?(s=n,n=r>=c?.5*(r+s):c):(r=n,n=c>=s?.5*(r+s):c)}return n},solveQuadratic:function(e,n,a,u,l,c){var d,f,g=0,_=l-o,v=c+o,p=1/0,m=n;if(n/=-2,f=n*n-e*a,0!==f&&i(f)y){var w=s(10,i(Math.floor(Math.log(y)*Math.LOG10E)));isFinite(w)||(w=0),e*=w,n*=w,a*=w,f=n*n-e*a}}if(i(e)=-h){var x=0>f?0:r(f),b=n+(0>n?-x:x);0===b?(d=a/e,p=-d):(d=b/e,p=a/b)}return isFinite(d)&&(null==l||d>_&&v>d)&&(u[g++]=null==l?d:t(d,l,c)),p!==d&&isFinite(p)&&(null==l||p>_&&v>p)&&(u[g++]=null==l?p:t(p,l,c)),g},solveCubic:function(e,n,u,l,c,d,f){var g,_,v,p=0;if(i(e)x?-1:1,x=-w/e,b=x>0?1.3247179572*Math.max(b,r(x)):b,m=g-C*b,m!==g){do if(g=m,S=e*g,_=S+n,v=_*g+u,w=(S+_)*g+v,y=v*g+l,m=0===w?g:g-y/w/P,m===g){g=m;break}while(C*m>C*g);i(e)*g*g>i(l/g)&&(v=-l/g,_=(v-u)/g)}}var p=a.solveQuadratic(e,_,v,c,d,f);return isFinite(g)&&(0===p||g!==c[p-1])&&(null==d||g>d-o&&f+o>g)&&(c[p++]=null==d?g:t(g,d,f)),p}}},o={_id:1,_pools:{},get:function(t){if(t){var e=t._class,n=this._pools[e];return n||(n=this._pools[e]={_id:1}),n._id++}return this._id++}},h=e.extend({_class:"Point",_readIndex:!0,initialize:function(t,e){var n=typeof t;if("number"===n){var i="number"==typeof e;this.x=t,this.y=i?e:t,this.__read&&(this.__read=i?2:1)}else"undefined"===n||null===t?(this.x=this.y=0,this.__read&&(this.__read=null===t?1:0)):(Array.isArray(t)?(this.x=t[0],this.y=t.length>1?t[1]:t[0]):null!=t.x?(this.x=t.x,this.y=t.y):null!=t.width?(this.x=t.width,this.y=t.height):null!=t.angle?(this.x=t.length,this.y=0,this.setAngle(t.angle)):(this.x=this.y=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.x=t,this.y=e,this},equals:function(t){return this===t||t&&(this.x===t.x&&this.y===t.y||Array.isArray(t)&&this.x===t[0]&&this.y===t[1])||!1},clone:function(){return new h(this.x,this.y)},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y)]},getLength:function(){return Math.sqrt(this.x*this.x+this.y*this.y)},setLength:function(t){if(this.isZero()){var e=this._angle||0;this.set(Math.cos(e)*t,Math.sin(e)*t)}else{var n=t/this.getLength();a.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}},getAngle:function(){return 180*this.getAngleInRadians.apply(this,arguments)/Math.PI},setAngle:function(t){this.setAngleInRadians.call(this,t*Math.PI/180)},getAngleInDegrees:"#getAngle",setAngleInDegrees:"#setAngle",getAngleInRadians:function(){if(arguments.length){var t=h.read(arguments),e=this.getLength()*t.getLength();if(a.isZero(e))return NaN;var n=this.dot(t)/e;return Math.acos(-1>n?-1:n>1?1:n)}return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x)},setAngleInRadians:function(t){if(this._angle=t,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3}},{beans:!1,getDirectedAngle:function(){var t=h.read(arguments);return 180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},getDistance:function(){var t=h.read(arguments),n=t.x-this.x,i=t.y-this.y,r=n*n+i*i,s=e.read(arguments);return s?r:Math.sqrt(r)},normalize:function(e){e===t&&(e=1);var n=this.getLength(),i=0!==n?e/n:0,r=new h(this.x*i,this.y*i);return i>=0&&(r._angle=this._angle),r},rotate:function(t,e){if(0===t)return this.clone();t=t*Math.PI/180;var n=e?this.subtract(e):this,i=Math.sin(t),r=Math.cos(t);return n=new h(n.x*r-n.y*i,n.x*i+n.y*r),e?n.add(e):n},transform:function(t){return t?t._transformPoint(this):this},add:function(){var t=h.read(arguments);return new h(this.x+t.x,this.y+t.y)},subtract:function(){var t=h.read(arguments);return new h(this.x-t.x,this.y-t.y)},multiply:function(){var t=h.read(arguments);return new h(this.x*t.x,this.y*t.y)},divide:function(){var t=h.read(arguments);return new h(this.x/t.x,this.y/t.y)},modulo:function(){var t=h.read(arguments);return new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},isInside:function(){return d.read(arguments).contains(this)},isClose:function(){var t=h.read(arguments),n=e.read(arguments);return this.getDistance(t)1?t[1]:t[0]):null!=t.width?(this.width=t.width,this.height=t.height):null!=t.x?(this.width=t.x,this.height=t.y):(this.width=this.height=0,this.__read&&(this.__read=0)),this.__read&&(this.__read=1))},set:function(t,e){return this.width=t,this.height=e,this},equals:function(t){return t===this||t&&(this.width===t.width&&this.height===t.height||Array.isArray(t)&&this.width===t[0]&&this.height===t[1])||!1},clone:function(){return new l(this.width,this.height)},toString:function(){var t=s.instance;return"{ width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.width),e.number(this.height)]},add:function(){var t=l.read(arguments);return new l(this.width+t.width,this.height+t.height)},subtract:function(){var t=l.read(arguments);return new l(this.width-t.width,this.height-t.height)},multiply:function(){var t=l.read(arguments);return new l(this.width*t.width,this.height*t.height)},divide:function(){var t=l.read(arguments);return new l(this.width/t.width,this.height/t.height)},modulo:function(){var t=l.read(arguments);return new l(this.width%t.width,this.height%t.height)},negate:function(){return new l(-this.width,-this.height)},isZero:function(){return a.isZero(this.width)&&a.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new l(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new l(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new l(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new l(e(this.width),e(this.height))}},{})),c=l.extend({initialize:function(t,e,n,i){this._width=t,this._height=e,this._owner=n,this._setter=i},set:function(t,e,n){return this._width=t,this._height=e,n||this._owner[this._setter](this),this},getWidth:function(){return this._width},setWidth:function(t){this._width=t,this._owner[this._setter](this)},getHeight:function(){return this._height},setHeight:function(t){this._height=t,this._owner[this._setter](this)}}),d=e.extend({_class:"Rectangle",_readIndex:!0,beans:!0,initialize:function(n,i,r,s){var a=typeof n,o=0;if("number"===a?(this.x=n,this.y=i,this.width=r,this.height=s,o=4):"undefined"===a||null===n?(this.x=this.y=this.width=this.height=0,o=null===n?1:0):1===arguments.length&&(Array.isArray(n)?(this.x=n[0],this.y=n[1],this.width=n[2],this.height=n[3],o=1):n.x!==t||n.width!==t?(this.x=n.x||0,this.y=n.y||0,this.width=n.width||0,this.height=n.height||0,o=1):n.from===t&&n.to===t&&(this.x=this.y=this.width=this.height=0,this._set(n),o=1)),!o){var u=h.readNamed(arguments,"from"),c=e.peek(arguments);if(this.x=u.x,this.y=u.y,c&&c.x!==t||e.hasNamed(arguments,"to")){var d=h.readNamed(arguments,"to");this.width=d.x-u.x,this.height=d.y-u.y,this.width<0&&(this.x=d.x,this.width=-this.width),this.height<0&&(this.y=d.y,this.height=-this.height)}else{var f=l.read(arguments);this.width=f.width,this.height=f.height}o=arguments.__index}this.__read&&(this.__read=o)},set:function(t,e,n,i){return this.x=t,this.y=e,this.width=n,this.height=i,this},clone:function(){return new d(this.x,this.y,this.width,this.height)},equals:function(t){var n=e.isPlainValue(t)?d.read(arguments):t;return n===this||n&&this.x===n.x&&this.y===n.y&&this.width===n.width&&this.height===n.height||!1},toString:function(){var t=s.instance;return"{ x: "+t.number(this.x)+", y: "+t.number(this.y)+", width: "+t.number(this.width)+", height: "+t.number(this.height)+" }"},_serialize:function(t){var e=t.formatter;return[e.number(this.x),e.number(this.y),e.number(this.width),e.number(this.height)]},getPoint:function(t){var e=t?h:u;return new e(this.x,this.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.x=t.x,this.y=t.y},getSize:function(t){var e=t?l:c;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=l.read(arguments);this._fixX&&(this.x+=(this.width-t.width)*this._fixX),this._fixY&&(this.y+=(this.height-t.height)*this._fixY),this.width=t.width,this.height=t.height,this._fixW=1,this._fixH=1},getLeft:function(){return this.x},setLeft:function(t){this._fixW||(this.width-=t-this.x),this.x=t,this._fixX=0},getTop:function(){return this.y},setTop:function(t){this._fixH||(this.height-=t-this.y),this.y=t,this._fixY=0},getRight:function(){return this.x+this.width},setRight:function(e){this._fixX!==t&&1!==this._fixX&&(this._fixW=0),this._fixW?this.x=e-this.width:this.width=e-this.x,this._fixX=1},getBottom:function(){return this.y+this.height},setBottom:function(e){this._fixY!==t&&1!==this._fixY&&(this._fixH=0),this._fixH?this.y=e-this.height:this.height=e-this.y,this._fixY=1},getCenterX:function(){return this.x+.5*this.width},setCenterX:function(t){this.x=t-.5*this.width,this._fixX=.5},getCenterY:function(){return this.y+.5*this.height},setCenterY:function(t){this.y=t-.5*this.height,this._fixY=.5},getCenter:function(t){var e=t?h:u;return new e(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(){var t=h.read(arguments);return this.setCenterX(t.x),this.setCenterY(t.y),this},getArea:function(){return this.width*this.height},isEmpty:function(){return 0===this.width||0===this.height},contains:function(e){return e&&e.width!==t||4==(Array.isArray(e)?e:arguments).length?this._containsRectangle(d.read(arguments)):this._containsPoint(h.read(arguments))},_containsPoint:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e<=this.x+this.width&&n<=this.y+this.height},_containsRectangle:function(t){var e=t.x,n=t.y;return e>=this.x&&n>=this.y&&e+t.width<=this.x+this.width&&n+t.height<=this.y+this.height},intersects:function(){var t=d.read(arguments);return t.x+t.width>this.x&&t.y+t.height>this.y&&t.x=this.x&&t.y+t.height>=this.y&&t.x<=this.x+this.width&&t.y<=this.y+this.height},intersect:function(){var t=d.read(arguments),e=Math.max(this.x,t.x),n=Math.max(this.y,t.y),i=Math.min(this.x+this.width,t.x+t.width),r=Math.min(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},unite:function(){var t=d.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x+t.width),r=Math.max(this.y+this.height,t.y+t.height);return new d(e,n,i-e,r-n)},include:function(){var t=h.read(arguments),e=Math.min(this.x,t.x),n=Math.min(this.y,t.y),i=Math.max(this.x+this.width,t.x),r=Math.max(this.y+this.height,t.y);return new d(e,n,i-e,r-n)},expand:function(){var t=l.read(arguments),e=t.width,n=t.height;return new d(this.x-e/2,this.y-n/2,this.width+e,this.height+n)},scale:function(e,n){return this.expand(this.width*e-this.width,this.height*(n===t?e:n)-this.height)}},e.each([["Top","Left"],["Top","Right"],["Bottom","Left"],["Bottom","Right"],["Left","Center"],["Top","Center"],["Right","Center"],["Bottom","Center"]],function(t,e){var n=t.join(""),i=/^[RL]/.test(n);e>=4&&(t[1]+=i?"Y":"X");var r=t[i?0:1],s=t[i?1:0],a="get"+r,o="get"+s,l="set"+r,c="set"+s,d="get"+n,f="set"+n;this[d]=function(t){var e=t?h:u;return new e(this[a](),this[o](),this,f)},this[f]=function(){var t=h.read(arguments);this[l](t.x),this[c](t.y)}},{beans:!0})),f=d.extend({initialize:function(t,e,n,i,r,s){this.set(t,e,n,i,!0),this._owner=r,this._setter=s},set:function(t,e,n,i,r){return this._x=t,this._y=e,this._width=n,this._height=i,r||this._owner[this._setter](this),this}},new function(){var t=d.prototype;return e.each(["x","y","width","height"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._dontNotify||this._owner[this._setter](this)}},e.each(["Point","Size","Center","Left","Top","Right","Bottom","CenterX","CenterY","TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],function(e){var n="set"+e;this[n]=function(){this._dontNotify=!0,t[n].apply(this,arguments),this._dontNotify=!1,this._owner[this._setter](this)}},{isSelected:function(){return this._owner._boundsSelected},setSelected:function(t){var e=this._owner;e.setSelected&&(e._boundsSelected=t,e.setSelected(t||e._selectedSegmentState>0))}}))}),g=e.extend({_class:"Matrix",initialize:function rt(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof rt?this.set(t._a,t._c,t._b,t._d,t._tx,t._ty):Array.isArray(t)?this.set.apply(this,t):n=!1:0===e?this.reset():n=!1,!n)throw Error("Unsupported matrix parameters")},set:function(t,e,n,i,r,s,a){return this._a=t,this._c=e,this._b=n,this._d=i,this._tx=r,this._ty=s,a||this._changed(),this},_serialize:function(t){return e.serialize(this.getValues(),t)},_changed:function(){var t=this._owner;t&&(t._applyMatrix?t.transform(null,!0):t._changed(9))},clone:function(){return new g(this._a,this._c,this._b,this._d,this._tx,this._ty)},equals:function(t){return t===this||t&&this._a===t._a&&this._b===t._b&&this._c===t._c&&this._d===t._d&&this._tx===t._tx&&this._ty===t._ty||!1},toString:function(){var t=s.instance;return"[["+[t.number(this._a),t.number(this._b),t.number(this._tx)].join(", ")+"], ["+[t.number(this._c),t.number(this._d),t.number(this._ty)].join(", ")+"]]"},reset:function(t){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,t||this._changed(),this},apply:function(t,n){var i=this._owner;return i?(i.transform(null,!0,e.pick(t,!0),n),this.isIdentity()):!1},translate:function(){var t=h.read(arguments),e=t.x,n=t.y;return this._tx+=e*this._a+n*this._b,this._ty+=e*this._c+n*this._d,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});return e&&this.translate(e),this._a*=t.x,this._c*=t.x,this._b*=t.y,this._d*=t.y,e&&this.translate(e.negate()),this._changed(),this},rotate:function(t){t*=Math.PI/180;var e=h.read(arguments,1),n=e.x,i=e.y,r=Math.cos(t),s=Math.sin(t),a=n-n*r+i*s,o=i-n*s-i*r,u=this._a,l=this._b,c=this._c,d=this._d;return this._a=r*u+s*l,this._b=-s*u+r*l,this._c=r*c+s*d,this._d=-s*c+r*d,this._tx+=a*u+o*l,this._ty+=a*c+o*d,this._changed(),this},shear:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0});e&&this.translate(e);var n=this._a,i=this._c;return this._a+=t.y*this._b,this._c+=t.y*this._d,this._b+=t.x*n,this._d+=t.x*i,e&&this.translate(e.negate()),this._changed(),this},skew:function(){var t=h.read(arguments),e=h.read(arguments,0,{readNull:!0}),n=Math.PI/180,i=new h(Math.tan(t.x*n),Math.tan(t.y*n));return this.shear(i,e)},concatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=t._a,a=t._b,o=t._c,h=t._d,u=t._tx,l=t._ty;return this._a=s*e+o*n, +this._b=a*e+h*n,this._c=s*i+o*r,this._d=a*i+h*r,this._tx+=u*e+l*n,this._ty+=u*i+l*r,this._changed(),this},preConcatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return this._a=o*e+h*i,this._b=o*n+h*r,this._c=u*e+l*i,this._d=u*n+l*r,this._tx=o*s+h*a+c,this._ty=u*s+l*a+d,this._changed(),this},chain:function(t){var e=this._a,n=this._b,i=this._c,r=this._d,s=this._tx,a=this._ty,o=t._a,h=t._b,u=t._c,l=t._d,c=t._tx,d=t._ty;return new g(o*e+u*n,o*i+u*r,h*e+l*n,h*i+l*r,s+c*e+d*n,a+c*i+d*r)},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},orNullIfIdentity:function(){return this.isIdentity()?null:this},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,n){return arguments.length<3?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,n)},_transformPoint:function(t,e,n){var i=t.x,r=t.y;return e||(e=new h),e.set(i*this._a+r*this._b+this._tx,i*this._c+r*this._d+this._ty,n)},_transformCoordinates:function(t,e,n){for(var i=0,r=0,s=2*n;s>i;){var a=t[i++],o=t[i++];e[r++]=a*this._a+o*this._b+this._tx,e[r++]=a*this._c+o*this._d+this._ty}return e},_transformCorners:function(t){var e=t.x,n=t.y,i=e+t.width,r=n+t.height,s=[e,n,i,n,i,r,e,r];return this._transformCoordinates(s,s,4)},_transformBounds:function(t,e,n){for(var i=this._transformCorners(t),r=i.slice(0,2),s=r.slice(),a=2;8>a;a++){var o=i[a],h=1&a;os[h]&&(s[h]=o)}return e||(e=new d),e.set(r[0],r[1],s[0]-r[0],s[1]-r[1],n)},inverseTransform:function(){return this._inverseTransform(h.read(arguments))},_getDeterminant:function(){var t=this._a*this._d-this._b*this._c;return isFinite(t)&&!a.isZero(t)&&isFinite(this._tx)&&isFinite(this._ty)?t:null},_inverseTransform:function(t,e,n){var i=this._getDeterminant();if(!i)return null;var r=t.x-this._tx,s=t.y-this._ty;return e||(e=new h),e.set((r*this._d-s*this._b)/i,(s*this._a-r*this._c)/i,n)},decompose:function(){var t=this._a,e=this._b,n=this._c,i=this._d;if(a.isZero(t*i-e*n))return null;var r=Math.sqrt(t*t+e*e);t/=r,e/=r;var s=t*n+e*i;n-=t*s,i-=e*s;var o=Math.sqrt(n*n+i*i);return n/=o,i/=o,s/=o,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,o),rotation:180*-Math.atan2(e,t)/Math.PI,shearing:s}},getValues:function(){return[this._a,this._c,this._b,this._d,this._tx,this._ty]},getTranslation:function(){return new h(this._tx,this._ty)},getScaling:function(){return(this.decompose()||{}).scaling},getRotation:function(){return(this.decompose()||{}).rotation},inverted:function(){var t=this._getDeterminant();return t&&new g(this._d/t,-this._c/t,-this._b/t,this._a/t,(this._b*this._ty-this._d*this._tx)/t,(this._c*this._tx-this._a*this._ty)/t)},shiftless:function(){return new g(this._a,this._c,this._b,this._d,0,0)},applyToContext:function(t){t.transform(this._a,this._c,this._b,this._d,this._tx,this._ty)}},e.each(["a","c","b","d","tx","ty"],function(t){var n=e.capitalize(t),i="_"+t;this["get"+n]=function(){return this[i]},this["set"+n]=function(t){this[i]=t,this._changed()}},{})),_=e.extend({_class:"Line",initialize:function(t,e,n,i,r){var s=!1;arguments.length>=4?(this._px=t,this._py=e,this._vx=n,this._vy=i,s=r):(this._px=t.x,this._py=t.y,this._vx=e.x,this._vy=e.y,s=n),s||(this._vx-=this._px,this._vy-=this._py)},getPoint:function(){return new h(this._px,this._py)},getVector:function(){return new h(this._vx,this._vy)},getLength:function(){return this.getVector().getLength()},intersect:function(t,e){return _.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t,e){return _.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0,e)},getDistance:function(t){return Math.abs(_.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},isCollinear:function(t){return h.isCollinear(this._vx,this._vy,t._vx,t._vy)},isOrthogonal:function(t){return h.isOrthogonal(this._vx,this._vy,t._vx,t._vy)},statics:{intersect:function(t,e,n,i,r,s,o,u,l,c){l||(n-=t,i-=e,o-=r,u-=s);var d=n*u-i*o;if(!a.isZero(d)){var f=t-r,g=e-s,_=(o*g-u*f)/d,v=(n*g-i*f)/d,p=1e-12,m=-p,y=1+p;if(c||_>m&&y>_&&v>m&&y>v)return c||(_=0>=_?0:_>=1?1:_),new h(t+_*n,e+_*i)}},getSide:function(t,e,n,i,r,s,a,o){a||(n-=t,i-=e);var h=r-t,u=s-e,l=h*i-u*n;return 0!==l||o||(l=(h*n+h*n)/(n*n+i*i),l>=0&&1>=l&&(l=0)),0>l?-1:l>0?1:0},getSignedDistance:function(t,e,n,i,r,s,a){return a||(n-=t,i-=e),0===n?i>0?r-t:t-r:0===i?0>n?s-e:e-s:((r-t)*i-(s-e)*n)/Math.sqrt(n*n+i*i)}}}),v=r.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){r.call(this,!0),this.layers=[],this._activeLayer=null,this.symbols=[],this._currentStyle=new D(null,null,this),this._view=V.create(this,t||Y.getCanvas(1,1)),this._selectedItems={},this._selectedItemCount=0,this._updateVersion=0},_serialize:function(t,n){return e.serialize(this.layers,t,!0,n)},clear:function(){for(var t=this.layers.length-1;t>=0;t--)this.layers[t].remove();this.symbols=[]},isEmpty:function(){return 0===this.layers.length},remove:function st(){return st.base.call(this)?(this._view&&this._view.remove(),!0):!1},getView:function(){return this._view},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},getOptions:function(){return this._scope.settings},getActiveLayer:function(){return this._activeLayer||new w({project:this})},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},insertChild:function(t,n,i){return n instanceof w?(n._remove(!1,!0),e.splice(this.layers,[n],t,0),n._setProject(this,!0),this._changes&&n._changed(5),this._activeLayer||(this._activeLayer=n)):n instanceof m?(this._activeLayer||this.insertChild(t,new w(m.NO_INSERT))).insertChild(t,n,i):n=null,n},addChild:function(e,n){return this.insertChild(t,e,n)},_updateSelection:function(t){var e=t._id,n=this._selectedItems;t._selected?n[e]!==t&&(this._selectedItemCount++,n[e]=t):n[e]===t&&(this._selectedItemCount--,delete n[e])},selectAll:function(){for(var t=this.layers,e=0,n=t.length;n>e;e++)t[e].setFullySelected(!0)},deselectAll:function(){var t=this._selectedItems;for(var e in t)t[e].setFullySelected(!1)},hitTest:function(){for(var t=h.read(arguments),n=S.getOptions(e.read(arguments)),i=this.layers.length-1;i>=0;i--){var r=this.layers[i]._hitTest(t,n);if(r)return r}return null},getItems:function(t){return m._getItems(this.layers,t)},getItem:function(t){return m._getItems(this.layers,t,null,null,!0)[0]||null},importJSON:function(t){this.activate();var n=this._activeLayer;return e.importJSON(t,n&&n.isEmpty()&&n)},draw:function(t,n,i){this._updateVersion++,t.save(),n.applyToContext(t);for(var r=new e({offset:new h(0,0),pixelRatio:i,viewMatrix:n.isIdentity()?null:n,matrices:[new g],updateMatrix:!0}),s=0,a=this.layers,o=a.length;o>s;s++)a[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;var u=this._selectedItems,l=this._scope.settings.handleSize,c=this._updateVersion;for(var d in u)u[d]._drawSelection(t,n,l,u,c);t.restore()}}}),p=e.extend({_class:"Symbol",initialize:function(t,e){this._id=o.get(),this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._class,this._definition],t,!1,n)})},_changed:function(t){8&t&&m._clearBoundsCache(this),1&t&&(this.project._needsUpdate=!0)},getDefinition:function(){return this._definition},setDefinition:function(t,e){t._parentSymbol&&(t=t.clone()),this._definition&&(this._definition._parentSymbol=null),this._definition=t,t.remove(),t.setSelected(!1),e||t.setPosition(new h),t._parentSymbol=this,this._changed(9)},place:function(t){return new C(this,t)},clone:function(){return new p(this._definition.clone(!1))},equals:function(t){return t===this||t&&this.definition.equals(t.definition)||!1}}),m=e.extend(n,{statics:{extend:function at(t){return t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields)),at.base.apply(this,arguments)},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,applyMatrix:null,matrix:new g,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,n){var i=t&&e.isPlainObject(t),r=i&&t.internal===!0,s=this._matrix=new g,a=i&&t.project||paper.project;return r||(this._id=o.get()),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,n&&s.translate(n),s._owner=this,this._style=new D(a._currentStyle,this,a),this._project||(r||i&&t.insert===!1?this._setProject(a):i&&t.parent?this.setParent(t.parent):(a._activeLayer||new w).addChild(this)),i&&t!==m.NO_INSERT&&this._set(t,{insert:!0,project:!0,parent:!0},!0),i},_events:e.each(["onMouseDown","onMouseUp","onMouseDrag","onClick","onDoubleClick","onMouseMove","onMouseEnter","onMouseLeave"],function(t){this[t]={install:function(t){this.getView()._installEvent(t)},uninstall:function(t){this.getView()._uninstallEvent(t)}}},{onFrame:{install:function(){this.getView()._animateItem(this,!0)},uninstall:function(){this.getView()._animateItem(this,!1)}},onLoad:{}}),_serialize:function(t,n){function i(i){for(var a in i){var o=s[a];e.equals(o,"leading"===a?1.2*i.fontSize:i[a])||(r[a]=e.serialize(o,t,"data"!==a,n))}}var r={},s=this;return i(this._serializeFields),this instanceof y||i(this._style._defaults),[this._class,r]},_changed:function(e){var n=this._parentSymbol,i=this._parent||n,r=this._project;if(8&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=this._currentPath=t),i&&40&e&&m._clearBoundsCache(i),2&e&&m._clearBoundsCache(this),r&&(1&e&&(r._needsUpdate=!0),r._changes)){var s=r._changesById[this._id];s?s.flags|=e:(s={item:this,flags:e},r._changesById[this._id]=s,r._changes.push(s))}n&&n._changed(e)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getName:function(){return this._name},setName:function(e,n){if(this._name&&this._removeNamed(),e===+e+"")throw Error("Names consisting only of numbers are not supported.");var i=this._parent;if(e&&i){for(var r=i._children,s=i._namedChildren,a=e,o=1;n&&r[e];)e=a+" "+o++;(s[e]=s[e]||[]).push(this),r[e]=this}this._name=e||t,this._changed(128)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)}},e.each(["locked","visible","blendMode","opacity","guide"],function(t){var n=e.capitalize(t),t="_"+t;this["get"+n]=function(){return this[t]},this["set"+n]=function(e){e!=this[t]&&(this[t]=e,this._changed("_locked"===t?128:129))}},{}),{beans:!0,_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=this._children,e=0,n=t.length;n>e;e++)if(t[e].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var n=this._children,i=0,r=n.length;r>i;i++)n[i].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(129))},_selected:!1,isFullySelected:function(){var t=this._children;if(t&&this._selected){for(var e=0,n=t.length;n>e;e++)if(!t[e].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){var e=this._children;if(e)for(var n=0,i=e.length;i>n;n++)e[n].setFullySelected(t);this.setSelected(t,!0)},isClipMask:function(){return this._clipMask},setClipMask:function(t){this._clipMask!=(t=!!t)&&(this._clipMask=t,t&&(this.setFillColor(null),this.setStrokeColor(null)),this._changed(129),this._parent&&this._parent._changed(1024))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(t){var e=this._position,n=t?h:u;if(!e){var i=this._pivot;e=this._position=i?this._matrix._transformPoint(i):this.getBounds().getCenter(!0)}return new n(e.x,e.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))},getPivot:function(t){var e=this._pivot;if(e){var n=t?h:u;e=new n(e.x,e.y,this,"setPivot")}return e},setPivot:function(){this._pivot=h.read(arguments,0,{clone:!0,readNull:!0}),this._position=t},_pivot:null},e.each(["bounds","strokeBounds","handleBounds","roughBounds","internalBounds","internalRoughBounds"],function(t){var n="get"+e.capitalize(t),i=t.match(/^internal(.*)$/),r=i?"get"+i[1]:null;this[n]=function(e){var i=this._boundsGetter,s=!r&&("string"==typeof i?i:i&&i[n])||n,a=this._getCachedBounds(s,e,this,r);return"bounds"===t?new f(a.x,a.y,a.width,a.height,this,"setBounds"):a}},{beans:!0,_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new d;m._updateBoundsCache(this,n);for(var r=1/0,s=-r,a=r,o=s,h=0,u=i.length;u>h;h++){var l=i[h];if(l._visible&&!l.isEmpty()){var c=l._getCachedBounds(t,e&&e.chain(l._matrix),n);r=Math.min(c.x,r),a=Math.min(c.y,a),s=Math.max(c.x+c.width,s),o=Math.max(c.y+c.height,o)}}return isFinite(r)?new d(r,a,s-r,o-a):new d},setBounds:function(){var t=d.read(arguments),e=this.getBounds(),n=new g,i=t.getCenter();n.translate(i),(t.width!=e.width||t.height!=e.height)&&n.scale(0!=e.width?t.width/e.width:1,0!=e.height?t.height/e.height:1),i=e.getCenter(),n.translate(-i.x,-i.y),this.transform(n)},_getCachedBounds:function(t,e,n,i){e=e&&e.orNullIfIdentity();var r=i?null:this._matrix.orNullIfIdentity(),s=(!e||e.equals(r))&&t;if(m._updateBoundsCache(this._parent||this._parentSymbol,n),s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();var a=this._getBounds(i||t,e||r,n);if(s){this._bounds||(this._bounds={});var o=this._bounds[s]=a.clone();o._internal=!!i}return a},statics:{_updateBoundsCache:function(t,e){if(t){var n=e._id,i=t._boundsCache=t._boundsCache||{ids:{},list:[]};i.ids[n]||(i.list.push(e),i.ids[n]=e)}},_clearBoundsCache:function(e){var n=e._boundsCache;if(n){e._bounds=e._position=e._boundsCache=t;for(var i=0,r=n.list,s=r.length;s>i;i++){var a=r[i];a!==e&&(a._bounds=a._position=t,a._boundsCache&&m._clearBoundsCache(a))}}}}}),{beans:!0,_decompose:function(){return this._decomposed=this._matrix.decompose()},getRotation:function(){var t=this._decomposed||this._decompose();return t&&t.rotation},setRotation:function(t){var e=this.getRotation();if(null!=e&&null!=t){var n=this._decomposed;this.rotate(t-e),n.rotation=t,this._decomposed=n}},getScaling:function(t){var e=this._decomposed||this._decompose(),n=e&&e.scaling,i=t?h:u;return n&&new i(n.x,n.y,this,"setScaling")},setScaling:function(){var t=this.getScaling();if(t){var e=h.read(arguments,0,{clone:!0}),n=this._decomposed;this.scale(e.x/t.x,e.y/t.y),n.scaling=e,this._decomposed=n}},getMatrix:function(){return this._matrix},setMatrix:function(){var t=this._matrix;t.initialize.apply(t,arguments),this._applyMatrix?this.transform(null,!0):this._changed(9)},getGlobalMatrix:function(t){var e=this._globalMatrix,n=this._project._updateVersion;if(e&&e._updateVersion!==n&&(e=null),!e){e=this._globalMatrix=this._matrix.clone();var i=this._parent;i&&e.preConcatenate(i.getGlobalMatrix(!0)),e._updateVersion=n}return t?e:e.clone()},getApplyMatrix:function(){return this._applyMatrix},setApplyMatrix:function(t){(this._applyMatrix=this._canApplyMatrix&&!!t)&&this.transform(null,!0)},getTransformContent:"#getApplyMatrix",setTransformContent:"#setApplyMatrix"},{getProject:function(){return this._project},_setProject:function(t,e){if(this._project!==t){this._project&&this._installEvents(!1),this._project=t;for(var n=this._children,i=0,r=n&&n.length;r>i;i++)n[i]._setProject(t);e=!0}e&&this._installEvents(!0)},getView:function(){return this._project.getView()},_installEvents:function ot(t){ot.base.call(this,t);for(var e=this._children,n=0,i=e&&e.length;i>n;n++)e[n]._installEvents(t)},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof w)return t;return null},getParent:function(){return this._parent},setParent:function(t){return t.addChild(this)},getChildren:function(){return this._children},setChildren:function(t){this.removeChildren(),this.addChildren(t)},getFirstChild:function(){return this._children&&this._children[0]||null},getLastChild:function(){return this._children&&this._children[this._children.length-1]||null},getNextSibling:function(){return this._parent&&this._parent._children[this._index+1]||null},getPreviousSibling:function(){return this._parent&&this._parent._children[this._index-1]||null},getIndex:function(){return this._index},equals:function(t){return t===this||t&&this._class===t._class&&this._style.equals(t._style)&&this._matrix.equals(t._matrix)&&this._locked===t._locked&&this._visible===t._visible&&this._blendMode===t._blendMode&&this._opacity===t._opacity&&this._clipMask===t._clipMask&&this._guide===t._guide&&this._equals(t)||!1},_equals:function(t){return e.equals(this._children,t._children)},clone:function(t){return this._clone(new this.constructor(m.NO_INSERT),t)},_clone:function(n,i,r){var s=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],a=this._children;n.setStyle(this._style);for(var o=0,h=a&&a.length;h>o;o++)n.addChild(a[o].clone(!1),!0);for(var o=0,h=s.length;h>o;o++){var u=s[o];this.hasOwnProperty(u)&&(n[u]=this[u])}return r!==!1&&n._matrix.initialize(this._matrix),n.setApplyMatrix(this._applyMatrix),n.setPivot(this._pivot),n.setSelected(this._selected),n._data=this._data?e.clone(this._data):null,(i||i===t)&&n.insertAbove(this),this._name&&n.setName(this._name,!0),n},copyTo:function(t){return t.addChild(this.clone(!1))},rasterize:function(t){var n=this.getStrokeBounds(),i=(t||this.getView().getResolution())/72,r=n.getTopLeft().floor(),s=n.getBottomRight().ceil(),a=new l(s.subtract(r)),o=Y.getCanvas(a.multiply(i)),h=o.getContext("2d"),u=(new g).scale(i).translate(r.negate());h.save(),u.applyToContext(h),this.draw(h,new e({matrices:[u]})),h.restore();var c=new b(m.NO_INSERT);return c.setCanvas(o),c.transform((new g).translate(r.add(a.divide(2))).scale(1/i)),c.insertAbove(this),c},contains:function(){return!!this._contains(this._matrix._inverseTransform(h.read(arguments)))},_contains:function(t){if(this._children){for(var e=this._children.length-1;e>=0;e--)if(this._children[e].contains(t))return!0;return!1}return t.isInside(this.getInternalBounds())},isInside:function(){return d.read(arguments).contains(this.getBounds())},_asPathItem:function(){return new A.Rectangle({rectangle:this.getInternalBounds(),matrix:this._matrix,insert:!1})},intersects:function(t,e){return t instanceof m?this._asPathItem().getIntersections(t._asPathItem(),null,e||t._matrix,!0).length>0:!1},hitTest:function(){return this._hitTest(h.read(arguments),S.getOptions(e.read(arguments)))},_hitTest:function(n,i){function r(i,r){var s=g["get"+r]();return n.subtract(s).divide(u).length<=1?new S(i,f,{name:e.hyphenate(r),point:s}):t}if(this._locked||!this._visible||this._guide&&!i.guides||this.isEmpty())return null;var s=this._matrix,a=i._totalMatrix,o=this.getView(),h=i._totalMatrix=a?a.chain(s):this.getGlobalMatrix().preConcatenate(o._matrix),u=i._tolerancePadding=new l(A._getPenPadding(1,h.inverted())).multiply(Math.max(i.tolerance,1e-6));if(n=s._inverseTransform(n),!this._children&&!this.getInternalRoughBounds().expand(u.multiply(2))._containsPoint(n))return null;var c,d=!(i.guides&&!this._guide||i.selected&&!this._selected||i.type&&i.type!==e.hyphenate(this._class)||i["class"]&&!(this instanceof i["class"])),f=this;if(d&&(i.center||i.bounds)&&this._parent){var g=this.getInternalBounds();if(i.center&&(c=r("center","Center")),!c&&i.bounds)for(var _=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],v=0;8>v&&!c;v++)c=r("bounds",_[v])}var p=!c&&this._children;if(p)for(var m=this._getChildHitTestOptions(i),v=p.length-1;v>=0&&!c;v--)c=p[v]._hitTest(n,m);return!c&&d&&(c=this._hitTestSelf(n,i)),c&&c.point&&(c.point=s.transform(c.point)),i._totalMatrix=a,c},_getChildHitTestOptions:function(t){return t},_hitTestSelf:function(e,n){return n.fill&&this.hasFill()&&this._contains(e)?new S("fill",this):t},matches:function(t,n){function i(t,n){for(var r in t)if(t.hasOwnProperty(r)){var s=t[r],a=n[r];if(e.isPlainObject(s)&&e.isPlainObject(a)){if(!i(s,a))return!1}else if(!e.equals(s,a))return!1}return!0}var r=typeof t;if("object"===r){for(var s in t)if(t.hasOwnProperty(s)&&!this.matches(s,t[s]))return!1}else{if("function"===r)return t(this);var a=/^(empty|editable)$/.test(t)?this["is"+e.capitalize(t)]():"type"===t?e.hyphenate(this._class):this[t];if(/^(constructor|class)$/.test(t)){if(!(this instanceof n))return!1}else if(n instanceof RegExp){if(!n.test(a))return!1}else if("function"==typeof n){if(!n(a))return!1}else if(e.isPlainObject(n)){if(!i(n,a))return!1}else if(!e.equals(a,n))return!1}return!0},getItems:function(t){return m._getItems(this._children,t,this._matrix)},getItem:function(t){return m._getItems(this._children,t,this._matrix,null,!0)[0]||null},statics:{_getItems:function ht(t,n,i,r,s){if(!r&&"object"==typeof n){var a=n.overlapping,o=n.inside,h=a||o,u=h&&d.read([h]);r={items:[],inside:!!o,overlapping:!!a,rect:u,path:a&&new A.Rectangle({rectangle:u,insert:!1})},h&&(n=e.set({},n,{inside:!0,overlapping:!0}))}var l=r&&r.items,u=r&&r.rect;i=u&&(i||new g);for(var c=0,f=t&&t.length;f>c;c++){var _=t[c],v=i&&i.chain(_._matrix),p=!0;if(u){var h=_.getBounds(v);if(!u.intersects(h))continue;r.inside&&u.contains(h)||r.overlapping&&(h.contains(u)||r.path.intersects(_,v))||(p=!1)}if(p&&_.matches(n)&&(l.push(_),s))break;if(ht(_._children,n,v,r,s),s&&l.length>0)break}return l}}},{importJSON:function(t){var n=e.importJSON(t,this);return n!==this?this.addChild(n):n},addChild:function(e,n){return this.insertChild(t,e,n)},insertChild:function(t,e,n){var i=e?this.insertChildren(t,[e],n):null;return i&&i[0]},addChildren:function(t,e){return this.insertChildren(this._children.length,t,e)},insertChildren:function(t,n,i,r){var s=this._children;if(s&&n&&n.length>0){n=Array.prototype.slice.apply(n);for(var a=n.length-1;a>=0;a--){var o=n[a];if(!r||o instanceof r){var h=o._parent===this&&o._indexa;a++){var o=n[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name),l&&this._changed(5)}this._changed(11)}else n=null;return n},_insertSibling:function(t,e,n){return this._parent?this._parent.insertChild(t,e,n):null},insertAbove:function(t,e){return t._insertSibling(t._index+1,this,e)},insertBelow:function(t,e){return t._insertSibling(t._index,this,e)},sendToBack:function(){return(this._parent||this instanceof w&&this._project).insertChild(0,this)},bringToFront:function(){return(this._parent||this instanceof w&&this._project).addChild(this)},appendTop:"#addChild",appendBottom:function(t){return this.insertChild(0,t)},moveAbove:"#insertAbove",moveBelow:"#insertBelow",reduce:function(){if(this._children&&1===this._children.length){var t=this._children[0].reduce();return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent;if(t){var e=t._children,n=t._namedChildren,i=this._name,r=n[i],s=r?r.indexOf(this):-1;-1!==s&&(e[i]==this&&delete e[i],r.splice(s,1),r.length?e[i]=r[r.length-1]:delete n[i])}},_remove:function(t,n){var i=this._parent;if(i){if(this._name&&this._removeNamed(),null!=this._index&&e.splice(i._children,null,this._index,1),this._installEvents(!1),t){var r=this._project;r&&r._changes&&this._changed(5)}return n&&i._changed(11),this._parent=null,!0}return!1},remove:function(){return this._remove(!0,!0)},replaceWith:function(t){var e=t&&t.insertBelow(this);return e&&this.remove(),e},removeChildren:function(t,n){if(!this._children)return null;t=t||0,n=e.pick(n,this._children.length);for(var i=e.splice(this._children,null,t,n-t),r=i.length-1;r>=0;r--)i[r]._remove(!0,!1);return i.length>0&&this._changed(11),i},clear:"#removeChildren",reverseChildren:function(){if(this._children){this._children.reverse();for(var t=0,e=this._children.length;e>t;t++)this._children[t]._index=t;this._changed(11)}},isEmpty:function(){return!this._children||0===this._children.length},isEditable:function(){for(var t=this;t;){if(!t._visible||t._locked)return!1;t=t._parent}return!0},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()},_getOrder:function(t){function e(t){var e=[];do e.unshift(t);while(t=t._parent);return e}for(var n=e(this),i=e(t),r=0,s=Math.min(n.length,i.length);s>r;r++)if(n[r]!=i[r])return n[r]._index0},isInserted:function(){return this._parent?this._parent.isInserted():!1},isAbove:function(t){return-1===this._getOrder(t)},isBelow:function(t){return 1===this._getOrder(t)},isParent:function(t){return this._parent===t},isChild:function(t){return t&&t._parent===this},isDescendant:function(t){for(var e=this;e=e._parent;)if(e==t)return!0;return!1},isAncestor:function(t){return t?t.isDescendant(this):!1},isSibling:function(t){return this._parent===t._parent},isGroupedWith:function(t){for(var e=this._parent;e;){if(e._parent&&/^(Group|Layer|CompoundPath)$/.test(e._class)&&t.isDescendant(e))return!0;e=e._parent}return!1},translate:function(){var t=new g;return this.transform(t.translate.apply(t,arguments))},rotate:function(t){return this.transform((new g).rotate(t,h.read(arguments,1,{readNull:!0})||this.getPosition(!0)))}},e.each(["scale","shear","skew"],function(t){this[t]=function(){var e=h.read(arguments),n=h.read(arguments,0,{readNull:!0});return this.transform((new g)[t](e,n||this.getPosition(!0)))}},{}),{transform:function(t,e,n,i){t&&t.isIdentity()&&(t=null);var r=this._matrix,s=(e||this._applyMatrix)&&(!r.isIdentity()||t||e&&n&&this._children);if(!t&&!s)return this;if(t&&r.preConcatenate(t),s=s&&this._transformContent(r,n,i)){var a=this._pivot,o=this._style,h=o.getFillColor(!0),u=o.getStrokeColor(!0);a&&r._transformPoint(a,a,!0),h&&h.transform(r),u&&u.transform(r),r.reset(!0),i&&this._canApplyMatrix&&(this._applyMatrix=!0)}var l=this._bounds,c=this._position;this._changed(9);var d=l&&t&&t.decompose();if(d&&!d.shearing&&d.rotation%90===0){for(var f in l){var g=l[f];(s||!g._internal)&&t._transformBounds(g,g)}var _=this._boundsGetter,g=l[_&&_.getBounds||_||"getBounds"];g&&(this._position=g.getCenter(!0)),this._bounds=l}else t&&c&&(this._position=t._transformPoint(c,c));return this},_transformContent:function(t,e,n){var i=this._children;if(i){for(var r=0,s=i.length;s>r;r++)i[r].transform(t,!0,e,n);return!0}},globalToLocal:function(){return this.getGlobalMatrix(!0)._inverseTransform(h.read(arguments))},localToGlobal:function(){return this.getGlobalMatrix(!0)._transformPoint(h.read(arguments))},parentToLocal:function(){return this._matrix._inverseTransform(h.read(arguments))},localToParent:function(){return this._matrix._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=d.read(arguments);var n=this.getBounds(),i=n.height/n.width,r=t.height/t.width,s=(e?i>r:r>i)?t.width/n.width:t.height/n.height,a=new d(new h,new l(n.width*s,n.height*s));a.setCenter(t.getCenter()),this.setBounds(a)},_setStyles:function(t){var e=this._style,n=e.getFillColor(),i=e.getStrokeColor(),r=e.getShadowColor();if(n&&(t.fillStyle=n.toCanvasStyle(t)),i){var s=e.getStrokeWidth();if(s>0){t.strokeStyle=i.toCanvasStyle(t),t.lineWidth=s;var a=e.getStrokeJoin(),o=e.getStrokeCap(),h=e.getMiterLimit();if(a&&(t.lineJoin=a),o&&(t.lineCap=o),h&&(t.miterLimit=h),paper.support.nativeDash){var u=e.getDashArray(),l=e.getDashOffset();u&&u.length&&("setLineDash"in t?(t.setLineDash(u),t.lineDashOffset=l):(t.mozDash=u,t.mozDashOffset=l))}}}if(r){var c=e.getShadowBlur();if(c>0){t.shadowColor=r.toCanvasStyle(t),t.shadowBlur=c;var d=this.getShadowOffset();t.shadowOffsetX=d.x,t.shadowOffsetY=d.y}}},draw:function(t,e,n){function i(t){return a?a.chain(t):t}var r=this._updateVersion=this._project._updateVersion;if(this._visible&&0!==this._opacity){var s=e.matrices,a=e.viewMatrix,o=this._matrix,h=s[s.length-1].chain(o);if(h.isInvertible()){s.push(h),e.updateMatrix&&(h._updateVersion=r,this._globalMatrix=h);var u,l,c,d=this._blendMode,f=this._opacity,g="normal"===d,_=K.nativeModes[d],v=g&&1===f||e.dontStart||e.clip||(_||g&&1>f)&&this._canComposite(),p=e.pixelRatio||1;if(!v){var m=this.getStrokeBounds(i(h));if(!m.width||!m.height)return;c=e.offset,l=e.offset=m.getTopLeft().floor(),u=t,t=Y.getContext(m.getSize().ceil().add(1).multiply(p)),1!==p&&t.scale(p,p)}t.save();var y=n?n.chain(o):!this.getStrokeScaling(!0)&&i(h),w=!v&&e.clipItem,x=!y||w;if(v?(t.globalAlpha=f,_&&(t.globalCompositeOperation=d)):x&&t.translate(-l.x,-l.y),x&&(v?o:i(h)).applyToContext(t),w&&e.clipItem.draw(t,e.extend({clip:!0})),y){t.setTransform(p,0,0,p,0,0);var b=e.offset;b&&t.translate(-b.x,-b.y)}this._draw(t,e,y),t.restore(),s.pop(),e.clip&&!e.dontFinish&&t.clip(),v||(K.process(d,t,u,f,l.subtract(c).multiply(p)),Y.release(t),e.offset=c)}}},_isUpdated:function(t){var e=this._parent;if(e instanceof O)return e._isUpdated(t);var n=this._updateVersion===t;return!n&&e&&e._visible&&e._isUpdated(t)&&(this._updateVersion=t,n=!0),n},_drawSelection:function(t,e,n,i,r){if((this._drawSelected||this._boundsSelected)&&this._isUpdated(r)){var s=this.getSelectedColor(!0)||this.getLayer().getSelectedColor(!0),a=e.chain(this.getGlobalMatrix(!0));if(t.strokeStyle=t.fillStyle=s?s.toCanvasStyle(t):"#009dec",this._drawSelected&&this._drawSelected(t,a,i),this._boundsSelected){var o=n/2,h=a._transformCorners(this.getInternalBounds());t.beginPath();for(var u=0;8>u;u++)t[0===u?"moveTo":"lineTo"](h[u],h[++u]);t.closePath(),t.stroke();for(var u=0;8>u;u++)t.fillRect(h[u]-o,h[++u]-o,n,n)}}},_canComposite:function(){return!1}},e.each(["down","drag","up","move"],function(t){this["removeOn"+e.capitalize(t)]=function(){var e={};return e[t]=!0,this.removeOn(e)}},{removeOn:function(t){for(var e in t)if(t[e]){var n="mouse"+e,i=this._project,r=i._removeSets=i._removeSets||{};r[n]=r[n]||{},r[n][this._id]=this}return this}})),y=m.extend({_class:"Group",_selectChildren:!0,_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ut(e){ut.base.call(this,e),1026&e&&(this._clipItem=t)},_getClipItem:function(){var e=this._clipItem;if(e===t){e=null;for(var n=0,i=this._children.length;i>n;n++){var r=this._children[n];if(r._clipMask){e=r;break}}this._clipItem=e}return e},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clip,i=!n&&this._getClipItem(),r=!0;if(e=e.extend({clipItem:i,clip:!1}),n?this._currentPath?(t.currentPath=this._currentPath,r=!1):(t.beginPath(),e.dontStart=e.dontFinish=!0):i&&i.draw(t,e.extend({clip:!0})),r)for(var s=0,a=this._children.length;a>s;s++){var o=this._children[s];o!==i&&o.draw(t,e)}n&&(this._currentPath=t.currentPath)}}),w=y.extend({_class:"Layer",initialize:function(n){var i=e.isPlainObject(n)?new e(n):{children:Array.isArray(n)?n:arguments},r=i.insert;i.insert=!1,y.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function lt(t,n){if(this._parent)return lt.base.call(this,t,n);if(null!=this._index){var i=this._project;return i._activeLayer===this&&(i._activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(i.layers,null,this._index,1),this._installEvents(!1),t&&i._changes&&this._changed(5),n&&(i._needsUpdate=!0),!0}return!1},getNextSibling:function ct(){return this._parent?ct.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function dt(){return this._parent?dt.base.call(this):this._project.layers[this._index-1]||null; +},isInserted:function ft(){return this._parent?ft.base.call(this):null!=this._index},activate:function(){this._project._activeLayer=this},_insertSibling:function gt(t,e,n){return this._parent?gt.base.call(this,t,e,n):this._project.insertChild(t,e,n)}}),x=m.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{type:null,size:null,radius:null},initialize:function(t){this._initialize(t)},_equals:function(t){return this._type===t._type&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new x(m.NO_INSERT);return e.setType(this._type),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getType:function(){return this._type},setType:function(t){this._type=t},getShape:"#getType",setShape:"#setType",getSize:function(){var t=this._size;return new c(t.width,t.height,this,"setSize")},setSize:function(){var t=l.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._type,n=t.width,i=t.height;if("rectangle"===e){var r=l.min(this._radius,t.divide(2));this._radius.set(r.width,r.height)}else"circle"===e?(n=i=(n+i)/2,this._radius=n/2):"ellipse"===e&&this._radius.set(n/2,i/2);this._size.set(n,i),this._changed(9)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._type?t:new c(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._type;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else if(t=l.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=l.max(this._size,t.multiply(2));this._size.set(n.width,n.height)}else"ellipse"===e&&this._size.set(2*t.width,2*t.height)}else this._radius=t.clone();this._changed(9)},isEmpty:function(){return!1},toPath:function(t){var n=this._clone(new(A[e.capitalize(this._type)])({center:new h,size:this._size,radius:this._radius,insert:!1}),t);return paper.settings.applyMatrix&&n.setApplyMatrix(!0),n},_draw:function(t,e,n){var i=this._style,r=i.hasFill(),s=i.hasStroke(),a=e.dontFinish||e.clip,o=!n;if(r||s||a){var h=this._type,u=this._radius,l="circle"===h;if(e.dontStart||t.beginPath(),o&&l)t.arc(0,0,u,0,2*Math.PI,!0);else{var c=l?u:u.width,d=l?u:u.height,f=this._size,g=f.width,_=f.height;if(o&&"rectangle"===h&&0===c&&0===d)t.rect(-g/2,-_/2,g,_);else{var v=g/2,p=_/2,m=.44771525016920644,y=c*m,w=d*m,x=[-v,-p+d,-v,-p+w,-v+y,-p,-v+c,-p,v-c,-p,v-y,-p,v,-p+w,v,-p+d,v,p-d,v,p-w,v-y,p,v-c,p,-v+c,p,-v+y,p,-v,p-w,-v,p-d];n&&n.transform(x,x,32),t.moveTo(x[0],x[1]),t.bezierCurveTo(x[2],x[3],x[4],x[5],x[6],x[7]),v!==c&&t.lineTo(x[8],x[9]),t.bezierCurveTo(x[10],x[11],x[12],x[13],x[14],x[15]),p!==d&&t.lineTo(x[16],x[17]),t.bezierCurveTo(x[18],x[19],x[20],x[21],x[22],x[23]),v!==c&&t.lineTo(x[24],x[25]),t.bezierCurveTo(x[26],x[27],x[28],x[29],x[30],x[31])}}t.closePath()}a||!r&&!s||(this._setStyles(t),r&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return"getBounds"!==t&&this.hasStroke()&&(n=n.expand(this.getStrokeWidth())),e?e._transformBounds(n):n}},new function(){function t(t,e,n){var i=t._radius;if(!i.isZero())for(var r=t._size.divide(2),s=0;4>s;s++){var a=new h(1&s?1:-1,s>1?1:-1),o=a.multiply(r),u=o.subtract(a.multiply(i)),l=new d(o,u);if((n?l.expand(n):l).contains(e))return u}}function e(t,e){var n=t.getAngleInRadians(),i=2*e.width,r=2*e.height,s=i*Math.sin(n),a=r*Math.cos(n);return i*r/(2*Math.sqrt(s*s+a*a))}return{_contains:function n(e){if("rectangle"===this._type){var i=t(this,e);return i?e.subtract(i).divide(this._radius).getLength()<=1:n.base.call(this,e)}return e.divide(this.size).getLength()<=.5},_hitTestSelf:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._type,o=this._radius,h=this.getStrokeWidth()+2*r.tolerance;if("rectangle"===a){var u=t(this,n,h);if(u){var l=n.subtract(u);s=2*Math.abs(l.getLength()-e(l,o))<=h}else{var c=new d(this._size).setCenter(0,0),f=c.expand(h),g=c.expand(-h);s=f._containsPoint(n)&&!g._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new S("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){var a=new x(e.getNamed(s));return a._type=t,a._size=i,a._radius=r,a.translate(n)}return{Circle:function(){var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t("circle",n,new l(2*i),i,arguments)},Rectangle:function(){var e=d.readNamed(arguments,"rectangle"),n=l.min(l.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=x._readEllipse(arguments),n=e.radius;return t("ellipse",e.center,n.multiply(2),n,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=h.readNamed(t,"center"),i=l.readNamed(t,"radius");else{var r=d.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),b=m.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{crossOrigin:null,source:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||("string"==typeof e?this.setSource(e):this.setImage(e)),this._size||(this._size=new l,this._loaded=!1)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new b(m.NO_INSERT),n=this._image,i=this._canvas;if(n)e.setImage(n);else if(i){var r=Y.getCanvas(this._size);r.getContext("2d").drawImage(i,0,0),e.setImage(r)}return e._crossOrigin=this._crossOrigin,this._clone(e,t)},getSize:function(){var t=this._size;return new c(t?t.width:0,t?t.height:0,this,"setSize")},setSize:function(){var t=l.read(arguments);if(!t.equals(this._size))if(t.width>0&&t.height>0){var e=this.getElement();this.setImage(Y.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}else this._canvas&&Y.release(this._canvas),this._size=t.clone()},getWidth:function(){return this._size?this._size.width:0},setWidth:function(t){this.setSize(t,this.getHeight())},getHeight:function(){return this._size?this._size.height:0},setHeight:function(t){this.setSize(this.getWidth(),t)},isEmpty:function(){var t=this._size;return!t||0===t.width&&0===t.height},getResolution:function(){var t=this._matrix,e=new h(0,0).transform(t),n=new h(1,0).transform(t).subtract(e),i=new h(0,1).transform(t).subtract(e);return new l(72/n.getLength(),72/i.getLength())},getPpi:"#getResolution",getImage:function(){return this._image},setImage:function(t){this._canvas&&Y.release(this._canvas),t&&t.getContext?(this._image=null,this._canvas=t,this._loaded=!0):(this._image=t,this._canvas=null,this._loaded=t&&t.complete),this._size=new l(t?t.naturalWidth||t.width:0,t?t.naturalHeight||t.height:0),this._context=null,this._changed(521)},getCanvas:function(){if(!this._canvas){var t=Y.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){Y.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(t){return this._context||(this._context=this.getCanvas().getContext("2d")),t&&(this._image=null,this._changed(513)),this._context},setContext:function(t){this._context=t},getSource:function(){return this._image&&this._image.src||this.toDataURL()},setSource:function(t){function e(){var t=i.getView();t&&(paper=t._scope,i.setImage(n),i.emit("load"),t.update())}var n,i=this,r=this._crossOrigin;n=document.getElementById(t)||new Image,r&&(n.crossOrigin=r),n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(q.add(n,{load:e}),n.src||(n.src=t)),this.setImage(n)},getCrossOrigin:function(){return this._image&&this._image.crossOrigin||this._crossOrigin||""},setCrossOrigin:function(t){this._crossOrigin=t,this._image&&(this._image.crossOrigin=t)},getElement:function(){return this._canvas||this._loaded&&this._image}},{beans:!1,getSubCanvas:function(){var t=d.read(arguments),e=Y.getContext(t.getSize());return e.drawImage(this.getCanvas(),t.x,t.y,t.width,t.height,0,0,t.width,t.height),e.canvas},getSubRaster:function(){var t=d.read(arguments),e=new b(m.NO_INSERT);return e.setImage(this.getSubCanvas(t)),e.translate(t.getCenter().subtract(this.getSize().divide(2))),e._matrix.preConcatenate(this._matrix),e.insertAbove(this),e},toDataURL:function(){var t=this._image&&this._image.src;if(/^data:/.test(t))return t;var e=this.getCanvas();return e?e.toDataURL.apply(e,arguments):null},drawImage:function(t){var e=h.read(arguments,1);this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var n,i;t?t instanceof z?(i=t,n=t.getBounds()):t.width?n=new d(t):t.x&&(n=new d(t.x-.5,t.y-.5,1,1)):n=this.getBounds();var r=32,s=Math.min(n.width,r),a=Math.min(n.height,r),o=b._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=b._sampleContext=Y.getContext(new l(r)),o.save();var h=(new g).scale(s/n.width,a/n.height).translate(-n.x,-n.y);h.applyToContext(o),i&&i.draw(o,new e({clip:!0,matrices:[h]})),this._matrix.applyToContext(o);var u=this.getElement(),c=this._size;u&&o.drawImage(u,-c.width/2,-c.height/2),o.restore();for(var f=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,_=[0,0,0],v=0,p=0,m=f.length;m>p;p+=4){var y=f[p+3];v+=y,y/=255,_[0]+=f[p]*y,_[1]+=f[p+1]*y,_[2]+=f[p+2]*y}for(var p=0;3>p;p++)_[p]/=v;return v?j.read(_):null},getPixel:function(){var t=h.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new j("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=j.read(arguments),n=e._convert("rgb"),i=e._alpha,r=this.getContext(!0),s=r.createImageData(1,1),a=s.data;a[0]=255*n[0],a[1]=255*n[1],a[2]=255*n[2],a[3]=null!=i?255*i:255,r.putImageData(s,t.x,t.y)},createImageData:function(){var t=l.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(){var t=d.read(arguments);return t.isEmpty()&&(t=new d(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t){var e=h.read(arguments,1);this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var n=new d(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTestSelf:function(t){if(this._contains(t)){var e=this;return new S("pixel",e,{offset:t.add(e._size.divide(2)).round(),color:{get:function(){return e.getPixel(this.offset)}}})}},_draw:function(t){var e=this.getElement();e&&(t.globalAlpha=this._opacity,t.drawImage(e,-this._size.width/2,-this._size.height/2))},_canComposite:function(){return!0}}),C=m.extend({_class:"PlacedSymbol",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:{getBounds:"getStrokeBounds"},_boundsSelected:!0,_serializeFields:{symbol:null},initialize:function(e,n){this._initialize(e,n!==t&&h.read(arguments,1))||this.setSymbol(e instanceof p?e:new p(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(9)},clone:function(t){var e=new C(m.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,n){var i=this.symbol._definition;return i._getCachedBounds(t,e&&e.chain(i._matrix),n)},_hitTestSelf:function(t,e){var n=this._symbol._definition._hitTest(t,e);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),S=e.extend({_class:"HitResult",initialize:function(t,e,n){this.type=t,this.item=e,n&&(n.enumerable=!0,this.inject(n))},statics:{getOptions:function(t){return new e({type:null,tolerance:paper.settings.hitTolerance,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1},t)}}}),P=e.extend({_class:"Segment",beans:!0,initialize:function(e,n,i,r,s,a){var o,h,u,l=arguments.length;0===l||(1===l?"point"in e?(o=e.point,h=e.handleIn,u=e.handleOut):o=e:2===l&&"number"==typeof e?o=arguments:3>=l?(o=e,h=n,u=i):(o=e!==t?[e,n]:null,h=i!==t?[i,r]:null,u=s!==t?[s,a]:null)),new k(o,this,"_point"),new k(h,this,"_handleIn"),new k(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.hasHandles()?[this._point,this._handleIn,this._handleOut]:this._point,t,!0)},_changed:function(t){var e=this._path;if(e){var n,i=e._curves,r=this._index;i&&(t&&t!==this._point&&t!==this._handleIn||!(n=r>0?i[r-1]:e._closed?i[i.length-1]:null)||n._changed(),t&&t!==this._point&&t!==this._handleOut||!(n=i[r])||n._changed()),e._changed(25)}},getPoint:function(){return this._point},setPoint:function(){var t=h.read(arguments);this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(){var t=h.read(arguments);this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(){var t=h.read(arguments);this._handleOut.set(t.x,t.y)},hasHandles:function(){return!this._handleIn.isZero()||!this._handleOut.isZero()},clearHandles:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},_selectionState:0,isSelected:function(t){var e=this._selectionState;return t?t===this._point?!!(4&e):t===this._handleIn?!!(1&e):t===this._handleOut?!!(2&e):!1:!!(7&e)},setSelected:function(t,e){var n=this._path,t=!!t,i=this._selectionState,r=i,s=e?e===this._point?4:e===this._handleIn?1:e===this._handleOut?2:0:7;t?i|=s:i&=~s,this._selectionState=i,n&&i!==r&&(n._updateSelection(this,r,i),n._changed(129))},getIndex:function(){return this._index!==t?this._index:null},getPath:function(){return this._path||null},getCurve:function(){var t=this._path,e=this._index;return t?(e>0&&!t._closed&&e===t._segments.length-1&&e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new M(t,this===t._segment1?0:1):null},getNext:function(){var t=this._path&&this._path._segments;return t&&(t[this._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._segments;return t&&(t[this._index-1]||this._path._closed&&t[t.length-1])||null},isFirst:function(){return 0===this._index},isLast:function(){var t=this._path;return t&&this._index===t._segments.length-1||!1},reverse:function(){var t=this._handleIn,e=this._handleOut,n=t._x,i=t._y;t.set(e._x,e._y),e.set(n,i)},reversed:function(){return new P(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new P(this._point,this._handleIn,this._handleOut)},equals:function(t){return t===this||t&&this._class===t._class&&this._point.equals(t._point)&&this._handleIn.equals(t._handleIn)&&this._handleOut.equals(t._handleOut)||!1},toString:function(){var t=["point: "+this._point];return this._handleIn.isZero()||t.push("handleIn: "+this._handleIn),this._handleOut.isZero()||t.push("handleOut: "+this._handleOut),"{ "+t.join(", ")+" }"},transform:function(t){this._transformCoordinates(t,Array(6),!0),this._changed()},_transformCoordinates:function(t,e,n){var i=this._point,r=n&&this._handleIn.isZero()?null:this._handleIn,s=n&&this._handleOut.isZero()?null:this._handleOut,a=i._x,o=i._y,h=2;return e[0]=a,e[1]=o,r&&(e[h++]=r._x+a,e[h++]=r._y+o),s&&(e[h++]=s._x+a,e[h++]=s._y+o),t&&(t._transformCoordinates(e,e,h/2),a=e[0],o=e[1],n?(i._x=a,i._y=o,h=2,r&&(r._x=e[h++]-a,r._y=e[h++]-o),s&&(s._x=e[h++]-a,s._y=e[h++]-o)):(r||(e[h++]=a,e[h++]=o),s||(e[h++]=a,e[h++]=o))),e}}),k=h.extend({initialize:function(e,n,i){var r,s,a;if(e)if((r=e[0])!==t)s=e[1];else{var o=e;(r=o.x)===t&&(o=h.read(arguments),r=o.x),s=o.y,a=o.selected}else r=s=0;this._x=r,this._y=s,this._owner=n,n[i]=this,a&&this.setSelected(!0)},set:function(t,e){return this._x=t,this._y=e,this._owner._changed(this),this},_serialize:function(t){var e=t.formatter,n=e.number(this._x),i=e.number(this._y);return this.isSelected()?{x:n,y:i,selected:!0}:[n,i]},getX:function(){return this._x},setX:function(t){this._x=t,this._owner._changed(this)},getY:function(){return this._y},setY:function(t){this._y=t,this._owner._changed(this)},isZero:function(){return a.isZero(this._x)&&a.isZero(this._y)},setSelected:function(t){this._owner.setSelected(t,this)},isSelected:function(){return this._owner.isSelected(this)}}),I=e.extend({_class:"Curve",initialize:function(t,e,n,i,r,s,a,o){var h,u,l,c,d,f,g=arguments.length;3===g?(this._path=t,h=e,u=n):0===g?(h=new P,u=new P):1===g?"segment1"in t?(h=new P(t.segment1),u=new P(t.segment2)):"point1"in t?(l=t.point1,d=t.handle1,f=t.handle2,c=t.point2):Array.isArray(t)&&(l=[t[0],t[1]],c=[t[6],t[7]],d=[t[2]-t[0],t[3]-t[1]],f=[t[4]-t[6],t[5]-t[7]]):2===g?(h=new P(t),u=new P(e)):4===g?(l=t,d=e,f=n,c=i):8===g&&(l=[t,e],c=[a,o],d=[n-t,i-e],f=[r-a,s-o]),this._segment1=h||new P(l,null,d),this._segment2=u||new P(c,f,null)},_serialize:function(t){return e.serialize(this.hasHandles()?[this.getPoint1(),this.getHandle1(),this.getHandle2(),this.getPoint2()]:[this.getPoint1(),this.getPoint2()],t,!0)},_changed:function(){this._length=this._bounds=t},clone:function(){return new I(this._segment1,this._segment2)},toString:function(){var t=["point1: "+this._segment1._point];return this._segment1._handleOut.isZero()||t.push("handle1: "+this._segment1._handleOut),this._segment2._handleIn.isZero()||t.push("handle2: "+this._segment2._handleIn),t.push("point2: "+this._segment2._point),"{ "+t.join(", ")+" }"},remove:function(){var t=!1;if(this._path){var e=this._segment2,n=e._handleOut;t=e.remove(),t&&this._segment1._handleOut.set(n.x,n.y)}return t},getPoint1:function(){return this._segment1._point},setPoint1:function(){var t=h.read(arguments);this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(){var t=h.read(arguments);this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(){var t=h.read(arguments);this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(){var t=h.read(arguments);this._segment2._handleIn.set(t.x,t.y)},getSegment1:function(){return this._segment1},getSegment2:function(){return this._segment2},getPath:function(){return this._path},getIndex:function(){return this._segment1._index},getNext:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index+1]||this._path._closed&&t[0])||null},getPrevious:function(){var t=this._path&&this._path._curves;return t&&(t[this._segment1._index-1]||this._path._closed&&t[t.length-1])||null},isFirst:function(){return 0===this._segment1._index},isLast:function(){var t=this._path;return t&&this._segment1._index===t._curves.length-1||!1},isSelected:function(){return this.getPoint1().isSelected()&&this.getHandle2().isSelected()&&this.getHandle2().isSelected()&&this.getPoint2().isSelected()},setSelected:function(t){this.getPoint1().setSelected(t),this.getHandle1().setSelected(t),this.getHandle2().setSelected(t),this.getPoint2().setSelected(t)},getValues:function(t){return I.getValues(this._segment1,this._segment2,t)},getPoints:function(){for(var t=this.getValues(),e=[],n=0;8>n;n+=2)e.push(new h(t[n],t[n+1]));return e},getLength:function(){return null==this._length&&(this._length=I.getLength(this.getValues(),0,1)),this._length},getArea:function(){return I.getArea(this.getValues())},getLine:function(){return new _(this._segment1._point,this._segment2._point)},getPart:function(t,e){return new I(I.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return I.getLength(this.getValues(),t,e)},getIntersections:function(t){return I._getIntersections(this.getValues(),t&&t!==this?t.getValues():null,this,t,[],{})},_getParameter:function(e,n){return n?e:e&&e.curve===this?e.parameter:e===t&&n===t?.5:this.getParameterAt(e,0)},divide:function(t,e,n){var i=this._getParameter(t,e),r=4e-7,s=1-r,a=null;if(i>=r&&s>=i){var o=I.subdivide(this.getValues(),i),u=o[0],l=o[1],c=n||this.hasHandles(),d=this._segment1,f=this._segment2,g=this._path;c&&(d._handleOut.set(u[2]-u[0],u[3]-u[1]),f._handleIn.set(l[4]-l[6],l[5]-l[7]));var _=u[6],v=u[7],p=new P(new h(_,v),c&&new h(u[4]-_,u[5]-v),c&&new h(l[2]-_,l[3]-v));g?(g.insert(d._index+1,p),a=this.getNext()):(this._segment2=p,a=new I(p,f))}return a},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},reversed:function(){return new I(this._segment2.reversed(),this._segment1.reversed())},clearHandles:function(){this._segment1._handleOut.set(0,0),this._segment2._handleIn.set(0,0)},statics:{getValues:function(t,e,n){var i=t._point,r=t._handleOut,s=e._handleIn,a=e._point,o=[i._x,i._y,i._x+r._x,i._y+r._y,a._x+s._x,a._y+s._y,a._x,a._y];return n&&n._transformCoordinates(o,o,4),o},subdivide:function(e,n){var i=e[0],r=e[1],s=e[2],a=e[3],o=e[4],h=e[5],u=e[6],l=e[7];n===t&&(n=.5);var c=1-n,d=c*i+n*s,f=c*r+n*a,g=c*s+n*o,_=c*a+n*h,v=c*o+n*u,p=c*h+n*l,m=c*d+n*g,y=c*f+n*_,w=c*g+n*v,x=c*_+n*p,b=c*m+n*w,C=c*y+n*x;return[[i,r,d,f,m,y,b,C],[b,C,w,x,v,p,u,l]]},solveCubic:function(t,e,n,i,r,s){var o=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-o),d=3*(u-h)-c,f=l-o-c-d;return a.solveCubic(f,d,c,o-n,i,r,s)},getParameterOf:function(t,e){var n=new h(t[0],t[1]),i=new h(t[6],t[7]),r=1e-12,s=e.isClose(n,r)?0:e.isClose(i,r)?1:null;if(null!==s)return s;for(var a=[e.x,e.y],o=[],u=2e-7,l=0;2>l;l++)for(var c=I.solveCubic(t,l,a[l],o,0,1),d=0;c>d;d++)if(s=o[d],e.isClose(I.getPoint(t,s),u))return s;return e.isClose(n,u)?0:e.isClose(i,u)?1:null},getNearestParameter:function(t,e){function n(n){if(n>=0&&1>=n){var i=e.getDistance(I.getPoint(t,n),!0);if(f>i)return f=i,g=n,!0}}if(I.isStraight(t)){var i=t[0],r=t[1],s=t[6],a=t[7],o=s-i,u=a-r,l=o*o+u*u;if(0===l)return 0;var c=((e.x-i)*o+(e.y-r)*u)/l;return 1e-12>c?0:c>.999999999999?1:I.getParameterOf(t,new h(i+c*o,r+c*u))}for(var d=100,f=1/0,g=0,_=0;d>=_;_++)n(_/d);for(var v=1/(2*d);v>4e-7;)n(g-v)||n(g+v)||(v/=2);return g},getPart:function(t,e,n){var i=e>n;if(i){var r=e;e=n,n=r}return e>0&&(t=I.subdivide(t,e)[1]),1>n&&(t=I.subdivide(t,(n-e)/(1-e))[0]),i?[t[6],t[7],t[4],t[5],t[2],t[3],t[0],t[1]]:t},hasHandles:function(t){var e=a.isZero;return!(e(t[0]-t[2])&&e(t[1]-t[3])&&e(t[4]-t[6])&&e(t[5]-t[7]))},isFlatEnough:function(t,e){var n=t[0],i=t[1],r=t[2],s=t[3],a=t[4],o=t[5],h=t[6],u=t[7],l=3*r-2*n-h,c=3*s-2*i-u,d=3*a-2*h-n,f=3*o-2*u-i;return Math.max(l*l,d*d)+Math.max(c*c,f*f)<10*e*e},getArea:function(t){var e=t[0],n=t[1],i=t[6],r=t[7],s=(t[2]+e)/2,a=(t[3]+n)/2,o=(t[4]+t[6])/2,h=(t[5]+t[7])/2;return 6*((e-s)*(a+n)+(s-o)*(h+a)+(o-i)*(r+h))/10},getBounds:function(t){for(var e=t.slice(0,2),n=e.slice(),i=[0,0],r=0;2>r;r++)I._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,n,i);return new d(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,o,h,u){function l(t,e){var n=t-e,i=t+e;nh[r]&&(h[r]=i)}var c=3*(e-n)-t+i,d=2*(t+n)-4*e,f=e-t,g=a.solveQuadratic(c,d,f,u),_=4e-7,v=1-_;l(i,0);for(var p=0;g>p;p++){var m=u[p],y=1-m;m>_&&v>m&&l(y*y*y*t+3*y*y*m*e+3*y*m*m*n+m*m*m*i,s)}}}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){this._bounds||(this._bounds={});var e=this._bounds[t];if(!e){var n=this._path;e=this._bounds[t]=A[t]([this._segment1,this._segment2],!1,n&&n.getStyle())}return e.clone()}},{}),e.each({isStraight:function(t,e,n){if(e.isZero()&&n.isZero())return!0;if(t.isZero())return!1;if(e.isCollinear(t)&&n.isCollinear(t)){var i=t.dot(t),r=t.dot(e)/i,s=t.dot(n)/i;return r>=0&&1>=r&&0>=s&&s>=-1}return!1},isLinear:function(t,e,n){var i=t.divide(3);return e.equals(i)&&n.negate().equals(i)}},function(t,e){this[e]=function(){var e=this._segment1,n=this._segment2;return t(n._point.subtract(e._point),e._handleOut,n._handleIn)},this.statics[e]=function(e){var n=e[0],i=e[1],r=e[6],s=e[7];return t(new h(r-n,s-i),new h(e[2]-n,e[3]-i),new h(e[4]-r,e[5]-s))}},{statics:{},hasHandles:function(){return!this._segment1._handleOut.isZero()||!this._segment2._handleIn.isZero()},isCollinear:function(t){return t&&this.isStraight()&&t.isStraight()&&this.getLine().isCollinear(t.getLine())},isHorizontal:function(){return this.isStraight()&&Math.abs(this.getTangentAt(.5,!0).y)<1e-7},isVertical:function(){return this.isStraight()&&Math.abs(this.getTangentAt(.5,!0).x)<1e-7}}),{beans:!1,getParameterAt:function(t,e){return I.getParameterAt(this.getValues(),t,e)},getParameterOf:function(){return I.getParameterOf(this.getValues(),h.read(arguments))},getLocationAt:function(t,e){var n=e?t:this.getParameterAt(t);return null!=n&&n>=0&&1>=n?new M(this,n):null},getLocationOf:function(){return this.getLocationAt(this.getParameterOf(h.read(arguments)),!0)},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getNearestLocation:function(){var t=h.read(arguments),e=this.getValues(),n=I.getNearestParameter(e,t),i=I.getPoint(e,n);return new M(this,n,i,null,t.getDistance(i))},getNearestPoint:function(){return this.getNearestLocation.apply(this,arguments).getPoint()}},new function(){var t=["getPoint","getTangent","getNormal","getWeightedTangent","getWeightedNormal","getCurvature"];return e.each(t,function(t){this[t+"At"]=function(e,n){var i=this.getValues();return I[t](i,n?e:I.getParameterAt(i,e,0))}},{statics:{evaluateMethods:t}})},new function(){function e(t){var e=t[0],n=t[1],i=t[2],r=t[3],s=t[4],a=t[5],o=t[6],h=t[7],u=9*(i-s)+3*(o-e),l=6*(e+s)-12*i,c=3*(i-e),d=9*(r-a)+3*(h-n),f=6*(n+a)-12*r,g=3*(r-n);return function(t){var e=(u*t+l)*t+c,n=(d*t+f)*t+g;return Math.sqrt(e*e+n*n)}}function n(t,e){return Math.max(2,Math.min(16,Math.ceil(32*Math.abs(e-t))))}function i(t,e,n,i){if(null==e||0>e||e>1)return null;var r,s,a=t[0],o=t[1],u=t[2],l=t[3],c=t[4],d=t[5],f=t[6],g=t[7],_=4e-7,v=1-_;if(0===n&&(_>e||e>v)){var p=_>e;r=p?a:f,s=p?o:g}else{var m=3*(u-a),y=3*(c-u)-m,w=f-a-m-y,x=3*(l-o),b=3*(d-l)-x,C=g-o-x-b;if(0===n)r=((w*e+y)*e+m)*e+a,s=((C*e+b)*e+x)*e+o;else{if(_>e?(r=m,s=x):e>v?(r=3*(f-c),s=3*(g-d)):(r=(3*w*e+2*y)*e+m,s=(3*C*e+2*b)*e+x),i){0===r&&0===s&&(_>e||e>v)&&(r=c-u,s=d-l);var S=Math.sqrt(r*r+s*s);S&&(r/=S,s/=S)}if(3===n){var P=6*w*e+2*y,k=6*C*e+2*b,I=Math.pow(r*r+s*s,1.5);r=0!==I?(r*k-s*P)/I:0,s=0}}}return 2===n?new h(s,-r):new h(r,s)}return{statics:{getLength:function(i,r,s){if(r===t&&(r=0),s===t&&(s=1),0===r&&1===s&&I.isStraight(i)){var o=i[6]-i[0],h=i[7]-i[1];return Math.sqrt(o*o+h*h)}var u=e(i);return a.integrate(u,r,s,n(r,s))},getParameterAt:function(i,r,s){function o(t){return _+=a.integrate(d,s,t,n(s,t)),s=t,_-r}if(s===t&&(s=0>r?1:0),0===r)return s;var h=Math.abs,u=r>0,l=u?s:0,c=u?1:s,d=e(i),f=a.integrate(d,l,c,n(l,c));if(h(r-f)<1e-12)return u?c:l;if(h(r)>f)return null;var g=r/f,_=0;return a.findRoot(o,d,s+g,l,c,32,1e-12)},getPoint:function(t,e){return i(t,e,0,!1)},getTangent:function(t,e){return i(t,e,1,!0)},getWeightedTangent:function(t,e){return i(t,e,1,!1)},getNormal:function(t,e){return i(t,e,2,!0)},getWeightedNormal:function(t,e){return i(t,e,2,!1)},getCurvature:function(t,e){return i(t,e,3,!1).x}}}},new function(){function t(t,e,n,i,r,s,a,o,h,u,l){var c=e.startConnected,d=e.endConnected,f=4e-7,g=1-f;if(null==r&&(r=I.getParameterOf(n,s)),null!==r&&r>=(c?f:0)&&(d?g:1)>=r&&(null==h&&(h=I.getParameterOf(a,u)),null!==h&&h>=(d?f:0)&&(c?g:1)>=h)){var _=e.renormalize;if(_){var v=_(r,h);r=v[0],h=v[1]}var p=new M(i,r,s||I.getPoint(n,r),l),m=new M(o,h,u||I.getPoint(a,h),l),y=p.getPath()===m.getPath()&&p.getIndex()>m.getIndex(),w=y?m:p,x=e.include;p._intersection=m,m._intersection=p,(!x||x(w))&&M.insert(t,w,!0)}}function e(r,s,a,o,h,u,l,c,d,f,g,v,p){if(!(++p>=24)){var m,y,w=s[0],x=s[1],b=s[6],C=s[7],S=_.getSignedDistance,P=S(w,x,b,C,s[2],s[3]),k=S(w,x,b,C,s[4],s[5]),M=P*k>0?.75:4/9,z=M*Math.min(0,P,k),A=M*Math.max(0,P,k),O=S(w,x,b,C,r[0],r[1]),T=S(w,x,b,C,r[2],r[3]),E=S(w,x,b,C,r[4],r[5]),L=S(w,x,b,C,r[6],r[7]),N=n(O,T,E,L),j=N[0],B=N[1];if(null!=(m=i(j,B,z,A))&&null!=(y=i(j.reverse(),B.reverse(),z,A))){r=I.getPart(r,m,y);var R=y-m,D=l+(c-l)*m,F=l+(c-l)*y;if(g>.5&&R>.5)if(F-D>f-d){var q=I.subdivide(r,.5),V=D+(F-D)/2;e(s,q[0],o,a,h,u,d,f,D,V,R,!v,p),e(s,q[1],o,a,h,u,d,f,V,F,R,!v,p)}else{var q=I.subdivide(s,.5),V=d+(f-d)/2;e(q[0],r,o,a,h,u,d,V,D,F,R,!v,p),e(q[1],r,o,a,h,u,V,f,D,F,R,!v,p)}else if(Math.max(f-d,F-D)<1e-7){var H=D+(F-D)/2,Z=d+(f-d)/2;r=a.getValues(),s=o.getValues(),t(h,u,v?s:r,v?o:a,v?Z:H,null,v?r:s,v?a:o,v?H:Z,null)}else R>1e-12&&e(s,r,o,a,h,u,d,f,D,F,R,!v,p)}}}function n(t,e,n,i){var r,s=[0,t],a=[1/3,e],o=[2/3,n],h=[1,i],u=e-(2*t+i)/3,l=n-(t+2*i)/3;if(0>u*l)r=[[s,a,h],[s,o,h]];else{var c=u/l;r=[c>=2?[s,a,h]:.5>=c?[s,o,h]:[s,a,o,h],[s,h]]}return 0>(u||l)?r.reverse():r}function i(t,e,n,i){return t[0][1]i?r(e,!1,i):t[0][0]}function r(t,e,n){for(var i=t[0][0],r=t[0][1],s=1,a=t.length;a>s;s++){var o=t[s][0],h=t[s][1];if(e?h>=n:n>=h)return h===n?o:i+(n-r)*(o-i)/(h-r);i=o,r=h}return null}function s(e,n,i,r,s,o){for(var h=I.isStraight(e),u=h?n:e,l=h?e:n,c=l[0],d=l[1],f=l[6],g=l[7],_=f-c,v=g-d,p=Math.atan2(-v,_),m=Math.sin(p),y=Math.cos(p),w=[],x=0;8>x;x+=2){var b=u[x]-c,C=u[x+1]-d;w.push(b*y-C*m,b*m+C*y)}for(var S=[],P=I.solveCubic(w,1,0,S,0,1),x=0;P>x;x++){var k=S[x],M=I.getPoint(u,k),z=I.getParameterOf(l,M);if(null!==z){var A=I.getPoint(l,z),O=h?z:k,T=h?k:z;(!o.endConnected||T>a.CURVETIME_EPSILON)&&t(s,o,e,i,O,h?A:M,n,r,T,h?M:A)}}}function o(e,n,i,r,s,a){var o=_.intersect(e[0],e[1],e[6],e[7],n[0],n[1],n[6],n[7]);o&&t(s,a,e,i,null,o,n,r,null,o)}return{statics:{_getIntersections:function(n,i,r,a,u,l){if(!i)return I._getSelfIntersection(n,r,u,l);var c=n[0],d=n[1],f=n[6],g=n[7],_=i[0],v=i[1],p=i[6],m=i[7],y=(3*n[2]+c)/4,w=(3*n[3]+d)/4,x=(3*n[4]+f)/4,b=(3*n[5]+g)/4,C=(3*i[2]+_)/4,S=(3*i[3]+v)/4,P=(3*i[4]+p)/4,k=(3*i[5]+m)/4,M=Math.min,z=Math.max;if(!(z(c,y,x,f)>=M(_,C,P,p)&&M(c,y,x,f)<=z(_,C,P,p)&&z(d,w,b,g)>=M(v,S,k,m)&&M(d,w,b,g)<=z(v,S,k,m)))return u;if(!l.startConnected&&!l.endConnected){var A=I.getOverlaps(n,i);if(A){for(var O=0;2>O;O++){var T=A[O];t(u,l,n,r,T[0],null,i,a,T[1],null,!0)}return u}}var E=I.isStraight(n),L=I.isStraight(i),N=E&&L,j=1e-12,B=u.length;if((N?o:E||L?s:e)(n,i,r,a,u,l,0,1,0,1,0,!1,0),N&&u.length>B)return u;var R=new h(c,d),D=new h(f,g),F=new h(_,v),q=new h(p,m);return R.isClose(F,j)&&t(u,l,n,r,0,R,i,a,0,F),!l.startConnected&&R.isClose(q,j)&&t(u,l,n,r,0,R,i,a,1,q),!l.endConnected&&D.isClose(F,j)&&t(u,l,n,r,1,D,i,a,0,F),D.isClose(q,j)&&t(u,l,n,r,1,D,i,a,1,q),u},_getSelfIntersection:function(t,e,n,i){var r=t[0],s=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],f=t[7],g=new _(r,s,d,f,!1),v=g.getSide(new h(o,u),!0),p=g.getSide(new h(l,c),!0);if(v===p){var m=(r-l)*(u-f)+(o-d)*(c-s);if(m*v>0)return n}var y=d-3*l+3*o-r,w=l-2*o+r,x=o-r,b=f-3*c+3*u-s,C=c-2*u+s,S=u-s,P=b*x-y*S,k=b*w-y*C,M=C*x-w*S;if(0>P*P-4*k*M){var z,A=[],O=a.solveCubic(y*y+b*b,3*(y*w+b*C),2*(w*w+C*C)+y*x+b*S,w*x+C*S,A,0,1);if(O>0){for(var T=0,E=0;O>T;T++){var L=Math.abs(e.getCurvatureAt(A[T],!0));L>E&&(E=L,z=A[T])}var N=I.subdivide(t,z);i.endConnected=!0,i.renormalize=function(t,e){return[t*z,e*(1-z)+z]},I._getIntersections(N[0],N[1],e,e,n,i)}}return n},getOverlaps:function(t,e){function n(t){var e=t[6]-t[0],n=t[7]-t[1];return e*e+n*n}var i=Math.abs,r=4e-7,s=2e-7,a=I.isStraight(t),o=I.isStraight(e),u=a&&o;if(u){var l=n(t)s||f.getDistance(new h(d[6],d[7]))>s)return null}else if(a^o)return null;for(var g=[t,e],v=[],p=0,m=0;2>p&&v.length<2;p+=0===m?0:1,m=1^m){var y=I.getParameterOf(g[1^p],new h(g[p][0===m?0:6],g[p][0===m?1:7]));if(null!=y){var w=0===p?[m,y]:[y,m];(0===v.length||i(w[0]-v[0][0])>r&&i(w[1]-v[0][1])>r)&&v.push(w)}if(1===p&&0===v.length)break}if(2!==v.length)v=null;else if(!u){var x=I.getPart(t,v[0][0],v[1][0]),b=I.getPart(e,v[0][1],v[1][1]);(i(b[2]-x[2])>s||i(b[3]-x[3])>s||i(b[4]-x[4])>s||i(b[5]-x[5])>s)&&(v=null)}return v}}}}),M=e.extend({_class:"CurveLocation",beans:!0,initialize:function _t(t,e,n,i,r){if(e>.9999996){var s=t.getNext();s&&(e=0,t=s)}this._id=o.get(_t),this._setCurve(t),this._parameter=e,this._point=n||t.getPointAt(e,!0),this._overlap=i,this._distance=r,this._intersection=this._next=this._prev=null},_setCurve:function(t){var e=t._path; +this._version=e?e._version:0,this._curve=t,this._segment=null,this._segment1=t._segment1,this._segment2=t._segment2},_setSegment:function(t){this._setCurve(t.getCurve()),this._segment=t,this._parameter=t===this._segment1?0:1,this._point=t._point.clone()},getSegment:function(){var t=this.getCurve(),e=this._segment;if(!e){var n=this.getParameter();0===n?e=t._segment1:1===n?e=t._segment2:null!=n&&(e=t.getPartLength(0,n)r;r++)t+=i[r].getLength();this._offset=t+=this.getCurveOffset()}return t},getCurveOffset:function(){var t=this.getCurve(),e=this.getParameter();return null!=e&&t&&t.getPartLength(0,e)},getIntersection:function(){return this._intersection},getDistance:function(){return this._distance},divide:function(){var t=this.getCurve(),e=null;return t&&(e=t.divide(this.getParameter(),!0),e&&this._setSegment(e._segment1)),e},split:function(){var t=this.getCurve();return t?t.split(this.getParameter(),!0):null},equals:function(t,e){var n=this===t,i=2e-7;if(!n&&t instanceof M&&this.getPath()===t.getPath()&&this.getPoint().isClose(t.getPoint(),i)){var r=this.getCurve(),s=t.getCurve(),a=Math.abs,o=a((r.isLast()&&s.isFirst()?-1:r.getIndex())+this.getParameter()-((s.isLast()&&r.isFirst()?-1:s.getIndex())+t.getParameter()));n=(4e-7>o||(o=a(this.getOffset()-t.getOffset()))e?t>e&&n>t:t>e&&l>=t||t>=-l&&n>t}var e=this._intersection;if(!e)return!1;var n=this.getParameter(),i=e.getParameter(),r=4e-7,s=1-r;if(n>=r&&s>=n||i>=r&&s>=i)return!this.isTouching();var a=this.getCurve(),o=a.getPrevious(),h=e.getCurve(),u=h.getPrevious(),l=Math.PI;if(!o||!u)return!1;var c=o.getTangentAt(s,!0).negate().getAngleInRadians(),d=a.getTangentAt(r,!0).getAngleInRadians(),f=u.getTangentAt(s,!0).negate().getAngleInRadians(),g=h.getTangentAt(r,!0).getAngleInRadians();return t(f,c,d)^t(g,c,d)&&t(f,d,c)^t(g,d,c)},isOverlap:function(){return!!this._overlap}},e.each(I.evaluateMethods,function(t){var e=t+"At";this[t]=function(){var t=this.getParameter(),n=this.getCurve();return null!=t&&n&&n[e](t,!0)}},{preserve:!0}),new function(){function t(t,e,n){function i(n,i){for(var s=n+i;s>=-1&&r>=s;s+=i){var a=t[(s%r+r)%r];if(!e.getPoint().isClose(a.getPoint(),2e-7))break;if(e.equals(a))return a}return null}for(var r=t.length,s=0,a=r-1;a>=s;){var o,h=s+a>>>1,u=t[h];if(n&&(o=e.equals(u)?u:i(h,-1)||i(h,1)))return e._overlap&&(o._overlap=o._intersection._overlap=!0),o;var l=e.getPath(),c=u.getPath(),d=l===c?e.getIndex()+e.getParameter()-(u.getIndex()+u.getParameter()):l._id-c._id;0>d?a=h-1:s=h+1}return t.splice(s,0,e),e}return{statics:{insert:t,expand:function(e){for(var n=e.slice(),i=0,r=e.length;r>i;i++)t(n,e[i]._intersection,!1);return n}}}}),z=m.extend({_class:"PathItem",initialize:function(){},getIntersections:function(t,e,n,i){var r=this===t||!t,s=this._matrix.orNullIfIdentity(),a=r?s:(n||t._matrix).orNullIfIdentity();if(!r&&!this.getBounds(s).touches(t.getBounds(a)))return[];for(var o,t,h=this.getCurves(),u=r?h:t.getCurves(),l=h.length,c=r?l:u.length,d=[],f=[],g=0;c>g;g++)d[g]=u[g].getValues(a);for(var g=0;l>g;g++){var _=h[g],v=r?d[g]:_.getValues(s),p=_.getPath();p!==t&&(t=p,o=[],f.push(o)),r&&I._getSelfIntersection(v,_,o,{include:e,startConnected:1===l&&_.getPoint1().equals(_.getPoint2())});for(var m=r?g+1:0;c>m;m++){if(i&&o.length)return o;var y=u[m];I._getIntersections(v,d[m],_,y,o,{include:e,startConnected:r&&_.getPrevious()===y,endConnected:r&&_.getNext()===y})}}o=[];for(var g=0,w=f.length;w>g;g++)o.push.apply(o,f[g]);return o},getCrossings:function(t){return this.getIntersections(t,function(t){return t.isCrossing()})},_asPathItem:function(){return this},setPathData:function(t){function e(t,e){var n=+i[t];return o&&(n+=u[e]),n}function n(t){return new h(e(t,"x"),e(t+1,"y"))}var i,r,s,a=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),o=!1,u=new h,c=new h;this.clear();for(var d=0,f=a&&a.length;f>d;d++){var g=a[d],_=g[0],v=_.toLowerCase();i=g.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var p=i&&i.length;switch(o=_===v,"z"!==r||/[mz]/.test(v)||this.moveTo(u=c),v){case"m":case"l":for(var m="m"===v,y=0;p>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=n(y));s=u,m&&(c=u);break;case"h":case"v":for(var w="h"===v?"x":"y",y=0;p>y;y++)u[w]=e(y,w),this.lineTo(u);s=u;break;case"c":for(var y=0;p>y;y+=6)this.cubicCurveTo(n(y),s=n(y+2),u=n(y+4));break;case"s":for(var y=0;p>y;y+=4)this.cubicCurveTo(/[cs]/.test(r)?u.multiply(2).subtract(s):u,s=n(y),u=n(y+2)),r=v;break;case"q":for(var y=0;p>y;y+=4)this.quadraticCurveTo(s=n(y),u=n(y+2));break;case"t":for(var y=0;p>y;y+=2)this.quadraticCurveTo(s=/[qt]/.test(r)?u.multiply(2).subtract(s):u,u=n(y)),r=v;break;case"a":for(var y=0;p>y;y+=7)this.arcTo(u=n(y+5),new l(+i[y],+i[y+1]),+i[y+2],+i[y+4],+i[y+3]);break;case"z":this.closePath(!0)}r=v}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t,!1,!0);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),A=z.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[],this._version=0;var n=Array.isArray(e)?"object"==typeof e[0]?e:arguments:!e||e.size!==t||e.x===t&&e.point===t?null:arguments;n&&n.length>0?this.setSegments(n):(this._curves=t,this._selectedSegmentState=0,n||"string"!=typeof e||(this.setPathData(e),e=null)),this._initialize(!n&&e)},_equals:function(t){return this._closed===t._closed&&e.equals(this._segments,t._segments)},clone:function(e){var n=new A(m.NO_INSERT);return n.setSegments(this._segments),n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),this._clone(n,e)},_changed:function vt(e){if(vt.base.call(this,e),8&e){var n=this._parent;if(n&&(n._currentPath=t),this._length=this._area=this._clockwise=this._monoCurves=t,16&e)this._version++;else if(this._curves)for(var i=0,r=this._curves.length;r>i;i++)this._curves[i]._changed()}else 32&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof O?t:this)._style},getSegments:function(){return this._segments},setSegments:function(e){var n=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,this._curves=t,e&&e.length>0&&this._add(P.readAll(e)),n&&this.setFullySelected(!0)},getFirstSegment:function(){return this._segments[0]},getLastSegment:function(){return this._segments[this._segments.length-1]},getCurves:function(){var t=this._curves,e=this._segments;if(!t){var n=this._countCurves();t=this._curves=Array(n);for(var i=0;n>i;i++)t[i]=new I(this,e[i],e[i+1]||e[0])}return t},getFirstCurve:function(){return this.getCurves()[0]},getLastCurve:function(){var t=this.getCurves();return t[t.length-1]},isClosed:function(){return this._closed},setClosed:function(t){if(this._closed!=(t=!!t)){if(this._closed=t,this._curves){var e=this._curves.length=this._countCurves();t&&(this._curves[e-1]=new I(this,this._segments[e-1],this._segments[0]))}this._changed(25)}}},{beans:!0,getPathData:function(t,e){function n(e,n){e._transformCoordinates(t,_,!1),i=_[0],r=_[1],v?(p.push("M"+g.pair(i,r)),v=!1):(h=_[2],u=_[3],h===i&&u===r&&l===a&&c===o?n||p.push("l"+g.pair(i-a,r-o)):p.push("c"+g.pair(l-a,c-o)+" "+g.pair(h-a,u-o)+" "+g.pair(i-a,r-o))),a=i,o=r,l=_[4],c=_[5]}var i,r,a,o,h,u,l,c,d=this._segments,f=d.length,g=new s(e),_=Array(6),v=!0,p=[];if(0===f)return"";for(var m=0;f>m;m++)n(d[m]);return this._closed&&f>0&&(n(d[0],!0),p.push("z")),p.join("")}},{isEmpty:function(){return 0===this._segments.length},_transformContent:function(t){for(var e=Array(6),n=0,i=this._segments.length;i>n;n++)this._segments[n]._transformCoordinates(t,e,!0);return!0},_add:function(t,e){for(var n=this._segments,i=this._curves,r=t.length,s=null==e,e=s?n.length:e,a=0;r>a;a++){var o=t[a];o._path&&(o=t[a]=o.clone()),o._path=this,o._index=e+a,o._selectionState&&this._updateSelection(o,0,o._selectionState)}if(s)n.push.apply(n,t);else{n.splice.apply(n,[e,0].concat(t));for(var a=e+r,h=n.length;h>a;a++)n[a]._index=a}if(i){var u=this._countCurves(),l=e+r-1===u?e-1:e,c=l,d=Math.min(l+r,u);t._curves&&(i.splice.apply(i,[l,0].concat(t._curves)),c+=t._curves.length);for(var a=c;d>a;a++)i.splice(a,0,new I(this,null,null));this._adjustCurves(l,d)}return this._changed(25),t},_adjustCurves:function(t,e){for(var n,i=this._segments,r=this._curves,s=t;e>s;s++)n=r[s],n._path=this,n._segment1=i[s],n._segment2=i[s+1]||i[0],n._changed();(n=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0],n._changed()),(n=r[e])&&(n._segment1=i[e],n._changed())},_countCurves:function(){var t=this._segments.length;return!this._closed&&t>0?t-1:t},add:function(t){return arguments.length>1&&"number"!=typeof t?this._add(P.readAll(arguments)):this._add([P.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(P.readAll(arguments,1),t):this._add([P.read(arguments,1)],t)[0]},addSegment:function(){return this._add([P.read(arguments)])[0]},insertSegment:function(t){return this._add([P.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(P.readAll(t))},insertSegments:function(t,e){return this._add(P.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,n,i){t=t||0,n=e.pick(n,this._segments.length);var r=this._segments,s=this._curves,a=r.length,o=r.splice(t,n-t),h=o.length;if(!h)return o;for(var u=0;h>u;u++){var l=o[u];l._selectionState&&this._updateSelection(l,l._selectionState,0),l._index=l._path=null}for(var u=t,c=r.length;c>u;u++)r[u]._index=u;if(s){var d=t>0&&n===a+(this._closed?1:0)?t-1:t,s=s.splice(d,h);i&&(o._curves=s.slice(1)),this._adjustCurves(d,d)}return this._changed(25),o},clear:"#removeSegments",hasHandles:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)if(t[e].hasHandles())return!0;return!1},clearHandles:function(){for(var t=this._segments,e=0,n=t.length;n>e;e++)t[e].clearHandles()},getLength:function(){if(null==this._length){for(var t=this.getCurves(),e=0,n=0,i=t.length;i>n;n++)e+=t[n].getLength();this._length=e}return this._length},getArea:function(){if(null==this._area){for(var t=this._segments,e=t.length,n=e-1,i=0,r=0,s=this._closed?e:n;s>r;r++)i+=I.getArea(I.getValues(t[r],t[n>r?r+1:0]));this._area=i}return this._area},isClockwise:function(){return this._clockwise!==t?this._clockwise:this.getArea()>=0},setClockwise:function(t){this.isClockwise()!=(t=!!t)&&this.reverse(),this._clockwise=t},isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===7*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function pt(t){t||this._selectSegments(!1),pt.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?7*e:0;for(var n=0;e>n;n++)this._segments[n]._selectionState=t?7:0},_updateSelection:function(t,e,n){t._selectionState=n;var i=this._selectedSegmentState+=n-e;i>0&&this.setSelected(!0)},flatten:function(t){for(var e=new T(this,64,.1),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new P(e.getPointAt(n))),n+=i;this.setSegments(s)},reduce:function(){for(var t=this.getCurves(),e=t.length-1;e>=0;e--){var n=t[e];n.hasHandles()||0!==n.getLength()&&!n.isCollinear(n.getNext())||n.remove()}return this},simplify:function(t){if(this._segments.length>2){var e=new E(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null===e)return null;if(1===arguments.length){var n=t;if("number"==typeof n&&(n=this.getLocationAt(n)),!n)return null;t=n.index,e=n.parameter}var i=4e-7,r=1-i;e>=r&&(t++,e--);var s=this.getCurves();if(t>=0&&t=i&&s[t++].divide(e,!0);var a,o=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),a=this):(a=new A(m.NO_INSERT),a.insertAbove(this,!0),this._clone(a)),a._add(o,0),this.addSegment(o[0]),a}return null},reverse:function(){this._segments.reverse();for(var e=0,n=this._segments.length;n>e;e++){var i=this._segments[e],r=i._handleIn;i._handleIn=i._handleOut,i._handleOut=r,i._index=e}this._curves=null,this._clockwise!==t&&(this._clockwise=!this._clockwise),this._changed(9)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();if(!i)return this;n&&n._point.equals(i._point)&&t.reverse();var r=t.getFirstSegment();if(n&&n._point.equals(r._point))n.setHandleOut(r._handleOut),this._add(e.slice(1));else{var s=this.getFirstSegment();s&&s._point.equals(r._point)&&t.reverse(),i=t.getLastSegment(),s&&s._point.equals(i._point)?(s.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())}t._closed&&this._add([e[0]]),t.remove()}var a=this.getFirstSegment(),o=this.getLastSegment();return a!==o&&a._point.equals(o._point)&&(a.setHandleIn(o._handleIn),o.remove(),this.setClosed(!0)),this},toShape:function(t){function e(t,e){var n=c[t],i=n.getNext(),r=c[e],s=r.getNext();return n._handleOut.isZero()&&i._handleIn.isZero()&&r._handleOut.isZero()&&s._handleIn.isZero()&&i._point.subtract(n._point).isCollinear(s._point.subtract(r._point))}function n(t){var e=c[t],n=e.getPrevious(),i=e.getNext();return n._handleOut.isZero()&&e._handleIn.isZero()&&e._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(n._point).isOrthogonal(i._point.subtract(e._point))}function i(t){var e=c[t],n=e.getNext(),i=e._handleOut,r=n._handleIn,s=.5522847498307936;if(i.isOrthogonal(r)){var o=e._point,h=n._point,u=new _(o,i,!0).intersect(new _(h,r,!0),!0);return u&&a.isZero(i.getLength()/u.subtract(o).getLength()-s)&&a.isZero(r.getLength()/u.subtract(h).getLength()-s)}return!1}function r(t,e){return c[t]._point.getDistance(c[e]._point)}if(!this._closed)return null;var s,o,h,u,c=this._segments;if(!this.hasHandles()&&4===c.length&&e(0,2)&&e(1,3)&&n(1)?(s=x.Rectangle,o=new l(r(0,3),r(0,1)),u=c[1]._point.add(c[2]._point).divide(2)):8===c.length&&i(0)&&i(2)&&i(4)&&i(6)&&e(1,5)&&e(3,7)?(s=x.Rectangle,o=new l(r(1,6),r(0,3)),h=o.subtract(new l(r(0,7),r(1,2))).divide(2),u=c[3]._point.add(c[4]._point).divide(2)):4===c.length&&i(0)&&i(1)&&i(2)&&i(3)&&(a.isZero(r(0,2)-r(1,3))?(s=x.Circle,h=r(0,2)/2):(s=x.Ellipse,h=new l(r(2,0)/2,r(3,1)/2)),u=c[1]._point),s){var d=this.getPosition(!0),f=this._clone(new s({center:d,size:o,radius:h,insert:!1}),t,!1);return f.rotate(u.subtract(d).getAngle()+90),f}return null},_hitTestSelf:function(t,e){function n(e,n){return t.subtract(e).divide(n).length<=1}function i(t,i,r){if(!e.selected||i.isSelected()){var s=t._point;if(i!==s&&(i=i.add(s)),n(i,w))return new S(r,g,{segment:t,point:i})}}function r(t,n){return(n||e.segments)&&i(t,t._point,"segment")||!n&&e.handles&&(i(t,t._handleIn,"handle-in")||i(t,t._handleOut,"handle-out"))}function s(t){c.add(t)}function a(e){if(("round"!==o||"round"!==u)&&(c=new A({internal:!0,closed:!0}),m||e._index>0&&e._index0||C?0:null;if(null!==P&&(P>0?(o=_.getStrokeJoin(),u=_.getStrokeCap(),l=P*_.getMiterLimit(),w=y.add(new h(P,P))):o=u="round"),!e.ends||e.segments||m){if(e.segments||e.handles)for(var k=0;p>k;k++)if(f=r(v[k]))return f}else if(f=r(v[0],!0)||r(v[p-1],!0))return f;if(null!==P){if(d=this.getNearestLocation(t)){var I=d.getParameter();0===I||1===I&&p>1?a(d.getSegment())||(d=null):n(d.getPoint(),w)||(d=null)}if(!d&&"miter"===o&&p>1)for(var k=0;p>k;k++){var M=v[k];if(t.getDistance(M._point)<=l&&a(M)){d=M.getLocation();break}}}return!d&&b&&this._contains(t)||d&&!x&&!C?new S("fill",this):d?new S(x?"stroke":"curve",this,{location:d,point:d.getPoint()}):null}},e.each(I.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getLocationAt(e,n);return i&&i[t]()}},{beans:!1,getLocationOf:function(){for(var t=h.read(arguments),e=this.getCurves(),n=0,i=e.length;i>n;n++){var r=e[n].getLocationOf(t);if(r)return r}return null},getOffsetOf:function(){var t=this.getLocationOf.apply(this,arguments);return t?t.getOffset():null},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t,s=n[r];return s?s.getLocationAt(t-r,!0):null}for(var a=0,o=n.length;o>a;a++){var h=i,s=n[a];if(i+=s.getLength(),i>t)return s.getLocationAt(t-h)}return n.length>0&&t<=this.getLength()?new M(n[n.length-1],1):null},getNearestLocation:function(){for(var t=h.read(arguments),e=this.getCurves(),n=1/0,i=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceo;o++){var u=e[o];u._transformCoordinates(n,a,!1);var l=u._selectionState,c=a[0],d=a[1];if(1&l&&r(2),2&l&&r(4),t.fillRect(c-s,d-s,i,i),!(4&l)){var f=t.fillStyle;t.fillStyle="#ffffff",t.fillRect(c-s+1,d-s+1,i-2,i-2),t.fillStyle=f}}}function e(t,e,n){function i(e){if(n)e._transformCoordinates(n,g,!1),r=g[0],s=g[1];else{var i=e._point;r=i._x,s=i._y}if(_)t.moveTo(r,s),_=!1;else{if(n)h=g[2],u=g[3];else{var d=e._handleIn;h=r+d._x,u=s+d._y}h===r&&u===s&&l===a&&c===o?t.lineTo(r,s):t.bezierCurveTo(l,c,h,u,r,s)}if(a=r,o=s,n)l=g[4],c=g[5];else{var d=e._handleOut;l=a+d._x,c=o+d._y}}for(var r,s,a,o,h,u,l,c,d=e._segments,f=d.length,g=Array(6),_=!0,v=0;f>v;v++)i(d[v]);e._closed&&f>0&&i(d[0])}return{_draw:function(t,n,i){function r(t){return l[(t%c+c)%c]}var s=n.dontStart,a=n.dontFinish||n.clip,o=this.getStyle(),h=o.hasFill(),u=o.hasStroke(),l=o.getDashArray(),c=!paper.support.nativeDash&&u&&l&&l.length;if(s||t.beginPath(),!s&&this._currentPath?t.currentPath=this._currentPath:(h||u&&!c||a)&&(e(t,this,i),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!a&&(h||u)&&(this._setStyles(t),h&&(t.fill(o.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),u)){if(c){s||t.beginPath();var d,f=new T(this,32,.25,i),g=f.length,_=-o.getDashOffset(),v=0;for(_%=g;_>0;)_-=r(v--)+r(v--);for(;g>_;)d=_+r(v++),(_>0||d>0)&&f.drawPart(t,Math.max(_,0),Math.max(d,0)),_=d+r(v++)}t.stroke()}},_drawSelected:function(n,i){n.beginPath(),e(n,this,i),n.stroke(),t(n,this._segments,i,paper.settings.handleSize)}}},new function(){function t(t){var e=t.length,n=[],i=[],r=2;n[0]=t[0]/r;for(var s=1;e>s;s++)i[s]=1/r,r=(e-1>s?4:2)-i[s],n[s]=(t[s]-n[s-1])/r;for(var s=1;e>s;s++)n[e-s-1]-=i[e-s]*n[e-s];return n}return{smooth:function(){var e=this._segments,n=e.length,i=this._closed,r=n,s=0;if(!(2>=n)){i&&(s=Math.min(n,4),r+=2*Math.min(n,s));for(var a=[],o=0;n>o;o++)a[o+s]=e[o]._point;if(i)for(var o=0;s>o;o++)a[o]=e[o+n-s]._point,a[o+n+s]=e[o]._point;else r--;for(var u=[],o=1;r-1>o;o++)u[o]=4*a[o]._x+2*a[o+1]._x;u[0]=a[0]._x+2*a[1]._x,u[r-1]=3*a[r-1]._x;for(var l=t(u),o=1;r-1>o;o++)u[o]=4*a[o]._y+2*a[o+1]._y;u[0]=a[0]._y+2*a[1]._y,u[r-1]=3*a[r-1]._y;var c=t(u);if(i){for(var o=0,d=n;s>o;o++,d++){var f=o/s,g=1-f,_=o+s,v=d+s;l[d]=l[o]*f+l[d]*g,c[d]=c[o]*f+c[d]*g,l[v]=l[_]*g+l[v]*f,c[v]=c[_]*g+c[v]*f}r--}for(var p=null,o=s;r-s>=o;o++){var m=e[o-s];p&&m.setHandleIn(p.subtract(m._point)),r>o&&(m.setHandleOut(new h(l[o],c[o]).subtract(m._point)),p=r-1>o?new h(2*a[o+1]._x-l[o+1],2*a[o+1]._y-c[o+1]):new h((a[r]._x+l[r-1])/2,(a[r]._y+c[r-1])/2))}if(i&&p){var m=this._segments[0];m.setHandleIn(p.subtract(m._point))}}}}},new function(){function t(t){var e=t._segments;if(0===e.length)throw Error("Use a moveTo() command first");return e[e.length-1]}return{moveTo:function(){var t=this._segments;1===t.length&&this.removeSegment(0),t.length||this._add([new P(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new P(h.read(arguments))])},cubicCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this);r.setHandleOut(e.subtract(r._point)),this._add([new P(i,n.subtract(i))])},quadraticCurveTo:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.cubicCurveTo(e.add(i.subtract(e).multiply(1/3)),e.add(n.subtract(e).multiply(1/3)),n)},curveTo:function(){var n=h.read(arguments),i=h.read(arguments),r=e.pick(e.read(arguments),.5),s=1-r,a=t(this)._point,o=n.subtract(a.multiply(s*s)).subtract(i.multiply(r*r)).divide(2*r*s);if(o.isNaN())throw Error("Cannot put a curve through points with parameter = "+r);this.quadraticCurveTo(o,i)},arcTo:function(){var n,i,r,s,a,o=t(this),u=o._point,c=h.read(arguments),d=e.peek(arguments),f=e.pick(d,!0);if("boolean"==typeof f)var v=u.add(c).divide(2),n=v.add(v.subtract(u).rotate(f?-90:90));else if(e.remain(arguments)<=2)n=c,c=h.read(arguments);else{var p=l.read(arguments);if(p.isZero())return this.lineTo(c);var m=e.read(arguments),f=!!e.read(arguments),y=!!e.read(arguments),v=u.add(c).divide(2),w=u.subtract(v).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=C(p.width),k=C(p.height),I=S*S,M=k*k,z=x*x,A=b*b,O=Math.sqrt(z/I+A/M);if(O>1&&(S*=O,k*=O,I=S*S,M=k*k),O=(I*M-I*A-M*z)/(I*A+M*z),C(O)<1e-12&&(O=0),0>O)throw Error("Cannot create an arc with the given arguments");i=new h(S*b/k,-k*x/S).multiply((y===f?-1:1)*Math.sqrt(O)).rotate(m).add(v),a=(new g).translate(i).rotate(m).scale(S,k),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(c)),!f&&r>0?r-=360:f&&0>r&&(r+=360)}if(n){var T=new _(u.add(n).divide(2),n.subtract(u).rotate(90),!0),E=new _(n.add(c).divide(2),c.subtract(n).rotate(90),!0),L=new _(u,c),N=L.getSide(n);if(i=T.intersect(E,!0),!i){if(!N)return this.lineTo(c);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(i),r=s.getDirectedAngle(c.subtract(i));var j=L.getSide(i);0===j?r=N*Math.abs(r):N===j&&(r+=0>r?360:-360)}for(var B=Math.abs(r),R=B>=360?4:Math.ceil(B/90),D=r/R,F=D*Math.PI/360,q=4/3*Math.sin(F)/(1+Math.cos(F)),V=[],H=0;R>=H;H++){var w=c,Z=null;if(R>H&&(Z=s.rotate(90).multiply(q),a?(w=a._transformPoint(s),Z=a._transformPoint(s.add(Z)).subtract(w)):w=i.add(s)),0===H)o.setHandleOut(Z);else{var U=s.rotate(-90).multiply(q);a&&(U=a._transformPoint(s.add(U)).subtract(w)),V.push(new P(w,U,Z))}s=s.rotate(D)}this._add(V)},lineBy:function(){var e=h.read(arguments),n=t(this)._point;this.lineTo(n.add(e))},curveBy:function(){var n=h.read(arguments),i=h.read(arguments),r=e.read(arguments),s=t(this)._point;this.curveTo(s.add(n),s.add(i),r)},cubicCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=h.read(arguments),r=t(this)._point;this.cubicCurveTo(r.add(e),r.add(n),r.add(i))},quadraticCurveBy:function(){var e=h.read(arguments),n=h.read(arguments),i=t(this)._point;this.quadraticCurveTo(i.add(e),i.add(n))},arcBy:function(){var n=t(this)._point,i=n.add(h.read(arguments)),r=e.pick(e.peek(arguments),!0);"boolean"==typeof r?this.arcTo(i,r):this.arcTo(i,n.add(h.read(arguments)))},closePath:function(t){this.setClosed(!0),t&&this.join()}}},{_getBounds:function(t,e){return A[t](this._segments,this._closed,this.getStyle(),e)},statics:{getBounds:function(t,e,n,i,r){function s(t){t._transformCoordinates(i,o,!1);for(var e=0;2>e;e++)I._addBounds(h[e],h[e+4],o[e+2],o[e],e,r?r[e]:0,u,l,c);var n=h;h=o,o=n}var a=t[0];if(!a)return new d;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),f=1,g=t.length;g>f;f++)s(t[f]);return e&&s(a),new d(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(t){f=f.include(i?i._transformPoint(t,t):t)}function s(t){f=f.unite(p.setCenter(i?i._transformPoint(t._point):t._point))}function a(t,e){var n=t._handleIn,i=t._handleOut;"round"===e||!n.isZero()&&!i.isZero()&&n.isCollinear(i)?s(t):A._addBevelJoin(t,e,u,v,r)}function o(t,e){"round"===e?s(t):A._addSquareCap(t,e,u,r)}if(!n.hasStroke())return A.getBounds(t,e,n,i);for(var h=t.length-(e?0:1),u=n.getStrokeWidth()/2,c=A._getPenPadding(u,i),f=A.getBounds(t,e,n,i,c),g=n.getStrokeJoin(),_=n.getStrokeCap(),v=u*n.getMiterLimit(),p=new d(new l(c).multiply(2)),m=1;h>m;m++)a(t[m],g);return e?a(t[0],g):h>0&&(o(t[0],_),o(t[t.length-1],_)),f},_getPenPadding:function(t,e){if(!e)return[t,t];var n=e.shiftless(),i=n.transform(new h(t,0)),r=n.transform(new h(0,t)),s=i.getAngleInRadians(),a=i.getLength(),o=r.getLength(),u=Math.sin(s),l=Math.cos(s),c=Math.tan(s),d=-Math.atan(o*c/a),f=Math.atan(o/(c*a));return[Math.abs(a*Math.cos(d)*l-o*Math.sin(d)*u),Math.abs(o*Math.sin(f)*l+a*Math.cos(f)*u)]},_addBevelJoin:function(t,e,n,i,r,s){var a=t.getCurve(),o=a.getPrevious(),u=a.getPointAt(0,!0),l=o.getNormalAt(1,!0),c=a.getNormalAt(0,!0),d=l.getDirectedAngle(c)<0?-n:n;if(l.setLength(d),c.setLength(d),s&&(r(u),r(u.add(l))),"miter"===e){var f=new _(u.add(l),new h(-l.y,l.x),!0).intersect(new _(u.add(c),new h(-c.y,c.x),!0),!0);if(f&&u.getDistance(f)<=i&&(r(f),!s))return}s||r(u.add(l)),r(u.add(c))},_addSquareCap:function(t,e,n,i,r){var s=t._point,a=t.getLocation(),o=a.getNormal().multiply(n);r&&(i(s.subtract(o)),i(s.add(o))),"square"===e&&(s=s.add(o.rotate(0===a.getParameter()?-90:90))),i(s.add(o)),i(s.subtract(o))},getHandleBounds:function(t,e,n,i,r,s){for(var a=Array(6),o=1/0,h=-o,u=o,l=h,c=0,f=t.length;f>c;c++){var g=t[c];g._transformCoordinates(i,a,!1);for(var _=0;6>_;_+=2){var v=0===_?s:r,p=v?v[0]:0,m=v?v[1]:0,y=a[_],w=a[_+1],x=y-p,b=y+p,C=w-m,S=w+m;o>x&&(o=x),b>h&&(h=b),u>C&&(u=C),S>l&&(l=S)}}return new d(o,u,h-o,l-u)},getRoughBounds:function(t,e,n,i){var r=n.hasStroke()?n.getStrokeWidth()/2:0,s=r;return r>0&&("miter"===n.getStrokeJoin()&&(s=r*n.getMiterLimit()),"square"===n.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),A.getHandleBounds(t,e,n,i,A._getPenPadding(r,i),A._getPenPadding(s,i))}}});A.inject({statics:new function(){function t(t,n,i){var r=e.getNamed(i),s=new A(r&&r.insert===!1&&m.NO_INSERT);return s._add(t),s._closed=n,s.set(r)}function n(e,n,i){for(var s=Array(4),a=0;4>a;a++){var o=r[a];s[a]=new P(o._point.multiply(n).add(e),o._handleIn.multiply(n),o._handleOut.multiply(n))}return t(s,!0,i)}var i=.5522847498307936,r=[new P([-1,0],[0,i],[0,-i]),new P([0,-1],[-i,0],[i,0]),new P([1,0],[0,-i],[0,i]),new P([0,1],[i,0],[-i,0])];return{Line:function(){return t([new P(h.readNamed(arguments,"from")),new P(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return n(t,new l(i),arguments)},Rectangle:function(){var e,n=d.readNamed(arguments,"rectangle"),r=l.readNamed(arguments,"radius",0,{readNull:!0}),s=n.getBottomLeft(!0),a=n.getTopLeft(!0),o=n.getTopRight(!0),h=n.getBottomRight(!0);if(!r||r.isZero())e=[new P(s),new P(a),new P(o),new P(h)];else{r=l.min(r,n.getSize(!0).divide(2));var u=r.width,c=r.height,f=u*i,g=c*i;e=[new P(s.add(u,0),null,[-f,0]),new P(s.subtract(0,c),[0,g]),new P(a.add(0,c),null,[0,-g]),new P(a.add(u,0),[-f,0],null),new P(o.subtract(u,0),null,[f,0]),new P(o.add(0,c),[0,-g],null),new P(h.subtract(0,c),null,[0,g]),new P(h.subtract(u,0),[f,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=x._readEllipse(arguments);return n(t.center,t.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),n=h.readNamed(arguments,"through"),i=h.readNamed(arguments,"to"),r=e.getNamed(arguments),s=new A(r&&r.insert===!1&&m.NO_INSERT);return s.moveTo(t),s.arcTo(n,i),s.set(r)},RegularPolygon:function(){for(var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"sides"),r=e.readNamed(arguments,"radius"),s=360/i,a=!(i%3),o=new h(0,a?-r:r),u=a?-1:.5,l=Array(i),c=0;i>c;c++)l[c]=new P(n.add(o.rotate((c+u)*s)));return t(l,!0,arguments)},Star:function(){for(var n=h.readNamed(arguments,"center"),i=2*e.readNamed(arguments,"points"),r=e.readNamed(arguments,"radius1"),s=e.readNamed(arguments,"radius2"),a=360/i,o=new h(0,-1),u=Array(i),l=0;i>l;l++)u[l]=new P(n.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var O=z.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||("string"==typeof t?this.setPathData(t):this.addChildren(Array.isArray(t)?t:arguments))},insertChildren:function mt(e,n,i){for(var r=n.length-1;r>=0;r--){var s=n[r];s instanceof O&&(n.splice.apply(n,[r,1].concat(s.removeChildren())),s.remove())}n=mt.base.call(this,e,n,i,A);for(var r=0,a=!i&&n&&n.length;a>r;r++){var s=n[r];s._clockwise===t&&s.setClockwise(0===s._index)}return n},reverse:function(){for(var t=this._children,e=0,n=t.length;n>e;e++)t[e].reverse()},smooth:function(){for(var t=0,e=this._children.length;e>t;t++)this._children[t].smooth()},reduce:function yt(){for(var t=this._children,e=t.length-1;e>=0;e--){var n=t[e].reduce();n.isEmpty()&&t.splice(e,1)}if(0===t.length){var n=new A(m.NO_INSERT);return n.insertAbove(this),n.setStyle(this._style),this.remove(),n}return yt.base.call(this)},isClockwise:function(){var t=this.getFirstChild();return t&&t.isClockwise()},setClockwise:function(t){this.isClockwise()!==!!t&&this.reverse()},getFirstSegment:function(){var t=this.getFirstChild();return t&&t.getFirstSegment()},getLastSegment:function(){var t=this.getLastChild();return t&&t.getLastSegment()},getCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n].getCurves());return e},getFirstCurve:function(){var t=this.getFirstChild();return t&&t.getFirstCurve()},getLastCurve:function(){var t=this.getLastChild();return t&&t.getFirstCurve()},getArea:function(){for(var t=this._children,e=0,n=0,i=t.length;i>n;n++)e+=t[n].getArea();return e}},{beans:!0,getPathData:function(t,e){for(var n=this._children,i=[],r=0,s=n.length;s>r;r++){var a=n[r],o=a._matrix;i.push(a.getPathData(t&&!o.isIdentity()?t.chain(o):t,e))}return i.join(" ")}},{_getChildHitTestOptions:function(t){return t["class"]===A||"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e,n){var i=this._children;if(0!==i.length){if(this._currentPath)t.currentPath=this._currentPath;else{e=e.extend({dontStart:!0,dontFinish:!0}),t.beginPath();for(var r=0,s=i.length;s>r;r++)i[r].draw(t,e,n);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var a=this._style;a.hasFill()&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),a.hasStroke()&&t.stroke()}}},_drawSelected:function(t,e,n){for(var i=this._children,r=0,s=i.length;s>r;r++){ +var a=i[r],o=a._matrix;n[a._id]||a._drawSelected(t,o.isIdentity()?e:e.chain(o))}}},new function(){function t(t,e){var n=t._children;if(e&&0===n.length)throw Error("Use a moveTo() command first");return n[n.length-1]}var n={moveTo:function(){var e=t(this),n=e&&e.isEmpty()?e:new A(m.NO_INSERT);n!==e&&this.addChild(n),n.moveTo.apply(n,arguments)},moveBy:function(){var e=t(this,!0),n=e&&e.getLastSegment(),i=h.read(arguments);this.moveTo(n?i.add(n._point):i)},closePath:function(e){t(this,!0).closePath(e)}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){n[e]=function(){var n=t(this,!0);n[e].apply(n,arguments)}}),n});z.inject(new function(){function e(t,e){var n=t.clone(!1).reduce().transform(null,!0,!0);return e?n.resolveCrossings().reorient():n}function n(t,e,n,i,r){var s=new t(m.NO_INSERT);return s.addChildren(e,!0),r&&(s=s.reduce()),s.insertAbove(i&&n.isSibling(i)&&n.getIndex()e;e++){var i=t[e];f.push.apply(f,i._segments),g.push.apply(g,i._getMonoCurves())}}if(!t._children&&!t._closed)return r(t,i,s);var h=e(t,!0),u=i&&t!==i&&e(i,!0);u&&/^(subtract|exclude)$/.test(s)^u.isClockwise()!==h.isClockwise()&&u.reverse();var d=M.expand(h.getIntersections(u,function(t){return u&&t.isOverlap()||t.isCrossing()}));o(d);var f=[],g=[];a(h._children||[h]),u&&a(u._children||[u]);for(var _=0,v=d.length;v>_;_++)l(d[_]._segment,h,u,g,s);for(var _=0,v=f.length;v>_;_++){var p=f[_];null==p._winding&&l(p,h,u,g,s)}return n(O,c(f,s),t,i,!0)}function r(i,r,s){function a(e){return h.contains(e.getPointAt(e.getLength()/2))^l?(c.unshift(e),!0):t}if(!r||!r._children&&!r._closed||!/^(subtract|intersect)$/.test(s))return null;for(var o=e(i,!1),h=e(r,!1),u=o.getIntersections(h,function(t){return t.isOverlap()||t.isCrossing()}),l="subtract"===s,c=[],d=u.length-1;d>=0;d--){var f=u[d].split();f&&(a(f)&&f.getFirstSegment().setHandleIn(0,0),o.getLastSegment().setHandleOut(0,0))}return a(o),n(y,c,i,r)}function s(t,e){for(var n=t;n;){if(n===e)return;n=n._prev}for(;t._next&&t._next!==e;)t=t._next;if(!t._next){for(;e._prev;)e=e._prev;t._next=e,e._prev=t}}function o(t){for(var e,n,i=4e-7,r=1-i,a=!1,o=[],h=t.length-1;h>=0;h--){var u=t[h],l=u._curve,c=u._parameter,d=c;l!==e?a=!l.hasHandles():n>0&&(c/=n);var f;i>c?f=l._segment1:c>r?f=l._segment2:(f=l.divide(c,!0,!0)._segment1,a&&o.push(f)),u._setSegment(f);var g=f._intersection,_=u._intersection;if(g){s(g,_);for(var v=g;v;)s(v._intersection,g),v=v._next}else f._intersection=_;e=l,n=d}for(var h=0,p=o.length;p>h;h++)o[h].clearHandles()}function u(t,e,n,i){var r=2e-7,s=4e-7,o=1-s,l=t.x,c=t.y,d=0,f=0,g=[],_=Math.abs;if(n){for(var v=-(1/0),p=1/0,m=c-r,y=c+r,w=0,x=e.length;x>w;w++){var b=e[w].values;if(I.solveCubic(b,0,l,g,0,1)>0)for(var C=g.length-1;C>=0;C--){var S=I.getPoint(b,g[C]).y;m>S&&S>v?v=S:S>y&&p>S&&(p=S)}}v=(v+c)/2,p=(p+c)/2,v>-(1/0)&&(d=u(new h(l,v),e,!1,i)),1/0>p&&(f=u(new h(l,p),e,!1,i))}else for(var P,k,M=l-r,z=l+r,A=!1,w=0,x=e.length;x>w;w++){var O=e[w],b=O.values,T=O.winding;if(T&&(1===T&&c>=b[1]&&c<=b[7]||c>=b[7]&&c<=b[1])&&1===I.solveCubic(b,1,c,g,0,1)){var E=g[0];if(!(E>o&&A&&O.next!==e[w+1]||s>E&&k>o&&O.previous===P)){var L=I.getPoint(b,E).x,N=I.getTangent(b,E).y,j=!1;a.isZero(N)&&!I.isStraight(b)||s>E&&N*I.getTangent(O.previous.values,1).y<0||E>o&&N*I.getTangent(O.next.values,0).y<0?i&&L>=M&&z>=L&&(++d,++f,j=!0):M>=L?(d+=T,j=!0):L>=z&&(f+=T,j=!0),O.previous!==e[w-1]&&(A=s>E&&j)}P=O,k=E}}return Math.max(_(d),_(f))}function l(t,e,n,i,r){var s=2e-7,a=[],o=t,h=0,l=0;do{var c=t.getCurve(),d=c.getLength();a.push({segment:t,curve:c,length:d}),h+=d,t=t.getNext()}while(t&&!t._intersection&&t!==o);for(var f=0;3>f;f++)for(var d=h*(f+1)/4,g=0,_=a.length;_>g;g++){var v=a[g],p=v.length;if(p>=d){(s>d||s>p-d)&&(d=p/2);var c=v.curve,m=c._path,y=m._parent,w=c.getPointAt(d),x=c.isHorizontal();y instanceof O&&(m=y),l+="subtract"===r&&n&&(m===e&&n._getWinding(w,x)||m===n&&!e._getWinding(w,x))?0:u(w,i,x);break}d-=p}for(var b=Math.round(l/3),C=a.length-1;C>=0;C--)a[C].segment._winding=b}function c(t,e){function n(t,e){if(t._visited)return!1;if(!l)return!0;var n=t._winding,i=t._intersection;return i&&e&&c&&i.isOverlap()&&(n=c[n]||n),l(n)}function i(t){return t===o||t===h}function r(t,e){if(!t._next)return t;for(;t;){var r=t._segment,s=r.getNext(),a=s._intersection;if(i(s)||!r._visited&&!s._visited&&(!l||(!e||n(r))&&(!(e&&a&&a.isOverlap())&&n(s)||!e&&a&&n(a._segment))))return t;t=t._next}return null}function s(t,e){for(;t;){var n=t._segment;if(i(n))return n;t=t[e?"_next":"_prev"]}}for(var o,h,u=[],l=d[e],c={unite:{1:2},intersect:{2:1}}[e],f=0,g=t.length;g>f;f++){var _=t[f],v=null,p=!1;if(n(_,!0)){for(o=h=null;!p;){var y=_._intersection,w=v&&_._handleIn;y=y&&(r(y,!0)||r(y,!1))||y;var x=y&&y._segment;if(x&&n(x)&&(_=x),_._visited){if(p=i(_),!p&&y){var b=s(y,!0)||s(y,!1);b&&(_=b,p=!0)}break}v||(v=new A(m.NO_INSERT),o=_,h=x),v.add(new P(_._point,w,_._handleOut)),_._visited=!0,_=_.getNext(),p=i(_)}p?(v.firstSegment.setHandleIn(_._handleIn),v.setClosed(!0)):v&&(console.error("Boolean operation resulted in open path","segments =",v._segments.length,"length =",v.getLength()),v=null),v&&(v._segments.length>8||!a.isZero(v.getArea()))&&(u.push(v),v=null)}}return u}var d={unite:function(t){return 1===t||0===t},intersect:function(t){return 2===t},subtract:function(t){return 1===t},exclude:function(t){return 1===t}};return{_getWinding:function(t,e,n){return u(t,this._getMonoCurves(),e,n)},unite:function(t){return i(this,t,"unite")},intersect:function(t){return i(this,t,"intersect")},subtract:function(t){return i(this,t,"subtract")},exclude:function(t){return i(this,t,"exclude")},divide:function(t){return n(y,[this.subtract(t),this.intersect(t)],this,t,!0)},resolveCrossings:function(){var t=this.getCrossings();if(!t.length)return this;o(M.expand(t));for(var e=this._children||[this],i=[],r=0,s=e.length;s>r;r++)i.push.apply(i,e[r]._segments);return n(O,c(i),this,null,!1)}}}),A.inject({_getMonoCurves:function(){function t(t){var e=t[1],r=t[7],s={values:t,winding:e===r?0:e>r?-1:1,previous:n,next:null};n&&(n.next=s),i.push(s),n=s}function e(e){if(0!==I.getLength(e)){var n=e[1],i=e[3],r=e[5],s=e[7];if(I.isStraight(e))t(e);else{var o=3*(i-r)-n+s,h=2*(n+r)-4*i,u=i-n,l=4e-7,c=1-l,d=[],f=a.solveQuadratic(o,h,u,d,l,c);if(0===f)t(e);else{d.sort();var g=d[0],_=I.subdivide(e,g);t(_[0]),f>1&&(g=(d[1]-g)/(1-g),_=I.subdivide(_[1],g),t(_[0])),t(_[1])}}}}var n,i=this._monoCurves;if(!i){i=this._monoCurves=[];for(var r=this.getCurves(),s=this._segments,o=0,h=r.length;h>o;o++)e(r[o].getValues());if(!this._closed&&s.length>1){var u=s[s.length-1]._point,l=s[0]._point,c=u._x,d=u._y,f=l._x,g=l._y;e([c,d,c,d,f,g,f,g])}if(i.length>0){var _=i[0],v=i[i.length-1];_.previous=v,v.next=_}}return i},getInteriorPoint:function(){var t=this.getBounds(),e=t.getCenter(!0);if(!this.contains(e)){for(var n=this._getMonoCurves(),i=[],r=e.y,s=[],a=0,o=n.length;o>a;a++){var h=n[a].values;if((1===n[a].winding&&r>=h[1]&&r<=h[7]||r>=h[7]&&r<=h[1])&&I.solveCubic(h,1,r,i,0,1)>0)for(var u=i.length-1;u>=0;u--)s.push(I.getPoint(h,i[u]).x);if(s.length>1)break}e.x=(s[0]+s[1])/2}return e},reorient:function(){return this.setClockwise(!0),this}}),O.inject({_getMonoCurves:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push.apply(e,t[n]._getMonoCurves());return e},reorient:function(){var t=this.removeChildren().sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()});if(t.length>0){this.addChildren(t);for(var e=t[0].isClockwise(),n=1,i=t.length;i>n;n++){for(var r=t[n].getInteriorPoint(),s=0,a=n-1;a>=0;a--)t[a].contains(r)&&s++;t[n].setClockwise(s%2===0&&e)}}return this}});var T=e.extend({_class:"PathIterator",initialize:function(t,e,n,i){function r(t,e){var n=I.getValues(t,e,i);o.push(n),s(n,t._index,0,1)}function s(t,e,i,r){if(r-i>l&&!I.isFlatEnough(t,n||.25)){var a=I.subdivide(t,.5),o=(i+r)/2;s(a[0],e,i,o),s(a[1],e,o,r)}else{var c=t[6]-t[0],d=t[7]-t[1],f=Math.sqrt(c*c+d*d);f>1e-6&&(u+=f,h.push({offset:u,value:r,index:e}))}}for(var a,o=[],h=[],u=0,l=1/(e||32),c=t._segments,d=c[0],f=1,g=c.length;g>f;f++)a=c[f],r(d,a),d=a;t._closed&&r(a,c[0]),this.curves=o,this.parts=h,this.length=u,this.index=0},getParameterAt:function(t){for(var e,n=this.index;e=n,!(0==n||this.parts[--n].offsete;e++){var r=this.parts[e];if(r.offset>=t){this.index=e;var s=this.parts[e-1],a=s&&s.index==r.index?s.value:0,o=s?s.offset:0;return{value:a+(r.value-a)*(t-o)/(r.offset-o),index:r.index}}}var r=this.parts[this.parts.length-1];return{value:1,index:r.index}},drawPart:function(t,e,n){e=this.getParameterAt(e),n=this.getParameterAt(n);for(var i=e.index;i<=n.index;i++){var r=I.getPart(this.curves[i],i==e.index?e.value:0,i==n.index?n.value:1);i==e.index&&t.moveTo(r[0],r[1]),t.bezierCurveTo.apply(t,r.slice(2))}}},e.each(I.evaluateMethods,function(t){this[t+"At"]=function(e,n){var i=this.getParameterAt(e);return I[t](this.curves[i.index],i.value,n)}},{})),E=e.extend({initialize:function(t,e){for(var n,i=this.points=[],r=t._segments,s=0,a=r.length;a>s;s++){var o=r[s].point.clone();n&&n.equals(o)||(i.push(o),n=o)}t._closed&&(this.closed=!0,i.unshift(i[i.length-1]),i.push(i[1])),this.error=e},fit:function(){var t=this.points,e=t.length,n=this.segments=e>0?[new P(t[0])]:[];return e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.closed&&(n.shift(),n.pop()),n},fitCubic:function(e,n,i,r){if(n-e==1){var s=this.points[e],a=this.points[n],o=s.getDistance(a)/3;return this.addCurve([s,s.add(i.normalize(o)),a.add(r.normalize(o)),a]),t}for(var h,u=this.chordLengthParameterize(e,n),l=Math.max(this.error,this.error*this.error),c=!0,d=0;4>=d;d++){var f=this.generateBezier(e,n,u,i,r),g=this.findMaxError(e,n,f,u);if(g.error=l)break;c=this.reparameterize(e,n,u,f),l=g.error}var _=this.points[h-1].subtract(this.points[h]),v=this.points[h].subtract(this.points[h+1]),p=_.add(v).divide(2).normalize();this.fitCubic(e,h,i,p),this.fitCubic(h,n,p.negate(),r)},addCurve:function(t){var e=this.segments[this.segments.length-1];e.setHandleOut(t[1].subtract(t[0])),this.segments.push(new P(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-12,a=this.points[t],o=this.points[e],h=[[0,0],[0,0]],u=[0,0],l=0,c=e-t+1;c>l;l++){var d=n[l],f=1-d,g=3*d*f,_=f*f*f,v=g*f,p=g*d,m=d*d*d,y=i.normalize(v),w=r.normalize(p),x=this.points[t+l].subtract(a.multiply(_+v)).subtract(o.multiply(p+m));h[0][0]+=y.dot(y),h[0][1]+=y.dot(w),h[1][0]=h[0][1],h[1][1]+=w.dot(w),u[0]+=y.dot(x),u[1]+=w.dot(x)}var b,C,S=h[0][0]*h[1][1]-h[1][0]*h[0][1];if(Math.abs(S)>s){var P=h[0][0]*u[1]-h[1][0]*u[0],k=u[0]*h[1][1]-u[1]*h[0][1];b=k/S,C=P/S}else{var I=h[0][0]+h[0][1],M=h[1][0]+h[1][1];b=C=Math.abs(I)>s?u[0]/I:Math.abs(M)>s?u[1]/M:0}var z,A,O=o.getDistance(a),T=s*O;if(T>b||T>C)b=C=O/3;else{var E=o.subtract(a);z=i.normalize(b),A=r.normalize(C),z.dot(E)-A.dot(E)>O*O&&(b=C=O/3,z=A=null)}return[a,a.add(z||i.normalize(b)),o.add(A||r.normalize(C)),o]},reparameterize:function(t,e,n,i){for(var r=t;e>=r;r++)n[r-t]=this.findRoot(i,this.points[r],n[r-t]);for(var r=1,s=n.length;s>r;r++)if(n[r]<=n[r-1])return!1;return!0},findRoot:function(t,e,n){for(var i=[],r=[],s=0;2>=s;s++)i[s]=t[s+1].subtract(t[s]).multiply(3);for(var s=0;1>=s;s++)r[s]=i[s+1].subtract(i[s]).multiply(2);var a=this.evaluate(3,t,n),o=this.evaluate(2,i,n),h=this.evaluate(1,r,n),u=a.subtract(e),l=o.dot(o)+u.dot(h);return Math.abs(l)<1e-6?n:n-u.dot(o)/l},evaluate:function(t,e,n){for(var i=e.slice(),r=1;t>=r;r++)for(var s=0;t-r>=s;s++)i[s]=i[s].multiply(1-n).add(i[s+1].multiply(n));return i[0]},chordLengthParameterize:function(t,e){for(var n=[0],i=t+1;e>=i;i++)n[i-t]=n[i-t-1]+this.points[i].getDistance(this.points[i-1]);for(var i=1,r=e-t;r>=i;i++)n[i]/=n[r];return n},findMaxError:function(t,e,n,i){for(var r=Math.floor((e-t+1)/2),s=0,a=t+1;e>a;a++){var o=this.evaluate(3,n,i[a-t]),h=o.subtract(this.points[a]),u=h.x*h.x+h.y*h.y;u>=s&&(s=u,r=a)}return{error:s,index:r}}}),L=m.extend({_class:"TextItem",_boundsSelected:!0,_applyMatrix:!1,_canApplyMatrix:!1,_serializeFields:{content:null},_boundsGetter:"getBounds",initialize:function(n){this._content="",this._lines=[];var i=n&&e.isPlainObject(n)&&n.x===t&&n.y===t;this._initialize(i&&n,!i&&h.read(arguments))},_equals:function(t){return this._content===t._content},_clone:function wt(t,e,n){return t.setContent(this._content),wt.base.call(this,t,e,n)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(265)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),N=L.extend({_class:"PointText",initialize:function(){L.apply(this,arguments)},clone:function(t){return this._clone(new N(m.NO_INSERT),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(){var t=h.read(arguments);this.translate(t.subtract(this._matrix.getTranslation()))},_draw:function(t){if(this._content){this._setStyles(t);var e=this._style,n=this._lines,i=e.getLeading(),r=t.shadowColor;t.font=e.getFontStyle(),t.textAlign=e.getJustification();for(var s=0,a=n.length;a>s;s++){t.shadowColor=r;var o=n[s];e.hasFill()&&(t.fillText(o,0,0),t.shadowColor="rgba(0,0,0,0)"),e.hasStroke()&&t.strokeText(o,0,0),t.translate(0,i)}}},_getBounds:function(t,e){var n=this._style,i=this._lines,r=i.length,s=n.getJustification(),a=n.getLeading(),o=this.getView().getTextWidth(n.getFontStyle(),i),h=0;"left"!==s&&(h-=o/("center"===s?2:1));var u=new d(h,r?-.75*a:0,o,r*a);return e?e._transformBounds(u,u):u}}),j=e.extend(new function(){function t(t){var e,i=t.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/);if(i){e=[0,0,0];for(var r=0;3>r;r++){var s=i[r+1];e[r]=parseInt(1==s.length?s+s:s,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var s=+e[r];e[r]=3>r?s/255:s}}else{var h=a[t];if(!h){n||(n=Y.getContext(1,1),n.globalCompositeOperation="copy"),n.fillStyle="rgba(0,0,0,0)",n.fillStyle=t,n.fillRect(0,0,1,1);var u=n.getImageData(0,0,1,1).data;h=a[t]=[u[0]/255,u[1]/255,u[2]/255]}e=h.slice()}return e}var n,i={gray:["gray"],rgb:["red","green","blue"],hsb:["hue","saturation","brightness"],hsl:["hue","saturation","lightness"],gradient:["gradient","origin","destination","highlight"]},r={},a={},u=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],l={"rgb-hsb":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4);return[a,0===i?0:s/i,i]},"hsb-rgb":function(t,e,n){t=(t/60%6+6)%6;var i=Math.floor(t),r=t-i,i=u[i],s=[n,n*(1-e),n*(1-e*r),n*(1-e*(1-r))];return[s[i[0]],s[i[1]],s[i[2]]]},"rgb-hsl":function(t,e,n){var i=Math.max(t,e,n),r=Math.min(t,e,n),s=i-r,a=0===s,o=a?0:60*(i==t?(e-n)/s+(n>e?6:0):i==e?(n-t)/s+2:(t-e)/s+4),h=(i+r)/2,u=a?0:.5>h?s/(i+r):s/(2-i-r);return[o,u,h]},"hsl-rgb":function(t,e,n){if(t=(t/360%1+1)%1,0===e)return[n,n,n];for(var i=[t+1/3,t,t-1/3],r=.5>n?n*(1+e):n+e-n*e,s=2*n-r,a=[],o=0;3>o;o++){var h=i[o];0>h&&(h+=1),h>1&&(h-=1),a[o]=1>6*h?s+6*(r-s)*h:1>2*h?r:2>3*h?s+(r-s)*(2/3-h)*6:s}return a},"rgb-gray":function(t,e,n){return[.2989*t+.587*e+.114*n]},"gray-rgb":function(t){return[t,t,t]},"gray-hsb":function(t){return[0,0,t]},"gray-hsl":function(t){return[0,0,t]},"gradient-rgb":function(){return[]},"rgb-gradient":function(){return[]}};return e.each(i,function(t,n){r[n]=[],e.each(t,function(t,s){var a=e.capitalize(t),o=/^(hue|saturation)$/.test(t),u=r[n][s]="gradient"===t?function(t){var e=this._components[0];return t=B.read(Array.isArray(t)?t:arguments,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"gradient"===n?function(){return h.read(arguments,0,{readNull:"highlight"===t,clone:!0})}:function(t){return null==t||isNaN(t)?0:t};this["get"+a]=function(){return this._type===n||o&&/^hs[bl]$/.test(this._type)?this._components[s]:this._convert(n)[s]},this["set"+a]=function(t){this._type===n||o&&/^hs[bl]$/.test(this._type)||(this._components=this._convert(n),this._properties=i[n],this._type=n),this._components[s]=u.call(this,t),this._changed()}},this)},{_class:"Color",_readIndex:!0,initialize:function c(e){var n,s,a,h,u=Array.prototype.slice,l=arguments,d=0;Array.isArray(e)&&(l=e,e=l[0]);var f=null!=e&&typeof e;if("string"===f&&e in i&&(n=e,e=l[1],Array.isArray(e)?(s=e,a=l[2]):(this.__read&&(d=1),l=u.call(l,1),f=typeof e)),!s){if(h="number"===f?l:"object"===f&&null!=e.length?e:null){n||(n=h.length>=3?"rgb":"gray");var g=i[n].length;a=h[g],this.__read&&(d+=h===arguments?g+(null!=a?1:0):1),h.length>g&&(h=u.call(h,0,g))}else if("string"===f)n="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===f)if(e.constructor===c){if(n=e._type,s=e._components.slice(),a=e._alpha,"gradient"===n)for(var _=1,v=s.length;v>_;_++){var p=s[_];p&&(s[_]=p.clone())}}else if(e.constructor===B)n="gradient",h=l;else{n="hue"in e?"lightness"in e?"hsl":"hsb":"gradient"in e||"stops"in e||"radial"in e?"gradient":"gray"in e?"gray":"rgb";var m=i[n],y=r[n];this._components=s=[];for(var _=0,v=m.length;v>_;_++){var w=e[m[_]];null==w&&0===_&&"gradient"===n&&"stops"in e&&(w={stops:e.stops,radial:e.radial}),w=y[_].call(this,w),null!=w&&(s[_]=w)}a=e.alpha}this.__read&&n&&(d=1)}if(this._type=n||"rgb",this._id=o.get(c),!s){this._components=s=[];for(var y=r[this._type],_=0,v=y.length;v>_;_++){var w=y[_].call(this,h&&h[_]);null!=w&&(s[_]=w)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=d)},_serialize:function(t,n){var i=this.getComponents();return e.serialize(/^(gray|rgb)$/.test(this._type)?i:[this._type].concat(i),t,!0,n)},_changed:function(){this._canvasStyle=null,this._owner&&this._owner._changed(65)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=l[this._type+"-"+t])?e.apply(this,this._components):l["rgb-"+t].apply(this,l[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new j(t,this._convert(t),this._alpha)},getType:function(){return this._type},setType:function(t){this._components=this._convert(t),this._properties=i[t],this._type=t},getComponents:function(){var t=this._components.slice();return null!=this._alpha&&t.push(this._alpha),t},getAlpha:function(){return null!=this._alpha?this._alpha:1},setAlpha:function(t){this._alpha=null==t?null:Math.min(Math.max(t,0),1),this._changed()},hasAlpha:function(){return null!=this._alpha},equals:function(t){var n=e.isPlainValue(t,!0)?j.read(arguments):t;return n===this||n&&this._class===n._class&&this._type===n._type&&this._alpha===n._alpha&&e.equals(this._components,n._components)||!1},toString:function(){for(var t=this._properties,e=[],n="gradient"===this._type,i=s.instance,r=0,a=t.length;a>r;r++){var o=this._components[r];null!=o&&e.push(t[r]+": "+(n?o:i.number(o)))}return null!=this._alpha&&e.push("alpha: "+i.number(this._alpha)),"{ "+e.join(", ")+" }"},toCSS:function(t){function e(t){return Math.round(255*(0>t?0:t>1?1:t))}var n=this._convert("rgb"),i=t||null==this._alpha?1:this._alpha;return n=[e(n[0]),e(n[1]),e(n[2])],1>i&&n.push(0>i?0:i),t?"#"+((1<<24)+(n[0]<<16)+(n[1]<<8)+n[2]).toString(16).slice(1):(4==n.length?"rgba(":"rgb(")+n.join(",")+")"},toCanvasStyle:function(t){if(this._canvasStyle)return this._canvasStyle;if("gradient"!==this._type)return this._canvasStyle=this.toCSS();var e,n=this._components,i=n[0],r=i._stops,s=n[1],a=n[2];if(i._radial){var o=a.getDistance(s),h=n[3];if(h){var u=h.subtract(s);u.getLength()>o&&(h=s.add(u.normalize(o-.1)))}var l=h||s;e=t.createRadialGradient(l.x,l.y,0,s.x,s.y,o)}else e=t.createLinearGradient(s.x,s.y,a.x,a.y);for(var c=0,d=r.length;d>c;c++){var f=r[c];e.addColorStop(f._rampPoint,f._color.toCanvasStyle())}return this._canvasStyle=e},transform:function(t){if("gradient"===this._type){for(var e=this._components,n=1,i=e.length;i>n;n++){var r=e[n];t._transformPoint(r,r,!0)}this._changed()}},statics:{_types:i,random:function(){var t=Math.random;return new j(t(),t(),t())}}})},new function(){var t={add:function(t,e){return t+e},subtract:function(t,e){return t-e},multiply:function(t,e){return t*e},divide:function(t,e){return t/e}};return e.each(t,function(t,e){this[e]=function(e){e=j.read(arguments);for(var n=this._type,i=this._components,r=e._convert(n),s=0,a=i.length;a>s;s++)r[s]=t(i[s],r[s]);return new j(n,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})}),B=e.extend({_class:"Gradient",initialize:function(t,e){this._id=o.get(),t&&this._set(t)&&(t=e=null),this._stops||this.setStops(t||["white","black"]),null==this._radial&&this.setRadial("string"==typeof e&&"radial"===e||e||!1)},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._stops,this._radial],t,!0,n)})},_changed:function(){for(var t=0,e=this._owners&&this._owners.length;e>t;t++)this._owners[t]._changed()},_addOwner:function(t){this._owners||(this._owners=[]),this._owners.push(t)},_removeOwner:function(e){var n=this._owners?this._owners.indexOf(e):-1;-1!=n&&(this._owners.splice(n,1),0===this._owners.length&&(this._owners=t))},clone:function(){for(var t=[],e=0,n=this._stops.length;n>e;e++)t[e]=this._stops[e].clone();return new B(t,this._radial)},getStops:function(){return this._stops},setStops:function(e){if(this.stops)for(var n=0,i=this._stops.length;i>n;n++)this._stops[n]._owner=t;if(e.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=R.readAll(e,0,{clone:!0});for(var n=0,i=this._stops.length;i>n;n++){var r=this._stops[n];r._owner=this,r._defaultRamp&&r.setRampPoint(n/(i-1))}this._changed()},getRadial:function(){return this._radial},setRadial:function(t){this._radial=t,this._changed()},equals:function(t){if(t===this)return!0;if(t&&this._class===t._class&&this._stops.length===t._stops.length){for(var e=0,n=this._stops.length;n>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),R=e.extend({_class:"GradientStop",initialize:function(e,n){if(e){var i,r;n===t&&Array.isArray(e)?(i=e[0],r=e[1]):e.color?(i=e.color,r=e.rampPoint):(i=e,r=n),this.setColor(i),this.setRampPoint(r)}},clone:function(){return new R(this._color.clone(),this._rampPoint)},_serialize:function(t,n){return e.serialize([this._color,this._rampPoint],t,!0,n)},_changed:function(){this._owner&&this._owner._changed(65)},getRampPoint:function(){return this._rampPoint},setRampPoint:function(t){this._defaultRamp=null==t,this._rampPoint=t||0,this._changed()},getColor:function(){return this._color},setColor:function(t){this._color=j.read(arguments),this._color===t&&(this._color=t.clone()),this._color._owner=this,this._changed()},equals:function(t){return t===this||t&&this._class===t._class&&this._color.equals(t._color)&&this._rampPoint==t._rampPoint||!1}}),D=e.extend(new function(){var n={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",strokeScaling:!0,miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,fontFamily:"sans-serif",fontWeight:"normal",fontSize:12,font:"sans-serif",leading:null,justification:"left"},i={strokeWidth:97,strokeCap:97,strokeJoin:97,strokeScaling:105,miterLimit:97,fontFamily:9,fontWeight:9,fontSize:9,font:9,leading:9,justification:9},r={beans:!0},s={_defaults:n,_textDefaults:new e(n,{fillColor:new j}),beans:!0};return e.each(n,function(n,a){var o=/Color$/.test(a),u="shadowOffset"===a,l=e.capitalize(a),c=i[a],d="set"+l,f="get"+l;s[d]=function(e){var n=this._owner,i=n&&n._children;if(i&&i.length>0&&!(n instanceof O))for(var r=0,s=i.length;s>r;r++)i[r]._style[d](e);else{var h=this._values[a];h!==e&&(o&&(h&&(h._owner=t),e&&e.constructor===j&&(e._owner&&(e=e.clone()),e._owner=n)),this._values[a]=e,n&&n._changed(c||65))}},s[f]=function(n){var i,r=this._owner,s=r&&r._children;if(!s||0===s.length||n||r instanceof O){var i=this._values[a];if(i===t)i=this._defaults[a],i&&i.clone&&(i=i.clone());else{var l=o?j:u?h:null;!l||i&&i.constructor===l||(this._values[a]=i=l.read([i],0,{readNull:!0,clone:!0}),i&&o&&(i._owner=r))}return i}for(var c=0,d=s.length;d>c;c++){var g=s[c]._style[f]();if(0===c)i=g;else if(!e.equals(i,g))return t}return i},r[f]=function(t){return this._style[f](t)},r[d]=function(t){this._style[d](t)}}),m.inject(r),s},{_class:"Style",initialize:function(t,e,n){this._values={},this._owner=e,this._project=e&&e._project||n||paper.project,e instanceof L&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof D,n=e?t._values:t;if(n)for(var i in n)if(i in this._defaults){var r=n[i];this[i]=r&&e&&r.clone?r.clone():r}},equals:function(t){return t===this||t&&this._class===t._class&&e.equals(this._values,t._values)||!1},hasFill:function(){return!!this.getFillColor()},hasStroke:function(){return!!this.getStrokeColor()&&this.getStrokeWidth()>0},hasShadow:function(){return!!this.getShadowColor()&&this.getShadowBlur()>0},getView:function(){return this._project.getView()},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function xt(){var t=xt.base.call(this),e=this.getFontSize();return/pt|em|%|px/.test(e)&&(e=this.getView().getPixelSize(e)),null!=t?t:1.2*e}}),F=new function(){function t(t,e,n,i){for(var r=["","webkit","moz","Moz","ms","o"],s=e[0].toUpperCase()+e.substring(1),a=0;6>a;a++){var o=r[a],h=o?o+s:e;if(h in t){if(!n)return t[h];t[h]=i;break}}}return{getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getBounds:function(t,e){var n,i=t.ownerDocument,r=i.body,s=i.documentElement;try{n=t.getBoundingClientRect()}catch(a){n={left:0,top:0,width:0,height:0}}var o=n.left-(s.clientLeft||r.clientLeft||0),h=n.top-(s.clientTop||r.clientTop||0);if(!e){var u=i.defaultView;o+=u.pageXOffset||s.scrollLeft||r.scrollLeft,h+=u.pageYOffset||s.scrollTop||r.scrollTop}return new d(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new d(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return F.getBounds(t,e).getPoint()},getSize:function(t){return F.getBounds(t,!0).getSize()},isInvisible:function(t){return F.getSize(t).equals(new l(0,0))},isInView:function(t){return!F.isInvisible(t)&&F.getViewportBounds(t).intersects(F.getBounds(t,!0))},getPrefixed:function(e,n){return t(e,n)},setPrefixed:function(e,n,i){if("object"==typeof n)for(var r in n)t(e,r,!0,n[r]);else t(e,n,!0,i)}}},q={add:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.addEventListener(r[s],i,!1)},remove:function(t,e){for(var n in e)for(var i=e[n],r=n.split(/[\s,]+/g),s=0,a=r.length;a>s;s++)t.removeEventListener(r[s],i,!1)},getPoint:function(t){var e=t.targetTouches?t.targetTouches.length?t.targetTouches[0]:t.changedTouches[0]:t;return new h(e.pageX||e.clientX+document.documentElement.scrollLeft,e.pageY||e.clientY+document.documentElement.scrollTop)},getTarget:function(t){return t.target||t.srcElement},getRelatedTarget:function(t){return t.relatedTarget||t.toElement},getOffset:function(t,e){return q.getPoint(t).subtract(F.getOffset(e||q.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};q.requestAnimationFrame=new function(){function t(){for(var e=s.length-1;e>=0;e--){var o=s[e],h=o[0],u=o[1];(!u||("true"==i.getAttribute(u,"keepalive")||a)&&F.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):r=!1)}var e,n=F.getPrefixed(window,"requestAnimationFrame"),r=!1,s=[],a=!0;return q.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(i,a){s.push([i,a]),n?r||(n(t),r=!0):e||(e=setInterval(t,1e3/60))}};var V=e.extend(n,{_class:"View",initialize:function bt(t,e){function n(t){return e[t]||parseInt(e.getAttribute(t),10)}function r(){var t=F.getSize(e);return t.isNaN()||t.isZero()?new l(n("width"),n("height")):t}this._project=t,this._scope=t._scope,this._element=e;var s;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=e.getAttribute("id"),null==this._id&&e.setAttribute("id",this._id="view-"+bt._id++),q.add(e,this._viewEvents);var a="none";if(F.setPrefixed(e.style,{userSelect:a,touchAction:a,touchCallout:a,contentZooming:a,userDrag:a,tapHighlightColor:"rgba(0,0,0,0)"}),i.hasAttribute(e,"resize")){var o=this;q.add(window,this._windowEvents={resize:function(){o.setViewSize(r())}})}if(this._setViewSize(s=r()),i.hasAttribute(e,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var h=this._stats.domElement,u=h.style,c=F.getOffset(e);u.position="absolute",u.left=c.x+"px",u.top=c.y+"px",document.body.appendChild(h)}bt._views.push(this),bt._viewsById[this._id]=this,this._viewSize=s,(this._matrix=new g)._owner=this,this._zoom=1,bt._focused||(bt._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(V._focused===this&&(V._focused=null),V._views.splice(V._views.indexOf(this),1),delete V._viewsById[this._id],this._project._view===this&&(this._project._view=null),q.remove(this._element,this._viewEvents),q.remove(window,this._windowEvents),this._element=this._project=null,this.off("frame"),this._animate=!1,this._frameItems={},!0):!1},_events:e.each(["onResize","onMouseDown","onMouseUp","onMouseMove"],function(t){this[t]={install:function(t){this._installEvent(t)},uninstall:function(t){this._uninstallEvent(t)}}},{onFrame:{install:function(){this.play()},uninstall:function(){this.pause()}}}),_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;q.requestAnimationFrame(function(){t._requested=!1,t._animate&&(t._requestFrame(),t._handleFrame())},this._element),this._requested=!0},_handleFrame:function(){paper=this._scope;var t=Date.now()/1e3,n=this._before?t-this._before:0;this._before=t,this._handlingFrame=!0,this.emit("frame",new e({delta:n,time:this._time+=n,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.update()},_animateItem:function(t,e){var n=this._frameItems;e?(n[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.on("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.off("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.emit("frame",new e(t,{time:i.time+=t.delta,count:i.count++}))}},_update:function(){this._project._needsUpdate=!0,this._handlingFrame||(this._animate?this._handleFrame():this.update())},_changed:function(t){1&t&&(this._project._needsUpdate=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._update()},getElement:function(){return this._element},getPixelRatio:function(){return this._pixelRatio},getResolution:function(){return 72*this._pixelRatio},getViewSize:function(){var t=this._viewSize;return new c(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=l.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.emit("resize",{size:t,delta:e}),this._update())},_setViewSize:function(t){var e=this._element;e.width=t.width,e.height=t.height},getBounds:function(){return this._bounds||(this._bounds=this._matrix.inverted()._transformBounds(new d(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(){var t=h.read(arguments);this.scrollBy(t.subtract(this.getCenter()))},getZoom:function(){return this._zoom},setZoom:function(t){this._transform((new g).scale(t/this._zoom,this.getCenter())),this._zoom=t},isVisible:function(){return F.isInView(this._element)},scrollBy:function(){ +this._transform((new g).translate(h.read(arguments).negate()))},play:function(){this._animate=!0,this._requested||this._requestFrame()},pause:function(){this._animate=!1},draw:function(){this.update()},projectToView:function(){return this._matrix._transformPoint(h.read(arguments))},viewToProject:function(){return this._matrix._inverseTransform(h.read(arguments))}},{statics:{_views:[],_viewsById:{},_id:0,create:function(t,e){return"string"==typeof e&&(e=document.getElementById(e)),new H(t,e)}}},new function(){function t(t){var e=q.getTarget(t);return e.getAttribute&&V._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(q.getOffset(e,t._element))}function n(){if(!V._focused||!V._focused.isVisible())for(var t=0,e=V._views.length;e>t;t++){var n=V._views[t];if(n&&n.isVisible()){V._focused=a=n;break}}}function i(t,e,n){t._handleEvent("mousemove",e,n);var i=t._scope.tool;return i&&i._handleEvent(l&&i.responds("mousedrag")?"mousedrag":"mousemove",e,n),t.update(),i}var r,s,a,o,h,u,l=!1,c=window.navigator;c.pointerEnabled||c.msPointerEnabled?(o="pointerdown MSPointerDown",h="pointermove MSPointerMove",u="pointerup pointercancel MSPointerUp MSPointerCancel"):(o="touchstart",h="touchmove",u="touchend touchcancel","ontouchstart"in window&&c.userAgent.match(/mobile|tablet|ip(ad|hone|od)|android|silk/i)||(o+=" mousedown",h+=" mousemove",u+=" mouseup"));var d={"selectstart dragstart":function(t){l&&t.preventDefault()}},f={mouseout:function(t){var n=V._focused,r=q.getRelatedTarget(t);!n||r&&"HTML"!==r.nodeName||i(n,e(n,t),t)},scroll:n};d[o]=function(n){var i=V._focused=t(n),s=e(i,n);l=!0,i._handleEvent("mousedown",s,n),(r=i._scope.tool)&&r._handleEvent("mousedown",s,n),i.update()},f[h]=function(o){var h=V._focused;if(!l){var u=t(o);u?(h!==u&&i(h,e(h,o),o),s=h,h=V._focused=a=u):a&&a===h&&(h=V._focused=s,n())}if(h){var c=e(h,o);(l||h.getBounds().contains(c))&&(r=i(h,c,o))}},f[u]=function(t){var n=V._focused;if(n&&l){var i=e(n,t);l=!1,n._handleEvent("mouseup",i,t),r&&r._handleEvent("mouseup",i,t),n.update()}},q.add(document,f),q.add(window,{load:n});var g={mousedown:{mousedown:1,mousedrag:1,click:1,doubleclick:1},mouseup:{mouseup:1,mousedrag:1,click:1,doubleclick:1},mousemove:{mousedrag:1,mousemove:1,mouseenter:1,mouseleave:1}};return{_viewEvents:d,_handleEvent:function(){},_installEvent:function(t){var e=this._eventCounters;if(e)for(var n in g)e[n]=(e[n]||0)+(g[n][t]||0)},_uninstallEvent:function(t){var e=this._eventCounters;if(e)for(var n in g)e[n]-=g[n][t]||0},statics:{updateFocus:n}}}),H=V.extend({_class:"CanvasView",initialize:function(t,e){if(!(e instanceof HTMLCanvasElement)){var n=l.read(arguments,1);if(n.isZero())throw Error("Cannot create CanvasView with the provided argument: "+[].slice.call(arguments,1));e=Y.getCanvas(n)}if(this._context=e.getContext("2d"),this._eventCounters={},this._pixelRatio=1,!/^off|false$/.test(i.getAttribute(e,"hidpi"))){var r=window.devicePixelRatio||1,s=F.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=r/s}V.call(this,t,e)},_setViewSize:function(t){var e=this._element,n=this._pixelRatio,r=t.width,s=t.height;if(e.width=r*n,e.height=s*n,1!==n){if(!i.hasAttribute(e,"resize")){var a=e.style;a.width=r+"px",a.height=s+"px"}this._context.scale(n,n)}},getPixelSize:function(t){var e,n=paper.browser;if(n&&n.firefox){var i=this._element.parentNode,r=document.createElement("div");r.style.fontSize=t,i.appendChild(r),e=parseFloat(F.getStyles(r).fontSize),i.removeChild(r)}else{var s=this._context,a=s.font;s.font=t+" serif",e=parseFloat(s.font),s.font=a}return e},getTextWidth:function(t,e){var n=this._context,i=n.font,r=0;n.font=t;for(var s=0,a=e.length;a>s;s++)r=Math.max(r,n.measureText(e[s]).width);return n.font=i,r},update:function(t){var e=this._project;if(!e||!t&&!e._needsUpdate)return!1;var n=this._context,i=this._viewSize;return n.clearRect(0,0,i.width+1,i.height+1),e.draw(n,this._matrix,this._pixelRatio),e._needsUpdate=!1,!0}},new function(){function e(e,n,i,r,s,a){function o(e){return e.responds(n)&&(h||(h=new G(n,i,r,s,a?r.subtract(a):null)),e.emit(n,h)&&h.isStopped)?(i.preventDefault(),!0):t}for(var h,u=s;u;){if(o(u))return!0;u=u.getParent()}return o(e)?!0:!1}var n,i,r,s,a,o,h,u,l;return{_handleEvent:function(t,c,d){if(this._eventCounters[t]){var f=this._project,g=f.hitTest(c,{tolerance:0,fill:!0,stroke:!0}),_=g&&g.item,v=!1;switch(t){case"mousedown":for(v=e(this,t,d,c,_),u=a==_&&Date.now()-l<300,s=a=_,n=i=r=c,h=!v&&_;h&&!h.responds("mousedrag");)h=h._parent;break;case"mouseup":v=e(this,t,d,c,_,n),h&&(i&&!i.equals(c)&&e(this,"mousedrag",d,c,h,i),_!==h&&(r=c,e(this,"mousemove",d,c,_,r))),!v&&_&&_===s&&(l=Date.now(),e(this,u&&s.responds("doubleclick")?"doubleclick":"click",d,n,_),u=!1),s=h=null;break;case"mousemove":h&&(v=e(this,"mousedrag",d,c,h,i)),v||(_!==o&&(r=c),v=e(this,t,d,c,_,r)),i=r=c,_!==o&&(e(this,"mouseleave",d,c,o),o=_,e(this,"mouseenter",d,c,_))}return v}}}}),Z=e.extend({_class:"Event",initialize:function(t){this.event=t},isPrevented:!1,isStopped:!1,preventDefault:function(){this.isPrevented=!0,this.event.preventDefault()},stopPropagation:function(){this.isStopped=!0,this.event.stopPropagation()},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return W.modifiers}}),U=Z.extend({_class:"KeyEvent",initialize:function(t,e,n,i){Z.call(this,i),this.type=t?"keydown":"keyup",this.key=e,this.character=n},toString:function(){return"{ type: '"+this.type+"', key: '"+this.key+"', character: '"+this.character+"', modifiers: "+this.getModifiers()+" }"}}),W=new function(){function t(i,s,u,l){var c,d=u?String.fromCharCode(u):"",f=r[s],g=f||d.toLowerCase(),_=i?"keydown":"keyup",v=V._focused,p=v&&v.isVisible()&&v._scope,m=p&&p.tool;if(h[g]=i,i?o[s]=u:delete o[s],f&&(c=e.camelize(f))in a){a[c]=i;var y=paper.browser;if("command"===c&&y&&y.mac)if(i)n={};else{for(var w in n)w in o&&t(!1,w,n[w],l);n=null}}else i&&n&&(n[s]=u);m&&m.responds(_)&&(paper=p,m.emit(_,new U(i,g,d,l)),v&&v.update())}var n,i,r={8:"backspace",9:"tab",13:"enter",16:"shift",17:"control",18:"option",19:"pause",20:"caps-lock",27:"escape",32:"space",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",46:"delete",91:"command",93:"command",224:"command"},s={9:!0,13:!0,32:!0},a=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),o={},h={};return q.add(document,{keydown:function(e){var n=e.which||e.keyCode;n in r||a.command?t(!0,n,n in s||a.command?n:0,e):i=n},keypress:function(e){null!=i&&(t(!0,i,e.which||e.keyCode,e),i=null)},keyup:function(e){var n=e.which||e.keyCode;n in o&&t(!1,n,o[n],e)}}),q.add(window,{blur:function(e){for(var n in o)t(!1,n,o[n],e)}}),{modifiers:a,isDown:function(t){return!!h[t]}}},G=Z.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){Z.call(this,e),this.type=t,this.point=n,this.target=i,this.delta=r},toString:function(){return"{ type: '"+this.type+"', point: "+this.point+", target: "+this.target+(this.delta?", delta: "+this.delta:"")+", modifiers: "+this.getModifiers()+" }"}}),$=Z.extend({_class:"ToolEvent",_item:null,initialize:function(t,e,n){this.tool=t,this.type=e,this.event=n},_choosePoint:function(t,e){return t?t:e?e.clone():null},getPoint:function(){return this._choosePoint(this._point,this.tool._point)},setPoint:function(t){this._point=t},getLastPoint:function(){return this._choosePoint(this._lastPoint,this.tool._lastPoint)},setLastPoint:function(t){this._lastPoint=t},getDownPoint:function(){return this._choosePoint(this._downPoint,this.tool._downPoint)},setDownPoint:function(t){this._downPoint=t},getMiddlePoint:function(){return!this._middlePoint&&this.tool._lastPoint?this.tool._point.add(this.tool._lastPoint).divide(2):this._middlePoint},setMiddlePoint:function(t){this._middlePoint=t},getDelta:function(){return!this._delta&&this.tool._lastPoint?this.tool._point.subtract(this.tool._lastPoint):this._delta},setDelta:function(t){this._delta=t},getCount:function(){return/^mouse(down|up)$/.test(this.type)?this.tool._downCount:this.tool._count},setCount:function(t){this.tool[/^mouse(down|up)$/.test(this.type)?"downCount":"count"]=t},getItem:function(){if(!this._item){var t=this.tool._scope.project.hitTest(this.getPoint());if(t){for(var e=t.item,n=e._parent;/^(Group|CompoundPath)$/.test(n._class);)e=n,n=n._parent;this._item=e}}return this._item},setItem:function(t){this._item=t},toString:function(){return"{ type: "+this.type+", point: "+this.getPoint()+", count: "+this.getCount()+", modifiers: "+this.getModifiers()+" }"}}),X=r.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){r.call(this),this._firstMove=!0,this._count=0,this._downCount=0,this._set(t)},getMinDistance:function(){return this._minDistance},setMinDistance:function(t){this._minDistance=t,null!=t&&null!=this._maxDistance&&t>this._maxDistance&&(this._maxDistance=t)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=t&&tu)return!1;if(null!=i&&0!=i)if(u>i)e=this._point.add(h.normalize(i));else if(a)return!1}if(s&&e.equals(this._point))return!1}switch(this._lastPoint=r&&"mousemove"==t?e:this._point,this._point=e,t){case"mousedown":this._lastPoint=this._downPoint,this._downPoint=this._point,this._downCount++;break;case"mouseup":this._lastPoint=this._downPoint}return this._count=r?0:this._count+1,!0},_fireEvent:function(t,e){var n=paper.project._removeSets;if(n){"mouseup"===t&&(n.mousedrag=null);var i=n[t];if(i){for(var r in i){var s=i[r];for(var a in n){var o=n[a];o&&o!=i&&delete o[s._id]}s.remove()}n[t]=null}}return this.responds(t)&&this.emit(t,new $(this,t,e))},_handleEvent:function(t,e,n){paper=this._scope;var i=!1;switch(t){case"mousedown":this._updateEvent(t,e,null,null,!0,!1,!1),i=this._fireEvent(t,n);break;case"mousedrag":for(var r=!1,s=!1;this._updateEvent(t,e,this.minDistance,this.maxDistance,!1,r,s);)i=this._fireEvent(t,n)||i,r=!0,s=!0;break;case"mouseup":!e.equals(this._point)&&this._updateEvent("mousedrag",e,this.minDistance,this.maxDistance,!1,!1,!1)&&(i=this._fireEvent("mousedrag",n)),this._updateEvent(t,e,null,this.maxDistance,!1,!1,!1),i=this._fireEvent(t,n)||i,this._updateEvent(t,e,null,null,!0,!1,!1),this._firstMove=!0;break;case"mousemove":for(;this._updateEvent(t,e,this.minDistance,this.maxDistance,this._firstMove,!0,!1);)i=this._fireEvent(t,n)||i,this._firstMove=!1}return i&&n.preventDefault(),i}}),J={request:function(e,n,i,r){r=r===t?!0:r;var s=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return s.open(e.toUpperCase(),n,r),"overrideMimeType"in s&&s.overrideMimeType("text/plain"),s.onreadystatechange=function(){if(4===s.readyState){var t=s.status;if(0!==t&&200!==t)throw Error("Could not load "+n+" (Error "+t+")");i.call(s,s.responseText)}},s.send(null)}},Y={canvases:[],getCanvas:function(t,e){var n,i=!0;"object"==typeof t&&(e=t.height,t=t.width),n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var r=n.getContext("2d");return n.width===t&&n.height===e?i&&r.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),r.save(),n},getContext:function(t,e){return this.getCanvas(t,e).getContext("2d")},release:function(t){var e=t.canvas?t.canvas:t;e.getContext("2d").restore(),this.canvases.push(e)}},K=new function(){function t(t,e,n){return.2989*t+.587*e+.114*n}function n(e,n,i,r){var s=r-t(e,n,i);f=e+s,g=n+s,_=i+s;var r=t(f,g,_),a=v(f,g,_),o=p(f,g,_);if(0>a){var h=r-a;f=r+(f-r)*r/h,g=r+(g-r)*r/h,_=r+(_-r)*r/h}if(o>255){var u=255-r,l=o-r;f=r+(f-r)*u/l,g=r+(g-r)*u/l,_=r+(_-r)*u/l}}function i(t,e,n){return p(t,e,n)-v(t,e,n)}function r(t,e,n,i){var r,s=[t,e,n],a=p(t,e,n),o=v(t,e,n);o=o===t?0:o===e?1:2,a=a===t?0:a===e?1:2,r=0===v(o,a)?1===p(o,a)?2:1:0,s[a]>s[o]?(s[r]=(s[r]-s[o])*i/(s[a]-s[o]),s[a]=i):s[r]=s[a]=0,s[o]=0,f=s[0],g=s[1],_=s[2]}var s,a,o,h,u,l,c,d,f,g,_,v=Math.min,p=Math.max,m=Math.abs,y={multiply:function(){f=u*s/255,g=l*a/255,_=c*o/255},screen:function(){f=u+s-u*s/255,g=l+a-l*a/255,_=c+o-c*o/255},overlay:function(){f=128>u?2*u*s/255:255-2*(255-u)*(255-s)/255,g=128>l?2*l*a/255:255-2*(255-l)*(255-a)/255,_=128>c?2*c*o/255:255-2*(255-c)*(255-o)/255},"soft-light":function(){var t=s*u/255;f=t+u*(255-(255-u)*(255-s)/255-t)/255,t=a*l/255,g=t+l*(255-(255-l)*(255-a)/255-t)/255,t=o*c/255,_=t+c*(255-(255-c)*(255-o)/255-t)/255},"hard-light":function(){f=128>s?2*s*u/255:255-2*(255-s)*(255-u)/255,g=128>a?2*a*l/255:255-2*(255-a)*(255-l)/255,_=128>o?2*o*c/255:255-2*(255-o)*(255-c)/255},"color-dodge":function(){f=0===u?0:255===s?255:v(255,255*u/(255-s)),g=0===l?0:255===a?255:v(255,255*l/(255-a)),_=0===c?0:255===o?255:v(255,255*c/(255-o))},"color-burn":function(){f=255===u?255:0===s?0:p(0,255-255*(255-u)/s),g=255===l?255:0===a?0:p(0,255-255*(255-l)/a),_=255===c?255:0===o?0:p(0,255-255*(255-c)/o)},darken:function(){f=s>u?u:s,g=a>l?l:a,_=o>c?c:o},lighten:function(){f=u>s?u:s,g=l>a?l:a,_=c>o?c:o},difference:function(){f=u-s,0>f&&(f=-f),g=l-a,0>g&&(g=-g),_=c-o,0>_&&(_=-_)},exclusion:function(){f=u+s*(255-u-u)/255,g=l+a*(255-l-l)/255,_=c+o*(255-c-c)/255},hue:function(){r(s,a,o,i(u,l,c)),n(f,g,_,t(u,l,c))},saturation:function(){r(u,l,c,i(s,a,o)),n(f,g,_,t(u,l,c))},luminosity:function(){n(u,l,c,t(s,a,o))},color:function(){n(s,a,o,t(u,l,c))},add:function(){f=v(u+s,255),g=v(l+a,255),_=v(c+o,255)},subtract:function(){f=p(u-s,0),g=p(l-a,0),_=p(c-o,0)},average:function(){f=(u+s)/2,g=(l+a)/2,_=(c+o)/2},negation:function(){f=255-m(255-s-u),g=255-m(255-a-l),_=255-m(255-o-c)}},w=this.nativeModes=e.each(["source-over","source-in","source-out","source-atop","destination-over","destination-in","destination-out","destination-atop","lighter","darker","copy","xor"],function(t){this[t]=!0},{}),x=Y.getContext(1,1);e.each(y,function(t,e){var n="darken"===e,i=!1;x.save();try{x.fillStyle=n?"#300":"#a00",x.fillRect(0,0,1,1),x.globalCompositeOperation=e,x.globalCompositeOperation===e&&(x.fillStyle=n?"#a00":"#300",x.fillRect(0,0,1,1),i=x.getImageData(0,0,1,1).data[0]!==n?170:51)}catch(r){}x.restore(),w[e]=i}),Y.release(x),this.process=function(t,e,n,i,r){var v=e.canvas,p="normal"===t;if(p||w[t])n.save(),n.setTransform(1,0,0,1,0,0),n.globalAlpha=i,p||(n.globalCompositeOperation=t),n.drawImage(v,r.x,r.y),n.restore();else{var m=y[t];if(!m)return;for(var x=n.getImageData(r.x,r.y,v.width,v.height),b=x.data,C=e.getImageData(0,0,v.width,v.height).data,S=0,P=b.length;P>S;S+=4){s=C[S],u=b[S],a=C[S+1],l=b[S+1],o=C[S+2],c=b[S+2],h=C[S+3],d=b[S+3],m();var k=h*i/255,I=1-k;b[S]=k*f+I*u,b[S+1]=k*g+I*l,b[S+2]=k*_+I*c,b[S+3]=h*i+I*d}n.putImageData(x,r.x,r.y)}}},Q=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],strokeScaling:["vector-effect","lookup",{"true":"none","false":"non-scaling-stroke"},function(t,e){return!e&&(t instanceof z||t instanceof x||t instanceof L)}],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],fontFamily:["font-family","string"],fontWeight:["font-weight","string"],fontSize:["font-size","number"],justification:["text-anchor","lookup",{left:"start",center:"middle",right:"end"}],opacity:["opacity","number"],blendMode:["mix-blend-mode","string"]},function(t,n){var i=e.capitalize(n),r=t[2];this[n]={type:t[1],property:n,attribute:t[0],toSVG:r,fromSVG:r&&e.each(r,function(t,e){this[t]=e},{}),exportFilter:t[3],get:"get"+i,set:"set"+i}},{}),tt={href:"http://www.w3.org/1999/xlink",xlink:"http://www.w3.org/2000/xmlns"};return new function(){function t(t,e){for(var n in e){var i=e[n],r=tt[n];"number"==typeof i&&(i=b.number(i)),r?t.setAttributeNS(r,n,i):t.setAttribute(n,i)}return t}function n(e,n){return t(document.createElementNS("http://www.w3.org/2000/svg",e),n)}function i(t,n,i){var r=new e,s=t.getTranslation();if(n){t=t.shiftless();var o=t._inverseTransform(s);r[i?"cx":"x"]=o.x,r[i?"cy":"y"]=o.y,s=null}if(!t.isIdentity()){var h=t.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;s&&!s.isZero()&&u.push("translate("+b.point(s)+")"),a.isZero(c.x-1)&&a.isZero(c.y-1)||u.push("scale("+b.point(c)+")"),l&&u.push("rotate("+b.number(l)+")"),r.transform=u.join(" ")}else r.transform="matrix("+t.getValues().join(",")+")"}return r}function r(e,r){for(var s=i(e._matrix),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=w(l,r);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),p(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function o(t,e){var r=i(t._matrix,!0),s=t.getSize(),a=t.getImage();return r.x-=s.width/2,r.y-=s.height/2,r.width=s.width,r.height=s.height,r.href=e.embedImages===!1&&a&&a.src||t.toDataURL(),n("image",r)}function h(t,e){var r=e.matchShapes;if(r){var s=t.toShape(!1);if(s)return u(s,e)}var a,o=t._segments,h=i(t._matrix);if(0===o.length)return null;if(r&&!t.hasHandles())if(o.length>=3){a=t._closed?"polygon":"polyline";for(var l=[],c=0,d=o.length;d>c;c++)l.push(b.point(o[c]._point));h.points=l.join(" ")}else{a="line";var f=o[0]._point,g=o[o.length-1]._point;h.set({x1:f.x,y1:f.y,x2:g.x,y2:g.y})}else a="path",h.d=t.getPathData(null,e.precision);return n(a,h)}function u(t){var e=t._type,r=t._radius,s=i(t._matrix,!0,"rectangle"!==e);if("rectangle"===e){e="rect";var a=t._size,o=a.width,h=a.height;s.x-=o/2,s.y-=h/2,s.width=o,s.height=h,r.isZero()&&(r=null)}return r&&("circle"===e?s.r=r:(s.rx=r.width,s.ry=r.height)),n(e,s)}function l(t,e){var r=i(t._matrix),s=t.getPathData(null,e.precision);return s&&(r.d=s),n("path",r)}function c(t,e){var r=i(t._matrix,!0),s=t.getSymbol(),a=_(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:b.rectangle(h)}),a.appendChild(w(o,e)),p(s,a,"symbol")),r.href="#"+a.id,r.x+=h.x,r.y+=h.y,r.width=b.number(h.width),r.height=b.number(h.height),r.overflow="visible",n("use",r)}function d(t){var e=_(t,"color");if(!e){var i,r=t.getGradient(),s=r._radial,a=t.getOrigin().transform(),o=t.getDestination().transform();if(s){i={cx:a.x,cy:a.y,r:a.getDistance(o)};var h=t.getHighlight();h&&(h=h.transform(),i.fx=h.x,i.fy=h.y)}else i={x1:a.x,y1:a.y,x2:o.x,y2:o.y};i.gradientUnits="userSpaceOnUse",e=n((s?"radial":"linear")+"Gradient",i);for(var u=r._stops,l=0,c=u.length;c>l;l++){var d=u[l],f=d._color,g=f.getAlpha();i={offset:d._rampPoint,"stop-color":f.toCSS(!0)},1>g&&(i["stop-opacity"]=g),e.appendChild(n("stop",i))}p(t,e,"color")}return"url(#"+e.id+")"}function f(t){var e=n("text",i(t._matrix,!0));return e.textContent=t._content,e}function g(n,i,r){var s={},a=!r&&n.getParent();return null!=n._name&&(s.id=n._name),e.each(Q,function(t){var i=t.get,r=t.type,o=n[i]();if(t.exportFilter?t.exportFilter(n,o):!a||!e.equals(a[i](),o)){if("color"===r&&null!=o){var h=o.getAlpha();1>h&&(s[t.attribute+"-opacity"]=h)}s[t.attribute]=null==o?"none":"number"===r?b.number(o):"color"===r?o.gradient?d(o,n):o.toCSS(!0):"array"===r?o.join(","):"lookup"===r?t.toSVG[o]:o}}),1===s.opacity&&delete s.opacity,n._visible||(s.visibility="hidden"),t(i,s)}function _(t,e){return C||(C={ids:{},svgs:{}}),t&&C.svgs[e+"-"+t._id]}function p(t,e,n){C||_();var i=C.ids[n]=(C.ids[n]||0)+1;e.id=n+"-"+i,C.svgs[n+"-"+t._id]=e}function y(t,e){var i=t,r=null;if(C){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in C.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(C.svgs[s]);C=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function w(t,e,n){var i=S[t._class],r=i&&i(t,e);if(r){var s=e.onExport;s&&(r=s(t,r,e)||r);var a=JSON.stringify(t._data);a&&"{}"!==a&&"null"!==a&&r.setAttribute("data-paper-data",a)}return r&&g(t,r,n)}function x(t){return t||(t={}),b=new s(t.precision),t}var b,C,S={Group:r,Layer:r,Raster:o,Path:h,Shape:u,CompoundPath:l,PlacedSymbol:c,PointText:f};m.inject({exportSVG:function(t){return t=x(t),y(w(this,t,!0),t)}}),v.inject({exportSVG:function(t){t=x(t);var e=this.layers,r=this.getView(),s=r.getViewSize(),a=n("svg",{x:0,y:0,width:s.width,height:s.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),o=a,h=r._matrix;h.isIdentity()||(o=a.appendChild(n("g",i(h))));for(var u=0,l=e.length;l>u;u++)o.appendChild(w(e[u],t,!0));return y(a,t)}})},new function(){function n(t,e,n,i){var r=tt[e],s=r?t.getAttributeNS(r,e):t.getAttribute(e);return"null"===s&&(s=null),null==s?i?null:n?"":0:n?s:parseFloat(s)}function i(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new h(e,i):null}function r(t,e,i,r){return e=n(t,e,!1,r),i=n(t,i,!1,r),!r||null!=e&&null!=i?new l(e,i):null}function s(t,e,n){return"none"===t?null:"number"===e?parseFloat(t):"array"===e?t?t.split(/[\s,]+/g).map(parseFloat):[]:"color"===e?S(t)||t:"lookup"===e?n[t]:t}function a(t,e,n,i){var r=t.childNodes,s="clippath"===e,a=new y,o=a._project,h=o._currentStyle,u=[];if(s||(a=C(a,t,i),o._currentStyle=a._style.clone()),i)for(var l=t.querySelectorAll("defs"),c=0,d=l.length;d>c;c++)P(l[c],n,!1);for(var c=0,d=r.length;d>c;c++){var f,g=r[c];1!==g.nodeType||"defs"===g.nodeName.toLowerCase()||!(f=P(g,n,!1))||f instanceof p||u.push(f)}return a.addChildren(u),s&&(a=C(a.reduce(),t,i)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){for(var n=t.getAttribute("points").match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g),i=[],r=0,s=n.length;s>r;r+=2)i.push(new h(parseFloat(n[r]),parseFloat(n[r+1])));var a=new A(i);return"polygon"===e&&a.closePath(),a}function u(t){var e=t.getAttribute("d"),n={pathData:e};return(e.match(/m/gi)||[]).length>1||/z\S+/i.test(e)?new O(n):new A(n)}function c(t,e){var r,s=(n(t,"href",!0)||"").substring(1),a="radialgradient"===e;if(s)r=M[s].getGradient();else{for(var o=t.childNodes,h=[],u=0,l=o.length;l>u;u++){var c=o[u];1===c.nodeType&&h.push(C(new R,c))}r=new B(h,a)}var d,f,g;return a?(d=i(t,"cx","cy"),f=d.add(n(t,"r"),0),g=i(t,"fx","fy",!0)):(d=i(t,"x1","y1"),f=i(t,"x2","y2")),C(new j(r,d,f,g),t),null}function f(t,e,n,i){for(var r=(i.getAttribute(n)||"").split(/\)\s*/g),s=new g,a=0,o=r.length;o>a;a++){var h=r[a];if(!h)break;for(var u=h.split(/\(\s*/),l=u[0],c=u[1].split(/[\s,]+/g),d=0,f=c.length;f>d;d++)c[d]=parseFloat(c[d]);switch(l){case"matrix":s.concatenate(new g(c[0],c[1],c[2],c[3],c[4],c[5]));break;case"rotate":s.rotate(c[0],c[1],c[2]);break;case"translate":s.translate(c[0],c[1]);break;case"scale":s.scale(c);break;case"skewX":s.skew(c[0],0);break;case"skewY":s.skew(0,c[0])}}t.transform(s)}function _(t,e,n){var i=t["fill-opacity"===n?"getFillColor":"getStrokeColor"]();i&&i.setAlpha(parseFloat(e))}function w(n,i,r){var s=n.attributes[i],a=s&&s.value;if(!a){var o=e.camelize(i);a=n.style[o],a||r.node[o]===r.parent[o]||(a=r.node[o])}return a?"none"===a?null:a:t}function C(n,i,r){var s={node:F.getStyles(i)||{},parent:!r&&F.getStyles(i.parentNode)||{}};return e.each(I,function(r,a){var o=w(i,a,s);o!==t&&(n=e.pick(r(n,o,a,i,s),n))}),n}function S(t){var e=t&&t.match(/\((?:#|)([^)']+)/);return e&&M[e[1]]}function P(t,n,i){function r(t){paper=a;var e=P(t,n,i),r=n.onLoad,s=a.project&&a.getView();r&&r.call(this,e),s.update()}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var s=t,a=paper;if(i)if("string"!=typeof t||/^.*s;s++){var o=r[s];if(1===o.nodeType){var h=o.nextSibling;document.body.appendChild(o);var u=P(o,n,i);return h?t.insertBefore(o,h):t.appendChild(o),u}}},g:a,svg:a,clippath:a,polygon:o,polyline:o,path:u,lineargradient:c,radialgradient:c,image:function(t){var e=new b(n(t,"href",!0));return e.on("load",function(){var e=r(t,"width","height");this.setSize(e);var n=this._matrix._transformPoint(i(t,"x","y").add(e.divide(2)));this.translate(n)}),e},symbol:function(t,e,n,i){return new p(a(t,e,n,i),!0)},defs:a,use:function(t){var e=(n(t,"href",!0)||"").substring(1),r=M[e],s=i(t,"x","y");return r?r instanceof p?r.place(s):r.clone().translate(s):null},circle:function(t){return new x.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new x.Ellipse({center:i(t,"cx","cy"),radius:r(t,"rx","ry")})},rect:function(t){var e=i(t,"x","y"),n=r(t,"width","height"),s=r(t,"rx","ry");return new x.Rectangle(new d(e,n),s)},line:function(t){return new A.Line(i(t,"x1","y1"),i(t,"x2","y2"))},text:function(t){var e=new N(i(t,"x","y").add(i(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},I=e.set(e.each(Q,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof x){var i=e[t.get]();i&&i.transform((new g).translate(e.getPosition(!0).negate()))}}},{}),{id:function(t,e){M[e]=t,t.setName&&t.setName(e)},"clip-path":function(t,e){var n=S(e);if(n){if(n=n.clone(),n.setClipMask(!0),!(t instanceof y))return new y(n,t);t.insertChild(0,n)}},gradientTransform:f,transform:f,"fill-opacity":_,"stroke-opacity":_,visibility:function(t,e){t.setVisible("visible"===e)},display:function(t,e){t.setVisible(null!==e)},"stop-color":function(t,e){t.setColor&&t.setColor(e)},"stop-opacity":function(t,e){t._color&&t._color.setAlpha(parseFloat(e))},offset:function(t,e){var n=e.match(/(.*)%$/);t.setRampPoint(n?n[1]/100:parseFloat(e))},viewBox:function(t,e,n,i,a){var o=new d(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof y){var u=h?o.getSize().divide(h):1,l=(new g).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof p){h&&o.setSize(h);var c="visible"!=w(i,"overflow",a),f=t._definition;c&&!o.contains(f.getBounds())&&(c=new x.Rectangle(o).transform(f._matrix),c.setClipMask(!0),f.addChild(c))}}}),M={};m.inject({importSVG:function(t,e){return this.addChild(P(t,e,!0))}}),v.inject({importSVG:function(t,e){return this.activate(),P(t,e,!0)}})},e.exports.PaperScript=function(){function t(t,e,n){var i=_[e];if(t&&t[i]){var r=t[i](n);return"!="===e?!r:r}switch(e){case"+":return t+n;case"-":return t-n;case"*":return t*n;case"/":return t/n;case"%":return t%n;case"==":return t==n;case"!=":return t!=n}}function n(t,e){var n=v[t];if(n&&e&&e[n])return e[n]();switch(t){case"+":return+e;case"-":return-e}}function r(t,e){return g.acorn.parse(t,e)}function s(t,e,n){function i(t){for(var e=0,n=u.length;n>e;e++){var i=u[e];if(i[0]>=t)break;t+=i[1]}return t}function s(e){return t.substring(i(e.range[0]),i(e.range[1]))}function a(e,n){return t.substring(i(e.range[1]),i(n.range[0]))}function o(e,n){for(var r=i(e.range[0]),s=i(e.range[1]),a=0,o=u.length-1;o>=0;o--)if(r>u[o][0]){a=o+1;break}u.splice(a,0,[r,n.length-s+r]),t=t.substring(0,r)+n+t.substring(s)}function h(t,e){if(t){for(var n in t)if("range"!==n&&"loc"!==n){var i=t[n];if(Array.isArray(i))for(var r=0,u=i.length;u>r;r++)h(i[r],t);else i&&"object"==typeof i&&h(i,t)}switch(t.type){case"UnaryExpression":if(t.operator in v&&"Literal"!==t.argument.type){var l=s(t.argument);o(t,'$__("'+t.operator+'", '+l+")")}break;case"BinaryExpression":if(t.operator in _&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right),f=a(t.left,t.right),g=t.operator;o(t,"__$__("+c+","+f.replace(RegExp("\\"+g),'"'+g+'"')+", "+d+")")}break;case"UpdateExpression":case"AssignmentExpression":var p=e&&e.type;if(!("ForStatement"===p||"BinaryExpression"===p&&/^[=!<>]/.test(e.operator)||"MemberExpression"===p&&e.computed))if("UpdateExpression"===t.type){var l=s(t.argument),m="__$__("+l+', "'+t.operator[0]+'", 1)',y=l+" = "+m;t.prefix||"AssignmentExpression"!==p&&"VariableDeclarator"!==p||(s(e.left||e.id)===l&&(y=m),y=l+"; "+y),o(t,y)}else if(/^.=$/.test(t.operator)&&"Literal"!==t.left.type){var c=s(t.left),d=s(t.right);o(t,c+" = __$__("+c+', "'+t.operator[0]+'", '+d+")")}}}}if(!t)return"";n=n||{},e=e||"";var u=[],l=null,c=paper.browser,d=c.versionNumber,f=/\r\n|\n|\r/gm;if(c.chrome&&d>=30||c.webkit&&d>=537.76||c.firefox&&d>=23){var g=0;if(0===window.location.href.indexOf(e)){var p=document.getElementsByTagName("html")[0].innerHTML;g=p.substr(0,p.indexOf(t)+1).match(f).length+1}var m=["AAAA"];m.length=(t.match(f)||[]).length+1+g,l={version:3,file:e,names:[],mappings:m.join(";AACA"),sourceRoot:"",sources:[e]};var y=n.source||!e&&t;y&&(l.sourcesContent=[y])}return h(r(t,{ranges:!0})),l&&(t=Array(g+1).join("\n")+t+"\n//# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(l))))+"\n//# sourceURL="+(e||"paperscript")),t}function a(i,r,a,o){function u(t,e){for(var n in t)!e&&/^_/.test(n)||!RegExp("([\\b\\s\\W]|^)"+n.replace(/\$/g,"\\$")+"\\b").test(i)||(_.push(n),v.push(t[n]))}paper=r;var l,c=r.getView(),d=/\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(i)?new X:null,f=d?d._events:[],g=["onFrame","onResize"].concat(f),_=[],v=[];i=s(i,a,o),u({__$__:t,$__:n,paper:r,view:c,tool:d},!0),u(r),g=e.each(g,function(t){RegExp("\\s+"+t+"\\b").test(i)&&(_.push(t),this.push(t+": "+t))},[]).join(", "),g&&(i+="\nreturn { "+g+" };");var p=paper.browser;if(p.chrome||p.firefox){var m=document.createElement("script"),y=document.head||document.getElementsByTagName("head")[0];p.firefox&&(i="\n"+i),m.appendChild(document.createTextNode("paper._execute = function("+_+") {"+i+"\n}")),y.appendChild(m),l=paper._execute,delete paper._execute,y.removeChild(m)}else l=Function(_,i);var w=l.apply(r,v)||{};e.each(f,function(t){var e=w[t];e&&(d[t]=e)}),c&&(w.onResize&&c.setOnResize(w.onResize),c.emit("resize",{size:c.size,delta:new h}),w.onFrame&&c.setOnFrame(w.onFrame),c.update())}function o(t){if(/^text\/(?:x-|)paperscript$/.test(t.type)&&"true"!==i.getAttribute(t,"ignore")){var e=i.getAttribute(t,"canvas"),n=document.getElementById(e),r=t.src||t.getAttribute("data-src"),s=i.hasAttribute(t,"async"),o="data-paper-scope";if(!n)throw Error('Unable to find canvas with id "'+e+'"');var h=i.get(n.getAttribute(o))||(new i).setup(n);return n.setAttribute(o,h._id),r?J.request("get",r,function(t){a(t,h,r)},s):a(t.innerHTML,h,t.baseURI),t.setAttribute("data-paper-ignore","true"),h}}function u(){e.each(document.getElementsByTagName("script"),o)}function c(t){return t?o(t):u()}var d,f,g=this;!function(t,e){return"object"==typeof d&&"object"==typeof module?e(d):"function"==typeof f&&f.amd?f(["exports"],e):void e(t.acorn||(t.acorn={}))}(this,function(t){"use strict";function e(t){ct=t||{};for(var e in _t)Object.prototype.hasOwnProperty.call(ct,e)||(ct[e]=_t[e]);gt=ct.sourceFile||null}function n(t,e){var n=vt(dt,t);e+=" ("+n.line+":"+n.column+")";var i=new SyntaxError(e);throw i.pos=t,i.loc=n,i.raisedAt=pt,i}function i(t){function e(t){if(1==t.length)return n+="return str === "+JSON.stringify(t[0])+";";n+="switch(str){";for(var e=0;e3){i.sort(function(t,e){return e.length-t.length}),n+="switch(str.length){";for(var r=0;rpt&&10!==n&&13!==n&&8232!==n&&8233!==n;)++pt,n=dt.charCodeAt(pt);ct.onComment&&ct.onComment(!1,dt.slice(t+2,pt),t,pt,e,ct.locations&&new r)}function u(){for(;ft>pt;){var t=dt.charCodeAt(pt);if(32===t)++pt;else if(13===t){++pt;var e=dt.charCodeAt(pt);10===e&&++pt,ct.locations&&(++Pt,kt=pt)}else if(10===t||8232===t||8233===t)++pt,ct.locations&&(++Pt,kt=pt);else if(t>8&&14>t)++pt;else if(47===t){var e=dt.charCodeAt(pt+1);if(42===e)o();else{if(47!==e)break;h()}}else if(160===t)++pt;else{if(!(t>=5760&&Ze.test(String.fromCharCode(t))))break;++pt}}}function l(){var t=dt.charCodeAt(pt+1);return t>=48&&57>=t?S(!0):(++pt,a(we))}function c(){var t=dt.charCodeAt(pt+1);return St?(++pt,x()):61===t?w(Se,2):w(be,1)}function d(){var t=dt.charCodeAt(pt+1);return 61===t?w(Se,2):w(je,1)}function f(t){var e=dt.charCodeAt(pt+1);return e===t?w(124===t?Me:ze,2):61===e?w(Se,2):w(124===t?Ae:Te,1)}function g(){var t=dt.charCodeAt(pt+1);return 61===t?w(Se,2):w(Oe,1)}function _(t){var e=dt.charCodeAt(pt+1);return e===t?45==e&&62==dt.charCodeAt(pt+2)&&Xe.test(dt.slice(Mt,pt))?(pt+=3,h(),u(),y()):w(ke,2):61===e?w(Se,2):w(Pe,1)}function v(t){var e=dt.charCodeAt(pt+1),n=1;return e===t?(n=62===t&&62===dt.charCodeAt(pt+2)?3:2,61===dt.charCodeAt(pt+n)?w(Se,n+1):w(Ne,n)):33==e&&60==t&&45==dt.charCodeAt(pt+2)&&45==dt.charCodeAt(pt+3)?(pt+=4,h(),u(),y()):(61===e&&(n=61===dt.charCodeAt(pt+2)?3:2),w(Le,n))}function p(t){var e=dt.charCodeAt(pt+1);return 61===e?w(Ee,61===dt.charCodeAt(pt+2)?3:2):w(61===t?Ce:Ie,1)}function m(t){switch(t){case 46:return l();case 40:return++pt,a(_e);case 41:return++pt,a(ve);case 59:return++pt,a(me);case 44:return++pt,a(pe);case 91:return++pt,a(ce);case 93:return++pt,a(de);case 123:return++pt,a(fe);case 125:return++pt,a(ge);case 58:return++pt,a(ye);case 63:return++pt,a(xe);case 48:var e=dt.charCodeAt(pt+1);if(120===e||88===e)return C();case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return S(!1);case 34:case 39:return P(t);case 47:return c(t);case 37:case 42:return d();case 124:case 38:return f(t);case 94:return g();case 43:case 45:return _(t);case 60:case 62:return v(t);case 61:case 33:return p(t);case 126:return w(Ie,1)}return!1}function y(t){if(t?pt=mt+1:mt=pt,ct.locations&&(wt=new r),t)return x();if(pt>=ft)return a(Rt);var e=dt.charCodeAt(pt);if(Ye(e)||92===e)return M();var i=m(e);if(i===!1){var s=String.fromCharCode(e);if("\\"===s||Ge.test(s))return M();n(pt,"Unexpected character '"+s+"'")}return i}function w(t,e){var n=dt.slice(pt,pt+e);pt+=e,a(t,n)}function x(){for(var t,e,i="",r=pt;;){pt>=ft&&n(r,"Unterminated regular expression");var s=dt.charAt(pt);if(Xe.test(s)&&n(r,"Unterminated regular expression"),t)t=!1;else{if("["===s)e=!0;else if("]"===s&&e)e=!1;else if("/"===s&&!e)break;t="\\"===s}++pt}var i=dt.slice(r,pt);++pt;var o=I();return o&&!/^[gmsiy]*$/.test(o)&&n(r,"Invalid regexp flag"),a(Nt,RegExp(i,o))}function b(t,e){for(var n=pt,i=0,r=0,s=null==e?1/0:e;s>r;++r){var a,o=dt.charCodeAt(pt);if(a=o>=97?o-97+10:o>=65?o-65+10:o>=48&&57>=o?o-48:1/0,a>=t)break;++pt,i=i*t+a}return pt===n||null!=e&&pt-n!==e?null:i}function C(){pt+=2;var t=b(16);return null==t&&n(mt+2,"Expected hexadecimal number"),Ye(dt.charCodeAt(pt))&&n(pt,"Identifier directly after number"),a(Lt,t)}function S(t){var e=pt,i=!1,r=48===dt.charCodeAt(pt);t||null!==b(10)||n(e,"Invalid number"),46===dt.charCodeAt(pt)&&(++pt,b(10),i=!0);var s=dt.charCodeAt(pt);(69===s||101===s)&&(s=dt.charCodeAt(++pt),(43===s||45===s)&&++pt,null===b(10)&&n(e,"Invalid number"),i=!0),Ye(dt.charCodeAt(pt))&&n(pt,"Identifier directly after number");var o,h=dt.slice(e,pt);return i?o=parseFloat(h):r&&1!==h.length?/[89]/.test(h)||Tt?n(e,"Invalid number"):o=parseInt(h,8):o=parseInt(h,10),a(Lt,o)}function P(t){pt++;for(var e="";;){pt>=ft&&n(mt,"Unterminated string constant");var i=dt.charCodeAt(pt);if(i===t)return++pt,a(jt,e);if(92===i){i=dt.charCodeAt(++pt);var r=/^[0-7]+/.exec(dt.slice(pt,pt+3));for(r&&(r=r[0]);r&&parseInt(r,8)>255;)r=r.slice(0,r.length-1);if("0"===r&&(r=null),++pt,r)Tt&&n(pt-2,"Octal literal in strict mode"),e+=String.fromCharCode(parseInt(r,8)),pt+=r.length-1;else switch(i){case 110:e+="\n";break;case 114:e+="\r";break;case 120:e+=String.fromCharCode(k(2));break;case 117:e+=String.fromCharCode(k(4));break;case 85:e+=String.fromCharCode(k(8));break;case 116:e+=" ";break;case 98:e+="\b";break;case 118:e+=" ";break;case 102:e+="\f";break;case 48:e+="\x00";break;case 13:10===dt.charCodeAt(pt)&&++pt;case 10:ct.locations&&(kt=pt,++Pt);break;default:e+=String.fromCharCode(i)}}else(13===i||10===i||8232===i||8233===i)&&n(mt,"Unterminated string constant"),e+=String.fromCharCode(i),++pt}}function k(t){var e=b(16,t);return null===e&&n(mt,"Bad character escape sequence"),e}function I(){Re=!1;for(var t,e=!0,i=pt;;){var r=dt.charCodeAt(pt);if(Ke(r))Re&&(t+=dt.charAt(pt)),++pt;else{if(92!==r)break;Re||(t=dt.slice(i,pt)),Re=!0,117!=dt.charCodeAt(++pt)&&n(pt,"Expecting Unicode escape sequence \\uXXXX"),++pt;var s=k(4),a=String.fromCharCode(s);a||n(pt-1,"Invalid Unicode escape"),(e?Ye(s):Ke(s))||n(pt-4,"Invalid Unicode escape"),t+=a}e=!1}return Re?t:dt.slice(i,pt)}function M(){var t=I(),e=Bt;return Re||(He(t)?e=le[t]:(ct.forbidReserved&&(3===ct.ecmaVersion?De:Fe)(t)||Tt&&qe(t))&&n(mt,"The keyword '"+t+"' is reserved")),a(e,t)}function z(){It=mt,Mt=yt,zt=xt,y()}function A(t){if(Tt=t,pt=Mt,ct.locations)for(;kt>pt;)kt=dt.lastIndexOf("\n",kt-2)+1,--Pt;u(),y()}function O(){this.type=null,this.start=mt,this.end=null}function T(){this.start=wt,this.end=null,null!==gt&&(this.source=gt)}function E(){var t=new O;return ct.locations&&(t.loc=new T),ct.ranges&&(t.range=[mt,0]),t}function L(t){var e=new O;return e.start=t.start,ct.locations&&(e.loc=new T,e.loc.start=t.loc.start),ct.ranges&&(e.range=[t.range[0],0]),e}function N(t,e){return t.type=e,t.end=Mt,ct.locations&&(t.loc.end=zt),ct.ranges&&(t.range[1]=Mt),t}function j(t){return ct.ecmaVersion>=5&&"ExpressionStatement"===t.type&&"Literal"===t.expression.type&&"use strict"===t.expression.value}function B(t){return bt===t?(z(),!0):void 0}function R(){return!ct.strictSemicolons&&(bt===Rt||bt===ge||Xe.test(dt.slice(Mt,mt)))}function D(){B(me)||R()||q()}function F(t){bt===t?z():q()}function q(){n(mt,"Unexpected token")}function V(t){"Identifier"!==t.type&&"MemberExpression"!==t.type&&n(t.start,"Assigning to rvalue"),Tt&&"Identifier"===t.type&&Ve(t.name)&&n(t.start,"Assigning to "+t.name+" in strict mode")}function H(t){It=Mt=pt,ct.locations&&(zt=new r),At=Tt=null,Ot=[],y();var e=t||E(),n=!0;for(t||(e.body=[]);bt!==Rt;){var i=Z();e.body.push(i),n&&j(i)&&A(!0),n=!1}return N(e,"Program")}function Z(){(bt===be||bt===Se&&"/="==Ct)&&y(!0);var t=bt,e=E();switch(t){case Dt:case Vt:z();var i=t===Dt;B(me)||R()?e.label=null:bt!==Bt?q():(e.label=lt(),D());for(var r=0;re){var r=L(t);r.left=t,r.operator=Ct,z(),r.right=tt(et(),i,n);var s=N(r,/&&|\|\|/.test(r.operator)?"LogicalExpression":"BinaryExpression");return tt(s,e,n)}return t}function et(){if(bt.prefix){var t=E(),e=bt.isUpdate;return t.operator=Ct,t.prefix=!0,St=!0,z(),t.argument=et(),e?V(t.argument):Tt&&"delete"===t.operator&&"Identifier"===t.argument.type&&n(t.start,"Deleting local variable in strict mode"),N(t,e?"UpdateExpression":"UnaryExpression")}for(var i=nt();bt.postfix&&!R();){var t=L(i);t.operator=Ct,t.prefix=!1,t.argument=i,V(i),z(),i=N(t,"UpdateExpression")}return i}function nt(){return it(rt())}function it(t,e){if(B(we)){var n=L(t);return n.object=t,n.property=lt(!0),n.computed=!1,it(N(n,"MemberExpression"),e)}if(B(ce)){var n=L(t);return n.object=t,n.property=J(),n.computed=!0,F(de),it(N(n,"MemberExpression"),e)}if(!e&&B(_e)){var n=L(t);return n.callee=t,n.arguments=ut(ve,!1),it(N(n,"CallExpression"),e)}return t}function rt(){switch(bt){case se:var t=E();return z(),N(t,"ThisExpression");case Bt:return lt();case Lt:case jt:case Nt:var t=E();return t.value=Ct,t.raw=dt.slice(mt,yt),z(),N(t,"Literal");case ae:case oe:case he:var t=E();return t.value=bt.atomValue,t.raw=bt.keyword,z(),N(t,"Literal");case _e:var e=wt,n=mt;z();var i=J();return i.start=n,i.end=yt,ct.locations&&(i.loc.start=e,i.loc.end=xt),ct.ranges&&(i.range=[n,yt]),F(ve),i;case ce:var t=E();return z(),t.elements=ut(de,!0,!0),N(t,"ArrayExpression");case fe:return at();case Xt:var t=E();return z(),ht(t,!1);case re:return st();default:q()}}function st(){var t=E();return z(),t.callee=it(rt(),!0),t.arguments=B(_e)?ut(ve,!1):Et,N(t,"NewExpression")}function at(){var t=E(),e=!0,i=!1;for(t.properties=[],z();!B(ge);){if(e)e=!1;else if(F(pe),ct.allowTrailingCommas&&B(ge))break;var r,s={key:ot()},a=!1;if(B(ye)?(s.value=J(!0),r=s.kind="init"):ct.ecmaVersion>=5&&"Identifier"===s.key.type&&("get"===s.key.name||"set"===s.key.name)?(a=i=!0,r=s.kind=s.key.name,s.key=ot(),bt!==_e&&q(),s.value=ht(E(),!1)):q(),"Identifier"===s.key.type&&(Tt||i))for(var o=0;oa?t.id:t.params[a];if((qe(o.name)||Ve(o.name))&&n(o.start,"Defining '"+o.name+"' in strict mode"),a>=0)for(var h=0;a>h;++h)o.name===t.params[h].name&&n(o.start,"Argument name clash in strict mode")}return N(t,e?"FunctionDeclaration":"FunctionExpression")}function ut(t,e,n){for(var i=[],r=!0;!B(t);){if(r)r=!1;else if(F(pe),e&&ct.allowTrailingCommas&&B(t))break;n&&bt===pe?i.push(null):i.push(J(!0))}return i}function lt(t){var e=E();return e.name=bt===Bt?Ct:t&&!ct.forbidReserved&&bt.keyword||q(),St=!1,z(),N(e,"Identifier")}t.version="0.4.0";var ct,dt,ft,gt;t.parse=function(t,n){return dt=t+"",ft=dt.length,e(n),s(),H(ct.program)};var _t=t.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},vt=t.getLineInfo=function(t,e){for(var n=1,i=0;;){Je.lastIndex=i;var r=Je.exec(t);if(!(r&&r.indext?36===t:91>t?!0:97>t?95===t:123>t?!0:t>=170&&Ge.test(String.fromCharCode(t))},Ke=t.isIdentifierChar=function(t){return 48>t?36===t:58>t?!0:65>t?!1:91>t?!0:97>t?95===t:123>t?!0:t>=170&&$e.test(String.fromCharCode(t))},Qe={kind:"loop"},tn={kind:"switch"}});var _={"+":"__add","-":"__subtract","*":"__multiply","/":"__divide","%":"__modulo","==":"equals","!=":"equals"},v={"-":"__negate","+":null},p=e.each(["add","subtract","multiply","divide","modulo","negate"],function(t){this["__"+t]="#"+t},{});return h.inject(p),l.inject(p),j.inject(p),"complete"===document.readyState?setTimeout(u):q.add(window,{load:u}),{compile:s,execute:a,load:c,parse:r}}.call(this),paper=new(i.inject(e.exports,{enumerable:!0,Base:e,Numerical:a,Key:W})),"function"==typeof define&&define.amd?define("paper",paper):"object"==typeof module&&module&&(module.exports=paper),paper}; \ No newline at end of file diff --git a/dist/paper-node.js b/dist/paper-node.js deleted file mode 120000 index 37e257c7..00000000 --- a/dist/paper-node.js +++ /dev/null @@ -1 +0,0 @@ -../src/load.js \ No newline at end of file diff --git a/dist/paper-node.js b/dist/paper-node.js new file mode 100644 index 00000000..5a261a61 --- /dev/null +++ b/dist/paper-node.js @@ -0,0 +1,12917 @@ +/*! + * Paper.js v0.9.25 - The Swiss Army Knife of Vector Graphics Scripting. + * http://paperjs.org/ + * + * Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey + * http://scratchdisk.com/ & http://jonathanpuckey.com/ + * + * Distributed under the MIT license. See LICENSE file for details. + * + * All rights reserved. + * + * Date: Sun Oct 25 11:23:38 2015 +0100 + * + *** + * + * Straps.js - Class inheritance library with support for bean-style accessors + * + * Copyright (c) 2006 - 2013 Juerg Lehni + * http://scratchdisk.com/ + * + * Distributed under the MIT license. + * + *** + * + * Acorn.js + * http://marijnhaverbeke.nl/acorn/ + * + * Acorn is a tiny, fast JavaScript parser written in JavaScript, + * created by Marijn Haverbeke and released under an MIT license. + * + */ + +var paper = new function(undefined) { + +var Base = new function() { + var hidden = /^(statics|enumerable|beans|preserve)$/, + + forEach = [].forEach || function(iter, bind) { + for (var i = 0, l = this.length; i < l; i++) + iter.call(bind, this[i], i, this); + }, + + forIn = function(iter, bind) { + for (var i in this) + if (this.hasOwnProperty(i)) + iter.call(bind, this[i], i, this); + }, + + create = Object.create || function(proto) { + return { __proto__: proto }; + }, + + describe = Object.getOwnPropertyDescriptor || function(obj, name) { + var get = obj.__lookupGetter__ && obj.__lookupGetter__(name); + return get + ? { get: get, set: obj.__lookupSetter__(name), + enumerable: true, configurable: true } + : obj.hasOwnProperty(name) + ? { value: obj[name], enumerable: true, + configurable: true, writable: true } + : null; + }, + + _define = Object.defineProperty || function(obj, name, desc) { + if ((desc.get || desc.set) && obj.__defineGetter__) { + if (desc.get) + obj.__defineGetter__(name, desc.get); + if (desc.set) + obj.__defineSetter__(name, desc.set); + } else { + obj[name] = desc.value; + } + return obj; + }, + + define = function(obj, name, desc) { + delete obj[name]; + return _define(obj, name, desc); + }; + + function inject(dest, src, enumerable, beans, preserve) { + var beansNames = {}; + + function field(name, val) { + val = val || (val = describe(src, name)) + && (val.get ? val : val.value); + if (typeof val === 'string' && val[0] === '#') + val = dest[val.substring(1)] || val; + var isFunc = typeof val === 'function', + res = val, + prev = preserve || isFunc && !val.base + ? (val && val.get ? name in dest : dest[name]) + : null, + bean; + if (!preserve || !prev) { + if (isFunc && prev) + val.base = prev; + if (isFunc && beans !== false + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beansNames[bean[3].toLowerCase() + bean[4]] = bean[2]; + if (!res || isFunc || !res.get || typeof res.get !== 'function' + || !Base.isPlainObject(res)) + res = { value: res, writable: true }; + if ((describe(dest, name) + || { configurable: true }).configurable) { + res.configurable = true; + res.enumerable = enumerable; + } + define(dest, name, res); + } + } + if (src) { + for (var name in src) { + if (src.hasOwnProperty(name) && !hidden.test(name)) + field(name); + } + for (var name in beansNames) { + var part = beansNames[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (beans === true || get.length === 0)) + field(name, { get: get, set: set }); + } + } + return dest; + } + + function each(obj, iter, bind) { + if (obj) + ('length' in obj && !obj.getLength + && typeof obj.length === 'number' + ? forEach + : forIn).call(obj, iter, bind = bind || obj); + return bind; + } + + function set(obj, props, exclude) { + for (var key in props) + if (props.hasOwnProperty(key) && !(exclude && exclude[key])) + obj[key] = props[key]; + return obj; + } + + return inject(function Base() { + for (var i = 0, l = arguments.length; i < l; i++) + set(this, arguments[i]); + }, { + inject: function(src) { + if (src) { + var statics = src.statics === true ? src : src.statics, + beans = src.beans, + preserve = src.preserve; + if (statics !== src) + inject(this.prototype, src, src.enumerable, beans, preserve); + inject(this, statics, true, beans, preserve); + } + for (var i = 1, l = arguments.length; i < l; i++) + this.inject(arguments[i]); + return this; + }, + + extend: function() { + var base = this, + ctor, + proto; + for (var i = 0, l = arguments.length; i < l; i++) + if (ctor = arguments[i].initialize) + break; + ctor = ctor || function() { + base.apply(this, arguments); + }; + proto = ctor.prototype = create(this.prototype); + define(proto, 'constructor', + { value: ctor, writable: true, configurable: true }); + inject(ctor, this, true); + if (arguments.length) + this.inject.apply(ctor, arguments); + ctor.base = base; + return ctor; + } + }, true).inject({ + inject: function() { + for (var i = 0, l = arguments.length; i < l; i++) { + var src = arguments[i]; + if (src) + inject(this, src, src.enumerable, src.beans, src.preserve); + } + return this; + }, + + extend: function() { + var res = create(this); + return res.inject.apply(res, arguments); + }, + + each: function(iter, bind) { + return each(this, iter, bind); + }, + + set: function(props) { + return set(this, props); + }, + + clone: function() { + return new this.constructor(this); + }, + + statics: { + each: each, + create: create, + define: define, + describe: describe, + set: set, + + clone: function(obj) { + return set(new obj.constructor(), obj); + }, + + isPlainObject: function(obj) { + var ctor = obj != null && obj.constructor; + return ctor && (ctor === Object || ctor === Base + || ctor.name === 'Object'); + }, + + pick: function(a, b) { + return a !== undefined ? a : b; + } + } + }); +}; + +if (typeof module !== 'undefined') + module.exports = Base; + +Base.inject({ + toString: function() { + return this._id != null + ? (this._class || 'Object') + (this._name + ? " '" + this._name + "'" + : ' @' + this._id) + : '{ ' + Base.each(this, function(value, key) { + if (!/^_/.test(key)) { + var type = typeof value; + this.push(key + ': ' + (type === 'number' + ? Formatter.instance.number(value) + : type === 'string' ? "'" + value + "'" : value)); + } + }, []).join(', ') + ' }'; + }, + + getClassName: function() { + return this._class || ''; + }, + + exportJSON: function(options) { + return Base.exportJSON(this, options); + }, + + toJSON: function() { + return Base.serialize(this); + }, + + _set: function(props, exclude, dontCheck) { + if (props && (dontCheck || Base.isPlainObject(props))) { + var keys = Object.keys(props._filtering || props); + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + if (!(exclude && exclude[key])) { + var value = props[key]; + if (value !== undefined) + this[key] = value; + } + } + return true; + } + }, + + statics: { + + exports: { + enumerable: true + }, + + extend: function extend() { + var res = extend.base.apply(this, arguments), + name = res.prototype._class; + if (name && !Base.exports[name]) + Base.exports[name] = res; + return res; + }, + + equals: function(obj1, obj2) { + if (obj1 === obj2) + return true; + if (obj1 && obj1.equals) + return obj1.equals(obj2); + if (obj2 && obj2.equals) + return obj2.equals(obj1); + if (obj1 && obj2 + && typeof obj1 === 'object' && typeof obj2 === 'object') { + if (Array.isArray(obj1) && Array.isArray(obj2)) { + var length = obj1.length; + if (length !== obj2.length) + return false; + while (length--) { + if (!Base.equals(obj1[length], obj2[length])) + return false; + } + } else { + var keys = Object.keys(obj1), + length = keys.length; + if (length !== Object.keys(obj2).length) + return false; + while (length--) { + var key = keys[length]; + if (!(obj2.hasOwnProperty(key) + && Base.equals(obj1[key], obj2[key]))) + return false; + } + } + return true; + } + return false; + }, + + read: function(list, start, options, length) { + if (this === Base) { + var value = this.peek(list, start); + list.__index++; + return value; + } + var proto = this.prototype, + readIndex = proto._readIndex, + index = start || readIndex && list.__index || 0; + if (!length) + length = list.length - index; + var obj = list[index]; + if (obj instanceof this + || options && options.readNull && obj == null && length <= 1) { + if (readIndex) + list.__index = index + 1; + return obj && options && options.clone ? obj.clone() : obj; + } + obj = Base.create(this.prototype); + if (readIndex) + obj.__read = true; + obj = obj.initialize.apply(obj, index > 0 || length < list.length + ? Array.prototype.slice.call(list, index, index + length) + : list) || obj; + if (readIndex) { + list.__index = index + obj.__read; + obj.__read = undefined; + } + return obj; + }, + + peek: function(list, start) { + return list[list.__index = start || list.__index || 0]; + }, + + remain: function(list) { + return list.length - (list.__index || 0); + }, + + readAll: function(list, start, options) { + var res = [], + entry; + for (var i = start || 0, l = list.length; i < l; i++) { + res.push(Array.isArray(entry = list[i]) + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); + } + return res; + }, + + readNamed: function(list, name, start, options, length) { + var value = this.getNamed(list, name), + hasObject = value !== undefined; + if (hasObject) { + var filtered = list._filtered; + if (!filtered) { + filtered = list._filtered = Base.create(list[0]); + filtered._filtering = list[0]; + } + filtered[name] = undefined; + } + return this.read(hasObject ? [value] : list, start, options, length); + }, + + getNamed: function(list, name) { + var arg = list[0]; + if (list._hasObject === undefined) + list._hasObject = list.length === 1 && Base.isPlainObject(arg); + if (list._hasObject) + return name ? arg[name] : list._filtered || arg; + }, + + hasNamed: function(list, name) { + return !!this.getNamed(list, name); + }, + + isPlainValue: function(obj, asString) { + return this.isPlainObject(obj) || Array.isArray(obj) + || asString && typeof obj === 'string'; + }, + + serialize: function(obj, options, compact, dictionary) { + options = options || {}; + + var root = !dictionary, + res; + if (root) { + options.formatter = new Formatter(options.precision); + dictionary = { + length: 0, + definitions: {}, + references: {}, + add: function(item, create) { + var id = '#' + item._id, + ref = this.references[id]; + if (!ref) { + this.length++; + var res = create.call(item), + name = item._class; + if (name && res[0] !== name) + res.unshift(name); + this.definitions[id] = res; + ref = this.references[id] = [id]; + } + return ref; + } + }; + } + if (obj && obj._serialize) { + res = obj._serialize(options, dictionary); + var name = obj._class; + if (name && !compact && !res._compact && res[0] !== name) + res.unshift(name); + } else if (Array.isArray(obj)) { + res = []; + for (var i = 0, l = obj.length; i < l; i++) + res[i] = Base.serialize(obj[i], options, compact, + dictionary); + if (compact) + res._compact = true; + } else if (Base.isPlainObject(obj)) { + res = {}; + var keys = Object.keys(obj); + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + res[key] = Base.serialize(obj[key], options, compact, + dictionary); + } + } else if (typeof obj === 'number') { + res = options.formatter.number(obj, options.precision); + } else { + res = obj; + } + return root && dictionary.length > 0 + ? [['dictionary', dictionary.definitions], res] + : res; + }, + + deserialize: function(json, create, _data, _isDictionary) { + var res = json, + isRoot = !_data; + _data = _data || {}; + if (Array.isArray(json)) { + var type = json[0], + isDictionary = type === 'dictionary'; + if (json.length == 1 && /^#/.test(type)) + return _data.dictionary[type]; + type = Base.exports[type]; + res = []; + if (_isDictionary) + _data.dictionary = res; + for (var i = type ? 1 : 0, l = json.length; i < l; i++) + res.push(Base.deserialize(json[i], create, _data, + isDictionary)); + if (type) { + var args = res; + if (create) { + res = create(type, args); + } else { + res = Base.create(type.prototype); + type.apply(res, args); + } + } + } else if (Base.isPlainObject(json)) { + res = {}; + if (_isDictionary) + _data.dictionary = res; + for (var key in json) + res[key] = Base.deserialize(json[key], create, _data); + } + return isRoot && json && json.length && json[0][0] === 'dictionary' + ? res[1] + : res; + }, + + exportJSON: function(obj, options) { + var json = Base.serialize(obj, options); + return options && options.asString === false + ? json + : JSON.stringify(json); + }, + + importJSON: function(json, target) { + return Base.deserialize( + typeof json === 'string' ? JSON.parse(json) : json, + function(type, args) { + var obj = target && target.constructor === type + ? target + : Base.create(type.prototype), + isTarget = obj === target; + if (args.length === 1 && obj instanceof Item + && (isTarget || !(obj instanceof Layer))) { + var arg = args[0]; + if (Base.isPlainObject(arg)) + arg.insert = false; + } + type.apply(obj, args); + if (isTarget) + target = null; + return obj; + }); + }, + + splice: function(list, items, index, remove) { + var amount = items && items.length, + append = index === undefined; + index = append ? list.length : index; + if (index > list.length) + index = list.length; + for (var i = 0; i < amount; i++) + items[i]._index = index + i; + if (append) { + list.push.apply(list, items); + return []; + } else { + var args = [index, remove]; + if (items) + args.push.apply(args, items); + var removed = list.splice.apply(list, args); + for (var i = 0, l = removed.length; i < l; i++) + removed[i]._index = undefined; + for (var i = index + amount, l = list.length; i < l; i++) + list[i]._index = i; + return removed; + } + }, + + capitalize: function(str) { + return str.replace(/\b[a-z]/g, function(match) { + return match.toUpperCase(); + }); + }, + + camelize: function(str) { + return str.replace(/-(.)/g, function(all, chr) { + return chr.toUpperCase(); + }); + }, + + hyphenate: function(str) { + return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); + } + } +}); + +var Emitter = { + on: function(type, func) { + if (typeof type !== 'string') { + Base.each(type, function(value, key) { + this.on(key, value); + }, this); + } else { + var types = this._eventTypes, + entry = types && types[type], + handlers = this._callbacks = this._callbacks || {}; + handlers = handlers[type] = handlers[type] || []; + if (handlers.indexOf(func) === -1) { + handlers.push(func); + if (entry && entry.install && handlers.length === 1) + entry.install.call(this, type); + } + } + return this; + }, + + off: function(type, func) { + if (typeof type !== 'string') { + Base.each(type, function(value, key) { + this.off(key, value); + }, this); + return; + } + var types = this._eventTypes, + entry = types && types[type], + handlers = this._callbacks && this._callbacks[type], + index; + if (handlers) { + if (!func || (index = handlers.indexOf(func)) !== -1 + && handlers.length === 1) { + if (entry && entry.uninstall) + entry.uninstall.call(this, type); + delete this._callbacks[type]; + } else if (index !== -1) { + handlers.splice(index, 1); + } + } + return this; + }, + + once: function(type, func) { + return this.on(type, function() { + func.apply(this, arguments); + this.off(type, func); + }); + }, + + emit: function(type, event) { + var handlers = this._callbacks && this._callbacks[type]; + if (!handlers) + return false; + var args = [].slice.call(arguments, 1); + handlers = handlers.slice(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(this, args) === false) { + if (event && event.stop) + event.stop(); + break; + } + } + return true; + }, + + responds: function(type) { + return !!(this._callbacks && this._callbacks[type]); + }, + + attach: '#on', + detach: '#off', + fire: '#emit', + + _installEvents: function(install) { + var handlers = this._callbacks, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var types = this._eventTypes, + entry = types && types[type], + func = entry && entry[key]; + if (func) + func.call(this, type); + } + } + }, + + statics: { + inject: function inject(src) { + var events = src._events; + if (events) { + var types = {}; + Base.each(events, function(entry, key) { + var isString = typeof entry === 'string', + name = isString ? entry : key, + part = Base.capitalize(name), + type = name.substring(2).toLowerCase(); + types[type] = isString ? {} : entry; + name = '_' + name; + src['get' + part] = function() { + return this[name]; + }; + src['set' + part] = function(func) { + var prev = this[name]; + if (prev) + this.off(type, prev); + if (func) + this.on(type, func); + this[name] = func; + }; + }); + src._eventTypes = types; + } + return inject.base.apply(this, arguments); + } + } +}; + +var PaperScope = Base.extend({ + _class: 'PaperScope', + + initialize: function PaperScope() { + paper = this; + this.settings = new Base({ + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }); + this.project = null; + this.projects = []; + this.tools = []; + this.palettes = []; + this._id = PaperScope._id++; + PaperScope._scopes[this._id] = this; + var proto = PaperScope.prototype; + if (!this.support) { + var ctx = CanvasProvider.getContext(1, 1); + proto.support = { + nativeDash: 'setLineDash' in ctx || 'mozDash' in ctx, + nativeBlendModes: BlendMode.nativeModes + }; + CanvasProvider.release(ctx); + } + + }, + + version: "0.9.25", + + getView: function() { + return this.project && this.project.getView(); + }, + + getPaper: function() { + return this; + }, + + execute: function(code, url, options) { + paper.PaperScript.execute(code, this, url, options); + View.updateFocus(); + }, + + install: function(scope) { + var that = this; + Base.each(['project', 'view', 'tool'], function(key) { + Base.define(scope, key, { + configurable: true, + get: function() { + return that[key]; + } + }); + }); + for (var key in this) + if (!/^_/.test(key) && this[key]) + scope[key] = this[key]; + }, + + setup: function(element) { + paper = this; + this.project = new Project(element); + return this; + }, + + activate: function() { + paper = this; + }, + + clear: function() { + for (var i = this.projects.length - 1; i >= 0; i--) + this.projects[i].remove(); + for (var i = this.tools.length - 1; i >= 0; i--) + this.tools[i].remove(); + for (var i = this.palettes.length - 1; i >= 0; i--) + this.palettes[i].remove(); + }, + + remove: function() { + this.clear(); + delete PaperScope._scopes[this._id]; + }, + + statics: new function() { + function handleAttribute(name) { + name += 'Attribute'; + return function(el, attr) { + return el[name](attr) || el[name]('data-paper-' + attr); + }; + } + + return { + _scopes: {}, + _id: 0, + + get: function(id) { + return this._scopes[id] || null; + }, + + getAttribute: handleAttribute('get'), + hasAttribute: handleAttribute('has') + }; + } +}); + +var PaperScopeItem = Base.extend(Emitter, { + + initialize: function(activate) { + this._scope = paper; + this._index = this._scope[this._list].push(this) - 1; + if (activate || !this._scope[this._reference]) + this.activate(); + }, + + activate: function() { + if (!this._scope) + return false; + var prev = this._scope[this._reference]; + if (prev && prev !== this) + prev.emit('deactivate'); + this._scope[this._reference] = this; + this.emit('activate', prev); + return true; + }, + + isActive: function() { + return this._scope[this._reference] === this; + }, + + remove: function() { + if (this._index == null) + return false; + Base.splice(this._scope[this._list], null, this._index, 1); + if (this._scope[this._reference] == this) + this._scope[this._reference] = null; + this._scope = null; + return true; + } +}); + +var Formatter = Base.extend({ + initialize: function(precision) { + this.precision = precision || 5; + this.multiplier = Math.pow(10, this.precision); + }, + + number: function(val) { + return Math.round(val * this.multiplier) / this.multiplier; + }, + + pair: function(val1, val2, separator) { + return this.number(val1) + (separator || ',') + this.number(val2); + }, + + point: function(val, separator) { + return this.number(val.x) + (separator || ',') + this.number(val.y); + }, + + size: function(val, separator) { + return this.number(val.width) + (separator || ',') + + this.number(val.height); + }, + + rectangle: function(val, separator) { + return this.point(val, separator) + (separator || ',') + + this.size(val, separator); + } +}); + +Formatter.instance = new Formatter(); + +var Numerical = new function() { + + var abscissas = [ + [ 0.5773502691896257645091488], + [0,0.7745966692414833770358531], + [ 0.3399810435848562648026658,0.8611363115940525752239465], + [0,0.5384693101056830910363144,0.9061798459386639927976269], + [ 0.2386191860831969086305017,0.6612093864662645136613996,0.9324695142031520278123016], + [0,0.4058451513773971669066064,0.7415311855993944398638648,0.9491079123427585245261897], + [ 0.1834346424956498049394761,0.5255324099163289858177390,0.7966664774136267395915539,0.9602898564975362316835609], + [0,0.3242534234038089290385380,0.6133714327005903973087020,0.8360311073266357942994298,0.9681602395076260898355762], + [ 0.1488743389816312108848260,0.4333953941292471907992659,0.6794095682990244062343274,0.8650633666889845107320967,0.9739065285171717200779640], + [0,0.2695431559523449723315320,0.5190961292068118159257257,0.7301520055740493240934163,0.8870625997680952990751578,0.9782286581460569928039380], + [ 0.1252334085114689154724414,0.3678314989981801937526915,0.5873179542866174472967024,0.7699026741943046870368938,0.9041172563704748566784659,0.9815606342467192506905491], + [0,0.2304583159551347940655281,0.4484927510364468528779129,0.6423493394403402206439846,0.8015780907333099127942065,0.9175983992229779652065478,0.9841830547185881494728294], + [ 0.1080549487073436620662447,0.3191123689278897604356718,0.5152486363581540919652907,0.6872929048116854701480198,0.8272013150697649931897947,0.9284348836635735173363911,0.9862838086968123388415973], + [0,0.2011940939974345223006283,0.3941513470775633698972074,0.5709721726085388475372267,0.7244177313601700474161861,0.8482065834104272162006483,0.9372733924007059043077589,0.9879925180204854284895657], + [ 0.0950125098376374401853193,0.2816035507792589132304605,0.4580167776572273863424194,0.6178762444026437484466718,0.7554044083550030338951012,0.8656312023878317438804679,0.9445750230732325760779884,0.9894009349916499325961542] + ]; + + var weights = [ + [1], + [0.8888888888888888888888889,0.5555555555555555555555556], + [0.6521451548625461426269361,0.3478548451374538573730639], + [0.5688888888888888888888889,0.4786286704993664680412915,0.2369268850561890875142640], + [0.4679139345726910473898703,0.3607615730481386075698335,0.1713244923791703450402961], + [0.4179591836734693877551020,0.3818300505051189449503698,0.2797053914892766679014678,0.1294849661688696932706114], + [0.3626837833783619829651504,0.3137066458778872873379622,0.2223810344533744705443560,0.1012285362903762591525314], + [0.3302393550012597631645251,0.3123470770400028400686304,0.2606106964029354623187429,0.1806481606948574040584720,0.0812743883615744119718922], + [0.2955242247147528701738930,0.2692667193099963550912269,0.2190863625159820439955349,0.1494513491505805931457763,0.0666713443086881375935688], + [0.2729250867779006307144835,0.2628045445102466621806889,0.2331937645919904799185237,0.1862902109277342514260976,0.1255803694649046246346943,0.0556685671161736664827537], + [0.2491470458134027850005624,0.2334925365383548087608499,0.2031674267230659217490645,0.1600783285433462263346525,0.1069393259953184309602547,0.0471753363865118271946160], + [0.2325515532308739101945895,0.2262831802628972384120902,0.2078160475368885023125232,0.1781459807619457382800467,0.1388735102197872384636018,0.0921214998377284479144218,0.0404840047653158795200216], + [0.2152638534631577901958764,0.2051984637212956039659241,0.1855383974779378137417166,0.1572031671581935345696019,0.1215185706879031846894148,0.0801580871597602098056333,0.0351194603317518630318329], + [0.2025782419255612728806202,0.1984314853271115764561183,0.1861610000155622110268006,0.1662692058169939335532009,0.1395706779261543144478048,0.1071592204671719350118695,0.0703660474881081247092674,0.0307532419961172683546284], + [0.1894506104550684962853967,0.1826034150449235888667637,0.1691565193950025381893121,0.1495959888165767320815017,0.1246289712555338720524763,0.0951585116824927848099251,0.0622535239386478928628438,0.0271524594117540948517806] + ]; + + var abs = Math.abs, + sqrt = Math.sqrt, + pow = Math.pow, + EPSILON = 1e-12, + MACHINE_EPSILON = 1.12e-16; + + function clip(value, min, max) { + return value < min ? min : value > max ? max : value; + } + + return { + TOLERANCE: 1e-6, + EPSILON: EPSILON, + MACHINE_EPSILON: MACHINE_EPSILON, + CURVETIME_EPSILON: 4e-7, + GEOMETRIC_EPSILON: 2e-7, + WINDING_EPSILON: 2e-7, + TRIGONOMETRIC_EPSILON: 1e-7, + CLIPPING_EPSILON: 1e-7, + KAPPA: 4 * (sqrt(2) - 1) / 3, + + isZero: function(val) { + return val >= -EPSILON && val <= EPSILON; + }, + + integrate: function(f, a, b, n) { + var x = abscissas[n - 2], + w = weights[n - 2], + A = (b - a) * 0.5, + B = A + a, + i = 0, + m = (n + 1) >> 1, + sum = n & 1 ? w[i++] * f(B) : 0; + while (i < m) { + var Ax = A * x[i]; + sum += w[i++] * (f(B + Ax) + f(B - Ax)); + } + return A * sum; + }, + + findRoot: function(f, df, x, a, b, n, tolerance) { + for (var i = 0; i < n; i++) { + var fx = f(x), + dx = fx / df(x), + nx = x - dx; + if (abs(dx) < tolerance) + return nx; + if (fx > 0) { + b = x; + x = nx <= a ? (a + b) * 0.5 : nx; + } else { + a = x; + x = nx >= b ? (a + b) * 0.5 : nx; + } + } + return x; + }, + + solveQuadratic: function(a, b, c, roots, min, max) { + var count = 0, + eMin = min - EPSILON, + eMax = max + EPSILON, + x1, x2 = Infinity, + B = b, + D; + b /= -2; + D = b * b - a * c; + if (D !== 0 && abs(D) < MACHINE_EPSILON) { + var gmC = pow(abs(a * b * c), 1 / 3); + if (gmC < 1e-8) { + var mult = pow(10, + abs(Math.floor(Math.log(gmC) * Math.LOG10E))); + if (!isFinite(mult)) + mult = 0; + a *= mult; + b *= mult; + c *= mult; + D = b * b - a * c; + } + } + if (abs(a) < EPSILON) { + if (abs(B) < EPSILON) + return abs(c) < EPSILON ? -1 : 0; + x1 = -c / B; + } else if (D >= -MACHINE_EPSILON) { + var Q = D < 0 ? 0 : sqrt(D), + R = b + (b < 0 ? -Q : Q); + if (R === 0) { + x1 = c / a; + x2 = -x1; + } else { + x1 = R / a; + x2 = c / R; + } + } + if (isFinite(x1) && (min == null || x1 > eMin && x1 < eMax)) + roots[count++] = min == null ? x1 : clip(x1, min, max); + if (x2 !== x1 + && isFinite(x2) && (min == null || x2 > eMin && x2 < eMax)) + roots[count++] = min == null ? x2 : clip(x2, min, max); + return count; + }, + + solveCubic: function(a, b, c, d, roots, min, max) { + var count = 0, + x, b1, c2; + if (abs(a) < EPSILON) { + a = b; + b1 = c; + c2 = d; + x = Infinity; + } else if (abs(d) < EPSILON) { + b1 = b; + c2 = c; + x = 0; + } else { + var ec = 1 + MACHINE_EPSILON, + x0, q, qd, t, r, s, tmp; + x = -(b / a) / 3; + tmp = a * x, + b1 = tmp + b, + c2 = b1 * x + c, + qd = (tmp + b1) * x + c2, + q = c2 * x + d; + t = q /a; + r = pow(abs(t), 1/3); + s = t < 0 ? -1 : 1; + t = -qd / a; + r = t > 0 ? 1.3247179572 * Math.max(r, sqrt(t)) : r; + x0 = x - s * r; + if (x0 !== x) { + do { + x = x0; + tmp = a * x, + b1 = tmp + b, + c2 = b1 * x + c, + qd = (tmp + b1) * x + c2, + q = c2 * x + d; + x0 = qd === 0 ? x : x - q / qd / ec; + if (x0 === x) { + x = x0; + break; + } + } while (s * x0 > s * x); + if (abs(a) * x * x > abs(d / x)) { + c2 = -d / x; + b1 = (c2 - c) / x; + } + } + } + var count = Numerical.solveQuadratic(a, b1, c2, roots, min, max); + if (isFinite(x) && (count === 0 || x !== roots[count - 1]) + && (min == null || x > min - EPSILON && x < max + EPSILON)) + roots[count++] = min == null ? x : clip(x, min, max); + return count; + } + }; +}; + +var UID = { + _id: 1, + _pools: {}, + + get: function(ctor) { + if (ctor) { + var name = ctor._class, + pool = this._pools[name]; + if (!pool) + pool = this._pools[name] = { _id: 1 }; + return pool._id++; + } else { + return this._id++; + } + } +}; + +var Point = Base.extend({ + _class: 'Point', + _readIndex: true, + + initialize: function Point(arg0, arg1) { + var type = typeof arg0; + if (type === 'number') { + var hasY = typeof arg1 === 'number'; + this.x = arg0; + this.y = hasY ? arg1 : arg0; + if (this.__read) + this.__read = hasY ? 2 : 1; + } else if (type === 'undefined' || arg0 === null) { + this.x = this.y = 0; + if (this.__read) + this.__read = arg0 === null ? 1 : 0; + } else { + if (Array.isArray(arg0)) { + this.x = arg0[0]; + this.y = arg0.length > 1 ? arg0[1] : arg0[0]; + } else if (arg0.x != null) { + this.x = arg0.x; + this.y = arg0.y; + } else if (arg0.width != null) { + this.x = arg0.width; + this.y = arg0.height; + } else if (arg0.angle != null) { + this.x = arg0.length; + this.y = 0; + this.setAngle(arg0.angle); + } else { + this.x = this.y = 0; + if (this.__read) + this.__read = 0; + } + if (this.__read) + this.__read = 1; + } + }, + + set: function(x, y) { + this.x = x; + this.y = y; + return this; + }, + + equals: function(point) { + return this === point || point + && (this.x === point.x && this.y === point.y + || Array.isArray(point) + && this.x === point[0] && this.y === point[1]) + || false; + }, + + clone: function() { + return new Point(this.x, this.y); + }, + + toString: function() { + var f = Formatter.instance; + return '{ x: ' + f.number(this.x) + ', y: ' + f.number(this.y) + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.x), f.number(this.y)]; + }, + + getLength: function() { + return Math.sqrt(this.x * this.x + this.y * this.y); + }, + + setLength: function(length) { + if (this.isZero()) { + var angle = this._angle || 0; + this.set( + Math.cos(angle) * length, + Math.sin(angle) * length + ); + } else { + var scale = length / this.getLength(); + if (Numerical.isZero(scale)) + this.getAngle(); + this.set( + this.x * scale, + this.y * scale + ); + } + }, + getAngle: function() { + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; + }, + + setAngle: function(angle) { + this.setAngleInRadians.call(this, angle * Math.PI / 180); + }, + + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + + getAngleInRadians: function() { + if (!arguments.length) { + return this.isZero() + ? this._angle || 0 + : this._angle = Math.atan2(this.y, this.x); + } else { + var point = Point.read(arguments), + div = this.getLength() * point.getLength(); + if (Numerical.isZero(div)) { + return NaN; + } else { + var a = this.dot(point) / div; + return Math.acos(a < -1 ? -1 : a > 1 ? 1 : a); + } + } + }, + + setAngleInRadians: function(angle) { + this._angle = angle; + if (!this.isZero()) { + var length = this.getLength(); + this.set( + Math.cos(angle) * length, + Math.sin(angle) * length + ); + } + }, + + getQuadrant: function() { + return this.x >= 0 ? this.y >= 0 ? 1 : 4 : this.y >= 0 ? 2 : 3; + } +}, { + beans: false, + + getDirectedAngle: function() { + var point = Point.read(arguments); + return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; + }, + + getDistance: function() { + var point = Point.read(arguments), + x = point.x - this.x, + y = point.y - this.y, + d = x * x + y * y, + squared = Base.read(arguments); + return squared ? d : Math.sqrt(d); + }, + + normalize: function(length) { + if (length === undefined) + length = 1; + var current = this.getLength(), + scale = current !== 0 ? length / current : 0, + point = new Point(this.x * scale, this.y * scale); + if (scale >= 0) + point._angle = this._angle; + return point; + }, + + rotate: function(angle, center) { + if (angle === 0) + return this.clone(); + angle = angle * Math.PI / 180; + var point = center ? this.subtract(center) : this, + sin = Math.sin(angle), + cos = Math.cos(angle); + point = new Point( + point.x * cos - point.y * sin, + point.x * sin + point.y * cos + ); + return center ? point.add(center) : point; + }, + + transform: function(matrix) { + return matrix ? matrix._transformPoint(this) : this; + }, + + add: function() { + var point = Point.read(arguments); + return new Point(this.x + point.x, this.y + point.y); + }, + + subtract: function() { + var point = Point.read(arguments); + return new Point(this.x - point.x, this.y - point.y); + }, + + multiply: function() { + var point = Point.read(arguments); + return new Point(this.x * point.x, this.y * point.y); + }, + + divide: function() { + var point = Point.read(arguments); + return new Point(this.x / point.x, this.y / point.y); + }, + + modulo: function() { + var point = Point.read(arguments); + return new Point(this.x % point.x, this.y % point.y); + }, + + negate: function() { + return new Point(-this.x, -this.y); + }, + + isInside: function() { + return Rectangle.read(arguments).contains(this); + }, + + isClose: function() { + var point = Point.read(arguments), + tolerance = Base.read(arguments); + return this.getDistance(point) < tolerance; + }, + + isCollinear: function() { + var point = Point.read(arguments); + return Point.isCollinear(this.x, this.y, point.x, point.y); + }, + + isColinear: '#isCollinear', + + isOrthogonal: function() { + var point = Point.read(arguments); + return Point.isOrthogonal(this.x, this.y, point.x, point.y); + }, + + isZero: function() { + return Numerical.isZero(this.x) && Numerical.isZero(this.y); + }, + + isNaN: function() { + return isNaN(this.x) || isNaN(this.y); + }, + + dot: function() { + var point = Point.read(arguments); + return this.x * point.x + this.y * point.y; + }, + + cross: function() { + var point = Point.read(arguments); + return this.x * point.y - this.y * point.x; + }, + + project: function() { + var point = Point.read(arguments), + scale = point.isZero() ? 0 : this.dot(point) / point.dot(point); + return new Point( + point.x * scale, + point.y * scale + ); + }, + + statics: { + min: function() { + var point1 = Point.read(arguments), + point2 = Point.read(arguments); + return new Point( + Math.min(point1.x, point2.x), + Math.min(point1.y, point2.y) + ); + }, + + max: function() { + var point1 = Point.read(arguments), + point2 = Point.read(arguments); + return new Point( + Math.max(point1.x, point2.x), + Math.max(point1.y, point2.y) + ); + }, + + random: function() { + return new Point(Math.random(), Math.random()); + }, + + isCollinear: function(x1, y1, x2, y2) { + return Math.abs(x1 * y2 - y1 * x2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; + }, + + isOrthogonal: function(x1, y1, x2, y2) { + return Math.abs(x1 * x2 + y1 * y2) + <= Math.sqrt((x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)) + * 1e-7; + } + } +}, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) { + var op = Math[name]; + this[name] = function() { + return new Point(op(this.x), op(this.y)); + }; +}, {})); + +var LinkedPoint = Point.extend({ + initialize: function Point(x, y, owner, setter) { + this._x = x; + this._y = y; + this._owner = owner; + this._setter = setter; + }, + + set: function(x, y, _dontNotify) { + this._x = x; + this._y = y; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + }, + + getX: function() { + return this._x; + }, + + setX: function(x) { + this._x = x; + this._owner[this._setter](this); + }, + + getY: function() { + return this._y; + }, + + setY: function(y) { + this._y = y; + this._owner[this._setter](this); + } +}); + +var Size = Base.extend({ + _class: 'Size', + _readIndex: true, + + initialize: function Size(arg0, arg1) { + var type = typeof arg0; + if (type === 'number') { + var hasHeight = typeof arg1 === 'number'; + this.width = arg0; + this.height = hasHeight ? arg1 : arg0; + if (this.__read) + this.__read = hasHeight ? 2 : 1; + } else if (type === 'undefined' || arg0 === null) { + this.width = this.height = 0; + if (this.__read) + this.__read = arg0 === null ? 1 : 0; + } else { + if (Array.isArray(arg0)) { + this.width = arg0[0]; + this.height = arg0.length > 1 ? arg0[1] : arg0[0]; + } else if (arg0.width != null) { + this.width = arg0.width; + this.height = arg0.height; + } else if (arg0.x != null) { + this.width = arg0.x; + this.height = arg0.y; + } else { + this.width = this.height = 0; + if (this.__read) + this.__read = 0; + } + if (this.__read) + this.__read = 1; + } + }, + + set: function(width, height) { + this.width = width; + this.height = height; + return this; + }, + + equals: function(size) { + return size === this || size && (this.width === size.width + && this.height === size.height + || Array.isArray(size) && this.width === size[0] + && this.height === size[1]) || false; + }, + + clone: function() { + return new Size(this.width, this.height); + }, + + toString: function() { + var f = Formatter.instance; + return '{ width: ' + f.number(this.width) + + ', height: ' + f.number(this.height) + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.width), + f.number(this.height)]; + }, + + add: function() { + var size = Size.read(arguments); + return new Size(this.width + size.width, this.height + size.height); + }, + + subtract: function() { + var size = Size.read(arguments); + return new Size(this.width - size.width, this.height - size.height); + }, + + multiply: function() { + var size = Size.read(arguments); + return new Size(this.width * size.width, this.height * size.height); + }, + + divide: function() { + var size = Size.read(arguments); + return new Size(this.width / size.width, this.height / size.height); + }, + + modulo: function() { + var size = Size.read(arguments); + return new Size(this.width % size.width, this.height % size.height); + }, + + negate: function() { + return new Size(-this.width, -this.height); + }, + + isZero: function() { + return Numerical.isZero(this.width) && Numerical.isZero(this.height); + }, + + isNaN: function() { + return isNaN(this.width) || isNaN(this.height); + }, + + statics: { + min: function(size1, size2) { + return new Size( + Math.min(size1.width, size2.width), + Math.min(size1.height, size2.height)); + }, + + max: function(size1, size2) { + return new Size( + Math.max(size1.width, size2.width), + Math.max(size1.height, size2.height)); + }, + + random: function() { + return new Size(Math.random(), Math.random()); + } + } +}, Base.each(['round', 'ceil', 'floor', 'abs'], function(name) { + var op = Math[name]; + this[name] = function() { + return new Size(op(this.width), op(this.height)); + }; +}, {})); + +var LinkedSize = Size.extend({ + initialize: function Size(width, height, owner, setter) { + this._width = width; + this._height = height; + this._owner = owner; + this._setter = setter; + }, + + set: function(width, height, _dontNotify) { + this._width = width; + this._height = height; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + }, + + getWidth: function() { + return this._width; + }, + + setWidth: function(width) { + this._width = width; + this._owner[this._setter](this); + }, + + getHeight: function() { + return this._height; + }, + + setHeight: function(height) { + this._height = height; + this._owner[this._setter](this); + } +}); + +var Rectangle = Base.extend({ + _class: 'Rectangle', + _readIndex: true, + beans: true, + + initialize: function Rectangle(arg0, arg1, arg2, arg3) { + var type = typeof arg0, + read = 0; + if (type === 'number') { + this.x = arg0; + this.y = arg1; + this.width = arg2; + this.height = arg3; + read = 4; + } else if (type === 'undefined' || arg0 === null) { + this.x = this.y = this.width = this.height = 0; + read = arg0 === null ? 1 : 0; + } else if (arguments.length === 1) { + if (Array.isArray(arg0)) { + this.x = arg0[0]; + this.y = arg0[1]; + this.width = arg0[2]; + this.height = arg0[3]; + read = 1; + } else if (arg0.x !== undefined || arg0.width !== undefined) { + this.x = arg0.x || 0; + this.y = arg0.y || 0; + this.width = arg0.width || 0; + this.height = arg0.height || 0; + read = 1; + } else if (arg0.from === undefined && arg0.to === undefined) { + this.x = this.y = this.width = this.height = 0; + this._set(arg0); + read = 1; + } + } + if (!read) { + var point = Point.readNamed(arguments, 'from'), + next = Base.peek(arguments); + this.x = point.x; + this.y = point.y; + if (next && next.x !== undefined || Base.hasNamed(arguments, 'to')) { + var to = Point.readNamed(arguments, 'to'); + this.width = to.x - point.x; + this.height = to.y - point.y; + if (this.width < 0) { + this.x = to.x; + this.width = -this.width; + } + if (this.height < 0) { + this.y = to.y; + this.height = -this.height; + } + } else { + var size = Size.read(arguments); + this.width = size.width; + this.height = size.height; + } + read = arguments.__index; + } + if (this.__read) + this.__read = read; + }, + + set: function(x, y, width, height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + return this; + }, + + clone: function() { + return new Rectangle(this.x, this.y, this.width, this.height); + }, + + equals: function(rect) { + var rt = Base.isPlainValue(rect) + ? Rectangle.read(arguments) + : rect; + return rt === this + || rt && this.x === rt.x && this.y === rt.y + && this.width === rt.width && this.height === rt.height + || false; + }, + + toString: function() { + var f = Formatter.instance; + return '{ x: ' + f.number(this.x) + + ', y: ' + f.number(this.y) + + ', width: ' + f.number(this.width) + + ', height: ' + f.number(this.height) + + ' }'; + }, + + _serialize: function(options) { + var f = options.formatter; + return [f.number(this.x), + f.number(this.y), + f.number(this.width), + f.number(this.height)]; + }, + + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); + }, + + setPoint: function() { + var point = Point.read(arguments); + this.x = point.x; + this.y = point.y; + }, + + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (this._fixX) + this.x += (this.width - size.width) * this._fixX; + if (this._fixY) + this.y += (this.height - size.height) * this._fixY; + this.width = size.width; + this.height = size.height; + this._fixW = 1; + this._fixH = 1; + }, + + getLeft: function() { + return this.x; + }, + + setLeft: function(left) { + if (!this._fixW) + this.width -= left - this.x; + this.x = left; + this._fixX = 0; + }, + + getTop: function() { + return this.y; + }, + + setTop: function(top) { + if (!this._fixH) + this.height -= top - this.y; + this.y = top; + this._fixY = 0; + }, + + getRight: function() { + return this.x + this.width; + }, + + setRight: function(right) { + if (this._fixX !== undefined && this._fixX !== 1) + this._fixW = 0; + if (this._fixW) + this.x = right - this.width; + else + this.width = right - this.x; + this._fixX = 1; + }, + + getBottom: function() { + return this.y + this.height; + }, + + setBottom: function(bottom) { + if (this._fixY !== undefined && this._fixY !== 1) + this._fixH = 0; + if (this._fixH) + this.y = bottom - this.height; + else + this.height = bottom - this.y; + this._fixY = 1; + }, + + getCenterX: function() { + return this.x + this.width * 0.5; + }, + + setCenterX: function(x) { + this.x = x - this.width * 0.5; + this._fixX = 0.5; + }, + + getCenterY: function() { + return this.y + this.height * 0.5; + }, + + setCenterY: function(y) { + this.y = y - this.height * 0.5; + this._fixY = 0.5; + }, + + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); + }, + + setCenter: function() { + var point = Point.read(arguments); + this.setCenterX(point.x); + this.setCenterY(point.y); + return this; + }, + + getArea: function() { + return this.width * this.height; + }, + + isEmpty: function() { + return this.width === 0 || this.height === 0; + }, + + contains: function(arg) { + return arg && arg.width !== undefined + || (Array.isArray(arg) ? arg : arguments).length == 4 + ? this._containsRectangle(Rectangle.read(arguments)) + : this._containsPoint(Point.read(arguments)); + }, + + _containsPoint: function(point) { + var x = point.x, + y = point.y; + return x >= this.x && y >= this.y + && x <= this.x + this.width + && y <= this.y + this.height; + }, + + _containsRectangle: function(rect) { + var x = rect.x, + y = rect.y; + return x >= this.x && y >= this.y + && x + rect.width <= this.x + this.width + && y + rect.height <= this.y + this.height; + }, + + intersects: function() { + var rect = Rectangle.read(arguments); + return rect.x + rect.width > this.x + && rect.y + rect.height > this.y + && rect.x < this.x + this.width + && rect.y < this.y + this.height; + }, + + touches: function() { + var rect = Rectangle.read(arguments); + return rect.x + rect.width >= this.x + && rect.y + rect.height >= this.y + && rect.x <= this.x + this.width + && rect.y <= this.y + this.height; + }, + + intersect: function() { + var rect = Rectangle.read(arguments), + x1 = Math.max(this.x, rect.x), + y1 = Math.max(this.y, rect.y), + x2 = Math.min(this.x + this.width, rect.x + rect.width), + y2 = Math.min(this.y + this.height, rect.y + rect.height); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + unite: function() { + var rect = Rectangle.read(arguments), + x1 = Math.min(this.x, rect.x), + y1 = Math.min(this.y, rect.y), + x2 = Math.max(this.x + this.width, rect.x + rect.width), + y2 = Math.max(this.y + this.height, rect.y + rect.height); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + include: function() { + var point = Point.read(arguments); + var x1 = Math.min(this.x, point.x), + y1 = Math.min(this.y, point.y), + x2 = Math.max(this.x + this.width, point.x), + y2 = Math.max(this.y + this.height, point.y); + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + expand: function() { + var amount = Size.read(arguments), + hor = amount.width, + ver = amount.height; + return new Rectangle(this.x - hor / 2, this.y - ver / 2, + this.width + hor, this.height + ver); + }, + + scale: function(hor, ver) { + return this.expand(this.width * hor - this.width, + this.height * (ver === undefined ? hor : ver) - this.height); + } +}, Base.each([ + ['Top', 'Left'], ['Top', 'Right'], + ['Bottom', 'Left'], ['Bottom', 'Right'], + ['Left', 'Center'], ['Top', 'Center'], + ['Right', 'Center'], ['Bottom', 'Center'] + ], + function(parts, index) { + var part = parts.join(''); + var xFirst = /^[RL]/.test(part); + if (index >= 4) + parts[1] += xFirst ? 'Y' : 'X'; + var x = parts[xFirst ? 0 : 1], + y = parts[xFirst ? 1 : 0], + getX = 'get' + x, + getY = 'get' + y, + setX = 'set' + x, + setY = 'set' + y, + get = 'get' + part, + set = 'set' + part; + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); + }; + this[set] = function() { + var point = Point.read(arguments); + this[setX](point.x); + this[setY](point.y); + }; + }, { + beans: true + } +)); + +var LinkedRectangle = Rectangle.extend({ + initialize: function Rectangle(x, y, width, height, owner, setter) { + this.set(x, y, width, height, true); + this._owner = owner; + this._setter = setter; + }, + + set: function(x, y, width, height, _dontNotify) { + this._x = x; + this._y = y; + this._width = width; + this._height = height; + if (!_dontNotify) + this._owner[this._setter](this); + return this; + } +}, +new function() { + var proto = Rectangle.prototype; + + return Base.each(['x', 'y', 'width', 'height'], function(key) { + var part = Base.capitalize(key); + var internal = '_' + key; + this['get' + part] = function() { + return this[internal]; + }; + + this['set' + part] = function(value) { + this[internal] = value; + if (!this._dontNotify) + this._owner[this._setter](this); + }; + }, Base.each(['Point', 'Size', 'Center', + 'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY', + 'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', + 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter'], + function(key) { + var name = 'set' + key; + this[name] = function() { + this._dontNotify = true; + proto[name].apply(this, arguments); + this._dontNotify = false; + this._owner[this._setter](this); + }; + }, { + isSelected: function() { + return this._owner._boundsSelected; + }, + + setSelected: function(selected) { + var owner = this._owner; + if (owner.setSelected) { + owner._boundsSelected = selected; + owner.setSelected(selected || owner._selectedSegmentState > 0); + } + } + }) + ); +}); + +var Matrix = Base.extend({ + _class: 'Matrix', + + initialize: function Matrix(arg) { + var count = arguments.length, + ok = true; + if (count === 6) { + this.set.apply(this, arguments); + } else if (count === 1) { + if (arg instanceof Matrix) { + this.set(arg._a, arg._c, arg._b, arg._d, arg._tx, arg._ty); + } else if (Array.isArray(arg)) { + this.set.apply(this, arg); + } else { + ok = false; + } + } else if (count === 0) { + this.reset(); + } else { + ok = false; + } + if (!ok) + throw new Error('Unsupported matrix parameters'); + }, + + set: function(a, c, b, d, tx, ty, _dontNotify) { + this._a = a; + this._c = c; + this._b = b; + this._d = d; + this._tx = tx; + this._ty = ty; + if (!_dontNotify) + this._changed(); + return this; + }, + + _serialize: function(options) { + return Base.serialize(this.getValues(), options); + }, + + _changed: function() { + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(9); + } + } + }, + + clone: function() { + return new Matrix(this._a, this._c, this._b, this._d, + this._tx, this._ty); + }, + + equals: function(mx) { + return mx === this || mx && this._a === mx._a && this._b === mx._b + && this._c === mx._c && this._d === mx._d + && this._tx === mx._tx && this._ty === mx._ty + || false; + }, + + toString: function() { + var f = Formatter.instance; + return '[[' + [f.number(this._a), f.number(this._b), + f.number(this._tx)].join(', ') + '], [' + + [f.number(this._c), f.number(this._d), + f.number(this._ty)].join(', ') + ']]'; + }, + + reset: function(_dontNotify) { + this._a = this._d = 1; + this._c = this._b = this._tx = this._ty = 0; + if (!_dontNotify) + this._changed(); + return this; + }, + + apply: function(recursively, _setApplyMatrix) { + var owner = this._owner; + if (owner) { + owner.transform(null, true, Base.pick(recursively, true), + _setApplyMatrix); + return this.isIdentity(); + } + return false; + }, + + translate: function() { + var point = Point.read(arguments), + x = point.x, + y = point.y; + this._tx += x * this._a + y * this._b; + this._ty += x * this._c + y * this._d; + this._changed(); + return this; + }, + + scale: function() { + var scale = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + if (center) + this.translate(center); + this._a *= scale.x; + this._c *= scale.x; + this._b *= scale.y; + this._d *= scale.y; + if (center) + this.translate(center.negate()); + this._changed(); + return this; + }, + + rotate: function(angle ) { + angle *= Math.PI / 180; + var center = Point.read(arguments, 1), + x = center.x, + y = center.y, + cos = Math.cos(angle), + sin = Math.sin(angle), + tx = x - x * cos + y * sin, + ty = y - x * sin - y * cos, + a = this._a, + b = this._b, + c = this._c, + d = this._d; + this._a = cos * a + sin * b; + this._b = -sin * a + cos * b; + this._c = cos * c + sin * d; + this._d = -sin * c + cos * d; + this._tx += tx * a + ty * b; + this._ty += tx * c + ty * d; + this._changed(); + return this; + }, + + shear: function() { + var shear = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + if (center) + this.translate(center); + var a = this._a, + c = this._c; + this._a += shear.y * this._b; + this._c += shear.y * this._d; + this._b += shear.x * a; + this._d += shear.x * c; + if (center) + this.translate(center.negate()); + this._changed(); + return this; + }, + + skew: function() { + var skew = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }), + toRadians = Math.PI / 180, + shear = new Point(Math.tan(skew.x * toRadians), + Math.tan(skew.y * toRadians)); + return this.shear(shear, center); + }, + + concatenate: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + this._a = a2 * a1 + c2 * b1; + this._b = b2 * a1 + d2 * b1; + this._c = a2 * c1 + c2 * d1; + this._d = b2 * c1 + d2 * d1; + this._tx += tx2 * a1 + ty2 * b1; + this._ty += tx2 * c1 + ty2 * d1; + this._changed(); + return this; + }, + + preConcatenate: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + tx1 = this._tx, + ty1 = this._ty, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + this._a = a2 * a1 + b2 * c1; + this._b = a2 * b1 + b2 * d1; + this._c = c2 * a1 + d2 * c1; + this._d = c2 * b1 + d2 * d1; + this._tx = a2 * tx1 + b2 * ty1 + tx2; + this._ty = c2 * tx1 + d2 * ty1 + ty2; + this._changed(); + return this; + }, + + chain: function(mx) { + var a1 = this._a, + b1 = this._b, + c1 = this._c, + d1 = this._d, + tx1 = this._tx, + ty1 = this._ty, + a2 = mx._a, + b2 = mx._b, + c2 = mx._c, + d2 = mx._d, + tx2 = mx._tx, + ty2 = mx._ty; + return new Matrix( + a2 * a1 + c2 * b1, + a2 * c1 + c2 * d1, + b2 * a1 + d2 * b1, + b2 * c1 + d2 * d1, + tx1 + tx2 * a1 + ty2 * b1, + ty1 + tx2 * c1 + ty2 * d1); + }, + + isIdentity: function() { + return this._a === 1 && this._c === 0 && this._b === 0 && this._d === 1 + && this._tx === 0 && this._ty === 0; + }, + + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + + isInvertible: function() { + return !!this._getDeterminant(); + }, + + isSingular: function() { + return !this._getDeterminant(); + }, + + transform: function( src, dst, count) { + return arguments.length < 3 + ? this._transformPoint(Point.read(arguments)) + : this._transformCoordinates(src, dst, count); + }, + + _transformPoint: function(point, dest, _dontNotify) { + var x = point.x, + y = point.y; + if (!dest) + dest = new Point(); + return dest.set( + x * this._a + y * this._b + this._tx, + x * this._c + y * this._d + this._ty, + _dontNotify + ); + }, + + _transformCoordinates: function(src, dst, count) { + var i = 0, + j = 0, + max = 2 * count; + while (i < max) { + var x = src[i++], + y = src[i++]; + dst[j++] = x * this._a + y * this._b + this._tx; + dst[j++] = x * this._c + y * this._d + this._ty; + } + return dst; + }, + + _transformCorners: function(rect) { + var x1 = rect.x, + y1 = rect.y, + x2 = x1 + rect.width, + y2 = y1 + rect.height, + coords = [ x1, y1, x2, y1, x2, y2, x1, y2 ]; + return this._transformCoordinates(coords, coords, 4); + }, + + _transformBounds: function(bounds, dest, _dontNotify) { + var coords = this._transformCorners(bounds), + min = coords.slice(0, 2), + max = min.slice(); + for (var i = 2; i < 8; i++) { + var val = coords[i], + j = i & 1; + if (val < min[j]) + min[j] = val; + else if (val > max[j]) + max[j] = val; + } + if (!dest) + dest = new Rectangle(); + return dest.set(min[0], min[1], max[0] - min[0], max[1] - min[1], + _dontNotify); + }, + + inverseTransform: function() { + return this._inverseTransform(Point.read(arguments)); + }, + + _getDeterminant: function() { + var det = this._a * this._d - this._b * this._c; + return isFinite(det) && !Numerical.isZero(det) + && isFinite(this._tx) && isFinite(this._ty) + ? det : null; + }, + + _inverseTransform: function(point, dest, _dontNotify) { + var det = this._getDeterminant(); + if (!det) + return null; + var x = point.x - this._tx, + y = point.y - this._ty; + if (!dest) + dest = new Point(); + return dest.set( + (x * this._d - y * this._b) / det, + (y * this._a - x * this._c) / det, + _dontNotify + ); + }, + + decompose: function() { + var a = this._a, b = this._b, c = this._c, d = this._d; + if (Numerical.isZero(a * d - b * c)) + return null; + + var scaleX = Math.sqrt(a * a + b * b); + a /= scaleX; + b /= scaleX; + + var shear = a * c + b * d; + c -= a * shear; + d -= b * shear; + + var scaleY = Math.sqrt(c * c + d * d); + c /= scaleY; + d /= scaleY; + shear /= scaleY; + + if (a * d < b * c) { + a = -a; + b = -b; + shear = -shear; + scaleX = -scaleX; + } + + return { + scaling: new Point(scaleX, scaleY), + rotation: -Math.atan2(b, a) * 180 / Math.PI, + shearing: shear + }; + }, + + getValues: function() { + return [ this._a, this._c, this._b, this._d, this._tx, this._ty ]; + }, + + getTranslation: function() { + return new Point(this._tx, this._ty); + }, + + getScaling: function() { + return (this.decompose() || {}).scaling; + }, + + getRotation: function() { + return (this.decompose() || {}).rotation; + }, + + inverted: function() { + var det = this._getDeterminant(); + return det && new Matrix( + this._d / det, + -this._c / det, + -this._b / det, + this._a / det, + (this._b * this._ty - this._d * this._tx) / det, + (this._c * this._tx - this._a * this._ty) / det); + }, + + shiftless: function() { + return new Matrix(this._a, this._c, this._b, this._d, 0, 0); + }, + + applyToContext: function(ctx) { + ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); + } +}, Base.each(['a', 'c', 'b', 'd', 'tx', 'ty'], function(name) { + var part = Base.capitalize(name), + prop = '_' + name; + this['get' + part] = function() { + return this[prop]; + }; + this['set' + part] = function(value) { + this[prop] = value; + this._changed(); + }; +}, {})); + +var Line = Base.extend({ + _class: 'Line', + + initialize: function Line(arg0, arg1, arg2, arg3, arg4) { + var asVector = false; + if (arguments.length >= 4) { + this._px = arg0; + this._py = arg1; + this._vx = arg2; + this._vy = arg3; + asVector = arg4; + } else { + this._px = arg0.x; + this._py = arg0.y; + this._vx = arg1.x; + this._vy = arg1.y; + asVector = arg2; + } + if (!asVector) { + this._vx -= this._px; + this._vy -= this._py; + } + }, + + getPoint: function() { + return new Point(this._px, this._py); + }, + + getVector: function() { + return new Point(this._vx, this._vy); + }, + + getLength: function() { + return this.getVector().getLength(); + }, + + intersect: function(line, isInfinite) { + return Line.intersect( + this._px, this._py, this._vx, this._vy, + line._px, line._py, line._vx, line._vy, + true, isInfinite); + }, + + getSide: function(point, isInfinite) { + return Line.getSide( + this._px, this._py, this._vx, this._vy, + point.x, point.y, true, isInfinite); + }, + + getDistance: function(point) { + return Math.abs(Line.getSignedDistance( + this._px, this._py, this._vx, this._vy, + point.x, point.y, true)); + }, + + isCollinear: function(line) { + return Point.isCollinear(this._vx, this._vy, line._vx, line._vy); + }, + + isOrthogonal: function(line) { + return Point.isOrthogonal(this._vx, this._vy, line._vx, line._vy); + }, + + statics: { + intersect: function(p1x, p1y, v1x, v1y, p2x, p2y, v2x, v2y, asVector, + isInfinite) { + if (!asVector) { + v1x -= p1x; + v1y -= p1y; + v2x -= p2x; + v2y -= p2y; + } + var cross = v1x * v2y - v1y * v2x; + if (!Numerical.isZero(cross)) { + var dx = p1x - p2x, + dy = p1y - p2y, + u1 = (v2x * dy - v2y * dx) / cross, + u2 = (v1x * dy - v1y * dx) / cross, + epsilon = 1e-12, + uMin = -epsilon, + uMax = 1 + epsilon; + if (isInfinite + || uMin < u1 && u1 < uMax && uMin < u2 && u2 < uMax) { + if (!isInfinite) { + u1 = u1 <= 0 ? 0 : u1 >= 1 ? 1 : u1; + } + return new Point( + p1x + u1 * v1x, + p1y + u1 * v1y); + } + } + }, + + getSide: function(px, py, vx, vy, x, y, asVector, isInfinite) { + if (!asVector) { + vx -= px; + vy -= py; + } + var v2x = x - px, + v2y = y - py, + ccw = v2x * vy - v2y * vx; + if (ccw === 0 && !isInfinite) { + ccw = (v2x * vx + v2x * vx) / (vx * vx + vy * vy); + if (ccw >= 0 && ccw <= 1) + ccw = 0; + } + return ccw < 0 ? -1 : ccw > 0 ? 1 : 0; + }, + + getSignedDistance: function(px, py, vx, vy, x, y, asVector) { + if (!asVector) { + vx -= px; + vy -= py; + } + return vx === 0 ? vy > 0 ? x - px : px - x + : vy === 0 ? vx < 0 ? y - py : py - y + : ((x-px) * vy - (y-py) * vx) / Math.sqrt(vx * vx + vy * vy); + } + } +}); + +var Project = PaperScopeItem.extend({ + _class: 'Project', + _list: 'projects', + _reference: 'project', + + initialize: function Project(element) { + PaperScopeItem.call(this, true); + this.layers = []; + this._activeLayer = null; + this.symbols = []; + this._currentStyle = new Style(null, null, this); + this._view = View.create(this, + element || CanvasProvider.getCanvas(1, 1)); + this._selectedItems = {}; + this._selectedItemCount = 0; + this._updateVersion = 0; + }, + + _serialize: function(options, dictionary) { + return Base.serialize(this.layers, options, true, dictionary); + }, + + clear: function() { + for (var i = this.layers.length - 1; i >= 0; i--) + this.layers[i].remove(); + this.symbols = []; + }, + + isEmpty: function() { + return this.layers.length === 0; + }, + + remove: function remove() { + if (!remove.base.call(this)) + return false; + if (this._view) + this._view.remove(); + return true; + }, + + getView: function() { + return this._view; + }, + + getCurrentStyle: function() { + return this._currentStyle; + }, + + setCurrentStyle: function(style) { + this._currentStyle.initialize(style); + }, + + getIndex: function() { + return this._index; + }, + + getOptions: function() { + return this._scope.settings; + }, + + getActiveLayer: function() { + return this._activeLayer || new Layer({ project: this }); + }, + + getSelectedItems: function() { + var items = []; + for (var id in this._selectedItems) { + var item = this._selectedItems[id]; + if (item.isInserted()) + items.push(item); + } + return items; + }, + + insertChild: function(index, item, _preserve) { + if (item instanceof Layer) { + item._remove(false, true); + Base.splice(this.layers, [item], index, 0); + item._setProject(this, true); + if (this._changes) + item._changed(5); + if (!this._activeLayer) + this._activeLayer = item; + } else if (item instanceof Item) { + (this._activeLayer + || this.insertChild(index, new Layer(Item.NO_INSERT))) + .insertChild(index, item, _preserve); + } else { + item = null; + } + return item; + }, + + addChild: function(item, _preserve) { + return this.insertChild(undefined, item, _preserve); + }, + + _updateSelection: function(item) { + var id = item._id, + selectedItems = this._selectedItems; + if (item._selected) { + if (selectedItems[id] !== item) { + this._selectedItemCount++; + selectedItems[id] = item; + } + } else if (selectedItems[id] === item) { + this._selectedItemCount--; + delete selectedItems[id]; + } + }, + + selectAll: function() { + var layers = this.layers; + for (var i = 0, l = layers.length; i < l; i++) + layers[i].setFullySelected(true); + }, + + deselectAll: function() { + var selectedItems = this._selectedItems; + for (var i in selectedItems) + selectedItems[i].setFullySelected(false); + }, + + hitTest: function() { + var point = Point.read(arguments), + options = HitResult.getOptions(Base.read(arguments)); + for (var i = this.layers.length - 1; i >= 0; i--) { + var res = this.layers[i]._hitTest(point, options); + if (res) return res; + } + return null; + }, + + getItems: function(match) { + return Item._getItems(this.layers, match); + }, + + getItem: function(match) { + return Item._getItems(this.layers, match, null, null, true)[0] || null; + }, + + importJSON: function(json) { + this.activate(); + var layer = this._activeLayer; + return Base.importJSON(json, layer && layer.isEmpty() && layer); + }, + + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; + ctx.save(); + matrix.applyToContext(ctx); + var param = new Base({ + offset: new Point(0, 0), + pixelRatio: pixelRatio, + viewMatrix: matrix.isIdentity() ? null : matrix, + matrices: [new Matrix()], + updateMatrix: true + }); + for (var i = 0, layers = this.layers, l = layers.length; i < l; i++) + layers[i].draw(ctx, param); + ctx.restore(); + + if (this._selectedItemCount > 0) { + ctx.save(); + ctx.strokeWidth = 1; + var items = this._selectedItems, + size = this._scope.settings.handleSize, + version = this._updateVersion; + for (var id in items) + items[id]._drawSelection(ctx, matrix, size, items, version); + ctx.restore(); + } + } +}); + +var Symbol = Base.extend({ + _class: 'Symbol', + + initialize: function Symbol(item, dontCenter) { + this._id = UID.get(); + this.project = paper.project; + this.project.symbols.push(this); + if (item) + this.setDefinition(item, dontCenter); + }, + + _serialize: function(options, dictionary) { + return dictionary.add(this, function() { + return Base.serialize([this._class, this._definition], + options, false, dictionary); + }); + }, + + _changed: function(flags) { + if (flags & 8) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } + }, + + getDefinition: function() { + return this._definition; + }, + + setDefinition: function(item, _dontCenter) { + if (item._parentSymbol) + item = item.clone(); + if (this._definition) + this._definition._parentSymbol = null; + this._definition = item; + item.remove(); + item.setSelected(false); + if (!_dontCenter) + item.setPosition(new Point()); + item._parentSymbol = this; + this._changed(9); + }, + + place: function(position) { + return new PlacedSymbol(this, position); + }, + + clone: function() { + return new Symbol(this._definition.clone(false)); + }, + + equals: function(symbol) { + return symbol === this + || symbol && this.definition.equals(symbol.definition) + || false; + } +}); + +var Item = Base.extend(Emitter, { + statics: { + extend: function extend(src) { + if (src._serializeFields) + src._serializeFields = new Base( + this.prototype._serializeFields, src._serializeFields); + return extend.base.apply(this, arguments); + }, + + NO_INSERT: { insert: false } + }, + + _class: 'Item', + _applyMatrix: true, + _canApplyMatrix: true, + _boundsSelected: false, + _selectChildren: false, + _serializeFields: { + name: null, + applyMatrix: null, + matrix: new Matrix(), + pivot: null, + locked: false, + visible: true, + blendMode: 'normal', + opacity: 1, + guide: false, + selected: false, + clipMask: false, + data: {} + }, + + initialize: function Item() { + }, + + _initialize: function(props, point) { + var hasProps = props && Base.isPlainObject(props), + internal = hasProps && props.internal === true, + matrix = this._matrix = new Matrix(), + project = hasProps && props.project || paper.project; + if (!internal) + this._id = UID.get(); + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this, project); + if (!this._project) { + if (internal || hasProps && props.insert === false) { + this._setProject(project); + } else if (hasProps && props.parent) { + this.setParent(props.parent); + } else { + (project._activeLayer || new Layer()).addChild(this); + } + } + if (hasProps && props !== Item.NO_INSERT) + this._set(props, { insert: true, project: true, parent: true }, + true); + return hasProps; + }, + + _events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', + 'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'], + function(name) { + this[name] = { + install: function(type) { + this.getView()._installEvent(type); + }, + + uninstall: function(type) { + this.getView()._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.getView()._animateItem(this, true); + }, + + uninstall: function() { + this.getView()._animateItem(this, false); + } + }, + + onLoad: {} + } + ), + + _serialize: function(options, dictionary) { + var props = {}, + that = this; + + function serialize(fields) { + for (var key in fields) { + var value = that[key]; + if (!Base.equals(value, key === 'leading' + ? fields.fontSize * 1.2 : fields[key])) { + props[key] = Base.serialize(value, options, + key !== 'data', dictionary); + } + } + } + + serialize(this._serializeFields); + if (!(this instanceof Group)) + serialize(this._style._defaults); + return [ this._class, props ]; + }, + + _changed: function(flags) { + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; + if (flags & 8) { + this._bounds = this._position = this._decomposed = + this._globalMatrix = this._currentPath = undefined; + } + if (cacheParent + && (flags & 40)) { + Item._clearBoundsCache(cacheParent); + } + if (flags & 2) { + Item._clearBoundsCache(this); + } + if (project) { + if (flags & 1) { + project._needsUpdate = true; + } + if (project._changes) { + var entry = project._changesById[this._id]; + if (entry) { + entry.flags |= flags; + } else { + entry = { item: this, flags: flags }; + project._changesById[this._id] = entry; + project._changes.push(entry); + } + } + } + if (symbol) + symbol._changed(flags); + }, + + set: function(props) { + if (props) + this._set(props); + return this; + }, + + getId: function() { + return this._id; + }, + + getName: function() { + return this._name; + }, + + setName: function(name, unique) { + + if (this._name) + this._removeNamed(); + if (name === (+name) + '') + throw new Error( + 'Names consisting only of numbers are not supported.'); + var parent = this._parent; + if (name && parent) { + var children = parent._children, + namedChildren = parent._namedChildren, + orig = name, + i = 1; + while (unique && children[name]) + name = orig + ' ' + (i++); + (namedChildren[name] = namedChildren[name] || []).push(this); + children[name] = this; + } + this._name = name || undefined; + this._changed(128); + }, + + getStyle: function() { + return this._style; + }, + + setStyle: function(style) { + this.getStyle().set(style); + } +}, Base.each(['locked', 'visible', 'blendMode', 'opacity', 'guide'], + function(name) { + var part = Base.capitalize(name), + name = '_' + name; + this['get' + part] = function() { + return this[name]; + }; + this['set' + part] = function(value) { + if (value != this[name]) { + this[name] = value; + this._changed(name === '_locked' + ? 128 : 129); + } + }; + }, +{}), { + beans: true, + + _locked: false, + + _visible: true, + + _blendMode: 'normal', + + _opacity: 1, + + _guide: false, + + isSelected: function() { + if (this._selectChildren) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + if (children[i].isSelected()) + return true; + } + return this._selected; + }, + + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + children[i].setSelected(selected); + } + if ((selected = !!selected) ^ this._selected) { + this._selected = selected; + this._project._updateSelection(this); + this._changed(129); + } + }, + + _selected: false, + + isFullySelected: function() { + var children = this._children; + if (children && this._selected) { + for (var i = 0, l = children.length; i < l; i++) + if (!children[i].isFullySelected()) + return false; + return true; + } + return this._selected; + }, + + setFullySelected: function(selected) { + var children = this._children; + if (children) { + for (var i = 0, l = children.length; i < l; i++) + children[i].setFullySelected(selected); + } + this.setSelected(selected, true); + }, + + isClipMask: function() { + return this._clipMask; + }, + + setClipMask: function(clipMask) { + if (this._clipMask != (clipMask = !!clipMask)) { + this._clipMask = clipMask; + if (clipMask) { + this.setFillColor(null); + this.setStrokeColor(null); + } + this._changed(129); + if (this._parent) + this._parent._changed(1024); + } + }, + + _clipMask: false, + + getData: function() { + if (!this._data) + this._data = {}; + return this._data; + }, + + setData: function(data) { + this._data = data; + }, + + getPosition: function(_dontLink) { + var position = this._position, + ctor = _dontLink ? Point : LinkedPoint; + if (!position) { + var pivot = this._pivot; + position = this._position = pivot + ? this._matrix._transformPoint(pivot) + : this.getBounds().getCenter(true); + } + return new ctor(position.x, position.y, this, 'setPosition'); + }, + + setPosition: function() { + this.translate(Point.read(arguments).subtract(this.getPosition(true))); + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setPivot'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments, 0, { clone: true, readNull: true }); + this._position = undefined; + }, + + _pivot: null, +}, Base.each(['bounds', 'strokeBounds', 'handleBounds', 'roughBounds', + 'internalBounds', 'internalRoughBounds'], + function(key) { + var getter = 'get' + Base.capitalize(key), + match = key.match(/^internal(.*)$/), + internalGetter = match ? 'get' + match[1] : null; + this[getter] = function(_matrix) { + var boundsGetter = this._boundsGetter, + name = !internalGetter && (typeof boundsGetter === 'string' + ? boundsGetter : boundsGetter && boundsGetter[getter]) + || getter, + bounds = this._getCachedBounds(name, _matrix, this, + internalGetter); + return key === 'bounds' + ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, + bounds.height, this, 'setBounds') + : bounds; + }; + }, +{ + beans: true, + + _getBounds: function(getter, matrix, cacheItem) { + var children = this._children; + if (!children || children.length == 0) + return new Rectangle(); + Item._updateBoundsCache(this, cacheItem); + var x1 = Infinity, + x2 = -x1, + y1 = x1, + y2 = x2; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i]; + if (child._visible && !child.isEmpty()) { + var rect = child._getCachedBounds(getter, + matrix && matrix.chain(child._matrix), cacheItem); + x1 = Math.min(rect.x, x1); + y1 = Math.min(rect.y, y1); + x2 = Math.max(rect.x + rect.width, x2); + y2 = Math.max(rect.y + rect.height, y2); + } + } + return isFinite(x1) + ? new Rectangle(x1, y1, x2 - x1, y2 - y1) + : new Rectangle(); + }, + + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), + matrix = new Matrix(), + center = rect.getCenter(); + matrix.translate(center); + if (rect.width != bounds.width || rect.height != bounds.height) { + matrix.scale( + bounds.width != 0 ? rect.width / bounds.width : 1, + bounds.height != 0 ? rect.height / bounds.height : 1); + } + center = bounds.getCenter(); + matrix.translate(-center.x, -center.y); + this.transform(matrix); + }, + + _getCachedBounds: function(getter, matrix, cacheItem, internalGetter) { + matrix = matrix && matrix.orNullIfIdentity(); + var _matrix = internalGetter ? null : this._matrix.orNullIfIdentity(), + cache = (!matrix || matrix.equals(_matrix)) && getter; + Item._updateBoundsCache(this._parent || this._parentSymbol, cacheItem); + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + var bounds = this._getBounds(internalGetter || getter, + matrix || _matrix, cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _updateBoundsCache: function(parent, item) { + if (parent) { + var id = item._id, + ref = parent._boundsCache = parent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(item); + ref.ids[id] = item; + } + } + }, + + _clearBoundsCache: function(item) { + var cache = item._boundsCache; + if (cache) { + item._bounds = item._position = item._boundsCache = undefined; + for (var i = 0, list = cache.list, l = list.length; i < l; i++){ + var other = list[i]; + if (other !== item) { + other._bounds = other._position = undefined; + if (other._boundsCache) + Item._clearBoundsCache(other); + } + } + } + } + } + +}), { + beans: true, + + _decompose: function() { + return this._decomposed = this._matrix.decompose(); + }, + + getRotation: function() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.rotation; + }, + + setRotation: function(rotation) { + var current = this.getRotation(); + if (current != null && rotation != null) { + var decomposed = this._decomposed; + this.rotate(rotation - current); + decomposed.rotation = rotation; + this._decomposed = decomposed; + } + }, + + getScaling: function(_dontLink) { + var decomposed = this._decomposed || this._decompose(), + scaling = decomposed && decomposed.scaling, + ctor = _dontLink ? Point : LinkedPoint; + return scaling && new ctor(scaling.x, scaling.y, this, 'setScaling'); + }, + + setScaling: function() { + var current = this.getScaling(); + if (current) { + var scaling = Point.read(arguments, 0, { clone: true }), + decomposed = this._decomposed; + this.scale(scaling.x / current.x, scaling.y / current.y); + decomposed.scaling = scaling; + this._decomposed = decomposed; + } + }, + + getMatrix: function() { + return this._matrix; + }, + + setMatrix: function() { + var matrix = this._matrix; + matrix.initialize.apply(matrix, arguments); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(9); + } + }, + + getGlobalMatrix: function(_dontClone) { + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + var parent = this._parent; + if (parent) + matrix.preConcatenate(parent.getGlobalMatrix(true)); + matrix._updateVersion = updateVersion; + } + return _dontClone ? matrix : matrix.clone(); + }, + + getApplyMatrix: function() { + return this._applyMatrix; + }, + + setApplyMatrix: function(apply) { + if (this._applyMatrix = this._canApplyMatrix && !!apply) + this.transform(null, true); + }, + + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', +}, { + getProject: function() { + return this._project; + }, + + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); + this._project = project; + var children = this._children; + for (var i = 0, l = children && children.length; i < l; i++) + children[i]._setProject(project); + installEvents = true; + } + if (installEvents) + this._installEvents(true); + }, + + getView: function() { + return this._project.getView(); + }, + + _installEvents: function _installEvents(install) { + _installEvents.base.call(this, install); + var children = this._children; + for (var i = 0, l = children && children.length; i < l; i++) + children[i]._installEvents(install); + }, + + getLayer: function() { + var parent = this; + while (parent = parent._parent) { + if (parent instanceof Layer) + return parent; + } + return null; + }, + + getParent: function() { + return this._parent; + }, + + setParent: function(item) { + return item.addChild(this); + }, + + getChildren: function() { + return this._children; + }, + + setChildren: function(items) { + this.removeChildren(); + this.addChildren(items); + }, + + getFirstChild: function() { + return this._children && this._children[0] || null; + }, + + getLastChild: function() { + return this._children && this._children[this._children.length - 1] + || null; + }, + + getNextSibling: function() { + return this._parent && this._parent._children[this._index + 1] || null; + }, + + getPreviousSibling: function() { + return this._parent && this._parent._children[this._index - 1] || null; + }, + + getIndex: function() { + return this._index; + }, + + equals: function(item) { + return item === this || item && this._class === item._class + && this._style.equals(item._style) + && this._matrix.equals(item._matrix) + && this._locked === item._locked + && this._visible === item._visible + && this._blendMode === item._blendMode + && this._opacity === item._opacity + && this._clipMask === item._clipMask + && this._guide === item._guide + && this._equals(item) + || false; + }, + + _equals: function(item) { + return Base.equals(this._children, item._children); + }, + + clone: function(insert) { + return this._clone(new this.constructor(Item.NO_INSERT), insert); + }, + + _clone: function(copy, insert, includeMatrix) { + var keys = ['_locked', '_visible', '_blendMode', '_opacity', + '_clipMask', '_guide'], + children = this._children; + copy.setStyle(this._style); + for (var i = 0, l = children && children.length; i < l; i++) { + copy.addChild(children[i].clone(false), true); + } + for (var i = 0, l = keys.length; i < l; i++) { + var key = keys[i]; + if (this.hasOwnProperty(key)) + copy[key] = this[key]; + } + if (includeMatrix !== false) + copy._matrix.initialize(this._matrix); + copy.setApplyMatrix(this._applyMatrix); + copy.setPivot(this._pivot); + copy.setSelected(this._selected); + copy._data = this._data ? Base.clone(this._data) : null; + if (insert || insert === undefined) + copy.insertAbove(this); + if (this._name) + copy.setName(this._name, true); + return copy; + }, + + copyTo: function(itemOrProject) { + return itemOrProject.addChild(this.clone(false)); + }, + + rasterize: function(resolution) { + var bounds = this.getStrokeBounds(), + scale = (resolution || this.getView().getResolution()) / 72, + topLeft = bounds.getTopLeft().floor(), + bottomRight = bounds.getBottomRight().ceil(), + size = new Size(bottomRight.subtract(topLeft)), + canvas = CanvasProvider.getCanvas(size.multiply(scale)), + ctx = canvas.getContext('2d'), + matrix = new Matrix().scale(scale).translate(topLeft.negate()); + ctx.save(); + matrix.applyToContext(ctx); + this.draw(ctx, new Base({ matrices: [matrix] })); + ctx.restore(); + var raster = new Raster(Item.NO_INSERT); + raster.setCanvas(canvas); + raster.transform(new Matrix().translate(topLeft.add(size.divide(2))) + .scale(1 / scale)); + raster.insertAbove(this); + return raster; + }, + + contains: function() { + return !!this._contains( + this._matrix._inverseTransform(Point.read(arguments))); + }, + + _contains: function(point) { + if (this._children) { + for (var i = this._children.length - 1; i >= 0; i--) { + if (this._children[i].contains(point)) + return true; + } + return false; + } + return point.isInside(this.getInternalBounds()); + }, + + isInside: function() { + return Rectangle.read(arguments).contains(this.getBounds()); + }, + + _asPathItem: function() { + return new Path.Rectangle({ + rectangle: this.getInternalBounds(), + matrix: this._matrix, + insert: false, + }); + }, + + intersects: function(item, _matrix) { + if (!(item instanceof Item)) + return false; + return this._asPathItem().getIntersections(item._asPathItem(), null, + _matrix || item._matrix, true).length > 0; + }, + + hitTest: function() { + return this._hitTest( + Point.read(arguments), + HitResult.getOptions(Base.read(arguments))); + }, + + _hitTest: function(point, options) { + if (this._locked || !this._visible || this._guide && !options.guides + || this.isEmpty()) + return null; + + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this.getView(), + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.chain(matrix) + : this.getGlobalMatrix().preConcatenate(view._matrix), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 1e-6) + ); + point = matrix._inverseTransform(point); + + if (!this._children && !this.getInternalRoughBounds() + .expand(tolerancePadding.multiply(2))._containsPoint(point)) + return null; + var checkSelf = !(options.guides && !this._guide + || options.selected && !this._selected + || options.type && options.type !== Base.hyphenate(this._class) + || options.class && !(this instanceof options.class)), + that = this, + res; + + function checkBounds(type, part) { + var pt = bounds['get' + part](); + if (point.subtract(pt).divide(tolerancePadding).length <= 1) + return new HitResult(type, that, + { name: Base.hyphenate(part), point: pt }); + } + + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); + if (options.center) + res = checkBounds('center', 'Center'); + if (!res && options.bounds) { + var points = [ + 'TopLeft', 'TopRight', 'BottomLeft', 'BottomRight', + 'LeftCenter', 'TopCenter', 'RightCenter', 'BottomCenter' + ]; + for (var i = 0; i < 8 && !res; i++) + res = checkBounds('bounds', points[i]); + } + } + + var children = !res && this._children; + if (children) { + var opts = this._getChildHitTestOptions(options); + for (var i = children.length - 1; i >= 0 && !res; i--) + res = children[i]._hitTest(point, opts); + } + if (!res && checkSelf) + res = this._hitTestSelf(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; + return res; + }, + + _getChildHitTestOptions: function(options) { + return options; + }, + + _hitTestSelf: function(point, options) { + if (options.fill && this.hasFill() && this._contains(point)) + return new HitResult('fill', this); + }, + + matches: function(name, compare) { + function matchObject(obj1, obj2) { + for (var i in obj1) { + if (obj1.hasOwnProperty(i)) { + var val1 = obj1[i], + val2 = obj2[i]; + if (Base.isPlainObject(val1) && Base.isPlainObject(val2)) { + if (!matchObject(val1, val2)) + return false; + } else if (!Base.equals(val1, val2)) { + return false; + } + } + } + return true; + } + var type = typeof name; + if (type === 'object') { + for (var key in name) { + if (name.hasOwnProperty(key) && !this.matches(key, name[key])) + return false; + } + } else if (type === 'function') { + return name(this); + } else { + var value = /^(empty|editable)$/.test(name) + ? this['is' + Base.capitalize(name)]() + : name === 'type' + ? Base.hyphenate(this._class) + : this[name]; + if (/^(constructor|class)$/.test(name)) { + if (!(this instanceof compare)) + return false; + } else if (compare instanceof RegExp) { + if (!compare.test(value)) + return false; + } else if (typeof compare === 'function') { + if (!compare(value)) + return false; + } else if (Base.isPlainObject(compare)) { + if (!matchObject(compare, value)) + return false; + } else if (!Base.equals(value, compare)) { + return false; + } + } + return true; + }, + + getItems: function(match) { + return Item._getItems(this._children, match, this._matrix); + }, + + getItem: function(match) { + return Item._getItems(this._children, match, this._matrix, null, true) + [0] || null; + }, + + statics: { + _getItems: function _getItems(children, match, matrix, param, + firstOnly) { + if (!param && typeof match === 'object') { + var overlapping = match.overlapping, + inside = match.inside, + bounds = overlapping || inside, + rect = bounds && Rectangle.read([bounds]); + param = { + items: [], + inside: !!inside, + overlapping: !!overlapping, + rect: rect, + path: overlapping && new Path.Rectangle({ + rectangle: rect, + insert: false + }) + }; + if (bounds) + match = Base.set({}, match, + { inside: true, overlapping: true }); + } + var items = param && param.items, + rect = param && param.rect; + matrix = rect && (matrix || new Matrix()); + for (var i = 0, l = children && children.length; i < l; i++) { + var child = children[i], + childMatrix = matrix && matrix.chain(child._matrix), + add = true; + if (rect) { + var bounds = child.getBounds(childMatrix); + if (!rect.intersects(bounds)) + continue; + if (!(param.inside && rect.contains(bounds)) + && !(param.overlapping && (bounds.contains(rect) + || param.path.intersects(child, childMatrix)))) + add = false; + } + if (add && child.matches(match)) { + items.push(child); + if (firstOnly) + break; + } + _getItems(child._children, match, + childMatrix, param, + firstOnly); + if (firstOnly && items.length > 0) + break; + } + return items; + } + } +}, { + + importJSON: function(json) { + var res = Base.importJSON(json, this); + return res !== this + ? this.addChild(res) + : res; + }, + + addChild: function(item, _preserve) { + return this.insertChild(undefined, item, _preserve); + }, + + insertChild: function(index, item, _preserve) { + var res = item ? this.insertChildren(index, [item], _preserve) : null; + return res && res[0]; + }, + + addChildren: function(items, _preserve) { + return this.insertChildren(this._children.length, items, _preserve); + }, + + insertChildren: function(index, items, _preserve, _proto) { + var children = this._children; + if (children && items && items.length > 0) { + items = Array.prototype.slice.apply(items); + for (var i = items.length - 1; i >= 0; i--) { + var item = items[i]; + if (_proto && !(item instanceof _proto)) { + items.splice(i, 1); + } else { + var shift = item._parent === this && item._index < index; + if (item._remove(false, true) && shift) + index--; + } + } + Base.splice(children, items, index, 0); + var project = this._project, + notifySelf = project && project._changes; + for (var i = 0, l = items.length; i < l; i++) { + var item = items[i]; + item._parent = this; + item._setProject(this._project, true); + if (item._name) + item.setName(item._name); + if (notifySelf) + this._changed(5); + } + this._changed(11); + } else { + items = null; + } + return items; + }, + + _insertSibling: function(index, item, _preserve) { + return this._parent + ? this._parent.insertChild(index, item, _preserve) + : null; + }, + + insertAbove: function(item, _preserve) { + return item._insertSibling(item._index + 1, this, _preserve); + }, + + insertBelow: function(item, _preserve) { + return item._insertSibling(item._index, this, _preserve); + }, + + sendToBack: function() { + return (this._parent || this instanceof Layer && this._project) + .insertChild(0, this); + }, + + bringToFront: function() { + return (this._parent || this instanceof Layer && this._project) + .addChild(this); + }, + + appendTop: '#addChild', + + appendBottom: function(item) { + return this.insertChild(0, item); + }, + + moveAbove: '#insertAbove', + + moveBelow: '#insertBelow', + + reduce: function() { + if (this._children && this._children.length === 1) { + var child = this._children[0].reduce(); + child.insertAbove(this); + child.setStyle(this._style); + this.remove(); + return child; + } + return this; + }, + + _removeNamed: function() { + var parent = this._parent; + if (parent) { + var children = parent._children, + namedChildren = parent._namedChildren, + name = this._name, + namedArray = namedChildren[name], + index = namedArray ? namedArray.indexOf(this) : -1; + if (index !== -1) { + if (children[name] == this) + delete children[name]; + namedArray.splice(index, 1); + if (namedArray.length) { + children[name] = namedArray[namedArray.length - 1]; + } else { + delete namedChildren[name]; + } + } + } + }, + + _remove: function(notifySelf, notifyParent) { + var parent = this._parent; + if (parent) { + if (this._name) + this._removeNamed(); + if (this._index != null) + Base.splice(parent._children, null, this._index, 1); + this._installEvents(false); + if (notifySelf) { + var project = this._project; + if (project && project._changes) + this._changed(5); + } + if (notifyParent) + parent._changed(11); + this._parent = null; + return true; + } + return false; + }, + + remove: function() { + return this._remove(true, true); + }, + + replaceWith: function(item) { + var ok = item && item.insertBelow(this); + if (ok) + this.remove(); + return ok; + }, + + removeChildren: function(from, to) { + if (!this._children) + return null; + from = from || 0; + to = Base.pick(to, this._children.length); + var removed = Base.splice(this._children, null, from, to - from); + for (var i = removed.length - 1; i >= 0; i--) { + removed[i]._remove(true, false); + } + if (removed.length > 0) + this._changed(11); + return removed; + }, + + clear: '#removeChildren', + + reverseChildren: function() { + if (this._children) { + this._children.reverse(); + for (var i = 0, l = this._children.length; i < l; i++) + this._children[i]._index = i; + this._changed(11); + } + }, + + isEmpty: function() { + return !this._children || this._children.length === 0; + }, + + isEditable: function() { + var item = this; + while (item) { + if (!item._visible || item._locked) + return false; + item = item._parent; + } + return true; + }, + + hasFill: function() { + return this.getStyle().hasFill(); + }, + + hasStroke: function() { + return this.getStyle().hasStroke(); + }, + + hasShadow: function() { + return this.getStyle().hasShadow(); + }, + + _getOrder: function(item) { + function getList(item) { + var list = []; + do { + list.unshift(item); + } while (item = item._parent); + return list; + } + var list1 = getList(this), + list2 = getList(item); + for (var i = 0, l = Math.min(list1.length, list2.length); i < l; i++) { + if (list1[i] != list2[i]) { + return list1[i]._index < list2[i]._index ? 1 : -1; + } + } + return 0; + }, + + hasChildren: function() { + return this._children && this._children.length > 0; + }, + + isInserted: function() { + return this._parent ? this._parent.isInserted() : false; + }, + + isAbove: function(item) { + return this._getOrder(item) === -1; + }, + + isBelow: function(item) { + return this._getOrder(item) === 1; + }, + + isParent: function(item) { + return this._parent === item; + }, + + isChild: function(item) { + return item && item._parent === this; + }, + + isDescendant: function(item) { + var parent = this; + while (parent = parent._parent) { + if (parent == item) + return true; + } + return false; + }, + + isAncestor: function(item) { + return item ? item.isDescendant(this) : false; + }, + + isSibling: function(item) { + return this._parent === item._parent; + }, + + isGroupedWith: function(item) { + var parent = this._parent; + while (parent) { + if (parent._parent + && /^(Group|Layer|CompoundPath)$/.test(parent._class) + && item.isDescendant(parent)) + return true; + parent = parent._parent; + } + return false; + }, + + translate: function() { + var mx = new Matrix(); + return this.transform(mx.translate.apply(mx, arguments)); + }, + + rotate: function(angle ) { + return this.transform(new Matrix().rotate(angle, + Point.read(arguments, 1, { readNull: true }) + || this.getPosition(true))); + } +}, Base.each(['scale', 'shear', 'skew'], function(name) { + this[name] = function() { + var point = Point.read(arguments), + center = Point.read(arguments, 0, { readNull: true }); + return this.transform(new Matrix()[name](point, + center || this.getPosition(true))); + }; +}, { + +}), { + transform: function(matrix, _applyMatrix, _applyRecursively, + _setApplyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && ((!_matrix.isIdentity() || matrix) + || _applyMatrix && _applyRecursively && this._children); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix, + _applyRecursively, _setApplyMatrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + _matrix._transformPoint(pivot, pivot, true); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); + if (_setApplyMatrix && this._canApplyMatrix) + this._applyMatrix = true; + } + var bounds = this._bounds, + position = this._position; + this._changed(9); + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { + for (var key in bounds) { + var rect = bounds[key]; + if (applyMatrix || !rect._internal) + matrix._transformBounds(rect, rect); + } + var getter = this._boundsGetter, + rect = bounds[getter && getter.getBounds || getter || 'getBounds']; + if (rect) + this._position = rect.getCenter(true); + this._bounds = bounds; + } else if (matrix && position) { + this._position = matrix._transformPoint(position, position); + } + return this; + }, + + _transformContent: function(matrix, applyRecursively, setApplyMatrix) { + var children = this._children; + if (children) { + for (var i = 0, l = children.length; i < l; i++) + children[i].transform(matrix, true, applyRecursively, + setApplyMatrix); + return true; + } + }, + + globalToLocal: function() { + return this.getGlobalMatrix(true)._inverseTransform( + Point.read(arguments)); + }, + + localToGlobal: function() { + return this.getGlobalMatrix(true)._transformPoint( + Point.read(arguments)); + }, + + parentToLocal: function() { + return this._matrix._inverseTransform(Point.read(arguments)); + }, + + localToParent: function() { + return this._matrix._transformPoint(Point.read(arguments)); + }, + + fitBounds: function(rectangle, fill) { + rectangle = Rectangle.read(arguments); + var bounds = this.getBounds(), + itemRatio = bounds.height / bounds.width, + rectRatio = rectangle.height / rectangle.width, + scale = (fill ? itemRatio > rectRatio : itemRatio < rectRatio) + ? rectangle.width / bounds.width + : rectangle.height / bounds.height, + newBounds = new Rectangle(new Point(), + new Size(bounds.width * scale, bounds.height * scale)); + newBounds.setCenter(rectangle.getCenter()); + this.setBounds(newBounds); + }, + + _setStyles: function(ctx) { + var style = this._style, + fillColor = style.getFillColor(), + strokeColor = style.getStrokeColor(), + shadowColor = style.getShadowColor(); + if (fillColor) + ctx.fillStyle = fillColor.toCanvasStyle(ctx); + if (strokeColor) { + var strokeWidth = style.getStrokeWidth(); + if (strokeWidth > 0) { + ctx.strokeStyle = strokeColor.toCanvasStyle(ctx); + ctx.lineWidth = strokeWidth; + var strokeJoin = style.getStrokeJoin(), + strokeCap = style.getStrokeCap(), + miterLimit = style.getMiterLimit(); + if (strokeJoin) + ctx.lineJoin = strokeJoin; + if (strokeCap) + ctx.lineCap = strokeCap; + if (miterLimit) + ctx.miterLimit = miterLimit; + if (paper.support.nativeDash) { + var dashArray = style.getDashArray(), + dashOffset = style.getDashOffset(); + if (dashArray && dashArray.length) { + if ('setLineDash' in ctx) { + ctx.setLineDash(dashArray); + ctx.lineDashOffset = dashOffset; + } else { + ctx.mozDash = dashArray; + ctx.mozDashOffset = dashOffset; + } + } + } + } + } + if (shadowColor) { + var shadowBlur = style.getShadowBlur(); + if (shadowBlur > 0) { + ctx.shadowColor = shadowColor.toCanvasStyle(ctx); + ctx.shadowBlur = shadowBlur; + var offset = this.getShadowOffset(); + ctx.shadowOffsetX = offset.x; + ctx.shadowOffsetY = offset.y; + } + } + }, + + draw: function(ctx, param, parentStrokeMatrix) { + var updateVersion = this._updateVersion = this._project._updateVersion; + if (!this._visible || this._opacity === 0) + return; + var matrices = param.matrices, + viewMatrix = param.viewMatrix, + matrix = this._matrix, + globalMatrix = matrices[matrices.length - 1].chain(matrix); + if (!globalMatrix.isInvertible()) + return; + + function getViewMatrix(matrix) { + return viewMatrix ? viewMatrix.chain(matrix) : matrix; + } + + matrices.push(globalMatrix); + if (param.updateMatrix) { + globalMatrix._updateVersion = updateVersion; + this._globalMatrix = globalMatrix; + } + + var blendMode = this._blendMode, + opacity = this._opacity, + normalBlend = blendMode === 'normal', + nativeBlend = BlendMode.nativeModes[blendMode], + direct = normalBlend && opacity === 1 + || param.dontStart + || param.clip + || (nativeBlend || normalBlend && opacity < 1) + && this._canComposite(), + pixelRatio = param.pixelRatio || 1, + mainCtx, itemOffset, prevOffset; + if (!direct) { + var bounds = this.getStrokeBounds(getViewMatrix(globalMatrix)); + if (!bounds.width || !bounds.height) + return; + prevOffset = param.offset; + itemOffset = param.offset = bounds.getTopLeft().floor(); + mainCtx = ctx; + ctx = CanvasProvider.getContext(bounds.getSize().ceil().add(1) + .multiply(pixelRatio)); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); + } + ctx.save(); + var strokeMatrix = parentStrokeMatrix + ? parentStrokeMatrix.chain(matrix) + : !this.getStrokeScaling(true) && getViewMatrix(globalMatrix), + clip = !direct && param.clipItem, + transform = !strokeMatrix || clip; + if (direct) { + ctx.globalAlpha = opacity; + if (nativeBlend) + ctx.globalCompositeOperation = blendMode; + } else if (transform) { + ctx.translate(-itemOffset.x, -itemOffset.y); + } + if (transform) + (direct ? matrix : getViewMatrix(globalMatrix)).applyToContext(ctx); + if (clip) + param.clipItem.draw(ctx, param.extend({ clip: true })); + if (strokeMatrix) { + ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); + var offset = param.offset; + if (offset) + ctx.translate(-offset.x, -offset.y); + } + this._draw(ctx, param, strokeMatrix); + ctx.restore(); + matrices.pop(); + if (param.clip && !param.dontFinish) + ctx.clip(); + if (!direct) { + BlendMode.process(blendMode, ctx, mainCtx, opacity, + itemOffset.subtract(prevOffset).multiply(pixelRatio)); + CanvasProvider.release(ctx); + param.offset = prevOffset; + } + }, + + _isUpdated: function(updateVersion) { + var parent = this._parent; + if (parent instanceof CompoundPath) + return parent._isUpdated(updateVersion); + var updated = this._updateVersion === updateVersion; + if (!updated && parent && parent._visible + && parent._isUpdated(updateVersion)) { + this._updateVersion = updateVersion; + updated = true; + } + return updated; + }, + + _drawSelection: function(ctx, matrix, size, selectedItems, updateVersion) { + if ((this._drawSelected || this._boundsSelected) + && this._isUpdated(updateVersion)) { + var color = this.getSelectedColor(true) + || this.getLayer().getSelectedColor(true), + mx = matrix.chain(this.getGlobalMatrix(true)); + ctx.strokeStyle = ctx.fillStyle = color + ? color.toCanvasStyle(ctx) : '#009dec'; + if (this._drawSelected) + this._drawSelected(ctx, mx, selectedItems); + if (this._boundsSelected) { + var half = size / 2, + coords = mx._transformCorners(this.getInternalBounds()); + ctx.beginPath(); + for (var i = 0; i < 8; i++) + ctx[i === 0 ? 'moveTo' : 'lineTo'](coords[i], coords[++i]); + ctx.closePath(); + ctx.stroke(); + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); + } + } + }, + + _canComposite: function() { + return false; + } +}, Base.each(['down', 'drag', 'up', 'move'], function(name) { + this['removeOn' + Base.capitalize(name)] = function() { + var hash = {}; + hash[name] = true; + return this.removeOn(hash); + }; +}, { + + removeOn: function(obj) { + for (var name in obj) { + if (obj[name]) { + var key = 'mouse' + name, + project = this._project, + sets = project._removeSets = project._removeSets || {}; + sets[key] = sets[key] || {}; + sets[key][this._id] = this; + } + } + return this; + } +})); + +var Group = Item.extend({ + _class: 'Group', + _selectChildren: true, + _serializeFields: { + children: [] + }, + + initialize: function Group(arg) { + this._children = []; + this._namedChildren = {}; + if (!this._initialize(arg)) + this.addChildren(Array.isArray(arg) ? arg : arguments); + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & 1026) { + this._clipItem = undefined; + } + }, + + _getClipItem: function() { + var clipItem = this._clipItem; + if (clipItem === undefined) { + clipItem = null; + for (var i = 0, l = this._children.length; i < l; i++) { + var child = this._children[i]; + if (child._clipMask) { + clipItem = child; + break; + } + } + this._clipItem = clipItem; + } + return clipItem; + }, + + isClipped: function() { + return !!this._getClipItem(); + }, + + setClipped: function(clipped) { + var child = this.getFirstChild(); + if (child) + child.setClipMask(clipped); + }, + + _draw: function(ctx, param) { + var clip = param.clip, + clipItem = !clip && this._getClipItem(), + draw = true; + param = param.extend({ clipItem: clipItem, clip: false }); + if (clip) { + if (this._currentPath) { + ctx.currentPath = this._currentPath; + draw = false; + } else { + ctx.beginPath(); + param.dontStart = param.dontFinish = true; + } + } else if (clipItem) { + clipItem.draw(ctx, param.extend({ clip: true })); + } + if (draw) { + for (var i = 0, l = this._children.length; i < l; i++) { + var item = this._children[i]; + if (item !== clipItem) + item.draw(ctx, param); + } + } + if (clip) { + this._currentPath = ctx.currentPath; + } + } +}); + +var Layer = Group.extend({ + _class: 'Layer', + + initialize: function Layer(arg) { + var props = Base.isPlainObject(arg) + ? new Base(arg) + : { children: Array.isArray(arg) ? arg : arguments }, + insert = props.insert; + props.insert = false; + Group.call(this, props); + if (insert || insert === undefined) { + this._project.addChild(this); + this.activate(); + } + }, + + _remove: function _remove(notifySelf, notifyParent) { + if (this._parent) + return _remove.base.call(this, notifySelf, notifyParent); + if (this._index != null) { + var project = this._project; + if (project._activeLayer === this) + project._activeLayer = this.getNextSibling() + || this.getPreviousSibling(); + Base.splice(project.layers, null, this._index, 1); + this._installEvents(false); + if (notifySelf && project._changes) + this._changed(5); + if (notifyParent) { + project._needsUpdate = true; + } + return true; + } + return false; + }, + + getNextSibling: function getNextSibling() { + return this._parent ? getNextSibling.base.call(this) + : this._project.layers[this._index + 1] || null; + }, + + getPreviousSibling: function getPreviousSibling() { + return this._parent ? getPreviousSibling.base.call(this) + : this._project.layers[this._index - 1] || null; + }, + + isInserted: function isInserted() { + return this._parent ? isInserted.base.call(this) : this._index != null; + }, + + activate: function() { + this._project._activeLayer = this; + }, + + _insertSibling: function _insertSibling(index, item, _preserve) { + return !this._parent + ? this._project.insertChild(index, item, _preserve) + : _insertSibling.base.call(this, index, item, _preserve); + } +}); + +var Shape = Item.extend({ + _class: 'Shape', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsSelected: true, + _serializeFields: { + type: null, + size: null, + radius: null + }, + + initialize: function Shape(props) { + this._initialize(props); + }, + + _equals: function(item) { + return this._type === item._type + && this._size.equals(item._size) + && Base.equals(this._radius, item._radius); + }, + + clone: function(insert) { + var copy = new Shape(Item.NO_INSERT); + copy.setType(this._type); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); + }, + + getType: function() { + return this._type; + }, + + setType: function(type) { + this._type = type; + }, + + getShape: '#getType', + setShape: '#setType', + + getSize: function() { + var size = this._size; + return new LinkedSize(size.width, size.height, this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var type = this._type, + width = size.width, + height = size.height; + if (type === 'rectangle') { + var radius = Size.min(this._radius, size.divide(2)); + this._radius.set(radius.width, radius.height); + } else if (type === 'circle') { + width = height = (width + height) / 2; + this._radius = width / 2; + } else if (type === 'ellipse') { + this._radius.set(width / 2, height / 2); + } + this._size.set(width, height); + this._changed(9); + } + }, + + getRadius: function() { + var rad = this._radius; + return this._type === 'circle' + ? rad + : new LinkedSize(rad.width, rad.height, this, 'setRadius'); + }, + + setRadius: function(radius) { + var type = this._type; + if (type === 'circle') { + if (radius === this._radius) + return; + var size = radius * 2; + this._radius = radius; + this._size.set(size, size); + } else { + radius = Size.read(arguments); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (type === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (type === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } + } + } + this._changed(9); + }, + + isEmpty: function() { + return false; + }, + + toPath: function(insert) { + var path = this._clone(new Path[Base.capitalize(this._type)]({ + center: new Point(), + size: this._size, + radius: this._radius, + insert: false + }), insert); + if (paper.settings.applyMatrix) + path.setApplyMatrix(true); + return path; + }, + + _draw: function(ctx, param, strokeMatrix) { + var style = this._style, + hasFill = style.hasFill(), + hasStroke = style.hasStroke(), + dontPaint = param.dontFinish || param.clip, + untransformed = !strokeMatrix; + if (hasFill || hasStroke || dontPaint) { + var type = this._type, + radius = this._radius, + isCircle = type === 'circle'; + if (!param.dontStart) + ctx.beginPath(); + if (untransformed && isCircle) { + ctx.arc(0, 0, radius, 0, Math.PI * 2, true); + } else { + var rx = isCircle ? radius : radius.width, + ry = isCircle ? radius : radius.height, + size = this._size, + width = size.width, + height = size.height; + if (untransformed && type === 'rectangle' && rx === 0 && ry === 0) { + ctx.rect(-width / 2, -height / 2, width, height); + } else { + var x = width / 2, + y = height / 2, + kappa = 1 - 0.5522847498307936, + cx = rx * kappa, + cy = ry * kappa, + c = [ + -x, -y + ry, + -x, -y + cy, + -x + cx, -y, + -x + rx, -y, + x - rx, -y, + x - cx, -y, + x, -y + cy, + x, -y + ry, + x, y - ry, + x, y - cy, + x - cx, y, + x - rx, y, + -x + rx, y, + -x + cx, y, + -x, y - cy, + -x, y - ry + ]; + if (strokeMatrix) + strokeMatrix.transform(c, c, 32); + ctx.moveTo(c[0], c[1]); + ctx.bezierCurveTo(c[2], c[3], c[4], c[5], c[6], c[7]); + if (x !== rx) + ctx.lineTo(c[8], c[9]); + ctx.bezierCurveTo(c[10], c[11], c[12], c[13], c[14], c[15]); + if (y !== ry) + ctx.lineTo(c[16], c[17]); + ctx.bezierCurveTo(c[18], c[19], c[20], c[21], c[22], c[23]); + if (x !== rx) + ctx.lineTo(c[24], c[25]); + ctx.bezierCurveTo(c[26], c[27], c[28], c[29], c[30], c[31]); + } + } + ctx.closePath(); + } + if (!dontPaint && (hasFill || hasStroke)) { + this._setStyles(ctx); + if (hasFill) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (hasStroke) + ctx.stroke(); + } + }, + + _canComposite: function() { + return !(this.hasFill() && this.hasStroke()); + }, + + _getBounds: function(getter, matrix) { + var rect = new Rectangle(this._size).setCenter(0, 0); + if (getter !== 'getBounds' && this.hasStroke()) + rect = rect.expand(this.getStrokeWidth()); + return matrix ? matrix._transformBounds(rect) : rect; + } +}, +new function() { + function getCornerCenter(that, point, expand) { + var radius = that._radius; + if (!radius.isZero()) { + var halfSize = that._size.divide(2); + for (var i = 0; i < 4; i++) { + var dir = new Point(i & 1 ? 1 : -1, i > 1 ? 1 : -1), + corner = dir.multiply(halfSize), + center = corner.subtract(dir.multiply(radius)), + rect = new Rectangle(corner, center); + if ((expand ? rect.expand(expand) : rect).contains(point)) + return center; + } + } + } + + function getEllipseRadius(point, radius) { + var angle = point.getAngleInRadians(), + width = radius.width * 2, + height = radius.height * 2, + x = width * Math.sin(angle), + y = height * Math.cos(angle); + return width * height / (2 * Math.sqrt(x * x + y * y)); + } + + return { + _contains: function _contains(point) { + if (this._type === 'rectangle') { + var center = getCornerCenter(this, point); + return center + ? point.subtract(center).divide(this._radius) + .getLength() <= 1 + : _contains.base.call(this, point); + } else { + return point.divide(this.size).getLength() <= 0.5; + } + }, + + _hitTestSelf: function _hitTestSelf(point, options) { + var hit = false; + if (this.hasStroke()) { + var type = this._type, + radius = this._radius, + strokeWidth = this.getStrokeWidth() + 2 * options.tolerance; + if (type === 'rectangle') { + var center = getCornerCenter(this, point, strokeWidth); + if (center) { + var pt = point.subtract(center); + hit = 2 * Math.abs(pt.getLength() + - getEllipseRadius(pt, radius)) <= strokeWidth; + } else { + var rect = new Rectangle(this._size).setCenter(0, 0), + outer = rect.expand(strokeWidth), + inner = rect.expand(-strokeWidth); + hit = outer._containsPoint(point) + && !inner._containsPoint(point); + } + } else { + if (type === 'ellipse') + radius = getEllipseRadius(point, radius); + hit = 2 * Math.abs(point.getLength() - radius) + <= strokeWidth; + } + } + return hit + ? new HitResult('stroke', this) + : _hitTestSelf.base.apply(this, arguments); + } + }; +}, { + +statics: new function() { + function createShape(type, point, size, radius, args) { + var item = new Shape(Base.getNamed(args)); + item._type = type; + item._size = size; + item._radius = radius; + return item.translate(point); + } + + return { + Circle: function() { + var center = Point.readNamed(arguments, 'center'), + radius = Base.readNamed(arguments, 'radius'); + return createShape('circle', center, new Size(radius * 2), radius, + arguments); + }, + + Rectangle: function() { + var rect = Rectangle.readNamed(arguments, 'rectangle'), + radius = Size.min(Size.readNamed(arguments, 'radius'), + rect.getSize(true).divide(2)); + return createShape('rectangle', rect.getCenter(true), + rect.getSize(true), radius, arguments); + }, + + Ellipse: function() { + var ellipse = Shape._readEllipse(arguments), + radius = ellipse.radius; + return createShape('ellipse', ellipse.center, radius.multiply(2), + radius, arguments); + }, + + _readEllipse: function(args) { + var center, + radius; + if (Base.hasNamed(args, 'radius')) { + center = Point.readNamed(args, 'center'); + radius = Size.readNamed(args, 'radius'); + } else { + var rect = Rectangle.readNamed(args, 'rectangle'); + center = rect.getCenter(true); + radius = rect.getSize(true).divide(2); + } + return { center: center, radius: radius }; + } + }; +}}); + +var Raster = Item.extend({ + _class: 'Raster', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsGetter: 'getBounds', + _boundsSelected: true, + _serializeFields: { + crossOrigin: null, + source: null + }, + + initialize: function Raster(object, position) { + if (!this._initialize(object, + position !== undefined && Point.read(arguments, 1))) { + if (typeof object === 'string') { + this.setSource(object); + } else { + this.setImage(object); + } + } + if (!this._size) { + this._size = new Size(); + this._loaded = false; + } + }, + + _equals: function(item) { + return this.getSource() === item.getSource(); + }, + + clone: function(insert) { + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; + if (image) { + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setImage(copyCanvas); + } + copy._crossOrigin = this._crossOrigin; + return this._clone(copy, insert); + }, + + getSize: function() { + var size = this._size; + return new LinkedSize(size ? size.width : 0, size ? size.height : 0, + this, 'setSize'); + }, + + setSize: function() { + var size = Size.read(arguments); + if (!size.equals(this._size)) { + if (size.width > 0 && size.height > 0) { + var element = this.getElement(); + this.setImage(CanvasProvider.getCanvas(size)); + if (element) + this.getContext(true).drawImage(element, 0, 0, + size.width, size.height); + } else { + if (this._canvas) + CanvasProvider.release(this._canvas); + this._size = size.clone(); + } + } + }, + + getWidth: function() { + return this._size ? this._size.width : 0; + }, + + setWidth: function(width) { + this.setSize(width, this.getHeight()); + }, + + getHeight: function() { + return this._size ? this._size.height : 0; + }, + + setHeight: function(height) { + this.setSize(this.getWidth(), height); + }, + + isEmpty: function() { + var size = this._size; + return !size || size.width === 0 && size.height === 0; + }, + + getResolution: function() { + var matrix = this._matrix, + orig = new Point(0, 0).transform(matrix), + u = new Point(1, 0).transform(matrix).subtract(orig), + v = new Point(0, 1).transform(matrix).subtract(orig); + return new Size( + 72 / u.getLength(), + 72 / v.getLength() + ); + }, + + getPpi: '#getResolution', + + getImage: function() { + return this._image; + }, + + setImage: function(image) { + if (this._canvas) + CanvasProvider.release(this._canvas); + if (image && image.getContext) { + this._image = null; + this._canvas = image; + this._loaded = true; + } else { + this._image = image; + this._canvas = null; + this._loaded = image && image.complete; + } + this._size = new Size( + image ? image.naturalWidth || image.width : 0, + image ? image.naturalHeight || image.height : 0); + this._context = null; + this._changed(521); + }, + + getCanvas: function() { + if (!this._canvas) { + var ctx = CanvasProvider.getContext(this._size); + try { + if (this._image) + ctx.drawImage(this._image, 0, 0); + this._canvas = ctx.canvas; + } catch (e) { + CanvasProvider.release(ctx); + } + } + return this._canvas; + }, + + setCanvas: '#setImage', + + getContext: function(modify) { + if (!this._context) + this._context = this.getCanvas().getContext('2d'); + if (modify) { + this._image = null; + this._changed(513); + } + return this._context; + }, + + setContext: function(context) { + this._context = context; + }, + + getSource: function() { + return this._image && this._image.src || this.toDataURL(); + }, + + setSource: function(src) { + var that = this, + crossOrigin = this._crossOrigin, + image; + + function loaded() { + var view = that.getView(); + if (view) { + paper = view._scope; + that.setImage(image); + that.emit('load'); + view.update(); + } + } + + image = new Image(); + if (crossOrigin) + image.crossOrigin = crossOrigin; + if (/^data:/.test(src)) { + image.src = this._data = src; + setTimeout(loaded, 0); + } else if (/^https?:\/\//.test(src)) { + require('request').get({ + url: src, + encoding: null + }, function (err, response, data) { + if (err) + throw err; + if (response.statusCode == 200) { + image.src = this._data = data; + loaded(); + } + }); + } else { + require('fs').readFile(src, function (err, data) { + if (err) + throw err; + image.src = this._data = data; + loaded(); + }); + } + this.setImage(image); + }, + + getCrossOrigin: function() { + return this._image && this._image.crossOrigin || this._crossOrigin || ''; + }, + + setCrossOrigin: function(crossOrigin) { + this._crossOrigin = crossOrigin; + if (this._image) + this._image.crossOrigin = crossOrigin; + }, + + getElement: function() { + return this._canvas || this._loaded && this._image; + } +}, { + beans: false, + + getSubCanvas: function() { + var rect = Rectangle.read(arguments), + ctx = CanvasProvider.getContext(rect.getSize()); + ctx.drawImage(this.getCanvas(), rect.x, rect.y, + rect.width, rect.height, 0, 0, rect.width, rect.height); + return ctx.canvas; + }, + + getSubRaster: function() { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setImage(this.getSubCanvas(rect)); + raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); + raster._matrix.preConcatenate(this._matrix); + raster.insertAbove(this); + return raster; + }, + + toDataURL: function() { + if (this._data) { + if (this._data instanceof Buffer) + this._data = this._data.toString('base64'); + return this._data; + } + var canvas = this.getCanvas(); + return canvas ? canvas.toDataURL.apply(canvas, arguments) : null; + }, + + drawImage: function(image ) { + var point = Point.read(arguments, 1); + this.getContext(true).drawImage(image, point.x, point.y); + }, + + getAverageColor: function(object) { + var bounds, path; + if (!object) { + bounds = this.getBounds(); + } else if (object instanceof PathItem) { + path = object; + bounds = object.getBounds(); + } else if (object.width) { + bounds = new Rectangle(object); + } else if (object.x) { + bounds = new Rectangle(object.x - 0.5, object.y - 0.5, 1, 1); + } + var sampleSize = 32, + width = Math.min(bounds.width, sampleSize), + height = Math.min(bounds.height, sampleSize); + var ctx = Raster._sampleContext; + if (!ctx) { + ctx = Raster._sampleContext = CanvasProvider.getContext( + new Size(sampleSize)); + } else { + ctx.clearRect(0, 0, sampleSize + 1, sampleSize + 1); + } + ctx.save(); + var matrix = new Matrix() + .scale(width / bounds.width, height / bounds.height) + .translate(-bounds.x, -bounds.y); + matrix.applyToContext(ctx); + if (path) + path.draw(ctx, new Base({ clip: true, matrices: [matrix] })); + this._matrix.applyToContext(ctx); + var element = this.getElement(), + size = this._size; + if (element) + ctx.drawImage(element, -size.width / 2, -size.height / 2); + ctx.restore(); + var pixels = ctx.getImageData(0.5, 0.5, Math.ceil(width), + Math.ceil(height)).data, + channels = [0, 0, 0], + total = 0; + for (var i = 0, l = pixels.length; i < l; i += 4) { + var alpha = pixels[i + 3]; + total += alpha; + alpha /= 255; + channels[0] += pixels[i] * alpha; + channels[1] += pixels[i + 1] * alpha; + channels[2] += pixels[i + 2] * alpha; + } + for (var i = 0; i < 3; i++) + channels[i] /= total; + return total ? Color.read(channels) : null; + }, + + getPixel: function() { + var point = Point.read(arguments); + var data = this.getContext().getImageData(point.x, point.y, 1, 1).data; + return new Color('rgb', [data[0] / 255, data[1] / 255, data[2] / 255], + data[3] / 255); + }, + + setPixel: function() { + var point = Point.read(arguments), + color = Color.read(arguments), + components = color._convert('rgb'), + alpha = color._alpha, + ctx = this.getContext(true), + imageData = ctx.createImageData(1, 1), + data = imageData.data; + data[0] = components[0] * 255; + data[1] = components[1] * 255; + data[2] = components[2] * 255; + data[3] = alpha != null ? alpha * 255 : 255; + ctx.putImageData(imageData, point.x, point.y); + }, + + createImageData: function() { + var size = Size.read(arguments); + return this.getContext().createImageData(size.width, size.height); + }, + + getImageData: function() { + var rect = Rectangle.read(arguments); + if (rect.isEmpty()) + rect = new Rectangle(this._size); + return this.getContext().getImageData(rect.x, rect.y, + rect.width, rect.height); + }, + + setImageData: function(data ) { + var point = Point.read(arguments, 1); + this.getContext(true).putImageData(data, point.x, point.y); + }, + + _getBounds: function(getter, matrix) { + var rect = new Rectangle(this._size).setCenter(0, 0); + return matrix ? matrix._transformBounds(rect) : rect; + }, + + _hitTestSelf: function(point) { + if (this._contains(point)) { + var that = this; + return new HitResult('pixel', that, { + offset: point.add(that._size.divide(2)).round(), + color: { + get: function() { + return that.getPixel(this.offset); + } + } + }); + } + }, + + _draw: function(ctx) { + var element = this.getElement(); + if (element) { + ctx.globalAlpha = this._opacity; + ctx.drawImage(element, + -this._size.width / 2, -this._size.height / 2); + } + }, + + _canComposite: function() { + return true; + } +}); + +var PlacedSymbol = Item.extend({ + _class: 'PlacedSymbol', + _applyMatrix: false, + _canApplyMatrix: false, + _boundsGetter: { getBounds: 'getStrokeBounds' }, + _boundsSelected: true, + _serializeFields: { + symbol: null + }, + + initialize: function PlacedSymbol(arg0, arg1) { + if (!this._initialize(arg0, + arg1 !== undefined && Point.read(arguments, 1))) + this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0)); + }, + + _equals: function(item) { + return this._symbol === item._symbol; + }, + + getSymbol: function() { + return this._symbol; + }, + + setSymbol: function(symbol) { + this._symbol = symbol; + this._changed(9); + }, + + clone: function(insert) { + var copy = new PlacedSymbol(Item.NO_INSERT); + copy.setSymbol(this._symbol); + return this._clone(copy, insert); + }, + + isEmpty: function() { + return this._symbol._definition.isEmpty(); + }, + + _getBounds: function(getter, matrix, cacheItem) { + var definition = this.symbol._definition; + return definition._getCachedBounds(getter, + matrix && matrix.chain(definition._matrix), cacheItem); + }, + + _hitTestSelf: function(point, options) { + var res = this._symbol._definition._hitTest(point, options); + if (res) + res.item = this; + return res; + }, + + _draw: function(ctx, param) { + this.symbol._definition.draw(ctx, param); + } + +}); + +var HitResult = Base.extend({ + _class: 'HitResult', + + initialize: function HitResult(type, item, values) { + this.type = type; + this.item = item; + if (values) { + values.enumerable = true; + this.inject(values); + } + }, + + statics: { + getOptions: function(options) { + return new Base({ + type: null, + tolerance: paper.settings.hitTolerance, + fill: !options, + stroke: !options, + segments: !options, + handles: false, + ends: false, + center: false, + bounds: false, + guides: false, + selected: false + }, options); + } + } +}); + +var Segment = Base.extend({ + _class: 'Segment', + beans: true, + + initialize: function Segment(arg0, arg1, arg2, arg3, arg4, arg5) { + var count = arguments.length, + point, handleIn, handleOut; + if (count === 0) { + } else if (count === 1) { + if ('point' in arg0) { + point = arg0.point; + handleIn = arg0.handleIn; + handleOut = arg0.handleOut; + } else { + point = arg0; + } + } else if (count === 2 && typeof arg0 === 'number') { + point = arguments; + } else if (count <= 3) { + point = arg0; + handleIn = arg1; + handleOut = arg2; + } else { + point = arg0 !== undefined ? [ arg0, arg1 ] : null; + handleIn = arg2 !== undefined ? [ arg2, arg3 ] : null; + handleOut = arg4 !== undefined ? [ arg4, arg5 ] : null; + } + new SegmentPoint(point, this, '_point'); + new SegmentPoint(handleIn, this, '_handleIn'); + new SegmentPoint(handleOut, this, '_handleOut'); + }, + + _serialize: function(options) { + return Base.serialize(this.hasHandles() + ? [this._point, this._handleIn, this._handleOut] + : this._point, + options, true); + }, + + _changed: function(point) { + var path = this._path; + if (!path) + return; + var curves = path._curves, + index = this._index, + curve; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curve = index > 0 ? curves[index - 1] : path._closed + ? curves[curves.length - 1] : null)) + curve._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curve = curves[index])) + curve._changed(); + } + path._changed(25); + }, + + getPoint: function() { + return this._point; + }, + + setPoint: function() { + var point = Point.read(arguments); + this._point.set(point.x, point.y); + }, + + getHandleIn: function() { + return this._handleIn; + }, + + setHandleIn: function() { + var point = Point.read(arguments); + this._handleIn.set(point.x, point.y); + }, + + getHandleOut: function() { + return this._handleOut; + }, + + setHandleOut: function() { + var point = Point.read(arguments); + this._handleOut.set(point.x, point.y); + }, + + hasHandles: function() { + return !this._handleIn.isZero() || !this._handleOut.isZero(); + }, + + clearHandles: function() { + this._handleIn.set(0, 0); + this._handleOut.set(0, 0); + }, + + _selectionState: 0, + + isSelected: function(_point) { + var state = this._selectionState; + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) + : false; + }, + + setSelected: function(selected, _point) { + var path = this._path, + selected = !!selected, + state = this._selectionState, + oldState = state, + flag = !_point ? 7 + : _point === this._point ? 4 + : _point === this._handleIn ? 1 + : _point === this._handleOut ? 2 + : 0; + if (selected) { + state |= flag; + } else { + state &= ~flag; + } + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); + path._changed(129); + } + }, + + getIndex: function() { + return this._index !== undefined ? this._index : null; + }, + + getPath: function() { + return this._path || null; + }, + + getCurve: function() { + var path = this._path, + index = this._index; + if (path) { + if (index > 0 && !path._closed + && index === path._segments.length - 1) + index--; + return path.getCurves()[index] || null; + } + return null; + }, + + getLocation: function() { + var curve = this.getCurve(); + return curve + ? new CurveLocation(curve, this === curve._segment1 ? 0 : 1) + : null; + }, + + getNext: function() { + var segments = this._path && this._path._segments; + return segments && (segments[this._index + 1] + || this._path._closed && segments[0]) || null; + }, + + getPrevious: function() { + var segments = this._path && this._path._segments; + return segments && (segments[this._index - 1] + || this._path._closed && segments[segments.length - 1]) || null; + }, + + isFirst: function() { + return this._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._index === path._segments.length - 1 || false; + }, + + reverse: function() { + var handleIn = this._handleIn, + handleOut = this._handleOut, + inX = handleIn._x, + inY = handleIn._y; + handleIn.set(handleOut._x, handleOut._y); + handleOut.set(inX, inY); + }, + + reversed: function() { + return new Segment(this._point, this._handleOut, this._handleIn); + }, + + remove: function() { + return this._path ? !!this._path.removeSegment(this._index) : false; + }, + + clone: function() { + return new Segment(this._point, this._handleIn, this._handleOut); + }, + + equals: function(segment) { + return segment === this || segment && this._class === segment._class + && this._point.equals(segment._point) + && this._handleIn.equals(segment._handleIn) + && this._handleOut.equals(segment._handleOut) + || false; + }, + + toString: function() { + var parts = [ 'point: ' + this._point ]; + if (!this._handleIn.isZero()) + parts.push('handleIn: ' + this._handleIn); + if (!this._handleOut.isZero()) + parts.push('handleOut: ' + this._handleOut); + return '{ ' + parts.join(', ') + ' }'; + }, + + transform: function(matrix) { + this._transformCoordinates(matrix, new Array(6), true); + this._changed(); + }, + + _transformCoordinates: function(matrix, coords, change) { + var point = this._point, + handleIn = !change || !this._handleIn.isZero() + ? this._handleIn : null, + handleOut = !change || !this._handleOut.isZero() + ? this._handleOut : null, + x = point._x, + y = point._y, + i = 2; + coords[0] = x; + coords[1] = y; + if (handleIn) { + coords[i++] = handleIn._x + x; + coords[i++] = handleIn._y + y; + } + if (handleOut) { + coords[i++] = handleOut._x + x; + coords[i++] = handleOut._y + y; + } + if (matrix) { + matrix._transformCoordinates(coords, coords, i / 2); + x = coords[0]; + y = coords[1]; + if (change) { + point._x = x; + point._y = y; + i = 2; + if (handleIn) { + handleIn._x = coords[i++] - x; + handleIn._y = coords[i++] - y; + } + if (handleOut) { + handleOut._x = coords[i++] - x; + handleOut._y = coords[i++] - y; + } + } else { + if (!handleIn) { + coords[i++] = x; + coords[i++] = y; + } + if (!handleOut) { + coords[i++] = x; + coords[i++] = y; + } + } + } + return coords; + } +}); + +var SegmentPoint = Point.extend({ + initialize: function SegmentPoint(point, owner, key) { + var x, y, selected; + if (!point) { + x = y = 0; + } else if ((x = point[0]) !== undefined) { + y = point[1]; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; + } + y = pt.y; + selected = pt.selected; + } + this._x = x; + this._y = y; + this._owner = owner; + owner[key] = this; + if (selected) + this.setSelected(true); + }, + + set: function(x, y) { + this._x = x; + this._y = y; + this._owner._changed(this); + return this; + }, + + _serialize: function(options) { + var f = options.formatter, + x = f.number(this._x), + y = f.number(this._y); + return this.isSelected() + ? { x: x, y: y, selected: true } + : [x, y]; + }, + + getX: function() { + return this._x; + }, + + setX: function(x) { + this._x = x; + this._owner._changed(this); + }, + + getY: function() { + return this._y; + }, + + setY: function(y) { + this._y = y; + this._owner._changed(this); + }, + + isZero: function() { + return Numerical.isZero(this._x) && Numerical.isZero(this._y); + }, + + setSelected: function(selected) { + this._owner.setSelected(selected, this); + }, + + isSelected: function() { + return this._owner.isSelected(this); + } +}); + +var Curve = Base.extend({ + _class: 'Curve', + + initialize: function Curve(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) { + var count = arguments.length, + seg1, seg2, + point1, point2, + handle1, handle2; + if (count === 3) { + this._path = arg0; + seg1 = arg1; + seg2 = arg2; + } else if (count === 0) { + seg1 = new Segment(); + seg2 = new Segment(); + } else if (count === 1) { + if ('segment1' in arg0) { + seg1 = new Segment(arg0.segment1); + seg2 = new Segment(arg0.segment2); + } else if ('point1' in arg0) { + point1 = arg0.point1; + handle1 = arg0.handle1; + handle2 = arg0.handle2; + point2 = arg0.point2; + } else if (Array.isArray(arg0)) { + point1 = [arg0[0], arg0[1]]; + point2 = [arg0[6], arg0[7]]; + handle1 = [arg0[2] - arg0[0], arg0[3] - arg0[1]]; + handle2 = [arg0[4] - arg0[6], arg0[5] - arg0[7]]; + } + } else if (count === 2) { + seg1 = new Segment(arg0); + seg2 = new Segment(arg1); + } else if (count === 4) { + point1 = arg0; + handle1 = arg1; + handle2 = arg2; + point2 = arg3; + } else if (count === 8) { + point1 = [arg0, arg1]; + point2 = [arg6, arg7]; + handle1 = [arg2 - arg0, arg3 - arg1]; + handle2 = [arg4 - arg6, arg5 - arg7]; + } + this._segment1 = seg1 || new Segment(point1, null, handle1); + this._segment2 = seg2 || new Segment(point2, handle2, null); + }, + + _serialize: function(options) { + return Base.serialize(this.hasHandles() + ? [this.getPoint1(), this.getHandle1(), this.getHandle2(), + this.getPoint2()] + : [this.getPoint1(), this.getPoint2()], + options, true); + }, + + _changed: function() { + this._length = this._bounds = undefined; + }, + + clone: function() { + return new Curve(this._segment1, this._segment2); + }, + + toString: function() { + var parts = [ 'point1: ' + this._segment1._point ]; + if (!this._segment1._handleOut.isZero()) + parts.push('handle1: ' + this._segment1._handleOut); + if (!this._segment2._handleIn.isZero()) + parts.push('handle2: ' + this._segment2._handleIn); + parts.push('point2: ' + this._segment2._point); + return '{ ' + parts.join(', ') + ' }'; + }, + + remove: function() { + var removed = false; + if (this._path) { + var segment2 = this._segment2, + handleOut = segment2._handleOut; + removed = segment2.remove(); + if (removed) + this._segment1._handleOut.set(handleOut.x, handleOut.y); + } + return removed; + }, + + getPoint1: function() { + return this._segment1._point; + }, + + setPoint1: function() { + var point = Point.read(arguments); + this._segment1._point.set(point.x, point.y); + }, + + getPoint2: function() { + return this._segment2._point; + }, + + setPoint2: function() { + var point = Point.read(arguments); + this._segment2._point.set(point.x, point.y); + }, + + getHandle1: function() { + return this._segment1._handleOut; + }, + + setHandle1: function() { + var point = Point.read(arguments); + this._segment1._handleOut.set(point.x, point.y); + }, + + getHandle2: function() { + return this._segment2._handleIn; + }, + + setHandle2: function() { + var point = Point.read(arguments); + this._segment2._handleIn.set(point.x, point.y); + }, + + getSegment1: function() { + return this._segment1; + }, + + getSegment2: function() { + return this._segment2; + }, + + getPath: function() { + return this._path; + }, + + getIndex: function() { + return this._segment1._index; + }, + + getNext: function() { + var curves = this._path && this._path._curves; + return curves && (curves[this._segment1._index + 1] + || this._path._closed && curves[0]) || null; + }, + + getPrevious: function() { + var curves = this._path && this._path._curves; + return curves && (curves[this._segment1._index - 1] + || this._path._closed && curves[curves.length - 1]) || null; + }, + + isFirst: function() { + return this._segment1._index === 0; + }, + + isLast: function() { + var path = this._path; + return path && this._segment1._index === path._curves.length - 1 + || false; + }, + + isSelected: function() { + return this.getPoint1().isSelected() + && this.getHandle2().isSelected() + && this.getHandle2().isSelected() + && this.getPoint2().isSelected(); + }, + + setSelected: function(selected) { + this.getPoint1().setSelected(selected); + this.getHandle1().setSelected(selected); + this.getHandle2().setSelected(selected); + this.getPoint2().setSelected(selected); + }, + + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); + }, + + getPoints: function() { + var coords = this.getValues(), + points = []; + for (var i = 0; i < 8; i += 2) + points.push(new Point(coords[i], coords[i + 1])); + return points; + }, + + getLength: function() { + if (this._length == null) + this._length = Curve.getLength(this.getValues(), 0, 1); + return this._length; + }, + + getArea: function() { + return Curve.getArea(this.getValues()); + }, + + getLine: function() { + return new Line(this._segment1._point, this._segment2._point); + }, + + getPart: function(from, to) { + return new Curve(Curve.getPart(this.getValues(), from, to)); + }, + + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + + getIntersections: function(curve) { + return Curve._getIntersections(this.getValues(), + curve && curve !== this ? curve.getValues() : null, + this, curve, [], {}); + }, + + _getParameter: function(offset, isParameter) { + return isParameter + ? offset + : offset && offset.curve === this + ? offset.parameter + : offset === undefined && isParameter === undefined + ? 0.5 + : this.getParameterAt(offset, 0); + }, + + divide: function(offset, isParameter, _setHandles) { + var parameter = this._getParameter(offset, isParameter), + tMin = 4e-7, + tMax = 1 - tMin, + res = null; + if (parameter >= tMin && parameter <= tMax) { + var parts = Curve.subdivide(this.getValues(), parameter), + left = parts[0], + right = parts[1], + setHandles = _setHandles || this.hasHandles(), + segment1 = this._segment1, + segment2 = this._segment2, + path = this._path; + if (setHandles) { + segment1._handleOut.set(left[2] - left[0], + left[3] - left[1]); + segment2._handleIn.set(right[4] - right[6], + right[5] - right[7]); + } + var x = left[6], y = left[7], + segment = new Segment(new Point(x, y), + setHandles && new Point(left[4] - x, left[5] - y), + setHandles && new Point(right[2] - x, right[3] - y)); + if (path) { + path.insert(segment1._index + 1, segment); + res = this.getNext(); + } else { + this._segment2 = segment; + res = new Curve(segment, segment2); + } + } + return res; + }, + + split: function(offset, isParameter) { + return this._path + ? this._path.split(this._segment1._index, + this._getParameter(offset, isParameter)) + : null; + }, + + reversed: function() { + return new Curve(this._segment2.reversed(), this._segment1.reversed()); + }, + + clearHandles: function() { + this._segment1._handleOut.set(0, 0); + this._segment2._handleIn.set(0, 0); + }, + +statics: { + getValues: function(segment1, segment2, matrix) { + var p1 = segment1._point, + h1 = segment1._handleOut, + h2 = segment2._handleIn, + p2 = segment2._point, + values = [ + p1._x, p1._y, + p1._x + h1._x, p1._y + h1._y, + p2._x + h2._x, p2._y + h2._y, + p2._x, p2._y + ]; + if (matrix) + matrix._transformCoordinates(values, values, 4); + return values; + }, + + subdivide: function(v, t) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7]; + if (t === undefined) + t = 0.5; + var u = 1 - t, + p3x = u * p1x + t * c1x, p3y = u * p1y + t * c1y, + p4x = u * c1x + t * c2x, p4y = u * c1y + t * c2y, + p5x = u * c2x + t * p2x, p5y = u * c2y + t * p2y, + p6x = u * p3x + t * p4x, p6y = u * p3y + t * p4y, + p7x = u * p4x + t * p5x, p7y = u * p4y + t * p5y, + p8x = u * p6x + t * p7x, p8y = u * p6y + t * p7y; + return [ + [p1x, p1y, p3x, p3y, p6x, p6y, p8x, p8y], + [p8x, p8y, p7x, p7y, p5x, p5y, p2x, p2y] + ]; + }, + + solveCubic: function (v, coord, val, roots, min, max) { + var p1 = v[coord], + c1 = v[coord + 2], + c2 = v[coord + 4], + p2 = v[coord + 6], + c = 3 * (c1 - p1), + b = 3 * (c2 - c1) - c, + a = p2 - p1 - c - b; + return Numerical.solveCubic(a, b, c, p1 - val, roots, min, max); + }, + + getParameterOf: function(v, point) { + var p1 = new Point(v[0], v[1]), + p2 = new Point(v[6], v[7]), + epsilon = 1e-12, + t = point.isClose(p1, epsilon) ? 0 + : point.isClose(p2, epsilon) ? 1 + : null; + if (t !== null) + return t; + var coords = [point.x, point.y], + roots = [], + geomEpsilon = 2e-7; + for (var c = 0; c < 2; c++) { + var count = Curve.solveCubic(v, c, coords[c], roots, 0, 1); + for (var i = 0; i < count; i++) { + t = roots[i]; + if (point.isClose(Curve.getPoint(v, t), geomEpsilon)) + return t; + } + } + return point.isClose(p1, geomEpsilon) ? 0 + : point.isClose(p2, geomEpsilon) ? 1 + : null; + }, + + getNearestParameter: function(v, point) { + if (Curve.isStraight(v)) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + vx = p2x - p1x, vy = p2y - p1y, + det = vx * vx + vy * vy; + if (det === 0) + return 0; + var u = ((point.x - p1x) * vx + (point.y - p1y) * vy) / det; + return u < 1e-12 ? 0 + : u > 0.999999999999 ? 1 + : Curve.getParameterOf(v, + new Point(p1x + u * vx, p1y + u * vy)); + } + + var count = 100, + minDist = Infinity, + minT = 0; + + function refine(t) { + if (t >= 0 && t <= 1) { + var dist = point.getDistance(Curve.getPoint(v, t), true); + if (dist < minDist) { + minDist = dist; + minT = t; + return true; + } + } + } + + for (var i = 0; i <= count; i++) + refine(i / count); + + var step = 1 / (count * 2); + while (step > 4e-7) { + if (!refine(minT - step) && !refine(minT + step)) + step /= 2; + } + return minT; + }, + + getPart: function(v, from, to) { + var flip = from > to; + if (flip) { + var tmp = from; + from = to; + to = tmp; + } + if (from > 0) + v = Curve.subdivide(v, from)[1]; + if (to < 1) + v = Curve.subdivide(v, (to - from) / (1 - from))[0]; + return flip + ? [v[6], v[7], v[4], v[5], v[2], v[3], v[0], v[1]] + : v; + }, + + hasHandles: function(v) { + var isZero = Numerical.isZero; + return !(isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + && isZero(v[4] - v[6]) && isZero(v[5] - v[7])); + }, + + isFlatEnough: function(v, tolerance) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + ux = 3 * c1x - 2 * p1x - p2x, + uy = 3 * c1y - 2 * p1y - p2y, + vx = 3 * c2x - 2 * p2x - p1x, + vy = 3 * c2y - 2 * p2y - p1y; + return Math.max(ux * ux, vx * vx) + Math.max(uy * uy, vy * vy) + < 10 * tolerance * tolerance; + }, + + getArea: function(v) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + h1x = (v[2] + p1x) / 2, + h1y = (v[3] + p1y) / 2, + h2x = (v[4] + v[6]) / 2, + h2y = (v[5] + v[7]) / 2; + return 6 * ((p1x - h1x) * (h1y + p1y) + + (h1x - h2x) * (h2y + h1y) + + (h2x - p2x) * (p2y + h2y)) / 10; + }, + + getBounds: function(v) { + var min = v.slice(0, 2), + max = min.slice(), + roots = [0, 0]; + for (var i = 0; i < 2; i++) + Curve._addBounds(v[i], v[i + 2], v[i + 4], v[i + 6], + i, 0, min, max, roots); + return new Rectangle(min[0], min[1], max[0] - min[0], max[1] - min[1]); + }, + + _addBounds: function(v0, v1, v2, v3, coord, padding, min, max, roots) { + function add(value, padding) { + var left = value - padding, + right = value + padding; + if (left < min[coord]) + min[coord] = left; + if (right > max[coord]) + max[coord] = right; + } + var a = 3 * (v1 - v2) - v0 + v3, + b = 2 * (v0 + v2) - 4 * v1, + c = v1 - v0, + count = Numerical.solveQuadratic(a, b, c, roots), + tMin = 4e-7, + tMax = 1 - tMin; + add(v3, 0); + for (var i = 0; i < count; i++) { + var t = roots[i], + u = 1 - t; + if (tMin < t && t < tMax) + add(u * u * u * v0 + + 3 * u * u * t * v1 + + 3 * u * t * t * v2 + + t * t * t * v3, + padding); + } + } +}}, Base.each( + ['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], + function(name) { + this[name] = function() { + if (!this._bounds) + this._bounds = {}; + var bounds = this._bounds[name]; + if (!bounds) { + var path = this._path; + bounds = this._bounds[name] = Path[name]( + [this._segment1, this._segment2], false, + path && path.getStyle()); + } + return bounds.clone(); + }; + }, +{ + +}), Base.each({ + isStraight: function(l, h1, h2) { + if (h1.isZero() && h2.isZero()) { + return true; + } else if (l.isZero()) { + return false; + } else if (h1.isCollinear(l) && h2.isCollinear(l)) { + var div = l.dot(l), + p1 = l.dot(h1) / div, + p2 = l.dot(h2) / div; + return p1 >= 0 && p1 <= 1 && p2 <= 0 && p2 >= -1; + } + return false; + }, + + isLinear: function(l, h1, h2) { + var third = l.divide(3); + return h1.equals(third) && h2.negate().equals(third); + } +}, function(test, name) { + this[name] = function() { + var seg1 = this._segment1, + seg2 = this._segment2; + return test(seg2._point.subtract(seg1._point), + seg1._handleOut, seg2._handleIn); + }; + + this.statics[name] = function(v) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7]; + return test(new Point(p2x - p1x, p2y - p1y), + new Point(v[2] - p1x, v[3] - p1y), + new Point(v[4] - p2x, v[5] - p2y)); + }; +}, { + statics: {}, + + hasHandles: function() { + return !this._segment1._handleOut.isZero() + || !this._segment2._handleIn.isZero(); + }, + + isCollinear: function(curve) { + return curve && this.isStraight() && curve.isStraight() + && this.getLine().isCollinear(curve.getLine()); + }, + + isHorizontal: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).y) + < 1e-7; + }, + + isVertical: function() { + return this.isStraight() && Math.abs(this.getTangentAt(0.5, true).x) + < 1e-7; + } +}), { + beans: false, + + getParameterAt: function(offset, start) { + return Curve.getParameterAt(this.getValues(), offset, start); + }, + + getParameterOf: function() { + return Curve.getParameterOf(this.getValues(), Point.read(arguments)); + }, + + getLocationAt: function(offset, isParameter) { + var t = isParameter ? offset : this.getParameterAt(offset); + return t != null && t >= 0 && t <= 1 + ? new CurveLocation(this, t) + : null; + }, + + getLocationOf: function() { + return this.getLocationAt(this.getParameterOf(Point.read(arguments)), + true); + }, + + getOffsetOf: function() { + var loc = this.getLocationOf.apply(this, arguments); + return loc ? loc.getOffset() : null; + }, + + getNearestLocation: function() { + var point = Point.read(arguments), + values = this.getValues(), + t = Curve.getNearestParameter(values, point), + pt = Curve.getPoint(values, t); + return new CurveLocation(this, t, pt, null, point.getDistance(pt)); + }, + + getNearestPoint: function() { + return this.getNearestLocation.apply(this, arguments).getPoint(); + } + +}, +new function() { + var methods = ['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', + 'getWeightedNormal', 'getCurvature']; + return Base.each(methods, + function(name) { + this[name + 'At'] = function(offset, isParameter) { + var values = this.getValues(); + return Curve[name](values, isParameter ? offset + : Curve.getParameterAt(values, offset, 0)); + }; + }, { + statics: { + evaluateMethods: methods + } + }) +}, +new function() { + + function getLengthIntegrand(v) { + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + + ax = 9 * (c1x - c2x) + 3 * (p2x - p1x), + bx = 6 * (p1x + c2x) - 12 * c1x, + cx = 3 * (c1x - p1x), + + ay = 9 * (c1y - c2y) + 3 * (p2y - p1y), + by = 6 * (p1y + c2y) - 12 * c1y, + cy = 3 * (c1y - p1y); + + return function(t) { + var dx = (ax * t + bx) * t + cx, + dy = (ay * t + by) * t + cy; + return Math.sqrt(dx * dx + dy * dy); + }; + } + + function getIterations(a, b) { + return Math.max(2, Math.min(16, Math.ceil(Math.abs(b - a) * 32))); + } + + function evaluate(v, t, type, normalized) { + if (t == null || t < 0 || t > 1) + return null; + var p1x = v[0], p1y = v[1], + c1x = v[2], c1y = v[3], + c2x = v[4], c2y = v[5], + p2x = v[6], p2y = v[7], + tMin = 4e-7, + tMax = 1 - tMin, + x, y; + + if (type === 0 && (t < tMin || t > tMax)) { + var isZero = t < tMin; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; + } else { + var cx = 3 * (c1x - p1x), + bx = 3 * (c2x - c1x) - cx, + ax = p2x - p1x - cx - bx, + + cy = 3 * (c1y - p1y), + by = 3 * (c2y - c1y) - cy, + ay = p2y - p1y - cy - by; + if (type === 0) { + x = ((ax * t + bx) * t + cx) * t + p1x; + y = ((ay * t + by) * t + cy) * t + p1y; + } else { + if (t < tMin) { + x = cx; + y = cy; + } else if (t > tMax) { + x = 3 * (p2x - c2x); + y = 3 * (p2y - c2y); + } else { + x = (3 * ax * t + 2 * bx) * t + cx; + y = (3 * ay * t + 2 * by) * t + cy; + } + if (normalized) { + if (x === 0 && y === 0 && (t < tMin || t > tMax)) { + x = c2x - c1x; + y = c2y - c1y; + } + var len = Math.sqrt(x * x + y * y); + if (len) { + x /= len; + y /= len; + } + } + if (type === 3) { + var x2 = 6 * ax * t + 2 * bx, + y2 = 6 * ay * t + 2 * by, + d = Math.pow(x * x + y * y, 3 / 2); + x = d !== 0 ? (x * y2 - y * x2) / d : 0; + y = 0; + } + } + } + return type === 2 ? new Point(y, -x) : new Point(x, y); + } + + return { statics: { + + getLength: function(v, a, b) { + if (a === undefined) + a = 0; + if (b === undefined) + b = 1; + if (a === 0 && b === 1 && Curve.isStraight(v)) { + var dx = v[6] - v[0], + dy = v[7] - v[1]; + return Math.sqrt(dx * dx + dy * dy); + } + var ds = getLengthIntegrand(v); + return Numerical.integrate(ds, a, b, getIterations(a, b)); + }, + + getParameterAt: function(v, offset, start) { + if (start === undefined) + start = offset < 0 ? 1 : 0 + if (offset === 0) + return start; + var abs = Math.abs, + forward = offset > 0, + a = forward ? start : 0, + b = forward ? 1 : start, + ds = getLengthIntegrand(v), + rangeLength = Numerical.integrate(ds, a, b, + getIterations(a, b)); + if (abs(offset - rangeLength) < 1e-12) { + return forward ? b : a; + } else if (abs(offset) > rangeLength) { + return null; + } + var guess = offset / rangeLength, + length = 0; + function f(t) { + length += Numerical.integrate(ds, start, t, + getIterations(start, t)); + start = t; + return length - offset; + } + return Numerical.findRoot(f, ds, start + guess, a, b, 32, + 1e-12); + }, + + getPoint: function(v, t) { + return evaluate(v, t, 0, false); + }, + + getTangent: function(v, t) { + return evaluate(v, t, 1, true); + }, + + getWeightedTangent: function(v, t) { + return evaluate(v, t, 1, false); + }, + + getNormal: function(v, t) { + return evaluate(v, t, 2, true); + }, + + getWeightedNormal: function(v, t) { + return evaluate(v, t, 2, false); + }, + + getCurvature: function(v, t) { + return evaluate(v, t, 3, false).x; + } + }}; +}, +new function() { + + function addLocation(locations, param, v1, c1, t1, p1, v2, c2, t2, p2, + overlap) { + var startConnected = param.startConnected, + endConnected = param.endConnected, + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 == null) + t1 = Curve.getParameterOf(v1, p1); + if (t1 !== null && t1 >= (startConnected ? tMin : 0) && + t1 <= (endConnected ? tMax : 1)) { + if (t2 == null) + t2 = Curve.getParameterOf(v2, p2); + if (t2 !== null && t2 >= (endConnected ? tMin : 0) && + t2 <= (startConnected ? tMax : 1)) { + var renormalize = param.renormalize; + if (renormalize) { + var res = renormalize(t1, t2); + t1 = res[0]; + t2 = res[1]; + } + var loc1 = new CurveLocation(c1, t1, + p1 || Curve.getPoint(v1, t1), overlap), + loc2 = new CurveLocation(c2, t2, + p2 || Curve.getPoint(v2, t2), overlap), + flip = loc1.getPath() === loc2.getPath() + && loc1.getIndex() > loc2.getIndex(), + loc = flip ? loc2 : loc1, + include = param.include; + loc1._intersection = loc2; + loc2._intersection = loc1; + if (!include || include(loc)) { + CurveLocation.insert(locations, loc, true); + } + } + } + } + + function addCurveIntersections(v1, v2, c1, c2, locations, param, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { + if (++recursion >= 24) + return; + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + getSignedDistance = Line.getSignedDistance, + d1 = getSignedDistance(q0x, q0y, q3x, q3y, v2[2], v2[3]), + d2 = getSignedDistance(q0x, q0y, q3x, q3y, v2[4], v2[5]), + factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, + dMin = factor * Math.min(0, d1, d2), + dMax = factor * Math.max(0, d1, d2), + dp0 = getSignedDistance(q0x, q0y, q3x, q3y, v1[0], v1[1]), + dp1 = getSignedDistance(q0x, q0y, q3x, q3y, v1[2], v1[3]), + dp2 = getSignedDistance(q0x, q0y, q3x, q3y, v1[4], v1[5]), + dp3 = getSignedDistance(q0x, q0y, q3x, q3y, v1[6], v1[7]), + hull = getConvexHull(dp0, dp1, dp2, dp3), + top = hull[0], + bottom = hull[1], + tMinClip, + tMaxClip; + if ((tMinClip = clipConvexHull(top, bottom, dMin, dMax)) == null || + (tMaxClip = clipConvexHull(top.reverse(), bottom.reverse(), + dMin, dMax)) == null) + return; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + var tDiff = tMaxClip - tMinClip, + tMinNew = tMin + (tMax - tMin) * tMinClip, + tMaxNew = tMin + (tMax - tMin) * tMaxClip; + if (oldTDiff > 0.5 && tDiff > 0.5) { + if (tMaxNew - tMinNew > uMax - uMin) { + var parts = Curve.subdivide(v1, 0.5), + t = tMinNew + (tMaxNew - tMinNew) / 2; + addCurveIntersections( + v2, parts[0], c2, c1, locations, param, + uMin, uMax, tMinNew, t, tDiff, !reverse, recursion); + addCurveIntersections( + v2, parts[1], c2, c1, locations, param, + uMin, uMax, t, tMaxNew, tDiff, !reverse, recursion); + } else { + var parts = Curve.subdivide(v2, 0.5), + t = uMin + (uMax - uMin) / 2; + addCurveIntersections( + parts[0], v1, c2, c1, locations, param, + uMin, t, tMinNew, tMaxNew, tDiff, !reverse, recursion); + addCurveIntersections( + parts[1], v1, c2, c1, locations, param, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); + } + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) + < 1e-7) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + v1 = c1.getValues(); + v2 = c2.getValues(); + addLocation(locations, param, + reverse ? v2 : v1, reverse ? c2 : c1, reverse ? t2 : t1, null, + reverse ? v1 : v2, reverse ? c1 : c2, reverse ? t1 : t2, null); + } else if (tDiff > 1e-12) { + addCurveIntersections(v2, v1, c2, c1, locations, param, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); + } + } + + function getConvexHull(dq0, dq1, dq2, dq3) { + var p0 = [ 0, dq0 ], + p1 = [ 1 / 3, dq1 ], + p2 = [ 2 / 3, dq2 ], + p3 = [ 1, dq3 ], + dist1 = dq1 - (2 * dq0 + dq3) / 3, + dist2 = dq2 - (dq0 + 2 * dq3) / 3, + hull; + if (dist1 * dist2 < 0) { + hull = [[p0, p1, p3], [p0, p2, p3]]; + } else { + var distRatio = dist1 / dist2; + hull = [ + distRatio >= 2 ? [p0, p1, p3] + : distRatio <= .5 ? [p0, p2, p3] + : [p0, p1, p2, p3], + [p0, p3] + ]; + } + return (dist1 || dist2) < 0 ? hull.reverse() : hull; + } + + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + if (hullTop[0][1] < dMin) { + return clipConvexHullPart(hullTop, true, dMin); + } else if (hullBottom[0][1] > dMax) { + return clipConvexHullPart(hullBottom, false, dMax); + } else { + return hullTop[0][0]; + } + } + + function clipConvexHullPart(part, top, threshold) { + var px = part[0][0], + py = part[0][1]; + for (var i = 1, l = part.length; i < l; i++) { + var qx = part[i][0], + qy = part[i][1]; + if (top ? qy >= threshold : qy <= threshold) { + return qy === threshold ? qx + : px + (threshold - py) * (qx - px) / (qy - py); + } + px = qx; + py = qy; + } + return null; + } + + function addCurveLineIntersections(v1, v2, c1, c2, locations, param) { + var flip = Curve.isStraight(v1), + vc = flip ? v2 : v1, + vl = flip ? v1 : v2, + lx1 = vl[0], ly1 = vl[1], + lx2 = vl[6], ly2 = vl[7], + ldx = lx2 - lx1, + ldy = ly2 - ly1, + angle = Math.atan2(-ldy, ldx), + sin = Math.sin(angle), + cos = Math.cos(angle), + rvc = []; + for(var i = 0; i < 8; i += 2) { + var x = vc[i] - lx1, + y = vc[i + 1] - ly1; + rvc.push( + x * cos - y * sin, + x * sin + y * cos); + } + var roots = [], + count = Curve.solveCubic(rvc, 1, 0, roots, 0, 1); + for (var i = 0; i < count; i++) { + var tc = roots[i], + pc = Curve.getPoint(vc, tc), + tl = Curve.getParameterOf(vl, pc); + if (tl !== null) { + var pl = Curve.getPoint(vl, tl), + t1 = flip ? tl : tc, + t2 = flip ? tc : tl; + if (!param.endConnected || t2 > Numerical.CURVETIME_EPSILON) { + addLocation(locations, param, + v1, c1, t1, flip ? pl : pc, + v2, c2, t2, flip ? pc : pl); + } + } + } + } + + function addLineIntersection(v1, v2, c1, c2, locations, param) { + var pt = Line.intersect( + v1[0], v1[1], v1[6], v1[7], + v2[0], v2[1], v2[6], v2[7]); + if (pt) { + addLocation(locations, param, v1, c1, null, pt, v2, c2, null, pt); + } + } + + return { statics: { + _getIntersections: function(v1, v2, c1, c2, locations, param) { + if (!v2) { + return Curve._getSelfIntersection(v1, c1, locations, param); + } + var c1p1x = v1[0], c1p1y = v1[1], + c1p2x = v1[6], c1p2y = v1[7], + c2p1x = v2[0], c2p1y = v2[1], + c2p2x = v2[6], c2p2y = v2[7], + c1s1x = (3 * v1[2] + c1p1x) / 4, + c1s1y = (3 * v1[3] + c1p1y) / 4, + c1s2x = (3 * v1[4] + c1p2x) / 4, + c1s2y = (3 * v1[5] + c1p2y) / 4, + c2s1x = (3 * v2[2] + c2p1x) / 4, + c2s1y = (3 * v2[3] + c2p1y) / 4, + c2s2x = (3 * v2[4] + c2p2x) / 4, + c2s2y = (3 * v2[5] + c2p2y) / 4, + min = Math.min, + max = Math.max; + if (!( max(c1p1x, c1s1x, c1s2x, c1p2x) >= + min(c2p1x, c2s1x, c2s2x, c2p2x) && + min(c1p1x, c1s1x, c1s2x, c1p2x) <= + max(c2p1x, c2s1x, c2s2x, c2p2x) && + max(c1p1y, c1s1y, c1s2y, c1p2y) >= + min(c2p1y, c2s1y, c2s2y, c2p2y) && + min(c1p1y, c1s1y, c1s2y, c1p2y) <= + max(c2p1y, c2s1y, c2s2y, c2p2y))) + return locations; + if (!param.startConnected && !param.endConnected) { + var overlaps = Curve.getOverlaps(v1, v2); + if (overlaps) { + for (var i = 0; i < 2; i++) { + var overlap = overlaps[i]; + addLocation(locations, param, + v1, c1, overlap[0], null, + v2, c2, overlap[1], null, true); + } + return locations; + } + } + + var straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2, + epsilon = 1e-12, + before = locations.length; + (straight + ? addLineIntersection + : straight1 || straight2 + ? addCurveLineIntersections + : addCurveIntersections)( + v1, v2, c1, c2, locations, param, + 0, 1, 0, 1, 0, false, 0); + if (straight && locations.length > before) + return locations; + var c1p1 = new Point(c1p1x, c1p1y), + c1p2 = new Point(c1p2x, c1p2y), + c2p1 = new Point(c2p1x, c2p1y), + c2p2 = new Point(c2p2x, c2p2y); + if (c1p1.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 0, c2p1); + if (!param.startConnected && c1p1.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 0, c1p1, v2, c2, 1, c2p2); + if (!param.endConnected && c1p2.isClose(c2p1, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 0, c2p1); + if (c1p2.isClose(c2p2, epsilon)) + addLocation(locations, param, v1, c1, 1, c1p2, v2, c2, 1, c2p2); + return locations; + }, + + _getSelfIntersection: function(v1, c1, locations, param) { + var p1x = v1[0], p1y = v1[1], + h1x = v1[2], h1y = v1[3], + h2x = v1[4], h2y = v1[5], + p2x = v1[6], p2y = v1[7]; + var line = new Line(p1x, p1y, p2x, p2y, false), + side1 = line.getSide(new Point(h1x, h1y), true), + side2 = line.getSide(new Point(h2x, h2y), true); + if (side1 === side2) { + var edgeSum = (p1x - h2x) * (h1y - p2y) + + (h1x - p2x) * (h2y - p1y); + if (edgeSum * side1 > 0) + return locations; + } + var ax = p2x - 3 * h2x + 3 * h1x - p1x, + bx = h2x - 2 * h1x + p1x, + cx = h1x - p1x, + ay = p2y - 3 * h2y + 3 * h1y - p1y, + by = h2y - 2 * h1y + p1y, + cy = h1y - p1y, + ac = ay * cx - ax * cy, + ab = ay * bx - ax * by, + bc = by * cx - bx * cy; + if (ac * ac - 4 * ab * bc < 0) { + var roots = [], + tSplit, + count = Numerical.solveCubic( + ax * ax + ay * ay, + 3 * (ax * bx + ay * by), + 2 * (bx * bx + by * by) + ax * cx + ay * cy, + bx * cx + by * cy, + roots, 0, 1); + if (count > 0) { + for (var i = 0, maxCurvature = 0; i < count; i++) { + var curvature = Math.abs( + c1.getCurvatureAt(roots[i], true)); + if (curvature > maxCurvature) { + maxCurvature = curvature; + tSplit = roots[i]; + } + } + var parts = Curve.subdivide(v1, tSplit); + param.endConnected = true; + param.renormalize = function(t1, t2) { + return [t1 * tSplit, t2 * (1 - tSplit) + tSplit]; + }; + Curve._getIntersections(parts[0], parts[1], c1, c1, + locations, param); + } + } + return locations; + }, + + getOverlaps: function(v1, v2) { + var abs = Math.abs, + timeEpsilon = 4e-7, + geomEpsilon = 2e-7, + straight1 = Curve.isStraight(v1), + straight2 = Curve.isStraight(v2), + straight = straight1 && straight2; + + function getLineLengthSquared(v) { + var x = v[6] - v[0], + y = v[7] - v[1]; + return x * x + y * y; + } + + if (straight) { + var flip = getLineLengthSquared(v1) < getLineLengthSquared(v2), + l1 = flip ? v2 : v1, + l2 = flip ? v1 : v2, + line = new Line(l1[0], l1[1], l1[6], l1[7]); + if (line.getDistance(new Point(l2[0], l2[1])) > geomEpsilon || + line.getDistance(new Point(l2[6], l2[7])) > geomEpsilon) + return null; + } else if (straight1 ^ straight2) { + return null; + } + + var v = [v1, v2], + pairs = []; + for (var i = 0, t1 = 0; + i < 2 && pairs.length < 2; + i += t1 === 0 ? 0 : 1, t1 = t1 ^ 1) { + var t2 = Curve.getParameterOf(v[i ^ 1], new Point( + v[i][t1 === 0 ? 0 : 6], + v[i][t1 === 0 ? 1 : 7])); + if (t2 != null) { + var pair = i === 0 ? [t1, t2] : [t2, t1]; + if (pairs.length === 0 || + abs(pair[0] - pairs[0][0]) > timeEpsilon && + abs(pair[1] - pairs[0][1]) > timeEpsilon) + pairs.push(pair); + } + if (i === 1 && pairs.length === 0) + break; + } + if (pairs.length !== 2) { + pairs = null; + } else if (!straight) { + var o1 = Curve.getPart(v1, pairs[0][0], pairs[1][0]), + o2 = Curve.getPart(v2, pairs[0][1], pairs[1][1]); + if (abs(o2[2] - o1[2]) > geomEpsilon || + abs(o2[3] - o1[3]) > geomEpsilon || + abs(o2[4] - o1[4]) > geomEpsilon || + abs(o2[5] - o1[5]) > geomEpsilon) + pairs = null; + } + return pairs; + } + }}; +}); + +var CurveLocation = Base.extend({ + _class: 'CurveLocation', + beans: true, + + initialize: function CurveLocation(curve, parameter, point, + _overlap, _distance) { + if (parameter > 0.9999996) { + var next = curve.getNext(); + if (next) { + parameter = 0; + curve = next; + } + } + this._id = UID.get(CurveLocation); + this._setCurve(curve); + this._parameter = parameter; + this._point = point || curve.getPointAt(parameter, true); + this._overlap = _overlap; + this._distance = _distance; + this._intersection = this._next = this._prev = null; + }, + + _setCurve: function(curve) { + var path = curve._path; + this._version = path ? path._version : 0; + this._curve = curve; + this._segment = null; + this._segment1 = curve._segment1; + this._segment2 = curve._segment2; + }, + + _setSegment: function(segment) { + this._setCurve(segment.getCurve()); + this._segment = segment; + this._parameter = segment === this._segment1 ? 0 : 1; + this._point = segment._point.clone(); + }, + + getSegment: function() { + var curve = this.getCurve(), + segment = this._segment; + if (!segment) { + var parameter = this.getParameter(); + if (parameter === 0) { + segment = curve._segment1; + } else if (parameter === 1) { + segment = curve._segment2; + } else if (parameter != null) { + segment = curve.getPartLength(0, parameter) + < curve.getPartLength(parameter, 1) + ? curve._segment1 + : curve._segment2; + } + this._segment = segment; + } + return segment; + }, + + getCurve: function() { + var curve = this._curve, + path = curve && curve._path, + that = this; + if (path && path._version !== this._version) { + curve = this._parameter = this._curve = this._offset = null; + } + + function trySegment(segment) { + var curve = segment && segment.getCurve(); + if (curve && (that._parameter = curve.getParameterOf(that._point)) + != null) { + that._setCurve(curve); + that._segment = segment; + return curve; + } + } + + return curve + || trySegment(this._segment) + || trySegment(this._segment1) + || trySegment(this._segment2.getPrevious()); + }, + + getPath: function() { + var curve = this.getCurve(); + return curve && curve._path; + }, + + getIndex: function() { + var curve = this.getCurve(); + return curve && curve.getIndex(); + }, + + getParameter: function() { + var curve = this.getCurve(), + parameter = this._parameter; + return curve && parameter == null + ? this._parameter = curve.getParameterOf(this._point) + : parameter; + }, + + getPoint: function() { + return this._point; + }, + + getOffset: function() { + var offset = this._offset; + if (offset == null) { + offset = 0; + var path = this.getPath(), + index = this.getIndex(); + if (path && index != null) { + var curves = path.getCurves(); + for (var i = 0; i < index; i++) + offset += curves[i].getLength(); + } + this._offset = offset += this.getCurveOffset(); + } + return offset; + }, + + getCurveOffset: function() { + var curve = this.getCurve(), + parameter = this.getParameter(); + return parameter != null && curve && curve.getPartLength(0, parameter); + }, + + getIntersection: function() { + return this._intersection; + }, + + getDistance: function() { + return this._distance; + }, + + divide: function() { + var curve = this.getCurve(), + res = null; + if (curve) { + res = curve.divide(this.getParameter(), true); + if (res) + this._setSegment(res._segment1); + } + return res; + }, + + split: function() { + var curve = this.getCurve(); + return curve ? curve.split(this.getParameter(), true) : null; + }, + + equals: function(loc, _ignoreOther) { + var res = this === loc, + epsilon = 2e-7; + if (!res && loc instanceof CurveLocation + && this.getPath() === loc.getPath() + && this.getPoint().isClose(loc.getPoint(), epsilon)) { + var c1 = this.getCurve(), + c2 = loc.getCurve(), + abs = Math.abs, + diff = abs( + ((c1.isLast() && c2.isFirst() ? -1 : c1.getIndex()) + + this.getParameter()) - + ((c2.isLast() && c1.isFirst() ? -1 : c2.getIndex()) + + loc.getParameter())); + res = (diff < 4e-7 + || ((diff = abs(this.getOffset() - loc.getOffset())) < epsilon + || abs(this.getPath().getLength() - diff) < epsilon)) + && (_ignoreOther + || (!this._intersection && !loc._intersection + || this._intersection && this._intersection.equals( + loc._intersection, true))); + } + return res; + }, + + toString: function() { + var parts = [], + point = this.getPoint(), + f = Formatter.instance; + if (point) + parts.push('point: ' + point); + var index = this.getIndex(); + if (index != null) + parts.push('index: ' + index); + var parameter = this.getParameter(); + if (parameter != null) + parts.push('parameter: ' + f.number(parameter)); + if (this._distance != null) + parts.push('distance: ' + f.number(this._distance)); + return '{ ' + parts.join(', ') + ' }'; + }, + + isTouching: function() { + var inter = this._intersection; + if (inter && this.getTangent().isCollinear(inter.getTangent())) { + var curve1 = this.getCurve(), + curve2 = inter.getCurve(); + return !(curve1.isStraight() && curve2.isStraight() + && curve1.getLine().intersect(curve2.getLine())); + } + return false; + }, + + isCrossing: function() { + var inter = this._intersection; + if (!inter) + return false; + var t1 = this.getParameter(), + t2 = inter.getParameter(), + tMin = 4e-7, + tMax = 1 - tMin; + if (t1 >= tMin && t1 <= tMax || t2 >= tMin && t2 <= tMax) + return !this.isTouching(); + var c2 = this.getCurve(), + c1 = c2.getPrevious(), + c4 = inter.getCurve(), + c3 = c4.getPrevious(), + PI = Math.PI; + if (!c1 || !c3) + return false; + + function isInRange(angle, min, max) { + return min < max + ? angle > min && angle < max + : angle > min && angle <= PI || angle >= -PI && angle < max; + } + + var a1 = c1.getTangentAt(tMax, true).negate().getAngleInRadians(), + a2 = c2.getTangentAt(tMin, true).getAngleInRadians(), + a3 = c3.getTangentAt(tMax, true).negate().getAngleInRadians(), + a4 = c4.getTangentAt(tMin, true).getAngleInRadians(); + + return (isInRange(a3, a1, a2) ^ isInRange(a4, a1, a2)) + && (isInRange(a3, a2, a1) ^ isInRange(a4, a2, a1)); + }, + + isOverlap: function() { + return !!this._overlap; + } +}, Base.each(Curve.evaluateMethods, function(name) { + var get = name + 'At'; + this[name] = function() { + var parameter = this.getParameter(), + curve = this.getCurve(); + return parameter != null && curve && curve[get](parameter, true); + }; +}, { + preserve: true +}), +new function() { + + function insert(locations, loc, merge) { + var length = locations.length, + l = 0, + r = length - 1; + + function search(index, dir) { + for (var i = index + dir; i >= -1 && i <= length; i += dir) { + var loc2 = locations[((i % length) + length) % length]; + if (!loc.getPoint().isClose(loc2.getPoint(), + 2e-7)) + break; + if (loc.equals(loc2)) + return loc2; + } + return null; + } + + while (l <= r) { + var m = (l + r) >>> 1, + loc2 = locations[m], + found; + if (merge && (found = loc.equals(loc2) ? loc2 + : (search(m, -1) || search(m, 1)))) { + if (loc._overlap) { + found._overlap = found._intersection._overlap = true; + } + return found; + } + var path1 = loc.getPath(), + path2 = loc2.getPath(), + diff = path1 === path2 + ? (loc.getIndex() + loc.getParameter()) + - (loc2.getIndex() + loc2.getParameter()) + : path1._id - path2._id; + if (diff < 0) { + r = m - 1; + } else { + l = m + 1; + } + } + locations.splice(l, 0, loc); + return loc; + } + + return { statics: { + insert: insert, + + expand: function(locations) { + var expanded = locations.slice(); + for (var i = 0, l = locations.length; i < l; i++) { + insert(expanded, locations[i]._intersection, false); + } + return expanded; + } + }}; +}); + +var PathItem = Item.extend({ + _class: 'PathItem', + + initialize: function PathItem() { + }, + + getIntersections: function(path, include, _matrix, _returnFirst) { + var self = this === path || !path, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = self ? matrix1 + : (_matrix || path._matrix).orNullIfIdentity(); + if (!self && !this.getBounds(matrix1).touches(path.getBounds(matrix2))) + return []; + var curves1 = this.getCurves(), + curves2 = self ? curves1 : path.getCurves(), + length1 = curves1.length, + length2 = self ? length1 : curves2.length, + values2 = [], + arrays = [], + locations, + path; + for (var i = 0; i < length2; i++) + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { + var curve1 = curves1[i], + values1 = self ? values2[i] : curve1.getValues(matrix1), + path1 = curve1.getPath(); + if (path1 !== path) { + path = path1; + locations = []; + arrays.push(locations); + } + if (self) { + Curve._getSelfIntersection(values1, curve1, locations, { + include: include, + startConnected: length1 === 1 && + curve1.getPoint1().equals(curve1.getPoint2()) + }); + } + for (var j = self ? i + 1 : 0; j < length2; j++) { + if (_returnFirst && locations.length) + return locations; + var curve2 = curves2[j]; + Curve._getIntersections( + values1, values2[j], curve1, curve2, locations, + { + include: include, + startConnected: self && curve1.getPrevious() === curve2, + endConnected: self && curve1.getNext() === curve2 + } + ); + } + } + locations = []; + for (var i = 0, l = arrays.length; i < l; i++) { + locations.push.apply(locations, arrays[i]); + } + return locations; + }, + + getCrossings: function(path) { + return this.getIntersections(path, function(inter) { + return inter.isCrossing(); + }); + }, + + _asPathItem: function() { + return this; + }, + + setPathData: function(data) { + + var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), + coords, + relative = false, + previous, + control, + current = new Point(), + start = new Point(); + + function getCoord(index, coord) { + var val = +coords[index]; + if (relative) + val += current[coord]; + return val; + } + + function getPoint(index) { + return new Point( + getCoord(index, 'x'), + getCoord(index + 1, 'y') + ); + } + + this.clear(); + + for (var i = 0, l = parts && parts.length; i < l; i++) { + var part = parts[i], + command = part[0], + lower = command.toLowerCase(); + coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); + var length = coords && coords.length; + relative = command === lower; + if (previous === 'z' && !/[mz]/.test(lower)) + this.moveTo(current = start); + switch (lower) { + case 'm': + case 'l': + var move = lower === 'm'; + for (var j = 0; j < length; j += 2) + this[j === 0 && move ? 'moveTo' : 'lineTo']( + current = getPoint(j)); + control = current; + if (move) + start = current; + break; + case 'h': + case 'v': + var coord = lower === 'h' ? 'x' : 'y'; + for (var j = 0; j < length; j++) { + current[coord] = getCoord(j, coord); + this.lineTo(current); + } + control = current; + break; + case 'c': + for (var j = 0; j < length; j += 6) { + this.cubicCurveTo( + getPoint(j), + control = getPoint(j + 2), + current = getPoint(j + 4)); + } + break; + case 's': + for (var j = 0; j < length; j += 4) { + this.cubicCurveTo( + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, + control = getPoint(j), + current = getPoint(j + 2)); + previous = lower; + } + break; + case 'q': + for (var j = 0; j < length; j += 4) { + this.quadraticCurveTo( + control = getPoint(j), + current = getPoint(j + 2)); + } + break; + case 't': + for (var j = 0; j < length; j += 2) { + this.quadraticCurveTo( + control = (/[qt]/.test(previous) + ? current.multiply(2).subtract(control) + : current), + current = getPoint(j)); + previous = lower; + } + break; + case 'a': + for (var j = 0; j < length; j += 7) { + this.arcTo(current = getPoint(j + 5), + new Size(+coords[j], +coords[j + 1]), + +coords[j + 2], +coords[j + 4], +coords[j + 3]); + } + break; + case 'z': + this.closePath(true); + break; + } + previous = lower; + } + }, + + _canComposite: function() { + return !(this.hasFill() && this.hasStroke()); + }, + + _contains: function(point) { + var winding = this._getWinding(point, false, true); + return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); + } + +}); + +var Path = PathItem.extend({ + _class: 'Path', + _serializeFields: { + segments: [], + closed: false + }, + + initialize: function Path(arg) { + this._closed = false; + this._segments = []; + this._version = 0; + var segments = Array.isArray(arg) + ? typeof arg[0] === 'object' + ? arg + : arguments + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) + ? arguments + : null; + if (segments && segments.length > 0) { + this.setSegments(segments); + } else { + this._curves = undefined; + this._selectedSegmentState = 0; + if (!segments && typeof arg === 'string') { + this.setPathData(arg); + arg = null; + } + } + this._initialize(!segments && arg); + }, + + _equals: function(item) { + return this._closed === item._closed + && Base.equals(this._segments, item._segments); + }, + + clone: function(insert) { + var copy = new Path(Item.NO_INSERT); + copy.setSegments(this._segments); + copy._closed = this._closed; + if (this._clockwise !== undefined) + copy._clockwise = this._clockwise; + return this._clone(copy, insert); + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & 8) { + var parent = this._parent; + if (parent) + parent._currentPath = undefined; + this._length = this._area = this._clockwise = this._monoCurves = + undefined; + if (flags & 16) { + this._version++; + } else if (this._curves) { + for (var i = 0, l = this._curves.length; i < l; i++) + this._curves[i]._changed(); + } + } else if (flags & 32) { + this._bounds = undefined; + } + }, + + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + + getSegments: function() { + return this._segments; + }, + + setSegments: function(segments) { + var fullySelected = this.isFullySelected(); + this._segments.length = 0; + this._selectedSegmentState = 0; + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); + if (fullySelected) + this.setFullySelected(true); + }, + + getFirstSegment: function() { + return this._segments[0]; + }, + + getLastSegment: function() { + return this._segments[this._segments.length - 1]; + }, + + getCurves: function() { + var curves = this._curves, + segments = this._segments; + if (!curves) { + var length = this._countCurves(); + curves = this._curves = new Array(length); + for (var i = 0; i < length; i++) + curves[i] = new Curve(this, segments[i], + segments[i + 1] || segments[0]); + } + return curves; + }, + + getFirstCurve: function() { + return this.getCurves()[0]; + }, + + getLastCurve: function() { + var curves = this.getCurves(); + return curves[curves.length - 1]; + }, + + isClosed: function() { + return this._closed; + }, + + setClosed: function(closed) { + if (this._closed != (closed = !!closed)) { + this._closed = closed; + if (this._curves) { + var length = this._curves.length = this._countCurves(); + if (closed) + this._curves[length - 1] = new Curve(this, + this._segments[length - 1], this._segments[0]); + } + this._changed(25); + } + } +}, { + beans: true, + + getPathData: function(_matrix, _precision) { + var segments = this._segments, + length = segments.length, + f = new Formatter(_precision), + coords = new Array(6), + first = true, + curX, curY, + prevX, prevY, + inX, inY, + outX, outY, + parts = []; + + function addSegment(segment, skipLine) { + segment._transformCoordinates(_matrix, coords, false); + curX = coords[0]; + curY = coords[1]; + if (first) { + parts.push('M' + f.pair(curX, curY)); + first = false; + } else { + inX = coords[2]; + inY = coords[3]; + if (inX === curX && inY === curY + && outX === prevX && outY === prevY) { + if (!skipLine) + parts.push('l' + f.pair(curX - prevX, curY - prevY)); + } else { + parts.push('c' + f.pair(outX - prevX, outY - prevY) + + ' ' + f.pair(inX - prevX, inY - prevY) + + ' ' + f.pair(curX - prevX, curY - prevY)); + } + } + prevX = curX; + prevY = curY; + outX = coords[4]; + outY = coords[5]; + } + + if (length === 0) + return ''; + + for (var i = 0; i < length; i++) + addSegment(segments[i]); + if (this._closed && length > 0) { + addSegment(segments[0], true); + parts.push('z'); + } + return parts.join(''); + } +}, { + + isEmpty: function() { + return this._segments.length === 0; + }, + + _transformContent: function(matrix) { + var coords = new Array(6); + for (var i = 0, l = this._segments.length; i < l; i++) + this._segments[i]._transformCoordinates(matrix, coords, true); + return true; + }, + + _add: function(segs, index) { + var segments = this._segments, + curves = this._curves, + amount = segs.length, + append = index == null, + index = append ? segments.length : index; + for (var i = 0; i < amount; i++) { + var segment = segs[i]; + if (segment._path) + segment = segs[i] = segment.clone(); + segment._path = this; + segment._index = index + i; + if (segment._selectionState) + this._updateSelection(segment, 0, segment._selectionState); + } + if (append) { + segments.push.apply(segments, segs); + } else { + segments.splice.apply(segments, [index, 0].concat(segs)); + for (var i = index + amount, l = segments.length; i < l; i++) + segments[i]._index = i; + } + if (curves) { + var total = this._countCurves(), + from = index + amount - 1 === total ? index - 1 : index, + start = from, + to = Math.min(from + amount, total); + if (segs._curves) { + curves.splice.apply(curves, [from, 0].concat(segs._curves)); + start += segs._curves.length; + } + for (var i = start; i < to; i++) + curves.splice(i, 0, new Curve(this, null, null)); + this._adjustCurves(from, to); + } + this._changed(25); + return segs; + }, + + _adjustCurves: function(from, to) { + var segments = this._segments, + curves = this._curves, + curve; + for (var i = from; i < to; i++) { + curve = curves[i]; + curve._path = this; + curve._segment1 = segments[i]; + curve._segment2 = segments[i + 1] || segments[0]; + curve._changed(); + } + if (curve = curves[this._closed && from === 0 ? segments.length - 1 + : from - 1]) { + curve._segment2 = segments[from] || segments[0]; + curve._changed(); + } + if (curve = curves[to]) { + curve._segment1 = segments[to]; + curve._changed(); + } + }, + + _countCurves: function() { + var length = this._segments.length; + return !this._closed && length > 0 ? length - 1 : length; + }, + + add: function(segment1 ) { + return arguments.length > 1 && typeof segment1 !== 'number' + ? this._add(Segment.readAll(arguments)) + : this._add([ Segment.read(arguments) ])[0]; + }, + + insert: function(index, segment1 ) { + return arguments.length > 2 && typeof segment1 !== 'number' + ? this._add(Segment.readAll(arguments, 1), index) + : this._add([ Segment.read(arguments, 1) ], index)[0]; + }, + + addSegment: function() { + return this._add([ Segment.read(arguments) ])[0]; + }, + + insertSegment: function(index ) { + return this._add([ Segment.read(arguments, 1) ], index)[0]; + }, + + addSegments: function(segments) { + return this._add(Segment.readAll(segments)); + }, + + insertSegments: function(index, segments) { + return this._add(Segment.readAll(segments), index); + }, + + removeSegment: function(index) { + return this.removeSegments(index, index + 1)[0] || null; + }, + + removeSegments: function(from, to, _includeCurves) { + from = from || 0; + to = Base.pick(to, this._segments.length); + var segments = this._segments, + curves = this._curves, + count = segments.length, + removed = segments.splice(from, to - from), + amount = removed.length; + if (!amount) + return removed; + for (var i = 0; i < amount; i++) { + var segment = removed[i]; + if (segment._selectionState) + this._updateSelection(segment, segment._selectionState, 0); + segment._index = segment._path = null; + } + for (var i = from, l = segments.length; i < l; i++) + segments[i]._index = i; + if (curves) { + var index = from > 0 && to === count + (this._closed ? 1 : 0) + ? from - 1 + : from, + curves = curves.splice(index, amount); + if (_includeCurves) + removed._curves = curves.slice(1); + this._adjustCurves(index, index); + } + this._changed(25); + return removed; + }, + + clear: '#removeSegments', + + hasHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) { + if (segments[i].hasHandles()) + return true; + } + return false; + }, + + clearHandles: function() { + var segments = this._segments; + for (var i = 0, l = segments.length; i < l; i++) + segments[i].clearHandles(); + }, + + getLength: function() { + if (this._length == null) { + var curves = this.getCurves(), + length = 0; + for (var i = 0, l = curves.length; i < l; i++) + length += curves[i].getLength(); + this._length = length; + } + return this._length; + }, + + getArea: function() { + if (this._area == null) { + var segments = this._segments, + count = segments.length, + last = count - 1, + area = 0; + for (var i = 0, l = this._closed ? count : last; i < l; i++) { + area += Curve.getArea(Curve.getValues( + segments[i], segments[i < last ? i + 1 : 0])); + } + this._area = area; + } + return this._area; + }, + + isClockwise: function() { + if (this._clockwise !== undefined) + return this._clockwise; + return this.getArea() >= 0; + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() != (clockwise = !!clockwise)) + this.reverse(); + this._clockwise = clockwise; + }, + + isFullySelected: function() { + var length = this._segments.length; + return this._selected && length > 0 && this._selectedSegmentState + === length * 7; + }, + + setFullySelected: function(selected) { + if (selected) + this._selectSegments(true); + this.setSelected(selected); + }, + + setSelected: function setSelected(selected) { + if (!selected) + this._selectSegments(false); + setSelected.base.call(this, selected); + }, + + _selectSegments: function(selected) { + var length = this._segments.length; + this._selectedSegmentState = selected + ? length * 7 : 0; + for (var i = 0; i < length; i++) + this._segments[i]._selectionState = selected + ? 7 : 0; + }, + + _updateSelection: function(segment, oldState, newState) { + segment._selectionState = newState; + var total = this._selectedSegmentState += newState - oldState; + if (total > 0) + this.setSelected(true); + }, + + flatten: function(maxDistance) { + var iterator = new PathIterator(this, 64, 0.1), + pos = 0, + step = iterator.length / Math.ceil(iterator.length / maxDistance), + end = iterator.length + (this._closed ? -step : step) / 2; + var segments = []; + while (pos <= end) { + segments.push(new Segment(iterator.getPointAt(pos))); + pos += step; + } + this.setSegments(segments); + }, + + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (!curve.hasHandles() && (curve.getLength() === 0 + || curve.isCollinear(curve.getNext()))) + curve.remove(); + } + return this; + }, + + simplify: function(tolerance) { + if (this._segments.length > 2) { + var fitter = new PathFitter(this, tolerance || 2.5); + this.setSegments(fitter.fit()); + } + }, + + split: function(index, parameter) { + if (parameter === null) + return null; + if (arguments.length === 1) { + var arg = index; + if (typeof arg === 'number') + arg = this.getLocationAt(arg); + if (!arg) + return null + index = arg.index; + parameter = arg.parameter; + } + var tMin = 4e-7, + tMax = 1 - tMin; + if (parameter >= tMax) { + index++; + parameter--; + } + var curves = this.getCurves(); + if (index >= 0 && index < curves.length) { + if (parameter >= tMin) { + curves[index++].divide(parameter, true); + } + var segs = this.removeSegments(index, this._segments.length, true), + path; + if (this._closed) { + this.setClosed(false); + path = this; + } else { + path = new Path(Item.NO_INSERT); + path.insertAbove(this, true); + this._clone(path); + } + path._add(segs, 0); + this.addSegment(segs[0]); + return path; + } + return null; + }, + + reverse: function() { + this._segments.reverse(); + for (var i = 0, l = this._segments.length; i < l; i++) { + var segment = this._segments[i]; + var handleIn = segment._handleIn; + segment._handleIn = segment._handleOut; + segment._handleOut = handleIn; + segment._index = i; + } + this._curves = null; + if (this._clockwise !== undefined) + this._clockwise = !this._clockwise; + this._changed(9); + }, + + join: function(path) { + if (path) { + var segments = path._segments, + last1 = this.getLastSegment(), + last2 = path.getLastSegment(); + if (!last2) + return this; + if (last1 && last1._point.equals(last2._point)) + path.reverse(); + var first2 = path.getFirstSegment(); + if (last1 && last1._point.equals(first2._point)) { + last1.setHandleOut(first2._handleOut); + this._add(segments.slice(1)); + } else { + var first1 = this.getFirstSegment(); + if (first1 && first1._point.equals(first2._point)) + path.reverse(); + last2 = path.getLastSegment(); + if (first1 && first1._point.equals(last2._point)) { + first1.setHandleIn(last2._handleIn); + this._add(segments.slice(0, segments.length - 1), 0); + } else { + this._add(segments.slice()); + } + } + if (path._closed) + this._add([segments[0]]); + path.remove(); + } + var first = this.getFirstSegment(), + last = this.getLastSegment(); + if (first !== last && first._point.equals(last._point)) { + first.setHandleIn(last._handleIn); + last.remove(); + this.setClosed(true); + } + return this; + }, + + toShape: function(insert) { + if (!this._closed) + return null; + + var segments = this._segments, + type, + size, + radius, + topCenter; + + function isCollinear(i, j) { + var seg1 = segments[i], + seg2 = seg1.getNext(), + seg3 = segments[j], + seg4 = seg3.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg3._handleOut.isZero() && seg4._handleIn.isZero() + && seg2._point.subtract(seg1._point).isCollinear( + seg4._point.subtract(seg3._point)); + } + + function isOrthogonal(i) { + var seg2 = segments[i], + seg1 = seg2.getPrevious(), + seg3 = seg2.getNext(); + return seg1._handleOut.isZero() && seg2._handleIn.isZero() + && seg2._handleOut.isZero() && seg3._handleIn.isZero() + && seg2._point.subtract(seg1._point).isOrthogonal( + seg3._point.subtract(seg2._point)); + } + + function isArc(i) { + var seg1 = segments[i], + seg2 = seg1.getNext(), + handle1 = seg1._handleOut, + handle2 = seg2._handleIn, + kappa = 0.5522847498307936; + if (handle1.isOrthogonal(handle2)) { + var pt1 = seg1._point, + pt2 = seg2._point, + corner = new Line(pt1, handle1, true).intersect( + new Line(pt2, handle2, true), true); + return corner && Numerical.isZero(handle1.getLength() / + corner.subtract(pt1).getLength() - kappa) + && Numerical.isZero(handle2.getLength() / + corner.subtract(pt2).getLength() - kappa); + } + return false; + } + + function getDistance(i, j) { + return segments[i]._point.getDistance(segments[j]._point); + } + + if (!this.hasHandles() && segments.length === 4 + && isCollinear(0, 2) && isCollinear(1, 3) && isOrthogonal(1)) { + type = Shape.Rectangle; + size = new Size(getDistance(0, 3), getDistance(0, 1)); + topCenter = segments[1]._point.add(segments[2]._point).divide(2); + } else if (segments.length === 8 && isArc(0) && isArc(2) && isArc(4) + && isArc(6) && isCollinear(1, 5) && isCollinear(3, 7)) { + type = Shape.Rectangle; + size = new Size(getDistance(1, 6), getDistance(0, 3)); + radius = size.subtract(new Size(getDistance(0, 7), + getDistance(1, 2))).divide(2); + topCenter = segments[3]._point.add(segments[4]._point).divide(2); + } else if (segments.length === 4 + && isArc(0) && isArc(1) && isArc(2) && isArc(3)) { + if (Numerical.isZero(getDistance(0, 2) - getDistance(1, 3))) { + type = Shape.Circle; + radius = getDistance(0, 2) / 2; + } else { + type = Shape.Ellipse; + radius = new Size(getDistance(2, 0) / 2, getDistance(3, 1) / 2); + } + topCenter = segments[1]._point; + } + + if (type) { + var center = this.getPosition(true), + shape = this._clone(new type({ + center: center, + size: size, + radius: radius, + insert: false + }), insert, false); + shape.rotate(topCenter.subtract(center).getAngle() + 90); + return shape; + } + return null; + }, + + _hitTestSelf: function(point, options) { + var that = this, + style = this.getStyle(), + segments = this._segments, + numSegments = segments.length, + closed = this._closed, + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hitStroke = options.stroke && style.hasStroke(), + hitFill = options.fill && style.hasFill(), + hitCurves = options.curves, + radius = hitStroke + ? style.getStrokeWidth() / 2 + : hitFill && options.tolerance > 0 || hitCurves + ? 0 : null; + if (radius !== null) { + if (radius > 0) { + join = style.getStrokeJoin(); + cap = style.getStrokeCap(); + miterLimit = radius * style.getMiterLimit(); + strokePadding = tolerancePadding.add(new Point(radius, radius)); + } else { + join = cap = 'round'; + } + } + + function isCloseEnough(pt, padding) { + return point.subtract(pt).divide(padding).length <= 1; + } + + function checkSegmentPoint(seg, pt, name) { + if (!options.selected || pt.isSelected()) { + var anchor = seg._point; + if (pt !== anchor) + pt = pt.add(anchor); + if (isCloseEnough(pt, strokePadding)) { + return new HitResult(name, that, { + segment: seg, + point: pt + }); + } + } + } + + function checkSegmentPoints(seg, ends) { + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') + || (!ends && options.handles) && ( + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); + } + + function addToArea(point) { + area.add(point); + } + + function checkSegmentStroke(segment) { + if (join !== 'round' || cap !== 'round') { + area = new Path({ internal: true, closed: true }); + if (closed || segment._index > 0 + && segment._index < numSegments - 1) { + if (join !== 'round' && (segment._handleIn.isZero() + || segment._handleOut.isZero())) + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); + } else if (cap !== 'round') { + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); + } + } + return isCloseEnough(segment._point, strokePadding); + } + + if (options.ends && !options.segments && !closed) { + if (res = checkSegmentPoints(segments[0], true) + || checkSegmentPoints(segments[numSegments - 1], true)) + return res; + } else if (options.segments || options.handles) { + for (var i = 0; i < numSegments; i++) + if (res = checkSegmentPoints(segments[i])) + return res; + } + if (radius !== null) { + loc = this.getNearestLocation(point); + if (loc) { + var parameter = loc.getParameter(); + if (parameter === 0 || parameter === 1 && numSegments > 1) { + if (!checkSegmentStroke(loc.getSegment())) + loc = null; + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { + loc = null; + } + } + if (!loc && join === 'miter' && numSegments > 1) { + for (var i = 0; i < numSegments; i++) { + var segment = segments[i]; + if (point.getDistance(segment._point) <= miterLimit + && checkSegmentStroke(segment)) { + loc = segment.getLocation(); + break; + } + } + } + } + return !loc && hitFill && this._contains(point) + || loc && !hitStroke && !hitCurves + ? new HitResult('fill', this) + : loc + ? new HitResult(hitStroke ? 'stroke' : 'curve', this, { + location: loc, + point: loc.getPoint() + }) + : null; + } + +}, Base.each(Curve.evaluateMethods, + function(name) { + this[name + 'At'] = function(offset, isParameter) { + var loc = this.getLocationAt(offset, isParameter); + return loc && loc[name](); + }; + }, +{ + beans: false, + + getLocationOf: function() { + var point = Point.read(arguments), + curves = this.getCurves(); + for (var i = 0, l = curves.length; i < l; i++) { + var loc = curves[i].getLocationOf(point); + if (loc) + return loc; + } + return null; + }, + + getOffsetOf: function() { + var loc = this.getLocationOf.apply(this, arguments); + return loc ? loc.getOffset() : null; + }, + + getLocationAt: function(offset, isParameter) { + var curves = this.getCurves(), + length = 0; + if (isParameter) { + var index = ~~offset, + curve = curves[index]; + return curve ? curve.getLocationAt(offset - index, true) : null; + } + for (var i = 0, l = curves.length; i < l; i++) { + var start = length, + curve = curves[i]; + length += curve.getLength(); + if (length > offset) { + return curve.getLocationAt(offset - start); + } + } + if (curves.length > 0 && offset <= this.getLength()) + return new CurveLocation(curves[curves.length - 1], 1); + return null; + }, + + getNearestLocation: function() { + var point = Point.read(arguments), + curves = this.getCurves(), + minDist = Infinity, + minLoc = null; + for (var i = 0, l = curves.length; i < l; i++) { + var loc = curves[i].getNearestLocation(point); + if (loc._distance < minDist) { + minDist = loc._distance; + minLoc = loc; + } + } + return minLoc; + }, + + getNearestPoint: function() { + return this.getNearestLocation.apply(this, arguments).getPoint(); + } +}), +new function() { + + function drawHandles(ctx, segments, matrix, size) { + var half = size / 2; + + function drawHandle(index) { + var hX = coords[index], + hY = coords[index + 1]; + if (pX != hX || pY != hY) { + ctx.beginPath(); + ctx.moveTo(pX, pY); + ctx.lineTo(hX, hY); + ctx.stroke(); + ctx.beginPath(); + ctx.arc(hX, hY, half, 0, Math.PI * 2, true); + ctx.fill(); + } + } + + var coords = new Array(6); + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + segment._transformCoordinates(matrix, coords, false); + var state = segment._selectionState, + pX = coords[0], + pY = coords[1]; + if (state & 1) + drawHandle(2); + if (state & 2) + drawHandle(4); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; + ctx.fillStyle = '#ffffff'; + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; + } + } + } + + function drawSegments(ctx, path, matrix) { + var segments = path._segments, + length = segments.length, + coords = new Array(6), + first = true, + curX, curY, + prevX, prevY, + inX, inY, + outX, outY; + + function drawSegment(segment) { + if (matrix) { + segment._transformCoordinates(matrix, coords, false); + curX = coords[0]; + curY = coords[1]; + } else { + var point = segment._point; + curX = point._x; + curY = point._y; + } + if (first) { + ctx.moveTo(curX, curY); + first = false; + } else { + if (matrix) { + inX = coords[2]; + inY = coords[3]; + } else { + var handle = segment._handleIn; + inX = curX + handle._x; + inY = curY + handle._y; + } + if (inX === curX && inY === curY + && outX === prevX && outY === prevY) { + ctx.lineTo(curX, curY); + } else { + ctx.bezierCurveTo(outX, outY, inX, inY, curX, curY); + } + } + prevX = curX; + prevY = curY; + if (matrix) { + outX = coords[4]; + outY = coords[5]; + } else { + var handle = segment._handleOut; + outX = prevX + handle._x; + outY = prevY + handle._y; + } + } + + for (var i = 0; i < length; i++) + drawSegment(segments[i]); + if (path._closed && length > 0) + drawSegment(segments[0]); + } + + return { + _draw: function(ctx, param, strokeMatrix) { + var dontStart = param.dontStart, + dontPaint = param.dontFinish || param.clip, + style = this.getStyle(), + hasFill = style.hasFill(), + hasStroke = style.hasStroke(), + dashArray = style.getDashArray(), + dashLength = !paper.support.nativeDash && hasStroke + && dashArray && dashArray.length; + + if (!dontStart) + ctx.beginPath(); + + if (!dontStart && this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || dontPaint) { + drawSegments(ctx, this, strokeMatrix); + if (this._closed) + ctx.closePath(); + if (!dontStart) + this._currentPath = ctx.currentPath; + } + + function getOffset(i) { + return dashArray[((i % dashLength) + dashLength) % dashLength]; + } + + if (!dontPaint && (hasFill || hasStroke)) { + this._setStyles(ctx); + if (hasFill) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (hasStroke) { + if (dashLength) { + if (!dontStart) + ctx.beginPath(); + var iterator = new PathIterator(this, 32, 0.25, + strokeMatrix), + length = iterator.length, + from = -style.getDashOffset(), to, + i = 0; + from = from % length; + while (from > 0) { + from -= getOffset(i--) + getOffset(i--); + } + while (from < length) { + to = from + getOffset(i++); + if (from > 0 || to > 0) + iterator.drawPart(ctx, + Math.max(from, 0), Math.max(to, 0)); + from = to + getOffset(i++); + } + } + ctx.stroke(); + } + } + }, + + _drawSelected: function(ctx, matrix) { + ctx.beginPath(); + drawSegments(ctx, this, matrix); + ctx.stroke(); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); + } + }; +}, +new function() { + function getFirstControlPoints(rhs) { + var n = rhs.length, + x = [], + tmp = [], + b = 2; + x[0] = rhs[0] / b; + for (var i = 1; i < n; i++) { + tmp[i] = 1 / b; + b = (i < n - 1 ? 4 : 2) - tmp[i]; + x[i] = (rhs[i] - x[i - 1]) / b; + } + for (var i = 1; i < n; i++) { + x[n - i - 1] -= tmp[n - i] * x[n - i]; + } + return x; + } + + return { + smooth: function() { + var segments = this._segments, + size = segments.length, + closed = this._closed, + n = size, + overlap = 0; + if (size <= 2) + return; + if (closed) { + overlap = Math.min(size, 4); + n += Math.min(size, overlap) * 2; + } + var knots = []; + for (var i = 0; i < size; i++) + knots[i + overlap] = segments[i]._point; + if (closed) { + for (var i = 0; i < overlap; i++) { + knots[i] = segments[i + size - overlap]._point; + knots[i + size + overlap] = segments[i]._point; + } + } else { + n--; + } + var rhs = []; + + for (var i = 1; i < n - 1; i++) + rhs[i] = 4 * knots[i]._x + 2 * knots[i + 1]._x; + rhs[0] = knots[0]._x + 2 * knots[1]._x; + rhs[n - 1] = 3 * knots[n - 1]._x; + var x = getFirstControlPoints(rhs); + + for (var i = 1; i < n - 1; i++) + rhs[i] = 4 * knots[i]._y + 2 * knots[i + 1]._y; + rhs[0] = knots[0]._y + 2 * knots[1]._y; + rhs[n - 1] = 3 * knots[n - 1]._y; + var y = getFirstControlPoints(rhs); + + if (closed) { + for (var i = 0, j = size; i < overlap; i++, j++) { + var f1 = i / overlap, + f2 = 1 - f1, + ie = i + overlap, + je = j + overlap; + x[j] = x[i] * f1 + x[j] * f2; + y[j] = y[i] * f1 + y[j] * f2; + x[je] = x[ie] * f2 + x[je] * f1; + y[je] = y[ie] * f2 + y[je] * f1; + } + n--; + } + var handleIn = null; + for (var i = overlap; i <= n - overlap; i++) { + var segment = segments[i - overlap]; + if (handleIn) + segment.setHandleIn(handleIn.subtract(segment._point)); + if (i < n) { + segment.setHandleOut( + new Point(x[i], y[i]).subtract(segment._point)); + handleIn = i < n - 1 + ? new Point( + 2 * knots[i + 1]._x - x[i + 1], + 2 * knots[i + 1]._y - y[i + 1]) + : new Point( + (knots[n]._x + x[n - 1]) / 2, + (knots[n]._y + y[n - 1]) / 2); + } + } + if (closed && handleIn) { + var segment = this._segments[0]; + segment.setHandleIn(handleIn.subtract(segment._point)); + } + } + }; +}, +new function() { + function getCurrentSegment(that) { + var segments = that._segments; + if (segments.length === 0) + throw new Error('Use a moveTo() command first'); + return segments[segments.length - 1]; + } + + return { + moveTo: function() { + var segments = this._segments; + if (segments.length === 1) + this.removeSegment(0); + if (!segments.length) + this._add([ new Segment(Point.read(arguments)) ]); + }, + + moveBy: function() { + throw new Error('moveBy() is unsupported on Path items.'); + }, + + lineTo: function() { + this._add([ new Segment(Point.read(arguments)) ]); + }, + + cubicCurveTo: function() { + var handle1 = Point.read(arguments), + handle2 = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this); + current.setHandleOut(handle1.subtract(current._point)); + this._add([ new Segment(to, handle2.subtract(to)) ]); + }, + + quadraticCurveTo: function() { + var handle = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.cubicCurveTo( + handle.add(current.subtract(handle).multiply(1 / 3)), + handle.add(to.subtract(handle).multiply(1 / 3)), + to + ); + }, + + curveTo: function() { + var through = Point.read(arguments), + to = Point.read(arguments), + t = Base.pick(Base.read(arguments), 0.5), + t1 = 1 - t, + current = getCurrentSegment(this)._point, + handle = through.subtract(current.multiply(t1 * t1)) + .subtract(to.multiply(t * t)).divide(2 * t * t1); + if (handle.isNaN()) + throw new Error( + 'Cannot put a curve through points with parameter = ' + t); + this.quadraticCurveTo(handle, to); + }, + + arcTo: function() { + var current = getCurrentSegment(this), + from = current._point, + to = Point.read(arguments), + through, + peek = Base.peek(arguments), + clockwise = Base.pick(peek, true), + center, extent, vector, matrix; + if (typeof clockwise === 'boolean') { + var middle = from.add(to).divide(2), + through = middle.add(middle.subtract(from).rotate( + clockwise ? -90 : 90)); + } else if (Base.remain(arguments) <= 2) { + through = to; + to = Point.read(arguments); + } else { + var radius = Size.read(arguments); + if (radius.isZero()) + return this.lineTo(to); + var rotation = Base.read(arguments), + clockwise = !!Base.read(arguments), + large = !!Base.read(arguments), + middle = from.add(to).divide(2), + pt = from.subtract(middle).rotate(-rotation), + x = pt.x, + y = pt.y, + abs = Math.abs, + rx = abs(radius.width), + ry = abs(radius.height), + rxSq = rx * rx, + rySq = ry * ry, + xSq = x * x, + ySq = y * y; + var factor = Math.sqrt(xSq / rxSq + ySq / rySq); + if (factor > 1) { + rx *= factor; + ry *= factor; + rxSq = rx * rx; + rySq = ry * ry; + } + factor = (rxSq * rySq - rxSq * ySq - rySq * xSq) / + (rxSq * ySq + rySq * xSq); + if (abs(factor) < 1e-12) + factor = 0; + if (factor < 0) + throw new Error( + 'Cannot create an arc with the given arguments'); + center = new Point(rx * y / ry, -ry * x / rx) + .multiply((large === clockwise ? -1 : 1) + * Math.sqrt(factor)) + .rotate(rotation).add(middle); + matrix = new Matrix().translate(center).rotate(rotation) + .scale(rx, ry); + vector = matrix._inverseTransform(from); + extent = vector.getDirectedAngle(matrix._inverseTransform(to)); + if (!clockwise && extent > 0) + extent -= 360; + else if (clockwise && extent < 0) + extent += 360; + } + if (through) { + var l1 = new Line(from.add(through).divide(2), + through.subtract(from).rotate(90), true), + l2 = new Line(through.add(to).divide(2), + to.subtract(through).rotate(90), true), + line = new Line(from, to), + throughSide = line.getSide(through); + center = l1.intersect(l2, true); + if (!center) { + if (!throughSide) + return this.lineTo(to); + throw new Error( + 'Cannot create an arc with the given arguments'); + } + vector = from.subtract(center); + extent = vector.getDirectedAngle(to.subtract(center)); + var centerSide = line.getSide(center); + if (centerSide === 0) { + extent = throughSide * Math.abs(extent); + } else if (throughSide === centerSide) { + extent += extent < 0 ? 360 : -360; + } + } + var ext = Math.abs(extent), + count = ext >= 360 ? 4 : Math.ceil(ext / 90), + inc = extent / count, + half = inc * Math.PI / 360, + z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), + segments = []; + for (var i = 0; i <= count; i++) { + var pt = to, + out = null; + if (i < count) { + out = vector.rotate(90).multiply(z); + if (matrix) { + pt = matrix._transformPoint(vector); + out = matrix._transformPoint(vector.add(out)) + .subtract(pt); + } else { + pt = center.add(vector); + } + } + if (i === 0) { + current.setHandleOut(out); + } else { + var _in = vector.rotate(-90).multiply(z); + if (matrix) { + _in = matrix._transformPoint(vector.add(_in)) + .subtract(pt); + } + segments.push(new Segment(pt, _in, out)); + } + vector = vector.rotate(inc); + } + this._add(segments); + }, + + lineBy: function() { + var to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.lineTo(current.add(to)); + }, + + curveBy: function() { + var through = Point.read(arguments), + to = Point.read(arguments), + parameter = Base.read(arguments), + current = getCurrentSegment(this)._point; + this.curveTo(current.add(through), current.add(to), parameter); + }, + + cubicCurveBy: function() { + var handle1 = Point.read(arguments), + handle2 = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.cubicCurveTo(current.add(handle1), current.add(handle2), + current.add(to)); + }, + + quadraticCurveBy: function() { + var handle = Point.read(arguments), + to = Point.read(arguments), + current = getCurrentSegment(this)._point; + this.quadraticCurveTo(current.add(handle), current.add(to)); + }, + + arcBy: function() { + var current = getCurrentSegment(this)._point, + point = current.add(Point.read(arguments)), + clockwise = Base.pick(Base.peek(arguments), true); + if (typeof clockwise === 'boolean') { + this.arcTo(point, clockwise); + } else { + this.arcTo(point, current.add(Point.read(arguments))); + } + }, + + closePath: function(join) { + this.setClosed(true); + if (join) + this.join(); + } + }; +}, { + + _getBounds: function(getter, matrix) { + return Path[getter](this._segments, this._closed, this.getStyle(), + matrix); + }, + +statics: { + getBounds: function(segments, closed, style, matrix, strokePadding) { + var first = segments[0]; + if (!first) + return new Rectangle(); + var coords = new Array(6), + prevCoords = first._transformCoordinates(matrix, new Array(6), false), + min = prevCoords.slice(0, 2), + max = min.slice(), + roots = new Array(2); + + function processSegment(segment) { + segment._transformCoordinates(matrix, coords, false); + for (var i = 0; i < 2; i++) { + Curve._addBounds( + prevCoords[i], + prevCoords[i + 4], + coords[i + 2], + coords[i], + i, strokePadding ? strokePadding[i] : 0, min, max, roots); + } + var tmp = prevCoords; + prevCoords = coords; + coords = tmp; + } + + for (var i = 1, l = segments.length; i < l; i++) + processSegment(segments[i]); + if (closed) + processSegment(first); + return new Rectangle(min[0], min[1], max[0] - min[0], max[1] - min[1]); + }, + + getStrokeBounds: function(segments, closed, style, matrix) { + if (!style.hasStroke()) + return Path.getBounds(segments, closed, style, matrix); + var length = segments.length - (closed ? 0 : 1), + radius = style.getStrokeWidth() / 2, + padding = Path._getPenPadding(radius, matrix), + bounds = Path.getBounds(segments, closed, style, matrix, padding), + join = style.getStrokeJoin(), + cap = style.getStrokeCap(), + miterLimit = radius * style.getMiterLimit(); + var joinBounds = new Rectangle(new Size(padding).multiply(2)); + + function add(point) { + bounds = bounds.include(matrix + ? matrix._transformPoint(point, point) : point); + } + + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + + function addJoin(segment, join) { + var handleIn = segment._handleIn, + handleOut = segment._handleOut; + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isCollinear(handleOut)) { + addRound(segment); + } else { + Path._addBevelJoin(segment, join, radius, miterLimit, add); + } + } + + function addCap(segment, cap) { + if (cap === 'round') { + addRound(segment); + } else { + Path._addSquareCap(segment, cap, radius, add); + } + } + + for (var i = 1; i < length; i++) + addJoin(segments[i], join); + if (closed) { + addJoin(segments[0], join); + } else if (length > 0) { + addCap(segments[0], cap); + addCap(segments[segments.length - 1], cap); + } + return bounds; + }, + + _getPenPadding: function(radius, matrix) { + if (!matrix) + return [radius, radius]; + var mx = matrix.shiftless(), + hor = mx.transform(new Point(radius, 0)), + ver = mx.transform(new Point(0, radius)), + phi = hor.getAngleInRadians(), + a = hor.getLength(), + b = ver.getLength(); + var sin = Math.sin(phi), + cos = Math.cos(phi), + tan = Math.tan(phi), + tx = -Math.atan(b * tan / a), + ty = Math.atan(b / (tan * a)); + return [Math.abs(a * Math.cos(tx) * cos - b * Math.sin(tx) * sin), + Math.abs(b * Math.sin(ty) * cos + a * Math.cos(ty) * sin)]; + }, + + _addBevelJoin: function(segment, join, radius, miterLimit, addPoint, area) { + var curve2 = segment.getCurve(), + curve1 = curve2.getPrevious(), + point = curve2.getPointAt(0, true), + normal1 = curve1.getNormalAt(1, true), + normal2 = curve2.getNormalAt(0, true), + step = normal1.getDirectedAngle(normal2) < 0 ? -radius : radius; + normal1.setLength(step); + normal2.setLength(step); + if (area) { + addPoint(point); + addPoint(point.add(normal1)); + } + if (join === 'miter') { + var corner = new Line( + point.add(normal1), + new Point(-normal1.y, normal1.x), true + ).intersect(new Line( + point.add(normal2), + new Point(-normal2.y, normal2.x), true + ), true); + if (corner && point.getDistance(corner) <= miterLimit) { + addPoint(corner); + if (!area) + return; + } + } + if (!area) + addPoint(point.add(normal1)); + addPoint(point.add(normal2)); + }, + + _addSquareCap: function(segment, cap, radius, addPoint, area) { + var point = segment._point, + loc = segment.getLocation(), + normal = loc.getNormal().multiply(radius); + if (area) { + addPoint(point.subtract(normal)); + addPoint(point.add(normal)); + } + if (cap === 'square') + point = point.add(normal.rotate(loc.getParameter() === 0 ? -90 : 90)); + addPoint(point.add(normal)); + addPoint(point.subtract(normal)); + }, + + getHandleBounds: function(segments, closed, style, matrix, strokePadding, + joinPadding) { + var coords = new Array(6), + x1 = Infinity, + x2 = -x1, + y1 = x1, + y2 = x2; + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + segment._transformCoordinates(matrix, coords, false); + for (var j = 0; j < 6; j += 2) { + var padding = j === 0 ? joinPadding : strokePadding, + paddingX = padding ? padding[0] : 0, + paddingY = padding ? padding[1] : 0, + x = coords[j], + y = coords[j + 1], + xn = x - paddingX, + xx = x + paddingX, + yn = y - paddingY, + yx = y + paddingY; + if (xn < x1) x1 = xn; + if (xx > x2) x2 = xx; + if (yn < y1) y1 = yn; + if (yx > y2) y2 = yx; + } + } + return new Rectangle(x1, y1, x2 - x1, y2 - y1); + }, + + getRoughBounds: function(segments, closed, style, matrix) { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { + if (style.getStrokeJoin() === 'miter') + joinRadius = strokeRadius * style.getMiterLimit(); + if (style.getStrokeCap() === 'square') + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); + } + return Path.getHandleBounds(segments, closed, style, matrix, + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); + } +}}); + +Path.inject({ statics: new function() { + + var kappa = 0.5522847498307936, + ellipseSegments = [ + new Segment([-1, 0], [0, kappa ], [0, -kappa]), + new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), + new Segment([1, 0], [0, -kappa], [0, kappa ]), + new Segment([0, 1], [kappa, 0 ], [-kappa, 0]) + ]; + + function createPath(segments, closed, args) { + var props = Base.getNamed(args), + path = new Path(props && props.insert === false && Item.NO_INSERT); + path._add(segments); + path._closed = closed; + return path.set(props); + } + + function createEllipse(center, radius, args) { + var segments = new Array(4); + for (var i = 0; i < 4; i++) { + var segment = ellipseSegments[i]; + segments[i] = new Segment( + segment._point.multiply(radius).add(center), + segment._handleIn.multiply(radius), + segment._handleOut.multiply(radius) + ); + } + return createPath(segments, true, args); + } + + return { + Line: function() { + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); + }, + + Circle: function() { + var center = Point.readNamed(arguments, 'center'), + radius = Base.readNamed(arguments, 'radius'); + return createEllipse(center, new Size(radius), arguments); + }, + + Rectangle: function() { + var rect = Rectangle.readNamed(arguments, 'rectangle'), + radius = Size.readNamed(arguments, 'radius', 0, + { readNull: true }), + bl = rect.getBottomLeft(true), + tl = rect.getTopLeft(true), + tr = rect.getTopRight(true), + br = rect.getBottomRight(true), + segments; + if (!radius || radius.isZero()) { + segments = [ + new Segment(bl), + new Segment(tl), + new Segment(tr), + new Segment(br) + ]; + } else { + radius = Size.min(radius, rect.getSize(true).divide(2)); + var rx = radius.width, + ry = radius.height, + hx = rx * kappa, + hy = ry * kappa; + segments = [ + new Segment(bl.add(rx, 0), null, [-hx, 0]), + new Segment(bl.subtract(0, ry), [0, hy]), + new Segment(tl.add(0, ry), null, [0, -hy]), + new Segment(tl.add(rx, 0), [-hx, 0], null), + new Segment(tr.subtract(rx, 0), null, [hx, 0]), + new Segment(tr.add(0, ry), [0, -hy], null), + new Segment(br.subtract(0, ry), null, [0, hy]), + new Segment(br.subtract(rx, 0), [hx, 0]) + ]; + } + return createPath(segments, true, arguments); + }, + + RoundRectangle: '#Rectangle', + + Ellipse: function() { + var ellipse = Shape._readEllipse(arguments); + return createEllipse(ellipse.center, ellipse.radius, arguments); + }, + + Oval: '#Ellipse', + + Arc: function() { + var from = Point.readNamed(arguments, 'from'), + through = Point.readNamed(arguments, 'through'), + to = Point.readNamed(arguments, 'to'), + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); + path.moveTo(from); + path.arcTo(through, to); + return path.set(props); + }, + + RegularPolygon: function() { + var center = Point.readNamed(arguments, 'center'), + sides = Base.readNamed(arguments, 'sides'), + radius = Base.readNamed(arguments, 'radius'), + step = 360 / sides, + three = !(sides % 3), + vector = new Point(0, three ? -radius : radius), + offset = three ? -1 : 0.5, + segments = new Array(sides); + for (var i = 0; i < sides; i++) + segments[i] = new Segment(center.add( + vector.rotate((i + offset) * step))); + return createPath(segments, true, arguments); + }, + + Star: function() { + var center = Point.readNamed(arguments, 'center'), + points = Base.readNamed(arguments, 'points') * 2, + radius1 = Base.readNamed(arguments, 'radius1'), + radius2 = Base.readNamed(arguments, 'radius2'), + step = 360 / points, + vector = new Point(0, -1), + segments = new Array(points); + for (var i = 0; i < points; i++) + segments[i] = new Segment(center.add(vector.rotate(step * i) + .multiply(i % 2 ? radius2 : radius1))); + return createPath(segments, true, arguments); + } + }; +}}); + +var CompoundPath = PathItem.extend({ + _class: 'CompoundPath', + _serializeFields: { + children: [] + }, + + initialize: function CompoundPath(arg) { + this._children = []; + this._namedChildren = {}; + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + insertChildren: function insertChildren(index, items, _preserve) { + for (var i = items.length - 1; i >= 0; i--) { + var item = items[i]; + if (item instanceof CompoundPath) { + items.splice.apply(items, [i, 1].concat(item.removeChildren())); + item.remove(); + } + } + items = insertChildren.base.call(this, index, items, _preserve, Path); + for (var i = 0, l = !_preserve && items && items.length; i < l; i++) { + var item = items[i]; + if (item._clockwise === undefined) + item.setClockwise(item._index === 0); + } + return items; + }, + + reverse: function() { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) + children[i].reverse(); + }, + + smooth: function() { + for (var i = 0, l = this._children.length; i < l; i++) + this._children[i].smooth(); + }, + + reduce: function reduce() { + var children = this._children; + for (var i = children.length - 1; i >= 0; i--) { + var path = children[i].reduce(); + if (path.isEmpty()) + children.splice(i, 1); + } + if (children.length === 0) { + var path = new Path(Item.NO_INSERT); + path.insertAbove(this); + path.setStyle(this._style); + this.remove(); + return path; + } + return reduce.base.call(this); + }, + + isClockwise: function() { + var child = this.getFirstChild(); + return child && child.isClockwise(); + }, + + setClockwise: function(clockwise) { + if (this.isClockwise() !== !!clockwise) + this.reverse(); + }, + + getFirstSegment: function() { + var first = this.getFirstChild(); + return first && first.getFirstSegment(); + }, + + getLastSegment: function() { + var last = this.getLastChild(); + return last && last.getLastSegment(); + }, + + getCurves: function() { + var children = this._children, + curves = []; + for (var i = 0, l = children.length; i < l; i++) + curves.push.apply(curves, children[i].getCurves()); + return curves; + }, + + getFirstCurve: function() { + var first = this.getFirstChild(); + return first && first.getFirstCurve(); + }, + + getLastCurve: function() { + var last = this.getLastChild(); + return last && last.getFirstCurve(); + }, + + getArea: function() { + var children = this._children, + area = 0; + for (var i = 0, l = children.length; i < l; i++) + area += children[i].getArea(); + return area; + } +}, { + beans: true, + + getPathData: function(_matrix, _precision) { + var children = this._children, + paths = []; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i], + mx = child._matrix; + paths.push(child.getPathData(_matrix && !mx.isIdentity() + ? _matrix.chain(mx) : _matrix, _precision)); + } + return paths.join(' '); + } +}, { + _getChildHitTestOptions: function(options) { + return options.class === Path || options.type === 'path' + ? options + : new Base(options, { fill: false }); + }, + + _draw: function(ctx, param, strokeMatrix) { + var children = this._children; + if (children.length === 0) + return; + + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + param = param.extend({ dontStart: true, dontFinish: true }); + ctx.beginPath(); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param, strokeMatrix); + this._currentPath = ctx.currentPath; + } + + if (!param.clip) { + this._setStyles(ctx); + var style = this._style; + if (style.hasFill()) { + ctx.fill(style.getWindingRule()); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (style.hasStroke()) + ctx.stroke(); + } + }, + + _drawSelected: function(ctx, matrix, selectedItems) { + var children = this._children; + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i], + mx = child._matrix; + if (!selectedItems[child._id]) + child._drawSelected(ctx, mx.isIdentity() ? matrix + : matrix.chain(mx)); + } + } +}, +new function() { + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) + throw new Error('Use a moveTo() command first'); + return children[children.length - 1]; + } + + var fields = { + moveTo: function() { + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current + : new Path(Item.NO_INSERT); + if (path !== current) + this.addChild(path); + path.moveTo.apply(path, arguments); + }, + + moveBy: function() { + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); + }, + + closePath: function(join) { + getCurrentPath(this, true).closePath(join); + } + }; + + Base.each(['lineTo', 'cubicCurveTo', 'quadraticCurveTo', 'curveTo', 'arcTo', + 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], + function(key) { + fields[key] = function() { + var path = getCurrentPath(this, true); + path[key].apply(path, arguments); + }; + } + ); + + return fields; +}); + +PathItem.inject(new function() { + var operators = { + unite: function(w) { + return w === 1 || w === 0; + }, + + intersect: function(w) { + return w === 2; + }, + + subtract: function(w) { + return w === 1; + }, + + exclude: function(w) { + return w === 1; + } + }; + + function preparePath(path, resolve) { + var res = path.clone(false).reduce().transform(null, true, true); + return resolve ? res.resolveCrossings().reorient() : res; + } + + function finishBoolean(ctor, paths, path1, path2, reduce) { + var result = new ctor(Item.NO_INSERT); + result.addChildren(paths, true); + if (reduce) + result = result.reduce(); + result.insertAbove(path2 && path1.isSibling(path2) + && path1.getIndex() < path2.getIndex() + ? path2 : path1); + result.setStyle(path1._style); + return result; + } + + function computeBoolean(path1, path2, operation) { + if (!path1._children && !path1._closed) + return computeOpenBoolean(path1, path2, operation); + var _path1 = preparePath(path1, true), + _path2 = path2 && path1 !== path2 && preparePath(path2, true); + if (_path2 && /^(subtract|exclude)$/.test(operation) + ^ (_path2.isClockwise() !== _path1.isClockwise())) + _path2.reverse(); + var intersections = CurveLocation.expand( + _path1.getIntersections(_path2, function(inter) { + return _path2 && inter.isOverlap() || inter.isCrossing(); + }) + ); + divideLocations(intersections); + + var segments = [], + monoCurves = []; + + function collect(paths) { + for (var i = 0, l = paths.length; i < l; i++) { + var path = paths[i]; + segments.push.apply(segments, path._segments); + monoCurves.push.apply(monoCurves, path._getMonoCurves()); + } + } + + collect(_path1._children || [_path1]); + if (_path2) + collect(_path2._children || [_path2]); + for (var i = 0, l = intersections.length; i < l; i++) { + propagateWinding(intersections[i]._segment, _path1, _path2, + monoCurves, operation); + } + for (var i = 0, l = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding == null) { + propagateWinding(segment, _path1, _path2, monoCurves, + operation); + } + } + return finishBoolean(CompoundPath, tracePaths(segments, operation), + path1, path2, true); + } + + function computeOpenBoolean(path1, path2, operation) { + if (!path2 || !path2._children && !path2._closed + || !/^(subtract|intersect)$/.test(operation)) + return null; + var _path1 = preparePath(path1, false), + _path2 = preparePath(path2, false), + intersections = _path1.getIntersections(_path2, function(inter) { + return inter.isOverlap() || inter.isCrossing(); + }), + sub = operation === 'subtract', + paths = []; + + function addPath(path) { + if (_path2.contains(path.getPointAt(path.getLength() / 2)) ^ sub) { + paths.unshift(path); + return true; + } + } + + for (var i = intersections.length - 1; i >= 0; i--) { + var path = intersections[i].split(); + if (path) { + if (addPath(path)) + path.getFirstSegment().setHandleIn(0, 0); + _path1.getLastSegment().setHandleOut(0, 0); + } + } + addPath(_path1); + return finishBoolean(Group, paths, path1, path2); + } + + function linkIntersections(from, to) { + var prev = from; + while (prev) { + if (prev === to) + return; + prev = prev._prev; + } + while (from._next && from._next !== to) + from = from._next; + if (!from._next) { + while (to._prev) + to = to._prev; + from._next = to; + to._prev = from; + } + } + + function divideLocations(locations) { + var tMin = 4e-7, + tMax = 1 - tMin, + noHandles = false, + clearSegments = [], + prevCurve, + prevT; + + for (var i = locations.length - 1; i >= 0; i--) { + var loc = locations[i], + curve = loc._curve, + t = loc._parameter, + origT = t; + if (curve !== prevCurve) { + noHandles = !curve.hasHandles(); + } else if (prevT > 0) { + t /= prevT; + } + var segment; + if (t < tMin) { + segment = curve._segment1; + } else if (t > tMax) { + segment = curve._segment2; + } else { + segment = curve.divide(t, true, true)._segment1; + if (noHandles) + clearSegments.push(segment); + } + loc._setSegment(segment); + var inter = segment._intersection, + dest = loc._intersection; + if (inter) { + linkIntersections(inter, dest); + var other = inter; + while (other) { + linkIntersections(other._intersection, inter); + other = other._next; + } + } else { + segment._intersection = dest; + } + prevCurve = curve; + prevT = origT; + } + for (var i = 0, l = clearSegments.length; i < l; i++) { + clearSegments[i].clearHandles(); + } + } + + function getWinding(point, curves, horizontal, testContains) { + var epsilon = 2e-7, + tMin = 4e-7, + tMax = 1 - tMin, + px = point.x, + py = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = py - epsilon, + yAfter = py + epsilon; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, px, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y = Curve.getPoint(values, roots[j]).y; + if (y < yBefore && y > yTop) { + yTop = y; + } else if (y > yAfter && y < yBottom) { + yBottom = y; + } + } + } + } + yTop = (yTop + py) / 2; + yBottom = (yBottom + py) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(px, yTop), curves, false, + testContains); + if (yBottom < Infinity) + windRight = getWinding(new Point(px, yBottom), curves, false, + testContains); + } else { + var xBefore = px - epsilon, + xAfter = px + epsilon; + var startCounted = false, + prevCurve, + prevT; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding; + if (winding && (winding === 1 + && py >= values[1] && py <= values[7] + || py >= values[7] && py <= values[1]) + && Curve.solveCubic(values, 1, py, roots, 0, 1) === 1) { + var t = roots[0]; + if (!( + t > tMax && startCounted && curve.next !== curves[i + 1] + || t < tMin && prevT > tMax + && curve.previous === prevCurve)) { + var x = Curve.getPoint(values, t).x, + slope = Curve.getTangent(values, t).y, + counted = false; + if (Numerical.isZero(slope) && !Curve.isStraight(values) + || t < tMin && slope * Curve.getTangent( + curve.previous.values, 1).y < 0 + || t > tMax && slope * Curve.getTangent( + curve.next.values, 0).y < 0) { + if (testContains && x >= xBefore && x <= xAfter) { + ++windLeft; + ++windRight; + counted = true; + } + } else if (x <= xBefore) { + windLeft += winding; + counted = true; + } else if (x >= xAfter) { + windRight += winding; + counted = true; + } + if (curve.previous !== curves[i - 1]) + startCounted = t < tMin && counted; + } + prevCurve = curve; + prevT = t; + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function propagateWinding(segment, path1, path2, monoCurves, operation) { + var epsilon = 2e-7, + chain = [], + start = segment, + totalLength = 0, + windingSum = 0; + do { + var curve = segment.getCurve(), + length = curve.getLength(); + chain.push({ segment: segment, curve: curve, length: length }); + totalLength += length; + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== start); + for (var i = 0; i < 3; i++) { + var length = totalLength * (i + 1) / 4; + for (var k = 0, m = chain.length; k < m; k++) { + var node = chain[k], + curveLength = node.length; + if (length <= curveLength) { + if (length < epsilon || curveLength - length < epsilon) + length = curveLength / 2; + var curve = node.curve, + path = curve._path, + parent = path._parent, + pt = curve.getPointAt(length), + hor = curve.isHorizontal(); + if (parent instanceof CompoundPath) + path = parent; + windingSum += operation === 'subtract' && path2 + && (path === path1 && path2._getWinding(pt, hor) + || path === path2 && !path1._getWinding(pt, hor)) + ? 0 + : getWinding(pt, monoCurves, hor); + break; + } + length -= curveLength; + } + } + var winding = Math.round(windingSum / 3); + for (var j = chain.length - 1; j >= 0; j--) + chain[j].segment._winding = winding; + } + + function tracePaths(segments, operation) { + var paths = [], + start, + otherStart, + operator = operators[operation], + overlapWinding = { + unite: { 1: 2 }, + intersect: { 2: 1 } + }[operation]; + + function isValid(seg, adjusted) { + if (seg._visited) + return false; + if (!operator) + return true; + var winding = seg._winding, + inter = seg._intersection; + if (inter && adjusted && overlapWinding && inter.isOverlap()) + winding = overlapWinding[winding] || winding; + return operator(winding); + } + + function isStart(seg) { + return seg === start || seg === otherStart; + } + + function findBestIntersection(inter, strict) { + if (!inter._next) + return inter; + while (inter) { + var seg = inter._segment, + nextSeg = seg.getNext(), + nextInter = nextSeg._intersection; + if (isStart(nextSeg) + || !seg._visited && !nextSeg._visited + && (!operator + || (!strict || isValid(seg)) + && (!(strict && nextInter && nextInter.isOverlap()) + && isValid(nextSeg) + || !strict && nextInter + && isValid(nextInter._segment)) + )) + return inter; + inter = inter._next; + } + return null; + } + + function findStartSegment(inter, next) { + while (inter) { + var seg = inter._segment; + if (isStart(seg)) + return seg; + inter = inter[next ? '_next' : '_prev']; + } + } + + for (var i = 0, l = segments.length; i < l; i++) { + var seg = segments[i], + path = null, + finished = false; + if (!isValid(seg, true)) + continue; + start = otherStart = null; + while (!finished) { + var inter = seg._intersection, + handleIn = path && seg._handleIn; + inter = inter && (findBestIntersection(inter, true) + || findBestIntersection(inter, false)) || inter; + var other = inter && inter._segment; + if (other && isValid(other)) + seg = other; + if (seg._visited) { + finished = isStart(seg); + if (!finished && inter) { + var found = findStartSegment(inter, true) + || findStartSegment(inter, false); + if (found) { + seg = found; + finished = true; + } + } + break; + } + if (!path) { + path = new Path(Item.NO_INSERT); + start = seg; + otherStart = other; + } + path.add(new Segment(seg._point, handleIn, seg._handleOut)); + seg._visited = true; + seg = seg.getNext(); + finished = isStart(seg); + } + if (finished) { + path.firstSegment.setHandleIn(seg._handleIn); + path.setClosed(true); + } else if (path) { + console.error('Boolean operation resulted in open path', + 'segments =', path._segments.length, + 'length =', path.getLength()); + path = null; + } + if (path && (path._segments.length > 8 + || !Numerical.isZero(path.getArea()))) { + paths.push(path); + path = null; + } + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, 'unite'); + }, + + intersect: function(path) { + return computeBoolean(this, path, 'intersect'); + }, + + subtract: function(path) { + return computeBoolean(this, path, 'subtract'); + }, + + exclude: function(path) { + return computeBoolean(this, path, 'exclude'); + }, + + divide: function(path) { + return finishBoolean(Group, + [this.subtract(path), this.intersect(path)], + this, path, true); + }, + + resolveCrossings: function() { + var crossings = this.getCrossings(); + if (!crossings.length) + return this; + divideLocations(CurveLocation.expand(crossings)); + var paths = this._children || [this], + segments = []; + for (var i = 0, l = paths.length; i < l; i++) { + segments.push.apply(segments, paths[i]._segments); + } + return finishBoolean(CompoundPath, tracePaths(segments), + this, null, false); + } + }; +}); + +Path.inject({ + _getMonoCurves: function() { + var monoCurves = this._monoCurves, + prevCurve; + + function insertCurve(v) { + var y0 = v[1], + y1 = v[7], + curve = { + values: v, + winding: y0 === y1 + ? 0 + : y0 > y1 + ? -1 + : 1, + previous: prevCurve, + next: null + }; + if (prevCurve) + prevCurve.next = curve; + monoCurves.push(curve); + prevCurve = curve; + } + + function handleCurve(v) { + if (Curve.getLength(v) === 0) + return; + var y0 = v[1], + y1 = v[3], + y2 = v[5], + y3 = v[7]; + if (Curve.isStraight(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + tMin = 4e-7, + tMax = 1 - tMin, + roots = [], + n = Numerical.solveQuadratic(a, b, c, roots, tMin, tMax); + if (n === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (n > 1) { + t = (roots[1] - t) / (1 - t); + parts = Curve.subdivide(parts[1], t); + insertCurve(parts[0]); + } + insertCurve(parts[1]); + } + } + } + + if (!monoCurves) { + monoCurves = this._monoCurves = []; + var curves = this.getCurves(), + segments = this._segments; + for (var i = 0, l = curves.length; i < l; i++) + handleCurve(curves[i].getValues()); + if (!this._closed && segments.length > 1) { + var p1 = segments[segments.length - 1]._point, + p2 = segments[0]._point, + p1x = p1._x, p1y = p1._y, + p2x = p2._x, p2y = p2._y; + handleCurve([p1x, p1y, p1x, p1y, p2x, p2y, p2x, p2y]); + } + if (monoCurves.length > 0) { + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + } + return monoCurves; + }, + + getInteriorPoint: function() { + var bounds = this.getBounds(), + point = bounds.getCenter(true); + if (!this.contains(point)) { + var curves = this._getMonoCurves(), + roots = [], + y = point.y, + xIntercepts = []; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if ((curves[i].winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) + xIntercepts.push(Curve.getPoint(values, roots[j]).x); + } + if (xIntercepts.length > 1) + break; + } + point.x = (xIntercepts[0] + xIntercepts[1]) / 2; + } + return point; + }, + + reorient: function() { + this.setClockwise(true); + return this; + } +}); + +CompoundPath.inject({ + _getMonoCurves: function() { + var children = this._children, + monoCurves = []; + for (var i = 0, l = children.length; i < l; i++) + monoCurves.push.apply(monoCurves, children[i]._getMonoCurves()); + return monoCurves; + }, + + reorient: function() { + var children = this.removeChildren().sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + if (children.length > 0) { + this.addChildren(children); + var clockwise = children[0].isClockwise(); + for (var i = 1, l = children.length; i < l; i++) { + var point = children[i].getInteriorPoint(), + counters = 0; + for (var j = i - 1; j >= 0; j--) { + if (children[j].contains(point)) + counters++; + } + children[i].setClockwise(counters % 2 === 0 && clockwise); + } + } + return this; + } +}); + +var PathIterator = Base.extend({ + _class: 'PathIterator', + + initialize: function(path, maxRecursion, tolerance, matrix) { + var curves = [], + parts = [], + length = 0, + minDifference = 1 / (maxRecursion || 32), + segments = path._segments, + segment1 = segments[0], + segment2; + + function addCurve(segment1, segment2) { + var curve = Curve.getValues(segment1, segment2, matrix); + curves.push(curve); + computeParts(curve, segment1._index, 0, 1); + } + + function computeParts(curve, index, minT, maxT) { + if ((maxT - minT) > minDifference + && !Curve.isFlatEnough(curve, tolerance || 0.25)) { + var split = Curve.subdivide(curve, 0.5), + halfT = (minT + maxT) / 2; + computeParts(split[0], index, minT, halfT); + computeParts(split[1], index, halfT, maxT); + } else { + var x = curve[6] - curve[0], + y = curve[7] - curve[1], + dist = Math.sqrt(x * x + y * y); + if (dist > 1e-6) { + length += dist; + parts.push({ + offset: length, + value: maxT, + index: index + }); + } + } + } + + for (var i = 1, l = segments.length; i < l; i++) { + segment2 = segments[i]; + addCurve(segment1, segment2); + segment1 = segment2; + } + if (path._closed) + addCurve(segment2, segments[0]); + + this.curves = curves; + this.parts = parts; + this.length = length; + this.index = 0; + }, + + getParameterAt: function(offset) { + var i, j = this.index; + for (;;) { + i = j; + if (j == 0 || this.parts[--j].offset < offset) + break; + } + for (var l = this.parts.length; i < l; i++) { + var part = this.parts[i]; + if (part.offset >= offset) { + this.index = i; + var prev = this.parts[i - 1]; + var prevVal = prev && prev.index == part.index ? prev.value : 0, + prevLen = prev ? prev.offset : 0; + return { + value: prevVal + (part.value - prevVal) + * (offset - prevLen) / (part.offset - prevLen), + index: part.index + }; + } + } + var part = this.parts[this.parts.length - 1]; + return { + value: 1, + index: part.index + }; + }, + + drawPart: function(ctx, from, to) { + from = this.getParameterAt(from); + to = this.getParameterAt(to); + for (var i = from.index; i <= to.index; i++) { + var curve = Curve.getPart(this.curves[i], + i == from.index ? from.value : 0, + i == to.index ? to.value : 1); + if (i == from.index) + ctx.moveTo(curve[0], curve[1]); + ctx.bezierCurveTo.apply(ctx, curve.slice(2)); + } + } +}, Base.each(Curve.evaluateMethods, + function(name) { + this[name + 'At'] = function(offset, weighted) { + var param = this.getParameterAt(offset); + return Curve[name](this.curves[param.index], param.value, weighted); + }; + }, {}) +); + +var PathFitter = Base.extend({ + initialize: function(path, error) { + var points = this.points = [], + segments = path._segments, + prev; + for (var i = 0, l = segments.length; i < l; i++) { + var point = segments[i].point.clone(); + if (!prev || !prev.equals(point)) { + points.push(point); + prev = point; + } + } + + if (path._closed) { + this.closed = true; + points.unshift(points[points.length - 1]); + points.push(points[1]); + } + + this.error = error; + }, + + fit: function() { + var points = this.points, + length = points.length, + segments = this.segments = length > 0 + ? [new Segment(points[0])] : []; + if (length > 1) + this.fitCubic(0, length - 1, + points[1].subtract(points[0]).normalize(), + points[length - 2].subtract(points[length - 1]).normalize()); + + if (this.closed) { + segments.shift(); + segments.pop(); + } + + return segments; + }, + + fitCubic: function(first, last, tan1, tan2) { + if (last - first == 1) { + var pt1 = this.points[first], + pt2 = this.points[last], + dist = pt1.getDistance(pt2) / 3; + this.addCurve([pt1, pt1.add(tan1.normalize(dist)), + pt2.add(tan2.normalize(dist)), pt2]); + return; + } + var uPrime = this.chordLengthParameterize(first, last), + maxError = Math.max(this.error, this.error * this.error), + split, + parametersInOrder = true; + for (var i = 0; i <= 4; i++) { + var curve = this.generateBezier(first, last, uPrime, tan1, tan2); + var max = this.findMaxError(first, last, curve, uPrime); + if (max.error < this.error && parametersInOrder) { + this.addCurve(curve); + return; + } + split = max.index; + if (max.error >= maxError) + break; + parametersInOrder = this.reparameterize(first, last, uPrime, curve); + maxError = max.error; + } + var V1 = this.points[split - 1].subtract(this.points[split]), + V2 = this.points[split].subtract(this.points[split + 1]), + tanCenter = V1.add(V2).divide(2).normalize(); + this.fitCubic(first, split, tan1, tanCenter); + this.fitCubic(split, last, tanCenter.negate(), tan2); + }, + + addCurve: function(curve) { + var prev = this.segments[this.segments.length - 1]; + prev.setHandleOut(curve[1].subtract(curve[0])); + this.segments.push( + new Segment(curve[3], curve[2].subtract(curve[3]))); + }, + + generateBezier: function(first, last, uPrime, tan1, tan2) { + var epsilon = 1e-12, + pt1 = this.points[first], + pt2 = this.points[last], + C = [[0, 0], [0, 0]], + X = [0, 0]; + + for (var i = 0, l = last - first + 1; i < l; i++) { + var u = uPrime[i], + t = 1 - u, + b = 3 * u * t, + b0 = t * t * t, + b1 = b * t, + b2 = b * u, + b3 = u * u * u, + a1 = tan1.normalize(b1), + a2 = tan2.normalize(b2), + tmp = this.points[first + i] + .subtract(pt1.multiply(b0 + b1)) + .subtract(pt2.multiply(b2 + b3)); + C[0][0] += a1.dot(a1); + C[0][1] += a1.dot(a2); + C[1][0] = C[0][1]; + C[1][1] += a2.dot(a2); + X[0] += a1.dot(tmp); + X[1] += a2.dot(tmp); + } + + var detC0C1 = C[0][0] * C[1][1] - C[1][0] * C[0][1], + alpha1, alpha2; + if (Math.abs(detC0C1) > epsilon) { + var detC0X = C[0][0] * X[1] - C[1][0] * X[0], + detXC1 = X[0] * C[1][1] - X[1] * C[0][1]; + alpha1 = detXC1 / detC0C1; + alpha2 = detC0X / detC0C1; + } else { + var c0 = C[0][0] + C[0][1], + c1 = C[1][0] + C[1][1]; + if (Math.abs(c0) > epsilon) { + alpha1 = alpha2 = X[0] / c0; + } else if (Math.abs(c1) > epsilon) { + alpha1 = alpha2 = X[1] / c1; + } else { + alpha1 = alpha2 = 0; + } + } + + var segLength = pt2.getDistance(pt1), + eps = epsilon * segLength, + handle1, + handle2; + if (alpha1 < eps || alpha2 < eps) { + alpha1 = alpha2 = segLength / 3; + } else { + var line = pt2.subtract(pt1); + handle1 = tan1.normalize(alpha1); + handle2 = tan2.normalize(alpha2); + if (handle1.dot(line) - handle2.dot(line) > segLength * segLength) { + alpha1 = alpha2 = segLength / 3; + handle1 = handle2 = null; + } + } + + return [pt1, pt1.add(handle1 || tan1.normalize(alpha1)), + pt2.add(handle2 || tan2.normalize(alpha2)), pt2]; + }, + + reparameterize: function(first, last, u, curve) { + for (var i = first; i <= last; i++) { + u[i - first] = this.findRoot(curve, this.points[i], u[i - first]); + } + for (var i = 1, l = u.length; i < l; i++) { + if (u[i] <= u[i - 1]) + return false; + } + return true; + }, + + findRoot: function(curve, point, u) { + var curve1 = [], + curve2 = []; + for (var i = 0; i <= 2; i++) { + curve1[i] = curve[i + 1].subtract(curve[i]).multiply(3); + } + for (var i = 0; i <= 1; i++) { + curve2[i] = curve1[i + 1].subtract(curve1[i]).multiply(2); + } + var pt = this.evaluate(3, curve, u), + pt1 = this.evaluate(2, curve1, u), + pt2 = this.evaluate(1, curve2, u), + diff = pt.subtract(point), + df = pt1.dot(pt1) + diff.dot(pt2); + if (Math.abs(df) < 1e-6) + return u; + return u - diff.dot(pt1) / df; + }, + + evaluate: function(degree, curve, t) { + var tmp = curve.slice(); + for (var i = 1; i <= degree; i++) { + for (var j = 0; j <= degree - i; j++) { + tmp[j] = tmp[j].multiply(1 - t).add(tmp[j + 1].multiply(t)); + } + } + return tmp[0]; + }, + + chordLengthParameterize: function(first, last) { + var u = [0]; + for (var i = first + 1; i <= last; i++) { + u[i - first] = u[i - first - 1] + + this.points[i].getDistance(this.points[i - 1]); + } + for (var i = 1, m = last - first; i <= m; i++) { + u[i] /= u[m]; + } + return u; + }, + + findMaxError: function(first, last, curve, u) { + var index = Math.floor((last - first + 1) / 2), + maxDist = 0; + for (var i = first + 1; i < last; i++) { + var P = this.evaluate(3, curve, u[i - first]); + var v = P.subtract(this.points[i]); + var dist = v.x * v.x + v.y * v.y; + if (dist >= maxDist) { + maxDist = dist; + index = i; + } + } + return { + error: maxDist, + index: index + }; + } +}); + +var TextItem = Item.extend({ + _class: 'TextItem', + _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, + _serializeFields: { + content: null + }, + _boundsGetter: 'getBounds', + + initialize: function TextItem(arg) { + this._content = ''; + this._lines = []; + var hasProps = arg && Base.isPlainObject(arg) + && arg.x === undefined && arg.y === undefined; + this._initialize(hasProps && arg, !hasProps && Point.read(arguments)); + }, + + _equals: function(item) { + return this._content === item._content; + }, + + _clone: function _clone(copy, insert, includeMatrix) { + copy.setContent(this._content); + return _clone.base.call(this, copy, insert, includeMatrix); + }, + + getContent: function() { + return this._content; + }, + + setContent: function(content) { + this._content = '' + content; + this._lines = this._content.split(/\r\n|\n|\r/mg); + this._changed(265); + }, + + isEmpty: function() { + return !this._content; + }, + + getCharacterStyle: '#getStyle', + setCharacterStyle: '#setStyle', + + getParagraphStyle: '#getStyle', + setParagraphStyle: '#setStyle' +}); + +var PointText = TextItem.extend({ + _class: 'PointText', + + initialize: function PointText() { + TextItem.apply(this, arguments); + }, + + clone: function(insert) { + return this._clone(new PointText(Item.NO_INSERT), insert); + }, + + getPoint: function() { + var point = this._matrix.getTranslation(); + return new LinkedPoint(point.x, point.y, this, 'setPoint'); + }, + + setPoint: function() { + var point = Point.read(arguments); + this.translate(point.subtract(this._matrix.getTranslation())); + }, + + _draw: function(ctx) { + if (!this._content) + return; + this._setStyles(ctx); + var style = this._style, + lines = this._lines, + leading = style.getLeading(), + shadowColor = ctx.shadowColor; + ctx.font = style.getFontStyle(); + ctx.textAlign = style.getJustification(); + for (var i = 0, l = lines.length; i < l; i++) { + ctx.shadowColor = shadowColor; + var line = lines[i]; + if (style.hasFill()) { + ctx.fillText(line, 0, 0); + ctx.shadowColor = 'rgba(0,0,0,0)'; + } + if (style.hasStroke()) + ctx.strokeText(line, 0, 0); + ctx.translate(0, leading); + } + }, + + _getBounds: function(getter, matrix) { + var style = this._style, + lines = this._lines, + numLines = lines.length, + justification = style.getJustification(), + leading = style.getLeading(), + width = this.getView().getTextWidth(style.getFontStyle(), lines), + x = 0; + if (justification !== 'left') + x -= width / (justification === 'center' ? 2: 1); + var bounds = new Rectangle(x, + numLines ? - 0.75 * leading : 0, + width, numLines * leading); + return matrix ? matrix._transformBounds(bounds, bounds) : bounds; + } +}); + +var Color = Base.extend(new function() { + var types = { + gray: ['gray'], + rgb: ['red', 'green', 'blue'], + hsb: ['hue', 'saturation', 'brightness'], + hsl: ['hue', 'saturation', 'lightness'], + gradient: ['gradient', 'origin', 'destination', 'highlight'] + }; + + var componentParsers = {}, + colorCache = {}, + colorCtx; + + function fromCSS(string) { + var match = string.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/), + components; + if (match) { + components = [0, 0, 0]; + for (var i = 0; i < 3; i++) { + var value = match[i + 1]; + components[i] = parseInt(value.length == 1 + ? value + value : value, 16) / 255; + } + } else if (match = string.match(/^rgba?\((.*)\)$/)) { + components = match[1].split(','); + for (var i = 0, l = components.length; i < l; i++) { + var value = +components[i]; + components[i] = i < 3 ? value / 255 : value; + } + } else { + var cached = colorCache[string]; + if (!cached) { + if (!colorCtx) { + colorCtx = CanvasProvider.getContext(1, 1); + colorCtx.globalCompositeOperation = 'copy'; + } + colorCtx.fillStyle = 'rgba(0,0,0,0)'; + colorCtx.fillStyle = string; + colorCtx.fillRect(0, 0, 1, 1); + var data = colorCtx.getImageData(0, 0, 1, 1).data; + cached = colorCache[string] = [ + data[0] / 255, + data[1] / 255, + data[2] / 255 + ]; + } + components = cached.slice(); + } + return components; + } + + var hsbIndices = [ + [0, 3, 1], + [2, 0, 1], + [1, 0, 3], + [1, 2, 0], + [3, 1, 0], + [0, 1, 2] + ]; + + var converters = { + 'rgb-hsb': function(r, g, b) { + var max = Math.max(r, g, b), + min = Math.min(r, g, b), + delta = max - min, + h = delta === 0 ? 0 + : ( max == r ? (g - b) / delta + (g < b ? 6 : 0) + : max == g ? (b - r) / delta + 2 + : (r - g) / delta + 4) * 60; + return [h, max === 0 ? 0 : delta / max, max]; + }, + + 'hsb-rgb': function(h, s, b) { + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), + f = h - i, + i = hsbIndices[i], + v = [ + b, + b * (1 - s), + b * (1 - s * f), + b * (1 - s * (1 - f)) + ]; + return [v[i[0]], v[i[1]], v[i[2]]]; + }, + + 'rgb-hsl': function(r, g, b) { + var max = Math.max(r, g, b), + min = Math.min(r, g, b), + delta = max - min, + achromatic = delta === 0, + h = achromatic ? 0 + : ( max == r ? (g - b) / delta + (g < b ? 6 : 0) + : max == g ? (b - r) / delta + 2 + : (r - g) / delta + 4) * 60, + l = (max + min) / 2, + s = achromatic ? 0 : l < 0.5 + ? delta / (max + min) + : delta / (2 - max - min); + return [h, s, l]; + }, + + 'hsl-rgb': function(h, s, l) { + h = (((h / 360) % 1) + 1) % 1; + if (s === 0) + return [l, l, l]; + var t3s = [ h + 1 / 3, h, h - 1 / 3 ], + t2 = l < 0.5 ? l * (1 + s) : l + s - l * s, + t1 = 2 * l - t2, + c = []; + for (var i = 0; i < 3; i++) { + var t3 = t3s[i]; + if (t3 < 0) t3 += 1; + if (t3 > 1) t3 -= 1; + c[i] = 6 * t3 < 1 + ? t1 + (t2 - t1) * 6 * t3 + : 2 * t3 < 1 + ? t2 + : 3 * t3 < 2 + ? t1 + (t2 - t1) * ((2 / 3) - t3) * 6 + : t1; + } + return c; + }, + + 'rgb-gray': function(r, g, b) { + return [r * 0.2989 + g * 0.587 + b * 0.114]; + }, + + 'gray-rgb': function(g) { + return [g, g, g]; + }, + + 'gray-hsb': function(g) { + return [0, 0, g]; + }, + + 'gray-hsl': function(g) { + return [0, 0, g]; + }, + + 'gradient-rgb': function() { + return []; + }, + + 'rgb-gradient': function() { + return []; + } + + }; + + return Base.each(types, function(properties, type) { + componentParsers[type] = []; + Base.each(properties, function(name, index) { + var part = Base.capitalize(name), + hasOverlap = /^(hue|saturation)$/.test(name), + parser = componentParsers[type][index] = name === 'gradient' + ? function(value) { + var current = this._components[0]; + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); + if (current !== value) { + if (current) + current._removeOwner(this); + if (value) + value._addOwner(this); + } + return value; + } + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); + } + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; + + this['get' + part] = function() { + return this._type === type + || hasOverlap && /^hs[bl]$/.test(this._type) + ? this._components[index] + : this._convert(type)[index]; + }; + + this['set' + part] = function(value) { + if (this._type !== type + && !(hasOverlap && /^hs[bl]$/.test(this._type))) { + this._components = this._convert(type); + this._properties = types[type]; + this._type = type; + } + this._components[index] = parser.call(this, value); + this._changed(); + }; + }, this); + }, { + _class: 'Color', + _readIndex: true, + + initialize: function Color(arg) { + var slice = Array.prototype.slice, + args = arguments, + read = 0, + type, + components, + alpha, + values; + if (Array.isArray(arg)) { + args = arg; + arg = args[0]; + } + var argType = arg != null && typeof arg; + if (argType === 'string' && arg in types) { + type = arg; + arg = args[1]; + if (Array.isArray(arg)) { + components = arg; + alpha = args[2]; + } else { + if (this.__read) + read = 1; + args = slice.call(args, 1); + argType = typeof arg; + } + } + if (!components) { + values = argType === 'number' + ? args + : argType === 'object' && arg.length != null + ? arg + : null; + if (values) { + if (!type) + type = values.length >= 3 + ? 'rgb' + : 'gray'; + var length = types[type].length; + alpha = values[length]; + if (this.__read) + read += values === arguments + ? length + (alpha != null ? 1 : 0) + : 1; + if (values.length > length) + values = slice.call(values, 0, length); + } else if (argType === 'string') { + type = 'rgb'; + components = fromCSS(arg); + if (components.length === 4) { + alpha = components[3]; + components.length--; + } + } else if (argType === 'object') { + if (arg.constructor === Color) { + type = arg._type; + components = arg._components.slice(); + alpha = arg._alpha; + if (type === 'gradient') { + for (var i = 1, l = components.length; i < l; i++) { + var point = components[i]; + if (point) + components[i] = point.clone(); + } + } + } else if (arg.constructor === Gradient) { + type = 'gradient'; + values = args; + } else { + type = 'hue' in arg + ? 'lightness' in arg + ? 'hsl' + : 'hsb' + : 'gradient' in arg || 'stops' in arg + || 'radial' in arg + ? 'gradient' + : 'gray' in arg + ? 'gray' + : 'rgb'; + var properties = types[type], + parsers = componentParsers[type]; + this._components = components = []; + for (var i = 0, l = properties.length; i < l; i++) { + var value = arg[properties[i]]; + if (value == null && i === 0 && type === 'gradient' + && 'stops' in arg) { + value = { + stops: arg.stops, + radial: arg.radial + }; + } + value = parsers[i].call(this, value); + if (value != null) + components[i] = value; + } + alpha = arg.alpha; + } + } + if (this.__read && type) + read = 1; + } + this._type = type || 'rgb'; + this._id = UID.get(Color); + if (!components) { + this._components = components = []; + var parsers = componentParsers[this._type]; + for (var i = 0, l = parsers.length; i < l; i++) { + var value = parsers[i].call(this, values && values[i]); + if (value != null) + components[i] = value; + } + } + this._components = components; + this._properties = types[this._type]; + this._alpha = alpha; + if (this.__read) + this.__read = read; + }, + + _serialize: function(options, dictionary) { + var components = this.getComponents(); + return Base.serialize( + /^(gray|rgb)$/.test(this._type) + ? components + : [this._type].concat(components), + options, true, dictionary); + }, + + _changed: function() { + this._canvasStyle = null; + if (this._owner) + this._owner._changed(65); + }, + + _convert: function(type) { + var converter; + return this._type === type + ? this._components.slice() + : (converter = converters[this._type + '-' + type]) + ? converter.apply(this, this._components) + : converters['rgb-' + type].apply(this, + converters[this._type + '-rgb'].apply(this, + this._components)); + }, + + convert: function(type) { + return new Color(type, this._convert(type), this._alpha); + }, + + getType: function() { + return this._type; + }, + + setType: function(type) { + this._components = this._convert(type); + this._properties = types[type]; + this._type = type; + }, + + getComponents: function() { + var components = this._components.slice(); + if (this._alpha != null) + components.push(this._alpha); + return components; + }, + + getAlpha: function() { + return this._alpha != null ? this._alpha : 1; + }, + + setAlpha: function(alpha) { + this._alpha = alpha == null ? null : Math.min(Math.max(alpha, 0), 1); + this._changed(); + }, + + hasAlpha: function() { + return this._alpha != null; + }, + + equals: function(color) { + var col = Base.isPlainValue(color, true) + ? Color.read(arguments) + : color; + return col === this || col && this._class === col._class + && this._type === col._type + && this._alpha === col._alpha + && Base.equals(this._components, col._components) + || false; + }, + + toString: function() { + var properties = this._properties, + parts = [], + isGradient = this._type === 'gradient', + f = Formatter.instance; + for (var i = 0, l = properties.length; i < l; i++) { + var value = this._components[i]; + if (value != null) + parts.push(properties[i] + ': ' + + (isGradient ? value : f.number(value))); + } + if (this._alpha != null) + parts.push('alpha: ' + f.number(this._alpha)); + return '{ ' + parts.join(', ') + ' }'; + }, + + toCSS: function(hex) { + var components = this._convert('rgb'), + alpha = hex || this._alpha == null ? 1 : this._alpha; + function convert(val) { + return Math.round((val < 0 ? 0 : val > 1 ? 1 : val) * 255); + } + components = [ + convert(components[0]), + convert(components[1]), + convert(components[2]) + ]; + if (alpha < 1) + components.push(alpha < 0 ? 0 : alpha); + return hex + ? '#' + ((1 << 24) + (components[0] << 16) + + (components[1] << 8) + + components[2]).toString(16).slice(1) + : (components.length == 4 ? 'rgba(' : 'rgb(') + + components.join(',') + ')'; + }, + + toCanvasStyle: function(ctx) { + if (this._canvasStyle) + return this._canvasStyle; + if (this._type !== 'gradient') + return this._canvasStyle = this.toCSS(); + var components = this._components, + gradient = components[0], + stops = gradient._stops, + origin = components[1], + destination = components[2], + canvasGradient; + if (gradient._radial) { + var radius = destination.getDistance(origin), + highlight = components[3]; + if (highlight) { + var vector = highlight.subtract(origin); + if (vector.getLength() > radius) + highlight = origin.add(vector.normalize(radius - 0.1)); + } + var start = highlight || origin; + canvasGradient = ctx.createRadialGradient(start.x, start.y, + 0, origin.x, origin.y, radius); + } else { + canvasGradient = ctx.createLinearGradient(origin.x, origin.y, + destination.x, destination.y); + } + for (var i = 0, l = stops.length; i < l; i++) { + var stop = stops[i]; + canvasGradient.addColorStop(stop._rampPoint, + stop._color.toCanvasStyle()); + } + return this._canvasStyle = canvasGradient; + }, + + transform: function(matrix) { + if (this._type === 'gradient') { + var components = this._components; + for (var i = 1, l = components.length; i < l; i++) { + var point = components[i]; + matrix._transformPoint(point, point, true); + } + this._changed(); + } + }, + + statics: { + _types: types, + + random: function() { + var random = Math.random; + return new Color(random(), random(), random()); + } + } + }); +}, +new function() { + var operators = { + add: function(a, b) { + return a + b; + }, + + subtract: function(a, b) { + return a - b; + }, + + multiply: function(a, b) { + return a * b; + }, + + divide: function(a, b) { + return a / b; + } + }; + + return Base.each(operators, function(operator, name) { + this[name] = function(color) { + color = Color.read(arguments); + var type = this._type, + components1 = this._components, + components2 = color._convert(type); + for (var i = 0, l = components1.length; i < l; i++) + components2[i] = operator(components1[i], components2[i]); + return new Color(type, components2, + this._alpha != null + ? operator(this._alpha, color.getAlpha()) + : null); + }; + }, { + }); +}); + +var Gradient = Base.extend({ + _class: 'Gradient', + + initialize: function Gradient(stops, radial) { + this._id = UID.get(); + if (stops && this._set(stops)) + stops = radial = null; + if (!this._stops) + this.setStops(stops || ['white', 'black']); + if (this._radial == null) + this.setRadial(typeof radial === 'string' && radial === 'radial' + || radial || false); + }, + + _serialize: function(options, dictionary) { + return dictionary.add(this, function() { + return Base.serialize([this._stops, this._radial], + options, true, dictionary); + }); + }, + + _changed: function() { + for (var i = 0, l = this._owners && this._owners.length; i < l; i++) + this._owners[i]._changed(); + }, + + _addOwner: function(color) { + if (!this._owners) + this._owners = []; + this._owners.push(color); + }, + + _removeOwner: function(color) { + var index = this._owners ? this._owners.indexOf(color) : -1; + if (index != -1) { + this._owners.splice(index, 1); + if (this._owners.length === 0) + this._owners = undefined; + } + }, + + clone: function() { + var stops = []; + for (var i = 0, l = this._stops.length; i < l; i++) + stops[i] = this._stops[i].clone(); + return new Gradient(stops, this._radial); + }, + + getStops: function() { + return this._stops; + }, + + setStops: function(stops) { + if (this.stops) { + for (var i = 0, l = this._stops.length; i < l; i++) + this._stops[i]._owner = undefined; + } + if (stops.length < 2) + throw new Error( + 'Gradient stop list needs to contain at least two stops.'); + this._stops = GradientStop.readAll(stops, 0, { clone: true }); + for (var i = 0, l = this._stops.length; i < l; i++) { + var stop = this._stops[i]; + stop._owner = this; + if (stop._defaultRamp) + stop.setRampPoint(i / (l - 1)); + } + this._changed(); + }, + + getRadial: function() { + return this._radial; + }, + + setRadial: function(radial) { + this._radial = radial; + this._changed(); + }, + + equals: function(gradient) { + if (gradient === this) + return true; + if (gradient && this._class === gradient._class + && this._stops.length === gradient._stops.length) { + for (var i = 0, l = this._stops.length; i < l; i++) { + if (!this._stops[i].equals(gradient._stops[i])) + return false; + } + return true; + } + return false; + } +}); + +var GradientStop = Base.extend({ + _class: 'GradientStop', + + initialize: function GradientStop(arg0, arg1) { + if (arg0) { + var color, rampPoint; + if (arg1 === undefined && Array.isArray(arg0)) { + color = arg0[0]; + rampPoint = arg0[1]; + } else if (arg0.color) { + color = arg0.color; + rampPoint = arg0.rampPoint; + } else { + color = arg0; + rampPoint = arg1; + } + this.setColor(color); + this.setRampPoint(rampPoint); + } + }, + + clone: function() { + return new GradientStop(this._color.clone(), this._rampPoint); + }, + + _serialize: function(options, dictionary) { + return Base.serialize([this._color, this._rampPoint], options, true, + dictionary); + }, + + _changed: function() { + if (this._owner) + this._owner._changed(65); + }, + + getRampPoint: function() { + return this._rampPoint; + }, + + setRampPoint: function(rampPoint) { + this._defaultRamp = rampPoint == null; + this._rampPoint = rampPoint || 0; + this._changed(); + }, + + getColor: function() { + return this._color; + }, + + setColor: function(color) { + this._color = Color.read(arguments); + if (this._color === color) + this._color = color.clone(); + this._color._owner = this; + this._changed(); + }, + + equals: function(stop) { + return stop === this || stop && this._class === stop._class + && this._color.equals(stop._color) + && this._rampPoint == stop._rampPoint + || false; + } +}); + +var Style = Base.extend(new function() { + var defaults = { + fillColor: undefined, + strokeColor: undefined, + strokeWidth: 1, + strokeCap: 'butt', + strokeJoin: 'miter', + strokeScaling: true, + miterLimit: 10, + dashOffset: 0, + dashArray: [], + windingRule: 'nonzero', + shadowColor: undefined, + shadowBlur: 0, + shadowOffset: new Point(), + selectedColor: undefined, + fontFamily: 'sans-serif', + fontWeight: 'normal', + fontSize: 12, + font: 'sans-serif', + leading: null, + justification: 'left' + }; + + var flags = { + strokeWidth: 97, + strokeCap: 97, + strokeJoin: 97, + strokeScaling: 105, + miterLimit: 97, + fontFamily: 9, + fontWeight: 9, + fontSize: 9, + font: 9, + leading: 9, + justification: 9 + }; + + var item = { beans: true }, + fields = { + _defaults: defaults, + _textDefaults: new Base(defaults, { + fillColor: new Color() + }), + beans: true + }; + + Base.each(defaults, function(value, key) { + var isColor = /Color$/.test(key), + isPoint = key === 'shadowOffset', + part = Base.capitalize(key), + flag = flags[key], + set = 'set' + part, + get = 'get' + part; + + fields[set] = function(value) { + var owner = this._owner, + children = owner && owner._children; + if (children && children.length > 0 + && !(owner instanceof CompoundPath)) { + for (var i = 0, l = children.length; i < l; i++) + children[i]._style[set](value); + } else { + var old = this._values[key]; + if (old !== value) { + if (isColor) { + if (old) + old._owner = undefined; + if (value && value.constructor === Color) { + if (value._owner) + value = value.clone(); + value._owner = owner; + } + } + this._values[key] = value; + if (owner) + owner._changed(flag || 65); + } + } + }; + + fields[get] = function(_dontMerge) { + var owner = this._owner, + children = owner && owner._children, + value; + if (!children || children.length === 0 || _dontMerge + || owner instanceof CompoundPath) { + var value = this._values[key]; + if (value === undefined) { + value = this._defaults[key]; + if (value && value.clone) + value = value.clone(); + } else { + var ctor = isColor ? Color : isPoint ? Point : null; + if (ctor && !(value && value.constructor === ctor)) { + this._values[key] = value = ctor.read([value], 0, + { readNull: true, clone: true }); + if (value && isColor) + value._owner = owner; + } + } + return value; + } + for (var i = 0, l = children.length; i < l; i++) { + var childValue = children[i]._style[get](); + if (i === 0) { + value = childValue; + } else if (!Base.equals(value, childValue)) { + return undefined; + } + } + return value; + }; + + item[get] = function(_dontMerge) { + return this._style[get](_dontMerge); + }; + + item[set] = function(value) { + this._style[set](value); + }; + }); + + Item.inject(item); + return fields; +}, { + _class: 'Style', + + initialize: function Style(style, _owner, _project) { + this._values = {}; + this._owner = _owner; + this._project = _owner && _owner._project || _project || paper.project; + if (_owner instanceof TextItem) + this._defaults = this._textDefaults; + if (style) + this.set(style); + }, + + set: function(style) { + var isStyle = style instanceof Style, + values = isStyle ? style._values : style; + if (values) { + for (var key in values) { + if (key in this._defaults) { + var value = values[key]; + this[key] = value && isStyle && value.clone + ? value.clone() : value; + } + } + } + }, + + equals: function(style) { + return style === this || style && this._class === style._class + && Base.equals(this._values, style._values) + || false; + }, + + hasFill: function() { + return !!this.getFillColor(); + }, + + hasStroke: function() { + return !!this.getStrokeColor() && this.getStrokeWidth() > 0; + }, + + hasShadow: function() { + return !!this.getShadowColor() && this.getShadowBlur() > 0; + }, + + getView: function() { + return this._project.getView(); + }, + + getFontStyle: function() { + var fontSize = this.getFontSize(); + return this.getFontWeight() + + ' ' + fontSize + (/[a-z]/i.test(fontSize + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + setFont: '#setFontFamily', + + getLeading: function getLeading() { + var leading = getLeading.base.call(this), + fontSize = this.getFontSize(); + if (/pt|em|%|px/.test(fontSize)) + fontSize = this.getView().getPixelSize(fontSize); + return leading != null ? leading : fontSize * 1.2; + } + +}); + +var jsdom = require('jsdom'), + domToHtml = require('jsdom/lib/jsdom/browser/domtohtml').domToHtml, + Canvas = require('canvas'), + document = jsdom.jsdom(''), + window = document.defaultView, + navigator = window.navigator, + HTMLCanvasElement = Canvas, + Image = Canvas.Image; + +function XMLSerializer() { +} + +XMLSerializer.prototype.serializeToString = function(node) { + var text = domToHtml(node); + var tagNames = ['linearGradient', 'radialGradient', 'clipPath']; + for (var i = 0, l = tagNames.length; i < l; i++) { + var tagName = tagNames[i]; + text = text.replace( + new RegExp('(<| 255) { + var ln = 255 - l, + mxl = mx - l; + dr = l + (dr - l) * ln / mxl; + dg = l + (dg - l) * ln / mxl; + db = l + (db - l) * ln / mxl; + } + } + + function getSat(r, g, b) { + return max(r, g, b) - min(r, g, b); + } + + function setSat(r, g, b, s) { + var col = [r, g, b], + mx = max(r, g, b), + mn = min(r, g, b), + md; + mn = mn === r ? 0 : mn === g ? 1 : 2; + mx = mx === r ? 0 : mx === g ? 1 : 2; + md = min(mn, mx) === 0 ? max(mn, mx) === 1 ? 2 : 1 : 0; + if (col[mx] > col[mn]) { + col[md] = (col[md] - col[mn]) * s / (col[mx] - col[mn]); + col[mx] = s; + } else { + col[md] = col[mx] = 0; + } + col[mn] = 0; + dr = col[0]; + dg = col[1]; + db = col[2]; + } + + var modes = { + multiply: function() { + dr = br * sr / 255; + dg = bg * sg / 255; + db = bb * sb / 255; + }, + + screen: function() { + dr = br + sr - (br * sr / 255); + dg = bg + sg - (bg * sg / 255); + db = bb + sb - (bb * sb / 255); + }, + + overlay: function() { + dr = br < 128 ? 2 * br * sr / 255 : 255 - 2 * (255 - br) * (255 - sr) / 255; + dg = bg < 128 ? 2 * bg * sg / 255 : 255 - 2 * (255 - bg) * (255 - sg) / 255; + db = bb < 128 ? 2 * bb * sb / 255 : 255 - 2 * (255 - bb) * (255 - sb) / 255; + }, + + 'soft-light': function() { + var t = sr * br / 255; + dr = t + br * (255 - (255 - br) * (255 - sr) / 255 - t) / 255; + t = sg * bg / 255; + dg = t + bg * (255 - (255 - bg) * (255 - sg) / 255 - t) / 255; + t = sb * bb / 255; + db = t + bb * (255 - (255 - bb) * (255 - sb) / 255 - t) / 255; + }, + + 'hard-light': function() { + dr = sr < 128 ? 2 * sr * br / 255 : 255 - 2 * (255 - sr) * (255 - br) / 255; + dg = sg < 128 ? 2 * sg * bg / 255 : 255 - 2 * (255 - sg) * (255 - bg) / 255; + db = sb < 128 ? 2 * sb * bb / 255 : 255 - 2 * (255 - sb) * (255 - bb) / 255; + }, + + 'color-dodge': function() { + dr = br === 0 ? 0 : sr === 255 ? 255 : min(255, 255 * br / (255 - sr)); + dg = bg === 0 ? 0 : sg === 255 ? 255 : min(255, 255 * bg / (255 - sg)); + db = bb === 0 ? 0 : sb === 255 ? 255 : min(255, 255 * bb / (255 - sb)); + }, + + 'color-burn': function() { + dr = br === 255 ? 255 : sr === 0 ? 0 : max(0, 255 - (255 - br) * 255 / sr); + dg = bg === 255 ? 255 : sg === 0 ? 0 : max(0, 255 - (255 - bg) * 255 / sg); + db = bb === 255 ? 255 : sb === 0 ? 0 : max(0, 255 - (255 - bb) * 255 / sb); + }, + + darken: function() { + dr = br < sr ? br : sr; + dg = bg < sg ? bg : sg; + db = bb < sb ? bb : sb; + }, + + lighten: function() { + dr = br > sr ? br : sr; + dg = bg > sg ? bg : sg; + db = bb > sb ? bb : sb; + }, + + difference: function() { + dr = br - sr; + if (dr < 0) + dr = -dr; + dg = bg - sg; + if (dg < 0) + dg = -dg; + db = bb - sb; + if (db < 0) + db = -db; + }, + + exclusion: function() { + dr = br + sr * (255 - br - br) / 255; + dg = bg + sg * (255 - bg - bg) / 255; + db = bb + sb * (255 - bb - bb) / 255; + }, + + hue: function() { + setSat(sr, sg, sb, getSat(br, bg, bb)); + setLum(dr, dg, db, getLum(br, bg, bb)); + }, + + saturation: function() { + setSat(br, bg, bb, getSat(sr, sg, sb)); + setLum(dr, dg, db, getLum(br, bg, bb)); + }, + + luminosity: function() { + setLum(br, bg, bb, getLum(sr, sg, sb)); + }, + + color: function() { + setLum(sr, sg, sb, getLum(br, bg, bb)); + }, + + add: function() { + dr = min(br + sr, 255); + dg = min(bg + sg, 255); + db = min(bb + sb, 255); + }, + + subtract: function() { + dr = max(br - sr, 0); + dg = max(bg - sg, 0); + db = max(bb - sb, 0); + }, + + average: function() { + dr = (br + sr) / 2; + dg = (bg + sg) / 2; + db = (bb + sb) / 2; + }, + + negation: function() { + dr = 255 - abs(255 - sr - br); + dg = 255 - abs(255 - sg - bg); + db = 255 - abs(255 - sb - bb); + } + }; + + var nativeModes = this.nativeModes = Base.each([ + 'source-over', 'source-in', 'source-out', 'source-atop', + 'destination-over', 'destination-in', 'destination-out', + 'destination-atop', 'lighter', 'darker', 'copy', 'xor' + ], function(mode) { + this[mode] = true; + }, {}); + + var ctx = CanvasProvider.getContext(1, 1); + Base.each(modes, function(func, mode) { + var darken = mode === 'darken', + ok = false; + ctx.save(); + try { + ctx.fillStyle = darken ? '#300' : '#a00'; + ctx.fillRect(0, 0, 1, 1); + ctx.globalCompositeOperation = mode; + if (ctx.globalCompositeOperation === mode) { + ctx.fillStyle = darken ? '#a00' : '#300'; + ctx.fillRect(0, 0, 1, 1); + ok = ctx.getImageData(0, 0, 1, 1).data[0] !== darken ? 170 : 51; + } + } catch (e) {} + ctx.restore(); + nativeModes[mode] = ok; + }); + CanvasProvider.release(ctx); + + this.process = function(mode, srcContext, dstContext, alpha, offset) { + var srcCanvas = srcContext.canvas, + normal = mode === 'normal'; + if (normal || nativeModes[mode]) { + dstContext.save(); + dstContext.setTransform(1, 0, 0, 1, 0, 0); + dstContext.globalAlpha = alpha; + if (!normal) + dstContext.globalCompositeOperation = mode; + dstContext.drawImage(srcCanvas, offset.x, offset.y); + dstContext.restore(); + } else { + var process = modes[mode]; + if (!process) + return; + var dstData = dstContext.getImageData(offset.x, offset.y, + srcCanvas.width, srcCanvas.height), + dst = dstData.data, + src = srcContext.getImageData(0, 0, + srcCanvas.width, srcCanvas.height).data; + for (var i = 0, l = dst.length; i < l; i += 4) { + sr = src[i]; + br = dst[i]; + sg = src[i + 1]; + bg = dst[i + 1]; + sb = src[i + 2]; + bb = dst[i + 2]; + sa = src[i + 3]; + ba = dst[i + 3]; + process(); + var a1 = sa * alpha / 255, + a2 = 1 - a1; + dst[i] = a1 * dr + a2 * br; + dst[i + 1] = a1 * dg + a2 * bg; + dst[i + 2] = a1 * db + a2 * bb; + dst[i + 3] = sa * alpha + a2 * ba; + } + dstContext.putImageData(dstData, offset.x, offset.y); + } + }; +}; + +var SVGStyles = Base.each({ + fillColor: ['fill', 'color'], + strokeColor: ['stroke', 'color'], + strokeWidth: ['stroke-width', 'number'], + strokeCap: ['stroke-linecap', 'string'], + strokeJoin: ['stroke-linejoin', 'string'], + strokeScaling: ['vector-effect', 'lookup', { + true: 'none', + false: 'non-scaling-stroke' + }, function(item, value) { + return !value + && (item instanceof PathItem + || item instanceof Shape + || item instanceof TextItem); + }], + miterLimit: ['stroke-miterlimit', 'number'], + dashArray: ['stroke-dasharray', 'array'], + dashOffset: ['stroke-dashoffset', 'number'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], + fontSize: ['font-size', 'number'], + justification: ['text-anchor', 'lookup', { + left: 'start', + center: 'middle', + right: 'end' + }], + opacity: ['opacity', 'number'], + blendMode: ['mix-blend-mode', 'string'] +}, function(entry, key) { + var part = Base.capitalize(key), + lookup = entry[2]; + this[key] = { + type: entry[1], + property: key, + attribute: entry[0], + toSVG: lookup, + fromSVG: lookup && Base.each(lookup, function(value, name) { + this[value] = name; + }, {}), + exportFilter: entry[3], + get: 'get' + part, + set: 'set' + part + }; +}, {}); + +var SVGNamespaces = { + href: 'http://www.w3.org/1999/xlink', + xlink: 'http://www.w3.org/2000/xmlns' +}; + +new function() { + var formatter; + + function setAttributes(node, attrs) { + for (var key in attrs) { + var val = attrs[key], + namespace = SVGNamespaces[key]; + if (typeof val === 'number') + val = formatter.number(val); + if (namespace) { + node.setAttributeNS(namespace, key, val); + } else { + node.setAttribute(key, val); + } + } + return node; + } + + function createElement(tag, attrs) { + return setAttributes( + document.createElementNS('http://www.w3.org/2000/svg', tag), attrs); + } + + function getTransform(matrix, coordinates, center) { + var attrs = new Base(), + trans = matrix.getTranslation(); + if (coordinates) { + matrix = matrix.shiftless(); + var point = matrix._inverseTransform(trans); + attrs[center ? 'cx' : 'x'] = point.x; + attrs[center ? 'cy' : 'y'] = point.y; + trans = null; + } + if (!matrix.isIdentity()) { + var decomposed = matrix.decompose(); + if (decomposed && !decomposed.shearing) { + var parts = [], + angle = decomposed.rotation, + scale = decomposed.scaling; + if (trans && !trans.isZero()) + parts.push('translate(' + formatter.point(trans) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + if (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } + } + return attrs; + } + + function exportGroup(item, options) { + var attrs = getTransform(item._matrix), + children = item._children; + var node = createElement('g', attrs); + for (var i = 0, l = children.length; i < l; i++) { + var child = children[i]; + var childNode = exportSVG(child, options); + if (childNode) { + if (child.isClipMask()) { + var clip = createElement('clipPath'); + clip.appendChild(childNode); + setDefinition(child, clip, 'clip'); + setAttributes(node, { + 'clip-path': 'url(#' + clip.id + ')' + }); + } else { + node.appendChild(childNode); + } + } + } + return node; + } + + function exportRaster(item, options) { + var attrs = getTransform(item._matrix, true), + size = item.getSize(), + image = item.getImage(); + attrs.x -= size.width / 2; + attrs.y -= size.height / 2; + attrs.width = size.width; + attrs.height = size.height; + attrs.href = options.embedImages === false && image && image.src + || item.toDataURL(); + return createElement('image', attrs); + } + + function exportPath(item, options) { + var matchShapes = options.matchShapes; + if (matchShapes) { + var shape = item.toShape(false); + if (shape) + return exportShape(shape, options); + } + var segments = item._segments, + type, + attrs = getTransform(item._matrix); + if (segments.length === 0) + return null; + if (matchShapes && !item.hasHandles()) { + if (segments.length >= 3) { + type = item._closed ? 'polygon' : 'polyline'; + var parts = []; + for(var i = 0, l = segments.length; i < l; i++) + parts.push(formatter.point(segments[i]._point)); + attrs.points = parts.join(' '); + } else { + type = 'line'; + var first = segments[0]._point, + last = segments[segments.length - 1]._point; + attrs.set({ + x1: first.x, + y1: first.y, + x2: last.x, + y2: last.y + }); + } + } else { + type = 'path'; + attrs.d = item.getPathData(null, options.precision); + } + return createElement(type, attrs); + } + + function exportShape(item) { + var type = item._type, + radius = item._radius, + attrs = getTransform(item._matrix, true, type !== 'rectangle'); + if (type === 'rectangle') { + type = 'rect'; + var size = item._size, + width = size.width, + height = size.height; + attrs.x -= width / 2; + attrs.y -= height / 2; + attrs.width = width; + attrs.height = height; + if (radius.isZero()) + radius = null; + } + if (radius) { + if (type === 'circle') { + attrs.r = radius; + } else { + attrs.rx = radius.width; + attrs.ry = radius.height; + } + } + return createElement(type, attrs); + } + + function exportCompoundPath(item, options) { + var attrs = getTransform(item._matrix); + var data = item.getPathData(null, options.precision); + if (data) + attrs.d = data; + return createElement('path', attrs); + } + + function exportPlacedSymbol(item, options) { + var attrs = getTransform(item._matrix, true), + symbol = item.getSymbol(), + symbolNode = getDefinition(symbol, 'symbol'), + definition = symbol.getDefinition(), + bounds = definition.getBounds(); + if (!symbolNode) { + symbolNode = createElement('symbol', { + viewBox: formatter.rectangle(bounds) + }); + symbolNode.appendChild(exportSVG(definition, options)); + setDefinition(symbol, symbolNode, 'symbol'); + } + attrs.href = '#' + symbolNode.id; + attrs.x += bounds.x; + attrs.y += bounds.y; + attrs.width = formatter.number(bounds.width); + attrs.height = formatter.number(bounds.height); + attrs.overflow = 'visible'; + return createElement('use', attrs); + } + + function exportGradient(color) { + var gradientNode = getDefinition(color, 'color'); + if (!gradientNode) { + var gradient = color.getGradient(), + radial = gradient._radial, + origin = color.getOrigin().transform(), + destination = color.getDestination().transform(), + attrs; + if (radial) { + attrs = { + cx: origin.x, + cy: origin.y, + r: origin.getDistance(destination) + }; + var highlight = color.getHighlight(); + if (highlight) { + highlight = highlight.transform(); + attrs.fx = highlight.x; + attrs.fy = highlight.y; + } + } else { + attrs = { + x1: origin.x, + y1: origin.y, + x2: destination.x, + y2: destination.y + }; + } + attrs.gradientUnits = 'userSpaceOnUse'; + gradientNode = createElement( + (radial ? 'radial' : 'linear') + 'Gradient', attrs); + var stops = gradient._stops; + for (var i = 0, l = stops.length; i < l; i++) { + var stop = stops[i], + stopColor = stop._color, + alpha = stopColor.getAlpha(); + attrs = { + offset: stop._rampPoint, + 'stop-color': stopColor.toCSS(true) + }; + if (alpha < 1) + attrs['stop-opacity'] = alpha; + gradientNode.appendChild(createElement('stop', attrs)); + } + setDefinition(color, gradientNode, 'color'); + } + return 'url(#' + gradientNode.id + ')'; + } + + function exportText(item) { + var node = createElement('text', getTransform(item._matrix, true)); + node.textContent = item._content; + return node; + } + + var exporters = { + Group: exportGroup, + Layer: exportGroup, + Raster: exportRaster, + Path: exportPath, + Shape: exportShape, + CompoundPath: exportCompoundPath, + PlacedSymbol: exportPlacedSymbol, + PointText: exportText + }; + + function applyStyle(item, node, isRoot) { + var attrs = {}, + parent = !isRoot && item.getParent(); + + if (item._name != null) + attrs.id = item._name; + + Base.each(SVGStyles, function(entry) { + var get = entry.get, + type = entry.type, + value = item[get](); + if (entry.exportFilter + ? entry.exportFilter(item, value) + : !parent || !Base.equals(parent[get](), value)) { + if (type === 'color' && value != null) { + var alpha = value.getAlpha(); + if (alpha < 1) + attrs[entry.attribute + '-opacity'] = alpha; + } + attrs[entry.attribute] = value == null + ? 'none' + : type === 'number' + ? formatter.number(value) + : type === 'color' + ? value.gradient + ? exportGradient(value, item) + : value.toCSS(true) + : type === 'array' + ? value.join(',') + : type === 'lookup' + ? entry.toSVG[value] + : value; + } + }); + + if (attrs.opacity === 1) + delete attrs.opacity; + + if (!item._visible) + attrs.visibility = 'hidden'; + + return setAttributes(node, attrs); + } + + var definitions; + function getDefinition(item, type) { + if (!definitions) + definitions = { ids: {}, svgs: {} }; + return item && definitions.svgs[type + '-' + item._id]; + } + + function setDefinition(item, node, type) { + if (!definitions) + getDefinition(); + var id = definitions.ids[type] = (definitions.ids[type] || 0) + 1; + node.id = type + '-' + id; + definitions.svgs[type + '-' + item._id] = node; + } + + function exportDefinitions(node, options) { + var svg = node, + defs = null; + if (definitions) { + svg = node.nodeName.toLowerCase() === 'svg' && node; + for (var i in definitions.svgs) { + if (!defs) { + if (!svg) { + svg = createElement('svg'); + svg.appendChild(node); + } + defs = svg.insertBefore(createElement('defs'), + svg.firstChild); + } + defs.appendChild(definitions.svgs[i]); + } + definitions = null; + } + return options.asString + ? new XMLSerializer().serializeToString(svg) + : svg; + } + + function exportSVG(item, options, isRoot) { + var exporter = exporters[item._class], + node = exporter && exporter(item, options); + if (node) { + var onExport = options.onExport; + if (onExport) + node = onExport(item, node, options) || node; + var data = JSON.stringify(item._data); + if (data && data !== '{}' && data !== 'null') + node.setAttribute('data-paper-data', data); + } + return node && applyStyle(item, node, isRoot); + } + + function setOptions(options) { + if (!options) + options = {}; + formatter = new Formatter(options.precision); + return options; + } + + Item.inject({ + exportSVG: function(options) { + options = setOptions(options); + return exportDefinitions(exportSVG(this, options, true), options); + } + }); + + Project.inject({ + exportSVG: function(options) { + options = setOptions(options); + var layers = this.layers, + view = this.getView(), + size = view.getViewSize(), + node = createElement('svg', { + x: 0, + y: 0, + width: size.width, + height: size.height, + version: '1.1', + xmlns: 'http://www.w3.org/2000/svg', + 'xmlns:xlink': 'http://www.w3.org/1999/xlink' + }), + parent = node, + matrix = view._matrix; + if (!matrix.isIdentity()) + parent = node.appendChild( + createElement('g', getTransform(matrix))); + for (var i = 0, l = layers.length; i < l; i++) + parent.appendChild(exportSVG(layers[i], options, true)); + return exportDefinitions(node, options); + } + }); +}; + +new function() { + + function getValue(node, name, isString, allowNull) { + var namespace = SVGNamespaces[name], + value = namespace + ? node.getAttributeNS(namespace, name) + : node.getAttribute(name); + if (value === 'null') + value = null; + return value == null + ? allowNull + ? null + : isString + ? '' + : 0 + : isString + ? value + : parseFloat(value); + } + + function getPoint(node, x, y, allowNull) { + x = getValue(node, x, false, allowNull); + y = getValue(node, y, false, allowNull); + return allowNull && (x == null || y == null) ? null + : new Point(x, y); + } + + function getSize(node, w, h, allowNull) { + w = getValue(node, w, false, allowNull); + h = getValue(node, h, false, allowNull); + return allowNull && (w == null || h == null) ? null + : new Size(w, h); + } + + function convertValue(value, type, lookup) { + return value === 'none' + ? null + : type === 'number' + ? parseFloat(value) + : type === 'array' + ? value ? value.split(/[\s,]+/g).map(parseFloat) : [] + : type === 'color' + ? getDefinition(value) || value + : type === 'lookup' + ? lookup[value] + : value; + } + + function importGroup(node, type, options, isRoot) { + var nodes = node.childNodes, + isClip = type === 'clippath', + item = new Group(), + project = item._project, + currentStyle = project._currentStyle, + children = []; + if (!isClip) { + item = applyAttributes(item, node, isRoot); + project._currentStyle = item._style.clone(); + } + if (isRoot) { + var defs = node.querySelectorAll('defs'); + for (var i = 0, l = defs.length; i < l; i++) { + importSVG(defs[i], options, false); + } + } + for (var i = 0, l = nodes.length; i < l; i++) { + var childNode = nodes[i], + child; + if (childNode.nodeType === 1 + && childNode.nodeName.toLowerCase() !== 'defs' + && (child = importSVG(childNode, options, false)) + && !(child instanceof Symbol)) + children.push(child); + } + item.addChildren(children); + if (isClip) + item = applyAttributes(item.reduce(), node, isRoot); + project._currentStyle = currentStyle; + if (isClip || type === 'defs') { + item.remove(); + item = null; + } + return item; + } + + function importPoly(node, type) { + var coords = node.getAttribute('points').match( + /[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g), + points = []; + for (var i = 0, l = coords.length; i < l; i += 2) + points.push(new Point( + parseFloat(coords[i]), + parseFloat(coords[i + 1]))); + var path = new Path(points); + if (type === 'polygon') + path.closePath(); + return path; + } + + function importPath(node) { + var data = node.getAttribute('d'), + param = { pathData: data }; + return (data.match(/m/gi) || []).length > 1 || /z\S+/i.test(data) + ? new CompoundPath(param) + : new Path(param); + } + + function importGradient(node, type) { + var id = (getValue(node, 'href', true) || '').substring(1), + isRadial = type === 'radialgradient', + gradient; + if (id) { + gradient = definitions[id].getGradient(); + } else { + var nodes = node.childNodes, + stops = []; + for (var i = 0, l = nodes.length; i < l; i++) { + var child = nodes[i]; + if (child.nodeType === 1) + stops.push(applyAttributes(new GradientStop(), child)); + } + gradient = new Gradient(stops, isRadial); + } + var origin, destination, highlight; + if (isRadial) { + origin = getPoint(node, 'cx', 'cy'); + destination = origin.add(getValue(node, 'r'), 0); + highlight = getPoint(node, 'fx', 'fy', true); + } else { + origin = getPoint(node, 'x1', 'y1'); + destination = getPoint(node, 'x2', 'y2'); + } + applyAttributes( + new Color(gradient, origin, destination, highlight), node); + return null; + } + + var importers = { + '#document': function (node, type, options, isRoot) { + var nodes = node.childNodes; + for (var i = 0, l = nodes.length; i < l; i++) { + var child = nodes[i]; + if (child.nodeType === 1) { + var next = child.nextSibling; + document.body.appendChild(child); + var item = importSVG(child, options, isRoot); + if (next) { + node.insertBefore(child, next); + } else { + node.appendChild(child); + } + return item; + } + } + }, + g: importGroup, + svg: importGroup, + clippath: importGroup, + polygon: importPoly, + polyline: importPoly, + path: importPath, + lineargradient: importGradient, + radialgradient: importGradient, + + image: function (node) { + var raster = new Raster(getValue(node, 'href', true)); + raster.on('load', function() { + var size = getSize(node, 'width', 'height'); + this.setSize(size); + var center = this._matrix._transformPoint( + getPoint(node, 'x', 'y').add(size.divide(2))); + this.translate(center); + }); + return raster; + }, + + symbol: function(node, type, options, isRoot) { + return new Symbol(importGroup(node, type, options, isRoot), true); + }, + + defs: importGroup, + + use: function(node) { + var id = (getValue(node, 'href', true) || '').substring(1), + definition = definitions[id], + point = getPoint(node, 'x', 'y'); + return definition + ? definition instanceof Symbol + ? definition.place(point) + : definition.clone().translate(point) + : null; + }, + + circle: function(node) { + return new Shape.Circle(getPoint(node, 'cx', 'cy'), + getValue(node, 'r')); + }, + + ellipse: function(node) { + return new Shape.Ellipse({ + center: getPoint(node, 'cx', 'cy'), + radius: getSize(node, 'rx', 'ry') + }); + }, + + rect: function(node) { + var point = getPoint(node, 'x', 'y'), + size = getSize(node, 'width', 'height'), + radius = getSize(node, 'rx', 'ry'); + return new Shape.Rectangle(new Rectangle(point, size), radius); + }, + + line: function(node) { + return new Path.Line(getPoint(node, 'x1', 'y1'), + getPoint(node, 'x2', 'y2')); + }, + + text: function(node) { + var text = new PointText(getPoint(node, 'x', 'y') + .add(getPoint(node, 'dx', 'dy'))); + text.setContent(node.textContent.trim() || ''); + return text; + } + }; + + function applyTransform(item, value, name, node) { + var transforms = (node.getAttribute(name) || '').split(/\)\s*/g), + matrix = new Matrix(); + for (var i = 0, l = transforms.length; i < l; i++) { + var transform = transforms[i]; + if (!transform) + break; + var parts = transform.split(/\(\s*/), + command = parts[0], + v = parts[1].split(/[\s,]+/g); + for (var j = 0, m = v.length; j < m; j++) + v[j] = parseFloat(v[j]); + switch (command) { + case 'matrix': + matrix.concatenate( + new Matrix(v[0], v[1], v[2], v[3], v[4], v[5])); + break; + case 'rotate': + matrix.rotate(v[0], v[1], v[2]); + break; + case 'translate': + matrix.translate(v[0], v[1]); + break; + case 'scale': + matrix.scale(v); + break; + case 'skewX': + matrix.skew(v[0], 0); + break; + case 'skewY': + matrix.skew(0, v[0]); + break; + } + } + item.transform(matrix); + } + + function applyOpacity(item, value, name) { + var color = item[name === 'fill-opacity' ? 'getFillColor' + : 'getStrokeColor'](); + if (color) + color.setAlpha(parseFloat(value)); + } + + var attributes = Base.set(Base.each(SVGStyles, function(entry) { + this[entry.attribute] = function(item, value) { + item[entry.set](convertValue(value, entry.type, entry.fromSVG)); + if (entry.type === 'color' && item instanceof Shape) { + var color = item[entry.get](); + if (color) + color.transform(new Matrix().translate( + item.getPosition(true).negate())); + } + }; + }, {}), { + id: function(item, value) { + definitions[value] = item; + if (item.setName) + item.setName(value); + }, + + 'clip-path': function(item, value) { + var clip = getDefinition(value); + if (clip) { + clip = clip.clone(); + clip.setClipMask(true); + if (item instanceof Group) { + item.insertChild(0, clip); + } else { + return new Group(clip, item); + } + } + }, + + gradientTransform: applyTransform, + transform: applyTransform, + + 'fill-opacity': applyOpacity, + 'stroke-opacity': applyOpacity, + + visibility: function(item, value) { + item.setVisible(value === 'visible'); + }, + + display: function(item, value) { + item.setVisible(value !== null); + }, + + 'stop-color': function(item, value) { + if (item.setColor) + item.setColor(value); + }, + + 'stop-opacity': function(item, value) { + if (item._color) + item._color.setAlpha(parseFloat(value)); + }, + + offset: function(item, value) { + var percentage = value.match(/(.*)%$/); + item.setRampPoint(percentage + ? percentage[1] / 100 + : parseFloat(value)); + }, + + viewBox: function(item, value, name, node, styles) { + var rect = new Rectangle(convertValue(value, 'array')), + size = getSize(node, 'width', 'height', true); + if (item instanceof Group) { + var scale = size ? rect.getSize().divide(size) : 1, + matrix = new Matrix().translate(rect.getPoint()).scale(scale); + item.transform(matrix.inverted()); + } else if (item instanceof Symbol) { + if (size) + rect.setSize(size); + var clip = getAttribute(node, 'overflow', styles) != 'visible', + group = item._definition; + if (clip && !rect.contains(group.getBounds())) { + clip = new Shape.Rectangle(rect).transform(group._matrix); + clip.setClipMask(true); + group.addChild(clip); + } + } + } + }); + + function getAttribute(node, name, styles) { + var attr = node.attributes[name], + value = attr && attr.value; + if (!value) { + var style = Base.camelize(name); + value = node.style[style]; + if (!value && styles.node[style] !== styles.parent[style]) + value = styles.node[style]; + } + return !value + ? undefined + : value === 'none' + ? null + : value; + } + + function applyAttributes(item, node, isRoot) { + var styles = { + node: DomElement.getStyles(node) || {}, + parent: !isRoot && DomElement.getStyles(node.parentNode) || {} + }; + Base.each(attributes, function(apply, name) { + var value = getAttribute(node, name, styles); + if (value !== undefined) + item = Base.pick(apply(item, value, name, node, styles), item); + }); + return item; + } + + var definitions = {}; + function getDefinition(value) { + var match = value && value.match(/\((?:#|)([^)']+)/); + return match && definitions[match[1]]; + } + + function importSVG(source, options, isRoot) { + if (!source) + return null; + if (!options) { + options = {}; + } else if (typeof options === 'function') { + options = { onLoad: options }; + } + + var node = source, + scope = paper; + + function onLoadCallback(svg) { + paper = scope; + var item = importSVG(svg, options, isRoot), + onLoad = options.onLoad, + view = scope.project && scope.getView(); + if (onLoad) + onLoad.call(this, item); + view.update(); + } + + if (isRoot) { + if (typeof source === 'string' && !/^.*3){n.sort(function(e,r){return r.length-e.length}),t+="switch(str.length){";for(var a=0;abr&&10!==t&&13!==t&&8232!==t&&8233!==t;)++br,t=dr.charCodeAt(br);fr.onComment&&fr.onComment(!1,dr.slice(e+2,br),e,br,r,fr.locations&&new a)}function u(){for(;pr>br;){var e=dr.charCodeAt(br);if(32===e)++br;else if(13===e){++br;var r=dr.charCodeAt(br);10===r&&++br,fr.locations&&(++Ar,Sr=br)}else if(10===e||8232===e||8233===e)++br,fr.locations&&(++Ar,Sr=br);else if(e>8&&14>e)++br;else if(47===e){var r=dr.charCodeAt(br+1);if(42===r)s();else{if(47!==r)break;c()}}else if(160===e)++br;else{if(!(e>=5760&&Jt.test(String.fromCharCode(e))))break;++br}}}function l(){var e=dr.charCodeAt(br+1);return e>=48&&57>=e?E(!0):(++br,i(xt))}function f(){var e=dr.charCodeAt(br+1);return Er?(++br,k()):61===e?x(Et,2):x(wt,1)}function d(){var e=dr.charCodeAt(br+1);return 61===e?x(Et,2):x(Dt,1)}function p(e){var r=dr.charCodeAt(br+1);return r===e?x(124===e?Lt:Ut,2):61===r?x(Et,2):x(124===e?Rt:Tt,1)}function h(){var e=dr.charCodeAt(br+1);return 61===e?x(Et,2):x(Vt,1)}function m(e){var r=dr.charCodeAt(br+1);return r===e?45==r&&62==dr.charCodeAt(br+2)&&Gt.test(dr.slice(Lr,br))?(br+=3,c(),u(),g()):x(St,2):61===r?x(Et,2):x(At,1)}function v(e){var r=dr.charCodeAt(br+1),t=1;return r===e?(t=62===e&&62===dr.charCodeAt(br+2)?3:2,61===dr.charCodeAt(br+t)?x(Et,t+1):x(jt,t)):33==r&&60==e&&45==dr.charCodeAt(br+2)&&45==dr.charCodeAt(br+3)?(br+=4,c(),u(),g()):(61===r&&(t=61===dr.charCodeAt(br+2)?3:2),x(Ot,t))}function b(e){var r=dr.charCodeAt(br+1);return 61===r?x(qt,61===dr.charCodeAt(br+2)?3:2):x(61===e?Ct:It,1)}function y(e){switch(e){case 46:return l();case 40:return++br,i(mt);case 41:return++br,i(vt);case 59:return++br,i(yt);case 44:return++br,i(bt);case 91:return++br,i(ft);case 93:return++br,i(dt);case 123:return++br,i(pt);case 125:return++br,i(ht);case 58:return++br,i(gt);case 63:return++br,i(kt);case 48:var r=dr.charCodeAt(br+1);if(120===r||88===r)return C();case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return E(!1);case 34:case 39:return A(e);case 47:return f(e);case 37:case 42:return d();case 124:case 38:return p(e);case 94:return h();case 43:case 45:return m(e);case 60:case 62:return v(e);case 61:case 33:return b(e);case 126:return x(It,1)}return!1}function g(e){if(e?br=yr+1:yr=br,fr.locations&&(xr=new a),e)return k();if(br>=pr)return i(Br);var r=dr.charCodeAt(br);if(Qt(r)||92===r)return L();var n=y(r);if(n===!1){var o=String.fromCharCode(r);if("\\"===o||$t.test(o))return L();t(br,"Unexpected character '"+o+"'")}return n}function x(e,r){var t=dr.slice(br,br+r);br+=r,i(e,t)}function k(){for(var e,r,n="",a=br;;){br>=pr&&t(a,"Unterminated regular expression");var o=dr.charAt(br);if(Gt.test(o)&&t(a,"Unterminated regular expression"),e)e=!1;else{if("["===o)r=!0;else if("]"===o&&r)r=!1;else if("/"===o&&!r)break;e="\\"===o}++br}var n=dr.slice(a,br);++br;var s=I();return s&&!/^[gmsiy]*$/.test(s)&&t(a,"Invalid regexp flag"),i(jr,new RegExp(n,s))}function w(e,r){for(var t=br,n=0,a=0,o=null==r?1/0:r;o>a;++a){var i,s=dr.charCodeAt(br);if(i=s>=97?s-97+10:s>=65?s-65+10:s>=48&&57>=s?s-48:1/0,i>=e)break;++br,n=n*e+i}return br===t||null!=r&&br-t!==r?null:n}function C(){br+=2;var e=w(16);return null==e&&t(yr+2,"Expected hexadecimal number"),Qt(dr.charCodeAt(br))&&t(br,"Identifier directly after number"),i(Or,e)}function E(e){var r=br,n=!1,a=48===dr.charCodeAt(br);e||null!==w(10)||t(r,"Invalid number"),46===dr.charCodeAt(br)&&(++br,w(10),n=!0);var o=dr.charCodeAt(br);(69===o||101===o)&&(o=dr.charCodeAt(++br),(43===o||45===o)&&++br,null===w(10)&&t(r,"Invalid number"),n=!0),Qt(dr.charCodeAt(br))&&t(br,"Identifier directly after number");var s,c=dr.slice(r,br);return n?s=parseFloat(c):a&&1!==c.length?/[89]/.test(c)||Tr?t(r,"Invalid number"):s=parseInt(c,8):s=parseInt(c,10),i(Or,s)}function A(e){br++;for(var r="";;){br>=pr&&t(yr,"Unterminated string constant");var n=dr.charCodeAt(br);if(n===e)return++br,i(Dr,r);if(92===n){n=dr.charCodeAt(++br);var a=/^[0-7]+/.exec(dr.slice(br,br+3));for(a&&(a=a[0]);a&&parseInt(a,8)>255;)a=a.slice(0,a.length-1);if("0"===a&&(a=null),++br,a)Tr&&t(br-2,"Octal literal in strict mode"),r+=String.fromCharCode(parseInt(a,8)),br+=a.length-1;else switch(n){case 110:r+="\n";break;case 114:r+="\r";break;case 120:r+=String.fromCharCode(S(2));break;case 117:r+=String.fromCharCode(S(4));break;case 85:r+=String.fromCharCode(S(8));break;case 116:r+=" ";break;case 98:r+="\b";break;case 118:r+=" ";break;case 102:r+="\f";break;case 48:r+="\0";break;case 13:10===dr.charCodeAt(br)&&++br;case 10:fr.locations&&(Sr=br,++Ar);break;default:r+=String.fromCharCode(n)}}else(13===n||10===n||8232===n||8233===n)&&t(yr,"Unterminated string constant"),r+=String.fromCharCode(n),++br}}function S(e){var r=w(16,e);return null===r&&t(yr,"Bad character escape sequence"),r}function I(){Bt=!1;for(var e,r=!0,n=br;;){var a=dr.charCodeAt(br);if(Yt(a))Bt&&(e+=dr.charAt(br)),++br;else{if(92!==a)break;Bt||(e=dr.slice(n,br)),Bt=!0,117!=dr.charCodeAt(++br)&&t(br,"Expecting Unicode escape sequence \\uXXXX"),++br;var o=S(4),i=String.fromCharCode(o);i||t(br-1,"Invalid Unicode escape"),(r?Qt(o):Yt(o))||t(br-4,"Invalid Unicode escape"),e+=i}r=!1}return Bt?e:dr.slice(n,br)}function L(){var e=I(),r=Fr;return Bt||(Wt(e)?r=lt[e]:(fr.forbidReserved&&(3===fr.ecmaVersion?Mt:zt)(e)||Tr&&Xt(e))&&t(yr,"The keyword '"+e+"' is reserved")),i(r,e)}function U(){Ir=yr,Lr=gr,Ur=kr,g()}function R(e){if(Tr=e,br=Lr,fr.locations)for(;Sr>br;)Sr=dr.lastIndexOf("\n",Sr-2)+1,--Ar;u(),g()}function V(){this.type=null,this.start=yr,this.end=null}function T(){this.start=xr,this.end=null,null!==hr&&(this.source=hr)}function q(){var e=new V;return fr.locations&&(e.loc=new T),fr.ranges&&(e.range=[yr,0]),e}function O(e){var r=new V;return r.start=e.start,fr.locations&&(r.loc=new T,r.loc.start=e.loc.start),fr.ranges&&(r.range=[e.range[0],0]),r}function j(e,r){return e.type=r,e.end=Lr,fr.locations&&(e.loc.end=Ur),fr.ranges&&(e.range[1]=Lr),e}function D(e){return fr.ecmaVersion>=5&&"ExpressionStatement"===e.type&&"Literal"===e.expression.type&&"use strict"===e.expression.value}function F(e){return wr===e?(U(),!0):void 0}function B(){return!fr.strictSemicolons&&(wr===Br||wr===ht||Gt.test(dr.slice(Lr,yr)))}function M(){F(yt)||B()||X()}function z(e){wr===e?U():X()}function X(){t(yr,"Unexpected token")}function N(e){"Identifier"!==e.type&&"MemberExpression"!==e.type&&t(e.start,"Assigning to rvalue"),Tr&&"Identifier"===e.type&&Nt(e.name)&&t(e.start,"Assigning to "+e.name+" in strict mode")}function W(e){Ir=Lr=br,fr.locations&&(Ur=new a),Rr=Tr=null,Vr=[],g();var r=e||q(),t=!0;for(e||(r.body=[]);wr!==Br;){var n=J();r.body.push(n),t&&D(n)&&R(!0),t=!1}return j(r,"Program")}function J(){(wr===wt||wr===Et&&"/="==Cr)&&g(!0);var e=wr,r=q();switch(e){case Mr:case Nr:U();var n=e===Mr;F(yt)||B()?r.label=null:wr!==Fr?X():(r.label=lr(),M());for(var a=0;ar){var a=O(e);a.left=e,a.operator=Cr,U(),a.right=er(rr(),n,t);var o=j(a,/&&|\|\|/.test(a.operator)?"LogicalExpression":"BinaryExpression");return er(o,r,t)}return e}function rr(){if(wr.prefix){var e=q(),r=wr.isUpdate;return e.operator=Cr,e.prefix=!0,Er=!0,U(),e.argument=rr(),r?N(e.argument):Tr&&"delete"===e.operator&&"Identifier"===e.argument.type&&t(e.start,"Deleting local variable in strict mode"),j(e,r?"UpdateExpression":"UnaryExpression")}for(var n=tr();wr.postfix&&!B();){var e=O(n);e.operator=Cr,e.prefix=!1,e.argument=n,N(n),U(),n=j(e,"UpdateExpression")}return n}function tr(){return nr(ar())}function nr(e,r){if(F(xt)){var t=O(e);return t.object=e,t.property=lr(!0),t.computed=!1,nr(j(t,"MemberExpression"),r)}if(F(ft)){var t=O(e);return t.object=e,t.property=K(),t.computed=!0,z(dt),nr(j(t,"MemberExpression"),r)}if(!r&&F(mt)){var t=O(e);return t.callee=e,t.arguments=ur(vt,!1),nr(j(t,"CallExpression"),r)}return e}function ar(){switch(wr){case ot:var e=q();return U(),j(e,"ThisExpression");case Fr:return lr();case Or:case Dr:case jr:var e=q();return e.value=Cr,e.raw=dr.slice(yr,gr),U(),j(e,"Literal");case it:case st:case ct:var e=q();return e.value=wr.atomValue,e.raw=wr.keyword,U(),j(e,"Literal");case mt:var r=xr,t=yr;U();var n=K();return n.start=t,n.end=gr,fr.locations&&(n.loc.start=r,n.loc.end=kr),fr.ranges&&(n.range=[t,gr]),z(vt),n;case ft:var e=q();return U(),e.elements=ur(dt,!0,!0),j(e,"ArrayExpression");case pt:return ir();case Gr:var e=q();return U(),cr(e,!1);case at:return or();default:X()}}function or(){var e=q();return U(),e.callee=nr(ar(),!0),e.arguments=F(mt)?ur(vt,!1):qr,j(e,"NewExpression")}function ir(){var e=q(),r=!0,n=!1;for(e.properties=[],U();!F(ht);){if(r)r=!1;else if(z(bt),fr.allowTrailingCommas&&F(ht))break;var a,o={key:sr()},i=!1;if(F(gt)?(o.value=K(!0),a=o.kind="init"):fr.ecmaVersion>=5&&"Identifier"===o.key.type&&("get"===o.key.name||"set"===o.key.name)?(i=n=!0,a=o.kind=o.key.name,o.key=sr(),wr!==mt&&X(),o.value=cr(q(),!1)):X(),"Identifier"===o.key.type&&(Tr||n))for(var s=0;si?e.id:e.params[i];if((Xt(s.name)||Nt(s.name))&&t(s.start,"Defining '"+s.name+"' in strict mode"),i>=0)for(var c=0;i>c;++c)s.name===e.params[c].name&&t(s.start,"Argument name clash in strict mode")}return j(e,r?"FunctionDeclaration":"FunctionExpression")}function ur(e,r,t){for(var n=[],a=!0;!F(e);){if(a)a=!1;else if(z(bt),r&&fr.allowTrailingCommas&&F(e))break;t&&wr===bt?n.push(null):n.push(K(!0))}return n}function lr(e){var r=q();return r.name=wr===Fr?Cr:e&&!fr.forbidReserved&&wr.keyword||X(),Er=!1,U(),j(r,"Identifier")}e.version="0.4.0";var fr,dr,pr,hr;e.parse=function(e,t){return dr=String(e),pr=dr.length,r(t),o(),W(fr.program)};var mr=e.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},vr=e.getLineInfo=function(e,r){for(var t=1,n=0;;){Kt.lastIndex=n;var a=Kt.exec(e);if(!(a&&a.indexe?36===e:91>e?!0:97>e?95===e:123>e?!0:e>=170&&$t.test(String.fromCharCode(e))},Yt=e.isIdentifierChar=function(e){return 48>e?36===e:58>e?!0:65>e?!1:91>e?!0:97>e?95===e:123>e?!0:e>=170&&_t.test(String.fromCharCode(e))},Zt={kind:"loop"},en={kind:"switch"}}); + + var binaryOperators = { + '+': '__add', + '-': '__subtract', + '*': '__multiply', + '/': '__divide', + '%': '__modulo', + '==': 'equals', + '!=': 'equals' + }; + + var unaryOperators = { + '-': '__negate', + '+': null + }; + + var fields = Base.each( + ['add', 'subtract', 'multiply', 'divide', 'modulo', 'negate'], + function(name) { + this['__' + name] = '#' + name; + }, + {} + ); + Point.inject(fields); + Size.inject(fields); + Color.inject(fields); + + function __$__(left, operator, right) { + var handler = binaryOperators[operator]; + if (left && left[handler]) { + var res = left[handler](right); + return operator === '!=' ? !res : res; + } + switch (operator) { + case '+': return left + right; + case '-': return left - right; + case '*': return left * right; + case '/': return left / right; + case '%': return left % right; + case '==': return left == right; + case '!=': return left != right; + } + } + + function $__(operator, value) { + var handler = unaryOperators[operator]; + if (handler && value && value[handler]) + return value[handler](); + switch (operator) { + case '+': return +value; + case '-': return -value; + } + } + + function parse(code, options) { + return scope.acorn.parse(code, options); + } + + function compile(code, url, options) { + if (!code) + return ''; + options = options || {}; + url = url || ''; + + var insertions = []; + + function getOffset(offset) { + for (var i = 0, l = insertions.length; i < l; i++) { + var insertion = insertions[i]; + if (insertion[0] >= offset) + break; + offset += insertion[1]; + } + return offset; + } + + function getCode(node) { + return code.substring(getOffset(node.range[0]), + getOffset(node.range[1])); + } + + function getBetween(left, right) { + return code.substring(getOffset(left.range[1]), + getOffset(right.range[0])); + } + + function replaceCode(node, str) { + var start = getOffset(node.range[0]), + end = getOffset(node.range[1]), + insert = 0; + for (var i = insertions.length - 1; i >= 0; i--) { + if (start > insertions[i][0]) { + insert = i + 1; + break; + } + } + insertions.splice(insert, 0, [start, str.length - end + start]); + code = code.substring(0, start) + str + code.substring(end); + } + + function walkAST(node, parent) { + if (!node) + return; + for (var key in node) { + if (key === 'range' || key === 'loc') + continue; + var value = node[key]; + if (Array.isArray(value)) { + for (var i = 0, l = value.length; i < l; i++) + walkAST(value[i], node); + } else if (value && typeof value === 'object') { + walkAST(value, node); + } + } + switch (node.type) { + case 'UnaryExpression': + if (node.operator in unaryOperators + && node.argument.type !== 'Literal') { + var arg = getCode(node.argument); + replaceCode(node, '$__("' + node.operator + '", ' + + arg + ')'); + } + break; + case 'BinaryExpression': + if (node.operator in binaryOperators + && node.left.type !== 'Literal') { + var left = getCode(node.left), + right = getCode(node.right), + between = getBetween(node.left, node.right), + operator = node.operator; + replaceCode(node, '__$__(' + left + ',' + + between.replace(new RegExp('\\' + operator), + '"' + operator + '"') + + ', ' + right + ')'); + } + break; + case 'UpdateExpression': + case 'AssignmentExpression': + var parentType = parent && parent.type; + if (!( + parentType === 'ForStatement' + || parentType === 'BinaryExpression' + && /^[=!<>]/.test(parent.operator) + || parentType === 'MemberExpression' && parent.computed + )) { + if (node.type === 'UpdateExpression') { + var arg = getCode(node.argument), + exp = '__$__(' + arg + ', "' + node.operator[0] + + '", 1)', + str = arg + ' = ' + exp; + if (!node.prefix + && (parentType === 'AssignmentExpression' + || parentType === 'VariableDeclarator')) { + if (getCode(parent.left || parent.id) === arg) + str = exp; + str = arg + '; ' + str; + } + replaceCode(node, str); + } else { + if (/^.=$/.test(node.operator) + && node.left.type !== 'Literal') { + var left = getCode(node.left), + right = getCode(node.right); + replaceCode(node, left + ' = __$__(' + left + ', "' + + node.operator[0] + '", ' + right + ')'); + } + } + } + break; + } + } + walkAST(parse(code, { ranges: true })); + return code; + } + + function execute(code, scope, url, options) { + paper = scope; + var view = scope.getView(), + tool = /\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(code) + ? new Tool() + : null, + toolHandlers = tool ? tool._events : [], + handlers = ['onFrame', 'onResize'].concat(toolHandlers), + params = [], + args = [], + func; + code = compile(code, url, options); + function expose(scope, hidden) { + for (var key in scope) { + if ((hidden || !/^_/.test(key)) && new RegExp('([\\b\\s\\W]|^)' + + key.replace(/\$/g, '\\$') + '\\b').test(code)) { + params.push(key); + args.push(scope[key]); + } + } + } + expose({ __$__: __$__, $__: $__, paper: scope, view: view, tool: tool }, + true); + expose(scope); + handlers = Base.each(handlers, function(key) { + if (new RegExp('\\s+' + key + '\\b').test(code)) { + params.push(key); + this.push(key + ': ' + key); + } + }, []).join(', '); + if (handlers) + code += '\nreturn { ' + handlers + ' };'; + func = Function(params, code); + var res = func.apply(scope, args) || {}; + Base.each(toolHandlers, function(key) { + var value = res[key]; + if (value) + tool[key] = value; + }); + if (view) { + if (res.onResize) + view.setOnResize(res.onResize); + view.emit('resize', { + size: view.size, + delta: new Point() + }); + if (res.onFrame) + view.setOnFrame(res.onFrame); + view.update(); + } + } + + var fs = require('fs'), + path = require('path'); + + require.extensions['.pjs'] = function(module, uri) { + module.exports = function(canvas) { + var source = compile(fs.readFileSync(uri, 'utf8')), + scope = new PaperScope(); + scope.setup(canvas); + scope.__filename = uri; + scope.__dirname = path.dirname(uri); + scope.require = require; + scope.console = console; + execute(source, scope); + return scope; + }; + }; + + return { + compile: compile, + execute: execute, + parse: parse + }; + +}).call(this); + +paper = new (PaperScope.inject(Base.exports, { + enumerable: true, + Base: Base, + Numerical: Numerical, + XMLSerializer: XMLSerializer, + DOMParser: DOMParser, + Canvas: Canvas +}))(); + +module.exports = paper; + +return paper; +}; diff --git a/package.json b/package.json index 612e05e7..2b7046b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "paper", - "version": "0.9.24", + "version": "0.9.25", "description": "The Swiss Army Knife of Vector Graphics Scripting", "license": "MIT", "homepage": "http://paperjs.org", diff --git a/src/options.js b/src/options.js index b17fc3a9..d3c0dcab 100644 --- a/src/options.js +++ b/src/options.js @@ -17,7 +17,7 @@ // The paper.js version. // NOTE: Adjust value here before calling publish.sh, which then updates and // publishes the various JSON package files automatically. -var version = '0.9.24'; +var version = '0.9.25'; var __options = { // If this file is loaded in the browser, we're in dev mode through load.js