Hi Paulo and other iText-ers,
derived Fonts carry along an AffineTransform (e.g. for fancy things like backwards
leaning font)
I fixed this almost by subclassing PdfGraphics2D. But there remains a discrepancy
between java.awt.Graphics2D and my com.lowagie.text.pdf.PdfGraphics in placing
Strings, which I really do not understand.
In the following two examples, uncommenting the global transform yield the same
picture on PDF as well as on screen, but with the global transform the two pictures
differ (obviously by the shearing amount is interpreted differently)... but I don't
see where this should/does/does not happen.
Thanks for your comments
Cheers
Erwin
----------------- derived font on screen
/*
* How does java.awt.Graphics2D deal with derived fonts, and where
* dose it place it?
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.geom.AffineTransform;
import java.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DerivedScreen extends JFrame {
public DerivedScreen()
{
super("DerivedScreen");
setSize(500,500); // SIZE of the Graphics
MyPane mine = new MyPane();
getContentPane().add(mine);
// ExitWindow exit = new ExitWindow();
// addWindowListener(exit);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
DerivedScreen screen = new DerivedScreen();
screen.show();
}
}
class MyPane extends JPanel
{
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
java.awt.Font myFont = new java.awt.Font("Arial", java.awt.Font.PLAIN,
12);
g2d.setFont(myFont);
g2d.drawString("This is arial plain",10,15);
AffineTransform trafo = AffineTransform.getShearInstance(0.5f,0f);
myFont = myFont.deriveFont(trafo);
g2d.transform(trafo); /////////////// leaving a global
///////////////
transform away,
///////////////
yields the same
///////////////
picture on pdf and on
/////////////// screen
g2d.setFont(myFont);
g2d.drawString("This is sheared arial plain",10,30);
g2d.drawLine(0,0,20,20);
}
}
// class ExitWindow extends WindowAdapter
// {
// public void windowClosing(WindowEvent e)
// {
// System.out.println(" terminating ");
// System.exit(0);
// }
// }
----------------- derived font in pdf
/*
* How does com.lowagie.text.pdf.PdfGraphics deal with derived fonts, and where
* dose it place it?
*
*/
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.geom.AffineTransform;
import java.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
class DerivedPdf {
public static void main(String[] args) {
System.out.println("Example: derived fonts");
// step 1: creation of a document-object
Document document = new Document();
Document.compress = false;
java.awt.Font myFont = new java.awt.Font("Arial", java.awt.Font.PLAIN,
12);
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream("d:/temp/fontderive.pdf"));
// step 3: we open the document
document.open();
PdfContentByte cb = writer.getDirectContent();
Graphics2D g2 = cb.createMyGraphics(500, 500); // SIZE of
// the
// Graphics
g2.setFont(myFont);
g2.drawString("This is arial plain",10,15);
AffineTransform trafo =
AffineTransform.getShearInstance(0.5f,0f);
myFont = myFont.deriveFont(trafo);
g2.transform(trafo); /////////////// leaving a global
///////////////
transform away,
///////////////
yields the same
///////////////
picture on pdf and on
/////////////// screen
g2.setFont(myFont);
g2.drawString("This is sheared arial plain",10,30);
g2.drawLine(0,0,20,20);
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
// step 5: we close the document
document.close();
}
}
----------------- my PdfGraphics subclassing PdfGraphics2D
/*
* $Id: PdfGraphics.java,v 1.1 2002/04/23 12:44:39 erwin.achermann Exp erwin.achermann
$
*
* $Date: 2002/04/23 12:44:39 $
*
*/
package com.lowagie.text.pdf;
import java.awt.geom.AffineTransform;
/**
* This class an extension of com.lowagie.text.pdf.PdfGraphics2D
* The only thing that we change is, that we want to retaint the
* transformAttibute of a font
*
*/
public class PdfGraphics extends PdfGraphics2D {
PdfGraphics(PdfContentByte cb, float width, float height, FontMapper fontMapper) {
super(cb,width,height,fontMapper);
fontTrafo = new AffineTransform();
}
AffineTransform fontTrafo;
public void setFont(java.awt.Font p_Font) {
fontTrafo = p_Font.getTransform();
// // fetch old font's transform attribute ...
// AffineTransform a = getFont().getTransform();
// // apply the old fonts trafo and the new fonts inverse trafo
// try {
// a.concatenate(p_Font.getTransform().createInverse());
// } catch (java.awt.geom.NoninvertibleTransformException e) {
// e.printStackTrace();
// } // end of try-catch
// transform(a);
// System.out.println(" set font: "+p_Font+" trafo: " + a);
// // finally set the new font
super.setFont(p_Font);
}
public void drawString(String s, float x, float y) {
// transform(fontTrafo);
// super.drawString(s,x,y);
// try {
// transform(fontTrafo.createInverse());
// } catch (java.awt.geom.NoninvertibleTransformException e) {
// e.printStackTrace();
// } // end of try-catch
AffineTransform sav = getTransform();
cb.saveState();
transform(fontTrafo);
super.drawString(s,x,y);
cb.restoreState();
setTransform(sav);
}
} // end class PdfGraphics
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions