Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

HSLFShape.java 18KB

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