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.

PresentationTextObject.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.modca;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import org.apache.fop.render.afp.AFPFontColor;
  23. /**
  24. * The Presentation Text object is the data object used in document processing
  25. * environments for representing text which has been prepared for presentation.
  26. * Text, as used here, means an ordered string of characters, such as graphic
  27. * symbols, numbers, and letters, that are suitable for the specific purpose of
  28. * representing coherent information. Text which has been prepared for
  29. * presentation has been reduced to a primitive form through explicit
  30. * specification of the characters and their placement in the presentation
  31. * space. Control sequences which designate specific control functions may be
  32. * embedded within the text. These functions extend the primitive form by
  33. * applying specific characteristics to the text when it is presented. The
  34. * collection of the graphic characters and control codes is called Presentation
  35. * Text, and the object that contains the Presentation Text is called the
  36. * PresentationText object.
  37. *
  38. */
  39. public class PresentationTextObject extends AbstractNamedAFPObject {
  40. /**
  41. * Default name for the presentation text object
  42. */
  43. private static final String DEFAULT_NAME = "PTO00001";
  44. private PresentationTextData currentPresentationTextData = null;
  45. private ArrayList presentationTextData = new ArrayList();
  46. /**
  47. * Default constructor for the PresentationTextObject
  48. */
  49. public PresentationTextObject() {
  50. this(DEFAULT_NAME);
  51. }
  52. /**
  53. * Construct a new PresentationTextObject for the specified name argument,
  54. * the name should be an 8 character identifier.
  55. */
  56. public PresentationTextObject(String name) {
  57. super(name);
  58. }
  59. /**
  60. * Create the presentation text data for the byte array of data.
  61. *
  62. * @param fontNumber
  63. * The font resource identifier.
  64. * @param x
  65. * The x coordinate for the text data.
  66. * @param y
  67. * The y coordinate for the text data.
  68. * @param col
  69. * The text color.
  70. * @param vsci
  71. * The variable space character increment.
  72. * @param ica
  73. * The inter character increment.
  74. * @param data
  75. * The text data to be created.
  76. */
  77. public void createTextData(int fontNumber, int x, int y, AFPFontColor col, int vsci, int ica, byte[] data) {
  78. // Use a default orientation of zero
  79. createTextData(fontNumber, x, y, 0, col, vsci, ica, data);
  80. }
  81. /**
  82. * Create the presentation text data for the byte array of data.
  83. *
  84. * @param fontNumber
  85. * The font resource identifier.
  86. * @param x
  87. * The x coordinate for the text data.
  88. * @param y
  89. * The y coordinate for the text data.
  90. * @param orientation
  91. * The orientation of the text data.
  92. * @param col
  93. * The text color.
  94. * @param vsci
  95. * The variable space character increment.
  96. * @param ica
  97. * The inter character adjustment.
  98. * @param data
  99. * The text data to be created.
  100. */
  101. public void createTextData(int fontNumber, int x, int y, int orientation,
  102. AFPFontColor col, int vsci, int ica, byte[] data) {
  103. if (currentPresentationTextData == null) {
  104. startPresentationTextData();
  105. }
  106. try {
  107. currentPresentationTextData.createTextData(fontNumber, x, y,
  108. orientation, col, vsci, ica, data);
  109. } catch (MaximumSizeExceededException msee) {
  110. endPresentationTextData();
  111. createTextData(fontNumber, x, y, orientation, col, vsci, ica, data);
  112. }
  113. }
  114. /**
  115. * Drawing of lines using the starting and ending coordinates, thickness.
  116. *
  117. * @param x1
  118. * The first x coordinate of the line.
  119. * @param y1
  120. * The first y coordinate of the line.
  121. * @param x2
  122. * The second x coordinate of the line.
  123. * @param y2
  124. * The second y coordinate of the line.
  125. * @param thickness
  126. * The thickness of the line.
  127. * @param col
  128. * The text color.
  129. */
  130. public void createLineData(int x1, int y1, int x2, int y2, int thickness, AFPFontColor col) {
  131. // Default orientation
  132. createLineData(x1, y1, x2, y2, thickness, 0, col);
  133. }
  134. /**
  135. * Drawing of lines using the starting and ending coordinates, thickness and
  136. * orientation arguments.
  137. *
  138. * @param x1
  139. * The first x coordinate of the line.
  140. * @param y1
  141. * The first y coordinate of the line.
  142. * @param x2
  143. * The second x coordinate of the line.
  144. * @param y2
  145. * The second y coordinate of the line.
  146. * @param thickness
  147. * The thickness of the line.
  148. * @param orientation
  149. * The orientation of the line.
  150. * @param col
  151. * The text color.
  152. */
  153. public void createLineData(int x1, int y1, int x2, int y2, int thickness,
  154. int orientation, AFPFontColor col) {
  155. if (currentPresentationTextData == null) {
  156. startPresentationTextData();
  157. }
  158. try {
  159. currentPresentationTextData.createLineData(x1, y1, x2, y2,
  160. thickness, orientation, col);
  161. } catch (MaximumSizeExceededException msee) {
  162. endPresentationTextData();
  163. createLineData(x1, y1, x2, y2, thickness, orientation, col);
  164. }
  165. }
  166. /**
  167. * Helper method to mark the start of the presentation text data
  168. */
  169. private void startPresentationTextData() {
  170. if (presentationTextData.size() == 0) {
  171. currentPresentationTextData = new PresentationTextData(true);
  172. } else {
  173. currentPresentationTextData = new PresentationTextData();
  174. }
  175. presentationTextData.add(currentPresentationTextData);
  176. }
  177. /**
  178. * Helper method to mark the end of the presentation text data
  179. */
  180. private void endPresentationTextData() {
  181. currentPresentationTextData = null;
  182. }
  183. /**
  184. * Accessor method to write the AFP datastream for the PresentationTextObject.
  185. * @param os The stream to write to
  186. * @throws java.io.IOException
  187. */
  188. public void writeDataStream(OutputStream os)
  189. throws IOException {
  190. writeStart(os);
  191. writeObjectList(presentationTextData, os);
  192. writeEnd(os);
  193. }
  194. public String getName() {
  195. return _name;
  196. }
  197. /**
  198. * Helper method to write the start of the presenation text object.
  199. * @param os The stream to write to
  200. */
  201. private void writeStart(OutputStream os)
  202. throws IOException {
  203. byte[] data = new byte[17];
  204. data[0] = 0x5A; // Structured field identifier
  205. data[1] = 0x00; // Length byte 1
  206. data[2] = 0x10; // Length byte 2
  207. data[3] = (byte) 0xD3; // Structured field id byte 1
  208. data[4] = (byte) 0xA8; // Structured field id byte 2
  209. data[5] = (byte) 0x9B; // Structured field id byte 3
  210. data[6] = 0x00; // Flags
  211. data[7] = 0x00; // Reserved
  212. data[8] = 0x00; // Reserved
  213. for (int i = 0; i < _nameBytes.length; i++) {
  214. data[9 + i] = _nameBytes[i];
  215. }
  216. os.write(data);
  217. }
  218. /**
  219. * Helper method to write the end of the presenation text object.
  220. * @param os The stream to write to
  221. */
  222. private void writeEnd(OutputStream os)
  223. throws IOException {
  224. byte[] data = new byte[17];
  225. data[0] = 0x5A; // Structured field identifier
  226. data[1] = 0x00; // Length byte 1
  227. data[2] = 0x10; // Length byte 2
  228. data[3] = (byte) 0xD3; // Structured field id byte 1
  229. data[4] = (byte) 0xA9; // Structured field id byte 2
  230. data[5] = (byte) 0x9B; // Structured field id byte 3
  231. data[6] = 0x00; // Flags
  232. data[7] = 0x00; // Reserved
  233. data[8] = 0x00; // Reserved
  234. for (int i = 0; i < _nameBytes.length; i++) {
  235. data[9 + i] = _nameBytes[i];
  236. }
  237. os.write(data);
  238. }
  239. /**
  240. * A control sequence is a sequence of bytes that specifies a control
  241. * function. A control sequence consists of a control sequence introducer
  242. * and zero or more parameters. The control sequence can extend multiple
  243. * presentation text data objects, but must eventually be terminated. This
  244. * method terminates the control sequence.
  245. */
  246. public void endControlSequence() {
  247. if (currentPresentationTextData == null) {
  248. startPresentationTextData();
  249. }
  250. try {
  251. currentPresentationTextData.endControlSequence();
  252. } catch (MaximumSizeExceededException msee) {
  253. endPresentationTextData();
  254. endControlSequence();
  255. }
  256. }
  257. }