1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.xslf.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import org.apache.poi.sl.draw.DrawFactory;
import org.apache.poi.sl.draw.geom.TestPresetGeometries;
import org.apache.poi.sl.usermodel.Placeholder;
import org.apache.poi.sl.usermodel.Slide;
import org.apache.poi.sl.usermodel.StrokeStyle.LineCap;
import org.apache.poi.sl.usermodel.StrokeStyle.LineDash;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.apache.xmlbeans.XmlObject;
import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleItem;
import org.openxmlformats.schemas.drawingml.x2006.main.CTEffectStyleList;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTStyleMatrix;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import org.openxmlformats.schemas.drawingml.x2006.main.STShapeType;
public class TestXSLFSimpleShape {
@Test
public void testLineStyles() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
assertEquals(1, slide.getShapes().size());
// line properties are not set by default
assertFalse(getSpPr(shape).isSetLn());
assertEquals(0., shape.getLineWidth(), 0);
assertNull(shape.getLineColor());
assertNull(shape.getLineDash());
assertNull(shape.getLineCap());
shape.setLineWidth(0);
shape.setLineColor(null);
shape.setLineDash(null);
shape.setLineCap(null);
assertTrue(getSpPr(shape).isSetLn());
assertTrue(getSpPr(shape).getLn().isSetNoFill());
// line width
shape.setLineWidth(1.0);
assertEquals(1.0, shape.getLineWidth(), 0);
assertEquals(Units.EMU_PER_POINT, getSpPr(shape).getLn().getW());
shape.setLineWidth(5.5);
assertEquals(5.5, shape.getLineWidth(), 0);
assertEquals(Units.toEMU(5.5), getSpPr(shape).getLn().getW());
shape.setLineWidth(0.0);
// setting line width to zero unsets the W attribute
assertFalse(getSpPr(shape).getLn().isSetW());
// line cap
shape.setLineCap(LineCap.FLAT);
assertEquals(LineCap.FLAT, shape.getLineCap());
assertEquals(STLineCap.FLAT, getSpPr(shape).getLn().getCap());
shape.setLineCap(LineCap.SQUARE);
assertEquals(LineCap.SQUARE, shape.getLineCap());
assertEquals(STLineCap.SQ, getSpPr(shape).getLn().getCap());
shape.setLineCap(LineCap.ROUND);
assertEquals(LineCap.ROUND, shape.getLineCap());
assertEquals(STLineCap.RND, getSpPr(shape).getLn().getCap());
shape.setLineCap(null);
// setting cap to null unsets the Cap attribute
assertFalse(getSpPr(shape).getLn().isSetCap());
// line dash
shape.setLineDash(LineDash.SOLID);
assertEquals(LineDash.SOLID, shape.getLineDash());
assertEquals(STPresetLineDashVal.SOLID, getSpPr(shape).getLn().getPrstDash().getVal());
shape.setLineDash(LineDash.DASH_DOT);
assertEquals(LineDash.DASH_DOT, shape.getLineDash());
assertEquals(STPresetLineDashVal.DASH_DOT, getSpPr(shape).getLn().getPrstDash().getVal());
shape.setLineDash(LineDash.LG_DASH_DOT);
assertEquals(LineDash.LG_DASH_DOT, shape.getLineDash());
assertEquals(STPresetLineDashVal.LG_DASH_DOT, getSpPr(shape).getLn().getPrstDash().getVal());
shape.setLineDash(null);
// setting dash width to null unsets the Dash element
assertFalse(getSpPr(shape).getLn().isSetPrstDash());
// line color
assertFalse(getSpPr(shape).getLn().isSetSolidFill());
shape.setLineColor(Color.RED);
assertEquals(Color.RED, shape.getLineColor());
assertTrue(getSpPr(shape).getLn().isSetSolidFill());
shape.setLineColor(Color.BLUE);
assertEquals(Color.BLUE, shape.getLineColor());
assertTrue(getSpPr(shape).getLn().isSetSolidFill());
shape.setLineColor(null);
assertNull(shape.getLineColor());
// setting dash width to null unsets the SolidFill element
assertFalse(getSpPr(shape).getLn().isSetSolidFill());
XSLFSimpleShape ln2 = slide.createAutoShape();
ln2.setLineDash(LineDash.DOT);
assertEquals(LineDash.DOT, ln2.getLineDash());
ln2.setLineWidth(0.);
assertEquals(0., ln2.getLineWidth(), 0);
XSLFSimpleShape ln3 = slide.createAutoShape();
ln3.setLineWidth(1.);
assertEquals(1., ln3.getLineWidth(), 0);
ln3.setLineDash(null);
assertNull(ln3.getLineDash());
ln3.setLineCap(null);
assertNull(ln3.getLineDash());
ppt.close();
}
@Test
public void testFill() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFAutoShape shape = slide.createAutoShape();
// line properties are not set by default
assertFalse(getSpPr(shape).isSetSolidFill());
assertNull(shape.getFillColor());
shape.setFillColor(null);
assertNull(shape.getFillColor());
assertFalse(getSpPr(shape).isSetSolidFill());
shape.setFillColor(Color.RED);
assertEquals(Color.RED, shape.getFillColor());
shape.setFillColor(Color.DARK_GRAY);
assertEquals(Color.DARK_GRAY, shape.getFillColor());
assertTrue(getSpPr(shape).isSetSolidFill());
shape.setFillColor(null);
assertNull(shape.getFillColor());
assertFalse(getSpPr(shape).isSetSolidFill());
ppt.close();
}
@Test
public void testDefaultProperties() throws IOException {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
XSLFSlide slide6 = ppt.getSlides().get(5);
List<XSLFShape> shapes = slide6.getShapes();
for(XSLFShape xs : shapes){
XSLFSimpleShape s = (XSLFSimpleShape)xs;
// all shapes have a theme color="accent1"
assertEquals("accent1", s.getSpStyle().getFillRef().getSchemeClr().getVal().toString());
assertEquals(2.0, s.getLineWidth(), 0);
assertEquals(LineCap.FLAT, s.getLineCap());
assertEquals(new Color(79,129,189), s.getLineColor());
}
XSLFSimpleShape s0 = (XSLFSimpleShape) shapes.get(0);
// fill is not set
assertNull(getSpPr(s0).getSolidFill());
//assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
assertEquals(new Color(79, 129, 189), s0.getFillColor());
// lighter 80%
XSLFSimpleShape s1 = (XSLFSimpleShape)shapes.get(1);
CTSchemeColor ref1 = getSpPr(s1).getSolidFill().getSchemeClr();
assertEquals(1, ref1.sizeOfLumModArray());
assertEquals(1, ref1.sizeOfLumOffArray());
assertEquals(20000, ref1.getLumModArray(0).getVal());
assertEquals(80000, ref1.getLumOffArray(0).getVal());
assertEquals("accent1", ref1.getVal().toString());
assertEquals(new Color(220, 230, 242), s1.getFillColor());
// lighter 60%
XSLFSimpleShape s2 = (XSLFSimpleShape)shapes.get(2);
CTSchemeColor ref2 = getSpPr(s2).getSolidFill().getSchemeClr();
assertEquals(1, ref2.sizeOfLumModArray());
assertEquals(1, ref2.sizeOfLumOffArray());
assertEquals(40000, ref2.getLumModArray(0).getVal());
assertEquals(60000, ref2.getLumOffArray(0).getVal());
assertEquals("accent1", ref2.getVal().toString());
assertEquals(new Color(185, 205, 229), s2.getFillColor());
// lighter 40%
XSLFSimpleShape s3 = (XSLFSimpleShape)shapes.get(3);
CTSchemeColor ref3 = getSpPr(s3).getSolidFill().getSchemeClr();
assertEquals(1, ref3.sizeOfLumModArray());
assertEquals(1, ref3.sizeOfLumOffArray());
assertEquals(60000, ref3.getLumModArray(0).getVal());
assertEquals(40000, ref3.getLumOffArray(0).getVal());
assertEquals("accent1", ref3.getVal().toString());
assertEquals(new Color(149, 179, 215), s3.getFillColor());
// darker 25%
XSLFSimpleShape s4 = (XSLFSimpleShape)shapes.get(4);
CTSchemeColor ref4 = getSpPr(s4).getSolidFill().getSchemeClr();
assertEquals(1, ref4.sizeOfLumModArray());
assertEquals(0, ref4.sizeOfLumOffArray());
assertEquals(75000, ref4.getLumModArray(0).getVal());
assertEquals("accent1", ref3.getVal().toString());
assertEquals(new Color(55, 96, 146), s4.getFillColor());
XSLFSimpleShape s5 = (XSLFSimpleShape)shapes.get(5);
CTSchemeColor ref5 = getSpPr(s5).getSolidFill().getSchemeClr();
assertEquals(1, ref5.sizeOfLumModArray());
assertEquals(0, ref5.sizeOfLumOffArray());
assertEquals(50000, ref5.getLumModArray(0).getVal());
assertEquals("accent1", ref5.getVal().toString());
assertEquals(new Color(37, 64, 97), s5.getFillColor());
ppt.close();
}
@Test
public void testAnchor() throws IOException {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
List<XSLFSlide> slide = ppt.getSlides();
XSLFSlide slide2 = slide.get(1);
XSLFSlideLayout layout2 = slide2.getSlideLayout();
List<XSLFShape> shapes2 = slide2.getShapes();
XSLFTextShape sh1 = (XSLFTextShape)shapes2.get(0);
assertEquals(Placeholder.CENTERED_TITLE, sh1.getTextType());
assertEquals("PPTX Title", sh1.getText());
assertFalse(getSpPr(sh1).isSetXfrm()); // xfrm is not set, the query is delegated to the slide layout
assertEquals(sh1.getAnchor(), layout2.getTextShapeByType(Placeholder.CENTERED_TITLE).getAnchor());
XSLFTextShape sh2 = (XSLFTextShape)shapes2.get(1);
assertEquals("Subtitle\nAnd second line", sh2.getText());
assertEquals(Placeholder.SUBTITLE, sh2.getTextType());
assertFalse(getSpPr(sh2).isSetXfrm()); // xfrm is not set, the query is delegated to the slide layout
assertEquals(sh2.getAnchor(), layout2.getTextShapeByType(Placeholder.SUBTITLE).getAnchor());
XSLFSlide slide5 = slide.get(4);
XSLFSlideLayout layout5 = slide5.getSlideLayout();
XSLFTextShape shTitle = slide5.getTextShapeByType(Placeholder.TITLE);
assertEquals("Hyperlinks", shTitle.getText());
// xfrm is not set, the query is delegated to the slide layout
assertFalse(getSpPr(shTitle).isSetXfrm());
// xfrm is not set, the query is delegated to the slide master
assertFalse(getSpPr(layout5.getTextShapeByType(Placeholder.TITLE)).isSetXfrm());
assertTrue(getSpPr(layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE)).isSetXfrm());
assertEquals(shTitle.getAnchor(), layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getAnchor());
ppt.close();
}
@SuppressWarnings("unused")
@Test
public void testShadowEffects() throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
CTStyleMatrix styleMatrix = slide.getTheme().getXmlObject().getThemeElements().getFmtScheme();
CTEffectStyleList lst = styleMatrix.getEffectStyleLst();
assertNotNull(lst);
for(CTEffectStyleItem ef : lst.getEffectStyleArray()){
CTOuterShadowEffect obj = ef.getEffectLst().getOuterShdw();
}
ppt.close();
}
@Test
public void testValidGeometry() throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
CTShapeProperties spPr = getSpPr(shape);
CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
prstGeom.setPrst(STShapeType.Enum.forInt(1));
assertNotNull(prstGeom.getPrst());
assertNotNull(prstGeom.getPrst().toString());
assertNotNull(spPr.getPrstGeom());
spPr.setPrstGeom(prstGeom);
assertNotNull(spPr.getPrstGeom().getPrst());
assertNotNull(spPr.getPrstGeom().getPrst().toString());
assertNotNull(shape.getGeometry());
ppt.close();
}
@Test
public void testInvalidGeometry() throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
CTShapeProperties spPr = getSpPr(shape);
CTPresetGeometry2D prstGeom = CTPresetGeometry2D.Factory.newInstance();
prstGeom.setPrst(STShapeType.Enum.forInt(1));
assertNotNull(prstGeom.getPrst());
assertNotNull(prstGeom.getPrst().toString());
assertNotNull(spPr.getPrstGeom());
spPr.setPrstGeom(prstGeom);
assertNotNull(spPr.getPrstGeom().getPrst());
assertNotNull(spPr.getPrstGeom().getPrst().toString());
try {
// cause the geometries to be not found
TestPresetGeometries.clearPreset();
try {
shape.getGeometry();
fail("Should fail without the geometry");
} catch (IllegalStateException e) {
assertTrue(e.getMessage(), e.getMessage().contains("line"));
}
} finally {
// reset to not affect other tests
TestPresetGeometries.resetPreset();
}
ppt.close();
}
@SuppressWarnings("Duplicates")
@Test
public void testArrayStoreException() throws IOException {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("aascu.org_workarea_downloadasset.aspx_id=5864.pptx");
Dimension pgsize = ppt.getPageSize();
for (Slide<?,?> s : ppt.getSlides()) {
//System.out.println("Slide: " + s);
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = img.createGraphics();
DrawFactory.getInstance(graphics).fixFonts(graphics);
// default rendering options
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
// draw stuff
s.draw(graphics);
graphics.dispose();
img.flush();
}
ppt.close();
}
static CTShapeProperties getSpPr(XSLFShape shape) {
XmlObject xo = shape.getShapeProperties();
assertTrue(xo instanceof CTShapeProperties);
return (CTShapeProperties)xo;
}
}
|