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 Alex Chew <ch...@gmail.com> on 2006/03/14 03:07:45 UTC

how can i generate clipPath?

Hi, all
   I am trying to generate some clipPath dynamically using batik, and then
apply them to specified elements. In my opinion, i can reach this through
following steps:
1, generate clippath and hold IDs
2, get specified elements and set attribute clippath
But,
I cannot get ID of clipPaths for either Graphics2D.clip() or
Graphics2D.setClip() returns void.
Moreover , i cannot find <clippath> element even i use Grpahics2D.setClip
(0,0,10,10).

would you please show me correct way?

Re: how can i generate clipPath?

Posted by Alex Chew <ch...@gmail.com>.
Thanks a lot, Thomas.

    I'll try that.

On 3/15/06, thomas.deweese@kodak.com <th...@kodak.com> wrote:
>
> Hi Alex,
>
> "Alex Chew" <ch...@gmail.com> wrote on 03/15/2006 12:43:21 AM:
>
> > Sorry for i didnot make me understood.
> > I want to modify an existing SVG file and add some ClipPaths to it for
> display
> > specified area.
> > There is no clippath on original SVG, so , i want to generate a new one,
> and
> > apply it to some element such as <g>. The new SVG file should look like
> this
> > <g style="clip-path:url(#clipPath);"><!--others--></g>
>
>   I would suggest simply constructing the SVG Elements you want using
> org.w3c.dom.Document.createElementNS(<svg namespace>, <element name>);
>
> Something like:
>        static final String SVGNS = "http://www.w3.org/2000/svg";
>        static int clipCnt=0;
>
>
>        String clipID = "clipPath_"+clipCnt++;
>        Element clipPath = document.createElementNS(SVGNS, "clipPath");
>        clipPath.setAttributeNS("id", clipID);
>
>        Element rect     = document.createElementNS(SVGNS, "rect");
>        rect.setAttributeNS(null, "x", "0");
>        rect.setAttributeNS(null, "y", "0");
>        rect.setAttributeNS(null, "width", "60");
>        rect.setAttributeNS(null, "height", "60");
>
>        clipPath.appendChild(rect);
>
>        Element defs     = document.getElementById("defs");
>        if (defs == null) {
>           defs = document.createElementNS(SVGNS, defs);
>           document.getRootElement().appendChild(defs);
>        }
>        defs.append(clipPath);
>
>        Element elemToClip = ....;
>        elemToClip.setAttributeNS(null, "clip-path", "url("+clipiD+")");
>
> >       //manipulate - add clipPath
> >       SVGGraphics2D generator = new SVGGraphics2D(doc);
> >       Shape circle = new Ellipse2D.Double (0,0,50,50);
> >       generator.setPaint(Color.red);
> >       generator.fill(circle);
> >       generator.draw(circle);
> >       generator.setPaint(Color.BLUE);
> >       generator.drawString("Dance on SVG",60,60);
> >       generator.setClip(0,0,60,60);
> >
> > Then, how can I use clipPath generated here?
> > Or, there are some other ways for this?
>
>        The SVGGenerator is not designed to modify existing documents.
> You can use it (a little painfully) to generate fragments to add to
> other documents but it's main purpose is to generate a complete
> SVG file that captures all drawing to a Graphics2D.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>


--
借鉴/思考/执行/检验/推广

Re: how can i generate clipPath?

Posted by th...@kodak.com.
Hi Alex,

"Alex Chew" <ch...@gmail.com> wrote on 03/15/2006 12:43:21 AM:

> Sorry for i didnot make me understood.
> I want to modify an existing SVG file and add some ClipPaths to it for 
display
> specified area.
> There is no clippath on original SVG, so , i want to generate a new one, 
and 
> apply it to some element such as <g>. The new SVG file should look like 
this 
> <g style="clip-path:url(#clipPath);"><!--others--></g>

   I would suggest simply constructing the SVG Elements you want using
org.w3c.dom.Document.createElementNS(<svg namespace>, <element name>);

  Something like:
        static final String SVGNS = "http://www.w3.org/2000/svg";
        static int clipCnt=0;


        String clipID = "clipPath_"+clipCnt++;
        Element clipPath = document.createElementNS(SVGNS, "clipPath");
        clipPath.setAttributeNS("id", clipID);

        Element rect     = document.createElementNS(SVGNS, "rect");
        rect.setAttributeNS(null, "x", "0");
        rect.setAttributeNS(null, "y", "0");
        rect.setAttributeNS(null, "width", "60");
        rect.setAttributeNS(null, "height", "60");
 
        clipPath.appendChild(rect);

        Element defs     = document.getElementById("defs");
        if (defs == null) {
           defs = document.createElementNS(SVGNS, defs);
           document.getRootElement().appendChild(defs);
        }
        defs.append(clipPath);

        Element elemToClip = ....;
        elemToClip.setAttributeNS(null, "clip-path", "url("+clipiD+")");

>       //manipulate - add clipPath
>       SVGGraphics2D generator = new SVGGraphics2D(doc);
>       Shape circle = new Ellipse2D.Double (0,0,50,50);
>       generator.setPaint(Color.red);
>       generator.fill(circle);
>       generator.draw(circle);
>       generator.setPaint(Color.BLUE);
>       generator.drawString("Dance on SVG",60,60); 
>       generator.setClip(0,0,60,60);
> 
> Then, how can I use clipPath generated here?
> Or, there are some other ways for this?

        The SVGGenerator is not designed to modify existing documents.
You can use it (a little painfully) to generate fragments to add to
other documents but it's main purpose is to generate a complete
SVG file that captures all drawing to a Graphics2D.

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


Re: how can i generate clipPath?

Posted by Alex Chew <ch...@gmail.com>.
Thomas, Thanks for your reply.
Sorry for i didnot make me understood.
I want to modify an existing SVG file and add some ClipPaths to it for
display specified area.
There is no clippath on original SVG, so , i want to generate a new one, and
apply it to some element such as <g>. The new SVG file should look like this

<g style="clip-path:url(#clipPath);"><!--others--></g>

I try to use SVGGraphics2D-sorry again-to reach that, following is code
fragment,

      //load an existing SVG file
      String parser = XMLResourceDescriptor.getXMLParserClassName();
      SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
      String filename = "D:\\old.svg";
      File file = new File(filename);
      String uri=file.toURI().toString();
      Document doc = f.createDocument(uri);
      //manipulate - add clipPath
      SVGGraphics2D generator = new SVGGraphics2D(doc);
      Shape circle = new Ellipse2D.Double(0,0,50,50);
      generator.setPaint(Color.red);
      generator.fill(circle);
      generator.draw(circle);
      generator.setPaint(Color.BLUE);
      generator.drawString("Dance on SVG",60,60);
      generator.setClip(0,0,60,60);

Then, how can I use clipPath generated here?
Or, there are some other ways for this?

Thanks
Alex

Re: how can i generate clipPath?

Posted by th...@kodak.com.
Hi Alex,

   I'm sorry I don't really get what you are trying to do.  A little more 
background might help...

"Alex Chew" <ch...@gmail.com> wrote on 03/13/2006 09:07:45 PM:

>    I am trying to generate some clipPath dynamically using batik, and 
then 
> apply them to specified elements. In my opinion, i can reach this 
through 
> following steps:
> 1, generate clippath and hold IDs

    When you create a clippath element why can't you assign it an ID?

> 2, get specified elements and set attribute clippath
> But,
> I cannot get ID of clipPaths for either Graphics2D.clip() or Graphics2D.
> setClip() returns void. 

    Why does Graphics2D come into this?  Are you trying to use clip
path with the SVGGraphics2D?  If so the SVGGraphics2D should generate
the clippath automatically for you.

> Moreover , i cannot find <clippath> element even i use 
Grpahics2D.setClip(0,0,10,10).

    The SVGGraphics2D may not create the clippath until you draw 
something...



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