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.

PresentationTextData.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.render.afp.AFPFontColor;
  22. import org.apache.fop.render.afp.tools.BinaryUtils;
  23. /**
  24. * Presentation text data contains the graphic characters and the control
  25. * sequences necessary to position the characters within the object space. The
  26. * data consists of: - graphic characters to be presented - control sequences
  27. * that position them - modal control sequences that adjust the positions by
  28. * small amounts - other functions causing text to be presented with differences
  29. * in appearance.
  30. *
  31. * The graphic characters are expected to conform to a coded font representation
  32. * so that they can be translated from the code point in the object data to the
  33. * character in the coded font. The units of measure for linear displacements
  34. * are derived from the PresentationTextDescriptor or from the hierarchical
  35. * defaults.
  36. *
  37. * In addition to graphic character code points, Presentation Text data can
  38. * contain embedded control sequences. These are strings of two or more bytes
  39. * which signal an alternate mode of processing for the content of the current
  40. * Presentation Text data.
  41. *
  42. */
  43. public class PresentationTextData extends AbstractAFPObject {
  44. /**
  45. * The maximum size of the presentation text data.
  46. */
  47. private static final int MAX_SIZE = 8192;
  48. /**
  49. * The afp data relating to this presentaion text data.
  50. */
  51. private ByteArrayOutputStream _baos = new ByteArrayOutputStream(1024);
  52. /**
  53. * The current x coordinate.
  54. */
  55. private int _currentXCoordinate = -1;
  56. /**
  57. * The current y cooridnate
  58. */
  59. private int _currentYCoordinate = -1;
  60. /**
  61. * The current font
  62. */
  63. private String _currentFont = "";
  64. /**
  65. * The current orientation
  66. */
  67. private int _currentOrientation = 0;
  68. /**
  69. * The current color
  70. */
  71. private AFPFontColor _currentColor = new AFPFontColor(0, 0, 0);
  72. /**
  73. * The current variable space increment
  74. */
  75. private int _currentVariableSpaceCharacterIncrement = 0;
  76. /**
  77. * The current inter character adjustment
  78. */
  79. private int _currentInterCharacterAdjustment = 0;
  80. /**
  81. * Default constructor for the PresentationTextData.
  82. */
  83. public PresentationTextData() {
  84. this(false);
  85. }
  86. /**
  87. * Constructor for the PresentationTextData, the boolean flag indicate
  88. * whether the control sequence prefix should be set to indicate the start
  89. * of a new control sequence.
  90. *
  91. * @param controlInd
  92. * The control sequence indicator.
  93. */
  94. public PresentationTextData(boolean controlInd) {
  95. _baos.write(new byte[] { 0x5A, // Structured field identifier
  96. 0x00, // Record length byte 1
  97. 0x00, // Record length byte 2
  98. (byte) 0xD3, // PresentationTextData identifier byte 1
  99. (byte) 0xEE, // PresentationTextData identifier byte 2
  100. (byte) 0x9B, // PresentationTextData identifier byte 3
  101. 0x00, // Flag
  102. 0x00, // Reserved
  103. 0x00, // Reserved
  104. }, 0, 9);
  105. if (controlInd) {
  106. _baos.write(new byte[] { 0x2B, (byte) 0xD3 }, 0, 2);
  107. }
  108. }
  109. /**
  110. * The Set Coded Font Local control sequence activates a coded font and
  111. * specifies the character attributes to be used. This is a modal control
  112. * sequence.
  113. *
  114. * @param font
  115. * The font local identifier.
  116. * @param afpdata
  117. * The output stream to which data should be written.
  118. */
  119. private void setCodedFont(byte font, ByteArrayOutputStream afpdata) {
  120. // Avoid unnecessary specification of the font
  121. if (String.valueOf(font).equals(_currentFont)) {
  122. return;
  123. } else {
  124. _currentFont = String.valueOf(font);
  125. }
  126. afpdata.write(new byte[] { 0x03, (byte) 0xF1, font, }, 0, 3);
  127. }
  128. /**
  129. * Establishes the current presentation position on the baseline at a new
  130. * I-axis coordinate, which is a specified number of measurement units from
  131. * the B-axis. There is no change to the current B-axis coordinate.
  132. *
  133. * @param coordinate
  134. * The coordinate for the inline move.
  135. * @param afpdata
  136. * The output stream to which data should be written.
  137. */
  138. private void absoluteMoveInline(int coordinate,
  139. ByteArrayOutputStream afpdata) {
  140. byte[] b = BinaryUtils.convert(coordinate, 2);
  141. afpdata.write(new byte[] { 0x04, (byte) 0xC7, b[0], b[1], }, 0, 4);
  142. _currentXCoordinate = coordinate;
  143. }
  144. /**
  145. * Establishes the baseline and the current presentation position at a new
  146. * B-axis coordinate, which is a specified number of measurement units from
  147. * the I-axis. There is no change to the current I-axis coordinate.
  148. *
  149. * @param coordinate
  150. * The coordinate for the baseline move.
  151. * @param afpdata
  152. * The output stream to which data should be written.
  153. */
  154. private void absoluteMoveBaseline(int coordinate,
  155. ByteArrayOutputStream afpdata) {
  156. byte[] b = BinaryUtils.convert(coordinate, 2);
  157. afpdata.write(new byte[] { 0x04, (byte) 0xD3, b[0], b[1], }, 0, 4);
  158. _currentYCoordinate = coordinate;
  159. }
  160. /**
  161. * The Transparent Data control sequence contains a sequence of code points
  162. * that are presented without a scan for embedded control sequences.
  163. *
  164. * @param data
  165. * The text data to add.
  166. * @param afpdata
  167. * The output stream to which data should be written.
  168. */
  169. private void addTransparentData(byte[] data, ByteArrayOutputStream afpdata) {
  170. // Calculate the length
  171. int l = data.length + 2;
  172. if (l > 255) {
  173. // Check that we are not exceeding the maximum length
  174. throw new IllegalArgumentException(
  175. "Transparent data is longer than 253 bytes: " + data);
  176. }
  177. afpdata.write(new byte[] { BinaryUtils.convert(l)[0], (byte) 0xDB, },
  178. 0, 2);
  179. afpdata.write(data, 0, data.length);
  180. }
  181. /**
  182. * Draws a line of specified length and specified width in the B-direction
  183. * from the current presentation position. The location of the current
  184. * presentation position is unchanged.
  185. *
  186. * @param length
  187. * The length of the rule.
  188. * @param width
  189. * The width of the rule.
  190. * @param afpdata
  191. * The output stream to which data should be written.
  192. */
  193. private void drawBaxisRule(int length, int width,
  194. ByteArrayOutputStream afpdata) {
  195. afpdata.write(new byte[] { 0x07, // Length
  196. (byte) 0xE7, // Type
  197. }, 0, 2);
  198. // Rule length
  199. byte[] data1 = BinaryUtils.shortToByteArray((short) length);
  200. afpdata.write(data1, 0, data1.length);
  201. // Rule width
  202. byte[] data2 = BinaryUtils.shortToByteArray((short) width);
  203. afpdata.write(data2, 0, data2.length);
  204. // Rule width fraction
  205. afpdata.write(0x00);
  206. }
  207. /**
  208. * Draws a line of specified length and specified width in the I-direction
  209. * from the current presentation position. The location of the current
  210. * presentation position is unchanged.
  211. *
  212. * @param length
  213. * The length of the rule.
  214. * @param width
  215. * The width of the rule.
  216. * @param afpdata
  217. * The output stream to which data should be written.
  218. */
  219. private void drawIaxisRule(int length, int width,
  220. ByteArrayOutputStream afpdata) {
  221. afpdata.write(new byte[] { 0x07, // Length
  222. (byte) 0xE5, // Type
  223. }, 0, 2);
  224. // Rule length
  225. byte[] data1 = BinaryUtils.shortToByteArray((short) length);
  226. afpdata.write(data1, 0, data1.length);
  227. // Rule width
  228. byte[] data2 = BinaryUtils.shortToByteArray((short) width);
  229. afpdata.write(data2, 0, data2.length);
  230. // Rule width fraction
  231. afpdata.write(0x00);
  232. }
  233. /**
  234. * Create the presentation text data for the byte array of data.
  235. *
  236. * @param fontNumber
  237. * The font resource identifier.
  238. * @param x
  239. * The x coordinate for the text data.
  240. * @param y
  241. * The y coordinate for the text data.
  242. * @param orientation
  243. * The orientation of the text data.
  244. * @param col
  245. * The text color.
  246. * @param vsci
  247. * The variable space character increment.
  248. * @param ica
  249. * The inter character adjustment.
  250. * @param data
  251. * The text data to be created.
  252. * @throws MaximumSizeExceededException
  253. */
  254. public void createTextData(int fontNumber, int x, int y, int orientation,
  255. AFPFontColor col, int vsci, int ica, byte[] data)
  256. throws MaximumSizeExceededException {
  257. ByteArrayOutputStream afpdata = new ByteArrayOutputStream();
  258. if (_currentOrientation != orientation) {
  259. setTextOrientation(orientation, afpdata);
  260. _currentOrientation = orientation;
  261. _currentXCoordinate = -1;
  262. _currentYCoordinate = -1;
  263. }
  264. // Avoid unnecessary specification of the Y co-ordinate
  265. if (y != _currentYCoordinate) {
  266. absoluteMoveBaseline(y, afpdata);
  267. _currentXCoordinate = -1;
  268. }
  269. // Avoid unnecessary specification of the X co-ordinate
  270. if (x != _currentXCoordinate) {
  271. absoluteMoveInline(x, afpdata);
  272. }
  273. // Avoid unnecessary specification of the variable space increment
  274. if (vsci != _currentVariableSpaceCharacterIncrement) {
  275. setVariableSpaceCharacterIncrement(vsci, afpdata);
  276. _currentVariableSpaceCharacterIncrement = vsci;
  277. }
  278. // Avoid unnecessary specification of the inter character adjustment
  279. if (ica != _currentInterCharacterAdjustment) {
  280. setInterCharacterAdjustment(ica, afpdata);
  281. _currentInterCharacterAdjustment = ica;
  282. }
  283. // Avoid unnecessary specification of the text color
  284. if (!col.equals(_currentColor)) {
  285. setExtendedTextColor(col, afpdata);
  286. _currentColor.setTo(col);
  287. }
  288. setCodedFont(BinaryUtils.convert(fontNumber)[0], afpdata);
  289. addTransparentData(data, afpdata);
  290. _currentXCoordinate = -1;
  291. int s = afpdata.size();
  292. if (_baos.size() + s > MAX_SIZE) {
  293. _currentXCoordinate = -1;
  294. _currentYCoordinate = -1;
  295. throw new MaximumSizeExceededException();
  296. }
  297. byte[] outputdata = afpdata.toByteArray();
  298. _baos.write(outputdata, 0, outputdata.length);
  299. }
  300. /**
  301. * Drawing of lines using the starting and ending coordinates, thickness and
  302. * colour arguments.
  303. *
  304. * @param x1
  305. * The starting X coordinate.
  306. * @param y1
  307. * The starting Y coordinate.
  308. * @param x2
  309. * The ending X coordinate.
  310. * @param y2
  311. * The ending Y coordinate.
  312. * @param thickness
  313. * The line thickness.
  314. * @param orientation
  315. * The orientation of the text data.
  316. * @param col
  317. * The text color.
  318. */
  319. public void createLineData(int x1, int y1, int x2, int y2, int thickness,
  320. int orientation, AFPFontColor col) throws MaximumSizeExceededException {
  321. ByteArrayOutputStream afpdata = new ByteArrayOutputStream();
  322. if (_currentOrientation != orientation) {
  323. setTextOrientation(orientation, afpdata);
  324. _currentOrientation = orientation;
  325. }
  326. // Avoid unnecessary specification of the Y coordinate
  327. if (y1 != _currentYCoordinate) {
  328. absoluteMoveBaseline(y1, afpdata);
  329. }
  330. // Avoid unnecessary specification of the X coordinate
  331. if (x1 != _currentXCoordinate) {
  332. absoluteMoveInline(x1, afpdata);
  333. }
  334. if (!col.equals(_currentColor)) {
  335. setExtendedTextColor(col, afpdata);
  336. _currentColor.setTo(col);
  337. }
  338. if (y1 == y2) {
  339. drawIaxisRule(x2 - x1, thickness, afpdata);
  340. } else if (x1 == x2) {
  341. drawBaxisRule(y2 - y1, thickness, afpdata);
  342. } else {
  343. return;
  344. }
  345. int s = afpdata.size();
  346. if (_baos.size() + s > MAX_SIZE) {
  347. _currentXCoordinate = -1;
  348. _currentYCoordinate = -1;
  349. throw new MaximumSizeExceededException();
  350. }
  351. byte[] outputdata = afpdata.toByteArray();
  352. _baos.write(outputdata, 0, outputdata.length);
  353. }
  354. /**
  355. * The Set Text Orientation control sequence establishes the I-direction and
  356. * B-direction for the subsequent text. This is a modal control sequence.
  357. *
  358. * Semantics: This control sequence specifies the I-axis and B-axis
  359. * orientations with respect to the Xp-axis for the current Presentation
  360. * Text object. The orientations are rotational values expressed in degrees
  361. * and minutes.
  362. *
  363. * @param orientation
  364. * The text orientation (0,90, 180, 270).
  365. * @param afpdata
  366. * The output stream to which data should be written.
  367. */
  368. private void setTextOrientation(int orientation,
  369. ByteArrayOutputStream afpdata) {
  370. afpdata.write(new byte[] { 0x06, (byte) 0xF7, }, 0, 2);
  371. switch (orientation) {
  372. case 90:
  373. afpdata.write(0x2D);
  374. afpdata.write(0x00);
  375. afpdata.write(0x5A);
  376. afpdata.write(0x00);
  377. break;
  378. case 180:
  379. afpdata.write(0x5A);
  380. afpdata.write(0x00);
  381. afpdata.write(0x87);
  382. afpdata.write(0x00);
  383. break;
  384. case 270:
  385. afpdata.write(0x87);
  386. afpdata.write(0x00);
  387. afpdata.write(0x00);
  388. afpdata.write(0x00);
  389. break;
  390. default:
  391. afpdata.write(0x00);
  392. afpdata.write(0x00);
  393. afpdata.write(0x2D);
  394. afpdata.write(0x00);
  395. break;
  396. }
  397. }
  398. /**
  399. * The Set Extended Text Color control sequence specifies a color value and
  400. * defines the color space and encoding for that value. The specified color
  401. * value is applied to foreground areas of the text presentation space.
  402. * This is a modal control sequence.
  403. *
  404. * @param col
  405. * The color to be set.
  406. * @param afpdata
  407. * The output stream to which data should be written.
  408. */
  409. private void setExtendedTextColor(AFPFontColor col,
  410. ByteArrayOutputStream afpdata) {
  411. afpdata.write(new byte[] {
  412. 15 // Control sequence length
  413. , (byte)0x81 // Control sequence function type
  414. , 0x00 // Reserved; must be zero
  415. , 0x01 // Color space - 0x01 = RGB
  416. , 0x00 // Reserved; must be zero
  417. , 0x00 // Reserved; must be zero
  418. , 0x00 // Reserved; must be zero
  419. , 0x00 // Reserved; must be zero
  420. , 8 // Number of bits in component 1
  421. , 8 // Number of bits in component 2
  422. , 8 // Number of bits in component 3
  423. , 0 // Number of bits in component 4
  424. , (byte)(col.getRed()) // Red intensity
  425. , (byte)(col.getGreen()) // Green intensity
  426. , (byte)(col.getBlue()) // Blue intensity
  427. }, 0, 15);
  428. }
  429. /**
  430. * //TODO
  431. * This is a modal control sequence.
  432. *
  433. * @param incr
  434. * The increment to be set.
  435. * @param afpdata
  436. * The output stream to which data should be written.
  437. */
  438. private void setVariableSpaceCharacterIncrement(int incr,
  439. ByteArrayOutputStream afpdata) {
  440. byte[] b = BinaryUtils.convert(incr, 2);
  441. afpdata.write(new byte[] {
  442. 4 // Control sequence length
  443. , (byte)0xC5 // Control sequence function type
  444. , b[0]
  445. , b[1]
  446. }, 0, 4);
  447. }
  448. /**
  449. * //TODO
  450. * This is a modal control sequence.
  451. *
  452. * @param incr
  453. * The increment to be set.
  454. * @param afpdata
  455. * The output stream to which data should be written.
  456. */
  457. private void setInterCharacterAdjustment(int incr,
  458. ByteArrayOutputStream afpdata) {
  459. byte[] b = BinaryUtils.convert(Math.abs(incr), 2);
  460. afpdata.write(new byte[] {
  461. 5 // Control sequence length
  462. , (byte)0xC3 // Control sequence function type
  463. , b[0]
  464. , b[1]
  465. , (byte)(incr >= 0 ? 0 : 1) // Direction
  466. }, 0, 5);
  467. }
  468. /**
  469. * Accessor method to write the AFP datastream for
  470. * the text data.
  471. * @param os The stream to write to
  472. * @throws java.io.IOException
  473. */
  474. public void writeDataStream(OutputStream os)
  475. throws IOException {
  476. byte[] data = _baos.toByteArray();
  477. byte[] size = BinaryUtils.convert(data.length - 1, 2);
  478. data[1] = size[0];
  479. data[2] = size[1];
  480. os.write(data);
  481. }
  482. /**
  483. * A control sequence is a sequence of bytes that specifies a control
  484. * function. A control sequence consists of a control sequence introducer
  485. * and zero or more parameters. The control sequence can extend multiple
  486. * presentation text data objects, but must eventually be terminated. This
  487. * method terminates the control sequence.
  488. *
  489. * @throws MaximumSizeExceededException
  490. */
  491. public void endControlSequence() throws MaximumSizeExceededException {
  492. byte[] data = new byte[2];
  493. data[0] = 0x02;
  494. data[1] = (byte) 0xF8;
  495. if (data.length + _baos.size() > MAX_SIZE) {
  496. throw new MaximumSizeExceededException();
  497. }
  498. _baos.write(data, 0, data.length);
  499. }
  500. }