You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PDFText.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /* $Id$ */
  18. package org.apache.fop.pdf;
  19. import java.io.ByteArrayOutputStream;
  20. import org.apache.avalon.framework.CascadingRuntimeException;
  21. /**
  22. * This class represents a simple number object. It also contains contains some
  23. * utility methods for outputting numbers to PDF.
  24. */
  25. public class PDFText extends PDFObject {
  26. private static final char[] DIGITS
  27. = {'0', '1', '2', '3', '4', '5', '6', '7',
  28. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  29. private String text;
  30. /**
  31. * Returns the text.
  32. * @return the text
  33. */
  34. public String getText() {
  35. return this.text;
  36. }
  37. /**
  38. * Sets the text.
  39. * @param text the text
  40. */
  41. public void setText(String text) {
  42. this.text = text;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. protected String toPDFString() {
  48. if (getText() == null) {
  49. throw new IllegalArgumentException(
  50. "The text of this PDFText must not be empty");
  51. }
  52. StringBuffer sb = new StringBuffer(64);
  53. sb.append(getObjectID());
  54. sb.append("(");
  55. sb.append(escapeText(getText()));
  56. sb.append(")");
  57. sb.append("\nendobj\n");
  58. return sb.toString();
  59. }
  60. /**
  61. * Escape text (see 4.4.1 in PDF 1.3 specs)
  62. * @param text the text to encode
  63. * @return encoded text
  64. */
  65. public static final String escapeText(final String text) {
  66. return escapeText(text, false);
  67. }
  68. /**
  69. * Escape text (see 4.4.1 in PDF 1.3 specs)
  70. * @param text the text to encode
  71. * @param forceHexMode true if the output should follow the hex encoding rules
  72. * @return encoded text
  73. */
  74. public static final String escapeText(final String text, boolean forceHexMode) {
  75. if (text != null && text.length() > 0) {
  76. boolean unicode = false;
  77. boolean hexMode = false;
  78. if (forceHexMode) {
  79. hexMode = true;
  80. } else {
  81. for (int i = 0, c = text.length(); i < c; i++) {
  82. if (text.charAt(i) >= 128) {
  83. unicode = true;
  84. hexMode = true;
  85. break;
  86. }
  87. }
  88. }
  89. if (hexMode) {
  90. final byte[] uniBytes;
  91. try {
  92. uniBytes = text.getBytes("UTF-16");
  93. } catch (java.io.UnsupportedEncodingException uee) {
  94. throw new CascadingRuntimeException("Incompatible VM", uee);
  95. }
  96. return toHex(uniBytes);
  97. } else {
  98. final StringBuffer result = new StringBuffer(text.length() * 2);
  99. result.append("(");
  100. final int l = text.length();
  101. if (unicode) {
  102. // byte order marker (0xfeff)
  103. result.append("\\376\\377");
  104. for (int i = 0; i < l; i++) {
  105. final char ch = text.charAt(i);
  106. final int high = (ch & 0xff00) >>> 8;
  107. final int low = ch & 0xff;
  108. result.append("\\");
  109. result.append(Integer.toOctalString(high));
  110. result.append("\\");
  111. result.append(Integer.toOctalString(low));
  112. }
  113. } else {
  114. for (int i = 0; i < l; i++) {
  115. final char ch = text.charAt(i);
  116. if (ch < 256) {
  117. escapeStringChar(ch, result);
  118. } else {
  119. throw new IllegalStateException(
  120. "Can only treat text in 8-bit ASCII/PDFEncoding");
  121. }
  122. }
  123. }
  124. result.append(")");
  125. return result.toString();
  126. }
  127. }
  128. return "()";
  129. }
  130. /**
  131. * Converts a byte array to a Hexadecimal String (3.2.3 in PDF 1.4 specs)
  132. * @param data the data to encode
  133. * @param brackets true if enclosing brackets should be included
  134. * @return String the resulting string
  135. */
  136. public static final String toHex(byte[] data, boolean brackets) {
  137. final StringBuffer sb = new StringBuffer(data.length * 2);
  138. if (brackets) {
  139. sb.append("<");
  140. }
  141. for (int i = 0; i < data.length; i++) {
  142. sb.append(DIGITS[(data[i] >>> 4) & 0x0F]);
  143. sb.append(DIGITS[data[i] & 0x0F]);
  144. }
  145. if (brackets) {
  146. sb.append(">");
  147. }
  148. return sb.toString();
  149. }
  150. /**
  151. * Converts a byte array to a Hexadecimal String (3.2.3 in PDF 1.4 specs)
  152. * @param data the data to encode
  153. * @return String the resulting string
  154. */
  155. public static final String toHex(byte[] data) {
  156. return toHex(data, true);
  157. }
  158. /**
  159. * Converts a String to UTF-16 (big endian).
  160. * @param text text to convert
  161. * @return byte[] UTF-16 stream
  162. */
  163. public static final byte[] toUTF16(String text) {
  164. try {
  165. return text.getBytes("UnicodeBig");
  166. } catch (java.io.UnsupportedEncodingException uee) {
  167. throw new CascadingRuntimeException("Incompatible VM", uee);
  168. }
  169. }
  170. /**
  171. * Convert a char to a multibyte hex representation
  172. * @param c character to encode
  173. * @return the encoded character
  174. */
  175. public static final String toUnicodeHex(char c) {
  176. final StringBuffer buf = new StringBuffer(4);
  177. final byte[] uniBytes;
  178. try {
  179. final char[] a = {c};
  180. uniBytes = new String(a).getBytes("UTF-16BE");
  181. } catch (java.io.UnsupportedEncodingException uee) {
  182. throw new CascadingRuntimeException("Incompatible VM", uee);
  183. }
  184. for (int i = 0; i < uniBytes.length; i++) {
  185. buf.append(DIGITS[(uniBytes[i] >>> 4) & 0x0F]);
  186. buf.append(DIGITS[uniBytes[i] & 0x0F]);
  187. }
  188. return buf.toString();
  189. }
  190. /**
  191. * Escaped a String as described in section 4.4 in the PDF 1.3 specs.
  192. * @param s String to escape
  193. * @return String the escaped String
  194. */
  195. public static final String escapeString(final String s) {
  196. if (s == null || s.length() == 0) {
  197. return "()";
  198. } else {
  199. final StringBuffer sb = new StringBuffer(64);
  200. sb.append("(");
  201. for (int i = 0; i < s.length(); i++) {
  202. final char c = s.charAt(i);
  203. escapeStringChar(c, sb);
  204. }
  205. sb.append(")");
  206. return sb.toString();
  207. }
  208. }
  209. /**
  210. * Escapes a character conforming to the rules established in the PostScript
  211. * Language Reference (Search for "Literal Text Strings").
  212. * @param c character to escape
  213. * @param target target StringBuffer to write the escaped character to
  214. */
  215. public static final void escapeStringChar(final char c, final StringBuffer target) {
  216. if (c > 127) {
  217. target.append("\\");
  218. target.append(Integer.toOctalString(c));
  219. } else {
  220. switch (c) {
  221. case '\n':
  222. target.append("\\n");
  223. break;
  224. case '\r':
  225. target.append("\\r");
  226. break;
  227. case '\t':
  228. target.append("\\t");
  229. break;
  230. case '\b':
  231. target.append("\\b");
  232. break;
  233. case '\f':
  234. target.append("\\f");
  235. break;
  236. case '\\':
  237. target.append("\\\\");
  238. break;
  239. case '(':
  240. target.append("\\(");
  241. break;
  242. case ')':
  243. target.append("\\)");
  244. break;
  245. default:
  246. target.append(c);
  247. }
  248. }
  249. }
  250. /**
  251. * Escape a byte array for output to PDF (Used for encrypted strings)
  252. * @param data data to encode
  253. * @return byte[] encoded data
  254. */
  255. public static final byte[] escapeByteArray(byte[] data) {
  256. ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length);
  257. bout.write((int)'(');
  258. for (int i = 0; i < data.length; i++) {
  259. final int b = data[i];
  260. switch (b) {
  261. case '\n':
  262. bout.write('\\');
  263. bout.write('n');
  264. break;
  265. case '\r':
  266. bout.write('\\');
  267. bout.write('r');
  268. break;
  269. case '\t':
  270. bout.write('\\');
  271. bout.write('t');
  272. break;
  273. case '\b':
  274. bout.write('\\');
  275. bout.write('b');
  276. break;
  277. case '\f':
  278. bout.write('\\');
  279. bout.write('f');
  280. break;
  281. case '\\':
  282. bout.write('\\');
  283. bout.write('\\');
  284. break;
  285. case '(':
  286. bout.write('\\');
  287. bout.write('(');
  288. break;
  289. case ')':
  290. bout.write('\\');
  291. bout.write(')');
  292. break;
  293. default:
  294. bout.write(b);
  295. }
  296. }
  297. bout.write((int)')');
  298. return bout.toByteArray();
  299. }
  300. }