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.

HSSFPicture.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.hssf.usermodel;
  16. import java.awt.Dimension;
  17. import java.io.ByteArrayInputStream;
  18. import org.apache.poi.ddf.DefaultEscherRecordFactory;
  19. import org.apache.poi.ddf.EscherBSERecord;
  20. import org.apache.poi.ddf.EscherBlipRecord;
  21. import org.apache.poi.ddf.EscherClientDataRecord;
  22. import org.apache.poi.ddf.EscherComplexProperty;
  23. import org.apache.poi.ddf.EscherContainerRecord;
  24. import org.apache.poi.ddf.EscherOptRecord;
  25. import org.apache.poi.ddf.EscherProperties;
  26. import org.apache.poi.ddf.EscherSimpleProperty;
  27. import org.apache.poi.ddf.EscherTextboxRecord;
  28. import org.apache.poi.hssf.model.InternalWorkbook;
  29. import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
  30. import org.apache.poi.hssf.record.EscherAggregate;
  31. import org.apache.poi.hssf.record.ObjRecord;
  32. import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
  33. import org.apache.poi.ss.usermodel.Picture;
  34. import org.apache.poi.ss.util.ImageUtils;
  35. import org.apache.poi.util.POILogFactory;
  36. import org.apache.poi.util.POILogger;
  37. import org.apache.poi.util.StringUtil;
  38. /**
  39. * Represents a escher picture. Eg. A GIF, JPEG etc...
  40. */
  41. public class HSSFPicture extends HSSFSimpleShape implements Picture {
  42. @SuppressWarnings("unused")
  43. private static POILogger logger = POILogFactory.getLogger(HSSFPicture.class);
  44. public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
  45. public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
  46. public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
  47. public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
  48. public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
  49. public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
  50. public HSSFPicture(EscherContainerRecord spContainer, ObjRecord objRecord) {
  51. super(spContainer, objRecord);
  52. }
  53. /**
  54. * Constructs a picture object.
  55. */
  56. public HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
  57. {
  58. super( parent, anchor );
  59. super.setShapeType(OBJECT_TYPE_PICTURE);
  60. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) getObjRecord().getSubRecords().get(0);
  61. cod.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE);
  62. }
  63. public int getPictureIndex()
  64. {
  65. EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.BLIP__BLIPTODISPLAY);
  66. if (null == property){
  67. return -1;
  68. }
  69. return property.getPropertyValue();
  70. }
  71. public void setPictureIndex( int pictureIndex )
  72. {
  73. setPropertyValue(new EscherSimpleProperty( EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex));
  74. }
  75. @Override
  76. protected EscherContainerRecord createSpContainer() {
  77. EscherContainerRecord spContainer = super.createSpContainer();
  78. EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
  79. opt.removeEscherProperty(EscherProperties.LINESTYLE__LINEDASHING);
  80. opt.removeEscherProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  81. spContainer.removeChildRecord(spContainer.getChildById(EscherTextboxRecord.RECORD_ID));
  82. return spContainer;
  83. }
  84. /**
  85. * Reset the image to the dimension of the embedded image
  86. *
  87. * <p>
  88. * Please note, that this method works correctly only for workbooks
  89. * with default font size (Arial 10pt for .xls).
  90. * If the default font is changed the resized image can be streched vertically or horizontally.
  91. * </p>
  92. */
  93. @Override
  94. public void resize(){
  95. resize(Double.MAX_VALUE);
  96. }
  97. /**
  98. * Resize the image proportionally.
  99. *
  100. * @see #resize(double, double)
  101. */
  102. @Override
  103. public void resize(double scale) {
  104. resize(scale,scale);
  105. }
  106. /**
  107. * Resize the image
  108. * <p>
  109. * Please note, that this method works correctly only for workbooks
  110. * with default font size (Arial 10pt for .xls).
  111. * If the default font is changed the resized image can be streched vertically or horizontally.
  112. * </p>
  113. * <p>
  114. * <code>resize(1.0,1.0)</code> keeps the original size,<br/>
  115. * <code>resize(0.5,0.5)</code> resize to 50% of the original,<br/>
  116. * <code>resize(2.0,2.0)</code> resizes to 200% of the original.<br/>
  117. * <code>resize({@link Double#MAX_VALUE},{@link Double#MAX_VALUE})</code> resizes to the dimension of the embedded image.
  118. * </p>
  119. *
  120. * @param scaleX the amount by which the image width is multiplied relative to the original width.
  121. * @param scaleY the amount by which the image height is multiplied relative to the original height.
  122. */
  123. @Override
  124. public void resize(double scaleX, double scaleY) {
  125. HSSFClientAnchor anchor = getClientAnchor();
  126. anchor.setAnchorType(AnchorType.MOVE_DONT_RESIZE);
  127. HSSFClientAnchor pref = getPreferredSize(scaleX,scaleY);
  128. int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
  129. int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
  130. anchor.setCol2((short)col2);
  131. // anchor.setDx1(0);
  132. anchor.setDx2(pref.getDx2());
  133. anchor.setRow2(row2);
  134. // anchor.setDy1(0);
  135. anchor.setDy2(pref.getDy2());
  136. }
  137. /**
  138. * Calculate the preferred size for this picture.
  139. *
  140. * @return HSSFClientAnchor with the preferred size for this image
  141. * @since POI 3.0.2
  142. */
  143. @Override
  144. public HSSFClientAnchor getPreferredSize(){
  145. return getPreferredSize(1.0);
  146. }
  147. /**
  148. * Calculate the preferred size for this picture.
  149. *
  150. * @param scale the amount by which image dimensions are multiplied relative to the original size.
  151. * @return HSSFClientAnchor with the preferred size for this image
  152. * @since POI 3.0.2
  153. */
  154. public HSSFClientAnchor getPreferredSize(double scale){
  155. return getPreferredSize(scale, scale);
  156. }
  157. /**
  158. * Calculate the preferred size for this picture.
  159. *
  160. * @param scaleX the amount by which image width is multiplied relative to the original width.
  161. * @param scaleY the amount by which image height is multiplied relative to the original height.
  162. * @return HSSFClientAnchor with the preferred size for this image
  163. * @since POI 3.11
  164. */
  165. @Override
  166. public HSSFClientAnchor getPreferredSize(double scaleX, double scaleY){
  167. ImageUtils.setPreferredSize(this, scaleX, scaleY);
  168. return getClientAnchor();
  169. }
  170. /**
  171. * Return the dimension of the embedded image in pixel
  172. *
  173. * @return image dimension in pixels
  174. */
  175. @Override
  176. public Dimension getImageDimension(){
  177. InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook();
  178. EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
  179. byte[] data = bse.getBlipRecord().getPicturedata();
  180. int type = bse.getBlipTypeWin32();
  181. return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
  182. }
  183. /**
  184. * Return picture data for this shape
  185. *
  186. * @return picture data for this shape or {@code null} if picture wasn't embedded, i.e. external linked
  187. */
  188. @Override
  189. public HSSFPictureData getPictureData(){
  190. int picIdx = getPictureIndex();
  191. if (picIdx == -1) {
  192. return null;
  193. }
  194. HSSFPatriarch patriarch = getPatriarch();
  195. HSSFShape parent = getParent();
  196. while(patriarch == null && parent != null) {
  197. patriarch = parent.getPatriarch();
  198. parent = parent.getParent();
  199. }
  200. if(patriarch == null) {
  201. throw new IllegalStateException("Could not find a patriarch for a HSSPicture");
  202. }
  203. InternalWorkbook iwb = patriarch.getSheet().getWorkbook().getWorkbook();
  204. EscherBSERecord bse = iwb.getBSERecord(picIdx);
  205. EscherBlipRecord blipRecord = bse.getBlipRecord();
  206. return new HSSFPictureData(blipRecord);
  207. }
  208. @Override
  209. void afterInsert(HSSFPatriarch patriarch) {
  210. EscherAggregate agg = patriarch.getBoundAggregate();
  211. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  212. if(getPictureIndex() != -1) {
  213. EscherBSERecord bse =
  214. patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex());
  215. bse.setRef(bse.getRef() + 1);
  216. }
  217. }
  218. /**
  219. * The filename of the embedded image
  220. */
  221. public String getFileName() {
  222. EscherComplexProperty propFile = (EscherComplexProperty) getOptRecord().lookup(
  223. EscherProperties.BLIP__BLIPFILENAME);
  224. return (null == propFile)
  225. ? ""
  226. : StringUtil.getFromUnicodeLE(propFile.getComplexData()).trim();
  227. }
  228. public void setFileName(String data){
  229. // TODO: add trailing \u0000?
  230. byte bytes[] = StringUtil.getToUnicodeLE(data);
  231. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, bytes);
  232. setPropertyValue(prop);
  233. }
  234. @Override
  235. public void setShapeType(int shapeType) {
  236. throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
  237. }
  238. @Override
  239. protected HSSFShape cloneShape() {
  240. EscherContainerRecord spContainer = new EscherContainerRecord();
  241. byte [] inSp = getEscherContainer().serialize();
  242. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  243. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  244. return new HSSFPicture(spContainer, obj);
  245. }
  246. /**
  247. * @return the anchor that is used by this picture.
  248. */
  249. @Override
  250. public HSSFClientAnchor getClientAnchor() {
  251. HSSFAnchor a = getAnchor();
  252. return (a instanceof HSSFClientAnchor) ? (HSSFClientAnchor)a : null;
  253. }
  254. /**
  255. * @return the sheet which contains the picture shape
  256. */
  257. @Override
  258. public HSSFSheet getSheet() {
  259. return getPatriarch().getSheet();
  260. }
  261. }