Skip to main content



      Home
Home » Eclipse Projects » GEF » Determining exact text width in current font
Determining exact text width in current font [message #11106] Sun, 23 June 2002 09:07 Go to next message
Eclipse UserFriend
This question is actually a more general SWT question, but it is in the
context of GEF/Draw2D, and the solution may be GEF/D2D specific so I
thought I would post it here first.

I have custom Shapes that resize themselves based on the text content
that's contained within them, so I would like to make sure that the size
(width and height) of the shape matches, and wholy contains the text
within.

Using the passed in Graphics to the shape's draw routine, I can obtain a
FontMetrics, and with it, I can compute the height accurately of my
text. However, FontMetrics only returns the average character width of
the current font, not the exact graphic width of a text string so the
figure width becomes pretty inexact if I have a set of especialy narrow
(all 'i'), or especially wide (all 'W') characters.

Is there something more akin to the FontMetrics class of AWT/Swing that
can actually return the exact string of a string that I pass into it, or
a way to compute this information from the current Font?



Thanks,
-Eugene.
Re: Determining exact text width in current font [message #11116 is a reply to message #11106] Sun, 23 June 2002 09:45 Go to previous messageGo to next message
Eclipse UserFriend
Nevermind, I stumbled across all the getTextExtend and getStringExtent
and FigureUtilities methods myself.


Eugene Bekker wrote:
> This question is actually a more general SWT question, but it is in the
> context of GEF/Draw2D, and the solution may be GEF/D2D specific so I
> thought I would post it here first.
>
> I have custom Shapes that resize themselves based on the text content
> that's contained within them, so I would like to make sure that the size
> (width and height) of the shape matches, and wholy contains the text
> within.
>
> Using the passed in Graphics to the shape's draw routine, I can obtain a
> FontMetrics, and with it, I can compute the height accurately of my
> text. However, FontMetrics only returns the average character width of
> the current font, not the exact graphic width of a text string so the
> figure width becomes pretty inexact if I have a set of especialy narrow
> (all 'i'), or especially wide (all 'W') characters.
>
> Is there something more akin to the FontMetrics class of AWT/Swing that
> can actually return the exact string of a string that I pass into it, or
> a way to compute this information from the current Font?
>
>
>
> Thanks,
> -Eugene.
>
Re: Determining exact text width in current font [message #112962 is a reply to message #11116] Wed, 21 January 2004 07:50 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: holger.herzog.hpi.uni-potsdam.de

Hi!

Eugene Bekker wrote:

> Nevermind, I stumbled across all the getTextExtend and getStringExtent
> and FigureUtilities methods myself.

Hm ... but these utilities always seem to use the current UI display
device to scale the font. Using these methods e.g. for printing will
lead to wrong values.


Regards,

Holger
Re: Determining exact text width in current font [message #113351 is a reply to message #112962] Thu, 22 January 2004 17:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

What do you suggest? In theory the fonts on the screen should scale the
same as everything else. WYSIWYG??

By the way, all of those methods require you to pass the font you want to
use for measuring. so, you could pass Printer equivalents if you wanted.

"Holger Herzog" <holger.herzog@hpi.uni-potsdam.de> wrote in message
news:bulsgb$ja0$1@eclipse.org...
> Hi!
>
> Eugene Bekker wrote:
>
> > Nevermind, I stumbled across all the getTextExtend and getStringExtent
> > and FigureUtilities methods myself.
>
> Hm ... but these utilities always seem to use the current UI display
> device to scale the font. Using these methods e.g. for printing will
> lead to wrong values.
>
>
> Regards,
>
> Holger
Re: Determining exact text width in current font [message #114412 is a reply to message #113351] Wed, 28 January 2004 11:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: holger.herzog.hpi.uni-potsdam.de

Hi!

Randy Hudson wrote:
> What do you suggest? In theory the fonts on the screen should scale the
> same as everything else. WYSIWYG??

In practice this doesn't seem to be true. ;) I think font scaling also
depends on the resolution of the device. e.g: if the text width in
pixels of a string using a 10pt font on 100 dpi device is 200 pixels.
then, the same font and string on a 600 dpi device should lead to 1.200
pixel. But because of quantisation effects it also can lead to e.g.
1.191 or 1.215 pixel. (I guess ;)

You can reproduce this effect with GEF: When e.g. running logic example
and placing a label figure onto the graph, there will appear a border
around the text. On the screen, the text will fit exactly into this
border. But when printing, the text will be scaled different and won't
fit exactly anymore.

Maybe I am not close enough to SWT. FigureUtilities.getTextDimension()
will pass the Font to the SWT GC object that was created by
FigureUtilities for this usage (see getGC()):

protected static GC getGC() {
if (gc == null) {
gc = new GC(new Shell());
appliedFont = gc.getFont();
}
return gc;
}

What happens when calling getGC.setFont(f) and getGC.textExtend(s)? I
would expect that the font f must have been created for the device that
the GC was created for? Or will the font become recreated for the given
device by the SWT-OS library? If so, what device would be used? The
device the GC was created on or the device the font was created on? I
would expect the first one - and that would explain the "wrong" scaling
when printing.

Randy Hudson wrote:
> What do you suggest?

Nothing yet, still thinking about it ...


Regards,

Holger
Re: Determining exact text width in current font [message #114504 is a reply to message #114412] Wed, 28 January 2004 13:15 Go to previous message
Eclipse UserFriend
Originally posted by: none.us.ibm.com

> In practice this doesn't seem to be true. ;) I think font scaling also
> depends on the resolution of the device. e.g: if the text width in
> pixels of a string using a 10pt font on 100 dpi device is 200 pixels.
> then, the same font and string on a 600 dpi device should lead to 1.200
> pixel. But because of quantisation effects it also can lead to e.g.
> 1.191 or 1.215 pixel. (I guess ;)

Mostly its due to the fact that a human hand-tunes the spacing for fonts at
their most common screen sizes, 10 and 12. In some cases a raster
representation is used instead of vector. This guarantees that something
like "!" has a gap between the dot and the line.

I believe AWT's Java2D is a little better regarding measuring and zooming
fonts.
Previous Topic:Font Family in GEF?
Next Topic:splitter class
Goto Forum:
  


Current Time: Fri May 09 11:54:14 EDT 2025

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

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

Back to the top