/***********************************************************************
 * 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.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.PDFConstants;
import org.eclipse.birt.report.engine.layout.area.impl.AreaFactory;
import org.eclipse.birt.report.engine.layout.area.impl.TemplateArea;
import org.eclipse.birt.report.engine.layout.pdf.font.FontHandler;
import org.eclipse.birt.report.engine.layout.pdf.font.FontInfo;

public class TemplateLayout extends Layout
{

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

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

	}

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

	}

	public void layout( )
	{
		boolean isInline = parent instanceof IInlineStackingLayout;
		if ( isInline )
		{
			if ( parent instanceof LineLayout )
			{
				ContainerLayout inlineContainer = new InlineContainerLayout(
						context, parent, content );
				inlineContainer.initialize( );
				addTemplateArea( inlineContainer, true );
				inlineContainer.closeLayout( );
			}
			else
			{
				addTemplateArea( parent, true );
			}
		}
		else
		{
			assert ( parent instanceof BlockStackingLayout );
			if ( content != null )
			{
				IStyle contentStyle = content.getComputedStyle( );
				String align = contentStyle.getTextAlign( );
				parent.content.getComputedStyle( ).setTextAlign( align );
			}

			LineLayout line = new LineLayout( context, parent );
			line.initialize( );
			addTemplateArea( line, false );
			line.closeLayout( );
		}

	}
	
	protected void addTemplateArea( ContainerLayout parent, boolean isInline )
	{
		IAutoTextContent autoText = (IAutoTextContent) content;	
		TemplateArea templateArea = (TemplateArea) AreaFactory
				.createTemplateArea( autoText );
		
		// get max available width
		int maxWidth = parent.getCurrentMaxContentWidth( );
		templateArea.setAllocatedWidth( maxWidth - parent.currentContext.currentIP );
		int maxAvaWidth = templateArea.getWidth( );
		// get user defined width
		int width = getDimensionValue( autoText.getWidth( ), maxWidth );
		
		if ( width == 0 )
		{
			// the default content width
			int defaultWidth = getDimensionValue( templateArea.getStyle( ).getFontSize( ) ) * 4;
			width = Math.min( maxAvaWidth, defaultWidth );
		}
		else if ( width > maxAvaWidth )
		{
			width = maxAvaWidth;
		}
		templateArea.setWidth( width );
		context.setTotalPageTemplateWidth( templateArea.getContentWidth( ) );
		
		FontHandler handler = new FontHandler( context.getFontManager( ),
				autoText, false );
		FontInfo fontInfo = handler.getFontInfo( );
		
		int height = getDimensionValue( autoText.getHeight( ), 0 );
		templateArea.setContentHeight( Math.max( 
				( int )( fontInfo.getWordHeight( )* PDFConstants.LAYOUT_TO_PDF_RATIO ), height ) );
		
		templateArea.setBaseLine( fontInfo.getBaseline( ) + templateArea.getContentY( ) );	
		parent.addToRoot( templateArea );
	}
}

@@ -690,62 +690,8 @@
 nt;%0A
-import org.eclipse.birt.report.engine.content.IStyle;%0A
 impo
@@ -1012,16 +1012,84 @@
 ontInfo;
+%0Aimport org.eclipse.birt.report.engine.layout.pdf.util.PropertyUtil;
 %0A%0Apublic
@@ -1927,208 +1927,205 @@
 %0A%09%09%09
-if ( content != null )%0A%09%09%09%7B%0A%09%09%09%09IStyle contentStyle = content.getComputedStyle( );%0A%09%09%09%09String align = contentStyle.getTextAlign( );%0A%09%09%09%09
+boolean inlineElement = PropertyUtil.isInlineElement( content );%0A%09%09%09if(!inlineElement)%0A%09%09%09%7B%0A%09%09%09%09BlockTextLayout tLayout = new BlockTextLayout(context, 
 parent
-.
+, 
 content
-.getComputedStyle( ).setTextAlign( align );%0A%09%09%09%7D%0A%0A
+);%0A%09%09%09%09tLayout.initialize( );%0A%09
 %09%09%09L
@@ -2170,21 +2170,22 @@
 xt, 
-paren
+tLayou
 t );%0A%09%09%09
 line
@@ -2176,24 +2176,25 @@
 ayout );%0A%09%09%09
+%09
 line.initial
@@ -2201,16 +2201,17 @@
 ize( );%0A
+%09
 %09%09%09addTe
@@ -2237,16 +2237,17 @@
 alse );%0A
+%09
 %09%09%09line.
@@ -2258,24 +2258,56 @@
 eLayout( );%0A
+%09%09%09%09tLayout.closeLayout();%0A%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%7D%0A%09%0A%09p

Fix Bugzilla  Bug 251051 The first cell in the first line of each page does not align with others cells in the same column [1200]