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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 java.util.List;
  21. import org.apache.logging.log4j.LogManager;
  22. import org.apache.logging.log4j.Logger;
  23. import org.apache.poi.ddf.AbstractEscherOptRecord;
  24. import org.apache.poi.ddf.EscherChildAnchorRecord;
  25. import org.apache.poi.ddf.EscherClientAnchorRecord;
  26. import org.apache.poi.ddf.EscherClientDataRecord;
  27. import org.apache.poi.ddf.EscherColorRef;
  28. import org.apache.poi.ddf.EscherColorRef.SysIndexProcedure;
  29. import org.apache.poi.ddf.EscherColorRef.SysIndexSource;
  30. import org.apache.poi.ddf.EscherComplexProperty;
  31. import org.apache.poi.ddf.EscherContainerRecord;
  32. import org.apache.poi.ddf.EscherProperty;
  33. import org.apache.poi.ddf.EscherPropertyTypes;
  34. import org.apache.poi.ddf.EscherRecord;
  35. import org.apache.poi.ddf.EscherRecordTypes;
  36. import org.apache.poi.ddf.EscherSimpleProperty;
  37. import org.apache.poi.ddf.EscherSpRecord;
  38. import org.apache.poi.ddf.EscherTextboxRecord;
  39. import org.apache.poi.hslf.record.ColorSchemeAtom;
  40. import org.apache.poi.hslf.record.HSLFEscherClientDataRecord;
  41. import org.apache.poi.hslf.record.Record;
  42. import org.apache.poi.sl.draw.DrawFactory;
  43. import org.apache.poi.sl.usermodel.FillStyle;
  44. import org.apache.poi.sl.usermodel.PresetColor;
  45. import org.apache.poi.sl.usermodel.Shape;
  46. import org.apache.poi.sl.usermodel.ShapeContainer;
  47. import org.apache.poi.sl.usermodel.ShapeType;
  48. import org.apache.poi.util.RecordFormatException;
  49. import org.apache.poi.util.Removal;
  50. import org.apache.poi.util.StringUtil;
  51. import org.apache.poi.util.Units;
  52. /**
  53. * Represents a Shape which is the elemental object that composes a drawing.
  54. * This class is a wrapper around EscherSpContainer which holds all information
  55. * about a shape in PowerPoint document.
  56. * <p>
  57. * When you add a shape, you usually specify the dimensions of the shape and the position
  58. * of the upper'left corner of the bounding box for the shape relative to the upper'left
  59. * corner of the page, worksheet, or slide. Distances in the drawing layer are measured
  60. * in points (72 points = 1 inch).
  61. */
  62. public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
  63. private static final Logger LOG = LogManager.getLogger(HSLFShape.class);
  64. /**
  65. * Either EscherSpContainer or EscheSpgrContainer record
  66. * which holds information about this shape.
  67. */
  68. private EscherContainerRecord _escherContainer;
  69. /**
  70. * Parent of this shape.
  71. * {@code null} for the topmost shapes.
  72. */
  73. private final ShapeContainer<HSLFShape,HSLFTextParagraph> _parent;
  74. /**
  75. * The {@code Sheet} this shape belongs to
  76. */
  77. private HSLFSheet _sheet;
  78. /**
  79. * Fill
  80. */
  81. private HSLFFill _fill;
  82. /**
  83. * Create a Shape object. This constructor is used when an existing Shape is read from a PowerPoint document.
  84. *
  85. * @param escherRecord {@code EscherSpContainer} container which holds information about this shape
  86. * @param parent the parent of this Shape
  87. */
  88. protected HSLFShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  89. _escherContainer = escherRecord;
  90. _parent = parent;
  91. }
  92. /**
  93. * Create and assign the lower level escher record to this shape
  94. */
  95. protected EscherContainerRecord createSpContainer(boolean isChild) {
  96. if (_escherContainer == null) {
  97. _escherContainer = new EscherContainerRecord();
  98. _escherContainer.setOptions((short)15);
  99. }
  100. return _escherContainer;
  101. }
  102. /**
  103. * @return the parent of this shape
  104. */
  105. @Override
  106. public ShapeContainer<HSLFShape,HSLFTextParagraph> getParent(){
  107. return _parent;
  108. }
  109. /**
  110. * @return name of the shape.
  111. */
  112. @Override
  113. public String getShapeName(){
  114. final EscherComplexProperty ep = getEscherProperty(getEscherOptRecord(), EscherPropertyTypes.GROUPSHAPE__SHAPENAME);
  115. if (ep != null) {
  116. final byte[] cd = ep.getComplexData();
  117. return StringUtil.getFromUnicodeLE0Terminated(cd, 0, cd.length/2);
  118. } else {
  119. return getShapeType().nativeName+" "+getShapeId();
  120. }
  121. }
  122. public ShapeType getShapeType(){
  123. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  124. return ShapeType.forId(spRecord.getShapeType(), false);
  125. }
  126. public void setShapeType(ShapeType type){
  127. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  128. spRecord.setShapeType( (short) type.nativeId );
  129. spRecord.setVersion( (short) 0x2 );
  130. }
  131. /**
  132. * Returns the anchor (the bounding box rectangle) of this shape.
  133. * All coordinates are expressed in points (72 dpi).
  134. *
  135. * @return the anchor of this shape
  136. */
  137. @Override
  138. public Rectangle2D getAnchor() {
  139. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  140. int flags = spRecord.getFlags();
  141. int x1,y1,x2,y2;
  142. EscherChildAnchorRecord childRec = getEscherChild(EscherChildAnchorRecord.RECORD_ID);
  143. boolean useChildRec = ((flags & EscherSpRecord.FLAG_CHILD) != 0);
  144. if (useChildRec && childRec != null){
  145. x1 = childRec.getDx1();
  146. y1 = childRec.getDy1();
  147. x2 = childRec.getDx2();
  148. y2 = childRec.getDy2();
  149. } else {
  150. if (useChildRec) {
  151. LOG.atWarn().log("EscherSpRecord.FLAG_CHILD is set but EscherChildAnchorRecord was not found");
  152. }
  153. EscherClientAnchorRecord clientRec = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  154. if (clientRec == null) {
  155. throw new RecordFormatException("Could not read record 'CLIENT_ANCHOR' with record-id: " + EscherClientAnchorRecord.RECORD_ID);
  156. }
  157. x1 = clientRec.getCol1();
  158. y1 = clientRec.getFlag();
  159. x2 = clientRec.getDx1();
  160. y2 = clientRec.getRow1();
  161. }
  162. // TODO: find out where this -1 value comes from at #57820 (link to ms docs?)
  163. return new Rectangle2D.Double(
  164. (x1 == -1 ? -1 : Units.masterToPoints(x1)),
  165. (y1 == -1 ? -1 : Units.masterToPoints(y1)),
  166. (x2 == -1 ? -1 : Units.masterToPoints(x2-x1)),
  167. (y2 == -1 ? -1 : Units.masterToPoints(y2-y1))
  168. );
  169. }
  170. /**
  171. * Sets the anchor (the bounding box rectangle) of this shape.
  172. * All coordinates should be expressed in points (72 dpi).
  173. *
  174. * @param anchor new anchor
  175. */
  176. public void setAnchor(Rectangle2D anchor){
  177. int x = Units.pointsToMaster(anchor.getX());
  178. int y = Units.pointsToMaster(anchor.getY());
  179. int w = Units.pointsToMaster(anchor.getWidth() + anchor.getX());
  180. int h = Units.pointsToMaster(anchor.getHeight() + anchor.getY());
  181. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  182. int flags = spRecord.getFlags();
  183. if ((flags & EscherSpRecord.FLAG_CHILD) != 0){
  184. EscherChildAnchorRecord rec = getEscherChild(EscherChildAnchorRecord.RECORD_ID);
  185. rec.setDx1(x);
  186. rec.setDy1(y);
  187. rec.setDx2(w);
  188. rec.setDy2(h);
  189. } else {
  190. EscherClientAnchorRecord rec = getEscherChild(EscherClientAnchorRecord.RECORD_ID);
  191. rec.setCol1((short)x);
  192. rec.setFlag((short)y);
  193. rec.setDx1((short)w);
  194. rec.setRow1((short)h);
  195. }
  196. }
  197. /**
  198. * Moves the top left corner of the shape to the specified point.
  199. *
  200. * @param x the x coordinate of the top left corner of the shape
  201. * @param y the y coordinate of the top left corner of the shape
  202. */
  203. public final void moveTo(double x, double y) {
  204. // This convenience method should be implemented via setAnchor in subclasses
  205. // see HSLFGroupShape.setAnchor() for a reference
  206. Rectangle2D anchor = getAnchor();
  207. anchor.setRect(x, y, anchor.getWidth(), anchor.getHeight());
  208. setAnchor(anchor);
  209. }
  210. /**
  211. * Helper method to return escher child by record ID
  212. *
  213. * @return escher record or {@code null} if not found.
  214. */
  215. public static <T extends EscherRecord> T getEscherChild(EscherContainerRecord owner, int recordId){
  216. return owner.getChildById((short)recordId);
  217. }
  218. /**
  219. * @since POI 3.14-Beta2
  220. */
  221. public static <T extends EscherRecord> T getEscherChild(EscherContainerRecord owner, EscherRecordTypes recordId){
  222. return getEscherChild(owner, recordId.typeID);
  223. }
  224. public <T extends EscherRecord> T getEscherChild(int recordId){
  225. if (_escherContainer == null) {
  226. throw new IllegalStateException("Did not have a container for fetching children");
  227. }
  228. return _escherContainer.getChildById((short)recordId);
  229. }
  230. /**
  231. * @since POI 3.14-Beta2
  232. */
  233. public <T extends EscherRecord> T getEscherChild(EscherRecordTypes recordId){
  234. return getEscherChild(recordId.typeID);
  235. }
  236. /**
  237. * Returns escher property by id.
  238. *
  239. * @return escher property or {@code null} if not found.
  240. *
  241. * @deprecated use {@link #getEscherProperty(EscherPropertyTypes)} instead
  242. */
  243. @Deprecated
  244. @Removal(version = "5.0.0")
  245. public static <T extends EscherProperty> T getEscherProperty(AbstractEscherOptRecord opt, int propId){
  246. return (opt == null) ? null : opt.lookup(propId);
  247. }
  248. /**
  249. * Returns escher property by type.
  250. *
  251. * @return escher property or {@code null} if not found.
  252. */
  253. public static <T extends EscherProperty> T getEscherProperty(AbstractEscherOptRecord opt, EscherPropertyTypes type){
  254. return (opt == null) ? null : opt.lookup(type);
  255. }
  256. /**
  257. * Set an escher property for this shape.
  258. *
  259. * @param opt The opt record to set the properties to.
  260. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  261. * @param value value of the property. If value = -1 then the property is removed.
  262. *
  263. * @deprecated use {@link #setEscherProperty(AbstractEscherOptRecord, EscherPropertyTypes, int)}
  264. */
  265. @Deprecated
  266. @Removal(version = "5.0.0")
  267. public static void setEscherProperty(AbstractEscherOptRecord opt, short propId, int value){
  268. List<EscherProperty> props = opt.getEscherProperties();
  269. for ( Iterator<EscherProperty> iterator = props.iterator(); iterator.hasNext(); ) {
  270. if (iterator.next().getPropertyNumber() == propId){
  271. iterator.remove();
  272. break;
  273. }
  274. }
  275. if (value != -1) {
  276. opt.addEscherProperty(new EscherSimpleProperty(propId, value));
  277. opt.sortProperties();
  278. }
  279. }
  280. /**
  281. * Set an escher property for this shape.
  282. *
  283. * @param opt The opt record to set the properties to.
  284. * @param propType The type of the property.
  285. * @param value value of the property. If value = -1 then the property is removed.
  286. */
  287. public static void setEscherProperty(AbstractEscherOptRecord opt, EscherPropertyTypes propType, int value){
  288. setEscherProperty(opt, propType, false, value);
  289. }
  290. public static void setEscherProperty(AbstractEscherOptRecord opt, EscherPropertyTypes propType, boolean isBlipId, int value){
  291. List<EscherProperty> props = opt.getEscherProperties();
  292. for ( Iterator<EscherProperty> iterator = props.iterator(); iterator.hasNext(); ) {
  293. if (iterator.next().getPropertyNumber() == propType.propNumber){
  294. iterator.remove();
  295. break;
  296. }
  297. }
  298. if (value != -1) {
  299. opt.addEscherProperty(new EscherSimpleProperty(propType, false, isBlipId, value));
  300. opt.sortProperties();
  301. }
  302. }
  303. /**
  304. * Set an simple escher property for this shape.
  305. *
  306. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  307. * @param value value of the property. If value = -1 then the property is removed.
  308. *
  309. * @deprecated use {@link #setEscherProperty(EscherPropertyTypes, int)}
  310. */
  311. @Deprecated
  312. @Removal(version = "5.0.0")
  313. public void setEscherProperty(short propId, int value){
  314. AbstractEscherOptRecord opt = getEscherOptRecord();
  315. setEscherProperty(opt, propId, value);
  316. }
  317. /**
  318. * Set an simple escher property for this shape.
  319. *
  320. * @param propType The type of the property.
  321. * @param value value of the property. If value = -1 then the property is removed.
  322. */
  323. public void setEscherProperty(EscherPropertyTypes propType, int value){
  324. AbstractEscherOptRecord opt = getEscherOptRecord();
  325. setEscherProperty(opt, propType, value);
  326. }
  327. /**
  328. * Get the value of a simple escher property for this shape.
  329. *
  330. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  331. */
  332. public int getEscherProperty(short propId){
  333. AbstractEscherOptRecord opt = getEscherOptRecord();
  334. EscherSimpleProperty prop = getEscherProperty(opt, propId);
  335. return prop == null ? 0 : prop.getPropertyValue();
  336. }
  337. /**
  338. * Get the value of a simple escher property for this shape.
  339. *
  340. * @param propType The type of the property. One of the constants defined in EscherOptRecord.
  341. */
  342. public int getEscherProperty(EscherPropertyTypes propType){
  343. AbstractEscherOptRecord opt = getEscherOptRecord();
  344. EscherSimpleProperty prop = getEscherProperty(opt, propType);
  345. return prop == null ? 0 : prop.getPropertyValue();
  346. }
  347. /**
  348. * Get the value of a simple escher property for this shape.
  349. *
  350. * @param propId The id of the property. One of the constants defined in EscherOptRecord.
  351. *
  352. * @deprecated use {@link #getEscherProperty(EscherPropertyTypes, int)} instead
  353. */
  354. @Deprecated
  355. @Removal(version = "5.0.0")
  356. public int getEscherProperty(short propId, int defaultValue){
  357. AbstractEscherOptRecord opt = getEscherOptRecord();
  358. EscherSimpleProperty prop = getEscherProperty(opt, propId);
  359. return prop == null ? defaultValue : prop.getPropertyValue();
  360. }
  361. /**
  362. * Get the value of a simple escher property for this shape.
  363. *
  364. * @param type The type of the property.
  365. */
  366. public int getEscherProperty(EscherPropertyTypes type, int defaultValue){
  367. AbstractEscherOptRecord opt = getEscherOptRecord();
  368. EscherSimpleProperty prop = getEscherProperty(opt, type);
  369. return prop == null ? defaultValue : prop.getPropertyValue();
  370. }
  371. /**
  372. * @return The shape container and its children that can represent this
  373. * shape.
  374. */
  375. public EscherContainerRecord getSpContainer(){
  376. return _escherContainer;
  377. }
  378. /**
  379. * Event which fires when a shape is inserted in the sheet.
  380. * In some cases we need to propagate changes to upper level containers.
  381. * <br>
  382. * Default implementation does nothing.
  383. *
  384. * @param sh - owning shape
  385. */
  386. protected void afterInsert(HSLFSheet sh){
  387. if(_fill != null) {
  388. _fill.afterInsert(sh);
  389. }
  390. }
  391. /**
  392. * @return the {@code SlideShow} this shape belongs to
  393. */
  394. @Override
  395. public HSLFSheet getSheet(){
  396. return _sheet;
  397. }
  398. /**
  399. * Assign the {@code SlideShow} this shape belongs to
  400. *
  401. * @param sheet owner of this shape
  402. */
  403. public void setSheet(HSLFSheet sheet){
  404. _sheet = sheet;
  405. }
  406. Color getColor(EscherPropertyTypes colorProperty, EscherPropertyTypes opacityProperty){
  407. final AbstractEscherOptRecord opt = getEscherOptRecord();
  408. final EscherSimpleProperty colProp = getEscherProperty(opt, colorProperty);
  409. final Color col;
  410. if (colProp == null) {
  411. col = Color.WHITE;
  412. } else {
  413. EscherColorRef ecr = new EscherColorRef(colProp.getPropertyValue());
  414. col = getColor(ecr);
  415. if (col == null) {
  416. return null;
  417. }
  418. }
  419. double alpha = getAlpha(opacityProperty);
  420. return new Color(col.getRed(), col.getGreen(), col.getBlue(), (int)(alpha*255.0));
  421. }
  422. Color getColor(EscherColorRef ecr) {
  423. boolean fPaletteIndex = ecr.hasPaletteIndexFlag();
  424. boolean fPaletteRGB = ecr.hasPaletteRGBFlag();
  425. boolean fSystemRGB = ecr.hasSystemRGBFlag();
  426. boolean fSchemeIndex = ecr.hasSchemeIndexFlag();
  427. boolean fSysIndex = ecr.hasSysIndexFlag();
  428. int[] rgb = ecr.getRGB();
  429. HSLFSheet sheet = getSheet();
  430. if (fSchemeIndex && sheet != null) {
  431. //red is the index to the color scheme
  432. ColorSchemeAtom ca = sheet.getColorScheme();
  433. int schemeColor = ca.getColor(ecr.getSchemeIndex());
  434. rgb[0] = (schemeColor >> 0) & 0xFF;
  435. rgb[1] = (schemeColor >> 8) & 0xFF;
  436. rgb[2] = (schemeColor >> 16) & 0xFF;
  437. } else if (fPaletteIndex) {
  438. //TODO
  439. } else if (fPaletteRGB) {
  440. //TODO
  441. } else if (fSystemRGB) {
  442. //TODO
  443. } else if (fSysIndex) {
  444. Color col = getSysIndexColor(ecr);
  445. col = applySysIndexProcedure(ecr, col);
  446. return col;
  447. }
  448. return new Color(rgb[0], rgb[1], rgb[2]);
  449. }
  450. private Color getSysIndexColor(EscherColorRef ecr) {
  451. SysIndexSource sis = ecr.getSysIndexSource();
  452. if (sis == null) {
  453. int sysIdx = ecr.getSysIndex();
  454. PresetColor pc = PresetColor.valueOfNativeId(sysIdx);
  455. return (pc != null) ? pc.color : null;
  456. }
  457. // TODO: check for recursive loops, when color getter also reference
  458. // a different color type
  459. switch (sis) {
  460. case FILL_COLOR: {
  461. return getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
  462. }
  463. case LINE_OR_FILL_COLOR: {
  464. Color col = null;
  465. if (this instanceof HSLFSimpleShape) {
  466. col = getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
  467. }
  468. if (col == null) {
  469. col = getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
  470. }
  471. return col;
  472. }
  473. case LINE_COLOR: {
  474. if (this instanceof HSLFSimpleShape) {
  475. return getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
  476. }
  477. break;
  478. }
  479. case SHADOW_COLOR: {
  480. if (this instanceof HSLFSimpleShape) {
  481. return ((HSLFSimpleShape)this).getShadowColor();
  482. }
  483. break;
  484. }
  485. case CURRENT_OR_LAST_COLOR: {
  486. // TODO ... read from graphics context???
  487. break;
  488. }
  489. case FILL_BACKGROUND_COLOR: {
  490. return getColor(EscherPropertyTypes.FILL__FILLBACKCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
  491. }
  492. case LINE_BACKGROUND_COLOR: {
  493. if (this instanceof HSLFSimpleShape) {
  494. return ((HSLFSimpleShape)this).getLineBackgroundColor();
  495. }
  496. break;
  497. }
  498. case FILL_OR_LINE_COLOR: {
  499. Color col = getColor(EscherPropertyTypes.FILL__FILLCOLOR, EscherPropertyTypes.FILL__FILLOPACITY);
  500. if (col == null && this instanceof HSLFSimpleShape) {
  501. col = getColor(EscherPropertyTypes.LINESTYLE__COLOR, EscherPropertyTypes.LINESTYLE__OPACITY);
  502. }
  503. return col;
  504. }
  505. default:
  506. break;
  507. }
  508. return null;
  509. }
  510. private Color applySysIndexProcedure(EscherColorRef ecr, Color col) {
  511. final SysIndexProcedure sip = ecr.getSysIndexProcedure();
  512. if (col == null || sip == null) {
  513. return col;
  514. }
  515. switch (sip) {
  516. case DARKEN_COLOR: {
  517. // see java.awt.Color#darken()
  518. double FACTOR = (ecr.getRGB()[2])/255.;
  519. int r = Math.toIntExact(Math.round(col.getRed()*FACTOR));
  520. int g = Math.toIntExact(Math.round(col.getGreen()*FACTOR));
  521. int b = Math.toIntExact(Math.round(col.getBlue()*FACTOR));
  522. return new Color(r,g,b);
  523. }
  524. case LIGHTEN_COLOR: {
  525. double FACTOR = (0xFF-ecr.getRGB()[2])/255.;
  526. int r = col.getRed();
  527. int g = col.getGreen();
  528. int b = col.getBlue();
  529. r = Math.toIntExact(Math.round(r + (0xFF-r)*FACTOR));
  530. g = Math.toIntExact(Math.round(g + (0xFF-g)*FACTOR));
  531. b = Math.toIntExact(Math.round(b + (0xFF-b)*FACTOR));
  532. return new Color(r,g,b);
  533. }
  534. default:
  535. // TODO ...
  536. break;
  537. }
  538. return col;
  539. }
  540. double getAlpha(EscherPropertyTypes opacityProperty) {
  541. AbstractEscherOptRecord opt = getEscherOptRecord();
  542. EscherSimpleProperty op = getEscherProperty(opt, opacityProperty);
  543. int defaultOpacity = 0x00010000;
  544. int opacity = (op == null) ? defaultOpacity : op.getPropertyValue();
  545. return Units.fixedPointToDouble(opacity);
  546. }
  547. Color toRGB(int val){
  548. int a = (val >> 24) & 0xFF;
  549. int b = (val >> 16) & 0xFF;
  550. int g = (val >> 8) & 0xFF;
  551. int r = (val >> 0) & 0xFF;
  552. if(a == 0xFE){
  553. // Color is an sRGB value specified by red, green, and blue fields.
  554. } else if (a == 0xFF){
  555. // Color is undefined.
  556. } else {
  557. // index in the color scheme
  558. ColorSchemeAtom ca = getSheet().getColorScheme();
  559. int schemeColor = ca.getColor(a);
  560. r = (schemeColor >> 0) & 0xFF;
  561. g = (schemeColor >> 8) & 0xFF;
  562. b = (schemeColor >> 16) & 0xFF;
  563. }
  564. return new Color(r, g, b);
  565. }
  566. @Override
  567. public int getShapeId(){
  568. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  569. return spRecord == null ? 0 : spRecord.getShapeId();
  570. }
  571. /**
  572. * Sets shape ID
  573. *
  574. * @param id of the shape
  575. */
  576. public void setShapeId(int id){
  577. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  578. if(spRecord != null) spRecord.setShapeId(id);
  579. }
  580. /**
  581. * Fill properties of this shape
  582. *
  583. * @return fill properties of this shape
  584. */
  585. public HSLFFill getFill(){
  586. if(_fill == null) {
  587. _fill = new HSLFFill(this);
  588. }
  589. return _fill;
  590. }
  591. public FillStyle getFillStyle() {
  592. return getFill().getFillStyle();
  593. }
  594. @Override
  595. public void draw(Graphics2D graphics, Rectangle2D bounds){
  596. DrawFactory.getInstance(graphics).drawShape(graphics, this, bounds);
  597. }
  598. public AbstractEscherOptRecord getEscherOptRecord() {
  599. AbstractEscherOptRecord opt = getEscherChild(EscherRecordTypes.OPT);
  600. if (opt == null) {
  601. opt = getEscherChild(EscherRecordTypes.USER_DEFINED);
  602. }
  603. return opt;
  604. }
  605. public boolean getFlipHorizontal(){
  606. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  607. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPHORIZ) != 0;
  608. }
  609. public void setFlipHorizontal(boolean flip) {
  610. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  611. int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPHORIZ;
  612. spRecord.setFlags(flag);
  613. }
  614. public boolean getFlipVertical(){
  615. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  616. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPVERT) != 0;
  617. }
  618. public void setFlipVertical(boolean flip) {
  619. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  620. int flag = spRecord.getFlags() | EscherSpRecord.FLAG_FLIPVERT;
  621. spRecord.setFlags(flag);
  622. }
  623. public double getRotation(){
  624. int rot = getEscherProperty(EscherPropertyTypes.TRANSFORM__ROTATION);
  625. return Units.fixedPointToDouble(rot);
  626. }
  627. public void setRotation(double theta){
  628. int rot = Units.doubleToFixedPoint(theta % 360.0);
  629. setEscherProperty(EscherPropertyTypes.TRANSFORM__ROTATION, rot);
  630. }
  631. public boolean isPlaceholder() {
  632. return false;
  633. }
  634. /**
  635. * Find a record in the underlying EscherClientDataRecord
  636. *
  637. * @param recordType type of the record to search
  638. */
  639. @SuppressWarnings("unchecked")
  640. public <T extends Record> T getClientDataRecord(int recordType) {
  641. List<? extends Record> records = getClientRecords();
  642. if (records != null) for (Record r : records) {
  643. if (r.getRecordType() == recordType){
  644. return (T)r;
  645. }
  646. }
  647. return null;
  648. }
  649. /**
  650. * Search for EscherClientDataRecord, if found, convert its contents into an array of HSLF records
  651. *
  652. * @return an array of HSLF records contained in the shape's EscherClientDataRecord or {@code null}
  653. */
  654. protected List<? extends Record> getClientRecords() {
  655. HSLFEscherClientDataRecord clientData = getClientData(false);
  656. return (clientData == null) ? null : clientData.getHSLFChildRecords();
  657. }
  658. /**
  659. * Create a new HSLF-specific EscherClientDataRecord
  660. *
  661. * @param create if true, create the missing record
  662. * @return the client record or null if it was missing and create wasn't activated
  663. */
  664. protected HSLFEscherClientDataRecord getClientData(boolean create) {
  665. HSLFEscherClientDataRecord clientData = getEscherChild(EscherClientDataRecord.RECORD_ID);
  666. if (clientData == null && create) {
  667. clientData = new HSLFEscherClientDataRecord();
  668. clientData.setOptions((short)15);
  669. clientData.setRecordId(EscherClientDataRecord.RECORD_ID);
  670. getSpContainer().addChildBefore(clientData, EscherTextboxRecord.RECORD_ID);
  671. }
  672. return clientData;
  673. }
  674. }