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.

HSLFShape.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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.hslf.usermodel;
  16. import java.awt.Color;
  17. import java.awt.Graphics2D;
  18. import java.awt.geom.Rectangle2D;
  19. import java.util.Iterator;
  20. import org.apache.poi.ddf.AbstractEscherOptRecord;
  21. import org.apache.poi.ddf.EscherChildAnchorRecord;
  22. import org.apache.poi.ddf.EscherClientAnchorRecord;
  23. import org.apache.poi.ddf.EscherColorRef;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.ddf.EscherOptRecord;
  26. import org.apache.poi.ddf.EscherProperties;
  27. import org.apache.poi.ddf.EscherProperty;
  28. import org.apache.poi.ddf.EscherRecord;
  29. import org.apache.poi.ddf.EscherSimpleProperty;
  30. import org.apache.poi.ddf.EscherSpRecord;
  31. import org.apache.poi.hslf.record.ColorSchemeAtom;
  32. import org.apache.poi.hslf.record.RecordTypes;
  33. import org.apache.poi.sl.usermodel.FillStyle;
  34. import org.apache.poi.sl.usermodel.Shape;
  35. import org.apache.poi.sl.usermodel.ShapeContainer;
  36. import org.apache.poi.sl.usermodel.ShapeType;
  37. import org.apache.poi.util.POILogFactory;
  38. import org.apache.poi.util.POILogger;
  39. import org.apache.poi.util.Units;
  40. /**
  41. * <p>
  42. * Represents a Shape which is the elemental object that composes a drawing.
  43. * This class is a wrapper around EscherSpContainer which holds all information
  44. * about a shape in PowerPoint document.
  45. * </p>
  46. * <p>
  47. * When you add a shape, you usually specify the dimensions of the shape and the position
  48. * of the upper'left corner of the bounding box for the shape relative to the upper'left
  49. * corner of the page, worksheet, or slide. Distances in the drawing layer are measured
  50. * in points (72 points = 1 inch).
  51. * </p>
  52. * <p>
  53. *
  54. * @author Yegor Kozlov
  55. */
  56. public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
  57. // For logging
  58. protected POILogger logger = POILogFactory.getLogger(this.getClass());
  59. /**
  60. * Either EscherSpContainer or EscheSpgrContainer record
  61. * which holds information about this shape.
  62. */
  63. protected EscherContainerRecord _escherContainer;
  64. /**
  65. * Parent of this shape.
  66. * <code>null</code> for the topmost shapes.
  67. */
  68. protected ShapeContainer<HSLFShape,HSLFTextParagraph> _parent;
  69. /**
  70. * The <code>Sheet</code> this shape belongs to
  71. */
  72. protected HSLFSheet _sheet;
  73. /**
  74. * Fill
  75. */
  76. protected HSLFFill _fill;
  77. /**
  78. * Create a Shape object. This constructor is used when an existing Shape is read from from a PowerPoint document.
  79. *
  80. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  81. * @param parent the parent of this Shape
  82. */
  83. protected HSLFShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  84. _escherContainer = escherRecord;
  85. _parent = parent;
  86. }
  87. /**
  88. * Creates the lowerlevel escher records for this shape.
  89. */
  90. protected abstract EscherContainerRecord createSpContainer(boolean isChild);
  91. /**
  92. * @return the parent of this shape
  93. */
  94. public ShapeContainer<HSLFShape,HSLFTextParagraph> getParent(){
  95. return _parent;
  96. }
  97. /**
  98. * @return name of the shape.
  99. */
  100. public String getShapeName(){
  101. return getShapeType().nativeName;
  102. }
  103. public ShapeType getShapeType(){
  104. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  105. return ShapeType.forId(spRecord.getShapeType(), false);
  106. }
  107. public void setShapeType(ShapeType type){
  108. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  109. spRecord.setShapeType( (short) type.nativeId );
  110. spRecord.setVersion( (short) 0x2 );
  111. }
  112. /**
  113. * Returns the anchor (the bounding box rectangle) of this shape.
  114. * All coordinates are expressed in points (72 dpi).
  115. *
  116. * @return the anchor of this shape
  117. */
  118. public Rectangle2D getAnchor() {
  119. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  120. int flags = spRecord.getFlags();
  121. int x1,y1,x2,y2;
  122. EscherChildAnchorRecord childRec = getEscherChild(EscherChildAnchorRecord.RECORD_ID);
  123. boolean useChildRec = ((flags & EscherSpRecord.FLAG_CHILD) != 0);
  124. if (useChildRec && childRec != null){
  125. x1 = childRec.getDx1();
  126. y1 = childRec.getDy1();
  127. x2 = childRec.getDx2();
  128. y2 = childRec.getDy2();
  129. } else {
  130. if (useChildRec) {
  131. logger.log(POILogger.WARN, "EscherSpRecord.FLAG_CHILD is set but EscherChildAnchorRecord was not found");
  132. }
  133. EscherClientAnchorRecord clientRec = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  134. x1 = clientRec.getCol1();
  135. y1 = clientRec.getFlag();
  136. x2 = clientRec.getDx1();
  137. y2 = clientRec.getRow1();
  138. }
  139. // TODO: find out where this -1 value comes from at #57820 (link to ms docs?)
  140. Rectangle2D anchor = new Rectangle2D.Double(
  141. (x1 == -1 ? -1 : Units.masterToPoints(x1)),
  142. (y1 == -1 ? -1 : Units.masterToPoints(y1)),
  143. (x2 == -1 ? -1 : Units.masterToPoints(x2-x1)),
  144. (y2 == -1 ? -1 : Units.masterToPoints(y2-y1))
  145. );
  146. return anchor;
  147. }
  148. /**
  149. * Sets the anchor (the bounding box rectangle) of this shape.
  150. * All coordinates should be expressed in points (72 dpi).
  151. *
  152. * @param anchor new anchor
  153. */
  154. public void setAnchor(Rectangle2D anchor){
  155. int x = Units.pointsToMaster(anchor.getX());
  156. int y = Units.pointsToMaster(anchor.getY());
  157. int w = Units.pointsToMaster(anchor.getWidth() + anchor.getX());
  158. int h = Units.pointsToMaster(anchor.getHeight() + anchor.getY());
  159. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  160. int flags = spRecord.getFlags();
  161. if ((flags & EscherSpRecord.FLAG_CHILD) != 0){
  162. EscherChildAnchorRecord rec = (EscherChildAnchorRecord)getEscherChild(EscherChildAnchorRecord.RECORD_ID);
  163. rec.setDx1(x);
  164. rec.setDy1(y);
  165. rec.setDx2(w);
  166. rec.setDy2(h);
  167. } else {
  168. EscherClientAnchorRecord rec = (EscherClientAnchorRecord)getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  169. rec.setCol1((short)x);
  170. rec.setFlag((short)y);
  171. rec.setDx1((short)w);
  172. rec.setRow1((short)h);
  173. }
  174. }
  175. /**
  176. * Moves the top left corner of the shape to the specified point.
  177. *
  178. * @param x the x coordinate of the top left corner of the shape
  179. * @param y the y coordinate of the top left corner of the shape
  180. */
  181. public final void moveTo(double x, double y) {
  182. // This convenience method should be implemented via setAnchor in subclasses
  183. // see HSLFGroupShape.setAnchor() for a reference
  184. Rectangle2D anchor = getAnchor();
  185. anchor.setRect(x, y, anchor.getWidth(), anchor.getHeight());
  186. setAnchor(anchor);
  187. }
  188. /**
  189. * Helper method to return escher child by record ID
  190. *
  191. * @return escher record or <code>null</code> if not found.
  192. */
  193. public static <T extends EscherRecord> T getEscherChild(EscherContainerRecord owner, int recordId){
  194. return owner.getChildById((short)recordId);
  195. }
  196. public <T extends EscherRecord> T getEscherChild(int recordId){
  197. return _escherContainer.getChildById((short)recordId);
  198. }
  199. /**
  200. * Returns escher property by id.
  201. *
  202. * @return escher property or <code>null</code> if not found.
  203. */
  204. public static <T extends EscherProperty> T getEscherProperty(AbstractEscherOptRecord opt, int propId){
  205. if (opt == null) return null;
  206. return opt.lookup(propId);
  207. }
  208. /**
  209. * Set an escher property for this shape.
  210. *
  211. * @param opt The opt record to set the properties to.
  212. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  213. * @param value value of the property. If value = -1 then the property is removed.
  214. */
  215. public static void setEscherProperty(AbstractEscherOptRecord opt, short propId, int value){
  216. java.util.List<EscherProperty> props = opt.getEscherProperties();
  217. for ( Iterator<EscherProperty> iterator = props.iterator(); iterator.hasNext(); ) {
  218. if (iterator.next().getPropertyNumber() == propId){
  219. iterator.remove();
  220. break;
  221. }
  222. }
  223. if (value != -1) {
  224. opt.addEscherProperty(new EscherSimpleProperty(propId, value));
  225. opt.sortProperties();
  226. }
  227. }
  228. /**
  229. * Set an simple escher property for this shape.
  230. *
  231. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  232. * @param value value of the property. If value = -1 then the property is removed.
  233. */
  234. public void setEscherProperty(short propId, int value){
  235. AbstractEscherOptRecord opt = getEscherOptRecord();
  236. setEscherProperty(opt, propId, value);
  237. }
  238. /**
  239. * Get the value of a simple escher property for this shape.
  240. *
  241. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  242. */
  243. public int getEscherProperty(short propId){
  244. AbstractEscherOptRecord opt = getEscherOptRecord();
  245. EscherSimpleProperty prop = getEscherProperty(opt, propId);
  246. return prop == null ? 0 : prop.getPropertyValue();
  247. }
  248. /**
  249. * Get the value of a simple escher property for this shape.
  250. *
  251. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  252. */
  253. public int getEscherProperty(short propId, int defaultValue){
  254. AbstractEscherOptRecord opt = getEscherOptRecord();
  255. EscherSimpleProperty prop = getEscherProperty(opt, propId);
  256. return prop == null ? defaultValue : prop.getPropertyValue();
  257. }
  258. /**
  259. * @return The shape container and it's children that can represent this
  260. * shape.
  261. */
  262. public EscherContainerRecord getSpContainer(){
  263. return _escherContainer;
  264. }
  265. /**
  266. * Event which fires when a shape is inserted in the sheet.
  267. * In some cases we need to propagate changes to upper level containers.
  268. * <br>
  269. * Default implementation does nothing.
  270. *
  271. * @param sh - owning shape
  272. */
  273. protected void afterInsert(HSLFSheet sh){
  274. if(_fill != null) {
  275. _fill.afterInsert(sh);
  276. }
  277. }
  278. /**
  279. * @return the <code>SlideShow</code> this shape belongs to
  280. */
  281. public HSLFSheet getSheet(){
  282. return _sheet;
  283. }
  284. /**
  285. * Assign the <code>SlideShow</code> this shape belongs to
  286. *
  287. * @param sheet owner of this shape
  288. */
  289. public void setSheet(HSLFSheet sheet){
  290. _sheet = sheet;
  291. }
  292. Color getColor(short colorProperty, short opacityProperty, int defaultColor){
  293. AbstractEscherOptRecord opt = getEscherOptRecord();
  294. EscherSimpleProperty p = getEscherProperty(opt, colorProperty);
  295. if(p == null && defaultColor == -1) return null;
  296. int val = (p == null) ? defaultColor : p.getPropertyValue();
  297. EscherColorRef ecr = new EscherColorRef(val);
  298. boolean fPaletteIndex = ecr.hasPaletteIndexFlag();
  299. boolean fPaletteRGB = ecr.hasPaletteRGBFlag();
  300. boolean fSystemRGB = ecr.hasSystemRGBFlag();
  301. boolean fSchemeIndex = ecr.hasSchemeIndexFlag();
  302. boolean fSysIndex = ecr.hasSysIndexFlag();
  303. int rgb[] = ecr.getRGB();
  304. HSLFSheet sheet = getSheet();
  305. if (fSchemeIndex && sheet != null) {
  306. //red is the index to the color scheme
  307. ColorSchemeAtom ca = sheet.getColorScheme();
  308. int schemeColor = ca.getColor(ecr.getSchemeIndex());
  309. rgb[0] = (schemeColor >> 0) & 0xFF;
  310. rgb[1] = (schemeColor >> 8) & 0xFF;
  311. rgb[2] = (schemeColor >> 16) & 0xFF;
  312. } else if (fPaletteIndex){
  313. //TODO
  314. } else if (fPaletteRGB){
  315. //TODO
  316. } else if (fSystemRGB){
  317. //TODO
  318. } else if (fSysIndex){
  319. //TODO
  320. }
  321. double alpha = getAlpha(opacityProperty);
  322. return new Color(rgb[0], rgb[1], rgb[2], (int)(alpha*255.0));
  323. }
  324. double getAlpha(short opacityProperty) {
  325. AbstractEscherOptRecord opt = getEscherOptRecord();
  326. EscherSimpleProperty op = getEscherProperty(opt, opacityProperty);
  327. int defaultOpacity = 0x00010000;
  328. int opacity = (op == null) ? defaultOpacity : op.getPropertyValue();
  329. return Units.fixedPointToDouble(opacity);
  330. }
  331. Color toRGB(int val){
  332. int a = (val >> 24) & 0xFF;
  333. int b = (val >> 16) & 0xFF;
  334. int g = (val >> 8) & 0xFF;
  335. int r = (val >> 0) & 0xFF;
  336. if(a == 0xFE){
  337. // Color is an sRGB value specified by red, green, and blue fields.
  338. } else if (a == 0xFF){
  339. // Color is undefined.
  340. } else {
  341. // index in the color scheme
  342. ColorSchemeAtom ca = getSheet().getColorScheme();
  343. int schemeColor = ca.getColor(a);
  344. r = (schemeColor >> 0) & 0xFF;
  345. g = (schemeColor >> 8) & 0xFF;
  346. b = (schemeColor >> 16) & 0xFF;
  347. }
  348. return new Color(r, g, b);
  349. }
  350. /**
  351. * @return id for the shape.
  352. */
  353. public int getShapeId(){
  354. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  355. return spRecord == null ? 0 : spRecord.getShapeId();
  356. }
  357. /**
  358. * Sets shape ID
  359. *
  360. * @param id of the shape
  361. */
  362. public void setShapeId(int id){
  363. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  364. if(spRecord != null) spRecord.setShapeId(id);
  365. }
  366. /**
  367. * Fill properties of this shape
  368. *
  369. * @return fill properties of this shape
  370. */
  371. public HSLFFill getFill(){
  372. if(_fill == null) {
  373. _fill = new HSLFFill(this);
  374. }
  375. return _fill;
  376. }
  377. public FillStyle getFillStyle() {
  378. return getFill().getFillStyle();
  379. }
  380. /**
  381. * Returns the hyperlink assigned to this shape
  382. *
  383. * @return the hyperlink assigned to this shape
  384. * or <code>null</code> if not found.
  385. */
  386. public HSLFHyperlink getHyperlink(){
  387. return HSLFHyperlink.find(this);
  388. }
  389. public void draw(Graphics2D graphics){
  390. logger.log(POILogger.INFO, "Rendering " + getShapeName());
  391. }
  392. public AbstractEscherOptRecord getEscherOptRecord() {
  393. AbstractEscherOptRecord opt = getEscherChild(EscherOptRecord.RECORD_ID);
  394. if (opt == null) {
  395. opt = getEscherChild(RecordTypes.EscherUserDefined);
  396. }
  397. return opt;
  398. }
  399. public boolean getFlipHorizontal(){
  400. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  401. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPHORIZ) != 0;
  402. }
  403. public void setFlipHorizontal(boolean flip) {
  404. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  405. int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ;
  406. spRecord.setFlags(flag);
  407. }
  408. public boolean getFlipVertical(){
  409. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  410. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPVERT) != 0;
  411. }
  412. public void setFlipVertical(boolean flip) {
  413. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  414. int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPVERT;
  415. spRecord.setFlags(flag);
  416. }
  417. public double getRotation(){
  418. int rot = getEscherProperty(EscherProperties.TRANSFORM__ROTATION);
  419. return Units.fixedPointToDouble(rot);
  420. }
  421. public void setRotation(double theta){
  422. int rot = Units.doubleToFixedPoint(theta % 360.0);
  423. setEscherProperty(EscherProperties.TRANSFORM__ROTATION, rot);
  424. }
  425. public boolean isPlaceholder() {
  426. return false;
  427. }
  428. }