/*
 * How does java.awt.Graphics2D deal with derived fonts, and where
 * doese it place it?
 */

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;


public class DerivedScreen extends JFrame  {

	public DerivedScreen()
	{
		super("DerivedScreen");
		setSize(500,500); // SIZE of the Graphics

		MyPane mine = new MyPane();
		getContentPane().add(mine);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// printing to pdffile
		try {
			System.out.println("Example: unicodes and derived fonts on graphics");
			// step 1: creation of a document-object
			Document document = new Document();
			Document.compress = false;

			// 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.createGraphics(500, 500); // SIZE of

			// the same operation is done down in the swing framework
			// to get it on screen
			mine.paintComponent(g2);

			BaseFont bfarial = BaseFont.createFont("c:\\winnt\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			com.lowagie.text.Font font = new com.lowagie.text.Font(bfarial, 12);

			String unicode = "on the other hand: 1\u20ac for a tone:\u266b and a smile:\u263a";
			document.add(new Paragraph(unicode, font));

			document.close();
		} catch(DocumentException de) {
			System.err.println(de.getMessage());
		} catch(IOException ioe) {
			System.err.println(ioe.getMessage());
		}
	}

    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);
		g2d.drawString("some unicodes: 1\u20ac for a tone:\u266b and a smile:\u263a",10,70);


		// other expriments, working well
		// AffineTransform shrink = AffineTransform.getScaleInstance(0.8,1f);
		// myFont = myFont.deriveFont(shrink);

		AffineTransform rot = AffineTransform.getRotateInstance(0.141);
		// myFont = myFont.deriveFont(rot);
		g2d.transform(rot);

		AffineTransform shear = AffineTransform.getShearInstance(0.5f,0f);
		myFont = myFont.deriveFont(shear);

		g2d.setFont(myFont);
		g2d.drawString("This is sheared and rotated 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);
//	}
// }
