Determining exact text width in current font [message #11106] |
Sun, 23 June 2002 09:07  |
Eclipse User |
|
|
|
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   |
Eclipse User |
|
|
|
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 #114504 is a reply to message #114412] |
Wed, 28 January 2004 13:15  |
Eclipse User |
|
|
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.04625 seconds