aboutsummaryrefslogtreecommitdiffstats
path: root/test/java/org/apache/fop/render/pdf/PDFGraphicsPainterTestCase.java
blob: 3130a73e7f88f8ea46330201ea31eab7350e805a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.pdf;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import static org.mockito.Matchers.endsWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.apache.fop.pdf.PDFNumber;

public class PDFGraphicsPainterTestCase {

    private PDFGraphicsPainter sut;

    private PDFContentGenerator generator;

    @Before
    public void setup() {
        generator = mock(PDFContentGenerator.class);
        sut = new PDFGraphicsPainter(generator);
    }

    @Test
    public void moveTo() {
        int x = 10;
        int y = 20;
        sut.moveTo(x, y);
        verify(generator).add(op("m", x, y));
    }

    @Test
    public void lineTo() {
        int x = 10;
        int y = 20;
        sut.lineTo(x, y);
        verify(generator).add(op("l", x, y));
    }

    @Test
    public void arcTo() throws IOException {
        int width = 10;
        int height = 10;
        int x = 0;
        int y = 0;
        double startAngle = 0;
        double endAngle = Math.PI / 2;
        sut.arcTo(startAngle, endAngle, x, y, width, height);
        //TODO stricter verification
        verify(generator).add(endsWith(" c "));
    }

    @Test
    public void closePath() {
        sut.closePath();
        verify(generator).add(op("h"));
    }

    @Test
    public void clip() {
        sut.clip();
        verify(generator).add(opln("W\nn"));
    }

    @Test
    public void saveGraphicsState() {
        sut.saveGraphicsState();
        verify(generator).add(opln("q"));
    }

    @Test
    public void restoreGraphicsState() {
        sut.restoreGraphicsState();
        verify(generator).add(opln("Q"));
    }

    @Test
    public void rotateCoordinates() throws IOException {
        double angle = 0;
        float s = (float) Math.sin(angle);
        float c = (float) Math.cos(angle);
        sut.rotateCoordinates(angle);
        testTransformCoordinatesF(c, s, -s, c, 0, 0);
    }

    @Test
    public void translateCoordinates() throws IOException {
        int x = 10;
        int y = 20;
        sut.translateCoordinates(x, y);
        testTransformCoordinates(1000, 0, 0, 1000, x, y);
    }

    @Test
    public void scaleCoordinates() throws IOException {
        float xScaleFactor = 10f;
        float yScaleFactor = 2f;
        sut.scaleCoordinates(xScaleFactor, yScaleFactor);
        testTransformCoordinatesF(xScaleFactor, 0f, 0f, yScaleFactor, 0f, 0f);
    }

    @Test
    public void cubicBezierTo() {
        int[] args = new int[]{1, 2, 3, 4, 5, 6};
        sut.cubicBezierTo(args[0], args[1], args[2], args[3], args[4], args[5]);
        verify(generator).add(op("c", args));
    }

    private void testTransformCoordinatesF(float... args) {
        verify(generator).add(opf("cm", args));
    }

    private void testTransformCoordinates(int... args) {
        verify(generator).add(op("cm", args));
    }

    private String opf(String op, float... args) {
        return opf(op, " ", args);
    }

    private String op(String op, int... args) {
        return op(op, " ", args);
    }

    private String opln(String op, int... args) {
        return op(op, "\n", args);
    }

    private String opf(String op, String ending, float... args) {
        StringBuilder sb = new StringBuilder();
        for (float arg : args) {
            sb.append("" +  PDFNumber.doubleOut(arg) + " ");
        }
        return sb.append(op.trim()).append(ending).toString();
    }

    private String op(String op, String ending, int... args) {
        float[] formattedArgs = new float[args.length];
        for (int i = 0; i < args.length; i++) {
            formattedArgs[i] = format(args[i]);
        }
        return opf(op, ending, formattedArgs);
    }

    private float format(int i) {
        return (float) i / 1000;
    }

}