xmlgraphics-batik-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zb <zweibie...@physpics.com>
Subject Re: "and then modify it." color via css
Date Wed, 05 Mar 2008 16:06:44 GMT

After more searching, I finally found a message that suggested a path: 
   
http://www.mail-archive.com/batik-users@xmlgraphics.apache.org/msg03376.html
as amended by its reply:
   
http://www.mail-archive.com/batik-users@xmlgraphics.apache.org/msg03379.html

The folowing outputs two maps with diffeent colors for the United States:


    public void testTranscode()  {
        try {
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            String filename = "C:/maps/BlankMap-World6.svg";
            String uri = "file:///" + filename;
            Document doc = f.createDocument(uri, new FileReader(filename));
            SVGDocument sdoc = (SVGDocument)doc;
            Element svgRoot = sdoc.getDocumentElement();
            PNGTranscoder t = new PNGTranscoder();
            TranscoderInput input = new TranscoderInput(doc);

            // find the existing stylesheet in the map
            NodeList stylesList = doc.getElementsByTagName("style");  
            Node styleNode = stylesList.item(0);

            // append another stylesheet after the existing one
            SVGStyleElement style = (SVGStyleElement)
                                doc.createElementNS(SVG_NAMESPACE_URI,
"style");
            style.setAttributeNS(null,"type","text/css");
            style.appendChild(doc.createCDATASection(".us {fill: blue;}"));
            styleNode.getParentNode().appendChild(style);
            
            // transcode the map
            OutputStream ostream = new FileOutputStream("outblue.jpg");
            TranscoderOutput output = new TranscoderOutput(ostream);
            t.transcode(input, output);
            ostream.close();

            // replace the appended stylesheet with another
            SVGStyleElement oldStyle = style;
            style = (SVGStyleElement)
                                doc.createElementNS(SVG_NAMESPACE_URI,
"style");
            style.setAttributeNS(null,"type","text/css");
            style.appendChild(doc.createCDATASection(".us {fill: green;}"));
            styleNode.getParentNode().replaceChild(style, oldStyle);
            
            // transcode the revised map
            ostream = new FileOutputStream("outgreen.jpg");
            output = new TranscoderOutput(ostream);
            t.transcode(input, output);
            ostream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }



zb wrote:
> 
> In message
>    http://www.nabble.com/Batik---PNG-Transcoder-to10713115.html#a10713183
> the author remarks that I can recolor SVG elements by loading the document
> with SAXSVGDocumentFactory.createDocument and "then modify it."
> 
> That post gives several valuable tips on how to color things with CSS, but
> nothing useful about modifying a style sheet that has been loaded with a
> document. Nor can I find any examples on the web. Where should I look?
> 
> To be more specific, I have downloaded an SVG map from wikipedia. I plan
> to load it into a Java application and transcode it to a png several
> times, each with a different coloring. As a learning experience, I'd like
> to use method calls to modify the stylesheet. But how?
> 
> Thanks,
> Fred Hansen
> 

-- 
View this message in context: http://www.nabble.com/%22and-then-modify-it.%22-color-via-css-tp15841042p15853807.html
Sent from the Batik - Dev mailing list archive at Nabble.com.


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


Mime
View raw message