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.

GraphicsData.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.goca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.afp.StructuredData;
  22. import org.apache.fop.afp.util.BinaryUtils;
  23. import org.apache.fop.afp.util.StringUtils;
  24. /**
  25. * A GOCA graphics data
  26. */
  27. public final class GraphicsData extends AbstractGraphicsDrawingOrderContainer {
  28. /** the maximum graphics data length */
  29. public static final int MAX_DATA_LEN = GraphicsChainedSegment.MAX_DATA_LEN + 16;
  30. //+16 to avoid unnecessary, practically empty GraphicsData instances.
  31. /** the graphics segment */
  32. private GraphicsChainedSegment currentSegment;
  33. private boolean segmentedData;
  34. /**
  35. * Main constructor
  36. */
  37. public GraphicsData() {
  38. }
  39. /** {@inheritDoc} */
  40. @Override
  41. public int getDataLength() {
  42. return 8 + super.getDataLength();
  43. }
  44. /**
  45. * Sets the indicator that this instance is a part of a series of segmented data chunks.
  46. * This indirectly sets the SegFlag on the SFI header.
  47. * @param segmented true if this data object is not the last of the series
  48. */
  49. public void setSegmentedData(boolean segmented) {
  50. this.segmentedData = segmented;
  51. }
  52. /**
  53. * Returns a new segment name
  54. *
  55. * @return a new segment name
  56. */
  57. public String createSegmentName() {
  58. return StringUtils.lpad(String.valueOf(
  59. (super.objects != null ? super.objects.size() : 0) + 1),
  60. '0', 4);
  61. }
  62. /**
  63. * Creates a new graphics segment.
  64. *
  65. * @return a newly created graphics segment
  66. */
  67. public GraphicsChainedSegment newSegment() {
  68. return newSegment(false, false);
  69. }
  70. /**
  71. * Creates a new graphics segment.
  72. * @param appended true if this segment is appended to the previous one
  73. * @param prologPresent true if started with a prolog
  74. * @return a newly created graphics segment
  75. */
  76. public GraphicsChainedSegment newSegment(boolean appended, boolean prologPresent) {
  77. String segmentName = createSegmentName();
  78. if (currentSegment == null) {
  79. currentSegment = new GraphicsChainedSegment(segmentName);
  80. } else {
  81. currentSegment.setComplete(true);
  82. currentSegment = new GraphicsChainedSegment(segmentName,
  83. currentSegment.getNameBytes(), appended, prologPresent);
  84. }
  85. super.addObject(currentSegment);
  86. return currentSegment;
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public void addObject(StructuredData object) {
  91. if (currentSegment == null
  92. || (currentSegment.getDataLength() + object.getDataLength())
  93. >= GraphicsChainedSegment.MAX_DATA_LEN) {
  94. newSegment(true, false);
  95. }
  96. currentSegment.addObject(object);
  97. }
  98. /**
  99. * Removes the current segment from this graphics data
  100. *
  101. * @return the current segment from this graphics data
  102. */
  103. public StructuredData removeCurrentSegment() {
  104. this.currentSegment = null;
  105. return super.removeLast();
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public void writeToStream(OutputStream os) throws IOException {
  110. byte[] data = new byte[9];
  111. copySF(data, SF_CLASS, Type.DATA, Category.GRAPHICS);
  112. int dataLength = getDataLength();
  113. byte[] len = BinaryUtils.convert(dataLength, 2);
  114. data[1] = len[0]; // Length byte 1
  115. data[2] = len[1]; // Length byte 2
  116. if (this.segmentedData) {
  117. data[6] |= 32; //Data is segmented
  118. }
  119. os.write(data);
  120. writeObjects(objects, os);
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String toString() {
  125. return "GraphicsData(len: " + getDataLength() + ")";
  126. }
  127. /**
  128. * Adds the given segment to this graphics data
  129. *
  130. * @param segment a graphics chained segment
  131. */
  132. public void addSegment(GraphicsChainedSegment segment) {
  133. currentSegment = segment;
  134. super.addObject(currentSegment);
  135. }
  136. }