PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/ppt/scratchpad/src/org/apache/poi/hslf/model/SimpleShape.java

https://github.com/minstrelsy/POI-Android
Java | 430 lines | 254 code | 55 blank | 121 comment | 49 complexity | 209d3abdc4c9f2d12e19f15421567bba MD5 | raw file
  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.model;
  16. import java.io.ByteArrayOutputStream;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import net.pbdavey.awt.Graphics2D;
  20. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  21. import org.apache.poi.ddf.EscherChildAnchorRecord;
  22. import org.apache.poi.ddf.EscherClientAnchorRecord;
  23. import org.apache.poi.ddf.EscherClientDataRecord;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.ddf.EscherOptRecord;
  26. import org.apache.poi.ddf.EscherProperties;
  27. import org.apache.poi.ddf.EscherRecord;
  28. import org.apache.poi.ddf.EscherSimpleProperty;
  29. import org.apache.poi.ddf.EscherSpRecord;
  30. import org.apache.poi.hslf.exceptions.HSLFException;
  31. import org.apache.poi.hslf.record.InteractiveInfo;
  32. import org.apache.poi.hslf.record.InteractiveInfoAtom;
  33. import org.apache.poi.hslf.record.Record;
  34. import org.apache.poi.util.LittleEndian;
  35. import and.awt.Color;
  36. import and.awt.geom.AffineTransform;
  37. import and.awt.geom.Rectangle2D;
  38. /**
  39. * An abstract simple (non-group) shape.
  40. * This is the parent class for all primitive shapes like Line, Rectangle, etc.
  41. *
  42. * @author Yegor Kozlov
  43. */
  44. public abstract class SimpleShape extends Shape {
  45. public final static double DEFAULT_LINE_WIDTH = 0.75;
  46. /**
  47. * Records stored in EscherClientDataRecord
  48. */
  49. protected Record[] _clientRecords;
  50. protected EscherClientDataRecord _clientData;
  51. /**
  52. * Create a SimpleShape object and initialize it from the supplied Record container.
  53. *
  54. * @param escherRecord <code>EscherSpContainer</code> container which holds information about this shape
  55. * @param parent the parent of the shape
  56. */
  57. protected SimpleShape(EscherContainerRecord escherRecord, Shape parent){
  58. super(escherRecord, parent);
  59. }
  60. /**
  61. * Create a new Shape
  62. *
  63. * @param isChild <code>true</code> if the Line is inside a group, <code>false</code> otherwise
  64. * @return the record container which holds this shape
  65. */
  66. protected EscherContainerRecord createSpContainer(boolean isChild) {
  67. _escherContainer = new EscherContainerRecord();
  68. _escherContainer.setRecordId( EscherContainerRecord.SP_CONTAINER );
  69. _escherContainer.setOptions((short)15);
  70. EscherSpRecord sp = new EscherSpRecord();
  71. int flags = EscherSpRecord.FLAG_HAVEANCHOR | EscherSpRecord.FLAG_HASSHAPETYPE;
  72. if (isChild) flags |= EscherSpRecord.FLAG_CHILD;
  73. sp.setFlags(flags);
  74. _escherContainer.addChildRecord(sp);
  75. EscherOptRecord opt = new EscherOptRecord();
  76. opt.setRecordId(EscherOptRecord.RECORD_ID);
  77. _escherContainer.addChildRecord(opt);
  78. EscherRecord anchor;
  79. if(isChild) anchor = new EscherChildAnchorRecord();
  80. else {
  81. anchor = new EscherClientAnchorRecord();
  82. //hack. internal variable EscherClientAnchorRecord.shortRecord can be
  83. //initialized only in fillFields(). We need to set shortRecord=false;
  84. byte[] header = new byte[16];
  85. LittleEndian.putUShort(header, 0, 0);
  86. LittleEndian.putUShort(header, 2, 0);
  87. LittleEndian.putInt(header, 4, 8);
  88. anchor.fillFields(header, 0, null);
  89. }
  90. _escherContainer.addChildRecord(anchor);
  91. return _escherContainer;
  92. }
  93. /**
  94. * Returns width of the line in in points
  95. */
  96. public double getLineWidth(){
  97. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  98. EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH);
  99. double width = prop == null ? DEFAULT_LINE_WIDTH : (double)prop.getPropertyValue()/EMU_PER_POINT;
  100. return width;
  101. }
  102. /**
  103. * Sets the width of line in in points
  104. * @param width the width of line in in points
  105. */
  106. public void setLineWidth(double width){
  107. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  108. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEWIDTH, (int)(width*EMU_PER_POINT));
  109. }
  110. /**
  111. * Sets the color of line
  112. *
  113. * @param color new color of the line
  114. */
  115. public void setLineColor(Color color){
  116. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  117. if (color == null) {
  118. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80000);
  119. } else {
  120. int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
  121. setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb);
  122. setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, color == null ? 0x180010 : 0x180018);
  123. }
  124. }
  125. /**
  126. * @return color of the line. If color is not set returns <code>and.awt.Color.black</code>
  127. */
  128. public Color getLineColor(){
  129. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  130. EscherSimpleProperty p = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  131. if(p != null && (p.getPropertyValue() & 0x8) == 0) return null;
  132. Color clr = getColor(EscherProperties.LINESTYLE__COLOR, EscherProperties.LINESTYLE__OPACITY, -1);
  133. return clr == null ? Color.black : clr;
  134. }
  135. /**
  136. * Gets line dashing. One of the PEN_* constants defined in this class.
  137. *
  138. * @return dashing of the line.
  139. */
  140. public int getLineDashing(){
  141. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  142. EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
  143. return prop == null ? Line.PEN_SOLID : prop.getPropertyValue();
  144. }
  145. /**
  146. * Sets line dashing. One of the PEN_* constants defined in this class.
  147. *
  148. * @param pen new style of the line.
  149. */
  150. public void setLineDashing(int pen){
  151. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  152. setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING, pen == Line.PEN_SOLID ? -1 : pen);
  153. }
  154. /**
  155. * Sets line style. One of the constants defined in this class.
  156. *
  157. * @param style new style of the line.
  158. */
  159. public void setLineStyle(int style){
  160. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  161. setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE, style == Line.LINE_SIMPLE ? -1 : style);
  162. }
  163. /**
  164. * Returns line style. One of the constants defined in this class.
  165. *
  166. * @return style of the line.
  167. */
  168. public int getLineStyle(){
  169. EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
  170. EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE);
  171. return prop == null ? Line.LINE_SIMPLE : prop.getPropertyValue();
  172. }
  173. /**
  174. * The color used to fill this shape.
  175. */
  176. public Color getFillColor(){
  177. return getFill().getForegroundColor();
  178. }
  179. /**
  180. * The color used to fill this shape.
  181. *
  182. * @param color the background color
  183. */
  184. public void setFillColor(Color color){
  185. getFill().setForegroundColor(color);
  186. }
  187. /**
  188. * Whether the shape is horizontally flipped
  189. *
  190. * @return whether the shape is horizontally flipped
  191. */
  192. public boolean getFlipHorizontal(){
  193. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  194. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPHORIZ) != 0;
  195. }
  196. /**
  197. * Whether the shape is vertically flipped
  198. *
  199. * @return whether the shape is vertically flipped
  200. */
  201. public boolean getFlipVertical(){
  202. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  203. return (spRecord.getFlags()& EscherSpRecord.FLAG_FLIPVERT) != 0;
  204. }
  205. /**
  206. * Rotation angle in degrees
  207. *
  208. * @return rotation angle in degrees
  209. */
  210. public int getRotation(){
  211. int rot = getEscherProperty(EscherProperties.TRANSFORM__ROTATION);
  212. int angle = (rot >> 16) % 360;
  213. return angle;
  214. }
  215. /**
  216. * Rotate this shape
  217. *
  218. * @param theta the rotation angle in degrees
  219. */
  220. public void setRotation(int theta){
  221. setEscherProperty(EscherProperties.TRANSFORM__ROTATION, (theta << 16));
  222. }
  223. /**
  224. *
  225. * @return 'absolute' anchor of this shape relative to the parent sheet
  226. */
  227. public Rectangle2D getLogicalAnchor2D(){
  228. Rectangle2D anchor = getAnchor2D();
  229. //if it is a groupped shape see if we need to transform the coordinates
  230. if (_parent != null){
  231. List<Shape> lst = new ArrayList<Shape>();
  232. lst.add(_parent);
  233. Shape top = _parent;
  234. while(top.getParent() != null) {
  235. top = top.getParent();
  236. lst.add(top);
  237. }
  238. AffineTransform tx = new AffineTransform();
  239. for(int i = lst.size() - 1; i >= 0; i--) {
  240. ShapeGroup prnt = (ShapeGroup)lst.get(i);
  241. Rectangle2D exterior = prnt.getAnchor2D();
  242. Rectangle2D interior = prnt.getCoordinates();
  243. double scaleX = exterior.getWidth() / interior.getWidth();
  244. double scaleY = exterior.getHeight() / interior.getHeight();
  245. tx.translate(exterior.getX(), exterior.getY());
  246. tx.scale(scaleX, scaleY);
  247. tx.translate(-interior.getX(), -interior.getY());
  248. }
  249. anchor = tx.createTransformedShape(anchor).getBounds2D();
  250. }
  251. int angle = getRotation();
  252. if(angle != 0){
  253. double centerX = anchor.getX() + anchor.getWidth()/2;
  254. double centerY = anchor.getY() + anchor.getHeight()/2;
  255. AffineTransform trans = new AffineTransform();
  256. trans.translate(centerX, centerY);
  257. trans.rotate(Math.toRadians(angle));
  258. trans.translate(-centerX, -centerY);
  259. Rectangle2D rect = trans.createTransformedShape(anchor).getBounds2D();
  260. if((anchor.getWidth() < anchor.getHeight() && rect.getWidth() > rect.getHeight()) ||
  261. (anchor.getWidth() > anchor.getHeight() && rect.getWidth() < rect.getHeight()) ){
  262. trans = new AffineTransform();
  263. trans.translate(centerX, centerY);
  264. trans.rotate(Math.PI/2);
  265. trans.translate(-centerX, -centerY);
  266. anchor = trans.createTransformedShape(anchor).getBounds2D();
  267. }
  268. }
  269. return anchor;
  270. }
  271. public void draw(Graphics2D graphics){
  272. // AffineTransform at = graphics.getTransform();
  273. graphics.canvas.save();
  274. ShapePainter.paint(this, graphics);
  275. graphics.canvas.restore();
  276. // graphics.setTransform(at);
  277. }
  278. /**
  279. * Find a record in the underlying EscherClientDataRecord
  280. *
  281. * @param recordType type of the record to search
  282. */
  283. protected Record getClientDataRecord(int recordType) {
  284. Record[] records = getClientRecords();
  285. if(records != null) for (int i = 0; i < records.length; i++) {
  286. if(records[i].getRecordType() == recordType){
  287. return records[i];
  288. }
  289. }
  290. return null;
  291. }
  292. /**
  293. * Search for EscherClientDataRecord, if found, convert its contents into an array of HSLF records
  294. *
  295. * @return an array of HSLF records contained in the shape's EscherClientDataRecord or <code>null</code>
  296. */
  297. protected Record[] getClientRecords() {
  298. if(_clientData == null){
  299. EscherRecord r = Shape.getEscherChild(getSpContainer(), EscherClientDataRecord.RECORD_ID);
  300. //ddf can return EscherContainerRecord with recordId=EscherClientDataRecord.RECORD_ID
  301. //convert in to EscherClientDataRecord on the fly
  302. if(r != null && !(r instanceof EscherClientDataRecord)){
  303. byte[] data = r.serialize();
  304. r = new EscherClientDataRecord();
  305. r.fillFields(data, 0, new DefaultEscherRecordFactory());
  306. }
  307. _clientData = (EscherClientDataRecord)r;
  308. }
  309. if(_clientData != null && _clientRecords == null){
  310. byte[] data = _clientData.getRemainingData();
  311. _clientRecords = Record.findChildRecords(data, 0, data.length);
  312. }
  313. return _clientRecords;
  314. }
  315. protected void updateClientData() {
  316. if(_clientData != null && _clientRecords != null){
  317. ByteArrayOutputStream out = new ByteArrayOutputStream();
  318. try {
  319. for (int i = 0; i < _clientRecords.length; i++) {
  320. _clientRecords[i].writeOut(out);
  321. }
  322. } catch(Exception e){
  323. throw new HSLFException(e);
  324. }
  325. _clientData.setRemainingData(out.toByteArray());
  326. }
  327. }
  328. public void setHyperlink(Hyperlink link){
  329. if(link.getId() == -1){
  330. throw new HSLFException("You must call SlideShow.addHyperlink(Hyperlink link) first");
  331. }
  332. EscherClientDataRecord cldata = new EscherClientDataRecord();
  333. cldata.setOptions((short)0xF);
  334. getSpContainer().addChildRecord(cldata); // TODO - junit to prove getChildRecords().add is wrong
  335. InteractiveInfo info = new InteractiveInfo();
  336. InteractiveInfoAtom infoAtom = info.getInteractiveInfoAtom();
  337. switch(link.getType()){
  338. case Hyperlink.LINK_FIRSTSLIDE:
  339. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  340. infoAtom.setJump(InteractiveInfoAtom.JUMP_FIRSTSLIDE);
  341. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_FirstSlide);
  342. break;
  343. case Hyperlink.LINK_LASTSLIDE:
  344. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  345. infoAtom.setJump(InteractiveInfoAtom.JUMP_LASTSLIDE);
  346. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_LastSlide);
  347. break;
  348. case Hyperlink.LINK_NEXTSLIDE:
  349. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  350. infoAtom.setJump(InteractiveInfoAtom.JUMP_NEXTSLIDE);
  351. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_NextSlide);
  352. break;
  353. case Hyperlink.LINK_PREVIOUSSLIDE:
  354. infoAtom.setAction(InteractiveInfoAtom.ACTION_JUMP);
  355. infoAtom.setJump(InteractiveInfoAtom.JUMP_PREVIOUSSLIDE);
  356. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_PreviousSlide);
  357. break;
  358. case Hyperlink.LINK_URL:
  359. infoAtom.setAction(InteractiveInfoAtom.ACTION_HYPERLINK);
  360. infoAtom.setJump(InteractiveInfoAtom.JUMP_NONE);
  361. infoAtom.setHyperlinkType(InteractiveInfoAtom.LINK_Url);
  362. break;
  363. }
  364. infoAtom.setHyperlinkID(link.getId());
  365. ByteArrayOutputStream out = new ByteArrayOutputStream();
  366. try {
  367. info.writeOut(out);
  368. } catch(Exception e){
  369. throw new HSLFException(e);
  370. }
  371. cldata.setRemainingData(out.toByteArray());
  372. }
  373. }