View | Details | Raw Unified | Return to bug 41046
Collapse All | Expand All

(-)src/documentation/content/xdocs/hslf/how-to-shapes.xml (+42 lines)
Lines 19-24 Link Here
19
                    <li><link href="#Shapes">Drawing a shape on a slide</link></li>
19
                    <li><link href="#Shapes">Drawing a shape on a slide</link></li>
20
                    <li><link href="#Pictures">How to work with pictures</link></li>
20
                    <li><link href="#Pictures">How to work with pictures</link></li>
21
                    <li><link href="#SlideTitle">How to set slide title</link></li>
21
                    <li><link href="#SlideTitle">How to set slide title</link></li>
22
                    <li><link href="#Fill">How to work with slide/shape background</link></li>
22
                </ul>
23
                </ul>
23
            </section>
24
            </section>
24
            <section><title>Features</title>
25
            <section><title>Features</title>
Lines 252-257 Link Here
252
    myDocument.Shapes.AddTitle.TextFrame.TextRange.Text = "Hello, World!"
253
    myDocument.Shapes.AddTitle.TextFrame.TextRange.Text = "Hello, World!"
253
                  </source>
254
                  </source>
254
                </section>
255
                </section>
256
                <anchor id="Fill"/>
257
                <section><title>How to modify background of a slide master</title>
258
                    <source>
259
        SlideShow ppt = new SlideShow();
260
        SlideMaster master = ppt.getSlidesMasters()[0];
261
262
        Fill fill = master.getBackground().getFill();
263
        int idx = ppt.addPicture(new File("background.png"), Picture.PNG);
264
        fill.setFillType(Fill.FILL_PICTURE);
265
        fill.setPictureData(idx);
266
                  </source>
267
                </section>
268
                <section><title>How to modify background of a slide</title>
269
                    <source>
270
        SlideShow ppt = new SlideShow();
271
        Slide slide = ppt.createSlide();
272
        
273
        //This slide has its own background. 
274
        //Without this line it will use master's background.
275
        slide.setFollowMasterBackground(false);
276
        Fill fill = slide.getBackground().getFill();
277
        int idx = ppt.addPicture(new File("background.png"), Picture.PNG);
278
        fill.setFillType(Fill.FILL_PATTERN);
279
        fill.setPictureData(idx);
280
                  </source>
281
                </section>
282
                <section><title>How to modify background of a shape</title>
283
                    <source>
284
        SlideShow ppt = new SlideShow();
285
        Slide slide = ppt.createSlide();
286
        
287
        Shape shape = new AutoShape(ShapeTypes.Rectangle);
288
        shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
289
        Fill fill = shape.getFill();
290
        fill.setFillType(Fill.FILL_SHADE);
291
        fill.setBackgroundColor(Color.red);
292
        fill.setForegroundColor(Color.green);
293
        
294
        slide.addShape(shape);
295
                  </source>
296
                </section>
255
                  
297
                  
256
            </section>
298
            </section>
257
        </section>
299
        </section>
(-)src/scratchpad/src/org/apache/poi/POIDocument.java (-1 / +1 lines)
Lines 120-126 Link Here
120
			byte[] data = bOut.toByteArray();
120
			byte[] data = bOut.toByteArray();
121
			ByteArrayInputStream bIn = new ByteArrayInputStream(data);
121
			ByteArrayInputStream bIn = new ByteArrayInputStream(data);
122
			outFS.createDocument(bIn,name);
122
			outFS.createDocument(bIn,name);
123
			System.out.println("Wrote property set " + name + " of size " + data.length);
123
			//System.out.println("Wrote property set " + name + " of size " + data.length);
124
		} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
124
		} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
125
			System.err.println("Couldn't write property set with name " + name + " as not supported by HPSF yet");
125
			System.err.println("Couldn't write property set with name " + name + " as not supported by HPSF yet");
126
		}
126
		}
(-)src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java (-2 / +2 lines)
Lines 584-590 Link Here
584
  		System.arraycopy(_slides, 0, s, 0, _slides.length);
584
  		System.arraycopy(_slides, 0, s, 0, _slides.length);
585
  		s[_slides.length] = slide;
585
  		s[_slides.length] = slide;
586
  		_slides = s;
586
  		_slides = s;
587
  		System.out.println("Added slide " + _slides.length + " with ref " + sp.getRefID() + " and identifier " + sp.getSlideIdentifier());
587
  		//System.out.println("Added slide " + _slides.length + " with ref " + sp.getRefID() + " and identifier " + sp.getSlideIdentifier());
588
  		
588
  		
589
  		// Add the core records for this new Slide to the record tree
589
  		// Add the core records for this new Slide to the record tree
590
  		org.apache.poi.hslf.record.Slide slideRecord = slide.getSlideRecord();
590
  		org.apache.poi.hslf.record.Slide slideRecord = slide.getSlideRecord();
Lines 620-626 Link Here
620
  		// (Also need to tell it where it is)
620
  		// (Also need to tell it where it is)
621
		slideRecord.setLastOnDiskOffset(slideOffset);
621
		slideRecord.setLastOnDiskOffset(slideOffset);
622
		ptr.addSlideLookup(sp.getRefID(), slideOffset);
622
		ptr.addSlideLookup(sp.getRefID(), slideOffset);
623
		System.out.println("New slide ended up at " + slideOffset);
623
		//System.out.println("New slide ended up at " + slideOffset);
624
624
625
		// Last view is now of the slide
625
		// Last view is now of the slide
626
  		usr.setLastViewType((short)UserEditAtom.LAST_VIEW_SLIDE_VIEW);
626
  		usr.setLastViewType((short)UserEditAtom.LAST_VIEW_SLIDE_VIEW);
(-)src/scratchpad/src/org/apache/poi/hslf/model/Slide.java (+54 lines)
Lines 20-31 Link Here
20
package org.apache.poi.hslf.model;
20
package org.apache.poi.hslf.model;
21
21
22
import java.util.Vector;
22
import java.util.Vector;
23
import java.util.List;
24
import java.util.Iterator;
25
import java.util.ArrayList;
23
26
24
import org.apache.poi.hslf.record.PPDrawing;
27
import org.apache.poi.hslf.record.PPDrawing;
25
import org.apache.poi.hslf.record.SlideAtom;
28
import org.apache.poi.hslf.record.SlideAtom;
26
import org.apache.poi.hslf.record.TextHeaderAtom;
29
import org.apache.poi.hslf.record.TextHeaderAtom;
27
import org.apache.poi.hslf.record.ColorSchemeAtom;
30
import org.apache.poi.hslf.record.ColorSchemeAtom;
28
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
31
import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
32
import org.apache.poi.ddf.EscherContainerRecord;
33
import org.apache.poi.ddf.EscherRecord;
29
34
30
/**
35
/**
31
 * This class represents a slide in a PowerPoint Document. It allows 
36
 * This class represents a slide in a PowerPoint Document. It allows 
Lines 33-38 Link Here
33
 *  the text side of things though
38
 *  the text side of things though
34
 *
39
 *
35
 * @author Nick Burch
40
 * @author Nick Burch
41
 * @author Yegor Kozlov
36
 */
42
 */
37
43
38
public class Slide extends Sheet
44
public class Slide extends Sheet
Lines 45-50 Link Here
45
	private TextRun[] _runs;
51
	private TextRun[] _runs;
46
	private TextRun[] _otherRuns; // Any from the PPDrawing, shouldn't really be any though
52
	private TextRun[] _otherRuns; // Any from the PPDrawing, shouldn't really be any though
47
	private Notes _notes; // usermodel needs to set this
53
	private Notes _notes; // usermodel needs to set this
54
    private Background _background;
48
55
49
	/**
56
	/**
50
	 * Constructs a Slide from the Slide record, and the SlideAtomsSet
57
	 * Constructs a Slide from the Slide record, and the SlideAtomsSet
Lines 245-248 Link Here
245
        return _slide.getColorScheme();
252
        return _slide.getColorScheme();
246
    }
253
    }
247
254
255
    /**
256
     * Returns the background shape for this sheet.
257
     *
258
     * @return the background shape for this sheet.
259
     */
260
    public Background getBackground(){
261
        if (_background == null){
262
            PPDrawing ppdrawing = getPPDrawing();
263
264
            EscherContainerRecord dg = (EscherContainerRecord)ppdrawing.getEscherRecords()[0];
265
            EscherContainerRecord spContainer = null;
266
            List ch = dg.getChildRecords();
267
268
            for (Iterator it = ch.iterator(); it.hasNext();) {
269
                EscherRecord rec = (EscherRecord)it.next();
270
                if (rec.getRecordId() == EscherContainerRecord.SP_CONTAINER){
271
                        spContainer = (EscherContainerRecord)rec;
272
                        break;
273
                }
274
            }
275
            _background = new Background(spContainer, null);
276
            _background.setSheet(this);
277
        }
278
        return _background;
279
    }
280
281
    /**
282
     * Sets whether this slide follows master background
283
     *
284
     * @param flag  <code>true</code> if the slide follows master,
285
     * <code>false</code> otherwise
286
     */
287
    public void setFollowMasterBackground(boolean flag){
288
        SlideAtom sa = _slide.getSlideAtom();
289
        sa.setFollowMasterBackground(flag);
290
    }
291
292
    /**
293
     * Whether this slide follows master sheet background
294
     *
295
     * @return <code>true</code> if the slide follows master background,
296
     * <code>false</code> otherwise
297
     */
298
    public boolean getFollowMasterBackground(){
299
        SlideAtom sa = _slide.getSlideAtom();
300
        return sa.getFollowMasterBackground();
301
    }
248
}
302
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/Shape.java (+22 lines)
Lines 17-23 Link Here
17
17
18
import org.apache.poi.ddf.*;
18
import org.apache.poi.ddf.*;
19
import org.apache.poi.hslf.model.ShapeTypes;
19
import org.apache.poi.hslf.model.ShapeTypes;
20
import org.apache.poi.hslf.record.ColorSchemeAtom;
21
20
import java.util.Iterator;
22
import java.util.Iterator;
23
import java.awt.*;
21
24
22
/**
25
/**
23
 *  <p>
26
 *  <p>
Lines 282-285 Link Here
282
        _sheet = sheet;
285
        _sheet = sheet;
283
    }
286
    }
284
287
288
    protected Color getColor(int rgb){
289
        if (rgb >= 0x8000000) {
290
            int idx = rgb - 0x8000000;
291
            ColorSchemeAtom ca = getSheet().getColorScheme();
292
            if(idx >= 0 && idx <= 7) rgb = ca.getColor(idx);
293
        }
294
        Color tmp = new Color(rgb, true);
295
        return new Color(tmp.getBlue(), tmp.getGreen(), tmp.getRed());
296
    }
297
298
    /**
299
     * Fill properties of this shape
300
     *
301
     * @return fill properties of this shape
302
     */
303
    public Fill getFill(){
304
        return new Fill(this);
305
    }
306
285
}
307
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/SlideMaster.java (+32 lines)
Lines 19-25 Link Here
19
import org.apache.poi.hslf.record.*;
19
import org.apache.poi.hslf.record.*;
20
import org.apache.poi.hslf.usermodel.SlideShow;
20
import org.apache.poi.hslf.usermodel.SlideShow;
21
import org.apache.poi.hslf.record.StyleTextPropAtom.*;
21
import org.apache.poi.hslf.record.StyleTextPropAtom.*;
22
import org.apache.poi.ddf.EscherContainerRecord;
23
import org.apache.poi.ddf.EscherRecord;
22
24
25
import java.util.List;
26
import java.util.Iterator;
27
23
/**
28
/**
24
 * SlideMaster determines the graphics, layout, and formatting for all the slides in a given presentation.
29
 * SlideMaster determines the graphics, layout, and formatting for all the slides in a given presentation.
25
 * It stores information about default font styles, placeholder sizes and positions,
30
 * It stores information about default font styles, placeholder sizes and positions,
Lines 32-37 Link Here
32
    private int _sheetNo;
37
    private int _sheetNo;
33
    private MainMaster _master;
38
    private MainMaster _master;
34
    private TextRun[] _runs;
39
    private TextRun[] _runs;
40
    private Background _background;
35
41
36
    /**
42
    /**
37
     * all TxMasterStyleAtoms available in this master
43
     * all TxMasterStyleAtoms available in this master
Lines 143-146 Link Here
143
        return _master.getColorScheme();
149
        return _master.getColorScheme();
144
    }
150
    }
145
151
152
    /**
153
     * Returns the background shape for this sheet.
154
     *
155
     * @return the background shape for this sheet.
156
     */
157
    public Background getBackground(){
158
        if (_background == null){
159
            PPDrawing ppdrawing = getPPDrawing();
160
161
            EscherContainerRecord dg = (EscherContainerRecord)ppdrawing.getEscherRecords()[0];
162
            EscherContainerRecord spContainer = null;
163
            List ch = dg.getChildRecords();
164
165
            for (Iterator it = ch.iterator(); it.hasNext();) {
166
                EscherRecord rec = (EscherRecord)it.next();
167
                if (rec.getRecordId() == EscherContainerRecord.SP_CONTAINER){
168
                        spContainer = (EscherContainerRecord)rec;
169
                        break;
170
                }
171
            }
172
            _background = new Background(spContainer, null);
173
            _background.setSheet(this);
174
        }
175
        return _background;
176
    }
177
146
}
178
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/Background.java (+37 lines)
Line 0 Link Here
1
2
/* ====================================================================
3
   Copyright 2002-2004   Apache Software Foundation
4
5
   Licensed under the Apache License, Version 2.0 (the "License");
6
   you may not use this file except in compliance with the License.
7
   You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.hslf.model;
19
20
import org.apache.poi.ddf.EscherContainerRecord;
21
22
/**
23
 * Background shape
24
 * 
25
 * @author Yegor Kozlov
26
 */
27
public class Background extends Shape {
28
29
    protected Background(EscherContainerRecord escherRecord, Shape parent){
30
        super(escherRecord, parent);
31
    }
32
33
    protected EscherContainerRecord createSpContainer(boolean isChild){
34
        return null;
35
    }
36
37
}
(-)src/scratchpad/src/org/apache/poi/hslf/model/Fill.java (+228 lines)
Line 0 Link Here
1
2
/* ====================================================================
3
   Copyright 2002-2004   Apache Software Foundation
4
5
   Licensed under the Apache License, Version 2.0 (the "License");
6
   you may not use this file except in compliance with the License.
7
   You may obtain a copy of the License at
8
9
       http://www.apache.org/licenses/LICENSE-2.0
10
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
   See the License for the specific language governing permissions and
15
   limitations under the License.
16
==================================================================== */
17
18
package org.apache.poi.hslf.model;
19
20
import org.apache.poi.ddf.*;
21
import org.apache.poi.hslf.record.*;
22
import org.apache.poi.hslf.usermodel.PictureData;
23
import org.apache.poi.hslf.usermodel.SlideShow;
24
25
import java.awt.*;
26
import java.util.*;
27
28
/**
29
 * Represents functionality provided by the 'Fill Effects' dialog in PowerPoint.
30
 *
31
 * @author Yegor Kozlov
32
 */
33
public class Fill {
34
    /**
35
     *  Fill with a solid color
36
     */
37
    public static final int FILL_SOLID = 0;
38
39
    /**
40
     *  Fill with a pattern (bitmap)
41
     */
42
    public static final int FILL_PATTERN = 1;
43
44
    /**
45
     *  A texture (pattern with its own color map)
46
     */
47
    public static final int FILL_TEXTURE = 2;
48
49
    /**
50
     *  Center a picture in the shape
51
     */
52
    public static final int FILL_PICTURE = 3;
53
54
    /**
55
     *  Shade from start to end points
56
     */
57
    public static final int FILL_SHADE = 4;
58
59
    /**
60
     *  Shade from bounding rectangle to end point
61
     */
62
    public static final int FILL_SHADE_CENTER = 5;
63
64
    /**
65
     *  Shade from shape outline to end point
66
     */
67
    public static final int FILL_SHADE_SHAPE = 6;
68
69
    /**
70
     *  Similar to FILL_SHADE, but the fill angle
71
     *  is additionally scaled by the aspect ratio of
72
     *  the shape. If shape is square, it is the same as FILL_SHADE
73
     */
74
    public static final int FILL_SHADE_SCALE = 7;
75
76
    /**
77
     *  shade to title
78
     */
79
    public static final int FILL_SHADE_TITLE = 8;
80
81
    /**
82
     *  Use the background fill color/pattern
83
     */
84
    public static final int FILL_BACKGROUND = 9;
85
86
87
88
    /**
89
     * The shape this background applies to
90
     */
91
    protected Shape shape;
92
93
    /**
94
     * Construct a <code>Fill</code> object for a shape.
95
     * Fill information will be read from shape's escher properties.
96
     *
97
     * @param shape the shape this background applies to
98
     */
99
    public Fill(Shape shape){
100
        this.shape = shape;
101
    }
102
103
    /**
104
     * Returns fill type.
105
     * Must be one of the <code>FILL_*</code> constants defined in this class.
106
     *
107
     * @return type of fill
108
     */
109
    public int getFillType(){
110
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
111
        EscherSimpleProperty prop = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLTYPE);
112
        return prop == null ? FILL_SOLID : prop.getPropertyValue();
113
    }
114
115
    /**
116
     * Sets fill type.
117
     * Must be one of the <code>FILL_*</code> constants defined in this class.
118
     *
119
     * @param type type of the fill
120
     */
121
    public void setFillType(int type){
122
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
123
        Shape.setEscherProperty(opt, EscherProperties.FILL__FILLTYPE, type);
124
    }
125
126
    /**
127
     * Foreground color
128
     */
129
    public Color getForegroundColor(){
130
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
131
        EscherSimpleProperty p1 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLCOLOR);
132
        EscherSimpleProperty p2 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST);
133
134
        int p2val = p2 == null ? 0 : p2.getPropertyValue();
135
136
        Color clr = null;
137
        if (p1 != null && (p2val  & 0x10) != 0){
138
            int rgb = p1.getPropertyValue();
139
            clr = shape.getColor(rgb);
140
        }
141
        return clr;
142
    }
143
144
    /**
145
     * Foreground color
146
     */
147
    public void setForegroundColor(Color color){
148
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
149
        if (color == null) {
150
            Shape.setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, -1);
151
        }
152
        else {
153
            int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
154
            Shape.setEscherProperty(opt, EscherProperties.FILL__FILLCOLOR, rgb);
155
        }
156
    }
157
158
    /**
159
     * Background color
160
     */
161
    public Color getBackgroundColor(){
162
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
163
        EscherSimpleProperty p1 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR);
164
        EscherSimpleProperty p2 = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST);
165
166
        int p2val = p2 == null ? 0 : p2.getPropertyValue();
167
168
        Color clr = null;
169
        if (p1 != null && (p2val  & 0x10) != 0){
170
            int rgb = p1.getPropertyValue();
171
            clr = shape.getColor(rgb);
172
        }
173
        return clr;
174
    }
175
176
    /**
177
     * Background color
178
     */
179
    public void setBackgroundColor(Color color){
180
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
181
        if (color == null) {
182
            Shape.setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, -1);
183
        }
184
        else {
185
            int rgb = new Color(color.getBlue(), color.getGreen(), color.getRed(), 0).getRGB();
186
            Shape.setEscherProperty(opt, EscherProperties.FILL__FILLBACKCOLOR, rgb);
187
        }
188
    }
189
190
    /**
191
     * <code>PictureData</code> object used in a texture, pattern of picture fill.
192
     */
193
    public PictureData getPictureData(){
194
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
195
        EscherSimpleProperty p = (EscherSimpleProperty)Shape.getEscherProperty(opt, EscherProperties.FILL__PATTERNTEXTURE);
196
        if (p == null) return null;
197
198
        SlideShow ppt = shape.getSheet().getSlideShow();
199
        PictureData[] pict = ppt.getPictureData();
200
        Document doc = ppt.getDocumentRecord();
201
202
        EscherContainerRecord dggContainer = doc.getPPDrawingGroup().getDggContainer();
203
        EscherContainerRecord bstore = (EscherContainerRecord)Shape.getEscherChild(dggContainer, EscherContainerRecord.BSTORE_CONTAINER);
204
205
        java.util.List lst = bstore.getChildRecords();
206
        int idx = p.getPropertyValue();
207
        EscherBSERecord bse = (EscherBSERecord)lst.get(idx);
208
        for ( int i = 0; i < pict.length; i++ ) {
209
			if (pict[i].getOffset() ==  bse.getOffset()){
210
                return pict[i];
211
            }
212
        }
213
        throw new RuntimeException("Picture data not found: \n" +
214
                "  bse: " + bse + " at " + bse.getOffset() );
215
216
    }
217
218
    /**
219
     * Assign picture used to fill the underlying shape.
220
     *
221
     * @param idx 0-based index of the picture added to this ppt by <code>SlideShow.addPicture</code> method. 
222
     */
223
    public void setPictureData(int idx){
224
        EscherOptRecord opt = (EscherOptRecord)Shape.getEscherChild(shape.getSpContainer(), EscherOptRecord.RECORD_ID);
225
        Shape.setEscherProperty(opt, (short)(EscherProperties.FILL__PATTERNTEXTURE + 0x4000), idx);
226
    }
227
228
}
(-)src/scratchpad/testcases/org/apache/poi/hslf/model/TestBackground.java (+182 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Copyright 2002-2004   Apache Software Foundation
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
==================================================================== */
16
package org.apache.poi.hslf.model;
17
18
import junit.framework.TestCase;
19
20
import java.io.*;
21
import java.awt.*;
22
23
import org.apache.poi.hslf.usermodel.SlideShow;
24
import org.apache.poi.hslf.HSLFSlideShow;
25
26
/**
27
 * Test <code>Fill</code> object.
28
 * 
29
 * @author Yegor Kozlov
30
 */
31
public class TestBackground extends TestCase {
32
33
    /**
34
     * Default background for slide, shape and slide master.
35
     */
36
    public void testDefaults() throws Exception {
37
        SlideShow ppt = new SlideShow();
38
39
        assertEquals(Fill.FILL_SOLID, ppt.getSlidesMasters()[0].getBackground().getFill().getFillType());
40
41
        Slide slide = ppt.createSlide();
42
        assertTrue(slide.getFollowMasterBackground());
43
        assertEquals(Fill.FILL_SOLID, slide.getBackground().getFill().getFillType());
44
45
        Shape shape = new AutoShape(ShapeTypes.Rectangle);
46
        assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
47
    }
48
49
    /**
50
     * Read fill information from an reference ppt file
51
     */
52
    public void testReadBackground() throws Exception {
53
        SlideShow ppt = new SlideShow(new HSLFSlideShow(System.getProperty("HSLF.testdata.path") + "/backgrounds.ppt"));
54
        Fill fill;
55
        Shape shape;
56
57
        Slide[] slide = ppt.getSlides();
58
59
        fill = slide[0].getBackground().getFill();
60
        assertEquals(Fill.FILL_PICTURE, fill.getFillType());
61
        shape = slide[0].getShapes()[0];
62
        assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
63
64
        fill = slide[1].getBackground().getFill();
65
        assertEquals(Fill.FILL_PATTERN, fill.getFillType());
66
        shape = slide[1].getShapes()[0];
67
        assertEquals(Fill.FILL_BACKGROUND, shape.getFill().getFillType());
68
69
        fill = slide[2].getBackground().getFill();
70
        assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
71
        shape = slide[2].getShapes()[0];
72
        assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
73
74
        fill = slide[3].getBackground().getFill();
75
        assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
76
        shape = slide[3].getShapes()[0];
77
        assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
78
    }
79
80
    /**
81
     * Create a ppt with various fill effects
82
     */
83
    public void testBackgroundPicture() throws Exception {
84
        SlideShow ppt = new SlideShow();
85
        Slide slide;
86
        Fill fill;
87
        Shape shape;
88
        int idx;
89
90
        //slide 1
91
        slide = ppt.createSlide();
92
        slide.setFollowMasterBackground(false);
93
        fill = slide.getBackground().getFill();
94
        idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
95
        fill.setFillType(Fill.FILL_PICTURE);
96
        fill.setPictureData(idx);
97
98
        shape = new AutoShape(ShapeTypes.Rectangle);
99
        shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
100
        fill = shape.getFill();
101
        fill.setFillType(Fill.FILL_SOLID);
102
        slide.addShape(shape);
103
104
        //slide 2
105
        slide = ppt.createSlide();
106
        slide.setFollowMasterBackground(false);
107
        fill = slide.getBackground().getFill();
108
        idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
109
        fill.setFillType(Fill.FILL_PATTERN);
110
        fill.setPictureData(idx);
111
        fill.setBackgroundColor(Color.green);
112
        fill.setForegroundColor(Color.red);
113
114
        shape = new AutoShape(ShapeTypes.Rectangle);
115
        shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
116
        fill = shape.getFill();
117
        fill.setFillType(Fill.FILL_BACKGROUND);
118
        slide.addShape(shape);
119
120
        //slide 3
121
        slide = ppt.createSlide();
122
        slide.setFollowMasterBackground(false);
123
        fill = slide.getBackground().getFill();
124
        idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/tomcat.png"), Picture.PNG);
125
        fill.setFillType(Fill.FILL_TEXTURE);
126
        fill.setPictureData(idx);
127
128
        shape = new AutoShape(ShapeTypes.Rectangle);
129
        shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
130
        fill = shape.getFill();
131
        fill.setFillType(Fill.FILL_PICTURE);
132
        idx = ppt.addPicture(new File(System.getProperty("HSLF.testdata.path") + "/clock.jpg"), Picture.JPEG);
133
        fill.setPictureData(idx);
134
        slide.addShape(shape);
135
136
        // slide 4
137
        slide = ppt.createSlide();
138
        slide.setFollowMasterBackground(false);
139
        fill = slide.getBackground().getFill();
140
        fill.setFillType(Fill.FILL_SHADE_CENTER);
141
        fill.setBackgroundColor(Color.white);
142
        fill.setForegroundColor(Color.darkGray);
143
144
        shape = new AutoShape(ShapeTypes.Rectangle);
145
        shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
146
        fill = shape.getFill();
147
        fill.setFillType(Fill.FILL_SHADE);
148
        fill.setBackgroundColor(Color.red);
149
        fill.setForegroundColor(Color.green);
150
        slide.addShape(shape);
151
152
        //serialize and read again
153
        ByteArrayOutputStream out = new ByteArrayOutputStream();
154
        ppt.write(out);
155
        out.close();
156
157
        ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
158
        Slide[] slides = ppt.getSlides();
159
160
        fill = slides[0].getBackground().getFill();
161
        assertEquals(Fill.FILL_PICTURE, fill.getFillType());
162
        shape = slides[0].getShapes()[0];
163
        assertEquals(Fill.FILL_SOLID, shape.getFill().getFillType());
164
165
        fill = slides[1].getBackground().getFill();
166
        assertEquals(Fill.FILL_PATTERN, fill.getFillType());
167
        shape = slides[1].getShapes()[0];
168
        assertEquals(Fill.FILL_BACKGROUND, shape.getFill().getFillType());
169
170
        fill = slides[2].getBackground().getFill();
171
        assertEquals(Fill.FILL_TEXTURE, fill.getFillType());
172
        shape = slides[2].getShapes()[0];
173
        assertEquals(Fill.FILL_PICTURE, shape.getFill().getFillType());
174
175
        fill = slides[3].getBackground().getFill();
176
        assertEquals(Fill.FILL_SHADE_CENTER, fill.getFillType());
177
        shape = slides[3].getShapes()[0];
178
        assertEquals(Fill.FILL_SHADE, shape.getFill().getFillType());
179
180
    }
181
182
}

Return to bug 41046