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.

PDFTextUtil.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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.awt.geom.AffineTransform;
  20. /**
  21. * Utility class for generating PDF text objects. It needs to be subclassed to add writing
  22. * functionality (see {@link #write(String)}).
  23. */
  24. public abstract class PDFTextUtil {
  25. /** The number of decimal places. */
  26. private static final int DEC = 8;
  27. /** PDF text rendering mode: Fill text */
  28. public static final int TR_FILL = 0;
  29. /** PDF text rendering mode: Stroke text */
  30. public static final int TR_STROKE = 1;
  31. /** PDF text rendering mode: Fill, then stroke text */
  32. public static final int TR_FILL_STROKE = 2;
  33. /** PDF text rendering mode: Neither fill nor stroke text (invisible) */
  34. public static final int TR_INVISIBLE = 3;
  35. /** PDF text rendering mode: Fill text and add to path for clipping */
  36. public static final int TR_FILL_CLIP = 4;
  37. /** PDF text rendering mode: Stroke text and add to path for clipping */
  38. public static final int TR_STROKE_CLIP = 5;
  39. /** PDF text rendering mode: Fill, then stroke text and add to path for clipping */
  40. public static final int TR_FILL_STROKE_CLIP = 6;
  41. /** PDF text rendering mode: Add text to path for clipping */
  42. public static final int TR_CLIP = 7;
  43. private boolean inTextObject;
  44. private String startText;
  45. private String endText;
  46. private boolean useMultiByte;
  47. private boolean useCid;
  48. private StringBuffer bufTJ;
  49. private int textRenderingMode = TR_FILL;
  50. private String currentFontName;
  51. private double currentFontSize;
  52. /**
  53. * Main constructor.
  54. */
  55. public PDFTextUtil() {
  56. //nop
  57. }
  58. /**
  59. * Writes PDF code.
  60. * @param code the PDF code to write
  61. */
  62. protected abstract void write(String code);
  63. /**
  64. * Writes PDF code.
  65. * @param code the PDF code to write
  66. */
  67. protected abstract void write(StringBuffer code);
  68. private void writeAffineTransform(AffineTransform at, StringBuffer sb) {
  69. double[] lt = new double[6];
  70. at.getMatrix(lt);
  71. PDFNumber.doubleOut(lt[0], DEC, sb);
  72. sb.append(' ');
  73. PDFNumber.doubleOut(lt[1], DEC, sb);
  74. sb.append(' ');
  75. PDFNumber.doubleOut(lt[2], DEC, sb);
  76. sb.append(' ');
  77. PDFNumber.doubleOut(lt[3], DEC, sb);
  78. sb.append(' ');
  79. PDFNumber.doubleOut(lt[4], DEC, sb);
  80. sb.append(' ');
  81. PDFNumber.doubleOut(lt[5], DEC, sb);
  82. }
  83. private static void writeChar(int codePoint, StringBuffer sb, boolean multibyte, boolean cid) {
  84. if (!multibyte) {
  85. if (cid || codePoint < 32 || codePoint > 127) {
  86. sb.append("\\").append(Integer.toOctalString(codePoint));
  87. } else {
  88. switch (codePoint) {
  89. case '(':
  90. case ')':
  91. case '\\':
  92. sb.append('\\');
  93. break;
  94. default:
  95. }
  96. sb.appendCodePoint(codePoint);
  97. }
  98. } else {
  99. PDFText.toUnicodeHex(codePoint, sb);
  100. }
  101. }
  102. private void writeChar(int codePoint, StringBuffer sb) {
  103. writeChar(codePoint, sb, useMultiByte, useCid);
  104. }
  105. private void checkInTextObject() {
  106. if (!inTextObject) {
  107. throw new IllegalStateException("Not in text object");
  108. }
  109. }
  110. /**
  111. * Indicates whether we are in a text object or not.
  112. * @return true if we are in a text object
  113. */
  114. public boolean isInTextObject() {
  115. return inTextObject;
  116. }
  117. /**
  118. * Called when a new text object should be started. Be sure to call setFont() before
  119. * issuing any text painting commands.
  120. */
  121. public void beginTextObject() {
  122. if (inTextObject) {
  123. throw new IllegalStateException("Already in text object");
  124. }
  125. write("BT\n");
  126. this.inTextObject = true;
  127. }
  128. /**
  129. * Called when a text object should be ended.
  130. */
  131. public void endTextObject() {
  132. checkInTextObject();
  133. write("ET\n");
  134. this.inTextObject = false;
  135. initValues();
  136. }
  137. /**
  138. * Resets the state fields.
  139. */
  140. protected void initValues() {
  141. this.currentFontName = null;
  142. this.currentFontSize = 0.0;
  143. this.textRenderingMode = TR_FILL;
  144. }
  145. /**
  146. * Creates a "cm" command.
  147. * @param at the transformation matrix
  148. */
  149. public void concatMatrix(AffineTransform at) {
  150. if (!at.isIdentity()) {
  151. writeTJ();
  152. StringBuffer sb = new StringBuffer();
  153. writeAffineTransform(at, sb);
  154. sb.append(" cm\n");
  155. write(sb);
  156. }
  157. }
  158. /**
  159. * Writes a "Tf" command, setting a new current font.
  160. * @param fontName the name of the font to select
  161. * @param fontSize the font size (in points)
  162. */
  163. public void writeTf(String fontName, double fontSize) {
  164. checkInTextObject();
  165. StringBuffer sb = new StringBuffer();
  166. sb.append('/');
  167. sb.append(fontName);
  168. sb.append(' ');
  169. PDFNumber.doubleOut(fontSize, 6, sb);
  170. sb.append(" Tf\n");
  171. write(sb);
  172. this.startText = useMultiByte ? "<" : "(";
  173. this.endText = useMultiByte ? ">" : ")";
  174. }
  175. /**
  176. * Updates the current font. This method only writes a "Tf" if the current font changes.
  177. * @param fontName the name of the font to select
  178. * @param fontSize the font size (in points)
  179. * @param multiByte true indicates the font is a multi-byte font, false means single-byte
  180. */
  181. public void updateTf(String fontName, double fontSize, boolean multiByte, boolean cid) {
  182. checkInTextObject();
  183. if (!fontName.equals(this.currentFontName) || (fontSize != this.currentFontSize)) {
  184. writeTJ();
  185. this.currentFontName = fontName;
  186. this.currentFontSize = fontSize;
  187. this.useMultiByte = multiByte;
  188. this.useCid = cid;
  189. writeTf(fontName, fontSize);
  190. }
  191. }
  192. /**
  193. * Sets the text rendering mode.
  194. * @param mode the rendering mode (value 0 to 7, see PDF Spec, constants: TR_*)
  195. */
  196. public void setTextRenderingMode(int mode) {
  197. if (mode < 0 || mode > 7) {
  198. throw new IllegalArgumentException(
  199. "Illegal value for text rendering mode. Expected: 0-7");
  200. }
  201. if (mode != this.textRenderingMode) {
  202. writeTJ();
  203. this.textRenderingMode = mode;
  204. write(this.textRenderingMode + " Tr\n");
  205. }
  206. }
  207. /**
  208. * Sets the text rendering mode.
  209. * @param fill true if the text should be filled
  210. * @param stroke true if the text should be stroked
  211. * @param addToClip true if the path should be added for clipping
  212. */
  213. public void setTextRenderingMode(boolean fill, boolean stroke, boolean addToClip) {
  214. int mode;
  215. if (fill) {
  216. mode = (stroke ? 2 : 0);
  217. } else {
  218. mode = (stroke ? 1 : 3);
  219. }
  220. if (addToClip) {
  221. mode += 4;
  222. }
  223. setTextRenderingMode(mode);
  224. }
  225. /**
  226. * Writes a "Tm" command, setting a new text transformation matrix.
  227. * @param localTransform the new text transformation matrix
  228. */
  229. public void writeTextMatrix(AffineTransform localTransform) {
  230. StringBuffer sb = new StringBuffer();
  231. writeAffineTransform(localTransform, sb);
  232. sb.append(" Tm ");
  233. write(sb);
  234. }
  235. /**
  236. * Writes a char to the "TJ-Buffer".
  237. * @param ch the mapped character (code point/character code)
  238. */
  239. public void writeTJMappedChar(char ch) {
  240. writeTJMappedCodePoint((int) ch);
  241. }
  242. /**
  243. * Writes a codepoint to the "TJ-Buffer".
  244. * @param codePoint the mapped character (code point/character code)
  245. */
  246. public void writeTJMappedCodePoint(int codePoint) {
  247. if (bufTJ == null) {
  248. bufTJ = new StringBuffer();
  249. }
  250. if (bufTJ.length() == 0) {
  251. bufTJ.append('[');
  252. bufTJ.append(startText);
  253. }
  254. writeChar(codePoint, bufTJ);
  255. }
  256. /**
  257. * Writes a glyph adjust value to the "TJ-Buffer".
  258. * <p>Assumes the following:</p>
  259. * <ol>
  260. * <li>if buffer is currently empty, then this is the start of the array object
  261. * that encodes the adjustment and character values, and, therfore, a LEFT
  262. * SQUARE BRACKET '[' must be prepended; and
  263. * </li>
  264. * <li>otherwise (the buffer is
  265. * not empty), then the last element written to the buffer was a mapped
  266. * character, and, therefore, a terminating '&gt;' or ')' followed by a space
  267. * must be appended to the buffer prior to appending the adjustment value.
  268. * </li>
  269. * </ol>
  270. * @param adjust the glyph adjust value in thousands of text unit space.
  271. */
  272. public void adjustGlyphTJ(double adjust) {
  273. if (bufTJ == null) {
  274. bufTJ = new StringBuffer();
  275. }
  276. if (bufTJ.length() == 0) {
  277. bufTJ.append('[');
  278. } else {
  279. bufTJ.append(endText);
  280. bufTJ.append(' ');
  281. }
  282. PDFNumber.doubleOut(adjust, DEC - 4, bufTJ);
  283. bufTJ.append(' ');
  284. bufTJ.append(startText);
  285. }
  286. /**
  287. * Writes a "TJ" command, writing out the accumulated buffer with the characters and glyph
  288. * positioning values. The buffer is reset afterwards.
  289. */
  290. public void writeTJ() {
  291. if (isInString()) {
  292. bufTJ.append(endText);
  293. bufTJ.append("] TJ\n");
  294. write(bufTJ);
  295. bufTJ.setLength(0);
  296. }
  297. }
  298. private boolean isInString() {
  299. return bufTJ != null && bufTJ.length() > 0;
  300. }
  301. /**
  302. * Writes a "Td" command with specified x and y coordinates.
  303. * @param x coordinate
  304. * @param y coordinate
  305. */
  306. public void writeTd(double x, double y) {
  307. StringBuffer sb = new StringBuffer();
  308. PDFNumber.doubleOut(x, DEC, sb);
  309. sb.append(' ');
  310. PDFNumber.doubleOut(y, DEC, sb);
  311. sb.append(" Td\n");
  312. write(sb);
  313. }
  314. /**
  315. * Writes a "Tj" command with specified character code.
  316. * @param ch character code to write
  317. */
  318. public void writeTj(char ch, boolean multibyte, boolean cid) {
  319. StringBuffer sb = new StringBuffer();
  320. sb.append(startText);
  321. writeChar(ch, sb, multibyte, cid);
  322. sb.append(endText);
  323. sb.append(" Tj\n");
  324. write(sb);
  325. }
  326. }