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.8KB

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