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
|
/* ====================================================================
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 org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.HSLFSlideShow;
import java.awt.*;
import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
/**
* Test drawing shapes via Graphics2D
*
* @author Yegor Kozlov
*/
public class TestShapes extends TestCase {
private SlideShow ppt;
protected void setUp() throws Exception {
String dirname = System.getProperty("HSLF.testdata.path");
String filename = dirname + "/empty.ppt";
ppt = new SlideShow(new HSLFSlideShow(filename));
}
public void testGraphics() throws Exception {
Slide slide = ppt.createSlide();
Line line = new Line();
java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100, 200, 50, 60);
line.setAnchor(lineAnchor);
System.out.println(line.getAnchor());
line.setLineWidth(3);
line.setLineStyle(Line.LineDashSys);
line.setLineColor(Color.red);
slide.addShape(line);
AutoShape ellipse = new AutoShape(ShapeTypes.Ellipse);
java.awt.Rectangle ellipseAnchor = new Rectangle(320, 154, 55, 111);
ellipse.setAnchor(ellipseAnchor);
ellipse.setLineWidth(2);
ellipse.setLineStyle(Line.LineSolid);
ellipse.setLineColor(Color.green);
ellipse.setFillColor(Color.lightGray);
slide.addShape(ellipse);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
//read ppt from byte array
ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
assertEquals(1, ppt.getSlides().length);
slide = ppt.getSlides()[0];
Shape[] shape = slide.getShapes();
assertEquals(2, shape.length);
assertTrue(shape[0] instanceof Line); //group shape
assertEquals(lineAnchor, shape[0].getAnchor()); //group shape
assertTrue(shape[1] instanceof AutoShape); //group shape
assertEquals(ellipseAnchor, shape[1].getAnchor()); //group shape
}
/**
* Verify that we can read TextBox shapes
* @throws Exception
*/
public void testTextBoxRead() throws Exception {
String dirname = System.getProperty("HSLF.testdata.path");
String filename = dirname + "/with_textbox.ppt";
ppt = new SlideShow(new HSLFSlideShow(filename));
Slide sl = ppt.getSlides()[0];
Shape[] sh = sl.getShapes();
for (int i = 0; i < sh.length; i++) {
assertTrue(sh[i] instanceof TextBox);
TextBox txtbox = (TextBox)sh[i];
String text = txtbox.getText();
assertNotNull(text);
assertEquals(txtbox.getRichTextRuns().length, 1);
RichTextRun rt = txtbox.getRichTextRuns()[0];
if (text.equals("Hello, World!!!")){
assertEquals(32, rt.getFontSize());
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
} else if (text.equals("I am just a poor boy")){
assertEquals(44, rt.getFontSize());
assertTrue(rt.isBold());
} else if (text.equals("This is Times New Roman")){
assertEquals(16, rt.getFontSize());
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertTrue(rt.isUnderlined());
} else if (text.equals("Plain Text")){
assertEquals(18, rt.getFontSize());
}
}
}
/**
* Verify that we can add TextBox shapes to a slide
* @throws Exception
*/
public void testTextBoxWrite() throws Exception {
ppt = new SlideShow();
Slide sl = ppt.createSlide();
TextBox txtbox = new TextBox();
txtbox.setText("Hello, World!");
txtbox.setFontSize(42);
txtbox.setBold(true);
txtbox.setItalic(true);
sl.addShape(txtbox);
txtbox = new TextBox();
txtbox.setText("Plain text in default font");
sl.addShape(txtbox);
assertEquals(sl.getShapes().length, 2);
//serialize and read again
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())));
sl = ppt.getSlides()[0];
assertEquals(sl.getShapes().length, 2);
Shape[] sh = sl.getShapes();
for (int i = 0; i < sh.length; i++) {
assertTrue(sh[i] instanceof TextBox);
txtbox = (TextBox)sh[i];
String text = txtbox.getText();
assertNotNull(text);
assertEquals(txtbox.getRichTextRuns().length, 1);
RichTextRun rt = txtbox.getRichTextRuns()[0];
if (text.equals("Hello, World!")){
assertEquals(42, rt.getFontSize());
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
}
}
}
}
|