You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Eric Hamacher <er...@enetrix.com> on 2008/03/06 19:10:10 UTC

dynamic changes to svg not appearing

Hello again:

 

I am loading an existing SVG file into a Document and adding some nodes
to it.

 

When I take the resulting Document and transcode it to JPEG or PNG, the
additions (a new <path>) I made do not show up in the image.  I printed
the dom to inspect it and my additions were present and formatted
correctly.

 

            //Load dom

String parser = XMLResourceDescriptor.getXMLParserClassName();

        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);

        String uri = new File(getServletContext().getRealPath("/") +
"svg\\polarity.svg").toURL().toString();

        SVGDocument doc = (SVGDocument) f.createDocument(uri);  

 

            . . .

 

            //perform addition of new <path> . . .

            

. . .

 

        DOMUtil.printDOM(doc);   //Print dom to console - everything is
fine

       

        response.setContentType("image/png");

        ServletOutputStream out = response.getOutputStream();

        PNGTranscoder t = new PNGTranscoder();

        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));

        t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, new
Float(1000));

        t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(1000));

        TranscoderInput input = new TranscoderInput(doc);

        

        TranscoderOutput output = new TranscoderOutput(out);

        try {

            t.transcode(input, output);

        }

        catch (TranscoderException e) {

            e.printStackTrace();

        }

 

        out.flush();

        out.close();

 

 

The added node prints as:

 

&lt;path
style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:10px;stro
ke-linecap:butt;strike-linejoin:miter;stroke-opacity:1" d="M -120,-300 C
-200,-300 -200,0 -200,0" id="curveA">

 

08/03/06 11:54:18 </path>

Other paths have the &lt; and are not affected.   Any suggestions?

 

 

 

Regards,

Eric Hamacher

 

******************************

THIS EMAIL IS INTENDED ONLY FOR THE REVIEW OF THE ADDRESSEE(S), AND MAY
CONTAIN CONFIDENTIAL AND LEGALLY PRIVILEGED INFORMATION. INTERCEPTION,
COPYING, DISSEMINATION, OR OTHER USE BY OTHER THAN THE ADDRESSEE(S) IS
PROHIBITED AND MAY BE PENALIZED UNDER APPLICABLE PRIVACY LAWS. IF YOU
RECEIVED THIS EMAIL IN ERROR, PLEASE DELETE IT AND NOTIFY ME BY RETURN
EMAIL TO eric.hamacher@enetrix.com *******************************

 


RE: dynamic changes to svg not appearing

Posted by Eric Hamacher <er...@enetrix.com>.
        
Here is the entire thing.  Thanks:



double actionPositive = new Double(request.getParameter("actpos")).doubleValue() * 100.0;
        double actionNegative = new Double(request.getParameter("actneg")).doubleValue() * 100.0;
        double reflectionPositive = new Double(request.getParameter("refpos")).doubleValue() * 100.0;
        double reflectionNegative = new Double(request.getParameter("refneg")).doubleValue() * 100.0;
                       
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        String uri = new File(getServletContext().getRealPath("/") + "svg\\polarity.svg").toURL().toString();
        SVGDocument doc = (SVGDocument) f.createDocument(uri);     
        
        double a = 200;
        double b = 120;
        
        
        
        Element svg = doc.getDocumentElement();


		//Set the viewBox attribute of svg element
        Attr viewBoxAttr = doc.createAttribute("viewBox");
        double upperlefty = 0;
        double upperleftx = -(a + 100.0);
        double lowerrightx = a + 100.0;
        double lowerrighty = 0;
        if (actionPositive >= reflectionPositive) {
            upperlefty = -(actionPositive + 100.0); 
        }
        else {
            upperlefty = -(reflectionPositive + 100);
        }
        if (actionNegative >= reflectionNegative) {
            lowerrighty = actionNegative + 100.0; 
        }
        else {
            lowerrighty = reflectionNegative + 100.0;
        }
        viewBoxAttr.setNodeValue(upperleftx + " " + upperlefty + " " + 2*lowerrightx + " " + (-upperlefty + lowerrighty));
        svg.setAttributeNode(viewBoxAttr);

        

		//Set the width and height attributes of svg element
        Attr widthAttr = doc.createAttribute("width");
        Attr heightAttr = doc.createAttribute("height");
        
        widthAttr.setNodeValue("" + (-upperleftx + lowerrightx));
        heightAttr.setNodeValue("" + (-upperlefty + lowerrighty));
        
        svg.setAttributeNode(widthAttr);
        svg.setAttributeNode(heightAttr);



		//Get the g element and add a new <path> for a cubic Bezier 
		// curve (new <path> will have id,d and style attributes)
        NodeList gList = svg.getElementsByTagName("g");
        Element g = (Element) gList.item(0);
      
        Element path = doc.createElement("path");

        Attr idAttr = doc.createAttribute("id");
        idAttr.setNodeValue("curveA");
	  path.setAttributeNode(idAttr);

        Attr dAttr = doc.createAttribute("d");
        String ad = "M " + Math.round(-b) + "," + Math.round(-actionPositive) +
            " C " + Math.round(-a) + "," + Math.round(-actionPositive) +
            " " + Math.round(-a) + "," + (Math.round(actionPositive - actionNegative)/2) +
            " " + Math.round(-a) + "," + (Math.round(actionPositive - actionNegative)/2);
        dAttr.setNodeValue(ad);
	  path.setAttributeNode(dAttr);
        

        Attr styleAttr = doc.createAttribute("style"); 
	  String style = "fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:10px;" +
            "stroke-linecap:butt;strike-linejoin:miter;stroke-opacity:1"; 
        styleAttr.setNodeValue(style);
        path.setAttributeNode(styleAttr);
                
        g.appendChild(path);
        
        DOMUtil.printDOM(doc); //prints dom to console - no effect if 						//withheld
        
        
        response.setContentType("image/png");
        ServletOutputStream out = response.getOutputStream();
        //JPEGTranscoder t = new JPEGTranscoder();
        PNGTranscoder t = new PNGTranscoder();
        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
        t.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, new Float(1000));
        t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(1000));
        
        TranscoderInput input = new TranscoderInput(doc);
        
        
        TranscoderOutput output = new TranscoderOutput(out);
        try {
            t.transcode(input, output);
        }
        catch (TranscoderException e) {
            e.printStackTrace();
        }

        out.flush();
        out.close();


this yields: 

&lt;path style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:10px;stroke-linecap:butt;strike-linejoin:miter;stroke-opacity:1" d="M -120,-300 C -200,-300 -200,0 -200,0" id="curveA">

which is within <g> which is within <svg>


-----Original Message-----
From: Cameron McCormack [mailto:cam@mcc.id.au] 
Sent: Thursday, March 06, 2008 1:58 PM
To: batik-users@xmlgraphics.apache.org
Subject: Re: dynamic changes to svg not appearing

Hi Eric.

Eric Hamacher:
> I am loading an existing SVG file into a Document and adding some nodes
> to it.
> 
> When I take the resulting Document and transcode it to JPEG or PNG, the
> additions (a new <path>) I made do not show up in the image.  I printed
> the dom to inspect it and my additions were present and formatted
> correctly.
…
>             //perform addition of new <path> . . .

I think you’ll have to show us this part of your code to see if there’s
any problem with it.  The rest of the code looks OK.

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


RE: dynamic changes to svg not appearing

Posted by Eric Hamacher <er...@enetrix.com>.
Hello all:

 

I finally got my curves to draw on the screen.  The trick was to use namespace-aware methods some of the time.  I had to mix and match.  So I was very fortunate to strike the correct formula.  Among the methods used: 

 

doc.createElementNS

doc.createAttribute

doc.createAttributeNS

path.setAttributeNodeNS

 

Well, once again I thank all of your input.  I’d be dead in the water without this mailing list!

 

Thanks,

Eric

________________________________

From: Abraham Rodriguez Mota [mailto:abraham.rdgz@gmail.com] 
Sent: Friday, March 07, 2008 4:41 AM
To: batik-users@xmlgraphics.apache.org
Subject: Re: dynamic changes to svg not appearing

 

By the way, I am sorry if I miss lead you in my last email
addAttributesNS<-- doesn't exist on the specification. A real example should be:

anyElement.setAttributeNS(null,"color","red");

All the best
Abraham

On Fri, Mar 7, 2008 at 2:18 AM, G. Wade Johnson <gw...@anomaly.org> wrote:

On Thu, 6 Mar 2008 21:05:27 +0000
"Abraham Rodriguez Mota" <ab...@gmail.com> wrote:

> Hi Eric,
>
> I think the problem is that you are not using the name space aware
> methods of batik.
>
> anyElement.addAttributesNS(svgNS,"color","red");

Although we do need to use the namespace-aware methods, most SVG
attributes are in the null namespace.

anyElement.addAttributesNS(null,"color","red");



> However, If you want a simple but raw solution, and if you don't have
> problems saving and loading files, you can try just to save the file
> after changes and reload it from the file system . Batik will  read
> it properly, I guess.
>
> All the best
> Abraham.
>
>
>
> On Thu, Mar 6, 2008 at 7:57 PM, Cameron McCormack <ca...@mcc.id.au>
> wrote:
>
> > Hi Eric.
> >
> > Eric Hamacher:
> > > I am loading an existing SVG file into a Document and adding some
> > > nodes to it.
> > >
> > > When I take the resulting Document and transcode it to JPEG or
> > > PNG, the additions (a new <path>) I made do not show up in the
> > > image.  I printed the dom to inspect it and my additions were
> > > present and formatted correctly.
> > …
> > >             //perform addition of new <path> . . .
> >
> > I think you'll have to show us this part of your code to see if
> > there's any problem with it.  The rest of the code looks OK.
> >
> > --
> > Cameron McCormack, http://mcc.id.au/
> >        xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > batik-users-unsubscribe@xmlgraphics.apache.org For additional
> > commands, e-mail: batik-users-help@xmlgraphics.apache.org
> >
> >



--
"Half of being clever is making certain you are not being stupid."
                                    -- "Owlflight", Mercedes Lackey


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

 


Re: dynamic changes to svg not appearing

Posted by Abraham Rodriguez Mota <ab...@gmail.com>.
By the way, I am sorry if I miss lead you in my last email
addAttributesNS<-- doesn't exist on the specification. A real example should
be:

anyElement.setAttributeNS(null,"color","red");

All the best
Abraham

On Fri, Mar 7, 2008 at 2:18 AM, G. Wade Johnson <gw...@anomaly.org> wrote:

> On Thu, 6 Mar 2008 21:05:27 +0000
> "Abraham Rodriguez Mota" <ab...@gmail.com> wrote:
>
> > Hi Eric,
> >
> > I think the problem is that you are not using the name space aware
> > methods of batik.
> >
> > anyElement.addAttributesNS(svgNS,"color","red");
>
> Although we do need to use the namespace-aware methods, most SVG
> attributes are in the null namespace.
>
> anyElement.addAttributesNS(null,"color","red");
>
>
> > However, If you want a simple but raw solution, and if you don't have
> > problems saving and loading files, you can try just to save the file
> > after changes and reload it from the file system . Batik will  read
> > it properly, I guess.
> >
> > All the best
> > Abraham.
> >
> >
> >
> > On Thu, Mar 6, 2008 at 7:57 PM, Cameron McCormack <ca...@mcc.id.au>
> > wrote:
> >
> > > Hi Eric.
> > >
> > > Eric Hamacher:
> > > > I am loading an existing SVG file into a Document and adding some
> > > > nodes to it.
> > > >
> > > > When I take the resulting Document and transcode it to JPEG or
> > > > PNG, the additions (a new <path>) I made do not show up in the
> > > > image.  I printed the dom to inspect it and my additions were
> > > > present and formatted correctly.
> > > …
> > > >             //perform addition of new <path> . . .
> > >
> > > I think you'll have to show us this part of your code to see if
> > > there's any problem with it.  The rest of the code looks OK.
> > >
> > > --
> > > Cameron McCormack, http://mcc.id.au/
> > >        xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > > batik-users-unsubscribe@xmlgraphics.apache.org For additional
> > > commands, e-mail: batik-users-help@xmlgraphics.apache.org
> > >
> > >
>
>
> --
> "Half of being clever is making certain you are not being stupid."
>                                     -- "Owlflight", Mercedes Lackey
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: dynamic changes to svg not appearing

Posted by "G. Wade Johnson" <gw...@anomaly.org>.
On Thu, 6 Mar 2008 21:05:27 +0000
"Abraham Rodriguez Mota" <ab...@gmail.com> wrote:

> Hi Eric,
> 
> I think the problem is that you are not using the name space aware
> methods of batik.
> 
> anyElement.addAttributesNS(svgNS,"color","red");

Although we do need to use the namespace-aware methods, most SVG
attributes are in the null namespace.

anyElement.addAttributesNS(null,"color","red");


> However, If you want a simple but raw solution, and if you don't have
> problems saving and loading files, you can try just to save the file
> after changes and reload it from the file system . Batik will  read
> it properly, I guess.
> 
> All the best
> Abraham.
> 
> 
> 
> On Thu, Mar 6, 2008 at 7:57 PM, Cameron McCormack <ca...@mcc.id.au>
> wrote:
> 
> > Hi Eric.
> >
> > Eric Hamacher:
> > > I am loading an existing SVG file into a Document and adding some
> > > nodes to it.
> > >
> > > When I take the resulting Document and transcode it to JPEG or
> > > PNG, the additions (a new <path>) I made do not show up in the
> > > image.  I printed the dom to inspect it and my additions were
> > > present and formatted correctly.
> > …
> > >             //perform addition of new <path> . . .
> >
> > I think you'll have to show us this part of your code to see if
> > there's any problem with it.  The rest of the code looks OK.
> >
> > --
> > Cameron McCormack, http://mcc.id.au/
> >        xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > batik-users-unsubscribe@xmlgraphics.apache.org For additional
> > commands, e-mail: batik-users-help@xmlgraphics.apache.org
> >
> >


-- 
"Half of being clever is making certain you are not being stupid."
                                     -- "Owlflight", Mercedes Lackey

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: dynamic changes to svg not appearing

Posted by Abraham Rodriguez Mota <ab...@gmail.com>.
Hi Eric,

I think the problem is that you are not using the name space aware methods
of batik.

anyElement.addAttributesNS(svgNS,"color","red");


However, If you want a simple but raw solution, and if you don't have
problems saving and loading files, you can try just to save the file after
changes and reload it from the file system . Batik will  read it properly, I
guess.

All the best
Abraham.



On Thu, Mar 6, 2008 at 7:57 PM, Cameron McCormack <ca...@mcc.id.au> wrote:

> Hi Eric.
>
> Eric Hamacher:
> > I am loading an existing SVG file into a Document and adding some nodes
> > to it.
> >
> > When I take the resulting Document and transcode it to JPEG or PNG, the
> > additions (a new <path>) I made do not show up in the image.  I printed
> > the dom to inspect it and my additions were present and formatted
> > correctly.
> …
> >             //perform addition of new <path> . . .
>
> I think you'll have to show us this part of your code to see if there's
> any problem with it.  The rest of the code looks OK.
>
> --
> Cameron McCormack, http://mcc.id.au/
>        xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

Re: dynamic changes to svg not appearing

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi Eric.

Eric Hamacher:
> I am loading an existing SVG file into a Document and adding some nodes
> to it.
> 
> When I take the resulting Document and transcode it to JPEG or PNG, the
> additions (a new <path>) I made do not show up in the image.  I printed
> the dom to inspect it and my additions were present and formatted
> correctly.
…
>             //perform addition of new <path> . . .

I think you’ll have to show us this part of your code to see if there’s
any problem with it.  The rest of the code looks OK.

-- 
Cameron McCormack, http://mcc.id.au/
	xmpp:heycam@jabber.org  ▪  ICQ 26955922  ▪  MSN cam@mcc.id.au

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org