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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = 8192;
  30. /** the graphics segment */
  31. private GraphicsChainedSegment currentSegment = null;
  32. /**
  33. * Main constructor
  34. */
  35. public GraphicsData() {
  36. }
  37. /** {@inheritDoc} */
  38. public int getDataLength() {
  39. return 8 + super.getDataLength();
  40. }
  41. /**
  42. * Returns a new segment name
  43. *
  44. * @return a new segment name
  45. */
  46. public String createSegmentName() {
  47. return StringUtils.lpad(String.valueOf(
  48. (super.objects != null ? super.objects.size() : 0) + 1),
  49. '0', 4);
  50. }
  51. /**
  52. * Creates a new graphics segment
  53. *
  54. * @return a newly created graphics segment
  55. */
  56. public GraphicsChainedSegment newSegment() {
  57. String segmentName = createSegmentName();
  58. if (currentSegment == null) {
  59. currentSegment = new GraphicsChainedSegment(segmentName);
  60. } else {
  61. currentSegment.setComplete(true);
  62. currentSegment = new GraphicsChainedSegment(segmentName, currentSegment.getNameBytes());
  63. }
  64. super.addObject(currentSegment);
  65. return currentSegment;
  66. }
  67. /** {@inheritDoc} */
  68. public void addObject(StructuredData object) {
  69. if (currentSegment == null
  70. || (currentSegment.getDataLength() + object.getDataLength())
  71. >= GraphicsChainedSegment.MAX_DATA_LEN) {
  72. newSegment();
  73. }
  74. currentSegment.addObject(object);
  75. }
  76. /**
  77. * Removes the current segment from this graphics data
  78. *
  79. * @return the current segment from this graphics data
  80. */
  81. public StructuredData removeCurrentSegment() {
  82. this.currentSegment = null;
  83. return super.removeLast();
  84. }
  85. /** {@inheritDoc} */
  86. public void writeToStream(OutputStream os) throws IOException {
  87. byte[] data = new byte[9];
  88. copySF(data, SF_CLASS, Type.DATA, Category.GRAPHICS);
  89. int dataLength = getDataLength();
  90. byte[] len = BinaryUtils.convert(dataLength, 2);
  91. data[1] = len[0]; // Length byte 1
  92. data[2] = len[1]; // Length byte 2
  93. os.write(data);
  94. writeObjects(objects, os);
  95. }
  96. /** {@inheritDoc} */
  97. public String toString() {
  98. return "GraphicsData";
  99. }
  100. /**
  101. * Adds the given segment to this graphics data
  102. *
  103. * @param segment a graphics chained segment
  104. */
  105. public void addSegment(GraphicsChainedSegment segment) {
  106. currentSegment = segment;
  107. super.addObject(currentSegment);
  108. }
  109. }