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.

HSLFSimpleShape.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.io.ByteArrayOutputStream;
  18. import org.apache.poi.ddf.*;
  19. import org.apache.poi.hslf.exceptions.HSLFException;
  20. import org.apache.poi.hslf.record.*;
  21. import org.apache.poi.sl.draw.DrawPaint;
  22. import org.apache.poi.sl.draw.geom.*;
  23. import org.apache.poi.sl.usermodel.*;
  24. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  25. import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound;
  26. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  27. import org.apache.poi.util.LittleEndian;
  28. import org.apache.poi.util.Units;
  29. /**
  30. * An abstract simple (non-group) shape.
  31. * This is the parent class for all primitive shapes like Line, Rectangle, etc.
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public abstract class HSLFSimpleShape extends HSLFShape implements SimpleShape {
  36. public final static double DEFAULT_LINE_WIDTH = 0.75;
  37. /**
  38. * Records stored in EscherClientDataRecord
  39. */
  40. protected Record[] _clientRecords;
  41. protected EscherClientDataRecord _clientData;
  42. /**
  43. * Create a SimpleShape object and initialize it from the supplied Record container.
  44. *
  45. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  46. * @param parent the parent of the shape
  47. */
  48. protected HSLFSimpleShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape> parent){
  49. super(escherRecord, parent);
  50. }
  51. /**
  52. * Create a new Shape
  53. *
  54. * @param isChild <code>true</code> if the Line is inside a group, <code>false</code> otherwise
  55. * @return the record container which holds this shape
  56. */
  57. protected EscherContainerRecord createSpContainer(boolean isChild) {
  58. _escherContainer = new EscherContainerRecord();
  59. _escherContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  60. _escherContainer.setOptions((short)15);
  61. EscherSpRecord sp = new EscherSpRecord();
  62. int flags = EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE;
  63. if (isChild) flags |= EscherSpRecord.FLAG_CHILD;
  64. sp.setFlags(flags);
  65. _escherContainer.addChildRecord(sp);
  66. EscherOptRecord opt = new EscherOptRecord();
  67. opt.setRecordId(EscherOptRecord.RECORD_ID);
  68. _escherContainer.addChildRecord(opt);
  69. EscherRecord anchor;
  70. if(isChild) anchor = new EscherChildAnchorRecord();
  71. else {
  72. anchor = new EscherClientAnchorRecord();
  73. //hack. internal variable EscherClientAnchorRecord.shortRecord can be
  74. //initialized only in fillFields(). We need to set shortRecord=false;
  75. byte[] header = new byte[16];
  76. LittleEndian.putUShort(header, 0, 0);
  77. LittleEndian.putUShort(header, 2, 0);
  78. LittleEndian.putInt(header, 4, 8);
  79. anchor.fillFields(header, 0, null);
  80. }
  81. _escherContainer.addChildRecord(anchor);
  82. return _escherContainer;
  83. }
  84. /**
  85. * Returns width of the line in in points
  86. */
  87. public double getLineWidth(){
  88. EscherOptRecord opt = getEscherOptRecord();
  89. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH);
  90. double width = (prop == null) ? DEFAULT_LINE_WIDTH : Units.toPoints(prop.getPropertyValue());
  91. return width;
  92. }
  93. /**
  94. * Sets the width of line in in points
  95. * @param width the width of line in in points
  96. */
  97. public void setLineWidth(double width){
  98. EscherOptRecord opt = getEscherOptRecord();
  99. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH, Units.toEMU(width));
  100. }
  101. /**
  102. * Sets the color of line
  103. *
  104. * @param color new color of the line
  105. */
  106. public void setLineColor(Color color){
  107. EscherOptRecord opt = getEscherOptRecord();
  108. if (color == null) {
  109. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80000);
  110. } else {
  111. int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
  112. setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb);
  113. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, color == null ? 0x180010 : 0x180018);
  114. }
  115. }
  116. /**
  117. * @return color of the line. If color is not set returns <code>java.awt.Color.black</code>
  118. */
  119. public Color getLineColor(){
  120. EscherOptRecord opt = getEscherOptRecord();
  121. EscherSimpleProperty p = getEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  122. if(p != null && (p.getPropertyValue() & 0x8) == 0) return null;
  123. Color clr = getColor(EscherProperties.LINESTYLE__COLOR, EscherProperties.LINESTYLE__OPACITY, -1);
  124. return clr == null ? Color.black : clr;
  125. }
  126. /**
  127. * Gets line dashing.
  128. *
  129. * @return dashing of the line.
  130. */
  131. public LineDash getLineDashing(){
  132. EscherOptRecord opt = getEscherOptRecord();
  133. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
  134. return (prop == null) ? LineDash.SOLID : LineDash.fromNativeId(prop.getPropertyValue());
  135. }
  136. /**
  137. * Sets line dashing.
  138. *
  139. * @param pen new style of the line.
  140. */
  141. public void setLineDashing(LineDash pen){
  142. EscherOptRecord opt = getEscherOptRecord();
  143. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING, pen == LineDash.SOLID ? -1 : pen.nativeId);
  144. }
  145. /**
  146. * Gets the line compound style
  147. *
  148. * @return the compound style of the line.
  149. */
  150. public LineCompound getLineCompound() {
  151. EscherOptRecord opt = getEscherOptRecord();
  152. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE);
  153. return (prop == null) ? LineCompound.SINGLE : LineCompound.fromNativeId(prop.getPropertyValue());
  154. }
  155. /**
  156. * Sets the line compound style
  157. *
  158. * @param style new compound style of the line.
  159. */
  160. public void setLineCompound(LineCompound style){
  161. EscherOptRecord opt = getEscherOptRecord();
  162. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE, style == LineCompound.SINGLE ? -1 : style.nativeId);
  163. }
  164. /**
  165. * Returns line style. One of the constants defined in this class.
  166. *
  167. * @return style of the line.
  168. */
  169. public StrokeStyle getStrokeStyle(){
  170. return new StrokeStyle() {
  171. public PaintStyle getPaint() {
  172. return DrawPaint.createSolidPaint(HSLFSimpleShape.this.getLineColor());
  173. }
  174. public LineCap getLineCap() {
  175. return null;
  176. }
  177. public LineDash getLineDash() {
  178. return HSLFSimpleShape.this.getLineDashing();
  179. }
  180. public LineCompound getLineCompound() {
  181. return HSLFSimpleShape.this.getLineCompound();
  182. }
  183. public double getLineWidth() {
  184. return HSLFSimpleShape.this.getLineWidth();
  185. }
  186. };
  187. }
  188. /**
  189. * The color used to fill this shape.
  190. */
  191. public Color getFillColor(){
  192. return getFill().getForegroundColor();
  193. }
  194. /**
  195. * The color used to fill this shape.
  196. *
  197. * @param color the background color
  198. */
  199. public void setFillColor(Color color){
  200. getFill().setForegroundColor(color);
  201. }
  202. /**
  203. * Find a record in the underlying EscherClientDataRecord
  204. *
  205. * @param recordType type of the record to search
  206. */
  207. @SuppressWarnings("unchecked")
  208. protected <T extends Record> T getClientDataRecord(int recordType) {
  209. Record[] records = getClientRecords();
  210. if(records != null) for (int i = 0; i < records.length; i++) {
  211. if(records[i].getRecordType() == recordType){
  212. return (T)records[i];
  213. }
  214. }
  215. return null;
  216. }
  217. /**
  218. * Search for EscherClientDataRecord, if found, convert its contents into an array of HSLF records
  219. *
  220. * @return an array of HSLF records contained in the shape's EscherClientDataRecord or <code>null</code>
  221. */
  222. protected Record[] getClientRecords() {
  223. if(_clientData == null){
  224. EscherRecord r = getEscherChild(EscherClientDataRecord.RECORD_ID);
  225. //ddf can return EscherContainerRecord with recordId=EscherClientDataRecord.RECORD_ID
  226. //convert in to EscherClientDataRecord on the fly
  227. if(r != null && !(r instanceof EscherClientDataRecord)){
  228. byte[] data = r.serialize();
  229. r = new EscherClientDataRecord();
  230. r.fillFields(data, 0, new DefaultEscherRecordFactory());
  231. }
  232. _clientData = (EscherClientDataRecord)r;
  233. }
  234. if(_clientData != null && _clientRecords == null){
  235. byte[] data = _clientData.getRemainingData();
  236. _clientRecords = Record.findChildRecords(data, 0, data.length);
  237. }
  238. return _clientRecords;
  239. }
  240. protected void updateClientData() {
  241. if(_clientData != null && _clientRecords != null){
  242. ByteArrayOutputStream out = new ByteArrayOutputStream();
  243. try {
  244. for (int i = 0; i < _clientRecords.length; i++) {
  245. _clientRecords[i].writeOut(out);
  246. }
  247. } catch(Exception e){
  248. throw new HSLFException(e);
  249. }
  250. _clientData.setRemainingData(out.toByteArray());
  251. }
  252. }
  253. public void setHyperlink(HSLFHyperlink link){
  254. if(link.getId() == -1){
  255. throw new HSLFException("You must call SlideShow.addHyperlink(Hyperlink link) first");
  256. }
  257. EscherClientDataRecord cldata = new EscherClientDataRecord();
  258. cldata.setOptions((short)0xF);
  259. getSpContainer().addChildRecord(cldata); // TODO - junit to prove getChildRecords().add is wrong
  260. InteractiveInfo info = new InteractiveInfo();
  261. InteractiveInfoAtom infoAtom = info.getInteractiveInfoAtom();
  262. switch(link.getType()){
  263. case HSLFHyperlink.LINK_FIRSTSLIDE:
  264. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  265. infoAtom.setJump(InteractiveInfoAtom.JUMP_FIRSTSLIDE);
  266. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_FirstSlide);
  267. break;
  268. case HSLFHyperlink.LINK_LASTSLIDE:
  269. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  270. infoAtom.setJump(InteractiveInfoAtom.JUMP_LASTSLIDE);
  271. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_LastSlide);
  272. break;
  273. case HSLFHyperlink.LINK_NEXTSLIDE:
  274. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  275. infoAtom.setJump(InteractiveInfoAtom.JUMP_NEXTSLIDE);
  276. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_NextSlide);
  277. break;
  278. case HSLFHyperlink.LINK_PREVIOUSSLIDE:
  279. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  280. infoAtom.setJump(InteractiveInfoAtom.JUMP_PREVIOUSSLIDE);
  281. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_PreviousSlide);
  282. break;
  283. case HSLFHyperlink.LINK_URL:
  284. infoAtom.setAction(InteractiveInfoAtom.ACTION_HYPERLINK);
  285. infoAtom.setJump(InteractiveInfoAtom.JUMP_NONE);
  286. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_Url);
  287. break;
  288. case HSLFHyperlink.LINK_SLIDENUMBER:
  289. infoAtom.setAction(InteractiveInfoAtom.ACTION_HYPERLINK);
  290. infoAtom.setJump(InteractiveInfoAtom.JUMP_NONE);
  291. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_SlideNumber);
  292. break;
  293. }
  294. infoAtom.setHyperlinkID(link.getId());
  295. ByteArrayOutputStream out = new ByteArrayOutputStream();
  296. try {
  297. info.writeOut(out);
  298. } catch(Exception e){
  299. throw new HSLFException(e);
  300. }
  301. cldata.setRemainingData(out.toByteArray());
  302. }
  303. public Guide getAdjustValue(String name) {
  304. if (name == null || !name.matches("adj([1-9]|10)")) {
  305. throw new IllegalArgumentException("Adjust value '"+name+"' not supported.");
  306. }
  307. short escherProp;
  308. switch (Integer.parseInt(name.substring(3))) {
  309. case 1: escherProp = EscherProperties.GEOMETRY__ADJUSTVALUE; break;
  310. case 2: escherProp = EscherProperties.GEOMETRY__ADJUST2VALUE; break;
  311. case 3: escherProp = EscherProperties.GEOMETRY__ADJUST3VALUE; break;
  312. case 4: escherProp = EscherProperties.GEOMETRY__ADJUST4VALUE; break;
  313. case 5: escherProp = EscherProperties.GEOMETRY__ADJUST5VALUE; break;
  314. case 6: escherProp = EscherProperties.GEOMETRY__ADJUST6VALUE; break;
  315. case 7: escherProp = EscherProperties.GEOMETRY__ADJUST7VALUE; break;
  316. case 8: escherProp = EscherProperties.GEOMETRY__ADJUST8VALUE; break;
  317. case 9: escherProp = EscherProperties.GEOMETRY__ADJUST9VALUE; break;
  318. case 10: escherProp = EscherProperties.GEOMETRY__ADJUST10VALUE; break;
  319. default: throw new RuntimeException();
  320. }
  321. int adjval = getEscherProperty(escherProp, -1);
  322. return (adjval == -1) ? null : new Guide(name, "val "+adjval);
  323. }
  324. public CustomGeometry getGeometry() {
  325. ShapeType st = getShapeType();
  326. String name = st.getOoxmlName();
  327. PresetGeometries dict = PresetGeometries.getInstance();
  328. CustomGeometry geom = dict.get(name);
  329. if(geom == null) {
  330. throw new IllegalStateException("Unknown shape geometry: " + name);
  331. }
  332. return geom;
  333. }
  334. public double getShadowAngle() {
  335. EscherOptRecord opt = getEscherOptRecord();
  336. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETX);
  337. int offX = (prop == null) ? 0 : prop.getPropertyValue();
  338. prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETY);
  339. int offY = (prop == null) ? 0 : prop.getPropertyValue();
  340. return Math.toDegrees(Math.atan2(offY, offX));
  341. }
  342. public double getShadowDistance() {
  343. EscherOptRecord opt = getEscherOptRecord();
  344. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETX);
  345. int offX = (prop == null) ? 0 : prop.getPropertyValue();
  346. prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETY);
  347. int offY = (prop == null) ? 0 : prop.getPropertyValue();
  348. return Units.toPoints((long)Math.hypot(offX, offY));
  349. }
  350. /**
  351. * @return color of the line. If color is not set returns <code>java.awt.Color.black</code>
  352. */
  353. public Color getShadowColor(){
  354. Color clr = getColor(EscherProperties.SHADOWSTYLE__COLOR, EscherProperties.SHADOWSTYLE__OPACITY, -1);
  355. return clr == null ? Color.black : clr;
  356. }
  357. public Shadow getShadow() {
  358. EscherOptRecord opt = getEscherOptRecord();
  359. EscherProperty shadowType = opt.lookup(EscherProperties.SHADOWSTYLE__TYPE);
  360. if (shadowType == null) return null;
  361. return new Shadow(){
  362. public SimpleShape getShadowParent() {
  363. return HSLFSimpleShape.this;
  364. }
  365. public double getDistance() {
  366. return getShadowDistance();
  367. }
  368. public double getAngle() {
  369. return getShadowAngle();
  370. }
  371. public double getBlur() {
  372. // TODO Auto-generated method stub
  373. return 0;
  374. }
  375. public SolidPaint getFillStyle() {
  376. return DrawPaint.createSolidPaint(getShadowColor());
  377. }
  378. };
  379. }
  380. public LineDecoration getLineDecoration() {
  381. return new LineDecoration() {
  382. public DecorationShape getHeadShape() {
  383. return DecorationShape.NONE;
  384. }
  385. public DecorationSize getHeadWidth() {
  386. return DecorationSize.MEDIUM;
  387. }
  388. public DecorationSize getHeadLength() {
  389. return DecorationSize.MEDIUM;
  390. }
  391. public DecorationShape getTailShape() {
  392. return DecorationShape.NONE;
  393. }
  394. public DecorationSize getTailWidth() {
  395. return DecorationSize.MEDIUM;
  396. }
  397. public DecorationSize getTailLength() {
  398. return DecorationSize.MEDIUM;
  399. }
  400. };
  401. }
  402. }