Index: src/documentation/content/xdocs/hslf/how-to-shapes.xml =================================================================== --- src/documentation/content/xdocs/hslf/how-to-shapes.xml (revision 479621) +++ src/documentation/content/xdocs/hslf/how-to-shapes.xml (working copy) @@ -19,6 +19,7 @@
true
if the slide follows master,
+ * false
otherwise
+ */
+ public void setFollowMasterBackground(boolean flag){
+ SlideAtom sa = _slide.getSlideAtom();
+ sa.setFollowMasterBackground(flag);
+ }
+
+ /**
+ * Whether this slide follows master sheet background
+ *
+ * @return true
if the slide follows master background,
+ * false
otherwise
+ */
+ public boolean getFollowMasterBackground(){
+ SlideAtom sa = _slide.getSlideAtom();
+ return sa.getFollowMasterBackground();
+ }
}
Index: src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
===================================================================
--- src/scratchpad/src/org/apache/poi/hslf/model/Shape.java (revision 479621)
+++ src/scratchpad/src/org/apache/poi/hslf/model/Shape.java (working copy)
@@ -17,7 +17,10 @@
import org.apache.poi.ddf.*;
import org.apache.poi.hslf.model.ShapeTypes;
+import org.apache.poi.hslf.record.ColorSchemeAtom;
+
import java.util.Iterator;
+import java.awt.*;
/**
*
@@ -282,4 +285,23 @@
_sheet = sheet;
}
+ protected Color getColor(int rgb){
+ 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);
+ return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
+ }
+
+ /**
+ * Fill properties of this shape
+ *
+ * @return fill properties of this shape
+ */
+ public Fill getFill(){
+ return new Fill(this);
+ }
+
}
Index: src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java
===================================================================
--- src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java (revision 479621)
+++ src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java (working copy)
@@ -19,7 +19,12 @@
import org.apache.poi.hslf.record.*;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.record.StyleTextPropAtom.*;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherRecord;
+import java.util.List;
+import java.util.Iterator;
+
/**
* 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,
@@ -32,6 +37,7 @@
private int _sheetNo;
private MainMaster _master;
private TextRun[] _runs;
+ private Background _background;
/**
* all TxMasterStyleAtoms available in this master
@@ -143,4 +149,30 @@
return _master.getColorScheme();
}
+ /**
+ * Returns the background shape for this sheet.
+ *
+ * @return the background shape for this sheet.
+ */
+ public Background getBackground(){
+ if (_background == null){
+ PPDrawing ppdrawing = getPPDrawing();
+
+ EscherContainerRecord dg = (EscherContainerRecord)ppdrawing.getEscherRecords()[0];
+ EscherContainerRecord spContainer = null;
+ List ch = dg.getChildRecords();
+
+ for (Iterator it = ch.iterator(); it.hasNext();) {
+ EscherRecord rec = (EscherRecord)it.next();
+ if (rec.getRecordId() == EscherContainerRecord.SP_CONTAINER){
+ spContainer = (EscherContainerRecord)rec;
+ break;
+ }
+ }
+ _background = new Background(spContainer, null);
+ _background.setSheet(this);
+ }
+ return _background;
+ }
+
}
Index: src/scratchpad/src/org/apache/poi/hslf/model/Background.java
===================================================================
--- src/scratchpad/src/org/apache/poi/hslf/model/Background.java (revision 0)
+++ src/scratchpad/src/org/apache/poi/hslf/model/Background.java (revision 0)
@@ -0,0 +1,37 @@
+
+/* ====================================================================
+ 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.ddf.EscherContainerRecord;
+
+/**
+ * Background shape
+ *
+ * @author Yegor Kozlov
+ */
+public class Background extends Shape {
+
+ protected Background(EscherContainerRecord escherRecord, Shape parent){
+ super(escherRecord, parent);
+ }
+
+ protected EscherContainerRecord createSpContainer(boolean isChild){
+ return null;
+ }
+
+}
Index: src/scratchpad/src/org/apache/poi/hslf/model/Fill.java
===================================================================
--- src/scratchpad/src/org/apache/poi/hslf/model/Fill.java (revision 0)
+++ src/scratchpad/src/org/apache/poi/hslf/model/Fill.java (revision 0)
@@ -0,0 +1,228 @@
+
+/* ====================================================================
+ 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.ddf.*;
+import org.apache.poi.hslf.record.*;
+import org.apache.poi.hslf.usermodel.PictureData;
+import org.apache.poi.hslf.usermodel.SlideShow;
+
+import java.awt.*;
+import java.util.*;
+
+/**
+ * Represents functionality provided by the 'Fill Effects' dialog in PowerPoint.
+ *
+ * @author Yegor Kozlov
+ */
+public class Fill {
+ /**
+ * Fill with a solid color
+ */
+ public static final int FILL_SOLID = 0;
+
+ /**
+ * Fill with a pattern (bitmap)
+ */
+ public static final int FILL_PATTERN = 1;
+
+ /**
+ * A texture (pattern with its own color map)
+ */
+ public static final int FILL_TEXTURE = 2;
+
+ /**
+ * Center a picture in the shape
+ */
+ public static final int FILL_PICTURE = 3;
+
+ /**
+ * Shade from start to end points
+ */
+ public static final int FILL_SHADE = 4;
+
+ /**
+ * Shade from bounding rectangle to end point
+ */
+ public static final int FILL_SHADE_CENTER = 5;
+
+ /**
+ * Shade from shape outline to end point
+ */
+ public static final int FILL_SHADE_SHAPE = 6;
+
+ /**
+ * Similar to FILL_SHADE, but the fill angle
+ * is additionally scaled by the aspect ratio of
+ * the shape. If shape is square, it is the same as FILL_SHADE
+ */
+ public static final int FILL_SHADE_SCALE = 7;
+
+ /**
+ * shade to title
+ */
+ public static final int FILL_SHADE_TITLE = 8;
+
+ /**
+ * Use the background fill color/pattern
+ */
+ public static final int FILL_BACKGROUND = 9;
+
+
+
+ /**
+ * The shape this background applies to
+ */
+ protected Shape shape;
+
+ /**
+ * Construct a Fill
object for a shape.
+ * Fill information will be read from shape's escher properties.
+ *
+ * @param shape the shape this background applies to
+ */
+ public Fill(Shape shape){
+ this.shape = shape;
+ }
+
+ /**
+ * Returns fill type.
+ * Must be one of the FILL_*
constants defined in this class.
+ *
+ * @return type of fill
+ */
+ public int getFillType(){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ EscherSimpleProperty prop = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLTYPE);
+ return prop == null ? FILL_SOLID : prop.getPropertyValue();
+ }
+
+ /**
+ * Sets fill type.
+ * Must be one of the FILL_*
constants defined in this class.
+ *
+ * @param type type of the fill
+ */
+ public void setFillType(int type){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ Shape.setEscherProperty(opt, EscherProperties.FILL__FILLTYPE, type);
+ }
+
+ /**
+ * Foreground color
+ */
+ public Color getForegroundColor(){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ EscherSimpleProperty p1 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLCOLOR);
+ EscherSimpleProperty p2 = (EscherSimpleProperty)Shape.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();
+ clr = shape.getColor(rgb);
+ }
+ return clr;
+ }
+
+ /**
+ * Foreground color
+ */
+ public void setForegroundColor(Color color){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ if (color == null) {
+ Shape.setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, -1);
+ }
+ else {
+ int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
+ Shape.setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb);
+ }
+ }
+
+ /**
+ * Background color
+ */
+ public Color getBackgroundColor(){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ EscherSimpleProperty p1 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR);
+ EscherSimpleProperty p2 = (EscherSimpleProperty)Shape.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();
+ clr = shape.getColor(rgb);
+ }
+ return clr;
+ }
+
+ /**
+ * Background color
+ */
+ public void setBackgroundColor(Color color){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ if (color == null) {
+ Shape.setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, -1);
+ }
+ else {
+ int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
+ Shape.setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, rgb);
+ }
+ }
+
+ /**
+ * PictureData
object used in a texture, pattern of picture fill.
+ */
+ public PictureData getPictureData(){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ EscherSimpleProperty p = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__PATTERNTEXTURE);
+ if (p == null) return null;
+
+ SlideShow ppt = shape.getSheet().getSlideShow();
+ PictureData[] pict = ppt.getPictureData();
+ Document doc = ppt.getDocumentRecord();
+
+ EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
+ EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
+
+ java.util.List lst = bstore.getChildRecords();
+ int idx = p.getPropertyValue();
+ EscherBSERecord bse = (EscherBSERecord)lst.get(idx);
+ for ( int i = 0; i < pict.length; i++ ) {
+ if (pict[i].getOffset() == bse.getOffset()){
+ return pict[i];
+ }
+ }
+ throw new RuntimeException("Picture data not found: \n" +
+ " bse: " + bse + " at " + bse.getOffset() );
+
+ }
+
+ /**
+ * Assign picture used to fill the underlying shape.
+ *
+ * @param idx 0-based index of the picture added to this ppt by SlideShow.addPicture
method.
+ */
+ public void setPictureData(int idx){
+ EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
+ Shape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), idx);
+ }
+
+}
Index: src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java
===================================================================
--- src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java (revision 0)
+++ src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java (revision 0)
@@ -0,0 +1,182 @@
+/* ====================================================================
+ 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 java.io.*;
+import java.awt.*;
+
+import org.apache.poi.hslf.usermodel.SlideShow;
+import org.apache.poi.hslf.HSLFSlideShow;
+
+/**
+ * Test Fill
object.
+ *
+ * @author Yegor Kozlov
+ */
+public class TestBackground extends TestCase {
+
+ /**
+ * Default background for slide, shape and slide master.
+ */
+ public void testDefaults() throws Exception {
+ SlideShow ppt = new SlideShow();
+
+ assertEquals(Fill.FILL_SOLID, ppt.getSlidesMasters()[0].getBackground().getFill().getFillType());
+
+ Slide slide = ppt.createSlide();
+ assertTrue(slide.getFollowMasterBackground());
+ assertEquals(Fill.FILL_SOLID, slide.getBackground().getFill().getFillType());
+
+ Shape shape = new AutoShape(ShapeTypes.Rectangle);
+ assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
+ }
+
+ /**
+ * Read fill information from an reference ppt file
+ */
+ public void testReadBackground() throws Exception {
+ SlideShow ppt = new SlideShow(new HSLFSlideShow(System.getProperty("HSLF.testdata.path") + "/backgrounds.ppt"));
+ Fill fill;
+ Shape shape;
+
+ Slide[] slide = ppt.getSlides();
+
+ fill = slide[0].getBackground().getFill();
+ assertEquals(Fill.FILL_PICTURE, fill.getFillType());
+ shape = slide[0].getShapes()[0];
+ assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
+
+ fill = slide[1].getBackground().getFill();
+ assertEquals(Fill.FILL_PATTERN, fill.getFillType());
+ shape = slide[1].getShapes()[0];
+ assertEquals(Fill.FILL_BACKGROUND, shape.getFill().getFillType());
+
+ fill = slide[2].getBackground().getFill();
+ assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
+ shape = slide[2].getShapes()[0];
+ assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
+
+ fill = slide[3].getBackground().getFill();
+ assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
+ shape = slide[3].getShapes()[0];
+ assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
+ }
+
+ /**
+ * Create a ppt with various fill effects
+ */
+ public void testBackgroundPicture() throws Exception {
+ SlideShow ppt = new SlideShow();
+ Slide slide;
+ Fill fill;
+ Shape shape;
+ int idx;
+
+ //slide 1
+ slide = ppt.createSlide();
+ slide.setFollowMasterBackground(false);
+ fill = slide.getBackground().getFill();
+ idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
+ fill.setFillType(Fill.FILL_PICTURE);
+ fill.setPictureData(idx);
+
+ shape = new AutoShape(ShapeTypes.Rectangle);
+ shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
+ fill = shape.getFill();
+ fill.setFillType(Fill.FILL_SOLID);
+ slide.addShape(shape);
+
+ //slide 2
+ slide = ppt.createSlide();
+ slide.setFollowMasterBackground(false);
+ fill = slide.getBackground().getFill();
+ idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
+ fill.setFillType(Fill.FILL_PATTERN);
+ fill.setPictureData(idx);
+ fill.setBackgroundColor(Color.green);
+ fill.setForegroundColor(Color.red);
+
+ shape = new AutoShape(ShapeTypes.Rectangle);
+ shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
+ fill = shape.getFill();
+ fill.setFillType(Fill.FILL_BACKGROUND);
+ slide.addShape(shape);
+
+ //slide 3
+ slide = ppt.createSlide();
+ slide.setFollowMasterBackground(false);
+ fill = slide.getBackground().getFill();
+ idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
+ fill.setFillType(Fill.FILL_TEXTURE);
+ fill.setPictureData(idx);
+
+ shape = new AutoShape(ShapeTypes.Rectangle);
+ shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
+ fill = shape.getFill();
+ fill.setFillType(Fill.FILL_PICTURE);
+ idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/clock.jpg"), Picture.JPEG);
+ fill.setPictureData(idx);
+ slide.addShape(shape);
+
+ // slide 4
+ slide = ppt.createSlide();
+ slide.setFollowMasterBackground(false);
+ fill = slide.getBackground().getFill();
+ fill.setFillType(Fill.FILL_SHADE_CENTER);
+ fill.setBackgroundColor(Color.white);
+ fill.setForegroundColor(Color.darkGray);
+
+ shape = new AutoShape(ShapeTypes.Rectangle);
+ shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
+ fill = shape.getFill();
+ fill.setFillType(Fill.FILL_SHADE);
+ fill.setBackgroundColor(Color.red);
+ fill.setForegroundColor(Color.green);
+ slide.addShape(shape);
+
+ //serialize and read again
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ppt.write(out);
+ out.close();
+
+ ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
+ Slide[] slides = ppt.getSlides();
+
+ fill = slides[0].getBackground().getFill();
+ assertEquals(Fill.FILL_PICTURE, fill.getFillType());
+ shape = slides[0].getShapes()[0];
+ assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
+
+ fill = slides[1].getBackground().getFill();
+ assertEquals(Fill.FILL_PATTERN, fill.getFillType());
+ shape = slides[1].getShapes()[0];
+ assertEquals(Fill.FILL_BACKGROUND, shape.getFill().getFillType());
+
+ fill = slides[2].getBackground().getFill();
+ assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
+ shape = slides[2].getShapes()[0];
+ assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
+
+ fill = slides[3].getBackground().getFill();
+ assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
+ shape = slides[3].getShapes()[0];
+ assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
+
+ }
+
+}
Index: src/scratchpad/testcases/org/apache/poi/hslf/data/backgrounds.ppt
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: src\scratchpad\testcases\org\apache\poi\hslf\data\backgrounds.ppt
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream