]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / svg / SVGUtils.java
index 2da60240e79c6beaf0d03d1df6989742d84af627..f7d7916cacb1647215eb3130044bf182ae389a02 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.svg;\r
-\r
-import java.util.Locale;\r
-\r
-//import org.apache.batik.bridge.BridgeContext;\r
-//import org.apache.batik.bridge.GVTBuilder;\r
-//import org.apache.batik.bridge.UserAgentAdapter;\r
-//import org.apache.batik.dom.svg.SAXSVGDocumentFactory;\r
-//import org.apache.batik.dom.svg12.SVG12DOMImplementation;\r
-//import org.apache.batik.dom.util.DOMUtilities;\r
-//import org.apache.batik.gvt.GraphicsNode;\r
-//import org.apache.batik.util.XMLResourceDescriptor;\r
-//import org.w3c.dom.svg.SVGDocument;\r
-//import org.w3c.dom.svg.SVGSVGElement;\r
-\r
-\r
-/**\r
- * Utility methods for DOM and SVG document handling.\r
- * \r
- * Currently disabled.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-final class SVGUtils {\r
-\r
-    public static final Locale        formatLocale = Locale.US;\r
-\r
-//    private static final NumberFormat nf           = NumberFormat.getNumberInstance(formatLocale);\r
-\r
-    /**\r
-     * Attempts to parse an SVG document form the specified input stream.\r
-     * \r
-     * @param inp\r
-     * @return\r
-     * @throws IOException\r
-     */\r
-//    public static SVGDocument parseSVGStream(InputStream inp) throws IOException {\r
-//        ClassLoader ldr = Thread.currentThread().getContextClassLoader();\r
-//        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());\r
-//        try {\r
-//            String parser = XMLResourceDescriptor.getXMLParserClassName();\r
-//            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);\r
-//            SVGDocument doc = f.createSVGDocument(null, inp);\r
-//            return doc;\r
-//        } finally {\r
-//            Thread.currentThread().setContextClassLoader(ldr);\r
-//        }\r
-//    }\r
-\r
-    /**\r
-     * Attempts to parse a String as svg/xml and return the resulting\r
-     * SVGDocument.\r
-     * \r
-     * @param svgData the svg/xml data\r
-     * @return the resulting SVGDocument instance\r
-     * @throws IOException when parsing as svg fails\r
-     */\r
-//    public static SVGDocument parseSVGString(String svgData) throws IOException {\r
-//        ClassLoader ldr = Thread.currentThread().getContextClassLoader();\r
-//        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());\r
-//        try {\r
-//            String parser = XMLResourceDescriptor.getXMLParserClassName();\r
-//            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);\r
-//            StringReader reader = new StringReader(svgData);\r
-//            SVGDocument doc = f.createSVGDocument(null, reader);\r
-//            return doc;\r
-//        } finally {\r
-//            Thread.currentThread().setContextClassLoader(ldr);\r
-//        }\r
-//    }\r
-    \r
-    /**\r
-     * Attempts to parse a String as svg/xml and return the root SVGSVGElement\r
-     * of the resulting SVGDocument.\r
-     * \r
-     * @param svgData the svg/xml data\r
-     * @return the resulting SVGDocument instance\r
-     * @throws IOException when parsing as svg fails\r
-     */\r
-//    public static SVGSVGElement parseSVGStringToRoot(String svgData) throws IOException {\r
-//        SVGDocument doc = parseSVGString(svgData);\r
-//        return doc.getRootElement();\r
-//    }\r
-    \r
-    /**\r
-     * Transcodes an SVGDocument instance into String form.\r
-     * \r
-     * @param doc the SVGDocument to transcode\r
-     * @return the resulting string\r
-     * @throws IOException if the document contains invalid data\r
-     */\r
-//    public static String svgToString(SVGDocument doc) throws IOException {\r
-//        StringWriter writer = new StringWriter();\r
-//        DOMUtilities.writeDocument(doc, writer);\r
-//        writer.close();\r
-//        return writer.toString();\r
-//    }\r
-    \r
-    /**\r
-     * Retrieves SVG document bounding box by constructing the corresponding GVT\r
-     * tree and getting the bounding information from there.\r
-     * \r
-     * <p>\r
-     * This allows us to get the bounding box of the actual rendered output, not\r
-     * just the bounding box of the primitives.\r
-     * </p>\r
-     * \r
-     * <p>\r
-     * BEWARE: using the specified document with a JSVGCanvas after invoking\r
-     * this method seems to break the functionality of the DOM/GVT binding in\r
-     * the canvas. If you need to do that, \r
-     * \r
-     * @param doc\r
-     * @param defaultValue the rectangle to return if the document has no\r
-     *        bounding box\r
-     * @return <code>null</code> if the document has no graphical elements,\r
-     *         i.e. no real bounding box and the given defaultValue was\r
-     *         <code>null</code>\r
-     */\r
-//    public static Rectangle2D getDocumentBoundingBox(SVGDocument doc, Rectangle2D defaultValue, boolean cloneDocument) {\r
-//        if (cloneDocument)\r
-//            doc = (SVGDocument) DOMUtilities.deepCloneDocument(doc, SVG12DOMImplementation.getDOMImplementation());\r
-//\r
-//        GVTBuilder builder = new GVTBuilder();\r
-//        BridgeContext ctx = new BridgeContext(new UserAgentAdapter());\r
-//        GraphicsNode gvtRoot = builder.build(ctx, doc);\r
-//\r
-//        Rectangle2D bounds = gvtRoot.getSensitiveBounds();\r
-//        if (bounds == null)\r
-//            return defaultValue;\r
-//        \r
-//        if (bounds.isEmpty())\r
-//            return defaultValue;\r
-//        \r
-//        Rectangle2D bbox = new Rectangle2D.Float();\r
-//        bbox.setFrame(bounds);\r
-//        return bbox;\r
-//    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.svg;
+
+import java.util.Locale;
+
+//import org.apache.batik.bridge.BridgeContext;
+//import org.apache.batik.bridge.GVTBuilder;
+//import org.apache.batik.bridge.UserAgentAdapter;
+//import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
+//import org.apache.batik.dom.svg12.SVG12DOMImplementation;
+//import org.apache.batik.dom.util.DOMUtilities;
+//import org.apache.batik.gvt.GraphicsNode;
+//import org.apache.batik.util.XMLResourceDescriptor;
+//import org.w3c.dom.svg.SVGDocument;
+//import org.w3c.dom.svg.SVGSVGElement;
+
+
+/**
+ * Utility methods for DOM and SVG document handling.
+ * 
+ * Currently disabled.
+ * 
+ * @author Tuukka Lehtonen
+ */
+final class SVGUtils {
+
+    public static final Locale        formatLocale = Locale.US;
+
+//    private static final NumberFormat nf           = NumberFormat.getNumberInstance(formatLocale);
+
+    /**
+     * Attempts to parse an SVG document form the specified input stream.
+     * 
+     * @param inp
+     * @return
+     * @throws IOException
+     */
+//    public static SVGDocument parseSVGStream(InputStream inp) throws IOException {
+//        ClassLoader ldr = Thread.currentThread().getContextClassLoader();
+//        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());
+//        try {
+//            String parser = XMLResourceDescriptor.getXMLParserClassName();
+//            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
+//            SVGDocument doc = f.createSVGDocument(null, inp);
+//            return doc;
+//        } finally {
+//            Thread.currentThread().setContextClassLoader(ldr);
+//        }
+//    }
+
+    /**
+     * Attempts to parse a String as svg/xml and return the resulting
+     * SVGDocument.
+     * 
+     * @param svgData the svg/xml data
+     * @return the resulting SVGDocument instance
+     * @throws IOException when parsing as svg fails
+     */
+//    public static SVGDocument parseSVGString(String svgData) throws IOException {
+//        ClassLoader ldr = Thread.currentThread().getContextClassLoader();
+//        Thread.currentThread().setContextClassLoader(SVGUtils.class.getClassLoader());
+//        try {
+//            String parser = XMLResourceDescriptor.getXMLParserClassName();
+//            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
+//            StringReader reader = new StringReader(svgData);
+//            SVGDocument doc = f.createSVGDocument(null, reader);
+//            return doc;
+//        } finally {
+//            Thread.currentThread().setContextClassLoader(ldr);
+//        }
+//    }
+    
+    /**
+     * Attempts to parse a String as svg/xml and return the root SVGSVGElement
+     * of the resulting SVGDocument.
+     * 
+     * @param svgData the svg/xml data
+     * @return the resulting SVGDocument instance
+     * @throws IOException when parsing as svg fails
+     */
+//    public static SVGSVGElement parseSVGStringToRoot(String svgData) throws IOException {
+//        SVGDocument doc = parseSVGString(svgData);
+//        return doc.getRootElement();
+//    }
+    
+    /**
+     * Transcodes an SVGDocument instance into String form.
+     * 
+     * @param doc the SVGDocument to transcode
+     * @return the resulting string
+     * @throws IOException if the document contains invalid data
+     */
+//    public static String svgToString(SVGDocument doc) throws IOException {
+//        StringWriter writer = new StringWriter();
+//        DOMUtilities.writeDocument(doc, writer);
+//        writer.close();
+//        return writer.toString();
+//    }
+    
+    /**
+     * Retrieves SVG document bounding box by constructing the corresponding GVT
+     * tree and getting the bounding information from there.
+     * 
+     * <p>
+     * This allows us to get the bounding box of the actual rendered output, not
+     * just the bounding box of the primitives.
+     * </p>
+     * 
+     * <p>
+     * BEWARE: using the specified document with a JSVGCanvas after invoking
+     * this method seems to break the functionality of the DOM/GVT binding in
+     * the canvas. If you need to do that, 
+     * 
+     * @param doc
+     * @param defaultValue the rectangle to return if the document has no
+     *        bounding box
+     * @return <code>null</code> if the document has no graphical elements,
+     *         i.e. no real bounding box and the given defaultValue was
+     *         <code>null</code>
+     */
+//    public static Rectangle2D getDocumentBoundingBox(SVGDocument doc, Rectangle2D defaultValue, boolean cloneDocument) {
+//        if (cloneDocument)
+//            doc = (SVGDocument) DOMUtilities.deepCloneDocument(doc, SVG12DOMImplementation.getDOMImplementation());
+//
+//        GVTBuilder builder = new GVTBuilder();
+//        BridgeContext ctx = new BridgeContext(new UserAgentAdapter());
+//        GraphicsNode gvtRoot = builder.build(ctx, doc);
+//
+//        Rectangle2D bounds = gvtRoot.getSensitiveBounds();
+//        if (bounds == null)
+//            return defaultValue;
+//        
+//        if (bounds.isEmpty())
+//            return defaultValue;
+//        
+//        Rectangle2D bbox = new Rectangle2D.Float();
+//        bbox.setFrame(bounds);
+//        return bbox;
+//    }
+
+}