1 18 package org.apache.batik.svggen; 19 20 import java.util.Stack ; 21 22 import org.apache.batik.ext.awt.g2d.GraphicContext; 23 import org.apache.batik.ext.awt.g2d.TransformStackElement; 24 import org.apache.batik.ext.awt.g2d.TransformType; 25 26 34 public class SVGTransform extends AbstractSVGConverter{ 35 38 private static double radiansToDegrees = 180.0 / Math.PI; 39 40 44 public SVGTransform(SVGGeneratorContext generatorContext) { 45 super(generatorContext); 46 } 47 48 58 public SVGDescriptor toSVG(GraphicContext gc){ 59 return new SVGTransformDescriptor(toSVGTransform(gc)); 60 } 61 62 68 public final String toSVGTransform(GraphicContext gc){ 69 return toSVGTransform(gc.getTransformStack()); 70 } 71 72 81 public final String toSVGTransform(TransformStackElement transformStack[]){ 82 StringBuffer transformStackBuffer = new StringBuffer (); 83 int nTransforms = transformStack.length; 84 Stack presentation = new Stack () { 88 91 public Object push(Object o) { 92 Object element; 93 if(((TransformStackElement)o).isIdentity()) { 94 element = pop(); 97 } else { 98 super.push(o); 101 element = null; 102 } 103 return element; 104 } 105 106 109 public Object pop() { 110 Object element = null; 111 if(!super.empty()) { 112 element = super.pop(); 113 } 114 return element; 115 } 116 }; 117 boolean canConcatenate = false; 118 int i = 0, j = 0, next = 0; 119 TransformStackElement element = null; 120 121 129 while(i < nTransforms) { 130 131 next = i; 134 if(element == null) { 135 element = (TransformStackElement) transformStack[i].clone(); 136 next++; 137 } 138 139 canConcatenate = true; 141 for(j = next; j < nTransforms; j++) { 142 canConcatenate = element.concatenate(transformStack[j]); 143 if(!canConcatenate) 144 break; 145 } 146 i = j; 156 157 element = (TransformStackElement) presentation.push(element); 162 } 163 164 if (element != null){ 166 presentation.push(element); 167 } 168 169 int nPresentations = presentation.size(); 173 174 for(i = 0; i < nPresentations; i++) { 175 transformStackBuffer.append(convertTransform((TransformStackElement) presentation.elementAt(i))); 176 transformStackBuffer.append(SPACE); 177 } 178 179 String transformValue = transformStackBuffer.toString().trim(); 180 return transformValue; 181 } 182 183 186 final String convertTransform(TransformStackElement transformElement){ 187 StringBuffer transformString = new StringBuffer (); 188 double transformParameters[] = transformElement.getTransformParameters(); 189 switch(transformElement.getType().toInt()){ 190 case TransformType.TRANSFORM_TRANSLATE: 191 if(!transformElement.isIdentity()) { 192 transformString.append(TRANSFORM_TRANSLATE); 193 transformString.append(OPEN_PARENTHESIS); 194 transformString.append(doubleString(transformParameters[0])); 195 transformString.append(COMMA); 196 transformString.append(doubleString(transformParameters[1])); 197 transformString.append(CLOSE_PARENTHESIS); 198 } 199 break; 200 case TransformType.TRANSFORM_ROTATE: 201 if(!transformElement.isIdentity()) { 202 transformString.append(TRANSFORM_ROTATE); 203 transformString.append(OPEN_PARENTHESIS); 204 transformString.append(doubleString(radiansToDegrees*transformParameters[0])); 205 transformString.append(CLOSE_PARENTHESIS); 206 } 207 break; 208 case TransformType.TRANSFORM_SCALE: 209 if(!transformElement.isIdentity()) { 210 transformString.append(TRANSFORM_SCALE); 211 transformString.append(OPEN_PARENTHESIS); 212 transformString.append(doubleString(transformParameters[0])); 213 transformString.append(COMMA); 214 transformString.append(doubleString(transformParameters[1])); 215 transformString.append(CLOSE_PARENTHESIS); 216 } 217 break; 218 case TransformType.TRANSFORM_SHEAR: 219 if(!transformElement.isIdentity()) { 220 transformString.append(TRANSFORM_MATRIX); 221 transformString.append(OPEN_PARENTHESIS); 222 transformString.append(1); 223 transformString.append(COMMA); 224 transformString.append(doubleString(transformParameters[1])); 225 transformString.append(COMMA); 226 transformString.append(doubleString(transformParameters[0])); 227 transformString.append(COMMA); 228 transformString.append(1); 229 transformString.append(COMMA); 230 transformString.append(0); 231 transformString.append(COMMA); 232 transformString.append(0); 233 transformString.append(CLOSE_PARENTHESIS); 234 } 235 break; 236 case TransformType.TRANSFORM_GENERAL: 237 if(!transformElement.isIdentity()) { 238 transformString.append(TRANSFORM_MATRIX); 239 transformString.append(OPEN_PARENTHESIS); 240 transformString.append(doubleString(transformParameters[0])); 241 transformString.append(COMMA); 242 transformString.append(doubleString(transformParameters[1])); 243 transformString.append(COMMA); 244 transformString.append(doubleString(transformParameters[2])); 245 transformString.append(COMMA); 246 transformString.append(doubleString(transformParameters[3])); 247 transformString.append(COMMA); 248 transformString.append(doubleString(transformParameters[4])); 249 transformString.append(COMMA); 250 transformString.append(doubleString(transformParameters[5])); 251 transformString.append(CLOSE_PARENTHESIS); 252 } 253 break; 254 default: 255 throw new Error (); 258 } 259 260 return transformString.toString(); 261 } 262 } 263 | Popular Tags |