diff --git a/src/documentation/content/xdocs/hslf/how-to-shapes.xml b/src/documentation/content/xdocs/hslf/how-to-shapes.xml index 8c90d2b8f0..cdd1605749 100644 --- a/src/documentation/content/xdocs/hslf/how-to-shapes.xml +++ b/src/documentation/content/xdocs/hslf/how-to-shapes.xml @@ -19,6 +19,7 @@
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);
+ }
+
+}
diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java b/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
index fe8788b97c..a4d5276345 100644
--- a/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
+++ b/src/scratchpad/src/org/apache/poi/hslf/model/Shape.java
@@ -17,7 +17,10 @@ package org.apache.poi.hslf.model;
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 @@ public abstract class Shape {
_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);
+ }
+
}
diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java b/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java
index eab57d8a02..b88aed9f4c 100644
--- a/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java
+++ b/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java
@@ -20,12 +20,17 @@
package org.apache.poi.hslf.model;
import java.util.Vector;
+import java.util.List;
+import java.util.Iterator;
+import java.util.ArrayList;
import org.apache.poi.hslf.record.PPDrawing;
import org.apache.poi.hslf.record.SlideAtom;
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.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherRecord;
/**
* This class represents a slide in a PowerPoint Document. It allows
@@ -33,6 +38,7 @@ import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
* the text side of things though
*
* @author Nick Burch
+ * @author Yegor Kozlov
*/
public class Slide extends Sheet
@@ -45,6 +51,7 @@ public class Slide extends Sheet
private TextRun[] _runs;
private TextRun[] _otherRuns; // Any from the PPDrawing, shouldn't really be any though
private Notes _notes; // usermodel needs to set this
+ private Background _background;
/**
* Constructs a Slide from the Slide record, and the SlideAtomsSet
@@ -245,4 +252,51 @@ public class Slide extends Sheet
return _slide.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;
+ }
+
+ /**
+ * Sets whether this slide follows master background
+ *
+ * @param flag 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();
+ }
}
diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java b/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java
index e09396a1b2..af2f97ef28 100644
--- a/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java
+++ b/src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java
@@ -19,6 +19,11 @@ 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.*;
+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.
@@ -32,6 +37,7 @@ public class SlideMaster extends MasterSheet {
private int _sheetNo;
private MainMaster _master;
private TextRun[] _runs;
+ private Background _background;
/**
* all TxMasterStyleAtoms available in this master
@@ -143,4 +149,30 @@ public class SlideMaster extends MasterSheet {
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;
+ }
+
}
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/backgrounds.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/backgrounds.ppt
new file mode 100644
index 0000000000..591efafef2
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/backgrounds.ppt differ
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java
new file mode 100644
index 0000000000..0c36150e96
--- /dev/null
+++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java
@@ -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());
+
+ }
+
+}