availableImageWidth = (availableImageWidth < 0)?0:availableImageWidth;
int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
availableImageHeight = (availableImageHeight < 0)?0:availableImageHeight;
JRRenderable renderer = printImage.getRenderer();
if (
renderer != null &&
availableImageWidth > 0 &&
availableImageHeight > 0
)
{
if (renderer.getType() == JRRenderable.TYPE_IMAGE)
{
// Image renderers are all asked for their image data at some point.
// Better to test and replace the renderer now, in case of lazy load error.
renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, printImage.getOnErrorTypeValue());
}
}
else
{
renderer = null;
}
if (renderer != null)
{
int xoffset = 0;
int yoffset = 0;
Chunk chunk = null;
float scaledWidth = availableImageWidth;
float scaledHeight = availableImageHeight;
if (renderer.getType() == JRRenderable.TYPE_IMAGE)
{
com.lowagie.text.Image image = null;
float xalignFactor = getXAlignFactor(printImage);
float yalignFactor = getYAlignFactor(printImage);
switch(printImage.getScaleImageValue())
{
case CLIP :
{
// Image load might fail, from given image data.
// Better to test and replace the renderer now, in case of lazy load error.
renderer =
JRImageRenderer.getOnErrorRendererForDimension(
renderer,
printImage.getOnErrorTypeValue()
);
if (renderer == null)
{
break;
}
int normalWidth = availableImageWidth;
int normalHeight = availableImageHeight;
Dimension2D dimension = renderer.getDimension();
if (dimension != null)
{
normalWidth = (int)dimension.getWidth();
normalHeight = (int)dimension.getHeight();
}
xoffset = (int)(xalignFactor * (availableImageWidth - normalWidth));
yoffset = (int)(yalignFactor * (availableImageHeight - normalHeight));
int minWidth = Math.min(normalWidth, availableImageWidth);
int minHeight = Math.min(normalHeight, availableImageHeight);
BufferedImage bi =
new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
if (printImage.getModeValue() == ModeEnum.OPAQUE)
{
g.setColor(printImage.getBackcolor());
g.fillRect(0, 0, minWidth, minHeight);
}
renderer.render(
g,
new java.awt.Rectangle(
(xoffset > 0 ? 0 : xoffset),
(yoffset > 0 ? 0 : yoffset),
normalWidth,
normalHeight
)
);
g.dispose();
xoffset = (xoffset < 0 ? 0 : xoffset);
yoffset = (yoffset < 0 ? 0 : yoffset);
//awtImage = bi.getSubimage(0, 0, minWidth, minHeight);
//image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
image = com.lowagie.text.Image.getInstance(bi, null);
break;
}
case FILL_FRAME :
{
if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer))
{
image = (com.lowagie.text.Image)loadedImagesMap.get(renderer);
}
else
{
try
{
image = com.lowagie.text.Image.getInstance(renderer.getImageData());
imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
}
catch(Exception e)
{
JRImageRenderer tmpRenderer =
JRImageRenderer.getOnErrorRendererForImage(
JRImageRenderer.getInstance(renderer.getImageData()),
printImage.getOnErrorTypeValue()
);
if (tmpRenderer == null)
{
break;
}
java.awt.Image awtImage = tmpRenderer.getImage();
image = com.lowagie.text.Image.getInstance(awtImage, null);
}
if (printImage.isUsingCache())
{
loadedImagesMap.put(renderer, image);
}
}
image.scaleAbsolute(availableImageWidth, availableImageHeight);
break;
}
case RETAIN_SHAPE :
default :
{
if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer))
{
image = (com.lowagie.text.Image)loadedImagesMap.get(renderer);
}
else
{
try
{
image = com.lowagie.text.Image.getInstance(renderer.getImageData());
imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
}
catch(Exception e)
{
JRImageRenderer tmpRenderer =
JRImageRenderer.getOnErrorRendererForImage(
JRImageRenderer.getInstance(renderer.getImageData()),
printImage.getOnErrorTypeValue()
);
if (tmpRenderer == null)
{
break;
}
java.awt.Image awtImage = tmpRenderer.getImage();
image = com.lowagie.text.Image.getInstance(awtImage, null);
}
if (printImage.isUsingCache())
{
loadedImagesMap.put(renderer, image);
}
}
image.scaleToFit(availableImageWidth, availableImageHeight);
xoffset = (int)(xalignFactor * (availableImageWidth - image.getPlainWidth()));
yoffset = (int)(yalignFactor * (availableImageHeight - image.getPlainHeight()));
xoffset = (xoffset < 0 ? 0 : xoffset);
yoffset = (yoffset < 0 ? 0 : yoffset);
break;
}
}
if (image != null)
{
chunk = new Chunk(image, 0, 0);
scaledWidth = image.getScaledWidth();
scaledHeight = image.getScaledHeight();
}
}
else
{
double normalWidth = availableImageWidth;
double normalHeight = availableImageHeight;
double displayWidth = availableImageWidth;
double displayHeight = availableImageHeight;
double ratioX = 1f;
double ratioY = 1f;
Rectangle2D clip = null;
Dimension2D dimension = renderer.getDimension();
if (dimension != null)
{
normalWidth = dimension.getWidth();
normalHeight = dimension.getHeight();
displayWidth = normalWidth;
displayHeight = normalHeight;
float xalignFactor = getXAlignFactor(printImage);
float yalignFactor = getYAlignFactor(printImage);
switch (printImage.getScaleImageValue())
{
case CLIP:
{
xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
clip =
new Rectangle2D.Double(
- xoffset,
- yoffset,
availableImageWidth,
availableImageHeight
);
break;
}
case FILL_FRAME:
{
ratioX = availableImageWidth / normalWidth;
ratioY = availableImageHeight / normalHeight;
normalWidth *= ratioX;
normalHeight *= ratioY;
xoffset = 0;
yoffset = 0;
break;
}
case RETAIN_SHAPE:
default:
{
ratioX = availableImageWidth / normalWidth;
ratioY = availableImageHeight / normalHeight;
ratioX = ratioX < ratioY ? ratioX : ratioY;
ratioY = ratioX;
normalWidth *= ratioX;
normalHeight *= ratioY;
xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
break;
}
}
}
PdfTemplate template = pdfContentByte.createTemplate((float)displayWidth, (float)displayHeight);
Graphics2D g = forceSvgShapes
? template.createGraphicsShapes((float)displayWidth, (float)displayHeight)
: template.createGraphics(availableImageWidth, availableImageHeight, new LocalFontMapper());
if (clip != null)
{
g.setClip(clip);
}
if (printImage.getModeValue() == ModeEnum.OPAQUE)
{
g.setColor(printImage.getBackcolor());
g.fillRect(0, 0, (int)displayWidth, (int)displayHeight);
}
Rectangle2D rectangle = new Rectangle2D.Double(0, 0, displayWidth, displayHeight);
renderer.render(g, rectangle);
g.dispose();
pdfContentByte.saveState();
pdfContentByte.addTemplate(
template,