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