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.

ObjectContainer.java 4.3KB

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.fop.afp.AFPDataObjectInfo;
  22. import org.apache.fop.afp.AFPObjectAreaInfo;
  23. import org.apache.fop.afp.AFPResourceInfo;
  24. import org.apache.fop.afp.AFPResourceLevel;
  25. import org.apache.fop.afp.Factory;
  26. import org.apache.fop.afp.modca.triplets.MappingOptionTriplet;
  27. import org.apache.fop.afp.util.BinaryUtils;
  28. /**
  29. * Object containers are MO:DCA objects that envelop and carry object data.
  30. */
  31. public class ObjectContainer extends AbstractDataObject {
  32. /** the object container data maximum length */
  33. private static final int MAX_DATA_LEN = 32759;
  34. private byte[] data;
  35. /**
  36. * Main constructor
  37. *
  38. * @param factory the object factory
  39. * @param name the name of this object container
  40. */
  41. public ObjectContainer(Factory factory, String name) {
  42. super(factory, name);
  43. }
  44. /** {@inheritDoc} */
  45. protected void writeStart(OutputStream os) throws IOException {
  46. byte[] headerData = new byte[17];
  47. copySF(headerData, Type.BEGIN, Category.OBJECT_CONTAINER);
  48. // Set the total record length
  49. int containerLen = headerData.length + getTripletDataLength() - 1;
  50. byte[] len = BinaryUtils.convert(containerLen, 2);
  51. headerData[1] = len[0]; // Length byte 1
  52. headerData[2] = len[1]; // Length byte 2
  53. os.write(headerData);
  54. }
  55. /** {@inheritDoc} */
  56. protected void writeContent(OutputStream os) throws IOException {
  57. super.writeContent(os); // write triplets and OEG
  58. // write OCDs
  59. byte[] dataHeader = new byte[9];
  60. copySF(dataHeader, SF_CLASS, Type.DATA, Category.OBJECT_CONTAINER);
  61. final int lengthOffset = 1;
  62. if (data != null) {
  63. writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
  64. }
  65. }
  66. /** {@inheritDoc} */
  67. protected void writeEnd(OutputStream os) throws IOException {
  68. byte[] data = new byte[17];
  69. copySF(data, Type.END, Category.OBJECT_CONTAINER);
  70. os.write(data);
  71. }
  72. /** {@inheritDoc} */
  73. public void setViewport(AFPDataObjectInfo dataObjectInfo) {
  74. AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
  75. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  76. // only need to set MCD and CDD when OC when is inlined (pre-2000 apps)
  77. if (resourceLevel.isInline()) {
  78. super.setViewport(dataObjectInfo);
  79. MapContainerData mapContainerData
  80. = factory.createMapContainerData(MappingOptionTriplet.SCALE_TO_FIT);
  81. getObjectEnvironmentGroup().setMapContainerData(mapContainerData);
  82. int dataWidth = dataObjectInfo.getDataWidth();
  83. int dataHeight = dataObjectInfo.getDataHeight();
  84. AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
  85. int widthRes = objectAreaInfo.getWidthRes();
  86. int heightRes = objectAreaInfo.getHeightRes();
  87. ContainerDataDescriptor containerDataDescriptor
  88. = factory.createContainerDataDescriptor(
  89. dataWidth, dataHeight, widthRes, heightRes);
  90. getObjectEnvironmentGroup().setDataDescriptor(containerDataDescriptor);
  91. }
  92. }
  93. /**
  94. * Sets the inputstream for the the object container data
  95. *
  96. * @param inputStream the inputstream for the object container data
  97. */
  98. public void setData(byte[] data) {
  99. this.data = data;
  100. }
  101. }