KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFText


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: PDFText.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.pdf;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24 import org.apache.avalon.framework.CascadingRuntimeException;
25
26 /**
27  * This class represents a simple number object. It also contains contains some
28  * utility methods for outputing numbers to PDF.
29  */

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 JavaDoc text;
37
38     /**
39      * Returns the text.
40      * @return the text
41      */

42     public String JavaDoc getText() {
43         return this.text;
44     }
45     
46     /**
47      * Sets the text.
48      * @param text the text
49      */

50     public void setText(String JavaDoc text) {
51         this.text = text;
52     }
53
54     /**
55      * @see org.apache.fop.pdf.PDFObject#toPDFString()
56      */

57     protected String JavaDoc toPDFString() {
58         if (getText() == null) {
59             throw new IllegalArgumentException JavaDoc(
60                 "The text of this PDFText must not be empty");
61         }
62         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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     /**
72      * Escape text (see 4.4.1 in PDF 1.3 specs)
73      * @param text the text to encode
74      * @return encoded text
75      */

76     public static final String JavaDoc escapeText(final String JavaDoc text) {
77         return escapeText(text, false);
78     }
79     /**
80      * Escape text (see 4.4.1 in PDF 1.3 specs)
81      * @param text the text to encode
82      * @param forceHexMode true if the output should follow the hex encoding rules
83      * @return encoded text
84      */

85     public static final String JavaDoc escapeText(final String JavaDoc 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 JavaDoc uee) {
106                     throw new CascadingRuntimeException("Incompatible VM", uee);
107                 }
108                 return toHex(uniBytes);
109             } else {
110                 final StringBuffer JavaDoc result = new StringBuffer JavaDoc(text.length() * 2);
111                 result.append("(");
112                 final int l = text.length();
113
114                 if (unicode) {
115                     // byte order marker (0xfeff)
116
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 JavaDoc(
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     /**
146      * Converts a byte array to a Hexadecimal String (3.2.3 in PDF 1.4 specs)
147      * @param data the data to encode
148      * @return String the resulting string
149      */

150     public static final String JavaDoc toHex(byte[] data) {
151         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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     /**
162      * Converts a String to UTF-16 (big endian).
163      * @param text text to convert
164      * @return byte[] UTF-17 stream
165      */

166     public static final byte[] toUTF16(String JavaDoc text) {
167         try {
168             return text.getBytes("UnicodeBig");
169         } catch (java.io.UnsupportedEncodingException JavaDoc uee) {
170             throw new CascadingRuntimeException("Incompatible VM", uee);
171         }
172     }
173
174     /**
175      * Convert a char to a multibyte hex representation
176      * @param c character to encode
177      * @return the encoded character
178      */

179     public static final String JavaDoc toUnicodeHex(char c) {
180         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(4);
181         final byte[] uniBytes;
182         try {
183             final char[] a = {c};
184             uniBytes = new String JavaDoc(a).getBytes("UTF-16BE");
185         } catch (java.io.UnsupportedEncodingException JavaDoc 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     /**
197      * Escaped a String as described in section 4.4 in the PDF 1.3 specs.
198      * @param s String to escape
199      * @return String the escaped String
200      */

201     public static final String JavaDoc escapeString(final String JavaDoc s) {
202         if (s == null || s.length() == 0) {
203             return "()";
204         } else {
205             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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     /**
217      * Escapes a character conforming to the rules established in the PostScript
218      * Language Reference (Search for "Literal Text Strings").
219      * @param c character to escape
220      * @param target target StringBuffer to write the escaped character to
221      */

222     public static final void escapeStringChar(final char c, final StringBuffer JavaDoc 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     /**
259      * Escape a byte array for output to PDF (Used for encrypted strings)
260      * @param data data to encode
261      * @return byte[] encoded data
262      */

263     public static final byte[] escapeByteArray(byte[] data) {
264         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc(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