/* ZssDrawing.java
{{IS_NOTE
Purpose:
Description:
History:
Apr 23, 2008 7:07:18 PM, Created by henrichen
}}IS_NOTE
Copyright (C) 2008 Potix Corporation. All Rights Reserved.
{{IS_RIGHT
This program is distributed under GPL Version 2.0 in the hope that
it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package jxl.biff.drawing;
import org.zkoss.lang.SystemException;
import jxl.CellView;
import jxl.Sheet;
import jxl.biff.IntegerHelper;
import jxl.read.biff.ZssWorkbookParser;
import java.io.File;
/**
* @author henrichen
*
*/
public class ZssDrawing {
private static final int DEFAULT_FONT_SIZE = 10;
private int _properties = 2;
private int _left;
private int _leftFraction;
private int _top;
private int _topFraction;
private int _right;
private int _rightFraction;
private int _bottom;
private int _bottomFraction;
private String _alt;
private String _name;
private Drawing _drawing;
private Sheet _sheet;
public ZssDrawing(Drawing drawing) {
_drawing = drawing;
final EscherContainer readSpContainer = getReadSpContainer(drawing);
final EscherRecord[] children = readSpContainer.getChildren();
final Opt opt = (Opt) children[1];
if (opt.getProperty(897) != null)
{
_alt = trimEndingZero(opt.getProperty(897).stringValue);
}
_name = trimEndingZero(drawing.getImageFile().getName());
/* if (opt.getProperty(896) != null) //"Picture 1"
{
pictureid = new java.io.File(opt.getProperty(896).stringValue);
}
*/
ClientAnchor clientAnchor = null;
for (int i = 2; i < children.length; i++)
{
if (children[i].getType() == EscherRecordType.CLIENT_ANCHOR)
{
clientAnchor = (ClientAnchor) children[i];
if (clientAnchor != null) break;
}
}
if (clientAnchor == null) {
throw new SystemException("Picture cannot find its anchor point. "+drawing.getImageFilePath());
}
final byte[] bytes = clientAnchor.getBytes();
// The properties
_properties = IntegerHelper.getInt(bytes[0], bytes[1]);
// The left-top cell
_left = IntegerHelper.getInt(bytes[2], bytes[3]);
_leftFraction = IntegerHelper.getInt(bytes[4], bytes[5]); //base is 1024
_top = IntegerHelper.getInt(bytes[6], bytes[7]);
_topFraction = IntegerHelper.getInt(bytes[8], bytes[9]); //base is 256
// The right-bottom cell
_right = IntegerHelper.getInt(bytes[10], bytes[11]);
_rightFraction = IntegerHelper.getInt(bytes[12], bytes[13]); //base is 1024
_bottom = IntegerHelper.getInt(bytes[14], bytes[15]);
_bottomFraction = IntegerHelper.getInt(bytes[16], bytes[17]);
}
public int getLeft() {
//return column index of the anchor cell
return _left;
}
public int getLeftFraction() {
//return column fraction of the anchor cell
return _leftFraction;
}
public int getTop() {
//return row index of the anchor cell
return _top;
}
public int getTopFraction() {
//return row fraction of the anchor cell
return _topFraction;
}
public int getRight() {
//return column index of the right-bottom cell
return _right;
}
public int getRightFraction() {
//return column fraction of the right-bottom cell
return _rightFraction;
}
public int getBottom() {
//return row index of the right-bottom cell
return _bottom;
}
public int getBottomFraction() {
//return row fraction of the right-bottom cell
return _bottomFraction;
}
//1: MOVE_AND_SIZE_WITH_CELLS
//2: MOVE_WITH_CELLS
//3: NO_MOVE_OR_SIZE_WITH_CELLS
public int getProperties() {
return _properties;
}
public byte[] getImageData() {
//return the image contents
return _drawing.getImageData();
}
public String getName() {
//return the file name
return _name;
}
public String getAlt() {
return _alt;
}
private EscherContainer getReadSpContainer(Drawing drawing) {
return (EscherContainer) ZssWorkbookParser.getField(drawing, "readSpContainer");
}
private String trimEndingZero(String v) {
int sz = v.length();
return v.charAt(sz-1) == 0 ? v.substring(0,sz-1) : v;
}
/*
public int getWidthInPx(ZssDrawing zd) {
int firstCol = zd.getLeft();
int lastCol = zd.getRight();
// **** MAGIC NUMBER ALERT ***
// multiply the point size of the font by 0.59 to give the point size
// I know of no explanation for this yet, other than that it seems to
// give the right answer
// Get the width of the image within the first column, allowing for
// fractional offsets
CellView cellView = _sheet.getColumnView(firstCol);
int firstColWidth = cellView.getSize();
final int leftFraction = getLeftFraction();
double firstColImageWidth = leftFraction >= 1024 ?
0d : (firstColWidth - firstColWidth * leftFraction / 1024);
double pointSize = (cellView.getFormat() != null) ?
cellView.getFormat().getFont().getPointSize() : DEFAULT_FONT_SIZE;
double firstColWidthInPoints = firstColImageWidth * 0.59 * pointSize / 256;
// Get the width of the image within the last column, allowing for
// fractional offsets
int lastColWidth = 0;
double lastColImageWidth = 0;
double lastColWidthInPoints = 0;
if (lastCol != firstCol)
{
cellView = _sheet.getColumnView(lastCol);
lastColWidth = cellView.getSize();
lastColImageWidth = lastColWidth * getRightFraction() / 1024;
pointSize = (cellView.getFormat() != null) ?
cellView.getFormat().getFont().getPointSize() : DEFAULT_FONT_SIZE;
lastColWidthInPoints = lastColImageWidth * 0.59 * pointSize / 256;
}
// Now get all the columns in between
double width = 0;
for (int j = firstCol + 1; j < lastCol ; ++j)
{
cellView = _sheet.getColumnView(j);
pointSize = (cellView.getFormat() != null) ?
cellView.getFormat().getFont().getPointSize() : DEFAULT_FONT_SIZE;
width += cellView.getSize() * 0.59 * pointSize / 256;
}
// Add on the first and last row contributions to get the width
double widthInPoints = width + firstColWidthInPoints + lastColWidthInPoints;
return (int) widthInPoints;
}
public int getHeightInPx() {
// The start and end row numbers
final int firstRow = getTop();
final int lastRow = getBottom();
// Get the height of the image within the first row, allowing for
// fractional offsets
int firstRowHeight = _sheet.getRowView(firstRow).getSize();
final int topFraction = getTopFraction();
double firstRowImageHeight = topFraction >= 256 ?
0d : (firstRowHeight - firstRowHeight * topFraction / 256);
// Get the height of the image within the last row, allowing for
// fractional offsets
int lastRowHeight = 0;
double lastRowImageHeight = 0;
if (lastRow != firstRow)
{
lastRowHeight = _sheet.getRowView(lastRow).getSize();
lastRowImageHeight = lastRowHeight * getBottomFraction() / 256;
}
// Now get all the rows in between
double height = 0;
for (int j = firstRow + 1; j < lastRow ; ++j)
{
height += _sheet.getRowView(j).getSize();
}
// Add on the first and last row contributions to get the height in twips
double heightInTwips = height + firstRowImageHeight + lastRowImageHeight;
// Now divide by the magic number to converts twips into pixels and
// return the value
double heightInPoints = heightInTwips / 20.0;
return (int) heightInPoints;
}
*/
}