Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Graphiti » Problems with calculated text size
Problems with calculated text size [message #1072662] Tue, 23 July 2013 04:21 Go to next message
Bernhard Berger is currently offline Bernhard Berger
Messages: 5
Registered: October 2011
Junior Member
Hi everybody,

I'm trying to layout some diagram and I have some problems with calculating the size of a text. Perhaps someone can enlighten me.
Concretely, I'm trying to display a rectangle and some text under it (you can see the result in the appended screenshot). Both elements are contained in an invisible rectangle. Now I've some problems with calculating the size of the text. I googled and found several forum threads mentioning calculateSize and so forth. I created the following code:

final Shape rectShape = peService.createShape(containerShape, true);
final RoundedRectangle rect = gaService.createRoundedRectangle(rectShape, 40, 40);

MultiText text = gaService.createMultiText(nameShape, businessObject.getName());
text.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER);

final Font textFont = gaService.manageDefaultFont(peService.getDiagramForShape(containerShape));
final IDimension dimension = GraphitiUi.getUiLayoutService().calculateTextSize(text.getValue(), textFont);

... layouting ...


My problem is that I do not understand the unit of the text dimension. The result is width=24 and height=9 but when I use it for the size of the text I can only see a very small part of it. If I multiply both values by 2 everything looks fine. Can someone explain the?

Regards,

Bernhard
Re: Problems with calculated text size [message #1072664 is a reply to message #1072662] Tue, 23 July 2013 04:22 Go to previous messageGo to next message
Bernhard Berger is currently offline Bernhard Berger
Messages: 5
Registered: October 2011
Junior Member
BTW: I made the screenshot in a very hackish state. When I multiply the values by 2 the text is in a single line.
Re: Problems with calculated text size [message #1072997 is a reply to message #1072664] Tue, 23 July 2013 19:03 Go to previous messageGo to next message
Aljoscha Hark is currently offline Aljoscha Hark
Messages: 24
Registered: March 2012
Junior Member
hi,

not sure about multitext, but on normal text GAs the function returns the exact size (as used by the GAs, too).

what to consider:
- the font thrown into the method should be the same as set to the text GA
- the text GA or could have padding/margins set, these have to be added

as far as I remember, the dimension calculation worked perfect when 'done right'.

regards,
aljoscha
Re: Problems with calculated text size [message #1075387 is a reply to message #1072997] Mon, 29 July 2013 06:45 Go to previous messageGo to next message
Bernhard Berger is currently offline Bernhard Berger
Messages: 5
Registered: October 2011
Junior Member
Hi Aljoscha,

I found the reason for my problem. I expected that Graphiti will use the default font if there is no font assigned to a text. Therefore, I used the default font for calculating the text size. But it seems that the default font is not used. If I explicitly set the font to the default font the look changes and the calculation is correct.

Regards, Bernhard
Re: Problems with calculated text size [message #1250899 is a reply to message #1072662] Wed, 19 February 2014 13:12 Go to previous messageGo to next message
nicolas h is currently offline nicolas h
Messages: 60
Registered: February 2011
Location: Grenoble, France
Member
Hi there,

I also need to calculate the size of a multitext, but I don't manage to.

I tried this two codes:

dimension = GraphitiUi.getUiLayoutService().calculateTextSize(text.getValue(), textFont);
and
dimension = gaService.calculateSize(text);


The first case works, but I want now to set the width et only calculate the height. Is that possible ?

I tried this:
gaService.setWidth(text, 100);
or
text.setWidth(100);


But when I calculate, the width value is greater than the one I set.

In the second case, the calculateSize method simply does not return a calculated value.

What is the best solution to calculate size at set width ?

Regards,

--
Nicolas
Re: Problems with calculated text size [message #1252790 is a reply to message #1250899] Fri, 21 February 2014 11:03 Go to previous messageGo to next message
Michael Wenz is currently offline Michael Wenz
Messages: 1590
Registered: July 2009
Location: Walldorf, Germany
Senior Member
Nicolas,

I'm afraid that is not easy. Graphiti (and AFAIK) also the GEF framework do
not offer a service for that.

To calculate that you can try to use
IUiLayoutService.calculateTextSize(String text, Font font, boolean
handleMultiline) and pass true for handleMultiline. When your text contains
newline characters at the location that would suite your with the method
will calculate the height.

The problem is in finding the location for the newline characted to add...
That could be possible to iteratively calculate by using the
calculateTextSize metod without the boolean and providing only the beginning
of the string until it fits.

But that's an ugly solution... :-(

Michael
Re: Problems with calculated text size [message #1258301 is a reply to message #1252790] Thu, 27 February 2014 03:46 Go to previous message
nicolas h is currently offline nicolas h
Messages: 60
Registered: February 2011
Location: Grenoble, France
Member
Hi Michael,

Thank's for your reply and my apologies for the lateness.

Indeed you're right, I found some topics that explains the same problems and some solutions that feel as ugly as the one you propose (iterating on each character and use the font width).

We could think that the feature is easy to implement but it's not true.

Many thanks for your reactivity Smile

Regards,

--
Nicolas
Previous Topic:Graphiti concepts and tutorial
Next Topic:Graphiti and RAP
Goto Forum:
  


Current Time: Mon Sep 01 07:38:58 EDT 2014

Powered by FUDForum. Page generated in 0.03962 seconds