public void handleImage(RenderingContext context, Image image, Rectangle pos)
throws IOException {
PSRenderingContext psContext = (PSRenderingContext)context;
PSGenerator gen = psContext.getGenerator();
ImageGraphics2D imageG2D = (ImageGraphics2D)image;
Graphics2DImagePainter painter = imageG2D.getGraphics2DImagePainter();
float fx = (float)pos.getX() / 1000f;
float fy = (float)pos.getY() / 1000f;
float fwidth = (float)pos.getWidth() / 1000f;
float fheight = (float)pos.getHeight() / 1000f;
// get the 'width' and 'height' attributes of the SVG document
Dimension dim = painter.getImageSize();
float imw = (float)dim.getWidth() / 1000f;
float imh = (float)dim.getHeight() / 1000f;
float sx = fwidth / (float)imw;
float sy = fheight / (float)imh;
gen.commentln("%FOPBeginGraphics2D");
gen.saveGraphicsState();
final boolean clip = false;
if (clip) {
// Clip to the image area.
gen.writeln("newpath");
gen.defineRect(fx, fy, fwidth, fheight);
gen.writeln("clip");
}
// transform so that the coordinates (0,0) is from the top left
// and positive is down and to the right. (0,0) is where the
// viewBox puts it.
gen.concatMatrix(sx, 0, 0, sy, fx, fy);
final boolean textAsShapes = false;
PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
AffineTransform transform = new AffineTransform();
// scale to viewbox
transform.translate(fx, fy);
gen.getCurrentState().concatMatrix(transform);
Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
painter.paint(graphics, area);
gen.restoreGraphicsState();
gen.commentln("%FOPEndGraphics2D");
}