KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > text > RtfPhrase


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

50
51 package com.lowagie.text.rtf.text;
52
53 import java.io.ByteArrayOutputStream JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56 import java.util.ArrayList JavaDoc;
57
58 import com.lowagie.text.Chunk;
59 import com.lowagie.text.DocumentException;
60 import com.lowagie.text.Element;
61 import com.lowagie.text.Phrase;
62 import com.lowagie.text.rtf.RtfBasicElement;
63 import com.lowagie.text.rtf.RtfElement;
64 import com.lowagie.text.rtf.document.RtfDocument;
65 import com.lowagie.text.rtf.style.RtfFont;
66
67
68 /**
69  * The RtfPhrase contains multiple RtfChunks
70  *
71  * @version $Id: RtfPhrase.java 2776 2007-05-23 20:01:40Z hallm $
72  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
73  * @author Thomas Bickel (tmb99@inode.at)
74  */

75 public class RtfPhrase extends RtfElement {
76
77     /**
78      * Constant for the resetting of the paragraph defaults
79      */

80     public static final byte[] PARAGRAPH_DEFAULTS = "\\pard".getBytes();
81     /**
82      * Constant for resetting of font settings to their defaults
83      */

84     public static final byte[] PLAIN = "\\plain".getBytes();
85     /**
86      * Constant for phrase in a table indication
87      */

88     public static final byte[] IN_TABLE = "\\intbl".getBytes();
89     /**
90      * Constant for the line spacing.
91      */

92     public static final byte[] LINE_SPACING = "\\sl".getBytes();
93     
94     /**
95      * ArrayList containing the RtfChunks of this RtfPhrase
96      */

97     protected ArrayList JavaDoc chunks = new ArrayList JavaDoc();
98     /**
99      * The height of each line.
100      */

101     private int lineLeading = 0;
102     
103     /**
104      * A basically empty constructor that is used by the RtfParagraph.
105      *
106      * @param doc The RtfDocument this RtfPhrase belongs to.
107      */

108     protected RtfPhrase(RtfDocument doc) {
109         super(doc);
110     }
111     
112     /**
113      * Constructs a new RtfPhrase for the RtfDocument with the given Phrase
114      *
115      * @param doc The RtfDocument this RtfPhrase belongs to
116      * @param phrase The Phrase this RtfPhrase is based on
117      */

118     public RtfPhrase(RtfDocument doc, Phrase phrase) {
119         super(doc);
120         
121         if(phrase == null) {
122             return;
123         }
124         
125         if(phrase.hasLeading()) {
126             this.lineLeading = (int) (phrase.getLeading() * RtfElement.TWIPS_FACTOR);
127         } else {
128             this.lineLeading = 0;
129         }
130         
131         RtfFont phraseFont = new RtfFont(null, phrase.getFont());
132         for(int i = 0; i < phrase.size(); i++) {
133             Element chunk = (Element) phrase.get(i);
134             if(chunk instanceof Chunk) {
135                 ((Chunk) chunk).setFont(phraseFont.difference(((Chunk) chunk).getFont()));
136             }
137             try {
138                 chunks.add(doc.getMapper().mapElement(chunk));
139             } catch(DocumentException de) {
140             }
141         }
142     }
143     
144     /**
145      * Write the content of this RtfPhrase. First resets to the paragraph defaults
146      * then if the RtfPhrase is in a RtfCell a marker for this is written and finally
147      * the RtfChunks of this RtfPhrase are written.
148      *
149      * @return The content of this RtfPhrase
150      * @deprecated replaced by {@link #writeContent(OutputStream)}
151      */

152     public byte[] write()
153     {
154         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
155         try {
156             writeContent(result);
157         } catch(IOException JavaDoc ioe) {
158             ioe.printStackTrace();
159         }
160         return result.toByteArray();
161     }
162     /**
163      * Write the content of this RtfPhrase. First resets to the paragraph defaults
164      * then if the RtfPhrase is in a RtfCell a marker for this is written and finally
165      * the RtfChunks of this RtfPhrase are written.
166      */

167     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
168     {
169         result.write(PARAGRAPH_DEFAULTS);
170         result.write(PLAIN);
171         if(inTable) {
172             result.write(IN_TABLE);
173         }
174         if(this.lineLeading > 0) {
175             result.write(LINE_SPACING);
176             result.write(intToByteArray(this.lineLeading));
177         }
178         for(int i = 0; i < chunks.size(); i++) {
179             RtfBasicElement rbe = (RtfBasicElement) chunks.get(i);
180             //.result.write((rbe).write());
181
rbe.writeContent(result);
182         }
183     }
184     
185     /**
186      * Sets whether this RtfPhrase is in a table. Sets the correct inTable setting for all
187      * child elements.
188      *
189      * @param inTable <code>True</code> if this RtfPhrase is in a table, <code>false</code> otherwise
190      */

191     public void setInTable(boolean inTable) {
192         super.setInTable(inTable);
193         for(int i = 0; i < this.chunks.size(); i++) {
194             ((RtfBasicElement) this.chunks.get(i)).setInTable(inTable);
195         }
196     }
197     
198     /**
199      * Sets whether this RtfPhrase is in a header. Sets the correct inTable setting for all
200      * child elements.
201      *
202      * @param inHeader <code>True</code> if this RtfPhrase is in a header, <code>false</code> otherwise
203      */

204     public void setInHeader(boolean inHeader) {
205         super.setInHeader(inHeader);
206         for(int i = 0; i < this.chunks.size(); i++) {
207             ((RtfBasicElement) this.chunks.get(i)).setInHeader(inHeader);
208         }
209     }
210     
211     /**
212      * Sets the RtfDocument this RtfPhrase belongs to. Also sets the RtfDocument for all child
213      * elements.
214      *
215      * @param doc The RtfDocument to use
216      */

217     public void setRtfDocument(RtfDocument doc) {
218         super.setRtfDocument(doc);
219         for(int i = 0; i < this.chunks.size(); i++) {
220             ((RtfBasicElement) this.chunks.get(i)).setRtfDocument(this.document);
221         }
222     }
223 }
224
Popular Tags