View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.model.svg;
19  
20  import org.vectomatic.dom.svg.OMSVGElement;
21  import org.vectomatic.dom.svg.OMSVGMatrix;
22  import org.vectomatic.dom.svg.OMSVGRect;
23  import org.vectomatic.dom.svg.OMSVGSVGElement;
24  import org.vectomatic.dom.svg.OMSVGTransform;
25  import org.vectomatic.dom.svg.OMSVGTransformList;
26  import org.vectomatic.dom.svg.itf.ISVGTransformable;
27  
28  import com.google.gwt.core.client.GWT;
29  
30  /**
31   * Class to decompose the transform attribute into a matrix
32   * product of the form: t.r.s.tinv
33   * @author laaglu
34   */
35  public class Transformation {
36  	/**
37  	 * The transformable to which this transformation applies
38  	 */
39  	private ISVGTransformable transformable;
40  	/**
41  	 * The manipulator scale transform
42  	 */
43  	public OMSVGTransform getS() {
44  		return transformable.getTransform().getBaseVal().getItem(2);
45  	}
46  	/**
47  	 * The manipulator rotate transform
48  	 */
49  	public OMSVGTransform getR() {
50  		return transformable.getTransform().getBaseVal().getItem(1);
51  	}
52  	/**
53  	 * The manipulator translate transform
54  	 */
55  	public OMSVGTransform getT() {
56  		return transformable.getTransform().getBaseVal().getItem(0);
57  	}
58  	/**
59  	 * The manipulator translate inverse transform
60  	 */
61  	public OMSVGTransform getTinv() {
62  		return transformable.getTransform().getBaseVal().getItem(3);
63  	}
64  	
65  	public static Transformation decompose(ISVGTransformable transformable) {
66  		OMSVGTransformList transforms = transformable.getTransform().getBaseVal();
67  		OMSVGSVGElement svg = ((OMSVGElement)transformable).getOwnerSVGElement();
68  		OMSVGRect bbox = transformable.getBBox();
69  		OMSVGTransform transform = null;
70  		if (transforms.getNumberOfItems() == 0) {
71  			transform = transforms.appendItem(svg.createSVGTransform());
72  		} else {
73  			transform = transforms.consolidate();
74  		}
75  		OMSVGMatrix m = transform.getMatrix();
76  		float sx = (float)Math.sqrt(m.getA() * m.getA() + m.getB() * m.getB());
77  		float sy = (float)Math.sqrt(m.getC() * m.getC() + m.getD() * m.getD());
78  		float rrad = (float)(Math.acos(m.getA() / sx));
79  		if (m.getB() < 0f) {
80  			rrad = 2f * (float)Math.PI - rrad;
81  		}
82  		if (m.getA() * m.getD() < 0f) {
83  			sy = -sy;
84  		}
85  		float cos = (float)Math.cos(rrad);
86  		float sin = (float)Math.sin(rrad);
87  		float cx = bbox.getCenterX();
88  		float cy = bbox.getCenterY();
89  		GWT.log("t1x=" + cx + " t1y=" + cy);
90  		float tx = m.getE() + sx * cx * cos - sy * cy * sin;
91  		float ty = m.getF() + sx * cx * sin + sy * cy * cos;
92  
93  		Transformation decomposition = new Transformation(transformable);
94  		decomposition.getS().setScale(sx, sy);
95  		decomposition.getR().setRotate((float)(rrad * 180f / Math.PI), 0, 0);
96  		decomposition.getT().setTranslate(tx, ty);
97  		decomposition.getTinv().setTranslate(-cx, -cy);
98  		
99  		return decomposition;		
100 	}
101 
102 	private Transformation(ISVGTransformable transformable) {
103 		this.transformable = transformable;
104 		OMSVGTransformList xforms = transformable.getTransform().getBaseVal();
105 		xforms.clear();
106 		OMSVGSVGElement svg = ((OMSVGElement)transformable).getOwnerSVGElement();
107 		for (int i = 0; i < 4; i++) {
108 			xforms.appendItem(svg.createSVGTransform());
109 		}
110 	}
111 	@Override
112 	public String toString() {
113 		StringBuilder builder = new StringBuilder();
114 		builder.append("t=");
115 		builder.append(getT().getDescription());
116 		builder.append(";r=");
117 		builder.append(getR().getDescription());
118 		builder.append(";s=");
119 		builder.append(getS().getDescription());
120 		builder.append(";tinv=");
121 		builder.append(getTinv().getDescription());
122 		return builder.toString();
123 	}
124 }