KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: RtfChunk.java 2784 2007-05-24 15:43: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.awt.Color JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 import com.lowagie.text.Chunk;
59 import com.lowagie.text.rtf.RtfElement;
60 import com.lowagie.text.rtf.document.RtfDocument;
61 import com.lowagie.text.rtf.style.RtfColor;
62 import com.lowagie.text.rtf.style.RtfFont;
63
64
65 /**
66  * The RtfChunk contains one piece of text. The smallest text element available
67  * in iText.
68  *
69  * @version $Id: RtfChunk.java 2784 2007-05-24 15:43:40Z hallm $
70  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
71  * @author Thomas Bickel (tmb99@inode.at)
72  */

73 public class RtfChunk extends RtfElement {
74
75     /**
76      * Constant for the subscript flag
77      */

78     private static final byte[] FONT_SUBSCRIPT = "\\sub".getBytes();
79     /**
80      * Constant for the superscript flag
81      */

82     private static final byte[] FONT_SUPERSCRIPT = "\\super".getBytes();
83     /**
84      * Constant for the end of sub / superscript flag
85      */

86     private static final byte[] FONT_END_SUPER_SUBSCRIPT = "\\nosupersub".getBytes();
87     /**
88      * Constant for background colour.
89      */

90     private static final byte[] HIGHLIGHT = "\\highlight".getBytes();
91
92     /**
93      * The font of this RtfChunk
94      */

95     private RtfFont font = null;
96     /**
97      * The actual content of this RtfChunk
98      */

99     private String JavaDoc content = "";
100     /**
101      * Whether to use soft line breaks instead of hard ones.
102      */

103     private boolean softLineBreaks = false;
104     /**
105      * The super / subscript of this RtfChunk
106      */

107     private float superSubScript = 0;
108     /**
109      * An optional background colour.
110      */

111     private RtfColor background = null;
112
113     /**
114      * Constructs a RtfChunk based on the content of a Chunk
115      *
116      * @param doc The RtfDocument that this Chunk belongs to
117      * @param chunk The Chunk that this RtfChunk is based on
118      */

119     public RtfChunk(RtfDocument doc, Chunk chunk) {
120         super(doc);
121         
122         if(chunk == null) {
123             return;
124         }
125         
126         if(chunk.getAttributes() != null && chunk.getAttributes().get(Chunk.SUBSUPSCRIPT) != null) {
127             this.superSubScript = ((Float JavaDoc)chunk.getAttributes().get(Chunk.SUBSUPSCRIPT)).floatValue();
128         }
129         if(chunk.getAttributes() != null && chunk.getAttributes().get(Chunk.BACKGROUND) != null) {
130             this.background = new RtfColor(this.document, (Color JavaDoc) ((Object JavaDoc[]) chunk.getAttributes().get(Chunk.BACKGROUND))[0]);
131         }
132         font = new RtfFont(doc, chunk.getFont());
133         content = chunk.getContent();
134     }
135     
136     /**
137      * Writes the content of this RtfChunk. First the font information
138      * is written, then the content, and then more font information
139      *
140      * @return A byte array with the content of this RtfChunk
141      * @deprecated replaced by {@link #writeContent(OutputStream)}
142      */

143     public byte[] write() {
144         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
145         try {
146             writeContent(result);
147         } catch(IOException JavaDoc ioe) {
148             ioe.printStackTrace();
149         }
150         return result.toByteArray();
151     }
152     /**
153      * Writes the content of this RtfChunk. First the font information
154      * is written, then the content, and then more font information
155      */

156     public void writeContent(final OutputStream JavaDoc result) throws IOException JavaDoc
157     {
158         if(this.background != null) {
159             result.write(OPEN_GROUP);
160         }
161         
162         result.write(font.writeBegin());
163         if(superSubScript < 0) {
164             result.write(FONT_SUBSCRIPT);
165         } else if(superSubScript > 0) {
166             result.write(FONT_SUPERSCRIPT);
167         }
168         if(this.background != null) {
169             result.write(HIGHLIGHT);
170             result.write(intToByteArray(this.background.getColorNumber()));
171         }
172         result.write(DELIMITER);
173         //.result.write(document.filterSpecialChar(content, false, softLineBreaks || this.document.getDocumentSettings().isAlwaysGenerateSoftLinebreaks()).getBytes());
174
document.filterSpecialChar(result, content, false, softLineBreaks || this.document.getDocumentSettings().isAlwaysGenerateSoftLinebreaks());
175         
176         if(superSubScript != 0) {
177             result.write(FONT_END_SUPER_SUBSCRIPT);
178         }
179         result.write(font.writeEnd());
180         
181         if(this.background != null) {
182             result.write(CLOSE_GROUP);
183         }
184     }
185     
186     /**
187      * Sets the RtfDocument this RtfChunk belongs to.
188      *
189      * @param doc The RtfDocument to use
190      */

191     public void setRtfDocument(RtfDocument doc) {
192         super.setRtfDocument(doc);
193         this.font.setRtfDocument(this.document);
194     }
195     
196     /**
197      * Sets whether to use soft line breaks instead of default hard ones.
198      *
199      * @param softLineBreaks whether to use soft line breaks instead of default hard ones.
200      */

201     public void setSoftLineBreaks(boolean softLineBreaks) {
202         this.softLineBreaks = softLineBreaks;
203     }
204     
205     /**
206      * Gets whether to use soft line breaks instead of default hard ones.
207      *
208      * @return whether to use soft line breaks instead of default hard ones.
209      */

210     public boolean getSoftLineBreaks() {
211         return this.softLineBreaks;
212     }
213 }
214
Popular Tags