Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Measuring text
Measuring text [message #649066] Mon, 17 January 2011 08:34 Go to next message
Greg Brown is currently offline Greg Brown
Messages: 16
Registered: December 2010
Junior Member
I'm looking for the equivalent of AWT's Font#getLineMetrics() and Font#getStringBounds() in AWT. I have seen this snippet:

http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.sn ippets/src/org/eclipse/swt/snippets/Snippet55.java?view=mark up&content-type=text%2Fvnd.viewcvs-markup&revision=H EAD

but it appears to require the existence of a GC in order to measure the text. Is there any way to determine the size of a string without a GC? Specifically, I need this information in order to perform layout calculations, which occur before the GC is created for painting.

If there is no existing public API for this, what is the recommended practice? In other words, how might I best simulate the behavior of AWT's getFontMetrics() and getStringBounds()?

Thanks,
Greg
Re: Measuring text [message #649406 is a reply to message #649066] Wed, 19 January 2011 02:20 Go to previous messageGo to next message
Viliam Durina is currently offline Viliam Durina
Messages: 13
Registered: July 2009
Junior Member
Maybe you could go with, but I did not test:

GC gc = new GC(Display.getDefault());

You need GC to set the font, color, style etc., and then measure the texts. Don't forget to dispose the gc.

Viliam

On 17.1.2011 14:34, Greg Brown wrote:
> I'm looking for the equivalent of AWT's Font#getLineMetrics() and Font#getStringBounds() in AWT. I have seen this snippet:
>
> http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.swt.sn ippets/src/org/eclipse/swt/snippets/Snippet55.java?view=mark up&content-type=text%2Fvnd.viewcvs-markup&revision=H EAD
>
> but it appears to require the existence of a GC in order to measure the text. Is there any way to determine the size of a string without a GC? Specifically, I need this information in order to perform layout calculations, which occur before the GC is created for painting.
>
> If there is no existing public API for this, what is the recommended practice? In other words, how might I best simulate the behavior of AWT's getFontMetrics() and getStringBounds()?
>
> Thanks,
> Greg
>
Re: Measuring text [message #649491 is a reply to message #649406] Wed, 19 January 2011 08:07 Go to previous messageGo to next message
Greg Brown is currently offline Greg Brown
Messages: 16
Registered: December 2010
Junior Member
Thanks Viliam. I think that will probably work, though I was hoping for a solution that did not require creating a GC. Seems wasteful and I'm not sure what the performance implications might be.

Greg
Re: Measuring text [message #649509 is a reply to message #649491] Wed, 19 January 2011 08:48 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel Krügler
Messages: 853
Registered: July 2009
Senior Member
On 2011-01-19 14:07, Greg Brown wrote:
> Thanks Viliam. I think that will probably work, though I was hoping for
> a solution that did not require creating a GC. Seems wasteful and I'm
> not sure what the performance implications might be.

You need a GC. Depending on your calling context this already exist
(part of the event type) or you need to create it.

Greetings from Bremen,

Daniel Krügler
Re: Measuring text [message #649515 is a reply to message #649509] Wed, 19 January 2011 09:39 Go to previous message
Greg Brown is currently offline Greg Brown
Messages: 16
Registered: December 2010
Junior Member
Hi Daniel,

Thanks for confirming that I need a GC for this. Any idea why that is? In AWT and Android, a drawing context is not necessary to measure text (though of course it is required to draw text). Just wondering what the difference might be.

Thanks,
Greg

Previous Topic:Drawing gradients
Next Topic:Hyperlinks in tree node text?
Goto Forum:
  


Current Time: Thu Jul 31 05:36:59 EDT 2014

Powered by FUDForum. Page generated in 0.01695 seconds