PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/batik-1.8/sources/org/apache/batik/bridge/SVGAnimateTransformElementBridge.java

#
Java | 286 lines | 226 code | 13 blank | 47 comment | 90 complexity | fd78fc19bd3dce9b7c189ecff386e6d9 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, IPL-1.0
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package org.apache.batik.bridge;
  16. import java.util.ArrayList;
  17. import org.apache.batik.anim.AbstractAnimation;
  18. import org.apache.batik.dom.anim.AnimationTarget;
  19. import org.apache.batik.anim.TransformAnimation;
  20. import org.apache.batik.anim.values.AnimatableValue;
  21. import org.apache.batik.anim.values.AnimatableTransformListValue;
  22. import org.apache.batik.dom.svg.SVGOMTransform;
  23. import org.apache.batik.util.SVGTypes;
  24. import org.w3c.dom.svg.SVGTransform;
  25. /**
  26. * Bridge class for the 'animateTransform' animation element.
  27. *
  28. * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
  29. * @version $Id: SVGAnimateTransformElementBridge.java 496029 2007-01-14 04:00:34Z cam $
  30. */
  31. public class SVGAnimateTransformElementBridge extends SVGAnimateElementBridge {
  32. /**
  33. * Returns 'animateTransform'.
  34. */
  35. public String getLocalName() {
  36. return SVG_ANIMATE_TRANSFORM_TAG;
  37. }
  38. /**
  39. * Returns a new instance of this bridge.
  40. */
  41. public Bridge getInstance() {
  42. return new SVGAnimateTransformElementBridge();
  43. }
  44. /**
  45. * Creates the animation object for the animation element.
  46. */
  47. protected AbstractAnimation createAnimation(AnimationTarget target) {
  48. short type = parseType();
  49. AnimatableValue from = null, to = null, by = null;
  50. if (element.hasAttributeNS(null, SVG_FROM_ATTRIBUTE)) {
  51. from = parseValue(element.getAttributeNS(null, SVG_FROM_ATTRIBUTE),
  52. type, target);
  53. }
  54. if (element.hasAttributeNS(null, SVG_TO_ATTRIBUTE)) {
  55. to = parseValue(element.getAttributeNS(null, SVG_TO_ATTRIBUTE),
  56. type, target);
  57. }
  58. if (element.hasAttributeNS(null, SVG_BY_ATTRIBUTE)) {
  59. by = parseValue(element.getAttributeNS(null, SVG_BY_ATTRIBUTE),
  60. type, target);
  61. }
  62. return new TransformAnimation(timedElement,
  63. this,
  64. parseCalcMode(),
  65. parseKeyTimes(),
  66. parseKeySplines(),
  67. parseAdditive(),
  68. parseAccumulate(),
  69. parseValues(type, target),
  70. from,
  71. to,
  72. by,
  73. type);
  74. }
  75. /**
  76. * Returns the parsed 'type' attribute from the animation element.
  77. */
  78. protected short parseType() {
  79. String typeString = element.getAttributeNS(null, SVG_TYPE_ATTRIBUTE);
  80. if (typeString.equals("translate")) {
  81. return SVGTransform.SVG_TRANSFORM_TRANSLATE;
  82. } else if (typeString.equals("scale")) {
  83. return SVGTransform.SVG_TRANSFORM_SCALE;
  84. } else if (typeString.equals("rotate")) {
  85. return SVGTransform.SVG_TRANSFORM_ROTATE;
  86. } else if (typeString.equals("skewX")) {
  87. return SVGTransform.SVG_TRANSFORM_SKEWX;
  88. } else if (typeString.equals("skewY")) {
  89. return SVGTransform.SVG_TRANSFORM_SKEWY;
  90. }
  91. throw new BridgeException
  92. (ctx, element, ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
  93. new Object[] { SVG_TYPE_ATTRIBUTE, typeString });
  94. }
  95. /**
  96. * Parses a transform value.
  97. */
  98. protected AnimatableValue parseValue(String s, short type,
  99. AnimationTarget target) {
  100. float val1, val2 = 0, val3 = 0;
  101. int i = 0;
  102. char c = ',';
  103. int len = s.length();
  104. while (i < len) {
  105. c = s.charAt(i);
  106. if (c == ' ' || c == ',') {
  107. break;
  108. }
  109. i++;
  110. }
  111. val1 = Float.parseFloat(s.substring(0, i));
  112. if (i < len) {
  113. i++;
  114. }
  115. int count = 1;
  116. if (i < len && c == ' ') {
  117. while (i < len) {
  118. c = s.charAt(i);
  119. if (c != ' ') {
  120. break;
  121. }
  122. i++;
  123. }
  124. if (c == ',') {
  125. i++;
  126. }
  127. }
  128. while (i < len && s.charAt(i) == ' ') {
  129. i++;
  130. }
  131. int j = i;
  132. if (i < len
  133. && type != SVGTransform.SVG_TRANSFORM_SKEWX
  134. && type != SVGTransform.SVG_TRANSFORM_SKEWY) {
  135. while (i < len) {
  136. c = s.charAt(i);
  137. if (c == ' ' || c == ',') {
  138. break;
  139. }
  140. i++;
  141. }
  142. val2 = Float.parseFloat(s.substring(j, i));
  143. if (i < len) {
  144. i++;
  145. }
  146. count++;
  147. if (i < len && c == ' ') {
  148. while (i < len) {
  149. c = s.charAt(i);
  150. if (c != ' ') {
  151. break;
  152. }
  153. i++;
  154. }
  155. if (c == ',') {
  156. i++;
  157. }
  158. }
  159. while (i < len && s.charAt(i) == ' ') {
  160. i++;
  161. }
  162. j = i;
  163. if (i < len && type == SVGTransform.SVG_TRANSFORM_ROTATE) {
  164. while (i < len) {
  165. c = s.charAt(i);
  166. if (c == ',' || c == ' ') {
  167. break;
  168. }
  169. i++;
  170. }
  171. val3 = Float.parseFloat(s.substring(j, i));
  172. if (i < len) {
  173. i++;
  174. }
  175. count++;
  176. while (i < len && s.charAt(i) == ' ') {
  177. i++;
  178. }
  179. }
  180. }
  181. if (i != len) {
  182. return null;
  183. }
  184. SVGOMTransform t = new SVGOMTransform();
  185. switch (type) {
  186. case SVGTransform.SVG_TRANSFORM_TRANSLATE:
  187. if (count == 2) {
  188. t.setTranslate(val1, val2);
  189. } else {
  190. t.setTranslate(val1, 0f);
  191. }
  192. break;
  193. case SVGTransform.SVG_TRANSFORM_SCALE:
  194. if (count == 2) {
  195. t.setScale(val1, val2);
  196. } else {
  197. t.setScale(val1, val1);
  198. }
  199. break;
  200. case SVGTransform.SVG_TRANSFORM_ROTATE:
  201. if (count == 3) {
  202. t.setRotate(val1, val2, val3);
  203. } else {
  204. t.setRotate(val1, 0f, 0f);
  205. }
  206. break;
  207. case SVGTransform.SVG_TRANSFORM_SKEWX:
  208. t.setSkewX(val1);
  209. break;
  210. case SVGTransform.SVG_TRANSFORM_SKEWY:
  211. t.setSkewY(val1);
  212. break;
  213. }
  214. return new AnimatableTransformListValue(target, t);
  215. }
  216. /**
  217. * Returns the parsed 'values' attribute from the animation element.
  218. */
  219. protected AnimatableValue[] parseValues(short type,
  220. AnimationTarget target) {
  221. String valuesString = element.getAttributeNS(null,
  222. SVG_VALUES_ATTRIBUTE);
  223. int len = valuesString.length();
  224. if (len == 0) {
  225. return null;
  226. }
  227. ArrayList values = new ArrayList(7);
  228. int i = 0, start = 0, end;
  229. char c;
  230. outer: while (i < len) {
  231. while (valuesString.charAt(i) == ' ') {
  232. i++;
  233. if (i == len) {
  234. break outer;
  235. }
  236. }
  237. start = i++;
  238. if (i < len) {
  239. c = valuesString.charAt(i);
  240. while (c != ';') {
  241. i++;
  242. if (i == len) {
  243. break;
  244. }
  245. c = valuesString.charAt(i);
  246. }
  247. }
  248. end = i++;
  249. String valueString = valuesString.substring(start, end);
  250. AnimatableValue value = parseValue(valueString, type, target);
  251. if (value == null) {
  252. throw new BridgeException
  253. (ctx, element, ErrorConstants.ERR_ATTRIBUTE_VALUE_MALFORMED,
  254. new Object[] { SVG_VALUES_ATTRIBUTE, valuesString });
  255. }
  256. values.add(value);
  257. }
  258. AnimatableValue[] ret = new AnimatableValue[values.size()];
  259. return (AnimatableValue[]) values.toArray(ret);
  260. }
  261. /**
  262. * Returns whether the animation element being handled by this bridge can
  263. * animate attributes of the specified type.
  264. * @param type one of the TYPE_ constants defined in {@link SVGTypes}.
  265. */
  266. protected boolean canAnimateType(int type) {
  267. return type == SVGTypes.TYPE_TRANSFORM_LIST;
  268. }
  269. }