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
|
/* ====================================================================
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 junit.framework.TestCase;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.XSLFTestDataSamples;
import org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor;
import org.openxmlformats.schemas.drawingml.x2006.main.STLineCap;
import org.openxmlformats.schemas.drawingml.x2006.main.STPresetLineDashVal;
import java.awt.Color;
/**
* @author Yegor Kozlov
*/
public class TestXSLFSimpleShape extends TestCase {
public void testLineStyles() {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFSimpleShape shape = slide.createAutoShape();
assertEquals(1, slide.getShapes().length);
// line properties are not set by default
assertFalse(shape.getSpPr().isSetLn());
assertEquals(0., shape.getLineWidth());
assertEquals(null, shape.getLineColor());
assertEquals(null, shape.getLineDash());
assertEquals(null, shape.getLineCap());
shape.setLineWidth(0);
shape.setLineColor(null);
shape.setLineDash(null);
shape.setLineCap(null);
// still no line properties
assertFalse(shape.getSpPr().isSetLn());
// line width
shape.setLineWidth(1.0);
assertEquals(1.0, shape.getLineWidth());
assertEquals(Units.EMU_PER_POINT, shape.getSpPr().getLn().getW());
shape.setLineWidth(5.5);
assertEquals(5.5, shape.getLineWidth());
assertEquals(Units.toEMU(5.5), shape.getSpPr().getLn().getW());
shape.setLineWidth(0.0);
// setting line width to zero unsets the W attribute
assertFalse(shape.getSpPr().getLn().isSetW());
// line cap
shape.setLineCap(LineCap.FLAT);
assertEquals(LineCap.FLAT, shape.getLineCap());
assertEquals(STLineCap.FLAT, shape.getSpPr().getLn().getCap());
shape.setLineCap(LineCap.SQUARE);
assertEquals(LineCap.SQUARE, shape.getLineCap());
assertEquals(STLineCap.SQ, shape.getSpPr().getLn().getCap());
shape.setLineCap(LineCap.ROUND);
assertEquals(LineCap.ROUND, shape.getLineCap());
assertEquals(STLineCap.RND, shape.getSpPr().getLn().getCap());
shape.setLineCap(null);
// setting cap to null unsets the Cap attribute
assertFalse(shape.getSpPr().getLn().isSetCap());
// line dash
shape.setLineDash(LineDash.SOLID);
assertEquals(LineDash.SOLID, shape.getLineDash());
assertEquals(STPresetLineDashVal.SOLID, shape.getSpPr().getLn().getPrstDash().getVal());
shape.setLineDash(LineDash.DASH_DOT);
assertEquals(LineDash.DASH_DOT, shape.getLineDash());
assertEquals(STPresetLineDashVal.DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
shape.setLineDash(LineDash.LG_DASH_DOT);
assertEquals(LineDash.LG_DASH_DOT, shape.getLineDash());
assertEquals(STPresetLineDashVal.LG_DASH_DOT, shape.getSpPr().getLn().getPrstDash().getVal());
shape.setLineDash(null);
// setting dash width to null unsets the Dash element
assertFalse(shape.getSpPr().getLn().isSetPrstDash());
// line color
assertFalse(shape.getSpPr().getLn().isSetSolidFill());
shape.setLineColor(Color.RED);
assertEquals(Color.RED, shape.getLineColor());
assertTrue(shape.getSpPr().getLn().isSetSolidFill());
shape.setLineColor(Color.BLUE);
assertEquals(Color.BLUE, shape.getLineColor());
assertTrue(shape.getSpPr().getLn().isSetSolidFill());
shape.setLineColor(null);
assertEquals(null, shape.getLineColor());
// setting dash width to null unsets the SolidFill element
assertFalse(shape.getSpPr().getLn().isSetSolidFill());
}
public void testFill() {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFAutoShape shape = slide.createAutoShape();
// line properties are not set by default
assertFalse(shape.getSpPr().isSetSolidFill());
assertNull(shape.getFillColor());
shape.setFillColor(null);
assertNull(shape.getFillColor());
assertFalse(shape.getSpPr().isSetSolidFill());
shape.setFillColor(Color.RED);
assertEquals(Color.RED, shape.getFillColor());
shape.setFillColor(Color.DARK_GRAY);
assertEquals(Color.DARK_GRAY, shape.getFillColor());
assertTrue(shape.getSpPr().isSetSolidFill());
shape.setFillColor(null);
assertNull(shape.getFillColor());
assertFalse(shape.getSpPr().isSetSolidFill());
}
public void testDefaultProperties() {
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
XSLFSlide slide6 = ppt.getSlides()[5];
XSLFShape[] shapes = slide6.getShapes();
for(int i = 1; i < shapes.length; i++){
XSLFSimpleShape s = (XSLFSimpleShape) shapes[i];
// all shapes have a theme color="accent1"
assertEquals("accent1", s.getSpStyle().getFillRef().getSchemeClr().getVal().toString());
assertEquals(2.0, s.getLineWidth());
assertEquals(LineCap.FLAT, s.getLineCap());
// YK: calculated color is slightly different from PowerPoint
assertEquals(new Color(39, 64, 94), s.getLineColor());
}
XSLFSimpleShape s0 = (XSLFSimpleShape) shapes[0];
// fill is not set
assertNull(s0.getSpPr().getSolidFill());
//assertEquals(slide6.getTheme().getColor("accent1").getColor(), s0.getFillColor());
assertEquals(new Color(79, 129, 189), s0.getFillColor());
// lighter 80%
XSLFSimpleShape s1 = (XSLFSimpleShape)shapes[1];
CTSchemeColor ref1 = s1.getSpPr().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[2];
CTSchemeColor ref2 = s2.getSpPr().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[3];
CTSchemeColor ref3 = s3.getSpPr().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[4];
CTSchemeColor ref4 = s4.getSpPr().getSolidFill().getSchemeClr();
assertEquals(1, ref4.sizeOfLumModArray());
assertEquals(0, ref4.sizeOfLumOffArray());
assertEquals(75000, ref4.getLumModArray(0).getVal());
assertEquals("accent1", ref3.getVal().toString());
// YK: calculated color is slightly different from PowerPoint
assertEquals(new Color(59, 97, 142), s4.getFillColor());
XSLFSimpleShape s5 = (XSLFSimpleShape)shapes[5];
CTSchemeColor ref5 = s5.getSpPr().getSolidFill().getSchemeClr();
assertEquals(1, ref5.sizeOfLumModArray());
assertEquals(0, ref5.sizeOfLumOffArray());
assertEquals(50000, ref5.getLumModArray(0).getVal());
assertEquals("accent1", ref5.getVal().toString());
// YK: calculated color is slightly different from PowerPoint
assertEquals(new Color(40, 65, 95), s5.getFillColor());
}
public void testAnchor(){
XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("shapes.pptx");
XSLFSlide[] slide = ppt.getSlides();
XSLFSlide slide2 = slide[1];
XSLFSlideLayout layout2 = slide2.getSlideLayout();
XSLFShape[] shapes2 = slide2.getShapes();
XSLFTextShape sh1 = (XSLFTextShape)shapes2[0];
assertEquals(Placeholder.CENTERED_TITLE, sh1.getTextType());
assertEquals("PPTX Title", sh1.getText());
assertNull(sh1.getSpPr().getXfrm()); // 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[1];
assertEquals("Subtitle\nAnd second line", sh2.getText());
assertEquals(Placeholder.SUBTITLE, sh2.getTextType());
assertNull(sh2.getSpPr().getXfrm()); // xfrm is not set, the query is delegated to the slide layout
assertEquals(sh2.getAnchor(), layout2.getTextShapeByType(Placeholder.SUBTITLE).getAnchor());
XSLFSlide slide5 = slide[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
assertNull(shTitle.getSpPr().getXfrm());
// xfrm is not set, the query is delegated to the slide master
assertNull(layout5.getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
assertNotNull(layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getSpPr().getXfrm());
assertEquals(shTitle.getAnchor(), layout5.getSlideMaster().getTextShapeByType(Placeholder.TITLE).getAnchor());
}
}
|