Search in sources :

Example 1 with GenericCDATASection

use of org.apache.batik.dom.GenericCDATASection 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;
    var svgOldColor = toHexString(oldColor.getRed(), oldColor.getGreen(), oldColor.getBlue());
    var svgNewColor = toHexString(newColor.getRed(), newColor.getGreen(), newColor.getBlue());
    var fillPattern = Pattern.compile("(?i)" + CSSConstants.CSS_FILL_PROPERTY + ":" + svgOldColor);
    var strokePattern = Pattern.compile("(?i)" + CSSConstants.CSS_STROKE_PROPERTY + ":" + svgOldColor);
    var fillReplace = CSSConstants.CSS_FILL_PROPERTY + ":" + svgNewColor;
    var strokeReplace = CSSConstants.CSS_STROKE_PROPERTY + ":" + svgNewColor;
    // Search for global style element <style type="text/css"></style>
    var styleList = doc.getElementsByTagName("style");
    for (var i = 0; i < styleList.getLength(); i++) {
        var style = (Element) styleList.item(i);
        var childList = style.getChildNodes();
        if (childList != null) {
            for (var j = 0; j < childList.getLength(); j++) {
                var child = childList.item(j);
                if (child instanceof GenericText || child instanceof GenericCDATASection) {
                    var cdata = (CharacterData) child;
                    var 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 : GenericCDATASection(org.apache.batik.dom.GenericCDATASection) CharacterData(org.w3c.dom.CharacterData) Matcher(java.util.regex.Matcher) SVGStylableElement(org.apache.batik.anim.dom.SVGStylableElement) Element(org.w3c.dom.Element) GenericText(org.apache.batik.dom.GenericText)

Aggregations

Matcher (java.util.regex.Matcher)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