You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Paul Reavis <pr...@partnersoft.com> on 2002/05/23 20:48:24 UTC

forceloading batik svg

This is probably a question for the batik list, but I'm not on that at
present so thought I'd ask here. 

What's the best way to get an SVG loaded so I can draw it to a
buffered image? I basically want to load an SVG and turn it into a
raster icon. I'm trying to use JSVGComponent but the loading is so
multithreaded I can't figure it out at all. Just trying to listen does
nothing - I wait forever for a loaded event.

 It seems similar to the annoying Java default image loading
behavior where it incrementally loads images - great for web-like
incremental drawing effects, but annoying if you just want to load an
image, right now, no questions.

I think it might be waiting on something - a paint() command or
similar - to actually load - lazy loading, in other words.

Here's my current craptastic code. It generates the following:
        Loading an SVG document as a Component.
        Loading document from file:/bag2/test/svg/arrowhead-blue.svg
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
        Waiting on SVG to load...
ERROR   Exception
 due to java.lang.NullPointerException: null
java.lang.NullPointerException
        at
org.apache.batik.swing.svg.JSVGComponent.getSVGDocumentSize(Unknown
Source)
        at
com.PartnerSoft.gui.SVGLib.createSVGComponent(SVGLib.java:52)
        at
com.PartnerSoft.gui.SVGLib.createImageFromSVG(SVGLib.java:28)
        at
com.PartnerSoft.gui.LocalImageFactory.getImage(LocalImageFactory.java


    private static JLabel scapegoat = new JLabel("BAAAA!!");

    public static Image createImageFromSVG(File svgFile) {
        try {
            Component component = createSVGComponent(svgFile);
            Image result = scapegoat.createImage(component.getWidth(),
component.getHeight());
            Graphics g = result.getGraphics();
            component.paint(g);
            g.dispose();
            return result;
            }
        catch (Exception oopsie) {
            SystemLog.singleton().error(oopsie);
            return GUILib.questionMark();
            }
            
        }

    public static Component createSVGComponent(File svgFile) {
        try {
            SystemLog.singleton().enter("Loading an SVG document as a
Component.");
            JSVGComponent component = new JSVGComponent();
            component.setProgressivePaint(false);
            String url = "file:" + svgFile.getCanonicalPath(); 
            SystemLog.singleton().enter("Loading document from " +
url);
            component.loadSVGDocument(url);
            SVGEar ear = new SVGEar();
            component.addSVGDocumentLoaderListener(ear);
            while (component.getSVGDocument() == null 
                   || component.getSVGDocumentSize() == null
                   || component.getSVGDocumentSize().getWidth() == 0
                   ) {
                SystemLog.singleton().enter("Waiting on SVG to
load...");
                try {Thread.sleep(100);} catch (InterruptedException
oopsie) {}
                }
            Dimension2D preferred = component.getSVGDocumentSize();
            SystemLog.singleton().enter("SVG is " + preferred);
            component.setSize((int)preferred.getWidth(),
(int)preferred.getHeight());
            return component;
            }
        catch (Exception oopsie) {
            SystemLog.singleton().error(oopsie);
            return new JLabel("AAIIEE!!");
            }

        }

    private static class SVGEar extends SVGDocumentLoaderAdapter {
        public boolean done = false;
        public void documentLoadingCompleted(SVGDocumentLoaderEvent
evt) {
            done = true;
            }
        }


I was adding that SVGEar as a loaderlistener to the component but it
never got an event (done was always false).


-- 

Paul Reavis                                      preavis@partnersoft.com
Design Lead
Partner Software, Inc.                        http://www.partnersoft.com

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: forceloading batik svg

Posted by Keiron Liddle <ke...@aftexsw.com>.
On Thu, 2002-05-23 at 20:48, Paul Reavis wrote:
> This is probably a question for the batik list, but I'm not on that at
> present so thought I'd ask here. 

I'm sure they would know better...

> What's the best way to get an SVG loaded so I can draw it to a
> buffered image? I basically want to load an SVG and turn it into a
> raster icon. I'm trying to use JSVGComponent but the loading is so
> multithreaded I can't figure it out at all. Just trying to listen does
> nothing - I wait forever for a loaded event.
> 
>  It seems similar to the annoying Java default image loading
> behavior where it incrementally loads images - great for web-like
> incremental drawing effects, but annoying if you just want to load an
> image, right now, no questions.
> 
> I think it might be waiting on something - a paint() command or
> similar - to actually load - lazy loading, in other words.

As far as I know the JSVGCanvas is really for viewing the document. This
means it also sets up a bunch of event handlers and threads.

This is the sort of code that you need (without error handling and other
things).

GVTBuilder builder = new GVTBuilder();
BridgeContext ctx = new BridgeContext(userAgent);
GraphicsNode root = builder.build(ctx, doc);
float w = (float)ctx.getDocumentSize().getWidth();
float h = (float)ctx.getDocumentSize().getHeight();
Graphics2D graphics = create graphic from buffered image...
root.paint(graphics);





---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org