Hi Thomas,

On Mar 4, 2006, at 8:29 AM, [EMAIL PROTECTED] wrote:

How about this for an alternative:

1.  Convert all the SVG's to PNG files
2.  Add them to a document as <image> elements

   This is certainly a viable alternative.

I'm giving this a shot for now; if it ends up not panning out I'll feel like I've done due diligence when I go back to the graphics folks to ask them to simplify the SVG images.

I'm having trouble getting the initial embedded PNG to size itself correctly when setting the original document on the JSVGCanvas. The image is larger than the initial size of the canvas, but it doesn't scale down correctly. Instead, I only see the upper-left corner of it.

Here's the code:

<snip>

// Load the PNG from the file
FileInputStream fileIn = new FileInputStream(inputFile);

PNGImageDecoder decoder = new PNGImageDecoder(fileIn, null);
RenderedImage image = decoder.decodeAsRenderedImage();
fileIn.close();

// Create the document & the image element
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
Document document = impl.createDocument(SVG_NS, "svg", null);

Element imgElement = document.createElementNS(SVG_NS, "image");

imgElement.setAttributeNS(null, "x", "0");
imgElement.setAttributeNS(null, "y", "0");
imgElement.setAttributeNS(null, "width", String.valueOf(image.getWidth ())); imgElement.setAttributeNS(null, "height", String.valueOf (image.getHeight()));

// Encode the PNG into the element
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Base64EncoderStream b64Encoder = new Base64EncoderStream(outStream);

ImageEncoder encoder = new PNGImageEncoder(b64Encoder, null);
try {
    encoder.encode(image);
    b64Encoder.close();
} catch (IOException e) {
    e.printStackTrace();
}

imgElement.setAttributeNS(XLINK_NAMESPACE_URI,
                    ATTR_XLINK_HREF,
                    DATA_PROTOCOL_PNG_PREFIX + outStream.toString());

// Attach the new element to the empty document
Element svgRoot = document.getDocumentElement();
svgRoot.appendChild(imgElement);

// Set the document on the canvas
JSVGCanvas canvas = gui.getCanvas();
canvas.setDocument(document);

</snip>


Interestingly enough, if I first set an SVG file as the document (I created a blank SVG file in Illustrator), then append PNG's to that, they are sized correctly. Apparently I'm not creating my initial document correctly ... ?

Also, I've noticed I'm not able to attach event listeners to image elements in the same way I was with SVG elements. Is there a difference in how those should be treated?


Again, thanks for your time,

Peter

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to