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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.afp.modca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.commons.io.output.ByteArrayOutputStream;
  22. import org.apache.fop.afp.ptoca.PtocaConstants;
  23. import org.apache.fop.afp.util.BinaryUtils;
  24. /**
  25. * Presentation text data contains the graphic characters and the control
  26. * sequences necessary to position the characters within the object space. The
  27. * data consists of: - graphic characters to be presented - control sequences
  28. * that position them - modal control sequences that adjust the positions by
  29. * small amounts - other functions causing text to be presented with differences
  30. * in appearance.
  31. * <p>
  32. * The graphic characters are expected to conform to a coded font representation
  33. * so that they can be translated from the code point in the object data to the
  34. * character in the coded font. The units of measure for linear displacements
  35. * are derived from the PresentationTextDescriptor or from the hierarchical
  36. * defaults.
  37. * <p>
  38. * In addition to graphic character code points, Presentation Text data can
  39. * contain embedded control sequences. These are strings of two or more bytes
  40. * which signal an alternate mode of processing for the content of the current
  41. * Presentation Text data.
  42. * <p>
  43. * The content for this object can be created using {@link org.apache.fop.afp.ptoca.PtocaBuilder}.
  44. */
  45. public class PresentationTextData extends AbstractAFPObject implements PtocaConstants {
  46. /** the maximum size of the presentation text data.*/
  47. private static final int MAX_SIZE = 8192;
  48. /** the AFP data relating to this presentation text data. */
  49. private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  50. /**
  51. * Default constructor for the PresentationTextData.
  52. */
  53. public PresentationTextData() {
  54. this(false);
  55. }
  56. private static final int HEADER_LENGTH = 9;
  57. /**
  58. * Constructor for the PresentationTextData, the boolean flag indicate
  59. * whether the control sequence prefix should be set to indicate the start
  60. * of a new control sequence.
  61. *
  62. * @param controlInd
  63. * The control sequence indicator.
  64. */
  65. public PresentationTextData(boolean controlInd) {
  66. final byte[] data = {
  67. 0x5A, // Structured field identifier
  68. 0x00, // Record length byte 1
  69. 0x00, // Record length byte 2
  70. SF_CLASS, // PresentationTextData identifier byte 1
  71. Type.DATA, // PresentationTextData identifier byte 2
  72. Category.PRESENTATION_TEXT, // PresentationTextData identifier byte 3
  73. 0x00, // Flag
  74. 0x00, // Reserved
  75. 0x00, // Reserved
  76. };
  77. baos.write(data, 0, HEADER_LENGTH);
  78. if (controlInd) {
  79. baos.write(new byte[] {0x2B, (byte) 0xD3}, 0, 2);
  80. }
  81. }
  82. /**
  83. * Returns the number of data bytes still available in this object until it is full and a new
  84. * one has to be started.
  85. * @return the number of data bytes available
  86. */
  87. public int getBytesAvailable() {
  88. return MAX_SIZE - baos.size() + HEADER_LENGTH;
  89. }
  90. /**
  91. * Returns the output stream the content data is written to.
  92. * @return the output stream
  93. */
  94. protected OutputStream getOutputStream() {
  95. return this.baos;
  96. }
  97. /** {@inheritDoc} */
  98. public void writeToStream(OutputStream os) throws IOException {
  99. assert getBytesAvailable() >= 0;
  100. byte[] data = baos.toByteArray();
  101. byte[] size = BinaryUtils.convert(data.length - 1, 2);
  102. data[1] = size[0];
  103. data[2] = size[1];
  104. os.write(data);
  105. }
  106. }