package com.newgen.SampleAFP;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.apache.fop.afp.AFPLineDataInfo;
import org.apache.fop.afp.AFPTextDataInfo;
import org.apache.fop.afp.Factory;
import org.apache.fop.afp.fonts.CharacterSet;
import org.apache.fop.afp.fonts.CharacterSetBuilder;
import org.apache.fop.afp.ioca.ImageCellPosition;
import org.apache.fop.afp.ioca.ImageInputDescriptor;
import org.apache.fop.afp.ioca.ImageOutputControl;
import org.apache.fop.afp.ioca.ImageRasterData;
import org.apache.fop.afp.ioca.ImageRasterPattern;
import org.apache.fop.afp.modca.Document;
import org.apache.fop.afp.modca.IMImageObject;
import org.apache.fop.afp.modca.PageObject;
import org.apache.fop.afp.modca.PresentationTextObject;
import org.apache.fop.afp.ptoca.PtocaBuilder;
import org.apache.fop.afp.ptoca.PtocaProducer;
import org.apache.fop.fonts.Typeface;
import org.xml.sax.SAXException;

public class SampleAFP
{
	static Document doc;
	static PageObject page; 
	
	public static void main(String arg[]) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, SAXException
	{
		Factory factory=new Factory();
		String docName="Doc";
		String pgName="Page";
		int pgwidth=150;
		int pgheight=150;
		int pgrotation=0;
		int pgheightRes=10;
		int pgwidthRes=10;
		
		
		doc=new Document(factory, docName);
		page=new PageObject(factory, pgName, pgwidth, pgheight, pgrotation, pgwidthRes, pgheightRes);
		
		
		Color col=new Color(90, 35, 90);
		
		AFPLineDataInfo lineDataInfo=new AFPLineDataInfo();
		lineDataInfo.setColor(col);
		lineDataInfo.setRotation(0);
		lineDataInfo.setThickness(2);
		lineDataInfo.setX1(3);
		lineDataInfo.setX2(90);
		lineDataInfo.setY1(10);
		lineDataInfo.setY2(10);
		page.createLine(lineDataInfo);
		
		PresentationTextObject pto=new PresentationTextObject("Text");
		final AFPTextDataInfo textDataInfo=new AFPTextDataInfo();
		textDataInfo.setColor(col);
		textDataInfo.setEncoding("Cp500");
		textDataInfo.setRotation(0);
		textDataInfo.setString("Hello World");
		textDataInfo.setX(15);
		textDataInfo.setY(15);
		
		pto.createTextData(textDataInfo);
		
		CharacterSetBuilder csb = CharacterSetBuilder.getSingleByteInstance();
        final CharacterSet charSet = csb.build("C0N200Z0", "T1V10500", "Cp500", Class.forName("org.apache.fop.fonts.base14.Helvetica").asSubclass(Typeface.class).newInstance(), null);
		
		PtocaProducer producer = new PtocaProducer() {
			
			@Override
			public void produce(PtocaBuilder builder) throws IOException {
				builder.setTextOrientation(textDataInfo.getRotation());
			    builder.absoluteMoveBaseline(textDataInfo.getY());
			    builder.absoluteMoveInline(textDataInfo.getX());
			    StringBuffer sb = new StringBuffer();
			    int l = textDataInfo.getString().length();
			    for (int i = 0; i < l; i++) {
	                char orgChar = textDataInfo.getString().charAt(i);
	                sb.append(orgChar);
			    }
			    flushText(builder, sb, charSet);
			}
			 private void flushText(PtocaBuilder builder, StringBuffer sb, final CharacterSet charSet) throws IOException {
		            if (sb.length() > 0) {
		                builder.addTransparentData(charSet.encodeChars(sb));
		                sb.setLength(0);
		            }
		        }
		};
		
		
	    page.createText(producer);
	    
/*	    float shade = (float) ((102 * 0.3) + (204 * 0.59) + (255 * 0.11));
	    int grayscale = Math.round((shade / 255) * 16);*/

        IMImageObject imImageObject = factory.createIMImageObject();
        ImageInputDescriptor imageInputDescriptor=new ImageInputDescriptor();
        
        imageInputDescriptor.setResolution(50);
		imImageObject.setImageInputDescriptor(imageInputDescriptor);
		
		ImageOutputControl imageOutputControl=new ImageOutputControl(0, 0);
		imageOutputControl.setOrientation(0);
		imImageObject.setImageOutputControl(imageOutputControl);
	    
		
	    ImageCellPosition imageCellPosition=new ImageCellPosition(100,100);
	    imageCellPosition.setXFillSize(20);
        imageCellPosition.setYFillSize(20);
        imageCellPosition.setXSize(50);
        imageCellPosition.setYSize(50);
        imImageObject.setImageCellPosition(imageCellPosition);
        
        File imgPath = new File("D:\\WorkSpace\\SampleAFP\\img\\newgen.jpg");
        BufferedImage bufferedImage = ImageIO.read(imgPath);

        // get DataBufferBytes from Raster
        WritableRaster raster = bufferedImage .getRaster();
        DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

        byte[] rasterData =data.getData(); 
        ImageRasterData imageRasterData=factory.createImageRasterData(rasterData);
		imImageObject.setImageRasterData(imageRasterData);
	  
	    
	   
       

        //defining this as a resource
       

      
	    page.addObject(imImageObject);
	    page.endPage();
	    
	    doc.addPage(page);
	    doc.endDocument();
		
	    OutputStream out = new FileOutputStream("D:\\WorkSpace\\SampleAFP\\AFPOutput\\SampleAFP_using_FOP_2.1.afp");
	    
		doc.writeToStream(out);
		System.out.println("End using FOP 2.1");
	}
}
