1 18 package org.apache.batik.dom.svg; 19 20 import java.awt.geom.AffineTransform ; 21 22 import org.apache.batik.parser.ParseException; 23 import org.apache.batik.parser.TransformListHandler; 24 import org.apache.batik.parser.TransformListParser; 25 import org.w3c.dom.DOMException ; 26 import org.w3c.dom.svg.SVGException; 27 import org.w3c.dom.svg.SVGMatrix; 28 import org.w3c.dom.svg.SVGTransform; 29 import org.w3c.dom.svg.SVGTransformList; 30 31 38 public abstract class AbstractSVGTransformList 39 extends AbstractSVGList 40 implements SVGTransformList { 41 42 45 public final static String SVG_TRANSFORMATION_LIST_SEPARATOR 46 = ""; 47 48 51 protected AbstractSVGTransformList() { 52 super(); 53 } 54 55 58 protected String getItemSeparator(){ 59 return SVG_TRANSFORMATION_LIST_SEPARATOR; 60 } 61 62 67 protected abstract SVGException createSVGException(short type, 68 String key, 69 Object [] args); 70 71 73 public SVGTransform initialize ( SVGTransform newItem ) 74 throws DOMException , SVGException { 75 76 return (SVGTransform)initializeImpl(newItem); 77 } 78 80 public SVGTransform getItem ( int index ) 81 throws DOMException { 82 83 return (SVGTransform)getItemImpl(index); 84 } 85 86 88 public SVGTransform insertItemBefore ( SVGTransform newItem, int index ) 89 throws DOMException , SVGException { 90 91 return (SVGTransform)insertItemBeforeImpl(newItem,index); 92 } 93 94 96 public SVGTransform replaceItem ( SVGTransform newItem, int index ) 97 throws DOMException , SVGException { 98 99 return (SVGTransform)replaceItemImpl(newItem,index); 100 } 101 102 104 public SVGTransform removeItem ( int index ) 105 throws DOMException { 106 107 return (SVGTransform)removeItemImpl(index); 108 } 109 110 112 public SVGTransform appendItem ( SVGTransform newItem ) 113 throws DOMException , SVGException { 114 115 return (SVGTransform) appendItemImpl(newItem); 116 } 117 118 120 public SVGTransform createSVGTransformFromMatrix ( SVGMatrix matrix ){ 121 SVGOMTransform transform = new SVGOMTransform(); 122 transform.setMatrix(matrix); 123 return transform; 124 } 125 126 128 public SVGTransform consolidate ( ){ 129 revalidate(); 130 131 if ( itemList.size() == 0 ){ 132 return null; 133 } 134 if ( itemList.size() == 1 ){ 135 return getItem(0); 136 } 137 138 SVGTransform view = (SVGTransform)getItemImpl(0); 139 140 AffineTransform transform = (AffineTransform ) 141 ((SVGTransformItem)view).affineTransform.clone(); 142 143 for(int i = 1 ; i < itemList.size() ; i++ ){ 144 view = (SVGTransform)getItemImpl(i); 145 transform.concatenate(((SVGTransformItem)view).affineTransform); 146 } 147 SVGOMMatrix matrix = new SVGOMMatrix(transform); 148 return initialize(createSVGTransformFromMatrix(matrix)); 149 } 150 151 153 protected SVGItem createSVGItem(Object newItem){ 154 155 SVGTransform transform= (SVGTransform)newItem; 156 157 return new SVGTransformItem(transform); 158 } 159 160 166 protected void doParse(String value, ListHandler handler) 167 throws ParseException{ 168 169 TransformListParser transformListParser = new TransformListParser(); 170 171 TransformListBuilder builder = new TransformListBuilder(handler); 172 173 transformListParser.setTransformListHandler(builder); 174 transformListParser.parse(value); 175 176 } 177 178 181 protected void checkItemType(Object newItem){ 182 if ( !( newItem instanceof SVGTransform ) ){ 183 createSVGException(SVGException.SVG_WRONG_TYPE_ERR, 184 "expected SVGTransform", 185 null); 186 } 187 } 188 189 192 protected class SVGTransformItem extends AbstractSVGTransform 193 implements SVGItem { 194 195 protected boolean xOnly; 196 protected boolean angleOnly; 197 198 201 protected AbstractSVGList parent; 202 203 209 protected String itemStringValue; 210 211 protected SVGTransformItem(){ 212 } 213 214 221 protected void resetAttribute(){ 222 if ( parent != null ){ 223 itemStringValue = null; 224 parent.itemChanged(); 225 } 226 } 227 228 233 public void setParent(AbstractSVGList list){ 234 parent = list; 235 } 236 237 242 public AbstractSVGList getParent(){ 243 return parent; 244 } 245 246 252 public String getValueAsString(){ 253 if ( itemStringValue == null ){ 254 itemStringValue = getStringValue(); 255 } 256 return itemStringValue; 257 } 258 259 protected SVGTransformItem(SVGTransform transform){ 260 super(); 261 type = transform.getType(); 262 SVGMatrix matrix = transform.getMatrix(); 263 switch(type){ 264 case SVGTransform.SVG_TRANSFORM_TRANSLATE: 265 setTranslate(matrix.getE(),matrix.getF()); 266 break; 267 case SVGTransform.SVG_TRANSFORM_SCALE: 268 setScale(matrix.getA(),matrix.getD()); 269 break; 270 case SVGTransform.SVG_TRANSFORM_ROTATE: 271 if (matrix.getE() == 0.0f ){ 272 rotate(transform.getAngle()); 273 } 274 else{ 275 angleOnly = false; 276 if ( matrix.getA() == 1.0f ){ 277 setRotate(transform.getAngle(),matrix.getE(),matrix.getF()); 278 } 279 else{ 280 if ( transform instanceof AbstractSVGTransform){ 281 AbstractSVGTransform internal = (AbstractSVGTransform)transform; 282 setRotate(internal.getAngle(),internal.getX(),internal.getY()); 283 } 284 } 285 } 286 break; 287 case SVGTransform.SVG_TRANSFORM_SKEWX: 288 setSkewX(transform.getAngle()); 289 break; 290 case SVGTransform.SVG_TRANSFORM_SKEWY: 291 setSkewY(transform.getAngle()); 292 break; 293 case SVGTransform.SVG_TRANSFORM_MATRIX: 294 setMatrix(matrix); 295 break; 296 } 297 298 } 299 300 protected void translate(float x){ 301 xOnly = true; 302 setTranslate(x,0.0f); 303 } 304 protected void rotate(float angle){ 305 angleOnly = true; 306 setRotate(angle,0.0f,0.0f); 307 } 308 protected void scale(float x){ 309 xOnly = true; 310 setScale(x,x); 311 } 312 protected void matrix(float a,float b,float c, 313 float d,float e,float f){ 314 setMatrix(new SVGOMMatrix(new AffineTransform (a,b,c,d,e,f))); 315 } 316 317 318 public void setMatrix ( SVGMatrix matrix ){ 319 super.setMatrix(matrix); 320 resetAttribute(); 321 } 322 public void setTranslate ( float tx, float ty ){ 323 super.setTranslate(tx,ty); 324 resetAttribute(); 325 } 326 public void setScale ( float sx, float sy ){ 327 super.setScale(sx,sy); 328 resetAttribute(); 329 } 330 public void setRotate ( float angle, float cx, float cy ){ 331 super.setRotate(angle,cx,cy); 332 resetAttribute(); 333 } 334 public void setSkewX ( float angle ){ 335 super.setSkewX(angle); 336 resetAttribute(); 337 } 338 public void setSkewY ( float angle ){ 339 super.setSkewY(angle); 340 resetAttribute(); 341 } 342 343 protected SVGMatrix createMatrix(){ 344 return new AbstractSVGMatrix(){ 345 protected AffineTransform getAffineTransform(){ 346 return SVGTransformItem.this.affineTransform; 347 } 348 349 public void setA(float a) throws DOMException { 350 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 351 super.setA(a); 352 SVGTransformItem.this.resetAttribute(); 353 } 354 public void setB(float b) throws DOMException { 355 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 356 super.setB(b); 357 SVGTransformItem.this.resetAttribute(); 358 } 359 public void setC(float c) throws DOMException { 360 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 361 super.setC(c); 362 SVGTransformItem.this.resetAttribute(); 363 } 364 public void setD(float d) throws DOMException { 365 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 366 super.setD(d); 367 SVGTransformItem.this.resetAttribute(); 368 } 369 public void setE(float e) throws DOMException { 370 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 371 super.setE(e); 372 SVGTransformItem.this.resetAttribute(); 373 } 374 public void setF(float f) throws DOMException { 375 SVGTransformItem.this.type = SVGTransform.SVG_TRANSFORM_MATRIX; 376 super.setF(f); 377 SVGTransformItem.this.resetAttribute(); 378 } 379 }; 380 } 381 382 protected String getStringValue(){ 383 StringBuffer buf = new StringBuffer (); 384 switch(type){ 385 case SVGTransform.SVG_TRANSFORM_TRANSLATE: 386 buf.append("translate("); 387 buf.append(affineTransform.getTranslateX()); 388 if( !xOnly ){ 389 buf.append(' '); 390 buf.append(affineTransform.getTranslateY()); 391 } 392 buf.append(')'); 393 break; 394 case SVGTransform.SVG_TRANSFORM_ROTATE: 395 buf.append("rotate("); 396 buf.append(angle); 397 if ( !angleOnly ){ 398 buf.append(' '); 399 buf.append(x); 400 buf.append(' '); 401 buf.append(y); 402 } 403 buf.append(')'); 404 break; 405 case SVGTransform.SVG_TRANSFORM_SCALE: 406 buf.append("scale("); 407 buf.append(affineTransform.getScaleX()); 408 if ( !xOnly ){ 409 buf.append(' '); 410 buf.append(affineTransform.getScaleY()); 411 } 412 buf.append(')'); 413 break; 414 case SVGTransform.SVG_TRANSFORM_SKEWX: 415 buf.append("skewX("); 416 buf.append(angle); 417 buf.append(')'); 418 break; 419 case SVGTransform.SVG_TRANSFORM_SKEWY: 420 buf.append("skewY("); 421 buf.append(angle); 422 buf.append(')'); 423 break; 424 case SVGTransform.SVG_TRANSFORM_MATRIX: 425 buf.append("matrix("); 426 double[] matrix = new double[6]; 427 affineTransform.getMatrix(matrix); 428 for(int i = 0 ; i < 6 ; i++ ){ 429 if ( i != 0 ){ 430 buf.append(' '); 431 } 432 buf.append((float)matrix[i]); 433 } 434 buf.append(')'); 435 break; 436 } 437 return buf.toString(); 438 } 439 } 440 441 445 protected class TransformListBuilder 446 implements TransformListHandler { 447 448 451 protected ListHandler listHandler; 452 453 public TransformListBuilder(ListHandler listHandler){ 454 this.listHandler = listHandler; 455 } 456 457 public void startTransformList() throws ParseException{ 458 listHandler.startList(); 459 } 460 461 467 public void matrix(float a, float b, float c, 468 float d, float e, float f) 469 throws ParseException{ 470 SVGTransformItem item = new SVGTransformItem(); 471 item.matrix(a,b,c,d,e,f); 472 listHandler.item(item); 473 } 474 475 481 public void rotate(float theta) throws ParseException{ 482 SVGTransformItem item = new SVGTransformItem(); 483 item.rotate(theta); 484 listHandler.item(item); 485 } 486 487 493 public void rotate(float theta, float cx, float cy) throws ParseException{ 494 SVGTransformItem item = new SVGTransformItem(); 495 item.setRotate(theta,cx,cy); 496 listHandler.item(item); 497 } 498 499 505 public void translate(float tx) throws ParseException{ 506 SVGTransformItem item = new SVGTransformItem(); 507 item.translate(tx); 508 listHandler.item(item); 509 510 } 511 512 518 public void translate(float tx, float ty) throws ParseException{ 519 SVGTransformItem item = new SVGTransformItem(); 520 item.setTranslate(tx,ty); 521 listHandler.item(item); 522 } 523 524 530 public void scale(float sx) throws ParseException{ 531 SVGTransformItem item = new SVGTransformItem(); 532 item.scale(sx); 533 listHandler.item(item); 534 535 } 536 537 543 public void scale(float sx, float sy) throws ParseException{ 544 SVGTransformItem item = new SVGTransformItem(); 545 item.setScale(sx,sy); 546 listHandler.item(item); 547 } 548 549 555 public void skewX(float skx) throws ParseException{ 556 SVGTransformItem item = new SVGTransformItem(); 557 item.setSkewX(skx); 558 listHandler.item(item); 559 } 560 561 567 public void skewY(float sky) throws ParseException{ 568 SVGTransformItem item = new SVGTransformItem(); 569 item.setSkewY(sky); 570 listHandler.item(item); 571 } 572 573 579 public void endTransformList() throws ParseException{ 580 listHandler.endList(); 581 } 582 } 583 584 } 585 | Popular Tags |