diff --git a/bower.json b/bower.json index ae64abd8..31e81607 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "paper", - "version": "0.9.15", + "version": "0.9.16", "main": "dist/paper.js", "ignore": [ "build", diff --git a/dist/paper-core.js b/dist/paper-core.js index cab0557c..753ad00f 100644 --- a/dist/paper-core.js +++ b/dist/paper-core.js @@ -1,28 +1,28 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * Acorn.js * http://marijnhaverbeke.nl/acorn/ * * Acorn is a tiny, fast JavaScript parser written in JavaScript, @@ -33,8 +33,7 @@ var paper = new function(undefined) { var Base = new function() { - var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/, - slice = [].slice, + var hidden = /^(statics|preserve|enumerable|prototype|toString|valueOf)$/, forEach = [].forEach || function(iter, bind) { for (var i = 0, l = this.length; i < l; i++) @@ -79,10 +78,10 @@ var Base = new function() { return _define(obj, name, desc); }; - function inject(dest, src, enumerable, base, preserve, generics) { + function inject(dest, src, enumerable, base, preserve) { var beans; - function field(name, val, dontCheck, generics) { + function field(name, val, dontCheck) { var val = val || (val = describe(src, name)) && (val.get ? val : val.value); if (typeof val === 'string' && val[0] === '#') @@ -96,11 +95,11 @@ var Base = new function() { && (!preserve || !prev)) { if (isFunc && prev) val.base = prev; - if (isFunc && beans && val.length === 0 - && (bean = name.match(/^(get|is)(([A-Z])(.*))$/))) - beans.push([ bean[3].toLowerCase() + bean[4], bean[2] ]); + if (isFunc && beans + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beans[bean[3].toLowerCase() + bean[4]] = bean[2]; if (!res || isFunc || !res.get || typeof res.get !== 'function' - || res.get.length !== 0) + || !Base.isPlainObject(res)) res = { value: res, writable: true }; if ((describe(dest, name) || { configurable: true }).configurable) { @@ -109,27 +108,20 @@ var Base = new function() { } define(dest, name, res); } - if (generics && isFunc && (!preserve || !generics[name])) { - generics[name] = function(bind) { - return bind && dest[name].apply(bind, - slice.call(arguments, 1)); - }; - } } if (src) { - beans = []; + beans = {}; for (var name in src) if (src.hasOwnProperty(name) && !hidden.test(name)) - field(name, null, true, generics); + field(name, null, true); field('toString'); field('valueOf'); - for (var i = 0, l = beans.length; i < l; i++) { - var bean = beans[i], - part = bean[1]; - field(bean[0], { - get: dest['get' + part] || dest['is' + part], - set: dest['set' + part] - }, true); + for (var name in beans) { + var part = beans[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (get.length === 0 || set)) + field(name, { get: get, set: set }, true); } } return dest; @@ -151,10 +143,6 @@ var Base = new function() { return dest; } - function clone(obj) { - return copy(new obj.constructor(), obj); - } - return inject(function Base() { for (var i = 0, l = arguments.length; i < l; i++) copy(this, arguments[i]); @@ -166,7 +154,7 @@ var Base = new function() { statics = src.statics === true ? src : src.statics; if (statics != src) inject(proto, src, src.enumerable, base && base.prototype, - src.preserve, src.generics && this); + src.preserve); inject(this, statics, true, base, src.preserve); } for (var i = 1, l = arguments.length; i < l; i++) @@ -184,6 +172,7 @@ var Base = new function() { base.apply(this, arguments); }; ctor.prototype = create(this.prototype); + ctor.base = base; define(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true }); inject(ctor, this, true); @@ -240,8 +229,6 @@ if (typeof module !== 'undefined') module.exports = Base; Base.inject({ - generics: true, - toString: function() { return this._id != null ? (this._class || 'Object') + (this._name @@ -282,7 +269,9 @@ Base.inject({ statics: { - exports: {}, + exports: { + enumerable: true + }, extend: function extend() { var res = extend.base.apply(this, arguments), @@ -319,7 +308,8 @@ Base.inject({ if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1)) return false; for (var i in obj1) { - if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i])) + if (obj1.hasOwnProperty(i) + && !Base.equals(obj1[i], obj2[i])) return false; } return true; @@ -327,58 +317,57 @@ Base.inject({ return false; }, - read: function(list, start, length, options) { + read: function(list, start, options, length) { if (this === Base) { var value = this.peek(list, start); - list._index++; - list.__read = 1; + list.__index++; return value; } var proto = this.prototype, readIndex = proto._readIndex, - index = start || readIndex && list._index || 0; + 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; + list.__index = index + 1; return obj && options && options.clone ? obj.clone() : obj; } obj = Base.create(this.prototype); if (readIndex) obj.__read = true; - if (options) - obj.__options = options; 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; - list.__read = obj.__read; - delete obj.__read; - if (options) - delete obj.__options; + list.__index = index + obj.__read; + obj.__read = undefined; } return obj; }, peek: function(list, start) { - return list[list._index = start || list._index || 0]; + 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; + 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, 0, options) - : this.read(list, i, 1, options)); + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); } return res; }, - readNamed: function(list, name, start, length, options) { + readNamed: function(list, name, start, options, length) { var value = this.getNamed(list, name), hasObject = value !== undefined; if (hasObject) { @@ -389,7 +378,7 @@ Base.inject({ } filtered[name] = undefined; } - return this.read(hasObject ? [value] : list, start, length, options); + return this.read(hasObject ? [value] : list, start, options, length); }, getNamed: function(list, name) { @@ -464,7 +453,8 @@ Base.inject({ }, deserialize: function(json, create, _data) { - var res = json; + var res = json, + isRoot = !_data; _data = _data || {}; if (Array.isArray(json)) { var type = json[0], @@ -482,7 +472,7 @@ Base.inject({ } else if (type) { var args = res; if (create) { - res = create(type, args); + res = create(type, args, isRoot); } else { res = Base.create(type.prototype); type.apply(res, args); @@ -497,18 +487,21 @@ Base.inject({ }, exportJSON: function(obj, options) { - return JSON.stringify(Base.serialize(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) { + function(type, args, isRoot) { var obj = target && target.constructor === type ? target : Base.create(type.prototype), isTarget = obj === target; - if (args.length === 1 && obj instanceof Item + if (!isRoot && args.length === 1 && obj instanceof Item && (!(obj instanceof Layer) || isTarget)) { var arg = args[0]; if (Base.isPlainObject(arg)) @@ -538,7 +531,7 @@ Base.inject({ args.push.apply(args, items); var removed = list.splice.apply(list, args); for (var i = 0, l = removed.length; i < l; i++) - delete removed[i]._index; + removed[i]._index = undefined; for (var i = index + amount, l = list.length; i < l; i++) list[i]._index = i; return removed; @@ -617,27 +610,14 @@ var Callback = { if (!handlers) return false; var args = [].slice.call(arguments, 1), - PaperScript = paper.PaperScript, - handleException = PaperScript && PaperScript.handleException, that = this; - - function callHandlers() { - for (var i in handlers) { - if (handlers[i].apply(that, args) === false - && event && event.stop) - event.stop(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(that, args) === false + && event && event.stop) { + event.stop(); + break; } } - - if (handleException) { - try { - callHandlers(); - } catch (e) { - handleException(e); - } - } else { - callHandlers(); - } return true; }, @@ -649,6 +629,19 @@ var Callback = { off: '#detach', trigger: '#fire', + _installEvents: function(install) { + var handlers = this._handlers, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var entry = this._eventTypes[type], + func = entry[key]; + if (func) + func.call(this, type); + } + } + }, + statics: { inject: function inject() { for (var i = 0, l = arguments.length; i < l; i++) { @@ -667,11 +660,11 @@ var Callback = { return this[name]; }; src['set' + part] = function(func) { - if (func) { + var prev = this[name]; + if (prev) + this.detach(type, prev); + if (func) this.attach(type, func); - } else if (this[name]) { - this.detach(type, this[name]); - } this[name] = func; }; }); @@ -689,6 +682,11 @@ var PaperScope = Base.extend({ initialize: function PaperScope(script) { paper = this; + this.settings = { + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }; this.project = null; this.projects = []; this.tools = []; @@ -708,26 +706,19 @@ var PaperScope = Base.extend({ } }, - version: '0.9.15', + version: '0.9.16', getView: function() { return this.project && this.project.view; }, - getTool: function() { - if (!this._tool) - this._tool = new Tool(); - return this._tool; - }, - getPaper: function() { return this; }, - evaluate: function(code) { - var res = paper.PaperScript.evaluate(code, this); + execute: function(code) { + paper.PaperScript.execute(code, this); View.updateFocus(); - return res; }, install: function(scope) { @@ -740,10 +731,9 @@ var PaperScope = Base.extend({ } }); }); - for (var key in this) { - if (!/^(version|_id)/.test(key)) + for (var key in this) + if (!/^_/.test(key) && this[key]) scope[key] = this[key]; - } }, setup: function(canvas) { @@ -783,7 +773,7 @@ var PaperScope = Base.extend({ _id: 0, get: function(id) { - if (typeof id === 'object') + if (id && id.getAttribute) id = id.getAttribute('id'); return this._scopes[id] || null; }, @@ -898,15 +888,29 @@ var Numerical = new function() { sqrt = Math.sqrt, pow = Math.pow, cos = Math.cos, - PI = Math.PI; + PI = Math.PI, + TOLERANCE = 10e-6, + EPSILON = 10e-12; + + function setupRoots(roots, min, max) { + var unbound = min === undefined, + minE = min - EPSILON, + maxE = max + EPSILON, + count = 0; + return function(root) { + if (unbound || root > minE && root < maxE) + roots[count++] = root < min ? min : root > max ? max : root; + return count; + }; + } return { - TOLERANCE: 10e-6, - EPSILON: 10e-12, + TOLERANCE: TOLERANCE, + EPSILON: EPSILON, KAPPA: 4 * (sqrt(2) - 1) / 3, isZero: function(val) { - return abs(val) <= Numerical.EPSILON; + return abs(val) <= EPSILON; }, integrate: function(f, a, b, n) { @@ -927,10 +931,10 @@ var Numerical = new function() { findRoot: function(f, df, x, a, b, n, tolerance) { for (var i = 0; i < n; i++) { var fx = f(x), - dx = fx / df(x); + dx = fx / df(x), + nx = x - dx; if (abs(dx) < tolerance) - return x; - var nx = x - dx; + return nx; if (fx > 0) { b = x; x = nx <= a ? 0.5 * (a + b) : nx; @@ -939,65 +943,45 @@ var Numerical = new function() { x = nx >= b ? 0.5 * (a + b) : nx; } } + return x; }, solveQuadratic: function(a, b, c, roots, min, max) { - var epsilon = Numerical.EPSILON, - unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; + var add = setupRoots(roots, min, max); - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - - if (abs(a) < epsilon) { - if (abs(b) >= epsilon) + if (abs(a) < EPSILON) { + if (abs(b) >= EPSILON) return add(-c / b); - return abs(c) < epsilon ? -1 : 0; + return abs(c) < EPSILON ? -1 : 0; } var p = b / (2 * a); var q = c / a; var p2 = p * p; - if (p2 < q - epsilon) + if (p2 < q - EPSILON) return 0; - var s = p2 > q ? sqrt(p2 - q) : 0; - add (s - p); + var s = p2 > q ? sqrt(p2 - q) : 0, + count = add(s - p); if (s > 0) - add(-s - p); + count = add(-s - p); return count; }, solveCubic: function(a, b, c, d, roots, min, max) { - var epsilon = Numerical.EPSILON; - if (abs(a) < epsilon) + if (abs(a) < EPSILON) return Numerical.solveQuadratic(b, c, d, roots, min, max); - var unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; - - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - b /= a; c /= a; d /= a; - var bb = b * b, + var add = setupRoots(roots, min, max), + bb = b * b, p = (bb - 3 * c) / 9, q = (2 * bb * b - 9 * b * c + 27 * d) / 54, ppp = p * p * p, D = q * q - ppp; b /= 3; - if (abs(D) < epsilon) { - if (abs(q) < epsilon) + if (abs(D) < EPSILON) { + if (abs(q) < EPSILON) return add(-b); var sqp = sqrt(p), snq = q > 0 ? 1 : -1; @@ -1066,10 +1050,11 @@ var Point = Base.extend({ }, equals: function(point) { - return point === this || point && (this.x === point.x - && this.y === point.y - || Array.isArray(point) && this.x === point[0] - && this.y === point[1]) || false; + 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() { @@ -1086,28 +1071,28 @@ var Point = Base.extend({ return [f.number(this.x), f.number(this.y)]; }, - add: function(point) { - point = Point.read(arguments); + add: function() { + var point = Point.read(arguments); return new Point(this.x + point.x, this.y + point.y); }, - subtract: function(point) { - point = Point.read(arguments); + subtract: function() { + var point = Point.read(arguments); return new Point(this.x - point.x, this.y - point.y); }, - multiply: function(point) { - point = Point.read(arguments); + multiply: function() { + var point = Point.read(arguments); return new Point(this.x * point.x, this.y * point.y); }, - divide: function(point) { - point = Point.read(arguments); + divide: function() { + var point = Point.read(arguments); return new Point(this.x / point.x, this.y / point.y); }, - modulo: function(point) { - point = Point.read(arguments); + modulo: function() { + var point = Point.read(arguments); return new Point(this.x % point.x, this.y % point.y); }, @@ -1119,17 +1104,17 @@ var Point = Base.extend({ return matrix ? matrix._transformPoint(this) : this; }, - getDistance: function(point, squared) { - point = Point.read(arguments); - var x = point.x - this.x, + getDistance: function(_point, squared) { + 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); }, getLength: function() { - var length = this.x * this.x + this.y * this.y; - return arguments.length && arguments[0] ? length : Math.sqrt(length); + return Math.sqrt(this.x * this.x + this.y * this.y); }, setLength: function(length) { @@ -1148,7 +1133,6 @@ var Point = Base.extend({ this.y * scale ); } - return this; }, normalize: function(length) { @@ -1157,28 +1141,24 @@ var Point = Base.extend({ var current = this.getLength(), scale = current !== 0 ? length / current : 0, point = new Point(this.x * scale, this.y * scale); - point._angle = this._angle; + if (scale >= 0) + point._angle = this._angle; return point; }, getAngle: function() { - return this.getAngleInRadians(arguments[0]) * 180 / Math.PI; + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; }, setAngle: function(angle) { - angle = this._angle = angle * Math.PI / 180; - if (!this.isZero()) { - var length = this.getLength(); - this.set( - Math.cos(angle) * length, - Math.sin(angle) * length - ); - } - return this; + this.setAngleInRadians.call(this, angle * Math.PI / 180); }, + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + getAngleInRadians: function() { - if (arguments[0] === undefined) { + if (!arguments.length) { return this.isZero() ? this._angle || 0 : this._angle = Math.atan2(this.y, this.x); @@ -1193,15 +1173,23 @@ var Point = Base.extend({ } }, - getAngleInDegrees: function() { - return this.getAngle(arguments[0]); + 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; }, - getDirectedAngle: function(point) { + getDirectedAngle: function(_point) { + var point = _point; point = Point.read(arguments); return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; }, @@ -1215,7 +1203,7 @@ var Point = Base.extend({ c = Math.cos(angle); point = new Point( point.x * c - point.y * s, - point.y * c + point.x * s + point.x * s + point.y * c ); return center ? point.add(center) : point; }, @@ -1229,11 +1217,11 @@ var Point = Base.extend({ }, isColinear: function(point) { - return this.cross(point) < 0.00001; + return Math.abs(this.cross(point)) < 0.00001; }, isOrthogonal: function(point) { - return this.dot(point) < 0.00001; + return Math.abs(this.dot(point)) < 0.00001; }, isZero: function() { @@ -1244,18 +1232,18 @@ var Point = Base.extend({ return isNaN(this.x) || isNaN(this.y); }, - dot: function(point) { - point = Point.read(arguments); + dot: function() { + var point = Point.read(arguments); return this.x * point.x + this.y * point.y; }, - cross: function(point) { - point = Point.read(arguments); + cross: function() { + var point = Point.read(arguments); return this.x * point.y - this.y * point.x; }, - project: function(point) { - point = Point.read(arguments); + project: function() { + var point = Point.read(arguments); if (point.isZero()) { return new Point(0, 0); } else { @@ -1397,28 +1385,28 @@ var Size = Base.extend({ f.number(this.height)]; }, - add: function(size) { - size = Size.read(arguments); + add: function() { + var size = Size.read(arguments); return new Size(this.width + size.width, this.height + size.height); }, - subtract: function(size) { - size = Size.read(arguments); + subtract: function() { + var size = Size.read(arguments); return new Size(this.width - size.width, this.height - size.height); }, - multiply: function(size) { - size = Size.read(arguments); + multiply: function() { + var size = Size.read(arguments); return new Size(this.width * size.width, this.height * size.height); }, - divide: function(size) { - size = Size.read(arguments); + divide: function() { + var size = Size.read(arguments); return new Size(this.width / size.width, this.height / size.height); }, - modulo: function(size) { - size = Size.read(arguments); + modulo: function() { + var size = Size.read(arguments); return new Size(this.width % size.width, this.height % size.height); }, @@ -1550,7 +1538,7 @@ var Rectangle = Base.extend({ this.width = size.width; this.height = size.height; } - read = arguments._index; + read = arguments.__index; } if (this.__read) this.__read = read; @@ -1569,11 +1557,12 @@ var Rectangle = Base.extend({ }, equals: function(rect) { - if (Base.isPlainValue(rect)) - rect = Rectangle.read(arguments); - return rect === this - || rect && this.x === rect.x && this.y === rect.y - && this.width === rect.width && this.height === rect.height + 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; }, @@ -1594,24 +1583,24 @@ var Rectangle = Base.extend({ f.number(this.height)]; }, - getPoint: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.x, this.y, this, 'setPoint'); + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.x = point.x; this.y = point.y; }, - getSize: function() { - return new (arguments[0] ? Size : LinkedSize) - (this.width, this.height, this, 'setSize'); + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); }, - setSize: function(size) { - size = Size.read(arguments); + setSize: function() { + var size = Size.read(arguments); if (this._fixX) this.x += (this.width - size.width) * this._fixX; if (this._fixY) @@ -1690,20 +1679,24 @@ var Rectangle = Base.extend({ this._fixY = 0.5; }, - getCenter: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.getCenterX(), this.getCenterY(), this, 'setCenter'); + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, - setCenter: function(point) { - point = Point.read(arguments); + 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; + return this.width === 0 || this.height === 0; }, contains: function(arg) { @@ -1729,42 +1722,42 @@ var Rectangle = Base.extend({ && y + rect.height <= this.y + this.height; }, - intersects: function(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.max(this.x, rect.x), + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.min(this.x, rect.x), + 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(point) { - point = Point.read(arguments); + 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), @@ -1772,9 +1765,10 @@ var Rectangle = Base.extend({ return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, - expand: function(hor, ver) { - if (ver === undefined) - ver = hor; + 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); }, @@ -1803,12 +1797,12 @@ var Rectangle = Base.extend({ setY = 'set' + y, get = 'get' + part, set = 'set' + part; - this[get] = function() { - return new (arguments[0] ? Point : LinkedPoint) - (this[getX](), this[getY](), this, set); + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); }; - this[set] = function(point) { - point = Point.read(arguments); + this[set] = function() { + var point = Point.read(arguments); this[setX](point.x); this[setY](point.y); }; @@ -1855,7 +1849,7 @@ var LinkedRectangle = Rectangle.extend({ this[name] = function() { this._dontNotify = true; proto[name].apply(this, arguments); - delete this._dontNotify; + this._dontNotify = false; this._owner[this._setter](this); }; }, { @@ -1916,8 +1910,14 @@ var Matrix = Base.extend({ }, _changed: function() { - if (this._owner) - this._owner._changed(5); + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(5); + } + } }, clone: function() { @@ -1940,16 +1940,36 @@ var Matrix = Base.extend({ f.number(this._ty)].join(', ') + ']]'; }, - reset: function() { + 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() { + var owner = this._owner; + if (owner) { + owner.transform(null, true); + 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, 0, { readNull: true }); + center = Point.read(arguments, 0, { readNull: true }); if (center) this.translate(center); this._a *= scale.x; @@ -1962,20 +1982,10 @@ var Matrix = Base.extend({ return this; }, - translate: function(point) { - point = Point.read(arguments); - var 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; - }, - - rotate: function(angle, center) { - center = Point.read(arguments, 1); - angle = angle * Math.PI / 180; - var x = center.x, + 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), @@ -1996,22 +2006,31 @@ var Matrix = Base.extend({ }, shear: function() { - var point = Point.read(arguments), - center = Point.read(arguments, 0, 0, { readNull: true }); + 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 += point.y * this._b; - this._c += point.y * this._d; - this._b += point.x * a; - this._d += point.x * 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 a = this._a, b = this._b, @@ -2049,6 +2068,10 @@ var Matrix = Base.extend({ && this._tx === 0 && this._ty === 0; }, + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + isInvertible: function() { return !!this._getDeterminant(); }, @@ -2167,7 +2190,6 @@ var Matrix = Base.extend({ } return { - translation: this.getTranslation(), scaling: new Point(scaleX, scaleY), rotation: -Math.atan2(b, a) * 180 / Math.PI, shearing: shear @@ -2182,36 +2204,14 @@ var Matrix = Base.extend({ return new Point(this._tx, this._ty); }, - setTranslation: function() { - var point = Point.read(arguments); - this._tx = point.x; - this._ty = point.y; - this._changed(); - }, - getScaling: function() { return (this.decompose() || {}).scaling; }, - setScaling: function() { - var scaling = this.getScaling(); - if (scaling != null) { - var scale = Point.read(arguments); - (this._owner || this).scale( - scale.x / scaling.x, scale.y / scaling.y); - } - }, - getRotation: function() { return (this.decompose() || {}).rotation; }, - setRotation: function(angle) { - var rotation = this.getRotation(); - if (rotation != null) - (this._owner || this).rotate(angle - rotation); - }, - inverted: function() { var det = this._getDeterminant(); return det && new Matrix( @@ -2231,19 +2231,13 @@ var Matrix = Base.extend({ ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); } }, new function() { - return Base.each({ - scaleX: '_a', - scaleY: '_d', - translateX: '_tx', - translateY: '_ty', - shearX: '_b', - shearY: '_c' - }, function(prop, name) { - name = Base.capitalize(name); - this['get' + name] = function() { + return 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' + name] = function(value) { + this['set' + part] = function(value) { this[prop] = value; this._changed(); }; @@ -2376,8 +2370,7 @@ var Project = PaperScopeItem.extend({ this.view = view instanceof View ? view : View.create(view); this._selectedItems = {}; this._selectedItemCount = 0; - this._drawCount = 0; - this.options = {}; + this._updateVersion = 0; }, _serialize: function(options, dictionary) { @@ -2422,7 +2415,7 @@ var Project = PaperScopeItem.extend({ this.activeLayer = child; } else if (child instanceof Item) { (this.activeLayer - || this.addChild(new Layer({ insert: false }))).addChild(child); + || this.addChild(new Layer(Item.NO_INSERT))).addChild(child); } else { child = null; } @@ -2439,6 +2432,10 @@ var Project = PaperScopeItem.extend({ return items; }, + getOptions: function() { + return this._scope.settings; + }, + _updateSelection: function(item) { var id = item._id, selectedItems = this._selectedItems; @@ -2465,9 +2462,9 @@ var Project = PaperScopeItem.extend({ selectedItems[i].setFullySelected(false); }, - hitTest: function(point, options) { - point = Point.read(arguments); - options = HitResult.getOptions(Base.read(arguments)); + 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; @@ -2505,14 +2502,13 @@ var Project = PaperScopeItem.extend({ return Base.importJSON(json, layer && layer.isEmpty() && layer); }, - draw: function(ctx, matrix, ratio) { - this._drawCount++; + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; ctx.save(); matrix.applyToContext(ctx); var param = new Base({ offset: new Point(0, 0), - ratio: ratio, - transforms: [matrix], + pixelRatio: pixelRatio, trackTransforms: true }); for (var i = 0, l = this.layers.length; i < l; i++) @@ -2523,30 +2519,30 @@ var Project = PaperScopeItem.extend({ ctx.save(); ctx.strokeWidth = 1; for (var id in this._selectedItems) { - var item = this._selectedItems[id]; - if (item._drawCount === this._drawCount + var item = this._selectedItems[id], + size = this._scope.settings.handleSize; + half = size / 2; + if (item._updateVersion === this._updateVersion && (item._drawSelected || item._boundsSelected)) { var color = item.getSelectedColor() || item.getLayer().getSelectedColor(); ctx.strokeStyle = ctx.fillStyle = color ? color.toCanvasStyle(ctx) : '#009dec'; - var mx = item._globalMatrix; + var mx = matrix.clone().concatenate(item._globalMatrix); if (item._drawSelected) item._drawSelected(ctx, mx); if (item._boundsSelected) { var coords = mx._transformCorners( - item._getBounds('getBounds')); + item.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.beginPath(); - ctx.rect(coords[i] - 2, coords[++i] - 2, 4, 4); - ctx.fill(); - } + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); } } } @@ -2564,7 +2560,6 @@ var Symbol = Base.extend({ this.project.symbols.push(this); if (item) this.setDefinition(item, dontCenter); - this._instances = {}; }, _serialize: function(options, dictionary) { @@ -2575,24 +2570,27 @@ var Symbol = Base.extend({ }, _changed: function(flags) { - Base.each(this._instances, function(item) { - item._changed(flags); - }); + if (flags & 4) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } }, getDefinition: function() { return this._definition; }, - setDefinition: function(item ) { + setDefinition: function(item, _dontCenter) { if (item._parentSymbol) item = item.clone(); if (this._definition) - delete this._definition._parentSymbol; + this._definition._parentSymbol = null; this._definition = item; item.remove(); item.setSelected(false); - if (!arguments[1]) + if (!_dontCenter) item.setPosition(new Point()); item._parentSymbol = this; this._changed(5); @@ -2619,15 +2617,20 @@ var Item = Base.extend(Callback, { if (name) proto._type = Base.hyphenate(name); return res; - } + }, + + NO_INSERT: { insert: false } }, _class: 'Item', - _transformContent: true, + _applyMatrix: true, + _canApplyMatrix: true, _boundsSelected: false, + _selectChildren: false, _serializeFields: { name: null, matrix: new Matrix(), + pivot: null, locked: false, visible: true, blendMode: 'normal', @@ -2635,6 +2638,7 @@ var Item = Base.extend(Callback, { guide: false, selected: false, clipMask: false, + applyMatrix: null, data: {} }, @@ -2642,21 +2646,26 @@ var Item = Base.extend(Callback, { }, _initialize: function(props, point) { - this._id = Item._id = (Item._id || 0) + 1; + var internal = props && props.internal === true, + matrix = this._matrix = new Matrix(); + project = paper.project; + if (!internal) + this._id = Item._id = (Item._id || 0) + 1; + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this); if (!this._project) { - var project = paper.project; - if (props && props.insert === false) { + if (internal || props && props.insert === false) { this._setProject(project); } else { (project.activeLayer || new Layer()).addChild(this); } } - this._style = new Style(this._project._currentStyle, this); - var matrix = this._matrix = new Matrix(); - if (point) - matrix.translate(point); - matrix._owner = this; - return props ? this._set(props, { insert: true }) : true; + return props && props !== Item.NO_INSERT + ? this._set(props, { insert: true }) + : true; }, _events: new function() { @@ -2746,24 +2755,23 @@ var Item = Base.extend(Callback, { }, _changed: function(flags) { - var parent = this._parent, - project = this._project, - symbol = this._parentSymbol; - this._drawCount = null; + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; if (flags & 4) { - delete this._bounds; - delete this._position; + this._bounds = this._position = this._decomposed = + this._globalMatrix = undefined; } - if (parent && (flags + if (cacheParent && (flags & (4 | 8))) { - parent._clearBoundsCache(); + Item._clearBoundsCache(cacheParent); } if (flags & 2) { - this._clearBoundsCache(); + Item._clearBoundsCache(this); } if (project) { if (flags & 1) { - project._needsRedraw = true; + project._needsUpdate = true; } if (project._changes) { var entry = project._changesById[this._id]; @@ -2782,7 +2790,7 @@ var Item = Base.extend(Callback, { set: function(props) { if (props) - this._set(props); + this._set(props, { insert: true }); return this; }, @@ -2865,7 +2873,7 @@ var Item = Base.extend(Callback, { _guide: false, isSelected: function() { - if (this._children) { + if (this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) if (this._children[i].isSelected()) return true; @@ -2873,12 +2881,12 @@ var Item = Base.extend(Callback, { return this._selected; }, - setSelected: function(selected ) { - if (this._children && !arguments[1]) { + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) this._children[i].setSelected(selected); } - if ((selected = !!selected) != this._selected) { + if ((selected = !!selected) ^ this._selected) { this._selected = selected; this._project._updateSelection(this); this._changed(33); @@ -2934,71 +2942,63 @@ var Item = Base.extend(Callback, { this._data = data; }, - getPosition: function() { - var pos = this._position - || (this._position = this.getBounds().getCenter(true)); - return new (arguments[0] ? Point : LinkedPoint) - (pos.x, pos.y, this, 'setPosition'); + 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))); - } -}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], - function(name) { - this[name] = function() { - var getter = this._boundsGetter, - bounds = this._getCachedBounds(typeof getter == 'string' - ? getter : getter && getter[name] || name, arguments[0]); - return name === 'getBounds' + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments); + this._position = undefined; + }, + + _pivot: null, + + getRegistration: '#getPivot', + setRegistration: '#setPivot' +}, 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, null, + internalGetter); + return key === 'bounds' ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, this, 'setBounds') : bounds; }; + this[key] = { + get: this[getter] + }; }, { - _getCachedBounds: function(getter, matrix, cacheItem) { - var cache = (!matrix || matrix.equals(this._matrix)) && getter; - if (cacheItem && this._parent) { - var id = cacheItem._id, - ref = this._parent._boundsCache - = this._parent._boundsCache || { - ids: {}, - list: [] - }; - if (!ref.ids[id]) { - ref.list.push(cacheItem); - ref.ids[id] = cacheItem; - } - } - if (cache && this._bounds && this._bounds[cache]) - return this._bounds[cache].clone(); - var identity = this._matrix.isIdentity(); - matrix = !matrix || matrix.isIdentity() - ? identity ? null : this._matrix - : identity ? matrix : matrix.clone().concatenate(this._matrix); - var bounds = this._getBounds(getter, matrix, cache ? this : cacheItem); - if (cache) { - if (!this._bounds) - this._bounds = {}; - this._bounds[cache] = bounds.clone(); - } - return bounds; - }, - - _clearBoundsCache: function() { - if (this._boundsCache) { - for (var i = 0, list = this._boundsCache.list, l = list.length; - i < l; i++) { - var item = list[i]; - delete item._bounds; - if (item != this && item._boundsCache) - item._clearBoundsCache(); - } - delete this._boundsCache; - } - }, - _getBounds: function(getter, matrix, cacheItem) { var children = this._children; if (!children || children.length == 0) @@ -3022,9 +3022,9 @@ var Item = Base.extend(Callback, { : new Rectangle(); }, - setBounds: function(rect) { - rect = Rectangle.read(arguments); - var bounds = this.getBounds(), + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), matrix = new Matrix(), center = rect.getCenter(); matrix.translate(center); @@ -3036,53 +3036,155 @@ var Item = Base.extend(Callback, { 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; + var cacheParent = this._parent || this._parentSymbol; + if (cacheItem && cacheParent) { + var id = cacheItem._id, + ref = cacheParent._boundsCache = cacheParent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(cacheItem); + ref.ids[id] = cacheItem; + } + } + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + matrix = !matrix + ? _matrix + : _matrix + ? matrix.clone().concatenate(_matrix) + : matrix; + var bounds = this._getBounds(internalGetter || getter, matrix, + cache ? this : cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _clearBoundsCache: function(item) { + if (item._boundsCache) { + for (var i = 0, list = item._boundsCache.list, l = list.length; + i < l; i++) { + var child = list[i]; + child._bounds = child._position = undefined; + if (child !== item && child._boundsCache) + child._clearBoundsCache(); + } + item._boundsCache = undefined; + } + } } }), { + _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() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.scaling; + }, + + setScaling: function() { + var current = this.getScaling(); + if (current != null) { + 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(matrix) { this._matrix.initialize(matrix); - if (this._transformContent) - this.applyMatrix(true); - this._changed(5); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(5); + } }, getGlobalMatrix: function() { - return this._drawCount === this._project._drawCount - && this._globalMatrix || null; + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + if (this._parent) + matrix.preConcatenate(this._parent.getGlobalMatrix()); + matrix._updateVersion = updateVersion; + } + return matrix; }, - getTransformContent: function() { - return this._transformContent; + getApplyMatrix: function() { + return this._applyMatrix; }, - setTransformContent: function(transform) { - this._transformContent = transform; - if (transform) - this.applyMatrix(); + setApplyMatrix: function(transform) { + if (this._applyMatrix = this._canApplyMatrix && !!transform) + this.transform(null, true); }, + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', + getProject: function() { return this._project; }, - _setProject: function(project) { - if (this._project != project) { - var hasOnFrame = this.responds('frame'); - if (hasOnFrame) - this._animateItem(false); + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); this._project = project; - if (hasOnFrame) - this._animateItem(true); - if (this._children) { - for (var i = 0, l = this._children.length; i < l; i++) { - this._children[i]._setProject(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); + }, + + _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() { @@ -3155,7 +3257,7 @@ var Item = Base.extend(Callback, { }, clone: function(insert) { - return this._clone(new this.constructor({ insert: false }), insert); + return this._clone(new this.constructor(Item.NO_INSERT), insert); }, _clone: function(copy, insert) { @@ -3167,7 +3269,7 @@ var Item = Base.extend(Callback, { if (insert || insert === undefined) copy.insertAbove(this); var keys = ['_locked', '_visible', '_blendMode', '_opacity', - '_clipMask', '_guide']; + '_clipMask', '_guide', '_applyMatrix']; for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; if (this.hasOwnProperty(key)) @@ -3187,22 +3289,22 @@ var Item = Base.extend(Callback, { rasterize: function(resolution) { var bounds = this.getStrokeBounds(), - scale = (resolution || 72) / 72, + view = this._project.view, + scale = (resolution || view && view.getResolution() || 72) / 72, topLeft = bounds.getTopLeft().floor(), bottomRight = bounds.getBottomRight().ceil() size = new Size(bottomRight.subtract(topLeft)), - canvas = CanvasProvider.getCanvas(size), + 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({ transforms: [matrix] })); ctx.restore(); - var raster = new Raster({ - canvas: canvas, - insert: false - }); - raster.setPosition(topLeft.add(size.divide(2))); + 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; }, @@ -3220,33 +3322,47 @@ var Item = Base.extend(Callback, { } return false; } - return point.isInside(this._getBounds('getBounds')); + return point.isInside(this.getInternalBounds()); }, hitTest: function(point, options) { point = Point.read(arguments); options = HitResult.getOptions(Base.read(arguments)); - if (this._locked || !this._visible || this._guide && !options.guides) return null; - if (!this._children && !this.getRoughBounds() - .expand(2 * options.tolerance)._containsPoint(point)) - return null; - point = this._matrix._inverseTransform(point); + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this._project.view, + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.clone().concatenate(matrix) + : this.getGlobalMatrix().clone().preConcatenate( + view ? view._matrix : new Matrix()), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 0.00001) + ); + point = matrix._inverseTransform(point); - var that = this, + 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 && this._type !== options.type), + that = this, res; + function checkBounds(type, part) { var pt = bounds['get' + part](); - if (point.getDistance(pt) < options.tolerance) + if (point.subtract(pt).divide(tolerancePadding).length <= 1) return new HitResult(type, that, { name: Base.hyphenate(part), point: pt }); } - if ((options.center || options.bounds) && - !(this instanceof Layer && !this._parent)) { - var bounds = this._getBounds('getBounds'); + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); if (options.center) res = checkBounds('center', 'Center'); if (!res && options.bounds) { @@ -3259,24 +3375,27 @@ var Item = Base.extend(Callback, { } } - if ((res || (res = this._children || !(options.guides && !this._guide - || options.selected && !this._selected) - ? this._hitTest(point, options) : null)) - && res.point) { - res.point = that._matrix.transform(res.point); + 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._hitTest(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; return res; }, + _getChildHitTestOptions: function(options) { + return options; + }, + _hitTest: function(point, options) { - var children = this._children; - if (children) { - for (var i = children.length - 1, res; i >= 0; i--) - if (res = children[i].hitTest(point, options)) - return res; - } else if (options.fill && this.hasFill() && this._contains(point)) { + if (options.fill && this.hasFill() && this._contains(point)) return new HitResult('fill', this); - } }, matches: function(match) { @@ -3375,16 +3494,17 @@ var Item = Base.extend(Callback, { items = Array.prototype.slice.apply(items); for (var i = items.length - 1; i >= 0; i--) { var item = items[i]; - if (_type && item._type !== _type) + if (_type && item._type !== _type) { items.splice(i, 1); - else + } else { item._remove(true); + } } Base.splice(children, items, index, 0); for (var i = 0, l = items.length; i < l; i++) { var item = items[i]; item._parent = this; - item._setProject(this._project); + item._setProject(this._project, true); if (item._name) item.setName(item._name); } @@ -3432,7 +3552,7 @@ var Item = Base.extend(Callback, { reduce: function() { if (this._children && this._children.length === 1) { - var child = this._children[0]; + var child = this._children[0].reduce(); child.insertAbove(this); child.setStyle(this._style); this.remove(); @@ -3465,8 +3585,7 @@ var Item = Base.extend(Callback, { this._removeNamed(); if (this._index != null) Base.splice(this._parent._children, null, this._index, 1); - if (this.responds('frame')) - this._animateItem(false); + this._installEvents(false); if (notify) this._parent._changed(7); this._parent = null; @@ -3580,90 +3699,87 @@ var Item = Base.extend(Callback, { return false; }, - scale: function(hor, ver , center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; - } - return this.transform(new Matrix().scale(hor, ver, - center || this.getPosition(true))); - }, - translate: function() { var mx = new Matrix(); return this.transform(mx.translate.apply(mx, arguments)); }, - rotate: function(angle, center) { + 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))); - }, + }; +}, { - shear: function(hor, ver, center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; +}), { + transform: function(matrix, _applyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && (!_matrix.isIdentity() || matrix); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + pivot.transform(_matrix); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); } - return this.transform(new Matrix().shear(hor, ver, - center || this.getPosition(true))); - }, - - transform: function(matrix ) { var bounds = this._bounds, position = this._position; - this._matrix.preConcatenate(matrix); - if (this._transformContent || arguments[1]) - this.applyMatrix(true); this._changed(5); - if (bounds && matrix.getRotation() % 90 === 0) { + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { for (var key in bounds) { var rect = bounds[key]; - matrix._transformBounds(rect, rect); + 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 (position) { + } else if (matrix && position) { this._position = matrix._transformPoint(position, position); } return this; }, - _applyMatrix: function(matrix, applyMatrix) { + _transformContent: function(matrix) { var children = this._children; - if (children && children.length > 0) { + if (children) { for (var i = 0, l = children.length; i < l; i++) - children[i].transform(matrix, applyMatrix); + children[i].transform(matrix, true); return true; } }, - applyMatrix: function(_dontNotify) { - var matrix = this._matrix; - if (this._applyMatrix(matrix, true)) { - var style = this._style, - fillColor = style.getFillColor(true), - strokeColor = style.getStrokeColor(true); - if (fillColor) - fillColor.transform(matrix); - if (strokeColor) - strokeColor.transform(matrix); - matrix.reset(); - } - if (!_dontNotify) - this._changed(5); - }, - globalToLocal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._transformPoint(Point.read(arguments)); + return matrix && matrix._inverseTransform(Point.read(arguments)); }, localToGlobal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._inverseTransform(Point.read(arguments)); + return matrix && matrix._transformPoint(Point.read(arguments)); }, fitBounds: function(rectangle, fill) { @@ -3731,13 +3847,21 @@ var Item = Base.extend(Callback, { draw: function(ctx, param) { if (!this._visible || this._opacity === 0) return; - this._drawCount = this._project._drawCount; + var updateVersion = this._updateVersion = this._project._updateVersion; var trackTransforms = param.trackTransforms, - transforms = param.transforms, + transforms = param.transforms = param.transforms || [new Matrix()], + matrix = this._matrix, parentMatrix = transforms[transforms.length - 1], - globalMatrix = parentMatrix.clone().concatenate(this._matrix); - if (trackTransforms) + globalMatrix = parentMatrix.clone().concatenate(matrix); + if (!globalMatrix.isInvertible()) + return; + if (trackTransforms) { + if (!transforms) + transforms = param.transforms = []; transforms.push(this._globalMatrix = globalMatrix); + globalMatrix._updateVersion = updateVersion; + } + var blendMode = this._blendMode, opacity = this._opacity, normalBlend = blendMode === 'normal', @@ -3754,7 +3878,8 @@ var Item = Base.extend(Callback, { itemOffset = param.offset = bounds.getTopLeft().floor(); mainCtx = ctx; ctx = CanvasProvider.getContext( - bounds.getSize().ceil().add(new Size(1, 1)), param.ratio); + bounds.getSize().ceil().add(new Size(1, 1)), + param.pixelRatio); } ctx.save(); if (direct) { @@ -3764,7 +3889,7 @@ var Item = Base.extend(Callback, { } else { ctx.translate(-itemOffset.x, -itemOffset.y); } - (direct ? this._matrix : globalMatrix).applyToContext(ctx); + (direct ? matrix : globalMatrix).applyToContext(ctx); if (!direct && param.clipItem) param.clipItem.draw(ctx, param.extend({ clip: true })); this._draw(ctx, param); @@ -3775,7 +3900,7 @@ var Item = Base.extend(Callback, { ctx.clip(); if (!direct) { BlendMode.process(blendMode, ctx, mainCtx, opacity, - itemOffset.subtract(prevOffset).multiply(param.ratio)); + itemOffset.subtract(prevOffset).multiply(param.pixelRatio)); CanvasProvider.release(ctx); param.offset = prevOffset; } @@ -3808,6 +3933,7 @@ var Item = Base.extend(Callback, { var Group = Item.extend({ _class: 'Group', + _selectChildren: true, _serializeFields: { children: [] }, @@ -3821,12 +3947,8 @@ var Group = Item.extend({ _changed: function _changed(flags) { _changed.base.call(this, flags); - if (flags & 2 && this._transformContent - && !this._matrix.isIdentity()) { - this.applyMatrix(); - } if (flags & (2 | 256)) { - delete this._clipItem; + this._clipItem = undefined; } }, @@ -3888,7 +4010,8 @@ var Layer = Group.extend({ this._project.activeLayer = this.getNextSibling() || this.getPreviousSibling(); Base.splice(this._project.layers, null, this._index, 1); - this._project._needsRedraw = true; + this._installEvents(false); + this._project._needsUpdate = true; return true; } return false; @@ -3917,7 +4040,7 @@ var Layer = Group.extend({ this._remove(true); Base.splice(item._project.layers, [this], item._index + (above ? 1 : 0), 0); - this._setProject(item._project); + this._setProject(item._project, true); return this; } return _insert.base.call(this, above, item, _preserve); @@ -3926,14 +4049,17 @@ var Layer = Group.extend({ var Shape = Item.extend({ _class: 'Shape', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsSelected: true, + _serializeFields: { + shape: null, + size: null, + radius: null + }, - initialize: function Shape(shape, center, size, radius, props) { - this._shape = shape; - this._size = size; - this._radius = radius; - this._initialize(props, center); + initialize: function Shape(props) { + this._initialize(props); }, _equals: function(item) { @@ -3943,26 +4069,33 @@ var Shape = Item.extend({ }, clone: function(insert) { - return this._clone(new Shape(this._shape, this.getPosition(true), - this._size.clone(), - this._radius.clone ? this._radius.clone() : this._radius, - { insert: false }), insert); + var copy = new Shape(Item.NO_INSERT); + copy.setShape(this._shape); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); }, getShape: function() { return this._shape; }, + setShape: function(shape) { + this._shape = shape; + }, + getSize: function() { var size = this._size; return new LinkedSize(size.width, size.height, this, 'setSize'); }, setSize: function() { - var shape = this._shape, - size = Size.read(arguments); - if (!this._size.equals(size)) { - var width = size.width, + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var shape = this._shape, + width = size.width, height = size.height; if (shape === 'rectangle') { var radius = Size.min(this._radius, size.divide(2)); @@ -3995,14 +4128,18 @@ var Shape = Item.extend({ this._size.set(size, size); } else { radius = Size.read(arguments); - if (this._radius.equals(radius)) - return; - this._radius.set(radius.width, radius.height); - if (shape === 'rectangle') { - var size = Size.max(this._size, radius.multiply(2)); - this._size.set(size.width, size.height); - } else if (shape === 'ellipse') { - this._size.set(radius.width * 2, radius.height * 2); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (shape === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (shape === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } } } this._changed(5); @@ -4040,7 +4177,7 @@ var Shape = Item.extend({ } else { var rx = radius.width, ry = radius.height, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (shape === 'ellipse') { var cx = rx * kappa, cy = ry * kappa; @@ -4170,7 +4307,11 @@ new function() { statics: new function() { function createShape(shape, point, size, radius, args) { - return new Shape(shape, point, size, radius, Base.getNamed(args)); + var item = new Shape(Base.getNamed(args)); + item._shape = shape; + item._size = size; + item._radius = radius; + return item.translate(point); } return { @@ -4214,7 +4355,8 @@ statics: new function() { var Raster = Item.extend({ _class: 'Raster', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: 'getBounds', _boundsSelected: true, _serializeFields: { @@ -4239,15 +4381,17 @@ var Raster = Item.extend({ }, clone: function(insert) { - var param = { insert: false }, - image = this._image; + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; if (image) { - param.image = image; - } else if (this._canvas) { - var canvas = param.canvas = CanvasProvider.getCanvas(this._size); - canvas.getContext('2d').drawImage(this._canvas, 0, 0); + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setCanvas(copyCanvas); } - return this._clone(new Raster(param), insert); + return this._clone(copy, insert); }, getSize: function() { @@ -4326,10 +4470,10 @@ var Raster = Item.extend({ setCanvas: '#setImage', - getContext: function() { + getContext: function(modify) { if (!this._context) this._context = this.getCanvas().getContext('2d'); - if (arguments[0]) { + if (modify) { this._image = null; this._changed(129); } @@ -4346,25 +4490,25 @@ var Raster = Item.extend({ setSource: function(src) { var that = this, - image = document.getElementById(src) || new Image(); + image; function loaded() { var view = that._project.view; if (view) paper = view._scope; + that.setImage(image); that.fire('load'); if (view) - view.draw(true); + view.update(); } + image = document.getElementById(src) || new Image(); + if (image.naturalWidth && image.naturalHeight) { setTimeout(loaded, 0); } else { DomEvent.add(image, { - load: function() { - that.setImage(image); - loaded(); - } + load: loaded }); if (!image.src) image.src = src; @@ -4376,20 +4520,18 @@ var Raster = Item.extend({ return this._canvas || this._image; }, - getSubCanvas: function(rect) { - rect = Rectangle.read(arguments); - var ctx = CanvasProvider.getContext(rect.getSize()); + getSubCanvas: function(rect) { + 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(rect) { - rect = Rectangle.read(arguments); - var raster = new Raster({ - canvas: this.getSubCanvas(rect), - insert: false - }); + getSubRaster: function(rect) { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setCanvas(this.getSubCanvas(rect)); raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); raster._matrix.preConcatenate(this._matrix); raster.insertAbove(this); @@ -4404,8 +4546,8 @@ var Raster = Item.extend({ return canvas ? canvas.toDataURL() : null; }, - drawImage: function(image, point) { - point = Point.read(arguments, 1); + drawImage: function(image ) { + var point = Point.read(arguments, 1); this.getContext(true).drawImage(image, point.x, point.y); }, @@ -4459,8 +4601,8 @@ var Raster = Item.extend({ return total ? Color.read(channels) : null; }, - getPixel: function(point) { - point = Point.read(arguments); + getPixel: function(point) { + 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); @@ -4481,21 +4623,21 @@ var Raster = Item.extend({ ctx.putImageData(imageData, point.x, point.y); }, - createImageData: function(size) { - size = Size.read(arguments); + createImageData: function() { + var size = Size.read(arguments); return this.getContext().createImageData(size.width, size.height); }, - getImageData: function(rect) { - rect = Rectangle.read(arguments); + getImageData: function(rect) { + 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, point) { - point = Point.read(arguments, 1); + setImageData: function(data ) { + var point = Point.read(arguments, 1); this.getContext(true).putImageData(data, point.x, point.y); }, @@ -4534,7 +4676,8 @@ var Raster = Item.extend({ var PlacedSymbol = Item.extend({ _class: 'PlacedSymbol', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: { getBounds: 'getStrokeBounds' }, _boundsSelected: true, _serializeFields: { @@ -4556,32 +4699,30 @@ var PlacedSymbol = Item.extend({ }, setSymbol: function(symbol) { - if (this._symbol) - delete this._symbol._instances[this._id]; this._symbol = symbol; - symbol._instances[this._id] = this; + this._changed(5); }, clone: function(insert) { - return this._clone(new PlacedSymbol({ - symbol: this.symbol, - insert: false - }), 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) { - return this.symbol._definition._getCachedBounds(getter, matrix); + _getBounds: function(getter, matrix, cacheItem) { + return this.symbol._definition._getCachedBounds(getter, matrix, + cacheItem); }, _hitTest: function(point, options, matrix) { - var result = this._symbol._definition._hitTest(point, options, matrix); - if (result) - result.item = this; - return result; + var res = this._symbol._definition._hitTest(point, options, matrix); + if (res) + res.item = this; + return res; }, _draw: function(ctx, param) { @@ -4606,7 +4747,7 @@ var HitResult = Base.extend({ getOptions: function(options) { return options && options._merged ? options : new Base({ type: null, - tolerance: paper.project.options.hitTolerance || 2, + tolerance: paper.settings.hitTolerance, fill: !options, stroke: !options, segments: !options, @@ -4659,27 +4800,30 @@ var Segment = Base.extend({ }, _changed: function(point) { - if (!this._path) + var path = this._path; + if (!path) return; - var curve = this._path._curves && this.getCurve(), - other; - if (curve) { - curve._changed(); - if (other = (curve[point == this._point - || point == this._handleIn && curve._segment1 == this - ? 'getPrevious' : 'getNext']())) { - other._changed(); - } + var curves = path._curves, + index = this._index, + curveIn, curveOut; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curveIn = curves[index - 1] + || path._closed && curves[curves.length - 1])) + curveIn._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curveOut = curves[index])) + curveOut._changed(); } - this._path._changed(5); + path._changed(5); }, getPoint: function() { return this._point; }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this._point.set(point.x, point.y); }, @@ -4687,8 +4831,8 @@ var Segment = Base.extend({ return this._handleIn; }, - setHandleIn: function(point) { - point = Point.read(arguments); + setHandleIn: function() { + var point = Point.read(arguments); this._handleIn.set(point.x, point.y); }, @@ -4696,8 +4840,8 @@ var Segment = Base.extend({ return this._handleOut; }, - setHandleOut: function(point) { - point = Point.read(arguments); + setHandleOut: function() { + var point = Point.read(arguments); this._handleOut.set(point.x, point.y); }, @@ -4732,7 +4876,7 @@ var Segment = Base.extend({ var next = this.getNext(), handle1 = this._handleOut, handle2 = next._handleIn, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (handle1.isOrthogonal(handle2)) { var from = this._point, to = next._point, @@ -4746,60 +4890,39 @@ var Segment = Base.extend({ return false; }, - _isSelected: function(point) { + _selectionState: 0, + + isSelected: function(_point) { var state = this._selectionState; - return point == this._point ? !!(state & 4) - : point == this._handleIn ? !!(state & 1) - : point == this._handleOut ? !!(state & 2) + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) : false; }, - _setSelected: function(point, selected) { + setSelected: function(selected, _point) { var path = this._path, selected = !!selected, - state = this._selectionState || 0, - selection = [ - !!(state & 4), - !!(state & 1), - !!(state & 2) - ]; - if (point === this._point) { - if (selected) { - selection[1] = selection[2] = false; - } else { - var previous = this.getPrevious(), - next = this.getNext(); - selection[1] = previous && (previous._point.isSelected() - || previous._handleOut.isSelected()); - selection[2] = next && (next._point.isSelected() - || next._handleIn.isSelected()); - } - selection[0] = 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 { - var index = point === this._handleIn ? 1 : 2; - if (selection[index] != selected) { - if (selected) - selection[0] = false; - selection[index] = selected; - } + state &= ~flag; } - this._selectionState = (selection[0] ? 4 : 0) - | (selection[1] ? 1 : 0) - | (selection[2] ? 2 : 0); - if (path && state != this._selectionState) { - path._updateSelection(this, state, this._selectionState); + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); path._changed(33); } }, - isSelected: function() { - return this._isSelected(this._point); - }, - - setSelected: function(selected) { - this._setSelected(this._point, selected); - }, - getIndex: function() { return this._index !== undefined ? this._index : null; }, @@ -4865,6 +4988,11 @@ var Segment = Base.extend({ 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() @@ -4922,13 +5050,14 @@ var SegmentPoint = Point.extend({ x = y = 0; } else if ((x = point[0]) !== undefined) { y = point[1]; - } else { - if ((x = point.x) === undefined) { - point = Point.read(arguments); - x = point.x; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; } - y = point.y; - selected = point.selected; + y = pt.y; + selected = pt.selected; } this._x = x; this._y = y; @@ -4977,11 +5106,11 @@ var SegmentPoint = Point.extend({ }, setSelected: function(selected) { - this._owner._setSelected(this, selected); + this._owner.setSelected(selected, this); }, isSelected: function() { - return this._owner._isSelected(this); + return this._owner.isSelected(this); } }); @@ -5021,16 +5150,15 @@ var Curve = Base.extend({ }, _changed: function() { - delete this._length; - delete this._bounds; + this._length = this._bounds = undefined; }, getPoint1: function() { return this._segment1._point; }, - setPoint1: function(point) { - point = Point.read(arguments); + setPoint1: function() { + var point = Point.read(arguments); this._segment1._point.set(point.x, point.y); }, @@ -5038,8 +5166,8 @@ var Curve = Base.extend({ return this._segment2._point; }, - setPoint2: function(point) { - point = Point.read(arguments); + setPoint2: function() { + var point = Point.read(arguments); this._segment2._point.set(point.x, point.y); }, @@ -5047,8 +5175,8 @@ var Curve = Base.extend({ return this._segment1._handleOut; }, - setHandle1: function(point) { - point = Point.read(arguments); + setHandle1: function() { + var point = Point.read(arguments); this._segment1._handleOut.set(point.x, point.y); }, @@ -5056,8 +5184,8 @@ var Curve = Base.extend({ return this._segment2._handleIn; }, - setHandle2: function(point) { - point = Point.read(arguments); + setHandle2: function() { + var point = Point.read(arguments); this._segment2._handleIn.set(point.x, point.y); }, @@ -5090,16 +5218,21 @@ var Curve = Base.extend({ }, isSelected: function() { - return this.getHandle1().isSelected() && this.getHandle2().isSelected(); + 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() { - return Curve.getValues(this._segment1, this._segment2); + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); }, getPoints: function() { @@ -5111,15 +5244,12 @@ var Curve = Base.extend({ }, getLength: function() { - var from = arguments[0], - to = arguments[1], - fullLength = arguments.length === 0 || from === 0 && to === 1; - if (fullLength && this._length != null) - return this._length; - var length = Curve.getLength(this.getValues(), from, to); - if (fullLength) - this._length = length; - return length; + if (this._length == null) { + this._length = this.isLinear() + ? this._segment2._point.getDistance(this._segment1._point) + : Curve.getLength(this.getValues(), 0, 1); + } + return this._length; }, getArea: function() { @@ -5130,20 +5260,25 @@ var Curve = Base.extend({ return new Curve(Curve.getPart(this.getValues(), from, to)); }, + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + isLinear: function() { return this._segment1._handleOut.isZero() && this._segment2._handleIn.isZero(); }, + isHorizontal: function() { + return this.isLinear() && Numerical.isZero( + this._segment1._point._y - this._segment2._point._y); + }, + getIntersections: function(curve) { return Curve.getIntersections(this.getValues(), curve.getValues(), this, curve, []); }, - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); - }, - _getParameter: function(offset, isParameter) { return isParameter ? offset @@ -5154,13 +5289,13 @@ var Curve = Base.extend({ : this.getParameterAt(offset, 0); }, - divide: function(offset, isParameter) { + divide: function(offset, isParameter, ignoreLinear) { var parameter = this._getParameter(offset, isParameter), tolerance = 0.00001, res = null; if (parameter > tolerance && parameter < 1 - tolerance) { var parts = Curve.subdivide(this.getValues(), parameter), - isLinear = this.isLinear(), + isLinear = ignoreLinear ? false : this.isLinear(), left = parts[0], right = parts[1]; @@ -5199,6 +5334,22 @@ var Curve = Base.extend({ : null; }, + reverse: function() { + return new Curve(this._segment2.reverse(), this._segment1.reverse()); + }, + + 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); }, @@ -5214,17 +5365,20 @@ var Curve = Base.extend({ }, statics: { - getValues: function(segment1, segment2) { + getValues: function(segment1, segment2, matrix) { var p1 = segment1._point, h1 = segment1._handleOut, h2 = segment2._handleIn, - p2 = segment2._point; - return [ - p1._x, p1._y, - p1._x + h1._x, p1._y + h1._y, - p2._x + h2._x, p2._y + h2._y, - p2._x, p2._y - ]; + 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, 0, values, 0, 6); + return values; }, evaluate: function(v, t, type) { @@ -5232,11 +5386,13 @@ statics: { c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], + tolerance = 0.00001, x, y; - if (type === 0 && (t === 0 || t === 1)) { - x = t === 0 ? p1x : p2x; - y = t === 0 ? p1y : p2y; + if (type === 0 && (t < tolerance || t > 1 - tolerance)) { + var isZero = t < tolerance; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; } else { var cx = 3 * (c1x - p1x), bx = 3 * (c2x - c1x) - cx, @@ -5249,11 +5405,16 @@ statics: { x = ((ax * t + bx) * t + cx) * t + p1x; y = ((ay * t + by) * t + cy) * t + p1y; } else { - var tolerance = 0.00001; if (t < tolerance && c1x === p1x && c1y === p1y || t > 1 - tolerance && c2x === p2x && c2y === p2y) { x = p2x - p1x; y = p2y - p1y; + } else if (t < tolerance) { + x = cx; + y = cy; + } else if (t > 1 - tolerance) { + 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; @@ -5265,7 +5426,7 @@ statics: { } } } - return type == 2 ? new Point(y, -x) : new Point(x, y); + return type === 2 ? new Point(y, -x) : new Point(x, y); }, subdivide: function(v, t) { @@ -5300,11 +5461,10 @@ statics: { }, getParameterOf: function(v, x, y) { - if (Math.abs(v[0] - x) < 0.00001 - && Math.abs(v[1] - y) < 0.00001) + var tolerance = 0.00001; + if (Math.abs(v[0] - x) < tolerance && Math.abs(v[1] - y) < tolerance) return 0; - if (Math.abs(v[6] - x) < 0.00001 - && Math.abs(v[7] - y) < 0.00001) + if (Math.abs(v[6] - x) < tolerance && Math.abs(v[7] - y) < tolerance) return 1; var txs = [], tys = [], @@ -5317,7 +5477,7 @@ statics: { 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) < 0.00001) + if (Math.abs(tx - ty) < tolerance) return (tx + ty) * 0.5; } } @@ -5404,87 +5564,6 @@ statics: { + t * t * t * v3, padding); } - }, - - _getWinding: function(v, prev, x, y, roots1, roots2) { - var tolerance = 0.00001, - abs = Math.abs; - - function getDirection(v) { - var y0 = v[1], - y1 = v[7], - dir = y0 > y1 ? -1 : 1; - return dir === 1 && (y < y0 || y > y1) - || dir === -1 && (y < y1 || y > y0) - ? 0 - : dir; - } - - if (Curve.isLinear(v)) { - var dir = getDirection(v); - if (!dir) - return 0; - var cross = (v[6] - v[0]) * (y - v[1]) - (v[7] - v[1]) * (x - v[0]); - return (cross < -tolerance ? -1 : 1) == dir ? 0 : dir; - } - - var y0 = v[1], - y1 = v[3], - y2 = v[5], - y3 = v[7]; - var a = 3 * (y1 - y2) - y0 + y3, - b = 2 * (y0 + y2) - 4 * y1, - c = y1 - y0; - var count = Numerical.solveQuadratic(a, b, c, roots1, tolerance, - 1 - tolerance), - part, - rest = v, - t1 = roots1[0], - winding = 0; - for (var i = 0; i <= count; i++) { - if (i === count) { - part = rest; - } else { - var curves = Curve.subdivide(rest, t1); - part = curves[0]; - rest = curves[1]; - t1 = roots1[i]; - t1 = (roots1[i + 1] - t1) / (1 - t1); - } - if (i > 0) - part[3] = part[1]; - if (i < count) - part[5] = rest[1]; - var dir = getDirection(part); - if (!dir) - continue; - var t2, - px; - if (Curve.solveCubic(part, 1, y, roots2, -tolerance, 1 + -tolerance) - === 1) { - t2 = roots2[0]; - px = Curve.evaluate(part, t2, 0).x; - } else { - var mid = (part[1] + part[7]) / 2; - t2 = y < mid && dir > 0 ? 0 : 1; - if (t2 === 1 && y == part[7]) - continue; - px = t2 === 0 ? part[0] : part[6]; - } - var slope = Curve.evaluate(part, t2, 1).y, - stationary = abs(slope) < tolerance || t2 < tolerance - && Curve.evaluate(prev, 1, 1).y * slope < 0; - if (x >= px + (stationary ? -tolerance : tolerance * dir) - && !(stationary && (abs(t2) < tolerance - && abs(x - part[0]) > tolerance - || abs(t2 - 1) < tolerance - && abs(x - part[6]) > tolerance))) { - winding += stationary && abs(t2 - (dir > 0 ? 1 : 0)) < tolerance - ? -dir : dir; - } - prev = part; - } - return winding; } }}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], function(name) { @@ -5518,8 +5597,8 @@ statics: { start !== undefined ? start : offset < 0 ? 1 : 0); }, - getParameterOf: function(point) { - point = Point.read(arguments); + getParameterOf: function(point) { + var point = Point.read(arguments); return Curve.getParameterOf(this.getValues(), point.x, point.y); }, @@ -5529,17 +5608,16 @@ statics: { return new CurveLocation(this, offset); }, - getLocationOf: function(point) { - point = Point.read(arguments); - var t = this.getParameterOf(point); + getLocationOf: function(point) { + var point = Point.read(arguments), + t = this.getParameterOf(point); return t != null ? new CurveLocation(this, t) : null; }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var values = this.getValues(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + values = this.getValues(), count = 100, - tolerance = Numerical.TOLERANCE, minDist = Infinity, minT = 0; @@ -5559,7 +5637,7 @@ statics: { refine(i / count); var step = 1 / (count * 2); - while (step > tolerance) { + while (step > 0.00001) { if (!refine(minT - step) && !refine(minT + step)) step /= 2; } @@ -5569,7 +5647,7 @@ statics: { }, getNearestPoint: function(point) { - point = Point.read(arguments); + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); } @@ -5610,11 +5688,12 @@ new function() { if (b === undefined) b = 1; var isZero = Numerical.isZero; - if (isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + 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])) { var dx = v[6] - v[0], dy = v[7] - v[1]; - return (b - a) * Math.sqrt(dx * dx + dy * dy); + return Math.sqrt(dx * dx + dy * dy); } var ds = getLengthIntegrand(v); return Numerical.integrate(ds, a, b, getIterations(a, b)); @@ -5648,148 +5727,89 @@ new function() { } }; }, new function() { - function addLocation(locations, curve1, t1, point1, curve2, t2, point2) { - var first = locations[0], - last = locations[locations.length - 1]; - if ((!first || !point1.isClose(first._point, Numerical.EPSILON)) - && (!last || !point1.isClose(last._point, Numerical.EPSILON))) - locations.push( - new CurveLocation(curve1, t1, point1, curve2, t2, point2)); + 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); } - function addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, range2, recursion) { - recursion = (recursion || 0) + 1; + function addCurveIntersections(v1, v2, curve1, curve2, locations, include, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { if (recursion > 20) return; - range1 = range1 || [ 0, 1 ]; - range2 = range2 || [ 0, 1 ]; - var part1 = Curve.getPart(v1, range1[0], range1[1]), - part2 = Curve.getPart(v2, range2[0], range2[1]), - iteration = 0; - while (iteration++ < 20) { - var range, - intersects1 = clipFatLine(part1, part2, range = range2.slice()), - intersects2 = 0; - if (intersects1 === 0) - break; - if (intersects1 > 0) { - range2 = range; - part2 = Curve.getPart(v2, range2[0], range2[1]); - intersects2 = clipFatLine(part2, part1, range = range1.slice()); - if (intersects2 === 0) - break; - if (intersects1 > 0) { - range1 = range; - part1 = Curve.getPart(v1, range1[0], range1[1]); - } + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + tolerance = 0.00001, + hullEpsilon = 1e-9, + 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, + 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]), + tMinNew, tMaxNew, tDiff; + if (q0x === q3x && uMax - uMin <= hullEpsilon && recursion > 3) { + tMinNew = (tMax + tMin) / 2; + tMaxNew = tMinNew; + 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 false; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + tDiff = tMaxClip - tMinClip; + tMinNew = tMax * tMinClip + tMin * (1 - tMinClip); + tMaxNew = tMax * tMaxClip + tMin * (1 - tMaxClip); + } + if (oldTDiff > 0.8 && tDiff > 0.8) { + 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); + addCurveIntersections( + v2, parts[1], curve2, curve1, locations, include, + 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); + addCurveIntersections( + parts[1], v1, curve2, curve1, locations, include, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } - if (intersects1 < 0 || intersects2 < 0) { - if (range1[1] - range1[0] > range2[1] - range2[0]) { - var t = (range1[0] + range1[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ range1[0], t ], range2, recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ t, range1[1] ], range2, recursion); - break; - } else { - var t = (range2[0] + range2[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ range2[0], t ], recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ t, range2[1] ], recursion); - break; - } - } - if (Math.abs(range1[1] - range1[0]) <= 0.00001 && - Math.abs(range2[1] - range2[0]) <= 0.00001) { - var t1 = (range1[0] + range1[1]) / 2, - t2 = (range2[0] + range2[1]) / 2; - addLocation(locations, + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) < tolerance) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + if (reverse) { + addLocation(locations, include, + curve2, t2, Curve.evaluate(v2, t2, 0), + curve1, t1, Curve.evaluate(v1, t1, 0)); + } else { + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); - break; } + } else { + addCurveIntersections(v2, v1, curve2, curve1, locations, include, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); } } - function clipFatLine(v1, v2, range2) { - var p0x = v1[0], p0y = v1[1], p1x = v1[2], p1y = v1[3], - p2x = v1[4], p2y = v1[5], p3x = v1[6], p3y = v1[7], - q0x = v2[0], q0y = v2[1], q1x = v2[2], q1y = v2[3], - q2x = v2[4], q2y = v2[5], q3x = v2[6], q3y = v2[7], - getSignedDistance = Line.getSignedDistance, - d1 = getSignedDistance(p0x, p0y, p3x, p3y, p1x, p1y) || 0, - d2 = getSignedDistance(p0x, p0y, p3x, p3y, p2x, p2y) || 0, - factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, - dmin = factor * Math.min(0, d1, d2), - dmax = factor * Math.max(0, d1, d2), - dq0 = getSignedDistance(p0x, p0y, p3x, p3y, q0x, q0y), - dq1 = getSignedDistance(p0x, p0y, p3x, p3y, q1x, q1y), - dq2 = getSignedDistance(p0x, p0y, p3x, p3y, q2x, q2y), - dq3 = getSignedDistance(p0x, p0y, p3x, p3y, q3x, q3y); - if (dmin > Math.max(dq0, dq1, dq2, dq3) - || dmax < Math.min(dq0, dq1, dq2, dq3)) - return 0; - var hull = getConvexHull(dq0, dq1, dq2, dq3), - swap; - if (dq3 < dq0) { - swap = dmin; - dmin = dmax; - dmax = swap; - } - var tmaxdmin = -Infinity, - tmin = Infinity, - tmax = -Infinity; - for (var i = 0, l = hull.length; i < l; i++) { - var p1 = hull[i], - p2 = hull[(i + 1) % l]; - if (p2[1] < p1[1]) { - swap = p2; - p2 = p1; - p1 = swap; - } - var x1 = p1[0], - y1 = p1[1], - x2 = p2[0], - y2 = p2[1]; - var inv = (y2 - y1) / (x2 - x1); - if (dmin >= y1 && dmin <= y2) { - var ixdx = x1 + (dmin - y1) / inv; - if (ixdx < tmin) - tmin = ixdx; - if (ixdx > tmaxdmin) - tmaxdmin = ixdx; - } - if (dmax >= y1 && dmax <= y2) { - var ixdx = x1 + (dmax - y1) / inv; - if (ixdx > tmax) - tmax = ixdx; - if (ixdx < tmin) - tmin = 0; - } - } - if (tmin !== Infinity && tmax !== -Infinity) { - var min = Math.min(dmin, dmax), - max = Math.max(dmin, dmax); - if (dq3 > min && dq3 < max) - tmax = 1; - if (dq0 > min && dq0 < max) - tmin = 0; - if (tmaxdmin > tmax) - tmax = 1; - var v2tmin = range2[0], - tdiff = range2[1] - v2tmin; - range2[0] = v2tmin + tmin * tdiff; - range2[1] = v2tmin + tmax * tdiff; - if ((tdiff - (range2[1] - range2[0])) / tdiff >= 0.2) - return 1; - } - if (Curve.getBounds(v1).touches(Curve.getBounds(v2))) - return -1; - return 0; - } - function getConvexHull(dq0, dq1, dq2, dq3) { var p0 = [ 0, dq0 ], p1 = [ 1 / 3, dq1 ], @@ -5797,26 +5817,74 @@ new function() { p3 = [ 1, dq3 ], getSignedDistance = Line.getSignedDistance, dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); + dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), + flip = false, + hull; if (dist1 * dist2 < 0) { - return [ p0, p1, p3, p2 ]; - } - var pmax, cross; - if (Math.abs(dist1) > Math.abs(dist2)) { - pmax = p1; - cross = (dq3 - dq2 - (dq3 - dq0) / 3) - * (2 * (dq3 - dq2) - dq3 + dq1) / 3; + hull = [[p0, p1, p3], [p0, p2, p3]]; + flip = dist1 < 0; } else { - pmax = p2; - cross = (dq1 - dq0 + (dq0 - dq3) / 3) - * (-2 * (dq0 - dq1) + dq0 - dq2) / 3; + 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; } - return cross < 0 - ? [ p0, pmax, p3 ] - : [ p0, p1, p2, p3 ]; + return flip ? hull.reverse() : hull; } - function addCurveLineIntersections(v1, v2, curve1, curve2, locations) { + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + var tProxy, + tVal = null, + px, py, + qx, qy; + for (var i = 0, l = hullBottom.length - 1; i < l; i++) { + py = hullBottom[i][1]; + qy = hullBottom[i + 1][1]; + if (py < qy) { + tProxy = null; + } else if (qy <= dMax) { + px = hullBottom[i][0]; + qx = hullBottom[i + 1][0]; + tProxy = px + (dMax - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + if (hullTop[0][1] <= dMax) + tProxy = hullTop[0][0]; + for (var i = 0, l = hullTop.length - 1; i < l; i++) { + py = hullTop[i][1]; + qy = hullTop[i + 1][1]; + if (py >= dMin) { + tVal = tProxy; + } else if (py > qy) { + tVal = null; + } else if (qy >= dMin) { + px = hullTop[i][0]; + qx = hullTop[i + 1][0]; + tVal = px + (dMin - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + return tVal; + } + + function addCurveLineIntersections(v1, v2, curve1, curve2, locations, + include) { var flip = Curve.isLinear(v1), vc = flip ? v2 : v1, vl = flip ? v1 : v2, @@ -5846,43 +5914,37 @@ new function() { var tl = Curve.getParameterOf(rvl, x, 0), t1 = flip ? tl : tc, t2 = flip ? tc : tl; - addLocation(locations, + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); } } } - function addLineIntersection(v1, v2, curve1, curve2, locations) { + function addLineIntersection(v1, v2, curve1, curve2, locations, include) { var point = Line.intersect( v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]); - if (point) - addLocation(locations, curve1, null, point, curve2); + 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); + } } return { statics: { - getIntersections: function(v1, v2, curve1, curve2, locations) { + getIntersections: function(v1, v2, curve1, curve2, locations, include) { var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2), - c1p1 = curve1.getPoint1(), - c1p2 = curve1.getPoint2(), - c2p1 = curve2.getPoint1(), - c2p2 = curve2.getPoint2(), - tolerance = 0.00001; - if (c1p1.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 0, c1p1); - if (c1p1.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 1, c1p1); + linear2 = Curve.isLinear(v2); (linear1 && linear2 ? addLineIntersection : linear1 || linear2 ? addCurveLineIntersections - : addCurveIntersections)(v1, v2, curve1, curve2, locations); - if (c1p2.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 0, c1p2); - if (c1p2.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 1, c1p2); + : addCurveIntersections)( + v1, v2, curve1, curve2, locations, include, + 0, 1, 0, 1, 0, false, 0); return locations; } }}; @@ -5904,13 +5966,13 @@ var CurveLocation = Base.extend({ this._distance = _distance; }, - getSegment: function() { + getSegment: function(_preferFirst) { if (!this._segment) { var curve = this.getCurve(), parameter = this.getParameter(); if (parameter === 1) { this._segment = curve._segment2; - } else if (parameter === 0 || arguments[0]) { + } else if (parameter === 0 || _preferFirst) { this._segment = curve._segment1; } else if (parameter == null) { return null; @@ -5924,8 +5986,12 @@ var CurveLocation = Base.extend({ return this._segment; }, - getCurve: function() { - if (!this._curve || arguments[0]) { + setSegment: function(segment) { + this._segment = segment; + }, + + getCurve: function(_uncached) { + if (!this._curve || _uncached) { this._curve = this._segment1.getCurve(); if (this._curve.getParameterOf(this._point) == null) this._curve = this._segment2.getPrevious().getCurve(); @@ -5933,6 +5999,10 @@ var CurveLocation = Base.extend({ return this._curve; }, + setCurve: function(curve) { + this._curve = curve; + }, + getIntersection: function() { var intersection = this._intersection; if (!intersection && this._curve2) { @@ -5965,22 +6035,30 @@ var CurveLocation = Base.extend({ return parameter != null && curve && curve.getLength(0, parameter); }, - getParameter: function() { - if ((this._parameter == null || arguments[0]) && this._point) { - var curve = this.getCurve(arguments[0] && this._point); + getParameter: function(_uncached) { + if ((this._parameter == null || _uncached) && this._point) { + var curve = this.getCurve(_uncached && this._point); this._parameter = curve && curve.getParameterOf(this._point); } return this._parameter; }, - getPoint: function() { - if ((!this._point || arguments[0]) && this._parameter != null) { + setParameter: function(parameter) { + this._parameter = parameter; + }, + + getPoint: function(_uncached) { + if ((!this._point || _uncached) && this._parameter != null) { var curve = this.getCurve(); this._point = curve && curve.getPointAt(this._parameter, true); } return this._point; }, + setPoint: function(point) { + this._point = point; + }, + getTangent: function() { var parameter = this.getParameter(), curve = this.getCurve(); @@ -6007,6 +6085,17 @@ var CurveLocation = Base.extend({ return curve && curve.split(this.getParameter(true), true); }, + equals: function(loc) { + var isZero = Numerical.isZero; + return this === loc + || loc + && this._curve === loc._curve + && this._curve2 === loc._curve2 + && isZero(this._parameter - loc._parameter) + && isZero(this._parameter2 - loc._parameter2) + || false; + }, + toString: function() { var parts = [], point = this.getPoint(), @@ -6031,22 +6120,95 @@ var PathItem = Item.extend({ initialize: function PathItem() { }, - getIntersections: function(path) { - if (!this.getBounds().touches(path.getBounds())) + getIntersections: function(path, _expand) { + if (this === path) + path = null; + if (path && !this.getBounds().touches(path.getBounds())) return []; var locations = [], curves1 = this.getCurves(), - curves2 = path.getCurves(), - length2 = curves2.length, - values2 = []; + curves2 = path ? path.getCurves() : curves1, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = path ? path._matrix.orNullIfIdentity() : matrix1, + length1 = curves1.length, + length2 = path ? curves2.length : length1, + values2 = [], + MIN = 1e-11, + MAX = 1 - 1e-11; for (var i = 0; i < length2; i++) - values2[i] = curves2[i].getValues(); - for (var i = 0, l = curves1.length; i < l; i++) { + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { var curve1 = curves1[i], - values1 = curve1.getValues(); - for (var j = 0; j < length2; j++) - Curve.getIntersections(values1, values2[j], curve1, curves2[j], - locations); + 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 <= MAX) { + loc._parameter /= 2; + loc._parameter2 = 0.5 + loc._parameter2 / 2; + return true; + } + } + ); + } + } + 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 >= MIN && t <= MAX; + } + ); + } + } + var last = locations.length - 1; + for (var i = last; i >= 0; i--) { + var loc = locations[i], + next = loc._curve.getNext(), + next2 = loc._curve2.getNext(); + if (next && loc._parameter >= MAX) { + loc._parameter = 0; + loc._curve = next; + } + if (next2 && loc._parameter2 >= MAX) { + loc._parameter2 = 0; + loc._curve2 = next2; + } + } + + 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 === 0 ? last : i - 1])) { + locations.splice(i, 1); + last--; + } + } + } + if (_expand) { + for (var i = last; i >= 0; i--) + locations.push(locations[i].getIntersection()); + locations.sort(compare); } return locations; }, @@ -6056,22 +6218,22 @@ var PathItem = Item.extend({ var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), coords, relative = false, + previous, control, - current = new Point(); + current = new Point(), + start = new Point(); - function getCoord(index, coord, isCurrent) { - var val = parseFloat(coords[index]); + function getCoord(index, coord) { + var val = +coords[index]; if (relative) val += current[coord]; - if (isCurrent) - current[coord] = val; return val; } - function getPoint(index, isCurrent) { + function getPoint(index) { return new Point( - getCoord(index, 'x', isCurrent), - getCoord(index + 1, 'y', isCurrent) + getCoord(index, 'x'), + getCoord(index + 1, 'y') ); } @@ -6079,24 +6241,31 @@ var PathItem = Item.extend({ for (var i = 0, l = parts.length; i < l; i++) { var part = parts[i], - cmd = part[0], - lower = cmd.toLowerCase(); + command = part[0], + lower = command.toLowerCase(); coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); var length = coords && coords.length; - relative = cmd === lower; + relative = command === lower; + if (previous === 'z' && lower !== 'z') + this.moveTo(current = start); switch (lower) { case 'm': case 'l': + var move = lower === 'm'; + if (move && previous && previous !== 'z') + this.closePath(); for (var j = 0; j < length; j += 2) - this[j === 0 && lower === 'm' ? 'moveTo' : 'lineTo']( - getPoint(j, true)); + 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'; + var coord = lower === 'h' ? 'x' : 'y'; for (var j = 0; j < length; j++) { - getCoord(j, coord, true); + current[coord] = getCoord(j, coord); this.lineTo(current); } control = current; @@ -6106,37 +6275,49 @@ var PathItem = Item.extend({ this.cubicCurveTo( getPoint(j), control = getPoint(j + 2), - getPoint(j + 4, true)); + current = getPoint(j + 4)); } break; case 's': for (var j = 0; j < length; j += 4) { this.cubicCurveTo( - current.multiply(2).subtract(control), + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); + previous = lower; } break; case 'q': for (var j = 0; j < length; j += 4) { this.quadraticCurveTo( control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); } break; case 't': for (var j = 0; j < length; j += 2) { this.quadraticCurveTo( - control = current.multiply(2).subtract(control), - getPoint(j, true)); + 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[0], +coords[1]), + +coords[2], +coords[4], +coords[3]); + } break; case 'z': this.closePath(); break; } + previous = lower; } }, @@ -6145,9 +6326,9 @@ var PathItem = Item.extend({ }, _contains: function(point) { - var winding = this._getWinding(point); + var winding = this._getWinding(point, false, true); return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); - } + }, }); @@ -6165,11 +6346,20 @@ var Path = PathItem.extend({ ? typeof arg[0] === 'object' ? arg : arguments - : arg && (arg.point !== undefined && arg.size === undefined - || arg.x !== undefined) + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) ? arguments : null; - this.setSegments(segments || []); + 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); }, @@ -6178,30 +6368,34 @@ var Path = PathItem.extend({ }, clone: function(insert) { - var copy = this._clone(new Path({ - segments: this._segments, - insert: false - }), 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 copy; + return this._clone(copy, insert); }, _changed: function _changed(flags) { _changed.base.call(this, flags); if (flags & 4) { - delete this._length; - delete this._clockwise; + (this._compound ? this._parent : this)._currentPath = undefined; + this._length = this._clockwise = undefined; if (this._curves) { for (var i = 0, l = this._curves.length; i < l; i++) this._curves[i]._changed(5); } + this._monoCurves = undefined; } else if (flags & 8) { - delete this._bounds; + this._bounds = undefined; } }, + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + getSegments: function() { return this._segments; }, @@ -6210,8 +6404,9 @@ var Path = PathItem.extend({ var fullySelected = this.isFullySelected(); this._segments.length = 0; this._selectedSegmentState = 0; - delete this._curves; - this._add(Segment.readAll(segments)); + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); if (fullySelected) this.setFullySelected(true); }, @@ -6246,9 +6441,8 @@ var Path = PathItem.extend({ return curves[curves.length - 1]; }, - getPathData: function() { + getPathData: function(precision) { var segments = this._segments, - precision = arguments[0], f = Formatter.instance, parts = []; @@ -6310,7 +6504,7 @@ var Path = PathItem.extend({ return true; }, - _applyMatrix: function(matrix) { + _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); @@ -6411,7 +6605,7 @@ var Path = PathItem.extend({ return this.removeSegments(index, index + 1)[0] || null; }, - removeSegments: function(from, to) { + removeSegments: function(from, to, _includeCurves) { from = from || 0; to = Base.pick(to, this._segments.length); var segments = this._segments, @@ -6425,8 +6619,7 @@ var Path = PathItem.extend({ var segment = removed[i]; if (segment._selectionState) this._updateSelection(segment, segment._selectionState, 0); - delete segment._index; - delete segment._path; + segment._index = segment._path = null; } for (var i = from, l = segments.length; i < l; i++) segments[i]._index = i; @@ -6435,7 +6628,7 @@ var Path = PathItem.extend({ ? from - 1 : from, curves = curves.splice(index, amount); - if (arguments[2]) + if (_includeCurves) removed._curves = curves.slice(1); this._adjustCurves(index, index); } @@ -6448,7 +6641,7 @@ var Path = PathItem.extend({ isFullySelected: function() { var length = this._segments.length; return this._selected && length > 0 && this._selectedSegmentState - === length * 4; + === length * 7; }, setFullySelected: function(selected) { @@ -6466,10 +6659,10 @@ var Path = PathItem.extend({ _selectSegments: function(selected) { var length = this._segments.length; this._selectedSegmentState = selected - ? length * 4 : 0; + ? length * 7 : 0; for (var i = 0; i < length; i++) this._segments[i]._selectionState = selected - ? 4 : 0; + ? 7 : 0; }, _updateSelection: function(segment, oldState, newState) { @@ -6492,6 +6685,16 @@ var Path = PathItem.extend({ this.setSegments(segments); }, + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (curve.isLinear() && curve.getLength() === 0) + curve.remove(); + } + return this; + }, + simplify: function(tolerance) { if (this._segments.length > 2) { var fitter = new PathFitter(this, tolerance || 2.5); @@ -6509,13 +6712,14 @@ var Path = PathItem.extend({ index = arg.index; parameter = arg.parameter; } - if (parameter >= 1) { + var tolerance = 0.00001; + if (parameter >= 1 - tolerance) { index++; parameter--; } var curves = this.getCurves(); if (index >= 0 && index < curves.length) { - if (parameter > 0) { + if (parameter > tolerance) { curves[index++].divide(parameter, true); } var segs = this.removeSegments(index, this._segments.length, true), @@ -6554,7 +6758,7 @@ var Path = PathItem.extend({ segment._handleOut = handleIn; segment._index = i; } - delete this._curves; + this._curves = null; if (this._clockwise !== undefined) this._clockwise = !this._clockwise; }, @@ -6630,9 +6834,9 @@ var Path = PathItem.extend({ return null; }, - getLocationOf: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(); + getLocationOf: function(point) { + 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) @@ -6652,7 +6856,7 @@ var Path = PathItem.extend({ var start = length, curve = curves[i]; length += curve.getLength(); - if (length >= offset) { + if (length > offset) { return curve.getLocationAt(offset - start); } } @@ -6676,9 +6880,9 @@ var Path = PathItem.extend({ return loc && loc.getNormal(); }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + curves = this.getCurves(), minDist = Infinity, minLoc = null; for (var i = 0, l = curves.length; i < l; i++) { @@ -6691,17 +6895,11 @@ var Path = PathItem.extend({ return minLoc; }, - getNearestPoint: function(point) { - point = Point.read(arguments); + getNearestPoint: function(point) { + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); }, - getStyle: function() { - var parent = this._parent; - return (parent && parent._type === 'compound-path' - ? parent : this)._style; - }, - toShape: function(insert) { if (!this._closed) return null; @@ -6769,114 +6967,80 @@ var Path = PathItem.extend({ return null; }, - _getWinding: function(point) { - var closed = this._closed; - if (!closed && !this.hasFill() - || !this._getBounds('getRoughBounds')._containsPoint(point)) - return 0; - var curves = this.getCurves(), - segments = this._segments, - winding = 0, - roots1 = [], - roots2 = [], - last = (closed - ? curves[curves.length - 1] - : new Curve(segments[segments.length - 1]._point, - segments[0]._point)).getValues(), - previous = last; - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i].getValues(), - x = curve[0], - y = curve[1]; - if (!(x === curve[2] && y === curve[3] && x === curve[4] - && y === curve[5] && x === curve[6] && y === curve[7])) { - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - } - if (!closed) { - winding += Curve._getWinding(last, previous, point.x, point.y, - roots1, roots2); - } - return winding; - }, - _hitTest: function(point, options) { - var style = this.getStyle(), + var that = this, + style = this.getStyle(), segments = this._segments, closed = this._closed, - tolerance = options.tolerance, - radius = 0, join, cap, miterLimit, - that = this, - area, loc, res; - - if (options.stroke) { - radius = style.getStrokeWidth() / 2; + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hasStroke = options.stroke && style.hasStroke(), + hasFill = options.fill && style.hasFill(), + radius = hasStroke ? style.getStrokeWidth() / 2 + : hasFill ? 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'; } - radius += tolerance; } - function checkPoint(seg, pt, name) { - if (point.getDistance(pt) < tolerance) - return new HitResult(name, that, { segment: seg, point: pt }); + 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) { - var pt = seg._point; - return (ends || options.segments) && checkPoint(seg, pt, 'segment') + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') || (!ends && options.handles) && ( - checkPoint(seg, pt.add(seg._handleIn), 'handle-in') || - checkPoint(seg, pt.add(seg._handleOut), 'handle-out')); + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); } - function addAreaPoint(point) { - area.push(point); - } - - function getAreaCurve(index) { - var p1 = area[index], - p2 = area[(index + 1) % area.length]; - return [p1.x, p1.y, p1.x, p1.y, p2.x, p2.y, p2.x ,p2.y]; - } - - function isInArea(point) { - var length = area.length, - previous = getAreaCurve(length - 1), - roots1 = [], - roots2 = [], - winding = 0; - for (var i = 0; i < length; i++) { - var curve = getAreaCurve(i); - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - return !!winding; + function addToArea(point) { + area.add(point); } function checkSegmentStroke(segment) { if (join !== 'round' || cap !== 'round') { - area = []; + area = new Path({ internal: true, closed: true }); if (closed || segment._index > 0 && segment._index < segments.length - 1) { if (join !== 'round' && (segment._handleIn.isZero() || segment._handleOut.isZero())) - Path._addSquareJoin(segment, join, radius, miterLimit, - addAreaPoint, true); + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); } else if (cap !== 'round') { - Path._addSquareCap(segment, cap, radius, addAreaPoint, true); + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); } - if (area.length > 0) - return isInArea(point); } - return point.getDistance(segment._point) <= radius; + return isCloseEnough(segment._point, strokePadding); } if (options.ends && !options.segments && !closed) { @@ -6884,19 +7048,18 @@ var Path = PathItem.extend({ || checkSegmentPoints(segments[segments.length - 1], true)) return res; } else if (options.segments || options.handles) { - for (var i = 0, l = segments.length; i < l; i++) { + for (var i = 0, l = segments.length; i < l; i++) if (res = checkSegmentPoints(segments[i])) return res; - } } - if (radius > 0) { + if (radius != null) { loc = this.getNearestLocation(point); if (loc) { var parameter = loc.getParameter(); if (parameter === 0 || parameter === 1) { if (!checkSegmentStroke(loc.getSegment())) loc = null; - } else if (loc._distance > radius) { + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { loc = null; } } @@ -6911,10 +7074,13 @@ var Path = PathItem.extend({ } } } - return !loc && options.fill && this.hasFill() && this._contains(point) + return !loc && hasFill && this._contains(point) || loc && !hasStroke ? new HitResult('fill', this) : loc - ? new HitResult('stroke', this, { location: loc }) + ? new HitResult('stroke', this, { + location: loc, + point: loc.getPoint() + }) : null; } @@ -6942,24 +7108,19 @@ var Path = PathItem.extend({ var segment = segments[i]; segment._transformCoordinates(matrix, coords, false); var state = segment._selectionState, - selected = state & 4, pX = coords[0], pY = coords[1]; - if (selected || (state & 1)) + if (state & 1) drawHandle(2); - if (selected || (state & 2)) + if (state & 2) drawHandle(4); - ctx.save(); - ctx.beginPath(); - ctx.rect(pX - half, pY - half, size, size); - ctx.fill(); - if (!selected) { - ctx.beginPath(); - ctx.rect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; ctx.fillStyle = '#ffffff'; - ctx.fill(); + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; } - ctx.restore(); } } @@ -7016,14 +7177,14 @@ var Path = PathItem.extend({ for (var i = 0; i < length; i++) drawSegment(i); - if (path._closed && length > 1) + if (path._closed && length > 0) drawSegment(0); } return { _draw: function(ctx, param) { var clip = param.clip, - compound = param.compound; + compound = this._compound = param.compound; if (!compound) ctx.beginPath(); @@ -7038,10 +7199,15 @@ var Path = PathItem.extend({ return dashArray[((i % dashLength) + dashLength) % dashLength]; } - if (hasFill || hasStroke && !dashLength || compound || clip) + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || compound || clip){ drawSegments(ctx, this); - if (this._closed) - ctx.closePath(); + if (this._closed) + ctx.closePath(); + if (!compound) + this._currentPath = ctx.currentPath; + } if (!clip && !compound && (hasFill || hasStroke)) { this._setStyles(ctx); @@ -7077,8 +7243,7 @@ var Path = PathItem.extend({ ctx.beginPath(); drawSegments(ctx, this, matrix); ctx.stroke(); - drawHandles(ctx, this._segments, matrix, - this._project.options.handleSize || 4); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); } }; }, new function() { @@ -7177,16 +7342,17 @@ var Path = PathItem.extend({ }, new function() { function getCurrentSegment(that) { var segments = that._segments; - if (segments.length == 0) + if (segments.length === 0) throw new Error('Use a moveTo() command first'); return segments[segments.length - 1]; } return { moveTo: function() { - if (this._segments.length === 1) + var segments = this._segments; + if (segments.length === 1) this.removeSegment(0); - if (!this._segments.length) + if (!segments.length) this._add([ new Segment(Point.read(arguments)) ]); }, @@ -7235,37 +7401,86 @@ var Path = PathItem.extend({ arcTo: function() { var current = getCurrentSegment(this), from = current._point, - through, to = Point.read(arguments), - clockwise = Base.pick(Base.peek(arguments), true); + 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 { + } else if (Base.remain(arguments) <= 2) { through = to; to = Point.read(arguments); - } - 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), - center = l1.intersect(l2, true), - line = new Line(from, to), - throughSide = line.getSide(through); - if (!center) { - if (!throughSide) + } else { + var radius = Size.read(arguments); + if (radius.isZero()) return this.lineTo(to); - throw new Error('Cannot put an arc through the given points: ' - + [from, through, 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, + EPSILON = 1e-11, + 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) < EPSILON) + 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; } - var vector = from.subtract(center), - extent = vector.getDirectedAngle(to.subtract(center)), - centerSide = line.getSide(center); - if (centerSide == 0) { - extent = throughSide * Math.abs(extent); - } else if (throughSide == centerSide) { - extent -= 360 * (extent < 0 ? -1 : 1); + 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), @@ -7274,13 +7489,27 @@ var Path = PathItem.extend({ z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), segments = []; for (var i = 0; i <= count; i++) { - var pt = i < count ? center.add(vector) : to; - var out = i < count ? vector.rotate(90).multiply(z) : null; - if (i == 0) { + 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 { - segments.push( - new Segment(pt, vector.rotate(-90).multiply(z), out)); + 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); } @@ -7331,7 +7560,7 @@ var Path = PathItem.extend({ closePath: function() { var first = this.getFirstSegment(), last = this.getLastSegment(); - if (first._point.equals(last._point)) { + if (first !== last && first._point.equals(last._point)) { first.setHandleIn(last._handleIn); last.remove(); } @@ -7390,29 +7619,11 @@ statics: { }, getStrokeBounds: function(segments, closed, style, matrix) { - function getPenPadding(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)]; - } - if (!style.hasStroke()) return Path.getBounds(segments, closed, style, matrix); var length = segments.length - (closed ? 0 : 1), radius = style.getStrokeWidth() / 2, - padding = getPenPadding(radius, matrix), + padding = Path._getPenPadding(radius, matrix), bounds = Path.getBounds(segments, closed, style, matrix, padding), join = style.getStrokeJoin(), cap = style.getStrokeCap(), @@ -7424,25 +7635,27 @@ statics: { ? matrix._transformPoint(point, point) : point); } + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + function addJoin(segment, join) { - if (join === 'round' || !segment._handleIn.isZero() - && !segment._handleOut.isZero()) { - bounds = bounds.unite(joinBounds.setCenter(matrix - ? matrix._transformPoint(segment._point) : segment._point)); + var handleIn = segment._handleIn, + handleOut = segment._handleOut + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isColinear(handleOut)) { + addRound(segment); } else { - Path._addSquareJoin(segment, join, radius, miterLimit, add); + Path._addBevelJoin(segment, join, radius, miterLimit, add); } } function addCap(segment, cap) { - switch (cap) { - case 'round': - addJoin(segment, cap); - break; - case 'butt': - case 'square': + if (cap === 'round') { + addRound(segment); + } else { Path._addSquareCap(segment, cap, radius, add); - break; } } @@ -7450,14 +7663,32 @@ statics: { addJoin(segments[i], join); if (closed) { addJoin(segments[0], join); - } else { + } else if (length > 0) { addCap(segments[0], cap); addCap(segments[segments.length - 1], cap); } return bounds; }, - _addSquareJoin: function(segment, join, radius, miterLimit, addPoint, area) { + _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), @@ -7510,19 +7741,19 @@ statics: { x2 = -x1, y1 = x1, y2 = x2; - strokePadding = strokePadding / 2 || 0; - joinPadding = joinPadding / 2 || 0; 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 - padding, - xx = x + padding, - yn = y - padding, - yx = y + padding; + 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; @@ -7533,24 +7764,23 @@ statics: { }, getRoughBounds: function(segments, closed, style, matrix) { - var strokeWidth = style.getStrokeColor() ? style.getStrokeWidth() : 0, - joinWidth = strokeWidth; - if (strokeWidth === 0) { - strokeWidth = 0.00001; - } else { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { if (style.getStrokeJoin() === 'miter') - joinWidth = strokeWidth * style.getMiterLimit(); + joinRadius = strokeRadius * style.getMiterLimit(); if (style.getStrokeCap() === 'square') - joinWidth = Math.max(joinWidth, strokeWidth * Math.sqrt(2)); + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); } return Path.getHandleBounds(segments, closed, style, matrix, - strokeWidth, joinWidth); + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); } }}); Path.inject({ statics: new function() { - var kappa = Numerical.KAPPA, + var kappa = 0.5522847498307936, ellipseSegments = [ new Segment([-1, 0], [0, kappa ], [0, -kappa]), new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), @@ -7558,9 +7788,16 @@ Path.inject({ statics: new function() { 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 path = new Path(), - segments = new Array(4); + var segments = new Array(4); for (var i = 0; i < 4; i++) { var segment = ellipseSegments[i]; segments[i] = new Segment( @@ -7569,17 +7806,15 @@ Path.inject({ statics: new function() { segment._handleOut.multiply(radius) ); } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(args)); + return createPath(segments, true, args); } return { Line: function() { - return new Path( - Point.readNamed(arguments, 'from'), - Point.readNamed(arguments, 'to') - ).set(Base.getNamed(arguments)); + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); }, Circle: function() { @@ -7590,27 +7825,27 @@ Path.inject({ statics: new function() { Rectangle: function() { var rect = Rectangle.readNamed(arguments, 'rectangle'), - radius = Size.readNamed(arguments, 'radius', 0, 0, + radius = Size.readNamed(arguments, 'radius', 0, { readNull: true }), bl = rect.getBottomLeft(true), tl = rect.getTopLeft(true), tr = rect.getTopRight(true), - br = rect.getBottomRight(true); - path = new Path(); + br = rect.getBottomRight(true), + segments; if (!radius || radius.isZero()) { - path._add([ + 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; - path._add([ + 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]), @@ -7619,10 +7854,9 @@ Path.inject({ statics: new function() { 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]) - ]); + ]; } - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, RoundRectangle: '#Rectangle', @@ -7638,29 +7872,27 @@ Path.inject({ statics: new function() { var from = Point.readNamed(arguments, 'from'), through = Point.readNamed(arguments, 'through'), to = Point.readNamed(arguments, 'to'), - path = new Path(); + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); path.moveTo(from); path.arcTo(through, to); - return path.set(Base.getNamed(arguments)); + return path.set(props); }, RegularPolygon: function() { var center = Point.readNamed(arguments, 'center'), sides = Base.readNamed(arguments, 'sides'), radius = Base.readNamed(arguments, 'radius'), - path = new Path(), 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++) { + for (var i = 0; i < sides; i++) segments[i] = new Segment(center.add( vector.rotate((i + offset) * step))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, Star: function() { @@ -7668,17 +7900,13 @@ Path.inject({ statics: new function() { points = Base.readNamed(arguments, 'points') * 2, radius1 = Base.readNamed(arguments, 'radius1'), radius2 = Base.readNamed(arguments, 'radius2'), - path = new Path(), 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))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + 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); } }; }}); @@ -7692,8 +7920,19 @@ var CompoundPath = PathItem.extend({ initialize: function CompoundPath(arg) { this._children = []; this._namedChildren = {}; - if (!this._initialize(arg)) - this.addChildren(Array.isArray(arg) ? arg : arguments); + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & (2 | 4)) + this._currentPath = undefined; }, insertChildren: function insertChildren(index, items, _preserve) { @@ -7741,7 +7980,7 @@ var CompoundPath = PathItem.extend({ var children = this._children, curves = []; for (var i = 0, l = children.length; i < l; i++) - curves = curves.concat(children[i].getCurves()); + curves.push.apply(curves, children[i].getCurves()); return curves; }, @@ -7763,36 +8002,18 @@ var CompoundPath = PathItem.extend({ return area; }, - getPathData: function() { + getPathData: function(precision) { var children = this._children, paths = []; for (var i = 0, l = children.length; i < l; i++) - paths.push(children[i].getPathData(arguments[0])); + paths.push(children[i].getPathData(precision)); return paths.join(' '); }, - _getWinding: function(point) { - var children = this._children, - winding = 0; - for (var i = 0, l = children.length; i < l; i++) - winding += children[i]._getWinding(point); - return winding; - }, - - _hitTest : function _hitTest(point, options) { - var res = _hitTest.base.call(this, point, - new Base(options, { fill: false })); - if (!res) { - if (options.compoundChildren) { - var children = this._children; - for (var i = children.length - 1; i >= 0 && !res; i--) - res = children[i]._hitTest(point, options); - } else if (options.fill && this.hasFill() - && this._contains(point)) { - res = new HitResult('fill', this); - } - } - return res; + _getChildHitTestOptions: function(options) { + return options.type === 'path' + ? options + : new Base(options, { fill: false }); }, _draw: function(ctx, param) { @@ -7800,10 +8021,15 @@ var CompoundPath = PathItem.extend({ if (children.length === 0) return; - ctx.beginPath(); - param = param.extend({ compound: true }); - for (var i = 0, l = children.length; i < l; i++) - children[i].draw(ctx, param); + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + ctx.beginPath(); + param = param.extend({ compound: true }); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param); + this._currentPath = ctx.currentPath; + } if (!param.clip) { this._setStyles(ctx); @@ -7817,26 +8043,31 @@ var CompoundPath = PathItem.extend({ } } }, new function() { - function getCurrentPath(that) { - if (!that._children.length) + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) throw new Error('Use a moveTo() command first'); - return that._children[that._children.length - 1]; + return children[children.length - 1]; } var fields = { moveTo: function() { - var path = new Path(); - this.addChild(path); + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current : new Path(); + if (path !== current) + this.addChild(path); path.moveTo.apply(path, arguments); }, moveBy: function() { - this.moveTo(getCurrentPath(this).getLastSegment()._point.add( - Point.read(arguments))); + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); }, closePath: function() { - getCurrentPath(this).closePath(); + getCurrentPath(this, true).closePath(); } }; @@ -7844,7 +8075,7 @@ var CompoundPath = PathItem.extend({ 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], function(key) { fields[key] = function() { - var path = getCurrentPath(this); + var path = getCurrentPath(this, true); path[key].apply(path, arguments); }; } @@ -7853,6 +8084,433 @@ var CompoundPath = PathItem.extend({ return fields; }); +PathItem.inject(new function() { + function reorientPath(path) { + path = path.clone(false).reduce().transform(null, true); + if (path instanceof CompoundPath) { + var children = path.removeChildren(), + length = children.length, + bounds = new Array(length), + counters = new Array(length), + clockwise; + children.sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + path.addChildren(children); + clockwise = children[0].isClockwise(); + for (var i = 0; i < length; i++) { + bounds[i] = children[i].getBounds(); + counters[i] = 0; + } + for (var i = 0; i < length; i++) { + for (var j = 1; j < length; j++) { + if (i !== j && bounds[i].intersects(bounds[j])) + counters[j]++; + } + if (i > 0 && counters[i] % 2 === 0) + children[i].setClockwise(clockwise); + } + } + return path; + } + + function computeBoolean(path1, path2, operator, subtract) { + var _path1 = reorientPath(path1); + _path2 = path2 && path1 !== path2 && reorientPath(path2); + if (!_path1.isClockwise()) + _path1.reverse(); + if (_path2 && !(subtract ^ _path2.isClockwise())) + _path2.reverse(); + splitPath(_path1.getIntersections(_path2, true)); + + var chain = [], + windings = [], + lengths = [], + 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]); + 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 = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding != null) + continue; + chain.length = windings.length = lengths.length = 0; + var totalLength = 0, + startSeg = segment; + do { + chain.push(segment); + lengths.push(totalLength += segment.getCurve().getLength()); + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== startSeg); + for (var j = 0; j < 3; j++) { + var length = totalLength * Math.random(), + amount = lengths.length; + k = 0; + do { + if (lengths[k] >= length) { + if (k > 0) + length -= lengths[k - 1]; + break; + } + } while (++k < amount); + var curve = chain[k].getCurve(), + point = curve.getPointAt(length), + hor = curve.isHorizontal(), + path = curve._path; + if (path._parent instanceof CompoundPath) + path = path._parent; + windings[j] = subtract && _path2 + && (path === _path1 && _path2._getWinding(point, hor) + || path === _path2 && !_path1._getWinding(point, hor)) + ? 0 + : getWinding(point, monoCurves, hor); + } + windings.sort(); + var winding = windings[1]; + for (var j = chain.length - 1; j >= 0; j--) + chain[j]._winding = winding; + } + var result = new CompoundPath(); + result.addChildren(tracePaths(segments, operator), true); + _path1.remove(); + if (_path2) + _path2.remove(); + return result.reduce(); + } + + function splitPath(intersections) { + var TOLERANCE = 0.00001, + linearSegments; + + function resetLinear() { + for (var i = 0, l = linearSegments.length; i < l; i++) { + var segment = linearSegments[i]; + segment._handleOut.set(0, 0); + segment._handleIn.set(0, 0); + } + } + + for (var i = intersections.length - 1, curve, prevLoc; i >= 0; i--) { + var loc = intersections[i], + t = loc._parameter; + if (prevLoc && prevLoc._curve === loc._curve + && prevLoc._parameter > 0) { + t /= prevLoc._parameter; + } else { + if (linearSegments) + resetLinear(); + curve = loc._curve; + linearSegments = curve.isLinear() && []; + } + var newCurve, + segment; + if (newCurve = curve.divide(t, true, true)) { + segment = newCurve._segment1; + curve = newCurve.getPrevious(); + } else { + segment = t < TOLERANCE + ? curve._segment1 + : t > 1 - TOLERANCE + ? curve._segment2 + : curve.getPartLength(0, t) < curve.getPartLength(t, 1) + ? curve._segment1 + : curve._segment2; + } + segment._intersection = loc.getIntersection(); + loc._segment = segment; + if (linearSegments) + linearSegments.push(segment); + prevLoc = loc; + } + if (linearSegments) + resetLinear(); + } + + function getWinding(point, curves, horizontal, testContains) { + var TOLERANCE = 0.00001, + x = point.x, + y = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs, + MAX = 1 - TOLERANCE; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = y - TOLERANCE, + yAfter = y + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, x, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y0 = Curve.evaluate(values, roots[j], 0).y; + if (y0 < yBefore && y0 > yTop) { + yTop = y0; + } else if (y0 > yAfter && y0 < yBottom) { + yBottom = y0; + } + } + } + } + yTop = (yTop + y) / 2; + yBottom = (yBottom + y) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(x, yTop), curves); + if (yBottom < Infinity) + windRight = getWinding(new Point(x, yBottom), curves); + } else { + var xBefore = x - TOLERANCE, + xAfter = x + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding, + next = curve.next; + if (winding && (winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, + !next.winding && next.values[1] === y ? 1 : MAX) === 1){ + var t = roots[0], + x0 = Curve.evaluate(values, t, 0).x, + slope = Curve.evaluate(values, t, 1).y; + if (abs(slope) < TOLERANCE && !Curve.isLinear(values) + || t < TOLERANCE && slope * Curve.evaluate( + curve.previous.values, t, 1).y < 0) { + if (testContains && x0 >= xBefore && x0 <= xAfter) { + ++windLeft; + ++windRight; + } + } else if (x0 <= xBefore) { + windLeft += winding; + } else if (x0 >= xAfter) { + windRight += winding; + } + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function tracePaths(segments, operator, selfOp) { + operator = operator || function() { + return true; + }; + var paths = [], + ZERO = 1e-3, + ONE = 1 - 1e-3; + for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) { + seg = startSeg = segments[i]; + if (seg._visited || !operator(seg._winding)) + 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) { + var c1 = seg.getCurve(); + if (dir > 0) + c1 = c1.getPrevious(); + var t1 = c1.getTangentAt(dir < 1 ? ZERO : ONE, true), + c4 = interSeg.getCurve(), + c3 = c4.getPrevious(), + t3 = c3.getTangentAt(ONE, true), + t4 = c4.getTangentAt(ZERO, true), + w3 = t1.cross(t3), + w4 = t1.cross(t4); + if (selfOp) { + seg._visited = interSeg._visited; + seg = interSeg; + dir = 1; + } else 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; + } + handleOut = dir > 0 ? seg._handleOut : seg._handleIn; + } + path.add(new Segment(seg._point, added && handleIn, handleOut)); + added = true; + 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); + } + if (path._segments.length > + (path._closed ? path.isPolygon() ? 2 : 0 : 1)) + paths.push(path); + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1 || w === 0; + }, false); + }, + + intersect: function(path) { + return computeBoolean(this, path, function(w) { + return w === 2; + }, false); + }, + + subtract: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1; + }, true); + }, + + exclude: function(path) { + return new Group([this.subtract(path), path.subtract(this)]); + }, + divide: function(path) { + return new Group([this.subtract(path), this.intersect(path)]); + } + }; +}); + +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.isLinear(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + TOLERANCE = 0.00001, + roots = []; + var count = Numerical.solveQuadratic(a, b, c, roots, TOLERANCE, + 1 - TOLERANCE); + if (count === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (count > 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]); + } + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + return monoCurves; + } +}); + +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; + } +}); + var PathFlattener = Base.extend({ initialize: function(path) { this.curves = []; @@ -8136,186 +8794,11 @@ var PathFitter = Base.extend({ } }); -PathItem.inject(new function() { - - function splitPath(intersections, collectOthers) { - intersections.sort(function(loc1, loc2) { - var path1 = loc1.getPath(), - path2 = loc2.getPath(); - return path1 === path2 - ? (loc1.getIndex() + loc1.getParameter()) - - (loc2.getIndex() + loc2.getParameter()) - : path1._id - path2._id; - }); - var others = collectOthers && []; - for (var i = intersections.length - 1; i >= 0; i--) { - var loc = intersections[i], - other = loc.getIntersection(), - curve = loc.divide(), - segment = curve && curve.getSegment1() || loc.getSegment(); - if (others) - others.push(other); - segment._intersection = other; - loc._segment = segment; - } - return others; - } - - function reorientPath(path) { - if (path instanceof CompoundPath) { - var children = path.removeChildren(), - length = children.length, - bounds = new Array(length), - counters = new Array(length), - clockwise; - children.sort(function(a, b){ - var b1 = a.getBounds(), b2 = b.getBounds(); - return b1._width * b1._height < b2._width * b2._height; - }); - path.addChildren(children); - clockwise = children[0].isClockwise(); - for (var i = 0; i < length; i++) { - bounds[i] = children[i].getBounds(); - counters[i] = 0; - } - for (var i = 0; i < length; i++) { - for (var j = 1; j < length; j++) { - if (i !== j && bounds[i].contains(bounds[j])) - counters[j]++; - } - if (i > 0 && counters[i] % 2 === 0) - children[i].setClockwise(clockwise); - } - } - return path; - } - - function computeBoolean(path1, path2, operator, subtract) { - path1 = reorientPath(path1.clone(false)); - path2 = reorientPath(path2.clone(false)); - var path1Clockwise = path1.isClockwise(), - path2Clockwise = path2.isClockwise(), - intersections = path1.getIntersections(path2); - splitPath(splitPath(intersections, true)); - if (!path1Clockwise) - path1.reverse(); - if (!(subtract ^ path2Clockwise)) - path2.reverse(); - path1Clockwise = true; - path2Clockwise = !subtract; - var paths = [] - .concat(path1._children || [path1]) - .concat(path2._children || [path2]), - segments = [], - result = new CompoundPath(); - for (var i = 0, l = paths.length; i < l; i++) { - var path = paths[i], - parent = path._parent, - clockwise = path.isClockwise(), - segs = path._segments; - path = parent instanceof CompoundPath ? parent : path; - for (var j = segs.length - 1; j >= 0; j--) { - var segment = segs[j], - midPoint = segment.getCurve().getPoint(0.5), - insidePath1 = path !== path1 && path1.contains(midPoint) - && (clockwise === path1Clockwise || subtract - || !testOnCurve(path1, midPoint)), - insidePath2 = path !== path2 && path2.contains(midPoint) - && (clockwise === path2Clockwise - || !testOnCurve(path2, midPoint)); - if (operator(path === path1, insidePath1, insidePath2)) { - segment._invalid = true; - } else { - segments.push(segment); - } - } - } - for (var i = 0, l = segments.length; i < l; i++) { - var segment = segments[i]; - if (segment._visited) - continue; - var path = new Path(), - loc = segment._intersection, - intersection = loc && loc.getSegment(true); - if (segment.getPrevious()._invalid) - segment.setHandleIn(intersection - ? intersection._handleIn - : new Point(0, 0)); - do { - segment._visited = true; - if (segment._invalid && segment._intersection) { - var inter = segment._intersection.getSegment(true); - path.add(new Segment(segment._point, segment._handleIn, - inter._handleOut)); - inter._visited = true; - segment = inter; - } else { - path.add(segment.clone()); - } - segment = segment.getNext(); - } while (segment && !segment._visited && segment !== intersection); - var amount = path._segments.length; - if (amount > 1 && (amount > 2 || !path.isPolygon())) { - path.setClosed(true); - result.addChild(path, true); - } else { - path.remove(); - } - } - path1.remove(); - path2.remove(); - return result.reduce(); - } - - function testOnCurve(path, point) { - var curves = path.getCurves(), - bounds = path.getBounds(); - if (bounds.contains(point)) { - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i]; - if (curve.getBounds().contains(point) - && curve.getParameterOf(point)) - return true; - } - } - return false; - } - - return { - unite: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isInPath1 || isInPath2; - }); - }, - - intersect: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return !(isInPath1 || isInPath2); - }); - }, - - subtract: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isPath1 && isInPath2 || !isPath1 && !isInPath1; - }, true); - }, - - exclude: function(path) { - return new Group([this.subtract(path), path.subtract(this)]); - }, - - divide: function(path) { - return new Group([this.subtract(path), this.intersect(path)]); - } - }; -}); - var TextItem = Item.extend({ _class: 'TextItem', _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, _serializeFields: { content: null }, @@ -8367,7 +8850,7 @@ var PointText = TextItem.extend({ }, clone: function(insert) { - return this._clone(new PointText({ insert: false }), insert); + return this._clone(new PointText(Item.NO_INSERT), insert); }, getPoint: function() { @@ -8375,8 +8858,8 @@ var PointText = TextItem.extend({ return new LinkedPoint(point.x, point.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.translate(point.subtract(this._matrix.getTranslation())); }, @@ -8457,7 +8940,7 @@ var Color = Base.extend(new function() { } else if (match = string.match(/^rgba?\((.*)\)$/)) { components = match[1].split(','); for (var i = 0, l = components.length; i < l; i++) { - var value = parseFloat(components[i]); + var value = +components[i]; components[i] = i < 3 ? value / 255 : value; } } else { @@ -8504,8 +8987,8 @@ var Color = Base.extend(new function() { }, 'hsb-rgb': function(h, s, b) { - var h = (h / 60) % 6, - i = Math.floor(h), + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), f = h - i, i = hsbIndices[i], v = [ @@ -8534,7 +9017,7 @@ var Color = Base.extend(new function() { }, 'hsl-rgb': function(h, s, l) { - h /= 360; + h = (((h / 360) % 1) + 1) % 1; if (s === 0) return [l, l, l]; var t3s = [ h + 1 / 3, h, h - 1 / 3 ], @@ -8590,9 +9073,8 @@ var Color = Base.extend(new function() { parser = componentParsers[type][index] = name === 'gradient' ? function(value) { var current = this._components[0]; - value = Gradient.read( - Array.isArray(value) ? value : arguments, - 0, 0, { readNull: true }); + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); if (current !== value) { if (current) current._removeOwner(this); @@ -8601,22 +9083,16 @@ var Color = Base.extend(new function() { } return value; } - : name === 'hue' - ? function(value) { - return isNaN(value) ? 0 - : ((value % 360) + 360) % 360; + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); } - : type === 'gradient' - ? function() { - return Point.read(arguments, 0, 0, { - readNull: name === 'highlight', - clone: true - }); - } - : function(value) { - return isNaN(value) ? 0 - : Math.min(Math.max(value, 0), 1); - }; + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; this['get' + part] = function() { return this._type === type @@ -8647,7 +9123,6 @@ var Color = Base.extend(new function() { var slice = Array.prototype.slice, args = arguments, read = 0, - parse = true, type, components, alpha, @@ -8671,7 +9146,6 @@ var Color = Base.extend(new function() { } } if (!components) { - parse = !(this.__options && this.__options.dontParse); values = argType === 'number' ? args : argType === 'object' && arg.length != null @@ -8724,7 +9198,7 @@ var Color = Base.extend(new function() { ? 'gray' : 'rgb'; var properties = types[type]; - parsers = parse && componentParsers[type]; + parsers = componentParsers[type]; this._components = components = []; for (var i = 0, l = properties.length; i < l; i++) { var value = arg[properties[i]]; @@ -8735,8 +9209,7 @@ var Color = Base.extend(new function() { radial: arg.radial }; } - if (parse) - value = parsers[i].call(this, value); + value = parsers[i].call(this, value); if (value != null) components[i] = value; } @@ -8753,9 +9226,7 @@ var Color = Base.extend(new function() { this._components = components = []; var parsers = componentParsers[this._type]; for (var i = 0, l = parsers.length; i < l; i++) { - var value = values && values[i]; - if (parse) - value = parsers[i].call(this, value); + var value = parsers[i].call(this, values && values[i]); if (value != null) components[i] = value; } @@ -8790,7 +9261,7 @@ var Color = Base.extend(new function() { ? converter.apply(this, this._components) : converters['rgb-' + type].apply(this, converters[this._type + '-rgb'].apply(this, - this._components)); + this._components)); }, convert: function(type) { @@ -8828,12 +9299,13 @@ var Color = Base.extend(new function() { }, equals: function(color) { - if (Base.isPlainValue(color)) - color = Color.read(arguments); - return color === this || color && this._class === color._class - && this._type === color._type - && this._alpha === color._alpha - && Base.equals(this._components, color._components) + var col = Base.isPlainValue(color) + ? 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; }, @@ -8856,13 +9328,16 @@ var Color = Base.extend(new function() { 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 = [ - Math.round(components[0] * 255), - Math.round(components[1] * 255), - Math.round(components[2] * 255) + convert(components[0]), + convert(components[1]), + convert(components[2]) ]; if (alpha < 1) - components.push(alpha); + components.push(alpha < 0 ? 0 : alpha); return hex ? '#' + ((1 << 24) + (components[0] << 16) + (components[1] << 8) @@ -8926,46 +9401,32 @@ var Color = Base.extend(new function() { } }); }, new function() { - function clamp(value, hue) { - return value < 0 - ? 0 - : hue && value > 360 - ? 360 - : !hue && value > 1 - ? 1 - : value; - } - var operators = { - add: function(a, b, hue) { - return clamp(a + b, hue); + add: function(a, b) { + return a + b; }, - subtract: function(a, b, hue) { - return clamp(a - b, hue); + subtract: function(a, b) { + return a - b; }, - multiply: function(a, b, hue) { - return clamp(a * b, hue); + multiply: function(a, b) { + return a * b; }, - divide: function(a, b, hue) { - return clamp(a / b, hue); + divide: function(a, b) { + return a / b; } }; return Base.each(operators, function(operator, name) { - var options = { dontParse: /^(multiply|divide)$/.test(name) }; - this[name] = function(color) { - color = Color.read(arguments, 0, 0, options); + color = Color.read(arguments); var type = this._type, - properties = this._properties, components1 = this._components, components2 = color._convert(type); for (var i = 0, l = components1.length; i < l; i++) - components2[i] = operator(components1[i], components2[i], - properties[i] === 'hue'); + components2[i] = operator(components1[i], components2[i]); return new Color(type, components2, this._alpha != null ? operator(this._alpha, color.getAlpha()) @@ -9030,7 +9491,7 @@ var Gradient = Base.extend({ if (index != -1) { this._owners.splice(index, 1); if (this._owners.length === 0) - delete this._owners; + this._owners = undefined; } }, @@ -9048,7 +9509,7 @@ var Gradient = Base.extend({ setStops: function(stops) { if (this.stops) { for (var i = 0, l = this._stops.length; i < l; i++) - delete this._stops[i]._owner; + this._stops[i]._owner = undefined; } if (stops.length < 2) throw new Error( @@ -9167,8 +9628,10 @@ var Style = Base.extend(new function() { shadowBlur: 0, shadowOffset: new Point(), selectedColor: undefined, - font: 'sans-serif', + fontFamily: 'sans-serif', + fontWeight: 'normal', fontSize: 12, + font: 'sans-serif', leading: null, justification: 'left' }; @@ -9178,8 +9641,10 @@ var Style = Base.extend(new function() { strokeCap: 25, strokeJoin: 25, miterLimit: 25, - font: 5, + fontFamily: 5, + fontWeight: 5, fontSize: 5, + font: 5, leading: 5, justification: 5 }; @@ -9210,7 +9675,7 @@ var Style = Base.extend(new function() { if (old != value) { if (isColor) { if (old) - delete old._owner; + old._owner = undefined; if (value && value.constructor === Color) { if (value._owner) value = value.clone(); @@ -9224,11 +9689,11 @@ var Style = Base.extend(new function() { } }; - fields[get] = function() { + fields[get] = function(_dontMerge) { var value, children = this._item && this._item._children; - if (!children || children.length === 0 || arguments[0] - || this._item._type === 'compound-path') { + if (!children || children.length === 0 || _dontMerge + || this._item instanceof CompoundPath) { var value = this._values[key]; if (value === undefined) { value = this._defaults[key]; @@ -9236,8 +9701,8 @@ var Style = Base.extend(new function() { value = value.clone(); this._values[key] = value; } else if (isColor && !(value && value.constructor === Color)) { - this._values[key] = value = Color.read( - [value], 0, 0, { readNull: true, clone: true }); + this._values[key] = value = Color.read([value], 0, + { readNull: true, clone: true }); if (value) value._owner = this._item; } @@ -9309,14 +9774,20 @@ var Style = Base.extend(new function() { return !!this.getShadowColor() && this.getShadowBlur() > 0; }, + getFontStyle: function() { + var size = this.getFontSize(); + return this.getFontWeight() + + ' ' + size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + + setFont: '#setFontFamily', + getLeading: function getLeading() { var leading = getLeading.base.call(this); return leading != null ? leading : this.getFontSize() * 1.2; - }, - - getFontStyle: function() { - var size = this.getFontSize(); - return size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + this.getFont(); } }); @@ -9347,6 +9818,23 @@ var DomElement = new function() { return res; } + 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 { create: function(nodes, parent) { var isArray = Array.isArray(nodes), @@ -9488,13 +9976,17 @@ var DomElement = new function() { this.getBounds(el, true)); }, - getPrefixValue: function(el, name) { - var value = el[name], - prefixes = ['webkit', 'moz', 'ms', 'o'], - suffix = name[0].toUpperCase() + name.substring(1); - for (var i = 0; i < 4 && value == null; i++) - value = el[prefixes[i] + suffix]; - return value; + 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); + } } }; }; @@ -9502,25 +9994,19 @@ var DomElement = new function() { var DomEvent = { add: function(el, events) { for (var type in events) { - var func = events[type]; - if (el.addEventListener) { - el.addEventListener(type, func, false); - } else if (el.attachEvent) { - el.attachEvent('on' + type, func.bound = function() { - func.call(el, window.event); - }); - } + 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]; - if (el.removeEventListener) { - el.removeEventListener(type, func, false); - } else if (el.detachEvent) { - el.detachEvent('on' + type, func.bound); - } + 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); } }, @@ -9540,36 +10026,23 @@ var DomEvent = { 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))); }, - preventDefault: function(event) { - if (event.preventDefault) { - event.preventDefault(); - } else { - event.returnValue = false; - } - }, - - stopPropagation: function(event) { - if (event.stopPropagation) { - event.stopPropagation(); - } else { - event.cancelBubble = true; - } - }, - stop: function(event) { - DomEvent.stopPropagation(event); - DomEvent.preventDefault(event); + event.stopPropagation(); + event.preventDefault(); } }; DomEvent.requestAnimationFrame = new function() { - var nativeRequest = DomElement.getPrefixValue(window, - 'requestAnimationFrame'), + var nativeRequest = DomElement.getPrefixed(window, 'requestAnimationFrame'), requested = false, callbacks = [], focused = true, @@ -9625,16 +10098,27 @@ var View = Base.extend(Callback, { this._project = paper.project; 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._viewHandlers); + 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)' + }); if (PaperScope.hasAttribute(element, 'resize')) { var offset = DomElement.getOffset(element, true), that = this; size = DomElement.getViewportBounds(element) .getSize().subtract(offset); - this._windowHandlers = { + this._windowEvents = { resize: function() { if (!DomElement.isInvisible(element)) offset = DomElement.getOffset(element, true); @@ -9642,7 +10126,7 @@ var View = Base.extend(Callback, { .getSize().subtract(offset)); } }; - DomEvent.add(window, this._windowHandlers); + DomEvent.add(window, this._windowEvents); } else { size = DomElement.getSize(element); if (size.isNaN() || size.isZero()) @@ -9675,16 +10159,17 @@ var View = Base.extend(Callback, { remove: function() { if (!this._project) return false; - if (View._focused == this) + 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._viewHandlers); - DomEvent.remove(window, this._windowHandlers); + DomEvent.remove(this._element, this._viewEvents); + DomEvent.remove(window, this._windowEvents); this._element = this._project = null; this.detach('frame'); + this._animate = false; this._frameItems = {}; return true; }, @@ -9692,13 +10177,11 @@ var View = Base.extend(Callback, { _events: { onFrame: { install: function() { - this._animate = true; - if (!this._requested) - this._requestFrame(); + this.play(); }, uninstall: function() { - this._animate = false; + this.pause(); } }, @@ -9735,7 +10218,7 @@ var View = Base.extend(Callback, { if (this._stats) this._stats.update(); this._handlingFrame = false; - this.draw(true); + this.update(); }, _animateItem: function(item, animate) { @@ -9766,40 +10249,48 @@ var View = Base.extend(Callback, { } }, - _redraw: function() { - this._project._needsRedraw = true; + _update: function() { + this._project._needsUpdate = true; if (this._handlingFrame) return; if (this._animate) { this._handleFrame(); } else { - this.draw(); + this.update(); } }, _changed: function(flags) { if (flags & 1) - this._project._needsRedraw = true; + this._project._needsUpdate = true; }, _transform: function(matrix) { this._matrix.concatenate(matrix); this._bounds = null; - this._redraw(); + 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(size) { - size = Size.read(arguments); - var delta = size.subtract(this._viewSize); + setViewSize: function() { + var size = Size.read(arguments), + delta = size.subtract(this._viewSize); if (delta.isZero()) return; this._viewSize.set(size.width, size.height); @@ -9809,7 +10300,7 @@ var View = Base.extend(Callback, { size: size, delta: delta }); - this._redraw(); + this._update(); }, _setViewSize: function(size) { @@ -9826,11 +10317,11 @@ var View = Base.extend(Callback, { }, getSize: function() { - return this.getBounds().getSize(arguments[0]); + return this.getBounds().getSize(); }, getCenter: function() { - return this.getBounds().getCenter(arguments[0]); + return this.getBounds().getCenter(); }, setCenter: function(center) { @@ -9856,13 +10347,27 @@ var View = Base.extend(Callback, { 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: { @@ -9903,80 +10408,107 @@ var View = Base.extend(Callback, { } } - function mousedown(event) { + 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; - if (view._onMouseDown) - view._onMouseDown(event, point); - if (tool = view._scope._tool) - tool._onHandleEvent('mousedown', point, event); - view.draw(true); - } + view._handleEvent('mousedown', point, event); + if (tool = view._scope.tool) + tool._handleEvent('mousedown', point, event); + view.update(); + }; - function mousemove(event) { - var view; + docEvents[mousemove] = function(event) { + var view = View._focused; if (!dragging) { - view = getView(event); - if (view) { - prevFocus = View._focused; - View._focused = tempFocus = view; - } else if (tempFocus && tempFocus == View._focused) { - View._focused = prevFocus; + 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 = view || View._focused)) - return; - var point = event && viewToProject(view, event); - if (view._onMouseMove) - view._onMouseMove(event, point); - if (tool = view._scope._tool) { - if (tool._onHandleEvent(dragging && tool.responds('mousedrag') - ? 'mousedrag' : 'mousemove', point, event)) - DomEvent.stop(event); + if (view) { + var point = viewToProject(view, event); + if (dragging || view.getBounds().contains(point)) + tool = handleMouseMove(view, point, event); } - view.draw(true); - } + }; - function mouseup(event) { + docEvents[mouseup] = function(event) { var view = View._focused; if (!view || !dragging) return; var point = viewToProject(view, event); curPoint = null; dragging = false; - if (view._onMouseUp) - view._onMouseUp(event, point); - if (tool && tool._onHandleEvent('mouseup', point, event)) - DomEvent.stop(event); - view.draw(true); - } + view._handleEvent('mouseup', point, event); + if (tool) + tool._handleEvent('mouseup', point, event); + view.update(); + }; - function selectstart(event) { - if (dragging) - DomEvent.stop(event); - } - - DomEvent.add(document, { - mousemove: mousemove, - mouseup: mouseup, - touchmove: mousemove, - touchend: mouseup, - selectstart: selectstart, - scroll: updateFocus - }); + DomEvent.add(document, docEvents); DomEvent.add(window, { load: updateFocus }); return { - _viewHandlers: { - mousedown: mousedown, - touchstart: mousedown, - selectstart: selectstart - }, + _viewEvents: viewEvents, + + _handleEvent: function() {}, statics: { updateFocus: updateFocus @@ -9998,12 +10530,12 @@ var CanvasView = View.extend({ } this._context = canvas.getContext('2d'); this._eventCounters = {}; - this._ratio = 1; + this._pixelRatio = 1; if (PaperScope.getAttribute(canvas, 'hidpi') !== 'off') { var deviceRatio = window.devicePixelRatio || 1, - backingStoreRatio = DomElement.getPrefixValue(this._context, + backingStoreRatio = DomElement.getPrefixed(this._context, 'backingStorePixelRatio') || 1; - this._ratio = deviceRatio / backingStoreRatio; + this._pixelRatio = deviceRatio / backingStoreRatio; } View.call(this, canvas); }, @@ -10011,26 +10543,26 @@ var CanvasView = View.extend({ _setViewSize: function(size) { var width = size.width, height = size.height, - ratio = this._ratio, + pixelRatio = this._pixelRatio, element = this._element, style = element.style; - element.width = width * ratio; - element.height = height * ratio; - if (ratio !== 1) { + element.width = width * pixelRatio; + element.height = height * pixelRatio; + if (pixelRatio !== 1) { style.width = width + 'px'; style.height = height + 'px'; - this._context.scale(ratio, ratio); + this._context.scale(pixelRatio, pixelRatio); } }, - draw: function(checkRedraw) { - if (checkRedraw && !this._project._needsRedraw) + update: function() { + if (!this._project._needsUpdate) return false; var ctx = this._context, size = this._viewSize; ctx.clearRect(0, 0, size.width + 1, size.height + 1); - this._project.draw(ctx, this._matrix, this._ratio); - this._project._needsRedraw = false; + this._project.draw(ctx, this._matrix, this._pixelRatio); + this._project._needsUpdate = false; return true; } }, new function() { @@ -10041,86 +10573,98 @@ var CanvasView = View.extend({ downItem, lastItem, overItem, - hasDrag, - doubleClick, + dragItem, + dblClick, clickTime; - function callEvent(type, event, point, target, lastPoint, bubble) { + function callEvent(view, type, event, point, target, lastPoint) { var item = target, mouseEvent; - while (item) { - if (item.responds(type)) { - if (!mouseEvent) + + function call(obj) { + if (obj.responds(type)) { + if (!mouseEvent) { mouseEvent = new MouseEvent(type, event, point, target, lastPoint ? point.subtract(lastPoint) : null); - if (item.fire(type, mouseEvent) - && (!bubble || mouseEvent._stopped)) - return false; + } + if (obj.fire(type, mouseEvent) && mouseEvent.isStopped) { + event.preventDefault(); + return true; + } } + } + + while (item) { + if (call(item)) + return true; item = item.getParent(); } - return true; - } - - function handleEvent(view, type, event, point, lastPoint) { - if (view._eventCounters[type]) { - var project = view._project, - hit = project.hitTest(point, { - tolerance: project.options.hitTolerance || 0, - fill: true, - stroke: true - }), - item = hit && hit.item; - if (item) { - if (type === 'mousemove' && item != overItem) - lastPoint = point; - if (type !== 'mousemove' || !hasDrag) - callEvent(type, event, point, item, lastPoint); - return item; - } - } + if (call(view)) + return true; + return false; } return { - _onMouseDown: function(event, point) { - var item = handleEvent(this, 'mousedown', event, point); - doubleClick = lastItem == item && (Date.now() - clickTime < 300); - downItem = lastItem = item; - downPoint = lastPoint = overPoint = point; - hasDrag = downItem && downItem.responds('mousedrag'); - }, - - _onMouseUp: function(event, point) { - var item = handleEvent(this, 'mouseup', event, point); - if (hasDrag) { - if (lastPoint && !lastPoint.equals(point)) - callEvent('mousedrag', event, point, downItem, lastPoint); - if (item != downItem) { - overPoint = point; - callEvent('mousemove', event, point, item, overPoint); + _handleEvent: function(type, point, event) { + if (!this._eventCounters[type]) + return; + var project = this._project, + hit = project.hitTest(point, { + tolerance: this._scope.settings.hitTolerance, + 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; } - if (item === downItem) { - clickTime = Date.now(); - if (!doubleClick - || callEvent('doubleclick', event, downPoint, item)) - callEvent('click', event, downPoint, item); - doubleClick = false; - } - downItem = null; - hasDrag = false; - }, - - _onMouseMove: function(event, point) { - if (downItem) - callEvent('mousedrag', event, point, downItem, lastPoint); - var item = handleEvent(this, 'mousemove', event, point, overPoint); - lastPoint = overPoint = point; - if (item !== overItem) { - callEvent('mouseleave', event, point, overItem); - overItem = item; - callEvent('mouseenter', event, point, item); - } + return stopped; } }; }); @@ -10132,14 +10676,17 @@ var Event = Base.extend({ this.event = event; }, + isPrevented: false, + isStopped: false, + preventDefault: function() { - this._prevented = true; - DomEvent.preventDefault(this.event); + this.isPrevented = true; + this.event.preventDefault(); }, stopPropagation: function() { - this._stopped = true; - DomEvent.stopPropagation(this.event); + this.isStopped = true; + this.event.stopPropagation(); }, stop: function() { @@ -10222,7 +10769,7 @@ var Key = new function() { type = down ? 'keydown' : 'keyup', view = View._focused, scope = view && view.isVisible() && view._scope, - tool = scope && scope._tool, + tool = scope && scope.tool, name; keyMap[key] = down; if (specialKey && (name = Base.camelize(specialKey)) in modifiers) @@ -10236,7 +10783,7 @@ var Key = new function() { paper = scope; tool.fire(type, new KeyEvent(down, key, character, event)); if (view) - view.draw(true); + view.update(); } } @@ -10402,7 +10949,7 @@ var ToolEvent = Event.extend({ var Tool = PaperScopeItem.extend({ _class: 'Tool', _list: 'tools', - _reference: '_tool', + _reference: 'tool', _events: [ 'onActivate', 'onDeactivate', 'onEditOptions', 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', 'onKeyDown', 'onKeyUp' ], @@ -10509,7 +11056,7 @@ var Tool = PaperScopeItem.extend({ && this.fire(type, new ToolEvent(this, type, event)); }, - _onHandleEvent: function(type, point, event) { + _handleEvent: function(type, point, event) { paper = this._scope; var called = false; switch (type) { @@ -10547,6 +11094,8 @@ var Tool = PaperScopeItem.extend({ } break; } + if (called) + event.preventDefault(); return called; } @@ -10577,19 +11126,19 @@ var Http = { var CanvasProvider = { canvases: [], - getCanvas: function(width, height, ratio) { + getCanvas: function(width, height, pixelRatio) { var canvas, init = true; if (typeof width === 'object') { - ratio = height; + pixelRatio = height; height = width.height; width = width.width; } - if (!ratio) { - ratio = 1; - } else if (ratio !== 1) { - width *= ratio; - height *= ratio; + if (!pixelRatio) { + pixelRatio = 1; + } else if (pixelRatio !== 1) { + width *= pixelRatio; + height *= pixelRatio; } if (this.canvases.length) { canvas = this.canvases.pop(); @@ -10605,8 +11154,8 @@ var CanvasProvider = { canvas.height = height; } ctx.save(); - if (ratio !== 1) - ctx.scale(ratio, ratio); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); return canvas; }, @@ -10876,7 +11425,8 @@ var SVGStyles = Base.each({ miterLimit: ['stroke-miterlimit', 'number'], dashArray: ['stroke-dasharray', 'array'], dashOffset: ['stroke-dashoffset', 'number'], - font: ['font-family', 'string'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], fontSize: ['font-size', 'number'], justification: ['text-anchor', 'lookup', { left: 'start', @@ -10940,22 +11490,23 @@ new function() { attrs[center ? 'cy' : 'y'] = point.y; trans = null; } - if (matrix.isIdentity()) - return attrs; - 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(',') + ')'; + 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 (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } } return attrs; } @@ -11334,7 +11885,7 @@ new function() { currentStyle = project._currentStyle, children = []; if (!isClip) { - item._transformContent = false; + item._applyMatrix = false; item = applyAttributes(item, node, isRoot); project._currentStyle = item._style.clone(); } @@ -11358,11 +11909,14 @@ new function() { } function importPoly(node, type) { - var path = new Path(), - points = node.points; - path.moveTo(points.getItem(0)); - for (var i = 1, l = points.numberOfItems; i < l; i++) - path.lineTo(points.getItem(i)); + 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; @@ -11370,11 +11924,10 @@ new function() { function importPath(node) { var data = node.getAttribute('d'), - path = data.match(/m/gi).length > 1 - ? new CompoundPath() - : new Path(); - path.setPathData(data); - return path; + 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) { @@ -11516,10 +12069,10 @@ new function() { matrix.scale(v); break; case 'skewX': + matrix.skew(v[0], 0); + break; case 'skewY': - var value = Math.tan(v[0] * Math.PI / 180), - isX = command == 'skewX'; - matrix.shear(isX ? value : 0, isX ? 0 : value); + matrix.skew(0, v[0]); break; } } @@ -11665,7 +12218,7 @@ new function() { view = scope.project && scope.project.view; if (onLoad) onLoad.call(this, item); - view.draw(true); + view.update(); } if (isRoot) { @@ -11722,7 +12275,7 @@ new function() { }); }; -paper = new (PaperScope.inject(new Base(Base.exports, { +paper = new (PaperScope.inject(Base.exports, { enumerable: true, Base: Base, Numerical: Numerical, @@ -11730,7 +12283,7 @@ paper = new (PaperScope.inject(new Base(Base.exports, { DomEvent: DomEvent, Http: Http, Key: Key -})))(); +}))(); if (typeof define === 'function' && define.amd) define('paper', paper); diff --git a/dist/paper-core.min.js b/dist/paper-core.min.js index d4d17eb3..c5f3d29b 100644 --- a/dist/paper-core.min.js +++ b/dist/paper-core.min.js @@ -1,37 +1,37 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * 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(t){var e=new function(){function i(e,i,n,r,o,h){function u(r,s,h,u){var s=s||(s=l(i,r))&&(s.get?s:s.value);"string"==typeof s&&"#"===s[0]&&(s=e[s.substring(1)]||s);var _,f="function"==typeof s,g=s,v=o||f?s&&s.get?r in e:e[r]:null;!(h||s!==t&&i.hasOwnProperty(r))||o&&v||(f&&v&&(s.base=v),f&&c&&0===s.length&&(_=r.match(/^(get|is)(([A-Z])(.*))$/))&&c.push([_[3].toLowerCase()+_[4],_[2]]),g&&!f&&g.get&&"function"==typeof g.get&&0===g.get.length||(g={value:g,writable:!0}),(l(e,r)||{configurable:!0}).configurable&&(g.configurable=!0,g.enumerable=n),d(e,r,g)),!u||!f||o&&u[r]||(u[r]=function(t){return t&&e[r].apply(t,a.call(arguments,1))})}var c;if(i){c=[];for(var _ in i)i.hasOwnProperty(_)&&!s.test(_)&&u(_,null,!0,h);u("toString"),u("valueOf");for(var f=0,g=c.length;g>f;f++){var v=c[f],m=v[1];u(v[0],{get:e["get"+m]||e["is"+m],set:e["set"+m]},!0)}}return e}function n(t,e,i){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?o:h).call(t,e,i=i||t),i}function r(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);return t}var s=/^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/,a=[].slice,o=[].forEach||function(t,e){for(var i=0,n=this.length;n>i;i++)t.call(e,this[i],i,this)},h=function(t,e){for(var i in this)this.hasOwnProperty(i)&&t.call(e,this[i],i,this)},u=Object.create||function(t){return{__proto__:t}},l=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},c=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},d=function(t,e,i){return delete t[e],c(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=this.prototype,n=Object.getPrototypeOf(e).constructor,r=t.statics===!0?t:t.statics;r!=t&&i(e,t,t.enumerable,n&&n.prototype,t.preserve,t.generics&&this),i(this,r,!0,n,t.preserve)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e=this,n=0,r=arguments.length;r>n&&!(t=arguments[n].initialize);n++);return t=t||function(){e.apply(this,arguments)},t.prototype=u(this.prototype),d(t.prototype,"constructor",{value:t,writable:!0,configurable:!0}),i(t,this,!0),arguments.length?this.inject.apply(t,arguments):t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++)i(this,arguments[t],arguments[t].enumerable);return this},extend:function(){var t=u(this);return t.inject.apply(t,arguments)},each:function(t,e){return n(this,t,e)},clone:function(){return new this.constructor(this)},statics:{each:n,create:u,define:d,describe:l,copy: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(){for(var e=0,i=arguments.length;i>e;e++)if(arguments[e]!==t)return arguments[e];return null}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({generics:!0,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?a.instance.number(t):"string"===i?"'"+t+"'":t))}},[]).join(", ")+" }"},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(i,n){if(i&&e.isPlainObject(i)){var r=i._filtering||i;for(var s in r)if(s in this&&r.hasOwnProperty(s)&&(!n||!n[s])){var a=i[s];a!==t&&(this[s]=a)}return!0}},statics:{exports:{},extend:function ie(){var t=ie.base.apply(this,arguments),i=t.prototype._class;return i&&!e.exports[i]&&(e.exports[i]=t),t},equals:function(t,i){function n(t,e){for(var i in t)if(t.hasOwnProperty(i)&&!e.hasOwnProperty(i))return!1;return!0}if(t===i)return!0;if(t&&t.equals)return t.equals(i);if(i&&i.equals)return i.equals(t);if(Array.isArray(t)&&Array.isArray(i)){if(t.length!==i.length)return!1;for(var r=0,s=t.length;s>r;r++)if(!e.equals(t[r],i[r]))return!1;return!0}if(t&&"object"==typeof t&&i&&"object"==typeof i){if(!n(t,i)||!n(i,t))return!1;for(var r in t)if(t.hasOwnProperty(r)&&!e.equals(t[r],i[r]))return!1;return!0}return!1},read:function(t,i,n,r){if(this===e){var s=this.peek(t,i);return t._index++,t.__read=1,s}var a=this.prototype,o=a._readIndex,h=i||o&&t._index||0;n||(n=t.length-h);var u=t[h];return u instanceof this||r&&r.readNull&&null==u&&1>=n?(o&&(t._index=h+1),u&&r&&r.clone?u.clone():u):(u=e.create(this.prototype),o&&(u.__read=!0),r&&(u.__options=r),u=u.initialize.apply(u,h>0||ns;s++)r.push(Array.isArray(n=t[s])?this.read(n,0,0,i):this.read(t,s,1,i));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){return this.isPlainObject(t)||Array.isArray(t)},serialize:function(t,i,n,r){i=i||{};var s,o=!r;if(o&&(i.formatter=new a(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){s=t._serialize(i,r);var h=t._class;!h||n||s._compact||s[0]===h||s.unshift(h)}else if(Array.isArray(t)){s=[];for(var u=0,l=t.length;l>u;u++)s[u]=e.serialize(t[u],i,n,r);n&&(s._compact=!0)}else if(e.isPlainObject(t)){s={};for(var u in t)t.hasOwnProperty(u)&&(s[u]=e.serialize(t[u],i,n,r))}else s="number"==typeof t?i.formatter.number(t,i.precision):t;return o&&r.length>0?[["dictionary",r.definitions],s]:s},deserialize:function(t,i,n){var r=t;if(n=n||{},Array.isArray(t)){var s=t[0],a="dictionary"===s;if(!a){if(n.dictionary&&1==t.length&&/^#/.test(s))return n.dictionary[s];s=e.exports[s]}r=[];for(var o=s?1:0,h=t.length;h>o;o++)r.push(e.deserialize(t[o],i,n));if(a)n.dictionary=r[0];else if(s){var u=r;i?r=i(s,u):(r=e.create(s.prototype),s.apply(r,u))}}else if(e.isPlainObject(t)){r={};for(var l in t)r[l]=e.deserialize(t[l],i,n)}return r},exportJSON:function(t,i){return JSON.stringify(e.serialize(t,i))},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 y&&(!(r instanceof x)||s)){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++)delete u[o]._index;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 n={attach:function(i,n){if("string"!=typeof i)return e.each(i,function(t,e){this.attach(e,t)},this),t;var r=this._eventTypes[i];if(r){var s=this._handlers=this._handlers||{};s=s[i]=s[i]||[],-1==s.indexOf(n)&&(s.push(n),r.install&&1==s.length&&r.install.call(this,i))}},detach:function(i,n){if("string"!=typeof i)return e.each(i,function(t,e){this.detach(e,t)},this),t;var r,s=this._eventTypes[i],a=this._handlers&&this._handlers[i];s&&a&&(!n||-1!=(r=a.indexOf(n))&&1==a.length?(s.uninstall&&s.uninstall.call(this,i),delete this._handlers[i]):-1!=r&&a.splice(r,1))},once:function(t,e){this.attach(t,function(){e.apply(this,arguments),this.detach(t,e)})},fire:function(t,e){function i(){for(var t in n)n[t].apply(o,r)===!1&&e&&e.stop&&e.stop()}var n=this._handlers&&this._handlers[t];if(!n)return!1;var r=[].slice.call(arguments,1),s=paper.PaperScript,a=s&&s.handleException,o=this;if(a)try{i()}catch(h){a(h)}else i();return!0},responds:function(t){return!(!this._handlers||!this._handlers[t])},on:"#attach",off:"#detach",trigger:"#fire",statics:{inject:function ne(){for(var t=0,i=arguments.length;i>t;t++){var n=arguments[t],r=n._events;if(r){var s={};e.each(r,function(t,i){var r="string"==typeof t,a=r?t:i,o=e.capitalize(a),h=a.substring(2).toLowerCase();s[h]=r?{}:t,a="_"+a,n["get"+o]=function(){return this[a]},n["set"+o]=function(t){t?this.attach(h,t):this[a]&&this.detach(h,this[a]),this[a]=t}}),n._eventTypes=s}ne.base.call(this,n)}return this}}},r=e.extend({_class:"PaperScope",initialize:function re(t){if(paper=this,this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=t&&(t.getAttribute("id")||t.src)||"paperscope-"+re._id++,t&&t.setAttribute("id",this._id),re._scopes[this._id]=this,!this.support){var e=K.getContext(1,1);re.prototype.support={nativeDash:"setLineDash"in e||"mozDash"in e,nativeBlendModes:Q.nativeModes},K.release(e)}},version:"0.9.15",getView:function(){return this.project&&this.project.view},getTool:function(){return this._tool||(this._tool=new U),this._tool},getPaper:function(){return this},evaluate:function(t){var e=paper.PaperScript.evaluate(t,this);return Z.updateFocus(),e},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)/^(version|_id)/.test(n)||(t[n]=this[n])},setup:function(t){return paper=this,this.project=new m(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 r._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"object"==typeof t&&(t=t.getAttribute("id")),this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),s=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.fire("deactivate"),this._scope[this._reference]=this,this.fire("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)}}),a=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},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)}});a.instance=new a;var o=new function(){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]],i=[[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,r=Math.sqrt,s=Math.pow,a=Math.cos,h=Math.PI;return{TOLERANCE:1e-5,EPSILON:1e-11,KAPPA:4*(r(2)-1)/3,isZero:function(t){return n(t)<=o.EPSILON},integrate:function(t,n,r,s){for(var a=e[s-2],o=i[s-2],h=.5*(r-n),u=h+n,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,i,r,s,a,o){for(var h=0;a>h;h++){var u=t(i),l=u/e(i);if(n(l)0?(s=i,i=r>=c?.5*(r+s):c):(r=i,i=c>=s?.5*(r+s):c)}},solveQuadratic:function(e,i,s,a,h,u){function l(t){return(d||t>_&&f>t)&&(a[g++]=h>t?h:t>u?u:t),g}var c=o.EPSILON,d=h===t,_=h-c,f=u+c,g=0;if(n(e)=c?l(-s/i):n(s)p)return 0;var y=p>m?r(p-m):0;return l(y-v),y>0&&l(-y-v),g},solveCubic:function(e,i,u,l,c,d,_){function f(t){return(v||t>m&&p>t)&&(c[y++]=d>t?d:t>_?_:t),y}var g=o.EPSILON;if(n(e)0?1:-1;return f(2*-z*P-i),f(z*P-i)}if(0>S){var P=r(x),M=Math.acos(b/(P*P*P))/3,I=-2*P,k=2*h/3;return f(I*a(M)-i),f(I*a(M+k)-i),f(I*a(M-k)-i)}var A=(b>0?-1:1)*s(n(b)+r(S),1/3);return f(A+x/A-i)}}},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 t===this||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=a.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)]},add:function(t){return t=h.read(arguments),new h(this.x+t.x,this.y+t.y)},subtract:function(t){return t=h.read(arguments),new h(this.x-t.x,this.y-t.y)},multiply:function(t){return t=h.read(arguments),new h(this.x*t.x,this.y*t.y)},divide:function(t){return t=h.read(arguments),new h(this.x/t.x,this.y/t.y)},modulo:function(t){return t=h.read(arguments),new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},transform:function(t){return t?t._transformPoint(this):this},getDistance:function(t,e){t=h.read(arguments);var i=t.x-this.x,n=t.y-this.y,r=i*i+n*n;return e?r:Math.sqrt(r)},getLength:function(){var t=this.x*this.x+this.y*this.y;return arguments.length&&arguments[0]?t:Math.sqrt(t)},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();o.isZero(i)&&this.getAngle(),this.set(this.x*i,this.y*i)}return this},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 r._angle=this._angle,r},getAngle:function(){return 180*this.getAngleInRadians(arguments[0])/Math.PI},setAngle:function(t){if(t=this._angle=t*Math.PI/180,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}return this},getAngleInRadians:function(){if(arguments[0]===t)return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x);var e=h.read(arguments),i=this.getLength()*e.getLength();return o.isZero(i)?0/0:Math.acos(this.dot(e)/i)},getAngleInDegrees:function(){return this.getAngle(arguments[0])},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3},getDirectedAngle:function(t){return t=h.read(arguments),180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},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.y*r+i.x*n),e?i.add(e):i},isInside:function(t){return t.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 c(this.width,this.height)},toString:function(){var t=a.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(t){return t=c.read(arguments),new c(this.width+t.width,this.height+t.height)},subtract:function(t){return t=c.read(arguments),new c(this.width-t.width,this.height-t.height)},multiply:function(t){return t=c.read(arguments),new c(this.width*t.width,this.height*t.height)},divide:function(t){return t=c.read(arguments),new c(this.width/t.width,this.height/t.height)},modulo:function(t){return t=c.read(arguments),new c(this.width%t.width,this.height%t.height)},negate:function(){return new c(-this.width,-this.height)},isZero:function(){return o.isZero(this.width)&&o.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new c(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new c(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new c(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new c(e(this.width),e(this.height))}},{})),d=c.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)}}),_=e.extend({_class:"Rectangle",_readIndex:!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"),l=e.peek(arguments);if(this.x=u.x,this.y=u.y,l&&l.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 _=c.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 _(this.x,this.y,this.width,this.height)},equals:function(t){return e.isPlainValue(t)&&(t=_.read(arguments)),t===this||t&&this.x===t.x&&this.y===t.y&&this.width===t.width&&this.height===t.height||!1},toString:function(){var t=a.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(){return new(arguments[0]?h:u)(this.x,this.y,this,"setPoint")},setPoint:function(t){t=h.read(arguments),this.x=t.x,this.y=t.y},getSize:function(){return new(arguments[0]?c:d)(this.width,this.height,this,"setSize")},setSize:function(t){t=c.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(){return new(arguments[0]?h:u)(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(t){return t=h.read(arguments),this.setCenterX(t.x),this.setCenterY(t.y),this},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(_.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(t){return t=_.read(arguments),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(t){t=_.read(arguments);var 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 _(e,i,n-e,r-i)},unite:function(t){t=_.read(arguments);var 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 _(e,i,n-e,r-i)},include:function(t){t=h.read(arguments);var 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 _(e,i,n-e,r-i)},expand:function(e,i){return i===t&&(i=e),new _(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)}},new function(){return 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(){return new(arguments[0]?h:u)(this[a](),this[o](),this,_)},this[_]=function(t){t=h.read(arguments),this[l](t.x),this[c](t.y)}},{})}),f=_.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=_.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),delete this._dontNotify,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 se(t){var e=arguments.length,i=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof se?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(){this._owner&&this._owner._changed(5)},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=a.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(){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,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},translate:function(t){t=h.read(arguments);var 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},rotate:function(t,e){e=h.read(arguments,1),t=t*Math.PI/180;var 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,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},concatenate:function(t){var e=this._a,i=this._b,n=this._c,r=this._d;return this._a=t._a*e+t._c*i,this._b=t._b*e+t._d*i,this._c=t._a*n+t._c*r,this._d=t._b*n+t._d*r,this._tx+=t._tx*e+t._ty*i,this._ty+=t._tx*n+t._ty*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;return this._a=t._a*e+t._b*n,this._b=t._a*i+t._b*r,this._c=t._c*e+t._d*n,this._d=t._c*i+t._d*r,this._tx=t._a*s+t._b*a+t._tx,this._ty=t._c*s+t._d*a+t._ty,this._changed(),this},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,i,n,r){return arguments.length<5?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,i,n,r)},_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,n,r){for(var s=e,a=n,o=s+2*r;o>s;){var h=t[s++],u=t[s++];i[a++]=h*this._a+u*this._b+this._tx,i[a++]=h*this._c+u*this._d+this._ty}return i},_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,0,s,0,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 _),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)&&!o.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(o.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 a=Math.sqrt(i*i+n*n);return i/=a,n/=a,s/=a,e*i>t*n&&(t=-t,e=-e,s=-s,r=-r),{translation:this.getTranslation(),scaling:new h(r,a),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)},setTranslation:function(){var t=h.read(arguments);this._tx=t.x,this._ty=t.y,this._changed()},getScaling:function(){return(this.decompose()||{}).scaling},setScaling:function(){var t=this.getScaling();if(null!=t){var e=h.read(arguments);(this._owner||this).scale(e.x/t.x,e.y/t.y)}},getRotation:function(){return(this.decompose()||{}).rotation},setRotation:function(t){var e=this.getRotation();null!=e&&(this._owner||this).rotate(t-e)},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)}},new function(){return e.each({scaleX:"_a",scaleY:"_d",translateX:"_tx",translateY:"_ty",shearX:"_b",shearY:"_c"},function(t,i){i=e.capitalize(i),this["get"+i]=function(){return this[t]},this["set"+i]=function(e){this[t]=e,this._changed()}},{})}),v=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 v.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return v.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(v.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,i,n,r,s,a,u,l,c){l||(i-=t,n-=e,a-=r,u-=s);var d=u*i-a*n;if(!o.isZero(d)){var _=t-r,f=e-s,g=(a*f-u*_)/d,v=(i*f-n*_)/d;if((c||g>=0&&1>=g)&&(c||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,a){a||(i-=t,n-=e);var o=n/i,h=e-o*t;return(s-o*r-h)/Math.sqrt(o*o+1)}}}),m=s.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){s.call(this,!0),this.layers=[],this.symbols=[],this._currentStyle=new q,this.activeLayer=new x,t&&(this.view=t instanceof Z?t:Z.create(t)),this._selectedItems={},this._selectedItemCount=0,this._drawCount=0,this.options={}},_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 this.layers.length<=1&&(!this.activeLayer||this.activeLayer.isEmpty())},remove:function ae(){return ae.base.call(this)?(this.view&&this.view.remove(),!0):!1},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},addChild:function(t){return t instanceof x?(e.splice(this.layers,[t]),this.activeLayer||(this.activeLayer=t)):t instanceof y?(this.activeLayer||this.addChild(new x({insert:!1}))).addChild(t):t=null,t},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var i=this._selectedItems[e];i.isInserted()&&t.push(i)}return t},_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(t,i){t=h.read(arguments),i=P.getOptions(e.read(arguments));for(var n=this.layers.length-1;n>=0;n--){var r=this.layers[n].hitTest(t,i);if(r)return r}return null}},new function(){function t(t,e,i){for(var n=t.layers,r=i&&[],s=0,a=n.length;a>s;s++){var o=n[s][i?"getItems":"getItem"](e);if(i)r.push.apply(r,o);else if(o)return o}return i?r:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{importJSON:function(t){this.activate();var i=this.activeLayer;return e.importJSON(t,i&&i.isEmpty()&&i)},draw:function(t,i,n){this._drawCount++,t.save(),i.applyToContext(t);for(var r=new e({offset:new h(0,0),ratio:n,transforms:[i],trackTransforms:!0}),s=0,a=this.layers.length;a>s;s++)this.layers[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;for(var o in this._selectedItems){var u=this._selectedItems[o];if(u._drawCount===this._drawCount&&(u._drawSelected||u._boundsSelected)){var l=u.getSelectedColor()||u.getLayer().getSelectedColor();t.strokeStyle=t.fillStyle=l?l.toCanvasStyle(t):"#009dec";var c=u._globalMatrix;if(u._drawSelected&&u._drawSelected(t,c),u._boundsSelected){var d=c._transformCorners(u._getBounds("getBounds"));t.beginPath();for(var s=0;8>s;s++)t[0===s?"moveTo":"lineTo"](d[s],d[++s]);t.closePath(),t.stroke();for(var s=0;8>s;s++)t.beginPath(),t.rect(d[s]-2,d[++s]-2,4,4),t.fill()}}}t.restore()}}}),p=e.extend({_class:"Symbol",initialize:function oe(t,e){this._id=oe._id=(oe._id||0)+1,this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e),this._instances={}},_serialize:function(t,i){return i.add(this,function(){return e.serialize([this._class,this._definition],t,!1,i)})},_changed:function(t){e.each(this._instances,function(e){e._changed(t)})},getDefinition:function(){return this._definition},setDefinition:function(t){t._parentSymbol&&(t=t.clone()),this._definition&&delete this._definition._parentSymbol,this._definition=t,t.remove(),t.setSelected(!1),arguments[1]||t.setPosition(new h),t._parentSymbol=this,this._changed(5)},place:function(t){return new S(this,t)},clone:function(){return new p(this._definition.clone(!1))}}),y=e.extend(n,{statics:{extend:function he(t){t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields));var i=he.base.apply(this,arguments),n=i.prototype,r=n._class;return r&&(n._type=e.hyphenate(r)),i}},_class:"Item",_transformContent:!0,_boundsSelected:!1,_serializeFields:{name:null,matrix:new g,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,e){if(this._id=y._id=(y._id||0)+1,!this._project){var i=paper.project;t&&t.insert===!1?this._setProject(i):(i.activeLayer||new x).addChild(this)}this._style=new q(this._project._currentStyle,this);var n=this._matrix=new g;return e&&n.translate(e),n._owner=this,t?this._set(t,{insert:!0}):!0},_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._project.view._eventCounters;if(i)for(var n in t)i[n]=(i[n]||0)+(t[n][e]||0)},uninstall:function(e){var i=this._project.view._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._project.view._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 w||n(this._style._defaults),[this._class,r]},_changed:function(t){var e=this._parent,i=this._project,n=this._parentSymbol;if(this._drawCount=null,4&t&&(delete this._bounds,delete this._position),e&&12&t&&e._clearBoundsCache(),2&t&&this._clearBoundsCache(),i&&(1&t&&(i._needsRedraw=!0),i._changes)){var r=i._changesById[this._id];r?r.flags|=t:(r={item:this,flags:t},i._changesById[this._id]=r,i._changes.push(r))}n&&n._changed(t)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getType:function(){return this._type},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.");if(e&&this._parent){for(var n=this._parent._children,r=this._parent._namedChildren,s=e,a=1;i&&n[e];)e=s+" "+a++;(r[e]=r[e]||[]).push(this),n[e]=this}this._name=e||t,this._changed(32)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()}},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?32:33))}},{}),{_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._children)for(var t=0,e=this._children.length;e>t;t++)if(this._children[t].isSelected())return!0;return this._selected},setSelected:function(t){if(this._children&&!arguments[1])for(var e=0,i=this._children.length;i>e;e++)this._children[e].setSelected(t);(t=!!t)!=this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(33))},_selected:!1,isFullySelected:function(){if(this._children&&this._selected){for(var t=0,e=this._children.length;e>t;t++)if(!this._children[t].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){if(this._children)for(var e=0,i=this._children.length;i>e;e++)this._children[e].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(33),this._parent&&this._parent._changed(256))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(){var t=this._position||(this._position=this.getBounds().getCenter(!0));return new(arguments[0]?h:u)(t.x,t.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){var e=this._boundsGetter,i=this._getCachedBounds("string"==typeof e?e:e&&e[t]||t,arguments[0]);return"getBounds"===t?new f(i.x,i.y,i.width,i.height,this,"setBounds"):i}},{_getCachedBounds:function(t,e,i){var n=(!e||e.equals(this._matrix))&&t;if(i&&this._parent){var r=i._id,s=this._parent._boundsCache=this._parent._boundsCache||{ids:{},list:[]};s.ids[r]||(s.list.push(i),s.ids[r]=i)}if(n&&this._bounds&&this._bounds[n])return this._bounds[n].clone();var a=this._matrix.isIdentity();e=!e||e.isIdentity()?a?null:this._matrix:a?e:e.clone().concatenate(this._matrix);var o=this._getBounds(t,e,n?this:i);return n&&(this._bounds||(this._bounds={}),this._bounds[n]=o.clone()),o},_clearBoundsCache:function(){if(this._boundsCache){for(var t=0,e=this._boundsCache.list,i=e.length;i>t;t++){var n=e[t];delete n._bounds,n!=this&&n._boundsCache&&n._clearBoundsCache()}delete this._boundsCache}},_getBounds:function(t,e,i){var n=this._children;if(!n||0==n.length)return new _;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,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 _(r,a,s-r,o-a):new _},setBounds:function(t){t=_.read(arguments);var e=this.getBounds(),i=new g,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)}}),{getMatrix:function(){return this._matrix},setMatrix:function(t){this._matrix.initialize(t),this._transformContent&&this.applyMatrix(!0),this._changed(5)},getGlobalMatrix:function(){return this._drawCount===this._project._drawCount&&this._globalMatrix||null},getTransformContent:function(){return this._transformContent},setTransformContent:function(t){this._transformContent=t,t&&this.applyMatrix()},getProject:function(){return this._project},_setProject:function(t){if(this._project!=t){var e=this.responds("frame");if(e&&this._animateItem(!1),this._project=t,e&&this._animateItem(!0),this._children)for(var i=0,n=this._children.length;n>i;i++)this._children[i]._setProject(t)}},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof x)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},isInserted:function(){return this._parent?this._parent.isInserted():!1},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({insert:!1}),t)},_clone:function(i,n){if(i.setStyle(this._style),this._children)for(var r=0,s=this._children.length;s>r;r++)i.addChild(this._children[r].clone(!1),!0);(n||n===t)&&i.insertAbove(this);for(var a=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],r=0,s=a.length;s>r;r++){var o=a[r];this.hasOwnProperty(o)&&(i[o]=this[o])}return i._matrix.initialize(this._matrix),i._data=this._data?e.clone(this._data):null,i.setSelected(this._selected),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||72)/72,r=i.getTopLeft().floor(),s=i.getBottomRight().ceil();size=new c(s.subtract(r)),canvas=K.getCanvas(size),ctx=canvas.getContext("2d"),matrix=(new g).scale(n).translate(r.negate()),ctx.save(),matrix.applyToContext(ctx),this.draw(ctx,new e({transforms:[matrix]})),ctx.restore();var a=new C({canvas:canvas,insert:!1});return a.setPosition(r.add(size.divide(2))),a.insertAbove(this),a},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._getBounds("getBounds"))},hitTest:function(i,n){function r(r,s){var h=o["get"+s]();return i.getDistance(h)l&&!s;l++)s=r("bounds",u[l])}return(s||(s=this._children||!(n.guides&&!this._guide||n.selected&&!this._selected)?this._hitTest(i,n):null))&&s.point&&(s.point=a._matrix.transform(s.point)),s},_hitTest:function(t,e){var i=this._children;if(i){for(var n,r=i.length-1;r>=0;r--)if(n=i[r].hitTest(t,e))return n}else if(e.fill&&this.hasFill()&&this._contains(t))return new P("fill",this)},matches:function(t){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}for(var n in t)if(t.hasOwnProperty(n)){var r=this[n],s=t[n];if(s instanceof RegExp){if(!s.test(r))return!1}else if("function"==typeof s){if(!s(r))return!1}else if(e.isPlainObject(s)){if(!i(s,r))return!1}else if(!e.equals(r,s))return!1}return!0}},new function(){function t(e,i,n){for(var r=e._children,s=n&&[],a=0,o=r&&r.length;o>a;a++){var h=r[a];if(h.matches(i)){if(!n)return h;s.push(h)}var u=t(h,i,n);if(n)s.push.apply(s,u);else if(u)return u}return n?s:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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=this.insertChildren(t,[e],i);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];r&&o._type!==r?i.splice(a,1):o._remove(!0)}e.splice(s,i,t,0);for(var a=0,h=i.length;h>a;a++){var o=i[a];o._parent=this,o._setProject(this._project),o._name&&o.setName(o._name)}this._changed(7)}else i=null;return i},_insert:function(t,e,i){if(!e._parent)return null;var n=e._index+(t?1:0);return e._parent===this._parent&&n>this._index&&n--,e._parent.insertChild(n,this,i)},insertAbove:function(t,e){return this._insert(!0,t,e)},insertBelow:function(t,e){return this._insert(!1,t,e)},sendToBack:function(){return this._parent.insertChild(0,this)},bringToFront:function(){return this._parent.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];return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent._children,e=this._parent._namedChildren,i=this._name,n=e[i],r=n?n.indexOf(this):-1;-1!=r&&(t[i]==this&&delete t[i],n.splice(r,1),n.length?t[i]=n[n.length-1]:delete e[i])},_remove:function(t){return this._parent?(this._name&&this._removeNamed(),null!=this._index&&e.splice(this._parent._children,null,this._index,1),this.responds("frame")&&this._animateItem(!1),t&&this._parent._changed(7),this._parent=null,!0):!1},remove:function(){return this._remove(!0)},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(!1);return n.length>0&&this._changed(7),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(7)}},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},_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},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|compound-path)$/.test(e._type)&&t.isDescendant(e))return!0;e=e._parent}return!1},scale:function(t,e,i){return(arguments.length<2||"object"==typeof e)&&(i=e,e=t),this.transform((new g).scale(t,e,i||this.getPosition(!0)))},translate:function(){var t=new g;return this.transform(t.translate.apply(t,arguments))},rotate:function(t,e){return this.transform((new g).rotate(t,e||this.getPosition(!0)))},shear:function(t,e,i){return(arguments.length<2||"object"==typeof e)&&(i=e,e=t),this.transform((new g).shear(t,e,i||this.getPosition(!0)))},transform:function(t){var e=this._bounds,i=this._position;if(this._matrix.preConcatenate(t),(this._transformContent||arguments[1])&&this.applyMatrix(!0),this._changed(5),e&&0===t.getRotation()%90){for(var n in e){var r=e[n];t._transformBounds(r,r)}var s=this._boundsGetter,r=e[s&&s.getBounds||s||"getBounds"];r&&(this._position=r.getCenter(!0)),this._bounds=e}else i&&(this._position=t._transformPoint(i,i));return this},_applyMatrix:function(t,e){var i=this._children;if(i&&i.length>0){for(var n=0,r=i.length;r>n;n++)i[n].transform(t,e);return!0}},applyMatrix:function(t){var e=this._matrix;if(this._applyMatrix(e,!0)){var i=this._style,n=i.getFillColor(!0),r=i.getStrokeColor(!0);n&&n.transform(e),r&&r.transform(e),e.reset()}t||this._changed(5)},globalToLocal:function(){var t=this.getGlobalMatrix();return t&&t._transformPoint(h.read(arguments))},localToGlobal:function(){var t=this.getGlobalMatrix();return t&&t._inverseTransform(h.read(arguments))},fitBounds:function(t,e){t=_.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 _(new h,new c(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){if(this._visible&&0!==this._opacity){this._drawCount=this._project._drawCount;var i=e.trackTransforms,n=e.transforms,r=n[n.length-1],s=r.clone().concatenate(this._matrix);i&&n.push(this._globalMatrix=s);var a,o,h,u=this._blendMode,l=this._opacity,d="normal"===u,_=Q.nativeModes[u],f=d&&1===l||(_||d&&1>l)&&this._canComposite();if(!f){var g=this.getStrokeBounds(r);if(!g.width||!g.height)return;h=e.offset,o=e.offset=g.getTopLeft().floor(),a=t,t=K.getContext(g.getSize().ceil().add(new c(1,1)),e.ratio)}t.save(),f?(t.globalAlpha=l,_&&(t.globalCompositeOperation=u)):t.translate(-o.x,-o.y),(f?this._matrix:s).applyToContext(t),!f&&e.clipItem&&e.clipItem.draw(t,e.extend({clip:!0})),this._draw(t,e),t.restore(),i&&n.pop(),e.clip&&t.clip(),f||(Q.process(u,t,a,l,o.subtract(h).multiply(e.ratio)),K.release(t),e.offset=h)}},_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}})),w=y.extend({_class:"Group",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function ue(t){ue.base.call(this,t),2&t&&this._transformContent&&!this._matrix.isIdentity()&&this.applyMatrix(),258&t&&delete this._clipItem},_getClipItem:function(){if(this._clipItem!==t)return this._clipItem;for(var e=0,i=this._children.length;i>e;e++){var n=this._children[e];if(n._clipMask)return this._clipItem=n}return this._clipItem=null},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var i=e.clipItem=this._getClipItem();i&&i.draw(t,e.extend({clip:!0}));for(var n=0,r=this._children.length;r>n;n++){var s=this._children[n];s!==i&&s.draw(t,e)}e.clipItem=null}}),x=w.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,w.call(this,n),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function le(t){return this._parent?le.base.call(this,t):null!=this._index?(this._project.activeLayer===this&&(this._project.activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(this._project.layers,null,this._index,1),this._project._needsRedraw=!0,!0):!1},getNextSibling:function ce(){return this._parent?ce.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function de(){return this._parent?de.base.call(this):this._project.layers[this._index-1]||null},isInserted:function _e(){return this._parent?_e.base.call(this):null!=this._index},activate:function(){this._project.activeLayer=this},_insert:function fe(t,i,n){return i instanceof x&&!i._parent?(this._remove(!0),e.splice(i._project.layers,[this],i._index+(t?1:0),0),this._setProject(i._project),this):fe.base.call(this,t,i,n)}}),b=y.extend({_class:"Shape",_transformContent:!1,_boundsSelected:!0,initialize:function(t,e,i,n,r){this._shape=t,this._size=i,this._radius=n,this._initialize(r,e)},_equals:function(t){return this._shape===t._shape&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){return this._clone(new b(this._shape,this.getPosition(!0),this._size.clone(),this._radius.clone?this._radius.clone():this._radius,{insert:!1}),t)},getShape:function(){return this._shape},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=this._shape,e=c.read(arguments);if(!this._size.equals(e)){var i=e.width,n=e.height;if("rectangle"===t){var r=c.min(this._radius,e.divide(2));this._radius.set(r.width,r.height)}else"circle"===t?(i=n=(i+n)/2,this._radius=i/2):"ellipse"===t&&this._radius.set(i/2,n/2);this._size.set(i,n),this._changed(5)}},getRadius:function(){var t=this._radius;return"circle"===this._shape?t:new d(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._shape;if("circle"===e){if(t===this._radius)return;var i=2*t;this._radius=t,this._size.set(i,i)}else{if(t=c.read(arguments),this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var i=c.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)}this._changed(5)},isEmpty:function(){return!1},toPath:function(i){var n=new(O[e.capitalize(this._shape)])({center:new h,size:this._size,radius:this._radius,insert:!1});return n.setStyle(this._style),n.transform(this._matrix),(i||i===t)&&n.insertAbove(this),n},_draw:function(t,e){var i=this._style,n=i.hasFill(),r=i.hasStroke(),s=e.clip;if(n||r||s){var a=this._radius,h=this._shape;if(t.beginPath(),"circle"===h)t.arc(0,0,a,0,2*Math.PI,!0);else{var u=a.width,l=a.height,c=o.KAPPA;if("ellipse"===h){var d=u*c,_=l*c;t.moveTo(-u,0),t.bezierCurveTo(-u,-_,-d,-l,0,-l),t.bezierCurveTo(d,-l,u,-_,u,0),t.bezierCurveTo(u,_,d,l,0,l),t.bezierCurveTo(-d,l,-u,_,-u,0)}else{var f=this._size,g=f.width,v=f.height;if(0===u&&0===l)t.rect(-g/2,-v/2,g,v);else{c=1-c;var m=g/2,p=v/2,d=u*c,_=l*c;t.moveTo(-m,-p+l),t.bezierCurveTo(-m,-p+_,-m+d,-p,-m+u,-p),t.lineTo(m-u,-p),t.bezierCurveTo(m-d,-p,m,-p+_,m,-p+l),t.lineTo(m,p-l),t.bezierCurveTo(m,p-_,m-d,p,m-u,p),t.lineTo(-m+u,p),t.bezierCurveTo(-m+d,p,-m,p-_,-m,p-l)}}}t.closePath()}s||!n&&!r||(this._setStyles(t),n&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),r&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var i=new _(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 _(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._shape){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},_hitTest:function n(i,r){var s=!1;if(this.hasStroke()){var a=this._shape,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 _(this._size).setCenter(0,0),d=c.expand(h),f=c.expand(-h);s=d._containsPoint(i)&&!f._containsPoint(i)}}else"ellipse"===a&&(o=e(i,o)),s=2*Math.abs(i.getLength()-o)<=h}return s?new P("stroke",this):n.base.apply(this,arguments)}}},{statics:new function(){function t(t,i,n,r,s){return new b(t,i,n,r,e.getNamed(s))}return{Circle:function(){var i=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"radius");return t("circle",i,new c(2*n),n,arguments)},Rectangle:function(){var e=_.readNamed(arguments,"rectangle"),i=c.min(c.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),i,arguments)},Ellipse:function(){var e=b._readEllipse(arguments);return radius=e.radius,t("ellipse",e.center,radius.multiply(2),radius,arguments)},_readEllipse:function(t){var i,n;if(e.hasNamed(t,"radius"))i=h.readNamed(t,"center"),n=c.readNamed(t,"radius");else{var r=_.readNamed(t,"rectangle");i=r.getCenter(!0),n=r.getSize(!0).divide(2)}return{center:i,radius:n}}}}}),C=y.extend({_class:"Raster",_transformContent:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{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 c)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e={insert:!1},i=this._image;if(i)e.image=i;else if(this._canvas){var n=e.canvas=K.getCanvas(this._size);n.getContext("2d").drawImage(this._canvas,0,0)}return this._clone(new C(e),t)},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(!this._size.equals(t)){var e=this.getElement();this.setCanvas(K.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}},getWidth:function(){return this._size.width},getHeight:function(){return this._size.height},isEmpty:function(){return 0==this._size.width&&0==this._size.height},getPpi: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 c(72/i.getLength(),72/n.getLength())},getImage:function(){return this._image},setImage:function(t){this._canvas&&K.release(this._canvas),t.getContext?(this._image=null,this._canvas=t):(this._image=t,this._canvas=null),this._size=new c(t.naturalWidth||t.width,t.naturalHeight||t.height),this._context=null,this._changed(133) -},getCanvas:function(){if(!this._canvas){var t=K.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){K.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(){return this._context||(this._context=this.getCanvas().getContext("2d")),arguments[0]&&(this._image=null,this._changed(129)),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._project.view;t&&(paper=t._scope),i.fire("load"),t&&t.draw(!0)}var i=this,n=document.getElementById(t)||new Image;n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(V.add(n,{load:function(){i.setImage(n),e()}}),n.src||(n.src=t)),this.setImage(n)},getElement:function(){return this._canvas||this._image},getSubCanvas:function(t){t=_.read(arguments);var e=K.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(t){t=_.read(arguments);var e=new C({canvas:this.getSubCanvas(t),insert:!1});return 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,e){e=h.read(arguments,1),this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var i,n;t?t instanceof A?(n=t,i=t.getBounds()):t.width?i=new _(t):t.x&&(i=new _(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=C._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=C._sampleContext=K.getContext(new c(r)),o.save();var h=(new g).scale(s/i.width,a/i.height).translate(-i.x,-i.y);h.applyToContext(o),n&&n.draw(o,new e({clip:!0,transforms:[h]})),this._matrix.applyToContext(o),o.drawImage(this.getElement(),-this._size.width/2,-this._size.height/2),o.restore();for(var u=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,l=[0,0,0],d=0,f=0,v=u.length;v>f;f+=4){var m=u[f+3];d+=m,m/=255,l[0]+=u[f]*m,l[1]+=u[f+1]*m,l[2]+=u[f+2]*m}for(var f=0;3>f;f++)l[f]/=d;return d?j.read(l):null},getPixel:function(t){t=h.read(arguments);var 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),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(t){return t=c.read(arguments),this.getContext().createImageData(t.width,t.height)},getImageData:function(t){return t=_.read(arguments),t.isEmpty()&&(t=new _(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t,e){e=h.read(arguments,1),this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var i=new _(this._size).setCenter(0,0);return e?e._transformBounds(i):i},_hitTest:function(t){if(this._contains(t)){var e=this;return new P("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}}),S=y.extend({_class:"PlacedSymbol",_transformContent:!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&&delete this._symbol._instances[this._id],this._symbol=t,t._instances[this._id]=this},clone:function(t){return this._clone(new S({symbol:this.symbol,insert:!1}),t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e){return this.symbol._definition._getCachedBounds(t,e)},_hitTest:function(t,e,i){var n=this._symbol._definition._hitTest(t,e,i);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),P=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 t&&t._merged?t:new e({type:null,tolerance:paper.project.options.hitTolerance||2,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1,_merged:!0},t)}}}),z=e.extend({_class:"Segment",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.isLinear()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){if(this._path){var e,i=this._path._curves&&this.getCurve();i&&(i._changed(),(e=i[t==this._point||t==this._handleIn&&i._segment1==this?"getPrevious":"getNext"]())&&e._changed()),this._path._changed(5)}},getPoint:function(){return this._point},setPoint:function(t){t=h.read(arguments),this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(t){t=h.read(arguments),this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(t){t=h.read(arguments),this._handleOut.set(t.x,t.y)},isLinear:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},setLinear:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},isColinear:function(t){var e=this.getNext(),i=t.getNext();return this._handleOut.isZero()&&e._handleIn.isZero()&&t._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(this._point).isColinear(i._point.subtract(t._point))},isOrthogonal:function(){var t=this.getPrevious(),e=this.getNext();return t._handleOut.isZero()&&this._handleIn.isZero()&&this._handleOut.isZero()&&e._handleIn.isZero()&&this._point.subtract(t._point).isOrthogonal(e._point.subtract(this._point))},isArc:function(){var t=this.getNext(),e=this._handleOut,i=t._handleIn,n=o.KAPPA;if(e.isOrthogonal(i)){var r=this._point,s=t._point,a=new v(r,e,!0).intersect(new v(s,i,!0),!0);return a&&o.isZero(e.getLength()/a.subtract(r).getLength()-n)&&o.isZero(i.getLength()/a.subtract(s).getLength()-n)}return!1},_isSelected:function(t){var e=this._selectionState;return t==this._point?!!(4&e):t==this._handleIn?!!(1&e):t==this._handleOut?!!(2&e):!1},_setSelected:function(t,e){var i=this._path,e=!!e,n=this._selectionState||0,r=[!!(4&n),!!(1&n),!!(2&n)];if(t===this._point){if(e)r[1]=r[2]=!1;else{var s=this.getPrevious(),a=this.getNext();r[1]=s&&(s._point.isSelected()||s._handleOut.isSelected()),r[2]=a&&(a._point.isSelected()||a._handleIn.isSelected())}r[0]=e}else{var o=t===this._handleIn?1:2;r[o]!=e&&(e&&(r[0]=!1),r[o]=e)}this._selectionState=(r[0]?4:0)|(r[1]?1:0)|(r[2]?2:0),i&&n!=this._selectionState&&(i._updateSelection(this,n,this._selectionState),i._changed(33))},isSelected:function(){return this._isSelected(this._point)},setSelected:function(t){this._setSelected(this._point,t)},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?(t._closed||e!=t._segments.length-1||e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new k(t,t.getNext()?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 z(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new z(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(", ")+" }"},_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,0,e,0,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}}),M=h.extend({initialize:function(e,i,n){var r,s,a;e?(r=e[0])!==t?s=e[1]:((r=e.x)===t&&(e=h.read(arguments),r=e.x),s=e.y,a=e.selected):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 o.isZero(this._x)&&o.isZero(this._y)},setSelected:function(t){this._owner._setSelected(this,t)},isSelected:function(){return this._owner._isSelected(this)}}),I=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 z,this._segment2=new z;else if(1===h)this._segment1=new z(t.segment1),this._segment2=new z(t.segment2);else if(2===h)this._segment1=new z(t),this._segment2=new z(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 z(u,null,l),this._segment2=new z(d,c,null)}},_changed:function(){delete this._length,delete this._bounds},getPoint1:function(){return this._segment1._point},setPoint1:function(t){t=h.read(arguments),this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(t){t=h.read(arguments),this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(t){t=h.read(arguments),this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(t){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.getHandle1().isSelected()&&this.getHandle2().isSelected()},setSelected:function(t){this.getHandle1().setSelected(t),this.getHandle2().setSelected(t)},getValues:function(){return I.getValues(this._segment1,this._segment2)},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(){var t=arguments[0],e=arguments[1],i=0===arguments.length||0===t&&1===e;if(i&&null!=this._length)return this._length;var n=I.getLength(this.getValues(),t,e);return i&&(this._length=n),n},getArea:function(){return I.getArea(this.getValues())},getPart:function(t,e){return new I(I.getPart(this.getValues(),t,e))},isLinear:function(){return this._segment1._handleOut.isZero()&&this._segment2._handleIn.isZero()},getIntersections:function(t){return I.getIntersections(this.getValues(),t.getValues(),this,t,[])},reverse:function(){return new I(this._segment2.reverse(),this._segment1.reverse())},_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){var i=this._getParameter(t,e),n=1e-5,r=null;if(i>n&&1-n>i){var s=I.subdivide(this.getValues(),i),a=this.isLinear(),o=s[0],u=s[1];a||(this._segment1._handleOut.set(o[2]-o[0],o[3]-o[1]),this._segment2._handleIn.set(u[4]-u[6],u[5]-u[7]));var l=o[6],c=o[7],d=new z(new h(l,c),!a&&new h(o[4]-l,o[5]-c),!a&&new h(u[2]-l,u[3]-c));if(this._path)this._segment1._index>0&&0===this._segment2._index?this._path.add(d):this._path.insert(this._segment2._index,d),r=this;else{var _=this._segment2;this._segment2=d,r=new I(d,_)}}return r},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},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(", ")+" }"},statics:{getValues:function(t,e){var i=t._point,n=t._handleOut,r=e._handleIn,s=e._point;return[i._x,i._y,i._x+n._x,i._y+n._y,s._x+r._x,s._y+r._y,s._x,s._y]},evaluate:function(t,e,i){var n,r,s=t[0],a=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],_=t[7];if(0!==i||0!==e&&1!==e){var f=3*(o-s),g=3*(l-o)-f,v=d-s-f-g,m=3*(u-a),p=3*(c-u)-m,y=_-a-m-p;if(0===i)n=((v*e+g)*e+f)*e+s,r=((y*e+p)*e+m)*e+a;else{var w=1e-5;if(w>e&&o===s&&u===a||e>1-w&&l===d&&c===_?(n=d-s,r=_-a):(n=(3*v*e+2*g)*e+f,r=(3*y*e+2*p)*e+m),3===i){var x=6*v*e+2*g,b=6*y*e+2*p;return(n*b-r*x)/Math.pow(n*n+r*r,1.5)}}}else n=0===e?s:d,r=0===e?a:_;return 2==i?new h(r,-n):new h(n,r)},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,m=c*h+i*l,p=c*d+i*f,y=c*_+i*g,w=c*f+i*v,x=c*g+i*m,b=c*p+i*w,C=c*y+i*x;return[[n,r,d,_,p,y,b,C],[b,C,w,x,v,m,u,l]]},solveCubic:function(t,e,i,n,r,s){var a=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-a),d=3*(u-h)-c,_=l-a-c-d;return o.solveCubic(_,d,c,a-i,n,r,s)},getParameterOf:function(t,e,i){if(Math.abs(t[0]-e)<1e-5&&Math.abs(t[1]-i)<1e-5)return 0;if(Math.abs(t[6]-e)<1e-5&&Math.abs(t[7]-i)<1e-5)return 1;for(var n,r,s=[],a=[],o=I.solveCubic(t,0,e,s),h=I.solveCubic(t,1,i,a),u=0;-1==o||o>u;)if(-1==o||(n=s[u++])>=0&&1>=n){for(var l=0;-1==h||h>l;)if((-1==h||(r=a[l++])>=0&&1>=r)&&(-1==o?n=r:-1==h&&(r=n),Math.abs(n-r)<1e-5))return.5*(n+r);if(-1==o)break}return null},getPart:function(t,e,i){return e>0&&(t=I.subdivide(t,e)[1]),1>i&&(t=I.subdivide(t,(i-e)/(1-e))[0]),t},isLinear:function(t){var e=o.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 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},getBounds:function(t){for(var e=t.slice(0,2),i=e.slice(),n=[0,0],r=0;2>r;r++)I._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,i,n);return new _(e[0],e[1],i[0]-e[0],i[1]-e[1])},_addBounds:function(t,e,i,n,r,s,a,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=o.solveQuadratic(c,d,_,u),g=1e-5,v=1-g;l(n,0);for(var m=0;f>m;m++){var p=u[m],y=1-p;p>g&&v>p&&l(y*y*y*t+3*y*y*p*e+3*y*p*p*i+p*p*p*n,s)}},_getWinding:function(t,e,i,n,r,s){function a(t){var e=t[1],i=t[7],r=e>i?-1:1;return 1===r&&(e>n||n>i)||-1===r&&(i>n||n>e)?0:r}var h=1e-5,u=Math.abs;if(I.isLinear(t)){var l=a(t);if(!l)return 0;var c=(t[6]-t[0])*(n-t[1])-(t[7]-t[1])*(i-t[0]);return(-h>c?-1:1)==l?0:l}for(var d,_=t[1],f=t[3],g=t[5],v=t[7],m=3*(f-g)-_+v,p=2*(_+g)-4*f,y=f-_,w=o.solveQuadratic(m,p,y,r,h,1-h),x=t,b=r[0],C=0,S=0;w>=S;S++){if(S===w)d=x;else{var P=I.subdivide(x,b);d=P[0],x=P[1],b=r[S],b=(r[S+1]-b)/(1-b)}S>0&&(d[3]=d[1]),w>S&&(d[5]=x[1]);var l=a(d);if(l){var z,M;if(1===I.solveCubic(d,1,n,s,-h,1+-h))z=s[0],M=I.evaluate(d,z,0).x;else{var k=(d[1]+d[7])/2;if(z=k>n&&l>0?0:1,1===z&&n==d[7])continue;M=0===z?d[0]:d[6]}var A=I.evaluate(d,z,1).y,O=u(A)z&&I.evaluate(e,1,1).y*A<0;!(i>=M+(O?-h:h*l))||O&&(u(z)h||u(z-1)h)||(C+=O&&u(z-(l>0?1:0))e?1:0)},getParameterOf:function(t){return t=h.read(arguments),I.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){return e||(t=this.getParameterAt(t)),new k(this,t)},getLocationOf:function(t){t=h.read(arguments);var e=this.getParameterOf(t);return null!=e?new k(this,e):null},getNearestLocation:function(t){function e(e){if(e>=0&&1>=e){var n=t.getDistance(I.evaluate(i,e,0),!0);if(s>n)return s=n,a=e,!0}}t=h.read(arguments);for(var i=this.getValues(),n=100,r=o.TOLERANCE,s=1/0,a=0,u=0;n>=u;u++)e(u/n);for(var l=1/(2*n);l>r;)e(a-l)||e(a+l)||(l/=2);var c=I.evaluate(i,a,0);return new k(this,a,c,null,null,null,t.getDistance(c))},getNearestPoint:function(t){return t=h.read(arguments),this.getNearestLocation(t).getPoint()}}),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))))}return{statics:!0,getLength:function(n,r,s){r===t&&(r=0),s===t&&(s=1);var a=o.isZero;if(a(n[0]-n[2])&&a(n[1]-n[3])&&a(n[6]-n[4])&&a(n[7]-n[5])){var h=n[6]-n[0],u=n[7]-n[1];return(s-r)*Math.sqrt(h*h+u*u)}var l=e(n);return o.integrate(l,r,s,i(r,s))},getParameterAt:function(t,n,r){function s(t){var e=i(r,t);return _+=t>r?o.integrate(l,r,t,e):-o.integrate(l,t,r,e),r=t,_-n}if(0===n)return r;var a=n>0,h=a?r:0,u=a?1:r,n=Math.abs(n),l=e(t),c=o.integrate(l,h,u,i(h,u));if(n>=c)return a?u:h;var d=n/c,_=0;return o.findRoot(s,l,a?h+d:u-d,h,u,16,1e-5)}}},new function(){function t(t,e,i,n,r,s,a){var h=t[0],u=t[t.length-1];h&&n.isClose(h._point,o.EPSILON)||u&&n.isClose(u._point,o.EPSILON)||t.push(new k(e,i,n,r,s,a))}function e(n,r,s,a,o,h,u,l){if(l=(l||0)+1,!(l>20)){h=h||[0,1],u=u||[0,1];for(var c=I.getPart(n,h[0],h[1]),d=I.getPart(r,u[0],u[1]),_=0;_++<20;){var f,g=i(c,d,f=u.slice()),v=0;if(0===g)break;if(g>0){if(u=f,d=I.getPart(r,u[0],u[1]),v=i(d,c,f=h.slice()),0===v)break;g>0&&(h=f,c=I.getPart(n,h[0],h[1]))}if(0>g||0>v){if(h[1]-h[0]>u[1]-u[0]){var m=(h[0]+h[1])/2;e(n,r,s,a,o,[h[0],m],u,l),e(n,r,s,a,o,[m,h[1]],u,l);break}var m=(u[0]+u[1])/2;e(n,r,s,a,o,h,[u[0],m],l),e(n,r,s,a,o,h,[m,u[1]],l);break}if(Math.abs(h[1]-h[0])<=1e-5&&Math.abs(u[1]-u[0])<=1e-5){var p=(h[0]+h[1])/2,y=(u[0]+u[1])/2;t(o,s,p,I.evaluate(n,p,0),a,y,I.evaluate(r,y,0));break}}}}function i(t,e,i){var r=t[0],s=t[1],a=t[2],o=t[3],h=t[4],u=t[5],l=t[6],c=t[7],d=e[0],_=e[1],f=e[2],g=e[3],m=e[4],p=e[5],y=e[6],w=e[7],x=v.getSignedDistance,b=x(r,s,l,c,a,o)||0,C=x(r,s,l,c,h,u)||0,S=b*C>0?.75:4/9,P=S*Math.min(0,b,C),z=S*Math.max(0,b,C),M=x(r,s,l,c,d,_),k=x(r,s,l,c,f,g),A=x(r,s,l,c,m,p),O=x(r,s,l,c,y,w);if(P>Math.max(M,k,A,O)||zO&&(L=P,P=z,z=L);for(var B=-1/0,D=1/0,N=-1/0,j=0,E=T.length;E>j;j++){var F=T[j],q=T[(j+1)%E];q[1]=V&&H>=P){var G=R+(P-V)/W;D>G&&(D=G),G>B&&(B=G)}if(z>=V&&H>=z){var G=R+(z-V)/W;G>N&&(N=G),D>G&&(D=0)}}if(1/0!==D&&N!==-1/0){var X=Math.min(P,z),J=Math.max(P,z);O>X&&J>O&&(N=1),M>X&&J>M&&(D=0),B>N&&(N=1);var Y=i[0],U=i[1]-Y;if(i[0]=Y+D*U,i[1]=Y+N*U,(U-(i[1]-i[0]))/U>=.2)return 1}return I.getBounds(t).touches(I.getBounds(e))?-1:0}function n(t,e,i,n){var r=[0,t],s=[1/3,e],a=[2/3,i],o=[1,n],h=v.getSignedDistance,u=h(0,t,1,n,1/3,e),l=h(0,t,1,n,2/3,i);if(0>u*l)return[r,s,o,a];var c,d;return Math.abs(u)>Math.abs(l)?(c=s,d=(n-i-(n-t)/3)*(2*(n-i)-n+e)/3):(c=a,d=(e-t+(t-n)/3)*(-2*(t-e)+t-i)/3),0>d?[r,c,o]:[r,s,a,o]}function r(e,i,n,r,s){for(var a=I.isLinear(e),o=a?i:e,h=a?e:i,u=h[0],l=h[1],c=h[6],d=h[7],_=c-u,f=d-l,g=Math.atan2(-f,_),v=Math.sin(g),m=Math.cos(g),p=_*m-f*v,y=[0,0,0,0,p,0,p,0],w=[],x=0;8>x;x+=2){var b=o[x]-u,C=o[x+1]-l;w.push(b*m-C*v,C*m+b*v)}for(var S=[],P=I.solveCubic(w,1,0,S,0,1),x=0;P>x;x++){var z=S[x],b=I.evaluate(w,z,0).x;if(b>=0&&p>=b){var M=I.getParameterOf(y,b,0),k=a?M:z,A=a?z:M;t(s,n,k,I.evaluate(e,k,0),r,A,I.evaluate(i,A,0))}}}function s(e,i,n,r,s){var a=v.intersect(e[0],e[1],e[6],e[7],i[0],i[1],i[6],i[7]);a&&t(s,n,null,a,r)}return{statics:{getIntersections:function(i,n,a,o,h){var u=I.isLinear(i),l=I.isLinear(n),c=a.getPoint1(),d=a.getPoint2(),_=o.getPoint1(),f=o.getPoint2(),g=1e-5;return c.isClose(_,g)&&t(h,a,0,c,o,0,c),c.isClose(f,g)&&t(h,a,0,c,o,1,c),(u&&l?s:u||l?r:e)(i,n,a,o,h),d.isClose(_,g)&&t(h,a,1,d,o,0,d),d.isClose(f,g)&&t(h,a,1,d,o,1,d),h}}}}),k=e.extend({_class:"CurveLocation",initialize:function ge(t,e,i,n,r,s,a){this._id=ge._id=(ge._id||0)+1,this._curve=t,this._segment1=t._segment1,this._segment2=t._segment2,this._parameter=e,this._point=i,this._curve2=n,this._parameter2=r,this._point2=s,this._distance=a},getSegment:function(){if(!this._segment){var t=this.getCurve(),e=this.getParameter();if(1===e)this._segment=t._segment2;else if(0===e||arguments[0])this._segment=t._segment1;else{if(null==e)return null;this._segment=t.getLength(0,e)a;a++)s[a]=n[a].getValues();for(var a=0,o=i.length;o>a;a++)for(var h=i[a],u=h.getValues(),l=0;r>l;l++)I.getIntersections(u,s[l],h,n[l],e);return e},setPathData:function(t){function e(t,e,i){var r=parseFloat(n[t]);return a&&(r+=o[e]),i&&(o[e]=r),r}function i(t,i){return new h(e(t,"x",i),e(t+1,"y",i))}var n,r,s=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),a=!1,o=new h;this.clear();for(var u=0,l=s.length;l>u;u++){var c=s[u],d=c[0],_=d.toLowerCase();n=c.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var f=n&&n.length;switch(a=d===_,_){case"m":case"l":for(var g=0;f>g;g+=2)this[0===g&&"m"===_?"moveTo":"lineTo"](i(g,!0));r=o;break;case"h":case"v":for(var v="h"==_?"x":"y",g=0;f>g;g++)e(g,v,!0),this.lineTo(o);r=o;break;case"c":for(var g=0;f>g;g+=6)this.cubicCurveTo(i(g),r=i(g+2),i(g+4,!0));break;case"s":for(var g=0;f>g;g+=4)this.cubicCurveTo(o.multiply(2).subtract(r),r=i(g),i(g+2,!0));break;case"q":for(var g=0;f>g;g+=4)this.quadraticCurveTo(r=i(g),i(g+2,!0));break;case"t":for(var g=0;f>g;g+=2)this.quadraticCurveTo(r=o.multiply(2).subtract(r),i(g,!0));break;case"a":break;case"z":this.closePath()}}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),O=A.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[];var i=Array.isArray(e)?"object"==typeof e[0]?e:arguments:e&&(e.point!==t&&e.size===t||e.x!==t)?arguments:null;this.setSegments(i||[]),this._initialize(!i&&e)},_equals:function(t){return e.equals(this._segments,t._segments)},clone:function(e){var i=this._clone(new O({segments:this._segments,insert:!1}),e);return i._closed=this._closed,this._clockwise!==t&&(i._clockwise=this._clockwise),i},_changed:function ve(t){if(ve.base.call(this,t),4&t){if(delete this._length,delete this._clockwise,this._curves)for(var e=0,i=this._curves.length;i>e;e++)this._curves[e]._changed(5)}else 8&t&&delete this._bounds},getSegments:function(){return this._segments},setSegments:function(t){var e=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,delete this._curves,this._add(z.readAll(t)),e&&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 I(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]},getPathData:function(){function t(t,e,s){var a=t._point,o=e._point,h=t._handleOut,u=e._handleIn;if(h.isZero()&&u.isZero())s||r.push("L"+n.point(o,i));else{var l=o.subtract(a);r.push("c"+n.point(h,i)+" "+n.point(l.add(u),i)+" "+n.point(l,i))}}var e=this._segments,i=arguments[0],n=a.instance,r=[];if(0===e.length)return"";r.push("M"+n.point(e[0]._point));for(var s=0,o=e.length-1;o>s;s++)t(e[s],e[s+1],!1);return this._closed&&(t(e[e.length-1],e[0],!0),r.push("z")),r.join("")},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(5)}},isEmpty:function(){return 0===this._segments.length},isPolygon:function(){for(var t=0,e=this._segments.length;e>t;t++)if(!this._segments[t].isLinear())return!1;return!0},_applyMatrix: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 I(this,null,null));this._adjustCurves(u,c)}return this._changed(5),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=r[this._closed&&0===t?n.length-1:t-1])&&(i._segment2=n[t]||n[0]),(i=r[e])&&(i._segment1=n[e])},_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(z.readAll(arguments)):this._add([z.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(z.readAll(arguments,1),t):this._add([z.read(arguments,1)],t)[0]},addSegment:function(){return this._add([z.read(arguments)])[0]},insertSegment:function(t){return this._add([z.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(z.readAll(t))},insertSegments:function(t,e){return this._add(z.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,i){t=t||0,i=e.pick(i,this._segments.length);var n=this._segments,r=this._curves,s=n.length,a=n.splice(t,i-t),o=a.length;if(!o)return a;for(var h=0;o>h;h++){var u=a[h];u._selectionState&&this._updateSelection(u,u._selectionState,0),delete u._index,delete u._path}for(var h=t,l=n.length;l>h;h++)n[h]._index=h;if(r){var c=t>0&&i===s+(this._closed?1:0)?t-1:t,r=r.splice(c,o);arguments[2]&&(a._curves=r.slice(1)),this._adjustCurves(c,c)}return this._changed(5),a},clear:"#removeSegments",isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===4*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function me(t){t||this._selectSegments(!1),me.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?4*e:0;for(var i=0;e>i;i++)this._segments[i]._selectionState=t?4: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),i=0,n=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-n:n)/2,s=[];r>=i;)s.push(new z(e.evaluate(i,0))),i+=n;this.setSegments(s)},simplify:function(t){if(this._segments.length>2){var e=new B(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null!==e){if(1===arguments.length){var i=t;"number"==typeof i&&(i=this.getLocationAt(i)),t=i.index,e=i.parameter}e>=1&&(t++,e--);var n=this.getCurves();if(t>=0&&t0&&n[t++].divide(e,!0);var r,s=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),r=this):t>0&&(r=this._clone((new O).insertAbove(this,!0))),r._add(s,0),this.addSegment(s[0]),r}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}delete this._curves,this._clockwise!==t&&(this._clockwise=!this._clockwise)},join:function(t){if(t){var e=t._segments,i=this.getLastSegment(),n=t.getLastSegment();i._point.equals(n._point)&&t.reverse();var r,s=t.getFirstSegment(); -return i._point.equals(s._point)?(i.setHandleOut(s._handleOut),this._add(e.slice(1))):(r=this.getFirstSegment(),r._point.equals(s._point)&&t.reverse(),n=t.getLastSegment(),r._point.equals(n._point)?(r.setHandleIn(n._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())),t.closed&&this._add([e[0]]),t.remove(),r=this.getFirstSegment(),i=this.getLastSegment(),i._point.equals(r._point)&&(r.setHandleIn(i._handleIn),i.remove(),this.setClosed(!0)),this._changed(5),!0}return!1},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},_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];return n+s.getLength(0,t.getParameter())}return null},getLocationOf:function(t){t=h.read(arguments);for(var e=this.getCurves(),i=0,n=e.length;n>i;i++){var r=e[i].getLocationOf(t);if(r)return r}return null},getLocationAt:function(t,e){var i=this.getCurves(),n=0;if(e){var r=~~t;return i[r].getLocationAt(t-r,!0)}for(var s=0,a=i.length;a>s;s++){var o=n,h=i[s];if(n+=h.getLength(),n>=t)return h.getLocationAt(t-o)}return t<=this.getLength()?new k(i[i.length-1],1):null},getPointAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getPoint()},getTangentAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getTangent()},getNormalAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getNormal()},getNearestLocation:function(t){t=h.read(arguments);for(var e=this.getCurves(),i=1/0,n=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceu;u++){var c=i[u].getValues(),d=c[0],_=c[1];(d!==c[2]||_!==c[3]||d!==c[4]||_!==c[5]||d!==c[6]||_!==c[7])&&(r+=I._getWinding(c,h,t.x,t.y,s,a),h=c)}return e||(r+=I._getWinding(o,h,t.x,t.y,s,a)),r},_hitTest:function(e,i){function n(i,n,r){return e.getDistance(n)o;o++){var h=a(o);s+=I._getWinding(h,i,t.x,t.y,n,r),i=h}return!!s}function h(t){return("round"!==u||"round"!==l)&&(d=[],m||t._index>0&&t._index0)?o(e):e.getDistance(t._point)<=y}var u,l,c,d,_,f,g=this.getStyle(),v=this._segments,m=this._closed,p=i.tolerance,y=0,w=this;if(i.stroke&&(y=g.getStrokeWidth()/2,y>0?(u=g.getStrokeJoin(),l=g.getStrokeCap(),c=y*g.getMiterLimit()):u=l="round",y+=p),!i.ends||i.segments||m){if(i.segments||i.handles)for(var x=0,b=v.length;b>x;x++)if(f=r(v[x]))return f}else if(f=r(v[0],!0)||r(v[v.length-1],!0))return f;if(y>0){if(_=this.getNearestLocation(e)){var C=_.getParameter();0===C||1===C?h(_.getSegment())||(_=null):_._distance>y&&(_=null)}if(!_&&"miter"===u)for(var x=0,b=v.length;b>x;x++){var S=v[x];if(e.getDistance(S._point)<=c&&h(S)){_=S.getLocation();break}}}return!_&&i.fill&&this.hasFill()&&this._contains(e)?new P("fill",this):_?new P("stroke",this,{location:_}):null}},new function(){function t(t,e,i,n){function r(e){var i=a[e],n=a[e+1];(d!=i||_!=n)&&(t.beginPath(),t.moveTo(d,_),t.lineTo(i,n),t.stroke(),t.beginPath(),t.arc(i,n,s,0,2*Math.PI,!0),t.fill())}for(var s=n/2,a=Array(6),o=0,h=e.length;h>o;o++){var u=e[o];u._transformCoordinates(i,a,!1);var l=u._selectionState,c=4&l,d=a[0],_=a[1];(c||1&l)&&r(2),(c||2&l)&&r(4),t.save(),t.beginPath(),t.rect(d-s,_-s,n,n),t.fill(),c||(t.beginPath(),t.rect(d-s+1,_-s+1,n-2,n-2),t.fillStyle="#ffffff",t.fill()),t.restore()}}function e(t,e,i){function n(e){var n=d[e];if(i)n._transformCoordinates(i,f,!1),r=f[0],s=f[1];else{var _=n._point;r=_._x,s=_._y}if(g)t.moveTo(r,s),g=!1;else{if(i)h=f[2],u=f[3];else{var v=n._handleIn;h=r+v._x,u=s+v._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 v=n._handleOut;l=a+v._x,c=o+v._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(v);e._closed&&_>1&&n(0)}return{_draw:function(t,i){function n(t){return u[(t%l+l)%l]}var r=i.clip,s=i.compound;s||t.beginPath();var a=this.getStyle(),o=a.hasFill(),h=a.hasStroke(),u=a.getDashArray(),l=!paper.support.nativeDash&&h&&u&&u.length;if((o||h&&!l||s||r)&&e(t,this),this._closed&&t.closePath(),!r&&!s&&(o||h)&&(this._setStyles(t),o&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),h)){if(l){t.beginPath();var c,d=new T(this),_=d.length,f=-a.getDashOffset(),g=0;for(f%=_;f>0;)f-=n(g--)+n(g--);for(;_>f;)c=f+n(g++),(f>0||c>0)&&d.drawPart(t,Math.max(f,0),Math.max(c,0)),f=c+n(g++)}t.stroke()}},_drawSelected:function(i,n){i.beginPath(),e(i,this,n),i.stroke(),t(i,this._segments,n,this._project.options.handleSize||4)}}},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 m=null,o=s;r-s>=o;o++){var p=e[o-s];m&&p.setHandleIn(m.subtract(p._point)),r>o&&(p.setHandleOut(new h(l[o],c[o]).subtract(p._point)),m=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&&m){var p=this._segments[0];p.setHandleIn(m.subtract(p._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(){1===this._segments.length&&this.removeSegment(0),this._segments.length||this._add([new z(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new z(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 z(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=t(this),r=n._point,s=h.read(arguments),a=e.pick(e.peek(arguments),!0);if("boolean"==typeof a)var o=r.add(s).divide(2),i=o.add(o.subtract(r).rotate(a?-90:90));else i=s,s=h.read(arguments);var u=new v(r.add(i).divide(2),i.subtract(r).rotate(90),!0),l=new v(i.add(s).divide(2),s.subtract(i).rotate(90),!0),c=u.intersect(l,!0),d=new v(r,s),_=d.getSide(i);if(!c){if(!_)return this.lineTo(s);throw Error("Cannot put an arc through the given points: "+[r,i,s])}var f=r.subtract(c),g=f.getDirectedAngle(s.subtract(c)),m=d.getSide(c);0==m?g=_*Math.abs(g):_==m&&(g-=360*(0>g?-1:1));for(var p=Math.abs(g),y=p>=360?4:Math.ceil(p/90),w=g/y,x=w*Math.PI/360,b=4/3*Math.sin(x)/(1+Math.cos(x)),C=[],S=0;y>=S;S++){var P=y>S?c.add(f):s,M=y>S?f.rotate(90).multiply(b):null;0==S?n.setHandleOut(M):C.push(new z(P,f.rotate(-90).multiply(b),M)),f=f.rotate(w)}this._add(C)},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(){var t=this.getFirstSegment(),e=this.getLastSegment();t._point.equals(e._point)&&(t.setHandleIn(e._handleIn),e.remove()),this.setClosed(!0)}}},{_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++)for(var r=I.getValues(t[i],t[n>i+1?i+1:0]),s=2;8>s;s+=2)e+=(r[s-2]-r[s])*(r[s+1]+r[s-1]);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++)I._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 _;for(var o=Array(6),h=a._transformCoordinates(n,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),d=1,f=t.length;f>d;d++)s(t[d]);return e&&s(a),new _(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,i,n){function r(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)]}function s(t){f=f.include(n?n._transformPoint(t,t):t)}function a(t,e){"round"===e||!t._handleIn.isZero()&&!t._handleOut.isZero()?f=f.unite(p.setCenter(n?n._transformPoint(t._point):t._point)):O._addSquareJoin(t,e,l,m,s)}function o(t,e){switch(e){case"round":a(t,e);break;case"butt":case"square":O._addSquareCap(t,e,l,s)}}if(!i.hasStroke())return O.getBounds(t,e,i,n);for(var u=t.length-(e?0:1),l=i.getStrokeWidth()/2,d=r(l,n),f=O.getBounds(t,e,i,n,d),g=i.getStrokeJoin(),v=i.getStrokeCap(),m=l*i.getMiterLimit(),p=new _(new c(d).multiply(2)),y=1;u>y;y++)a(t[y],g);return e?a(t[0],g):(o(t[0],v),o(t[t.length-1],v)),f},_addSquareJoin: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 v(u.add(l),new h(-l.y,l.x),!0).intersect(new v(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().normalize(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){var a=Array(6),o=1/0,h=-o,u=o,l=h;r=r/2||0,s=s/2||0;for(var c=0,d=t.length;d>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,m=a[g],p=a[g+1],y=m-v,w=m+v,x=p-v,b=p+v;o>y&&(o=y),w>h&&(h=w),u>x&&(u=x),b>l&&(l=b)}}return new _(o,u,h-o,l-u)},getRoughBounds:function(t,e,i,n){var r=i.getStrokeColor()?i.getStrokeWidth():0,s=r;return 0===r?r=1e-5:("miter"===i.getStrokeJoin()&&(s=r*i.getMiterLimit()),"square"===i.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),O.getHandleBounds(t,e,i,n,r,s)}}});O.inject({statics:new function(){function t(t,i,r){for(var s=new O,a=Array(4),o=0;4>o;o++){var h=n[o];a[o]=new z(h._point.multiply(i).add(t),h._handleIn.multiply(i),h._handleOut.multiply(i))}return s._add(a),s._closed=!0,s.set(e.getNamed(r))}var i=o.KAPPA,n=[new z([-1,0],[0,i],[0,-i]),new z([0,-1],[-i,0],[i,0]),new z([1,0],[0,-i],[0,i]),new z([0,1],[i,0],[-i,0])];return{Line:function(){return new O(h.readNamed(arguments,"from"),h.readNamed(arguments,"to")).set(e.getNamed(arguments))},Circle:function(){var i=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"radius");return t(i,new c(n),arguments)},Rectangle:function(){var t=_.readNamed(arguments,"rectangle"),n=c.readNamed(arguments,"radius",0,0,{readNull:!0}),r=t.getBottomLeft(!0),s=t.getTopLeft(!0),a=t.getTopRight(!0),o=t.getBottomRight(!0);if(path=new O,!n||n.isZero())path._add([new z(r),new z(s),new z(a),new z(o)]);else{n=c.min(n,t.getSize(!0).divide(2));var h=n.width,u=n.height,l=h*i,d=u*i;path._add([new z(r.add(h,0),null,[-l,0]),new z(r.subtract(0,u),[0,d]),new z(s.add(0,u),null,[0,-d]),new z(s.add(h,0),[-l,0],null),new z(a.subtract(h,0),null,[l,0]),new z(a.add(0,u),[0,-d],null),new z(o.subtract(0,u),null,[0,d]),new z(o.subtract(h,0),[l,0])])}return path._closed=!0,path.set(e.getNamed(arguments))},RoundRectangle:"#Rectangle",Ellipse:function(){var e=b._readEllipse(arguments);return t(e.center,e.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),i=h.readNamed(arguments,"through"),n=h.readNamed(arguments,"to"),r=new O;return r.moveTo(t),r.arcTo(i,n),r.set(e.getNamed(arguments))},RegularPolygon:function(){for(var t=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"sides"),n=e.readNamed(arguments,"radius"),r=new O,s=360/i,a=!(i%3),o=new h(0,a?-n:n),u=a?-1:.5,l=Array(i),c=0;i>c;c++)l[c]=new z(t.add(o.rotate((c+u)*s)));return r._add(l),r._closed=!0,r.set(e.getNamed(arguments))},Star:function(){for(var t=h.readNamed(arguments,"center"),i=2*e.readNamed(arguments,"points"),n=e.readNamed(arguments,"radius1"),r=e.readNamed(arguments,"radius2"),s=new O,a=360/i,o=new h(0,-1),u=Array(i),l=0;i>l;l++)u[l]=new z(t.add(o.rotate(a*l).multiply(l%2?r:n)));return s._add(u),s._closed=!0,s.set(e.getNamed(arguments))}}}});var L=A.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},insertChildren:function pe(e,i,n){i=pe.base.call(this,e,i,n,"path");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()},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=e.concat(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},getPathData:function(){for(var t=this._children,e=[],i=0,n=t.length;n>i;i++)e.push(t[i].getPathData(arguments[0]));return e.join(" ")},_getWinding:function(t){for(var e=this._children,i=0,n=0,r=e.length;r>n;n++)i+=e[n]._getWinding(t);return i},_hitTest:function ye(t,i){var n=ye.base.call(this,t,new e(i,{fill:!1}));if(!n)if(i.compoundChildren)for(var r=this._children,s=r.length-1;s>=0&&!n;s--)n=r[s]._hitTest(t,i);else i.fill&&this.hasFill()&&this._contains(t)&&(n=new P("fill",this));return n},_draw:function(t,e){var i=this._children;if(0!==i.length){t.beginPath(),e=e.extend({compound:!0});for(var n=0,r=i.length;r>n;n++)i[n].draw(t,e);if(!e.clip){this._setStyles(t);var s=this._style;s.hasFill()&&(t.fill(s.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s.hasStroke()&&t.stroke()}}}},new function(){function t(t){if(!t._children.length)throw Error("Use a moveTo() command first");return t._children[t._children.length-1]}var i={moveTo:function(){var t=new O;this.addChild(t),t.moveTo.apply(t,arguments)},moveBy:function(){this.moveTo(t(this).getLastSegment()._point.add(h.read(arguments)))},closePath:function(){t(this).closePath()}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){i[e]=function(){var i=t(this);i[e].apply(i,arguments)}}),i}),T=e.extend({initialize:function(t){function e(t,e){var i=I.getValues(t,e);s.curves.push(i),s._computeParts(i,t._index,0,1)}this.curves=[],this.parts=[],this.length=0,this.index=0;for(var i,n=t._segments,r=n[0],s=this,a=1,o=n.length;o>a;a++)i=n[a],e(r,i),r=i;t._closed&&e(i,n[0])},_computeParts:function(t,e,i,n){if(n-i>1/32&&!I.isFlatEnough(t,.25)){var r=I.subdivide(t),s=(i+n)/2;this._computeParts(r[0],e,i,s),this._computeParts(r[1],e,s,n)}else{var a=t[6]-t[0],o=t[7]-t[1],h=Math.sqrt(a*a+o*o);h>1e-5&&(this.length+=h,this.parts.push({offset:this.length,value:n,index:e}))}},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}},evaluate:function(t,e){var i=this.getParameterAt(t);return I.evaluate(this.curves[i.index],i.value,e)},drawPart:function(t,e,i){e=this.getParameterAt(e),i=this.getParameterAt(i);for(var n=e.index;n<=i.index;n++){var r=I.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))}}}),B=e.extend({initialize:function(t,e){this.points=[];for(var i,n=t._segments,r=0,s=n.length;s>r;r++){var a=n[r].point.clone();i&&i.equals(a)||(this.points.push(a),i=a)}this.error=e},fit:function(){var t=this.points,e=t.length;return this.segments=e>0?[new z(t[0])]:[],e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.segments},fitCubic:function(e,i,n,r){if(1==i-e){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;4>=c;c++){var d=this.generateBezier(e,i,u,n,r),_=this.findMaxError(e,i,d,u);if(_.error=l)break;this.reparameterize(e,i,u,d),l=_.error}var f=this.points[h-1].subtract(this.points[h]),g=this.points[h].subtract(this.points[h+1]),v=f.add(g).divide(2).normalize();this.fitCubic(e,h,n,v),this.fitCubic(h,i,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 z(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,i,n,r){for(var s=1e-11,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*_,m=f*d,p=d*d*d,y=n.normalize(v),w=r.normalize(m),x=this.points[t+l].subtract(a.multiply(g+v)).subtract(o.multiply(m+p));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],z=u[0]*h[1][1]-u[1]*h[0][1];b=z/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 k=o.getDistance(a);return s*=k,(s>b||s>C)&&(b=C=k/3),[a,a.add(n.normalize(b)),o.add(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])},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-5?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}}});A.inject(new function(){function t(t,e){t.sort(function(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 i=e&&[],n=t.length-1;n>=0;n--){var r=t[n],s=r.getIntersection(),a=r.divide(),o=a&&a.getSegment1()||r.getSegment();i&&i.push(s),o._intersection=s,r._segment=o}return i}function e(t){if(t instanceof L){var e,i=t.removeChildren(),n=i.length,r=Array(n),s=Array(n);i.sort(function(t,e){var i=t.getBounds(),n=e.getBounds();return i._width*i._heighta;a++)r[a]=i[a].getBounds(),s[a]=0;for(var a=0;n>a;a++){for(var o=1;n>o;o++)a!==o&&r[a].contains(r[o])&&s[o]++;a>0&&0===s[a]%2&&i[a].setClockwise(e)}}return t}function i(i,r,s,a){i=e(i.clone(!1)),r=e(r.clone(!1));var o=i.isClockwise(),u=r.isClockwise(),l=i.getIntersections(r);t(t(l,!0)),o||i.reverse(),a^u||r.reverse(),o=!0,u=!a;for(var c=[].concat(i._children||[i]).concat(r._children||[r]),d=[],_=new L,f=0,g=c.length;g>f;f++){var v=c[f],m=v._parent,p=v.isClockwise(),y=v._segments;v=m instanceof L?m:v;for(var w=y.length-1;w>=0;w--){var x=y[w],b=x.getCurve().getPoint(.5),C=v!==i&&i.contains(b)&&(p===o||a||!n(i,b)),S=v!==r&&r.contains(b)&&(p===u||!n(r,b));s(v===i,C,S)?x._invalid=!0:d.push(x)}}for(var f=0,g=d.length;g>f;f++){var x=d[f];if(!x._visited){var v=new O,P=x._intersection,M=P&&P.getSegment(!0);x.getPrevious()._invalid&&x.setHandleIn(M?M._handleIn:new h(0,0));do{if(x._visited=!0,x._invalid&&x._intersection){var I=x._intersection.getSegment(!0);v.add(new z(x._point,x._handleIn,I._handleOut)),I._visited=!0,x=I}else v.add(x.clone());x=x.getNext()}while(x&&!x._visited&&x!==M);var k=v._segments.length;k>1&&(k>2||!v.isPolygon())?(v.setClosed(!0),_.addChild(v,!0)):v.remove()}}return i.remove(),r.remove(),_.reduce()}function n(t,e){var i=t.getCurves(),n=t.getBounds();if(n.contains(e))for(var r=0,s=i.length;s>r;r++){var a=i[r];if(a.getBounds().contains(e)&&a.getParameterOf(e))return!0}return!1}return{unite:function(t){return i(this,t,function(t,e,i){return e||i})},intersect:function(t){return i(this,t,function(t,e,i){return!(e||i)})},subtract:function(t){return i(this,t,function(t,e,i){return t&&i||!t&&!e},!0)},exclude:function(t){return new w([this.subtract(t),t.subtract(this)])},divide:function(t){return new w([this.subtract(t),this.intersect(t)])}}});var D=y.extend({_class:"TextItem",_boundsSelected:!0,_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 we(t){return t.setContent(this._content),we.base.call(this,t)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(69)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),N=D.extend({_class:"PointText",initialize:function(){D.apply(this,arguments)},clone:function(t){return this._clone(new N({insert:!1}),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(t){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)}}}},new function(){var t=null;return{_getBounds:function(e,i){t||(t=K.getContext(1,1));var n=this._style,r=this._lines,s=r.length,a=n.getJustification(),o=n.getLeading(),h=0;t.font=n.getFontStyle();for(var u=0,l=0;s>l;l++)u=Math.max(u,t.measureText(r[l]).width);"left"!==a&&(h-=u/("center"===a?2:1));var c=new _(h,s?-.75*o:0,u,s*o);return i?i._transformBounds(c,c):c}}}),j=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 a=n[r+1];e[r]=parseInt(1==a.length?a+a:a,16)/255}}else if(n=t.match(/^rgba?\((.*)\)$/)){e=n[1].split(",");for(var r=0,o=e.length;o>r;r++){var a=parseFloat(e[r]);e[r]=3>r?a/255:a}}else{var h=s[t];if(!h){i||(i=K.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=s[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={},s={},o=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],u={"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){var t=t/60%6,n=Math.floor(t),r=t-n,n=o[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/=360,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+6*(r-s)*(2/3-h):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=E.read(Array.isArray(t)?t:arguments,0,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"hue"===t?function(t){return isNaN(t)?0:(t%360+360)%360}:"gradient"===i?function(){return h.read(arguments,0,0,{readNull:"highlight"===t,clone:!0})}:function(t){return isNaN(t)?0:Math.min(Math.max(t,0),1)};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),t=u.call(this,t),null!=t&&(this._components[s]=t,this._changed())}},this)},{_class:"Color",_readIndex:!0,initialize:function l(e){var i,s,a,o,h=Array.prototype.slice,u=arguments,c=0,d=!0;Array.isArray(e)&&(u=e,e=u[0]);var _=null!=e&&typeof e;if("string"===_&&e in n&&(i=e,e=u[1],Array.isArray(e)?(s=e,a=u[2]):(this.__read&&(c=1),u=h.call(u,1),_=typeof e)),!s){if(d=!(this.__options&&this.__options.dontParse),o="number"===_?u:"object"===_&&null!=e.length?e:null){i||(i=o.length>=3?"rgb":"gray");var f=n[i].length;a=o[f],this.__read&&(c+=o===arguments?f+(null!=a?1:0):1),o.length>f&&(o=h.call(o,0,f))}else if("string"===_)i="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===_)if(e.constructor===l){if(i=e._type,s=e._components.slice(),a=e._alpha,"gradient"===i)for(var g=1,v=s.length;v>g;g++){var m=s[g];m&&(s[g]=m.clone())}}else if(e.constructor===E)i="gradient",o=u;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 p=n[i];w=d&&r[i],this._components=s=[];for(var g=0,v=p.length;v>g;g++){var y=e[p[g]];null==y&&0===g&&"gradient"===i&&"stops"in e&&(y={stops:e.stops,radial:e.radial}),d&&(y=w[g].call(this,y)),null!=y&&(s[g]=y)}a=e.alpha}this.__read&&i&&(c=1)}if(this._type=i||"rgb","gradient"===i&&(this._id=l._id=(l._id||0)+1),!s){this._components=s=[];for(var w=r[this._type],g=0,v=w.length;v>g;g++){var y=o&&o[g];d&&(y=w[g].call(this,y)),null!=y&&(s[g]=y)}}this._components=s,this._properties=n[this._type],this._alpha=a,this.__read&&(this.__read=c)},_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(17)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=u[this._type+"-"+t])?e.apply(this,this._components):u["rgb-"+t].apply(this,u[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=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){return e.isPlainValue(t)&&(t=j.read(arguments)),t===this||t&&this._class===t._class&&this._type===t._type&&this._alpha===t._alpha&&e.equals(this._components,t._components)||!1},toString:function(){for(var t=this._properties,e=[],i="gradient"===this._type,n=a.instance,r=0,s=t.length;s>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){var e=this._convert("rgb"),i=t||null==this._alpha?1:this._alpha;return e=[Math.round(255*e[0]),Math.round(255*e[1]),Math.round(255*e[2])],1>i&&e.push(i),t?"#"+((1<<24)+(e[0]<<16)+(e[1]<<8)+e[2]).toString(16).slice(1):(4==e.length?"rgba(":"rgb(")+e.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 j(t(),t(),t())}}})},new function(){function t(t,e){return 0>t?0:e&&t>360?360:!e&&t>1?1:t}var i={add:function(e,i,n){return t(e+i,n)},subtract:function(e,i,n){return t(e-i,n)},multiply:function(e,i,n){return t(e*i,n)},divide:function(e,i,n){return t(e/i,n)}};return e.each(i,function(t,e){var i={dontParse:/^(multiply|divide)$/.test(e)};this[e]=function(e){e=j.read(arguments,0,0,i);for(var n=this._type,r=this._properties,s=this._components,a=e._convert(n),o=0,h=s.length;h>o;o++)a[o]=t(s[o],a[o],"hue"===r[o]);return new j(n,a,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(j._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 j(i,n):new j(t)};if(3==i.length){var r=i.toUpperCase();j[r]=this[r+"Color"]=n}},e.exports);var E=e.extend({_class:"Gradient",initialize:function xe(t,e){this._id=xe._id=(xe._id||0)+1,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(t){var e=this._owners?this._owners.indexOf(t):-1;-1!=e&&(this._owners.splice(e,1),0===this._owners.length&&delete this._owners)},clone:function(){for(var t=[],e=0,i=this._stops.length;i>e;e++)t[e]=this._stops[e].clone();return new E(t)},getStops:function(){return this._stops},setStops:function(t){if(this.stops)for(var e=0,i=this._stops.length;i>e;e++)delete this._stops[e]._owner;if(t.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=F.readAll(t,0,!1,!0);for(var e=0,i=this._stops.length;i>e;e++){var n=this._stops[e];n._owner=this,n._defaultRamp&&n.setRampPoint(e/(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,i=this._stops.length;i>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),F=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 F(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(17)},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}}),q=e.extend(new function(){var i={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,font:"sans-serif",fontSize:12,leading:null,justification:"left"},n={strokeWidth:25,strokeCap:25,strokeJoin:25,miterLimit:25,font:5,fontSize:5,leading:5,justification:5},r={},s={_defaults:i,_textDefaults:new e(i,{fillColor:new j})};return e.each(i,function(i,a){var o=/Color$/.test(a),h=e.capitalize(a),u=n[a],l="set"+h,c="get"+h;s[l]=function(t){var e=this._item&&this._item._children;if(e&&e.length>0&&"compound-path"!==this._item._type)for(var i=0,n=e.length;n>i;i++)e[i]._style[l](t);else{var r=this._values[a];r!=t&&(o&&(r&&delete r._owner,t&&t.constructor===j&&(t._owner&&(t=t.clone()),t._owner=this._item)),this._values[a]=t,this._item&&this._item._changed(u||17))}},s[c]=function(){var i,n=this._item&&this._item._children;if(!n||0===n.length||arguments[0]||"compound-path"===this._item._type){var i=this._values[a];return i===t?(i=this._defaults[a],i&&i.clone&&(i=i.clone()),this._values[a]=i):!o||i&&i.constructor===j||(this._values[a]=i=j.read([i],0,0,{readNull:!0,clone:!0}),i&&(i._owner=this._item)),i}for(var r=0,s=n.length;s>r;r++){var h=n[r]._style[c]();if(0===r)i=h;else if(!e.equals(i,h))return t}return i},r[c]=function(){return this._style[c]()},r[l]=function(t){this._style[l](t)}}),y.inject(r),s},{_class:"Style",initialize:function(t,e){this._values={},this._item=e,e instanceof D&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof q,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},getLeading:function be(){var t=be.base.call(this);return null!=t?t:1.2*this.getFontSize()},getFontStyle:function(){var t=this.getFontSize();return t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFont()}}),R=new function(){function i(t,n){for(var r=[],s=0,a=t&&t.length;a>s;){var o=t[s++];if("string"==typeof o)o=document.createElement(o);else if(!o||!o.nodeType)continue;e.isPlainObject(t[s])&&R.set(o,t[s++]),Array.isArray(t[s])&&i(t[s++],o),n&&n.appendChild(o),r.push(o)}return r}var n=/^(checked|value|selected|disabled)$/i,r={text:"textContent",html:"innerHTML"},s={lineHeight:1,zoom:1,zIndex:1,opacity:1};return{create:function(t,e){var n=Array.isArray(t),r=i(n?t:arguments,n?e:null);return 1==r.length?r[0]:r},find:function(t,e){return(e||document).querySelector(t)},findAll:function(t,e){return(e||document).querySelectorAll(t)},get:function(t,e){return t?n.test(e)?"value"===e||"string"!=typeof t[e]?t[e]:!0:e in r?t[r[e]]:t.getAttribute(e):null},set:function(e,i,s){if("string"!=typeof i)for(var a in i)i.hasOwnProperty(a)&&this.set(e,a,i[a]);else{if(!e||s===t)return e;n.test(i)?e[i]=s:i in r?e[r[i]]=s:"style"===i?this.setStyle(e,s):"events"===i?V.add(e,s):e.setAttribute(i,s)}return e},getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,i=e&&e.defaultView;return i&&i.getComputedStyle(t,"")},getStyle:function(t,e){return t&&t.style[e]||this.getStyles(t)[e]||null},setStyle:function(t,e,i){if("string"!=typeof e)for(var n in e)e.hasOwnProperty(n)&&this.setStyle(t,n,e[n]);else!/^-?[\d\.]+$/.test(i)||e in s||(i+="px"),t.style[e]=i;return t},hasClass:function(t,e){return RegExp("\\s*"+e+"\\s*").test(t.className)},addClass:function(t,e){t.className=(t.className+" "+e).trim()},removeClass:function(t,e){t.className=t.className.replace(RegExp("\\s*"+e+"\\s*")," ").trim()},remove:function(t){t.parentNode&&t.parentNode.removeChild(t)},removeChildren:function(t){for(;t.firstChild;)t.removeChild(t.firstChild)},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 _(o,h,i.width,i.height)},getViewportBounds:function(t){var e=t.ownerDocument,i=e.defaultView,n=e.documentElement;return new _(0,0,i.innerWidth||n.clientWidth,i.innerHeight||n.clientHeight)},getOffset:function(t,e){return this.getBounds(t,e).getPoint()},getSize:function(t){return this.getBounds(t,!0).getSize()},isInvisible:function(t){return this.getSize(t).equals(new c(0,0))},isInView:function(t){return!this.isInvisible(t)&&this.getViewportBounds(t).intersects(this.getBounds(t,!0))},getPrefixValue:function(t,e){for(var i=t[e],n=["webkit","moz","ms","o"],r=e[0].toUpperCase()+e.substring(1),s=0;4>s&&null==i;s++)i=t[n[s]+r];return i}}},V={add:function(t,e){for(var i in e){var n=e[i];t.addEventListener?t.addEventListener(i,n,!1):t.attachEvent&&t.attachEvent("on"+i,n.bound=function(){n.call(t,window.event)})}},remove:function(t,e){for(var i in e){var n=e[i];t.removeEventListener?t.removeEventListener(i,n,!1):t.detachEvent&&t.detachEvent("on"+i,n.bound)}},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},getOffset:function(t,e){return V.getPoint(t).subtract(R.getOffset(e||V.getTarget(t)))},preventDefault:function(t){t.preventDefault?t.preventDefault():t.returnValue=!1},stopPropagation:function(t){t.stopPropagation?t.stopPropagation():t.cancelBubble=!0},stop:function(t){V.stopPropagation(t),V.preventDefault(t)}};V.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"==r.getAttribute(u,"keepalive")||a)&&R.isInView(u))&&(s.splice(e,1),h())}i&&(s.length?i(t):n=!1)}var e,i=R.getPrefixValue(window,"requestAnimationFrame"),n=!1,s=[],a=!0;return V.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(r,a){s.push([r,a]),i?n||(i(t),n=!0):e||(e=setInterval(t,1e3/60))}};var Z=e.extend(n,{_class:"View",initialize:function Ce(t){this._scope=paper,this._project=paper.project,this._element=t;var e;if(this._id=t.getAttribute("id"),null==this._id&&t.setAttribute("id",this._id="view-"+Ce._id++),V.add(t,this._viewHandlers),r.hasAttribute(t,"resize")){var i=R.getOffset(t,!0),n=this;e=R.getViewportBounds(t).getSize().subtract(i),this._windowHandlers={resize:function(){R.isInvisible(t)||(i=R.getOffset(t,!0)),n.setViewSize(R.getViewportBounds(t).getSize().subtract(i))}},V.add(window,this._windowHandlers)}else e=R.getSize(t),(e.isNaN()||e.isZero())&&(e=new c(parseInt(t.getAttribute("width"),10),parseInt(t.getAttribute("height"),10)));if(this._setViewSize(e),r.hasAttribute(t,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var s=this._stats.domElement,a=s.style,i=R.getOffset(t);a.position="absolute",a.left=i.x+"px",a.top=i.y+"px",document.body.appendChild(s)}Ce._views.push(this),Ce._viewsById[this._id]=this,this._viewSize=e,(this._matrix=new g)._owner=this,this._zoom=1,Ce._focused||(Ce._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(Z._focused==this&&(Z._focused=null),Z._views.splice(Z._views.indexOf(this),1),delete Z._viewsById[this._id],this._project.view==this&&(this._project.view=null),V.remove(this._element,this._viewHandlers),V.remove(window,this._windowHandlers),this._element=this._project=null,this.detach("frame"),this._frameItems={},!0):!1},_events:{onFrame:{install:function(){this._animate=!0,this._requested||this._requestFrame()},uninstall:function(){this._animate=!1}},onResize:{}},_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;V.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.fire("frame",new e({delta:i,time:this._time+=i,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.draw(!0)},_animateItem:function(t,e){var i=this._frameItems;e?(i[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.attach("frame",this._handleFrameItems)):(delete i[t._id],0===--this._frameItemCount&&this.detach("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var i in this._frameItems){var n=this._frameItems[i];n.item.fire("frame",new e(t,{time:n.time+=t.delta,count:n.count++}))}},_redraw:function(){this._project._needsRedraw=!0,this._handlingFrame||(this._animate?this._handleFrame():this.draw())},_changed:function(t){1&t&&(this._project._needsRedraw=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._redraw()},getElement:function(){return this._element},getViewSize:function(){var t=this._viewSize;return new d(t.width,t.height,this,"setViewSize")},setViewSize:function(t){t=c.read(arguments);var e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.fire("resize",{size:t,delta:e}),this._redraw())},_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 _(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize(arguments[0])},getCenter:function(){return this.getBounds().getCenter(arguments[0])},setCenter:function(t){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 R.isInView(this._element)},scrollBy:function(){this._transform((new g).translate(h.read(arguments).negate()))},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){return"string"==typeof t&&(t=document.getElementById(t)),new H(t)}}},new function(){function t(t){var e=V.getTarget(t);return e.getAttribute&&Z._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(V.getOffset(e,t._element))}function i(){if(!Z._focused||!Z._focused.isVisible())for(var t=0,e=Z._views.length;e>t;t++){var i=Z._views[t];if(i&&i.isVisible()){Z._focused=u=i;break}}}function n(i){var n=Z._focused=t(i),r=e(n,i);l=!0,n._onMouseDown&&n._onMouseDown(i,r),(o=n._scope._tool)&&o._onHandleEvent("mousedown",r,i),n.draw(!0)}function r(n){var r;if(l||(r=t(n),r?(h=Z._focused,Z._focused=u=r):u&&u==Z._focused&&(Z._focused=h,i())),r=r||Z._focused){var s=n&&e(r,n);r._onMouseMove&&r._onMouseMove(n,s),(o=r._scope._tool)&&o._onHandleEvent(l&&o.responds("mousedrag")?"mousedrag":"mousemove",s,n)&&V.stop(n),r.draw(!0)}}function s(t){var i=Z._focused;if(i&&l){var n=e(i,t);curPoint=null,l=!1,i._onMouseUp&&i._onMouseUp(t,n),o&&o._onHandleEvent("mouseup",n,t)&&V.stop(t),i.draw(!0)}}function a(t){l&&V.stop(t)}var o,h,u,l=!1;return V.add(document,{mousemove:r,mouseup:s,touchmove:r,touchend:s,selectstart:a,scroll:i}),V.add(window,{load:i}),{_viewHandlers:{mousedown:n,touchstart:n,selectstart:a},statics:{updateFocus:i}}}),H=Z.extend({_class:"CanvasView",initialize:function(t){if(!(t instanceof HTMLCanvasElement)){var e=c.read(arguments);if(e.isZero())throw Error("Cannot create CanvasView with the provided argument: "+t);t=K.getCanvas(e)}if(this._context=t.getContext("2d"),this._eventCounters={},this._ratio=1,"off"!==r.getAttribute(t,"hidpi")){var i=window.devicePixelRatio||1,n=R.getPrefixValue(this._context,"backingStorePixelRatio")||1;this._ratio=i/n}Z.call(this,t)},_setViewSize:function(t){var e=t.width,i=t.height,n=this._ratio,r=this._element,s=r.style;r.width=e*n,r.height=i*n,1!==n&&(s.width=e+"px",s.height=i+"px",this._context.scale(n,n))},draw:function(t){if(t&&!this._project._needsRedraw)return!1;var e=this._context,i=this._viewSize;return e.clearRect(0,0,i.width+1,i.height+1),this._project.draw(e,this._matrix,this._ratio),this._project._needsRedraw=!1,!0}},new function(){function t(t,e,i,n,r,s){for(var a,o=n;o;){if(o.responds(t)&&(a||(a=new J(t,e,i,n,r?i.subtract(r):null)),o.fire(t,a)&&(!s||a._stopped)))return!1;o=o.getParent()}return!0}function e(e,i,n,r,s){if(e._eventCounters[i]){var a=e._project,u=a.hitTest(r,{tolerance:a.options.hitTolerance||0,fill:!0,stroke:!0}),l=u&&u.item;if(l)return"mousemove"===i&&l!=o&&(s=r),"mousemove"===i&&h||t(i,n,r,l,s),l}}var i,n,r,s,a,o,h,u,l;return{_onMouseDown:function(t,o){var c=e(this,"mousedown",t,o);u=a==c&&Date.now()-l<300,s=a=c,i=n=r=o,h=s&&s.responds("mousedrag")},_onMouseUp:function(a,o){var c=e(this,"mouseup",a,o);h&&(n&&!n.equals(o)&&t("mousedrag",a,o,s,n),c!=s&&(r=o,t("mousemove",a,o,c,r))),c===s&&(l=Date.now(),(!u||t("doubleclick",a,i,c))&&t("click",a,i,c),u=!1),s=null,h=!1},_onMouseMove:function(i,a){s&&t("mousedrag",i,a,s,n);var h=e(this,"mousemove",i,a,r);n=r=a,h!==o&&(t("mouseleave",i,a,o),o=h,t("mouseenter",i,a,h))}}}),W=e.extend({_class:"Event",initialize:function(t){this.event=t},preventDefault:function(){this._prevented=!0,V.preventDefault(this.event)},stopPropagation:function(){this._stopped=!0,V.stopPropagation(this.event)},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return X.modifiers}}),G=W.extend({_class:"KeyEvent",initialize:function(t,e,i,n){W.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()+" }"}}),X=new function(){function t(t,i,r,h){var u,l=r?String.fromCharCode(r):"",c=n[i],d=c||l.toLowerCase(),_=t?"keydown":"keyup",f=Z._focused,g=f&&f.isVisible()&&f._scope,v=g&&g._tool;o[d]=t,c&&(u=e.camelize(c))in s&&(s[u]=t),t?a[i]=r:delete a[i],v&&v.responds(_)&&(paper=g,v.fire(_,new G(t,d,l,h)),f&&f.draw(!0))}var i,n={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"},r={9:!0,13:!0,32:!0},s=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),a={},o={};return V.add(document,{keydown:function(e){var s=e.which||e.keyCode;s in n?t(!0,s,s in r?s:0,e):i=s},keypress:function(e){null!=i&&(t(!0,i,e.which||e.keyCode,e),i=null)},keyup:function(e){var i=e.which||e.keyCode;i in a&&t(!1,i,a[i],e)}}),V.add(window,{blur:function(e){for(var i in a)t(!1,i,a[i],e)}}),{modifiers:s,isDown:function(t){return!!o[t]}}},J=W.extend({_class:"MouseEvent",initialize:function(t,e,i,n,r){W.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()+" }"}}),Y=W.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|compound-path)$/.test(i._type);)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()+" }"}}),U=s.extend({_class:"Tool",_list:"tools",_reference:"_tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){s.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!=this._minDistance&&null!=this._maxDistance&&this._minDistance>this._maxDistance&&(this._maxDistance=this._minDistance)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=this._maxDistance&&this._maxDistanceu)return!1;var l=null!=n?n:0;if(0!=l)if(u>l)e=this._point.add(h.normalize(l));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.fire(t,new Y(this,t,e))},_onHandleEvent: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}}),$={request:function(t,e,i){var n=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return n.open(t.toUpperCase(),e,!0),"overrideMimeType"in n&&n.overrideMimeType("text/plain"),n.onreadystatechange=function(){if(4===n.readyState){var t=n.status;if(0!==t&&200!==t)throw Error("Could not load "+e+" (Error "+t+")");i.call(n,n.responseText)}},n.send(null)}},K={canvases:[],getCanvas:function(t,e,i){var n,r=!0;"object"==typeof t&&(i=e,e=t.height,t=t.width),i?1!==i&&(t*=i,e*=i):i=1,n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var s=n.getContext("2d");return n.width===t&&n.height===e?r&&s.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),s.save(),1!==i&&s.scale(i,i),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)}},Q=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=m(_,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 m(t,e,i)-v(t,e,i)}function r(t,e,i,n){var r,s=[t,e,i],a=m(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===m(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,m=Math.max,p=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:m(0,255-255*(255-u)/s),f=255===l?255:0===a?0:m(0,255-255*(255-l)/a),g=255===c?255:0===o?0:m(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(){_=m(u-s,0),f=m(l-a,0),g=m(c-o,0)},average:function(){_=(u+s)/2,f=(l+a)/2,g=(c+o)/2},negation:function(){_=255-p(255-s-u),f=255-p(255-a-l),g=255-p(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=K.getContext(1,1);e.each(y,function(t,e){x.save();var i="darken"===e,n=!1;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)),w[e]=n,x.restore()}),K.release(x),this.process=function(t,e,i,n,r){var v=e.canvas,m="normal"===t;if(m||w[t])i.save(),i.setTransform(1,0,0,1,0,0),i.globalAlpha=n,m||(i.globalCompositeOperation=t),i.drawImage(v,r.x,r.y),i.restore();else{var p=y[t];if(!p)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],p();var z=h*n/255,M=1-z;b[S]=z*_+M*u,b[S+1]=z*f+M*l,b[S+2]=z*g+M*c,b[S+3]=h*n+M*d}i.putImageData(x,r.x,r.y)}}},te=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],font:["font-family","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},{}),get:"get"+n,set:"set"+n}},{}),ee={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=ee[i];"number"==typeof n&&(n=S.number(n)),r?t.setAttributeNS(r,i,n):t.setAttribute(i,n)}return t}function n(e,i){return t(document.createElementNS("http://www.w3.org/2000/svg",e),i)}function r(t,e,i){var n=t._matrix,r=n.getTranslation(),s={};if(e){n=n.shiftless();var a=n._inverseTransform(r);s[i?"cx":"x"]=a.x,s[i?"cy":"y"]=a.y,r=null}if(n.isIdentity())return s;var h=n.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;r&&!r.isZero()&&u.push("translate("+S.point(r)+")"),o.isZero(c.x-1)&&o.isZero(c.y-1)||u.push("scale("+S.point(c)+")"),l&&u.push("rotate("+S.number(l)+")"),s.transform=u.join(" ")}else s.transform="matrix("+n.getValues().join(",")+")";return s}function s(e,i){for(var s=r(e),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=b(l,i);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),w(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function h(t){var e=r(t,!0),i=t.getSize();return e.x-=i.width/2,e.y-=i.height/2,e.width=i.width,e.height=i.height,e.href=t.toDataURL(),n("image",e)}function u(t,e){if(e.matchShapes){var r=t.toShape(!1);if(r)return c(r,e)}var s,a,o=t._segments;if(0===o.length)return null;if(t.isPolygon())if(o.length>=3){s=t._closed?"polygon":"polyline";var h=[];for(i=0,l=o.length;l>i;i++)h.push(S.point(o[i]._point));a={points:h.join(" ")}}else{s="line";var u=o[0]._point,d=o[o.length-1]._point;a={x1:u.x,y1:u.y,x2:d.x,y2:d.y}}else{s="path";var _=t.getPathData();a=_&&{d:_}}return n(s,a)}function c(t){var e=t._shape,i=t._radius,s=r(t,!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,i.isZero()&&(i=null)}return i&&("circle"===e?s.r=i:(s.rx=i.width,s.ry=i.height)),n(e,s)}function d(t){var e=r(t,!0),i=t.getPathData();return i&&(e.d=i),n("path",e)}function _(t,e){var i=r(t,!0),s=t.getSymbol(),a=p(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:S.rectangle(h)}),a.appendChild(b(o,e)),w(s,a,"symbol")),i.href="#"+a.id,i.x+=h.x,i.y+=h.y,i.width=S.number(h.width),i.height=S.number(h.height),n("use",i)}function f(t){var e=p(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))}w(t,e,"color")}return"url(#"+e.id+")"}function g(t){var e=n("text",r(t,!0));return e.textContent=t._content,e}function v(i,n){var r={},s=i.getParent();return null!=i._name&&(r.id=i._name),e.each(te,function(t){var n=t.get,a=t.type,o=i[n]();if(!s||!e.equals(s[n](),o)){if("color"===a&&null!=o){var h=o.getAlpha();1>h&&(r[t.attribute+"-opacity"]=h)}r[t.attribute]=null==o?"none":"number"===a?S.number(o):"color"===a?o.gradient?f(o,i):o.toCSS(!0):"array"===a?o.join(","):"lookup"===a?t.toSVG[o]:o}}),1===r.opacity&&delete r.opacity,null==i._visibility||i._visibility||(r.visibility="hidden"),t(n,r)}function p(t,e){return P||(P={ids:{},svgs:{}}),t&&P.svgs[e+"-"+t._id]}function w(t,e,i){P||p();var n=P.ids[i]=(P.ids[i]||0)+1;e.id=i+"-"+n,P.svgs[i+"-"+t._id]=e}function x(t,e){var i=t,r=null;if(P){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in P.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(P.svgs[s]);P=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function b(t,e){var i=z[t._type],n=i&&i(t,e);return n&&t._data&&n.setAttribute("data-paper-data",JSON.stringify(t._data)),n&&v(t,n)}function C(t){return t||(t={}),S=new a(t.precision),t -}var S,P,z={group:s,layer:s,raster:h,path:u,shape:c,"compound-path":d,"placed-symbol":_,"point-text":g};y.inject({exportSVG:function(t){return t=C(t),x(b(this,t),t)}}),m.inject({exportSVG:function(t){t=C(t);for(var e=this.layers,i=this.view.getSize(),r=n("svg",{x:0,y:0,width:i.width,height:i.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),s=0,a=e.length;a>s;s++)r.appendChild(b(e[s],t));return x(r,t)}})},new function(){function i(t,e,i,n){var r=ee[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 c(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 w,o=a._project,h=o._currentStyle,u=[];s||(a._transformContent=!1,a=x(a,t,i),o._currentStyle=a._style.clone());for(var l=0,c=r.length;c>l;l++){var d,_=r[l];1!==_.nodeType||!(d=P(_,!1,n))||d instanceof p||u.push(d)}return a.addChildren(u),s&&(a=x(a.reduce(),t,i)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){var i=new O,n=t.points;i.moveTo(n.getItem(0));for(var r=1,s=n.numberOfItems;s>r;r++)i.lineTo(n.getItem(r));return"polygon"===e&&i.closePath(),i}function u(t){var e=t.getAttribute("d"),i=e.match(/m/gi).length>1?new L:new O;return i.setPathData(e),i}function l(t,e){for(var r=t.childNodes,s=[],a=0,o=r.length;o>a;a++){var h=r[a];1===h.nodeType&&s.push(x(new F,h))}var u,l,c,d="radialgradient"===e,_=new E(s,d);return d?(u=n(t,"cx","cy"),l=u.add(i(t,"r"),0),c=n(t,"fx","fy",!0)):(u=n(t,"x1","y1"),l=n(t,"x2","y2")),x(new j(_,u,l,c),t),null}function d(t,e,i,n){for(var r=(n.getAttribute(i)||"").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("("),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 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":case"skewY":var e=Math.tan(c[0]*Math.PI/180),f="skewX"==l;s.shear(f?e:0,f?0:e)}}t.transform(s)}function f(t,e,i){var n=t["fill-opacity"===i?"getFillColor":"getStrokeColor"]();n&&n.setAlpha(parseFloat(e))}function v(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 x(i,n,r){var s={node:R.getStyles(n)||{},parent:!r&&R.getStyles(n.parentNode)||{}};return e.each(M,function(r,a){var o=v(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,e,i){function n(t){paper=s;var n=P(t,e,i),r=i.onLoad,a=s.project&&s.project.view;r&&r.call(this,n),a.draw(!0)}if(!t)return null;i?"function"==typeof i&&(i={onLoad:i}):i={};var r=t,s=paper;if(e)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:l,radialgradient:l,image:function(t){var e=new C(i(t,"href",!0));return e.attach("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 b.Circle(n(t,"cx","cy"),i(t,"r"))},ellipse:function(t){return new b.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 b.Rectangle(new _(e,i),s)},line:function(t){return new O.Line(n(t,"x1","y1"),n(t,"x2","y2"))},text:function(t){var e=new N(n(t,"x","y").add(n(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},M=e.each(te,function(t){this[t.attribute]=function(e,i){if(e[t.set](s(i,t.type,t.fromSVG)),"color"===t.type&&e instanceof b){var n=e[t.get]();n&&n.transform((new g).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 w))return new w(i,t);t.insertChild(0,i)}},gradientTransform:d,transform:d,"fill-opacity":f,"stroke-opacity":f,visibility:function(t,e){t.setVisible("visible"===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 _(s(e,"array")),h=r(n,"width","height",!0);if(t instanceof w){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"!=v(n,"overflow",a),d=t._definition;c&&!o.contains(d.getBounds())&&(c=new b.Rectangle(o).transform(d._matrix),c.setClipMask(!0),d.addChild(c))}}}),I={};y.inject({importSVG:function(t,e){return this.addChild(P(t,!0,e))}}),m.inject({importSVG:function(t,e){return this.activate(),P(t,!0,e)}})},paper=new(r.inject(new e(e.exports,{enumerable:!0,Base:e,Numerical:o,DomElement:R,DomEvent:V,Http:$,Key:X}))),"function"==typeof define&&define.amd&&define("paper",paper),paper}; \ No newline at end of file +var paper=new function(t){var e=new function(){function i(i,n,r,a,o){function h(s,a,h){var a=a||(a=u(n,s))&&(a.get?a:a.value);"string"==typeof a&&"#"===a[0]&&(a=i[a.substring(1)]||a);var d,_="function"==typeof a,f=a,g=o||_?a&&a.get?s in i:i[s]:null;!(h||a!==t&&n.hasOwnProperty(s))||o&&g||(_&&g&&(a.base=g),_&&l&&(d=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(l[d[3].toLowerCase()+d[4]]=d[2]),f&&!_&&f.get&&"function"==typeof f.get&&e.isPlainObject(f)||(f={value:f,writable:!0}),(u(i,s)||{configurable:!0}).configurable&&(f.configurable=!0,f.enumerable=r),c(i,s,f))}var l;if(n){l={};for(var d in n)n.hasOwnProperty(d)&&!s.test(d)&&h(d,null,!0);h("toString"),h("valueOf");for(var d in l){var _=l[d],f=i["set"+_],g=i["get"+_]||f&&i["is"+_];g&&(0===g.length||f)&&h(d,{get:g,set:f},!0)}}return i}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){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i]);return t}var s=/^(statics|preserve|enumerable|prototype|toString|valueOf)$/,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=this.prototype,n=Object.getPrototypeOf(e).constructor,r=t.statics===!0?t:t.statics;r!=t&&i(e,t,t.enumerable,n&&n.prototype,t.preserve),i(this,r,!0,n,t.preserve)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e=this,n=0,r=arguments.length;r>n&&!(t=arguments[n].initialize);n++);return t=t||function(){e.apply(this,arguments)},t.prototype=h(this.prototype),t.base=e,c(t.prototype,"constructor",{value:t,writable:!0,configurable:!0}),i(t,this,!0),arguments.length?this.inject.apply(t,arguments):t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++)i(this,arguments[t],arguments[t].enumerable);return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return n(this,t,e)},clone:function(){return new this.constructor(this)},statics:{each:n,create:h,define:c,describe:u,copy: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(){for(var e=0,i=arguments.length;i>e;e++)if(arguments[e]!==t)return arguments[e];return null}}})};"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?a.instance.number(t):"string"===i?"'"+t+"'":t))}},[]).join(", ")+" }"},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(i,n){if(i&&e.isPlainObject(i)){var r=i._filtering||i;for(var s in r)if(s in this&&r.hasOwnProperty(s)&&(!n||!n[s])){var a=i[s];a!==t&&(this[s]=a)}return!0}},statics:{exports:{enumerable:!0},extend:function ie(){var t=ie.base.apply(this,arguments),i=t.prototype._class;return i&&!e.exports[i]&&(e.exports[i]=t),t},equals:function(t,i){function n(t,e){for(var i in t)if(t.hasOwnProperty(i)&&!e.hasOwnProperty(i))return!1;return!0}if(t===i)return!0;if(t&&t.equals)return t.equals(i);if(i&&i.equals)return i.equals(t);if(Array.isArray(t)&&Array.isArray(i)){if(t.length!==i.length)return!1;for(var r=0,s=t.length;s>r;r++)if(!e.equals(t[r],i[r]))return!1;return!0}if(t&&"object"==typeof t&&i&&"object"==typeof i){if(!n(t,i)||!n(i,t))return!1;for(var r in t)if(t.hasOwnProperty(r)&&!e.equals(t[r],i[r]))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){return this.isPlainObject(t)||Array.isArray(t)},serialize:function(t,i,n,r){i=i||{};var s,o=!r;if(o&&(i.formatter=new a(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){s=t._serialize(i,r);var h=t._class;!h||n||s._compact||s[0]===h||s.unshift(h)}else if(Array.isArray(t)){s=[];for(var u=0,l=t.length;l>u;u++)s[u]=e.serialize(t[u],i,n,r);n&&(s._compact=!0)}else if(e.isPlainObject(t)){s={};for(var u in t)t.hasOwnProperty(u)&&(s[u]=e.serialize(t[u],i,n,r))}else s="number"==typeof t?i.formatter.number(t,i.precision):t;return o&&r.length>0?[["dictionary",r.definitions],s]:s},deserialize:function(t,i,n){var r=t,s=!n;if(n=n||{},Array.isArray(t)){var a=t[0],o="dictionary"===a;if(!o){if(n.dictionary&&1==t.length&&/^#/.test(a))return n.dictionary[a];a=e.exports[a]}r=[];for(var h=a?1:0,u=t.length;u>h;h++)r.push(e.deserialize(t[h],i,n));if(o)n.dictionary=r[0];else if(a){var l=r;i?r=i(a,l,s):(r=e.create(a.prototype),a.apply(r,l))}}else if(e.isPlainObject(t)){r={};for(var c in t)r[c]=e.deserialize(t[c],i,n)}return r},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,r){var s=i&&i.constructor===t?i:e.create(t.prototype),a=s===i;if(!r&&1===n.length&&s instanceof y&&(!(s instanceof x)||a)){var o=n[0];e.isPlainObject(o)&&(o.insert=!1)}return t.apply(s,n),a&&(i=null),s})},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 n={attach:function(i,n){if("string"!=typeof i)return e.each(i,function(t,e){this.attach(e,t)},this),t;var r=this._eventTypes[i];if(r){var s=this._handlers=this._handlers||{};s=s[i]=s[i]||[],-1==s.indexOf(n)&&(s.push(n),r.install&&1==s.length&&r.install.call(this,i))}},detach:function(i,n){if("string"!=typeof i)return e.each(i,function(t,e){this.detach(e,t)},this),t;var r,s=this._eventTypes[i],a=this._handlers&&this._handlers[i];s&&a&&(!n||-1!=(r=a.indexOf(n))&&1==a.length?(s.uninstall&&s.uninstall.call(this,i),delete this._handlers[i]):-1!=r&&a.splice(r,1))},once:function(t,e){this.attach(t,function(){e.apply(this,arguments),this.detach(t,e)})},fire:function(t,e){var i=this._handlers&&this._handlers[t];if(!i)return!1;for(var n=[].slice.call(arguments,1),r=this,s=0,a=i.length;a>s;s++)if(i[s].apply(r,n)===!1&&e&&e.stop){e.stop();break}return!0},responds:function(t){return!(!this._handlers||!this._handlers[t])},on:"#attach",off:"#detach",trigger:"#fire",_installEvents:function(t){var e=this._handlers,i=t?"install":"uninstall";for(var n in e)if(e[n].length>0){var r=this._eventTypes[n],s=r[i];s&&s.call(this,n)}},statics:{inject:function ne(){for(var t=0,i=arguments.length;i>t;t++){var n=arguments[t],r=n._events;if(r){var s={};e.each(r,function(t,i){var r="string"==typeof t,a=r?t:i,o=e.capitalize(a),h=a.substring(2).toLowerCase();s[h]=r?{}:t,a="_"+a,n["get"+o]=function(){return this[a]},n["set"+o]=function(t){var e=this[a];e&&this.detach(h,e),t&&this.attach(h,t),this[a]=t}}),n._eventTypes=s}ne.base.call(this,n)}return this}}},r=e.extend({_class:"PaperScope",initialize:function re(t){if(paper=this,this.settings={applyMatrix:!0,handleSize:4,hitTolerance:0},this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=t&&(t.getAttribute("id")||t.src)||"paperscope-"+re._id++,t&&t.setAttribute("id",this._id),re._scopes[this._id]=this,!this.support){var e=K.getContext(1,1);re.prototype.support={nativeDash:"setLineDash"in e||"mozDash"in e,nativeBlendModes:Q.nativeModes},K.release(e)}},version:"0.9.16",getView:function(){return this.project&&this.project.view},getPaper:function(){return this},execute:function(t){paper.PaperScript.execute(t,this),H.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 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 r._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 t&&t.getAttribute&&(t=t.getAttribute("id")),this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),s=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.fire("deactivate"),this._scope[this._reference]=this,this.fire("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)}}),a=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},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)}});a.instance=new a;var o=new function(){function e(e,i,n){var r=i===t,s=i-c,a=n+c,o=0;return function(t){return(r||t>s&&a>t)&&(e[o++]=i>t?i:t>n?n:t),o}}var i=[[.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]],r=Math.abs,s=Math.sqrt,a=Math.pow,h=Math.cos,u=Math.PI,l=1e-5,c=1e-11;return{TOLERANCE:l,EPSILON:c,KAPPA:4*(s(2)-1)/3,isZero:function(t){return r(t)<=c},integrate:function(t,e,r,s){for(var a=i[s-2],o=n[s-2],h=.5*(r-e),u=h+e,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,i,n,s,a,o){for(var h=0;a>h;h++){var u=t(i),l=u/e(i),c=i-l;if(r(l)0?(s=i,i=n>=c?.5*(n+s):c):(n=i,i=c>=s?.5*(n+s):c)}return i},solveQuadratic:function(t,i,n,a,o,h){var u=e(a,o,h);if(r(t)=c?u(-n/i):r(n)_)return 0;var f=_>d?s(_-d):0,g=u(f-l);return f>0&&(g=u(-f-l)),g},solveCubic:function(t,i,n,l,d,_,f){if(r(t)0?1:-1;return g(2*-b*x-i),g(b*x-i)}if(0>w){var x=s(p),C=Math.acos(m/(x*x*x))/3,S=-2*x,P=2*u/3;return g(S*h(C)-i),g(S*h(C+P)-i),g(S*h(C-P)-i)}var z=(m>0?-1:1)*a(r(m)+s(w),1/3);return g(z+p/z-i)}}},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=a.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)]},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)},transform:function(t){return t?t._transformPoint(this):this},getDistance:function(t,i){var n=h.read(arguments),r=n.x-this.x,s=n.y-this.y,a=r*r+s*s;return i=e.read(arguments),i?a:Math.sqrt(a)},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();o.isZero(i)&&this.getAngle(),this.set(this.x*i,this.y*i)}},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},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();return o.isZero(e)?0/0:Math.acos(this.dot(t)/e)}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},getDirectedAngle:function(t){var e=t;return e=h.read(arguments),180*Math.atan2(this.cross(e),this.dot(e))/Math.PI},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},isInside:function(t){return t.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 c(this.width,this.height)},toString:function(){var t=a.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=c.read(arguments);return new c(this.width+t.width,this.height+t.height)},subtract:function(){var t=c.read(arguments);return new c(this.width-t.width,this.height-t.height)},multiply:function(){var t=c.read(arguments);return new c(this.width*t.width,this.height*t.height)},divide:function(){var t=c.read(arguments);return new c(this.width/t.width,this.height/t.height)},modulo:function(){var t=c.read(arguments);return new c(this.width%t.width,this.height%t.height)},negate:function(){return new c(-this.width,-this.height)},isZero:function(){return o.isZero(this.width)&&o.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new c(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new c(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new c(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new c(e(this.width),e(this.height))}},{})),d=c.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)}}),_=e.extend({_class:"Rectangle",_readIndex:!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"),l=e.peek(arguments);if(this.x=u.x,this.y=u.y,l&&l.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 _=c.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 _(this.x,this.y,this.width,this.height)},equals:function(t){var i=e.isPlainValue(t)?_.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=a.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?c:d;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=c.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(_.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=_.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=_.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 _(e,i,n-e,r-i)},unite:function(){var t=_.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 _(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 _(e,i,n-e,r-i)},expand:function(){var t=c.read(arguments),e=t.width,i=t.height;return new _(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)}},new function(){return 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)}},{})}),f=_.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=_.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))}}))}),g=e.extend({_class:"Matrix",initialize:function se(t){var e=arguments.length,i=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof se?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(5))},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=a.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(){var t=this._owner;return t?(t.transform(null,!0),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;return this._a=t._a*e+t._c*i,this._b=t._b*e+t._d*i,this._c=t._a*n+t._c*r,this._d=t._b*n+t._d*r,this._tx+=t._tx*e+t._ty*i,this._ty+=t._tx*n+t._ty*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;return this._a=t._a*e+t._b*n,this._b=t._a*i+t._b*r,this._c=t._c*e+t._d*n,this._d=t._c*i+t._d*r,this._tx=t._a*s+t._b*a+t._tx,this._ty=t._c*s+t._d*a+t._ty,this._changed(),this},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,n,r){return arguments.length<5?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,i,n,r)},_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,n,r){for(var s=e,a=n,o=s+2*r;o>s;){var h=t[s++],u=t[s++];i[a++]=h*this._a+u*this._b+this._tx,i[a++]=h*this._c+u*this._d+this._ty}return i},_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,0,s,0,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 _),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)&&!o.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(o.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 a=Math.sqrt(i*i+n*n);return i/=a,n/=a,s/=a,e*i>t*n&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,a),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)}},new function(){return 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()}},{})}),v=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 v.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return v.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(v.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,i,n,r,s,a,u,l,c){l||(i-=t,n-=e,a-=r,u-=s);var d=u*i-a*n;if(!o.isZero(d)){var _=t-r,f=e-s,g=(a*f-u*_)/d,v=(i*f-n*_)/d;if((c||g>=0&&1>=g)&&(c||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,a){a||(i-=t,n-=e);var o=n/i,h=e-o*t;return(s-o*r-h)/Math.sqrt(o*o+1)}}}),p=s.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){s.call(this,!0),this.layers=[],this.symbols=[],this._currentStyle=new q,this.activeLayer=new x,t&&(this.view=t instanceof H?t:H.create(t)),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 this.layers.length<=1&&(!this.activeLayer||this.activeLayer.isEmpty())},remove:function ae(){return ae.base.call(this)?(this.view&&this.view.remove(),!0):!1},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},addChild:function(t){return t instanceof x?(e.splice(this.layers,[t]),this.activeLayer||(this.activeLayer=t)):t instanceof y?(this.activeLayer||this.addChild(new x(y.NO_INSERT))).addChild(t):t=null,t},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var i=this._selectedItems[e];i.isInserted()&&t.push(i)}return t},getOptions:function(){return this._scope.settings},_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=P.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}},new function(){function t(t,e,i){for(var n=t.layers,r=i&&[],s=0,a=n.length;a>s;s++){var o=n[s][i?"getItems":"getItem"](e);if(i)r.push.apply(r,o);else if(o)return o}return i?r:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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,trackTransforms:!0}),s=0,a=this.layers.length;a>s;s++)this.layers[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;for(var o in this._selectedItems){var u=this._selectedItems[o],l=this._scope.settings.handleSize;if(half=l/2,u._updateVersion===this._updateVersion&&(u._drawSelected||u._boundsSelected)){var c=u.getSelectedColor()||u.getLayer().getSelectedColor();t.strokeStyle=t.fillStyle=c?c.toCanvasStyle(t):"#009dec";var d=i.clone().concatenate(u._globalMatrix);if(u._drawSelected&&u._drawSelected(t,d),u._boundsSelected){var _=d._transformCorners(u.getInternalBounds());t.beginPath();for(var s=0;8>s;s++)t[0===s?"moveTo":"lineTo"](_[s],_[++s]);t.closePath(),t.stroke();for(var s=0;8>s;s++)t.fillRect(_[s]-half,_[++s]-half,l,l)}}}t.restore()}}}),m=e.extend({_class:"Symbol",initialize:function oe(t,e){this._id=oe._id=(oe._id||0)+1,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){4&t&&y._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(5)},place:function(t){return new S(this,t)},clone:function(){return new m(this._definition.clone(!1))}}),y=e.extend(n,{statics:{extend:function he(t){t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields));var i=he.base.apply(this,arguments),n=i.prototype,r=n._class;return r&&(n._type=e.hyphenate(r)),i},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,matrix:new g,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,applyMatrix:null,data:{}},initialize:function(){},_initialize:function(t,e){var i=t&&t.internal===!0,n=this._matrix=new g;return project=paper.project,i||(this._id=y._id=(y._id||0)+1),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,e&&n.translate(e),n._owner=this,this._style=new q(project._currentStyle,this),this._project||(i||t&&t.insert===!1?this._setProject(project):(project.activeLayer||new x).addChild(this)),t&&t!==y.NO_INSERT?this._set(t,{insert:!0}):!0},_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._project.view._eventCounters;if(i)for(var n in t)i[n]=(i[n]||0)+(t[n][e]||0)},uninstall:function(e){var i=this._project.view._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._project.view._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 w||n(this._style._defaults),[this._class,r]},_changed:function(e){var i=this._parentSymbol,n=this._parent||i,r=this._project;if(4&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=t),n&&12&e&&y._clearBoundsCache(n),2&e&&y._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,{insert:!0}),this},getId:function(){return this._id},getType:function(){return this._type},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.");if(e&&this._parent){for(var n=this._parent._children,r=this._parent._namedChildren,s=e,a=1;i&&n[e];)e=s+" "+a++;(r[e]=r[e]||[]).push(this),n[e]=this}this._name=e||t,this._changed(32)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()}},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?32:33))}},{}),{_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=0,e=this._children.length;e>t;t++)if(this._children[t].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var i=0,n=this._children.length;n>i;i++)this._children[i].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(33))},_selected:!1,isFullySelected:function(){if(this._children&&this._selected){for(var t=0,e=this._children.length;e>t;t++)if(!this._children[t].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){if(this._children)for(var e=0,i=this._children.length;i>e;e++)this._children[e].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(33),this._parent&&this._parent._changed(256))},_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,"setAnchor")}return e},setPivot:function(){this._pivot=h.read(arguments),this._position=t},_pivot:null,getRegistration:"#getPivot",setRegistration:"#setPivot"},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,null,r);return"bounds"===t?new f(a.x,a.y,a.width,a.height,this,"setBounds"):a},this[t]={get:this[i]}},{_getBounds:function(t,e,i){var n=this._children;if(!n||0==n.length)return new _;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,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 _(r,a,s-r,o-a):new _},setBounds:function(){var t=_.read(arguments),e=this.getBounds(),i=new g,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,a=this._parent||this._parentSymbol;if(i&&a){var o=i._id,h=a._boundsCache=a._boundsCache||{ids:{},list:[]};h.ids[o]||(h.list.push(i),h.ids[o]=i)}if(s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();e=e?r?e.clone().concatenate(r):e:r;var u=this._getBounds(n||t,e,s?this:i);if(s){this._bounds||(this._bounds={});var l=this._bounds[s]=u.clone();l._internal=!!n}return u},statics:{_clearBoundsCache:function(e){if(e._boundsCache){for(var i=0,n=e._boundsCache.list,r=n.length;r>i;i++){var s=n[i];s._bounds=s._position=t,s!==e&&s._boundsCache&&s._clearBoundsCache()}e._boundsCache=t}}}}),{_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(){var t=this._decomposed||this._decompose();return t&&t.scaling},setScaling:function(){var t=this.getScaling();if(null!=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(t){this._matrix.initialize(t),this._applyMatrix?this.transform(null,!0):this._changed(5)},getGlobalMatrix:function(){var t=this._globalMatrix,e=this._project._updateVersion;return t&&t._updateVersion!==e&&(t=null),t||(t=this._globalMatrix=this._matrix.clone(),this._parent&&t.preConcatenate(this._parent.getGlobalMatrix()),t._updateVersion=e),t},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)},_installEvents:function ue(t){ue.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 x)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},isInserted:function(){return this._parent?this._parent.isInserted():!1},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(y.NO_INSERT),t)},_clone:function(i,n){if(i.setStyle(this._style),this._children)for(var r=0,s=this._children.length;s>r;r++)i.addChild(this._children[r].clone(!1),!0);(n||n===t)&&i.insertAbove(this);for(var a=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide","_applyMatrix"],r=0,s=a.length;s>r;r++){var o=a[r];this.hasOwnProperty(o)&&(i[o]=this[o])}return i._matrix.initialize(this._matrix),i._data=this._data?e.clone(this._data):null,i.setSelected(this._selected),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=this._project.view,r=(t||n&&n.getResolution()||72)/72,s=i.getTopLeft().floor(),a=i.getBottomRight().ceil();size=new c(a.subtract(s)),canvas=K.getCanvas(size.multiply(r)),ctx=canvas.getContext("2d"),matrix=(new g).scale(r).translate(s.negate()),ctx.save(),matrix.applyToContext(ctx),this.draw(ctx,new e({transforms:[matrix]})),ctx.restore();var o=new C(y.NO_INSERT);return o.setCanvas(canvas),o.transform((new g).translate(s.add(size.divide(2))).scale(1/r)),o.insertAbove(this),o},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())},hitTest:function(i,n){function r(n,r){var s=v["get"+r]();return i.subtract(s).divide(l).length<=1?new P(n,f,{name:e.hyphenate(r),point:s}):t}if(i=h.read(arguments),n=P.getOptions(e.read(arguments)),this._locked||!this._visible||this._guide&&!n.guides)return null;var s=this._matrix,a=n._totalMatrix,o=this._project.view,u=n._totalMatrix=a?a.clone().concatenate(s):this.getGlobalMatrix().clone().preConcatenate(o?o._matrix:new g),l=n._tolerancePadding=new c(T._getPenPadding(1,u.inverted())).multiply(Math.max(n.tolerance,1e-5));if(i=s._inverseTransform(i),!this._children&&!this.getInternalRoughBounds().expand(l.multiply(2))._containsPoint(i))return null;var d,_=!(n.guides&&!this._guide||n.selected&&!this._selected||n.type&&this._type!==n.type),f=this;if(_&&(n.center||n.bounds)&&this._parent){var v=this.getInternalBounds();if(n.center&&(d=r("center","Center")),!d&&n.bounds)for(var p=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],m=0;8>m&&!d;m++)d=r("bounds",p[m])}var y=!d&&this._children;if(y)for(var w=this._getChildHitTestOptions(n),m=y.length-1;m>=0&&!d;m--)d=y[m].hitTest(i,w);return!d&&_&&(d=this._hitTest(i,n)),d&&d.point&&(d.point=s.transform(d.point)),n._totalMatrix=a,d},_getChildHitTestOptions:function(t){return t},_hitTest:function(e,i){return i.fill&&this.hasFill()&&this._contains(e)?new P("fill",this):t},matches:function(t){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}for(var n in t)if(t.hasOwnProperty(n)){var r=this[n],s=t[n];if(s instanceof RegExp){if(!s.test(r))return!1}else if("function"==typeof s){if(!s(r))return!1}else if(e.isPlainObject(s)){if(!i(s,r))return!1}else if(!e.equals(r,s))return!1}return!0}},new function(){function t(e,i,n){for(var r=e._children,s=n&&[],a=0,o=r&&r.length;o>a;a++){var h=r[a];if(h.matches(i)){if(!n)return h;s.push(h)}var u=t(h,i,n);if(n)s.push.apply(s,u);else if(u)return u}return n?s:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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=this.insertChildren(t,[e],i);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];r&&o._type!==r?i.splice(a,1):o._remove(!0)}e.splice(s,i,t,0);for(var a=0,h=i.length;h>a;a++){var o=i[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name)}this._changed(7)}else i=null;return i},_insert:function(t,e,i){if(!e._parent)return null;var n=e._index+(t?1:0);return e._parent===this._parent&&n>this._index&&n--,e._parent.insertChild(n,this,i)},insertAbove:function(t,e){return this._insert(!0,t,e)},insertBelow:function(t,e){return this._insert(!1,t,e)},sendToBack:function(){return this._parent.insertChild(0,this)},bringToFront:function(){return this._parent.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._children,e=this._parent._namedChildren,i=this._name,n=e[i],r=n?n.indexOf(this):-1;-1!=r&&(t[i]==this&&delete t[i],n.splice(r,1),n.length?t[i]=n[n.length-1]:delete e[i])},_remove:function(t){return this._parent?(this._name&&this._removeNamed(),null!=this._index&&e.splice(this._parent._children,null,this._index,1),this._installEvents(!1),t&&this._parent._changed(7),this._parent=null,!0):!1},remove:function(){return this._remove(!0)},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(!1);return n.length>0&&this._changed(7),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(7)}},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},_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},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|compound-path)$/.test(e._type)&&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),i=h.read(arguments,0,{readNull:!0});return this.transform((new g)[t](e,i||this.getPosition(!0)))}},{}),{transform:function(t,e){t&&t.isIdentity()&&(t=null);var i=this._matrix,n=(e||this._applyMatrix)&&(!i.isIdentity()||t);if(!t&&!n)return this;if(t&&i.preConcatenate(t),n=n&&this._transformContent(i)){var r=this._pivot,s=this._style,a=s.getFillColor(!0),o=s.getStrokeColor(!0);r&&r.transform(i),a&&a.transform(i),o&&o.transform(i),i.reset(!0)}var h=this._bounds,u=this._position;this._changed(5);var l=h&&t&&t.decompose();if(l&&!l.shearing&&0===l.rotation%90){for(var c in h){var d=h[c];(n||!d._internal)&&t._transformBounds(d,d)}var _=this._boundsGetter,d=h[_&&_.getBounds||_||"getBounds"];d&&(this._position=d.getCenter(!0)),this._bounds=h}else t&&u&&(this._position=t._transformPoint(u,u));return this},_transformContent:function(t){var e=this._children;if(e){for(var i=0,n=e.length;n>i;i++)e[i].transform(t,!0);return!0}},globalToLocal:function(){var t=this.getGlobalMatrix();return t&&t._inverseTransform(h.read(arguments))},localToGlobal:function(){var t=this.getGlobalMatrix();return t&&t._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=_.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 _(new h,new c(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){if(this._visible&&0!==this._opacity){var i=this._updateVersion=this._project._updateVersion,n=e.trackTransforms,r=e.transforms=e.transforms||[new g],s=this._matrix,a=r[r.length-1],o=a.clone().concatenate(s);if(o.isInvertible()){n&&(r||(r=e.transforms=[]),r.push(this._globalMatrix=o),o._updateVersion=i);var h,u,l,d=this._blendMode,_=this._opacity,f="normal"===d,v=Q.nativeModes[d],p=f&&1===_||(v||f&&1>_)&&this._canComposite();if(!p){var m=this.getStrokeBounds(a);if(!m.width||!m.height)return;l=e.offset,u=e.offset=m.getTopLeft().floor(),h=t,t=K.getContext(m.getSize().ceil().add(new c(1,1)),e.pixelRatio)}t.save(),p?(t.globalAlpha=_,v&&(t.globalCompositeOperation=d)):t.translate(-u.x,-u.y),(p?s:o).applyToContext(t),!p&&e.clipItem&&e.clipItem.draw(t,e.extend({clip:!0})),this._draw(t,e),t.restore(),n&&r.pop(),e.clip&&t.clip(),p||(Q.process(d,t,h,_,u.subtract(l).multiply(e.pixelRatio)),K.release(t),e.offset=l)}}},_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}})),w=y.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 le(e){le.base.call(this,e),258&e&&(this._clipItem=t)},_getClipItem:function(){if(this._clipItem!==t)return this._clipItem;for(var e=0,i=this._children.length;i>e;e++){var n=this._children[e];if(n._clipMask)return this._clipItem=n}return this._clipItem=null},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var i=e.clipItem=this._getClipItem();i&&i.draw(t,e.extend({clip:!0}));for(var n=0,r=this._children.length;r>n;n++){var s=this._children[n];s!==i&&s.draw(t,e)}e.clipItem=null}}),x=w.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,w.call(this,n),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function ce(t){return this._parent?ce.base.call(this,t):null!=this._index?(this._project.activeLayer===this&&(this._project.activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(this._project.layers,null,this._index,1),this._installEvents(!1),this._project._needsUpdate=!0,!0):!1},getNextSibling:function de(){return this._parent?de.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function _e(){return this._parent?_e.base.call(this):this._project.layers[this._index-1]||null},isInserted:function fe(){return this._parent?fe.base.call(this):null!=this._index},activate:function(){this._project.activeLayer=this},_insert:function ge(t,i,n){return i instanceof x&&!i._parent?(this._remove(!0),e.splice(i._project.layers,[this],i._index+(t?1:0),0),this._setProject(i._project,!0),this):ge.base.call(this,t,i,n)}}),b=y.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{shape:null,size:null,radius:null},initialize:function(t){this._initialize(t)},_equals:function(t){return this._shape===t._shape&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new b(y.NO_INSERT);return e.setShape(this._shape),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getShape:function(){return this._shape},setShape:function(t){this._shape=t},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._shape,i=t.width,n=t.height;if("rectangle"===e){var r=c.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(5)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._shape?t:new d(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._shape;if("circle"===e){if(t===this._radius)return;var i=2*t;this._radius=t,this._size.set(i,i)}else if(t=c.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var i=c.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(5)},isEmpty:function(){return!1},toPath:function(i){var n=new(T[e.capitalize(this._shape)])({center:new h,size:this._size,radius:this._radius,insert:!1});return n.setStyle(this._style),n.transform(this._matrix),(i||i===t)&&n.insertAbove(this),n},_draw:function(t,e){var i=this._style,n=i.hasFill(),r=i.hasStroke(),s=e.clip;if(n||r||s){var a=this._radius,o=this._shape;if(t.beginPath(),"circle"===o)t.arc(0,0,a,0,2*Math.PI,!0);else{var h=a.width,u=a.height,l=.5522847498307936;if("ellipse"===o){var c=h*l,d=u*l;t.moveTo(-h,0),t.bezierCurveTo(-h,-d,-c,-u,0,-u),t.bezierCurveTo(c,-u,h,-d,h,0),t.bezierCurveTo(h,d,c,u,0,u),t.bezierCurveTo(-c,u,-h,d,-h,0)}else{var _=this._size,f=_.width,g=_.height;if(0===h&&0===u)t.rect(-f/2,-g/2,f,g);else{l=1-l;var v=f/2,p=g/2,c=h*l,d=u*l;t.moveTo(-v,-p+u),t.bezierCurveTo(-v,-p+d,-v+c,-p,-v+h,-p),t.lineTo(v-h,-p),t.bezierCurveTo(v-c,-p,v,-p+d,v,-p+u),t.lineTo(v,p-u),t.bezierCurveTo(v,p-d,v-c,p,v-h,p),t.lineTo(-v+h,p),t.bezierCurveTo(-v+c,p,-v,p-d,-v,p-u)}}}t.closePath()}s||!n&&!r||(this._setStyles(t),n&&(t.fill(i.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),r&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var i=new _(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 _(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._shape){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},_hitTest:function n(i,r){var s=!1;if(this.hasStroke()){var a=this._shape,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 _(this._size).setCenter(0,0),d=c.expand(h),f=c.expand(-h); +s=d._containsPoint(i)&&!f._containsPoint(i)}}else"ellipse"===a&&(o=e(i,o)),s=2*Math.abs(i.getLength()-o)<=h}return s?new P("stroke",this):n.base.apply(this,arguments)}}},{statics:new function(){function t(t,i,n,r,s){var a=new b(e.getNamed(s));return a._shape=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 c(2*n),n,arguments)},Rectangle:function(){var e=_.readNamed(arguments,"rectangle"),i=c.min(c.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),i,arguments)},Ellipse:function(){var e=b._readEllipse(arguments);return radius=e.radius,t("ellipse",e.center,radius.multiply(2),radius,arguments)},_readEllipse:function(t){var i,n;if(e.hasNamed(t,"radius"))i=h.readNamed(t,"center"),n=c.readNamed(t,"radius");else{var r=_.readNamed(t,"rectangle");i=r.getCenter(!0),n=r.getSize(!0).divide(2)}return{center:i,radius:n}}}}}),C=y.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{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 c)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new C(y.NO_INSERT),i=this._image,n=this._canvas;if(i)e.setImage(i);else if(n){var r=K.getCanvas(this._size);r.getContext("2d").drawImage(n,0,0),e.setCanvas(r)}return this._clone(e,t)},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(!this._size.equals(t)){var e=this.getElement();this.setCanvas(K.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}},getWidth:function(){return this._size.width},getHeight:function(){return this._size.height},isEmpty:function(){return 0==this._size.width&&0==this._size.height},getPpi: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 c(72/i.getLength(),72/n.getLength())},getImage:function(){return this._image},setImage:function(t){this._canvas&&K.release(this._canvas),t.getContext?(this._image=null,this._canvas=t):(this._image=t,this._canvas=null),this._size=new c(t.naturalWidth||t.width,t.naturalHeight||t.height),this._context=null,this._changed(133)},getCanvas:function(){if(!this._canvas){var t=K.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){K.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(129)),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._project.view;t&&(paper=t._scope),n.setImage(i),n.fire("load"),t&&t.update()}var i,n=this;i=document.getElementById(t)||new Image,i.naturalWidth&&i.naturalHeight?setTimeout(e,0):(Z.add(i,{load:e}),i.src||(i.src=t)),this.setImage(i)},getElement:function(){return this._canvas||this._image},getSubCanvas:function(t){var t=_.read(arguments),e=K.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(t){var t=_.read(arguments),e=new C(y.NO_INSERT);return e.setCanvas(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 O?(n=t,i=t.getBounds()):t.width?i=new _(t):t.x&&(i=new _(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=C._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=C._sampleContext=K.getContext(new c(r)),o.save();var h=(new g).scale(s/i.width,a/i.height).translate(-i.x,-i.y);h.applyToContext(o),n&&n.draw(o,new e({clip:!0,transforms:[h]})),this._matrix.applyToContext(o),o.drawImage(this.getElement(),-this._size.width/2,-this._size.height/2),o.restore();for(var u=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,l=[0,0,0],d=0,f=0,v=u.length;v>f;f+=4){var p=u[f+3];d+=p,p/=255,l[0]+=u[f]*p,l[1]+=u[f+1]*p,l[2]+=u[f+2]*p}for(var f=0;3>f;f++)l[f]/=d;return d?E.read(l):null},getPixel:function(t){var t=h.read(arguments),e=this.getContext().getImageData(t.x,t.y,1,1).data;return new E("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=E.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=c.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(t){var t=_.read(arguments);return t.isEmpty()&&(t=new _(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 _(this._size).setCenter(0,0);return e?e._transformBounds(i):i},_hitTest:function(t){if(this._contains(t)){var e=this;return new P("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}}),S=y.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 m?e:new m(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(5)},clone:function(t){var e=new S(y.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,i){return this.symbol._definition._getCachedBounds(t,e,i)},_hitTest:function(t,e,i){var n=this._symbol._definition._hitTest(t,e,i);return n&&(n.item=this),n},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),P=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 t&&t._merged?t: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,_merged:!0},t)}}}),z=e.extend({_class:"Segment",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.isLinear()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){var e=this._path;if(e){var i,n,r=e._curves,s=this._index;r&&(t&&t!==this._point&&t!==this._handleIn||!(i=r[s-1]||e._closed&&r[r.length-1])||i._changed(),t&&t!==this._point&&t!==this._handleOut||!(n=r[s])||n._changed()),e._changed(5)}},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)},isLinear:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},setLinear:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},isColinear:function(t){var e=this.getNext(),i=t.getNext();return this._handleOut.isZero()&&e._handleIn.isZero()&&t._handleOut.isZero()&&i._handleIn.isZero()&&e._point.subtract(this._point).isColinear(i._point.subtract(t._point))},isOrthogonal:function(){var t=this.getPrevious(),e=this.getNext();return t._handleOut.isZero()&&this._handleIn.isZero()&&this._handleOut.isZero()&&e._handleIn.isZero()&&this._point.subtract(t._point).isOrthogonal(e._point.subtract(this._point))},isArc:function(){var t=this.getNext(),e=this._handleOut,i=t._handleIn,n=.5522847498307936;if(e.isOrthogonal(i)){var r=this._point,s=t._point,a=new v(r,e,!0).intersect(new v(s,i,!0),!0);return a&&o.isZero(e.getLength()/a.subtract(r).getLength()-n)&&o.isZero(i.getLength()/a.subtract(s).getLength()-n)}return!1},_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(33))},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?(t._closed||e!=t._segments.length-1||e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new A(t,t.getNext()?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 z(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new z(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,0,e,0,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}}),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 o.isZero(this._x)&&o.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,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 z,this._segment2=new z;else if(1===h)this._segment1=new z(t.segment1),this._segment2=new z(t.segment2);else if(2===h)this._segment1=new z(t),this._segment2=new z(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 z(u,null,l),this._segment2=new z(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 I.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):I.getLength(this.getValues(),0,1)),this._length},getArea:function(){return I.getArea(this.getValues())},getPart:function(t,e){return new I(I.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return I.getLength(this.getValues(),t,e)},isLinear:function(){return this._segment1._handleOut.isZero()&&this._segment2._handleIn.isZero()},isHorizontal:function(){return this.isLinear()&&o.isZero(this._segment1._point._y-this._segment2._point._y)},getIntersections:function(t){return I.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-5,s=null;if(n>r&&1-r>n){var a=I.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 z(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 I(_,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 I(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 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(", ")+" }"},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,0,o,0,6),o},evaluate:function(t,e,i){var n,r,s=t[0],a=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],_=t[7],f=1e-5;if(0===i&&(f>e||e>1-f)){var g=f>e;n=g?s:d,r=g?a:_}else{var v=3*(o-s),p=3*(l-o)-v,m=d-s-v-p,y=3*(u-a),w=3*(c-u)-y,x=_-a-y-w;if(0===i)n=((m*e+p)*e+v)*e+s,r=((x*e+w)*e+y)*e+a;else if(f>e&&o===s&&u===a||e>1-f&&l===d&&c===_?(n=d-s,r=_-a):f>e?(n=v,r=y):e>1-f?(n=3*(d-l),r=3*(_-c)):(n=(3*m*e+2*p)*e+v,r=(3*x*e+2*w)*e+y),3===i){var b=6*m*e+2*p,C=6*x*e+2*w;return(n*C-r*b)/Math.pow(n*n+r*r,1.5)}}return 2===i?new h(r,-n):new h(n,r)},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 a=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-a),d=3*(u-h)-c,_=l-a-c-d;return o.solveCubic(_,d,c,a-i,n,r,s)},getParameterOf:function(t,e,i){var n=1e-5;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=I.subdivide(t,e)[1]),1>i&&(t=I.subdivide(t,(i-e)/(1-e))[0]),t},isLinear:function(t){var e=o.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 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},getBounds:function(t){for(var e=t.slice(0,2),i=e.slice(),n=[0,0],r=0;2>r;r++)I._addBounds(t[r],t[r+2],t[r+4],t[r+6],r,0,e,i,n);return new _(e[0],e[1],i[0]-e[0],i[1]-e[1])},_addBounds:function(t,e,i,n,r,s,a,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=o.solveQuadratic(c,d,_,u),g=1e-5,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]=T[t]([this._segment1,this._segment2],!1,this._path.getStyle())),e.clone()}},{}),e.each(["getPoint","getTangent","getNormal","getCurvature"],function(t,e){this[t+"At"]=function(t,i){var n=this.getValues();return I.evaluate(n,i?t:I.getParameterAt(n,t,0),e)},this[t]=function(t){return I.evaluate(this.getValues(),t,e)}},{getParameterAt:function(e,i){return I.getParameterAt(this.getValues(),e,i!==t?i:0>e?1:0)},getParameterOf:function(t){var t=h.read(arguments);return I.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){return e||(t=this.getParameterAt(t)),new A(this,t)},getLocationOf:function(t){var t=h.read(arguments),e=this.getParameterOf(t);return null!=e?new A(this,e):null},getNearestLocation:function(t){function e(e){if(e>=0&&1>=e){var n=t.getDistance(I.evaluate(i,e,0),!0);if(r>n)return r=n,s=e,!0}}for(var t=h.read(arguments),i=this.getValues(),n=100,r=1/0,s=0,a=0;n>=a;a++)e(a/n);for(var o=1/(2*n);o>1e-5;)e(s-o)||e(s+o)||(o/=2);var u=I.evaluate(i,s,0);return new A(this,s,u,null,null,null,t.getDistance(u))},getNearestPoint:function(t){var t=h.read(arguments);return this.getNearestLocation(t).getPoint()}}),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))))}return{statics:!0,getLength:function(n,r,s){r===t&&(r=0),s===t&&(s=1);var a=o.isZero;if(0===r&&1===s&&a(n[0]-n[2])&&a(n[1]-n[3])&&a(n[6]-n[4])&&a(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 o.integrate(l,r,s,i(r,s))},getParameterAt:function(t,n,r){function s(t){var e=i(r,t);return _+=t>r?o.integrate(l,r,t,e):-o.integrate(l,t,r,e),r=t,_-n}if(0===n)return r;var a=n>0,h=a?r:0,u=a?1:r,n=Math.abs(n),l=e(t),c=o.integrate(l,h,u,i(h,u));if(n>=c)return a?u:h;var d=n/c,_=0;return o.findRoot(s,l,a?h+d:u-d,h,u,16,1e-5)}}},new function(){function t(t,e,i,n,r,s,a,o){var h=new A(i,n,r,s,a,o);(!e||e(h))&&t.push(h)}function e(r,s,a,o,h,u,l,c,d,_,f,g,p){if(!(p>20)){var m,y,w,x=s[0],b=s[1],C=s[6],S=s[7],P=1e-5,z=1e-9,M=v.getSignedDistance,k=M(x,b,C,S,s[2],s[3])||0,A=M(x,b,C,S,s[4],s[5])||0,O=k*A>0?.75:4/9,T=O*Math.min(0,k,A),L=O*Math.max(0,k,A),N=M(x,b,C,S,r[0],r[1]),j=M(x,b,C,S,r[2],r[3]),D=M(x,b,C,S,r[4],r[5]),B=M(x,b,C,S,r[6],r[7]);if(x===C&&z>=_-d&&p>3)m=(c+l)/2,y=m,w=0;else{var E,R,F=i(N,j,D,B),q=F[0],V=F[1];if(E=n(q,V,T,L),q.reverse(),V.reverse(),R=n(q,V,T,L),null==E||null==R)return!1;r=I.getPart(r,E,R),w=R-E,m=c*E+l*(1-E),y=c*R+l*(1-R)}if(f>.8&&w>.8)if(y-m>_-d){var Z=I.subdivide(r,.5),H=m+(y-m)/2;e(s,Z[0],o,a,h,u,d,_,m,H,w,!g,++p),e(s,Z[1],o,a,h,u,d,_,H,y,w,!g,p)}else{var Z=I.subdivide(s,.5),H=d+(_-d)/2;e(Z[0],r,o,a,h,u,d,H,m,y,w,!g,++p),e(Z[1],r,o,a,h,u,H,_,m,y,w,!g,p)}else if(Math.max(_-d,y-m)l*c)r=[[s,a,h],[s,o,h]],d=0>l;else{var _,f=0,g=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||g?[[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){for(var r,s,a,o,h,u=null,l=0,c=e.length-1;c>l;l++){if(a=e[l][1],h=e[l+1][1],h>a)r=null;else{if(!(n>=h))continue;s=e[l][0],o=e[l+1][0],r=s+(n-a)*(o-s)/(h-a)}break}t[0][1]<=n&&(r=t[0][0]);for(var l=0,c=t.length-1;c>l;l++){if(a=t[l][1],h=t[l+1][1],a>=i)u=r;else if(a>h)u=null;else{if(!(h>=i))continue;s=t[l][0],o=t[l+1][0],u=s+(i-a)*(o-s)/(h-a)}break}return u}function r(e,i,n,r,s,a){for(var o=I.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=[],z=I.solveCubic(x,1,0,P,0,1),b=0;z>b;b++){var M=P[b],C=I.evaluate(x,M,0).x;if(C>=0&&y>=C){var k=I.getParameterOf(w,C,0),A=o?k:M,O=o?M:k;t(s,a,n,A,I.evaluate(e,A,0),r,O,I.evaluate(i,O,0))}}}function s(e,i,n,r,s,a){var o=v.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,I.getParameterOf(e,h,u),o,r,I.getParameterOf(i,h,u),o)}}return{statics:{getIntersections:function(t,i,n,a,o,h){var u=I.isLinear(t),l=I.isLinear(i);return(u&&l?s:u||l?r:e)(t,i,n,a,o,h,0,1,0,1,0,!1,0),o}}}}),A=e.extend({_class:"CurveLocation",initialize:function ve(t,e,i,n,r,s,a){this._id=ve._id=(ve._id||0)+1,this._curve=t,this._segment1=t._segment1,this._segment2=t._segment2,this._parameter=e,this._point=i,this._curve2=n,this._parameter2=r,this._point2=s,this._distance=a},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.getLength(0,i)f;f++)c[f]=a[f].getValues(h);for(var f=0;u>f;f++){var g=s[f],p=e?g.getValues(o):c[f];if(!e){var m=g.getSegment1(),y=g.getSegment2(),w=m._handleOut,x=y._handleIn;if(new v(m._point.subtract(w),w.multiply(2),!0).intersect(new v(y._point.subtract(x),x.multiply(2),!0),!1)){var b=I.subdivide(p);I.getIntersections(b[0],b[1],g,g,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++)I.getIntersections(p,c[C],g,a[C],r,!e&&(C===f+1||C===l-1&&0===f)&&function(t){var e=t._parameter;return e>=d&&_>=e})}for(var S=r.length-1,f=S;f>=0;f--){var P=r[f],z=P._curve.getNext(),M=P._curve2.getNext();z&&P._parameter>=_&&(P._parameter=0,P._curve=z),M&&P._parameter2>=_&&(P._parameter2=0,P._curve2=M)}if(S>0){r.sort(n);for(var f=S;f>=0;f--)r[f].equals(r[0===f?S:f-1])&&(r.splice(f,1),S--)}if(i){for(var f=S;f>=0;f--)r.push(r[f].getIntersection());r.sort(n)}return r},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,l=new h;this.clear();for(var d=0,_=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&&"z"!==v&&this.moveTo(u=l),v){case"m":case"l":var m="m"===v;m&&r&&"z"!==r&&this.closePath();for(var y=0;p>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=i(y));s=u,m&&(l=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 c(+n[0],+n[1]),+n[2],+n[4],+n[3]);break;case"z":this.closePath()}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)}}),T=O.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[];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 e.equals(this._segments,t._segments)},clone:function(e){var i=new T(y.NO_INSERT);return i.setSegments(this._segments),i._closed=this._closed,this._clockwise!==t&&(i._clockwise=this._clockwise),this._clone(i,e)},_changed:function pe(e){if(pe.base.call(this,e),4&e){if((this._compound?this._parent:this)._currentPath=t,this._length=this._clockwise=t,this._curves)for(var i=0,n=this._curves.length;n>i;i++)this._curves[i]._changed(5);this._monoCurves=t}else 8&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof L?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(z.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 I(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]},getPathData:function(t){function e(e,i,s){var a=e._point,o=i._point,h=e._handleOut,u=i._handleIn;if(h.isZero()&&u.isZero())s||r.push("L"+n.point(o,t));else{var l=o.subtract(a);r.push("c"+n.point(h,t)+" "+n.point(l.add(u),t)+" "+n.point(l,t))}}var i=this._segments,n=a.instance,r=[];if(0===i.length)return"";r.push("M"+n.point(i[0]._point));for(var s=0,o=i.length-1;o>s;s++)e(i[s],i[s+1],!1);return this._closed&&(e(i[i.length-1],i[0],!0),r.push("z")),r.join("")},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(5)}},isEmpty:function(){return 0===this._segments.length},isPolygon:function(){for(var t=0,e=this._segments.length;e>t;t++)if(!this._segments[t].isLinear())return!1;return!0},_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 I(this,null,null));this._adjustCurves(u,c) +}return this._changed(5),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=r[this._closed&&0===t?n.length-1:t-1])&&(i._segment2=n[t]||n[0]),(i=r[e])&&(i._segment1=n[e])},_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(z.readAll(arguments)):this._add([z.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(z.readAll(arguments,1),t):this._add([z.read(arguments,1)],t)[0]},addSegment:function(){return this._add([z.read(arguments)])[0]},insertSegment:function(t){return this._add([z.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(z.readAll(t))},insertSegments:function(t,e){return this._add(z.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(5),o},clear:"#removeSegments",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 me(t){t||this._selectSegments(!1),me.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 N(this),i=0,n=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-n:n)/2,s=[];r>=i;)s.push(new z(e.evaluate(i,0))),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 j(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null!==e){if(1===arguments.length){var i=t;"number"==typeof i&&(i=this.getLocationAt(i)),t=i.index,e=i.parameter}var n=1e-5;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):t>0&&(s=this._clone((new T).insertAbove(this,!0))),s._add(a,0),this.addSegment(a[0]),s}return null}},isClockwise:function(){return this._clockwise!==t?this._clockwise:T.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)},join:function(t){if(t){var e=t._segments,i=this.getLastSegment(),n=t.getLastSegment();i._point.equals(n._point)&&t.reverse();var r,s=t.getFirstSegment();return i._point.equals(s._point)?(i.setHandleOut(s._handleOut),this._add(e.slice(1))):(r=this.getFirstSegment(),r._point.equals(s._point)&&t.reverse(),n=t.getLastSegment(),r._point.equals(n._point)?(r.setHandleIn(n._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())),t.closed&&this._add([e[0]]),t.remove(),r=this.getFirstSegment(),i=this.getLastSegment(),i._point.equals(r._point)&&(r.setHandleIn(i._handleIn),i.remove(),this.setClosed(!0)),this._changed(5),!0}return!1},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},_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];return n+s.getLength(0,t.getParameter())}return null},getLocationOf:function(t){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},getLocationAt:function(t,e){var i=this.getCurves(),n=0;if(e){var r=~~t;return i[r].getLocationAt(t-r,!0)}for(var s=0,a=i.length;a>s;s++){var o=n,h=i[s];if(n+=h.getLength(),n>t)return h.getLocationAt(t-o)}return t<=this.getLength()?new A(i[i.length-1],1):null},getPointAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getPoint()},getTangentAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getTangent()},getNormalAt:function(t,e){var i=this.getLocationAt(t,e);return i&&i.getNormal()},getNearestLocation:function(t){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._distance0&&e._index0?(o=g.getStrokeJoin(),u=g.getStrokeCap(),l=b*g.getMiterLimit(),y=m.add(new h(b,b))):o=u="round"),!e.ends||e.segments||p){if(e.segments||e.handles)for(var C=0,S=v.length;S>C;C++)if(_=r(v[C]))return _}else if(_=r(v[0],!0)||r(v[v.length-1],!0))return _;if(null!=b){if(d=this.getNearestLocation(t)){var z=d.getParameter();0===z||1===z?a(d.getSegment())||(d=null):i(d.getPoint(),y)||(d=null)}if(!d&&"miter"===o)for(var C=0,S=v.length;S>C;C++){var M=v[C];if(t.getDistance(M._point)<=l&&a(M)){d=M.getLocation();break}}}return!d&&x&&this._contains(t)||d&&!w?new P("fill",this):d?new P("stroke",this,{location:d,point:d.getPoint()}):null}},new function(){function t(t,e,i,n){function r(e){var i=a[e],n=a[e+1];(c!=i||d!=n)&&(t.beginPath(),t.moveTo(c,d),t.lineTo(i,n),t.stroke(),t.beginPath(),t.arc(i,n,s,0,2*Math.PI,!0),t.fill())}for(var s=n/2,a=Array(6),o=0,h=e.length;h>o;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){var n=d[e];if(i)n._transformCoordinates(i,f,!1),r=f[0],s=f[1];else{var _=n._point;r=_._x,s=_._y}if(g)t.moveTo(r,s),g=!1;else{if(i)h=f[2],u=f[3];else{var v=n._handleIn;h=r+v._x,u=s+v._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 v=n._handleOut;l=a+v._x,c=o+v._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(v);e._closed&&_>0&&n(0)}return{_draw:function(t,i){function n(t){return u[(t%l+l)%l]}var r=i.clip,s=this._compound=i.compound;s||t.beginPath();var a=this.getStyle(),o=a.hasFill(),h=a.hasStroke(),u=a.getDashArray(),l=!paper.support.nativeDash&&h&&u&&u.length;if(this._currentPath?t.currentPath=this._currentPath:(o||h&&!l||s||r)&&(e(t,this),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!r&&!s&&(o||h)&&(this._setStyles(t),o&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),h)){if(l){t.beginPath();var c,d=new N(this),_=d.length,f=-a.getDashOffset(),g=0;for(f%=_;f>0;)f-=n(g--)+n(g--);for(;_>f;)c=f+n(g++),(f>0||c>0)&&d.drawPart(t,Math.max(f,0),Math.max(c,0)),f=c+n(g++)}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 z(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new z(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 z(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,l=h.read(arguments),d=e.peek(arguments),_=e.pick(d,!0);if("boolean"==typeof _)var f=u.add(l).divide(2),i=f.add(f.subtract(u).rotate(_?-90:90));else if(e.remain(arguments)<=2)i=l,l=h.read(arguments);else{var p=c.read(arguments);if(p.isZero())return this.lineTo(l);var m=e.read(arguments),_=!!e.read(arguments),y=!!e.read(arguments),f=u.add(l).divide(2),w=u.subtract(f).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=1e-11,P=C(p.width),M=C(p.height),I=P*P,k=M*M,A=x*x,O=b*b,T=Math.sqrt(A/I+O/k);if(T>1&&(P*=T,M*=T,I=P*P,k=M*M),T=(I*k-I*O-k*A)/(I*O+k*A),C(T)T)throw Error("Cannot create an arc with the given arguments");n=new h(P*b/M,-M*x/P).multiply((y===_?-1:1)*Math.sqrt(T)).rotate(m).add(f),a=(new g).translate(n).rotate(m).scale(P,M),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(l)),!_&&r>0?r-=360:_&&0>r&&(r+=360)}if(i){var L=new v(u.add(i).divide(2),i.subtract(u).rotate(90),!0),N=new v(i.add(l).divide(2),l.subtract(i).rotate(90),!0),j=new v(u,l),D=j.getSide(i);if(n=L.intersect(N,!0),!n){if(!D)return this.lineTo(l);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(n),r=s.getDirectedAngle(l.subtract(n));var B=j.getSide(n);0===B?r=D*Math.abs(r):D===B&&(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=l,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 z(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(){var t=this.getFirstSegment(),e=this.getLastSegment();t!==e&&t._point.equals(e._point)&&(t.setHandleIn(e._handleIn),e.remove()),this.setClosed(!0)}}},{_getBounds:function(t,e){return T[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,i=0,n=t.length;n>i;i++)for(var r=I.getValues(t[i],t[n>i+1?i+1:0]),s=2;8>s;s+=2)e+=(r[s-2]-r[s])*(r[s+1]+r[s-1]);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++)I._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 _;for(var o=Array(6),h=a._transformCoordinates(n,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),d=1,f=t.length;f>d;d++)s(t[d]);return e&&s(a),new _(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,i,n){function r(t){d=d.include(n?n._transformPoint(t,t):t)}function s(t){d=d.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.isColinear(n)?s(t):T._addBevelJoin(t,e,u,v,r)}function o(t,e){"round"===e?s(t):T._addSquareCap(t,e,u,r)}if(!i.hasStroke())return T.getBounds(t,e,i,n);for(var h=t.length-(e?0:1),u=i.getStrokeWidth()/2,l=T._getPenPadding(u,n),d=T.getBounds(t,e,i,n,l),f=i.getStrokeJoin(),g=i.getStrokeCap(),v=u*i.getMiterLimit(),p=new _(new c(l).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)),d},_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 v(u.add(l),new h(-l.y,l.x),!0).intersect(new v(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().normalize(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,d=t.length;d>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 _(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)))),T.getHandleBounds(t,e,i,n,T._getPenPadding(r,n),T._getPenPadding(s,n))}}});T.inject({statics:new function(){function t(t,i,n){var r=e.getNamed(n),s=new T(r&&r.insert===!1&&y.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 z(o._point.multiply(i).add(e),o._handleIn.multiply(i),o._handleOut.multiply(i))}return t(s,!0,n)}var n=.5522847498307936,r=[new z([-1,0],[0,n],[0,-n]),new z([0,-1],[-n,0],[n,0]),new z([1,0],[0,-n],[0,n]),new z([0,1],[n,0],[-n,0])];return{Line:function(){return t([new z(h.readNamed(arguments,"from")),new z(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"radius");return i(t,new c(n),arguments)},Rectangle:function(){var e,i=_.readNamed(arguments,"rectangle"),r=c.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 z(s),new z(a),new z(o),new z(h)];else{r=c.min(r,i.getSize(!0).divide(2));var u=r.width,l=r.height,d=u*n,f=l*n;e=[new z(s.add(u,0),null,[-d,0]),new z(s.subtract(0,l),[0,f]),new z(a.add(0,l),null,[0,-f]),new z(a.add(u,0),[-d,0],null),new z(o.subtract(u,0),null,[d,0]),new z(o.add(0,l),[0,-f],null),new z(h.subtract(0,l),null,[0,f]),new z(h.subtract(u,0),[d,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=b._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 T(r&&r.insert===!1&&y.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 z(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 z(i.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var L=O.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))},_changed:function ye(e){ye.base.call(this,e),6&e&&(this._currentPath=t)},insertChildren:function we(e,i,n){i=we.base.call(this,e,i,n,"path");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()},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},getPathData:function(t){for(var e=this._children,i=[],n=0,r=e.length;r>n;n++)i.push(e[n].getPathData(t));return i.join(" ")},_getChildHitTestOptions:function(t){return"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e){var i=this._children;if(0!==i.length){if(this._currentPath)t.currentPath=this._currentPath;else{t.beginPath(),e=e.extend({compound:!0});for(var n=0,r=i.length;r>n;n++)i[n].draw(t,e);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var s=this._style;s.hasFill()&&(t.fill(s.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s.hasStroke()&&t.stroke()}}}},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 T;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(){t(this,!0).closePath()}};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});O.inject(new function(){function t(t){if(t=t.clone(!1).reduce().transform(null,!0),t instanceof L){var e,i=t.removeChildren(),n=i.length,r=Array(n),s=Array(n);i.sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()}),t.addChildren(i),e=i[0].isClockwise();for(var a=0;n>a;a++)r[a]=i[a].getBounds(),s[a]=0;for(var a=0;n>a;a++){for(var o=1;n>o;o++)a!==o&&r[a].intersects(r[o])&&s[o]++;a>0&&0===s[a]%2&&i[a].setClockwise(e)}}return t}function e(e,s,a,o){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=t(e);_path2=s&&e!==s&&t(s),u.isClockwise()||u.reverse(),!_path2||o^_path2.isClockwise()||_path2.reverse(),i(u.getIntersections(_path2,!0));var l=[],c=[],d=[],_=[],f=[];h(u._children||[u]),_path2&&h(_path2._children||[_path2]),_.sort(function(t,e){var i=t._intersection,n=e._intersection;return!i&&!n||i&&n?0:i?-1:1});for(var g=0,v=_.length;v>g;g++){var p=_[g];if(null==p._winding){l.length=c.length=d.length=0;var m=0,y=p;do l.push(p),d.push(m+=p.getCurve().getLength()),p=p.getNext();while(p&&!p._intersection&&p!==y);for(var w=0;3>w;w++){var x=m*Math.random(),b=d.length;k=0;do if(d[k]>=x){k>0&&(x-=d[k-1]);break}while(++k=0;w--)l[w]._winding=M}}var I=new L;return I.addChildren(r(_,a),!0),u.remove(),_path2&&_path2.remove(),I.reduce()}function i(t){function e(){for(var t=0,e=i.length;e>t;t++){var n=i[t];n._handleOut.set(0,0),n._handleIn.set(0,0)}}for(var i,n,r,s=1e-5,a=t.length-1;a>=0;a--){var o=t[a],h=o._parameter;r&&r._curve===o._curve&&r._parameter>0?h/=r._parameter:(i&&e(),n=o._curve,i=n.isLinear()&&[]);var u,l;(u=n.divide(h,!0,!0))?(l=u._segment1,n=u.getPrevious()):l=s>h?n._segment1:h>1-s?n._segment2:n.getPartLength(0,h)m;m++){var w=e[m].values;if(I.solveCubic(w,0,a,c,0,1)>0)for(var x=c.length-1;x>=0;x--){var b=I.evaluate(w,c[x],0).y;v>b&&b>f?f=b:b>p&&g>b&&(g=b)}}f=(f+o)/2,g=(g+o)/2,f>-1/0&&(u=n(new h(a,f),e)),1/0>g&&(l=n(new h(a,g),e))}else for(var C=a-s,S=a+s,m=0,y=e.length;y>m;m++){var P=e[m],w=P.values,z=P.winding,M=P.next;if(z&&(1===z&&o>=w[1]&&o<=w[7]||o>=w[7]&&o<=w[1])&&1===I.solveCubic(w,1,o,c,0,M.winding||M.values[1]!==o?_:1)){var k=c[0],A=I.evaluate(w,k,0).x,O=I.evaluate(w,k,1).y;d(O)k&&O*I.evaluate(P.previous.values,k,1).y<0?r&&A>=C&&S>=A&&(++u,++l):C>=A?u+=z:A>=S&&(l+=z)}}return Math.max(d(u),d(l))}function r(t,e,i){e=e||function(){return!0};for(var n,r,s=[],a=.001,o=.999,h=0,u=t.length;u>h;h++)if(n=r=t[h],!n._visited&&e(n._winding)){var l=new T(y.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){var m=n.getCurve();f>0&&(m=m.getPrevious());var w=m.getTangentAt(1>f?a:o,!0),x=g.getCurve(),b=x.getPrevious(),C=b.getTangentAt(o,!0),S=x.getTangentAt(a,!0),P=w.cross(C),M=w.cross(S);if(i)n._visited=g._visited,n=g,f=1;else if(0!==P*M){var I=M>P?b:x,k=e(I._segment1._winding)?I:M>P?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 z(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.isPolygon()?2:0:1)&&s.push(l)}return s}return{_getWinding:function(t,e,i){return n(t,this._getMonoCurves(),e,i)},unite:function(t){return e(this,t,function(t){return 1===t||0===t},!1)},intersect:function(t){return e(this,t,function(t){return 2===t},!1)},subtract:function(t){return e(this,t,function(t){return 1===t},!0)},exclude:function(t){return new w([this.subtract(t),t.subtract(this)])},divide:function(t){return new w([this.subtract(t),this.intersect(t)])}}}),T.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!==I.getLength(e)){var i=e[1],n=e[3],r=e[5],s=e[7];if(I.isLinear(e))t(e);else{var a=3*(n-r)-i+s,h=2*(i+r)-4*n,u=n-i,l=1e-5,c=[],d=o.solveQuadratic(a,h,u,c,l,1-l);if(0===d)t(e);else{c.sort();var _=c[0],f=I.subdivide(e,_);t(f[0]),d>1&&(_=(c[1]-_)/(1-_),f=I.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,a=0,h=r.length;h>a;a++)e(r[a].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])}var g=n[0],v=n[n.length-1];g.previous=v,v.next=g}return n}}),L.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}});var N=e.extend({initialize:function(t){function e(t,e){var i=I.getValues(t,e);s.curves.push(i),s._computeParts(i,t._index,0,1)}this.curves=[],this.parts=[],this.length=0,this.index=0;for(var i,n=t._segments,r=n[0],s=this,a=1,o=n.length;o>a;a++)i=n[a],e(r,i),r=i;t._closed&&e(i,n[0])},_computeParts:function(t,e,i,n){if(n-i>1/32&&!I.isFlatEnough(t,.25)){var r=I.subdivide(t),s=(i+n)/2;this._computeParts(r[0],e,i,s),this._computeParts(r[1],e,s,n)}else{var a=t[6]-t[0],o=t[7]-t[1],h=Math.sqrt(a*a+o*o);h>1e-5&&(this.length+=h,this.parts.push({offset:this.length,value:n,index:e}))}},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}},evaluate:function(t,e){var i=this.getParameterAt(t);return I.evaluate(this.curves[i.index],i.value,e)},drawPart:function(t,e,i){e=this.getParameterAt(e),i=this.getParameterAt(i);for(var n=e.index;n<=i.index;n++){var r=I.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))}}}),j=e.extend({initialize:function(t,e){this.points=[];for(var i,n=t._segments,r=0,s=n.length;s>r;r++){var a=n[r].point.clone();i&&i.equals(a)||(this.points.push(a),i=a)}this.error=e},fit:function(){var t=this.points,e=t.length;return this.segments=e>0?[new z(t[0])]:[],e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.segments},fitCubic:function(e,i,n,r){if(1==i-e){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;4>=c;c++){var d=this.generateBezier(e,i,u,n,r),_=this.findMaxError(e,i,d,u);if(_.error=l)break;this.reparameterize(e,i,u,d),l=_.error}var f=this.points[h-1].subtract(this.points[h]),g=this.points[h].subtract(this.points[h+1]),v=f.add(g).divide(2).normalize();this.fitCubic(e,h,n,v),this.fitCubic(h,i,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 z(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,i,n,r){for(var s=1e-11,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],z=u[0]*h[1][1]-u[1]*h[0][1];b=z/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 k=o.getDistance(a);return s*=k,(s>b||s>C)&&(b=C=k/3),[a,a.add(n.normalize(b)),o.add(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])},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-5?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}}}),D=y.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 xe(t){return t.setContent(this._content),xe.base.call(this,t)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(69)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),B=D.extend({_class:"PointText",initialize:function(){D.apply(this,arguments) +},clone:function(t){return this._clone(new B(y.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)}}}},new function(){var t=null;return{_getBounds:function(e,i){t||(t=K.getContext(1,1));var n=this._style,r=this._lines,s=r.length,a=n.getJustification(),o=n.getLeading(),h=0;t.font=n.getFontStyle();for(var u=0,l=0;s>l;l++)u=Math.max(u,t.measureText(r[l]).width);"left"!==a&&(h-=u/("center"===a?2:1));var c=new _(h,s?-.75*o:0,u,s*o);return i?i._transformBounds(c,c):c}}}),E=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 a=n[r+1];e[r]=parseInt(1==a.length?a+a:a,16)/255}}else if(n=t.match(/^rgba?\((.*)\)$/)){e=n[1].split(",");for(var r=0,o=e.length;o>r;r++){var a=+e[r];e[r]=3>r?a/255:a}}else{var h=s[t];if(!h){i||(i=K.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=s[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={},s={},o=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],u={"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=o[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+6*(r-s)*(2/3-h):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=R.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),t=u.call(this,t),null!=t&&(this._components[s]=t,this._changed())}},this)},{_class:"Color",_readIndex:!0,initialize:function l(e){var i,s,a,o,h=Array.prototype.slice,u=arguments,c=0;Array.isArray(e)&&(u=e,e=u[0]);var d=null!=e&&typeof e;if("string"===d&&e in n&&(i=e,e=u[1],Array.isArray(e)?(s=e,a=u[2]):(this.__read&&(c=1),u=h.call(u,1),d=typeof e)),!s){if(o="number"===d?u:"object"===d&&null!=e.length?e:null){i||(i=o.length>=3?"rgb":"gray");var _=n[i].length;a=o[_],this.__read&&(c+=o===arguments?_+(null!=a?1:0):1),o.length>_&&(o=h.call(o,0,_))}else if("string"===d)i="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===d)if(e.constructor===l){if(i=e._type,s=e._components.slice(),a=e._alpha,"gradient"===i)for(var f=1,g=s.length;g>f;f++){var v=s[f];v&&(s[f]=v.clone())}}else if(e.constructor===R)i="gradient",o=u;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 p=n[i];y=r[i],this._components=s=[];for(var f=0,g=p.length;g>f;f++){var m=e[p[f]];null==m&&0===f&&"gradient"===i&&"stops"in e&&(m={stops:e.stops,radial:e.radial}),m=y[f].call(this,m),null!=m&&(s[f]=m)}a=e.alpha}this.__read&&i&&(c=1)}if(this._type=i||"rgb","gradient"===i&&(this._id=l._id=(l._id||0)+1),!s){this._components=s=[];for(var y=r[this._type],f=0,g=y.length;g>f;f++){var m=y[f].call(this,o&&o[f]);null!=m&&(s[f]=m)}}this._components=s,this._properties=n[this._type],this._alpha=a,this.__read&&(this.__read=c)},_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(17)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=u[this._type+"-"+t])?e.apply(this,this._components):u["rgb-"+t].apply(this,u[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new E(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)?E.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=a.instance,r=0,s=t.length;s>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 E(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=E.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 E(i,r,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(E._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 E(i,n):new E(t)};if(3==i.length){var r=i.toUpperCase();E[r]=this[r+"Color"]=n}},e.exports);var R=e.extend({_class:"Gradient",initialize:function be(t,e){this._id=be._id=(be._id||0)+1,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 R(t)},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=F.readAll(e,0,!1,!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}}),F=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 F(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(17)},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=E.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}}),q=e.extend(new function(){var i={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",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:25,strokeCap:25,strokeJoin:25,miterLimit:25,fontFamily:5,fontWeight:5,fontSize:5,font:5,leading:5,justification:5},r={},s={_defaults:i,_textDefaults:new e(i,{fillColor:new E})};return e.each(i,function(i,a){var o=/Color$/.test(a),h=e.capitalize(a),u=n[a],l="set"+h,c="get"+h;s[l]=function(e){var i=this._item&&this._item._children;if(i&&i.length>0&&"compound-path"!==this._item._type)for(var n=0,r=i.length;r>n;n++)i[n]._style[l](e);else{var s=this._values[a];s!=e&&(o&&(s&&(s._owner=t),e&&e.constructor===E&&(e._owner&&(e=e.clone()),e._owner=this._item)),this._values[a]=e,this._item&&this._item._changed(u||17))}},s[c]=function(i){var n,r=this._item&&this._item._children;if(!r||0===r.length||i||this._item instanceof L){var n=this._values[a];return n===t?(n=this._defaults[a],n&&n.clone&&(n=n.clone()),this._values[a]=n):!o||n&&n.constructor===E||(this._values[a]=n=E.read([n],0,{readNull:!0,clone:!0}),n&&(n._owner=this._item)),n}for(var s=0,h=r.length;h>s;s++){var u=r[s]._style[c]();if(0===s)n=u;else if(!e.equals(n,u))return t}return n},r[c]=function(){return this._style[c]()},r[l]=function(t){this._style[l](t)}}),y.inject(r),s},{_class:"Style",initialize:function(t,e){this._values={},this._item=e,e instanceof D&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof q,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},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function Ce(){var t=Ce.base.call(this);return null!=t?t:1.2*this.getFontSize()}}),V=new function(){function i(t,n){for(var r=[],s=0,a=t&&t.length;a>s;){var o=t[s++];if("string"==typeof o)o=document.createElement(o);else if(!o||!o.nodeType)continue;e.isPlainObject(t[s])&&V.set(o,t[s++]),Array.isArray(t[s])&&i(t[s++],o),n&&n.appendChild(o),r.push(o)}return r}function n(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}}}var r=/^(checked|value|selected|disabled)$/i,s={text:"textContent",html:"innerHTML"},a={lineHeight:1,zoom:1,zIndex:1,opacity:1};return{create:function(t,e){var n=Array.isArray(t),r=i(n?t:arguments,n?e:null);return 1==r.length?r[0]:r},find:function(t,e){return(e||document).querySelector(t)},findAll:function(t,e){return(e||document).querySelectorAll(t)},get:function(t,e){return t?r.test(e)?"value"===e||"string"!=typeof t[e]?t[e]:!0:e in s?t[s[e]]:t.getAttribute(e):null},set:function(e,i,n){if("string"!=typeof i)for(var a in i)i.hasOwnProperty(a)&&this.set(e,a,i[a]);else{if(!e||n===t)return e;r.test(i)?e[i]=n:i in s?e[s[i]]=n:"style"===i?this.setStyle(e,n):"events"===i?Z.add(e,n):e.setAttribute(i,n)}return e},getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,i=e&&e.defaultView;return i&&i.getComputedStyle(t,"")},getStyle:function(t,e){return t&&t.style[e]||this.getStyles(t)[e]||null},setStyle:function(t,e,i){if("string"!=typeof e)for(var n in e)e.hasOwnProperty(n)&&this.setStyle(t,n,e[n]);else!/^-?[\d\.]+$/.test(i)||e in a||(i+="px"),t.style[e]=i;return t},hasClass:function(t,e){return RegExp("\\s*"+e+"\\s*").test(t.className)},addClass:function(t,e){t.className=(t.className+" "+e).trim()},removeClass:function(t,e){t.className=t.className.replace(RegExp("\\s*"+e+"\\s*")," ").trim()},remove:function(t){t.parentNode&&t.parentNode.removeChild(t)},removeChildren:function(t){for(;t.firstChild;)t.removeChild(t.firstChild)},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 _(o,h,i.width,i.height)},getViewportBounds:function(t){var e=t.ownerDocument,i=e.defaultView,n=e.documentElement;return new _(0,0,i.innerWidth||n.clientWidth,i.innerHeight||n.clientHeight)},getOffset:function(t,e){return this.getBounds(t,e).getPoint()},getSize:function(t){return this.getBounds(t,!0).getSize()},isInvisible:function(t){return this.getSize(t).equals(new c(0,0))},isInView:function(t){return!this.isInvisible(t)&&this.getViewportBounds(t).intersects(this.getBounds(t,!0))},getPrefixed:function(t,e){return n(t,e)},setPrefixed:function(t,e,i){if("object"==typeof e)for(var r in e)n(t,r,!0,e[r]);else n(t,e,!0,i)}}},Z={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 Z.getPoint(t).subtract(V.getOffset(e||Z.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};Z.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"==r.getAttribute(u,"keepalive")||a)&&V.isInView(u))&&(s.splice(e,1),h())}i&&(s.length?i(t):n=!1)}var e,i=V.getPrefixed(window,"requestAnimationFrame"),n=!1,s=[],a=!0;return Z.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(r,a){s.push([r,a]),i?n||(i(t),n=!0):e||(e=setInterval(t,1e3/60))}};var H=e.extend(n,{_class:"View",initialize:function Se(t){this._scope=paper,this._project=paper.project,this._element=t;var e;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=t.getAttribute("id"),null==this._id&&t.setAttribute("id",this._id="view-"+Se._id++),Z.add(t,this._viewEvents);var i="none";if(V.setPrefixed(t.style,{userSelect:i,touchAction:i,touchCallout:i,contentZooming:i,userDrag:i,tapHighlightColor:"rgba(0,0,0,0)"}),r.hasAttribute(t,"resize")){var n=V.getOffset(t,!0),s=this;e=V.getViewportBounds(t).getSize().subtract(n),this._windowEvents={resize:function(){V.isInvisible(t)||(n=V.getOffset(t,!0)),s.setViewSize(V.getViewportBounds(t).getSize().subtract(n))}},Z.add(window,this._windowEvents)}else e=V.getSize(t),(e.isNaN()||e.isZero())&&(e=new c(parseInt(t.getAttribute("width"),10),parseInt(t.getAttribute("height"),10)));if(this._setViewSize(e),r.hasAttribute(t,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var a=this._stats.domElement,o=a.style,n=V.getOffset(t);o.position="absolute",o.left=n.x+"px",o.top=n.y+"px",document.body.appendChild(a)}Se._views.push(this),Se._viewsById[this._id]=this,this._viewSize=e,(this._matrix=new g)._owner=this,this._zoom=1,Se._focused||(Se._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(H._focused===this&&(H._focused=null),H._views.splice(H._views.indexOf(this),1),delete H._viewsById[this._id],this._project.view==this&&(this._project.view=null),Z.remove(this._element,this._viewEvents),Z.remove(window,this._windowEvents),this._element=this._project=null,this.detach("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;Z.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.fire("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.attach("frame",this._handleFrameItems)):(delete i[t._id],0===--this._frameItemCount&&this.detach("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var i in this._frameItems){var n=this._frameItems[i];n.item.fire("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 d(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=c.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.fire("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 _(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(t){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 V.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){return"string"==typeof t&&(t=document.getElementById(t)),new W(t)}}},new function(){function t(t){var e=Z.getTarget(t);return e.getAttribute&&H._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(Z.getOffset(e,t._element))}function i(){if(!H._focused||!H._focused.isVisible())for(var t=0,e=H._views.length;e>t;t++){var i=H._views[t];if(i&&i.isVisible()){H._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=H._focused,r=Z.getRelatedTarget(t);!i||r&&"HTML"!==r.nodeName||n(i,e(i,t),t)},scroll:i};return d[o]=function(i){var n=H._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=H._focused;if(!l){var u=t(o);u?(h!==u&&n(h,e(h,o),o),s=h,h=H._focused=a=u):a&&a===h&&(h=H._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=H._focused;if(i&&l){var n=e(i,t);curPoint=null,l=!1,i._handleEvent("mouseup",n,t),r&&r._handleEvent("mouseup",n,t),i.update()}},Z.add(document,_),Z.add(window,{load:i}),{_viewEvents:d,_handleEvent:function(){},statics:{updateFocus:i}}}),W=H.extend({_class:"CanvasView",initialize:function(t){if(!(t instanceof HTMLCanvasElement)){var e=c.read(arguments);if(e.isZero())throw Error("Cannot create CanvasView with the provided argument: "+t);t=K.getCanvas(e)}if(this._context=t.getContext("2d"),this._eventCounters={},this._pixelRatio=1,"off"!==r.getAttribute(t,"hidpi")){var i=window.devicePixelRatio||1,n=V.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=i/n}H.call(this,t)},_setViewSize:function(t){var e=t.width,i=t.height,n=this._pixelRatio,r=this._element,s=r.style;r.width=e*n,r.height=i*n,1!==n&&(s.width=e+"px",s.height=i+"px",this._context.scale(n,n))},update:function(){if(!this._project._needsUpdate)return!1;var t=this._context,e=this._viewSize;return t.clearRect(0,0,e.width+1,e.height+1),this._project.draw(t,this._matrix,this._pixelRatio),this._project._needsUpdate=!1,!0}},new function(){function e(e,i,n,r,s,a){function o(e){return e.responds(i)&&(h||(h=new X(i,n,r,s,a?r.subtract(a):null)),e.fire(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:this._scope.settings.hitTolerance,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}}}}),G=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 U.modifiers}}),J=G.extend({_class:"KeyEvent",initialize:function(t,e,i,n){G.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()+" }"}}),U=new function(){function t(t,i,r,h){var u,l=r?String.fromCharCode(r):"",c=n[i],d=c||l.toLowerCase(),_=t?"keydown":"keyup",f=H._focused,g=f&&f.isVisible()&&f._scope,v=g&&g.tool;o[d]=t,c&&(u=e.camelize(c))in s&&(s[u]=t),t?a[i]=r:delete a[i],v&&v.responds(_)&&(paper=g,v.fire(_,new J(t,d,l,h)),f&&f.update())}var i,n={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"},r={9:!0,13:!0,32:!0},s=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),a={},o={};return Z.add(document,{keydown:function(e){var s=e.which||e.keyCode;s in n?t(!0,s,s in r?s:0,e):i=s},keypress:function(e){null!=i&&(t(!0,i,e.which||e.keyCode,e),i=null)},keyup:function(e){var i=e.which||e.keyCode;i in a&&t(!1,i,a[i],e)}}),Z.add(window,{blur:function(e){for(var i in a)t(!1,i,a[i],e)}}),{modifiers:s,isDown:function(t){return!!o[t]}}},X=G.extend({_class:"MouseEvent",initialize:function(t,e,i,n,r){G.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()+" }"}}),Y=G.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|compound-path)$/.test(i._type);)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()+" }"}});s.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){s.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!=this._minDistance&&null!=this._maxDistance&&this._minDistance>this._maxDistance&&(this._maxDistance=this._minDistance)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=this._maxDistance&&this._maxDistanceu)return!1;var l=null!=n?n:0;if(0!=l)if(u>l)e=this._point.add(h.normalize(l));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.fire(t,new Y(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}});var $={request:function(t,e,i){var n=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return n.open(t.toUpperCase(),e,!0),"overrideMimeType"in n&&n.overrideMimeType("text/plain"),n.onreadystatechange=function(){if(4===n.readyState){var t=n.status;if(0!==t&&200!==t)throw Error("Could not load "+e+" (Error "+t+")");i.call(n,n.responseText)}},n.send(null)}},K={canvases:[],getCanvas:function(t,e,i){var n,r=!0;"object"==typeof t&&(i=e,e=t.height,t=t.width),i?1!==i&&(t*=i,e*=i):i=1,n=this.canvases.length?this.canvases.pop():document.createElement("canvas");var s=n.getContext("2d");return n.width===t&&n.height===e?r&&s.clearRect(0,0,t+1,e+1):(n.width=t,n.height=e),s.save(),1!==i&&s.scale(i,i),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)}},Q=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=K.getContext(1,1);e.each(y,function(t,e){x.save();var i="darken"===e,n=!1;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)),w[e]=n,x.restore()}),K.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 z=h*n/255,M=1-z;b[S]=z*_+M*u,b[S+1]=z*f+M*l,b[S+2]=z*g+M*c,b[S+3]=h*n+M*d}i.putImageData(x,r.x,r.y)}}},te=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],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},{}),get:"get"+n,set:"set"+n}},{}),ee={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=ee[i];"number"==typeof n&&(n=S.number(n)),r?t.setAttributeNS(r,i,n):t.setAttribute(i,n)}return t}function n(e,i){return t(document.createElementNS("http://www.w3.org/2000/svg",e),i)}function r(t,e,i){var n=t._matrix,r=n.getTranslation(),s={};if(e){n=n.shiftless();var a=n._inverseTransform(r);s[i?"cx":"x"]=a.x,s[i?"cy":"y"]=a.y,r=null}if(!n.isIdentity()){var h=n.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;r&&!r.isZero()&&u.push("translate("+S.point(r)+")"),l&&u.push("rotate("+S.number(l)+")"),o.isZero(c.x-1)&&o.isZero(c.y-1)||u.push("scale("+S.point(c)+")"),s.transform=u.join(" ")}else s.transform="matrix("+n.getValues().join(",")+")"}return s}function s(e,i){for(var s=r(e),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=b(l,i);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),w(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function h(t){var e=r(t,!0),i=t.getSize();return e.x-=i.width/2,e.y-=i.height/2,e.width=i.width,e.height=i.height,e.href=t.toDataURL(),n("image",e)}function u(t,e){if(e.matchShapes){var r=t.toShape(!1);if(r)return c(r,e)}var s,a,o=t._segments;if(0===o.length)return null;if(t.isPolygon())if(o.length>=3){s=t._closed?"polygon":"polyline";var h=[];for(i=0,l=o.length;l>i;i++)h.push(S.point(o[i]._point));a={points:h.join(" ")}}else{s="line";var u=o[0]._point,d=o[o.length-1]._point;a={x1:u.x,y1:u.y,x2:d.x,y2:d.y}}else{s="path";var _=t.getPathData();a=_&&{d:_}}return n(s,a)}function c(t){var e=t._shape,i=t._radius,s=r(t,!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,i.isZero()&&(i=null)}return i&&("circle"===e?s.r=i:(s.rx=i.width,s.ry=i.height)),n(e,s)}function d(t){var e=r(t,!0),i=t.getPathData();return i&&(e.d=i),n("path",e)}function _(t,e){var i=r(t,!0),s=t.getSymbol(),a=m(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:S.rectangle(h)}),a.appendChild(b(o,e)),w(s,a,"symbol")),i.href="#"+a.id,i.x+=h.x,i.y+=h.y,i.width=S.number(h.width),i.height=S.number(h.height),n("use",i)}function f(t){var e=m(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))}w(t,e,"color")}return"url(#"+e.id+")"}function g(t){var e=n("text",r(t,!0));return e.textContent=t._content,e}function v(i,n){var r={},s=i.getParent();return null!=i._name&&(r.id=i._name),e.each(te,function(t){var n=t.get,a=t.type,o=i[n]();if(!s||!e.equals(s[n](),o)){if("color"===a&&null!=o){var h=o.getAlpha();1>h&&(r[t.attribute+"-opacity"]=h)}r[t.attribute]=null==o?"none":"number"===a?S.number(o):"color"===a?o.gradient?f(o,i):o.toCSS(!0):"array"===a?o.join(","):"lookup"===a?t.toSVG[o]:o}}),1===r.opacity&&delete r.opacity,null==i._visibility||i._visibility||(r.visibility="hidden"),t(n,r)}function m(t,e){return P||(P={ids:{},svgs:{}}),t&&P.svgs[e+"-"+t._id]}function w(t,e,i){P||m();var n=P.ids[i]=(P.ids[i]||0)+1;e.id=i+"-"+n,P.svgs[i+"-"+t._id]=e}function x(t,e){var i=t,r=null;if(P){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in P.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(P.svgs[s]);P=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function b(t,e){var i=z[t._type],n=i&&i(t,e);return n&&t._data&&n.setAttribute("data-paper-data",JSON.stringify(t._data)),n&&v(t,n)}function C(t){return t||(t={}),S=new a(t.precision),t}var S,P,z={group:s,layer:s,raster:h,path:u,shape:c,"compound-path":d,"placed-symbol":_,"point-text":g};y.inject({exportSVG:function(t){return t=C(t),x(b(this,t),t)}}),p.inject({exportSVG:function(t){t=C(t);for(var e=this.layers,i=this.view.getSize(),r=n("svg",{x:0,y:0,width:i.width,height:i.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),s=0,a=e.length;a>s;s++)r.appendChild(b(e[s],t));return x(r,t)}})},new function(){function i(t,e,i,n){var r=ee[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 c(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 w,o=a._project,h=o._currentStyle,u=[];s||(a._applyMatrix=!1,a=x(a,t,i),o._currentStyle=a._style.clone());for(var l=0,c=r.length;c>l;l++){var d,_=r[l];1!==_.nodeType||!(d=P(_,!1,n))||d instanceof m||u.push(d)}return a.addChildren(u),s&&(a=x(a.reduce(),t,i)),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 T(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 L(i):new T(i)}function l(t,e){for(var r=t.childNodes,s=[],a=0,o=r.length;o>a;a++){var h=r[a];1===h.nodeType&&s.push(x(new F,h))}var u,l,c,d="radialgradient"===e,_=new R(s,d);return d?(u=n(t,"cx","cy"),l=u.add(i(t,"r"),0),c=n(t,"fx","fy",!0)):(u=n(t,"x1","y1"),l=n(t,"x2","y2")),x(new E(_,u,l,c),t),null}function d(t,e,i,n){for(var r=(n.getAttribute(i)||"").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("("),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 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 f(t,e,i){var n=t["fill-opacity"===i?"getFillColor":"getStrokeColor"]();n&&n.setAlpha(parseFloat(e))}function v(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 x(i,n,r){var s={node:V.getStyles(n)||{},parent:!r&&V.getStyles(n.parentNode)||{}};return e.each(M,function(r,a){var o=v(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,e,i){function n(t){paper=s;var n=P(t,e,i),r=i.onLoad,a=s.project&&s.project.view;r&&r.call(this,n),a.update()}if(!t)return null;i?"function"==typeof i&&(i={onLoad:i}):i={};var r=t,s=paper;if(e)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:l,radialgradient:l,image:function(t){var e=new C(i(t,"href",!0));return e.attach("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 m(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 m?r.place(s):r.clone().translate(s):null},circle:function(t){return new b.Circle(n(t,"cx","cy"),i(t,"r"))},ellipse:function(t){return new b.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 b.Rectangle(new _(e,i),s)},line:function(t){return new T.Line(n(t,"x1","y1"),n(t,"x2","y2"))},text:function(t){var e=new B(n(t,"x","y").add(n(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},M=e.each(te,function(t){this[t.attribute]=function(e,i){if(e[t.set](s(i,t.type,t.fromSVG)),"color"===t.type&&e instanceof b){var n=e[t.get]();n&&n.transform((new g).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 w))return new w(i,t);t.insertChild(0,i)}},gradientTransform:d,transform:d,"fill-opacity":f,"stroke-opacity":f,visibility:function(t,e){t.setVisible("visible"===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 _(s(e,"array")),h=r(n,"width","height",!0);if(t instanceof w){var u=h?o.getSize().divide(h):1,l=(new g).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof m){h&&o.setSize(h);var c="visible"!=v(n,"overflow",a),d=t._definition;c&&!o.contains(d.getBounds())&&(c=new b.Rectangle(o).transform(d._matrix),c.setClipMask(!0),d.addChild(c))}}}),I={};y.inject({importSVG:function(t,e){return this.addChild(P(t,!0,e))}}),p.inject({importSVG:function(t,e){return this.activate(),P(t,!0,e)}})},paper=new(r.inject(e.exports,{enumerable:!0,Base:e,Numerical:o,DomElement:V,DomEvent:Z,Http:$,Key:U})),"function"==typeof define&&define.amd&&define("paper",paper),paper}; \ No newline at end of file diff --git a/dist/paper-full.js b/dist/paper-full.js index f1420e1d..039086ac 100644 --- a/dist/paper-full.js +++ b/dist/paper-full.js @@ -1,28 +1,28 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * Acorn.js * http://marijnhaverbeke.nl/acorn/ * * Acorn is a tiny, fast JavaScript parser written in JavaScript, @@ -33,8 +33,7 @@ var paper = new function(undefined) { var Base = new function() { - var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/, - slice = [].slice, + var hidden = /^(statics|preserve|enumerable|prototype|toString|valueOf)$/, forEach = [].forEach || function(iter, bind) { for (var i = 0, l = this.length; i < l; i++) @@ -79,10 +78,10 @@ var Base = new function() { return _define(obj, name, desc); }; - function inject(dest, src, enumerable, base, preserve, generics) { + function inject(dest, src, enumerable, base, preserve) { var beans; - function field(name, val, dontCheck, generics) { + function field(name, val, dontCheck) { var val = val || (val = describe(src, name)) && (val.get ? val : val.value); if (typeof val === 'string' && val[0] === '#') @@ -96,11 +95,11 @@ var Base = new function() { && (!preserve || !prev)) { if (isFunc && prev) val.base = prev; - if (isFunc && beans && val.length === 0 - && (bean = name.match(/^(get|is)(([A-Z])(.*))$/))) - beans.push([ bean[3].toLowerCase() + bean[4], bean[2] ]); + if (isFunc && beans + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beans[bean[3].toLowerCase() + bean[4]] = bean[2]; if (!res || isFunc || !res.get || typeof res.get !== 'function' - || res.get.length !== 0) + || !Base.isPlainObject(res)) res = { value: res, writable: true }; if ((describe(dest, name) || { configurable: true }).configurable) { @@ -109,27 +108,20 @@ var Base = new function() { } define(dest, name, res); } - if (generics && isFunc && (!preserve || !generics[name])) { - generics[name] = function(bind) { - return bind && dest[name].apply(bind, - slice.call(arguments, 1)); - }; - } } if (src) { - beans = []; + beans = {}; for (var name in src) if (src.hasOwnProperty(name) && !hidden.test(name)) - field(name, null, true, generics); + field(name, null, true); field('toString'); field('valueOf'); - for (var i = 0, l = beans.length; i < l; i++) { - var bean = beans[i], - part = bean[1]; - field(bean[0], { - get: dest['get' + part] || dest['is' + part], - set: dest['set' + part] - }, true); + for (var name in beans) { + var part = beans[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (get.length === 0 || set)) + field(name, { get: get, set: set }, true); } } return dest; @@ -151,10 +143,6 @@ var Base = new function() { return dest; } - function clone(obj) { - return copy(new obj.constructor(), obj); - } - return inject(function Base() { for (var i = 0, l = arguments.length; i < l; i++) copy(this, arguments[i]); @@ -166,7 +154,7 @@ var Base = new function() { statics = src.statics === true ? src : src.statics; if (statics != src) inject(proto, src, src.enumerable, base && base.prototype, - src.preserve, src.generics && this); + src.preserve); inject(this, statics, true, base, src.preserve); } for (var i = 1, l = arguments.length; i < l; i++) @@ -184,6 +172,7 @@ var Base = new function() { base.apply(this, arguments); }; ctor.prototype = create(this.prototype); + ctor.base = base; define(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true }); inject(ctor, this, true); @@ -240,8 +229,6 @@ if (typeof module !== 'undefined') module.exports = Base; Base.inject({ - generics: true, - toString: function() { return this._id != null ? (this._class || 'Object') + (this._name @@ -282,7 +269,9 @@ Base.inject({ statics: { - exports: {}, + exports: { + enumerable: true + }, extend: function extend() { var res = extend.base.apply(this, arguments), @@ -319,7 +308,8 @@ Base.inject({ if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1)) return false; for (var i in obj1) { - if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i])) + if (obj1.hasOwnProperty(i) + && !Base.equals(obj1[i], obj2[i])) return false; } return true; @@ -327,58 +317,57 @@ Base.inject({ return false; }, - read: function(list, start, length, options) { + read: function(list, start, options, length) { if (this === Base) { var value = this.peek(list, start); - list._index++; - list.__read = 1; + list.__index++; return value; } var proto = this.prototype, readIndex = proto._readIndex, - index = start || readIndex && list._index || 0; + 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; + list.__index = index + 1; return obj && options && options.clone ? obj.clone() : obj; } obj = Base.create(this.prototype); if (readIndex) obj.__read = true; - if (options) - obj.__options = options; 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; - list.__read = obj.__read; - delete obj.__read; - if (options) - delete obj.__options; + list.__index = index + obj.__read; + obj.__read = undefined; } return obj; }, peek: function(list, start) { - return list[list._index = start || list._index || 0]; + 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; + 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, 0, options) - : this.read(list, i, 1, options)); + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); } return res; }, - readNamed: function(list, name, start, length, options) { + readNamed: function(list, name, start, options, length) { var value = this.getNamed(list, name), hasObject = value !== undefined; if (hasObject) { @@ -389,7 +378,7 @@ Base.inject({ } filtered[name] = undefined; } - return this.read(hasObject ? [value] : list, start, length, options); + return this.read(hasObject ? [value] : list, start, options, length); }, getNamed: function(list, name) { @@ -464,7 +453,8 @@ Base.inject({ }, deserialize: function(json, create, _data) { - var res = json; + var res = json, + isRoot = !_data; _data = _data || {}; if (Array.isArray(json)) { var type = json[0], @@ -482,7 +472,7 @@ Base.inject({ } else if (type) { var args = res; if (create) { - res = create(type, args); + res = create(type, args, isRoot); } else { res = Base.create(type.prototype); type.apply(res, args); @@ -497,18 +487,21 @@ Base.inject({ }, exportJSON: function(obj, options) { - return JSON.stringify(Base.serialize(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) { + function(type, args, isRoot) { var obj = target && target.constructor === type ? target : Base.create(type.prototype), isTarget = obj === target; - if (args.length === 1 && obj instanceof Item + if (!isRoot && args.length === 1 && obj instanceof Item && (!(obj instanceof Layer) || isTarget)) { var arg = args[0]; if (Base.isPlainObject(arg)) @@ -538,7 +531,7 @@ Base.inject({ args.push.apply(args, items); var removed = list.splice.apply(list, args); for (var i = 0, l = removed.length; i < l; i++) - delete removed[i]._index; + removed[i]._index = undefined; for (var i = index + amount, l = list.length; i < l; i++) list[i]._index = i; return removed; @@ -617,27 +610,14 @@ var Callback = { if (!handlers) return false; var args = [].slice.call(arguments, 1), - PaperScript = paper.PaperScript, - handleException = PaperScript && PaperScript.handleException, that = this; - - function callHandlers() { - for (var i in handlers) { - if (handlers[i].apply(that, args) === false - && event && event.stop) - event.stop(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(that, args) === false + && event && event.stop) { + event.stop(); + break; } } - - if (handleException) { - try { - callHandlers(); - } catch (e) { - handleException(e); - } - } else { - callHandlers(); - } return true; }, @@ -649,6 +629,19 @@ var Callback = { off: '#detach', trigger: '#fire', + _installEvents: function(install) { + var handlers = this._handlers, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var entry = this._eventTypes[type], + func = entry[key]; + if (func) + func.call(this, type); + } + } + }, + statics: { inject: function inject() { for (var i = 0, l = arguments.length; i < l; i++) { @@ -667,11 +660,11 @@ var Callback = { return this[name]; }; src['set' + part] = function(func) { - if (func) { + var prev = this[name]; + if (prev) + this.detach(type, prev); + if (func) this.attach(type, func); - } else if (this[name]) { - this.detach(type, this[name]); - } this[name] = func; }; }); @@ -689,6 +682,11 @@ var PaperScope = Base.extend({ initialize: function PaperScope(script) { paper = this; + this.settings = { + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }; this.project = null; this.projects = []; this.tools = []; @@ -708,26 +706,19 @@ var PaperScope = Base.extend({ } }, - version: '0.9.15', + version: '0.9.16', getView: function() { return this.project && this.project.view; }, - getTool: function() { - if (!this._tool) - this._tool = new Tool(); - return this._tool; - }, - getPaper: function() { return this; }, - evaluate: function(code) { - var res = paper.PaperScript.evaluate(code, this); + execute: function(code) { + paper.PaperScript.execute(code, this); View.updateFocus(); - return res; }, install: function(scope) { @@ -740,10 +731,9 @@ var PaperScope = Base.extend({ } }); }); - for (var key in this) { - if (!/^(version|_id)/.test(key)) + for (var key in this) + if (!/^_/.test(key) && this[key]) scope[key] = this[key]; - } }, setup: function(canvas) { @@ -783,7 +773,7 @@ var PaperScope = Base.extend({ _id: 0, get: function(id) { - if (typeof id === 'object') + if (id && id.getAttribute) id = id.getAttribute('id'); return this._scopes[id] || null; }, @@ -898,15 +888,29 @@ var Numerical = new function() { sqrt = Math.sqrt, pow = Math.pow, cos = Math.cos, - PI = Math.PI; + PI = Math.PI, + TOLERANCE = 10e-6, + EPSILON = 10e-12; + + function setupRoots(roots, min, max) { + var unbound = min === undefined, + minE = min - EPSILON, + maxE = max + EPSILON, + count = 0; + return function(root) { + if (unbound || root > minE && root < maxE) + roots[count++] = root < min ? min : root > max ? max : root; + return count; + }; + } return { - TOLERANCE: 10e-6, - EPSILON: 10e-12, + TOLERANCE: TOLERANCE, + EPSILON: EPSILON, KAPPA: 4 * (sqrt(2) - 1) / 3, isZero: function(val) { - return abs(val) <= Numerical.EPSILON; + return abs(val) <= EPSILON; }, integrate: function(f, a, b, n) { @@ -927,10 +931,10 @@ var Numerical = new function() { findRoot: function(f, df, x, a, b, n, tolerance) { for (var i = 0; i < n; i++) { var fx = f(x), - dx = fx / df(x); + dx = fx / df(x), + nx = x - dx; if (abs(dx) < tolerance) - return x; - var nx = x - dx; + return nx; if (fx > 0) { b = x; x = nx <= a ? 0.5 * (a + b) : nx; @@ -939,65 +943,45 @@ var Numerical = new function() { x = nx >= b ? 0.5 * (a + b) : nx; } } + return x; }, solveQuadratic: function(a, b, c, roots, min, max) { - var epsilon = Numerical.EPSILON, - unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; + var add = setupRoots(roots, min, max); - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - - if (abs(a) < epsilon) { - if (abs(b) >= epsilon) + if (abs(a) < EPSILON) { + if (abs(b) >= EPSILON) return add(-c / b); - return abs(c) < epsilon ? -1 : 0; + return abs(c) < EPSILON ? -1 : 0; } var p = b / (2 * a); var q = c / a; var p2 = p * p; - if (p2 < q - epsilon) + if (p2 < q - EPSILON) return 0; - var s = p2 > q ? sqrt(p2 - q) : 0; - add (s - p); + var s = p2 > q ? sqrt(p2 - q) : 0, + count = add(s - p); if (s > 0) - add(-s - p); + count = add(-s - p); return count; }, solveCubic: function(a, b, c, d, roots, min, max) { - var epsilon = Numerical.EPSILON; - if (abs(a) < epsilon) + if (abs(a) < EPSILON) return Numerical.solveQuadratic(b, c, d, roots, min, max); - var unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; - - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - b /= a; c /= a; d /= a; - var bb = b * b, + var add = setupRoots(roots, min, max), + bb = b * b, p = (bb - 3 * c) / 9, q = (2 * bb * b - 9 * b * c + 27 * d) / 54, ppp = p * p * p, D = q * q - ppp; b /= 3; - if (abs(D) < epsilon) { - if (abs(q) < epsilon) + if (abs(D) < EPSILON) { + if (abs(q) < EPSILON) return add(-b); var sqp = sqrt(p), snq = q > 0 ? 1 : -1; @@ -1066,10 +1050,11 @@ var Point = Base.extend({ }, equals: function(point) { - return point === this || point && (this.x === point.x - && this.y === point.y - || Array.isArray(point) && this.x === point[0] - && this.y === point[1]) || false; + 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() { @@ -1086,28 +1071,28 @@ var Point = Base.extend({ return [f.number(this.x), f.number(this.y)]; }, - add: function(point) { - point = Point.read(arguments); + add: function() { + var point = Point.read(arguments); return new Point(this.x + point.x, this.y + point.y); }, - subtract: function(point) { - point = Point.read(arguments); + subtract: function() { + var point = Point.read(arguments); return new Point(this.x - point.x, this.y - point.y); }, - multiply: function(point) { - point = Point.read(arguments); + multiply: function() { + var point = Point.read(arguments); return new Point(this.x * point.x, this.y * point.y); }, - divide: function(point) { - point = Point.read(arguments); + divide: function() { + var point = Point.read(arguments); return new Point(this.x / point.x, this.y / point.y); }, - modulo: function(point) { - point = Point.read(arguments); + modulo: function() { + var point = Point.read(arguments); return new Point(this.x % point.x, this.y % point.y); }, @@ -1119,17 +1104,17 @@ var Point = Base.extend({ return matrix ? matrix._transformPoint(this) : this; }, - getDistance: function(point, squared) { - point = Point.read(arguments); - var x = point.x - this.x, + getDistance: function(_point, squared) { + 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); }, getLength: function() { - var length = this.x * this.x + this.y * this.y; - return arguments.length && arguments[0] ? length : Math.sqrt(length); + return Math.sqrt(this.x * this.x + this.y * this.y); }, setLength: function(length) { @@ -1148,7 +1133,6 @@ var Point = Base.extend({ this.y * scale ); } - return this; }, normalize: function(length) { @@ -1157,28 +1141,24 @@ var Point = Base.extend({ var current = this.getLength(), scale = current !== 0 ? length / current : 0, point = new Point(this.x * scale, this.y * scale); - point._angle = this._angle; + if (scale >= 0) + point._angle = this._angle; return point; }, getAngle: function() { - return this.getAngleInRadians(arguments[0]) * 180 / Math.PI; + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; }, setAngle: function(angle) { - angle = this._angle = angle * Math.PI / 180; - if (!this.isZero()) { - var length = this.getLength(); - this.set( - Math.cos(angle) * length, - Math.sin(angle) * length - ); - } - return this; + this.setAngleInRadians.call(this, angle * Math.PI / 180); }, + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + getAngleInRadians: function() { - if (arguments[0] === undefined) { + if (!arguments.length) { return this.isZero() ? this._angle || 0 : this._angle = Math.atan2(this.y, this.x); @@ -1193,15 +1173,23 @@ var Point = Base.extend({ } }, - getAngleInDegrees: function() { - return this.getAngle(arguments[0]); + 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; }, - getDirectedAngle: function(point) { + getDirectedAngle: function(_point) { + var point = _point; point = Point.read(arguments); return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; }, @@ -1215,7 +1203,7 @@ var Point = Base.extend({ c = Math.cos(angle); point = new Point( point.x * c - point.y * s, - point.y * c + point.x * s + point.x * s + point.y * c ); return center ? point.add(center) : point; }, @@ -1229,11 +1217,11 @@ var Point = Base.extend({ }, isColinear: function(point) { - return this.cross(point) < 0.00001; + return Math.abs(this.cross(point)) < 0.00001; }, isOrthogonal: function(point) { - return this.dot(point) < 0.00001; + return Math.abs(this.dot(point)) < 0.00001; }, isZero: function() { @@ -1244,18 +1232,18 @@ var Point = Base.extend({ return isNaN(this.x) || isNaN(this.y); }, - dot: function(point) { - point = Point.read(arguments); + dot: function() { + var point = Point.read(arguments); return this.x * point.x + this.y * point.y; }, - cross: function(point) { - point = Point.read(arguments); + cross: function() { + var point = Point.read(arguments); return this.x * point.y - this.y * point.x; }, - project: function(point) { - point = Point.read(arguments); + project: function() { + var point = Point.read(arguments); if (point.isZero()) { return new Point(0, 0); } else { @@ -1397,28 +1385,28 @@ var Size = Base.extend({ f.number(this.height)]; }, - add: function(size) { - size = Size.read(arguments); + add: function() { + var size = Size.read(arguments); return new Size(this.width + size.width, this.height + size.height); }, - subtract: function(size) { - size = Size.read(arguments); + subtract: function() { + var size = Size.read(arguments); return new Size(this.width - size.width, this.height - size.height); }, - multiply: function(size) { - size = Size.read(arguments); + multiply: function() { + var size = Size.read(arguments); return new Size(this.width * size.width, this.height * size.height); }, - divide: function(size) { - size = Size.read(arguments); + divide: function() { + var size = Size.read(arguments); return new Size(this.width / size.width, this.height / size.height); }, - modulo: function(size) { - size = Size.read(arguments); + modulo: function() { + var size = Size.read(arguments); return new Size(this.width % size.width, this.height % size.height); }, @@ -1550,7 +1538,7 @@ var Rectangle = Base.extend({ this.width = size.width; this.height = size.height; } - read = arguments._index; + read = arguments.__index; } if (this.__read) this.__read = read; @@ -1569,11 +1557,12 @@ var Rectangle = Base.extend({ }, equals: function(rect) { - if (Base.isPlainValue(rect)) - rect = Rectangle.read(arguments); - return rect === this - || rect && this.x === rect.x && this.y === rect.y - && this.width === rect.width && this.height === rect.height + 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; }, @@ -1594,24 +1583,24 @@ var Rectangle = Base.extend({ f.number(this.height)]; }, - getPoint: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.x, this.y, this, 'setPoint'); + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.x = point.x; this.y = point.y; }, - getSize: function() { - return new (arguments[0] ? Size : LinkedSize) - (this.width, this.height, this, 'setSize'); + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); }, - setSize: function(size) { - size = Size.read(arguments); + setSize: function() { + var size = Size.read(arguments); if (this._fixX) this.x += (this.width - size.width) * this._fixX; if (this._fixY) @@ -1690,20 +1679,24 @@ var Rectangle = Base.extend({ this._fixY = 0.5; }, - getCenter: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.getCenterX(), this.getCenterY(), this, 'setCenter'); + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, - setCenter: function(point) { - point = Point.read(arguments); + 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; + return this.width === 0 || this.height === 0; }, contains: function(arg) { @@ -1729,42 +1722,42 @@ var Rectangle = Base.extend({ && y + rect.height <= this.y + this.height; }, - intersects: function(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.max(this.x, rect.x), + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.min(this.x, rect.x), + 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(point) { - point = Point.read(arguments); + 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), @@ -1772,9 +1765,10 @@ var Rectangle = Base.extend({ return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, - expand: function(hor, ver) { - if (ver === undefined) - ver = hor; + 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); }, @@ -1803,12 +1797,12 @@ var Rectangle = Base.extend({ setY = 'set' + y, get = 'get' + part, set = 'set' + part; - this[get] = function() { - return new (arguments[0] ? Point : LinkedPoint) - (this[getX](), this[getY](), this, set); + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); }; - this[set] = function(point) { - point = Point.read(arguments); + this[set] = function() { + var point = Point.read(arguments); this[setX](point.x); this[setY](point.y); }; @@ -1855,7 +1849,7 @@ var LinkedRectangle = Rectangle.extend({ this[name] = function() { this._dontNotify = true; proto[name].apply(this, arguments); - delete this._dontNotify; + this._dontNotify = false; this._owner[this._setter](this); }; }, { @@ -1916,8 +1910,14 @@ var Matrix = Base.extend({ }, _changed: function() { - if (this._owner) - this._owner._changed(5); + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(5); + } + } }, clone: function() { @@ -1940,16 +1940,36 @@ var Matrix = Base.extend({ f.number(this._ty)].join(', ') + ']]'; }, - reset: function() { + 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() { + var owner = this._owner; + if (owner) { + owner.transform(null, true); + 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, 0, { readNull: true }); + center = Point.read(arguments, 0, { readNull: true }); if (center) this.translate(center); this._a *= scale.x; @@ -1962,20 +1982,10 @@ var Matrix = Base.extend({ return this; }, - translate: function(point) { - point = Point.read(arguments); - var 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; - }, - - rotate: function(angle, center) { - center = Point.read(arguments, 1); - angle = angle * Math.PI / 180; - var x = center.x, + 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), @@ -1996,22 +2006,31 @@ var Matrix = Base.extend({ }, shear: function() { - var point = Point.read(arguments), - center = Point.read(arguments, 0, 0, { readNull: true }); + 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 += point.y * this._b; - this._c += point.y * this._d; - this._b += point.x * a; - this._d += point.x * 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 a = this._a, b = this._b, @@ -2049,6 +2068,10 @@ var Matrix = Base.extend({ && this._tx === 0 && this._ty === 0; }, + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + isInvertible: function() { return !!this._getDeterminant(); }, @@ -2167,7 +2190,6 @@ var Matrix = Base.extend({ } return { - translation: this.getTranslation(), scaling: new Point(scaleX, scaleY), rotation: -Math.atan2(b, a) * 180 / Math.PI, shearing: shear @@ -2182,36 +2204,14 @@ var Matrix = Base.extend({ return new Point(this._tx, this._ty); }, - setTranslation: function() { - var point = Point.read(arguments); - this._tx = point.x; - this._ty = point.y; - this._changed(); - }, - getScaling: function() { return (this.decompose() || {}).scaling; }, - setScaling: function() { - var scaling = this.getScaling(); - if (scaling != null) { - var scale = Point.read(arguments); - (this._owner || this).scale( - scale.x / scaling.x, scale.y / scaling.y); - } - }, - getRotation: function() { return (this.decompose() || {}).rotation; }, - setRotation: function(angle) { - var rotation = this.getRotation(); - if (rotation != null) - (this._owner || this).rotate(angle - rotation); - }, - inverted: function() { var det = this._getDeterminant(); return det && new Matrix( @@ -2231,19 +2231,13 @@ var Matrix = Base.extend({ ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); } }, new function() { - return Base.each({ - scaleX: '_a', - scaleY: '_d', - translateX: '_tx', - translateY: '_ty', - shearX: '_b', - shearY: '_c' - }, function(prop, name) { - name = Base.capitalize(name); - this['get' + name] = function() { + return 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' + name] = function(value) { + this['set' + part] = function(value) { this[prop] = value; this._changed(); }; @@ -2376,8 +2370,7 @@ var Project = PaperScopeItem.extend({ this.view = view instanceof View ? view : View.create(view); this._selectedItems = {}; this._selectedItemCount = 0; - this._drawCount = 0; - this.options = {}; + this._updateVersion = 0; }, _serialize: function(options, dictionary) { @@ -2422,7 +2415,7 @@ var Project = PaperScopeItem.extend({ this.activeLayer = child; } else if (child instanceof Item) { (this.activeLayer - || this.addChild(new Layer({ insert: false }))).addChild(child); + || this.addChild(new Layer(Item.NO_INSERT))).addChild(child); } else { child = null; } @@ -2439,6 +2432,10 @@ var Project = PaperScopeItem.extend({ return items; }, + getOptions: function() { + return this._scope.settings; + }, + _updateSelection: function(item) { var id = item._id, selectedItems = this._selectedItems; @@ -2465,9 +2462,9 @@ var Project = PaperScopeItem.extend({ selectedItems[i].setFullySelected(false); }, - hitTest: function(point, options) { - point = Point.read(arguments); - options = HitResult.getOptions(Base.read(arguments)); + 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; @@ -2505,14 +2502,13 @@ var Project = PaperScopeItem.extend({ return Base.importJSON(json, layer && layer.isEmpty() && layer); }, - draw: function(ctx, matrix, ratio) { - this._drawCount++; + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; ctx.save(); matrix.applyToContext(ctx); var param = new Base({ offset: new Point(0, 0), - ratio: ratio, - transforms: [matrix], + pixelRatio: pixelRatio, trackTransforms: true }); for (var i = 0, l = this.layers.length; i < l; i++) @@ -2523,30 +2519,30 @@ var Project = PaperScopeItem.extend({ ctx.save(); ctx.strokeWidth = 1; for (var id in this._selectedItems) { - var item = this._selectedItems[id]; - if (item._drawCount === this._drawCount + var item = this._selectedItems[id], + size = this._scope.settings.handleSize; + half = size / 2; + if (item._updateVersion === this._updateVersion && (item._drawSelected || item._boundsSelected)) { var color = item.getSelectedColor() || item.getLayer().getSelectedColor(); ctx.strokeStyle = ctx.fillStyle = color ? color.toCanvasStyle(ctx) : '#009dec'; - var mx = item._globalMatrix; + var mx = matrix.clone().concatenate(item._globalMatrix); if (item._drawSelected) item._drawSelected(ctx, mx); if (item._boundsSelected) { var coords = mx._transformCorners( - item._getBounds('getBounds')); + item.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.beginPath(); - ctx.rect(coords[i] - 2, coords[++i] - 2, 4, 4); - ctx.fill(); - } + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); } } } @@ -2564,7 +2560,6 @@ var Symbol = Base.extend({ this.project.symbols.push(this); if (item) this.setDefinition(item, dontCenter); - this._instances = {}; }, _serialize: function(options, dictionary) { @@ -2575,24 +2570,27 @@ var Symbol = Base.extend({ }, _changed: function(flags) { - Base.each(this._instances, function(item) { - item._changed(flags); - }); + if (flags & 4) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } }, getDefinition: function() { return this._definition; }, - setDefinition: function(item ) { + setDefinition: function(item, _dontCenter) { if (item._parentSymbol) item = item.clone(); if (this._definition) - delete this._definition._parentSymbol; + this._definition._parentSymbol = null; this._definition = item; item.remove(); item.setSelected(false); - if (!arguments[1]) + if (!_dontCenter) item.setPosition(new Point()); item._parentSymbol = this; this._changed(5); @@ -2619,15 +2617,20 @@ var Item = Base.extend(Callback, { if (name) proto._type = Base.hyphenate(name); return res; - } + }, + + NO_INSERT: { insert: false } }, _class: 'Item', - _transformContent: true, + _applyMatrix: true, + _canApplyMatrix: true, _boundsSelected: false, + _selectChildren: false, _serializeFields: { name: null, matrix: new Matrix(), + pivot: null, locked: false, visible: true, blendMode: 'normal', @@ -2635,6 +2638,7 @@ var Item = Base.extend(Callback, { guide: false, selected: false, clipMask: false, + applyMatrix: null, data: {} }, @@ -2642,21 +2646,26 @@ var Item = Base.extend(Callback, { }, _initialize: function(props, point) { - this._id = Item._id = (Item._id || 0) + 1; + var internal = props && props.internal === true, + matrix = this._matrix = new Matrix(); + project = paper.project; + if (!internal) + this._id = Item._id = (Item._id || 0) + 1; + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this); if (!this._project) { - var project = paper.project; - if (props && props.insert === false) { + if (internal || props && props.insert === false) { this._setProject(project); } else { (project.activeLayer || new Layer()).addChild(this); } } - this._style = new Style(this._project._currentStyle, this); - var matrix = this._matrix = new Matrix(); - if (point) - matrix.translate(point); - matrix._owner = this; - return props ? this._set(props, { insert: true }) : true; + return props && props !== Item.NO_INSERT + ? this._set(props, { insert: true }) + : true; }, _events: new function() { @@ -2746,24 +2755,23 @@ var Item = Base.extend(Callback, { }, _changed: function(flags) { - var parent = this._parent, - project = this._project, - symbol = this._parentSymbol; - this._drawCount = null; + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; if (flags & 4) { - delete this._bounds; - delete this._position; + this._bounds = this._position = this._decomposed = + this._globalMatrix = undefined; } - if (parent && (flags + if (cacheParent && (flags & (4 | 8))) { - parent._clearBoundsCache(); + Item._clearBoundsCache(cacheParent); } if (flags & 2) { - this._clearBoundsCache(); + Item._clearBoundsCache(this); } if (project) { if (flags & 1) { - project._needsRedraw = true; + project._needsUpdate = true; } if (project._changes) { var entry = project._changesById[this._id]; @@ -2782,7 +2790,7 @@ var Item = Base.extend(Callback, { set: function(props) { if (props) - this._set(props); + this._set(props, { insert: true }); return this; }, @@ -2865,7 +2873,7 @@ var Item = Base.extend(Callback, { _guide: false, isSelected: function() { - if (this._children) { + if (this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) if (this._children[i].isSelected()) return true; @@ -2873,12 +2881,12 @@ var Item = Base.extend(Callback, { return this._selected; }, - setSelected: function(selected ) { - if (this._children && !arguments[1]) { + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) this._children[i].setSelected(selected); } - if ((selected = !!selected) != this._selected) { + if ((selected = !!selected) ^ this._selected) { this._selected = selected; this._project._updateSelection(this); this._changed(33); @@ -2934,71 +2942,63 @@ var Item = Base.extend(Callback, { this._data = data; }, - getPosition: function() { - var pos = this._position - || (this._position = this.getBounds().getCenter(true)); - return new (arguments[0] ? Point : LinkedPoint) - (pos.x, pos.y, this, 'setPosition'); + 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))); - } -}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], - function(name) { - this[name] = function() { - var getter = this._boundsGetter, - bounds = this._getCachedBounds(typeof getter == 'string' - ? getter : getter && getter[name] || name, arguments[0]); - return name === 'getBounds' + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments); + this._position = undefined; + }, + + _pivot: null, + + getRegistration: '#getPivot', + setRegistration: '#setPivot' +}, 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, null, + internalGetter); + return key === 'bounds' ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, this, 'setBounds') : bounds; }; + this[key] = { + get: this[getter] + }; }, { - _getCachedBounds: function(getter, matrix, cacheItem) { - var cache = (!matrix || matrix.equals(this._matrix)) && getter; - if (cacheItem && this._parent) { - var id = cacheItem._id, - ref = this._parent._boundsCache - = this._parent._boundsCache || { - ids: {}, - list: [] - }; - if (!ref.ids[id]) { - ref.list.push(cacheItem); - ref.ids[id] = cacheItem; - } - } - if (cache && this._bounds && this._bounds[cache]) - return this._bounds[cache].clone(); - var identity = this._matrix.isIdentity(); - matrix = !matrix || matrix.isIdentity() - ? identity ? null : this._matrix - : identity ? matrix : matrix.clone().concatenate(this._matrix); - var bounds = this._getBounds(getter, matrix, cache ? this : cacheItem); - if (cache) { - if (!this._bounds) - this._bounds = {}; - this._bounds[cache] = bounds.clone(); - } - return bounds; - }, - - _clearBoundsCache: function() { - if (this._boundsCache) { - for (var i = 0, list = this._boundsCache.list, l = list.length; - i < l; i++) { - var item = list[i]; - delete item._bounds; - if (item != this && item._boundsCache) - item._clearBoundsCache(); - } - delete this._boundsCache; - } - }, - _getBounds: function(getter, matrix, cacheItem) { var children = this._children; if (!children || children.length == 0) @@ -3022,9 +3022,9 @@ var Item = Base.extend(Callback, { : new Rectangle(); }, - setBounds: function(rect) { - rect = Rectangle.read(arguments); - var bounds = this.getBounds(), + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), matrix = new Matrix(), center = rect.getCenter(); matrix.translate(center); @@ -3036,53 +3036,155 @@ var Item = Base.extend(Callback, { 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; + var cacheParent = this._parent || this._parentSymbol; + if (cacheItem && cacheParent) { + var id = cacheItem._id, + ref = cacheParent._boundsCache = cacheParent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(cacheItem); + ref.ids[id] = cacheItem; + } + } + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + matrix = !matrix + ? _matrix + : _matrix + ? matrix.clone().concatenate(_matrix) + : matrix; + var bounds = this._getBounds(internalGetter || getter, matrix, + cache ? this : cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _clearBoundsCache: function(item) { + if (item._boundsCache) { + for (var i = 0, list = item._boundsCache.list, l = list.length; + i < l; i++) { + var child = list[i]; + child._bounds = child._position = undefined; + if (child !== item && child._boundsCache) + child._clearBoundsCache(); + } + item._boundsCache = undefined; + } + } } }), { + _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() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.scaling; + }, + + setScaling: function() { + var current = this.getScaling(); + if (current != null) { + 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(matrix) { this._matrix.initialize(matrix); - if (this._transformContent) - this.applyMatrix(true); - this._changed(5); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(5); + } }, getGlobalMatrix: function() { - return this._drawCount === this._project._drawCount - && this._globalMatrix || null; + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + if (this._parent) + matrix.preConcatenate(this._parent.getGlobalMatrix()); + matrix._updateVersion = updateVersion; + } + return matrix; }, - getTransformContent: function() { - return this._transformContent; + getApplyMatrix: function() { + return this._applyMatrix; }, - setTransformContent: function(transform) { - this._transformContent = transform; - if (transform) - this.applyMatrix(); + setApplyMatrix: function(transform) { + if (this._applyMatrix = this._canApplyMatrix && !!transform) + this.transform(null, true); }, + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', + getProject: function() { return this._project; }, - _setProject: function(project) { - if (this._project != project) { - var hasOnFrame = this.responds('frame'); - if (hasOnFrame) - this._animateItem(false); + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); this._project = project; - if (hasOnFrame) - this._animateItem(true); - if (this._children) { - for (var i = 0, l = this._children.length; i < l; i++) { - this._children[i]._setProject(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); + }, + + _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() { @@ -3155,7 +3257,7 @@ var Item = Base.extend(Callback, { }, clone: function(insert) { - return this._clone(new this.constructor({ insert: false }), insert); + return this._clone(new this.constructor(Item.NO_INSERT), insert); }, _clone: function(copy, insert) { @@ -3167,7 +3269,7 @@ var Item = Base.extend(Callback, { if (insert || insert === undefined) copy.insertAbove(this); var keys = ['_locked', '_visible', '_blendMode', '_opacity', - '_clipMask', '_guide']; + '_clipMask', '_guide', '_applyMatrix']; for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; if (this.hasOwnProperty(key)) @@ -3187,22 +3289,22 @@ var Item = Base.extend(Callback, { rasterize: function(resolution) { var bounds = this.getStrokeBounds(), - scale = (resolution || 72) / 72, + view = this._project.view, + scale = (resolution || view && view.getResolution() || 72) / 72, topLeft = bounds.getTopLeft().floor(), bottomRight = bounds.getBottomRight().ceil() size = new Size(bottomRight.subtract(topLeft)), - canvas = CanvasProvider.getCanvas(size), + 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({ transforms: [matrix] })); ctx.restore(); - var raster = new Raster({ - canvas: canvas, - insert: false - }); - raster.setPosition(topLeft.add(size.divide(2))); + 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; }, @@ -3220,33 +3322,47 @@ var Item = Base.extend(Callback, { } return false; } - return point.isInside(this._getBounds('getBounds')); + return point.isInside(this.getInternalBounds()); }, hitTest: function(point, options) { point = Point.read(arguments); options = HitResult.getOptions(Base.read(arguments)); - if (this._locked || !this._visible || this._guide && !options.guides) return null; - if (!this._children && !this.getRoughBounds() - .expand(2 * options.tolerance)._containsPoint(point)) - return null; - point = this._matrix._inverseTransform(point); + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this._project.view, + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.clone().concatenate(matrix) + : this.getGlobalMatrix().clone().preConcatenate( + view ? view._matrix : new Matrix()), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 0.00001) + ); + point = matrix._inverseTransform(point); - var that = this, + 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 && this._type !== options.type), + that = this, res; + function checkBounds(type, part) { var pt = bounds['get' + part](); - if (point.getDistance(pt) < options.tolerance) + if (point.subtract(pt).divide(tolerancePadding).length <= 1) return new HitResult(type, that, { name: Base.hyphenate(part), point: pt }); } - if ((options.center || options.bounds) && - !(this instanceof Layer && !this._parent)) { - var bounds = this._getBounds('getBounds'); + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); if (options.center) res = checkBounds('center', 'Center'); if (!res && options.bounds) { @@ -3259,24 +3375,27 @@ var Item = Base.extend(Callback, { } } - if ((res || (res = this._children || !(options.guides && !this._guide - || options.selected && !this._selected) - ? this._hitTest(point, options) : null)) - && res.point) { - res.point = that._matrix.transform(res.point); + 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._hitTest(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; return res; }, + _getChildHitTestOptions: function(options) { + return options; + }, + _hitTest: function(point, options) { - var children = this._children; - if (children) { - for (var i = children.length - 1, res; i >= 0; i--) - if (res = children[i].hitTest(point, options)) - return res; - } else if (options.fill && this.hasFill() && this._contains(point)) { + if (options.fill && this.hasFill() && this._contains(point)) return new HitResult('fill', this); - } }, matches: function(match) { @@ -3375,16 +3494,17 @@ var Item = Base.extend(Callback, { items = Array.prototype.slice.apply(items); for (var i = items.length - 1; i >= 0; i--) { var item = items[i]; - if (_type && item._type !== _type) + if (_type && item._type !== _type) { items.splice(i, 1); - else + } else { item._remove(true); + } } Base.splice(children, items, index, 0); for (var i = 0, l = items.length; i < l; i++) { var item = items[i]; item._parent = this; - item._setProject(this._project); + item._setProject(this._project, true); if (item._name) item.setName(item._name); } @@ -3432,7 +3552,7 @@ var Item = Base.extend(Callback, { reduce: function() { if (this._children && this._children.length === 1) { - var child = this._children[0]; + var child = this._children[0].reduce(); child.insertAbove(this); child.setStyle(this._style); this.remove(); @@ -3465,8 +3585,7 @@ var Item = Base.extend(Callback, { this._removeNamed(); if (this._index != null) Base.splice(this._parent._children, null, this._index, 1); - if (this.responds('frame')) - this._animateItem(false); + this._installEvents(false); if (notify) this._parent._changed(7); this._parent = null; @@ -3580,90 +3699,87 @@ var Item = Base.extend(Callback, { return false; }, - scale: function(hor, ver , center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; - } - return this.transform(new Matrix().scale(hor, ver, - center || this.getPosition(true))); - }, - translate: function() { var mx = new Matrix(); return this.transform(mx.translate.apply(mx, arguments)); }, - rotate: function(angle, center) { + 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))); - }, + }; +}, { - shear: function(hor, ver, center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; +}), { + transform: function(matrix, _applyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && (!_matrix.isIdentity() || matrix); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + pivot.transform(_matrix); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); } - return this.transform(new Matrix().shear(hor, ver, - center || this.getPosition(true))); - }, - - transform: function(matrix ) { var bounds = this._bounds, position = this._position; - this._matrix.preConcatenate(matrix); - if (this._transformContent || arguments[1]) - this.applyMatrix(true); this._changed(5); - if (bounds && matrix.getRotation() % 90 === 0) { + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { for (var key in bounds) { var rect = bounds[key]; - matrix._transformBounds(rect, rect); + 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 (position) { + } else if (matrix && position) { this._position = matrix._transformPoint(position, position); } return this; }, - _applyMatrix: function(matrix, applyMatrix) { + _transformContent: function(matrix) { var children = this._children; - if (children && children.length > 0) { + if (children) { for (var i = 0, l = children.length; i < l; i++) - children[i].transform(matrix, applyMatrix); + children[i].transform(matrix, true); return true; } }, - applyMatrix: function(_dontNotify) { - var matrix = this._matrix; - if (this._applyMatrix(matrix, true)) { - var style = this._style, - fillColor = style.getFillColor(true), - strokeColor = style.getStrokeColor(true); - if (fillColor) - fillColor.transform(matrix); - if (strokeColor) - strokeColor.transform(matrix); - matrix.reset(); - } - if (!_dontNotify) - this._changed(5); - }, - globalToLocal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._transformPoint(Point.read(arguments)); + return matrix && matrix._inverseTransform(Point.read(arguments)); }, localToGlobal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._inverseTransform(Point.read(arguments)); + return matrix && matrix._transformPoint(Point.read(arguments)); }, fitBounds: function(rectangle, fill) { @@ -3731,13 +3847,21 @@ var Item = Base.extend(Callback, { draw: function(ctx, param) { if (!this._visible || this._opacity === 0) return; - this._drawCount = this._project._drawCount; + var updateVersion = this._updateVersion = this._project._updateVersion; var trackTransforms = param.trackTransforms, - transforms = param.transforms, + transforms = param.transforms = param.transforms || [new Matrix()], + matrix = this._matrix, parentMatrix = transforms[transforms.length - 1], - globalMatrix = parentMatrix.clone().concatenate(this._matrix); - if (trackTransforms) + globalMatrix = parentMatrix.clone().concatenate(matrix); + if (!globalMatrix.isInvertible()) + return; + if (trackTransforms) { + if (!transforms) + transforms = param.transforms = []; transforms.push(this._globalMatrix = globalMatrix); + globalMatrix._updateVersion = updateVersion; + } + var blendMode = this._blendMode, opacity = this._opacity, normalBlend = blendMode === 'normal', @@ -3754,7 +3878,8 @@ var Item = Base.extend(Callback, { itemOffset = param.offset = bounds.getTopLeft().floor(); mainCtx = ctx; ctx = CanvasProvider.getContext( - bounds.getSize().ceil().add(new Size(1, 1)), param.ratio); + bounds.getSize().ceil().add(new Size(1, 1)), + param.pixelRatio); } ctx.save(); if (direct) { @@ -3764,7 +3889,7 @@ var Item = Base.extend(Callback, { } else { ctx.translate(-itemOffset.x, -itemOffset.y); } - (direct ? this._matrix : globalMatrix).applyToContext(ctx); + (direct ? matrix : globalMatrix).applyToContext(ctx); if (!direct && param.clipItem) param.clipItem.draw(ctx, param.extend({ clip: true })); this._draw(ctx, param); @@ -3775,7 +3900,7 @@ var Item = Base.extend(Callback, { ctx.clip(); if (!direct) { BlendMode.process(blendMode, ctx, mainCtx, opacity, - itemOffset.subtract(prevOffset).multiply(param.ratio)); + itemOffset.subtract(prevOffset).multiply(param.pixelRatio)); CanvasProvider.release(ctx); param.offset = prevOffset; } @@ -3808,6 +3933,7 @@ var Item = Base.extend(Callback, { var Group = Item.extend({ _class: 'Group', + _selectChildren: true, _serializeFields: { children: [] }, @@ -3821,12 +3947,8 @@ var Group = Item.extend({ _changed: function _changed(flags) { _changed.base.call(this, flags); - if (flags & 2 && this._transformContent - && !this._matrix.isIdentity()) { - this.applyMatrix(); - } if (flags & (2 | 256)) { - delete this._clipItem; + this._clipItem = undefined; } }, @@ -3888,7 +4010,8 @@ var Layer = Group.extend({ this._project.activeLayer = this.getNextSibling() || this.getPreviousSibling(); Base.splice(this._project.layers, null, this._index, 1); - this._project._needsRedraw = true; + this._installEvents(false); + this._project._needsUpdate = true; return true; } return false; @@ -3917,7 +4040,7 @@ var Layer = Group.extend({ this._remove(true); Base.splice(item._project.layers, [this], item._index + (above ? 1 : 0), 0); - this._setProject(item._project); + this._setProject(item._project, true); return this; } return _insert.base.call(this, above, item, _preserve); @@ -3926,14 +4049,17 @@ var Layer = Group.extend({ var Shape = Item.extend({ _class: 'Shape', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsSelected: true, + _serializeFields: { + shape: null, + size: null, + radius: null + }, - initialize: function Shape(shape, center, size, radius, props) { - this._shape = shape; - this._size = size; - this._radius = radius; - this._initialize(props, center); + initialize: function Shape(props) { + this._initialize(props); }, _equals: function(item) { @@ -3943,26 +4069,33 @@ var Shape = Item.extend({ }, clone: function(insert) { - return this._clone(new Shape(this._shape, this.getPosition(true), - this._size.clone(), - this._radius.clone ? this._radius.clone() : this._radius, - { insert: false }), insert); + var copy = new Shape(Item.NO_INSERT); + copy.setShape(this._shape); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); }, getShape: function() { return this._shape; }, + setShape: function(shape) { + this._shape = shape; + }, + getSize: function() { var size = this._size; return new LinkedSize(size.width, size.height, this, 'setSize'); }, setSize: function() { - var shape = this._shape, - size = Size.read(arguments); - if (!this._size.equals(size)) { - var width = size.width, + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var shape = this._shape, + width = size.width, height = size.height; if (shape === 'rectangle') { var radius = Size.min(this._radius, size.divide(2)); @@ -3995,14 +4128,18 @@ var Shape = Item.extend({ this._size.set(size, size); } else { radius = Size.read(arguments); - if (this._radius.equals(radius)) - return; - this._radius.set(radius.width, radius.height); - if (shape === 'rectangle') { - var size = Size.max(this._size, radius.multiply(2)); - this._size.set(size.width, size.height); - } else if (shape === 'ellipse') { - this._size.set(radius.width * 2, radius.height * 2); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (shape === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (shape === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } } } this._changed(5); @@ -4040,7 +4177,7 @@ var Shape = Item.extend({ } else { var rx = radius.width, ry = radius.height, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (shape === 'ellipse') { var cx = rx * kappa, cy = ry * kappa; @@ -4170,7 +4307,11 @@ new function() { statics: new function() { function createShape(shape, point, size, radius, args) { - return new Shape(shape, point, size, radius, Base.getNamed(args)); + var item = new Shape(Base.getNamed(args)); + item._shape = shape; + item._size = size; + item._radius = radius; + return item.translate(point); } return { @@ -4214,7 +4355,8 @@ statics: new function() { var Raster = Item.extend({ _class: 'Raster', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: 'getBounds', _boundsSelected: true, _serializeFields: { @@ -4239,15 +4381,17 @@ var Raster = Item.extend({ }, clone: function(insert) { - var param = { insert: false }, - image = this._image; + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; if (image) { - param.image = image; - } else if (this._canvas) { - var canvas = param.canvas = CanvasProvider.getCanvas(this._size); - canvas.getContext('2d').drawImage(this._canvas, 0, 0); + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setCanvas(copyCanvas); } - return this._clone(new Raster(param), insert); + return this._clone(copy, insert); }, getSize: function() { @@ -4326,10 +4470,10 @@ var Raster = Item.extend({ setCanvas: '#setImage', - getContext: function() { + getContext: function(modify) { if (!this._context) this._context = this.getCanvas().getContext('2d'); - if (arguments[0]) { + if (modify) { this._image = null; this._changed(129); } @@ -4346,25 +4490,25 @@ var Raster = Item.extend({ setSource: function(src) { var that = this, - image = document.getElementById(src) || new Image(); + image; function loaded() { var view = that._project.view; if (view) paper = view._scope; + that.setImage(image); that.fire('load'); if (view) - view.draw(true); + view.update(); } + image = document.getElementById(src) || new Image(); + if (image.naturalWidth && image.naturalHeight) { setTimeout(loaded, 0); } else { DomEvent.add(image, { - load: function() { - that.setImage(image); - loaded(); - } + load: loaded }); if (!image.src) image.src = src; @@ -4376,20 +4520,18 @@ var Raster = Item.extend({ return this._canvas || this._image; }, - getSubCanvas: function(rect) { - rect = Rectangle.read(arguments); - var ctx = CanvasProvider.getContext(rect.getSize()); + getSubCanvas: function(rect) { + 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(rect) { - rect = Rectangle.read(arguments); - var raster = new Raster({ - canvas: this.getSubCanvas(rect), - insert: false - }); + getSubRaster: function(rect) { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setCanvas(this.getSubCanvas(rect)); raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); raster._matrix.preConcatenate(this._matrix); raster.insertAbove(this); @@ -4404,8 +4546,8 @@ var Raster = Item.extend({ return canvas ? canvas.toDataURL() : null; }, - drawImage: function(image, point) { - point = Point.read(arguments, 1); + drawImage: function(image ) { + var point = Point.read(arguments, 1); this.getContext(true).drawImage(image, point.x, point.y); }, @@ -4459,8 +4601,8 @@ var Raster = Item.extend({ return total ? Color.read(channels) : null; }, - getPixel: function(point) { - point = Point.read(arguments); + getPixel: function(point) { + 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); @@ -4481,21 +4623,21 @@ var Raster = Item.extend({ ctx.putImageData(imageData, point.x, point.y); }, - createImageData: function(size) { - size = Size.read(arguments); + createImageData: function() { + var size = Size.read(arguments); return this.getContext().createImageData(size.width, size.height); }, - getImageData: function(rect) { - rect = Rectangle.read(arguments); + getImageData: function(rect) { + 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, point) { - point = Point.read(arguments, 1); + setImageData: function(data ) { + var point = Point.read(arguments, 1); this.getContext(true).putImageData(data, point.x, point.y); }, @@ -4534,7 +4676,8 @@ var Raster = Item.extend({ var PlacedSymbol = Item.extend({ _class: 'PlacedSymbol', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: { getBounds: 'getStrokeBounds' }, _boundsSelected: true, _serializeFields: { @@ -4556,32 +4699,30 @@ var PlacedSymbol = Item.extend({ }, setSymbol: function(symbol) { - if (this._symbol) - delete this._symbol._instances[this._id]; this._symbol = symbol; - symbol._instances[this._id] = this; + this._changed(5); }, clone: function(insert) { - return this._clone(new PlacedSymbol({ - symbol: this.symbol, - insert: false - }), 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) { - return this.symbol._definition._getCachedBounds(getter, matrix); + _getBounds: function(getter, matrix, cacheItem) { + return this.symbol._definition._getCachedBounds(getter, matrix, + cacheItem); }, _hitTest: function(point, options, matrix) { - var result = this._symbol._definition._hitTest(point, options, matrix); - if (result) - result.item = this; - return result; + var res = this._symbol._definition._hitTest(point, options, matrix); + if (res) + res.item = this; + return res; }, _draw: function(ctx, param) { @@ -4606,7 +4747,7 @@ var HitResult = Base.extend({ getOptions: function(options) { return options && options._merged ? options : new Base({ type: null, - tolerance: paper.project.options.hitTolerance || 2, + tolerance: paper.settings.hitTolerance, fill: !options, stroke: !options, segments: !options, @@ -4659,27 +4800,30 @@ var Segment = Base.extend({ }, _changed: function(point) { - if (!this._path) + var path = this._path; + if (!path) return; - var curve = this._path._curves && this.getCurve(), - other; - if (curve) { - curve._changed(); - if (other = (curve[point == this._point - || point == this._handleIn && curve._segment1 == this - ? 'getPrevious' : 'getNext']())) { - other._changed(); - } + var curves = path._curves, + index = this._index, + curveIn, curveOut; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curveIn = curves[index - 1] + || path._closed && curves[curves.length - 1])) + curveIn._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curveOut = curves[index])) + curveOut._changed(); } - this._path._changed(5); + path._changed(5); }, getPoint: function() { return this._point; }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this._point.set(point.x, point.y); }, @@ -4687,8 +4831,8 @@ var Segment = Base.extend({ return this._handleIn; }, - setHandleIn: function(point) { - point = Point.read(arguments); + setHandleIn: function() { + var point = Point.read(arguments); this._handleIn.set(point.x, point.y); }, @@ -4696,8 +4840,8 @@ var Segment = Base.extend({ return this._handleOut; }, - setHandleOut: function(point) { - point = Point.read(arguments); + setHandleOut: function() { + var point = Point.read(arguments); this._handleOut.set(point.x, point.y); }, @@ -4732,7 +4876,7 @@ var Segment = Base.extend({ var next = this.getNext(), handle1 = this._handleOut, handle2 = next._handleIn, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (handle1.isOrthogonal(handle2)) { var from = this._point, to = next._point, @@ -4746,60 +4890,39 @@ var Segment = Base.extend({ return false; }, - _isSelected: function(point) { + _selectionState: 0, + + isSelected: function(_point) { var state = this._selectionState; - return point == this._point ? !!(state & 4) - : point == this._handleIn ? !!(state & 1) - : point == this._handleOut ? !!(state & 2) + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) : false; }, - _setSelected: function(point, selected) { + setSelected: function(selected, _point) { var path = this._path, selected = !!selected, - state = this._selectionState || 0, - selection = [ - !!(state & 4), - !!(state & 1), - !!(state & 2) - ]; - if (point === this._point) { - if (selected) { - selection[1] = selection[2] = false; - } else { - var previous = this.getPrevious(), - next = this.getNext(); - selection[1] = previous && (previous._point.isSelected() - || previous._handleOut.isSelected()); - selection[2] = next && (next._point.isSelected() - || next._handleIn.isSelected()); - } - selection[0] = 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 { - var index = point === this._handleIn ? 1 : 2; - if (selection[index] != selected) { - if (selected) - selection[0] = false; - selection[index] = selected; - } + state &= ~flag; } - this._selectionState = (selection[0] ? 4 : 0) - | (selection[1] ? 1 : 0) - | (selection[2] ? 2 : 0); - if (path && state != this._selectionState) { - path._updateSelection(this, state, this._selectionState); + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); path._changed(33); } }, - isSelected: function() { - return this._isSelected(this._point); - }, - - setSelected: function(selected) { - this._setSelected(this._point, selected); - }, - getIndex: function() { return this._index !== undefined ? this._index : null; }, @@ -4865,6 +4988,11 @@ var Segment = Base.extend({ 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() @@ -4922,13 +5050,14 @@ var SegmentPoint = Point.extend({ x = y = 0; } else if ((x = point[0]) !== undefined) { y = point[1]; - } else { - if ((x = point.x) === undefined) { - point = Point.read(arguments); - x = point.x; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; } - y = point.y; - selected = point.selected; + y = pt.y; + selected = pt.selected; } this._x = x; this._y = y; @@ -4977,11 +5106,11 @@ var SegmentPoint = Point.extend({ }, setSelected: function(selected) { - this._owner._setSelected(this, selected); + this._owner.setSelected(selected, this); }, isSelected: function() { - return this._owner._isSelected(this); + return this._owner.isSelected(this); } }); @@ -5021,16 +5150,15 @@ var Curve = Base.extend({ }, _changed: function() { - delete this._length; - delete this._bounds; + this._length = this._bounds = undefined; }, getPoint1: function() { return this._segment1._point; }, - setPoint1: function(point) { - point = Point.read(arguments); + setPoint1: function() { + var point = Point.read(arguments); this._segment1._point.set(point.x, point.y); }, @@ -5038,8 +5166,8 @@ var Curve = Base.extend({ return this._segment2._point; }, - setPoint2: function(point) { - point = Point.read(arguments); + setPoint2: function() { + var point = Point.read(arguments); this._segment2._point.set(point.x, point.y); }, @@ -5047,8 +5175,8 @@ var Curve = Base.extend({ return this._segment1._handleOut; }, - setHandle1: function(point) { - point = Point.read(arguments); + setHandle1: function() { + var point = Point.read(arguments); this._segment1._handleOut.set(point.x, point.y); }, @@ -5056,8 +5184,8 @@ var Curve = Base.extend({ return this._segment2._handleIn; }, - setHandle2: function(point) { - point = Point.read(arguments); + setHandle2: function() { + var point = Point.read(arguments); this._segment2._handleIn.set(point.x, point.y); }, @@ -5090,16 +5218,21 @@ var Curve = Base.extend({ }, isSelected: function() { - return this.getHandle1().isSelected() && this.getHandle2().isSelected(); + 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() { - return Curve.getValues(this._segment1, this._segment2); + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); }, getPoints: function() { @@ -5111,15 +5244,12 @@ var Curve = Base.extend({ }, getLength: function() { - var from = arguments[0], - to = arguments[1], - fullLength = arguments.length === 0 || from === 0 && to === 1; - if (fullLength && this._length != null) - return this._length; - var length = Curve.getLength(this.getValues(), from, to); - if (fullLength) - this._length = length; - return length; + if (this._length == null) { + this._length = this.isLinear() + ? this._segment2._point.getDistance(this._segment1._point) + : Curve.getLength(this.getValues(), 0, 1); + } + return this._length; }, getArea: function() { @@ -5130,20 +5260,25 @@ var Curve = Base.extend({ return new Curve(Curve.getPart(this.getValues(), from, to)); }, + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + isLinear: function() { return this._segment1._handleOut.isZero() && this._segment2._handleIn.isZero(); }, + isHorizontal: function() { + return this.isLinear() && Numerical.isZero( + this._segment1._point._y - this._segment2._point._y); + }, + getIntersections: function(curve) { return Curve.getIntersections(this.getValues(), curve.getValues(), this, curve, []); }, - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); - }, - _getParameter: function(offset, isParameter) { return isParameter ? offset @@ -5154,13 +5289,13 @@ var Curve = Base.extend({ : this.getParameterAt(offset, 0); }, - divide: function(offset, isParameter) { + divide: function(offset, isParameter, ignoreLinear) { var parameter = this._getParameter(offset, isParameter), tolerance = 0.00001, res = null; if (parameter > tolerance && parameter < 1 - tolerance) { var parts = Curve.subdivide(this.getValues(), parameter), - isLinear = this.isLinear(), + isLinear = ignoreLinear ? false : this.isLinear(), left = parts[0], right = parts[1]; @@ -5199,6 +5334,22 @@ var Curve = Base.extend({ : null; }, + reverse: function() { + return new Curve(this._segment2.reverse(), this._segment1.reverse()); + }, + + 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); }, @@ -5214,17 +5365,20 @@ var Curve = Base.extend({ }, statics: { - getValues: function(segment1, segment2) { + getValues: function(segment1, segment2, matrix) { var p1 = segment1._point, h1 = segment1._handleOut, h2 = segment2._handleIn, - p2 = segment2._point; - return [ - p1._x, p1._y, - p1._x + h1._x, p1._y + h1._y, - p2._x + h2._x, p2._y + h2._y, - p2._x, p2._y - ]; + 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, 0, values, 0, 6); + return values; }, evaluate: function(v, t, type) { @@ -5232,11 +5386,13 @@ statics: { c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], + tolerance = 0.00001, x, y; - if (type === 0 && (t === 0 || t === 1)) { - x = t === 0 ? p1x : p2x; - y = t === 0 ? p1y : p2y; + if (type === 0 && (t < tolerance || t > 1 - tolerance)) { + var isZero = t < tolerance; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; } else { var cx = 3 * (c1x - p1x), bx = 3 * (c2x - c1x) - cx, @@ -5249,11 +5405,16 @@ statics: { x = ((ax * t + bx) * t + cx) * t + p1x; y = ((ay * t + by) * t + cy) * t + p1y; } else { - var tolerance = 0.00001; if (t < tolerance && c1x === p1x && c1y === p1y || t > 1 - tolerance && c2x === p2x && c2y === p2y) { x = p2x - p1x; y = p2y - p1y; + } else if (t < tolerance) { + x = cx; + y = cy; + } else if (t > 1 - tolerance) { + 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; @@ -5265,7 +5426,7 @@ statics: { } } } - return type == 2 ? new Point(y, -x) : new Point(x, y); + return type === 2 ? new Point(y, -x) : new Point(x, y); }, subdivide: function(v, t) { @@ -5300,11 +5461,10 @@ statics: { }, getParameterOf: function(v, x, y) { - if (Math.abs(v[0] - x) < 0.00001 - && Math.abs(v[1] - y) < 0.00001) + var tolerance = 0.00001; + if (Math.abs(v[0] - x) < tolerance && Math.abs(v[1] - y) < tolerance) return 0; - if (Math.abs(v[6] - x) < 0.00001 - && Math.abs(v[7] - y) < 0.00001) + if (Math.abs(v[6] - x) < tolerance && Math.abs(v[7] - y) < tolerance) return 1; var txs = [], tys = [], @@ -5317,7 +5477,7 @@ statics: { 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) < 0.00001) + if (Math.abs(tx - ty) < tolerance) return (tx + ty) * 0.5; } } @@ -5404,87 +5564,6 @@ statics: { + t * t * t * v3, padding); } - }, - - _getWinding: function(v, prev, x, y, roots1, roots2) { - var tolerance = 0.00001, - abs = Math.abs; - - function getDirection(v) { - var y0 = v[1], - y1 = v[7], - dir = y0 > y1 ? -1 : 1; - return dir === 1 && (y < y0 || y > y1) - || dir === -1 && (y < y1 || y > y0) - ? 0 - : dir; - } - - if (Curve.isLinear(v)) { - var dir = getDirection(v); - if (!dir) - return 0; - var cross = (v[6] - v[0]) * (y - v[1]) - (v[7] - v[1]) * (x - v[0]); - return (cross < -tolerance ? -1 : 1) == dir ? 0 : dir; - } - - var y0 = v[1], - y1 = v[3], - y2 = v[5], - y3 = v[7]; - var a = 3 * (y1 - y2) - y0 + y3, - b = 2 * (y0 + y2) - 4 * y1, - c = y1 - y0; - var count = Numerical.solveQuadratic(a, b, c, roots1, tolerance, - 1 - tolerance), - part, - rest = v, - t1 = roots1[0], - winding = 0; - for (var i = 0; i <= count; i++) { - if (i === count) { - part = rest; - } else { - var curves = Curve.subdivide(rest, t1); - part = curves[0]; - rest = curves[1]; - t1 = roots1[i]; - t1 = (roots1[i + 1] - t1) / (1 - t1); - } - if (i > 0) - part[3] = part[1]; - if (i < count) - part[5] = rest[1]; - var dir = getDirection(part); - if (!dir) - continue; - var t2, - px; - if (Curve.solveCubic(part, 1, y, roots2, -tolerance, 1 + -tolerance) - === 1) { - t2 = roots2[0]; - px = Curve.evaluate(part, t2, 0).x; - } else { - var mid = (part[1] + part[7]) / 2; - t2 = y < mid && dir > 0 ? 0 : 1; - if (t2 === 1 && y == part[7]) - continue; - px = t2 === 0 ? part[0] : part[6]; - } - var slope = Curve.evaluate(part, t2, 1).y, - stationary = abs(slope) < tolerance || t2 < tolerance - && Curve.evaluate(prev, 1, 1).y * slope < 0; - if (x >= px + (stationary ? -tolerance : tolerance * dir) - && !(stationary && (abs(t2) < tolerance - && abs(x - part[0]) > tolerance - || abs(t2 - 1) < tolerance - && abs(x - part[6]) > tolerance))) { - winding += stationary && abs(t2 - (dir > 0 ? 1 : 0)) < tolerance - ? -dir : dir; - } - prev = part; - } - return winding; } }}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], function(name) { @@ -5518,8 +5597,8 @@ statics: { start !== undefined ? start : offset < 0 ? 1 : 0); }, - getParameterOf: function(point) { - point = Point.read(arguments); + getParameterOf: function(point) { + var point = Point.read(arguments); return Curve.getParameterOf(this.getValues(), point.x, point.y); }, @@ -5529,17 +5608,16 @@ statics: { return new CurveLocation(this, offset); }, - getLocationOf: function(point) { - point = Point.read(arguments); - var t = this.getParameterOf(point); + getLocationOf: function(point) { + var point = Point.read(arguments), + t = this.getParameterOf(point); return t != null ? new CurveLocation(this, t) : null; }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var values = this.getValues(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + values = this.getValues(), count = 100, - tolerance = Numerical.TOLERANCE, minDist = Infinity, minT = 0; @@ -5559,7 +5637,7 @@ statics: { refine(i / count); var step = 1 / (count * 2); - while (step > tolerance) { + while (step > 0.00001) { if (!refine(minT - step) && !refine(minT + step)) step /= 2; } @@ -5569,7 +5647,7 @@ statics: { }, getNearestPoint: function(point) { - point = Point.read(arguments); + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); } @@ -5610,11 +5688,12 @@ new function() { if (b === undefined) b = 1; var isZero = Numerical.isZero; - if (isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + 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])) { var dx = v[6] - v[0], dy = v[7] - v[1]; - return (b - a) * Math.sqrt(dx * dx + dy * dy); + return Math.sqrt(dx * dx + dy * dy); } var ds = getLengthIntegrand(v); return Numerical.integrate(ds, a, b, getIterations(a, b)); @@ -5648,148 +5727,89 @@ new function() { } }; }, new function() { - function addLocation(locations, curve1, t1, point1, curve2, t2, point2) { - var first = locations[0], - last = locations[locations.length - 1]; - if ((!first || !point1.isClose(first._point, Numerical.EPSILON)) - && (!last || !point1.isClose(last._point, Numerical.EPSILON))) - locations.push( - new CurveLocation(curve1, t1, point1, curve2, t2, point2)); + 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); } - function addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, range2, recursion) { - recursion = (recursion || 0) + 1; + function addCurveIntersections(v1, v2, curve1, curve2, locations, include, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { if (recursion > 20) return; - range1 = range1 || [ 0, 1 ]; - range2 = range2 || [ 0, 1 ]; - var part1 = Curve.getPart(v1, range1[0], range1[1]), - part2 = Curve.getPart(v2, range2[0], range2[1]), - iteration = 0; - while (iteration++ < 20) { - var range, - intersects1 = clipFatLine(part1, part2, range = range2.slice()), - intersects2 = 0; - if (intersects1 === 0) - break; - if (intersects1 > 0) { - range2 = range; - part2 = Curve.getPart(v2, range2[0], range2[1]); - intersects2 = clipFatLine(part2, part1, range = range1.slice()); - if (intersects2 === 0) - break; - if (intersects1 > 0) { - range1 = range; - part1 = Curve.getPart(v1, range1[0], range1[1]); - } + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + tolerance = 0.00001, + hullEpsilon = 1e-9, + 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, + 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]), + tMinNew, tMaxNew, tDiff; + if (q0x === q3x && uMax - uMin <= hullEpsilon && recursion > 3) { + tMinNew = (tMax + tMin) / 2; + tMaxNew = tMinNew; + 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 false; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + tDiff = tMaxClip - tMinClip; + tMinNew = tMax * tMinClip + tMin * (1 - tMinClip); + tMaxNew = tMax * tMaxClip + tMin * (1 - tMaxClip); + } + if (oldTDiff > 0.8 && tDiff > 0.8) { + 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); + addCurveIntersections( + v2, parts[1], curve2, curve1, locations, include, + 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); + addCurveIntersections( + parts[1], v1, curve2, curve1, locations, include, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } - if (intersects1 < 0 || intersects2 < 0) { - if (range1[1] - range1[0] > range2[1] - range2[0]) { - var t = (range1[0] + range1[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ range1[0], t ], range2, recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ t, range1[1] ], range2, recursion); - break; - } else { - var t = (range2[0] + range2[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ range2[0], t ], recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ t, range2[1] ], recursion); - break; - } - } - if (Math.abs(range1[1] - range1[0]) <= 0.00001 && - Math.abs(range2[1] - range2[0]) <= 0.00001) { - var t1 = (range1[0] + range1[1]) / 2, - t2 = (range2[0] + range2[1]) / 2; - addLocation(locations, + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) < tolerance) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + if (reverse) { + addLocation(locations, include, + curve2, t2, Curve.evaluate(v2, t2, 0), + curve1, t1, Curve.evaluate(v1, t1, 0)); + } else { + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); - break; } + } else { + addCurveIntersections(v2, v1, curve2, curve1, locations, include, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); } } - function clipFatLine(v1, v2, range2) { - var p0x = v1[0], p0y = v1[1], p1x = v1[2], p1y = v1[3], - p2x = v1[4], p2y = v1[5], p3x = v1[6], p3y = v1[7], - q0x = v2[0], q0y = v2[1], q1x = v2[2], q1y = v2[3], - q2x = v2[4], q2y = v2[5], q3x = v2[6], q3y = v2[7], - getSignedDistance = Line.getSignedDistance, - d1 = getSignedDistance(p0x, p0y, p3x, p3y, p1x, p1y) || 0, - d2 = getSignedDistance(p0x, p0y, p3x, p3y, p2x, p2y) || 0, - factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, - dmin = factor * Math.min(0, d1, d2), - dmax = factor * Math.max(0, d1, d2), - dq0 = getSignedDistance(p0x, p0y, p3x, p3y, q0x, q0y), - dq1 = getSignedDistance(p0x, p0y, p3x, p3y, q1x, q1y), - dq2 = getSignedDistance(p0x, p0y, p3x, p3y, q2x, q2y), - dq3 = getSignedDistance(p0x, p0y, p3x, p3y, q3x, q3y); - if (dmin > Math.max(dq0, dq1, dq2, dq3) - || dmax < Math.min(dq0, dq1, dq2, dq3)) - return 0; - var hull = getConvexHull(dq0, dq1, dq2, dq3), - swap; - if (dq3 < dq0) { - swap = dmin; - dmin = dmax; - dmax = swap; - } - var tmaxdmin = -Infinity, - tmin = Infinity, - tmax = -Infinity; - for (var i = 0, l = hull.length; i < l; i++) { - var p1 = hull[i], - p2 = hull[(i + 1) % l]; - if (p2[1] < p1[1]) { - swap = p2; - p2 = p1; - p1 = swap; - } - var x1 = p1[0], - y1 = p1[1], - x2 = p2[0], - y2 = p2[1]; - var inv = (y2 - y1) / (x2 - x1); - if (dmin >= y1 && dmin <= y2) { - var ixdx = x1 + (dmin - y1) / inv; - if (ixdx < tmin) - tmin = ixdx; - if (ixdx > tmaxdmin) - tmaxdmin = ixdx; - } - if (dmax >= y1 && dmax <= y2) { - var ixdx = x1 + (dmax - y1) / inv; - if (ixdx > tmax) - tmax = ixdx; - if (ixdx < tmin) - tmin = 0; - } - } - if (tmin !== Infinity && tmax !== -Infinity) { - var min = Math.min(dmin, dmax), - max = Math.max(dmin, dmax); - if (dq3 > min && dq3 < max) - tmax = 1; - if (dq0 > min && dq0 < max) - tmin = 0; - if (tmaxdmin > tmax) - tmax = 1; - var v2tmin = range2[0], - tdiff = range2[1] - v2tmin; - range2[0] = v2tmin + tmin * tdiff; - range2[1] = v2tmin + tmax * tdiff; - if ((tdiff - (range2[1] - range2[0])) / tdiff >= 0.2) - return 1; - } - if (Curve.getBounds(v1).touches(Curve.getBounds(v2))) - return -1; - return 0; - } - function getConvexHull(dq0, dq1, dq2, dq3) { var p0 = [ 0, dq0 ], p1 = [ 1 / 3, dq1 ], @@ -5797,26 +5817,74 @@ new function() { p3 = [ 1, dq3 ], getSignedDistance = Line.getSignedDistance, dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); + dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), + flip = false, + hull; if (dist1 * dist2 < 0) { - return [ p0, p1, p3, p2 ]; - } - var pmax, cross; - if (Math.abs(dist1) > Math.abs(dist2)) { - pmax = p1; - cross = (dq3 - dq2 - (dq3 - dq0) / 3) - * (2 * (dq3 - dq2) - dq3 + dq1) / 3; + hull = [[p0, p1, p3], [p0, p2, p3]]; + flip = dist1 < 0; } else { - pmax = p2; - cross = (dq1 - dq0 + (dq0 - dq3) / 3) - * (-2 * (dq0 - dq1) + dq0 - dq2) / 3; + 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; } - return cross < 0 - ? [ p0, pmax, p3 ] - : [ p0, p1, p2, p3 ]; + return flip ? hull.reverse() : hull; } - function addCurveLineIntersections(v1, v2, curve1, curve2, locations) { + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + var tProxy, + tVal = null, + px, py, + qx, qy; + for (var i = 0, l = hullBottom.length - 1; i < l; i++) { + py = hullBottom[i][1]; + qy = hullBottom[i + 1][1]; + if (py < qy) { + tProxy = null; + } else if (qy <= dMax) { + px = hullBottom[i][0]; + qx = hullBottom[i + 1][0]; + tProxy = px + (dMax - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + if (hullTop[0][1] <= dMax) + tProxy = hullTop[0][0]; + for (var i = 0, l = hullTop.length - 1; i < l; i++) { + py = hullTop[i][1]; + qy = hullTop[i + 1][1]; + if (py >= dMin) { + tVal = tProxy; + } else if (py > qy) { + tVal = null; + } else if (qy >= dMin) { + px = hullTop[i][0]; + qx = hullTop[i + 1][0]; + tVal = px + (dMin - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + return tVal; + } + + function addCurveLineIntersections(v1, v2, curve1, curve2, locations, + include) { var flip = Curve.isLinear(v1), vc = flip ? v2 : v1, vl = flip ? v1 : v2, @@ -5846,43 +5914,37 @@ new function() { var tl = Curve.getParameterOf(rvl, x, 0), t1 = flip ? tl : tc, t2 = flip ? tc : tl; - addLocation(locations, + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); } } } - function addLineIntersection(v1, v2, curve1, curve2, locations) { + function addLineIntersection(v1, v2, curve1, curve2, locations, include) { var point = Line.intersect( v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]); - if (point) - addLocation(locations, curve1, null, point, curve2); + 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); + } } return { statics: { - getIntersections: function(v1, v2, curve1, curve2, locations) { + getIntersections: function(v1, v2, curve1, curve2, locations, include) { var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2), - c1p1 = curve1.getPoint1(), - c1p2 = curve1.getPoint2(), - c2p1 = curve2.getPoint1(), - c2p2 = curve2.getPoint2(), - tolerance = 0.00001; - if (c1p1.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 0, c1p1); - if (c1p1.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 1, c1p1); + linear2 = Curve.isLinear(v2); (linear1 && linear2 ? addLineIntersection : linear1 || linear2 ? addCurveLineIntersections - : addCurveIntersections)(v1, v2, curve1, curve2, locations); - if (c1p2.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 0, c1p2); - if (c1p2.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 1, c1p2); + : addCurveIntersections)( + v1, v2, curve1, curve2, locations, include, + 0, 1, 0, 1, 0, false, 0); return locations; } }}; @@ -5904,13 +5966,13 @@ var CurveLocation = Base.extend({ this._distance = _distance; }, - getSegment: function() { + getSegment: function(_preferFirst) { if (!this._segment) { var curve = this.getCurve(), parameter = this.getParameter(); if (parameter === 1) { this._segment = curve._segment2; - } else if (parameter === 0 || arguments[0]) { + } else if (parameter === 0 || _preferFirst) { this._segment = curve._segment1; } else if (parameter == null) { return null; @@ -5924,8 +5986,12 @@ var CurveLocation = Base.extend({ return this._segment; }, - getCurve: function() { - if (!this._curve || arguments[0]) { + setSegment: function(segment) { + this._segment = segment; + }, + + getCurve: function(_uncached) { + if (!this._curve || _uncached) { this._curve = this._segment1.getCurve(); if (this._curve.getParameterOf(this._point) == null) this._curve = this._segment2.getPrevious().getCurve(); @@ -5933,6 +5999,10 @@ var CurveLocation = Base.extend({ return this._curve; }, + setCurve: function(curve) { + this._curve = curve; + }, + getIntersection: function() { var intersection = this._intersection; if (!intersection && this._curve2) { @@ -5965,22 +6035,30 @@ var CurveLocation = Base.extend({ return parameter != null && curve && curve.getLength(0, parameter); }, - getParameter: function() { - if ((this._parameter == null || arguments[0]) && this._point) { - var curve = this.getCurve(arguments[0] && this._point); + getParameter: function(_uncached) { + if ((this._parameter == null || _uncached) && this._point) { + var curve = this.getCurve(_uncached && this._point); this._parameter = curve && curve.getParameterOf(this._point); } return this._parameter; }, - getPoint: function() { - if ((!this._point || arguments[0]) && this._parameter != null) { + setParameter: function(parameter) { + this._parameter = parameter; + }, + + getPoint: function(_uncached) { + if ((!this._point || _uncached) && this._parameter != null) { var curve = this.getCurve(); this._point = curve && curve.getPointAt(this._parameter, true); } return this._point; }, + setPoint: function(point) { + this._point = point; + }, + getTangent: function() { var parameter = this.getParameter(), curve = this.getCurve(); @@ -6007,6 +6085,17 @@ var CurveLocation = Base.extend({ return curve && curve.split(this.getParameter(true), true); }, + equals: function(loc) { + var isZero = Numerical.isZero; + return this === loc + || loc + && this._curve === loc._curve + && this._curve2 === loc._curve2 + && isZero(this._parameter - loc._parameter) + && isZero(this._parameter2 - loc._parameter2) + || false; + }, + toString: function() { var parts = [], point = this.getPoint(), @@ -6031,22 +6120,95 @@ var PathItem = Item.extend({ initialize: function PathItem() { }, - getIntersections: function(path) { - if (!this.getBounds().touches(path.getBounds())) + getIntersections: function(path, _expand) { + if (this === path) + path = null; + if (path && !this.getBounds().touches(path.getBounds())) return []; var locations = [], curves1 = this.getCurves(), - curves2 = path.getCurves(), - length2 = curves2.length, - values2 = []; + curves2 = path ? path.getCurves() : curves1, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = path ? path._matrix.orNullIfIdentity() : matrix1, + length1 = curves1.length, + length2 = path ? curves2.length : length1, + values2 = [], + MIN = 1e-11, + MAX = 1 - 1e-11; for (var i = 0; i < length2; i++) - values2[i] = curves2[i].getValues(); - for (var i = 0, l = curves1.length; i < l; i++) { + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { var curve1 = curves1[i], - values1 = curve1.getValues(); - for (var j = 0; j < length2; j++) - Curve.getIntersections(values1, values2[j], curve1, curves2[j], - locations); + 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 <= MAX) { + loc._parameter /= 2; + loc._parameter2 = 0.5 + loc._parameter2 / 2; + return true; + } + } + ); + } + } + 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 >= MIN && t <= MAX; + } + ); + } + } + var last = locations.length - 1; + for (var i = last; i >= 0; i--) { + var loc = locations[i], + next = loc._curve.getNext(), + next2 = loc._curve2.getNext(); + if (next && loc._parameter >= MAX) { + loc._parameter = 0; + loc._curve = next; + } + if (next2 && loc._parameter2 >= MAX) { + loc._parameter2 = 0; + loc._curve2 = next2; + } + } + + 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 === 0 ? last : i - 1])) { + locations.splice(i, 1); + last--; + } + } + } + if (_expand) { + for (var i = last; i >= 0; i--) + locations.push(locations[i].getIntersection()); + locations.sort(compare); } return locations; }, @@ -6056,22 +6218,22 @@ var PathItem = Item.extend({ var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), coords, relative = false, + previous, control, - current = new Point(); + current = new Point(), + start = new Point(); - function getCoord(index, coord, isCurrent) { - var val = parseFloat(coords[index]); + function getCoord(index, coord) { + var val = +coords[index]; if (relative) val += current[coord]; - if (isCurrent) - current[coord] = val; return val; } - function getPoint(index, isCurrent) { + function getPoint(index) { return new Point( - getCoord(index, 'x', isCurrent), - getCoord(index + 1, 'y', isCurrent) + getCoord(index, 'x'), + getCoord(index + 1, 'y') ); } @@ -6079,24 +6241,31 @@ var PathItem = Item.extend({ for (var i = 0, l = parts.length; i < l; i++) { var part = parts[i], - cmd = part[0], - lower = cmd.toLowerCase(); + command = part[0], + lower = command.toLowerCase(); coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); var length = coords && coords.length; - relative = cmd === lower; + relative = command === lower; + if (previous === 'z' && lower !== 'z') + this.moveTo(current = start); switch (lower) { case 'm': case 'l': + var move = lower === 'm'; + if (move && previous && previous !== 'z') + this.closePath(); for (var j = 0; j < length; j += 2) - this[j === 0 && lower === 'm' ? 'moveTo' : 'lineTo']( - getPoint(j, true)); + 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'; + var coord = lower === 'h' ? 'x' : 'y'; for (var j = 0; j < length; j++) { - getCoord(j, coord, true); + current[coord] = getCoord(j, coord); this.lineTo(current); } control = current; @@ -6106,37 +6275,49 @@ var PathItem = Item.extend({ this.cubicCurveTo( getPoint(j), control = getPoint(j + 2), - getPoint(j + 4, true)); + current = getPoint(j + 4)); } break; case 's': for (var j = 0; j < length; j += 4) { this.cubicCurveTo( - current.multiply(2).subtract(control), + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); + previous = lower; } break; case 'q': for (var j = 0; j < length; j += 4) { this.quadraticCurveTo( control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); } break; case 't': for (var j = 0; j < length; j += 2) { this.quadraticCurveTo( - control = current.multiply(2).subtract(control), - getPoint(j, true)); + 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[0], +coords[1]), + +coords[2], +coords[4], +coords[3]); + } break; case 'z': this.closePath(); break; } + previous = lower; } }, @@ -6145,9 +6326,9 @@ var PathItem = Item.extend({ }, _contains: function(point) { - var winding = this._getWinding(point); + var winding = this._getWinding(point, false, true); return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); - } + }, }); @@ -6165,11 +6346,20 @@ var Path = PathItem.extend({ ? typeof arg[0] === 'object' ? arg : arguments - : arg && (arg.point !== undefined && arg.size === undefined - || arg.x !== undefined) + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) ? arguments : null; - this.setSegments(segments || []); + 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); }, @@ -6178,30 +6368,34 @@ var Path = PathItem.extend({ }, clone: function(insert) { - var copy = this._clone(new Path({ - segments: this._segments, - insert: false - }), 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 copy; + return this._clone(copy, insert); }, _changed: function _changed(flags) { _changed.base.call(this, flags); if (flags & 4) { - delete this._length; - delete this._clockwise; + (this._compound ? this._parent : this)._currentPath = undefined; + this._length = this._clockwise = undefined; if (this._curves) { for (var i = 0, l = this._curves.length; i < l; i++) this._curves[i]._changed(5); } + this._monoCurves = undefined; } else if (flags & 8) { - delete this._bounds; + this._bounds = undefined; } }, + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + getSegments: function() { return this._segments; }, @@ -6210,8 +6404,9 @@ var Path = PathItem.extend({ var fullySelected = this.isFullySelected(); this._segments.length = 0; this._selectedSegmentState = 0; - delete this._curves; - this._add(Segment.readAll(segments)); + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); if (fullySelected) this.setFullySelected(true); }, @@ -6246,9 +6441,8 @@ var Path = PathItem.extend({ return curves[curves.length - 1]; }, - getPathData: function() { + getPathData: function(precision) { var segments = this._segments, - precision = arguments[0], f = Formatter.instance, parts = []; @@ -6310,7 +6504,7 @@ var Path = PathItem.extend({ return true; }, - _applyMatrix: function(matrix) { + _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); @@ -6411,7 +6605,7 @@ var Path = PathItem.extend({ return this.removeSegments(index, index + 1)[0] || null; }, - removeSegments: function(from, to) { + removeSegments: function(from, to, _includeCurves) { from = from || 0; to = Base.pick(to, this._segments.length); var segments = this._segments, @@ -6425,8 +6619,7 @@ var Path = PathItem.extend({ var segment = removed[i]; if (segment._selectionState) this._updateSelection(segment, segment._selectionState, 0); - delete segment._index; - delete segment._path; + segment._index = segment._path = null; } for (var i = from, l = segments.length; i < l; i++) segments[i]._index = i; @@ -6435,7 +6628,7 @@ var Path = PathItem.extend({ ? from - 1 : from, curves = curves.splice(index, amount); - if (arguments[2]) + if (_includeCurves) removed._curves = curves.slice(1); this._adjustCurves(index, index); } @@ -6448,7 +6641,7 @@ var Path = PathItem.extend({ isFullySelected: function() { var length = this._segments.length; return this._selected && length > 0 && this._selectedSegmentState - === length * 4; + === length * 7; }, setFullySelected: function(selected) { @@ -6466,10 +6659,10 @@ var Path = PathItem.extend({ _selectSegments: function(selected) { var length = this._segments.length; this._selectedSegmentState = selected - ? length * 4 : 0; + ? length * 7 : 0; for (var i = 0; i < length; i++) this._segments[i]._selectionState = selected - ? 4 : 0; + ? 7 : 0; }, _updateSelection: function(segment, oldState, newState) { @@ -6492,6 +6685,16 @@ var Path = PathItem.extend({ this.setSegments(segments); }, + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (curve.isLinear() && curve.getLength() === 0) + curve.remove(); + } + return this; + }, + simplify: function(tolerance) { if (this._segments.length > 2) { var fitter = new PathFitter(this, tolerance || 2.5); @@ -6509,13 +6712,14 @@ var Path = PathItem.extend({ index = arg.index; parameter = arg.parameter; } - if (parameter >= 1) { + var tolerance = 0.00001; + if (parameter >= 1 - tolerance) { index++; parameter--; } var curves = this.getCurves(); if (index >= 0 && index < curves.length) { - if (parameter > 0) { + if (parameter > tolerance) { curves[index++].divide(parameter, true); } var segs = this.removeSegments(index, this._segments.length, true), @@ -6554,7 +6758,7 @@ var Path = PathItem.extend({ segment._handleOut = handleIn; segment._index = i; } - delete this._curves; + this._curves = null; if (this._clockwise !== undefined) this._clockwise = !this._clockwise; }, @@ -6630,9 +6834,9 @@ var Path = PathItem.extend({ return null; }, - getLocationOf: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(); + getLocationOf: function(point) { + 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) @@ -6652,7 +6856,7 @@ var Path = PathItem.extend({ var start = length, curve = curves[i]; length += curve.getLength(); - if (length >= offset) { + if (length > offset) { return curve.getLocationAt(offset - start); } } @@ -6676,9 +6880,9 @@ var Path = PathItem.extend({ return loc && loc.getNormal(); }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + curves = this.getCurves(), minDist = Infinity, minLoc = null; for (var i = 0, l = curves.length; i < l; i++) { @@ -6691,17 +6895,11 @@ var Path = PathItem.extend({ return minLoc; }, - getNearestPoint: function(point) { - point = Point.read(arguments); + getNearestPoint: function(point) { + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); }, - getStyle: function() { - var parent = this._parent; - return (parent && parent._type === 'compound-path' - ? parent : this)._style; - }, - toShape: function(insert) { if (!this._closed) return null; @@ -6769,114 +6967,80 @@ var Path = PathItem.extend({ return null; }, - _getWinding: function(point) { - var closed = this._closed; - if (!closed && !this.hasFill() - || !this._getBounds('getRoughBounds')._containsPoint(point)) - return 0; - var curves = this.getCurves(), - segments = this._segments, - winding = 0, - roots1 = [], - roots2 = [], - last = (closed - ? curves[curves.length - 1] - : new Curve(segments[segments.length - 1]._point, - segments[0]._point)).getValues(), - previous = last; - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i].getValues(), - x = curve[0], - y = curve[1]; - if (!(x === curve[2] && y === curve[3] && x === curve[4] - && y === curve[5] && x === curve[6] && y === curve[7])) { - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - } - if (!closed) { - winding += Curve._getWinding(last, previous, point.x, point.y, - roots1, roots2); - } - return winding; - }, - _hitTest: function(point, options) { - var style = this.getStyle(), + var that = this, + style = this.getStyle(), segments = this._segments, closed = this._closed, - tolerance = options.tolerance, - radius = 0, join, cap, miterLimit, - that = this, - area, loc, res; - - if (options.stroke) { - radius = style.getStrokeWidth() / 2; + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hasStroke = options.stroke && style.hasStroke(), + hasFill = options.fill && style.hasFill(), + radius = hasStroke ? style.getStrokeWidth() / 2 + : hasFill ? 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'; } - radius += tolerance; } - function checkPoint(seg, pt, name) { - if (point.getDistance(pt) < tolerance) - return new HitResult(name, that, { segment: seg, point: pt }); + 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) { - var pt = seg._point; - return (ends || options.segments) && checkPoint(seg, pt, 'segment') + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') || (!ends && options.handles) && ( - checkPoint(seg, pt.add(seg._handleIn), 'handle-in') || - checkPoint(seg, pt.add(seg._handleOut), 'handle-out')); + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); } - function addAreaPoint(point) { - area.push(point); - } - - function getAreaCurve(index) { - var p1 = area[index], - p2 = area[(index + 1) % area.length]; - return [p1.x, p1.y, p1.x, p1.y, p2.x, p2.y, p2.x ,p2.y]; - } - - function isInArea(point) { - var length = area.length, - previous = getAreaCurve(length - 1), - roots1 = [], - roots2 = [], - winding = 0; - for (var i = 0; i < length; i++) { - var curve = getAreaCurve(i); - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - return !!winding; + function addToArea(point) { + area.add(point); } function checkSegmentStroke(segment) { if (join !== 'round' || cap !== 'round') { - area = []; + area = new Path({ internal: true, closed: true }); if (closed || segment._index > 0 && segment._index < segments.length - 1) { if (join !== 'round' && (segment._handleIn.isZero() || segment._handleOut.isZero())) - Path._addSquareJoin(segment, join, radius, miterLimit, - addAreaPoint, true); + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); } else if (cap !== 'round') { - Path._addSquareCap(segment, cap, radius, addAreaPoint, true); + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); } - if (area.length > 0) - return isInArea(point); } - return point.getDistance(segment._point) <= radius; + return isCloseEnough(segment._point, strokePadding); } if (options.ends && !options.segments && !closed) { @@ -6884,19 +7048,18 @@ var Path = PathItem.extend({ || checkSegmentPoints(segments[segments.length - 1], true)) return res; } else if (options.segments || options.handles) { - for (var i = 0, l = segments.length; i < l; i++) { + for (var i = 0, l = segments.length; i < l; i++) if (res = checkSegmentPoints(segments[i])) return res; - } } - if (radius > 0) { + if (radius != null) { loc = this.getNearestLocation(point); if (loc) { var parameter = loc.getParameter(); if (parameter === 0 || parameter === 1) { if (!checkSegmentStroke(loc.getSegment())) loc = null; - } else if (loc._distance > radius) { + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { loc = null; } } @@ -6911,10 +7074,13 @@ var Path = PathItem.extend({ } } } - return !loc && options.fill && this.hasFill() && this._contains(point) + return !loc && hasFill && this._contains(point) || loc && !hasStroke ? new HitResult('fill', this) : loc - ? new HitResult('stroke', this, { location: loc }) + ? new HitResult('stroke', this, { + location: loc, + point: loc.getPoint() + }) : null; } @@ -6942,24 +7108,19 @@ var Path = PathItem.extend({ var segment = segments[i]; segment._transformCoordinates(matrix, coords, false); var state = segment._selectionState, - selected = state & 4, pX = coords[0], pY = coords[1]; - if (selected || (state & 1)) + if (state & 1) drawHandle(2); - if (selected || (state & 2)) + if (state & 2) drawHandle(4); - ctx.save(); - ctx.beginPath(); - ctx.rect(pX - half, pY - half, size, size); - ctx.fill(); - if (!selected) { - ctx.beginPath(); - ctx.rect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; ctx.fillStyle = '#ffffff'; - ctx.fill(); + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; } - ctx.restore(); } } @@ -7016,14 +7177,14 @@ var Path = PathItem.extend({ for (var i = 0; i < length; i++) drawSegment(i); - if (path._closed && length > 1) + if (path._closed && length > 0) drawSegment(0); } return { _draw: function(ctx, param) { var clip = param.clip, - compound = param.compound; + compound = this._compound = param.compound; if (!compound) ctx.beginPath(); @@ -7038,10 +7199,15 @@ var Path = PathItem.extend({ return dashArray[((i % dashLength) + dashLength) % dashLength]; } - if (hasFill || hasStroke && !dashLength || compound || clip) + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || compound || clip){ drawSegments(ctx, this); - if (this._closed) - ctx.closePath(); + if (this._closed) + ctx.closePath(); + if (!compound) + this._currentPath = ctx.currentPath; + } if (!clip && !compound && (hasFill || hasStroke)) { this._setStyles(ctx); @@ -7077,8 +7243,7 @@ var Path = PathItem.extend({ ctx.beginPath(); drawSegments(ctx, this, matrix); ctx.stroke(); - drawHandles(ctx, this._segments, matrix, - this._project.options.handleSize || 4); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); } }; }, new function() { @@ -7177,16 +7342,17 @@ var Path = PathItem.extend({ }, new function() { function getCurrentSegment(that) { var segments = that._segments; - if (segments.length == 0) + if (segments.length === 0) throw new Error('Use a moveTo() command first'); return segments[segments.length - 1]; } return { moveTo: function() { - if (this._segments.length === 1) + var segments = this._segments; + if (segments.length === 1) this.removeSegment(0); - if (!this._segments.length) + if (!segments.length) this._add([ new Segment(Point.read(arguments)) ]); }, @@ -7235,37 +7401,86 @@ var Path = PathItem.extend({ arcTo: function() { var current = getCurrentSegment(this), from = current._point, - through, to = Point.read(arguments), - clockwise = Base.pick(Base.peek(arguments), true); + 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 { + } else if (Base.remain(arguments) <= 2) { through = to; to = Point.read(arguments); - } - 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), - center = l1.intersect(l2, true), - line = new Line(from, to), - throughSide = line.getSide(through); - if (!center) { - if (!throughSide) + } else { + var radius = Size.read(arguments); + if (radius.isZero()) return this.lineTo(to); - throw new Error('Cannot put an arc through the given points: ' - + [from, through, 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, + EPSILON = 1e-11, + 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) < EPSILON) + 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; } - var vector = from.subtract(center), - extent = vector.getDirectedAngle(to.subtract(center)), - centerSide = line.getSide(center); - if (centerSide == 0) { - extent = throughSide * Math.abs(extent); - } else if (throughSide == centerSide) { - extent -= 360 * (extent < 0 ? -1 : 1); + 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), @@ -7274,13 +7489,27 @@ var Path = PathItem.extend({ z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), segments = []; for (var i = 0; i <= count; i++) { - var pt = i < count ? center.add(vector) : to; - var out = i < count ? vector.rotate(90).multiply(z) : null; - if (i == 0) { + 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 { - segments.push( - new Segment(pt, vector.rotate(-90).multiply(z), out)); + 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); } @@ -7331,7 +7560,7 @@ var Path = PathItem.extend({ closePath: function() { var first = this.getFirstSegment(), last = this.getLastSegment(); - if (first._point.equals(last._point)) { + if (first !== last && first._point.equals(last._point)) { first.setHandleIn(last._handleIn); last.remove(); } @@ -7390,29 +7619,11 @@ statics: { }, getStrokeBounds: function(segments, closed, style, matrix) { - function getPenPadding(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)]; - } - if (!style.hasStroke()) return Path.getBounds(segments, closed, style, matrix); var length = segments.length - (closed ? 0 : 1), radius = style.getStrokeWidth() / 2, - padding = getPenPadding(radius, matrix), + padding = Path._getPenPadding(radius, matrix), bounds = Path.getBounds(segments, closed, style, matrix, padding), join = style.getStrokeJoin(), cap = style.getStrokeCap(), @@ -7424,25 +7635,27 @@ statics: { ? matrix._transformPoint(point, point) : point); } + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + function addJoin(segment, join) { - if (join === 'round' || !segment._handleIn.isZero() - && !segment._handleOut.isZero()) { - bounds = bounds.unite(joinBounds.setCenter(matrix - ? matrix._transformPoint(segment._point) : segment._point)); + var handleIn = segment._handleIn, + handleOut = segment._handleOut + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isColinear(handleOut)) { + addRound(segment); } else { - Path._addSquareJoin(segment, join, radius, miterLimit, add); + Path._addBevelJoin(segment, join, radius, miterLimit, add); } } function addCap(segment, cap) { - switch (cap) { - case 'round': - addJoin(segment, cap); - break; - case 'butt': - case 'square': + if (cap === 'round') { + addRound(segment); + } else { Path._addSquareCap(segment, cap, radius, add); - break; } } @@ -7450,14 +7663,32 @@ statics: { addJoin(segments[i], join); if (closed) { addJoin(segments[0], join); - } else { + } else if (length > 0) { addCap(segments[0], cap); addCap(segments[segments.length - 1], cap); } return bounds; }, - _addSquareJoin: function(segment, join, radius, miterLimit, addPoint, area) { + _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), @@ -7510,19 +7741,19 @@ statics: { x2 = -x1, y1 = x1, y2 = x2; - strokePadding = strokePadding / 2 || 0; - joinPadding = joinPadding / 2 || 0; 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 - padding, - xx = x + padding, - yn = y - padding, - yx = y + padding; + 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; @@ -7533,24 +7764,23 @@ statics: { }, getRoughBounds: function(segments, closed, style, matrix) { - var strokeWidth = style.getStrokeColor() ? style.getStrokeWidth() : 0, - joinWidth = strokeWidth; - if (strokeWidth === 0) { - strokeWidth = 0.00001; - } else { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { if (style.getStrokeJoin() === 'miter') - joinWidth = strokeWidth * style.getMiterLimit(); + joinRadius = strokeRadius * style.getMiterLimit(); if (style.getStrokeCap() === 'square') - joinWidth = Math.max(joinWidth, strokeWidth * Math.sqrt(2)); + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); } return Path.getHandleBounds(segments, closed, style, matrix, - strokeWidth, joinWidth); + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); } }}); Path.inject({ statics: new function() { - var kappa = Numerical.KAPPA, + var kappa = 0.5522847498307936, ellipseSegments = [ new Segment([-1, 0], [0, kappa ], [0, -kappa]), new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), @@ -7558,9 +7788,16 @@ Path.inject({ statics: new function() { 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 path = new Path(), - segments = new Array(4); + var segments = new Array(4); for (var i = 0; i < 4; i++) { var segment = ellipseSegments[i]; segments[i] = new Segment( @@ -7569,17 +7806,15 @@ Path.inject({ statics: new function() { segment._handleOut.multiply(radius) ); } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(args)); + return createPath(segments, true, args); } return { Line: function() { - return new Path( - Point.readNamed(arguments, 'from'), - Point.readNamed(arguments, 'to') - ).set(Base.getNamed(arguments)); + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); }, Circle: function() { @@ -7590,27 +7825,27 @@ Path.inject({ statics: new function() { Rectangle: function() { var rect = Rectangle.readNamed(arguments, 'rectangle'), - radius = Size.readNamed(arguments, 'radius', 0, 0, + radius = Size.readNamed(arguments, 'radius', 0, { readNull: true }), bl = rect.getBottomLeft(true), tl = rect.getTopLeft(true), tr = rect.getTopRight(true), - br = rect.getBottomRight(true); - path = new Path(); + br = rect.getBottomRight(true), + segments; if (!radius || radius.isZero()) { - path._add([ + 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; - path._add([ + 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]), @@ -7619,10 +7854,9 @@ Path.inject({ statics: new function() { 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]) - ]); + ]; } - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, RoundRectangle: '#Rectangle', @@ -7638,29 +7872,27 @@ Path.inject({ statics: new function() { var from = Point.readNamed(arguments, 'from'), through = Point.readNamed(arguments, 'through'), to = Point.readNamed(arguments, 'to'), - path = new Path(); + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); path.moveTo(from); path.arcTo(through, to); - return path.set(Base.getNamed(arguments)); + return path.set(props); }, RegularPolygon: function() { var center = Point.readNamed(arguments, 'center'), sides = Base.readNamed(arguments, 'sides'), radius = Base.readNamed(arguments, 'radius'), - path = new Path(), 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++) { + for (var i = 0; i < sides; i++) segments[i] = new Segment(center.add( vector.rotate((i + offset) * step))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, Star: function() { @@ -7668,17 +7900,13 @@ Path.inject({ statics: new function() { points = Base.readNamed(arguments, 'points') * 2, radius1 = Base.readNamed(arguments, 'radius1'), radius2 = Base.readNamed(arguments, 'radius2'), - path = new Path(), 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))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + 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); } }; }}); @@ -7692,8 +7920,19 @@ var CompoundPath = PathItem.extend({ initialize: function CompoundPath(arg) { this._children = []; this._namedChildren = {}; - if (!this._initialize(arg)) - this.addChildren(Array.isArray(arg) ? arg : arguments); + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & (2 | 4)) + this._currentPath = undefined; }, insertChildren: function insertChildren(index, items, _preserve) { @@ -7741,7 +7980,7 @@ var CompoundPath = PathItem.extend({ var children = this._children, curves = []; for (var i = 0, l = children.length; i < l; i++) - curves = curves.concat(children[i].getCurves()); + curves.push.apply(curves, children[i].getCurves()); return curves; }, @@ -7763,36 +8002,18 @@ var CompoundPath = PathItem.extend({ return area; }, - getPathData: function() { + getPathData: function(precision) { var children = this._children, paths = []; for (var i = 0, l = children.length; i < l; i++) - paths.push(children[i].getPathData(arguments[0])); + paths.push(children[i].getPathData(precision)); return paths.join(' '); }, - _getWinding: function(point) { - var children = this._children, - winding = 0; - for (var i = 0, l = children.length; i < l; i++) - winding += children[i]._getWinding(point); - return winding; - }, - - _hitTest : function _hitTest(point, options) { - var res = _hitTest.base.call(this, point, - new Base(options, { fill: false })); - if (!res) { - if (options.compoundChildren) { - var children = this._children; - for (var i = children.length - 1; i >= 0 && !res; i--) - res = children[i]._hitTest(point, options); - } else if (options.fill && this.hasFill() - && this._contains(point)) { - res = new HitResult('fill', this); - } - } - return res; + _getChildHitTestOptions: function(options) { + return options.type === 'path' + ? options + : new Base(options, { fill: false }); }, _draw: function(ctx, param) { @@ -7800,10 +8021,15 @@ var CompoundPath = PathItem.extend({ if (children.length === 0) return; - ctx.beginPath(); - param = param.extend({ compound: true }); - for (var i = 0, l = children.length; i < l; i++) - children[i].draw(ctx, param); + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + ctx.beginPath(); + param = param.extend({ compound: true }); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param); + this._currentPath = ctx.currentPath; + } if (!param.clip) { this._setStyles(ctx); @@ -7817,26 +8043,31 @@ var CompoundPath = PathItem.extend({ } } }, new function() { - function getCurrentPath(that) { - if (!that._children.length) + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) throw new Error('Use a moveTo() command first'); - return that._children[that._children.length - 1]; + return children[children.length - 1]; } var fields = { moveTo: function() { - var path = new Path(); - this.addChild(path); + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current : new Path(); + if (path !== current) + this.addChild(path); path.moveTo.apply(path, arguments); }, moveBy: function() { - this.moveTo(getCurrentPath(this).getLastSegment()._point.add( - Point.read(arguments))); + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); }, closePath: function() { - getCurrentPath(this).closePath(); + getCurrentPath(this, true).closePath(); } }; @@ -7844,7 +8075,7 @@ var CompoundPath = PathItem.extend({ 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], function(key) { fields[key] = function() { - var path = getCurrentPath(this); + var path = getCurrentPath(this, true); path[key].apply(path, arguments); }; } @@ -7853,6 +8084,433 @@ var CompoundPath = PathItem.extend({ return fields; }); +PathItem.inject(new function() { + function reorientPath(path) { + path = path.clone(false).reduce().transform(null, true); + if (path instanceof CompoundPath) { + var children = path.removeChildren(), + length = children.length, + bounds = new Array(length), + counters = new Array(length), + clockwise; + children.sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + path.addChildren(children); + clockwise = children[0].isClockwise(); + for (var i = 0; i < length; i++) { + bounds[i] = children[i].getBounds(); + counters[i] = 0; + } + for (var i = 0; i < length; i++) { + for (var j = 1; j < length; j++) { + if (i !== j && bounds[i].intersects(bounds[j])) + counters[j]++; + } + if (i > 0 && counters[i] % 2 === 0) + children[i].setClockwise(clockwise); + } + } + return path; + } + + function computeBoolean(path1, path2, operator, subtract) { + var _path1 = reorientPath(path1); + _path2 = path2 && path1 !== path2 && reorientPath(path2); + if (!_path1.isClockwise()) + _path1.reverse(); + if (_path2 && !(subtract ^ _path2.isClockwise())) + _path2.reverse(); + splitPath(_path1.getIntersections(_path2, true)); + + var chain = [], + windings = [], + lengths = [], + 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]); + 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 = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding != null) + continue; + chain.length = windings.length = lengths.length = 0; + var totalLength = 0, + startSeg = segment; + do { + chain.push(segment); + lengths.push(totalLength += segment.getCurve().getLength()); + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== startSeg); + for (var j = 0; j < 3; j++) { + var length = totalLength * Math.random(), + amount = lengths.length; + k = 0; + do { + if (lengths[k] >= length) { + if (k > 0) + length -= lengths[k - 1]; + break; + } + } while (++k < amount); + var curve = chain[k].getCurve(), + point = curve.getPointAt(length), + hor = curve.isHorizontal(), + path = curve._path; + if (path._parent instanceof CompoundPath) + path = path._parent; + windings[j] = subtract && _path2 + && (path === _path1 && _path2._getWinding(point, hor) + || path === _path2 && !_path1._getWinding(point, hor)) + ? 0 + : getWinding(point, monoCurves, hor); + } + windings.sort(); + var winding = windings[1]; + for (var j = chain.length - 1; j >= 0; j--) + chain[j]._winding = winding; + } + var result = new CompoundPath(); + result.addChildren(tracePaths(segments, operator), true); + _path1.remove(); + if (_path2) + _path2.remove(); + return result.reduce(); + } + + function splitPath(intersections) { + var TOLERANCE = 0.00001, + linearSegments; + + function resetLinear() { + for (var i = 0, l = linearSegments.length; i < l; i++) { + var segment = linearSegments[i]; + segment._handleOut.set(0, 0); + segment._handleIn.set(0, 0); + } + } + + for (var i = intersections.length - 1, curve, prevLoc; i >= 0; i--) { + var loc = intersections[i], + t = loc._parameter; + if (prevLoc && prevLoc._curve === loc._curve + && prevLoc._parameter > 0) { + t /= prevLoc._parameter; + } else { + if (linearSegments) + resetLinear(); + curve = loc._curve; + linearSegments = curve.isLinear() && []; + } + var newCurve, + segment; + if (newCurve = curve.divide(t, true, true)) { + segment = newCurve._segment1; + curve = newCurve.getPrevious(); + } else { + segment = t < TOLERANCE + ? curve._segment1 + : t > 1 - TOLERANCE + ? curve._segment2 + : curve.getPartLength(0, t) < curve.getPartLength(t, 1) + ? curve._segment1 + : curve._segment2; + } + segment._intersection = loc.getIntersection(); + loc._segment = segment; + if (linearSegments) + linearSegments.push(segment); + prevLoc = loc; + } + if (linearSegments) + resetLinear(); + } + + function getWinding(point, curves, horizontal, testContains) { + var TOLERANCE = 0.00001, + x = point.x, + y = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs, + MAX = 1 - TOLERANCE; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = y - TOLERANCE, + yAfter = y + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, x, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y0 = Curve.evaluate(values, roots[j], 0).y; + if (y0 < yBefore && y0 > yTop) { + yTop = y0; + } else if (y0 > yAfter && y0 < yBottom) { + yBottom = y0; + } + } + } + } + yTop = (yTop + y) / 2; + yBottom = (yBottom + y) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(x, yTop), curves); + if (yBottom < Infinity) + windRight = getWinding(new Point(x, yBottom), curves); + } else { + var xBefore = x - TOLERANCE, + xAfter = x + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding, + next = curve.next; + if (winding && (winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, + !next.winding && next.values[1] === y ? 1 : MAX) === 1){ + var t = roots[0], + x0 = Curve.evaluate(values, t, 0).x, + slope = Curve.evaluate(values, t, 1).y; + if (abs(slope) < TOLERANCE && !Curve.isLinear(values) + || t < TOLERANCE && slope * Curve.evaluate( + curve.previous.values, t, 1).y < 0) { + if (testContains && x0 >= xBefore && x0 <= xAfter) { + ++windLeft; + ++windRight; + } + } else if (x0 <= xBefore) { + windLeft += winding; + } else if (x0 >= xAfter) { + windRight += winding; + } + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function tracePaths(segments, operator, selfOp) { + operator = operator || function() { + return true; + }; + var paths = [], + ZERO = 1e-3, + ONE = 1 - 1e-3; + for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) { + seg = startSeg = segments[i]; + if (seg._visited || !operator(seg._winding)) + 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) { + var c1 = seg.getCurve(); + if (dir > 0) + c1 = c1.getPrevious(); + var t1 = c1.getTangentAt(dir < 1 ? ZERO : ONE, true), + c4 = interSeg.getCurve(), + c3 = c4.getPrevious(), + t3 = c3.getTangentAt(ONE, true), + t4 = c4.getTangentAt(ZERO, true), + w3 = t1.cross(t3), + w4 = t1.cross(t4); + if (selfOp) { + seg._visited = interSeg._visited; + seg = interSeg; + dir = 1; + } else 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; + } + handleOut = dir > 0 ? seg._handleOut : seg._handleIn; + } + path.add(new Segment(seg._point, added && handleIn, handleOut)); + added = true; + 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); + } + if (path._segments.length > + (path._closed ? path.isPolygon() ? 2 : 0 : 1)) + paths.push(path); + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1 || w === 0; + }, false); + }, + + intersect: function(path) { + return computeBoolean(this, path, function(w) { + return w === 2; + }, false); + }, + + subtract: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1; + }, true); + }, + + exclude: function(path) { + return new Group([this.subtract(path), path.subtract(this)]); + }, + divide: function(path) { + return new Group([this.subtract(path), this.intersect(path)]); + } + }; +}); + +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.isLinear(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + TOLERANCE = 0.00001, + roots = []; + var count = Numerical.solveQuadratic(a, b, c, roots, TOLERANCE, + 1 - TOLERANCE); + if (count === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (count > 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]); + } + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + return monoCurves; + } +}); + +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; + } +}); + var PathFlattener = Base.extend({ initialize: function(path) { this.curves = []; @@ -8136,186 +8794,11 @@ var PathFitter = Base.extend({ } }); -PathItem.inject(new function() { - - function splitPath(intersections, collectOthers) { - intersections.sort(function(loc1, loc2) { - var path1 = loc1.getPath(), - path2 = loc2.getPath(); - return path1 === path2 - ? (loc1.getIndex() + loc1.getParameter()) - - (loc2.getIndex() + loc2.getParameter()) - : path1._id - path2._id; - }); - var others = collectOthers && []; - for (var i = intersections.length - 1; i >= 0; i--) { - var loc = intersections[i], - other = loc.getIntersection(), - curve = loc.divide(), - segment = curve && curve.getSegment1() || loc.getSegment(); - if (others) - others.push(other); - segment._intersection = other; - loc._segment = segment; - } - return others; - } - - function reorientPath(path) { - if (path instanceof CompoundPath) { - var children = path.removeChildren(), - length = children.length, - bounds = new Array(length), - counters = new Array(length), - clockwise; - children.sort(function(a, b){ - var b1 = a.getBounds(), b2 = b.getBounds(); - return b1._width * b1._height < b2._width * b2._height; - }); - path.addChildren(children); - clockwise = children[0].isClockwise(); - for (var i = 0; i < length; i++) { - bounds[i] = children[i].getBounds(); - counters[i] = 0; - } - for (var i = 0; i < length; i++) { - for (var j = 1; j < length; j++) { - if (i !== j && bounds[i].contains(bounds[j])) - counters[j]++; - } - if (i > 0 && counters[i] % 2 === 0) - children[i].setClockwise(clockwise); - } - } - return path; - } - - function computeBoolean(path1, path2, operator, subtract) { - path1 = reorientPath(path1.clone(false)); - path2 = reorientPath(path2.clone(false)); - var path1Clockwise = path1.isClockwise(), - path2Clockwise = path2.isClockwise(), - intersections = path1.getIntersections(path2); - splitPath(splitPath(intersections, true)); - if (!path1Clockwise) - path1.reverse(); - if (!(subtract ^ path2Clockwise)) - path2.reverse(); - path1Clockwise = true; - path2Clockwise = !subtract; - var paths = [] - .concat(path1._children || [path1]) - .concat(path2._children || [path2]), - segments = [], - result = new CompoundPath(); - for (var i = 0, l = paths.length; i < l; i++) { - var path = paths[i], - parent = path._parent, - clockwise = path.isClockwise(), - segs = path._segments; - path = parent instanceof CompoundPath ? parent : path; - for (var j = segs.length - 1; j >= 0; j--) { - var segment = segs[j], - midPoint = segment.getCurve().getPoint(0.5), - insidePath1 = path !== path1 && path1.contains(midPoint) - && (clockwise === path1Clockwise || subtract - || !testOnCurve(path1, midPoint)), - insidePath2 = path !== path2 && path2.contains(midPoint) - && (clockwise === path2Clockwise - || !testOnCurve(path2, midPoint)); - if (operator(path === path1, insidePath1, insidePath2)) { - segment._invalid = true; - } else { - segments.push(segment); - } - } - } - for (var i = 0, l = segments.length; i < l; i++) { - var segment = segments[i]; - if (segment._visited) - continue; - var path = new Path(), - loc = segment._intersection, - intersection = loc && loc.getSegment(true); - if (segment.getPrevious()._invalid) - segment.setHandleIn(intersection - ? intersection._handleIn - : new Point(0, 0)); - do { - segment._visited = true; - if (segment._invalid && segment._intersection) { - var inter = segment._intersection.getSegment(true); - path.add(new Segment(segment._point, segment._handleIn, - inter._handleOut)); - inter._visited = true; - segment = inter; - } else { - path.add(segment.clone()); - } - segment = segment.getNext(); - } while (segment && !segment._visited && segment !== intersection); - var amount = path._segments.length; - if (amount > 1 && (amount > 2 || !path.isPolygon())) { - path.setClosed(true); - result.addChild(path, true); - } else { - path.remove(); - } - } - path1.remove(); - path2.remove(); - return result.reduce(); - } - - function testOnCurve(path, point) { - var curves = path.getCurves(), - bounds = path.getBounds(); - if (bounds.contains(point)) { - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i]; - if (curve.getBounds().contains(point) - && curve.getParameterOf(point)) - return true; - } - } - return false; - } - - return { - unite: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isInPath1 || isInPath2; - }); - }, - - intersect: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return !(isInPath1 || isInPath2); - }); - }, - - subtract: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isPath1 && isInPath2 || !isPath1 && !isInPath1; - }, true); - }, - - exclude: function(path) { - return new Group([this.subtract(path), path.subtract(this)]); - }, - - divide: function(path) { - return new Group([this.subtract(path), this.intersect(path)]); - } - }; -}); - var TextItem = Item.extend({ _class: 'TextItem', _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, _serializeFields: { content: null }, @@ -8367,7 +8850,7 @@ var PointText = TextItem.extend({ }, clone: function(insert) { - return this._clone(new PointText({ insert: false }), insert); + return this._clone(new PointText(Item.NO_INSERT), insert); }, getPoint: function() { @@ -8375,8 +8858,8 @@ var PointText = TextItem.extend({ return new LinkedPoint(point.x, point.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.translate(point.subtract(this._matrix.getTranslation())); }, @@ -8457,7 +8940,7 @@ var Color = Base.extend(new function() { } else if (match = string.match(/^rgba?\((.*)\)$/)) { components = match[1].split(','); for (var i = 0, l = components.length; i < l; i++) { - var value = parseFloat(components[i]); + var value = +components[i]; components[i] = i < 3 ? value / 255 : value; } } else { @@ -8504,8 +8987,8 @@ var Color = Base.extend(new function() { }, 'hsb-rgb': function(h, s, b) { - var h = (h / 60) % 6, - i = Math.floor(h), + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), f = h - i, i = hsbIndices[i], v = [ @@ -8534,7 +9017,7 @@ var Color = Base.extend(new function() { }, 'hsl-rgb': function(h, s, l) { - h /= 360; + h = (((h / 360) % 1) + 1) % 1; if (s === 0) return [l, l, l]; var t3s = [ h + 1 / 3, h, h - 1 / 3 ], @@ -8590,9 +9073,8 @@ var Color = Base.extend(new function() { parser = componentParsers[type][index] = name === 'gradient' ? function(value) { var current = this._components[0]; - value = Gradient.read( - Array.isArray(value) ? value : arguments, - 0, 0, { readNull: true }); + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); if (current !== value) { if (current) current._removeOwner(this); @@ -8601,22 +9083,16 @@ var Color = Base.extend(new function() { } return value; } - : name === 'hue' - ? function(value) { - return isNaN(value) ? 0 - : ((value % 360) + 360) % 360; + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); } - : type === 'gradient' - ? function() { - return Point.read(arguments, 0, 0, { - readNull: name === 'highlight', - clone: true - }); - } - : function(value) { - return isNaN(value) ? 0 - : Math.min(Math.max(value, 0), 1); - }; + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; this['get' + part] = function() { return this._type === type @@ -8647,7 +9123,6 @@ var Color = Base.extend(new function() { var slice = Array.prototype.slice, args = arguments, read = 0, - parse = true, type, components, alpha, @@ -8671,7 +9146,6 @@ var Color = Base.extend(new function() { } } if (!components) { - parse = !(this.__options && this.__options.dontParse); values = argType === 'number' ? args : argType === 'object' && arg.length != null @@ -8724,7 +9198,7 @@ var Color = Base.extend(new function() { ? 'gray' : 'rgb'; var properties = types[type]; - parsers = parse && componentParsers[type]; + parsers = componentParsers[type]; this._components = components = []; for (var i = 0, l = properties.length; i < l; i++) { var value = arg[properties[i]]; @@ -8735,8 +9209,7 @@ var Color = Base.extend(new function() { radial: arg.radial }; } - if (parse) - value = parsers[i].call(this, value); + value = parsers[i].call(this, value); if (value != null) components[i] = value; } @@ -8753,9 +9226,7 @@ var Color = Base.extend(new function() { this._components = components = []; var parsers = componentParsers[this._type]; for (var i = 0, l = parsers.length; i < l; i++) { - var value = values && values[i]; - if (parse) - value = parsers[i].call(this, value); + var value = parsers[i].call(this, values && values[i]); if (value != null) components[i] = value; } @@ -8790,7 +9261,7 @@ var Color = Base.extend(new function() { ? converter.apply(this, this._components) : converters['rgb-' + type].apply(this, converters[this._type + '-rgb'].apply(this, - this._components)); + this._components)); }, convert: function(type) { @@ -8828,12 +9299,13 @@ var Color = Base.extend(new function() { }, equals: function(color) { - if (Base.isPlainValue(color)) - color = Color.read(arguments); - return color === this || color && this._class === color._class - && this._type === color._type - && this._alpha === color._alpha - && Base.equals(this._components, color._components) + var col = Base.isPlainValue(color) + ? 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; }, @@ -8856,13 +9328,16 @@ var Color = Base.extend(new function() { 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 = [ - Math.round(components[0] * 255), - Math.round(components[1] * 255), - Math.round(components[2] * 255) + convert(components[0]), + convert(components[1]), + convert(components[2]) ]; if (alpha < 1) - components.push(alpha); + components.push(alpha < 0 ? 0 : alpha); return hex ? '#' + ((1 << 24) + (components[0] << 16) + (components[1] << 8) @@ -8926,46 +9401,32 @@ var Color = Base.extend(new function() { } }); }, new function() { - function clamp(value, hue) { - return value < 0 - ? 0 - : hue && value > 360 - ? 360 - : !hue && value > 1 - ? 1 - : value; - } - var operators = { - add: function(a, b, hue) { - return clamp(a + b, hue); + add: function(a, b) { + return a + b; }, - subtract: function(a, b, hue) { - return clamp(a - b, hue); + subtract: function(a, b) { + return a - b; }, - multiply: function(a, b, hue) { - return clamp(a * b, hue); + multiply: function(a, b) { + return a * b; }, - divide: function(a, b, hue) { - return clamp(a / b, hue); + divide: function(a, b) { + return a / b; } }; return Base.each(operators, function(operator, name) { - var options = { dontParse: /^(multiply|divide)$/.test(name) }; - this[name] = function(color) { - color = Color.read(arguments, 0, 0, options); + color = Color.read(arguments); var type = this._type, - properties = this._properties, components1 = this._components, components2 = color._convert(type); for (var i = 0, l = components1.length; i < l; i++) - components2[i] = operator(components1[i], components2[i], - properties[i] === 'hue'); + components2[i] = operator(components1[i], components2[i]); return new Color(type, components2, this._alpha != null ? operator(this._alpha, color.getAlpha()) @@ -9030,7 +9491,7 @@ var Gradient = Base.extend({ if (index != -1) { this._owners.splice(index, 1); if (this._owners.length === 0) - delete this._owners; + this._owners = undefined; } }, @@ -9048,7 +9509,7 @@ var Gradient = Base.extend({ setStops: function(stops) { if (this.stops) { for (var i = 0, l = this._stops.length; i < l; i++) - delete this._stops[i]._owner; + this._stops[i]._owner = undefined; } if (stops.length < 2) throw new Error( @@ -9167,8 +9628,10 @@ var Style = Base.extend(new function() { shadowBlur: 0, shadowOffset: new Point(), selectedColor: undefined, - font: 'sans-serif', + fontFamily: 'sans-serif', + fontWeight: 'normal', fontSize: 12, + font: 'sans-serif', leading: null, justification: 'left' }; @@ -9178,8 +9641,10 @@ var Style = Base.extend(new function() { strokeCap: 25, strokeJoin: 25, miterLimit: 25, - font: 5, + fontFamily: 5, + fontWeight: 5, fontSize: 5, + font: 5, leading: 5, justification: 5 }; @@ -9210,7 +9675,7 @@ var Style = Base.extend(new function() { if (old != value) { if (isColor) { if (old) - delete old._owner; + old._owner = undefined; if (value && value.constructor === Color) { if (value._owner) value = value.clone(); @@ -9224,11 +9689,11 @@ var Style = Base.extend(new function() { } }; - fields[get] = function() { + fields[get] = function(_dontMerge) { var value, children = this._item && this._item._children; - if (!children || children.length === 0 || arguments[0] - || this._item._type === 'compound-path') { + if (!children || children.length === 0 || _dontMerge + || this._item instanceof CompoundPath) { var value = this._values[key]; if (value === undefined) { value = this._defaults[key]; @@ -9236,8 +9701,8 @@ var Style = Base.extend(new function() { value = value.clone(); this._values[key] = value; } else if (isColor && !(value && value.constructor === Color)) { - this._values[key] = value = Color.read( - [value], 0, 0, { readNull: true, clone: true }); + this._values[key] = value = Color.read([value], 0, + { readNull: true, clone: true }); if (value) value._owner = this._item; } @@ -9309,14 +9774,20 @@ var Style = Base.extend(new function() { return !!this.getShadowColor() && this.getShadowBlur() > 0; }, + getFontStyle: function() { + var size = this.getFontSize(); + return this.getFontWeight() + + ' ' + size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + + setFont: '#setFontFamily', + getLeading: function getLeading() { var leading = getLeading.base.call(this); return leading != null ? leading : this.getFontSize() * 1.2; - }, - - getFontStyle: function() { - var size = this.getFontSize(); - return size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + this.getFont(); } }); @@ -9347,6 +9818,23 @@ var DomElement = new function() { return res; } + 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 { create: function(nodes, parent) { var isArray = Array.isArray(nodes), @@ -9488,13 +9976,17 @@ var DomElement = new function() { this.getBounds(el, true)); }, - getPrefixValue: function(el, name) { - var value = el[name], - prefixes = ['webkit', 'moz', 'ms', 'o'], - suffix = name[0].toUpperCase() + name.substring(1); - for (var i = 0; i < 4 && value == null; i++) - value = el[prefixes[i] + suffix]; - return value; + 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); + } } }; }; @@ -9502,25 +9994,19 @@ var DomElement = new function() { var DomEvent = { add: function(el, events) { for (var type in events) { - var func = events[type]; - if (el.addEventListener) { - el.addEventListener(type, func, false); - } else if (el.attachEvent) { - el.attachEvent('on' + type, func.bound = function() { - func.call(el, window.event); - }); - } + 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]; - if (el.removeEventListener) { - el.removeEventListener(type, func, false); - } else if (el.detachEvent) { - el.detachEvent('on' + type, func.bound); - } + 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); } }, @@ -9540,36 +10026,23 @@ var DomEvent = { 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))); }, - preventDefault: function(event) { - if (event.preventDefault) { - event.preventDefault(); - } else { - event.returnValue = false; - } - }, - - stopPropagation: function(event) { - if (event.stopPropagation) { - event.stopPropagation(); - } else { - event.cancelBubble = true; - } - }, - stop: function(event) { - DomEvent.stopPropagation(event); - DomEvent.preventDefault(event); + event.stopPropagation(); + event.preventDefault(); } }; DomEvent.requestAnimationFrame = new function() { - var nativeRequest = DomElement.getPrefixValue(window, - 'requestAnimationFrame'), + var nativeRequest = DomElement.getPrefixed(window, 'requestAnimationFrame'), requested = false, callbacks = [], focused = true, @@ -9625,16 +10098,27 @@ var View = Base.extend(Callback, { this._project = paper.project; 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._viewHandlers); + 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)' + }); if (PaperScope.hasAttribute(element, 'resize')) { var offset = DomElement.getOffset(element, true), that = this; size = DomElement.getViewportBounds(element) .getSize().subtract(offset); - this._windowHandlers = { + this._windowEvents = { resize: function() { if (!DomElement.isInvisible(element)) offset = DomElement.getOffset(element, true); @@ -9642,7 +10126,7 @@ var View = Base.extend(Callback, { .getSize().subtract(offset)); } }; - DomEvent.add(window, this._windowHandlers); + DomEvent.add(window, this._windowEvents); } else { size = DomElement.getSize(element); if (size.isNaN() || size.isZero()) @@ -9675,16 +10159,17 @@ var View = Base.extend(Callback, { remove: function() { if (!this._project) return false; - if (View._focused == this) + 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._viewHandlers); - DomEvent.remove(window, this._windowHandlers); + DomEvent.remove(this._element, this._viewEvents); + DomEvent.remove(window, this._windowEvents); this._element = this._project = null; this.detach('frame'); + this._animate = false; this._frameItems = {}; return true; }, @@ -9692,13 +10177,11 @@ var View = Base.extend(Callback, { _events: { onFrame: { install: function() { - this._animate = true; - if (!this._requested) - this._requestFrame(); + this.play(); }, uninstall: function() { - this._animate = false; + this.pause(); } }, @@ -9735,7 +10218,7 @@ var View = Base.extend(Callback, { if (this._stats) this._stats.update(); this._handlingFrame = false; - this.draw(true); + this.update(); }, _animateItem: function(item, animate) { @@ -9766,40 +10249,48 @@ var View = Base.extend(Callback, { } }, - _redraw: function() { - this._project._needsRedraw = true; + _update: function() { + this._project._needsUpdate = true; if (this._handlingFrame) return; if (this._animate) { this._handleFrame(); } else { - this.draw(); + this.update(); } }, _changed: function(flags) { if (flags & 1) - this._project._needsRedraw = true; + this._project._needsUpdate = true; }, _transform: function(matrix) { this._matrix.concatenate(matrix); this._bounds = null; - this._redraw(); + 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(size) { - size = Size.read(arguments); - var delta = size.subtract(this._viewSize); + setViewSize: function() { + var size = Size.read(arguments), + delta = size.subtract(this._viewSize); if (delta.isZero()) return; this._viewSize.set(size.width, size.height); @@ -9809,7 +10300,7 @@ var View = Base.extend(Callback, { size: size, delta: delta }); - this._redraw(); + this._update(); }, _setViewSize: function(size) { @@ -9826,11 +10317,11 @@ var View = Base.extend(Callback, { }, getSize: function() { - return this.getBounds().getSize(arguments[0]); + return this.getBounds().getSize(); }, getCenter: function() { - return this.getBounds().getCenter(arguments[0]); + return this.getBounds().getCenter(); }, setCenter: function(center) { @@ -9856,13 +10347,27 @@ var View = Base.extend(Callback, { 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: { @@ -9903,80 +10408,107 @@ var View = Base.extend(Callback, { } } - function mousedown(event) { + 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; - if (view._onMouseDown) - view._onMouseDown(event, point); - if (tool = view._scope._tool) - tool._onHandleEvent('mousedown', point, event); - view.draw(true); - } + view._handleEvent('mousedown', point, event); + if (tool = view._scope.tool) + tool._handleEvent('mousedown', point, event); + view.update(); + }; - function mousemove(event) { - var view; + docEvents[mousemove] = function(event) { + var view = View._focused; if (!dragging) { - view = getView(event); - if (view) { - prevFocus = View._focused; - View._focused = tempFocus = view; - } else if (tempFocus && tempFocus == View._focused) { - View._focused = prevFocus; + 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 = view || View._focused)) - return; - var point = event && viewToProject(view, event); - if (view._onMouseMove) - view._onMouseMove(event, point); - if (tool = view._scope._tool) { - if (tool._onHandleEvent(dragging && tool.responds('mousedrag') - ? 'mousedrag' : 'mousemove', point, event)) - DomEvent.stop(event); + if (view) { + var point = viewToProject(view, event); + if (dragging || view.getBounds().contains(point)) + tool = handleMouseMove(view, point, event); } - view.draw(true); - } + }; - function mouseup(event) { + docEvents[mouseup] = function(event) { var view = View._focused; if (!view || !dragging) return; var point = viewToProject(view, event); curPoint = null; dragging = false; - if (view._onMouseUp) - view._onMouseUp(event, point); - if (tool && tool._onHandleEvent('mouseup', point, event)) - DomEvent.stop(event); - view.draw(true); - } + view._handleEvent('mouseup', point, event); + if (tool) + tool._handleEvent('mouseup', point, event); + view.update(); + }; - function selectstart(event) { - if (dragging) - DomEvent.stop(event); - } - - DomEvent.add(document, { - mousemove: mousemove, - mouseup: mouseup, - touchmove: mousemove, - touchend: mouseup, - selectstart: selectstart, - scroll: updateFocus - }); + DomEvent.add(document, docEvents); DomEvent.add(window, { load: updateFocus }); return { - _viewHandlers: { - mousedown: mousedown, - touchstart: mousedown, - selectstart: selectstart - }, + _viewEvents: viewEvents, + + _handleEvent: function() {}, statics: { updateFocus: updateFocus @@ -9998,12 +10530,12 @@ var CanvasView = View.extend({ } this._context = canvas.getContext('2d'); this._eventCounters = {}; - this._ratio = 1; + this._pixelRatio = 1; if (PaperScope.getAttribute(canvas, 'hidpi') !== 'off') { var deviceRatio = window.devicePixelRatio || 1, - backingStoreRatio = DomElement.getPrefixValue(this._context, + backingStoreRatio = DomElement.getPrefixed(this._context, 'backingStorePixelRatio') || 1; - this._ratio = deviceRatio / backingStoreRatio; + this._pixelRatio = deviceRatio / backingStoreRatio; } View.call(this, canvas); }, @@ -10011,26 +10543,26 @@ var CanvasView = View.extend({ _setViewSize: function(size) { var width = size.width, height = size.height, - ratio = this._ratio, + pixelRatio = this._pixelRatio, element = this._element, style = element.style; - element.width = width * ratio; - element.height = height * ratio; - if (ratio !== 1) { + element.width = width * pixelRatio; + element.height = height * pixelRatio; + if (pixelRatio !== 1) { style.width = width + 'px'; style.height = height + 'px'; - this._context.scale(ratio, ratio); + this._context.scale(pixelRatio, pixelRatio); } }, - draw: function(checkRedraw) { - if (checkRedraw && !this._project._needsRedraw) + update: function() { + if (!this._project._needsUpdate) return false; var ctx = this._context, size = this._viewSize; ctx.clearRect(0, 0, size.width + 1, size.height + 1); - this._project.draw(ctx, this._matrix, this._ratio); - this._project._needsRedraw = false; + this._project.draw(ctx, this._matrix, this._pixelRatio); + this._project._needsUpdate = false; return true; } }, new function() { @@ -10041,86 +10573,98 @@ var CanvasView = View.extend({ downItem, lastItem, overItem, - hasDrag, - doubleClick, + dragItem, + dblClick, clickTime; - function callEvent(type, event, point, target, lastPoint, bubble) { + function callEvent(view, type, event, point, target, lastPoint) { var item = target, mouseEvent; - while (item) { - if (item.responds(type)) { - if (!mouseEvent) + + function call(obj) { + if (obj.responds(type)) { + if (!mouseEvent) { mouseEvent = new MouseEvent(type, event, point, target, lastPoint ? point.subtract(lastPoint) : null); - if (item.fire(type, mouseEvent) - && (!bubble || mouseEvent._stopped)) - return false; + } + if (obj.fire(type, mouseEvent) && mouseEvent.isStopped) { + event.preventDefault(); + return true; + } } + } + + while (item) { + if (call(item)) + return true; item = item.getParent(); } - return true; - } - - function handleEvent(view, type, event, point, lastPoint) { - if (view._eventCounters[type]) { - var project = view._project, - hit = project.hitTest(point, { - tolerance: project.options.hitTolerance || 0, - fill: true, - stroke: true - }), - item = hit && hit.item; - if (item) { - if (type === 'mousemove' && item != overItem) - lastPoint = point; - if (type !== 'mousemove' || !hasDrag) - callEvent(type, event, point, item, lastPoint); - return item; - } - } + if (call(view)) + return true; + return false; } return { - _onMouseDown: function(event, point) { - var item = handleEvent(this, 'mousedown', event, point); - doubleClick = lastItem == item && (Date.now() - clickTime < 300); - downItem = lastItem = item; - downPoint = lastPoint = overPoint = point; - hasDrag = downItem && downItem.responds('mousedrag'); - }, - - _onMouseUp: function(event, point) { - var item = handleEvent(this, 'mouseup', event, point); - if (hasDrag) { - if (lastPoint && !lastPoint.equals(point)) - callEvent('mousedrag', event, point, downItem, lastPoint); - if (item != downItem) { - overPoint = point; - callEvent('mousemove', event, point, item, overPoint); + _handleEvent: function(type, point, event) { + if (!this._eventCounters[type]) + return; + var project = this._project, + hit = project.hitTest(point, { + tolerance: this._scope.settings.hitTolerance, + 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; } - if (item === downItem) { - clickTime = Date.now(); - if (!doubleClick - || callEvent('doubleclick', event, downPoint, item)) - callEvent('click', event, downPoint, item); - doubleClick = false; - } - downItem = null; - hasDrag = false; - }, - - _onMouseMove: function(event, point) { - if (downItem) - callEvent('mousedrag', event, point, downItem, lastPoint); - var item = handleEvent(this, 'mousemove', event, point, overPoint); - lastPoint = overPoint = point; - if (item !== overItem) { - callEvent('mouseleave', event, point, overItem); - overItem = item; - callEvent('mouseenter', event, point, item); - } + return stopped; } }; }); @@ -10132,14 +10676,17 @@ var Event = Base.extend({ this.event = event; }, + isPrevented: false, + isStopped: false, + preventDefault: function() { - this._prevented = true; - DomEvent.preventDefault(this.event); + this.isPrevented = true; + this.event.preventDefault(); }, stopPropagation: function() { - this._stopped = true; - DomEvent.stopPropagation(this.event); + this.isStopped = true; + this.event.stopPropagation(); }, stop: function() { @@ -10222,7 +10769,7 @@ var Key = new function() { type = down ? 'keydown' : 'keyup', view = View._focused, scope = view && view.isVisible() && view._scope, - tool = scope && scope._tool, + tool = scope && scope.tool, name; keyMap[key] = down; if (specialKey && (name = Base.camelize(specialKey)) in modifiers) @@ -10236,7 +10783,7 @@ var Key = new function() { paper = scope; tool.fire(type, new KeyEvent(down, key, character, event)); if (view) - view.draw(true); + view.update(); } } @@ -10641,7 +11188,7 @@ var ToolEvent = Event.extend({ var Tool = PaperScopeItem.extend({ _class: 'Tool', _list: 'tools', - _reference: '_tool', + _reference: 'tool', _events: [ 'onActivate', 'onDeactivate', 'onEditOptions', 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', 'onKeyDown', 'onKeyUp' ], @@ -10748,7 +11295,7 @@ var Tool = PaperScopeItem.extend({ && this.fire(type, new ToolEvent(this, type, event)); }, - _onHandleEvent: function(type, point, event) { + _handleEvent: function(type, point, event) { paper = this._scope; var called = false; switch (type) { @@ -10786,6 +11333,8 @@ var Tool = PaperScopeItem.extend({ } break; } + if (called) + event.preventDefault(); return called; } @@ -10816,19 +11365,19 @@ var Http = { var CanvasProvider = { canvases: [], - getCanvas: function(width, height, ratio) { + getCanvas: function(width, height, pixelRatio) { var canvas, init = true; if (typeof width === 'object') { - ratio = height; + pixelRatio = height; height = width.height; width = width.width; } - if (!ratio) { - ratio = 1; - } else if (ratio !== 1) { - width *= ratio; - height *= ratio; + if (!pixelRatio) { + pixelRatio = 1; + } else if (pixelRatio !== 1) { + width *= pixelRatio; + height *= pixelRatio; } if (this.canvases.length) { canvas = this.canvases.pop(); @@ -10844,8 +11393,8 @@ var CanvasProvider = { canvas.height = height; } ctx.save(); - if (ratio !== 1) - ctx.scale(ratio, ratio); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); return canvas; }, @@ -11115,7 +11664,8 @@ var SVGStyles = Base.each({ miterLimit: ['stroke-miterlimit', 'number'], dashArray: ['stroke-dasharray', 'array'], dashOffset: ['stroke-dashoffset', 'number'], - font: ['font-family', 'string'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], fontSize: ['font-size', 'number'], justification: ['text-anchor', 'lookup', { left: 'start', @@ -11179,22 +11729,23 @@ new function() { attrs[center ? 'cy' : 'y'] = point.y; trans = null; } - if (matrix.isIdentity()) - return attrs; - 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(',') + ')'; + 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 (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } } return attrs; } @@ -11573,7 +12124,7 @@ new function() { currentStyle = project._currentStyle, children = []; if (!isClip) { - item._transformContent = false; + item._applyMatrix = false; item = applyAttributes(item, node, isRoot); project._currentStyle = item._style.clone(); } @@ -11597,11 +12148,14 @@ new function() { } function importPoly(node, type) { - var path = new Path(), - points = node.points; - path.moveTo(points.getItem(0)); - for (var i = 1, l = points.numberOfItems; i < l; i++) - path.lineTo(points.getItem(i)); + 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; @@ -11609,11 +12163,10 @@ new function() { function importPath(node) { var data = node.getAttribute('d'), - path = data.match(/m/gi).length > 1 - ? new CompoundPath() - : new Path(); - path.setPathData(data); - return path; + 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) { @@ -11755,10 +12308,10 @@ new function() { matrix.scale(v); break; case 'skewX': + matrix.skew(v[0], 0); + break; case 'skewY': - var value = Math.tan(v[0] * Math.PI / 180), - isX = command == 'skewX'; - matrix.shear(isX ? value : 0, isX ? 0 : value); + matrix.skew(0, v[0]); break; } } @@ -11904,7 +12457,7 @@ new function() { view = scope.project && scope.project.view; if (onLoad) onLoad.call(this, item); - view.draw(true); + view.update(); } if (isRoot) { @@ -11961,55 +12514,36 @@ new function() { }); }; -paper = new (PaperScope.inject(new Base(Base.exports, { - enumerable: true, - Base: Base, - Numerical: Numerical, - DomElement: DomElement, - DomEvent: DomEvent, - Http: Http, - Key: Key -})))(); - -if (typeof define === 'function' && define.amd) - define('paper', paper); - -return paper; -}; - -paper.PaperScope.prototype.PaperScript = (function(root) { - var Base = paper.Base, - PaperScope = paper.PaperScope, - PaperScript, - exports, define, +Base.exports.PaperScript = (function() { + var exports, define, scope = this; !function(e,r){return"object"==typeof exports&&"object"==typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):(r(e.acorn||(e.acorn={})),void 0)}(this,function(e){"use strict";function r(e){fr=e||{};for(var r in hr)Object.prototype.hasOwnProperty.call(fr,r)||(fr[r]=hr[r]);mr=fr.sourceFile||null}function t(e,r){var t=vr(pr,e);r+=" ("+t.line+":"+t.column+")";var n=new SyntaxError(r);throw n.pos=e,n.loc=t,n.raisedAt=br,n}function n(e){function r(e){if(1==e.length)return t+="return str === "+JSON.stringify(e[0])+";";t+="switch(str){";for(var r=0;r3){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&&8329!==t;)++br,t=pr.charCodeAt(br);fr.onComment&&fr.onComment(!1,pr.slice(e+2,br),e,br,r,fr.locations&&new a)}function u(){for(;dr>br;){var e=pr.charCodeAt(br);if(32===e)++br;else if(13===e){++br;var r=pr.charCodeAt(br);10===r&&++br,fr.locations&&(++Ar,Sr=br)}else if(10===e)++br,++Ar,Sr=br;else if(14>e&&e>8)++br;else if(47===e){var r=pr.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=pr.charCodeAt(br+1);return e>=48&&57>=e?E(!0):(++br,i(xt))}function f(){var e=pr.charCodeAt(br+1);return Er?(++br,k()):61===e?x(Et,2):x(wt,1)}function p(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Ft,1)}function d(e){var r=pr.charCodeAt(br+1);return r===e?x(124===e?Lt:Ut,2):61===r?x(Et,2):x(124===e?Rt:Vt,1)}function m(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Tt,1)}function h(e){var r=pr.charCodeAt(br+1);return r===e?x(St,2):61===r?x(Et,2):x(At,1)}function v(e){var r=pr.charCodeAt(br+1),t=1;return r===e?(t=62===e&&62===pr.charCodeAt(br+2)?3:2,61===pr.charCodeAt(br+t)?x(Et,t+1):x(jt,t)):(61===r&&(t=61===pr.charCodeAt(br+2)?3:2),x(Ot,t))}function b(e){var r=pr.charCodeAt(br+1);return 61===r?x(qt,61===pr.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(ht);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(pt);case 123:return++br,i(dt);case 125:return++br,i(mt);case 58:return++br,i(gt);case 63:return++br,i(kt);case 48:var r=pr.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 p();case 124:case 38:return d(e);case 94:return m();case 43:case 45:return h(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>=dr)return i(Br);var r=pr.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=pr.slice(br,br+r);br+=r,i(e,t)}function k(){for(var e,r,n="",a=br;;){br>=dr&&t(a,"Unterminated regular expression");var o=pr.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=pr.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=pr.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(pr.charCodeAt(br))&&t(br,"Identifier directly after number"),i(Or,e)}function E(e){var r=br,n=!1,a=48===pr.charCodeAt(br);e||null!==w(10)||t(r,"Invalid number"),46===pr.charCodeAt(br)&&(++br,w(10),n=!0);var o=pr.charCodeAt(br);(69===o||101===o)&&(o=pr.charCodeAt(++br),(43===o||45===o)&&++br,null===w(10)&&t(r,"Invalid number"),n=!0),Qt(pr.charCodeAt(br))&&t(br,"Identifier directly after number");var s,c=pr.slice(r,br);return n?s=parseFloat(c):a&&1!==c.length?/[89]/.test(c)||Vr?t(r,"Invalid number"):s=parseInt(c,8):s=parseInt(c,10),i(Or,s)}function A(e){br++;for(var r="";;){br>=dr&&t(yr,"Unterminated string constant");var n=pr.charCodeAt(br);if(n===e)return++br,i(Fr,r);if(92===n){n=pr.charCodeAt(++br);var a=/^[0-7]+/.exec(pr.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)Vr&&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===pr.charCodeAt(br)&&++br;case 10:fr.locations&&(Sr=br,++Ar);break;default:r+=String.fromCharCode(n)}}else(13===n||10===n||8232===n||8329===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=pr.charCodeAt(br);if(Yt(a))Bt&&(e+=pr.charAt(br)),++br;else{if(92!==a)break;Bt||(e=pr.slice(n,br)),Bt=!0,117!=pr.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:pr.slice(n,br)}function L(){var e=I(),r=Dr;return Bt||(Wt(e)?r=lt[e]:(fr.forbidReserved&&(3===fr.ecmaVersion?Mt:zt)(e)||Vr&&Xt(e))&&t(yr,"The keyword '"+e+"' is reserved")),i(r,e)}function U(){Ir=yr,Lr=gr,Ur=kr,g()}function R(e){for(Vr=e,br=Lr;Sr>br;)Sr=pr.lastIndexOf("\n",Sr-2)+1,--Ar;u(),g()}function T(){this.type=null,this.start=yr,this.end=null}function V(){this.start=xr,this.end=null,null!==mr&&(this.source=mr)}function q(){var e=new T;return fr.locations&&(e.loc=new V),fr.ranges&&(e.range=[yr,0]),e}function O(e){var r=new T;return r.start=e.start,fr.locations&&(r.loc=new V,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 F(e){return fr.ecmaVersion>=5&&"ExpressionStatement"===e.type&&"Literal"===e.expression.type&&"use strict"===e.expression.value}function D(e){return wr===e?(U(),!0):void 0}function B(){return!fr.strictSemicolons&&(wr===Br||wr===mt||Gt.test(pr.slice(Lr,yr)))}function M(){D(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"),Vr&&"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=Vr=null,Tr=[],g();var r=e||q(),t=!0;for(e||(r.body=[]);wr!==Br;){var n=J();r.body.push(n),t&&F(n)&&R(!0),t=!1}return j(r,"Program")}function J(){wr===wt&&g(!0);var e=wr,r=q();switch(e){case Mr:case Nr:U();var n=e===Mr;D(yt)||B()?r.label=null:wr!==Dr?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 a=j(a,/&&|\|\|/.test(a.operator)?"LogicalExpression":"BinaryExpression");return er(a,r,t)}return e}function rr(){if(wr.prefix){var e=q(),r=wr.isUpdate;return e.operator=Cr,e.prefix=!0,U(),e.argument=rr(),r?N(e.argument):Vr&&"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(D(xt)){var t=O(e);return t.object=e,t.property=lr(!0),t.computed=!1,nr(j(t,"MemberExpression"),r)}if(D(ft)){var t=O(e);return t.object=e,t.property=K(),t.computed=!0,z(pt),nr(j(t,"MemberExpression"),r)}if(!r&&D(ht)){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 Dr:return lr();case Or:case Fr:case jr:var e=q();return e.value=Cr,e.raw=pr.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 ht: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(pt,!0,!0),j(e,"ArrayExpression");case dt: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=D(ht)?ur(vt,!1):qr,j(e,"NewExpression")}function ir(){var e=q(),r=!0,n=!1;for(e.properties=[],U();!D(mt);){if(r)r=!1;else if(z(bt),fr.allowTrailingCommas&&D(mt))break;var a,o={key:sr()},i=!1;if(D(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!==ht&&X(),o.value=cr(q(),!1)):X(),"Identifier"===o.key.type&&(Vr||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;!D(e);){if(a)a=!1;else if(z(bt),r&&fr.allowTrailingCommas&&D(e))break;t&&wr===bt?n.push(null):n.push(K(!0))}return n}function lr(e){var r=q();return r.name=wr===Dr?Cr:e&&!fr.forbidReserved&&wr.keyword||X(),U(),j(r,"Identifier")}e.version="0.3.2";var fr,pr,dr,mr;e.parse=function(e,t){return pr=String(e),dr=pr.length,r(t),o(),W(fr.program)};var hr=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', + '+': '__add', + '-': '__subtract', + '*': '__multiply', + '/': '__divide', + '%': '__modulo', '==': 'equals', '!=': 'equals' }; var unaryOperators = { - '-': '_negate', + '-': '__negate', '+': null }; var fields = Base.each( ['add', 'subtract', 'multiply', 'divide', 'modulo', 'negate'], function(name) { - this['_' + name] = '#' + name; + this['__' + name] = '#' + name; }, {} ); - paper.Point.inject(fields); - paper.Size.inject(fields); - paper.Color.inject(fields); + Point.inject(fields); + Size.inject(fields); + Color.inject(fields); function _$_(left, operator, right) { var handler = binaryOperators[operator]; @@ -12059,8 +12593,8 @@ paper.PaperScope.prototype.PaperScript = (function(root) { function replaceCode(node, str) { var start = getOffset(node.range[0]), - end = getOffset(node.range[1]); - var insert = 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; @@ -12086,7 +12620,15 @@ paper.PaperScope.prototype.PaperScript = (function(root) { } } switch (node && node.type) { - case 'BinaryExpression': + 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), @@ -12095,32 +12637,29 @@ paper.PaperScope.prototype.PaperScript = (function(root) { + '", ' + right + ')'); } break; - case 'AssignmentExpression': - 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; - case 'UpdateExpression': - if (!node.prefix && !(parent && ( - parent.type === 'BinaryExpression' + case 'UpdateExpression': + case 'AssignmentExpression': + if (!(parent && ( + parent.type === 'ForStatement' + || parent.type === 'BinaryExpression' && /^[=!<>]/.test(parent.operator) || parent.type === 'MemberExpression' && parent.computed))) { - var arg = getCode(node.argument); - replaceCode(node, arg + ' = _$_(' + arg + ', "' - + node.operator[0] + '", 1)'); - } - break; - case 'UnaryExpression': - if (node.operator in unaryOperators - && node.argument.type !== 'Literal') { - var arg = getCode(node.argument); - replaceCode(node, '$_("' + node.operator + '", ' - + arg + ')'); + if (node.type === 'UpdateExpression') { + if (!node.prefix) { + var arg = getCode(node.argument); + replaceCode(node, arg + ' = _$_(' + arg + ', "' + + node.operator[0] + '", 1)'); + } + } 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; } @@ -12129,57 +12668,70 @@ paper.PaperScope.prototype.PaperScript = (function(root) { return code; } - function evaluate(code, scope) { + function execute(code, scope) { paper = scope; - var view = scope.project && scope.project.view, - res; - with (scope) { - (function() { - var onActivate, onDeactivate, onEditOptions, - onMouseDown, onMouseUp, onMouseDrag, onMouseMove, - onKeyDown, onKeyUp, onFrame, onResize; - code = compile(code); - if (root.InstallTrigger) { - var handle = PaperScript.handleException; - if (!handle) { - handle = PaperScript.handleException = function(e) { - throw e.lineNumber >= lineNumber - ? new Error(e.message, e.fileName, - e.lineNumber - lineNumber) - : e; - } - var lineNumber = new Error().lineNumber; - lineNumber += (new Error().lineNumber - lineNumber) * 3; - } - try { - res = eval(';' + code); - } catch (e) { - handle(e); - } - } else { - res = eval(code); + 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); + function expose(scope, hidden) { + for (var key in scope) { + if ((hidden || !/^_/.test(key)) && new RegExp( + '\\b' + key.replace(/\$/g, '\\$') + '\\b').test(code)) { + params.push(key); + args.push(scope[key]); } - if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) { - Base.each(paper.Tool.prototype._events, function(key) { - var value = eval(key); - if (value) { - scope.getTool()[key] = value; - } - }); - } - if (view) { - view.setOnResize(onResize); - view.fire('resize', { - size: view.size, - delta: new Point() - }); - if (onFrame) - view.setOnFrame(onFrame); - view.draw(); - } - }).call(scope); + } + } + expose({ _$_: _$_, $_: $_, 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 firefox = window.InstallTrigger; + if (firefox || window.chrome) { + var script = document.createElement('script'), + head = document.head; + if (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.fire('resize', { + size: view.size, + delta: new Point() + }); + if (res.onFrame) + view.setOnFrame(res.onFrame); + view.update(); } - return res; } function load() { @@ -12191,11 +12743,11 @@ paper.PaperScope.prototype.PaperScript = (function(root) { || new PaperScope(script).setup(canvas), src = script.src; if (src) { - paper.Http.request('get', src, function(code) { - evaluate(code, scope); + Http.request('get', src, function(code) { + execute(code, scope); }); } else { - evaluate(script.innerHTML, scope); + execute(script.innerHTML, scope); } script.setAttribute('data-paper-ignore', true); } @@ -12205,14 +12757,30 @@ paper.PaperScope.prototype.PaperScript = (function(root) { if (document.readyState === 'complete') { setTimeout(load); } else { - paper.DomEvent.add(window, { load: load }); + DomEvent.add(window, { load: load }); } - return PaperScript = { + return { compile: compile, - evaluate: evaluate, + execute: execute, load: load, lineNumberBase: 0 }; -})(this); +}).call(this); + +paper = new (PaperScope.inject(Base.exports, { + enumerable: true, + Base: Base, + Numerical: Numerical, + DomElement: DomElement, + DomEvent: DomEvent, + Http: Http, + Key: Key +}))(); + +if (typeof define === 'function' && define.amd) + define('paper', paper); + +return paper; +}; diff --git a/dist/paper-full.min.js b/dist/paper-full.min.js index 9ea23840..36f91e12 100644 --- a/dist/paper-full.min.js +++ b/dist/paper-full.min.js @@ -1,38 +1,38 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * 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(t){var e=new function(){function n(e,n,i,r,o,h){function u(r,s,h,u){var s=s||(s=l(n,r))&&(s.get?s:s.value);"string"==typeof s&&"#"===s[0]&&(s=e[s.substring(1)]||s);var f,_="function"==typeof s,g=s,p=o||_?s&&s.get?r in e:e[r]:null;!(h||s!==t&&n.hasOwnProperty(r))||o&&p||(_&&p&&(s.base=p),_&&c&&0===s.length&&(f=r.match(/^(get|is)(([A-Z])(.*))$/))&&c.push([f[3].toLowerCase()+f[4],f[2]]),g&&!_&&g.get&&"function"==typeof g.get&&0===g.get.length||(g={value:g,writable:!0}),(l(e,r)||{configurable:!0}).configurable&&(g.configurable=!0,g.enumerable=i),d(e,r,g)),!u||!_||o&&u[r]||(u[r]=function(t){return t&&e[r].apply(t,a.call(arguments,1))})}var c;if(n){c=[];for(var f in n)n.hasOwnProperty(f)&&!s.test(f)&&u(f,null,!0,h);u("toString"),u("valueOf");for(var _=0,g=c.length;g>_;_++){var p=c[_],v=p[1];u(p[0],{get:e["get"+v]||e["is"+v],set:e["set"+v]},!0)}}return e}function i(t,e,n){return t&&("length"in t&&!t.getLength&&"number"==typeof t.length?o:h).call(t,e,n=n||t),n}function r(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}var s=/^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/,a=[].slice,o=[].forEach||function(t,e){for(var n=0,i=this.length;i>n;n++)t.call(e,this[n],n,this)},h=function(t,e){for(var n in this)this.hasOwnProperty(n)&&t.call(e,this[n],n,this)},u=Object.create||function(t){return{__proto__:t}},l=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},c=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},d=function(t,e,n){return delete t[e],c(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=this.prototype,i=Object.getPrototypeOf(e).constructor,r=t.statics===!0?t:t.statics;r!=t&&n(e,t,t.enumerable,i&&i.prototype,t.preserve,t.generics&&this),n(this,r,!0,i,t.preserve)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e=this,i=0,r=arguments.length;r>i&&!(t=arguments[i].initialize);i++);return t=t||function(){e.apply(this,arguments)},t.prototype=u(this.prototype),d(t.prototype,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length?this.inject.apply(t,arguments):t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++)n(this,arguments[t],arguments[t].enumerable);return this},extend:function(){var t=u(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},clone:function(){return new this.constructor(this)},statics:{each:i,create:u,define:d,describe:l,copy: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(){for(var e=0,n=arguments.length;n>e;e++)if(arguments[e]!==t)return arguments[e];return null}}})};"undefined"!=typeof module&&(module.exports=e),e.inject({generics:!0,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?a.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i){if(n&&e.isPlainObject(n)){var r=n._filtering||n;for(var s in r)if(s in this&&r.hasOwnProperty(s)&&(!i||!i[s])){var a=n[s];a!==t&&(this[s]=a)}return!0}},statics:{exports:{},extend:function ie(){var t=ie.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){function i(t,e){for(var n in t)if(t.hasOwnProperty(n)&&!e.hasOwnProperty(n))return!1;return!0}if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(var r=0,s=t.length;s>r;r++)if(!e.equals(t[r],n[r]))return!1;return!0}if(t&&"object"==typeof t&&n&&"object"==typeof n){if(!i(t,n)||!i(n,t))return!1;for(var r in t)if(t.hasOwnProperty(r)&&!e.equals(t[r],n[r]))return!1;return!0}return!1},read:function(t,n,i,r){if(this===e){var s=this.peek(t,n);return t._index++,t.__read=1,s}var a=this.prototype,o=a._readIndex,h=n||o&&t._index||0;i||(i=t.length-h);var u=t[h];return u instanceof this||r&&r.readNull&&null==u&&1>=i?(o&&(t._index=h+1),u&&r&&r.clone?u.clone():u):(u=e.create(this.prototype),o&&(u.__read=!0),r&&(u.__options=r),u=u.initialize.apply(u,h>0||is;s++)r.push(Array.isArray(i=t[s])?this.read(i,0,0,n):this.read(t,s,1,n));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){return this.isPlainObject(t)||Array.isArray(t)},serialize:function(t,n,i,r){n=n||{};var s,o=!r;if(o&&(n.formatter=new a(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){s=t._serialize(n,r);var h=t._class;!h||i||s._compact||s[0]===h||s.unshift(h)}else if(Array.isArray(t)){s=[];for(var u=0,l=t.length;l>u;u++)s[u]=e.serialize(t[u],n,i,r);i&&(s._compact=!0)}else if(e.isPlainObject(t)){s={};for(var u in t)t.hasOwnProperty(u)&&(s[u]=e.serialize(t[u],n,i,r))}else s="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],s]:s},deserialize:function(t,n,i){var r=t;if(i=i||{},Array.isArray(t)){var s=t[0],a="dictionary"===s;if(!a){if(i.dictionary&&1==t.length&&/^#/.test(s))return i.dictionary[s];s=e.exports[s]}r=[];for(var o=s?1:0,h=t.length;h>o;o++)r.push(e.deserialize(t[o],n,i));if(a)i.dictionary=r[0];else if(s){var u=r;n?r=n(s,u):(r=e.create(s.prototype),s.apply(r,u))}}else if(e.isPlainObject(t)){r={};for(var l in t)r[l]=e.deserialize(t[l],n,i)}return r},exportJSON:function(t,n){return JSON.stringify(e.serialize(t,n))},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 y&&(!(r instanceof x)||s)){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++)delete u[o]._index;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={attach:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.attach(e,t)},this),t;var r=this._eventTypes[n];if(r){var s=this._handlers=this._handlers||{};s=s[n]=s[n]||[],-1==s.indexOf(i)&&(s.push(i),r.install&&1==s.length&&r.install.call(this,n))}},detach:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.detach(e,t)},this),t;var r,s=this._eventTypes[n],a=this._handlers&&this._handlers[n];s&&a&&(!i||-1!=(r=a.indexOf(i))&&1==a.length?(s.uninstall&&s.uninstall.call(this,n),delete this._handlers[n]):-1!=r&&a.splice(r,1))},once:function(t,e){this.attach(t,function(){e.apply(this,arguments),this.detach(t,e)})},fire:function(t,e){function n(){for(var t in i)i[t].apply(o,r)===!1&&e&&e.stop&&e.stop()}var i=this._handlers&&this._handlers[t];if(!i)return!1;var r=[].slice.call(arguments,1),s=paper.PaperScript,a=s&&s.handleException,o=this;if(a)try{n()}catch(h){a(h)}else n();return!0},responds:function(t){return!(!this._handlers||!this._handlers[t])},on:"#attach",off:"#detach",trigger:"#fire",statics:{inject:function re(){for(var t=0,n=arguments.length;n>t;t++){var i=arguments[t],r=i._events;if(r){var s={};e.each(r,function(t,n){var r="string"==typeof t,a=r?t:n,o=e.capitalize(a),h=a.substring(2).toLowerCase();s[h]=r?{}:t,a="_"+a,i["get"+o]=function(){return this[a]},i["set"+o]=function(t){t?this.attach(h,t):this[a]&&this.detach(h,this[a]),this[a]=t}}),i._eventTypes=s}re.base.call(this,i)}return this}}},r=e.extend({_class:"PaperScope",initialize:function se(t){if(paper=this,this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=t&&(t.getAttribute("id")||t.src)||"paperscope-"+se._id++,t&&t.setAttribute("id",this._id),se._scopes[this._id]=this,!this.support){var e=Q.getContext(1,1);se.prototype.support={nativeDash:"setLineDash"in e||"mozDash"in e,nativeBlendModes:te.nativeModes},Q.release(e)}},version:"0.9.15",getView:function(){return this.project&&this.project.view},getTool:function(){return this._tool||(this._tool=new Y),this._tool},getPaper:function(){return this},evaluate:function(t){var e=paper.PaperScript.evaluate(t,this);return Z.updateFocus(),e},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)/^(version|_id)/.test(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 r._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"object"==typeof t&&(t=t.getAttribute("id")),this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),s=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.fire("deactivate"),this._scope[this._reference]=this,this.fire("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)}}),a=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},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)}});a.instance=new a;var o=new function(){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,a=Math.cos,h=Math.PI;return{TOLERANCE:1e-5,EPSILON:1e-11,KAPPA:4*(r(2)-1)/3,isZero:function(t){return i(t)<=o.EPSILON},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);if(i(l)0?(s=n,n=r>=c?.5*(r+s):c):(r=n,n=c>=s?.5*(r+s):c)}},solveQuadratic:function(e,n,s,a,h,u){function l(t){return(d||t>f&&_>t)&&(a[g++]=h>t?h:t>u?u:t),g}var c=o.EPSILON,d=h===t,f=h-c,_=u+c,g=0;if(i(e)=c?l(-s/n):i(s)m)return 0;var y=m>v?r(m-v):0;return l(y-p),y>0&&l(-y-p),g},solveCubic:function(e,n,u,l,c,d,f){function _(t){return(p||t>v&&m>t)&&(c[y++]=d>t?d:t>f?f:t),y}var g=o.EPSILON;if(i(e)0?1:-1;return _(2*-k*P-n),_(k*P-n)}if(0>S){var P=r(x),z=Math.acos(b/(P*P*P))/3,M=-2*P,A=2*h/3;return _(M*a(z)-n),_(M*a(z+A)-n),_(M*a(z-A)-n)}var I=(b>0?-1:1)*s(i(b)+r(S),1/3);return _(I+x/I-n)}}},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 t===this||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=a.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)]},add:function(t){return t=h.read(arguments),new h(this.x+t.x,this.y+t.y)},subtract:function(t){return t=h.read(arguments),new h(this.x-t.x,this.y-t.y)},multiply:function(t){return t=h.read(arguments),new h(this.x*t.x,this.y*t.y)},divide:function(t){return t=h.read(arguments),new h(this.x/t.x,this.y/t.y)},modulo:function(t){return t=h.read(arguments),new h(this.x%t.x,this.y%t.y)},negate:function(){return new h(-this.x,-this.y)},transform:function(t){return t?t._transformPoint(this):this},getDistance:function(t,e){t=h.read(arguments);var n=t.x-this.x,i=t.y-this.y,r=n*n+i*i;return e?r:Math.sqrt(r)},getLength:function(){var t=this.x*this.x+this.y*this.y;return arguments.length&&arguments[0]?t:Math.sqrt(t)},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();o.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}return this},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 r._angle=this._angle,r},getAngle:function(){return 180*this.getAngleInRadians(arguments[0])/Math.PI},setAngle:function(t){if(t=this._angle=t*Math.PI/180,!this.isZero()){var e=this.getLength();this.set(Math.cos(t)*e,Math.sin(t)*e)}return this},getAngleInRadians:function(){if(arguments[0]===t)return this.isZero()?this._angle||0:this._angle=Math.atan2(this.y,this.x);var e=h.read(arguments),n=this.getLength()*e.getLength();return o.isZero(n)?0/0:Math.acos(this.dot(e)/n)},getAngleInDegrees:function(){return this.getAngle(arguments[0])},getQuadrant:function(){return this.x>=0?this.y>=0?1:4:this.y>=0?2:3},getDirectedAngle:function(t){return t=h.read(arguments),180*Math.atan2(this.cross(t),this.dot(t))/Math.PI},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.y*r+n.x*i),e?n.add(e):n},isInside:function(t){return t.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 c(this.width,this.height)},toString:function(){var t=a.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(t){return t=c.read(arguments),new c(this.width+t.width,this.height+t.height)},subtract:function(t){return t=c.read(arguments),new c(this.width-t.width,this.height-t.height)},multiply:function(t){return t=c.read(arguments),new c(this.width*t.width,this.height*t.height)},divide:function(t){return t=c.read(arguments),new c(this.width/t.width,this.height/t.height)},modulo:function(t){return t=c.read(arguments),new c(this.width%t.width,this.height%t.height)},negate:function(){return new c(-this.width,-this.height)},isZero:function(){return o.isZero(this.width)&&o.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new c(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new c(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new c(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new c(e(this.width),e(this.height))}},{})),d=c.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)}}),f=e.extend({_class:"Rectangle",_readIndex:!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"),l=e.peek(arguments);if(this.x=u.x,this.y=u.y,l&&l.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=c.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 f(this.x,this.y,this.width,this.height)},equals:function(t){return e.isPlainValue(t)&&(t=f.read(arguments)),t===this||t&&this.x===t.x&&this.y===t.y&&this.width===t.width&&this.height===t.height||!1},toString:function(){var t=a.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(){return new(arguments[0]?h:u)(this.x,this.y,this,"setPoint")},setPoint:function(t){t=h.read(arguments),this.x=t.x,this.y=t.y},getSize:function(){return new(arguments[0]?c:d)(this.width,this.height,this,"setSize")},setSize:function(t){t=c.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(){return new(arguments[0]?h:u)(this.getCenterX(),this.getCenterY(),this,"setCenter")},setCenter:function(t){return t=h.read(arguments),this.setCenterX(t.x),this.setCenterY(t.y),this},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(f.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(t){return t=f.read(arguments),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(t){t=f.read(arguments);var 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 f(e,n,i-e,r-n)},unite:function(t){t=f.read(arguments);var 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 f(e,n,i-e,r-n)},include:function(t){t=h.read(arguments);var 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 f(e,n,i-e,r-n)},expand:function(e,n){return n===t&&(n=e),new f(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)}},new function(){return 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(){return new(arguments[0]?h:u)(this[a](),this[o](),this,f)},this[f]=function(t){t=h.read(arguments),this[l](t.x),this[c](t.y)}},{})}),_=f.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=f.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),delete this._dontNotify,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 ae(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof ae?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(){this._owner&&this._owner._changed(5)},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=a.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(){return this._a=this._d=1,this._c=this._b=this._tx=this._ty=0,this._changed(),this},scale:function(){var t=h.read(arguments),e=h.read(arguments,0,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},translate:function(t){t=h.read(arguments);var 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},rotate:function(t,e){e=h.read(arguments,1),t=t*Math.PI/180;var 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,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},concatenate:function(t){var e=this._a,n=this._b,i=this._c,r=this._d;return this._a=t._a*e+t._c*n,this._b=t._b*e+t._d*n,this._c=t._a*i+t._c*r,this._d=t._b*i+t._d*r,this._tx+=t._tx*e+t._ty*n,this._ty+=t._tx*i+t._ty*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;return this._a=t._a*e+t._b*i,this._b=t._a*n+t._b*r,this._c=t._c*e+t._d*i,this._d=t._c*n+t._d*r,this._tx=t._a*s+t._b*a+t._tx,this._ty=t._c*s+t._d*a+t._ty,this._changed(),this},isIdentity:function(){return 1===this._a&&0===this._c&&0===this._b&&1===this._d&&0===this._tx&&0===this._ty},isInvertible:function(){return!!this._getDeterminant()},isSingular:function(){return!this._getDeterminant()},transform:function(t,e,n,i,r){return arguments.length<5?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,n,i,r)},_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,i,r){for(var s=e,a=i,o=s+2*r;o>s;){var h=t[s++],u=t[s++];n[a++]=h*this._a+u*this._b+this._tx,n[a++]=h*this._c+u*this._d+this._ty}return n},_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,0,s,0,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 f),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)&&!o.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(o.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 a=Math.sqrt(n*n+i*i);return n/=a,i/=a,s/=a,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{translation:this.getTranslation(),scaling:new h(r,a),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)},setTranslation:function(){var t=h.read(arguments);this._tx=t.x,this._ty=t.y,this._changed()},getScaling:function(){return(this.decompose()||{}).scaling},setScaling:function(){var t=this.getScaling();if(null!=t){var e=h.read(arguments);(this._owner||this).scale(e.x/t.x,e.y/t.y)}},getRotation:function(){return(this.decompose()||{}).rotation},setRotation:function(t){var e=this.getRotation();null!=e&&(this._owner||this).rotate(t-e)},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)}},new function(){return e.each({scaleX:"_a",scaleY:"_d",translateX:"_tx",translateY:"_ty",shearX:"_b",shearY:"_c"},function(t,n){n=e.capitalize(n),this["get"+n]=function(){return this[t]},this["set"+n]=function(e){this[t]=e,this._changed()}},{})}),p=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 p.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return p.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(p.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,n,i,r,s,a,u,l,c){l||(n-=t,i-=e,a-=r,u-=s);var d=u*n-a*i;if(!o.isZero(d)){var f=t-r,_=e-s,g=(a*_-u*f)/d,p=(n*_-i*f)/d;if((c||g>=0&&1>=g)&&(c||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,a){a||(n-=t,i-=e);var o=i/n,h=e-o*t;return(s-o*r-h)/Math.sqrt(o*o+1)}}}),v=s.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){s.call(this,!0),this.layers=[],this.symbols=[],this._currentStyle=new R,this.activeLayer=new x,t&&(this.view=t instanceof Z?t:Z.create(t)),this._selectedItems={},this._selectedItemCount=0,this._drawCount=0,this.options={}},_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 this.layers.length<=1&&(!this.activeLayer||this.activeLayer.isEmpty())},remove:function oe(){return oe.base.call(this)?(this.view&&this.view.remove(),!0):!1},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},addChild:function(t){return t instanceof x?(e.splice(this.layers,[t]),this.activeLayer||(this.activeLayer=t)):t instanceof y?(this.activeLayer||this.addChild(new x({insert:!1}))).addChild(t):t=null,t},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},_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(t,n){t=h.read(arguments),n=P.getOptions(e.read(arguments));for(var i=this.layers.length-1;i>=0;i--){var r=this.layers[i].hitTest(t,n);if(r)return r}return null}},new function(){function t(t,e,n){for(var i=t.layers,r=n&&[],s=0,a=i.length;a>s;s++){var o=i[s][n?"getItems":"getItem"](e);if(n)r.push.apply(r,o);else if(o)return o}return n?r:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{importJSON:function(t){this.activate();var n=this.activeLayer;return e.importJSON(t,n&&n.isEmpty()&&n)},draw:function(t,n,i){this._drawCount++,t.save(),n.applyToContext(t);for(var r=new e({offset:new h(0,0),ratio:i,transforms:[n],trackTransforms:!0}),s=0,a=this.layers.length;a>s;s++)this.layers[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;for(var o in this._selectedItems){var u=this._selectedItems[o];if(u._drawCount===this._drawCount&&(u._drawSelected||u._boundsSelected)){var l=u.getSelectedColor()||u.getLayer().getSelectedColor();t.strokeStyle=t.fillStyle=l?l.toCanvasStyle(t):"#009dec";var c=u._globalMatrix;if(u._drawSelected&&u._drawSelected(t,c),u._boundsSelected){var d=c._transformCorners(u._getBounds("getBounds"));t.beginPath();for(var s=0;8>s;s++)t[0===s?"moveTo":"lineTo"](d[s],d[++s]);t.closePath(),t.stroke();for(var s=0;8>s;s++)t.beginPath(),t.rect(d[s]-2,d[++s]-2,4,4),t.fill()}}}t.restore()}}}),m=e.extend({_class:"Symbol",initialize:function he(t,e){this._id=he._id=(he._id||0)+1,this.project=paper.project,this.project.symbols.push(this),t&&this.setDefinition(t,e),this._instances={}},_serialize:function(t,n){return n.add(this,function(){return e.serialize([this._class,this._definition],t,!1,n)})},_changed:function(t){e.each(this._instances,function(e){e._changed(t)})},getDefinition:function(){return this._definition},setDefinition:function(t){t._parentSymbol&&(t=t.clone()),this._definition&&delete this._definition._parentSymbol,this._definition=t,t.remove(),t.setSelected(!1),arguments[1]||t.setPosition(new h),t._parentSymbol=this,this._changed(5)},place:function(t){return new S(this,t)},clone:function(){return new m(this._definition.clone(!1))}}),y=e.extend(n,{statics:{extend:function ue(t){t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields));var n=ue.base.apply(this,arguments),i=n.prototype,r=i._class;return r&&(i._type=e.hyphenate(r)),n}},_class:"Item",_transformContent:!0,_boundsSelected:!1,_serializeFields:{name:null,matrix:new g,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,data:{}},initialize:function(){},_initialize:function(t,e){if(this._id=y._id=(y._id||0)+1,!this._project){var n=paper.project;t&&t.insert===!1?this._setProject(n):(n.activeLayer||new x).addChild(this)}this._style=new R(this._project._currentStyle,this);var i=this._matrix=new g;return e&&i.translate(e),i._owner=this,t?this._set(t,{insert:!0}):!0},_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._project.view._eventCounters;if(n)for(var i in t)n[i]=(n[i]||0)+(t[i][e]||0)},uninstall:function(e){var n=this._project.view._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._project.view._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 w||i(this._style._defaults),[this._class,r]},_changed:function(t){var e=this._parent,n=this._project,i=this._parentSymbol;if(this._drawCount=null,4&t&&(delete this._bounds,delete this._position),e&&12&t&&e._clearBoundsCache(),2&t&&this._clearBoundsCache(),n&&(1&t&&(n._needsRedraw=!0),n._changes)){var r=n._changesById[this._id];r?r.flags|=t:(r={item:this,flags:t},n._changesById[this._id]=r,n._changes.push(r))}i&&i._changed(t)},set:function(t){return t&&this._set(t),this},getId:function(){return this._id},getType:function(){return this._type},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.");if(e&&this._parent){for(var i=this._parent._children,r=this._parent._namedChildren,s=e,a=1;n&&i[e];)e=s+" "+a++;(r[e]=r[e]||[]).push(this),i[e]=this}this._name=e||t,this._changed(32)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()}},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?32:33))}},{}),{_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._children)for(var t=0,e=this._children.length;e>t;t++)if(this._children[t].isSelected())return!0;return this._selected},setSelected:function(t){if(this._children&&!arguments[1])for(var e=0,n=this._children.length;n>e;e++)this._children[e].setSelected(t);(t=!!t)!=this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(33))},_selected:!1,isFullySelected:function(){if(this._children&&this._selected){for(var t=0,e=this._children.length;e>t;t++)if(!this._children[t].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){if(this._children)for(var e=0,n=this._children.length;n>e;e++)this._children[e].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(33),this._parent&&this._parent._changed(256))},_clipMask:!1,getData:function(){return this._data||(this._data={}),this._data},setData:function(t){this._data=t},getPosition:function(){var t=this._position||(this._position=this.getBounds().getCenter(!0));return new(arguments[0]?h:u)(t.x,t.y,this,"setPosition")},setPosition:function(){this.translate(h.read(arguments).subtract(this.getPosition(!0)))}},e.each(["getBounds","getStrokeBounds","getHandleBounds","getRoughBounds"],function(t){this[t]=function(){var e=this._boundsGetter,n=this._getCachedBounds("string"==typeof e?e:e&&e[t]||t,arguments[0]);return"getBounds"===t?new _(n.x,n.y,n.width,n.height,this,"setBounds"):n}},{_getCachedBounds:function(t,e,n){var i=(!e||e.equals(this._matrix))&&t;if(n&&this._parent){var r=n._id,s=this._parent._boundsCache=this._parent._boundsCache||{ids:{},list:[]};s.ids[r]||(s.list.push(n),s.ids[r]=n)}if(i&&this._bounds&&this._bounds[i])return this._bounds[i].clone();var a=this._matrix.isIdentity();e=!e||e.isIdentity()?a?null:this._matrix:a?e:e.clone().concatenate(this._matrix);var o=this._getBounds(t,e,i?this:n);return i&&(this._bounds||(this._bounds={}),this._bounds[i]=o.clone()),o},_clearBoundsCache:function(){if(this._boundsCache){for(var t=0,e=this._boundsCache.list,n=e.length;n>t;t++){var i=e[t];delete i._bounds,i!=this&&i._boundsCache&&i._clearBoundsCache()}delete this._boundsCache}},_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new f;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,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 f(r,a,s-r,o-a):new f},setBounds:function(t){t=f.read(arguments);var 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)}}),{getMatrix:function(){return this._matrix},setMatrix:function(t){this._matrix.initialize(t),this._transformContent&&this.applyMatrix(!0),this._changed(5)},getGlobalMatrix:function(){return this._drawCount===this._project._drawCount&&this._globalMatrix||null},getTransformContent:function(){return this._transformContent},setTransformContent:function(t){this._transformContent=t,t&&this.applyMatrix()},getProject:function(){return this._project},_setProject:function(t){if(this._project!=t){var e=this.responds("frame");if(e&&this._animateItem(!1),this._project=t,e&&this._animateItem(!0),this._children)for(var n=0,i=this._children.length;i>n;n++)this._children[n]._setProject(t)}},getLayer:function(){for(var t=this;t=t._parent;)if(t instanceof x)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},isInserted:function(){return this._parent?this._parent.isInserted():!1},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({insert:!1}),t)},_clone:function(n,i){if(n.setStyle(this._style),this._children)for(var r=0,s=this._children.length;s>r;r++)n.addChild(this._children[r].clone(!1),!0);(i||i===t)&&n.insertAbove(this);for(var a=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide"],r=0,s=a.length;s>r;r++){var o=a[r];this.hasOwnProperty(o)&&(n[o]=this[o])}return n._matrix.initialize(this._matrix),n._data=this._data?e.clone(this._data):null,n.setSelected(this._selected),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||72)/72,r=n.getTopLeft().floor(),s=n.getBottomRight().ceil();size=new c(s.subtract(r)),canvas=Q.getCanvas(size),ctx=canvas.getContext("2d"),matrix=(new g).scale(i).translate(r.negate()),ctx.save(),matrix.applyToContext(ctx),this.draw(ctx,new e({transforms:[matrix]})),ctx.restore();var a=new C({canvas:canvas,insert:!1});return a.setPosition(r.add(size.divide(2))),a.insertAbove(this),a},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._getBounds("getBounds"))},hitTest:function(n,i){function r(r,s){var h=o["get"+s]();return n.getDistance(h)l&&!s;l++)s=r("bounds",u[l])}return(s||(s=this._children||!(i.guides&&!this._guide||i.selected&&!this._selected)?this._hitTest(n,i):null))&&s.point&&(s.point=a._matrix.transform(s.point)),s},_hitTest:function(t,e){var n=this._children;if(n){for(var i,r=n.length-1;r>=0;r--)if(i=n[r].hitTest(t,e))return i}else if(e.fill&&this.hasFill()&&this._contains(t))return new P("fill",this)},matches:function(t){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}for(var i in t)if(t.hasOwnProperty(i)){var r=this[i],s=t[i];if(s instanceof RegExp){if(!s.test(r))return!1}else if("function"==typeof s){if(!s(r))return!1}else if(e.isPlainObject(s)){if(!n(s,r))return!1}else if(!e.equals(r,s))return!1}return!0}},new function(){function t(e,n,i){for(var r=e._children,s=i&&[],a=0,o=r&&r.length;o>a;a++){var h=r[a];if(h.matches(n)){if(!i)return h;s.push(h)}var u=t(h,n,i);if(i)s.push.apply(s,u);else if(u)return u}return i?s:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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=this.insertChildren(t,[e],n);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];r&&o._type!==r?n.splice(a,1):o._remove(!0)}e.splice(s,n,t,0);for(var a=0,h=n.length;h>a;a++){var o=n[a];o._parent=this,o._setProject(this._project),o._name&&o.setName(o._name)}this._changed(7)}else n=null;return n},_insert:function(t,e,n){if(!e._parent)return null;var i=e._index+(t?1:0);return e._parent===this._parent&&i>this._index&&i--,e._parent.insertChild(i,this,n)},insertAbove:function(t,e){return this._insert(!0,t,e)},insertBelow:function(t,e){return this._insert(!1,t,e)},sendToBack:function(){return this._parent.insertChild(0,this)},bringToFront:function(){return this._parent.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];return t.insertAbove(this),t.setStyle(this._style),this.remove(),t}return this},_removeNamed:function(){var t=this._parent._children,e=this._parent._namedChildren,n=this._name,i=e[n],r=i?i.indexOf(this):-1;-1!=r&&(t[n]==this&&delete t[n],i.splice(r,1),i.length?t[n]=i[i.length-1]:delete e[n])},_remove:function(t){return this._parent?(this._name&&this._removeNamed(),null!=this._index&&e.splice(this._parent._children,null,this._index,1),this.responds("frame")&&this._animateItem(!1),t&&this._parent._changed(7),this._parent=null,!0):!1},remove:function(){return this._remove(!0)},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(!1);return i.length>0&&this._changed(7),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(7)}},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},_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},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|compound-path)$/.test(e._type)&&t.isDescendant(e))return!0;e=e._parent}return!1},scale:function(t,e,n){return(arguments.length<2||"object"==typeof e)&&(n=e,e=t),this.transform((new g).scale(t,e,n||this.getPosition(!0)))},translate:function(){var t=new g;return this.transform(t.translate.apply(t,arguments))},rotate:function(t,e){return this.transform((new g).rotate(t,e||this.getPosition(!0)))},shear:function(t,e,n){return(arguments.length<2||"object"==typeof e)&&(n=e,e=t),this.transform((new g).shear(t,e,n||this.getPosition(!0)))},transform:function(t){var e=this._bounds,n=this._position;if(this._matrix.preConcatenate(t),(this._transformContent||arguments[1])&&this.applyMatrix(!0),this._changed(5),e&&0===t.getRotation()%90){for(var i in e){var r=e[i];t._transformBounds(r,r)}var s=this._boundsGetter,r=e[s&&s.getBounds||s||"getBounds"];r&&(this._position=r.getCenter(!0)),this._bounds=e}else n&&(this._position=t._transformPoint(n,n));return this},_applyMatrix:function(t,e){var n=this._children;if(n&&n.length>0){for(var i=0,r=n.length;r>i;i++)n[i].transform(t,e);return!0}},applyMatrix:function(t){var e=this._matrix;if(this._applyMatrix(e,!0)){var n=this._style,i=n.getFillColor(!0),r=n.getStrokeColor(!0);i&&i.transform(e),r&&r.transform(e),e.reset()}t||this._changed(5)},globalToLocal:function(){var t=this.getGlobalMatrix();return t&&t._transformPoint(h.read(arguments))},localToGlobal:function(){var t=this.getGlobalMatrix();return t&&t._inverseTransform(h.read(arguments))},fitBounds:function(t,e){t=f.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 f(new h,new c(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){if(this._visible&&0!==this._opacity){this._drawCount=this._project._drawCount;var n=e.trackTransforms,i=e.transforms,r=i[i.length-1],s=r.clone().concatenate(this._matrix);n&&i.push(this._globalMatrix=s);var a,o,h,u=this._blendMode,l=this._opacity,d="normal"===u,f=te.nativeModes[u],_=d&&1===l||(f||d&&1>l)&&this._canComposite();if(!_){var g=this.getStrokeBounds(r);if(!g.width||!g.height)return;h=e.offset,o=e.offset=g.getTopLeft().floor(),a=t,t=Q.getContext(g.getSize().ceil().add(new c(1,1)),e.ratio)}t.save(),_?(t.globalAlpha=l,f&&(t.globalCompositeOperation=u)):t.translate(-o.x,-o.y),(_?this._matrix:s).applyToContext(t),!_&&e.clipItem&&e.clipItem.draw(t,e.extend({clip:!0})),this._draw(t,e),t.restore(),n&&i.pop(),e.clip&&t.clip(),_||(te.process(u,t,a,l,o.subtract(h).multiply(e.ratio)),Q.release(t),e.offset=h)}},_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}})),w=y.extend({_class:"Group",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},_changed:function le(t){le.base.call(this,t),2&t&&this._transformContent&&!this._matrix.isIdentity()&&this.applyMatrix(),258&t&&delete this._clipItem},_getClipItem:function(){if(this._clipItem!==t)return this._clipItem;for(var e=0,n=this._children.length;n>e;e++){var i=this._children[e];if(i._clipMask)return this._clipItem=i}return this._clipItem=null},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clipItem=this._getClipItem();n&&n.draw(t,e.extend({clip:!0}));for(var i=0,r=this._children.length;r>i;i++){var s=this._children[i];s!==n&&s.draw(t,e)}e.clipItem=null}}),x=w.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,w.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function ce(t){return this._parent?ce.base.call(this,t):null!=this._index?(this._project.activeLayer===this&&(this._project.activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(this._project.layers,null,this._index,1),this._project._needsRedraw=!0,!0):!1},getNextSibling:function de(){return this._parent?de.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function fe(){return this._parent?fe.base.call(this):this._project.layers[this._index-1]||null},isInserted:function _e(){return this._parent?_e.base.call(this):null!=this._index},activate:function(){this._project.activeLayer=this},_insert:function ge(t,n,i){return n instanceof x&&!n._parent?(this._remove(!0),e.splice(n._project.layers,[this],n._index+(t?1:0),0),this._setProject(n._project),this):ge.base.call(this,t,n,i)}}),b=y.extend({_class:"Shape",_transformContent:!1,_boundsSelected:!0,initialize:function(t,e,n,i,r){this._shape=t,this._size=n,this._radius=i,this._initialize(r,e)},_equals:function(t){return this._shape===t._shape&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){return this._clone(new b(this._shape,this.getPosition(!0),this._size.clone(),this._radius.clone?this._radius.clone():this._radius,{insert:!1}),t)},getShape:function(){return this._shape},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=this._shape,e=c.read(arguments);if(!this._size.equals(e)){var n=e.width,i=e.height;if("rectangle"===t){var r=c.min(this._radius,e.divide(2));this._radius.set(r.width,r.height)}else"circle"===t?(n=i=(n+i)/2,this._radius=n/2):"ellipse"===t&&this._radius.set(n/2,i/2);this._size.set(n,i),this._changed(5)}},getRadius:function(){var t=this._radius;return"circle"===this._shape?t:new d(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._shape;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else{if(t=c.read(arguments),this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=c.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)}this._changed(5)},isEmpty:function(){return!1},toPath:function(n){var i=new(O[e.capitalize(this._shape)])({center:new h,size:this._size,radius:this._radius,insert:!1});return i.setStyle(this._style),i.transform(this._matrix),(n||n===t)&&i.insertAbove(this),i},_draw:function(t,e){var n=this._style,i=n.hasFill(),r=n.hasStroke(),s=e.clip;if(i||r||s){var a=this._radius,h=this._shape;if(t.beginPath(),"circle"===h)t.arc(0,0,a,0,2*Math.PI,!0);else{var u=a.width,l=a.height,c=o.KAPPA;if("ellipse"===h){var d=u*c,f=l*c;t.moveTo(-u,0),t.bezierCurveTo(-u,-f,-d,-l,0,-l),t.bezierCurveTo(d,-l,u,-f,u,0),t.bezierCurveTo(u,f,d,l,0,l),t.bezierCurveTo(-d,l,-u,f,-u,0)}else{var _=this._size,g=_.width,p=_.height;if(0===u&&0===l)t.rect(-g/2,-p/2,g,p);else{c=1-c;var v=g/2,m=p/2,d=u*c,f=l*c;t.moveTo(-v,-m+l),t.bezierCurveTo(-v,-m+f,-v+d,-m,-v+u,-m),t.lineTo(v-u,-m),t.bezierCurveTo(v-d,-m,v,-m+f,v,-m+l),t.lineTo(v,m-l),t.bezierCurveTo(v,m-f,v-d,m,v-u,m),t.lineTo(-v+u,m),t.bezierCurveTo(-v+d,m,-v,m-f,-v,m-l)}}}t.closePath()}s||!i&&!r||(this._setStyles(t),i&&(t.fill(n.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),r&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new f(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 f(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._shape){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},_hitTest:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._shape,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 f(this._size).setCenter(0,0),d=c.expand(h),_=c.expand(-h);s=d._containsPoint(n)&&!_._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new P("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){return new b(t,n,i,r,e.getNamed(s))}return{Circle:function(){var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t("circle",n,new c(2*i),i,arguments)},Rectangle:function(){var e=f.readNamed(arguments,"rectangle"),n=c.min(c.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=b._readEllipse(arguments);return radius=e.radius,t("ellipse",e.center,radius.multiply(2),radius,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=h.readNamed(t,"center"),i=c.readNamed(t,"radius");else{var r=f.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),C=y.extend({_class:"Raster",_transformContent:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{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 c)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e={insert:!1},n=this._image;if(n)e.image=n;else if(this._canvas){var i=e.canvas=Q.getCanvas(this._size);i.getContext("2d").drawImage(this._canvas,0,0)}return this._clone(new C(e),t)},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(!this._size.equals(t)){var e=this.getElement();this.setCanvas(Q.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}},getWidth:function(){return this._size.width},getHeight:function(){return this._size.height},isEmpty:function(){return 0==this._size.width&&0==this._size.height},getPpi: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 c(72/n.getLength(),72/i.getLength())},getImage:function(){return this._image},setImage:function(t){this._canvas&&Q.release(this._canvas),t.getContext?(this._image=null,this._canvas=t):(this._image=t,this._canvas=null),this._size=new c(t.naturalWidth||t.width,t.naturalHeight||t.height),this._context=null,this._changed(133) -},getCanvas:function(){if(!this._canvas){var t=Q.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){Q.release(t)}}return this._canvas},setCanvas:"#setImage",getContext:function(){return this._context||(this._context=this.getCanvas().getContext("2d")),arguments[0]&&(this._image=null,this._changed(129)),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._project.view;t&&(paper=t._scope),n.fire("load"),t&&t.draw(!0)}var n=this,i=document.getElementById(t)||new Image;i.naturalWidth&&i.naturalHeight?setTimeout(e,0):(V.add(i,{load:function(){n.setImage(i),e()}}),i.src||(i.src=t)),this.setImage(i)},getElement:function(){return this._canvas||this._image},getSubCanvas:function(t){t=f.read(arguments);var e=Q.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(t){t=f.read(arguments);var e=new C({canvas:this.getSubCanvas(t),insert:!1});return 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,e){e=h.read(arguments,1),this.getContext(!0).drawImage(t,e.x,e.y)},getAverageColor:function(t){var n,i;t?t instanceof I?(i=t,n=t.getBounds()):t.width?n=new f(t):t.x&&(n=new f(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=C._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=C._sampleContext=Q.getContext(new c(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,transforms:[h]})),this._matrix.applyToContext(o),o.drawImage(this.getElement(),-this._size.width/2,-this._size.height/2),o.restore();for(var u=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,l=[0,0,0],d=0,_=0,p=u.length;p>_;_+=4){var v=u[_+3];d+=v,v/=255,l[0]+=u[_]*v,l[1]+=u[_+1]*v,l[2]+=u[_+2]*v}for(var _=0;3>_;_++)l[_]/=d;return d?D.read(l):null},getPixel:function(t){t=h.read(arguments);var e=this.getContext().getImageData(t.x,t.y,1,1).data;return new D("rgb",[e[0]/255,e[1]/255,e[2]/255],e[3]/255)},setPixel:function(){var t=h.read(arguments),e=D.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(t){return t=c.read(arguments),this.getContext().createImageData(t.width,t.height)},getImageData:function(t){return t=f.read(arguments),t.isEmpty()&&(t=new f(this._size)),this.getContext().getImageData(t.x,t.y,t.width,t.height)},setImageData:function(t,e){e=h.read(arguments,1),this.getContext(!0).putImageData(t,e.x,e.y)},_getBounds:function(t,e){var n=new f(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTest:function(t){if(this._contains(t)){var e=this;return new P("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}}),S=y.extend({_class:"PlacedSymbol",_transformContent:!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 m?e:new m(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol&&delete this._symbol._instances[this._id],this._symbol=t,t._instances[this._id]=this},clone:function(t){return this._clone(new S({symbol:this.symbol,insert:!1}),t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e){return this.symbol._definition._getCachedBounds(t,e)},_hitTest:function(t,e,n){var i=this._symbol._definition._hitTest(t,e,n);return i&&(i.item=this),i},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),P=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 t&&t._merged?t:new e({type:null,tolerance:paper.project.options.hitTolerance||2,fill:!t,stroke:!t,segments:!t,handles:!1,ends:!1,center:!1,bounds:!1,guides:!1,selected:!1,_merged:!0},t)}}}),k=e.extend({_class:"Segment",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 z(o,this,"_point"),new z(h,this,"_handleIn"),new z(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.isLinear()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){if(this._path){var e,n=this._path._curves&&this.getCurve();n&&(n._changed(),(e=n[t==this._point||t==this._handleIn&&n._segment1==this?"getPrevious":"getNext"]())&&e._changed()),this._path._changed(5)}},getPoint:function(){return this._point},setPoint:function(t){t=h.read(arguments),this._point.set(t.x,t.y)},getHandleIn:function(){return this._handleIn},setHandleIn:function(t){t=h.read(arguments),this._handleIn.set(t.x,t.y)},getHandleOut:function(){return this._handleOut},setHandleOut:function(t){t=h.read(arguments),this._handleOut.set(t.x,t.y)},isLinear:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},setLinear:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},isColinear:function(t){var e=this.getNext(),n=t.getNext();return this._handleOut.isZero()&&e._handleIn.isZero()&&t._handleOut.isZero()&&n._handleIn.isZero()&&e._point.subtract(this._point).isColinear(n._point.subtract(t._point))},isOrthogonal:function(){var t=this.getPrevious(),e=this.getNext();return t._handleOut.isZero()&&this._handleIn.isZero()&&this._handleOut.isZero()&&e._handleIn.isZero()&&this._point.subtract(t._point).isOrthogonal(e._point.subtract(this._point))},isArc:function(){var t=this.getNext(),e=this._handleOut,n=t._handleIn,i=o.KAPPA;if(e.isOrthogonal(n)){var r=this._point,s=t._point,a=new p(r,e,!0).intersect(new p(s,n,!0),!0);return a&&o.isZero(e.getLength()/a.subtract(r).getLength()-i)&&o.isZero(n.getLength()/a.subtract(s).getLength()-i)}return!1},_isSelected:function(t){var e=this._selectionState;return t==this._point?!!(4&e):t==this._handleIn?!!(1&e):t==this._handleOut?!!(2&e):!1},_setSelected:function(t,e){var n=this._path,e=!!e,i=this._selectionState||0,r=[!!(4&i),!!(1&i),!!(2&i)];if(t===this._point){if(e)r[1]=r[2]=!1;else{var s=this.getPrevious(),a=this.getNext();r[1]=s&&(s._point.isSelected()||s._handleOut.isSelected()),r[2]=a&&(a._point.isSelected()||a._handleIn.isSelected())}r[0]=e}else{var o=t===this._handleIn?1:2;r[o]!=e&&(e&&(r[0]=!1),r[o]=e)}this._selectionState=(r[0]?4:0)|(r[1]?1:0)|(r[2]?2:0),n&&i!=this._selectionState&&(n._updateSelection(this,i,this._selectionState),n._changed(33))},isSelected:function(){return this._isSelected(this._point)},setSelected:function(t){this._setSelected(this._point,t)},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?(t._closed||e!=t._segments.length-1||e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new A(t,t.getNext()?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 k(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new k(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(", ")+" }"},_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,0,e,0,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}}),z=h.extend({initialize:function(e,n,i){var r,s,a;e?(r=e[0])!==t?s=e[1]:((r=e.x)===t&&(e=h.read(arguments),r=e.x),s=e.y,a=e.selected):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 o.isZero(this._x)&&o.isZero(this._y)},setSelected:function(t){this._owner._setSelected(this,t)},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 k,this._segment2=new k;else if(1===h)this._segment1=new k(t.segment1),this._segment2=new k(t.segment2);else if(2===h)this._segment1=new k(t),this._segment2=new k(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 k(u,null,l),this._segment2=new k(d,c,null)}},_changed:function(){delete this._length,delete this._bounds},getPoint1:function(){return this._segment1._point},setPoint1:function(t){t=h.read(arguments),this._segment1._point.set(t.x,t.y)},getPoint2:function(){return this._segment2._point},setPoint2:function(t){t=h.read(arguments),this._segment2._point.set(t.x,t.y)},getHandle1:function(){return this._segment1._handleOut},setHandle1:function(t){t=h.read(arguments),this._segment1._handleOut.set(t.x,t.y)},getHandle2:function(){return this._segment2._handleIn},setHandle2:function(t){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.getHandle1().isSelected()&&this.getHandle2().isSelected()},setSelected:function(t){this.getHandle1().setSelected(t),this.getHandle2().setSelected(t)},getValues:function(){return M.getValues(this._segment1,this._segment2)},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(){var t=arguments[0],e=arguments[1],n=0===arguments.length||0===t&&1===e;if(n&&null!=this._length)return this._length;var i=M.getLength(this.getValues(),t,e);return n&&(this._length=i),i},getArea:function(){return M.getArea(this.getValues())},getPart:function(t,e){return new M(M.getPart(this.getValues(),t,e))},isLinear:function(){return this._segment1._handleOut.isZero()&&this._segment2._handleIn.isZero()},getIntersections:function(t){return M.getIntersections(this.getValues(),t.getValues(),this,t,[])},reverse:function(){return new M(this._segment2.reverse(),this._segment1.reverse())},_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){var n=this._getParameter(t,e),i=1e-5,r=null;if(n>i&&1-i>n){var s=M.subdivide(this.getValues(),n),a=this.isLinear(),o=s[0],u=s[1];a||(this._segment1._handleOut.set(o[2]-o[0],o[3]-o[1]),this._segment2._handleIn.set(u[4]-u[6],u[5]-u[7]));var l=o[6],c=o[7],d=new k(new h(l,c),!a&&new h(o[4]-l,o[5]-c),!a&&new h(u[2]-l,u[3]-c));if(this._path)this._segment1._index>0&&0===this._segment2._index?this._path.add(d):this._path.insert(this._segment2._index,d),r=this;else{var f=this._segment2;this._segment2=d,r=new M(d,f)}}return r},split:function(t,e){return this._path?this._path.split(this._segment1._index,this._getParameter(t,e)):null},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){var n=t._point,i=t._handleOut,r=e._handleIn,s=e._point;return[n._x,n._y,n._x+i._x,n._y+i._y,s._x+r._x,s._y+r._y,s._x,s._y]},evaluate:function(t,e,n){var i,r,s=t[0],a=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],f=t[7];if(0!==n||0!==e&&1!==e){var _=3*(o-s),g=3*(l-o)-_,p=d-s-_-g,v=3*(u-a),m=3*(c-u)-v,y=f-a-v-m;if(0===n)i=((p*e+g)*e+_)*e+s,r=((y*e+m)*e+v)*e+a;else{var w=1e-5;if(w>e&&o===s&&u===a||e>1-w&&l===d&&c===f?(i=d-s,r=f-a):(i=(3*p*e+2*g)*e+_,r=(3*y*e+2*m)*e+v),3===n){var x=6*p*e+2*g,b=6*y*e+2*m;return(i*b-r*x)/Math.pow(i*i+r*r,1.5)}}}else i=0===e?s:d,r=0===e?a:f;return 2==n?new h(r,-i):new h(i,r)},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 a=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-a),d=3*(u-h)-c,f=l-a-c-d;return o.solveCubic(f,d,c,a-n,i,r,s)},getParameterOf:function(t,e,n){if(Math.abs(t[0]-e)<1e-5&&Math.abs(t[1]-n)<1e-5)return 0;if(Math.abs(t[6]-e)<1e-5&&Math.abs(t[7]-n)<1e-5)return 1;for(var i,r,s=[],a=[],o=M.solveCubic(t,0,e,s),h=M.solveCubic(t,1,n,a),u=0;-1==o||o>u;)if(-1==o||(i=s[u++])>=0&&1>=i){for(var l=0;-1==h||h>l;)if((-1==h||(r=a[l++])>=0&&1>=r)&&(-1==o?i=r:-1==h&&(r=i),Math.abs(i-r)<1e-5))return.5*(i+r);if(-1==o)break}return null},getPart:function(t,e,n){return e>0&&(t=M.subdivide(t,e)[1]),1>n&&(t=M.subdivide(t,(n-e)/(1-e))[0]),t},isLinear:function(t){var e=o.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[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},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 f(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,a,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,_=o.solveQuadratic(c,d,f,u),g=1e-5,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)}},_getWinding:function(t,e,n,i,r,s){function a(t){var e=t[1],n=t[7],r=e>n?-1:1;return 1===r&&(e>i||i>n)||-1===r&&(n>i||i>e)?0:r}var h=1e-5,u=Math.abs;if(M.isLinear(t)){var l=a(t);if(!l)return 0;var c=(t[6]-t[0])*(i-t[1])-(t[7]-t[1])*(n-t[0]);return(-h>c?-1:1)==l?0:l}for(var d,f=t[1],_=t[3],g=t[5],p=t[7],v=3*(_-g)-f+p,m=2*(f+g)-4*_,y=_-f,w=o.solveQuadratic(v,m,y,r,h,1-h),x=t,b=r[0],C=0,S=0;w>=S;S++){if(S===w)d=x;else{var P=M.subdivide(x,b);d=P[0],x=P[1],b=r[S],b=(r[S+1]-b)/(1-b)}S>0&&(d[3]=d[1]),w>S&&(d[5]=x[1]);var l=a(d);if(l){var k,z;if(1===M.solveCubic(d,1,i,s,-h,1+-h))k=s[0],z=M.evaluate(d,k,0).x;else{var A=(d[1]+d[7])/2;if(k=A>i&&l>0?0:1,1===k&&i==d[7])continue;z=0===k?d[0]:d[6]}var I=M.evaluate(d,k,1).y,O=u(I)k&&M.evaluate(e,1,1).y*I<0;!(n>=z+(O?-h:h*l))||O&&(u(k)h||u(k-1)h)||(C+=O&&u(k-(l>0?1:0))e?1:0)},getParameterOf:function(t){return t=h.read(arguments),M.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){return e||(t=this.getParameterAt(t)),new A(this,t)},getLocationOf:function(t){t=h.read(arguments);var e=this.getParameterOf(t);return null!=e?new A(this,e):null},getNearestLocation:function(t){function e(e){if(e>=0&&1>=e){var i=t.getDistance(M.evaluate(n,e,0),!0);if(s>i)return s=i,a=e,!0}}t=h.read(arguments);for(var n=this.getValues(),i=100,r=o.TOLERANCE,s=1/0,a=0,u=0;i>=u;u++)e(u/i);for(var l=1/(2*i);l>r;)e(a-l)||e(a+l)||(l/=2);var c=M.evaluate(n,a,0);return new A(this,a,c,null,null,null,t.getDistance(c))},getNearestPoint:function(t){return t=h.read(arguments),this.getNearestLocation(t).getPoint()}}),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))))}return{statics:!0,getLength:function(i,r,s){r===t&&(r=0),s===t&&(s=1);var a=o.isZero;if(a(i[0]-i[2])&&a(i[1]-i[3])&&a(i[6]-i[4])&&a(i[7]-i[5])){var h=i[6]-i[0],u=i[7]-i[1];return(s-r)*Math.sqrt(h*h+u*u)}var l=e(i);return o.integrate(l,r,s,n(r,s))},getParameterAt:function(t,i,r){function s(t){var e=n(r,t);return f+=t>r?o.integrate(l,r,t,e):-o.integrate(l,t,r,e),r=t,f-i}if(0===i)return r;var a=i>0,h=a?r:0,u=a?1:r,i=Math.abs(i),l=e(t),c=o.integrate(l,h,u,n(h,u));if(i>=c)return a?u:h;var d=i/c,f=0;return o.findRoot(s,l,a?h+d:u-d,h,u,16,1e-5)}}},new function(){function t(t,e,n,i,r,s,a){var h=t[0],u=t[t.length-1];h&&i.isClose(h._point,o.EPSILON)||u&&i.isClose(u._point,o.EPSILON)||t.push(new A(e,n,i,r,s,a))}function e(i,r,s,a,o,h,u,l){if(l=(l||0)+1,!(l>20)){h=h||[0,1],u=u||[0,1];for(var c=M.getPart(i,h[0],h[1]),d=M.getPart(r,u[0],u[1]),f=0;f++<20;){var _,g=n(c,d,_=u.slice()),p=0;if(0===g)break;if(g>0){if(u=_,d=M.getPart(r,u[0],u[1]),p=n(d,c,_=h.slice()),0===p)break;g>0&&(h=_,c=M.getPart(i,h[0],h[1]))}if(0>g||0>p){if(h[1]-h[0]>u[1]-u[0]){var v=(h[0]+h[1])/2;e(i,r,s,a,o,[h[0],v],u,l),e(i,r,s,a,o,[v,h[1]],u,l);break}var v=(u[0]+u[1])/2;e(i,r,s,a,o,h,[u[0],v],l),e(i,r,s,a,o,h,[v,u[1]],l);break}if(Math.abs(h[1]-h[0])<=1e-5&&Math.abs(u[1]-u[0])<=1e-5){var m=(h[0]+h[1])/2,y=(u[0]+u[1])/2;t(o,s,m,M.evaluate(i,m,0),a,y,M.evaluate(r,y,0));break}}}}function n(t,e,n){var r=t[0],s=t[1],a=t[2],o=t[3],h=t[4],u=t[5],l=t[6],c=t[7],d=e[0],f=e[1],_=e[2],g=e[3],v=e[4],m=e[5],y=e[6],w=e[7],x=p.getSignedDistance,b=x(r,s,l,c,a,o)||0,C=x(r,s,l,c,h,u)||0,S=b*C>0?.75:4/9,P=S*Math.min(0,b,C),k=S*Math.max(0,b,C),z=x(r,s,l,c,d,f),A=x(r,s,l,c,_,g),I=x(r,s,l,c,v,m),O=x(r,s,l,c,y,w);if(P>Math.max(z,A,I,O)||kO&&(L=P,P=k,k=L);for(var E=-1/0,B=1/0,N=-1/0,D=0,j=T.length;j>D;D++){var F=T[D],R=T[(D+1)%j];R[1]=V&&H>=P){var W=q+(P-V)/U;B>W&&(B=W),W>E&&(E=W)}if(k>=V&&H>=k){var W=q+(k-V)/U;W>N&&(N=W),B>W&&(B=0)}}if(1/0!==B&&N!==-1/0){var X=Math.min(P,k),G=Math.max(P,k);O>X&&G>O&&(N=1),z>X&&G>z&&(B=0),E>N&&(N=1);var J=n[0],$=n[1]-J;if(n[0]=J+B*$,n[1]=J+N*$,($-(n[1]-n[0]))/$>=.2)return 1}return M.getBounds(t).touches(M.getBounds(e))?-1:0}function i(t,e,n,i){var r=[0,t],s=[1/3,e],a=[2/3,n],o=[1,i],h=p.getSignedDistance,u=h(0,t,1,i,1/3,e),l=h(0,t,1,i,2/3,n);if(0>u*l)return[r,s,o,a];var c,d;return Math.abs(u)>Math.abs(l)?(c=s,d=(i-n-(i-t)/3)*(2*(i-n)-i+e)/3):(c=a,d=(e-t+(t-i)/3)*(-2*(t-e)+t-n)/3),0>d?[r,c,o]:[r,s,a,o]}function r(e,n,i,r,s){for(var a=M.isLinear(e),o=a?n:e,h=a?e:n,u=h[0],l=h[1],c=h[6],d=h[7],f=c-u,_=d-l,g=Math.atan2(-_,f),p=Math.sin(g),v=Math.cos(g),m=f*v-_*p,y=[0,0,0,0,m,0,m,0],w=[],x=0;8>x;x+=2){var b=o[x]-u,C=o[x+1]-l;w.push(b*v-C*p,C*v+b*p)}for(var S=[],P=M.solveCubic(w,1,0,S,0,1),x=0;P>x;x++){var k=S[x],b=M.evaluate(w,k,0).x;if(b>=0&&m>=b){var z=M.getParameterOf(y,b,0),A=a?z:k,I=a?k:z;t(s,i,A,M.evaluate(e,A,0),r,I,M.evaluate(n,I,0))}}}function s(e,n,i,r,s){var a=p.intersect(e[0],e[1],e[6],e[7],n[0],n[1],n[6],n[7]);a&&t(s,i,null,a,r)}return{statics:{getIntersections:function(n,i,a,o,h){var u=M.isLinear(n),l=M.isLinear(i),c=a.getPoint1(),d=a.getPoint2(),f=o.getPoint1(),_=o.getPoint2(),g=1e-5;return c.isClose(f,g)&&t(h,a,0,c,o,0,c),c.isClose(_,g)&&t(h,a,0,c,o,1,c),(u&&l?s:u||l?r:e)(n,i,a,o,h),d.isClose(f,g)&&t(h,a,1,d,o,0,d),d.isClose(_,g)&&t(h,a,1,d,o,1,d),h}}}}),A=e.extend({_class:"CurveLocation",initialize:function pe(t,e,n,i,r,s,a){this._id=pe._id=(pe._id||0)+1,this._curve=t,this._segment1=t._segment1,this._segment2=t._segment2,this._parameter=e,this._point=n,this._curve2=i,this._parameter2=r,this._point2=s,this._distance=a},getSegment:function(){if(!this._segment){var t=this.getCurve(),e=this.getParameter();if(1===e)this._segment=t._segment2;else if(0===e||arguments[0])this._segment=t._segment1;else{if(null==e)return null;this._segment=t.getLength(0,e)a;a++)s[a]=i[a].getValues();for(var a=0,o=n.length;o>a;a++)for(var h=n[a],u=h.getValues(),l=0;r>l;l++)M.getIntersections(u,s[l],h,i[l],e);return e},setPathData:function(t){function e(t,e,n){var r=parseFloat(i[t]);return a&&(r+=o[e]),n&&(o[e]=r),r}function n(t,n){return new h(e(t,"x",n),e(t+1,"y",n))}var i,r,s=t.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/gi),a=!1,o=new h;this.clear();for(var u=0,l=s.length;l>u;u++){var c=s[u],d=c[0],f=d.toLowerCase();i=c.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g);var _=i&&i.length;switch(a=d===f,f){case"m":case"l":for(var g=0;_>g;g+=2)this[0===g&&"m"===f?"moveTo":"lineTo"](n(g,!0));r=o;break;case"h":case"v":for(var p="h"==f?"x":"y",g=0;_>g;g++)e(g,p,!0),this.lineTo(o);r=o;break;case"c":for(var g=0;_>g;g+=6)this.cubicCurveTo(n(g),r=n(g+2),n(g+4,!0));break;case"s":for(var g=0;_>g;g+=4)this.cubicCurveTo(o.multiply(2).subtract(r),r=n(g),n(g+2,!0));break;case"q":for(var g=0;_>g;g+=4)this.quadraticCurveTo(r=n(g),n(g+2,!0));break;case"t":for(var g=0;_>g;g+=2)this.quadraticCurveTo(r=o.multiply(2).subtract(r),n(g,!0));break;case"a":break;case"z":this.closePath()}}},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_contains:function(t){var e=this._getWinding(t);return!!("evenodd"===this.getWindingRule()?1&e:e)}}),O=I.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[];var n=Array.isArray(e)?"object"==typeof e[0]?e:arguments:e&&(e.point!==t&&e.size===t||e.x!==t)?arguments:null;this.setSegments(n||[]),this._initialize(!n&&e)},_equals:function(t){return e.equals(this._segments,t._segments)},clone:function(e){var n=this._clone(new O({segments:this._segments,insert:!1}),e);return n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),n},_changed:function ve(t){if(ve.base.call(this,t),4&t){if(delete this._length,delete this._clockwise,this._curves)for(var e=0,n=this._curves.length;n>e;e++)this._curves[e]._changed(5)}else 8&t&&delete this._bounds},getSegments:function(){return this._segments},setSegments:function(t){var e=this.isFullySelected();this._segments.length=0,this._selectedSegmentState=0,delete this._curves,this._add(k.readAll(t)),e&&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]},getPathData:function(){function t(t,e,s){var a=t._point,o=e._point,h=t._handleOut,u=e._handleIn;if(h.isZero()&&u.isZero())s||r.push("L"+i.point(o,n));else{var l=o.subtract(a);r.push("c"+i.point(h,n)+" "+i.point(l.add(u),n)+" "+i.point(l,n))}}var e=this._segments,n=arguments[0],i=a.instance,r=[];if(0===e.length)return"";r.push("M"+i.point(e[0]._point));for(var s=0,o=e.length-1;o>s;s++)t(e[s],e[s+1],!1);return this._closed&&(t(e[e.length-1],e[0],!0),r.push("z")),r.join("")},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(5)}},isEmpty:function(){return 0===this._segments.length},isPolygon:function(){for(var t=0,e=this._segments.length;e>t;t++)if(!this._segments[t].isLinear())return!1;return!0},_applyMatrix: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(5),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=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0]),(n=r[e])&&(n._segment1=i[e])},_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(k.readAll(arguments)):this._add([k.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(k.readAll(arguments,1),t):this._add([k.read(arguments,1)],t)[0]},addSegment:function(){return this._add([k.read(arguments)])[0]},insertSegment:function(t){return this._add([k.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(k.readAll(t))},insertSegments:function(t,e){return this._add(k.readAll(e),t)},removeSegment:function(t){return this.removeSegments(t,t+1)[0]||null},removeSegments:function(t,n){t=t||0,n=e.pick(n,this._segments.length);var i=this._segments,r=this._curves,s=i.length,a=i.splice(t,n-t),o=a.length;if(!o)return a;for(var h=0;o>h;h++){var u=a[h];u._selectionState&&this._updateSelection(u,u._selectionState,0),delete u._index,delete u._path}for(var h=t,l=i.length;l>h;h++)i[h]._index=h;if(r){var c=t>0&&n===s+(this._closed?1:0)?t-1:t,r=r.splice(c,o);arguments[2]&&(a._curves=r.slice(1)),this._adjustCurves(c,c)}return this._changed(5),a},clear:"#removeSegments",isFullySelected:function(){var t=this._segments.length;return this._selected&&t>0&&this._selectedSegmentState===4*t},setFullySelected:function(t){t&&this._selectSegments(!0),this.setSelected(t)},setSelected:function me(t){t||this._selectSegments(!1),me.base.call(this,t)},_selectSegments:function(t){var e=this._segments.length;this._selectedSegmentState=t?4*e:0;for(var n=0;e>n;n++)this._segments[n]._selectionState=t?4: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),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new k(e.evaluate(n,0))),n+=i;this.setSegments(s)},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){if(1===arguments.length){var n=t;"number"==typeof n&&(n=this.getLocationAt(n)),t=n.index,e=n.parameter}e>=1&&(t++,e--);var i=this.getCurves();if(t>=0&&t0&&i[t++].divide(e,!0);var r,s=this.removeSegments(t,this._segments.length,!0);return this._closed?(this.setClosed(!1),r=this):t>0&&(r=this._clone((new O).insertAbove(this,!0))),r._add(s,0),this.addSegment(s[0]),r}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,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}delete this._curves,this._clockwise!==t&&(this._clockwise=!this._clockwise)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();n._point.equals(i._point)&&t.reverse();var r,s=t.getFirstSegment(); -return n._point.equals(s._point)?(n.setHandleOut(s._handleOut),this._add(e.slice(1))):(r=this.getFirstSegment(),r._point.equals(s._point)&&t.reverse(),i=t.getLastSegment(),r._point.equals(i._point)?(r.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())),t.closed&&this._add([e[0]]),t.remove(),r=this.getFirstSegment(),n=this.getLastSegment(),n._point.equals(r._point)&&(r.setHandleIn(n._handleIn),n.remove(),this.setClosed(!0)),this._changed(5),!0}return!1},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},_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];return i+s.getLength(0,t.getParameter())}return null},getLocationOf:function(t){t=h.read(arguments);for(var e=this.getCurves(),n=0,i=e.length;i>n;n++){var r=e[n].getLocationOf(t);if(r)return r}return null},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t;return n[r].getLocationAt(t-r,!0)}for(var s=0,a=n.length;a>s;s++){var o=i,h=n[s];if(i+=h.getLength(),i>=t)return h.getLocationAt(t-o)}return t<=this.getLength()?new A(n[n.length-1],1):null},getPointAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getPoint()},getTangentAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getTangent()},getNormalAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getNormal()},getNearestLocation:function(t){t=h.read(arguments);for(var e=this.getCurves(),n=1/0,i=null,r=0,s=e.length;s>r;r++){var a=e[r].getNearestLocation(t);a._distanceu;u++){var c=n[u].getValues(),d=c[0],f=c[1];(d!==c[2]||f!==c[3]||d!==c[4]||f!==c[5]||d!==c[6]||f!==c[7])&&(r+=M._getWinding(c,h,t.x,t.y,s,a),h=c)}return e||(r+=M._getWinding(o,h,t.x,t.y,s,a)),r},_hitTest:function(e,n){function i(n,i,r){return e.getDistance(i)o;o++){var h=a(o);s+=M._getWinding(h,n,t.x,t.y,i,r),n=h}return!!s}function h(t){return("round"!==u||"round"!==l)&&(d=[],v||t._index>0&&t._index0)?o(e):e.getDistance(t._point)<=y}var u,l,c,d,f,_,g=this.getStyle(),p=this._segments,v=this._closed,m=n.tolerance,y=0,w=this;if(n.stroke&&(y=g.getStrokeWidth()/2,y>0?(u=g.getStrokeJoin(),l=g.getStrokeCap(),c=y*g.getMiterLimit()):u=l="round",y+=m),!n.ends||n.segments||v){if(n.segments||n.handles)for(var x=0,b=p.length;b>x;x++)if(_=r(p[x]))return _}else if(_=r(p[0],!0)||r(p[p.length-1],!0))return _;if(y>0){if(f=this.getNearestLocation(e)){var C=f.getParameter();0===C||1===C?h(f.getSegment())||(f=null):f._distance>y&&(f=null)}if(!f&&"miter"===u)for(var x=0,b=p.length;b>x;x++){var S=p[x];if(e.getDistance(S._point)<=c&&h(S)){f=S.getLocation();break}}}return!f&&n.fill&&this.hasFill()&&this._contains(e)?new P("fill",this):f?new P("stroke",this,{location:f}):null}},new function(){function t(t,e,n,i){function r(e){var n=a[e],i=a[e+1];(d!=n||f!=i)&&(t.beginPath(),t.moveTo(d,f),t.lineTo(n,i),t.stroke(),t.beginPath(),t.arc(n,i,s,0,2*Math.PI,!0),t.fill())}for(var s=i/2,a=Array(6),o=0,h=e.length;h>o;o++){var u=e[o];u._transformCoordinates(n,a,!1);var l=u._selectionState,c=4&l,d=a[0],f=a[1];(c||1&l)&&r(2),(c||2&l)&&r(4),t.save(),t.beginPath(),t.rect(d-s,f-s,i,i),t.fill(),c||(t.beginPath(),t.rect(d-s+1,f-s+1,i-2,i-2),t.fillStyle="#ffffff",t.fill()),t.restore()}}function e(t,e,n){function i(e){var i=d[e];if(n)i._transformCoordinates(n,_,!1),r=_[0],s=_[1];else{var f=i._point;r=f._x,s=f._y}if(g)t.moveTo(r,s),g=!1;else{if(n)h=_[2],u=_[3];else{var p=i._handleIn;h=r+p._x,u=s+p._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 p=i._handleOut;l=a+p._x,c=o+p._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(p);e._closed&&f>1&&i(0)}return{_draw:function(t,n){function i(t){return u[(t%l+l)%l]}var r=n.clip,s=n.compound;s||t.beginPath();var a=this.getStyle(),o=a.hasFill(),h=a.hasStroke(),u=a.getDashArray(),l=!paper.support.nativeDash&&h&&u&&u.length;if((o||h&&!l||s||r)&&e(t,this),this._closed&&t.closePath(),!r&&!s&&(o||h)&&(this._setStyles(t),o&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),h)){if(l){t.beginPath();var c,d=new T(this),f=d.length,_=-a.getDashOffset(),g=0;for(_%=f;_>0;)_-=i(g--)+i(g--);for(;f>_;)c=_+i(g++),(_>0||c>0)&&d.drawPart(t,Math.max(_,0),Math.max(c,0)),_=c+i(g++)}t.stroke()}},_drawSelected:function(n,i){n.beginPath(),e(n,this,i),n.stroke(),t(n,this._segments,i,this._project.options.handleSize||4)}}},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(){1===this._segments.length&&this.removeSegment(0),this._segments.length||this._add([new k(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new k(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 k(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=t(this),r=i._point,s=h.read(arguments),a=e.pick(e.peek(arguments),!0);if("boolean"==typeof a)var o=r.add(s).divide(2),n=o.add(o.subtract(r).rotate(a?-90:90));else n=s,s=h.read(arguments);var u=new p(r.add(n).divide(2),n.subtract(r).rotate(90),!0),l=new p(n.add(s).divide(2),s.subtract(n).rotate(90),!0),c=u.intersect(l,!0),d=new p(r,s),f=d.getSide(n);if(!c){if(!f)return this.lineTo(s);throw Error("Cannot put an arc through the given points: "+[r,n,s])}var _=r.subtract(c),g=_.getDirectedAngle(s.subtract(c)),v=d.getSide(c);0==v?g=f*Math.abs(g):f==v&&(g-=360*(0>g?-1:1));for(var m=Math.abs(g),y=m>=360?4:Math.ceil(m/90),w=g/y,x=w*Math.PI/360,b=4/3*Math.sin(x)/(1+Math.cos(x)),C=[],S=0;y>=S;S++){var P=y>S?c.add(_):s,z=y>S?_.rotate(90).multiply(b):null;0==S?i.setHandleOut(z):C.push(new k(P,_.rotate(-90).multiply(b),z)),_=_.rotate(w)}this._add(C)},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(){var t=this.getFirstSegment(),e=this.getLastSegment();t._point.equals(e._point)&&(t.setHandleIn(e._handleIn),e.remove()),this.setClosed(!0)}}},{_getBounds:function(t,e){return O[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,n=0,i=t.length;i>n;n++)for(var r=M.getValues(t[n],t[i>n+1?n+1:0]),s=2;8>s;s+=2)e+=(r[s-2]-r[s])*(r[s+1]+r[s-1]);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 f;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),d=1,_=t.length;_>d;d++)s(t[d]);return e&&s(a),new f(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(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)]}function s(t){_=_.include(i?i._transformPoint(t,t):t)}function a(t,e){"round"===e||!t._handleIn.isZero()&&!t._handleOut.isZero()?_=_.unite(m.setCenter(i?i._transformPoint(t._point):t._point)):O._addSquareJoin(t,e,l,v,s)}function o(t,e){switch(e){case"round":a(t,e);break;case"butt":case"square":O._addSquareCap(t,e,l,s)}}if(!n.hasStroke())return O.getBounds(t,e,n,i);for(var u=t.length-(e?0:1),l=n.getStrokeWidth()/2,d=r(l,i),_=O.getBounds(t,e,n,i,d),g=n.getStrokeJoin(),p=n.getStrokeCap(),v=l*n.getMiterLimit(),m=new f(new c(d).multiply(2)),y=1;u>y;y++)a(t[y],g);return e?a(t[0],g):(o(t[0],p),o(t[t.length-1],p)),_},_addSquareJoin: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 p(u.add(l),new h(-l.y,l.x),!0).intersect(new p(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().normalize(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){var a=Array(6),o=1/0,h=-o,u=o,l=h;r=r/2||0,s=s/2||0;for(var c=0,d=t.length;d>c;c++){var _=t[c];_._transformCoordinates(i,a,!1);for(var g=0;6>g;g+=2){var p=0==g?s:r,v=a[g],m=a[g+1],y=v-p,w=v+p,x=m-p,b=m+p;o>y&&(o=y),w>h&&(h=w),u>x&&(u=x),b>l&&(l=b)}}return new f(o,u,h-o,l-u)},getRoughBounds:function(t,e,n,i){var r=n.getStrokeColor()?n.getStrokeWidth():0,s=r;return 0===r?r=1e-5:("miter"===n.getStrokeJoin()&&(s=r*n.getMiterLimit()),"square"===n.getStrokeCap()&&(s=Math.max(s,r*Math.sqrt(2)))),O.getHandleBounds(t,e,n,i,r,s)}}});O.inject({statics:new function(){function t(t,n,r){for(var s=new O,a=Array(4),o=0;4>o;o++){var h=i[o];a[o]=new k(h._point.multiply(n).add(t),h._handleIn.multiply(n),h._handleOut.multiply(n))}return s._add(a),s._closed=!0,s.set(e.getNamed(r))}var n=o.KAPPA,i=[new k([-1,0],[0,n],[0,-n]),new k([0,-1],[-n,0],[n,0]),new k([1,0],[0,-n],[0,n]),new k([0,1],[n,0],[-n,0])];return{Line:function(){return new O(h.readNamed(arguments,"from"),h.readNamed(arguments,"to")).set(e.getNamed(arguments))},Circle:function(){var n=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return t(n,new c(i),arguments)},Rectangle:function(){var t=f.readNamed(arguments,"rectangle"),i=c.readNamed(arguments,"radius",0,0,{readNull:!0}),r=t.getBottomLeft(!0),s=t.getTopLeft(!0),a=t.getTopRight(!0),o=t.getBottomRight(!0);if(path=new O,!i||i.isZero())path._add([new k(r),new k(s),new k(a),new k(o)]);else{i=c.min(i,t.getSize(!0).divide(2));var h=i.width,u=i.height,l=h*n,d=u*n;path._add([new k(r.add(h,0),null,[-l,0]),new k(r.subtract(0,u),[0,d]),new k(s.add(0,u),null,[0,-d]),new k(s.add(h,0),[-l,0],null),new k(a.subtract(h,0),null,[l,0]),new k(a.add(0,u),[0,-d],null),new k(o.subtract(0,u),null,[0,d]),new k(o.subtract(h,0),[l,0])])}return path._closed=!0,path.set(e.getNamed(arguments))},RoundRectangle:"#Rectangle",Ellipse:function(){var e=b._readEllipse(arguments);return t(e.center,e.radius,arguments)},Oval:"#Ellipse",Arc:function(){var t=h.readNamed(arguments,"from"),n=h.readNamed(arguments,"through"),i=h.readNamed(arguments,"to"),r=new O;return r.moveTo(t),r.arcTo(n,i),r.set(e.getNamed(arguments))},RegularPolygon:function(){for(var t=h.readNamed(arguments,"center"),n=e.readNamed(arguments,"sides"),i=e.readNamed(arguments,"radius"),r=new O,s=360/n,a=!(n%3),o=new h(0,a?-i:i),u=a?-1:.5,l=Array(n),c=0;n>c;c++)l[c]=new k(t.add(o.rotate((c+u)*s)));return r._add(l),r._closed=!0,r.set(e.getNamed(arguments))},Star:function(){for(var t=h.readNamed(arguments,"center"),n=2*e.readNamed(arguments,"points"),i=e.readNamed(arguments,"radius1"),r=e.readNamed(arguments,"radius2"),s=new O,a=360/n,o=new h(0,-1),u=Array(n),l=0;n>l;l++)u[l]=new k(t.add(o.rotate(a*l).multiply(l%2?r:i)));return s._add(u),s._closed=!0,s.set(e.getNamed(arguments))}}}});var L=I.extend({_class:"CompoundPath",_serializeFields:{children:[]},initialize:function(t){this._children=[],this._namedChildren={},this._initialize(t)||this.addChildren(Array.isArray(t)?t:arguments)},insertChildren:function ye(e,n,i){n=ye.base.call(this,e,n,i,"path");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()},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=e.concat(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},getPathData:function(){for(var t=this._children,e=[],n=0,i=t.length;i>n;n++)e.push(t[n].getPathData(arguments[0]));return e.join(" ")},_getWinding:function(t){for(var e=this._children,n=0,i=0,r=e.length;r>i;i++)n+=e[i]._getWinding(t);return n},_hitTest:function we(t,n){var i=we.base.call(this,t,new e(n,{fill:!1}));if(!i)if(n.compoundChildren)for(var r=this._children,s=r.length-1;s>=0&&!i;s--)i=r[s]._hitTest(t,n);else n.fill&&this.hasFill()&&this._contains(t)&&(i=new P("fill",this));return i},_draw:function(t,e){var n=this._children;if(0!==n.length){t.beginPath(),e=e.extend({compound:!0});for(var i=0,r=n.length;r>i;i++)n[i].draw(t,e);if(!e.clip){this._setStyles(t);var s=this._style;s.hasFill()&&(t.fill(s.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s.hasStroke()&&t.stroke()}}}},new function(){function t(t){if(!t._children.length)throw Error("Use a moveTo() command first");return t._children[t._children.length-1]}var n={moveTo:function(){var t=new O;this.addChild(t),t.moveTo.apply(t,arguments)},moveBy:function(){this.moveTo(t(this).getLastSegment()._point.add(h.read(arguments)))},closePath:function(){t(this).closePath()}};return e.each(["lineTo","cubicCurveTo","quadraticCurveTo","curveTo","arcTo","lineBy","cubicCurveBy","quadraticCurveBy","curveBy","arcBy"],function(e){n[e]=function(){var n=t(this);n[e].apply(n,arguments)}}),n}),T=e.extend({initialize:function(t){function e(t,e){var n=M.getValues(t,e);s.curves.push(n),s._computeParts(n,t._index,0,1)}this.curves=[],this.parts=[],this.length=0,this.index=0;for(var n,i=t._segments,r=i[0],s=this,a=1,o=i.length;o>a;a++)n=i[a],e(r,n),r=n;t._closed&&e(n,i[0])},_computeParts:function(t,e,n,i){if(i-n>1/32&&!M.isFlatEnough(t,.25)){var r=M.subdivide(t),s=(n+i)/2;this._computeParts(r[0],e,n,s),this._computeParts(r[1],e,s,i)}else{var a=t[6]-t[0],o=t[7]-t[1],h=Math.sqrt(a*a+o*o);h>1e-5&&(this.length+=h,this.parts.push({offset:this.length,value:i,index:e}))}},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}},evaluate:function(t,e){var n=this.getParameterAt(t);return M.evaluate(this.curves[n.index],n.value,e)},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=e.extend({initialize:function(t,e){this.points=[];for(var n,i=t._segments,r=0,s=i.length;s>r;r++){var a=i[r].point.clone();n&&n.equals(a)||(this.points.push(a),n=a)}this.error=e},fit:function(){var t=this.points,e=t.length;return this.segments=e>0?[new k(t[0])]:[],e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.segments},fitCubic:function(e,n,i,r){if(1==n-e){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;4>=c;c++){var d=this.generateBezier(e,n,u,i,r),f=this.findMaxError(e,n,d,u);if(f.error=l)break;this.reparameterize(e,n,u,d),l=f.error}var _=this.points[h-1].subtract(this.points[h]),g=this.points[h].subtract(this.points[h+1]),p=_.add(g).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 k(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-11,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 z=h[0][0]+h[0][1],M=h[1][0]+h[1][1];b=C=Math.abs(z)>s?u[0]/z:Math.abs(M)>s?u[1]/M:0}var A=o.getDistance(a);return s*=A,(s>b||s>C)&&(b=C=A/3),[a,a.add(i.normalize(b)),o.add(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])},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-5?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}}});I.inject(new function(){function t(t,e){t.sort(function(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 n=e&&[],i=t.length-1;i>=0;i--){var r=t[i],s=r.getIntersection(),a=r.divide(),o=a&&a.getSegment1()||r.getSegment();n&&n.push(s),o._intersection=s,r._segment=o}return n}function e(t){if(t instanceof L){var e,n=t.removeChildren(),i=n.length,r=Array(i),s=Array(i);n.sort(function(t,e){var n=t.getBounds(),i=e.getBounds();return n._width*n._heighta;a++)r[a]=n[a].getBounds(),s[a]=0;for(var a=0;i>a;a++){for(var o=1;i>o;o++)a!==o&&r[a].contains(r[o])&&s[o]++;a>0&&0===s[a]%2&&n[a].setClockwise(e)}}return t}function n(n,r,s,a){n=e(n.clone(!1)),r=e(r.clone(!1));var o=n.isClockwise(),u=r.isClockwise(),l=n.getIntersections(r);t(t(l,!0)),o||n.reverse(),a^u||r.reverse(),o=!0,u=!a;for(var c=[].concat(n._children||[n]).concat(r._children||[r]),d=[],f=new L,_=0,g=c.length;g>_;_++){var p=c[_],v=p._parent,m=p.isClockwise(),y=p._segments;p=v instanceof L?v:p;for(var w=y.length-1;w>=0;w--){var x=y[w],b=x.getCurve().getPoint(.5),C=p!==n&&n.contains(b)&&(m===o||a||!i(n,b)),S=p!==r&&r.contains(b)&&(m===u||!i(r,b));s(p===n,C,S)?x._invalid=!0:d.push(x)}}for(var _=0,g=d.length;g>_;_++){var x=d[_];if(!x._visited){var p=new O,P=x._intersection,z=P&&P.getSegment(!0);x.getPrevious()._invalid&&x.setHandleIn(z?z._handleIn:new h(0,0));do{if(x._visited=!0,x._invalid&&x._intersection){var M=x._intersection.getSegment(!0);p.add(new k(x._point,x._handleIn,M._handleOut)),M._visited=!0,x=M}else p.add(x.clone());x=x.getNext()}while(x&&!x._visited&&x!==z);var A=p._segments.length;A>1&&(A>2||!p.isPolygon())?(p.setClosed(!0),f.addChild(p,!0)):p.remove()}}return n.remove(),r.remove(),f.reduce()}function i(t,e){var n=t.getCurves(),i=t.getBounds();if(i.contains(e))for(var r=0,s=n.length;s>r;r++){var a=n[r];if(a.getBounds().contains(e)&&a.getParameterOf(e))return!0}return!1}return{unite:function(t){return n(this,t,function(t,e,n){return e||n})},intersect:function(t){return n(this,t,function(t,e,n){return!(e||n)})},subtract:function(t){return n(this,t,function(t,e,n){return t&&n||!t&&!e},!0)},exclude:function(t){return new w([this.subtract(t),t.subtract(this)])},divide:function(t){return new w([this.subtract(t),this.intersect(t)])}}});var B=y.extend({_class:"TextItem",_boundsSelected:!0,_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 xe(t){return t.setContent(this._content),xe.base.call(this,t)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(69)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),N=B.extend({_class:"PointText",initialize:function(){B.apply(this,arguments)},clone:function(t){return this._clone(new N({insert:!1}),t)},getPoint:function(){var t=this._matrix.getTranslation();return new u(t.x,t.y,this,"setPoint")},setPoint:function(t){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)}}}},new function(){var t=null;return{_getBounds:function(e,n){t||(t=Q.getContext(1,1));var i=this._style,r=this._lines,s=r.length,a=i.getJustification(),o=i.getLeading(),h=0;t.font=i.getFontStyle();for(var u=0,l=0;s>l;l++)u=Math.max(u,t.measureText(r[l]).width);"left"!==a&&(h-=u/("center"===a?2:1));var c=new f(h,s?-.75*o:0,u,s*o);return n?n._transformBounds(c,c):c}}}),D=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 a=i[r+1];e[r]=parseInt(1==a.length?a+a:a,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var a=parseFloat(e[r]);e[r]=3>r?a/255:a}}else{var h=s[t];if(!h){n||(n=Q.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=s[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={},s={},o=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],u={"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){var t=t/60%6,i=Math.floor(t),r=t-i,i=o[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/=360,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+6*(r-s)*(2/3-h):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,0,{readNull:!0}),e!==t&&(e&&e._removeOwner(this),t&&t._addOwner(this)),t}:"hue"===t?function(t){return isNaN(t)?0:(t%360+360)%360}:"gradient"===n?function(){return h.read(arguments,0,0,{readNull:"highlight"===t,clone:!0})}:function(t){return isNaN(t)?0:Math.min(Math.max(t,0),1)};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),t=u.call(this,t),null!=t&&(this._components[s]=t,this._changed())}},this)},{_class:"Color",_readIndex:!0,initialize:function l(e){var n,s,a,o,h=Array.prototype.slice,u=arguments,c=0,d=!0;Array.isArray(e)&&(u=e,e=u[0]);var f=null!=e&&typeof e;if("string"===f&&e in i&&(n=e,e=u[1],Array.isArray(e)?(s=e,a=u[2]):(this.__read&&(c=1),u=h.call(u,1),f=typeof e)),!s){if(d=!(this.__options&&this.__options.dontParse),o="number"===f?u:"object"===f&&null!=e.length?e:null){n||(n=o.length>=3?"rgb":"gray");var _=i[n].length;a=o[_],this.__read&&(c+=o===arguments?_+(null!=a?1:0):1),o.length>_&&(o=h.call(o,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===l){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===j)n="gradient",o=u;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=d&&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}),d&&(y=w[g].call(this,y)),null!=y&&(s[g]=y)}a=e.alpha}this.__read&&n&&(c=1)}if(this._type=n||"rgb","gradient"===n&&(this._id=l._id=(l._id||0)+1),!s){this._components=s=[];for(var w=r[this._type],g=0,p=w.length;p>g;g++){var y=o&&o[g];d&&(y=w[g].call(this,y)),null!=y&&(s[g]=y)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=c)},_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(17)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=u[this._type+"-"+t])?e.apply(this,this._components):u["rgb-"+t].apply(this,u[this._type+"-rgb"].apply(this,this._components))},convert:function(t){return new D(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){return e.isPlainValue(t)&&(t=D.read(arguments)),t===this||t&&this._class===t._class&&this._type===t._type&&this._alpha===t._alpha&&e.equals(this._components,t._components)||!1},toString:function(){for(var t=this._properties,e=[],n="gradient"===this._type,i=a.instance,r=0,s=t.length;s>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){var e=this._convert("rgb"),n=t||null==this._alpha?1:this._alpha;return e=[Math.round(255*e[0]),Math.round(255*e[1]),Math.round(255*e[2])],1>n&&e.push(n),t?"#"+((1<<24)+(e[0]<<16)+(e[1]<<8)+e[2]).toString(16).slice(1):(4==e.length?"rgba(":"rgb(")+e.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 D(t(),t(),t())}}})},new function(){function t(t,e){return 0>t?0:e&&t>360?360:!e&&t>1?1:t}var n={add:function(e,n,i){return t(e+n,i)},subtract:function(e,n,i){return t(e-n,i)},multiply:function(e,n,i){return t(e*n,i)},divide:function(e,n,i){return t(e/n,i)}};return e.each(n,function(t,e){var n={dontParse:/^(multiply|divide)$/.test(e)};this[e]=function(e){e=D.read(arguments,0,0,n);for(var i=this._type,r=this._properties,s=this._components,a=e._convert(i),o=0,h=s.length;h>o;o++)a[o]=t(s[o],a[o],"hue"===r[o]);return new D(i,a,null!=this._alpha?t(this._alpha,e.getAlpha()):null)}},{})});e.each(D._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 D(n,i):new D(t)};if(3==n.length){var r=n.toUpperCase();D[r]=this[r+"Color"]=i}},e.exports);var j=e.extend({_class:"Gradient",initialize:function be(t,e){this._id=be._id=(be._id||0)+1,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(t){var e=this._owners?this._owners.indexOf(t):-1;-1!=e&&(this._owners.splice(e,1),0===this._owners.length&&delete this._owners)},clone:function(){for(var t=[],e=0,n=this._stops.length;n>e;e++)t[e]=this._stops[e].clone();return new j(t)},getStops:function(){return this._stops},setStops:function(t){if(this.stops)for(var e=0,n=this._stops.length;n>e;e++)delete this._stops[e]._owner;if(t.length<2)throw Error("Gradient stop list needs to contain at least two stops.");this._stops=F.readAll(t,0,!1,!0);for(var e=0,n=this._stops.length;n>e;e++){var i=this._stops[e];i._owner=this,i._defaultRamp&&i.setRampPoint(e/(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,n=this._stops.length;n>e;e++)if(!this._stops[e].equals(t._stops[e]))return!1;return!0}return!1}}),F=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 F(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(17)},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=D.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",miterLimit:10,dashOffset:0,dashArray:[],windingRule:"nonzero",shadowColor:t,shadowBlur:0,shadowOffset:new h,selectedColor:t,font:"sans-serif",fontSize:12,leading:null,justification:"left"},i={strokeWidth:25,strokeCap:25,strokeJoin:25,miterLimit:25,font:5,fontSize:5,leading:5,justification:5},r={},s={_defaults:n,_textDefaults:new e(n,{fillColor:new D})};return e.each(n,function(n,a){var o=/Color$/.test(a),h=e.capitalize(a),u=i[a],l="set"+h,c="get"+h;s[l]=function(t){var e=this._item&&this._item._children;if(e&&e.length>0&&"compound-path"!==this._item._type)for(var n=0,i=e.length;i>n;n++)e[n]._style[l](t);else{var r=this._values[a];r!=t&&(o&&(r&&delete r._owner,t&&t.constructor===D&&(t._owner&&(t=t.clone()),t._owner=this._item)),this._values[a]=t,this._item&&this._item._changed(u||17))}},s[c]=function(){var n,i=this._item&&this._item._children;if(!i||0===i.length||arguments[0]||"compound-path"===this._item._type){var n=this._values[a];return n===t?(n=this._defaults[a],n&&n.clone&&(n=n.clone()),this._values[a]=n):!o||n&&n.constructor===D||(this._values[a]=n=D.read([n],0,0,{readNull:!0,clone:!0}),n&&(n._owner=this._item)),n}for(var r=0,s=i.length;s>r;r++){var h=i[r]._style[c]();if(0===r)n=h;else if(!e.equals(n,h))return t}return n},r[c]=function(){return this._style[c]()},r[l]=function(t){this._style[l](t)}}),y.inject(r),s},{_class:"Style",initialize:function(t,e){this._values={},this._item=e,e instanceof B&&(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},getLeading:function Ce(){var t=Ce.base.call(this);return null!=t?t:1.2*this.getFontSize()},getFontStyle:function(){var t=this.getFontSize();return t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFont()}}),q=new function(){function n(t,i){for(var r=[],s=0,a=t&&t.length;a>s;){var o=t[s++];if("string"==typeof o)o=document.createElement(o);else if(!o||!o.nodeType)continue;e.isPlainObject(t[s])&&q.set(o,t[s++]),Array.isArray(t[s])&&n(t[s++],o),i&&i.appendChild(o),r.push(o)}return r}var i=/^(checked|value|selected|disabled)$/i,r={text:"textContent",html:"innerHTML"},s={lineHeight:1,zoom:1,zIndex:1,opacity:1};return{create:function(t,e){var i=Array.isArray(t),r=n(i?t:arguments,i?e:null);return 1==r.length?r[0]:r},find:function(t,e){return(e||document).querySelector(t)},findAll:function(t,e){return(e||document).querySelectorAll(t)},get:function(t,e){return t?i.test(e)?"value"===e||"string"!=typeof t[e]?t[e]:!0:e in r?t[r[e]]:t.getAttribute(e):null},set:function(e,n,s){if("string"!=typeof n)for(var a in n)n.hasOwnProperty(a)&&this.set(e,a,n[a]);else{if(!e||s===t)return e;i.test(n)?e[n]=s:n in r?e[r[n]]=s:"style"===n?this.setStyle(e,s):"events"===n?V.add(e,s):e.setAttribute(n,s)}return e},getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getStyle:function(t,e){return t&&t.style[e]||this.getStyles(t)[e]||null},setStyle:function(t,e,n){if("string"!=typeof e)for(var i in e)e.hasOwnProperty(i)&&this.setStyle(t,i,e[i]);else!/^-?[\d\.]+$/.test(n)||e in s||(n+="px"),t.style[e]=n;return t},hasClass:function(t,e){return RegExp("\\s*"+e+"\\s*").test(t.className)},addClass:function(t,e){t.className=(t.className+" "+e).trim()},removeClass:function(t,e){t.className=t.className.replace(RegExp("\\s*"+e+"\\s*")," ").trim()},remove:function(t){t.parentNode&&t.parentNode.removeChild(t)},removeChildren:function(t){for(;t.firstChild;)t.removeChild(t.firstChild)},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 f(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new f(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return this.getBounds(t,e).getPoint()},getSize:function(t){return this.getBounds(t,!0).getSize()},isInvisible:function(t){return this.getSize(t).equals(new c(0,0))},isInView:function(t){return!this.isInvisible(t)&&this.getViewportBounds(t).intersects(this.getBounds(t,!0))},getPrefixValue:function(t,e){for(var n=t[e],i=["webkit","moz","ms","o"],r=e[0].toUpperCase()+e.substring(1),s=0;4>s&&null==n;s++)n=t[i[s]+r];return n}}},V={add:function(t,e){for(var n in e){var i=e[n];t.addEventListener?t.addEventListener(n,i,!1):t.attachEvent&&t.attachEvent("on"+n,i.bound=function(){i.call(t,window.event)})}},remove:function(t,e){for(var n in e){var i=e[n];t.removeEventListener?t.removeEventListener(n,i,!1):t.detachEvent&&t.detachEvent("on"+n,i.bound)}},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},getOffset:function(t,e){return V.getPoint(t).subtract(q.getOffset(e||V.getTarget(t)))},preventDefault:function(t){t.preventDefault?t.preventDefault():t.returnValue=!1},stopPropagation:function(t){t.stopPropagation?t.stopPropagation():t.cancelBubble=!0},stop:function(t){V.stopPropagation(t),V.preventDefault(t)}};V.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"==r.getAttribute(u,"keepalive")||a)&&q.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):i=!1)}var e,n=q.getPrefixValue(window,"requestAnimationFrame"),i=!1,s=[],a=!0;return V.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(r,a){s.push([r,a]),n?i||(n(t),i=!0):e||(e=setInterval(t,1e3/60))}};var Z=e.extend(n,{_class:"View",initialize:function Se(t){this._scope=paper,this._project=paper.project,this._element=t;var e;if(this._id=t.getAttribute("id"),null==this._id&&t.setAttribute("id",this._id="view-"+Se._id++),V.add(t,this._viewHandlers),r.hasAttribute(t,"resize")){var n=q.getOffset(t,!0),i=this;e=q.getViewportBounds(t).getSize().subtract(n),this._windowHandlers={resize:function(){q.isInvisible(t)||(n=q.getOffset(t,!0)),i.setViewSize(q.getViewportBounds(t).getSize().subtract(n))}},V.add(window,this._windowHandlers)}else e=q.getSize(t),(e.isNaN()||e.isZero())&&(e=new c(parseInt(t.getAttribute("width"),10),parseInt(t.getAttribute("height"),10)));if(this._setViewSize(e),r.hasAttribute(t,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var s=this._stats.domElement,a=s.style,n=q.getOffset(t);a.position="absolute",a.left=n.x+"px",a.top=n.y+"px",document.body.appendChild(s)}Se._views.push(this),Se._viewsById[this._id]=this,this._viewSize=e,(this._matrix=new g)._owner=this,this._zoom=1,Se._focused||(Se._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(Z._focused==this&&(Z._focused=null),Z._views.splice(Z._views.indexOf(this),1),delete Z._viewsById[this._id],this._project.view==this&&(this._project.view=null),V.remove(this._element,this._viewHandlers),V.remove(window,this._windowHandlers),this._element=this._project=null,this.detach("frame"),this._frameItems={},!0):!1},_events:{onFrame:{install:function(){this._animate=!0,this._requested||this._requestFrame()},uninstall:function(){this._animate=!1}},onResize:{}},_animate:!1,_time:0,_count:0,_requestFrame:function(){var t=this;V.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.fire("frame",new e({delta:n,time:this._time+=n,count:this._count++})),this._stats&&this._stats.update(),this._handlingFrame=!1,this.draw(!0)},_animateItem:function(t,e){var n=this._frameItems;e?(n[t._id]={item:t,time:0,count:0},1===++this._frameItemCount&&this.attach("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.detach("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.fire("frame",new e(t,{time:i.time+=t.delta,count:i.count++}))}},_redraw:function(){this._project._needsRedraw=!0,this._handlingFrame||(this._animate?this._handleFrame():this.draw())},_changed:function(t){1&t&&(this._project._needsRedraw=!0)},_transform:function(t){this._matrix.concatenate(t),this._bounds=null,this._redraw()},getElement:function(){return this._element},getViewSize:function(){var t=this._viewSize;return new d(t.width,t.height,this,"setViewSize")},setViewSize:function(t){t=c.read(arguments);var e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.fire("resize",{size:t,delta:e}),this._redraw())},_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 f(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize(arguments[0])},getCenter:function(){return this.getBounds().getCenter(arguments[0])},setCenter:function(t){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 q.isInView(this._element)},scrollBy:function(){this._transform((new g).translate(h.read(arguments).negate()))},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){return"string"==typeof t&&(t=document.getElementById(t)),new H(t)}}},new function(){function t(t){var e=V.getTarget(t);return e.getAttribute&&Z._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(V.getOffset(e,t._element))}function n(){if(!Z._focused||!Z._focused.isVisible())for(var t=0,e=Z._views.length;e>t;t++){var n=Z._views[t];if(n&&n.isVisible()){Z._focused=u=n;break}}}function i(n){var i=Z._focused=t(n),r=e(i,n);l=!0,i._onMouseDown&&i._onMouseDown(n,r),(o=i._scope._tool)&&o._onHandleEvent("mousedown",r,n),i.draw(!0)}function r(i){var r;if(l||(r=t(i),r?(h=Z._focused,Z._focused=u=r):u&&u==Z._focused&&(Z._focused=h,n())),r=r||Z._focused){var s=i&&e(r,i);r._onMouseMove&&r._onMouseMove(i,s),(o=r._scope._tool)&&o._onHandleEvent(l&&o.responds("mousedrag")?"mousedrag":"mousemove",s,i)&&V.stop(i),r.draw(!0)}}function s(t){var n=Z._focused;if(n&&l){var i=e(n,t);curPoint=null,l=!1,n._onMouseUp&&n._onMouseUp(t,i),o&&o._onHandleEvent("mouseup",i,t)&&V.stop(t),n.draw(!0)}}function a(t){l&&V.stop(t)}var o,h,u,l=!1;return V.add(document,{mousemove:r,mouseup:s,touchmove:r,touchend:s,selectstart:a,scroll:n}),V.add(window,{load:n}),{_viewHandlers:{mousedown:i,touchstart:i,selectstart:a},statics:{updateFocus:n}}}),H=Z.extend({_class:"CanvasView",initialize:function(t){if(!(t instanceof HTMLCanvasElement)){var e=c.read(arguments);if(e.isZero())throw Error("Cannot create CanvasView with the provided argument: "+t);t=Q.getCanvas(e)}if(this._context=t.getContext("2d"),this._eventCounters={},this._ratio=1,"off"!==r.getAttribute(t,"hidpi")){var n=window.devicePixelRatio||1,i=q.getPrefixValue(this._context,"backingStorePixelRatio")||1;this._ratio=n/i}Z.call(this,t)},_setViewSize:function(t){var e=t.width,n=t.height,i=this._ratio,r=this._element,s=r.style;r.width=e*i,r.height=n*i,1!==i&&(s.width=e+"px",s.height=n+"px",this._context.scale(i,i))},draw:function(t){if(t&&!this._project._needsRedraw)return!1;var e=this._context,n=this._viewSize;return e.clearRect(0,0,n.width+1,n.height+1),this._project.draw(e,this._matrix,this._ratio),this._project._needsRedraw=!1,!0}},new function(){function t(t,e,n,i,r,s){for(var a,o=i;o;){if(o.responds(t)&&(a||(a=new G(t,e,n,i,r?n.subtract(r):null)),o.fire(t,a)&&(!s||a._stopped)))return!1;o=o.getParent()}return!0}function e(e,n,i,r,s){if(e._eventCounters[n]){var a=e._project,u=a.hitTest(r,{tolerance:a.options.hitTolerance||0,fill:!0,stroke:!0}),l=u&&u.item;if(l)return"mousemove"===n&&l!=o&&(s=r),"mousemove"===n&&h||t(n,i,r,l,s),l}}var n,i,r,s,a,o,h,u,l;return{_onMouseDown:function(t,o){var c=e(this,"mousedown",t,o);u=a==c&&Date.now()-l<300,s=a=c,n=i=r=o,h=s&&s.responds("mousedrag")},_onMouseUp:function(a,o){var c=e(this,"mouseup",a,o);h&&(i&&!i.equals(o)&&t("mousedrag",a,o,s,i),c!=s&&(r=o,t("mousemove",a,o,c,r))),c===s&&(l=Date.now(),(!u||t("doubleclick",a,n,c))&&t("click",a,n,c),u=!1),s=null,h=!1},_onMouseMove:function(n,a){s&&t("mousedrag",n,a,s,i);var h=e(this,"mousemove",n,a,r);i=r=a,h!==o&&(t("mouseleave",n,a,o),o=h,t("mouseenter",n,a,h))}}}),U=e.extend({_class:"Event",initialize:function(t){this.event=t},preventDefault:function(){this._prevented=!0,V.preventDefault(this.event)},stopPropagation:function(){this._stopped=!0,V.stopPropagation(this.event)},stop:function(){this.stopPropagation(),this.preventDefault()},getModifiers:function(){return X.modifiers}}),W=U.extend({_class:"KeyEvent",initialize:function(t,e,n,i){U.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()+" }"}}),X=new function(){function t(t,n,r,h){var u,l=r?String.fromCharCode(r):"",c=i[n],d=c||l.toLowerCase(),f=t?"keydown":"keyup",_=Z._focused,g=_&&_.isVisible()&&_._scope,p=g&&g._tool;o[d]=t,c&&(u=e.camelize(c))in s&&(s[u]=t),t?a[n]=r:delete a[n],p&&p.responds(f)&&(paper=g,p.fire(f,new W(t,d,l,h)),_&&_.draw(!0))}var n,i={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"},r={9:!0,13:!0,32:!0},s=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),a={},o={};return V.add(document,{keydown:function(e){var s=e.which||e.keyCode;s in i?t(!0,s,s in r?s:0,e):n=s},keypress:function(e){null!=n&&(t(!0,n,e.which||e.keyCode,e),n=null)},keyup:function(e){var n=e.which||e.keyCode;n in a&&t(!1,n,a[n],e)}}),V.add(window,{blur:function(e){for(var n in a)t(!1,n,a[n],e)}}),{modifiers:s,isDown:function(t){return!!o[t]}}},G=U.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){U.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()+" }"}});e.extend(n,{_class:"Palette",_events:["onChange"],initialize:function(n,i,r){var s=q.find(".palettejs-panel")||q.find("body").appendChild(q.create("div",{"class":"palettejs-panel"}));this._element=s.appendChild(q.create("table",{"class":"palettejs-pane"})),this._title=n,r||(r={});for(var a in this.components=i){var o=i[a];o instanceof J||(null==o.value&&(o.value=r[a]),o.name=a,o=i[a]=new J(o)),this._element.appendChild(o._element),o._palette=this,r[a]===t&&(r[a]=o.value)}this.values=e.each(r,function(t,n){var s=i[n];s&&e.define(r,n,{enumerable:!0,configurable:!0,get:function(){return s._value},set:function(t){s.setValue(t)}})}),window.paper&&paper.palettes.push(this)},reset:function(){for(var t in this.components)this.components[t].reset()},remove:function(){q.remove(this._element)}});var J=e.extend(n,{_class:"Component",_events:["onChange","onClick"],_types:{"boolean":{type:"checkbox",value:"checked"},string:{type:"text"},number:{type:"number",number:!0},button:{type:"button"},text:{tag:"div",value:"text"},slider:{type:"range",number:!0},list:{tag:"select",setOptions:function(){q.removeChildren(this._input),q.create(e.each(this._options,function(t){this.push("option",{value:t,text:t})},[]),this._input)}},color:{type:"color",getValue:function(t){return new D(t)},setValue:function(t){return new D(t).toCSS("color"===q.get(this._input,"type"))}}},initialize:function Pe(t){this._id=Pe._id=(Pe._id||0)+1,this._type=t.type in this._types?t.type:"options"in t?"list":"onClick"in t?"button":typeof t.value,this._meta=this._types[this._type]||{type:this._type};var n=this,i="component-"+this._id;this._dontFire=!0,this._input=q.create(this._meta.tag||"input",{id:i,type:this._meta.type,events:{change:function(){n.setValue(q.get(this,n._meta.value||"value"))},click:function(){n.fire("click")}}}),this.attach("change",function(t){this._dontFire||this._palette.fire("change",this,this.name,t)}),this._element=q.create("tr",["td",[this._label=q.create("label",{"for":i})],"td",[this._input]]),e.each(t,function(t,e){this[e]=t},this),this._defaultValue=this._value,this._dontFire=!1},getType:function(){return this._type},getLabel:function(){return this.__label},setLabel:function(t){this.__label=t,q.set(this._label,"text",t+":")},getOptions:function(){return this._options},setOptions:function(t){this._options=t;var e=this._meta.setOptions;e&&e.call(this)},getValue:function(){var t=this._value,e=this._meta.getValue;return e?e.call(this,t):t},setValue:function(t){var e=this._meta.value||"value",n=this._meta.setValue;n&&(t=n.call(this,t)),q.set(this._input,e,t),t=q.get(this._input,e),this._meta.number&&(t=parseFloat(t,10)),this._value!==t&&(this._value=t,this._dontFire||this.fire("change",this.getValue()))},getRange:function(){return[parseFloat(q.get(this._input,"min")),parseFloat(q.get(this._input,"max"))]},setRange:function(t,e){var n=Array.isArray(t)?t:[t,e];q.set(this._input,{min:n[0],max:n[1]})},getMin:function(){return this.getRange()[0]},setMin:function(t){this.setRange(t,this.getMax())},getMax:function(){return this.getRange()[1]},setMax:function(t){this.setRange(this.getMin(),t)},getStep:function(){return parseFloat(q.get(this._input,"step"))},setStep:function(t){q.set(this._input,"step",t)},reset:function(){this.setValue(this._defaultValue)}}),$=U.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|compound-path)$/.test(n._type);)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()+" }"}}),Y=s.extend({_class:"Tool",_list:"tools",_reference:"_tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){s.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!=this._minDistance&&null!=this._maxDistance&&this._minDistance>this._maxDistance&&(this._maxDistance=this._minDistance)},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=this._maxDistance&&this._maxDistanceu)return!1;var l=null!=i?i:0;if(0!=l)if(u>l)e=this._point.add(h.normalize(l));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.fire(t,new $(this,t,e))},_onHandleEvent: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}}),K={request:function(t,e,n){var i=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return i.open(t.toUpperCase(),e,!0),"overrideMimeType"in i&&i.overrideMimeType("text/plain"),i.onreadystatechange=function(){if(4===i.readyState){var t=i.status;if(0!==t&&200!==t)throw Error("Could not load "+e+" (Error "+t+")");n.call(i,i.responseText)}},i.send(null)}},Q={canvases:[],getCanvas:function(t,e,n){var i,r=!0;"object"==typeof t&&(n=e,e=t.height,t=t.width),n?1!==n&&(t*=n,e*=n):n=1,i=this.canvases.length?this.canvases.pop():document.createElement("canvas");var s=i.getContext("2d");return i.width===t&&i.height===e?r&&s.clearRect(0,0,t+1,e+1):(i.width=t,i.height=e),s.save(),1!==n&&s.scale(n,n),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)}},te=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=Q.getContext(1,1);e.each(y,function(t,e){x.save();var n="darken"===e,i=!1;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)),w[e]=i,x.restore()}),Q.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,z=1-k;b[S]=k*f+z*u,b[S+1]=k*_+z*l,b[S+2]=k*g+z*c,b[S+3]=h*i+z*d}n.putImageData(x,r.x,r.y)}}},ee=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],miterLimit:["stroke-miterlimit","number"],dashArray:["stroke-dasharray","array"],dashOffset:["stroke-dashoffset","number"],font:["font-family","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},{}),get:"get"+i,set:"set"+i}},{}),ne={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=ne[n];"number"==typeof i&&(i=S.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 r(t,e,n){var i=t._matrix,r=i.getTranslation(),s={};if(e){i=i.shiftless();var a=i._inverseTransform(r);s[n?"cx":"x"]=a.x,s[n?"cy":"y"]=a.y,r=null}if(i.isIdentity())return s;var h=i.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;r&&!r.isZero()&&u.push("translate("+S.point(r)+")"),o.isZero(c.x-1)&&o.isZero(c.y-1)||u.push("scale("+S.point(c)+")"),l&&u.push("rotate("+S.number(l)+")"),s.transform=u.join(" ") -}else s.transform="matrix("+i.getValues().join(",")+")";return s}function s(e,i){for(var s=r(e),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=b(l,i);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),w(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function h(t){var e=r(t,!0),i=t.getSize();return e.x-=i.width/2,e.y-=i.height/2,e.width=i.width,e.height=i.height,e.href=t.toDataURL(),n("image",e)}function u(t,e){if(e.matchShapes){var r=t.toShape(!1);if(r)return c(r,e)}var s,a,o=t._segments;if(0===o.length)return null;if(t.isPolygon())if(o.length>=3){s=t._closed?"polygon":"polyline";var h=[];for(i=0,l=o.length;l>i;i++)h.push(S.point(o[i]._point));a={points:h.join(" ")}}else{s="line";var u=o[0]._point,d=o[o.length-1]._point;a={x1:u.x,y1:u.y,x2:d.x,y2:d.y}}else{s="path";var f=t.getPathData();a=f&&{d:f}}return n(s,a)}function c(t){var e=t._shape,i=t._radius,s=r(t,!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,i.isZero()&&(i=null)}return i&&("circle"===e?s.r=i:(s.rx=i.width,s.ry=i.height)),n(e,s)}function d(t){var e=r(t,!0),i=t.getPathData();return i&&(e.d=i),n("path",e)}function f(t,e){var i=r(t,!0),s=t.getSymbol(),a=m(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:S.rectangle(h)}),a.appendChild(b(o,e)),w(s,a,"symbol")),i.href="#"+a.id,i.x+=h.x,i.y+=h.y,i.width=S.number(h.width),i.height=S.number(h.height),n("use",i)}function _(t){var e=m(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))}w(t,e,"color")}return"url(#"+e.id+")"}function g(t){var e=n("text",r(t,!0));return e.textContent=t._content,e}function p(n,i){var r={},s=n.getParent();return null!=n._name&&(r.id=n._name),e.each(ee,function(t){var i=t.get,a=t.type,o=n[i]();if(!s||!e.equals(s[i](),o)){if("color"===a&&null!=o){var h=o.getAlpha();1>h&&(r[t.attribute+"-opacity"]=h)}r[t.attribute]=null==o?"none":"number"===a?S.number(o):"color"===a?o.gradient?_(o,n):o.toCSS(!0):"array"===a?o.join(","):"lookup"===a?t.toSVG[o]:o}}),1===r.opacity&&delete r.opacity,null==n._visibility||n._visibility||(r.visibility="hidden"),t(i,r)}function m(t,e){return P||(P={ids:{},svgs:{}}),t&&P.svgs[e+"-"+t._id]}function w(t,e,n){P||m();var i=P.ids[n]=(P.ids[n]||0)+1;e.id=n+"-"+i,P.svgs[n+"-"+t._id]=e}function x(t,e){var i=t,r=null;if(P){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in P.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(P.svgs[s]);P=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function b(t,e){var n=k[t._type],i=n&&n(t,e);return i&&t._data&&i.setAttribute("data-paper-data",JSON.stringify(t._data)),i&&p(t,i)}function C(t){return t||(t={}),S=new a(t.precision),t}var S,P,k={group:s,layer:s,raster:h,path:u,shape:c,"compound-path":d,"placed-symbol":f,"point-text":g};y.inject({exportSVG:function(t){return t=C(t),x(b(this,t),t)}}),v.inject({exportSVG:function(t){t=C(t);for(var e=this.layers,i=this.view.getSize(),r=n("svg",{x:0,y:0,width:i.width,height:i.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),s=0,a=e.length;a>s;s++)r.appendChild(b(e[s],t));return x(r,t)}})},new function(){function n(t,e,n,i){var r=ne[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 c(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 w,o=a._project,h=o._currentStyle,u=[];s||(a._transformContent=!1,a=x(a,t,n),o._currentStyle=a._style.clone());for(var l=0,c=r.length;c>l;l++){var d,f=r[l];1!==f.nodeType||!(d=P(f,!1,i))||d instanceof m||u.push(d)}return a.addChildren(u),s&&(a=x(a.reduce(),t,n)),o._currentStyle=h,(s||"defs"===e)&&(a.remove(),a=null),a}function o(t,e){var n=new O,i=t.points;n.moveTo(i.getItem(0));for(var r=1,s=i.numberOfItems;s>r;r++)n.lineTo(i.getItem(r));return"polygon"===e&&n.closePath(),n}function u(t){var e=t.getAttribute("d"),n=e.match(/m/gi).length>1?new L:new O;return n.setPathData(e),n}function l(t,e){for(var r=t.childNodes,s=[],a=0,o=r.length;o>a;a++){var h=r[a];1===h.nodeType&&s.push(x(new F,h))}var u,l,c,d="radialgradient"===e,f=new j(s,d);return d?(u=i(t,"cx","cy"),l=u.add(n(t,"r"),0),c=i(t,"fx","fy",!0)):(u=i(t,"x1","y1"),l=i(t,"x2","y2")),x(new D(f,u,l,c),t),null}function d(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("("),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":case"skewY":var e=Math.tan(c[0]*Math.PI/180),_="skewX"==l;s.shear(_?e:0,_?0:e)}}t.transform(s)}function _(t,e,n){var i=t["fill-opacity"===n?"getFillColor":"getStrokeColor"]();i&&i.setAlpha(parseFloat(e))}function p(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 x(n,i,r){var s={node:q.getStyles(i)||{},parent:!r&&q.getStyles(i.parentNode)||{}};return e.each(z,function(r,a){var o=p(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,e,n){function i(t){paper=s;var i=P(t,e,n),r=n.onLoad,a=s.project&&s.project.view;r&&r.call(this,i),a.draw(!0)}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var r=t,s=paper;if(e)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:l,radialgradient:l,image:function(t){var e=new C(n(t,"href",!0));return e.attach("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 m(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 m?r.place(s):r.clone().translate(s):null},circle:function(t){return new b.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new b.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 b.Rectangle(new f(e,n),s)},line:function(t){return new O.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}},z=e.each(ee,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof b){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 w))return new w(n,t);t.insertChild(0,n)}},gradientTransform:d,transform:d,"fill-opacity":_,"stroke-opacity":_,visibility:function(t,e){t.setVisible("visible"===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 f(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof w){var u=h?o.getSize().divide(h):1,l=(new g).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof m){h&&o.setSize(h);var c="visible"!=p(i,"overflow",a),d=t._definition;c&&!o.contains(d.getBounds())&&(c=new b.Rectangle(o).transform(d._matrix),c.setClipMask(!0),d.addChild(c))}}}),M={};y.inject({importSVG:function(t,e){return this.addChild(P(t,!0,e))}}),v.inject({importSVG:function(t,e){return this.activate(),P(t,!0,e)}})},paper=new(r.inject(new e(e.exports,{enumerable:!0,Base:e,Numerical:o,DomElement:q,DomEvent:V,Http:K,Key:X}))),"function"==typeof define&&define.amd&&define("paper",paper),paper};paper.PaperScope.prototype.PaperScript=function(root){function _$_(t,e,n){var i=binaryOperators[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 $_(t,e){var n=unaryOperators[t];if(n&&e&&e[n])return e[n]();switch(t){case"+":return+e;case"-":return-e}}function compile(t){function e(t){for(var e=0,n=s.length;n>e;e++){var i=s[e];if(i[0]>=t)break;t+=i[1]}return t}function n(n){return t.substring(e(n.range[0]),e(n.range[1]))}function i(n,i){for(var r=e(n.range[0]),a=e(n.range[1]),o=0,h=s.length-1;h>=0;h--)if(r>s[h][0]){o=h+1;break}s.splice(o,0,[r,i.length-a+r]),t=t.substring(0,r)+i+t.substring(a)}function r(t,e){if(t){for(var s in t)if("range"!==s){var a=t[s];if(Array.isArray(a))for(var o=0,h=a.length;h>o;o++)r(a[o],t);else a&&"object"==typeof a&&r(a,t)}switch(t&&t.type){case"BinaryExpression":if(t.operator in binaryOperators&&"Literal"!==t.left.type){var u=n(t.left),l=n(t.right);i(t,"_$_("+u+', "'+t.operator+'", '+l+")")}break;case"AssignmentExpression":if(/^.=$/.test(t.operator)&&"Literal"!==t.left.type){var u=n(t.left),l=n(t.right);i(t,u+" = _$_("+u+', "'+t.operator[0]+'", '+l+")")}break;case"UpdateExpression":if(!(t.prefix||e&&("BinaryExpression"===e.type&&/^[=!<>]/.test(e.operator)||"MemberExpression"===e.type&&e.computed))){var c=n(t.argument);i(t,c+" = _$_("+c+', "'+t.operator[0]+'", 1)')}break;case"UnaryExpression":if(t.operator in unaryOperators&&"Literal"!==t.argument.type){var c=n(t.argument);i(t,'$_("'+t.operator+'", '+c+")")}}}}var s=[];return r(scope.acorn.parse(t,{ranges:!0})),t}function evaluate(code,scope){paper=scope;var view=scope.project&&scope.project.view,res;with(scope)!function(){var onActivate,onDeactivate,onEditOptions,onMouseDown,onMouseUp,onMouseDrag,onMouseMove,onKeyDown,onKeyUp,onFrame,onResize;if(code=compile(code),root.InstallTrigger){var handle=PaperScript.handleException;if(!handle){handle=PaperScript.handleException=function(t){throw t.lineNumber>=lineNumber?Error(t.message,t.fileName,t.lineNumber-lineNumber):t};var lineNumber=Error().lineNumber;lineNumber+=3*(Error().lineNumber-lineNumber)}try{res=eval(";"+code)}catch(e){handle(e)}}else res=eval(code);/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)&&Base.each(paper.Tool.prototype._events,function(key){var value=eval(key);value&&(scope.getTool()[key]=value)}),view&&(view.setOnResize(onResize),view.fire("resize",{size:view.size,delta:new Point}),onFrame&&view.setOnFrame(onFrame),view.draw())}.call(scope);return res}function load(){Base.each(document.getElementsByTagName("script"),function(t){if(/^text\/(?:x-|)paperscript$/.test(t.type)&&!t.getAttribute("data-paper-ignore")){var e=PaperScope.getAttribute(t,"canvas"),n=PaperScope.get(e)||new PaperScope(t).setup(e),i=t.src;i?paper.Http.request("get",i,function(t){evaluate(t,n)}):evaluate(t.innerHTML,n),t.setAttribute("data-paper-ignore",!0)}},this)}var Base=paper.Base,PaperScope=paper.PaperScope,PaperScript,exports,define,scope=this;!function(t,e){return"object"==typeof exports&&"object"==typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(e(t.acorn||(t.acorn={})),void 0)}(this,function(t){"use strict";function e(t){ce=t||{};for(var e in ge)Object.prototype.hasOwnProperty.call(ce,e)||(ce[e]=ge[e]);_e=ce.sourceFile||null}function n(t,e){var n=pe(de,t);e+=" ("+n.line+":"+n.column+")";var i=new SyntaxError(e);throw i.pos=t,i.loc=n,i.raisedAt=ve,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;rve&&10!==n&&13!==n&&8232!==n&&8329!==n;)++ve,n=de.charCodeAt(ve);ce.onComment&&ce.onComment(!1,de.slice(t+2,ve),t,ve,e,ce.locations&&new r)}function u(){for(;fe>ve;){var t=de.charCodeAt(ve);if(32===t)++ve;else if(13===t){++ve;var e=de.charCodeAt(ve);10===e&&++ve,ce.locations&&(++Pe,ke=ve)}else if(10===t)++ve,++Pe,ke=ve;else if(14>t&&t>8)++ve;else if(47===t){var e=de.charCodeAt(ve+1);if(42===e)o();else{if(47!==e)break;h()}}else if(160===t)++ve;else{if(!(t>=5760&&Un.test(String.fromCharCode(t))))break;++ve}}}function l(){var t=de.charCodeAt(ve+1);return t>=48&&57>=t?S(!0):(++ve,a(xn))}function c(){var t=de.charCodeAt(ve+1);return Se?(++ve,x()):61===t?w(Pn,2):w(Cn,1)}function d(){var t=de.charCodeAt(ve+1);return 61===t?w(Pn,2):w(Dn,1)}function f(t){var e=de.charCodeAt(ve+1);return e===t?w(124===t?An:In,2):61===e?w(Pn,2):w(124===t?On:Tn,1)}function _(){var t=de.charCodeAt(ve+1);return 61===t?w(Pn,2):w(Ln,1)}function g(t){var e=de.charCodeAt(ve+1);return e===t?w(zn,2):61===e?w(Pn,2):w(kn,1)}function p(t){var e=de.charCodeAt(ve+1),n=1;return e===t?(n=62===t&&62===de.charCodeAt(ve+2)?3:2,61===de.charCodeAt(ve+n)?w(Pn,n+1):w(Nn,n)):(61===e&&(n=61===de.charCodeAt(ve+2)?3:2),w(Bn,n))}function v(t){var e=de.charCodeAt(ve+1);return 61===e?w(En,61===de.charCodeAt(ve+2)?3:2):w(61===t?Sn:Mn,1)}function m(t){switch(t){case 46:return l();case 40:return++ve,a(pn);case 41:return++ve,a(vn);case 59:return++ve,a(yn);case 44:return++ve,a(mn);case 91:return++ve,a(dn);case 93:return++ve,a(fn);case 123:return++ve,a(_n);case 125:return++ve,a(gn);case 58:return++ve,a(wn);case 63:return++ve,a(bn);case 48:var e=de.charCodeAt(ve+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(Mn,1)}return!1}function y(t){if(t?ve=me+1:me=ve,ce.locations&&(we=new r),t)return x();if(ve>=fe)return a(je);var e=de.charCodeAt(ve);if(Kn(e)||92===e)return M();var i=m(e);if(i===!1){var s=String.fromCharCode(e);if("\\"===s||Gn.test(s))return M();n(ve,"Unexpected character '"+s+"'")}return i}function w(t,e){var n=de.slice(ve,ve+e);ve+=e,a(t,n)}function x(){for(var t,e,i="",r=ve;;){ve>=fe&&n(r,"Unterminated regular expression");var s=de.charAt(ve);if($n.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}++ve}var i=de.slice(r,ve);++ve;var o=z();return o&&!/^[gmsiy]*$/.test(o)&&n(r,"Invalid regexp flag"),a(Be,RegExp(i,o))}function b(t,e){for(var n=ve,i=0,r=0,s=null==e?1/0:e;s>r;++r){var a,o=de.charCodeAt(ve);if(a=o>=97?o-97+10:o>=65?o-65+10:o>=48&&57>=o?o-48:1/0,a>=t)break;++ve,i=i*t+a}return ve===n||null!=e&&ve-n!==e?null:i}function C(){ve+=2;var t=b(16);return null==t&&n(me+2,"Expected hexadecimal number"),Kn(de.charCodeAt(ve))&&n(ve,"Identifier directly after number"),a(Ee,t)}function S(t){var e=ve,i=!1,r=48===de.charCodeAt(ve);t||null!==b(10)||n(e,"Invalid number"),46===de.charCodeAt(ve)&&(++ve,b(10),i=!0);var s=de.charCodeAt(ve);(69===s||101===s)&&(s=de.charCodeAt(++ve),(43===s||45===s)&&++ve,null===b(10)&&n(e,"Invalid number"),i=!0),Kn(de.charCodeAt(ve))&&n(ve,"Identifier directly after number");var o,h=de.slice(e,ve);return i?o=parseFloat(h):r&&1!==h.length?/[89]/.test(h)||Le?n(e,"Invalid number"):o=parseInt(h,8):o=parseInt(h,10),a(Ee,o)}function P(t){ve++;for(var e="";;){ve>=fe&&n(me,"Unterminated string constant");var i=de.charCodeAt(ve);if(i===t)return++ve,a(Ne,e);if(92===i){i=de.charCodeAt(++ve);var r=/^[0-7]+/.exec(de.slice(ve,ve+3));for(r&&(r=r[0]);r&&parseInt(r,8)>255;)r=r.slice(0,r.length-1);if("0"===r&&(r=null),++ve,r)Le&&n(ve-2,"Octal literal in strict mode"),e+=String.fromCharCode(parseInt(r,8)),ve+=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+="\0";break;case 13:10===de.charCodeAt(ve)&&++ve;case 10:ce.locations&&(ke=ve,++Pe);break;default:e+=String.fromCharCode(i)}}else(13===i||10===i||8232===i||8329===i)&&n(me,"Unterminated string constant"),e+=String.fromCharCode(i),++ve}}function k(t){var e=b(16,t);return null===e&&n(me,"Bad character escape sequence"),e}function z(){Fn=!1;for(var t,e=!0,i=ve;;){var r=de.charCodeAt(ve);if(Qn(r))Fn&&(t+=de.charAt(ve)),++ve;else{if(92!==r)break;Fn||(t=de.slice(i,ve)),Fn=!0,117!=de.charCodeAt(++ve)&&n(ve,"Expecting Unicode escape sequence \\uXXXX"),++ve;var s=k(4),a=String.fromCharCode(s);a||n(ve-1,"Invalid Unicode escape"),(e?Kn(s):Qn(s))||n(ve-4,"Invalid Unicode escape"),t+=a}e=!1}return Fn?t:de.slice(i,ve)}function M(){var t=z(),e=De;return Fn||(Hn(t)?e=cn[t]:(ce.forbidReserved&&(3===ce.ecmaVersion?Rn:qn)(t)||Le&&Vn(t))&&n(me,"The keyword '"+t+"' is reserved")),a(e,t)}function A(){ze=me,Me=ye,Ae=xe,y()}function I(t){for(Le=t,ve=Me;ke>ve;)ke=de.lastIndexOf("\n",ke-2)+1,--Pe;u(),y()}function O(){this.type=null,this.start=me,this.end=null}function L(){this.start=we,this.end=null,null!==_e&&(this.source=_e)}function T(){var t=new O;return ce.locations&&(t.loc=new L),ce.ranges&&(t.range=[me,0]),t}function E(t){var e=new O;return e.start=t.start,ce.locations&&(e.loc=new L,e.loc.start=t.loc.start),ce.ranges&&(e.range=[t.range[0],0]),e}function B(t,e){return t.type=e,t.end=Me,ce.locations&&(t.loc.end=Ae),ce.ranges&&(t.range[1]=Me),t}function N(t){return ce.ecmaVersion>=5&&"ExpressionStatement"===t.type&&"Literal"===t.expression.type&&"use strict"===t.expression.value}function D(t){return be===t?(A(),!0):void 0}function j(){return!ce.strictSemicolons&&(be===je||be===gn||$n.test(de.slice(Me,me)))}function F(){D(yn)||j()||q()}function R(t){be===t?A():q()}function q(){n(me,"Unexpected token")}function V(t){"Identifier"!==t.type&&"MemberExpression"!==t.type&&n(t.start,"Assigning to rvalue"),Le&&"Identifier"===t.type&&Zn(t.name)&&n(t.start,"Assigning to "+t.name+" in strict mode")}function Z(t){ze=Me=ve,ce.locations&&(Ae=new r),Ie=Le=null,Oe=[],y();var e=t||T(),n=!0;for(t||(e.body=[]);be!==je;){var i=H();e.body.push(i),n&&N(i)&&I(!0),n=!1}return B(e,"Program")}function H(){be===Cn&&y(!0);var t=be,e=T();switch(t){case Fe:case Ve:A();var i=t===Fe;D(yn)||j()?e.label=null:be!==De?q():(e.label=le(),F());for(var r=0;re){var r=E(t);r.left=t,r.operator=Ce,A(),r.right=te(ee(),i,n);var r=B(r,/&&|\|\|/.test(r.operator)?"LogicalExpression":"BinaryExpression");return te(r,e,n)}return t}function ee(){if(be.prefix){var t=T(),e=be.isUpdate;return t.operator=Ce,t.prefix=!0,A(),t.argument=ee(),e?V(t.argument):Le&&"delete"===t.operator&&"Identifier"===t.argument.type&&n(t.start,"Deleting local variable in strict mode"),B(t,e?"UpdateExpression":"UnaryExpression")}for(var i=ne();be.postfix&&!j();){var t=E(i);t.operator=Ce,t.prefix=!1,t.argument=i,V(i),A(),i=B(t,"UpdateExpression")}return i}function ne(){return ie(re())}function ie(t,e){if(D(xn)){var n=E(t);return n.object=t,n.property=le(!0),n.computed=!1,ie(B(n,"MemberExpression"),e)}if(D(dn)){var n=E(t);return n.object=t,n.property=$(),n.computed=!0,R(fn),ie(B(n,"MemberExpression"),e)}if(!e&&D(pn)){var n=E(t);return n.callee=t,n.arguments=ue(vn,!1),ie(B(n,"CallExpression"),e)}return t}function re(){switch(be){case an:var t=T();return A(),B(t,"ThisExpression");case De:return le();case Ee:case Ne:case Be:var t=T();return t.value=Ce,t.raw=de.slice(me,ye),A(),B(t,"Literal");case on:case hn:case un:var t=T();return t.value=be.atomValue,t.raw=be.keyword,A(),B(t,"Literal");case pn:var e=we,n=me;A();var i=$();return i.start=n,i.end=ye,ce.locations&&(i.loc.start=e,i.loc.end=xe),ce.ranges&&(i.range=[n,ye]),R(vn),i;case dn:var t=T();return A(),t.elements=ue(fn,!0,!0),B(t,"ArrayExpression");case _n:return ae();case Je:var t=T();return A(),he(t,!1);case sn:return se();default:q()}}function se(){var t=T();return A(),t.callee=ie(re(),!0),t.arguments=D(pn)?ue(vn,!1):Te,B(t,"NewExpression")}function ae(){var t=T(),e=!0,i=!1;for(t.properties=[],A();!D(gn);){if(e)e=!1;else if(R(mn),ce.allowTrailingCommas&&D(gn))break;var r,s={key:oe()},a=!1;if(D(wn)?(s.value=$(!0),r=s.kind="init"):ce.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=oe(),be!==pn&&q(),s.value=he(T(),!1)):q(),"Identifier"===s.key.type&&(Le||i))for(var o=0;oa?t.id:t.params[a];if((Vn(o.name)||Zn(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 B(t,e?"FunctionDeclaration":"FunctionExpression")}function ue(t,e,n){for(var i=[],r=!0;!D(t);){if(r)r=!1;else if(R(mn),e&&ce.allowTrailingCommas&&D(t))break;n&&be===mn?i.push(null):i.push($(!0))}return i}function le(t){var e=T();return e.name=be===De?Ce:t&&!ce.forbidReserved&&be.keyword||q(),A(),B(e,"Identifier")}t.version="0.3.2";var ce,de,fe,_e;t.parse=function(t,n){return de=t+"",fe=de.length,e(n),s(),Z(ce.program)};var ge=t.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},pe=t.getLineInfo=function(t,e){for(var n=1,i=0;;){Yn.lastIndex=i;var r=Yn.exec(t);if(!(r&&r.indext?36===t:91>t?!0:97>t?95===t:123>t?!0:t>=170&&Gn.test(String.fromCharCode(t)) -},Qn=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&&Jn.test(String.fromCharCode(t))},ti={kind:"loop"},ei={kind:"switch"}});var binaryOperators={"+":"_add","-":"_subtract","*":"_multiply","/":"_divide","%":"_modulo","==":"equals","!=":"equals"},unaryOperators={"-":"_negate","+":null},fields=Base.each(["add","subtract","multiply","divide","modulo","negate"],function(t){this["_"+t]="#"+t},{});return paper.Point.inject(fields),paper.Size.inject(fields),paper.Color.inject(fields),"complete"===document.readyState?setTimeout(load):paper.DomEvent.add(window,{load:load}),PaperScript={compile:compile,evaluate:evaluate,load:load,lineNumberBase:0}}(this); \ No newline at end of file +var paper=new function(t){var e=new function(){function n(n,i,r,a,o){function h(s,a,h){var a=a||(a=u(i,s))&&(a.get?a:a.value);"string"==typeof a&&"#"===a[0]&&(a=n[a.substring(1)]||a);var d,f="function"==typeof a,_=a,g=o||f?a&&a.get?s in n:n[s]:null;!(h||a!==t&&i.hasOwnProperty(s))||o&&g||(f&&g&&(a.base=g),f&&l&&(d=s.match(/^([gs]et|is)(([A-Z])(.*))$/))&&(l[d[3].toLowerCase()+d[4]]=d[2]),_&&!f&&_.get&&"function"==typeof _.get&&e.isPlainObject(_)||(_={value:_,writable:!0}),(u(n,s)||{configurable:!0}).configurable&&(_.configurable=!0,_.enumerable=r),c(n,s,_))}var l;if(i){l={};for(var d in i)i.hasOwnProperty(d)&&!s.test(d)&&h(d,null,!0);h("toString"),h("valueOf");for(var d in l){var f=l[d],_=n["set"+f],g=n["get"+f]||_&&n["is"+f];g&&(0===g.length||_)&&h(d,{get:g,set:_},!0)}}return n}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){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}var s=/^(statics|preserve|enumerable|prototype|toString|valueOf)$/,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=this.prototype,i=Object.getPrototypeOf(e).constructor,r=t.statics===!0?t:t.statics;r!=t&&n(e,t,t.enumerable,i&&i.prototype,t.preserve),n(this,r,!0,i,t.preserve)}for(var s=1,a=arguments.length;a>s;s++)this.inject(arguments[s]);return this},extend:function(){for(var t,e=this,i=0,r=arguments.length;r>i&&!(t=arguments[i].initialize);i++);return t=t||function(){e.apply(this,arguments)},t.prototype=h(this.prototype),t.base=e,c(t.prototype,"constructor",{value:t,writable:!0,configurable:!0}),n(t,this,!0),arguments.length?this.inject.apply(t,arguments):t}},!0).inject({inject:function(){for(var t=0,e=arguments.length;e>t;t++)n(this,arguments[t],arguments[t].enumerable);return this},extend:function(){var t=h(this);return t.inject.apply(t,arguments)},each:function(t,e){return i(this,t,e)},clone:function(){return new this.constructor(this)},statics:{each:i,create:h,define:c,describe:u,copy: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(){for(var e=0,n=arguments.length;n>e;e++)if(arguments[e]!==t)return arguments[e];return null}}})};"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?a.instance.number(t):"string"===n?"'"+t+"'":t))}},[]).join(", ")+" }"},exportJSON:function(t){return e.exportJSON(this,t)},toJSON:function(){return e.serialize(this)},_set:function(n,i){if(n&&e.isPlainObject(n)){var r=n._filtering||n;for(var s in r)if(s in this&&r.hasOwnProperty(s)&&(!i||!i[s])){var a=n[s];a!==t&&(this[s]=a)}return!0}},statics:{exports:{enumerable:!0},extend:function re(){var t=re.base.apply(this,arguments),n=t.prototype._class;return n&&!e.exports[n]&&(e.exports[n]=t),t},equals:function(t,n){function i(t,e){for(var n in t)if(t.hasOwnProperty(n)&&!e.hasOwnProperty(n))return!1;return!0}if(t===n)return!0;if(t&&t.equals)return t.equals(n);if(n&&n.equals)return n.equals(t);if(Array.isArray(t)&&Array.isArray(n)){if(t.length!==n.length)return!1;for(var r=0,s=t.length;s>r;r++)if(!e.equals(t[r],n[r]))return!1;return!0}if(t&&"object"==typeof t&&n&&"object"==typeof n){if(!i(t,n)||!i(n,t))return!1;for(var r in t)if(t.hasOwnProperty(r)&&!e.equals(t[r],n[r]))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){return this.isPlainObject(t)||Array.isArray(t)},serialize:function(t,n,i,r){n=n||{};var s,o=!r;if(o&&(n.formatter=new a(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){s=t._serialize(n,r);var h=t._class;!h||i||s._compact||s[0]===h||s.unshift(h)}else if(Array.isArray(t)){s=[];for(var u=0,l=t.length;l>u;u++)s[u]=e.serialize(t[u],n,i,r);i&&(s._compact=!0)}else if(e.isPlainObject(t)){s={};for(var u in t)t.hasOwnProperty(u)&&(s[u]=e.serialize(t[u],n,i,r))}else s="number"==typeof t?n.formatter.number(t,n.precision):t;return o&&r.length>0?[["dictionary",r.definitions],s]:s},deserialize:function(t,n,i){var r=t,s=!i;if(i=i||{},Array.isArray(t)){var a=t[0],o="dictionary"===a;if(!o){if(i.dictionary&&1==t.length&&/^#/.test(a))return i.dictionary[a];a=e.exports[a]}r=[];for(var h=a?1:0,u=t.length;u>h;h++)r.push(e.deserialize(t[h],n,i));if(o)i.dictionary=r[0];else if(a){var l=r;n?r=n(a,l,s):(r=e.create(a.prototype),a.apply(r,l))}}else if(e.isPlainObject(t)){r={};for(var c in t)r[c]=e.deserialize(t[c],n,i)}return r},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,r){var s=n&&n.constructor===t?n:e.create(t.prototype),a=s===n;if(!r&&1===i.length&&s instanceof y&&(!(s instanceof x)||a)){var o=i[0];e.isPlainObject(o)&&(o.insert=!1)}return t.apply(s,i),a&&(n=null),s})},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={attach:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.attach(e,t)},this),t;var r=this._eventTypes[n];if(r){var s=this._handlers=this._handlers||{};s=s[n]=s[n]||[],-1==s.indexOf(i)&&(s.push(i),r.install&&1==s.length&&r.install.call(this,n))}},detach:function(n,i){if("string"!=typeof n)return e.each(n,function(t,e){this.detach(e,t)},this),t;var r,s=this._eventTypes[n],a=this._handlers&&this._handlers[n];s&&a&&(!i||-1!=(r=a.indexOf(i))&&1==a.length?(s.uninstall&&s.uninstall.call(this,n),delete this._handlers[n]):-1!=r&&a.splice(r,1))},once:function(t,e){this.attach(t,function(){e.apply(this,arguments),this.detach(t,e)})},fire:function(t,e){var n=this._handlers&&this._handlers[t];if(!n)return!1;for(var i=[].slice.call(arguments,1),r=this,s=0,a=n.length;a>s;s++)if(n[s].apply(r,i)===!1&&e&&e.stop){e.stop();break}return!0},responds:function(t){return!(!this._handlers||!this._handlers[t])},on:"#attach",off:"#detach",trigger:"#fire",_installEvents:function(t){var e=this._handlers,n=t?"install":"uninstall";for(var i in e)if(e[i].length>0){var r=this._eventTypes[i],s=r[n];s&&s.call(this,i)}},statics:{inject:function se(){for(var t=0,n=arguments.length;n>t;t++){var i=arguments[t],r=i._events;if(r){var s={};e.each(r,function(t,n){var r="string"==typeof t,a=r?t:n,o=e.capitalize(a),h=a.substring(2).toLowerCase();s[h]=r?{}:t,a="_"+a,i["get"+o]=function(){return this[a]},i["set"+o]=function(t){var e=this[a];e&&this.detach(h,e),t&&this.attach(h,t),this[a]=t}}),i._eventTypes=s}se.base.call(this,i)}return this}}},r=e.extend({_class:"PaperScope",initialize:function ae(t){if(paper=this,this.settings={applyMatrix:!0,handleSize:4,hitTolerance:0},this.project=null,this.projects=[],this.tools=[],this.palettes=[],this._id=t&&(t.getAttribute("id")||t.src)||"paperscope-"+ae._id++,t&&t.setAttribute("id",this._id),ae._scopes[this._id]=this,!this.support){var e=te.getContext(1,1);ae.prototype.support={nativeDash:"setLineDash"in e||"mozDash"in e,nativeBlendModes:ee.nativeModes},te.release(e)}},version:"0.9.16",getView:function(){return this.project&&this.project.view},getPaper:function(){return this},execute:function(t){paper.PaperScript.execute(t,this),H.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 r._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 t&&t.getAttribute&&(t=t.getAttribute("id")),this._scopes[t]||null},getAttribute:t("get"),hasAttribute:t("has")}}}),s=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.fire("deactivate"),this._scope[this._reference]=this,this.fire("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)}}),a=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},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)}});a.instance=new a;var o=new function(){function e(e,n,i){var r=n===t,s=n-c,a=i+c,o=0;return function(t){return(r||t>s&&a>t)&&(e[o++]=n>t?n:t>i?i:t),o}}var n=[[.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]],i=[[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]],r=Math.abs,s=Math.sqrt,a=Math.pow,h=Math.cos,u=Math.PI,l=1e-5,c=1e-11;return{TOLERANCE:l,EPSILON:c,KAPPA:4*(s(2)-1)/3,isZero:function(t){return r(t)<=c},integrate:function(t,e,r,s){for(var a=n[s-2],o=i[s-2],h=.5*(r-e),u=h+e,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,i,s,a,o){for(var h=0;a>h;h++){var u=t(n),l=u/e(n),c=n-l;if(r(l)0?(s=n,n=i>=c?.5*(i+s):c):(i=n,n=c>=s?.5*(i+s):c)}return n},solveQuadratic:function(t,n,i,a,o,h){var u=e(a,o,h);if(r(t)=c?u(-i/n):r(i)f)return 0;var _=f>d?s(f-d):0,g=u(_-l);return _>0&&(g=u(-_-l)),g},solveCubic:function(t,n,i,l,d,f,_){if(r(t)0?1:-1;return g(2*-b*x-n),g(b*x-n)}if(0>w){var x=s(v),C=Math.acos(m/(x*x*x))/3,S=-2*x,P=2*u/3;return g(S*h(C)-n),g(S*h(C+P)-n),g(S*h(C-P)-n)}var k=(m>0?-1:1)*a(r(m)+s(w),1/3);return g(k+v/k-n)}}},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=a.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)]},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)},transform:function(t){return t?t._transformPoint(this):this},getDistance:function(t,n){var i=h.read(arguments),r=i.x-this.x,s=i.y-this.y,a=r*r+s*s;return n=e.read(arguments),n?a:Math.sqrt(a)},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();o.isZero(n)&&this.getAngle(),this.set(this.x*n,this.y*n)}},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},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();return o.isZero(e)?0/0:Math.acos(this.dot(t)/e)}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},getDirectedAngle:function(t){var e=t;return e=h.read(arguments),180*Math.atan2(this.cross(e),this.dot(e))/Math.PI},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},isInside:function(t){return t.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 c(this.width,this.height)},toString:function(){var t=a.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=c.read(arguments);return new c(this.width+t.width,this.height+t.height)},subtract:function(){var t=c.read(arguments);return new c(this.width-t.width,this.height-t.height)},multiply:function(){var t=c.read(arguments);return new c(this.width*t.width,this.height*t.height)},divide:function(){var t=c.read(arguments);return new c(this.width/t.width,this.height/t.height)},modulo:function(){var t=c.read(arguments);return new c(this.width%t.width,this.height%t.height)},negate:function(){return new c(-this.width,-this.height)},isZero:function(){return o.isZero(this.width)&&o.isZero(this.height)},isNaN:function(){return isNaN(this.width)||isNaN(this.height)},statics:{min:function(t,e){return new c(Math.min(t.width,e.width),Math.min(t.height,e.height))},max:function(t,e){return new c(Math.max(t.width,e.width),Math.max(t.height,e.height))},random:function(){return new c(Math.random(),Math.random())}}},e.each(["round","ceil","floor","abs"],function(t){var e=Math[t];this[t]=function(){return new c(e(this.width),e(this.height))}},{})),d=c.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)}}),f=e.extend({_class:"Rectangle",_readIndex:!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"),l=e.peek(arguments);if(this.x=u.x,this.y=u.y,l&&l.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=c.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 f(this.x,this.y,this.width,this.height)},equals:function(t){var n=e.isPlainValue(t)?f.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=a.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?c:d;return new e(this.width,this.height,this,"setSize")},setSize:function(){var t=c.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(f.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=f.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=f.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 f(e,n,i-e,r-n)},unite:function(){var t=f.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 f(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 f(e,n,i-e,r-n)},expand:function(){var t=c.read(arguments),e=t.width,n=t.height;return new f(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)}},new function(){return 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)}},{})}),_=f.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=f.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 oe(t){var e=arguments.length,n=!0;if(6===e?this.set.apply(this,arguments):1===e?t instanceof oe?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(5))},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=a.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(){var t=this._owner;return t?(t.transform(null,!0),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;return this._a=t._a*e+t._c*n,this._b=t._b*e+t._d*n,this._c=t._a*i+t._c*r,this._d=t._b*i+t._d*r,this._tx+=t._tx*e+t._ty*n,this._ty+=t._tx*i+t._ty*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;return this._a=t._a*e+t._b*i,this._b=t._a*n+t._b*r,this._c=t._c*e+t._d*i,this._d=t._c*n+t._d*r,this._tx=t._a*s+t._b*a+t._tx,this._ty=t._c*s+t._d*a+t._ty,this._changed(),this},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,i,r){return arguments.length<5?this._transformPoint(h.read(arguments)):this._transformCoordinates(t,e,n,i,r)},_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,i,r){for(var s=e,a=i,o=s+2*r;o>s;){var h=t[s++],u=t[s++];n[a++]=h*this._a+u*this._b+this._tx,n[a++]=h*this._c+u*this._d+this._ty}return n},_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,0,s,0,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 f),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)&&!o.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(o.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 a=Math.sqrt(n*n+i*i);return n/=a,i/=a,s/=a,e*n>t*i&&(t=-t,e=-e,s=-s,r=-r),{scaling:new h(r,a),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)}},new function(){return 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()}},{})}),p=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 p.intersect(this._px,this._py,this._vx,this._vy,t._px,t._py,t._vx,t._vy,!0,e)},getSide:function(t){return p.getSide(this._px,this._py,this._vx,this._vy,t.x,t.y,!0)},getDistance:function(t){return Math.abs(p.getSignedDistance(this._px,this._py,this._vx,this._vy,t.x,t.y,!0))},statics:{intersect:function(t,e,n,i,r,s,a,u,l,c){l||(n-=t,i-=e,a-=r,u-=s);var d=u*n-a*i;if(!o.isZero(d)){var f=t-r,_=e-s,g=(a*_-u*f)/d,p=(n*_-i*f)/d;if((c||g>=0&&1>=g)&&(c||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,a){a||(n-=t,i-=e);var o=i/n,h=e-o*t;return(s-o*r-h)/Math.sqrt(o*o+1)}}}),v=s.extend({_class:"Project",_list:"projects",_reference:"project",initialize:function(t){s.call(this,!0),this.layers=[],this.symbols=[],this._currentStyle=new q,this.activeLayer=new x,t&&(this.view=t instanceof H?t:H.create(t)),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 this.layers.length<=1&&(!this.activeLayer||this.activeLayer.isEmpty())},remove:function he(){return he.base.call(this)?(this.view&&this.view.remove(),!0):!1},getCurrentStyle:function(){return this._currentStyle},setCurrentStyle:function(t){this._currentStyle.initialize(t)},getIndex:function(){return this._index},addChild:function(t){return t instanceof x?(e.splice(this.layers,[t]),this.activeLayer||(this.activeLayer=t)):t instanceof y?(this.activeLayer||this.addChild(new x(y.NO_INSERT))).addChild(t):t=null,t},getSelectedItems:function(){var t=[];for(var e in this._selectedItems){var n=this._selectedItems[e];n.isInserted()&&t.push(n)}return t},getOptions:function(){return this._scope.settings},_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=P.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}},new function(){function t(t,e,n){for(var i=t.layers,r=n&&[],s=0,a=i.length;a>s;s++){var o=i[s][n?"getItems":"getItem"](e);if(n)r.push.apply(r,o);else if(o)return o}return n?r:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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,trackTransforms:!0}),s=0,a=this.layers.length;a>s;s++)this.layers[s].draw(t,r);if(t.restore(),this._selectedItemCount>0){t.save(),t.strokeWidth=1;for(var o in this._selectedItems){var u=this._selectedItems[o],l=this._scope.settings.handleSize;if(half=l/2,u._updateVersion===this._updateVersion&&(u._drawSelected||u._boundsSelected)){var c=u.getSelectedColor()||u.getLayer().getSelectedColor();t.strokeStyle=t.fillStyle=c?c.toCanvasStyle(t):"#009dec";var d=n.clone().concatenate(u._globalMatrix);if(u._drawSelected&&u._drawSelected(t,d),u._boundsSelected){var f=d._transformCorners(u.getInternalBounds());t.beginPath();for(var s=0;8>s;s++)t[0===s?"moveTo":"lineTo"](f[s],f[++s]);t.closePath(),t.stroke();for(var s=0;8>s;s++)t.fillRect(f[s]-half,f[++s]-half,l,l)}}}t.restore()}}}),m=e.extend({_class:"Symbol",initialize:function ue(t,e){this._id=ue._id=(ue._id||0)+1,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){4&t&&y._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(5)},place:function(t){return new S(this,t)},clone:function(){return new m(this._definition.clone(!1))}}),y=e.extend(n,{statics:{extend:function le(t){t._serializeFields&&(t._serializeFields=new e(this.prototype._serializeFields,t._serializeFields));var n=le.base.apply(this,arguments),i=n.prototype,r=i._class;return r&&(i._type=e.hyphenate(r)),n},NO_INSERT:{insert:!1}},_class:"Item",_applyMatrix:!0,_canApplyMatrix:!0,_boundsSelected:!1,_selectChildren:!1,_serializeFields:{name:null,matrix:new g,pivot:null,locked:!1,visible:!0,blendMode:"normal",opacity:1,guide:!1,selected:!1,clipMask:!1,applyMatrix:null,data:{}},initialize:function(){},_initialize:function(t,e){var n=t&&t.internal===!0,i=this._matrix=new g;return project=paper.project,n||(this._id=y._id=(y._id||0)+1),this._applyMatrix=this._canApplyMatrix&&paper.settings.applyMatrix,e&&i.translate(e),i._owner=this,this._style=new q(project._currentStyle,this),this._project||(n||t&&t.insert===!1?this._setProject(project):(project.activeLayer||new x).addChild(this)),t&&t!==y.NO_INSERT?this._set(t,{insert:!0}):!0},_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._project.view._eventCounters;if(n)for(var i in t)n[i]=(n[i]||0)+(t[i][e]||0)},uninstall:function(e){var n=this._project.view._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._project.view._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 w||i(this._style._defaults),[this._class,r]},_changed:function(e){var n=this._parentSymbol,i=this._parent||n,r=this._project;if(4&e&&(this._bounds=this._position=this._decomposed=this._globalMatrix=t),i&&12&e&&y._clearBoundsCache(i),2&e&&y._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,{insert:!0}),this},getId:function(){return this._id},getType:function(){return this._type},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.");if(e&&this._parent){for(var i=this._parent._children,r=this._parent._namedChildren,s=e,a=1;n&&i[e];)e=s+" "+a++;(r[e]=r[e]||[]).push(this),i[e]=this}this._name=e||t,this._changed(32)},getStyle:function(){return this._style},setStyle:function(t){this.getStyle().set(t)},hasFill:function(){return this.getStyle().hasFill()},hasStroke:function(){return this.getStyle().hasStroke()},hasShadow:function(){return this.getStyle().hasShadow()}},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?32:33))}},{}),{_locked:!1,_visible:!0,_blendMode:"normal",_opacity:1,_guide:!1,isSelected:function(){if(this._selectChildren)for(var t=0,e=this._children.length;e>t;t++)if(this._children[t].isSelected())return!0;return this._selected},setSelected:function(t,e){if(!e&&this._selectChildren)for(var n=0,i=this._children.length;i>n;n++)this._children[n].setSelected(t);(t=!!t)^this._selected&&(this._selected=t,this._project._updateSelection(this),this._changed(33))},_selected:!1,isFullySelected:function(){if(this._children&&this._selected){for(var t=0,e=this._children.length;e>t;t++)if(!this._children[t].isFullySelected())return!1;return!0}return this._selected},setFullySelected:function(t){if(this._children)for(var e=0,n=this._children.length;n>e;e++)this._children[e].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(33),this._parent&&this._parent._changed(256))},_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,"setAnchor")}return e},setPivot:function(){this._pivot=h.read(arguments),this._position=t},_pivot:null,getRegistration:"#getPivot",setRegistration:"#setPivot"},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,null,r);return"bounds"===t?new _(a.x,a.y,a.width,a.height,this,"setBounds"):a},this[t]={get:this[n]}},{_getBounds:function(t,e,n){var i=this._children;if(!i||0==i.length)return new f;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,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 f(r,a,s-r,o-a):new f},setBounds:function(){var t=f.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,a=this._parent||this._parentSymbol;if(n&&a){var o=n._id,h=a._boundsCache=a._boundsCache||{ids:{},list:[]};h.ids[o]||(h.list.push(n),h.ids[o]=n)}if(s&&this._bounds&&this._bounds[s])return this._bounds[s].clone();e=e?r?e.clone().concatenate(r):e:r;var u=this._getBounds(i||t,e,s?this:n);if(s){this._bounds||(this._bounds={});var l=this._bounds[s]=u.clone();l._internal=!!i}return u},statics:{_clearBoundsCache:function(e){if(e._boundsCache){for(var n=0,i=e._boundsCache.list,r=i.length;r>n;n++){var s=i[n];s._bounds=s._position=t,s!==e&&s._boundsCache&&s._clearBoundsCache()}e._boundsCache=t}}}}),{_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(){var t=this._decomposed||this._decompose();return t&&t.scaling},setScaling:function(){var t=this.getScaling();if(null!=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(t){this._matrix.initialize(t),this._applyMatrix?this.transform(null,!0):this._changed(5)},getGlobalMatrix:function(){var t=this._globalMatrix,e=this._project._updateVersion;return t&&t._updateVersion!==e&&(t=null),t||(t=this._globalMatrix=this._matrix.clone(),this._parent&&t.preConcatenate(this._parent.getGlobalMatrix()),t._updateVersion=e),t},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)},_installEvents:function ce(t){ce.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 x)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},isInserted:function(){return this._parent?this._parent.isInserted():!1},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(y.NO_INSERT),t)},_clone:function(n,i){if(n.setStyle(this._style),this._children)for(var r=0,s=this._children.length;s>r;r++)n.addChild(this._children[r].clone(!1),!0);(i||i===t)&&n.insertAbove(this);for(var a=["_locked","_visible","_blendMode","_opacity","_clipMask","_guide","_applyMatrix"],r=0,s=a.length;s>r;r++){var o=a[r];this.hasOwnProperty(o)&&(n[o]=this[o])}return n._matrix.initialize(this._matrix),n._data=this._data?e.clone(this._data):null,n.setSelected(this._selected),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=this._project.view,r=(t||i&&i.getResolution()||72)/72,s=n.getTopLeft().floor(),a=n.getBottomRight().ceil();size=new c(a.subtract(s)),canvas=te.getCanvas(size.multiply(r)),ctx=canvas.getContext("2d"),matrix=(new g).scale(r).translate(s.negate()),ctx.save(),matrix.applyToContext(ctx),this.draw(ctx,new e({transforms:[matrix]})),ctx.restore();var o=new C(y.NO_INSERT);return o.setCanvas(canvas),o.transform((new g).translate(s.add(size.divide(2))).scale(1/r)),o.insertAbove(this),o},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())},hitTest:function(n,i){function r(i,r){var s=p["get"+r]();return n.subtract(s).divide(l).length<=1?new P(i,_,{name:e.hyphenate(r),point:s}):t}if(n=h.read(arguments),i=P.getOptions(e.read(arguments)),this._locked||!this._visible||this._guide&&!i.guides)return null;var s=this._matrix,a=i._totalMatrix,o=this._project.view,u=i._totalMatrix=a?a.clone().concatenate(s):this.getGlobalMatrix().clone().preConcatenate(o?o._matrix:new g),l=i._tolerancePadding=new c(T._getPenPadding(1,u.inverted())).multiply(Math.max(i.tolerance,1e-5));if(n=s._inverseTransform(n),!this._children&&!this.getInternalRoughBounds().expand(l.multiply(2))._containsPoint(n))return null;var d,f=!(i.guides&&!this._guide||i.selected&&!this._selected||i.type&&this._type!==i.type),_=this;if(f&&(i.center||i.bounds)&&this._parent){var p=this.getInternalBounds();if(i.center&&(d=r("center","Center")),!d&&i.bounds)for(var v=["TopLeft","TopRight","BottomLeft","BottomRight","LeftCenter","TopCenter","RightCenter","BottomCenter"],m=0;8>m&&!d;m++)d=r("bounds",v[m])}var y=!d&&this._children;if(y)for(var w=this._getChildHitTestOptions(i),m=y.length-1;m>=0&&!d;m--)d=y[m].hitTest(n,w);return!d&&f&&(d=this._hitTest(n,i)),d&&d.point&&(d.point=s.transform(d.point)),i._totalMatrix=a,d},_getChildHitTestOptions:function(t){return t},_hitTest:function(e,n){return n.fill&&this.hasFill()&&this._contains(e)?new P("fill",this):t},matches:function(t){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}for(var i in t)if(t.hasOwnProperty(i)){var r=this[i],s=t[i];if(s instanceof RegExp){if(!s.test(r))return!1}else if("function"==typeof s){if(!s(r))return!1}else if(e.isPlainObject(s)){if(!n(s,r))return!1}else if(!e.equals(r,s))return!1}return!0}},new function(){function t(e,n,i){for(var r=e._children,s=i&&[],a=0,o=r&&r.length;o>a;a++){var h=r[a];if(h.matches(n)){if(!i)return h;s.push(h)}var u=t(h,n,i);if(i)s.push.apply(s,u);else if(u)return u}return i?s:null}return{getItems:function(e){return t(this,e,!0)},getItem:function(e){return t(this,e,!1)}}},{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=this.insertChildren(t,[e],n);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];r&&o._type!==r?n.splice(a,1):o._remove(!0)}e.splice(s,n,t,0);for(var a=0,h=n.length;h>a;a++){var o=n[a];o._parent=this,o._setProject(this._project,!0),o._name&&o.setName(o._name)}this._changed(7)}else n=null;return n},_insert:function(t,e,n){if(!e._parent)return null;var i=e._index+(t?1:0);return e._parent===this._parent&&i>this._index&&i--,e._parent.insertChild(i,this,n)},insertAbove:function(t,e){return this._insert(!0,t,e)},insertBelow:function(t,e){return this._insert(!1,t,e)},sendToBack:function(){return this._parent.insertChild(0,this)},bringToFront:function(){return this._parent.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._children,e=this._parent._namedChildren,n=this._name,i=e[n],r=i?i.indexOf(this):-1;-1!=r&&(t[n]==this&&delete t[n],i.splice(r,1),i.length?t[n]=i[i.length-1]:delete e[n])},_remove:function(t){return this._parent?(this._name&&this._removeNamed(),null!=this._index&&e.splice(this._parent._children,null,this._index,1),this._installEvents(!1),t&&this._parent._changed(7),this._parent=null,!0):!1},remove:function(){return this._remove(!0)},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(!1);return i.length>0&&this._changed(7),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(7)}},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},_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},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|compound-path)$/.test(e._type)&&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){t&&t.isIdentity()&&(t=null);var n=this._matrix,i=(e||this._applyMatrix)&&(!n.isIdentity()||t);if(!t&&!i)return this;if(t&&n.preConcatenate(t),i=i&&this._transformContent(n)){var r=this._pivot,s=this._style,a=s.getFillColor(!0),o=s.getStrokeColor(!0);r&&r.transform(n),a&&a.transform(n),o&&o.transform(n),n.reset(!0)}var h=this._bounds,u=this._position;this._changed(5);var l=h&&t&&t.decompose();if(l&&!l.shearing&&0===l.rotation%90){for(var c in h){var d=h[c];(i||!d._internal)&&t._transformBounds(d,d)}var f=this._boundsGetter,d=h[f&&f.getBounds||f||"getBounds"];d&&(this._position=d.getCenter(!0)),this._bounds=h}else t&&u&&(this._position=t._transformPoint(u,u));return this},_transformContent:function(t){var e=this._children;if(e){for(var n=0,i=e.length;i>n;n++)e[n].transform(t,!0);return!0}},globalToLocal:function(){var t=this.getGlobalMatrix();return t&&t._inverseTransform(h.read(arguments))},localToGlobal:function(){var t=this.getGlobalMatrix();return t&&t._transformPoint(h.read(arguments))},fitBounds:function(t,e){t=f.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 f(new h,new c(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){if(this._visible&&0!==this._opacity){var n=this._updateVersion=this._project._updateVersion,i=e.trackTransforms,r=e.transforms=e.transforms||[new g],s=this._matrix,a=r[r.length-1],o=a.clone().concatenate(s);if(o.isInvertible()){i&&(r||(r=e.transforms=[]),r.push(this._globalMatrix=o),o._updateVersion=n);var h,u,l,d=this._blendMode,f=this._opacity,_="normal"===d,p=ee.nativeModes[d],v=_&&1===f||(p||_&&1>f)&&this._canComposite();if(!v){var m=this.getStrokeBounds(a);if(!m.width||!m.height)return;l=e.offset,u=e.offset=m.getTopLeft().floor(),h=t,t=te.getContext(m.getSize().ceil().add(new c(1,1)),e.pixelRatio)}t.save(),v?(t.globalAlpha=f,p&&(t.globalCompositeOperation=d)):t.translate(-u.x,-u.y),(v?s:o).applyToContext(t),!v&&e.clipItem&&e.clipItem.draw(t,e.extend({clip:!0})),this._draw(t,e),t.restore(),i&&r.pop(),e.clip&&t.clip(),v||(ee.process(d,t,h,f,u.subtract(l).multiply(e.pixelRatio)),te.release(t),e.offset=l)}}},_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}})),w=y.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 de(e){de.base.call(this,e),258&e&&(this._clipItem=t)},_getClipItem:function(){if(this._clipItem!==t)return this._clipItem;for(var e=0,n=this._children.length;n>e;e++){var i=this._children[e];if(i._clipMask)return this._clipItem=i}return this._clipItem=null},isClipped:function(){return!!this._getClipItem()},setClipped:function(t){var e=this.getFirstChild();e&&e.setClipMask(t)},_draw:function(t,e){var n=e.clipItem=this._getClipItem();n&&n.draw(t,e.extend({clip:!0}));for(var i=0,r=this._children.length;r>i;i++){var s=this._children[i];s!==n&&s.draw(t,e)}e.clipItem=null}}),x=w.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,w.call(this,i),(r||r===t)&&(this._project.addChild(this),this.activate())},_remove:function fe(t){return this._parent?fe.base.call(this,t):null!=this._index?(this._project.activeLayer===this&&(this._project.activeLayer=this.getNextSibling()||this.getPreviousSibling()),e.splice(this._project.layers,null,this._index,1),this._installEvents(!1),this._project._needsUpdate=!0,!0):!1},getNextSibling:function _e(){return this._parent?_e.base.call(this):this._project.layers[this._index+1]||null},getPreviousSibling:function ge(){return this._parent?ge.base.call(this):this._project.layers[this._index-1]||null},isInserted:function pe(){return this._parent?pe.base.call(this):null!=this._index},activate:function(){this._project.activeLayer=this},_insert:function ve(t,n,i){return n instanceof x&&!n._parent?(this._remove(!0),e.splice(n._project.layers,[this],n._index+(t?1:0),0),this._setProject(n._project,!0),this):ve.base.call(this,t,n,i)}}),b=y.extend({_class:"Shape",_applyMatrix:!1,_canApplyMatrix:!1,_boundsSelected:!0,_serializeFields:{shape:null,size:null,radius:null},initialize:function(t){this._initialize(t)},_equals:function(t){return this._shape===t._shape&&this._size.equals(t._size)&&e.equals(this._radius,t._radius)},clone:function(t){var e=new b(y.NO_INSERT);return e.setShape(this._shape),e.setSize(this._size),e.setRadius(this._radius),this._clone(e,t)},getShape:function(){return this._shape},setShape:function(t){this._shape=t},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(this._size){if(!this._size.equals(t)){var e=this._shape,n=t.width,i=t.height;if("rectangle"===e){var r=c.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(5)}}else this._size=t.clone()},getRadius:function(){var t=this._radius;return"circle"===this._shape?t:new d(t.width,t.height,this,"setRadius")},setRadius:function(t){var e=this._shape;if("circle"===e){if(t===this._radius)return;var n=2*t;this._radius=t,this._size.set(n,n)}else if(t=c.read(arguments),this._radius){if(this._radius.equals(t))return;if(this._radius.set(t.width,t.height),"rectangle"===e){var n=c.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(5)},isEmpty:function(){return!1},toPath:function(n){var i=new(T[e.capitalize(this._shape)])({center:new h,size:this._size,radius:this._radius,insert:!1});return i.setStyle(this._style),i.transform(this._matrix),(n||n===t)&&i.insertAbove(this),i},_draw:function(t,e){var n=this._style,i=n.hasFill(),r=n.hasStroke(),s=e.clip;if(i||r||s){var a=this._radius,o=this._shape;if(t.beginPath(),"circle"===o)t.arc(0,0,a,0,2*Math.PI,!0);else{var h=a.width,u=a.height,l=.5522847498307936;if("ellipse"===o){var c=h*l,d=u*l;t.moveTo(-h,0),t.bezierCurveTo(-h,-d,-c,-u,0,-u),t.bezierCurveTo(c,-u,h,-d,h,0),t.bezierCurveTo(h,d,c,u,0,u),t.bezierCurveTo(-c,u,-h,d,-h,0)}else{var f=this._size,_=f.width,g=f.height;if(0===h&&0===u)t.rect(-_/2,-g/2,_,g);else{l=1-l;var p=_/2,v=g/2,c=h*l,d=u*l;t.moveTo(-p,-v+u),t.bezierCurveTo(-p,-v+d,-p+c,-v,-p+h,-v),t.lineTo(p-h,-v),t.bezierCurveTo(p-c,-v,p,-v+d,p,-v+u),t.lineTo(p,v-u),t.bezierCurveTo(p,v-d,p-c,v,p-h,v),t.lineTo(-p+h,v),t.bezierCurveTo(-p+c,v,-p,v-d,-p,v-u)}}}t.closePath()}s||!i&&!r||(this._setStyles(t),i&&(t.fill(n.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),r&&t.stroke())},_canComposite:function(){return!(this.hasFill()&&this.hasStroke())},_getBounds:function(t,e){var n=new f(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 f(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._shape){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},_hitTest:function i(n,r){var s=!1;if(this.hasStroke()){var a=this._shape,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 f(this._size).setCenter(0,0),d=c.expand(h),_=c.expand(-h); +s=d._containsPoint(n)&&!_._containsPoint(n)}}else"ellipse"===a&&(o=e(n,o)),s=2*Math.abs(n.getLength()-o)<=h}return s?new P("stroke",this):i.base.apply(this,arguments)}}},{statics:new function(){function t(t,n,i,r,s){var a=new b(e.getNamed(s));return a._shape=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 c(2*i),i,arguments)},Rectangle:function(){var e=f.readNamed(arguments,"rectangle"),n=c.min(c.readNamed(arguments,"radius"),e.getSize(!0).divide(2));return t("rectangle",e.getCenter(!0),e.getSize(!0),n,arguments)},Ellipse:function(){var e=b._readEllipse(arguments);return radius=e.radius,t("ellipse",e.center,radius.multiply(2),radius,arguments)},_readEllipse:function(t){var n,i;if(e.hasNamed(t,"radius"))n=h.readNamed(t,"center"),i=c.readNamed(t,"radius");else{var r=f.readNamed(t,"rectangle");n=r.getCenter(!0),i=r.getSize(!0).divide(2)}return{center:n,radius:i}}}}}),C=y.extend({_class:"Raster",_applyMatrix:!1,_canApplyMatrix:!1,_boundsGetter:"getBounds",_boundsSelected:!0,_serializeFields:{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 c)},_equals:function(t){return this.getSource()===t.getSource()},clone:function(t){var e=new C(y.NO_INSERT),n=this._image,i=this._canvas;if(n)e.setImage(n);else if(i){var r=te.getCanvas(this._size);r.getContext("2d").drawImage(i,0,0),e.setCanvas(r)}return this._clone(e,t)},getSize:function(){var t=this._size;return new d(t.width,t.height,this,"setSize")},setSize:function(){var t=c.read(arguments);if(!this._size.equals(t)){var e=this.getElement();this.setCanvas(te.getCanvas(t)),e&&this.getContext(!0).drawImage(e,0,0,t.width,t.height)}},getWidth:function(){return this._size.width},getHeight:function(){return this._size.height},isEmpty:function(){return 0==this._size.width&&0==this._size.height},getPpi: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 c(72/n.getLength(),72/i.getLength())},getImage:function(){return this._image},setImage:function(t){this._canvas&&te.release(this._canvas),t.getContext?(this._image=null,this._canvas=t):(this._image=t,this._canvas=null),this._size=new c(t.naturalWidth||t.width,t.naturalHeight||t.height),this._context=null,this._changed(133)},getCanvas:function(){if(!this._canvas){var t=te.getContext(this._size);try{this._image&&t.drawImage(this._image,0,0),this._canvas=t.canvas}catch(e){te.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(129)),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._project.view;t&&(paper=t._scope),i.setImage(n),i.fire("load"),t&&t.update()}var n,i=this;n=document.getElementById(t)||new Image,n.naturalWidth&&n.naturalHeight?setTimeout(e,0):(Z.add(n,{load:e}),n.src||(n.src=t)),this.setImage(n)},getElement:function(){return this._canvas||this._image},getSubCanvas:function(t){var t=f.read(arguments),e=te.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(t){var t=f.read(arguments),e=new C(y.NO_INSERT);return e.setCanvas(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 O?(i=t,n=t.getBounds()):t.width?n=new f(t):t.x&&(n=new f(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=C._sampleContext;o?o.clearRect(0,0,r+1,r+1):o=C._sampleContext=te.getContext(new c(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,transforms:[h]})),this._matrix.applyToContext(o),o.drawImage(this.getElement(),-this._size.width/2,-this._size.height/2),o.restore();for(var u=o.getImageData(.5,.5,Math.ceil(s),Math.ceil(a)).data,l=[0,0,0],d=0,_=0,p=u.length;p>_;_+=4){var v=u[_+3];d+=v,v/=255,l[0]+=u[_]*v,l[1]+=u[_+1]*v,l[2]+=u[_+2]*v}for(var _=0;3>_;_++)l[_]/=d;return d?B.read(l):null},getPixel:function(t){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=c.read(arguments);return this.getContext().createImageData(t.width,t.height)},getImageData:function(t){var t=f.read(arguments);return t.isEmpty()&&(t=new f(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 f(this._size).setCenter(0,0);return e?e._transformBounds(n):n},_hitTest:function(t){if(this._contains(t)){var e=this;return new P("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}}),S=y.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 m?e:new m(e))},_equals:function(t){return this._symbol===t._symbol},getSymbol:function(){return this._symbol},setSymbol:function(t){this._symbol=t,this._changed(5)},clone:function(t){var e=new S(y.NO_INSERT);return e.setSymbol(this._symbol),this._clone(e,t)},isEmpty:function(){return this._symbol._definition.isEmpty()},_getBounds:function(t,e,n){return this.symbol._definition._getCachedBounds(t,e,n)},_hitTest:function(t,e,n){var i=this._symbol._definition._hitTest(t,e,n);return i&&(i.item=this),i},_draw:function(t,e){this.symbol._definition.draw(t,e)}}),P=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 t&&t._merged?t: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,_merged:!0},t)}}}),M=e.extend({_class:"Segment",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 z(o,this,"_point"),new z(h,this,"_handleIn"),new z(u,this,"_handleOut")},_serialize:function(t){return e.serialize(this.isLinear()?this._point:[this._point,this._handleIn,this._handleOut],t,!0)},_changed:function(t){var e=this._path;if(e){var n,i,r=e._curves,s=this._index;r&&(t&&t!==this._point&&t!==this._handleIn||!(n=r[s-1]||e._closed&&r[r.length-1])||n._changed(),t&&t!==this._point&&t!==this._handleOut||!(i=r[s])||i._changed()),e._changed(5)}},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)},isLinear:function(){return this._handleIn.isZero()&&this._handleOut.isZero()},setLinear:function(){this._handleIn.set(0,0),this._handleOut.set(0,0)},isColinear:function(t){var e=this.getNext(),n=t.getNext();return this._handleOut.isZero()&&e._handleIn.isZero()&&t._handleOut.isZero()&&n._handleIn.isZero()&&e._point.subtract(this._point).isColinear(n._point.subtract(t._point))},isOrthogonal:function(){var t=this.getPrevious(),e=this.getNext();return t._handleOut.isZero()&&this._handleIn.isZero()&&this._handleOut.isZero()&&e._handleIn.isZero()&&this._point.subtract(t._point).isOrthogonal(e._point.subtract(this._point))},isArc:function(){var t=this.getNext(),e=this._handleOut,n=t._handleIn,i=.5522847498307936;if(e.isOrthogonal(n)){var r=this._point,s=t._point,a=new p(r,e,!0).intersect(new p(s,n,!0),!0);return a&&o.isZero(e.getLength()/a.subtract(r).getLength()-i)&&o.isZero(n.getLength()/a.subtract(s).getLength()-i)}return!1},_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(33))},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?(t._closed||e!=t._segments.length-1||e--,t.getCurves()[e]||null):null},getLocation:function(){var t=this.getCurve();return t?new A(t,t.getNext()?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 M(this._point,this._handleOut,this._handleIn)},remove:function(){return this._path?!!this._path.removeSegment(this._index):!1},clone:function(){return new M(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,0,e,0,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}}),z=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 o.isZero(this._x)&&o.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=arguments.length;if(3===h)this._path=t,this._segment1=e,this._segment2=n;else if(0===h)this._segment1=new M,this._segment2=new M;else if(1===h)this._segment1=new M(t.segment1),this._segment2=new M(t.segment2);else if(2===h)this._segment1=new M(t),this._segment2=new M(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 M(u,null,l),this._segment2=new M(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 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=this.isLinear()?this._segment2._point.getDistance(this._segment1._point):I.getLength(this.getValues(),0,1)),this._length},getArea:function(){return I.getArea(this.getValues())},getPart:function(t,e){return new I(I.getPart(this.getValues(),t,e))},getPartLength:function(t,e){return I.getLength(this.getValues(),t,e)},isLinear:function(){return this._segment1._handleOut.isZero()&&this._segment2._handleIn.isZero()},isHorizontal:function(){return this.isLinear()&&o.isZero(this._segment1._point._y-this._segment2._point._y)},getIntersections:function(t){return I.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-5,s=null;if(i>r&&1-r>i){var a=I.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 M(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 I(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 I(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 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(", ")+" }"},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,0,o,0,6),o},evaluate:function(t,e,n){var i,r,s=t[0],a=t[1],o=t[2],u=t[3],l=t[4],c=t[5],d=t[6],f=t[7],_=1e-5;if(0===n&&(_>e||e>1-_)){var g=_>e;i=g?s:d,r=g?a:f}else{var p=3*(o-s),v=3*(l-o)-p,m=d-s-p-v,y=3*(u-a),w=3*(c-u)-y,x=f-a-y-w;if(0===n)i=((m*e+v)*e+p)*e+s,r=((x*e+w)*e+y)*e+a;else if(_>e&&o===s&&u===a||e>1-_&&l===d&&c===f?(i=d-s,r=f-a):_>e?(i=p,r=y):e>1-_?(i=3*(d-l),r=3*(f-c)):(i=(3*m*e+2*v)*e+p,r=(3*x*e+2*w)*e+y),3===n){var b=6*m*e+2*v,C=6*x*e+2*w;return(i*C-r*b)/Math.pow(i*i+r*r,1.5)}}return 2===n?new h(r,-i):new h(i,r)},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 a=t[e],h=t[e+2],u=t[e+4],l=t[e+6],c=3*(h-a),d=3*(u-h)-c,f=l-a-c-d;return o.solveCubic(f,d,c,a-n,i,r,s)},getParameterOf:function(t,e,n){var i=1e-5;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=I.subdivide(t,e)[1]),1>n&&(t=I.subdivide(t,(n-e)/(1-e))[0]),t},isLinear:function(t){var e=o.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[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},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 f(e[0],e[1],n[0]-e[0],n[1]-e[1])},_addBounds:function(t,e,n,i,r,s,a,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,_=o.solveQuadratic(c,d,f,u),g=1e-5,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]=T[t]([this._segment1,this._segment2],!1,this._path.getStyle())),e.clone()}},{}),e.each(["getPoint","getTangent","getNormal","getCurvature"],function(t,e){this[t+"At"]=function(t,n){var i=this.getValues();return I.evaluate(i,n?t:I.getParameterAt(i,t,0),e)},this[t]=function(t){return I.evaluate(this.getValues(),t,e)}},{getParameterAt:function(e,n){return I.getParameterAt(this.getValues(),e,n!==t?n:0>e?1:0)},getParameterOf:function(t){var t=h.read(arguments);return I.getParameterOf(this.getValues(),t.x,t.y)},getLocationAt:function(t,e){return e||(t=this.getParameterAt(t)),new A(this,t)},getLocationOf:function(t){var t=h.read(arguments),e=this.getParameterOf(t);return null!=e?new A(this,e):null},getNearestLocation:function(t){function e(e){if(e>=0&&1>=e){var i=t.getDistance(I.evaluate(n,e,0),!0);if(r>i)return r=i,s=e,!0}}for(var t=h.read(arguments),n=this.getValues(),i=100,r=1/0,s=0,a=0;i>=a;a++)e(a/i);for(var o=1/(2*i);o>1e-5;)e(s-o)||e(s+o)||(o/=2);var u=I.evaluate(n,s,0);return new A(this,s,u,null,null,null,t.getDistance(u))},getNearestPoint:function(t){var t=h.read(arguments);return this.getNearestLocation(t).getPoint()}}),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))))}return{statics:!0,getLength:function(i,r,s){r===t&&(r=0),s===t&&(s=1);var a=o.isZero;if(0===r&&1===s&&a(i[0]-i[2])&&a(i[1]-i[3])&&a(i[6]-i[4])&&a(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 o.integrate(l,r,s,n(r,s))},getParameterAt:function(t,i,r){function s(t){var e=n(r,t);return f+=t>r?o.integrate(l,r,t,e):-o.integrate(l,t,r,e),r=t,f-i}if(0===i)return r;var a=i>0,h=a?r:0,u=a?1:r,i=Math.abs(i),l=e(t),c=o.integrate(l,h,u,n(h,u));if(i>=c)return a?u:h;var d=i/c,f=0;return o.findRoot(s,l,a?h+d:u-d,h,u,16,1e-5)}}},new function(){function t(t,e,n,i,r,s,a,o){var h=new A(n,i,r,s,a,o);(!e||e(h))&&t.push(h)}function e(r,s,a,o,h,u,l,c,d,f,_,g,v){if(!(v>20)){var m,y,w,x=s[0],b=s[1],C=s[6],S=s[7],P=1e-5,k=1e-9,M=p.getSignedDistance,z=M(x,b,C,S,s[2],s[3])||0,A=M(x,b,C,S,s[4],s[5])||0,O=z*A>0?.75:4/9,T=O*Math.min(0,z,A),L=O*Math.max(0,z,A),E=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]),D=M(x,b,C,S,r[6],r[7]);if(x===C&&k>=f-d&&v>3)m=(c+l)/2,y=m,w=0;else{var B,R,F=n(E,N,j,D),q=F[0],V=F[1];if(B=i(q,V,T,L),q.reverse(),V.reverse(),R=i(q,V,T,L),null==B||null==R)return!1;r=I.getPart(r,B,R),w=R-B,m=c*B+l*(1-B),y=c*R+l*(1-R)}if(_>.8&&w>.8)if(y-m>f-d){var Z=I.subdivide(r,.5),H=m+(y-m)/2;e(s,Z[0],o,a,h,u,d,f,m,H,w,!g,++v),e(s,Z[1],o,a,h,u,d,f,H,y,w,!g,v)}else{var Z=I.subdivide(s,.5),H=d+(f-d)/2;e(Z[0],r,o,a,h,u,d,H,m,y,w,!g,++v),e(Z[1],r,o,a,h,u,H,f,m,y,w,!g,v)}else if(Math.max(f-d,y-m)l*c)r=[[s,a,h],[s,o,h]],d=0>l;else{var f,_=0,g=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>_||g?[[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){for(var r,s,a,o,h,u=null,l=0,c=e.length-1;c>l;l++){if(a=e[l][1],h=e[l+1][1],h>a)r=null;else{if(!(i>=h))continue;s=e[l][0],o=e[l+1][0],r=s+(i-a)*(o-s)/(h-a)}break}t[0][1]<=i&&(r=t[0][0]);for(var l=0,c=t.length-1;c>l;l++){if(a=t[l][1],h=t[l+1][1],a>=n)u=r;else if(a>h)u=null;else{if(!(h>=n))continue;s=t[l][0],o=t[l+1][0],u=s+(n-a)*(o-s)/(h-a)}break}return u}function r(e,n,i,r,s,a){for(var o=I.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=I.solveCubic(x,1,0,P,0,1),b=0;k>b;b++){var M=P[b],C=I.evaluate(x,M,0).x;if(C>=0&&y>=C){var z=I.getParameterOf(w,C,0),A=o?z:M,O=o?M:z;t(s,a,i,A,I.evaluate(e,A,0),r,O,I.evaluate(n,O,0))}}}function s(e,n,i,r,s,a){var o=p.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,I.getParameterOf(e,h,u),o,r,I.getParameterOf(n,h,u),o)}}return{statics:{getIntersections:function(t,n,i,a,o,h){var u=I.isLinear(t),l=I.isLinear(n);return(u&&l?s:u||l?r:e)(t,n,i,a,o,h,0,1,0,1,0,!1,0),o}}}}),A=e.extend({_class:"CurveLocation",initialize:function me(t,e,n,i,r,s,a){this._id=me._id=(me._id||0)+1,this._curve=t,this._segment1=t._segment1,this._segment2=t._segment2,this._parameter=e,this._point=n,this._curve2=i,this._parameter2=r,this._point2=s,this._distance=a},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.getLength(0,n)_;_++)c[_]=a[_].getValues(h);for(var _=0;u>_;_++){var g=s[_],v=e?g.getValues(o):c[_];if(!e){var m=g.getSegment1(),y=g.getSegment2(),w=m._handleOut,x=y._handleIn;if(new p(m._point.subtract(w),w.multiply(2),!0).intersect(new p(y._point.subtract(x),x.multiply(2),!0),!1)){var b=I.subdivide(v);I.getIntersections(b[0],b[1],g,g,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++)I.getIntersections(v,c[C],g,a[C],r,!e&&(C===_+1||C===l-1&&0===_)&&function(t){var e=t._parameter;return e>=d&&f>=e})}for(var S=r.length-1,_=S;_>=0;_--){var P=r[_],k=P._curve.getNext(),M=P._curve2.getNext();k&&P._parameter>=f&&(P._parameter=0,P._curve=k),M&&P._parameter2>=f&&(P._parameter2=0,P._curve2=M)}if(S>0){r.sort(i);for(var _=S;_>=0;_--)r[_].equals(r[0===_?S:_-1])&&(r.splice(_,1),S--)}if(n){for(var _=S;_>=0;_--)r.push(r[_].getIntersection());r.sort(i)}return r},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,l=new h;this.clear();for(var d=0,f=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&&"z"!==p&&this.moveTo(u=l),p){case"m":case"l":var m="m"===p;m&&r&&"z"!==r&&this.closePath();for(var y=0;v>y;y+=2)this[0===y&&m?"moveTo":"lineTo"](u=n(y));s=u,m&&(l=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 c(+i[0],+i[1]),+i[2],+i[4],+i[3]);break;case"z":this.closePath()}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)}}),T=O.extend({_class:"Path",_serializeFields:{segments:[],closed:!1},initialize:function(e){this._closed=!1,this._segments=[];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 e.equals(this._segments,t._segments)},clone:function(e){var n=new T(y.NO_INSERT);return n.setSegments(this._segments),n._closed=this._closed,this._clockwise!==t&&(n._clockwise=this._clockwise),this._clone(n,e)},_changed:function ye(e){if(ye.base.call(this,e),4&e){if((this._compound?this._parent:this)._currentPath=t,this._length=this._clockwise=t,this._curves)for(var n=0,i=this._curves.length;i>n;n++)this._curves[n]._changed(5);this._monoCurves=t}else 8&e&&(this._bounds=t)},getStyle:function(){var t=this._parent;return(t instanceof L?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(M.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]},getPathData:function(t){function e(e,n,s){var a=e._point,o=n._point,h=e._handleOut,u=n._handleIn;if(h.isZero()&&u.isZero())s||r.push("L"+i.point(o,t));else{var l=o.subtract(a);r.push("c"+i.point(h,t)+" "+i.point(l.add(u),t)+" "+i.point(l,t))}}var n=this._segments,i=a.instance,r=[];if(0===n.length)return"";r.push("M"+i.point(n[0]._point));for(var s=0,o=n.length-1;o>s;s++)e(n[s],n[s+1],!1);return this._closed&&(e(n[n.length-1],n[0],!0),r.push("z")),r.join("")},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(5)}},isEmpty:function(){return 0===this._segments.length},isPolygon:function(){for(var t=0,e=this._segments.length;e>t;t++)if(!this._segments[t].isLinear())return!1;return!0},_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 I(this,null,null)); +this._adjustCurves(u,c)}return this._changed(5),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=r[this._closed&&0===t?i.length-1:t-1])&&(n._segment2=i[t]||i[0]),(n=r[e])&&(n._segment1=i[e])},_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(M.readAll(arguments)):this._add([M.read(arguments)])[0]},insert:function(t,e){return arguments.length>2&&"number"!=typeof e?this._add(M.readAll(arguments,1),t):this._add([M.read(arguments,1)],t)[0]},addSegment:function(){return this._add([M.read(arguments)])[0]},insertSegment:function(t){return this._add([M.read(arguments,1)],t)[0]},addSegments:function(t){return this._add(M.readAll(t))},insertSegments:function(t,e){return this._add(M.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(5),o},clear:"#removeSegments",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 we(t){t||this._selectSegments(!1),we.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 E(this),n=0,i=e.length/Math.ceil(e.length/t),r=e.length+(this._closed?-i:i)/2,s=[];r>=n;)s.push(new M(e.evaluate(n,0))),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 N(this,t||2.5);this.setSegments(e.fit())}},split:function(t,e){if(null!==e){if(1===arguments.length){var n=t;"number"==typeof n&&(n=this.getLocationAt(n)),t=n.index,e=n.parameter}var i=1e-5;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):t>0&&(s=this._clone((new T).insertAbove(this,!0))),s._add(a,0),this.addSegment(a[0]),s}return null}},isClockwise:function(){return this._clockwise!==t?this._clockwise:T.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)},join:function(t){if(t){var e=t._segments,n=this.getLastSegment(),i=t.getLastSegment();n._point.equals(i._point)&&t.reverse();var r,s=t.getFirstSegment();return n._point.equals(s._point)?(n.setHandleOut(s._handleOut),this._add(e.slice(1))):(r=this.getFirstSegment(),r._point.equals(s._point)&&t.reverse(),i=t.getLastSegment(),r._point.equals(i._point)?(r.setHandleIn(i._handleIn),this._add(e.slice(0,e.length-1),0)):this._add(e.slice())),t.closed&&this._add([e[0]]),t.remove(),r=this.getFirstSegment(),n=this.getLastSegment(),n._point.equals(r._point)&&(r.setHandleIn(n._handleIn),n.remove(),this.setClosed(!0)),this._changed(5),!0}return!1},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},_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];return i+s.getLength(0,t.getParameter())}return null},getLocationOf:function(t){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},getLocationAt:function(t,e){var n=this.getCurves(),i=0;if(e){var r=~~t;return n[r].getLocationAt(t-r,!0)}for(var s=0,a=n.length;a>s;s++){var o=i,h=n[s];if(i+=h.getLength(),i>t)return h.getLocationAt(t-o)}return t<=this.getLength()?new A(n[n.length-1],1):null},getPointAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getPoint()},getTangentAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getTangent()},getNormalAt:function(t,e){var n=this.getLocationAt(t,e);return n&&n.getNormal()},getNearestLocation:function(t){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._distance0&&e._index0?(o=g.getStrokeJoin(),u=g.getStrokeCap(),l=b*g.getMiterLimit(),y=m.add(new h(b,b))):o=u="round"),!e.ends||e.segments||v){if(e.segments||e.handles)for(var C=0,S=p.length;S>C;C++)if(f=r(p[C]))return f}else if(f=r(p[0],!0)||r(p[p.length-1],!0))return f;if(null!=b){if(d=this.getNearestLocation(t)){var k=d.getParameter();0===k||1===k?a(d.getSegment())||(d=null):n(d.getPoint(),y)||(d=null)}if(!d&&"miter"===o)for(var C=0,S=p.length;S>C;C++){var M=p[C];if(t.getDistance(M._point)<=l&&a(M)){d=M.getLocation();break}}}return!d&&x&&this._contains(t)||d&&!w?new P("fill",this):d?new P("stroke",this,{location:d,point:d.getPoint()}):null}},new function(){function t(t,e,n,i){function r(e){var n=a[e],i=a[e+1];(c!=n||d!=i)&&(t.beginPath(),t.moveTo(c,d),t.lineTo(n,i),t.stroke(),t.beginPath(),t.arc(n,i,s,0,2*Math.PI,!0),t.fill())}for(var s=i/2,a=Array(6),o=0,h=e.length;h>o;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){var i=d[e];if(n)i._transformCoordinates(n,_,!1),r=_[0],s=_[1];else{var f=i._point;r=f._x,s=f._y}if(g)t.moveTo(r,s),g=!1;else{if(n)h=_[2],u=_[3];else{var p=i._handleIn;h=r+p._x,u=s+p._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 p=i._handleOut;l=a+p._x,c=o+p._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(p);e._closed&&f>0&&i(0)}return{_draw:function(t,n){function i(t){return u[(t%l+l)%l]}var r=n.clip,s=this._compound=n.compound;s||t.beginPath();var a=this.getStyle(),o=a.hasFill(),h=a.hasStroke(),u=a.getDashArray(),l=!paper.support.nativeDash&&h&&u&&u.length;if(this._currentPath?t.currentPath=this._currentPath:(o||h&&!l||s||r)&&(e(t,this),this._closed&&t.closePath(),s||(this._currentPath=t.currentPath)),!r&&!s&&(o||h)&&(this._setStyles(t),o&&(t.fill(a.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),h)){if(l){t.beginPath();var c,d=new E(this),f=d.length,_=-a.getDashOffset(),g=0;for(_%=f;_>0;)_-=i(g--)+i(g--);for(;f>_;)c=_+i(g++),(_>0||c>0)&&d.drawPart(t,Math.max(_,0),Math.max(c,0)),_=c+i(g++)}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 M(h.read(arguments))])},moveBy:function(){throw Error("moveBy() is unsupported on Path items.")},lineTo:function(){this._add([new M(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 M(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,l=h.read(arguments),d=e.peek(arguments),f=e.pick(d,!0);if("boolean"==typeof f)var _=u.add(l).divide(2),n=_.add(_.subtract(u).rotate(f?-90:90));else if(e.remain(arguments)<=2)n=l,l=h.read(arguments);else{var v=c.read(arguments);if(v.isZero())return this.lineTo(l);var m=e.read(arguments),f=!!e.read(arguments),y=!!e.read(arguments),_=u.add(l).divide(2),w=u.subtract(_).rotate(-m),x=w.x,b=w.y,C=Math.abs,S=1e-11,P=C(v.width),k=C(v.height),z=P*P,I=k*k,A=x*x,O=b*b,T=Math.sqrt(A/z+O/I);if(T>1&&(P*=T,k*=T,z=P*P,I=k*k),T=(z*I-z*O-I*A)/(z*O+I*A),C(T)T)throw Error("Cannot create an arc with the given arguments");i=new h(P*b/k,-k*x/P).multiply((y===f?-1:1)*Math.sqrt(T)).rotate(m).add(_),a=(new g).translate(i).rotate(m).scale(P,k),s=a._inverseTransform(u),r=s.getDirectedAngle(a._inverseTransform(l)),!f&&r>0?r-=360:f&&0>r&&(r+=360)}if(n){var L=new p(u.add(n).divide(2),n.subtract(u).rotate(90),!0),E=new p(n.add(l).divide(2),l.subtract(n).rotate(90),!0),N=new p(u,l),j=N.getSide(n);if(i=L.intersect(E,!0),!i){if(!j)return this.lineTo(l);throw Error("Cannot create an arc with the given arguments")}s=u.subtract(i),r=s.getDirectedAngle(l.subtract(i));var D=N.getSide(i);0===D?r=j*Math.abs(r):j===D&&(r+=0>r?360:-360)}for(var B=Math.abs(r),R=B>=360?4:Math.ceil(B/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=l,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 M(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(){var t=this.getFirstSegment(),e=this.getLastSegment();t!==e&&t._point.equals(e._point)&&(t.setHandleIn(e._handleIn),e.remove()),this.setClosed(!0)}}},{_getBounds:function(t,e){return T[t](this._segments,this._closed,this.getStyle(),e)},statics:{isClockwise:function(t){for(var e=0,n=0,i=t.length;i>n;n++)for(var r=I.getValues(t[n],t[i>n+1?n+1:0]),s=2;8>s;s+=2)e+=(r[s-2]-r[s])*(r[s+1]+r[s-1]);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++)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 f;for(var o=Array(6),h=a._transformCoordinates(i,Array(6),!1),u=h.slice(0,2),l=u.slice(),c=Array(2),d=1,_=t.length;_>d;d++)s(t[d]);return e&&s(a),new f(u[0],u[1],l[0]-u[0],l[1]-u[1])},getStrokeBounds:function(t,e,n,i){function r(t){d=d.include(i?i._transformPoint(t,t):t)}function s(t){d=d.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.isColinear(i)?s(t):T._addBevelJoin(t,e,u,p,r)}function o(t,e){"round"===e?s(t):T._addSquareCap(t,e,u,r)}if(!n.hasStroke())return T.getBounds(t,e,n,i);for(var h=t.length-(e?0:1),u=n.getStrokeWidth()/2,l=T._getPenPadding(u,i),d=T.getBounds(t,e,n,i,l),_=n.getStrokeJoin(),g=n.getStrokeCap(),p=u*n.getMiterLimit(),v=new f(new c(l).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)),d},_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 p(u.add(l),new h(-l.y,l.x),!0).intersect(new p(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().normalize(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,d=t.length;d>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 f(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)))),T.getHandleBounds(t,e,n,i,T._getPenPadding(r,i),T._getPenPadding(s,i))}}});T.inject({statics:new function(){function t(t,n,i){var r=e.getNamed(i),s=new T(r&&r.insert===!1&&y.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 M(o._point.multiply(n).add(e),o._handleIn.multiply(n),o._handleOut.multiply(n))}return t(s,!0,i)}var i=.5522847498307936,r=[new M([-1,0],[0,i],[0,-i]),new M([0,-1],[-i,0],[i,0]),new M([1,0],[0,-i],[0,i]),new M([0,1],[i,0],[-i,0])];return{Line:function(){return t([new M(h.readNamed(arguments,"from")),new M(h.readNamed(arguments,"to"))],!1,arguments)},Circle:function(){var t=h.readNamed(arguments,"center"),i=e.readNamed(arguments,"radius");return n(t,new c(i),arguments)},Rectangle:function(){var e,n=f.readNamed(arguments,"rectangle"),r=c.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 M(s),new M(a),new M(o),new M(h)];else{r=c.min(r,n.getSize(!0).divide(2));var u=r.width,l=r.height,d=u*i,_=l*i;e=[new M(s.add(u,0),null,[-d,0]),new M(s.subtract(0,l),[0,_]),new M(a.add(0,l),null,[0,-_]),new M(a.add(u,0),[-d,0],null),new M(o.subtract(u,0),null,[d,0]),new M(o.add(0,l),[0,-_],null),new M(h.subtract(0,l),null,[0,_]),new M(h.subtract(u,0),[d,0])]}return t(e,!0,arguments)},RoundRectangle:"#Rectangle",Ellipse:function(){var t=b._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 T(r&&r.insert===!1&&y.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 M(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 M(n.add(o.rotate(a*l).multiply(l%2?s:r)));return t(u,!0,arguments)}}}});var L=O.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))},_changed:function xe(e){xe.base.call(this,e),6&e&&(this._currentPath=t)},insertChildren:function be(e,n,i){n=be.base.call(this,e,n,i,"path");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()},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},getPathData:function(t){for(var e=this._children,n=[],i=0,r=e.length;r>i;i++)n.push(e[i].getPathData(t));return n.join(" ")},_getChildHitTestOptions:function(t){return"path"===t.type?t:new e(t,{fill:!1})},_draw:function(t,e){var n=this._children;if(0!==n.length){if(this._currentPath)t.currentPath=this._currentPath;else{t.beginPath(),e=e.extend({compound:!0});for(var i=0,r=n.length;r>i;i++)n[i].draw(t,e);this._currentPath=t.currentPath}if(!e.clip){this._setStyles(t);var s=this._style;s.hasFill()&&(t.fill(s.getWindingRule()),t.shadowColor="rgba(0,0,0,0)"),s.hasStroke()&&t.stroke()}}}},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 T;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(){t(this,!0).closePath()}};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});O.inject(new function(){function t(t){if(t=t.clone(!1).reduce().transform(null,!0),t instanceof L){var e,n=t.removeChildren(),i=n.length,r=Array(i),s=Array(i);n.sort(function(t,e){return e.getBounds().getArea()-t.getBounds().getArea()}),t.addChildren(n),e=n[0].isClockwise();for(var a=0;i>a;a++)r[a]=n[a].getBounds(),s[a]=0;for(var a=0;i>a;a++){for(var o=1;i>o;o++)a!==o&&r[a].intersects(r[o])&&s[o]++;a>0&&0===s[a]%2&&n[a].setClockwise(e)}}return t}function e(e,s,a,o){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=t(e);_path2=s&&e!==s&&t(s),u.isClockwise()||u.reverse(),!_path2||o^_path2.isClockwise()||_path2.reverse(),n(u.getIntersections(_path2,!0));var l=[],c=[],d=[],f=[],_=[];h(u._children||[u]),_path2&&h(_path2._children||[_path2]),f.sort(function(t,e){var n=t._intersection,i=e._intersection;return!n&&!i||n&&i?0:n?-1:1});for(var g=0,p=f.length;p>g;g++){var v=f[g];if(null==v._winding){l.length=c.length=d.length=0;var m=0,y=v;do l.push(v),d.push(m+=v.getCurve().getLength()),v=v.getNext();while(v&&!v._intersection&&v!==y);for(var w=0;3>w;w++){var x=m*Math.random(),b=d.length;k=0;do if(d[k]>=x){k>0&&(x-=d[k-1]);break}while(++k=0;w--)l[w]._winding=z}}var I=new L;return I.addChildren(r(f,a),!0),u.remove(),_path2&&_path2.remove(),I.reduce()}function n(t){function e(){for(var t=0,e=n.length;e>t;t++){var i=n[t];i._handleOut.set(0,0),i._handleIn.set(0,0)}}for(var n,i,r,s=1e-5,a=t.length-1;a>=0;a--){var o=t[a],h=o._parameter;r&&r._curve===o._curve&&r._parameter>0?h/=r._parameter:(n&&e(),i=o._curve,n=i.isLinear()&&[]);var u,l;(u=i.divide(h,!0,!0))?(l=u._segment1,i=u.getPrevious()):l=s>h?i._segment1:h>1-s?i._segment2:i.getPartLength(0,h)m;m++){var w=e[m].values;if(I.solveCubic(w,0,a,c,0,1)>0)for(var x=c.length-1;x>=0;x--){var b=I.evaluate(w,c[x],0).y;p>b&&b>_?_=b:b>v&&g>b&&(g=b)}}_=(_+o)/2,g=(g+o)/2,_>-1/0&&(u=i(new h(a,_),e)),1/0>g&&(l=i(new h(a,g),e))}else for(var C=a-s,S=a+s,m=0,y=e.length;y>m;m++){var P=e[m],w=P.values,k=P.winding,M=P.next;if(k&&(1===k&&o>=w[1]&&o<=w[7]||o>=w[7]&&o<=w[1])&&1===I.solveCubic(w,1,o,c,0,M.winding||M.values[1]!==o?f:1)){var z=c[0],A=I.evaluate(w,z,0).x,O=I.evaluate(w,z,1).y;d(O)z&&O*I.evaluate(P.previous.values,z,1).y<0?r&&A>=C&&S>=A&&(++u,++l):C>=A?u+=k:A>=S&&(l+=k)}}return Math.max(d(u),d(l))}function r(t,e,n){e=e||function(){return!0};for(var i,r,s=[],a=.001,o=.999,h=0,u=t.length;u>h;h++)if(i=r=t[h],!i._visited&&e(i._winding)){var l=new T(y.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){var m=i.getCurve();_>0&&(m=m.getPrevious());var w=m.getTangentAt(1>_?a:o,!0),x=g.getCurve(),b=x.getPrevious(),C=b.getTangentAt(o,!0),S=x.getTangentAt(a,!0),P=w.cross(C),k=w.cross(S);if(n)i._visited=g._visited,i=g,_=1;else if(0!==P*k){var z=k>P?b:x,I=e(z._segment1._winding)?z:k>P?x:b,A=I._segment1;_=I===b?-1:1,A._visited&&i._path!==A._path||!e(A._winding)?_=1:(i._visited=g._visited,i=g,A._visited&&(_=1))}else _=1;v=_>0?i._handleOut:i._handleIn}l.add(new M(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.isPolygon()?2:0:1)&&s.push(l)}return s}return{_getWinding:function(t,e,n){return i(t,this._getMonoCurves(),e,n)},unite:function(t){return e(this,t,function(t){return 1===t||0===t},!1)},intersect:function(t){return e(this,t,function(t){return 2===t},!1)},subtract:function(t){return e(this,t,function(t){return 1===t},!0)},exclude:function(t){return new w([this.subtract(t),t.subtract(this)])},divide:function(t){return new w([this.subtract(t),this.intersect(t)])}}}),T.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.isLinear(e))t(e);else{var a=3*(i-r)-n+s,h=2*(n+r)-4*i,u=i-n,l=1e-5,c=[],d=o.solveQuadratic(a,h,u,c,l,1-l);if(0===d)t(e);else{c.sort();var f=c[0],_=I.subdivide(e,f);t(_[0]),d>1&&(f=(c[1]-f)/(1-f),_=I.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,a=0,h=r.length;h>a;a++)e(r[a].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,_])}var g=i[0],p=i[i.length-1];g.previous=p,p.next=g}return i}}),L.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}});var E=e.extend({initialize:function(t){function e(t,e){var n=I.getValues(t,e);s.curves.push(n),s._computeParts(n,t._index,0,1)}this.curves=[],this.parts=[],this.length=0,this.index=0;for(var n,i=t._segments,r=i[0],s=this,a=1,o=i.length;o>a;a++)n=i[a],e(r,n),r=n;t._closed&&e(n,i[0])},_computeParts:function(t,e,n,i){if(i-n>1/32&&!I.isFlatEnough(t,.25)){var r=I.subdivide(t),s=(n+i)/2;this._computeParts(r[0],e,n,s),this._computeParts(r[1],e,s,i)}else{var a=t[6]-t[0],o=t[7]-t[1],h=Math.sqrt(a*a+o*o);h>1e-5&&(this.length+=h,this.parts.push({offset:this.length,value:i,index:e}))}},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}},evaluate:function(t,e){var n=this.getParameterAt(t);return I.evaluate(this.curves[n.index],n.value,e)},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))}}}),N=e.extend({initialize:function(t,e){this.points=[];for(var n,i=t._segments,r=0,s=i.length;s>r;r++){var a=i[r].point.clone();n&&n.equals(a)||(this.points.push(a),n=a)}this.error=e},fit:function(){var t=this.points,e=t.length;return this.segments=e>0?[new M(t[0])]:[],e>1&&this.fitCubic(0,e-1,t[1].subtract(t[0]).normalize(),t[e-2].subtract(t[e-1]).normalize()),this.segments},fitCubic:function(e,n,i,r){if(1==n-e){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;4>=c;c++){var d=this.generateBezier(e,n,u,i,r),f=this.findMaxError(e,n,d,u);if(f.error=l)break;this.reparameterize(e,n,u,d),l=f.error}var _=this.points[h-1].subtract(this.points[h]),g=this.points[h].subtract(this.points[h+1]),p=_.add(g).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 M(t[3],t[2].subtract(t[3])))},generateBezier:function(t,e,n,i,r){for(var s=1e-11,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],z=h[1][0]+h[1][1];b=C=Math.abs(M)>s?u[0]/M:Math.abs(z)>s?u[1]/z:0}var I=o.getDistance(a);return s*=I,(s>b||s>C)&&(b=C=I/3),[a,a.add(i.normalize(b)),o.add(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])},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-5?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}}}),j=y.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 Ce(t){return t.setContent(this._content),Ce.base.call(this,t)},getContent:function(){return this._content},setContent:function(t){this._content=""+t,this._lines=this._content.split(/\r\n|\n|\r/gm),this._changed(69)},isEmpty:function(){return!this._content},getCharacterStyle:"#getStyle",setCharacterStyle:"#setStyle",getParagraphStyle:"#getStyle",setParagraphStyle:"#setStyle"}),D=j.extend({_class:"PointText",initialize:function(){j.apply(this,arguments) +},clone:function(t){return this._clone(new D(y.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)}}}},new function(){var t=null;return{_getBounds:function(e,n){t||(t=te.getContext(1,1));var i=this._style,r=this._lines,s=r.length,a=i.getJustification(),o=i.getLeading(),h=0;t.font=i.getFontStyle();for(var u=0,l=0;s>l;l++)u=Math.max(u,t.measureText(r[l]).width);"left"!==a&&(h-=u/("center"===a?2:1));var c=new f(h,s?-.75*o:0,u,s*o);return n?n._transformBounds(c,c):c}}}),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 a=i[r+1];e[r]=parseInt(1==a.length?a+a:a,16)/255}}else if(i=t.match(/^rgba?\((.*)\)$/)){e=i[1].split(",");for(var r=0,o=e.length;o>r;r++){var a=+e[r];e[r]=3>r?a/255:a}}else{var h=s[t];if(!h){n||(n=te.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=s[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={},s={},o=[[0,3,1],[2,0,1],[1,0,3],[1,2,0],[3,1,0],[0,1,2]],u={"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=o[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+6*(r-s)*(2/3-h):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=R.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),t=u.call(this,t),null!=t&&(this._components[s]=t,this._changed())}},this)},{_class:"Color",_readIndex:!0,initialize:function l(e){var n,s,a,o,h=Array.prototype.slice,u=arguments,c=0;Array.isArray(e)&&(u=e,e=u[0]);var d=null!=e&&typeof e;if("string"===d&&e in i&&(n=e,e=u[1],Array.isArray(e)?(s=e,a=u[2]):(this.__read&&(c=1),u=h.call(u,1),d=typeof e)),!s){if(o="number"===d?u:"object"===d&&null!=e.length?e:null){n||(n=o.length>=3?"rgb":"gray");var f=i[n].length;a=o[f],this.__read&&(c+=o===arguments?f+(null!=a?1:0):1),o.length>f&&(o=h.call(o,0,f))}else if("string"===d)n="rgb",s=t(e),4===s.length&&(a=s[3],s.length--);else if("object"===d)if(e.constructor===l){if(n=e._type,s=e._components.slice(),a=e._alpha,"gradient"===n)for(var _=1,g=s.length;g>_;_++){var p=s[_];p&&(s[_]=p.clone())}}else if(e.constructor===R)n="gradient",o=u;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 v=i[n];y=r[n],this._components=s=[];for(var _=0,g=v.length;g>_;_++){var m=e[v[_]];null==m&&0===_&&"gradient"===n&&"stops"in e&&(m={stops:e.stops,radial:e.radial}),m=y[_].call(this,m),null!=m&&(s[_]=m)}a=e.alpha}this.__read&&n&&(c=1)}if(this._type=n||"rgb","gradient"===n&&(this._id=l._id=(l._id||0)+1),!s){this._components=s=[];for(var y=r[this._type],_=0,g=y.length;g>_;_++){var m=y[_].call(this,o&&o[_]);null!=m&&(s[_]=m)}}this._components=s,this._properties=i[this._type],this._alpha=a,this.__read&&(this.__read=c)},_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(17)},_convert:function(t){var e;return this._type===t?this._components.slice():(e=u[this._type+"-"+t])?e.apply(this,this._components):u["rgb-"+t].apply(this,u[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)?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=a.instance,r=0,s=t.length;s>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 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)}},{})});e.each(B._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 B(n,i):new B(t)};if(3==n.length){var r=n.toUpperCase();B[r]=this[r+"Color"]=i}},e.exports);var R=e.extend({_class:"Gradient",initialize:function Se(t,e){this._id=Se._id=(Se._id||0)+1,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 R(t)},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=F.readAll(e,0,!1,!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}}),F=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 F(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(17)},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}}),q=e.extend(new function(){var n={fillColor:t,strokeColor:t,strokeWidth:1,strokeCap:"butt",strokeJoin:"miter",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:25,strokeCap:25,strokeJoin:25,miterLimit:25,fontFamily:5,fontWeight:5,fontSize:5,font:5,leading:5,justification:5},r={},s={_defaults:n,_textDefaults:new e(n,{fillColor:new B})};return e.each(n,function(n,a){var o=/Color$/.test(a),h=e.capitalize(a),u=i[a],l="set"+h,c="get"+h;s[l]=function(e){var n=this._item&&this._item._children;if(n&&n.length>0&&"compound-path"!==this._item._type)for(var i=0,r=n.length;r>i;i++)n[i]._style[l](e);else{var s=this._values[a];s!=e&&(o&&(s&&(s._owner=t),e&&e.constructor===B&&(e._owner&&(e=e.clone()),e._owner=this._item)),this._values[a]=e,this._item&&this._item._changed(u||17))}},s[c]=function(n){var i,r=this._item&&this._item._children;if(!r||0===r.length||n||this._item instanceof L){var i=this._values[a];return i===t?(i=this._defaults[a],i&&i.clone&&(i=i.clone()),this._values[a]=i):!o||i&&i.constructor===B||(this._values[a]=i=B.read([i],0,{readNull:!0,clone:!0}),i&&(i._owner=this._item)),i}for(var s=0,h=r.length;h>s;s++){var u=r[s]._style[c]();if(0===s)i=u;else if(!e.equals(i,u))return t}return i},r[c]=function(){return this._style[c]()},r[l]=function(t){this._style[l](t)}}),y.inject(r),s},{_class:"Style",initialize:function(t,e){this._values={},this._item=e,e instanceof j&&(this._defaults=this._textDefaults),t&&this.set(t)},set:function(t){var e=t instanceof q,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},getFontStyle:function(){var t=this.getFontSize();return this.getFontWeight()+" "+t+(/[a-z]/i.test(t+"")?" ":"px ")+this.getFontFamily()},getFont:"#getFontFamily",setFont:"#setFontFamily",getLeading:function Pe(){var t=Pe.base.call(this);return null!=t?t:1.2*this.getFontSize()}}),V=new function(){function n(t,i){for(var r=[],s=0,a=t&&t.length;a>s;){var o=t[s++];if("string"==typeof o)o=document.createElement(o);else if(!o||!o.nodeType)continue;e.isPlainObject(t[s])&&V.set(o,t[s++]),Array.isArray(t[s])&&n(t[s++],o),i&&i.appendChild(o),r.push(o)}return r}function i(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}}}var r=/^(checked|value|selected|disabled)$/i,s={text:"textContent",html:"innerHTML"},a={lineHeight:1,zoom:1,zIndex:1,opacity:1};return{create:function(t,e){var i=Array.isArray(t),r=n(i?t:arguments,i?e:null);return 1==r.length?r[0]:r},find:function(t,e){return(e||document).querySelector(t)},findAll:function(t,e){return(e||document).querySelectorAll(t)},get:function(t,e){return t?r.test(e)?"value"===e||"string"!=typeof t[e]?t[e]:!0:e in s?t[s[e]]:t.getAttribute(e):null},set:function(e,n,i){if("string"!=typeof n)for(var a in n)n.hasOwnProperty(a)&&this.set(e,a,n[a]);else{if(!e||i===t)return e;r.test(n)?e[n]=i:n in s?e[s[n]]=i:"style"===n?this.setStyle(e,i):"events"===n?Z.add(e,i):e.setAttribute(n,i)}return e},getStyles:function(t){var e=t&&9!==t.nodeType?t.ownerDocument:t,n=e&&e.defaultView;return n&&n.getComputedStyle(t,"")},getStyle:function(t,e){return t&&t.style[e]||this.getStyles(t)[e]||null},setStyle:function(t,e,n){if("string"!=typeof e)for(var i in e)e.hasOwnProperty(i)&&this.setStyle(t,i,e[i]);else!/^-?[\d\.]+$/.test(n)||e in a||(n+="px"),t.style[e]=n;return t},hasClass:function(t,e){return RegExp("\\s*"+e+"\\s*").test(t.className)},addClass:function(t,e){t.className=(t.className+" "+e).trim()},removeClass:function(t,e){t.className=t.className.replace(RegExp("\\s*"+e+"\\s*")," ").trim()},remove:function(t){t.parentNode&&t.parentNode.removeChild(t)},removeChildren:function(t){for(;t.firstChild;)t.removeChild(t.firstChild)},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 f(o,h,n.width,n.height)},getViewportBounds:function(t){var e=t.ownerDocument,n=e.defaultView,i=e.documentElement;return new f(0,0,n.innerWidth||i.clientWidth,n.innerHeight||i.clientHeight)},getOffset:function(t,e){return this.getBounds(t,e).getPoint()},getSize:function(t){return this.getBounds(t,!0).getSize()},isInvisible:function(t){return this.getSize(t).equals(new c(0,0))},isInView:function(t){return!this.isInvisible(t)&&this.getViewportBounds(t).intersects(this.getBounds(t,!0))},getPrefixed:function(t,e){return i(t,e)},setPrefixed:function(t,e,n){if("object"==typeof e)for(var r in e)i(t,r,!0,e[r]);else i(t,e,!0,n)}}},Z={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 Z.getPoint(t).subtract(V.getOffset(e||Z.getTarget(t)))},stop:function(t){t.stopPropagation(),t.preventDefault()}};Z.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"==r.getAttribute(u,"keepalive")||a)&&V.isInView(u))&&(s.splice(e,1),h())}n&&(s.length?n(t):i=!1)}var e,n=V.getPrefixed(window,"requestAnimationFrame"),i=!1,s=[],a=!0;return Z.add(window,{focus:function(){a=!0},blur:function(){a=!1}}),function(r,a){s.push([r,a]),n?i||(n(t),i=!0):e||(e=setInterval(t,1e3/60))}};var H=e.extend(n,{_class:"View",initialize:function ke(t){this._scope=paper,this._project=paper.project,this._element=t;var e;this._pixelRatio||(this._pixelRatio=window.devicePixelRatio||1),this._id=t.getAttribute("id"),null==this._id&&t.setAttribute("id",this._id="view-"+ke._id++),Z.add(t,this._viewEvents);var n="none";if(V.setPrefixed(t.style,{userSelect:n,touchAction:n,touchCallout:n,contentZooming:n,userDrag:n,tapHighlightColor:"rgba(0,0,0,0)"}),r.hasAttribute(t,"resize")){var i=V.getOffset(t,!0),s=this;e=V.getViewportBounds(t).getSize().subtract(i),this._windowEvents={resize:function(){V.isInvisible(t)||(i=V.getOffset(t,!0)),s.setViewSize(V.getViewportBounds(t).getSize().subtract(i))}},Z.add(window,this._windowEvents)}else e=V.getSize(t),(e.isNaN()||e.isZero())&&(e=new c(parseInt(t.getAttribute("width"),10),parseInt(t.getAttribute("height"),10)));if(this._setViewSize(e),r.hasAttribute(t,"stats")&&"undefined"!=typeof Stats){this._stats=new Stats;var a=this._stats.domElement,o=a.style,i=V.getOffset(t);o.position="absolute",o.left=i.x+"px",o.top=i.y+"px",document.body.appendChild(a)}ke._views.push(this),ke._viewsById[this._id]=this,this._viewSize=e,(this._matrix=new g)._owner=this,this._zoom=1,ke._focused||(ke._focused=this),this._frameItems={},this._frameItemCount=0},remove:function(){return this._project?(H._focused===this&&(H._focused=null),H._views.splice(H._views.indexOf(this),1),delete H._viewsById[this._id],this._project.view==this&&(this._project.view=null),Z.remove(this._element,this._viewEvents),Z.remove(window,this._windowEvents),this._element=this._project=null,this.detach("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;Z.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.fire("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.attach("frame",this._handleFrameItems)):(delete n[t._id],0===--this._frameItemCount&&this.detach("frame",this._handleFrameItems))},_handleFrameItems:function(t){for(var n in this._frameItems){var i=this._frameItems[n];i.item.fire("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 d(t.width,t.height,this,"setViewSize")},setViewSize:function(){var t=c.read(arguments),e=t.subtract(this._viewSize);e.isZero()||(this._viewSize.set(t.width,t.height),this._setViewSize(t),this._bounds=null,this.fire("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 f(new h,this._viewSize))),this._bounds},getSize:function(){return this.getBounds().getSize()},getCenter:function(){return this.getBounds().getCenter()},setCenter:function(t){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 V.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){return"string"==typeof t&&(t=document.getElementById(t)),new U(t)}}},new function(){function t(t){var e=Z.getTarget(t);return e.getAttribute&&H._viewsById[e.getAttribute("id")]}function e(t,e){return t.viewToProject(Z.getOffset(e,t._element))}function n(){if(!H._focused||!H._focused.isVisible())for(var t=0,e=H._views.length;e>t;t++){var n=H._views[t];if(n&&n.isVisible()){H._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=H._focused,r=Z.getRelatedTarget(t);!n||r&&"HTML"!==r.nodeName||i(n,e(n,t),t)},scroll:n};return d[o]=function(n){var i=H._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=H._focused;if(!l){var u=t(o);u?(h!==u&&i(h,e(h,o),o),s=h,h=H._focused=a=u):a&&a===h&&(h=H._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=H._focused;if(n&&l){var i=e(n,t);curPoint=null,l=!1,n._handleEvent("mouseup",i,t),r&&r._handleEvent("mouseup",i,t),n.update()}},Z.add(document,f),Z.add(window,{load:n}),{_viewEvents:d,_handleEvent:function(){},statics:{updateFocus:n}}}),U=H.extend({_class:"CanvasView",initialize:function(t){if(!(t instanceof HTMLCanvasElement)){var e=c.read(arguments);if(e.isZero())throw Error("Cannot create CanvasView with the provided argument: "+t);t=te.getCanvas(e)}if(this._context=t.getContext("2d"),this._eventCounters={},this._pixelRatio=1,"off"!==r.getAttribute(t,"hidpi")){var n=window.devicePixelRatio||1,i=V.getPrefixed(this._context,"backingStorePixelRatio")||1;this._pixelRatio=n/i}H.call(this,t)},_setViewSize:function(t){var e=t.width,n=t.height,i=this._pixelRatio,r=this._element,s=r.style;r.width=e*i,r.height=n*i,1!==i&&(s.width=e+"px",s.height=n+"px",this._context.scale(i,i))},update:function(){if(!this._project._needsUpdate)return!1;var t=this._context,e=this._viewSize;return t.clearRect(0,0,e.width+1,e.height+1),this._project.draw(t,this._matrix,this._pixelRatio),this._project._needsUpdate=!1,!0}},new function(){function e(e,n,i,r,s,a){function o(e){return e.responds(n)&&(h||(h=new $(n,i,r,s,a?r.subtract(a):null)),e.fire(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:this._scope.settings.hitTolerance,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}}}}),W=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 X.modifiers}}),G=W.extend({_class:"KeyEvent",initialize:function(t,e,n,i){W.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()+" }"}}),X=new function(){function t(t,n,r,h){var u,l=r?String.fromCharCode(r):"",c=i[n],d=c||l.toLowerCase(),f=t?"keydown":"keyup",_=H._focused,g=_&&_.isVisible()&&_._scope,p=g&&g.tool;o[d]=t,c&&(u=e.camelize(c))in s&&(s[u]=t),t?a[n]=r:delete a[n],p&&p.responds(f)&&(paper=g,p.fire(f,new G(t,d,l,h)),_&&_.update())}var n,i={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"},r={9:!0,13:!0,32:!0},s=new e({shift:!1,control:!1,option:!1,command:!1,capsLock:!1,space:!1}),a={},o={};return Z.add(document,{keydown:function(e){var s=e.which||e.keyCode;s in i?t(!0,s,s in r?s:0,e):n=s},keypress:function(e){null!=n&&(t(!0,n,e.which||e.keyCode,e),n=null)},keyup:function(e){var n=e.which||e.keyCode;n in a&&t(!1,n,a[n],e)}}),Z.add(window,{blur:function(e){for(var n in a)t(!1,n,a[n],e)}}),{modifiers:s,isDown:function(t){return!!o[t]}}},$=W.extend({_class:"MouseEvent",initialize:function(t,e,n,i,r){W.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()+" }"}});e.extend(n,{_class:"Palette",_events:["onChange"],initialize:function(n,i,r){var s=V.find(".palettejs-panel")||V.find("body").appendChild(V.create("div",{"class":"palettejs-panel"}));this._element=s.appendChild(V.create("table",{"class":"palettejs-pane"})),this._title=n,r||(r={});for(var a in this.components=i){var o=i[a];o instanceof J||(null==o.value&&(o.value=r[a]),o.name=a,o=i[a]=new J(o)),this._element.appendChild(o._element),o._palette=this,r[a]===t&&(r[a]=o.value)}this.values=e.each(r,function(t,n){var s=i[n];s&&e.define(r,n,{enumerable:!0,configurable:!0,get:function(){return s._value},set:function(t){s.setValue(t)}})}),window.paper&&paper.palettes.push(this)},reset:function(){for(var t in this.components)this.components[t].reset()},remove:function(){V.remove(this._element)}});var J=e.extend(n,{_class:"Component",_events:["onChange","onClick"],_types:{"boolean":{type:"checkbox",value:"checked"},string:{type:"text"},number:{type:"number",number:!0},button:{type:"button"},text:{tag:"div",value:"text"},slider:{type:"range",number:!0},list:{tag:"select",setOptions:function(){V.removeChildren(this._input),V.create(e.each(this._options,function(t){this.push("option",{value:t,text:t})},[]),this._input)}},color:{type:"color",getValue:function(t){return new B(t)},setValue:function(t){return new B(t).toCSS("color"===V.get(this._input,"type"))}}},initialize:function Me(t){this._id=Me._id=(Me._id||0)+1,this._type=t.type in this._types?t.type:"options"in t?"list":"onClick"in t?"button":typeof t.value,this._meta=this._types[this._type]||{type:this._type};var n=this,i="component-"+this._id;this._dontFire=!0,this._input=V.create(this._meta.tag||"input",{id:i,type:this._meta.type,events:{change:function(){n.setValue(V.get(this,n._meta.value||"value"))},click:function(){n.fire("click")}}}),this.attach("change",function(t){this._dontFire||this._palette.fire("change",this,this.name,t)}),this._element=V.create("tr",["td",[this._label=V.create("label",{"for":i})],"td",[this._input]]),e.each(t,function(t,e){this[e]=t},this),this._defaultValue=this._value,this._dontFire=!1},getType:function(){return this._type},getLabel:function(){return this.__label},setLabel:function(t){this.__label=t,V.set(this._label,"text",t+":")},getOptions:function(){return this._options},setOptions:function(t){this._options=t;var e=this._meta.setOptions;e&&e.call(this)},getValue:function(){var t=this._value,e=this._meta.getValue;return e?e.call(this,t):t},setValue:function(t){var e=this._meta.value||"value",n=this._meta.setValue;n&&(t=n.call(this,t)),V.set(this._input,e,t),t=V.get(this._input,e),this._meta.number&&(t=parseFloat(t,10)),this._value!==t&&(this._value=t,this._dontFire||this.fire("change",this.getValue()))},getRange:function(){return[parseFloat(V.get(this._input,"min")),parseFloat(V.get(this._input,"max"))]},setRange:function(t,e){var n=Array.isArray(t)?t:[t,e];V.set(this._input,{min:n[0],max:n[1]})},getMin:function(){return this.getRange()[0]},setMin:function(t){this.setRange(t,this.getMax())},getMax:function(){return this.getRange()[1]},setMax:function(t){this.setRange(this.getMin(),t)},getStep:function(){return parseFloat(V.get(this._input,"step"))},setStep:function(t){V.set(this._input,"step",t)},reset:function(){this.setValue(this._defaultValue)}}),Y=W.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|compound-path)$/.test(n._type);)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()+" }"}}),K=s.extend({_class:"Tool",_list:"tools",_reference:"tool",_events:["onActivate","onDeactivate","onEditOptions","onMouseDown","onMouseUp","onMouseDrag","onMouseMove","onKeyDown","onKeyUp"],initialize:function(t){s.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!=this._minDistance&&null!=this._maxDistance&&this._minDistance>this._maxDistance&&(this._maxDistance=this._minDistance) +},getMaxDistance:function(){return this._maxDistance},setMaxDistance:function(t){this._maxDistance=t,null!=this._minDistance&&null!=this._maxDistance&&this._maxDistanceu)return!1;var l=null!=i?i:0;if(0!=l)if(u>l)e=this._point.add(h.normalize(l));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.fire(t,new Y(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}}),Q={request:function(t,e,n){var i=new(window.ActiveXObject||XMLHttpRequest)("Microsoft.XMLHTTP");return i.open(t.toUpperCase(),e,!0),"overrideMimeType"in i&&i.overrideMimeType("text/plain"),i.onreadystatechange=function(){if(4===i.readyState){var t=i.status;if(0!==t&&200!==t)throw Error("Could not load "+e+" (Error "+t+")");n.call(i,i.responseText)}},i.send(null)}},te={canvases:[],getCanvas:function(t,e,n){var i,r=!0;"object"==typeof t&&(n=e,e=t.height,t=t.width),n?1!==n&&(t*=n,e*=n):n=1,i=this.canvases.length?this.canvases.pop():document.createElement("canvas");var s=i.getContext("2d");return i.width===t&&i.height===e?r&&s.clearRect(0,0,t+1,e+1):(i.width=t,i.height=e),s.save(),1!==n&&s.scale(n,n),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)}},ee=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=te.getContext(1,1);e.each(y,function(t,e){x.save();var n="darken"===e,i=!1;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)),w[e]=i,x.restore()}),te.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)}}},ne=e.each({fillColor:["fill","color"],strokeColor:["stroke","color"],strokeWidth:["stroke-width","number"],strokeCap:["stroke-linecap","string"],strokeJoin:["stroke-linejoin","string"],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},{}),get:"get"+i,set:"set"+i}},{}),ie={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=ie[n];"number"==typeof i&&(i=S.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 r(t,e,n){var i=t._matrix,r=i.getTranslation(),s={};if(e){i=i.shiftless();var a=i._inverseTransform(r);s[n?"cx":"x"]=a.x,s[n?"cy":"y"]=a.y,r=null}if(!i.isIdentity()){var h=i.decompose();if(h&&!h.shearing){var u=[],l=h.rotation,c=h.scaling;r&&!r.isZero()&&u.push("translate("+S.point(r)+")"),l&&u.push("rotate("+S.number(l)+")"),o.isZero(c.x-1)&&o.isZero(c.y-1)||u.push("scale("+S.point(c)+")"),s.transform=u.join(" ")}else s.transform="matrix("+i.getValues().join(",")+")"}return s}function s(e,i){for(var s=r(e),a=e._children,o=n("g",s),h=0,u=a.length;u>h;h++){var l=a[h],c=b(l,i);if(c)if(l.isClipMask()){var d=n("clipPath");d.appendChild(c),w(l,d,"clip"),t(o,{"clip-path":"url(#"+d.id+")"})}else o.appendChild(c)}return o}function h(t){var e=r(t,!0),i=t.getSize();return e.x-=i.width/2,e.y-=i.height/2,e.width=i.width,e.height=i.height,e.href=t.toDataURL(),n("image",e)}function u(t,e){if(e.matchShapes){var r=t.toShape(!1);if(r)return c(r,e)}var s,a,o=t._segments;if(0===o.length)return null;if(t.isPolygon())if(o.length>=3){s=t._closed?"polygon":"polyline";var h=[];for(i=0,l=o.length;l>i;i++)h.push(S.point(o[i]._point));a={points:h.join(" ")}}else{s="line";var u=o[0]._point,d=o[o.length-1]._point;a={x1:u.x,y1:u.y,x2:d.x,y2:d.y}}else{s="path";var f=t.getPathData();a=f&&{d:f}}return n(s,a)}function c(t){var e=t._shape,i=t._radius,s=r(t,!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,i.isZero()&&(i=null)}return i&&("circle"===e?s.r=i:(s.rx=i.width,s.ry=i.height)),n(e,s)}function d(t){var e=r(t,!0),i=t.getPathData();return i&&(e.d=i),n("path",e)}function f(t,e){var i=r(t,!0),s=t.getSymbol(),a=m(s,"symbol"),o=s.getDefinition(),h=o.getBounds();return a||(a=n("symbol",{viewBox:S.rectangle(h)}),a.appendChild(b(o,e)),w(s,a,"symbol")),i.href="#"+a.id,i.x+=h.x,i.y+=h.y,i.width=S.number(h.width),i.height=S.number(h.height),n("use",i)}function _(t){var e=m(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))}w(t,e,"color")}return"url(#"+e.id+")"}function g(t){var e=n("text",r(t,!0));return e.textContent=t._content,e}function p(n,i){var r={},s=n.getParent();return null!=n._name&&(r.id=n._name),e.each(ne,function(t){var i=t.get,a=t.type,o=n[i]();if(!s||!e.equals(s[i](),o)){if("color"===a&&null!=o){var h=o.getAlpha();1>h&&(r[t.attribute+"-opacity"]=h)}r[t.attribute]=null==o?"none":"number"===a?S.number(o):"color"===a?o.gradient?_(o,n):o.toCSS(!0):"array"===a?o.join(","):"lookup"===a?t.toSVG[o]:o}}),1===r.opacity&&delete r.opacity,null==n._visibility||n._visibility||(r.visibility="hidden"),t(i,r)}function m(t,e){return P||(P={ids:{},svgs:{}}),t&&P.svgs[e+"-"+t._id]}function w(t,e,n){P||m();var i=P.ids[n]=(P.ids[n]||0)+1;e.id=n+"-"+i,P.svgs[n+"-"+t._id]=e}function x(t,e){var i=t,r=null;if(P){i="svg"===t.nodeName.toLowerCase()&&t;for(var s in P.svgs)r||(i||(i=n("svg"),i.appendChild(t)),r=i.insertBefore(n("defs"),i.firstChild)),r.appendChild(P.svgs[s]);P=null}return e.asString?(new XMLSerializer).serializeToString(i):i}function b(t,e){var n=k[t._type],i=n&&n(t,e);return i&&t._data&&i.setAttribute("data-paper-data",JSON.stringify(t._data)),i&&p(t,i)}function C(t){return t||(t={}),S=new a(t.precision),t}var S,P,k={group:s,layer:s,raster:h,path:u,shape:c,"compound-path":d,"placed-symbol":f,"point-text":g};y.inject({exportSVG:function(t){return t=C(t),x(b(this,t),t)}}),v.inject({exportSVG:function(t){t=C(t);for(var e=this.layers,i=this.view.getSize(),r=n("svg",{x:0,y:0,width:i.width,height:i.height,version:"1.1",xmlns:"http://www.w3.org/2000/svg","xmlns:xlink":"http://www.w3.org/1999/xlink"}),s=0,a=e.length;a>s;s++)r.appendChild(b(e[s],t));return x(r,t)}})},new function(){function n(t,e,n,i){var r=ie[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 c(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 w,o=a._project,h=o._currentStyle,u=[];s||(a._applyMatrix=!1,a=x(a,t,n),o._currentStyle=a._style.clone());for(var l=0,c=r.length;c>l;l++){var d,f=r[l];1!==f.nodeType||!(d=P(f,!1,i))||d instanceof m||u.push(d)}return a.addChildren(u),s&&(a=x(a.reduce(),t,n)),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 T(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 L(n):new T(n)}function l(t,e){for(var r=t.childNodes,s=[],a=0,o=r.length;o>a;a++){var h=r[a];1===h.nodeType&&s.push(x(new F,h))}var u,l,c,d="radialgradient"===e,f=new R(s,d);return d?(u=i(t,"cx","cy"),l=u.add(n(t,"r"),0),c=i(t,"fx","fy",!0)):(u=i(t,"x1","y1"),l=i(t,"x2","y2")),x(new B(f,u,l,c),t),null}function d(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("("),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 p(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 x(n,i,r){var s={node:V.getStyles(i)||{},parent:!r&&V.getStyles(i.parentNode)||{}};return e.each(M,function(r,a){var o=p(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,e,n){function i(t){paper=s;var i=P(t,e,n),r=n.onLoad,a=s.project&&s.project.view;r&&r.call(this,i),a.update()}if(!t)return null;n?"function"==typeof n&&(n={onLoad:n}):n={};var r=t,s=paper;if(e)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:l,radialgradient:l,image:function(t){var e=new C(n(t,"href",!0));return e.attach("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 m(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 m?r.place(s):r.clone().translate(s):null},circle:function(t){return new b.Circle(i(t,"cx","cy"),n(t,"r"))},ellipse:function(t){return new b.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 b.Rectangle(new f(e,n),s)},line:function(t){return new T.Line(i(t,"x1","y1"),i(t,"x2","y2"))},text:function(t){var e=new D(i(t,"x","y").add(i(t,"dx","dy")));return e.setContent(t.textContent.trim()||""),e}},M=e.each(ne,function(t){this[t.attribute]=function(e,n){if(e[t.set](s(n,t.type,t.fromSVG)),"color"===t.type&&e instanceof b){var i=e[t.get]();i&&i.transform((new g).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 w))return new w(n,t);t.insertChild(0,n)}},gradientTransform:d,transform:d,"fill-opacity":_,"stroke-opacity":_,visibility:function(t,e){t.setVisible("visible"===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 f(s(e,"array")),h=r(i,"width","height",!0);if(t instanceof w){var u=h?o.getSize().divide(h):1,l=(new g).translate(o.getPoint()).scale(u);t.transform(l.inverted())}else if(t instanceof m){h&&o.setSize(h);var c="visible"!=p(i,"overflow",a),d=t._definition;c&&!o.contains(d.getBounds())&&(c=new b.Rectangle(o).transform(d._matrix),c.setClipMask(!0),d.addChild(c))}}}),z={};y.inject({importSVG:function(t,e){return this.addChild(P(t,!0,e))}}),v.inject({importSVG:function(t,e){return this.activate(),P(t,!0,e)}})},e.exports.PaperScript=function(){function t(t,e,n){var i=d[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=f[t];if(n&&e&&e[n])return e[n]();switch(t){case"+":return+e;case"-":return-e}}function i(t){function e(t){for(var e=0,n=s.length;n>e;e++){var i=s[e];if(i[0]>=t)break;t+=i[1]}return t}function n(n){return t.substring(e(n.range[0]),e(n.range[1]))}function i(n,i){for(var r=e(n.range[0]),a=e(n.range[1]),o=0,h=s.length-1;h>=0;h--)if(r>s[h][0]){o=h+1;break}s.splice(o,0,[r,i.length-a+r]),t=t.substring(0,r)+i+t.substring(a)}function r(t,e){if(t){for(var s in t)if("range"!==s){var a=t[s];if(Array.isArray(a))for(var o=0,h=a.length;h>o;o++)r(a[o],t);else a&&"object"==typeof a&&r(a,t)}switch(t&&t.type){case"UnaryExpression":if(t.operator in f&&"Literal"!==t.argument.type){var u=n(t.argument);i(t,'$_("'+t.operator+'", '+u+")")}break;case"BinaryExpression":if(t.operator in d&&"Literal"!==t.left.type){var l=n(t.left),c=n(t.right);i(t,"_$_("+l+', "'+t.operator+'", '+c+")")}break;case"UpdateExpression":case"AssignmentExpression":if(!e||!("ForStatement"===e.type||"BinaryExpression"===e.type&&/^[=!<>]/.test(e.operator)||"MemberExpression"===e.type&&e.computed))if("UpdateExpression"===t.type){if(!t.prefix){var u=n(t.argument);i(t,u+" = _$_("+u+', "'+t.operator[0]+'", 1)')}}else if(/^.=$/.test(t.operator)&&"Literal"!==t.left.type){var l=n(t.left),c=n(t.right);i(t,l+" = _$_("+l+', "'+t.operator[0]+'", '+c+")")}}}}var s=[];return r(l.acorn.parse(t,{ranges:!0})),t}function s(r,s){function a(t,e){for(var n in t)!e&&/^_/.test(n)||!RegExp("\\b"+n.replace(/\$/g,"\\$")+"\\b").test(r)||(f.push(n),_.push(t[n]))}paper=s;var o,u=s.getView(),l=/\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(r)?new K:null,c=l?l._events:[],d=["onFrame","onResize"].concat(c),f=[],_=[];r=i(r),a({_$_:t,$_:n,view:u,tool:l},!0),a(s),d=e.each(d,function(t){RegExp("\\s+"+t+"\\b").test(r)&&(f.push(t),this.push(t+": "+t))},[]).join(", "),d&&(r+="\nreturn { "+d+" };");var g=window.InstallTrigger;if(g||window.chrome){var p=document.createElement("script"),v=document.head;g&&(r="\n"+r),p.appendChild(document.createTextNode("paper._execute = function("+f+") {"+r+"\n}")),v.appendChild(p),o=paper._execute,delete paper._execute,v.removeChild(p)}else o=Function(f,r);var m=o.apply(s,_)||{};e.each(c,function(t){var e=m[t];e&&(l[t]=e)}),u&&(m.onResize&&u.setOnResize(m.onResize),u.fire("resize",{size:u.size,delta:new h}),m.onFrame&&u.setOnFrame(m.onFrame),u.update())}function a(){e.each(document.getElementsByTagName("script"),function(t){if(/^text\/(?:x-|)paperscript$/.test(t.type)&&!t.getAttribute("data-paper-ignore")){var e=r.getAttribute(t,"canvas"),n=r.get(e)||new r(t).setup(e),i=t.src;i?Q.request("get",i,function(t){s(t,n)}):s(t.innerHTML,n),t.setAttribute("data-paper-ignore",!0)}},this)}var o,u,l=this;!function(t,e){return"object"==typeof o&&"object"==typeof module?e(o):"function"==typeof u&&u.amd?u(["exports"],e):(e(t.acorn||(t.acorn={})),void 0)}(this,function(t){"use strict";function e(t){ce=t||{};for(var e in ge)Object.prototype.hasOwnProperty.call(ce,e)||(ce[e]=ge[e]);_e=ce.sourceFile||null}function n(t,e){var n=pe(de,t);e+=" ("+n.line+":"+n.column+")";var i=new SyntaxError(e);throw i.pos=t,i.loc=n,i.raisedAt=ve,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;rve&&10!==n&&13!==n&&8232!==n&&8329!==n;)++ve,n=de.charCodeAt(ve);ce.onComment&&ce.onComment(!1,de.slice(t+2,ve),t,ve,e,ce.locations&&new r)}function u(){for(;fe>ve;){var t=de.charCodeAt(ve);if(32===t)++ve;else if(13===t){++ve;var e=de.charCodeAt(ve);10===e&&++ve,ce.locations&&(++Pe,ke=ve)}else if(10===t)++ve,++Pe,ke=ve;else if(14>t&&t>8)++ve;else if(47===t){var e=de.charCodeAt(ve+1);if(42===e)o();else{if(47!==e)break;h()}}else if(160===t)++ve;else{if(!(t>=5760&&Un.test(String.fromCharCode(t))))break;++ve}}}function l(){var t=de.charCodeAt(ve+1);return t>=48&&57>=t?S(!0):(++ve,a(xn))}function c(){var t=de.charCodeAt(ve+1);return Se?(++ve,x()):61===t?w(Pn,2):w(Cn,1)}function d(){var t=de.charCodeAt(ve+1);return 61===t?w(Pn,2):w(Dn,1)}function f(t){var e=de.charCodeAt(ve+1);return e===t?w(124===t?In:An,2):61===e?w(Pn,2):w(124===t?On:Ln,1)}function _(){var t=de.charCodeAt(ve+1);return 61===t?w(Pn,2):w(Tn,1)}function g(t){var e=de.charCodeAt(ve+1);return e===t?w(Mn,2):61===e?w(Pn,2):w(kn,1)}function p(t){var e=de.charCodeAt(ve+1),n=1;return e===t?(n=62===t&&62===de.charCodeAt(ve+2)?3:2,61===de.charCodeAt(ve+n)?w(Pn,n+1):w(jn,n)):(61===e&&(n=61===de.charCodeAt(ve+2)?3:2),w(Nn,n))}function v(t){var e=de.charCodeAt(ve+1);return 61===e?w(En,61===de.charCodeAt(ve+2)?3:2):w(61===t?Sn:zn,1)}function m(t){switch(t){case 46:return l();case 40:return++ve,a(pn);case 41:return++ve,a(vn);case 59:return++ve,a(yn);case 44:return++ve,a(mn);case 91:return++ve,a(dn);case 93:return++ve,a(fn);case 123:return++ve,a(_n);case 125:return++ve,a(gn);case 58:return++ve,a(wn);case 63:return++ve,a(bn);case 48:var e=de.charCodeAt(ve+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(zn,1)}return!1}function y(t){if(t?ve=me+1:me=ve,ce.locations&&(we=new r),t)return x();if(ve>=fe)return a(Be);var e=de.charCodeAt(ve);if(Kn(e)||92===e)return z();var i=m(e);if(i===!1){var s=String.fromCharCode(e);if("\\"===s||Xn.test(s))return z();n(ve,"Unexpected character '"+s+"'")}return i}function w(t,e){var n=de.slice(ve,ve+e);ve+=e,a(t,n)}function x(){for(var t,e,i="",r=ve;;){ve>=fe&&n(r,"Unterminated regular expression");var s=de.charAt(ve);if(Jn.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}++ve}var i=de.slice(r,ve);++ve;var o=M();return o&&!/^[gmsiy]*$/.test(o)&&n(r,"Invalid regexp flag"),a(Ne,RegExp(i,o))}function b(t,e){for(var n=ve,i=0,r=0,s=null==e?1/0:e;s>r;++r){var a,o=de.charCodeAt(ve);if(a=o>=97?o-97+10:o>=65?o-65+10:o>=48&&57>=o?o-48:1/0,a>=t)break;++ve,i=i*t+a}return ve===n||null!=e&&ve-n!==e?null:i}function C(){ve+=2;var t=b(16);return null==t&&n(me+2,"Expected hexadecimal number"),Kn(de.charCodeAt(ve))&&n(ve,"Identifier directly after number"),a(Ee,t)}function S(t){var e=ve,i=!1,r=48===de.charCodeAt(ve);t||null!==b(10)||n(e,"Invalid number"),46===de.charCodeAt(ve)&&(++ve,b(10),i=!0);var s=de.charCodeAt(ve);(69===s||101===s)&&(s=de.charCodeAt(++ve),(43===s||45===s)&&++ve,null===b(10)&&n(e,"Invalid number"),i=!0),Kn(de.charCodeAt(ve))&&n(ve,"Identifier directly after number");var o,h=de.slice(e,ve);return i?o=parseFloat(h):r&&1!==h.length?/[89]/.test(h)||Te?n(e,"Invalid number"):o=parseInt(h,8):o=parseInt(h,10),a(Ee,o)}function P(t){ve++;for(var e="";;){ve>=fe&&n(me,"Unterminated string constant");var i=de.charCodeAt(ve);if(i===t)return++ve,a(je,e);if(92===i){i=de.charCodeAt(++ve);var r=/^[0-7]+/.exec(de.slice(ve,ve+3));for(r&&(r=r[0]);r&&parseInt(r,8)>255;)r=r.slice(0,r.length-1);if("0"===r&&(r=null),++ve,r)Te&&n(ve-2,"Octal literal in strict mode"),e+=String.fromCharCode(parseInt(r,8)),ve+=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+="\0";break;case 13:10===de.charCodeAt(ve)&&++ve;case 10:ce.locations&&(ke=ve,++Pe);break;default:e+=String.fromCharCode(i)}}else(13===i||10===i||8232===i||8329===i)&&n(me,"Unterminated string constant"),e+=String.fromCharCode(i),++ve}}function k(t){var e=b(16,t);return null===e&&n(me,"Bad character escape sequence"),e}function M(){Rn=!1;for(var t,e=!0,i=ve;;){var r=de.charCodeAt(ve);if(Qn(r))Rn&&(t+=de.charAt(ve)),++ve;else{if(92!==r)break;Rn||(t=de.slice(i,ve)),Rn=!0,117!=de.charCodeAt(++ve)&&n(ve,"Expecting Unicode escape sequence \\uXXXX"),++ve;var s=k(4),a=String.fromCharCode(s);a||n(ve-1,"Invalid Unicode escape"),(e?Kn(s):Qn(s))||n(ve-4,"Invalid Unicode escape"),t+=a}e=!1}return Rn?t:de.slice(i,ve)}function z(){var t=M(),e=De;return Rn||(Hn(t)?e=cn[t]:(ce.forbidReserved&&(3===ce.ecmaVersion?Fn:qn)(t)||Te&&Vn(t))&&n(me,"The keyword '"+t+"' is reserved")),a(e,t)}function I(){Me=me,ze=ye,Ie=xe,y()}function A(t){for(Te=t,ve=ze;ke>ve;)ke=de.lastIndexOf("\n",ke-2)+1,--Pe;u(),y()}function O(){this.type=null,this.start=me,this.end=null}function T(){this.start=we,this.end=null,null!==_e&&(this.source=_e)}function L(){var t=new O;return ce.locations&&(t.loc=new T),ce.ranges&&(t.range=[me,0]),t}function E(t){var e=new O;return e.start=t.start,ce.locations&&(e.loc=new T,e.loc.start=t.loc.start),ce.ranges&&(e.range=[t.range[0],0]),e}function N(t,e){return t.type=e,t.end=ze,ce.locations&&(t.loc.end=Ie),ce.ranges&&(t.range[1]=ze),t}function j(t){return ce.ecmaVersion>=5&&"ExpressionStatement"===t.type&&"Literal"===t.expression.type&&"use strict"===t.expression.value}function D(t){return be===t?(I(),!0):void 0}function B(){return!ce.strictSemicolons&&(be===Be||be===gn||Jn.test(de.slice(ze,me)))}function R(){D(yn)||B()||q()}function F(t){be===t?I():q()}function q(){n(me,"Unexpected token")}function V(t){"Identifier"!==t.type&&"MemberExpression"!==t.type&&n(t.start,"Assigning to rvalue"),Te&&"Identifier"===t.type&&Zn(t.name)&&n(t.start,"Assigning to "+t.name+" in strict mode")}function Z(t){Me=ze=ve,ce.locations&&(Ie=new r),Ae=Te=null,Oe=[],y();var e=t||L(),n=!0;for(t||(e.body=[]);be!==Be;){var i=H();e.body.push(i),n&&j(i)&&A(!0),n=!1}return N(e,"Program")}function H(){be===Cn&&y(!0);var t=be,e=L();switch(t){case Re:case Ve:I();var i=t===Re;D(yn)||B()?e.label=null:be!==De?q():(e.label=le(),R());for(var r=0;re){var r=E(t);r.left=t,r.operator=Ce,I(),r.right=te(ee(),i,n);var r=N(r,/&&|\|\|/.test(r.operator)?"LogicalExpression":"BinaryExpression");return te(r,e,n)}return t}function ee(){if(be.prefix){var t=L(),e=be.isUpdate;return t.operator=Ce,t.prefix=!0,I(),t.argument=ee(),e?V(t.argument):Te&&"delete"===t.operator&&"Identifier"===t.argument.type&&n(t.start,"Deleting local variable in strict mode"),N(t,e?"UpdateExpression":"UnaryExpression") +}for(var i=ne();be.postfix&&!B();){var t=E(i);t.operator=Ce,t.prefix=!1,t.argument=i,V(i),I(),i=N(t,"UpdateExpression")}return i}function ne(){return ie(re())}function ie(t,e){if(D(xn)){var n=E(t);return n.object=t,n.property=le(!0),n.computed=!1,ie(N(n,"MemberExpression"),e)}if(D(dn)){var n=E(t);return n.object=t,n.property=J(),n.computed=!0,F(fn),ie(N(n,"MemberExpression"),e)}if(!e&&D(pn)){var n=E(t);return n.callee=t,n.arguments=ue(vn,!1),ie(N(n,"CallExpression"),e)}return t}function re(){switch(be){case an:var t=L();return I(),N(t,"ThisExpression");case De:return le();case Ee:case je:case Ne:var t=L();return t.value=Ce,t.raw=de.slice(me,ye),I(),N(t,"Literal");case on:case hn:case un:var t=L();return t.value=be.atomValue,t.raw=be.keyword,I(),N(t,"Literal");case pn:var e=we,n=me;I();var i=J();return i.start=n,i.end=ye,ce.locations&&(i.loc.start=e,i.loc.end=xe),ce.ranges&&(i.range=[n,ye]),F(vn),i;case dn:var t=L();return I(),t.elements=ue(fn,!0,!0),N(t,"ArrayExpression");case _n:return ae();case $e:var t=L();return I(),he(t,!1);case sn:return se();default:q()}}function se(){var t=L();return I(),t.callee=ie(re(),!0),t.arguments=D(pn)?ue(vn,!1):Le,N(t,"NewExpression")}function ae(){var t=L(),e=!0,i=!1;for(t.properties=[],I();!D(gn);){if(e)e=!1;else if(F(mn),ce.allowTrailingCommas&&D(gn))break;var r,s={key:oe()},a=!1;if(D(wn)?(s.value=J(!0),r=s.kind="init"):ce.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=oe(),be!==pn&&q(),s.value=he(L(),!1)):q(),"Identifier"===s.key.type&&(Te||i))for(var o=0;oa?t.id:t.params[a];if((Vn(o.name)||Zn(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 ue(t,e,n){for(var i=[],r=!0;!D(t);){if(r)r=!1;else if(F(mn),e&&ce.allowTrailingCommas&&D(t))break;n&&be===mn?i.push(null):i.push(J(!0))}return i}function le(t){var e=L();return e.name=be===De?Ce:t&&!ce.forbidReserved&&be.keyword||q(),I(),N(e,"Identifier")}t.version="0.3.2";var ce,de,fe,_e;t.parse=function(t,n){return de=t+"",fe=de.length,e(n),s(),Z(ce.program)};var ge=t.defaultOptions={ecmaVersion:5,strictSemicolons:!1,allowTrailingCommas:!0,forbidReserved:!1,locations:!1,onComment:null,ranges:!1,program:null,sourceFile:null},pe=t.getLineInfo=function(t,e){for(var n=1,i=0;;){Yn.lastIndex=i;var r=Yn.exec(t);if(!(r&&r.indext?36===t:91>t?!0:97>t?95===t:123>t?!0:t>=170&&Xn.test(String.fromCharCode(t))},Qn=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&&$n.test(String.fromCharCode(t))},ti={kind:"loop"},ei={kind:"switch"}});var d={"+":"__add","-":"__subtract","*":"__multiply","/":"__divide","%":"__modulo","==":"equals","!=":"equals"},f={"-":"__negate","+":null},_=e.each(["add","subtract","multiply","divide","modulo","negate"],function(t){this["__"+t]="#"+t},{});return h.inject(_),c.inject(_),B.inject(_),"complete"===document.readyState?setTimeout(a):Z.add(window,{load:a}),{compile:i,execute:s,load:a,lineNumberBase:0}}.call(this),paper=new(r.inject(e.exports,{enumerable:!0,Base:e,Numerical:o,DomElement:V,DomEvent:Z,Http:Q,Key:X})),"function"==typeof define&&define.amd&&define("paper",paper),paper}; \ No newline at end of file diff --git a/dist/paper-node.js b/dist/paper-node.js index 060a254c..c6ef0675 100644 --- a/dist/paper-node.js +++ b/dist/paper-node.js @@ -1,28 +1,28 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * Acorn.js * http://marijnhaverbeke.nl/acorn/ * * Acorn is a tiny, fast JavaScript parser written in JavaScript, @@ -33,8 +33,7 @@ var paper = new function(undefined) { var Base = new function() { - var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/, - slice = [].slice, + var hidden = /^(statics|preserve|enumerable|prototype|toString|valueOf)$/, forEach = [].forEach || function(iter, bind) { for (var i = 0, l = this.length; i < l; i++) @@ -79,10 +78,10 @@ var Base = new function() { return _define(obj, name, desc); }; - function inject(dest, src, enumerable, base, preserve, generics) { + function inject(dest, src, enumerable, base, preserve) { var beans; - function field(name, val, dontCheck, generics) { + function field(name, val, dontCheck) { var val = val || (val = describe(src, name)) && (val.get ? val : val.value); if (typeof val === 'string' && val[0] === '#') @@ -96,11 +95,11 @@ var Base = new function() { && (!preserve || !prev)) { if (isFunc && prev) val.base = prev; - if (isFunc && beans && val.length === 0 - && (bean = name.match(/^(get|is)(([A-Z])(.*))$/))) - beans.push([ bean[3].toLowerCase() + bean[4], bean[2] ]); + if (isFunc && beans + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beans[bean[3].toLowerCase() + bean[4]] = bean[2]; if (!res || isFunc || !res.get || typeof res.get !== 'function' - || res.get.length !== 0) + || !Base.isPlainObject(res)) res = { value: res, writable: true }; if ((describe(dest, name) || { configurable: true }).configurable) { @@ -109,27 +108,20 @@ var Base = new function() { } define(dest, name, res); } - if (generics && isFunc && (!preserve || !generics[name])) { - generics[name] = function(bind) { - return bind && dest[name].apply(bind, - slice.call(arguments, 1)); - }; - } } if (src) { - beans = []; + beans = {}; for (var name in src) if (src.hasOwnProperty(name) && !hidden.test(name)) - field(name, null, true, generics); + field(name, null, true); field('toString'); field('valueOf'); - for (var i = 0, l = beans.length; i < l; i++) { - var bean = beans[i], - part = bean[1]; - field(bean[0], { - get: dest['get' + part] || dest['is' + part], - set: dest['set' + part] - }, true); + for (var name in beans) { + var part = beans[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (get.length === 0 || set)) + field(name, { get: get, set: set }, true); } } return dest; @@ -151,10 +143,6 @@ var Base = new function() { return dest; } - function clone(obj) { - return copy(new obj.constructor(), obj); - } - return inject(function Base() { for (var i = 0, l = arguments.length; i < l; i++) copy(this, arguments[i]); @@ -166,7 +154,7 @@ var Base = new function() { statics = src.statics === true ? src : src.statics; if (statics != src) inject(proto, src, src.enumerable, base && base.prototype, - src.preserve, src.generics && this); + src.preserve); inject(this, statics, true, base, src.preserve); } for (var i = 1, l = arguments.length; i < l; i++) @@ -184,6 +172,7 @@ var Base = new function() { base.apply(this, arguments); }; ctor.prototype = create(this.prototype); + ctor.base = base; define(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true }); inject(ctor, this, true); @@ -240,8 +229,6 @@ if (typeof module !== 'undefined') module.exports = Base; Base.inject({ - generics: true, - toString: function() { return this._id != null ? (this._class || 'Object') + (this._name @@ -282,7 +269,9 @@ Base.inject({ statics: { - exports: {}, + exports: { + enumerable: true + }, extend: function extend() { var res = extend.base.apply(this, arguments), @@ -319,7 +308,8 @@ Base.inject({ if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1)) return false; for (var i in obj1) { - if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i])) + if (obj1.hasOwnProperty(i) + && !Base.equals(obj1[i], obj2[i])) return false; } return true; @@ -327,58 +317,57 @@ Base.inject({ return false; }, - read: function(list, start, length, options) { + read: function(list, start, options, length) { if (this === Base) { var value = this.peek(list, start); - list._index++; - list.__read = 1; + list.__index++; return value; } var proto = this.prototype, readIndex = proto._readIndex, - index = start || readIndex && list._index || 0; + 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; + list.__index = index + 1; return obj && options && options.clone ? obj.clone() : obj; } obj = Base.create(this.prototype); if (readIndex) obj.__read = true; - if (options) - obj.__options = options; 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; - list.__read = obj.__read; - delete obj.__read; - if (options) - delete obj.__options; + list.__index = index + obj.__read; + obj.__read = undefined; } return obj; }, peek: function(list, start) { - return list[list._index = start || list._index || 0]; + 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; + 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, 0, options) - : this.read(list, i, 1, options)); + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); } return res; }, - readNamed: function(list, name, start, length, options) { + readNamed: function(list, name, start, options, length) { var value = this.getNamed(list, name), hasObject = value !== undefined; if (hasObject) { @@ -389,7 +378,7 @@ Base.inject({ } filtered[name] = undefined; } - return this.read(hasObject ? [value] : list, start, length, options); + return this.read(hasObject ? [value] : list, start, options, length); }, getNamed: function(list, name) { @@ -464,7 +453,8 @@ Base.inject({ }, deserialize: function(json, create, _data) { - var res = json; + var res = json, + isRoot = !_data; _data = _data || {}; if (Array.isArray(json)) { var type = json[0], @@ -482,7 +472,7 @@ Base.inject({ } else if (type) { var args = res; if (create) { - res = create(type, args); + res = create(type, args, isRoot); } else { res = Base.create(type.prototype); type.apply(res, args); @@ -497,18 +487,21 @@ Base.inject({ }, exportJSON: function(obj, options) { - return JSON.stringify(Base.serialize(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) { + function(type, args, isRoot) { var obj = target && target.constructor === type ? target : Base.create(type.prototype), isTarget = obj === target; - if (args.length === 1 && obj instanceof Item + if (!isRoot && args.length === 1 && obj instanceof Item && (!(obj instanceof Layer) || isTarget)) { var arg = args[0]; if (Base.isPlainObject(arg)) @@ -538,7 +531,7 @@ Base.inject({ args.push.apply(args, items); var removed = list.splice.apply(list, args); for (var i = 0, l = removed.length; i < l; i++) - delete removed[i]._index; + removed[i]._index = undefined; for (var i = index + amount, l = list.length; i < l; i++) list[i]._index = i; return removed; @@ -617,27 +610,14 @@ var Callback = { if (!handlers) return false; var args = [].slice.call(arguments, 1), - PaperScript = paper.PaperScript, - handleException = PaperScript && PaperScript.handleException, that = this; - - function callHandlers() { - for (var i in handlers) { - if (handlers[i].apply(that, args) === false - && event && event.stop) - event.stop(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(that, args) === false + && event && event.stop) { + event.stop(); + break; } } - - if (handleException) { - try { - callHandlers(); - } catch (e) { - handleException(e); - } - } else { - callHandlers(); - } return true; }, @@ -649,6 +629,19 @@ var Callback = { off: '#detach', trigger: '#fire', + _installEvents: function(install) { + var handlers = this._handlers, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var entry = this._eventTypes[type], + func = entry[key]; + if (func) + func.call(this, type); + } + } + }, + statics: { inject: function inject() { for (var i = 0, l = arguments.length; i < l; i++) { @@ -667,11 +660,11 @@ var Callback = { return this[name]; }; src['set' + part] = function(func) { - if (func) { + var prev = this[name]; + if (prev) + this.detach(type, prev); + if (func) this.attach(type, func); - } else if (this[name]) { - this.detach(type, this[name]); - } this[name] = func; }; }); @@ -689,6 +682,11 @@ var PaperScope = Base.extend({ initialize: function PaperScope(script) { paper = this; + this.settings = { + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }; this.project = null; this.projects = []; this.tools = []; @@ -708,26 +706,19 @@ var PaperScope = Base.extend({ } }, - version: '0.9.15', + version: '0.9.16', getView: function() { return this.project && this.project.view; }, - getTool: function() { - if (!this._tool) - this._tool = new Tool(); - return this._tool; - }, - getPaper: function() { return this; }, - evaluate: function(code) { - var res = paper.PaperScript.evaluate(code, this); + execute: function(code) { + paper.PaperScript.execute(code, this); View.updateFocus(); - return res; }, install: function(scope) { @@ -740,10 +731,9 @@ var PaperScope = Base.extend({ } }); }); - for (var key in this) { - if (!/^(version|_id)/.test(key)) + for (var key in this) + if (!/^_/.test(key) && this[key]) scope[key] = this[key]; - } }, setup: function(canvas) { @@ -783,7 +773,7 @@ var PaperScope = Base.extend({ _id: 0, get: function(id) { - if (typeof id === 'object') + if (id && id.getAttribute) id = id.getAttribute('id'); return this._scopes[id] || null; }, @@ -898,15 +888,29 @@ var Numerical = new function() { sqrt = Math.sqrt, pow = Math.pow, cos = Math.cos, - PI = Math.PI; + PI = Math.PI, + TOLERANCE = 10e-6, + EPSILON = 10e-12; + + function setupRoots(roots, min, max) { + var unbound = min === undefined, + minE = min - EPSILON, + maxE = max + EPSILON, + count = 0; + return function(root) { + if (unbound || root > minE && root < maxE) + roots[count++] = root < min ? min : root > max ? max : root; + return count; + }; + } return { - TOLERANCE: 10e-6, - EPSILON: 10e-12, + TOLERANCE: TOLERANCE, + EPSILON: EPSILON, KAPPA: 4 * (sqrt(2) - 1) / 3, isZero: function(val) { - return abs(val) <= Numerical.EPSILON; + return abs(val) <= EPSILON; }, integrate: function(f, a, b, n) { @@ -927,10 +931,10 @@ var Numerical = new function() { findRoot: function(f, df, x, a, b, n, tolerance) { for (var i = 0; i < n; i++) { var fx = f(x), - dx = fx / df(x); + dx = fx / df(x), + nx = x - dx; if (abs(dx) < tolerance) - return x; - var nx = x - dx; + return nx; if (fx > 0) { b = x; x = nx <= a ? 0.5 * (a + b) : nx; @@ -939,65 +943,45 @@ var Numerical = new function() { x = nx >= b ? 0.5 * (a + b) : nx; } } + return x; }, solveQuadratic: function(a, b, c, roots, min, max) { - var epsilon = Numerical.EPSILON, - unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; + var add = setupRoots(roots, min, max); - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - - if (abs(a) < epsilon) { - if (abs(b) >= epsilon) + if (abs(a) < EPSILON) { + if (abs(b) >= EPSILON) return add(-c / b); - return abs(c) < epsilon ? -1 : 0; + return abs(c) < EPSILON ? -1 : 0; } var p = b / (2 * a); var q = c / a; var p2 = p * p; - if (p2 < q - epsilon) + if (p2 < q - EPSILON) return 0; - var s = p2 > q ? sqrt(p2 - q) : 0; - add (s - p); + var s = p2 > q ? sqrt(p2 - q) : 0, + count = add(s - p); if (s > 0) - add(-s - p); + count = add(-s - p); return count; }, solveCubic: function(a, b, c, d, roots, min, max) { - var epsilon = Numerical.EPSILON; - if (abs(a) < epsilon) + if (abs(a) < EPSILON) return Numerical.solveQuadratic(b, c, d, roots, min, max); - var unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; - - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - b /= a; c /= a; d /= a; - var bb = b * b, + var add = setupRoots(roots, min, max), + bb = b * b, p = (bb - 3 * c) / 9, q = (2 * bb * b - 9 * b * c + 27 * d) / 54, ppp = p * p * p, D = q * q - ppp; b /= 3; - if (abs(D) < epsilon) { - if (abs(q) < epsilon) + if (abs(D) < EPSILON) { + if (abs(q) < EPSILON) return add(-b); var sqp = sqrt(p), snq = q > 0 ? 1 : -1; @@ -1066,10 +1050,11 @@ var Point = Base.extend({ }, equals: function(point) { - return point === this || point && (this.x === point.x - && this.y === point.y - || Array.isArray(point) && this.x === point[0] - && this.y === point[1]) || false; + 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() { @@ -1086,28 +1071,28 @@ var Point = Base.extend({ return [f.number(this.x), f.number(this.y)]; }, - add: function(point) { - point = Point.read(arguments); + add: function() { + var point = Point.read(arguments); return new Point(this.x + point.x, this.y + point.y); }, - subtract: function(point) { - point = Point.read(arguments); + subtract: function() { + var point = Point.read(arguments); return new Point(this.x - point.x, this.y - point.y); }, - multiply: function(point) { - point = Point.read(arguments); + multiply: function() { + var point = Point.read(arguments); return new Point(this.x * point.x, this.y * point.y); }, - divide: function(point) { - point = Point.read(arguments); + divide: function() { + var point = Point.read(arguments); return new Point(this.x / point.x, this.y / point.y); }, - modulo: function(point) { - point = Point.read(arguments); + modulo: function() { + var point = Point.read(arguments); return new Point(this.x % point.x, this.y % point.y); }, @@ -1119,17 +1104,17 @@ var Point = Base.extend({ return matrix ? matrix._transformPoint(this) : this; }, - getDistance: function(point, squared) { - point = Point.read(arguments); - var x = point.x - this.x, + getDistance: function(_point, squared) { + 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); }, getLength: function() { - var length = this.x * this.x + this.y * this.y; - return arguments.length && arguments[0] ? length : Math.sqrt(length); + return Math.sqrt(this.x * this.x + this.y * this.y); }, setLength: function(length) { @@ -1148,7 +1133,6 @@ var Point = Base.extend({ this.y * scale ); } - return this; }, normalize: function(length) { @@ -1157,28 +1141,24 @@ var Point = Base.extend({ var current = this.getLength(), scale = current !== 0 ? length / current : 0, point = new Point(this.x * scale, this.y * scale); - point._angle = this._angle; + if (scale >= 0) + point._angle = this._angle; return point; }, getAngle: function() { - return this.getAngleInRadians(arguments[0]) * 180 / Math.PI; + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; }, setAngle: function(angle) { - angle = this._angle = angle * Math.PI / 180; - if (!this.isZero()) { - var length = this.getLength(); - this.set( - Math.cos(angle) * length, - Math.sin(angle) * length - ); - } - return this; + this.setAngleInRadians.call(this, angle * Math.PI / 180); }, + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + getAngleInRadians: function() { - if (arguments[0] === undefined) { + if (!arguments.length) { return this.isZero() ? this._angle || 0 : this._angle = Math.atan2(this.y, this.x); @@ -1193,15 +1173,23 @@ var Point = Base.extend({ } }, - getAngleInDegrees: function() { - return this.getAngle(arguments[0]); + 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; }, - getDirectedAngle: function(point) { + getDirectedAngle: function(_point) { + var point = _point; point = Point.read(arguments); return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; }, @@ -1215,7 +1203,7 @@ var Point = Base.extend({ c = Math.cos(angle); point = new Point( point.x * c - point.y * s, - point.y * c + point.x * s + point.x * s + point.y * c ); return center ? point.add(center) : point; }, @@ -1229,11 +1217,11 @@ var Point = Base.extend({ }, isColinear: function(point) { - return this.cross(point) < 0.00001; + return Math.abs(this.cross(point)) < 0.00001; }, isOrthogonal: function(point) { - return this.dot(point) < 0.00001; + return Math.abs(this.dot(point)) < 0.00001; }, isZero: function() { @@ -1244,18 +1232,18 @@ var Point = Base.extend({ return isNaN(this.x) || isNaN(this.y); }, - dot: function(point) { - point = Point.read(arguments); + dot: function() { + var point = Point.read(arguments); return this.x * point.x + this.y * point.y; }, - cross: function(point) { - point = Point.read(arguments); + cross: function() { + var point = Point.read(arguments); return this.x * point.y - this.y * point.x; }, - project: function(point) { - point = Point.read(arguments); + project: function() { + var point = Point.read(arguments); if (point.isZero()) { return new Point(0, 0); } else { @@ -1397,28 +1385,28 @@ var Size = Base.extend({ f.number(this.height)]; }, - add: function(size) { - size = Size.read(arguments); + add: function() { + var size = Size.read(arguments); return new Size(this.width + size.width, this.height + size.height); }, - subtract: function(size) { - size = Size.read(arguments); + subtract: function() { + var size = Size.read(arguments); return new Size(this.width - size.width, this.height - size.height); }, - multiply: function(size) { - size = Size.read(arguments); + multiply: function() { + var size = Size.read(arguments); return new Size(this.width * size.width, this.height * size.height); }, - divide: function(size) { - size = Size.read(arguments); + divide: function() { + var size = Size.read(arguments); return new Size(this.width / size.width, this.height / size.height); }, - modulo: function(size) { - size = Size.read(arguments); + modulo: function() { + var size = Size.read(arguments); return new Size(this.width % size.width, this.height % size.height); }, @@ -1550,7 +1538,7 @@ var Rectangle = Base.extend({ this.width = size.width; this.height = size.height; } - read = arguments._index; + read = arguments.__index; } if (this.__read) this.__read = read; @@ -1569,11 +1557,12 @@ var Rectangle = Base.extend({ }, equals: function(rect) { - if (Base.isPlainValue(rect)) - rect = Rectangle.read(arguments); - return rect === this - || rect && this.x === rect.x && this.y === rect.y - && this.width === rect.width && this.height === rect.height + 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; }, @@ -1594,24 +1583,24 @@ var Rectangle = Base.extend({ f.number(this.height)]; }, - getPoint: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.x, this.y, this, 'setPoint'); + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.x = point.x; this.y = point.y; }, - getSize: function() { - return new (arguments[0] ? Size : LinkedSize) - (this.width, this.height, this, 'setSize'); + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); }, - setSize: function(size) { - size = Size.read(arguments); + setSize: function() { + var size = Size.read(arguments); if (this._fixX) this.x += (this.width - size.width) * this._fixX; if (this._fixY) @@ -1690,20 +1679,24 @@ var Rectangle = Base.extend({ this._fixY = 0.5; }, - getCenter: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.getCenterX(), this.getCenterY(), this, 'setCenter'); + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, - setCenter: function(point) { - point = Point.read(arguments); + 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; + return this.width === 0 || this.height === 0; }, contains: function(arg) { @@ -1729,42 +1722,42 @@ var Rectangle = Base.extend({ && y + rect.height <= this.y + this.height; }, - intersects: function(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.max(this.x, rect.x), + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.min(this.x, rect.x), + 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(point) { - point = Point.read(arguments); + 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), @@ -1772,9 +1765,10 @@ var Rectangle = Base.extend({ return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, - expand: function(hor, ver) { - if (ver === undefined) - ver = hor; + 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); }, @@ -1803,12 +1797,12 @@ var Rectangle = Base.extend({ setY = 'set' + y, get = 'get' + part, set = 'set' + part; - this[get] = function() { - return new (arguments[0] ? Point : LinkedPoint) - (this[getX](), this[getY](), this, set); + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); }; - this[set] = function(point) { - point = Point.read(arguments); + this[set] = function() { + var point = Point.read(arguments); this[setX](point.x); this[setY](point.y); }; @@ -1855,7 +1849,7 @@ var LinkedRectangle = Rectangle.extend({ this[name] = function() { this._dontNotify = true; proto[name].apply(this, arguments); - delete this._dontNotify; + this._dontNotify = false; this._owner[this._setter](this); }; }, { @@ -1916,8 +1910,14 @@ var Matrix = Base.extend({ }, _changed: function() { - if (this._owner) - this._owner._changed(5); + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(5); + } + } }, clone: function() { @@ -1940,16 +1940,36 @@ var Matrix = Base.extend({ f.number(this._ty)].join(', ') + ']]'; }, - reset: function() { + 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() { + var owner = this._owner; + if (owner) { + owner.transform(null, true); + 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, 0, { readNull: true }); + center = Point.read(arguments, 0, { readNull: true }); if (center) this.translate(center); this._a *= scale.x; @@ -1962,20 +1982,10 @@ var Matrix = Base.extend({ return this; }, - translate: function(point) { - point = Point.read(arguments); - var 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; - }, - - rotate: function(angle, center) { - center = Point.read(arguments, 1); - angle = angle * Math.PI / 180; - var x = center.x, + 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), @@ -1996,22 +2006,31 @@ var Matrix = Base.extend({ }, shear: function() { - var point = Point.read(arguments), - center = Point.read(arguments, 0, 0, { readNull: true }); + 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 += point.y * this._b; - this._c += point.y * this._d; - this._b += point.x * a; - this._d += point.x * 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 a = this._a, b = this._b, @@ -2049,6 +2068,10 @@ var Matrix = Base.extend({ && this._tx === 0 && this._ty === 0; }, + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + isInvertible: function() { return !!this._getDeterminant(); }, @@ -2167,7 +2190,6 @@ var Matrix = Base.extend({ } return { - translation: this.getTranslation(), scaling: new Point(scaleX, scaleY), rotation: -Math.atan2(b, a) * 180 / Math.PI, shearing: shear @@ -2182,36 +2204,14 @@ var Matrix = Base.extend({ return new Point(this._tx, this._ty); }, - setTranslation: function() { - var point = Point.read(arguments); - this._tx = point.x; - this._ty = point.y; - this._changed(); - }, - getScaling: function() { return (this.decompose() || {}).scaling; }, - setScaling: function() { - var scaling = this.getScaling(); - if (scaling != null) { - var scale = Point.read(arguments); - (this._owner || this).scale( - scale.x / scaling.x, scale.y / scaling.y); - } - }, - getRotation: function() { return (this.decompose() || {}).rotation; }, - setRotation: function(angle) { - var rotation = this.getRotation(); - if (rotation != null) - (this._owner || this).rotate(angle - rotation); - }, - inverted: function() { var det = this._getDeterminant(); return det && new Matrix( @@ -2231,19 +2231,13 @@ var Matrix = Base.extend({ ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); } }, new function() { - return Base.each({ - scaleX: '_a', - scaleY: '_d', - translateX: '_tx', - translateY: '_ty', - shearX: '_b', - shearY: '_c' - }, function(prop, name) { - name = Base.capitalize(name); - this['get' + name] = function() { + return 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' + name] = function(value) { + this['set' + part] = function(value) { this[prop] = value; this._changed(); }; @@ -2376,8 +2370,7 @@ var Project = PaperScopeItem.extend({ this.view = view instanceof View ? view : View.create(view); this._selectedItems = {}; this._selectedItemCount = 0; - this._drawCount = 0; - this.options = {}; + this._updateVersion = 0; }, _serialize: function(options, dictionary) { @@ -2422,7 +2415,7 @@ var Project = PaperScopeItem.extend({ this.activeLayer = child; } else if (child instanceof Item) { (this.activeLayer - || this.addChild(new Layer({ insert: false }))).addChild(child); + || this.addChild(new Layer(Item.NO_INSERT))).addChild(child); } else { child = null; } @@ -2439,6 +2432,10 @@ var Project = PaperScopeItem.extend({ return items; }, + getOptions: function() { + return this._scope.settings; + }, + _updateSelection: function(item) { var id = item._id, selectedItems = this._selectedItems; @@ -2465,9 +2462,9 @@ var Project = PaperScopeItem.extend({ selectedItems[i].setFullySelected(false); }, - hitTest: function(point, options) { - point = Point.read(arguments); - options = HitResult.getOptions(Base.read(arguments)); + 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; @@ -2505,14 +2502,13 @@ var Project = PaperScopeItem.extend({ return Base.importJSON(json, layer && layer.isEmpty() && layer); }, - draw: function(ctx, matrix, ratio) { - this._drawCount++; + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; ctx.save(); matrix.applyToContext(ctx); var param = new Base({ offset: new Point(0, 0), - ratio: ratio, - transforms: [matrix], + pixelRatio: pixelRatio, trackTransforms: true }); for (var i = 0, l = this.layers.length; i < l; i++) @@ -2523,30 +2519,30 @@ var Project = PaperScopeItem.extend({ ctx.save(); ctx.strokeWidth = 1; for (var id in this._selectedItems) { - var item = this._selectedItems[id]; - if (item._drawCount === this._drawCount + var item = this._selectedItems[id], + size = this._scope.settings.handleSize; + half = size / 2; + if (item._updateVersion === this._updateVersion && (item._drawSelected || item._boundsSelected)) { var color = item.getSelectedColor() || item.getLayer().getSelectedColor(); ctx.strokeStyle = ctx.fillStyle = color ? color.toCanvasStyle(ctx) : '#009dec'; - var mx = item._globalMatrix; + var mx = matrix.clone().concatenate(item._globalMatrix); if (item._drawSelected) item._drawSelected(ctx, mx); if (item._boundsSelected) { var coords = mx._transformCorners( - item._getBounds('getBounds')); + item.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.beginPath(); - ctx.rect(coords[i] - 2, coords[++i] - 2, 4, 4); - ctx.fill(); - } + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); } } } @@ -2564,7 +2560,6 @@ var Symbol = Base.extend({ this.project.symbols.push(this); if (item) this.setDefinition(item, dontCenter); - this._instances = {}; }, _serialize: function(options, dictionary) { @@ -2575,24 +2570,27 @@ var Symbol = Base.extend({ }, _changed: function(flags) { - Base.each(this._instances, function(item) { - item._changed(flags); - }); + if (flags & 4) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } }, getDefinition: function() { return this._definition; }, - setDefinition: function(item ) { + setDefinition: function(item, _dontCenter) { if (item._parentSymbol) item = item.clone(); if (this._definition) - delete this._definition._parentSymbol; + this._definition._parentSymbol = null; this._definition = item; item.remove(); item.setSelected(false); - if (!arguments[1]) + if (!_dontCenter) item.setPosition(new Point()); item._parentSymbol = this; this._changed(5); @@ -2619,15 +2617,20 @@ var Item = Base.extend(Callback, { if (name) proto._type = Base.hyphenate(name); return res; - } + }, + + NO_INSERT: { insert: false } }, _class: 'Item', - _transformContent: true, + _applyMatrix: true, + _canApplyMatrix: true, _boundsSelected: false, + _selectChildren: false, _serializeFields: { name: null, matrix: new Matrix(), + pivot: null, locked: false, visible: true, blendMode: 'normal', @@ -2635,6 +2638,7 @@ var Item = Base.extend(Callback, { guide: false, selected: false, clipMask: false, + applyMatrix: null, data: {} }, @@ -2642,21 +2646,26 @@ var Item = Base.extend(Callback, { }, _initialize: function(props, point) { - this._id = Item._id = (Item._id || 0) + 1; + var internal = props && props.internal === true, + matrix = this._matrix = new Matrix(); + project = paper.project; + if (!internal) + this._id = Item._id = (Item._id || 0) + 1; + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this); if (!this._project) { - var project = paper.project; - if (props && props.insert === false) { + if (internal || props && props.insert === false) { this._setProject(project); } else { (project.activeLayer || new Layer()).addChild(this); } } - this._style = new Style(this._project._currentStyle, this); - var matrix = this._matrix = new Matrix(); - if (point) - matrix.translate(point); - matrix._owner = this; - return props ? this._set(props, { insert: true }) : true; + return props && props !== Item.NO_INSERT + ? this._set(props, { insert: true }) + : true; }, _events: new function() { @@ -2746,24 +2755,23 @@ var Item = Base.extend(Callback, { }, _changed: function(flags) { - var parent = this._parent, - project = this._project, - symbol = this._parentSymbol; - this._drawCount = null; + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; if (flags & 4) { - delete this._bounds; - delete this._position; + this._bounds = this._position = this._decomposed = + this._globalMatrix = undefined; } - if (parent && (flags + if (cacheParent && (flags & (4 | 8))) { - parent._clearBoundsCache(); + Item._clearBoundsCache(cacheParent); } if (flags & 2) { - this._clearBoundsCache(); + Item._clearBoundsCache(this); } if (project) { if (flags & 1) { - project._needsRedraw = true; + project._needsUpdate = true; } if (project._changes) { var entry = project._changesById[this._id]; @@ -2782,7 +2790,7 @@ var Item = Base.extend(Callback, { set: function(props) { if (props) - this._set(props); + this._set(props, { insert: true }); return this; }, @@ -2865,7 +2873,7 @@ var Item = Base.extend(Callback, { _guide: false, isSelected: function() { - if (this._children) { + if (this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) if (this._children[i].isSelected()) return true; @@ -2873,12 +2881,12 @@ var Item = Base.extend(Callback, { return this._selected; }, - setSelected: function(selected ) { - if (this._children && !arguments[1]) { + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) this._children[i].setSelected(selected); } - if ((selected = !!selected) != this._selected) { + if ((selected = !!selected) ^ this._selected) { this._selected = selected; this._project._updateSelection(this); this._changed(33); @@ -2934,71 +2942,63 @@ var Item = Base.extend(Callback, { this._data = data; }, - getPosition: function() { - var pos = this._position - || (this._position = this.getBounds().getCenter(true)); - return new (arguments[0] ? Point : LinkedPoint) - (pos.x, pos.y, this, 'setPosition'); + 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))); - } -}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], - function(name) { - this[name] = function() { - var getter = this._boundsGetter, - bounds = this._getCachedBounds(typeof getter == 'string' - ? getter : getter && getter[name] || name, arguments[0]); - return name === 'getBounds' + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments); + this._position = undefined; + }, + + _pivot: null, + + getRegistration: '#getPivot', + setRegistration: '#setPivot' +}, 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, null, + internalGetter); + return key === 'bounds' ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, this, 'setBounds') : bounds; }; + this[key] = { + get: this[getter] + }; }, { - _getCachedBounds: function(getter, matrix, cacheItem) { - var cache = (!matrix || matrix.equals(this._matrix)) && getter; - if (cacheItem && this._parent) { - var id = cacheItem._id, - ref = this._parent._boundsCache - = this._parent._boundsCache || { - ids: {}, - list: [] - }; - if (!ref.ids[id]) { - ref.list.push(cacheItem); - ref.ids[id] = cacheItem; - } - } - if (cache && this._bounds && this._bounds[cache]) - return this._bounds[cache].clone(); - var identity = this._matrix.isIdentity(); - matrix = !matrix || matrix.isIdentity() - ? identity ? null : this._matrix - : identity ? matrix : matrix.clone().concatenate(this._matrix); - var bounds = this._getBounds(getter, matrix, cache ? this : cacheItem); - if (cache) { - if (!this._bounds) - this._bounds = {}; - this._bounds[cache] = bounds.clone(); - } - return bounds; - }, - - _clearBoundsCache: function() { - if (this._boundsCache) { - for (var i = 0, list = this._boundsCache.list, l = list.length; - i < l; i++) { - var item = list[i]; - delete item._bounds; - if (item != this && item._boundsCache) - item._clearBoundsCache(); - } - delete this._boundsCache; - } - }, - _getBounds: function(getter, matrix, cacheItem) { var children = this._children; if (!children || children.length == 0) @@ -3022,9 +3022,9 @@ var Item = Base.extend(Callback, { : new Rectangle(); }, - setBounds: function(rect) { - rect = Rectangle.read(arguments); - var bounds = this.getBounds(), + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), matrix = new Matrix(), center = rect.getCenter(); matrix.translate(center); @@ -3036,53 +3036,155 @@ var Item = Base.extend(Callback, { 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; + var cacheParent = this._parent || this._parentSymbol; + if (cacheItem && cacheParent) { + var id = cacheItem._id, + ref = cacheParent._boundsCache = cacheParent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(cacheItem); + ref.ids[id] = cacheItem; + } + } + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + matrix = !matrix + ? _matrix + : _matrix + ? matrix.clone().concatenate(_matrix) + : matrix; + var bounds = this._getBounds(internalGetter || getter, matrix, + cache ? this : cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _clearBoundsCache: function(item) { + if (item._boundsCache) { + for (var i = 0, list = item._boundsCache.list, l = list.length; + i < l; i++) { + var child = list[i]; + child._bounds = child._position = undefined; + if (child !== item && child._boundsCache) + child._clearBoundsCache(); + } + item._boundsCache = undefined; + } + } } }), { + _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() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.scaling; + }, + + setScaling: function() { + var current = this.getScaling(); + if (current != null) { + 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(matrix) { this._matrix.initialize(matrix); - if (this._transformContent) - this.applyMatrix(true); - this._changed(5); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(5); + } }, getGlobalMatrix: function() { - return this._drawCount === this._project._drawCount - && this._globalMatrix || null; + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + if (this._parent) + matrix.preConcatenate(this._parent.getGlobalMatrix()); + matrix._updateVersion = updateVersion; + } + return matrix; }, - getTransformContent: function() { - return this._transformContent; + getApplyMatrix: function() { + return this._applyMatrix; }, - setTransformContent: function(transform) { - this._transformContent = transform; - if (transform) - this.applyMatrix(); + setApplyMatrix: function(transform) { + if (this._applyMatrix = this._canApplyMatrix && !!transform) + this.transform(null, true); }, + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', + getProject: function() { return this._project; }, - _setProject: function(project) { - if (this._project != project) { - var hasOnFrame = this.responds('frame'); - if (hasOnFrame) - this._animateItem(false); + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); this._project = project; - if (hasOnFrame) - this._animateItem(true); - if (this._children) { - for (var i = 0, l = this._children.length; i < l; i++) { - this._children[i]._setProject(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); + }, + + _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() { @@ -3155,7 +3257,7 @@ var Item = Base.extend(Callback, { }, clone: function(insert) { - return this._clone(new this.constructor({ insert: false }), insert); + return this._clone(new this.constructor(Item.NO_INSERT), insert); }, _clone: function(copy, insert) { @@ -3167,7 +3269,7 @@ var Item = Base.extend(Callback, { if (insert || insert === undefined) copy.insertAbove(this); var keys = ['_locked', '_visible', '_blendMode', '_opacity', - '_clipMask', '_guide']; + '_clipMask', '_guide', '_applyMatrix']; for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; if (this.hasOwnProperty(key)) @@ -3187,22 +3289,22 @@ var Item = Base.extend(Callback, { rasterize: function(resolution) { var bounds = this.getStrokeBounds(), - scale = (resolution || 72) / 72, + view = this._project.view, + scale = (resolution || view && view.getResolution() || 72) / 72, topLeft = bounds.getTopLeft().floor(), bottomRight = bounds.getBottomRight().ceil() size = new Size(bottomRight.subtract(topLeft)), - canvas = CanvasProvider.getCanvas(size), + 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({ transforms: [matrix] })); ctx.restore(); - var raster = new Raster({ - canvas: canvas, - insert: false - }); - raster.setPosition(topLeft.add(size.divide(2))); + 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; }, @@ -3220,33 +3322,47 @@ var Item = Base.extend(Callback, { } return false; } - return point.isInside(this._getBounds('getBounds')); + return point.isInside(this.getInternalBounds()); }, hitTest: function(point, options) { point = Point.read(arguments); options = HitResult.getOptions(Base.read(arguments)); - if (this._locked || !this._visible || this._guide && !options.guides) return null; - if (!this._children && !this.getRoughBounds() - .expand(2 * options.tolerance)._containsPoint(point)) - return null; - point = this._matrix._inverseTransform(point); + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this._project.view, + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.clone().concatenate(matrix) + : this.getGlobalMatrix().clone().preConcatenate( + view ? view._matrix : new Matrix()), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 0.00001) + ); + point = matrix._inverseTransform(point); - var that = this, + 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 && this._type !== options.type), + that = this, res; + function checkBounds(type, part) { var pt = bounds['get' + part](); - if (point.getDistance(pt) < options.tolerance) + if (point.subtract(pt).divide(tolerancePadding).length <= 1) return new HitResult(type, that, { name: Base.hyphenate(part), point: pt }); } - if ((options.center || options.bounds) && - !(this instanceof Layer && !this._parent)) { - var bounds = this._getBounds('getBounds'); + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); if (options.center) res = checkBounds('center', 'Center'); if (!res && options.bounds) { @@ -3259,24 +3375,27 @@ var Item = Base.extend(Callback, { } } - if ((res || (res = this._children || !(options.guides && !this._guide - || options.selected && !this._selected) - ? this._hitTest(point, options) : null)) - && res.point) { - res.point = that._matrix.transform(res.point); + 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._hitTest(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; return res; }, + _getChildHitTestOptions: function(options) { + return options; + }, + _hitTest: function(point, options) { - var children = this._children; - if (children) { - for (var i = children.length - 1, res; i >= 0; i--) - if (res = children[i].hitTest(point, options)) - return res; - } else if (options.fill && this.hasFill() && this._contains(point)) { + if (options.fill && this.hasFill() && this._contains(point)) return new HitResult('fill', this); - } }, matches: function(match) { @@ -3375,16 +3494,17 @@ var Item = Base.extend(Callback, { items = Array.prototype.slice.apply(items); for (var i = items.length - 1; i >= 0; i--) { var item = items[i]; - if (_type && item._type !== _type) + if (_type && item._type !== _type) { items.splice(i, 1); - else + } else { item._remove(true); + } } Base.splice(children, items, index, 0); for (var i = 0, l = items.length; i < l; i++) { var item = items[i]; item._parent = this; - item._setProject(this._project); + item._setProject(this._project, true); if (item._name) item.setName(item._name); } @@ -3432,7 +3552,7 @@ var Item = Base.extend(Callback, { reduce: function() { if (this._children && this._children.length === 1) { - var child = this._children[0]; + var child = this._children[0].reduce(); child.insertAbove(this); child.setStyle(this._style); this.remove(); @@ -3465,8 +3585,7 @@ var Item = Base.extend(Callback, { this._removeNamed(); if (this._index != null) Base.splice(this._parent._children, null, this._index, 1); - if (this.responds('frame')) - this._animateItem(false); + this._installEvents(false); if (notify) this._parent._changed(7); this._parent = null; @@ -3580,90 +3699,87 @@ var Item = Base.extend(Callback, { return false; }, - scale: function(hor, ver , center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; - } - return this.transform(new Matrix().scale(hor, ver, - center || this.getPosition(true))); - }, - translate: function() { var mx = new Matrix(); return this.transform(mx.translate.apply(mx, arguments)); }, - rotate: function(angle, center) { + 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))); - }, + }; +}, { - shear: function(hor, ver, center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; +}), { + transform: function(matrix, _applyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && (!_matrix.isIdentity() || matrix); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + pivot.transform(_matrix); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); } - return this.transform(new Matrix().shear(hor, ver, - center || this.getPosition(true))); - }, - - transform: function(matrix ) { var bounds = this._bounds, position = this._position; - this._matrix.preConcatenate(matrix); - if (this._transformContent || arguments[1]) - this.applyMatrix(true); this._changed(5); - if (bounds && matrix.getRotation() % 90 === 0) { + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { for (var key in bounds) { var rect = bounds[key]; - matrix._transformBounds(rect, rect); + 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 (position) { + } else if (matrix && position) { this._position = matrix._transformPoint(position, position); } return this; }, - _applyMatrix: function(matrix, applyMatrix) { + _transformContent: function(matrix) { var children = this._children; - if (children && children.length > 0) { + if (children) { for (var i = 0, l = children.length; i < l; i++) - children[i].transform(matrix, applyMatrix); + children[i].transform(matrix, true); return true; } }, - applyMatrix: function(_dontNotify) { - var matrix = this._matrix; - if (this._applyMatrix(matrix, true)) { - var style = this._style, - fillColor = style.getFillColor(true), - strokeColor = style.getStrokeColor(true); - if (fillColor) - fillColor.transform(matrix); - if (strokeColor) - strokeColor.transform(matrix); - matrix.reset(); - } - if (!_dontNotify) - this._changed(5); - }, - globalToLocal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._transformPoint(Point.read(arguments)); + return matrix && matrix._inverseTransform(Point.read(arguments)); }, localToGlobal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._inverseTransform(Point.read(arguments)); + return matrix && matrix._transformPoint(Point.read(arguments)); }, fitBounds: function(rectangle, fill) { @@ -3731,13 +3847,21 @@ var Item = Base.extend(Callback, { draw: function(ctx, param) { if (!this._visible || this._opacity === 0) return; - this._drawCount = this._project._drawCount; + var updateVersion = this._updateVersion = this._project._updateVersion; var trackTransforms = param.trackTransforms, - transforms = param.transforms, + transforms = param.transforms = param.transforms || [new Matrix()], + matrix = this._matrix, parentMatrix = transforms[transforms.length - 1], - globalMatrix = parentMatrix.clone().concatenate(this._matrix); - if (trackTransforms) + globalMatrix = parentMatrix.clone().concatenate(matrix); + if (!globalMatrix.isInvertible()) + return; + if (trackTransforms) { + if (!transforms) + transforms = param.transforms = []; transforms.push(this._globalMatrix = globalMatrix); + globalMatrix._updateVersion = updateVersion; + } + var blendMode = this._blendMode, opacity = this._opacity, normalBlend = blendMode === 'normal', @@ -3754,7 +3878,8 @@ var Item = Base.extend(Callback, { itemOffset = param.offset = bounds.getTopLeft().floor(); mainCtx = ctx; ctx = CanvasProvider.getContext( - bounds.getSize().ceil().add(new Size(1, 1)), param.ratio); + bounds.getSize().ceil().add(new Size(1, 1)), + param.pixelRatio); } ctx.save(); if (direct) { @@ -3764,7 +3889,7 @@ var Item = Base.extend(Callback, { } else { ctx.translate(-itemOffset.x, -itemOffset.y); } - (direct ? this._matrix : globalMatrix).applyToContext(ctx); + (direct ? matrix : globalMatrix).applyToContext(ctx); if (!direct && param.clipItem) param.clipItem.draw(ctx, param.extend({ clip: true })); this._draw(ctx, param); @@ -3775,7 +3900,7 @@ var Item = Base.extend(Callback, { ctx.clip(); if (!direct) { BlendMode.process(blendMode, ctx, mainCtx, opacity, - itemOffset.subtract(prevOffset).multiply(param.ratio)); + itemOffset.subtract(prevOffset).multiply(param.pixelRatio)); CanvasProvider.release(ctx); param.offset = prevOffset; } @@ -3808,6 +3933,7 @@ var Item = Base.extend(Callback, { var Group = Item.extend({ _class: 'Group', + _selectChildren: true, _serializeFields: { children: [] }, @@ -3821,12 +3947,8 @@ var Group = Item.extend({ _changed: function _changed(flags) { _changed.base.call(this, flags); - if (flags & 2 && this._transformContent - && !this._matrix.isIdentity()) { - this.applyMatrix(); - } if (flags & (2 | 256)) { - delete this._clipItem; + this._clipItem = undefined; } }, @@ -3888,7 +4010,8 @@ var Layer = Group.extend({ this._project.activeLayer = this.getNextSibling() || this.getPreviousSibling(); Base.splice(this._project.layers, null, this._index, 1); - this._project._needsRedraw = true; + this._installEvents(false); + this._project._needsUpdate = true; return true; } return false; @@ -3917,7 +4040,7 @@ var Layer = Group.extend({ this._remove(true); Base.splice(item._project.layers, [this], item._index + (above ? 1 : 0), 0); - this._setProject(item._project); + this._setProject(item._project, true); return this; } return _insert.base.call(this, above, item, _preserve); @@ -3926,14 +4049,17 @@ var Layer = Group.extend({ var Shape = Item.extend({ _class: 'Shape', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsSelected: true, + _serializeFields: { + shape: null, + size: null, + radius: null + }, - initialize: function Shape(shape, center, size, radius, props) { - this._shape = shape; - this._size = size; - this._radius = radius; - this._initialize(props, center); + initialize: function Shape(props) { + this._initialize(props); }, _equals: function(item) { @@ -3943,26 +4069,33 @@ var Shape = Item.extend({ }, clone: function(insert) { - return this._clone(new Shape(this._shape, this.getPosition(true), - this._size.clone(), - this._radius.clone ? this._radius.clone() : this._radius, - { insert: false }), insert); + var copy = new Shape(Item.NO_INSERT); + copy.setShape(this._shape); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); }, getShape: function() { return this._shape; }, + setShape: function(shape) { + this._shape = shape; + }, + getSize: function() { var size = this._size; return new LinkedSize(size.width, size.height, this, 'setSize'); }, setSize: function() { - var shape = this._shape, - size = Size.read(arguments); - if (!this._size.equals(size)) { - var width = size.width, + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var shape = this._shape, + width = size.width, height = size.height; if (shape === 'rectangle') { var radius = Size.min(this._radius, size.divide(2)); @@ -3995,14 +4128,18 @@ var Shape = Item.extend({ this._size.set(size, size); } else { radius = Size.read(arguments); - if (this._radius.equals(radius)) - return; - this._radius.set(radius.width, radius.height); - if (shape === 'rectangle') { - var size = Size.max(this._size, radius.multiply(2)); - this._size.set(size.width, size.height); - } else if (shape === 'ellipse') { - this._size.set(radius.width * 2, radius.height * 2); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (shape === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (shape === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } } } this._changed(5); @@ -4040,7 +4177,7 @@ var Shape = Item.extend({ } else { var rx = radius.width, ry = radius.height, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (shape === 'ellipse') { var cx = rx * kappa, cy = ry * kappa; @@ -4170,7 +4307,11 @@ new function() { statics: new function() { function createShape(shape, point, size, radius, args) { - return new Shape(shape, point, size, radius, Base.getNamed(args)); + var item = new Shape(Base.getNamed(args)); + item._shape = shape; + item._size = size; + item._radius = radius; + return item.translate(point); } return { @@ -4214,7 +4355,8 @@ statics: new function() { var Raster = Item.extend({ _class: 'Raster', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: 'getBounds', _boundsSelected: true, _serializeFields: { @@ -4239,15 +4381,17 @@ var Raster = Item.extend({ }, clone: function(insert) { - var param = { insert: false }, - image = this._image; + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; if (image) { - param.image = image; - } else if (this._canvas) { - var canvas = param.canvas = CanvasProvider.getCanvas(this._size); - canvas.getContext('2d').drawImage(this._canvas, 0, 0); + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setCanvas(copyCanvas); } - return this._clone(new Raster(param), insert); + return this._clone(copy, insert); }, getSize: function() { @@ -4326,10 +4470,10 @@ var Raster = Item.extend({ setCanvas: '#setImage', - getContext: function() { + getContext: function(modify) { if (!this._context) this._context = this.getCanvas().getContext('2d'); - if (arguments[0]) { + if (modify) { this._image = null; this._changed(129); } @@ -4345,11 +4489,42 @@ var Raster = Item.extend({ }, setSource: function(src) { - var image = new Image(); + var that = this, + image; + + function loaded() { + var view = that._project.view; + if (view) + paper = view._scope; + that.setImage(image); + that.fire('load'); + if (view) + view.update(); + } + + image = new Image(); 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 { - image.src = fs.readFileSync(src); + require('fs').readFile(src, function (err, data) { + if (err) + throw err; + image.src = this._data = data; + loaded(); + }); } this.setImage(image); }, @@ -4358,20 +4533,18 @@ var Raster = Item.extend({ return this._canvas || this._image; }, - getSubCanvas: function(rect) { - rect = Rectangle.read(arguments); - var ctx = CanvasProvider.getContext(rect.getSize()); + getSubCanvas: function(rect) { + 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(rect) { - rect = Rectangle.read(arguments); - var raster = new Raster({ - canvas: this.getSubCanvas(rect), - insert: false - }); + getSubRaster: function(rect) { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setCanvas(this.getSubCanvas(rect)); raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); raster._matrix.preConcatenate(this._matrix); raster.insertAbove(this); @@ -4379,14 +4552,17 @@ var Raster = Item.extend({ }, toDataURL: function() { - if (this._data) + 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() : null; }, - drawImage: function(image, point) { - point = Point.read(arguments, 1); + drawImage: function(image ) { + var point = Point.read(arguments, 1); this.getContext(true).drawImage(image, point.x, point.y); }, @@ -4440,8 +4616,8 @@ var Raster = Item.extend({ return total ? Color.read(channels) : null; }, - getPixel: function(point) { - point = Point.read(arguments); + getPixel: function(point) { + 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); @@ -4462,21 +4638,21 @@ var Raster = Item.extend({ ctx.putImageData(imageData, point.x, point.y); }, - createImageData: function(size) { - size = Size.read(arguments); + createImageData: function() { + var size = Size.read(arguments); return this.getContext().createImageData(size.width, size.height); }, - getImageData: function(rect) { - rect = Rectangle.read(arguments); + getImageData: function(rect) { + 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, point) { - point = Point.read(arguments, 1); + setImageData: function(data ) { + var point = Point.read(arguments, 1); this.getContext(true).putImageData(data, point.x, point.y); }, @@ -4515,7 +4691,8 @@ var Raster = Item.extend({ var PlacedSymbol = Item.extend({ _class: 'PlacedSymbol', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: { getBounds: 'getStrokeBounds' }, _boundsSelected: true, _serializeFields: { @@ -4537,32 +4714,30 @@ var PlacedSymbol = Item.extend({ }, setSymbol: function(symbol) { - if (this._symbol) - delete this._symbol._instances[this._id]; this._symbol = symbol; - symbol._instances[this._id] = this; + this._changed(5); }, clone: function(insert) { - return this._clone(new PlacedSymbol({ - symbol: this.symbol, - insert: false - }), 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) { - return this.symbol._definition._getCachedBounds(getter, matrix); + _getBounds: function(getter, matrix, cacheItem) { + return this.symbol._definition._getCachedBounds(getter, matrix, + cacheItem); }, _hitTest: function(point, options, matrix) { - var result = this._symbol._definition._hitTest(point, options, matrix); - if (result) - result.item = this; - return result; + var res = this._symbol._definition._hitTest(point, options, matrix); + if (res) + res.item = this; + return res; }, _draw: function(ctx, param) { @@ -4587,7 +4762,7 @@ var HitResult = Base.extend({ getOptions: function(options) { return options && options._merged ? options : new Base({ type: null, - tolerance: paper.project.options.hitTolerance || 2, + tolerance: paper.settings.hitTolerance, fill: !options, stroke: !options, segments: !options, @@ -4640,27 +4815,30 @@ var Segment = Base.extend({ }, _changed: function(point) { - if (!this._path) + var path = this._path; + if (!path) return; - var curve = this._path._curves && this.getCurve(), - other; - if (curve) { - curve._changed(); - if (other = (curve[point == this._point - || point == this._handleIn && curve._segment1 == this - ? 'getPrevious' : 'getNext']())) { - other._changed(); - } + var curves = path._curves, + index = this._index, + curveIn, curveOut; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curveIn = curves[index - 1] + || path._closed && curves[curves.length - 1])) + curveIn._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curveOut = curves[index])) + curveOut._changed(); } - this._path._changed(5); + path._changed(5); }, getPoint: function() { return this._point; }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this._point.set(point.x, point.y); }, @@ -4668,8 +4846,8 @@ var Segment = Base.extend({ return this._handleIn; }, - setHandleIn: function(point) { - point = Point.read(arguments); + setHandleIn: function() { + var point = Point.read(arguments); this._handleIn.set(point.x, point.y); }, @@ -4677,8 +4855,8 @@ var Segment = Base.extend({ return this._handleOut; }, - setHandleOut: function(point) { - point = Point.read(arguments); + setHandleOut: function() { + var point = Point.read(arguments); this._handleOut.set(point.x, point.y); }, @@ -4713,7 +4891,7 @@ var Segment = Base.extend({ var next = this.getNext(), handle1 = this._handleOut, handle2 = next._handleIn, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (handle1.isOrthogonal(handle2)) { var from = this._point, to = next._point, @@ -4727,60 +4905,39 @@ var Segment = Base.extend({ return false; }, - _isSelected: function(point) { + _selectionState: 0, + + isSelected: function(_point) { var state = this._selectionState; - return point == this._point ? !!(state & 4) - : point == this._handleIn ? !!(state & 1) - : point == this._handleOut ? !!(state & 2) + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) : false; }, - _setSelected: function(point, selected) { + setSelected: function(selected, _point) { var path = this._path, selected = !!selected, - state = this._selectionState || 0, - selection = [ - !!(state & 4), - !!(state & 1), - !!(state & 2) - ]; - if (point === this._point) { - if (selected) { - selection[1] = selection[2] = false; - } else { - var previous = this.getPrevious(), - next = this.getNext(); - selection[1] = previous && (previous._point.isSelected() - || previous._handleOut.isSelected()); - selection[2] = next && (next._point.isSelected() - || next._handleIn.isSelected()); - } - selection[0] = 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 { - var index = point === this._handleIn ? 1 : 2; - if (selection[index] != selected) { - if (selected) - selection[0] = false; - selection[index] = selected; - } + state &= ~flag; } - this._selectionState = (selection[0] ? 4 : 0) - | (selection[1] ? 1 : 0) - | (selection[2] ? 2 : 0); - if (path && state != this._selectionState) { - path._updateSelection(this, state, this._selectionState); + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); path._changed(33); } }, - isSelected: function() { - return this._isSelected(this._point); - }, - - setSelected: function(selected) { - this._setSelected(this._point, selected); - }, - getIndex: function() { return this._index !== undefined ? this._index : null; }, @@ -4846,6 +5003,11 @@ var Segment = Base.extend({ 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() @@ -4903,13 +5065,14 @@ var SegmentPoint = Point.extend({ x = y = 0; } else if ((x = point[0]) !== undefined) { y = point[1]; - } else { - if ((x = point.x) === undefined) { - point = Point.read(arguments); - x = point.x; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; } - y = point.y; - selected = point.selected; + y = pt.y; + selected = pt.selected; } this._x = x; this._y = y; @@ -4958,11 +5121,11 @@ var SegmentPoint = Point.extend({ }, setSelected: function(selected) { - this._owner._setSelected(this, selected); + this._owner.setSelected(selected, this); }, isSelected: function() { - return this._owner._isSelected(this); + return this._owner.isSelected(this); } }); @@ -5002,16 +5165,15 @@ var Curve = Base.extend({ }, _changed: function() { - delete this._length; - delete this._bounds; + this._length = this._bounds = undefined; }, getPoint1: function() { return this._segment1._point; }, - setPoint1: function(point) { - point = Point.read(arguments); + setPoint1: function() { + var point = Point.read(arguments); this._segment1._point.set(point.x, point.y); }, @@ -5019,8 +5181,8 @@ var Curve = Base.extend({ return this._segment2._point; }, - setPoint2: function(point) { - point = Point.read(arguments); + setPoint2: function() { + var point = Point.read(arguments); this._segment2._point.set(point.x, point.y); }, @@ -5028,8 +5190,8 @@ var Curve = Base.extend({ return this._segment1._handleOut; }, - setHandle1: function(point) { - point = Point.read(arguments); + setHandle1: function() { + var point = Point.read(arguments); this._segment1._handleOut.set(point.x, point.y); }, @@ -5037,8 +5199,8 @@ var Curve = Base.extend({ return this._segment2._handleIn; }, - setHandle2: function(point) { - point = Point.read(arguments); + setHandle2: function() { + var point = Point.read(arguments); this._segment2._handleIn.set(point.x, point.y); }, @@ -5071,16 +5233,21 @@ var Curve = Base.extend({ }, isSelected: function() { - return this.getHandle1().isSelected() && this.getHandle2().isSelected(); + 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() { - return Curve.getValues(this._segment1, this._segment2); + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); }, getPoints: function() { @@ -5092,15 +5259,12 @@ var Curve = Base.extend({ }, getLength: function() { - var from = arguments[0], - to = arguments[1], - fullLength = arguments.length === 0 || from === 0 && to === 1; - if (fullLength && this._length != null) - return this._length; - var length = Curve.getLength(this.getValues(), from, to); - if (fullLength) - this._length = length; - return length; + if (this._length == null) { + this._length = this.isLinear() + ? this._segment2._point.getDistance(this._segment1._point) + : Curve.getLength(this.getValues(), 0, 1); + } + return this._length; }, getArea: function() { @@ -5111,20 +5275,25 @@ var Curve = Base.extend({ return new Curve(Curve.getPart(this.getValues(), from, to)); }, + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + isLinear: function() { return this._segment1._handleOut.isZero() && this._segment2._handleIn.isZero(); }, + isHorizontal: function() { + return this.isLinear() && Numerical.isZero( + this._segment1._point._y - this._segment2._point._y); + }, + getIntersections: function(curve) { return Curve.getIntersections(this.getValues(), curve.getValues(), this, curve, []); }, - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); - }, - _getParameter: function(offset, isParameter) { return isParameter ? offset @@ -5135,13 +5304,13 @@ var Curve = Base.extend({ : this.getParameterAt(offset, 0); }, - divide: function(offset, isParameter) { + divide: function(offset, isParameter, ignoreLinear) { var parameter = this._getParameter(offset, isParameter), tolerance = 0.00001, res = null; if (parameter > tolerance && parameter < 1 - tolerance) { var parts = Curve.subdivide(this.getValues(), parameter), - isLinear = this.isLinear(), + isLinear = ignoreLinear ? false : this.isLinear(), left = parts[0], right = parts[1]; @@ -5180,6 +5349,22 @@ var Curve = Base.extend({ : null; }, + reverse: function() { + return new Curve(this._segment2.reverse(), this._segment1.reverse()); + }, + + 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); }, @@ -5195,17 +5380,20 @@ var Curve = Base.extend({ }, statics: { - getValues: function(segment1, segment2) { + getValues: function(segment1, segment2, matrix) { var p1 = segment1._point, h1 = segment1._handleOut, h2 = segment2._handleIn, - p2 = segment2._point; - return [ - p1._x, p1._y, - p1._x + h1._x, p1._y + h1._y, - p2._x + h2._x, p2._y + h2._y, - p2._x, p2._y - ]; + 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, 0, values, 0, 6); + return values; }, evaluate: function(v, t, type) { @@ -5213,11 +5401,13 @@ statics: { c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], + tolerance = 0.00001, x, y; - if (type === 0 && (t === 0 || t === 1)) { - x = t === 0 ? p1x : p2x; - y = t === 0 ? p1y : p2y; + if (type === 0 && (t < tolerance || t > 1 - tolerance)) { + var isZero = t < tolerance; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; } else { var cx = 3 * (c1x - p1x), bx = 3 * (c2x - c1x) - cx, @@ -5230,11 +5420,16 @@ statics: { x = ((ax * t + bx) * t + cx) * t + p1x; y = ((ay * t + by) * t + cy) * t + p1y; } else { - var tolerance = 0.00001; if (t < tolerance && c1x === p1x && c1y === p1y || t > 1 - tolerance && c2x === p2x && c2y === p2y) { x = p2x - p1x; y = p2y - p1y; + } else if (t < tolerance) { + x = cx; + y = cy; + } else if (t > 1 - tolerance) { + 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; @@ -5246,7 +5441,7 @@ statics: { } } } - return type == 2 ? new Point(y, -x) : new Point(x, y); + return type === 2 ? new Point(y, -x) : new Point(x, y); }, subdivide: function(v, t) { @@ -5281,11 +5476,10 @@ statics: { }, getParameterOf: function(v, x, y) { - if (Math.abs(v[0] - x) < 0.00001 - && Math.abs(v[1] - y) < 0.00001) + var tolerance = 0.00001; + if (Math.abs(v[0] - x) < tolerance && Math.abs(v[1] - y) < tolerance) return 0; - if (Math.abs(v[6] - x) < 0.00001 - && Math.abs(v[7] - y) < 0.00001) + if (Math.abs(v[6] - x) < tolerance && Math.abs(v[7] - y) < tolerance) return 1; var txs = [], tys = [], @@ -5298,7 +5492,7 @@ statics: { 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) < 0.00001) + if (Math.abs(tx - ty) < tolerance) return (tx + ty) * 0.5; } } @@ -5385,87 +5579,6 @@ statics: { + t * t * t * v3, padding); } - }, - - _getWinding: function(v, prev, x, y, roots1, roots2) { - var tolerance = 0.00001, - abs = Math.abs; - - function getDirection(v) { - var y0 = v[1], - y1 = v[7], - dir = y0 > y1 ? -1 : 1; - return dir === 1 && (y < y0 || y > y1) - || dir === -1 && (y < y1 || y > y0) - ? 0 - : dir; - } - - if (Curve.isLinear(v)) { - var dir = getDirection(v); - if (!dir) - return 0; - var cross = (v[6] - v[0]) * (y - v[1]) - (v[7] - v[1]) * (x - v[0]); - return (cross < -tolerance ? -1 : 1) == dir ? 0 : dir; - } - - var y0 = v[1], - y1 = v[3], - y2 = v[5], - y3 = v[7]; - var a = 3 * (y1 - y2) - y0 + y3, - b = 2 * (y0 + y2) - 4 * y1, - c = y1 - y0; - var count = Numerical.solveQuadratic(a, b, c, roots1, tolerance, - 1 - tolerance), - part, - rest = v, - t1 = roots1[0], - winding = 0; - for (var i = 0; i <= count; i++) { - if (i === count) { - part = rest; - } else { - var curves = Curve.subdivide(rest, t1); - part = curves[0]; - rest = curves[1]; - t1 = roots1[i]; - t1 = (roots1[i + 1] - t1) / (1 - t1); - } - if (i > 0) - part[3] = part[1]; - if (i < count) - part[5] = rest[1]; - var dir = getDirection(part); - if (!dir) - continue; - var t2, - px; - if (Curve.solveCubic(part, 1, y, roots2, -tolerance, 1 + -tolerance) - === 1) { - t2 = roots2[0]; - px = Curve.evaluate(part, t2, 0).x; - } else { - var mid = (part[1] + part[7]) / 2; - t2 = y < mid && dir > 0 ? 0 : 1; - if (t2 === 1 && y == part[7]) - continue; - px = t2 === 0 ? part[0] : part[6]; - } - var slope = Curve.evaluate(part, t2, 1).y, - stationary = abs(slope) < tolerance || t2 < tolerance - && Curve.evaluate(prev, 1, 1).y * slope < 0; - if (x >= px + (stationary ? -tolerance : tolerance * dir) - && !(stationary && (abs(t2) < tolerance - && abs(x - part[0]) > tolerance - || abs(t2 - 1) < tolerance - && abs(x - part[6]) > tolerance))) { - winding += stationary && abs(t2 - (dir > 0 ? 1 : 0)) < tolerance - ? -dir : dir; - } - prev = part; - } - return winding; } }}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], function(name) { @@ -5499,8 +5612,8 @@ statics: { start !== undefined ? start : offset < 0 ? 1 : 0); }, - getParameterOf: function(point) { - point = Point.read(arguments); + getParameterOf: function(point) { + var point = Point.read(arguments); return Curve.getParameterOf(this.getValues(), point.x, point.y); }, @@ -5510,17 +5623,16 @@ statics: { return new CurveLocation(this, offset); }, - getLocationOf: function(point) { - point = Point.read(arguments); - var t = this.getParameterOf(point); + getLocationOf: function(point) { + var point = Point.read(arguments), + t = this.getParameterOf(point); return t != null ? new CurveLocation(this, t) : null; }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var values = this.getValues(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + values = this.getValues(), count = 100, - tolerance = Numerical.TOLERANCE, minDist = Infinity, minT = 0; @@ -5540,7 +5652,7 @@ statics: { refine(i / count); var step = 1 / (count * 2); - while (step > tolerance) { + while (step > 0.00001) { if (!refine(minT - step) && !refine(minT + step)) step /= 2; } @@ -5550,7 +5662,7 @@ statics: { }, getNearestPoint: function(point) { - point = Point.read(arguments); + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); } @@ -5591,11 +5703,12 @@ new function() { if (b === undefined) b = 1; var isZero = Numerical.isZero; - if (isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + 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])) { var dx = v[6] - v[0], dy = v[7] - v[1]; - return (b - a) * Math.sqrt(dx * dx + dy * dy); + return Math.sqrt(dx * dx + dy * dy); } var ds = getLengthIntegrand(v); return Numerical.integrate(ds, a, b, getIterations(a, b)); @@ -5629,148 +5742,89 @@ new function() { } }; }, new function() { - function addLocation(locations, curve1, t1, point1, curve2, t2, point2) { - var first = locations[0], - last = locations[locations.length - 1]; - if ((!first || !point1.isClose(first._point, Numerical.EPSILON)) - && (!last || !point1.isClose(last._point, Numerical.EPSILON))) - locations.push( - new CurveLocation(curve1, t1, point1, curve2, t2, point2)); + 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); } - function addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, range2, recursion) { - recursion = (recursion || 0) + 1; + function addCurveIntersections(v1, v2, curve1, curve2, locations, include, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { if (recursion > 20) return; - range1 = range1 || [ 0, 1 ]; - range2 = range2 || [ 0, 1 ]; - var part1 = Curve.getPart(v1, range1[0], range1[1]), - part2 = Curve.getPart(v2, range2[0], range2[1]), - iteration = 0; - while (iteration++ < 20) { - var range, - intersects1 = clipFatLine(part1, part2, range = range2.slice()), - intersects2 = 0; - if (intersects1 === 0) - break; - if (intersects1 > 0) { - range2 = range; - part2 = Curve.getPart(v2, range2[0], range2[1]); - intersects2 = clipFatLine(part2, part1, range = range1.slice()); - if (intersects2 === 0) - break; - if (intersects1 > 0) { - range1 = range; - part1 = Curve.getPart(v1, range1[0], range1[1]); - } + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + tolerance = 0.00001, + hullEpsilon = 1e-9, + 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, + 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]), + tMinNew, tMaxNew, tDiff; + if (q0x === q3x && uMax - uMin <= hullEpsilon && recursion > 3) { + tMinNew = (tMax + tMin) / 2; + tMaxNew = tMinNew; + 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 false; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + tDiff = tMaxClip - tMinClip; + tMinNew = tMax * tMinClip + tMin * (1 - tMinClip); + tMaxNew = tMax * tMaxClip + tMin * (1 - tMaxClip); + } + if (oldTDiff > 0.8 && tDiff > 0.8) { + 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); + addCurveIntersections( + v2, parts[1], curve2, curve1, locations, include, + 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); + addCurveIntersections( + parts[1], v1, curve2, curve1, locations, include, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } - if (intersects1 < 0 || intersects2 < 0) { - if (range1[1] - range1[0] > range2[1] - range2[0]) { - var t = (range1[0] + range1[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ range1[0], t ], range2, recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ t, range1[1] ], range2, recursion); - break; - } else { - var t = (range2[0] + range2[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ range2[0], t ], recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ t, range2[1] ], recursion); - break; - } - } - if (Math.abs(range1[1] - range1[0]) <= 0.00001 && - Math.abs(range2[1] - range2[0]) <= 0.00001) { - var t1 = (range1[0] + range1[1]) / 2, - t2 = (range2[0] + range2[1]) / 2; - addLocation(locations, + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) < tolerance) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + if (reverse) { + addLocation(locations, include, + curve2, t2, Curve.evaluate(v2, t2, 0), + curve1, t1, Curve.evaluate(v1, t1, 0)); + } else { + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); - break; } + } else { + addCurveIntersections(v2, v1, curve2, curve1, locations, include, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); } } - function clipFatLine(v1, v2, range2) { - var p0x = v1[0], p0y = v1[1], p1x = v1[2], p1y = v1[3], - p2x = v1[4], p2y = v1[5], p3x = v1[6], p3y = v1[7], - q0x = v2[0], q0y = v2[1], q1x = v2[2], q1y = v2[3], - q2x = v2[4], q2y = v2[5], q3x = v2[6], q3y = v2[7], - getSignedDistance = Line.getSignedDistance, - d1 = getSignedDistance(p0x, p0y, p3x, p3y, p1x, p1y) || 0, - d2 = getSignedDistance(p0x, p0y, p3x, p3y, p2x, p2y) || 0, - factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, - dmin = factor * Math.min(0, d1, d2), - dmax = factor * Math.max(0, d1, d2), - dq0 = getSignedDistance(p0x, p0y, p3x, p3y, q0x, q0y), - dq1 = getSignedDistance(p0x, p0y, p3x, p3y, q1x, q1y), - dq2 = getSignedDistance(p0x, p0y, p3x, p3y, q2x, q2y), - dq3 = getSignedDistance(p0x, p0y, p3x, p3y, q3x, q3y); - if (dmin > Math.max(dq0, dq1, dq2, dq3) - || dmax < Math.min(dq0, dq1, dq2, dq3)) - return 0; - var hull = getConvexHull(dq0, dq1, dq2, dq3), - swap; - if (dq3 < dq0) { - swap = dmin; - dmin = dmax; - dmax = swap; - } - var tmaxdmin = -Infinity, - tmin = Infinity, - tmax = -Infinity; - for (var i = 0, l = hull.length; i < l; i++) { - var p1 = hull[i], - p2 = hull[(i + 1) % l]; - if (p2[1] < p1[1]) { - swap = p2; - p2 = p1; - p1 = swap; - } - var x1 = p1[0], - y1 = p1[1], - x2 = p2[0], - y2 = p2[1]; - var inv = (y2 - y1) / (x2 - x1); - if (dmin >= y1 && dmin <= y2) { - var ixdx = x1 + (dmin - y1) / inv; - if (ixdx < tmin) - tmin = ixdx; - if (ixdx > tmaxdmin) - tmaxdmin = ixdx; - } - if (dmax >= y1 && dmax <= y2) { - var ixdx = x1 + (dmax - y1) / inv; - if (ixdx > tmax) - tmax = ixdx; - if (ixdx < tmin) - tmin = 0; - } - } - if (tmin !== Infinity && tmax !== -Infinity) { - var min = Math.min(dmin, dmax), - max = Math.max(dmin, dmax); - if (dq3 > min && dq3 < max) - tmax = 1; - if (dq0 > min && dq0 < max) - tmin = 0; - if (tmaxdmin > tmax) - tmax = 1; - var v2tmin = range2[0], - tdiff = range2[1] - v2tmin; - range2[0] = v2tmin + tmin * tdiff; - range2[1] = v2tmin + tmax * tdiff; - if ((tdiff - (range2[1] - range2[0])) / tdiff >= 0.2) - return 1; - } - if (Curve.getBounds(v1).touches(Curve.getBounds(v2))) - return -1; - return 0; - } - function getConvexHull(dq0, dq1, dq2, dq3) { var p0 = [ 0, dq0 ], p1 = [ 1 / 3, dq1 ], @@ -5778,26 +5832,74 @@ new function() { p3 = [ 1, dq3 ], getSignedDistance = Line.getSignedDistance, dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); + dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), + flip = false, + hull; if (dist1 * dist2 < 0) { - return [ p0, p1, p3, p2 ]; - } - var pmax, cross; - if (Math.abs(dist1) > Math.abs(dist2)) { - pmax = p1; - cross = (dq3 - dq2 - (dq3 - dq0) / 3) - * (2 * (dq3 - dq2) - dq3 + dq1) / 3; + hull = [[p0, p1, p3], [p0, p2, p3]]; + flip = dist1 < 0; } else { - pmax = p2; - cross = (dq1 - dq0 + (dq0 - dq3) / 3) - * (-2 * (dq0 - dq1) + dq0 - dq2) / 3; + 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; } - return cross < 0 - ? [ p0, pmax, p3 ] - : [ p0, p1, p2, p3 ]; + return flip ? hull.reverse() : hull; } - function addCurveLineIntersections(v1, v2, curve1, curve2, locations) { + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + var tProxy, + tVal = null, + px, py, + qx, qy; + for (var i = 0, l = hullBottom.length - 1; i < l; i++) { + py = hullBottom[i][1]; + qy = hullBottom[i + 1][1]; + if (py < qy) { + tProxy = null; + } else if (qy <= dMax) { + px = hullBottom[i][0]; + qx = hullBottom[i + 1][0]; + tProxy = px + (dMax - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + if (hullTop[0][1] <= dMax) + tProxy = hullTop[0][0]; + for (var i = 0, l = hullTop.length - 1; i < l; i++) { + py = hullTop[i][1]; + qy = hullTop[i + 1][1]; + if (py >= dMin) { + tVal = tProxy; + } else if (py > qy) { + tVal = null; + } else if (qy >= dMin) { + px = hullTop[i][0]; + qx = hullTop[i + 1][0]; + tVal = px + (dMin - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + return tVal; + } + + function addCurveLineIntersections(v1, v2, curve1, curve2, locations, + include) { var flip = Curve.isLinear(v1), vc = flip ? v2 : v1, vl = flip ? v1 : v2, @@ -5827,43 +5929,37 @@ new function() { var tl = Curve.getParameterOf(rvl, x, 0), t1 = flip ? tl : tc, t2 = flip ? tc : tl; - addLocation(locations, + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); } } } - function addLineIntersection(v1, v2, curve1, curve2, locations) { + function addLineIntersection(v1, v2, curve1, curve2, locations, include) { var point = Line.intersect( v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]); - if (point) - addLocation(locations, curve1, null, point, curve2); + 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); + } } return { statics: { - getIntersections: function(v1, v2, curve1, curve2, locations) { + getIntersections: function(v1, v2, curve1, curve2, locations, include) { var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2), - c1p1 = curve1.getPoint1(), - c1p2 = curve1.getPoint2(), - c2p1 = curve2.getPoint1(), - c2p2 = curve2.getPoint2(), - tolerance = 0.00001; - if (c1p1.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 0, c1p1); - if (c1p1.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 1, c1p1); + linear2 = Curve.isLinear(v2); (linear1 && linear2 ? addLineIntersection : linear1 || linear2 ? addCurveLineIntersections - : addCurveIntersections)(v1, v2, curve1, curve2, locations); - if (c1p2.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 0, c1p2); - if (c1p2.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 1, c1p2); + : addCurveIntersections)( + v1, v2, curve1, curve2, locations, include, + 0, 1, 0, 1, 0, false, 0); return locations; } }}; @@ -5885,13 +5981,13 @@ var CurveLocation = Base.extend({ this._distance = _distance; }, - getSegment: function() { + getSegment: function(_preferFirst) { if (!this._segment) { var curve = this.getCurve(), parameter = this.getParameter(); if (parameter === 1) { this._segment = curve._segment2; - } else if (parameter === 0 || arguments[0]) { + } else if (parameter === 0 || _preferFirst) { this._segment = curve._segment1; } else if (parameter == null) { return null; @@ -5905,8 +6001,12 @@ var CurveLocation = Base.extend({ return this._segment; }, - getCurve: function() { - if (!this._curve || arguments[0]) { + setSegment: function(segment) { + this._segment = segment; + }, + + getCurve: function(_uncached) { + if (!this._curve || _uncached) { this._curve = this._segment1.getCurve(); if (this._curve.getParameterOf(this._point) == null) this._curve = this._segment2.getPrevious().getCurve(); @@ -5914,6 +6014,10 @@ var CurveLocation = Base.extend({ return this._curve; }, + setCurve: function(curve) { + this._curve = curve; + }, + getIntersection: function() { var intersection = this._intersection; if (!intersection && this._curve2) { @@ -5946,22 +6050,30 @@ var CurveLocation = Base.extend({ return parameter != null && curve && curve.getLength(0, parameter); }, - getParameter: function() { - if ((this._parameter == null || arguments[0]) && this._point) { - var curve = this.getCurve(arguments[0] && this._point); + getParameter: function(_uncached) { + if ((this._parameter == null || _uncached) && this._point) { + var curve = this.getCurve(_uncached && this._point); this._parameter = curve && curve.getParameterOf(this._point); } return this._parameter; }, - getPoint: function() { - if ((!this._point || arguments[0]) && this._parameter != null) { + setParameter: function(parameter) { + this._parameter = parameter; + }, + + getPoint: function(_uncached) { + if ((!this._point || _uncached) && this._parameter != null) { var curve = this.getCurve(); this._point = curve && curve.getPointAt(this._parameter, true); } return this._point; }, + setPoint: function(point) { + this._point = point; + }, + getTangent: function() { var parameter = this.getParameter(), curve = this.getCurve(); @@ -5988,6 +6100,17 @@ var CurveLocation = Base.extend({ return curve && curve.split(this.getParameter(true), true); }, + equals: function(loc) { + var isZero = Numerical.isZero; + return this === loc + || loc + && this._curve === loc._curve + && this._curve2 === loc._curve2 + && isZero(this._parameter - loc._parameter) + && isZero(this._parameter2 - loc._parameter2) + || false; + }, + toString: function() { var parts = [], point = this.getPoint(), @@ -6012,22 +6135,95 @@ var PathItem = Item.extend({ initialize: function PathItem() { }, - getIntersections: function(path) { - if (!this.getBounds().touches(path.getBounds())) + getIntersections: function(path, _expand) { + if (this === path) + path = null; + if (path && !this.getBounds().touches(path.getBounds())) return []; var locations = [], curves1 = this.getCurves(), - curves2 = path.getCurves(), - length2 = curves2.length, - values2 = []; + curves2 = path ? path.getCurves() : curves1, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = path ? path._matrix.orNullIfIdentity() : matrix1, + length1 = curves1.length, + length2 = path ? curves2.length : length1, + values2 = [], + MIN = 1e-11, + MAX = 1 - 1e-11; for (var i = 0; i < length2; i++) - values2[i] = curves2[i].getValues(); - for (var i = 0, l = curves1.length; i < l; i++) { + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { var curve1 = curves1[i], - values1 = curve1.getValues(); - for (var j = 0; j < length2; j++) - Curve.getIntersections(values1, values2[j], curve1, curves2[j], - locations); + 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 <= MAX) { + loc._parameter /= 2; + loc._parameter2 = 0.5 + loc._parameter2 / 2; + return true; + } + } + ); + } + } + 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 >= MIN && t <= MAX; + } + ); + } + } + var last = locations.length - 1; + for (var i = last; i >= 0; i--) { + var loc = locations[i], + next = loc._curve.getNext(), + next2 = loc._curve2.getNext(); + if (next && loc._parameter >= MAX) { + loc._parameter = 0; + loc._curve = next; + } + if (next2 && loc._parameter2 >= MAX) { + loc._parameter2 = 0; + loc._curve2 = next2; + } + } + + 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 === 0 ? last : i - 1])) { + locations.splice(i, 1); + last--; + } + } + } + if (_expand) { + for (var i = last; i >= 0; i--) + locations.push(locations[i].getIntersection()); + locations.sort(compare); } return locations; }, @@ -6037,22 +6233,22 @@ var PathItem = Item.extend({ var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), coords, relative = false, + previous, control, - current = new Point(); + current = new Point(), + start = new Point(); - function getCoord(index, coord, isCurrent) { - var val = parseFloat(coords[index]); + function getCoord(index, coord) { + var val = +coords[index]; if (relative) val += current[coord]; - if (isCurrent) - current[coord] = val; return val; } - function getPoint(index, isCurrent) { + function getPoint(index) { return new Point( - getCoord(index, 'x', isCurrent), - getCoord(index + 1, 'y', isCurrent) + getCoord(index, 'x'), + getCoord(index + 1, 'y') ); } @@ -6060,24 +6256,31 @@ var PathItem = Item.extend({ for (var i = 0, l = parts.length; i < l; i++) { var part = parts[i], - cmd = part[0], - lower = cmd.toLowerCase(); + command = part[0], + lower = command.toLowerCase(); coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); var length = coords && coords.length; - relative = cmd === lower; + relative = command === lower; + if (previous === 'z' && lower !== 'z') + this.moveTo(current = start); switch (lower) { case 'm': case 'l': + var move = lower === 'm'; + if (move && previous && previous !== 'z') + this.closePath(); for (var j = 0; j < length; j += 2) - this[j === 0 && lower === 'm' ? 'moveTo' : 'lineTo']( - getPoint(j, true)); + 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'; + var coord = lower === 'h' ? 'x' : 'y'; for (var j = 0; j < length; j++) { - getCoord(j, coord, true); + current[coord] = getCoord(j, coord); this.lineTo(current); } control = current; @@ -6087,37 +6290,49 @@ var PathItem = Item.extend({ this.cubicCurveTo( getPoint(j), control = getPoint(j + 2), - getPoint(j + 4, true)); + current = getPoint(j + 4)); } break; case 's': for (var j = 0; j < length; j += 4) { this.cubicCurveTo( - current.multiply(2).subtract(control), + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); + previous = lower; } break; case 'q': for (var j = 0; j < length; j += 4) { this.quadraticCurveTo( control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); } break; case 't': for (var j = 0; j < length; j += 2) { this.quadraticCurveTo( - control = current.multiply(2).subtract(control), - getPoint(j, true)); + 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[0], +coords[1]), + +coords[2], +coords[4], +coords[3]); + } break; case 'z': this.closePath(); break; } + previous = lower; } }, @@ -6126,9 +6341,9 @@ var PathItem = Item.extend({ }, _contains: function(point) { - var winding = this._getWinding(point); + var winding = this._getWinding(point, false, true); return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); - } + }, }); @@ -6146,11 +6361,20 @@ var Path = PathItem.extend({ ? typeof arg[0] === 'object' ? arg : arguments - : arg && (arg.point !== undefined && arg.size === undefined - || arg.x !== undefined) + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) ? arguments : null; - this.setSegments(segments || []); + 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); }, @@ -6159,30 +6383,34 @@ var Path = PathItem.extend({ }, clone: function(insert) { - var copy = this._clone(new Path({ - segments: this._segments, - insert: false - }), 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 copy; + return this._clone(copy, insert); }, _changed: function _changed(flags) { _changed.base.call(this, flags); if (flags & 4) { - delete this._length; - delete this._clockwise; + (this._compound ? this._parent : this)._currentPath = undefined; + this._length = this._clockwise = undefined; if (this._curves) { for (var i = 0, l = this._curves.length; i < l; i++) this._curves[i]._changed(5); } + this._monoCurves = undefined; } else if (flags & 8) { - delete this._bounds; + this._bounds = undefined; } }, + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + getSegments: function() { return this._segments; }, @@ -6191,8 +6419,9 @@ var Path = PathItem.extend({ var fullySelected = this.isFullySelected(); this._segments.length = 0; this._selectedSegmentState = 0; - delete this._curves; - this._add(Segment.readAll(segments)); + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); if (fullySelected) this.setFullySelected(true); }, @@ -6227,9 +6456,8 @@ var Path = PathItem.extend({ return curves[curves.length - 1]; }, - getPathData: function() { + getPathData: function(precision) { var segments = this._segments, - precision = arguments[0], f = Formatter.instance, parts = []; @@ -6291,7 +6519,7 @@ var Path = PathItem.extend({ return true; }, - _applyMatrix: function(matrix) { + _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); @@ -6392,7 +6620,7 @@ var Path = PathItem.extend({ return this.removeSegments(index, index + 1)[0] || null; }, - removeSegments: function(from, to) { + removeSegments: function(from, to, _includeCurves) { from = from || 0; to = Base.pick(to, this._segments.length); var segments = this._segments, @@ -6406,8 +6634,7 @@ var Path = PathItem.extend({ var segment = removed[i]; if (segment._selectionState) this._updateSelection(segment, segment._selectionState, 0); - delete segment._index; - delete segment._path; + segment._index = segment._path = null; } for (var i = from, l = segments.length; i < l; i++) segments[i]._index = i; @@ -6416,7 +6643,7 @@ var Path = PathItem.extend({ ? from - 1 : from, curves = curves.splice(index, amount); - if (arguments[2]) + if (_includeCurves) removed._curves = curves.slice(1); this._adjustCurves(index, index); } @@ -6429,7 +6656,7 @@ var Path = PathItem.extend({ isFullySelected: function() { var length = this._segments.length; return this._selected && length > 0 && this._selectedSegmentState - === length * 4; + === length * 7; }, setFullySelected: function(selected) { @@ -6447,10 +6674,10 @@ var Path = PathItem.extend({ _selectSegments: function(selected) { var length = this._segments.length; this._selectedSegmentState = selected - ? length * 4 : 0; + ? length * 7 : 0; for (var i = 0; i < length; i++) this._segments[i]._selectionState = selected - ? 4 : 0; + ? 7 : 0; }, _updateSelection: function(segment, oldState, newState) { @@ -6473,6 +6700,16 @@ var Path = PathItem.extend({ this.setSegments(segments); }, + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (curve.isLinear() && curve.getLength() === 0) + curve.remove(); + } + return this; + }, + simplify: function(tolerance) { if (this._segments.length > 2) { var fitter = new PathFitter(this, tolerance || 2.5); @@ -6490,13 +6727,14 @@ var Path = PathItem.extend({ index = arg.index; parameter = arg.parameter; } - if (parameter >= 1) { + var tolerance = 0.00001; + if (parameter >= 1 - tolerance) { index++; parameter--; } var curves = this.getCurves(); if (index >= 0 && index < curves.length) { - if (parameter > 0) { + if (parameter > tolerance) { curves[index++].divide(parameter, true); } var segs = this.removeSegments(index, this._segments.length, true), @@ -6535,7 +6773,7 @@ var Path = PathItem.extend({ segment._handleOut = handleIn; segment._index = i; } - delete this._curves; + this._curves = null; if (this._clockwise !== undefined) this._clockwise = !this._clockwise; }, @@ -6611,9 +6849,9 @@ var Path = PathItem.extend({ return null; }, - getLocationOf: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(); + getLocationOf: function(point) { + 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) @@ -6633,7 +6871,7 @@ var Path = PathItem.extend({ var start = length, curve = curves[i]; length += curve.getLength(); - if (length >= offset) { + if (length > offset) { return curve.getLocationAt(offset - start); } } @@ -6657,9 +6895,9 @@ var Path = PathItem.extend({ return loc && loc.getNormal(); }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + curves = this.getCurves(), minDist = Infinity, minLoc = null; for (var i = 0, l = curves.length; i < l; i++) { @@ -6672,17 +6910,11 @@ var Path = PathItem.extend({ return minLoc; }, - getNearestPoint: function(point) { - point = Point.read(arguments); + getNearestPoint: function(point) { + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); }, - getStyle: function() { - var parent = this._parent; - return (parent && parent._type === 'compound-path' - ? parent : this)._style; - }, - toShape: function(insert) { if (!this._closed) return null; @@ -6750,114 +6982,80 @@ var Path = PathItem.extend({ return null; }, - _getWinding: function(point) { - var closed = this._closed; - if (!closed && !this.hasFill() - || !this._getBounds('getRoughBounds')._containsPoint(point)) - return 0; - var curves = this.getCurves(), - segments = this._segments, - winding = 0, - roots1 = [], - roots2 = [], - last = (closed - ? curves[curves.length - 1] - : new Curve(segments[segments.length - 1]._point, - segments[0]._point)).getValues(), - previous = last; - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i].getValues(), - x = curve[0], - y = curve[1]; - if (!(x === curve[2] && y === curve[3] && x === curve[4] - && y === curve[5] && x === curve[6] && y === curve[7])) { - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - } - if (!closed) { - winding += Curve._getWinding(last, previous, point.x, point.y, - roots1, roots2); - } - return winding; - }, - _hitTest: function(point, options) { - var style = this.getStyle(), + var that = this, + style = this.getStyle(), segments = this._segments, closed = this._closed, - tolerance = options.tolerance, - radius = 0, join, cap, miterLimit, - that = this, - area, loc, res; - - if (options.stroke) { - radius = style.getStrokeWidth() / 2; + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hasStroke = options.stroke && style.hasStroke(), + hasFill = options.fill && style.hasFill(), + radius = hasStroke ? style.getStrokeWidth() / 2 + : hasFill ? 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'; } - radius += tolerance; } - function checkPoint(seg, pt, name) { - if (point.getDistance(pt) < tolerance) - return new HitResult(name, that, { segment: seg, point: pt }); + 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) { - var pt = seg._point; - return (ends || options.segments) && checkPoint(seg, pt, 'segment') + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') || (!ends && options.handles) && ( - checkPoint(seg, pt.add(seg._handleIn), 'handle-in') || - checkPoint(seg, pt.add(seg._handleOut), 'handle-out')); + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); } - function addAreaPoint(point) { - area.push(point); - } - - function getAreaCurve(index) { - var p1 = area[index], - p2 = area[(index + 1) % area.length]; - return [p1.x, p1.y, p1.x, p1.y, p2.x, p2.y, p2.x ,p2.y]; - } - - function isInArea(point) { - var length = area.length, - previous = getAreaCurve(length - 1), - roots1 = [], - roots2 = [], - winding = 0; - for (var i = 0; i < length; i++) { - var curve = getAreaCurve(i); - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - return !!winding; + function addToArea(point) { + area.add(point); } function checkSegmentStroke(segment) { if (join !== 'round' || cap !== 'round') { - area = []; + area = new Path({ internal: true, closed: true }); if (closed || segment._index > 0 && segment._index < segments.length - 1) { if (join !== 'round' && (segment._handleIn.isZero() || segment._handleOut.isZero())) - Path._addSquareJoin(segment, join, radius, miterLimit, - addAreaPoint, true); + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); } else if (cap !== 'round') { - Path._addSquareCap(segment, cap, radius, addAreaPoint, true); + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); } - if (area.length > 0) - return isInArea(point); } - return point.getDistance(segment._point) <= radius; + return isCloseEnough(segment._point, strokePadding); } if (options.ends && !options.segments && !closed) { @@ -6865,19 +7063,18 @@ var Path = PathItem.extend({ || checkSegmentPoints(segments[segments.length - 1], true)) return res; } else if (options.segments || options.handles) { - for (var i = 0, l = segments.length; i < l; i++) { + for (var i = 0, l = segments.length; i < l; i++) if (res = checkSegmentPoints(segments[i])) return res; - } } - if (radius > 0) { + if (radius != null) { loc = this.getNearestLocation(point); if (loc) { var parameter = loc.getParameter(); if (parameter === 0 || parameter === 1) { if (!checkSegmentStroke(loc.getSegment())) loc = null; - } else if (loc._distance > radius) { + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { loc = null; } } @@ -6892,10 +7089,13 @@ var Path = PathItem.extend({ } } } - return !loc && options.fill && this.hasFill() && this._contains(point) + return !loc && hasFill && this._contains(point) || loc && !hasStroke ? new HitResult('fill', this) : loc - ? new HitResult('stroke', this, { location: loc }) + ? new HitResult('stroke', this, { + location: loc, + point: loc.getPoint() + }) : null; } @@ -6923,24 +7123,19 @@ var Path = PathItem.extend({ var segment = segments[i]; segment._transformCoordinates(matrix, coords, false); var state = segment._selectionState, - selected = state & 4, pX = coords[0], pY = coords[1]; - if (selected || (state & 1)) + if (state & 1) drawHandle(2); - if (selected || (state & 2)) + if (state & 2) drawHandle(4); - ctx.save(); - ctx.beginPath(); - ctx.rect(pX - half, pY - half, size, size); - ctx.fill(); - if (!selected) { - ctx.beginPath(); - ctx.rect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; ctx.fillStyle = '#ffffff'; - ctx.fill(); + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; } - ctx.restore(); } } @@ -6997,14 +7192,14 @@ var Path = PathItem.extend({ for (var i = 0; i < length; i++) drawSegment(i); - if (path._closed && length > 1) + if (path._closed && length > 0) drawSegment(0); } return { _draw: function(ctx, param) { var clip = param.clip, - compound = param.compound; + compound = this._compound = param.compound; if (!compound) ctx.beginPath(); @@ -7019,10 +7214,15 @@ var Path = PathItem.extend({ return dashArray[((i % dashLength) + dashLength) % dashLength]; } - if (hasFill || hasStroke && !dashLength || compound || clip) + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || compound || clip){ drawSegments(ctx, this); - if (this._closed) - ctx.closePath(); + if (this._closed) + ctx.closePath(); + if (!compound) + this._currentPath = ctx.currentPath; + } if (!clip && !compound && (hasFill || hasStroke)) { this._setStyles(ctx); @@ -7058,8 +7258,7 @@ var Path = PathItem.extend({ ctx.beginPath(); drawSegments(ctx, this, matrix); ctx.stroke(); - drawHandles(ctx, this._segments, matrix, - this._project.options.handleSize || 4); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); } }; }, new function() { @@ -7158,16 +7357,17 @@ var Path = PathItem.extend({ }, new function() { function getCurrentSegment(that) { var segments = that._segments; - if (segments.length == 0) + if (segments.length === 0) throw new Error('Use a moveTo() command first'); return segments[segments.length - 1]; } return { moveTo: function() { - if (this._segments.length === 1) + var segments = this._segments; + if (segments.length === 1) this.removeSegment(0); - if (!this._segments.length) + if (!segments.length) this._add([ new Segment(Point.read(arguments)) ]); }, @@ -7216,37 +7416,86 @@ var Path = PathItem.extend({ arcTo: function() { var current = getCurrentSegment(this), from = current._point, - through, to = Point.read(arguments), - clockwise = Base.pick(Base.peek(arguments), true); + 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 { + } else if (Base.remain(arguments) <= 2) { through = to; to = Point.read(arguments); - } - 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), - center = l1.intersect(l2, true), - line = new Line(from, to), - throughSide = line.getSide(through); - if (!center) { - if (!throughSide) + } else { + var radius = Size.read(arguments); + if (radius.isZero()) return this.lineTo(to); - throw new Error('Cannot put an arc through the given points: ' - + [from, through, 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, + EPSILON = 1e-11, + 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) < EPSILON) + 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; } - var vector = from.subtract(center), - extent = vector.getDirectedAngle(to.subtract(center)), - centerSide = line.getSide(center); - if (centerSide == 0) { - extent = throughSide * Math.abs(extent); - } else if (throughSide == centerSide) { - extent -= 360 * (extent < 0 ? -1 : 1); + 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), @@ -7255,13 +7504,27 @@ var Path = PathItem.extend({ z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), segments = []; for (var i = 0; i <= count; i++) { - var pt = i < count ? center.add(vector) : to; - var out = i < count ? vector.rotate(90).multiply(z) : null; - if (i == 0) { + 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 { - segments.push( - new Segment(pt, vector.rotate(-90).multiply(z), out)); + 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); } @@ -7312,7 +7575,7 @@ var Path = PathItem.extend({ closePath: function() { var first = this.getFirstSegment(), last = this.getLastSegment(); - if (first._point.equals(last._point)) { + if (first !== last && first._point.equals(last._point)) { first.setHandleIn(last._handleIn); last.remove(); } @@ -7371,29 +7634,11 @@ statics: { }, getStrokeBounds: function(segments, closed, style, matrix) { - function getPenPadding(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)]; - } - if (!style.hasStroke()) return Path.getBounds(segments, closed, style, matrix); var length = segments.length - (closed ? 0 : 1), radius = style.getStrokeWidth() / 2, - padding = getPenPadding(radius, matrix), + padding = Path._getPenPadding(radius, matrix), bounds = Path.getBounds(segments, closed, style, matrix, padding), join = style.getStrokeJoin(), cap = style.getStrokeCap(), @@ -7405,25 +7650,27 @@ statics: { ? matrix._transformPoint(point, point) : point); } + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + function addJoin(segment, join) { - if (join === 'round' || !segment._handleIn.isZero() - && !segment._handleOut.isZero()) { - bounds = bounds.unite(joinBounds.setCenter(matrix - ? matrix._transformPoint(segment._point) : segment._point)); + var handleIn = segment._handleIn, + handleOut = segment._handleOut + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isColinear(handleOut)) { + addRound(segment); } else { - Path._addSquareJoin(segment, join, radius, miterLimit, add); + Path._addBevelJoin(segment, join, radius, miterLimit, add); } } function addCap(segment, cap) { - switch (cap) { - case 'round': - addJoin(segment, cap); - break; - case 'butt': - case 'square': + if (cap === 'round') { + addRound(segment); + } else { Path._addSquareCap(segment, cap, radius, add); - break; } } @@ -7431,14 +7678,32 @@ statics: { addJoin(segments[i], join); if (closed) { addJoin(segments[0], join); - } else { + } else if (length > 0) { addCap(segments[0], cap); addCap(segments[segments.length - 1], cap); } return bounds; }, - _addSquareJoin: function(segment, join, radius, miterLimit, addPoint, area) { + _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), @@ -7491,19 +7756,19 @@ statics: { x2 = -x1, y1 = x1, y2 = x2; - strokePadding = strokePadding / 2 || 0; - joinPadding = joinPadding / 2 || 0; 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 - padding, - xx = x + padding, - yn = y - padding, - yx = y + padding; + 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; @@ -7514,24 +7779,23 @@ statics: { }, getRoughBounds: function(segments, closed, style, matrix) { - var strokeWidth = style.getStrokeColor() ? style.getStrokeWidth() : 0, - joinWidth = strokeWidth; - if (strokeWidth === 0) { - strokeWidth = 0.00001; - } else { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { if (style.getStrokeJoin() === 'miter') - joinWidth = strokeWidth * style.getMiterLimit(); + joinRadius = strokeRadius * style.getMiterLimit(); if (style.getStrokeCap() === 'square') - joinWidth = Math.max(joinWidth, strokeWidth * Math.sqrt(2)); + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); } return Path.getHandleBounds(segments, closed, style, matrix, - strokeWidth, joinWidth); + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); } }}); Path.inject({ statics: new function() { - var kappa = Numerical.KAPPA, + var kappa = 0.5522847498307936, ellipseSegments = [ new Segment([-1, 0], [0, kappa ], [0, -kappa]), new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), @@ -7539,9 +7803,16 @@ Path.inject({ statics: new function() { 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 path = new Path(), - segments = new Array(4); + var segments = new Array(4); for (var i = 0; i < 4; i++) { var segment = ellipseSegments[i]; segments[i] = new Segment( @@ -7550,17 +7821,15 @@ Path.inject({ statics: new function() { segment._handleOut.multiply(radius) ); } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(args)); + return createPath(segments, true, args); } return { Line: function() { - return new Path( - Point.readNamed(arguments, 'from'), - Point.readNamed(arguments, 'to') - ).set(Base.getNamed(arguments)); + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); }, Circle: function() { @@ -7571,27 +7840,27 @@ Path.inject({ statics: new function() { Rectangle: function() { var rect = Rectangle.readNamed(arguments, 'rectangle'), - radius = Size.readNamed(arguments, 'radius', 0, 0, + radius = Size.readNamed(arguments, 'radius', 0, { readNull: true }), bl = rect.getBottomLeft(true), tl = rect.getTopLeft(true), tr = rect.getTopRight(true), - br = rect.getBottomRight(true); - path = new Path(); + br = rect.getBottomRight(true), + segments; if (!radius || radius.isZero()) { - path._add([ + 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; - path._add([ + 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]), @@ -7600,10 +7869,9 @@ Path.inject({ statics: new function() { 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]) - ]); + ]; } - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, RoundRectangle: '#Rectangle', @@ -7619,29 +7887,27 @@ Path.inject({ statics: new function() { var from = Point.readNamed(arguments, 'from'), through = Point.readNamed(arguments, 'through'), to = Point.readNamed(arguments, 'to'), - path = new Path(); + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); path.moveTo(from); path.arcTo(through, to); - return path.set(Base.getNamed(arguments)); + return path.set(props); }, RegularPolygon: function() { var center = Point.readNamed(arguments, 'center'), sides = Base.readNamed(arguments, 'sides'), radius = Base.readNamed(arguments, 'radius'), - path = new Path(), 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++) { + for (var i = 0; i < sides; i++) segments[i] = new Segment(center.add( vector.rotate((i + offset) * step))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, Star: function() { @@ -7649,17 +7915,13 @@ Path.inject({ statics: new function() { points = Base.readNamed(arguments, 'points') * 2, radius1 = Base.readNamed(arguments, 'radius1'), radius2 = Base.readNamed(arguments, 'radius2'), - path = new Path(), 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))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + 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); } }; }}); @@ -7673,8 +7935,19 @@ var CompoundPath = PathItem.extend({ initialize: function CompoundPath(arg) { this._children = []; this._namedChildren = {}; - if (!this._initialize(arg)) - this.addChildren(Array.isArray(arg) ? arg : arguments); + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & (2 | 4)) + this._currentPath = undefined; }, insertChildren: function insertChildren(index, items, _preserve) { @@ -7722,7 +7995,7 @@ var CompoundPath = PathItem.extend({ var children = this._children, curves = []; for (var i = 0, l = children.length; i < l; i++) - curves = curves.concat(children[i].getCurves()); + curves.push.apply(curves, children[i].getCurves()); return curves; }, @@ -7744,36 +8017,18 @@ var CompoundPath = PathItem.extend({ return area; }, - getPathData: function() { + getPathData: function(precision) { var children = this._children, paths = []; for (var i = 0, l = children.length; i < l; i++) - paths.push(children[i].getPathData(arguments[0])); + paths.push(children[i].getPathData(precision)); return paths.join(' '); }, - _getWinding: function(point) { - var children = this._children, - winding = 0; - for (var i = 0, l = children.length; i < l; i++) - winding += children[i]._getWinding(point); - return winding; - }, - - _hitTest : function _hitTest(point, options) { - var res = _hitTest.base.call(this, point, - new Base(options, { fill: false })); - if (!res) { - if (options.compoundChildren) { - var children = this._children; - for (var i = children.length - 1; i >= 0 && !res; i--) - res = children[i]._hitTest(point, options); - } else if (options.fill && this.hasFill() - && this._contains(point)) { - res = new HitResult('fill', this); - } - } - return res; + _getChildHitTestOptions: function(options) { + return options.type === 'path' + ? options + : new Base(options, { fill: false }); }, _draw: function(ctx, param) { @@ -7781,10 +8036,15 @@ var CompoundPath = PathItem.extend({ if (children.length === 0) return; - ctx.beginPath(); - param = param.extend({ compound: true }); - for (var i = 0, l = children.length; i < l; i++) - children[i].draw(ctx, param); + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + ctx.beginPath(); + param = param.extend({ compound: true }); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param); + this._currentPath = ctx.currentPath; + } if (!param.clip) { this._setStyles(ctx); @@ -7798,26 +8058,31 @@ var CompoundPath = PathItem.extend({ } } }, new function() { - function getCurrentPath(that) { - if (!that._children.length) + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) throw new Error('Use a moveTo() command first'); - return that._children[that._children.length - 1]; + return children[children.length - 1]; } var fields = { moveTo: function() { - var path = new Path(); - this.addChild(path); + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current : new Path(); + if (path !== current) + this.addChild(path); path.moveTo.apply(path, arguments); }, moveBy: function() { - this.moveTo(getCurrentPath(this).getLastSegment()._point.add( - Point.read(arguments))); + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); }, closePath: function() { - getCurrentPath(this).closePath(); + getCurrentPath(this, true).closePath(); } }; @@ -7825,7 +8090,7 @@ var CompoundPath = PathItem.extend({ 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], function(key) { fields[key] = function() { - var path = getCurrentPath(this); + var path = getCurrentPath(this, true); path[key].apply(path, arguments); }; } @@ -7834,6 +8099,433 @@ var CompoundPath = PathItem.extend({ return fields; }); +PathItem.inject(new function() { + function reorientPath(path) { + path = path.clone(false).reduce().transform(null, true); + if (path instanceof CompoundPath) { + var children = path.removeChildren(), + length = children.length, + bounds = new Array(length), + counters = new Array(length), + clockwise; + children.sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + path.addChildren(children); + clockwise = children[0].isClockwise(); + for (var i = 0; i < length; i++) { + bounds[i] = children[i].getBounds(); + counters[i] = 0; + } + for (var i = 0; i < length; i++) { + for (var j = 1; j < length; j++) { + if (i !== j && bounds[i].intersects(bounds[j])) + counters[j]++; + } + if (i > 0 && counters[i] % 2 === 0) + children[i].setClockwise(clockwise); + } + } + return path; + } + + function computeBoolean(path1, path2, operator, subtract) { + var _path1 = reorientPath(path1); + _path2 = path2 && path1 !== path2 && reorientPath(path2); + if (!_path1.isClockwise()) + _path1.reverse(); + if (_path2 && !(subtract ^ _path2.isClockwise())) + _path2.reverse(); + splitPath(_path1.getIntersections(_path2, true)); + + var chain = [], + windings = [], + lengths = [], + 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]); + 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 = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding != null) + continue; + chain.length = windings.length = lengths.length = 0; + var totalLength = 0, + startSeg = segment; + do { + chain.push(segment); + lengths.push(totalLength += segment.getCurve().getLength()); + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== startSeg); + for (var j = 0; j < 3; j++) { + var length = totalLength * Math.random(), + amount = lengths.length; + k = 0; + do { + if (lengths[k] >= length) { + if (k > 0) + length -= lengths[k - 1]; + break; + } + } while (++k < amount); + var curve = chain[k].getCurve(), + point = curve.getPointAt(length), + hor = curve.isHorizontal(), + path = curve._path; + if (path._parent instanceof CompoundPath) + path = path._parent; + windings[j] = subtract && _path2 + && (path === _path1 && _path2._getWinding(point, hor) + || path === _path2 && !_path1._getWinding(point, hor)) + ? 0 + : getWinding(point, monoCurves, hor); + } + windings.sort(); + var winding = windings[1]; + for (var j = chain.length - 1; j >= 0; j--) + chain[j]._winding = winding; + } + var result = new CompoundPath(); + result.addChildren(tracePaths(segments, operator), true); + _path1.remove(); + if (_path2) + _path2.remove(); + return result.reduce(); + } + + function splitPath(intersections) { + var TOLERANCE = 0.00001, + linearSegments; + + function resetLinear() { + for (var i = 0, l = linearSegments.length; i < l; i++) { + var segment = linearSegments[i]; + segment._handleOut.set(0, 0); + segment._handleIn.set(0, 0); + } + } + + for (var i = intersections.length - 1, curve, prevLoc; i >= 0; i--) { + var loc = intersections[i], + t = loc._parameter; + if (prevLoc && prevLoc._curve === loc._curve + && prevLoc._parameter > 0) { + t /= prevLoc._parameter; + } else { + if (linearSegments) + resetLinear(); + curve = loc._curve; + linearSegments = curve.isLinear() && []; + } + var newCurve, + segment; + if (newCurve = curve.divide(t, true, true)) { + segment = newCurve._segment1; + curve = newCurve.getPrevious(); + } else { + segment = t < TOLERANCE + ? curve._segment1 + : t > 1 - TOLERANCE + ? curve._segment2 + : curve.getPartLength(0, t) < curve.getPartLength(t, 1) + ? curve._segment1 + : curve._segment2; + } + segment._intersection = loc.getIntersection(); + loc._segment = segment; + if (linearSegments) + linearSegments.push(segment); + prevLoc = loc; + } + if (linearSegments) + resetLinear(); + } + + function getWinding(point, curves, horizontal, testContains) { + var TOLERANCE = 0.00001, + x = point.x, + y = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs, + MAX = 1 - TOLERANCE; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = y - TOLERANCE, + yAfter = y + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, x, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y0 = Curve.evaluate(values, roots[j], 0).y; + if (y0 < yBefore && y0 > yTop) { + yTop = y0; + } else if (y0 > yAfter && y0 < yBottom) { + yBottom = y0; + } + } + } + } + yTop = (yTop + y) / 2; + yBottom = (yBottom + y) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(x, yTop), curves); + if (yBottom < Infinity) + windRight = getWinding(new Point(x, yBottom), curves); + } else { + var xBefore = x - TOLERANCE, + xAfter = x + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding, + next = curve.next; + if (winding && (winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, + !next.winding && next.values[1] === y ? 1 : MAX) === 1){ + var t = roots[0], + x0 = Curve.evaluate(values, t, 0).x, + slope = Curve.evaluate(values, t, 1).y; + if (abs(slope) < TOLERANCE && !Curve.isLinear(values) + || t < TOLERANCE && slope * Curve.evaluate( + curve.previous.values, t, 1).y < 0) { + if (testContains && x0 >= xBefore && x0 <= xAfter) { + ++windLeft; + ++windRight; + } + } else if (x0 <= xBefore) { + windLeft += winding; + } else if (x0 >= xAfter) { + windRight += winding; + } + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function tracePaths(segments, operator, selfOp) { + operator = operator || function() { + return true; + }; + var paths = [], + ZERO = 1e-3, + ONE = 1 - 1e-3; + for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) { + seg = startSeg = segments[i]; + if (seg._visited || !operator(seg._winding)) + 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) { + var c1 = seg.getCurve(); + if (dir > 0) + c1 = c1.getPrevious(); + var t1 = c1.getTangentAt(dir < 1 ? ZERO : ONE, true), + c4 = interSeg.getCurve(), + c3 = c4.getPrevious(), + t3 = c3.getTangentAt(ONE, true), + t4 = c4.getTangentAt(ZERO, true), + w3 = t1.cross(t3), + w4 = t1.cross(t4); + if (selfOp) { + seg._visited = interSeg._visited; + seg = interSeg; + dir = 1; + } else 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; + } + handleOut = dir > 0 ? seg._handleOut : seg._handleIn; + } + path.add(new Segment(seg._point, added && handleIn, handleOut)); + added = true; + 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); + } + if (path._segments.length > + (path._closed ? path.isPolygon() ? 2 : 0 : 1)) + paths.push(path); + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1 || w === 0; + }, false); + }, + + intersect: function(path) { + return computeBoolean(this, path, function(w) { + return w === 2; + }, false); + }, + + subtract: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1; + }, true); + }, + + exclude: function(path) { + return new Group([this.subtract(path), path.subtract(this)]); + }, + divide: function(path) { + return new Group([this.subtract(path), this.intersect(path)]); + } + }; +}); + +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.isLinear(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + TOLERANCE = 0.00001, + roots = []; + var count = Numerical.solveQuadratic(a, b, c, roots, TOLERANCE, + 1 - TOLERANCE); + if (count === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (count > 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]); + } + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + return monoCurves; + } +}); + +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; + } +}); + var PathFlattener = Base.extend({ initialize: function(path) { this.curves = []; @@ -8117,186 +8809,11 @@ var PathFitter = Base.extend({ } }); -PathItem.inject(new function() { - - function splitPath(intersections, collectOthers) { - intersections.sort(function(loc1, loc2) { - var path1 = loc1.getPath(), - path2 = loc2.getPath(); - return path1 === path2 - ? (loc1.getIndex() + loc1.getParameter()) - - (loc2.getIndex() + loc2.getParameter()) - : path1._id - path2._id; - }); - var others = collectOthers && []; - for (var i = intersections.length - 1; i >= 0; i--) { - var loc = intersections[i], - other = loc.getIntersection(), - curve = loc.divide(), - segment = curve && curve.getSegment1() || loc.getSegment(); - if (others) - others.push(other); - segment._intersection = other; - loc._segment = segment; - } - return others; - } - - function reorientPath(path) { - if (path instanceof CompoundPath) { - var children = path.removeChildren(), - length = children.length, - bounds = new Array(length), - counters = new Array(length), - clockwise; - children.sort(function(a, b){ - var b1 = a.getBounds(), b2 = b.getBounds(); - return b1._width * b1._height < b2._width * b2._height; - }); - path.addChildren(children); - clockwise = children[0].isClockwise(); - for (var i = 0; i < length; i++) { - bounds[i] = children[i].getBounds(); - counters[i] = 0; - } - for (var i = 0; i < length; i++) { - for (var j = 1; j < length; j++) { - if (i !== j && bounds[i].contains(bounds[j])) - counters[j]++; - } - if (i > 0 && counters[i] % 2 === 0) - children[i].setClockwise(clockwise); - } - } - return path; - } - - function computeBoolean(path1, path2, operator, subtract) { - path1 = reorientPath(path1.clone(false)); - path2 = reorientPath(path2.clone(false)); - var path1Clockwise = path1.isClockwise(), - path2Clockwise = path2.isClockwise(), - intersections = path1.getIntersections(path2); - splitPath(splitPath(intersections, true)); - if (!path1Clockwise) - path1.reverse(); - if (!(subtract ^ path2Clockwise)) - path2.reverse(); - path1Clockwise = true; - path2Clockwise = !subtract; - var paths = [] - .concat(path1._children || [path1]) - .concat(path2._children || [path2]), - segments = [], - result = new CompoundPath(); - for (var i = 0, l = paths.length; i < l; i++) { - var path = paths[i], - parent = path._parent, - clockwise = path.isClockwise(), - segs = path._segments; - path = parent instanceof CompoundPath ? parent : path; - for (var j = segs.length - 1; j >= 0; j--) { - var segment = segs[j], - midPoint = segment.getCurve().getPoint(0.5), - insidePath1 = path !== path1 && path1.contains(midPoint) - && (clockwise === path1Clockwise || subtract - || !testOnCurve(path1, midPoint)), - insidePath2 = path !== path2 && path2.contains(midPoint) - && (clockwise === path2Clockwise - || !testOnCurve(path2, midPoint)); - if (operator(path === path1, insidePath1, insidePath2)) { - segment._invalid = true; - } else { - segments.push(segment); - } - } - } - for (var i = 0, l = segments.length; i < l; i++) { - var segment = segments[i]; - if (segment._visited) - continue; - var path = new Path(), - loc = segment._intersection, - intersection = loc && loc.getSegment(true); - if (segment.getPrevious()._invalid) - segment.setHandleIn(intersection - ? intersection._handleIn - : new Point(0, 0)); - do { - segment._visited = true; - if (segment._invalid && segment._intersection) { - var inter = segment._intersection.getSegment(true); - path.add(new Segment(segment._point, segment._handleIn, - inter._handleOut)); - inter._visited = true; - segment = inter; - } else { - path.add(segment.clone()); - } - segment = segment.getNext(); - } while (segment && !segment._visited && segment !== intersection); - var amount = path._segments.length; - if (amount > 1 && (amount > 2 || !path.isPolygon())) { - path.setClosed(true); - result.addChild(path, true); - } else { - path.remove(); - } - } - path1.remove(); - path2.remove(); - return result.reduce(); - } - - function testOnCurve(path, point) { - var curves = path.getCurves(), - bounds = path.getBounds(); - if (bounds.contains(point)) { - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i]; - if (curve.getBounds().contains(point) - && curve.getParameterOf(point)) - return true; - } - } - return false; - } - - return { - unite: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isInPath1 || isInPath2; - }); - }, - - intersect: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return !(isInPath1 || isInPath2); - }); - }, - - subtract: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isPath1 && isInPath2 || !isPath1 && !isInPath1; - }, true); - }, - - exclude: function(path) { - return new Group([this.subtract(path), path.subtract(this)]); - }, - - divide: function(path) { - return new Group([this.subtract(path), this.intersect(path)]); - } - }; -}); - var TextItem = Item.extend({ _class: 'TextItem', _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, _serializeFields: { content: null }, @@ -8348,7 +8865,7 @@ var PointText = TextItem.extend({ }, clone: function(insert) { - return this._clone(new PointText({ insert: false }), insert); + return this._clone(new PointText(Item.NO_INSERT), insert); }, getPoint: function() { @@ -8356,8 +8873,8 @@ var PointText = TextItem.extend({ return new LinkedPoint(point.x, point.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.translate(point.subtract(this._matrix.getTranslation())); }, @@ -8438,7 +8955,7 @@ var Color = Base.extend(new function() { } else if (match = string.match(/^rgba?\((.*)\)$/)) { components = match[1].split(','); for (var i = 0, l = components.length; i < l; i++) { - var value = parseFloat(components[i]); + var value = +components[i]; components[i] = i < 3 ? value / 255 : value; } } else { @@ -8485,8 +9002,8 @@ var Color = Base.extend(new function() { }, 'hsb-rgb': function(h, s, b) { - var h = (h / 60) % 6, - i = Math.floor(h), + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), f = h - i, i = hsbIndices[i], v = [ @@ -8515,7 +9032,7 @@ var Color = Base.extend(new function() { }, 'hsl-rgb': function(h, s, l) { - h /= 360; + h = (((h / 360) % 1) + 1) % 1; if (s === 0) return [l, l, l]; var t3s = [ h + 1 / 3, h, h - 1 / 3 ], @@ -8571,9 +9088,8 @@ var Color = Base.extend(new function() { parser = componentParsers[type][index] = name === 'gradient' ? function(value) { var current = this._components[0]; - value = Gradient.read( - Array.isArray(value) ? value : arguments, - 0, 0, { readNull: true }); + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); if (current !== value) { if (current) current._removeOwner(this); @@ -8582,22 +9098,16 @@ var Color = Base.extend(new function() { } return value; } - : name === 'hue' - ? function(value) { - return isNaN(value) ? 0 - : ((value % 360) + 360) % 360; + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); } - : type === 'gradient' - ? function() { - return Point.read(arguments, 0, 0, { - readNull: name === 'highlight', - clone: true - }); - } - : function(value) { - return isNaN(value) ? 0 - : Math.min(Math.max(value, 0), 1); - }; + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; this['get' + part] = function() { return this._type === type @@ -8628,7 +9138,6 @@ var Color = Base.extend(new function() { var slice = Array.prototype.slice, args = arguments, read = 0, - parse = true, type, components, alpha, @@ -8652,7 +9161,6 @@ var Color = Base.extend(new function() { } } if (!components) { - parse = !(this.__options && this.__options.dontParse); values = argType === 'number' ? args : argType === 'object' && arg.length != null @@ -8705,7 +9213,7 @@ var Color = Base.extend(new function() { ? 'gray' : 'rgb'; var properties = types[type]; - parsers = parse && componentParsers[type]; + parsers = componentParsers[type]; this._components = components = []; for (var i = 0, l = properties.length; i < l; i++) { var value = arg[properties[i]]; @@ -8716,8 +9224,7 @@ var Color = Base.extend(new function() { radial: arg.radial }; } - if (parse) - value = parsers[i].call(this, value); + value = parsers[i].call(this, value); if (value != null) components[i] = value; } @@ -8734,9 +9241,7 @@ var Color = Base.extend(new function() { this._components = components = []; var parsers = componentParsers[this._type]; for (var i = 0, l = parsers.length; i < l; i++) { - var value = values && values[i]; - if (parse) - value = parsers[i].call(this, value); + var value = parsers[i].call(this, values && values[i]); if (value != null) components[i] = value; } @@ -8771,7 +9276,7 @@ var Color = Base.extend(new function() { ? converter.apply(this, this._components) : converters['rgb-' + type].apply(this, converters[this._type + '-rgb'].apply(this, - this._components)); + this._components)); }, convert: function(type) { @@ -8809,12 +9314,13 @@ var Color = Base.extend(new function() { }, equals: function(color) { - if (Base.isPlainValue(color)) - color = Color.read(arguments); - return color === this || color && this._class === color._class - && this._type === color._type - && this._alpha === color._alpha - && Base.equals(this._components, color._components) + var col = Base.isPlainValue(color) + ? 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; }, @@ -8837,13 +9343,16 @@ var Color = Base.extend(new function() { 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 = [ - Math.round(components[0] * 255), - Math.round(components[1] * 255), - Math.round(components[2] * 255) + convert(components[0]), + convert(components[1]), + convert(components[2]) ]; if (alpha < 1) - components.push(alpha); + components.push(alpha < 0 ? 0 : alpha); return hex ? '#' + ((1 << 24) + (components[0] << 16) + (components[1] << 8) @@ -8907,46 +9416,32 @@ var Color = Base.extend(new function() { } }); }, new function() { - function clamp(value, hue) { - return value < 0 - ? 0 - : hue && value > 360 - ? 360 - : !hue && value > 1 - ? 1 - : value; - } - var operators = { - add: function(a, b, hue) { - return clamp(a + b, hue); + add: function(a, b) { + return a + b; }, - subtract: function(a, b, hue) { - return clamp(a - b, hue); + subtract: function(a, b) { + return a - b; }, - multiply: function(a, b, hue) { - return clamp(a * b, hue); + multiply: function(a, b) { + return a * b; }, - divide: function(a, b, hue) { - return clamp(a / b, hue); + divide: function(a, b) { + return a / b; } }; return Base.each(operators, function(operator, name) { - var options = { dontParse: /^(multiply|divide)$/.test(name) }; - this[name] = function(color) { - color = Color.read(arguments, 0, 0, options); + color = Color.read(arguments); var type = this._type, - properties = this._properties, components1 = this._components, components2 = color._convert(type); for (var i = 0, l = components1.length; i < l; i++) - components2[i] = operator(components1[i], components2[i], - properties[i] === 'hue'); + components2[i] = operator(components1[i], components2[i]); return new Color(type, components2, this._alpha != null ? operator(this._alpha, color.getAlpha()) @@ -9011,7 +9506,7 @@ var Gradient = Base.extend({ if (index != -1) { this._owners.splice(index, 1); if (this._owners.length === 0) - delete this._owners; + this._owners = undefined; } }, @@ -9029,7 +9524,7 @@ var Gradient = Base.extend({ setStops: function(stops) { if (this.stops) { for (var i = 0, l = this._stops.length; i < l; i++) - delete this._stops[i]._owner; + this._stops[i]._owner = undefined; } if (stops.length < 2) throw new Error( @@ -9148,8 +9643,10 @@ var Style = Base.extend(new function() { shadowBlur: 0, shadowOffset: new Point(), selectedColor: undefined, - font: 'sans-serif', + fontFamily: 'sans-serif', + fontWeight: 'normal', fontSize: 12, + font: 'sans-serif', leading: null, justification: 'left' }; @@ -9159,8 +9656,10 @@ var Style = Base.extend(new function() { strokeCap: 25, strokeJoin: 25, miterLimit: 25, - font: 5, + fontFamily: 5, + fontWeight: 5, fontSize: 5, + font: 5, leading: 5, justification: 5 }; @@ -9191,7 +9690,7 @@ var Style = Base.extend(new function() { if (old != value) { if (isColor) { if (old) - delete old._owner; + old._owner = undefined; if (value && value.constructor === Color) { if (value._owner) value = value.clone(); @@ -9205,11 +9704,11 @@ var Style = Base.extend(new function() { } }; - fields[get] = function() { + fields[get] = function(_dontMerge) { var value, children = this._item && this._item._children; - if (!children || children.length === 0 || arguments[0] - || this._item._type === 'compound-path') { + if (!children || children.length === 0 || _dontMerge + || this._item instanceof CompoundPath) { var value = this._values[key]; if (value === undefined) { value = this._defaults[key]; @@ -9217,8 +9716,8 @@ var Style = Base.extend(new function() { value = value.clone(); this._values[key] = value; } else if (isColor && !(value && value.constructor === Color)) { - this._values[key] = value = Color.read( - [value], 0, 0, { readNull: true, clone: true }); + this._values[key] = value = Color.read([value], 0, + { readNull: true, clone: true }); if (value) value._owner = this._item; } @@ -9290,23 +9789,28 @@ var Style = Base.extend(new function() { return !!this.getShadowColor() && this.getShadowBlur() > 0; }, + getFontStyle: function() { + var size = this.getFontSize(); + return this.getFontWeight() + + ' ' + size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + + setFont: '#setFontFamily', + getLeading: function getLeading() { var leading = getLeading.base.call(this); return leading != null ? leading : this.getFontSize() * 1.2; - }, - - getFontStyle: function() { - var size = this.getFontSize(); - return size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + this.getFont(); } }); var jsdom = require('jsdom'), domToHtml = require('jsdom/lib/jsdom/browser/domtohtml').domToHtml, - Canvas = require('canvas'); - -var document = jsdom.jsdom(''), + Canvas = require('canvas'), + document = jsdom.jsdom(''), window = document.createWindow(), navigator = window.navigator, HTMLCanvasElement = Canvas, @@ -9364,6 +9868,23 @@ var DomElement = new function() { return res; } + 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 { create: function(nodes, parent) { var isArray = Array.isArray(nodes), @@ -9505,13 +10026,17 @@ var DomElement = new function() { this.getBounds(el, true)); }, - getPrefixValue: function(el, name) { - var value = el[name], - prefixes = ['webkit', 'moz', 'ms', 'o'], - suffix = name[0].toUpperCase() + name.substring(1); - for (var i = 0; i < 4 && value == null; i++) - value = el[prefixes[i] + suffix]; - return value; + 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); + } } }; }; @@ -9524,6 +10049,8 @@ var View = Base.extend(Callback, { this._project = paper.project; this._element = element; var size; + if (!this._pixelRatio) + this._pixelRatio = 1; this._id = 'view-' + View._id++; size = new Size(element.width, element.height); View._views.push(this); @@ -9540,7 +10067,7 @@ var View = Base.extend(Callback, { remove: function() { if (!this._project) return false; - if (View._focused == this) + if (View._focused === this) View._focused = null; View._views.splice(View._views.indexOf(this), 1); delete View._viewsById[this._id]; @@ -9548,6 +10075,7 @@ var View = Base.extend(Callback, { this._project.view = null; this._element = this._project = null; this.detach('frame'); + this._animate = false; this._frameItems = {}; return true; }, @@ -9555,10 +10083,11 @@ var View = Base.extend(Callback, { _events: { onFrame: { install: function() { + this.play(); }, uninstall: function() { - this._animate = false; + this.pause(); } }, @@ -9586,7 +10115,7 @@ var View = Base.extend(Callback, { if (this._stats) this._stats.update(); this._handlingFrame = false; - this.draw(true); + this.update(); }, _animateItem: function(item, animate) { @@ -9617,40 +10146,48 @@ var View = Base.extend(Callback, { } }, - _redraw: function() { - this._project._needsRedraw = true; + _update: function() { + this._project._needsUpdate = true; if (this._handlingFrame) return; if (this._animate) { this._handleFrame(); } else { - this.draw(); + this.update(); } }, _changed: function(flags) { if (flags & 1) - this._project._needsRedraw = true; + this._project._needsUpdate = true; }, _transform: function(matrix) { this._matrix.concatenate(matrix); this._bounds = null; - this._redraw(); + 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(size) { - size = Size.read(arguments); - var delta = size.subtract(this._viewSize); + setViewSize: function() { + var size = Size.read(arguments), + delta = size.subtract(this._viewSize); if (delta.isZero()) return; this._viewSize.set(size.width, size.height); @@ -9660,7 +10197,7 @@ var View = Base.extend(Callback, { size: size, delta: delta }); - this._redraw(); + this._update(); }, _setViewSize: function(size) { @@ -9677,11 +10214,11 @@ var View = Base.extend(Callback, { }, getSize: function() { - return this.getBounds().getSize(arguments[0]); + return this.getBounds().getSize(); }, getCenter: function() { - return this.getBounds().getCenter(arguments[0]); + return this.getBounds().getCenter(); }, setCenter: function(center) { @@ -9707,13 +10244,25 @@ var View = Base.extend(Callback, { this._transform(new Matrix().translate(Point.read(arguments).negate())); }, + play: function() { + this._animate = true; + }, + + 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: { @@ -9742,33 +10291,33 @@ var CanvasView = View.extend({ } this._context = canvas.getContext('2d'); this._eventCounters = {}; - this._ratio = 1; + this._pixelRatio = 1; View.call(this, canvas); }, _setViewSize: function(size) { var width = size.width, height = size.height, - ratio = this._ratio, + pixelRatio = this._pixelRatio, element = this._element, style = element.style; - element.width = width * ratio; - element.height = height * ratio; - if (ratio !== 1) { + element.width = width * pixelRatio; + element.height = height * pixelRatio; + if (pixelRatio !== 1) { style.width = width + 'px'; style.height = height + 'px'; - this._context.scale(ratio, ratio); + this._context.scale(pixelRatio, pixelRatio); } }, - draw: function(checkRedraw) { - if (checkRedraw && !this._project._needsRedraw) + update: function() { + if (!this._project._needsUpdate) return false; var ctx = this._context, size = this._viewSize; ctx.clearRect(0, 0, size.width + 1, size.height + 1); - this._project.draw(ctx, this._matrix, this._ratio); - this._project._needsRedraw = false; + this._project.draw(ctx, this._matrix, this._pixelRatio); + this._project._needsUpdate = false; return true; } }, new function() { @@ -9779,86 +10328,98 @@ var CanvasView = View.extend({ downItem, lastItem, overItem, - hasDrag, - doubleClick, + dragItem, + dblClick, clickTime; - function callEvent(type, event, point, target, lastPoint, bubble) { + function callEvent(view, type, event, point, target, lastPoint) { var item = target, mouseEvent; - while (item) { - if (item.responds(type)) { - if (!mouseEvent) + + function call(obj) { + if (obj.responds(type)) { + if (!mouseEvent) { mouseEvent = new MouseEvent(type, event, point, target, lastPoint ? point.subtract(lastPoint) : null); - if (item.fire(type, mouseEvent) - && (!bubble || mouseEvent._stopped)) - return false; + } + if (obj.fire(type, mouseEvent) && mouseEvent.isStopped) { + event.preventDefault(); + return true; + } } + } + + while (item) { + if (call(item)) + return true; item = item.getParent(); } - return true; - } - - function handleEvent(view, type, event, point, lastPoint) { - if (view._eventCounters[type]) { - var project = view._project, - hit = project.hitTest(point, { - tolerance: project.options.hitTolerance || 0, - fill: true, - stroke: true - }), - item = hit && hit.item; - if (item) { - if (type === 'mousemove' && item != overItem) - lastPoint = point; - if (type !== 'mousemove' || !hasDrag) - callEvent(type, event, point, item, lastPoint); - return item; - } - } + if (call(view)) + return true; + return false; } return { - _onMouseDown: function(event, point) { - var item = handleEvent(this, 'mousedown', event, point); - doubleClick = lastItem == item && (Date.now() - clickTime < 300); - downItem = lastItem = item; - downPoint = lastPoint = overPoint = point; - hasDrag = downItem && downItem.responds('mousedrag'); - }, - - _onMouseUp: function(event, point) { - var item = handleEvent(this, 'mouseup', event, point); - if (hasDrag) { - if (lastPoint && !lastPoint.equals(point)) - callEvent('mousedrag', event, point, downItem, lastPoint); - if (item != downItem) { - overPoint = point; - callEvent('mousemove', event, point, item, overPoint); + _handleEvent: function(type, point, event) { + if (!this._eventCounters[type]) + return; + var project = this._project, + hit = project.hitTest(point, { + tolerance: this._scope.settings.hitTolerance, + 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; } - if (item === downItem) { - clickTime = Date.now(); - if (!doubleClick - || callEvent('doubleclick', event, downPoint, item)) - callEvent('click', event, downPoint, item); - doubleClick = false; - } - downItem = null; - hasDrag = false; - }, - - _onMouseMove: function(event, point) { - if (downItem) - callEvent('mousedrag', event, point, downItem, lastPoint); - var item = handleEvent(this, 'mousemove', event, point, overPoint); - lastPoint = overPoint = point; - if (item !== overItem) { - callEvent('mouseleave', event, point, overItem); - overItem = item; - callEvent('mouseenter', event, point, item); - } + return stopped; } }; }); @@ -9941,19 +10502,19 @@ CanvasView.inject(new function() { var CanvasProvider = { canvases: [], - getCanvas: function(width, height, ratio) { + getCanvas: function(width, height, pixelRatio) { var canvas, init = true; if (typeof width === 'object') { - ratio = height; + pixelRatio = height; height = width.height; width = width.width; } - if (!ratio) { - ratio = 1; - } else if (ratio !== 1) { - width *= ratio; - height *= ratio; + if (!pixelRatio) { + pixelRatio = 1; + } else if (pixelRatio !== 1) { + width *= pixelRatio; + height *= pixelRatio; } if (this.canvases.length) { canvas = this.canvases.pop(); @@ -9970,8 +10531,8 @@ var CanvasProvider = { canvas.height = height; } ctx.save(); - if (ratio !== 1) - ctx.scale(ratio, ratio); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); return canvas; }, @@ -10241,7 +10802,8 @@ var SVGStyles = Base.each({ miterLimit: ['stroke-miterlimit', 'number'], dashArray: ['stroke-dasharray', 'array'], dashOffset: ['stroke-dashoffset', 'number'], - font: ['font-family', 'string'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], fontSize: ['font-size', 'number'], justification: ['text-anchor', 'lookup', { left: 'start', @@ -10305,22 +10867,23 @@ new function() { attrs[center ? 'cy' : 'y'] = point.y; trans = null; } - if (matrix.isIdentity()) - return attrs; - 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(',') + ')'; + 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 (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } } return attrs; } @@ -10699,7 +11262,7 @@ new function() { currentStyle = project._currentStyle, children = []; if (!isClip) { - item._transformContent = false; + item._applyMatrix = false; item = applyAttributes(item, node, isRoot); project._currentStyle = item._style.clone(); } @@ -10723,11 +11286,14 @@ new function() { } function importPoly(node, type) { - var path = new Path(), - points = node.points; - path.moveTo(points.getItem(0)); - for (var i = 1, l = points.numberOfItems; i < l; i++) - path.lineTo(points.getItem(i)); + 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; @@ -10735,11 +11301,10 @@ new function() { function importPath(node) { var data = node.getAttribute('d'), - path = data.match(/m/gi).length > 1 - ? new CompoundPath() - : new Path(); - path.setPathData(data); - return path; + 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) { @@ -10881,10 +11446,10 @@ new function() { matrix.scale(v); break; case 'skewX': + matrix.skew(v[0], 0); + break; case 'skewY': - var value = Math.tan(v[0] * Math.PI / 180), - isX = command == 'skewX'; - matrix.shear(isX ? value : 0, isX ? 0 : value); + matrix.skew(0, v[0]); break; } } @@ -11030,7 +11595,7 @@ new function() { view = scope.project && scope.project.view; if (onLoad) onLoad.call(this, item); - view.draw(true); + view.update(); } if (isRoot) { @@ -11081,54 +11646,36 @@ new function() { }); }; -paper = new (PaperScope.inject(new Base(Base.exports, { - enumerable: true, - Base: Base, - Numerical: Numerical, - DomElement: DomElement, - XMLSerializer: XMLSerializer, - DOMParser: DOMParser, - Canvas: Canvas -})))(); - -module.exports = paper; - -return paper; -}; - -paper.PaperScope.prototype.PaperScript = (function(root) { - var Base = paper.Base, - PaperScope = paper.PaperScope, - PaperScript, - exports, define, +Base.exports.PaperScript = (function() { + var exports, define, scope = this; !function(e,r){return"object"==typeof exports&&"object"==typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):(r(e.acorn||(e.acorn={})),void 0)}(this,function(e){"use strict";function r(e){fr=e||{};for(var r in hr)Object.prototype.hasOwnProperty.call(fr,r)||(fr[r]=hr[r]);mr=fr.sourceFile||null}function t(e,r){var t=vr(pr,e);r+=" ("+t.line+":"+t.column+")";var n=new SyntaxError(r);throw n.pos=e,n.loc=t,n.raisedAt=br,n}function n(e){function r(e){if(1==e.length)return t+="return str === "+JSON.stringify(e[0])+";";t+="switch(str){";for(var r=0;r3){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&&8329!==t;)++br,t=pr.charCodeAt(br);fr.onComment&&fr.onComment(!1,pr.slice(e+2,br),e,br,r,fr.locations&&new a)}function u(){for(;dr>br;){var e=pr.charCodeAt(br);if(32===e)++br;else if(13===e){++br;var r=pr.charCodeAt(br);10===r&&++br,fr.locations&&(++Ar,Sr=br)}else if(10===e)++br,++Ar,Sr=br;else if(14>e&&e>8)++br;else if(47===e){var r=pr.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=pr.charCodeAt(br+1);return e>=48&&57>=e?E(!0):(++br,i(xt))}function f(){var e=pr.charCodeAt(br+1);return Er?(++br,k()):61===e?x(Et,2):x(wt,1)}function p(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Ft,1)}function d(e){var r=pr.charCodeAt(br+1);return r===e?x(124===e?Lt:Ut,2):61===r?x(Et,2):x(124===e?Rt:Vt,1)}function m(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Tt,1)}function h(e){var r=pr.charCodeAt(br+1);return r===e?x(St,2):61===r?x(Et,2):x(At,1)}function v(e){var r=pr.charCodeAt(br+1),t=1;return r===e?(t=62===e&&62===pr.charCodeAt(br+2)?3:2,61===pr.charCodeAt(br+t)?x(Et,t+1):x(jt,t)):(61===r&&(t=61===pr.charCodeAt(br+2)?3:2),x(Ot,t))}function b(e){var r=pr.charCodeAt(br+1);return 61===r?x(qt,61===pr.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(ht);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(pt);case 123:return++br,i(dt);case 125:return++br,i(mt);case 58:return++br,i(gt);case 63:return++br,i(kt);case 48:var r=pr.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 p();case 124:case 38:return d(e);case 94:return m();case 43:case 45:return h(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>=dr)return i(Br);var r=pr.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=pr.slice(br,br+r);br+=r,i(e,t)}function k(){for(var e,r,n="",a=br;;){br>=dr&&t(a,"Unterminated regular expression");var o=pr.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=pr.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=pr.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(pr.charCodeAt(br))&&t(br,"Identifier directly after number"),i(Or,e)}function E(e){var r=br,n=!1,a=48===pr.charCodeAt(br);e||null!==w(10)||t(r,"Invalid number"),46===pr.charCodeAt(br)&&(++br,w(10),n=!0);var o=pr.charCodeAt(br);(69===o||101===o)&&(o=pr.charCodeAt(++br),(43===o||45===o)&&++br,null===w(10)&&t(r,"Invalid number"),n=!0),Qt(pr.charCodeAt(br))&&t(br,"Identifier directly after number");var s,c=pr.slice(r,br);return n?s=parseFloat(c):a&&1!==c.length?/[89]/.test(c)||Vr?t(r,"Invalid number"):s=parseInt(c,8):s=parseInt(c,10),i(Or,s)}function A(e){br++;for(var r="";;){br>=dr&&t(yr,"Unterminated string constant");var n=pr.charCodeAt(br);if(n===e)return++br,i(Fr,r);if(92===n){n=pr.charCodeAt(++br);var a=/^[0-7]+/.exec(pr.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)Vr&&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===pr.charCodeAt(br)&&++br;case 10:fr.locations&&(Sr=br,++Ar);break;default:r+=String.fromCharCode(n)}}else(13===n||10===n||8232===n||8329===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=pr.charCodeAt(br);if(Yt(a))Bt&&(e+=pr.charAt(br)),++br;else{if(92!==a)break;Bt||(e=pr.slice(n,br)),Bt=!0,117!=pr.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:pr.slice(n,br)}function L(){var e=I(),r=Dr;return Bt||(Wt(e)?r=lt[e]:(fr.forbidReserved&&(3===fr.ecmaVersion?Mt:zt)(e)||Vr&&Xt(e))&&t(yr,"The keyword '"+e+"' is reserved")),i(r,e)}function U(){Ir=yr,Lr=gr,Ur=kr,g()}function R(e){for(Vr=e,br=Lr;Sr>br;)Sr=pr.lastIndexOf("\n",Sr-2)+1,--Ar;u(),g()}function T(){this.type=null,this.start=yr,this.end=null}function V(){this.start=xr,this.end=null,null!==mr&&(this.source=mr)}function q(){var e=new T;return fr.locations&&(e.loc=new V),fr.ranges&&(e.range=[yr,0]),e}function O(e){var r=new T;return r.start=e.start,fr.locations&&(r.loc=new V,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 F(e){return fr.ecmaVersion>=5&&"ExpressionStatement"===e.type&&"Literal"===e.expression.type&&"use strict"===e.expression.value}function D(e){return wr===e?(U(),!0):void 0}function B(){return!fr.strictSemicolons&&(wr===Br||wr===mt||Gt.test(pr.slice(Lr,yr)))}function M(){D(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"),Vr&&"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=Vr=null,Tr=[],g();var r=e||q(),t=!0;for(e||(r.body=[]);wr!==Br;){var n=J();r.body.push(n),t&&F(n)&&R(!0),t=!1}return j(r,"Program")}function J(){wr===wt&&g(!0);var e=wr,r=q();switch(e){case Mr:case Nr:U();var n=e===Mr;D(yt)||B()?r.label=null:wr!==Dr?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 a=j(a,/&&|\|\|/.test(a.operator)?"LogicalExpression":"BinaryExpression");return er(a,r,t)}return e}function rr(){if(wr.prefix){var e=q(),r=wr.isUpdate;return e.operator=Cr,e.prefix=!0,U(),e.argument=rr(),r?N(e.argument):Vr&&"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(D(xt)){var t=O(e);return t.object=e,t.property=lr(!0),t.computed=!1,nr(j(t,"MemberExpression"),r)}if(D(ft)){var t=O(e);return t.object=e,t.property=K(),t.computed=!0,z(pt),nr(j(t,"MemberExpression"),r)}if(!r&&D(ht)){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 Dr:return lr();case Or:case Fr:case jr:var e=q();return e.value=Cr,e.raw=pr.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 ht: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(pt,!0,!0),j(e,"ArrayExpression");case dt: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=D(ht)?ur(vt,!1):qr,j(e,"NewExpression")}function ir(){var e=q(),r=!0,n=!1;for(e.properties=[],U();!D(mt);){if(r)r=!1;else if(z(bt),fr.allowTrailingCommas&&D(mt))break;var a,o={key:sr()},i=!1;if(D(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!==ht&&X(),o.value=cr(q(),!1)):X(),"Identifier"===o.key.type&&(Vr||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;!D(e);){if(a)a=!1;else if(z(bt),r&&fr.allowTrailingCommas&&D(e))break;t&&wr===bt?n.push(null):n.push(K(!0))}return n}function lr(e){var r=q();return r.name=wr===Dr?Cr:e&&!fr.forbidReserved&&wr.keyword||X(),U(),j(r,"Identifier")}e.version="0.3.2";var fr,pr,dr,mr;e.parse=function(e,t){return pr=String(e),dr=pr.length,r(t),o(),W(fr.program)};var hr=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', + '+': '__add', + '-': '__subtract', + '*': '__multiply', + '/': '__divide', + '%': '__modulo', '==': 'equals', '!=': 'equals' }; var unaryOperators = { - '-': '_negate', + '-': '__negate', '+': null }; var fields = Base.each( ['add', 'subtract', 'multiply', 'divide', 'modulo', 'negate'], function(name) { - this['_' + name] = '#' + name; + this['__' + name] = '#' + name; }, {} ); - paper.Point.inject(fields); - paper.Size.inject(fields); - paper.Color.inject(fields); + Point.inject(fields); + Size.inject(fields); + Color.inject(fields); function _$_(left, operator, right) { var handler = binaryOperators[operator]; @@ -11178,8 +11725,8 @@ paper.PaperScope.prototype.PaperScript = (function(root) { function replaceCode(node, str) { var start = getOffset(node.range[0]), - end = getOffset(node.range[1]); - var insert = 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; @@ -11205,7 +11752,15 @@ paper.PaperScope.prototype.PaperScript = (function(root) { } } switch (node && node.type) { - case 'BinaryExpression': + 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), @@ -11214,32 +11769,29 @@ paper.PaperScope.prototype.PaperScript = (function(root) { + '", ' + right + ')'); } break; - case 'AssignmentExpression': - 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; - case 'UpdateExpression': - if (!node.prefix && !(parent && ( - parent.type === 'BinaryExpression' + case 'UpdateExpression': + case 'AssignmentExpression': + if (!(parent && ( + parent.type === 'ForStatement' + || parent.type === 'BinaryExpression' && /^[=!<>]/.test(parent.operator) || parent.type === 'MemberExpression' && parent.computed))) { - var arg = getCode(node.argument); - replaceCode(node, arg + ' = _$_(' + arg + ', "' - + node.operator[0] + '", 1)'); - } - break; - case 'UnaryExpression': - if (node.operator in unaryOperators - && node.argument.type !== 'Literal') { - var arg = getCode(node.argument); - replaceCode(node, '$_("' + node.operator + '", ' - + arg + ')'); + if (node.type === 'UpdateExpression') { + if (!node.prefix) { + var arg = getCode(node.argument); + replaceCode(node, arg + ' = _$_(' + arg + ', "' + + node.operator[0] + '", 1)'); + } + } 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; } @@ -11248,57 +11800,92 @@ paper.PaperScope.prototype.PaperScript = (function(root) { return code; } - function evaluate(code, scope) { + function execute(code, scope) { paper = scope; - var view = scope.project && scope.project.view, - res; - with (scope) { - (function() { - var onActivate, onDeactivate, onEditOptions, - onMouseDown, onMouseUp, onMouseDrag, onMouseMove, - onKeyDown, onKeyUp, onFrame, onResize; - code = compile(code); - res = eval(code); - if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) { - Base.each(paper.Tool.prototype._events, function(key) { - var value = eval(key); - if (value) { - scope.getTool()[key] = value; - } - }); + 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); + function expose(scope, hidden) { + for (var key in scope) { + if ((hidden || !/^_/.test(key)) && new RegExp( + '\\b' + key.replace(/\$/g, '\\$') + '\\b').test(code)) { + params.push(key); + args.push(scope[key]); } - if (view) { - view.setOnResize(onResize); - view.fire('resize', { - size: view.size, - delta: new Point() - }); - if (onFrame) - view.setOnFrame(onFrame); - view.draw(); - } - }).call(scope); + } + } + expose({ _$_: _$_, $_: $_, 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.fire('resize', { + size: view.size, + delta: new Point() + }); + if (res.onFrame) + view.setOnFrame(res.onFrame); + view.update(); } - return res; } var fs = require('fs'), path = require('path'); require.extensions['.pjs'] = function(module, uri) { - var source = compile(fs.readFileSync(uri, 'utf8')), - scope = new PaperScope(); - scope.__filename = uri; - scope.__dirname = path.dirname(uri); - scope.require = require; - scope.console = console; - evaluate(source, scope); - module.exports = scope; + 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 PaperScript = { + return { compile: compile, - evaluate: evaluate + execute: execute }; -})(this); +}).call(this); + +paper = new (PaperScope.inject(Base.exports, { + enumerable: true, + Base: Base, + Numerical: Numerical, + DomElement: DomElement, + XMLSerializer: XMLSerializer, + DOMParser: DOMParser, + Canvas: Canvas +}))(); + +module.exports = paper; + +return paper; +}; diff --git a/dist/paper.js b/dist/paper.js index f1420e1d..039086ac 100644 --- a/dist/paper.js +++ b/dist/paper.js @@ -1,28 +1,28 @@ /*! - * Paper.js v0.9.15 - The Swiss Army Knife of Vector Graphics Scripting. + * Paper.js v0.9.16 - The Swiss Army Knife of Vector Graphics Scripting. * http://paperjs.org/ * - * Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ + * 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 Dec 1 23:54:52 2013 +0100 + * Date: Thu Mar 13 17:48:12 2014 +0100 * *** * - * straps.js - Class inheritance library with support for bean-style accessors + * Straps.js - Class inheritance library with support for bean-style accessors * * Copyright (c) 2006 - 2013 Juerg Lehni - * http://lehni.org/ + * http://scratchdisk.com/ * * Distributed under the MIT license. * *** * - * acorn.js + * Acorn.js * http://marijnhaverbeke.nl/acorn/ * * Acorn is a tiny, fast JavaScript parser written in JavaScript, @@ -33,8 +33,7 @@ var paper = new function(undefined) { var Base = new function() { - var hidden = /^(statics|generics|preserve|enumerable|prototype|toString|valueOf)$/, - slice = [].slice, + var hidden = /^(statics|preserve|enumerable|prototype|toString|valueOf)$/, forEach = [].forEach || function(iter, bind) { for (var i = 0, l = this.length; i < l; i++) @@ -79,10 +78,10 @@ var Base = new function() { return _define(obj, name, desc); }; - function inject(dest, src, enumerable, base, preserve, generics) { + function inject(dest, src, enumerable, base, preserve) { var beans; - function field(name, val, dontCheck, generics) { + function field(name, val, dontCheck) { var val = val || (val = describe(src, name)) && (val.get ? val : val.value); if (typeof val === 'string' && val[0] === '#') @@ -96,11 +95,11 @@ var Base = new function() { && (!preserve || !prev)) { if (isFunc && prev) val.base = prev; - if (isFunc && beans && val.length === 0 - && (bean = name.match(/^(get|is)(([A-Z])(.*))$/))) - beans.push([ bean[3].toLowerCase() + bean[4], bean[2] ]); + if (isFunc && beans + && (bean = name.match(/^([gs]et|is)(([A-Z])(.*))$/))) + beans[bean[3].toLowerCase() + bean[4]] = bean[2]; if (!res || isFunc || !res.get || typeof res.get !== 'function' - || res.get.length !== 0) + || !Base.isPlainObject(res)) res = { value: res, writable: true }; if ((describe(dest, name) || { configurable: true }).configurable) { @@ -109,27 +108,20 @@ var Base = new function() { } define(dest, name, res); } - if (generics && isFunc && (!preserve || !generics[name])) { - generics[name] = function(bind) { - return bind && dest[name].apply(bind, - slice.call(arguments, 1)); - }; - } } if (src) { - beans = []; + beans = {}; for (var name in src) if (src.hasOwnProperty(name) && !hidden.test(name)) - field(name, null, true, generics); + field(name, null, true); field('toString'); field('valueOf'); - for (var i = 0, l = beans.length; i < l; i++) { - var bean = beans[i], - part = bean[1]; - field(bean[0], { - get: dest['get' + part] || dest['is' + part], - set: dest['set' + part] - }, true); + for (var name in beans) { + var part = beans[name], + set = dest['set' + part], + get = dest['get' + part] || set && dest['is' + part]; + if (get && (get.length === 0 || set)) + field(name, { get: get, set: set }, true); } } return dest; @@ -151,10 +143,6 @@ var Base = new function() { return dest; } - function clone(obj) { - return copy(new obj.constructor(), obj); - } - return inject(function Base() { for (var i = 0, l = arguments.length; i < l; i++) copy(this, arguments[i]); @@ -166,7 +154,7 @@ var Base = new function() { statics = src.statics === true ? src : src.statics; if (statics != src) inject(proto, src, src.enumerable, base && base.prototype, - src.preserve, src.generics && this); + src.preserve); inject(this, statics, true, base, src.preserve); } for (var i = 1, l = arguments.length; i < l; i++) @@ -184,6 +172,7 @@ var Base = new function() { base.apply(this, arguments); }; ctor.prototype = create(this.prototype); + ctor.base = base; define(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true }); inject(ctor, this, true); @@ -240,8 +229,6 @@ if (typeof module !== 'undefined') module.exports = Base; Base.inject({ - generics: true, - toString: function() { return this._id != null ? (this._class || 'Object') + (this._name @@ -282,7 +269,9 @@ Base.inject({ statics: { - exports: {}, + exports: { + enumerable: true + }, extend: function extend() { var res = extend.base.apply(this, arguments), @@ -319,7 +308,8 @@ Base.inject({ if (!checkKeys(obj1, obj2) || !checkKeys(obj2, obj1)) return false; for (var i in obj1) { - if (obj1.hasOwnProperty(i) && !Base.equals(obj1[i], obj2[i])) + if (obj1.hasOwnProperty(i) + && !Base.equals(obj1[i], obj2[i])) return false; } return true; @@ -327,58 +317,57 @@ Base.inject({ return false; }, - read: function(list, start, length, options) { + read: function(list, start, options, length) { if (this === Base) { var value = this.peek(list, start); - list._index++; - list.__read = 1; + list.__index++; return value; } var proto = this.prototype, readIndex = proto._readIndex, - index = start || readIndex && list._index || 0; + 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; + list.__index = index + 1; return obj && options && options.clone ? obj.clone() : obj; } obj = Base.create(this.prototype); if (readIndex) obj.__read = true; - if (options) - obj.__options = options; 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; - list.__read = obj.__read; - delete obj.__read; - if (options) - delete obj.__options; + list.__index = index + obj.__read; + obj.__read = undefined; } return obj; }, peek: function(list, start) { - return list[list._index = start || list._index || 0]; + 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; + 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, 0, options) - : this.read(list, i, 1, options)); + ? this.read(entry, 0, options) + : this.read(list, i, options, 1)); } return res; }, - readNamed: function(list, name, start, length, options) { + readNamed: function(list, name, start, options, length) { var value = this.getNamed(list, name), hasObject = value !== undefined; if (hasObject) { @@ -389,7 +378,7 @@ Base.inject({ } filtered[name] = undefined; } - return this.read(hasObject ? [value] : list, start, length, options); + return this.read(hasObject ? [value] : list, start, options, length); }, getNamed: function(list, name) { @@ -464,7 +453,8 @@ Base.inject({ }, deserialize: function(json, create, _data) { - var res = json; + var res = json, + isRoot = !_data; _data = _data || {}; if (Array.isArray(json)) { var type = json[0], @@ -482,7 +472,7 @@ Base.inject({ } else if (type) { var args = res; if (create) { - res = create(type, args); + res = create(type, args, isRoot); } else { res = Base.create(type.prototype); type.apply(res, args); @@ -497,18 +487,21 @@ Base.inject({ }, exportJSON: function(obj, options) { - return JSON.stringify(Base.serialize(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) { + function(type, args, isRoot) { var obj = target && target.constructor === type ? target : Base.create(type.prototype), isTarget = obj === target; - if (args.length === 1 && obj instanceof Item + if (!isRoot && args.length === 1 && obj instanceof Item && (!(obj instanceof Layer) || isTarget)) { var arg = args[0]; if (Base.isPlainObject(arg)) @@ -538,7 +531,7 @@ Base.inject({ args.push.apply(args, items); var removed = list.splice.apply(list, args); for (var i = 0, l = removed.length; i < l; i++) - delete removed[i]._index; + removed[i]._index = undefined; for (var i = index + amount, l = list.length; i < l; i++) list[i]._index = i; return removed; @@ -617,27 +610,14 @@ var Callback = { if (!handlers) return false; var args = [].slice.call(arguments, 1), - PaperScript = paper.PaperScript, - handleException = PaperScript && PaperScript.handleException, that = this; - - function callHandlers() { - for (var i in handlers) { - if (handlers[i].apply(that, args) === false - && event && event.stop) - event.stop(); + for (var i = 0, l = handlers.length; i < l; i++) { + if (handlers[i].apply(that, args) === false + && event && event.stop) { + event.stop(); + break; } } - - if (handleException) { - try { - callHandlers(); - } catch (e) { - handleException(e); - } - } else { - callHandlers(); - } return true; }, @@ -649,6 +629,19 @@ var Callback = { off: '#detach', trigger: '#fire', + _installEvents: function(install) { + var handlers = this._handlers, + key = install ? 'install' : 'uninstall'; + for (var type in handlers) { + if (handlers[type].length > 0) { + var entry = this._eventTypes[type], + func = entry[key]; + if (func) + func.call(this, type); + } + } + }, + statics: { inject: function inject() { for (var i = 0, l = arguments.length; i < l; i++) { @@ -667,11 +660,11 @@ var Callback = { return this[name]; }; src['set' + part] = function(func) { - if (func) { + var prev = this[name]; + if (prev) + this.detach(type, prev); + if (func) this.attach(type, func); - } else if (this[name]) { - this.detach(type, this[name]); - } this[name] = func; }; }); @@ -689,6 +682,11 @@ var PaperScope = Base.extend({ initialize: function PaperScope(script) { paper = this; + this.settings = { + applyMatrix: true, + handleSize: 4, + hitTolerance: 0 + }; this.project = null; this.projects = []; this.tools = []; @@ -708,26 +706,19 @@ var PaperScope = Base.extend({ } }, - version: '0.9.15', + version: '0.9.16', getView: function() { return this.project && this.project.view; }, - getTool: function() { - if (!this._tool) - this._tool = new Tool(); - return this._tool; - }, - getPaper: function() { return this; }, - evaluate: function(code) { - var res = paper.PaperScript.evaluate(code, this); + execute: function(code) { + paper.PaperScript.execute(code, this); View.updateFocus(); - return res; }, install: function(scope) { @@ -740,10 +731,9 @@ var PaperScope = Base.extend({ } }); }); - for (var key in this) { - if (!/^(version|_id)/.test(key)) + for (var key in this) + if (!/^_/.test(key) && this[key]) scope[key] = this[key]; - } }, setup: function(canvas) { @@ -783,7 +773,7 @@ var PaperScope = Base.extend({ _id: 0, get: function(id) { - if (typeof id === 'object') + if (id && id.getAttribute) id = id.getAttribute('id'); return this._scopes[id] || null; }, @@ -898,15 +888,29 @@ var Numerical = new function() { sqrt = Math.sqrt, pow = Math.pow, cos = Math.cos, - PI = Math.PI; + PI = Math.PI, + TOLERANCE = 10e-6, + EPSILON = 10e-12; + + function setupRoots(roots, min, max) { + var unbound = min === undefined, + minE = min - EPSILON, + maxE = max + EPSILON, + count = 0; + return function(root) { + if (unbound || root > minE && root < maxE) + roots[count++] = root < min ? min : root > max ? max : root; + return count; + }; + } return { - TOLERANCE: 10e-6, - EPSILON: 10e-12, + TOLERANCE: TOLERANCE, + EPSILON: EPSILON, KAPPA: 4 * (sqrt(2) - 1) / 3, isZero: function(val) { - return abs(val) <= Numerical.EPSILON; + return abs(val) <= EPSILON; }, integrate: function(f, a, b, n) { @@ -927,10 +931,10 @@ var Numerical = new function() { findRoot: function(f, df, x, a, b, n, tolerance) { for (var i = 0; i < n; i++) { var fx = f(x), - dx = fx / df(x); + dx = fx / df(x), + nx = x - dx; if (abs(dx) < tolerance) - return x; - var nx = x - dx; + return nx; if (fx > 0) { b = x; x = nx <= a ? 0.5 * (a + b) : nx; @@ -939,65 +943,45 @@ var Numerical = new function() { x = nx >= b ? 0.5 * (a + b) : nx; } } + return x; }, solveQuadratic: function(a, b, c, roots, min, max) { - var epsilon = Numerical.EPSILON, - unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; + var add = setupRoots(roots, min, max); - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - - if (abs(a) < epsilon) { - if (abs(b) >= epsilon) + if (abs(a) < EPSILON) { + if (abs(b) >= EPSILON) return add(-c / b); - return abs(c) < epsilon ? -1 : 0; + return abs(c) < EPSILON ? -1 : 0; } var p = b / (2 * a); var q = c / a; var p2 = p * p; - if (p2 < q - epsilon) + if (p2 < q - EPSILON) return 0; - var s = p2 > q ? sqrt(p2 - q) : 0; - add (s - p); + var s = p2 > q ? sqrt(p2 - q) : 0, + count = add(s - p); if (s > 0) - add(-s - p); + count = add(-s - p); return count; }, solveCubic: function(a, b, c, d, roots, min, max) { - var epsilon = Numerical.EPSILON; - if (abs(a) < epsilon) + if (abs(a) < EPSILON) return Numerical.solveQuadratic(b, c, d, roots, min, max); - var unbound = min === undefined, - minE = min - epsilon, - maxE = max + epsilon, - count = 0; - - function add(root) { - if (unbound || root > minE && root < maxE) - roots[count++] = root < min ? min : root > max ? max : root; - return count; - } - b /= a; c /= a; d /= a; - var bb = b * b, + var add = setupRoots(roots, min, max), + bb = b * b, p = (bb - 3 * c) / 9, q = (2 * bb * b - 9 * b * c + 27 * d) / 54, ppp = p * p * p, D = q * q - ppp; b /= 3; - if (abs(D) < epsilon) { - if (abs(q) < epsilon) + if (abs(D) < EPSILON) { + if (abs(q) < EPSILON) return add(-b); var sqp = sqrt(p), snq = q > 0 ? 1 : -1; @@ -1066,10 +1050,11 @@ var Point = Base.extend({ }, equals: function(point) { - return point === this || point && (this.x === point.x - && this.y === point.y - || Array.isArray(point) && this.x === point[0] - && this.y === point[1]) || false; + 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() { @@ -1086,28 +1071,28 @@ var Point = Base.extend({ return [f.number(this.x), f.number(this.y)]; }, - add: function(point) { - point = Point.read(arguments); + add: function() { + var point = Point.read(arguments); return new Point(this.x + point.x, this.y + point.y); }, - subtract: function(point) { - point = Point.read(arguments); + subtract: function() { + var point = Point.read(arguments); return new Point(this.x - point.x, this.y - point.y); }, - multiply: function(point) { - point = Point.read(arguments); + multiply: function() { + var point = Point.read(arguments); return new Point(this.x * point.x, this.y * point.y); }, - divide: function(point) { - point = Point.read(arguments); + divide: function() { + var point = Point.read(arguments); return new Point(this.x / point.x, this.y / point.y); }, - modulo: function(point) { - point = Point.read(arguments); + modulo: function() { + var point = Point.read(arguments); return new Point(this.x % point.x, this.y % point.y); }, @@ -1119,17 +1104,17 @@ var Point = Base.extend({ return matrix ? matrix._transformPoint(this) : this; }, - getDistance: function(point, squared) { - point = Point.read(arguments); - var x = point.x - this.x, + getDistance: function(_point, squared) { + 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); }, getLength: function() { - var length = this.x * this.x + this.y * this.y; - return arguments.length && arguments[0] ? length : Math.sqrt(length); + return Math.sqrt(this.x * this.x + this.y * this.y); }, setLength: function(length) { @@ -1148,7 +1133,6 @@ var Point = Base.extend({ this.y * scale ); } - return this; }, normalize: function(length) { @@ -1157,28 +1141,24 @@ var Point = Base.extend({ var current = this.getLength(), scale = current !== 0 ? length / current : 0, point = new Point(this.x * scale, this.y * scale); - point._angle = this._angle; + if (scale >= 0) + point._angle = this._angle; return point; }, getAngle: function() { - return this.getAngleInRadians(arguments[0]) * 180 / Math.PI; + return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; }, setAngle: function(angle) { - angle = this._angle = angle * Math.PI / 180; - if (!this.isZero()) { - var length = this.getLength(); - this.set( - Math.cos(angle) * length, - Math.sin(angle) * length - ); - } - return this; + this.setAngleInRadians.call(this, angle * Math.PI / 180); }, + getAngleInDegrees: '#getAngle', + setAngleInDegrees: '#setAngle', + getAngleInRadians: function() { - if (arguments[0] === undefined) { + if (!arguments.length) { return this.isZero() ? this._angle || 0 : this._angle = Math.atan2(this.y, this.x); @@ -1193,15 +1173,23 @@ var Point = Base.extend({ } }, - getAngleInDegrees: function() { - return this.getAngle(arguments[0]); + 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; }, - getDirectedAngle: function(point) { + getDirectedAngle: function(_point) { + var point = _point; point = Point.read(arguments); return Math.atan2(this.cross(point), this.dot(point)) * 180 / Math.PI; }, @@ -1215,7 +1203,7 @@ var Point = Base.extend({ c = Math.cos(angle); point = new Point( point.x * c - point.y * s, - point.y * c + point.x * s + point.x * s + point.y * c ); return center ? point.add(center) : point; }, @@ -1229,11 +1217,11 @@ var Point = Base.extend({ }, isColinear: function(point) { - return this.cross(point) < 0.00001; + return Math.abs(this.cross(point)) < 0.00001; }, isOrthogonal: function(point) { - return this.dot(point) < 0.00001; + return Math.abs(this.dot(point)) < 0.00001; }, isZero: function() { @@ -1244,18 +1232,18 @@ var Point = Base.extend({ return isNaN(this.x) || isNaN(this.y); }, - dot: function(point) { - point = Point.read(arguments); + dot: function() { + var point = Point.read(arguments); return this.x * point.x + this.y * point.y; }, - cross: function(point) { - point = Point.read(arguments); + cross: function() { + var point = Point.read(arguments); return this.x * point.y - this.y * point.x; }, - project: function(point) { - point = Point.read(arguments); + project: function() { + var point = Point.read(arguments); if (point.isZero()) { return new Point(0, 0); } else { @@ -1397,28 +1385,28 @@ var Size = Base.extend({ f.number(this.height)]; }, - add: function(size) { - size = Size.read(arguments); + add: function() { + var size = Size.read(arguments); return new Size(this.width + size.width, this.height + size.height); }, - subtract: function(size) { - size = Size.read(arguments); + subtract: function() { + var size = Size.read(arguments); return new Size(this.width - size.width, this.height - size.height); }, - multiply: function(size) { - size = Size.read(arguments); + multiply: function() { + var size = Size.read(arguments); return new Size(this.width * size.width, this.height * size.height); }, - divide: function(size) { - size = Size.read(arguments); + divide: function() { + var size = Size.read(arguments); return new Size(this.width / size.width, this.height / size.height); }, - modulo: function(size) { - size = Size.read(arguments); + modulo: function() { + var size = Size.read(arguments); return new Size(this.width % size.width, this.height % size.height); }, @@ -1550,7 +1538,7 @@ var Rectangle = Base.extend({ this.width = size.width; this.height = size.height; } - read = arguments._index; + read = arguments.__index; } if (this.__read) this.__read = read; @@ -1569,11 +1557,12 @@ var Rectangle = Base.extend({ }, equals: function(rect) { - if (Base.isPlainValue(rect)) - rect = Rectangle.read(arguments); - return rect === this - || rect && this.x === rect.x && this.y === rect.y - && this.width === rect.width && this.height === rect.height + 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; }, @@ -1594,24 +1583,24 @@ var Rectangle = Base.extend({ f.number(this.height)]; }, - getPoint: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.x, this.y, this, 'setPoint'); + getPoint: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.x, this.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.x = point.x; this.y = point.y; }, - getSize: function() { - return new (arguments[0] ? Size : LinkedSize) - (this.width, this.height, this, 'setSize'); + getSize: function(_dontLink) { + var ctor = _dontLink ? Size : LinkedSize; + return new ctor(this.width, this.height, this, 'setSize'); }, - setSize: function(size) { - size = Size.read(arguments); + setSize: function() { + var size = Size.read(arguments); if (this._fixX) this.x += (this.width - size.width) * this._fixX; if (this._fixY) @@ -1690,20 +1679,24 @@ var Rectangle = Base.extend({ this._fixY = 0.5; }, - getCenter: function() { - return new (arguments[0] ? Point : LinkedPoint) - (this.getCenterX(), this.getCenterY(), this, 'setCenter'); + getCenter: function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); }, - setCenter: function(point) { - point = Point.read(arguments); + 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; + return this.width === 0 || this.height === 0; }, contains: function(arg) { @@ -1729,42 +1722,42 @@ var Rectangle = Base.extend({ && y + rect.height <= this.y + this.height; }, - intersects: function(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.max(this.x, rect.x), + 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(rect) { - rect = Rectangle.read(arguments); - var x1 = Math.min(this.x, rect.x), + 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(point) { - point = Point.read(arguments); + 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), @@ -1772,9 +1765,10 @@ var Rectangle = Base.extend({ return new Rectangle(x1, y1, x2 - x1, y2 - y1); }, - expand: function(hor, ver) { - if (ver === undefined) - ver = hor; + 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); }, @@ -1803,12 +1797,12 @@ var Rectangle = Base.extend({ setY = 'set' + y, get = 'get' + part, set = 'set' + part; - this[get] = function() { - return new (arguments[0] ? Point : LinkedPoint) - (this[getX](), this[getY](), this, set); + this[get] = function(_dontLink) { + var ctor = _dontLink ? Point : LinkedPoint; + return new ctor(this[getX](), this[getY](), this, set); }; - this[set] = function(point) { - point = Point.read(arguments); + this[set] = function() { + var point = Point.read(arguments); this[setX](point.x); this[setY](point.y); }; @@ -1855,7 +1849,7 @@ var LinkedRectangle = Rectangle.extend({ this[name] = function() { this._dontNotify = true; proto[name].apply(this, arguments); - delete this._dontNotify; + this._dontNotify = false; this._owner[this._setter](this); }; }, { @@ -1916,8 +1910,14 @@ var Matrix = Base.extend({ }, _changed: function() { - if (this._owner) - this._owner._changed(5); + var owner = this._owner; + if (owner) { + if (owner._applyMatrix) { + owner.transform(null, true); + } else { + owner._changed(5); + } + } }, clone: function() { @@ -1940,16 +1940,36 @@ var Matrix = Base.extend({ f.number(this._ty)].join(', ') + ']]'; }, - reset: function() { + 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() { + var owner = this._owner; + if (owner) { + owner.transform(null, true); + 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, 0, { readNull: true }); + center = Point.read(arguments, 0, { readNull: true }); if (center) this.translate(center); this._a *= scale.x; @@ -1962,20 +1982,10 @@ var Matrix = Base.extend({ return this; }, - translate: function(point) { - point = Point.read(arguments); - var 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; - }, - - rotate: function(angle, center) { - center = Point.read(arguments, 1); - angle = angle * Math.PI / 180; - var x = center.x, + 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), @@ -1996,22 +2006,31 @@ var Matrix = Base.extend({ }, shear: function() { - var point = Point.read(arguments), - center = Point.read(arguments, 0, 0, { readNull: true }); + 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 += point.y * this._b; - this._c += point.y * this._d; - this._b += point.x * a; - this._d += point.x * 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 a = this._a, b = this._b, @@ -2049,6 +2068,10 @@ var Matrix = Base.extend({ && this._tx === 0 && this._ty === 0; }, + orNullIfIdentity: function() { + return this.isIdentity() ? null : this; + }, + isInvertible: function() { return !!this._getDeterminant(); }, @@ -2167,7 +2190,6 @@ var Matrix = Base.extend({ } return { - translation: this.getTranslation(), scaling: new Point(scaleX, scaleY), rotation: -Math.atan2(b, a) * 180 / Math.PI, shearing: shear @@ -2182,36 +2204,14 @@ var Matrix = Base.extend({ return new Point(this._tx, this._ty); }, - setTranslation: function() { - var point = Point.read(arguments); - this._tx = point.x; - this._ty = point.y; - this._changed(); - }, - getScaling: function() { return (this.decompose() || {}).scaling; }, - setScaling: function() { - var scaling = this.getScaling(); - if (scaling != null) { - var scale = Point.read(arguments); - (this._owner || this).scale( - scale.x / scaling.x, scale.y / scaling.y); - } - }, - getRotation: function() { return (this.decompose() || {}).rotation; }, - setRotation: function(angle) { - var rotation = this.getRotation(); - if (rotation != null) - (this._owner || this).rotate(angle - rotation); - }, - inverted: function() { var det = this._getDeterminant(); return det && new Matrix( @@ -2231,19 +2231,13 @@ var Matrix = Base.extend({ ctx.transform(this._a, this._c, this._b, this._d, this._tx, this._ty); } }, new function() { - return Base.each({ - scaleX: '_a', - scaleY: '_d', - translateX: '_tx', - translateY: '_ty', - shearX: '_b', - shearY: '_c' - }, function(prop, name) { - name = Base.capitalize(name); - this['get' + name] = function() { + return 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' + name] = function(value) { + this['set' + part] = function(value) { this[prop] = value; this._changed(); }; @@ -2376,8 +2370,7 @@ var Project = PaperScopeItem.extend({ this.view = view instanceof View ? view : View.create(view); this._selectedItems = {}; this._selectedItemCount = 0; - this._drawCount = 0; - this.options = {}; + this._updateVersion = 0; }, _serialize: function(options, dictionary) { @@ -2422,7 +2415,7 @@ var Project = PaperScopeItem.extend({ this.activeLayer = child; } else if (child instanceof Item) { (this.activeLayer - || this.addChild(new Layer({ insert: false }))).addChild(child); + || this.addChild(new Layer(Item.NO_INSERT))).addChild(child); } else { child = null; } @@ -2439,6 +2432,10 @@ var Project = PaperScopeItem.extend({ return items; }, + getOptions: function() { + return this._scope.settings; + }, + _updateSelection: function(item) { var id = item._id, selectedItems = this._selectedItems; @@ -2465,9 +2462,9 @@ var Project = PaperScopeItem.extend({ selectedItems[i].setFullySelected(false); }, - hitTest: function(point, options) { - point = Point.read(arguments); - options = HitResult.getOptions(Base.read(arguments)); + 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; @@ -2505,14 +2502,13 @@ var Project = PaperScopeItem.extend({ return Base.importJSON(json, layer && layer.isEmpty() && layer); }, - draw: function(ctx, matrix, ratio) { - this._drawCount++; + draw: function(ctx, matrix, pixelRatio) { + this._updateVersion++; ctx.save(); matrix.applyToContext(ctx); var param = new Base({ offset: new Point(0, 0), - ratio: ratio, - transforms: [matrix], + pixelRatio: pixelRatio, trackTransforms: true }); for (var i = 0, l = this.layers.length; i < l; i++) @@ -2523,30 +2519,30 @@ var Project = PaperScopeItem.extend({ ctx.save(); ctx.strokeWidth = 1; for (var id in this._selectedItems) { - var item = this._selectedItems[id]; - if (item._drawCount === this._drawCount + var item = this._selectedItems[id], + size = this._scope.settings.handleSize; + half = size / 2; + if (item._updateVersion === this._updateVersion && (item._drawSelected || item._boundsSelected)) { var color = item.getSelectedColor() || item.getLayer().getSelectedColor(); ctx.strokeStyle = ctx.fillStyle = color ? color.toCanvasStyle(ctx) : '#009dec'; - var mx = item._globalMatrix; + var mx = matrix.clone().concatenate(item._globalMatrix); if (item._drawSelected) item._drawSelected(ctx, mx); if (item._boundsSelected) { var coords = mx._transformCorners( - item._getBounds('getBounds')); + item.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.beginPath(); - ctx.rect(coords[i] - 2, coords[++i] - 2, 4, 4); - ctx.fill(); - } + for (var i = 0; i < 8; i++) + ctx.fillRect(coords[i] - half, coords[++i] - half, + size, size); } } } @@ -2564,7 +2560,6 @@ var Symbol = Base.extend({ this.project.symbols.push(this); if (item) this.setDefinition(item, dontCenter); - this._instances = {}; }, _serialize: function(options, dictionary) { @@ -2575,24 +2570,27 @@ var Symbol = Base.extend({ }, _changed: function(flags) { - Base.each(this._instances, function(item) { - item._changed(flags); - }); + if (flags & 4) { + Item._clearBoundsCache(this); + } + if (flags & 1) { + this.project._needsUpdate = true; + } }, getDefinition: function() { return this._definition; }, - setDefinition: function(item ) { + setDefinition: function(item, _dontCenter) { if (item._parentSymbol) item = item.clone(); if (this._definition) - delete this._definition._parentSymbol; + this._definition._parentSymbol = null; this._definition = item; item.remove(); item.setSelected(false); - if (!arguments[1]) + if (!_dontCenter) item.setPosition(new Point()); item._parentSymbol = this; this._changed(5); @@ -2619,15 +2617,20 @@ var Item = Base.extend(Callback, { if (name) proto._type = Base.hyphenate(name); return res; - } + }, + + NO_INSERT: { insert: false } }, _class: 'Item', - _transformContent: true, + _applyMatrix: true, + _canApplyMatrix: true, _boundsSelected: false, + _selectChildren: false, _serializeFields: { name: null, matrix: new Matrix(), + pivot: null, locked: false, visible: true, blendMode: 'normal', @@ -2635,6 +2638,7 @@ var Item = Base.extend(Callback, { guide: false, selected: false, clipMask: false, + applyMatrix: null, data: {} }, @@ -2642,21 +2646,26 @@ var Item = Base.extend(Callback, { }, _initialize: function(props, point) { - this._id = Item._id = (Item._id || 0) + 1; + var internal = props && props.internal === true, + matrix = this._matrix = new Matrix(); + project = paper.project; + if (!internal) + this._id = Item._id = (Item._id || 0) + 1; + this._applyMatrix = this._canApplyMatrix && paper.settings.applyMatrix; + if (point) + matrix.translate(point); + matrix._owner = this; + this._style = new Style(project._currentStyle, this); if (!this._project) { - var project = paper.project; - if (props && props.insert === false) { + if (internal || props && props.insert === false) { this._setProject(project); } else { (project.activeLayer || new Layer()).addChild(this); } } - this._style = new Style(this._project._currentStyle, this); - var matrix = this._matrix = new Matrix(); - if (point) - matrix.translate(point); - matrix._owner = this; - return props ? this._set(props, { insert: true }) : true; + return props && props !== Item.NO_INSERT + ? this._set(props, { insert: true }) + : true; }, _events: new function() { @@ -2746,24 +2755,23 @@ var Item = Base.extend(Callback, { }, _changed: function(flags) { - var parent = this._parent, - project = this._project, - symbol = this._parentSymbol; - this._drawCount = null; + var symbol = this._parentSymbol, + cacheParent = this._parent || symbol, + project = this._project; if (flags & 4) { - delete this._bounds; - delete this._position; + this._bounds = this._position = this._decomposed = + this._globalMatrix = undefined; } - if (parent && (flags + if (cacheParent && (flags & (4 | 8))) { - parent._clearBoundsCache(); + Item._clearBoundsCache(cacheParent); } if (flags & 2) { - this._clearBoundsCache(); + Item._clearBoundsCache(this); } if (project) { if (flags & 1) { - project._needsRedraw = true; + project._needsUpdate = true; } if (project._changes) { var entry = project._changesById[this._id]; @@ -2782,7 +2790,7 @@ var Item = Base.extend(Callback, { set: function(props) { if (props) - this._set(props); + this._set(props, { insert: true }); return this; }, @@ -2865,7 +2873,7 @@ var Item = Base.extend(Callback, { _guide: false, isSelected: function() { - if (this._children) { + if (this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) if (this._children[i].isSelected()) return true; @@ -2873,12 +2881,12 @@ var Item = Base.extend(Callback, { return this._selected; }, - setSelected: function(selected ) { - if (this._children && !arguments[1]) { + setSelected: function(selected, noChildren) { + if (!noChildren && this._selectChildren) { for (var i = 0, l = this._children.length; i < l; i++) this._children[i].setSelected(selected); } - if ((selected = !!selected) != this._selected) { + if ((selected = !!selected) ^ this._selected) { this._selected = selected; this._project._updateSelection(this); this._changed(33); @@ -2934,71 +2942,63 @@ var Item = Base.extend(Callback, { this._data = data; }, - getPosition: function() { - var pos = this._position - || (this._position = this.getBounds().getCenter(true)); - return new (arguments[0] ? Point : LinkedPoint) - (pos.x, pos.y, this, 'setPosition'); + 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))); - } -}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], - function(name) { - this[name] = function() { - var getter = this._boundsGetter, - bounds = this._getCachedBounds(typeof getter == 'string' - ? getter : getter && getter[name] || name, arguments[0]); - return name === 'getBounds' + }, + + getPivot: function(_dontLink) { + var pivot = this._pivot; + if (pivot) { + var ctor = _dontLink ? Point : LinkedPoint; + pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor'); + } + return pivot; + }, + + setPivot: function() { + this._pivot = Point.read(arguments); + this._position = undefined; + }, + + _pivot: null, + + getRegistration: '#getPivot', + setRegistration: '#setPivot' +}, 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, null, + internalGetter); + return key === 'bounds' ? new LinkedRectangle(bounds.x, bounds.y, bounds.width, bounds.height, this, 'setBounds') : bounds; }; + this[key] = { + get: this[getter] + }; }, { - _getCachedBounds: function(getter, matrix, cacheItem) { - var cache = (!matrix || matrix.equals(this._matrix)) && getter; - if (cacheItem && this._parent) { - var id = cacheItem._id, - ref = this._parent._boundsCache - = this._parent._boundsCache || { - ids: {}, - list: [] - }; - if (!ref.ids[id]) { - ref.list.push(cacheItem); - ref.ids[id] = cacheItem; - } - } - if (cache && this._bounds && this._bounds[cache]) - return this._bounds[cache].clone(); - var identity = this._matrix.isIdentity(); - matrix = !matrix || matrix.isIdentity() - ? identity ? null : this._matrix - : identity ? matrix : matrix.clone().concatenate(this._matrix); - var bounds = this._getBounds(getter, matrix, cache ? this : cacheItem); - if (cache) { - if (!this._bounds) - this._bounds = {}; - this._bounds[cache] = bounds.clone(); - } - return bounds; - }, - - _clearBoundsCache: function() { - if (this._boundsCache) { - for (var i = 0, list = this._boundsCache.list, l = list.length; - i < l; i++) { - var item = list[i]; - delete item._bounds; - if (item != this && item._boundsCache) - item._clearBoundsCache(); - } - delete this._boundsCache; - } - }, - _getBounds: function(getter, matrix, cacheItem) { var children = this._children; if (!children || children.length == 0) @@ -3022,9 +3022,9 @@ var Item = Base.extend(Callback, { : new Rectangle(); }, - setBounds: function(rect) { - rect = Rectangle.read(arguments); - var bounds = this.getBounds(), + setBounds: function() { + var rect = Rectangle.read(arguments), + bounds = this.getBounds(), matrix = new Matrix(), center = rect.getCenter(); matrix.translate(center); @@ -3036,53 +3036,155 @@ var Item = Base.extend(Callback, { 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; + var cacheParent = this._parent || this._parentSymbol; + if (cacheItem && cacheParent) { + var id = cacheItem._id, + ref = cacheParent._boundsCache = cacheParent._boundsCache || { + ids: {}, + list: [] + }; + if (!ref.ids[id]) { + ref.list.push(cacheItem); + ref.ids[id] = cacheItem; + } + } + if (cache && this._bounds && this._bounds[cache]) + return this._bounds[cache].clone(); + matrix = !matrix + ? _matrix + : _matrix + ? matrix.clone().concatenate(_matrix) + : matrix; + var bounds = this._getBounds(internalGetter || getter, matrix, + cache ? this : cacheItem); + if (cache) { + if (!this._bounds) + this._bounds = {}; + var cached = this._bounds[cache] = bounds.clone(); + cached._internal = !!internalGetter; + } + return bounds; + }, + + statics: { + _clearBoundsCache: function(item) { + if (item._boundsCache) { + for (var i = 0, list = item._boundsCache.list, l = list.length; + i < l; i++) { + var child = list[i]; + child._bounds = child._position = undefined; + if (child !== item && child._boundsCache) + child._clearBoundsCache(); + } + item._boundsCache = undefined; + } + } } }), { + _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() { + var decomposed = this._decomposed || this._decompose(); + return decomposed && decomposed.scaling; + }, + + setScaling: function() { + var current = this.getScaling(); + if (current != null) { + 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(matrix) { this._matrix.initialize(matrix); - if (this._transformContent) - this.applyMatrix(true); - this._changed(5); + if (this._applyMatrix) { + this.transform(null, true); + } else { + this._changed(5); + } }, getGlobalMatrix: function() { - return this._drawCount === this._project._drawCount - && this._globalMatrix || null; + var matrix = this._globalMatrix, + updateVersion = this._project._updateVersion; + if (matrix && matrix._updateVersion !== updateVersion) + matrix = null; + if (!matrix) { + matrix = this._globalMatrix = this._matrix.clone(); + if (this._parent) + matrix.preConcatenate(this._parent.getGlobalMatrix()); + matrix._updateVersion = updateVersion; + } + return matrix; }, - getTransformContent: function() { - return this._transformContent; + getApplyMatrix: function() { + return this._applyMatrix; }, - setTransformContent: function(transform) { - this._transformContent = transform; - if (transform) - this.applyMatrix(); + setApplyMatrix: function(transform) { + if (this._applyMatrix = this._canApplyMatrix && !!transform) + this.transform(null, true); }, + getTransformContent: '#getApplyMatrix', + setTransformContent: '#setApplyMatrix', + getProject: function() { return this._project; }, - _setProject: function(project) { - if (this._project != project) { - var hasOnFrame = this.responds('frame'); - if (hasOnFrame) - this._animateItem(false); + _setProject: function(project, installEvents) { + if (this._project !== project) { + if (this._project) + this._installEvents(false); this._project = project; - if (hasOnFrame) - this._animateItem(true); - if (this._children) { - for (var i = 0, l = this._children.length; i < l; i++) { - this._children[i]._setProject(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); + }, + + _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() { @@ -3155,7 +3257,7 @@ var Item = Base.extend(Callback, { }, clone: function(insert) { - return this._clone(new this.constructor({ insert: false }), insert); + return this._clone(new this.constructor(Item.NO_INSERT), insert); }, _clone: function(copy, insert) { @@ -3167,7 +3269,7 @@ var Item = Base.extend(Callback, { if (insert || insert === undefined) copy.insertAbove(this); var keys = ['_locked', '_visible', '_blendMode', '_opacity', - '_clipMask', '_guide']; + '_clipMask', '_guide', '_applyMatrix']; for (var i = 0, l = keys.length; i < l; i++) { var key = keys[i]; if (this.hasOwnProperty(key)) @@ -3187,22 +3289,22 @@ var Item = Base.extend(Callback, { rasterize: function(resolution) { var bounds = this.getStrokeBounds(), - scale = (resolution || 72) / 72, + view = this._project.view, + scale = (resolution || view && view.getResolution() || 72) / 72, topLeft = bounds.getTopLeft().floor(), bottomRight = bounds.getBottomRight().ceil() size = new Size(bottomRight.subtract(topLeft)), - canvas = CanvasProvider.getCanvas(size), + 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({ transforms: [matrix] })); ctx.restore(); - var raster = new Raster({ - canvas: canvas, - insert: false - }); - raster.setPosition(topLeft.add(size.divide(2))); + 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; }, @@ -3220,33 +3322,47 @@ var Item = Base.extend(Callback, { } return false; } - return point.isInside(this._getBounds('getBounds')); + return point.isInside(this.getInternalBounds()); }, hitTest: function(point, options) { point = Point.read(arguments); options = HitResult.getOptions(Base.read(arguments)); - if (this._locked || !this._visible || this._guide && !options.guides) return null; - if (!this._children && !this.getRoughBounds() - .expand(2 * options.tolerance)._containsPoint(point)) - return null; - point = this._matrix._inverseTransform(point); + var matrix = this._matrix, + parentTotalMatrix = options._totalMatrix, + view = this._project.view, + totalMatrix = options._totalMatrix = parentTotalMatrix + ? parentTotalMatrix.clone().concatenate(matrix) + : this.getGlobalMatrix().clone().preConcatenate( + view ? view._matrix : new Matrix()), + tolerancePadding = options._tolerancePadding = new Size( + Path._getPenPadding(1, totalMatrix.inverted()) + ).multiply( + Math.max(options.tolerance, 0.00001) + ); + point = matrix._inverseTransform(point); - var that = this, + 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 && this._type !== options.type), + that = this, res; + function checkBounds(type, part) { var pt = bounds['get' + part](); - if (point.getDistance(pt) < options.tolerance) + if (point.subtract(pt).divide(tolerancePadding).length <= 1) return new HitResult(type, that, { name: Base.hyphenate(part), point: pt }); } - if ((options.center || options.bounds) && - !(this instanceof Layer && !this._parent)) { - var bounds = this._getBounds('getBounds'); + if (checkSelf && (options.center || options.bounds) && this._parent) { + var bounds = this.getInternalBounds(); if (options.center) res = checkBounds('center', 'Center'); if (!res && options.bounds) { @@ -3259,24 +3375,27 @@ var Item = Base.extend(Callback, { } } - if ((res || (res = this._children || !(options.guides && !this._guide - || options.selected && !this._selected) - ? this._hitTest(point, options) : null)) - && res.point) { - res.point = that._matrix.transform(res.point); + 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._hitTest(point, options); + if (res && res.point) + res.point = matrix.transform(res.point); + options._totalMatrix = parentTotalMatrix; return res; }, + _getChildHitTestOptions: function(options) { + return options; + }, + _hitTest: function(point, options) { - var children = this._children; - if (children) { - for (var i = children.length - 1, res; i >= 0; i--) - if (res = children[i].hitTest(point, options)) - return res; - } else if (options.fill && this.hasFill() && this._contains(point)) { + if (options.fill && this.hasFill() && this._contains(point)) return new HitResult('fill', this); - } }, matches: function(match) { @@ -3375,16 +3494,17 @@ var Item = Base.extend(Callback, { items = Array.prototype.slice.apply(items); for (var i = items.length - 1; i >= 0; i--) { var item = items[i]; - if (_type && item._type !== _type) + if (_type && item._type !== _type) { items.splice(i, 1); - else + } else { item._remove(true); + } } Base.splice(children, items, index, 0); for (var i = 0, l = items.length; i < l; i++) { var item = items[i]; item._parent = this; - item._setProject(this._project); + item._setProject(this._project, true); if (item._name) item.setName(item._name); } @@ -3432,7 +3552,7 @@ var Item = Base.extend(Callback, { reduce: function() { if (this._children && this._children.length === 1) { - var child = this._children[0]; + var child = this._children[0].reduce(); child.insertAbove(this); child.setStyle(this._style); this.remove(); @@ -3465,8 +3585,7 @@ var Item = Base.extend(Callback, { this._removeNamed(); if (this._index != null) Base.splice(this._parent._children, null, this._index, 1); - if (this.responds('frame')) - this._animateItem(false); + this._installEvents(false); if (notify) this._parent._changed(7); this._parent = null; @@ -3580,90 +3699,87 @@ var Item = Base.extend(Callback, { return false; }, - scale: function(hor, ver , center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; - } - return this.transform(new Matrix().scale(hor, ver, - center || this.getPosition(true))); - }, - translate: function() { var mx = new Matrix(); return this.transform(mx.translate.apply(mx, arguments)); }, - rotate: function(angle, center) { + 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))); - }, + }; +}, { - shear: function(hor, ver, center) { - if (arguments.length < 2 || typeof ver === 'object') { - center = ver; - ver = hor; +}), { + transform: function(matrix, _applyMatrix) { + if (matrix && matrix.isIdentity()) + matrix = null; + var _matrix = this._matrix, + applyMatrix = (_applyMatrix || this._applyMatrix) + && (!_matrix.isIdentity() || matrix); + if (!matrix && !applyMatrix) + return this; + if (matrix) + _matrix.preConcatenate(matrix); + if (applyMatrix = applyMatrix && this._transformContent(_matrix)) { + var pivot = this._pivot, + style = this._style, + fillColor = style.getFillColor(true), + strokeColor = style.getStrokeColor(true); + if (pivot) + pivot.transform(_matrix); + if (fillColor) + fillColor.transform(_matrix); + if (strokeColor) + strokeColor.transform(_matrix); + _matrix.reset(true); } - return this.transform(new Matrix().shear(hor, ver, - center || this.getPosition(true))); - }, - - transform: function(matrix ) { var bounds = this._bounds, position = this._position; - this._matrix.preConcatenate(matrix); - if (this._transformContent || arguments[1]) - this.applyMatrix(true); this._changed(5); - if (bounds && matrix.getRotation() % 90 === 0) { + var decomp = bounds && matrix && matrix.decompose(); + if (decomp && !decomp.shearing && decomp.rotation % 90 === 0) { for (var key in bounds) { var rect = bounds[key]; - matrix._transformBounds(rect, rect); + 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 (position) { + } else if (matrix && position) { this._position = matrix._transformPoint(position, position); } return this; }, - _applyMatrix: function(matrix, applyMatrix) { + _transformContent: function(matrix) { var children = this._children; - if (children && children.length > 0) { + if (children) { for (var i = 0, l = children.length; i < l; i++) - children[i].transform(matrix, applyMatrix); + children[i].transform(matrix, true); return true; } }, - applyMatrix: function(_dontNotify) { - var matrix = this._matrix; - if (this._applyMatrix(matrix, true)) { - var style = this._style, - fillColor = style.getFillColor(true), - strokeColor = style.getStrokeColor(true); - if (fillColor) - fillColor.transform(matrix); - if (strokeColor) - strokeColor.transform(matrix); - matrix.reset(); - } - if (!_dontNotify) - this._changed(5); - }, - globalToLocal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._transformPoint(Point.read(arguments)); + return matrix && matrix._inverseTransform(Point.read(arguments)); }, localToGlobal: function() { var matrix = this.getGlobalMatrix(); - return matrix && matrix._inverseTransform(Point.read(arguments)); + return matrix && matrix._transformPoint(Point.read(arguments)); }, fitBounds: function(rectangle, fill) { @@ -3731,13 +3847,21 @@ var Item = Base.extend(Callback, { draw: function(ctx, param) { if (!this._visible || this._opacity === 0) return; - this._drawCount = this._project._drawCount; + var updateVersion = this._updateVersion = this._project._updateVersion; var trackTransforms = param.trackTransforms, - transforms = param.transforms, + transforms = param.transforms = param.transforms || [new Matrix()], + matrix = this._matrix, parentMatrix = transforms[transforms.length - 1], - globalMatrix = parentMatrix.clone().concatenate(this._matrix); - if (trackTransforms) + globalMatrix = parentMatrix.clone().concatenate(matrix); + if (!globalMatrix.isInvertible()) + return; + if (trackTransforms) { + if (!transforms) + transforms = param.transforms = []; transforms.push(this._globalMatrix = globalMatrix); + globalMatrix._updateVersion = updateVersion; + } + var blendMode = this._blendMode, opacity = this._opacity, normalBlend = blendMode === 'normal', @@ -3754,7 +3878,8 @@ var Item = Base.extend(Callback, { itemOffset = param.offset = bounds.getTopLeft().floor(); mainCtx = ctx; ctx = CanvasProvider.getContext( - bounds.getSize().ceil().add(new Size(1, 1)), param.ratio); + bounds.getSize().ceil().add(new Size(1, 1)), + param.pixelRatio); } ctx.save(); if (direct) { @@ -3764,7 +3889,7 @@ var Item = Base.extend(Callback, { } else { ctx.translate(-itemOffset.x, -itemOffset.y); } - (direct ? this._matrix : globalMatrix).applyToContext(ctx); + (direct ? matrix : globalMatrix).applyToContext(ctx); if (!direct && param.clipItem) param.clipItem.draw(ctx, param.extend({ clip: true })); this._draw(ctx, param); @@ -3775,7 +3900,7 @@ var Item = Base.extend(Callback, { ctx.clip(); if (!direct) { BlendMode.process(blendMode, ctx, mainCtx, opacity, - itemOffset.subtract(prevOffset).multiply(param.ratio)); + itemOffset.subtract(prevOffset).multiply(param.pixelRatio)); CanvasProvider.release(ctx); param.offset = prevOffset; } @@ -3808,6 +3933,7 @@ var Item = Base.extend(Callback, { var Group = Item.extend({ _class: 'Group', + _selectChildren: true, _serializeFields: { children: [] }, @@ -3821,12 +3947,8 @@ var Group = Item.extend({ _changed: function _changed(flags) { _changed.base.call(this, flags); - if (flags & 2 && this._transformContent - && !this._matrix.isIdentity()) { - this.applyMatrix(); - } if (flags & (2 | 256)) { - delete this._clipItem; + this._clipItem = undefined; } }, @@ -3888,7 +4010,8 @@ var Layer = Group.extend({ this._project.activeLayer = this.getNextSibling() || this.getPreviousSibling(); Base.splice(this._project.layers, null, this._index, 1); - this._project._needsRedraw = true; + this._installEvents(false); + this._project._needsUpdate = true; return true; } return false; @@ -3917,7 +4040,7 @@ var Layer = Group.extend({ this._remove(true); Base.splice(item._project.layers, [this], item._index + (above ? 1 : 0), 0); - this._setProject(item._project); + this._setProject(item._project, true); return this; } return _insert.base.call(this, above, item, _preserve); @@ -3926,14 +4049,17 @@ var Layer = Group.extend({ var Shape = Item.extend({ _class: 'Shape', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsSelected: true, + _serializeFields: { + shape: null, + size: null, + radius: null + }, - initialize: function Shape(shape, center, size, radius, props) { - this._shape = shape; - this._size = size; - this._radius = radius; - this._initialize(props, center); + initialize: function Shape(props) { + this._initialize(props); }, _equals: function(item) { @@ -3943,26 +4069,33 @@ var Shape = Item.extend({ }, clone: function(insert) { - return this._clone(new Shape(this._shape, this.getPosition(true), - this._size.clone(), - this._radius.clone ? this._radius.clone() : this._radius, - { insert: false }), insert); + var copy = new Shape(Item.NO_INSERT); + copy.setShape(this._shape); + copy.setSize(this._size); + copy.setRadius(this._radius); + return this._clone(copy, insert); }, getShape: function() { return this._shape; }, + setShape: function(shape) { + this._shape = shape; + }, + getSize: function() { var size = this._size; return new LinkedSize(size.width, size.height, this, 'setSize'); }, setSize: function() { - var shape = this._shape, - size = Size.read(arguments); - if (!this._size.equals(size)) { - var width = size.width, + var size = Size.read(arguments); + if (!this._size) { + this._size = size.clone(); + } else if (!this._size.equals(size)) { + var shape = this._shape, + width = size.width, height = size.height; if (shape === 'rectangle') { var radius = Size.min(this._radius, size.divide(2)); @@ -3995,14 +4128,18 @@ var Shape = Item.extend({ this._size.set(size, size); } else { radius = Size.read(arguments); - if (this._radius.equals(radius)) - return; - this._radius.set(radius.width, radius.height); - if (shape === 'rectangle') { - var size = Size.max(this._size, radius.multiply(2)); - this._size.set(size.width, size.height); - } else if (shape === 'ellipse') { - this._size.set(radius.width * 2, radius.height * 2); + if (!this._radius) { + this._radius = radius.clone(); + } else { + if (this._radius.equals(radius)) + return; + this._radius.set(radius.width, radius.height); + if (shape === 'rectangle') { + var size = Size.max(this._size, radius.multiply(2)); + this._size.set(size.width, size.height); + } else if (shape === 'ellipse') { + this._size.set(radius.width * 2, radius.height * 2); + } } } this._changed(5); @@ -4040,7 +4177,7 @@ var Shape = Item.extend({ } else { var rx = radius.width, ry = radius.height, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (shape === 'ellipse') { var cx = rx * kappa, cy = ry * kappa; @@ -4170,7 +4307,11 @@ new function() { statics: new function() { function createShape(shape, point, size, radius, args) { - return new Shape(shape, point, size, radius, Base.getNamed(args)); + var item = new Shape(Base.getNamed(args)); + item._shape = shape; + item._size = size; + item._radius = radius; + return item.translate(point); } return { @@ -4214,7 +4355,8 @@ statics: new function() { var Raster = Item.extend({ _class: 'Raster', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: 'getBounds', _boundsSelected: true, _serializeFields: { @@ -4239,15 +4381,17 @@ var Raster = Item.extend({ }, clone: function(insert) { - var param = { insert: false }, - image = this._image; + var copy = new Raster(Item.NO_INSERT), + image = this._image, + canvas = this._canvas; if (image) { - param.image = image; - } else if (this._canvas) { - var canvas = param.canvas = CanvasProvider.getCanvas(this._size); - canvas.getContext('2d').drawImage(this._canvas, 0, 0); + copy.setImage(image); + } else if (canvas) { + var copyCanvas = CanvasProvider.getCanvas(this._size); + copyCanvas.getContext('2d').drawImage(canvas, 0, 0); + copy.setCanvas(copyCanvas); } - return this._clone(new Raster(param), insert); + return this._clone(copy, insert); }, getSize: function() { @@ -4326,10 +4470,10 @@ var Raster = Item.extend({ setCanvas: '#setImage', - getContext: function() { + getContext: function(modify) { if (!this._context) this._context = this.getCanvas().getContext('2d'); - if (arguments[0]) { + if (modify) { this._image = null; this._changed(129); } @@ -4346,25 +4490,25 @@ var Raster = Item.extend({ setSource: function(src) { var that = this, - image = document.getElementById(src) || new Image(); + image; function loaded() { var view = that._project.view; if (view) paper = view._scope; + that.setImage(image); that.fire('load'); if (view) - view.draw(true); + view.update(); } + image = document.getElementById(src) || new Image(); + if (image.naturalWidth && image.naturalHeight) { setTimeout(loaded, 0); } else { DomEvent.add(image, { - load: function() { - that.setImage(image); - loaded(); - } + load: loaded }); if (!image.src) image.src = src; @@ -4376,20 +4520,18 @@ var Raster = Item.extend({ return this._canvas || this._image; }, - getSubCanvas: function(rect) { - rect = Rectangle.read(arguments); - var ctx = CanvasProvider.getContext(rect.getSize()); + getSubCanvas: function(rect) { + 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(rect) { - rect = Rectangle.read(arguments); - var raster = new Raster({ - canvas: this.getSubCanvas(rect), - insert: false - }); + getSubRaster: function(rect) { + var rect = Rectangle.read(arguments), + raster = new Raster(Item.NO_INSERT); + raster.setCanvas(this.getSubCanvas(rect)); raster.translate(rect.getCenter().subtract(this.getSize().divide(2))); raster._matrix.preConcatenate(this._matrix); raster.insertAbove(this); @@ -4404,8 +4546,8 @@ var Raster = Item.extend({ return canvas ? canvas.toDataURL() : null; }, - drawImage: function(image, point) { - point = Point.read(arguments, 1); + drawImage: function(image ) { + var point = Point.read(arguments, 1); this.getContext(true).drawImage(image, point.x, point.y); }, @@ -4459,8 +4601,8 @@ var Raster = Item.extend({ return total ? Color.read(channels) : null; }, - getPixel: function(point) { - point = Point.read(arguments); + getPixel: function(point) { + 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); @@ -4481,21 +4623,21 @@ var Raster = Item.extend({ ctx.putImageData(imageData, point.x, point.y); }, - createImageData: function(size) { - size = Size.read(arguments); + createImageData: function() { + var size = Size.read(arguments); return this.getContext().createImageData(size.width, size.height); }, - getImageData: function(rect) { - rect = Rectangle.read(arguments); + getImageData: function(rect) { + 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, point) { - point = Point.read(arguments, 1); + setImageData: function(data ) { + var point = Point.read(arguments, 1); this.getContext(true).putImageData(data, point.x, point.y); }, @@ -4534,7 +4676,8 @@ var Raster = Item.extend({ var PlacedSymbol = Item.extend({ _class: 'PlacedSymbol', - _transformContent: false, + _applyMatrix: false, + _canApplyMatrix: false, _boundsGetter: { getBounds: 'getStrokeBounds' }, _boundsSelected: true, _serializeFields: { @@ -4556,32 +4699,30 @@ var PlacedSymbol = Item.extend({ }, setSymbol: function(symbol) { - if (this._symbol) - delete this._symbol._instances[this._id]; this._symbol = symbol; - symbol._instances[this._id] = this; + this._changed(5); }, clone: function(insert) { - return this._clone(new PlacedSymbol({ - symbol: this.symbol, - insert: false - }), 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) { - return this.symbol._definition._getCachedBounds(getter, matrix); + _getBounds: function(getter, matrix, cacheItem) { + return this.symbol._definition._getCachedBounds(getter, matrix, + cacheItem); }, _hitTest: function(point, options, matrix) { - var result = this._symbol._definition._hitTest(point, options, matrix); - if (result) - result.item = this; - return result; + var res = this._symbol._definition._hitTest(point, options, matrix); + if (res) + res.item = this; + return res; }, _draw: function(ctx, param) { @@ -4606,7 +4747,7 @@ var HitResult = Base.extend({ getOptions: function(options) { return options && options._merged ? options : new Base({ type: null, - tolerance: paper.project.options.hitTolerance || 2, + tolerance: paper.settings.hitTolerance, fill: !options, stroke: !options, segments: !options, @@ -4659,27 +4800,30 @@ var Segment = Base.extend({ }, _changed: function(point) { - if (!this._path) + var path = this._path; + if (!path) return; - var curve = this._path._curves && this.getCurve(), - other; - if (curve) { - curve._changed(); - if (other = (curve[point == this._point - || point == this._handleIn && curve._segment1 == this - ? 'getPrevious' : 'getNext']())) { - other._changed(); - } + var curves = path._curves, + index = this._index, + curveIn, curveOut; + if (curves) { + if ((!point || point === this._point || point === this._handleIn) + && (curveIn = curves[index - 1] + || path._closed && curves[curves.length - 1])) + curveIn._changed(); + if ((!point || point === this._point || point === this._handleOut) + && (curveOut = curves[index])) + curveOut._changed(); } - this._path._changed(5); + path._changed(5); }, getPoint: function() { return this._point; }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this._point.set(point.x, point.y); }, @@ -4687,8 +4831,8 @@ var Segment = Base.extend({ return this._handleIn; }, - setHandleIn: function(point) { - point = Point.read(arguments); + setHandleIn: function() { + var point = Point.read(arguments); this._handleIn.set(point.x, point.y); }, @@ -4696,8 +4840,8 @@ var Segment = Base.extend({ return this._handleOut; }, - setHandleOut: function(point) { - point = Point.read(arguments); + setHandleOut: function() { + var point = Point.read(arguments); this._handleOut.set(point.x, point.y); }, @@ -4732,7 +4876,7 @@ var Segment = Base.extend({ var next = this.getNext(), handle1 = this._handleOut, handle2 = next._handleIn, - kappa = Numerical.KAPPA; + kappa = 0.5522847498307936; if (handle1.isOrthogonal(handle2)) { var from = this._point, to = next._point, @@ -4746,60 +4890,39 @@ var Segment = Base.extend({ return false; }, - _isSelected: function(point) { + _selectionState: 0, + + isSelected: function(_point) { var state = this._selectionState; - return point == this._point ? !!(state & 4) - : point == this._handleIn ? !!(state & 1) - : point == this._handleOut ? !!(state & 2) + return !_point ? !!(state & 7) + : _point === this._point ? !!(state & 4) + : _point === this._handleIn ? !!(state & 1) + : _point === this._handleOut ? !!(state & 2) : false; }, - _setSelected: function(point, selected) { + setSelected: function(selected, _point) { var path = this._path, selected = !!selected, - state = this._selectionState || 0, - selection = [ - !!(state & 4), - !!(state & 1), - !!(state & 2) - ]; - if (point === this._point) { - if (selected) { - selection[1] = selection[2] = false; - } else { - var previous = this.getPrevious(), - next = this.getNext(); - selection[1] = previous && (previous._point.isSelected() - || previous._handleOut.isSelected()); - selection[2] = next && (next._point.isSelected() - || next._handleIn.isSelected()); - } - selection[0] = 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 { - var index = point === this._handleIn ? 1 : 2; - if (selection[index] != selected) { - if (selected) - selection[0] = false; - selection[index] = selected; - } + state &= ~flag; } - this._selectionState = (selection[0] ? 4 : 0) - | (selection[1] ? 1 : 0) - | (selection[2] ? 2 : 0); - if (path && state != this._selectionState) { - path._updateSelection(this, state, this._selectionState); + this._selectionState = state; + if (path && state !== oldState) { + path._updateSelection(this, oldState, state); path._changed(33); } }, - isSelected: function() { - return this._isSelected(this._point); - }, - - setSelected: function(selected) { - this._setSelected(this._point, selected); - }, - getIndex: function() { return this._index !== undefined ? this._index : null; }, @@ -4865,6 +4988,11 @@ var Segment = Base.extend({ 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() @@ -4922,13 +5050,14 @@ var SegmentPoint = Point.extend({ x = y = 0; } else if ((x = point[0]) !== undefined) { y = point[1]; - } else { - if ((x = point.x) === undefined) { - point = Point.read(arguments); - x = point.x; + } else { + var pt = point; + if ((x = pt.x) === undefined) { + pt = Point.read(arguments); + x = pt.x; } - y = point.y; - selected = point.selected; + y = pt.y; + selected = pt.selected; } this._x = x; this._y = y; @@ -4977,11 +5106,11 @@ var SegmentPoint = Point.extend({ }, setSelected: function(selected) { - this._owner._setSelected(this, selected); + this._owner.setSelected(selected, this); }, isSelected: function() { - return this._owner._isSelected(this); + return this._owner.isSelected(this); } }); @@ -5021,16 +5150,15 @@ var Curve = Base.extend({ }, _changed: function() { - delete this._length; - delete this._bounds; + this._length = this._bounds = undefined; }, getPoint1: function() { return this._segment1._point; }, - setPoint1: function(point) { - point = Point.read(arguments); + setPoint1: function() { + var point = Point.read(arguments); this._segment1._point.set(point.x, point.y); }, @@ -5038,8 +5166,8 @@ var Curve = Base.extend({ return this._segment2._point; }, - setPoint2: function(point) { - point = Point.read(arguments); + setPoint2: function() { + var point = Point.read(arguments); this._segment2._point.set(point.x, point.y); }, @@ -5047,8 +5175,8 @@ var Curve = Base.extend({ return this._segment1._handleOut; }, - setHandle1: function(point) { - point = Point.read(arguments); + setHandle1: function() { + var point = Point.read(arguments); this._segment1._handleOut.set(point.x, point.y); }, @@ -5056,8 +5184,8 @@ var Curve = Base.extend({ return this._segment2._handleIn; }, - setHandle2: function(point) { - point = Point.read(arguments); + setHandle2: function() { + var point = Point.read(arguments); this._segment2._handleIn.set(point.x, point.y); }, @@ -5090,16 +5218,21 @@ var Curve = Base.extend({ }, isSelected: function() { - return this.getHandle1().isSelected() && this.getHandle2().isSelected(); + 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() { - return Curve.getValues(this._segment1, this._segment2); + getValues: function(matrix) { + return Curve.getValues(this._segment1, this._segment2, matrix); }, getPoints: function() { @@ -5111,15 +5244,12 @@ var Curve = Base.extend({ }, getLength: function() { - var from = arguments[0], - to = arguments[1], - fullLength = arguments.length === 0 || from === 0 && to === 1; - if (fullLength && this._length != null) - return this._length; - var length = Curve.getLength(this.getValues(), from, to); - if (fullLength) - this._length = length; - return length; + if (this._length == null) { + this._length = this.isLinear() + ? this._segment2._point.getDistance(this._segment1._point) + : Curve.getLength(this.getValues(), 0, 1); + } + return this._length; }, getArea: function() { @@ -5130,20 +5260,25 @@ var Curve = Base.extend({ return new Curve(Curve.getPart(this.getValues(), from, to)); }, + getPartLength: function(from, to) { + return Curve.getLength(this.getValues(), from, to); + }, + isLinear: function() { return this._segment1._handleOut.isZero() && this._segment2._handleIn.isZero(); }, + isHorizontal: function() { + return this.isLinear() && Numerical.isZero( + this._segment1._point._y - this._segment2._point._y); + }, + getIntersections: function(curve) { return Curve.getIntersections(this.getValues(), curve.getValues(), this, curve, []); }, - reverse: function() { - return new Curve(this._segment2.reverse(), this._segment1.reverse()); - }, - _getParameter: function(offset, isParameter) { return isParameter ? offset @@ -5154,13 +5289,13 @@ var Curve = Base.extend({ : this.getParameterAt(offset, 0); }, - divide: function(offset, isParameter) { + divide: function(offset, isParameter, ignoreLinear) { var parameter = this._getParameter(offset, isParameter), tolerance = 0.00001, res = null; if (parameter > tolerance && parameter < 1 - tolerance) { var parts = Curve.subdivide(this.getValues(), parameter), - isLinear = this.isLinear(), + isLinear = ignoreLinear ? false : this.isLinear(), left = parts[0], right = parts[1]; @@ -5199,6 +5334,22 @@ var Curve = Base.extend({ : null; }, + reverse: function() { + return new Curve(this._segment2.reverse(), this._segment1.reverse()); + }, + + 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); }, @@ -5214,17 +5365,20 @@ var Curve = Base.extend({ }, statics: { - getValues: function(segment1, segment2) { + getValues: function(segment1, segment2, matrix) { var p1 = segment1._point, h1 = segment1._handleOut, h2 = segment2._handleIn, - p2 = segment2._point; - return [ - p1._x, p1._y, - p1._x + h1._x, p1._y + h1._y, - p2._x + h2._x, p2._y + h2._y, - p2._x, p2._y - ]; + 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, 0, values, 0, 6); + return values; }, evaluate: function(v, t, type) { @@ -5232,11 +5386,13 @@ statics: { c1x = v[2], c1y = v[3], c2x = v[4], c2y = v[5], p2x = v[6], p2y = v[7], + tolerance = 0.00001, x, y; - if (type === 0 && (t === 0 || t === 1)) { - x = t === 0 ? p1x : p2x; - y = t === 0 ? p1y : p2y; + if (type === 0 && (t < tolerance || t > 1 - tolerance)) { + var isZero = t < tolerance; + x = isZero ? p1x : p2x; + y = isZero ? p1y : p2y; } else { var cx = 3 * (c1x - p1x), bx = 3 * (c2x - c1x) - cx, @@ -5249,11 +5405,16 @@ statics: { x = ((ax * t + bx) * t + cx) * t + p1x; y = ((ay * t + by) * t + cy) * t + p1y; } else { - var tolerance = 0.00001; if (t < tolerance && c1x === p1x && c1y === p1y || t > 1 - tolerance && c2x === p2x && c2y === p2y) { x = p2x - p1x; y = p2y - p1y; + } else if (t < tolerance) { + x = cx; + y = cy; + } else if (t > 1 - tolerance) { + 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; @@ -5265,7 +5426,7 @@ statics: { } } } - return type == 2 ? new Point(y, -x) : new Point(x, y); + return type === 2 ? new Point(y, -x) : new Point(x, y); }, subdivide: function(v, t) { @@ -5300,11 +5461,10 @@ statics: { }, getParameterOf: function(v, x, y) { - if (Math.abs(v[0] - x) < 0.00001 - && Math.abs(v[1] - y) < 0.00001) + var tolerance = 0.00001; + if (Math.abs(v[0] - x) < tolerance && Math.abs(v[1] - y) < tolerance) return 0; - if (Math.abs(v[6] - x) < 0.00001 - && Math.abs(v[7] - y) < 0.00001) + if (Math.abs(v[6] - x) < tolerance && Math.abs(v[7] - y) < tolerance) return 1; var txs = [], tys = [], @@ -5317,7 +5477,7 @@ statics: { 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) < 0.00001) + if (Math.abs(tx - ty) < tolerance) return (tx + ty) * 0.5; } } @@ -5404,87 +5564,6 @@ statics: { + t * t * t * v3, padding); } - }, - - _getWinding: function(v, prev, x, y, roots1, roots2) { - var tolerance = 0.00001, - abs = Math.abs; - - function getDirection(v) { - var y0 = v[1], - y1 = v[7], - dir = y0 > y1 ? -1 : 1; - return dir === 1 && (y < y0 || y > y1) - || dir === -1 && (y < y1 || y > y0) - ? 0 - : dir; - } - - if (Curve.isLinear(v)) { - var dir = getDirection(v); - if (!dir) - return 0; - var cross = (v[6] - v[0]) * (y - v[1]) - (v[7] - v[1]) * (x - v[0]); - return (cross < -tolerance ? -1 : 1) == dir ? 0 : dir; - } - - var y0 = v[1], - y1 = v[3], - y2 = v[5], - y3 = v[7]; - var a = 3 * (y1 - y2) - y0 + y3, - b = 2 * (y0 + y2) - 4 * y1, - c = y1 - y0; - var count = Numerical.solveQuadratic(a, b, c, roots1, tolerance, - 1 - tolerance), - part, - rest = v, - t1 = roots1[0], - winding = 0; - for (var i = 0; i <= count; i++) { - if (i === count) { - part = rest; - } else { - var curves = Curve.subdivide(rest, t1); - part = curves[0]; - rest = curves[1]; - t1 = roots1[i]; - t1 = (roots1[i + 1] - t1) / (1 - t1); - } - if (i > 0) - part[3] = part[1]; - if (i < count) - part[5] = rest[1]; - var dir = getDirection(part); - if (!dir) - continue; - var t2, - px; - if (Curve.solveCubic(part, 1, y, roots2, -tolerance, 1 + -tolerance) - === 1) { - t2 = roots2[0]; - px = Curve.evaluate(part, t2, 0).x; - } else { - var mid = (part[1] + part[7]) / 2; - t2 = y < mid && dir > 0 ? 0 : 1; - if (t2 === 1 && y == part[7]) - continue; - px = t2 === 0 ? part[0] : part[6]; - } - var slope = Curve.evaluate(part, t2, 1).y, - stationary = abs(slope) < tolerance || t2 < tolerance - && Curve.evaluate(prev, 1, 1).y * slope < 0; - if (x >= px + (stationary ? -tolerance : tolerance * dir) - && !(stationary && (abs(t2) < tolerance - && abs(x - part[0]) > tolerance - || abs(t2 - 1) < tolerance - && abs(x - part[6]) > tolerance))) { - winding += stationary && abs(t2 - (dir > 0 ? 1 : 0)) < tolerance - ? -dir : dir; - } - prev = part; - } - return winding; } }}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], function(name) { @@ -5518,8 +5597,8 @@ statics: { start !== undefined ? start : offset < 0 ? 1 : 0); }, - getParameterOf: function(point) { - point = Point.read(arguments); + getParameterOf: function(point) { + var point = Point.read(arguments); return Curve.getParameterOf(this.getValues(), point.x, point.y); }, @@ -5529,17 +5608,16 @@ statics: { return new CurveLocation(this, offset); }, - getLocationOf: function(point) { - point = Point.read(arguments); - var t = this.getParameterOf(point); + getLocationOf: function(point) { + var point = Point.read(arguments), + t = this.getParameterOf(point); return t != null ? new CurveLocation(this, t) : null; }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var values = this.getValues(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + values = this.getValues(), count = 100, - tolerance = Numerical.TOLERANCE, minDist = Infinity, minT = 0; @@ -5559,7 +5637,7 @@ statics: { refine(i / count); var step = 1 / (count * 2); - while (step > tolerance) { + while (step > 0.00001) { if (!refine(minT - step) && !refine(minT + step)) step /= 2; } @@ -5569,7 +5647,7 @@ statics: { }, getNearestPoint: function(point) { - point = Point.read(arguments); + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); } @@ -5610,11 +5688,12 @@ new function() { if (b === undefined) b = 1; var isZero = Numerical.isZero; - if (isZero(v[0] - v[2]) && isZero(v[1] - v[3]) + 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])) { var dx = v[6] - v[0], dy = v[7] - v[1]; - return (b - a) * Math.sqrt(dx * dx + dy * dy); + return Math.sqrt(dx * dx + dy * dy); } var ds = getLengthIntegrand(v); return Numerical.integrate(ds, a, b, getIterations(a, b)); @@ -5648,148 +5727,89 @@ new function() { } }; }, new function() { - function addLocation(locations, curve1, t1, point1, curve2, t2, point2) { - var first = locations[0], - last = locations[locations.length - 1]; - if ((!first || !point1.isClose(first._point, Numerical.EPSILON)) - && (!last || !point1.isClose(last._point, Numerical.EPSILON))) - locations.push( - new CurveLocation(curve1, t1, point1, curve2, t2, point2)); + 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); } - function addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, range2, recursion) { - recursion = (recursion || 0) + 1; + function addCurveIntersections(v1, v2, curve1, curve2, locations, include, + tMin, tMax, uMin, uMax, oldTDiff, reverse, recursion) { if (recursion > 20) return; - range1 = range1 || [ 0, 1 ]; - range2 = range2 || [ 0, 1 ]; - var part1 = Curve.getPart(v1, range1[0], range1[1]), - part2 = Curve.getPart(v2, range2[0], range2[1]), - iteration = 0; - while (iteration++ < 20) { - var range, - intersects1 = clipFatLine(part1, part2, range = range2.slice()), - intersects2 = 0; - if (intersects1 === 0) - break; - if (intersects1 > 0) { - range2 = range; - part2 = Curve.getPart(v2, range2[0], range2[1]); - intersects2 = clipFatLine(part2, part1, range = range1.slice()); - if (intersects2 === 0) - break; - if (intersects1 > 0) { - range1 = range; - part1 = Curve.getPart(v1, range1[0], range1[1]); - } + var q0x = v2[0], q0y = v2[1], q3x = v2[6], q3y = v2[7], + tolerance = 0.00001, + hullEpsilon = 1e-9, + 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, + 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]), + tMinNew, tMaxNew, tDiff; + if (q0x === q3x && uMax - uMin <= hullEpsilon && recursion > 3) { + tMinNew = (tMax + tMin) / 2; + tMaxNew = tMinNew; + 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 false; + v1 = Curve.getPart(v1, tMinClip, tMaxClip); + tDiff = tMaxClip - tMinClip; + tMinNew = tMax * tMinClip + tMin * (1 - tMinClip); + tMaxNew = tMax * tMaxClip + tMin * (1 - tMaxClip); + } + if (oldTDiff > 0.8 && tDiff > 0.8) { + 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); + addCurveIntersections( + v2, parts[1], curve2, curve1, locations, include, + 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); + addCurveIntersections( + parts[1], v1, curve2, curve1, locations, include, + t, uMax, tMinNew, tMaxNew, tDiff, !reverse, recursion); } - if (intersects1 < 0 || intersects2 < 0) { - if (range1[1] - range1[0] > range2[1] - range2[0]) { - var t = (range1[0] + range1[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ range1[0], t ], range2, recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - [ t, range1[1] ], range2, recursion); - break; - } else { - var t = (range2[0] + range2[1]) / 2; - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ range2[0], t ], recursion); - addCurveIntersections(v1, v2, curve1, curve2, locations, - range1, [ t, range2[1] ], recursion); - break; - } - } - if (Math.abs(range1[1] - range1[0]) <= 0.00001 && - Math.abs(range2[1] - range2[0]) <= 0.00001) { - var t1 = (range1[0] + range1[1]) / 2, - t2 = (range2[0] + range2[1]) / 2; - addLocation(locations, + } else if (Math.max(uMax - uMin, tMaxNew - tMinNew) < tolerance) { + var t1 = tMinNew + (tMaxNew - tMinNew) / 2, + t2 = uMin + (uMax - uMin) / 2; + if (reverse) { + addLocation(locations, include, + curve2, t2, Curve.evaluate(v2, t2, 0), + curve1, t1, Curve.evaluate(v1, t1, 0)); + } else { + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); - break; } + } else { + addCurveIntersections(v2, v1, curve2, curve1, locations, include, + uMin, uMax, tMinNew, tMaxNew, tDiff, !reverse, ++recursion); } } - function clipFatLine(v1, v2, range2) { - var p0x = v1[0], p0y = v1[1], p1x = v1[2], p1y = v1[3], - p2x = v1[4], p2y = v1[5], p3x = v1[6], p3y = v1[7], - q0x = v2[0], q0y = v2[1], q1x = v2[2], q1y = v2[3], - q2x = v2[4], q2y = v2[5], q3x = v2[6], q3y = v2[7], - getSignedDistance = Line.getSignedDistance, - d1 = getSignedDistance(p0x, p0y, p3x, p3y, p1x, p1y) || 0, - d2 = getSignedDistance(p0x, p0y, p3x, p3y, p2x, p2y) || 0, - factor = d1 * d2 > 0 ? 3 / 4 : 4 / 9, - dmin = factor * Math.min(0, d1, d2), - dmax = factor * Math.max(0, d1, d2), - dq0 = getSignedDistance(p0x, p0y, p3x, p3y, q0x, q0y), - dq1 = getSignedDistance(p0x, p0y, p3x, p3y, q1x, q1y), - dq2 = getSignedDistance(p0x, p0y, p3x, p3y, q2x, q2y), - dq3 = getSignedDistance(p0x, p0y, p3x, p3y, q3x, q3y); - if (dmin > Math.max(dq0, dq1, dq2, dq3) - || dmax < Math.min(dq0, dq1, dq2, dq3)) - return 0; - var hull = getConvexHull(dq0, dq1, dq2, dq3), - swap; - if (dq3 < dq0) { - swap = dmin; - dmin = dmax; - dmax = swap; - } - var tmaxdmin = -Infinity, - tmin = Infinity, - tmax = -Infinity; - for (var i = 0, l = hull.length; i < l; i++) { - var p1 = hull[i], - p2 = hull[(i + 1) % l]; - if (p2[1] < p1[1]) { - swap = p2; - p2 = p1; - p1 = swap; - } - var x1 = p1[0], - y1 = p1[1], - x2 = p2[0], - y2 = p2[1]; - var inv = (y2 - y1) / (x2 - x1); - if (dmin >= y1 && dmin <= y2) { - var ixdx = x1 + (dmin - y1) / inv; - if (ixdx < tmin) - tmin = ixdx; - if (ixdx > tmaxdmin) - tmaxdmin = ixdx; - } - if (dmax >= y1 && dmax <= y2) { - var ixdx = x1 + (dmax - y1) / inv; - if (ixdx > tmax) - tmax = ixdx; - if (ixdx < tmin) - tmin = 0; - } - } - if (tmin !== Infinity && tmax !== -Infinity) { - var min = Math.min(dmin, dmax), - max = Math.max(dmin, dmax); - if (dq3 > min && dq3 < max) - tmax = 1; - if (dq0 > min && dq0 < max) - tmin = 0; - if (tmaxdmin > tmax) - tmax = 1; - var v2tmin = range2[0], - tdiff = range2[1] - v2tmin; - range2[0] = v2tmin + tmin * tdiff; - range2[1] = v2tmin + tmax * tdiff; - if ((tdiff - (range2[1] - range2[0])) / tdiff >= 0.2) - return 1; - } - if (Curve.getBounds(v1).touches(Curve.getBounds(v2))) - return -1; - return 0; - } - function getConvexHull(dq0, dq1, dq2, dq3) { var p0 = [ 0, dq0 ], p1 = [ 1 / 3, dq1 ], @@ -5797,26 +5817,74 @@ new function() { p3 = [ 1, dq3 ], getSignedDistance = Line.getSignedDistance, dist1 = getSignedDistance(0, dq0, 1, dq3, 1 / 3, dq1), - dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2); + dist2 = getSignedDistance(0, dq0, 1, dq3, 2 / 3, dq2), + flip = false, + hull; if (dist1 * dist2 < 0) { - return [ p0, p1, p3, p2 ]; - } - var pmax, cross; - if (Math.abs(dist1) > Math.abs(dist2)) { - pmax = p1; - cross = (dq3 - dq2 - (dq3 - dq0) / 3) - * (2 * (dq3 - dq2) - dq3 + dq1) / 3; + hull = [[p0, p1, p3], [p0, p2, p3]]; + flip = dist1 < 0; } else { - pmax = p2; - cross = (dq1 - dq0 + (dq0 - dq3) / 3) - * (-2 * (dq0 - dq1) + dq0 - dq2) / 3; + 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; } - return cross < 0 - ? [ p0, pmax, p3 ] - : [ p0, p1, p2, p3 ]; + return flip ? hull.reverse() : hull; } - function addCurveLineIntersections(v1, v2, curve1, curve2, locations) { + function clipConvexHull(hullTop, hullBottom, dMin, dMax) { + var tProxy, + tVal = null, + px, py, + qx, qy; + for (var i = 0, l = hullBottom.length - 1; i < l; i++) { + py = hullBottom[i][1]; + qy = hullBottom[i + 1][1]; + if (py < qy) { + tProxy = null; + } else if (qy <= dMax) { + px = hullBottom[i][0]; + qx = hullBottom[i + 1][0]; + tProxy = px + (dMax - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + if (hullTop[0][1] <= dMax) + tProxy = hullTop[0][0]; + for (var i = 0, l = hullTop.length - 1; i < l; i++) { + py = hullTop[i][1]; + qy = hullTop[i + 1][1]; + if (py >= dMin) { + tVal = tProxy; + } else if (py > qy) { + tVal = null; + } else if (qy >= dMin) { + px = hullTop[i][0]; + qx = hullTop[i + 1][0]; + tVal = px + (dMin - py) * (qx - px) / (qy - py); + } else { + continue; + } + break; + } + return tVal; + } + + function addCurveLineIntersections(v1, v2, curve1, curve2, locations, + include) { var flip = Curve.isLinear(v1), vc = flip ? v2 : v1, vl = flip ? v1 : v2, @@ -5846,43 +5914,37 @@ new function() { var tl = Curve.getParameterOf(rvl, x, 0), t1 = flip ? tl : tc, t2 = flip ? tc : tl; - addLocation(locations, + addLocation(locations, include, curve1, t1, Curve.evaluate(v1, t1, 0), curve2, t2, Curve.evaluate(v2, t2, 0)); } } } - function addLineIntersection(v1, v2, curve1, curve2, locations) { + function addLineIntersection(v1, v2, curve1, curve2, locations, include) { var point = Line.intersect( v1[0], v1[1], v1[6], v1[7], v2[0], v2[1], v2[6], v2[7]); - if (point) - addLocation(locations, curve1, null, point, curve2); + 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); + } } return { statics: { - getIntersections: function(v1, v2, curve1, curve2, locations) { + getIntersections: function(v1, v2, curve1, curve2, locations, include) { var linear1 = Curve.isLinear(v1), - linear2 = Curve.isLinear(v2), - c1p1 = curve1.getPoint1(), - c1p2 = curve1.getPoint2(), - c2p1 = curve2.getPoint1(), - c2p2 = curve2.getPoint2(), - tolerance = 0.00001; - if (c1p1.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 0, c1p1); - if (c1p1.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 0, c1p1, curve2, 1, c1p1); + linear2 = Curve.isLinear(v2); (linear1 && linear2 ? addLineIntersection : linear1 || linear2 ? addCurveLineIntersections - : addCurveIntersections)(v1, v2, curve1, curve2, locations); - if (c1p2.isClose(c2p1, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 0, c1p2); - if (c1p2.isClose(c2p2, tolerance)) - addLocation(locations, curve1, 1, c1p2, curve2, 1, c1p2); + : addCurveIntersections)( + v1, v2, curve1, curve2, locations, include, + 0, 1, 0, 1, 0, false, 0); return locations; } }}; @@ -5904,13 +5966,13 @@ var CurveLocation = Base.extend({ this._distance = _distance; }, - getSegment: function() { + getSegment: function(_preferFirst) { if (!this._segment) { var curve = this.getCurve(), parameter = this.getParameter(); if (parameter === 1) { this._segment = curve._segment2; - } else if (parameter === 0 || arguments[0]) { + } else if (parameter === 0 || _preferFirst) { this._segment = curve._segment1; } else if (parameter == null) { return null; @@ -5924,8 +5986,12 @@ var CurveLocation = Base.extend({ return this._segment; }, - getCurve: function() { - if (!this._curve || arguments[0]) { + setSegment: function(segment) { + this._segment = segment; + }, + + getCurve: function(_uncached) { + if (!this._curve || _uncached) { this._curve = this._segment1.getCurve(); if (this._curve.getParameterOf(this._point) == null) this._curve = this._segment2.getPrevious().getCurve(); @@ -5933,6 +5999,10 @@ var CurveLocation = Base.extend({ return this._curve; }, + setCurve: function(curve) { + this._curve = curve; + }, + getIntersection: function() { var intersection = this._intersection; if (!intersection && this._curve2) { @@ -5965,22 +6035,30 @@ var CurveLocation = Base.extend({ return parameter != null && curve && curve.getLength(0, parameter); }, - getParameter: function() { - if ((this._parameter == null || arguments[0]) && this._point) { - var curve = this.getCurve(arguments[0] && this._point); + getParameter: function(_uncached) { + if ((this._parameter == null || _uncached) && this._point) { + var curve = this.getCurve(_uncached && this._point); this._parameter = curve && curve.getParameterOf(this._point); } return this._parameter; }, - getPoint: function() { - if ((!this._point || arguments[0]) && this._parameter != null) { + setParameter: function(parameter) { + this._parameter = parameter; + }, + + getPoint: function(_uncached) { + if ((!this._point || _uncached) && this._parameter != null) { var curve = this.getCurve(); this._point = curve && curve.getPointAt(this._parameter, true); } return this._point; }, + setPoint: function(point) { + this._point = point; + }, + getTangent: function() { var parameter = this.getParameter(), curve = this.getCurve(); @@ -6007,6 +6085,17 @@ var CurveLocation = Base.extend({ return curve && curve.split(this.getParameter(true), true); }, + equals: function(loc) { + var isZero = Numerical.isZero; + return this === loc + || loc + && this._curve === loc._curve + && this._curve2 === loc._curve2 + && isZero(this._parameter - loc._parameter) + && isZero(this._parameter2 - loc._parameter2) + || false; + }, + toString: function() { var parts = [], point = this.getPoint(), @@ -6031,22 +6120,95 @@ var PathItem = Item.extend({ initialize: function PathItem() { }, - getIntersections: function(path) { - if (!this.getBounds().touches(path.getBounds())) + getIntersections: function(path, _expand) { + if (this === path) + path = null; + if (path && !this.getBounds().touches(path.getBounds())) return []; var locations = [], curves1 = this.getCurves(), - curves2 = path.getCurves(), - length2 = curves2.length, - values2 = []; + curves2 = path ? path.getCurves() : curves1, + matrix1 = this._matrix.orNullIfIdentity(), + matrix2 = path ? path._matrix.orNullIfIdentity() : matrix1, + length1 = curves1.length, + length2 = path ? curves2.length : length1, + values2 = [], + MIN = 1e-11, + MAX = 1 - 1e-11; for (var i = 0; i < length2; i++) - values2[i] = curves2[i].getValues(); - for (var i = 0, l = curves1.length; i < l; i++) { + values2[i] = curves2[i].getValues(matrix2); + for (var i = 0; i < length1; i++) { var curve1 = curves1[i], - values1 = curve1.getValues(); - for (var j = 0; j < length2; j++) - Curve.getIntersections(values1, values2[j], curve1, curves2[j], - locations); + 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 <= MAX) { + loc._parameter /= 2; + loc._parameter2 = 0.5 + loc._parameter2 / 2; + return true; + } + } + ); + } + } + 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 >= MIN && t <= MAX; + } + ); + } + } + var last = locations.length - 1; + for (var i = last; i >= 0; i--) { + var loc = locations[i], + next = loc._curve.getNext(), + next2 = loc._curve2.getNext(); + if (next && loc._parameter >= MAX) { + loc._parameter = 0; + loc._curve = next; + } + if (next2 && loc._parameter2 >= MAX) { + loc._parameter2 = 0; + loc._curve2 = next2; + } + } + + 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 === 0 ? last : i - 1])) { + locations.splice(i, 1); + last--; + } + } + } + if (_expand) { + for (var i = last; i >= 0; i--) + locations.push(locations[i].getIntersection()); + locations.sort(compare); } return locations; }, @@ -6056,22 +6218,22 @@ var PathItem = Item.extend({ var parts = data.match(/[mlhvcsqtaz][^mlhvcsqtaz]*/ig), coords, relative = false, + previous, control, - current = new Point(); + current = new Point(), + start = new Point(); - function getCoord(index, coord, isCurrent) { - var val = parseFloat(coords[index]); + function getCoord(index, coord) { + var val = +coords[index]; if (relative) val += current[coord]; - if (isCurrent) - current[coord] = val; return val; } - function getPoint(index, isCurrent) { + function getPoint(index) { return new Point( - getCoord(index, 'x', isCurrent), - getCoord(index + 1, 'y', isCurrent) + getCoord(index, 'x'), + getCoord(index + 1, 'y') ); } @@ -6079,24 +6241,31 @@ var PathItem = Item.extend({ for (var i = 0, l = parts.length; i < l; i++) { var part = parts[i], - cmd = part[0], - lower = cmd.toLowerCase(); + command = part[0], + lower = command.toLowerCase(); coords = part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); var length = coords && coords.length; - relative = cmd === lower; + relative = command === lower; + if (previous === 'z' && lower !== 'z') + this.moveTo(current = start); switch (lower) { case 'm': case 'l': + var move = lower === 'm'; + if (move && previous && previous !== 'z') + this.closePath(); for (var j = 0; j < length; j += 2) - this[j === 0 && lower === 'm' ? 'moveTo' : 'lineTo']( - getPoint(j, true)); + 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'; + var coord = lower === 'h' ? 'x' : 'y'; for (var j = 0; j < length; j++) { - getCoord(j, coord, true); + current[coord] = getCoord(j, coord); this.lineTo(current); } control = current; @@ -6106,37 +6275,49 @@ var PathItem = Item.extend({ this.cubicCurveTo( getPoint(j), control = getPoint(j + 2), - getPoint(j + 4, true)); + current = getPoint(j + 4)); } break; case 's': for (var j = 0; j < length; j += 4) { this.cubicCurveTo( - current.multiply(2).subtract(control), + /[cs]/.test(previous) + ? current.multiply(2).subtract(control) + : current, control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); + previous = lower; } break; case 'q': for (var j = 0; j < length; j += 4) { this.quadraticCurveTo( control = getPoint(j), - getPoint(j + 2, true)); + current = getPoint(j + 2)); } break; case 't': for (var j = 0; j < length; j += 2) { this.quadraticCurveTo( - control = current.multiply(2).subtract(control), - getPoint(j, true)); + 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[0], +coords[1]), + +coords[2], +coords[4], +coords[3]); + } break; case 'z': this.closePath(); break; } + previous = lower; } }, @@ -6145,9 +6326,9 @@ var PathItem = Item.extend({ }, _contains: function(point) { - var winding = this._getWinding(point); + var winding = this._getWinding(point, false, true); return !!(this.getWindingRule() === 'evenodd' ? winding & 1 : winding); - } + }, }); @@ -6165,11 +6346,20 @@ var Path = PathItem.extend({ ? typeof arg[0] === 'object' ? arg : arguments - : arg && (arg.point !== undefined && arg.size === undefined - || arg.x !== undefined) + : arg && (arg.size === undefined && (arg.x !== undefined + || arg.point !== undefined)) ? arguments : null; - this.setSegments(segments || []); + 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); }, @@ -6178,30 +6368,34 @@ var Path = PathItem.extend({ }, clone: function(insert) { - var copy = this._clone(new Path({ - segments: this._segments, - insert: false - }), 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 copy; + return this._clone(copy, insert); }, _changed: function _changed(flags) { _changed.base.call(this, flags); if (flags & 4) { - delete this._length; - delete this._clockwise; + (this._compound ? this._parent : this)._currentPath = undefined; + this._length = this._clockwise = undefined; if (this._curves) { for (var i = 0, l = this._curves.length; i < l; i++) this._curves[i]._changed(5); } + this._monoCurves = undefined; } else if (flags & 8) { - delete this._bounds; + this._bounds = undefined; } }, + getStyle: function() { + var parent = this._parent; + return (parent instanceof CompoundPath ? parent : this)._style; + }, + getSegments: function() { return this._segments; }, @@ -6210,8 +6404,9 @@ var Path = PathItem.extend({ var fullySelected = this.isFullySelected(); this._segments.length = 0; this._selectedSegmentState = 0; - delete this._curves; - this._add(Segment.readAll(segments)); + this._curves = undefined; + if (segments && segments.length > 0) + this._add(Segment.readAll(segments)); if (fullySelected) this.setFullySelected(true); }, @@ -6246,9 +6441,8 @@ var Path = PathItem.extend({ return curves[curves.length - 1]; }, - getPathData: function() { + getPathData: function(precision) { var segments = this._segments, - precision = arguments[0], f = Formatter.instance, parts = []; @@ -6310,7 +6504,7 @@ var Path = PathItem.extend({ return true; }, - _applyMatrix: function(matrix) { + _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); @@ -6411,7 +6605,7 @@ var Path = PathItem.extend({ return this.removeSegments(index, index + 1)[0] || null; }, - removeSegments: function(from, to) { + removeSegments: function(from, to, _includeCurves) { from = from || 0; to = Base.pick(to, this._segments.length); var segments = this._segments, @@ -6425,8 +6619,7 @@ var Path = PathItem.extend({ var segment = removed[i]; if (segment._selectionState) this._updateSelection(segment, segment._selectionState, 0); - delete segment._index; - delete segment._path; + segment._index = segment._path = null; } for (var i = from, l = segments.length; i < l; i++) segments[i]._index = i; @@ -6435,7 +6628,7 @@ var Path = PathItem.extend({ ? from - 1 : from, curves = curves.splice(index, amount); - if (arguments[2]) + if (_includeCurves) removed._curves = curves.slice(1); this._adjustCurves(index, index); } @@ -6448,7 +6641,7 @@ var Path = PathItem.extend({ isFullySelected: function() { var length = this._segments.length; return this._selected && length > 0 && this._selectedSegmentState - === length * 4; + === length * 7; }, setFullySelected: function(selected) { @@ -6466,10 +6659,10 @@ var Path = PathItem.extend({ _selectSegments: function(selected) { var length = this._segments.length; this._selectedSegmentState = selected - ? length * 4 : 0; + ? length * 7 : 0; for (var i = 0; i < length; i++) this._segments[i]._selectionState = selected - ? 4 : 0; + ? 7 : 0; }, _updateSelection: function(segment, oldState, newState) { @@ -6492,6 +6685,16 @@ var Path = PathItem.extend({ this.setSegments(segments); }, + reduce: function() { + var curves = this.getCurves(); + for (var i = curves.length - 1; i >= 0; i--) { + var curve = curves[i]; + if (curve.isLinear() && curve.getLength() === 0) + curve.remove(); + } + return this; + }, + simplify: function(tolerance) { if (this._segments.length > 2) { var fitter = new PathFitter(this, tolerance || 2.5); @@ -6509,13 +6712,14 @@ var Path = PathItem.extend({ index = arg.index; parameter = arg.parameter; } - if (parameter >= 1) { + var tolerance = 0.00001; + if (parameter >= 1 - tolerance) { index++; parameter--; } var curves = this.getCurves(); if (index >= 0 && index < curves.length) { - if (parameter > 0) { + if (parameter > tolerance) { curves[index++].divide(parameter, true); } var segs = this.removeSegments(index, this._segments.length, true), @@ -6554,7 +6758,7 @@ var Path = PathItem.extend({ segment._handleOut = handleIn; segment._index = i; } - delete this._curves; + this._curves = null; if (this._clockwise !== undefined) this._clockwise = !this._clockwise; }, @@ -6630,9 +6834,9 @@ var Path = PathItem.extend({ return null; }, - getLocationOf: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(); + getLocationOf: function(point) { + 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) @@ -6652,7 +6856,7 @@ var Path = PathItem.extend({ var start = length, curve = curves[i]; length += curve.getLength(); - if (length >= offset) { + if (length > offset) { return curve.getLocationAt(offset - start); } } @@ -6676,9 +6880,9 @@ var Path = PathItem.extend({ return loc && loc.getNormal(); }, - getNearestLocation: function(point) { - point = Point.read(arguments); - var curves = this.getCurves(), + getNearestLocation: function(point) { + var point = Point.read(arguments), + curves = this.getCurves(), minDist = Infinity, minLoc = null; for (var i = 0, l = curves.length; i < l; i++) { @@ -6691,17 +6895,11 @@ var Path = PathItem.extend({ return minLoc; }, - getNearestPoint: function(point) { - point = Point.read(arguments); + getNearestPoint: function(point) { + var point = Point.read(arguments); return this.getNearestLocation(point).getPoint(); }, - getStyle: function() { - var parent = this._parent; - return (parent && parent._type === 'compound-path' - ? parent : this)._style; - }, - toShape: function(insert) { if (!this._closed) return null; @@ -6769,114 +6967,80 @@ var Path = PathItem.extend({ return null; }, - _getWinding: function(point) { - var closed = this._closed; - if (!closed && !this.hasFill() - || !this._getBounds('getRoughBounds')._containsPoint(point)) - return 0; - var curves = this.getCurves(), - segments = this._segments, - winding = 0, - roots1 = [], - roots2 = [], - last = (closed - ? curves[curves.length - 1] - : new Curve(segments[segments.length - 1]._point, - segments[0]._point)).getValues(), - previous = last; - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i].getValues(), - x = curve[0], - y = curve[1]; - if (!(x === curve[2] && y === curve[3] && x === curve[4] - && y === curve[5] && x === curve[6] && y === curve[7])) { - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - } - if (!closed) { - winding += Curve._getWinding(last, previous, point.x, point.y, - roots1, roots2); - } - return winding; - }, - _hitTest: function(point, options) { - var style = this.getStyle(), + var that = this, + style = this.getStyle(), segments = this._segments, closed = this._closed, - tolerance = options.tolerance, - radius = 0, join, cap, miterLimit, - that = this, - area, loc, res; - - if (options.stroke) { - radius = style.getStrokeWidth() / 2; + tolerancePadding = options._tolerancePadding, + strokePadding = tolerancePadding, + join, cap, miterLimit, + area, loc, res, + hasStroke = options.stroke && style.hasStroke(), + hasFill = options.fill && style.hasFill(), + radius = hasStroke ? style.getStrokeWidth() / 2 + : hasFill ? 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'; } - radius += tolerance; } - function checkPoint(seg, pt, name) { - if (point.getDistance(pt) < tolerance) - return new HitResult(name, that, { segment: seg, point: pt }); + 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) { - var pt = seg._point; - return (ends || options.segments) && checkPoint(seg, pt, 'segment') + return (ends || options.segments) + && checkSegmentPoint(seg, seg._point, 'segment') || (!ends && options.handles) && ( - checkPoint(seg, pt.add(seg._handleIn), 'handle-in') || - checkPoint(seg, pt.add(seg._handleOut), 'handle-out')); + checkSegmentPoint(seg, seg._handleIn, 'handle-in') || + checkSegmentPoint(seg, seg._handleOut, 'handle-out')); } - function addAreaPoint(point) { - area.push(point); - } - - function getAreaCurve(index) { - var p1 = area[index], - p2 = area[(index + 1) % area.length]; - return [p1.x, p1.y, p1.x, p1.y, p2.x, p2.y, p2.x ,p2.y]; - } - - function isInArea(point) { - var length = area.length, - previous = getAreaCurve(length - 1), - roots1 = [], - roots2 = [], - winding = 0; - for (var i = 0; i < length; i++) { - var curve = getAreaCurve(i); - winding += Curve._getWinding(curve, previous, point.x, point.y, - roots1, roots2); - previous = curve; - } - return !!winding; + function addToArea(point) { + area.add(point); } function checkSegmentStroke(segment) { if (join !== 'round' || cap !== 'round') { - area = []; + area = new Path({ internal: true, closed: true }); if (closed || segment._index > 0 && segment._index < segments.length - 1) { if (join !== 'round' && (segment._handleIn.isZero() || segment._handleOut.isZero())) - Path._addSquareJoin(segment, join, radius, miterLimit, - addAreaPoint, true); + Path._addBevelJoin(segment, join, radius, miterLimit, + addToArea, true); } else if (cap !== 'round') { - Path._addSquareCap(segment, cap, radius, addAreaPoint, true); + Path._addSquareCap(segment, cap, radius, addToArea, true); + } + if (!area.isEmpty()) { + var loc; + return area.contains(point) + || (loc = area.getNearestLocation(point)) + && isCloseEnough(loc.getPoint(), tolerancePadding); } - if (area.length > 0) - return isInArea(point); } - return point.getDistance(segment._point) <= radius; + return isCloseEnough(segment._point, strokePadding); } if (options.ends && !options.segments && !closed) { @@ -6884,19 +7048,18 @@ var Path = PathItem.extend({ || checkSegmentPoints(segments[segments.length - 1], true)) return res; } else if (options.segments || options.handles) { - for (var i = 0, l = segments.length; i < l; i++) { + for (var i = 0, l = segments.length; i < l; i++) if (res = checkSegmentPoints(segments[i])) return res; - } } - if (radius > 0) { + if (radius != null) { loc = this.getNearestLocation(point); if (loc) { var parameter = loc.getParameter(); if (parameter === 0 || parameter === 1) { if (!checkSegmentStroke(loc.getSegment())) loc = null; - } else if (loc._distance > radius) { + } else if (!isCloseEnough(loc.getPoint(), strokePadding)) { loc = null; } } @@ -6911,10 +7074,13 @@ var Path = PathItem.extend({ } } } - return !loc && options.fill && this.hasFill() && this._contains(point) + return !loc && hasFill && this._contains(point) || loc && !hasStroke ? new HitResult('fill', this) : loc - ? new HitResult('stroke', this, { location: loc }) + ? new HitResult('stroke', this, { + location: loc, + point: loc.getPoint() + }) : null; } @@ -6942,24 +7108,19 @@ var Path = PathItem.extend({ var segment = segments[i]; segment._transformCoordinates(matrix, coords, false); var state = segment._selectionState, - selected = state & 4, pX = coords[0], pY = coords[1]; - if (selected || (state & 1)) + if (state & 1) drawHandle(2); - if (selected || (state & 2)) + if (state & 2) drawHandle(4); - ctx.save(); - ctx.beginPath(); - ctx.rect(pX - half, pY - half, size, size); - ctx.fill(); - if (!selected) { - ctx.beginPath(); - ctx.rect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillRect(pX - half, pY - half, size, size); + if (!(state & 4)) { + var fillStyle = ctx.fillStyle; ctx.fillStyle = '#ffffff'; - ctx.fill(); + ctx.fillRect(pX - half + 1, pY - half + 1, size - 2, size - 2); + ctx.fillStyle = fillStyle; } - ctx.restore(); } } @@ -7016,14 +7177,14 @@ var Path = PathItem.extend({ for (var i = 0; i < length; i++) drawSegment(i); - if (path._closed && length > 1) + if (path._closed && length > 0) drawSegment(0); } return { _draw: function(ctx, param) { var clip = param.clip, - compound = param.compound; + compound = this._compound = param.compound; if (!compound) ctx.beginPath(); @@ -7038,10 +7199,15 @@ var Path = PathItem.extend({ return dashArray[((i % dashLength) + dashLength) % dashLength]; } - if (hasFill || hasStroke && !dashLength || compound || clip) + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else if (hasFill || hasStroke && !dashLength || compound || clip){ drawSegments(ctx, this); - if (this._closed) - ctx.closePath(); + if (this._closed) + ctx.closePath(); + if (!compound) + this._currentPath = ctx.currentPath; + } if (!clip && !compound && (hasFill || hasStroke)) { this._setStyles(ctx); @@ -7077,8 +7243,7 @@ var Path = PathItem.extend({ ctx.beginPath(); drawSegments(ctx, this, matrix); ctx.stroke(); - drawHandles(ctx, this._segments, matrix, - this._project.options.handleSize || 4); + drawHandles(ctx, this._segments, matrix, paper.settings.handleSize); } }; }, new function() { @@ -7177,16 +7342,17 @@ var Path = PathItem.extend({ }, new function() { function getCurrentSegment(that) { var segments = that._segments; - if (segments.length == 0) + if (segments.length === 0) throw new Error('Use a moveTo() command first'); return segments[segments.length - 1]; } return { moveTo: function() { - if (this._segments.length === 1) + var segments = this._segments; + if (segments.length === 1) this.removeSegment(0); - if (!this._segments.length) + if (!segments.length) this._add([ new Segment(Point.read(arguments)) ]); }, @@ -7235,37 +7401,86 @@ var Path = PathItem.extend({ arcTo: function() { var current = getCurrentSegment(this), from = current._point, - through, to = Point.read(arguments), - clockwise = Base.pick(Base.peek(arguments), true); + 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 { + } else if (Base.remain(arguments) <= 2) { through = to; to = Point.read(arguments); - } - 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), - center = l1.intersect(l2, true), - line = new Line(from, to), - throughSide = line.getSide(through); - if (!center) { - if (!throughSide) + } else { + var radius = Size.read(arguments); + if (radius.isZero()) return this.lineTo(to); - throw new Error('Cannot put an arc through the given points: ' - + [from, through, 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, + EPSILON = 1e-11, + 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) < EPSILON) + 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; } - var vector = from.subtract(center), - extent = vector.getDirectedAngle(to.subtract(center)), - centerSide = line.getSide(center); - if (centerSide == 0) { - extent = throughSide * Math.abs(extent); - } else if (throughSide == centerSide) { - extent -= 360 * (extent < 0 ? -1 : 1); + 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), @@ -7274,13 +7489,27 @@ var Path = PathItem.extend({ z = 4 / 3 * Math.sin(half) / (1 + Math.cos(half)), segments = []; for (var i = 0; i <= count; i++) { - var pt = i < count ? center.add(vector) : to; - var out = i < count ? vector.rotate(90).multiply(z) : null; - if (i == 0) { + 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 { - segments.push( - new Segment(pt, vector.rotate(-90).multiply(z), out)); + 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); } @@ -7331,7 +7560,7 @@ var Path = PathItem.extend({ closePath: function() { var first = this.getFirstSegment(), last = this.getLastSegment(); - if (first._point.equals(last._point)) { + if (first !== last && first._point.equals(last._point)) { first.setHandleIn(last._handleIn); last.remove(); } @@ -7390,29 +7619,11 @@ statics: { }, getStrokeBounds: function(segments, closed, style, matrix) { - function getPenPadding(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)]; - } - if (!style.hasStroke()) return Path.getBounds(segments, closed, style, matrix); var length = segments.length - (closed ? 0 : 1), radius = style.getStrokeWidth() / 2, - padding = getPenPadding(radius, matrix), + padding = Path._getPenPadding(radius, matrix), bounds = Path.getBounds(segments, closed, style, matrix, padding), join = style.getStrokeJoin(), cap = style.getStrokeCap(), @@ -7424,25 +7635,27 @@ statics: { ? matrix._transformPoint(point, point) : point); } + function addRound(segment) { + bounds = bounds.unite(joinBounds.setCenter(matrix + ? matrix._transformPoint(segment._point) : segment._point)); + } + function addJoin(segment, join) { - if (join === 'round' || !segment._handleIn.isZero() - && !segment._handleOut.isZero()) { - bounds = bounds.unite(joinBounds.setCenter(matrix - ? matrix._transformPoint(segment._point) : segment._point)); + var handleIn = segment._handleIn, + handleOut = segment._handleOut + if (join === 'round' || !handleIn.isZero() && !handleOut.isZero() + && handleIn.isColinear(handleOut)) { + addRound(segment); } else { - Path._addSquareJoin(segment, join, radius, miterLimit, add); + Path._addBevelJoin(segment, join, radius, miterLimit, add); } } function addCap(segment, cap) { - switch (cap) { - case 'round': - addJoin(segment, cap); - break; - case 'butt': - case 'square': + if (cap === 'round') { + addRound(segment); + } else { Path._addSquareCap(segment, cap, radius, add); - break; } } @@ -7450,14 +7663,32 @@ statics: { addJoin(segments[i], join); if (closed) { addJoin(segments[0], join); - } else { + } else if (length > 0) { addCap(segments[0], cap); addCap(segments[segments.length - 1], cap); } return bounds; }, - _addSquareJoin: function(segment, join, radius, miterLimit, addPoint, area) { + _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), @@ -7510,19 +7741,19 @@ statics: { x2 = -x1, y1 = x1, y2 = x2; - strokePadding = strokePadding / 2 || 0; - joinPadding = joinPadding / 2 || 0; 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 - padding, - xx = x + padding, - yn = y - padding, - yx = y + padding; + 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; @@ -7533,24 +7764,23 @@ statics: { }, getRoughBounds: function(segments, closed, style, matrix) { - var strokeWidth = style.getStrokeColor() ? style.getStrokeWidth() : 0, - joinWidth = strokeWidth; - if (strokeWidth === 0) { - strokeWidth = 0.00001; - } else { + var strokeRadius = style.hasStroke() ? style.getStrokeWidth() / 2 : 0, + joinRadius = strokeRadius; + if (strokeRadius > 0) { if (style.getStrokeJoin() === 'miter') - joinWidth = strokeWidth * style.getMiterLimit(); + joinRadius = strokeRadius * style.getMiterLimit(); if (style.getStrokeCap() === 'square') - joinWidth = Math.max(joinWidth, strokeWidth * Math.sqrt(2)); + joinRadius = Math.max(joinRadius, strokeRadius * Math.sqrt(2)); } return Path.getHandleBounds(segments, closed, style, matrix, - strokeWidth, joinWidth); + Path._getPenPadding(strokeRadius, matrix), + Path._getPenPadding(joinRadius, matrix)); } }}); Path.inject({ statics: new function() { - var kappa = Numerical.KAPPA, + var kappa = 0.5522847498307936, ellipseSegments = [ new Segment([-1, 0], [0, kappa ], [0, -kappa]), new Segment([0, -1], [-kappa, 0], [kappa, 0 ]), @@ -7558,9 +7788,16 @@ Path.inject({ statics: new function() { 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 path = new Path(), - segments = new Array(4); + var segments = new Array(4); for (var i = 0; i < 4; i++) { var segment = ellipseSegments[i]; segments[i] = new Segment( @@ -7569,17 +7806,15 @@ Path.inject({ statics: new function() { segment._handleOut.multiply(radius) ); } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(args)); + return createPath(segments, true, args); } return { Line: function() { - return new Path( - Point.readNamed(arguments, 'from'), - Point.readNamed(arguments, 'to') - ).set(Base.getNamed(arguments)); + return createPath([ + new Segment(Point.readNamed(arguments, 'from')), + new Segment(Point.readNamed(arguments, 'to')) + ], false, arguments); }, Circle: function() { @@ -7590,27 +7825,27 @@ Path.inject({ statics: new function() { Rectangle: function() { var rect = Rectangle.readNamed(arguments, 'rectangle'), - radius = Size.readNamed(arguments, 'radius', 0, 0, + radius = Size.readNamed(arguments, 'radius', 0, { readNull: true }), bl = rect.getBottomLeft(true), tl = rect.getTopLeft(true), tr = rect.getTopRight(true), - br = rect.getBottomRight(true); - path = new Path(); + br = rect.getBottomRight(true), + segments; if (!radius || radius.isZero()) { - path._add([ + 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; - path._add([ + 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]), @@ -7619,10 +7854,9 @@ Path.inject({ statics: new function() { 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]) - ]); + ]; } - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, RoundRectangle: '#Rectangle', @@ -7638,29 +7872,27 @@ Path.inject({ statics: new function() { var from = Point.readNamed(arguments, 'from'), through = Point.readNamed(arguments, 'through'), to = Point.readNamed(arguments, 'to'), - path = new Path(); + props = Base.getNamed(arguments), + path = new Path(props && props.insert === false + && Item.NO_INSERT); path.moveTo(from); path.arcTo(through, to); - return path.set(Base.getNamed(arguments)); + return path.set(props); }, RegularPolygon: function() { var center = Point.readNamed(arguments, 'center'), sides = Base.readNamed(arguments, 'sides'), radius = Base.readNamed(arguments, 'radius'), - path = new Path(), 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++) { + for (var i = 0; i < sides; i++) segments[i] = new Segment(center.add( vector.rotate((i + offset) * step))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + return createPath(segments, true, arguments); }, Star: function() { @@ -7668,17 +7900,13 @@ Path.inject({ statics: new function() { points = Base.readNamed(arguments, 'points') * 2, radius1 = Base.readNamed(arguments, 'radius1'), radius2 = Base.readNamed(arguments, 'radius2'), - path = new Path(), 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))); - } - path._add(segments); - path._closed = true; - return path.set(Base.getNamed(arguments)); + 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); } }; }}); @@ -7692,8 +7920,19 @@ var CompoundPath = PathItem.extend({ initialize: function CompoundPath(arg) { this._children = []; this._namedChildren = {}; - if (!this._initialize(arg)) - this.addChildren(Array.isArray(arg) ? arg : arguments); + if (!this._initialize(arg)) { + if (typeof arg === 'string') { + this.setPathData(arg); + } else { + this.addChildren(Array.isArray(arg) ? arg : arguments); + } + } + }, + + _changed: function _changed(flags) { + _changed.base.call(this, flags); + if (flags & (2 | 4)) + this._currentPath = undefined; }, insertChildren: function insertChildren(index, items, _preserve) { @@ -7741,7 +7980,7 @@ var CompoundPath = PathItem.extend({ var children = this._children, curves = []; for (var i = 0, l = children.length; i < l; i++) - curves = curves.concat(children[i].getCurves()); + curves.push.apply(curves, children[i].getCurves()); return curves; }, @@ -7763,36 +8002,18 @@ var CompoundPath = PathItem.extend({ return area; }, - getPathData: function() { + getPathData: function(precision) { var children = this._children, paths = []; for (var i = 0, l = children.length; i < l; i++) - paths.push(children[i].getPathData(arguments[0])); + paths.push(children[i].getPathData(precision)); return paths.join(' '); }, - _getWinding: function(point) { - var children = this._children, - winding = 0; - for (var i = 0, l = children.length; i < l; i++) - winding += children[i]._getWinding(point); - return winding; - }, - - _hitTest : function _hitTest(point, options) { - var res = _hitTest.base.call(this, point, - new Base(options, { fill: false })); - if (!res) { - if (options.compoundChildren) { - var children = this._children; - for (var i = children.length - 1; i >= 0 && !res; i--) - res = children[i]._hitTest(point, options); - } else if (options.fill && this.hasFill() - && this._contains(point)) { - res = new HitResult('fill', this); - } - } - return res; + _getChildHitTestOptions: function(options) { + return options.type === 'path' + ? options + : new Base(options, { fill: false }); }, _draw: function(ctx, param) { @@ -7800,10 +8021,15 @@ var CompoundPath = PathItem.extend({ if (children.length === 0) return; - ctx.beginPath(); - param = param.extend({ compound: true }); - for (var i = 0, l = children.length; i < l; i++) - children[i].draw(ctx, param); + if (this._currentPath) { + ctx.currentPath = this._currentPath; + } else { + ctx.beginPath(); + param = param.extend({ compound: true }); + for (var i = 0, l = children.length; i < l; i++) + children[i].draw(ctx, param); + this._currentPath = ctx.currentPath; + } if (!param.clip) { this._setStyles(ctx); @@ -7817,26 +8043,31 @@ var CompoundPath = PathItem.extend({ } } }, new function() { - function getCurrentPath(that) { - if (!that._children.length) + function getCurrentPath(that, check) { + var children = that._children; + if (check && children.length === 0) throw new Error('Use a moveTo() command first'); - return that._children[that._children.length - 1]; + return children[children.length - 1]; } var fields = { moveTo: function() { - var path = new Path(); - this.addChild(path); + var current = getCurrentPath(this), + path = current && current.isEmpty() ? current : new Path(); + if (path !== current) + this.addChild(path); path.moveTo.apply(path, arguments); }, moveBy: function() { - this.moveTo(getCurrentPath(this).getLastSegment()._point.add( - Point.read(arguments))); + var current = getCurrentPath(this, true), + last = current && current.getLastSegment(), + point = Point.read(arguments); + this.moveTo(last ? point.add(last._point) : point); }, closePath: function() { - getCurrentPath(this).closePath(); + getCurrentPath(this, true).closePath(); } }; @@ -7844,7 +8075,7 @@ var CompoundPath = PathItem.extend({ 'lineBy', 'cubicCurveBy', 'quadraticCurveBy', 'curveBy', 'arcBy'], function(key) { fields[key] = function() { - var path = getCurrentPath(this); + var path = getCurrentPath(this, true); path[key].apply(path, arguments); }; } @@ -7853,6 +8084,433 @@ var CompoundPath = PathItem.extend({ return fields; }); +PathItem.inject(new function() { + function reorientPath(path) { + path = path.clone(false).reduce().transform(null, true); + if (path instanceof CompoundPath) { + var children = path.removeChildren(), + length = children.length, + bounds = new Array(length), + counters = new Array(length), + clockwise; + children.sort(function(a, b) { + return b.getBounds().getArea() - a.getBounds().getArea(); + }); + path.addChildren(children); + clockwise = children[0].isClockwise(); + for (var i = 0; i < length; i++) { + bounds[i] = children[i].getBounds(); + counters[i] = 0; + } + for (var i = 0; i < length; i++) { + for (var j = 1; j < length; j++) { + if (i !== j && bounds[i].intersects(bounds[j])) + counters[j]++; + } + if (i > 0 && counters[i] % 2 === 0) + children[i].setClockwise(clockwise); + } + } + return path; + } + + function computeBoolean(path1, path2, operator, subtract) { + var _path1 = reorientPath(path1); + _path2 = path2 && path1 !== path2 && reorientPath(path2); + if (!_path1.isClockwise()) + _path1.reverse(); + if (_path2 && !(subtract ^ _path2.isClockwise())) + _path2.reverse(); + splitPath(_path1.getIntersections(_path2, true)); + + var chain = [], + windings = [], + lengths = [], + 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]); + 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 = segments.length; i < l; i++) { + var segment = segments[i]; + if (segment._winding != null) + continue; + chain.length = windings.length = lengths.length = 0; + var totalLength = 0, + startSeg = segment; + do { + chain.push(segment); + lengths.push(totalLength += segment.getCurve().getLength()); + segment = segment.getNext(); + } while (segment && !segment._intersection && segment !== startSeg); + for (var j = 0; j < 3; j++) { + var length = totalLength * Math.random(), + amount = lengths.length; + k = 0; + do { + if (lengths[k] >= length) { + if (k > 0) + length -= lengths[k - 1]; + break; + } + } while (++k < amount); + var curve = chain[k].getCurve(), + point = curve.getPointAt(length), + hor = curve.isHorizontal(), + path = curve._path; + if (path._parent instanceof CompoundPath) + path = path._parent; + windings[j] = subtract && _path2 + && (path === _path1 && _path2._getWinding(point, hor) + || path === _path2 && !_path1._getWinding(point, hor)) + ? 0 + : getWinding(point, monoCurves, hor); + } + windings.sort(); + var winding = windings[1]; + for (var j = chain.length - 1; j >= 0; j--) + chain[j]._winding = winding; + } + var result = new CompoundPath(); + result.addChildren(tracePaths(segments, operator), true); + _path1.remove(); + if (_path2) + _path2.remove(); + return result.reduce(); + } + + function splitPath(intersections) { + var TOLERANCE = 0.00001, + linearSegments; + + function resetLinear() { + for (var i = 0, l = linearSegments.length; i < l; i++) { + var segment = linearSegments[i]; + segment._handleOut.set(0, 0); + segment._handleIn.set(0, 0); + } + } + + for (var i = intersections.length - 1, curve, prevLoc; i >= 0; i--) { + var loc = intersections[i], + t = loc._parameter; + if (prevLoc && prevLoc._curve === loc._curve + && prevLoc._parameter > 0) { + t /= prevLoc._parameter; + } else { + if (linearSegments) + resetLinear(); + curve = loc._curve; + linearSegments = curve.isLinear() && []; + } + var newCurve, + segment; + if (newCurve = curve.divide(t, true, true)) { + segment = newCurve._segment1; + curve = newCurve.getPrevious(); + } else { + segment = t < TOLERANCE + ? curve._segment1 + : t > 1 - TOLERANCE + ? curve._segment2 + : curve.getPartLength(0, t) < curve.getPartLength(t, 1) + ? curve._segment1 + : curve._segment2; + } + segment._intersection = loc.getIntersection(); + loc._segment = segment; + if (linearSegments) + linearSegments.push(segment); + prevLoc = loc; + } + if (linearSegments) + resetLinear(); + } + + function getWinding(point, curves, horizontal, testContains) { + var TOLERANCE = 0.00001, + x = point.x, + y = point.y, + windLeft = 0, + windRight = 0, + roots = [], + abs = Math.abs, + MAX = 1 - TOLERANCE; + if (horizontal) { + var yTop = -Infinity, + yBottom = Infinity, + yBefore = y - TOLERANCE, + yAfter = y + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var values = curves[i].values; + if (Curve.solveCubic(values, 0, x, roots, 0, 1) > 0) { + for (var j = roots.length - 1; j >= 0; j--) { + var y0 = Curve.evaluate(values, roots[j], 0).y; + if (y0 < yBefore && y0 > yTop) { + yTop = y0; + } else if (y0 > yAfter && y0 < yBottom) { + yBottom = y0; + } + } + } + } + yTop = (yTop + y) / 2; + yBottom = (yBottom + y) / 2; + if (yTop > -Infinity) + windLeft = getWinding(new Point(x, yTop), curves); + if (yBottom < Infinity) + windRight = getWinding(new Point(x, yBottom), curves); + } else { + var xBefore = x - TOLERANCE, + xAfter = x + TOLERANCE; + for (var i = 0, l = curves.length; i < l; i++) { + var curve = curves[i], + values = curve.values, + winding = curve.winding, + next = curve.next; + if (winding && (winding === 1 + && y >= values[1] && y <= values[7] + || y >= values[7] && y <= values[1]) + && Curve.solveCubic(values, 1, y, roots, 0, + !next.winding && next.values[1] === y ? 1 : MAX) === 1){ + var t = roots[0], + x0 = Curve.evaluate(values, t, 0).x, + slope = Curve.evaluate(values, t, 1).y; + if (abs(slope) < TOLERANCE && !Curve.isLinear(values) + || t < TOLERANCE && slope * Curve.evaluate( + curve.previous.values, t, 1).y < 0) { + if (testContains && x0 >= xBefore && x0 <= xAfter) { + ++windLeft; + ++windRight; + } + } else if (x0 <= xBefore) { + windLeft += winding; + } else if (x0 >= xAfter) { + windRight += winding; + } + } + } + } + return Math.max(abs(windLeft), abs(windRight)); + } + + function tracePaths(segments, operator, selfOp) { + operator = operator || function() { + return true; + }; + var paths = [], + ZERO = 1e-3, + ONE = 1 - 1e-3; + for (var i = 0, seg, startSeg, l = segments.length; i < l; i++) { + seg = startSeg = segments[i]; + if (seg._visited || !operator(seg._winding)) + 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) { + var c1 = seg.getCurve(); + if (dir > 0) + c1 = c1.getPrevious(); + var t1 = c1.getTangentAt(dir < 1 ? ZERO : ONE, true), + c4 = interSeg.getCurve(), + c3 = c4.getPrevious(), + t3 = c3.getTangentAt(ONE, true), + t4 = c4.getTangentAt(ZERO, true), + w3 = t1.cross(t3), + w4 = t1.cross(t4); + if (selfOp) { + seg._visited = interSeg._visited; + seg = interSeg; + dir = 1; + } else 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; + } + handleOut = dir > 0 ? seg._handleOut : seg._handleIn; + } + path.add(new Segment(seg._point, added && handleIn, handleOut)); + added = true; + 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); + } + if (path._segments.length > + (path._closed ? path.isPolygon() ? 2 : 0 : 1)) + paths.push(path); + } + return paths; + } + + return { + _getWinding: function(point, horizontal, testContains) { + return getWinding(point, this._getMonoCurves(), + horizontal, testContains); + }, + + unite: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1 || w === 0; + }, false); + }, + + intersect: function(path) { + return computeBoolean(this, path, function(w) { + return w === 2; + }, false); + }, + + subtract: function(path) { + return computeBoolean(this, path, function(w) { + return w === 1; + }, true); + }, + + exclude: function(path) { + return new Group([this.subtract(path), path.subtract(this)]); + }, + divide: function(path) { + return new Group([this.subtract(path), this.intersect(path)]); + } + }; +}); + +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.isLinear(v)) { + insertCurve(v); + } else { + var a = 3 * (y1 - y2) - y0 + y3, + b = 2 * (y0 + y2) - 4 * y1, + c = y1 - y0, + TOLERANCE = 0.00001, + roots = []; + var count = Numerical.solveQuadratic(a, b, c, roots, TOLERANCE, + 1 - TOLERANCE); + if (count === 0) { + insertCurve(v); + } else { + roots.sort(); + var t = roots[0], + parts = Curve.subdivide(v, t); + insertCurve(parts[0]); + if (count > 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]); + } + var first = monoCurves[0], + last = monoCurves[monoCurves.length - 1]; + first.previous = last; + last.next = first; + } + return monoCurves; + } +}); + +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; + } +}); + var PathFlattener = Base.extend({ initialize: function(path) { this.curves = []; @@ -8136,186 +8794,11 @@ var PathFitter = Base.extend({ } }); -PathItem.inject(new function() { - - function splitPath(intersections, collectOthers) { - intersections.sort(function(loc1, loc2) { - var path1 = loc1.getPath(), - path2 = loc2.getPath(); - return path1 === path2 - ? (loc1.getIndex() + loc1.getParameter()) - - (loc2.getIndex() + loc2.getParameter()) - : path1._id - path2._id; - }); - var others = collectOthers && []; - for (var i = intersections.length - 1; i >= 0; i--) { - var loc = intersections[i], - other = loc.getIntersection(), - curve = loc.divide(), - segment = curve && curve.getSegment1() || loc.getSegment(); - if (others) - others.push(other); - segment._intersection = other; - loc._segment = segment; - } - return others; - } - - function reorientPath(path) { - if (path instanceof CompoundPath) { - var children = path.removeChildren(), - length = children.length, - bounds = new Array(length), - counters = new Array(length), - clockwise; - children.sort(function(a, b){ - var b1 = a.getBounds(), b2 = b.getBounds(); - return b1._width * b1._height < b2._width * b2._height; - }); - path.addChildren(children); - clockwise = children[0].isClockwise(); - for (var i = 0; i < length; i++) { - bounds[i] = children[i].getBounds(); - counters[i] = 0; - } - for (var i = 0; i < length; i++) { - for (var j = 1; j < length; j++) { - if (i !== j && bounds[i].contains(bounds[j])) - counters[j]++; - } - if (i > 0 && counters[i] % 2 === 0) - children[i].setClockwise(clockwise); - } - } - return path; - } - - function computeBoolean(path1, path2, operator, subtract) { - path1 = reorientPath(path1.clone(false)); - path2 = reorientPath(path2.clone(false)); - var path1Clockwise = path1.isClockwise(), - path2Clockwise = path2.isClockwise(), - intersections = path1.getIntersections(path2); - splitPath(splitPath(intersections, true)); - if (!path1Clockwise) - path1.reverse(); - if (!(subtract ^ path2Clockwise)) - path2.reverse(); - path1Clockwise = true; - path2Clockwise = !subtract; - var paths = [] - .concat(path1._children || [path1]) - .concat(path2._children || [path2]), - segments = [], - result = new CompoundPath(); - for (var i = 0, l = paths.length; i < l; i++) { - var path = paths[i], - parent = path._parent, - clockwise = path.isClockwise(), - segs = path._segments; - path = parent instanceof CompoundPath ? parent : path; - for (var j = segs.length - 1; j >= 0; j--) { - var segment = segs[j], - midPoint = segment.getCurve().getPoint(0.5), - insidePath1 = path !== path1 && path1.contains(midPoint) - && (clockwise === path1Clockwise || subtract - || !testOnCurve(path1, midPoint)), - insidePath2 = path !== path2 && path2.contains(midPoint) - && (clockwise === path2Clockwise - || !testOnCurve(path2, midPoint)); - if (operator(path === path1, insidePath1, insidePath2)) { - segment._invalid = true; - } else { - segments.push(segment); - } - } - } - for (var i = 0, l = segments.length; i < l; i++) { - var segment = segments[i]; - if (segment._visited) - continue; - var path = new Path(), - loc = segment._intersection, - intersection = loc && loc.getSegment(true); - if (segment.getPrevious()._invalid) - segment.setHandleIn(intersection - ? intersection._handleIn - : new Point(0, 0)); - do { - segment._visited = true; - if (segment._invalid && segment._intersection) { - var inter = segment._intersection.getSegment(true); - path.add(new Segment(segment._point, segment._handleIn, - inter._handleOut)); - inter._visited = true; - segment = inter; - } else { - path.add(segment.clone()); - } - segment = segment.getNext(); - } while (segment && !segment._visited && segment !== intersection); - var amount = path._segments.length; - if (amount > 1 && (amount > 2 || !path.isPolygon())) { - path.setClosed(true); - result.addChild(path, true); - } else { - path.remove(); - } - } - path1.remove(); - path2.remove(); - return result.reduce(); - } - - function testOnCurve(path, point) { - var curves = path.getCurves(), - bounds = path.getBounds(); - if (bounds.contains(point)) { - for (var i = 0, l = curves.length; i < l; i++) { - var curve = curves[i]; - if (curve.getBounds().contains(point) - && curve.getParameterOf(point)) - return true; - } - } - return false; - } - - return { - unite: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isInPath1 || isInPath2; - }); - }, - - intersect: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return !(isInPath1 || isInPath2); - }); - }, - - subtract: function(path) { - return computeBoolean(this, path, - function(isPath1, isInPath1, isInPath2) { - return isPath1 && isInPath2 || !isPath1 && !isInPath1; - }, true); - }, - - exclude: function(path) { - return new Group([this.subtract(path), path.subtract(this)]); - }, - - divide: function(path) { - return new Group([this.subtract(path), this.intersect(path)]); - } - }; -}); - var TextItem = Item.extend({ _class: 'TextItem', _boundsSelected: true, + _applyMatrix: false, + _canApplyMatrix: false, _serializeFields: { content: null }, @@ -8367,7 +8850,7 @@ var PointText = TextItem.extend({ }, clone: function(insert) { - return this._clone(new PointText({ insert: false }), insert); + return this._clone(new PointText(Item.NO_INSERT), insert); }, getPoint: function() { @@ -8375,8 +8858,8 @@ var PointText = TextItem.extend({ return new LinkedPoint(point.x, point.y, this, 'setPoint'); }, - setPoint: function(point) { - point = Point.read(arguments); + setPoint: function() { + var point = Point.read(arguments); this.translate(point.subtract(this._matrix.getTranslation())); }, @@ -8457,7 +8940,7 @@ var Color = Base.extend(new function() { } else if (match = string.match(/^rgba?\((.*)\)$/)) { components = match[1].split(','); for (var i = 0, l = components.length; i < l; i++) { - var value = parseFloat(components[i]); + var value = +components[i]; components[i] = i < 3 ? value / 255 : value; } } else { @@ -8504,8 +8987,8 @@ var Color = Base.extend(new function() { }, 'hsb-rgb': function(h, s, b) { - var h = (h / 60) % 6, - i = Math.floor(h), + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), f = h - i, i = hsbIndices[i], v = [ @@ -8534,7 +9017,7 @@ var Color = Base.extend(new function() { }, 'hsl-rgb': function(h, s, l) { - h /= 360; + h = (((h / 360) % 1) + 1) % 1; if (s === 0) return [l, l, l]; var t3s = [ h + 1 / 3, h, h - 1 / 3 ], @@ -8590,9 +9073,8 @@ var Color = Base.extend(new function() { parser = componentParsers[type][index] = name === 'gradient' ? function(value) { var current = this._components[0]; - value = Gradient.read( - Array.isArray(value) ? value : arguments, - 0, 0, { readNull: true }); + value = Gradient.read(Array.isArray(value) ? value + : arguments, 0, { readNull: true }); if (current !== value) { if (current) current._removeOwner(this); @@ -8601,22 +9083,16 @@ var Color = Base.extend(new function() { } return value; } - : name === 'hue' - ? function(value) { - return isNaN(value) ? 0 - : ((value % 360) + 360) % 360; + : type === 'gradient' + ? function() { + return Point.read(arguments, 0, { + readNull: name === 'highlight', + clone: true + }); } - : type === 'gradient' - ? function() { - return Point.read(arguments, 0, 0, { - readNull: name === 'highlight', - clone: true - }); - } - : function(value) { - return isNaN(value) ? 0 - : Math.min(Math.max(value, 0), 1); - }; + : function(value) { + return value == null || isNaN(value) ? 0 : value; + }; this['get' + part] = function() { return this._type === type @@ -8647,7 +9123,6 @@ var Color = Base.extend(new function() { var slice = Array.prototype.slice, args = arguments, read = 0, - parse = true, type, components, alpha, @@ -8671,7 +9146,6 @@ var Color = Base.extend(new function() { } } if (!components) { - parse = !(this.__options && this.__options.dontParse); values = argType === 'number' ? args : argType === 'object' && arg.length != null @@ -8724,7 +9198,7 @@ var Color = Base.extend(new function() { ? 'gray' : 'rgb'; var properties = types[type]; - parsers = parse && componentParsers[type]; + parsers = componentParsers[type]; this._components = components = []; for (var i = 0, l = properties.length; i < l; i++) { var value = arg[properties[i]]; @@ -8735,8 +9209,7 @@ var Color = Base.extend(new function() { radial: arg.radial }; } - if (parse) - value = parsers[i].call(this, value); + value = parsers[i].call(this, value); if (value != null) components[i] = value; } @@ -8753,9 +9226,7 @@ var Color = Base.extend(new function() { this._components = components = []; var parsers = componentParsers[this._type]; for (var i = 0, l = parsers.length; i < l; i++) { - var value = values && values[i]; - if (parse) - value = parsers[i].call(this, value); + var value = parsers[i].call(this, values && values[i]); if (value != null) components[i] = value; } @@ -8790,7 +9261,7 @@ var Color = Base.extend(new function() { ? converter.apply(this, this._components) : converters['rgb-' + type].apply(this, converters[this._type + '-rgb'].apply(this, - this._components)); + this._components)); }, convert: function(type) { @@ -8828,12 +9299,13 @@ var Color = Base.extend(new function() { }, equals: function(color) { - if (Base.isPlainValue(color)) - color = Color.read(arguments); - return color === this || color && this._class === color._class - && this._type === color._type - && this._alpha === color._alpha - && Base.equals(this._components, color._components) + var col = Base.isPlainValue(color) + ? 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; }, @@ -8856,13 +9328,16 @@ var Color = Base.extend(new function() { 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 = [ - Math.round(components[0] * 255), - Math.round(components[1] * 255), - Math.round(components[2] * 255) + convert(components[0]), + convert(components[1]), + convert(components[2]) ]; if (alpha < 1) - components.push(alpha); + components.push(alpha < 0 ? 0 : alpha); return hex ? '#' + ((1 << 24) + (components[0] << 16) + (components[1] << 8) @@ -8926,46 +9401,32 @@ var Color = Base.extend(new function() { } }); }, new function() { - function clamp(value, hue) { - return value < 0 - ? 0 - : hue && value > 360 - ? 360 - : !hue && value > 1 - ? 1 - : value; - } - var operators = { - add: function(a, b, hue) { - return clamp(a + b, hue); + add: function(a, b) { + return a + b; }, - subtract: function(a, b, hue) { - return clamp(a - b, hue); + subtract: function(a, b) { + return a - b; }, - multiply: function(a, b, hue) { - return clamp(a * b, hue); + multiply: function(a, b) { + return a * b; }, - divide: function(a, b, hue) { - return clamp(a / b, hue); + divide: function(a, b) { + return a / b; } }; return Base.each(operators, function(operator, name) { - var options = { dontParse: /^(multiply|divide)$/.test(name) }; - this[name] = function(color) { - color = Color.read(arguments, 0, 0, options); + color = Color.read(arguments); var type = this._type, - properties = this._properties, components1 = this._components, components2 = color._convert(type); for (var i = 0, l = components1.length; i < l; i++) - components2[i] = operator(components1[i], components2[i], - properties[i] === 'hue'); + components2[i] = operator(components1[i], components2[i]); return new Color(type, components2, this._alpha != null ? operator(this._alpha, color.getAlpha()) @@ -9030,7 +9491,7 @@ var Gradient = Base.extend({ if (index != -1) { this._owners.splice(index, 1); if (this._owners.length === 0) - delete this._owners; + this._owners = undefined; } }, @@ -9048,7 +9509,7 @@ var Gradient = Base.extend({ setStops: function(stops) { if (this.stops) { for (var i = 0, l = this._stops.length; i < l; i++) - delete this._stops[i]._owner; + this._stops[i]._owner = undefined; } if (stops.length < 2) throw new Error( @@ -9167,8 +9628,10 @@ var Style = Base.extend(new function() { shadowBlur: 0, shadowOffset: new Point(), selectedColor: undefined, - font: 'sans-serif', + fontFamily: 'sans-serif', + fontWeight: 'normal', fontSize: 12, + font: 'sans-serif', leading: null, justification: 'left' }; @@ -9178,8 +9641,10 @@ var Style = Base.extend(new function() { strokeCap: 25, strokeJoin: 25, miterLimit: 25, - font: 5, + fontFamily: 5, + fontWeight: 5, fontSize: 5, + font: 5, leading: 5, justification: 5 }; @@ -9210,7 +9675,7 @@ var Style = Base.extend(new function() { if (old != value) { if (isColor) { if (old) - delete old._owner; + old._owner = undefined; if (value && value.constructor === Color) { if (value._owner) value = value.clone(); @@ -9224,11 +9689,11 @@ var Style = Base.extend(new function() { } }; - fields[get] = function() { + fields[get] = function(_dontMerge) { var value, children = this._item && this._item._children; - if (!children || children.length === 0 || arguments[0] - || this._item._type === 'compound-path') { + if (!children || children.length === 0 || _dontMerge + || this._item instanceof CompoundPath) { var value = this._values[key]; if (value === undefined) { value = this._defaults[key]; @@ -9236,8 +9701,8 @@ var Style = Base.extend(new function() { value = value.clone(); this._values[key] = value; } else if (isColor && !(value && value.constructor === Color)) { - this._values[key] = value = Color.read( - [value], 0, 0, { readNull: true, clone: true }); + this._values[key] = value = Color.read([value], 0, + { readNull: true, clone: true }); if (value) value._owner = this._item; } @@ -9309,14 +9774,20 @@ var Style = Base.extend(new function() { return !!this.getShadowColor() && this.getShadowBlur() > 0; }, + getFontStyle: function() { + var size = this.getFontSize(); + return this.getFontWeight() + + ' ' + size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + + this.getFontFamily(); + }, + + getFont: '#getFontFamily', + + setFont: '#setFontFamily', + getLeading: function getLeading() { var leading = getLeading.base.call(this); return leading != null ? leading : this.getFontSize() * 1.2; - }, - - getFontStyle: function() { - var size = this.getFontSize(); - return size + (/[a-z]/i.test(size + '') ? ' ' : 'px ') + this.getFont(); } }); @@ -9347,6 +9818,23 @@ var DomElement = new function() { return res; } + 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 { create: function(nodes, parent) { var isArray = Array.isArray(nodes), @@ -9488,13 +9976,17 @@ var DomElement = new function() { this.getBounds(el, true)); }, - getPrefixValue: function(el, name) { - var value = el[name], - prefixes = ['webkit', 'moz', 'ms', 'o'], - suffix = name[0].toUpperCase() + name.substring(1); - for (var i = 0; i < 4 && value == null; i++) - value = el[prefixes[i] + suffix]; - return value; + 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); + } } }; }; @@ -9502,25 +9994,19 @@ var DomElement = new function() { var DomEvent = { add: function(el, events) { for (var type in events) { - var func = events[type]; - if (el.addEventListener) { - el.addEventListener(type, func, false); - } else if (el.attachEvent) { - el.attachEvent('on' + type, func.bound = function() { - func.call(el, window.event); - }); - } + 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]; - if (el.removeEventListener) { - el.removeEventListener(type, func, false); - } else if (el.detachEvent) { - el.detachEvent('on' + type, func.bound); - } + 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); } }, @@ -9540,36 +10026,23 @@ var DomEvent = { 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))); }, - preventDefault: function(event) { - if (event.preventDefault) { - event.preventDefault(); - } else { - event.returnValue = false; - } - }, - - stopPropagation: function(event) { - if (event.stopPropagation) { - event.stopPropagation(); - } else { - event.cancelBubble = true; - } - }, - stop: function(event) { - DomEvent.stopPropagation(event); - DomEvent.preventDefault(event); + event.stopPropagation(); + event.preventDefault(); } }; DomEvent.requestAnimationFrame = new function() { - var nativeRequest = DomElement.getPrefixValue(window, - 'requestAnimationFrame'), + var nativeRequest = DomElement.getPrefixed(window, 'requestAnimationFrame'), requested = false, callbacks = [], focused = true, @@ -9625,16 +10098,27 @@ var View = Base.extend(Callback, { this._project = paper.project; 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._viewHandlers); + 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)' + }); if (PaperScope.hasAttribute(element, 'resize')) { var offset = DomElement.getOffset(element, true), that = this; size = DomElement.getViewportBounds(element) .getSize().subtract(offset); - this._windowHandlers = { + this._windowEvents = { resize: function() { if (!DomElement.isInvisible(element)) offset = DomElement.getOffset(element, true); @@ -9642,7 +10126,7 @@ var View = Base.extend(Callback, { .getSize().subtract(offset)); } }; - DomEvent.add(window, this._windowHandlers); + DomEvent.add(window, this._windowEvents); } else { size = DomElement.getSize(element); if (size.isNaN() || size.isZero()) @@ -9675,16 +10159,17 @@ var View = Base.extend(Callback, { remove: function() { if (!this._project) return false; - if (View._focused == this) + 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._viewHandlers); - DomEvent.remove(window, this._windowHandlers); + DomEvent.remove(this._element, this._viewEvents); + DomEvent.remove(window, this._windowEvents); this._element = this._project = null; this.detach('frame'); + this._animate = false; this._frameItems = {}; return true; }, @@ -9692,13 +10177,11 @@ var View = Base.extend(Callback, { _events: { onFrame: { install: function() { - this._animate = true; - if (!this._requested) - this._requestFrame(); + this.play(); }, uninstall: function() { - this._animate = false; + this.pause(); } }, @@ -9735,7 +10218,7 @@ var View = Base.extend(Callback, { if (this._stats) this._stats.update(); this._handlingFrame = false; - this.draw(true); + this.update(); }, _animateItem: function(item, animate) { @@ -9766,40 +10249,48 @@ var View = Base.extend(Callback, { } }, - _redraw: function() { - this._project._needsRedraw = true; + _update: function() { + this._project._needsUpdate = true; if (this._handlingFrame) return; if (this._animate) { this._handleFrame(); } else { - this.draw(); + this.update(); } }, _changed: function(flags) { if (flags & 1) - this._project._needsRedraw = true; + this._project._needsUpdate = true; }, _transform: function(matrix) { this._matrix.concatenate(matrix); this._bounds = null; - this._redraw(); + 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(size) { - size = Size.read(arguments); - var delta = size.subtract(this._viewSize); + setViewSize: function() { + var size = Size.read(arguments), + delta = size.subtract(this._viewSize); if (delta.isZero()) return; this._viewSize.set(size.width, size.height); @@ -9809,7 +10300,7 @@ var View = Base.extend(Callback, { size: size, delta: delta }); - this._redraw(); + this._update(); }, _setViewSize: function(size) { @@ -9826,11 +10317,11 @@ var View = Base.extend(Callback, { }, getSize: function() { - return this.getBounds().getSize(arguments[0]); + return this.getBounds().getSize(); }, getCenter: function() { - return this.getBounds().getCenter(arguments[0]); + return this.getBounds().getCenter(); }, setCenter: function(center) { @@ -9856,13 +10347,27 @@ var View = Base.extend(Callback, { 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: { @@ -9903,80 +10408,107 @@ var View = Base.extend(Callback, { } } - function mousedown(event) { + 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; - if (view._onMouseDown) - view._onMouseDown(event, point); - if (tool = view._scope._tool) - tool._onHandleEvent('mousedown', point, event); - view.draw(true); - } + view._handleEvent('mousedown', point, event); + if (tool = view._scope.tool) + tool._handleEvent('mousedown', point, event); + view.update(); + }; - function mousemove(event) { - var view; + docEvents[mousemove] = function(event) { + var view = View._focused; if (!dragging) { - view = getView(event); - if (view) { - prevFocus = View._focused; - View._focused = tempFocus = view; - } else if (tempFocus && tempFocus == View._focused) { - View._focused = prevFocus; + 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 = view || View._focused)) - return; - var point = event && viewToProject(view, event); - if (view._onMouseMove) - view._onMouseMove(event, point); - if (tool = view._scope._tool) { - if (tool._onHandleEvent(dragging && tool.responds('mousedrag') - ? 'mousedrag' : 'mousemove', point, event)) - DomEvent.stop(event); + if (view) { + var point = viewToProject(view, event); + if (dragging || view.getBounds().contains(point)) + tool = handleMouseMove(view, point, event); } - view.draw(true); - } + }; - function mouseup(event) { + docEvents[mouseup] = function(event) { var view = View._focused; if (!view || !dragging) return; var point = viewToProject(view, event); curPoint = null; dragging = false; - if (view._onMouseUp) - view._onMouseUp(event, point); - if (tool && tool._onHandleEvent('mouseup', point, event)) - DomEvent.stop(event); - view.draw(true); - } + view._handleEvent('mouseup', point, event); + if (tool) + tool._handleEvent('mouseup', point, event); + view.update(); + }; - function selectstart(event) { - if (dragging) - DomEvent.stop(event); - } - - DomEvent.add(document, { - mousemove: mousemove, - mouseup: mouseup, - touchmove: mousemove, - touchend: mouseup, - selectstart: selectstart, - scroll: updateFocus - }); + DomEvent.add(document, docEvents); DomEvent.add(window, { load: updateFocus }); return { - _viewHandlers: { - mousedown: mousedown, - touchstart: mousedown, - selectstart: selectstart - }, + _viewEvents: viewEvents, + + _handleEvent: function() {}, statics: { updateFocus: updateFocus @@ -9998,12 +10530,12 @@ var CanvasView = View.extend({ } this._context = canvas.getContext('2d'); this._eventCounters = {}; - this._ratio = 1; + this._pixelRatio = 1; if (PaperScope.getAttribute(canvas, 'hidpi') !== 'off') { var deviceRatio = window.devicePixelRatio || 1, - backingStoreRatio = DomElement.getPrefixValue(this._context, + backingStoreRatio = DomElement.getPrefixed(this._context, 'backingStorePixelRatio') || 1; - this._ratio = deviceRatio / backingStoreRatio; + this._pixelRatio = deviceRatio / backingStoreRatio; } View.call(this, canvas); }, @@ -10011,26 +10543,26 @@ var CanvasView = View.extend({ _setViewSize: function(size) { var width = size.width, height = size.height, - ratio = this._ratio, + pixelRatio = this._pixelRatio, element = this._element, style = element.style; - element.width = width * ratio; - element.height = height * ratio; - if (ratio !== 1) { + element.width = width * pixelRatio; + element.height = height * pixelRatio; + if (pixelRatio !== 1) { style.width = width + 'px'; style.height = height + 'px'; - this._context.scale(ratio, ratio); + this._context.scale(pixelRatio, pixelRatio); } }, - draw: function(checkRedraw) { - if (checkRedraw && !this._project._needsRedraw) + update: function() { + if (!this._project._needsUpdate) return false; var ctx = this._context, size = this._viewSize; ctx.clearRect(0, 0, size.width + 1, size.height + 1); - this._project.draw(ctx, this._matrix, this._ratio); - this._project._needsRedraw = false; + this._project.draw(ctx, this._matrix, this._pixelRatio); + this._project._needsUpdate = false; return true; } }, new function() { @@ -10041,86 +10573,98 @@ var CanvasView = View.extend({ downItem, lastItem, overItem, - hasDrag, - doubleClick, + dragItem, + dblClick, clickTime; - function callEvent(type, event, point, target, lastPoint, bubble) { + function callEvent(view, type, event, point, target, lastPoint) { var item = target, mouseEvent; - while (item) { - if (item.responds(type)) { - if (!mouseEvent) + + function call(obj) { + if (obj.responds(type)) { + if (!mouseEvent) { mouseEvent = new MouseEvent(type, event, point, target, lastPoint ? point.subtract(lastPoint) : null); - if (item.fire(type, mouseEvent) - && (!bubble || mouseEvent._stopped)) - return false; + } + if (obj.fire(type, mouseEvent) && mouseEvent.isStopped) { + event.preventDefault(); + return true; + } } + } + + while (item) { + if (call(item)) + return true; item = item.getParent(); } - return true; - } - - function handleEvent(view, type, event, point, lastPoint) { - if (view._eventCounters[type]) { - var project = view._project, - hit = project.hitTest(point, { - tolerance: project.options.hitTolerance || 0, - fill: true, - stroke: true - }), - item = hit && hit.item; - if (item) { - if (type === 'mousemove' && item != overItem) - lastPoint = point; - if (type !== 'mousemove' || !hasDrag) - callEvent(type, event, point, item, lastPoint); - return item; - } - } + if (call(view)) + return true; + return false; } return { - _onMouseDown: function(event, point) { - var item = handleEvent(this, 'mousedown', event, point); - doubleClick = lastItem == item && (Date.now() - clickTime < 300); - downItem = lastItem = item; - downPoint = lastPoint = overPoint = point; - hasDrag = downItem && downItem.responds('mousedrag'); - }, - - _onMouseUp: function(event, point) { - var item = handleEvent(this, 'mouseup', event, point); - if (hasDrag) { - if (lastPoint && !lastPoint.equals(point)) - callEvent('mousedrag', event, point, downItem, lastPoint); - if (item != downItem) { - overPoint = point; - callEvent('mousemove', event, point, item, overPoint); + _handleEvent: function(type, point, event) { + if (!this._eventCounters[type]) + return; + var project = this._project, + hit = project.hitTest(point, { + tolerance: this._scope.settings.hitTolerance, + 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; } - if (item === downItem) { - clickTime = Date.now(); - if (!doubleClick - || callEvent('doubleclick', event, downPoint, item)) - callEvent('click', event, downPoint, item); - doubleClick = false; - } - downItem = null; - hasDrag = false; - }, - - _onMouseMove: function(event, point) { - if (downItem) - callEvent('mousedrag', event, point, downItem, lastPoint); - var item = handleEvent(this, 'mousemove', event, point, overPoint); - lastPoint = overPoint = point; - if (item !== overItem) { - callEvent('mouseleave', event, point, overItem); - overItem = item; - callEvent('mouseenter', event, point, item); - } + return stopped; } }; }); @@ -10132,14 +10676,17 @@ var Event = Base.extend({ this.event = event; }, + isPrevented: false, + isStopped: false, + preventDefault: function() { - this._prevented = true; - DomEvent.preventDefault(this.event); + this.isPrevented = true; + this.event.preventDefault(); }, stopPropagation: function() { - this._stopped = true; - DomEvent.stopPropagation(this.event); + this.isStopped = true; + this.event.stopPropagation(); }, stop: function() { @@ -10222,7 +10769,7 @@ var Key = new function() { type = down ? 'keydown' : 'keyup', view = View._focused, scope = view && view.isVisible() && view._scope, - tool = scope && scope._tool, + tool = scope && scope.tool, name; keyMap[key] = down; if (specialKey && (name = Base.camelize(specialKey)) in modifiers) @@ -10236,7 +10783,7 @@ var Key = new function() { paper = scope; tool.fire(type, new KeyEvent(down, key, character, event)); if (view) - view.draw(true); + view.update(); } } @@ -10641,7 +11188,7 @@ var ToolEvent = Event.extend({ var Tool = PaperScopeItem.extend({ _class: 'Tool', _list: 'tools', - _reference: '_tool', + _reference: 'tool', _events: [ 'onActivate', 'onDeactivate', 'onEditOptions', 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', 'onKeyDown', 'onKeyUp' ], @@ -10748,7 +11295,7 @@ var Tool = PaperScopeItem.extend({ && this.fire(type, new ToolEvent(this, type, event)); }, - _onHandleEvent: function(type, point, event) { + _handleEvent: function(type, point, event) { paper = this._scope; var called = false; switch (type) { @@ -10786,6 +11333,8 @@ var Tool = PaperScopeItem.extend({ } break; } + if (called) + event.preventDefault(); return called; } @@ -10816,19 +11365,19 @@ var Http = { var CanvasProvider = { canvases: [], - getCanvas: function(width, height, ratio) { + getCanvas: function(width, height, pixelRatio) { var canvas, init = true; if (typeof width === 'object') { - ratio = height; + pixelRatio = height; height = width.height; width = width.width; } - if (!ratio) { - ratio = 1; - } else if (ratio !== 1) { - width *= ratio; - height *= ratio; + if (!pixelRatio) { + pixelRatio = 1; + } else if (pixelRatio !== 1) { + width *= pixelRatio; + height *= pixelRatio; } if (this.canvases.length) { canvas = this.canvases.pop(); @@ -10844,8 +11393,8 @@ var CanvasProvider = { canvas.height = height; } ctx.save(); - if (ratio !== 1) - ctx.scale(ratio, ratio); + if (pixelRatio !== 1) + ctx.scale(pixelRatio, pixelRatio); return canvas; }, @@ -11115,7 +11664,8 @@ var SVGStyles = Base.each({ miterLimit: ['stroke-miterlimit', 'number'], dashArray: ['stroke-dasharray', 'array'], dashOffset: ['stroke-dashoffset', 'number'], - font: ['font-family', 'string'], + fontFamily: ['font-family', 'string'], + fontWeight: ['font-weight', 'string'], fontSize: ['font-size', 'number'], justification: ['text-anchor', 'lookup', { left: 'start', @@ -11179,22 +11729,23 @@ new function() { attrs[center ? 'cy' : 'y'] = point.y; trans = null; } - if (matrix.isIdentity()) - return attrs; - 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(',') + ')'; + 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 (angle) + parts.push('rotate(' + formatter.number(angle) + ')'); + if (!Numerical.isZero(scale.x - 1) + || !Numerical.isZero(scale.y - 1)) + parts.push('scale(' + formatter.point(scale) +')'); + attrs.transform = parts.join(' '); + } else { + attrs.transform = 'matrix(' + matrix.getValues().join(',') + ')'; + } } return attrs; } @@ -11573,7 +12124,7 @@ new function() { currentStyle = project._currentStyle, children = []; if (!isClip) { - item._transformContent = false; + item._applyMatrix = false; item = applyAttributes(item, node, isRoot); project._currentStyle = item._style.clone(); } @@ -11597,11 +12148,14 @@ new function() { } function importPoly(node, type) { - var path = new Path(), - points = node.points; - path.moveTo(points.getItem(0)); - for (var i = 1, l = points.numberOfItems; i < l; i++) - path.lineTo(points.getItem(i)); + 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; @@ -11609,11 +12163,10 @@ new function() { function importPath(node) { var data = node.getAttribute('d'), - path = data.match(/m/gi).length > 1 - ? new CompoundPath() - : new Path(); - path.setPathData(data); - return path; + 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) { @@ -11755,10 +12308,10 @@ new function() { matrix.scale(v); break; case 'skewX': + matrix.skew(v[0], 0); + break; case 'skewY': - var value = Math.tan(v[0] * Math.PI / 180), - isX = command == 'skewX'; - matrix.shear(isX ? value : 0, isX ? 0 : value); + matrix.skew(0, v[0]); break; } } @@ -11904,7 +12457,7 @@ new function() { view = scope.project && scope.project.view; if (onLoad) onLoad.call(this, item); - view.draw(true); + view.update(); } if (isRoot) { @@ -11961,55 +12514,36 @@ new function() { }); }; -paper = new (PaperScope.inject(new Base(Base.exports, { - enumerable: true, - Base: Base, - Numerical: Numerical, - DomElement: DomElement, - DomEvent: DomEvent, - Http: Http, - Key: Key -})))(); - -if (typeof define === 'function' && define.amd) - define('paper', paper); - -return paper; -}; - -paper.PaperScope.prototype.PaperScript = (function(root) { - var Base = paper.Base, - PaperScope = paper.PaperScope, - PaperScript, - exports, define, +Base.exports.PaperScript = (function() { + var exports, define, scope = this; !function(e,r){return"object"==typeof exports&&"object"==typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):(r(e.acorn||(e.acorn={})),void 0)}(this,function(e){"use strict";function r(e){fr=e||{};for(var r in hr)Object.prototype.hasOwnProperty.call(fr,r)||(fr[r]=hr[r]);mr=fr.sourceFile||null}function t(e,r){var t=vr(pr,e);r+=" ("+t.line+":"+t.column+")";var n=new SyntaxError(r);throw n.pos=e,n.loc=t,n.raisedAt=br,n}function n(e){function r(e){if(1==e.length)return t+="return str === "+JSON.stringify(e[0])+";";t+="switch(str){";for(var r=0;r3){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&&8329!==t;)++br,t=pr.charCodeAt(br);fr.onComment&&fr.onComment(!1,pr.slice(e+2,br),e,br,r,fr.locations&&new a)}function u(){for(;dr>br;){var e=pr.charCodeAt(br);if(32===e)++br;else if(13===e){++br;var r=pr.charCodeAt(br);10===r&&++br,fr.locations&&(++Ar,Sr=br)}else if(10===e)++br,++Ar,Sr=br;else if(14>e&&e>8)++br;else if(47===e){var r=pr.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=pr.charCodeAt(br+1);return e>=48&&57>=e?E(!0):(++br,i(xt))}function f(){var e=pr.charCodeAt(br+1);return Er?(++br,k()):61===e?x(Et,2):x(wt,1)}function p(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Ft,1)}function d(e){var r=pr.charCodeAt(br+1);return r===e?x(124===e?Lt:Ut,2):61===r?x(Et,2):x(124===e?Rt:Vt,1)}function m(){var e=pr.charCodeAt(br+1);return 61===e?x(Et,2):x(Tt,1)}function h(e){var r=pr.charCodeAt(br+1);return r===e?x(St,2):61===r?x(Et,2):x(At,1)}function v(e){var r=pr.charCodeAt(br+1),t=1;return r===e?(t=62===e&&62===pr.charCodeAt(br+2)?3:2,61===pr.charCodeAt(br+t)?x(Et,t+1):x(jt,t)):(61===r&&(t=61===pr.charCodeAt(br+2)?3:2),x(Ot,t))}function b(e){var r=pr.charCodeAt(br+1);return 61===r?x(qt,61===pr.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(ht);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(pt);case 123:return++br,i(dt);case 125:return++br,i(mt);case 58:return++br,i(gt);case 63:return++br,i(kt);case 48:var r=pr.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 p();case 124:case 38:return d(e);case 94:return m();case 43:case 45:return h(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>=dr)return i(Br);var r=pr.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=pr.slice(br,br+r);br+=r,i(e,t)}function k(){for(var e,r,n="",a=br;;){br>=dr&&t(a,"Unterminated regular expression");var o=pr.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=pr.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=pr.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(pr.charCodeAt(br))&&t(br,"Identifier directly after number"),i(Or,e)}function E(e){var r=br,n=!1,a=48===pr.charCodeAt(br);e||null!==w(10)||t(r,"Invalid number"),46===pr.charCodeAt(br)&&(++br,w(10),n=!0);var o=pr.charCodeAt(br);(69===o||101===o)&&(o=pr.charCodeAt(++br),(43===o||45===o)&&++br,null===w(10)&&t(r,"Invalid number"),n=!0),Qt(pr.charCodeAt(br))&&t(br,"Identifier directly after number");var s,c=pr.slice(r,br);return n?s=parseFloat(c):a&&1!==c.length?/[89]/.test(c)||Vr?t(r,"Invalid number"):s=parseInt(c,8):s=parseInt(c,10),i(Or,s)}function A(e){br++;for(var r="";;){br>=dr&&t(yr,"Unterminated string constant");var n=pr.charCodeAt(br);if(n===e)return++br,i(Fr,r);if(92===n){n=pr.charCodeAt(++br);var a=/^[0-7]+/.exec(pr.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)Vr&&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===pr.charCodeAt(br)&&++br;case 10:fr.locations&&(Sr=br,++Ar);break;default:r+=String.fromCharCode(n)}}else(13===n||10===n||8232===n||8329===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=pr.charCodeAt(br);if(Yt(a))Bt&&(e+=pr.charAt(br)),++br;else{if(92!==a)break;Bt||(e=pr.slice(n,br)),Bt=!0,117!=pr.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:pr.slice(n,br)}function L(){var e=I(),r=Dr;return Bt||(Wt(e)?r=lt[e]:(fr.forbidReserved&&(3===fr.ecmaVersion?Mt:zt)(e)||Vr&&Xt(e))&&t(yr,"The keyword '"+e+"' is reserved")),i(r,e)}function U(){Ir=yr,Lr=gr,Ur=kr,g()}function R(e){for(Vr=e,br=Lr;Sr>br;)Sr=pr.lastIndexOf("\n",Sr-2)+1,--Ar;u(),g()}function T(){this.type=null,this.start=yr,this.end=null}function V(){this.start=xr,this.end=null,null!==mr&&(this.source=mr)}function q(){var e=new T;return fr.locations&&(e.loc=new V),fr.ranges&&(e.range=[yr,0]),e}function O(e){var r=new T;return r.start=e.start,fr.locations&&(r.loc=new V,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 F(e){return fr.ecmaVersion>=5&&"ExpressionStatement"===e.type&&"Literal"===e.expression.type&&"use strict"===e.expression.value}function D(e){return wr===e?(U(),!0):void 0}function B(){return!fr.strictSemicolons&&(wr===Br||wr===mt||Gt.test(pr.slice(Lr,yr)))}function M(){D(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"),Vr&&"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=Vr=null,Tr=[],g();var r=e||q(),t=!0;for(e||(r.body=[]);wr!==Br;){var n=J();r.body.push(n),t&&F(n)&&R(!0),t=!1}return j(r,"Program")}function J(){wr===wt&&g(!0);var e=wr,r=q();switch(e){case Mr:case Nr:U();var n=e===Mr;D(yt)||B()?r.label=null:wr!==Dr?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 a=j(a,/&&|\|\|/.test(a.operator)?"LogicalExpression":"BinaryExpression");return er(a,r,t)}return e}function rr(){if(wr.prefix){var e=q(),r=wr.isUpdate;return e.operator=Cr,e.prefix=!0,U(),e.argument=rr(),r?N(e.argument):Vr&&"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(D(xt)){var t=O(e);return t.object=e,t.property=lr(!0),t.computed=!1,nr(j(t,"MemberExpression"),r)}if(D(ft)){var t=O(e);return t.object=e,t.property=K(),t.computed=!0,z(pt),nr(j(t,"MemberExpression"),r)}if(!r&&D(ht)){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 Dr:return lr();case Or:case Fr:case jr:var e=q();return e.value=Cr,e.raw=pr.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 ht: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(pt,!0,!0),j(e,"ArrayExpression");case dt: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=D(ht)?ur(vt,!1):qr,j(e,"NewExpression")}function ir(){var e=q(),r=!0,n=!1;for(e.properties=[],U();!D(mt);){if(r)r=!1;else if(z(bt),fr.allowTrailingCommas&&D(mt))break;var a,o={key:sr()},i=!1;if(D(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!==ht&&X(),o.value=cr(q(),!1)):X(),"Identifier"===o.key.type&&(Vr||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;!D(e);){if(a)a=!1;else if(z(bt),r&&fr.allowTrailingCommas&&D(e))break;t&&wr===bt?n.push(null):n.push(K(!0))}return n}function lr(e){var r=q();return r.name=wr===Dr?Cr:e&&!fr.forbidReserved&&wr.keyword||X(),U(),j(r,"Identifier")}e.version="0.3.2";var fr,pr,dr,mr;e.parse=function(e,t){return pr=String(e),dr=pr.length,r(t),o(),W(fr.program)};var hr=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', + '+': '__add', + '-': '__subtract', + '*': '__multiply', + '/': '__divide', + '%': '__modulo', '==': 'equals', '!=': 'equals' }; var unaryOperators = { - '-': '_negate', + '-': '__negate', '+': null }; var fields = Base.each( ['add', 'subtract', 'multiply', 'divide', 'modulo', 'negate'], function(name) { - this['_' + name] = '#' + name; + this['__' + name] = '#' + name; }, {} ); - paper.Point.inject(fields); - paper.Size.inject(fields); - paper.Color.inject(fields); + Point.inject(fields); + Size.inject(fields); + Color.inject(fields); function _$_(left, operator, right) { var handler = binaryOperators[operator]; @@ -12059,8 +12593,8 @@ paper.PaperScope.prototype.PaperScript = (function(root) { function replaceCode(node, str) { var start = getOffset(node.range[0]), - end = getOffset(node.range[1]); - var insert = 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; @@ -12086,7 +12620,15 @@ paper.PaperScope.prototype.PaperScript = (function(root) { } } switch (node && node.type) { - case 'BinaryExpression': + 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), @@ -12095,32 +12637,29 @@ paper.PaperScope.prototype.PaperScript = (function(root) { + '", ' + right + ')'); } break; - case 'AssignmentExpression': - 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; - case 'UpdateExpression': - if (!node.prefix && !(parent && ( - parent.type === 'BinaryExpression' + case 'UpdateExpression': + case 'AssignmentExpression': + if (!(parent && ( + parent.type === 'ForStatement' + || parent.type === 'BinaryExpression' && /^[=!<>]/.test(parent.operator) || parent.type === 'MemberExpression' && parent.computed))) { - var arg = getCode(node.argument); - replaceCode(node, arg + ' = _$_(' + arg + ', "' - + node.operator[0] + '", 1)'); - } - break; - case 'UnaryExpression': - if (node.operator in unaryOperators - && node.argument.type !== 'Literal') { - var arg = getCode(node.argument); - replaceCode(node, '$_("' + node.operator + '", ' - + arg + ')'); + if (node.type === 'UpdateExpression') { + if (!node.prefix) { + var arg = getCode(node.argument); + replaceCode(node, arg + ' = _$_(' + arg + ', "' + + node.operator[0] + '", 1)'); + } + } 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; } @@ -12129,57 +12668,70 @@ paper.PaperScope.prototype.PaperScript = (function(root) { return code; } - function evaluate(code, scope) { + function execute(code, scope) { paper = scope; - var view = scope.project && scope.project.view, - res; - with (scope) { - (function() { - var onActivate, onDeactivate, onEditOptions, - onMouseDown, onMouseUp, onMouseDrag, onMouseMove, - onKeyDown, onKeyUp, onFrame, onResize; - code = compile(code); - if (root.InstallTrigger) { - var handle = PaperScript.handleException; - if (!handle) { - handle = PaperScript.handleException = function(e) { - throw e.lineNumber >= lineNumber - ? new Error(e.message, e.fileName, - e.lineNumber - lineNumber) - : e; - } - var lineNumber = new Error().lineNumber; - lineNumber += (new Error().lineNumber - lineNumber) * 3; - } - try { - res = eval(';' + code); - } catch (e) { - handle(e); - } - } else { - res = eval(code); + 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); + function expose(scope, hidden) { + for (var key in scope) { + if ((hidden || !/^_/.test(key)) && new RegExp( + '\\b' + key.replace(/\$/g, '\\$') + '\\b').test(code)) { + params.push(key); + args.push(scope[key]); } - if (/on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code)) { - Base.each(paper.Tool.prototype._events, function(key) { - var value = eval(key); - if (value) { - scope.getTool()[key] = value; - } - }); - } - if (view) { - view.setOnResize(onResize); - view.fire('resize', { - size: view.size, - delta: new Point() - }); - if (onFrame) - view.setOnFrame(onFrame); - view.draw(); - } - }).call(scope); + } + } + expose({ _$_: _$_, $_: $_, 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 firefox = window.InstallTrigger; + if (firefox || window.chrome) { + var script = document.createElement('script'), + head = document.head; + if (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.fire('resize', { + size: view.size, + delta: new Point() + }); + if (res.onFrame) + view.setOnFrame(res.onFrame); + view.update(); } - return res; } function load() { @@ -12191,11 +12743,11 @@ paper.PaperScope.prototype.PaperScript = (function(root) { || new PaperScope(script).setup(canvas), src = script.src; if (src) { - paper.Http.request('get', src, function(code) { - evaluate(code, scope); + Http.request('get', src, function(code) { + execute(code, scope); }); } else { - evaluate(script.innerHTML, scope); + execute(script.innerHTML, scope); } script.setAttribute('data-paper-ignore', true); } @@ -12205,14 +12757,30 @@ paper.PaperScope.prototype.PaperScript = (function(root) { if (document.readyState === 'complete') { setTimeout(load); } else { - paper.DomEvent.add(window, { load: load }); + DomEvent.add(window, { load: load }); } - return PaperScript = { + return { compile: compile, - evaluate: evaluate, + execute: execute, load: load, lineNumberBase: 0 }; -})(this); +}).call(this); + +paper = new (PaperScope.inject(Base.exports, { + enumerable: true, + Base: Base, + Numerical: Numerical, + DomElement: DomElement, + DomEvent: DomEvent, + Http: Http, + Key: Key +}))(); + +if (typeof define === 'function' && define.amd) + define('paper', paper); + +return paper; +}; diff --git a/package.json b/package.json index d1c3a009..ba9ea62f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "paper", - "version": "0.9.15", + "version": "0.9.16", "description": "The Swiss Army Knife of Vector Graphics Scripting", "homepage": "http://paperjs.org", "repository": "git://github.com/paperjs/paper.js",