JDK-6804856 : Make FontMetrics used for printing public and permanent
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2009-02-12
  • Updated: 2010-04-04
  • Resolved: 2009-02-13
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
JTextComponent now uses TextComponentPrintable to print and control page breaks.  The main benefit was to make sure lines of text did not get split between two different pages.  It also provided rudimentary header / footer mechanism.

Unfortunately, this change relies on native fonts for the layout of the printed content.  Specifically:

fm = sun.font.FontDesignMetrics.getMetrics(font, context)

While this change helps those who could not control printing before, it breaks code for those of us who had.

Prior to this change we could measure the size and placement of views to determine where to make page breaks.  We could then use these measurements to control more sophisticated page breaks in html documents.

For example, we could examine placement of a section of text on a page to make sure critical sections of documents were kept together.  (e.g.,  we could keep signature lines on the same page as important preceding text.)  We called this feature conditional page breaks.

However, now that printing uses a different set of fonts, with different metrics, our old mechanisms could not reliably measure the document views for printing purposes.



JUSTIFICATION :
We temporarily solved our problem by using:

fm = sun.font.FontDesignMetrics.getMetrics(font, context)

However, when compiled, the compiler warns that this feature may be removed in the future.

We need a reliable and permanent interface that will allow us to accurately measure printed text, so we do not have to rewrite this (again) in the future.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
If Sun uses a different set of fonts to control printing, those same fonts should be accessible to everyone else, and we should be able to rely on them over time.
ACTUAL -
The fonts are accessible, but this accessibility does not appear to be permanent.  If you change the fonts you use, make them available to us!

---------- BEGIN SOURCE ----------
there are numerous bug reports regarding spacing and sizing of printed fonts.


try to print a JEditorPage

1. using print(g2d) or rootView.paint(g2d, rect);
2.  with html content that uses serif fonts and
3. has multiple consecutive words with bold text


You will see that the spaces between words disappear in some cases.  For example, you might see THISISATITLE instead of THIS IS A TITLE.

The reason is that the Graphics2D fonts have different metrics than the native fonts that are now used for printing.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
class MyClass extends JEditorPane implements Printable
{

        print(....)
        {
            Graphics2D g2d = (Graphics2D)g;
            context = g2d.getFontRenderContext();
            ...
        }

        public FontMetrics getFontMetrics(Font font)
        {
            // This access needs to be permanent
            return sun.font.FontDesignMetrics.getMetrics(font, context);
        }


}

Comments
EVALUATION I think this is just the same as bug 6784965 and 6784965. which reduces to problems in TextLayout.getJustifiedLayout() which is used by Swing since 6u10.
13-02-2009