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.

TestXSLFBugs.java 52KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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.xslf;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.sl.draw.DrawTextParagraph.HYPERLINK_HREF;
  18. import static org.apache.poi.sl.draw.DrawTextParagraph.HYPERLINK_LABEL;
  19. import static org.apache.poi.xslf.XSLFTestDataSamples.openSampleDocument;
  20. import static org.apache.poi.xslf.XSLFTestDataSamples.writeOutAndReadBack;
  21. import static org.junit.jupiter.api.Assertions.*;
  22. import static org.junit.jupiter.api.Assumptions.assumeFalse;
  23. import java.awt.Color;
  24. import java.awt.LinearGradientPaint;
  25. import java.awt.MultipleGradientPaint;
  26. import java.awt.Paint;
  27. import java.awt.RadialGradientPaint;
  28. import java.awt.geom.Point2D;
  29. import java.awt.geom.Rectangle2D;
  30. import java.io.File;
  31. import java.io.FileInputStream;
  32. import java.io.IOException;
  33. import java.net.URI;
  34. import java.text.AttributedCharacterIterator;
  35. import java.text.AttributedCharacterIterator.Attribute;
  36. import java.text.CharacterIterator;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.Collection;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.Optional;
  43. import java.util.function.Function;
  44. import java.util.stream.Collectors;
  45. import java.util.stream.Stream;
  46. import org.apache.commons.io.output.NullOutputStream;
  47. import org.apache.commons.io.output.NullPrintStream;
  48. import org.apache.poi.POIDataSamples;
  49. import org.apache.poi.common.usermodel.HyperlinkType;
  50. import org.apache.poi.extractor.ExtractorFactory;
  51. import org.apache.poi.ooxml.POIXMLDocumentPart;
  52. import org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart;
  53. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  54. import org.apache.poi.openxml4j.opc.OPCPackage;
  55. import org.apache.poi.openxml4j.opc.PackagePartName;
  56. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  57. import org.apache.poi.sl.draw.DrawFactory;
  58. import org.apache.poi.sl.draw.DrawPaint;
  59. import org.apache.poi.sl.extractor.SlideShowExtractor;
  60. import org.apache.poi.sl.usermodel.Hyperlink;
  61. import org.apache.poi.sl.usermodel.PaintStyle;
  62. import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
  63. import org.apache.poi.sl.usermodel.PaintStyle.TexturePaint;
  64. import org.apache.poi.sl.usermodel.PictureData;
  65. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  66. import org.apache.poi.sl.usermodel.PictureShape;
  67. import org.apache.poi.sl.usermodel.Shape;
  68. import org.apache.poi.sl.usermodel.ShapeType;
  69. import org.apache.poi.sl.usermodel.Slide;
  70. import org.apache.poi.sl.usermodel.SlideShow;
  71. import org.apache.poi.sl.usermodel.SlideShowFactory;
  72. import org.apache.poi.sl.usermodel.TextParagraph;
  73. import org.apache.poi.sl.usermodel.TextRun;
  74. import org.apache.poi.sl.usermodel.TextShape;
  75. import org.apache.poi.sl.usermodel.VerticalAlignment;
  76. import org.apache.poi.xslf.usermodel.*;
  77. import org.apache.poi.xslf.util.DummyGraphics2d;
  78. import org.apache.poi.xssf.XSSFTestDataSamples;
  79. import org.junit.jupiter.api.BeforeAll;
  80. import org.junit.jupiter.api.Disabled;
  81. import org.junit.jupiter.api.Test;
  82. import org.junit.jupiter.api.function.ThrowingSupplier;
  83. import org.junit.jupiter.params.ParameterizedTest;
  84. import org.junit.jupiter.params.provider.ValueSource;
  85. import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
  86. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  87. class TestXSLFBugs {
  88. private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance();
  89. private static boolean xslfOnly;
  90. @BeforeAll
  91. public static void checkHslf() {
  92. try {
  93. Class.forName("org.apache.poi.hslf.usermodel.HSLFSlideShow");
  94. } catch (Exception e) {
  95. xslfOnly = true;
  96. }
  97. }
  98. @Test
  99. void bug62929() throws Exception {
  100. try (XMLSlideShow ss1 = openSampleDocument("missing-blip-fill.pptx")) {
  101. assertEquals(1, ss1.getSlides().size());
  102. XSLFSlide slide = ss1.getSlides().get(0);
  103. assertEquals(1,slide.getShapes().size());
  104. XSLFPictureShape picture = (XSLFPictureShape) slide.getShapes().get(0);
  105. assertEquals(662, picture.getShapeId());
  106. assertFalse(picture.isExternalLinkedPicture());
  107. assertNull(picture.getPictureData());
  108. assertNull(picture.getPictureLink());
  109. assertNull(picture.getClipping());
  110. }
  111. }
  112. @Test
  113. void bug62736() throws Exception {
  114. try (XMLSlideShow ss1 = openSampleDocument("bug62736.pptx")) {
  115. assertEquals(1, ss1.getSlides().size());
  116. XSLFSlide slide0 = ss1.getSlides().get(0);
  117. assertEquals(4, slide0.getShapes().size());
  118. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  119. assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
  120. assertRelation(slide0, "/ppt/media/image1.png", "rId2");
  121. assertEquals(2, slide0.getRelations().size());
  122. List<XSLFPictureShape> pictures = new ArrayList<>();
  123. for (XSLFShape shape : slide0.getShapes()) {
  124. if (shape instanceof XSLFPictureShape) {
  125. pictures.add((XSLFPictureShape) shape);
  126. }
  127. }
  128. assertEquals(2, pictures.size());
  129. assertEquals("image1.png", pictures.get(0).getPictureData().getFileName());
  130. assertEquals("image1.png", pictures.get(1).getPictureData().getFileName());
  131. // blipId is rId2 of both pictures
  132. // remove just the first picture
  133. slide0.removeShape(pictures.get(0));
  134. assertEquals(3, slide0.getShapes().size());
  135. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  136. assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
  137. // the bug is that the following relation is gone
  138. assertRelation(slide0, "/ppt/media/image1.png", "rId2");
  139. assertEquals(2, slide0.getRelations().size());
  140. // Save and re-load
  141. try (XMLSlideShow ss2 = writeOutAndReadBack(ss1)) {
  142. assertEquals(1, ss2.getSlides().size());
  143. slide0 = ss2.getSlides().get(0);
  144. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  145. assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
  146. assertRelation(slide0, "/ppt/media/image1.png", "rId2");
  147. assertEquals(2, slide0.getRelations().size());
  148. pictures.clear();
  149. for (XSLFShape shape : slide0.getShapes()) {
  150. if (shape instanceof XSLFPictureShape) {
  151. pictures.add((XSLFPictureShape) shape);
  152. }
  153. }
  154. assertEquals(1, pictures.size());
  155. assertEquals("image1.png", pictures.get(0).getPictureData().getFileName());
  156. slide0.removeShape(pictures.get(0));
  157. assertEquals(2, slide0.getShapes().size());
  158. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  159. assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
  160. assertNull(slide0.getRelationById("rId2"));
  161. assertEquals(1, slide0.getRelations().size());
  162. // Save and re-load
  163. try (XMLSlideShow ss3 = writeOutAndReadBack(ss2)) {
  164. assertEquals(1, ss3.getSlides().size());
  165. slide0 = ss3.getSlides().get(0);
  166. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  167. assertRelation(slide0, "/ppt/slideLayouts/slideLayout1.xml", "rId1");
  168. assertEquals(2, slide0.getShapes().size());
  169. }
  170. }
  171. }
  172. }
  173. @Test
  174. void bug61589() throws IOException {
  175. try (XMLSlideShow src = new XMLSlideShow();
  176. XMLSlideShow dest = new XMLSlideShow()) {
  177. XSLFSlide slide = src.createSlide();
  178. XSLFSlide slide2 = src.createSlide();
  179. XSLFTextBox shape = slide.createTextBox();
  180. shape.setAnchor(new Rectangle2D.Double(100, 100, 400, 100));
  181. XSLFTextParagraph p = shape.addNewTextParagraph();
  182. XSLFTextRun r = p.addNewTextRun();
  183. p.addLineBreak();
  184. r.setText("Apache POI");
  185. r.createHyperlink().setAddress("https://poi.apache.org");
  186. // create hyperlink pointing to a page, which isn't available at the time of importing the content
  187. r = p.addNewTextRun();
  188. r.setText("Slide 2");
  189. r.createHyperlink().linkToSlide(slide2);
  190. shape = slide2.createTextBox();
  191. shape.setAnchor(new Rectangle2D.Double(100, 100, 400, 100));
  192. shape.setText("slide 2");
  193. dest.createSlide().importContent(slide);
  194. dest.createSlide().importContent(slide2);
  195. try (XMLSlideShow ppt3 = writeOutAndReadBack(dest)) {
  196. XSLFSlide slide3 = ppt3.getSlides().get(0);
  197. XSLFTextBox shape3 = (XSLFTextBox) slide3.getShapes().get(0);
  198. XSLFTextParagraph p3 = shape3.getTextParagraphs().get(1);
  199. XSLFHyperlink h1 = p3.getTextRuns().get(0).getHyperlink();
  200. assertNotNull(h1);
  201. assertEquals("https://poi.apache.org", h1.getAddress());
  202. XSLFHyperlink h2 = p3.getTextRuns().get(2).getHyperlink();
  203. assertNotNull(h2);
  204. // relative url will be resolved to an absolute url, therefore this doesn't equals to "slide2.xml"
  205. assertEquals("/ppt/slides/slide2.xml", h2.getAddress());
  206. RelationPart sldRef = slide3.getRelationPartById(h2.getXmlObject().getId());
  207. assertTrue(sldRef.getDocumentPart() instanceof XSLFSlide);
  208. }
  209. }
  210. }
  211. @Test
  212. void bug62587() throws IOException {
  213. Object[][] pics = {
  214. {"santa.wmf", PictureType.WMF, XSLFRelation.IMAGE_WMF},
  215. {"tomcat.png", PictureType.PNG, XSLFRelation.IMAGE_PNG},
  216. {"clock.jpg", PictureType.JPEG, XSLFRelation.IMAGE_JPEG}
  217. };
  218. try (XMLSlideShow ppt1 = new XMLSlideShow()) {
  219. Slide<?, ?> slide = ppt1.createSlide();
  220. XSLFPictureData pd1 = ppt1.addPicture(slTests.getFile("wrench.emf"), PictureType.EMF);
  221. PictureShape<?, ?> ps1 = slide.createPicture(pd1);
  222. ps1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
  223. try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt1)) {
  224. XSLFSlide s1 = ppt2.getSlides().get(0);
  225. for (Object[] p : pics) {
  226. XSLFSlide s2 = ppt2.createSlide();
  227. s2.importContent(s1);
  228. XSLFPictureData pd2 = ppt2.addPicture(slTests.getFile((String) p[0]), (PictureType) p[1]);
  229. XSLFPictureShape ps2 = (XSLFPictureShape) s2.getShapes().get(0);
  230. Rectangle2D anchor2 = ps2.getAnchor();
  231. s2.removeShape(ps2);
  232. ps2 = s2.createPicture(pd2);
  233. ps2.setAnchor(anchor2);
  234. }
  235. try (XMLSlideShow ppt3 = writeOutAndReadBack(ppt2)) {
  236. for (XSLFSlide sl : ppt3.getSlides()) {
  237. List<RelationPart> rels = sl.getRelationParts();
  238. assertEquals(2, rels.size());
  239. RelationPart rel0 = rels.get(0);
  240. assertEquals("rId1", rel0.getRelationship().getId());
  241. assertEquals(XSLFRelation.SLIDE_LAYOUT.getRelation(), rel0.getRelationship().getRelationshipType());
  242. RelationPart rel1 = rels.get(1);
  243. assertNotEquals("rId1", rel1.getRelationship().getId());
  244. assertEquals(XSLFRelation.IMAGES.getRelation(), rel1.getRelationship().getRelationshipType());
  245. }
  246. }
  247. }
  248. }
  249. }
  250. @Test
  251. void bug60499() throws IOException, InvalidFormatException {
  252. PackagePartName ppn = PackagingURIHelper.createPartName("/ppt/media/image1.png");
  253. try (XMLSlideShow ppt1 = openSampleDocument("bug60499.pptx")) {
  254. XSLFSlide slide1 = ppt1.getSlides().get(0);
  255. Optional<XSLFShape> shapeToDelete1 =
  256. slide1.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).findFirst();
  257. assertTrue(shapeToDelete1.isPresent());
  258. slide1.removeShape(shapeToDelete1.get());
  259. assertTrue(slide1.getRelationParts().stream()
  260. .allMatch(rp -> "rId1,rId3".contains(rp.getRelationship().getId())));
  261. assertNotNull(ppt1.getPackage().getPart(ppn));
  262. }
  263. try (XMLSlideShow ppt2 = openSampleDocument("bug60499.pptx")) {
  264. XSLFSlide slide2 = ppt2.getSlides().get(0);
  265. Optional<XSLFShape> shapeToDelete2 =
  266. slide2.getShapes().stream().filter(s -> s instanceof XSLFPictureShape).skip(1).findFirst();
  267. assertTrue(shapeToDelete2.isPresent());
  268. slide2.removeShape(shapeToDelete2.get());
  269. assertTrue(slide2.getRelationParts().stream()
  270. .allMatch(rp -> "rId1,rId2".contains(rp.getRelationship().getId())));
  271. assertNotNull(ppt2.getPackage().getPart(ppn));
  272. }
  273. try (XMLSlideShow ppt3 = openSampleDocument("bug60499.pptx")) {
  274. XSLFSlide slide3 = ppt3.getSlides().get(0);
  275. slide3.getShapes().stream()
  276. .filter(s -> s instanceof XSLFPictureShape)
  277. .collect(Collectors.toList())
  278. .forEach(slide3::removeShape);
  279. assertNull(ppt3.getPackage().getPart(ppn));
  280. }
  281. }
  282. @Test
  283. void bug51187() throws Exception {
  284. try (XMLSlideShow ss1 = openSampleDocument("51187.pptx")) {
  285. assertEquals(1, ss1.getSlides().size());
  286. // Check the relations on it
  287. // Note - rId3 is a self reference
  288. XSLFSlide slide0 = ss1.getSlides().get(0);
  289. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  290. assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
  291. assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
  292. assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
  293. assertRelation(slide0, "/ppt/media/image1.png", "rId4");
  294. // Save and re-load
  295. try (XMLSlideShow ss2 = writeOutAndReadBack(ss1)) {
  296. assertEquals(1, ss2.getSlides().size());
  297. slide0 = ss2.getSlides().get(0);
  298. assertRelation(slide0, "/ppt/slides/slide1.xml", null);
  299. assertRelation(slide0, "/ppt/slideLayouts/slideLayout12.xml", "rId1");
  300. assertRelation(slide0, "/ppt/notesSlides/notesSlide1.xml", "rId2");
  301. // TODO Fix this
  302. assertRelation(slide0, "/ppt/slides/slide1.xml", "rId3");
  303. assertRelation(slide0, "/ppt/media/image1.png", "rId4");
  304. }
  305. }
  306. }
  307. private static void assertRelation(XSLFSlide slide, String exp, String rId) {
  308. POIXMLDocumentPart pd = (rId != null) ? slide.getRelationById(rId) : slide;
  309. assertNotNull(pd);
  310. assertEquals(exp, pd.getPackagePart().getPartName().getName());
  311. }
  312. /**
  313. * Slide relations with anchors in them
  314. */
  315. @Test
  316. void tika705() throws Exception {
  317. try (XMLSlideShow ss = openSampleDocument("with_japanese.pptx")) {
  318. // Should have one slide
  319. assertEquals(1, ss.getSlides().size());
  320. XSLFSlide slide = ss.getSlides().get(0);
  321. // Check the relations from this
  322. Collection<RelationPart> rels = slide.getRelationParts();
  323. // Should have 6 relations:
  324. // 1 external hyperlink (skipped from list)
  325. // 4 internal hyperlinks
  326. // 1 slide layout
  327. assertEquals(5, rels.size());
  328. int layouts = 0;
  329. int hyperlinks = 0;
  330. for (RelationPart p : rels) {
  331. if (p.getRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
  332. hyperlinks++;
  333. } else if (p.getDocumentPart() instanceof XSLFSlideLayout) {
  334. layouts++;
  335. }
  336. }
  337. assertEquals(1, layouts);
  338. assertEquals(4, hyperlinks);
  339. // Hyperlinks should all be to #_ftn1 or #ftnref1
  340. for (RelationPart p : rels) {
  341. if (p.getRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
  342. URI target = p.getRelationship().getTargetURI();
  343. String frag = target.getFragment();
  344. assertTrue(frag.equals("_ftn1") || frag.equals("_ftnref1"), "Invalid target " + frag + " on " + target);
  345. }
  346. }
  347. }
  348. }
  349. /**
  350. * A slideshow can have more than one rID pointing to a given
  351. * slide, eg presentation.xml rID1 -> slide1.xml, but slide1.xml
  352. * rID2 -> slide3.xml
  353. */
  354. @Test
  355. void bug54916() throws IOException {
  356. try (XMLSlideShow ss = openSampleDocument("OverlappingRelations.pptx")) {
  357. XSLFSlide slide;
  358. // Should find 4 slides
  359. assertEquals(4, ss.getSlides().size());
  360. // Check the text, to see we got them in order
  361. slide = ss.getSlides().get(0);
  362. assertContains(getSlideText(ss, slide), "POI cannot read this");
  363. slide = ss.getSlides().get(1);
  364. assertContains(getSlideText(ss, slide), "POI can read this");
  365. assertContains(getSlideText(ss, slide), "Has a relationship to another slide");
  366. slide = ss.getSlides().get(2);
  367. assertContains(getSlideText(ss, slide), "POI can read this");
  368. slide = ss.getSlides().get(3);
  369. assertContains(getSlideText(ss, slide), "POI can read this");
  370. }
  371. }
  372. /**
  373. * When the picture is not embedded but inserted only as a "link to file",
  374. * there is no data available and XSLFPictureShape.getPictureData()
  375. * gives a NPE, see bug #56812
  376. */
  377. @Test
  378. void bug56812() throws Exception {
  379. try (XMLSlideShow ppt = openSampleDocument("56812.pptx")) {
  380. int internalPictures = 0;
  381. int externalPictures = 0;
  382. for (XSLFSlide slide : ppt.getSlides()) {
  383. for (XSLFShape shape : slide.getShapes()) {
  384. assertNotNull(shape);
  385. if (shape instanceof XSLFPictureShape) {
  386. XSLFPictureShape picture = (XSLFPictureShape) shape;
  387. if (picture.isExternalLinkedPicture()) {
  388. externalPictures++;
  389. assertNotNull(picture.getPictureLink());
  390. } else {
  391. internalPictures++;
  392. XSLFPictureData data = picture.getPictureData();
  393. assertNotNull(data);
  394. assertNotNull(data.getFileName());
  395. }
  396. }
  397. }
  398. }
  399. assertEquals(2, internalPictures);
  400. assertEquals(1, externalPictures);
  401. }
  402. }
  403. private String getSlideText(XMLSlideShow ppt, XSLFSlide slide) throws IOException {
  404. try (SlideShowExtractor<XSLFShape, XSLFTextParagraph> extr = new SlideShowExtractor<>(ppt)) {
  405. // do not auto-close the slideshow
  406. extr.setCloseFilesystem(false);
  407. extr.setSlidesByDefault(true);
  408. extr.setNotesByDefault(false);
  409. extr.setMasterByDefault(false);
  410. return extr.getText(slide);
  411. }
  412. }
  413. @Test
  414. void bug57250() throws Exception {
  415. try (XMLSlideShow ss = new XMLSlideShow()) {
  416. for (String s : new String[]{"Slide1", "Slide2"}) {
  417. ss.createSlide().createTextBox().setText(s);
  418. }
  419. validateSlides(ss, false, "Slide1", "Slide2");
  420. XSLFSlide slide = ss.createSlide();
  421. slide.createTextBox().setText("New slide");
  422. validateSlides(ss, true, "Slide1", "Slide2", "New slide");
  423. // Move backward
  424. ss.setSlideOrder(slide, 0);
  425. validateSlides(ss, true, "New slide", "Slide1", "Slide2");
  426. // Move forward
  427. ss.setSlideOrder(slide, 1);
  428. validateSlides(ss, true, "Slide1", "New slide", "Slide2");
  429. // Move to end
  430. ss.setSlideOrder(slide, 0);
  431. ss.setSlideOrder(slide, 2);
  432. validateSlides(ss, true, "Slide1", "Slide2", "New slide");
  433. }
  434. }
  435. /**
  436. * When working with >9 images, make sure the sorting ensures
  437. * that image10.foo isn't between image1.foo and image2.foo
  438. */
  439. @Test
  440. void test57552() throws Exception {
  441. try (XMLSlideShow ss = new XMLSlideShow()) {
  442. for (String s : new String[]{"Slide1", "Slide2"}) {
  443. ss.createSlide().createTextBox().setText(s);
  444. }
  445. // Slide starts with just layout relation
  446. XSLFSlide slide = ss.getSlides().get(0);
  447. assertEquals(0, ss.getPictureData().size());
  448. assertEquals(1, slide.getShapes().size());
  449. assertEquals(1, slide.getRelations().size());
  450. final XSLFRelation expected = XSLFRelation.SLIDE_LAYOUT;
  451. final POIXMLDocumentPart relation = slide.getRelations().get(0);
  452. assertEquals(expected.getContentType(), relation.getPackagePart().getContentType());
  453. assertEquals(expected.getFileName(expected.getFileNameIndex(relation)), relation.getPackagePart().getPartName().getName());
  454. // Some dummy pictures
  455. byte[][] pics = new byte[15][3];
  456. for (int i = 0; i < pics.length; i++) {
  457. Arrays.fill(pics[i], (byte) i);
  458. }
  459. // Add a few pictures
  460. addPictures(ss, slide, pics, 0, 10);
  461. // Re-fetch the pictures and check
  462. for (int i = 0; i < 10; i++) {
  463. XSLFPictureShape shape = (XSLFPictureShape) slide.getShapes().get(i + 1);
  464. assertNotNull(shape.getPictureData());
  465. assertArrayEquals(pics[i], shape.getPictureData().getData());
  466. }
  467. // Add past 10
  468. addPictures(ss, slide, pics, 10, 15);
  469. // Check all pictures
  470. for (int i = 0; i < 15; i++) {
  471. XSLFPictureShape shape = (XSLFPictureShape) slide.getShapes().get(i + 1);
  472. assertNotNull(shape.getPictureData());
  473. assertArrayEquals(pics[i], shape.getPictureData().getData());
  474. }
  475. // Add a duplicate, check the right one is picked
  476. XSLFPictureData data = ss.addPicture(pics[3], PictureType.JPEG);
  477. assertEquals(3, data.getIndex());
  478. assertEquals(15, ss.getPictureData().size());
  479. XSLFPictureShape shape = slide.createPicture(data);
  480. assertNotNull(shape.getPictureData());
  481. assertArrayEquals(pics[3], shape.getPictureData().getData());
  482. assertEquals(17, slide.getShapes().size());
  483. // Save and re-load
  484. try (XMLSlideShow ss2 = writeOutAndReadBack(ss)) {
  485. slide = ss2.getSlides().get(0);
  486. // Check the 15 individual ones added
  487. for (int i = 0; i < 15; i++) {
  488. shape = (XSLFPictureShape) slide.getShapes().get(i + 1);
  489. assertNotNull(shape.getPictureData());
  490. assertArrayEquals(pics[i], shape.getPictureData().getData());
  491. }
  492. // Check the duplicate
  493. shape = (XSLFPictureShape) slide.getShapes().get(16);
  494. assertNotNull(shape.getPictureData());
  495. assertArrayEquals(pics[3], shape.getPictureData().getData());
  496. // Add another duplicate
  497. data = ss2.addPicture(pics[5], PictureType.JPEG);
  498. assertEquals(5, data.getIndex());
  499. assertEquals(15, ss2.getPictureData().size());
  500. shape = slide.createPicture(data);
  501. assertNotNull(shape.getPictureData());
  502. assertArrayEquals(pics[5], shape.getPictureData().getData());
  503. assertEquals(18, slide.getShapes().size());
  504. }
  505. }
  506. }
  507. private void addPictures(XMLSlideShow ss, XSLFSlide slide, byte[][] pics, int start, int end) {
  508. for (int i = start; i < end; i++) {
  509. XSLFPictureData data = ss.addPicture(pics[i], PictureType.JPEG);
  510. assertEquals(i, data.getIndex());
  511. assertEquals(i + 1, ss.getPictureData().size());
  512. XSLFPictureShape shape = slide.createPicture(data);
  513. assertNotNull(shape.getPictureData());
  514. assertArrayEquals(pics[i], shape.getPictureData().getData());
  515. assertEquals(i + 2, slide.getShapes().size());
  516. }
  517. }
  518. private void validateSlides(XMLSlideShow ss, boolean saveAndReload, String... slideTexts) throws IOException {
  519. if (saveAndReload) {
  520. try (XMLSlideShow ss2 = writeOutAndReadBack(ss)) {
  521. validateSlides(ss2, slideTexts);
  522. }
  523. } else {
  524. validateSlides(ss, slideTexts);
  525. }
  526. }
  527. private void validateSlides(XMLSlideShow ss, String... slideTexts) throws IOException {
  528. assertEquals(slideTexts.length, ss.getSlides().size());
  529. for (int i = 0; i < slideTexts.length; i++) {
  530. XSLFSlide slide = ss.getSlides().get(i);
  531. assertContains(getSlideText(ss, slide), slideTexts[i]);
  532. }
  533. }
  534. @Test
  535. void bug58205() throws IOException {
  536. try (XMLSlideShow ss = openSampleDocument("themes.pptx")) {
  537. int i = 1;
  538. for (XSLFSlideMaster sm : ss.getSlideMasters()) {
  539. assertEquals("rId" + (i++), ss.getRelationId(sm));
  540. }
  541. }
  542. }
  543. @ParameterizedTest
  544. @ValueSource(strings = {"45541_Footer.pptx", "SampleShow.pptx"})
  545. void bug55791(String fileName) throws IOException {
  546. try (XMLSlideShow ppt = openSampleDocument(fileName)) {
  547. assertTrue(ppt.getSlides().size() > 1);
  548. ppt.removeSlide(1);
  549. assertNotNull(ppt.createSlide());
  550. }
  551. }
  552. @Test
  553. void blibFillAlternateContent() throws IOException {
  554. try (XMLSlideShow ppt = openSampleDocument("2411-Performance_Up.pptx")) {
  555. XSLFPictureShape ps = (XSLFPictureShape) ppt.getSlides().get(4).getShapes().get(0);
  556. assertNotNull(ps.getPictureData());
  557. }
  558. }
  559. @Test
  560. void bug59434() throws IOException {
  561. String url1 = "https://poi.apache.org/changes.html";
  562. String url2 = "https://poi.apache.org/faq.html";
  563. try (XMLSlideShow ppt1 = new XMLSlideShow()) {
  564. PictureData pd1 = ppt1.addPicture(slTests.readFile("tomcat.png"), PictureType.PNG);
  565. PictureData pd2 = ppt1.addPicture(slTests.readFile("santa.wmf"), PictureType.WMF);
  566. XSLFSlide slide = ppt1.createSlide();
  567. XSLFPictureShape ps1 = slide.createPicture(pd1);
  568. ps1.setAnchor(new Rectangle2D.Double(20, 20, 100, 100));
  569. XSLFHyperlink hl1 = ps1.createHyperlink();
  570. hl1.linkToUrl(url1);
  571. XSLFPictureShape ps2 = slide.createPicture(pd2);
  572. ps2.setAnchor(new Rectangle2D.Double(120, 120, 100, 100));
  573. XSLFHyperlink hl2 = ps2.createHyperlink();
  574. hl2.linkToUrl(url2);
  575. try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt1)) {
  576. slide = ppt2.getSlides().get(0);
  577. ps1 = (XSLFPictureShape) slide.getShapes().get(0);
  578. ps2 = (XSLFPictureShape) slide.getShapes().get(1);
  579. assertEquals(url1, ps1.getHyperlink().getAddress());
  580. assertEquals(url2, ps2.getHyperlink().getAddress());
  581. }
  582. }
  583. }
  584. @Test
  585. void bug58217() throws IOException {
  586. Color fillColor = new Color(1f, 1f, 0f, 0.1f);
  587. Color lineColor = new Color(25.3f / 255f, 1f, 0f, 0.4f);
  588. Color textColor = new Color(1f, 1f, 0f, 0.6f);
  589. try (XMLSlideShow ppt1 = new XMLSlideShow()) {
  590. XSLFSlide sl = ppt1.createSlide();
  591. XSLFAutoShape as = sl.createAutoShape();
  592. as.setShapeType(ShapeType.STAR_10);
  593. as.setAnchor(new Rectangle2D.Double(100, 100, 300, 300));
  594. as.setFillColor(fillColor);
  595. as.setLineColor(lineColor);
  596. as.setText("Alpha");
  597. as.setVerticalAlignment(VerticalAlignment.MIDDLE);
  598. as.setHorizontalCentered(true);
  599. XSLFTextRun tr = as.getTextParagraphs().get(0).getTextRuns().get(0);
  600. tr.setFontSize(32d);
  601. tr.setFontColor(textColor);
  602. try (XMLSlideShow ppt2 = writeOutAndReadBack(ppt1)) {
  603. sl = ppt2.getSlides().get(0);
  604. as = (XSLFAutoShape) sl.getShapes().get(0);
  605. checkColor(fillColor, as.getFillStyle().getPaint());
  606. checkColor(lineColor, as.getStrokeStyle().getPaint());
  607. checkColor(textColor, as.getTextParagraphs().get(0).getTextRuns().get(0).getFontColor());
  608. }
  609. }
  610. }
  611. private static void checkColor(Color expected, PaintStyle actualStyle) {
  612. assertTrue(actualStyle instanceof SolidPaint);
  613. SolidPaint ps = (SolidPaint) actualStyle;
  614. Color actual = DrawPaint.applyColorTransform(ps.getSolidColor());
  615. float[] expRGB = expected.getRGBComponents(null);
  616. float[] actRGB = actual.getRGBComponents(null);
  617. assertArrayEquals(expRGB, actRGB, 0.0001f);
  618. }
  619. @Test
  620. void bug55714() throws IOException {
  621. try (XMLSlideShow srcPptx = openSampleDocument("pptx2svg.pptx");
  622. XMLSlideShow newPptx = new XMLSlideShow()) {
  623. XSLFSlide srcSlide = srcPptx.getSlides().get(0);
  624. XSLFSlide newSlide = newPptx.createSlide();
  625. XSLFSlideLayout srcSlideLayout = srcSlide.getSlideLayout();
  626. XSLFSlideLayout newSlideLayout = newSlide.getSlideLayout();
  627. newSlideLayout.importContent(srcSlideLayout);
  628. XSLFSlideMaster srcSlideMaster = srcSlide.getSlideMaster();
  629. XSLFSlideMaster newSlideMaster = newSlide.getSlideMaster();
  630. newSlideMaster.importContent(srcSlideMaster);
  631. newSlide.importContent(srcSlide);
  632. try (XMLSlideShow rwPptx = writeOutAndReadBack(newPptx)) {
  633. PaintStyle ps = rwPptx.getSlides().get(0).getBackground().getFillStyle().getPaint();
  634. assertTrue(ps instanceof TexturePaint);
  635. }
  636. }
  637. }
  638. @Test
  639. void bug59273() throws IOException {
  640. try (XMLSlideShow ppt = openSampleDocument("bug59273.potx")) {
  641. ppt.getPackage().replaceContentType(
  642. XSLFRelation.PRESENTATIONML_TEMPLATE.getContentType(),
  643. XSLFRelation.MAIN.getContentType()
  644. );
  645. try (XMLSlideShow rwPptx = writeOutAndReadBack(ppt)) {
  646. OPCPackage pkg = rwPptx.getPackage();
  647. int size = pkg.getPartsByContentType(XSLFRelation.MAIN.getContentType()).size();
  648. assertEquals(1, size);
  649. size = pkg.getPartsByContentType(XSLFRelation.PRESENTATIONML_TEMPLATE.getContentType()).size();
  650. assertEquals(0, size);
  651. }
  652. }
  653. }
  654. @Test
  655. void bug60373() throws IOException {
  656. try (XMLSlideShow ppt = new XMLSlideShow()) {
  657. XSLFSlide sl = ppt.createSlide();
  658. XSLFTable t = sl.createTable();
  659. XSLFTableRow r = t.addRow();
  660. bug60373_addCell(r);
  661. bug60373_addCell(r);
  662. r = t.addRow();
  663. XSLFTableCell c = bug60373_addCell(r);
  664. // call getTextHeight, when table is not fully populated
  665. double th = c.getTextHeight();
  666. assertTrue(th > 10);
  667. }
  668. }
  669. private static XSLFTableCell bug60373_addCell(XSLFTableRow r) {
  670. XSLFTableCell cell = r.addCell();
  671. XSLFTextParagraph p = cell.addNewTextParagraph();
  672. XSLFTextRun tr = p.addNewTextRun();
  673. tr.setText("t");
  674. return cell;
  675. }
  676. @Test
  677. void bug60715() throws IOException {
  678. try (XMLSlideShow ppt = openSampleDocument("bug60715.pptx")) {
  679. assertDoesNotThrow((ThrowingSupplier<XSLFSlide>) ppt::createSlide);
  680. }
  681. }
  682. @Test
  683. void bug60662() throws IOException {
  684. try (XMLSlideShow src = new XMLSlideShow();
  685. XMLSlideShow dst = new XMLSlideShow()) {
  686. XSLFSlide sl = src.createSlide();
  687. XSLFGroupShape gs = sl.createGroup();
  688. gs.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
  689. gs.setInteriorAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  690. XSLFAutoShape as = gs.createAutoShape();
  691. as.setAnchor(new Rectangle2D.Double(0, 0, 100, 100));
  692. as.setShapeType(ShapeType.STAR_24);
  693. as.setFillColor(Color.YELLOW);
  694. CTShape csh = (CTShape) as.getXmlObject();
  695. CTOuterShadowEffect shadow = csh.getSpPr().addNewEffectLst().addNewOuterShdw();
  696. shadow.setDir(270000);
  697. shadow.setDist(100000);
  698. shadow.addNewSrgbClr().setVal(new byte[]{0x00, (byte) 0xFF, 0x00});
  699. XSLFSlide sl2 = dst.createSlide();
  700. sl2.importContent(sl);
  701. XSLFGroupShape gs2 = (XSLFGroupShape) sl2.getShapes().get(0);
  702. XSLFAutoShape as2 = (XSLFAutoShape) gs2.getShapes().get(0);
  703. CTShape csh2 = (CTShape) as2.getXmlObject();
  704. assertTrue(csh2.getSpPr().isSetEffectLst());
  705. }
  706. }
  707. @Test
  708. void test60810() throws IOException {
  709. try (XMLSlideShow ppt = openSampleDocument("60810.pptx")) {
  710. for (XSLFSlide slide : ppt.getSlides()) {
  711. XSLFNotes notesSlide = ppt.getNotesSlide(slide);
  712. assertNotNull(notesSlide);
  713. }
  714. }
  715. }
  716. @Test
  717. void test60042() throws IOException {
  718. try (XMLSlideShow ppt = openSampleDocument("60042.pptx")) {
  719. ppt.removeSlide(0);
  720. ppt.createSlide();
  721. assertEquals(2, ppt.getSlides().size());
  722. }
  723. }
  724. @Test
  725. void test61515() throws IOException {
  726. try (XMLSlideShow ppt = openSampleDocument("61515.pptx")) {
  727. ppt.removeSlide(0);
  728. assertEquals(1, ppt.createSlide().getRelations().size());
  729. try (XMLSlideShow saved = writeOutAndReadBack(ppt)) {
  730. assertEquals(1, saved.getSlides().size());
  731. XSLFSlide slide = saved.getSlides().get(0);
  732. assertEquals(1, slide.getRelations().size());
  733. }
  734. }
  735. }
  736. @Test
  737. void testAptia() throws IOException {
  738. try (XMLSlideShow ppt = openSampleDocument("aptia.pptx");
  739. XMLSlideShow saved = writeOutAndReadBack(ppt)) {
  740. assertEquals(ppt.getSlides().size(), saved.getSlides().size());
  741. }
  742. }
  743. @Disabled
  744. @Test
  745. void testDivinoRevelado() throws IOException {
  746. try (XMLSlideShow ppt = openSampleDocument("Divino_Revelado.pptx");
  747. XMLSlideShow saved = writeOutAndReadBack(ppt)) {
  748. assertEquals(ppt.getSlides().size(), saved.getSlides().size());
  749. }
  750. }
  751. @Test
  752. void bug62051() throws IOException {
  753. final Function<List<XSLFShape>, int[]> ids = (shapes) ->
  754. shapes.stream().mapToInt(Shape::getShapeId).toArray();
  755. try (final XMLSlideShow ppt = new XMLSlideShow()) {
  756. final XSLFSlide slide = ppt.createSlide();
  757. final List<XSLFShape> shapes = new ArrayList<>();
  758. shapes.add(slide.createAutoShape());
  759. final XSLFGroupShape g1 = slide.createGroup();
  760. shapes.add(g1);
  761. final XSLFGroupShape g2 = g1.createGroup();
  762. shapes.add(g2);
  763. shapes.add(g2.createAutoShape());
  764. shapes.add(slide.createAutoShape());
  765. shapes.add(g2.createAutoShape());
  766. shapes.add(g1.createAutoShape());
  767. assertArrayEquals(new int[]{2, 3, 4, 5, 6, 7, 8}, ids.apply(shapes));
  768. g1.removeShape(g2);
  769. shapes.remove(5);
  770. shapes.remove(3);
  771. shapes.remove(2);
  772. shapes.add(g1.createAutoShape());
  773. assertArrayEquals(new int[]{2, 3, 6, 8, 4}, ids.apply(shapes));
  774. }
  775. }
  776. @Test
  777. void bug63200() throws Exception {
  778. try (XMLSlideShow ss1 = openSampleDocument("63200.pptx")) {
  779. assertEquals(1, ss1.getSlides().size());
  780. XSLFSlide slide = ss1.getSlides().get(0);
  781. assertEquals(1, slide.getShapes().size());
  782. XSLFGroupShape group = (XSLFGroupShape) slide.getShapes().get(0);
  783. assertEquals(2, group.getShapes().size());
  784. XSLFAutoShape oval = (XSLFAutoShape) group.getShapes().get(0);
  785. XSLFAutoShape arrow = (XSLFAutoShape) group.getShapes().get(1);
  786. assertNull(oval.getFillColor());
  787. assertNull(arrow.getFillColor());
  788. }
  789. }
  790. @Test
  791. void alternateContent() throws Exception {
  792. try (XMLSlideShow ppt = openSampleDocument("alterman_security.pptx")) {
  793. XSLFSlideMaster slide = ppt.getSlideMasters().get(0);
  794. XSLFObjectShape os = (XSLFObjectShape) slide.getShapes().get(0);
  795. // ctOleObject is nested in AlternateContent in this file
  796. // if there are casting errors, we would fail early and wouldn't reach this point anyway
  797. assertNotNull(os.getCTOleObject());
  798. // accessing the picture data of the AlternateContent fallback part
  799. XSLFPictureData picData = os.getPictureData();
  800. assertNotNull(picData);
  801. }
  802. try (XMLSlideShow ppt = openSampleDocument("2411-Performance_Up.pptx")) {
  803. XSLFSlide slide = ppt.getSlides().get(4);
  804. XSLFPictureShape ps = (XSLFPictureShape) slide.getShapes().get(0);
  805. assertEquals("image4.png", ps.getPictureData().getFileName());
  806. assertEquals("Picture 5", ps.getShapeName());
  807. }
  808. }
  809. @Test
  810. void bug57796() throws IOException {
  811. assumeFalse(xslfOnly);
  812. try (SlideShow<?, ?> ppt = SlideShowFactory.create(slTests.getFile("WithLinks.ppt"))) {
  813. Slide<?, ?> slide = ppt.getSlides().get(0);
  814. TextShape<?, ?> shape = (TextShape<?, ?>) slide.getShapes().get(1);
  815. TextRun r = shape.getTextParagraphs().get(1).getTextRuns().get(0);
  816. Hyperlink<?, ?> hlRun = r.getHyperlink();
  817. assertNotNull(hlRun);
  818. assertEquals("http://jakarta.apache.org/poi/", hlRun.getAddress());
  819. assertEquals("http://jakarta.apache.org/poi/", hlRun.getLabel());
  820. assertEquals(HyperlinkType.URL, hlRun.getType());
  821. final List<Object> strings = new ArrayList<>();
  822. DummyGraphics2d dgfx = new DummyGraphics2d(new NullPrintStream()) {
  823. @Override
  824. public void drawString(AttributedCharacterIterator iterator, float x, float y) {
  825. // For the test file, common sl draws textruns one by one and not mixed
  826. // so we evaluate the whole iterator
  827. Map<Attribute, Object> attributes = null;
  828. StringBuilder sb = new StringBuilder();
  829. for (char c = iterator.first();
  830. c != CharacterIterator.DONE;
  831. c = iterator.next()) {
  832. sb.append(c);
  833. attributes = iterator.getAttributes();
  834. }
  835. if ("Jakarta HSSF".equals(sb.toString())) {
  836. // this is a test for a manually modified ppt, for real hyperlink label
  837. // one would need to access the screen tip record
  838. Attribute[] obj = {HYPERLINK_HREF, HYPERLINK_LABEL};
  839. assertNotNull(attributes);
  840. Stream.of(obj).map(attributes::get).forEach(strings::add);
  841. }
  842. }
  843. };
  844. ppt.getSlides().get(1).draw(dgfx);
  845. assertEquals(2, strings.size());
  846. assertEquals("http://jakarta.apache.org/poi/hssf/", strings.get(0));
  847. assertEquals("Open Jakarta POI HSSF module test ", strings.get(1));
  848. }
  849. }
  850. @Test
  851. void bug59056() throws IOException {
  852. assumeFalse(xslfOnly);
  853. final double[][] clips = {
  854. { 50.999999999999986, 51.0, 298.0, 98.0 },
  855. { 51.00000000000003, 51.0, 298.0, 98.0 },
  856. { 51.0, 51.0, 298.0, 98.0 },
  857. { 250.02000796164992, 93.10370370370373, 78.61839367617523, 55.89629629629627 },
  858. { 79.58198774450841, 53.20887318960063, 109.13118501448272, 9.40935058567127 },
  859. };
  860. DummyGraphics2d dgfx = new DummyGraphics2d(new NullPrintStream()) {
  861. int idx = 0;
  862. @Override
  863. public void clip(java.awt.Shape s) {
  864. assertTrue(s instanceof Rectangle2D);
  865. Rectangle2D r = (Rectangle2D)s;
  866. double[] clip = clips[idx++];
  867. assertEquals(clip[0], r.getX(), 0.5);
  868. assertEquals(clip[1], r.getY(), 0.5);
  869. assertEquals(clip[2], r.getWidth(), 0.5);
  870. assertEquals(clip[3], r.getHeight(), 0.5);
  871. }
  872. };
  873. Rectangle2D box = new Rectangle2D.Double(51, 51, 298, 98);
  874. DrawFactory df = DrawFactory.getInstance(dgfx);
  875. try (SlideShow<?,?> ppt = SlideShowFactory.create(slTests.getFile("54541_cropped_bitmap.ppt"))) {
  876. ppt.getSlides().get(0).getShapes().forEach(shape -> df.drawShape(dgfx, shape, box));
  877. }
  878. }
  879. @Test
  880. public void bug65228() throws IOException {
  881. try (XMLSlideShow ppt = openSampleDocument("bug65228.pptx")) {
  882. TextRun.TextCap act = ppt.getSlides().stream()
  883. .flatMap(s -> s.getShapes().stream())
  884. .filter(s -> "März 2021\u2026".equals(s.getShapeName()))
  885. .map(XSLFTextShape.class::cast)
  886. .flatMap(s -> s.getTextParagraphs().stream())
  887. .flatMap(s -> s.getTextRuns().stream())
  888. .map(XSLFTextRun::getTextCap)
  889. .findFirst().orElse(null);
  890. assertEquals(TextRun.TextCap.ALL, act);
  891. }
  892. }
  893. @Test
  894. public void bug65523() throws IOException {
  895. try (XMLSlideShow sourcePresentation = openSampleDocument("bug65523.pptx")) {
  896. assertEquals(2, sourcePresentation.getPictureData().size());
  897. XMLSlideShow targetPresentation = new XMLSlideShow();
  898. XSLFSlide targetPresentationSlide = targetPresentation.createSlide();
  899. XSLFSlide sourceSlide = sourcePresentation.getSlides().get(0);
  900. targetPresentationSlide.getSlideMaster().importContent(sourceSlide.getSlideMaster());
  901. targetPresentationSlide.getSlideLayout().importContent(sourceSlide.getSlideLayout());
  902. targetPresentationSlide.importContent(sourceSlide);
  903. XSLFSlide targetSlide = targetPresentation.getSlides().get(0);
  904. assertNotNull(targetSlide);
  905. assertEquals(2, targetPresentation.getPictureData().size());
  906. targetPresentation.write(NullOutputStream.NULL_OUTPUT_STREAM);
  907. }
  908. }
  909. @Test
  910. public void bug65551() throws IOException {
  911. try (XMLSlideShow ppt = openSampleDocument("bug65551.pptx")) {
  912. XSLFTextShape shape = (XSLFTextShape)ppt.getSlideMasters().get(0).getShapes().get(1);
  913. XSLFTextParagraph tp = shape.getTextParagraphs().get(0);
  914. assertEquals(TextParagraph.TextAlign.RIGHT, tp.getTextAlign());
  915. XSLFTextRun tr = tp.getTextRuns().get(0);
  916. PaintStyle fc = tr.getFontColor();
  917. assertTrue(fc instanceof SolidPaint);
  918. SolidPaint sp = (SolidPaint)fc;
  919. assertEquals(Color.RED, sp.getSolidColor().getColor());
  920. }
  921. }
  922. @Test
  923. void bug65634() throws IOException {
  924. File file = XSSFTestDataSamples.getSampleFile("workbook.xml");
  925. try (FileInputStream fis = new FileInputStream(file)) {
  926. IOException ex = assertThrows(IOException.class, () -> SlideShowFactory.create(fis));
  927. assertEquals("Can't open slideshow - unsupported file type: XML", ex.getMessage());
  928. }
  929. IOException ie = assertThrows(IOException.class, () -> SlideShowFactory.create(file));
  930. assertEquals("Can't open slideshow - unsupported file type: XML", ie.getMessage());
  931. try (FileInputStream fis = new FileInputStream(file)) {
  932. IOException ex = assertThrows(IOException.class, () -> ExtractorFactory.createExtractor(fis));
  933. assertEquals("Can't create extractor - unsupported file type: XML", ex.getMessage());
  934. }
  935. ie = assertThrows(IOException.class, () -> ExtractorFactory.createExtractor(file));
  936. assertEquals("Can't create extractor - unsupported file type: XML", ie.getMessage());
  937. }
  938. @Test
  939. void bug65673() throws IOException {
  940. try (XMLSlideShow slideShowModel = openSampleDocument("bug65673.pptx")) {
  941. final XSLFSlide modelSlide = slideShowModel.getSlides().get(0);
  942. try (XMLSlideShow newSlideShow = new XMLSlideShow()) {
  943. XSLFSlide slide = newSlideShow.createSlide().importContent(modelSlide);
  944. assertNotNull(slide);
  945. }
  946. }
  947. }
  948. @Test
  949. void tika2605() throws IOException {
  950. try (XMLSlideShow slideShowModel = openSampleDocument("tika-2605.pptx")) {
  951. for (XSLFSlide slide : slideShowModel.getSlides()) {
  952. assertNotNull(slide);
  953. for (XSLFShape shape : slide.getShapes()) {
  954. assertNotNull(shape);
  955. }
  956. }
  957. }
  958. }
  959. @Test
  960. void loadPptxWithArtisticEffect() throws IOException {
  961. try (XMLSlideShow slideShowModel = openSampleDocument("ArtisticEffectSample.pptx")) {
  962. for (XSLFSlide slide : slideShowModel.getSlides()) {
  963. assertNotNull(slide);
  964. for (XSLFShape shape : slide.getShapes()) {
  965. assertNotNull(shape);
  966. }
  967. }
  968. }
  969. }
  970. @Test
  971. void identicalGradientStopsBug() throws IOException {
  972. final ArrayList<LinearGradientPaint> linearGradients = new ArrayList<>();
  973. final ArrayList<RadialGradientPaint> radialGradients = new ArrayList<>();
  974. final DummyGraphics2d dgfx = new DummyGraphics2d(new NullPrintStream())
  975. {
  976. public void setPaint(final Paint paint) {
  977. if (paint instanceof LinearGradientPaint) {
  978. linearGradients.add((LinearGradientPaint) paint);
  979. }
  980. if (paint instanceof RadialGradientPaint) {
  981. radialGradients.add((RadialGradientPaint) paint);
  982. }
  983. }
  984. };
  985. final List<LinearGradientPaint> expectedLinearGradients = Arrays.asList(
  986. new LinearGradientPaint(new Point2D.Double(30.731732283464567, 138.7317322834646),
  987. new Point2D.Double(122.91549846753813, 46.54796609939099),
  988. new float[] { 0.0f, 0.99999994f, 1.0f },
  989. new Color[] { new Color(81, 124, 252, 255),
  990. new Color(81, 124, 252, 255),
  991. new Color(17,21,27, 204) }),
  992. new LinearGradientPaint(new Point2D.Double(174.7317322834646, 138.73173228346457),
  993. new Point2D.Double(266.9154984675381, 46.547966099391004),
  994. new float[] { 0.0f, 0.00000005f, 1.0f },
  995. new Color[] { new Color(17,21,27, 204),
  996. new Color(81, 124, 252, 255),
  997. new Color(81, 124, 252, 255) }),
  998. new LinearGradientPaint(new Point2D.Double(318.73173228346457, 138.73173228346462),
  999. new Point2D.Double(410.9154984675381, 46.547966099391004),
  1000. new float[] { 0.0f, 0.5f, 0.50000006f, 1.0f },
  1001. new Color[] { new Color(17,21,27, 204),
  1002. new Color(17,21,27, 204),
  1003. new Color(81, 124, 252, 255),
  1004. new Color(81, 124, 252, 255) })
  1005. );
  1006. final List<RadialGradientPaint> expectedRadialGradients = Arrays.asList(
  1007. new RadialGradientPaint(new Point2D.Double(30.731732283464567, 138.7317322834646),
  1008. 108.0f, new Point2D.Double(122.91549846753813, 46.54796609939099),
  1009. new float[] { 0.0f, 0.5f, 0.50000006f, 1.0f },
  1010. new Color[] { new Color(17,21,27, 204),
  1011. new Color(17,21,27, 204),
  1012. new Color(81, 124, 252, 255),
  1013. new Color(81, 124, 252, 255) },
  1014. MultipleGradientPaint.CycleMethod.NO_CYCLE),
  1015. new RadialGradientPaint(new Point2D.Double(228.73173228346457, 226.9755905511811),
  1016. 108.0f, new Point2D.Double(282.73173228346457, 280.9755905511811),
  1017. new float[] { 0.0f, 0.00000005f, 1.0f },
  1018. new Color[] { new Color(17,21,27, 204),
  1019. new Color(81, 124, 252, 255),
  1020. new Color(81, 124, 252, 255) },
  1021. MultipleGradientPaint.CycleMethod.NO_CYCLE),
  1022. new RadialGradientPaint(new Point2D.Double(84.73173228346457, 226.9755905511811),
  1023. 108.0f, new Point2D.Double(138.73173228346457, 280.9755905511811),
  1024. new float[] { 0.0f, 0.99999994f, 1.0f },
  1025. new Color[] { new Color(81, 124, 252, 255),
  1026. new Color(81, 124, 252, 255),
  1027. new Color(17,21,27, 204) },
  1028. MultipleGradientPaint.CycleMethod.NO_CYCLE)
  1029. );
  1030. try (XMLSlideShow slideShowModel = openSampleDocument("minimal-gradient-fill-issue.pptx")) {
  1031. // Render the first (and only) slide.
  1032. slideShowModel.getSlides().get(0).draw(dgfx);
  1033. // Test that the linear gradients have the expected data (stops modified)
  1034. assertEquals(3, linearGradients.size());
  1035. for (int i = 0 ; i < expectedLinearGradients.size() ; i++) {
  1036. final LinearGradientPaint expected = expectedLinearGradients.get(i);
  1037. final LinearGradientPaint actual = linearGradients.get(i);
  1038. assertEquals(expected.getStartPoint(), expected.getStartPoint());
  1039. assertEquals(expected.getEndPoint(), expected.getEndPoint());
  1040. assertArrayEquals(expected.getFractions(), actual.getFractions());
  1041. assertArrayEquals(expected.getColors(), actual.getColors());
  1042. }
  1043. // Test that the radial gradients have the expected data (stops modified)
  1044. assertEquals(3, radialGradients.size());
  1045. for (int i = 0 ; i < expectedRadialGradients.size() ; i++) {
  1046. final RadialGradientPaint expected = expectedRadialGradients.get(i);
  1047. final RadialGradientPaint actual = radialGradients.get(i);
  1048. assertEquals(expected.getCenterPoint(), expected.getCenterPoint());
  1049. assertEquals(expected.getFocusPoint(), expected.getFocusPoint());
  1050. assertArrayEquals(expected.getFractions(), actual.getFractions());
  1051. assertArrayEquals(expected.getColors(), actual.getColors());
  1052. }
  1053. }
  1054. }
  1055. }