aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/modca/GraphicsObject.java
blob: 710e7364bcaaecd09344d4c2108e1a7039c452fe (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * 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.modca;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.fop.afp.AFPDataObjectInfo;
import org.apache.fop.afp.AFPObjectAreaInfo;
import org.apache.fop.afp.Completable;
import org.apache.fop.afp.Factory;
import org.apache.fop.afp.StructuredData;
import org.apache.fop.afp.goca.GraphicsAreaBegin;
import org.apache.fop.afp.goca.GraphicsAreaEnd;
import org.apache.fop.afp.goca.GraphicsBox;
import org.apache.fop.afp.goca.GraphicsChainedSegment;
import org.apache.fop.afp.goca.GraphicsCharacterString;
import org.apache.fop.afp.goca.GraphicsData;
import org.apache.fop.afp.goca.GraphicsFillet;
import org.apache.fop.afp.goca.GraphicsFullArc;
import org.apache.fop.afp.goca.GraphicsImage;
import org.apache.fop.afp.goca.GraphicsLine;
import org.apache.fop.afp.goca.GraphicsSetArcParameters;
import org.apache.fop.afp.goca.GraphicsSetCharacterSet;
import org.apache.fop.afp.goca.GraphicsSetCurrentPosition;
import org.apache.fop.afp.goca.GraphicsSetLineType;
import org.apache.fop.afp.goca.GraphicsSetLineWidth;
import org.apache.fop.afp.goca.GraphicsSetPatternSymbol;
import org.apache.fop.afp.goca.GraphicsSetProcessColor;

/**
 * Top-level GOCA graphics object.
 *
 * Acts as container and factory of all other graphic objects
 */
public class GraphicsObject extends AbstractDataObject {

    /** The graphics data */
    private GraphicsData currentData = null;

    /** list of objects contained within this container */
    protected List/*<GraphicsDrawingOrder>*/ objects
        = new java.util.ArrayList/*<GraphicsDrawingOrder>*/();

    /** the current color */
    private Color currentColor;

    /** the current line type */
    private byte currentLineType;

    /** the current line width */
    private int currentLineWidth;

    /** the current fill pattern */
    private byte currentPatternSymbol;

    /** the current character set */
    private int currentCharacterSet;

    /**
     * Default constructor
     *
     * @param factory the object factory
     * @param name the name of graphics object
     */
    public GraphicsObject(Factory factory, String name) {
        super(factory, name);
    }

    /** {@inheritDoc} */
    public void setViewport(AFPDataObjectInfo dataObjectInfo) {
        super.setViewport(dataObjectInfo);

        AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
        int width = objectAreaInfo.getWidth();
        int height = objectAreaInfo.getHeight();
        int widthRes = objectAreaInfo.getWidthRes();
        int heightRes = objectAreaInfo.getHeightRes();
        final int leftEdge = 0;
        final int topEdge = 0;
        GraphicsDataDescriptor graphicsDataDescriptor = factory.createGraphicsDataDescriptor(
                    leftEdge, width, topEdge, height, widthRes, heightRes);

        getObjectEnvironmentGroup().setDataDescriptor(graphicsDataDescriptor);
    }

    /** {@inheritDoc} */
    public void addObject(StructuredData object) {
        if (currentData == null) {
            newData();
        } else if (currentData.getDataLength() + object.getDataLength()
                >= GraphicsData.MAX_DATA_LEN) {
            // graphics data full so transfer current incomplete segment to new data
            GraphicsChainedSegment currentSegment
                = (GraphicsChainedSegment)currentData.removeCurrentSegment();
            currentSegment.setName(newData().createSegmentName());
            currentData.addSegment(currentSegment);
        }
        currentData.addObject(object);
    }

    /**
     * Gets the current graphics data, creating a new one if necessary
     *
     * @return the current graphics data
     */
    private GraphicsData getData() {
        if (this.currentData == null) {
            return newData();
        }
        return this.currentData;
    }

    /**
     * Creates a new graphics data
     *
     * @return a newly created graphics data
     */
    private GraphicsData newData() {
        if (currentData != null) {
            currentData.setComplete(true);
        }
        this.currentData = factory.createGraphicsData();
        objects.add(currentData);
        return currentData;
    }

    /**
     * Sets the current color
     *
     * @param color the active color to use
     */
    public void setColor(Color color) {
        if (!color.equals(currentColor)) {
            this.currentColor = color;
            addObject(new GraphicsSetProcessColor(color));
        }
    }

    /**
     * Sets the current position
     *
     * @param coords the x and y coordinates of the current position
     */
    public void setCurrentPosition(int[] coords) {
        addObject(new GraphicsSetCurrentPosition(coords));
    }

    /**
     * Sets the line width
     *
     * @param lineWidth the line width multiplier
     */
    public void setLineWidth(int lineWidth) {
        if (lineWidth != currentLineWidth) {
            currentLineWidth = lineWidth;
            addObject(new GraphicsSetLineWidth(lineWidth));
        }
    }

    /**
     * Sets the line type
     *
     * @param lineType the line type
     */
    public void setLineType(byte lineType) {
        if (lineType != currentLineType) {
            currentLineType = lineType;
            addObject(new GraphicsSetLineType(lineType));
        }
    }

    /**
     * Sets whether the following shape is to be filled
     *
     * @param fill true if the following shape is to be filled
     */
    public void setFill(boolean fill) {
        setPatternSymbol(fill ?
                GraphicsSetPatternSymbol.SOLID_FILL :
                    GraphicsSetPatternSymbol.NO_FILL);
    }

    /**
     * Sets the fill pattern of the next shape
     *
     * @param the fill pattern of the next shape
     */
    public void setPatternSymbol(byte patternSymbol) {
        if (currentPatternSymbol != patternSymbol) {
            currentPatternSymbol = patternSymbol;
            addObject(new GraphicsSetPatternSymbol(patternSymbol));
        }
    }

    /**
     * Sets the character set to use
     *
     * @param characterSet the character set (font) reference
     */
    public void setCharacterSet(int characterSet) {
        if (currentCharacterSet != characterSet) {
            currentCharacterSet = characterSet;
            addObject(new GraphicsSetCharacterSet(characterSet));
        }
    }

    /**
     * Adds a line at the given x/y coordinates
     *
     * @param coords the x/y coordinates (can be a series)
     */
    public void addLine(int[] coords) {
        addLine(coords, false);
    }

    /**
     * Adds a line at the given x/y coordinates
     *
     * @param coords the x/y coordinates (can be a series)
     * @param relative relative true for a line at current position (relative to)
     */
    public void addLine(int[] coords, boolean relative) {
        addObject(new GraphicsLine(coords, relative));
    }

    /**
     * Adds a box at the given coordinates
     *
     * @param coords the x/y coordinates
     */
    public void addBox(int[] coords) {
        addObject(new GraphicsBox(coords));
    }

    /**
     * Adds a fillet (curve) at the given coordinates
     *
     * @param coords the x/y coordinates
     */
    public void addFillet(int[] coords) {
        addFillet(coords, false);
    }

    /**
     * Adds a fillet (curve) at the given coordinates
     *
     * @param coords the x/y coordinates
     * @param relative relative true for a fillet (curve) at current position (relative to)
     */
    public void addFillet(int[] coords, boolean relative) {
        addObject(new GraphicsFillet(coords, relative));
    }

    /**
     * Sets the arc parameters
     *
     * @param xmaj the maximum value of the x coordinate
     * @param ymin the minimum value of the y coordinate
     * @param xmin the minimum value of the x coordinate
     * @param ymaj the maximum value of the y coordinate
     */
    public void setArcParams(int xmaj, int ymin, int xmin, int ymaj) {
        addObject(new GraphicsSetArcParameters(xmaj, ymin, xmin, ymaj));
    }

    /**
     * Adds a full arc
     *
     * @param x the x coordinate
     * @param y the y coordinate
     * @param mh the integer portion of the multiplier
     * @param mhr the fractional portion of the multiplier
     */
    public void addFullArc(int x, int y, int mh, int mhr) {
        addObject(new GraphicsFullArc(x, y, mh, mhr));
    }

    /**
     * Adds an image
     *
     * @param x the x coordinate
     * @param y the y coordinate
     * @param width the image width
     * @param height the image height
     * @param imgData the image data
     */
    public void addImage(int x, int y, int width, int height, byte[] imgData) {
        addObject(new GraphicsImage(x, y, width, height, imgData));
    }

    /**
     * Adds a string
     *
     * @param str the string
     * @param x the x coordinate
     * @param y the y coordinate
     */
    public void addString(String str, int x, int y) {
        addObject(new GraphicsCharacterString(str, x, y));
    }

    /**
     * Begins a graphics area (start of fill)
     */
    public void beginArea() {
        addObject(new GraphicsAreaBegin());
    }

    /**
     * Ends a graphics area (end of fill)
     */
    public void endArea() {
        addObject(new GraphicsAreaEnd());
    }

    /** {@inheritDoc} */
    public String toString() {
        return "GraphicsObject: " + getName();
    }

    /**
     * Creates a new graphics segment
     */
    public void newSegment() {
        getData().newSegment();
    }

    /** {@inheritDoc} */
    public void setComplete(boolean complete) {
        Iterator it = objects.iterator();
        while (it.hasNext()) {
            Completable completedObject = (Completable)it.next();
            completedObject.setComplete(true);
        }
        super.setComplete(complete);
    }

    /** {@inheritDoc} */
    protected void writeStart(OutputStream os) throws IOException {
        super.writeStart(os);
        byte[] data = new byte[17];
        copySF(data, Type.BEGIN, Category.GRAPHICS);
        os.write(data);
    }

    /** {@inheritDoc} */
    protected void writeContent(OutputStream os) throws IOException {
        super.writeContent(os);
        writeObjects(objects, os);
    }

    /** {@inheritDoc} */
    protected void writeEnd(OutputStream os) throws IOException {
        byte[] data = new byte[17];
        copySF(data, Type.END, Category.GRAPHICS);
        os.write(data);
    }

}