1 17 18 19 20 package org.apache.fop.pdf; 21 22 import java.io.ByteArrayOutputStream ; 23 24 import org.apache.avalon.framework.CascadingRuntimeException; 25 26 30 public class PDFText extends PDFObject { 31 32 private static final char[] DIGITS = 33 {'0', '1', '2', '3', '4', '5', '6', '7', 34 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 35 36 private String text; 37 38 42 public String getText() { 43 return this.text; 44 } 45 46 50 public void setText(String text) { 51 this.text = text; 52 } 53 54 57 protected String toPDFString() { 58 if (getText() == null) { 59 throw new IllegalArgumentException ( 60 "The text of this PDFText must not be empty"); 61 } 62 StringBuffer sb = new StringBuffer (64); 63 sb.append(getObjectID()); 64 sb.append("("); 65 sb.append(escapeText(getText())); 66 sb.append(")"); 67 sb.append("\nendobj\n"); 68 return sb.toString(); 69 } 70 71 76 public static final String escapeText(final String text) { 77 return escapeText(text, false); 78 } 79 85 public static final String escapeText(final String text, boolean forceHexMode) { 86 if (text != null && text.length() > 0) { 87 boolean unicode = false; 88 boolean hexMode = false; 89 if (forceHexMode) { 90 hexMode = true; 91 } else { 92 for (int i = 0, c = text.length(); i < c; i++) { 93 if (text.charAt(i) >= 128) { 94 unicode = true; 95 hexMode = true; 96 break; 97 } 98 } 99 } 100 101 if (hexMode) { 102 final byte[] uniBytes; 103 try { 104 uniBytes = text.getBytes("UTF-16"); 105 } catch (java.io.UnsupportedEncodingException uee) { 106 throw new CascadingRuntimeException("Incompatible VM", uee); 107 } 108 return toHex(uniBytes); 109 } else { 110 final StringBuffer result = new StringBuffer (text.length() * 2); 111 result.append("("); 112 final int l = text.length(); 113 114 if (unicode) { 115 result.append("\\376\\377"); 117 118 for (int i = 0; i < l; i++) { 119 final char ch = text.charAt(i); 120 final int high = (ch & 0xff00) >>> 8; 121 final int low = ch & 0xff; 122 result.append("\\"); 123 result.append(Integer.toOctalString(high)); 124 result.append("\\"); 125 result.append(Integer.toOctalString(low)); 126 } 127 } else { 128 for (int i = 0; i < l; i++) { 129 final char ch = text.charAt(i); 130 if (ch < 256) { 131 escapeStringChar(ch, result); 132 } else { 133 throw new IllegalStateException ( 134 "Can only treat text in 8-bit ASCII/PDFEncoding"); 135 } 136 } 137 } 138 result.append(")"); 139 return result.toString(); 140 } 141 } 142 return "()"; 143 } 144 145 150 public static final String toHex(byte[] data) { 151 final StringBuffer sb = new StringBuffer (data.length * 2); 152 sb.append("<"); 153 for (int i = 0; i < data.length; i++) { 154 sb.append(DIGITS[(data[i] >>> 4) & 0x0F]); 155 sb.append(DIGITS[data[i] & 0x0F]); 156 } 157 sb.append(">"); 158 return sb.toString(); 159 } 160 161 166 public static final byte[] toUTF16(String text) { 167 try { 168 return text.getBytes("UnicodeBig"); 169 } catch (java.io.UnsupportedEncodingException uee) { 170 throw new CascadingRuntimeException("Incompatible VM", uee); 171 } 172 } 173 174 179 public static final String toUnicodeHex(char c) { 180 final StringBuffer buf = new StringBuffer (4); 181 final byte[] uniBytes; 182 try { 183 final char[] a = {c}; 184 uniBytes = new String (a).getBytes("UTF-16BE"); 185 } catch (java.io.UnsupportedEncodingException uee) { 186 throw new CascadingRuntimeException("Incompatible VM", uee); 187 } 188 189 for (int i = 0; i < uniBytes.length; i++) { 190 buf.append(DIGITS[(uniBytes[i] >>> 4) & 0x0F]); 191 buf.append(DIGITS[uniBytes[i] & 0x0F]); 192 } 193 return buf.toString(); 194 } 195 196 201 public static final String escapeString(final String s) { 202 if (s == null || s.length() == 0) { 203 return "()"; 204 } else { 205 final StringBuffer sb = new StringBuffer (64); 206 sb.append("("); 207 for (int i = 0; i < s.length(); i++) { 208 final char c = s.charAt(i); 209 escapeStringChar(c, sb); 210 } 211 sb.append(")"); 212 return sb.toString(); 213 } 214 } 215 216 222 public static final void escapeStringChar(final char c, final StringBuffer target) { 223 if (c > 127) { 224 target.append("\\"); 225 target.append(Integer.toOctalString(c)); 226 } else { 227 switch (c) { 228 case '\n': 229 target.append("\\n"); 230 break; 231 case '\r': 232 target.append("\\r"); 233 break; 234 case '\t': 235 target.append("\\t"); 236 break; 237 case '\b': 238 target.append("\\b"); 239 break; 240 case '\f': 241 target.append("\\f"); 242 break; 243 case '\\': 244 target.append("\\\\"); 245 break; 246 case '(': 247 target.append("\\("); 248 break; 249 case ')': 250 target.append("\\)"); 251 break; 252 default: 253 target.append(c); 254 } 255 } 256 } 257 258 263 public static final byte[] escapeByteArray(byte[] data) { 264 ByteArrayOutputStream bout = new ByteArrayOutputStream (data.length); 265 bout.write((int)'('); 266 for (int i = 0; i < data.length; i++) { 267 final int b = data[i]; 268 switch (b) { 269 case '\n': 270 bout.write('\\'); 271 bout.write('n'); 272 break; 273 case '\r': 274 bout.write('\\'); 275 bout.write('r'); 276 break; 277 case '\t': 278 bout.write('\\'); 279 bout.write('t'); 280 break; 281 case '\b': 282 bout.write('\\'); 283 bout.write('b'); 284 break; 285 case '\f': 286 bout.write('\\'); 287 bout.write('f'); 288 break; 289 case '\\': 290 bout.write('\\'); 291 bout.write('\\'); 292 break; 293 case '(': 294 bout.write('\\'); 295 bout.write('('); 296 break; 297 case ')': 298 bout.write('\\'); 299 bout.write(')'); 300 break; 301 default: 302 bout.write(b); 303 } 304 } 305 bout.write((int)')'); 306 return bout.toByteArray(); 307 } 308 309 } 310 311 | Popular Tags |