Printing Chinese characters

Hello all,

I have been wriiten a program that prints styled Chinese characters,

using TextLayout class. The strokes are incorrect, some shorter, some longer.

The TextLayout firstly layout the text and attributes with specified width

and height (in case word wrapping is needed), then when printing , the Graphics

object pass into the textLayout.draw method to print the text. I found that

there is no problem when rendering on screen, and also print using drawString

method. Is anyone know how to solve this problem? thanks in advance. Here is

the standalone testing program.

Jason

import java.text.*;

import java.util.*;

import java.awt.*;

import java.awt.font.*;

import java.awt.event.*;

import java.awt.print.*;

import java.awt.image.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.text.*;

public class Test extends JFrame {

public static void main(String[] args) {

Locale.setDefault(Locale.CHINESE);

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() {

super(false);

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);

g.dispose();

}

public int print(Graphics _g, PageFormat pageFormat, int pageIndex) throws PrinterException {

if (pageIndex >= 1) {

_g.dispose();

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);

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,

RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

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);

g.dispose();

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;

}

}

[8241 byte] By [jasition] at [2007-9-26 7:12:58]
# 1
Amending the above question:I am using Window 98 Taiwan Edition, JDK1.3.1 international, and I have set locale to TW (Taiwan)Jason
jasition at 2007-7-1 16:57:17 > top of Java-index,Security,Cryptography...