2009-02-13 17:11:16 -05:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
2005-05-01 18:00:12 -04:00
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
2009-12-08 19:12:32 -05:00
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.io.ByteArrayInputStream;
|
2012-07-11 08:08:38 -04:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2009-12-08 19:12:32 -05:00
|
|
|
|
2012-06-22 05:37:17 -04:00
|
|
|
import org.apache.poi.ddf.*;
|
|
|
|
import org.apache.poi.hssf.record.ObjRecord;
|
2008-11-12 02:15:37 -05:00
|
|
|
import org.apache.poi.ss.usermodel.Picture;
|
2009-11-06 14:08:41 -05:00
|
|
|
import org.apache.poi.ss.util.ImageUtils;
|
2011-07-20 03:38:01 -04:00
|
|
|
import org.apache.poi.hssf.model.InternalWorkbook;
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2005-05-01 18:00:12 -04:00
|
|
|
/**
|
|
|
|
* Represents a escher picture. Eg. A GIF, JPEG etc...
|
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis
|
2007-10-01 14:07:32 -04:00
|
|
|
* @author Yegor Kozlov (yegor at apache.org)
|
2005-05-01 18:00:12 -04:00
|
|
|
*/
|
2009-02-13 17:11:16 -05:00
|
|
|
public final class HSSFPicture extends HSSFSimpleShape implements Picture {
|
2007-10-01 14:07:32 -04:00
|
|
|
public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
|
|
|
|
public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
|
|
|
|
public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
|
|
|
|
public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
|
|
|
|
public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
|
|
|
|
public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
|
2005-05-01 18:00:12 -04:00
|
|
|
|
2008-06-08 11:31:05 -04:00
|
|
|
/**
|
|
|
|
* width of 1px in columns with default width in units of 1/256 of a character width
|
|
|
|
*/
|
|
|
|
private static final float PX_DEFAULT = 32.00f;
|
|
|
|
/**
|
|
|
|
* width of 1px in columns with overridden width in units of 1/256 of a character width
|
|
|
|
*/
|
|
|
|
private static final float PX_MODIFIED = 36.56f;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Height of 1px of a row
|
|
|
|
*/
|
|
|
|
private static final int PX_ROW = 15;
|
|
|
|
|
2012-06-22 05:37:17 -04:00
|
|
|
public HSSFPicture(EscherContainerRecord spContainer, ObjRecord objRecord) {
|
|
|
|
super(spContainer, objRecord);
|
|
|
|
}
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2005-05-01 18:00:12 -04:00
|
|
|
/**
|
|
|
|
* Constructs a picture object.
|
|
|
|
*/
|
2010-10-04 09:34:17 -04:00
|
|
|
public HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
|
2005-05-01 18:00:12 -04:00
|
|
|
{
|
|
|
|
super( parent, anchor );
|
|
|
|
setShapeType(OBJECT_TYPE_PICTURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPictureIndex()
|
|
|
|
{
|
2012-07-11 08:08:38 -04:00
|
|
|
EscherSimpleProperty property = getOptRecord().lookup(EscherProperties.BLIP__BLIPTODISPLAY);
|
2012-06-22 05:37:17 -04:00
|
|
|
if (null == property){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return property.getPropertyValue();
|
2005-05-01 18:00:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setPictureIndex( int pictureIndex )
|
|
|
|
{
|
2012-06-22 05:37:17 -04:00
|
|
|
setPropertyValue(new EscherSimpleProperty( EscherProperties.BLIP__BLIPTODISPLAY, false, true, pictureIndex));
|
2005-05-01 18:00:12 -04:00
|
|
|
}
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2012-07-11 08:08:38 -04:00
|
|
|
@Override
|
|
|
|
protected EscherContainerRecord createSpContainer() {
|
|
|
|
EscherContainerRecord spContainer = super.createSpContainer();
|
|
|
|
EscherOptRecord opt = spContainer.getChildById(EscherOptRecord.RECORD_ID);
|
|
|
|
removeEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
|
|
|
|
removeEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
|
|
|
|
return spContainer;
|
|
|
|
}
|
|
|
|
|
2007-10-01 14:07:32 -04:00
|
|
|
/**
|
2008-11-16 08:24:42 -05:00
|
|
|
* Resize the image
|
2009-12-21 09:51:39 -05:00
|
|
|
* <p>
|
|
|
|
* Please note, that this method works correctly only for workbooks
|
|
|
|
* with default font size (Arial 10pt for .xls).
|
|
|
|
* If the default font is changed the resized image can be streched vertically or horizontally.
|
|
|
|
* </p>
|
2007-10-01 14:07:32 -04:00
|
|
|
*
|
2008-11-16 08:24:42 -05:00
|
|
|
* @param scale the amount by which image dimensions are multiplied relative to the original size.
|
|
|
|
* <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
|
|
|
|
* <code>resize(2.0)</code> resizes to 200% of the original.
|
2007-10-01 14:07:32 -04:00
|
|
|
*/
|
2008-11-16 08:24:42 -05:00
|
|
|
public void resize(double scale){
|
2007-10-01 14:07:32 -04:00
|
|
|
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
|
|
|
|
anchor.setAnchorType(2);
|
|
|
|
|
2008-11-16 08:24:42 -05:00
|
|
|
HSSFClientAnchor pref = getPreferredSize(scale);
|
2007-10-01 14:07:32 -04:00
|
|
|
|
|
|
|
int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
|
|
|
|
int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
|
|
|
|
|
|
|
|
anchor.setCol2((short)col2);
|
|
|
|
anchor.setDx1(0);
|
|
|
|
anchor.setDx2(pref.getDx2());
|
|
|
|
|
|
|
|
anchor.setRow2(row2);
|
|
|
|
anchor.setDy1(0);
|
|
|
|
anchor.setDy2(pref.getDy2());
|
|
|
|
}
|
|
|
|
|
2008-11-16 08:24:42 -05:00
|
|
|
/**
|
|
|
|
* Reset the image to the original size.
|
2009-12-21 09:51:39 -05:00
|
|
|
*
|
|
|
|
* <p>
|
|
|
|
* Please note, that this method works correctly only for workbooks
|
|
|
|
* with default font size (Arial 10pt for .xls).
|
|
|
|
* If the default font is changed the resized image can be streched vertically or horizontally.
|
|
|
|
* </p>
|
2008-11-16 08:24:42 -05:00
|
|
|
*/
|
|
|
|
public void resize(){
|
|
|
|
resize(1.0);
|
|
|
|
}
|
|
|
|
|
2007-10-01 14:07:32 -04:00
|
|
|
/**
|
2007-11-03 15:50:40 -04:00
|
|
|
* Calculate the preferred size for this picture.
|
2007-10-01 14:07:32 -04:00
|
|
|
*
|
2007-11-03 15:50:40 -04:00
|
|
|
* @return HSSFClientAnchor with the preferred size for this image
|
2007-10-01 14:07:32 -04:00
|
|
|
* @since POI 3.0.2
|
|
|
|
*/
|
2007-11-03 15:50:40 -04:00
|
|
|
public HSSFClientAnchor getPreferredSize(){
|
2008-11-16 08:24:42 -05:00
|
|
|
return getPreferredSize(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the preferred size for this picture.
|
|
|
|
*
|
|
|
|
* @param scale the amount by which image dimensions are multiplied relative to the original size.
|
|
|
|
* @return HSSFClientAnchor with the preferred size for this image
|
|
|
|
* @since POI 3.0.2
|
|
|
|
*/
|
|
|
|
public HSSFClientAnchor getPreferredSize(double scale){
|
2008-06-08 11:31:05 -04:00
|
|
|
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-06-08 11:31:05 -04:00
|
|
|
Dimension size = getImageDimension();
|
2008-11-16 08:24:42 -05:00
|
|
|
double scaledWidth = size.getWidth() * scale;
|
|
|
|
double scaledHeight = size.getHeight() * scale;
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-06-08 11:31:05 -04:00
|
|
|
float w = 0;
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-06-08 11:31:05 -04:00
|
|
|
//space in the leftmost cell
|
2012-06-18 16:59:32 -04:00
|
|
|
w += getColumnWidthInPixels(anchor.getCol1())*(1 - (float)anchor.getDx1()/1024);
|
|
|
|
short col2 = (short)(anchor.getCol1() + 1);
|
2008-06-08 11:31:05 -04:00
|
|
|
int dx2 = 0;
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-11-16 08:24:42 -05:00
|
|
|
while(w < scaledWidth){
|
2008-06-08 11:31:05 -04:00
|
|
|
w += getColumnWidthInPixels(col2++);
|
|
|
|
}
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-11-16 08:24:42 -05:00
|
|
|
if(w > scaledWidth) {
|
2008-06-08 11:31:05 -04:00
|
|
|
//calculate dx2, offset in the rightmost cell
|
|
|
|
col2--;
|
2008-11-16 08:24:42 -05:00
|
|
|
double cw = getColumnWidthInPixels(col2);
|
|
|
|
double delta = w - scaledWidth;
|
2008-06-08 11:31:05 -04:00
|
|
|
dx2 = (int)((cw-delta)/cw*1024);
|
|
|
|
}
|
2012-06-18 16:59:32 -04:00
|
|
|
anchor.setCol2(col2);
|
|
|
|
anchor.setDx2(dx2);
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-06-08 11:31:05 -04:00
|
|
|
float h = 0;
|
2012-06-18 16:59:32 -04:00
|
|
|
h += (1 - (float)anchor.getDy1()/256)* getRowHeightInPixels(anchor.getRow1());
|
|
|
|
int row2 = anchor.getRow1() + 1;
|
2008-06-08 11:31:05 -04:00
|
|
|
int dy2 = 0;
|
2007-10-01 14:07:32 -04:00
|
|
|
|
2008-11-16 08:24:42 -05:00
|
|
|
while(h < scaledHeight){
|
2008-06-08 11:31:05 -04:00
|
|
|
h += getRowHeightInPixels(row2++);
|
|
|
|
}
|
2008-11-16 08:24:42 -05:00
|
|
|
if(h > scaledHeight) {
|
2008-06-08 11:31:05 -04:00
|
|
|
row2--;
|
2008-11-16 08:24:42 -05:00
|
|
|
double ch = getRowHeightInPixels(row2);
|
|
|
|
double delta = h - scaledHeight;
|
2008-06-08 11:31:05 -04:00
|
|
|
dy2 = (int)((ch-delta)/ch*256);
|
2007-10-01 14:07:32 -04:00
|
|
|
}
|
2012-06-18 16:59:32 -04:00
|
|
|
anchor.setRow2(row2);
|
|
|
|
anchor.setDy2(dy2);
|
2008-06-08 11:31:05 -04:00
|
|
|
|
2007-10-01 14:07:32 -04:00
|
|
|
return anchor;
|
|
|
|
}
|
|
|
|
|
2008-09-16 17:56:36 -04:00
|
|
|
private float getColumnWidthInPixels(int column){
|
2008-06-08 11:31:05 -04:00
|
|
|
|
2009-02-13 17:11:16 -05:00
|
|
|
int cw = _patriarch._sheet.getColumnWidth(column);
|
2008-06-08 11:31:05 -04:00
|
|
|
float px = getPixelWidth(column);
|
|
|
|
|
|
|
|
return cw/px;
|
|
|
|
}
|
|
|
|
|
|
|
|
private float getRowHeightInPixels(int i){
|
|
|
|
|
2009-02-13 17:11:16 -05:00
|
|
|
HSSFRow row = _patriarch._sheet.getRow(i);
|
2008-06-08 11:31:05 -04:00
|
|
|
float height;
|
|
|
|
if(row != null) height = row.getHeight();
|
2009-02-13 17:11:16 -05:00
|
|
|
else height = _patriarch._sheet.getDefaultRowHeight();
|
2008-06-08 11:31:05 -04:00
|
|
|
|
|
|
|
return height/PX_ROW;
|
|
|
|
}
|
|
|
|
|
2008-09-16 17:56:36 -04:00
|
|
|
private float getPixelWidth(int column){
|
2008-06-08 11:31:05 -04:00
|
|
|
|
2009-02-13 17:11:16 -05:00
|
|
|
int def = _patriarch._sheet.getDefaultColumnWidth()*256;
|
|
|
|
int cw = _patriarch._sheet.getColumnWidth(column);
|
2008-06-08 11:31:05 -04:00
|
|
|
|
|
|
|
return cw == def ? PX_DEFAULT : PX_MODIFIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the dimension of this image
|
|
|
|
*
|
|
|
|
* @return image dimension
|
|
|
|
*/
|
|
|
|
public Dimension getImageDimension(){
|
2012-06-22 05:37:17 -04:00
|
|
|
EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(getPictureIndex());
|
2008-06-08 11:31:05 -04:00
|
|
|
byte[] data = bse.getBlipRecord().getPicturedata();
|
|
|
|
int type = bse.getBlipTypeWin32();
|
2009-11-06 14:08:41 -05:00
|
|
|
return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
|
2008-06-08 11:31:05 -04:00
|
|
|
}
|
2011-07-20 03:38:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return picture data for this shape
|
|
|
|
*
|
|
|
|
* @return picture data for this shape
|
|
|
|
*/
|
|
|
|
public HSSFPictureData getPictureData(){
|
|
|
|
InternalWorkbook iwb = _patriarch._sheet.getWorkbook().getWorkbook();
|
2012-06-22 05:37:17 -04:00
|
|
|
EscherBlipRecord blipRecord = iwb.getBSERecord(getPictureIndex()).getBlipRecord();
|
2011-07-20 03:38:01 -04:00
|
|
|
return new HSSFPictureData(blipRecord);
|
|
|
|
}
|
2012-07-11 08:08:38 -04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
void afterInsert(HSSFPatriarch patriarch) {
|
|
|
|
super.afterInsert(patriarch);
|
|
|
|
EscherBSERecord bse =
|
|
|
|
patriarch._sheet.getWorkbook().getWorkbook().getBSERecord(getPictureIndex());
|
|
|
|
bse.setRef(bse.getRef() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The color applied to the lines of this shape.
|
|
|
|
*/
|
|
|
|
public String getAdditionalData() {
|
|
|
|
EscherComplexProperty propFile = (EscherComplexProperty) getOptRecord().lookup(
|
|
|
|
EscherProperties.BLIP__BLIPFILENAME);
|
|
|
|
try {
|
|
|
|
if (null == propFile){
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return new String(propFile.getComplexData(), "UTF-16LE").trim();
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAdditionalData(String data){
|
|
|
|
try {
|
|
|
|
EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, data.getBytes("UTF-16LE"));
|
|
|
|
setPropertyValue(prop);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
System.out.println("Unsupported encoding: UTF-16LE");
|
|
|
|
}
|
|
|
|
}
|
2005-05-01 18:00:12 -04:00
|
|
|
}
|