1 17 18 19 20 21 27 28 package org.apache.fop.render.rtf.rtflib.rtfdoc; 29 30 import java.io.IOException ; 31 import java.io.Writer ; 32 33 36 37 public class RtfText extends RtfElement { 38 private static final int CHAR_NBSP = 160; 40 private static final int CHAR_TAB = 137; 41 private static final int CHAR_NEW_LINE = 141; 42 48 private static final int CHAR_BOLD_START = 130; 49 private static final int CHAR_BOLD_END = 131; 50 51 52 private String text; 53 private final RtfAttributes attr; 54 55 56 58 59 public static final String ATTR_BOLD = "b"; 60 61 public static final String ATTR_ITALIC = "i"; 62 63 public static final String ATTR_UNDERLINE = "ul"; 64 65 public static final String ATTR_STRIKETHROUGH = "strike"; 66 67 public static final String ATTR_FONT_SIZE = "fs"; 68 69 public static final String ATTR_FONT_FAMILY = "f"; 70 71 public static final String ATTR_FONT_COLOR = "cf"; 72 73 public static final String ATTR_BACKGROUND_COLOR = "chcbpat"; 75 public static final String ATTR_SUPERSCRIPT = "super"; 76 77 public static final String ATTR_SUBSCRIPT = "sub"; 78 79 80 81 public static final String SHADING = "shading"; 82 83 public static final String SHADING_FRONT_COLOR = "cfpat"; 84 85 public static final int FULL_SHADING = 10000; 86 87 88 89 public static final String ALIGN_CENTER = "qc"; 90 91 public static final String ALIGN_LEFT = "ql"; 92 93 public static final String ALIGN_RIGHT = "qr"; 94 95 public static final String ALIGN_JUSTIFIED = "qj"; 96 97 public static final String ALIGN_DISTRIBUTED = "qd"; 98 99 100 102 public static final String BDR_BOTTOM_SINGLE = "brdrb\\brsp40\\brdrs"; 103 104 public static final String BDR_BOTTOM_DOUBLE = "brdrb\\brsp40\\brdrdb"; 105 106 public static final String BDR_BOTTOM_EMBOSS = "brdrb\\brsp40\\brdremboss"; 107 108 public static final String BDR_BOTTOM_DOTTED = "brdrb\\brsp40\\brdrdot"; 109 110 public static final String BDR_BOTTOM_DASH = "brdrb\\brsp40\\brdrdash"; 111 112 113 117 public static final String RTF_FIELD = "field"; 118 119 public static final String RTF_FIELD_PAGE = "fldinst { PAGE }"; 120 121 public static final String RTF_FIELD_RESULT = "fldrslt"; 122 123 124 126 public static final String LEFT_INDENT_BODY = "li"; 127 128 public static final String LEFT_INDENT_FIRST = "fi-"; 129 130 public static final String RIGHT_INDENT_BODY = "ri"; 131 132 133 public static final String TAB_CENTER = "tqc\\tx"; 134 135 public static final String TAB_RIGHT = "tqr\\tx"; 136 137 public static final String TAB_LEADER_DOTS = "tldot"; 138 139 public static final String TAB_LEADER_HYPHEN = "tlhyph"; 140 141 public static final String TAB_LEADER_UNDER = "tlul"; 142 143 public static final String TAB_LEADER_THICK = "tlth"; 144 145 public static final String TAB_LEADER_EQUALS = "tleq"; 146 147 148 public static final String SPACE_BEFORE = "sb"; 150 151 public static final String SPACE_AFTER = "sa"; 152 153 154 public static final String [] ALIGNMENT = new String [] 155 { 156 ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_JUSTIFIED, ALIGN_DISTRIBUTED 157 }; 158 159 160 public static final String [] BORDER = new String [] 162 { 163 BDR_BOTTOM_SINGLE, BDR_BOTTOM_DOUBLE, BDR_BOTTOM_EMBOSS, BDR_BOTTOM_DOTTED, 164 BDR_BOTTOM_DASH 165 }; 166 167 168 public static final String [] INDENT = new String [] 169 { 170 LEFT_INDENT_BODY, LEFT_INDENT_FIRST 171 }; 172 173 174 public static final String [] TABS = new String [] 175 { 176 TAB_CENTER, TAB_RIGHT, TAB_LEADER_DOTS, TAB_LEADER_HYPHEN, TAB_LEADER_UNDER, 177 TAB_LEADER_THICK, TAB_LEADER_EQUALS 178 }; 179 180 181 182 public static final String [] ATTR_NAMES = { 183 ATTR_BOLD, 184 ATTR_ITALIC, 185 ATTR_UNDERLINE, 186 ATTR_FONT_SIZE, 187 ATTR_FONT_FAMILY, 188 ATTR_FONT_COLOR, 189 ATTR_BACKGROUND_COLOR 190 }; 191 192 195 RtfText(IRtfTextContainer parent, Writer w, String str, RtfAttributes attr) 196 throws IOException { 197 super((RtfContainer)parent, w); 198 this.text = str; 199 this.attr = attr; 200 } 201 202 206 public void writeRtfContent() throws IOException { 207 writeChars: { 208 209 if (attr != null) { 211 writeAttributes(attr, new String [] {RtfText.SPACE_BEFORE}); 212 writeAttributes(attr, new String [] {RtfText.SPACE_AFTER}); 213 } 214 215 if (isTab()) { 216 writeControlWord("tab"); 217 } else if (isNewLine()) { 218 break writeChars; 219 } else if (isBold(true)) { 220 writeControlWord("b"); 221 } else if (isBold(false)) { 222 writeControlWord("b0"); 223 } else { 226 writeGroupMark(true); 227 if (attr != null && mustWriteAttributes()) { 228 writeAttributes(attr, RtfText.ATTR_NAMES); 229 } 230 RtfStringConverter.getInstance().writeRtfString(writer, text); 231 writeGroupMark(false); 232 } 233 } 234 } 235 236 237 private boolean mustWriteAttributes() { 238 return !isEmpty() && !isNbsp(); 239 } 240 241 243 public RtfAttributes getTextContainerAttributes() { 244 if (attrib == null) { 245 return null; 246 } 247 return (RtfAttributes)this.attrib.clone(); 248 } 249 250 251 String getText() { 252 return text; 253 } 254 255 256 void setText(String str) { 257 text = str; 258 } 259 260 266 public boolean isEmpty () { 267 return text == null || text.trim().length() == 0; 268 } 269 270 278 public boolean isNbsp () { 279 if (!isEmpty ()) { 280 if (text.trim ().length () == 1 && text.charAt (0) == CHAR_NBSP) { 281 return true; 282 } 283 } 284 return false; 285 } 286 287 290 public boolean isTab() { 291 return (text.trim().length() == 1 && text.charAt(0) == CHAR_TAB); 292 } 293 294 297 public boolean isNewLine() { 298 return (text.trim().length() == 1 && text.charAt(0) == CHAR_NEW_LINE); 299 } 300 301 305 public boolean isBold(boolean isStart) { 306 if (isStart) { 307 return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_START); 308 } else { 309 return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_END); 310 } 311 } 312 313 314 public RtfAttributes getTextAttributes() { 315 return attr; 316 } 317 } 318 | Popular Tags |