KickJava   Java API By Example, From Geeks To Geeks.

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


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: PresentationTextObject.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.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * The Presentation Text object is the data object used in document processing
30  * environments for representing text which has been prepared for presentation.
31  * Text, as used here, means an ordered string of characters, such as graphic
32  * symbols, numbers, and letters, that are suitable for the specific purpose of
33  * representing coherent information. Text which has been prepared for
34  * presentation has been reduced to a primitive form through explicit
35  * specification of the characters and their placement in the presentation
36  * space. Control sequences which designate specific control functions may be
37  * embedded within the text. These functions extend the primitive form by
38  * applying specific characteristics to the text when it is presented. The
39  * collection of the graphic characters and control codes is called Presentation
40  * Text, and the object that contains the Presentation Text is called the
41  * PresentationText object.
42  *
43  */

44 public class PresentationTextObject extends AbstractNamedAFPObject {
45
46     /**
47      * Default name for the presentation text object
48      */

49     private static final String JavaDoc DEFAULT_NAME = "PTO00001";
50
51     private PresentationTextData currentPresentationTextData = null;
52
53     private ArrayList JavaDoc presentationTextData = new ArrayList JavaDoc();
54
55     /**
56      * Default constructor for the PresentationTextObject
57      */

58     public PresentationTextObject() {
59
60         this(DEFAULT_NAME);
61
62     }
63
64     /**
65      * Construct a new PresentationTextObject for the specified name argument,
66      * the name should be an 8 character identifier.
67      */

68     public PresentationTextObject(String JavaDoc name) {
69
70         super(name);
71
72     }
73
74     /**
75      * Create the presentation text data for the byte array of data.
76      *
77      * @param fontNumber
78      * The font resource identifier.
79      * @param x
80      * The x coordinate for the text data.
81      * @param y
82      * The y coordinate for the text data.
83      * @param col
84      * The text color.
85      * @param vsci
86      * The variable space character increment.
87      * @param ica
88      * The inter character increment.
89      * @param data
90      * The text data to be created.
91      */

92     public void createTextData(int fontNumber, int x, int y, Color JavaDoc col, int vsci, int ica, byte[] data) {
93
94         // Use a default orientation of zero
95
createTextData(fontNumber, x, y, 0, col, vsci, ica, data);
96
97     }
98
99     /**
100      * Create the presentation text data for the byte array of data.
101      *
102      * @param fontNumber
103      * The font resource identifier.
104      * @param x
105      * The x coordinate for the text data.
106      * @param y
107      * The y coordinate for the text data.
108      * @param orientation
109      * The orientation of the text data.
110      * @param col
111      * The text color.
112      * @param vsci
113      * The variable space character increment.
114      * @param ica
115      * The inter character adjustment.
116      * @param data
117      * The text data to be created.
118      */

119     public void createTextData(int fontNumber, int x, int y, int orientation,
120         Color JavaDoc col, int vsci, int ica, byte[] data) {
121
122         if (currentPresentationTextData == null) {
123             startPresentationTextData();
124         }
125
126         try {
127
128             currentPresentationTextData.createTextData(fontNumber, x, y,
129                 orientation, col, vsci, ica, data);
130
131         } catch (MaximumSizeExceededException msee) {
132
133             endPresentationTextData();
134             createTextData(fontNumber, x, y, orientation, col, vsci, ica, data);
135
136         }
137
138     }
139
140     /**
141      * Drawing of lines using the starting and ending coordinates, thickness.
142      *
143      * @param x1
144      * The first x coordinate of the line.
145      * @param y1
146      * The first y coordinate of the line.
147      * @param x2
148      * The second x coordinate of the line.
149      * @param y2
150      * The second y coordinate of the line.
151      * @param thickness
152      * The thickness of the line.
153      * @param col
154      * The text color.
155      */

156     public void createLineData(int x1, int y1, int x2, int y2, int thickness, Color JavaDoc col) {
157         // Default orientation
158
createLineData(x1, y1, x2, y2, thickness, 0, col);
159     }
160
161     /**
162      * Drawing of lines using the starting and ending coordinates, thickness and
163      * orientation arguments.
164      *
165      * @param x1
166      * The first x coordinate of the line.
167      * @param y1
168      * The first y coordinate of the line.
169      * @param x2
170      * The second x coordinate of the line.
171      * @param y2
172      * The second y coordinate of the line.
173      * @param thickness
174      * The thickness of the line.
175      * @param orientation
176      * The orientation of the line.
177      * @param col
178      * The text color.
179      */

180     public void createLineData(int x1, int y1, int x2, int y2, int thickness,
181         int orientation, Color JavaDoc col) {
182
183         if (currentPresentationTextData == null) {
184             startPresentationTextData();
185         }
186
187         try {
188
189             currentPresentationTextData.createLineData(x1, y1, x2, y2,
190                 thickness, orientation, col);
191
192         } catch (MaximumSizeExceededException msee) {
193
194             endPresentationTextData();
195             createLineData(x1, y1, x2, y2, thickness, orientation, col);
196
197         }
198
199     }
200
201     /**
202      * Helper method to mark the start of the presentation text data
203      */

204     private void startPresentationTextData() {
205
206         if (presentationTextData.size() == 0) {
207             currentPresentationTextData = new PresentationTextData(true);
208         } else {
209             currentPresentationTextData = new PresentationTextData();
210         }
211
212         presentationTextData.add(currentPresentationTextData);
213
214     }
215
216     /**
217      * Helper method to mark the end of the presentation text data
218      */

219     private void endPresentationTextData() {
220
221         currentPresentationTextData = null;
222
223     }
224
225     /**
226      * Accessor method to write the AFP datastream for the PresentationTextObject.
227      * @param os The stream to write to
228      * @throws java.io.IOException
229      */

230     public void writeDataStream(OutputStream JavaDoc os)
231         throws IOException JavaDoc {
232
233         writeStart(os);
234
235         writeObjectList(presentationTextData, os);
236
237         writeEnd(os);
238
239     }
240
241     public String JavaDoc getName() {
242
243         return _name;
244
245     }
246
247     /**
248      * Helper method to write the start of the presenation text object.
249      * @param os The stream to write to
250      */

251     private void writeStart(OutputStream JavaDoc os)
252         throws IOException JavaDoc {
253
254         byte[] data = new byte[17];
255
256         data[0] = 0x5A; // Structured field identifier
257
data[1] = 0x00; // Length byte 1
258
data[2] = 0x10; // Length byte 2
259
data[3] = (byte) 0xD3; // Structured field id byte 1
260
data[4] = (byte) 0xA8; // Structured field id byte 2
261
data[5] = (byte) 0x9B; // Structured field id byte 3
262
data[6] = 0x00; // Flags
263
data[7] = 0x00; // Reserved
264
data[8] = 0x00; // Reserved
265

266         for (int i = 0; i < _nameBytes.length; i++) {
267
268             data[9 + i] = _nameBytes[i];
269
270         }
271
272         os.write(data);
273
274     }
275
276     /**
277      * Helper method to write the end of the presenation text object.
278      * @param os The stream to write to
279      */

280     private void writeEnd(OutputStream JavaDoc os)
281         throws IOException JavaDoc {
282
283
284         byte[] data = new byte[17];
285
286         data[0] = 0x5A; // Structured field identifier
287
data[1] = 0x00; // Length byte 1
288
data[2] = 0x10; // Length byte 2
289
data[3] = (byte) 0xD3; // Structured field id byte 1
290
data[4] = (byte) 0xA9; // Structured field id byte 2
291
data[5] = (byte) 0x9B; // Structured field id byte 3
292
data[6] = 0x00; // Flags
293
data[7] = 0x00; // Reserved
294
data[8] = 0x00; // Reserved
295

296         for (int i = 0; i < _nameBytes.length; i++) {
297
298             data[9 + i] = _nameBytes[i];
299
300         }
301
302         os.write(data);
303
304     }
305
306     /**
307      * A control sequence is a sequence of bytes that specifies a control
308      * function. A control sequence consists of a control sequence introducer
309      * and zero or more parameters. The control sequence can extend multiple
310      * presentation text data objects, but must eventually be terminated. This
311      * method terminates the control sequence.
312      */

313     public void endControlSequence() {
314
315         if (currentPresentationTextData == null) {
316             startPresentationTextData();
317         }
318
319         try {
320
321             currentPresentationTextData.endControlSequence();
322
323         } catch (MaximumSizeExceededException msee) {
324
325             endPresentationTextData();
326             endControlSequence();
327
328         }
329
330     }
331
332 }
Popular Tags