1 50 51 package com.lowagie.text.rtf.text; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.ArrayList ; 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 75 public class RtfPhrase extends RtfElement { 76 77 80 public static final byte[] PARAGRAPH_DEFAULTS = "\\pard".getBytes(); 81 84 public static final byte[] PLAIN = "\\plain".getBytes(); 85 88 public static final byte[] IN_TABLE = "\\intbl".getBytes(); 89 92 public static final byte[] LINE_SPACING = "\\sl".getBytes(); 93 94 97 protected ArrayList chunks = new ArrayList (); 98 101 private int lineLeading = 0; 102 103 108 protected RtfPhrase(RtfDocument doc) { 109 super(doc); 110 } 111 112 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 152 public byte[] write() 153 { 154 ByteArrayOutputStream result = new ByteArrayOutputStream (); 155 try { 156 writeContent(result); 157 } catch(IOException ioe) { 158 ioe.printStackTrace(); 159 } 160 return result.toByteArray(); 161 } 162 167 public void writeContent(final OutputStream result) throws IOException 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 rbe.writeContent(result); 182 } 183 } 184 185 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 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 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 |