As i am using transcode(TranscoderInput,TranscoderOutput);method in my
rasterizer class. The "uri" attribute is null. The same uri's ref object
only we are passing to ViewBox's ViewBox.getViewTransform(ref, root,
width, height) method. It checks whether ref element is null or not. If it
is null it calls getPreserveAspectRatioTransform() method. That is why i am
not able to get the desired behaviour.
I tried setting affinetranform's SX,SY only but it gives me wiered
behaviour. Pls let me know how should i move further. i am attaching
dummyImageTranscoder.java
thanks in advace
Bala
public abstract class DummyImageTrascoder extends ImageTranscoder
{
double scalex=-1;
double scaley=-1;
DummyImageTrascoder(){}
DummyImageTrascoder(double sx,double sy)
{
scalex = sx;
scaley = sy;
}
protected void transcode(Document document,String uri,TranscoderOutput
output)
throws TranscoderException
{
if (!(document instanceof SVGOMDocument))
{
throw new TranscoderException(
Messages.formatMessage("notsvg", null));
}
SVGDocument svgDoc = (SVGDocument)document;
// set the alternate stylesheet if any
if (hints.containsKey(KEY_ALTERNATE_STYLESHEET))
{
String stylesheetName =
(String)hints.get(KEY_ALTERNATE_STYLESHEET);
((SVGOMDocument)svgDoc).enableAlternateStyleSheet(stylesheetName);
}
SVGSVGElement root = svgDoc.getRootElement();
// initialize the SVG document with the appropriate context
DefaultSVGContext svgCtx = new DefaultSVGContext();
svgCtx.setPixelToMM(userAgent.getPixelToMM());
((SVGOMDocument)document).setSVGContext(svgCtx);
// build the GVT tree
GVTBuilder builder = new GVTBuilder();
ImageRendererFactory rendFactory = new
ConcreteImageRendererFactory();
BridgeContext ctx = new BridgeContext(userAgent);
// flag that indicates if the document is dynamic
boolean isDynamic =
(hints.containsKey(KEY_EXECUTE_ONLOAD) &&
((Boolean)hints.get(KEY_EXECUTE_ONLOAD)).booleanValue() &&
BaseScriptingEnvironment.isDynamicDocument(svgDoc));
ctx.setDynamic(isDynamic);
GraphicsNode gvtRoot;
try
{
gvtRoot = builder.build(ctx, svgDoc);
// dispatch an 'onload' event if needed
if (ctx.isDynamic())
{
BaseScriptingEnvironment se = new
BaseScriptingEnvironment(ctx);
se.loadScripts();
se.dispatchSVGLoadEvent();
}
} catch (BridgeException ex)
{
throw new TranscoderException(ex);
}
// get the 'width' and 'height' attributes of the SVG document
float docWidth = (float)ctx.getDocumentSize().getWidth();
float docHeight = (float)ctx.getDocumentSize().getHeight();
ctx = null;
builder = null;
// compute the image's width and height according the hints
float imgWidth = -1;
if (hints.containsKey(KEY_WIDTH))
{
imgWidth = ((Float)hints.get(KEY_WIDTH)).floatValue();
}
float imgHeight = -1;
if (hints.containsKey(KEY_HEIGHT))
{
imgHeight = ((Float)hints.get(KEY_HEIGHT)).floatValue();
}
float width, height;
if (imgWidth > 0 && imgHeight > 0)
{
System.out.println("imgWidth > 0 && imgHeight > 0");
width = imgWidth;
height = imgHeight;
}
else if (imgHeight > 0)
{
System.out.println("imgHeight > 0");
width = (docWidth * imgHeight) / docHeight;
height = imgHeight;
}
else if (imgWidth > 0)
{
System.out.println("imgWidth > 0");
width = imgWidth;
height = (docHeight * imgWidth) / docWidth;
}
else
{
width = docWidth;
height = docHeight;
}
// compute the preserveAspectRatio matrix
AffineTransform Px;
String ref = null;
try
{
ref = new URL(uri).getRef();
}catch (MalformedURLException ex)
{
// nothing to do, catched previously
System.out.println(ex);
}
try
{
Px = ViewBox.getViewTransform(ref, root, width, height);
}catch (BridgeException ex)
{
throw new TranscoderException(ex);
}
/* if(scalex>0&&scaley>0)
Px.setToScale(scalex,scaley);*/
System.out.println("Scalex" + Px.getScaleX() + " scaley "
+Px.getScaleY()+" Width "+width+" height "+height+ref);
if (Px.isIdentity() && (width != docWidth || height != docHeight))
{
// The document has no viewBox, we need to resize it by hand.
// we want to keep the document size ratio
float d = Math.max(docWidth, docHeight);
float dd = Math.max(width, height);
float scale = dd/d;
System.out.println("d "+d+" dd "+dd+" scale "+scale);
Px = AffineTransform.getScaleInstance(scale, scale);
}
// take the AOI into account if any
if (hints.containsKey(KEY_AOI))
{
System.out.println("KEY_AOI");
Rectangle2D aoi = (Rectangle2D)hints.get(KEY_AOI);
// transform the AOI into the image's coordinate system
aoi = Px.createTransformedShape(aoi).getBounds2D();
AffineTransform Mx = new AffineTransform();
double sx = width / aoi.getWidth();
double sy = height / aoi.getHeight();
Mx.scale(sx, sy);
double tx = -aoi.getX();
double ty = -aoi.getY();
Mx.translate(tx, ty);
// take the AOI transformation matrix into account
// we apply first the preserveAspectRatio matrix
Px.preConcatenate(Mx);
}
// prepare the image to be painted
int w = (int)(width+0.5);
int h = (int)(height+0.5);
System.out.println("Width "+w);
System.out.println("Height "+h);
// paint the SVG document using the bridge package
// create the appropriate renderer
ImageRenderer renderer = rendFactory.createStaticImageRenderer();
renderer.updateOffScreen(w, h);
renderer.setTransform(Px);
renderer.setTree(gvtRoot);
gvtRoot = null; // We're done with it...
try
{
// now we are sure that the aoi is the image size
Shape raoi = new Rectangle2D.Float(0, 0, width, height);
// Warning: the renderer's AOI must be in user space
renderer.repaint(Px.createInverse().createTransformedShape(raoi));
BufferedImage rend = renderer.getOffScreen();
renderer = null; // We're done with it...
BufferedImage dest = createImage(w, h);
Graphics2D g2d = GraphicsUtil.createGraphics(dest);
if (hints.containsKey(KEY_BACKGROUND_COLOR))
{
Paint bgcolor = (Paint)hints.get(KEY_BACKGROUND_COLOR);
g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(bgcolor);
g2d.fillRect(0, 0, w, h);
}
if (rend != null) // might be null if the svg document is
empty
{
g2d.drawRenderedImage(rend, new AffineTransform());
}
g2d.dispose();
rend = null; // We're done with it...
writeImage(dest, output);
}catch (Exception ex)
{
throw new TranscoderException(ex);
}
}
}
-----Original Message-----
From: Thierry Kormann [mailto:[EMAIL PROTECTED]]
Sent: Friday, May 03, 2002 12:44 PM
To: Batik Users
Subject: RE: Problem with the rasterizer
> i am using following class to construct the Buffered image from SVG
> file. I got this code from Batik users site only. In this when i set
> TranscodingHints.setHeight, TranscodingHints.setWidth to different values
> say width=50 and height=750. This is not setting the height properly. It
> just renders it as 50, 50. Is image transcoder keeps the aspect ration in
> case of diffence betweek width and height is too. Is there any way to
> overcome that.
The aspect ratio is kept. I can add an a new TranscodingHint to bypass that.
I will let you know when the patch will be included.
You can for now, subclass the ImageTranscoder, copy/paste the code and add a
simple test around the width/height TranscodingHints check and computation
to do what you want.
Regards,
Thierry.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
"THIS E-MAIL MESSAGE ALONG WITH ANY ATTACHMENTS IS INTENDED ONLY FOR THE
ADDRESSEE and may contain confidential and privileged information.
If the reader of this message is not the intended recipient,
you are notified that any dissemination, distribution or copy of this
communication is strictly Prohibited.
If you have received this message by error, please notify us
immediately, return the original mail to the sender and delete the
message from your system."
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]