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.

ArcToBezierCurveTransformerTestCase.java 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. package org.apache.fop.render.intermediate;
  18. import java.io.IOException;
  19. import org.junit.Test;
  20. import org.mockito.ArgumentCaptor;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.fail;
  23. import static org.mockito.Mockito.mock;
  24. import static org.mockito.Mockito.verify;
  25. public class ArcToBezierCurveTransformerTestCase {
  26. @Test
  27. public void arcTo() throws Exception {
  28. testArcTo(Math.PI / 3, Math.PI / 2, 100, 200, 1000, 1000);
  29. }
  30. private void testArcTo(double startAngle, double endAngle, int xCenter, int yCenter, int width,
  31. int height) throws IOException {
  32. assertAngleWithinFirstQuadrant(startAngle);
  33. assertAngleWithinFirstQuadrant(endAngle);
  34. BezierCurvePainter bezierCurvePainter = mock(BezierCurvePainter.class);
  35. ArcToBezierCurveTransformer sut = new ArcToBezierCurveTransformer(bezierCurvePainter);
  36. sut.arcTo(startAngle, endAngle, xCenter, yCenter, width, height);
  37. double tan1 = Math.tan(startAngle);
  38. double tan2 = Math.tan(endAngle);
  39. double lambda1 = Math.atan(height * tan1 / width);
  40. double lambda2 = Math.atan(height * tan2 / width);
  41. double xStart = width * Math.cos(lambda1) + xCenter;
  42. double yStart = height * Math.sin(lambda1) + yCenter;
  43. double xEnd = width * Math.cos(lambda2) + xCenter;
  44. double yEnd = height * Math.sin(lambda2) + yCenter;
  45. ArgumentCaptor<Integer> xP1Captor = ArgumentCaptor.forClass(Integer.class);
  46. ArgumentCaptor<Integer> yP1Captor = ArgumentCaptor.forClass(Integer.class);
  47. ArgumentCaptor<Integer> xP2Captor = ArgumentCaptor.forClass(Integer.class);
  48. ArgumentCaptor<Integer> yP2Captor = ArgumentCaptor.forClass(Integer.class);
  49. ArgumentCaptor<Integer> xP3Captor = ArgumentCaptor.forClass(Integer.class);
  50. ArgumentCaptor<Integer> yP3Captor = ArgumentCaptor.forClass(Integer.class);
  51. verify(bezierCurvePainter).cubicBezierTo(xP1Captor.capture(), yP1Captor.capture(),
  52. xP2Captor.capture(), yP2Captor.capture(), xP3Captor.capture(), yP3Captor.capture());
  53. int xP1 = xP1Captor.getValue();
  54. int yP1 = yP1Captor.getValue();
  55. int xP2 = xP2Captor.getValue();
  56. int yP2 = yP2Captor.getValue();
  57. int xP3 = xP3Captor.getValue();
  58. int yP3 = yP3Captor.getValue();
  59. // TODO do more than check the direction of the tangents at the end
  60. // points
  61. assertEquals((yP1 - yStart) / (xP1 - xStart), -width * width / height / height / tan1, 0.01);
  62. assertEquals((yP2 - yEnd) / (xP2 - xEnd), -width * width / height / height / tan2, 0.01);
  63. assertEquals((int) xEnd, xP3);
  64. assertEquals((int) yEnd, yP3);
  65. }
  66. private void assertAngleWithinFirstQuadrant(double angle) {
  67. if (angle <= 0 || angle > Math.PI / 2) {
  68. fail("Angle " + angle + " is in (0, " + Math.PI / 2 + ")");
  69. }
  70. }
  71. }