4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-draw-engine-Vml'>/**
19 </span> * @class Ext.draw.engine.Vml
20 * @extends Ext.draw.Surface
21 * Provides specific methods to draw with VML.
24 Ext.define('Ext.draw.engine.Vml', {
26 /* Begin Definitions */
28 extend: 'Ext.draw.Surface',
30 requires: ['Ext.draw.Draw', 'Ext.draw.Color', 'Ext.draw.Sprite', 'Ext.draw.Matrix', 'Ext.Element'],
36 map: {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
37 bitesRe: /([clmz]),?([^clmz]*)/gi,
39 fillUrlRe: /^url\(\s*['"]?([^\)]+?)['"]?\s*\)$/i,
40 pathlike: /^(path|rect)$/,
41 NonVmlPathRe: /[ahqstv]/ig, // Non-VML Pathing ops
42 partialPathRe: /[clmz]/g,
43 fontFamilyRe: /^['"]+|['"]+$/g,
44 baseVmlCls: Ext.baseCSSPrefix + 'vml-base',
45 vmlGroupCls: Ext.baseCSSPrefix + 'vml-group',
46 spriteCls: Ext.baseCSSPrefix + 'vml-sprite',
47 measureSpanCls: Ext.baseCSSPrefix + 'vml-measure-span',
52 // VML uses CSS z-index and therefore doesn't need sprites to be kept in zIndex order
53 orderSpritesByZIndex: false,
56 // Convert an SVG standard path into a VML path
57 path2vml: function (path) {
59 nonVML = me.NonVmlPathRe,
64 command = Ext.Function.bind(Ext.draw.Draw.pathToAbsolute, Ext.draw.Draw),
65 res, pa, p, r, i, ii, j, jj;
66 if (String(path).match(nonVML)) {
67 command = Ext.Function.bind(Ext.draw.Draw.path2curve, Ext.draw.Draw);
68 } else if (!String(path).match(me.partialPathRe)) {
69 res = String(path).replace(bites, function (all, command, args) {
71 isMove = command.toLowerCase() == "m",
73 args.replace(val, function (value) {
74 if (isMove && vals[length] == 2) {
75 res += vals + map[command == "m" ? "l" : "L"];
78 vals.push(Math.round(value * zoom));
86 for (i = 0, ii = pa.length; i < ii; i++) {
88 r = pa[i][0].toLowerCase();
89 if (r == "z") {
92 for (j = 1, jj = p.length; j < jj; j++) {
93 r += Math.round(p[j] * me.zoom) + (j != jj - 1 ? "," : "");
97 return res.join(" ");
100 // @private - set of attributes which need to be translated from the sprite API to the native browser API
102 radius: "r",
103 radiusX: "rx",
104 radiusY: "ry",
105 lineWidth: "stroke-width",
106 fillOpacity: "fill-opacity",
107 strokeOpacity: "stroke-opacity",
108 strokeLinejoin: "stroke-linejoin"
111 // @private - Minimun set of defaults for different types of sprites.
114 fill: "none",
116 "stroke-width": null,
118 "fill-opacity": null,
119 "stroke-opacity": null
126 fill: "none",
128 "stroke-width": null,
130 "fill-opacity": null,
131 "stroke-opacity": null
140 fill: "none",
142 "stroke-width": null,
144 "fill-opacity": null,
145 "stroke-opacity": null
150 "text-anchor": "start",
151 font: '10px "Arial"',
152 fill: "#000",
154 "stroke-width": null,
156 "fill-opacity": null,
157 "stroke-opacity": null
161 fill: "none",
163 "stroke-width": null,
165 "fill-opacity": null,
166 "stroke-opacity": null
173 preserveAspectRatio: "none",
179 onMouseEnter: function(e) {
180 this.fireEvent("mouseenter", e);
184 onMouseLeave: function(e) {
185 this.fireEvent("mouseleave", e);
188 // @private - Normalize a delegated single event from the main container to each sprite and sprite group
189 processEvent: function(name, e) {
190 var target = e.getTarget(),
191 surface = this.surface,
193 this.fireEvent(name, e);
194 sprite = this.items.get(target.id);
196 sprite.fireEvent(name, sprite, e);
200 // Create the VML element/elements and append them to the DOM
201 createSpriteElement: function(sprite) {
206 vml = sprite.vml || (sprite.vml = {}),
208 el = me.createNode('shape'),
209 path, skew, textPath;
211 el.coordsize = zoom + ' ' + zoom;
212 el.coordorigin = attr.coordorigin || "0 0";
213 Ext.get(el).addCls(me.spriteCls);
214 if (type == "text") {
215 vml.path = path = me.createNode("path");
216 path.textpathok = true;
217 vml.textpath = textPath = me.createNode("textpath");
219 el.appendChild(textPath);
220 el.appendChild(path);
223 sprite.el = Ext.get(el);
224 me.el.appendChild(el);
225 if (type !== 'image') {
226 skew = me.createNode("skew");
228 el.appendChild(skew);
231 sprite.matrix = Ext.create('Ext.draw.Matrix');
236 sprite.fireEvent("render", sprite);
240 // @private - Get bounding box for the sprite. The Sprite itself has the public method.
241 getBBox: function (sprite, isWithoutTransform) {
242 var realPath = this["getPath" + sprite.type](sprite);
243 if (isWithoutTransform) {
244 sprite.bbox.plain = sprite.bbox.plain || Ext.draw.Draw.pathDimensions(realPath);
245 return sprite.bbox.plain;
247 sprite.bbox.transform = sprite.bbox.transform || Ext.draw.Draw.pathDimensions(Ext.draw.Draw.mapPath(realPath, sprite.matrix));
248 return sprite.bbox.transform;
251 getBBoxText: function (sprite) {
252 var vml = sprite.vml;
254 x: vml.X + (vml.bbx || 0) - vml.W / 2,
255 y: vml.Y - vml.H / 2,
261 applyAttrs: function (sprite) {
264 group = sprite.group,
265 spriteAttr = sprite.attr,
268 style, name, groups, i, ln, scrubbedAttrs, font, key, bbox;
271 groups = [].concat(group);
273 for (i = 0; i < ln; i++) {
275 me.getGroup(group).add(sprite);
279 scrubbedAttrs = me.scrubAttrs(sprite) || {};
281 if (sprite.zIndexDirty) {
282 me.setZIndex(sprite);
285 // Apply minimum default attributes
286 Ext.applyIf(scrubbedAttrs, me.minDefaults[sprite.type]);
289 dom.href = scrubbedAttrs.href;
292 dom.title = scrubbedAttrs.title;
295 dom.target = scrubbedAttrs.target;
298 dom.cursor = scrubbedAttrs.cursor;
302 if (sprite.dirtyHidden) {
303 (scrubbedAttrs.hidden) ? me.hidePrim(sprite) : me.showPrim(sprite);
304 sprite.dirtyHidden = false;
308 if (sprite.dirtyPath) {
309 if (sprite.type == "circle" || sprite.type == "ellipse") {
310 var cx = scrubbedAttrs.x,
311 cy = scrubbedAttrs.y,
312 rx = scrubbedAttrs.rx || scrubbedAttrs.r || 0,
313 ry = scrubbedAttrs.ry || scrubbedAttrs.r || 0;
314 dom.path = Ext.String.format("ar{0},{1},{2},{3},{4},{1},{4},{1}",
315 Math.round((cx - rx) * me.zoom),
316 Math.round((cy - ry) * me.zoom),
317 Math.round((cx + rx) * me.zoom),
318 Math.round((cy + ry) * me.zoom),
319 Math.round(cx * me.zoom));
320 sprite.dirtyPath = false;
322 else if (sprite.type !== "text") {
323 sprite.attr.path = scrubbedAttrs.path = me.setPaths(sprite, scrubbedAttrs) || scrubbedAttrs.path;
324 dom.path = me.path2vml(scrubbedAttrs.path);
325 sprite.dirtyPath = false;
330 if ("clip-rect" in scrubbedAttrs) {
331 me.setClip(sprite, scrubbedAttrs);
334 // Handle text (special handling required)
335 if (sprite.type == "text") {
336 me.setTextAttributes(sprite, scrubbedAttrs);
339 // Handle fill and opacity
340 if (sprite.type == 'image' || scrubbedAttrs.opacity || scrubbedAttrs['fill-opacity'] || scrubbedAttrs.fill) {
341 me.setFill(sprite, scrubbedAttrs);
344 // Handle stroke (all fills require a stroke element)
345 if (scrubbedAttrs.stroke || scrubbedAttrs['stroke-opacity'] || scrubbedAttrs.fill) {
346 me.setStroke(sprite, scrubbedAttrs);
350 style = spriteAttr.style;
355 sprite.dirty = false;
358 setZIndex: function(sprite) {
360 if (sprite.attr.zIndex != undefined) {
361 sprite.el.setStyle('zIndex', sprite.attr.zIndex);
363 sprite.zIndexDirty = false;
367 // Normalize all virtualized types into paths.
368 setPaths: function(sprite, params) {
369 var spriteAttr = sprite.attr;
371 sprite.bbox.plain = null;
372 sprite.bbox.transform = null;
373 if (sprite.type == 'circle') {
374 spriteAttr.rx = spriteAttr.ry = params.r;
375 return Ext.draw.Draw.ellipsePath(sprite);
377 else if (sprite.type == 'ellipse') {
378 spriteAttr.rx = params.rx;
379 spriteAttr.ry = params.ry;
380 return Ext.draw.Draw.ellipsePath(sprite);
382 else if (sprite.type == 'rect' || sprite.type == 'image') {
383 spriteAttr.rx = spriteAttr.ry = params.r;
384 return Ext.draw.Draw.rectPath(sprite);
386 else if (sprite.type == 'path' && spriteAttr.path) {
387 return Ext.draw.Draw.pathToAbsolute(spriteAttr.path);
392 setFill: function(sprite, params) {
396 fillEl = dom.getElementsByTagName('fill')[0],
397 opacity, gradient, fillUrl, rotation, angle;
400 dom.removeChild(fillEl);
402 fillEl = me.createNode('fill');
404 if (Ext.isArray(params.fill)) {
405 params.fill = params.fill[0];
407 if (sprite.type == 'image') {
409 fillEl.src = params.src;
410 fillEl.type = "tile";
411 fillEl.rotate = true;
412 } else if (params.fill == "none") {
415 if (typeof params.opacity == "number") {
416 fillEl.opacity = params.opacity;
418 if (typeof params["fill-opacity"] == "number") {
419 fillEl.opacity = params["fill-opacity"];
422 if (typeof params.fill == "string") {
423 fillUrl = params.fill.match(me.fillUrlRe);
425 fillUrl = fillUrl[1];
426 // If the URL matches one of the registered gradients, render that gradient
427 if (fillUrl.charAt(0) == "#") {
428 gradient = me.gradientsColl.getByKey(fillUrl.substring(1));
431 // VML angle is offset and inverted from standard, and must be adjusted to match rotation transform
432 rotation = params.rotation;
433 angle = -(gradient.angle + 270 + (rotation ? rotation.degrees : 0)) % 360;
434 // IE will flip the angle at 0 degrees...
438 fillEl.angle = angle;
439 fillEl.type = "gradient";
440 fillEl.method = "sigma";
441 fillEl.colors = gradient.colors;
443 // Otherwise treat it as an image
445 fillEl.src = fillUrl;
446 fillEl.type = "tile";
447 fillEl.rotate = true;
451 fillEl.color = Ext.draw.Color.toHex(params.fill) || params.fill;
452 fillEl.src = "";
453 fillEl.type = "solid";
457 dom.appendChild(fillEl);
460 setStroke: function(sprite, params) {
463 strokeEl = sprite.strokeEl,
468 strokeEl = sprite.strokeEl = me.createNode("stroke");
471 if (Ext.isArray(params.stroke)) {
472 params.stroke = params.stroke[0];
474 if (!params.stroke || params.stroke == "none" || params.stroke == 0 || params["stroke-width"] == 0) {
479 if (params.stroke && !params.stroke.match(me.fillUrlRe)) {
480 // VML does NOT support a gradient stroke :(
481 strokeEl.color = Ext.draw.Color.toHex(params.stroke);
483 strokeEl.joinstyle = params["stroke-linejoin"];
484 strokeEl.endcap = params["stroke-linecap"] || "round";
485 strokeEl.miterlimit = params["stroke-miterlimit"] || 8;
486 width = parseFloat(params["stroke-width"] || 1) * 0.75;
487 opacity = params["stroke-opacity"] || 1;
488 // VML Does not support stroke widths under 1, so we're going to fiddle with stroke-opacity instead.
489 if (Ext.isNumber(width) && width < 1) {
491 strokeEl.opacity = opacity * width;
494 strokeEl.weight = width;
495 strokeEl.opacity = opacity;
499 el.appendChild(strokeEl);
503 setClip: function(sprite, params) {
506 clipEl = sprite.clipEl,
507 rect = String(params["clip-rect"]).split(me.separatorRe);
509 clipEl = sprite.clipEl = me.el.insertFirst(Ext.getDoc().dom.createElement("div"));
510 clipEl.addCls(Ext.baseCSSPrefix + 'vml-sprite');
512 if (rect.length == 4) {
513 rect[2] = +rect[2] + (+rect[0]);
514 rect[3] = +rect[3] + (+rect[1]);
515 clipEl.setStyle("clip", Ext.String.format("rect({1}px {2}px {3}px {0}px)", rect[0], rect[1], rect[2], rect[3]));
516 clipEl.setSize(me.el.width, me.el.height);
519 clipEl.setStyle("clip", "");
523 setTextAttributes: function(sprite, params) {
526 textStyle = vml.textpath.style,
527 spanCacheStyle = me.span.style,
531 fontSize: "font-size",
532 fontWeight: "font-weight",
533 fontStyle: "font-style"
537 if (sprite.dirtyFont) {
539 textStyle.font = spanCacheStyle.font = params.font;
541 if (params["font-family"]) {
542 textStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(me.fontFamilyRe, "") + '"';
543 spanCacheStyle.fontFamily = params["font-family"];
546 for (fontProp in fontObj) {
547 paramProp = params[fontObj[fontProp]];
549 textStyle[fontProp] = spanCacheStyle[fontProp] = paramProp;
553 me.setText(sprite, params.text);
555 if (vml.textpath.string) {
556 me.span.innerHTML = String(vml.textpath.string).replace(/</g, "&#60;").replace(/&/g, "&#38;").replace(/\n/g, "<br>");
558 vml.W = me.span.offsetWidth;
559 vml.H = me.span.offsetHeight + 2; // TODO handle baseline differences and offset in VML Textpath
561 // text-anchor emulation
562 if (params["text-anchor"] == "middle") {
563 textStyle["v-text-align"] = "center";
565 else if (params["text-anchor"] == "end") {
566 textStyle["v-text-align"] = "right";
567 vml.bbx = -Math.round(vml.W / 2);
570 textStyle["v-text-align"] = "left";
571 vml.bbx = Math.round(vml.W / 2);
576 vml.path.v = Ext.String.format("m{0},{1}l{2},{1}", Math.round(vml.X * zoom), Math.round(vml.Y * zoom), Math.round(vml.X * zoom) + 1);
578 sprite.bbox.plain = null;
579 sprite.bbox.transform = null;
580 sprite.dirtyFont = false;
583 setText: function(sprite, text) {
584 sprite.vml.textpath.string = Ext.htmlDecode(text);
595 hidePrim: function(sprite) {
596 sprite.el.addCls(Ext.baseCSSPrefix + 'hide-visibility');
599 showPrim: function(sprite) {
600 sprite.el.removeCls(Ext.baseCSSPrefix + 'hide-visibility');
603 setSize: function(width, height) {
605 width = width || me.width;
606 height = height || me.height;
612 if (width != undefined) {
613 me.el.setWidth(width);
615 if (height != undefined) {
616 me.el.setHeight(height);
619 // Handle viewBox sizing
622 me.callParent(arguments);
626 setViewBox: function(x, y, width, height) {
627 this.callParent(arguments);
637 <span id='Ext-draw-engine-Vml-method-applyViewBox'> /**
638 </span> * @private Using the current viewBox property and the surface's width and height, calculate the
639 * appropriate viewBoxShift that will be applied as a persistent transform to all sprites.
641 applyViewBox: function() {
643 viewBox = me.viewBox,
646 viewBoxX, viewBoxY, viewBoxWidth, viewBoxHeight,
647 relativeHeight, relativeWidth, size;
649 if (viewBox && (width || height)) {
650 viewBoxX = viewBox.x;
651 viewBoxY = viewBox.y;
652 viewBoxWidth = viewBox.width;
653 viewBoxHeight = viewBox.height;
654 relativeHeight = height / viewBoxHeight;
655 relativeWidth = width / viewBoxWidth;
657 if (viewBoxWidth * relativeHeight < width) {
658 viewBoxX -= (width - viewBoxWidth * relativeHeight) / 2 / relativeHeight;
660 if (viewBoxHeight * relativeWidth < height) {
661 viewBoxY -= (height - viewBoxHeight * relativeWidth) / 2 / relativeWidth;
664 size = 1 / Math.max(viewBoxWidth / width, viewBoxHeight / height);
671 me.items.each(function(item) {
677 onAdd: function(item) {
678 this.callParent(arguments);
680 this.renderItem(item);
684 onRemove: function(sprite) {
689 this.callParent(arguments);
692 // VML Node factory method (createNode)
693 createNode : (function () {
695 var doc = Ext.getDoc().dom;
696 if (!doc.namespaces.rvml) {
697 doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
699 return function (tagName) {
700 return doc.createElement("<rvml:" + tagName + ' class="rvml">');
703 return function (tagName) {
704 return doc.createElement("<" + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
709 render: function (container) {
711 doc = Ext.getDoc().dom;
714 var el = doc.createElement("div");
716 me.el.addCls(me.baseVmlCls);
718 // Measuring span (offscrren)
719 me.span = doc.createElement("span");
720 Ext.get(me.span).addCls(me.measureSpanCls);
721 el.appendChild(me.span);
722 me.el.setSize(me.width || 10, me.height || 10);
723 container.appendChild(el);
726 mouseup: me.onMouseUp,
727 mousedown: me.onMouseDown,
728 mouseover: me.onMouseOver,
729 mouseout: me.onMouseOut,
730 mousemove: me.onMouseMove,
731 mouseenter: me.onMouseEnter,
732 mouseleave: me.onMouseLeave,
739 renderAll: function() {
740 this.items.each(this.renderItem, this);
743 redraw: function(sprite) {
745 this.renderItem(sprite);
748 renderItem: function (sprite) {
749 // Does the surface element exist?
754 // Create sprite element if necessary
756 this.createSpriteElement(sprite);
760 this.applyAttrs(sprite);
761 if (sprite.dirtyTransform) {
762 this.applyTransformations(sprite);
767 rotationCompensation: function (deg, dx, dy) {
768 var matrix = Ext.create('Ext.draw.Matrix');
769 matrix.rotate(-deg, 0.5, 0.5);
776 extractTransform: function (sprite) {
778 matrix = Ext.create('Ext.draw.Matrix'), scale,
779 transformstions, tranformationsLength,
781 shift = me.viewBoxShift;
783 for(transformstions = sprite.transformations, tranformationsLength = transformstions.length;
784 i < tranformationsLength; i ++) {
785 transform = transformstions[i];
786 switch (transform.type) {
788 matrix.translate(transform.x, transform.y);
791 matrix.rotate(transform.degrees, transform.x, transform.y);
794 matrix.scale(transform.x || transform.scale, transform.y || transform.scale, transform.centerX, transform.centerY);
800 matrix.add(1, 0, 0, 1, shift.dx, shift.dy);
801 matrix.prepend(shift.scale, 0, 0, shift.scale, 0, 0);
804 return sprite.matrix = matrix;
807 setSimpleCoords: function(sprite, sx, sy, dx, dy, rotate) {
809 matrix = sprite.matrix,
814 fill = dom.getElementsByTagName('fill')[0],
817 rotationCompensation;
821 dom.coordsize = Math.abs(kx) + ' ' + Math.abs(ky);
822 style.rotation = rotate * (sx * sy < 0 ? -1 : 1);
824 rotationCompensation = me.rotationCompensation(rotate, dx, dy);
825 dx = rotationCompensation.x;
826 dy = rotationCompensation.y;
829 flip += "x"
832 flip += " y";
836 dom.coordorigin = (dx * -kx) + ' ' + (dy * -ky);
838 dom.removeChild(fill);
839 rotationCompensation = me.rotationCompensation(rotate, matrix.x(sprite.x, sprite.y), matrix.y(sprite.x, sprite.y));
840 fill.position = rotationCompensation.x * yFlipper + ' ' + rotationCompensation.y * yFlipper;
841 fill.size = sprite.width * Math.abs(sx) + ' ' + sprite.height * Math.abs(sy);
842 dom.appendChild(fill);
846 transform : function (sprite) {
851 domStyle = dom.style,
852 matrix = me.extractTransform(sprite).clone(),
853 split, zoom = me.zoom,
854 fill = dom.getElementsByTagName('fill')[0],
855 isPatt = !String(sprite.fill).indexOf("url("),
859 // Hide element while we transform
861 if (sprite.type != "image" && skew && !isPatt) {
862 // matrix transform via VML skew
863 skew.matrix = matrix.toString();
864 // skew.offset = '32767,1' OK
865 // skew.offset = '32768,1' Crash
867 offset = matrix.offset();
868 if (offset[0] > 32767) {
870 } else if (offset[0] < -32768) {
873 if (offset[1] > 32767) {
875 } else if (offset[1] < -32768) {
878 skew.offset = offset;
881 skew.matrix = "1 0 0 1";
882 skew.offset = "0 0";
884 split = matrix.split();
885 if (split.isSimple) {
886 domStyle.filter = '';
887 me.setSimpleCoords(sprite, split.scaleX, split.scaleY, split.translateX, split.translateY, split.rotate / Math.PI * 180);
889 domStyle.filter = matrix.toFilter();
890 var bb = me.getBBox(sprite),
891 dx = bb.x - sprite.x,
892 dy = bb.y - sprite.y;
893 dom.coordorigin = (dx * -zoom) + ' ' + (dy * -zoom);
895 dom.removeChild(fill);
896 fill.position = dx + ' ' + dy;
897 fill.size = sprite.width * sprite.scale.x + ' ' + sprite.height * 1.1;
898 dom.appendChild(fill);
904 createItem: function (config) {
905 return Ext.create('Ext.draw.Sprite', config);
908 getRegion: function() {
909 return this.el.getRegion();
912 addCls: function(sprite, className) {
913 if (sprite && sprite.el) {
914 sprite.el.addCls(className);
918 removeCls: function(sprite, className) {
919 if (sprite && sprite.el) {
920 sprite.el.removeCls(className);
924 <span id='Ext-draw-engine-Vml-method-addGradient'> /**
925 </span> * Adds a definition to this Surface for a linear gradient. We convert the gradient definition
926 * to its corresponding VML attributes and store it for later use by individual sprites.
927 * @param {Object} gradient
929 addGradient: function(gradient) {
930 var gradients = this.gradientsColl || (this.gradientsColl = Ext.create('Ext.util.MixedCollection')),
932 stops = Ext.create('Ext.util.MixedCollection');
934 // Build colors string
935 stops.addAll(gradient.stops);
936 stops.sortByKey("ASC", function(a, b) {
939 return a > b ? 1 : (a < b ? -1 : 0);
941 stops.eachKey(function(k, v) {
942 colors.push(k + "% " + v.color);
945 gradients.add(gradient.id, {
946 colors: colors.join(","),
947 angle: gradient.angle
951 destroy: function() {
954 me.callParent(arguments);