KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > VerticalText


1 /*
2  *
3  * Copyright 2002 by Paulo Soares.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version 1.1
6  * (the "License"); you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the License.
12  *
13  * The Original Code is 'iText, a free JAVA-PDF library'.
14  *
15  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
16  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
17  * All Rights Reserved.
18  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
19  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
20  *
21  * Contributor(s): all the names of the contributors are added in the source code
22  * where applicable.
23  *
24  * Alternatively, the contents of this file may be used under the terms of the
25  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
26  * provisions of LGPL are applicable instead of those above. If you wish to
27  * allow use of your version of this file only under the terms of the LGPL
28  * License and not to allow others to use your version of this file under
29  * the MPL, indicate your decision by deleting the provisions above and
30  * replace them with the notice and other provisions required by the LGPL.
31  * If you do not delete the provisions above, a recipient may use your version
32  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
33  *
34  * This library is free software; you can redistribute it and/or modify it
35  * under the terms of the MPL as stated above or under the terms of the GNU
36  * Library General Public License as published by the Free Software Foundation;
37  * either version 2 of the License, or any later version.
38  *
39  * This library is distributed in the hope that it will be useful, but WITHOUT
40  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
41  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
42  * details.
43  *
44  * If you didn't download this code from the following link, you should check if
45  * you aren't using an obsolete version:
46  * http://www.lowagie.com/iText/
47  */

48 package com.lowagie.text.pdf;
49 import java.awt.Color JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52
53 import com.lowagie.text.Chunk;
54 import com.lowagie.text.Element;
55 import com.lowagie.text.Phrase;
56
57 /** Writes text vertically. Note that the naming is done according
58  * to horizontal text although it referrs to vertical text.
59  * A line with the alignment Element.LEFT_ALIGN will actually
60  * be top aligned.
61  */

62 public class VerticalText {
63
64 /** Signals that there are no more text available. */
65     public static final int NO_MORE_TEXT = 1;
66     
67 /** Signals that there is no more column. */
68     public static final int NO_MORE_COLUMN = 2;
69
70 /** The chunks that form the text. */
71     protected ArrayList JavaDoc chunks = new ArrayList JavaDoc();
72
73     /** The <CODE>PdfContent</CODE> where the text will be written to. */
74     protected PdfContentByte text;
75     
76     /** The column alignment. Default is left alignment. */
77     protected int alignment = Element.ALIGN_LEFT;
78
79     /** Marks the chunks to be eliminated when the line is written. */
80     protected int currentChunkMarker = -1;
81     
82     /** The chunk created by the splitting. */
83     protected PdfChunk currentStandbyChunk;
84     
85     /** The chunk created by the splitting. */
86     protected String JavaDoc splittedChunkText;
87
88     /** The leading
89      */

90     protected float leading;
91     
92     /** The X coordinate.
93      */

94     protected float startX;
95     
96     /** The Y coordinate.
97      */

98     protected float startY;
99     
100     /** The maximum number of vertical lines.
101      */

102     protected int maxLines;
103     
104     /** The height of the text.
105      */

106     protected float height;
107     
108     /** Creates new VerticalText
109      * @param text the place where the text will be written to. Can
110      * be a template.
111      */

112     public VerticalText(PdfContentByte text) {
113         this.text = text;
114     }
115     
116     /**
117      * Adds a <CODE>Phrase</CODE> to the current text array.
118      * @param phrase the text
119      */

120     public void addText(Phrase phrase) {
121         for (Iterator JavaDoc j = phrase.getChunks().iterator(); j.hasNext();) {
122             chunks.add(new PdfChunk((Chunk)j.next(), null));
123         }
124     }
125     
126     /**
127      * Adds a <CODE>Chunk</CODE> to the current text array.
128      * @param chunk the text
129      */

130     public void addText(Chunk chunk) {
131         chunks.add(new PdfChunk(chunk, null));
132     }
133
134     /** Sets the layout.
135      * @param startX the top right X line position
136      * @param startY the top right Y line position
137      * @param height the height of the lines
138      * @param maxLines the maximum number of lines
139      * @param leading the separation between the lines
140      */

141     public void setVerticalLayout(float startX, float startY, float height, int maxLines, float leading) {
142         this.startX = startX;
143         this.startY = startY;
144         this.height = height;
145         this.maxLines = maxLines;
146         setLeading(leading);
147     }
148     
149     /** Sets the separation between the vertical lines.
150      * @param leading the vertical line separation
151      */

152     public void setLeading(float leading) {
153         this.leading = leading;
154     }
155
156     /** Gets the separation between the vertical lines.
157      * @return the vertical line separation
158      */

159     public float getLeading() {
160         return leading;
161     }
162     
163     /**
164      * Creates a line from the chunk array.
165      * @param width the width of the line
166      * @return the line or null if no more chunks
167      */

168     protected PdfLine createLine(float width) {
169         if (chunks.isEmpty())
170             return null;
171         splittedChunkText = null;
172         currentStandbyChunk = null;
173         PdfLine line = new PdfLine(0, width, alignment, 0);
174         String JavaDoc total;
175         for (currentChunkMarker = 0; currentChunkMarker < chunks.size(); ++currentChunkMarker) {
176             PdfChunk original = (PdfChunk)(chunks.get(currentChunkMarker));
177             total = original.toString();
178             currentStandbyChunk = line.add(original);
179             if (currentStandbyChunk != null) {
180                 splittedChunkText = original.toString();
181                 original.setValue(total);
182                 return line;
183             }
184         }
185         return line;
186     }
187     
188     /**
189      * Normalizes the list of chunks when the line is accepted.
190      */

191     protected void shortenChunkArray() {
192         if (currentChunkMarker < 0)
193             return;
194         if (currentChunkMarker >= chunks.size()) {
195             chunks.clear();
196             return;
197         }
198         PdfChunk split = (PdfChunk)(chunks.get(currentChunkMarker));
199         split.setValue(splittedChunkText);
200         chunks.set(currentChunkMarker, currentStandbyChunk);
201         for (int j = currentChunkMarker - 1; j >= 0; --j)
202             chunks.remove(j);
203     }
204
205     /**
206      * Outputs the lines to the document. It is equivalent to <CODE>go(false)</CODE>.
207      * @return returns the result of the operation. It can be <CODE>NO_MORE_TEXT</CODE>
208      * and/or <CODE>NO_MORE_COLUMN</CODE>
209      */

210     public int go() {
211         return go(false);
212     }
213     
214     /**
215      * Outputs the lines to the document. The output can be simulated.
216      * @param simulate <CODE>true</CODE> to simulate the writting to the document
217      * @return returns the result of the operation. It can be <CODE>NO_MORE_TEXT</CODE>
218      * and/or <CODE>NO_MORE_COLUMN</CODE>
219      */

220     public int go(boolean simulate) {
221         boolean dirty = false;
222         PdfContentByte graphics = null;
223         if (text != null) {
224             graphics = text.getDuplicate();
225         }
226         else if (!simulate)
227             throw new NullPointerException JavaDoc("VerticalText.go with simulate==false and text==null.");
228         int status = 0;
229         for (;;) {
230             if (maxLines <= 0) {
231                 status = NO_MORE_COLUMN;
232                 if (chunks.isEmpty())
233                     status |= NO_MORE_TEXT;
234                 break;
235             }
236             if (chunks.isEmpty()) {
237                 status = NO_MORE_TEXT;
238                 break;
239             }
240             PdfLine line = createLine(height);
241             if (!simulate && !dirty) {
242                 text.beginText();
243                 dirty = true;
244             }
245             shortenChunkArray();
246             if (!simulate) {
247                 text.setTextMatrix(startX, startY - line.indentLeft());
248                 writeLine(line, text, graphics);
249             }
250             --maxLines;
251             startX -= leading;
252         }
253         if (dirty) {
254             text.endText();
255             text.add(graphics);
256         }
257         return status;
258     }
259     
260     void writeLine(PdfLine line, PdfContentByte text, PdfContentByte graphics) {
261         PdfFont currentFont = null;
262         PdfChunk chunk;
263         for (Iterator JavaDoc j = line.iterator(); j.hasNext(); ) {
264             chunk = (PdfChunk) j.next();
265             
266             if (chunk.font().compareTo(currentFont) != 0) {
267                 currentFont = chunk.font();
268                 text.setFontAndSize(currentFont.getFont(), currentFont.size());
269             }
270             Color JavaDoc color = chunk.color();
271             if (color != null)
272                 text.setColorFill(color);
273             text.showText(chunk.toString());
274             if (color != null)
275                 text.resetRGBColorFill();
276         }
277     }
278     
279     /** Sets the new text origin.
280      * @param startX the X coordinate
281      * @param startY the Y coordinate
282      */

283     public void setOrigin(float startX, float startY) {
284         this.startX = startX;
285         this.startY = startY;
286     }
287     
288     /** Gets the X coordinate where the next line will be writen. This value will change
289      * after each call to <code>go()</code>.
290      * @return the X coordinate
291      */

292     public float getOriginX() {
293         return startX;
294     }
295
296     /** Gets the Y coordinate where the next line will be writen.
297      * @return the Y coordinate
298      */

299     public float getOriginY() {
300         return startY;
301     }
302     
303     /** Gets the maximum number of available lines. This value will change
304      * after each call to <code>go()</code>.
305      * @return Value of property maxLines.
306      */

307     public int getMaxLines() {
308         return maxLines;
309     }
310     
311     /** Sets the maximum number of lines.
312      * @param maxLines the maximum number of lines
313      */

314     public void setMaxLines(int maxLines) {
315         this.maxLines = maxLines;
316     }
317     
318     /** Gets the height of the line
319      * @return the height
320      */

321     public float getHeight() {
322         return height;
323     }
324     
325     /** Sets the height of the line
326      * @param height the new height
327      */

328     public void setHeight(float height) {
329         this.height = height;
330     }
331     
332     /**
333      * Sets the alignment.
334      * @param alignment the alignment
335      */

336     public void setAlignment(int alignment) {
337         this.alignment = alignment;
338     }
339     
340     /**
341      * Gets the alignment.
342      * @return the alignment
343      */

344     public int getAlignment() {
345         return alignment;
346     }
347 }
348
Popular Tags