This discussion is archived
6 Replies Latest reply: Oct 27, 2012 11:14 AM by Jörg RSS

Additional CR when printing unicode character

Jörg Explorer
Currently Being Moderated
Hello,

the following code prints two lines on a printer. The second line contains an escaped unicode character. When running the example code I can't even see the unicode character on the paper; but in my original application it is visible. So this is not the problem. But always an additional carriage return is inserted after the unicode character, thus overprinting the beginning of the line with the remaining text. This did not occur in java 6 if I remember correctly. Is there any workaround for this?

Regards
J.
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
 
public class HelloWorldPrinter implements Printable, ActionListener {
 
    public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {
        if (page > 0) return NO_SUCH_PAGE;
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g.drawString("Hello world!", 100, 100);
        g.drawString("Now the line with a unicode \u2259 character.", 100, 120);
        return PAGE_EXISTS;
    }
 
    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
                  System.out.println(ex);
             }
         }
    }
 
    public static void main(String args[]) {
//        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Hello World Printer");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
           System.exit(0);
        }
        });
        JButton printButton = new JButton("Print");
        printButton.addActionListener(new HelloWorldPrinter());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
}
  • 1. Re: Additional CR when printing unicode character
    sabre150 Expert
    Currently Being Moderated
    I don't see this problem using 1.7.0_09 on Ubuntu 11.04 . Though I don't like the implications, it seems likely to me that on your system the Font being used does not have a Glyph for the \u2259 character . If I am right then the solution is to find a font that does support the \u2259 character.
  • 2. Re: Additional CR when printing unicode character
    Jörg Explorer
    Currently Being Moderated
    Hello Sabre,

    thank you for testing. Of course it's good news that you don't see any overprinting, but unfortunately that doesn't help me on my site (Win7 64-Bit, java 1.7.0_09).
    Since I can see the character in my original application as I wrote, I made up the following test case and met a strange behaviour. I can make my desired unicode character appear without any overprinting as long as I don't use any other character in the string below 2000(16).
    Kindly run the following code. It prints a line of unicode characters starting with my desired one.
    Then change iConstant in makeTextLine() to any value below 0x2000. At my site this results in overprinting.
    Does it also happen at yours?
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.print.*;
    import javax.swing.*;
    
    public class UnicodeTest extends JFrame implements Printable {
      String text= "Test line with unicode \u2259 character.";
    
      public UnicodeTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 200);
        text= makeTextLine();
        JLabel lb= new JLabel("<html>Printing started...<br>"+text);
        add(lb);
        setVisible(true);
        PrinterJob printerJob= PrinterJob.getPrinterJob();
        printerJob.setPrintable(this);
        try {
          printerJob.print();
        }
        catch (PrinterException e) {
          System.out.println("PrinterException: "+e);
        }
      }
    
    
      public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
         new UnicodeTest();
          }
        });
      }
    
    
      public String makeTextLine() {
        StringBuilder sb= new StringBuilder();
        int iStartValue= 0x2259;
        int iConstant= 0x2020; // Tested with 0x100, 200, 400, 800, 1000, 2000.
        for (int i=iStartValue; i<iStartValue+30; i++) {
          sb.append((char)i);
          sb.append((char)iConstant);
        }
        return sb.toString();
      }
    
    
      public int print(Graphics g, PageFormat pf, int page) throws
                                                            PrinterException {
        if (page > 0) return NO_SUCH_PAGE;
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        g2d.setFont(new Font("Dialog", Font.PLAIN, 12));
        g.drawString(text, 50, 40);
        return PAGE_EXISTS;
      }
    
    }
  • 3. Re: Additional CR when printing unicode character
    sabre150 Expert
    Currently Being Moderated
    Did you look for a font that does display those characters?
  • 4. Re: Additional CR when printing unicode character
    Jörg Explorer
    Currently Being Moderated
    I could and can see it with the ordinary "Dialog" Font. So I didn't look for another.
  • 5. Re: Additional CR when printing unicode character
    sabre150 Expert
    Currently Being Moderated
    Jörg wrote:
    I could and can see it with the ordinary "Dialog" Font. So I didn't look for another.
    I could be wrong but the only cause for this that I can envisage is if there is a Font problem. To decide whether I am right or wrong you need to try several different fonts that have glyphs for the offending characters. If ALL of these still cause the CR effect then you can confidently raise a bug report but of course unless you can afford to wait for a long time for the fix you will still have to find a work-around.
  • 6. Re: Additional CR when printing unicode character
    Jörg Explorer
    Currently Being Moderated
    You are right, it is - also - a font problem. For I replaced now the "Dialog" by "Arial" and by "Courier New" and in both cases overprinting doesn't happen any more, even if I use a capital A as constant character between the high ranking unicodes. Still the glyph must be defined also in "Dialog" font, how else could it appear correctly on the JLabel or if I replace the "A" by a character of 0x2000 or above?

    And specifying one of the "Arial" or "Courier New" fonts doesn't solve the problem either. For if I use my original string "Test line with unicode \u2259 character.", the unicode character is printed as a square which normally signals that the glyph is not defined. But the string assembled in the StringBuilder printed everything correctly and is thus a prove that the glyph is defined.

    Very strange all this.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points