package examples.writing;

import java.io.File;

import java.awt.Color;

import de.vc.jfo.Document;
import de.vc.jfo.attributes.FontSupport;
import de.vc.jfo.attributes.Values;
import de.vc.jfo.flow.Flow;
import de.vc.jfo.pagination.SimplePageMaster;
import de.vc.jfo.renderer.OutputFormat;
import de.vc.jfo.renderer.Renderer;
import de.vc.jfo.renderer.RendererFactory;
import de.vc.jfo.writing.CellStyle;
import de.vc.jfo.writing.FlowWriter;
import de.vc.util.Measure;

/**
 * This example shows you how to use the FlowWriter of jFO.
 * The FlowWriter supports you writing text, paragraphs and tables.
 */

public class FlowWriting {

  private final static String FO_FILE  = "docs-out/writing/FlowWriting.fo";
  private final static String PDF_FILE  = "docs-out/writing/FlowWriting.pdf";


  public static void main(String[] args) throws Exception{

    // Create a simple document in DIN A4
    Document doc = new Document( SimplePageMaster.DIN_A4 );

    // Get the flow-Object of first page sequence
    Flow flow = doc.getFirstPageSequence().getFlow();

    // Now let's create a writer
    FlowWriter writer = new FlowWriter( flow );

    // Set font name helvetica
    writer.getFlowStyle().setFontFamily("Helvetica");

    // New paragraph with centered alignment
    writer.newParagraph( Values.ALIGNMENT_CENTER );
    writer.write("This is an example for the FlowWriter", 16, FontSupport.WEIGHT_BOLD );

    // New
    writer.newParagraph( 24, Values.ALIGNMENT_LEFT );
    writer.write("A simple table:", 12, FontSupport.WEIGHT_NORMAL);

    // Before starting a table we have to define the style
    // for the first table cell
    writer.setCellStyle( new CellStyle(2, Color.green) );

    // Now we can start the table with a header
    writer.startTable(
        // Column widths
        new Measure[]{new Measure("1cm"),new Measure("5cm"),new Measure("5cm")},
        // Column headers
        new String[]{"No.","Col 1", "Col 2"},
        // Border size of header cells
        4,
        //Border color of header cells.
        Color.red);

    for (int i=1;i<10;i++){
      writer.newParagraph(Values.ALIGNMENT_CENTER);
      writer.write(i+".");
      writer.nextTableCell();
      writer.newParagraph(Values.ALIGNMENT_LEFT);
      writer.write("something", FontSupport.STYLE_ITALIC);
      writer.nextTableCell();
      writer.write("something other", FontSupport.STYLE_NORMAL);
      writer.nextTableRow();
    }
    writer.endTable();

    writer.write("Some text after table...");


    // Now we can store XSL-FO
    doc.writeTo( new File(FO_FILE) );

    // Or render to any other format
    OutputFormat format = OutputFormat.PDF;
    Renderer r = RendererFactory.instance().getRenderer( format );
    if (r==null)
      // If FOP is not available we cannot render... :-(
      System.err.println("No renderer found for "+format+". Is FOP in classpath?");
    else
      // Render the FO-document into a PDF-file
      doc.render( r, format, new File(PDF_FILE) );
  }

}
