JDK-8255021 : Graphics drawString with AffineTransform font, can't print to paper
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 8u261,11,15,16
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86_64
  • Submitted: 2020-10-14
  • Updated: 2023-12-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 23
23Unresolved
Description
ADDITIONAL SYSTEM INFORMATION :
JDK8 U261/LINUX

A DESCRIPTION OF THE PROBLEM :
1, Graphics drawString with normal font, can print to paper.
2, Graphics drawString with AffineTransform font, can't print to paper.


REGRESSION : Last worked in version 8u261

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1, Use Eclipse run text code as Application, would popup a print dialog.
2, Click OK to print.
3, Check paper.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Hope there have text on paper.
ACTUAL -
No text on paper.

---------- BEGIN SOURCE ----------
package taishan;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.Properties;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class PrintFrame2 extends JFrame implements Printable
{
    private final static AffineTransform CLOCKWISE_TX     = AffineTransform.getRotateInstance( Math.PI / 2);
    private final static String FONT_NAME_SONGTI = "Dialog";
    private final static int FONT_SIZE  = 30;

	private final static String TAISHAN = "Taishan Office";
	
	private final static int   startX   = 200;
	private final static int   startY   = 200;

	public PrintFrame2()
	{
	    this.getContentPane().setBackground(Color.WHITE);
	    
	    Font font = new Font(FONT_NAME_SONGTI, 0, FONT_SIZE);
	    font = font.deriveFont(CLOCKWISE_TX);
		this.setFont(font);
	}

	@Override
    public void paint(Graphics g)
    {
		super.paint(g);
	    g.drawString(TAISHAN, startX, startY);
    }
	
	public void startPrint()
	{
		Toolkit kit = Toolkit.getDefaultToolkit();
		
		Properties props = new Properties();
		props.put("awt.print.printer",  "durango");
		props.put("awt.print.numCopies", "1");
		PrintJob printJob = kit.getPrintJob(this, "Print Frame", props);
		
		Graphics g = printJob.getGraphics();
		this.paintAll(g);
		g.dispose();
		
		printJob.end();
	}
    
	@Override
    public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
    {
        if (pageIndex > 0)
        {
            return Printable.NO_SUCH_PAGE;
        }
        return Printable.PAGE_EXISTS;
    }

    public static void main(String[] args)
    {
    	PrintFrame2 frame = new PrintFrame2();
        frame.setSize(1000, 600);
        frame.setVisible(true);
        
       	frame.startPrint();
    }

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I resolved this problem.
sun.print.PathGraphics
{
    protected void drawString(String str, float x, float y,
                              Font font, FontRenderContext frc, float w) {
        TextLayout layout =
            new TextLayout(str, font, frc);
        Shape textShape =
            layout.getOutline(AffineTransform.getTranslateInstance(x, y));
        fill(textShape);

        // !!! add this code would print it OK. !!!
       layout.draw(this, x, y);
    }

}

FREQUENCY : always



Comments
Printing rotated fonts does work. We do it all the time. Now this test case looks just plain weird. It is using the old AWT 1.1 PrintJob API but implements the newer 1.2 2D Printable but does not use it. I'd be more interested if this reproduced using that since the 1.1 API only supports Graphics not Graphics2D And the "fix" is odd since TextLayout.draw() should ultimately end up, after more work, back doing a fill of an outline equivalent to the line above that supposedly does not work.
19-10-2020

Checked with attached testcase for following JDK version in Ubuntu 18.04, issue is reproducible, no text is observed, printing to PDF Results: ========== 8u261: FAIL 11.0.8: FAIL 16ea20: FAIL
19-10-2020