X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fgfx%2FTextImageDescriptor.java;fp=bundles%2Forg.simantics.utils.ui%2Fsrc%2Forg%2Fsimantics%2Futils%2Fui%2Fgfx%2FTextImageDescriptor.java;h=0289334f11848d3f94230dbc0da075b7013b1a87;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..0289334f1 --- /dev/null +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/gfx/TextImageDescriptor.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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.utils.ui.gfx; + +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.awt.image.Raster; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.PaletteData; +import org.simantics.utils.ObjectUtils; + +public class TextImageDescriptor extends ImageDescriptor { + + static final PaletteData _RGB = new PaletteData(0x00ff0000, 0x0000ff00, 0x000000ff); + + public String text; + public int width, height; + public String font; + public int fontSize; + public int style; + public int rgb; + + private transient int hash; + + public TextImageDescriptor(String text, int width, int height, + String font, int fontSize, + int style, int rgb) { + this.text = text; + this.width = width; + this.height = height; + this.font = font; + this.fontSize = fontSize; + this.style = style; + this.rgb = rgb; + + hash = text.hashCode() + 3*width + 5*height + 7*font.hashCode() + 11*fontSize + 13*style + 17*rgb; + } + + @Override + public ImageData getImageData() { + ImageData id = new ImageData(width, height, 24, _RGB); + BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) bi.getGraphics(); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + Font f = new Font(font, style, fontSize); + g.setFont( f ); + Color c = new Color(rgb); + g.setColor( c ); + FontMetrics fm = g.getFontMetrics(f); + Rectangle2D rect = fm.getStringBounds(text, g); + g.drawString(text, (float) ((width-rect.getWidth())/2), (float) ( (height/2) + (height-rect.getHeight())/2) ); + g.dispose(); + + Raster alpha = bi.getAlphaRaster(); + for (int x=0; x