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