Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

HSLFPictureShape.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.image.BufferedImage;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import javax.imageio.ImageIO;
  22. import org.apache.poi.ddf.*;
  23. import org.apache.poi.hslf.blip.Bitmap;
  24. import org.apache.poi.hslf.record.Document;
  25. import org.apache.poi.sl.usermodel.*;
  26. import org.apache.poi.util.*;
  27. /**
  28. * Represents a picture in a PowerPoint document.
  29. *
  30. * @author Yegor Kozlov
  31. */
  32. public class HSLFPictureShape extends HSLFSimpleShape implements PictureShape {
  33. /**
  34. * Windows Enhanced Metafile (EMF)
  35. */
  36. public static final int EMF = 2;
  37. /**
  38. * Windows Metafile (WMF)
  39. */
  40. public static final int WMF = 3;
  41. /**
  42. * Macintosh PICT
  43. */
  44. public static final int PICT = 4;
  45. /**
  46. * JPEG
  47. */
  48. public static final int JPEG = 5;
  49. /**
  50. * PNG
  51. */
  52. public static final int PNG = 6;
  53. /**
  54. * Windows DIB (BMP)
  55. */
  56. public static final byte DIB = 7;
  57. /**
  58. * Create a new <code>Picture</code>
  59. *
  60. * @param idx the index of the picture
  61. */
  62. public HSLFPictureShape(int idx){
  63. this(idx, null);
  64. }
  65. /**
  66. * Create a new <code>Picture</code>
  67. *
  68. * @param idx the index of the picture
  69. * @param parent the parent shape
  70. */
  71. public HSLFPictureShape(int idx, ShapeContainer<HSLFShape> parent) {
  72. super(null, parent);
  73. _escherContainer = createSpContainer(idx, parent instanceof HSLFGroupShape);
  74. }
  75. /**
  76. * Create a <code>Picture</code> object
  77. *
  78. * @param escherRecord the <code>EscherSpContainer</code> record which holds information about
  79. * this picture in the <code>Slide</code>
  80. * @param parent the parent shape of this picture
  81. */
  82. protected HSLFPictureShape(EscherContainerRecord escherRecord, ShapeContainer<HSLFShape> parent){
  83. super(escherRecord, parent);
  84. }
  85. /**
  86. * Returns index associated with this picture.
  87. * Index starts with 1 and points to a EscherBSE record which
  88. * holds information about this picture.
  89. *
  90. * @return the index to this picture (1 based).
  91. */
  92. public int getPictureIndex(){
  93. EscherOptRecord opt = getEscherOptRecord();
  94. EscherSimpleProperty prop = getEscherProperty(opt, EscherProperties.BLIP__BLIPTODISPLAY);
  95. return prop == null ? 0 : prop.getPropertyValue();
  96. }
  97. /**
  98. * Create a new Picture and populate the inital structure of the <code>EscherSp</code> record which holds information about this picture.
  99. * @param idx the index of the picture which refers to <code>EscherBSE</code> container.
  100. * @return the create Picture object
  101. */
  102. protected EscherContainerRecord createSpContainer(int idx, boolean isChild) {
  103. _escherContainer = super.createSpContainer(isChild);
  104. _escherContainer.setOptions((short)15);
  105. EscherSpRecord spRecord = _escherContainer.getChildById(EscherSpRecord.RECORD_ID);
  106. spRecord.setOptions((short)((ShapeType.FRAME.nativeId << 4) | 0x2));
  107. //set default properties for a picture
  108. EscherOptRecord opt = getEscherOptRecord();
  109. setEscherProperty(opt, EscherProperties.PROTECTION__LOCKAGAINSTGROUPING, 0x800080);
  110. //another weird feature of powerpoint: for picture id we must add 0x4000.
  111. setEscherProperty(opt, (short)(EscherProperties.BLIP__BLIPTODISPLAY + 0x4000), idx);
  112. return _escherContainer;
  113. }
  114. /**
  115. * Resize this picture to the default size.
  116. * For PNG and JPEG resizes the image to 100%,
  117. * for other types sets the default size of 200x200 pixels.
  118. */
  119. public void setDefaultSize(){
  120. HSLFPictureData pict = getPictureData();
  121. if (pict instanceof Bitmap){
  122. BufferedImage img = null;
  123. try {
  124. img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  125. }
  126. catch (IOException e){}
  127. catch (NegativeArraySizeException ne) {}
  128. if(img != null) {
  129. // Valid image, set anchor from it
  130. setAnchor(new java.awt.Rectangle(0, 0, img.getWidth()*POINT_DPI/PIXEL_DPI, img.getHeight()*POINT_DPI/PIXEL_DPI));
  131. } else {
  132. // Invalid image, go with the default metafile size
  133. setAnchor(new java.awt.Rectangle(0, 0, 200, 200));
  134. }
  135. } else {
  136. //default size of a metafile picture is 200x200
  137. setAnchor(new java.awt.Rectangle(50, 50, 200, 200));
  138. }
  139. }
  140. @Override
  141. public HSLFPictureData getPictureData(){
  142. HSLFSlideShow ppt = getSheet().getSlideShow();
  143. HSLFPictureData[] pict = ppt.getPictureData();
  144. EscherBSERecord bse = getEscherBSERecord();
  145. if (bse == null){
  146. logger.log(POILogger.ERROR, "no reference to picture data found ");
  147. } else {
  148. for ( int i = 0; i < pict.length; i++ ) {
  149. if (pict[i].getOffset() == bse.getOffset()){
  150. return pict[i];
  151. }
  152. }
  153. logger.log(POILogger.ERROR, "no picture found for our BSE offset " + bse.getOffset());
  154. }
  155. return null;
  156. }
  157. protected EscherBSERecord getEscherBSERecord(){
  158. HSLFSlideShow ppt = getSheet().getSlideShow();
  159. Document doc = ppt.getDocumentRecord();
  160. EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
  161. EscherContainerRecord bstore = HSLFShape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
  162. if(bstore == null) {
  163. logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found ");
  164. return null;
  165. }
  166. List<EscherRecord> lst = bstore.getChildRecords();
  167. int idx = getPictureIndex();
  168. if (idx == 0){
  169. logger.log(POILogger.DEBUG, "picture index was not found, returning ");
  170. return null;
  171. }
  172. return (EscherBSERecord)lst.get(idx-1);
  173. }
  174. /**
  175. * Name of this picture.
  176. *
  177. * @return name of this picture
  178. */
  179. public String getPictureName(){
  180. EscherOptRecord opt = getEscherOptRecord();
  181. EscherComplexProperty prop = getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME);
  182. if (prop == null) return null;
  183. String name = StringUtil.getFromUnicodeLE(prop.getComplexData());
  184. return name.trim();
  185. }
  186. /**
  187. * Name of this picture.
  188. *
  189. * @param name of this picture
  190. */
  191. public void setPictureName(String name){
  192. EscherOptRecord opt = getEscherOptRecord();
  193. byte[] data = StringUtil.getToUnicodeLE(name + '\u0000');
  194. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
  195. opt.addEscherProperty(prop);
  196. }
  197. /**
  198. * By default set the orininal image size
  199. */
  200. protected void afterInsert(HSLFSheet sh){
  201. super.afterInsert(sh);
  202. EscherBSERecord bse = getEscherBSERecord();
  203. bse.setRef(bse.getRef() + 1);
  204. java.awt.Rectangle anchor = getAnchor();
  205. if (anchor.equals(new java.awt.Rectangle())){
  206. setDefaultSize();
  207. }
  208. }
  209. /**
  210. * The anchor specified by {@link #getLogicalAnchor2D()} is the displayed size,
  211. * i.e. the size of the already clipped image
  212. */
  213. @Override
  214. public Insets getClipping() {
  215. EscherOptRecord opt = getEscherOptRecord();
  216. double top = getFractProp(opt, EscherProperties.BLIP__CROPFROMTOP);
  217. double bottom = getFractProp(opt, EscherProperties.BLIP__CROPFROMBOTTOM);
  218. double left = getFractProp(opt, EscherProperties.BLIP__CROPFROMLEFT);
  219. double right = getFractProp(opt, EscherProperties.BLIP__CROPFROMRIGHT);
  220. // if all crop values are zero (the default) then no crop rectangle is set, return null
  221. return (top==0 && bottom==0 && left==0 && right==0)
  222. ? null
  223. : new Insets((int)(top*100000), (int)(left*100000), (int)(bottom*100000), (int)(right*100000));
  224. }
  225. /**
  226. * @return the fractional property or 0 if not defined
  227. */
  228. private static double getFractProp(EscherOptRecord opt, short propertyId) {
  229. EscherSimpleProperty prop = getEscherProperty(opt, propertyId);
  230. if (prop == null) return 0;
  231. int fixedPoint = prop.getPropertyValue();
  232. return Units.fixedPointToDouble(fixedPoint);
  233. }
  234. }