From 16c1a52a5a83cc9d1f2fd0192c27e6171a607f29 Mon Sep 17 00:00:00 2001 From: sguan Date: Tue, 10 Jun 2014 17:49:00 -0700 Subject: [PATCH] Implement the initial tableWriter for pptx --- .../engine/emitter/pptx/PPTXRender.java | 31 +- .../engine/emitter/pptx/TableWriter.java | 310 +++++++++++++++++- .../engine/emitter/pptx/writer/Slide.java | 40 +-- .../layout/emitter/PageDeviceRender.java | 15 +- 4 files changed, 359 insertions(+), 37 deletions(-) diff --git a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/PPTXRender.java b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/PPTXRender.java index 1cb7fb9e070..047196377d9 100644 --- a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/PPTXRender.java +++ b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/PPTXRender.java @@ -42,7 +42,7 @@ public class PPTXRender extends PageDeviceRender { private OutputStream out = null; - private String tempFileDir; + private final String tempFileDir; /** The default output PPT file name. */ public static final String REPORT_FILE = "Report.pptx"; //$NON-NLS-1$ @@ -54,6 +54,7 @@ public PPTXRender(IEmitterServices services) throws EngineException { tempFileDir = services.getReportEngine().getConfig().getTempDir(); } + @Override public IPageDevice createPageDevice(String title, String author, String subject, String description, IReportContext context, IReportContent report) throws Exception { @@ -83,6 +84,7 @@ private CompressionMode getCompressionMode(RenderOption renderOption) { * * @return the output format */ + @Override public String getOutputFormat() { return "pptx"; } @@ -107,6 +109,7 @@ public void initialize(IEmitterServices services) throws EngineException { this.out = EmitterUtil.getOuputStream(services, REPORT_FILE); } + @Override public void visitImage(IImageArea imageArea) { PPTXPage page = (PPTXPage) pageGraphic; page.setLink(PPTUtil.getHyperlink(imageArea, services, reportRunnable, @@ -124,6 +127,7 @@ public void visitText(ITextArea textArea) { page.setLink(null); } + @Override protected void drawTextAt(ITextArea text, int x, int y, int width, int height, TextStyle textStyle) { pageGraphic.drawText(text.getLogicalOrderText(), x, y, width, height, @@ -171,4 +175,29 @@ public Slide getSlide( ) { return ((PPTXPage) pageGraphic).getSlide( ); } + + public int getCurrentX() + { + return currentX; + } + + public int getCurrentY() + { + return currentY; + } + + public void setCurrentX( int x ) + { + currentX = x; + } + + public void setCurrentY( int y ) + { + currentY = y; + } + + public float getScale() + { + return scale; + } } diff --git a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/TableWriter.java b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/TableWriter.java index a244d12ce69..39bfd3f10bf 100644 --- a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/TableWriter.java +++ b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/TableWriter.java @@ -1,20 +1,326 @@ package org.eclipse.birt.report.engine.emitter.pptx; +import java.awt.Color; +import java.util.Iterator; +import java.util.Stack; + +import org.eclipse.birt.report.engine.emitter.pptx.writer.Slide; +import org.eclipse.birt.report.engine.nLayout.area.IArea; +import org.eclipse.birt.report.engine.nLayout.area.IContainerArea; +import org.eclipse.birt.report.engine.nLayout.area.impl.CellArea; +import org.eclipse.birt.report.engine.nLayout.area.impl.RowArea; import org.eclipse.birt.report.engine.nLayout.area.impl.TableArea; +import org.eclipse.birt.report.engine.nLayout.area.style.BackgroundImageInfo; +import org.eclipse.birt.report.engine.nLayout.area.style.BorderInfo; +import org.eclipse.birt.report.engine.nLayout.area.style.BoxStyle; +import org.eclipse.birt.report.engine.nLayout.area.style.DiagonalInfo; +import org.eclipse.birt.report.engine.ooxml.writer.OOXmlWriter; public class TableWriter { - private PPTXRender render; + private final Slide slide; + private int currentX; + private int currentY; + protected Stack rowStyleStack = new Stack(); + private final float scale; + private final PPTXRender render; + private final PPTXPage pageGraphic; + protected OOXmlWriter writer; public TableWriter( PPTXRender render ) { this.render = render; + slide = render.getSlide(); + writer = slide.getWriter(); + pageGraphic = render.getGraphic(); + scale = render.getScale(); + currentX = render.getCurrentX(); + currentY = render.getCurrentY(); } public void outputTable( TableArea table ) { - render.visitTable( table ); + slide.setTable(this); //queue table + drawTable( table ); + } + + public void drawTable(IContainerArea tableElement) + { + startContainer( tableElement ); + visitChildren( tableElement ); + endContainer( tableElement ); + } + + /** + * The container may be a TableArea, RowArea, etc. Or just the + * border of textArea/imageArea. This method draws the border and background + * of the given container. + * + * @param TableArea + * the TableArea specified from layout + */ + protected void startContainer( IContainerArea tableElement ) + { + + if ( tableElement.needClip( ) ) + { + render.startClip( tableElement ); + + } + if ( tableElement instanceof RowArea ) + { + rowStyleStack.push( tableElement.getBoxStyle( ) ); + } + else if ( tableElement instanceof CellArea ) + { + drawCell( (CellArea) tableElement ); + } + + else + { + + render.drawContainer( tableElement ); + } + currentX += getX( tableElement ); + currentY += getY( tableElement ); + } + + protected void visitChildren( IContainerArea container ) + { + Iterator iter = container.getChildren( ); + while ( iter.hasNext( ) ) + { + IArea child = iter.next( ); + //if table, row or cell visit inside else out. + if( child instanceof CellArea || child instanceof RowArea ) + { + drawTable( (IContainerArea) child ); + } + else{ + updateRenderXY(); + child.accept( render ); + } + + } + } + + /** + * This method will be invoked while a containerArea ends. + * + * @param container + * the ContainerArea specified from layout + */ + protected void endContainer( IContainerArea container ) + { + currentX -= getX( container ); + currentY -= getY( container ); + + + if(container instanceof RowArea) + { + rowStyleStack.pop( ); + } + if ( container instanceof TableArea ) + { + updateRenderXY(); + render.drawTableBorder( (TableArea) container ); + } + else if ( !( container instanceof CellArea ) ) + { + render.setCurrentX(currentX); + render.setCurrentY(currentY); + org.eclipse.birt.report.engine.layout.emitter.BorderInfo[] borders = render.cacheBorderInfo( container ); + render.drawBorder( borders ); + } + if ( container.needClip( ) ) + { + pageGraphic.endClip( ); + } + + } + + protected void drawCell( CellArea container ) + { + drawCellDiagonal( container ); + Color rowbc = null; + BackgroundImageInfo rowbi = null; + BoxStyle rowStyle = null; + // get the style of the row + if ( rowStyleStack.size( ) > 0 ) + { + rowStyle = rowStyleStack.peek( ); + if(rowStyle!=null) + { + rowbc = rowStyle.getBackgroundColor( ); + rowbi = rowStyle.getBackgroundImage( ); + } + } + + BoxStyle style = container.getBoxStyle( ); + Color bc = style.getBackgroundColor( ); + BackgroundImageInfo bi = style.getBackgroundImage( ); + //String imageUrl = EmitterUtil.getBackgroundImageUrl( style,reportDesign ); + + if ( rowbc != null || rowbi != null || bc != null + || bi != null ) + { + // the container's start position (the left top corner of the + // container) + int startX = currentX + getX( container ); + int startY = currentY + getY( container ); + + // the dimension of the container + int width = getWidth( container ); + int height = getHeight( container ); + + if ( rowbc != null ) + { + pageGraphic.drawBackgroundColor( rowbc, startX, startY, width, + height ); + } + if ( rowbi != null ) + { + render.drawBackgroundImage( rowbi, startX, startY, + width, height ); + } + if ( bc != null ) + { + // Draws background color for the container, if the background + // color is NOT set, draws nothing. + pageGraphic.drawBackgroundColor( bc, startX, startY, width, + height ); + } + if ( bi != null ) + { + // Draws background image for the container. if the background + // image is NOT set, draws nothing. + render.drawBackgroundImage( bi, startX, startY, width, height ); + } + } + + } + + protected void drawCellDiagonal( CellArea cell ) + { + DiagonalInfo diagonalInfo = cell.getDiagonalInfo( ); + if ( diagonalInfo != null ) + { + int startX = currentX + getX( cell ); + int startY = currentY + getY( cell ); + + // the dimension of the container + int width = getWidth( cell ); + int height = getHeight( cell ); + int dw = diagonalInfo.getDiagonalWidth( ); + int ds = diagonalInfo.getDiagonalStyle( ); + // support double style, use solid style instead. + if ( ds == DiagonalInfo.BORDER_STYLE_DOUBLE ) + { + ds = DiagonalInfo.BORDER_STYLE_SOLID; + } + switch ( diagonalInfo.getDiagonalNumber( ) ) + { + case 2 : + drawLine( startX + width/2 , startY , startX + + width, startY + height - dw/2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + drawLine( startX, startY + height/2 , startX + + width, startY + height - dw/2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + break; + case 1 : + drawLine( startX, startY + dw / 2, startX + + width, startY + height - dw / 2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + break; + + default : + drawLine( startX, startY + dw / 2, startX + + width, startY + height - dw / 2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + drawLine( startX + width/2 , startY + dw/2, startX + + width, startY + height - dw/2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + drawLine( startX, startY + height/2 , startX + + width, startY + height - dw/2, + getScaledValue( dw ), diagonalInfo.getDiagonalColor( ), ds ); + break; + } + + } + } + + /** + * @param startX + * @param startY + * @param endX + * @param endY + * @param width + * @param color + * @param lineStyle + * + * pre: all are set in EMU units + */ + public void drawLine( int startX, int startY, int endX, int endY, + int width, Color color, int lineStyle ) + { + if ( color == null || width == 0f + || lineStyle==BorderInfo.BORDER_STYLE_NONE ) + { + return; + } + writer.openTag( "p:cxnSp" ); + writer.openTag( "p:nvCxnSpPr" ); + writer.openTag( "p:cNvPr" ); + int shapeId = slide.nextShapeId( ); + writer.attribute( "id", shapeId ); + writer.attribute( "name", "Line " + shapeId ); + writer.closeTag( "p:cNvPr" ); + writer.openTag( "p:cNvCxnSpPr" ); + writer.closeTag( "p:cNvCxnSpPr" ); + writer.openTag( "p:nvPr" ); + writer.closeTag( "p:nvPr" ); + writer.closeTag( "p:nvCxnSpPr" ); + writer.openTag( "p:spPr" ); + slide.setPosition( startX, startY, endX - startX, endY - startY ); + writer.openTag( "a:prstGeom" ); + writer.attribute( "prst", "line" ); + writer.closeTag( "a:prstGeom" ); + slide.setProperty( color, width, lineStyle ); + writer.closeTag( "p:spPr" ); + writer.closeTag( "p:cxnSp" ); + } + + private int getY(IContainerArea area) + { + return getScaledValue( area.getY() ); + } + + private int getX(IContainerArea area) + { + return getScaledValue( area.getX() ); } + + private int getHeight(CellArea area) + { + return getScaledValue( area.getHeight() ); + } + + private int getWidth(CellArea area) + { + return getScaledValue( area.getWidth()); + } + + protected int getScaledValue( int value ) + { + return (int) ( value * scale ); + } + + protected void updateRenderXY(){ + render.setCurrentX( currentX ); + render.setCurrentY( currentY ); + } + } diff --git a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/writer/Slide.java b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/writer/Slide.java index cb116f63b35..eb08d2c8c61 100644 --- a/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/writer/Slide.java +++ b/engine/org.eclipse.birt.report.engine.emitter.pptx/src/org/eclipse/birt/report/engine/emitter/pptx/writer/Slide.java @@ -20,6 +20,7 @@ import org.eclipse.birt.report.engine.emitter.EmitterUtil; import org.eclipse.birt.report.engine.emitter.ppt.util.PPTUtil.HyperlinkDef; +import org.eclipse.birt.report.engine.emitter.pptx.TableWriter; import org.eclipse.birt.report.engine.emitter.pptx.util.PPTXUtil; import org.eclipse.birt.report.engine.layout.emitter.Image; import org.eclipse.birt.report.engine.layout.emitter.util.BackgroundImageLayout; @@ -50,6 +51,8 @@ public class Slide extends Component private boolean isClosed = false; private ImageManager imageManager; + private TableWriter table = null; + public Slide( Presentation presentation, int slideIndex ) throws IOException { @@ -105,31 +108,8 @@ private void drawSlideBackgroundColor( Color color ) public void drawLine( int startX, int startY, int endX, int endY, int width, Color color, int lineStyle ) { - if ( color == null || width == 0f - || lineStyle==BorderInfo.BORDER_STYLE_NONE ) - { - return; - } - writer.openTag( "p:cxnSp" ); - writer.openTag( "p:nvCxnSpPr" ); - writer.openTag( "p:cNvPr" ); - int shapeId = nextShapeId( ); - writer.attribute( "id", shapeId ); - writer.attribute( "name", "Line " + shapeId ); - writer.closeTag( "p:cNvPr" ); - writer.openTag( "p:cNvCxnSpPr" ); - writer.closeTag( "p:cNvCxnSpPr" ); - writer.openTag( "p:nvPr" ); - writer.closeTag( "p:nvPr" ); - writer.closeTag( "p:nvCxnSpPr" ); - writer.openTag( "p:spPr" ); - setPosition( startX, startY, endX - startX, endY - startY ); - writer.openTag( "a:prstGeom" ); - writer.attribute( "prst", "line" ); - writer.closeTag( "a:prstGeom" ); - setProperty( color, width, lineStyle ); - writer.closeTag( "p:spPr" ); - writer.closeTag( "p:cxnSp" ); + table.drawLine(startX, startY, endX, endY, width, color, lineStyle); + } public void drawText( String text, int textX, int textY, int width, @@ -588,7 +568,7 @@ private void setColor( Color color ) } } - private void setPosition( int startX, int startY, int width, int height ) + public void setPosition( int startX, int startY, int width, int height ) { writer.openTag( "a:xfrm" ); writer.openTag( "a:off" ); @@ -602,7 +582,7 @@ private void setPosition( int startX, int startY, int width, int height ) writer.closeTag( "a:xfrm" ); } - private void setProperty( Color color, int width, int style ) + public void setProperty( Color color, int width, int style ) { writer.openTag( "a:ln" ); writer.attribute( "w", width ); @@ -662,7 +642,7 @@ public void dispose( ) } } - private int nextShapeId( ) + public int nextShapeId( ) //change to public { return shapeCount++; } @@ -729,4 +709,8 @@ public OOXmlWriter getWriter( ) { return writer; } + + public void setTable( TableWriter table){ + this.table = table; + } } diff --git a/engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/layout/emitter/PageDeviceRender.java b/engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/layout/emitter/PageDeviceRender.java index fb6948db7bb..2b6073b1fac 100644 --- a/engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/layout/emitter/PageDeviceRender.java +++ b/engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/layout/emitter/PageDeviceRender.java @@ -253,12 +253,15 @@ protected void startContainer( IContainerArea container ) } if ( container instanceof RowArea ) { + System.out.println("read a row..."); rowStyleStack.push( container.getBoxStyle( ) ); } else if ( container instanceof CellArea ) { + System.out.println("read a cell..."); drawCell( (CellArea) container ); } + else { drawContainer( container ); @@ -644,7 +647,7 @@ protected void addExtendDirection( int direction ) this.extendDirection |= direction; } - private void startClip( IArea area ) + public void startClip( IArea area ) { int startX = currentX + getX( area ); int startY = currentY + getY( area ); @@ -674,7 +677,7 @@ private void endClip( ) * @param height * container height */ - private void drawBackgroundImage( BackgroundImageInfo bi, int startX, + public void drawBackgroundImage( BackgroundImageInfo bi, int startX, int startY, int width, int height ) { try @@ -697,7 +700,7 @@ private void drawBackgroundImage( BackgroundImageInfo bi, int startX, * @param container * the containerArea whose border and background need to be drew */ - protected void drawContainer( IContainerArea container ) + public void drawContainer( IContainerArea container ) { // get the style of the container BoxStyle style = container.getBoxStyle( ); @@ -805,7 +808,7 @@ private BorderInfo[] cacheCellBorder( CellArea container ) return null; } - private BorderInfo[] cacheBorderInfo( IContainerArea container ) + public BorderInfo[] cacheBorderInfo( IContainerArea container ) { // get the style of the container BoxStyle style = container.getBoxStyle( ); @@ -1144,7 +1147,7 @@ private void drawBorder( TableBorder tb ) * @param borders * the border info */ - private void drawBorder( BorderInfo[] borders ) + public void drawBorder( BorderInfo[] borders ) { if ( borders == null ) return; @@ -1322,7 +1325,7 @@ protected int getScaledValue( int value ) // return getScaledValue( PropertyUtil.getDimensionValue( cssValue ) ); // } - protected void drawTableBorder( TableArea table ) + public void drawTableBorder( TableArea table ) { TableBorder tb = new TableBorder( table.getX( ), table.getY( ) ); traverseRows( tb, table, tb.tableX, tb.tableY );