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.

IncludeObject.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.modca.triplets.MappingOptionTriplet;
  22. import org.apache.fop.afp.modca.triplets.MeasurementUnitsTriplet;
  23. import org.apache.fop.afp.modca.triplets.ObjectAreaSizeTriplet;
  24. import org.apache.fop.afp.util.BinaryUtils;
  25. /**
  26. * An Include Object structured field references an object on a page or overlay.
  27. * It optionally contains parameters that identify the object and that specify
  28. * presentation parameters such as object position, size, orientation, mapping,
  29. * and default color.
  30. * <p>
  31. * Where the presentation parameters conflict with parameters specified in the
  32. * object's environment group (OEG), the parameters in the Include Object
  33. * structured field override. If the referenced object is a page segment, the
  34. * IOB parameters override the corresponding environment group parameters on all
  35. * data objects in the page segment.
  36. * </p>
  37. */
  38. public class IncludeObject extends AbstractNamedAFPObject {
  39. /** the object referenced is of type page segment */
  40. public static final byte TYPE_PAGE_SEGMENT = (byte)0x5F;
  41. /** the object referenced is of type other */
  42. public static final byte TYPE_OTHER = (byte)0x92;
  43. /** the object referenced is of type graphic */
  44. public static final byte TYPE_GRAPHIC = (byte)0xBB;
  45. /** the object referenced is of type barcode */
  46. public static final byte TYPE_BARCODE = (byte)0xEB;
  47. /** the object referenced is of type image */
  48. public static final byte TYPE_IMAGE = (byte)0xFB;
  49. /** the object type referenced (default is other) */
  50. private byte objectType = TYPE_OTHER;
  51. /** the X-axis origin of the object area */
  52. private int xoaOset;
  53. /** the Y-axis origin of the object area */
  54. private int yoaOset;
  55. /** the orientation of the referenced object */
  56. private AxisOrientation oaOrent = AxisOrientation.RIGHT_HANDED_0;
  57. /** the X-axis origin defined in the object */
  58. private int xocaOset = -1;
  59. /** the Y-axis origin defined in the object */
  60. private int yocaOset = -1;
  61. /**
  62. * Constructor for the include object with the specified name, the name must
  63. * be a fixed length of eight characters and is the name of the referenced
  64. * object.
  65. *
  66. * @param name the name of this include object
  67. */
  68. public IncludeObject(String name) {
  69. super(name);
  70. }
  71. /**
  72. * Sets the orientation to use for the Include Object.
  73. *
  74. * @param orientation
  75. * The orientation (0,90, 180, 270)
  76. */
  77. public void setObjectAreaOrientation(int orientation) {
  78. this.oaOrent = AxisOrientation.getRightHandedAxisOrientationFor(orientation);
  79. }
  80. /**
  81. * Sets the x and y offset to the origin in the object area
  82. *
  83. * @param x the X-axis origin of the object area
  84. * @param y the Y-axis origin of the object area
  85. */
  86. public void setObjectAreaOffset(int x, int y) {
  87. this.xoaOset = x;
  88. this.yoaOset = y;
  89. }
  90. /**
  91. * Sets the x and y offset of the content area to the object area
  92. * used in conjunction with the
  93. * {@link MappingOptionTriplet#POSITION} and
  94. * {@link MappingOptionTriplet#POSITION_AND_TRIM}.
  95. *
  96. * @param x the X-axis origin defined in the object
  97. * @param y the Y-axis origin defined in the object
  98. */
  99. public void setContentAreaOffset(int x, int y) {
  100. this.xocaOset = x;
  101. this.yocaOset = y;
  102. }
  103. /**
  104. * Sets the data object type
  105. *
  106. * @param type the data object type
  107. */
  108. public void setObjectType(byte type) {
  109. this.objectType = type;
  110. }
  111. /** {@inheritDoc} */
  112. public void writeToStream(OutputStream os) throws IOException {
  113. byte[] data = new byte[36];
  114. super.copySF(data, Type.INCLUDE, Category.DATA_RESOURCE);
  115. // Set the total record length
  116. int tripletDataLength = getTripletDataLength();
  117. byte[] len = BinaryUtils.convert(35 + tripletDataLength, 2); //Ignore first byte
  118. data[1] = len[0];
  119. data[2] = len[1];
  120. data[17] = 0x00; // reserved
  121. data[18] = objectType;
  122. writeOsetTo(data, 19, xoaOset);
  123. writeOsetTo(data, 22, yoaOset);
  124. oaOrent.writeTo(data, 25);
  125. writeOsetTo(data, 29, xocaOset);
  126. writeOsetTo(data, 32, yocaOset);
  127. // RefCSys (Reference coordinate system)
  128. data[35] = 0x01; // Page or overlay coordinate system
  129. // Write structured field data
  130. os.write(data);
  131. // Write triplet for FQN internal/external object reference
  132. writeTriplets(os);
  133. }
  134. private static void writeOsetTo(byte[] out, int offset, int oset) {
  135. if (oset > -1) {
  136. byte[] y = BinaryUtils.convert(oset, 3);
  137. out[offset] = y[0];
  138. out[offset + 1] = y[1];
  139. out[offset + 2] = y[2];
  140. } else {
  141. out[offset] = (byte)0xFF;
  142. out[offset + 1] = (byte)0xFF;
  143. out[offset + 2] = (byte)0xFF;
  144. }
  145. }
  146. private String getObjectTypeName() {
  147. String objectTypeName = null;
  148. if (objectType == TYPE_PAGE_SEGMENT) {
  149. objectTypeName = "page segment";
  150. } else if (objectType == TYPE_OTHER) {
  151. objectTypeName = "other";
  152. } else if (objectType == TYPE_GRAPHIC) {
  153. objectTypeName = "graphic";
  154. } else if (objectType == TYPE_BARCODE) {
  155. objectTypeName = "barcode";
  156. } else if (objectType == TYPE_IMAGE) {
  157. objectTypeName = "image";
  158. }
  159. return objectTypeName;
  160. }
  161. /** {@inheritDoc} */
  162. public String toString() {
  163. return "IncludeObject{name=" + this.getName()
  164. + ", objectType=" + getObjectTypeName()
  165. + ", xoaOset=" + xoaOset
  166. + ", yoaOset=" + yoaOset
  167. + ", oaOrent" + oaOrent
  168. + ", xocaOset=" + xocaOset
  169. + ", yocaOset=" + yocaOset
  170. + "}";
  171. }
  172. /**
  173. * Sets the mapping option
  174. *
  175. * @param optionValue the mapping option value
  176. */
  177. public void setMappingOption(byte optionValue) {
  178. addTriplet(new MappingOptionTriplet(optionValue));
  179. }
  180. /**
  181. * Sets the extent of an object area in the X and Y directions
  182. *
  183. * @param x the x direction extent
  184. * @param y the y direction extent
  185. */
  186. public void setObjectAreaSize(int x, int y) {
  187. addTriplet(new ObjectAreaSizeTriplet(x, y));
  188. }
  189. /**
  190. * Sets the measurement units used to specify the units of measure
  191. *
  192. * @param xRes units per base on the x-axis
  193. * @param yRes units per base on the y-axis
  194. */
  195. public void setMeasurementUnits(int xRes, int yRes) {
  196. addTriplet(new MeasurementUnitsTriplet(xRes, xRes));
  197. }
  198. }