Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

HSLFShape.java 26KB

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