Skip to main content


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 13:34 Go to next message
Greg Brown is currently offline Greg BrownFriend
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 07:20 Go to previous messageGo to next message
Viliam Durina is currently offline Viliam DurinaFriend
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 13:07 Go to previous messageGo to next message
Greg Brown is currently offline Greg BrownFriend
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 13:48 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
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 14:39 Go to previous message
Greg Brown is currently offline Greg BrownFriend
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: Fri Apr 26 00:59:35 GMT 2024

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

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

Back to the top