1 50 51 package com.lowagie.text.rtf.text; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.io.OutputStream ; 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 73 public class RtfChunk extends RtfElement { 74 75 78 private static final byte[] FONT_SUBSCRIPT = "\\sub".getBytes(); 79 82 private static final byte[] FONT_SUPERSCRIPT = "\\super".getBytes(); 83 86 private static final byte[] FONT_END_SUPER_SUBSCRIPT = "\\nosupersub".getBytes(); 87 90 private static final byte[] HIGHLIGHT = "\\highlight".getBytes(); 91 92 95 private RtfFont font = null; 96 99 private String content = ""; 100 103 private boolean softLineBreaks = false; 104 107 private float superSubScript = 0; 108 111 private RtfColor background = null; 112 113 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 )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 ) ((Object []) chunk.getAttributes().get(Chunk.BACKGROUND))[0]); 131 } 132 font = new RtfFont(doc, chunk.getFont()); 133 content = chunk.getContent(); 134 } 135 136 143 public byte[] write() { 144 ByteArrayOutputStream result = new ByteArrayOutputStream (); 145 try { 146 writeContent(result); 147 } catch(IOException ioe) { 148 ioe.printStackTrace(); 149 } 150 return result.toByteArray(); 151 } 152 156 public void writeContent(final OutputStream result) throws IOException 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 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 191 public void setRtfDocument(RtfDocument doc) { 192 super.setRtfDocument(doc); 193 this.font.setRtfDocument(this.document); 194 } 195 196 201 public void setSoftLineBreaks(boolean softLineBreaks) { 202 this.softLineBreaks = softLineBreaks; 203 } 204 205 210 public boolean getSoftLineBreaks() { 211 return this.softLineBreaks; 212 } 213 } 214 | Popular Tags |