Hello,
I have tested the program under Window 98 using HP LaserJet 4L. I am tried both
JDK1.3.1
English and international version, also I am tried to set locate to TW (Taiwan), US,
and default locale, but the stroke of character is not correctly rendered when
printing. There is no problem when rendering on screeen. Here is a standalone source
extracted from the orginal set of classes:
Thanks in advance.
Jason
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class Test extends JFrame {
public static void main(String[] args) {
Test test = new Test();
test.setVisible(true);
}
PrinterJob job;
PageFormat pageFormat;
DrawingPanel drawingPanel = new DrawingPanel();
JPanel buttonPanel = new JPanel();
JButton printButton = new JButton("Print");
Test() {
super("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawingPanel.setPreferredSize(new Dimension(500, 400));
getContentPane().add(drawingPanel, BorderLayout.CENTER);
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
job = PrinterJob.getPrinterJob();
pageFormat = job.defaultPage();
job.setPrintable(drawingPanel, pageFormat);
job.setCopies(1);
if (job.printDialog()) {
try {
job.print();
}
catch (Exception e) {
System.out.println("FilePrint : " + e);
}
}
}
});
buttonPanel.add(printButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
pack();
}
}
class DrawingPanel extends JPanel implements Printable {
static final String chineseText =
"\ufe51\ufe50\ufe55\ufe54\ufe56\ufe57\u300c\u300d\u3002\u3010\u3011\u3014\u3015\u4e0d\u662f\u516c\u6709\u7684;
\u4e0d\u80fd\u88ab\u5916\u90e8\u5305";
DefaultStyledDocument document = new DefaultStyledDocument();
String text = chineseText;
Vector textVector;
Vector attrStrVector;
int x = 0;
int y = 0;
int width = 400;
int height = 400;
int lineSpacing = 5;
Color textColor = Color.black;
DrawingPanel() {
try {
SimpleAttributeSet attrSet1 = new SimpleAttributeSet();
document.insertString(0, chineseText, attrSet1);
}
catch (BadLocationException ble) {
}
}
public void draw(Graphics2D g,int x,int y) {
Vector cache = layout(g);
textDraw(g, cache, x, y);
}
private void textDraw(Graphics2D g2d,Vector cache,int offsetX,int offsetY) {
int accHeight = 0;
for (int i=0; i < cache.size(); i++) {
TextLayout textLayout = (TextLayout)cache.elementAt(i);
accHeight += textLayout.getAscent();
g2d.setColor(textColor);
textLayout.draw(g2d, offsetX + x + 1,
offsetY + y + 1 + accHeight);
accHeight += (textLayout.getDescent() + (textLayout.getLeading() * lineSpacing));
}
textVector.clear();
attrStrVector.clear();
}
public void paint(Graphics _g) {
Graphics2D g = (Graphics2D) _g;
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
draw(g, 10, 10);
}
public int print(Graphics _g, PageFormat pageFormat, int pageIndex) throws
PrinterException {
if (pageIndex > 1) {
return Printable.NO_SUCH_PAGE;
}
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Graphics2D g = (Graphics2D) _g;
g.setColor(Color.white);
g.fillRect(0,
0,
(int)pageFormat.getWidth(),
(int)pageFormat.getHeight());
int newOffsetX = 0;
int newOffsetY = (int)(pageFormat.getImageableHeight() * pageIndex);
Shape oldShape = g.getClip();
g.setClip((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY(),
(int)(pageFormat.getImageableWidth()),
(int)(pageFormat.getImageableHeight()));
draw(g,
(int)(x - newOffsetX + pageFormat.getImageableX()),
(int)(y - newOffsetY + pageFormat.getImageableY()));
RepaintManager.currentManager(this).setDoubleBufferingEnabled(true);
return Printable.PAGE_EXISTS;
}
private Vector layout(Graphics2D g) {
Vector cache = new Vector();
float accHeight = 0;
TextFormatting formatting = new TextFormatting(document, text);
textVector = formatting.getTextVector();
attrStrVector = formatting.getAttrStrVector();
for (int j = 0; j < attrStrVector.size(); j++) {
String text = (String)textVector.elementAt(j);
if (text.length() == 0) {
AttributedString spaceAs = new AttributedString(" ");
AttributedCharacterIterator spaceAci = spaceAs.getIterator();
FontRenderContext spaceFrc = g.getFontRenderContext();
TextLayout spaceTl = new TextLayout(spaceAci,spaceFrc);
float spaceHeight = spaceTl.getAscent() + spaceTl.getDescent() +
spaceTl.getLeading();
accHeight += spaceHeight;
cache.addElement(spaceTl);
continue;
}
AttributedString as = (AttributedString)attrStrVector.elementAt(j);
FontRenderContext frc = g.getFontRenderContext();
AttributedCharacterIterator aci = as.getIterator();
LineBreakMeasurer lbm = new LineBreakMeasurer(aci,frc);
lbm.setPosition(0);
int i = 0;
while (true) {
TextLayout textLayout = lbm.nextLayout(width - 2);
if (textLayout == null)
break;
accHeight += (textLayout.getAscent() + textLayout.getDescent());
if (accHeight > height - 2)
break;
cache.addElement(textLayout);
accHeight += (textLayout.getLeading() * lineSpacing);
}
}
return cache;
}
}
class TextFormatting {
DefaultStyledDocument document;
String text;
Vector textVector = new Vector();
Vector attrStrVector = new Vector();
TextFormatting(DefaultStyledDocument document, String text) {
this.document = document;
this.text = text;
format();
}
private void format() {
boolean quit = false;
int index = 0, increment = 1;
int nextIndex;
int textIndex = 0;
String subText;
if (text.indexOf("\r\n") != -1)
increment = 2;
while (!quit) {
if (increment == 2)
nextIndex = text.indexOf("\r\n",index);
else
nextIndex = text.indexOf("\n",index);
if (nextIndex == -1) {
subText = text.substring(index);
quit = true;
}
else {
subText = text.substring(index,nextIndex);
}
textVector.addElement(subText);
AttributedString as = new AttributedString(subText);
for (int i = 0; i < subText.length(); i++) {
Element ele = document.getCharacterElement(textIndex++);
AttributeSet attrSet = ele.getAttributes();
Font f = document.getFont(attrSet);
as.addAttribute(TextAttribute.FONT, f, i, i + 1);
if (StyleConstants.isUnderline(attrSet)) {
as.addAttribute(TextAttribute.UNDERLINE,
TextAttribute.UNDERLINE_ON,
i,
i + 1);
}
}
attrStrVector.addElement(as);
index = nextIndex + increment;
textIndex++;
}
}
public Vector getTextVector() {
return this.textVector;
}
public Vector getAttrStrVector() {
return this.attrStrVector;
}
}
> printing text like this should work if the screen font works.
>
> I guess we need more info: your
>
> 1) O/S
> 2) Printer model
> 3) JDK version
> 4) Locale
> 5) and How exactly are you printing it (example code please,
> ideally a complete but small program)
>
>
> -phil.
>
> > Date: Tue, 28 Aug 2001 09:41:18 +0800
> > From: "Jason T. S. Chow" <[EMAIL PROTECTED]>
> > Subject: [JAVA2D] Printing styled Chinese text using TextLayout
> > To: [EMAIL PROTECTED]
> >
> > Hello all,
> >
> > I have written a program that prints styled Chinese text. I have used
> TextLayout to layout
> > the text stored in DefaultStyledDocument. The display on screen is ok, but
> when it prints,
> > the stroke is a mess. Is there any way to correctly print styled Chinese text?
> >
> > Thanks a lot.
> >
> > Jason
> > ----------------------------------------------
> > �w��ϥ�HongKong.com�l��t��
> > Thank you for using hongkong.com Email system
> >
> > ===========================================================================
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> > of the message "signoff JAVA2D-INTEREST". For general help, send email to
> > [EMAIL PROTECTED] and include in the body of the message "help".
>
>
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
---------------------------------------------
�w��ϥ�HongKong.com�l��t��
Thank you for using hongkong.com Email system
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".