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 36KB

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