001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hslf.model;
018:
019: import junit.framework.TestCase;
020: import org.apache.poi.hslf.usermodel.SlideShow;
021: import org.apache.poi.hslf.usermodel.RichTextRun;
022: import org.apache.poi.hslf.HSLFSlideShow;
023:
024: import java.awt.*;
025: import java.awt.Rectangle;
026: import java.io.ByteArrayOutputStream;
027: import java.io.ByteArrayInputStream;
028: import java.io.File;
029: import java.util.ArrayList;
030:
031: /**
032: * Test drawing shapes via Graphics2D
033: *
034: * @author Yegor Kozlov
035: */
036: public class TestShapes extends TestCase {
037: private SlideShow ppt;
038: private SlideShow pptB;
039:
040: protected void setUp() throws Exception {
041: String dirname = System.getProperty("HSLF.testdata.path");
042: String filename = dirname + "/empty.ppt";
043: ppt = new SlideShow(new HSLFSlideShow(filename));
044:
045: String filenameB = dirname + "/empty_textbox.ppt";
046: pptB = new SlideShow(new HSLFSlideShow(filenameB));
047: }
048:
049: public void testGraphics() throws Exception {
050: Slide slide = ppt.createSlide();
051:
052: Line line = new Line();
053: java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100,
054: 200, 50, 60);
055: line.setAnchor(lineAnchor);
056: line.setLineWidth(3);
057: line.setLineStyle(Line.PEN_DASH);
058: line.setLineColor(Color.red);
059: slide.addShape(line);
060:
061: AutoShape ellipse = new AutoShape(ShapeTypes.Ellipse);
062: java.awt.Rectangle ellipseAnchor = new Rectangle(320, 154, 55,
063: 111);
064: ellipse.setAnchor(ellipseAnchor);
065: ellipse.setLineWidth(2);
066: ellipse.setLineStyle(Line.PEN_SOLID);
067: ellipse.setLineColor(Color.green);
068: ellipse.setFillColor(Color.lightGray);
069: slide.addShape(ellipse);
070:
071: ByteArrayOutputStream out = new ByteArrayOutputStream();
072: ppt.write(out);
073: out.close();
074:
075: //read ppt from byte array
076:
077: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
078: out.toByteArray())));
079: assertEquals(1, ppt.getSlides().length);
080:
081: slide = ppt.getSlides()[0];
082: Shape[] shape = slide.getShapes();
083: assertEquals(2, shape.length);
084:
085: assertTrue(shape[0] instanceof Line); //group shape
086: assertEquals(lineAnchor, shape[0].getAnchor()); //group shape
087:
088: assertTrue(shape[1] instanceof AutoShape); //group shape
089: assertEquals(ellipseAnchor, shape[1].getAnchor()); //group shape
090: }
091:
092: /**
093: * Verify that we can read TextBox shapes
094: * @throws Exception
095: */
096: public void testTextBoxRead() throws Exception {
097: String dirname = System.getProperty("HSLF.testdata.path");
098: String filename = dirname + "/with_textbox.ppt";
099: ppt = new SlideShow(new HSLFSlideShow(filename));
100: Slide sl = ppt.getSlides()[0];
101: Shape[] sh = sl.getShapes();
102: for (int i = 0; i < sh.length; i++) {
103: assertTrue(sh[i] instanceof TextBox);
104: TextBox txtbox = (TextBox) sh[i];
105: String text = txtbox.getText();
106: assertNotNull(text);
107:
108: assertEquals(txtbox.getTextRun().getRichTextRuns().length,
109: 1);
110: RichTextRun rt = txtbox.getTextRun().getRichTextRuns()[0];
111:
112: if (text.equals("Hello, World!!!")) {
113: assertEquals(32, rt.getFontSize());
114: assertTrue(rt.isBold());
115: assertTrue(rt.isItalic());
116: } else if (text.equals("I am just a poor boy")) {
117: assertEquals(44, rt.getFontSize());
118: assertTrue(rt.isBold());
119: } else if (text.equals("This is Times New Roman")) {
120: assertEquals(16, rt.getFontSize());
121: assertTrue(rt.isBold());
122: assertTrue(rt.isItalic());
123: assertTrue(rt.isUnderlined());
124: } else if (text.equals("Plain Text")) {
125: assertEquals(18, rt.getFontSize());
126: }
127: }
128: }
129:
130: /**
131: * Verify that we can add TextBox shapes to a slide
132: * and set some of the style attributes
133: */
134: public void testTextBoxWriteBytes() throws Exception {
135: ppt = new SlideShow();
136: Slide sl = ppt.createSlide();
137: RichTextRun rt;
138:
139: String val = "Hello, World!";
140:
141: // Create a new textbox, and give it lots of properties
142: TextBox txtbox = new TextBox();
143: rt = txtbox.getTextRun().getRichTextRuns()[0];
144: txtbox.setText(val);
145: rt.setFontName("Arial");
146: rt.setFontSize(42);
147: rt.setBold(true);
148: rt.setItalic(true);
149: rt.setUnderlined(false);
150: rt.setFontColor(Color.red);
151: sl.addShape(txtbox);
152:
153: // Check it before save
154: rt = txtbox.getTextRun().getRichTextRuns()[0];
155: assertEquals(val, rt.getText());
156: assertEquals(42, rt.getFontSize());
157: assertTrue(rt.isBold());
158: assertTrue(rt.isItalic());
159: assertFalse(rt.isUnderlined());
160: assertEquals("Arial", rt.getFontName());
161: assertEquals(Color.red, rt.getFontColor());
162:
163: // Serialize and read again
164: ByteArrayOutputStream out = new ByteArrayOutputStream();
165: ppt.write(out);
166: out.close();
167:
168: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
169: out.toByteArray())));
170: sl = ppt.getSlides()[0];
171:
172: txtbox = (TextBox) sl.getShapes()[0];
173: rt = txtbox.getTextRun().getRichTextRuns()[0];
174:
175: // Check after save
176: assertEquals(val, rt.getText());
177: assertEquals(42, rt.getFontSize());
178: assertTrue(rt.isBold());
179: assertTrue(rt.isItalic());
180: assertFalse(rt.isUnderlined());
181: assertEquals("Arial", rt.getFontName());
182: assertEquals(Color.red, rt.getFontColor());
183: }
184:
185: /**
186: * Test with an empty text box
187: */
188: public void testEmptyTextBox() throws Exception {
189: assertEquals(2, pptB.getSlides().length);
190: Slide s1 = pptB.getSlides()[0];
191: Slide s2 = pptB.getSlides()[1];
192:
193: // Check we can get the shapes count
194: assertEquals(2, s1.getShapes().length);
195: assertEquals(2, s2.getShapes().length);
196: }
197:
198: /**
199: * If you iterate over text shapes in a slide and collect them in a set
200: * it must be the same as returned by Slide.getTextRuns().
201: */
202: public void testTextBoxSet() throws Exception {
203: textBoxSet("/with_textbox.ppt");
204: textBoxSet("/basic_test_ppt_file.ppt");
205: textBoxSet("/next_test_ppt_file.ppt");
206: textBoxSet("/Single_Coloured_Page.ppt");
207: textBoxSet("/Single_Coloured_Page_With_Fonts_and_Alignments.ppt");
208: textBoxSet("/incorrect_slide_order.ppt");
209: }
210:
211: private void textBoxSet(String filename) throws Exception {
212: String dirname = System.getProperty("HSLF.testdata.path");
213: SlideShow ppt = new SlideShow(new HSLFSlideShow(dirname
214: + filename));
215: Slide[] sl = ppt.getSlides();
216: for (int k = 0; k < sl.length; k++) {
217: ArrayList lst1 = new ArrayList();
218: TextRun[] txt = sl[k].getTextRuns();
219: for (int i = 0; i < txt.length; i++) {
220: lst1.add(txt[i].getText());
221: }
222:
223: ArrayList lst2 = new ArrayList();
224: Shape[] sh = sl[k].getShapes();
225: for (int i = 0; i < sh.length; i++) {
226: if (sh[i] instanceof TextBox) {
227: TextBox tbox = (TextBox) sh[i];
228: lst2.add(tbox.getText());
229: }
230: }
231: assertTrue(lst1.containsAll(lst2));
232: assertTrue(lst2.containsAll(lst1));
233: }
234: }
235:
236: /**
237: * Test adding shapes to <code>ShapeGroup</code>
238: */
239: public void testShapeGroup() throws Exception {
240: String cwd = System.getProperty("HSLF.testdata.path");
241: SlideShow ppt = new SlideShow();
242:
243: Slide slide = ppt.createSlide();
244: Dimension pgsize = ppt.getPageSize();
245:
246: ShapeGroup group = new ShapeGroup();
247:
248: group.setAnchor(new Rectangle(0, 0, (int) pgsize.getWidth(),
249: (int) pgsize.getHeight()));
250: slide.addShape(group);
251:
252: File img = new File(cwd, "clock.jpg");
253: int idx = ppt.addPicture(img, Picture.JPEG);
254: Picture pict = new Picture(idx, group);
255: pict.setAnchor(new Rectangle(0, 0, 200, 200));
256: group.addShape(pict);
257:
258: Line line = new Line(group);
259: line.setAnchor(new Rectangle(300, 300, 500, 0));
260: group.addShape(line);
261:
262: //serialize and read again.
263: ByteArrayOutputStream out = new ByteArrayOutputStream();
264: ppt.write(out);
265: out.close();
266:
267: ByteArrayInputStream is = new ByteArrayInputStream(out
268: .toByteArray());
269: ppt = new SlideShow(is);
270: is.close();
271:
272: slide = ppt.getSlides()[0];
273:
274: Shape[] shape = slide.getShapes();
275: assertEquals(1, shape.length);
276: assertTrue(shape[0] instanceof ShapeGroup);
277:
278: group = (ShapeGroup) shape[0];
279: Shape[] grshape = group.getShapes();
280: assertEquals(2, grshape.length);
281: assertTrue(grshape[0] instanceof Picture);
282: assertTrue(grshape[1] instanceof Line);
283:
284: pict = (Picture) grshape[0];
285: assertEquals(new Rectangle(0, 0, 200, 200), pict.getAnchor());
286:
287: line = (Line) grshape[1];
288: assertEquals(new Rectangle(300, 300, 500, 0), line.getAnchor());
289: }
290: }
|