You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AffineTransformArrayParser.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.intermediate;
  19. import java.awt.geom.AffineTransform;
  20. import java.io.Reader;
  21. import java.util.List;
  22. import org.apache.batik.parser.ParseException;
  23. import org.apache.batik.parser.TransformListHandler;
  24. import org.apache.batik.parser.TransformListParser;
  25. /**
  26. * This class parses a sequence of transformations into an array of {@link AffineTransform}
  27. * instances.
  28. */
  29. public class AffineTransformArrayParser implements TransformListHandler {
  30. private static final AffineTransform[] EMPTY_ARRAY = new AffineTransform[0];
  31. private List transforms;
  32. /**
  33. * Utility method for creating an AffineTransform array.
  34. * @param r The reader used to read the transform specification.
  35. * @return the AffineTransform array
  36. * @throws ParseException if there's a parse error
  37. */
  38. public static AffineTransform[] createAffineTransform(Reader r)
  39. throws ParseException {
  40. TransformListParser p = new TransformListParser();
  41. AffineTransformArrayParser th = new AffineTransformArrayParser();
  42. p.setTransformListHandler(th);
  43. p.parse(r);
  44. return th.getAffineTransforms();
  45. }
  46. /**
  47. * Utility method for creating an AffineTransform.
  48. * @param s The transform specification.
  49. * @return the AffineTransform array
  50. * @throws ParseException if there's a parse error
  51. */
  52. public static AffineTransform[] createAffineTransform(String s)
  53. throws ParseException {
  54. if (s == null) {
  55. return EMPTY_ARRAY;
  56. }
  57. TransformListParser p = new TransformListParser();
  58. AffineTransformArrayParser th = new AffineTransformArrayParser();
  59. p.setTransformListHandler(th);
  60. p.parse(s);
  61. return th.getAffineTransforms();
  62. }
  63. /**
  64. * Returns the AffineTransform array initialized during the last parsing.
  65. * @return the array or null if this handler has not been used by
  66. * a parser.
  67. */
  68. public AffineTransform[] getAffineTransforms() {
  69. if (this.transforms == null) {
  70. return null;
  71. } else {
  72. int count = this.transforms.size();
  73. return (AffineTransform[])this.transforms.toArray(new AffineTransform[count]);
  74. }
  75. }
  76. /** {@inheritDoc} */
  77. public void startTransformList() throws ParseException {
  78. this.transforms = new java.util.ArrayList();
  79. }
  80. /** {@inheritDoc} */
  81. public void matrix(float a, float b, float c, float d, float e, float f)
  82. throws ParseException {
  83. this.transforms.add(new AffineTransform(a, b, c, d, e, f));
  84. }
  85. /** {@inheritDoc} */
  86. public void rotate(float theta) throws ParseException {
  87. this.transforms.add(AffineTransform.getRotateInstance(Math.toRadians(theta)));
  88. }
  89. /** {@inheritDoc} */
  90. public void rotate(float theta, float cx, float cy) throws ParseException {
  91. AffineTransform at
  92. = AffineTransform.getRotateInstance(Math.toRadians(theta), cx, cy);
  93. this.transforms.add(at);
  94. }
  95. /** {@inheritDoc} */
  96. public void translate(float tx) throws ParseException {
  97. AffineTransform at = AffineTransform.getTranslateInstance(tx, 0);
  98. this.transforms.add(at);
  99. }
  100. /** {@inheritDoc} */
  101. public void translate(float tx, float ty) throws ParseException {
  102. AffineTransform at = AffineTransform.getTranslateInstance(tx, ty);
  103. this.transforms.add(at);
  104. }
  105. /** {@inheritDoc} */
  106. public void scale(float sx) throws ParseException {
  107. this.transforms.add(AffineTransform.getScaleInstance(sx, sx));
  108. }
  109. /** {@inheritDoc} */
  110. public void scale(float sx, float sy) throws ParseException {
  111. this.transforms.add(AffineTransform.getScaleInstance(sx, sy));
  112. }
  113. /** {@inheritDoc} */
  114. public void skewX(float skx) throws ParseException {
  115. this.transforms.add(
  116. AffineTransform.getShearInstance(Math.tan(Math.toRadians(skx)), 0));
  117. }
  118. /** {@inheritDoc} */
  119. public void skewY(float sky) throws ParseException {
  120. this.transforms.add(
  121. AffineTransform.getShearInstance(0, Math.tan(Math.toRadians(sky))));
  122. }
  123. /** {@inheritDoc} */
  124. public void endTransformList() throws ParseException {
  125. }
  126. }