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.

GraphicsChainedSegment.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.render.afp.modca.goca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.render.afp.modca.PreparedAFPObject;
  22. import org.apache.fop.render.afp.tools.BinaryUtils;
  23. /**
  24. * A GOCA graphics segment
  25. */
  26. public final class GraphicsChainedSegment extends AbstractGraphicsContainer {
  27. /**
  28. * The maximum segment data length
  29. */
  30. protected static final int MAX_DATA_LEN = 8192;
  31. /** the current area */
  32. private GraphicsArea currentArea = null;
  33. /** the previous segment in the chain */
  34. private GraphicsChainedSegment previous = null;
  35. /** the next segment in the chain */
  36. private GraphicsChainedSegment next = null;
  37. /**
  38. * Main constructor
  39. *
  40. * @param name
  41. * the name of this graphics segment
  42. */
  43. public GraphicsChainedSegment(String name) {
  44. super(name);
  45. }
  46. /**
  47. * Constructor
  48. *
  49. * @param name
  50. * the name of this graphics segment
  51. * @param previous
  52. * the previous graphics segment in this chain
  53. */
  54. public GraphicsChainedSegment(String name, GraphicsChainedSegment previous) {
  55. super(name);
  56. previous.next = this;
  57. this.previous = previous;
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public int getDataLength() {
  63. int dataLen = 14 + super.getDataLength();
  64. if (previous == null) {
  65. GraphicsChainedSegment current = this.next;
  66. while (current != null) {
  67. dataLen += current.getDataLength();
  68. current = current.next;
  69. }
  70. }
  71. return dataLen;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. protected int getNameLength() {
  77. return 4;
  78. }
  79. private static final byte APPEND_NEW_SEGMENT = 0;
  80. private static final byte PROLOG = 4;
  81. private static final byte APPEND_TO_EXISING = 48;
  82. /**
  83. * {@inheritDoc}
  84. */
  85. protected void writeStart(OutputStream os) throws IOException {
  86. super.writeStart(os);
  87. int len = super.getDataLength();
  88. byte[] segLen = BinaryUtils.convert(len, 2);
  89. byte[] data = new byte[] {
  90. 0x70, // BEGIN_SEGMENT
  91. 0x0C, // Length of following parameters
  92. this.nameBytes[0],
  93. this.nameBytes[1],
  94. this.nameBytes[2],
  95. this.nameBytes[3],
  96. 0x00, // FLAG1 (ignored)
  97. APPEND_NEW_SEGMENT,
  98. segLen[0], // SEGL
  99. segLen[1],
  100. 0x00,
  101. 0x00,
  102. 0x00,
  103. 0x00
  104. };
  105. // P/S NAME (predecessor name)
  106. if (previous != null) {
  107. data[10] = previous.nameBytes[0];
  108. data[11] = previous.nameBytes[1];
  109. data[12] = previous.nameBytes[2];
  110. data[13] = previous.nameBytes[3];
  111. }
  112. os.write(data);
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. protected void writeEnd(OutputStream os) throws IOException {
  118. // I am the first segment in the chain so write out the rest
  119. if (previous == null) {
  120. GraphicsChainedSegment current = this.next;
  121. while (current != null) {
  122. current.writeDataStream(os);
  123. current = current.next;
  124. }
  125. }
  126. }
  127. /**
  128. * Begins a graphics area (start of fill)
  129. */
  130. protected void beginArea() {
  131. this.currentArea = new GraphicsArea();
  132. super.addDrawingOrder(currentArea);
  133. }
  134. /**
  135. * Ends a graphics area (end of fill)
  136. */
  137. protected void endArea() {
  138. this.currentArea = null;
  139. }
  140. /**
  141. * {@inheritDoc}
  142. */
  143. protected PreparedAFPObject addDrawingOrder(PreparedAFPObject drawingOrder) {
  144. if (currentArea != null) {
  145. currentArea.addDrawingOrder(drawingOrder);
  146. } else {
  147. super.addDrawingOrder(drawingOrder);
  148. }
  149. return drawingOrder;
  150. }
  151. /**
  152. * {@inheritDoc}
  153. */
  154. public String toString() {
  155. return "GraphicsChainedSegment(name=" + super.name + ")";
  156. }
  157. }