Reputation: 73
I have been trying to process and rasterize an svg file using SVG Salamander & Java during the last couple of days and just can't get it to work.
Here is my SVG file, which is generated by Adobe Illustrator. It basically contains some random text, with one of the tspans being styled to show yellow text.
With my program I aim to change some of the tspan text colors and then rasterize the svg as a .png. One first method traverses the SVG Document Tree and adds unique ids to all the tags. A second method randomly picks a set of those ids, selects the according tspan and sets the fill attribute (up until here everything seems to work fine, some debugging showed me that I get the correct tspan and the fill attribute is correctly set) and then rasterizes the svg. Here is where the problem starts: The resulting .pngs don't show the altered font colors (Text colors are just plain black, even the tspan which is colored yellow in the example xml is somehow "changed" to black. Some testing showed me, it doesn't even show altered text content of a tspan. Here is the code that does the job of setting attributes and rendering:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="595.28px" height="841.89px" viewBox="0 0 595.28 841.89" enable-background="new 0 0 595.28 841.89" xml:space="preserve">
<text transform="matrix(1 0 0 1 117 180)"><tspan x="0" y="0" font-family="'MyriadPro-Regular'" font-size="12">asdfasdfasdfasdfasdfasdfasdf</tspan><tspan x="0" y="14.4" font-family="'MyriadPro-Regular'" font-size="12">asdfiausldöfjaöljfdölajsdf</tspan><tspan x="0" y="28.8" font-family="'MyriadPro-Regular'" font-size="12">adsfal+sdfajksdflkasdf</tspan><tspan x="0" y="57.6" fill="#FFDE17" font-family="'MyriadPro-Regular'" font-size="12">asdlfköajsdöfjkaölsdfj</tspan></text>
</svg>
SVGElement element = tempDiagram.getElement(String.valueOf(curId));
if (!element.hasAttribute("fill", AnimationElement.AT_CSS)) {
element.addAttribute("fill", AnimationElement.AT_CSS, "#ff0000");
} else {
element.setAttribute("fill", AnimationElement.AT_CSS, "#ff0000");
}
tempDiagram.updateTime(0f);
BufferedImage bi = new BufferedImage(2480,3508,BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
ig2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
tempDiagram.render(ig2);
ImageIO.write(bi, "png", new File("test" + String.valueOf(i) + ".png"));
Do you see anything that could explain my problem so far? Any help is appreciated!
P.s: If I place a red rectangle in the SVG using Illustrator, the rectangle is also red in the resulting .png. So somehow the problem only seems to affect tspans or tags that I "touch" in my program...
Upvotes: 1
Views: 1962