001: /*
002: * $Id: RtfChunk.java 2784 2007-05-24 15:43:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.text;
052:
053: import java.awt.Color;
054: import java.io.ByteArrayOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057:
058: import com.lowagie.text.Chunk;
059: import com.lowagie.text.rtf.RtfElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061: import com.lowagie.text.rtf.style.RtfColor;
062: import com.lowagie.text.rtf.style.RtfFont;
063:
064: /**
065: * The RtfChunk contains one piece of text. The smallest text element available
066: * in iText.
067: *
068: * @version $Id: RtfChunk.java 2784 2007-05-24 15:43:40Z hallm $
069: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
070: * @author Thomas Bickel (tmb99@inode.at)
071: */
072: public class RtfChunk extends RtfElement {
073:
074: /**
075: * Constant for the subscript flag
076: */
077: private static final byte[] FONT_SUBSCRIPT = "\\sub".getBytes();
078: /**
079: * Constant for the superscript flag
080: */
081: private static final byte[] FONT_SUPERSCRIPT = "\\super".getBytes();
082: /**
083: * Constant for the end of sub / superscript flag
084: */
085: private static final byte[] FONT_END_SUPER_SUBSCRIPT = "\\nosupersub"
086: .getBytes();
087: /**
088: * Constant for background colour.
089: */
090: private static final byte[] HIGHLIGHT = "\\highlight".getBytes();
091:
092: /**
093: * The font of this RtfChunk
094: */
095: private RtfFont font = null;
096: /**
097: * The actual content of this RtfChunk
098: */
099: private String 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 super SubScript = 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
127: && chunk.getAttributes().get(Chunk.SUBSUPSCRIPT) != null) {
128: this .super SubScript = ((Float) chunk.getAttributes().get(
129: Chunk.SUBSUPSCRIPT)).floatValue();
130: }
131: if (chunk.getAttributes() != null
132: && chunk.getAttributes().get(Chunk.BACKGROUND) != null) {
133: this .background = new RtfColor(this .document,
134: (Color) ((Object[]) chunk.getAttributes().get(
135: Chunk.BACKGROUND))[0]);
136: }
137: font = new RtfFont(doc, chunk.getFont());
138: content = chunk.getContent();
139: }
140:
141: /**
142: * Writes the content of this RtfChunk. First the font information
143: * is written, then the content, and then more font information
144: *
145: * @return A byte array with the content of this RtfChunk
146: * @deprecated replaced by {@link #writeContent(OutputStream)}
147: */
148: public byte[] write() {
149: ByteArrayOutputStream result = new ByteArrayOutputStream();
150: try {
151: writeContent(result);
152: } catch (IOException ioe) {
153: ioe.printStackTrace();
154: }
155: return result.toByteArray();
156: }
157:
158: /**
159: * Writes the content of this RtfChunk. First the font information
160: * is written, then the content, and then more font information
161: */
162: public void writeContent(final OutputStream result)
163: throws IOException {
164: if (this .background != null) {
165: result.write(OPEN_GROUP);
166: }
167:
168: result.write(font.writeBegin());
169: if (super SubScript < 0) {
170: result.write(FONT_SUBSCRIPT);
171: } else if (super SubScript > 0) {
172: result.write(FONT_SUPERSCRIPT);
173: }
174: if (this .background != null) {
175: result.write(HIGHLIGHT);
176: result.write(intToByteArray(this .background
177: .getColorNumber()));
178: }
179: result.write(DELIMITER);
180: //.result.write(document.filterSpecialChar(content, false, softLineBreaks || this.document.getDocumentSettings().isAlwaysGenerateSoftLinebreaks()).getBytes());
181: document.filterSpecialChar(result, content, false,
182: softLineBreaks
183: || this .document.getDocumentSettings()
184: .isAlwaysGenerateSoftLinebreaks());
185:
186: if (super SubScript != 0) {
187: result.write(FONT_END_SUPER_SUBSCRIPT);
188: }
189: result.write(font.writeEnd());
190:
191: if (this .background != null) {
192: result.write(CLOSE_GROUP);
193: }
194: }
195:
196: /**
197: * Sets the RtfDocument this RtfChunk belongs to.
198: *
199: * @param doc The RtfDocument to use
200: */
201: public void setRtfDocument(RtfDocument doc) {
202: super .setRtfDocument(doc);
203: this .font.setRtfDocument(this .document);
204: }
205:
206: /**
207: * Sets whether to use soft line breaks instead of default hard ones.
208: *
209: * @param softLineBreaks whether to use soft line breaks instead of default hard ones.
210: */
211: public void setSoftLineBreaks(boolean softLineBreaks) {
212: this .softLineBreaks = softLineBreaks;
213: }
214:
215: /**
216: * Gets whether to use soft line breaks instead of default hard ones.
217: *
218: * @return whether to use soft line breaks instead of default hard ones.
219: */
220: public boolean getSoftLineBreaks() {
221: return this.softLineBreaks;
222: }
223: }
|