]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.ui.gfx;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.FontMetrics;\r
17 import java.awt.Graphics2D;\r
18 import java.awt.RenderingHints;\r
19 import java.awt.geom.Rectangle2D;\r
20 import java.awt.image.BufferedImage;\r
21 import java.awt.image.Raster;\r
22 \r
23 import org.eclipse.jface.resource.ImageDescriptor;\r
24 import org.eclipse.swt.graphics.ImageData;\r
25 import org.eclipse.swt.graphics.PaletteData;\r
26 import org.simantics.utils.ObjectUtils;\r
27 \r
28 public class TextImageDescriptor extends ImageDescriptor {\r
29 \r
30         static final PaletteData _RGB = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff);\r
31         \r
32         public String text;\r
33         public int width, height;\r
34         public String font;\r
35         public int fontSize;\r
36         public int style;\r
37         public int rgb;\r
38         \r
39         private transient int hash;\r
40         \r
41         public TextImageDescriptor(String text, int width, int height, \r
42                         String font, int fontSize, \r
43                         int style, int rgb) {\r
44                 this.text = text;\r
45                 this.width = width;\r
46                 this.height = height;\r
47                 this.font = font;\r
48                 this.fontSize = fontSize;\r
49                 this.style = style;\r
50                 this.rgb = rgb;\r
51                                 \r
52                 hash = text.hashCode() + 3*width + 5*height + 7*font.hashCode() + 11*fontSize + 13*style + 17*rgb;\r
53         }\r
54 \r
55         @Override\r
56         public ImageData getImageData() {\r
57         ImageData id = new ImageData(width, height, 24, _RGB);          \r
58         BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);\r
59         Graphics2D g = (Graphics2D) bi.getGraphics();\r
60         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
61         Font f = new Font(font, style, fontSize);\r
62         g.setFont( f );\r
63         Color c = new Color(rgb);\r
64         g.setColor( c );\r
65         FontMetrics fm = g.getFontMetrics(f);\r
66         Rectangle2D rect = fm.getStringBounds(text, g);\r
67         g.drawString(text, (float) ((width-rect.getWidth())/2), (float) ( (height/2) + (height-rect.getHeight())/2) );\r
68         g.dispose();        \r
69         \r
70         Raster alpha = bi.getAlphaRaster();        \r
71         for (int x=0; x<width; x++) {\r
72                 for (int y=0; y<height; y++) {\r
73                         int a = alpha.getSample(x, y, 0);\r
74                         int rgb = bi.getRGB(x, y);\r
75                         id.setAlpha(x, y, a);\r
76                         id.setPixel(x, y, rgb);\r
77                 }\r
78         }\r
79         \r
80                 return id;\r
81         }\r
82         \r
83         @Override\r
84         public int hashCode() {\r
85                 return hash;\r
86         }\r
87         \r
88         @Override\r
89         public boolean equals(Object obj) {\r
90                 if (obj instanceof TextImageDescriptor==false) return false;\r
91                 TextImageDescriptor o = (TextImageDescriptor) obj;\r
92                 if ( !ObjectUtils.objectEquals(text, o.text) ) return false;\r
93                 if ( !ObjectUtils.objectEquals(font, o.font) ) return false;\r
94                 return width==o.width && height==o.height && fontSize==o.fontSize && style==o.style && rgb==o.rgb;\r
95         }\r
96 \r
97 }\r