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.

HSSFPolygon.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import org.apache.logging.log4j.LogManager;
  17. import org.apache.logging.log4j.Logger;
  18. import org.apache.poi.ddf.EscherArrayProperty;
  19. import org.apache.poi.ddf.EscherBoolProperty;
  20. import org.apache.poi.ddf.EscherClientDataRecord;
  21. import org.apache.poi.ddf.EscherContainerRecord;
  22. import org.apache.poi.ddf.EscherOptRecord;
  23. import org.apache.poi.ddf.EscherPropertyTypes;
  24. import org.apache.poi.ddf.EscherRGBProperty;
  25. import org.apache.poi.ddf.EscherRecord;
  26. import org.apache.poi.ddf.EscherShapePathProperty;
  27. import org.apache.poi.ddf.EscherSimpleProperty;
  28. import org.apache.poi.ddf.EscherSpRecord;
  29. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  30. import org.apache.poi.hssf.record.EndSubRecord;
  31. import org.apache.poi.hssf.record.EscherAggregate;
  32. import org.apache.poi.hssf.record.ObjRecord;
  33. import org.apache.poi.hssf.record.TextObjectRecord;
  34. import org.apache.poi.util.LittleEndian;
  35. /**
  36. *
  37. */
  38. public class HSSFPolygon extends HSSFSimpleShape {
  39. public static final short OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING = 0x1E;
  40. private static final Logger LOG = LogManager.getLogger(HSSFPolygon.class);
  41. public HSSFPolygon(EscherContainerRecord spContainer, ObjRecord objRecord, TextObjectRecord _textObjectRecord) {
  42. super(spContainer, objRecord, _textObjectRecord);
  43. }
  44. public HSSFPolygon(EscherContainerRecord spContainer, ObjRecord objRecord) {
  45. super(spContainer, objRecord);
  46. }
  47. HSSFPolygon(HSSFShape parent, HSSFAnchor anchor) {
  48. super(parent, anchor);
  49. }
  50. @Override
  51. protected TextObjectRecord createTextObjRecord() {
  52. return null;
  53. }
  54. /**
  55. * Generates the shape records for this shape.
  56. */
  57. protected EscherContainerRecord createSpContainer() {
  58. EscherContainerRecord spContainer = new EscherContainerRecord();
  59. EscherSpRecord sp = new EscherSpRecord();
  60. EscherOptRecord opt = new EscherOptRecord();
  61. EscherClientDataRecord clientData = new EscherClientDataRecord();
  62. spContainer.setRecordId(EscherContainerRecord.SP_CONTAINER);
  63. spContainer.setOptions((short) 0x000F);
  64. sp.setRecordId(EscherSpRecord.RECORD_ID);
  65. sp.setOptions((short) ((EscherAggregate.ST_NOT_PRIMATIVE << 4) | 0x2));
  66. if (getParent() == null) {
  67. sp.setFlags(EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
  68. } else {
  69. sp.setFlags(EscherSpRecord.FLAG_CHILD | EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE);
  70. }
  71. opt.setRecordId(EscherOptRecord.RECORD_ID);
  72. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.TRANSFORM__ROTATION, false, false, 0));
  73. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GEOMETRY__RIGHT, false, false, 100));
  74. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GEOMETRY__BOTTOM, false, false, 100));
  75. opt.setEscherProperty(new EscherShapePathProperty(EscherPropertyTypes.GEOMETRY__SHAPEPATH, EscherShapePathProperty.COMPLEX));
  76. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.GEOMETRY__FILLOK, false, false, 0x00010001));
  77. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINESTARTARROWHEAD, false, false, 0x0));
  78. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEENDARROWHEAD, false, false, 0x0));
  79. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEENDCAPSTYLE, false, false, 0x0));
  80. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEDASHING, LINESTYLE_SOLID));
  81. opt.setEscherProperty( new EscherBoolProperty( EscherPropertyTypes.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
  82. opt.setEscherProperty(new EscherSimpleProperty(EscherPropertyTypes.LINESTYLE__LINEWIDTH, LINEWIDTH_DEFAULT));
  83. opt.setEscherProperty(new EscherRGBProperty(EscherPropertyTypes.FILL__FILLCOLOR, FILL__FILLCOLOR_DEFAULT));
  84. opt.setEscherProperty(new EscherRGBProperty(EscherPropertyTypes.LINESTYLE__COLOR, LINESTYLE__COLOR_DEFAULT));
  85. opt.setEscherProperty(new EscherBoolProperty(EscherPropertyTypes.FILL__NOFILLHITTEST, 1));
  86. opt.setEscherProperty(new EscherBoolProperty( EscherPropertyTypes.GROUPSHAPE__FLAGS, 0x080000));
  87. EscherRecord anchor = getAnchor().getEscherAnchor();
  88. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  89. clientData.setOptions((short) 0x0000);
  90. spContainer.addChildRecord(sp);
  91. spContainer.addChildRecord(opt);
  92. spContainer.addChildRecord(anchor);
  93. spContainer.addChildRecord(clientData);
  94. return spContainer;
  95. }
  96. /**
  97. * Creates the low level OBJ record for this shape.
  98. */
  99. protected ObjRecord createObjRecord() {
  100. ObjRecord obj = new ObjRecord();
  101. CommonObjectDataSubRecord c = new CommonObjectDataSubRecord();
  102. c.setObjectType(OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING);
  103. c.setLocked(true);
  104. c.setPrintable(true);
  105. c.setAutofill(true);
  106. c.setAutoline(true);
  107. EndSubRecord e = new EndSubRecord();
  108. obj.addSubRecord(c);
  109. obj.addSubRecord(e);
  110. return obj;
  111. }
  112. @Override
  113. protected void afterRemove(HSSFPatriarch patriarch) {
  114. patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID));
  115. }
  116. /**
  117. * @return array of x coordinates
  118. */
  119. public int[] getXPoints() {
  120. EscherArrayProperty verticesProp = getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES);
  121. if (null == verticesProp){
  122. return new int[]{};
  123. }
  124. int []array = new int[verticesProp.getNumberOfElementsInArray()-1];
  125. for (int i=0; i< verticesProp.getNumberOfElementsInArray()-1; i++){
  126. byte[] property = verticesProp.getElement(i);
  127. short x = LittleEndian.getShort(property, 0);
  128. array[i] = x;
  129. }
  130. return array;
  131. }
  132. /**
  133. * @return array of y coordinates
  134. */
  135. public int[] getYPoints() {
  136. EscherArrayProperty verticesProp = getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__VERTICES);
  137. if (null == verticesProp){
  138. return new int[]{};
  139. }
  140. int []array = new int[verticesProp.getNumberOfElementsInArray()-1];
  141. for (int i=0; i< verticesProp.getNumberOfElementsInArray()-1; i++){
  142. byte[] property = verticesProp.getElement(i);
  143. short x = LittleEndian.getShort(property, 2);
  144. array[i] = x;
  145. }
  146. return array;
  147. }
  148. /**
  149. * @param xPoints - array of x coordinates
  150. * @param yPoints - array of y coordinates
  151. */
  152. public void setPoints(int[] xPoints, int[] yPoints) {
  153. if (xPoints.length != yPoints.length){
  154. LOG.atError().log("xPoint.length must be equal to yPoints.length");
  155. return;
  156. }
  157. if (xPoints.length == 0){
  158. LOG.atError().log("HSSFPolygon must have at least one point");
  159. }
  160. EscherArrayProperty verticesProp = new EscherArrayProperty(EscherPropertyTypes.GEOMETRY__VERTICES, false, 0);
  161. verticesProp.setNumberOfElementsInArray(xPoints.length+1);
  162. verticesProp.setNumberOfElementsInMemory(xPoints.length+1);
  163. verticesProp.setSizeOfElements(0xFFF0);
  164. for (int i = 0; i < xPoints.length; i++)
  165. {
  166. byte[] data = new byte[4];
  167. LittleEndian.putShort(data, 0, (short)xPoints[i]);
  168. LittleEndian.putShort(data, 2, (short)yPoints[i]);
  169. verticesProp.setElement(i, data);
  170. }
  171. int point = xPoints.length;
  172. byte[] data = new byte[4];
  173. LittleEndian.putShort(data, 0, (short)xPoints[0]);
  174. LittleEndian.putShort(data, 2, (short)yPoints[0]);
  175. verticesProp.setElement(point, data);
  176. setPropertyValue(verticesProp);
  177. EscherArrayProperty segmentsProp = new EscherArrayProperty(EscherPropertyTypes.GEOMETRY__SEGMENTINFO, false, 0);
  178. segmentsProp.setSizeOfElements(0x0002);
  179. segmentsProp.setNumberOfElementsInArray(xPoints.length * 2 + 4);
  180. segmentsProp.setNumberOfElementsInMemory(xPoints.length * 2 + 4);
  181. segmentsProp.setElement(0, new byte[] { (byte)0x00, (byte)0x40 } );
  182. segmentsProp.setElement(1, new byte[] { (byte)0x00, (byte)0xAC } );
  183. for (int i = 0; i < xPoints.length; i++)
  184. {
  185. segmentsProp.setElement(2 + i * 2, new byte[] { (byte)0x01, (byte)0x00 } );
  186. segmentsProp.setElement(3 + i * 2, new byte[] { (byte)0x00, (byte)0xAC } );
  187. }
  188. segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 2, new byte[] { (byte)0x01, (byte)0x60 } );
  189. segmentsProp.setElement(segmentsProp.getNumberOfElementsInArray() - 1, new byte[] { (byte)0x00, (byte)0x80 } );
  190. setPropertyValue(segmentsProp);
  191. }
  192. /**
  193. * Defines the width and height of the points in the polygon
  194. * @param width
  195. * @param height
  196. */
  197. public void setPolygonDrawArea(int width, int height) {
  198. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GEOMETRY__RIGHT, width));
  199. setPropertyValue(new EscherSimpleProperty(EscherPropertyTypes.GEOMETRY__BOTTOM, height));
  200. }
  201. /**
  202. * @return shape width
  203. */
  204. public int getDrawAreaWidth() {
  205. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__RIGHT);
  206. return property == null ? 100: property.getPropertyValue();
  207. }
  208. /**
  209. * @return shape height
  210. */
  211. public int getDrawAreaHeight() {
  212. EscherSimpleProperty property = getOptRecord().lookup(EscherPropertyTypes.GEOMETRY__BOTTOM);
  213. return property == null ? 100: property.getPropertyValue();
  214. }
  215. }