From 3d96a478d6ef68cd69105ce21f1338bf6bd1652c Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Tue, 15 Apr 2008 15:12:58 +0000 Subject: [PATCH] Improved factoring of ppt objects. For ppt tabels Slide.getShapes() returns the Table object (was ShapeGroup) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@648276 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hslf/model/ShapeFactory.java | 50 +++++++++++++++--- .../src/org/apache/poi/hslf/model/Table.java | 52 ++++++++++++++++++- .../org/apache/poi/hslf/model/TextRun.java | 20 ++++--- 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java b/src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java index 0c36bf053a..76b43ebb8a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/ShapeFactory.java @@ -17,6 +17,10 @@ package org.apache.poi.hslf.model; import org.apache.poi.ddf.*; +import org.apache.poi.util.POILogger; +import org.apache.poi.util.POILogFactory; + +import java.util.List; /** * Create a Shape object depending on its type @@ -24,15 +28,46 @@ import org.apache.poi.ddf.*; * @author Yegor Kozlov */ public class ShapeFactory { + // For logging + protected static POILogger logger = POILogFactory.getLogger(ShapeFactory.class); /** * Create a new shape from the data provided. */ public static Shape createShape(EscherContainerRecord spContainer, Shape parent){ if (spContainer.getRecordId() == EscherContainerRecord.SPGR_CONTAINER){ - return new ShapeGroup(spContainer, parent); + return createShapeGroup(spContainer, parent); + } else { + return createSimpeShape(spContainer, parent); + + } + } + + public static ShapeGroup createShapeGroup(EscherContainerRecord spContainer, Shape parent){ + ShapeGroup group = null; + UnknownEscherRecord opt = (UnknownEscherRecord)Shape.getEscherChild((EscherContainerRecord)spContainer.getChild(0), (short)0xF122); + if(opt != null){ + try { + EscherPropertyFactory f = new EscherPropertyFactory(); + List props = f.createProperties( opt.getData(), 0, opt.getInstance() ); + EscherSimpleProperty p = (EscherSimpleProperty)props.get(0); + if(p.getPropertyNumber() == 0x39F && p.getPropertyValue() == 1){ + group = new ShapeGroup(spContainer, parent); + } else { + group = new ShapeGroup(spContainer, parent); + } + } catch (Exception e){ + logger.log(POILogger.WARN, e.getMessage()); + group = new ShapeGroup(spContainer, parent); + } + } else { + group = new ShapeGroup(spContainer, parent); } + return group; + } + + public static Shape createSimpeShape(EscherContainerRecord spContainer, Shape parent){ Shape shape; EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID); @@ -48,12 +83,13 @@ public class ShapeFactory { shape = new Line(spContainer, parent); break; case ShapeTypes.NotPrimitive: - if ((spRecord.getFlags() & EscherSpRecord.FLAG_GROUP) != 0) - //TODO: check if the shape group is a Table - shape = new ShapeGroup(spContainer, parent); + EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(spContainer, EscherOptRecord.RECORD_ID); + EscherProperty prop = Shape.getEscherProperty(opt, EscherProperties.GEOMETRY__VERTICES); + if(prop != null) + shape = new Freeform(spContainer, parent); else { - //TODO: check if the shape has GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO properties. - //if it does, then return Freeform or Polygon + + logger.log(POILogger.WARN, "Creating AutoShape for a NotPrimitive shape"); shape = new AutoShape(spContainer, parent); } break; @@ -62,6 +98,6 @@ public class ShapeFactory { break; } return shape; - } + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Table.java b/src/scratchpad/src/org/apache/poi/hslf/model/Table.java index a9f21b54f3..947c41af63 100755 --- a/src/scratchpad/src/org/apache/poi/hslf/model/Table.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/Table.java @@ -20,10 +20,10 @@ package org.apache.poi.hslf.model; import org.apache.poi.ddf.*; import org.apache.poi.util.LittleEndian; -import java.util.ArrayList; +import java.util.*; import java.util.List; -import java.util.Iterator; import java.awt.*; +import java.awt.geom.Rectangle2D; /** * Represents a table in a PowerPoint presentation @@ -144,6 +144,54 @@ public class Table extends ShapeGroup { } + protected void initTable(){ + Shape[] sh = getShapes(); + Arrays.sort(sh, new Comparator(){ + public int compare( Object o1, Object o2 ) { + Rectangle anchor1 = ((Shape)o1).getAnchor(); + Rectangle anchor2 = ((Shape)o2).getAnchor(); + int delta = anchor1.y - anchor2.y; + if(delta == 0) delta = anchor1.x - anchor2.y; + return delta; + } + }); + int y0 = -1; + int maxrowlen = 0; + ArrayList lst = new ArrayList(); + ArrayList row = null; + for (int i = 0; i < sh.length; i++) { + if(sh[i] instanceof TextShape){ + Rectangle anchor = sh[i].getAnchor(); + if(anchor.y != y0){ + y0 = anchor.y; + if(row != null) maxrowlen = Math.max(maxrowlen, row.size()); + row = new ArrayList(); + lst.add(row); + } + row.add(sh[i]); + } + } + cells = new TableCell[lst.size()][maxrowlen]; + for (int i = 0; i < lst.size(); i++) { + row = (ArrayList)lst.get(i); + for (int j = 0; j < row.size(); j++) { + TextShape tx = (TextShape)row.get(j); + cells[i][j] = new TableCell(tx.getSpContainer(), getParent()); + cells[i][j].setSheet(tx.getSheet()); + } + } + } + + /** + * Assign the SlideShow this shape belongs to + * + * @param sheet owner of this shape + */ + public void setSheet(Sheet sheet){ + super.setSheet(sheet); + if(cells == null) initTable(); + } + /** * Sets the row height. * diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java index 586932e739..5834e2eaec 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java @@ -24,12 +24,7 @@ import java.util.LinkedList; import java.util.Vector; import org.apache.poi.hslf.model.textproperties.TextPropCollection; -import org.apache.poi.hslf.record.Record; -import org.apache.poi.hslf.record.RecordContainer; -import org.apache.poi.hslf.record.StyleTextPropAtom; -import org.apache.poi.hslf.record.TextBytesAtom; -import org.apache.poi.hslf.record.TextCharsAtom; -import org.apache.poi.hslf.record.TextHeaderAtom; +import org.apache.poi.hslf.record.*; import org.apache.poi.hslf.usermodel.RichTextRun; import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.util.StringUtil; @@ -50,6 +45,7 @@ public class TextRun protected TextBytesAtom _byteAtom; protected TextCharsAtom _charAtom; protected StyleTextPropAtom _styleAtom; + protected TextSpecInfoAtom _specAtom; protected boolean _isUnicode; protected RichTextRun[] _rtRuns; private SlideShow slideShow; @@ -477,6 +473,18 @@ public class TextRun // Recreate rich text run with no styling _rtRuns[0] = new RichTextRun(this,0,s.length()); } + + /** + * If TextSpecInfoAtom is present, we must update the text size, + * otherwise the ppt will be corrupted + */ + if(_records != null) for (int i = 0; i < _records.length; i++) { + if(_records[i] instanceof TextSpecInfoAtom){ + TextSpecInfoAtom specAtom = (TextSpecInfoAtom)_records[i]; + specAtom.setTextSize(s.length()); + } + + } } /** -- 2.39.5