--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * 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.diagram.symbollibrary;\r
+\r
+import java.awt.Color;\r
+import java.awt.Graphics2D;\r
+import java.awt.Point;\r
+import java.awt.RenderingHints;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+import java.awt.image.BufferedImage;\r
+\r
+import org.eclipse.swt.graphics.ImageData;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.query.DiagramRequests;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.stubs.G2DResource;\r
+import org.simantics.g2d.diagram.DiagramClass;\r
+import org.simantics.g2d.diagram.DiagramHints;\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.impl.Diagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.handler.StaticSymbol;\r
+import org.simantics.g2d.image.DefaultImages;\r
+import org.simantics.g2d.image.Image;\r
+import org.simantics.scenegraph.g2d.G2DSceneGraph;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.utils.ui.gfx.ImageUtils;\r
+\r
+/**\r
+ * @author Hannu Niemistö\r
+ */\r
+public class IconRenderingUtil {\r
+\r
+ public static BufferedImage renderBufferedImage(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {\r
+ // Ensure these resources are available as services.\r
+ G2DResource.getInstance(graph);\r
+ StructuralResource2.getInstance(graph);\r
+ DiagramResource.getInstance(graph);\r
+\r
+ IDiagram diagram = Diagram.spawnNew(DiagramClass.DEFAULT);\r
+\r
+ diagram.setHint(DiagramHints.KEY_ELEMENT_RASTER_TARGET_SIZE, new Point(size,size));\r
+ ElementClass ec = \r
+ graph.syncRequest( DiagramRequests.getElementClass(symbolResource, diagram) );\r
+ StaticSymbol ss = ec.getSingleItem(StaticSymbol.class);\r
+ Image source = ss == null ? DefaultImages.UNKNOWN2.get() : ss.getImage();\r
+ G2DSceneGraph sceneGraph = new G2DSceneGraph();\r
+ source.init(sceneGraph);\r
+\r
+ BufferedImage targetImage = new BufferedImage(size, size, \r
+ java.awt.image.BufferedImage.TYPE_INT_ARGB);\r
+ Graphics2D g2d = (Graphics2D)targetImage.getGraphics();\r
+ g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);\r
+ g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);\r
+ g2d.setBackground(Color.WHITE);\r
+ g2d.clearRect(0, 0, size, size);\r
+ Rectangle2D bounds = source.getBounds();\r
+ double maxB = Math.max(bounds.getWidth(), bounds.getHeight());\r
+ double scale = size/maxB;\r
+ g2d.setTransform(new AffineTransform(\r
+ scale, 0.0, 0.0, scale, -bounds.getMinX()*scale, -bounds.getMinY()*scale\r
+ ));\r
+ sceneGraph.render(g2d);\r
+\r
+ return targetImage;\r
+ }\r
+\r
+ public static ImageData renderIcon(ReadGraph graph, Resource symbolResource, int size) throws DatabaseException {\r
+ try {\r
+ BufferedImage img = renderBufferedImage(graph, symbolResource, size);\r
+ if (img == null)\r
+ return null;\r
+ return ImageUtils.convertToSWT(img);\r
+ } catch (Exception e) {\r
+ // FIXME: this is here because rendering a symbol may crash in\r
+ // totally unexpected ways if the symbol contains unrenderable content.\r
+ return null;\r
+ }\r
+ }\r
+\r
+}\r