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 12KB

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