PageRenderTime 98ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/ppt/scratchpad/src/org/apache/poi/hslf/model/Shape.java

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