final long internalImageHeight = StrictGeomUtility.toInternalValue(scaleFactor * imageHeight);
final long cellWidth = cellBounds.getWidth();
final long cellHeight = cellBounds.getHeight();
final StrictBounds cb;
final int pictureId;
try
{
if (shouldScale)
{
final double scaleX;
final double scaleY;
final boolean keepAspectRatio = layoutContext.getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
if (keepAspectRatio)
{
final double imgScaleFactor = Math.min(cellWidth / (double) internalImageWidth,
cellHeight / (double) internalImageHeight);
scaleX = imgScaleFactor;
scaleY = imgScaleFactor;
}
else
{
scaleX = cellWidth / (double) internalImageWidth;
scaleY = cellHeight / (double) internalImageHeight;
}
final long clipWidth = (long) (scaleX * internalImageWidth);
final long clipHeight = (long) (scaleY * internalImageHeight);
final long alignmentX = RenderUtility.computeHorizontalAlignment(horizontalAlignment, cellWidth, clipWidth);
final long alignmentY = RenderUtility.computeVerticalAlignment(verticalAlignment, cellHeight, clipHeight);
cb = new StrictBounds(cellBounds.getX() + alignmentX,
cellBounds.getY() + alignmentY,
Math.min(clipWidth, cellWidth),
Math.min(clipHeight, cellHeight));
// Recompute the cells that this image will cover (now that it has been resized)
rectangle = currentLayout.getTableBounds(cb, rectangle);
pictureId = loadImage(workbook, image);
if (pictureId <= 0)
{
return;
}
}
else
{
// unscaled ..
if (internalImageWidth <= cellWidth &&
internalImageHeight <= cellHeight)
{
// No clipping needed.
final long alignmentX = RenderUtility.computeHorizontalAlignment
(horizontalAlignment, cellBounds.getWidth(), internalImageWidth);
final long alignmentY = RenderUtility.computeVerticalAlignment
(verticalAlignment, cellBounds.getHeight(), internalImageHeight);
cb = new StrictBounds(cellBounds.getX() + alignmentX,
cellBounds.getY() + alignmentY,
internalImageWidth,
internalImageHeight);
// Recompute the cells that this image will cover (now that it has been resized)
rectangle = currentLayout.getTableBounds(cb, rectangle);
pictureId = loadImage(workbook, image);
if (pictureId <= 0)
{
return;
}
}
else
{
// at least somewhere there is clipping needed.
final long clipWidth = Math.min(cellWidth, internalImageWidth);
final long clipHeight = Math.min(cellHeight, internalImageHeight);
final long alignmentX = RenderUtility.computeHorizontalAlignment
(horizontalAlignment, cellBounds.getWidth(), clipWidth);
final long alignmentY = RenderUtility.computeVerticalAlignment
(verticalAlignment, cellBounds.getHeight(), clipHeight);
cb = new StrictBounds(cellBounds.getX() + alignmentX,
cellBounds.getY() + alignmentY,
clipWidth,
clipHeight);
// Recompute the cells that this image will cover (now that it has been resized)
rectangle = currentLayout.getTableBounds(cb, rectangle);
pictureId = loadImageWithClipping(workbook, image, clipWidth, clipHeight, scaleFactor);
if (pictureId <= 0)
{
return;
}
}
}
}
catch (UnsupportedEncoderException uee)
{
// should not happen, as PNG is always supported.
logger.warn("Assertation-Failure: PNG encoding failed.", uee);
return;
}
final int cell1x = rectangle.getX1();
final int cell1y = rectangle.getY1();
final int cell2x = Math.max(cell1x, rectangle.getX2() - 1);
final int cell2y = Math.max(cell1y, rectangle.getY2() - 1);
final long cell1width = currentLayout.getCellWidth(cell1x);
final long cell1height = currentLayout.getRowHeight(cell1y);
final long cell2width = currentLayout.getCellWidth(cell2x);
final long cell2height = currentLayout.getRowHeight(cell2y);
final long cell1xPos = currentLayout.getXPosition(cell1x);
final long cell1yPos = currentLayout.getYPosition(cell1y);
final long cell2xPos = currentLayout.getXPosition(cell2x);
final long cell2yPos = currentLayout.getYPosition(cell2y);
final int dx1 = (int) (1023 * ((cb.getX() - cell1xPos) / (double) cell1width));
final int dy1 = (int) (255 * ((cb.getY() - cell1yPos) / (double) cell1height));
final int dx2 = (int) (1023 * ((cb.getX() + cb.getWidth() - cell2xPos) / (double) cell2width));
final int dy2 = (int) (255 * ((cb.getY() + cb.getHeight() - cell2yPos) / (double) cell2height));
final ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setDx1(dx1);
anchor.setDy1(dy1);
anchor.setDx2(dx2);