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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 java.io.UnsupportedEncodingException;
  19. import org.apache.poi.ddf.*;
  20. import org.apache.poi.hssf.record.EscherAggregate;
  21. import org.apache.poi.hssf.record.ObjRecord;
  22. import org.apache.poi.ss.usermodel.Picture;
  23. import org.apache.poi.ss.util.ImageUtils;
  24. import org.apache.poi.hssf.model.InternalWorkbook;
  25. /**
  26. * Represents a escher picture. Eg. A GIF, JPEG etc...
  27. *
  28. * @author Glen Stampoultzis
  29. * @author Yegor Kozlov (yegor at apache.org)
  30. */
  31. public class HSSFPicture extends HSSFSimpleShape implements Picture {
  32. public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
  33. public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
  34. public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
  35. public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
  36. public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
  37. public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
  38. /**
  39. * width of 1px in columns with default width in units of 1/256 of a character width
  40. */
  41. private static final float PX_DEFAULT = 32.00f;
  42. /**
  43. * width of 1px in columns with overridden width in units of 1/256 of a character width
  44. */
  45. private static final float PX_MODIFIED = 36.56f;
  46. /**
  47. * Height of 1px of a row
  48. */
  49. private static final int PX_ROW = 15;
  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. }
  61. public int getPictureIndex()
  62. {
  63. EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.BLIP__BLIPTODISPLAY);
  64. if (null == property){
  65. return -1;
  66. }
  67. return property.getPropertyValue();
  68. }
  69. public void setPictureIndex( int pictureIndex )
  70. {
  71. setPropertyValue(new EscherSimpleProperty( EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex));
  72. }
  73. @Override
  74. protected EscherContainerRecord createSpContainer() {
  75. EscherContainerRecord spContainer = super.createSpContainer();
  76. EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
  77. removeEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
  78. removeEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
  79. spContainer.removeChildRecord(spContainer.getChildById(EscherTextboxRecord.RECORD_ID));
  80. return spContainer;
  81. }
  82. /**
  83. * Resize the image
  84. * <p>
  85. * Please note, that this method works correctly only for workbooks
  86. * with default font size (Arial 10pt for .xls).
  87. * If the default font is changed the resized image can be streched vertically or horizontally.
  88. * </p>
  89. *
  90. * @param scale the amount by which image dimensions are multiplied relative to the original size.
  91. * <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
  92. * <code>resize(2.0)</code> resizes to 200% of the original.
  93. */
  94. public void resize(double scale){
  95. HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
  96. anchor.setAnchorType(2);
  97. HSSFClientAnchor pref = getPreferredSize(scale);
  98. int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
  99. int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
  100. anchor.setCol2((short)col2);
  101. anchor.setDx1(0);
  102. anchor.setDx2(pref.getDx2());
  103. anchor.setRow2(row2);
  104. anchor.setDy1(0);
  105. anchor.setDy2(pref.getDy2());
  106. }
  107. /**
  108. * Reset the image to the original size.
  109. *
  110. * <p>
  111. * Please note, that this method works correctly only for workbooks
  112. * with default font size (Arial 10pt for .xls).
  113. * If the default font is changed the resized image can be streched vertically or horizontally.
  114. * </p>
  115. */
  116. public void resize(){
  117. resize(1.0);
  118. }
  119. /**
  120. * Calculate the preferred size for this picture.
  121. *
  122. * @return HSSFClientAnchor with the preferred size for this image
  123. * @since POI 3.0.2
  124. */
  125. public HSSFClientAnchor getPreferredSize(){
  126. return getPreferredSize(1.0);
  127. }
  128. /**
  129. * Calculate the preferred size for this picture.
  130. *
  131. * @param scale the amount by which image dimensions are multiplied relative to the original size.
  132. * @return HSSFClientAnchor with the preferred size for this image
  133. * @since POI 3.0.2
  134. */
  135. public HSSFClientAnchor getPreferredSize(double scale){
  136. HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
  137. Dimension size = getImageDimension();
  138. double scaledWidth = size.getWidth() * scale;
  139. double scaledHeight = size.getHeight() * scale;
  140. float w = 0;
  141. //space in the leftmost cell
  142. w += getColumnWidthInPixels(anchor.getCol1())*(1 - (float)anchor.getDx1()/1024);
  143. short col2 = (short)(anchor.getCol1() + 1);
  144. int dx2 = 0;
  145. while(w < scaledWidth){
  146. w += getColumnWidthInPixels(col2++);
  147. }
  148. if(w > scaledWidth) {
  149. //calculate dx2, offset in the rightmost cell
  150. col2--;
  151. double cw = getColumnWidthInPixels(col2);
  152. double delta = w - scaledWidth;
  153. dx2 = (int)((cw-delta)/cw*1024);
  154. }
  155. anchor.setCol2(col2);
  156. anchor.setDx2(dx2);
  157. float h = 0;
  158. h += (1 - (float)anchor.getDy1()/256)* getRowHeightInPixels(anchor.getRow1());
  159. int row2 = anchor.getRow1() + 1;
  160. int dy2 = 0;
  161. while(h < scaledHeight){
  162. h += getRowHeightInPixels(row2++);
  163. }
  164. if(h > scaledHeight) {
  165. row2--;
  166. double ch = getRowHeightInPixels(row2);
  167. double delta = h - scaledHeight;
  168. dy2 = (int)((ch-delta)/ch*256);
  169. }
  170. anchor.setRow2(row2);
  171. anchor.setDy2(dy2);
  172. return anchor;
  173. }
  174. private float getColumnWidthInPixels(int column){
  175. int cw = _patriarch.getSheet().getColumnWidth(column);
  176. float px = getPixelWidth(column);
  177. return cw/px;
  178. }
  179. private float getRowHeightInPixels(int i){
  180. HSSFRow row = _patriarch.getSheet().getRow(i);
  181. float height;
  182. if(row != null) height = row.getHeight();
  183. else height = _patriarch.getSheet().getDefaultRowHeight();
  184. return height/PX_ROW;
  185. }
  186. private float getPixelWidth(int column){
  187. int def = _patriarch.getSheet().getDefaultColumnWidth()*256;
  188. int cw = _patriarch.getSheet().getColumnWidth(column);
  189. return cw == def ? PX_DEFAULT : PX_MODIFIED;
  190. }
  191. /**
  192. * Return the dimension of this image
  193. *
  194. * @return image dimension
  195. */
  196. public Dimension getImageDimension(){
  197. EscherBSERecord bse = _patriarch.getSheet()._book.getBSERecord(getPictureIndex());
  198. byte[] data = bse.getBlipRecord().getPicturedata();
  199. int type = bse.getBlipTypeWin32();
  200. return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
  201. }
  202. /**
  203. * Return picture data for this shape
  204. *
  205. * @return picture data for this shape
  206. */
  207. public HSSFPictureData getPictureData(){
  208. InternalWorkbook iwb = _patriarch.getSheet().getWorkbook().getWorkbook();
  209. EscherBlipRecord blipRecord = iwb.getBSERecord(getPictureIndex()).getBlipRecord();
  210. return new HSSFPictureData(blipRecord);
  211. }
  212. @Override
  213. void afterInsert(HSSFPatriarch patriarch) {
  214. EscherAggregate agg = patriarch._getBoundAggregate();
  215. agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord());
  216. EscherBSERecord bse =
  217. patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex());
  218. bse.setRef(bse.getRef() + 1);
  219. }
  220. /**
  221. * The color applied to the lines of this shape.
  222. */
  223. public String getFileName() {
  224. EscherComplexProperty propFile = (EscherComplexProperty) getOptRecord().lookup(
  225. EscherProperties.BLIP__BLIPFILENAME);
  226. try {
  227. if (null == propFile){
  228. return "";
  229. }
  230. return new String(propFile.getComplexData(), "UTF-16LE").trim();
  231. } catch (UnsupportedEncodingException e) {
  232. return "";
  233. }
  234. }
  235. public void setFileName(String data){
  236. try {
  237. EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, data.getBytes("UTF-16LE"));
  238. setPropertyValue(prop);
  239. } catch (UnsupportedEncodingException e) {
  240. System.out.println("Unsupported encoding: UTF-16LE");
  241. }
  242. }
  243. @Override
  244. public void setShapeType(int shapeType) {
  245. throw new IllegalStateException("Shape type can not be changed in "+this.getClass().getSimpleName());
  246. }
  247. @Override
  248. public HSSFShape cloneShape() {
  249. EscherContainerRecord spContainer = new EscherContainerRecord();
  250. byte [] inSp = getEscherContainer().serialize();
  251. spContainer.fillFields(inSp, 0, new DefaultEscherRecordFactory());
  252. ObjRecord obj = (ObjRecord) getObjRecord().cloneViaReserialise();
  253. return new HSSFPicture(spContainer, obj);
  254. }
  255. }