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
This commit is contained in:
Yegor Kozlov 2008-04-15 15:12:58 +00:00
parent e643ec7129
commit 437dc3b77d
3 changed files with 107 additions and 15 deletions

View File

@ -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 <code>Shape</code> 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;
}
}
}

View File

@ -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 <code>SlideShow</code> 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.
*

View File

@ -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());
}
}
}
/**