]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / svg / SVGUtils.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGUtils.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/svg/SVGUtils.java
new file mode 100644 (file)
index 0000000..2da6024
--- /dev/null
@@ -0,0 +1,151 @@
+/*******************************************************************************\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