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/04/09 09:10:29 UTC
modifying css in live svg document
Hi --
Anyone have some examples of modifying the css stylesheet for a live
svg document?
I want to edit the styles for classes of svg elements, see the results
in the JSVGCanvas, and then get the modified style sheet as a string
to save to a file.
I've found out how to get the original style sheet and convert it to a
string. I can see how to hack the attributes of the SVG document and
get the modified SVG document as a string. But it isn't obvious how
to hack the style sheet and get the modified CSS document as a string.
-- 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 --
This is helpful.
I was keeping the stylesheet in an external file included with an
<?xml-stylesheet href=... /> processing instruction. So I was able
to get the StyleSheet interface object with:
/*
* Get the stylesheet for our document.
* This fetches an external stylesheet.
*/
protected StyleSheet getStyleSheet(SVGDocument doc) {
NodeList list = doc.getChildNodes();
for (int i = 0; i < list.getLength(); i += 1) {
if (list.item(i) instanceof SVGStyleSheetProcessingInstruction)
return ((SVGStyleSheetProcessingInstruction)list.item(i)).getCSSStyleSheet();
}
return null;
}
I was then serializing the style sheet with:
/*
* Write the stylesheet to out.
*/
protected void writeCSS(PrintStream out) {
CSSEngine eng = ((SVGOMDocument)svgDoc).getCSSEngine();
StyleSheet css = getStyleSheet(svgDoc);
if (css != null && eng != null)
out.print(css.toString(eng));
}
Using the <style></style> element, as you suggest, I find that the
style sheet is serialized with the svg when I use:
/*
* Write the svg document to out.
*/
protected void writeSVG(PrintStream out) {
StringWriter sw = new StringWriter();
try {
DOMUtilities.writeDocument(svgDoc, sw);
} catch (IOException e) {
throw new Error("IO");
}
out.print(sw.toString());
}
I guess I was hoping for some clues to using the CSS DOM to get at and
modify the parsed rules for the style sheet. I'll keep looking to see
if I can make that work.
Thank you,
-- 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;
> }
>
> Also, to get the modified CSS document as a string;
>
> public static String serialize( SVGDocument doc ) {
> String svg = null;
> try {
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> OutputFormat format = new OutputFormat(doc, "UTF-8", true);
> format.setLineWidth(256);
> DOMSerializer serializer = new XMLSerializer(baos,
> format).asDOMSerializer();
> serializer.serialize(doc);
> svg = baos.toString("UTF-8");
> } catch (Exception e) { e.printStackTrace(); }
> return svg;
> }
>
> Hope this will help.
>
> Shin
>
> > -----Original Message-----
> > From: rogercritchlow@gmail.com
> > [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> > Sent: Sunday, April 09, 2006 4:10 PM
> > To: batik-users@xmlgraphics.apache.org
> > Subject: modifying css in live svg document
> >
> >
> > Hi --
> >
> > Anyone have some examples of modifying the css stylesheet for a live
> > svg document?
> >
> > I want to edit the styles for classes of svg elements, see the results
> > in the JSVGCanvas, and then get the modified style sheet as a string
> > to save to a file.
> >
> > I've found out how to get the original style sheet and convert it to a
> > string. I can see how to hack the attributes of the SVG document and
> > get the modified SVG document as a string. But it isn't obvious how
> > to hack the style sheet and get the modified CSS document as a string.
> >
> > -- 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 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
Re: modifying css in live svg document
Posted by Roger Critchlow <re...@elf.org>.
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 sugawara <su...@humane-systems.co.jp>.
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;
}
Also, to get the modified CSS document as a string;
public static String serialize( SVGDocument doc ) {
String svg = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat(doc, "UTF-8", true);
format.setLineWidth(256);
DOMSerializer serializer = new XMLSerializer(baos,
format).asDOMSerializer();
serializer.serialize(doc);
svg = baos.toString("UTF-8");
} catch (Exception e) { e.printStackTrace(); }
return svg;
}
Hope this will help.
Shin
> -----Original Message-----
> From: rogercritchlow@gmail.com
> [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> Sent: Sunday, April 09, 2006 4:10 PM
> To: batik-users@xmlgraphics.apache.org
> Subject: modifying css in live svg document
>
>
> Hi --
>
> Anyone have some examples of modifying the css stylesheet for a live
> svg document?
>
> I want to edit the styles for classes of svg elements, see the results
> in the JSVGCanvas, and then get the modified style sheet as a string
> to save to a file.
>
> I've found out how to get the original style sheet and convert it to a
> string. I can see how to hack the attributes of the SVG document and
> get the modified SVG document as a string. But it isn't obvious how
> to hack the style sheet and get the modified CSS document as a string.
>
> -- 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