Article View: borland.public.jbuilder.java.api
Article #1717Printing and font-width
From: H�kan �hlstr�m
Date: Mon, 15 Oct 2001 14:33
Date: Mon, 15 Oct 2001 14:33
125 lines
5311 bytes
5311 bytes
Hi
Is it possible to obtain the correct width of a string when printing. I have
tried with
different calculations and different settings, but I can't get it to work.
Below is a program
that tries to print two strings after each other. Depending on what font I
use the result
is disappointing. It prints 4 pages, and it is slow because it uses a lot of
fonts.
When I print with this program the "Serif" font and "Times New Roman" font
are not
printed the same. I thought that Serif was translated into Times Now Roman
on windows.
Can anyone tell me whats wrong?
Regards
H�kan �hlstr�m
OS win2000 sp2
jdk 1.3.1 b24
package print_test;
import java.awt.*;
import java.awt.print.*;
public class TestPrint implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) throws
PrinterException {
Graphics2D g2 = (Graphics2D) g;
switch(pageIndex) {
case 0: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
break;
case 1: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
break;
case 2: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
break;
case 3: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
break;
default: return NO_SUCH_PAGE;
}
int y = (int)pf.getImageableY();
int advance = 30;
y = writeLines(g2, pf, Font.PLAIN, y) + advance;
y = writeLines(g2, pf, Font.BOLD, y) + advance;
y = writeLines(g2, pf, Font.ITALIC, y) + advance;
y = writeLines(g2, pf, Font.BOLD + Font.ITALIC, y);
return PAGE_EXISTS;
}
private int writeLines(Graphics2D g2, PageFormat pf, int style, int y){
int advance = 10;
writeLn(g2, pf, "Arial", style, y += advance);
writeLn(g2, pf, "Arioso", style, y += advance);
writeLn(g2, pf, "Comic Sans MS", style, y += advance);
writeLn(g2, pf, "Conga", style, y += advance);
writeLn(g2, pf, "Courier", style, y += advance);
writeLn(g2, pf, "Garamond", style, y += advance);
writeLn(g2, pf, "Georgia", style, y += advance);
writeLn(g2, pf, "Helmet", style, y += advance);
writeLn(g2, pf, "Lucida Bright", style, y += advance);
writeLn(g2, pf, "Lucida Sans", style, y += advance);
writeLn(g2, pf, "Lucida Sans Typewriter", style, y += advance);
writeLn(g2, pf, "Microsoft Sans Serif", style, y += advance);
writeLn(g2, pf, "Palatino Linotype", style, y += advance);
writeLn(g2, pf, "Tahoma", style, y += advance);
writeLn(g2, pf, "Times New Roman", style, y += advance);
writeLn(g2, pf, "Serif", style, y += advance);
return y;
}
private void writeLn(Graphics2D g2, PageFormat pf, String fontName, int
style, int y){
String s1 = "KKKKKKKKKKKKKKKKKKKKKKKKKKKKK";
String s2 = "kkkkkkkkk";
Font f = new Font(fontName, style, 9);
g2.setFont(f);
g2.drawString(s1, (int)pf.getImageableX(), y);
// int widthOfText = g2.getFontMetrics().stringWidth(s1);
// int widthOfText = (int)g2.getFontMetrics().getStringBounds(s1,
g2).getWidth();
int widthOfText = (int)g2.getFont().getStringBounds(s1,
g2.getFontRenderContext()).getWidth();
g2.drawString(s2, (int)pf.getImageableX() + widthOfText, y);
g2.drawString(fontName, 400, y);
}
public static void main(String[] args){
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = new PageFormat();
Paper p = new Paper();
p.setSize(21 * 72 / 2.54, 29.7 * 72 / 2.54);
p.setImageableArea(28, 28, 530, 765);
pf.setPaper(p);
pf.setOrientation(PageFormat.PORTRAIT);
job.setPrintable(new TestPrint(), pf);
if (job.printDialog()){
try {
job.print();
} catch(PrinterException e){
}
}
System.exit(0);
}
}
Technical Details
Message-ID:
<3bcaf3cb_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!feeder.qis.net!news.maxwell.syr.edu!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1089d0,4a74dc8ca421578f,start X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-10-15 07:41:55 PST From: "H�kan �hlstr�m" <hakan.ahlstrom@liber.se> Newsgroups: borland.public.jbuilder.java.api Subject: Printing and font-width Date: Mon, 15 Oct 2001 16:33:42 +0200 Lines: 127 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 NNTP-Posting-Host: 193.14.71.253 Message-ID: <3bcaf3cb_2@dnews> X-Trace: dnews 1003156427 193.14.71.253 (15 Oct 2001 07:33:47 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!feeder.qis.net!news.maxwell.syr.edu!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnews