🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9285

FontMetrics.getStringBounds() gives wrong results when used with a scaled Graphics

#9285
From: Udo Corban
Date: Thu, 20 Apr 2000 00:00
53 lines
2680 bytes
Hi,
I am struggling with finding out the exact lenght of a string when drawing
to screen.
We are working inside a user coordinate space (in 1/100 mm, for example) and
scale the graphics down on drawing.
When i try to draw a text and then draw a surrounding rectangle, which width
and height I got width FontMetrics.getStringBounds(), the width of the
Rectangle doesn't match the width of the text. Sometimes the text is shorter
than the surrounding rectangle, but sometimes , even worse, the text extends
over the rectangle.
The problem can easily be reproduced by replacing the paintComponent method
of a JPanel with:

      protected void paintComponent (Graphics g)
         {
         int scale = 2;  // works only with scale == 1, wrong results for
all other values
         int iy;
         FontMetrics curFontMetrics = null;
         java.awt.geom.Rectangle2D bounds = null;
         Font curFont;

         super.paintComponent (g);

         Graphics2D gd = (Graphics2D) g;
         gd.scale(1.0 / scale, 1.0 / scale);
         iy = 0;
         String text = "a quick brown Fox jumps over the lazy dog";

         for (int fontSize = 6; fontSize < 24; fontSize++)
            {
            iy = fontSize * 24 * scale;
            curFont = new Font ("Arial", 0, fontSize * scale);
            gd.setFont (curFont);
            g.drawString (text, 0, iy);

            curFontMetrics = g.getFontMetrics (curFont);
            bounds = curFontMetrics.getStringBounds(text, gd);
            g.drawRect(0, iy - curFontMetrics.getAscent()+
curFontMetrics.getDescent(), (int) bounds.getWidth(), (int)
bounds.getHeight());
            }
         }
      }

The text output stays the same, whatever scale factor I use, but the width
of the surrounding rectangle varies.
The only way I found out to make it work is to use the TEXT_ANTIALIASING and
FRACTIONAL_FONTMETRICS rendering flags with the Graphics, but I don't wan't
to be forced to do this.
Anybody knows whats going wrong here?

Udo Corban
Technical Details
Message-ID: <8dmut8$81r64$1@fu-berlin.de>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,d456f1d38f8b7692,start
X-Google-Attributes: gid111d45,public
X-Google-Thread: 10fd6d,d456f1d38f8b7692,start
X-Google-Attributes: gid10fd6d,public
From: "Udo Corban" <udo.corban@netronic.de>
Subject: FontMetrics.getStringBounds() gives wrong results when used with a scaled Graphics
Date: 2000/04/20
Message-ID: <8dmut8$81r64$1@fu-berlin.de>
X-Deja-AN: 613566014
X-Trace: fu-berlin.de 956235496 8449220 212.184.13.154 (16 [13018])
Newsgroups: borland.public.jbuilder.java.swing,comp.lang.java.gui
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0