Skip to main content



      Home
Home » Modeling » Graphiti » Calculate minimal width of text in shape
Calculate minimal width of text in shape [message #698875] Wed, 20 July 2011 08:14 Go to next message
Eclipse UserFriend
Hello,

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();
gc.dispose();
int width = fontMetrics.getAverageCharWidth() * text.toCharArray().length;

Joerg
Re: Calculate minimal width of text in shape [message #698888 is a reply to message #698875] Wed, 20 July 2011 08:33 Go to previous messageGo to next message
Eclipse UserFriend
Good question. I tried to find something for this as well and could not find it either. Also see this thread: http://www.eclipse.org/forums/index.php/m/651158/?srch=text+graphiti#msg_651158

Jos
Re: Calculate minimal width of text in shape [message #698898 is a reply to message #698888] Wed, 20 July 2011 08:40 Go to previous messageGo to next message
Eclipse UserFriend
Hi Jos,

with new Shell() the code seems to work.

GC gc = new GC(new Shell());
FontMetrics fontMetrics = gc.getFontMetrics();
gc.dispose();
int width = fontMetrics.getAverageCharWidth() * ((text != null) ? text.toCharArray().length : 0);

This was inspired by:
http://www.eclipse.org/forums/index.php/mv/msg/164898/522303/#msg_522303

There is also a FigureUtilities.getTextExtents but this requires a org.eclipse.swt.graphics.Font to pass
http://www.eclipse.org/forums/index.php/mv/msg/3735/11116/#msg_11116

Joerg
Re: Calculate minimal width of text in shape [message #699346 is a reply to message #698898] Thu, 21 July 2011 07:54 Go to previous messageGo to next message
Eclipse UserFriend
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
fonts.

The thread Jos found is a different issue.

HTH,
Michael

"Joerg Reichert" schrieb im Newsbeitrag
news:j06hpp$eli$1@news.eclipse.org...

Hi Jos,

with new Shell() the code seems to work.

GC gc = new GC(new Shell());
FontMetrics fontMetrics = gc.getFontMetrics();
gc.dispose();
int width = fontMetrics.getAverageCharWidth() * ((text != null) ?
text.toCharArray().length : 0);

This was inspired by:
http://www.eclipse.org/forums/index.php/mv/msg/164898/522303/#msg_522303

There is also a FigureUtilities.getTextExtents but this requires a
org.eclipse.swt.graphics.Font to pass
http://www.eclipse.org/forums/index.php/mv/msg/3735/11116/#msg_11116

Joerg
--
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Calculate minimal width of text in shape [message #699357 is a reply to message #699346] Thu, 21 July 2011 08:28 Go to previous messageGo to next message
Eclipse UserFriend
Hi Michael,

sure you need the concrete font properties to calculate the size.
Thanks for pointing to GraphitiUi.getUiLayoutService().calculateTextSize

There are two overloading methods one taking a org.eclipse.graphiti.mm.algorithms.styles.Font
and one taking a org.eclipse.swt.graphics.Font, so it fits will.

Regards
Joerg

[Updated on: Thu, 21 July 2011 09:04] by Moderator

Re: Calculate minimal width of text in shape [message #699385 is a reply to message #699357] Thu, 21 July 2011 09:08 Go to previous messageGo to next message
Eclipse UserFriend
Really, are you sure?

I see org.eclipse.graphiti.mm.algorithms.styles.Font for both the return
value of AbstractText.getFont and the Parameter font in
IUiLayoutService.calculateTextSize.

Michael


"Joerg Reichert" schrieb im Newsbeitrag
news:j095ed$v6m$1@news.eclipse.org...

Hi Michael,

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

org.eclipse.graphiti.ui.internal.util.DataTypeTransformation.toSwtFont(Font
pictogramFont)

When searching for references where this method is used, I found this method

org.eclipse.graphiti.ui.internal.services.impl.GefService.calculateTextSize(String
text, Font font)

that takes a org.eclipse.graphiti.mm.algorithms.styles.Font. So will use
this method instead (although it is in a internal package).

Regards
Joerg
--
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Re: Calculate minimal width of text in shape [message #699392 is a reply to message #699385] Thu, 21 July 2011 09:35 Go to previous message
Eclipse UserFriend
oh, sorry I mixed it up in the Eclipse search result view. There is only one method for org.eclipse.graphiti.mm.algorithms.styles.Font.

Joerg
Previous Topic:ImageProvider using provided images from third-party plugins
Next Topic:Resizing of a Polygon - Bug?
Goto Forum:
  


Current Time: Wed Jul 23 17:34:06 EDT 2025

Powered by FUDForum. Page generated in 0.08480 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top