PageRenderTime 58ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/model/PolygonShape.java

http://github.com/openmicroscopy/bioformats
Java | 183 lines | 111 code | 21 blank | 51 comment | 4 complexity | 568f42341c2f49cf457f7336fcfb066f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.model;
  38. import loci.poi.ddf.*;
  39. import loci.poi.hssf.record.ObjRecord;
  40. import loci.poi.hssf.record.EscherAggregate;
  41. import loci.poi.hssf.record.CommonObjectDataSubRecord;
  42. import loci.poi.hssf.record.EndSubRecord;
  43. import loci.poi.hssf.usermodel.HSSFShape;
  44. import loci.poi.hssf.usermodel.HSSFPolygon;
  45. import loci.poi.util.LittleEndian;
  46. public class PolygonShape
  47. extends AbstractShape
  48. {
  49. public final static short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 30;
  50. private EscherContainerRecord spContainer;
  51. private ObjRecord objRecord;
  52. /**
  53. * Creates the low evel records for an polygon.
  54. *
  55. * @param hssfShape The highlevel shape.
  56. * @param shapeId The shape id to use for this shape.
  57. */
  58. PolygonShape( HSSFPolygon hssfShape, int shapeId )
  59. {
  60. spContainer = createSpContainer( hssfShape, shapeId );
  61. objRecord = createObjRecord( hssfShape, shapeId );
  62. }
  63. /**
  64. * Generates the shape records for this shape.
  65. *
  66. */
  67. private EscherContainerRecord createSpContainer( HSSFPolygon hssfShape, int shapeId )
  68. {
  69. HSSFShape shape = hssfShape;
  70. EscherContainerRecord spContainer = new EscherContainerRecord();
  71. EscherSpRecord sp = new EscherSpRecord();
  72. EscherOptRecord opt = new EscherOptRecord();
  73. EscherClientDataRecord clientData = new EscherClientDataRecord();
  74. spContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  75. spContainer.setOptions( (short) 0x000F );
  76. sp.setRecordId( EscherSpRecord.RECORD_ID );
  77. sp.setOptions( (short) ( ( EscherAggregate.ST_DONUT << 4 ) | 0x2 ) );
  78. sp.setShapeId( shapeId );
  79. if (hssfShape.getParent() == null)
  80. sp.setFlags( EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
  81. else
  82. sp.setFlags( EscherSpRecord.FLAG_CHILD | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE );
  83. opt.setRecordId( EscherOptRecord.RECORD_ID );
  84. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.TRANSFORM__ROTATION, false, false, 0));
  85. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__RIGHT, false, false, hssfShape.getDrawAreaWidth()));
  86. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__BOTTOM, false, false, hssfShape.getDrawAreaHeight()));
  87. opt.addEscherProperty(new EscherShapePathProperty(EscherProperties.GEOMETRY__SHAPEPATH, EscherShapePathProperty.COMPLEX));
  88. EscherArrayProperty verticesProp = new EscherArrayProperty(EscherProperties.GEOMETRY__VERTICES, false, new byte[0] );
  89. verticesProp.setNumberOfElementsInArray(hssfShape.getXPoints().length+1);
  90. verticesProp.setNumberOfElementsInMemory(hssfShape.getXPoints().length+1);
  91. verticesProp.setSizeOfElements(0xFFF0);
  92. for (int i = 0; i < hssfShape.getXPoints().length; i++)
  93. {
  94. byte[] data = new byte[4];
  95. LittleEndian.putShort(data, 0, (short)hssfShape.getXPoints()[i]);
  96. LittleEndian.putShort(data, 2, (short)hssfShape.getYPoints()[i]);
  97. verticesProp.setElement(i, data);
  98. }
  99. int point = hssfShape.getXPoints().length;
  100. byte[] data = new byte[4];
  101. LittleEndian.putShort(data, 0, (short)hssfShape.getXPoints()[0]);
  102. LittleEndian.putShort(data, 2, (short)hssfShape.getYPoints()[0]);
  103. verticesProp.setElement(point, data);
  104. opt.addEscherProperty(verticesProp);
  105. EscherArrayProperty segmentsProp = new EscherArrayProperty(EscherProperties.GEOMETRY__SEGMENTINFO, false, null );
  106. segmentsProp.setSizeOfElements(0x0002);
  107. segmentsProp.setNumberOfElementsInArray(hssfShape.getXPoints().length * 2 + 4);
  108. segmentsProp.setNumberOfElementsInMemory(hssfShape.getXPoints().length * 2 + 4);
  109. segmentsProp.setElement(0, new byte[] { (byte)0x00, (byte)0x40 } );
  110. segmentsProp.setElement(1, new byte[] { (byte)0x00, (byte)0xAC } );
  111. for (int i = 0; i < hssfShape.getXPoints().length; i++)
  112. {
  113. segmentsProp.setElement(2 + i * 2, new byte[] { (byte)0x01, (byte)0x00 } );
  114. segmentsProp.setElement(3 + i * 2, new byte[] { (byte)0x00, (byte)0xAC } );
  115. }
  116. segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 2, new byte[] { (byte)0x01, (byte)0x60 } );
  117. segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 1, new byte[] { (byte)0x00, (byte)0x80 } );
  118. opt.addEscherProperty(segmentsProp);
  119. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__FILLOK, false, false, 0x00010001));
  120. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINESTARTARROWHEAD, false, false, 0x0));
  121. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEENDARROWHEAD, false, false, 0x0));
  122. opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEENDCAPSTYLE, false, false, 0x0));
  123. addStandardOptions(shape, opt);
  124. EscherRecord anchor = createAnchor( shape.getAnchor() );
  125. clientData.setRecordId( EscherClientDataRecord.RECORD_ID );
  126. clientData.setOptions( (short) 0x0000 );
  127. spContainer.addChildRecord( sp );
  128. spContainer.addChildRecord( opt );
  129. spContainer.addChildRecord( anchor );
  130. spContainer.addChildRecord( clientData );
  131. return spContainer;
  132. }
  133. /**
  134. * Creates the low level OBJ record for this shape.
  135. */
  136. private ObjRecord createObjRecord( HSSFShape hssfShape, int shapeId )
  137. {
  138. HSSFShape shape = hssfShape;
  139. ObjRecord obj = new ObjRecord();
  140. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  141. c.setObjectType( OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING );
  142. c.setObjectId( (short) ( shapeId ) );
  143. c.setLocked( true );
  144. c.setPrintable( true );
  145. c.setAutofill( true );
  146. c.setAutoline( true );
  147. EndSubRecord e = new EndSubRecord();
  148. obj.addSubRecord( c );
  149. obj.addSubRecord( e );
  150. return obj;
  151. }
  152. public EscherContainerRecord getSpContainer()
  153. {
  154. return spContainer;
  155. }
  156. public ObjRecord getObjRecord()
  157. {
  158. return objRecord;
  159. }
  160. }