aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/afp/AFPState.java
blob: 5977d0de259398d29cdb3d990e7347cb2f502c23 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * 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.afp;

import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * This keeps information about the current state when writing to pdf.
 */
public class AFPState {
    private Data data = new Data();
    
    private List stateStack = new java.util.ArrayList();

    /**
     * Set the current color.
     * Check if the new color is a change and then set the current color.
     *
     * @param col the color to set
     * @return true if the color has changed
     */
    protected boolean setColor(Color col) {
        if (!col.equals(getData().color)) {
            getData().color = col;
            return true;
        }
        return false;
    }

    /**
     * Sets if the current painted shape is to be filled
     * @param fill true if the current painted shape is to be filled
     * @return true if the fill value has changed
     */
    protected boolean setFill(boolean fill) {
        if (fill != getData().filled) {
            getData().filled = fill;
            return true;
        }
        return false;
    }

    /**
     * Get the color.
     * @return the color
     */
    protected Color getColor() {
        if (getData().color == null) {
            getData().color = Color.black;
        }
        return getData().color;
    }

    /**
     * Set the current line width.
     * @param width the line width in points
     * @return true if the line width has changed
     */
    protected boolean setLineWidth(float width) {
        if (getData().lineWidth != width) {
            getData().lineWidth = width;
            return true;
        }
        return false;
    }

    /**
     * Sets the dash array (line type) for the current basic stroke
     * @param dash the line dash array
     * @return true if the dash array has changed
     */
    public boolean setDashArray(float[] dash) {
        if (!Arrays.equals(dash, getData().dashArray)) {
            getData().dashArray = dash;
            return true;
        }
        return false;
    }

    /**
     * Gets the current line width
     * @return the current line width
     */
    protected float getLineWidth() {
        return getData().lineWidth;
    }

    /**
     * Get the background color.
     * @return the background color
     */
    protected Color getBackColor() {
        if (getData().backColor == null) {
            getData().backColor = Color.white;
        }
        return getData().backColor;
    }

    /**
     * Set the current background color.
     * Check if the new background color is a change and then set the current background color.
     *
     * @param col the background color to set
     * @return true if the color has changed
     */
    protected boolean setBackColor(Color col) {
        if (!col.equals(getData().backColor)) {
            getData().backColor = col;
            return true;
        }
        return false;
    }

    /**
     * Set the current font name
     * @param internalFontName the internal font name
     * @return true if the font name has changed
     */
    protected boolean setFontName(String internalFontName) {
        if (!internalFontName.equals(getData().fontName)) {
            getData().fontName = internalFontName;
            return true;
        }
        return false;
    }

    /**
     * Gets the current font name
     * @return the current font name
     */
    protected String getFontName() {
        return getData().fontName;
    }
    
    /**
     * Gets the current font size
     * @return the current font size
     */
    protected int getFontSize() {
        return getData().fontSize;
    }

    /**
     * Set the current font size.
     * Check if the font size is a change and then set the current font size.
     *
     * @param size the font size to set
     * @return true if the font size has changed
     */
    protected boolean setFontSize(int size) {
        if (size != getData().fontSize) {
            getData().fontSize = size;
            return true;
        }
        return false;
    }

    /**
     * Gets the current page fonts
     * @return the current page fonts
     */
    protected AFPPageFonts getPageFonts() {
        if (getData().pageFonts == null) {
            getData().pageFonts = new AFPPageFonts();
        }
        return getData().pageFonts;
    }

    /**
     * Push the current state onto the stack.
     * This call should be used when the q operator is used
     * so that the state is known when popped.
     */
    public void push() {
        Data copy;
        try {
            copy = (Data)getData().clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e.getMessage());
        }
        stateStack.add(copy);
    }

    /**
     * Get the current stack level.
     *
     * @return the current stack level
     */
    public int getStackLevel() {
        return stateStack.size();
    }

    /**
     * Pop the state from the stack and set current values to popped state.
     * This should be called when a Q operator is used so
     * the state is restored to the correct values.
     * @return the restored state, null if the stack is empty
     */
    public Data pop() {
        if (getStackLevel() > 0) {
            Data popped = (Data)stateStack.remove(stateStack.size() - 1);
            data = popped;
            return popped;
        } else {
            return null;
        }
    }

    /**
     * @return the currently valid state
     */
    public Data getData() {
        return data;
    }
    
    public class Data implements Cloneable, Serializable {
        private static final long serialVersionUID = -1789481244175275686L;

        /** The current color */
        private Color color = null;

        /** The current background color */
        private Color backColor = null;

        /** The current font name */
        private String fontName = null;

        /** The current font size */
        private int fontSize = 0;

        /** The current line width */
        private float lineWidth = 0;

        /** The dash array for the current basic stroke (line type) */
        private float[] dashArray = null;

        /** The current fill status */
        private boolean filled = false;
        
        /** The fonts on the current page */
        private AFPPageFonts pageFonts = null;

        /** {@inheritDoc} */
        public Object clone() throws CloneNotSupportedException {
            Data obj = new Data();
            obj.color = this.color;
            obj.backColor = this.backColor;
            obj.fontName = this.fontName;
            obj.fontSize = this.fontSize;
            obj.lineWidth = this.lineWidth;
            obj.dashArray = this.dashArray;
            obj.filled = this.filled;
            obj.pageFonts = this.pageFonts;
            return obj;
        }
    }
}