protected boolean drawImage(final RenderableReplacedContentBox content,
final Image image,
final com.lowagie.text.Image itextImage)
{
final StyleSheet layoutContext = content.getStyleSheet();
final boolean shouldScale = layoutContext.getBooleanStyleProperty(ElementStyleKeys.SCALE);
final int x = (int) StrictGeomUtility.toExternalValue(content.getX());
final int y = (int) StrictGeomUtility.toExternalValue(content.getY());
final int width = (int) StrictGeomUtility.toExternalValue(content.getWidth());
final int height = (int) StrictGeomUtility.toExternalValue(content.getHeight());
if (width == 0 || height == 0)
{
PdfLogicalPageDrawable.logger.debug("Error: Image area is empty: " + content);
return false;
}
final WaitingImageObserver obs = new WaitingImageObserver(image);
obs.waitImageLoaded();
final int imageWidth = image.getWidth(obs);
final int imageHeight = image.getHeight(obs);
if (imageWidth < 1 || imageHeight < 1)
{
return false;
}
final Rectangle2D.Double drawAreaBounds = new Rectangle2D.Double(x, y, width, height);
final AffineTransform scaleTransform;
final Graphics2D g2;
if (shouldScale == false)
{
double deviceScaleFactor = 1;
final double devResolution = getMetaData().getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
if (getMetaData().isFeatureSupported(OutputProcessorFeature.IMAGE_RESOLUTION_MAPPING))
{
if (devResolution != 72.0 && devResolution > 0)
{
// Need to scale the device to its native resolution before attempting to draw the image..
deviceScaleFactor = (72.0 / devResolution);
}
}
final int clipWidth = Math.min(width, (int) Math.ceil(deviceScaleFactor * imageWidth));
final int clipHeight = Math.min(height, (int) Math.ceil(deviceScaleFactor * imageHeight));
final ElementAlignment horizontalAlignment =
(ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
final ElementAlignment verticalAlignment =
(ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);
g2 = (Graphics2D) getGraphics().create();
g2.clip(drawAreaBounds);
g2.translate(x, y);
g2.translate(alignmentX, alignmentY);
g2.clip(new Rectangle2D.Float(0, 0, clipWidth, clipHeight));
g2.scale(deviceScaleFactor, deviceScaleFactor);
scaleTransform = null;
}
else
{
g2 = (Graphics2D) getGraphics().create();
g2.clip(drawAreaBounds);
g2.translate(x, y);
g2.clip(new Rectangle2D.Float(0, 0, width, height));
final double scaleX;
final double scaleY;
final boolean keepAspectRatio = layoutContext.getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
if (keepAspectRatio)
{
final double scaleFactor = Math.min(width / (double) imageWidth, height / (double) imageHeight);
scaleX = scaleFactor;
scaleY = scaleFactor;
}
else
{
scaleX = width / (double) imageWidth;
scaleY = height / (double) imageHeight;
}
final int clipWidth = (int) (scaleX * imageWidth);
final int clipHeight = (int) (scaleY * imageHeight);
final ElementAlignment horizontalAlignment =
(ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.ALIGNMENT);
final ElementAlignment verticalAlignment =
(ElementAlignment) layoutContext.getStyleProperty(ElementStyleKeys.VALIGNMENT);
final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width, clipWidth);
final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height, clipHeight);
g2.translate(alignmentX, alignmentY);
scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);