1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.pdf;
import java.io.ByteArrayOutputStream;
import org.apache.avalon.framework.CascadingRuntimeException;
/**
* This class represents a simple number object. It also contains contains some
* utility methods for outputting numbers to PDF.
*/
public class PDFText extends PDFObject {
private static final char[] DIGITS
= {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private String text;
/**
* Returns the text.
* @return the text
*/
public String getText() {
return this.text;
}
/**
* Sets the text.
* @param text the text
*/
public void setText(String text) {
this.text = text;
}
/**
* {@inheritDoc}
*/
protected String toPDFString() {
if (getText() == null) {
throw new IllegalArgumentException(
"The text of this PDFText must not be empty");
}
StringBuffer sb = new StringBuffer(64);
sb.append(getObjectID());
sb.append("(");
sb.append(escapeText(getText()));
sb.append(")");
sb.append("\nendobj\n");
return sb.toString();
}
/**
* Escape text (see 4.4.1 in PDF 1.3 specs)
* @param text the text to encode
* @return encoded text
*/
public static final String escapeText(final String text) {
return escapeText(text, false);
}
/**
* Escape text (see 4.4.1 in PDF 1.3 specs)
* @param text the text to encode
* @param forceHexMode true if the output should follow the hex encoding rules
* @return encoded text
*/
public static final String escapeText(final String text, boolean forceHexMode) {
if (text != null && text.length() > 0) {
boolean unicode = false;
boolean hexMode = false;
if (forceHexMode) {
hexMode = true;
} else {
for (int i = 0, c = text.length(); i < c; i++) {
if (text.charAt(i) >= 128) {
unicode = true;
hexMode = true;
break;
}
}
}
if (hexMode) {
final byte[] uniBytes;
try {
uniBytes = text.getBytes("UTF-16");
} catch (java.io.UnsupportedEncodingException uee) {
throw new CascadingRuntimeException("Incompatible VM", uee);
}
return toHex(uniBytes);
} else {
final StringBuffer result = new StringBuffer(text.length() * 2);
result.append("(");
final int l = text.length();
if (unicode) {
// byte order marker (0xfeff)
result.append("\\376\\377");
for (int i = 0; i < l; i++) {
final char ch = text.charAt(i);
final int high = (ch & 0xff00) >>> 8;
final int low = ch & 0xff;
result.append("\\");
result.append(Integer.toOctalString(high));
result.append("\\");
result.append(Integer.toOctalString(low));
}
} else {
for (int i = 0; i < l; i++) {
final char ch = text.charAt(i);
if (ch < 256) {
escapeStringChar(ch, result);
} else {
throw new IllegalStateException(
"Can only treat text in 8-bit ASCII/PDFEncoding");
}
}
}
result.append(")");
return result.toString();
}
}
return "()";
}
/**
* Converts a byte array to a Hexadecimal String (3.2.3 in PDF 1.4 specs)
* @param data the data to encode
* @param brackets true if enclosing brackets should be included
* @return String the resulting string
*/
public static final String toHex(byte[] data, boolean brackets) {
final StringBuffer sb = new StringBuffer(data.length * 2);
if (brackets) {
sb.append("<");
}
for (int i = 0; i < data.length; i++) {
sb.append(DIGITS[(data[i] >>> 4) & 0x0F]);
sb.append(DIGITS[data[i] & 0x0F]);
}
if (brackets) {
sb.append(">");
}
return sb.toString();
}
/**
* Converts a byte array to a Hexadecimal String (3.2.3 in PDF 1.4 specs)
* @param data the data to encode
* @return String the resulting string
*/
public static final String toHex(byte[] data) {
return toHex(data, true);
}
/**
* Converts a String to UTF-16 (big endian).
* @param text text to convert
* @return byte[] UTF-16 stream
*/
public static final byte[] toUTF16(String text) {
try {
return text.getBytes("UnicodeBig");
} catch (java.io.UnsupportedEncodingException uee) {
throw new CascadingRuntimeException("Incompatible VM", uee);
}
}
/**
* Convert a char to a multibyte hex representation
* @param c character to encode
* @return the encoded character
*/
public static final String toUnicodeHex(char c) {
final StringBuffer buf = new StringBuffer(4);
final byte[] uniBytes;
try {
final char[] a = {c};
uniBytes = new String(a).getBytes("UTF-16BE");
} catch (java.io.UnsupportedEncodingException uee) {
throw new CascadingRuntimeException("Incompatible VM", uee);
}
for (int i = 0; i < uniBytes.length; i++) {
buf.append(DIGITS[(uniBytes[i] >>> 4) & 0x0F]);
buf.append(DIGITS[uniBytes[i] & 0x0F]);
}
return buf.toString();
}
/**
* Escaped a String as described in section 4.4 in the PDF 1.3 specs.
* @param s String to escape
* @return String the escaped String
*/
public static final String escapeString(final String s) {
if (s == null || s.length() == 0) {
return "()";
} else {
final StringBuffer sb = new StringBuffer(64);
sb.append("(");
for (int i = 0; i < s.length(); i++) {
final char c = s.charAt(i);
escapeStringChar(c, sb);
}
sb.append(")");
return sb.toString();
}
}
/**
* Escapes a character conforming to the rules established in the PostScript
* Language Reference (Search for "Literal Text Strings").
* @param c character to escape
* @param target target StringBuffer to write the escaped character to
*/
public static final void escapeStringChar(final char c, final StringBuffer target) {
if (c > 127) {
target.append("\\");
target.append(Integer.toOctalString(c));
} else {
switch (c) {
case '\n':
target.append("\\n");
break;
case '\r':
target.append("\\r");
break;
case '\t':
target.append("\\t");
break;
case '\b':
target.append("\\b");
break;
case '\f':
target.append("\\f");
break;
case '\\':
target.append("\\\\");
break;
case '(':
target.append("\\(");
break;
case ')':
target.append("\\)");
break;
default:
target.append(c);
}
}
}
/**
* Escape a byte array for output to PDF (Used for encrypted strings)
* @param data data to encode
* @return byte[] encoded data
*/
public static final byte[] escapeByteArray(byte[] data) {
ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
bout.write((int)'(');
for (int i = 0; i < data.length; i++) {
final int b = data[i];
switch (b) {
case '\n':
bout.write('\\');
bout.write('n');
break;
case '\r':
bout.write('\\');
bout.write('r');
break;
case '\t':
bout.write('\\');
bout.write('t');
break;
case '\b':
bout.write('\\');
bout.write('b');
break;
case '\f':
bout.write('\\');
bout.write('f');
break;
case '\\':
bout.write('\\');
bout.write('\\');
break;
case '(':
bout.write('\\');
bout.write('(');
break;
case ')':
bout.write('\\');
bout.write(')');
break;
default:
bout.write(b);
}
}
bout.write((int)')');
return bout.toByteArray();
}
/**
* Converts a text to PDF's "string" data type. Unsupported characters get converted to '?'
* characters (similar to what the Java "US-ASCII" encoding does).
* @see #toPDFString(CharSequence, char)
* @param text the text to convert
* @return the converted string
*/
public static String toPDFString(CharSequence text) {
return toPDFString(text, '?');
}
/**
* Converts a text to PDF's "string" data type. Unsupported characters get converted to the
* given replacement character.
* <p>
* The PDF library currently doesn't properly distinguish between the PDF
* data types "string" and "text string", so we currently restrict "string" to US-ASCII, also
* because "string" seems somewhat under-specified concerning the upper 128 bytes.
* @param text the text to convert
* @param replacement the replacement character used when substituting a character
* @return the converted string
*/
public static String toPDFString(CharSequence text, char replacement) {
StringBuffer sb = new StringBuffer();
for (int i = 0, c = text.length(); i < c; i++) {
char ch = text.charAt(i);
if (ch > 127) {
//TODO Revisit the restriction to US-ASCII once "string" and "text string" are
//"disentangled".
sb.append(replacement);
} else {
sb.append(ch);
}
}
return sb.toString();
}
}
|