KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbstractPageObject.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21
22 import java.awt.Color JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.fop.render.afp.fonts.AFPFont;
28 import org.apache.fop.render.afp.tools.StringUtils;
29
30 /**
31  * Pages contain the data objects that comprise a presentation document. Each
32  * page has a set of data objects associated with it. Each page within a
33  * document is independent from any other page, and each must establish its own
34  * environment parameters.
35  *
36  * The page is the level in the document component hierarchy that is used for
37  * printing or displaying a document's content. The data objects contained in
38  * the page envelope in the data stream are presented when the page is
39  * presented. Each data object has layout information associated with it that
40  * directs the placement and orientation of the data on the page. In addition,
41  * each page contains layout information that specifies the measurement units,
42  * page width, and page depth.
43  *
44  * A page is initiated by a begin page structured field and terminated by an end
45  * page structured field. Structured fields that define objects and active
46  * environment groups or that specify attributes of the page may be encountered
47  * in page state.
48  *
49  */

50 public abstract class AbstractPageObject extends AbstractNamedAFPObject {
51
52     /**
53      * The active environment group for the page
54      */

55     protected ActiveEnvironmentGroup _activeEnvironmentGroup = null;
56
57     /**
58      * The presentation text object, we only have one per page
59      */

60     private PresentationTextObject _presentationTextObject = null;
61
62     /**
63      * The list of objects within the page
64      */

65     protected List JavaDoc _objects = new ArrayList JavaDoc();
66
67     /**
68      * The list of tag logical elements
69      */

70     protected ArrayList JavaDoc _tagLogicalElements = new ArrayList JavaDoc();
71
72     /**
73      * The list of the include page segments
74      */

75     protected ArrayList JavaDoc _segments = new ArrayList JavaDoc();
76
77     /**
78      * The page width
79      */

80     private int _width;
81
82     /**
83      * The page height
84      */

85     private int _height;
86
87     /**
88      * The page rotation
89      */

90     private int _rotation = 0;
91
92     /**
93      * The page state
94      */

95     private boolean _complete = false;
96
97     /**
98      * Construct a new page object for the specified name argument, the page
99      * name should be an 8 character identifier.
100      *
101      * @param name
102      * the name of the page.
103      * @param width
104      * the width of the page.
105      * @param height
106      * the height of the page.
107      * @param rotation
108      * the rotation of the page.
109      */

110     public AbstractPageObject(String JavaDoc name, int width, int height, int rotation) {
111
112         super(name);
113
114         _name = name;
115
116         _rotation = rotation;
117         _width = width;
118         _height = height;
119
120         /**
121          * Every page object must have an ActiveEnvironmentGroup
122          */

123         _activeEnvironmentGroup = new ActiveEnvironmentGroup(_width, _height);
124
125         if (_rotation != 0) {
126             switch (_rotation) {
127                 case 90:
128                     _activeEnvironmentGroup.setPosition(_width, 0, _rotation);
129                     break;
130                 case 180:
131                     _activeEnvironmentGroup.setPosition(_width, _height, _rotation);
132                     break;
133                 case 270:
134                     _activeEnvironmentGroup.setPosition(0, _height, _rotation);
135                     break;
136             }
137         }
138
139         /**
140          * We have a presentation text object per page
141          */

142         _presentationTextObject = new PresentationTextObject();
143         _objects.add(_presentationTextObject);
144
145     }
146
147     /**
148      * Helper method to create a map coded font object on the current page, this
149      * method delegates the construction of the map coded font object to the
150      * active environment group on the page.
151      *
152      * @param fontReference
153      * the font number used as the resource identifier
154      * @param font
155      * the font
156      * @param size
157      * the point size of the font
158      */

159     public void createFont(byte fontReference, AFPFont font, int size) {
160
161         _activeEnvironmentGroup.createFont(fontReference, font, size, 0);
162
163     }
164
165     /**
166      * Helper method to create a line on the current page, this method delegates
167      * to the presentation text object in order to construct the line.
168      *
169      * @param x1
170      * the first x coordinate of the line
171      * @param y1
172      * the first y coordinate of the line
173      * @param x2
174      * the second x coordinate of the line
175      * @param y2
176      * the second y coordinate of the line
177      * @param thickness
178      * the thickness of the line
179      * @param rotation
180      * the rotation of the line
181      * @param col
182      * The text color.
183      */

184     public void createLine(int x1, int y1, int x2, int y2, int thickness, int rotation, Color JavaDoc col) {
185
186         if (_presentationTextObject == null) {
187             _presentationTextObject = new PresentationTextObject();
188             _objects.add(_presentationTextObject);
189         }
190         _presentationTextObject.createLineData(x1, y1, x2, y2, thickness, rotation, col);
191
192     }
193
194     /**
195      * Helper method to create text on the current page, this method delegates
196      * to the presentation text object in order to construct the text.
197      *
198      * @param fontNumber
199      * the font number used as the resource identifier
200      * @param x
201      * the x coordinate of the text data
202      * @param y
203      * the y coordinate of the text data
204      * @param rotation
205      * the rotation of the text data
206      * @param col
207      * the text color
208      * @param vsci
209      * The variable space character increment.
210      * @param ica
211      * The inter character adjustment.
212      * @param data
213      * the text data to create
214      */

215     public void createText(int fontNumber, int x, int y, int rotation, Color JavaDoc col, int vsci, int ica, byte[] data) {
216
217         if (_presentationTextObject == null) {
218             _presentationTextObject = new PresentationTextObject();
219             _objects.add(_presentationTextObject);
220         }
221         _presentationTextObject.createTextData(fontNumber, x, y, rotation, col, vsci, ica, data);
222
223     }
224
225     /**
226      * Helper method to mark the end of the page. This should end the control
227      * sequence on the current presenation text object.
228      */

229     public void endPage() {
230
231         if (_presentationTextObject != null) {
232             _presentationTextObject.endControlSequence();
233         }
234
235         _complete = true;
236
237     }
238
239     /**
240      * This method will create shading on the page using the specified
241      * coordinates (the shading contrast is controlled via the red, green blue
242      * parameters, by converting this to grey scale).
243      *
244      * @param x
245      * the x coordinate of the shading
246      * @param y
247      * the y coordinate of the shading
248      * @param w
249      * the width of the shaded area
250      * @param h
251      * the height of the shaded area
252      * @param red
253      * the red value
254      * @param green
255      * the green value
256      * @param blue
257      * the blue value
258      */

259     public void createShading(int x, int y, int w, int h, int red, int green,
260         int blue) {
261
262         int xCoord = 0;
263         int yCoord = 0;
264         int width = 0;
265         int height = 0;
266
267         switch (_rotation) {
268             case 90:
269                 xCoord = _width - y - h;
270                 yCoord = x;
271                 width = h;
272                 height = w;
273                 break;
274             case 180:
275                 xCoord = _width - x - w;
276                 yCoord = _height - y - h;
277                 width = w;
278                 height = h;
279                 break;
280             case 270:
281                 xCoord = y;
282                 yCoord = _height - x - w;
283                 width = h;
284                 height = w;
285                 break;
286             default:
287                 xCoord = x;
288                 yCoord = y;
289                 width = w;
290                 height = h;
291                 break;
292         }
293
294         // Convert the color to grey scale
295
float shade = (float) ((red * 0.3) + (green * 0.59) + (blue * 0.11));
296
297         int greyscale = Math.round((shade / 255) * 16);
298
299         String JavaDoc imageName = "IMG"
300             + StringUtils.lpad(String.valueOf(_objects.size() + 1),
301             '0', 5);
302
303         IMImageObject io = new IMImageObject(imageName);
304         ImageOutputControl ioc = new ImageOutputControl(0, 0);
305         ImageInputDescriptor iid = new ImageInputDescriptor();
306         ImageCellPosition icp = new ImageCellPosition(xCoord, yCoord);
307         icp.setXFillSize(width);
308         icp.setYFillSize(height);
309         icp.setXSize(64);
310         icp.setYSize(8);
311
312         //defing this as a resource
313
ImageRasterData ird = new ImageRasterData(ImageRasterPattern
314             .getRasterData(greyscale));
315
316         io.setImageOutputControl(ioc);
317         io.setImageInputDescriptor(iid);
318         io.setImageCellPosition(icp);
319         io.setImageRasterData(ird);
320         _objects.add(io);
321
322     }
323
324     /**
325      * Helper method to create an image on the current page and to return
326      * the object.
327      */

328     public ImageObject getImageObject() {
329
330         if (_presentationTextObject != null) {
331             _presentationTextObject.endControlSequence();
332         }
333         _presentationTextObject = null;
334
335         String JavaDoc imageName = "IMG"
336             + StringUtils.lpad(String.valueOf(_objects.size() + 1),
337             '0', 5);
338
339         ImageObject io = new ImageObject(imageName);
340         _objects.add(io);
341         return io;
342     }
343
344     /**
345      * Creates a TagLogicalElement on the page.
346      *
347      * @param name
348      * the name of the tag
349      * @param value
350      * the value of the tag
351      */

352     public void createTagLogicalElement(String JavaDoc name, String JavaDoc value) {
353
354         TagLogicalElement tle = new TagLogicalElement(name, value);
355         _tagLogicalElements.add(tle);
356
357     }
358
359     /**
360      * Creates an IncludePageSegment on the current page.
361      *
362      * @param name
363      * the name of the page segment
364      * @param xCoor
365      * the x cooridinate of the page segment.
366      * @param yCoor
367      * the y cooridinate of the page segment.
368      */

369     public void createIncludePageSegment(String JavaDoc name, int xCoor, int yCoor) {
370
371         IncludePageSegment ips = new IncludePageSegment(name, xCoor, yCoor);
372         _segments.add(ips);
373
374     }
375
376     /**
377      * Returns the ActiveEnvironmentGroup associated with this page.
378      *
379      * @return the ActiveEnvironmentGroup object
380      */

381     public ActiveEnvironmentGroup getActiveEnvironmentGroup() {
382         return _activeEnvironmentGroup;
383     }
384
385     /**
386      * Returns an indication if the page is complete
387      */

388     public boolean isComplete() {
389         return _complete;
390     }
391
392     /**
393      * Returns the height of the page
394      */

395     public int getHeight() {
396         return _height;
397     }
398
399     /**
400      * Returns the width of the page
401      */

402     public int getWidth() {
403         return _width;
404     }
405
406     /**
407      * Returns the rotation of the page
408      */

409     public int getRotation() {
410         return _rotation;
411     }
412
413 }
Popular Tags