/***********************************************************************
 * Copyright (c) 2008 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Actuate Corporation - initial API and implementation
 ***********************************************************************/

package org.eclipse.birt.report.engine.layout.pdf.emitter;

import org.eclipse.birt.core.format.NumberFormatter;
import org.eclipse.birt.report.engine.content.Dimension;
import org.eclipse.birt.report.engine.content.IAutoTextContent;
import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IStyle;
import org.eclipse.birt.report.engine.layout.area.impl.AbstractArea;
import org.eclipse.birt.report.engine.layout.area.impl.AreaFactory;
import org.eclipse.birt.report.engine.layout.area.impl.ContainerArea;
import org.eclipse.birt.report.engine.layout.area.impl.TemplateArea;


public class TemplateLayout extends Layout
{

	public TemplateLayout( LayoutEngineContext context, ContainerLayout parent,
			IContent content )
	{
		super( context, parent, content );
		handleAutoText( (IAutoTextContent) content );
	}

	protected void closeLayout( )
	{
		// TODO Auto-generated method stub

	}

	protected void initialize( )
	{
		// TODO Auto-generated method stub

	}

	public void layout( )
	{
		IAutoTextContent autoText = (IAutoTextContent) content;
		ContainerArea templateContainer = (ContainerArea) AreaFactory.createInlineContainer(
				autoText, true, true );
		IStyle areaStyle = templateContainer.getStyle( );
		int maxWidth = parent.getCurrentMaxContentWidth( );

		int width = getDimensionValue( autoText.getWidth( ), maxWidth );
		templateContainer.setAllocatedWidth( maxWidth - parent.getCurrentIP( ) );
		int minContentWidth = getDimensionValue( areaStyle.getFontSize( ) ) * 4;
		int maxContentWidth = templateContainer.getWidth( );
		int preWidth = 0;
		if ( width >= maxContentWidth )
		{
			preWidth = Math.max( maxContentWidth, minContentWidth );
		}
		else
		{
			preWidth = Math.max( minContentWidth, width );
		}

		templateContainer.setWidth( preWidth );

		int height = getDimensionValue( autoText.getHeight( ), maxWidth );
		templateContainer.setContentHeight( Math.max(
				(int) ( getDimensionValue( areaStyle.getFontSize( ) ) * 1.35 ),
				height ) );

		Dimension templateDimension = new Dimension( );
		templateDimension.setDimension( templateContainer.getContentWidth( ),
				templateContainer.getContentHeight( ) );
		AbstractArea templateArea = createTemplateArea( autoText,
				templateDimension );
		templateContainer.addChild( templateArea );

		templateArea
				.setPosition(
						templateContainer.getContentX( ),
						templateContainer.getContentY( ) );
		parent.addArea( templateContainer );
	}
	
	protected void handleAutoText(IAutoTextContent autoText )
	{
		if ( IAutoTextContent.TOTAL_PAGE == autoText.getType( ) )
		{
			context.addUnresolvedContent( autoText );
		}
		if ( IAutoTextContent.PAGE_NUMBER == autoText.getType( ) )
		{

			String originalPageNumber = autoText.getText( );
			NumberFormatter nf = new NumberFormatter( );
			String patternStr = autoText.getComputedStyle( ).getNumberFormat( );
			nf.applyPattern( patternStr );
			autoText.setText( nf
					.format( Integer.parseInt( originalPageNumber ) ) );
		}
	}

	/**
	 * create template area by autoText content
	 * 
	 * @param autoText
	 *            the autoText content
	 * @param contentDimension
	 *            content dimension
	 * @return
	 */
	private TemplateArea createTemplateArea( IAutoTextContent autoText,
			Dimension contentDimension )
	{
		TemplateArea templateArea = (TemplateArea) AreaFactory
				.createTemplateArea( autoText );
		templateArea.setWidth( contentDimension.getWidth( ) );
		templateArea.setHeight( contentDimension.getHeight( ) );
		return templateArea;
	}

}

@@ -850,16 +850,75 @@
 IStyle;%0A
+import org.eclipse.birt.report.engine.layout.PDFConstants;%0A
 import o
@@ -1185,16 +1185,146 @@
 teArea;%0A
+import org.eclipse.birt.report.engine.layout.pdf.font.FontHandler;%0Aimport org.eclipse.birt.report.engine.layout.pdf.font.FontInfo;
 %0A%0Apublic
@@ -1789,16 +1789,155 @@
 ontent;%0A
+%0A%09%09FontHandler handler = new FontHandler( context.getFontManager( ),%0A%09%09%09%09autoText, false );%0A%09%09FontInfo fontInfo = handler.getFontInfo( );%0A%0A
 %09%09Contai
@@ -1991,16 +1991,21 @@
 aFactory
+%0A%09%09%09%09
 .createI
@@ -2019,21 +2019,17 @@
 ntainer(
-%0A%09%09%09%09
+ 
 autoText
@@ -2650,24 +2650,100 @@
 preWidth );%0A
+%09%09templateContainer.setBaseLine( handler.getFontInfo( ).getBaseline( ) );%0A%09%09
 %0A%09%09int heigh
@@ -2851,81 +2851,86 @@
 max(
+ 
 %0A%09%09%09%09(
+ 
 int
-) ( getDimensionValue( areaStyle.getFontSize( ) ) * 1.35 ),%0A%09%09%09%09
+ )( fontInfo.getWordHeight( )* PDFConstants.LAYOUT_TO_PDF_RATIO ), 
 heig
@@ -3236,17 +3236,16 @@
 Area );%0A
-%0A
 %09%09templa
@@ -3250,21 +3250,86 @@
 lateArea
-%0A%09%09%09%09
+.setBaseLine( handler.getFontInfo( ).getBaseline( ) );%0A%0A%09%09templateArea
 .setPosi
@@ -3333,23 +3333,17 @@
 osition(
-%0A%09%09%09%09%09%09
+ 
 template
@@ -3368,18 +3368,16 @@
 ntX( ),%0A
-%09%09
 %09%09%09%09temp
@@ -3450,17 +3450,16 @@
 r );%0A%09%7D%0A
-%09
 %0A%09protec
@@ -3482,16 +3482,17 @@
 utoText(
+ 
 IAutoTex

optimize the autoText vertical alignment.