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

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