Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PDFTextUtil.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 = false;
  44. private String startText;
  45. private String endText;
  46. private boolean useMultiByte;
  47. private StringBuffer bufTJ;
  48. private int textRenderingMode = TR_FILL;
  49. private String currentFontName;
  50. private double currentFontSize;
  51. /**
  52. * Main constructor.
  53. */
  54. public PDFTextUtil() {
  55. //nop
  56. }
  57. /**
  58. * Writes PDF code.
  59. * @param code the PDF code to write
  60. */
  61. protected abstract void write(String code);
  62. private void writeAffineTransform(AffineTransform at, StringBuffer sb) {
  63. double[] lt = new double[6];
  64. at.getMatrix(lt);
  65. sb.append(PDFNumber.doubleOut(lt[0], DEC)).append(" ");
  66. sb.append(PDFNumber.doubleOut(lt[1], DEC)).append(" ");
  67. sb.append(PDFNumber.doubleOut(lt[2], DEC)).append(" ");
  68. sb.append(PDFNumber.doubleOut(lt[3], DEC)).append(" ");
  69. sb.append(PDFNumber.doubleOut(lt[4], DEC)).append(" ");
  70. sb.append(PDFNumber.doubleOut(lt[5], DEC));
  71. }
  72. private void writeChar(char ch, StringBuffer sb) {
  73. if (!useMultiByte) {
  74. if (ch < 32 || ch > 127) {
  75. sb.append("\\").append(Integer.toOctalString(ch));
  76. } else {
  77. switch (ch) {
  78. case '(':
  79. case ')':
  80. case '\\':
  81. sb.append("\\");
  82. break;
  83. default:
  84. }
  85. sb.append(ch);
  86. }
  87. } else {
  88. sb.append(PDFText.toUnicodeHex(ch));
  89. }
  90. }
  91. private void checkInTextObject() {
  92. if (!inTextObject) {
  93. throw new IllegalStateException("Not in text object");
  94. }
  95. }
  96. /**
  97. * Indicates whether we are in a text object or not.
  98. * @return true if we are in a text object
  99. */
  100. public boolean isInTextObject() {
  101. return inTextObject;
  102. }
  103. /**
  104. * Called when a new text object should be started. Be sure to call setFont() before
  105. * issuing any text painting commands.
  106. */
  107. public void beginTextObject() {
  108. if (inTextObject) {
  109. throw new IllegalStateException("Already in text object");
  110. }
  111. write("BT\n");
  112. this.inTextObject = true;
  113. }
  114. /**
  115. * Called when a text object should be ended.
  116. */
  117. public void endTextObject() {
  118. checkInTextObject();
  119. write("ET\n");
  120. this.inTextObject = false;
  121. initValues();
  122. }
  123. /**
  124. * Resets the state fields.
  125. */
  126. protected void initValues() {
  127. this.currentFontName = null;
  128. this.currentFontSize = 0.0;
  129. this.textRenderingMode = TR_FILL;
  130. }
  131. /**
  132. * Creates a "cm" command.
  133. * @param at the transformation matrix
  134. */
  135. public void concatMatrix(AffineTransform at) {
  136. if (!at.isIdentity()) {
  137. writeTJ();
  138. StringBuffer sb = new StringBuffer();
  139. writeAffineTransform(at, sb);
  140. sb.append(" cm\n");
  141. write(sb.toString());
  142. }
  143. }
  144. /**
  145. * Writes a "Tf" command, setting a new current font.
  146. * @param fontName the name of the font to select
  147. * @param fontSize the font size (in points)
  148. */
  149. public void writeTf(String fontName, double fontSize) {
  150. checkInTextObject();
  151. write("/" + fontName + " " + PDFNumber.doubleOut(fontSize) + " Tf\n");
  152. this.startText = useMultiByte ? "<" : "(";
  153. this.endText = useMultiByte ? ">" : ")";
  154. }
  155. /**
  156. * Updates the current font. This method only writes a "Tf" if the current font changes.
  157. * @param fontName the name of the font to select
  158. * @param fontSize the font size (in points)
  159. * @param multiByte true indicates the font is a multi-byte font, false means single-byte
  160. */
  161. public void updateTf(String fontName, double fontSize, boolean multiByte) {
  162. checkInTextObject();
  163. if (!fontName.equals(this.currentFontName) || (fontSize != this.currentFontSize)) {
  164. writeTJ();
  165. this.currentFontName = fontName;
  166. this.currentFontSize = fontSize;
  167. this.useMultiByte = multiByte;
  168. writeTf(fontName, fontSize);
  169. }
  170. }
  171. /**
  172. * Sets the text rendering mode.
  173. * @param mode the rendering mode (value 0 to 7, see PDF Spec, constants: TR_*)
  174. */
  175. public void setTextRenderingMode(int mode) {
  176. if (mode < 0 || mode > 7) {
  177. throw new IllegalArgumentException(
  178. "Illegal value for text rendering mode. Expected: 0-7");
  179. }
  180. if (mode != this.textRenderingMode) {
  181. writeTJ();
  182. this.textRenderingMode = mode;
  183. write(this.textRenderingMode + " Tr\n");
  184. }
  185. }
  186. /**
  187. * Sets the text rendering mode.
  188. * @param fill true if the text should be filled
  189. * @param stroke true if the text should be stroked
  190. * @param addToClip true if the path should be added for clipping
  191. */
  192. public void setTextRenderingMode(boolean fill, boolean stroke, boolean addToClip) {
  193. int mode;
  194. if (fill) {
  195. mode = (stroke ? 2 : 0);
  196. } else {
  197. mode = (stroke ? 1 : 3);
  198. }
  199. if (addToClip) {
  200. mode += 4;
  201. }
  202. setTextRenderingMode(mode);
  203. }
  204. /**
  205. * Writes a "Tm" command, setting a new text transformation matrix.
  206. * @param localTransform the new text transformation matrix
  207. */
  208. public void writeTextMatrix(AffineTransform localTransform) {
  209. StringBuffer sb = new StringBuffer();
  210. writeAffineTransform(localTransform, sb);
  211. sb.append(" Tm ");
  212. write(sb.toString());
  213. }
  214. /**
  215. * Writes a char to the "TJ-Buffer".
  216. * @param codepoint the mapped character (code point/character code)
  217. */
  218. public void writeTJMappedChar(char codepoint) {
  219. if (bufTJ == null) {
  220. bufTJ = new StringBuffer();
  221. }
  222. if (bufTJ.length() == 0) {
  223. bufTJ.append("[").append(startText);
  224. }
  225. writeChar(codepoint, bufTJ);
  226. }
  227. /**
  228. * Writes a glyph adjust value to the "TJ-Buffer".
  229. * @param adjust the glyph adjust value in thousands of text unit space.
  230. */
  231. public void adjustGlyphTJ(double adjust) {
  232. if (bufTJ == null) {
  233. bufTJ = new StringBuffer();
  234. }
  235. if (bufTJ.length() > 0) {
  236. bufTJ.append(endText).append(" ");
  237. }
  238. if (bufTJ.length() == 0) {
  239. bufTJ.append("[");
  240. }
  241. bufTJ.append(PDFNumber.doubleOut(adjust, DEC - 4));
  242. bufTJ.append(" ");
  243. bufTJ.append(startText);
  244. }
  245. /**
  246. * Writes a "TJ" command, writing out the accumulated buffer with the characters and glyph
  247. * positioning values. The buffer is reset afterwards.
  248. */
  249. public void writeTJ() {
  250. if (isInString()) {
  251. bufTJ.append(endText).append("] TJ\n");
  252. write(bufTJ.toString());
  253. bufTJ.setLength(0);
  254. }
  255. }
  256. private boolean isInString() {
  257. return bufTJ != null && bufTJ.length() > 0;
  258. }
  259. }