001: /*
002: * $Id: RtfPhrase.java 2776 2007-05-23 20:01: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.io.ByteArrayOutputStream;
054: import java.io.IOException;
055: import java.io.OutputStream;
056: import java.util.ArrayList;
057:
058: import com.lowagie.text.Chunk;
059: import com.lowagie.text.DocumentException;
060: import com.lowagie.text.Element;
061: import com.lowagie.text.Phrase;
062: import com.lowagie.text.rtf.RtfBasicElement;
063: import com.lowagie.text.rtf.RtfElement;
064: import com.lowagie.text.rtf.document.RtfDocument;
065: import com.lowagie.text.rtf.style.RtfFont;
066:
067: /**
068: * The RtfPhrase contains multiple RtfChunks
069: *
070: * @version $Id: RtfPhrase.java 2776 2007-05-23 20:01:40Z hallm $
071: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
072: * @author Thomas Bickel (tmb99@inode.at)
073: */
074: public class RtfPhrase extends RtfElement {
075:
076: /**
077: * Constant for the resetting of the paragraph defaults
078: */
079: public static final byte[] PARAGRAPH_DEFAULTS = "\\pard".getBytes();
080: /**
081: * Constant for resetting of font settings to their defaults
082: */
083: public static final byte[] PLAIN = "\\plain".getBytes();
084: /**
085: * Constant for phrase in a table indication
086: */
087: public static final byte[] IN_TABLE = "\\intbl".getBytes();
088: /**
089: * Constant for the line spacing.
090: */
091: public static final byte[] LINE_SPACING = "\\sl".getBytes();
092:
093: /**
094: * ArrayList containing the RtfChunks of this RtfPhrase
095: */
096: protected ArrayList chunks = new ArrayList();
097: /**
098: * The height of each line.
099: */
100: private int lineLeading = 0;
101:
102: /**
103: * A basically empty constructor that is used by the RtfParagraph.
104: *
105: * @param doc The RtfDocument this RtfPhrase belongs to.
106: */
107: protected RtfPhrase(RtfDocument doc) {
108: super (doc);
109: }
110:
111: /**
112: * Constructs a new RtfPhrase for the RtfDocument with the given Phrase
113: *
114: * @param doc The RtfDocument this RtfPhrase belongs to
115: * @param phrase The Phrase this RtfPhrase is based on
116: */
117: public RtfPhrase(RtfDocument doc, Phrase phrase) {
118: super (doc);
119:
120: if (phrase == null) {
121: return;
122: }
123:
124: if (phrase.hasLeading()) {
125: this .lineLeading = (int) (phrase.getLeading() * RtfElement.TWIPS_FACTOR);
126: } else {
127: this .lineLeading = 0;
128: }
129:
130: RtfFont phraseFont = new RtfFont(null, phrase.getFont());
131: for (int i = 0; i < phrase.size(); i++) {
132: Element chunk = (Element) phrase.get(i);
133: if (chunk instanceof Chunk) {
134: ((Chunk) chunk).setFont(phraseFont
135: .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: ByteArrayOutputStream result = new ByteArrayOutputStream();
154: try {
155: writeContent(result);
156: } catch (IOException ioe) {
157: ioe.printStackTrace();
158: }
159: return result.toByteArray();
160: }
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 result)
168: throws IOException {
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))
208: .setInHeader(inHeader);
209: }
210: }
211:
212: /**
213: * Sets the RtfDocument this RtfPhrase belongs to. Also sets the RtfDocument for all child
214: * elements.
215: *
216: * @param doc The RtfDocument to use
217: */
218: public void setRtfDocument(RtfDocument doc) {
219: super .setRtfDocument(doc);
220: for (int i = 0; i < this .chunks.size(); i++) {
221: ((RtfBasicElement) this.chunks.get(i))
222: .setRtfDocument(this.document);
223: }
224: }
225: }
|