Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

HSLFSimpleShape.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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.util.List;
  18. import org.apache.poi.ddf.AbstractEscherOptRecord;
  19. import org.apache.poi.ddf.EscherChildAnchorRecord;
  20. import org.apache.poi.ddf.EscherClientAnchorRecord;
  21. import org.apache.poi.ddf.EscherContainerRecord;
  22. import org.apache.poi.ddf.EscherOptRecord;
  23. import org.apache.poi.ddf.EscherProperties;
  24. import org.apache.poi.ddf.EscherProperty;
  25. import org.apache.poi.ddf.EscherRecord;
  26. import org.apache.poi.ddf.EscherSimpleProperty;
  27. import org.apache.poi.ddf.EscherSpRecord;
  28. import org.apache.poi.hslf.exceptions.HSLFException;
  29. import org.apache.poi.hslf.record.HSLFEscherClientDataRecord;
  30. import org.apache.poi.hslf.record.InteractiveInfo;
  31. import org.apache.poi.hslf.record.InteractiveInfoAtom;
  32. import org.apache.poi.hslf.record.OEPlaceholderAtom;
  33. import org.apache.poi.hslf.record.Record;
  34. import org.apache.poi.hslf.record.RoundTripHFPlaceholder12;
  35. import org.apache.poi.sl.draw.DrawPaint;
  36. import org.apache.poi.sl.draw.geom.CustomGeometry;
  37. import org.apache.poi.sl.draw.geom.Guide;
  38. import org.apache.poi.sl.draw.geom.PresetGeometries;
  39. import org.apache.poi.sl.usermodel.LineDecoration;
  40. import org.apache.poi.sl.usermodel.LineDecoration.DecorationShape;
  41. import org.apache.poi.sl.usermodel.LineDecoration.DecorationSize;
  42. import org.apache.poi.sl.usermodel.PaintStyle;
  43. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  44. import org.apache.poi.sl.usermodel.Placeholder;
  45. import org.apache.poi.sl.usermodel.Shadow;
  46. import org.apache.poi.sl.usermodel.ShapeContainer;
  47. import org.apache.poi.sl.usermodel.ShapeType;
  48. import org.apache.poi.sl.usermodel.SimpleShape;
  49. import org.apache.poi.sl.usermodel.StrokeStyle;
  50. import org.apache.poi.sl.usermodel.StrokeStyle.LineCap;
  51. import org.apache.poi.sl.usermodel.StrokeStyle.LineCompound;
  52. import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
  53. import org.apache.poi.util.LittleEndian;
  54. import org.apache.poi.util.POILogger;
  55. import org.apache.poi.util.Units;
  56. /**
  57. * An abstract simple (non-group) shape.
  58. * This is the parent class for all primitive shapes like Line, Rectangle, etc.
  59. *
  60. * @author Yegor Kozlov
  61. */
  62. public abstract class HSLFSimpleShape extends HSLFShape implements SimpleShape<HSLFShape,HSLFTextParagraph> {
  63. public final static double DEFAULT_LINE_WIDTH = 0.75;
  64. /**
  65. * Create a SimpleShape object and initialize it from the supplied Record container.
  66. *
  67. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  68. * @param parent the parent of the shape
  69. */
  70. protected HSLFSimpleShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  71. super(escherRecord, parent);
  72. }
  73. /**
  74. * Create a new Shape
  75. *
  76. * @param isChild <code>true</code> if the Line is inside a group, <code>false</code> otherwise
  77. * @return the record container which holds this shape
  78. */
  79. protected EscherContainerRecord createSpContainer(boolean isChild) {
  80. _escherContainer = new EscherContainerRecord();
  81. _escherContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  82. _escherContainer.setOptions((short)15);
  83. EscherSpRecord sp = new EscherSpRecord();
  84. int flags = EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE;
  85. if (isChild) flags |= EscherSpRecord.FLAG_CHILD;
  86. sp.setFlags(flags);
  87. _escherContainer.addChildRecord(sp);
  88. AbstractEscherOptRecord opt = new EscherOptRecord();
  89. opt.setRecordId(EscherOptRecord.RECORD_ID);
  90. _escherContainer.addChildRecord(opt);
  91. EscherRecord anchor;
  92. if(isChild) {
  93. anchor = new EscherChildAnchorRecord();
  94. } else {
  95. anchor = new EscherClientAnchorRecord();
  96. //hack. internal variable EscherClientAnchorRecord.shortRecord can be
  97. //initialized only in fillFields(). We need to set shortRecord=false;
  98. byte[] header = new byte[16];
  99. LittleEndian.putUShort(header, 0, 0);
  100. LittleEndian.putUShort(header, 2, 0);
  101. LittleEndian.putInt(header, 4, 8);
  102. anchor.fillFields(header, 0, null);
  103. }
  104. _escherContainer.addChildRecord(anchor);
  105. return _escherContainer;
  106. }
  107. /**
  108. * Returns width of the line in in points
  109. */
  110. public double getLineWidth(){
  111. AbstractEscherOptRecord opt = getEscherOptRecord();
  112. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH);
  113. double width = (prop == null) ? DEFAULT_LINE_WIDTH : Units.toPoints(prop.getPropertyValue());
  114. return width;
  115. }
  116. /**
  117. * Sets the width of line in in points
  118. * @param width the width of line in in points
  119. */
  120. public void setLineWidth(double width){
  121. AbstractEscherOptRecord opt = getEscherOptRecord();
  122. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH, Units.toEMU(width));
  123. }
  124. /**
  125. * Sets the color of line
  126. *
  127. * @param color new color of the line
  128. */
  129. public void setLineColor(Color color){
  130. AbstractEscherOptRecord opt = getEscherOptRecord();
  131. if (color == null) {
  132. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80000);
  133. } else {
  134. int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
  135. setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb);
  136. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x180018);
  137. }
  138. }
  139. /**
  140. * @return color of the line. If color is not set returns <code>java.awt.Color.black</code>
  141. */
  142. public Color getLineColor(){
  143. AbstractEscherOptRecord opt = getEscherOptRecord();
  144. EscherSimpleProperty p = getEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  145. if(p != null && (p.getPropertyValue() & 0x8) == 0) return null;
  146. Color clr = getColor(EscherProperties.LINESTYLE__COLOR, EscherProperties.LINESTYLE__OPACITY, -1);
  147. return clr == null ? Color.black : clr;
  148. }
  149. /**
  150. * Gets line cap.
  151. *
  152. * @return cap of the line.
  153. */
  154. public LineCap getLineCap(){
  155. AbstractEscherOptRecord opt = getEscherOptRecord();
  156. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDCAPSTYLE);
  157. return (prop == null) ? LineCap.FLAT : LineCap.fromNativeId(prop.getPropertyValue());
  158. }
  159. /**
  160. * Sets line cap.
  161. *
  162. * @param pen new style of the line.
  163. */
  164. public void setLineCap(LineCap pen){
  165. AbstractEscherOptRecord opt = getEscherOptRecord();
  166. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDCAPSTYLE, pen == LineCap.FLAT ? -1 : pen.nativeId);
  167. }
  168. /**
  169. * Gets line dashing.
  170. *
  171. * @return dashing of the line.
  172. */
  173. public LineDash getLineDash(){
  174. AbstractEscherOptRecord opt = getEscherOptRecord();
  175. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
  176. return (prop == null) ? LineDash.SOLID : LineDash.fromNativeId(prop.getPropertyValue());
  177. }
  178. /**
  179. * Sets line dashing.
  180. *
  181. * @param pen new style of the line.
  182. */
  183. public void setLineDash(LineDash pen){
  184. AbstractEscherOptRecord opt = getEscherOptRecord();
  185. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING, pen == LineDash.SOLID ? -1 : pen.nativeId);
  186. }
  187. /**
  188. * Gets the line compound style
  189. *
  190. * @return the compound style of the line.
  191. */
  192. public LineCompound getLineCompound() {
  193. AbstractEscherOptRecord opt = getEscherOptRecord();
  194. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE);
  195. return (prop == null) ? LineCompound.SINGLE : LineCompound.fromNativeId(prop.getPropertyValue());
  196. }
  197. /**
  198. * Sets the line compound style
  199. *
  200. * @param style new compound style of the line.
  201. */
  202. public void setLineCompound(LineCompound style){
  203. AbstractEscherOptRecord opt = getEscherOptRecord();
  204. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE, style == LineCompound.SINGLE ? -1 : style.nativeId);
  205. }
  206. /**
  207. * Returns line style. One of the constants defined in this class.
  208. *
  209. * @return style of the line.
  210. */
  211. public StrokeStyle getStrokeStyle(){
  212. return new StrokeStyle() {
  213. public PaintStyle getPaint() {
  214. return DrawPaint.createSolidPaint(HSLFSimpleShape.this.getLineColor());
  215. }
  216. public LineCap getLineCap() {
  217. return null;
  218. }
  219. public LineDash getLineDash() {
  220. return HSLFSimpleShape.this.getLineDash();
  221. }
  222. public LineCompound getLineCompound() {
  223. return HSLFSimpleShape.this.getLineCompound();
  224. }
  225. public double getLineWidth() {
  226. return HSLFSimpleShape.this.getLineWidth();
  227. }
  228. };
  229. }
  230. @Override
  231. public Color getFillColor() {
  232. return getFill().getForegroundColor();
  233. }
  234. @Override
  235. public void setFillColor(Color color) {
  236. getFill().setForegroundColor(color);
  237. }
  238. public void setHyperlink(HSLFHyperlink link){
  239. if(link.getId() == -1){
  240. throw new HSLFException("You must call SlideShow.addHyperlink(Hyperlink link) first");
  241. }
  242. InteractiveInfo info = new InteractiveInfo();
  243. InteractiveInfoAtom infoAtom = info.getInteractiveInfoAtom();
  244. switch(link.getType()){
  245. case HSLFHyperlink.LINK_FIRSTSLIDE:
  246. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  247. infoAtom.setJump(InteractiveInfoAtom.JUMP_FIRSTSLIDE);
  248. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_FirstSlide);
  249. break;
  250. case HSLFHyperlink.LINK_LASTSLIDE:
  251. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  252. infoAtom.setJump(InteractiveInfoAtom.JUMP_LASTSLIDE);
  253. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_LastSlide);
  254. break;
  255. case HSLFHyperlink.LINK_NEXTSLIDE:
  256. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  257. infoAtom.setJump(InteractiveInfoAtom.JUMP_NEXTSLIDE);
  258. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_NextSlide);
  259. break;
  260. case HSLFHyperlink.LINK_PREVIOUSSLIDE:
  261. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  262. infoAtom.setJump(InteractiveInfoAtom.JUMP_PREVIOUSSLIDE);
  263. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_PreviousSlide);
  264. break;
  265. case HSLFHyperlink.LINK_URL:
  266. infoAtom.setAction(InteractiveInfoAtom.ACTION_HYPERLINK);
  267. infoAtom.setJump(InteractiveInfoAtom.JUMP_NONE);
  268. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_Url);
  269. break;
  270. case HSLFHyperlink.LINK_SLIDENUMBER:
  271. infoAtom.setAction(InteractiveInfoAtom.ACTION_HYPERLINK);
  272. infoAtom.setJump(InteractiveInfoAtom.JUMP_NONE);
  273. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_SlideNumber);
  274. break;
  275. default:
  276. logger.log(POILogger.WARN, "Ignore unknown hyperlink type : "+link.getLabel());
  277. break;
  278. }
  279. infoAtom.setHyperlinkID(link.getId());
  280. HSLFEscherClientDataRecord cldata = getClientData(true);
  281. cldata.addChild(infoAtom);
  282. }
  283. public Guide getAdjustValue(String name) {
  284. if (name == null || !name.matches("adj([1-9]|10)?")) {
  285. throw new IllegalArgumentException("Adjust value '"+name+"' not supported.");
  286. }
  287. name = name.replace("adj", "");
  288. if ("".equals(name)) name = "1";
  289. short escherProp;
  290. switch (Integer.parseInt(name)) {
  291. case 1: escherProp = EscherProperties.GEOMETRY__ADJUSTVALUE; break;
  292. case 2: escherProp = EscherProperties.GEOMETRY__ADJUST2VALUE; break;
  293. case 3: escherProp = EscherProperties.GEOMETRY__ADJUST3VALUE; break;
  294. case 4: escherProp = EscherProperties.GEOMETRY__ADJUST4VALUE; break;
  295. case 5: escherProp = EscherProperties.GEOMETRY__ADJUST5VALUE; break;
  296. case 6: escherProp = EscherProperties.GEOMETRY__ADJUST6VALUE; break;
  297. case 7: escherProp = EscherProperties.GEOMETRY__ADJUST7VALUE; break;
  298. case 8: escherProp = EscherProperties.GEOMETRY__ADJUST8VALUE; break;
  299. case 9: escherProp = EscherProperties.GEOMETRY__ADJUST9VALUE; break;
  300. case 10: escherProp = EscherProperties.GEOMETRY__ADJUST10VALUE; break;
  301. default: throw new RuntimeException();
  302. }
  303. int adjval = getEscherProperty(escherProp, -1);
  304. return (adjval == -1) ? null : new Guide(name, "val "+adjval);
  305. }
  306. public CustomGeometry getGeometry() {
  307. PresetGeometries dict = PresetGeometries.getInstance();
  308. ShapeType st = getShapeType();
  309. String name = st.getOoxmlName();
  310. CustomGeometry geom = dict.get(name);
  311. if(geom == null) {
  312. if (name == null && st != null) name = st.toString();
  313. logger.log(POILogger.WARN, "No preset shape definition for shapeType: "+name);
  314. return null;
  315. }
  316. return geom;
  317. }
  318. public double getShadowAngle() {
  319. AbstractEscherOptRecord opt = getEscherOptRecord();
  320. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETX);
  321. int offX = (prop == null) ? 0 : prop.getPropertyValue();
  322. prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETY);
  323. int offY = (prop == null) ? 0 : prop.getPropertyValue();
  324. return Math.toDegrees(Math.atan2(offY, offX));
  325. }
  326. public double getShadowDistance() {
  327. AbstractEscherOptRecord opt = getEscherOptRecord();
  328. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETX);
  329. int offX = (prop == null) ? 0 : prop.getPropertyValue();
  330. prop = getEscherProperty(opt, EscherProperties.SHADOWSTYLE__OFFSETY);
  331. int offY = (prop == null) ? 0 : prop.getPropertyValue();
  332. return Units.toPoints((long)Math.hypot(offX, offY));
  333. }
  334. /**
  335. * @return color of the line. If color is not set returns <code>java.awt.Color.black</code>
  336. */
  337. public Color getShadowColor(){
  338. Color clr = getColor(EscherProperties.SHADOWSTYLE__COLOR, EscherProperties.SHADOWSTYLE__OPACITY, -1);
  339. return clr == null ? Color.black : clr;
  340. }
  341. public Shadow<HSLFShape,HSLFTextParagraph> getShadow() {
  342. AbstractEscherOptRecord opt = getEscherOptRecord();
  343. EscherProperty shadowType = opt.lookup(EscherProperties.SHADOWSTYLE__TYPE);
  344. if (shadowType == null) return null;
  345. return new Shadow<HSLFShape,HSLFTextParagraph>(){
  346. public SimpleShape<HSLFShape,HSLFTextParagraph> getShadowParent() {
  347. return HSLFSimpleShape.this;
  348. }
  349. public double getDistance() {
  350. return getShadowDistance();
  351. }
  352. public double getAngle() {
  353. return getShadowAngle();
  354. }
  355. public double getBlur() {
  356. // TODO Auto-generated method stub
  357. return 0;
  358. }
  359. public SolidPaint getFillStyle() {
  360. return DrawPaint.createSolidPaint(getShadowColor());
  361. }
  362. };
  363. }
  364. public DecorationShape getLineHeadDecoration(){
  365. AbstractEscherOptRecord opt = getEscherOptRecord();
  366. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWHEAD);
  367. return (prop == null) ? null : DecorationShape.fromNativeId(prop.getPropertyValue());
  368. }
  369. public void setLineHeadDecoration(DecorationShape decoShape){
  370. AbstractEscherOptRecord opt = getEscherOptRecord();
  371. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWHEAD, decoShape == null ? -1 : decoShape.nativeId);
  372. }
  373. public DecorationSize getLineHeadWidth(){
  374. AbstractEscherOptRecord opt = getEscherOptRecord();
  375. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWWIDTH);
  376. return (prop == null) ? null : DecorationSize.fromNativeId(prop.getPropertyValue());
  377. }
  378. public void setLineHeadWidth(DecorationSize decoSize){
  379. AbstractEscherOptRecord opt = getEscherOptRecord();
  380. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWWIDTH, decoSize == null ? -1 : decoSize.nativeId);
  381. }
  382. public DecorationSize getLineHeadLength(){
  383. AbstractEscherOptRecord opt = getEscherOptRecord();
  384. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWLENGTH);
  385. return (prop == null) ? null : DecorationSize.fromNativeId(prop.getPropertyValue());
  386. }
  387. public void setLineHeadLength(DecorationSize decoSize){
  388. AbstractEscherOptRecord opt = getEscherOptRecord();
  389. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTARTARROWLENGTH, decoSize == null ? -1 : decoSize.nativeId);
  390. }
  391. public DecorationShape getLineTailDecoration(){
  392. AbstractEscherOptRecord opt = getEscherOptRecord();
  393. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWHEAD);
  394. return (prop == null) ? null : DecorationShape.fromNativeId(prop.getPropertyValue());
  395. }
  396. public void setLineTailDecoration(DecorationShape decoShape){
  397. AbstractEscherOptRecord opt = getEscherOptRecord();
  398. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWHEAD, decoShape == null ? -1 : decoShape.nativeId);
  399. }
  400. public DecorationSize getLineTailWidth(){
  401. AbstractEscherOptRecord opt = getEscherOptRecord();
  402. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWWIDTH);
  403. return (prop == null) ? null : DecorationSize.fromNativeId(prop.getPropertyValue());
  404. }
  405. public void setLineTailWidth(DecorationSize decoSize){
  406. AbstractEscherOptRecord opt = getEscherOptRecord();
  407. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWWIDTH, decoSize == null ? -1 : decoSize.nativeId);
  408. }
  409. public DecorationSize getLineTailLength(){
  410. AbstractEscherOptRecord opt = getEscherOptRecord();
  411. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWLENGTH);
  412. return (prop == null) ? null : DecorationSize.fromNativeId(prop.getPropertyValue());
  413. }
  414. public void setLineTailLength(DecorationSize decoSize){
  415. AbstractEscherOptRecord opt = getEscherOptRecord();
  416. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEENDARROWLENGTH, decoSize == null ? -1 : decoSize.nativeId);
  417. }
  418. public LineDecoration getLineDecoration() {
  419. return new LineDecoration() {
  420. public DecorationShape getHeadShape() {
  421. return HSLFSimpleShape.this.getLineHeadDecoration();
  422. }
  423. public DecorationSize getHeadWidth() {
  424. return HSLFSimpleShape.this.getLineHeadWidth();
  425. }
  426. public DecorationSize getHeadLength() {
  427. return HSLFSimpleShape.this.getLineHeadLength();
  428. }
  429. public DecorationShape getTailShape() {
  430. return HSLFSimpleShape.this.getLineTailDecoration();
  431. }
  432. public DecorationSize getTailWidth() {
  433. return HSLFSimpleShape.this.getLineTailWidth();
  434. }
  435. public DecorationSize getTailLength() {
  436. return HSLFSimpleShape.this.getLineTailLength();
  437. }
  438. };
  439. }
  440. @Override
  441. public Placeholder getPlaceholder() {
  442. List<? extends Record> clRecords = getClientRecords();
  443. if (clRecords == null) {
  444. return null;
  445. }
  446. for (Record r : clRecords) {
  447. if (r instanceof OEPlaceholderAtom) {
  448. OEPlaceholderAtom oep = (OEPlaceholderAtom)r;
  449. return Placeholder.lookupNative(oep.getPlaceholderId());
  450. } else if (r instanceof RoundTripHFPlaceholder12) {
  451. RoundTripHFPlaceholder12 rtp = (RoundTripHFPlaceholder12)r;
  452. return Placeholder.lookupNative(rtp.getPlaceholderId());
  453. }
  454. }
  455. return null;
  456. }
  457. @Override
  458. public void setPlaceholder(Placeholder placeholder) {
  459. EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
  460. int flags = spRecord.getFlags();
  461. if (placeholder == null) {
  462. flags ^= EscherSpRecord.FLAG_HAVEMASTER;
  463. } else {
  464. flags |= EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HAVEMASTER;
  465. }
  466. spRecord.setFlags(flags);
  467. // Placeholders can't be grouped
  468. setEscherProperty(EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, (placeholder == null ? -1 : 262144));
  469. HSLFEscherClientDataRecord clientData = getClientData(false);
  470. if (placeholder == null) {
  471. if (clientData != null) {
  472. clientData.removeChild(OEPlaceholderAtom.class);
  473. clientData.removeChild(RoundTripHFPlaceholder12.class);
  474. // remove client data if the placeholder was the only child to be carried
  475. if (clientData.getChildRecords().isEmpty()) {
  476. getSpContainer().removeChildRecord(clientData);
  477. }
  478. }
  479. return;
  480. }
  481. if (clientData == null) {
  482. clientData = getClientData(true);
  483. }
  484. // OEPlaceholderAtom tells powerpoint that this shape is a placeholder
  485. OEPlaceholderAtom oep = null;
  486. RoundTripHFPlaceholder12 rtp = null;
  487. for (Record r : clientData.getHSLFChildRecords()) {
  488. if (r instanceof OEPlaceholderAtom) {
  489. oep = (OEPlaceholderAtom)r;
  490. break;
  491. }
  492. if (r instanceof RoundTripHFPlaceholder12) {
  493. rtp = (RoundTripHFPlaceholder12)r;
  494. break;
  495. }
  496. }
  497. /**
  498. * Extract from MSDN:
  499. *
  500. * There is a special case when the placeholder does not have a position in the layout.
  501. * This occurs when the user has moved the placeholder from its original position.
  502. * In this case the placeholder ID is -1.
  503. */
  504. byte phId;
  505. HSLFSheet sheet = getSheet();
  506. // TODO: implement/switch NotesMaster
  507. if (sheet instanceof HSLFSlideMaster) {
  508. phId = (byte)placeholder.nativeSlideMasterId;
  509. } else if (sheet instanceof HSLFNotes) {
  510. phId = (byte)placeholder.nativeNotesId;
  511. } else {
  512. phId = (byte)placeholder.nativeSlideId;
  513. }
  514. if (phId == -2) {
  515. throw new HSLFException("Placeholder "+placeholder.name()+" not supported for this sheet type ("+sheet.getClass()+")");
  516. }
  517. switch (placeholder) {
  518. case HEADER:
  519. case FOOTER:
  520. if (rtp == null) {
  521. rtp = new RoundTripHFPlaceholder12();
  522. rtp.setPlaceholderId(phId);
  523. clientData.addChild(rtp);
  524. }
  525. if (oep != null) {
  526. clientData.removeChild(OEPlaceholderAtom.class);
  527. }
  528. break;
  529. default:
  530. if (rtp != null) {
  531. clientData.removeChild(RoundTripHFPlaceholder12.class);
  532. }
  533. if (oep == null) {
  534. oep = new OEPlaceholderAtom();
  535. oep.setPlaceholderSize((byte)OEPlaceholderAtom.PLACEHOLDER_FULLSIZE);
  536. // TODO: placement id only "SHOULD" be unique ... check other placeholders on sheet for unique id
  537. oep.setPlacementId(-1);
  538. oep.setPlaceholderId(phId);
  539. clientData.addChild(oep);
  540. }
  541. break;
  542. }
  543. }
  544. @Override
  545. public void setStrokeStyle(Object... styles) {
  546. if (styles.length == 0) {
  547. // remove stroke
  548. setLineColor(null);
  549. return;
  550. }
  551. // TODO: handle PaintStyle
  552. for (Object st : styles) {
  553. if (st instanceof Number) {
  554. setLineWidth(((Number)st).doubleValue());
  555. } else if (st instanceof LineCap) {
  556. setLineCap((LineCap)st);
  557. } else if (st instanceof LineDash) {
  558. setLineDash((LineDash)st);
  559. } else if (st instanceof LineCompound) {
  560. setLineCompound((LineCompound)st);
  561. } else if (st instanceof Color) {
  562. setLineColor((Color)st);
  563. }
  564. }
  565. }
  566. }