xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tiwaris <tiwa...@gmail.com>
Subject Re: SVGGraphics2D cannot reproduce gradients (Bug 42387)
Date Mon, 11 Oct 2010 16:11:59 GMT

Hello,

Thanks for your response.

I downloaded the 1.8pre build (dated October 6th, 2010).

http://people.apache.org/builds/xml-batik/batik-src-10-10-06.zip

I created a test case to demonstrate my problem.

Here is the source code.


public class TestSvg {


	public static org.apache.batik.gvt.GraphicsNode getSvgIcon(java.net.URL
url) {
		org.apache.batik.gvt.GraphicsNode svgIcon = null;
		try {
			String xmlParser =
org.apache.batik.util.XMLResourceDescriptor.getXMLParserClassName();
			org.apache.batik.dom.svg.SAXSVGDocumentFactory df = new
org.apache.batik.dom.svg.SAXSVGDocumentFactory(
				xmlParser);
			org.w3c.dom.svg.SVGDocument doc = df.createSVGDocument(url.toString());
			org.apache.batik.bridge.UserAgent userAgent = new
org.apache.batik.bridge.UserAgentAdapter();
			org.apache.batik.bridge.DocumentLoader loader = new
org.apache.batik.bridge.DocumentLoader(userAgent);
			org.apache.batik.bridge.BridgeContext ctx = new
org.apache.batik.bridge.BridgeContext(userAgent, loader);
			ctx.setDynamicState(org.apache.batik.bridge.BridgeContext.DYNAMIC);
			org.apache.batik.bridge.GVTBuilder builder = new
org.apache.batik.bridge.GVTBuilder();
			svgIcon = builder.build(ctx, doc);
		} catch (Exception excp) {
			svgIcon = null;
			excp.printStackTrace();
		}
		return svgIcon;
	}


	public static void paintSvgIcon(java.awt.Graphics2D g,
org.apache.batik.gvt.GraphicsNode svgIcon, int x, int y,
		double scaleX, double scaleY) {
		java.awt.geom.AffineTransform transform = new
java.awt.geom.AffineTransform(scaleX, 0.0, 0.0, scaleY, x, y);
		svgIcon.setTransform(transform);
		svgIcon.paint(g);
	}


	public static org.apache.batik.svggen.SVGGraphics2D getSvgPainter() {
		org.w3c.dom.DOMImplementation impl =
org.apache.batik.dom.GenericDOMImplementation.getDOMImplementation();
		String svgNS = "http://www.w3.org/2000/svg";
		org.w3c.dom.Document document = impl.createDocument(svgNS, "svg", null);
		org.apache.batik.svggen.SVGGeneratorContext ctx =
org.apache.batik.svggen.SVGGeneratorContext.createDefault(
			document);
		ctx.setComment("SVG Test");
		ctx.setEmbeddedFontsOn(true);

		org.apache.batik.svggen.SVGGraphics2D g2D = new
org.apache.batik.svggen.SVGGraphics2D(ctx, true);
		g2D.addRenderingHints(new
java.awt.RenderingHints(java.awt.RenderingHints.KEY_ANTIALIASING,
			java.awt.RenderingHints.VALUE_ANTIALIAS_ON));
		g2D.setRenderingHint(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING,
			java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		return g2D;
	}


	public static String
getSvgFileAsString(org.apache.batik.svggen.SVGGraphics2D svgPainter) {
		boolean useCSS = true;
		java.io.StringWriter writer = new java.io.StringWriter(100000);
		try {
			svgPainter.stream(writer, useCSS);
		} catch (org.apache.batik.svggen.SVGGraphics2DIOException excp) {
			System.err.println("Could not write SVG");
			System.err.println(excp);
			return null;
		}
		return writer.toString();
	}


	public static void main(String[] args) {
		java.net.URL url = TestSvg.class.getResource("Process-stop.svg");
		final org.apache.batik.gvt.GraphicsNode icon = getSvgIcon(url);

		//Show the graphics on screen
		javax.swing.JFrame frame = new javax.swing.JFrame("SVG");
		frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
		javax.swing.JPanel panel = new javax.swing.JPanel() {


			public void paintComponent(java.awt.Graphics g) {
				java.awt.Graphics2D g2D = (java.awt.Graphics2D)g;
				paintSvgIcon(g2D, icon, 50, 50, 1.0, 1.0);
			}


		};
		frame.getContentPane().add(panel);
		frame.setSize(150,150);
		frame.setVisible(true);

		//Save the graphics to a file
		org.apache.batik.svggen.SVGGraphics2D painter = getSvgPainter();
		painter.setSVGCanvasSize(new java.awt.Dimension(frame.getWidth(),
frame.getHeight()));
		frame.paint(painter);
		String fileContents = getSvgFileAsString(painter);
		try {
			java.io.BufferedWriter writer = new java.io.BufferedWriter(new
java.io.FileWriter("/home/stiwari/snap.svg"));
			writer.write(fileContents);
			writer.flush();
			writer.close();
		} catch (java.io.IOException excp) {
			excp.printStackTrace();
		}


	}


}


Here is a sample icon I am using to test the code.

http://upload.wikimedia.org/wikipedia/commons/0/04/Process-stop.svg

Save the icon at the same location as the source file.

On the screen, the icon is displayed perfectly fine, but when written to a
file, the gradients are colored black.



-- 
View this message in context: http://batik.2283329.n4.nabble.com/SVGGraphics2D-cannot-reproduce-gradients-Bug-42387-tp2979486p2990078.html
Sent from the Batik - Users mailing list archive at Nabble.com.

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


Mime
View raw message