--- /dev/null
+// Copyright 2006 Google Inc.\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+\r
+// Known Issues:\r
+//\r
+// * Patterns are not implemented.\r
+// * Radial gradient are not implemented. The VML version of these look very\r
+// different from the canvas one.\r
+// * Clipping paths are not implemented.\r
+// * Coordsize. The width and height attribute have higher priority than the\r
+// width and height style values which isn't correct.\r
+// * Painting mode isn't implemented.\r
+// * Canvas width/height should is using content-box by default. IE in\r
+// Quirks mode will draw the canvas using border-box. Either change your\r
+// doctype to HTML5\r
+// (http://www.whatwg.org/specs/web-apps/current-work/#the-doctype)\r
+// or use Box Sizing Behavior from WebFX\r
+// (http://webfx.eae.net/dhtml/boxsizing/boxsizing.html)\r
+// * Non uniform scaling does not correctly scale strokes.\r
+// * Optimize. There is always room for speed improvements.\r
+\r
+// Only add this code if we do not already have a canvas implementation\r
+if (!document.createElement('canvas').getContext) {\r
+\r
+(function() {\r
+\r
+ // alias some functions to make (compiled) code shorter\r
+ var m = Math;\r
+ var mr = m.round;\r
+ var ms = m.sin;\r
+ var mc = m.cos;\r
+ var abs = m.abs;\r
+ var sqrt = m.sqrt;\r
+\r
+ // this is used for sub pixel precision\r
+ var Z = 10;\r
+ var Z2 = Z / 2;\r
+\r
+ /**\r
+ * This funtion is assigned to the <canvas> elements as element.getContext().\r
+ * @this {HTMLElement}\r
+ * @return {CanvasRenderingContext2D_}\r
+ */\r
+ function getContext() {\r
+ return this.context_ ||\r
+ (this.context_ = new CanvasRenderingContext2D_(this));\r
+ }\r
+\r
+ var slice = Array.prototype.slice;\r
+\r
+ /**\r
+ * Binds a function to an object. The returned function will always use the\r
+ * passed in {@code obj} as {@code this}.\r
+ *\r
+ * Example:\r
+ *\r
+ * g = bind(f, obj, a, b)\r
+ * g(c, d) // will do f.call(obj, a, b, c, d)\r
+ *\r
+ * @param {Function} f The function to bind the object to\r
+ * @param {Object} obj The object that should act as this when the function\r
+ * is called\r
+ * @param {*} var_args Rest arguments that will be used as the initial\r
+ * arguments when the function is called\r
+ * @return {Function} A new function that has bound this\r
+ */\r
+ function bind(f, obj, var_args) {\r
+ var a = slice.call(arguments, 2);\r
+ return function() {\r
+ return f.apply(obj, a.concat(slice.call(arguments)));\r
+ };\r
+ }\r
+\r
+ var G_vmlCanvasManager_ = {\r
+ init: function(opt_doc) {\r
+ if (/MSIE/.test(navigator.userAgent) && !window.opera) {\r
+ var doc = opt_doc || document;\r
+ // Create a dummy element so that IE will allow canvas elements to be\r
+ // recognized.\r
+ doc.createElement('canvas');\r
+ doc.attachEvent('onreadystatechange', bind(this.init_, this, doc));\r
+ }\r
+ },\r
+\r
+ init_: function(doc) {\r
+ // create xmlns\r
+ if (!doc.namespaces['g_vml_']) {\r
+ doc.namespaces.add('g_vml_', 'urn:schemas-microsoft-com:vml',\r
+ '#default#VML');\r
+\r
+ }\r
+ if (!doc.namespaces['g_o_']) {\r
+ doc.namespaces.add('g_o_', 'urn:schemas-microsoft-com:office:office',\r
+ '#default#VML');\r
+ }\r
+\r
+ // Setup default CSS. Only add one style sheet per document\r
+ if (!doc.styleSheets['ex_canvas_']) {\r
+ var ss = doc.createStyleSheet();\r
+ ss.owningElement.id = 'ex_canvas_';\r
+ ss.cssText = 'canvas{display:inline-block;overflow:hidden;' +\r
+ // default size is 300x150 in Gecko and Opera\r
+ 'text-align:left;width:300px;height:150px}' +\r
+ 'g_vml_\\:*{behavior:url(#default#VML)}' +\r
+ 'g_o_\\:*{behavior:url(#default#VML)}';\r
+\r
+ }\r
+\r
+ // find all canvas elements\r
+ var els = doc.getElementsByTagName('canvas');\r
+ for (var i = 0; i < els.length; i++) {\r
+ this.initElement(els[i]);\r
+ }\r
+ },\r
+\r
+ /**\r
+ * Public initializes a canvas element so that it can be used as canvas\r
+ * element from now on. This is called automatically before the page is\r
+ * loaded but if you are creating elements using createElement you need to\r
+ * make sure this is called on the element.\r
+ * @param {HTMLElement} el The canvas element to initialize.\r
+ * @return {HTMLElement} the element that was created.\r
+ */\r
+ initElement: function(el) {\r
+ if (!el.getContext) {\r
+\r
+ el.getContext = getContext;\r
+\r
+ // Remove fallback content. There is no way to hide text nodes so we\r
+ // just remove all childNodes. We could hide all elements and remove\r
+ // text nodes but who really cares about the fallback content.\r
+ el.innerHTML = '';\r
+\r
+ // do not use inline function because that will leak memory\r
+ el.attachEvent('onpropertychange', onPropertyChange);\r
+ el.attachEvent('onresize', onResize);\r
+\r
+ var attrs = el.attributes;\r
+ if (attrs.width && attrs.width.specified) {\r
+ // TODO: use runtimeStyle and coordsize\r
+ // el.getContext().setWidth_(attrs.width.nodeValue);\r
+ el.style.width = attrs.width.nodeValue + 'px';\r
+ } else {\r
+ el.width = el.clientWidth;\r
+ }\r
+ if (attrs.height && attrs.height.specified) {\r
+ // TODO: use runtimeStyle and coordsize\r
+ // el.getContext().setHeight_(attrs.height.nodeValue);\r
+ el.style.height = attrs.height.nodeValue + 'px';\r
+ } else {\r
+ el.height = el.clientHeight;\r
+ }\r
+ //el.getContext().setCoordsize_()\r
+ }\r
+ return el;\r
+ }\r
+ };\r
+\r
+ function onPropertyChange(e) {\r
+ var el = e.srcElement;\r
+\r
+ switch (e.propertyName) {\r
+ case 'width':\r
+ el.style.width = el.attributes.width.nodeValue + 'px';\r
+ el.getContext().clearRect();\r
+ break;\r
+ case 'height':\r
+ el.style.height = el.attributes.height.nodeValue + 'px';\r
+ el.getContext().clearRect();\r
+ break;\r
+ }\r
+ }\r
+\r
+ function onResize(e) {\r
+ var el = e.srcElement;\r
+ if (el.firstChild) {\r
+ el.firstChild.style.width = el.clientWidth + 'px';\r
+ el.firstChild.style.height = el.clientHeight + 'px';\r
+ }\r
+ }\r
+\r
+ G_vmlCanvasManager_.init();\r
+\r
+ // precompute "00" to "FF"\r
+ var dec2hex = [];\r
+ for (var i = 0; i < 16; i++) {\r
+ for (var j = 0; j < 16; j++) {\r
+ dec2hex[i * 16 + j] = i.toString(16) + j.toString(16);\r
+ }\r
+ }\r
+\r
+ function createMatrixIdentity() {\r
+ return [\r
+ [1, 0, 0],\r
+ [0, 1, 0],\r
+ [0, 0, 1]\r
+ ];\r
+ }\r
+\r
+ function matrixMultiply(m1, m2) {\r
+ var result = createMatrixIdentity();\r
+\r
+ for (var x = 0; x < 3; x++) {\r
+ for (var y = 0; y < 3; y++) {\r
+ var sum = 0;\r
+\r
+ for (var z = 0; z < 3; z++) {\r
+ sum += m1[x][z] * m2[z][y];\r
+ }\r
+\r
+ result[x][y] = sum;\r
+ }\r
+ }\r
+ return result;\r
+ }\r
+\r
+ function copyState(o1, o2) {\r
+ o2.fillStyle = o1.fillStyle;\r
+ o2.lineCap = o1.lineCap;\r
+ o2.lineJoin = o1.lineJoin;\r
+ o2.lineWidth = o1.lineWidth;\r
+ o2.miterLimit = o1.miterLimit;\r
+ o2.shadowBlur = o1.shadowBlur;\r
+ o2.shadowColor = o1.shadowColor;\r
+ o2.shadowOffsetX = o1.shadowOffsetX;\r
+ o2.shadowOffsetY = o1.shadowOffsetY;\r
+ o2.strokeStyle = o1.strokeStyle;\r
+ o2.globalAlpha = o1.globalAlpha;\r
+ o2.arcScaleX_ = o1.arcScaleX_;\r
+ o2.arcScaleY_ = o1.arcScaleY_;\r
+ o2.lineScale_ = o1.lineScale_;\r
+ }\r
+\r
+ function processStyle(styleString) {\r
+ var str, alpha = 1;\r
+\r
+ styleString = String(styleString);\r
+ if (styleString.substring(0, 3) == 'rgb') {\r
+ var start = styleString.indexOf('(', 3);\r
+ var end = styleString.indexOf(')', start + 1);\r
+ var guts = styleString.substring(start + 1, end).split(',');\r
+\r
+ str = '#';\r
+ for (var i = 0; i < 3; i++) {\r
+ str += dec2hex[Number(guts[i])];\r
+ }\r
+\r
+ if (guts.length == 4 && styleString.substr(3, 1) == 'a') {\r
+ alpha = guts[3];\r
+ }\r
+ } else {\r
+ str = styleString;\r
+ }\r
+\r
+ return {color: str, alpha: alpha};\r
+ }\r
+\r
+ function processLineCap(lineCap) {\r
+ switch (lineCap) {\r
+ case 'butt':\r
+ return 'flat';\r
+ case 'round':\r
+ return 'round';\r
+ case 'square':\r
+ default:\r
+ return 'square';\r
+ }\r
+ }\r
+\r
+ /**\r
+ * This class implements CanvasRenderingContext2D interface as described by\r
+ * the WHATWG.\r
+ * @param {HTMLElement} surfaceElement The element that the 2D context should\r
+ * be associated with\r
+ */\r
+ function CanvasRenderingContext2D_(surfaceElement) {\r
+ this.m_ = createMatrixIdentity();\r
+\r
+ this.mStack_ = [];\r
+ this.aStack_ = [];\r
+ this.currentPath_ = [];\r
+\r
+ // Canvas context properties\r
+ this.strokeStyle = '#000';\r
+ this.fillStyle = '#000';\r
+\r
+ this.lineWidth = 1;\r
+ this.lineJoin = 'miter';\r
+ this.lineCap = 'butt';\r
+ this.miterLimit = Z * 1;\r
+ this.globalAlpha = 1;\r
+ this.canvas = surfaceElement;\r
+\r
+ var el = surfaceElement.ownerDocument.createElement('div');\r
+ el.style.width = surfaceElement.clientWidth + 'px';\r
+ el.style.height = surfaceElement.clientHeight + 'px';\r
+ el.style.overflow = 'hidden';\r
+ el.style.position = 'absolute';\r
+ surfaceElement.appendChild(el);\r
+\r
+ this.element_ = el;\r
+ this.arcScaleX_ = 1;\r
+ this.arcScaleY_ = 1;\r
+ this.lineScale_ = 1;\r
+ }\r
+\r
+ var contextPrototype = CanvasRenderingContext2D_.prototype;\r
+ contextPrototype.clearRect = function() {\r
+ this.element_.innerHTML = '';\r
+ };\r
+\r
+ contextPrototype.beginPath = function() {\r
+ // TODO: Branch current matrix so that save/restore has no effect\r
+ // as per safari docs.\r
+ this.currentPath_ = [];\r
+ };\r
+\r
+ contextPrototype.moveTo = function(aX, aY) {\r
+ var p = this.getCoords_(aX, aY);\r
+ this.currentPath_.push({type: 'moveTo', x: p.x, y: p.y});\r
+ this.currentX_ = p.x;\r
+ this.currentY_ = p.y;\r
+ };\r
+\r
+ contextPrototype.lineTo = function(aX, aY) {\r
+ var p = this.getCoords_(aX, aY);\r
+ this.currentPath_.push({type: 'lineTo', x: p.x, y: p.y});\r
+\r
+ this.currentX_ = p.x;\r
+ this.currentY_ = p.y;\r
+ };\r
+\r
+ contextPrototype.bezierCurveTo = function(aCP1x, aCP1y,\r
+ aCP2x, aCP2y,\r
+ aX, aY) {\r
+ var p = this.getCoords_(aX, aY);\r
+ var cp1 = this.getCoords_(aCP1x, aCP1y);\r
+ var cp2 = this.getCoords_(aCP2x, aCP2y);\r
+ bezierCurveTo(this, cp1, cp2, p);\r
+ };\r
+\r
+ // Helper function that takes the already fixed cordinates.\r
+ function bezierCurveTo(self, cp1, cp2, p) {\r
+ self.currentPath_.push({\r
+ type: 'bezierCurveTo',\r
+ cp1x: cp1.x,\r
+ cp1y: cp1.y,\r
+ cp2x: cp2.x,\r
+ cp2y: cp2.y,\r
+ x: p.x,\r
+ y: p.y\r
+ });\r
+ self.currentX_ = p.x;\r
+ self.currentY_ = p.y;\r
+ }\r
+\r
+ contextPrototype.quadraticCurveTo = function(aCPx, aCPy, aX, aY) {\r
+ // the following is lifted almost directly from\r
+ // http://developer.mozilla.org/en/docs/Canvas_tutorial:Drawing_shapes\r
+\r
+ var cp = this.getCoords_(aCPx, aCPy);\r
+ var p = this.getCoords_(aX, aY);\r
+\r
+ var cp1 = {\r
+ x: this.currentX_ + 2.0 / 3.0 * (cp.x - this.currentX_),\r
+ y: this.currentY_ + 2.0 / 3.0 * (cp.y - this.currentY_)\r
+ };\r
+ var cp2 = {\r
+ x: cp1.x + (p.x - this.currentX_) / 3.0,\r
+ y: cp1.y + (p.y - this.currentY_) / 3.0\r
+ };\r
+\r
+ bezierCurveTo(this, cp1, cp2, p);\r
+ };\r
+\r
+ contextPrototype.arc = function(aX, aY, aRadius,\r
+ aStartAngle, aEndAngle, aClockwise) {\r
+ aRadius *= Z;\r
+ var arcType = aClockwise ? 'at' : 'wa';\r
+\r
+ var xStart = aX + mc(aStartAngle) * aRadius - Z2;\r
+ var yStart = aY + ms(aStartAngle) * aRadius - Z2;\r
+\r
+ var xEnd = aX + mc(aEndAngle) * aRadius - Z2;\r
+ var yEnd = aY + ms(aEndAngle) * aRadius - Z2;\r
+\r
+ // IE won't render arches drawn counter clockwise if xStart == xEnd.\r
+ if (xStart == xEnd && !aClockwise) {\r
+ xStart += 0.125; // Offset xStart by 1/80 of a pixel. Use something\r
+ // that can be represented in binary\r
+ }\r
+\r
+ var p = this.getCoords_(aX, aY);\r
+ var pStart = this.getCoords_(xStart, yStart);\r
+ var pEnd = this.getCoords_(xEnd, yEnd);\r
+\r
+ this.currentPath_.push({type: arcType,\r
+ x: p.x,\r
+ y: p.y,\r
+ radius: aRadius,\r
+ xStart: pStart.x,\r
+ yStart: pStart.y,\r
+ xEnd: pEnd.x,\r
+ yEnd: pEnd.y});\r
+\r
+ };\r
+\r
+ contextPrototype.rect = function(aX, aY, aWidth, aHeight) {\r
+ this.moveTo(aX, aY);\r
+ this.lineTo(aX + aWidth, aY);\r
+ this.lineTo(aX + aWidth, aY + aHeight);\r
+ this.lineTo(aX, aY + aHeight);\r
+ this.closePath();\r
+ };\r
+\r
+ contextPrototype.strokeRect = function(aX, aY, aWidth, aHeight) {\r
+ var oldPath = this.currentPath_;\r
+ this.beginPath();\r
+\r
+ this.moveTo(aX, aY);\r
+ this.lineTo(aX + aWidth, aY);\r
+ this.lineTo(aX + aWidth, aY + aHeight);\r
+ this.lineTo(aX, aY + aHeight);\r
+ this.closePath();\r
+ this.stroke();\r
+\r
+ this.currentPath_ = oldPath;\r
+ };\r
+\r
+ contextPrototype.fillRect = function(aX, aY, aWidth, aHeight) {\r
+ var oldPath = this.currentPath_;\r
+ this.beginPath();\r
+\r
+ this.moveTo(aX, aY);\r
+ this.lineTo(aX + aWidth, aY);\r
+ this.lineTo(aX + aWidth, aY + aHeight);\r
+ this.lineTo(aX, aY + aHeight);\r
+ this.closePath();\r
+ this.fill();\r
+\r
+ this.currentPath_ = oldPath;\r
+ };\r
+\r
+ contextPrototype.createLinearGradient = function(aX0, aY0, aX1, aY1) {\r
+ var gradient = new CanvasGradient_('gradient');\r
+ gradient.x0_ = aX0;\r
+ gradient.y0_ = aY0;\r
+ gradient.x1_ = aX1;\r
+ gradient.y1_ = aY1;\r
+ return gradient;\r
+ };\r
+\r
+ contextPrototype.createRadialGradient = function(aX0, aY0, aR0,\r
+ aX1, aY1, aR1) {\r
+ var gradient = new CanvasGradient_('gradientradial');\r
+ gradient.x0_ = aX0;\r
+ gradient.y0_ = aY0;\r
+ gradient.r0_ = aR0;\r
+ gradient.x1_ = aX1;\r
+ gradient.y1_ = aY1;\r
+ gradient.r1_ = aR1;\r
+ return gradient;\r
+ };\r
+\r
+ contextPrototype.drawImage = function(image, var_args) {\r
+ var dx, dy, dw, dh, sx, sy, sw, sh;\r
+\r
+ // to find the original width we overide the width and height\r
+ var oldRuntimeWidth = image.runtimeStyle.width;\r
+ var oldRuntimeHeight = image.runtimeStyle.height;\r
+ image.runtimeStyle.width = 'auto';\r
+ image.runtimeStyle.height = 'auto';\r
+\r
+ // get the original size\r
+ var w = image.width;\r
+ var h = image.height;\r
+\r
+ // and remove overides\r
+ image.runtimeStyle.width = oldRuntimeWidth;\r
+ image.runtimeStyle.height = oldRuntimeHeight;\r
+\r
+ if (arguments.length == 3) {\r
+ dx = arguments[1];\r
+ dy = arguments[2];\r
+ sx = sy = 0;\r
+ sw = dw = w;\r
+ sh = dh = h;\r
+ } else if (arguments.length == 5) {\r
+ dx = arguments[1];\r
+ dy = arguments[2];\r
+ dw = arguments[3];\r
+ dh = arguments[4];\r
+ sx = sy = 0;\r
+ sw = w;\r
+ sh = h;\r
+ } else if (arguments.length == 9) {\r
+ sx = arguments[1];\r
+ sy = arguments[2];\r
+ sw = arguments[3];\r
+ sh = arguments[4];\r
+ dx = arguments[5];\r
+ dy = arguments[6];\r
+ dw = arguments[7];\r
+ dh = arguments[8];\r
+ } else {\r
+ throw Error('Invalid number of arguments');\r
+ }\r
+\r
+ var d = this.getCoords_(dx, dy);\r
+\r
+ var w2 = sw / 2;\r
+ var h2 = sh / 2;\r
+\r
+ var vmlStr = [];\r
+\r
+ var W = 10;\r
+ var H = 10;\r
+\r
+ // For some reason that I've now forgotten, using divs didn't work\r
+ vmlStr.push(' <g_vml_:group',\r
+ ' coordsize="', Z * W, ',', Z * H, '"',\r
+ ' coordorigin="0,0"' ,\r
+ ' style="width:', W, 'px;height:', H, 'px;position:absolute;');\r
+\r
+ // If filters are necessary (rotation exists), create them\r
+ // filters are bog-slow, so only create them if abbsolutely necessary\r
+ // The following check doesn't account for skews (which don't exist\r
+ // in the canvas spec (yet) anyway.\r
+\r
+ if (this.m_[0][0] != 1 || this.m_[0][1]) {\r
+ var filter = [];\r
+\r
+ // Note the 12/21 reversal\r
+ filter.push('M11=', this.m_[0][0], ',',\r
+ 'M12=', this.m_[1][0], ',',\r
+ 'M21=', this.m_[0][1], ',',\r
+ 'M22=', this.m_[1][1], ',',\r
+ 'Dx=', mr(d.x / Z), ',',\r
+ 'Dy=', mr(d.y / Z), '');\r
+\r
+ // Bounding box calculation (need to minimize displayed area so that\r
+ // filters don't waste time on unused pixels.\r
+ var max = d;\r
+ var c2 = this.getCoords_(dx + dw, dy);\r
+ var c3 = this.getCoords_(dx, dy + dh);\r
+ var c4 = this.getCoords_(dx + dw, dy + dh);\r
+\r
+ max.x = m.max(max.x, c2.x, c3.x, c4.x);\r
+ max.y = m.max(max.y, c2.y, c3.y, c4.y);\r
+\r
+ vmlStr.push('padding:0 ', mr(max.x / Z), 'px ', mr(max.y / Z),\r
+ 'px 0;filter:progid:DXImageTransform.Microsoft.Matrix(',\r
+ filter.join(''), ", sizingmethod='clip');")\r
+ } else {\r
+ vmlStr.push('top:', mr(d.y / Z), 'px;left:', mr(d.x / Z), 'px;');\r
+ }\r
+\r
+ vmlStr.push(' ">' ,\r
+ '<g_vml_:image src="', image.src, '"',\r
+ ' style="width:', Z * dw, 'px;',\r
+ ' height:', Z * dh, 'px;"',\r
+ ' cropleft="', sx / w, '"',\r
+ ' croptop="', sy / h, '"',\r
+ ' cropright="', (w - sx - sw) / w, '"',\r
+ ' cropbottom="', (h - sy - sh) / h, '"',\r
+ ' />',\r
+ '</g_vml_:group>');\r
+\r
+ this.element_.insertAdjacentHTML('BeforeEnd',\r
+ vmlStr.join(''));\r
+ };\r
+\r
+ contextPrototype.stroke = function(aFill) {\r
+ var lineStr = [];\r
+ var lineOpen = false;\r
+ var a = processStyle(aFill ? this.fillStyle : this.strokeStyle);\r
+ var color = a.color;\r
+ var opacity = a.alpha * this.globalAlpha;\r
+\r
+ var W = 10;\r
+ var H = 10;\r
+\r
+ lineStr.push('<g_vml_:shape',\r
+ ' filled="', !!aFill, '"',\r
+ ' style="position:absolute;width:', W, 'px;height:', H, 'px;"',\r
+ ' coordorigin="0 0" coordsize="', Z * W, ' ', Z * H, '"',\r
+ ' stroked="', !aFill, '"',\r
+ ' path="');\r
+\r
+ var newSeq = false;\r
+ var min = {x: null, y: null};\r
+ var max = {x: null, y: null};\r
+\r
+ for (var i = 0; i < this.currentPath_.length; i++) {\r
+ var p = this.currentPath_[i];\r
+ var c;\r
+\r
+ switch (p.type) {\r
+ case 'moveTo':\r
+ c = p;\r
+ lineStr.push(' m ', mr(p.x), ',', mr(p.y));\r
+ break;\r
+ case 'lineTo':\r
+ lineStr.push(' l ', mr(p.x), ',', mr(p.y));\r
+ break;\r
+ case 'close':\r
+ lineStr.push(' x ');\r
+ p = null;\r
+ break;\r
+ case 'bezierCurveTo':\r
+ lineStr.push(' c ',\r
+ mr(p.cp1x), ',', mr(p.cp1y), ',',\r
+ mr(p.cp2x), ',', mr(p.cp2y), ',',\r
+ mr(p.x), ',', mr(p.y));\r
+ break;\r
+ case 'at':\r
+ case 'wa':\r
+ lineStr.push(' ', p.type, ' ',\r
+ mr(p.x - this.arcScaleX_ * p.radius), ',',\r
+ mr(p.y - this.arcScaleY_ * p.radius), ' ',\r
+ mr(p.x + this.arcScaleX_ * p.radius), ',',\r
+ mr(p.y + this.arcScaleY_ * p.radius), ' ',\r
+ mr(p.xStart), ',', mr(p.yStart), ' ',\r
+ mr(p.xEnd), ',', mr(p.yEnd));\r
+ break;\r
+ }\r
+\r
+\r
+ // TODO: Following is broken for curves due to\r
+ // move to proper paths.\r
+\r
+ // Figure out dimensions so we can do gradient fills\r
+ // properly\r
+ if (p) {\r
+ if (min.x == null || p.x < min.x) {\r
+ min.x = p.x;\r
+ }\r
+ if (max.x == null || p.x > max.x) {\r
+ max.x = p.x;\r
+ }\r
+ if (min.y == null || p.y < min.y) {\r
+ min.y = p.y;\r
+ }\r
+ if (max.y == null || p.y > max.y) {\r
+ max.y = p.y;\r
+ }\r
+ }\r
+ }\r
+ lineStr.push(' ">');\r
+\r
+ if (!aFill) {\r
+ var lineWidth = this.lineScale_ * this.lineWidth;\r
+\r
+ // VML cannot correctly render a line if the width is less than 1px.\r
+ // In that case, we dilute the color to make the line look thinner.\r
+ if (lineWidth < 1) {\r
+ opacity *= lineWidth;\r
+ }\r
+\r
+ lineStr.push(\r
+ '<g_vml_:stroke',\r
+ ' opacity="', opacity, '"',\r
+ ' joinstyle="', this.lineJoin, '"',\r
+ ' miterlimit="', this.miterLimit, '"',\r
+ ' endcap="', processLineCap(this.lineCap), '"',\r
+ ' weight="', lineWidth, 'px"',\r
+ ' color="', color, '" />'\r
+ );\r
+ } else if (typeof this.fillStyle == 'object') {\r
+ var fillStyle = this.fillStyle;\r
+ var angle = 0;\r
+ var focus = {x: 0, y: 0};\r
+\r
+ // additional offset\r
+ var shift = 0;\r
+ // scale factor for offset\r
+ var expansion = 1;\r
+\r
+ if (fillStyle.type_ == 'gradient') {\r
+ var x0 = fillStyle.x0_ / this.arcScaleX_;\r
+ var y0 = fillStyle.y0_ / this.arcScaleY_;\r
+ var x1 = fillStyle.x1_ / this.arcScaleX_;\r
+ var y1 = fillStyle.y1_ / this.arcScaleY_;\r
+ var p0 = this.getCoords_(x0, y0);\r
+ var p1 = this.getCoords_(x1, y1);\r
+ var dx = p1.x - p0.x;\r
+ var dy = p1.y - p0.y;\r
+ angle = Math.atan2(dx, dy) * 180 / Math.PI;\r
+\r
+ // The angle should be a non-negative number.\r
+ if (angle < 0) {\r
+ angle += 360;\r
+ }\r
+\r
+ // Very small angles produce an unexpected result because they are\r
+ // converted to a scientific notation string.\r
+ if (angle < 1e-6) {\r
+ angle = 0;\r
+ }\r
+ } else {\r
+ var p0 = this.getCoords_(fillStyle.x0_, fillStyle.y0_);\r
+ var width = max.x - min.x;\r
+ var height = max.y - min.y;\r
+ focus = {\r
+ x: (p0.x - min.x) / width,\r
+ y: (p0.y - min.y) / height\r
+ };\r
+\r
+ width /= this.arcScaleX_ * Z;\r
+ height /= this.arcScaleY_ * Z;\r
+ var dimension = m.max(width, height);\r
+ shift = 2 * fillStyle.r0_ / dimension;\r
+ expansion = 2 * fillStyle.r1_ / dimension - shift;\r
+ }\r
+\r
+ // We need to sort the color stops in ascending order by offset,\r
+ // otherwise IE won't interpret it correctly.\r
+ var stops = fillStyle.colors_;\r
+ stops.sort(function(cs1, cs2) {\r
+ return cs1.offset - cs2.offset;\r
+ });\r
+\r
+ var length = stops.length;\r
+ var color1 = stops[0].color;\r
+ var color2 = stops[length - 1].color;\r
+ var opacity1 = stops[0].alpha * this.globalAlpha;\r
+ var opacity2 = stops[length - 1].alpha * this.globalAlpha;\r
+\r
+ var colors = [];\r
+ for (var i = 0; i < length; i++) {\r
+ var stop = stops[i];\r
+ colors.push(stop.offset * expansion + shift + ' ' + stop.color);\r
+ }\r
+\r
+ // When colors attribute is used, the meanings of opacity and o:opacity2\r
+ // are reversed.\r
+ lineStr.push('<g_vml_:fill type="', fillStyle.type_, '"',\r
+ ' method="none" focus="100%"',\r
+ ' color="', color1, '"',\r
+ ' color2="', color2, '"',\r
+ ' colors="', colors.join(','), '"',\r
+ ' opacity="', opacity2, '"',\r
+ ' g_o_:opacity2="', opacity1, '"',\r
+ ' angle="', angle, '"',\r
+ ' focusposition="', focus.x, ',', focus.y, '" />');\r
+ } else {\r
+ lineStr.push('<g_vml_:fill color="', color, '" opacity="', opacity,\r
+ '" />');\r
+ }\r
+\r
+ lineStr.push('</g_vml_:shape>');\r
+\r
+ this.element_.insertAdjacentHTML('beforeEnd', lineStr.join(''));\r
+ };\r
+\r
+ contextPrototype.fill = function() {\r
+ this.stroke(true);\r
+ }\r
+\r
+ contextPrototype.closePath = function() {\r
+ this.currentPath_.push({type: 'close'});\r
+ };\r
+\r
+ /**\r
+ * @private\r
+ */\r
+ contextPrototype.getCoords_ = function(aX, aY) {\r
+ var m = this.m_;\r
+ return {\r
+ x: Z * (aX * m[0][0] + aY * m[1][0] + m[2][0]) - Z2,\r
+ y: Z * (aX * m[0][1] + aY * m[1][1] + m[2][1]) - Z2\r
+ }\r
+ };\r
+\r
+ contextPrototype.save = function() {\r
+ var o = {};\r
+ copyState(this, o);\r
+ this.aStack_.push(o);\r
+ this.mStack_.push(this.m_);\r
+ this.m_ = matrixMultiply(createMatrixIdentity(), this.m_);\r
+ };\r
+\r
+ contextPrototype.restore = function() {\r
+ copyState(this.aStack_.pop(), this);\r
+ this.m_ = this.mStack_.pop();\r
+ };\r
+\r
+ function matrixIsFinite(m) {\r
+ for (var j = 0; j < 3; j++) {\r
+ for (var k = 0; k < 2; k++) {\r
+ if (!isFinite(m[j][k]) || isNaN(m[j][k])) {\r
+ return false;\r
+ }\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ function setM(ctx, m, updateLineScale) {\r
+ if (!matrixIsFinite(m)) {\r
+ return;\r
+ }\r
+ ctx.m_ = m;\r
+\r
+ if (updateLineScale) {\r
+ // Get the line scale.\r
+ // Determinant of this.m_ means how much the area is enlarged by the\r
+ // transformation. So its square root can be used as a scale factor\r
+ // for width.\r
+ var det = m[0][0] * m[1][1] - m[0][1] * m[1][0];\r
+ ctx.lineScale_ = sqrt(abs(det));\r
+ }\r
+ }\r
+\r
+ contextPrototype.translate = function(aX, aY) {\r
+ var m1 = [\r
+ [1, 0, 0],\r
+ [0, 1, 0],\r
+ [aX, aY, 1]\r
+ ];\r
+\r
+ setM(this, matrixMultiply(m1, this.m_), false);\r
+ };\r
+\r
+ contextPrototype.rotate = function(aRot) {\r
+ var c = mc(aRot);\r
+ var s = ms(aRot);\r
+\r
+ var m1 = [\r
+ [c, s, 0],\r
+ [-s, c, 0],\r
+ [0, 0, 1]\r
+ ];\r
+\r
+ setM(this, matrixMultiply(m1, this.m_), false);\r
+ };\r
+\r
+ contextPrototype.scale = function(aX, aY) {\r
+ this.arcScaleX_ *= aX;\r
+ this.arcScaleY_ *= aY;\r
+ var m1 = [\r
+ [aX, 0, 0],\r
+ [0, aY, 0],\r
+ [0, 0, 1]\r
+ ];\r
+\r
+ setM(this, matrixMultiply(m1, this.m_), true);\r
+ };\r
+\r
+ contextPrototype.transform = function(m11, m12, m21, m22, dx, dy) {\r
+ var m1 = [\r
+ [m11, m12, 0],\r
+ [m21, m22, 0],\r
+ [dx, dy, 1]\r
+ ];\r
+\r
+ setM(this, matrixMultiply(m1, this.m_), true);\r
+ };\r
+\r
+ contextPrototype.setTransform = function(m11, m12, m21, m22, dx, dy) {\r
+ var m = [\r
+ [m11, m12, 0],\r
+ [m21, m22, 0],\r
+ [dx, dy, 1]\r
+ ];\r
+\r
+ setM(this, m, true);\r
+ };\r
+\r
+ /******** STUBS ********/\r
+ contextPrototype.clip = function() {\r
+ // TODO: Implement\r
+ };\r
+\r
+ contextPrototype.arcTo = function() {\r
+ // TODO: Implement\r
+ };\r
+\r
+ contextPrototype.createPattern = function() {\r
+ return new CanvasPattern_;\r
+ };\r
+\r
+ // Gradient / Pattern Stubs\r
+ function CanvasGradient_(aType) {\r
+ this.type_ = aType;\r
+ this.x0_ = 0;\r
+ this.y0_ = 0;\r
+ this.r0_ = 0;\r
+ this.x1_ = 0;\r
+ this.y1_ = 0;\r
+ this.r1_ = 0;\r
+ this.colors_ = [];\r
+ }\r
+\r
+ CanvasGradient_.prototype.addColorStop = function(aOffset, aColor) {\r
+ aColor = processStyle(aColor);\r
+ this.colors_.push({offset: aOffset,\r
+ color: aColor.color,\r
+ alpha: aColor.alpha});\r
+ };\r
+\r
+ function CanvasPattern_() {}\r
+\r
+ // set up externs\r
+ G_vmlCanvasManager = G_vmlCanvasManager_;\r
+ CanvasRenderingContext2D = CanvasRenderingContext2D_;\r
+ CanvasGradient = CanvasGradient_;\r
+ CanvasPattern = CanvasPattern_;\r
+\r
+})();\r
+\r
+} // if\r
+\r
+if (dhtmlx && dhtmlx._modules)\r
+ dhtmlx._modules["thirdparty/excanvas/excanvas.js"] = true;
\ No newline at end of file