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.

HSLFPictureShape.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.Insets;
  17. import java.awt.geom.Rectangle2D;
  18. import java.util.List;
  19. import org.apache.poi.ddf.AbstractEscherOptRecord;
  20. import org.apache.poi.ddf.EscherBSERecord;
  21. import org.apache.poi.ddf.EscherComplexProperty;
  22. import org.apache.poi.ddf.EscherContainerRecord;
  23. import org.apache.poi.ddf.EscherProperties;
  24. import org.apache.poi.ddf.EscherRecord;
  25. import org.apache.poi.ddf.EscherSimpleProperty;
  26. import org.apache.poi.ddf.EscherSpRecord;
  27. import org.apache.poi.hslf.record.Document;
  28. import org.apache.poi.sl.draw.DrawPictureShape;
  29. import org.apache.poi.sl.usermodel.PictureShape;
  30. import org.apache.poi.sl.usermodel.ShapeContainer;
  31. import org.apache.poi.sl.usermodel.ShapeType;
  32. import org.apache.poi.util.POILogger;
  33. import org.apache.poi.util.StringUtil;
  34. import org.apache.poi.util.Units;
  35. /**
  36. * Represents a picture in a PowerPoint document.
  37. *
  38. * @author Yegor Kozlov
  39. */
  40. public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape<HSLFShape,HSLFTextParagraph> {
  41. /**
  42. * Create a new <code>Picture</code>
  43. *
  44. * @param data the picture data
  45. */
  46. public HSLFPictureShape(HSLFPictureData data){
  47. this(data, null);
  48. }
  49. /**
  50. * Create a new <code>Picture</code>
  51. *
  52. * @param data the picture data
  53. * @param parent the parent shape
  54. */
  55. public HSLFPictureShape(HSLFPictureData data, ShapeContainer<HSLFShape,HSLFTextParagraph> parent) {
  56. super(null, parent);
  57. _escherContainer = createSpContainer(data.getIndex(), parent instanceof HSLFGroupShape);
  58. }
  59. /**
  60. * Create a <code>Picture</code> object
  61. *
  62. * @param escherRecord the <code>EscherSpContainer</code> record which holds information about
  63. * this picture in the <code>Slide</code>
  64. * @param parent the parent shape of this picture
  65. */
  66. protected HSLFPictureShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape,HSLFTextParagraph> parent){
  67. super(escherRecord, parent);
  68. }
  69. /**
  70. * Returns index associated with this picture.
  71. * Index starts with 1 and points to a EscherBSE record which
  72. * holds information about this picture.
  73. *
  74. * @return the index to this picture (1 based).
  75. */
  76. public int getPictureIndex(){
  77. AbstractEscherOptRecord opt = getEscherOptRecord();
  78. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY);
  79. return prop == null ? 0 : prop.getPropertyValue();
  80. }
  81. /**
  82. * Create a new Picture and populate the inital structure of the <code>EscherSp</code> record which holds information about this picture.
  83. * @param idx the index of the picture which refers to <code>EscherBSE</code> container.
  84. * @return the create Picture object
  85. */
  86. protected EscherContainerRecord createSpContainer(int idx, boolean isChild) {
  87. _escherContainer = super.createSpContainer(isChild);
  88. _escherContainer.setOptions((short)15);
  89. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  90. spRecord.setOptions((short)((ShapeType.FRAME.nativeId << 4) | 0x2));
  91. //set default properties for a picture
  92. AbstractEscherOptRecord opt = getEscherOptRecord();
  93. setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x800080);
  94. //another weird feature of powerpoint: for picture id we must add 0x4000.
  95. setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx);
  96. return _escherContainer;
  97. }
  98. @SuppressWarnings("resource")
  99. @Override
  100. public HSLFPictureData getPictureData(){
  101. HSLFSlideShow ppt = getSheet().getSlideShow();
  102. List<HSLFPictureData> pict = ppt.getPictureData();
  103. EscherBSERecord bse = getEscherBSERecord();
  104. if (bse == null){
  105. logger.log(POILogger.ERROR, "no reference to picture data found ");
  106. } else {
  107. for (HSLFPictureData pd : pict) {
  108. if (pd.getOffset() == bse.getOffset()){
  109. return pd;
  110. }
  111. }
  112. logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
  113. }
  114. return null;
  115. }
  116. @SuppressWarnings("resource")
  117. protected EscherBSERecord getEscherBSERecord(){
  118. HSLFSlideShow ppt = getSheet().getSlideShow();
  119. Document doc = ppt.getDocumentRecord();
  120. EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
  121. EscherContainerRecord bstore = HSLFShape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
  122. if(bstore == null) {
  123. logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found ");
  124. return null;
  125. }
  126. List<EscherRecord> lst = bstore.getChildRecords();
  127. int idx = getPictureIndex();
  128. if (idx == 0){
  129. logger.log(POILogger.DEBUG, "picture index was not found, returning ");
  130. return null;
  131. }
  132. return (EscherBSERecord)lst.get(idx-1);
  133. }
  134. /**
  135. * Name of this picture.
  136. *
  137. * @return name of this picture
  138. */
  139. public String getPictureName(){
  140. AbstractEscherOptRecord opt = getEscherOptRecord();
  141. EscherComplexProperty prop = getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME);
  142. if (prop == null) return null;
  143. String name = StringUtil.getFromUnicodeLE(prop.getComplexData());
  144. return name.trim();
  145. }
  146. /**
  147. * Name of this picture.
  148. *
  149. * @param name of this picture
  150. */
  151. public void setPictureName(String name){
  152. AbstractEscherOptRecord opt = getEscherOptRecord();
  153. byte[] data = StringUtil.getToUnicodeLE(name + '\u0000');
  154. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
  155. opt.addEscherProperty(prop);
  156. }
  157. /**
  158. * By default set the orininal image size
  159. */
  160. protected void afterInsert(HSLFSheet sh){
  161. super.afterInsert(sh);
  162. EscherBSERecord bse = getEscherBSERecord();
  163. bse.setRef(bse.getRef() + 1);
  164. Rectangle2D anchor = getAnchor();
  165. if (anchor.isEmpty()){
  166. new DrawPictureShape(this).resize();
  167. }
  168. }
  169. @Override
  170. public Insets getClipping() {
  171. // The anchor specified by the escher properties is the displayed size,
  172. // i.e. the size of the already clipped image
  173. AbstractEscherOptRecord opt = getEscherOptRecord();
  174. double top = getFractProp(opt, EscherProperties.BLIP__CROPFROMTOP);
  175. double bottom = getFractProp(opt, EscherProperties.BLIP__CROPFROMBOTTOM);
  176. double left = getFractProp(opt, EscherProperties.BLIP__CROPFROMLEFT);
  177. double right = getFractProp(opt, EscherProperties.BLIP__CROPFROMRIGHT);
  178. // if all crop values are zero (the default) then no crop rectangle is set, return null
  179. return (top==0 && bottom==0 && left==0 && right==0)
  180. ? null
  181. : new Insets((int)(top*100000), (int)(left*100000), (int)(bottom*100000), (int)(right*100000));
  182. }
  183. /**
  184. * @return the fractional property or 0 if not defined
  185. */
  186. private static double getFractProp(AbstractEscherOptRecord opt, short propertyId) {
  187. EscherSimpleProperty prop = getEscherProperty(opt, propertyId);
  188. if (prop == null) return 0;
  189. int fixedPoint = prop.getPropertyValue();
  190. return Units.fixedPointToDouble(fixedPoint);
  191. }
  192. }