svnno****@sourc*****
svnno****@sourc*****
2009年 9月 23日 (水) 22:24:03 JST
Revision: 1277 http://sourceforge.jp/projects/sie/svn/view?view=rev&revision=1277 Author: dhrname Date: 2009-09-23 22:24:03 +0900 (Wed, 23 Sep 2009) Log Message: ----------- SVGMatrixオブジェクトの実装 Modified Paths: -------------- branches/DOM/org/w3c/dom/svg.js Modified: branches/DOM/org/w3c/dom/svg.js =================================================================== --- branches/DOM/org/w3c/dom/svg.js 2009-09-22 13:47:07 UTC (rev 1276) +++ branches/DOM/org/w3c/dom/svg.js 2009-09-23 13:24:03 UTC (rev 1277) @@ -1198,41 +1198,6 @@ return matri; } -//SVGPointを参照 -function Point( /*number*/ x, /*number*/ y) { - this.x = x; this.y = y; - return this; -} -Point.prototype.matrixTransform = function pmatrixtransform( /*Matrix*/ m) { - var x = parseInt(m.a * this.x + m.c * this.y + m.e); - var y = parseInt(m.b * this.x + m.d * this.y + m.f); - if (-1 < x && x < 1) {x=1;} - if (-1 < y && y < 1) {y=1;} - var s = new Point(x,y); - return s; -} - -//Pointのリスト。一括で処理できる -function PList( /*Array*/ d) { - this.list = d; - return this; -} -PList.prototype.matrixTransform = function plmatrixtransform( /*Matrix*/ ttm) { - var F = this.list; - for (var i = 0, Fli = F.length; i < Fli;) { - if (isNaN(F[i])) { //コマンド文字は読み飛ばす - i++; - continue; - } - var p = new Point(parseFloat(F[i]), parseFloat(F[i+1])); - var pmt = p.matrixTransform(ttm); - F[i++] = pmt.x; - F[i++] = pmt.y; - } - var s = new PList(F); - return s; -} - //SVGMatrixを参照。行列 function Matrix(a,b,c,d,e,f) { //引数はすべてNumber型 this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; @@ -2338,66 +2303,184 @@ function SVGStyleElement() { SVGElement.apply(this, arguments); //以下はそれぞれ、属性の値に対応している - /*DOMString*/ xmlspace; - /*DOMString*/ type = "text/css"; - /*DOMString*/ media; - /*DOMString*/ title; + /*DOMString*/ this.xmlspace; + /*DOMString*/ this.type = "text/css"; + /*DOMString*/ this.media; + /*DOMString*/ this.title; return this; }; SVGStyleElement.constructor = SVGElement; SVGStyleElement.prototype = new SVGElement(); -function SVGPoint { - float x; - // raises DOMException on setting - float y; - // raises DOMException on setting - SVGPoint matrixTransform ( in SVGMatrix matrix ); - }; -function SVGPointList { - /*readonly unsigned long numberOfItems; - void clear ( ) - raises( DOMException ); - SVGPoint initialize ( in SVGPoint newItem ) - raises( DOMException, SVGException ); - SVGPoint getItem ( in unsigned long index ) - raises( DOMException ); - SVGPoint insertItemBefore ( in SVGPoint newItem, in unsigned long index ) - raises( DOMException, SVGException ); - SVGPoint replaceItem ( in SVGPoint newItem, in unsigned long index ) - raises( DOMException, SVGException ); - SVGPoint removeItem ( in unsigned long index ) - raises( DOMException ); - SVGPoint appendItem ( in SVGPoint newItem ) - raises( DOMException, SVGException ); - }; -function SVGMatrix { - float a; - // raises DOMException on setting - float b; - // raises DOMException on setting - float c; - // raises DOMException on setting - float d; - // raises DOMException on setting - float e; - // raises DOMException on setting - float f; - // raises DOMException on setting - SVGMatrix multiply ( in SVGMatrix secondMatrix ); - SVGMatrix inverse ( ) - raises( SVGException ); - SVGMatrix translate ( in float x, in float y ); - SVGMatrix scale ( in float scaleFactor ); - SVGMatrix scaleNonUniform ( in float scaleFactorX, in float scaleFactorY ); - SVGMatrix rotate ( in float angle ); - SVGMatrix rotateFromVector ( in float x, in float y ) - raises( SVGException ); - SVGMatrix flipX ( ); - SVGMatrix flipY ( ); - SVGMatrix skewX ( in float angle ); - SVGMatrix skewY ( in float angle ); - }; +/*SVGPoint + *2次元座標の点(x,y)を表すオブジェクト + */ +function SVGPoint() { + /*float*/ this.x = 0; + /*float*/ this.y = 0; + return this; +}; +SVGPoint.prototype.matrixTransform = function(/*SVGMatrix*/ matrix ) { + //整数にしているのは、VMLの設計上、小数点を扱えないときがあるため + var x = parseInt(matrix.a * this.x + matrix.c * this.y + matrix.e); + var y = parseInt(matrix.b * this.x + matrix.d * this.y + matrix.f); + if (-1 < x && x < 1) {x=1;} + if (-1 < y && y < 1) {y=1;} + var s = new SVGPoint(); + s.x = x; + s.y = y; + return s; +}; + +function SVGPointList() { + /*readonly unsigned long*/ this.numberOfItems = 0; + this._list = []; + return this; +}; +for (var prop in SVGStringList.prototype) { //prototypeのコピーで継承を行う + SVGPointList.prototype[prop] = SVGStringList.prototype[prop]; +} +//以下は独自の実装。リストを一度に処理できる +SVGPointList.prototype._matrixTransform = function ( /*Matrix*/ ttm) { + var F = this._list; + for (var i = 0, Fli = F.length; i < Fli;) { + if (isNaN(F[i])) { //コマンド文字は読み飛ばす + ++i; + continue; + } + var p = new SVGPoint(); + p.x = parseFloat(F[i]); + p.y = parseFloat(F[i+1]); + var pmt = p.matrixTransform(ttm); + F[i++] = pmt.x; + F[i++] = pmt.y; + p = pmt = null; + } + var s = new SVGPointList(); + s._list = F; + return s; +} +/*SVGMatrix + *行列をあらわすオブジェクト。写像に用いる。以下のように表現できる + *[a c e] + *[b d f] + *[0 0 1] + */ +function SVGMatrix() { + /*float*/ this.a = 1; + /*float*/ this.b = 0; + /*float*/ this.c = 0; + /*float*/ this.d = 1; + /*float*/ this.e = 0; + /*float*/ this.f = 0; + return this; +}; +SVGMatrix.prototype = { + /*SVGMatrix*/ multiply : function(/*SVGMatrix*/ secondMatrix ) { + var s = new SVGMatrix(), m = secondMatrix; + s.a = this.a * m.a + this.c * m.b; + s.b = this.b * m.a + this.d * m.b; + s.c = this.a * m.c + this.c * m.d; + s.d = this.b * m.c + this.d * m.d; + s.e = this.a * m.e + this.c * m.f + this.e; + s.f = this.b * m.e + this.d * m.f + this.f; + m = secondMatrix = null; + return s; + }, + /*SVGMatrix*/ inverse : function() { + var s = new SVGMatrix(), n = this._determinant(); + if (n !== 0) { + s.a = this.d / n; + s.b = -this.b / n; + s.c = this.c / n; + s.d = this.a / n; + s.e = (this.c * this.f - this.d * this.e) / n; + s.f = (this.b * this.e - this.a * this.f) / n; + return s; + } else { + throw (new Error(SVGException.SVG_MATRIX_NOT_INVERTABLE)) + } + }, + /*SVGMatrix*/ translate : function(/*float*/ x, /*float*/ y ) { + var m = new SVGMatrix(); + m.e = x; + m.f = y; + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ scale : function(/*float*/ scaleFactor ) { + var m = new SVGMatrix(); + m.a = scaleFactor; + m.d = scaleFactor; + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ scaleNonUniform : function(/*float*/ scaleFactorX, /*float*/ scaleFactorY ) { + var m = new SVGMatrix(); + m.a = scaleFactorX; + m.d = scaleFactorY; + var s = this.multiply(m); + m = null; + return s; + } + /*SVGMatrix*/ rotate : function(/*float*/ angle ) { + var m = new SVGMatrix(), rad = angle / 180 * Math.PI; //ラジアン変換 + m.a = Math.cos(rad); + m.b = Math.sin(rad); + m.c = -m.b; + m.d = m.a; + var s = this.multiply(m); + m = null; + return s; + }, + //座標(x, y)と原点の角度の分だけ、回転する + /*SVGMatrix*/ rotateFromVector : function(/*float*/ x, /*float*/ y ) { + if (x === 0 || y === 0) { + throw (new Error(SVGException.SVG_INVALID_VALUE_ERR)) + } + var m = this.rotate(Math.atan2(y, x)); + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ flipX : function() { + var m = new SVGMatrix(); + m.a = -m.a; + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ flipY : function() { + var m = new SVGMatrix(); + m.d = -m.d; + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ skewX : function(/*float*/ angle ){ + var m = new SVGMatrix(), rad = angle / 180 * Math.PI; //ラジアン変換 + m.c = Math.tan(rad); + var s = this.multiply(m); + m = null; + return s; + }, + /*SVGMatrix*/ skewY : function(/*float*/ angle ){ + var m = new SVGMatrix(), rad = angle / 180 * Math.PI; + m.b = Math.tan(rad); + var s = this.multiply(m); + m = null; + return s; + }, + //行列式 + /*float*/ _determinant : function() { + return (this.a * this.d - this.b * this.c); + } +}; + +}; + function SVGTransform { // Transform Types const unsigned short SVG_TRANSFORM_UNKNOWN = 0; @@ -2493,87 +2576,87 @@ }; function SVGPathSegClosePath : SVGPathSeg {}; function SVGPathSegMovetoAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegMovetoRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegLinetoAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegLinetoRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegCurvetoCubicAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x1; + /*float*/ x1; // raises DOMException on setting - float y1; + /*float*/ y1; // raises DOMException on setting - float x2; + /*float*/ x2; // raises DOMException on setting - float y2; + /*float*/ y2; // raises DOMException on setting }; function SVGPathSegCurvetoCubicRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x1; + /*float*/ x1; // raises DOMException on setting - float y1; + /*float*/ y1; // raises DOMException on setting - float x2; + /*float*/ x2; // raises DOMException on setting - float y2; + /*float*/ y2; // raises DOMException on setting }; function SVGPathSegCurvetoQuadraticAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x1; + /*float*/ x1; // raises DOMException on setting - float y1; + /*float*/ y1; // raises DOMException on setting }; function SVGPathSegCurvetoQuadraticRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x1; + /*float*/ x1; // raises DOMException on setting - float y1; + /*float*/ y1; // raises DOMException on setting }; function SVGPathSegArcAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float r1; + /*float*/ r1; // raises DOMException on setting - float r2; + /*float*/ r2; // raises DOMException on setting - float angle; + /*float*/ angle; // raises DOMException on setting boolean largeArcFlag; // raises DOMException on setting @@ -2581,15 +2664,15 @@ // raises DOMException on setting }; function SVGPathSegArcRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float r1; + /*float*/ r1; // raises DOMException on setting - float r2; + /*float*/ r2; // raises DOMException on setting - float angle; + /*float*/ angle; // raises DOMException on setting boolean largeArcFlag; // raises DOMException on setting @@ -2597,51 +2680,51 @@ // raises DOMException on setting }; function SVGPathSegLinetoHorizontalAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting }; function SVGPathSegLinetoHorizontalRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting }; function SVGPathSegLinetoVerticalAbs : SVGPathSeg { - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegLinetoVerticalRel : SVGPathSeg { - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegCurvetoCubicSmoothAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x2; + /*float*/ x2; // raises DOMException on setting - float y2; + /*float*/ y2; // raises DOMException on setting }; function SVGPathSegCurvetoCubicSmoothRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float x2; + /*float*/ x2; // raises DOMException on setting - float y2; + /*float*/ y2; // raises DOMException on setting }; function SVGPathSegCurvetoQuadraticSmoothAbs : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegCurvetoQuadraticSmoothRel : SVGPathSeg { - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting }; function SVGPathSegList { @@ -2851,13 +2934,13 @@ // raises DOMException on setting DOMString format; // raises DOMException on setting - float x; + /*float*/ x; // raises DOMException on setting - float y; + /*float*/ y; // raises DOMException on setting - float dx; + /*float*/ dx; // raises DOMException on setting - float dy; + /*float*/ dy; // raises DOMException on setting }; function SVGPaint : SVGColor {