Bug in getFontMetrics

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).
Locked
mick.francis
Posts: 2
Joined: Fri Oct 07, 2016 3:51 pm
antibot: No, of course not.

Bug in getFontMetrics

Post by mick.francis » Fri Oct 07, 2016 4:15 pm

I am seeing incorrect behaviour with regard to getting String widths in an SVGGraphics2D object; the x-scaling is being ignored.

I think getFontMetrics(...) should be setting the current Transform into the temporary Graphics object:

Code: Select all

    @Override
    public FontMetrics getFontMetrics(Font f) {
        if (this.fmImage == null) {
            this.fmImage = new BufferedImage(10, 10, 
                    BufferedImage.TYPE_INT_RGB);
            this.fmImageG2D = this.fmImage.createGraphics();
            this.fmImageG2D.setRenderingHint(
                    RenderingHints.KEY_FRACTIONALMETRICS, 
                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        }
        this.fmImageG2D.setTransform(this.getTransform()); // **** ADD THIS LINE? ****
        return this.fmImageG2D.getFontMetrics(f);
    }
Great package by the way - thanks!

Regards,

Mick.

mick.francis
Posts: 2
Joined: Fri Oct 07, 2016 3:51 pm
antibot: No, of course not.

Re: Bug in getFontMetrics

Post by mick.francis » Fri Oct 07, 2016 5:36 pm

Ah - just tried version 3.1 and this fixes my problems!

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Bug in getFontMetrics

Post by david.gilbert » Sun Oct 09, 2016 10:22 am

That looks an interesting case...not sure why version 3.1 would change the outcome. I'll take a closer look.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Bug in getFontMetrics

Post by david.gilbert » Sun Oct 09, 2016 10:50 am

As far as I can tell, the font measurements don't change in relation to the transform set on the Graphics2D. Here is the test case I tried (against the Graphics2D obtained from a BufferedImage):

Code: Select all

    /** 
     * A test to check whether setting a transform on the Graphics2D affects
     * the results of text measurements performed via getFontMetrics().
     */
    @Test
    public void testGetFontMetrics() {
        Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 10);
        FontMetrics fm = this.g2.getFontMetrics(f);
        int w = fm.stringWidth("ABC");
        Rectangle2D bounds = fm.getStringBounds("ABC", this.g2);
        
        // after scaling, the string width is not changed
        this.g2.setTransform(AffineTransform.getScaleInstance(3.0, 2.0));
        fm = this.g2.getFontMetrics(f);
        assertEquals(w, fm.stringWidth("ABC"));
        assertEquals(bounds.getWidth(), fm.getStringBounds("ABC", this.g2).getWidth(), EPSILON);
    }
If you are running into a different case, could you try modifying the above test to demonstrate it?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked