]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/TextImageDescriptor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / gfx / TextImageDescriptor.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/TextImageDescriptor.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/TextImageDescriptor.java
new file mode 100644 (file)
index 0000000..0289334
--- /dev/null
@@ -0,0 +1,97 @@
+/*******************************************************************************\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.utils.ui.gfx;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.FontMetrics;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.geom.Rectangle2D;\r
+import java.awt.image.BufferedImage;\r
+import java.awt.image.Raster;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.swt.graphics.ImageData;\r
+import org.eclipse.swt.graphics.PaletteData;\r
+import org.simantics.utils.ObjectUtils;\r
+\r
+public class TextImageDescriptor extends ImageDescriptor {\r
+\r
+       static final PaletteData _RGB = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);\r
+       \r
+       public String text;\r
+       public int width, height;\r
+       public String font;\r
+       public int fontSize;\r
+       public int style;\r
+       public int rgb;\r
+       \r
+       private transient int hash;\r
+       \r
+       public TextImageDescriptor(String text, int width, int height, \r
+                       String font, int fontSize, \r
+                       int style, int rgb) {\r
+               this.text = text;\r
+               this.width = width;\r
+               this.height = height;\r
+               this.font = font;\r
+               this.fontSize = fontSize;\r
+               this.style = style;\r
+               this.rgb = rgb;\r
+                               \r
+               hash = text.hashCode() + 3*width + 5*height + 7*font.hashCode() + 11*fontSize + 13*style + 17*rgb;\r
+       }\r
+\r
+       @Override\r
+       public ImageData getImageData() {\r
+        ImageData id = new ImageData(width, height, 24, _RGB);         \r
+        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);\r
+        Graphics2D g = (Graphics2D) bi.getGraphics();\r
+        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
+        Font f = new Font(font, style, fontSize);\r
+        g.setFont( f );\r
+        Color c = new Color(rgb);\r
+        g.setColor( c );\r
+        FontMetrics fm = g.getFontMetrics(f);\r
+        Rectangle2D rect = fm.getStringBounds(text, g);\r
+        g.drawString(text, (float) ((width-rect.getWidth())/2), (float) ( (height/2) + (height-rect.getHeight())/2) );\r
+        g.dispose();        \r
+        \r
+        Raster alpha = bi.getAlphaRaster();        \r
+        for (int x=0; x<width; x++) {\r
+               for (int y=0; y<height; y++) {\r
+                       int a = alpha.getSample(x, y, 0);\r
+                       int rgb = bi.getRGB(x, y);\r
+                       id.setAlpha(x, y, a);\r
+                       id.setPixel(x, y, rgb);\r
+               }\r
+        }\r
+        \r
+               return id;\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               return hash;\r
+       }\r
+       \r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj instanceof TextImageDescriptor==false) return false;\r
+               TextImageDescriptor o = (TextImageDescriptor) obj;\r
+               if ( !ObjectUtils.objectEquals(text, o.text) ) return false;\r
+               if ( !ObjectUtils.objectEquals(font, o.font) ) return false;\r
+               return width==o.width && height==o.height && fontSize==o.fontSize && style==o.style && rgb==o.rgb;\r
+       }\r
+\r
+}\r