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.

HSSFShape.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.poi.ddf.*;
  17. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  18. import org.apache.poi.hssf.record.ObjRecord;
  19. import org.apache.poi.util.LittleEndian;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import java.util.Iterator;
  23. /**
  24. * An abstract shape.
  25. *
  26. * @author Glen Stampoultzis (glens at apache.org)
  27. */
  28. public abstract class HSSFShape {
  29. public static final int LINEWIDTH_ONE_PT = 12700;
  30. public static final int LINEWIDTH_DEFAULT = 9525;
  31. public static final int LINESTYLE__COLOR_DEFAULT = 0x08000040;
  32. public static final int FILL__FILLCOLOR_DEFAULT = 0x08000009;
  33. public static final boolean NO_FILL_DEFAULT = true;
  34. public static final int LINESTYLE_SOLID = 0; // Solid (continuous) pen
  35. public static final int LINESTYLE_DASHSYS = 1; // PS_DASH system dash style
  36. public static final int LINESTYLE_DOTSYS = 2; // PS_DOT system dash style
  37. public static final int LINESTYLE_DASHDOTSYS = 3; // PS_DASHDOT system dash style
  38. public static final int LINESTYLE_DASHDOTDOTSYS = 4; // PS_DASHDOTDOT system dash style
  39. public static final int LINESTYLE_DOTGEL = 5; // square dot style
  40. public static final int LINESTYLE_DASHGEL = 6; // dash style
  41. public static final int LINESTYLE_LONGDASHGEL = 7; // long dash style
  42. public static final int LINESTYLE_DASHDOTGEL = 8; // dash short dash
  43. public static final int LINESTYLE_LONGDASHDOTGEL = 9; // long dash short dash
  44. public static final int LINESTYLE_LONGDASHDOTDOTGEL = 10; // long dash short dash short dash
  45. public static final int LINESTYLE_NONE = -1;
  46. public static final int LINESTYLE_DEFAULT = LINESTYLE_NONE;
  47. // TODO - make all these fields private
  48. HSSFShape parent;
  49. HSSFAnchor anchor;
  50. HSSFPatriarch _patriarch;
  51. private final EscherContainerRecord _escherContainer;
  52. private final ObjRecord _objRecord;
  53. private final EscherOptRecord _optRecord;
  54. public final static int NO_FILLHITTEST_TRUE = 0x00110000;
  55. public final static int NO_FILLHITTEST_FALSE = 0x00010000;
  56. public HSSFShape(EscherContainerRecord spContainer, ObjRecord objRecord) {
  57. this._escherContainer = spContainer;
  58. this._objRecord = objRecord;
  59. this._optRecord = spContainer.getChildById(EscherOptRecord.RECORD_ID);
  60. this.anchor = HSSFAnchor.createAnchorFromEscher(spContainer);
  61. }
  62. /**
  63. * Create a new shape with the specified parent and anchor.
  64. */
  65. public HSSFShape(HSSFShape parent, HSSFAnchor anchor) {
  66. this.parent = parent;
  67. this.anchor = anchor;
  68. this._escherContainer = createSpContainer();
  69. _optRecord = _escherContainer.getChildById(EscherOptRecord.RECORD_ID);
  70. _objRecord = createObjRecord();
  71. }
  72. protected abstract EscherContainerRecord createSpContainer();
  73. protected abstract ObjRecord createObjRecord();
  74. protected abstract void afterRemove(HSSFPatriarch patriarch);
  75. protected void removeEscherProperty(EscherOptRecord opt, int num){
  76. for ( Iterator<EscherProperty> iterator = opt.getEscherProperties().iterator(); iterator.hasNext(); ) {
  77. EscherProperty prop = iterator.next();
  78. if (prop.getPropertyNumber() == num){
  79. iterator.remove();
  80. }
  81. }
  82. }
  83. void setShapeId(int shapeId){
  84. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  85. spRecord.setShapeId(shapeId);
  86. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) _objRecord.getSubRecords().get(0);
  87. cod.setObjectId((short) (shapeId%1024));
  88. }
  89. int getShapeId(){
  90. return ((EscherSpRecord)_escherContainer.getChildById(EscherSpRecord.RECORD_ID)).getShapeId();
  91. }
  92. abstract void afterInsert(HSSFPatriarch patriarch);
  93. public EscherContainerRecord getEscherContainer() {
  94. return _escherContainer;
  95. }
  96. public ObjRecord getObjRecord() {
  97. return _objRecord;
  98. }
  99. public EscherOptRecord getOptRecord() {
  100. return _optRecord;
  101. }
  102. /**
  103. * Gets the parent shape.
  104. */
  105. public HSSFShape getParent() {
  106. return parent;
  107. }
  108. /**
  109. * @return the anchor that is used by this shape.
  110. */
  111. public HSSFAnchor getAnchor() {
  112. return anchor;
  113. }
  114. /**
  115. * Sets a particular anchor. A top-level shape must have an anchor of
  116. * HSSFClientAnchor. A child anchor must have an anchor of HSSFChildAnchor
  117. *
  118. * @param anchor the anchor to use.
  119. * @throws IllegalArgumentException when the wrong anchor is used for
  120. * this particular shape.
  121. * @see HSSFChildAnchor
  122. * @see HSSFClientAnchor
  123. */
  124. public void setAnchor(HSSFAnchor anchor) {
  125. int i = 0;
  126. int recordId = -1;
  127. if (parent == null) {
  128. if (anchor instanceof HSSFChildAnchor)
  129. throw new IllegalArgumentException("Must use client anchors for shapes directly attached to sheet.");
  130. EscherClientAnchorRecord anch = _escherContainer.getChildById(EscherClientAnchorRecord.RECORD_ID);
  131. if (null != anch) {
  132. for (i=0; i< _escherContainer.getChildRecords().size(); i++){
  133. if (_escherContainer.getChild(i).getRecordId() == EscherClientAnchorRecord.RECORD_ID){
  134. if (i != _escherContainer.getChildRecords().size() -1){
  135. recordId = _escherContainer.getChild(i+1).getRecordId();
  136. }
  137. }
  138. }
  139. _escherContainer.removeChildRecord(anch);
  140. }
  141. } else {
  142. if (anchor instanceof HSSFClientAnchor)
  143. throw new IllegalArgumentException("Must use child anchors for shapes attached to groups.");
  144. EscherChildAnchorRecord anch = _escherContainer.getChildById(EscherChildAnchorRecord.RECORD_ID);
  145. if (null != anch) {
  146. for (i=0; i< _escherContainer.getChildRecords().size(); i++){
  147. if (_escherContainer.getChild(i).getRecordId() == EscherChildAnchorRecord.RECORD_ID){
  148. if (i != _escherContainer.getChildRecords().size() -1){
  149. recordId = _escherContainer.getChild(i+1).getRecordId();
  150. }
  151. }
  152. }
  153. _escherContainer.removeChildRecord(anch);
  154. }
  155. }
  156. if (-1 == recordId){
  157. _escherContainer.addChildRecord(anchor.getEscherAnchor());
  158. } else {
  159. _escherContainer.addChildBefore(anchor.getEscherAnchor(), recordId);
  160. }
  161. this.anchor = anchor;
  162. }
  163. /**
  164. * The color applied to the lines of this shape.
  165. */
  166. public int getLineStyleColor() {
  167. EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.LINESTYLE__COLOR);
  168. return rgbProperty == null ? LINESTYLE__COLOR_DEFAULT : rgbProperty.getRgbColor();
  169. }
  170. /**
  171. * The color applied to the lines of this shape.
  172. */
  173. public void setLineStyleColor(int lineStyleColor) {
  174. setPropertyValue(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor));
  175. }
  176. /**
  177. * The color applied to the lines of this shape.
  178. */
  179. public void setLineStyleColor(int red, int green, int blue) {
  180. int lineStyleColor = ((blue) << 16) | ((green) << 8) | red;
  181. setPropertyValue(new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, lineStyleColor));
  182. }
  183. /**
  184. * The color used to fill this shape.
  185. */
  186. public int getFillColor() {
  187. EscherRGBProperty rgbProperty = _optRecord.lookup(EscherProperties.FILL__FILLCOLOR);
  188. return rgbProperty == null ? FILL__FILLCOLOR_DEFAULT : rgbProperty.getRgbColor();
  189. }
  190. /**
  191. * The color used to fill this shape.
  192. */
  193. public void setFillColor(int fillColor) {
  194. setPropertyValue(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor));
  195. }
  196. /**
  197. * The color used to fill this shape.
  198. */
  199. public void setFillColor(int red, int green, int blue) {
  200. int fillColor = ((blue) << 16) | ((green) << 8) | red;
  201. setPropertyValue(new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, fillColor));
  202. }
  203. /**
  204. * @return returns with width of the line in EMUs. 12700 = 1 pt.
  205. */
  206. public int getLineWidth() {
  207. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEWIDTH);
  208. return property == null ? LINEWIDTH_DEFAULT: property.getPropertyValue();
  209. }
  210. /**
  211. * Sets the width of the line. 12700 = 1 pt.
  212. *
  213. * @param lineWidth width in EMU's. 12700EMU's = 1 pt
  214. * @see HSSFShape#LINEWIDTH_ONE_PT
  215. */
  216. public void setLineWidth(int lineWidth) {
  217. setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEWIDTH, lineWidth));
  218. }
  219. /**
  220. * @return One of the constants in LINESTYLE_*
  221. */
  222. public int getLineStyle() {
  223. EscherSimpleProperty property = _optRecord.lookup(EscherProperties.LINESTYLE__LINEDASHING);
  224. if (null == property){
  225. return LINESTYLE_DEFAULT;
  226. }
  227. return property.getPropertyValue();
  228. }
  229. /**
  230. * Sets the line style.
  231. *
  232. * @param lineStyle One of the constants in LINESTYLE_*
  233. */
  234. public void setLineStyle(int lineStyle) {
  235. setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEDASHING, lineStyle));
  236. if (getLineStyle() != HSSFShape.LINESTYLE_SOLID) {
  237. setPropertyValue(new EscherSimpleProperty(EscherProperties.LINESTYLE__LINEENDCAPSTYLE, 0));
  238. if (getLineStyle() == HSSFShape.LINESTYLE_NONE){
  239. setPropertyValue(new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080000));
  240. } else {
  241. setPropertyValue( new EscherBoolProperty( EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x00080008));
  242. }
  243. }
  244. }
  245. /**
  246. * @return <code>true</code> if this shape is not filled with a color.
  247. */
  248. public boolean isNoFill() {
  249. EscherBoolProperty property = _optRecord.lookup(EscherProperties.FILL__NOFILLHITTEST);
  250. return property == null ? NO_FILL_DEFAULT : property.getPropertyValue() == NO_FILLHITTEST_TRUE;
  251. }
  252. /**
  253. * Sets whether this shape is filled or transparent.
  254. */
  255. public void setNoFill(boolean noFill) {
  256. setPropertyValue(new EscherBoolProperty(EscherProperties.FILL__NOFILLHITTEST, noFill ? NO_FILLHITTEST_TRUE : NO_FILLHITTEST_FALSE));
  257. }
  258. protected void setPropertyValue(EscherProperty property){
  259. _optRecord.setEscherProperty(property);
  260. }
  261. public void setFlipVertical(boolean value){
  262. EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  263. if (value){
  264. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPVERT);
  265. } else {
  266. sp.setFlags(sp.getFlags() & (Integer.MAX_VALUE - EscherSpRecord.FLAG_FLIPVERT));
  267. }
  268. }
  269. public void setFlipHorizontal(boolean value){
  270. EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  271. if (value){
  272. sp.setFlags(sp.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ);
  273. } else {
  274. sp.setFlags(sp.getFlags() & (Integer.MAX_VALUE - EscherSpRecord.FLAG_FLIPHORIZ));
  275. }
  276. }
  277. public boolean isFlipVertical(){
  278. EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  279. return (sp.getFlags() & EscherSpRecord.FLAG_FLIPVERT) != 0;
  280. }
  281. public boolean isFlipHorizontal(){
  282. EscherSpRecord sp = getEscherContainer().getChildById(EscherSpRecord.RECORD_ID);
  283. return (sp.getFlags() & EscherSpRecord.FLAG_FLIPHORIZ) != 0;
  284. }
  285. public int getRotationDegree(){
  286. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  287. EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.TRANSFORM__ROTATION);
  288. if (null == property){
  289. return 0;
  290. }
  291. try {
  292. LittleEndian.putInt(property.getPropertyValue(), bos);
  293. return LittleEndian.getShort(bos.toByteArray(), 2);
  294. } catch (IOException e) {
  295. e.printStackTrace();
  296. return 0;
  297. }
  298. }
  299. public void setRotationDegree(short value){
  300. setPropertyValue(new EscherSimpleProperty(EscherProperties.TRANSFORM__ROTATION , (value << 16)));
  301. }
  302. /**
  303. * Count of all children and their children's children.
  304. */
  305. public int countOfAllChildren() {
  306. return 1;
  307. }
  308. public abstract HSSFShape cloneShape();
  309. }