My context: I create a shape with an icon and (multi-)text in the AddFeature and I want to set the width of the shape according to the length of the text. Is there already a Graphiti function for doing that?
My current approach is to multiply the text length with a certain factor but this works out not very well. I found another approch by using FontMetrics in another Eclipse forum post, but do not know how to get a drawable from a Graphiti shape.
GC gc = new GC(drawable);
FontMetrics fontMetrics = gc.getFontMetrics();
int width = fontMetrics.getAverageCharWidth() * text.toCharArray().length;
Michael Wenz Messages: 1812 Registered: July 2009 Location: Walldorf, Germany
Graphiti provides the UI layout service for doing this
(GraphitiUi.getUiLayoutService().calculateTextSize(String text, Font font)).
As you see it also requires the font (and in the end delegates to draw2D
TextUtilities), but for knowing the correct extend of a text it is important
to know the font because characters might have different widths in different
sure you need the concrete font properties to calculate the size.
Thanks for pointing to GraphitiUi.getUiLayoutService().calculateTextSize
But org.eclipse.graphiti.mm.algorithms.AbstractText#getFont returns
org.eclipse.graphiti.mm.algorithms.styles.Font the method above needs
org.eclipse.swt.graphics.Font. SO you have to convert the Graphiti font
to a SWT font first