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 Roger Critchlow <re...@elf.org> on 2006/05/10 05:56:31 UTC

Re: modifying css in live svg document

Hi Shin --

So I've done it your way, parsing out the style element CDATA and then
modifying it with the CDATA mutation operators in the DOM
updateThread.

But I don't see any change in the rendered graphics until I trigger a
redraw with a mouseover.

So I need to tweak the rendered elements so they pick up the new style?

-- rec --

On 4/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> Hi Roger,
>
> To hack the style sheet;
> ---- following must run under a RunnableQueue thread, needless to say ----
> SVGSVGElement root = document.getRootElement();
> SVGOMStyleElement style =
> (SVGOMStyleElement)getFirstChildByNodeName(root,"style");
> for (Node node=style.getFirstChild(); node!=null;
> node=node.getNextSibling()) {
>     if (node.getNodeType()==Node.CDATA_SECTION_NODE) {
>         CDATASection cdata = (CDATASection)node;
>         String value = cdata.getNodeValue();
>         int indx=0,indx1=0,indx2;
>         switch (textType) {
>         case GText.BLOCKNAME:   indx = value.indexOf(".bname");  break;
>         case GText.PORTNAME:    indx = value.indexOf(".pname");  break;
>         case GText.RESULT:              indx = value.indexOf(".result"); break;
>         }
>         indx1 = value.indexOf("fill:", indx);
>         if (indx1 < 0) {
>             indx1 = value.indexOf('}', indx);
>             cdata.insertData(indx1, ";fill:"+color);
>         } else {
>             indx1 += 5;
>             indx2 = value.indexOf('}', indx1);
>             cdata.replaceData(indx1, (indx2-indx1), color);
>         }
>         return;
>     }
> }
>
> public static Element getFirstChildByNodeName(Element parent, String
> nodeName) {
>     for (Node node=parent.getFirstChild(); node!=null;
> node=node.getNextSibling()) {
>         if (node.getNodeType()==Node.ELEMENT_NODE &&
> nodeName.equals(node.getNodeName()))
>             return (Element)node;
>         }
>     return null;
> }

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


Re: modifying css in live svg document

Posted by Roger Critchlow <re...@elf.org>.
Thomas --

You're right, rebuilding batik-all.jar from
svn:xmlgraphics/batik/tags/batik-1_6 works just fine.

-- rec --

On 5/11/06, thomas.deweese@kodak.com <th...@kodak.com> wrote:
> Hi Roger,
>
> rogercritchlow@gmail.com wrote on 05/10/2006 10:14:17 PM:
>
> > On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > > Why don't you try all separated batik jars in batik-1.6.zip instead of
> > > batik-all.jar?
> >
> > That did the trick.
> >
> > So batik-all.jar, though nothing is left undefined, doesn't work the
> > same as all the batik jars taken separately.  I wonder which parts of
> > batik are stepping on which other parts?
>
>    Actually I'm guessing this is a bug introduced in trunk.  Trunk
> has a fairly large number of modifications to the DOM so I can easily
> imagine that the listener for style element changes might have broken...
>
> > > > -----Original Message-----
> > > > From: rogercritchlow@gmail.com
> > > > [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> > > > Sent: Thursday, May 11, 2006 2:55 AM
> > > > To: batik-users@xmlgraphics.apache.org
> > > > Subject: Re: modifying css in live svg document
> > > >
> > > >
> > > > On 5/10/06, Roger Critchlow <re...@elf.org> wrote:
> > > > > Hi Shin --
> > > > >
> > > > > On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > > > > > Hi Roger,
> > > > > >
> > > > > > I also made a minimal sample.
> > > > >
> > > > > Well, that narrows things down a bit.  Your example doesn't
> > > > work for me either.
> > > > >
> > > > > I'm using batik-all.jar built from svn batik/trunk running
> > > > under j2sdk1.4.2_11.
> > > > >
> > > > > Is this possibly a symptom of the need to override the DOM3
> > > > > implementation in the jre?  I'll go read the FAQ, again, and see
> if
> > > > > that helps.
> > > >
> > > > I don't see how the DOM3 interfaces could be the problem.
> > > >
> > > > I've rebuilt svn batik/trunk under jdk1.5.0_06 and run the test
> under
> > > > 1.5 with no change in the result.
> > > >
> > > > Anyone else have any ideas?  Shin's example, a few posts up the
> > > > thread, modifies the style sheet of a loaded SVG document and he
> sees
> > > > an immediate change in the rendered document.  I see no change in
> the
> > > > rendered document running the same example.
> > > >
> > > > Why?
> > > >
> > > > -- rec --
> > > >
> > > >
> ---------------------------------------------------------------------
> > > > To unsubscribe, e-mail:
> batik-users-unsubscribe@xmlgraphics.apache.org
> > > > For additional commands, e-mail:
> batik-users-help@xmlgraphics.apache.org
> > > >
> > > >
> > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > > For additional commands, e-mail:
> batik-users-help@xmlgraphics.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> >
>
>

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


Re: modifying css in live svg document

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

rogercritchlow@gmail.com wrote on 05/10/2006 10:14:17 PM:

> On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > Why don't you try all separated batik jars in batik-1.6.zip instead of
> > batik-all.jar?
> 
> That did the trick.
> 
> So batik-all.jar, though nothing is left undefined, doesn't work the
> same as all the batik jars taken separately.  I wonder which parts of
> batik are stepping on which other parts?

   Actually I'm guessing this is a bug introduced in trunk.  Trunk
has a fairly large number of modifications to the DOM so I can easily
imagine that the listener for style element changes might have broken...

> > > -----Original Message-----
> > > From: rogercritchlow@gmail.com
> > > [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> > > Sent: Thursday, May 11, 2006 2:55 AM
> > > To: batik-users@xmlgraphics.apache.org
> > > Subject: Re: modifying css in live svg document
> > >
> > >
> > > On 5/10/06, Roger Critchlow <re...@elf.org> wrote:
> > > > Hi Shin --
> > > >
> > > > On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > > > > Hi Roger,
> > > > >
> > > > > I also made a minimal sample.
> > > >
> > > > Well, that narrows things down a bit.  Your example doesn't
> > > work for me either.
> > > >
> > > > I'm using batik-all.jar built from svn batik/trunk running
> > > under j2sdk1.4.2_11.
> > > >
> > > > Is this possibly a symptom of the need to override the DOM3
> > > > implementation in the jre?  I'll go read the FAQ, again, and see 
if
> > > > that helps.
> > >
> > > I don't see how the DOM3 interfaces could be the problem.
> > >
> > > I've rebuilt svn batik/trunk under jdk1.5.0_06 and run the test 
under
> > > 1.5 with no change in the result.
> > >
> > > Anyone else have any ideas?  Shin's example, a few posts up the
> > > thread, modifies the style sheet of a loaded SVG document and he 
sees
> > > an immediate change in the rendered document.  I see no change in 
the
> > > rendered document running the same example.
> > >
> > > Why?
> > >
> > > -- rec --
> > >
> > > 
---------------------------------------------------------------------
> > > To unsubscribe, e-mail: 
batik-users-unsubscribe@xmlgraphics.apache.org
> > > For additional commands, e-mail: 
batik-users-help@xmlgraphics.apache.org
> > >
> > >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > For additional commands, e-mail: 
batik-users-help@xmlgraphics.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


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


Re: modifying css in live svg document

Posted by Roger Critchlow <re...@elf.org>.
Shin --

Thanks.

On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> Hi Roger,
>
[ ... ]
> Why don't you try all separated batik jars in batik-1.6.zip instead of
> batik-all.jar?

That did the trick.

So batik-all.jar, though nothing is left undefined, doesn't work the
same as all the batik jars taken separately.  I wonder which parts of
batik are stepping on which other parts?

-- rec --



> > -----Original Message-----
> > From: rogercritchlow@gmail.com
> > [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> > Sent: Thursday, May 11, 2006 2:55 AM
> > To: batik-users@xmlgraphics.apache.org
> > Subject: Re: modifying css in live svg document
> >
> >
> > On 5/10/06, Roger Critchlow <re...@elf.org> wrote:
> > > Hi Shin --
> > >
> > > On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > > > Hi Roger,
> > > >
> > > > I also made a minimal sample.
> > >
> > > Well, that narrows things down a bit.  Your example doesn't
> > work for me either.
> > >
> > > I'm using batik-all.jar built from svn batik/trunk running
> > under j2sdk1.4.2_11.
> > >
> > > Is this possibly a symptom of the need to override the DOM3
> > > implementation in the jre?  I'll go read the FAQ, again, and see if
> > > that helps.
> >
> > I don't see how the DOM3 interfaces could be the problem.
> >
> > I've rebuilt svn batik/trunk under jdk1.5.0_06 and run the test under
> > 1.5 with no change in the result.
> >
> > Anyone else have any ideas?  Shin's example, a few posts up the
> > thread, modifies the style sheet of a loaded SVG document and he sees
> > an immediate change in the rendered document.  I see no change in the
> > rendered document running the same example.
> >
> > Why?
> >
> > -- rec --
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> > For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

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


RE: modifying css in live svg document

Posted by sugawara <su...@humane-systems.co.jp>.
Hi Roger,

I don't see what's wrong in your environment, but my environment is
eclipse3.1.1 as IDE with jre1.5.0_06 and all jars from batik1.6's lib
directory are in the classpath. Batik lib directroy is that was created
automatically from batik-1.6.zip in batik download site. Those batik
jars are;

batik-awt-util.jar
batik-bridge.jar
batik-css.jar
batik-dom.jar
batik-ext.jar
batik-extension.jar
batik-gui-util.jar
batik-gvt.jar
batik-parser.jar
batik-script.jar
batik-svg-dom.jar
batik-svggen.jar
batik-swing.jar
batik-transcoder.jar
batik-util.jar
batik-xml.jar
js.jar
pdf-transcoder.jar
xerces_2_5_0.jar
xml-apis.jar

Perhaps some of above is missing or different from stuff in your
batik-all.jar.
Why don't you try all separated batik jars in batik-1.6.zip instead of
batik-all.jar?


> -----Original Message-----
> From: rogercritchlow@gmail.com
> [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> Sent: Thursday, May 11, 2006 2:55 AM
> To: batik-users@xmlgraphics.apache.org
> Subject: Re: modifying css in live svg document
>
>
> On 5/10/06, Roger Critchlow <re...@elf.org> wrote:
> > Hi Shin --
> >
> > On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > > Hi Roger,
> > >
> > > I also made a minimal sample.
> >
> > Well, that narrows things down a bit.  Your example doesn't
> work for me either.
> >
> > I'm using batik-all.jar built from svn batik/trunk running
> under j2sdk1.4.2_11.
> >
> > Is this possibly a symptom of the need to override the DOM3
> > implementation in the jre?  I'll go read the FAQ, again, and see if
> > that helps.
>
> I don't see how the DOM3 interfaces could be the problem.
>
> I've rebuilt svn batik/trunk under jdk1.5.0_06 and run the test under
> 1.5 with no change in the result.
>
> Anyone else have any ideas?  Shin's example, a few posts up the
> thread, modifies the style sheet of a loaded SVG document and he sees
> an immediate change in the rendered document.  I see no change in the
> rendered document running the same example.
>
> Why?
>
> -- rec --
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>



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


Re: modifying css in live svg document

Posted by Roger Critchlow <re...@elf.org>.
On 5/10/06, Roger Critchlow <re...@elf.org> wrote:
> Hi Shin --
>
> On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> > Hi Roger,
> >
> > I also made a minimal sample.
>
> Well, that narrows things down a bit.  Your example doesn't work for me either.
>
> I'm using batik-all.jar built from svn batik/trunk running under j2sdk1.4.2_11.
>
> Is this possibly a symptom of the need to override the DOM3
> implementation in the jre?  I'll go read the FAQ, again, and see if
> that helps.

I don't see how the DOM3 interfaces could be the problem.

I've rebuilt svn batik/trunk under jdk1.5.0_06 and run the test under
1.5 with no change in the result.

Anyone else have any ideas?  Shin's example, a few posts up the
thread, modifies the style sheet of a loaded SVG document and he sees
an immediate change in the rendered document.  I see no change in the
rendered document running the same example.

Why?

-- rec --

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


Re: modifying css in live svg document

Posted by Roger Critchlow <re...@elf.org>.
Hi Shin --

On 5/10/06, sugawara <su...@humane-systems.co.jp> wrote:
> Hi Roger,
>
> I also made a minimal sample.

Well, that narrows things down a bit.  Your example doesn't work for me either.

I'm using batik-all.jar built from svn batik/trunk running under j2sdk1.4.2_11.

Is this possibly a symptom of the need to override the DOM3
implementation in the jre?  I'll go read the FAQ, again, and see if
that helps.

-- rec --

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


RE: modifying css in live svg document

Posted by sugawara <su...@humane-systems.co.jp>.
Hi Roger,

I also made a minimal sample.
It does not use any mouse listeners but does a key listener instead.
You can see the color of the <rect> element is toggled between
cyan and red by pressing any key.

Hope this saves your time to make your own example.

Good luck.

--------------------------------------------
package RogersIssue;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.StringReader;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGOMStyleElement;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererListener;
import org.apache.batik.util.RunnableQueue;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
import org.w3c.dom.svg.SVGSVGElement;

public class CSSTest extends JFrame {
  Canvas canvas;
  SVGDocument document;
  RunnableQueue runQ = null;
  String svg	=
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  + "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"
\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
  + "<svg width=\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\"
version=\"1.1\">\n"
  + "\t<style type=\"text/css\">"
  + "\t<![CDATA[.rect{fill:cyan}}]]>"
  + "\t</style>"
  + "\t<rect id=\"rect\" x=\"50\" y=\"50\" width=\"100\" height=\"100\"
class=\"rect\"/>\n"
  + "</svg>\n";

  public CSSTest() {
    super("");
    canvas = new Canvas();
    setContentPane(canvas);

    WindowAdapter windowAdapter = new WindowAdapter() {
      public void windowOpened(WindowEvent we) {
        SVGDocument doc = null;
        try {
          String parser = XMLResourceDescriptor.getXMLParserClassName();
          SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
          StringReader reader = new StringReader(svg);
          doc = f.createSVGDocument(null, reader);
        } catch (Exception ex) {}
        canvas.setSVGDocument(doc);
      }
    };
    addWindowListener(windowAdapter);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(200,200);
    setVisible(true);
  }

  public class Canvas extends JSVGCanvas implements GVTTreeRendererListener
{
    public Canvas() {
      canvas = this;
      setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
      addGVTTreeRendererListener(this);

      KeyAdapter keyAdapter = new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
          if (runQ==null) return;
          runQ.invokeLater(new Runnable(){public void run(){
            changeColor();
          }});
        }
      };
      addKeyListener(keyAdapter);
   }

    public void gvtRenderingPrepare(GVTTreeRendererEvent e) {}
    public void gvtRenderingStarted(GVTTreeRendererEvent e) {}
    public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
      document = canvas.getSVGDocument();
      runQ = canvas.getUpdateManager().getUpdateRunnableQueue();
    }
    public void gvtRenderingCancelled(GVTTreeRendererEvent e) {}
    public void gvtRenderingFailed	(GVTTreeRendererEvent e) {}
  }
  private void changeColor() {
    SVGSVGElement root = document.getRootElement();
    SVGOMStyleElement style =
(SVGOMStyleElement)getFirstChildByNodeName(root,"style");
    for (Node node=style.getFirstChild(); node!=null;
node=node.getNextSibling()) {
      if (node.getNodeType()==Node.CDATA_SECTION_NODE) {
        CDATASection cdata = (CDATASection)node;
        String value = cdata.getNodeValue();
        int indx = value.indexOf(".rect");
        int indx1 = value.indexOf("fill:", indx) + 5;
        int indx2 = value.indexOf('}', indx1);
        String currColor = value.substring(indx1, indx2);
        String newColor = currColor.equals("cyan") ? "red" : "cyan";
        cdata.replaceData(indx1, (indx2-indx1), newColor);
        return;
      }
    }
  }
  public static Element getFirstChildByNodeName(Element parent, String
nodeName) {
    for (Node node=parent.getFirstChild(); node!=null;
node=node.getNextSibling()) {
      if (node.getNodeType()==Node.ELEMENT_NODE &&
nodeName.equals(node.getNodeName()))
        return (Element)node;
    }
    return null;
  }
  public static void main( String[] args ) {
    SwingUtilities.invokeLater( new Runnable() { public void run() {
      new CSSTest();
    }});
}

}



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


Re: modifying css in live svg document

Posted by Roger Critchlow <re...@elf.org>.
How odd.

The modification is definitely running on the right thread.  It's
launched from an ActionEvent, but it gets tossed onto the right thread
before it modifies the style CDATA.

I'll work up a minimal example.

Thanks,

-- rec --

On 5/9/06, sugawara <su...@humane-systems.co.jp> wrote:
> Hi Roger,
>
> >
> > So I need to tweak the rendered elements so they pick up the new style?
> >
> I don't think so. At least, with my program, the result of the changed style
> can be seen immediately without any mouse operation.
> My program calls changeTextColor method which is that I told you before
> in a popup menu action listener like as follws:
>
>   RunnableQueue runQ = canvas.getUpdateManager().getUpdateRunnableQueue();
>   runQ.invokeLater(new Runnable(){public void run() {
>     changeTextColor(SVGDocument document, Color.red);
>   }});
>
> There is no magic other than above.
>
> > modifying it with the CDATA mutation operators in the DOM
> > updateThread.
> >
> > But I don't see any change in the rendered graphics until I trigger a
> > redraw with a mouseover.
> >
> I'm not sure why, but I can suspect that your modifying is not run under
> SVG canvas's UpdateManager thread.
> Is your 'DOM updateThread' right thread?
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>

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


RE: modifying css in live svg document

Posted by sugawara <su...@humane-systems.co.jp>.
Hi Roger,

>
> So I need to tweak the rendered elements so they pick up the new style?
>
I don't think so. At least, with my program, the result of the changed style
can be seen immediately without any mouse operation.
My program calls changeTextColor method which is that I told you before
in a popup menu action listener like as follws:

  RunnableQueue runQ = canvas.getUpdateManager().getUpdateRunnableQueue();
  runQ.invokeLater(new Runnable(){public void run() {
    changeTextColor(SVGDocument document, Color.red);
  }});

There is no magic other than above.

> modifying it with the CDATA mutation operators in the DOM
> updateThread.
>
> But I don't see any change in the rendered graphics until I trigger a
> redraw with a mouseover.
>
I'm not sure why, but I can suspect that your modifying is not run under
SVG canvas's UpdateManager thread.
Is your 'DOM updateThread' right thread?



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