1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.svg;
\r
14 import java.util.Locale;
\r
16 //import org.apache.batik.bridge.BridgeContext;
\r
17 //import org.apache.batik.bridge.GVTBuilder;
\r
18 //import org.apache.batik.bridge.UserAgentAdapter;
\r
19 //import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
\r
20 //import org.apache.batik.dom.svg12.SVG12DOMImplementation;
\r
21 //import org.apache.batik.dom.util.DOMUtilities;
\r
22 //import org.apache.batik.gvt.GraphicsNode;
\r
23 //import org.apache.batik.util.XMLResourceDescriptor;
\r
24 //import org.w3c.dom.svg.SVGDocument;
\r
25 //import org.w3c.dom.svg.SVGSVGElement;
\r
29 * Utility methods for DOM and SVG document handling.
\r
31 * Currently disabled.
\r
33 * @author Tuukka Lehtonen
\r
35 final class SVGUtils {
\r
37 public static final Locale formatLocale = Locale.US;
\r
39 // private static final NumberFormat nf = NumberFormat.getNumberInstance(formatLocale);
\r
42 * Attempts to parse an SVG document form the specified input stream.
\r
46 * @throws IOException
\r
48 // public static SVGDocument parseSVGStream(InputStream inp) throws IOException {
\r
49 // ClassLoader ldr = Thread.currentThread().getContextClassLoader();
\r
50 // Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());
\r
52 // String parser = XMLResourceDescriptor.getXMLParserClassName();
\r
53 // SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
\r
54 // SVGDocument doc = f.createSVGDocument(null, inp);
\r
57 // Thread.currentThread().setContextClassLoader(ldr);
\r
62 * Attempts to parse a String as svg/xml and return the resulting
\r
65 * @param svgData the svg/xml data
\r
66 * @return the resulting SVGDocument instance
\r
67 * @throws IOException when parsing as svg fails
\r
69 // public static SVGDocument parseSVGString(String svgData) throws IOException {
\r
70 // ClassLoader ldr = Thread.currentThread().getContextClassLoader();
\r
71 // Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());
\r
73 // String parser = XMLResourceDescriptor.getXMLParserClassName();
\r
74 // SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
\r
75 // StringReader reader = new StringReader(svgData);
\r
76 // SVGDocument doc = f.createSVGDocument(null, reader);
\r
79 // Thread.currentThread().setContextClassLoader(ldr);
\r
84 * Attempts to parse a String as svg/xml and return the root SVGSVGElement
\r
85 * of the resulting SVGDocument.
\r
87 * @param svgData the svg/xml data
\r
88 * @return the resulting SVGDocument instance
\r
89 * @throws IOException when parsing as svg fails
\r
91 // public static SVGSVGElement parseSVGStringToRoot(String svgData) throws IOException {
\r
92 // SVGDocument doc = parseSVGString(svgData);
\r
93 // return doc.getRootElement();
\r
97 * Transcodes an SVGDocument instance into String form.
\r
99 * @param doc the SVGDocument to transcode
\r
100 * @return the resulting string
\r
101 * @throws IOException if the document contains invalid data
\r
103 // public static String svgToString(SVGDocument doc) throws IOException {
\r
104 // StringWriter writer = new StringWriter();
\r
105 // DOMUtilities.writeDocument(doc, writer);
\r
107 // return writer.toString();
\r
111 * Retrieves SVG document bounding box by constructing the corresponding GVT
\r
112 * tree and getting the bounding information from there.
\r
115 * This allows us to get the bounding box of the actual rendered output, not
\r
116 * just the bounding box of the primitives.
\r
120 * BEWARE: using the specified document with a JSVGCanvas after invoking
\r
121 * this method seems to break the functionality of the DOM/GVT binding in
\r
122 * the canvas. If you need to do that,
\r
125 * @param defaultValue the rectangle to return if the document has no
\r
127 * @return <code>null</code> if the document has no graphical elements,
\r
128 * i.e. no real bounding box and the given defaultValue was
\r
129 * <code>null</code>
\r
131 // public static Rectangle2D getDocumentBoundingBox(SVGDocument doc, Rectangle2D defaultValue, boolean cloneDocument) {
\r
132 // if (cloneDocument)
\r
133 // doc = (SVGDocument) DOMUtilities.deepCloneDocument(doc, SVG12DOMImplementation.getDOMImplementation());
\r
135 // GVTBuilder builder = new GVTBuilder();
\r
136 // BridgeContext ctx = new BridgeContext(new UserAgentAdapter());
\r
137 // GraphicsNode gvtRoot = builder.build(ctx, doc);
\r
139 // Rectangle2D bounds = gvtRoot.getSensitiveBounds();
\r
140 // if (bounds == null)
\r
141 // return defaultValue;
\r
143 // if (bounds.isEmpty())
\r
144 // return defaultValue;
\r
146 // Rectangle2D bbox = new Rectangle2D.Float();
\r
147 // bbox.setFrame(bounds);
\r