KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > modca > ActiveEnvironmentGroup


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 /* $Id: ActiveEnvironmentGroup.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import org.apache.fop.render.afp.fonts.AFPFont;
26
27 /**
28  * An Active Environment Group (AEG) is associated with each page,
29  * and is contained in the page's begin-end envelope in the data stream.
30  * The active environment group contains layout and formatting information
31  * that defines the measurement units and size of the page, and may contain
32  * resource information.
33  *
34  * Any objects that are required for page presentation and that are to be
35  * treated as resource objects must be mapped with a map structured field
36  * in the AEG. The scope of an active environment group is the scope of its
37  * containing page or overlay.
38  *
39  */

40 public final class ActiveEnvironmentGroup extends AbstractNamedAFPObject {
41
42     /**
43      * Default name for the active environment group
44      */

45     private static final String JavaDoc DEFAULT_NAME = "AEG00001";
46
47     /**
48      * The collection of MapCodedFont objects
49      */

50     private ArrayList JavaDoc _mapCodedFonts = new ArrayList JavaDoc();
51
52     /**
53      * The Object Area Descriptor for the active environment group
54      */

55     private ObjectAreaDescriptor _objectAreaDescriptor = null;
56
57     /**
58      * The Object Area Position for the active environment group
59      */

60     private ObjectAreaPosition _objectAreaPosition = null;
61
62     /**
63      * The PresentationTextDescriptor for the active environment group
64      */

65     private PresentationTextDescriptor _presentationTextDataDescriptor = null;
66
67     /**
68      * The PageDescriptor for the active environment group
69      */

70     private PageDescriptor _pageDescriptor = null;
71
72     /**
73      * The collection of MapPageOverlay objects
74      */

75     private ArrayList JavaDoc _mapPageOverlays = new ArrayList JavaDoc();
76
77     /**
78      * Default constructor for the ActiveEnvironmentGroup.
79      * @param width the page width
80      * @param height the page height
81      */

82     public ActiveEnvironmentGroup(int width, int height) {
83
84         this(DEFAULT_NAME, width, height);
85
86     }
87
88     /**
89      * Constructor for the ActiveEnvironmentGroup, this takes a
90      * name parameter which must be 8 characters long.
91      * @param name the active environment group name
92      * @param width the page width
93      * @param height the page height
94      */

95     public ActiveEnvironmentGroup(String JavaDoc name, int width, int height) {
96
97         super(name);
98
99         // Create PageDescriptor
100
_pageDescriptor = new PageDescriptor(width, height);
101
102         // Create ObjectAreaDescriptor
103
_objectAreaDescriptor = new ObjectAreaDescriptor(width, height);
104
105         // Create PresentationTextDataDescriptor
106
_presentationTextDataDescriptor =
107             new PresentationTextDescriptor(width, height);
108
109     }
110
111     /**
112      * Set the position of the object area
113      * @param x the x offset
114      * @param y the y offset
115      * @param rotation the rotation
116      */

117     public void setPosition(int x, int y, int rotation) {
118
119         // Create ObjectAreaPosition
120
_objectAreaPosition = new ObjectAreaPosition(x, y, rotation);
121
122     }
123
124     /**
125      * Accessor method to obtain the PageDescriptor object of the
126      * active environment group.
127      * @return the page descriptor object
128      */

129     public PageDescriptor getPageDescriptor() {
130
131         return _pageDescriptor;
132
133     }
134
135     /**
136      * Accessor method to obtain the PresentationTextDataDescriptor object of
137      * the active environment group.
138      * @return the presentation text descriptor
139      */

140     public PresentationTextDescriptor getPresentationTextDataDescriptor() {
141
142         return _presentationTextDataDescriptor;
143
144     }
145
146     /**
147      * Accessor method to write the AFP datastream for the active environment group.
148      * @param os The stream to write to
149      * @throws java.io.IOException
150      */

151     public void writeDataStream(OutputStream JavaDoc os)
152         throws IOException JavaDoc {
153
154         writeStart(os);
155
156         writeObjectList(_mapCodedFonts, os);
157
158         writeObjectList(_mapPageOverlays, os);
159
160         _pageDescriptor.writeDataStream(os);
161
162         if (_objectAreaDescriptor != null && _objectAreaPosition != null) {
163             _objectAreaDescriptor.writeDataStream(os);
164             _objectAreaPosition.writeDataStream(os);
165         }
166
167         _presentationTextDataDescriptor.writeDataStream(os);
168
169         writeEnd(os);
170
171     }
172
173     /**
174      * Helper method to write the start of the active environment group.
175      * @param os The stream to write to
176      */

177     private void writeStart(OutputStream JavaDoc os)
178         throws IOException JavaDoc {
179
180         byte[] data = new byte[17];
181
182         data[0] = 0x5A; // Structured field identifier
183
data[1] = 0x00; // Length byte 1
184
data[2] = 0x10; // Length byte 2
185
data[3] = (byte) 0xD3; // Structured field id byte 1
186
data[4] = (byte) 0xA8; // Structured field id byte 2
187
data[5] = (byte) 0xC9; // Structured field id byte 3
188
data[6] = 0x00; // Flags
189
data[7] = 0x00; // Reserved
190
data[8] = 0x00; // Reserved
191

192         for (int i = 0; i < _nameBytes.length; i++) {
193
194             data[9 + i] = _nameBytes[i];
195
196         }
197
198        os.write(data);
199
200     }
201
202     /**
203      * Helper method to write the end of the active environment group.
204      * @param os The stream to write to
205      */

206     private void writeEnd(OutputStream JavaDoc os)
207         throws IOException JavaDoc {
208
209         byte[] data = new byte[17];
210
211         data[0] = 0x5A; // Structured field identifier
212
data[1] = 0x00; // Length byte 1
213
data[2] = 0x10; // Length byte 2
214
data[3] = (byte) 0xD3; // Structured field id byte 1
215
data[4] = (byte) 0xA9; // Structured field id byte 2
216
data[5] = (byte) 0xC9; // Structured field id byte 3
217
data[6] = 0x00; // Flags
218
data[7] = 0x00; // Reserved
219
data[8] = 0x00; // Reserved
220

221         for (int i = 0; i < _nameBytes.length; i++) {
222
223             data[9 + i] = _nameBytes[i];
224
225         }
226
227         os.write(data);
228
229     }
230
231     /**
232      * Method to create a map coded font object
233      * @param fontReference the font number used as the resource identifier
234      * @param font the font
235      * @param size the point size of the font
236      * @param orientation the orientation of the font (e.g. 0, 90, 180, 270)
237      */

238     public void createFont(
239         byte fontReference,
240         AFPFont font,
241         int size,
242         int orientation) {
243
244         MapCodedFont mcf = getCurrentMapCodedFont();
245
246         if (mcf == null) {
247             mcf = new MapCodedFont();
248             _mapCodedFonts.add(mcf);
249         }
250
251         try {
252
253             mcf.addFont(
254                 fontReference,
255                 font,
256                 size,
257                 orientation);
258
259         } catch (MaximumSizeExceededException msee) {
260
261             mcf = new MapCodedFont();
262             _mapCodedFonts.add(mcf);
263
264             try {
265
266                 mcf.addFont(
267                     fontReference,
268                     font,
269                     size,
270                     orientation);
271
272             } catch (MaximumSizeExceededException ex) {
273
274                 // Should never happen (but log just in case)
275
log.error("createFont():: resulted in a MaximumSizeExceededException");
276
277             }
278
279         }
280
281     }
282
283     /**
284      * Actually creates the MPO object.
285      * Also creates the supporting object (an IPO)
286      * @param name the name of the overlay to be used
287      */

288     public void createOverlay(String JavaDoc name) {
289
290         MapPageOverlay mpo = getCurrentMapPageOverlay();
291
292         if (mpo == null) {
293             mpo = new MapPageOverlay();
294             _mapPageOverlays.add(mpo);
295         }
296
297         try {
298
299             mpo.addOverlay(name);
300
301         } catch (MaximumSizeExceededException msee) {
302             mpo = new MapPageOverlay();
303             _mapPageOverlays.add(mpo);
304             try {
305                 mpo.addOverlay(name);
306             } catch (MaximumSizeExceededException ex) {
307                 // Should never happen (but log just in case)
308
log.error("createOverlay():: resulted in a MaximumSizeExceededException");
309             }
310         }
311     }
312
313     /**
314      * Getter method for the most recent MapCodedFont added to the
315      * Active Environment Group (returns null if no MapCodedFonts exist)
316      * @return the most recent Map Coded Font.
317      */

318     private MapCodedFont getCurrentMapCodedFont() {
319
320         int size = _mapCodedFonts.size();
321         if (size > 0) {
322             return (MapCodedFont) _mapCodedFonts.get(_mapCodedFonts.size() - 1);
323         } else {
324             return null;
325         }
326
327     }
328
329     /**
330      * Getter method for the most recent MapPageOverlay added to the
331      * Active Environment Group (returns null if no MapPageOverlay exist)
332      * @return the most recent Map Coded Font
333      */

334     private MapPageOverlay getCurrentMapPageOverlay() {
335
336         int size = _mapPageOverlays.size();
337         if (size > 0) {
338             return (MapPageOverlay) _mapPageOverlays.get(
339                 _mapPageOverlays.size() - 1);
340         } else {
341             return null;
342         }
343
344     }
345
346 }
Popular Tags