Support for Master Sheets from Yegor (bug 40753)

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@464982 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2006-10-17 18:01:19 +00:00
parent 966759c29b
commit 5f167f01b2
26 changed files with 1074 additions and 329 deletions

View File

@ -45,17 +45,15 @@ public class AutoShape extends SimpleShape {
short type = (short)((shapeType << 4) | 0x2); short type = (short)((shapeType << 4) | 0x2);
spRecord.setOptions(type); spRecord.setOptions(type);
//set default properties for a line //set default properties for an autoshape
EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID);
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__FILLCOLOR, 134217732)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__FILLCOLOR, 0x8000004));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__FILLBACKCOLOR, 134217728)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__FILLBACKCOLOR, 0x8000000));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__NOFILLHITTEST, 1048592)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.FILL__NOFILLHITTEST, 0x100010));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__COLOR, 134217729)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__COLOR, 0x8000001));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 524296)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80008));
opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.SHADOWSTYLE__COLOR, 134217730)); opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.SHADOWSTYLE__COLOR, 0x8000002));
opt.sortProperties();
return spcont; return spcont;
} }

View File

@ -27,66 +27,69 @@ public class Line extends SimpleShape {
/** /**
* Solid (continuous) pen * Solid (continuous) pen
*/ */
public static final int LineSolid = 1; public static final int PEN_SOLID = 1;
/** /**
* PS_DASH system dash style * PS_DASH system dash style
*/ */
public static final int LineDashSys = 2; public static final int PEN_PS_DASH = 2;
/** /**
* PS_DOT system dash style * PS_DOT system dash style
*/ */
public static final int LineDotSys = 3; public static final int PEN_DOT = 3;
/** /**
* PS_DASHDOT system dash style * PS_DASHDOT system dash style
*/ */
public static final int LineDashDotSys = 4; public static final int PEN_DASHDOT = 4;
/** /**
* PS_DASHDOTDOT system dash style * PS_DASHDOTDOT system dash style
*/ */
public static final int LineDashDotDotSys = 5; public static final int PEN_DASHDOTDOT = 5;
/** /**
* square dot style * square dot style
*/ */
public static final int LineDotGEL = 6; public static final int PEN_DOTGEL = 6;
/** /**
* dash style * dash style
*/ */
public static final int LineDashGEL = 7; public static final int PEN_DASH = 7;
/** /**
* long dash style * long dash style
*/ */
public static final int LineLongDashGEL = 8; public static final int PEN_LONGDASHGEL = 8;
/** /**
* dash short dash * dash short dash
*/ */
public static final int LineDashDotGEL = 9; public static final int PEN_DASHDOTGEL = 9;
/** /**
* long dash short dash * long dash short dash
*/ */
public static final int LineLongDashDotGEL = 10; public static final int PEN_LONGDASHDOTGEL = 10;
/** /**
* long dash short dash short dash * long dash short dash short dash
*/ */
public static final int LineLongDashDotDotGEL = 11; public static final int PEN_LONGDASHDOTDOTGEL = 11;
/** /**
* Decoration of the end of line, * Single line (of width lineWidth)
* reserved in API but not supported.
*/ */
public static final int LINE_SIMPLE = 0;
/**
* Double lines of equal width
*/
public static final int LINE_DOUBLE = 1;
/**
* Double lines, one thick, one thin
*/
public static final int LINE_THICKTHIN = 2;
/**
* Double lines, reverse order
*/
public static final int LINE_THINTHICK = 3;
/**
* Three lines, thin, thick, thin
*/
public static final int LINE_TRIPLE = 4;
/**
* Line ends at end point
*/
public static final int EndCapFlat = 0;
/**
* Rounded ends - the default
*/
public static final int EndCapRound = 1;
/**
* Square protrudes by half line width
*/
public static final int EndCapSquare = 2;
protected Line(EscherContainerRecord escherRecord, Shape parent){ protected Line(EscherContainerRecord escherRecord, Shape parent){
super(escherRecord, parent); super(escherRecord, parent);
@ -111,7 +114,13 @@ public class Line extends SimpleShape {
//set default properties for a line //set default properties for a line
EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID);
opt.sortProperties(); //default line properties
setEscherProperty(opt, EscherProperties.GEOMETRY__SHAPEPATH, 4);
setEscherProperty(opt, EscherProperties.GEOMETRY__FILLOK, 0x10000);
setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x100000);
setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, 0x8000001);
setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0xA0008);
setEscherProperty(opt, EscherProperties.SHADOWSTYLE__COLOR, 0x8000002);
return spcont; return spcont;
} }

View File

@ -0,0 +1,12 @@
package org.apache.poi.hslf.model;
/**
* The superclass of all master sheets - Slide masters, Notes masters, etc.
*
* For now it's empty. When we understand more about masters in ppt we will add the common functionality here.
*
* @author Yegor Kozlov
*/
public abstract class MasterSheet extends Sheet {
}

View File

@ -26,8 +26,10 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator; import java.awt.geom.PathIterator;
import java.text.AttributedCharacterIterator; import java.text.AttributedCharacterIterator;
import java.util.Map; import java.util.Map;
import java.util.ArrayList;
import org.apache.poi.ddf.EscherProperties; import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.hslf.usermodel.RichTextRun;
/** /**
* Translates Graphics2D calls into PowerPoint. * Translates Graphics2D calls into PowerPoint.
@ -45,9 +47,9 @@ public class PPGraphics2D extends Graphics2D {
private Color foreground; private Color foreground;
private Color background = Color.white; private Color background = Color.white;
private Shape clip; private Shape clip;
int count = 0;
/** /**
* Construct an powerpoint Graphics object. * Construct Java Graphics object which translates graphic calls in ppt drawing layer.
* *
* @param group The shape group to write the graphics calls into. * @param group The shape group to write the graphics calls into.
*/ */
@ -106,8 +108,11 @@ public class PPGraphics2D extends Graphics2D {
public void draw(Shape shape){ public void draw(Shape shape){
if(clip != null) { if(clip != null) {
if (!clip.getBounds().contains(transform.createTransformedShape(shape).getBounds())) { java.awt.Rectangle bounds = getTransform().createTransformedShape(shape).getBounds();
//return; if (bounds.width == 0) bounds.width = 1;
if (bounds.height == 0) bounds.height = 1;
if (!clip.getBounds().contains(bounds)) {
return;
} }
} }
@ -123,6 +128,8 @@ public class PPGraphics2D extends Graphics2D {
if (stroke instanceof BasicStroke){ if (stroke instanceof BasicStroke){
BasicStroke bs = (BasicStroke)stroke; BasicStroke bs = (BasicStroke)stroke;
line.setLineWidth(bs.getLineWidth()); line.setLineWidth(bs.getLineWidth());
float[] dash = bs.getDashArray();
if (dash != null) line.setLineDashing(Line.PEN_DASH);
} }
if(getColor() != null) line.setLineColor(getColor()); if(getColor() != null) line.setLineColor(getColor());
if (type == PathIterator.SEG_LINETO) { if (type == PathIterator.SEG_LINETO) {
@ -139,24 +146,26 @@ public class PPGraphics2D extends Graphics2D {
} }
public void drawString(String string, float x, float y){ public void drawString(String string, float x, float y){
TextBox txt = new TextBox(group); TextBox txt = new TextBox(group);
txt.getTextRun().supplySlideShow(group.getSheet().getSlideShow());
txt.getTextRun().setSheet(group.getSheet());
txt.setText(string);
RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
rt.setFontSize(font.getSize());
rt.setFontName(font.getFamily());
if(getColor() != null) rt.setFontColor(getColor());
if (font.isBold()) rt.setBold(true);
if (font.isItalic()) rt.setItalic(true);
txt.setMarginBottom(0); txt.setMarginBottom(0);
txt.setMarginTop(0); txt.setMarginTop(0);
txt.setMarginLeft(0); txt.setMarginLeft(0);
txt.setMarginRight(0); txt.setMarginRight(0);
txt.setText(string);
txt.setWordWrap(TextBox.WrapNone); txt.setWordWrap(TextBox.WrapNone);
if (font != null){ if (!"".equals(string)) txt.resizeToFitText();
txt.setFontSize(font.getSize());
txt.setFontName(font.getName());
if(getColor() != null) txt.setFontColor(getColor());
if (font.isBold()) txt.setBold(true);
if (font.isItalic()) txt.setItalic(true);
}
txt.resizeToFitText();
int height = (int)txt.getAnchor().getHeight(); int height = (int)txt.getAnchor().getHeight();
/* /*
@ -166,15 +175,57 @@ public class PPGraphics2D extends Graphics2D {
*/ */
txt.moveTo((int)x, (int)(y - height)); txt.moveTo((int)x, (int)(y - height));
group.addShape(txt); if(clip != null) {
if (!clip.getBounds().contains(txt.getAnchor())) {
;//return;
}
}
group.addShape(txt);
} }
public void fill(Shape shape){ public void fill(Shape shape){
if (paint instanceof Color){ if(clip != null) {
Color color = (Color)paint; java.awt.Rectangle bounds = getTransform().createTransformedShape(shape).getBounds();
if (bounds.width == 0) bounds.width = 1;
if (bounds.height == 0) bounds.height = 1;
if (!clip.getBounds().contains(bounds)) {
return;
}
}
PathIterator it = shape.getPathIterator(transform);
ArrayList pnt = new ArrayList();
double[] coords = new double[6];
while(!it.isDone()){
int type = it.currentSegment(coords);
if (type != PathIterator.SEG_CLOSE) {
pnt.add(new Point((int)coords[0], (int)coords[1]));
}
it.next();
}
int[] xPoints= new int[pnt.size()];
int[] yPoints= new int[pnt.size()];
for (int i = 0; i < pnt.size(); i++) {
Point p = (Point)pnt.get(i);
xPoints[i] = p.x;
yPoints[i] = p.y;
} }
throw new RuntimeException("Not implemented"); AutoShape r = new AutoShape(ShapeTypes.Rectangle);
if (paint instanceof Color){
Color color = (Color)paint;
r.setFillColor(color);
}
if(getColor() != null) r.setLineColor(getColor());
if (stroke instanceof BasicStroke){
BasicStroke bs = (BasicStroke)stroke;
r.setLineWidth(bs.getLineWidth());
float[] dash = bs.getDashArray();
if (dash != null) r.setLineDashing(Line.PEN_DASH);
}
java.awt.Rectangle bounds = transform.createTransformedShape(shape).getBounds();
r.setAnchor(bounds);
group.addShape(r);
} }
public void translate(int x, int y) { public void translate(int x, int y) {
@ -458,5 +509,4 @@ public class PPGraphics2D extends Graphics2D {
public void drawRenderableImage(RenderableImage renderableimage, AffineTransform affinetransform) { public void drawRenderableImage(RenderableImage renderableimage, AffineTransform affinetransform) {
throw new RuntimeException("Not implemented"); throw new RuntimeException("Not implemented");
} }
} }

View File

@ -37,19 +37,16 @@ public class ShapeFactory {
int type = spRecord.getOptions() >> 4; int type = spRecord.getOptions() >> 4;
switch (type){ switch (type){
case ShapeTypes.Rectangle:
EscherTextboxRecord txtbox = (EscherTextboxRecord)Shape.getEscherChild(spContainer, EscherTextboxRecord.RECORD_ID);
if (txtbox == null) shape = new AutoShape(spContainer, parent);
else{
if(Shape.getEscherChild(spContainer, EscherClientDataRecord.RECORD_ID) != null )
shape = new Placeholder(spContainer, parent);
else
shape = new TextBox(spContainer, parent);
}
break;
case ShapeTypes.TextBox: case ShapeTypes.TextBox:
shape = new TextBox(spContainer, parent); shape = new TextBox(spContainer, parent);
break; break;
case ShapeTypes.Rectangle:
EscherTextboxRecord txtbox = (EscherTextboxRecord)Shape.getEscherChild(spContainer, EscherTextboxRecord.RECORD_ID);
if (txtbox == null)
shape = new AutoShape(spContainer, parent);
else
shape = new TextBox(spContainer, parent);
break;
case ShapeTypes.PictureFrame: case ShapeTypes.PictureFrame:
shape = new Picture(spContainer, parent); shape = new Picture(spContainer, parent);
break; break;
@ -57,7 +54,10 @@ public class ShapeFactory {
shape = new Line(spContainer, parent); shape = new Line(spContainer, parent);
break; break;
case ShapeTypes.NotPrimitive: case ShapeTypes.NotPrimitive:
shape = new ShapeGroup(spContainer, parent); if ((spRecord.getFlags() & EscherSpRecord.FLAG_GROUP) != 0)
shape = new ShapeGroup(spContainer, parent);
else
shape = new AutoShape(spContainer, parent);
break; break;
default: default:
shape = new AutoShape(spContainer, parent); shape = new AutoShape(spContainer, parent);

View File

@ -95,17 +95,17 @@ public class ShapeGroup extends Shape{
LittleEndian.putInt(header, 4, 8); LittleEndian.putInt(header, 4, 8);
clientAnchor.fillFields(header, 0, null); clientAnchor.fillFields(header, 0, null);
clientAnchor.setFlag((short)anchor.y); clientAnchor.setFlag((short)(anchor.y*MASTER_DPI/POINT_DPI));
clientAnchor.setCol1((short)anchor.x); clientAnchor.setCol1((short)(anchor.x*MASTER_DPI/POINT_DPI));
clientAnchor.setDx1((short)(anchor.width + anchor.x)); clientAnchor.setDx1((short)((anchor.width + anchor.x)*MASTER_DPI/POINT_DPI));
clientAnchor.setRow1((short)(anchor.height + anchor.y)); clientAnchor.setRow1((short)((anchor.height + anchor.y)*MASTER_DPI/POINT_DPI));
EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(spContainer, EscherSpgrRecord.RECORD_ID); EscherSpgrRecord spgr = (EscherSpgrRecord)getEscherChild(spContainer, EscherSpgrRecord.RECORD_ID);
spgr.setRectX1(anchor.x); spgr.setRectX1(anchor.x*MASTER_DPI/POINT_DPI);
spgr.setRectY1(anchor.y); spgr.setRectY1(anchor.y*MASTER_DPI/POINT_DPI);
spgr.setRectX2(anchor.x + anchor.width); spgr.setRectX2((anchor.x + anchor.width)*MASTER_DPI/POINT_DPI);
spgr.setRectY2(anchor.y + anchor.height); spgr.setRectY2((anchor.y + anchor.height)*MASTER_DPI/POINT_DPI);
} }
/** /**
@ -145,6 +145,15 @@ public class ShapeGroup extends Shape{
*/ */
public void addShape(Shape shape){ public void addShape(Shape shape){
_escherContainer.addChildRecord(shape.getSpContainer()); _escherContainer.addChildRecord(shape.getSpContainer());
Sheet sheet = getSheet();
shape.setSheet(sheet);
shape.afterInsert(sheet);
if(shape instanceof TextBox) {
TextBox tbox = (TextBox)shape;
getSheet().getPPDrawing().addTextboxWrapper(tbox._txtbox);
}
} }
/** /**

View File

@ -209,4 +209,18 @@ public abstract class Sheet
ppdrawing.addTextboxWrapper(tbox._txtbox); ppdrawing.addTextboxWrapper(tbox._txtbox);
} }
} }
/**
* Return the master sheet .
*/
public MasterSheet getMasterSheet(){
return null;
}
/**
* Color scheme for this sheet.
*/
public ColorSchemeAtom getColorScheme(){
return null;
}
} }

View File

@ -18,6 +18,7 @@ package org.apache.poi.hslf.model;
import org.apache.poi.ddf.*; import org.apache.poi.ddf.*;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import java.awt.*; import java.awt.*;
@ -105,6 +106,7 @@ public class SimpleShape extends Shape {
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB(); int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb); setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, rgb);
setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, color == null ? 0x180010 : 0x180018);
} }
/** /**
@ -112,13 +114,45 @@ public class SimpleShape extends Shape {
*/ */
public Color getLineColor(){ public Color getLineColor(){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherRGBProperty prop = (EscherRGBProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__COLOR);
Color color = Color.black; EscherSimpleProperty p1 = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__COLOR);
if (prop != null){ EscherSimpleProperty p2 = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH);
Color swp = new Color(prop.getRgbColor()); int p2val = p2 == null ? 0 : p2.getPropertyValue();
color = new Color(swp.getBlue(), swp.getGreen(), swp.getRed()); Color clr = null;
if (p1 != null && (p2val & 0x8) != 0){
int rgb = p1.getPropertyValue();
if (rgb >= 0x8000000) {
int idx = rgb % 0x8000000;
ColorSchemeAtom ca = getSheet().getColorScheme();
if(idx >= 0 && idx <= 7) rgb = ca.getColor(idx);
}
Color tmp = new Color(rgb, true);
clr = new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
} }
return color; return clr;
}
/**
* Gets line dashing. One of the PEN_* constants defined in this class.
*
* @return dashing of the line.
*/
public int getLineDashing(){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING);
return prop == null ? Line.PEN_SOLID : prop.getPropertyValue();
}
/**
* Sets line dashing. One of the PEN_* constants defined in this class.
*
* @param pen new style of the line.
*/
public void setLineDashing(int pen){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING, pen == Line.PEN_SOLID ? -1 : pen);
} }
/** /**
@ -128,7 +162,7 @@ public class SimpleShape extends Shape {
*/ */
public void setLineStyle(int style){ public void setLineStyle(int style){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
setEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING, style == Line.LineSolid ? -1 : style); setEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE, style == Line.LINE_SIMPLE ? -1 : style);
} }
/** /**
@ -138,8 +172,34 @@ public class SimpleShape extends Shape {
*/ */
public int getLineStyle(){ public int getLineStyle(){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINEDASHING); EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.LINESTYLE__LINESTYLE);
return prop == null ? Line.LineSolid : prop.getPropertyValue(); return prop == null ? Line.LINE_SIMPLE : prop.getPropertyValue();
}
/**
* The color used to fill this shape.
*
* @param color the background color
*/
public Color getFillColor(Color color){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherSimpleProperty p1 = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.FILL__FILLCOLOR);
EscherSimpleProperty p2= (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST);
int p2val = p2 == null ? 0 : p2.getPropertyValue();
Color clr = null;
if (p1 != null && (p2val & 0x10) != 0){
int rgb = p1.getPropertyValue();
if (rgb >= 0x8000000) {
int idx = rgb % 0x8000000;
ColorSchemeAtom ca = getSheet().getColorScheme();
rgb = ca.getColor(idx);
}
Color tmp = new Color(rgb, true);
clr = new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
}
return clr;
} }
/** /**
@ -151,8 +211,7 @@ public class SimpleShape extends Shape {
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB(); int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb); setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb);
setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 1376273); setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, color == null ? 0x150010 : 0x150011);
} }
} }

View File

@ -24,6 +24,7 @@ import java.util.Vector;
import org.apache.poi.hslf.record.PPDrawing; import org.apache.poi.hslf.record.PPDrawing;
import org.apache.poi.hslf.record.SlideAtom; import org.apache.poi.hslf.record.SlideAtom;
import org.apache.poi.hslf.record.TextHeaderAtom; import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet; import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
/** /**
@ -81,10 +82,12 @@ public class Slide extends Sheet
int i=0; int i=0;
for(i=0; i<textRuns.size(); i++) { for(i=0; i<textRuns.size(); i++) {
_runs[i] = (TextRun)textRuns.get(i); _runs[i] = (TextRun)textRuns.get(i);
_runs[i].setSheet(this);
} }
// Grab text from slide's PPDrawing // Grab text from slide's PPDrawing
for(int k=0; k<_otherRuns.length; i++, k++) { for(int k=0; k<_otherRuns.length; i++, k++) {
_runs[i] = _otherRuns[k]; _runs[i] = _otherRuns[k];
_runs[i].setSheet(this);
} }
} }
@ -135,7 +138,7 @@ public class Slide extends Sheet
public TextBox addTitle() { public TextBox addTitle() {
Placeholder pl = new Placeholder(); Placeholder pl = new Placeholder();
pl.setShapeType(ShapeTypes.Rectangle); pl.setShapeType(ShapeTypes.Rectangle);
pl.setTextType(TextHeaderAtom.TITLE_TYPE); pl.getTextRun().setRunType(TextHeaderAtom.TITLE_TYPE);
pl.setText("Click to edit title"); pl.setText("Click to edit title");
pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90)); pl.setAnchor(new java.awt.Rectangle(54, 48, 612, 90));
addShape(pl); addShape(pl);
@ -212,4 +215,34 @@ public class Slide extends Sheet
* which hold text data for this slide (typically for placeholders). * which hold text data for this slide (typically for placeholders).
*/ */
protected SlideAtomsSet getSlideAtomsSet() { return _atomSet; } protected SlideAtomsSet getSlideAtomsSet() { return _atomSet; }
/**
* Returns the slide master associated with this slide.
*
* @return the slide master associated with this slide.
*/
public MasterSheet getMasterSheet(){
SlideMaster[] master = getSlideShow().getSlidesMasters();
SlideAtom sa = _slide.getSlideAtom();
int masterId = sa.getMasterID();
for (int i = 0; i < master.length; i++) {
if (masterId == master[i]._getSheetNumber()) return master[i];
}
throw new RuntimeException("Master slide not found for slide " + _slideNo);
}
/**
* Change Master of this slide.
*/
public void setMasterSheet(MasterSheet master){
SlideAtom sa = _slide.getSlideAtom();
int sheetNo = master._getSheetNumber();
sa.setMasterID(sheetNo);
}
public ColorSchemeAtom getColorScheme(){
return _slide.getColorScheme();
}
} }

View File

@ -0,0 +1,146 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.model;
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.record.StyleTextPropAtom.*;
/**
* SlideMaster determines the graphics, layout, and formatting for all the slides in a given presentation.
* It stores information about default font styles, placeholder sizes and positions,
* background design, and color schemes.
*
* @author Yegor Kozlov
*/
public class SlideMaster extends MasterSheet {
private int _refSheetNo;
private int _sheetNo;
private MainMaster _master;
private TextRun[] _runs;
/**
* all TxMasterStyleAtoms available in this master
*/
private TxMasterStyleAtom[] _txmaster;
/**
* Constructs a SlideMaster from the MainMaster record,
*
*/
public SlideMaster(org.apache.poi.hslf.record.MainMaster rec, int slideId) {
_master = rec;
// Grab our internal sheet ID
_refSheetNo = rec.getSheetId();
// Grab the number of the slide we're for, via the NotesAtom
_sheetNo = slideId;
_runs = findTextRuns(_master.getPPDrawing());
}
/**
* Returns an array of all the TextRuns found
*/
public TextRun[] getTextRuns() {
return _runs;
}
/**
* Returns the (internal, RefID based) sheet number, as used
* to in PersistPtr stuff.
*/
public int _getSheetRefId() {
return _refSheetNo;
}
/**
* Returns the (internal, SlideIdentifer based) number of the
* slide we're attached to
*/
public int _getSheetNumber() {
return _sheetNo;
}
/**
* Returns the PPDrawing associated with this slide master
*/
protected PPDrawing getPPDrawing() {
return _master.getPPDrawing();
}
/**
* Pickup a style attribute from the master.
* This is the "workhorse" which returns the default style attrubutes.
*/
public TextProp getStyleAttribute(int txtype, int level, String name, boolean isCharacter) {
TextProp prop = null;
for (int i = level; i >= 0; i--) {
TextPropCollection[] styles =
isCharacter ? _txmaster[txtype].getCharacterStyles() : _txmaster[txtype].getParagraphStyles();
if (i < styles.length) prop = styles[i].findByName(name);
if (prop != null) break;
}
if (prop == null) {
switch (txtype) {
case TextHeaderAtom.CENTRE_BODY_TYPE:
case TextHeaderAtom.HALF_BODY_TYPE:
case TextHeaderAtom.QUARTER_BODY_TYPE:
txtype = TextHeaderAtom.BODY_TYPE;
break;
case TextHeaderAtom.CENTER_TITLE_TYPE:
txtype = TextHeaderAtom.TITLE_TYPE;
break;
default:
return null;
}
prop = getStyleAttribute(txtype, level, name, isCharacter);
}
return prop;
}
/**
* Assign SlideShow for this slide master.
* (Used interanlly)
*/
public void setSlideShow(SlideShow ss) {
super.setSlideShow(ss);
//after the slide show is assigned collect all available style records
if (_txmaster == null) {
_txmaster = new TxMasterStyleAtom[9];
TxMasterStyleAtom txdoc = getSlideShow().getDocumentRecord().getEnvironment().getTxMasterStyleAtom();
_txmaster[txdoc.getTextType()] = txdoc;
TxMasterStyleAtom[] txrec = _master.getTxMasterStyleAtoms();
for (int i = 0; i < txrec.length; i++) {
_txmaster[txrec[i].getTextType()] = txrec[i];
}
}
}
/**
* Returns the ColorSchemeAtom associated with this slide master
*/
public ColorSchemeAtom getColorScheme(){
return _master.getColorScheme();
}
}

View File

@ -69,11 +69,6 @@ public class TextBox extends SimpleShape {
public static final int AlignRight = 2; public static final int AlignRight = 2;
public static final int AlignJustify = 3; public static final int AlignJustify = 3;
/**
* Default font size
*/
public static final int DefaultFontSize = 24;
/** /**
* Low-level object which holds actual text and format data * Low-level object which holds actual text and format data
*/ */
@ -139,14 +134,12 @@ public class TextBox extends SimpleShape {
EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(spcont, EscherOptRecord.RECORD_ID);
setEscherProperty(opt, EscherProperties.TEXT__TEXTID, 0); setEscherProperty(opt, EscherProperties.TEXT__TEXTID, 0);
setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, 134217732); setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, 0x8000004);
setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, 134217728); setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, 0x8000000);
setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 1048576); setEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST, 0x100000);
setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, 134217729); setEscherProperty(opt, EscherProperties.LINESTYLE__COLOR, 0x8000001);
setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 524288); setEscherProperty(opt, EscherProperties.LINESTYLE__NOLINEDRAWDASH, 0x80000);
setEscherProperty(opt, EscherProperties.SHADOWSTYLE__COLOR, 134217730); setEscherProperty(opt, EscherProperties.SHADOWSTYLE__COLOR, 0x8000002);
opt.sortProperties();
//create EscherTextboxWrapper //create EscherTextboxWrapper
_txtbox = new EscherTextboxWrapper(); _txtbox = new EscherTextboxWrapper();
@ -155,13 +148,13 @@ public class TextBox extends SimpleShape {
tha.setParentRecord(_txtbox); // TextHeaderAtom is parent aware tha.setParentRecord(_txtbox); // TextHeaderAtom is parent aware
_txtbox.appendChildRecord(tha); _txtbox.appendChildRecord(tha);
TextBytesAtom tba = new TextBytesAtom(); TextCharsAtom tca = new TextCharsAtom();
_txtbox.appendChildRecord(tba); _txtbox.appendChildRecord(tca);
StyleTextPropAtom sta = new StyleTextPropAtom(0); StyleTextPropAtom sta = new StyleTextPropAtom(0);
_txtbox.appendChildRecord(sta); _txtbox.appendChildRecord(sta);
_txtrun = new TextRun(tha,tba,sta); _txtrun = new TextRun(tha,tca,sta);
_txtrun.setText(""); _txtrun.setText("");
spcont.addChildRecord(_txtbox.getEscherRecord()); spcont.addChildRecord(_txtbox.getEscherRecord());
@ -205,40 +198,31 @@ public class TextBox extends SimpleShape {
} }
/** /**
* Returns the bounds of this <code>TextFrame</code>. * Adjust the size of the TextBox so it encompasses the text inside it.
* <code>Note</b>, this is very primitive estimation, the precision is poor.
*
* @return the bounds of this <code>TextFrame</code>.
*/ */
protected Dimension getTextDimensions(){ public void resizeToFitText(){
try{
FontRenderContext frc = new FontRenderContext(null, true, true); FontRenderContext frc = new FontRenderContext(null, true, true);
RichTextRun rt = _txtrun.getRichTextRuns()[0]; RichTextRun rt = _txtrun.getRichTextRuns()[0];
int size = rt.getFontSize(); int size = rt.getFontSize();
if (size == -1) size = TextBox.DefaultFontSize;
int style = 0; int style = 0;
if (rt.isBold()) style |= Font.BOLD; if (rt.isBold()) style |= Font.BOLD;
if (rt.isItalic()) style |= Font.ITALIC; if (rt.isItalic()) style |= Font.ITALIC;
String fntname = rt.getFontName(); String fntname = rt.getFontName();
if (fntname == null) //get the default font from Document.Environment.FontCollection
fntname = getSheet().getSlideShow().getDocumentRecord().getEnvironment().getFontCollection().getFontWithId(0);
Font font = new Font(fntname, style, size); Font font = new Font(fntname, style, size);
TextLayout layout = new TextLayout(getText(), font, frc); TextLayout layout = new TextLayout(getText(), font, frc);
int width = Math.round(layout.getAdvance()); int width = Math.round(layout.getAdvance());
width += getMarginLeft() + getMarginRight() + 2;
int height = Math.round(layout.getAscent()); int height = Math.round(layout.getAscent());
height += getMarginTop() + getMarginBottom() + 12;
return new Dimension(width, height);
}
/** Dimension txsize = new Dimension(width, height);
* Adjust the size of the TextBox so it encompasses the text inside it.
*/
public void resizeToFitText(){
Dimension size = getTextDimensions();
java.awt.Rectangle anchor = getAnchor(); java.awt.Rectangle anchor = getAnchor();
anchor.setSize(size); anchor.setSize(txsize);
setAnchor(anchor); setAnchor(anchor);
} catch (Exception e){
e.printStackTrace();
}
} }
/** /**
@ -250,7 +234,22 @@ public class TextBox extends SimpleShape {
public int getVerticalAlignment(){ public int getVerticalAlignment(){
EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID); EscherOptRecord opt = (EscherOptRecord)getEscherChild(_escherContainer, EscherOptRecord.RECORD_ID);
EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT); EscherSimpleProperty prop = (EscherSimpleProperty)getEscherProperty(opt, EscherProperties.TEXT__ANCHORTEXT);
return prop == null ? AlignCenter : prop.getPropertyValue(); int valign;
if (prop == null){
int type = getTextRun().getRunType();
switch (type){
case TextHeaderAtom.TITLE_TYPE:
case TextHeaderAtom.CENTER_TITLE_TYPE:
valign = TextBox.AnchorMiddle;
break;
default:
valign = TextBox.AnchorTop;
break;
}
} else {
valign = prop.getPropertyValue();
}
return valign;
} }
/** /**
@ -425,129 +424,7 @@ public class TextBox extends SimpleShape {
return _txtrun; return _txtrun;
} }
/** public void setSheet(Sheet sheet){
* @return array of RichTextRun objects which control text formatting in this text box
*/
public RichTextRun[] getRichTextRuns(){
return _txtrun.getRichTextRuns();
}
/**
* Sets the <code>Font</code> object for this text frame
*
* @param size the size of the font
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setFontSize(int size){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setFontSize(size);
}
/**
*
* @return the size of the font applied to this text shape
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public int getFontSize(){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
return rt.getFontSize();
}
/**
*
* @return the size of the font applied to this text shape
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public Color getFontColor(){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
Color color = new Color(rt.getFontColor());
//in PowerPont RGB bytes are swapped,
return new Color(color.getBlue(), color.getGreen(), color.getRed(), 255);
}
/**
* Set whether to use bold or not
*
* @param bold <code>true</code> if the text should be bold, <code>false</code> otherwise
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setBold(boolean bold){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setBold(bold);
}
/**
* Set whether to use italic or not
*
* @param italic <code>true</code> if the text should be italic, <code>false</code> otherwise
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setItalic(boolean italic){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setItalic(italic);
}
/**
* Set whether to use underline or not
*
* @param underline <code>true</code> if the text should be underlined, <code>false</code> otherwise
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setUnderline(boolean underline){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setUnderlined(underline);
}
/**
* Sets the font of this text shape
*
* @param name the name of the font to be applied to this text shape
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setFontName(String name){
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setFontName(name);
}
/**
* Sets the font color
* @param color the font color
*
* @deprecated Use <code>RichTextRun</code> to work with the text format.
* <p>This method will be permanently removed in a future version of the POI HSLF API.</p>
*/
public void setFontColor(Color color){
//in PowerPont RGB bytes are swapped,
int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 254).getRGB();
RichTextRun rt = _txtrun.getRichTextRuns()[0];
rt.setFontColor(rgb);
}
/**
* Set type of the text.
* Must be one of the static constants defined in <code>TextHeaderAtom</code>
*
* @param type type of the text
*/
public void setTextType(int type){
_txtrun._headerAtom.setTextType(type);
}
public void setSheet(Sheet sheet){
_sheet = sheet; _sheet = sheet;
// Initialize _txtrun object. // Initialize _txtrun object.
@ -564,6 +441,7 @@ public class TextBox extends SimpleShape {
} }
// Supply the sheet to our child RichTextRuns // Supply the sheet to our child RichTextRuns
_txtrun.setSheet(sheet);
RichTextRun[] rt = _txtrun.getRichTextRuns(); RichTextRun[] rt = _txtrun.getRichTextRuns();
for (int i = 0; i < rt.length; i++) { for (int i = 0; i < rt.length; i++) {
rt[i].supplySlideShow(_sheet.getSlideShow()); rt[i].supplySlideShow(_sheet.getSlideShow());

View File

@ -47,6 +47,7 @@ public class TextRun
protected boolean _isUnicode; protected boolean _isUnicode;
protected RichTextRun[] _rtRuns; protected RichTextRun[] _rtRuns;
private SlideShow slideShow; private SlideShow slideShow;
private Sheet sheet;
/** /**
* Constructs a Text Run from a Unicode text block * Constructs a Text Run from a Unicode text block
@ -378,10 +379,12 @@ public class TextRun
public synchronized void setText(String s) { public synchronized void setText(String s) {
// Save the new text to the atoms // Save the new text to the atoms
storeText(s); storeText(s);
RichTextRun fst = _rtRuns[0];
// Finally, zap and re-do the RichTextRuns // Finally, zap and re-do the RichTextRuns
for(int i=0; i<_rtRuns.length; i++) { _rtRuns[i] = null; } for(int i=0; i<_rtRuns.length; i++) { _rtRuns[i] = null; }
_rtRuns = new RichTextRun[1]; _rtRuns = new RichTextRun[1];
_rtRuns[0] = fst;
// Now handle record stylings: // Now handle record stylings:
// If there isn't styling // If there isn't styling
@ -395,17 +398,7 @@ public class TextRun
LinkedList cStyles = _styleAtom.getCharacterStyles(); LinkedList cStyles = _styleAtom.getCharacterStyles();
while(cStyles.size() > 1) { cStyles.removeLast(); } while(cStyles.size() > 1) { cStyles.removeLast(); }
// Note - TextPropCollection's idea of the text length must _rtRuns[0].setText(s);
// be one larger than it actually is!
// (This indicates that new text added to the end should
// get the same styling as the current text)
TextPropCollection pCol = (TextPropCollection)pStyles.getFirst();
TextPropCollection cCol = (TextPropCollection)cStyles.getFirst();
pCol.updateTextSize(s.length()+1);
cCol.updateTextSize(s.length()+1);
// Recreate rich text run with first styling
_rtRuns[0] = new RichTextRun(this,0,s.length(), pCol, cCol, false, false);
} else { } else {
// Recreate rich text run with no styling // Recreate rich text run with no styling
_rtRuns[0] = new RichTextRun(this,0,s.length()); _rtRuns[0] = new RichTextRun(this,0,s.length());
@ -515,4 +508,12 @@ public class TextRun
} }
} }
} }
public void setSheet(Sheet sheet){
this.sheet = sheet;
}
public Sheet getSheet(){
return this.sheet;
}
} }

View File

@ -205,4 +205,17 @@ public class ColorSchemeAtom extends RecordAtom
writeLittleEndian(accentAndHyperlinkColourRGB,out); writeLittleEndian(accentAndHyperlinkColourRGB,out);
writeLittleEndian(accentAndFollowingHyperlinkColourRGB,out); writeLittleEndian(accentAndFollowingHyperlinkColourRGB,out);
} }
/**
* Returns color by its index
*
* @param idx 0-based color index
* @return color by its index
*/
public int getColor(int idx){
int[] clr = {backgroundColourRGB, textAndLinesColourRGB, shadowsColourRGB, titleTextColourRGB,
fillsColourRGB, accentColourRGB, accentAndHyperlinkColourRGB, accentAndFollowingHyperlinkColourRGB};
return clr[idx];
}
} }

View File

@ -34,6 +34,8 @@ public class Environment extends PositionDependentRecordContainer
// Links to our more interesting children // Links to our more interesting children
private FontCollection fontCollection; private FontCollection fontCollection;
//master style for text with type=TextHeaderAtom.OTHER_TYPE
private TxMasterStyleAtom txmaster;
/** /**
* Returns the FontCollection of this Environment * Returns the FontCollection of this Environment
@ -56,7 +58,9 @@ public class Environment extends PositionDependentRecordContainer
for(int i=0; i<_children.length; i++) { for(int i=0; i<_children.length; i++) {
if(_children[i] instanceof FontCollection) { if(_children[i] instanceof FontCollection) {
fontCollection = (FontCollection)_children[i]; fontCollection = (FontCollection)_children[i];
} } else if (_children[i] instanceof TxMasterStyleAtom){
txmaster = (TxMasterStyleAtom)_children[i];
}
} }
if(fontCollection == null) { if(fontCollection == null) {
@ -64,6 +68,9 @@ public class Environment extends PositionDependentRecordContainer
} }
} }
public TxMasterStyleAtom getTxMasterStyleAtom(){
return txmaster;
}
/** /**
* We are of type 1010 * We are of type 1010

View File

@ -0,0 +1,111 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.record;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import org.apache.poi.util.LittleEndian;
/**
* Master slide
*
* @author Yegor Kozlov
*/
public class MainMaster extends PositionDependentRecordContainer
{
private byte[] _header;
private static long _type = 1016;
// Links to our more interesting children
private SlideAtom slideAtom;
private PPDrawing ppDrawing;
private TxMasterStyleAtom[] txmasters;
private ColorSchemeAtom[] clrscheme;
private ColorSchemeAtom _colorScheme;
/**
* Returns the SlideAtom of this Slide
*/
public SlideAtom getSlideAtom() { return slideAtom; }
/**
* Returns the PPDrawing of this Slide, which has all the
* interesting data in it
*/
public PPDrawing getPPDrawing() { return ppDrawing; }
public TxMasterStyleAtom[] getTxMasterStyleAtoms() { return txmasters; }
public ColorSchemeAtom[] getColorSchemeAtoms() { return clrscheme; }
/**
* Set things up, and find our more interesting children
*/
protected MainMaster(byte[] source, int start, int len) {
// Grab the header
_header = new byte[8];
System.arraycopy(source,start,_header,0,8);
// Find our children
_children = Record.findChildRecords(source,start+8,len-8);
ArrayList tx = new ArrayList();
ArrayList clr = new ArrayList();
// Find the interesting ones in there
for(int i=0; i<_children.length; i++) {
if(_children[i] instanceof SlideAtom) {
slideAtom = (SlideAtom)_children[i];
} else if(_children[i] instanceof PPDrawing) {
ppDrawing = (PPDrawing)_children[i];
} else if(_children[i] instanceof TxMasterStyleAtom) {
tx.add(_children[i]);
} else if(_children[i] instanceof ColorSchemeAtom) {
clr.add(_children[i]);
}
if(ppDrawing != null && _children[i] instanceof ColorSchemeAtom) {
_colorScheme = (ColorSchemeAtom)_children[i];
}
}
txmasters = (TxMasterStyleAtom[])tx.toArray(new TxMasterStyleAtom[tx.size()]);
clrscheme = (ColorSchemeAtom[])clr.toArray(new ColorSchemeAtom[clr.size()]);
}
/**
* We are of type 1016
*/
public long getRecordType() { return _type; }
/**
* Write the contents of the record back, so it can be written
* to disk
*/
public void writeOut(OutputStream out) throws IOException {
writeOut(_header[0],_header[1],_type,_children,out);
}
public ColorSchemeAtom getColorScheme(){
return _colorScheme;
}
}

View File

@ -44,7 +44,7 @@ public class RecordTypes {
public static final Type Environment = new Type(1010,Environment.class); public static final Type Environment = new Type(1010,Environment.class);
public static final Type SlidePersistAtom = new Type(1011,SlidePersistAtom.class); public static final Type SlidePersistAtom = new Type(1011,SlidePersistAtom.class);
public static final Type SSlideLayoutAtom = new Type(1015,null); public static final Type SSlideLayoutAtom = new Type(1015,null);
public static final Type MainMaster = new Type(1016,DummyPositionSensitiveRecordWithChildren.class); public static final Type MainMaster = new Type(1016,MainMaster.class);
public static final Type SSSlideInfoAtom = new Type(1017,null); public static final Type SSSlideInfoAtom = new Type(1017,null);
public static final Type SlideViewInfo = new Type(1018,null); public static final Type SlideViewInfo = new Type(1018,null);
public static final Type GuideAtom = new Type(1019,null); public static final Type GuideAtom = new Type(1019,null);

View File

@ -38,6 +38,7 @@ public class Slide extends PositionDependentRecordContainer
// Links to our more interesting children // Links to our more interesting children
private SlideAtom slideAtom; private SlideAtom slideAtom;
private PPDrawing ppDrawing; private PPDrawing ppDrawing;
private ColorSchemeAtom _colorScheme;
/** /**
* Returns the SlideAtom of this Slide * Returns the SlideAtom of this Slide
@ -67,9 +68,13 @@ public class Slide extends PositionDependentRecordContainer
if(_children[i] instanceof SlideAtom) { if(_children[i] instanceof SlideAtom) {
slideAtom = (SlideAtom)_children[i]; slideAtom = (SlideAtom)_children[i];
} }
if(_children[i] instanceof PPDrawing) { else if(_children[i] instanceof PPDrawing) {
ppDrawing = (PPDrawing)_children[i]; ppDrawing = (PPDrawing)_children[i];
} }
if(ppDrawing != null && _children[i] instanceof ColorSchemeAtom) {
_colorScheme = (ColorSchemeAtom)_children[i];
}
} }
} }
@ -107,4 +112,8 @@ public class Slide extends PositionDependentRecordContainer
public void writeOut(OutputStream out) throws IOException { public void writeOut(OutputStream out) throws IOException {
writeOut(_header[0],_header[1],_type,_children,out); writeOut(_header[0],_header[1],_type,_children,out);
} }
public ColorSchemeAtom getColorScheme(){
return _colorScheme;
}
} }

View File

@ -49,6 +49,8 @@ public class SlideAtom extends RecordAtom
/** Get the ID of the master slide used. 0 if this is a master slide, otherwise -2147483648 */ /** Get the ID of the master slide used. 0 if this is a master slide, otherwise -2147483648 */
public int getMasterID() { return masterID; } public int getMasterID() { return masterID; }
/** Change slide master. */
public void setMasterID(int id) { masterID = id; }
/** Get the ID of the notes for this slide. 0 if doesn't have one */ /** Get the ID of the notes for this slide. 0 if doesn't have one */
public int getNotesID() { return notesID; } public int getNotesID() { return notesID; }
/** Get the embeded SSlideLayoutAtom */ /** Get the embeded SSlideLayoutAtom */

View File

@ -20,9 +20,12 @@
package org.apache.poi.hslf.usermodel; package org.apache.poi.hslf.usermodel;
import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.Sheet;
import org.apache.poi.hslf.model.SlideMaster;
import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp; import org.apache.poi.hslf.record.StyleTextPropAtom.CharFlagsTextProp;
import org.apache.poi.hslf.record.StyleTextPropAtom.TextProp; import org.apache.poi.hslf.record.StyleTextPropAtom.TextProp;
import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection; import org.apache.poi.hslf.record.StyleTextPropAtom.TextPropCollection;
import org.apache.poi.hslf.record.ColorSchemeAtom;
import java.awt.*; import java.awt.*;
@ -47,7 +50,8 @@ public class RichTextRun
/** How long a string (in the parent TextRun) we represent */ /** How long a string (in the parent TextRun) we represent */
private int length; private int length;
/** private String _fontname;
/**
* Our paragraph and character style. * Our paragraph and character style.
* Note - we may share these styles with other RichTextRuns * Note - we may share these styles with other RichTextRuns
*/ */
@ -56,7 +60,6 @@ public class RichTextRun
private boolean sharingParagraphStyle; private boolean sharingParagraphStyle;
private boolean sharingCharacterStyle; private boolean sharingCharacterStyle;
private String _fontname;
/** /**
* Create a new wrapper around a (currently not) * Create a new wrapper around a (currently not)
* rich text string * rich text string
@ -158,14 +161,20 @@ public class RichTextRun
* text property won't be set if there's no CharFlagsTextProp. * text property won't be set if there's no CharFlagsTextProp.
*/ */
private boolean isCharFlagsTextPropVal(int index) { private boolean isCharFlagsTextPropVal(int index) {
if(characterStyle == null) { return false; } CharFlagsTextProp cftp = null;
if (characterStyle != null){
cftp = (CharFlagsTextProp)characterStyle.findByName("char_flags");
}
if (cftp == null){
Sheet sheet = parentRun.getSheet();
int txtype = parentRun.getRunType();
SlideMaster master = (SlideMaster)sheet.getMasterSheet();
cftp = (CharFlagsTextProp)master.getStyleAttribute(txtype, getIndentLevel(), "char_flags", true);
}
CharFlagsTextProp cftp = (CharFlagsTextProp) return cftp == null ? false : cftp.getSubValue(index);
characterStyle.findByName("char_flags");
if(cftp == null) { return false; }
return cftp.getSubValue(index);
} }
/** /**
* Set the value of the given flag in the CharFlagsTextProp, adding * Set the value of the given flag in the CharFlagsTextProp, adding
* it if required. * it if required.
@ -204,11 +213,18 @@ public class RichTextRun
* Master Sheet will apply. * Master Sheet will apply.
*/ */
private int getCharTextPropVal(String propName) { private int getCharTextPropVal(String propName) {
if(characterStyle == null) { return -1; } TextProp prop = null;
if (characterStyle != null){
prop = characterStyle.findByName(propName);
}
TextProp cTextProp = characterStyle.findByName(propName); if (prop == null){
if(cTextProp == null) { return -1; } Sheet sheet = parentRun.getSheet();
return cTextProp.getValue(); int txtype = parentRun.getRunType();
SlideMaster master = (SlideMaster)sheet.getMasterSheet();
prop = master.getStyleAttribute(txtype, getIndentLevel(), propName, true);
}
return prop == null ? -1 : prop.getValue();
} }
/** /**
* Fetch the value of the given Paragraph related TextProp. * Fetch the value of the given Paragraph related TextProp.
@ -217,11 +233,18 @@ public class RichTextRun
* Master Sheet will apply. * Master Sheet will apply.
*/ */
private int getParaTextPropVal(String propName) { private int getParaTextPropVal(String propName) {
if(paragraphStyle == null) { return -1; } TextProp prop = null;
if (paragraphStyle != null){
prop = paragraphStyle.findByName(propName);
}
if (prop == null){
Sheet sheet = parentRun.getSheet();
int txtype = parentRun.getRunType();
SlideMaster master = (SlideMaster)sheet.getMasterSheet();
prop = master.getStyleAttribute(txtype, getIndentLevel(), propName, false);
}
TextProp pTextProp = paragraphStyle.findByName(propName); return prop == null ? -1 : prop.getValue();
if(pTextProp == null) { return -1; }
return pTextProp.getValue();
} }
/** /**
@ -290,27 +313,40 @@ public class RichTextRun
if (slideShow == null) { if (slideShow == null) {
//we can't set font since slideshow is not assigned yet //we can't set font since slideshow is not assigned yet
_fontname = fontName; _fontname = fontName;
} else{ } else {
// Get the index for this font (adding if needed) // Get the index for this font (adding if needed)
int fontIdx = slideShow.getFontCollection().addFont(fontName); int fontIdx = slideShow.getFontCollection().addFont(fontName);
setCharTextPropVal("font.index", fontIdx); setCharTextPropVal("font.index", fontIdx);
} }
} }
public String getFontName() { public String getFontName() {
int fontIdx = getCharTextPropVal("font.index"); if (slideShow == null) {
if(fontIdx == -1) { return null; } return _fontname;
return slideShow.getFontCollection().getFontWithId(fontIdx); } else {
int fontIdx = getCharTextPropVal("font.index");
if(fontIdx == -1) { return null; }
return slideShow.getFontCollection().getFontWithId(fontIdx);
}
} }
/** /**
* @return font color as RGB value * @return font color as RGB value
* @see java.awt.Color * @see java.awt.Color
*/ */
public int getFontColor() { public Color getFontColor() {
return getCharTextPropVal("font.color"); int rgb = getCharTextPropVal("font.color");
if (rgb >= 0x8000000) {
int idx = rgb % 0x8000000;
ColorSchemeAtom ca = parentRun.getSheet().getColorScheme();
if(idx >= 0 && idx <= 7) rgb = ca.getColor(idx);
}
Color tmp = new Color(rgb, true);
return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
} }
/** /**
* Sets color of the text, as a RGB value * Sets color of the text, as a int rgb
* @see java.awt.Color * @see java.awt.Color
*/ */
public void setFontColor(int rgb) { public void setFontColor(int rgb) {

View File

@ -80,11 +80,10 @@ public class SlideShow
private Document _documentRecord; private Document _documentRecord;
// Friendly objects for people to deal with // Friendly objects for people to deal with
private SlideMaster[] _masters;
private Slide[] _slides; private Slide[] _slides;
private Notes[] _notes; private Notes[] _notes;
private FontCollection _fonts; private FontCollection _fonts;
// MetaSheets (eg masters) not yet supported
// private MetaSheets[] _msheets;
/* =============================================================== /* ===============================================================
@ -305,6 +304,18 @@ public class SlideShow
SlideListWithText slidesSLWT = _documentRecord.getSlideSlideListWithText(); SlideListWithText slidesSLWT = _documentRecord.getSlideSlideListWithText();
SlideListWithText notesSLWT = _documentRecord.getNotesSlideListWithText(); SlideListWithText notesSLWT = _documentRecord.getNotesSlideListWithText();
//find master slides
SlideAtomsSet[] masterSets = new SlideAtomsSet[0];
org.apache.poi.hslf.record.MainMaster[] masterRecords = null;
if (masterSLWT != null){
masterSets = masterSLWT.getSlideAtomsSets();
masterRecords = new org.apache.poi.hslf.record.MainMaster[masterSets.length];
for(int i=0; i<masterRecords.length; i++) {
masterRecords[i] = (org.apache.poi.hslf.record.MainMaster)getCoreRecordForSAS(masterSets[i]);
}
}
// Start by finding the notes records to go with the entries in // Start by finding the notes records to go with the entries in
// notesSLWT // notesSLWT
org.apache.poi.hslf.record.Notes[] notesRecords; org.apache.poi.hslf.record.Notes[] notesRecords;
@ -359,6 +370,14 @@ public class SlideShow
} }
// Finally, generate model objects for everything // Finally, generate model objects for everything
_masters = new SlideMaster[masterRecords.length];
for(int i=0; i<_masters.length; i++) {
SlideAtomsSet sas = masterSets[i];
int sheetNo = sas.getSlidePersistAtom().getSlideIdentifier();
_masters[i] = new SlideMaster(masterRecords[i], sheetNo);
_masters[i].setSlideShow(this);
}
// Notes first // Notes first
_notes = new Notes[notesRecords.length]; _notes = new Notes[notesRecords.length];
for(int i=0; i<_notes.length; i++) { for(int i=0; i<_notes.length; i++) {
@ -420,10 +439,9 @@ public class SlideShow
public Notes[] getNotes() { return _notes; } public Notes[] getNotes() { return _notes; }
/** /**
* Returns an array of all the meta Sheets (master sheets etc) * Returns an array of all the normal Slides found in the slideshow
* found in the slideshow
*/ */
//public MetaSheet[] getMetaSheets() { return _msheets; } public SlideMaster[] getSlidesMasters() { return _masters; }
/** /**
* Returns the data of all the pictures attached to the SlideShow * Returns the data of all the pictures attached to the SlideShow

View File

@ -0,0 +1,133 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.model;
import junit.framework.*;
import java.io.FileOutputStream;
import java.awt.*;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.HSLFSlideShow;
/**
* Test Line shape.
*
* @author Yegor Kozlov
*/
public class TestLine extends TestCase {
public void setUp() throws Exception {
}
public void testCreateLines() throws Exception {
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
slide.addTitle().setText("Lines tester");
Line line;
/**
* line styles
*/
line = new Line();
line.setAnchor(new Rectangle(75, 200, 300, 0));
line.setLineStyle(Line.LINE_SIMPLE);
line.setLineColor(Color.blue);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 230, 300, 0));
line.setLineStyle(Line.LINE_DOUBLE);
line.setLineWidth(3.5);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 260, 300, 0));
line.setLineStyle(Line.LINE_TRIPLE);
line.setLineWidth(6);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 290, 300, 0));
line.setLineStyle(Line.LINE_THICKTHIN);
line.setLineWidth(4.5);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 320, 300, 0));
line.setLineStyle(Line.LINE_THINTHICK);
line.setLineWidth(5.5);
slide.addShape(line);
/**
* line dashing
*/
line = new Line();
line.setAnchor(new java.awt.Rectangle(450, 200, 300, 0));
line.setLineDashing(Line.PEN_SOLID);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(450, 230, 300, 0));
line.setLineDashing(Line.PEN_PS_DASH);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(450, 260, 300, 0));
line.setLineDashing(Line.PEN_DOT);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(450, 290, 300, 0));
line.setLineDashing(Line.PEN_DOTGEL);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(450, 320, 300, 0));
line.setLineDashing(Line.PEN_LONGDASHDOTDOTGEL);
slide.addShape(line);
/**
* Combinations
*/
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 400, 300, 0));
line.setLineDashing(Line.PEN_DASHDOT);
line.setLineStyle(Line.LINE_TRIPLE);
line.setLineWidth(5.0);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 430, 300, 0));
line.setLineDashing(Line.PEN_DASH);
line.setLineStyle(Line.LINE_THICKTHIN);
line.setLineWidth(4.0);
slide.addShape(line);
line = new Line();
line.setAnchor(new java.awt.Rectangle(75, 460, 300, 0));
line.setLineDashing(Line.PEN_DOT);
line.setLineStyle(Line.LINE_DOUBLE);
line.setLineWidth(8.0);
slide.addShape(line);
}
}

View File

@ -46,15 +46,16 @@ public class TestSetBoldItalic extends TestCase {
// Create a new textbox, and give it lots of properties // Create a new textbox, and give it lots of properties
TextBox txtbox = new TextBox(); TextBox txtbox = new TextBox();
rt = txtbox.getTextRun().getRichTextRuns()[0];
txtbox.setText(val); txtbox.setText(val);
txtbox.setFontSize(42); rt.setFontSize(42);
txtbox.setBold(true); rt.setBold(true);
txtbox.setItalic(true); rt.setItalic(true);
txtbox.setUnderline(false); rt.setUnderlined(false);
sl.addShape(txtbox); sl.addShape(txtbox);
// Check it before save // Check it before save
rt = txtbox.getRichTextRuns()[0]; rt = txtbox.getTextRun().getRichTextRuns()[0];
assertEquals(val, rt.getText()); assertEquals(val, rt.getText());
assertEquals(42, rt.getFontSize()); assertEquals(42, rt.getFontSize());
assertTrue(rt.isBold()); assertTrue(rt.isBold());
@ -69,7 +70,7 @@ public class TestSetBoldItalic extends TestCase {
sl = ppt.getSlides()[0]; sl = ppt.getSlides()[0];
txtbox = (TextBox)sl.getShapes()[0]; txtbox = (TextBox)sl.getShapes()[0];
rt = txtbox.getRichTextRuns()[0]; rt = txtbox.getTextRun().getRichTextRuns()[0];
// Check after save // Check after save
assertEquals(val, rt.getText()); assertEquals(val, rt.getText());

View File

@ -50,9 +50,8 @@ public class TestShapes extends TestCase {
Line line = new Line(); Line line = new Line();
java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100, 200, 50, 60); java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100, 200, 50, 60);
line.setAnchor(lineAnchor); line.setAnchor(lineAnchor);
System.out.println(line.getAnchor());
line.setLineWidth(3); line.setLineWidth(3);
line.setLineStyle(Line.LineDashSys); line.setLineStyle(Line.PEN_DASH);
line.setLineColor(Color.red); line.setLineColor(Color.red);
slide.addShape(line); slide.addShape(line);
@ -60,7 +59,7 @@ public class TestShapes extends TestCase {
java.awt.Rectangle ellipseAnchor = new Rectangle(320, 154, 55, 111); java.awt.Rectangle ellipseAnchor = new Rectangle(320, 154, 55, 111);
ellipse.setAnchor(ellipseAnchor); ellipse.setAnchor(ellipseAnchor);
ellipse.setLineWidth(2); ellipse.setLineWidth(2);
ellipse.setLineStyle(Line.LineSolid); ellipse.setLineStyle(Line.PEN_SOLID);
ellipse.setLineColor(Color.green); ellipse.setLineColor(Color.green);
ellipse.setFillColor(Color.lightGray); ellipse.setFillColor(Color.lightGray);
slide.addShape(ellipse); slide.addShape(ellipse);
@ -101,8 +100,8 @@ public class TestShapes extends TestCase {
String text = txtbox.getText(); String text = txtbox.getText();
assertNotNull(text); assertNotNull(text);
assertEquals(txtbox.getRichTextRuns().length, 1); assertEquals(txtbox.getTextRun().getRichTextRuns().length, 1);
RichTextRun rt = txtbox.getRichTextRuns()[0]; RichTextRun rt = txtbox.getTextRun().getRichTextRuns()[0];
if (text.equals("Hello, World!!!")){ if (text.equals("Hello, World!!!")){
assertEquals(32, rt.getFontSize()); assertEquals(32, rt.getFontSize());
@ -135,24 +134,25 @@ public class TestShapes extends TestCase {
// Create a new textbox, and give it lots of properties // Create a new textbox, and give it lots of properties
TextBox txtbox = new TextBox(); TextBox txtbox = new TextBox();
rt = txtbox.getTextRun().getRichTextRuns()[0];
txtbox.setText(val); txtbox.setText(val);
txtbox.setFontName("Arial"); rt.setFontName("Arial");
txtbox.setFontSize(42); rt.setFontSize(42);
txtbox.setBold(true); rt.setBold(true);
txtbox.setItalic(true); rt.setItalic(true);
txtbox.setUnderline(false); rt.setUnderlined(false);
txtbox.setFontColor(Color.red); rt.setFontColor(Color.red);
sl.addShape(txtbox); sl.addShape(txtbox);
// Check it before save // Check it before save
rt = txtbox.getRichTextRuns()[0]; rt = txtbox.getTextRun().getRichTextRuns()[0];
assertEquals(val, rt.getText()); assertEquals(val, rt.getText());
assertEquals(42, rt.getFontSize()); assertEquals(42, rt.getFontSize());
assertTrue(rt.isBold()); assertTrue(rt.isBold());
assertTrue(rt.isItalic()); assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined()); assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontName()); assertEquals("Arial", rt.getFontName());
assertEquals(Color.red, txtbox.getFontColor()); assertEquals(Color.red, rt.getFontColor());
// Serialize and read again // Serialize and read again
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
@ -162,7 +162,7 @@ public class TestShapes extends TestCase {
ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()))); ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
txtbox = (TextBox)sl.getShapes()[0]; txtbox = (TextBox)sl.getShapes()[0];
rt = txtbox.getRichTextRuns()[0]; rt = txtbox.getTextRun().getRichTextRuns()[0];
// Check after save // Check after save
assertEquals(val, rt.getText()); assertEquals(val, rt.getText());
@ -171,7 +171,7 @@ public class TestShapes extends TestCase {
assertTrue(rt.isItalic()); assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined()); assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontName()); assertEquals("Arial", rt.getFontName());
assertEquals(Color.red, txtbox.getFontColor()); assertEquals(Color.red, rt.getFontColor());
} }
/** /**

View File

@ -0,0 +1,201 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.model;
import junit.framework.TestCase;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.record.StyleTextPropAtom.*;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
/**
* Tests for SlideMaster
*
* @author Yegor Kozlov
*/
public class TestSlideMaster extends TestCase{
String home;
public void setUp() throws Exception {
home = System.getProperty("HSLF.testdata.path");
}
/**
* The reference ppt has two masters.
* Check we can read their attributes.
*/
public void testSlideMaster() throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow(home + "/slide_master.ppt"));
Environment env = ppt.getDocumentRecord().getEnvironment();
SlideMaster[] master = ppt.getSlidesMasters();
assertEquals(2, master.length);
//character attributes
assertEquals(40, master[0].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.size", true).getValue());
assertEquals(48, master[1].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.size", true).getValue());
int font1 = master[0].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.index", true).getValue();
int font2 = master[1].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "font.index", true).getValue();
assertEquals("Arial", env.getFontCollection().getFontWithId(font1));
assertEquals("Georgia", env.getFontCollection().getFontWithId(font2));
CharFlagsTextProp prop1 = (CharFlagsTextProp)master[0].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "char_flags", true);
assertEquals(false, prop1.getSubValue(CharFlagsTextProp.BOLD_IDX));
assertEquals(false, prop1.getSubValue(CharFlagsTextProp.ITALIC_IDX));
assertEquals(true, prop1.getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
CharFlagsTextProp prop2 = (CharFlagsTextProp)master[1].getStyleAttribute(TextHeaderAtom.TITLE_TYPE, 0, "char_flags", true);
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.BOLD_IDX));
assertEquals(true, prop2.getSubValue(CharFlagsTextProp.ITALIC_IDX));
assertEquals(false, prop2.getSubValue(CharFlagsTextProp.UNDERLINE_IDX));
//now paragraph attributes
assertEquals(0x266B, master[0].getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false).getValue());
assertEquals(0x2022, master[1].getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.char", false).getValue());
int b1 = master[0].getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.font", false).getValue();
int b2 = master[1].getStyleAttribute(TextHeaderAtom.BODY_TYPE, 0, "bullet.font", false).getValue();
assertEquals("Arial", env.getFontCollection().getFontWithId(b1));
assertEquals("Georgia", env.getFontCollection().getFontWithId(b2));
}
/**
* If a style attribute is not set ensure it is read from the master
*/
public void testMasterAttributes() throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow(home + "/slide_master.ppt"));
Slide[] slide = ppt.getSlides();
assertEquals(2, slide.length);
TextRun[] trun;
trun = slide[0].getTextRuns();
for (int i = 0; i < trun.length; i++) {
if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE){
RichTextRun rt = trun[i].getRichTextRuns()[0];
assertEquals(40, rt.getFontSize());
assertEquals(true, rt.isUnderlined());
assertEquals("Arial", rt.getFontName());
} else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE){
RichTextRun rt;
rt = trun[i].getRichTextRuns()[0];
assertEquals(0, rt.getIndentLevel());
assertEquals(32, rt.getFontSize());
assertEquals("Arial", rt.getFontName());
rt = trun[i].getRichTextRuns()[1];
assertEquals(1, rt.getIndentLevel());
assertEquals(28, rt.getFontSize());
assertEquals("Arial", rt.getFontName());
}
}
trun = slide[1].getTextRuns();
for (int i = 0; i < trun.length; i++) {
if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE){
RichTextRun rt = trun[i].getRichTextRuns()[0];
assertEquals(48, rt.getFontSize());
assertEquals(true, rt.isItalic());
assertEquals("Georgia", rt.getFontName());
} else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE){
RichTextRun rt;
rt = trun[i].getRichTextRuns()[0];
assertEquals(0, rt.getIndentLevel());
assertEquals(32, rt.getFontSize());
assertEquals("Courier New", rt.getFontName());
}
}
}
/**
* Check we can dynamically assign a slide master to a slide.
*/
public void testChangeSlideMaster() throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow(home + "/slide_master.ppt"));
SlideMaster[] master = ppt.getSlidesMasters();
Slide[] slide = ppt.getSlides();
int sheetNo;
//each slide uses its own master
assertEquals(slide[0].getMasterSheet()._getSheetNumber(), master[0]._getSheetNumber());
assertEquals(slide[1].getMasterSheet()._getSheetNumber(), master[1]._getSheetNumber());
//all slides use the first master slide
sheetNo = master[0]._getSheetNumber();
for (int i = 0; i < slide.length; i++) {
slide[i].setMasterSheet(master[0]);
}
ByteArrayOutputStream out;
out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
master = ppt.getSlidesMasters();
slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
assertEquals(sheetNo, slide[i].getMasterSheet()._getSheetNumber());
}
}
/**
* Varify we can read attrubutes for different identtation levels.
* (typical for the "bullted body" placeholder)
*/
public void testIndentation() throws Exception {
SlideShow ppt = new SlideShow(new HSLFSlideShow(home + "/slide_master.ppt"));
Slide slide = ppt.getSlides()[0];
TextRun[] trun;
trun = slide.getTextRuns();
for (int i = 0; i < trun.length; i++) {
if (trun[i].getRunType() == TextHeaderAtom.TITLE_TYPE){
RichTextRun rt = trun[i].getRichTextRuns()[0];
assertEquals(40, rt.getFontSize());
assertEquals(true, rt.isUnderlined());
assertEquals("Arial", rt.getFontName());
} else if (trun[i].getRunType() == TextHeaderAtom.BODY_TYPE){
RichTextRun[] rt = trun[i].getRichTextRuns();
for (int j = 0; j < rt.length; j++) {
int indent = rt[j].getIndentLevel();
switch (indent){
case 0:
assertEquals(32, rt[j].getFontSize());
break;
case 1:
assertEquals(28, rt[j].getFontSize());
break;
case 2:
assertEquals(24, rt[j].getFontSize());
break;
}
}
}
}
}
}

View File

@ -8,6 +8,7 @@ import java.io.File;
import org.apache.poi.hslf.HSLFSlideShow; import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide; import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.SlideMaster;
import org.apache.poi.hslf.record.Record; import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.SlideListWithText; import org.apache.poi.hslf.record.SlideListWithText;
@ -114,7 +115,8 @@ public class TestRichTextRun extends TestCase {
* rich text runs * rich text runs
*/ */
public void testFontSize() throws Exception { public void testFontSize() throws Exception {
Slide slideOne = ss.getSlides()[0]; SlideMaster master;
Slide slideOne = ss.getSlides()[0];
TextRun[] textRuns = slideOne.getTextRuns(); TextRun[] textRuns = slideOne.getTextRuns();
RichTextRun rtr = textRuns[0].getRichTextRuns()[0]; RichTextRun rtr = textRuns[0].getRichTextRuns()[0];
@ -124,13 +126,16 @@ public class TestRichTextRun extends TestCase {
RichTextRun rtrRb = textRunsR[1].getRichTextRuns()[0]; RichTextRun rtrRb = textRunsR[1].getRichTextRuns()[0];
RichTextRun rtrRc = textRunsR[1].getRichTextRuns()[3]; RichTextRun rtrRc = textRunsR[1].getRichTextRuns()[3];
String defaultFont = "Arial";
// Start off with rich one // Start off with rich one
// First run has defaults // First run has defaults
assertEquals(-1, rtrRa.getFontSize()); assertEquals(44, rtrRa.getFontSize());
assertEquals(null, rtrRa.getFontName()); assertEquals(defaultFont, rtrRa.getFontName());
// Second is size 20, default font // Second is size 20, default font
assertEquals(20, rtrRb.getFontSize()); assertEquals(20, rtrRb.getFontSize());
assertEquals(null, rtrRb.getFontName()); assertEquals(defaultFont, rtrRb.getFontName());
// Third is size 24, alt font // Third is size 24, alt font
assertEquals(24, rtrRc.getFontSize()); assertEquals(24, rtrRc.getFontSize());
assertEquals("Times New Roman", rtrRc.getFontName()); assertEquals("Times New Roman", rtrRc.getFontName());
@ -145,8 +150,8 @@ public class TestRichTextRun extends TestCase {
// Now do non rich one // Now do non rich one
assertEquals(-1, rtr.getFontSize()); assertEquals(44, rtr.getFontSize());
assertEquals(null, rtr.getFontName()); assertEquals(defaultFont, rtr.getFontName());
assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default
assertNull(rtr._getRawCharacterStyle()); assertNull(rtr._getRawCharacterStyle());
assertNull(rtr._getRawParagraphStyle()); assertNull(rtr._getRawParagraphStyle());
@ -154,7 +159,7 @@ public class TestRichTextRun extends TestCase {
// Change Font size // Change Font size
rtr.setFontSize(99); rtr.setFontSize(99);
assertEquals(99, rtr.getFontSize()); assertEquals(99, rtr.getFontSize());
assertEquals(null, rtr.getFontName()); assertEquals(defaultFont, rtr.getFontName());
assertNotNull(rtr._getRawCharacterStyle()); assertNotNull(rtr._getRawCharacterStyle());
assertNotNull(rtr._getRawParagraphStyle()); assertNotNull(rtr._getRawParagraphStyle());
assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default assertEquals(1, ss.getFontCollection().getChildRecords().length); // Default