Search in sources :

Example 1 with GenericText

use of org.apache.batik.dom.GenericText in project yamcs-studio by yamcs.

the class SimpleImageTranscoder method changeColor.

private void changeColor(Document doc, Color oldColor, Color newColor) {
    if (oldColor.equals(newColor))
        return;
    Matcher matcher = null;
    String svgOldColor = toHexString(oldColor.getRed(), oldColor.getGreen(), oldColor.getBlue());
    String svgNewColor = toHexString(newColor.getRed(), newColor.getGreen(), newColor.getBlue());
    Pattern fillPattern = Pattern.compile("(?i)" + CSSConstants.CSS_FILL_PROPERTY + ":" + svgOldColor);
    Pattern strokePattern = Pattern.compile("(?i)" + CSSConstants.CSS_STROKE_PROPERTY + ":" + svgOldColor);
    String fillReplace = CSSConstants.CSS_FILL_PROPERTY + ":" + svgNewColor;
    String strokeReplace = CSSConstants.CSS_STROKE_PROPERTY + ":" + svgNewColor;
    // Search for global style element <style type="text/css"></style>
    NodeList styleList = doc.getElementsByTagName("style");
    for (int i = 0; i < styleList.getLength(); i++) {
        Element style = (Element) styleList.item(i);
        NodeList childList = style.getChildNodes();
        if (childList != null) {
            for (int j = 0; j < childList.getLength(); j++) {
                Node child = childList.item(j);
                if (child instanceof GenericText || child instanceof GenericCDATASection) {
                    CharacterData cdata = (CharacterData) child;
                    String data = cdata.getData();
                    matcher = fillPattern.matcher(data);
                    data = matcher.replaceAll(fillReplace);
                    matcher = strokePattern.matcher(data);
                    data = matcher.replaceAll(strokeReplace);
                    data = replaceRGB(oldColor, newColor, data);
                    cdata.setData(data);
                }
            }
        }
    }
    recursiveCC(doc.getDocumentElement(), oldColor, newColor, fillPattern, strokePattern, fillReplace, strokeReplace);
}
Also used : Pattern(java.util.regex.Pattern) GenericCDATASection(org.apache.batik.dom.GenericCDATASection) CharacterData(org.w3c.dom.CharacterData) Matcher(java.util.regex.Matcher) NodeList(org.w3c.dom.NodeList) SVGSVGElement(org.w3c.dom.svg.SVGSVGElement) SVGStylableElement(org.apache.batik.anim.dom.SVGStylableElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) GenericText(org.apache.batik.dom.GenericText)

Aggregations

Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 SVGStylableElement (org.apache.batik.anim.dom.SVGStylableElement)1 GenericCDATASection (org.apache.batik.dom.GenericCDATASection)1 GenericText (org.apache.batik.dom.GenericText)1 CharacterData (org.w3c.dom.CharacterData)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1 SVGSVGElement (org.w3c.dom.svg.SVGSVGElement)1