/*******************************************************************************
 * Copyright (c) 2007 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.emitter.ppt.device;

import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.eclipse.birt.report.engine.emitter.ppt.PPTWriter;
import org.eclipse.birt.report.engine.layout.TextStyle;
import org.eclipse.birt.report.engine.layout.emitter.AbstractPage;

public class PPTPage extends AbstractPage
{

	private PPTWriter writer;
	private boolean isDisposed;

	public PPTPage( int pageWidth, int pageHeight, Color backgroundColor,
			PPTWriter writer )
	{
		super( pageWidth, pageHeight );
		writer.newPage( this.pageWidth, this.pageHeight, backgroundColor );
		this.writer = writer;
		this.isDisposed = false;
	}

	public void clipRestore( )
	{
	}

	public void clipSave( )
	{
	}

	public void dispose( )
	{
		if ( !isDisposed )
		{
			writer.endPage( );
			isDisposed = true;
		}
	}

	protected void clip( float startX, float startY, float width, float height )
	{
	}
	
	protected void drawBackgroundColor( Color color, float x, float y,
			float width, float height )
	{
		writer.drawBackgroundColor( color, x, y, width, height );
	}

	protected void drawBackgroundImage( float x, float y, float width,
			float height, String repeat, String imageUrl, float absPosX,
			float absPosY ) throws IOException
	{
		writer.drawBackgroundImage( imageUrl, x, y, width, height, absPosX,
				absPosY, repeat );
	}

	protected void drawImage( byte[] imageData, String extension, float imageX,
			float imageY, float height, float width, String helpText )
			throws Exception
	{
		writer.drawImage( imageData, extension, imageX, imageY, height, width,
				helpText );
	}

	protected void drawImage( String uri, String extension, float imageX,
			float imageY, float height, float width, String helpText )
			throws Exception
	{
		if ( uri == null )
		{
			return;
		}
		byte[] imageData = null;
		InputStream imageStream = new URL( uri ).openStream( );
		imageData = new byte[imageStream.available( )];
		imageStream.read( imageData );

		drawImage( imageData, extension, imageX, imageY, height, width,
				helpText );
	}

	protected void drawLine( float startX, float startY, float endX,
			float endY, float width, Color color, String lineStyle )
	{
		writer.drawLine( startX, startY, endX, endY, width, color, lineStyle );
	}

	protected void drawText( String text, float textX, float textY, float baseline,
			float width, float height, TextStyle textStyle )
	{
		// width of text is enlarged by 1 point because in ppt the text will be
		// automatically wrapped if the width of textbox equals to the width of
		// text exactly.
		writer.drawText( text, textX, textY, width + 1f, height, textStyle
				.getFontInfo( ), textStyle.getColor( ) );
	}
}

@@ -604,16 +604,54 @@
 .Color;%0A
+import java.io.ByteArrayOutputStream;%0A
 import j
@@ -2410,35 +2410,8 @@
 %09%09%7D%0A
-%09%09byte%5B%5D imageData = null;%0A
 %09%09In
@@ -2471,57 +2471,105 @@
 %0A%09%09i
-mageData
+nt data;%0A%09%09ByteArrayOutputStream byteArrayOut
  = new 
-byte%5BimageStream.available
+ByteArrayOutputStream
 ( )
-%5D
 ;%0A%09%09
+while ( ( data = 
 imag
@@ -2586,21 +2586,58 @@
 ad( 
-imageD
+) ) != -1 )%0A%09%09%7B%0A%09%09%09byteArrayOut.write( d
 ata );%0A
+%09%09%7D
 %0A%09%09d
@@ -2642,33 +2642,51 @@
 %09drawImage( 
-imageData
+byteArrayOut.toByteArray( )
 , extension,
@@ -2693,33 +2693,37 @@
  imageX, imageY,
- 
+%0A%09%09%09%09
 height, width,%0A%09
@@ -2712,37 +2712,33 @@
 %09%09height, width,
-%0A%09%09%09%09
+ 
 helpText );%0A%09%7D%0A%0A

Fix SCR bug 95805: Image shown as X sign when exporting report into PPT format in SV.