You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestBugs.java 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.usermodel;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import java.awt.Color;
  23. import java.awt.Graphics2D;
  24. import java.awt.geom.Ellipse2D;
  25. import java.awt.geom.Path2D;
  26. import java.awt.geom.Rectangle2D;
  27. import java.awt.image.BufferedImage;
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import java.io.OutputStream;
  33. import java.io.PrintStream;
  34. import java.text.AttributedCharacterIterator;
  35. import java.text.AttributedCharacterIterator.Attribute;
  36. import java.util.ArrayList;
  37. import java.util.Date;
  38. import java.util.HashMap;
  39. import java.util.HashSet;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.Set;
  43. import org.apache.poi.POIDataSamples;
  44. import org.apache.poi.ddf.AbstractEscherOptRecord;
  45. import org.apache.poi.ddf.EscherArrayProperty;
  46. import org.apache.poi.ddf.EscherColorRef;
  47. import org.apache.poi.ddf.EscherProperties;
  48. import org.apache.poi.hslf.HSLFTestDataSamples;
  49. import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
  50. import org.apache.poi.hslf.extractor.PowerPointExtractor;
  51. import org.apache.poi.hslf.model.HeadersFooters;
  52. import org.apache.poi.hslf.record.DocInfoListContainer;
  53. import org.apache.poi.hslf.record.Document;
  54. import org.apache.poi.hslf.record.Record;
  55. import org.apache.poi.hslf.record.RecordTypes;
  56. import org.apache.poi.hslf.record.SlideListWithText;
  57. import org.apache.poi.hslf.record.SlideListWithText.SlideAtomsSet;
  58. import org.apache.poi.hslf.record.TextHeaderAtom;
  59. import org.apache.poi.hslf.record.VBAInfoAtom;
  60. import org.apache.poi.hslf.record.VBAInfoContainer;
  61. import org.apache.poi.hssf.usermodel.DummyGraphics2d;
  62. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  63. import org.apache.poi.poifs.macros.VBAMacroReader;
  64. import org.apache.poi.sl.draw.DrawFactory;
  65. import org.apache.poi.sl.draw.DrawPaint;
  66. import org.apache.poi.sl.draw.DrawTextParagraph;
  67. import org.apache.poi.sl.usermodel.ColorStyle;
  68. import org.apache.poi.sl.usermodel.PaintStyle;
  69. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  70. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  71. import org.apache.poi.sl.usermodel.Placeholder;
  72. import org.apache.poi.sl.usermodel.Slide;
  73. import org.apache.poi.sl.usermodel.SlideShow;
  74. import org.apache.poi.sl.usermodel.SlideShowFactory;
  75. import org.apache.poi.sl.usermodel.TextBox;
  76. import org.apache.poi.sl.usermodel.TextParagraph;
  77. import org.apache.poi.sl.usermodel.TextParagraph.TextAlign;
  78. import org.apache.poi.sl.usermodel.TextRun;
  79. import org.apache.poi.util.IOUtils;
  80. import org.apache.poi.util.LittleEndian;
  81. import org.apache.poi.util.StringUtil;
  82. import org.apache.poi.util.Units;
  83. import org.junit.Test;
  84. /**
  85. * Testcases for bugs entered in bugzilla
  86. * the Test name contains the bugzilla bug id
  87. */
  88. public final class TestBugs {
  89. /**
  90. * Bug 41384: Array index wrong in record creation
  91. */
  92. @Test
  93. public void bug41384() throws IOException {
  94. HSLFSlideShow ppt = open("41384.ppt");
  95. assertEquals(1, ppt.getSlides().size());
  96. List<HSLFPictureData> pict = ppt.getPictureData();
  97. assertEquals(2, pict.size());
  98. assertEquals(PictureType.JPEG, pict.get(0).getType());
  99. assertEquals(PictureType.JPEG, pict.get(1).getType());
  100. ppt.close();
  101. }
  102. /**
  103. * First fix from Bug 42474: NPE in RichTextRun.isBold()
  104. * when the RichTextRun comes from a Notes model object
  105. */
  106. @Test
  107. public void bug42474_1() throws IOException {
  108. HSLFSlideShow ppt = open("42474-1.ppt");
  109. assertEquals(2, ppt.getSlides().size());
  110. List<HSLFTextParagraph> txrun;
  111. HSLFNotes notes;
  112. notes = ppt.getSlides().get(0).getNotes();
  113. assertNotNull(notes);
  114. txrun = notes.getTextParagraphs().get(0);
  115. assertEquals("Notes-1", HSLFTextParagraph.getRawText(txrun));
  116. assertEquals(false, txrun.get(0).getTextRuns().get(0).isBold());
  117. //notes for the second slide are in bold
  118. notes = ppt.getSlides().get(1).getNotes();
  119. assertNotNull(notes);
  120. txrun = notes.getTextParagraphs().get(0);
  121. assertEquals("Notes-2", HSLFTextParagraph.getRawText(txrun));
  122. assertEquals(true, txrun.get(0).getTextRuns().get(0).isBold());
  123. ppt.close();
  124. }
  125. /**
  126. * Second fix from Bug 42474: Incorrect matching of notes to slides
  127. */
  128. @Test
  129. public void bug42474_2() throws IOException {
  130. HSLFSlideShow ppt = open("42474-2.ppt");
  131. //map slide number and starting phrase of its notes
  132. Map<Integer, String> notesMap = new HashMap<Integer, String>();
  133. notesMap.put(Integer.valueOf(4), "For decades before calculators");
  134. notesMap.put(Integer.valueOf(5), "Several commercial applications");
  135. notesMap.put(Integer.valueOf(6), "There are three variations of LNS that are discussed here");
  136. notesMap.put(Integer.valueOf(7), "Although multiply and square root are easier");
  137. notesMap.put(Integer.valueOf(8), "The bus Z is split into Z_H and Z_L");
  138. for (HSLFSlide slide : ppt.getSlides()) {
  139. Integer slideNumber = Integer.valueOf(slide.getSlideNumber());
  140. HSLFNotes notes = slide.getNotes();
  141. if (notesMap.containsKey(slideNumber)){
  142. assertNotNull(notes);
  143. String text = HSLFTextParagraph.getRawText(notes.getTextParagraphs().get(0));
  144. String startingPhrase = notesMap.get(slideNumber);
  145. assertTrue("Notes for slide " + slideNumber + " must start with " +
  146. startingPhrase , text.startsWith(startingPhrase));
  147. }
  148. }
  149. ppt.close();
  150. }
  151. /**
  152. * Bug 42485: All TextBoxes inside ShapeGroups have null TextRuns
  153. */
  154. @Test
  155. public void bug42485 () throws IOException {
  156. HSLFSlideShow ppt = open("42485.ppt");
  157. for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
  158. if(shape instanceof HSLFGroupShape){
  159. HSLFGroupShape group = (HSLFGroupShape)shape;
  160. for (HSLFShape sh : group.getShapes()) {
  161. if(sh instanceof HSLFTextBox){
  162. HSLFTextBox txt = (HSLFTextBox)sh;
  163. assertNotNull(txt.getTextParagraphs());
  164. }
  165. }
  166. }
  167. }
  168. ppt.close();
  169. }
  170. /**
  171. * Bug 42484: NullPointerException from ShapeGroup.getAnchor()
  172. */
  173. @Test
  174. public void bug42484 () throws IOException {
  175. HSLFSlideShow ppt = open("42485.ppt");
  176. for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
  177. if(shape instanceof HSLFGroupShape){
  178. HSLFGroupShape group = (HSLFGroupShape)shape;
  179. assertNotNull(group.getAnchor());
  180. for (HSLFShape sh : group.getShapes()) {
  181. assertNotNull(sh.getAnchor());
  182. }
  183. }
  184. }
  185. ppt.close();
  186. }
  187. /**
  188. * Bug 41381: Exception from Slide.getMasterSheet() on a seemingly valid PPT file
  189. */
  190. @Test
  191. public void bug41381() throws IOException {
  192. HSLFSlideShow ppt = open("alterman_security.ppt");
  193. assertTrue("No Exceptions while reading file", true);
  194. assertEquals(1, ppt.getSlideMasters().size());
  195. assertEquals(1, ppt.getTitleMasters().size());
  196. boolean isFirst = true;
  197. for (HSLFSlide slide : ppt.getSlides()) {
  198. HSLFMasterSheet master = slide.getMasterSheet();
  199. // the first slide follows TitleMaster
  200. assertTrue(isFirst ? master instanceof HSLFTitleMaster : master instanceof HSLFSlideMaster);
  201. isFirst = false;
  202. }
  203. ppt.close();
  204. }
  205. /**
  206. * Bug 42486: Failure parsing a seemingly valid PPT
  207. */
  208. @SuppressWarnings("unused")
  209. @Test
  210. public void bug42486 () throws IOException {
  211. HSLFSlideShow ppt = open("42486.ppt");
  212. for (HSLFSlide slide : ppt.getSlides()) {
  213. List<HSLFShape> shape = slide.getShapes();
  214. }
  215. ppt.close();
  216. }
  217. /**
  218. * Bug 42524: NPE in Shape.getShapeType()
  219. */
  220. @Test
  221. public void bug42524 () throws IOException {
  222. HSLFSlideShow ppt = open("42486.ppt");
  223. //walk down the tree and see if there were no errors while reading
  224. for (HSLFSlide slide : ppt.getSlides()) {
  225. for (HSLFShape shape : slide.getShapes()) {
  226. assertNotNull(shape.getShapeName());
  227. if (shape instanceof HSLFGroupShape){
  228. HSLFGroupShape group = (HSLFGroupShape)shape;
  229. for (HSLFShape comps : group.getShapes()) {
  230. assertNotNull(comps.getShapeName());
  231. }
  232. }
  233. }
  234. }
  235. ppt.close();
  236. }
  237. /**
  238. * Bug 42520: NPE in Picture.getPictureData()
  239. */
  240. @SuppressWarnings("unused")
  241. @Test
  242. public void bug42520 () throws IOException {
  243. HSLFSlideShow ppt = open("42520.ppt");
  244. //test case from the bug report
  245. HSLFGroupShape shapeGroup = (HSLFGroupShape)ppt.getSlides().get(11).getShapes().get(10);
  246. HSLFPictureShape picture = (HSLFPictureShape)shapeGroup.getShapes().get(0);
  247. picture.getPictureData();
  248. //walk down the tree and see if there were no errors while reading
  249. for (HSLFSlide slide : ppt.getSlides()) {
  250. for (HSLFShape shape : slide.getShapes()) {
  251. if (shape instanceof HSLFGroupShape){
  252. HSLFGroupShape group = (HSLFGroupShape)shape;
  253. for (HSLFShape comp : group.getShapes()) {
  254. if (comp instanceof HSLFPictureShape){
  255. HSLFPictureData pict = ((HSLFPictureShape)comp).getPictureData();
  256. }
  257. }
  258. }
  259. }
  260. }
  261. ppt.close();
  262. }
  263. /**
  264. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  265. * ( also fixed followup: getTextRuns() returns no text )
  266. */
  267. @Test
  268. public void bug38256 () throws IOException {
  269. HSLFSlideShow ppt = open("38256.ppt");
  270. List<HSLFSlide> slide = ppt.getSlides();
  271. assertEquals(1, slide.size());
  272. List<List<HSLFTextParagraph>> paras = slide.get(0).getTextParagraphs();
  273. assertEquals(4, paras.size());
  274. Set<String> txt = new HashSet<String>();
  275. txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
  276. txt.add("Have a HAPPY DAY");
  277. txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
  278. txt.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
  279. for (List<HSLFTextParagraph> para : paras) {
  280. String text = HSLFTextParagraph.getRawText(para);
  281. assertTrue(text, txt.contains(text));
  282. }
  283. ppt.close();
  284. }
  285. /**
  286. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  287. * ( also fixed followup: getTextRuns() returns no text )
  288. */
  289. @Test
  290. public void bug43781() throws IOException {
  291. HSLFSlideShow ppt = open("43781.ppt");
  292. // Check the first slide
  293. HSLFSlide slide = ppt.getSlides().get(0);
  294. List<List<HSLFTextParagraph>> slTr = slide.getTextParagraphs();
  295. // Has 3 text paragraphs, two from slide text (empty title / filled body), one from drawing
  296. assertEquals(3, slTr.size());
  297. assertFalse(slTr.get(0).get(0).isDrawingBased());
  298. assertFalse(slTr.get(1).get(0).isDrawingBased());
  299. assertTrue(slTr.get(2).get(0).isDrawingBased());
  300. assertEquals("", HSLFTextParagraph.getRawText(slTr.get(0)));
  301. assertEquals("First run", HSLFTextParagraph.getRawText(slTr.get(1)));
  302. assertEquals("Second run", HSLFTextParagraph.getRawText(slTr.get(2)));
  303. // Check the shape based text runs
  304. List<HSLFTextParagraph> lst = new ArrayList<HSLFTextParagraph>();
  305. for (HSLFShape shape : slide.getShapes()) {
  306. if (shape instanceof HSLFTextShape){
  307. List<HSLFTextParagraph> textRun = ((HSLFTextShape)shape).getTextParagraphs();
  308. lst.addAll(textRun);
  309. }
  310. }
  311. // There are two shapes in the ppt
  312. assertEquals(2, lst.size());
  313. assertEquals("First runSecond run", HSLFTextParagraph.getRawText(lst));
  314. ppt.close();
  315. }
  316. /**
  317. * Bug 44296: HSLF Not Extracting Slide Background Image
  318. */
  319. @Test
  320. public void bug44296 () throws IOException {
  321. HSLFSlideShow ppt = open("44296.ppt");
  322. HSLFSlide slide = ppt.getSlides().get(0);
  323. HSLFBackground b = slide.getBackground();
  324. HSLFFill f = b.getFill();
  325. assertEquals(HSLFFill.FILL_PICTURE, f.getFillType());
  326. HSLFPictureData pict = f.getPictureData();
  327. assertNotNull(pict);
  328. assertEquals(PictureType.JPEG, pict.getType());
  329. ppt.close();
  330. }
  331. /**
  332. * Bug 44770: java.lang.RuntimeException: Couldn't instantiate the class for
  333. * type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing
  334. */
  335. @Test
  336. public void bug44770() throws IOException {
  337. open("44770.ppt").close();
  338. }
  339. /**
  340. * Bug 41071: Will not extract text from Powerpoint TextBoxes
  341. */
  342. @Test
  343. public void bug41071() throws IOException {
  344. HSLFSlideShow ppt = open("41071.ppt");
  345. HSLFSlide slide = ppt.getSlides().get(0);
  346. List<HSLFShape> sh = slide.getShapes();
  347. assertEquals(1, sh.size());
  348. assertTrue(sh.get(0) instanceof HSLFTextShape);
  349. HSLFTextShape tx = (HSLFTextShape)sh.get(0);
  350. assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(tx.getTextParagraphs()));
  351. List<List<HSLFTextParagraph>> run = slide.getTextParagraphs();
  352. assertEquals(3, run.size());
  353. assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(run.get(2)));
  354. ppt.close();
  355. }
  356. /**
  357. * PowerPoint 95 files should throw a more helpful exception
  358. * @throws IOException
  359. */
  360. @Test(expected=OldPowerPointFormatException.class)
  361. public void bug41711() throws IOException {
  362. // New file is fine
  363. open("SampleShow.ppt").close();
  364. // PowerPoint 95 gives an old format exception
  365. open("PPT95.ppt").close();
  366. }
  367. /**
  368. * Changing text from Ascii to Unicode
  369. */
  370. @Test
  371. public void bug49648() throws IOException {
  372. HSLFSlideShow ppt = open("49648.ppt");
  373. for (HSLFSlide slide : ppt.getSlides()) {
  374. for (List<HSLFTextParagraph> run : slide.getTextParagraphs()) {
  375. String text = HSLFTextParagraph.getRawText(run);
  376. text.replace("{txtTot}", "With \u0123\u1234\u5678 unicode");
  377. HSLFTextParagraph.setText(run, text);
  378. }
  379. }
  380. ppt.close();
  381. }
  382. /**
  383. * Bug 41246: AIOOB with illegal note references
  384. */
  385. @Test
  386. public void bug41246a() throws IOException {
  387. HSLFSlideShow ppt = open("41246-1.ppt");
  388. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  389. ppt.close();
  390. }
  391. @Test
  392. public void bug41246b() throws IOException {
  393. HSLFSlideShow ppt = open("41246-2.ppt");
  394. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  395. ppt.close();
  396. }
  397. /**
  398. * Bug 45776: Fix corrupt file problem using TextRun.setText
  399. */
  400. @Test
  401. public void bug45776() throws IOException {
  402. HSLFSlideShow ppt = open("45776.ppt");
  403. // get slides
  404. for (HSLFSlide slide : ppt.getSlides()) {
  405. for (HSLFShape shape : slide.getShapes()) {
  406. if (!(shape instanceof HSLFTextBox)) continue;
  407. HSLFTextBox tb = (HSLFTextBox) shape;
  408. // work with TextBox
  409. String str = tb.getText();
  410. if (!str.contains("$$DATE$$")) continue;
  411. str = str.replace("$$DATE$$", new Date().toString());
  412. tb.setText(str);
  413. List<HSLFTextParagraph> tr = tb.getTextParagraphs();
  414. assertEquals(str.length()+1,tr.get(0).getParagraphStyle().getCharactersCovered());
  415. assertEquals(str.length()+1,tr.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered());
  416. }
  417. }
  418. ppt.close();
  419. }
  420. @Test
  421. public void bug55732() throws IOException {
  422. HSLFSlideShow ppt = open("bug55732.ppt");
  423. List<HSLFSlide> _slides = ppt.getSlides();
  424. /* Iterate over slides and extract text */
  425. for( HSLFSlide slide : _slides ) {
  426. HeadersFooters hf = slide.getHeadersFooters();
  427. /*boolean visible =*/ hf.isHeaderVisible(); // exception happens here
  428. }
  429. ppt.close();
  430. }
  431. @Test
  432. public void bug56260() throws IOException {
  433. HSLFSlideShow ppt = open("56260.ppt");
  434. List<HSLFSlide> _slides = ppt.getSlides();
  435. assertEquals(13, _slides.size());
  436. // Check the number of TextHeaderAtoms on Slide 1
  437. Document dr = ppt.getDocumentRecord();
  438. SlideListWithText slidesSLWT = dr.getSlideSlideListWithText();
  439. SlideAtomsSet s1 = slidesSLWT.getSlideAtomsSets()[0];
  440. int tha = 0;
  441. for (Record r : s1.getSlideRecords()) {
  442. if (r instanceof TextHeaderAtom) tha++;
  443. }
  444. assertEquals(2, tha);
  445. // Check to see that we have a pair next to each other
  446. assertEquals(TextHeaderAtom.class, s1.getSlideRecords()[0].getClass());
  447. assertEquals(TextHeaderAtom.class, s1.getSlideRecords()[1].getClass());
  448. // Check the number of text runs based on the slide (not textbox)
  449. // Will have skipped the empty one
  450. int str = 0;
  451. for (List<HSLFTextParagraph> tr : _slides.get(0).getTextParagraphs()) {
  452. if (! tr.get(0).isDrawingBased()) str++;
  453. }
  454. assertEquals(2, str);
  455. ppt.close();
  456. }
  457. @Test
  458. public void bug37625() throws IOException {
  459. HSLFSlideShow ppt1 = open("37625.ppt");
  460. assertEquals(29, ppt1.getSlides().size());
  461. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  462. assertNotNull(ppt2);
  463. assertEquals(29, ppt2.getSlides().size());
  464. ppt2.close();
  465. ppt1.close();
  466. }
  467. @Test
  468. public void bug57272() throws IOException {
  469. HSLFSlideShow ppt1 = open("57272_corrupted_usereditatom.ppt");
  470. assertEquals(6, ppt1.getSlides().size());
  471. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  472. assertNotNull(ppt2);
  473. assertEquals(6, ppt2.getSlides().size());
  474. ppt2.close();
  475. ppt1.close();
  476. }
  477. @Test
  478. public void bug49541() throws IOException {
  479. HSLFSlideShow ppt = open("49541_symbol_map.ppt");
  480. HSLFSlide slide = ppt.getSlides().get(0);
  481. HSLFGroupShape sg = (HSLFGroupShape)slide.getShapes().get(0);
  482. HSLFTextBox tb = (HSLFTextBox)sg.getShapes().get(0);
  483. String text = StringUtil.mapMsCodepointString(tb.getText());
  484. assertEquals("\u226575 years", text);
  485. ppt.close();
  486. }
  487. @Test
  488. public void bug47261() throws IOException {
  489. HSLFSlideShow ppt = open("bug47261.ppt");
  490. ppt.removeSlide(0);
  491. ppt.createSlide();
  492. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  493. ppt.close();
  494. }
  495. @Test
  496. public void bug56240() throws IOException {
  497. HSLFSlideShow ppt = open("bug56240.ppt");
  498. int slideCnt = ppt.getSlides().size();
  499. assertEquals(105, slideCnt);
  500. HSLFTestDataSamples.writeOutAndReadBack(ppt).close();
  501. ppt.close();
  502. }
  503. @Test
  504. public void bug46441() throws IOException {
  505. HSLFSlideShow ppt = open("bug46441.ppt");
  506. HSLFAutoShape as = (HSLFAutoShape)ppt.getSlides().get(0).getShapes().get(0);
  507. AbstractEscherOptRecord opt = as.getEscherOptRecord();
  508. EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS);
  509. double exp[][] = {
  510. // r, g, b, position
  511. { 94, 158, 255, 0 },
  512. { 133, 194, 255, 0.399994 },
  513. { 196, 214, 235, 0.699997 },
  514. { 255, 235, 250, 1 }
  515. };
  516. int i = 0;
  517. for (byte data[] : ep) {
  518. EscherColorRef ecr = new EscherColorRef(data, 0, 4);
  519. int rgb[] = ecr.getRGB();
  520. double pos = Units.fixedPointToDouble(LittleEndian.getInt(data, 4));
  521. assertEquals((int)exp[i][0], rgb[0]);
  522. assertEquals((int)exp[i][1], rgb[1]);
  523. assertEquals((int)exp[i][2], rgb[2]);
  524. assertEquals(exp[i][3], pos, 0.01);
  525. i++;
  526. }
  527. ppt.close();
  528. }
  529. @Test
  530. public void bug58516() throws IOException {
  531. open("bug58516.ppt").close();
  532. }
  533. @Test
  534. public void bug45124() throws IOException {
  535. SlideShow<?,?> ppt = open("bug45124.ppt");
  536. Slide<?,?> slide1 = ppt.getSlides().get(1);
  537. TextBox<?,?> res = slide1.createTextBox();
  538. res.setAnchor(new java.awt.Rectangle(60, 150, 700, 100));
  539. res.setText("I am italic-false, bold-true inserted text");
  540. TextParagraph<?,?,?> tp = res.getTextParagraphs().get(0);
  541. TextRun rt = tp.getTextRuns().get(0);
  542. rt.setItalic(false);
  543. assertTrue(rt.isBold());
  544. tp.setBulletStyle(Color.red, 'A');
  545. SlideShow<?,?> ppt2 = HSLFTestDataSamples.writeOutAndReadBack((HSLFSlideShow)ppt);
  546. ppt.close();
  547. res = (TextBox<?,?>)ppt2.getSlides().get(1).getShapes().get(1);
  548. tp = res.getTextParagraphs().get(0);
  549. rt = tp.getTextRuns().get(0);
  550. assertFalse(rt.isItalic());
  551. assertTrue(rt.isBold());
  552. PaintStyle ps = tp.getBulletStyle().getBulletFontColor();
  553. assertTrue(ps instanceof SolidPaint);
  554. Color actColor = DrawPaint.applyColorTransform(((SolidPaint)ps).getSolidColor());
  555. assertEquals(Color.red, actColor);
  556. assertEquals("A", tp.getBulletStyle().getBulletCharacter());
  557. ppt2.close();
  558. }
  559. @Test
  560. public void bug45088() throws IOException {
  561. String template = "[SYSDATE]";
  562. String textExp = "REPLACED_DATE_WITH_A_LONG_ONE";
  563. HSLFSlideShow ppt1 = open("bug45088.ppt");
  564. for (HSLFSlide slide : ppt1.getSlides()) {
  565. for (List<HSLFTextParagraph> paraList : slide.getTextParagraphs()) {
  566. for (HSLFTextParagraph para : paraList) {
  567. for (HSLFTextRun run : para.getTextRuns()) {
  568. String text = run.getRawText();
  569. if (text != null && text.contains(template)) {
  570. String replacedText = text.replace(template, textExp);
  571. run.setText(replacedText);
  572. para.setDirty();
  573. }
  574. }
  575. }
  576. }
  577. }
  578. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  579. ppt1.close();
  580. HSLFTextBox tb = (HSLFTextBox)ppt2.getSlides().get(0).getShapes().get(1);
  581. String textAct = tb.getTextParagraphs().get(0).getTextRuns().get(0).getRawText().trim();
  582. assertEquals(textExp, textAct);
  583. ppt2.close();
  584. }
  585. @Test
  586. public void bug45908() throws IOException {
  587. HSLFSlideShow ppt1 = open("bug45908.ppt");
  588. HSLFSlide slide = ppt1.getSlides().get(0);
  589. HSLFAutoShape styleShape = (HSLFAutoShape)slide.getShapes().get(1);
  590. HSLFTextParagraph tp0 = styleShape.getTextParagraphs().get(0);
  591. HSLFTextRun tr0 = tp0.getTextRuns().get(0);
  592. int rows = 5;
  593. int cols = 2;
  594. HSLFTable table = slide.createTable(rows, cols);
  595. for (int i = 0; i < rows; i++) {
  596. for (int j = 0; j < cols; j++) {
  597. HSLFTableCell cell = table.getCell(i, j);
  598. cell.setText("Test");
  599. HSLFTextParagraph tp = cell.getTextParagraphs().get(0);
  600. tp.setBulletStyle('%', tp0.getBulletColor(), tp0.getBulletFont(), tp0.getBulletSize());
  601. tp.setIndent(tp0.getIndent());
  602. tp.setTextAlign(tp0.getTextAlign());
  603. tp.setIndentLevel(tp0.getIndentLevel());
  604. tp.setSpaceAfter(tp0.getSpaceAfter());
  605. tp.setSpaceBefore(tp0.getSpaceBefore());
  606. tp.setBulletStyle();
  607. HSLFTextRun tr = tp.getTextRuns().get(0);
  608. tr.setBold(tr0.isBold());
  609. // rt.setEmbossed();
  610. tr.setFontColor(Color.BLACK);
  611. tr.setFontFamily(tr0.getFontFamily());
  612. tr.setFontSize(tr0.getFontSize());
  613. tr.setItalic(tr0.isItalic());
  614. tr.setShadowed(tr0.isShadowed());
  615. tr.setStrikethrough(tr0.isStrikethrough());
  616. tr.setUnderlined(tr0.isUnderlined());
  617. }
  618. }
  619. table.moveTo(100, 100);
  620. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  621. ppt1.close();
  622. HSLFTable tab = (HSLFTable)ppt2.getSlides().get(0).getShapes().get(2);
  623. HSLFTableCell c2 = tab.getCell(0, 0);
  624. HSLFTextParagraph tp1 = c2.getTextParagraphs().get(0);
  625. HSLFTextRun tr1 = tp1.getTextRuns().get(0);
  626. assertFalse(tp1.isBullet());
  627. assertEquals(tp0.getBulletColor(), tp1.getBulletColor());
  628. assertEquals(tp0.getBulletFont(), tp1.getBulletFont());
  629. assertEquals(tp0.getBulletSize(), tp1.getBulletSize());
  630. assertEquals(tp0.getIndent(), tp1.getIndent());
  631. assertEquals(tp0.getTextAlign(), tp1.getTextAlign());
  632. assertEquals(tp0.getIndentLevel(), tp1.getIndentLevel());
  633. assertEquals(tp0.getSpaceAfter(), tp1.getSpaceAfter());
  634. assertEquals(tp0.getSpaceBefore(), tp1.getSpaceBefore());
  635. assertEquals(tr0.isBold(), tr1.isBold());
  636. assertEquals(Color.black, DrawPaint.applyColorTransform(tr1.getFontColor().getSolidColor()));
  637. assertEquals(tr0.getFontFamily(), tr1.getFontFamily());
  638. assertEquals(tr0.getFontSize(), tr1.getFontSize());
  639. assertEquals(tr0.isItalic(), tr1.isItalic());
  640. assertEquals(tr0.isShadowed(), tr1.isShadowed());
  641. assertEquals(tr0.isStrikethrough(), tr1.isStrikethrough());
  642. assertEquals(tr0.isUnderlined(), tr1.isUnderlined());
  643. ppt2.close();
  644. }
  645. @Test
  646. public void bug47904() throws IOException {
  647. HSLFSlideShow ppt1 = new HSLFSlideShow();
  648. HSLFSlideMaster sm = ppt1.getSlideMasters().get(0);
  649. HSLFAutoShape as = (HSLFAutoShape)sm.getShapes().get(0);
  650. HSLFTextParagraph tp = as.getTextParagraphs().get(0);
  651. HSLFTextRun tr = tp.getTextRuns().get(0);
  652. tr.setFontFamily("Tahoma");
  653. tr.setShadowed(true);
  654. tr.setFontSize(44.);
  655. tr.setFontColor(Color.red);
  656. tp.setTextAlign(TextAlign.RIGHT);
  657. ppt1.createSlide().addTitle().setText("foobaa");
  658. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  659. ppt1.close();
  660. HSLFTextShape ts = (HSLFTextShape)ppt2.getSlides().get(0).getShapes().get(0);
  661. tp = ts.getTextParagraphs().get(0);
  662. tr = tp.getTextRuns().get(0);
  663. assertEquals(44., tr.getFontSize(), 0);
  664. assertEquals("Tahoma", tr.getFontFamily());
  665. Color colorAct = DrawPaint.applyColorTransform(tr.getFontColor().getSolidColor());
  666. assertEquals(Color.red, colorAct);
  667. assertEquals(TextAlign.RIGHT, tp.getTextAlign());
  668. assertEquals("foobaa", tr.getRawText());
  669. ppt2.close();
  670. }
  671. @Test
  672. public void bug58718() throws IOException {
  673. String files[] = { "bug58718_008524.ppt","bug58718_008558.ppt","bug58718_349008.ppt","bug58718_008495.ppt", };
  674. for (String f : files) {
  675. File sample = HSLFTestDataSamples.getSampleFile(f);
  676. PowerPointExtractor ex = new PowerPointExtractor(sample.getAbsolutePath());
  677. assertNotNull(ex.getText());
  678. ex.close();
  679. }
  680. }
  681. @Test
  682. public void bug58733() throws IOException {
  683. File sample = HSLFTestDataSamples.getSampleFile("bug58733_671884.ppt");
  684. PowerPointExtractor ex = new PowerPointExtractor(sample.getAbsolutePath());
  685. assertNotNull(ex.getText());
  686. ex.close();
  687. }
  688. @Test
  689. public void bug58159() throws IOException {
  690. HSLFSlideShow ppt = open("bug58159_headers-and-footers.ppt");
  691. HeadersFooters hf = ppt.getSlideHeadersFooters();
  692. assertNull(hf.getHeaderText());
  693. assertEquals("Slide footer", hf.getFooterText());
  694. hf = ppt.getNotesHeadersFooters();
  695. assertEquals("Notes header", hf.getHeaderText());
  696. assertEquals("Notes footer", hf.getFooterText());
  697. HSLFSlide sl = ppt.getSlides().get(0);
  698. hf = sl.getHeadersFooters();
  699. assertNull(hf.getHeaderText());
  700. assertEquals("Slide footer", hf.getFooterText());
  701. for (HSLFShape shape : sl.getShapes()) {
  702. if (shape instanceof HSLFTextShape) {
  703. HSLFTextShape ts = (HSLFTextShape)shape;
  704. Placeholder ph = ts.getPlaceholder();
  705. if (Placeholder.FOOTER == ph) {
  706. assertEquals("Slide footer", ts.getText());
  707. }
  708. }
  709. }
  710. ppt.close();
  711. }
  712. @Test
  713. public void bug55030() throws IOException {
  714. HSLFSlideShow ppt = open("bug55030.ppt");
  715. String expFamily = "\u96b6\u4e66";
  716. HSLFSlide sl = ppt.getSlides().get(0);
  717. for (List<HSLFTextParagraph> paraList : sl.getTextParagraphs()) {
  718. for (HSLFTextParagraph htp : paraList) {
  719. for (HSLFTextRun htr : htp) {
  720. String actFamily = htr.getFontFamily();
  721. assertEquals(expFamily, actFamily);
  722. }
  723. }
  724. }
  725. ppt.close();
  726. }
  727. @Test
  728. @SuppressWarnings("resource")
  729. public void bug57796() throws IOException {
  730. HSLFSlideShow ppt = open("WithLinks.ppt");
  731. HSLFSlide slide = ppt.getSlides().get(0);
  732. HSLFTextShape shape = (HSLFTextShape)slide.getShapes().get(1);
  733. List<HSLFHyperlink> hlList = HSLFHyperlink.find(shape);
  734. HSLFHyperlink hlShape = hlList.get(0);
  735. HSLFTextRun r = shape.getTextParagraphs().get(1).getTextRuns().get(0);
  736. HSLFHyperlink hlRun = r.getHyperlink();
  737. assertEquals(hlRun.getId(), hlShape.getId());
  738. assertEquals(hlRun.getAddress(), hlShape.getAddress());
  739. assertEquals(hlRun.getLabel(), hlShape.getLabel());
  740. assertEquals(hlRun.getType(), hlShape.getType());
  741. assertEquals(hlRun.getStartIndex(), hlShape.getStartIndex());
  742. assertEquals(hlRun.getEndIndex(), hlShape.getEndIndex());
  743. OutputStream nullOutput = new OutputStream(){
  744. public void write(int b) throws IOException {}
  745. };
  746. final boolean found[] = { false };
  747. DummyGraphics2d dgfx = new DummyGraphics2d(new PrintStream(nullOutput)){
  748. public void drawString(AttributedCharacterIterator iterator, float x, float y) {
  749. // For the test file, common sl draws textruns one by one and not mixed
  750. // so we evaluate the whole iterator
  751. Map<Attribute, Object> attributes = null;
  752. StringBuffer sb = new StringBuffer();
  753. for (char c = iterator.first();
  754. c != AttributedCharacterIterator.DONE;
  755. c = iterator.next()) {
  756. sb.append(c);
  757. attributes = iterator.getAttributes();
  758. }
  759. if ("Jakarta HSSF".equals(sb.toString())) {
  760. // this is a test for a manually modified ppt, for real hyperlink label
  761. // one would need to access the screen tip record
  762. String href = (String)attributes.get(DrawTextParagraph.HYPERLINK_HREF);
  763. String label = (String)attributes.get(DrawTextParagraph.HYPERLINK_LABEL);
  764. assertEquals("http://jakarta.apache.org/poi/hssf/", href);
  765. assertEquals("Open Jakarta POI HSSF module test ", label);
  766. found[0] = true;
  767. }
  768. }
  769. };
  770. ppt.getSlides().get(1).draw(dgfx);
  771. assertTrue(found[0]);
  772. ppt.close();
  773. }
  774. @Test
  775. public void bug59056() throws IOException {
  776. HSLFSlideShow ppt = open("54541_cropped_bitmap.ppt");
  777. for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) {
  778. BufferedImage img = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
  779. Graphics2D graphics = img.createGraphics();
  780. Rectangle2D box = new Rectangle2D.Double(50,50,300,100);
  781. graphics.setPaint(Color.red);
  782. graphics.fill(box);
  783. box = new Rectangle2D.Double(box.getX()+1,box.getY()+1,box.getWidth()-2,box.getHeight()-2);
  784. DrawFactory.getInstance(graphics).drawShape(graphics, shape, box);
  785. graphics.dispose();
  786. // ImageIO.write(img, "png", new File("bla"+shape.getShapeId()+".png"));
  787. }
  788. ppt.close();
  789. }
  790. private static HSLFSlideShow open(String fileName) throws IOException {
  791. File sample = HSLFTestDataSamples.getSampleFile(fileName);
  792. return (HSLFSlideShow)SlideShowFactory.create(sample);
  793. }
  794. @Test
  795. public void bug55983() throws IOException {
  796. HSLFSlideShow ppt1 = new HSLFSlideShow();
  797. HSLFSlide sl = ppt1.createSlide();
  798. sl.getBackground().getFill().setForegroundColor(Color.blue);
  799. HSLFFreeformShape fs = sl.createFreeform();
  800. Ellipse2D.Double el = new Ellipse2D.Double(0,0,300,200);
  801. fs.setAnchor(new Rectangle2D.Double(100,100,300,200));
  802. fs.setPath(new Path2D.Double(el));
  803. Color cExp = new Color(50,100,150,200);
  804. fs.setFillColor(cExp);
  805. HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
  806. ppt1.close();
  807. sl = ppt2.getSlides().get(0);
  808. fs = (HSLFFreeformShape)sl.getShapes().get(0);
  809. Color cAct = fs.getFillColor();
  810. assertEquals(cExp.getRed(), cAct.getRed());
  811. assertEquals(cExp.getGreen(), cAct.getGreen());
  812. assertEquals(cExp.getBlue(), cAct.getBlue());
  813. assertEquals(cExp.getAlpha(), cAct.getAlpha(), 1);
  814. PaintStyle ps = fs.getFillStyle().getPaint();
  815. assertTrue(ps instanceof SolidPaint);
  816. ColorStyle cs = ((SolidPaint)ps).getSolidColor();
  817. cAct = cs.getColor();
  818. assertEquals(cExp.getRed(), cAct.getRed());
  819. assertEquals(cExp.getGreen(), cAct.getGreen());
  820. assertEquals(cExp.getBlue(), cAct.getBlue());
  821. assertEquals(255, cAct.getAlpha());
  822. assertEquals(cExp.getAlpha()*100000./255., cs.getAlpha(), 1);
  823. ppt2.close();
  824. }
  825. @Test
  826. public void bug59302() throws IOException {
  827. //add extraction from PPT
  828. Map<String, String> macros = getMacrosFromHSLF("59302.ppt");
  829. assertNotNull("couldn't find macros", macros);
  830. assertNotNull("couldn't find second module", macros.get("Module2"));
  831. assertContains(macros.get("Module2"), "newMacro in Module2");
  832. assertNotNull("couldn't find first module", macros.get("Module1"));
  833. assertContains(macros.get("Module1"), "Italicize");
  834. macros = getMacrosFromHSLF("SimpleMacro.ppt");
  835. assertNotNull(macros.get("Module1"));
  836. assertContains(macros.get("Module1"), "This is a macro slideshow");
  837. }
  838. //It isn't pretty, but it works...
  839. private Map<String, String> getMacrosFromHSLF(String fileName) throws IOException {
  840. InputStream is = null;
  841. NPOIFSFileSystem npoifs = null;
  842. try {
  843. is = new FileInputStream(POIDataSamples.getSlideShowInstance().getFile(fileName));
  844. npoifs = new NPOIFSFileSystem(is);
  845. //TODO: should we run the VBAMacroReader on this npoifs?
  846. //TBD: We know that ppt typically don't store macros in the regular place,
  847. //but _can_ they?
  848. HSLFSlideShow ppt = new HSLFSlideShow(npoifs);
  849. //get macro persist id
  850. DocInfoListContainer list = (DocInfoListContainer)ppt.getDocumentRecord().findFirstOfType(RecordTypes.List.typeID);
  851. VBAInfoContainer vbaInfo = (VBAInfoContainer)list.findFirstOfType(RecordTypes.VBAInfo.typeID);
  852. VBAInfoAtom vbaAtom = (VBAInfoAtom)vbaInfo.findFirstOfType(RecordTypes.VBAInfoAtom.typeID);
  853. long persistId = vbaAtom.getPersistIdRef();
  854. for (HSLFObjectData objData : ppt.getEmbeddedObjects()) {
  855. if (objData.getExOleObjStg().getPersistId() == persistId) {
  856. return new VBAMacroReader(objData.getData()).readMacros();
  857. }
  858. }
  859. } finally {
  860. IOUtils.closeQuietly(npoifs);
  861. IOUtils.closeQuietly(is);
  862. }
  863. return null;
  864. }
  865. }