aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/AFPBorderPainter.java
blob: 4c56c0def3d190f52d90d1dde102d0ae5f4ccc18 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * 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.afp;

import java.awt.geom.AffineTransform;

import org.apache.fop.fo.Constants;
import org.apache.fop.util.ColorUtil;

/**
 * Handles the drawing of borders/lines in AFP
 */
public class AFPBorderPainter extends AbstractAFPPainter {

    /**
     * Main constructor
     *
     * @param paintingState the AFP painting state converter
     * @param dataStream the AFP datastream
     */
    public AFPBorderPainter(AFPPaintingState paintingState, DataStream dataStream) {
        super(paintingState, dataStream);
    }

    /** {@inheritDoc} */
    public void paint(PaintingInfo paintInfo) {
        BorderPaintingInfo borderPaintInfo = (BorderPaintingInfo)paintInfo;
        float w = borderPaintInfo.getX2() - borderPaintInfo.getX1();
        float h = borderPaintInfo.getY2() - borderPaintInfo.getY1();
        if ((w < 0) || (h < 0)) {
            log.error("Negative extent received. Border won't be painted.");
            return;
        }

        int pageWidth = dataStream.getCurrentPage().getWidth();
        int pageHeight = dataStream.getCurrentPage().getHeight();
        AFPUnitConverter unitConv = paintingState.getUnitConverter();
        AffineTransform at = paintingState.getData().getTransform();

        float x1 = unitConv.pt2units(borderPaintInfo.getX1());
        float y1 = unitConv.pt2units(borderPaintInfo.getY1());
        float x2 = unitConv.pt2units(borderPaintInfo.getX2());
        float y2 = unitConv.pt2units(borderPaintInfo.getY2());

        switch (paintingState.getRotation()) {
        case 0:
            x1 += at.getTranslateX();
            y1 += at.getTranslateY();
            x2 += at.getTranslateX();
            y2 += at.getTranslateY();
            break;
        case 90:
            x1 += at.getTranslateY();
            y1 += (float) (pageWidth - at.getTranslateX());
            x2 += at.getTranslateY();
            y2 += (float) (pageWidth - at.getTranslateX());
            break;
        case 180:
            x1 += (float) (pageWidth - at.getTranslateX());
            y1 += (float) (pageHeight - at.getTranslateY());
            x2 += (float) (pageWidth - at.getTranslateX());
            y2 += (float) (pageHeight - at.getTranslateY());
            break;
        case 270:
            x1 = (float) (pageHeight - at.getTranslateY());
            y1 += (float) at.getTranslateX();
            x2 += x1;
            y2 += (float) at.getTranslateX();
            break;
        }

        AFPLineDataInfo lineDataInfo = new AFPLineDataInfo();
        lineDataInfo.setColor(borderPaintInfo.getColor());
        lineDataInfo.setRotation(paintingState.getRotation());
        lineDataInfo.x1 = Math.round(x1);
        lineDataInfo.y1 = Math.round(y1);
        if (borderPaintInfo.isHorizontal()) {
            lineDataInfo.setThickness(Math.round(y2 - y1));
        } else {
            lineDataInfo.setThickness(Math.round(x2 - x1));
        }

        // handle border-*-style
        switch (borderPaintInfo.getStyle()) {
        case Constants.EN_DOUBLE:
            if (borderPaintInfo.isHorizontal()) {
                lineDataInfo.x2 = Math.round(x2);
                lineDataInfo.y2 = lineDataInfo.y1;
                dataStream.createLine(lineDataInfo);
                lineDataInfo.y1 += Math.round((lineDataInfo.thickness / 3) * 2);
                dataStream.createLine(lineDataInfo);
            } else {
                lineDataInfo.x2 = lineDataInfo.x1;
                lineDataInfo.y2 = Math.round(y2);
                dataStream.createLine(lineDataInfo);
                lineDataInfo.x1 += Math.round((lineDataInfo.thickness / 3) * 2);
                dataStream.createLine(lineDataInfo);
            }
            break;
        case Constants.EN_DASHED:
            int thick = lineDataInfo.thickness * 3;
            if (borderPaintInfo.isHorizontal()) {
                lineDataInfo.x2 = lineDataInfo.x1 + thick;
                lineDataInfo.y2 = lineDataInfo.y1;
                int ex2 = Math.round(x2);
                while (lineDataInfo.x1 + thick < ex2) {
                    dataStream.createLine(lineDataInfo);
                    lineDataInfo.x1 += 2 * thick;
                    lineDataInfo.x2 = lineDataInfo.x1 + thick;
                }
            } else {
                lineDataInfo.x2 = lineDataInfo.x1;
                lineDataInfo.y2 = lineDataInfo.y1 + thick;
                int ey2 = Math.round(y2);
                while (lineDataInfo.y1 + thick < ey2) {
                    dataStream.createLine(lineDataInfo);
                    lineDataInfo.y1 += 2 * thick;
                    lineDataInfo.y2 = lineDataInfo.y1 + thick;
                }
            }
            break;
        case Constants.EN_DOTTED:
            if (borderPaintInfo.isHorizontal()) {
                lineDataInfo.x2 = lineDataInfo.x1 + lineDataInfo.thickness;
                lineDataInfo.y2 = lineDataInfo.y1;
                int ex2 = Math.round(x2);
                while (lineDataInfo.x1 + lineDataInfo.thickness < ex2) {
                    dataStream.createLine(lineDataInfo);
                    lineDataInfo.x1 += 3 * lineDataInfo.thickness;
                    lineDataInfo.x2 = lineDataInfo.x1 + lineDataInfo.thickness;
                }
            } else {
                lineDataInfo.x2 = lineDataInfo.x1;
                lineDataInfo.y2 = lineDataInfo.y1 + lineDataInfo.thickness;
                int ey2 = Math.round(y2);
                while (lineDataInfo.y1 + lineDataInfo.thickness < ey2) {
                    dataStream.createLine(lineDataInfo);
                    lineDataInfo.y1 += 3 * lineDataInfo.thickness;
                    lineDataInfo.y2 = lineDataInfo.y1 + lineDataInfo.thickness;
                }
            }
            break;
        case Constants.EN_GROOVE:
        case Constants.EN_RIDGE:
            //TODO
            lineDataInfo.x2 = Math.round(x2);
            float colFactor = (borderPaintInfo.getStyle() == Constants.EN_GROOVE ? 0.4f : -0.4f);
            float h3 = (y2 - y1) / 3;
            lineDataInfo.color = ColorUtil.lightenColor(borderPaintInfo.getColor(), -colFactor);
            lineDataInfo.thickness = Math.round(h3);
            lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1);
            dataStream.createLine(lineDataInfo);
            lineDataInfo.color = borderPaintInfo.getColor();
            lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1 + h3);
            dataStream.createLine(lineDataInfo);
            lineDataInfo.color = ColorUtil.lightenColor(borderPaintInfo.getColor(), colFactor);
            lineDataInfo.y1 = lineDataInfo.y2 = Math.round(y1 + h3 + h3);
            dataStream.createLine(lineDataInfo);
            break;
        case Constants.EN_HIDDEN:
            break;
        case Constants.EN_INSET:
        case Constants.EN_OUTSET:
        case Constants.EN_SOLID:
        default:
            if (borderPaintInfo.isHorizontal()) {
                lineDataInfo.x2 = Math.round(x2);
                lineDataInfo.y2 = lineDataInfo.y1;
            } else {
                lineDataInfo.x2 = lineDataInfo.x1;
                lineDataInfo.y2 = Math.round(y2);
            }
            dataStream.createLine(lineDataInfo);
        }
    }

}